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)
53 struct screen_info screen_info;
56 unsigned long fw_arg0, fw_arg1;
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)
126 char *dmi_data = (char *)dm;
128 bios_extern = *(dmi_data + SMBIOS_BIOSEXTERN_OFFSET);
129 b_info.bios_size = (*(dmi_data + SMBIOS_BIOSSIZE_OFFSET) + 1) << 6;
131 if (bios_extern & LOONGSON_EFI_ENABLE)
132 set_bit(EFI_BOOT, &efi.flags);
134 clear_bit(EFI_BOOT, &efi.flags);
137 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
140 case 0x0: /* Extern BIOS */
141 parse_bios_table(dm);
143 case 0x4: /* Calling interface */
148 static void __init smbios_parse(void)
150 b_info.bios_vendor = (void *)dmi_get_system_info(DMI_BIOS_VENDOR);
151 b_info.bios_version = (void *)dmi_get_system_info(DMI_BIOS_VERSION);
152 b_info.bios_release_date = (void *)dmi_get_system_info(DMI_BIOS_DATE);
153 b_info.board_vendor = (void *)dmi_get_system_info(DMI_BOARD_VENDOR);
154 b_info.board_name = (void *)dmi_get_system_info(DMI_BOARD_NAME);
155 dmi_walk(find_tokens, NULL);
158 static int usermem __initdata;
160 static int __init early_parse_mem(char *p)
162 phys_addr_t start, size;
165 pr_err("mem parameter is empty, do nothing\n");
170 * If a user specifies memory size, we
171 * blow away any automatically generated
176 memblock_remove(memblock_start_of_DRAM(),
177 memblock_end_of_DRAM() - memblock_start_of_DRAM());
180 size = memparse(p, &p);
182 start = memparse(p + 1, &p);
184 pr_err("Invalid format!\n");
188 if (!IS_ENABLED(CONFIG_NUMA))
189 memblock_add(start, size);
191 memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
195 early_param("mem", early_parse_mem);
197 void __init platform_init(void)
200 #ifdef CONFIG_ACPI_TABLE_UPGRADE
201 acpi_table_upgrade();
204 acpi_gbl_use_default_register_widths = false;
205 acpi_boot_table_init();
214 pr_info("The BIOS Version: %s\n", b_info.bios_version);
219 static void __init check_kernel_sections_mem(void)
221 phys_addr_t start = __pa_symbol(&_text);
222 phys_addr_t size = __pa_symbol(&_end) - start;
224 if (!memblock_is_region_memory(start, size)) {
225 pr_info("Kernel sections are not in the memory maps\n");
226 memblock_add(start, size);
231 * arch_mem_init - initialize memory management subsystem
233 static void __init arch_mem_init(char **cmdline_p)
236 pr_info("User-defined physical RAM map overwrite\n");
238 check_kernel_sections_mem();
241 * In order to reduce the possibility of kernel panic when failed to
242 * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
243 * low memory as small as possible before plat_swiotlb_setup(), so
244 * make sparse_init() using top-down allocation.
246 memblock_set_bottom_up(false);
248 memblock_set_bottom_up(true);
250 swiotlb_init(true, SWIOTLB_VERBOSE);
252 dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
256 early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
259 static void __init resource_init(void)
263 struct resource *res;
264 struct memblock_region *region;
266 code_resource.start = __pa_symbol(&_text);
267 code_resource.end = __pa_symbol(&_etext) - 1;
268 data_resource.start = __pa_symbol(&_etext);
269 data_resource.end = __pa_symbol(&_edata) - 1;
270 bss_resource.start = __pa_symbol(&__bss_start);
271 bss_resource.end = __pa_symbol(&__bss_stop) - 1;
273 num_standard_resources = memblock.memory.cnt;
274 res_size = num_standard_resources * sizeof(*standard_resources);
275 standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
277 for_each_mem_region(region) {
278 res = &standard_resources[i++];
279 if (!memblock_is_nomap(region)) {
280 res->name = "System RAM";
281 res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
282 res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
283 res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
285 res->name = "Reserved";
286 res->flags = IORESOURCE_MEM;
287 res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
288 res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
291 request_resource(&iomem_resource, res);
294 * We don't know which RAM region contains kernel data,
295 * so we try it repeatedly and let the resource manager
298 request_resource(res, &code_resource);
299 request_resource(res, &data_resource);
300 request_resource(res, &bss_resource);
304 static int __init reserve_memblock_reserved_regions(void)
308 for (i = 0; i < num_standard_resources; ++i) {
309 struct resource *mem = &standard_resources[i];
310 phys_addr_t r_start, r_end, mem_size = resource_size(mem);
312 if (!memblock_is_region_reserved(mem->start, mem_size))
315 for_each_reserved_mem_range(j, &r_start, &r_end) {
316 resource_size_t start, end;
318 start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
319 end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
321 if (start > mem->end || end < mem->start)
324 reserve_region_with_split(mem, start, end, "Reserved");
330 arch_initcall(reserve_memblock_reserved_regions);
333 static void __init prefill_possible_map(void)
337 possible = num_processors + disabled_cpus;
338 if (possible > nr_cpu_ids)
339 possible = nr_cpu_ids;
341 pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
342 possible, max((possible - num_processors), 0));
344 for (i = 0; i < possible; i++)
345 set_cpu_possible(i, true);
346 for (; i < NR_CPUS; i++)
347 set_cpu_possible(i, false);
349 nr_cpu_ids = possible;
353 void __init setup_arch(char **cmdline_p)
356 *cmdline_p = boot_command_line;
364 arch_mem_init(cmdline_p);
369 prefill_possible_map();