ACPI: thinkpad-acpi: minor refactor on radio switch init
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / sparse.c
blob1facdff257d9f73a65c9752376a06795f6ba9334
1 /*
2 * sparse memory mappings.
3 */
4 #include <linux/mm.h>
5 #include <linux/mmzone.h>
6 #include <linux/bootmem.h>
7 #include <linux/highmem.h>
8 #include <linux/module.h>
9 #include <linux/spinlock.h>
10 #include <linux/vmalloc.h>
11 #include <asm/dma.h>
14 * Permanent SPARSEMEM data:
16 * 1) mem_section - memory sections, mem_map's for valid memory
18 #ifdef CONFIG_SPARSEMEM_EXTREME
19 struct mem_section *mem_section[NR_SECTION_ROOTS]
20 ____cacheline_internodealigned_in_smp;
21 #else
22 struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
23 ____cacheline_internodealigned_in_smp;
24 #endif
25 EXPORT_SYMBOL(mem_section);
27 #ifdef NODE_NOT_IN_PAGE_FLAGS
29 * If we did not store the node number in the page then we have to
30 * do a lookup in the section_to_node_table in order to find which
31 * node the page belongs to.
33 #if MAX_NUMNODES <= 256
34 static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
35 #else
36 static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
37 #endif
39 int page_to_nid(struct page *page)
41 return section_to_node_table[page_to_section(page)];
43 EXPORT_SYMBOL(page_to_nid);
45 static void set_section_nid(unsigned long section_nr, int nid)
47 section_to_node_table[section_nr] = nid;
49 #else /* !NODE_NOT_IN_PAGE_FLAGS */
50 static inline void set_section_nid(unsigned long section_nr, int nid)
53 #endif
55 #ifdef CONFIG_SPARSEMEM_EXTREME
56 static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
58 struct mem_section *section = NULL;
59 unsigned long array_size = SECTIONS_PER_ROOT *
60 sizeof(struct mem_section);
62 if (slab_is_available())
63 section = kmalloc_node(array_size, GFP_KERNEL, nid);
64 else
65 section = alloc_bootmem_node(NODE_DATA(nid), array_size);
67 if (section)
68 memset(section, 0, array_size);
70 return section;
73 static int __meminit sparse_index_init(unsigned long section_nr, int nid)
75 static DEFINE_SPINLOCK(index_init_lock);
76 unsigned long root = SECTION_NR_TO_ROOT(section_nr);
77 struct mem_section *section;
78 int ret = 0;
80 if (mem_section[root])
81 return -EEXIST;
83 section = sparse_index_alloc(nid);
85 * This lock keeps two different sections from
86 * reallocating for the same index
88 spin_lock(&index_init_lock);
90 if (mem_section[root]) {
91 ret = -EEXIST;
92 goto out;
95 mem_section[root] = section;
96 out:
97 spin_unlock(&index_init_lock);
98 return ret;
100 #else /* !SPARSEMEM_EXTREME */
101 static inline int sparse_index_init(unsigned long section_nr, int nid)
103 return 0;
105 #endif
108 * Although written for the SPARSEMEM_EXTREME case, this happens
109 * to also work for the flat array case becase
110 * NR_SECTION_ROOTS==NR_MEM_SECTIONS.
112 int __section_nr(struct mem_section* ms)
114 unsigned long root_nr;
115 struct mem_section* root;
117 for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
118 root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
119 if (!root)
120 continue;
122 if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
123 break;
126 return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
130 * During early boot, before section_mem_map is used for an actual
131 * mem_map, we use section_mem_map to store the section's NUMA
132 * node. This keeps us from having to use another data structure. The
133 * node information is cleared just before we store the real mem_map.
135 static inline unsigned long sparse_encode_early_nid(int nid)
137 return (nid << SECTION_NID_SHIFT);
140 static inline int sparse_early_nid(struct mem_section *section)
142 return (section->section_mem_map >> SECTION_NID_SHIFT);
145 /* Record a memory area against a node. */
146 void __init memory_present(int nid, unsigned long start, unsigned long end)
148 unsigned long pfn;
150 start &= PAGE_SECTION_MASK;
151 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
152 unsigned long section = pfn_to_section_nr(pfn);
153 struct mem_section *ms;
155 sparse_index_init(section, nid);
156 set_section_nid(section, nid);
158 ms = __nr_to_section(section);
159 if (!ms->section_mem_map)
160 ms->section_mem_map = sparse_encode_early_nid(nid) |
161 SECTION_MARKED_PRESENT;
166 * Only used by the i386 NUMA architecures, but relatively
167 * generic code.
169 unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
170 unsigned long end_pfn)
172 unsigned long pfn;
173 unsigned long nr_pages = 0;
175 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
176 if (nid != early_pfn_to_nid(pfn))
177 continue;
179 if (pfn_valid(pfn))
180 nr_pages += PAGES_PER_SECTION;
183 return nr_pages * sizeof(struct page);
187 * Subtle, we encode the real pfn into the mem_map such that
188 * the identity pfn - section_mem_map will return the actual
189 * physical page frame number.
191 static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
193 return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
197 * We need this if we ever free the mem_maps. While not implemented yet,
198 * this function is included for parity with its sibling.
200 static __attribute((unused))
201 struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
203 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
206 static int __meminit sparse_init_one_section(struct mem_section *ms,
207 unsigned long pnum, struct page *mem_map)
209 if (!valid_section(ms))
210 return -EINVAL;
212 ms->section_mem_map &= ~SECTION_MAP_MASK;
213 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum);
215 return 1;
218 static struct page __init *sparse_early_mem_map_alloc(unsigned long pnum)
220 struct page *map;
221 struct mem_section *ms = __nr_to_section(pnum);
222 int nid = sparse_early_nid(ms);
224 map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
225 if (map)
226 return map;
228 map = alloc_bootmem_node(NODE_DATA(nid),
229 sizeof(struct page) * PAGES_PER_SECTION);
230 if (map)
231 return map;
233 printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__);
234 ms->section_mem_map = 0;
235 return NULL;
239 * Allocate the accumulated non-linear sections, allocate a mem_map
240 * for each and record the physical to section mapping.
242 void __init sparse_init(void)
244 unsigned long pnum;
245 struct page *map;
247 for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
248 if (!valid_section_nr(pnum))
249 continue;
251 map = sparse_early_mem_map_alloc(pnum);
252 if (!map)
253 continue;
254 sparse_init_one_section(__nr_to_section(pnum), pnum, map);
258 #ifdef CONFIG_MEMORY_HOTPLUG
259 static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
261 struct page *page, *ret;
262 unsigned long memmap_size = sizeof(struct page) * nr_pages;
264 page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
265 if (page)
266 goto got_map_page;
268 ret = vmalloc(memmap_size);
269 if (ret)
270 goto got_map_ptr;
272 return NULL;
273 got_map_page:
274 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
275 got_map_ptr:
276 memset(ret, 0, memmap_size);
278 return ret;
281 static int vaddr_in_vmalloc_area(void *addr)
283 if (addr >= (void *)VMALLOC_START &&
284 addr < (void *)VMALLOC_END)
285 return 1;
286 return 0;
289 static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
291 if (vaddr_in_vmalloc_area(memmap))
292 vfree(memmap);
293 else
294 free_pages((unsigned long)memmap,
295 get_order(sizeof(struct page) * nr_pages));
299 * returns the number of sections whose mem_maps were properly
300 * set. If this is <=0, then that means that the passed-in
301 * map was not consumed and must be freed.
303 int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
304 int nr_pages)
306 unsigned long section_nr = pfn_to_section_nr(start_pfn);
307 struct pglist_data *pgdat = zone->zone_pgdat;
308 struct mem_section *ms;
309 struct page *memmap;
310 unsigned long flags;
311 int ret;
314 * no locking for this, because it does its own
315 * plus, it does a kmalloc
317 sparse_index_init(section_nr, pgdat->node_id);
318 memmap = __kmalloc_section_memmap(nr_pages);
320 pgdat_resize_lock(pgdat, &flags);
322 ms = __pfn_to_section(start_pfn);
323 if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
324 ret = -EEXIST;
325 goto out;
327 ms->section_mem_map |= SECTION_MARKED_PRESENT;
329 ret = sparse_init_one_section(ms, section_nr, memmap);
331 out:
332 pgdat_resize_unlock(pgdat, &flags);
333 if (ret <= 0)
334 __kfree_section_memmap(memmap, nr_pages);
335 return ret;
337 #endif