1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
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
13 #include <linux/init.h>
14 #include <linux/acpi.h>
15 #include <linux/dmi.h>
16 #include <linux/efi.h>
17 #include <linux/export.h>
18 #include <linux/screen_info.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>
37 #include <asm/addrspace.h>
38 #include <asm/alternative.h>
39 #include <asm/bootinfo.h>
41 #include <asm/cache.h>
45 #include <asm/loongson.h>
47 #include <asm/pgalloc.h>
48 #include <asm/sections.h>
49 #include <asm/setup.h>
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)
60 struct screen_info screen_info __section(".data");
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;
66 EXPORT_SYMBOL(cpu_data);
68 struct loongson_board_info b_info;
69 static const char dmi_empty_string[] = " ";
74 * These are initialized so they are in the .data section
76 char init_command_line[COMMAND_LINE_SIZE] __initdata;
78 static int num_standard_resources;
79 static struct resource *standard_resources;
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", };
85 const char *get_system_type(void)
87 return "generic-loongson-machine";
90 void __init check_bugs(void)
92 alternative_instructions();
95 static const char *dmi_string_parse(const struct dmi_header *dm, u8 s)
97 const u8 *bp = ((u8 *) dm) + dm->length;
101 while (s > 0 && *bp) {
102 bp += strlen(bp) + 1;
107 size_t len = strlen(bp)+1;
108 size_t cmp_len = len > 8 ? 8 : len;
110 if (!memcmp(bp, dmi_empty_string, cmp_len))
111 return dmi_empty_string;
120 static void __init parse_cpu_table(const struct dmi_header *dm)
123 char *dmi_data = (char *)dm;
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;
129 loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
130 loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_CORE_PACKAGE_OFFSET);
132 pr_info("CpuClock = %llu\n", cpu_clock_freq);
135 static void __init parse_bios_table(const struct dmi_header *dm)
137 char *dmi_data = (char *)dm;
139 b_info.bios_size = (*(dmi_data + SMBIOS_BIOSSIZE_OFFSET) + 1) << 6;
142 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
145 case 0x0: /* Extern BIOS */
146 parse_bios_table(dm);
148 case 0x4: /* Calling interface */
153 static void __init smbios_parse(void)
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);
163 static int usermem __initdata;
165 static int __init early_parse_mem(char *p)
167 phys_addr_t start, size;
170 pr_err("mem parameter is empty, do nothing\n");
175 * If a user specifies memory size, we
176 * blow away any automatically generated
181 memblock_remove(memblock_start_of_DRAM(),
182 memblock_end_of_DRAM() - memblock_start_of_DRAM());
185 size = memparse(p, &p);
187 start = memparse(p + 1, &p);
189 pr_err("Invalid format!\n");
193 if (!IS_ENABLED(CONFIG_NUMA))
194 memblock_add(start, size);
196 memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
200 early_param("mem", early_parse_mem);
202 static void __init arch_reserve_vmcore(void)
204 #ifdef CONFIG_PROC_VMCORE
206 phys_addr_t start, end;
208 if (!is_kdump_kernel())
211 if (!elfcorehdr_size) {
212 for_each_mem_range(i, &start, &end) {
213 if (elfcorehdr_addr >= start && elfcorehdr_addr < end) {
215 * Reserve from the elf core header to the end of
216 * the memory segment, that should all be kdump
219 elfcorehdr_size = end - elfcorehdr_addr;
225 if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) {
226 pr_warn("elfcorehdr is overlapped\n");
230 memblock_reserve(elfcorehdr_addr, elfcorehdr_size);
232 pr_info("Reserving %llu KiB of memory at 0x%llx for elfcorehdr\n",
233 elfcorehdr_size >> 10, elfcorehdr_addr);
237 static void __init arch_parse_crashkernel(void)
241 unsigned long long start;
242 unsigned long long total_mem;
243 unsigned long long crash_base, crash_size;
245 total_mem = memblock_phys_mem_size();
246 ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
247 if (ret < 0 || crash_size <= 0)
250 start = memblock_phys_alloc_range(crash_size, 1, crash_base, crash_base + crash_size);
251 if (start != crash_base) {
252 pr_warn("Invalid memory region reserved for crash kernel\n");
256 crashk_res.start = crash_base;
257 crashk_res.end = crash_base + crash_size - 1;
261 static void __init fdt_setup(void)
263 #ifdef CONFIG_OF_EARLY_FLATTREE
266 /* ACPI-based systems do not require parsing fdt */
267 if (acpi_os_get_root_pointer())
270 /* Look for a device tree configuration table entry */
271 fdt_pointer = efi_fdt_pointer();
272 if (!fdt_pointer || fdt_check_header(fdt_pointer))
275 early_init_dt_scan(fdt_pointer);
276 early_init_fdt_reserve_self();
278 max_low_pfn = PFN_PHYS(memblock_end_of_DRAM());
282 static void __init bootcmdline_init(char **cmdline_p)
285 * If CONFIG_CMDLINE_FORCE is enabled then initializing the command line
286 * is trivial - we simply use the built-in command line unconditionally &
289 if (IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
290 strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
294 #ifdef CONFIG_OF_FLATTREE
296 * If CONFIG_CMDLINE_BOOTLOADER is enabled and we are in FDT-based system,
297 * the boot_command_line will be overwritten by early_init_dt_scan_chosen().
298 * So we need to append init_command_line (the original copy of boot_command_line)
299 * to boot_command_line.
301 if (initial_boot_params) {
302 if (boot_command_line[0])
303 strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
305 strlcat(boot_command_line, init_command_line, COMMAND_LINE_SIZE);
310 *cmdline_p = boot_command_line;
313 void __init platform_init(void)
315 arch_reserve_vmcore();
316 arch_parse_crashkernel();
318 #ifdef CONFIG_ACPI_TABLE_UPGRADE
319 acpi_table_upgrade();
322 acpi_gbl_use_default_register_widths = false;
323 acpi_boot_table_init();
325 unflatten_and_copy_device_tree();
332 pr_info("The BIOS Version: %s\n", b_info.bios_version);
337 static void __init check_kernel_sections_mem(void)
339 phys_addr_t start = __pa_symbol(&_text);
340 phys_addr_t size = __pa_symbol(&_end) - start;
342 if (!memblock_is_region_memory(start, size)) {
343 pr_info("Kernel sections are not in the memory maps\n");
344 memblock_add(start, size);
349 * arch_mem_init - initialize memory management subsystem
351 static void __init arch_mem_init(char **cmdline_p)
354 pr_info("User-defined physical RAM map overwrite\n");
356 check_kernel_sections_mem();
358 early_init_fdt_scan_reserved_mem();
361 * In order to reduce the possibility of kernel panic when failed to
362 * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
363 * low memory as small as possible before plat_swiotlb_setup(), so
364 * make sparse_init() using top-down allocation.
366 memblock_set_bottom_up(false);
368 memblock_set_bottom_up(true);
370 swiotlb_init(true, SWIOTLB_VERBOSE);
372 dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
374 /* Reserve for hibernation. */
375 register_nosave_region(PFN_DOWN(__pa_symbol(&__nosave_begin)),
376 PFN_UP(__pa_symbol(&__nosave_end)));
380 early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
383 static void __init resource_init(void)
387 struct resource *res;
388 struct memblock_region *region;
390 code_resource.start = __pa_symbol(&_text);
391 code_resource.end = __pa_symbol(&_etext) - 1;
392 data_resource.start = __pa_symbol(&_etext);
393 data_resource.end = __pa_symbol(&_edata) - 1;
394 bss_resource.start = __pa_symbol(&__bss_start);
395 bss_resource.end = __pa_symbol(&__bss_stop) - 1;
397 num_standard_resources = memblock.memory.cnt;
398 res_size = num_standard_resources * sizeof(*standard_resources);
399 standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
401 for_each_mem_region(region) {
402 res = &standard_resources[i++];
403 if (!memblock_is_nomap(region)) {
404 res->name = "System RAM";
405 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
406 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
407 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
409 res->name = "Reserved";
410 res->flags = IORESOURCE_MEM;
411 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
412 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
415 request_resource(&iomem_resource, res);
418 * We don't know which RAM region contains kernel data,
419 * so we try it repeatedly and let the resource manager
422 request_resource(res, &code_resource);
423 request_resource(res, &data_resource);
424 request_resource(res, &bss_resource);
428 if (crashk_res.start < crashk_res.end) {
429 insert_resource(&iomem_resource, &crashk_res);
430 pr_info("Reserving %ldMB of memory at %ldMB for crashkernel\n",
431 (unsigned long)((crashk_res.end - crashk_res.start + 1) >> 20),
432 (unsigned long)(crashk_res.start >> 20));
437 static int __init add_legacy_isa_io(struct fwnode_handle *fwnode,
438 resource_size_t hw_start, resource_size_t size)
442 struct logic_pio_hwaddr *range;
444 range = kzalloc(sizeof(*range), GFP_ATOMIC);
448 range->fwnode = fwnode;
449 range->size = size = round_up(size, PAGE_SIZE);
450 range->hw_start = hw_start;
451 range->flags = LOGIC_PIO_CPU_MMIO;
453 ret = logic_pio_register_range(range);
459 /* Legacy ISA must placed at the start of PCI_IOBASE */
460 if (range->io_start != 0) {
461 logic_pio_unregister_range(range);
466 vaddr = (unsigned long)(PCI_IOBASE + range->io_start);
467 ioremap_page_range(vaddr, vaddr + size, hw_start, pgprot_device(PAGE_KERNEL));
472 static __init int arch_reserve_pio_range(void)
474 struct device_node *np;
476 for_each_node_by_name(np, "isa") {
477 struct of_range range;
478 struct of_range_parser parser;
480 pr_info("ISA Bridge: %pOF\n", np);
482 if (of_range_parser_init(&parser, np)) {
483 pr_info("Failed to parse resources.\n");
488 for_each_of_range(&parser, &range) {
489 switch (range.flags & IORESOURCE_TYPE_BITS) {
491 pr_info(" IO 0x%016llx..0x%016llx -> 0x%016llx\n",
493 range.cpu_addr + range.size - 1,
495 if (add_legacy_isa_io(&np->fwnode, range.cpu_addr, range.size))
496 pr_warn("Failed to reserve legacy IO in Logic PIO\n");
499 pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx\n",
501 range.cpu_addr + range.size - 1,
510 arch_initcall(arch_reserve_pio_range);
512 static int __init reserve_memblock_reserved_regions(void)
516 for (i = 0; i < num_standard_resources; ++i) {
517 struct resource *mem = &standard_resources[i];
518 phys_addr_t r_start, r_end, mem_size = resource_size(mem);
520 if (!memblock_is_region_reserved(mem->start, mem_size))
523 for_each_reserved_mem_range(j, &r_start, &r_end) {
524 resource_size_t start, end;
526 start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
527 end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
529 if (start > mem->end || end < mem->start)
532 reserve_region_with_split(mem, start, end, "Reserved");
538 arch_initcall(reserve_memblock_reserved_regions);
541 static void __init prefill_possible_map(void)
545 possible = num_processors + disabled_cpus;
546 if (possible > nr_cpu_ids)
547 possible = nr_cpu_ids;
549 pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
550 possible, max((possible - num_processors), 0));
552 for (i = 0; i < possible; i++)
553 set_cpu_possible(i, true);
554 for (; i < NR_CPUS; i++)
555 set_cpu_possible(i, false);
557 set_nr_cpu_ids(possible);
561 void __init setup_arch(char **cmdline_p)
570 bootcmdline_init(cmdline_p);
572 reserve_initrd_mem();
575 arch_mem_init(cmdline_p);
580 prefill_possible_map();