aboutsummaryrefslogtreecommitdiff
path: root/drivers/firmware/scmi/optee_agent.c
blob: 48dbb88a3fb504ff9ca18eab58e36230b3ed6516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2020-2022 Linaro Limited.
 */

#define LOG_CATEGORY UCLASS_SCMI_AGENT

#include <common.h>
#include <dm.h>
#include <errno.h>
#include <scmi_agent.h>
#include <scmi_agent-uclass.h>
#include <string.h>
#include <tee.h>
#include <asm/types.h>
#include <dm/device_compat.h>
#include <dm/devres.h>
#include <linux/arm-smccc.h>
#include <linux/bug.h>
#include <linux/compat.h>

#include "smt.h"

#define SCMI_SHM_SIZE		128

/**
 * struct scmi_optee_channel - Description of an SCMI OP-TEE transport
 * @channel_id:		Channel identifier
 * @smt:		Shared memory buffer with synchronisation protocol
 * @dyn_shm:		True if using dynamically allocated shared memory
 */
struct scmi_optee_channel {
	unsigned int channel_id;
	struct scmi_smt smt;
	bool dyn_shm;
};

/**
 * struct scmi_channel - Channel instance referenced in SCMI drivers
 * @ref: Reference to local channel instance
 **/
struct scmi_channel {
	struct scmi_optee_channel ref;
};

/**
 * struct channel_session - Aggreates SCMI service session context references
 * @tee:		OP-TEE device to invoke
 * @tee_session:	OP-TEE session identifier
 * @tee_shm:		Dynamically allocated OP-TEE shared memory, or NULL
 * @channel_hdl:	Channel handle provided by OP-TEE SCMI service
 */
struct channel_session {
	struct udevice *tee;
	u32 tee_session;
	struct tee_shm *tee_shm;
	u32 channel_hdl;
};

#define TA_SCMI_UUID { 0xa8cfe406, 0xd4f5, 0x4a2e, \
		      { 0x9f, 0x8d, 0xa2, 0x5d, 0xc7, 0x54, 0xc0, 0x99 } }

enum optee_smci_pta_cmd {
	/*
	 * PTA_SCMI_CMD_CAPABILITIES - Get channel capabilities
	 *
	 * [out]    value[0].a: Capability bit mask (enum pta_scmi_caps)
	 * [out]    value[0].b: Extended capabilities or 0
	 */
	PTA_SCMI_CMD_CAPABILITIES = 0,

	/*
	 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL - Process SCMI message in SMT buffer
	 *
	 * [in]     value[0].a: Channel handle
	 *
	 * Shared memory used for SCMI message/response exhange is expected
	 * already identified and bound to channel handle in both SCMI agent
	 * and SCMI server (OP-TEE) parts.
	 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
	 * protocol message ID).
	 */
	PTA_SCMI_CMD_PROCESS_SMT_CHANNEL = 1,

	/*
	 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE - Process SMT/SCMI message
	 *
	 * [in]     value[0].a: Channel handle
	 * [in/out] memref[1]: Message/response buffer (SMT and SCMI payload)
	 *
	 * Shared memory used for SCMI message/response is a SMT buffer
	 * referenced by param[1]. It shall be 128 bytes large to fit response
	 * payload whatever message playload size.
	 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
	 * protocol message ID).
	 */
	PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE = 2,

	/*
	 * PTA_SCMI_CMD_GET_CHANNEL - Get channel handle
	 *
	 * [in]     value[0].a: Channel identifier
	 * [out]    value[0].a: Returned channel handle
	 * [in]     value[0].b: Requested capabilities mask (enum pta_scmi_caps)
	 */
	PTA_SCMI_CMD_GET_CHANNEL = 3,

	/*
	 * PTA_SCMI_CMD_PROCESS_MSG_CHANNEL - Process SCMI message in MSG
	 * buffers pointed by memref parameters
	 *
	 * [in]     value[0].a: Channel handle
	 * [in]     memref[1]: Message buffer (MSG header and SCMI payload)
	 * [out]    memref[2]: Response buffer (MSG header and SCMI payload)
	 *
	 * Shared memories used for SCMI message/response are MSG buffers
	 * referenced by param[1] and param[2]. MSG transport protocol
	 * uses a 32bit header to carry SCMI meta-data (protocol ID and
	 * protocol message ID) followed by the effective SCMI message
	 * payload.
	 */
	PTA_SCMI_CMD_PROCESS_MSG_CHANNEL = 4,
};

/*
 * OP-TEE SCMI service capabilities bit flags (32bit)
 *
 * PTA_SCMI_CAPS_SMT_HEADER
 * When set, OP-TEE supports command using SMT header protocol (SCMI shmem) in
 * shared memory buffers to carry SCMI protocol synchronisation information.
 *
 * PTA_SCMI_CAPS_MSG_HEADER
 * When set, OP-TEE supports command using MSG header protocol in an OP-TEE
 * shared memory to carry SCMI protocol synchronisation information and SCMI
 * message payload.
 */
#define PTA_SCMI_CAPS_NONE		0
#define PTA_SCMI_CAPS_SMT_HEADER	BIT(0)
#define PTA_SCMI_CAPS_MSG_HEADER	BIT(1)
#define PTA_SCMI_CAPS_MASK		(PTA_SCMI_CAPS_SMT_HEADER | \
					 PTA_SCMI_CAPS_MSG_HEADER)

static int open_channel(struct udevice *dev, struct scmi_optee_channel *chan,
			struct channel_session *sess)
{
	const struct tee_optee_ta_uuid uuid = TA_SCMI_UUID;
	struct tee_open_session_arg sess_arg = { };
	struct tee_invoke_arg cmd_arg = { };
	struct tee_param param[1] = { };
	int ret;

	memset(sess, 0, sizeof(*sess));

	sess->tee = tee_find_device(NULL, NULL, NULL, NULL);
	if (!sess->tee)
		return -ENODEV;

	sess_arg.clnt_login = TEE_LOGIN_REE_KERNEL;
	tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid);

	ret = tee_open_session(sess->tee, &sess_arg, 0, NULL);
	if (ret) {
		dev_err(dev, "can't open session: %d\n", ret);
		return ret;
	}

	cmd_arg.func = PTA_SCMI_CMD_GET_CHANNEL;
	cmd_arg.session = sess_arg.session;

	param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INOUT;
	param[0].u.value.a = chan->channel_id;
	if (chan->dyn_shm)
		param[0].u.value.b = PTA_SCMI_CAPS_MSG_HEADER;
	else
		param[0].u.value.b = PTA_SCMI_CAPS_SMT_HEADER;

	ret = tee_invoke_func(sess->tee, &cmd_arg, ARRAY_SIZE(param), param);
	if (ret || cmd_arg.ret) {
		dev_err(dev, "Invoke failed: %d, 0x%x\n", ret, cmd_arg.ret);
		if (!ret)
			ret = -EPROTO;

		tee_close_session(sess->tee, sess_arg.session);
		return ret;
	}

	sess->tee_session = sess_arg.session;
	sess->channel_hdl = param[0].u.value.a;

	return 0;
}

static void close_channel(struct channel_session *sess)
{
	tee_close_session(sess->tee, sess->tee_session);
}

static int invoke_cmd(struct udevice *dev, struct scmi_optee_channel *chan,
		      struct channel_session *sess, struct scmi_msg *msg)
{
	struct tee_invoke_arg arg = { };
	struct tee_param param[3] = { };
	int ret;

	arg.session = sess->tee_session;
	param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
	param[0].u.value.a = sess->channel_hdl;

	if (sess->tee_shm) {
		size_t in_size;

		ret = scmi_msg_to_smt_msg(dev, &chan->smt, msg, &in_size);
		if (ret < 0)
			return ret;

		arg.func = PTA_SCMI_CMD_PROCESS_MSG_CHANNEL;
		param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
		param[1].u.memref.shm = sess->tee_shm;
		param[1].u.memref.size = in_size;
		param[2].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
		param[2].u.memref.shm = sess->tee_shm;
		param[2].u.memref.size = sess->tee_shm->size;
	} else {
		arg.func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL;
		scmi_write_msg_to_smt(dev, &chan->smt, msg);
	}

	ret = tee_invoke_func(sess->tee, &arg, ARRAY_SIZE(param), param);
	if (ret || arg.ret) {
		if (!ret)
			ret = -EPROTO;

		return ret;
	}

	if (sess->tee_shm) {
		ret = scmi_msg_from_smt_msg(dev, &chan->smt, msg,
					    param[2].u.memref.size);
	} else {
		ret = scmi_read_resp_from_smt(dev, &chan->smt, msg);
		scmi_clear_smt_channel(&chan->smt);
	}

	return ret;
}

static int prepare_shm(struct udevice *dev, struct scmi_optee_channel *chan,
		       struct channel_session *sess)
{
	int ret;

	/* Static shm is already prepared by the firmware: nothing to do */
	if (!chan->dyn_shm)
		return 0;

	chan->smt.size = SCMI_SHM_SIZE;

	ret = tee_shm_alloc(sess->tee, chan->smt.size, 0, &sess->tee_shm);
	if (ret) {
		dev_err(dev, "Failed to allocated shmem: %d\n", ret);
		return ret;
	}

	chan->smt.buf = sess->tee_shm->addr;

	return 0;
}

static void release_shm(struct udevice *dev, struct channel_session *sess)
{
	struct scmi_optee_channel *chan = dev_get_plat(dev);

	if (chan->dyn_shm)
		tee_shm_free(sess->tee_shm);
}

static int scmi_optee_process_msg(struct udevice *dev,
				  struct scmi_channel *channel,
				  struct scmi_msg *msg)
{
	struct scmi_optee_channel *chan = &channel->ref;
	struct channel_session sess = { };
	int ret;

	ret = open_channel(dev, chan, &sess);
	if (ret)
		return ret;

	ret = prepare_shm(dev, chan, &sess);
	if (ret)
		goto out;

	ret = invoke_cmd(dev, chan, &sess, msg);

	release_shm(dev, &sess);

out:
	close_channel(&sess);

	return ret;
}

static int setup_channel(struct udevice *dev, struct scmi_optee_channel *chan)
{
	int ret;

	if (dev_read_u32(dev, "linaro,optee-channel-id", &chan->channel_id)) {
		dev_err(dev, "Missing property linaro,optee-channel-id\n");
		return -EINVAL;
	}

	if (dev_read_prop(dev, "shmem", NULL)) {
		ret = scmi_dt_get_smt_buffer(dev, &chan->smt);
		if (ret) {
			dev_err(dev, "Failed to get smt resources: %d\n", ret);
			return ret;
		}
		chan->dyn_shm = false;
	} else {
		chan->dyn_shm = true;
	}

	return 0;
}

static int scmi_optee_get_channel(struct udevice *dev,
				  struct udevice *protocol,
				  struct scmi_channel **channel)
{
	struct scmi_optee_channel *base_chan = dev_get_plat(dev);
	struct scmi_optee_channel *chan;
	u32 channel_id;
	int ret;

	if (dev_read_u32(protocol, "linaro,optee-channel-id", &channel_id)) {
		/* Uses agent base channel */
		*channel = container_of(base_chan, struct scmi_channel, ref);

		return 0;
	}

	/* Setup a dedicated channel */
	chan = calloc(1, sizeof(*chan));
	if (!chan)
		return -ENOMEM;

	ret = setup_channel(protocol, chan);
	if (ret) {
		free(chan);
		return ret;
	}

	*channel = container_of(chan, struct scmi_channel, ref);

	return 0;
}

static int scmi_optee_of_to_plat(struct udevice *dev)
{
	struct scmi_optee_channel *chan = dev_get_plat(dev);

	return setup_channel(dev, chan);
}

static int scmi_optee_probe(struct udevice *dev)
{
	struct scmi_optee_channel *chan = dev_get_plat(dev);
	struct channel_session sess;
	int ret;

	/* Check OP-TEE service acknowledges the SCMI channel */
	ret = open_channel(dev, chan, &sess);
	if (!ret)
		close_channel(&sess);

	return ret;
}

static const struct udevice_id scmi_optee_ids[] = {
	{ .compatible = "linaro,scmi-optee" },
	{ }
};

static const struct scmi_agent_ops scmi_optee_ops = {
	.of_get_channel = scmi_optee_get_channel,
	.process_msg = scmi_optee_process_msg,
};

U_BOOT_DRIVER(scmi_optee) = {
	.name		= "scmi-over-optee",
	.id		= UCLASS_SCMI_AGENT,
	.of_match	= scmi_optee_ids,
	.plat_auto	= sizeof(struct scmi_optee_channel),
	.of_to_plat	= scmi_optee_of_to_plat,
	.probe		= scmi_optee_probe,
	.flags		= DM_FLAG_OS_PREPARE,
	.ops		= &scmi_optee_ops,
};