blob: ac26eb0d73cddeaf53b20fb7a7d31f954b8bc117 [file] [log] [blame]
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001/*
2 * sparse memory mappings.
3 */
Andy Whitcroftd41dee32005-06-23 00:07:54 -07004#include <linux/mm.h>
5#include <linux/mmzone.h>
6#include <linux/bootmem.h>
Dave Hansen0b0acbe2005-10-29 18:16:55 -07007#include <linux/highmem.h>
Andy Whitcroftd41dee32005-06-23 00:07:54 -07008#include <linux/module.h>
Dave Hansen28ae55c2005-09-03 15:54:29 -07009#include <linux/spinlock.h>
Dave Hansen0b0acbe2005-10-29 18:16:55 -070010#include <linux/vmalloc.h>
Andy Whitcroftd41dee32005-06-23 00:07:54 -070011#include <asm/dma.h>
12
13/*
14 * Permanent SPARSEMEM data:
15 *
16 * 1) mem_section - memory sections, mem_map's for valid memory
17 */
Bob Picco3e347262005-09-03 15:54:28 -070018#ifdef CONFIG_SPARSEMEM_EXTREME
Bob Picco802f1922005-09-03 15:54:26 -070019struct mem_section *mem_section[NR_SECTION_ROOTS]
Ravikiran G Thirumalai22fc6ec2006-01-08 01:01:27 -080020 ____cacheline_internodealigned_in_smp;
Bob Picco3e347262005-09-03 15:54:28 -070021#else
22struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
Ravikiran G Thirumalai22fc6ec2006-01-08 01:01:27 -080023 ____cacheline_internodealigned_in_smp;
Bob Picco3e347262005-09-03 15:54:28 -070024#endif
25EXPORT_SYMBOL(mem_section);
26
Christoph Lameter89689ae2006-12-06 20:31:45 -080027#ifdef NODE_NOT_IN_PAGE_FLAGS
28/*
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.
32 */
33#if MAX_NUMNODES <= 256
34static u8 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
35#else
36static u16 section_to_node_table[NR_MEM_SECTIONS] __cacheline_aligned;
37#endif
38
Andy Whitcroft25ba77c2006-12-06 20:33:03 -080039int page_to_nid(struct page *page)
Christoph Lameter89689ae2006-12-06 20:31:45 -080040{
41 return section_to_node_table[page_to_section(page)];
42}
43EXPORT_SYMBOL(page_to_nid);
44#endif
45
Bob Picco3e347262005-09-03 15:54:28 -070046#ifdef CONFIG_SPARSEMEM_EXTREME
Dave Hansen28ae55c2005-09-03 15:54:29 -070047static struct mem_section *sparse_index_alloc(int nid)
Bob Picco802f1922005-09-03 15:54:26 -070048{
Dave Hansen28ae55c2005-09-03 15:54:29 -070049 struct mem_section *section = NULL;
50 unsigned long array_size = SECTIONS_PER_ROOT *
51 sizeof(struct mem_section);
Bob Picco802f1922005-09-03 15:54:26 -070052
Mike Kravetz39d24e62006-05-15 09:44:13 -070053 if (slab_is_available())
Mike Kravetz46a66ee2006-05-01 12:16:09 -070054 section = kmalloc_node(array_size, GFP_KERNEL, nid);
55 else
56 section = alloc_bootmem_node(NODE_DATA(nid), array_size);
Bob Picco3e347262005-09-03 15:54:28 -070057
Dave Hansen28ae55c2005-09-03 15:54:29 -070058 if (section)
59 memset(section, 0, array_size);
Bob Picco3e347262005-09-03 15:54:28 -070060
Dave Hansen28ae55c2005-09-03 15:54:29 -070061 return section;
Bob Picco802f1922005-09-03 15:54:26 -070062}
Dave Hansen28ae55c2005-09-03 15:54:29 -070063
64static int sparse_index_init(unsigned long section_nr, int nid)
65{
Ingo Molnar34af9462006-06-27 02:53:55 -070066 static DEFINE_SPINLOCK(index_init_lock);
Dave Hansen28ae55c2005-09-03 15:54:29 -070067 unsigned long root = SECTION_NR_TO_ROOT(section_nr);
68 struct mem_section *section;
69 int ret = 0;
70
Christoph Lameter89689ae2006-12-06 20:31:45 -080071#ifdef NODE_NOT_IN_PAGE_FLAGS
72 section_to_node_table[section_nr] = nid;
73#endif
74
Dave Hansen28ae55c2005-09-03 15:54:29 -070075 if (mem_section[root])
76 return -EEXIST;
77
78 section = sparse_index_alloc(nid);
79 /*
80 * This lock keeps two different sections from
81 * reallocating for the same index
82 */
83 spin_lock(&index_init_lock);
84
85 if (mem_section[root]) {
86 ret = -EEXIST;
87 goto out;
88 }
89
90 mem_section[root] = section;
91out:
92 spin_unlock(&index_init_lock);
93 return ret;
94}
95#else /* !SPARSEMEM_EXTREME */
96static inline int sparse_index_init(unsigned long section_nr, int nid)
97{
98 return 0;
99}
100#endif
101
Dave Hansen4ca644d2005-10-29 18:16:51 -0700102/*
103 * Although written for the SPARSEMEM_EXTREME case, this happens
104 * to also work for the flat array case becase
105 * NR_SECTION_ROOTS==NR_MEM_SECTIONS.
106 */
107int __section_nr(struct mem_section* ms)
108{
109 unsigned long root_nr;
110 struct mem_section* root;
111
Mike Kravetz12783b02006-05-20 15:00:05 -0700112 for (root_nr = 0; root_nr < NR_SECTION_ROOTS; root_nr++) {
113 root = __nr_to_section(root_nr * SECTIONS_PER_ROOT);
Dave Hansen4ca644d2005-10-29 18:16:51 -0700114 if (!root)
115 continue;
116
117 if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
118 break;
119 }
120
121 return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
122}
123
Andy Whitcroft30c253e2006-06-23 02:03:41 -0700124/*
125 * During early boot, before section_mem_map is used for an actual
126 * mem_map, we use section_mem_map to store the section's NUMA
127 * node. This keeps us from having to use another data structure. The
128 * node information is cleared just before we store the real mem_map.
129 */
130static inline unsigned long sparse_encode_early_nid(int nid)
131{
132 return (nid << SECTION_NID_SHIFT);
133}
134
135static inline int sparse_early_nid(struct mem_section *section)
136{
137 return (section->section_mem_map >> SECTION_NID_SHIFT);
138}
139
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700140/* Record a memory area against a node. */
141void memory_present(int nid, unsigned long start, unsigned long end)
142{
143 unsigned long pfn;
144
145 start &= PAGE_SECTION_MASK;
146 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
147 unsigned long section = pfn_to_section_nr(pfn);
Bob Picco802f1922005-09-03 15:54:26 -0700148 struct mem_section *ms;
149
150 sparse_index_init(section, nid);
151
152 ms = __nr_to_section(section);
153 if (!ms->section_mem_map)
Andy Whitcroft30c253e2006-06-23 02:03:41 -0700154 ms->section_mem_map = sparse_encode_early_nid(nid) |
155 SECTION_MARKED_PRESENT;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700156 }
157}
158
159/*
160 * Only used by the i386 NUMA architecures, but relatively
161 * generic code.
162 */
163unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
164 unsigned long end_pfn)
165{
166 unsigned long pfn;
167 unsigned long nr_pages = 0;
168
169 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
170 if (nid != early_pfn_to_nid(pfn))
171 continue;
172
173 if (pfn_valid(pfn))
174 nr_pages += PAGES_PER_SECTION;
175 }
176
177 return nr_pages * sizeof(struct page);
178}
179
180/*
Andy Whitcroft29751f62005-06-23 00:08:00 -0700181 * Subtle, we encode the real pfn into the mem_map such that
182 * the identity pfn - section_mem_map will return the actual
183 * physical page frame number.
184 */
185static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
186{
187 return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
188}
189
190/*
191 * We need this if we ever free the mem_maps. While not implemented yet,
192 * this function is included for parity with its sibling.
193 */
194static __attribute((unused))
195struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
196{
197 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
198}
199
200static int sparse_init_one_section(struct mem_section *ms,
201 unsigned long pnum, struct page *mem_map)
202{
203 if (!valid_section(ms))
204 return -EINVAL;
205
Andy Whitcroft30c253e2006-06-23 02:03:41 -0700206 ms->section_mem_map &= ~SECTION_MAP_MASK;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700207 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum);
208
209 return 1;
210}
211
212static struct page *sparse_early_mem_map_alloc(unsigned long pnum)
213{
214 struct page *map;
Bob Picco802f1922005-09-03 15:54:26 -0700215 struct mem_section *ms = __nr_to_section(pnum);
Andy Whitcroft30c253e2006-06-23 02:03:41 -0700216 int nid = sparse_early_nid(ms);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700217
218 map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
219 if (map)
220 return map;
221
222 map = alloc_bootmem_node(NODE_DATA(nid),
223 sizeof(struct page) * PAGES_PER_SECTION);
224 if (map)
225 return map;
226
227 printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__);
Bob Picco802f1922005-09-03 15:54:26 -0700228 ms->section_mem_map = 0;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700229 return NULL;
230}
231
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700232static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
233{
234 struct page *page, *ret;
235 unsigned long memmap_size = sizeof(struct page) * nr_pages;
236
Yasunori Gotof2d0aa52006-10-28 10:38:32 -0700237 page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700238 if (page)
239 goto got_map_page;
240
241 ret = vmalloc(memmap_size);
242 if (ret)
243 goto got_map_ptr;
244
245 return NULL;
246got_map_page:
247 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
248got_map_ptr:
249 memset(ret, 0, memmap_size);
250
251 return ret;
252}
253
254static int vaddr_in_vmalloc_area(void *addr)
255{
256 if (addr >= (void *)VMALLOC_START &&
257 addr < (void *)VMALLOC_END)
258 return 1;
259 return 0;
260}
261
262static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
263{
264 if (vaddr_in_vmalloc_area(memmap))
265 vfree(memmap);
266 else
267 free_pages((unsigned long)memmap,
268 get_order(sizeof(struct page) * nr_pages));
269}
270
Andy Whitcroft29751f62005-06-23 00:08:00 -0700271/*
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700272 * Allocate the accumulated non-linear sections, allocate a mem_map
273 * for each and record the physical to section mapping.
274 */
275void sparse_init(void)
276{
277 unsigned long pnum;
278 struct page *map;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700279
280 for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
Andy Whitcroft29751f62005-06-23 00:08:00 -0700281 if (!valid_section_nr(pnum))
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700282 continue;
283
Andy Whitcroft29751f62005-06-23 00:08:00 -0700284 map = sparse_early_mem_map_alloc(pnum);
Bob Picco802f1922005-09-03 15:54:26 -0700285 if (!map)
286 continue;
287 sparse_init_one_section(__nr_to_section(pnum), pnum, map);
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700288 }
289}
Andy Whitcroft29751f62005-06-23 00:08:00 -0700290
291/*
292 * returns the number of sections whose mem_maps were properly
293 * set. If this is <=0, then that means that the passed-in
294 * map was not consumed and must be freed.
295 */
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700296int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
297 int nr_pages)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700298{
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700299 unsigned long section_nr = pfn_to_section_nr(start_pfn);
300 struct pglist_data *pgdat = zone->zone_pgdat;
301 struct mem_section *ms;
302 struct page *memmap;
303 unsigned long flags;
304 int ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700305
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700306 /*
307 * no locking for this, because it does its own
308 * plus, it does a kmalloc
309 */
310 sparse_index_init(section_nr, pgdat->node_id);
311 memmap = __kmalloc_section_memmap(nr_pages);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700312
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700313 pgdat_resize_lock(pgdat, &flags);
314
315 ms = __pfn_to_section(start_pfn);
316 if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
317 ret = -EEXIST;
318 goto out;
319 }
Andy Whitcroft29751f62005-06-23 00:08:00 -0700320 ms->section_mem_map |= SECTION_MARKED_PRESENT;
321
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700322 ret = sparse_init_one_section(ms, section_nr, memmap);
323
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700324out:
325 pgdat_resize_unlock(pgdat, &flags);
Mike Kravetz46a66ee2006-05-01 12:16:09 -0700326 if (ret <= 0)
327 __kfree_section_memmap(memmap, nr_pages);
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700328 return ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700329}