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 struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
28 unsigned long arg1, unsigned long arg2,
29 unsigned long arg3, unsigned long arg4,
34 register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);
35 register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);
36 register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);
37 register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3);
38 register uintptr_t a4 asm ("a4") = (uintptr_t)(arg4);
39 register uintptr_t a5 asm ("a5") = (uintptr_t)(arg5);
40 register uintptr_t a6 asm ("a6") = (uintptr_t)(fid);
41 register uintptr_t a7 asm ("a7") = (uintptr_t)(ext);
43 : "+r" (a0), "+r" (a1)
44 : "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7)
51 EXPORT_SYMBOL(sbi_ecall);
53 int sbi_err_map_linux_errno(int err)
60 case SBI_ERR_INVALID_PARAM:
62 case SBI_ERR_INVALID_ADDRESS:
64 case SBI_ERR_NOT_SUPPORTED:
70 EXPORT_SYMBOL(sbi_err_map_linux_errno);
72 #ifdef CONFIG_RISCV_SBI_V01
73 static unsigned long __sbi_v01_cpumask_to_hartmask(const struct cpumask *cpu_mask)
75 unsigned long cpuid, hartid;
76 unsigned long hmask = 0;
79 * There is no maximum hartid concept in RISC-V and NR_CPUS must not be
80 * associated with hartid. As SBI v0.1 is only kept for backward compatibility
81 * and will be removed in the future, there is no point in supporting hartid
82 * greater than BITS_PER_LONG (32 for RV32 and 64 for RV64). Ideally, SBI v0.2
83 * should be used for platforms with hartid greater than BITS_PER_LONG.
85 for_each_cpu(cpuid, cpu_mask) {
86 hartid = cpuid_to_hartid_map(cpuid);
87 if (hartid >= BITS_PER_LONG) {
88 pr_warn("Unable to send any request to hartid > BITS_PER_LONG for SBI v0.1\n");
98 * sbi_console_putchar() - Writes given character to the console device.
99 * @ch: The data to be written to the console.
103 void sbi_console_putchar(int ch)
105 sbi_ecall(SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, ch, 0, 0, 0, 0, 0);
107 EXPORT_SYMBOL(sbi_console_putchar);
110 * sbi_console_getchar() - Reads a byte from console device.
112 * Returns the value read from console.
114 int sbi_console_getchar(void)
118 ret = sbi_ecall(SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 0, 0, 0, 0, 0, 0);
122 EXPORT_SYMBOL(sbi_console_getchar);
125 * sbi_shutdown() - Remove all the harts from executing supervisor code.
129 void sbi_shutdown(void)
131 sbi_ecall(SBI_EXT_0_1_SHUTDOWN, 0, 0, 0, 0, 0, 0, 0);
133 EXPORT_SYMBOL(sbi_shutdown);
136 * __sbi_set_timer_v01() - Program the timer for next timer event.
137 * @stime_value: The value after which next timer event should fire.
141 static void __sbi_set_timer_v01(uint64_t stime_value)
143 #if __riscv_xlen == 32
144 sbi_ecall(SBI_EXT_0_1_SET_TIMER, 0, stime_value,
145 stime_value >> 32, 0, 0, 0, 0);
147 sbi_ecall(SBI_EXT_0_1_SET_TIMER, 0, stime_value, 0, 0, 0, 0, 0);
151 static void __sbi_send_ipi_v01(unsigned int cpu)
153 unsigned long hart_mask =
154 __sbi_v01_cpumask_to_hartmask(cpumask_of(cpu));
155 sbi_ecall(SBI_EXT_0_1_SEND_IPI, 0, (unsigned long)(&hart_mask),
159 static int __sbi_rfence_v01(int fid, const struct cpumask *cpu_mask,
160 unsigned long start, unsigned long size,
161 unsigned long arg4, unsigned long arg5)
164 unsigned long hart_mask;
166 if (!cpu_mask || cpumask_empty(cpu_mask))
167 cpu_mask = cpu_online_mask;
168 hart_mask = __sbi_v01_cpumask_to_hartmask(cpu_mask);
170 /* v0.2 function IDs are equivalent to v0.1 extension IDs */
172 case SBI_EXT_RFENCE_REMOTE_FENCE_I:
173 sbi_ecall(SBI_EXT_0_1_REMOTE_FENCE_I, 0,
174 (unsigned long)&hart_mask, 0, 0, 0, 0, 0);
176 case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA:
177 sbi_ecall(SBI_EXT_0_1_REMOTE_SFENCE_VMA, 0,
178 (unsigned long)&hart_mask, start, size,
181 case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID:
182 sbi_ecall(SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID, 0,
183 (unsigned long)&hart_mask, start, size,
187 pr_err("SBI call [%d]not supported in SBI v0.1\n", fid);
194 static void sbi_set_power_off(void)
196 pm_power_off = sbi_shutdown;
199 static void __sbi_set_timer_v01(uint64_t stime_value)
201 pr_warn("Timer extension is not available in SBI v%lu.%lu\n",
202 sbi_major_version(), sbi_minor_version());
205 static void __sbi_send_ipi_v01(unsigned int cpu)
207 pr_warn("IPI extension is not available in SBI v%lu.%lu\n",
208 sbi_major_version(), sbi_minor_version());
211 static int __sbi_rfence_v01(int fid, const struct cpumask *cpu_mask,
212 unsigned long start, unsigned long size,
213 unsigned long arg4, unsigned long arg5)
215 pr_warn("remote fence extension is not available in SBI v%lu.%lu\n",
216 sbi_major_version(), sbi_minor_version());
221 static void sbi_set_power_off(void) {}
222 #endif /* CONFIG_RISCV_SBI_V01 */
224 static void __sbi_set_timer_v02(uint64_t stime_value)
226 #if __riscv_xlen == 32
227 sbi_ecall(SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, stime_value,
228 stime_value >> 32, 0, 0, 0, 0);
230 sbi_ecall(SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, stime_value, 0,
235 static void __sbi_send_ipi_v02(unsigned int cpu)
238 struct sbiret ret = {0};
240 ret = sbi_ecall(SBI_EXT_IPI, SBI_EXT_IPI_SEND_IPI,
241 1UL, cpuid_to_hartid_map(cpu), 0, 0, 0, 0);
243 result = sbi_err_map_linux_errno(ret.error);
244 pr_err("%s: hbase = [%lu] failed (error [%d])\n",
245 __func__, cpuid_to_hartid_map(cpu), result);
249 static int __sbi_rfence_v02_call(unsigned long fid, unsigned long hmask,
250 unsigned long hbase, unsigned long start,
251 unsigned long size, unsigned long arg4,
254 struct sbiret ret = {0};
255 int ext = SBI_EXT_RFENCE;
259 case SBI_EXT_RFENCE_REMOTE_FENCE_I:
260 ret = sbi_ecall(ext, fid, hmask, hbase, 0, 0, 0, 0);
262 case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA:
263 ret = sbi_ecall(ext, fid, hmask, hbase, start,
266 case SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID:
267 ret = sbi_ecall(ext, fid, hmask, hbase, start,
271 case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA:
272 ret = sbi_ecall(ext, fid, hmask, hbase, start,
275 case SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID:
276 ret = sbi_ecall(ext, fid, hmask, hbase, start,
279 case SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA:
280 ret = sbi_ecall(ext, fid, hmask, hbase, start,
283 case SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID:
284 ret = sbi_ecall(ext, fid, hmask, hbase, start,
288 pr_err("unknown function ID [%lu] for SBI extension [%d]\n",
294 result = sbi_err_map_linux_errno(ret.error);
295 pr_err("%s: hbase = [%lu] hmask = [0x%lx] failed (error [%d])\n",
296 __func__, hbase, hmask, result);
302 static int __sbi_rfence_v02(int fid, const struct cpumask *cpu_mask,
303 unsigned long start, unsigned long size,
304 unsigned long arg4, unsigned long arg5)
306 unsigned long hartid, cpuid, hmask = 0, hbase = 0, htop = 0;
309 if (!cpu_mask || cpumask_empty(cpu_mask))
310 cpu_mask = cpu_online_mask;
312 for_each_cpu(cpuid, cpu_mask) {
313 hartid = cpuid_to_hartid_map(cpuid);
315 if (hartid + BITS_PER_LONG <= htop ||
316 hbase + BITS_PER_LONG <= hartid) {
317 result = __sbi_rfence_v02_call(fid, hmask,
318 hbase, start, size, arg4, arg5);
322 } else if (hartid < hbase) {
323 /* shift the mask to fit lower hartid */
324 hmask <<= hbase - hartid;
331 } else if (hartid > htop) {
334 hmask |= BIT(hartid - hbase);
338 result = __sbi_rfence_v02_call(fid, hmask, hbase,
339 start, size, arg4, arg5);
348 * sbi_set_timer() - Program the timer for next timer event.
349 * @stime_value: The value after which next timer event should fire.
353 void sbi_set_timer(uint64_t stime_value)
355 __sbi_set_timer(stime_value);
359 * sbi_send_ipi() - Send an IPI to any hart.
360 * @cpu: Logical id of the target CPU.
362 void sbi_send_ipi(unsigned int cpu)
366 EXPORT_SYMBOL(sbi_send_ipi);
369 * sbi_remote_fence_i() - Execute FENCE.I instruction on given remote harts.
370 * @cpu_mask: A cpu mask containing all the target harts.
372 * Return: 0 on success, appropriate linux error code otherwise.
374 int sbi_remote_fence_i(const struct cpumask *cpu_mask)
376 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_FENCE_I,
377 cpu_mask, 0, 0, 0, 0);
379 EXPORT_SYMBOL(sbi_remote_fence_i);
382 * sbi_remote_sfence_vma_asid() - Execute SFENCE.VMA instructions on given
383 * remote harts for a virtual address range belonging to a specific ASID or not.
385 * @cpu_mask: A cpu mask containing all the target harts.
386 * @start: Start of the virtual address
387 * @size: Total size of the virtual address range.
388 * @asid: The value of address space identifier (ASID), or FLUSH_TLB_NO_ASID
389 * for flushing all address spaces.
391 * Return: 0 on success, appropriate linux error code otherwise.
393 int sbi_remote_sfence_vma_asid(const struct cpumask *cpu_mask,
398 if (asid == FLUSH_TLB_NO_ASID)
399 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA,
400 cpu_mask, start, size, 0, 0);
402 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID,
403 cpu_mask, start, size, asid, 0);
405 EXPORT_SYMBOL(sbi_remote_sfence_vma_asid);
408 * sbi_remote_hfence_gvma() - Execute HFENCE.GVMA instructions on given remote
409 * harts for the specified guest physical address range.
410 * @cpu_mask: A cpu mask containing all the target harts.
411 * @start: Start of the guest physical address
412 * @size: Total size of the guest physical address range.
416 int sbi_remote_hfence_gvma(const struct cpumask *cpu_mask,
420 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
421 cpu_mask, start, size, 0, 0);
423 EXPORT_SYMBOL_GPL(sbi_remote_hfence_gvma);
426 * sbi_remote_hfence_gvma_vmid() - Execute HFENCE.GVMA instructions on given
427 * remote harts for a guest physical address range belonging to a specific VMID.
429 * @cpu_mask: A cpu mask containing all the target harts.
430 * @start: Start of the guest physical address
431 * @size: Total size of the guest physical address range.
432 * @vmid: The value of guest ID (VMID).
434 * Return: 0 if success, Error otherwise.
436 int sbi_remote_hfence_gvma_vmid(const struct cpumask *cpu_mask,
441 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID,
442 cpu_mask, start, size, vmid, 0);
444 EXPORT_SYMBOL(sbi_remote_hfence_gvma_vmid);
447 * sbi_remote_hfence_vvma() - Execute HFENCE.VVMA instructions on given remote
448 * harts for the current guest virtual address range.
449 * @cpu_mask: A cpu mask containing all the target harts.
450 * @start: Start of the current guest virtual address
451 * @size: Total size of the current guest virtual address range.
455 int sbi_remote_hfence_vvma(const struct cpumask *cpu_mask,
459 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA,
460 cpu_mask, start, size, 0, 0);
462 EXPORT_SYMBOL(sbi_remote_hfence_vvma);
465 * sbi_remote_hfence_vvma_asid() - Execute HFENCE.VVMA instructions on given
466 * remote harts for current guest virtual address range belonging to a specific
469 * @cpu_mask: A cpu mask containing all the target harts.
470 * @start: Start of the current guest virtual address
471 * @size: Total size of the current guest virtual address range.
472 * @asid: The value of address space identifier (ASID).
476 int sbi_remote_hfence_vvma_asid(const struct cpumask *cpu_mask,
481 return __sbi_rfence(SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID,
482 cpu_mask, start, size, asid, 0);
484 EXPORT_SYMBOL(sbi_remote_hfence_vvma_asid);
486 static void sbi_srst_reset(unsigned long type, unsigned long reason)
488 sbi_ecall(SBI_EXT_SRST, SBI_EXT_SRST_RESET, type, reason,
490 pr_warn("%s: type=0x%lx reason=0x%lx failed\n",
491 __func__, type, reason);
494 static int sbi_srst_reboot(struct notifier_block *this,
495 unsigned long mode, void *cmd)
497 sbi_srst_reset((mode == REBOOT_WARM || mode == REBOOT_SOFT) ?
498 SBI_SRST_RESET_TYPE_WARM_REBOOT :
499 SBI_SRST_RESET_TYPE_COLD_REBOOT,
500 SBI_SRST_RESET_REASON_NONE);
504 static struct notifier_block sbi_srst_reboot_nb;
506 static void sbi_srst_power_off(void)
508 sbi_srst_reset(SBI_SRST_RESET_TYPE_SHUTDOWN,
509 SBI_SRST_RESET_REASON_NONE);
513 * sbi_probe_extension() - Check if an SBI extension ID is supported or not.
514 * @extid: The extension ID to be probed.
516 * Return: 1 or an extension specific nonzero value if yes, 0 otherwise.
518 long sbi_probe_extension(int extid)
522 ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, extid,
529 EXPORT_SYMBOL(sbi_probe_extension);
531 static long __sbi_base_ecall(int fid)
535 ret = sbi_ecall(SBI_EXT_BASE, fid, 0, 0, 0, 0, 0, 0);
539 return sbi_err_map_linux_errno(ret.error);
542 static inline long sbi_get_spec_version(void)
544 return __sbi_base_ecall(SBI_EXT_BASE_GET_SPEC_VERSION);
547 static inline long sbi_get_firmware_id(void)
549 return __sbi_base_ecall(SBI_EXT_BASE_GET_IMP_ID);
552 static inline long sbi_get_firmware_version(void)
554 return __sbi_base_ecall(SBI_EXT_BASE_GET_IMP_VERSION);
557 long sbi_get_mvendorid(void)
559 return __sbi_base_ecall(SBI_EXT_BASE_GET_MVENDORID);
561 EXPORT_SYMBOL_GPL(sbi_get_mvendorid);
563 long sbi_get_marchid(void)
565 return __sbi_base_ecall(SBI_EXT_BASE_GET_MARCHID);
567 EXPORT_SYMBOL_GPL(sbi_get_marchid);
569 long sbi_get_mimpid(void)
571 return __sbi_base_ecall(SBI_EXT_BASE_GET_MIMPID);
573 EXPORT_SYMBOL_GPL(sbi_get_mimpid);
575 bool sbi_debug_console_available;
577 int sbi_debug_console_write(const char *bytes, unsigned int num_bytes)
579 phys_addr_t base_addr;
582 if (!sbi_debug_console_available)
585 if (is_vmalloc_addr(bytes))
586 base_addr = page_to_phys(vmalloc_to_page(bytes)) +
587 offset_in_page(bytes);
589 base_addr = __pa(bytes);
590 if (PAGE_SIZE < (offset_in_page(bytes) + num_bytes))
591 num_bytes = PAGE_SIZE - offset_in_page(bytes);
593 if (IS_ENABLED(CONFIG_32BIT))
594 ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
595 num_bytes, lower_32_bits(base_addr),
596 upper_32_bits(base_addr), 0, 0, 0);
598 ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
599 num_bytes, base_addr, 0, 0, 0, 0);
601 if (ret.error == SBI_ERR_FAILURE)
603 return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;
606 int sbi_debug_console_read(char *bytes, unsigned int num_bytes)
608 phys_addr_t base_addr;
611 if (!sbi_debug_console_available)
614 if (is_vmalloc_addr(bytes))
615 base_addr = page_to_phys(vmalloc_to_page(bytes)) +
616 offset_in_page(bytes);
618 base_addr = __pa(bytes);
619 if (PAGE_SIZE < (offset_in_page(bytes) + num_bytes))
620 num_bytes = PAGE_SIZE - offset_in_page(bytes);
622 if (IS_ENABLED(CONFIG_32BIT))
623 ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
624 num_bytes, lower_32_bits(base_addr),
625 upper_32_bits(base_addr), 0, 0, 0);
627 ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
628 num_bytes, base_addr, 0, 0, 0, 0);
630 if (ret.error == SBI_ERR_FAILURE)
632 return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;
635 void __init sbi_init(void)
640 ret = sbi_get_spec_version();
642 sbi_spec_version = ret;
644 pr_info("SBI specification v%lu.%lu detected\n",
645 sbi_major_version(), sbi_minor_version());
647 if (!sbi_spec_is_0_1()) {
648 pr_info("SBI implementation ID=0x%lx Version=0x%lx\n",
649 sbi_get_firmware_id(), sbi_get_firmware_version());
650 if (sbi_probe_extension(SBI_EXT_TIME)) {
651 __sbi_set_timer = __sbi_set_timer_v02;
652 pr_info("SBI TIME extension detected\n");
654 __sbi_set_timer = __sbi_set_timer_v01;
656 if (sbi_probe_extension(SBI_EXT_IPI)) {
657 __sbi_send_ipi = __sbi_send_ipi_v02;
658 pr_info("SBI IPI extension detected\n");
660 __sbi_send_ipi = __sbi_send_ipi_v01;
662 if (sbi_probe_extension(SBI_EXT_RFENCE)) {
663 __sbi_rfence = __sbi_rfence_v02;
664 pr_info("SBI RFENCE extension detected\n");
666 __sbi_rfence = __sbi_rfence_v01;
668 if ((sbi_spec_version >= sbi_mk_version(0, 3)) &&
669 sbi_probe_extension(SBI_EXT_SRST)) {
670 pr_info("SBI SRST extension detected\n");
671 pm_power_off = sbi_srst_power_off;
672 sbi_srst_reboot_nb.notifier_call = sbi_srst_reboot;
673 sbi_srst_reboot_nb.priority = 192;
674 register_restart_handler(&sbi_srst_reboot_nb);
676 if ((sbi_spec_version >= sbi_mk_version(2, 0)) &&
677 (sbi_probe_extension(SBI_EXT_DBCN) > 0)) {
678 pr_info("SBI DBCN extension detected\n");
679 sbi_debug_console_available = true;
682 __sbi_set_timer = __sbi_set_timer_v01;
683 __sbi_send_ipi = __sbi_send_ipi_v01;
684 __sbi_rfence = __sbi_rfence_v01;