1/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
4 *
5 * Authors:
6 *   Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
7 */
8
9#ifndef __ARM_FFA_PRV_H
10#define __ARM_FFA_PRV_H
11
12#include <mapmem.h>
13#include <linux/bitfield.h>
14#include <linux/bitops.h>
15
16/* This header is exclusively used by the FF-A Uclass and FF-A driver(s) */
17
18/* Arm FF-A driver name */
19#define FFA_DRV_NAME "arm_ffa"
20
21/* The FF-A SMC function definitions */
22
23#if CONFIG_IS_ENABLED(SANDBOX)
24
25/* Providing Arm SMCCC declarations to sandbox */
26
27/**
28 * struct sandbox_smccc_1_2_regs - emulated SMC call arguments or results
29 * @a0-a17 argument values from registers 0 to 17
30 */
31struct sandbox_smccc_1_2_regs {
32	ulong a0;
33	ulong a1;
34	ulong a2;
35	ulong a3;
36	ulong a4;
37	ulong a5;
38	ulong a6;
39	ulong a7;
40	ulong a8;
41	ulong a9;
42	ulong a10;
43	ulong a11;
44	ulong a12;
45	ulong a13;
46	ulong a14;
47	ulong a15;
48	ulong a16;
49	ulong a17;
50};
51
52typedef struct sandbox_smccc_1_2_regs ffa_value_t;
53
54#define ARM_SMCCC_FAST_CALL		1UL
55#define ARM_SMCCC_OWNER_STANDARD	4
56#define ARM_SMCCC_SMC_32		0
57#define ARM_SMCCC_SMC_64		1
58#define ARM_SMCCC_TYPE_SHIFT		31
59#define ARM_SMCCC_CALL_CONV_SHIFT	30
60#define ARM_SMCCC_OWNER_MASK		0x3f
61#define ARM_SMCCC_OWNER_SHIFT		24
62#define ARM_SMCCC_FUNC_MASK		0xffff
63
64#define ARM_SMCCC_CALL_VAL(type, calling_convention, owner, func_num) \
65	(((type) << ARM_SMCCC_TYPE_SHIFT) | \
66	((calling_convention) << ARM_SMCCC_CALL_CONV_SHIFT) | \
67	(((owner) & ARM_SMCCC_OWNER_MASK) << ARM_SMCCC_OWNER_SHIFT) | \
68	((func_num) & ARM_SMCCC_FUNC_MASK))
69
70#else
71/* CONFIG_ARM64 */
72#include <linux/arm-smccc.h>
73typedef struct arm_smccc_1_2_regs ffa_value_t;
74#endif
75
76/* Defining the function pointer type for the function executing the FF-A ABIs */
77typedef void (*invoke_ffa_fn_t)(ffa_value_t args, ffa_value_t *res);
78
79/* FF-A driver version definitions */
80
81#define MAJOR_VERSION_MASK		GENMASK(30, 16)
82#define MINOR_VERSION_MASK		GENMASK(15, 0)
83#define GET_FFA_MAJOR_VERSION(x)		\
84				((u16)(FIELD_GET(MAJOR_VERSION_MASK, (x))))
85#define GET_FFA_MINOR_VERSION(x)		\
86				((u16)(FIELD_GET(MINOR_VERSION_MASK, (x))))
87#define PACK_VERSION_INFO(major, minor)			\
88	(FIELD_PREP(MAJOR_VERSION_MASK, (major)) |	\
89	 FIELD_PREP(MINOR_VERSION_MASK, (minor)))
90
91#define FFA_MAJOR_VERSION		(1)
92#define FFA_MINOR_VERSION		(0)
93#define FFA_VERSION_1_0		\
94			PACK_VERSION_INFO(FFA_MAJOR_VERSION, FFA_MINOR_VERSION)
95
96/* Endpoint ID mask (u-boot endpoint ID) */
97
98#define GET_SELF_ENDPOINT_ID_MASK		GENMASK(15, 0)
99#define GET_SELF_ENDPOINT_ID(x)		\
100			((u16)(FIELD_GET(GET_SELF_ENDPOINT_ID_MASK, (x))))
101
102#define PREP_SELF_ENDPOINT_ID_MASK		GENMASK(31, 16)
103#define PREP_SELF_ENDPOINT_ID(x)		\
104			(FIELD_PREP(PREP_SELF_ENDPOINT_ID_MASK, (x)))
105
106/* Partition endpoint ID mask  (partition with which u-boot communicates with) */
107
108#define PREP_PART_ENDPOINT_ID_MASK		GENMASK(15, 0)
109#define PREP_PART_ENDPOINT_ID(x)		\
110			(FIELD_PREP(PREP_PART_ENDPOINT_ID_MASK, (x)))
111
112/* Definitions of the Arm FF-A interfaces supported by the Arm FF-A driver */
113
114#define FFA_SMC(calling_convention, func_num)				\
115	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, (calling_convention),	\
116			   ARM_SMCCC_OWNER_STANDARD, (func_num))
117
118#define FFA_SMC_32(func_num)				FFA_SMC(ARM_SMCCC_SMC_32, (func_num))
119#define FFA_SMC_64(func_num)				FFA_SMC(ARM_SMCCC_SMC_64, (func_num))
120
121enum ffa_abis {
122	FFA_ERROR                 = 0x60,
123	FFA_SUCCESS               = 0x61,
124	FFA_INTERRUPT             = 0x62,
125	FFA_VERSION               = 0x63,
126	FFA_FEATURES              = 0x64,
127	FFA_RX_RELEASE            = 0x65,
128	FFA_RXTX_MAP              = 0x66,
129	FFA_RXTX_UNMAP            = 0x67,
130	FFA_PARTITION_INFO_GET    = 0x68,
131	FFA_ID_GET                = 0x69,
132	FFA_RUN                   = 0x6d,
133	FFA_MSG_SEND_DIRECT_REQ   = 0x6f,
134	FFA_MSG_SEND_DIRECT_RESP  = 0x70,
135
136	/* To be updated when adding new FFA IDs */
137	FFA_FIRST_ID              = FFA_ERROR, /* Lowest number ID */
138	FFA_LAST_ID               = FFA_MSG_SEND_DIRECT_RESP, /* Highest number ID */
139};
140
141enum ffa_abi_errcode {
142	NOT_SUPPORTED = 1,
143	INVALID_PARAMETERS,
144	NO_MEMORY,
145	BUSY,
146	INTERRUPTED,
147	DENIED,
148	RETRY,
149	ABORTED,
150	MAX_NUMBER_FFA_ERR
151};
152
153extern int ffa_to_std_errmap[MAX_NUMBER_FFA_ERR];
154
155/* Container structure and helper macros to map between an FF-A error and relevant error log */
156struct ffa_abi_errmap {
157	char *err_str[MAX_NUMBER_FFA_ERR];
158};
159
160#define FFA_ERRMAP_COUNT (FFA_LAST_ID - FFA_FIRST_ID + 1)
161#define FFA_ID_TO_ERRMAP_ID(ffa_id) ((ffa_id) - FFA_FIRST_ID)
162
163/**
164 * enum ffa_rxtx_buf_sizes - minimum sizes supported
165 * for the RX/TX buffers
166 */
167enum ffa_rxtx_buf_sizes {
168	RXTX_4K,
169	RXTX_64K,
170	RXTX_16K
171};
172
173/**
174 * struct ffa_rxtxpair - Hosts the RX/TX buffers virtual addresses
175 * @rxbuf:	virtual address of the RX buffer
176 * @txbuf:	virtual address of the TX buffer
177 * @rxtx_min_pages:	RX/TX buffers minimum size in pages
178 *
179 * Hosts the virtual addresses of the mapped RX/TX buffers
180 * These addresses are used by the FF-A functions that use the RX/TX buffers
181 */
182struct ffa_rxtxpair {
183	void *rxbuf; /* Virtual address returned by memalign */
184	void *txbuf; /* Virtual address returned by memalign */
185	size_t rxtx_min_pages; /* Minimum number of pages in each of the RX/TX buffers */
186};
187
188struct ffa_partition_desc;
189
190/**
191 * struct ffa_partitions - descriptors for all secure partitions
192 * @count:	The number of partitions descriptors
193 * @descs	The partitions descriptors table
194 *
195 * Contains the partitions descriptors table
196 */
197struct ffa_partitions {
198	u32 count;
199	struct ffa_partition_desc *descs; /* Virtual address */
200};
201
202/**
203 * struct ffa_priv - the driver private data structure
204 *
205 * @fwk_version:	FF-A framework version
206 * @emul:	FF-A sandbox emulator
207 * @id:	u-boot endpoint ID
208 * @partitions:	The partitions descriptors structure
209 * @pair:	The RX/TX buffers pair
210 *
211 * The device private data structure containing all the
212 * data read from secure world.
213 */
214struct ffa_priv {
215	u32 fwk_version;
216	struct udevice *emul;
217	u16 id;
218	struct ffa_partitions partitions;
219	struct ffa_rxtxpair pair;
220};
221
222/**
223 * ffa_get_version_hdlr() - FFA_VERSION handler function
224 * @dev: The FF-A bus device
225 *
226 * Implement FFA_VERSION FF-A function
227 * to get from the secure world the FF-A framework version
228 * FFA_VERSION is used to discover the FF-A framework.
229 *
230 * Return:
231 *
232 * 0 on success. Otherwise, failure
233 */
234int ffa_get_version_hdlr(struct udevice *dev);
235
236/**
237 * invoke_ffa_fn() - SMC wrapper
238 * @args: FF-A ABI arguments to be copied to Xn registers
239 * @res: FF-A ABI return data to be copied from Xn registers
240 *
241 * Calls low level SMC implementation.
242 * This function should be implemented by the user driver.
243 */
244void invoke_ffa_fn(ffa_value_t args, ffa_value_t *res);
245
246#endif
247