blob: d7c32de99ee8c49d0e850a06084d5efe508a7625 [file] [log] [blame]
Andy Whitcroftd41dee32005-06-23 00:07:54 -07001/*
2 * sparse memory mappings.
3 */
4#include <linux/config.h>
5#include <linux/mm.h>
6#include <linux/mmzone.h>
7#include <linux/bootmem.h>
Dave Hansen0b0acbe2005-10-29 18:16:55 -07008#include <linux/highmem.h>
Andy Whitcroftd41dee32005-06-23 00:07:54 -07009#include <linux/module.h>
Dave Hansen28ae55c2005-09-03 15:54:29 -070010#include <linux/spinlock.h>
Dave Hansen0b0acbe2005-10-29 18:16:55 -070011#include <linux/vmalloc.h>
Andy Whitcroftd41dee32005-06-23 00:07:54 -070012#include <asm/dma.h>
13
14/*
15 * Permanent SPARSEMEM data:
16 *
17 * 1) mem_section - memory sections, mem_map's for valid memory
18 */
Bob Picco3e347262005-09-03 15:54:28 -070019#ifdef CONFIG_SPARSEMEM_EXTREME
Bob Picco802f1922005-09-03 15:54:26 -070020struct mem_section *mem_section[NR_SECTION_ROOTS]
Ravikiran G Thirumalai22fc6ec2006-01-08 01:01:27 -080021 ____cacheline_internodealigned_in_smp;
Bob Picco3e347262005-09-03 15:54:28 -070022#else
23struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
Ravikiran G Thirumalai22fc6ec2006-01-08 01:01:27 -080024 ____cacheline_internodealigned_in_smp;
Bob Picco3e347262005-09-03 15:54:28 -070025#endif
26EXPORT_SYMBOL(mem_section);
27
Bob Picco3e347262005-09-03 15:54:28 -070028#ifdef CONFIG_SPARSEMEM_EXTREME
Dave Hansen28ae55c2005-09-03 15:54:29 -070029static struct mem_section *sparse_index_alloc(int nid)
Bob Picco802f1922005-09-03 15:54:26 -070030{
Dave Hansen28ae55c2005-09-03 15:54:29 -070031 struct mem_section *section = NULL;
32 unsigned long array_size = SECTIONS_PER_ROOT *
33 sizeof(struct mem_section);
Bob Picco802f1922005-09-03 15:54:26 -070034
Mike Kravetz46a66ee2006-05-01 12:16:09 -070035 if (system_state == SYSTEM_RUNNING)
36 section = kmalloc_node(array_size, GFP_KERNEL, nid);
37 else
38 section = alloc_bootmem_node(NODE_DATA(nid), array_size);
Bob Picco3e347262005-09-03 15:54:28 -070039
Dave Hansen28ae55c2005-09-03 15:54:29 -070040 if (section)
41 memset(section, 0, array_size);
Bob Picco3e347262005-09-03 15:54:28 -070042
Dave Hansen28ae55c2005-09-03 15:54:29 -070043 return section;
Bob Picco802f1922005-09-03 15:54:26 -070044}
Dave Hansen28ae55c2005-09-03 15:54:29 -070045
46static int sparse_index_init(unsigned long section_nr, int nid)
47{
48 static spinlock_t index_init_lock = SPIN_LOCK_UNLOCKED;
49 unsigned long root = SECTION_NR_TO_ROOT(section_nr);
50 struct mem_section *section;
51 int ret = 0;
52
53 if (mem_section[root])
54 return -EEXIST;
55
56 section = sparse_index_alloc(nid);
57 /*
58 * This lock keeps two different sections from
59 * reallocating for the same index
60 */
61 spin_lock(&index_init_lock);
62
63 if (mem_section[root]) {
64 ret = -EEXIST;
65 goto out;
66 }
67
68 mem_section[root] = section;
69out:
70 spin_unlock(&index_init_lock);
71 return ret;
72}
73#else /* !SPARSEMEM_EXTREME */
74static inline int sparse_index_init(unsigned long section_nr, int nid)
75{
76 return 0;
77}
78#endif
79
Dave Hansen4ca644d2005-10-29 18:16:51 -070080/*
81 * Although written for the SPARSEMEM_EXTREME case, this happens
82 * to also work for the flat array case becase
83 * NR_SECTION_ROOTS==NR_MEM_SECTIONS.
84 */
85int __section_nr(struct mem_section* ms)
86{
87 unsigned long root_nr;
88 struct mem_section* root;
89
90 for (root_nr = 0;
91 root_nr < NR_MEM_SECTIONS;
92 root_nr += SECTIONS_PER_ROOT) {
93 root = __nr_to_section(root_nr);
94
95 if (!root)
96 continue;
97
98 if ((ms >= root) && (ms < (root + SECTIONS_PER_ROOT)))
99 break;
100 }
101
102 return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
103}
104
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700105/* Record a memory area against a node. */
106void memory_present(int nid, unsigned long start, unsigned long end)
107{
108 unsigned long pfn;
109
110 start &= PAGE_SECTION_MASK;
111 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
112 unsigned long section = pfn_to_section_nr(pfn);
Bob Picco802f1922005-09-03 15:54:26 -0700113 struct mem_section *ms;
114
115 sparse_index_init(section, nid);
116
117 ms = __nr_to_section(section);
118 if (!ms->section_mem_map)
119 ms->section_mem_map = SECTION_MARKED_PRESENT;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700120 }
121}
122
123/*
124 * Only used by the i386 NUMA architecures, but relatively
125 * generic code.
126 */
127unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
128 unsigned long end_pfn)
129{
130 unsigned long pfn;
131 unsigned long nr_pages = 0;
132
133 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
134 if (nid != early_pfn_to_nid(pfn))
135 continue;
136
137 if (pfn_valid(pfn))
138 nr_pages += PAGES_PER_SECTION;
139 }
140
141 return nr_pages * sizeof(struct page);
142}
143
144/*
Andy Whitcroft29751f62005-06-23 00:08:00 -0700145 * Subtle, we encode the real pfn into the mem_map such that
146 * the identity pfn - section_mem_map will return the actual
147 * physical page frame number.
148 */
149static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
150{
151 return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
152}
153
154/*
155 * We need this if we ever free the mem_maps. While not implemented yet,
156 * this function is included for parity with its sibling.
157 */
158static __attribute((unused))
159struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
160{
161 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
162}
163
164static int sparse_init_one_section(struct mem_section *ms,
165 unsigned long pnum, struct page *mem_map)
166{
167 if (!valid_section(ms))
168 return -EINVAL;
169
170 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum);
171
172 return 1;
173}
174
175static struct page *sparse_early_mem_map_alloc(unsigned long pnum)
176{
177 struct page *map;
178 int nid = early_pfn_to_nid(section_nr_to_pfn(pnum));
Bob Picco802f1922005-09-03 15:54:26 -0700179 struct mem_section *ms = __nr_to_section(pnum);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700180
181 map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
182 if (map)
183 return map;
184
185 map = alloc_bootmem_node(NODE_DATA(nid),
186 sizeof(struct page) * PAGES_PER_SECTION);
187 if (map)
188 return map;
189
190 printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__);
Bob Picco802f1922005-09-03 15:54:26 -0700191 ms->section_mem_map = 0;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700192 return NULL;
193}
194
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700195static struct page *__kmalloc_section_memmap(unsigned long nr_pages)
196{
197 struct page *page, *ret;
198 unsigned long memmap_size = sizeof(struct page) * nr_pages;
199
200 page = alloc_pages(GFP_KERNEL, get_order(memmap_size));
201 if (page)
202 goto got_map_page;
203
204 ret = vmalloc(memmap_size);
205 if (ret)
206 goto got_map_ptr;
207
208 return NULL;
209got_map_page:
210 ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
211got_map_ptr:
212 memset(ret, 0, memmap_size);
213
214 return ret;
215}
216
217static int vaddr_in_vmalloc_area(void *addr)
218{
219 if (addr >= (void *)VMALLOC_START &&
220 addr < (void *)VMALLOC_END)
221 return 1;
222 return 0;
223}
224
225static void __kfree_section_memmap(struct page *memmap, unsigned long nr_pages)
226{
227 if (vaddr_in_vmalloc_area(memmap))
228 vfree(memmap);
229 else
230 free_pages((unsigned long)memmap,
231 get_order(sizeof(struct page) * nr_pages));
232}
233
Andy Whitcroft29751f62005-06-23 00:08:00 -0700234/*
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700235 * Allocate the accumulated non-linear sections, allocate a mem_map
236 * for each and record the physical to section mapping.
237 */
238void sparse_init(void)
239{
240 unsigned long pnum;
241 struct page *map;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700242
243 for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
Andy Whitcroft29751f62005-06-23 00:08:00 -0700244 if (!valid_section_nr(pnum))
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700245 continue;
246
Andy Whitcroft29751f62005-06-23 00:08:00 -0700247 map = sparse_early_mem_map_alloc(pnum);
Bob Picco802f1922005-09-03 15:54:26 -0700248 if (!map)
249 continue;
250 sparse_init_one_section(__nr_to_section(pnum), pnum, map);
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700251 }
252}
Andy Whitcroft29751f62005-06-23 00:08:00 -0700253
254/*
255 * returns the number of sections whose mem_maps were properly
256 * set. If this is <=0, then that means that the passed-in
257 * map was not consumed and must be freed.
258 */
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700259int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
260 int nr_pages)
Andy Whitcroft29751f62005-06-23 00:08:00 -0700261{
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700262 unsigned long section_nr = pfn_to_section_nr(start_pfn);
263 struct pglist_data *pgdat = zone->zone_pgdat;
264 struct mem_section *ms;
265 struct page *memmap;
266 unsigned long flags;
267 int ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700268
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700269 /*
270 * no locking for this, because it does its own
271 * plus, it does a kmalloc
272 */
273 sparse_index_init(section_nr, pgdat->node_id);
274 memmap = __kmalloc_section_memmap(nr_pages);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700275
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700276 pgdat_resize_lock(pgdat, &flags);
277
278 ms = __pfn_to_section(start_pfn);
279 if (ms->section_mem_map & SECTION_MARKED_PRESENT) {
280 ret = -EEXIST;
281 goto out;
282 }
Andy Whitcroft29751f62005-06-23 00:08:00 -0700283 ms->section_mem_map |= SECTION_MARKED_PRESENT;
284
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700285 ret = sparse_init_one_section(ms, section_nr, memmap);
286
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700287out:
288 pgdat_resize_unlock(pgdat, &flags);
Mike Kravetz46a66ee2006-05-01 12:16:09 -0700289 if (ret <= 0)
290 __kfree_section_memmap(memmap, nr_pages);
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700291 return ret;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700292}