Merge tag 'riscv-for-linus-6.10-mw1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / arch / riscv / include / asm / sbi.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2015 Regents of the University of California
4  * Copyright (c) 2020 Western Digital Corporation or its affiliates.
5  */
6
7 #ifndef _ASM_RISCV_SBI_H
8 #define _ASM_RISCV_SBI_H
9
10 #include <linux/types.h>
11 #include <linux/cpumask.h>
12
13 #ifdef CONFIG_RISCV_SBI
14 enum sbi_ext_id {
15 #ifdef CONFIG_RISCV_SBI_V01
16         SBI_EXT_0_1_SET_TIMER = 0x0,
17         SBI_EXT_0_1_CONSOLE_PUTCHAR = 0x1,
18         SBI_EXT_0_1_CONSOLE_GETCHAR = 0x2,
19         SBI_EXT_0_1_CLEAR_IPI = 0x3,
20         SBI_EXT_0_1_SEND_IPI = 0x4,
21         SBI_EXT_0_1_REMOTE_FENCE_I = 0x5,
22         SBI_EXT_0_1_REMOTE_SFENCE_VMA = 0x6,
23         SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID = 0x7,
24         SBI_EXT_0_1_SHUTDOWN = 0x8,
25 #endif
26         SBI_EXT_BASE = 0x10,
27         SBI_EXT_TIME = 0x54494D45,
28         SBI_EXT_IPI = 0x735049,
29         SBI_EXT_RFENCE = 0x52464E43,
30         SBI_EXT_HSM = 0x48534D,
31         SBI_EXT_SRST = 0x53525354,
32         SBI_EXT_SUSP = 0x53555350,
33         SBI_EXT_PMU = 0x504D55,
34         SBI_EXT_DBCN = 0x4442434E,
35         SBI_EXT_STA = 0x535441,
36
37         /* Experimentals extensions must lie within this range */
38         SBI_EXT_EXPERIMENTAL_START = 0x08000000,
39         SBI_EXT_EXPERIMENTAL_END = 0x08FFFFFF,
40
41         /* Vendor extensions must lie within this range */
42         SBI_EXT_VENDOR_START = 0x09000000,
43         SBI_EXT_VENDOR_END = 0x09FFFFFF,
44 };
45
46 enum sbi_ext_base_fid {
47         SBI_EXT_BASE_GET_SPEC_VERSION = 0,
48         SBI_EXT_BASE_GET_IMP_ID,
49         SBI_EXT_BASE_GET_IMP_VERSION,
50         SBI_EXT_BASE_PROBE_EXT,
51         SBI_EXT_BASE_GET_MVENDORID,
52         SBI_EXT_BASE_GET_MARCHID,
53         SBI_EXT_BASE_GET_MIMPID,
54 };
55
56 enum sbi_ext_time_fid {
57         SBI_EXT_TIME_SET_TIMER = 0,
58 };
59
60 enum sbi_ext_ipi_fid {
61         SBI_EXT_IPI_SEND_IPI = 0,
62 };
63
64 enum sbi_ext_rfence_fid {
65         SBI_EXT_RFENCE_REMOTE_FENCE_I = 0,
66         SBI_EXT_RFENCE_REMOTE_SFENCE_VMA,
67         SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID,
68         SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID,
69         SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
70         SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID,
71         SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA,
72 };
73
74 enum sbi_ext_hsm_fid {
75         SBI_EXT_HSM_HART_START = 0,
76         SBI_EXT_HSM_HART_STOP,
77         SBI_EXT_HSM_HART_STATUS,
78         SBI_EXT_HSM_HART_SUSPEND,
79 };
80
81 enum sbi_hsm_hart_state {
82         SBI_HSM_STATE_STARTED = 0,
83         SBI_HSM_STATE_STOPPED,
84         SBI_HSM_STATE_START_PENDING,
85         SBI_HSM_STATE_STOP_PENDING,
86         SBI_HSM_STATE_SUSPENDED,
87         SBI_HSM_STATE_SUSPEND_PENDING,
88         SBI_HSM_STATE_RESUME_PENDING,
89 };
90
91 #define SBI_HSM_SUSP_BASE_MASK                  0x7fffffff
92 #define SBI_HSM_SUSP_NON_RET_BIT                0x80000000
93 #define SBI_HSM_SUSP_PLAT_BASE                  0x10000000
94
95 #define SBI_HSM_SUSPEND_RET_DEFAULT             0x00000000
96 #define SBI_HSM_SUSPEND_RET_PLATFORM            SBI_HSM_SUSP_PLAT_BASE
97 #define SBI_HSM_SUSPEND_RET_LAST                SBI_HSM_SUSP_BASE_MASK
98 #define SBI_HSM_SUSPEND_NON_RET_DEFAULT         SBI_HSM_SUSP_NON_RET_BIT
99 #define SBI_HSM_SUSPEND_NON_RET_PLATFORM        (SBI_HSM_SUSP_NON_RET_BIT | \
100                                                  SBI_HSM_SUSP_PLAT_BASE)
101 #define SBI_HSM_SUSPEND_NON_RET_LAST            (SBI_HSM_SUSP_NON_RET_BIT | \
102                                                  SBI_HSM_SUSP_BASE_MASK)
103
104 enum sbi_ext_srst_fid {
105         SBI_EXT_SRST_RESET = 0,
106 };
107
108 enum sbi_srst_reset_type {
109         SBI_SRST_RESET_TYPE_SHUTDOWN = 0,
110         SBI_SRST_RESET_TYPE_COLD_REBOOT,
111         SBI_SRST_RESET_TYPE_WARM_REBOOT,
112 };
113
114 enum sbi_srst_reset_reason {
115         SBI_SRST_RESET_REASON_NONE = 0,
116         SBI_SRST_RESET_REASON_SYS_FAILURE,
117 };
118
119 enum sbi_ext_susp_fid {
120         SBI_EXT_SUSP_SYSTEM_SUSPEND = 0,
121 };
122
123 enum sbi_ext_susp_sleep_type {
124         SBI_SUSP_SLEEP_TYPE_SUSPEND_TO_RAM = 0,
125 };
126
127 enum sbi_ext_pmu_fid {
128         SBI_EXT_PMU_NUM_COUNTERS = 0,
129         SBI_EXT_PMU_COUNTER_GET_INFO,
130         SBI_EXT_PMU_COUNTER_CFG_MATCH,
131         SBI_EXT_PMU_COUNTER_START,
132         SBI_EXT_PMU_COUNTER_STOP,
133         SBI_EXT_PMU_COUNTER_FW_READ,
134         SBI_EXT_PMU_COUNTER_FW_READ_HI,
135         SBI_EXT_PMU_SNAPSHOT_SET_SHMEM,
136 };
137
138 union sbi_pmu_ctr_info {
139         unsigned long value;
140         struct {
141                 unsigned long csr:12;
142                 unsigned long width:6;
143 #if __riscv_xlen == 32
144                 unsigned long reserved:13;
145 #else
146                 unsigned long reserved:45;
147 #endif
148                 unsigned long type:1;
149         };
150 };
151
152 /* Data structure to contain the pmu snapshot data */
153 struct riscv_pmu_snapshot_data {
154         u64 ctr_overflow_mask;
155         u64 ctr_values[64];
156         u64 reserved[447];
157 };
158
159 #define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0)
160 #define RISCV_PMU_RAW_EVENT_IDX 0x20000
161
162 /** General pmu event codes specified in SBI PMU extension */
163 enum sbi_pmu_hw_generic_events_t {
164         SBI_PMU_HW_NO_EVENT                     = 0,
165         SBI_PMU_HW_CPU_CYCLES                   = 1,
166         SBI_PMU_HW_INSTRUCTIONS                 = 2,
167         SBI_PMU_HW_CACHE_REFERENCES             = 3,
168         SBI_PMU_HW_CACHE_MISSES                 = 4,
169         SBI_PMU_HW_BRANCH_INSTRUCTIONS          = 5,
170         SBI_PMU_HW_BRANCH_MISSES                = 6,
171         SBI_PMU_HW_BUS_CYCLES                   = 7,
172         SBI_PMU_HW_STALLED_CYCLES_FRONTEND      = 8,
173         SBI_PMU_HW_STALLED_CYCLES_BACKEND       = 9,
174         SBI_PMU_HW_REF_CPU_CYCLES               = 10,
175
176         SBI_PMU_HW_GENERAL_MAX,
177 };
178
179 /**
180  * Special "firmware" events provided by the firmware, even if the hardware
181  * does not support performance events. These events are encoded as a raw
182  * event type in Linux kernel perf framework.
183  */
184 enum sbi_pmu_fw_generic_events_t {
185         SBI_PMU_FW_MISALIGNED_LOAD      = 0,
186         SBI_PMU_FW_MISALIGNED_STORE     = 1,
187         SBI_PMU_FW_ACCESS_LOAD          = 2,
188         SBI_PMU_FW_ACCESS_STORE         = 3,
189         SBI_PMU_FW_ILLEGAL_INSN         = 4,
190         SBI_PMU_FW_SET_TIMER            = 5,
191         SBI_PMU_FW_IPI_SENT             = 6,
192         SBI_PMU_FW_IPI_RCVD             = 7,
193         SBI_PMU_FW_FENCE_I_SENT         = 8,
194         SBI_PMU_FW_FENCE_I_RCVD         = 9,
195         SBI_PMU_FW_SFENCE_VMA_SENT      = 10,
196         SBI_PMU_FW_SFENCE_VMA_RCVD      = 11,
197         SBI_PMU_FW_SFENCE_VMA_ASID_SENT = 12,
198         SBI_PMU_FW_SFENCE_VMA_ASID_RCVD = 13,
199
200         SBI_PMU_FW_HFENCE_GVMA_SENT     = 14,
201         SBI_PMU_FW_HFENCE_GVMA_RCVD     = 15,
202         SBI_PMU_FW_HFENCE_GVMA_VMID_SENT = 16,
203         SBI_PMU_FW_HFENCE_GVMA_VMID_RCVD = 17,
204
205         SBI_PMU_FW_HFENCE_VVMA_SENT     = 18,
206         SBI_PMU_FW_HFENCE_VVMA_RCVD     = 19,
207         SBI_PMU_FW_HFENCE_VVMA_ASID_SENT = 20,
208         SBI_PMU_FW_HFENCE_VVMA_ASID_RCVD = 21,
209         SBI_PMU_FW_MAX,
210 };
211
212 /* SBI PMU event types */
213 enum sbi_pmu_event_type {
214         SBI_PMU_EVENT_TYPE_HW = 0x0,
215         SBI_PMU_EVENT_TYPE_CACHE = 0x1,
216         SBI_PMU_EVENT_TYPE_RAW = 0x2,
217         SBI_PMU_EVENT_TYPE_FW = 0xf,
218 };
219
220 /* SBI PMU event types */
221 enum sbi_pmu_ctr_type {
222         SBI_PMU_CTR_TYPE_HW = 0x0,
223         SBI_PMU_CTR_TYPE_FW,
224 };
225
226 /* Helper macros to decode event idx */
227 #define SBI_PMU_EVENT_IDX_OFFSET 20
228 #define SBI_PMU_EVENT_IDX_MASK 0xFFFFF
229 #define SBI_PMU_EVENT_IDX_CODE_MASK 0xFFFF
230 #define SBI_PMU_EVENT_IDX_TYPE_MASK 0xF0000
231 #define SBI_PMU_EVENT_RAW_IDX 0x20000
232 #define SBI_PMU_FIXED_CTR_MASK 0x07
233
234 #define SBI_PMU_EVENT_CACHE_ID_CODE_MASK 0xFFF8
235 #define SBI_PMU_EVENT_CACHE_OP_ID_CODE_MASK 0x06
236 #define SBI_PMU_EVENT_CACHE_RESULT_ID_CODE_MASK 0x01
237
238 #define SBI_PMU_EVENT_CACHE_ID_SHIFT 3
239 #define SBI_PMU_EVENT_CACHE_OP_SHIFT 1
240
241 #define SBI_PMU_EVENT_IDX_INVALID 0xFFFFFFFF
242
243 /* Flags defined for config matching function */
244 #define SBI_PMU_CFG_FLAG_SKIP_MATCH     BIT(0)
245 #define SBI_PMU_CFG_FLAG_CLEAR_VALUE    BIT(1)
246 #define SBI_PMU_CFG_FLAG_AUTO_START     BIT(2)
247 #define SBI_PMU_CFG_FLAG_SET_VUINH      BIT(3)
248 #define SBI_PMU_CFG_FLAG_SET_VSINH      BIT(4)
249 #define SBI_PMU_CFG_FLAG_SET_UINH       BIT(5)
250 #define SBI_PMU_CFG_FLAG_SET_SINH       BIT(6)
251 #define SBI_PMU_CFG_FLAG_SET_MINH       BIT(7)
252
253 /* Flags defined for counter start function */
254 #define SBI_PMU_START_FLAG_SET_INIT_VALUE BIT(0)
255 #define SBI_PMU_START_FLAG_INIT_SNAPSHOT BIT(1)
256
257 /* Flags defined for counter stop function */
258 #define SBI_PMU_STOP_FLAG_RESET BIT(0)
259 #define SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT BIT(1)
260
261 enum sbi_ext_dbcn_fid {
262         SBI_EXT_DBCN_CONSOLE_WRITE = 0,
263         SBI_EXT_DBCN_CONSOLE_READ = 1,
264         SBI_EXT_DBCN_CONSOLE_WRITE_BYTE = 2,
265 };
266
267 /* SBI STA (steal-time accounting) extension */
268 enum sbi_ext_sta_fid {
269         SBI_EXT_STA_STEAL_TIME_SET_SHMEM = 0,
270 };
271
272 struct sbi_sta_struct {
273         __le32 sequence;
274         __le32 flags;
275         __le64 steal;
276         u8 preempted;
277         u8 pad[47];
278 } __packed;
279
280 #define SBI_SHMEM_DISABLE               -1
281
282 /* SBI spec version fields */
283 #define SBI_SPEC_VERSION_DEFAULT        0x1
284 #define SBI_SPEC_VERSION_MAJOR_SHIFT    24
285 #define SBI_SPEC_VERSION_MAJOR_MASK     0x7f
286 #define SBI_SPEC_VERSION_MINOR_MASK     0xffffff
287
288 /* SBI return error codes */
289 #define SBI_SUCCESS             0
290 #define SBI_ERR_FAILURE         -1
291 #define SBI_ERR_NOT_SUPPORTED   -2
292 #define SBI_ERR_INVALID_PARAM   -3
293 #define SBI_ERR_DENIED          -4
294 #define SBI_ERR_INVALID_ADDRESS -5
295 #define SBI_ERR_ALREADY_AVAILABLE -6
296 #define SBI_ERR_ALREADY_STARTED -7
297 #define SBI_ERR_ALREADY_STOPPED -8
298 #define SBI_ERR_NO_SHMEM        -9
299
300 extern unsigned long sbi_spec_version;
301 struct sbiret {
302         long error;
303         long value;
304 };
305
306 void sbi_init(void);
307 struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
308                         unsigned long arg1, unsigned long arg2,
309                         unsigned long arg3, unsigned long arg4,
310                         unsigned long arg5);
311
312 #ifdef CONFIG_RISCV_SBI_V01
313 void sbi_console_putchar(int ch);
314 int sbi_console_getchar(void);
315 #else
316 static inline void sbi_console_putchar(int ch) { }
317 static inline int sbi_console_getchar(void) { return -ENOENT; }
318 #endif
319 long sbi_get_mvendorid(void);
320 long sbi_get_marchid(void);
321 long sbi_get_mimpid(void);
322 void sbi_set_timer(uint64_t stime_value);
323 void sbi_shutdown(void);
324 void sbi_send_ipi(unsigned int cpu);
325 int sbi_remote_fence_i(const struct cpumask *cpu_mask);
326
327 int sbi_remote_sfence_vma_asid(const struct cpumask *cpu_mask,
328                                 unsigned long start,
329                                 unsigned long size,
330                                 unsigned long asid);
331 int sbi_remote_hfence_gvma(const struct cpumask *cpu_mask,
332                            unsigned long start,
333                            unsigned long size);
334 int sbi_remote_hfence_gvma_vmid(const struct cpumask *cpu_mask,
335                                 unsigned long start,
336                                 unsigned long size,
337                                 unsigned long vmid);
338 int sbi_remote_hfence_vvma(const struct cpumask *cpu_mask,
339                            unsigned long start,
340                            unsigned long size);
341 int sbi_remote_hfence_vvma_asid(const struct cpumask *cpu_mask,
342                                 unsigned long start,
343                                 unsigned long size,
344                                 unsigned long asid);
345 long sbi_probe_extension(int ext);
346
347 /* Check if current SBI specification version is 0.1 or not */
348 static inline int sbi_spec_is_0_1(void)
349 {
350         return (sbi_spec_version == SBI_SPEC_VERSION_DEFAULT) ? 1 : 0;
351 }
352
353 /* Get the major version of SBI */
354 static inline unsigned long sbi_major_version(void)
355 {
356         return (sbi_spec_version >> SBI_SPEC_VERSION_MAJOR_SHIFT) &
357                 SBI_SPEC_VERSION_MAJOR_MASK;
358 }
359
360 /* Get the minor version of SBI */
361 static inline unsigned long sbi_minor_version(void)
362 {
363         return sbi_spec_version & SBI_SPEC_VERSION_MINOR_MASK;
364 }
365
366 /* Make SBI version */
367 static inline unsigned long sbi_mk_version(unsigned long major,
368                                             unsigned long minor)
369 {
370         return ((major & SBI_SPEC_VERSION_MAJOR_MASK) << SBI_SPEC_VERSION_MAJOR_SHIFT)
371                 | (minor & SBI_SPEC_VERSION_MINOR_MASK);
372 }
373
374 int sbi_err_map_linux_errno(int err);
375
376 extern bool sbi_debug_console_available;
377 int sbi_debug_console_write(const char *bytes, unsigned int num_bytes);
378 int sbi_debug_console_read(char *bytes, unsigned int num_bytes);
379
380 #else /* CONFIG_RISCV_SBI */
381 static inline int sbi_remote_fence_i(const struct cpumask *cpu_mask) { return -1; }
382 static inline void sbi_init(void) {}
383 #endif /* CONFIG_RISCV_SBI */
384
385 unsigned long riscv_cached_mvendorid(unsigned int cpu_id);
386 unsigned long riscv_cached_marchid(unsigned int cpu_id);
387 unsigned long riscv_cached_mimpid(unsigned int cpu_id);
388
389 #if IS_ENABLED(CONFIG_SMP) && IS_ENABLED(CONFIG_RISCV_SBI)
390 DECLARE_STATIC_KEY_FALSE(riscv_sbi_for_rfence);
391 #define riscv_use_sbi_for_rfence() \
392         static_branch_unlikely(&riscv_sbi_for_rfence)
393 void sbi_ipi_init(void);
394 #else
395 static inline bool riscv_use_sbi_for_rfence(void) { return false; }
396 static inline void sbi_ipi_init(void) { }
397 #endif
398
399 #endif /* _ASM_RISCV_SBI_H */