xref: /linux/arch/mips/kernel/setup.c (revision 5bdd5fbb35ab0fe21bf2263106f51c5bee466a07)
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1995 Linus Torvalds
7  * Copyright (C) 1995 Waldorf Electronics
8  * Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 01, 02, 03  Ralf Baechle
9  * Copyright (C) 1996 Stoned Elipot
10  * Copyright (C) 1999 Silicon Graphics, Inc.
11  * Copyright (C) 2000, 2001, 2002, 2007	 Maciej W. Rozycki
12  */
13 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/export.h>
16 #include <linux/screen_info.h>
17 #include <linux/memblock.h>
18 #include <linux/bootmem.h>
19 #include <linux/initrd.h>
20 #include <linux/root_dev.h>
21 #include <linux/highmem.h>
22 #include <linux/console.h>
23 #include <linux/pfn.h>
24 #include <linux/debugfs.h>
25 #include <linux/kexec.h>
26 #include <linux/sizes.h>
27 #include <linux/device.h>
28 #include <linux/dma-contiguous.h>
29 #include <linux/decompress/generic.h>
30 #include <linux/of_fdt.h>
31 
32 #include <asm/addrspace.h>
33 #include <asm/bootinfo.h>
34 #include <asm/bugs.h>
35 #include <asm/cache.h>
36 #include <asm/cdmm.h>
37 #include <asm/cpu.h>
38 #include <asm/debug.h>
39 #include <asm/dma-coherence.h>
40 #include <asm/sections.h>
41 #include <asm/setup.h>
42 #include <asm/smp-ops.h>
43 #include <asm/prom.h>
44 
45 #ifdef CONFIG_MIPS_ELF_APPENDED_DTB
46 const char __section(.appended_dtb) __appended_dtb[0x100000];
47 #endif /* CONFIG_MIPS_ELF_APPENDED_DTB */
48 
49 struct cpuinfo_mips cpu_data[NR_CPUS] __read_mostly;
50 
51 EXPORT_SYMBOL(cpu_data);
52 
53 #ifdef CONFIG_VT
54 struct screen_info screen_info;
55 #endif
56 
57 /*
58  * Setup information
59  *
60  * These are initialized so they are in the .data section
61  */
62 unsigned long mips_machtype __read_mostly = MACH_UNKNOWN;
63 
64 EXPORT_SYMBOL(mips_machtype);
65 
66 struct boot_mem_map boot_mem_map;
67 
68 static char __initdata command_line[COMMAND_LINE_SIZE];
69 char __initdata arcs_cmdline[COMMAND_LINE_SIZE];
70 
71 #ifdef CONFIG_CMDLINE_BOOL
72 static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
73 #endif
74 
75 /*
76  * mips_io_port_base is the begin of the address space to which x86 style
77  * I/O ports are mapped.
78  */
79 const unsigned long mips_io_port_base = -1;
80 EXPORT_SYMBOL(mips_io_port_base);
81 
82 static struct resource code_resource = { .name = "Kernel code", };
83 static struct resource data_resource = { .name = "Kernel data", };
84 static struct resource bss_resource = { .name = "Kernel bss", };
85 
86 static void *detect_magic __initdata = detect_memory_region;
87 
88 void __init add_memory_region(phys_addr_t start, phys_addr_t size, long type)
89 {
90 	int x = boot_mem_map.nr_map;
91 	int i;
92 
93 	/*
94 	 * If the region reaches the top of the physical address space, adjust
95 	 * the size slightly so that (start + size) doesn't overflow
96 	 */
97 	if (start + size - 1 == PHYS_ADDR_MAX)
98 		--size;
99 
100 	/* Sanity check */
101 	if (start + size < start) {
102 		pr_warn("Trying to add an invalid memory region, skipped\n");
103 		return;
104 	}
105 
106 	/*
107 	 * Try to merge with existing entry, if any.
108 	 */
109 	for (i = 0; i < boot_mem_map.nr_map; i++) {
110 		struct boot_mem_map_entry *entry = boot_mem_map.map + i;
111 		unsigned long top;
112 
113 		if (entry->type != type)
114 			continue;
115 
116 		if (start + size < entry->addr)
117 			continue;			/* no overlap */
118 
119 		if (entry->addr + entry->size < start)
120 			continue;			/* no overlap */
121 
122 		top = max(entry->addr + entry->size, start + size);
123 		entry->addr = min(entry->addr, start);
124 		entry->size = top - entry->addr;
125 
126 		return;
127 	}
128 
129 	if (boot_mem_map.nr_map == BOOT_MEM_MAP_MAX) {
130 		pr_err("Ooops! Too many entries in the memory map!\n");
131 		return;
132 	}
133 
134 	boot_mem_map.map[x].addr = start;
135 	boot_mem_map.map[x].size = size;
136 	boot_mem_map.map[x].type = type;
137 	boot_mem_map.nr_map++;
138 }
139 
140 void __init detect_memory_region(phys_addr_t start, phys_addr_t sz_min, phys_addr_t sz_max)
141 {
142 	void *dm = &detect_magic;
143 	phys_addr_t size;
144 
145 	for (size = sz_min; size < sz_max; size <<= 1) {
146 		if (!memcmp(dm, dm + size, sizeof(detect_magic)))
147 			break;
148 	}
149 
150 	pr_debug("Memory: %lluMB of RAM detected at 0x%llx (min: %lluMB, max: %lluMB)\n",
151 		((unsigned long long) size) / SZ_1M,
152 		(unsigned long long) start,
153 		((unsigned long long) sz_min) / SZ_1M,
154 		((unsigned long long) sz_max) / SZ_1M);
155 
156 	add_memory_region(start, size, BOOT_MEM_RAM);
157 }
158 
159 static bool __init __maybe_unused memory_region_available(phys_addr_t start,
160 							  phys_addr_t size)
161 {
162 	int i;
163 	bool in_ram = false, free = true;
164 
165 	for (i = 0; i < boot_mem_map.nr_map; i++) {
166 		phys_addr_t start_, end_;
167 
168 		start_ = boot_mem_map.map[i].addr;
169 		end_ = boot_mem_map.map[i].addr + boot_mem_map.map[i].size;
170 
171 		switch (boot_mem_map.map[i].type) {
172 		case BOOT_MEM_RAM:
173 			if (start >= start_ && start + size <= end_)
174 				in_ram = true;
175 			break;
176 		case BOOT_MEM_RESERVED:
177 			if ((start >= start_ && start < end_) ||
178 			    (start < start_ && start + size >= start_))
179 				free = false;
180 			break;
181 		default:
182 			continue;
183 		}
184 	}
185 
186 	return in_ram && free;
187 }
188 
189 static void __init print_memory_map(void)
190 {
191 	int i;
192 	const int field = 2 * sizeof(unsigned long);
193 
194 	for (i = 0; i < boot_mem_map.nr_map; i++) {
195 		printk(KERN_INFO " memory: %0*Lx @ %0*Lx ",
196 		       field, (unsigned long long) boot_mem_map.map[i].size,
197 		       field, (unsigned long long) boot_mem_map.map[i].addr);
198 
199 		switch (boot_mem_map.map[i].type) {
200 		case BOOT_MEM_RAM:
201 			printk(KERN_CONT "(usable)\n");
202 			break;
203 		case BOOT_MEM_INIT_RAM:
204 			printk(KERN_CONT "(usable after init)\n");
205 			break;
206 		case BOOT_MEM_ROM_DATA:
207 			printk(KERN_CONT "(ROM data)\n");
208 			break;
209 		case BOOT_MEM_RESERVED:
210 			printk(KERN_CONT "(reserved)\n");
211 			break;
212 		default:
213 			printk(KERN_CONT "type %lu\n", boot_mem_map.map[i].type);
214 			break;
215 		}
216 	}
217 }
218 
219 /*
220  * Manage initrd
221  */
222 #ifdef CONFIG_BLK_DEV_INITRD
223 
224 static int __init rd_start_early(char *p)
225 {
226 	unsigned long start = memparse(p, &p);
227 
228 #ifdef CONFIG_64BIT
229 	/* Guess if the sign extension was forgotten by bootloader */
230 	if (start < XKPHYS)
231 		start = (int)start;
232 #endif
233 	initrd_start = start;
234 	initrd_end += start;
235 	return 0;
236 }
237 early_param("rd_start", rd_start_early);
238 
239 static int __init rd_size_early(char *p)
240 {
241 	initrd_end += memparse(p, &p);
242 	return 0;
243 }
244 early_param("rd_size", rd_size_early);
245 
246 /* it returns the next free pfn after initrd */
247 static unsigned long __init init_initrd(void)
248 {
249 	unsigned long end;
250 
251 	/*
252 	 * Board specific code or command line parser should have
253 	 * already set up initrd_start and initrd_end. In these cases
254 	 * perfom sanity checks and use them if all looks good.
255 	 */
256 	if (!initrd_start || initrd_end <= initrd_start)
257 		goto disable;
258 
259 	if (initrd_start & ~PAGE_MASK) {
260 		pr_err("initrd start must be page aligned\n");
261 		goto disable;
262 	}
263 	if (initrd_start < PAGE_OFFSET) {
264 		pr_err("initrd start < PAGE_OFFSET\n");
265 		goto disable;
266 	}
267 
268 	/*
269 	 * Sanitize initrd addresses. For example firmware
270 	 * can't guess if they need to pass them through
271 	 * 64-bits values if the kernel has been built in pure
272 	 * 32-bit. We need also to switch from KSEG0 to XKPHYS
273 	 * addresses now, so the code can now safely use __pa().
274 	 */
275 	end = __pa(initrd_end);
276 	initrd_end = (unsigned long)__va(end);
277 	initrd_start = (unsigned long)__va(__pa(initrd_start));
278 
279 	ROOT_DEV = Root_RAM0;
280 	return PFN_UP(end);
281 disable:
282 	initrd_start = 0;
283 	initrd_end = 0;
284 	return 0;
285 }
286 
287 /* In some conditions (e.g. big endian bootloader with a little endian
288    kernel), the initrd might appear byte swapped.  Try to detect this and
289    byte swap it if needed.  */
290 static void __init maybe_bswap_initrd(void)
291 {
292 #if defined(CONFIG_CPU_CAVIUM_OCTEON)
293 	u64 buf;
294 
295 	/* Check for CPIO signature */
296 	if (!memcmp((void *)initrd_start, "070701", 6))
297 		return;
298 
299 	/* Check for compressed initrd */
300 	if (decompress_method((unsigned char *)initrd_start, 8, NULL))
301 		return;
302 
303 	/* Try again with a byte swapped header */
304 	buf = swab64p((u64 *)initrd_start);
305 	if (!memcmp(&buf, "070701", 6) ||
306 	    decompress_method((unsigned char *)(&buf), 8, NULL)) {
307 		unsigned long i;
308 
309 		pr_info("Byteswapped initrd detected\n");
310 		for (i = initrd_start; i < ALIGN(initrd_end, 8); i += 8)
311 			swab64s((u64 *)i);
312 	}
313 #endif
314 }
315 
316 static void __init finalize_initrd(void)
317 {
318 	unsigned long size = initrd_end - initrd_start;
319 
320 	if (size == 0) {
321 		printk(KERN_INFO "Initrd not found or empty");
322 		goto disable;
323 	}
324 	if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
325 		printk(KERN_ERR "Initrd extends beyond end of memory");
326 		goto disable;
327 	}
328 
329 	maybe_bswap_initrd();
330 
331 	reserve_bootmem(__pa(initrd_start), size, BOOTMEM_DEFAULT);
332 	initrd_below_start_ok = 1;
333 
334 	pr_info("Initial ramdisk at: 0x%lx (%lu bytes)\n",
335 		initrd_start, size);
336 	return;
337 disable:
338 	printk(KERN_CONT " - disabling initrd\n");
339 	initrd_start = 0;
340 	initrd_end = 0;
341 }
342 
343 #else  /* !CONFIG_BLK_DEV_INITRD */
344 
345 static unsigned long __init init_initrd(void)
346 {
347 	return 0;
348 }
349 
350 #define finalize_initrd()	do {} while (0)
351 
352 #endif
353 
354 /*
355  * Initialize the bootmem allocator. It also setup initrd related data
356  * if needed.
357  */
358 #if defined(CONFIG_SGI_IP27) || (defined(CONFIG_CPU_LOONGSON3) && defined(CONFIG_NUMA))
359 
360 static void __init bootmem_init(void)
361 {
362 	init_initrd();
363 	finalize_initrd();
364 }
365 
366 #else  /* !CONFIG_SGI_IP27 */
367 
368 static unsigned long __init bootmap_bytes(unsigned long pages)
369 {
370 	unsigned long bytes = DIV_ROUND_UP(pages, 8);
371 
372 	return ALIGN(bytes, sizeof(long));
373 }
374 
375 static void __init bootmem_init(void)
376 {
377 	unsigned long reserved_end;
378 	unsigned long mapstart = ~0UL;
379 	unsigned long bootmap_size;
380 	phys_addr_t ramstart = PHYS_ADDR_MAX;
381 	bool bootmap_valid = false;
382 	int i;
383 
384 	/*
385 	 * Sanity check any INITRD first. We don't take it into account
386 	 * for bootmem setup initially, rely on the end-of-kernel-code
387 	 * as our memory range starting point. Once bootmem is inited we
388 	 * will reserve the area used for the initrd.
389 	 */
390 	init_initrd();
391 	reserved_end = (unsigned long) PFN_UP(__pa_symbol(&_end));
392 
393 	/*
394 	 * max_low_pfn is not a number of pages. The number of pages
395 	 * of the system is given by 'max_low_pfn - min_low_pfn'.
396 	 */
397 	min_low_pfn = ~0UL;
398 	max_low_pfn = 0;
399 
400 	/*
401 	 * Find the highest page frame number we have available
402 	 * and the lowest used RAM address
403 	 */
404 	for (i = 0; i < boot_mem_map.nr_map; i++) {
405 		unsigned long start, end;
406 
407 		if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
408 			continue;
409 
410 		start = PFN_UP(boot_mem_map.map[i].addr);
411 		end = PFN_DOWN(boot_mem_map.map[i].addr
412 				+ boot_mem_map.map[i].size);
413 
414 		ramstart = min(ramstart, boot_mem_map.map[i].addr);
415 
416 #ifndef CONFIG_HIGHMEM
417 		/*
418 		 * Skip highmem here so we get an accurate max_low_pfn if low
419 		 * memory stops short of high memory.
420 		 * If the region overlaps HIGHMEM_START, end is clipped so
421 		 * max_pfn excludes the highmem portion.
422 		 */
423 		if (start >= PFN_DOWN(HIGHMEM_START))
424 			continue;
425 		if (end > PFN_DOWN(HIGHMEM_START))
426 			end = PFN_DOWN(HIGHMEM_START);
427 #endif
428 
429 		if (end > max_low_pfn)
430 			max_low_pfn = end;
431 		if (start < min_low_pfn)
432 			min_low_pfn = start;
433 		if (end <= reserved_end)
434 			continue;
435 #ifdef CONFIG_BLK_DEV_INITRD
436 		/* Skip zones before initrd and initrd itself */
437 		if (initrd_end && end <= (unsigned long)PFN_UP(__pa(initrd_end)))
438 			continue;
439 #endif
440 		if (start >= mapstart)
441 			continue;
442 		mapstart = max(reserved_end, start);
443 	}
444 
445 	/*
446 	 * Reserve any memory between the start of RAM and PHYS_OFFSET
447 	 */
448 	if (ramstart > PHYS_OFFSET)
449 		add_memory_region(PHYS_OFFSET, ramstart - PHYS_OFFSET,
450 				  BOOT_MEM_RESERVED);
451 
452 	if (min_low_pfn >= max_low_pfn)
453 		panic("Incorrect memory mapping !!!");
454 	if (min_low_pfn > ARCH_PFN_OFFSET) {
455 		pr_info("Wasting %lu bytes for tracking %lu unused pages\n",
456 			(min_low_pfn - ARCH_PFN_OFFSET) * sizeof(struct page),
457 			min_low_pfn - ARCH_PFN_OFFSET);
458 	} else if (ARCH_PFN_OFFSET - min_low_pfn > 0UL) {
459 		pr_info("%lu free pages won't be used\n",
460 			ARCH_PFN_OFFSET - min_low_pfn);
461 	}
462 	min_low_pfn = ARCH_PFN_OFFSET;
463 
464 	/*
465 	 * Determine low and high memory ranges
466 	 */
467 	max_pfn = max_low_pfn;
468 	if (max_low_pfn > PFN_DOWN(HIGHMEM_START)) {
469 #ifdef CONFIG_HIGHMEM
470 		highstart_pfn = PFN_DOWN(HIGHMEM_START);
471 		highend_pfn = max_low_pfn;
472 #endif
473 		max_low_pfn = PFN_DOWN(HIGHMEM_START);
474 	}
475 
476 #ifdef CONFIG_BLK_DEV_INITRD
477 	/*
478 	 * mapstart should be after initrd_end
479 	 */
480 	if (initrd_end)
481 		mapstart = max(mapstart, (unsigned long)PFN_UP(__pa(initrd_end)));
482 #endif
483 
484 	/*
485 	 * check that mapstart doesn't overlap with any of
486 	 * memory regions that have been reserved through eg. DTB
487 	 */
488 	bootmap_size = bootmap_bytes(max_low_pfn - min_low_pfn);
489 
490 	bootmap_valid = memory_region_available(PFN_PHYS(mapstart),
491 						bootmap_size);
492 	for (i = 0; i < boot_mem_map.nr_map && !bootmap_valid; i++) {
493 		unsigned long mapstart_addr;
494 
495 		switch (boot_mem_map.map[i].type) {
496 		case BOOT_MEM_RESERVED:
497 			mapstart_addr = PFN_ALIGN(boot_mem_map.map[i].addr +
498 						boot_mem_map.map[i].size);
499 			if (PHYS_PFN(mapstart_addr) < mapstart)
500 				break;
501 
502 			bootmap_valid = memory_region_available(mapstart_addr,
503 								bootmap_size);
504 			if (bootmap_valid)
505 				mapstart = PHYS_PFN(mapstart_addr);
506 			break;
507 		default:
508 			break;
509 		}
510 	}
511 
512 	if (!bootmap_valid)
513 		panic("No memory area to place a bootmap bitmap");
514 
515 	/*
516 	 * Initialize the boot-time allocator with low memory only.
517 	 */
518 	if (bootmap_size != init_bootmem_node(NODE_DATA(0), mapstart,
519 					 min_low_pfn, max_low_pfn))
520 		panic("Unexpected memory size required for bootmap");
521 
522 	for (i = 0; i < boot_mem_map.nr_map; i++) {
523 		unsigned long start, end;
524 
525 		start = PFN_UP(boot_mem_map.map[i].addr);
526 		end = PFN_DOWN(boot_mem_map.map[i].addr
527 				+ boot_mem_map.map[i].size);
528 
529 		if (start <= min_low_pfn)
530 			start = min_low_pfn;
531 		if (start >= end)
532 			continue;
533 
534 #ifndef CONFIG_HIGHMEM
535 		if (end > max_low_pfn)
536 			end = max_low_pfn;
537 
538 		/*
539 		 * ... finally, is the area going away?
540 		 */
541 		if (end <= start)
542 			continue;
543 #endif
544 
545 		memblock_add_node(PFN_PHYS(start), PFN_PHYS(end - start), 0);
546 	}
547 
548 	/*
549 	 * Register fully available low RAM pages with the bootmem allocator.
550 	 */
551 	for (i = 0; i < boot_mem_map.nr_map; i++) {
552 		unsigned long start, end, size;
553 
554 		start = PFN_UP(boot_mem_map.map[i].addr);
555 		end   = PFN_DOWN(boot_mem_map.map[i].addr
556 				    + boot_mem_map.map[i].size);
557 
558 		/*
559 		 * Reserve usable memory.
560 		 */
561 		switch (boot_mem_map.map[i].type) {
562 		case BOOT_MEM_RAM:
563 			break;
564 		case BOOT_MEM_INIT_RAM:
565 			memory_present(0, start, end);
566 			continue;
567 		default:
568 			/* Not usable memory */
569 			if (start > min_low_pfn && end < max_low_pfn)
570 				reserve_bootmem(boot_mem_map.map[i].addr,
571 						boot_mem_map.map[i].size,
572 						BOOTMEM_DEFAULT);
573 			continue;
574 		}
575 
576 		/*
577 		 * We are rounding up the start address of usable memory
578 		 * and at the end of the usable range downwards.
579 		 */
580 		if (start >= max_low_pfn)
581 			continue;
582 		if (start < reserved_end)
583 			start = reserved_end;
584 		if (end > max_low_pfn)
585 			end = max_low_pfn;
586 
587 		/*
588 		 * ... finally, is the area going away?
589 		 */
590 		if (end <= start)
591 			continue;
592 		size = end - start;
593 
594 		/* Register lowmem ranges */
595 		free_bootmem(PFN_PHYS(start), size << PAGE_SHIFT);
596 		memory_present(0, start, end);
597 	}
598 
599 	/*
600 	 * Reserve the bootmap memory.
601 	 */
602 	reserve_bootmem(PFN_PHYS(mapstart), bootmap_size, BOOTMEM_DEFAULT);
603 
604 #ifdef CONFIG_RELOCATABLE
605 	/*
606 	 * The kernel reserves all memory below its _end symbol as bootmem,
607 	 * but the kernel may now be at a much higher address. The memory
608 	 * between the original and new locations may be returned to the system.
609 	 */
610 	if (__pa_symbol(_text) > __pa_symbol(VMLINUX_LOAD_ADDRESS)) {
611 		unsigned long offset;
612 		extern void show_kernel_relocation(const char *level);
613 
614 		offset = __pa_symbol(_text) - __pa_symbol(VMLINUX_LOAD_ADDRESS);
615 		free_bootmem(__pa_symbol(VMLINUX_LOAD_ADDRESS), offset);
616 
617 #if defined(CONFIG_DEBUG_KERNEL) && defined(CONFIG_DEBUG_INFO)
618 		/*
619 		 * This information is necessary when debugging the kernel
620 		 * But is a security vulnerability otherwise!
621 		 */
622 		show_kernel_relocation(KERN_INFO);
623 #endif
624 	}
625 #endif
626 
627 	/*
628 	 * Reserve initrd memory if needed.
629 	 */
630 	finalize_initrd();
631 }
632 
633 #endif	/* CONFIG_SGI_IP27 */
634 
635 /*
636  * arch_mem_init - initialize memory management subsystem
637  *
638  *  o plat_mem_setup() detects the memory configuration and will record detected
639  *    memory areas using add_memory_region.
640  *
641  * At this stage the memory configuration of the system is known to the
642  * kernel but generic memory management system is still entirely uninitialized.
643  *
644  *  o bootmem_init()
645  *  o sparse_init()
646  *  o paging_init()
647  *  o dma_contiguous_reserve()
648  *
649  * At this stage the bootmem allocator is ready to use.
650  *
651  * NOTE: historically plat_mem_setup did the entire platform initialization.
652  *	 This was rather impractical because it meant plat_mem_setup had to
653  * get away without any kind of memory allocator.  To keep old code from
654  * breaking plat_setup was just renamed to plat_mem_setup and a second platform
655  * initialization hook for anything else was introduced.
656  */
657 
658 static int usermem __initdata;
659 
660 static int __init early_parse_mem(char *p)
661 {
662 	phys_addr_t start, size;
663 
664 	/*
665 	 * If a user specifies memory size, we
666 	 * blow away any automatically generated
667 	 * size.
668 	 */
669 	if (usermem == 0) {
670 		boot_mem_map.nr_map = 0;
671 		usermem = 1;
672 	}
673 	start = 0;
674 	size = memparse(p, &p);
675 	if (*p == '@')
676 		start = memparse(p + 1, &p);
677 
678 	add_memory_region(start, size, BOOT_MEM_RAM);
679 
680 	return 0;
681 }
682 early_param("mem", early_parse_mem);
683 
684 static int __init early_parse_memmap(char *p)
685 {
686 	char *oldp;
687 	u64 start_at, mem_size;
688 
689 	if (!p)
690 		return -EINVAL;
691 
692 	if (!strncmp(p, "exactmap", 8)) {
693 		pr_err("\"memmap=exactmap\" invalid on MIPS\n");
694 		return 0;
695 	}
696 
697 	oldp = p;
698 	mem_size = memparse(p, &p);
699 	if (p == oldp)
700 		return -EINVAL;
701 
702 	if (*p == '@') {
703 		start_at = memparse(p+1, &p);
704 		add_memory_region(start_at, mem_size, BOOT_MEM_RAM);
705 	} else if (*p == '#') {
706 		pr_err("\"memmap=nn#ss\" (force ACPI data) invalid on MIPS\n");
707 		return -EINVAL;
708 	} else if (*p == '$') {
709 		start_at = memparse(p+1, &p);
710 		add_memory_region(start_at, mem_size, BOOT_MEM_RESERVED);
711 	} else {
712 		pr_err("\"memmap\" invalid format!\n");
713 		return -EINVAL;
714 	}
715 
716 	if (*p == '\0') {
717 		usermem = 1;
718 		return 0;
719 	} else
720 		return -EINVAL;
721 }
722 early_param("memmap", early_parse_memmap);
723 
724 #ifdef CONFIG_PROC_VMCORE
725 unsigned long setup_elfcorehdr, setup_elfcorehdr_size;
726 static int __init early_parse_elfcorehdr(char *p)
727 {
728 	int i;
729 
730 	setup_elfcorehdr = memparse(p, &p);
731 
732 	for (i = 0; i < boot_mem_map.nr_map; i++) {
733 		unsigned long start = boot_mem_map.map[i].addr;
734 		unsigned long end = (boot_mem_map.map[i].addr +
735 				     boot_mem_map.map[i].size);
736 		if (setup_elfcorehdr >= start && setup_elfcorehdr < end) {
737 			/*
738 			 * Reserve from the elf core header to the end of
739 			 * the memory segment, that should all be kdump
740 			 * reserved memory.
741 			 */
742 			setup_elfcorehdr_size = end - setup_elfcorehdr;
743 			break;
744 		}
745 	}
746 	/*
747 	 * If we don't find it in the memory map, then we shouldn't
748 	 * have to worry about it, as the new kernel won't use it.
749 	 */
750 	return 0;
751 }
752 early_param("elfcorehdr", early_parse_elfcorehdr);
753 #endif
754 
755 static void __init arch_mem_addpart(phys_addr_t mem, phys_addr_t end, int type)
756 {
757 	phys_addr_t size;
758 	int i;
759 
760 	size = end - mem;
761 	if (!size)
762 		return;
763 
764 	/* Make sure it is in the boot_mem_map */
765 	for (i = 0; i < boot_mem_map.nr_map; i++) {
766 		if (mem >= boot_mem_map.map[i].addr &&
767 		    mem < (boot_mem_map.map[i].addr +
768 			   boot_mem_map.map[i].size))
769 			return;
770 	}
771 	add_memory_region(mem, size, type);
772 }
773 
774 #ifdef CONFIG_KEXEC
775 static inline unsigned long long get_total_mem(void)
776 {
777 	unsigned long long total;
778 
779 	total = max_pfn - min_low_pfn;
780 	return total << PAGE_SHIFT;
781 }
782 
783 static void __init mips_parse_crashkernel(void)
784 {
785 	unsigned long long total_mem;
786 	unsigned long long crash_size, crash_base;
787 	int ret;
788 
789 	total_mem = get_total_mem();
790 	ret = parse_crashkernel(boot_command_line, total_mem,
791 				&crash_size, &crash_base);
792 	if (ret != 0 || crash_size <= 0)
793 		return;
794 
795 	if (!memory_region_available(crash_base, crash_size)) {
796 		pr_warn("Invalid memory region reserved for crash kernel\n");
797 		return;
798 	}
799 
800 	crashk_res.start = crash_base;
801 	crashk_res.end	 = crash_base + crash_size - 1;
802 }
803 
804 static void __init request_crashkernel(struct resource *res)
805 {
806 	int ret;
807 
808 	if (crashk_res.start == crashk_res.end)
809 		return;
810 
811 	ret = request_resource(res, &crashk_res);
812 	if (!ret)
813 		pr_info("Reserving %ldMB of memory at %ldMB for crashkernel\n",
814 			(unsigned long)((crashk_res.end -
815 					 crashk_res.start + 1) >> 20),
816 			(unsigned long)(crashk_res.start  >> 20));
817 }
818 #else /* !defined(CONFIG_KEXEC)		*/
819 static void __init mips_parse_crashkernel(void)
820 {
821 }
822 
823 static void __init request_crashkernel(struct resource *res)
824 {
825 }
826 #endif /* !defined(CONFIG_KEXEC)  */
827 
828 #define USE_PROM_CMDLINE	IS_ENABLED(CONFIG_MIPS_CMDLINE_FROM_BOOTLOADER)
829 #define USE_DTB_CMDLINE		IS_ENABLED(CONFIG_MIPS_CMDLINE_FROM_DTB)
830 #define EXTEND_WITH_PROM	IS_ENABLED(CONFIG_MIPS_CMDLINE_DTB_EXTEND)
831 #define BUILTIN_EXTEND_WITH_PROM	\
832 	IS_ENABLED(CONFIG_MIPS_CMDLINE_BUILTIN_EXTEND)
833 
834 static void __init arch_mem_init(char **cmdline_p)
835 {
836 	struct memblock_region *reg;
837 	extern void plat_mem_setup(void);
838 
839 #if defined(CONFIG_CMDLINE_BOOL) && defined(CONFIG_CMDLINE_OVERRIDE)
840 	strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
841 #else
842 	if ((USE_PROM_CMDLINE && arcs_cmdline[0]) ||
843 	    (USE_DTB_CMDLINE && !boot_command_line[0]))
844 		strlcpy(boot_command_line, arcs_cmdline, COMMAND_LINE_SIZE);
845 
846 	if (EXTEND_WITH_PROM && arcs_cmdline[0]) {
847 		if (boot_command_line[0])
848 			strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
849 		strlcat(boot_command_line, arcs_cmdline, COMMAND_LINE_SIZE);
850 	}
851 
852 #if defined(CONFIG_CMDLINE_BOOL)
853 	if (builtin_cmdline[0]) {
854 		if (boot_command_line[0])
855 			strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
856 		strlcat(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
857 	}
858 
859 	if (BUILTIN_EXTEND_WITH_PROM && arcs_cmdline[0]) {
860 		if (boot_command_line[0])
861 			strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
862 		strlcat(boot_command_line, arcs_cmdline, COMMAND_LINE_SIZE);
863 	}
864 #endif
865 #endif
866 
867 	/* call board setup routine */
868 	plat_mem_setup();
869 
870 	/*
871 	 * Make sure all kernel memory is in the maps.  The "UP" and
872 	 * "DOWN" are opposite for initdata since if it crosses over
873 	 * into another memory section you don't want that to be
874 	 * freed when the initdata is freed.
875 	 */
876 	arch_mem_addpart(PFN_DOWN(__pa_symbol(&_text)) << PAGE_SHIFT,
877 			 PFN_UP(__pa_symbol(&_edata)) << PAGE_SHIFT,
878 			 BOOT_MEM_RAM);
879 	arch_mem_addpart(PFN_UP(__pa_symbol(&__init_begin)) << PAGE_SHIFT,
880 			 PFN_DOWN(__pa_symbol(&__init_end)) << PAGE_SHIFT,
881 			 BOOT_MEM_INIT_RAM);
882 
883 	pr_info("Determined physical RAM map:\n");
884 	print_memory_map();
885 
886 	strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
887 
888 	*cmdline_p = command_line;
889 
890 	parse_early_param();
891 
892 	if (usermem) {
893 		pr_info("User-defined physical RAM map:\n");
894 		print_memory_map();
895 	}
896 
897 	early_init_fdt_reserve_self();
898 	early_init_fdt_scan_reserved_mem();
899 
900 	bootmem_init();
901 #ifdef CONFIG_PROC_VMCORE
902 	if (setup_elfcorehdr && setup_elfcorehdr_size) {
903 		printk(KERN_INFO "kdump reserved memory at %lx-%lx\n",
904 		       setup_elfcorehdr, setup_elfcorehdr_size);
905 		reserve_bootmem(setup_elfcorehdr, setup_elfcorehdr_size,
906 				BOOTMEM_DEFAULT);
907 	}
908 #endif
909 
910 	mips_parse_crashkernel();
911 #ifdef CONFIG_KEXEC
912 	if (crashk_res.start != crashk_res.end)
913 		reserve_bootmem(crashk_res.start,
914 				crashk_res.end - crashk_res.start + 1,
915 				BOOTMEM_DEFAULT);
916 #endif
917 	device_tree_init();
918 	sparse_init();
919 	plat_swiotlb_setup();
920 
921 	dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
922 	/* Tell bootmem about cma reserved memblock section */
923 	for_each_memblock(reserved, reg)
924 		if (reg->size != 0)
925 			reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT);
926 
927 	reserve_bootmem_region(__pa_symbol(&__nosave_begin),
928 			__pa_symbol(&__nosave_end)); /* Reserve for hibernation */
929 }
930 
931 static void __init resource_init(void)
932 {
933 	int i;
934 
935 	if (UNCAC_BASE != IO_BASE)
936 		return;
937 
938 	code_resource.start = __pa_symbol(&_text);
939 	code_resource.end = __pa_symbol(&_etext) - 1;
940 	data_resource.start = __pa_symbol(&_etext);
941 	data_resource.end = __pa_symbol(&_edata) - 1;
942 	bss_resource.start = __pa_symbol(&__bss_start);
943 	bss_resource.end = __pa_symbol(&__bss_stop) - 1;
944 
945 	for (i = 0; i < boot_mem_map.nr_map; i++) {
946 		struct resource *res;
947 		unsigned long start, end;
948 
949 		start = boot_mem_map.map[i].addr;
950 		end = boot_mem_map.map[i].addr + boot_mem_map.map[i].size - 1;
951 		if (start >= HIGHMEM_START)
952 			continue;
953 		if (end >= HIGHMEM_START)
954 			end = HIGHMEM_START - 1;
955 
956 		res = alloc_bootmem(sizeof(struct resource));
957 
958 		res->start = start;
959 		res->end = end;
960 		res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
961 
962 		switch (boot_mem_map.map[i].type) {
963 		case BOOT_MEM_RAM:
964 		case BOOT_MEM_INIT_RAM:
965 		case BOOT_MEM_ROM_DATA:
966 			res->name = "System RAM";
967 			res->flags |= IORESOURCE_SYSRAM;
968 			break;
969 		case BOOT_MEM_RESERVED:
970 		default:
971 			res->name = "reserved";
972 		}
973 
974 		request_resource(&iomem_resource, res);
975 
976 		/*
977 		 *  We don't know which RAM region contains kernel data,
978 		 *  so we try it repeatedly and let the resource manager
979 		 *  test it.
980 		 */
981 		request_resource(res, &code_resource);
982 		request_resource(res, &data_resource);
983 		request_resource(res, &bss_resource);
984 		request_crashkernel(res);
985 	}
986 }
987 
988 #ifdef CONFIG_SMP
989 static void __init prefill_possible_map(void)
990 {
991 	int i, possible = num_possible_cpus();
992 
993 	if (possible > nr_cpu_ids)
994 		possible = nr_cpu_ids;
995 
996 	for (i = 0; i < possible; i++)
997 		set_cpu_possible(i, true);
998 	for (; i < NR_CPUS; i++)
999 		set_cpu_possible(i, false);
1000 
1001 	nr_cpu_ids = possible;
1002 }
1003 #else
1004 static inline void prefill_possible_map(void) {}
1005 #endif
1006 
1007 void __init setup_arch(char **cmdline_p)
1008 {
1009 	cpu_probe();
1010 	mips_cm_probe();
1011 	prom_init();
1012 
1013 	setup_early_fdc_console();
1014 #ifdef CONFIG_EARLY_PRINTK
1015 	setup_early_printk();
1016 #endif
1017 	cpu_report();
1018 	check_bugs_early();
1019 
1020 #if defined(CONFIG_VT)
1021 #if defined(CONFIG_VGA_CONSOLE)
1022 	conswitchp = &vga_con;
1023 #elif defined(CONFIG_DUMMY_CONSOLE)
1024 	conswitchp = &dummy_con;
1025 #endif
1026 #endif
1027 
1028 	arch_mem_init(cmdline_p);
1029 
1030 	resource_init();
1031 	plat_smp_setup();
1032 	prefill_possible_map();
1033 
1034 	cpu_cache_init();
1035 	paging_init();
1036 }
1037 
1038 unsigned long kernelsp[NR_CPUS];
1039 unsigned long fw_arg0, fw_arg1, fw_arg2, fw_arg3;
1040 
1041 #ifdef CONFIG_USE_OF
1042 unsigned long fw_passed_dtb;
1043 #endif
1044 
1045 #ifdef CONFIG_DEBUG_FS
1046 struct dentry *mips_debugfs_dir;
1047 static int __init debugfs_mips(void)
1048 {
1049 	struct dentry *d;
1050 
1051 	d = debugfs_create_dir("mips", NULL);
1052 	if (!d)
1053 		return -ENOMEM;
1054 	mips_debugfs_dir = d;
1055 	return 0;
1056 }
1057 arch_initcall(debugfs_mips);
1058 #endif
1059 
1060 #if defined(CONFIG_DMA_MAYBE_COHERENT) && !defined(CONFIG_DMA_PERDEV_COHERENT)
1061 /* User defined DMA coherency from command line. */
1062 enum coherent_io_user_state coherentio = IO_COHERENCE_DEFAULT;
1063 EXPORT_SYMBOL_GPL(coherentio);
1064 int hw_coherentio = 0;	/* Actual hardware supported DMA coherency setting. */
1065 
1066 static int __init setcoherentio(char *str)
1067 {
1068 	coherentio = IO_COHERENCE_ENABLED;
1069 	pr_info("Hardware DMA cache coherency (command line)\n");
1070 	return 0;
1071 }
1072 early_param("coherentio", setcoherentio);
1073 
1074 static int __init setnocoherentio(char *str)
1075 {
1076 	coherentio = IO_COHERENCE_DISABLED;
1077 	pr_info("Software DMA cache coherency (command line)\n");
1078 	return 0;
1079 }
1080 early_param("nocoherentio", setnocoherentio);
1081 #endif
1082