setup.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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_default_register_widths = false;
  300. acpi_boot_table_init();
  301. #endif
  302. early_init_fdt_scan_reserved_mem();
  303. unflatten_and_copy_device_tree();
  304. #ifdef CONFIG_NUMA
  305. init_numa_memory();
  306. #endif
  307. dmi_setup();
  308. smbios_parse();
  309. pr_info("The BIOS Version: %s\n", b_info.bios_version);
  310. efi_runtime_init();
  311. }
  312. static void __init check_kernel_sections_mem(void)
  313. {
  314. phys_addr_t start = __pa_symbol(&_text);
  315. phys_addr_t size = __pa_symbol(&_end) - start;
  316. if (!memblock_is_region_memory(start, size)) {
  317. pr_info("Kernel sections are not in the memory maps\n");
  318. memblock_add(start, size);
  319. }
  320. }
  321. /*
  322. * arch_mem_init - initialize memory management subsystem
  323. */
  324. static void __init arch_mem_init(char **cmdline_p)
  325. {
  326. if (usermem)
  327. pr_info("User-defined physical RAM map overwrite\n");
  328. check_kernel_sections_mem();
  329. /*
  330. * In order to reduce the possibility of kernel panic when failed to
  331. * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
  332. * low memory as small as possible before swiotlb_init(), so make
  333. * sparse_init() using top-down allocation.
  334. */
  335. memblock_set_bottom_up(false);
  336. sparse_init();
  337. memblock_set_bottom_up(true);
  338. swiotlb_init(true, SWIOTLB_VERBOSE);
  339. dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
  340. /* Reserve for hibernation. */
  341. register_nosave_region(PFN_DOWN(__pa_symbol(&__nosave_begin)),
  342. PFN_UP(__pa_symbol(&__nosave_end)));
  343. memblock_dump_all();
  344. early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
  345. }
  346. static void __init resource_init(void)
  347. {
  348. long i = 0;
  349. size_t res_size;
  350. struct resource *res;
  351. struct memblock_region *region;
  352. code_resource.start = __pa_symbol(&_text);
  353. code_resource.end = __pa_symbol(&_etext) - 1;
  354. data_resource.start = __pa_symbol(&_etext);
  355. data_resource.end = __pa_symbol(&_edata) - 1;
  356. bss_resource.start = __pa_symbol(&__bss_start);
  357. bss_resource.end = __pa_symbol(&__bss_stop) - 1;
  358. num_standard_resources = memblock.memory.cnt;
  359. res_size = num_standard_resources * sizeof(*standard_resources);
  360. standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
  361. for_each_mem_region(region) {
  362. res = &standard_resources[i++];
  363. if (!memblock_is_nomap(region)) {
  364. res->name = "System RAM";
  365. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  366. res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
  367. res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
  368. } else {
  369. res->name = "Reserved";
  370. res->flags = IORESOURCE_MEM;
  371. res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
  372. res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
  373. }
  374. request_resource(&iomem_resource, res);
  375. /*
  376. * We don't know which RAM region contains kernel data,
  377. * so we try it repeatedly and let the resource manager
  378. * test it.
  379. */
  380. request_resource(res, &code_resource);
  381. request_resource(res, &data_resource);
  382. request_resource(res, &bss_resource);
  383. }
  384. }
  385. static int __init add_legacy_isa_io(struct fwnode_handle *fwnode,
  386. resource_size_t hw_start, resource_size_t size)
  387. {
  388. int ret = 0;
  389. unsigned long vaddr;
  390. struct logic_pio_hwaddr *range;
  391. range = kzalloc(sizeof(*range), GFP_ATOMIC);
  392. if (!range)
  393. return -ENOMEM;
  394. range->fwnode = fwnode;
  395. range->size = size = round_up(size, PAGE_SIZE);
  396. range->hw_start = hw_start;
  397. range->flags = LOGIC_PIO_CPU_MMIO;
  398. ret = logic_pio_register_range(range);
  399. if (ret) {
  400. kfree(range);
  401. return ret;
  402. }
  403. /* Legacy ISA must placed at the start of PCI_IOBASE */
  404. if (range->io_start != 0) {
  405. logic_pio_unregister_range(range);
  406. kfree(range);
  407. return -EINVAL;
  408. }
  409. vaddr = (unsigned long)(PCI_IOBASE + range->io_start);
  410. vmap_page_range(vaddr, vaddr + size, hw_start, pgprot_device(PAGE_KERNEL));
  411. return 0;
  412. }
  413. static __init int arch_reserve_pio_range(void)
  414. {
  415. struct device_node *np;
  416. for_each_node_by_name(np, "isa") {
  417. struct of_range range;
  418. struct of_range_parser parser;
  419. pr_info("ISA Bridge: %pOF\n", np);
  420. if (of_range_parser_init(&parser, np)) {
  421. pr_info("Failed to parse resources.\n");
  422. of_node_put(np);
  423. break;
  424. }
  425. for_each_of_range(&parser, &range) {
  426. switch (range.flags & IORESOURCE_TYPE_BITS) {
  427. case IORESOURCE_IO:
  428. pr_info(" IO 0x%016llx..0x%016llx -> 0x%016llx\n",
  429. range.cpu_addr,
  430. range.cpu_addr + range.size - 1,
  431. range.bus_addr);
  432. if (add_legacy_isa_io(&np->fwnode, range.cpu_addr, range.size))
  433. pr_warn("Failed to reserve legacy IO in Logic PIO\n");
  434. break;
  435. case IORESOURCE_MEM:
  436. pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx\n",
  437. range.cpu_addr,
  438. range.cpu_addr + range.size - 1,
  439. range.bus_addr);
  440. break;
  441. }
  442. }
  443. }
  444. return 0;
  445. }
  446. arch_initcall(arch_reserve_pio_range);
  447. static int __init reserve_memblock_reserved_regions(void)
  448. {
  449. u64 i, j;
  450. for (i = 0; i < num_standard_resources; ++i) {
  451. struct resource *mem = &standard_resources[i];
  452. phys_addr_t r_start, r_end, mem_size = resource_size(mem);
  453. if (!memblock_is_region_reserved(mem->start, mem_size))
  454. continue;
  455. for_each_reserved_mem_range(j, &r_start, &r_end) {
  456. resource_size_t start, end;
  457. start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
  458. end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
  459. if (start > mem->end || end < mem->start)
  460. continue;
  461. reserve_region_with_split(mem, start, end, "Reserved");
  462. }
  463. }
  464. return 0;
  465. }
  466. arch_initcall(reserve_memblock_reserved_regions);
  467. #ifdef CONFIG_SMP
  468. static void __init prefill_possible_map(void)
  469. {
  470. int i, possible;
  471. possible = num_processors + disabled_cpus;
  472. if (possible > nr_cpu_ids)
  473. possible = nr_cpu_ids;
  474. pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
  475. possible, max((possible - num_processors), 0));
  476. for (i = 0; i < possible; i++)
  477. set_cpu_possible(i, true);
  478. for (; i < NR_CPUS; i++) {
  479. set_cpu_present(i, false);
  480. set_cpu_possible(i, false);
  481. }
  482. set_nr_cpu_ids(possible);
  483. }
  484. #endif
  485. void __init setup_arch(char **cmdline_p)
  486. {
  487. cpu_probe();
  488. unwind_init();
  489. init_environ();
  490. efi_init();
  491. fdt_setup();
  492. memblock_init();
  493. pagetable_init();
  494. bootcmdline_init(cmdline_p);
  495. parse_early_param();
  496. reserve_initrd_mem();
  497. platform_init();
  498. arch_mem_init(cmdline_p);
  499. resource_init();
  500. jump_label_init(); /* Initialise the static keys for paravirtualization */
  501. #ifdef CONFIG_SMP
  502. plat_smp_setup();
  503. prefill_possible_map();
  504. #endif
  505. paging_init();
  506. #ifdef CONFIG_KASAN
  507. kasan_init();
  508. #endif
  509. }