http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / kernel / linux / include / linux / mmzone.h
1 #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 <asm/atomic.h>
15
16 /* Free memory management - zoned buddy allocator.  */
17 #ifndef CONFIG_FORCE_MAX_ZONEORDER
18 #define MAX_ORDER 11
19 #else
20 #define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
21 #endif
22
23 /*
24  * system hash table size limits
25  * - on large memory machines, we may want to allocate a bigger hash than that
26  *   permitted by MAX_ORDER, so we allocate with the bootmem allocator, and are
27  *   limited to this size
28  */
29 #if MAX_ORDER > 14
30 #define MAX_SYS_HASH_TABLE_ORDER MAX_ORDER
31 #else
32 #define MAX_SYS_HASH_TABLE_ORDER 14
33 #endif
34
35 struct free_area {
36         struct list_head        free_list;
37         unsigned long           *map;
38 };
39
40 struct pglist_data;
41
42 /*
43  * zone->lock and zone->lru_lock are two of the hottest locks in the kernel.
44  * So add a wild amount of padding here to ensure that they fall into separate
45  * cachelines.  There are very few zone structures in the machine, so space
46  * consumption is not a concern here.
47  */
48 #if defined(CONFIG_SMP)
49 struct zone_padding {
50         int x;
51 } ____cacheline_maxaligned_in_smp;
52 #define ZONE_PADDING(name)      struct zone_padding name;
53 #else
54 #define ZONE_PADDING(name)
55 #endif
56
57 struct per_cpu_pages {
58         int count;              /* number of pages in the list */
59         int low;                /* low watermark, refill needed */
60         int high;               /* high watermark, emptying needed */
61         int batch;              /* chunk size for buddy add/remove */
62         struct list_head list;  /* the list of pages */
63 };
64
65 struct per_cpu_pageset {
66         struct per_cpu_pages pcp[2];    /* 0: hot.  1: cold */
67 #ifdef CONFIG_NUMA
68         unsigned long numa_hit;         /* allocated in intended node */
69         unsigned long numa_miss;        /* allocated in non intended node */
70         unsigned long numa_foreign;     /* was intended here, hit elsewhere */
71         unsigned long interleave_hit;   /* interleaver prefered this zone */
72         unsigned long local_node;       /* allocation from local node */
73         unsigned long other_node;       /* allocation from other node */
74 #endif
75 } ____cacheline_aligned_in_smp;
76
77 #define ZONE_DMA                0
78 #define ZONE_NORMAL             1
79 #define ZONE_HIGHMEM            2
80
81 #define MAX_NR_ZONES            3       /* Sync this with ZONES_SHIFT */
82 #define ZONES_SHIFT             2       /* ceil(log2(MAX_NR_ZONES)) */
83
84
85 /*
86  * When a memory allocation must conform to specific limitations (such
87  * as being suitable for DMA) the caller will pass in hints to the
88  * allocator in the gfp_mask, in the zone modifier bits.  These bits
89  * are used to select a priority ordered list of memory zones which
90  * match the requested limits.  GFP_ZONEMASK defines which bits within
91  * the gfp_mask should be considered as zone modifiers.  Each valid
92  * combination of the zone modifier bits has a corresponding list
93  * of zones (in node_zonelists).  Thus for two zone modifiers there
94  * will be a maximum of 4 (2 ** 2) zonelists, for 3 modifiers there will
95  * be 8 (2 ** 3) zonelists.  GFP_ZONETYPES defines the number of possible
96  * combinations of zone modifiers in "zone modifier space".
97  */
98 #define GFP_ZONEMASK    0x03
99 /*
100  * As an optimisation any zone modifier bits which are only valid when
101  * no other zone modifier bits are set (loners) should be placed in
102  * the highest order bits of this field.  This allows us to reduce the
103  * extent of the zonelists thus saving space.  For example in the case
104  * of three zone modifier bits, we could require up to eight zonelists.
105  * If the left most zone modifier is a "loner" then the highest valid
106  * zonelist would be four allowing us to allocate only five zonelists.
107  * Use the first form when the left most bit is not a "loner", otherwise
108  * use the second.
109  */
110 /* #define GFP_ZONETYPES        (GFP_ZONEMASK + 1) */           /* Non-loner */
111 #define GFP_ZONETYPES   ((GFP_ZONEMASK + 1) / 2 + 1)            /* Loner */
112
113 /*
114  * On machines where it is needed (eg PCs) we divide physical memory
115  * into multiple physical zones. On a PC we have 3 zones:
116  *
117  * ZONE_DMA       < 16 MB       ISA DMA capable memory
118  * ZONE_NORMAL  16-896 MB       direct mapped by the kernel
119  * ZONE_HIGHMEM  > 896 MB       only page cache and user processes
120  */
121
122 struct zone {
123         /*
124          * Commonly accessed fields:
125          */
126         spinlock_t              lock;
127         unsigned long           free_pages;
128         unsigned long           pages_min, pages_low, pages_high;
129         /*
130          * protection[] is a pre-calculated number of extra pages that must be
131          * available in a zone in order for __alloc_pages() to allocate memory
132          * from the zone. i.e., for a GFP_KERNEL alloc of "order" there must
133          * be "(1<<order) + protection[ZONE_NORMAL]" free pages in the zone
134          * for us to choose to allocate the page from that zone.
135          *
136          * It uses both min_free_kbytes and sysctl_lower_zone_protection.
137          * The protection values are recalculated if either of these values
138          * change.  The array elements are in zonelist order:
139          *      [0] == GFP_DMA, [1] == GFP_KERNEL, [2] == GFP_HIGHMEM.
140          */
141         unsigned long           protection[MAX_NR_ZONES];
142
143         ZONE_PADDING(_pad1_)
144
145         spinlock_t              lru_lock;       
146         struct list_head        active_list;
147         struct list_head        inactive_list;
148         unsigned long           nr_scan_active;
149         unsigned long           nr_scan_inactive;
150         unsigned long           nr_active;
151         unsigned long           nr_inactive;
152         int                     all_unreclaimable; /* All pages pinned */
153         unsigned long           pages_scanned;     /* since last reclaim */
154
155         ZONE_PADDING(_pad2_)
156
157         /*
158          * prev_priority holds the scanning priority for this zone.  It is
159          * defined as the scanning priority at which we achieved our reclaim
160          * target at the previous try_to_free_pages() or balance_pgdat()
161          * invokation.
162          *
163          * We use prev_priority as a measure of how much stress page reclaim is
164          * under - it drives the swappiness decision: whether to unmap mapped
165          * pages.
166          *
167          * temp_priority is used to remember the scanning priority at which
168          * this zone was successfully refilled to free_pages == pages_high.
169          *
170          * Access to both these fields is quite racy even on uniprocessor.  But
171          * it is expected to average out OK.
172          */
173         int temp_priority;
174         int prev_priority;
175
176         /*
177          * free areas of different sizes
178          */
179         struct free_area        free_area[MAX_ORDER];
180
181         /*
182          * wait_table           -- the array holding the hash table
183          * wait_table_size      -- the size of the hash table array
184          * wait_table_bits      -- wait_table_size == (1 << wait_table_bits)
185          *
186          * The purpose of all these is to keep track of the people
187          * waiting for a page to become available and make them
188          * runnable again when possible. The trouble is that this
189          * consumes a lot of space, especially when so few things
190          * wait on pages at a given time. So instead of using
191          * per-page waitqueues, we use a waitqueue hash table.
192          *
193          * The bucket discipline is to sleep on the same queue when
194          * colliding and wake all in that wait queue when removing.
195          * When something wakes, it must check to be sure its page is
196          * truly available, a la thundering herd. The cost of a
197          * collision is great, but given the expected load of the
198          * table, they should be so rare as to be outweighed by the
199          * benefits from the saved space.
200          *
201          * __wait_on_page_locked() and unlock_page() in mm/filemap.c, are the
202          * primary users of these fields, and in mm/page_alloc.c
203          * free_area_init_core() performs the initialization of them.
204          */
205         wait_queue_head_t       * wait_table;
206         unsigned long           wait_table_size;
207         unsigned long           wait_table_bits;
208
209         ZONE_PADDING(_pad3_)
210
211         struct per_cpu_pageset  pageset[NR_CPUS];
212
213         /*
214          * Discontig memory support fields.
215          */
216         struct pglist_data      *zone_pgdat;
217         struct page             *zone_mem_map;
218         /* zone_start_pfn == zone_start_paddr >> PAGE_SHIFT */
219         unsigned long           zone_start_pfn;
220
221         /*
222          * rarely used fields:
223          */
224         char                    *name;
225         unsigned long           spanned_pages;  /* total size, including holes */
226         unsigned long           present_pages;  /* amount of memory (excluding holes) */
227 } ____cacheline_maxaligned_in_smp;
228
229
230 /*
231  * The "priority" of VM scanning is how much of the queues we will scan in one
232  * go. A value of 12 for DEF_PRIORITY implies that we will scan 1/4096th of the
233  * queues ("queue_length >> 12") during an aging round.
234  */
235
236 #if 0
237 #define DEF_PRIORITY 12
238 #else
239 /* No swap disk, not possible to swap out pages. Start from highest priority to scan more area for free pages*/
240 #define DEF_PRIORITY 0
241 #endif
242
243 /*
244  * One allocation request operates on a zonelist. A zonelist
245  * is a list of zones, the first one is the 'goal' of the
246  * allocation, the other zones are fallback zones, in decreasing
247  * priority.
248  *
249  * Right now a zonelist takes up less than a cacheline. We never
250  * modify it apart from boot-up, and only a few indices are used,
251  * so despite the zonelist table being relatively big, the cache
252  * footprint of this construct is very small.
253  */
254 struct zonelist {
255         struct zone *zones[MAX_NUMNODES * MAX_NR_ZONES + 1]; // NULL delimited
256 };
257
258
259 /*
260  * The pg_data_t structure is used in machines with CONFIG_DISCONTIGMEM
261  * (mostly NUMA machines?) to denote a higher-level memory zone than the
262  * zone denotes.
263  *
264  * On NUMA machines, each NUMA node would have a pg_data_t to describe
265  * it's memory layout.
266  *
267  * Memory statistics and page replacement data structures are maintained on a
268  * per-zone basis.
269  */
270 struct bootmem_data;
271 typedef struct pglist_data {
272         struct zone node_zones[MAX_NR_ZONES];
273         struct zonelist node_zonelists[GFP_ZONETYPES];
274         int nr_zones;
275         struct page *node_mem_map;
276         struct bootmem_data *bdata;
277         unsigned long node_start_pfn;
278         unsigned long node_present_pages; /* total number of physical pages */
279         unsigned long node_spanned_pages; /* total size of physical page
280                                              range, including holes */
281         int node_id;
282         struct pglist_data *pgdat_next;
283         wait_queue_head_t       kswapd_wait;
284         struct task_struct *kswapd;
285 } pg_data_t;
286
287 #define node_present_pages(nid) (NODE_DATA(nid)->node_present_pages)
288 #define node_spanned_pages(nid) (NODE_DATA(nid)->node_spanned_pages)
289
290 extern int numnodes;
291 extern struct pglist_data *pgdat_list;
292
293 void get_zone_counts(unsigned long *active, unsigned long *inactive,
294                         unsigned long *free);
295 void build_all_zonelists(void);
296 void wakeup_kswapd(struct zone *zone);
297
298 /*
299  * zone_idx() returns 0 for the ZONE_DMA zone, 1 for the ZONE_NORMAL zone, etc.
300  */
301 #define zone_idx(zone)          ((zone) - (zone)->zone_pgdat->node_zones)
302
303 /**
304  * for_each_pgdat - helper macro to iterate over all nodes
305  * @pgdat - pointer to a pg_data_t variable
306  *
307  * Meant to help with common loops of the form
308  * pgdat = pgdat_list;
309  * while(pgdat) {
310  *      ...
311  *      pgdat = pgdat->pgdat_next;
312  * }
313  */
314 #define for_each_pgdat(pgdat) \
315         for (pgdat = pgdat_list; pgdat; pgdat = pgdat->pgdat_next)
316
317 /*
318  * next_zone - helper magic for for_each_zone()
319  * Thanks to William Lee Irwin III for this piece of ingenuity.
320  */
321 static inline struct zone *next_zone(struct zone *zone)
322 {
323         pg_data_t *pgdat = zone->zone_pgdat;
324
325         if (zone - pgdat->node_zones < MAX_NR_ZONES - 1)
326                 zone++;
327         else if (pgdat->pgdat_next) {
328                 pgdat = pgdat->pgdat_next;
329                 zone = pgdat->node_zones;
330         } else
331                 zone = NULL;
332
333         return zone;
334 }
335
336 /**
337  * for_each_zone - helper macro to iterate over all memory zones
338  * @zone - pointer to struct zone variable
339  *
340  * The user only needs to declare the zone variable, for_each_zone
341  * fills it in. This basically means for_each_zone() is an
342  * easier to read version of this piece of code:
343  *
344  * for (pgdat = pgdat_list; pgdat; pgdat = pgdat->node_next)
345  *      for (i = 0; i < MAX_NR_ZONES; ++i) {
346  *              struct zone * z = pgdat->node_zones + i;
347  *              ...
348  *      }
349  * }
350  */
351 #define for_each_zone(zone) \
352         for (zone = pgdat_list->node_zones; zone; zone = next_zone(zone))
353
354 static inline int is_highmem_idx(int idx)
355 {
356         return (idx == ZONE_HIGHMEM);
357 }
358
359 static inline int is_normal_idx(int idx)
360 {
361         return (idx == ZONE_NORMAL);
362 }
363 /**
364  * is_highmem - helper function to quickly check if a struct zone is a 
365  *              highmem zone or not.  This is an attempt to keep references
366  *              to ZONE_{DMA/NORMAL/HIGHMEM/etc} in general code to a minimum.
367  * @zone - pointer to struct zone variable
368  */
369 static inline int is_highmem(struct zone *zone)
370 {
371         return (is_highmem_idx(zone - zone->zone_pgdat->node_zones));
372 }
373
374 static inline int is_normal(struct zone *zone)
375 {
376         return (is_normal_idx(zone - zone->zone_pgdat->node_zones));
377 }
378
379 /* These two functions are used to setup the per zone pages min values */
380 struct ctl_table;
381 struct file;
382 int min_free_kbytes_sysctl_handler(struct ctl_table *, int, struct file *, 
383                                         void __user *, size_t *, loff_t *);
384 int lower_zone_protection_sysctl_handler(struct ctl_table *, int, struct file *,
385                                         void __user *, size_t *, loff_t *);
386
387 #include <linux/topology.h>
388 /* Returns the number of the current Node. */
389 #define numa_node_id()          (cpu_to_node(smp_processor_id()))
390
391 #ifndef CONFIG_DISCONTIGMEM
392
393 extern struct pglist_data contig_page_data;
394 #define NODE_DATA(nid)          (&contig_page_data)
395 #define NODE_MEM_MAP(nid)       mem_map
396 #define MAX_NODES_SHIFT         1
397 #define pfn_to_nid(pfn)         (0)
398
399 #else /* CONFIG_DISCONTIGMEM */
400
401 #include <asm/mmzone.h>
402
403 #if BITS_PER_LONG == 32 || defined(ARCH_HAS_ATOMIC_UNSIGNED)
404 /*
405  * with 32 bit page->flags field, we reserve 8 bits for node/zone info.
406  * there are 3 zones (2 bits) and this leaves 8-2=6 bits for nodes.
407  */
408 #define MAX_NODES_SHIFT         6
409 #elif BITS_PER_LONG == 64
410 /*
411  * with 64 bit flags field, there's plenty of room.
412  */
413 #define MAX_NODES_SHIFT         10
414 #endif
415
416 #endif /* !CONFIG_DISCONTIGMEM */
417
418 #if NODES_SHIFT > MAX_NODES_SHIFT
419 #error NODES_SHIFT > MAX_NODES_SHIFT
420 #endif
421
422 /* There are currently 3 zones: DMA, Normal & Highmem, thus we need 2 bits */
423 #define MAX_ZONES_SHIFT         2
424
425 #if ZONES_SHIFT > MAX_ZONES_SHIFT
426 #error ZONES_SHIFT > MAX_ZONES_SHIFT
427 #endif
428
429 extern DECLARE_BITMAP(node_online_map, MAX_NUMNODES);
430
431 #if defined(CONFIG_DISCONTIGMEM) || defined(CONFIG_NUMA)
432
433 #define node_online(node)       test_bit(node, node_online_map)
434 #define node_set_online(node)   set_bit(node, node_online_map)
435 #define node_set_offline(node)  clear_bit(node, node_online_map)
436 static inline unsigned int num_online_nodes(void)
437 {
438         int i, num = 0;
439
440         for(i = 0; i < MAX_NUMNODES; i++){
441                 if (node_online(i))
442                         num++;
443         }
444         return num;
445 }
446
447 #else /* !CONFIG_DISCONTIGMEM && !CONFIG_NUMA */
448
449 #define node_online(node) \
450         ({ BUG_ON((node) != 0); test_bit(node, node_online_map); })
451 #define node_set_online(node) \
452         ({ BUG_ON((node) != 0); set_bit(node, node_online_map); })
453 #define node_set_offline(node) \
454         ({ BUG_ON((node) != 0); clear_bit(node, node_online_map); })
455 #define num_online_nodes()      1
456
457 #endif /* CONFIG_DISCONTIGMEM || CONFIG_NUMA */
458 #endif /* !__ASSEMBLY__ */
459 #endif /* __KERNEL__ */
460 #endif /* _LINUX_MMZONE_H */