setup.c 15 KB

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