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/root_dev.h>
23 #include <linux/console.h>
24 #include <linux/pfn.h>
25 #include <linux/platform_device.h>
26 #include <linux/sizes.h>
27 #include <linux/device.h>
28 #include <linux/dma-map-ops.h>
29 #include <linux/swiotlb.h>
31 #include <asm/addrspace.h>
32 #include <asm/bootinfo.h>
33 #include <asm/cache.h>
37 #include <asm/loongson.h>
39 #include <asm/pgalloc.h>
40 #include <asm/sections.h>
41 #include <asm/setup.h>
44 #define SMBIOS_BIOSSIZE_OFFSET 0x09
45 #define SMBIOS_BIOSEXTERN_OFFSET 0x13
46 #define SMBIOS_FREQLOW_OFFSET 0x16
47 #define SMBIOS_FREQHIGH_OFFSET 0x17
48 #define SMBIOS_FREQLOW_MASK 0xFF
49 #define SMBIOS_CORE_PACKAGE_OFFSET 0x23
50 #define LOONGSON_EFI_ENABLE (1 << 3)
52 struct screen_info screen_info __section(".data");
54 unsigned long fw_arg0, fw_arg1, fw_arg2;
55 DEFINE_PER_CPU(unsigned long, kernelsp);
56 struct cpuinfo_loongarch cpu_data[NR_CPUS] __read_mostly;
58 EXPORT_SYMBOL(cpu_data);
60 struct loongson_board_info b_info;
61 static const char dmi_empty_string[] = " ";
66 * These are initialized so they are in the .data section
69 static int num_standard_resources;
70 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", };
76 const char *get_system_type(void)
78 return "generic-loongson-machine";
81 static const char *dmi_string_parse(const struct dmi_header *dm, u8 s)
83 const u8 *bp = ((u8 *) dm) + dm->length;
87 while (s > 0 && *bp) {
93 size_t len = strlen(bp)+1;
94 size_t cmp_len = len > 8 ? 8 : len;
96 if (!memcmp(bp, dmi_empty_string, cmp_len))
97 return dmi_empty_string;
106 static void __init parse_cpu_table(const struct dmi_header *dm)
109 char *dmi_data = (char *)dm;
111 freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
112 ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
113 cpu_clock_freq = freq_temp * 1000000;
115 loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
116 loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_CORE_PACKAGE_OFFSET);
118 pr_info("CpuClock = %llu\n", cpu_clock_freq);
121 static void __init parse_bios_table(const struct dmi_header *dm)
123 char *dmi_data = (char *)dm;
125 b_info.bios_size = (*(dmi_data + SMBIOS_BIOSSIZE_OFFSET) + 1) << 6;
128 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
131 case 0x0: /* Extern BIOS */
132 parse_bios_table(dm);
134 case 0x4: /* Calling interface */
139 static void __init smbios_parse(void)
141 b_info.bios_vendor = (void *)dmi_get_system_info(DMI_BIOS_VENDOR);
142 b_info.bios_version = (void *)dmi_get_system_info(DMI_BIOS_VERSION);
143 b_info.bios_release_date = (void *)dmi_get_system_info(DMI_BIOS_DATE);
144 b_info.board_vendor = (void *)dmi_get_system_info(DMI_BOARD_VENDOR);
145 b_info.board_name = (void *)dmi_get_system_info(DMI_BOARD_NAME);
146 dmi_walk(find_tokens, NULL);
149 static int usermem __initdata;
151 static int __init early_parse_mem(char *p)
153 phys_addr_t start, size;
156 pr_err("mem parameter is empty, do nothing\n");
161 * If a user specifies memory size, we
162 * blow away any automatically generated
167 memblock_remove(memblock_start_of_DRAM(),
168 memblock_end_of_DRAM() - memblock_start_of_DRAM());
171 size = memparse(p, &p);
173 start = memparse(p + 1, &p);
175 pr_err("Invalid format!\n");
179 if (!IS_ENABLED(CONFIG_NUMA))
180 memblock_add(start, size);
182 memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
186 early_param("mem", early_parse_mem);
188 void __init platform_init(void)
190 #ifdef CONFIG_ACPI_TABLE_UPGRADE
191 acpi_table_upgrade();
194 acpi_gbl_use_default_register_widths = false;
195 acpi_boot_table_init();
204 pr_info("The BIOS Version: %s\n", b_info.bios_version);
209 static void __init check_kernel_sections_mem(void)
211 phys_addr_t start = __pa_symbol(&_text);
212 phys_addr_t size = __pa_symbol(&_end) - start;
214 if (!memblock_is_region_memory(start, size)) {
215 pr_info("Kernel sections are not in the memory maps\n");
216 memblock_add(start, size);
221 * arch_mem_init - initialize memory management subsystem
223 static void __init arch_mem_init(char **cmdline_p)
226 pr_info("User-defined physical RAM map overwrite\n");
228 check_kernel_sections_mem();
231 * In order to reduce the possibility of kernel panic when failed to
232 * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
233 * low memory as small as possible before plat_swiotlb_setup(), so
234 * make sparse_init() using top-down allocation.
236 memblock_set_bottom_up(false);
238 memblock_set_bottom_up(true);
240 swiotlb_init(true, SWIOTLB_VERBOSE);
242 dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
246 early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
249 static void __init resource_init(void)
253 struct resource *res;
254 struct memblock_region *region;
256 code_resource.start = __pa_symbol(&_text);
257 code_resource.end = __pa_symbol(&_etext) - 1;
258 data_resource.start = __pa_symbol(&_etext);
259 data_resource.end = __pa_symbol(&_edata) - 1;
260 bss_resource.start = __pa_symbol(&__bss_start);
261 bss_resource.end = __pa_symbol(&__bss_stop) - 1;
263 num_standard_resources = memblock.memory.cnt;
264 res_size = num_standard_resources * sizeof(*standard_resources);
265 standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
267 for_each_mem_region(region) {
268 res = &standard_resources[i++];
269 if (!memblock_is_nomap(region)) {
270 res->name = "System RAM";
271 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
272 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
273 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
275 res->name = "Reserved";
276 res->flags = IORESOURCE_MEM;
277 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
278 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
281 request_resource(&iomem_resource, res);
284 * We don't know which RAM region contains kernel data,
285 * so we try it repeatedly and let the resource manager
288 request_resource(res, &code_resource);
289 request_resource(res, &data_resource);
290 request_resource(res, &bss_resource);
294 static int __init reserve_memblock_reserved_regions(void)
298 for (i = 0; i < num_standard_resources; ++i) {
299 struct resource *mem = &standard_resources[i];
300 phys_addr_t r_start, r_end, mem_size = resource_size(mem);
302 if (!memblock_is_region_reserved(mem->start, mem_size))
305 for_each_reserved_mem_range(j, &r_start, &r_end) {
306 resource_size_t start, end;
308 start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
309 end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
311 if (start > mem->end || end < mem->start)
314 reserve_region_with_split(mem, start, end, "Reserved");
320 arch_initcall(reserve_memblock_reserved_regions);
323 static void __init prefill_possible_map(void)
327 possible = num_processors + disabled_cpus;
328 if (possible > nr_cpu_ids)
329 possible = nr_cpu_ids;
331 pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
332 possible, max((possible - num_processors), 0));
334 for (i = 0; i < possible; i++)
335 set_cpu_possible(i, true);
336 for (; i < NR_CPUS; i++)
337 set_cpu_possible(i, false);
339 nr_cpu_ids = possible;
343 void __init setup_arch(char **cmdline_p)
346 *cmdline_p = boot_command_line;
355 arch_mem_init(cmdline_p);
360 prefill_possible_map();