File Index
Files and directories
Sign in
FILE

setup.c

File preview with syntax highlighting.

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