18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Regents of the University of California
48c2ecf20Sopenharmony_ci * Copyright (c) 2020 Western Digital Corporation or its affiliates.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef _ASM_RISCV_SBI_H
88c2ecf20Sopenharmony_ci#define _ASM_RISCV_SBI_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/types.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#ifdef CONFIG_RISCV_SBI
138c2ecf20Sopenharmony_cienum sbi_ext_id {
148c2ecf20Sopenharmony_ci#ifdef CONFIG_RISCV_SBI_V01
158c2ecf20Sopenharmony_ci	SBI_EXT_0_1_SET_TIMER = 0x0,
168c2ecf20Sopenharmony_ci	SBI_EXT_0_1_CONSOLE_PUTCHAR = 0x1,
178c2ecf20Sopenharmony_ci	SBI_EXT_0_1_CONSOLE_GETCHAR = 0x2,
188c2ecf20Sopenharmony_ci	SBI_EXT_0_1_CLEAR_IPI = 0x3,
198c2ecf20Sopenharmony_ci	SBI_EXT_0_1_SEND_IPI = 0x4,
208c2ecf20Sopenharmony_ci	SBI_EXT_0_1_REMOTE_FENCE_I = 0x5,
218c2ecf20Sopenharmony_ci	SBI_EXT_0_1_REMOTE_SFENCE_VMA = 0x6,
228c2ecf20Sopenharmony_ci	SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID = 0x7,
238c2ecf20Sopenharmony_ci	SBI_EXT_0_1_SHUTDOWN = 0x8,
248c2ecf20Sopenharmony_ci#endif
258c2ecf20Sopenharmony_ci	SBI_EXT_BASE = 0x10,
268c2ecf20Sopenharmony_ci	SBI_EXT_TIME = 0x54494D45,
278c2ecf20Sopenharmony_ci	SBI_EXT_IPI = 0x735049,
288c2ecf20Sopenharmony_ci	SBI_EXT_RFENCE = 0x52464E43,
298c2ecf20Sopenharmony_ci	SBI_EXT_HSM = 0x48534D,
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cienum sbi_ext_base_fid {
338c2ecf20Sopenharmony_ci	SBI_EXT_BASE_GET_SPEC_VERSION = 0,
348c2ecf20Sopenharmony_ci	SBI_EXT_BASE_GET_IMP_ID,
358c2ecf20Sopenharmony_ci	SBI_EXT_BASE_GET_IMP_VERSION,
368c2ecf20Sopenharmony_ci	SBI_EXT_BASE_PROBE_EXT,
378c2ecf20Sopenharmony_ci	SBI_EXT_BASE_GET_MVENDORID,
388c2ecf20Sopenharmony_ci	SBI_EXT_BASE_GET_MARCHID,
398c2ecf20Sopenharmony_ci	SBI_EXT_BASE_GET_MIMPID,
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cienum sbi_ext_time_fid {
438c2ecf20Sopenharmony_ci	SBI_EXT_TIME_SET_TIMER = 0,
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cienum sbi_ext_ipi_fid {
478c2ecf20Sopenharmony_ci	SBI_EXT_IPI_SEND_IPI = 0,
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cienum sbi_ext_rfence_fid {
518c2ecf20Sopenharmony_ci	SBI_EXT_RFENCE_REMOTE_FENCE_I = 0,
528c2ecf20Sopenharmony_ci	SBI_EXT_RFENCE_REMOTE_SFENCE_VMA,
538c2ecf20Sopenharmony_ci	SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID,
548c2ecf20Sopenharmony_ci	SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID,
558c2ecf20Sopenharmony_ci	SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
568c2ecf20Sopenharmony_ci	SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID,
578c2ecf20Sopenharmony_ci	SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA,
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cienum sbi_ext_hsm_fid {
618c2ecf20Sopenharmony_ci	SBI_EXT_HSM_HART_START = 0,
628c2ecf20Sopenharmony_ci	SBI_EXT_HSM_HART_STOP,
638c2ecf20Sopenharmony_ci	SBI_EXT_HSM_HART_STATUS,
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cienum sbi_hsm_hart_status {
678c2ecf20Sopenharmony_ci	SBI_HSM_HART_STATUS_STARTED = 0,
688c2ecf20Sopenharmony_ci	SBI_HSM_HART_STATUS_STOPPED,
698c2ecf20Sopenharmony_ci	SBI_HSM_HART_STATUS_START_PENDING,
708c2ecf20Sopenharmony_ci	SBI_HSM_HART_STATUS_STOP_PENDING,
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define SBI_SPEC_VERSION_DEFAULT	0x1
748c2ecf20Sopenharmony_ci#define SBI_SPEC_VERSION_MAJOR_SHIFT	24
758c2ecf20Sopenharmony_ci#define SBI_SPEC_VERSION_MAJOR_MASK	0x7f
768c2ecf20Sopenharmony_ci#define SBI_SPEC_VERSION_MINOR_MASK	0xffffff
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/* SBI return error codes */
798c2ecf20Sopenharmony_ci#define SBI_SUCCESS		0
808c2ecf20Sopenharmony_ci#define SBI_ERR_FAILURE		-1
818c2ecf20Sopenharmony_ci#define SBI_ERR_NOT_SUPPORTED	-2
828c2ecf20Sopenharmony_ci#define SBI_ERR_INVALID_PARAM	-3
838c2ecf20Sopenharmony_ci#define SBI_ERR_DENIED		-4
848c2ecf20Sopenharmony_ci#define SBI_ERR_INVALID_ADDRESS	-5
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ciextern unsigned long sbi_spec_version;
878c2ecf20Sopenharmony_cistruct sbiret {
888c2ecf20Sopenharmony_ci	long error;
898c2ecf20Sopenharmony_ci	long value;
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ciint sbi_init(void);
938c2ecf20Sopenharmony_cistruct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
948c2ecf20Sopenharmony_ci			unsigned long arg1, unsigned long arg2,
958c2ecf20Sopenharmony_ci			unsigned long arg3, unsigned long arg4,
968c2ecf20Sopenharmony_ci			unsigned long arg5);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_civoid sbi_console_putchar(int ch);
998c2ecf20Sopenharmony_ciint sbi_console_getchar(void);
1008c2ecf20Sopenharmony_civoid sbi_set_timer(uint64_t stime_value);
1018c2ecf20Sopenharmony_civoid sbi_shutdown(void);
1028c2ecf20Sopenharmony_civoid sbi_clear_ipi(void);
1038c2ecf20Sopenharmony_civoid sbi_send_ipi(const unsigned long *hart_mask);
1048c2ecf20Sopenharmony_civoid sbi_remote_fence_i(const unsigned long *hart_mask);
1058c2ecf20Sopenharmony_civoid sbi_remote_sfence_vma(const unsigned long *hart_mask,
1068c2ecf20Sopenharmony_ci			   unsigned long start,
1078c2ecf20Sopenharmony_ci			   unsigned long size);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_civoid sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
1108c2ecf20Sopenharmony_ci				unsigned long start,
1118c2ecf20Sopenharmony_ci				unsigned long size,
1128c2ecf20Sopenharmony_ci				unsigned long asid);
1138c2ecf20Sopenharmony_ciint sbi_remote_hfence_gvma(const unsigned long *hart_mask,
1148c2ecf20Sopenharmony_ci			   unsigned long start,
1158c2ecf20Sopenharmony_ci			   unsigned long size);
1168c2ecf20Sopenharmony_ciint sbi_remote_hfence_gvma_vmid(const unsigned long *hart_mask,
1178c2ecf20Sopenharmony_ci				unsigned long start,
1188c2ecf20Sopenharmony_ci				unsigned long size,
1198c2ecf20Sopenharmony_ci				unsigned long vmid);
1208c2ecf20Sopenharmony_ciint sbi_remote_hfence_vvma(const unsigned long *hart_mask,
1218c2ecf20Sopenharmony_ci			   unsigned long start,
1228c2ecf20Sopenharmony_ci			   unsigned long size);
1238c2ecf20Sopenharmony_ciint sbi_remote_hfence_vvma_asid(const unsigned long *hart_mask,
1248c2ecf20Sopenharmony_ci				unsigned long start,
1258c2ecf20Sopenharmony_ci				unsigned long size,
1268c2ecf20Sopenharmony_ci				unsigned long asid);
1278c2ecf20Sopenharmony_ciint sbi_probe_extension(int ext);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/* Check if current SBI specification version is 0.1 or not */
1308c2ecf20Sopenharmony_cistatic inline int sbi_spec_is_0_1(void)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	return (sbi_spec_version == SBI_SPEC_VERSION_DEFAULT) ? 1 : 0;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/* Get the major version of SBI */
1368c2ecf20Sopenharmony_cistatic inline unsigned long sbi_major_version(void)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	return (sbi_spec_version >> SBI_SPEC_VERSION_MAJOR_SHIFT) &
1398c2ecf20Sopenharmony_ci		SBI_SPEC_VERSION_MAJOR_MASK;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/* Get the minor version of SBI */
1438c2ecf20Sopenharmony_cistatic inline unsigned long sbi_minor_version(void)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	return sbi_spec_version & SBI_SPEC_VERSION_MINOR_MASK;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ciint sbi_err_map_linux_errno(int err);
1498c2ecf20Sopenharmony_ci#else /* CONFIG_RISCV_SBI */
1508c2ecf20Sopenharmony_ci/* stubs for code that is only reachable under IS_ENABLED(CONFIG_RISCV_SBI): */
1518c2ecf20Sopenharmony_civoid sbi_set_timer(uint64_t stime_value);
1528c2ecf20Sopenharmony_civoid sbi_clear_ipi(void);
1538c2ecf20Sopenharmony_civoid sbi_send_ipi(const unsigned long *hart_mask);
1548c2ecf20Sopenharmony_civoid sbi_remote_fence_i(const unsigned long *hart_mask);
1558c2ecf20Sopenharmony_civoid sbi_init(void);
1568c2ecf20Sopenharmony_ci#endif /* CONFIG_RISCV_SBI */
1578c2ecf20Sopenharmony_ci#endif /* _ASM_RISCV_SBI_H */
158