blob: fa01292157a9aca0b66c461d9074e998ef43dcb5 [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>
8#include <linux/module.h>
9#include <asm/dma.h>
10
11/*
12 * Permanent SPARSEMEM data:
13 *
14 * 1) mem_section - memory sections, mem_map's for valid memory
15 */
Bob Picco3e347262005-09-03 15:54:28 -070016#ifdef CONFIG_SPARSEMEM_EXTREME
Bob Picco802f1922005-09-03 15:54:26 -070017struct mem_section *mem_section[NR_SECTION_ROOTS]
18 ____cacheline_maxaligned_in_smp;
Bob Picco3e347262005-09-03 15:54:28 -070019#else
20struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT]
21 ____cacheline_maxaligned_in_smp;
22#endif
23EXPORT_SYMBOL(mem_section);
24
25static void sparse_alloc_root(unsigned long root, int nid)
26{
27#ifdef CONFIG_SPARSEMEM_EXTREME
28 mem_section[root] = alloc_bootmem_node(NODE_DATA(nid), PAGE_SIZE);
29#endif
30}
Bob Picco802f1922005-09-03 15:54:26 -070031
32static void sparse_index_init(unsigned long section, int nid)
33{
Bob Picco3e347262005-09-03 15:54:28 -070034 unsigned long root = SECTION_NR_TO_ROOT(section);
Bob Picco802f1922005-09-03 15:54:26 -070035
36 if (mem_section[root])
37 return;
Bob Picco3e347262005-09-03 15:54:28 -070038
39 sparse_alloc_root(root, nid);
40
Bob Picco802f1922005-09-03 15:54:26 -070041 if (mem_section[root])
42 memset(mem_section[root], 0, PAGE_SIZE);
43 else
44 panic("memory_present: NO MEMORY\n");
45}
Andy Whitcroftd41dee32005-06-23 00:07:54 -070046/* Record a memory area against a node. */
47void memory_present(int nid, unsigned long start, unsigned long end)
48{
49 unsigned long pfn;
50
51 start &= PAGE_SECTION_MASK;
52 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
53 unsigned long section = pfn_to_section_nr(pfn);
Bob Picco802f1922005-09-03 15:54:26 -070054 struct mem_section *ms;
55
56 sparse_index_init(section, nid);
57
58 ms = __nr_to_section(section);
59 if (!ms->section_mem_map)
60 ms->section_mem_map = SECTION_MARKED_PRESENT;
Andy Whitcroftd41dee32005-06-23 00:07:54 -070061 }
62}
63
64/*
65 * Only used by the i386 NUMA architecures, but relatively
66 * generic code.
67 */
68unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
69 unsigned long end_pfn)
70{
71 unsigned long pfn;
72 unsigned long nr_pages = 0;
73
74 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
75 if (nid != early_pfn_to_nid(pfn))
76 continue;
77
78 if (pfn_valid(pfn))
79 nr_pages += PAGES_PER_SECTION;
80 }
81
82 return nr_pages * sizeof(struct page);
83}
84
85/*
Andy Whitcroft29751f62005-06-23 00:08:00 -070086 * Subtle, we encode the real pfn into the mem_map such that
87 * the identity pfn - section_mem_map will return the actual
88 * physical page frame number.
89 */
90static unsigned long sparse_encode_mem_map(struct page *mem_map, unsigned long pnum)
91{
92 return (unsigned long)(mem_map - (section_nr_to_pfn(pnum)));
93}
94
95/*
96 * We need this if we ever free the mem_maps. While not implemented yet,
97 * this function is included for parity with its sibling.
98 */
99static __attribute((unused))
100struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum)
101{
102 return ((struct page *)coded_mem_map) + section_nr_to_pfn(pnum);
103}
104
105static int sparse_init_one_section(struct mem_section *ms,
106 unsigned long pnum, struct page *mem_map)
107{
108 if (!valid_section(ms))
109 return -EINVAL;
110
111 ms->section_mem_map |= sparse_encode_mem_map(mem_map, pnum);
112
113 return 1;
114}
115
116static struct page *sparse_early_mem_map_alloc(unsigned long pnum)
117{
118 struct page *map;
119 int nid = early_pfn_to_nid(section_nr_to_pfn(pnum));
Bob Picco802f1922005-09-03 15:54:26 -0700120 struct mem_section *ms = __nr_to_section(pnum);
Andy Whitcroft29751f62005-06-23 00:08:00 -0700121
122 map = alloc_remap(nid, sizeof(struct page) * PAGES_PER_SECTION);
123 if (map)
124 return map;
125
126 map = alloc_bootmem_node(NODE_DATA(nid),
127 sizeof(struct page) * PAGES_PER_SECTION);
128 if (map)
129 return map;
130
131 printk(KERN_WARNING "%s: allocation failed\n", __FUNCTION__);
Bob Picco802f1922005-09-03 15:54:26 -0700132 ms->section_mem_map = 0;
Andy Whitcroft29751f62005-06-23 00:08:00 -0700133 return NULL;
134}
135
136/*
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700137 * Allocate the accumulated non-linear sections, allocate a mem_map
138 * for each and record the physical to section mapping.
139 */
140void sparse_init(void)
141{
142 unsigned long pnum;
143 struct page *map;
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700144
145 for (pnum = 0; pnum < NR_MEM_SECTIONS; pnum++) {
Andy Whitcroft29751f62005-06-23 00:08:00 -0700146 if (!valid_section_nr(pnum))
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700147 continue;
148
Andy Whitcroft29751f62005-06-23 00:08:00 -0700149 map = sparse_early_mem_map_alloc(pnum);
Bob Picco802f1922005-09-03 15:54:26 -0700150 if (!map)
151 continue;
152 sparse_init_one_section(__nr_to_section(pnum), pnum, map);
Andy Whitcroftd41dee32005-06-23 00:07:54 -0700153 }
154}
Andy Whitcroft29751f62005-06-23 00:08:00 -0700155
156/*
157 * returns the number of sections whose mem_maps were properly
158 * set. If this is <=0, then that means that the passed-in
159 * map was not consumed and must be freed.
160 */
161int sparse_add_one_section(unsigned long start_pfn, int nr_pages, struct page *map)
162{
163 struct mem_section *ms = __pfn_to_section(start_pfn);
164
165 if (ms->section_mem_map & SECTION_MARKED_PRESENT)
166 return -EEXIST;
167
168 ms->section_mem_map |= SECTION_MARKED_PRESENT;
169
170 return sparse_init_one_section(ms, pfn_to_section_nr(start_pfn), map);
171}