xref: /openbmc/linux/arch/loongarch/kernel/setup.c (revision 4e174665)
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/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/libfdt.h>
32 #include <linux/of_fdt.h>
33 #include <linux/of_address.h>
34 #include <linux/suspend.h>
35 #include <linux/swiotlb.h>
36 
37 #include <asm/addrspace.h>
38 #include <asm/alternative.h>
39 #include <asm/bootinfo.h>
40 #include <asm/bugs.h>
41 #include <asm/cache.h>
42 #include <asm/cpu.h>
43 #include <asm/dma.h>
44 #include <asm/efi.h>
45 #include <asm/loongson.h>
46 #include <asm/numa.h>
47 #include <asm/pgalloc.h>
48 #include <asm/sections.h>
49 #include <asm/setup.h>
50 #include <asm/time.h>
51 
52 #define SMBIOS_BIOSSIZE_OFFSET		0x09
53 #define SMBIOS_BIOSEXTERN_OFFSET	0x13
54 #define SMBIOS_FREQLOW_OFFSET		0x16
55 #define SMBIOS_FREQHIGH_OFFSET		0x17
56 #define SMBIOS_FREQLOW_MASK		0xFF
57 #define SMBIOS_CORE_PACKAGE_OFFSET	0x23
58 #define LOONGSON_EFI_ENABLE		(1 << 3)
59 
60 struct screen_info screen_info __section(".data");
61 
62 unsigned long fw_arg0, fw_arg1, fw_arg2;
63 DEFINE_PER_CPU(unsigned long, kernelsp);
64 struct cpuinfo_loongarch cpu_data[NR_CPUS] __read_mostly;
65 
66 EXPORT_SYMBOL(cpu_data);
67 
68 struct loongson_board_info b_info;
69 static const char dmi_empty_string[] = "        ";
70 
71 /*
72  * Setup information
73  *
74  * These are initialized so they are in the .data section
75  */
76 char init_command_line[COMMAND_LINE_SIZE] __initdata;
77 
78 static int num_standard_resources;
79 static struct resource *standard_resources;
80 
81 static struct resource code_resource = { .name = "Kernel code", };
82 static struct resource data_resource = { .name = "Kernel data", };
83 static struct resource bss_resource  = { .name = "Kernel bss", };
84 
85 const char *get_system_type(void)
86 {
87 	return "generic-loongson-machine";
88 }
89 
90 void __init check_bugs(void)
91 {
92 	alternative_instructions();
93 }
94 
95 static const char *dmi_string_parse(const struct dmi_header *dm, u8 s)
96 {
97 	const u8 *bp = ((u8 *) dm) + dm->length;
98 
99 	if (s) {
100 		s--;
101 		while (s > 0 && *bp) {
102 			bp += strlen(bp) + 1;
103 			s--;
104 		}
105 
106 		if (*bp != 0) {
107 			size_t len = strlen(bp)+1;
108 			size_t cmp_len = len > 8 ? 8 : len;
109 
110 			if (!memcmp(bp, dmi_empty_string, cmp_len))
111 				return dmi_empty_string;
112 
113 			return bp;
114 		}
115 	}
116 
117 	return "";
118 }
119 
120 static void __init parse_cpu_table(const struct dmi_header *dm)
121 {
122 	long freq_temp = 0;
123 	char *dmi_data = (char *)dm;
124 
125 	freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
126 			((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
127 	cpu_clock_freq = freq_temp * 1000000;
128 
129 	loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
130 	loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_CORE_PACKAGE_OFFSET);
131 
132 	pr_info("CpuClock = %llu\n", cpu_clock_freq);
133 }
134 
135 static void __init parse_bios_table(const struct dmi_header *dm)
136 {
137 	char *dmi_data = (char *)dm;
138 
139 	b_info.bios_size = (*(dmi_data + SMBIOS_BIOSSIZE_OFFSET) + 1) << 6;
140 }
141 
142 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
143 {
144 	switch (dm->type) {
145 	case 0x0: /* Extern BIOS */
146 		parse_bios_table(dm);
147 		break;
148 	case 0x4: /* Calling interface */
149 		parse_cpu_table(dm);
150 		break;
151 	}
152 }
153 static void __init smbios_parse(void)
154 {
155 	b_info.bios_vendor = (void *)dmi_get_system_info(DMI_BIOS_VENDOR);
156 	b_info.bios_version = (void *)dmi_get_system_info(DMI_BIOS_VERSION);
157 	b_info.bios_release_date = (void *)dmi_get_system_info(DMI_BIOS_DATE);
158 	b_info.board_vendor = (void *)dmi_get_system_info(DMI_BOARD_VENDOR);
159 	b_info.board_name = (void *)dmi_get_system_info(DMI_BOARD_NAME);
160 	dmi_walk(find_tokens, NULL);
161 }
162 
163 static int usermem __initdata;
164 
165 static int __init early_parse_mem(char *p)
166 {
167 	phys_addr_t start, size;
168 
169 	if (!p) {
170 		pr_err("mem parameter is empty, do nothing\n");
171 		return -EINVAL;
172 	}
173 
174 	/*
175 	 * If a user specifies memory size, we
176 	 * blow away any automatically generated
177 	 * size.
178 	 */
179 	if (usermem == 0) {
180 		usermem = 1;
181 		memblock_remove(memblock_start_of_DRAM(),
182 			memblock_end_of_DRAM() - memblock_start_of_DRAM());
183 	}
184 	start = 0;
185 	size = memparse(p, &p);
186 	if (*p == '@')
187 		start = memparse(p + 1, &p);
188 	else {
189 		pr_err("Invalid format!\n");
190 		return -EINVAL;
191 	}
192 
193 	if (!IS_ENABLED(CONFIG_NUMA))
194 		memblock_add(start, size);
195 	else
196 		memblock_add_node(start, size, pa_to_nid(start), MEMBLOCK_NONE);
197 
198 	return 0;
199 }
200 early_param("mem", early_parse_mem);
201 
202 static void __init arch_reserve_vmcore(void)
203 {
204 #ifdef CONFIG_PROC_VMCORE
205 	u64 i;
206 	phys_addr_t start, end;
207 
208 	if (!is_kdump_kernel())
209 		return;
210 
211 	if (!elfcorehdr_size) {
212 		for_each_mem_range(i, &start, &end) {
213 			if (elfcorehdr_addr >= start && elfcorehdr_addr < end) {
214 				/*
215 				 * Reserve from the elf core header to the end of
216 				 * the memory segment, that should all be kdump
217 				 * reserved memory.
218 				 */
219 				elfcorehdr_size = end - elfcorehdr_addr;
220 				break;
221 			}
222 		}
223 	}
224 
225 	if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) {
226 		pr_warn("elfcorehdr is overlapped\n");
227 		return;
228 	}
229 
230 	memblock_reserve(elfcorehdr_addr, elfcorehdr_size);
231 
232 	pr_info("Reserving %llu KiB of memory at 0x%llx for elfcorehdr\n",
233 		elfcorehdr_size >> 10, elfcorehdr_addr);
234 #endif
235 }
236 
237 /* 2MB alignment for crash kernel regions */
238 #define CRASH_ALIGN	SZ_2M
239 #define CRASH_ADDR_MAX	SZ_4G
240 
241 static void __init arch_parse_crashkernel(void)
242 {
243 #ifdef CONFIG_KEXEC
244 	int ret;
245 	unsigned long long total_mem;
246 	unsigned long long crash_base, crash_size;
247 
248 	total_mem = memblock_phys_mem_size();
249 	ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
250 	if (ret < 0 || crash_size <= 0)
251 		return;
252 
253 	if (crash_base <= 0) {
254 		crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN, CRASH_ALIGN, CRASH_ADDR_MAX);
255 		if (!crash_base) {
256 			pr_warn("crashkernel reservation failed - No suitable area found.\n");
257 			return;
258 		}
259 	} else if (!memblock_phys_alloc_range(crash_size, CRASH_ALIGN, crash_base, crash_base + crash_size)) {
260 		pr_warn("Invalid memory region reserved for crash kernel\n");
261 		return;
262 	}
263 
264 	crashk_res.start = crash_base;
265 	crashk_res.end	 = crash_base + crash_size - 1;
266 #endif
267 }
268 
269 static void __init fdt_setup(void)
270 {
271 #ifdef CONFIG_OF_EARLY_FLATTREE
272 	void *fdt_pointer;
273 
274 	/* ACPI-based systems do not require parsing fdt */
275 	if (acpi_os_get_root_pointer())
276 		return;
277 
278 	/* Look for a device tree configuration table entry */
279 	fdt_pointer = efi_fdt_pointer();
280 	if (!fdt_pointer || fdt_check_header(fdt_pointer))
281 		return;
282 
283 	early_init_dt_scan(fdt_pointer);
284 	early_init_fdt_reserve_self();
285 
286 	max_low_pfn = PFN_PHYS(memblock_end_of_DRAM());
287 #endif
288 }
289 
290 static void __init bootcmdline_init(char **cmdline_p)
291 {
292 	/*
293 	 * If CONFIG_CMDLINE_FORCE is enabled then initializing the command line
294 	 * is trivial - we simply use the built-in command line unconditionally &
295 	 * unmodified.
296 	 */
297 	if (IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
298 		strscpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
299 		goto out;
300 	}
301 
302 #ifdef CONFIG_OF_FLATTREE
303 	/*
304 	 * If CONFIG_CMDLINE_BOOTLOADER is enabled and we are in FDT-based system,
305 	 * the boot_command_line will be overwritten by early_init_dt_scan_chosen().
306 	 * So we need to append init_command_line (the original copy of boot_command_line)
307 	 * to boot_command_line.
308 	 */
309 	if (initial_boot_params) {
310 		if (boot_command_line[0])
311 			strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
312 
313 		strlcat(boot_command_line, init_command_line, COMMAND_LINE_SIZE);
314 	}
315 #endif
316 
317 out:
318 	*cmdline_p = boot_command_line;
319 }
320 
321 void __init platform_init(void)
322 {
323 	arch_reserve_vmcore();
324 	arch_parse_crashkernel();
325 
326 #ifdef CONFIG_ACPI_TABLE_UPGRADE
327 	acpi_table_upgrade();
328 #endif
329 #ifdef CONFIG_ACPI
330 	acpi_gbl_use_default_register_widths = false;
331 	acpi_boot_table_init();
332 #endif
333 	unflatten_and_copy_device_tree();
334 
335 #ifdef CONFIG_NUMA
336 	init_numa_memory();
337 #endif
338 	dmi_setup();
339 	smbios_parse();
340 	pr_info("The BIOS Version: %s\n", b_info.bios_version);
341 
342 	efi_runtime_init();
343 }
344 
345 static void __init check_kernel_sections_mem(void)
346 {
347 	phys_addr_t start = __pa_symbol(&_text);
348 	phys_addr_t size = __pa_symbol(&_end) - start;
349 
350 	if (!memblock_is_region_memory(start, size)) {
351 		pr_info("Kernel sections are not in the memory maps\n");
352 		memblock_add(start, size);
353 	}
354 }
355 
356 /*
357  * arch_mem_init - initialize memory management subsystem
358  */
359 static void __init arch_mem_init(char **cmdline_p)
360 {
361 	if (usermem)
362 		pr_info("User-defined physical RAM map overwrite\n");
363 
364 	check_kernel_sections_mem();
365 
366 	early_init_fdt_scan_reserved_mem();
367 
368 	/*
369 	 * In order to reduce the possibility of kernel panic when failed to
370 	 * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
371 	 * low memory as small as possible before plat_swiotlb_setup(), so
372 	 * make sparse_init() using top-down allocation.
373 	 */
374 	memblock_set_bottom_up(false);
375 	sparse_init();
376 	memblock_set_bottom_up(true);
377 
378 	swiotlb_init(true, SWIOTLB_VERBOSE);
379 
380 	dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
381 
382 	/* Reserve for hibernation. */
383 	register_nosave_region(PFN_DOWN(__pa_symbol(&__nosave_begin)),
384 				   PFN_UP(__pa_symbol(&__nosave_end)));
385 
386 	memblock_dump_all();
387 
388 	early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
389 }
390 
391 static void __init resource_init(void)
392 {
393 	long i = 0;
394 	size_t res_size;
395 	struct resource *res;
396 	struct memblock_region *region;
397 
398 	code_resource.start = __pa_symbol(&_text);
399 	code_resource.end = __pa_symbol(&_etext) - 1;
400 	data_resource.start = __pa_symbol(&_etext);
401 	data_resource.end = __pa_symbol(&_edata) - 1;
402 	bss_resource.start = __pa_symbol(&__bss_start);
403 	bss_resource.end = __pa_symbol(&__bss_stop) - 1;
404 
405 	num_standard_resources = memblock.memory.cnt;
406 	res_size = num_standard_resources * sizeof(*standard_resources);
407 	standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
408 
409 	for_each_mem_region(region) {
410 		res = &standard_resources[i++];
411 		if (!memblock_is_nomap(region)) {
412 			res->name  = "System RAM";
413 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
414 			res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
415 			res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
416 		} else {
417 			res->name  = "Reserved";
418 			res->flags = IORESOURCE_MEM;
419 			res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
420 			res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
421 		}
422 
423 		request_resource(&iomem_resource, res);
424 
425 		/*
426 		 *  We don't know which RAM region contains kernel data,
427 		 *  so we try it repeatedly and let the resource manager
428 		 *  test it.
429 		 */
430 		request_resource(res, &code_resource);
431 		request_resource(res, &data_resource);
432 		request_resource(res, &bss_resource);
433 	}
434 
435 #ifdef CONFIG_KEXEC
436 	if (crashk_res.start < crashk_res.end) {
437 		insert_resource(&iomem_resource, &crashk_res);
438 		pr_info("Reserving %ldMB of memory at %ldMB for crashkernel\n",
439 			(unsigned long)((crashk_res.end - crashk_res.start + 1) >> 20),
440 			(unsigned long)(crashk_res.start  >> 20));
441 	}
442 #endif
443 }
444 
445 static int __init add_legacy_isa_io(struct fwnode_handle *fwnode,
446 				resource_size_t hw_start, resource_size_t size)
447 {
448 	int ret = 0;
449 	unsigned long vaddr;
450 	struct logic_pio_hwaddr *range;
451 
452 	range = kzalloc(sizeof(*range), GFP_ATOMIC);
453 	if (!range)
454 		return -ENOMEM;
455 
456 	range->fwnode = fwnode;
457 	range->size = size = round_up(size, PAGE_SIZE);
458 	range->hw_start = hw_start;
459 	range->flags = LOGIC_PIO_CPU_MMIO;
460 
461 	ret = logic_pio_register_range(range);
462 	if (ret) {
463 		kfree(range);
464 		return ret;
465 	}
466 
467 	/* Legacy ISA must placed at the start of PCI_IOBASE */
468 	if (range->io_start != 0) {
469 		logic_pio_unregister_range(range);
470 		kfree(range);
471 		return -EINVAL;
472 	}
473 
474 	vaddr = (unsigned long)(PCI_IOBASE + range->io_start);
475 	ioremap_page_range(vaddr, vaddr + size, hw_start, pgprot_device(PAGE_KERNEL));
476 
477 	return 0;
478 }
479 
480 static __init int arch_reserve_pio_range(void)
481 {
482 	struct device_node *np;
483 
484 	for_each_node_by_name(np, "isa") {
485 		struct of_range range;
486 		struct of_range_parser parser;
487 
488 		pr_info("ISA Bridge: %pOF\n", np);
489 
490 		if (of_range_parser_init(&parser, np)) {
491 			pr_info("Failed to parse resources.\n");
492 			of_node_put(np);
493 			break;
494 		}
495 
496 		for_each_of_range(&parser, &range) {
497 			switch (range.flags & IORESOURCE_TYPE_BITS) {
498 			case IORESOURCE_IO:
499 				pr_info(" IO 0x%016llx..0x%016llx  ->  0x%016llx\n",
500 					range.cpu_addr,
501 					range.cpu_addr + range.size - 1,
502 					range.bus_addr);
503 				if (add_legacy_isa_io(&np->fwnode, range.cpu_addr, range.size))
504 					pr_warn("Failed to reserve legacy IO in Logic PIO\n");
505 				break;
506 			case IORESOURCE_MEM:
507 				pr_info(" MEM 0x%016llx..0x%016llx  ->  0x%016llx\n",
508 					range.cpu_addr,
509 					range.cpu_addr + range.size - 1,
510 					range.bus_addr);
511 				break;
512 			}
513 		}
514 	}
515 
516 	return 0;
517 }
518 arch_initcall(arch_reserve_pio_range);
519 
520 static int __init reserve_memblock_reserved_regions(void)
521 {
522 	u64 i, j;
523 
524 	for (i = 0; i < num_standard_resources; ++i) {
525 		struct resource *mem = &standard_resources[i];
526 		phys_addr_t r_start, r_end, mem_size = resource_size(mem);
527 
528 		if (!memblock_is_region_reserved(mem->start, mem_size))
529 			continue;
530 
531 		for_each_reserved_mem_range(j, &r_start, &r_end) {
532 			resource_size_t start, end;
533 
534 			start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
535 			end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
536 
537 			if (start > mem->end || end < mem->start)
538 				continue;
539 
540 			reserve_region_with_split(mem, start, end, "Reserved");
541 		}
542 	}
543 
544 	return 0;
545 }
546 arch_initcall(reserve_memblock_reserved_regions);
547 
548 #ifdef CONFIG_SMP
549 static void __init prefill_possible_map(void)
550 {
551 	int i, possible;
552 
553 	possible = num_processors + disabled_cpus;
554 	if (possible > nr_cpu_ids)
555 		possible = nr_cpu_ids;
556 
557 	pr_info("SMP: Allowing %d CPUs, %d hotplug CPUs\n",
558 			possible, max((possible - num_processors), 0));
559 
560 	for (i = 0; i < possible; i++)
561 		set_cpu_possible(i, true);
562 	for (; i < NR_CPUS; i++)
563 		set_cpu_possible(i, false);
564 
565 	set_nr_cpu_ids(possible);
566 }
567 #endif
568 
569 void __init setup_arch(char **cmdline_p)
570 {
571 	cpu_probe();
572 
573 	init_environ();
574 	efi_init();
575 	fdt_setup();
576 	memblock_init();
577 	pagetable_init();
578 	bootcmdline_init(cmdline_p);
579 	parse_early_param();
580 	reserve_initrd_mem();
581 
582 	platform_init();
583 	arch_mem_init(cmdline_p);
584 
585 	resource_init();
586 #ifdef CONFIG_SMP
587 	plat_smp_setup();
588 	prefill_possible_map();
589 #endif
590 
591 	paging_init();
592 }
593