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