Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4 *
5 * Derived from MIPS:
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/acpi.h>
15#include <linux/cpu.h>
16#include <linux/dmi.h>
17#include <linux/efi.h>
18#include <linux/export.h>
19#include <linux/memblock.h>
20#include <linux/initrd.h>
21#include <linux/ioport.h>
22#include <linux/kexec.h>
23#include <linux/crash_dump.h>
24#include <linux/root_dev.h>
25#include <linux/console.h>
26#include <linux/pfn.h>
27#include <linux/platform_device.h>
28#include <linux/sizes.h>
29#include <linux/device.h>
30#include <linux/dma-map-ops.h>
31#include <linux/libfdt.h>
32#include <linux/of_fdt.h>
33#include <linux/of_address.h>
34#include <linux/suspend.h>
35#include <linux/swiotlb.h>
36
37#include <asm/addrspace.h>
38#include <asm/alternative.h>
39#include <asm/bootinfo.h>
40#include <asm/cache.h>
41#include <asm/cpu.h>
42#include <asm/dma.h>
43#include <asm/efi.h>
44#include <asm/loongson.h>
45#include <asm/numa.h>
46#include <asm/pgalloc.h>
47#include <asm/sections.h>
48#include <asm/setup.h>
49#include <asm/time.h>
50
51#define SMBIOS_BIOSSIZE_OFFSET 0x09
52#define SMBIOS_BIOSEXTERN_OFFSET 0x13
53#define SMBIOS_FREQLOW_OFFSET 0x16
54#define SMBIOS_FREQHIGH_OFFSET 0x17
55#define SMBIOS_FREQLOW_MASK 0xFF
56#define SMBIOS_CORE_PACKAGE_OFFSET 0x23
57#define LOONGSON_EFI_ENABLE (1 << 3)
58
59unsigned long fw_arg0, fw_arg1, fw_arg2;
60DEFINE_PER_CPU(unsigned long, kernelsp);
61struct cpuinfo_loongarch cpu_data[NR_CPUS] __read_mostly;
62
63EXPORT_SYMBOL(cpu_data);
64
65struct loongson_board_info b_info;
66static const char dmi_empty_string[] = " ";
67
68/*
69 * Setup information
70 *
71 * These are initialized so they are in the .data section
72 */
73char init_command_line[COMMAND_LINE_SIZE] __initdata;
74
75static int num_standard_resources;
76static struct resource *standard_resources;
77
78static struct resource code_resource = { .name = "Kernel code", };
79static struct resource data_resource = { .name = "Kernel data", };
80static struct resource bss_resource = { .name = "Kernel bss", };
81
82const char *get_system_type(void)
83{
84 return "generic-loongson-machine";
85}
86
87void __init arch_cpu_finalize_init(void)
88{
89 alternative_instructions();
90}
91
92static const char *dmi_string_parse(const struct dmi_header *dm, u8 s)
93{
94 const u8 *bp = ((u8 *) dm) + dm->length;
95
96 if (s) {
97 s--;
98 while (s > 0 && *bp) {
99 bp += strlen(bp) + 1;
100 s--;
101 }
102
103 if (*bp != 0) {
104 size_t len = strlen(bp)+1;
105 size_t cmp_len = len > 8 ? 8 : len;
106
107 if (!memcmp(bp, dmi_empty_string, cmp_len))
108 return dmi_empty_string;
109
110 return bp;
111 }
112 }
113
114 return "";
115}
116
117static void __init parse_cpu_table(const struct dmi_header *dm)
118{
119 long freq_temp = 0;
120 char *dmi_data = (char *)dm;
121
122 freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
123 ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
124 cpu_clock_freq = freq_temp * 1000000;
125
126 loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
127 loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_CORE_PACKAGE_OFFSET);
128
129 pr_info("CpuClock = %llu\n", cpu_clock_freq);
130}
131
132static void __init parse_bios_table(const struct dmi_header *dm)
133{
134 char *dmi_data = (char *)dm;
135
136 b_info.bios_size = (*(dmi_data + SMBIOS_BIOSSIZE_OFFSET) + 1) << 6;
137}
138
139static void __init find_tokens(const struct dmi_header *dm, void *dummy)
140{
141 switch (dm->type) {
142 case 0x0: /* Extern BIOS */
143 parse_bios_table(dm);
144 break;
145 case 0x4: /* Calling interface */
146 parse_cpu_table(dm);
147 break;
148 }
149}
150static void __init smbios_parse(void)
151{
152 b_info.bios_vendor = (void *)dmi_get_system_info(DMI_BIOS_VENDOR);
153 b_info.bios_version = (void *)dmi_get_system_info(DMI_BIOS_VERSION);
154 b_info.bios_release_date = (void *)dmi_get_system_info(DMI_BIOS_DATE);
155 b_info.board_vendor = (void *)dmi_get_system_info(DMI_BOARD_VENDOR);
156 b_info.board_name = (void *)dmi_get_system_info(DMI_BOARD_NAME);
157 dmi_walk(find_tokens, NULL);
158}
159
160#ifdef CONFIG_ARCH_WRITECOMBINE
161bool wc_enabled = true;
162#else
163bool wc_enabled = false;
164#endif
165
166EXPORT_SYMBOL(wc_enabled);
167
168static int __init setup_writecombine(char *p)
169{
170 if (!strcmp(p, "on"))
171 wc_enabled = true;
172 else if (!strcmp(p, "off"))
173 wc_enabled = false;
174 else
175 pr_warn("Unknown writecombine setting \"%s\".\n", p);
176
177 return 0;
178}
179early_param("writecombine", setup_writecombine);
180
181static int usermem __initdata;
182
183static int __init early_parse_mem(char *p)
184{
185 phys_addr_t start, size;
186
187 if (!p) {
188 pr_err("mem parameter is empty, do nothing\n");
189 return -EINVAL;
190 }
191
192 /*
193 * If a user specifies memory size, we
194 * blow away any automatically generated
195 * size.
196 */
197 if (usermem == 0) {
198 usermem = 1;
199 memblock_remove(memblock_start_of_DRAM(),
200 memblock_end_of_DRAM() - memblock_start_of_DRAM());
201 }
202 start = 0;
203 size = memparse(p, &p);
204 if (*p == '@')
205 start = memparse(p + 1, &p);
206 else {
207 pr_err("Invalid format!\n");
208 return -EINVAL;
209 }
210
211 if (!IS_ENABLED(CONFIG_NUMA))
212 memblock_add(start, size);
213 else
214 memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
215
216 return 0;
217}
218early_param("mem", early_parse_mem);
219
220static void __init arch_reserve_vmcore(void)
221{
222#ifdef CONFIG_PROC_VMCORE
223 u64 i;
224 phys_addr_t start, end;
225
226 if (!is_kdump_kernel())
227 return;
228
229 if (!elfcorehdr_size) {
230 for_each_mem_range(i, &start, &end) {
231 if (elfcorehdr_addr >= start && elfcorehdr_addr < end) {
232 /*
233 * Reserve from the elf core header to the end of
234 * the memory segment, that should all be kdump
235 * reserved memory.
236 */
237 elfcorehdr_size = end - elfcorehdr_addr;
238 break;
239 }
240 }
241 }
242
243 if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) {
244 pr_warn("elfcorehdr is overlapped\n");
245 return;
246 }
247
248 memblock_reserve(elfcorehdr_addr, elfcorehdr_size);
249
250 pr_info("Reserving %llu KiB of memory at 0x%llx for elfcorehdr\n",
251 elfcorehdr_size >> 10, elfcorehdr_addr);
252#endif
253}
254
255static void __init arch_reserve_crashkernel(void)
256{
257 int ret;
258 unsigned long long low_size = 0;
259 unsigned long long crash_base, crash_size;
260 char *cmdline = boot_command_line;
261 bool high = false;
262
263 if (!IS_ENABLED(CONFIG_KEXEC_CORE))
264 return;
265
266 ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
267 &crash_size, &crash_base, &low_size, &high);
268 if (ret)
269 return;
270
271 reserve_crashkernel_generic(cmdline, crash_size, crash_base, low_size, high);
272}
273
274static void __init fdt_setup(void)
275{
276#ifdef CONFIG_OF_EARLY_FLATTREE
277 void *fdt_pointer;
278
279 /* ACPI-based systems do not require parsing fdt */
280 if (acpi_os_get_root_pointer())
281 return;
282
283 /* Prefer to use built-in dtb, checking its legality first. */
284 if (!fdt_check_header(__dtb_start))
285 fdt_pointer = __dtb_start;
286 else
287 fdt_pointer = efi_fdt_pointer(); /* Fallback to firmware dtb */
288
289 if (!fdt_pointer || fdt_check_header(fdt_pointer))
290 return;
291
292 early_init_dt_scan(fdt_pointer);
293 early_init_fdt_reserve_self();
294
295 max_low_pfn = PFN_PHYS(memblock_end_of_DRAM());
296#endif
297}
298
299static void __init bootcmdline_init(char **cmdline_p)
300{
301 /*
302 * If CONFIG_CMDLINE_FORCE is enabled then initializing the command line
303 * is trivial - we simply use the built-in command line unconditionally &
304 * unmodified.
305 */
306 if (IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
307 strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
308 goto out;
309 }
310
311#ifdef CONFIG_OF_FLATTREE
312 /*
313 * If CONFIG_CMDLINE_BOOTLOADER is enabled and we are in FDT-based system,
314 * the boot_command_line will be overwritten by early_init_dt_scan_chosen().
315 * So we need to append init_command_line (the original copy of boot_command_line)
316 * to boot_command_line.
317 */
318 if (initial_boot_params) {
319 if (boot_command_line[0])
320 strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
321
322 if (!strstr(boot_command_line, init_command_line))
323 strlcat(boot_command_line, init_command_line, COMMAND_LINE_SIZE);
324
325 goto out;
326 }
327#endif
328
329 /*
330 * Append built-in command line to the bootloader command line if
331 * CONFIG_CMDLINE_EXTEND is enabled.
332 */
333 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) && CONFIG_CMDLINE[0]) {
334 strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
335 strlcat(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
336 }
337
338 /*
339 * Use built-in command line if the bootloader command line is empty.
340 */
341 if (IS_ENABLED(CONFIG_CMDLINE_BOOTLOADER) && !boot_command_line[0])
342 strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
343
344out:
345 *cmdline_p = boot_command_line;
346}
347
348void __init platform_init(void)
349{
350 arch_reserve_vmcore();
351 arch_reserve_crashkernel();
352
353#ifdef CONFIG_ACPI_TABLE_UPGRADE
354 acpi_table_upgrade();
355#endif
356#ifdef CONFIG_ACPI
357 acpi_gbl_use_default_register_widths = false;
358 acpi_boot_table_init();
359#endif
360
361 early_init_fdt_scan_reserved_mem();
362 unflatten_and_copy_device_tree();
363
364#ifdef CONFIG_NUMA
365 init_numa_memory();
366#endif
367 dmi_setup();
368 smbios_parse();
369 pr_info("The BIOS Version: %s\n", b_info.bios_version);
370
371 efi_runtime_init();
372}
373
374static void __init check_kernel_sections_mem(void)
375{
376 phys_addr_t start = __pa_symbol(&_text);
377 phys_addr_t size = __pa_symbol(&_end) - start;
378
379 if (!memblock_is_region_memory(start, size)) {
380 pr_info("Kernel sections are not in the memory maps\n");
381 memblock_add(start, size);
382 }
383}
384
385/*
386 * arch_mem_init - initialize memory management subsystem
387 */
388static void __init arch_mem_init(char **cmdline_p)
389{
390 if (usermem)
391 pr_info("User-defined physical RAM map overwrite\n");
392
393 check_kernel_sections_mem();
394
395 /*
396 * In order to reduce the possibility of kernel panic when failed to
397 * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
398 * low memory as small as possible before swiotlb_init(), so make
399 * sparse_init() using top-down allocation.
400 */
401 memblock_set_bottom_up(false);
402 sparse_init();
403 memblock_set_bottom_up(true);
404
405 swiotlb_init(true, SWIOTLB_VERBOSE);
406
407 dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
408
409 /* Reserve for hibernation. */
410 register_nosave_region(PFN_DOWN(__pa_symbol(&__nosave_begin)),
411 PFN_UP(__pa_symbol(&__nosave_end)));
412
413 memblock_dump_all();
414
415 early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
416}
417
418static void __init resource_init(void)
419{
420 long i = 0;
421 size_t res_size;
422 struct resource *res;
423 struct memblock_region *region;
424
425 code_resource.start = __pa_symbol(&_text);
426 code_resource.end = __pa_symbol(&_etext) - 1;
427 data_resource.start = __pa_symbol(&_etext);
428 data_resource.end = __pa_symbol(&_edata) - 1;
429 bss_resource.start = __pa_symbol(&__bss_start);
430 bss_resource.end = __pa_symbol(&__bss_stop) - 1;
431
432 num_standard_resources = memblock.memory.cnt;
433 res_size = num_standard_resources * sizeof(*standard_resources);
434 standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
435
436 for_each_mem_region(region) {
437 res = &standard_resources[i++];
438 if (!memblock_is_nomap(region)) {
439 res->name = "System RAM";
440 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
441 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
442 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
443 } else {
444 res->name = "Reserved";
445 res->flags = IORESOURCE_MEM;
446 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
447 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
448 }
449
450 request_resource(&iomem_resource, res);
451
452 /*
453 * We don't know which RAM region contains kernel data,
454 * so we try it repeatedly and let the resource manager
455 * test it.
456 */
457 request_resource(res, &code_resource);
458 request_resource(res, &data_resource);
459 request_resource(res, &bss_resource);
460 }
461}
462
463static int __init add_legacy_isa_io(struct fwnode_handle *fwnode,
464 resource_size_t hw_start, resource_size_t size)
465{
466 int ret = 0;
467 unsigned long vaddr;
468 struct logic_pio_hwaddr *range;
469
470 range = kzalloc(sizeof(*range), GFP_ATOMIC);
471 if (!range)
472 return -ENOMEM;
473
474 range->fwnode = fwnode;
475 range->size = size = round_up(size, PAGE_SIZE);
476 range->hw_start = hw_start;
477 range->flags = LOGIC_PIO_CPU_MMIO;
478
479 ret = logic_pio_register_range(range);
480 if (ret) {
481 kfree(range);
482 return ret;
483 }
484
485 /* Legacy ISA must placed at the start of PCI_IOBASE */
486 if (range->io_start != 0) {
487 logic_pio_unregister_range(range);
488 kfree(range);
489 return -EINVAL;
490 }
491
492 vaddr = (unsigned long)(PCI_IOBASE + range->io_start);
493 ioremap_page_range(vaddr, vaddr + size, hw_start, pgprot_device(PAGE_KERNEL));
494
495 return 0;
496}
497
498static __init int arch_reserve_pio_range(void)
499{
500 struct device_node *np;
501
502 for_each_node_by_name(np, "isa") {
503 struct of_range range;
504 struct of_range_parser parser;
505
506 pr_info("ISA Bridge: %pOF\n", np);
507
508 if (of_range_parser_init(&parser, np)) {
509 pr_info("Failed to parse resources.\n");
510 of_node_put(np);
511 break;
512 }
513
514 for_each_of_range(&parser, &range) {
515 switch (range.flags & IORESOURCE_TYPE_BITS) {
516 case IORESOURCE_IO:
517 pr_info(" IO 0x%016llx..0x%016llx -> 0x%016llx\n",
518 range.cpu_addr,
519 range.cpu_addr + range.size - 1,
520 range.bus_addr);
521 if (add_legacy_isa_io(&np->fwnode, range.cpu_addr, range.size))
522 pr_warn("Failed to reserve legacy IO in Logic PIO\n");
523 break;
524 case IORESOURCE_MEM:
525 pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx\n",
526 range.cpu_addr,
527 range.cpu_addr + range.size - 1,
528 range.bus_addr);
529 break;
530 }
531 }
532 }
533
534 return 0;
535}
536arch_initcall(arch_reserve_pio_range);
537
538static int __init reserve_memblock_reserved_regions(void)
539{
540 u64 i, j;
541
542 for (i = 0; i < num_standard_resources; ++i) {
543 struct resource *mem = &standard_resources[i];
544 phys_addr_t r_start, r_end, mem_size = resource_size(mem);
545
546 if (!memblock_is_region_reserved(mem->start, mem_size))
547 continue;
548
549 for_each_reserved_mem_range(j, &r_start, &r_end) {
550 resource_size_t start, end;
551
552 start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
553 end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
554
555 if (start > mem->end || end < mem->start)
556 continue;
557
558 reserve_region_with_split(mem, start, end, "Reserved");
559 }
560 }
561
562 return 0;
563}
564arch_initcall(reserve_memblock_reserved_regions);
565
566#ifdef CONFIG_SMP
567static void __init prefill_possible_map(void)
568{
569 int i, possible;
570
571 possible = num_processors + disabled_cpus;
572 if (possible > nr_cpu_ids)
573 possible = nr_cpu_ids;
574
575 pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
576 possible, max((possible - num_processors), 0));
577
578 for (i = 0; i < possible; i++)
579 set_cpu_possible(i, true);
580 for (; i < NR_CPUS; i++)
581 set_cpu_possible(i, false);
582
583 set_nr_cpu_ids(possible);
584}
585#endif
586
587void __init setup_arch(char **cmdline_p)
588{
589 cpu_probe();
590
591 init_environ();
592 efi_init();
593 fdt_setup();
594 memblock_init();
595 pagetable_init();
596 bootcmdline_init(cmdline_p);
597 parse_early_param();
598 reserve_initrd_mem();
599
600 platform_init();
601 arch_mem_init(cmdline_p);
602
603 resource_init();
604#ifdef CONFIG_SMP
605 plat_smp_setup();
606 prefill_possible_map();
607#endif
608
609 paging_init();
610
611#ifdef CONFIG_KASAN
612 kasan_init();
613#endif
614}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4 *
5 * Derived from MIPS:
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/acpi.h>
15#include <linux/cpu.h>
16#include <linux/dmi.h>
17#include <linux/efi.h>
18#include <linux/export.h>
19#include <linux/memblock.h>
20#include <linux/initrd.h>
21#include <linux/ioport.h>
22#include <linux/kexec.h>
23#include <linux/crash_dump.h>
24#include <linux/root_dev.h>
25#include <linux/console.h>
26#include <linux/pfn.h>
27#include <linux/platform_device.h>
28#include <linux/sizes.h>
29#include <linux/device.h>
30#include <linux/dma-map-ops.h>
31#include <linux/libfdt.h>
32#include <linux/of_fdt.h>
33#include <linux/of_address.h>
34#include <linux/suspend.h>
35#include <linux/swiotlb.h>
36
37#include <asm/addrspace.h>
38#include <asm/alternative.h>
39#include <asm/bootinfo.h>
40#include <asm/cache.h>
41#include <asm/cpu.h>
42#include <asm/dma.h>
43#include <asm/efi.h>
44#include <asm/loongson.h>
45#include <asm/numa.h>
46#include <asm/pgalloc.h>
47#include <asm/sections.h>
48#include <asm/setup.h>
49#include <asm/time.h>
50#include <asm/unwind.h>
51
52#define SMBIOS_BIOSSIZE_OFFSET 0x09
53#define SMBIOS_BIOSEXTERN_OFFSET 0x13
54#define SMBIOS_FREQLOW_OFFSET 0x16
55#define SMBIOS_FREQHIGH_OFFSET 0x17
56#define SMBIOS_FREQLOW_MASK 0xFF
57#define SMBIOS_CORE_PACKAGE_OFFSET 0x23
58#define LOONGSON_EFI_ENABLE (1 << 3)
59
60unsigned long fw_arg0, fw_arg1, fw_arg2;
61DEFINE_PER_CPU(unsigned long, kernelsp);
62struct cpuinfo_loongarch cpu_data[NR_CPUS] __read_mostly;
63
64EXPORT_SYMBOL(cpu_data);
65
66struct loongson_board_info b_info;
67static const char dmi_empty_string[] = " ";
68
69/*
70 * Setup information
71 *
72 * These are initialized so they are in the .data section
73 */
74char init_command_line[COMMAND_LINE_SIZE] __initdata;
75
76static int num_standard_resources;
77static struct resource *standard_resources;
78
79static struct resource code_resource = { .name = "Kernel code", };
80static struct resource data_resource = { .name = "Kernel data", };
81static struct resource bss_resource = { .name = "Kernel bss", };
82
83const char *get_system_type(void)
84{
85 return "generic-loongson-machine";
86}
87
88void __init arch_cpu_finalize_init(void)
89{
90 alternative_instructions();
91}
92
93static const char *dmi_string_parse(const struct dmi_header *dm, u8 s)
94{
95 const u8 *bp = ((u8 *) dm) + dm->length;
96
97 if (s) {
98 s--;
99 while (s > 0 && *bp) {
100 bp += strlen(bp) + 1;
101 s--;
102 }
103
104 if (*bp != 0) {
105 size_t len = strlen(bp)+1;
106 size_t cmp_len = len > 8 ? 8 : len;
107
108 if (!memcmp(bp, dmi_empty_string, cmp_len))
109 return dmi_empty_string;
110
111 return bp;
112 }
113 }
114
115 return "";
116}
117
118static void __init parse_cpu_table(const struct dmi_header *dm)
119{
120 long freq_temp = 0;
121 char *dmi_data = (char *)dm;
122
123 freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
124 ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
125 cpu_clock_freq = freq_temp * 1000000;
126
127 loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
128 loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_CORE_PACKAGE_OFFSET);
129
130 pr_info("CpuClock = %llu\n", cpu_clock_freq);
131}
132
133static void __init parse_bios_table(const struct dmi_header *dm)
134{
135 char *dmi_data = (char *)dm;
136
137 b_info.bios_size = (*(dmi_data + SMBIOS_BIOSSIZE_OFFSET) + 1) << 6;
138}
139
140static void __init find_tokens(const struct dmi_header *dm, void *dummy)
141{
142 switch (dm->type) {
143 case 0x0: /* Extern BIOS */
144 parse_bios_table(dm);
145 break;
146 case 0x4: /* Calling interface */
147 parse_cpu_table(dm);
148 break;
149 }
150}
151static void __init smbios_parse(void)
152{
153 b_info.bios_vendor = (void *)dmi_get_system_info(DMI_BIOS_VENDOR);
154 b_info.bios_version = (void *)dmi_get_system_info(DMI_BIOS_VERSION);
155 b_info.bios_release_date = (void *)dmi_get_system_info(DMI_BIOS_DATE);
156 b_info.board_vendor = (void *)dmi_get_system_info(DMI_BOARD_VENDOR);
157 b_info.board_name = (void *)dmi_get_system_info(DMI_BOARD_NAME);
158 dmi_walk(find_tokens, NULL);
159}
160
161#ifdef CONFIG_ARCH_WRITECOMBINE
162bool wc_enabled = true;
163#else
164bool wc_enabled = false;
165#endif
166
167EXPORT_SYMBOL(wc_enabled);
168
169static int __init setup_writecombine(char *p)
170{
171 if (!strcmp(p, "on"))
172 wc_enabled = true;
173 else if (!strcmp(p, "off"))
174 wc_enabled = false;
175 else
176 pr_warn("Unknown writecombine setting \"%s\".\n", p);
177
178 return 0;
179}
180early_param("writecombine", setup_writecombine);
181
182static int usermem __initdata;
183
184static int __init early_parse_mem(char *p)
185{
186 phys_addr_t start, size;
187
188 if (!p) {
189 pr_err("mem parameter is empty, do nothing\n");
190 return -EINVAL;
191 }
192
193 /*
194 * If a user specifies memory size, we
195 * blow away any automatically generated
196 * size.
197 */
198 if (usermem == 0) {
199 usermem = 1;
200 memblock_remove(memblock_start_of_DRAM(),
201 memblock_end_of_DRAM() - memblock_start_of_DRAM());
202 }
203 start = 0;
204 size = memparse(p, &p);
205 if (*p == '@')
206 start = memparse(p + 1, &p);
207 else {
208 pr_err("Invalid format!\n");
209 return -EINVAL;
210 }
211
212 if (!IS_ENABLED(CONFIG_NUMA))
213 memblock_add(start, size);
214 else
215 memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
216
217 return 0;
218}
219early_param("mem", early_parse_mem);
220
221static void __init arch_reserve_vmcore(void)
222{
223#ifdef CONFIG_PROC_VMCORE
224 u64 i;
225 phys_addr_t start, end;
226
227 if (!is_kdump_kernel())
228 return;
229
230 if (!elfcorehdr_size) {
231 for_each_mem_range(i, &start, &end) {
232 if (elfcorehdr_addr >= start && elfcorehdr_addr < end) {
233 /*
234 * Reserve from the elf core header to the end of
235 * the memory segment, that should all be kdump
236 * reserved memory.
237 */
238 elfcorehdr_size = end - elfcorehdr_addr;
239 break;
240 }
241 }
242 }
243
244 if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) {
245 pr_warn("elfcorehdr is overlapped\n");
246 return;
247 }
248
249 memblock_reserve(elfcorehdr_addr, elfcorehdr_size);
250
251 pr_info("Reserving %llu KiB of memory at 0x%llx for elfcorehdr\n",
252 elfcorehdr_size >> 10, elfcorehdr_addr);
253#endif
254}
255
256static void __init arch_reserve_crashkernel(void)
257{
258 int ret;
259 unsigned long long low_size = 0;
260 unsigned long long crash_base, crash_size;
261 char *cmdline = boot_command_line;
262 bool high = false;
263
264 if (!IS_ENABLED(CONFIG_CRASH_RESERVE))
265 return;
266
267 ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
268 &crash_size, &crash_base, &low_size, &high);
269 if (ret)
270 return;
271
272 reserve_crashkernel_generic(cmdline, crash_size, crash_base, low_size, high);
273}
274
275static void __init fdt_setup(void)
276{
277#ifdef CONFIG_OF_EARLY_FLATTREE
278 void *fdt_pointer;
279
280 /* ACPI-based systems do not require parsing fdt */
281 if (acpi_os_get_root_pointer())
282 return;
283
284 /* Prefer to use built-in dtb, checking its legality first. */
285 if (!fdt_check_header(__dtb_start))
286 fdt_pointer = __dtb_start;
287 else
288 fdt_pointer = efi_fdt_pointer(); /* Fallback to firmware dtb */
289
290 if (!fdt_pointer || fdt_check_header(fdt_pointer))
291 return;
292
293 early_init_dt_scan(fdt_pointer);
294 early_init_fdt_reserve_self();
295
296 max_low_pfn = PFN_PHYS(memblock_end_of_DRAM());
297#endif
298}
299
300static void __init bootcmdline_init(char **cmdline_p)
301{
302 /*
303 * If CONFIG_CMDLINE_FORCE is enabled then initializing the command line
304 * is trivial - we simply use the built-in command line unconditionally &
305 * unmodified.
306 */
307 if (IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
308 strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
309 goto out;
310 }
311
312#ifdef CONFIG_OF_FLATTREE
313 /*
314 * If CONFIG_CMDLINE_BOOTLOADER is enabled and we are in FDT-based system,
315 * the boot_command_line will be overwritten by early_init_dt_scan_chosen().
316 * So we need to append init_command_line (the original copy of boot_command_line)
317 * to boot_command_line.
318 */
319 if (initial_boot_params) {
320 if (boot_command_line[0])
321 strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
322
323 if (!strstr(boot_command_line, init_command_line))
324 strlcat(boot_command_line, init_command_line, COMMAND_LINE_SIZE);
325
326 goto out;
327 }
328#endif
329
330 /*
331 * Append built-in command line to the bootloader command line if
332 * CONFIG_CMDLINE_EXTEND is enabled.
333 */
334 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) && CONFIG_CMDLINE[0]) {
335 strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
336 strlcat(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
337 }
338
339 /*
340 * Use built-in command line if the bootloader command line is empty.
341 */
342 if (IS_ENABLED(CONFIG_CMDLINE_BOOTLOADER) && !boot_command_line[0])
343 strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
344
345out:
346 *cmdline_p = boot_command_line;
347}
348
349void __init platform_init(void)
350{
351 arch_reserve_vmcore();
352 arch_reserve_crashkernel();
353
354#ifdef CONFIG_ACPI_TABLE_UPGRADE
355 acpi_table_upgrade();
356#endif
357#ifdef CONFIG_ACPI
358 acpi_gbl_use_default_register_widths = false;
359 acpi_boot_table_init();
360#endif
361
362 early_init_fdt_scan_reserved_mem();
363 unflatten_and_copy_device_tree();
364
365#ifdef CONFIG_NUMA
366 init_numa_memory();
367#endif
368 dmi_setup();
369 smbios_parse();
370 pr_info("The BIOS Version: %s\n", b_info.bios_version);
371
372 efi_runtime_init();
373}
374
375static void __init check_kernel_sections_mem(void)
376{
377 phys_addr_t start = __pa_symbol(&_text);
378 phys_addr_t size = __pa_symbol(&_end) - start;
379
380 if (!memblock_is_region_memory(start, size)) {
381 pr_info("Kernel sections are not in the memory maps\n");
382 memblock_add(start, size);
383 }
384}
385
386/*
387 * arch_mem_init - initialize memory management subsystem
388 */
389static void __init arch_mem_init(char **cmdline_p)
390{
391 if (usermem)
392 pr_info("User-defined physical RAM map overwrite\n");
393
394 check_kernel_sections_mem();
395
396 /*
397 * In order to reduce the possibility of kernel panic when failed to
398 * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
399 * low memory as small as possible before swiotlb_init(), so make
400 * sparse_init() using top-down allocation.
401 */
402 memblock_set_bottom_up(false);
403 sparse_init();
404 memblock_set_bottom_up(true);
405
406 swiotlb_init(true, SWIOTLB_VERBOSE);
407
408 dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
409
410 /* Reserve for hibernation. */
411 register_nosave_region(PFN_DOWN(__pa_symbol(&__nosave_begin)),
412 PFN_UP(__pa_symbol(&__nosave_end)));
413
414 memblock_dump_all();
415
416 early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
417}
418
419static void __init resource_init(void)
420{
421 long i = 0;
422 size_t res_size;
423 struct resource *res;
424 struct memblock_region *region;
425
426 code_resource.start = __pa_symbol(&_text);
427 code_resource.end = __pa_symbol(&_etext) - 1;
428 data_resource.start = __pa_symbol(&_etext);
429 data_resource.end = __pa_symbol(&_edata) - 1;
430 bss_resource.start = __pa_symbol(&__bss_start);
431 bss_resource.end = __pa_symbol(&__bss_stop) - 1;
432
433 num_standard_resources = memblock.memory.cnt;
434 res_size = num_standard_resources * sizeof(*standard_resources);
435 standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
436
437 for_each_mem_region(region) {
438 res = &standard_resources[i++];
439 if (!memblock_is_nomap(region)) {
440 res->name = "System RAM";
441 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
442 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
443 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
444 } else {
445 res->name = "Reserved";
446 res->flags = IORESOURCE_MEM;
447 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
448 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
449 }
450
451 request_resource(&iomem_resource, res);
452
453 /*
454 * We don't know which RAM region contains kernel data,
455 * so we try it repeatedly and let the resource manager
456 * test it.
457 */
458 request_resource(res, &code_resource);
459 request_resource(res, &data_resource);
460 request_resource(res, &bss_resource);
461 }
462}
463
464static int __init add_legacy_isa_io(struct fwnode_handle *fwnode,
465 resource_size_t hw_start, resource_size_t size)
466{
467 int ret = 0;
468 unsigned long vaddr;
469 struct logic_pio_hwaddr *range;
470
471 range = kzalloc(sizeof(*range), GFP_ATOMIC);
472 if (!range)
473 return -ENOMEM;
474
475 range->fwnode = fwnode;
476 range->size = size = round_up(size, PAGE_SIZE);
477 range->hw_start = hw_start;
478 range->flags = LOGIC_PIO_CPU_MMIO;
479
480 ret = logic_pio_register_range(range);
481 if (ret) {
482 kfree(range);
483 return ret;
484 }
485
486 /* Legacy ISA must placed at the start of PCI_IOBASE */
487 if (range->io_start != 0) {
488 logic_pio_unregister_range(range);
489 kfree(range);
490 return -EINVAL;
491 }
492
493 vaddr = (unsigned long)(PCI_IOBASE + range->io_start);
494 vmap_page_range(vaddr, vaddr + size, hw_start, pgprot_device(PAGE_KERNEL));
495
496 return 0;
497}
498
499static __init int arch_reserve_pio_range(void)
500{
501 struct device_node *np;
502
503 for_each_node_by_name(np, "isa") {
504 struct of_range range;
505 struct of_range_parser parser;
506
507 pr_info("ISA Bridge: %pOF\n", np);
508
509 if (of_range_parser_init(&parser, np)) {
510 pr_info("Failed to parse resources.\n");
511 of_node_put(np);
512 break;
513 }
514
515 for_each_of_range(&parser, &range) {
516 switch (range.flags & IORESOURCE_TYPE_BITS) {
517 case IORESOURCE_IO:
518 pr_info(" IO 0x%016llx..0x%016llx -> 0x%016llx\n",
519 range.cpu_addr,
520 range.cpu_addr + range.size - 1,
521 range.bus_addr);
522 if (add_legacy_isa_io(&np->fwnode, range.cpu_addr, range.size))
523 pr_warn("Failed to reserve legacy IO in Logic PIO\n");
524 break;
525 case IORESOURCE_MEM:
526 pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx\n",
527 range.cpu_addr,
528 range.cpu_addr + range.size - 1,
529 range.bus_addr);
530 break;
531 }
532 }
533 }
534
535 return 0;
536}
537arch_initcall(arch_reserve_pio_range);
538
539static int __init reserve_memblock_reserved_regions(void)
540{
541 u64 i, j;
542
543 for (i = 0; i < num_standard_resources; ++i) {
544 struct resource *mem = &standard_resources[i];
545 phys_addr_t r_start, r_end, mem_size = resource_size(mem);
546
547 if (!memblock_is_region_reserved(mem->start, mem_size))
548 continue;
549
550 for_each_reserved_mem_range(j, &r_start, &r_end) {
551 resource_size_t start, end;
552
553 start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
554 end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
555
556 if (start > mem->end || end < mem->start)
557 continue;
558
559 reserve_region_with_split(mem, start, end, "Reserved");
560 }
561 }
562
563 return 0;
564}
565arch_initcall(reserve_memblock_reserved_regions);
566
567#ifdef CONFIG_SMP
568static void __init prefill_possible_map(void)
569{
570 int i, possible;
571
572 possible = num_processors + disabled_cpus;
573 if (possible > nr_cpu_ids)
574 possible = nr_cpu_ids;
575
576 pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
577 possible, max((possible - num_processors), 0));
578
579 for (i = 0; i < possible; i++)
580 set_cpu_possible(i, true);
581 for (; i < NR_CPUS; i++)
582 set_cpu_possible(i, false);
583
584 set_nr_cpu_ids(possible);
585}
586#endif
587
588void __init setup_arch(char **cmdline_p)
589{
590 cpu_probe();
591 unwind_init();
592
593 init_environ();
594 efi_init();
595 fdt_setup();
596 memblock_init();
597 pagetable_init();
598 bootcmdline_init(cmdline_p);
599 parse_early_param();
600 reserve_initrd_mem();
601
602 platform_init();
603 arch_mem_init(cmdline_p);
604
605 resource_init();
606#ifdef CONFIG_SMP
607 plat_smp_setup();
608 prefill_possible_map();
609#endif
610
611 paging_init();
612
613#ifdef CONFIG_KASAN
614 kasan_init();
615#endif
616}