xref: /freebsd/sys/riscv/include/sbi.h (revision c55272fd)
15f8228b2SRuslan Bukin /*-
2af19cc59SRuslan Bukin  * Copyright (c) 2016-2017 Ruslan Bukin <br@bsdpad.com>
35f8228b2SRuslan Bukin  * All rights reserved.
40a0f40c7SMitchell Horne  * Copyright (c) 2019 Mitchell Horne <mhorne@FreeBSD.org>
55f8228b2SRuslan Bukin  *
65f8228b2SRuslan Bukin  * Portions of this software were developed by SRI International and the
75f8228b2SRuslan Bukin  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
85f8228b2SRuslan Bukin  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
95f8228b2SRuslan Bukin  *
105f8228b2SRuslan Bukin  * Portions of this software were developed by the University of Cambridge
115f8228b2SRuslan Bukin  * Computer Laboratory as part of the CTSRD Project, with support from the
125f8228b2SRuslan Bukin  * UK Higher Education Innovation Fund (HEIF).
135f8228b2SRuslan Bukin  *
145f8228b2SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
155f8228b2SRuslan Bukin  * modification, are permitted provided that the following conditions
165f8228b2SRuslan Bukin  * are met:
175f8228b2SRuslan Bukin  * 1. Redistributions of source code must retain the above copyright
185f8228b2SRuslan Bukin  *    notice, this list of conditions and the following disclaimer.
195f8228b2SRuslan Bukin  * 2. Redistributions in binary form must reproduce the above copyright
205f8228b2SRuslan Bukin  *    notice, this list of conditions and the following disclaimer in the
215f8228b2SRuslan Bukin  *    documentation and/or other materials provided with the distribution.
225f8228b2SRuslan Bukin  *
235f8228b2SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
245f8228b2SRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
255f8228b2SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
265f8228b2SRuslan Bukin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
275f8228b2SRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
285f8228b2SRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
295f8228b2SRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
305f8228b2SRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
315f8228b2SRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
325f8228b2SRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
335f8228b2SRuslan Bukin  * SUCH DAMAGE.
345f8228b2SRuslan Bukin  */
355f8228b2SRuslan Bukin 
365f8228b2SRuslan Bukin #ifndef _MACHINE_SBI_H_
375f8228b2SRuslan Bukin #define	_MACHINE_SBI_H_
385f8228b2SRuslan Bukin 
390a0f40c7SMitchell Horne /* SBI Specification Version */
400a0f40c7SMitchell Horne #define	SBI_SPEC_VERS_MAJOR_OFFSET	24
410a0f40c7SMitchell Horne #define	SBI_SPEC_VERS_MAJOR_MASK	(0x7F << SBI_SPEC_VERS_MAJOR_OFFSET)
420a0f40c7SMitchell Horne #define	SBI_SPEC_VERS_MINOR_OFFSET	0
430a0f40c7SMitchell Horne #define	SBI_SPEC_VERS_MINOR_MASK	(0xFFFFFF << SBI_SPEC_VERS_MINOR_OFFSET)
440a0f40c7SMitchell Horne 
450a0f40c7SMitchell Horne /* SBI Implementation IDs */
460a0f40c7SMitchell Horne #define	SBI_IMPL_ID_BBL			0
470a0f40c7SMitchell Horne #define	SBI_IMPL_ID_OPENSBI		1
4825de8fb6SMitchell Horne #define	SBI_IMPL_ID_XVISOR		2
4925de8fb6SMitchell Horne #define	SBI_IMPL_ID_KVM			3
5025de8fb6SMitchell Horne #define	SBI_IMPL_ID_RUSTSBI		4
5125de8fb6SMitchell Horne #define	SBI_IMPL_ID_DIOSIX		5
520a0f40c7SMitchell Horne 
530a0f40c7SMitchell Horne /* SBI Error Codes */
540a0f40c7SMitchell Horne #define	SBI_SUCCESS			0
550a0f40c7SMitchell Horne #define	SBI_ERR_FAILURE			-1
560a0f40c7SMitchell Horne #define	SBI_ERR_NOT_SUPPORTED		-2
570a0f40c7SMitchell Horne #define	SBI_ERR_INVALID_PARAM		-3
580a0f40c7SMitchell Horne #define	SBI_ERR_DENIED			-4
590a0f40c7SMitchell Horne #define	SBI_ERR_INVALID_ADDRESS		-5
60bfe918faSMitchell Horne #define	SBI_ERR_ALREADY_AVAILABLE	-6
610a0f40c7SMitchell Horne 
620a0f40c7SMitchell Horne /* SBI Base Extension */
630a0f40c7SMitchell Horne #define	SBI_EXT_ID_BASE			0x10
640a0f40c7SMitchell Horne #define	SBI_BASE_GET_SPEC_VERSION	0
650a0f40c7SMitchell Horne #define	SBI_BASE_GET_IMPL_ID		1
660a0f40c7SMitchell Horne #define	SBI_BASE_GET_IMPL_VERSION	2
670a0f40c7SMitchell Horne #define	SBI_BASE_PROBE_EXTENSION	3
680a0f40c7SMitchell Horne #define	SBI_BASE_GET_MVENDORID		4
690a0f40c7SMitchell Horne #define	SBI_BASE_GET_MARCHID		5
700a0f40c7SMitchell Horne #define	SBI_BASE_GET_MIMPID		6
710a0f40c7SMitchell Horne 
7289f34929SMitchell Horne /* Timer (TIME) Extension */
7389f34929SMitchell Horne #define	SBI_EXT_ID_TIME			0x54494D45
7489f34929SMitchell Horne #define	SBI_TIME_SET_TIMER		0
7589f34929SMitchell Horne 
7689f34929SMitchell Horne /* IPI (IPI) Extension */
7789f34929SMitchell Horne #define	SBI_EXT_ID_IPI			0x735049
7889f34929SMitchell Horne #define	SBI_IPI_SEND_IPI		0
7989f34929SMitchell Horne 
8089f34929SMitchell Horne /* RFENCE (RFNC) Extension */
8189f34929SMitchell Horne #define	SBI_EXT_ID_RFNC				0x52464E43
8289f34929SMitchell Horne #define	SBI_RFNC_REMOTE_FENCE_I			0
8389f34929SMitchell Horne #define	SBI_RFNC_REMOTE_SFENCE_VMA		1
8489f34929SMitchell Horne #define	SBI_RFNC_REMOTE_SFENCE_VMA_ASID		2
8589f34929SMitchell Horne #define	SBI_RFNC_REMOTE_HFENCE_GVMA_VMID	3
8689f34929SMitchell Horne #define	SBI_RFNC_REMOTE_HFENCE_GVMA		4
8789f34929SMitchell Horne #define	SBI_RFNC_REMOTE_HFENCE_VVMA_ASID	5
8889f34929SMitchell Horne #define	SBI_RFNC_REMOTE_HFENCE_VVMA		6
8989f34929SMitchell Horne 
90bfe918faSMitchell Horne /* Hart State Management (HSM) Extension */
91bfe918faSMitchell Horne #define	SBI_EXT_ID_HSM			0x48534D
92bfe918faSMitchell Horne #define	SBI_HSM_HART_START		0
93bfe918faSMitchell Horne #define	SBI_HSM_HART_STOP		1
94bfe918faSMitchell Horne #define	SBI_HSM_HART_STATUS		2
95bfe918faSMitchell Horne #define	 SBI_HSM_STATUS_STARTED		0
96bfe918faSMitchell Horne #define	 SBI_HSM_STATUS_STOPPED		1
97bfe918faSMitchell Horne #define	 SBI_HSM_STATUS_START_PENDING	2
98bfe918faSMitchell Horne #define	 SBI_HSM_STATUS_STOP_PENDING	3
99bfe918faSMitchell Horne 
1009bae4ce6SDanjel Qyteza /* System Reset (SRST) Extension */
1019bae4ce6SDanjel Qyteza #define	SBI_EXT_ID_SRST			0x53525354
1029bae4ce6SDanjel Qyteza #define	SBI_SRST_SYSTEM_RESET		0
1039bae4ce6SDanjel Qyteza #define	 SBI_SRST_TYPE_SHUTDOWN		0
1049bae4ce6SDanjel Qyteza #define	 SBI_SRST_TYPE_COLD_REBOOT	1
1059bae4ce6SDanjel Qyteza #define	 SBI_SRST_TYPE_WARM_REBOOT	2
1069bae4ce6SDanjel Qyteza #define	 SBI_SRST_REASON_NONE		0
1079bae4ce6SDanjel Qyteza #define	 SBI_SRST_REASON_SYSTEM_FAILURE	1
1089bae4ce6SDanjel Qyteza 
1090a0f40c7SMitchell Horne /* Legacy Extensions */
110af19cc59SRuslan Bukin #define	SBI_SET_TIMER			0
111af19cc59SRuslan Bukin #define	SBI_CONSOLE_PUTCHAR		1
112af19cc59SRuslan Bukin #define	SBI_CONSOLE_GETCHAR		2
113af19cc59SRuslan Bukin #define	SBI_CLEAR_IPI			3
114af19cc59SRuslan Bukin #define	SBI_SEND_IPI			4
115af19cc59SRuslan Bukin #define	SBI_REMOTE_FENCE_I		5
116af19cc59SRuslan Bukin #define	SBI_REMOTE_SFENCE_VMA		6
117af19cc59SRuslan Bukin #define	SBI_REMOTE_SFENCE_VMA_ASID	7
118af19cc59SRuslan Bukin #define	SBI_SHUTDOWN			8
1195f8228b2SRuslan Bukin 
12089f34929SMitchell Horne #define	SBI_CALL0(e, f)				SBI_CALL5(e, f, 0, 0, 0, 0, 0)
12189f34929SMitchell Horne #define	SBI_CALL1(e, f, p1)			SBI_CALL5(e, f, p1, 0, 0, 0, 0)
12289f34929SMitchell Horne #define	SBI_CALL2(e, f, p1, p2)			SBI_CALL5(e, f, p1, p2, 0, 0, 0)
12389f34929SMitchell Horne #define	SBI_CALL3(e, f, p1, p2, p3)		SBI_CALL5(e, f, p1, p2, p3, 0, 0)
12489f34929SMitchell Horne #define	SBI_CALL4(e, f, p1, p2, p3, p4)		SBI_CALL5(e, f, p1, p2, p3, p4, 0)
12589f34929SMitchell Horne #define	SBI_CALL5(e, f, p1, p2, p3, p4, p5)	sbi_call(e, f, p1, p2, p3, p4, p5)
126331baa6fSMitchell Horne 
12773efa2fbSJohn Baldwin /*
12873efa2fbSJohn Baldwin  * Documentation available at
1290a0f40c7SMitchell Horne  * https://github.com/riscv/riscv-sbi-doc/blob/master/riscv-sbi.adoc
13073efa2fbSJohn Baldwin  */
1315f8228b2SRuslan Bukin 
1320a0f40c7SMitchell Horne struct sbi_ret {
1330a0f40c7SMitchell Horne 	long error;
1340a0f40c7SMitchell Horne 	long value;
1350a0f40c7SMitchell Horne };
1360a0f40c7SMitchell Horne 
1370a0f40c7SMitchell Horne static __inline struct sbi_ret
sbi_call(uint64_t arg7,uint64_t arg6,uint64_t arg0,uint64_t arg1,uint64_t arg2,uint64_t arg3,uint64_t arg4)138331baa6fSMitchell Horne sbi_call(uint64_t arg7, uint64_t arg6, uint64_t arg0, uint64_t arg1,
13989f34929SMitchell Horne     uint64_t arg2, uint64_t arg3, uint64_t arg4)
14073efa2fbSJohn Baldwin {
1410a0f40c7SMitchell Horne 	struct sbi_ret ret;
1420a0f40c7SMitchell Horne 
143af19cc59SRuslan Bukin 	register uintptr_t a0 __asm ("a0") = (uintptr_t)(arg0);
144af19cc59SRuslan Bukin 	register uintptr_t a1 __asm ("a1") = (uintptr_t)(arg1);
145af19cc59SRuslan Bukin 	register uintptr_t a2 __asm ("a2") = (uintptr_t)(arg2);
14673efa2fbSJohn Baldwin 	register uintptr_t a3 __asm ("a3") = (uintptr_t)(arg3);
14789f34929SMitchell Horne 	register uintptr_t a4 __asm ("a4") = (uintptr_t)(arg4);
148331baa6fSMitchell Horne 	register uintptr_t a6 __asm ("a6") = (uintptr_t)(arg6);
149af19cc59SRuslan Bukin 	register uintptr_t a7 __asm ("a7") = (uintptr_t)(arg7);
15073efa2fbSJohn Baldwin 
151af19cc59SRuslan Bukin 	__asm __volatile(			\
152af19cc59SRuslan Bukin 		"ecall"				\
1530a0f40c7SMitchell Horne 		:"+r"(a0), "+r"(a1)		\
15489f34929SMitchell Horne 		:"r"(a2), "r"(a3), "r"(a4), "r"(a6), "r"(a7)	\
155af19cc59SRuslan Bukin 		:"memory");
1565f8228b2SRuslan Bukin 
1570a0f40c7SMitchell Horne 	ret.error = a0;
1580a0f40c7SMitchell Horne 	ret.value = a1;
1590a0f40c7SMitchell Horne 	return (ret);
160af19cc59SRuslan Bukin }
1615f8228b2SRuslan Bukin 
162c55272fdSJessica Clarke /* Base extension functions. */
1630a0f40c7SMitchell Horne static __inline long
sbi_probe_extension(long id)1640a0f40c7SMitchell Horne sbi_probe_extension(long id)
1650a0f40c7SMitchell Horne {
1660a0f40c7SMitchell Horne 	return (SBI_CALL1(SBI_EXT_ID_BASE, SBI_BASE_PROBE_EXTENSION, id).value);
1670a0f40c7SMitchell Horne }
1680a0f40c7SMitchell Horne 
16989f34929SMitchell Horne /* TIME extension functions. */
17089f34929SMitchell Horne void sbi_set_timer(uint64_t val);
17189f34929SMitchell Horne 
17289f34929SMitchell Horne /* IPI extension functions. */
17389f34929SMitchell Horne void sbi_send_ipi(const u_long *hart_mask);
17489f34929SMitchell Horne 
17589f34929SMitchell Horne /* RFENCE extension functions. */
17689f34929SMitchell Horne void sbi_remote_fence_i(const u_long *hart_mask);
17789f34929SMitchell Horne void sbi_remote_sfence_vma(const u_long *hart_mask, u_long start, u_long size);
17889f34929SMitchell Horne void sbi_remote_sfence_vma_asid(const u_long *hart_mask, u_long start,
17989f34929SMitchell Horne     u_long size, u_long asid);
18089f34929SMitchell Horne 
181bfe918faSMitchell Horne /* Hart State Management extension functions. */
182bfe918faSMitchell Horne 
183bfe918faSMitchell Horne /*
184bfe918faSMitchell Horne  * Start execution on the specified hart at physical address start_addr. The
185bfe918faSMitchell Horne  * register a0 will contain the hart's ID, and a1 will contain the value of
186bfe918faSMitchell Horne  * priv.
187bfe918faSMitchell Horne  */
188bfe918faSMitchell Horne int sbi_hsm_hart_start(u_long hart, u_long start_addr, u_long priv);
189bfe918faSMitchell Horne 
190bfe918faSMitchell Horne /*
191bfe918faSMitchell Horne  * Stop execution on the current hart. Interrupts should be disabled, or this
192bfe918faSMitchell Horne  * function may return.
193bfe918faSMitchell Horne  */
194bfe918faSMitchell Horne void sbi_hsm_hart_stop(void);
195bfe918faSMitchell Horne 
196bfe918faSMitchell Horne /*
197bfe918faSMitchell Horne  * Get the execution status of the specified hart. The status will be one of:
198bfe918faSMitchell Horne  *  - SBI_HSM_STATUS_STARTED
199bfe918faSMitchell Horne  *  - SBI_HSM_STATUS_STOPPED
200bfe918faSMitchell Horne  *  - SBI_HSM_STATUS_START_PENDING
201bfe918faSMitchell Horne  *  - SBI_HSM_STATUS_STOP_PENDING
202bfe918faSMitchell Horne  */
203bfe918faSMitchell Horne int sbi_hsm_hart_status(u_long hart);
204bfe918faSMitchell Horne 
2059bae4ce6SDanjel Qyteza /* System Reset extension functions. */
2069bae4ce6SDanjel Qyteza 
2079bae4ce6SDanjel Qyteza /*
2089bae4ce6SDanjel Qyteza  * Reset the system based on the following 'type' and 'reason' chosen from:
2099bae4ce6SDanjel Qyteza  *  - SBI_SRST_TYPE_SHUTDOWN
2109bae4ce6SDanjel Qyteza  *  - SBI_SRST_TYPE_COLD_REBOOT
2119bae4ce6SDanjel Qyteza  *  - SBI_SRST_TYPE_WARM_REBOOT
2129bae4ce6SDanjel Qyteza  *  - SBI_SRST_REASON_NONE
2139bae4ce6SDanjel Qyteza  *  - SBI_SRST_REASON_SYSTEM_FAILURE
2149bae4ce6SDanjel Qyteza  */
2159bae4ce6SDanjel Qyteza void sbi_system_reset(u_long reset_type, u_long reset_reason);
2169bae4ce6SDanjel Qyteza 
2170a0f40c7SMitchell Horne /* Legacy extension functions. */
218af19cc59SRuslan Bukin static __inline void
sbi_console_putchar(int ch)219af19cc59SRuslan Bukin sbi_console_putchar(int ch)
220af19cc59SRuslan Bukin {
221af19cc59SRuslan Bukin 
222331baa6fSMitchell Horne 	(void)SBI_CALL1(SBI_CONSOLE_PUTCHAR, 0, ch);
223af19cc59SRuslan Bukin }
224af19cc59SRuslan Bukin 
225af19cc59SRuslan Bukin static __inline int
sbi_console_getchar(void)226af19cc59SRuslan Bukin sbi_console_getchar(void)
227af19cc59SRuslan Bukin {
228af19cc59SRuslan Bukin 
2290a0f40c7SMitchell Horne 	/*
2300a0f40c7SMitchell Horne 	 * XXX: The "error" is returned here because legacy SBI functions
2310a0f40c7SMitchell Horne 	 * continue to return their value in a0.
2320a0f40c7SMitchell Horne 	 */
2330a0f40c7SMitchell Horne 	return (SBI_CALL0(SBI_CONSOLE_GETCHAR, 0).error);
234af19cc59SRuslan Bukin }
235af19cc59SRuslan Bukin 
236a1092942SMitchell Horne void sbi_print_version(void);
2370a0f40c7SMitchell Horne void sbi_init(void);
2380a0f40c7SMitchell Horne 
2395f8228b2SRuslan Bukin #endif /* !_MACHINE_SBI_H_ */
240