xref: /openbmc/linux/arch/loongarch/kernel/setup.c (revision b61a40af)
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/time.h>
43 
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)
51 
52 struct screen_info screen_info __section(".data");
53 
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;
57 
58 EXPORT_SYMBOL(cpu_data);
59 
60 struct loongson_board_info b_info;
61 static const char dmi_empty_string[] = "        ";
62 
63 /*
64  * Setup information
65  *
66  * These are initialized so they are in the .data section
67  */
68 
69 static int num_standard_resources;
70 static struct resource *standard_resources;
71 
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", };
75 
76 const char *get_system_type(void)
77 {
78 	return "generic-loongson-machine";
79 }
80 
81 static const char *dmi_string_parse(const struct dmi_header *dm, u8 s)
82 {
83 	const u8 *bp = ((u8 *) dm) + dm->length;
84 
85 	if (s) {
86 		s--;
87 		while (s > 0 && *bp) {
88 			bp += strlen(bp) + 1;
89 			s--;
90 		}
91 
92 		if (*bp != 0) {
93 			size_t len = strlen(bp)+1;
94 			size_t cmp_len = len > 8 ? 8 : len;
95 
96 			if (!memcmp(bp, dmi_empty_string, cmp_len))
97 				return dmi_empty_string;
98 
99 			return bp;
100 		}
101 	}
102 
103 	return "";
104 }
105 
106 static void __init parse_cpu_table(const struct dmi_header *dm)
107 {
108 	long freq_temp = 0;
109 	char *dmi_data = (char *)dm;
110 
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;
114 
115 	loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
116 	loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_CORE_PACKAGE_OFFSET);
117 
118 	pr_info("CpuClock = %llu\n", cpu_clock_freq);
119 }
120 
121 static void __init parse_bios_table(const struct dmi_header *dm)
122 {
123 	char *dmi_data = (char *)dm;
124 
125 	b_info.bios_size = (*(dmi_data + SMBIOS_BIOSSIZE_OFFSET) + 1) << 6;
126 }
127 
128 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
129 {
130 	switch (dm->type) {
131 	case 0x0: /* Extern BIOS */
132 		parse_bios_table(dm);
133 		break;
134 	case 0x4: /* Calling interface */
135 		parse_cpu_table(dm);
136 		break;
137 	}
138 }
139 static void __init smbios_parse(void)
140 {
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);
147 }
148 
149 static int usermem __initdata;
150 
151 static int __init early_parse_mem(char *p)
152 {
153 	phys_addr_t start, size;
154 
155 	if (!p) {
156 		pr_err("mem parameter is empty, do nothing\n");
157 		return -EINVAL;
158 	}
159 
160 	/*
161 	 * If a user specifies memory size, we
162 	 * blow away any automatically generated
163 	 * size.
164 	 */
165 	if (usermem == 0) {
166 		usermem = 1;
167 		memblock_remove(memblock_start_of_DRAM(),
168 			memblock_end_of_DRAM() - memblock_start_of_DRAM());
169 	}
170 	start = 0;
171 	size = memparse(p, &p);
172 	if (*p == '@')
173 		start = memparse(p + 1, &p);
174 	else {
175 		pr_err("Invalid format!\n");
176 		return -EINVAL;
177 	}
178 
179 	if (!IS_ENABLED(CONFIG_NUMA))
180 		memblock_add(start, size);
181 	else
182 		memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
183 
184 	return 0;
185 }
186 early_param("mem", early_parse_mem);
187 
188 void __init platform_init(void)
189 {
190 #ifdef CONFIG_ACPI_TABLE_UPGRADE
191 	acpi_table_upgrade();
192 #endif
193 #ifdef CONFIG_ACPI
194 	acpi_gbl_use_default_register_widths = false;
195 	acpi_boot_table_init();
196 	acpi_boot_init();
197 #endif
198 
199 #ifdef CONFIG_NUMA
200 	init_numa_memory();
201 #endif
202 	dmi_setup();
203 	smbios_parse();
204 	pr_info("The BIOS Version: %s\n", b_info.bios_version);
205 
206 	efi_runtime_init();
207 }
208 
209 static void __init check_kernel_sections_mem(void)
210 {
211 	phys_addr_t start = __pa_symbol(&_text);
212 	phys_addr_t size = __pa_symbol(&_end) - start;
213 
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);
217 	}
218 }
219 
220 /*
221  * arch_mem_init - initialize memory management subsystem
222  */
223 static void __init arch_mem_init(char **cmdline_p)
224 {
225 	if (usermem)
226 		pr_info("User-defined physical RAM map overwrite\n");
227 
228 	check_kernel_sections_mem();
229 
230 	/*
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.
235 	 */
236 	memblock_set_bottom_up(false);
237 	sparse_init();
238 	memblock_set_bottom_up(true);
239 
240 	plat_swiotlb_setup();
241 
242 	dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
243 
244 	memblock_dump_all();
245 
246 	early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
247 }
248 
249 static void __init resource_init(void)
250 {
251 	long i = 0;
252 	size_t res_size;
253 	struct resource *res;
254 	struct memblock_region *region;
255 
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;
262 
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);
266 
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;
274 		} else {
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;
279 		}
280 
281 		request_resource(&iomem_resource, res);
282 
283 		/*
284 		 *  We don't know which RAM region contains kernel data,
285 		 *  so we try it repeatedly and let the resource manager
286 		 *  test it.
287 		 */
288 		request_resource(res, &code_resource);
289 		request_resource(res, &data_resource);
290 		request_resource(res, &bss_resource);
291 	}
292 }
293 
294 static int __init reserve_memblock_reserved_regions(void)
295 {
296 	u64 i, j;
297 
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);
301 
302 		if (!memblock_is_region_reserved(mem->start, mem_size))
303 			continue;
304 
305 		for_each_reserved_mem_range(j, &r_start, &r_end) {
306 			resource_size_t start, end;
307 
308 			start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
309 			end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
310 
311 			if (start > mem->end || end < mem->start)
312 				continue;
313 
314 			reserve_region_with_split(mem, start, end, "Reserved");
315 		}
316 	}
317 
318 	return 0;
319 }
320 arch_initcall(reserve_memblock_reserved_regions);
321 
322 #ifdef CONFIG_SMP
323 static void __init prefill_possible_map(void)
324 {
325 	int i, possible;
326 
327 	possible = num_processors + disabled_cpus;
328 	if (possible > nr_cpu_ids)
329 		possible = nr_cpu_ids;
330 
331 	pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
332 			possible, max((possible - num_processors), 0));
333 
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);
338 
339 	nr_cpu_ids = possible;
340 }
341 #endif
342 
343 void __init setup_arch(char **cmdline_p)
344 {
345 	cpu_probe();
346 	*cmdline_p = boot_command_line;
347 
348 	init_environ();
349 	efi_init();
350 	memblock_init();
351 	parse_early_param();
352 
353 	platform_init();
354 	pagetable_init();
355 	arch_mem_init(cmdline_p);
356 
357 	resource_init();
358 #ifdef CONFIG_SMP
359 	plat_smp_setup();
360 	prefill_possible_map();
361 #endif
362 
363 	paging_init();
364 }
365