1 // SPDX-License-Identifier: GPL-2.0-only
3 * SBI initialilization and all extension implementation.
5 * Copyright (c) 2020 Western Digital Corporation or its affiliates.
8 #include <linux/bits.h>
9 #include <linux/init.h>
12 #include <linux/reboot.h>
15 #include <asm/tlbflush.h>
17 /* default SBI version is 0.1 */
18 unsigned long sbi_spec_version __ro_after_init = SBI_SPEC_VERSION_DEFAULT;
19 EXPORT_SYMBOL(sbi_spec_version);
21 static void (*__sbi_set_timer)(uint64_t stime) __ro_after_init;
22 static void (*__sbi_send_ipi)(unsigned int cpu) __ro_after_init;
23 static int (*__sbi_rfence)(int fid, const struct cpumask *cpu_mask,
24 unsigned long start, unsigned long size,
25 unsigned long arg4, unsigned long arg5) __ro_after_init;
27 #ifdef CONFIG_RISCV_SBI_V01
28 static unsigned long __sbi_v01_cpumask_to_hartmask(const struct cpumask *cpu_mask)
30 unsigned long cpuid, hartid;
31 unsigned long hmask = 0;
34 * There is no maximum hartid concept in RISC-V and NR_CPUS must not be
35 * associated with hartid. As SBI v0.1 is only kept for backward compatibility
36 * and will be removed in the future, there is no point in supporting hartid
37 * greater than BITS_PER_LONG (32 for RV32 and 64 for RV64). Ideally, SBI v0.2
38 * should be used for platforms with hartid greater than BITS_PER_LONG.
40 for_each_cpu(cpuid, cpu_mask) {
41 hartid = cpuid_to_hartid_map(cpuid);
42 if (hartid >= BITS_PER_LONG) {
43 pr_warn("Unable to send any request to hartid > BITS_PER_LONG for SBI v0.1\n");
53 * sbi_console_putchar() - Writes given character to the console device.
54 * @ch: The data to be written to the console.
58 void sbi_console_putchar(int ch)
60 sbi_ecall(SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, ch, 0, 0, 0, 0, 0);
62 EXPORT_SYMBOL(sbi_console_putchar);
65 * sbi_console_getchar() - Reads a byte from console device.
67 * Returns the value read from console.
69 int sbi_console_getchar(void)
73 ret = sbi_ecall(SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 0, 0, 0, 0, 0, 0);
77 EXPORT_SYMBOL(sbi_console_getchar);
80 * sbi_shutdown() - Remove all the harts from executing supervisor code.
84 void sbi_shutdown(void)
86 sbi_ecall(SBI_EXT_0_1_SHUTDOWN, 0, 0, 0, 0, 0, 0, 0);
88 EXPORT_SYMBOL(sbi_shutdown);
91 * __sbi_set_timer_v01() - Program the timer for next timer event.
92 * @stime_value: The value after which next timer event should fire.
96 static void __sbi_set_timer_v01(uint64_t stime_value)
98 #if __riscv_xlen == 32
99 sbi_ecall(SBI_EXT_0_1_SET_TIMER, 0, stime_value,
100 stime_value >> 32, 0, 0, 0, 0);
102 sbi_ecall(SBI_EXT_0_1_SET_TIMER, 0, stime_value, 0, 0, 0, 0, 0);
106 static void __sbi_send_ipi_v01(unsigned int cpu)
108 unsigned long hart_mask =
109 __sbi_v01_cpumask_to_hartmask(cpumask_of(cpu));
110 sbi_ecall(SBI_EXT_0_1_SEND_IPI, 0, (unsigned long)(&hart_mask),
114 static int __sbi_rfence_v01(int fid, const struct cpumask *cpu_mask,
115 unsigned long start, unsigned long size,
116 unsigned long arg4, unsigned long arg5)
119 unsigned long hart_mask;
121 if (!cpu_mask || cpumask_empty(cpu_mask))
122 cpu_mask = cpu_online_mask;
123 hart_mask = __sbi_v01_cpumask_to_hartmask(cpu_mask);
125 /* v0.2 function IDs are equivalent to v0.1 extension IDs */
127 case SBI_EXT_RFENCE_REMOTE_FENCE_I:
128 sbi_ecall(SBI_EXT_0_1_REMOTE_FENCE_I, 0,
129 (unsigned long)&hart_mask, 0, 0, 0, 0, 0);
131 case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA:
132 sbi_ecall(SBI_EXT_0_1_REMOTE_SFENCE_VMA, 0,
133 (unsigned long)&hart_mask, start, size,
136 case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID:
137 sbi_ecall(SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID, 0,
138 (unsigned long)&hart_mask, start, size,
142 pr_err("SBI call [%d]not supported in SBI v0.1\n", fid);
149 static void sbi_set_power_off(void)
151 pm_power_off = sbi_shutdown;
154 static void __sbi_set_timer_v01(uint64_t stime_value)
156 pr_warn("Timer extension is not available in SBI v%lu.%lu\n",
157 sbi_major_version(), sbi_minor_version());
160 static void __sbi_send_ipi_v01(unsigned int cpu)
162 pr_warn("IPI extension is not available in SBI v%lu.%lu\n",
163 sbi_major_version(), sbi_minor_version());
166 static int __sbi_rfence_v01(int fid, const struct cpumask *cpu_mask,
167 unsigned long start, unsigned long size,
168 unsigned long arg4, unsigned long arg5)
170 pr_warn("remote fence extension is not available in SBI v%lu.%lu\n",
171 sbi_major_version(), sbi_minor_version());
176 static void sbi_set_power_off(void) {}
177 #endif /* CONFIG_RISCV_SBI_V01 */
179 static void __sbi_set_timer_v02(uint64_t stime_value)
181 #if __riscv_xlen == 32
182 sbi_ecall(SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, stime_value,
183 stime_value >> 32, 0, 0, 0, 0);
185 sbi_ecall(SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, stime_value, 0,
190 static void __sbi_send_ipi_v02(unsigned int cpu)
193 struct sbiret ret = {0};
195 ret = sbi_ecall(SBI_EXT_IPI, SBI_EXT_IPI_SEND_IPI,
196 1UL, cpuid_to_hartid_map(cpu), 0, 0, 0, 0);
198 result = sbi_err_map_linux_errno(ret.error);
199 pr_err("%s: hbase = [%lu] failed (error [%d])\n",
200 __func__, cpuid_to_hartid_map(cpu), result);
204 static int __sbi_rfence_v02_call(unsigned long fid, unsigned long hmask,
205 unsigned long hbase, unsigned long start,
206 unsigned long size, unsigned long arg4,
209 struct sbiret ret = {0};
210 int ext = SBI_EXT_RFENCE;
214 case SBI_EXT_RFENCE_REMOTE_FENCE_I:
215 ret = sbi_ecall(ext, fid, hmask, hbase, 0, 0, 0, 0);
217 case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA:
218 ret = sbi_ecall(ext, fid, hmask, hbase, start,
221 case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID:
222 ret = sbi_ecall(ext, fid, hmask, hbase, start,
226 case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA:
227 ret = sbi_ecall(ext, fid, hmask, hbase, start,
230 case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID:
231 ret = sbi_ecall(ext, fid, hmask, hbase, start,
234 case SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA:
235 ret = sbi_ecall(ext, fid, hmask, hbase, start,
238 case SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID:
239 ret = sbi_ecall(ext, fid, hmask, hbase, start,
243 pr_err("unknown function ID [%lu] for SBI extension [%d]\n",
249 result = sbi_err_map_linux_errno(ret.error);
250 pr_err("%s: hbase = [%lu] hmask = [0x%lx] failed (error [%d])\n",
251 __func__, hbase, hmask, result);
257 static int __sbi_rfence_v02(int fid, const struct cpumask *cpu_mask,
258 unsigned long start, unsigned long size,
259 unsigned long arg4, unsigned long arg5)
261 unsigned long hartid, cpuid, hmask = 0, hbase = 0, htop = 0;
264 if (!cpu_mask || cpumask_empty(cpu_mask))
265 cpu_mask = cpu_online_mask;
267 for_each_cpu(cpuid, cpu_mask) {
268 hartid = cpuid_to_hartid_map(cpuid);
270 if (hartid + BITS_PER_LONG <= htop ||
271 hbase + BITS_PER_LONG <= hartid) {
272 result = __sbi_rfence_v02_call(fid, hmask,
273 hbase, start, size, arg4, arg5);
277 } else if (hartid < hbase) {
278 /* shift the mask to fit lower hartid */
279 hmask <<= hbase - hartid;
286 } else if (hartid > htop) {
289 hmask |= BIT(hartid - hbase);
293 result = __sbi_rfence_v02_call(fid, hmask, hbase,
294 start, size, arg4, arg5);
302 static bool sbi_fwft_supported;
304 struct fwft_set_req {
311 static void cpu_sbi_fwft_set(void *arg)
313 struct fwft_set_req *req = arg;
316 ret = sbi_fwft_set(req->feature, req->value, req->flags);
318 atomic_set(&req->error, ret);
322 * sbi_fwft_set() - Set a feature on the local hart
323 * @feature: The feature ID to be set
324 * @value: The feature value to be set
325 * @flags: FWFT feature set flags
327 * Return: 0 on success, appropriate linux error code otherwise.
329 int sbi_fwft_set(u32 feature, unsigned long value, unsigned long flags)
333 if (!sbi_fwft_supported)
336 ret = sbi_ecall(SBI_EXT_FWFT, SBI_EXT_FWFT_SET,
337 feature, value, flags, 0, 0, 0);
339 return sbi_err_map_linux_errno(ret.error);
343 * sbi_fwft_set_cpumask() - Set a feature for the specified cpumask
344 * @mask: CPU mask of cpus that need the feature to be set
345 * @feature: The feature ID to be set
346 * @value: The feature value to be set
347 * @flags: FWFT feature set flags
349 * Return: 0 on success, appropriate linux error code otherwise.
351 int sbi_fwft_set_cpumask(const cpumask_t *mask, u32 feature,
352 unsigned long value, unsigned long flags)
354 struct fwft_set_req req = {
358 .error = ATOMIC_INIT(0),
361 if (!sbi_fwft_supported)
364 if (feature & SBI_FWFT_GLOBAL_FEATURE_BIT)
367 on_each_cpu_mask(mask, cpu_sbi_fwft_set, &req, 1);
369 return atomic_read(&req.error);
373 * sbi_set_timer() - Program the timer for next timer event.
374 * @stime_value: The value after which next timer event should fire.
378 void sbi_set_timer(uint64_t stime_value)
380 __sbi_set_timer(stime_value);
384 * sbi_send_ipi() - Send an IPI to any hart.
385 * @cpu: Logical id of the target CPU.
387 void sbi_send_ipi(unsigned int cpu)
391 EXPORT_SYMBOL(sbi_send_ipi);
394 * sbi_remote_fence_i() - Execute FENCE.I instruction on given remote harts.
395 * @cpu_mask: A cpu mask containing all the target harts.
397 * Return: 0 on success, appropriate linux error code otherwise.
399 int sbi_remote_fence_i(const struct cpumask *cpu_mask)
401 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_FENCE_I,
402 cpu_mask, 0, 0, 0, 0);
404 EXPORT_SYMBOL(sbi_remote_fence_i);
407 * sbi_remote_sfence_vma_asid() - Execute SFENCE.VMA instructions on given
408 * remote harts for a virtual address range belonging to a specific ASID or not.
410 * @cpu_mask: A cpu mask containing all the target harts.
411 * @start: Start of the virtual address
412 * @size: Total size of the virtual address range.
413 * @asid: The value of address space identifier (ASID), or FLUSH_TLB_NO_ASID
414 * for flushing all address spaces.
416 * Return: 0 on success, appropriate linux error code otherwise.
418 int sbi_remote_sfence_vma_asid(const struct cpumask *cpu_mask,
423 if (asid == FLUSH_TLB_NO_ASID)
424 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA,
425 cpu_mask, start, size, 0, 0);
427 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID,
428 cpu_mask, start, size, asid, 0);
430 EXPORT_SYMBOL(sbi_remote_sfence_vma_asid);
433 * sbi_remote_hfence_gvma() - Execute HFENCE.GVMA instructions on given remote
434 * harts for the specified guest physical address range.
435 * @cpu_mask: A cpu mask containing all the target harts.
436 * @start: Start of the guest physical address
437 * @size: Total size of the guest physical address range.
441 int sbi_remote_hfence_gvma(const struct cpumask *cpu_mask,
445 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
446 cpu_mask, start, size, 0, 0);
448 EXPORT_SYMBOL_GPL(sbi_remote_hfence_gvma);
451 * sbi_remote_hfence_gvma_vmid() - Execute HFENCE.GVMA instructions on given
452 * remote harts for a guest physical address range belonging to a specific VMID.
454 * @cpu_mask: A cpu mask containing all the target harts.
455 * @start: Start of the guest physical address
456 * @size: Total size of the guest physical address range.
457 * @vmid: The value of guest ID (VMID).
459 * Return: 0 if success, Error otherwise.
461 int sbi_remote_hfence_gvma_vmid(const struct cpumask *cpu_mask,
466 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID,
467 cpu_mask, start, size, vmid, 0);
469 EXPORT_SYMBOL(sbi_remote_hfence_gvma_vmid);
472 * sbi_remote_hfence_vvma() - Execute HFENCE.VVMA instructions on given remote
473 * harts for the current guest virtual address range.
474 * @cpu_mask: A cpu mask containing all the target harts.
475 * @start: Start of the current guest virtual address
476 * @size: Total size of the current guest virtual address range.
480 int sbi_remote_hfence_vvma(const struct cpumask *cpu_mask,
484 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA,
485 cpu_mask, start, size, 0, 0);
487 EXPORT_SYMBOL(sbi_remote_hfence_vvma);
490 * sbi_remote_hfence_vvma_asid() - Execute HFENCE.VVMA instructions on given
491 * remote harts for current guest virtual address range belonging to a specific
494 * @cpu_mask: A cpu mask containing all the target harts.
495 * @start: Start of the current guest virtual address
496 * @size: Total size of the current guest virtual address range.
497 * @asid: The value of address space identifier (ASID).
501 int sbi_remote_hfence_vvma_asid(const struct cpumask *cpu_mask,
506 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID,
507 cpu_mask, start, size, asid, 0);
509 EXPORT_SYMBOL(sbi_remote_hfence_vvma_asid);
511 static void sbi_srst_reset(unsigned long type, unsigned long reason)
513 sbi_ecall(SBI_EXT_SRST, SBI_EXT_SRST_RESET, type, reason,
515 pr_warn("%s: type=0x%lx reason=0x%lx failed\n",
516 __func__, type, reason);
519 static int sbi_srst_reboot(struct notifier_block *this,
520 unsigned long mode, void *cmd)
522 sbi_srst_reset((mode == REBOOT_WARM || mode == REBOOT_SOFT) ?
523 SBI_SRST_RESET_TYPE_WARM_REBOOT :
524 SBI_SRST_RESET_TYPE_COLD_REBOOT,
525 SBI_SRST_RESET_REASON_NONE);
529 static struct notifier_block sbi_srst_reboot_nb;
531 static void sbi_srst_power_off(void)
533 sbi_srst_reset(SBI_SRST_RESET_TYPE_SHUTDOWN,
534 SBI_SRST_RESET_REASON_NONE);
538 * sbi_probe_extension() - Check if an SBI extension ID is supported or not.
539 * @extid: The extension ID to be probed.
541 * Return: 1 or an extension specific nonzero value if yes, 0 otherwise.
543 long sbi_probe_extension(int extid)
547 ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, extid,
554 EXPORT_SYMBOL(sbi_probe_extension);
556 static inline long sbi_get_spec_version(void)
558 return __sbi_base_ecall(SBI_EXT_BASE_GET_SPEC_VERSION);
561 static inline long sbi_get_firmware_id(void)
563 return __sbi_base_ecall(SBI_EXT_BASE_GET_IMP_ID);
566 static inline long sbi_get_firmware_version(void)
568 return __sbi_base_ecall(SBI_EXT_BASE_GET_IMP_VERSION);
571 long sbi_get_mvendorid(void)
573 return __sbi_base_ecall(SBI_EXT_BASE_GET_MVENDORID);
575 EXPORT_SYMBOL_GPL(sbi_get_mvendorid);
577 long sbi_get_marchid(void)
579 return __sbi_base_ecall(SBI_EXT_BASE_GET_MARCHID);
581 EXPORT_SYMBOL_GPL(sbi_get_marchid);
583 long sbi_get_mimpid(void)
585 return __sbi_base_ecall(SBI_EXT_BASE_GET_MIMPID);
587 EXPORT_SYMBOL_GPL(sbi_get_mimpid);
589 bool sbi_debug_console_available;
591 int sbi_debug_console_write(const char *bytes, unsigned int num_bytes)
593 phys_addr_t base_addr;
596 if (!sbi_debug_console_available)
599 if (is_vmalloc_addr(bytes))
600 base_addr = page_to_phys(vmalloc_to_page(bytes)) +
601 offset_in_page(bytes);
603 base_addr = __pa(bytes);
604 if (PAGE_SIZE < (offset_in_page(bytes) + num_bytes))
605 num_bytes = PAGE_SIZE - offset_in_page(bytes);
607 if (IS_ENABLED(CONFIG_32BIT))
608 ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
609 num_bytes, lower_32_bits(base_addr),
610 upper_32_bits(base_addr), 0, 0, 0);
612 ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
613 num_bytes, base_addr, 0, 0, 0, 0);
615 if (ret.error == SBI_ERR_FAILURE)
617 return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;
620 int sbi_debug_console_read(char *bytes, unsigned int num_bytes)
622 phys_addr_t base_addr;
625 if (!sbi_debug_console_available)
628 if (is_vmalloc_addr(bytes))
629 base_addr = page_to_phys(vmalloc_to_page(bytes)) +
630 offset_in_page(bytes);
632 base_addr = __pa(bytes);
633 if (PAGE_SIZE < (offset_in_page(bytes) + num_bytes))
634 num_bytes = PAGE_SIZE - offset_in_page(bytes);
636 if (IS_ENABLED(CONFIG_32BIT))
637 ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
638 num_bytes, lower_32_bits(base_addr),
639 upper_32_bits(base_addr), 0, 0, 0);
641 ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
642 num_bytes, base_addr, 0, 0, 0, 0);
644 if (ret.error == SBI_ERR_FAILURE)
646 return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;
649 void __init sbi_init(void)
654 ret = sbi_get_spec_version();
656 sbi_spec_version = ret;
658 pr_info("SBI specification v%lu.%lu detected\n",
659 sbi_major_version(), sbi_minor_version());
661 if (!sbi_spec_is_0_1()) {
662 pr_info("SBI implementation ID=0x%lx Version=0x%lx\n",
663 sbi_get_firmware_id(), sbi_get_firmware_version());
664 if (sbi_probe_extension(SBI_EXT_TIME)) {
665 __sbi_set_timer = __sbi_set_timer_v02;
666 pr_info("SBI TIME extension detected\n");
668 __sbi_set_timer = __sbi_set_timer_v01;
670 if (sbi_probe_extension(SBI_EXT_IPI)) {
671 __sbi_send_ipi = __sbi_send_ipi_v02;
672 pr_info("SBI IPI extension detected\n");
674 __sbi_send_ipi = __sbi_send_ipi_v01;
676 if (sbi_probe_extension(SBI_EXT_RFENCE)) {
677 __sbi_rfence = __sbi_rfence_v02;
678 pr_info("SBI RFENCE extension detected\n");
680 __sbi_rfence = __sbi_rfence_v01;
682 if (sbi_spec_version >= sbi_mk_version(0, 3) &&
683 sbi_probe_extension(SBI_EXT_SRST)) {
684 pr_info("SBI SRST extension detected\n");
685 pm_power_off = sbi_srst_power_off;
686 sbi_srst_reboot_nb.notifier_call = sbi_srst_reboot;
687 sbi_srst_reboot_nb.priority = 192;
688 register_restart_handler(&sbi_srst_reboot_nb);
690 if (sbi_spec_version >= sbi_mk_version(2, 0) &&
691 sbi_probe_extension(SBI_EXT_DBCN) > 0) {
692 pr_info("SBI DBCN extension detected\n");
693 sbi_debug_console_available = true;
695 if (sbi_spec_version >= sbi_mk_version(3, 0) &&
696 sbi_probe_extension(SBI_EXT_FWFT)) {
697 pr_info("SBI FWFT extension detected\n");
698 sbi_fwft_supported = true;
701 __sbi_set_timer = __sbi_set_timer_v01;
702 __sbi_send_ipi = __sbi_send_ipi_v01;
703 __sbi_rfence = __sbi_rfence_v01;