History

LoongArch: Add boot and setup routines

Add basic boot, setup and reset routines for LoongArch. Now, LoongArch machines use UEFI-based firmware. The firmware passes configuration information to the kernel via ACPI and DMI/SMBIOS. Currently an existing interface between the kernel and the bootloader is implemented. Kernel gets 2 values from the bootloader, passed in registers a0 and a1; a0 is an "EFI boot flag" distinguishing UEFI and non-UEFI firmware, while a1 is a pointer to an FDT with systable, memmap, cmdline and initrd information. The standard UEFI boot protocol (EFISTUB) will be added later.

Huacai Chen committed 4 years ago

Commit 628c3bb1 parent b738c1018 files · +1738 −0Browse files

Unified · Split

18 files
  1. arch/loongarch/include/asm/acenv.h +18 −0
  2. arch/loongarch/include/asm/acpi.h +38 −0
  3. arch/loongarch/include/asm/bootinfo.h +41 −0
  4. arch/loongarch/include/asm/dmi.h +24 −0
  5. arch/loongarch/include/asm/efi.h +41 −0
  6. arch/loongarch/include/asm/reboot.h +10 −0
  7. arch/loongarch/include/asm/setup.h +21 −0
  8. arch/loongarch/kernel/acpi.c +169 −0
  9. arch/loongarch/kernel/cacheinfo.c +122 −0
  10. arch/loongarch/kernel/cpu-probe.c +292 −0
  11. arch/loongarch/kernel/efi.c +72 −0
  12. arch/loongarch/kernel/env.c +101 −0
  13. arch/loongarch/kernel/head.S +68 −0
  14. arch/loongarch/kernel/mem.c +64 −0
  15. arch/loongarch/kernel/reset.c +90 −0
  16. arch/loongarch/kernel/setup.c +341 −0
  17. arch/loongarch/kernel/time.c +213 −0
  18. arch/loongarch/kernel/topology.c +13 −0
arch/loongarch/include/asm/acenv.h+18−0
@@ -0,0 +1,18 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/*
3+ * LoongArch specific ACPICA environments and implementation
4+ *
5+ * Author: Jianmin Lv <lvjianmin@loongson.cn>
6+ * Huacai Chen <chenhuacai@loongson.cn>
7+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
8+ */
9+
10+#ifndef _ASM_LOONGARCH_ACENV_H
11+#define _ASM_LOONGARCH_ACENV_H
12+
13+/*
14+ * This header is required by ACPI core, but we have nothing to fill in
15+ * right now. Will be updated later when needed.
16+ */
17+
18+#endif /* _ASM_LOONGARCH_ACENV_H */
arch/loongarch/include/asm/acpi.h+38−0
@@ -0,0 +1,38 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/*
3+ * Author: Jianmin Lv <lvjianmin@loongson.cn>
4+ * Huacai Chen <chenhuacai@loongson.cn>
5+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6+ */
7+
8+#ifndef _ASM_LOONGARCH_ACPI_H
9+#define _ASM_LOONGARCH_ACPI_H
10+
11+#ifdef CONFIG_ACPI
12+extern int acpi_strict;
13+extern int acpi_disabled;
14+extern int acpi_pci_disabled;
15+extern int acpi_noirq;
16+
17+#define acpi_os_ioremap acpi_os_ioremap
18+void __init __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size);
19+
20+static inline void disable_acpi(void)
21+{
22+ acpi_disabled = 1;
23+ acpi_pci_disabled = 1;
24+ acpi_noirq = 1;
25+}
26+
27+static inline bool acpi_has_cpu_in_madt(void)
28+{
29+ return true;
30+}
31+
32+extern struct list_head acpi_wakeup_device_list;
33+
34+#endif /* !CONFIG_ACPI */
35+
36+#define ACPI_TABLE_UPGRADE_MAX_PHYS ARCH_LOW_ADDRESS_LIMIT
37+
38+#endif /* _ASM_LOONGARCH_ACPI_H */
arch/loongarch/include/asm/bootinfo.h+41−0
@@ -0,0 +1,41 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/*
3+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+ */
5+#ifndef _ASM_BOOTINFO_H
6+#define _ASM_BOOTINFO_H
7+
8+#include <linux/types.h>
9+#include <asm/setup.h>
10+
11+const char *get_system_type(void);
12+
13+extern void init_environ(void);
14+extern void memblock_init(void);
15+extern void platform_init(void);
16+
17+struct loongson_board_info {
18+ int bios_size;
19+ const char *bios_vendor;
20+ const char *bios_version;
21+ const char *bios_release_date;
22+ const char *board_name;
23+ const char *board_vendor;
24+};
25+
26+struct loongson_system_configuration {
27+ int nr_cpus;
28+ int nr_nodes;
29+ int nr_io_pics;
30+ int boot_cpu_id;
31+ int cores_per_node;
32+ int cores_per_package;
33+ const char *cpuname;
34+};
35+
36+extern u64 efi_system_table;
37+extern unsigned long fw_arg0, fw_arg1;
38+extern struct loongson_board_info b_info;
39+extern struct loongson_system_configuration loongson_sysconf;
40+
41+#endif /* _ASM_BOOTINFO_H */
arch/loongarch/include/asm/dmi.h+24−0
@@ -0,0 +1,24 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/*
3+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+ */
5+#ifndef _ASM_DMI_H
6+#define _ASM_DMI_H
7+
8+#include <linux/io.h>
9+#include <linux/memblock.h>
10+
11+#define dmi_early_remap(x, l) dmi_remap(x, l)
12+#define dmi_early_unmap(x, l) dmi_unmap(x)
13+#define dmi_alloc(l) memblock_alloc(l, PAGE_SIZE)
14+
15+static inline void *dmi_remap(u64 phys_addr, unsigned long size)
16+{
17+ return ((void *)TO_CACHE(phys_addr));
18+}
19+
20+static inline void dmi_unmap(void *addr)
21+{
22+}
23+
24+#endif /* _ASM_DMI_H */
arch/loongarch/include/asm/efi.h+41−0
@@ -0,0 +1,41 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/*
3+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+ */
5+#ifndef _ASM_LOONGARCH_EFI_H
6+#define _ASM_LOONGARCH_EFI_H
7+
8+#include <linux/efi.h>
9+
10+void __init efi_init(void);
11+void __init efi_runtime_init(void);
12+void efifb_setup_from_dmi(struct screen_info *si, const char *opt);
13+
14+#define ARCH_EFI_IRQ_FLAGS_MASK 0x00000004 /* Bit 2: CSR.CRMD.IE */
15+
16+#define arch_efi_call_virt_setup() \
17+({ \
18+})
19+
20+#define arch_efi_call_virt(p, f, args...) \
21+({ \
22+ efi_##f##_t * __f; \
23+ __f = p->f; \
24+ __f(args); \
25+})
26+
27+#define arch_efi_call_virt_teardown() \
28+({ \
29+})
30+
31+#define EFI_ALLOC_ALIGN SZ_64K
32+
33+struct screen_info *alloc_screen_info(void);
34+void free_screen_info(struct screen_info *si);
35+
36+static inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr)
37+{
38+ return ULONG_MAX;
39+}
40+
41+#endif /* _ASM_LOONGARCH_EFI_H */
arch/loongarch/include/asm/reboot.h+10−0
@@ -0,0 +1,10 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/*
3+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+ */
5+#ifndef _ASM_REBOOT_H
6+#define _ASM_REBOOT_H
7+
8+extern void (*pm_restart)(void);
9+
10+#endif /* _ASM_REBOOT_H */
arch/loongarch/include/asm/setup.h+21−0
@@ -0,0 +1,21 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/*
3+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+ */
5+
6+#ifndef _LOONGARCH_SETUP_H
7+#define _LOONGARCH_SETUP_H
8+
9+#include <linux/types.h>
10+#include <uapi/asm/setup.h>
11+
12+#define VECSIZE 0x200
13+
14+extern unsigned long eentry;
15+extern unsigned long tlbrentry;
16+extern void cpu_cache_init(void);
17+extern void per_cpu_trap_init(int cpu);
18+extern void set_handler(unsigned long offset, void *addr, unsigned long len);
19+extern void set_merr_handler(unsigned long offset, void *addr, unsigned long len);
20+
21+#endif /* __SETUP_H */
arch/loongarch/kernel/acpi.c+169−0
@@ -0,0 +1,169 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * acpi.c - Architecture-Specific Low-Level ACPI Boot Support
4+ *
5+ * Author: Jianmin Lv <lvjianmin@loongson.cn>
6+ * Huacai Chen <chenhuacai@loongson.cn>
7+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
8+ */
9+
10+#include <linux/init.h>
11+#include <linux/acpi.h>
12+#include <linux/irq.h>
13+#include <linux/irqdomain.h>
14+#include <linux/memblock.h>
15+#include <linux/serial_core.h>
16+#include <asm/io.h>
17+#include <asm/loongson.h>
18+
19+int acpi_disabled;
20+EXPORT_SYMBOL(acpi_disabled);
21+int acpi_noirq;
22+int acpi_pci_disabled;
23+EXPORT_SYMBOL(acpi_pci_disabled);
24+int acpi_strict = 1; /* We have no workarounds on LoongArch */
25+int num_processors;
26+int disabled_cpus;
27+enum acpi_irq_model_id acpi_irq_model = ACPI_IRQ_MODEL_PLATFORM;
28+
29+u64 acpi_saved_sp;
30+
31+#define MAX_CORE_PIC 256
32+
33+#define PREFIX "ACPI: "
34+
35+int acpi_gsi_to_irq(u32 gsi, unsigned int *irqp)
36+{
37+ if (irqp != NULL)
38+ *irqp = acpi_register_gsi(NULL, gsi, -1, -1);
39+ return (*irqp >= 0) ? 0 : -EINVAL;
40+}
41+EXPORT_SYMBOL_GPL(acpi_gsi_to_irq);
42+
43+int acpi_isa_irq_to_gsi(unsigned int isa_irq, u32 *gsi)
44+{
45+ if (gsi)
46+ *gsi = isa_irq;
47+ return 0;
48+}
49+
50+/*
51+ * success: return IRQ number (>=0)
52+ * failure: return < 0
53+ */
54+int acpi_register_gsi(struct device *dev, u32 gsi, int trigger, int polarity)
55+{
56+ struct irq_fwspec fwspec;
57+
58+ switch (gsi) {
59+ case GSI_MIN_CPU_IRQ ... GSI_MAX_CPU_IRQ:
60+ fwspec.fwnode = liointc_domain->fwnode;
61+ fwspec.param[0] = gsi - GSI_MIN_CPU_IRQ;
62+ fwspec.param_count = 1;
63+
64+ return irq_create_fwspec_mapping(&fwspec);
65+
66+ case GSI_MIN_LPC_IRQ ... GSI_MAX_LPC_IRQ:
67+ if (!pch_lpc_domain)
68+ return -EINVAL;
69+
70+ fwspec.fwnode = pch_lpc_domain->fwnode;
71+ fwspec.param[0] = gsi - GSI_MIN_LPC_IRQ;
72+ fwspec.param[1] = acpi_dev_get_irq_type(trigger, polarity);
73+ fwspec.param_count = 2;
74+
75+ return irq_create_fwspec_mapping(&fwspec);
76+
77+ case GSI_MIN_PCH_IRQ ... GSI_MAX_PCH_IRQ:
78+ if (!pch_pic_domain[0])
79+ return -EINVAL;
80+
81+ fwspec.fwnode = pch_pic_domain[0]->fwnode;
82+ fwspec.param[0] = gsi - GSI_MIN_PCH_IRQ;
83+ fwspec.param[1] = IRQ_TYPE_LEVEL_HIGH;
84+ fwspec.param_count = 2;
85+
86+ return irq_create_fwspec_mapping(&fwspec);
87+ }
88+
89+ return -EINVAL;
90+}
91+EXPORT_SYMBOL_GPL(acpi_register_gsi);
92+
93+void acpi_unregister_gsi(u32 gsi)
94+{
95+
96+}
97+EXPORT_SYMBOL_GPL(acpi_unregister_gsi);
98+
99+void __init __iomem * __acpi_map_table(unsigned long phys, unsigned long size)
100+{
101+
102+ if (!phys || !size)
103+ return NULL;
104+
105+ return early_memremap(phys, size);
106+}
107+void __init __acpi_unmap_table(void __iomem *map, unsigned long size)
108+{
109+ if (!map || !size)
110+ return;
111+
112+ early_memunmap(map, size);
113+}
114+
115+void __init __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
116+{
117+ if (!memblock_is_memory(phys))
118+ return ioremap(phys, size);
119+ else
120+ return ioremap_cache(phys, size);
121+}
122+
123+void __init acpi_boot_table_init(void)
124+{
125+ /*
126+ * If acpi_disabled, bail out
127+ */
128+ if (acpi_disabled)
129+ return;
130+
131+ /*
132+ * Initialize the ACPI boot-time table parser.
133+ */
134+ if (acpi_table_init()) {
135+ disable_acpi();
136+ return;
137+ }
138+}
139+
140+static void __init acpi_process_madt(void)
141+{
142+ loongson_sysconf.nr_cpus = num_processors;
143+}
144+
145+int __init acpi_boot_init(void)
146+{
147+ /*
148+ * If acpi_disabled, bail out
149+ */
150+ if (acpi_disabled)
151+ return -1;
152+
153+ loongson_sysconf.boot_cpu_id = read_csr_cpuid();
154+
155+ /*
156+ * Process the Multiple APIC Description Table (MADT), if present
157+ */
158+ acpi_process_madt();
159+
160+ /* Do not enable ACPI SPCR console by default */
161+ acpi_parse_spcr(earlycon_acpi_spcr_enable, false);
162+
163+ return 0;
164+}
165+
166+void __init arch_reserve_mem_area(acpi_physical_address addr, size_t size)
167+{
168+ memblock_reserve(addr, size);
169+}
arch/loongarch/kernel/cacheinfo.c+122−0
@@ -0,0 +1,122 @@
1+// SPDX-License-Identifier: GPL-2.0-only
2+/*
3+ * LoongArch cacheinfo support
4+ *
5+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6+ */
7+#include <linux/cacheinfo.h>
8+
9+/* Populates leaf and increments to next leaf */
10+#define populate_cache(cache, leaf, c_level, c_type) \
11+do { \
12+ leaf->type = c_type; \
13+ leaf->level = c_level; \
14+ leaf->coherency_line_size = c->cache.linesz; \
15+ leaf->number_of_sets = c->cache.sets; \
16+ leaf->ways_of_associativity = c->cache.ways; \
17+ leaf->size = c->cache.linesz * c->cache.sets * \
18+ c->cache.ways; \
19+ leaf++; \
20+} while (0)
21+
22+int init_cache_level(unsigned int cpu)
23+{
24+ struct cpuinfo_loongarch *c = &current_cpu_data;
25+ struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
26+ int levels = 0, leaves = 0;
27+
28+ /*
29+ * If Dcache is not set, we assume the cache structures
30+ * are not properly initialized.
31+ */
32+ if (c->dcache.waysize)
33+ levels += 1;
34+ else
35+ return -ENOENT;
36+
37+
38+ leaves += (c->icache.waysize) ? 2 : 1;
39+
40+ if (c->vcache.waysize) {
41+ levels++;
42+ leaves++;
43+ }
44+
45+ if (c->scache.waysize) {
46+ levels++;
47+ leaves++;
48+ }
49+
50+ if (c->tcache.waysize) {
51+ levels++;
52+ leaves++;
53+ }
54+
55+ this_cpu_ci->num_levels = levels;
56+ this_cpu_ci->num_leaves = leaves;
57+ return 0;
58+}
59+
60+static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
61+ struct cacheinfo *sib_leaf)
62+{
63+ return !((this_leaf->level == 1) || (this_leaf->level == 2));
64+}
65+
66+static void cache_cpumap_setup(unsigned int cpu)
67+{
68+ struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
69+ struct cacheinfo *this_leaf, *sib_leaf;
70+ unsigned int index;
71+
72+ for (index = 0; index < this_cpu_ci->num_leaves; index++) {
73+ unsigned int i;
74+
75+ this_leaf = this_cpu_ci->info_list + index;
76+ /* skip if shared_cpu_map is already populated */
77+ if (!cpumask_empty(&this_leaf->shared_cpu_map))
78+ continue;
79+
80+ cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
81+ for_each_online_cpu(i) {
82+ struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
83+
84+ if (i == cpu || !sib_cpu_ci->info_list)
85+ continue;/* skip if itself or no cacheinfo */
86+ sib_leaf = sib_cpu_ci->info_list + index;
87+ if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
88+ cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map);
89+ cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
90+ }
91+ }
92+ }
93+}
94+
95+int populate_cache_leaves(unsigned int cpu)
96+{
97+ int level = 1;
98+ struct cpuinfo_loongarch *c = &current_cpu_data;
99+ struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
100+ struct cacheinfo *this_leaf = this_cpu_ci->info_list;
101+
102+ if (c->icache.waysize) {
103+ populate_cache(dcache, this_leaf, level, CACHE_TYPE_DATA);
104+ populate_cache(icache, this_leaf, level++, CACHE_TYPE_INST);
105+ } else {
106+ populate_cache(dcache, this_leaf, level++, CACHE_TYPE_UNIFIED);
107+ }
108+
109+ if (c->vcache.waysize)
110+ populate_cache(vcache, this_leaf, level++, CACHE_TYPE_UNIFIED);
111+
112+ if (c->scache.waysize)
113+ populate_cache(scache, this_leaf, level++, CACHE_TYPE_UNIFIED);
114+
115+ if (c->tcache.waysize)
116+ populate_cache(tcache, this_leaf, level++, CACHE_TYPE_UNIFIED);
117+
118+ cache_cpumap_setup(cpu);
119+ this_cpu_ci->cpu_map_populated = true;
120+
121+ return 0;
122+}
arch/loongarch/kernel/cpu-probe.c+292−0
@@ -0,0 +1,292 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * Processor capabilities determination functions.
4+ *
5+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6+ */
7+#include <linux/init.h>
8+#include <linux/kernel.h>
9+#include <linux/ptrace.h>
10+#include <linux/smp.h>
11+#include <linux/stddef.h>
12+#include <linux/export.h>
13+#include <linux/printk.h>
14+#include <linux/uaccess.h>
15+
16+#include <asm/cpu-features.h>
17+#include <asm/elf.h>
18+#include <asm/fpu.h>
19+#include <asm/loongarch.h>
20+#include <asm/pgtable-bits.h>
21+#include <asm/setup.h>
22+
23+/* Hardware capabilities */
24+unsigned int elf_hwcap __read_mostly;
25+EXPORT_SYMBOL_GPL(elf_hwcap);
26+
27+/*
28+ * Determine the FCSR mask for FPU hardware.
29+ */
30+static inline void cpu_set_fpu_fcsr_mask(struct cpuinfo_loongarch *c)
31+{
32+ unsigned long sr, mask, fcsr, fcsr0, fcsr1;
33+
34+ fcsr = c->fpu_csr0;
35+ mask = FPU_CSR_ALL_X | FPU_CSR_ALL_E | FPU_CSR_ALL_S | FPU_CSR_RM;
36+
37+ sr = read_csr_euen();
38+ enable_fpu();
39+
40+ fcsr0 = fcsr & mask;
41+ write_fcsr(LOONGARCH_FCSR0, fcsr0);
42+ fcsr0 = read_fcsr(LOONGARCH_FCSR0);
43+
44+ fcsr1 = fcsr | ~mask;
45+ write_fcsr(LOONGARCH_FCSR0, fcsr1);
46+ fcsr1 = read_fcsr(LOONGARCH_FCSR0);
47+
48+ write_fcsr(LOONGARCH_FCSR0, fcsr);
49+
50+ write_csr_euen(sr);
51+
52+ c->fpu_mask = ~(fcsr0 ^ fcsr1) & ~mask;
53+}
54+
55+static inline void set_elf_platform(int cpu, const char *plat)
56+{
57+ if (cpu == 0)
58+ __elf_platform = plat;
59+}
60+
61+/* MAP BASE */
62+unsigned long vm_map_base;
63+EXPORT_SYMBOL_GPL(vm_map_base);
64+
65+static void cpu_probe_addrbits(struct cpuinfo_loongarch *c)
66+{
67+#ifdef __NEED_ADDRBITS_PROBE
68+ c->pabits = (read_cpucfg(LOONGARCH_CPUCFG1) & CPUCFG1_PABITS) >> 4;
69+ c->vabits = (read_cpucfg(LOONGARCH_CPUCFG1) & CPUCFG1_VABITS) >> 12;
70+ vm_map_base = 0UL - (1UL << c->vabits);
71+#endif
72+}
73+
74+static void set_isa(struct cpuinfo_loongarch *c, unsigned int isa)
75+{
76+ switch (isa) {
77+ case LOONGARCH_CPU_ISA_LA64:
78+ c->isa_level |= LOONGARCH_CPU_ISA_LA64;
79+ fallthrough;
80+ case LOONGARCH_CPU_ISA_LA32S:
81+ c->isa_level |= LOONGARCH_CPU_ISA_LA32S;
82+ fallthrough;
83+ case LOONGARCH_CPU_ISA_LA32R:
84+ c->isa_level |= LOONGARCH_CPU_ISA_LA32R;
85+ break;
86+ }
87+}
88+
89+static void cpu_probe_common(struct cpuinfo_loongarch *c)
90+{
91+ unsigned int config;
92+ unsigned long asid_mask;
93+
94+ c->options = LOONGARCH_CPU_CPUCFG | LOONGARCH_CPU_CSR |
95+ LOONGARCH_CPU_TLB | LOONGARCH_CPU_VINT | LOONGARCH_CPU_WATCH;
96+
97+ elf_hwcap |= HWCAP_LOONGARCH_CRC32;
98+
99+ config = read_cpucfg(LOONGARCH_CPUCFG1);
100+ if (config & CPUCFG1_UAL) {
101+ c->options |= LOONGARCH_CPU_UAL;
102+ elf_hwcap |= HWCAP_LOONGARCH_UAL;
103+ }
104+
105+ config = read_cpucfg(LOONGARCH_CPUCFG2);
106+ if (config & CPUCFG2_LAM) {
107+ c->options |= LOONGARCH_CPU_LAM;
108+ elf_hwcap |= HWCAP_LOONGARCH_LAM;
109+ }
110+ if (config & CPUCFG2_FP) {
111+ c->options |= LOONGARCH_CPU_FPU;
112+ elf_hwcap |= HWCAP_LOONGARCH_FPU;
113+ }
114+ if (config & CPUCFG2_COMPLEX) {
115+ c->options |= LOONGARCH_CPU_COMPLEX;
116+ elf_hwcap |= HWCAP_LOONGARCH_COMPLEX;
117+ }
118+ if (config & CPUCFG2_CRYPTO) {
119+ c->options |= LOONGARCH_CPU_CRYPTO;
120+ elf_hwcap |= HWCAP_LOONGARCH_CRYPTO;
121+ }
122+ if (config & CPUCFG2_LVZP) {
123+ c->options |= LOONGARCH_CPU_LVZ;
124+ elf_hwcap |= HWCAP_LOONGARCH_LVZ;
125+ }
126+
127+ config = read_cpucfg(LOONGARCH_CPUCFG6);
128+ if (config & CPUCFG6_PMP)
129+ c->options |= LOONGARCH_CPU_PMP;
130+
131+ config = iocsr_read32(LOONGARCH_IOCSR_FEATURES);
132+ if (config & IOCSRF_CSRIPI)
133+ c->options |= LOONGARCH_CPU_CSRIPI;
134+ if (config & IOCSRF_EXTIOI)
135+ c->options |= LOONGARCH_CPU_EXTIOI;
136+ if (config & IOCSRF_FREQSCALE)
137+ c->options |= LOONGARCH_CPU_SCALEFREQ;
138+ if (config & IOCSRF_FLATMODE)
139+ c->options |= LOONGARCH_CPU_FLATMODE;
140+ if (config & IOCSRF_EIODECODE)
141+ c->options |= LOONGARCH_CPU_EIODECODE;
142+ if (config & IOCSRF_VM)
143+ c->options |= LOONGARCH_CPU_HYPERVISOR;
144+
145+ config = csr_read32(LOONGARCH_CSR_ASID);
146+ config = (config & CSR_ASID_BIT) >> CSR_ASID_BIT_SHIFT;
147+ asid_mask = GENMASK(config - 1, 0);
148+ set_cpu_asid_mask(c, asid_mask);
149+
150+ config = read_csr_prcfg1();
151+ c->ksave_mask = GENMASK((config & CSR_CONF1_KSNUM) - 1, 0);
152+ c->ksave_mask &= ~(EXC_KSAVE_MASK | PERCPU_KSAVE_MASK | KVM_KSAVE_MASK);
153+
154+ config = read_csr_prcfg3();
155+ switch (config & CSR_CONF3_TLBTYPE) {
156+ case 0:
157+ c->tlbsizemtlb = 0;
158+ c->tlbsizestlbsets = 0;
159+ c->tlbsizestlbways = 0;
160+ c->tlbsize = 0;
161+ break;
162+ case 1:
163+ c->tlbsizemtlb = ((config & CSR_CONF3_MTLBSIZE) >> CSR_CONF3_MTLBSIZE_SHIFT) + 1;
164+ c->tlbsizestlbsets = 0;
165+ c->tlbsizestlbways = 0;
166+ c->tlbsize = c->tlbsizemtlb + c->tlbsizestlbsets * c->tlbsizestlbways;
167+ break;
168+ case 2:
169+ c->tlbsizemtlb = ((config & CSR_CONF3_MTLBSIZE) >> CSR_CONF3_MTLBSIZE_SHIFT) + 1;
170+ c->tlbsizestlbsets = 1 << ((config & CSR_CONF3_STLBIDX) >> CSR_CONF3_STLBIDX_SHIFT);
171+ c->tlbsizestlbways = ((config & CSR_CONF3_STLBWAYS) >> CSR_CONF3_STLBWAYS_SHIFT) + 1;
172+ c->tlbsize = c->tlbsizemtlb + c->tlbsizestlbsets * c->tlbsizestlbways;
173+ break;
174+ default:
175+ pr_warn("Warning: unknown TLB type\n");
176+ }
177+}
178+
179+#define MAX_NAME_LEN 32
180+#define VENDOR_OFFSET 0
181+#define CPUNAME_OFFSET 9
182+
183+static char cpu_full_name[MAX_NAME_LEN] = " - ";
184+
185+static inline void cpu_probe_loongson(struct cpuinfo_loongarch *c, unsigned int cpu)
186+{
187+ uint64_t *vendor = (void *)(&cpu_full_name[VENDOR_OFFSET]);
188+ uint64_t *cpuname = (void *)(&cpu_full_name[CPUNAME_OFFSET]);
189+
190+ __cpu_full_name[cpu] = cpu_full_name;
191+ *vendor = iocsr_read64(LOONGARCH_IOCSR_VENDOR);
192+ *cpuname = iocsr_read64(LOONGARCH_IOCSR_CPUNAME);
193+
194+ switch (c->processor_id & PRID_SERIES_MASK) {
195+ case PRID_SERIES_LA132:
196+ c->cputype = CPU_LOONGSON32;
197+ set_isa(c, LOONGARCH_CPU_ISA_LA32S);
198+ __cpu_family[cpu] = "Loongson-32bit";
199+ pr_info("32-bit Loongson Processor probed (LA132 Core)\n");
200+ break;
201+ case PRID_SERIES_LA264:
202+ c->cputype = CPU_LOONGSON64;
203+ set_isa(c, LOONGARCH_CPU_ISA_LA64);
204+ __cpu_family[cpu] = "Loongson-64bit";
205+ pr_info("64-bit Loongson Processor probed (LA264 Core)\n");
206+ break;
207+ case PRID_SERIES_LA364:
208+ c->cputype = CPU_LOONGSON64;
209+ set_isa(c, LOONGARCH_CPU_ISA_LA64);
210+ __cpu_family[cpu] = "Loongson-64bit";
211+ pr_info("64-bit Loongson Processor probed (LA364 Core)\n");
212+ break;
213+ case PRID_SERIES_LA464:
214+ c->cputype = CPU_LOONGSON64;
215+ set_isa(c, LOONGARCH_CPU_ISA_LA64);
216+ __cpu_family[cpu] = "Loongson-64bit";
217+ pr_info("64-bit Loongson Processor probed (LA464 Core)\n");
218+ break;
219+ case PRID_SERIES_LA664:
220+ c->cputype = CPU_LOONGSON64;
221+ set_isa(c, LOONGARCH_CPU_ISA_LA64);
222+ __cpu_family[cpu] = "Loongson-64bit";
223+ pr_info("64-bit Loongson Processor probed (LA664 Core)\n");
224+ break;
225+ default: /* Default to 64 bit */
226+ c->cputype = CPU_LOONGSON64;
227+ set_isa(c, LOONGARCH_CPU_ISA_LA64);
228+ __cpu_family[cpu] = "Loongson-64bit";
229+ pr_info("64-bit Loongson Processor probed (Unknown Core)\n");
230+ }
231+}
232+
233+#ifdef CONFIG_64BIT
234+/* For use by uaccess.h */
235+u64 __ua_limit;
236+EXPORT_SYMBOL(__ua_limit);
237+#endif
238+
239+const char *__cpu_family[NR_CPUS];
240+const char *__cpu_full_name[NR_CPUS];
241+const char *__elf_platform;
242+
243+static void cpu_report(void)
244+{
245+ struct cpuinfo_loongarch *c = &current_cpu_data;
246+
247+ pr_info("CPU%d revision is: %08x (%s)\n",
248+ smp_processor_id(), c->processor_id, cpu_family_string());
249+ if (c->options & LOONGARCH_CPU_FPU)
250+ pr_info("FPU%d revision is: %08x\n", smp_processor_id(), c->fpu_vers);
251+}
252+
253+void cpu_probe(void)
254+{
255+ unsigned int cpu = smp_processor_id();
256+ struct cpuinfo_loongarch *c = &current_cpu_data;
257+
258+ /*
259+ * Set a default ELF platform, cpu probe may later
260+ * overwrite it with a more precise value
261+ */
262+ set_elf_platform(cpu, "loongarch");
263+
264+ c->cputype = CPU_UNKNOWN;
265+ c->processor_id = read_cpucfg(LOONGARCH_CPUCFG0);
266+ c->fpu_vers = (read_cpucfg(LOONGARCH_CPUCFG2) >> 3) & 0x3;
267+
268+ c->fpu_csr0 = FPU_CSR_RN;
269+ c->fpu_mask = FPU_CSR_RSVD;
270+
271+ cpu_probe_common(c);
272+
273+ per_cpu_trap_init(cpu);
274+
275+ switch (c->processor_id & PRID_COMP_MASK) {
276+ case PRID_COMP_LOONGSON:
277+ cpu_probe_loongson(c, cpu);
278+ break;
279+ }
280+
281+ BUG_ON(!__cpu_family[cpu]);
282+ BUG_ON(c->cputype == CPU_UNKNOWN);
283+
284+ cpu_probe_addrbits(c);
285+
286+#ifdef CONFIG_64BIT
287+ if (cpu == 0)
288+ __ua_limit = ~((1ull << cpu_vabits) - 1);
289+#endif
290+
291+ cpu_report();
292+}
arch/loongarch/kernel/efi.c+72−0
@@ -0,0 +1,72 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * EFI initialization
4+ *
5+ * Author: Jianmin Lv <lvjianmin@loongson.cn>
6+ * Huacai Chen <chenhuacai@loongson.cn>
7+ *
8+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
9+ */
10+
11+#include <linux/acpi.h>
12+#include <linux/efi.h>
13+#include <linux/efi-bgrt.h>
14+#include <linux/init.h>
15+#include <linux/kernel.h>
16+#include <linux/export.h>
17+#include <linux/io.h>
18+#include <linux/kobject.h>
19+#include <linux/memblock.h>
20+#include <linux/reboot.h>
21+#include <linux/uaccess.h>
22+
23+#include <asm/early_ioremap.h>
24+#include <asm/efi.h>
25+#include <asm/loongson.h>
26+
27+static unsigned long efi_nr_tables;
28+static unsigned long efi_config_table;
29+
30+static efi_system_table_t *efi_systab;
31+static efi_config_table_type_t arch_tables[] __initdata = {{},};
32+
33+void __init efi_runtime_init(void)
34+{
35+ if (!efi_enabled(EFI_BOOT))
36+ return;
37+
38+ if (efi_runtime_disabled()) {
39+ pr_info("EFI runtime services will be disabled.\n");
40+ return;
41+ }
42+
43+ efi.runtime = (efi_runtime_services_t *)efi_systab->runtime;
44+ efi.runtime_version = (unsigned int)efi.runtime->hdr.revision;
45+
46+ efi_native_runtime_setup();
47+ set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
48+}
49+
50+void __init efi_init(void)
51+{
52+ int size;
53+ void *config_tables;
54+
55+ if (!efi_system_table)
56+ return;
57+
58+ efi_systab = (efi_system_table_t *)early_memremap_ro(efi_system_table, sizeof(*efi_systab));
59+ if (!efi_systab) {
60+ pr_err("Can't find EFI system table.\n");
61+ return;
62+ }
63+
64+ set_bit(EFI_64BIT, &efi.flags);
65+ efi_nr_tables = efi_systab->nr_tables;
66+ efi_config_table = (unsigned long)efi_systab->tables;
67+
68+ size = sizeof(efi_config_table_t);
69+ config_tables = early_memremap(efi_config_table, efi_nr_tables * size);
70+ efi_config_parse_tables(config_tables, efi_systab->nr_tables, arch_tables);
71+ early_memunmap(config_tables, efi_nr_tables * size);
72+}
arch/loongarch/kernel/env.c+101−0
@@ -0,0 +1,101 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * Author: Huacai Chen <chenhuacai@loongson.cn>
4+ *
5+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6+ */
7+#include <linux/acpi.h>
8+#include <linux/efi.h>
9+#include <linux/export.h>
10+#include <linux/memblock.h>
11+#include <linux/of_fdt.h>
12+#include <asm/early_ioremap.h>
13+#include <asm/bootinfo.h>
14+#include <asm/loongson.h>
15+
16+u64 efi_system_table;
17+struct loongson_system_configuration loongson_sysconf;
18+EXPORT_SYMBOL(loongson_sysconf);
19+
20+u64 loongson_chipcfg[MAX_PACKAGES];
21+u64 loongson_chiptemp[MAX_PACKAGES];
22+u64 loongson_freqctrl[MAX_PACKAGES];
23+unsigned long long smp_group[MAX_PACKAGES];
24+
25+static void __init register_addrs_set(u64 *registers, const u64 addr, int num)
26+{
27+ u64 i;
28+
29+ for (i = 0; i < num; i++) {
30+ *registers = (i << 44) | addr;
31+ registers++;
32+ }
33+}
34+
35+void __init init_environ(void)
36+{
37+ int efi_boot = fw_arg0;
38+ struct efi_memory_map_data data;
39+ void *fdt_ptr = early_memremap_ro(fw_arg1, SZ_64K);
40+
41+ if (efi_boot)
42+ set_bit(EFI_BOOT, &efi.flags);
43+ else
44+ clear_bit(EFI_BOOT, &efi.flags);
45+
46+ early_init_dt_scan(fdt_ptr);
47+ early_init_fdt_reserve_self();
48+ efi_system_table = efi_get_fdt_params(&data);
49+
50+ efi_memmap_init_early(&data);
51+ memblock_reserve(data.phys_map & PAGE_MASK,
52+ PAGE_ALIGN(data.size + (data.phys_map & ~PAGE_MASK)));
53+
54+ register_addrs_set(smp_group, TO_UNCACHE(0x1fe01000), 16);
55+ register_addrs_set(loongson_chipcfg, TO_UNCACHE(0x1fe00180), 16);
56+ register_addrs_set(loongson_chiptemp, TO_UNCACHE(0x1fe0019c), 16);
57+ register_addrs_set(loongson_freqctrl, TO_UNCACHE(0x1fe001d0), 16);
58+}
59+
60+static int __init init_cpu_fullname(void)
61+{
62+ int cpu;
63+
64+ if (loongson_sysconf.cpuname && !strncmp(loongson_sysconf.cpuname, "Loongson", 8)) {
65+ for (cpu = 0; cpu < NR_CPUS; cpu++)
66+ __cpu_full_name[cpu] = loongson_sysconf.cpuname;
67+ }
68+ return 0;
69+}
70+arch_initcall(init_cpu_fullname);
71+
72+static ssize_t boardinfo_show(struct kobject *kobj,
73+ struct kobj_attribute *attr, char *buf)
74+{
75+ return sprintf(buf,
76+ "BIOS Information\n"
77+ "Vendor\t\t\t: %s\n"
78+ "Version\t\t\t: %s\n"
79+ "ROM Size\t\t: %d KB\n"
80+ "Release Date\t\t: %s\n\n"
81+ "Board Information\n"
82+ "Manufacturer\t\t: %s\n"
83+ "Board Name\t\t: %s\n"
84+ "Family\t\t\t: LOONGSON64\n\n",
85+ b_info.bios_vendor, b_info.bios_version,
86+ b_info.bios_size, b_info.bios_release_date,
87+ b_info.board_vendor, b_info.board_name);
88+}
89+
90+static struct kobj_attribute boardinfo_attr = __ATTR(boardinfo, 0444,
91+ boardinfo_show, NULL);
92+
93+static int __init boardinfo_init(void)
94+{
95+ struct kobject *loongson_kobj;
96+
97+ loongson_kobj = kobject_create_and_add("loongson", firmware_kobj);
98+
99+ return sysfs_create_file(loongson_kobj, &boardinfo_attr.attr);
100+}
101+late_initcall(boardinfo_init);
arch/loongarch/kernel/head.S+68−0
@@ -0,0 +1,68 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/*
3+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+ */
5+#include <linux/init.h>
6+#include <linux/threads.h>
7+
8+#include <asm/addrspace.h>
9+#include <asm/asm.h>
10+#include <asm/asmmacro.h>
11+#include <asm/regdef.h>
12+#include <asm/loongarch.h>
13+#include <asm/stackframe.h>
14+
15+ __REF
16+
17+SYM_ENTRY(_stext, SYM_L_GLOBAL, SYM_A_NONE)
18+
19+SYM_CODE_START(kernel_entry) # kernel entry point
20+
21+ /* Config direct window and set PG */
22+ li.d t0, CSR_DMW0_INIT # UC, PLV0, 0x8000 xxxx xxxx xxxx
23+ csrwr t0, LOONGARCH_CSR_DMWIN0
24+ li.d t0, CSR_DMW1_INIT # CA, PLV0, 0x9000 xxxx xxxx xxxx
25+ csrwr t0, LOONGARCH_CSR_DMWIN1
26+ /* Enable PG */
27+ li.w t0, 0xb0 # PLV=0, IE=0, PG=1
28+ csrwr t0, LOONGARCH_CSR_CRMD
29+ li.w t0, 0x04 # PLV=0, PIE=1, PWE=0
30+ csrwr t0, LOONGARCH_CSR_PRMD
31+ li.w t0, 0x00 # FPE=0, SXE=0, ASXE=0, BTE=0
32+ csrwr t0, LOONGARCH_CSR_EUEN
33+
34+ /* We might not get launched at the address the kernel is linked to,
35+ so we jump there. */
36+ la.abs t0, 0f
37+ jirl zero, t0, 0
38+0:
39+ la t0, __bss_start # clear .bss
40+ st.d zero, t0, 0
41+ la t1, __bss_stop - LONGSIZE
42+1:
43+ addi.d t0, t0, LONGSIZE
44+ st.d zero, t0, 0
45+ bne t0, t1, 1b
46+
47+ la t0, fw_arg0
48+ st.d a0, t0, 0 # firmware arguments
49+ la t0, fw_arg1
50+ st.d a1, t0, 0
51+
52+ /* KSave3 used for percpu base, initialized as 0 */
53+ csrwr zero, PERCPU_BASE_KS
54+ /* GPR21 used for percpu base (runtime), initialized as 0 */
55+ or u0, zero, zero
56+
57+ la tp, init_thread_union
58+ /* Set the SP after an empty pt_regs. */
59+ PTR_LI sp, (_THREAD_SIZE - 32 - PT_SIZE)
60+ PTR_ADD sp, sp, tp
61+ set_saved_sp sp, t0, t1
62+ PTR_ADDI sp, sp, -4 * SZREG # init stack pointer
63+
64+ bl start_kernel
65+
66+SYM_CODE_END(kernel_entry)
67+
68+SYM_ENTRY(kernel_entry_end, SYM_L_GLOBAL, SYM_A_NONE)
arch/loongarch/kernel/mem.c+64−0
@@ -0,0 +1,64 @@
1+// SPDX-License-Identifier: GPL-2.0-or-later
2+/*
3+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+ */
5+#include <linux/efi.h>
6+#include <linux/initrd.h>
7+#include <linux/memblock.h>
8+
9+#include <asm/bootinfo.h>
10+#include <asm/loongson.h>
11+#include <asm/sections.h>
12+
13+void __init memblock_init(void)
14+{
15+ u32 mem_type;
16+ u64 mem_start, mem_end, mem_size;
17+ efi_memory_desc_t *md;
18+
19+ /* Parse memory information */
20+ for_each_efi_memory_desc(md) {
21+ mem_type = md->type;
22+ mem_start = md->phys_addr;
23+ mem_size = md->num_pages << EFI_PAGE_SHIFT;
24+ mem_end = mem_start + mem_size;
25+
26+ switch (mem_type) {
27+ case EFI_LOADER_CODE:
28+ case EFI_LOADER_DATA:
29+ case EFI_BOOT_SERVICES_CODE:
30+ case EFI_BOOT_SERVICES_DATA:
31+ case EFI_PERSISTENT_MEMORY:
32+ case EFI_CONVENTIONAL_MEMORY:
33+ memblock_add(mem_start, mem_size);
34+ if (max_low_pfn < (mem_end >> PAGE_SHIFT))
35+ max_low_pfn = mem_end >> PAGE_SHIFT;
36+ break;
37+ case EFI_PAL_CODE:
38+ case EFI_UNUSABLE_MEMORY:
39+ case EFI_ACPI_RECLAIM_MEMORY:
40+ memblock_add(mem_start, mem_size);
41+ fallthrough;
42+ case EFI_RESERVED_TYPE:
43+ case EFI_RUNTIME_SERVICES_CODE:
44+ case EFI_RUNTIME_SERVICES_DATA:
45+ case EFI_MEMORY_MAPPED_IO:
46+ case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
47+ memblock_reserve(mem_start, mem_size);
48+ break;
49+ }
50+ }
51+
52+ memblock_set_current_limit(PFN_PHYS(max_low_pfn));
53+ memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
54+
55+ /* Reserve the first 2MB */
56+ memblock_reserve(PHYS_OFFSET, 0x200000);
57+
58+ /* Reserve the kernel text/data/bss */
59+ memblock_reserve(__pa_symbol(&_text),
60+ __pa_symbol(&_end) - __pa_symbol(&_text));
61+
62+ /* Reserve the initrd */
63+ reserve_initrd_mem();
64+}
arch/loongarch/kernel/reset.c+90−0
@@ -0,0 +1,90 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4+ */
5+#include <linux/kernel.h>
6+#include <linux/acpi.h>
7+#include <linux/efi.h>
8+#include <linux/export.h>
9+#include <linux/pm.h>
10+#include <linux/types.h>
11+#include <linux/reboot.h>
12+#include <linux/delay.h>
13+#include <linux/console.h>
14+
15+#include <acpi/reboot.h>
16+#include <asm/compiler.h>
17+#include <asm/idle.h>
18+#include <asm/loongarch.h>
19+#include <asm/reboot.h>
20+
21+static void default_halt(void)
22+{
23+ local_irq_disable();
24+ clear_csr_ecfg(ECFG0_IM);
25+
26+ pr_notice("\n\n** You can safely turn off the power now **\n\n");
27+ console_flush_on_panic(CONSOLE_FLUSH_PENDING);
28+
29+ while (true) {
30+ __arch_cpu_idle();
31+ }
32+}
33+
34+static void default_poweroff(void)
35+{
36+#ifdef CONFIG_EFI
37+ efi.reset_system(EFI_RESET_SHUTDOWN, EFI_SUCCESS, 0, NULL);
38+#endif
39+ while (true) {
40+ __arch_cpu_idle();
41+ }
42+}
43+
44+static void default_restart(void)
45+{
46+#ifdef CONFIG_EFI
47+ if (efi_capsule_pending(NULL))
48+ efi_reboot(REBOOT_WARM, NULL);
49+ else
50+ efi_reboot(REBOOT_COLD, NULL);
51+#endif
52+ if (!acpi_disabled)
53+ acpi_reboot();
54+
55+ while (true) {
56+ __arch_cpu_idle();
57+ }
58+}
59+
60+void (*pm_restart)(void);
61+EXPORT_SYMBOL(pm_restart);
62+
63+void (*pm_power_off)(void);
64+EXPORT_SYMBOL(pm_power_off);
65+
66+void machine_halt(void)
67+{
68+ default_halt();
69+}
70+
71+void machine_power_off(void)
72+{
73+ pm_power_off();
74+}
75+
76+void machine_restart(char *command)
77+{
78+ do_kernel_restart(command);
79+ pm_restart();
80+}
81+
82+static int __init loongarch_reboot_setup(void)
83+{
84+ pm_restart = default_restart;
85+ pm_power_off = default_poweroff;
86+
87+ return 0;
88+}
89+
90+arch_initcall(loongarch_reboot_setup);
arch/loongarch/kernel/setup.c+341−0
@@ -0,0 +1,341 @@
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/pgalloc.h>
39+#include <asm/sections.h>
40+#include <asm/setup.h>
41+#include <asm/time.h>
42+
43+#define SMBIOS_BIOSSIZE_OFFSET 0x09
44+#define SMBIOS_BIOSEXTERN_OFFSET 0x13
45+#define SMBIOS_FREQLOW_OFFSET 0x16
46+#define SMBIOS_FREQHIGH_OFFSET 0x17
47+#define SMBIOS_FREQLOW_MASK 0xFF
48+#define SMBIOS_CORE_PACKAGE_OFFSET 0x23
49+#define LOONGSON_EFI_ENABLE (1 << 3)
50+
51+#ifdef CONFIG_VT
52+struct screen_info screen_info;
53+#endif
54+
55+unsigned long fw_arg0, fw_arg1;
56+DEFINE_PER_CPU(unsigned long, kernelsp);
57+struct cpuinfo_loongarch cpu_data[NR_CPUS] __read_mostly;
58+
59+EXPORT_SYMBOL(cpu_data);
60+
61+struct loongson_board_info b_info;
62+static const char dmi_empty_string[] = " ";
63+
64+/*
65+ * Setup information
66+ *
67+ * These are initialized so they are in the .data section
68+ */
69+
70+static int num_standard_resources;
71+static struct resource *standard_resources;
72+
73+static struct resource code_resource = { .name = "Kernel code", };
74+static struct resource data_resource = { .name = "Kernel data", };
75+static struct resource bss_resource = { .name = "Kernel bss", };
76+
77+const char *get_system_type(void)
78+{
79+ return "generic-loongson-machine";
80+}
81+
82+static const char *dmi_string_parse(const struct dmi_header *dm, u8 s)
83+{
84+ const u8 *bp = ((u8 *) dm) + dm->length;
85+
86+ if (s) {
87+ s--;
88+ while (s > 0 && *bp) {
89+ bp += strlen(bp) + 1;
90+ s--;
91+ }
92+
93+ if (*bp != 0) {
94+ size_t len = strlen(bp)+1;
95+ size_t cmp_len = len > 8 ? 8 : len;
96+
97+ if (!memcmp(bp, dmi_empty_string, cmp_len))
98+ return dmi_empty_string;
99+
100+ return bp;
101+ }
102+ }
103+
104+ return "";
105+}
106+
107+static void __init parse_cpu_table(const struct dmi_header *dm)
108+{
109+ long freq_temp = 0;
110+ char *dmi_data = (char *)dm;
111+
112+ freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
113+ ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
114+ cpu_clock_freq = freq_temp * 1000000;
115+
116+ loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
117+ loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_CORE_PACKAGE_OFFSET);
118+
119+ pr_info("CpuClock = %llu\n", cpu_clock_freq);
120+}
121+
122+static void __init parse_bios_table(const struct dmi_header *dm)
123+{
124+ int bios_extern;
125+ char *dmi_data = (char *)dm;
126+
127+ bios_extern = *(dmi_data + SMBIOS_BIOSEXTERN_OFFSET);
128+ b_info.bios_size = *(dmi_data + SMBIOS_BIOSSIZE_OFFSET);
129+
130+ if (bios_extern & LOONGSON_EFI_ENABLE)
131+ set_bit(EFI_BOOT, &efi.flags);
132+ else
133+ clear_bit(EFI_BOOT, &efi.flags);
134+}
135+
136+static void __init find_tokens(const struct dmi_header *dm, void *dummy)
137+{
138+ switch (dm->type) {
139+ case 0x0: /* Extern BIOS */
140+ parse_bios_table(dm);
141+ break;
142+ case 0x4: /* Calling interface */
143+ parse_cpu_table(dm);
144+ break;
145+ }
146+}
147+static void __init smbios_parse(void)
148+{
149+ b_info.bios_vendor = (void *)dmi_get_system_info(DMI_BIOS_VENDOR);
150+ b_info.bios_version = (void *)dmi_get_system_info(DMI_BIOS_VERSION);
151+ b_info.bios_release_date = (void *)dmi_get_system_info(DMI_BIOS_DATE);
152+ b_info.board_vendor = (void *)dmi_get_system_info(DMI_BOARD_VENDOR);
153+ b_info.board_name = (void *)dmi_get_system_info(DMI_BOARD_NAME);
154+ dmi_walk(find_tokens, NULL);
155+}
156+
157+static int usermem __initdata;
158+
159+static int __init early_parse_mem(char *p)
160+{
161+ phys_addr_t start, size;
162+
163+ if (!p) {
164+ pr_err("mem parameter is empty, do nothing\n");
165+ return -EINVAL;
166+ }
167+
168+ /*
169+ * If a user specifies memory size, we
170+ * blow away any automatically generated
171+ * size.
172+ */
173+ if (usermem == 0) {
174+ usermem = 1;
175+ memblock_remove(memblock_start_of_DRAM(),
176+ memblock_end_of_DRAM() - memblock_start_of_DRAM());
177+ }
178+ start = 0;
179+ size = memparse(p, &p);
180+ if (*p == '@')
181+ start = memparse(p + 1, &p);
182+ else {
183+ pr_err("Invalid format!\n");
184+ return -EINVAL;
185+ }
186+
187+ memblock_add(start, size);
188+
189+ return 0;
190+}
191+early_param("mem", early_parse_mem);
192+
193+void __init platform_init(void)
194+{
195+ efi_init();
196+#ifdef CONFIG_ACPI_TABLE_UPGRADE
197+ acpi_table_upgrade();
198+#endif
199+#ifdef CONFIG_ACPI
200+ acpi_gbl_use_default_register_widths = false;
201+ acpi_boot_table_init();
202+ acpi_boot_init();
203+#endif
204+
205+ dmi_setup();
206+ smbios_parse();
207+ pr_info("The BIOS Version: %s\n", b_info.bios_version);
208+
209+ efi_runtime_init();
210+}
211+
212+static void __init check_kernel_sections_mem(void)
213+{
214+ phys_addr_t start = __pa_symbol(&_text);
215+ phys_addr_t size = __pa_symbol(&_end) - start;
216+
217+ if (!memblock_is_region_memory(start, size)) {
218+ pr_info("Kernel sections are not in the memory maps\n");
219+ memblock_add(start, size);
220+ }
221+}
222+
223+/*
224+ * arch_mem_init - initialize memory management subsystem
225+ */
226+static void __init arch_mem_init(char **cmdline_p)
227+{
228+ if (usermem)
229+ pr_info("User-defined physical RAM map overwrite\n");
230+
231+ check_kernel_sections_mem();
232+
233+ /*
234+ * In order to reduce the possibility of kernel panic when failed to
235+ * get IO TLB memory under CONFIG_SWIOTLB, it is better to allocate
236+ * low memory as small as possible before plat_swiotlb_setup(), so
237+ * make sparse_init() using top-down allocation.
238+ */
239+ memblock_set_bottom_up(false);
240+ sparse_init();
241+ memblock_set_bottom_up(true);
242+
243+ swiotlb_init(true, SWIOTLB_VERBOSE);
244+
245+ dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
246+
247+ memblock_dump_all();
248+
249+ early_memtest(PFN_PHYS(ARCH_PFN_OFFSET), PFN_PHYS(max_low_pfn));
250+}
251+
252+static void __init resource_init(void)
253+{
254+ long i = 0;
255+ size_t res_size;
256+ struct resource *res;
257+ struct memblock_region *region;
258+
259+ code_resource.start = __pa_symbol(&_text);
260+ code_resource.end = __pa_symbol(&_etext) - 1;
261+ data_resource.start = __pa_symbol(&_etext);
262+ data_resource.end = __pa_symbol(&_edata) - 1;
263+ bss_resource.start = __pa_symbol(&__bss_start);
264+ bss_resource.end = __pa_symbol(&__bss_stop) - 1;
265+
266+ num_standard_resources = memblock.memory.cnt;
267+ res_size = num_standard_resources * sizeof(*standard_resources);
268+ standard_resources = memblock_alloc(res_size, SMP_CACHE_BYTES);
269+
270+ for_each_mem_region(region) {
271+ res = &standard_resources[i++];
272+ if (!memblock_is_nomap(region)) {
273+ res->name = "System RAM";
274+ res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
275+ res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
276+ res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
277+ } else {
278+ res->name = "Reserved";
279+ res->flags = IORESOURCE_MEM;
280+ res->start = __pfn_to_phys(memblock_region_reserved_base_pfn(region));
281+ res->end = __pfn_to_phys(memblock_region_reserved_end_pfn(region)) - 1;
282+ }
283+
284+ request_resource(&iomem_resource, res);
285+
286+ /*
287+ * We don't know which RAM region contains kernel data,
288+ * so we try it repeatedly and let the resource manager
289+ * test it.
290+ */
291+ request_resource(res, &code_resource);
292+ request_resource(res, &data_resource);
293+ request_resource(res, &bss_resource);
294+ }
295+}
296+
297+static int __init reserve_memblock_reserved_regions(void)
298+{
299+ u64 i, j;
300+
301+ for (i = 0; i < num_standard_resources; ++i) {
302+ struct resource *mem = &standard_resources[i];
303+ phys_addr_t r_start, r_end, mem_size = resource_size(mem);
304+
305+ if (!memblock_is_region_reserved(mem->start, mem_size))
306+ continue;
307+
308+ for_each_reserved_mem_range(j, &r_start, &r_end) {
309+ resource_size_t start, end;
310+
311+ start = max(PFN_PHYS(PFN_DOWN(r_start)), mem->start);
312+ end = min(PFN_PHYS(PFN_UP(r_end)) - 1, mem->end);
313+
314+ if (start > mem->end || end < mem->start)
315+ continue;
316+
317+ reserve_region_with_split(mem, start, end, "Reserved");
318+ }
319+ }
320+
321+ return 0;
322+}
323+arch_initcall(reserve_memblock_reserved_regions);
324+
325+void __init setup_arch(char **cmdline_p)
326+{
327+ cpu_probe();
328+ *cmdline_p = boot_command_line;
329+
330+ init_environ();
331+ memblock_init();
332+ parse_early_param();
333+
334+ platform_init();
335+ pagetable_init();
336+ arch_mem_init(cmdline_p);
337+
338+ resource_init();
339+
340+ paging_init();
341+}
arch/loongarch/kernel/time.c+213−0
@@ -0,0 +1,213 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * Common time service routines for LoongArch machines.
4+ *
5+ * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6+ */
7+#include <linux/clockchips.h>
8+#include <linux/delay.h>
9+#include <linux/export.h>
10+#include <linux/init.h>
11+#include <linux/interrupt.h>
12+#include <linux/kernel.h>
13+#include <linux/sched_clock.h>
14+#include <linux/spinlock.h>
15+
16+#include <asm/cpu-features.h>
17+#include <asm/loongarch.h>
18+#include <asm/time.h>
19+
20+u64 cpu_clock_freq;
21+EXPORT_SYMBOL(cpu_clock_freq);
22+u64 const_clock_freq;
23+EXPORT_SYMBOL(const_clock_freq);
24+
25+static DEFINE_RAW_SPINLOCK(state_lock);
26+static DEFINE_PER_CPU(struct clock_event_device, constant_clockevent_device);
27+
28+static void constant_event_handler(struct clock_event_device *dev)
29+{
30+}
31+
32+irqreturn_t constant_timer_interrupt(int irq, void *data)
33+{
34+ int cpu = smp_processor_id();
35+ struct clock_event_device *cd;
36+
37+ /* Clear Timer Interrupt */
38+ write_csr_tintclear(CSR_TINTCLR_TI);
39+ cd = &per_cpu(constant_clockevent_device, cpu);
40+ cd->event_handler(cd);
41+
42+ return IRQ_HANDLED;
43+}
44+
45+static int constant_set_state_oneshot(struct clock_event_device *evt)
46+{
47+ unsigned long timer_config;
48+
49+ raw_spin_lock(&state_lock);
50+
51+ timer_config = csr_read64(LOONGARCH_CSR_TCFG);
52+ timer_config |= CSR_TCFG_EN;
53+ timer_config &= ~CSR_TCFG_PERIOD;
54+ csr_write64(timer_config, LOONGARCH_CSR_TCFG);
55+
56+ raw_spin_unlock(&state_lock);
57+
58+ return 0;
59+}
60+
61+static int constant_set_state_oneshot_stopped(struct clock_event_device *evt)
62+{
63+ unsigned long timer_config;
64+
65+ raw_spin_lock(&state_lock);
66+
67+ timer_config = csr_read64(LOONGARCH_CSR_TCFG);
68+ timer_config &= ~CSR_TCFG_EN;
69+ csr_write64(timer_config, LOONGARCH_CSR_TCFG);
70+
71+ raw_spin_unlock(&state_lock);
72+
73+ return 0;
74+}
75+
76+static int constant_set_state_periodic(struct clock_event_device *evt)
77+{
78+ unsigned long period;
79+ unsigned long timer_config;
80+
81+ raw_spin_lock(&state_lock);
82+
83+ period = const_clock_freq / HZ;
84+ timer_config = period & CSR_TCFG_VAL;
85+ timer_config |= (CSR_TCFG_PERIOD | CSR_TCFG_EN);
86+ csr_write64(timer_config, LOONGARCH_CSR_TCFG);
87+
88+ raw_spin_unlock(&state_lock);
89+
90+ return 0;
91+}
92+
93+static int constant_set_state_shutdown(struct clock_event_device *evt)
94+{
95+ return 0;
96+}
97+
98+static int constant_timer_next_event(unsigned long delta, struct clock_event_device *evt)
99+{
100+ unsigned long timer_config;
101+
102+ delta &= CSR_TCFG_VAL;
103+ timer_config = delta | CSR_TCFG_EN;
104+ csr_write64(timer_config, LOONGARCH_CSR_TCFG);
105+
106+ return 0;
107+}
108+
109+static unsigned long __init get_loops_per_jiffy(void)
110+{
111+ unsigned long lpj = (unsigned long)const_clock_freq;
112+
113+ do_div(lpj, HZ);
114+
115+ return lpj;
116+}
117+
118+static long init_timeval;
119+
120+void sync_counter(void)
121+{
122+ /* Ensure counter begin at 0 */
123+ csr_write64(-init_timeval, LOONGARCH_CSR_CNTC);
124+}
125+
126+int constant_clockevent_init(void)
127+{
128+ unsigned int irq;
129+ unsigned int cpu = smp_processor_id();
130+ unsigned long min_delta = 0x600;
131+ unsigned long max_delta = (1UL << 48) - 1;
132+ struct clock_event_device *cd;
133+ static int timer_irq_installed = 0;
134+
135+ irq = EXCCODE_TIMER - EXCCODE_INT_START;
136+
137+ cd = &per_cpu(constant_clockevent_device, cpu);
138+
139+ cd->name = "Constant";
140+ cd->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_PERCPU;
141+
142+ cd->irq = irq;
143+ cd->rating = 320;
144+ cd->cpumask = cpumask_of(cpu);
145+ cd->set_state_oneshot = constant_set_state_oneshot;
146+ cd->set_state_oneshot_stopped = constant_set_state_oneshot_stopped;
147+ cd->set_state_periodic = constant_set_state_periodic;
148+ cd->set_state_shutdown = constant_set_state_shutdown;
149+ cd->set_next_event = constant_timer_next_event;
150+ cd->event_handler = constant_event_handler;
151+
152+ clockevents_config_and_register(cd, const_clock_freq, min_delta, max_delta);
153+
154+ if (timer_irq_installed)
155+ return 0;
156+
157+ timer_irq_installed = 1;
158+
159+ sync_counter();
160+
161+ if (request_irq(irq, constant_timer_interrupt, IRQF_PERCPU | IRQF_TIMER, "timer", NULL))
162+ pr_err("Failed to request irq %d (timer)\n", irq);
163+
164+ lpj_fine = get_loops_per_jiffy();
165+ pr_info("Constant clock event device register\n");
166+
167+ return 0;
168+}
169+
170+static u64 read_const_counter(struct clocksource *clk)
171+{
172+ return drdtime();
173+}
174+
175+static u64 native_sched_clock(void)
176+{
177+ return read_const_counter(NULL);
178+}
179+
180+static struct clocksource clocksource_const = {
181+ .name = "Constant",
182+ .rating = 400,
183+ .read = read_const_counter,
184+ .mask = CLOCKSOURCE_MASK(64),
185+ .flags = CLOCK_SOURCE_IS_CONTINUOUS,
186+};
187+
188+int __init constant_clocksource_init(void)
189+{
190+ int res;
191+ unsigned long freq = const_clock_freq;
192+
193+ res = clocksource_register_hz(&clocksource_const, freq);
194+
195+ sched_clock_register(native_sched_clock, 64, freq);
196+
197+ pr_info("Constant clock source device register\n");
198+
199+ return res;
200+}
201+
202+void __init time_init(void)
203+{
204+ if (!cpu_has_cpucfg)
205+ const_clock_freq = cpu_clock_freq;
206+ else
207+ const_clock_freq = calc_const_freq();
208+
209+ init_timeval = drdtime() - csr_read64(LOONGARCH_CSR_CNTC);
210+
211+ constant_clockevent_init();
212+ constant_clocksource_init();
213+}
arch/loongarch/kernel/topology.c+13−0
@@ -0,0 +1,13 @@
1+// SPDX-License-Identifier: GPL-2.0
2+#include <linux/cpu.h>
3+#include <linux/init.h>
4+#include <linux/percpu.h>
5+
6+static struct cpu cpu_device;
7+
8+static int __init topology_init(void)
9+{
10+ return register_cpu(&cpu_device, 0);
11+}
12+
13+subsys_initcall(topology_init);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t