Bin Meng | 055700e | 2018-09-26 06:55:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com> |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Bin Meng | 7a3bbfb | 2018-12-12 06:12:34 -0800 | [diff] [blame] | 7 | #include <cpu.h> |
Bin Meng | edfe9a9 | 2018-12-12 06:12:38 -0800 | [diff] [blame] | 8 | #include <dm.h> |
Heinrich Schuchardt | cc382ff | 2021-09-12 21:11:46 +0200 | [diff] [blame] | 9 | #include <dm/lists.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 10 | #include <init.h> |
Bin Meng | 7a3bbfb | 2018-12-12 06:12:34 -0800 | [diff] [blame] | 11 | #include <log.h> |
Bin Meng | a7544ed | 2018-12-12 06:12:40 -0800 | [diff] [blame] | 12 | #include <asm/encoding.h> |
Bin Meng | edfe9a9 | 2018-12-12 06:12:38 -0800 | [diff] [blame] | 13 | #include <dm/uclass-internal.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Bin Meng | 055700e | 2018-09-26 06:55:14 -0700 | [diff] [blame] | 15 | |
Lukas Auer | 39a652b | 2018-11-22 11:26:29 +0100 | [diff] [blame] | 16 | /* |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 17 | * The variables here must be stored in the data section since they are used |
Lukas Auer | 39a652b | 2018-11-22 11:26:29 +0100 | [diff] [blame] | 18 | * before the bss section is available. |
| 19 | */ |
Rick Chen | e5e6c36 | 2019-04-30 13:49:33 +0800 | [diff] [blame] | 20 | #ifndef CONFIG_XIP |
Marek BehĂșn | 4bebdd3 | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 21 | u32 hart_lottery __section(".data") = 0; |
Lukas Auer | a359665 | 2019-03-17 19:28:37 +0100 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * The main hart running U-Boot has acquired available_harts_lock until it has |
| 25 | * finished initialization of global data. |
| 26 | */ |
| 27 | u32 available_harts_lock = 1; |
Rick Chen | e5e6c36 | 2019-04-30 13:49:33 +0800 | [diff] [blame] | 28 | #endif |
Lukas Auer | 39a652b | 2018-11-22 11:26:29 +0100 | [diff] [blame] | 29 | |
Bin Meng | 055700e | 2018-09-26 06:55:14 -0700 | [diff] [blame] | 30 | static inline bool supports_extension(char ext) |
| 31 | { |
Bin Meng | edfe9a9 | 2018-12-12 06:12:38 -0800 | [diff] [blame] | 32 | #ifdef CONFIG_CPU |
| 33 | struct udevice *dev; |
| 34 | char desc[32]; |
| 35 | |
| 36 | uclass_find_first_device(UCLASS_CPU, &dev); |
| 37 | if (!dev) { |
| 38 | debug("unable to find the RISC-V cpu device\n"); |
| 39 | return false; |
| 40 | } |
| 41 | if (!cpu_get_desc(dev, desc, sizeof(desc))) { |
| 42 | /* skip the first 4 characters (rv32|rv64) */ |
| 43 | if (strchr(desc + 4, ext)) |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | return false; |
| 48 | #else /* !CONFIG_CPU */ |
Lukas Auer | 6134659 | 2019-08-21 21:14:43 +0200 | [diff] [blame] | 49 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
Bin Meng | f942636 | 2019-07-10 23:43:13 -0700 | [diff] [blame] | 50 | return csr_read(CSR_MISA) & (1 << (ext - 'a')); |
Lukas Auer | 6134659 | 2019-08-21 21:14:43 +0200 | [diff] [blame] | 51 | #else /* !CONFIG_IS_ENABLED(RISCV_MMODE) */ |
Bin Meng | edfe9a9 | 2018-12-12 06:12:38 -0800 | [diff] [blame] | 52 | #warning "There is no way to determine the available extensions in S-mode." |
| 53 | #warning "Please convert your board to use the RISC-V CPU driver." |
| 54 | return false; |
Lukas Auer | 6134659 | 2019-08-21 21:14:43 +0200 | [diff] [blame] | 55 | #endif /* CONFIG_IS_ENABLED(RISCV_MMODE) */ |
Bin Meng | edfe9a9 | 2018-12-12 06:12:38 -0800 | [diff] [blame] | 56 | #endif /* CONFIG_CPU */ |
Bin Meng | 055700e | 2018-09-26 06:55:14 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Bin Meng | 7a3bbfb | 2018-12-12 06:12:34 -0800 | [diff] [blame] | 59 | static int riscv_cpu_probe(void) |
| 60 | { |
| 61 | #ifdef CONFIG_CPU |
| 62 | int ret; |
| 63 | |
| 64 | /* probe cpus so that RISC-V timer can be bound */ |
| 65 | ret = cpu_probe_all(); |
| 66 | if (ret) |
| 67 | return log_msg_ret("RISC-V cpus probe failed\n", ret); |
| 68 | #endif |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Sean Anderson | dd1cd70 | 2020-09-21 07:51:38 -0400 | [diff] [blame] | 73 | /* |
| 74 | * This is called on secondary harts just after the IPI is init'd. Currently |
| 75 | * there's nothing to do, since we just need to clear any existing IPIs, and |
| 76 | * that is handled by the sending of an ipi itself. |
| 77 | */ |
| 78 | #if CONFIG_IS_ENABLED(SMP) |
| 79 | static void dummy_pending_ipi_clear(ulong hart, ulong arg0, ulong arg1) |
| 80 | { |
| 81 | } |
| 82 | #endif |
| 83 | |
Bin Meng | 7a3bbfb | 2018-12-12 06:12:34 -0800 | [diff] [blame] | 84 | int arch_cpu_init_dm(void) |
| 85 | { |
Bin Meng | a7544ed | 2018-12-12 06:12:40 -0800 | [diff] [blame] | 86 | int ret; |
| 87 | |
| 88 | ret = riscv_cpu_probe(); |
| 89 | if (ret) |
| 90 | return ret; |
| 91 | |
| 92 | /* Enable FPU */ |
| 93 | if (supports_extension('d') || supports_extension('f')) { |
| 94 | csr_set(MODE_PREFIX(status), MSTATUS_FS); |
Bin Meng | f942636 | 2019-07-10 23:43:13 -0700 | [diff] [blame] | 95 | csr_write(CSR_FCSR, 0); |
Bin Meng | a7544ed | 2018-12-12 06:12:40 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | if (CONFIG_IS_ENABLED(RISCV_MMODE)) { |
| 99 | /* |
| 100 | * Enable perf counters for cycle, time, |
| 101 | * and instret counters only |
| 102 | */ |
Sean Anderson | 7f4b666 | 2020-06-24 06:41:19 -0400 | [diff] [blame] | 103 | #ifdef CONFIG_RISCV_PRIV_1_9 |
| 104 | csr_write(CSR_MSCOUNTEREN, GENMASK(2, 0)); |
| 105 | csr_write(CSR_MUCOUNTEREN, GENMASK(2, 0)); |
| 106 | #else |
Bin Meng | f942636 | 2019-07-10 23:43:13 -0700 | [diff] [blame] | 107 | csr_write(CSR_MCOUNTEREN, GENMASK(2, 0)); |
Sean Anderson | 7f4b666 | 2020-06-24 06:41:19 -0400 | [diff] [blame] | 108 | #endif |
Bin Meng | a7544ed | 2018-12-12 06:12:40 -0800 | [diff] [blame] | 109 | |
| 110 | /* Disable paging */ |
| 111 | if (supports_extension('s')) |
Sean Anderson | 7f4b666 | 2020-06-24 06:41:19 -0400 | [diff] [blame] | 112 | #ifdef CONFIG_RISCV_PRIV_1_9 |
| 113 | csr_read_clear(CSR_MSTATUS, SR_VM); |
| 114 | #else |
Bin Meng | f942636 | 2019-07-10 23:43:13 -0700 | [diff] [blame] | 115 | csr_write(CSR_SATP, 0); |
Sean Anderson | 7f4b666 | 2020-06-24 06:41:19 -0400 | [diff] [blame] | 116 | #endif |
Bin Meng | a7544ed | 2018-12-12 06:12:40 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Bin Meng | 257875d | 2020-07-19 23:17:07 -0700 | [diff] [blame] | 119 | #if CONFIG_IS_ENABLED(SMP) |
Sean Anderson | b1d0cb3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 120 | ret = riscv_init_ipi(); |
| 121 | if (ret) |
| 122 | return ret; |
Sean Anderson | dd1cd70 | 2020-09-21 07:51:38 -0400 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * Clear all pending IPIs on secondary harts. We don't do anything on |
| 126 | * the boot hart, since we never send an IPI to ourselves, and no |
| 127 | * interrupts are enabled |
| 128 | */ |
| 129 | ret = smp_call_function((ulong)dummy_pending_ipi_clear, 0, 0, 0); |
| 130 | if (ret) |
| 131 | return ret; |
Sean Anderson | b1d0cb3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 132 | #endif |
| 133 | |
Bin Meng | a7544ed | 2018-12-12 06:12:40 -0800 | [diff] [blame] | 134 | return 0; |
Bin Meng | 7a3bbfb | 2018-12-12 06:12:34 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int arch_early_init_r(void) |
| 138 | { |
Heinrich Schuchardt | cc382ff | 2021-09-12 21:11:46 +0200 | [diff] [blame] | 139 | int ret; |
| 140 | |
| 141 | ret = riscv_cpu_probe(); |
| 142 | if (ret) |
| 143 | return ret; |
| 144 | |
| 145 | if (IS_ENABLED(CONFIG_SYSRESET_SBI)) |
| 146 | device_bind_driver(gd->dm_root, "sbi-sysreset", |
| 147 | "sbi-sysreset", NULL); |
| 148 | |
| 149 | return 0; |
Bin Meng | 7a3bbfb | 2018-12-12 06:12:34 -0800 | [diff] [blame] | 150 | } |
Green Wan | 2612080 | 2021-05-02 23:23:04 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * harts_early_init() - A callback function called by start.S to configure |
| 154 | * feature settings of each hart. |
| 155 | * |
| 156 | * In a multi-core system, memory access shall be careful here, it shall |
| 157 | * take care of race conditions. |
| 158 | */ |
| 159 | __weak void harts_early_init(void) |
| 160 | { |
| 161 | } |