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/swiotlb.h>
33 #include <asm/addrspace.h>
34 #include <asm/bootinfo.h>
35 #include <asm/cache.h>
39 #include <asm/loongson.h>
41 #include <asm/pgalloc.h>
42 #include <asm/sections.h>
43 #include <asm/setup.h>
46 #define SMBIOS_BIOSSIZE_OFFSET 0x09
47 #define SMBIOS_BIOSEXTERN_OFFSET 0x13
48 #define SMBIOS_FREQLOW_OFFSET 0x16
49 #define SMBIOS_FREQHIGH_OFFSET 0x17
50 #define SMBIOS_FREQLOW_MASK 0xFF
51 #define SMBIOS_CORE_PACKAGE_OFFSET 0x23
52 #define LOONGSON_EFI_ENABLE (1 << 3)
54 struct screen_info screen_info __section(".data");
56 unsigned long fw_arg0, fw_arg1, fw_arg2;
57 DEFINE_PER_CPU(unsigned long, kernelsp);
58 struct cpuinfo_loongarch cpu_data[NR_CPUS] __read_mostly;
60 EXPORT_SYMBOL(cpu_data);
62 struct loongson_board_info b_info;
63 static const char dmi_empty_string[] = " ";
68 * These are initialized so they are in the .data section
71 static int num_standard_resources;
72 static struct resource *standard_resources;
74 static struct resource code_resource = { .name = "Kernel code", };
75 static struct resource data_resource = { .name = "Kernel data", };
76 static struct resource bss_resource = { .name = "Kernel bss", };
78 const char *get_system_type(void)
80 return "generic-loongson-machine";
83 static const char *dmi_string_parse(const struct dmi_header *dm, u8 s)
85 const u8 *bp = ((u8 *) dm) + dm->length;
89 while (s > 0 && *bp) {
95 size_t len = strlen(bp)+1;
96 size_t cmp_len = len > 8 ? 8 : len;
98 if (!memcmp(bp, dmi_empty_string, cmp_len))
99 return dmi_empty_string;
108 static void __init parse_cpu_table(const struct dmi_header *dm)
111 char *dmi_data = (char *)dm;
113 freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
114 ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
115 cpu_clock_freq = freq_temp * 1000000;
117 loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
118 loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_CORE_PACKAGE_OFFSET);
120 pr_info("CpuClock = %llu\n", cpu_clock_freq);
123 static void __init parse_bios_table(const struct dmi_header *dm)
125 char *dmi_data = (char *)dm;
127 b_info.bios_size = (*(dmi_data + SMBIOS_BIOSSIZE_OFFSET) + 1) << 6;
130 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
133 case 0x0: /* Extern BIOS */
134 parse_bios_table(dm);
136 case 0x4: /* Calling interface */
141 static void __init smbios_parse(void)
143 b_info.bios_vendor = (void *)dmi_get_system_info(DMI_BIOS_VENDOR);
144 b_info.bios_version = (void *)dmi_get_system_info(DMI_BIOS_VERSION);
145 b_info.bios_release_date = (void *)dmi_get_system_info(DMI_BIOS_DATE);
146 b_info.board_vendor = (void *)dmi_get_system_info(DMI_BOARD_VENDOR);
147 b_info.board_name = (void *)dmi_get_system_info(DMI_BOARD_NAME);
148 dmi_walk(find_tokens, NULL);
151 static int usermem __initdata;
153 static int __init early_parse_mem(char *p)
155 phys_addr_t start, size;
158 pr_err("mem parameter is empty, do nothing\n");
163 * If a user specifies memory size, we
164 * blow away any automatically generated
169 memblock_remove(memblock_start_of_DRAM(),
170 memblock_end_of_DRAM() - memblock_start_of_DRAM());
173 size = memparse(p, &p);
175 start = memparse(p + 1, &p);
177 pr_err("Invalid format!\n");
181 if (!IS_ENABLED(CONFIG_NUMA))
182 memblock_add(start, size);
184 memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
188 early_param("mem", early_parse_mem);
190 static void __init arch_reserve_vmcore(void)
192 #ifdef CONFIG_PROC_VMCORE
194 phys_addr_t start, end;
196 if (!is_kdump_kernel())
199 if (!elfcorehdr_size) {
200 for_each_mem_range(i, &start, &end) {
201 if (elfcorehdr_addr >= start && elfcorehdr_addr < end) {
203 * Reserve from the elf core header to the end of
204 * the memory segment, that should all be kdump
207 elfcorehdr_size = end - elfcorehdr_addr;
213 if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) {
214 pr_warn("elfcorehdr is overlapped\n");
218 memblock_reserve(elfcorehdr_addr, elfcorehdr_size);
220 pr_info("Reserving %llu KiB of memory at 0x%llx for elfcorehdr\n",
221 elfcorehdr_size >> 10, elfcorehdr_addr);
225 static void __init arch_parse_crashkernel(void)
229 unsigned long long start;
230 unsigned long long total_mem;
231 unsigned long long crash_base, crash_size;
233 total_mem = memblock_phys_mem_size();
234 ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
235 if (ret < 0 || crash_size <= 0)
238 start = memblock_phys_alloc_range(crash_size, 1, crash_base, crash_base + crash_size);
239 if (start != crash_base) {
240 pr_warn("Invalid memory region reserved for crash kernel\n");
244 crashk_res.start = crash_base;
245 crashk_res.end = crash_base + crash_size - 1;
249 void __init platform_init(void)
251 arch_reserve_vmcore();
252 arch_parse_crashkernel();
254 #ifdef CONFIG_ACPI_TABLE_UPGRADE
255 acpi_table_upgrade();
258 acpi_gbl_use_default_register_widths = false;
259 acpi_boot_table_init();
268 pr_info("The BIOS Version: %s\n", b_info.bios_version);
273 static void __init check_kernel_sections_mem(void)
275 phys_addr_t start = __pa_symbol(&_text);
276 phys_addr_t size = __pa_symbol(&_end) - start;
278 if (!memblock_is_region_memory(start, size)) {
279 pr_info("Kernel sections are not in the memory maps\n");
280 memblock_add(start, size);
285 * arch_mem_init - initialize memory management subsystem
287 static void __init arch_mem_init(char **cmdline_p)
290 pr_info("User-defined physical RAM map overwrite\n");
292 check_kernel_sections_mem();
295 * In order to reduce the possibility of kernel panic when failed to
296 * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
297 * low memory as small as possible before plat_swiotlb_setup(), so
298 * make sparse_init() using top-down allocation.
300 memblock_set_bottom_up(false);
302 memblock_set_bottom_up(true);
304 swiotlb_init(true, SWIOTLB_VERBOSE);
306 dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
310 early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
313 static void __init resource_init(void)
317 struct resource *res;
318 struct memblock_region *region;
320 code_resource.start = __pa_symbol(&_text);
321 code_resource.end = __pa_symbol(&_etext) - 1;
322 data_resource.start = __pa_symbol(&_etext);
323 data_resource.end = __pa_symbol(&_edata) - 1;
324 bss_resource.start = __pa_symbol(&__bss_start);
325 bss_resource.end = __pa_symbol(&__bss_stop) - 1;
327 num_standard_resources = memblock.memory.cnt;
328 res_size = num_standard_resources * sizeof(*standard_resources);
329 standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
331 for_each_mem_region(region) {
332 res = &standard_resources[i++];
333 if (!memblock_is_nomap(region)) {
334 res->name = "System RAM";
335 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
336 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
337 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
339 res->name = "Reserved";
340 res->flags = IORESOURCE_MEM;
341 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
342 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
345 request_resource(&iomem_resource, res);
348 * We don't know which RAM region contains kernel data,
349 * so we try it repeatedly and let the resource manager
352 request_resource(res, &code_resource);
353 request_resource(res, &data_resource);
354 request_resource(res, &bss_resource);
358 if (crashk_res.start < crashk_res.end) {
359 insert_resource(&iomem_resource, &crashk_res);
360 pr_info("Reserving %ldMB of memory at %ldMB for crashkernel\n",
361 (unsigned long)((crashk_res.end - crashk_res.start + 1) >> 20),
362 (unsigned long)(crashk_res.start >> 20));
367 static int __init reserve_memblock_reserved_regions(void)
371 for (i = 0; i < num_standard_resources; ++i) {
372 struct resource *mem = &standard_resources[i];
373 phys_addr_t r_start, r_end, mem_size = resource_size(mem);
375 if (!memblock_is_region_reserved(mem->start, mem_size))
378 for_each_reserved_mem_range(j, &r_start, &r_end) {
379 resource_size_t start, end;
381 start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
382 end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
384 if (start > mem->end || end < mem->start)
387 reserve_region_with_split(mem, start, end, "Reserved");
393 arch_initcall(reserve_memblock_reserved_regions);
396 static void __init prefill_possible_map(void)
400 possible = num_processors + disabled_cpus;
401 if (possible > nr_cpu_ids)
402 possible = nr_cpu_ids;
404 pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
405 possible, max((possible - num_processors), 0));
407 for (i = 0; i < possible; i++)
408 set_cpu_possible(i, true);
409 for (; i < NR_CPUS; i++)
410 set_cpu_possible(i, false);
412 set_nr_cpu_ids(possible);
416 void __init setup_arch(char **cmdline_p)
419 *cmdline_p = boot_command_line;
426 reserve_initrd_mem();
429 arch_mem_init(cmdline_p);
434 prefill_possible_map();