1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020-2021 Linaro Limited.
6 #define LOG_CATEGORY UCLASS_SCMI_AGENT
11 #include <scmi_agent.h>
12 #include <scmi_agent-uclass.h>
15 #include <asm/types.h>
16 #include <dm/device_compat.h>
17 #include <dm/devres.h>
18 #include <linux/arm-smccc.h>
19 #include <linux/bug.h>
20 #include <linux/compat.h>
24 #define SCMI_SHM_SIZE 128
27 * struct scmi_optee_channel - Description of an SCMI OP-TEE transport
28 * @channel_id: Channel identifier
29 * @smt: Shared memory buffer with synchronisation protocol
30 * @dyn_shm: True if using dynamically allocated shared memory
32 struct scmi_optee_channel {
33 unsigned int channel_id;
39 * struct channel_session - Aggreates SCMI service session context references
40 * @tee: OP-TEE device to invoke
41 * @tee_session: OP-TEE session identifier
42 * @tee_shm: Dynamically allocated OP-TEE shared memory, or NULL
43 * @channel_hdl: Channel handle provided by OP-TEE SCMI service
45 struct channel_session {
48 struct tee_shm *tee_shm;
52 #define TA_SCMI_UUID { 0xa8cfe406, 0xd4f5, 0x4a2e, \
53 { 0x9f, 0x8d, 0xa2, 0x5d, 0xc7, 0x54, 0xc0, 0x99 } }
55 enum optee_smci_pta_cmd {
57 * PTA_SCMI_CMD_CAPABILITIES - Get channel capabilities
59 * [out] value[0].a: Capability bit mask (enum pta_scmi_caps)
60 * [out] value[0].b: Extended capabilities or 0
62 PTA_SCMI_CMD_CAPABILITIES = 0,
65 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL - Process SCMI message in SMT buffer
67 * [in] value[0].a: Channel handle
69 * Shared memory used for SCMI message/response exhange is expected
70 * already identified and bound to channel handle in both SCMI agent
71 * and SCMI server (OP-TEE) parts.
72 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
73 * protocol message ID).
75 PTA_SCMI_CMD_PROCESS_SMT_CHANNEL = 1,
78 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE - Process SMT/SCMI message
80 * [in] value[0].a: Channel handle
81 * [in/out] memref[1]: Message/response buffer (SMT and SCMI payload)
83 * Shared memory used for SCMI message/response is a SMT buffer
84 * referenced by param[1]. It shall be 128 bytes large to fit response
85 * payload whatever message playload size.
86 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
87 * protocol message ID).
89 PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE = 2,
92 * PTA_SCMI_CMD_GET_CHANNEL - Get channel handle
94 * SCMI shm information are 0 if agent expects to use OP-TEE regular SHM
96 * [in] value[0].a: Channel identifier
97 * [out] value[0].a: Returned channel handle
98 * [in] value[0].b: Requested capabilities mask (enum pta_scmi_caps)
100 PTA_SCMI_CMD_GET_CHANNEL = 3,
104 * OP-TEE SCMI service capabilities bit flags (32bit)
106 * PTA_SCMI_CAPS_SMT_HEADER
107 * When set, OP-TEE supports command using SMT header protocol (SCMI shmem) in
108 * shared memory buffers to carry SCMI protocol synchronisation information.
110 #define PTA_SCMI_CAPS_NONE 0
111 #define PTA_SCMI_CAPS_SMT_HEADER BIT(0)
113 static int open_channel(struct udevice *dev, struct channel_session *sess)
115 const struct tee_optee_ta_uuid uuid = TA_SCMI_UUID;
116 struct scmi_optee_channel *chan = dev_get_plat(dev);
117 struct tee_open_session_arg sess_arg = { };
118 struct tee_invoke_arg cmd_arg = { };
119 struct tee_param param[1] = { };
122 memset(sess, 0, sizeof(sess));
124 sess->tee = tee_find_device(NULL, NULL, NULL, NULL);
128 sess_arg.clnt_login = TEE_LOGIN_REE_KERNEL;
129 tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid);
131 ret = tee_open_session(sess->tee, &sess_arg, 0, NULL);
133 dev_err(dev, "can't open session: %d\n", ret);
137 cmd_arg.func = PTA_SCMI_CMD_GET_CHANNEL;
138 cmd_arg.session = sess_arg.session;
140 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INOUT;
141 param[0].u.value.a = chan->channel_id;
142 param[0].u.value.b = PTA_SCMI_CAPS_SMT_HEADER;
144 ret = tee_invoke_func(sess->tee, &cmd_arg, ARRAY_SIZE(param), param);
145 if (ret || cmd_arg.ret) {
146 dev_err(dev, "Invoke failed: %d, 0x%x\n", ret, cmd_arg.ret);
150 tee_close_session(sess->tee, sess_arg.session);
154 sess->tee_session = sess_arg.session;
155 sess->channel_hdl = param[0].u.value.a;
160 static void close_channel(struct channel_session *sess)
162 tee_close_session(sess->tee, sess->tee_session);
165 static int invoke_cmd(struct udevice *dev, struct channel_session *sess,
166 struct scmi_msg *msg)
168 struct scmi_optee_channel *chan = dev_get_plat(dev);
169 struct tee_invoke_arg arg = { };
170 struct tee_param param[2] = { };
173 scmi_write_msg_to_smt(dev, &chan->smt, msg);
175 arg.session = sess->tee_session;
176 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
177 param[0].u.value.a = sess->channel_hdl;
180 arg.func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE;
181 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
182 param[1].u.memref.shm = sess->tee_shm;
183 param[1].u.memref.size = SCMI_SHM_SIZE;
185 arg.func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL;
188 ret = tee_invoke_func(sess->tee, &arg, ARRAY_SIZE(param), param);
189 if (ret || arg.ret) {
193 ret = scmi_read_resp_from_smt(dev, &chan->smt, msg);
196 scmi_clear_smt_channel(&chan->smt);
201 static int prepare_shm(struct udevice *dev, struct channel_session *sess)
203 struct scmi_optee_channel *chan = dev_get_plat(dev);
206 /* Static shm is already prepared by the firmware: nothing to do */
210 chan->smt.size = SCMI_SHM_SIZE;
212 ret = tee_shm_alloc(sess->tee, chan->smt.size, 0, &sess->tee_shm);
214 dev_err(dev, "Failed to allocated shmem: %d\n", ret);
218 chan->smt.buf = sess->tee_shm->addr;
220 /* Initialize shm buffer for message exchanges */
221 scmi_clear_smt_channel(&chan->smt);
226 static void release_shm(struct udevice *dev, struct channel_session *sess)
228 struct scmi_optee_channel *chan = dev_get_plat(dev);
231 tee_shm_free(sess->tee_shm);
234 static int scmi_optee_process_msg(struct udevice *dev, struct scmi_msg *msg)
236 struct channel_session sess;
239 ret = open_channel(dev, &sess);
243 ret = prepare_shm(dev, &sess);
247 ret = invoke_cmd(dev, &sess, msg);
249 release_shm(dev, &sess);
252 close_channel(&sess);
257 static int scmi_optee_of_to_plat(struct udevice *dev)
259 struct scmi_optee_channel *chan = dev_get_plat(dev);
262 if (dev_read_u32(dev, "linaro,optee-channel-id", &chan->channel_id)) {
263 dev_err(dev, "Missing property linaro,optee-channel-id\n");
267 if (dev_read_prop(dev, "shmem", NULL)) {
268 ret = scmi_dt_get_smt_buffer(dev, &chan->smt);
270 dev_err(dev, "Failed to get smt resources: %d\n", ret);
273 chan->dyn_shm = false;
275 chan->dyn_shm = true;
281 static int scmi_optee_probe(struct udevice *dev)
283 struct channel_session sess;
286 /* Check OP-TEE service acknowledges the SCMI channel */
287 ret = open_channel(dev, &sess);
289 close_channel(&sess);
294 static const struct udevice_id scmi_optee_ids[] = {
295 { .compatible = "linaro,scmi-optee" },
299 static const struct scmi_agent_ops scmi_optee_ops = {
300 .process_msg = scmi_optee_process_msg,
303 U_BOOT_DRIVER(scmi_optee) = {
304 .name = "scmi-over-optee",
305 .id = UCLASS_SCMI_AGENT,
306 .of_match = scmi_optee_ids,
307 .plat_auto = sizeof(struct scmi_optee_channel),
308 .of_to_plat = scmi_optee_of_to_plat,
309 .probe = scmi_optee_probe,
310 .flags = DM_FLAG_OS_PREPARE,
311 .ops = &scmi_optee_ops,