blob: e050d68963a126ec06dbe7cfdee2b59722c3071f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_MMZONE_H
2#define _LINUX_MMZONE_H
3
4#ifdef __KERNEL__
5#ifndef __ASSEMBLY__
6
7#include <linux/config.h>
8#include <linux/spinlock.h>
9#include <linux/list.h>
10#include <linux/wait.h>
11#include <linux/cache.h>
12#include <linux/threads.h>
13#include <linux/numa.h>
14#include <linux/init.h>
15#include <asm/atomic.h>
16
17/* Free memory management - zoned buddy allocator. */
18#ifndef CONFIG_FORCE_MAX_ZONEORDER
19#define MAX_ORDER 11
20#else
21#define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
22#endif
23
24struct free_area {
25 struct list_head free_list;
26 unsigned long nr_free;
27};
28
29struct pglist_data;
30
31/*
32 * zone->lock and zone->lru_lock are two of the hottest locks in the kernel.
33 * So add a wild amount of padding here to ensure that they fall into separate
34 * cachelines. There are very few zone structures in the machine, so space
35 * consumption is not a concern here.
36 */
37#if defined(CONFIG_SMP)
38struct zone_padding {
39 char x[0];
40} ____cacheline_maxaligned_in_smp;
41#define ZONE_PADDING(name) struct zone_padding name;
42#else
43#define ZONE_PADDING(name)
44#endif
45
46struct per_cpu_pages {
47 int count; /* number of pages in the list */
48 int low; /* low watermark, refill needed */
49 int high; /* high watermark, emptying needed */
50 int batch; /* chunk size for buddy add/remove */
51 struct list_head list; /* the list of pages */
52};
53
54struct per_cpu_pageset {
55 struct per_cpu_pages pcp[2]; /* 0: hot. 1: cold */
56#ifdef CONFIG_NUMA
57 unsigned long numa_hit; /* allocated in intended node */
58 unsigned long numa_miss; /* allocated in non intended node */
59 unsigned long numa_foreign; /* was intended here, hit elsewhere */
60 unsigned long interleave_hit; /* interleaver prefered this zone */
61 unsigned long local_node; /* allocation from local node */
62 unsigned long other_node; /* allocation from other node */
63#endif
64} ____cacheline_aligned_in_smp;
65
Christoph Lametere7c8d5c2005-06-21 17:14:47 -070066#ifdef CONFIG_NUMA
67#define zone_pcp(__z, __cpu) ((__z)->pageset[(__cpu)])
68#else
69#define zone_pcp(__z, __cpu) (&(__z)->pageset[(__cpu)])
70#endif
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define ZONE_DMA 0
73#define ZONE_NORMAL 1
74#define ZONE_HIGHMEM 2
75
76#define MAX_NR_ZONES 3 /* Sync this with ZONES_SHIFT */
77#define ZONES_SHIFT 2 /* ceil(log2(MAX_NR_ZONES)) */
78
79
80/*
81 * When a memory allocation must conform to specific limitations (such
82 * as being suitable for DMA) the caller will pass in hints to the
83 * allocator in the gfp_mask, in the zone modifier bits. These bits
84 * are used to select a priority ordered list of memory zones which
85 * match the requested limits. GFP_ZONEMASK defines which bits within
86 * the gfp_mask should be considered as zone modifiers. Each valid
87 * combination of the zone modifier bits has a corresponding list
88 * of zones (in node_zonelists). Thus for two zone modifiers there
89 * will be a maximum of 4 (2 ** 2) zonelists, for 3 modifiers there will
90 * be 8 (2 ** 3) zonelists. GFP_ZONETYPES defines the number of possible
91 * combinations of zone modifiers in "zone modifier space".
92 */
93#define GFP_ZONEMASK 0x03
94/*
95 * As an optimisation any zone modifier bits which are only valid when
96 * no other zone modifier bits are set (loners) should be placed in
97 * the highest order bits of this field. This allows us to reduce the
98 * extent of the zonelists thus saving space. For example in the case
99 * of three zone modifier bits, we could require up to eight zonelists.
100 * If the left most zone modifier is a "loner" then the highest valid
101 * zonelist would be four allowing us to allocate only five zonelists.
102 * Use the first form when the left most bit is not a "loner", otherwise
103 * use the second.
104 */
105/* #define GFP_ZONETYPES (GFP_ZONEMASK + 1) */ /* Non-loner */
106#define GFP_ZONETYPES ((GFP_ZONEMASK + 1) / 2 + 1) /* Loner */
107
108/*
109 * On machines where it is needed (eg PCs) we divide physical memory
110 * into multiple physical zones. On a PC we have 3 zones:
111 *
112 * ZONE_DMA < 16 MB ISA DMA capable memory
113 * ZONE_NORMAL 16-896 MB direct mapped by the kernel
114 * ZONE_HIGHMEM > 896 MB only page cache and user processes
115 */
116
117struct zone {
118 /* Fields commonly accessed by the page allocator */
119 unsigned long free_pages;
120 unsigned long pages_min, pages_low, pages_high;
121 /*
122 * We don't know if the memory that we're going to allocate will be freeable
123 * or/and it will be released eventually, so to avoid totally wasting several
124 * GB of ram we must reserve some of the lower zone memory (otherwise we risk
125 * to run OOM on the lower zones despite there's tons of freeable ram
126 * on the higher zones). This array is recalculated at runtime if the
127 * sysctl_lowmem_reserve_ratio sysctl changes.
128 */
129 unsigned long lowmem_reserve[MAX_NR_ZONES];
130
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700131#ifdef CONFIG_NUMA
132 struct per_cpu_pageset *pageset[NR_CPUS];
133#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct per_cpu_pageset pageset[NR_CPUS];
Christoph Lametere7c8d5c2005-06-21 17:14:47 -0700135#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /*
137 * free areas of different sizes
138 */
139 spinlock_t lock;
140 struct free_area free_area[MAX_ORDER];
141
142
143 ZONE_PADDING(_pad1_)
144
145 /* Fields commonly accessed by the page reclaim scanner */
146 spinlock_t lru_lock;
147 struct list_head active_list;
148 struct list_head inactive_list;
149 unsigned long nr_scan_active;
150 unsigned long nr_scan_inactive;
151 unsigned long nr_active;
152 unsigned long nr_inactive;
153 unsigned long pages_scanned; /* since last reclaim */
154 int all_unreclaimable; /* All pages pinned */
155
156 /*
Martin Hicks753ee722005-06-21 17:14:41 -0700157 * Does the allocator try to reclaim pages from the zone as soon
158 * as it fails a watermark_ok() in __alloc_pages?
159 */
160 int reclaim_pages;
Martin Hicks1e7e5a92005-06-21 17:14:43 -0700161 /* A count of how many reclaimers are scanning this zone */
162 atomic_t reclaim_in_progress;
Martin Hicks753ee722005-06-21 17:14:41 -0700163
164 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * prev_priority holds the scanning priority for this zone. It is
166 * defined as the scanning priority at which we achieved our reclaim
167 * target at the previous try_to_free_pages() or balance_pgdat()
168 * invokation.
169 *
170 * We use prev_priority as a measure of how much stress page reclaim is
171 * under - it drives the swappiness decision: whether to unmap mapped
172 * pages.
173 *
174 * temp_priority is used to remember the scanning priority at which
175 * this zone was successfully refilled to free_pages == pages_high.
176 *
177 * Access to both these fields is quite racy even on uniprocessor. But
178 * it is expected to average out OK.
179 */
180 int temp_priority;
181 int prev_priority;
182
183
184 ZONE_PADDING(_pad2_)
185 /* Rarely used or read-mostly fields */
186
187 /*
188 * wait_table -- the array holding the hash table
189 * wait_table_size -- the size of the hash table array
190 * wait_table_bits -- wait_table_size == (1 << wait_table_bits)
191 *
192 * The purpose of all these is to keep track of the people
193 * waiting for a page to become available and make them
194 * runnable again when possible. The trouble is that this
195 * consumes a lot of space, especially when so few things
196 * wait on pages at a given time. So instead of using
197 * per-page waitqueues, we use a waitqueue hash table.
198 *
199 * The bucket discipline is to sleep on the same queue when
200 * colliding and wake all in that wait queue when removing.
201 * When something wakes, it must check to be sure its page is
202 * truly available, a la thundering herd. The cost of a
203 * collision is great, but given the expected load of the
204 * table, they should be so rare as to be outweighed by the
205 * benefits from the saved space.
206 *
207 * __wait_on_page_locked() and unlock_page() in mm/filemap.c, are the
208 * primary users of these fields, and in mm/page_alloc.c
209 * free_area_init_core() performs the initialization of them.
210 */
211 wait_queue_head_t * wait_table;
212 unsigned long wait_table_size;
213 unsigned long wait_table_bits;
214
215 /*
216 * Discontig memory support fields.
217 */
218 struct pglist_data *zone_pgdat;
219 struct page *zone_mem_map;
220 /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
221 unsigned long zone_start_pfn;
222
223 unsigned long spanned_pages; /* total size, including holes */
224 unsigned long present_pages; /* amount of memory (excluding holes) */
225
226 /*
227 * rarely used fields:
228 */
229 char *name;
230} ____cacheline_maxaligned_in_smp;
231
232
233/*
234 * The "priority" of VM scanning is how much of the queues we will scan in one
235 * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
236 * queues ("queue_length >> 12") during an aging round.
237 */
238#define DEF_PRIORITY 12
239
240/*
241 * One allocation request operates on a zonelist. A zonelist
242 * is a list of zones, the first one is the 'goal' of the
243 * allocation, the other zones are fallback zones, in decreasing
244 * priority.
245 *
246 * Right now a zonelist takes up less than a cacheline. We never
247 * modify it apart from boot-up, and only a few indices are used,
248 * so despite the zonelist table being relatively big, the cache
249 * footprint of this construct is very small.
250 */
251struct zonelist {
252 struct zone *zones[MAX_NUMNODES * MAX_NR_ZONES + 1]; // NULL delimited
253};
254
255
256/*
257 * The pg_data_t structure is used in machines with CONFIG_DISCONTIGMEM
258 * (mostly NUMA machines?) to denote a higher-level memory zone than the
259 * zone denotes.
260 *
261 * On NUMA machines, each NUMA node would have a pg_data_t to describe
262 * it's memory layout.
263 *
264 * Memory statistics and page replacement data structures are maintained on a
265 * per-zone basis.
266 */
267struct bootmem_data;
268typedef struct pglist_data {
269 struct zone node_zones[MAX_NR_ZONES];
270 struct zonelist node_zonelists[GFP_ZONETYPES];
271 int nr_zones;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700272#ifdef CONFIG_FLAT_NODE_MEM_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct page *node_mem_map;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700274#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct bootmem_data *bdata;
Dave Hansen208d54e2005-10-29 18:16:52 -0700276#ifdef CONFIG_MEMORY_HOTPLUG
277 /*
278 * Must be held any time you expect node_start_pfn, node_present_pages
279 * or node_spanned_pages stay constant. Holding this will also
280 * guarantee that any pfn_valid() stays that way.
281 *
282 * Nests above zone->lock and zone->size_seqlock.
283 */
284 spinlock_t node_size_lock;
285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 unsigned long node_start_pfn;
287 unsigned long node_present_pages; /* total number of physical pages */
288 unsigned long node_spanned_pages; /* total size of physical page
289 range, including holes */
290 int node_id;
291 struct pglist_data *pgdat_next;
292 wait_queue_head_t kswapd_wait;
293 struct task_struct *kswapd;
294 int kswapd_max_order;
295} pg_data_t;
296
297#define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
298#define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700299#ifdef CONFIG_FLAT_NODE_MEM_MAP
Dave Hansen408fde82005-06-23 00:07:37 -0700300#define pgdat_page_nr(pgdat, pagenr) ((pgdat)->node_mem_map + (pagenr))
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700301#else
302#define pgdat_page_nr(pgdat, pagenr) pfn_to_page((pgdat)->node_start_pfn + (pagenr))
303#endif
Dave Hansen408fde82005-06-23 00:07:37 -0700304#define nid_page_nr(nid, pagenr) pgdat_page_nr(NODE_DATA(nid),(pagenr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Dave Hansen208d54e2005-10-29 18:16:52 -0700306#include <linux/memory_hotplug.h>
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308extern struct pglist_data *pgdat_list;
309
310void __get_zone_counts(unsigned long *active, unsigned long *inactive,
311 unsigned long *free, struct pglist_data *pgdat);
312void get_zone_counts(unsigned long *active, unsigned long *inactive,
313 unsigned long *free);
314void build_all_zonelists(void);
315void wakeup_kswapd(struct zone *zone, int order);
316int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
Al Viro260b2362005-10-21 03:22:44 -0400317 int alloc_type, int can_try_harder, gfp_t gfp_high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319#ifdef CONFIG_HAVE_MEMORY_PRESENT
320void memory_present(int nid, unsigned long start, unsigned long end);
321#else
322static inline void memory_present(int nid, unsigned long start, unsigned long end) {}
323#endif
324
325#ifdef CONFIG_NEED_NODE_MEMMAP_SIZE
326unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
327#endif
328
329/*
330 * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
331 */
332#define zone_idx(zone) ((zone) - (zone)->zone_pgdat->node_zones)
333
334/**
335 * for_each_pgdat - helper macro to iterate over all nodes
336 * @pgdat - pointer to a pg_data_t variable
337 *
338 * Meant to help with common loops of the form
339 * pgdat = pgdat_list;
340 * while(pgdat) {
341 * ...
342 * pgdat = pgdat->pgdat_next;
343 * }
344 */
345#define for_each_pgdat(pgdat) \
346 for (pgdat = pgdat_list; pgdat; pgdat = pgdat->pgdat_next)
347
348/*
349 * next_zone - helper magic for for_each_zone()
350 * Thanks to William Lee Irwin III for this piece of ingenuity.
351 */
352static inline struct zone *next_zone(struct zone *zone)
353{
354 pg_data_t *pgdat = zone->zone_pgdat;
355
356 if (zone < pgdat->node_zones + MAX_NR_ZONES - 1)
357 zone++;
358 else if (pgdat->pgdat_next) {
359 pgdat = pgdat->pgdat_next;
360 zone = pgdat->node_zones;
361 } else
362 zone = NULL;
363
364 return zone;
365}
366
367/**
368 * for_each_zone - helper macro to iterate over all memory zones
369 * @zone - pointer to struct zone variable
370 *
371 * The user only needs to declare the zone variable, for_each_zone
372 * fills it in. This basically means for_each_zone() is an
373 * easier to read version of this piece of code:
374 *
375 * for (pgdat = pgdat_list; pgdat; pgdat = pgdat->node_next)
376 * for (i = 0; i < MAX_NR_ZONES; ++i) {
377 * struct zone * z = pgdat->node_zones + i;
378 * ...
379 * }
380 * }
381 */
382#define for_each_zone(zone) \
383 for (zone = pgdat_list->node_zones; zone; zone = next_zone(zone))
384
385static inline int is_highmem_idx(int idx)
386{
387 return (idx == ZONE_HIGHMEM);
388}
389
390static inline int is_normal_idx(int idx)
391{
392 return (idx == ZONE_NORMAL);
393}
394/**
395 * is_highmem - helper function to quickly check if a struct zone is a
396 * highmem zone or not. This is an attempt to keep references
397 * to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
398 * @zone - pointer to struct zone variable
399 */
400static inline int is_highmem(struct zone *zone)
401{
402 return zone == zone->zone_pgdat->node_zones + ZONE_HIGHMEM;
403}
404
405static inline int is_normal(struct zone *zone)
406{
407 return zone == zone->zone_pgdat->node_zones + ZONE_NORMAL;
408}
409
410/* These two functions are used to setup the per zone pages min values */
411struct ctl_table;
412struct file;
413int min_free_kbytes_sysctl_handler(struct ctl_table *, int, struct file *,
414 void __user *, size_t *, loff_t *);
415extern int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1];
416int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *, int, struct file *,
417 void __user *, size_t *, loff_t *);
418
419#include <linux/topology.h>
420/* Returns the number of the current Node. */
Ingo Molnar39c715b2005-06-21 17:14:34 -0700421#define numa_node_id() (cpu_to_node(raw_smp_processor_id()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Dave Hansen93b75042005-06-23 00:07:47 -0700423#ifndef CONFIG_NEED_MULTIPLE_NODES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425extern struct pglist_data contig_page_data;
426#define NODE_DATA(nid) (&contig_page_data)
427#define NODE_MEM_MAP(nid) mem_map
428#define MAX_NODES_SHIFT 1
429#define pfn_to_nid(pfn) (0)
430
Dave Hansen93b75042005-06-23 00:07:47 -0700431#else /* CONFIG_NEED_MULTIPLE_NODES */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433#include <asm/mmzone.h>
434
Dave Hansen93b75042005-06-23 00:07:47 -0700435#endif /* !CONFIG_NEED_MULTIPLE_NODES */
Dave Hansen348f8b62005-06-23 00:07:40 -0700436
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700437#ifdef CONFIG_SPARSEMEM
438#include <asm/sparsemem.h>
439#endif
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#if BITS_PER_LONG == 32 || defined(ARCH_HAS_ATOMIC_UNSIGNED)
442/*
443 * with 32 bit page->flags field, we reserve 8 bits for node/zone info.
444 * there are 3 zones (2 bits) and this leaves 8-2=6 bits for nodes.
445 */
Dave Hansen348f8b62005-06-23 00:07:40 -0700446#define FLAGS_RESERVED 8
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448#elif BITS_PER_LONG == 64
449/*
450 * with 64 bit flags field, there's plenty of room.
451 */
Dave Hansen348f8b62005-06-23 00:07:40 -0700452#define FLAGS_RESERVED 32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Dave Hansen348f8b62005-06-23 00:07:40 -0700454#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dave Hansen348f8b62005-06-23 00:07:40 -0700456#error BITS_PER_LONG not defined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#endif
459
Andy Whitcroftb159d432005-06-23 00:07:52 -0700460#ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
461#define early_pfn_to_nid(nid) (0UL)
462#endif
463
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700464#define pfn_to_section_nr(pfn) ((pfn) >> PFN_SECTION_SHIFT)
465#define section_nr_to_pfn(sec) ((sec) << PFN_SECTION_SHIFT)
466
467#ifdef CONFIG_SPARSEMEM
468
469/*
470 * SECTION_SHIFT #bits space required to store a section #
471 *
472 * PA_SECTION_SHIFT physical address to/from section number
473 * PFN_SECTION_SHIFT pfn to/from section number
474 */
475#define SECTIONS_SHIFT (MAX_PHYSMEM_BITS - SECTION_SIZE_BITS)
476
477#define PA_SECTION_SHIFT (SECTION_SIZE_BITS)
478#define PFN_SECTION_SHIFT (SECTION_SIZE_BITS - PAGE_SHIFT)
479
480#define NR_MEM_SECTIONS (1UL << SECTIONS_SHIFT)
481
482#define PAGES_PER_SECTION (1UL << PFN_SECTION_SHIFT)
483#define PAGE_SECTION_MASK (~(PAGES_PER_SECTION-1))
484
485#if (MAX_ORDER - 1 + PAGE_SHIFT) > SECTION_SIZE_BITS
486#error Allocator MAX_ORDER exceeds SECTION_SIZE
487#endif
488
489struct page;
490struct mem_section {
Andy Whitcroft29751f62005-06-23 00:08:00 -0700491 /*
492 * This is, logically, a pointer to an array of struct
493 * pages. However, it is stored with some other magic.
494 * (see sparse.c::sparse_init_one_section())
495 *
496 * Making it a UL at least makes someone do a cast
497 * before using it wrong.
498 */
499 unsigned long section_mem_map;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700500};
501
Bob Picco3e347262005-09-03 15:54:28 -0700502#ifdef CONFIG_SPARSEMEM_EXTREME
503#define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section))
Bob Picco802f1922005-09-03 15:54:26 -0700504#else
Bob Picco3e347262005-09-03 15:54:28 -0700505#define SECTIONS_PER_ROOT 1
506#endif
Bob Picco802f1922005-09-03 15:54:26 -0700507
Bob Picco3e347262005-09-03 15:54:28 -0700508#define SECTION_NR_TO_ROOT(sec) ((sec) / SECTIONS_PER_ROOT)
509#define NR_SECTION_ROOTS (NR_MEM_SECTIONS / SECTIONS_PER_ROOT)
510#define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1)
511
512#ifdef CONFIG_SPARSEMEM_EXTREME
513extern struct mem_section *mem_section[NR_SECTION_ROOTS];
514#else
515extern struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT];
516#endif
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700517
Andy Whitcroft29751f62005-06-23 00:08:00 -0700518static inline struct mem_section *__nr_to_section(unsigned long nr)
519{
Bob Picco3e347262005-09-03 15:54:28 -0700520 if (!mem_section[SECTION_NR_TO_ROOT(nr)])
521 return NULL;
522 return &mem_section[SECTION_NR_TO_ROOT(nr)][nr & SECTION_ROOT_MASK];
Andy Whitcroft29751f62005-06-23 00:08:00 -0700523}
Dave Hansen4ca644d2005-10-29 18:16:51 -0700524extern int __section_nr(struct mem_section* ms);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700525
526/*
527 * We use the lower bits of the mem_map pointer to store
528 * a little bit of information. There should be at least
529 * 3 bits here due to 32-bit alignment.
530 */
531#define SECTION_MARKED_PRESENT (1UL<<0)
532#define SECTION_HAS_MEM_MAP (1UL<<1)
533#define SECTION_MAP_LAST_BIT (1UL<<2)
534#define SECTION_MAP_MASK (~(SECTION_MAP_LAST_BIT-1))
535
536static inline struct page *__section_mem_map_addr(struct mem_section *section)
537{
538 unsigned long map = section->section_mem_map;
539 map &= SECTION_MAP_MASK;
540 return (struct page *)map;
541}
542
543static inline int valid_section(struct mem_section *section)
544{
Bob Picco802f1922005-09-03 15:54:26 -0700545 return (section && (section->section_mem_map & SECTION_MARKED_PRESENT));
Andy Whitcroft29751f62005-06-23 00:08:00 -0700546}
547
548static inline int section_has_mem_map(struct mem_section *section)
549{
Bob Picco802f1922005-09-03 15:54:26 -0700550 return (section && (section->section_mem_map & SECTION_HAS_MEM_MAP));
Andy Whitcroft29751f62005-06-23 00:08:00 -0700551}
552
553static inline int valid_section_nr(unsigned long nr)
554{
555 return valid_section(__nr_to_section(nr));
556}
557
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700558/*
559 * Given a kernel address, find the home node of the underlying memory.
560 */
561#define kvaddr_to_nid(kaddr) pfn_to_nid(__pa(kaddr) >> PAGE_SHIFT)
562
563static inline struct mem_section *__pfn_to_section(unsigned long pfn)
564{
Andy Whitcroft29751f62005-06-23 00:08:00 -0700565 return __nr_to_section(pfn_to_section_nr(pfn));
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700566}
567
568#define pfn_to_page(pfn) \
569({ \
570 unsigned long __pfn = (pfn); \
Andy Whitcroft29751f62005-06-23 00:08:00 -0700571 __section_mem_map_addr(__pfn_to_section(__pfn)) + __pfn; \
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700572})
573#define page_to_pfn(page) \
574({ \
Andy Whitcroft29751f62005-06-23 00:08:00 -0700575 page - __section_mem_map_addr(__nr_to_section( \
576 page_to_section(page))); \
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700577})
578
579static inline int pfn_valid(unsigned long pfn)
580{
581 if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
582 return 0;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700583 return valid_section(__nr_to_section(pfn_to_section_nr(pfn)));
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700584}
585
586/*
587 * These are _only_ used during initialisation, therefore they
588 * can use __initdata ... They could have names to indicate
589 * this restriction.
590 */
591#ifdef CONFIG_NUMA
592#define pfn_to_nid early_pfn_to_nid
593#endif
594
595#define pfn_to_pgdat(pfn) \
596({ \
597 NODE_DATA(pfn_to_nid(pfn)); \
598})
599
600#define early_pfn_valid(pfn) pfn_valid(pfn)
601void sparse_init(void);
602#else
603#define sparse_init() do {} while (0)
Dave Hansen28ae55c2005-09-03 15:54:29 -0700604#define sparse_index_init(_sec, _nid) do {} while (0)
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700605#endif /* CONFIG_SPARSEMEM */
606
Andy Whitcroft641c7672005-06-23 00:07:59 -0700607#ifdef CONFIG_NODES_SPAN_OTHER_NODES
608#define early_pfn_in_nid(pfn, nid) (early_pfn_to_nid(pfn) == (nid))
609#else
610#define early_pfn_in_nid(pfn, nid) (1)
611#endif
612
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700613#ifndef early_pfn_valid
614#define early_pfn_valid(pfn) (1)
615#endif
616
617void memory_present(int nid, unsigned long start, unsigned long end);
618unsigned long __init node_memmap_size_bytes(int, unsigned long, unsigned long);
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620#endif /* !__ASSEMBLY__ */
621#endif /* __KERNEL__ */
622#endif /* _LINUX_MMZONE_H */