xref: /linux/arch/riscv/include/asm/sbi.h (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
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/args.h>
11 #include <linux/types.h>
12 #include <linux/cpumask.h>
13 #include <linux/jump_label.h>
14 #include <linux/tracepoint-defs.h>
15 
16 #ifdef CONFIG_RISCV_SBI
17 enum sbi_ext_id {
18 #ifdef CONFIG_RISCV_SBI_V01
19 	SBI_EXT_0_1_SET_TIMER = 0x0,
20 	SBI_EXT_0_1_CONSOLE_PUTCHAR = 0x1,
21 	SBI_EXT_0_1_CONSOLE_GETCHAR = 0x2,
22 	SBI_EXT_0_1_CLEAR_IPI = 0x3,
23 	SBI_EXT_0_1_SEND_IPI = 0x4,
24 	SBI_EXT_0_1_REMOTE_FENCE_I = 0x5,
25 	SBI_EXT_0_1_REMOTE_SFENCE_VMA = 0x6,
26 	SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID = 0x7,
27 	SBI_EXT_0_1_SHUTDOWN = 0x8,
28 #endif
29 	SBI_EXT_BASE = 0x10,
30 	SBI_EXT_TIME = 0x54494D45,
31 	SBI_EXT_IPI = 0x735049,
32 	SBI_EXT_RFENCE = 0x52464E43,
33 	SBI_EXT_HSM = 0x48534D,
34 	SBI_EXT_SRST = 0x53525354,
35 	SBI_EXT_SUSP = 0x53555350,
36 	SBI_EXT_PMU = 0x504D55,
37 	SBI_EXT_DBCN = 0x4442434E,
38 	SBI_EXT_STA = 0x535441,
39 	SBI_EXT_NACL = 0x4E41434C,
40 	SBI_EXT_FWFT = 0x46574654,
41 	SBI_EXT_MPXY = 0x4D505859,
42 	SBI_EXT_DBTR = 0x44425452,
43 
44 	/* Experimentals extensions must lie within this range */
45 	SBI_EXT_EXPERIMENTAL_START = 0x08000000,
46 	SBI_EXT_EXPERIMENTAL_END = 0x08FFFFFF,
47 
48 	/* Vendor extensions must lie within this range */
49 	SBI_EXT_VENDOR_START = 0x09000000,
50 	SBI_EXT_VENDOR_END = 0x09FFFFFF,
51 };
52 
53 enum sbi_ext_base_fid {
54 	SBI_EXT_BASE_GET_SPEC_VERSION = 0,
55 	SBI_EXT_BASE_GET_IMP_ID,
56 	SBI_EXT_BASE_GET_IMP_VERSION,
57 	SBI_EXT_BASE_PROBE_EXT,
58 	SBI_EXT_BASE_GET_MVENDORID,
59 	SBI_EXT_BASE_GET_MARCHID,
60 	SBI_EXT_BASE_GET_MIMPID,
61 };
62 
63 enum sbi_ext_time_fid {
64 	SBI_EXT_TIME_SET_TIMER = 0,
65 };
66 
67 enum sbi_ext_ipi_fid {
68 	SBI_EXT_IPI_SEND_IPI = 0,
69 };
70 
71 enum sbi_ext_rfence_fid {
72 	SBI_EXT_RFENCE_REMOTE_FENCE_I = 0,
73 	SBI_EXT_RFENCE_REMOTE_SFENCE_VMA,
74 	SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID,
75 	SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID,
76 	SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
77 	SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID,
78 	SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA,
79 };
80 
81 enum sbi_ext_hsm_fid {
82 	SBI_EXT_HSM_HART_START = 0,
83 	SBI_EXT_HSM_HART_STOP,
84 	SBI_EXT_HSM_HART_STATUS,
85 	SBI_EXT_HSM_HART_SUSPEND,
86 };
87 
88 enum sbi_hsm_hart_state {
89 	SBI_HSM_STATE_STARTED = 0,
90 	SBI_HSM_STATE_STOPPED,
91 	SBI_HSM_STATE_START_PENDING,
92 	SBI_HSM_STATE_STOP_PENDING,
93 	SBI_HSM_STATE_SUSPENDED,
94 	SBI_HSM_STATE_SUSPEND_PENDING,
95 	SBI_HSM_STATE_RESUME_PENDING,
96 };
97 
98 #define SBI_HSM_SUSP_BASE_MASK			0x7fffffff
99 #define SBI_HSM_SUSP_NON_RET_BIT		0x80000000
100 #define SBI_HSM_SUSP_PLAT_BASE			0x10000000
101 
102 #define SBI_HSM_SUSPEND_RET_DEFAULT		0x00000000
103 #define SBI_HSM_SUSPEND_RET_PLATFORM		SBI_HSM_SUSP_PLAT_BASE
104 #define SBI_HSM_SUSPEND_RET_LAST		SBI_HSM_SUSP_BASE_MASK
105 #define SBI_HSM_SUSPEND_NON_RET_DEFAULT		SBI_HSM_SUSP_NON_RET_BIT
106 #define SBI_HSM_SUSPEND_NON_RET_PLATFORM	(SBI_HSM_SUSP_NON_RET_BIT | \
107 						 SBI_HSM_SUSP_PLAT_BASE)
108 #define SBI_HSM_SUSPEND_NON_RET_LAST		(SBI_HSM_SUSP_NON_RET_BIT | \
109 						 SBI_HSM_SUSP_BASE_MASK)
110 
111 enum sbi_ext_srst_fid {
112 	SBI_EXT_SRST_RESET = 0,
113 };
114 
115 enum sbi_srst_reset_type {
116 	SBI_SRST_RESET_TYPE_SHUTDOWN = 0,
117 	SBI_SRST_RESET_TYPE_COLD_REBOOT,
118 	SBI_SRST_RESET_TYPE_WARM_REBOOT,
119 };
120 
121 enum sbi_srst_reset_reason {
122 	SBI_SRST_RESET_REASON_NONE = 0,
123 	SBI_SRST_RESET_REASON_SYS_FAILURE,
124 };
125 
126 enum sbi_ext_susp_fid {
127 	SBI_EXT_SUSP_SYSTEM_SUSPEND = 0,
128 };
129 
130 enum sbi_ext_susp_sleep_type {
131 	SBI_SUSP_SLEEP_TYPE_SUSPEND_TO_RAM = 0,
132 };
133 
134 enum sbi_ext_pmu_fid {
135 	SBI_EXT_PMU_NUM_COUNTERS = 0,
136 	SBI_EXT_PMU_COUNTER_GET_INFO,
137 	SBI_EXT_PMU_COUNTER_CFG_MATCH,
138 	SBI_EXT_PMU_COUNTER_START,
139 	SBI_EXT_PMU_COUNTER_STOP,
140 	SBI_EXT_PMU_COUNTER_FW_READ,
141 	SBI_EXT_PMU_COUNTER_FW_READ_HI,
142 	SBI_EXT_PMU_SNAPSHOT_SET_SHMEM,
143 	SBI_EXT_PMU_EVENT_GET_INFO,
144 };
145 
146 union sbi_pmu_ctr_info {
147 	unsigned long value;
148 	struct {
149 		unsigned long csr:12;
150 		unsigned long width:6;
151 #if __riscv_xlen == 32
152 		unsigned long reserved:13;
153 #else
154 		unsigned long reserved:45;
155 #endif
156 		unsigned long type:1;
157 	};
158 };
159 
160 /* Data structure to contain the pmu snapshot data */
161 struct riscv_pmu_snapshot_data {
162 	u64 ctr_overflow_mask;
163 	u64 ctr_values[64];
164 	u64 reserved[447];
165 };
166 
167 struct riscv_pmu_event_info {
168 	u32 event_idx;
169 	u32 output;
170 	u64 event_data;
171 };
172 
173 #define RISCV_PMU_EVENT_INFO_OUTPUT_MASK 0x01
174 
175 #define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0)
176 #define RISCV_PMU_PLAT_FW_EVENT_MASK GENMASK_ULL(61, 0)
177 /* SBI v3.0 allows extended hpmeventX width value */
178 #define RISCV_PMU_RAW_EVENT_V2_MASK GENMASK_ULL(55, 0)
179 #define RISCV_PMU_RAW_EVENT_IDX 0x20000
180 #define RISCV_PMU_RAW_EVENT_V2_IDX 0x30000
181 #define RISCV_PLAT_FW_EVENT	0xFFFF
182 
183 /** General pmu event codes specified in SBI PMU extension */
184 enum sbi_pmu_hw_generic_events_t {
185 	SBI_PMU_HW_NO_EVENT			= 0,
186 	SBI_PMU_HW_CPU_CYCLES			= 1,
187 	SBI_PMU_HW_INSTRUCTIONS			= 2,
188 	SBI_PMU_HW_CACHE_REFERENCES		= 3,
189 	SBI_PMU_HW_CACHE_MISSES			= 4,
190 	SBI_PMU_HW_BRANCH_INSTRUCTIONS		= 5,
191 	SBI_PMU_HW_BRANCH_MISSES		= 6,
192 	SBI_PMU_HW_BUS_CYCLES			= 7,
193 	SBI_PMU_HW_STALLED_CYCLES_FRONTEND	= 8,
194 	SBI_PMU_HW_STALLED_CYCLES_BACKEND	= 9,
195 	SBI_PMU_HW_REF_CPU_CYCLES		= 10,
196 
197 	SBI_PMU_HW_GENERAL_MAX,
198 };
199 
200 /**
201  * Special "firmware" events provided by the firmware, even if the hardware
202  * does not support performance events. These events are encoded as a raw
203  * event type in Linux kernel perf framework.
204  */
205 enum sbi_pmu_fw_generic_events_t {
206 	SBI_PMU_FW_MISALIGNED_LOAD	= 0,
207 	SBI_PMU_FW_MISALIGNED_STORE	= 1,
208 	SBI_PMU_FW_ACCESS_LOAD		= 2,
209 	SBI_PMU_FW_ACCESS_STORE		= 3,
210 	SBI_PMU_FW_ILLEGAL_INSN		= 4,
211 	SBI_PMU_FW_SET_TIMER		= 5,
212 	SBI_PMU_FW_IPI_SENT		= 6,
213 	SBI_PMU_FW_IPI_RCVD		= 7,
214 	SBI_PMU_FW_FENCE_I_SENT		= 8,
215 	SBI_PMU_FW_FENCE_I_RCVD		= 9,
216 	SBI_PMU_FW_SFENCE_VMA_SENT	= 10,
217 	SBI_PMU_FW_SFENCE_VMA_RCVD	= 11,
218 	SBI_PMU_FW_SFENCE_VMA_ASID_SENT	= 12,
219 	SBI_PMU_FW_SFENCE_VMA_ASID_RCVD	= 13,
220 
221 	SBI_PMU_FW_HFENCE_GVMA_SENT	= 14,
222 	SBI_PMU_FW_HFENCE_GVMA_RCVD	= 15,
223 	SBI_PMU_FW_HFENCE_GVMA_VMID_SENT = 16,
224 	SBI_PMU_FW_HFENCE_GVMA_VMID_RCVD = 17,
225 
226 	SBI_PMU_FW_HFENCE_VVMA_SENT	= 18,
227 	SBI_PMU_FW_HFENCE_VVMA_RCVD	= 19,
228 	SBI_PMU_FW_HFENCE_VVMA_ASID_SENT = 20,
229 	SBI_PMU_FW_HFENCE_VVMA_ASID_RCVD = 21,
230 	SBI_PMU_FW_MAX,
231 };
232 
233 /* SBI PMU event types */
234 enum sbi_pmu_event_type {
235 	SBI_PMU_EVENT_TYPE_HW = 0x0,
236 	SBI_PMU_EVENT_TYPE_CACHE = 0x1,
237 	SBI_PMU_EVENT_TYPE_RAW = 0x2,
238 	SBI_PMU_EVENT_TYPE_RAW_V2 = 0x3,
239 	SBI_PMU_EVENT_TYPE_FW = 0xf,
240 };
241 
242 /* SBI PMU event types */
243 enum sbi_pmu_ctr_type {
244 	SBI_PMU_CTR_TYPE_HW = 0x0,
245 	SBI_PMU_CTR_TYPE_FW,
246 };
247 
248 /* Helper macros to decode event idx */
249 #define SBI_PMU_EVENT_IDX_OFFSET 20
250 #define SBI_PMU_EVENT_IDX_MASK 0xFFFFF
251 #define SBI_PMU_EVENT_IDX_CODE_MASK 0xFFFF
252 #define SBI_PMU_EVENT_IDX_TYPE_MASK 0xF0000
253 #define SBI_PMU_EVENT_RAW_IDX 0x20000
254 #define SBI_PMU_FIXED_CTR_MASK 0x07
255 
256 #define SBI_PMU_EVENT_CACHE_ID_CODE_MASK 0xFFF8
257 #define SBI_PMU_EVENT_CACHE_OP_ID_CODE_MASK 0x06
258 #define SBI_PMU_EVENT_CACHE_RESULT_ID_CODE_MASK 0x01
259 
260 #define SBI_PMU_EVENT_CACHE_ID_SHIFT 3
261 #define SBI_PMU_EVENT_CACHE_OP_SHIFT 1
262 
263 #define SBI_PMU_EVENT_IDX_INVALID 0xFFFFFFFF
264 
265 /* Flags defined for config matching function */
266 #define SBI_PMU_CFG_FLAG_SKIP_MATCH	BIT(0)
267 #define SBI_PMU_CFG_FLAG_CLEAR_VALUE	BIT(1)
268 #define SBI_PMU_CFG_FLAG_AUTO_START	BIT(2)
269 #define SBI_PMU_CFG_FLAG_SET_VUINH	BIT(3)
270 #define SBI_PMU_CFG_FLAG_SET_VSINH	BIT(4)
271 #define SBI_PMU_CFG_FLAG_SET_UINH	BIT(5)
272 #define SBI_PMU_CFG_FLAG_SET_SINH	BIT(6)
273 #define SBI_PMU_CFG_FLAG_SET_MINH	BIT(7)
274 
275 /* Flags defined for counter start function */
276 #define SBI_PMU_START_FLAG_SET_INIT_VALUE BIT(0)
277 #define SBI_PMU_START_FLAG_INIT_SNAPSHOT BIT(1)
278 
279 /* Flags defined for counter stop function */
280 #define SBI_PMU_STOP_FLAG_RESET BIT(0)
281 #define SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT BIT(1)
282 
283 enum sbi_ext_dbcn_fid {
284 	SBI_EXT_DBCN_CONSOLE_WRITE = 0,
285 	SBI_EXT_DBCN_CONSOLE_READ = 1,
286 	SBI_EXT_DBCN_CONSOLE_WRITE_BYTE = 2,
287 };
288 
289 /* SBI STA (steal-time accounting) extension */
290 enum sbi_ext_sta_fid {
291 	SBI_EXT_STA_STEAL_TIME_SET_SHMEM = 0,
292 };
293 
294 struct sbi_sta_struct {
295 	__le32 sequence;
296 	__le32 flags;
297 	__le64 steal;
298 	u8 preempted;
299 	u8 pad[47];
300 } __packed;
301 
302 #define SBI_SHMEM_DISABLE		-1
303 
304 enum sbi_ext_nacl_fid {
305 	SBI_EXT_NACL_PROBE_FEATURE = 0x0,
306 	SBI_EXT_NACL_SET_SHMEM = 0x1,
307 	SBI_EXT_NACL_SYNC_CSR = 0x2,
308 	SBI_EXT_NACL_SYNC_HFENCE = 0x3,
309 	SBI_EXT_NACL_SYNC_SRET = 0x4,
310 };
311 
312 enum sbi_ext_nacl_feature {
313 	SBI_NACL_FEAT_SYNC_CSR = 0x0,
314 	SBI_NACL_FEAT_SYNC_HFENCE = 0x1,
315 	SBI_NACL_FEAT_SYNC_SRET = 0x2,
316 	SBI_NACL_FEAT_AUTOSWAP_CSR = 0x3,
317 };
318 
319 #define SBI_NACL_SHMEM_ADDR_SHIFT	12
320 #define SBI_NACL_SHMEM_SCRATCH_OFFSET	0x0000
321 #define SBI_NACL_SHMEM_SCRATCH_SIZE	0x1000
322 #define SBI_NACL_SHMEM_SRET_OFFSET	0x0000
323 #define SBI_NACL_SHMEM_SRET_SIZE	0x0200
324 #define SBI_NACL_SHMEM_AUTOSWAP_OFFSET	(SBI_NACL_SHMEM_SRET_OFFSET + \
325 					 SBI_NACL_SHMEM_SRET_SIZE)
326 #define SBI_NACL_SHMEM_AUTOSWAP_SIZE	0x0080
327 #define SBI_NACL_SHMEM_UNUSED_OFFSET	(SBI_NACL_SHMEM_AUTOSWAP_OFFSET + \
328 					 SBI_NACL_SHMEM_AUTOSWAP_SIZE)
329 #define SBI_NACL_SHMEM_UNUSED_SIZE	0x0580
330 #define SBI_NACL_SHMEM_HFENCE_OFFSET	(SBI_NACL_SHMEM_UNUSED_OFFSET + \
331 					 SBI_NACL_SHMEM_UNUSED_SIZE)
332 #define SBI_NACL_SHMEM_HFENCE_SIZE	0x0780
333 #define SBI_NACL_SHMEM_DBITMAP_OFFSET	(SBI_NACL_SHMEM_HFENCE_OFFSET + \
334 					 SBI_NACL_SHMEM_HFENCE_SIZE)
335 #define SBI_NACL_SHMEM_DBITMAP_SIZE	0x0080
336 #define SBI_NACL_SHMEM_CSR_OFFSET	(SBI_NACL_SHMEM_DBITMAP_OFFSET + \
337 					 SBI_NACL_SHMEM_DBITMAP_SIZE)
338 #define SBI_NACL_SHMEM_CSR_SIZE		((__riscv_xlen / 8) * 1024)
339 #define SBI_NACL_SHMEM_SIZE		(SBI_NACL_SHMEM_CSR_OFFSET + \
340 					 SBI_NACL_SHMEM_CSR_SIZE)
341 
342 #define SBI_NACL_SHMEM_CSR_INDEX(__csr_num)	\
343 		((((__csr_num) & 0xc00) >> 2) | ((__csr_num) & 0xff))
344 
345 #define SBI_NACL_SHMEM_HFENCE_ENTRY_SZ		((__riscv_xlen / 8) * 4)
346 #define SBI_NACL_SHMEM_HFENCE_ENTRY_MAX		\
347 		(SBI_NACL_SHMEM_HFENCE_SIZE /	\
348 		 SBI_NACL_SHMEM_HFENCE_ENTRY_SZ)
349 #define SBI_NACL_SHMEM_HFENCE_ENTRY(__num)	\
350 		(SBI_NACL_SHMEM_HFENCE_OFFSET +	\
351 		 (__num) * SBI_NACL_SHMEM_HFENCE_ENTRY_SZ)
352 #define SBI_NACL_SHMEM_HFENCE_ENTRY_CONFIG(__num)	\
353 		SBI_NACL_SHMEM_HFENCE_ENTRY(__num)
354 #define SBI_NACL_SHMEM_HFENCE_ENTRY_PNUM(__num)\
355 		(SBI_NACL_SHMEM_HFENCE_ENTRY(__num) + (__riscv_xlen / 8))
356 #define SBI_NACL_SHMEM_HFENCE_ENTRY_PCOUNT(__num)\
357 		(SBI_NACL_SHMEM_HFENCE_ENTRY(__num) + \
358 		 ((__riscv_xlen / 8) * 3))
359 
360 #define SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_BITS	1
361 #define SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_SHIFT	\
362 		(__riscv_xlen - SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_BITS)
363 #define SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_MASK	\
364 		((1UL << SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_BITS) - 1)
365 #define SBI_NACL_SHMEM_HFENCE_CONFIG_PEND		\
366 		(SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_MASK << \
367 		 SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_SHIFT)
368 
369 #define SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD1_BITS	3
370 #define SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD1_SHIFT \
371 		(SBI_NACL_SHMEM_HFENCE_CONFIG_PEND_SHIFT - \
372 		 SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD1_BITS)
373 
374 #define SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_BITS	4
375 #define SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_SHIFT	\
376 		(SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD1_SHIFT - \
377 		 SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_BITS)
378 #define SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_MASK	\
379 		((1UL << SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_BITS) - 1)
380 
381 #define SBI_NACL_SHMEM_HFENCE_TYPE_GVMA		0x0
382 #define SBI_NACL_SHMEM_HFENCE_TYPE_GVMA_ALL	0x1
383 #define SBI_NACL_SHMEM_HFENCE_TYPE_GVMA_VMID	0x2
384 #define SBI_NACL_SHMEM_HFENCE_TYPE_GVMA_VMID_ALL 0x3
385 #define SBI_NACL_SHMEM_HFENCE_TYPE_VVMA		0x4
386 #define SBI_NACL_SHMEM_HFENCE_TYPE_VVMA_ALL	0x5
387 #define SBI_NACL_SHMEM_HFENCE_TYPE_VVMA_ASID	0x6
388 #define SBI_NACL_SHMEM_HFENCE_TYPE_VVMA_ASID_ALL 0x7
389 
390 #define SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD2_BITS	1
391 #define SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD2_SHIFT \
392 		(SBI_NACL_SHMEM_HFENCE_CONFIG_TYPE_SHIFT - \
393 		 SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD2_BITS)
394 
395 #define SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_BITS	7
396 #define SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_SHIFT \
397 		(SBI_NACL_SHMEM_HFENCE_CONFIG_RSVD2_SHIFT - \
398 		 SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_BITS)
399 #define SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_MASK	\
400 		((1UL << SBI_NACL_SHMEM_HFENCE_CONFIG_ORDER_BITS) - 1)
401 #define SBI_NACL_SHMEM_HFENCE_ORDER_BASE	12
402 
403 #if __riscv_xlen == 32
404 #define SBI_NACL_SHMEM_HFENCE_CONFIG_ASID_BITS	9
405 #define SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_BITS	7
406 #else
407 #define SBI_NACL_SHMEM_HFENCE_CONFIG_ASID_BITS	16
408 #define SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_BITS	14
409 #endif
410 #define SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_SHIFT	\
411 				SBI_NACL_SHMEM_HFENCE_CONFIG_ASID_BITS
412 #define SBI_NACL_SHMEM_HFENCE_CONFIG_ASID_MASK	\
413 		((1UL << SBI_NACL_SHMEM_HFENCE_CONFIG_ASID_BITS) - 1)
414 #define SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_MASK	\
415 		((1UL << SBI_NACL_SHMEM_HFENCE_CONFIG_VMID_BITS) - 1)
416 
417 #define SBI_NACL_SHMEM_AUTOSWAP_FLAG_HSTATUS	BIT(0)
418 #define SBI_NACL_SHMEM_AUTOSWAP_HSTATUS		((__riscv_xlen / 8) * 1)
419 
420 #define SBI_NACL_SHMEM_SRET_X(__i)		((__riscv_xlen / 8) * (__i))
421 #define SBI_NACL_SHMEM_SRET_X_LAST		31
422 
423 /* SBI function IDs for FW feature extension */
424 #define SBI_EXT_FWFT_SET		0x0
425 #define SBI_EXT_FWFT_GET		0x1
426 
427 enum sbi_fwft_feature_t {
428 	SBI_FWFT_MISALIGNED_EXC_DELEG		= 0x0,
429 	SBI_FWFT_LANDING_PAD			= 0x1,
430 	SBI_FWFT_SHADOW_STACK			= 0x2,
431 	SBI_FWFT_DOUBLE_TRAP			= 0x3,
432 	SBI_FWFT_PTE_AD_HW_UPDATING		= 0x4,
433 	SBI_FWFT_POINTER_MASKING_PMLEN		= 0x5,
434 	SBI_FWFT_LOCAL_RESERVED_START		= 0x6,
435 	SBI_FWFT_LOCAL_RESERVED_END		= 0x3fffffff,
436 	SBI_FWFT_LOCAL_PLATFORM_START		= 0x40000000,
437 	SBI_FWFT_LOCAL_PLATFORM_END		= 0x7fffffff,
438 
439 	SBI_FWFT_GLOBAL_RESERVED_START		= 0x80000000,
440 	SBI_FWFT_GLOBAL_RESERVED_END		= 0xbfffffff,
441 	SBI_FWFT_GLOBAL_PLATFORM_START		= 0xc0000000,
442 	SBI_FWFT_GLOBAL_PLATFORM_END		= 0xffffffff,
443 };
444 
445 #define SBI_FWFT_PLATFORM_FEATURE_BIT		BIT(30)
446 #define SBI_FWFT_GLOBAL_FEATURE_BIT		BIT(31)
447 
448 #define SBI_FWFT_SET_FLAG_LOCK			BIT(0)
449 
450 enum sbi_ext_mpxy_fid {
451 	SBI_EXT_MPXY_GET_SHMEM_SIZE,
452 	SBI_EXT_MPXY_SET_SHMEM,
453 	SBI_EXT_MPXY_GET_CHANNEL_IDS,
454 	SBI_EXT_MPXY_READ_ATTRS,
455 	SBI_EXT_MPXY_WRITE_ATTRS,
456 	SBI_EXT_MPXY_SEND_MSG_WITH_RESP,
457 	SBI_EXT_MPXY_SEND_MSG_WITHOUT_RESP,
458 	SBI_EXT_MPXY_GET_NOTIFICATION_EVENTS,
459 };
460 
461 enum sbi_mpxy_attribute_id {
462 	/* Standard channel attributes managed by MPXY framework */
463 	SBI_MPXY_ATTR_MSG_PROT_ID		= 0x00000000,
464 	SBI_MPXY_ATTR_MSG_PROT_VER		= 0x00000001,
465 	SBI_MPXY_ATTR_MSG_MAX_LEN		= 0x00000002,
466 	SBI_MPXY_ATTR_MSG_SEND_TIMEOUT		= 0x00000003,
467 	SBI_MPXY_ATTR_MSG_COMPLETION_TIMEOUT	= 0x00000004,
468 	SBI_MPXY_ATTR_CHANNEL_CAPABILITY	= 0x00000005,
469 	SBI_MPXY_ATTR_SSE_EVENT_ID		= 0x00000006,
470 	SBI_MPXY_ATTR_MSI_CONTROL		= 0x00000007,
471 	SBI_MPXY_ATTR_MSI_ADDR_LO		= 0x00000008,
472 	SBI_MPXY_ATTR_MSI_ADDR_HI		= 0x00000009,
473 	SBI_MPXY_ATTR_MSI_DATA			= 0x0000000A,
474 	SBI_MPXY_ATTR_EVENTS_STATE_CONTROL	= 0x0000000B,
475 	SBI_MPXY_ATTR_STD_ATTR_MAX_IDX,
476 	/*
477 	 * Message protocol specific attributes, managed by
478 	 * the message protocol specification.
479 	 */
480 	SBI_MPXY_ATTR_MSGPROTO_ATTR_START	= 0x80000000,
481 	SBI_MPXY_ATTR_MSGPROTO_ATTR_END		= 0xffffffff
482 };
483 
484 /* Possible values of MSG_PROT_ID attribute as-per SBI v3.0 (or higher) */
485 enum sbi_mpxy_msgproto_id {
486 	SBI_MPXY_MSGPROTO_RPMI_ID = 0x0,
487 };
488 
489 /* RPMI message protocol specific MPXY attributes */
490 enum sbi_mpxy_rpmi_attribute_id {
491 	SBI_MPXY_RPMI_ATTR_SERVICEGROUP_ID = SBI_MPXY_ATTR_MSGPROTO_ATTR_START,
492 	SBI_MPXY_RPMI_ATTR_SERVICEGROUP_VERSION,
493 	SBI_MPXY_RPMI_ATTR_IMPL_ID,
494 	SBI_MPXY_RPMI_ATTR_IMPL_VERSION,
495 	SBI_MPXY_RPMI_ATTR_MAX_ID
496 };
497 
498 /* Encoding of MSG_PROT_VER attribute */
499 #define SBI_MPXY_MSG_PROT_VER_MAJOR(__ver)	upper_16_bits(__ver)
500 #define SBI_MPXY_MSG_PROT_VER_MINOR(__ver)	lower_16_bits(__ver)
501 #define SBI_MPXY_MSG_PROT_MKVER(__maj, __min)	(((u32)(__maj) << 16) | (u16)(__min))
502 
503 /* Capabilities available through CHANNEL_CAPABILITY attribute */
504 #define SBI_MPXY_CHAN_CAP_MSI			BIT(0)
505 #define SBI_MPXY_CHAN_CAP_SSE			BIT(1)
506 #define SBI_MPXY_CHAN_CAP_EVENTS_STATE		BIT(2)
507 #define SBI_MPXY_CHAN_CAP_SEND_WITH_RESP	BIT(3)
508 #define SBI_MPXY_CHAN_CAP_SEND_WITHOUT_RESP	BIT(4)
509 #define SBI_MPXY_CHAN_CAP_GET_NOTIFICATIONS	BIT(5)
510 
511 /* SBI debug triggers function IDs */
512 enum sbi_ext_dbtr_fid {
513 	SBI_EXT_DBTR_NUM_TRIGGERS = 0,
514 	SBI_EXT_DBTR_SETUP_SHMEM,
515 	SBI_EXT_DBTR_TRIG_READ,
516 	SBI_EXT_DBTR_TRIG_INSTALL,
517 	SBI_EXT_DBTR_TRIG_UPDATE,
518 	SBI_EXT_DBTR_TRIG_UNINSTALL,
519 	SBI_EXT_DBTR_TRIG_ENABLE,
520 	SBI_EXT_DBTR_TRIG_DISABLE,
521 };
522 
523 struct sbi_dbtr_data_msg {
524 	unsigned long tstate;
525 	unsigned long tdata1;
526 	unsigned long tdata2;
527 	unsigned long tdata3;
528 };
529 
530 struct sbi_dbtr_id_msg {
531 	unsigned long idx;
532 };
533 
534 union sbi_dbtr_shmem_entry {
535 	struct sbi_dbtr_data_msg data;
536 	struct sbi_dbtr_id_msg id;
537 };
538 
539 /* SBI spec version fields */
540 #define SBI_SPEC_VERSION_DEFAULT	0x1
541 #define SBI_SPEC_VERSION_MAJOR_SHIFT	24
542 #define SBI_SPEC_VERSION_MAJOR_MASK	0x7f
543 #define SBI_SPEC_VERSION_MINOR_MASK	0xffffff
544 
545 /* SBI return error codes */
546 #define SBI_SUCCESS		0
547 #define SBI_ERR_FAILURE		-1
548 #define SBI_ERR_NOT_SUPPORTED	-2
549 #define SBI_ERR_INVALID_PARAM	-3
550 #define SBI_ERR_DENIED		-4
551 #define SBI_ERR_INVALID_ADDRESS	-5
552 #define SBI_ERR_ALREADY_AVAILABLE -6
553 #define SBI_ERR_ALREADY_STARTED -7
554 #define SBI_ERR_ALREADY_STOPPED -8
555 #define SBI_ERR_NO_SHMEM	-9
556 #define SBI_ERR_INVALID_STATE	-10
557 #define SBI_ERR_BAD_RANGE	-11
558 #define SBI_ERR_TIMEOUT		-12
559 #define SBI_ERR_IO		-13
560 #define SBI_ERR_DENIED_LOCKED	-14
561 
562 extern unsigned long sbi_spec_version;
563 struct sbiret {
564 	long error;
565 	long value;
566 };
567 
568 #ifdef CONFIG_TRACEPOINTS
569 DECLARE_TRACEPOINT(sbi_call);
570 DECLARE_TRACEPOINT(sbi_return);
571 extern void do_trace_sbi_call(int ext, int fid);
572 extern void do_trace_sbi_return(int ext, long error, long value);
573 #else
574 static inline void do_trace_sbi_call(int ext, int fid) {};
575 static inline void do_trace_sbi_return(int ext, long error, long value) {};
576 #endif
577 
578 void sbi_init(void);
579 long __sbi_base_ecall(int fid);
580 
581 #define __sbi_ecall_args2(e, f) \
582 	uintptr_t __ta7 = (uintptr_t)(e); \
583 	uintptr_t __ta6 = (uintptr_t)(f)
584 #define __sbi_ecall_args3(e, f, a0) \
585 	__sbi_ecall_args2(e, f); uintptr_t __ta0 = (uintptr_t)(a0)
586 #define __sbi_ecall_args4(e, f, a0, a1) \
587 	__sbi_ecall_args3(e, f, a0); uintptr_t __ta1 = (uintptr_t)(a1)
588 #define __sbi_ecall_args5(e, f, a0, a1, a2) \
589 	__sbi_ecall_args4(e, f, a0, a1); uintptr_t __ta2 = (uintptr_t)(a2)
590 #define __sbi_ecall_args6(e, f, a0, a1, a2, a3) \
591 	__sbi_ecall_args5(e, f, a0, a1, a2); uintptr_t __ta3 = (uintptr_t)(a3)
592 #define __sbi_ecall_args7(e, f, a0, a1, a2, a3, a4) \
593 	__sbi_ecall_args6(e, f, a0, a1, a2, a3); uintptr_t __ta4 = (uintptr_t)(a4)
594 #define __sbi_ecall_args8(e, f, a0, a1, a2, a3, a4, a5) \
595 	__sbi_ecall_args7(e, f, a0, a1, a2, a3, a4); uintptr_t __ta5 = (uintptr_t)(a5)
596 
597 #define __sbi_ecall_regs2 \
598 	register uintptr_t __a7 asm ("a7") = __ta7; \
599 	register uintptr_t __a6 asm ("a6") = __ta6
600 #define __sbi_ecall_regs3  __sbi_ecall_regs2; register uintptr_t __a0 asm ("a0") = __ta0
601 #define __sbi_ecall_regs4  __sbi_ecall_regs3; register uintptr_t __a1 asm ("a1") = __ta1
602 #define __sbi_ecall_regs5  __sbi_ecall_regs4; register uintptr_t __a2 asm ("a2") = __ta2
603 #define __sbi_ecall_regs6  __sbi_ecall_regs5; register uintptr_t __a3 asm ("a3") = __ta3
604 #define __sbi_ecall_regs7  __sbi_ecall_regs6; register uintptr_t __a4 asm ("a4") = __ta4
605 #define __sbi_ecall_regs8  __sbi_ecall_regs7; register uintptr_t __a5 asm ("a5") = __ta5
606 
607 #define __sbi_ecall_constraints1                            "r" (__a7)
608 #define __sbi_ecall_constraints2  __sbi_ecall_constraints1, "r" (__a6)
609 #define __sbi_ecall_constraints3  __sbi_ecall_constraints2, "r" (__a0)
610 #define __sbi_ecall_constraints4  __sbi_ecall_constraints3, "r" (__a1)
611 #define __sbi_ecall_constraints5  __sbi_ecall_constraints4, "r" (__a2)
612 #define __sbi_ecall_constraints6  __sbi_ecall_constraints5, "r" (__a3)
613 #define __sbi_ecall_constraints7  __sbi_ecall_constraints6, "r" (__a4)
614 #define __sbi_ecall_constraints8  __sbi_ecall_constraints7, "r" (__a5)
615 
616 #define __sbi_ecall_trace_call() \
617 	if (tracepoint_enabled(sbi_call)) \
618 		do_trace_sbi_call(__ta7, __ta6)
619 
620 #define __sbi_ecall_trace_return() \
621 	if (tracepoint_enabled(sbi_return)) \
622 		do_trace_sbi_return(__ta7, __ret.error, __ret.value)
623 
624 /*
625  * Clear a1 to avoid leaking unrelated kernel state through tracepoints in case
626  * the register doesn't get overwritten by the ecall nor the arguments.
627  */
628 #define sbi_ecall(A...) \
629 ({ \
630 	CONCATENATE(__sbi_ecall_args, COUNT_ARGS(A))(A); \
631 	__sbi_ecall_trace_call(); \
632 	register uintptr_t __r0 asm ("a0"); \
633 	register uintptr_t __r1 asm ("a1") = 0; \
634 	CONCATENATE(__sbi_ecall_regs, COUNT_ARGS(A)); \
635 	asm volatile ("ecall" \
636 			: "=r" (__r0), "=r" (__r1) \
637 			: CONCATENATE(__sbi_ecall_constraints, COUNT_ARGS(A)) \
638 			: "memory"); \
639 	struct sbiret __ret = {.error = __r0, .value = __r1}; \
640 	__sbi_ecall_trace_return(); \
641 	__ret; \
642 })
643 
644 #ifdef CONFIG_RISCV_SBI_V01
645 void sbi_console_putchar(int ch);
646 int sbi_console_getchar(void);
647 #else
648 static inline void sbi_console_putchar(int ch) { }
649 static inline int sbi_console_getchar(void) { return -ENOENT; }
650 #endif
651 long sbi_get_mvendorid(void);
652 long sbi_get_marchid(void);
653 long sbi_get_mimpid(void);
654 void sbi_set_timer(uint64_t stime_value);
655 void sbi_shutdown(void);
656 void sbi_send_ipi(unsigned int cpu);
657 int sbi_remote_fence_i(const struct cpumask *cpu_mask);
658 
659 int sbi_remote_sfence_vma_asid(const struct cpumask *cpu_mask,
660 				unsigned long start,
661 				unsigned long size,
662 				unsigned long asid);
663 int sbi_remote_hfence_gvma(const struct cpumask *cpu_mask,
664 			   unsigned long start,
665 			   unsigned long size);
666 int sbi_remote_hfence_gvma_vmid(const struct cpumask *cpu_mask,
667 				unsigned long start,
668 				unsigned long size,
669 				unsigned long vmid);
670 int sbi_remote_hfence_vvma(const struct cpumask *cpu_mask,
671 			   unsigned long start,
672 			   unsigned long size);
673 int sbi_remote_hfence_vvma_asid(const struct cpumask *cpu_mask,
674 				unsigned long start,
675 				unsigned long size,
676 				unsigned long asid);
677 long sbi_probe_extension(int ext);
678 
679 int sbi_fwft_set(u32 feature, unsigned long value, unsigned long flags);
680 int sbi_fwft_set_cpumask(const cpumask_t *mask, u32 feature,
681 			 unsigned long value, unsigned long flags);
682 /**
683  * sbi_fwft_set_online_cpus() - Set a feature on all online cpus
684  * @feature: The feature to be set
685  * @value: The feature value to be set
686  * @flags: FWFT feature set flags
687  *
688  * Return: 0 on success, appropriate linux error code otherwise.
689  */
690 static inline int sbi_fwft_set_online_cpus(u32 feature, unsigned long value,
691 					   unsigned long flags)
692 {
693 	return sbi_fwft_set_cpumask(cpu_online_mask, feature, value, flags);
694 }
695 
696 /* Check if current SBI specification version is 0.1 or not */
697 static inline int sbi_spec_is_0_1(void)
698 {
699 	return (sbi_spec_version == SBI_SPEC_VERSION_DEFAULT) ? 1 : 0;
700 }
701 
702 /* Get the major version of SBI */
703 static inline unsigned long sbi_major_version(void)
704 {
705 	return (sbi_spec_version >> SBI_SPEC_VERSION_MAJOR_SHIFT) &
706 		SBI_SPEC_VERSION_MAJOR_MASK;
707 }
708 
709 /* Get the minor version of SBI */
710 static inline unsigned long sbi_minor_version(void)
711 {
712 	return sbi_spec_version & SBI_SPEC_VERSION_MINOR_MASK;
713 }
714 
715 /* Make SBI version */
716 static inline unsigned long sbi_mk_version(unsigned long major,
717 					    unsigned long minor)
718 {
719 	return ((major & SBI_SPEC_VERSION_MAJOR_MASK) << SBI_SPEC_VERSION_MAJOR_SHIFT)
720 		| (minor & SBI_SPEC_VERSION_MINOR_MASK);
721 }
722 
723 static inline int sbi_err_map_linux_errno(int err)
724 {
725 	switch (err) {
726 	case SBI_SUCCESS:
727 		return 0;
728 	case SBI_ERR_DENIED:
729 	case SBI_ERR_DENIED_LOCKED:
730 		return -EPERM;
731 	case SBI_ERR_INVALID_PARAM:
732 	case SBI_ERR_INVALID_STATE:
733 		return -EINVAL;
734 	case SBI_ERR_BAD_RANGE:
735 		return -ERANGE;
736 	case SBI_ERR_INVALID_ADDRESS:
737 		return -EFAULT;
738 	case SBI_ERR_NO_SHMEM:
739 		return -ENOMEM;
740 	case SBI_ERR_TIMEOUT:
741 		return -ETIMEDOUT;
742 	case SBI_ERR_IO:
743 		return -EIO;
744 	case SBI_ERR_NOT_SUPPORTED:
745 	case SBI_ERR_FAILURE:
746 	default:
747 		return -ENOTSUPP;
748 	};
749 }
750 
751 extern bool sbi_debug_console_available;
752 int sbi_debug_console_write(const char *bytes, unsigned int num_bytes);
753 int sbi_debug_console_read(char *bytes, unsigned int num_bytes);
754 
755 #else /* CONFIG_RISCV_SBI */
756 static inline int sbi_remote_fence_i(const struct cpumask *cpu_mask) { return -1; }
757 static inline void sbi_init(void) {}
758 #endif /* CONFIG_RISCV_SBI */
759 
760 unsigned long riscv_get_mvendorid(void);
761 unsigned long riscv_get_marchid(void);
762 unsigned long riscv_cached_mvendorid(unsigned int cpu_id);
763 unsigned long riscv_cached_marchid(unsigned int cpu_id);
764 unsigned long riscv_cached_mimpid(unsigned int cpu_id);
765 
766 #if IS_ENABLED(CONFIG_SMP) && IS_ENABLED(CONFIG_RISCV_SBI)
767 DECLARE_STATIC_KEY_FALSE(riscv_sbi_for_rfence);
768 #define riscv_use_sbi_for_rfence() \
769 	static_branch_unlikely(&riscv_sbi_for_rfence)
770 void sbi_ipi_init(void);
771 #else
772 static inline bool riscv_use_sbi_for_rfence(void) { return false; }
773 static inline void sbi_ipi_init(void) { }
774 #endif
775 
776 #endif /* _ASM_RISCV_SBI_H */
777