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