xref: /openbmc/linux/arch/loongarch/kernel/setup.c (revision 8e7ae8ba)
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/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>
30 
31 #include <asm/addrspace.h>
32 #include <asm/bootinfo.h>
33 #include <asm/cache.h>
34 #include <asm/cpu.h>
35 #include <asm/dma.h>
36 #include <asm/efi.h>
37 #include <asm/loongson.h>
38 #include <asm/numa.h>
39 #include <asm/pgalloc.h>
40 #include <asm/sections.h>
41 #include <asm/setup.h>
42 #include <asm/smp.h>
43 #include <asm/time.h>
44 
45 #define SMBIOS_BIOSSIZE_OFFSET		0x09
46 #define SMBIOS_BIOSEXTERN_OFFSET	0x13
47 #define SMBIOS_FREQLOW_OFFSET		0x16
48 #define SMBIOS_FREQHIGH_OFFSET		0x17
49 #define SMBIOS_FREQLOW_MASK		0xFF
50 #define SMBIOS_CORE_PACKAGE_OFFSET	0x23
51 #define LOONGSON_EFI_ENABLE		(1 << 3)
52 
53 #ifdef CONFIG_VT
54 struct screen_info screen_info;
55 #endif
56 
57 unsigned long fw_arg0, fw_arg1;
58 DEFINE_PER_CPU(unsigned long, kernelsp);
59 struct cpuinfo_loongarch cpu_data[NR_CPUS] __read_mostly;
60 
61 EXPORT_SYMBOL(cpu_data);
62 
63 struct loongson_board_info b_info;
64 static const char dmi_empty_string[] = "        ";
65 
66 /*
67  * Setup information
68  *
69  * These are initialized so they are in the .data section
70  */
71 
72 static int num_standard_resources;
73 static struct resource *standard_resources;
74 
75 static struct resource code_resource = { .name = "Kernel code", };
76 static struct resource data_resource = { .name = "Kernel data", };
77 static struct resource bss_resource  = { .name = "Kernel bss", };
78 
79 const char *get_system_type(void)
80 {
81 	return "generic-loongson-machine";
82 }
83 
84 static const char *dmi_string_parse(const struct dmi_header *dm, u8 s)
85 {
86 	const u8 *bp = ((u8 *) dm) + dm->length;
87 
88 	if (s) {
89 		s--;
90 		while (s > 0 && *bp) {
91 			bp += strlen(bp) + 1;
92 			s--;
93 		}
94 
95 		if (*bp != 0) {
96 			size_t len = strlen(bp)+1;
97 			size_t cmp_len = len > 8 ? 8 : len;
98 
99 			if (!memcmp(bp, dmi_empty_string, cmp_len))
100 				return dmi_empty_string;
101 
102 			return bp;
103 		}
104 	}
105 
106 	return "";
107 }
108 
109 static void __init parse_cpu_table(const struct dmi_header *dm)
110 {
111 	long freq_temp = 0;
112 	char *dmi_data = (char *)dm;
113 
114 	freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
115 			((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
116 	cpu_clock_freq = freq_temp * 1000000;
117 
118 	loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
119 	loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_CORE_PACKAGE_OFFSET);
120 
121 	pr_info("CpuClock = %llu\n", cpu_clock_freq);
122 }
123 
124 static void __init parse_bios_table(const struct dmi_header *dm)
125 {
126 	int bios_extern;
127 	char *dmi_data = (char *)dm;
128 
129 	bios_extern = *(dmi_data + SMBIOS_BIOSEXTERN_OFFSET);
130 	b_info.bios_size = *(dmi_data + SMBIOS_BIOSSIZE_OFFSET);
131 
132 	if (bios_extern & LOONGSON_EFI_ENABLE)
133 		set_bit(EFI_BOOT, &efi.flags);
134 	else
135 		clear_bit(EFI_BOOT, &efi.flags);
136 }
137 
138 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
139 {
140 	switch (dm->type) {
141 	case 0x0: /* Extern BIOS */
142 		parse_bios_table(dm);
143 		break;
144 	case 0x4: /* Calling interface */
145 		parse_cpu_table(dm);
146 		break;
147 	}
148 }
149 static void __init smbios_parse(void)
150 {
151 	b_info.bios_vendor = (void *)dmi_get_system_info(DMI_BIOS_VENDOR);
152 	b_info.bios_version = (void *)dmi_get_system_info(DMI_BIOS_VERSION);
153 	b_info.bios_release_date = (void *)dmi_get_system_info(DMI_BIOS_DATE);
154 	b_info.board_vendor = (void *)dmi_get_system_info(DMI_BOARD_VENDOR);
155 	b_info.board_name = (void *)dmi_get_system_info(DMI_BOARD_NAME);
156 	dmi_walk(find_tokens, NULL);
157 }
158 
159 static int usermem __initdata;
160 
161 static int __init early_parse_mem(char *p)
162 {
163 	phys_addr_t start, size;
164 
165 	if (!p) {
166 		pr_err("mem parameter is empty, do nothing\n");
167 		return -EINVAL;
168 	}
169 
170 	/*
171 	 * If a user specifies memory size, we
172 	 * blow away any automatically generated
173 	 * size.
174 	 */
175 	if (usermem == 0) {
176 		usermem = 1;
177 		memblock_remove(memblock_start_of_DRAM(),
178 			memblock_end_of_DRAM() - memblock_start_of_DRAM());
179 	}
180 	start = 0;
181 	size = memparse(p, &p);
182 	if (*p == '@')
183 		start = memparse(p + 1, &p);
184 	else {
185 		pr_err("Invalid format!\n");
186 		return -EINVAL;
187 	}
188 
189 	if (!IS_ENABLED(CONFIG_NUMA))
190 		memblock_add(start, size);
191 	else
192 		memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
193 
194 	return 0;
195 }
196 early_param("mem", early_parse_mem);
197 
198 void __init platform_init(void)
199 {
200 	efi_init();
201 #ifdef CONFIG_ACPI_TABLE_UPGRADE
202 	acpi_table_upgrade();
203 #endif
204 #ifdef CONFIG_ACPI
205 	acpi_gbl_use_default_register_widths = false;
206 	acpi_boot_table_init();
207 	acpi_boot_init();
208 #endif
209 
210 #ifdef CONFIG_NUMA
211 	init_numa_memory();
212 #endif
213 	dmi_setup();
214 	smbios_parse();
215 	pr_info("The BIOS Version: %s\n", b_info.bios_version);
216 
217 	efi_runtime_init();
218 }
219 
220 static void __init check_kernel_sections_mem(void)
221 {
222 	phys_addr_t start = __pa_symbol(&_text);
223 	phys_addr_t size = __pa_symbol(&_end) - start;
224 
225 	if (!memblock_is_region_memory(start, size)) {
226 		pr_info("Kernel sections are not in the memory maps\n");
227 		memblock_add(start, size);
228 	}
229 }
230 
231 /*
232  * arch_mem_init - initialize memory management subsystem
233  */
234 static void __init arch_mem_init(char **cmdline_p)
235 {
236 	if (usermem)
237 		pr_info("User-defined physical RAM map overwrite\n");
238 
239 	check_kernel_sections_mem();
240 
241 	/*
242 	 * In order to reduce the possibility of kernel panic when failed to
243 	 * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
244 	 * low memory as small as possible before plat_swiotlb_setup(), so
245 	 * make sparse_init() using top-down allocation.
246 	 */
247 	memblock_set_bottom_up(false);
248 	sparse_init();
249 	memblock_set_bottom_up(true);
250 
251 	plat_swiotlb_setup();
252 
253 	dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
254 
255 	memblock_dump_all();
256 
257 	early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
258 }
259 
260 static void __init resource_init(void)
261 {
262 	long i = 0;
263 	size_t res_size;
264 	struct resource *res;
265 	struct memblock_region *region;
266 
267 	code_resource.start = __pa_symbol(&_text);
268 	code_resource.end = __pa_symbol(&_etext) - 1;
269 	data_resource.start = __pa_symbol(&_etext);
270 	data_resource.end = __pa_symbol(&_edata) - 1;
271 	bss_resource.start = __pa_symbol(&__bss_start);
272 	bss_resource.end = __pa_symbol(&__bss_stop) - 1;
273 
274 	num_standard_resources = memblock.memory.cnt;
275 	res_size = num_standard_resources * sizeof(*standard_resources);
276 	standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
277 
278 	for_each_mem_region(region) {
279 		res = &standard_resources[i++];
280 		if (!memblock_is_nomap(region)) {
281 			res->name  = "System RAM";
282 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
283 			res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
284 			res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
285 		} else {
286 			res->name  = "Reserved";
287 			res->flags = IORESOURCE_MEM;
288 			res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
289 			res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
290 		}
291 
292 		request_resource(&iomem_resource, res);
293 
294 		/*
295 		 *  We don't know which RAM region contains kernel data,
296 		 *  so we try it repeatedly and let the resource manager
297 		 *  test it.
298 		 */
299 		request_resource(res, &code_resource);
300 		request_resource(res, &data_resource);
301 		request_resource(res, &bss_resource);
302 	}
303 }
304 
305 static int __init reserve_memblock_reserved_regions(void)
306 {
307 	u64 i, j;
308 
309 	for (i = 0; i < num_standard_resources; ++i) {
310 		struct resource *mem = &standard_resources[i];
311 		phys_addr_t r_start, r_end, mem_size = resource_size(mem);
312 
313 		if (!memblock_is_region_reserved(mem->start, mem_size))
314 			continue;
315 
316 		for_each_reserved_mem_range(j, &r_start, &r_end) {
317 			resource_size_t start, end;
318 
319 			start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
320 			end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
321 
322 			if (start > mem->end || end < mem->start)
323 				continue;
324 
325 			reserve_region_with_split(mem, start, end, "Reserved");
326 		}
327 	}
328 
329 	return 0;
330 }
331 arch_initcall(reserve_memblock_reserved_regions);
332 
333 #ifdef CONFIG_SMP
334 static void __init prefill_possible_map(void)
335 {
336 	int i, possible;
337 
338 	possible = num_processors + disabled_cpus;
339 	if (possible > nr_cpu_ids)
340 		possible = nr_cpu_ids;
341 
342 	pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
343 			possible, max((possible - num_processors), 0));
344 
345 	for (i = 0; i < possible; i++)
346 		set_cpu_possible(i, true);
347 	for (; i < NR_CPUS; i++)
348 		set_cpu_possible(i, false);
349 
350 	nr_cpu_ids = possible;
351 }
352 #else
353 static inline void prefill_possible_map(void) {}
354 #endif
355 
356 void __init setup_arch(char **cmdline_p)
357 {
358 	cpu_probe();
359 	*cmdline_p = boot_command_line;
360 
361 	init_environ();
362 	memblock_init();
363 	parse_early_param();
364 
365 	platform_init();
366 	pagetable_init();
367 	arch_mem_init(cmdline_p);
368 
369 	resource_init();
370 	plat_smp_setup();
371 	prefill_possible_map();
372 
373 	paging_init();
374 }
375