blob: 2b2b8c1670a355b737dd76fe2c145d11e72f89fa [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
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <scmi_agent.h>
12#include <scmi_agent-uclass.h>
13#include <string.h>
14#include <tee.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>
21
22#include "smt.h"
23
24#define SCMI_SHM_SIZE 128
25
26/**
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
31 */
32struct scmi_optee_channel {
33 unsigned int channel_id;
34 struct scmi_smt smt;
35 bool dyn_shm;
36};
37
38/**
Etienne Carriere6ae56442022-05-31 18:09:24 +020039 * struct scmi_channel - Channel instance referenced in SCMI drivers
40 * @ref: Reference to local channel instance
41 **/
42struct scmi_channel {
43 struct scmi_optee_channel ref;
44};
45
46/**
Etienne Carriered4932bd2021-11-09 17:08:24 +010047 * struct channel_session - Aggreates SCMI service session context references
48 * @tee: OP-TEE device to invoke
49 * @tee_session: OP-TEE session identifier
50 * @tee_shm: Dynamically allocated OP-TEE shared memory, or NULL
51 * @channel_hdl: Channel handle provided by OP-TEE SCMI service
52 */
53struct channel_session {
54 struct udevice *tee;
55 u32 tee_session;
56 struct tee_shm *tee_shm;
57 u32 channel_hdl;
58};
59
60#define TA_SCMI_UUID { 0xa8cfe406, 0xd4f5, 0x4a2e, \
61 { 0x9f, 0x8d, 0xa2, 0x5d, 0xc7, 0x54, 0xc0, 0x99 } }
62
63enum optee_smci_pta_cmd {
64 /*
65 * PTA_SCMI_CMD_CAPABILITIES - Get channel capabilities
66 *
67 * [out] value[0].a: Capability bit mask (enum pta_scmi_caps)
68 * [out] value[0].b: Extended capabilities or 0
69 */
70 PTA_SCMI_CMD_CAPABILITIES = 0,
71
72 /*
73 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL - Process SCMI message in SMT buffer
74 *
75 * [in] value[0].a: Channel handle
76 *
77 * Shared memory used for SCMI message/response exhange is expected
78 * already identified and bound to channel handle in both SCMI agent
79 * and SCMI server (OP-TEE) parts.
80 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
81 * protocol message ID).
82 */
83 PTA_SCMI_CMD_PROCESS_SMT_CHANNEL = 1,
84
85 /*
86 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE - Process SMT/SCMI message
87 *
88 * [in] value[0].a: Channel handle
89 * [in/out] memref[1]: Message/response buffer (SMT and SCMI payload)
90 *
91 * Shared memory used for SCMI message/response is a SMT buffer
92 * referenced by param[1]. It shall be 128 bytes large to fit response
93 * payload whatever message playload size.
94 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
95 * protocol message ID).
96 */
97 PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE = 2,
98
99 /*
100 * PTA_SCMI_CMD_GET_CHANNEL - Get channel handle
101 *
Etienne Carriered4932bd2021-11-09 17:08:24 +0100102 * [in] value[0].a: Channel identifier
103 * [out] value[0].a: Returned channel handle
104 * [in] value[0].b: Requested capabilities mask (enum pta_scmi_caps)
105 */
106 PTA_SCMI_CMD_GET_CHANNEL = 3,
Etienne Carrierec624c972022-05-31 18:09:16 +0200107
108 /*
109 * PTA_SCMI_CMD_PROCESS_MSG_CHANNEL - Process SCMI message in MSG
110 * buffers pointed by memref parameters
111 *
112 * [in] value[0].a: Channel handle
113 * [in] memref[1]: Message buffer (MSG header and SCMI payload)
114 * [out] memref[2]: Response buffer (MSG header and SCMI payload)
115 *
116 * Shared memories used for SCMI message/response are MSG buffers
117 * referenced by param[1] and param[2]. MSG transport protocol
118 * uses a 32bit header to carry SCMI meta-data (protocol ID and
119 * protocol message ID) followed by the effective SCMI message
120 * payload.
121 */
122 PTA_SCMI_CMD_PROCESS_MSG_CHANNEL = 4,
Etienne Carriered4932bd2021-11-09 17:08:24 +0100123};
124
125/*
126 * OP-TEE SCMI service capabilities bit flags (32bit)
127 *
128 * PTA_SCMI_CAPS_SMT_HEADER
129 * When set, OP-TEE supports command using SMT header protocol (SCMI shmem) in
130 * shared memory buffers to carry SCMI protocol synchronisation information.
Etienne Carrierec624c972022-05-31 18:09:16 +0200131 *
132 * PTA_SCMI_CAPS_MSG_HEADER
133 * When set, OP-TEE supports command using MSG header protocol in an OP-TEE
134 * shared memory to carry SCMI protocol synchronisation information and SCMI
135 * message payload.
Etienne Carriered4932bd2021-11-09 17:08:24 +0100136 */
137#define PTA_SCMI_CAPS_NONE 0
138#define PTA_SCMI_CAPS_SMT_HEADER BIT(0)
Etienne Carrierec624c972022-05-31 18:09:16 +0200139#define PTA_SCMI_CAPS_MSG_HEADER BIT(1)
140#define PTA_SCMI_CAPS_MASK (PTA_SCMI_CAPS_SMT_HEADER | \
141 PTA_SCMI_CAPS_MSG_HEADER)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100142
Etienne Carriere6ae56442022-05-31 18:09:24 +0200143static int open_channel(struct udevice *dev, struct scmi_optee_channel *chan,
144 struct channel_session *sess)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100145{
146 const struct tee_optee_ta_uuid uuid = TA_SCMI_UUID;
Etienne Carriered4932bd2021-11-09 17:08:24 +0100147 struct tee_open_session_arg sess_arg = { };
148 struct tee_invoke_arg cmd_arg = { };
149 struct tee_param param[1] = { };
150 int ret;
151
152 memset(sess, 0, sizeof(sess));
153
154 sess->tee = tee_find_device(NULL, NULL, NULL, NULL);
155 if (!sess->tee)
156 return -ENODEV;
157
158 sess_arg.clnt_login = TEE_LOGIN_REE_KERNEL;
159 tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid);
160
161 ret = tee_open_session(sess->tee, &sess_arg, 0, NULL);
162 if (ret) {
163 dev_err(dev, "can't open session: %d\n", ret);
164 return ret;
165 }
166
167 cmd_arg.func = PTA_SCMI_CMD_GET_CHANNEL;
168 cmd_arg.session = sess_arg.session;
169
170 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INOUT;
171 param[0].u.value.a = chan->channel_id;
Etienne Carrierec624c972022-05-31 18:09:16 +0200172 if (chan->dyn_shm)
173 param[0].u.value.b = PTA_SCMI_CAPS_MSG_HEADER;
174 else
175 param[0].u.value.b = PTA_SCMI_CAPS_SMT_HEADER;
Etienne Carriered4932bd2021-11-09 17:08:24 +0100176
177 ret = tee_invoke_func(sess->tee, &cmd_arg, ARRAY_SIZE(param), param);
178 if (ret || cmd_arg.ret) {
179 dev_err(dev, "Invoke failed: %d, 0x%x\n", ret, cmd_arg.ret);
180 if (!ret)
181 ret = -EPROTO;
182
183 tee_close_session(sess->tee, sess_arg.session);
184 return ret;
185 }
186
187 sess->tee_session = sess_arg.session;
188 sess->channel_hdl = param[0].u.value.a;
189
190 return 0;
191}
192
193static void close_channel(struct channel_session *sess)
194{
195 tee_close_session(sess->tee, sess->tee_session);
196}
197
Etienne Carriere6ae56442022-05-31 18:09:24 +0200198static int invoke_cmd(struct udevice *dev, struct scmi_optee_channel *chan,
199 struct channel_session *sess, struct scmi_msg *msg)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100200{
Etienne Carriered4932bd2021-11-09 17:08:24 +0100201 struct tee_invoke_arg arg = { };
Etienne Carrierec624c972022-05-31 18:09:16 +0200202 struct tee_param param[3] = { };
Etienne Carriered4932bd2021-11-09 17:08:24 +0100203 int ret;
204
Etienne Carriered4932bd2021-11-09 17:08:24 +0100205 arg.session = sess->tee_session;
206 param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
207 param[0].u.value.a = sess->channel_hdl;
208
Etienne Carrierec624c972022-05-31 18:09:16 +0200209 if (sess->tee_shm) {
210 size_t in_size;
211
212 ret = scmi_msg_to_smt_msg(dev, &chan->smt, msg, &in_size);
213 if (ret < 0)
214 return ret;
215
216 arg.func = PTA_SCMI_CMD_PROCESS_MSG_CHANNEL;
217 param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
Etienne Carriered4932bd2021-11-09 17:08:24 +0100218 param[1].u.memref.shm = sess->tee_shm;
Etienne Carrierec624c972022-05-31 18:09:16 +0200219 param[1].u.memref.size = in_size;
220 param[2].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
221 param[2].u.memref.shm = sess->tee_shm;
222 param[2].u.memref.size = sess->tee_shm->size;
Etienne Carriered4932bd2021-11-09 17:08:24 +0100223 } else {
224 arg.func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL;
Etienne Carrierec624c972022-05-31 18:09:16 +0200225 scmi_write_msg_to_smt(dev, &chan->smt, msg);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100226 }
227
228 ret = tee_invoke_func(sess->tee, &arg, ARRAY_SIZE(param), param);
229 if (ret || arg.ret) {
230 if (!ret)
231 ret = -EPROTO;
Etienne Carrierec624c972022-05-31 18:09:16 +0200232
233 return ret;
234 }
235
236 if (sess->tee_shm) {
237 ret = scmi_msg_from_smt_msg(dev, &chan->smt, msg,
238 param[2].u.memref.size);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100239 } else {
240 ret = scmi_read_resp_from_smt(dev, &chan->smt, msg);
Etienne Carrierec624c972022-05-31 18:09:16 +0200241 scmi_clear_smt_channel(&chan->smt);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100242 }
243
Etienne Carriered4932bd2021-11-09 17:08:24 +0100244 return ret;
245}
246
Etienne Carriere6ae56442022-05-31 18:09:24 +0200247static int prepare_shm(struct udevice *dev, struct scmi_optee_channel *chan,
248 struct channel_session *sess)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100249{
Etienne Carriered4932bd2021-11-09 17:08:24 +0100250 int ret;
251
252 /* Static shm is already prepared by the firmware: nothing to do */
253 if (!chan->dyn_shm)
254 return 0;
255
256 chan->smt.size = SCMI_SHM_SIZE;
257
258 ret = tee_shm_alloc(sess->tee, chan->smt.size, 0, &sess->tee_shm);
259 if (ret) {
260 dev_err(dev, "Failed to allocated shmem: %d\n", ret);
261 return ret;
262 }
263
264 chan->smt.buf = sess->tee_shm->addr;
265
Etienne Carriered4932bd2021-11-09 17:08:24 +0100266 return 0;
267}
268
269static void release_shm(struct udevice *dev, struct channel_session *sess)
270{
271 struct scmi_optee_channel *chan = dev_get_plat(dev);
272
273 if (chan->dyn_shm)
274 tee_shm_free(sess->tee_shm);
275}
276
Etienne Carriere05440292022-05-31 18:09:19 +0200277static int scmi_optee_process_msg(struct udevice *dev,
278 struct scmi_channel *channel,
279 struct scmi_msg *msg)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100280{
Etienne Carriere76b690a2022-05-31 18:09:29 +0200281 struct scmi_optee_channel *chan = &channel->ref;
Etienne Carrierec624c972022-05-31 18:09:16 +0200282 struct channel_session sess = { };
Etienne Carriered4932bd2021-11-09 17:08:24 +0100283 int ret;
284
Etienne Carriere6ae56442022-05-31 18:09:24 +0200285 ret = open_channel(dev, chan, &sess);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100286 if (ret)
287 return ret;
288
Etienne Carriere6ae56442022-05-31 18:09:24 +0200289 ret = prepare_shm(dev, chan, &sess);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100290 if (ret)
291 goto out;
292
Etienne Carriere6ae56442022-05-31 18:09:24 +0200293 ret = invoke_cmd(dev, chan, &sess, msg);
Etienne Carriered4932bd2021-11-09 17:08:24 +0100294
295 release_shm(dev, &sess);
296
297out:
298 close_channel(&sess);
299
300 return ret;
301}
302
Etienne Carriere6ae56442022-05-31 18:09:24 +0200303static int setup_channel(struct udevice *dev, struct scmi_optee_channel *chan)
Etienne Carriered4932bd2021-11-09 17:08:24 +0100304{
Etienne Carriered4932bd2021-11-09 17:08:24 +0100305 int ret;
306
307 if (dev_read_u32(dev, "linaro,optee-channel-id", &chan->channel_id)) {
308 dev_err(dev, "Missing property linaro,optee-channel-id\n");
309 return -EINVAL;
310 }
311
312 if (dev_read_prop(dev, "shmem", NULL)) {
313 ret = scmi_dt_get_smt_buffer(dev, &chan->smt);
314 if (ret) {
315 dev_err(dev, "Failed to get smt resources: %d\n", ret);
316 return ret;
317 }
318 chan->dyn_shm = false;
319 } else {
320 chan->dyn_shm = true;
321 }
322
323 return 0;
324}
325
Etienne Carriere6ae56442022-05-31 18:09:24 +0200326static int scmi_optee_get_channel(struct udevice *dev,
327 struct scmi_channel **channel)
328{
329 struct scmi_optee_channel *base_chan = dev_get_plat(dev->parent);
330 struct scmi_optee_channel *chan;
331 u32 channel_id;
332 int ret;
333
334 if (dev_read_u32(dev, "linaro,optee-channel-id", &channel_id)) {
335 /* 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
346 ret = setup_channel(dev, chan);
347 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};