blob: 32ddad5dc9320470857bfed191105244c5b7ca49 [file] [log] [blame]
Yinghai Lufb74fb62010-08-25 13:39:15 -07001#include <linux/kernel.h>
2#include <linux/types.h>
3#include <linux/init.h>
4#include <linux/bitops.h>
5#include <linux/memblock.h>
6#include <linux/bootmem.h>
7#include <linux/mm.h>
8#include <linux/range.h>
9
10/* Check for already reserved areas */
11static inline bool __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
12{
13 struct memblock_region *r;
14 u64 addr = *addrp, last;
15 u64 size = *sizep;
16 bool changed = false;
17
18again:
19 last = addr + size;
20 for_each_memblock(reserved, r) {
21 if (last > r->base && addr < r->base) {
22 size = r->base - addr;
23 changed = true;
24 goto again;
25 }
26 if (last > (r->base + r->size) && addr < (r->base + r->size)) {
27 addr = round_up(r->base + r->size, align);
28 size = last - addr;
29 changed = true;
30 goto again;
31 }
32 if (last <= (r->base + r->size) && addr >= r->base) {
33 (*sizep)++;
34 return false;
35 }
36 }
37 if (changed) {
38 *addrp = addr;
39 *sizep = size;
40 }
41 return changed;
42}
43
44static u64 __init __memblock_x86_find_in_range_size(u64 ei_start, u64 ei_last, u64 start,
45 u64 *sizep, u64 align)
46{
47 u64 addr, last;
48
49 addr = round_up(ei_start, align);
50 if (addr < start)
51 addr = round_up(start, align);
52 if (addr >= ei_last)
53 goto out;
54 *sizep = ei_last - addr;
55 while (bad_addr_size(&addr, sizep, align) && addr + *sizep <= ei_last)
56 ;
57 last = addr + *sizep;
58 if (last > ei_last)
59 goto out;
60
61 return addr;
62
63out:
64 return MEMBLOCK_ERROR;
65}
66
67/*
68 * Find next free range after start, and size is returned in *sizep
69 */
70u64 __init memblock_x86_find_in_range_size(u64 start, u64 *sizep, u64 align)
71{
72 struct memblock_region *r;
73
74 for_each_memblock(memory, r) {
75 u64 ei_start = r->base;
76 u64 ei_last = ei_start + r->size;
77 u64 addr;
78
79 addr = __memblock_x86_find_in_range_size(ei_start, ei_last, start,
80 sizep, align);
81
82 if (addr != MEMBLOCK_ERROR)
83 return addr;
84 }
85
86 return MEMBLOCK_ERROR;
87}
Yinghai Lu27de7942010-08-25 13:39:15 -070088
Yinghai Lu4d5cf862010-08-25 13:39:16 -070089static __init struct range *find_range_array(int count)
90{
91 u64 end, size, mem;
92 struct range *range;
93
94 size = sizeof(struct range) * count;
95 end = memblock.current_limit;
96
97 mem = memblock_find_in_range(0, end, size, sizeof(struct range));
98 if (mem == MEMBLOCK_ERROR)
99 panic("can not find more space for range array");
100
101 /*
102 * This range is tempoaray, so don't reserve it, it will not be
103 * overlapped because We will not alloccate new buffer before
104 * We discard this one
105 */
106 range = __va(mem);
107 memset(range, 0, size);
108
109 return range;
110}
111
112#ifdef CONFIG_NO_BOOTMEM
113static void __init memblock_x86_subtract_reserved(struct range *range, int az)
114{
115 u64 final_start, final_end;
116 struct memblock_region *r;
117
118 /* Take out region array itself at first*/
119 memblock_free_reserved_regions();
120
121 pr_info("Subtract (%ld early reservations)\n", memblock.reserved.cnt);
122
123 for_each_memblock(reserved, r) {
124 pr_info(" [%010llx-%010llx]\n", (u64)r->base, (u64)r->base + r->size - 1);
125 final_start = PFN_DOWN(r->base);
126 final_end = PFN_UP(r->base + r->size);
127 if (final_start >= final_end)
128 continue;
129 subtract_range(range, az, final_start, final_end);
130 }
131
132 /* Put region array back ? */
133 memblock_reserve_reserved_regions();
134}
135
136struct count_data {
137 int nr;
138};
139
140static int __init count_work_fn(unsigned long start_pfn,
141 unsigned long end_pfn, void *datax)
142{
143 struct count_data *data = datax;
144
145 data->nr++;
146
147 return 0;
148}
149
150static int __init count_early_node_map(int nodeid)
151{
152 struct count_data data;
153
154 data.nr = 0;
155 work_with_active_regions(nodeid, count_work_fn, &data);
156
157 return data.nr;
158}
159
160int __init get_free_all_memory_range(struct range **rangep, int nodeid)
161{
162 int count;
163 struct range *range;
164 int nr_range;
165
166 count = (memblock.reserved.cnt + count_early_node_map(nodeid)) * 2;
167
168 range = find_range_array(count);
169 nr_range = 0;
170
171 /*
172 * Use early_node_map[] and memblock.reserved.region to get range array
173 * at first
174 */
175 nr_range = add_from_early_node_map(range, count, nr_range, nodeid);
176#ifdef CONFIG_X86_32
177 subtract_range(range, count, max_low_pfn, -1ULL);
178#endif
179 memblock_x86_subtract_reserved(range, count);
180 nr_range = clean_sort_range(range, count);
181
182 *rangep = range;
183 return nr_range;
184}
185#else
Yinghai Lu27de7942010-08-25 13:39:15 -0700186void __init memblock_x86_to_bootmem(u64 start, u64 end)
187{
188 int count;
189 u64 final_start, final_end;
190 struct memblock_region *r;
191
192 /* Take out region array itself */
193 memblock_free_reserved_regions();
194
195 count = memblock.reserved.cnt;
196 pr_info("(%d early reservations) ==> bootmem [%010llx-%010llx]\n", count, start, end - 1);
197 for_each_memblock(reserved, r) {
198 pr_info(" [%010llx-%010llx] ", (u64)r->base, (u64)r->base + r->size - 1);
199 final_start = max(start, r->base);
200 final_end = min(end, r->base + r->size);
201 if (final_start >= final_end) {
202 pr_cont("\n");
203 continue;
204 }
205 pr_cont(" ==> [%010llx-%010llx]\n", final_start, final_end - 1);
206 reserve_bootmem_generic(final_start, final_end - final_start, BOOTMEM_DEFAULT);
207 }
208
209 /* Put region array back ? */
210 memblock_reserve_reserved_regions();
211}
212#endif
Yinghai Lu9dc5d562010-08-25 13:39:15 -0700213
Yinghai Lue82d42b2010-08-25 13:39:17 -0700214static u64 __init __memblock_x86_memory_in_range(u64 addr, u64 limit, bool get_free)
Yinghai Lub52c17c2010-08-25 13:39:16 -0700215{
216 int i, count;
217 struct range *range;
218 int nr_range;
219 u64 final_start, final_end;
220 u64 free_size;
221 struct memblock_region *r;
222
223 count = (memblock.reserved.cnt + memblock.memory.cnt) * 2;
224
225 range = find_range_array(count);
226 nr_range = 0;
227
228 addr = PFN_UP(addr);
229 limit = PFN_DOWN(limit);
230
231 for_each_memblock(memory, r) {
232 final_start = PFN_UP(r->base);
233 final_end = PFN_DOWN(r->base + r->size);
234 if (final_start >= final_end)
235 continue;
236 if (final_start >= limit || final_end <= addr)
237 continue;
238
239 nr_range = add_range(range, count, nr_range, final_start, final_end);
240 }
241 subtract_range(range, count, 0, addr);
242 subtract_range(range, count, limit, -1ULL);
Yinghai Lue82d42b2010-08-25 13:39:17 -0700243
244 /* Subtract memblock.reserved.region in range ? */
245 if (!get_free)
246 goto sort_and_count_them;
Yinghai Lub52c17c2010-08-25 13:39:16 -0700247 for_each_memblock(reserved, r) {
248 final_start = PFN_DOWN(r->base);
249 final_end = PFN_UP(r->base + r->size);
250 if (final_start >= final_end)
251 continue;
252 if (final_start >= limit || final_end <= addr)
253 continue;
254
255 subtract_range(range, count, final_start, final_end);
256 }
Yinghai Lue82d42b2010-08-25 13:39:17 -0700257
258sort_and_count_them:
Yinghai Lub52c17c2010-08-25 13:39:16 -0700259 nr_range = clean_sort_range(range, count);
260
261 free_size = 0;
262 for (i = 0; i < nr_range; i++)
263 free_size += range[i].end - range[i].start;
264
265 return free_size << PAGE_SHIFT;
266}
267
Yinghai Lue82d42b2010-08-25 13:39:17 -0700268u64 __init memblock_x86_free_memory_in_range(u64 addr, u64 limit)
269{
270 return __memblock_x86_memory_in_range(addr, limit, true);
271}
272
273u64 __init memblock_x86_memory_in_range(u64 addr, u64 limit)
274{
275 return __memblock_x86_memory_in_range(addr, limit, false);
276}
277
Yinghai Lu9dc5d562010-08-25 13:39:15 -0700278void __init memblock_x86_reserve_range(u64 start, u64 end, char *name)
279{
280 if (start == end)
281 return;
282
283 if (WARN_ONCE(start > end, "memblock_x86_reserve_range: wrong range [%#llx, %#llx]\n", start, end))
284 return;
285
286 memblock_reserve(start, end - start);
287}
288
289void __init memblock_x86_free_range(u64 start, u64 end)
290{
291 if (start == end)
292 return;
293
294 if (WARN_ONCE(start > end, "memblock_x86_free_range: wrong range [%#llx, %#llx]\n", start, end))
295 return;
296
297 memblock_free(start, end - start);
298}
Yinghai Lu88ba0882010-08-25 13:39:16 -0700299
300/*
Yinghai Lu6bcc8172010-08-25 13:39:16 -0700301 * Need to call this function after memblock_x86_register_active_regions,
302 * so early_node_map[] is filled already.
303 */
304u64 __init memblock_x86_find_in_range_node(int nid, u64 start, u64 end, u64 size, u64 align)
305{
306 u64 addr;
307 addr = find_memory_core_early(nid, size, align, start, end);
308 if (addr != MEMBLOCK_ERROR)
309 return addr;
310
311 /* Fallback, should already have start end within node range */
312 return memblock_find_in_range(start, end, size, align);
313}
314
315/*
Yinghai Lu88ba0882010-08-25 13:39:16 -0700316 * Finds an active region in the address range from start_pfn to last_pfn and
317 * returns its range in ei_startpfn and ei_endpfn for the memblock entry.
318 */
319static int __init memblock_x86_find_active_region(const struct memblock_region *ei,
320 unsigned long start_pfn,
321 unsigned long last_pfn,
322 unsigned long *ei_startpfn,
323 unsigned long *ei_endpfn)
324{
325 u64 align = PAGE_SIZE;
326
327 *ei_startpfn = round_up(ei->base, align) >> PAGE_SHIFT;
328 *ei_endpfn = round_down(ei->base + ei->size, align) >> PAGE_SHIFT;
329
330 /* Skip map entries smaller than a page */
331 if (*ei_startpfn >= *ei_endpfn)
332 return 0;
333
334 /* Skip if map is outside the node */
335 if (*ei_endpfn <= start_pfn || *ei_startpfn >= last_pfn)
336 return 0;
337
338 /* Check for overlaps */
339 if (*ei_startpfn < start_pfn)
340 *ei_startpfn = start_pfn;
341 if (*ei_endpfn > last_pfn)
342 *ei_endpfn = last_pfn;
343
344 return 1;
345}
346
347/* Walk the memblock.memory map and register active regions within a node */
348void __init memblock_x86_register_active_regions(int nid, unsigned long start_pfn,
349 unsigned long last_pfn)
350{
351 unsigned long ei_startpfn;
352 unsigned long ei_endpfn;
353 struct memblock_region *r;
354
355 for_each_memblock(memory, r)
356 if (memblock_x86_find_active_region(r, start_pfn, last_pfn,
357 &ei_startpfn, &ei_endpfn))
358 add_active_range(nid, ei_startpfn, ei_endpfn);
359}
360
361/*
362 * Find the hole size (in bytes) in the memory range.
363 * @start: starting address of the memory range to scan
364 * @end: ending address of the memory range to scan
365 */
366u64 __init memblock_x86_hole_size(u64 start, u64 end)
367{
368 unsigned long start_pfn = start >> PAGE_SHIFT;
369 unsigned long last_pfn = end >> PAGE_SHIFT;
370 unsigned long ei_startpfn, ei_endpfn, ram = 0;
371 struct memblock_region *r;
372
373 for_each_memblock(memory, r)
374 if (memblock_x86_find_active_region(r, start_pfn, last_pfn,
375 &ei_startpfn, &ei_endpfn))
376 ram += ei_endpfn - ei_startpfn;
377
378 return end - start - ((u64)ram << PAGE_SHIFT);
379}