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