blob: 631625d715bfaaf71578da69a0dd4f853d056b48 [file] [log] [blame]
Etienne Carriered4932bd2021-11-09 17:08:24 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
Etienne Carrierec624c972022-05-31 18:09:16 +02003 * Copyright (C) 2020-2022 Linaro Limited.
Etienne Carriered4932bd2021-11-09 17:08:24 +01004 */
5
6#define LOG_CATEGORY UCLASS_SCMI_AGENT
7
Etienne Carriered4932bd2021-11-09 17:08:24 +01008#include <dm.h>
9#include <errno.h>
10#include <scmi_agent.h>
11#include <scmi_agent-uclass.h>
12#include <string.h>
13#include <tee.h>
14#include <asm/types.h>
15#include <dm/device_compat.h>
16#include <dm/devres.h>
17#include <linux/arm-smccc.h>
18#include <linux/bug.h>
19#include <linux/compat.h>
20
21#include "smt.h"
22
23#define SCMI_SHM_SIZE 128
24
25/**
26 * struct scmi_optee_channel - Description of an SCMI OP-TEE transport
27 * @channel_id: Channel identifier
28 * @smt: Shared memory buffer with synchronisation protocol
29 * @dyn_shm: True if using dynamically allocated shared memory
30 */
31struct scmi_optee_channel {
32 unsigned int channel_id;
33 struct scmi_smt smt;
34 bool dyn_shm;
35};
36
37/**
Etienne Carriere6ae56442022-05-31 18:09:24 +020038 * struct scmi_channel - Channel instance referenced in SCMI drivers
39 * @ref: Reference to local channel instance
40 **/
41struct scmi_channel {
42 struct scmi_optee_channel ref;
43};
44
45/**
Etienne Carriered4932bd2021-11-09 17:08:24 +010046 * struct channel_session - Aggreates SCMI service session context references
47 * @tee: OP-TEE device to invoke
48 * @tee_session: OP-TEE session identifier
49 * @tee_shm: Dynamically allocated OP-TEE shared memory, or NULL
50 * @channel_hdl: Channel handle provided by OP-TEE SCMI service
51 */
52struct channel_session {
53 struct udevice *tee;
54 u32 tee_session;
55 struct tee_shm *tee_shm;
56 u32 channel_hdl;
57};
58
59#define TA_SCMI_UUID { 0xa8cfe406, 0xd4f5, 0x4a2e, \
60 { 0x9f, 0x8d, 0xa2, 0x5d, 0xc7, 0x54, 0xc0, 0x99 } }
61
62enum optee_smci_pta_cmd {
63 /*
64 * PTA_SCMI_CMD_CAPABILITIES - Get channel capabilities
65 *
66 * [out] value[0].a: Capability bit mask (enum pta_scmi_caps)
67 * [out] value[0].b: Extended capabilities or 0
68 */
69 PTA_SCMI_CMD_CAPABILITIES = 0,
70
71 /*
72 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL - Process SCMI message in SMT buffer
73 *
74 * [in] value[0].a: Channel handle
75 *
76 * Shared memory used for SCMI message/response exhange is expected
77 * already identified and bound to channel handle in both SCMI agent
78 * and SCMI server (OP-TEE) parts.
79 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
80 * protocol message ID).
81 */
82 PTA_SCMI_CMD_PROCESS_SMT_CHANNEL = 1,
83
84 /*
85 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE - Process SMT/SCMI message
86 *
87 * [in] value[0].a: Channel handle
88 * [in/out] memref[1]: Message/response buffer (SMT and SCMI payload)
89 *
90 * Shared memory used for SCMI message/response is a SMT buffer
91 * referenced by param[1]. It shall be 128 bytes large to fit response
92 * payload whatever message playload size.
93 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
94 * protocol message ID).
95 */
96 PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE = 2,
97
98 /*
99 * PTA_SCMI_CMD_GET_CHANNEL - Get channel handle
100 *
Etienne Carriered4932bd2021-11-09 17:08:24 +0100101 * [in] value[0].a: Channel identifier
102 * [out] value[0].a: Returned channel handle
103 * [in] value[0].b: Requested capabilities mask (enum pta_scmi_caps)
104 */
105 PTA_SCMI_CMD_GET_CHANNEL = 3,
Etienne Carrierec624c972022-05-31 18:09:16 +0200106
107 /*
108 * PTA_SCMI_CMD_PROCESS_MSG_CHANNEL - Process SCMI message in MSG
109 * buffers pointed by memref parameters
110 *
111 * [in] value[0].a: Channel handle
112 * [in] memref[1]: Message buffer (MSG header and SCMI payload)
113 * [out] memref[2]: Response buffer (MSG header and SCMI payload)
114 *
115 * Shared memories used for SCMI message/response are MSG buffers
116 * referenced by param[1] and param[2]. MSG transport protocol
117 * uses a 32bit header to carry SCMI meta-data (protocol ID and
118 * protocol message ID) followed by the effective SCMI message
119 * payload.
120 */
121 PTA_SCMI_CMD_PROCESS_MSG_CHANNEL = 4,
Etienne Carriered4932bd2021-11-09 17:08:24 +0100122};
123
124/*
125 * OP-TEE SCMI service capabilities bit flags (32bit)
126 *
127 * PTA_SCMI_CAPS_SMT_HEADER
128 * When set, OP-TEE supports command using SMT header protocol (SCMI shmem) in
129 * shared memory buffers to carry SCMI protocol synchronisation information.
Etienne Carrierec624c972022-05-31 18:09:16 +0200130 *
131 * PTA_SCMI_CAPS_MSG_HEADER
132 * When set, OP-TEE supports command using MSG header protocol in an OP-TEE
133 * shared memory to carry SCMI protocol synchronisation information and SCMI
134 * message payload.
Etienne Carriered4932bd2021-11-09 17:08:24 +0100135 */
136#define PTA_SCMI_CAPS_NONE 0
137#define PTA_SCMI_CAPS_SMT_HEADER BIT(0)
Etienne Carrierec624c972022-05-31 18:09:16 +0200138#define PTA_SCMI_CAPS_MSG_HEADER BIT(1)
139#define PTA_SCMI_CAPS_MASK (PTA_SCMI_CAPS_SMT_HEADER | \
140 PTA_SCMI_CAPS_MSG_HEADER)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100141
Etienne Carriere6ae56442022-05-31 18:09:24 +0200142static int open_channel(struct udevice *dev, struct scmi_optee_channel *chan,
143 struct channel_session *sess)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100144{
145 const struct tee_optee_ta_uuid uuid = TA_SCMI_UUID;
Etienne Carriered4932bd2021-11-09 17:08:24 +0100146 struct tee_open_session_arg sess_arg = { };
147 struct tee_invoke_arg cmd_arg = { };
148 struct tee_param param[1] = { };
149 int ret;
150
Francois Berderd54ca4a2023-10-10 19:44:32 +0200151 memset(sess, 0, sizeof(*sess));
Etienne Carriered4932bd2021-11-09 17:08:24 +0100152
153 sess->tee = tee_find_device(NULL, NULL, NULL, NULL);
154 if (!sess->tee)
155 return -ENODEV;
156
157 sess_arg.clnt_login = TEE_LOGIN_REE_KERNEL;
158 tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid);
159
160 ret = tee_open_session(sess->tee, &sess_arg, 0, NULL);
161 if (ret) {
162 dev_err(dev, "can't open session: %d\n", ret);
163 return ret;
164 }
165
166 cmd_arg.func = PTA_SCMI_CMD_GET_CHANNEL;
167 cmd_arg.session = sess_arg.session;
168
169 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INOUT;
170 param[0].u.value.a = chan->channel_id;
Etienne Carrierec624c972022-05-31 18:09:16 +0200171 if (chan->dyn_shm)
172 param[0].u.value.b = PTA_SCMI_CAPS_MSG_HEADER;
173 else
174 param[0].u.value.b = PTA_SCMI_CAPS_SMT_HEADER;
Etienne Carriered4932bd2021-11-09 17:08:24 +0100175
176 ret = tee_invoke_func(sess->tee, &cmd_arg, ARRAY_SIZE(param), param);
177 if (ret || cmd_arg.ret) {
178 dev_err(dev, "Invoke failed: %d, 0x%x\n", ret, cmd_arg.ret);
179 if (!ret)
180 ret = -EPROTO;
181
182 tee_close_session(sess->tee, sess_arg.session);
183 return ret;
184 }
185
186 sess->tee_session = sess_arg.session;
187 sess->channel_hdl = param[0].u.value.a;
188
189 return 0;
190}
191
192static void close_channel(struct channel_session *sess)
193{
194 tee_close_session(sess->tee, sess->tee_session);
195}
196
Etienne Carriere6ae56442022-05-31 18:09:24 +0200197static int invoke_cmd(struct udevice *dev, struct scmi_optee_channel *chan,
198 struct channel_session *sess, struct scmi_msg *msg)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100199{
Etienne Carriered4932bd2021-11-09 17:08:24 +0100200 struct tee_invoke_arg arg = { };
Etienne Carrierec624c972022-05-31 18:09:16 +0200201 struct tee_param param[3] = { };
Etienne Carriered4932bd2021-11-09 17:08:24 +0100202 int ret;
203
Etienne Carriered4932bd2021-11-09 17:08:24 +0100204 arg.session = sess->tee_session;
205 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
206 param[0].u.value.a = sess->channel_hdl;
207
Etienne Carrierec624c972022-05-31 18:09:16 +0200208 if (sess->tee_shm) {
209 size_t in_size;
210
211 ret = scmi_msg_to_smt_msg(dev, &chan->smt, msg, &in_size);
212 if (ret < 0)
213 return ret;
214
215 arg.func = PTA_SCMI_CMD_PROCESS_MSG_CHANNEL;
216 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
Etienne Carriered4932bd2021-11-09 17:08:24 +0100217 param[1].u.memref.shm = sess->tee_shm;
Etienne Carrierec624c972022-05-31 18:09:16 +0200218 param[1].u.memref.size = in_size;
219 param[2].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
220 param[2].u.memref.shm = sess->tee_shm;
221 param[2].u.memref.size = sess->tee_shm->size;
Etienne Carriered4932bd2021-11-09 17:08:24 +0100222 } else {
223 arg.func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL;
Etienne Carrierec624c972022-05-31 18:09:16 +0200224 scmi_write_msg_to_smt(dev, &chan->smt, msg);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100225 }
226
227 ret = tee_invoke_func(sess->tee, &arg, ARRAY_SIZE(param), param);
228 if (ret || arg.ret) {
229 if (!ret)
230 ret = -EPROTO;
Etienne Carrierec624c972022-05-31 18:09:16 +0200231
232 return ret;
233 }
234
235 if (sess->tee_shm) {
236 ret = scmi_msg_from_smt_msg(dev, &chan->smt, msg,
237 param[2].u.memref.size);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100238 } else {
239 ret = scmi_read_resp_from_smt(dev, &chan->smt, msg);
Etienne Carrierec624c972022-05-31 18:09:16 +0200240 scmi_clear_smt_channel(&chan->smt);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100241 }
242
Etienne Carriered4932bd2021-11-09 17:08:24 +0100243 return ret;
244}
245
Etienne Carriere6ae56442022-05-31 18:09:24 +0200246static int prepare_shm(struct udevice *dev, struct scmi_optee_channel *chan,
247 struct channel_session *sess)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100248{
Etienne Carriered4932bd2021-11-09 17:08:24 +0100249 int ret;
250
251 /* Static shm is already prepared by the firmware: nothing to do */
252 if (!chan->dyn_shm)
253 return 0;
254
255 chan->smt.size = SCMI_SHM_SIZE;
256
257 ret = tee_shm_alloc(sess->tee, chan->smt.size, 0, &sess->tee_shm);
258 if (ret) {
259 dev_err(dev, "Failed to allocated shmem: %d\n", ret);
260 return ret;
261 }
262
263 chan->smt.buf = sess->tee_shm->addr;
264
Etienne Carriered4932bd2021-11-09 17:08:24 +0100265 return 0;
266}
267
268static void release_shm(struct udevice *dev, struct channel_session *sess)
269{
270 struct scmi_optee_channel *chan = dev_get_plat(dev);
271
272 if (chan->dyn_shm)
273 tee_shm_free(sess->tee_shm);
274}
275
Etienne Carriere05440292022-05-31 18:09:19 +0200276static int scmi_optee_process_msg(struct udevice *dev,
277 struct scmi_channel *channel,
278 struct scmi_msg *msg)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100279{
Etienne Carriere76b690a2022-05-31 18:09:29 +0200280 struct scmi_optee_channel *chan = &channel->ref;
Etienne Carrierec624c972022-05-31 18:09:16 +0200281 struct channel_session sess = { };
Etienne Carriered4932bd2021-11-09 17:08:24 +0100282 int ret;
283
Etienne Carriere6ae56442022-05-31 18:09:24 +0200284 ret = open_channel(dev, chan, &sess);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100285 if (ret)
286 return ret;
287
Etienne Carriere6ae56442022-05-31 18:09:24 +0200288 ret = prepare_shm(dev, chan, &sess);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100289 if (ret)
290 goto out;
291
Etienne Carriere6ae56442022-05-31 18:09:24 +0200292 ret = invoke_cmd(dev, chan, &sess, msg);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100293
294 release_shm(dev, &sess);
295
296out:
297 close_channel(&sess);
298
299 return ret;
300}
301
Etienne Carriere6ae56442022-05-31 18:09:24 +0200302static int setup_channel(struct udevice *dev, struct scmi_optee_channel *chan)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100303{
Etienne Carriered4932bd2021-11-09 17:08:24 +0100304 int ret;
305
306 if (dev_read_u32(dev, "linaro,optee-channel-id", &chan->channel_id)) {
307 dev_err(dev, "Missing property linaro,optee-channel-id\n");
308 return -EINVAL;
309 }
310
311 if (dev_read_prop(dev, "shmem", NULL)) {
312 ret = scmi_dt_get_smt_buffer(dev, &chan->smt);
313 if (ret) {
314 dev_err(dev, "Failed to get smt resources: %d\n", ret);
315 return ret;
316 }
317 chan->dyn_shm = false;
318 } else {
319 chan->dyn_shm = true;
320 }
321
322 return 0;
323}
324
Etienne Carriere6ae56442022-05-31 18:09:24 +0200325static int scmi_optee_get_channel(struct udevice *dev,
AKASHI Takahiro589ec9a2023-10-11 19:06:55 +0900326 struct udevice *protocol,
Etienne Carriere6ae56442022-05-31 18:09:24 +0200327 struct scmi_channel **channel)
328{
Patrick Delaunay3d8b9502022-09-30 09:36:38 +0200329 struct scmi_optee_channel *base_chan = dev_get_plat(dev);
Etienne Carriere6ae56442022-05-31 18:09:24 +0200330 struct scmi_optee_channel *chan;
331 u32 channel_id;
332 int ret;
333
AKASHI Takahiro589ec9a2023-10-11 19:06:55 +0900334 if (dev_read_u32(protocol, "linaro,optee-channel-id", &channel_id)) {
Etienne Carriere6ae56442022-05-31 18:09:24 +0200335 /* Uses agent base channel */
336 *channel = container_of(base_chan, struct scmi_channel, ref);
337
338 return 0;
339 }
340
341 /* Setup a dedicated channel */
342 chan = calloc(1, sizeof(*chan));
343 if (!chan)
344 return -ENOMEM;
345
AKASHI Takahiro589ec9a2023-10-11 19:06:55 +0900346 ret = setup_channel(protocol, chan);
Etienne Carriere6ae56442022-05-31 18:09:24 +0200347 if (ret) {
348 free(chan);
349 return ret;
350 }
351
352 *channel = container_of(chan, struct scmi_channel, ref);
353
354 return 0;
355}
356
357static int scmi_optee_of_to_plat(struct udevice *dev)
358{
359 struct scmi_optee_channel *chan = dev_get_plat(dev);
360
361 return setup_channel(dev, chan);
362}
363
Etienne Carriered4932bd2021-11-09 17:08:24 +0100364static int scmi_optee_probe(struct udevice *dev)
365{
Etienne Carriere6ae56442022-05-31 18:09:24 +0200366 struct scmi_optee_channel *chan = dev_get_plat(dev);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100367 struct channel_session sess;
368 int ret;
369
370 /* Check OP-TEE service acknowledges the SCMI channel */
Etienne Carriere6ae56442022-05-31 18:09:24 +0200371 ret = open_channel(dev, chan, &sess);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100372 if (!ret)
373 close_channel(&sess);
374
375 return ret;
376}
377
378static const struct udevice_id scmi_optee_ids[] = {
379 { .compatible = "linaro,scmi-optee" },
380 { }
381};
382
383static const struct scmi_agent_ops scmi_optee_ops = {
Etienne Carriere6ae56442022-05-31 18:09:24 +0200384 .of_get_channel = scmi_optee_get_channel,
Etienne Carriered4932bd2021-11-09 17:08:24 +0100385 .process_msg = scmi_optee_process_msg,
386};
387
388U_BOOT_DRIVER(scmi_optee) = {
389 .name = "scmi-over-optee",
390 .id = UCLASS_SCMI_AGENT,
391 .of_match = scmi_optee_ids,
392 .plat_auto = sizeof(struct scmi_optee_channel),
393 .of_to_plat = scmi_optee_of_to_plat,
394 .probe = scmi_optee_probe,
395 .flags = DM_FLAG_OS_PREPARE,
396 .ops = &scmi_optee_ops,
397};