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