1 /*
2  * Handle the memory map.
3  * The functions here do the job until bootmem takes over.
4  * $Id: e820.c,v 1.13 2004/03/22 00:31:08 ak Exp $
5  */
6 #include <linux/config.h>
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/init.h>
10 #include <linux/acpi.h>
11 #include <linux/bootmem.h>
12 #include <linux/ioport.h>
13 #include <asm/page.h>
14 #include <asm/e820.h>
15 #include <asm/proto.h>
16 #include <asm/acpi.h>
17 #include <asm/apic.h>
18 #include <asm/bootsetup.h>
19 #include <asm/mpspec.h>
20 #include <asm/io_apic.h>
21 
22 extern unsigned long table_start, table_end;
23 extern char _end[];
24 
25 #ifdef	CONFIG_ACPI_BOOT
26 extern acpi_interrupt_flags	acpi_sci_flags;
27 #endif
28 
29 extern struct resource code_resource, data_resource, vram_resource;
30 
31 /* Check for some hardcoded bad areas that early boot is not allowed to touch */
bad_addr(unsigned long * addrp,unsigned long size)32 static inline int bad_addr(unsigned long *addrp, unsigned long size)
33 {
34 	unsigned long addr = *addrp, last = addr + size;
35 
36 	/* various gunk below that needed for SMP startup */
37 	if (addr < 7*PAGE_SIZE) {
38 		*addrp = 7*PAGE_SIZE;
39 		return 1;
40 	}
41 	/* direct mapping tables of the kernel */
42 	if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
43 		*addrp = table_end << PAGE_SHIFT;
44 		return 1;
45 	}
46 	/* initrd */
47 #ifdef CONFIG_BLK_DEV_INITRD
48 	if (LOADER_TYPE && INITRD_START && last >= INITRD_START &&
49 	    addr < INITRD_START+INITRD_SIZE) {
50 		*addrp = INITRD_START + INITRD_SIZE;
51 		return 1;
52 	}
53 #endif
54 	/* kernel code + 640k memory hole (later should not be needed, but
55 	   be paranoid for now) */
56 	if (last >= 640*1024 && addr < __pa_symbol(&_end)) {
57 		*addrp = __pa_symbol(&_end);
58 		return 1;
59 	}
60 	/* XXX ramdisk image here? */
61 	return 0;
62 }
63 
e820_mapped(unsigned long start,unsigned long end,int type)64 int __init e820_mapped(unsigned long start, unsigned long end, int type)
65 {
66 	int i;
67 	for (i = 0; i < e820.nr_map; i++) {
68 		struct e820entry *ei = &e820.map[i];
69 		if (type && ei->type != type)
70 			continue;
71 		if (ei->addr >= end || ei->addr + ei->size < start)
72 			continue;
73 		return 1;
74 	}
75 	return 0;
76 }
77 
78 /*
79  * Find a free area in a specific range.
80  */
find_e820_area(unsigned long start,unsigned long end,unsigned size)81 unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size)
82 {
83 	int i;
84 	for (i = 0; i < e820.nr_map; i++) {
85 		struct e820entry *ei = &e820.map[i];
86 		unsigned long addr = ei->addr, last;
87 		if (ei->type != E820_RAM)
88 			continue;
89 		if (addr < start)
90 			addr = start;
91 		if (addr > ei->addr + ei->size)
92 			continue;
93 		while (bad_addr(&addr, size) && addr+size < ei->addr + ei->size)
94 			;
95 		last = addr + size;
96 		if (last > ei->addr + ei->size)
97 			continue;
98 		if (last > end)
99 			continue;
100 		return addr;
101 	}
102 	return -1UL;
103 }
104 
105 /*
106  * Free bootmem based on the e820 table for a node.
107  */
e820_bootmem_free(pg_data_t * pgdat,unsigned long start,unsigned long end)108 void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end)
109 {
110 	int i;
111 	for (i = 0; i < e820.nr_map; i++) {
112 		struct e820entry *ei = &e820.map[i];
113 		unsigned long last, addr;
114 
115 		if (ei->type != E820_RAM ||
116 		    ei->addr+ei->size <= start ||
117 		    ei->addr > end)
118 			continue;
119 
120 		addr = round_up(ei->addr, PAGE_SIZE);
121 		if (addr < start)
122 			addr = start;
123 
124 		last = round_down(ei->addr + ei->size, PAGE_SIZE);
125 		if (last >= end)
126 			last = end;
127 
128 		if (last > addr && last-addr >= PAGE_SIZE)
129 			free_bootmem_node(pgdat, addr, last-addr);
130 	}
131 }
132 
133 /*
134  * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
135  * The direct mapping extends to end_pfn_map, so that we can directly access
136  * ACPI and other tables without having to play with fixmaps.
137  */
138 unsigned long end_pfn_map;
139 
140 /*
141  * Last pfn which the user wants to use.
142  */
143 unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT;
144 
145 /*
146  * Find the highest page frame number we have available
147  */
148 
e820_end_of_ram(void)149 void __init e820_end_of_ram(void)
150 {
151 	int i;
152 	end_pfn = 0;
153 
154 	for (i = 0; i < e820.nr_map; i++) {
155 		struct e820entry *ei = &e820.map[i];
156 		unsigned long start, end;
157 
158 		start = round_up(ei->addr, PAGE_SIZE);
159 		end = round_down(ei->addr + ei->size, PAGE_SIZE);
160 		if (start >= end)
161 			continue;
162 		if (ei->type == E820_RAM) {
163 		if (end > end_pfn<<PAGE_SHIFT)
164 			end_pfn = end>>PAGE_SHIFT;
165 		} else {
166 			if (end > end_pfn_map<<PAGE_SHIFT)
167 				end_pfn_map = end>>PAGE_SHIFT;
168 		}
169 	}
170 
171 	if (end_pfn > end_pfn_map)
172 		end_pfn_map = end_pfn;
173 	if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
174 		end_pfn_map = MAXMEM>>PAGE_SHIFT;
175 	if (end_pfn > end_user_pfn)
176 		end_pfn = end_user_pfn;
177 	if (end_pfn > end_pfn_map)
178 		end_pfn = end_pfn_map;
179 }
180 
181 /*
182  * Mark e820 reserved areas as busy for the resource manager.
183  */
e820_reserve_resources(void)184 void __init e820_reserve_resources(void)
185 {
186 	int i;
187 	for (i = 0; i < e820.nr_map; i++) {
188 		struct resource *res;
189 		if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
190 			continue;
191 		res = alloc_bootmem_low(sizeof(struct resource));
192 		switch (e820.map[i].type) {
193 		case E820_RAM:	res->name = "System RAM"; break;
194 		case E820_ACPI:	res->name = "ACPI Tables"; break;
195 		case E820_NVS:	res->name = "ACPI Non-volatile Storage"; break;
196 		default:	res->name = "reserved";
197 		}
198 		res->start = e820.map[i].addr;
199 		res->end = res->start + e820.map[i].size - 1;
200 		res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
201 		request_resource(&iomem_resource, res);
202 		if (e820.map[i].type == E820_RAM) {
203 			/*
204 			 *  We dont't know which RAM region contains kernel data,
205 			 *  so we try it repeatedly and let the resource manager
206 			 *  test it.
207 			 */
208 			request_resource(res, &code_resource);
209 			request_resource(res, &data_resource);
210 		}
211 	}
212 }
213 
214 /*
215  * Add a memory region to the kernel e820 map.
216  */
add_memory_region(unsigned long start,unsigned long size,int type)217 void __init add_memory_region(unsigned long start, unsigned long size, int type)
218 {
219 	int x = e820.nr_map;
220 
221 	if (x == E820MAX) {
222 		printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
223 		return;
224 	}
225 
226 	e820.map[x].addr = start;
227 	e820.map[x].size = size;
228 	e820.map[x].type = type;
229 	e820.nr_map++;
230 }
231 
e820_print_map(char * who)232 void __init e820_print_map(char *who)
233 {
234 	int i;
235 
236 	for (i = 0; i < e820.nr_map; i++) {
237 		printk(" %s: %016Lx - %016Lx ", who,
238 			(unsigned long long) e820.map[i].addr,
239 			(unsigned long long) (e820.map[i].addr + e820.map[i].size));
240 		switch (e820.map[i].type) {
241 		case E820_RAM:	printk("(usable)\n");
242 				break;
243 		case E820_RESERVED:
244 				printk("(reserved)\n");
245 				break;
246 		case E820_ACPI:
247 				printk("(ACPI data)\n");
248 				break;
249 		case E820_NVS:
250 				printk("(ACPI NVS)\n");
251 				break;
252 		default:	printk("type %u\n", e820.map[i].type);
253 				break;
254 		}
255 	}
256 }
257 
258 /*
259  * Sanitize the BIOS e820 map.
260  *
261  * Some e820 responses include overlapping entries.  The following
262  * replaces the original e820 map with a new one, removing overlaps.
263  *
264  */
sanitize_e820_map(struct e820entry * biosmap,char * pnr_map)265 static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
266 {
267 	struct change_member {
268 		struct e820entry *pbios; /* pointer to original bios entry */
269 		unsigned long long addr; /* address for this change point */
270 	};
271 	static struct change_member change_point_list[2*E820MAX] __initdata;
272 	static struct change_member *change_point[2*E820MAX] __initdata;
273 	static struct e820entry *overlap_list[E820MAX] __initdata;
274 	static struct e820entry new_bios[E820MAX] __initdata;
275 	struct change_member *change_tmp;
276 	unsigned long current_type, last_type;
277 	unsigned long long last_addr;
278 	int chgidx, still_changing;
279 	int overlap_entries;
280 	int new_bios_entry;
281 	int old_nr, new_nr;
282 	int i;
283 
284 	/*
285 		Visually we're performing the following (1,2,3,4 = memory types)...
286 
287 		Sample memory map (w/overlaps):
288 		   ____22__________________
289 		   ______________________4_
290 		   ____1111________________
291 		   _44_____________________
292 		   11111111________________
293 		   ____________________33__
294 		   ___________44___________
295 		   __________33333_________
296 		   ______________22________
297 		   ___________________2222_
298 		   _________111111111______
299 		   _____________________11_
300 		   _________________4______
301 
302 		Sanitized equivalent (no overlap):
303 		   1_______________________
304 		   _44_____________________
305 		   ___1____________________
306 		   ____22__________________
307 		   ______11________________
308 		   _________1______________
309 		   __________3_____________
310 		   ___________44___________
311 		   _____________33_________
312 		   _______________2________
313 		   ________________1_______
314 		   _________________4______
315 		   ___________________2____
316 		   ____________________33__
317 		   ______________________4_
318 	*/
319 
320 	/* if there's only one memory region, don't bother */
321 	if (*pnr_map < 2)
322 		return -1;
323 
324 	old_nr = *pnr_map;
325 
326 	/* bail out if we find any unreasonable addresses in bios map */
327 	for (i=0; i<old_nr; i++)
328 		if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
329 			return -1;
330 
331 	/* create pointers for initial change-point information (for sorting) */
332 	for (i=0; i < 2*old_nr; i++)
333 		change_point[i] = &change_point_list[i];
334 
335 	/* record all known change-points (starting and ending addresses) */
336 	chgidx = 0;
337 	for (i=0; i < old_nr; i++)	{
338 		change_point[chgidx]->addr = biosmap[i].addr;
339 		change_point[chgidx++]->pbios = &biosmap[i];
340 		change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
341 		change_point[chgidx++]->pbios = &biosmap[i];
342 	}
343 
344 	/* sort change-point list by memory addresses (low -> high) */
345 	still_changing = 1;
346 	while (still_changing)	{
347 		still_changing = 0;
348 		for (i=1; i < 2*old_nr; i++)  {
349 			/* if <current_addr> > <last_addr>, swap */
350 			/* or, if current=<start_addr> & last=<end_addr>, swap */
351 			if ((change_point[i]->addr < change_point[i-1]->addr) ||
352 				((change_point[i]->addr == change_point[i-1]->addr) &&
353 				 (change_point[i]->addr == change_point[i]->pbios->addr) &&
354 				 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
355 			   )
356 			{
357 				change_tmp = change_point[i];
358 				change_point[i] = change_point[i-1];
359 				change_point[i-1] = change_tmp;
360 				still_changing=1;
361 			}
362 		}
363 	}
364 
365 	/* create a new bios memory map, removing overlaps */
366 	overlap_entries=0;	 /* number of entries in the overlap table */
367 	new_bios_entry=0;	 /* index for creating new bios map entries */
368 	last_type = 0;		 /* start with undefined memory type */
369 	last_addr = 0;		 /* start with 0 as last starting address */
370 	/* loop through change-points, determining affect on the new bios map */
371 	for (chgidx=0; chgidx < 2*old_nr; chgidx++)
372 	{
373 		/* keep track of all overlapping bios entries */
374 		if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
375 		{
376 			/* add map entry to overlap list (> 1 entry implies an overlap) */
377 			overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
378 		}
379 		else
380 		{
381 			/* remove entry from list (order independent, so swap with last) */
382 			for (i=0; i<overlap_entries; i++)
383 			{
384 				if (overlap_list[i] == change_point[chgidx]->pbios)
385 					overlap_list[i] = overlap_list[overlap_entries-1];
386 			}
387 			overlap_entries--;
388 		}
389 		/* if there are overlapping entries, decide which "type" to use */
390 		/* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
391 		current_type = 0;
392 		for (i=0; i<overlap_entries; i++)
393 			if (overlap_list[i]->type > current_type)
394 				current_type = overlap_list[i]->type;
395 		/* continue building up new bios map based on this information */
396 		if (current_type != last_type)	{
397 			if (last_type != 0)	 {
398 				new_bios[new_bios_entry].size =
399 					change_point[chgidx]->addr - last_addr;
400 				/* move forward only if the new size was non-zero */
401 				if (new_bios[new_bios_entry].size != 0)
402 					if (++new_bios_entry >= E820MAX)
403 						break; 	/* no more space left for new bios entries */
404 			}
405 			if (current_type != 0)	{
406 				new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
407 				new_bios[new_bios_entry].type = current_type;
408 				last_addr=change_point[chgidx]->addr;
409 			}
410 			last_type = current_type;
411 		}
412 	}
413 	new_nr = new_bios_entry;   /* retain count for new bios entries */
414 
415 	/* copy new bios mapping into original location */
416 	memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
417 	*pnr_map = new_nr;
418 
419 	return 0;
420 }
421 
422 /*
423  * Copy the BIOS e820 map into a safe place.
424  *
425  * Sanity-check it while we're at it..
426  *
427  * If we're lucky and live on a modern system, the setup code
428  * will have given us a memory map that we can use to properly
429  * set up memory.  If we aren't, we'll fake a memory map.
430  *
431  * We check to see that the memory map contains at least 2 elements
432  * before we'll use it, because the detection code in setup.S may
433  * not be perfect and most every PC known to man has two memory
434  * regions: one from 0 to 640k, and one from 1mb up.  (The IBM
435  * thinkpad 560x, for example, does not cooperate with the memory
436  * detection code.)
437  */
copy_e820_map(struct e820entry * biosmap,int nr_map)438 static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
439 {
440 	/* Only one memory region (or negative)? Ignore it */
441 	if (nr_map < 2)
442 		return -1;
443 
444 	do {
445 		unsigned long start = biosmap->addr;
446 		unsigned long size = biosmap->size;
447 		unsigned long end = start + size;
448 		unsigned long type = biosmap->type;
449 
450 		/* Overflow in 64 bits? Ignore the memory map. */
451 		if (start > end)
452 			return -1;
453 
454 		/*
455 		 * Some BIOSes claim RAM in the 640k - 1M region.
456 		 * Not right. Fix it up.
457 		 *
458 		 * This should be removed on Hammer which is supposed to not
459 		 * have non e820 covered ISA mappings there, but I had some strange
460 		 * problems so it stays for now.  -AK
461 		 */
462 		if (type == E820_RAM) {
463 			if (start < 0x100000ULL && end > 0xA0000ULL) {
464 				if (start < 0xA0000ULL)
465 					add_memory_region(start, 0xA0000ULL-start, type);
466 				if (end <= 0x100000ULL)
467 					continue;
468 				start = 0x100000ULL;
469 				size = end - start;
470 			}
471 		}
472 
473 		add_memory_region(start, size, type);
474 	} while (biosmap++,--nr_map);
475 	return 0;
476 }
477 
setup_memory_region(void)478 void __init setup_memory_region(void)
479 {
480 	char *who = "BIOS-e820";
481 
482 	/*
483 	 * Try to copy the BIOS-supplied E820-map.
484 	 *
485 	 * Otherwise fake a memory map; one section from 0k->640k,
486 	 * the next section from 1mb->appropriate_mem_k
487 	 */
488 	sanitize_e820_map(E820_MAP, &E820_MAP_NR);
489 	if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
490 		unsigned long mem_size;
491 
492 		/* compare results from other methods and take the greater */
493 		if (ALT_MEM_K < EXT_MEM_K) {
494 			mem_size = EXT_MEM_K;
495 			who = "BIOS-88";
496 		} else {
497 			mem_size = ALT_MEM_K;
498 			who = "BIOS-e801";
499 		}
500 		e820.nr_map = 0;
501 		add_memory_region(0, LOWMEMSIZE(), E820_RAM);
502 		add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
503   	}
504 	printk(KERN_INFO "BIOS-provided physical RAM map:\n");
505 	e820_print_map(who);
506 }
507 
508 extern char command_line[], saved_command_line[];
509 extern int fallback_aper_order;
510 extern int iommu_setup(char *opt);
511 
parse_mem_cmdline(char ** cmdline_p)512 void __init parse_mem_cmdline (char ** cmdline_p)
513 {
514 	char c = ' ', *to = command_line, *from = COMMAND_LINE;
515 	int len = 0;
516 
517 	/* Save unparsed command line copy for /proc/cmdline */
518 	memcpy(saved_command_line, COMMAND_LINE, COMMAND_LINE_SIZE);
519 	saved_command_line[COMMAND_LINE_SIZE-1] = '\0';
520 
521 	for (;;) {
522 		if (c != ' ')
523 			goto next;
524 
525 		/*
526 		 * mem=XXX[kKmM] limits kernel memory to XXX+1MB
527 		 *
528 		 * It would be more logical to count from 0 instead of from
529 		 * HIGH_MEMORY, but we keep that for now for i386 compatibility. -AK
530 		 */
531 		if (!memcmp(from, "mem=", 4)) {
532 			/*
533 			 * No support for custom mapping like i386.
534 			 * The reason is that we need to read the e820 map
535 			 * anyways to handle the ACPI mappings in the
536 			 * direct map.
537 			 * Also on x86-64 there should be always a good e820
538 			 * map. This is only an upper limit, you cannot force
539 			 * usage of memory not in e820.
540 					 */
541 			end_user_pfn = memparse(from+4, &from) + HIGH_MEMORY;
542 			end_user_pfn >>= PAGE_SHIFT;
543 		}
544 #ifdef CONFIG_GART_IOMMU
545 		else if (!memcmp(from,"iommu=",6)) {
546 			iommu_setup(from+6);
547 		}
548 #endif
549 #ifdef	CONFIG_SMP
550 		/*
551 		 * If the BIOS enumerates physical processors before logical,
552 		 * maxcpus=N at enumeration-time can be used to disable HT.
553 		 */
554 		else if (!memcmp(from, "maxcpus=", 8)) {
555 			extern unsigned int max_cpus;
556 
557 			max_cpus = simple_strtoul(from + 8, NULL, 0);
558 		}
559 #endif
560 
561 #ifdef	CONFIG_ACPI_BOOT
562  		else if (!memcmp(from, "acpi=off", 8))
563   			disable_acpi();
564 
565 		/* acpi=strict disables out-of-spec workarounds */
566 		else if (!memcmp(from, "acpi=strict", 11)) {
567 			acpi_strict = 1;
568 		}
569 
570 		else if (!memcmp(from, "pci=noacpi", 10))
571 			acpi_disable_pci();
572 		else if (!memcmp(from, "acpi=noirq", 10))
573 			acpi_noirq_set();
574 		else if (!memcmp(from, "acpi_sci=edge", 13))
575 			acpi_sci_flags.trigger =  1;
576 		else if (!memcmp(from, "acpi_sci=level", 14))
577 			acpi_sci_flags.trigger = 3;
578 		else if (!memcmp(from, "acpi_sci=high", 13))
579 			acpi_sci_flags.polarity = 1;
580 		else if (!memcmp(from, "acpi_sci=low", 12))
581 			acpi_sci_flags.polarity = 3;
582 #endif
583 		else if (!memcmp(from,"maxcpus=0",9)) {
584 			disable_ioapic_setup();
585 			apic_disabled = 1;
586 		}
587 
588 		else if (!memcmp(from, "noapic", 6))
589 			disable_ioapic_setup();
590 		else if (!memcmp(from, "nolocalapic", 11) || !memcmp(from,"nolapic",7))
591 			apic_disabled = 1;
592 		else if (!memcmp(from,"apic",4)) {
593 			extern int ioapic_force;
594 			ioapic_force = 1;
595 			skip_ioapic_setup = 0;
596 		}
597 		else if (!memcmp(from, "noexec=", 7)) {
598 			extern int nonx_setup(char *);
599 			nonx_setup(from + 7);
600 		}
601 	next:
602 		c = *(from++);
603 		if (!c)
604 			break;
605 		if (COMMAND_LINE_SIZE <= ++len)
606 			break;
607 		*(to++) = c;
608 	}
609 	*to = '\0';
610 	*cmdline_p = command_line;
611 }
612