1/* SPDX-License-Identifier: BSD-3-Clause */
2
3/* Copyright (c) 2010-2017, The Regents of the University of California
4 * (Regents).  All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Regents nor the
14 * names of its contributors may be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
18 * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
19 * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
20 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 *
22 * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
25 * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
26 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
27 */
28
29/* This file is copied from RISC-V tools/linux project, it might change for
30 * new spec releases.
31 */
32
33#pragma once
34
35
36#include <stdint.h>
37
38#define SBI_SET_TIMER 0
39#define SBI_CONSOLE_PUTCHAR 1
40#define SBI_CONSOLE_GETCHAR 2
41#define SBI_CLEAR_IPI 3
42#define SBI_SEND_IPI 4
43#define SBI_REMOTE_FENCE_I 5
44#define SBI_REMOTE_SFENCE_VMA 6
45#define SBI_REMOTE_SFENCE_VMA_ASID 7
46#define SBI_SHUTDOWN 8
47
48static inline register_t sbi_call(register_t cmd,
49                                  register_t arg_0,
50                                  register_t arg_1,
51                                  register_t arg_2)
52{
53    register register_t a0 asm("a0") = arg_0;
54    register register_t a1 asm("a1") = arg_1;
55    register register_t a2 asm("a2") = arg_2;
56    register register_t a7 asm("a7") = cmd;
57    register register_t result asm("a0");
58    asm volatile("ecall"
59                 : "=r"(result)
60                 : "r"(a0), "r"(a1), "r"(a2), "r"(a7)
61                 : "memory");
62    return result;
63}
64
65/* Lazy implementations until SBI is finalized */
66#define SBI_CALL_0(which) sbi_call(which, 0, 0, 0)
67#define SBI_CALL_1(which, arg0) sbi_call(which, arg0, 0, 0)
68#define SBI_CALL_2(which, arg0, arg1) sbi_call(which, arg0, arg1, 0)
69
70static inline void sbi_console_putchar(int ch)
71{
72    SBI_CALL_1(SBI_CONSOLE_PUTCHAR, ch);
73}
74
75static inline int sbi_console_getchar(void)
76{
77    return (int)(SBI_CALL_0(SBI_CONSOLE_GETCHAR));
78}
79
80static inline void sbi_set_timer(unsigned long long stime_value)
81{
82#if __riscv_xlen == 32
83    SBI_CALL_2(SBI_SET_TIMER, stime_value, stime_value >> 32);
84#else
85    SBI_CALL_1(SBI_SET_TIMER, stime_value);
86#endif
87}
88
89static inline void sbi_shutdown(void)
90{
91    SBI_CALL_0(SBI_SHUTDOWN);
92}
93
94static inline void sbi_clear_ipi(void)
95{
96    SBI_CALL_0(SBI_CLEAR_IPI);
97}
98
99static inline void sbi_send_ipi(const unsigned long *hart_mask)
100{
101    SBI_CALL_1(SBI_SEND_IPI, (register_t)hart_mask);
102}
103
104static inline void sbi_remote_fence_i(const unsigned long *hart_mask)
105{
106    SBI_CALL_1(SBI_REMOTE_FENCE_I, (register_t)hart_mask);
107}
108
109static inline void sbi_remote_sfence_vma(const unsigned long *hart_mask,
110                                         unsigned long start,
111                                         unsigned long size)
112{
113    SBI_CALL_1(SBI_REMOTE_SFENCE_VMA, (register_t)hart_mask);
114}
115
116static inline void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
117                                              unsigned long start,
118                                              unsigned long size,
119                                              unsigned long asid)
120{
121    SBI_CALL_1(SBI_REMOTE_SFENCE_VMA_ASID, (register_t)hart_mask);
122}
123
124