Commit | Line | Data |
---|---|---|
5f90f189 EC |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Copyright (C) 2019-2021 Linaro Ltd. | |
4 | */ | |
5 | ||
6 | #include <linux/io.h> | |
7 | #include <linux/of.h> | |
8 | #include <linux/of_address.h> | |
9 | #include <linux/kernel.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/mutex.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/tee_drv.h> | |
14 | #include <linux/uuid.h> | |
15 | #include <uapi/linux/tee.h> | |
16 | ||
17 | #include "common.h" | |
18 | ||
19 | #define SCMI_OPTEE_MAX_MSG_SIZE 128 | |
20 | ||
21 | enum scmi_optee_pta_cmd { | |
22 | /* | |
23 | * PTA_SCMI_CMD_CAPABILITIES - Get channel capabilities | |
24 | * | |
25 | * [out] value[0].a: Capability bit mask (enum pta_scmi_caps) | |
26 | * [out] value[0].b: Extended capabilities or 0 | |
27 | */ | |
28 | PTA_SCMI_CMD_CAPABILITIES = 0, | |
29 | ||
30 | /* | |
31 | * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL - Process SCMI message in SMT buffer | |
32 | * | |
33 | * [in] value[0].a: Channel handle | |
34 | * | |
35 | * Shared memory used for SCMI message/response exhange is expected | |
36 | * already identified and bound to channel handle in both SCMI agent | |
37 | * and SCMI server (OP-TEE) parts. | |
38 | * The memory uses SMT header to carry SCMI meta-data (protocol ID and | |
39 | * protocol message ID). | |
40 | */ | |
41 | PTA_SCMI_CMD_PROCESS_SMT_CHANNEL = 1, | |
42 | ||
43 | /* | |
44 | * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE - Process SMT/SCMI message | |
45 | * | |
46 | * [in] value[0].a: Channel handle | |
47 | * [in/out] memref[1]: Message/response buffer (SMT and SCMI payload) | |
48 | * | |
49 | * Shared memory used for SCMI message/response is a SMT buffer | |
50 | * referenced by param[1]. It shall be 128 bytes large to fit response | |
51 | * payload whatever message playload size. | |
52 | * The memory uses SMT header to carry SCMI meta-data (protocol ID and | |
53 | * protocol message ID). | |
54 | */ | |
55 | PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE = 2, | |
56 | ||
57 | /* | |
58 | * PTA_SCMI_CMD_GET_CHANNEL - Get channel handle | |
59 | * | |
60 | * SCMI shm information are 0 if agent expects to use OP-TEE regular SHM | |
61 | * | |
62 | * [in] value[0].a: Channel identifier | |
63 | * [out] value[0].a: Returned channel handle | |
64 | * [in] value[0].b: Requested capabilities mask (enum pta_scmi_caps) | |
65 | */ | |
66 | PTA_SCMI_CMD_GET_CHANNEL = 3, | |
67 | }; | |
68 | ||
69 | /* | |
70 | * OP-TEE SCMI service capabilities bit flags (32bit) | |
71 | * | |
72 | * PTA_SCMI_CAPS_SMT_HEADER | |
73 | * When set, OP-TEE supports command using SMT header protocol (SCMI shmem) in | |
74 | * shared memory buffers to carry SCMI protocol synchronisation information. | |
75 | */ | |
76 | #define PTA_SCMI_CAPS_NONE 0 | |
77 | #define PTA_SCMI_CAPS_SMT_HEADER BIT(0) | |
78 | ||
79 | /** | |
80 | * struct scmi_optee_channel - Description of an OP-TEE SCMI channel | |
81 | * | |
82 | * @channel_id: OP-TEE channel ID used for this transport | |
83 | * @tee_session: TEE session identifier | |
84 | * @caps: OP-TEE SCMI channel capabilities | |
85 | * @mu: Mutex protection on channel access | |
86 | * @cinfo: SCMI channel information | |
87 | * @shmem: Virtual base address of the shared memory | |
88 | * @tee_shm: Reference to TEE shared memory or NULL if using static shmem | |
89 | * @link: Reference in agent's channel list | |
90 | */ | |
91 | struct scmi_optee_channel { | |
92 | u32 channel_id; | |
93 | u32 tee_session; | |
94 | u32 caps; | |
95 | struct mutex mu; | |
96 | struct scmi_chan_info *cinfo; | |
97 | struct scmi_shared_mem __iomem *shmem; | |
98 | struct tee_shm *tee_shm; | |
99 | struct list_head link; | |
100 | }; | |
101 | ||
102 | /** | |
103 | * struct scmi_optee_agent - OP-TEE transport private data | |
104 | * | |
105 | * @dev: Device used for communication with TEE | |
106 | * @tee_ctx: TEE context used for communication | |
107 | * @caps: Supported channel capabilities | |
108 | * @mu: Mutex for protection of @channel_list | |
109 | * @channel_list: List of all created channels for the agent | |
110 | */ | |
111 | struct scmi_optee_agent { | |
112 | struct device *dev; | |
113 | struct tee_context *tee_ctx; | |
114 | u32 caps; | |
115 | struct mutex mu; | |
116 | struct list_head channel_list; | |
117 | }; | |
118 | ||
119 | /* There can be only 1 SCMI service in OP-TEE we connect to */ | |
120 | static struct scmi_optee_agent *scmi_optee_private; | |
121 | ||
122 | /* Forward reference to scmi_optee transport initialization */ | |
123 | static int scmi_optee_init(void); | |
124 | ||
125 | /* Open a session toward SCMI OP-TEE service with REE_KERNEL identity */ | |
126 | static int open_session(struct scmi_optee_agent *agent, u32 *tee_session) | |
127 | { | |
128 | struct device *dev = agent->dev; | |
129 | struct tee_client_device *scmi_pta = to_tee_client_device(dev); | |
130 | struct tee_ioctl_open_session_arg arg = { }; | |
131 | int ret; | |
132 | ||
133 | memcpy(arg.uuid, scmi_pta->id.uuid.b, TEE_IOCTL_UUID_LEN); | |
134 | arg.clnt_login = TEE_IOCTL_LOGIN_REE_KERNEL; | |
135 | ||
136 | ret = tee_client_open_session(agent->tee_ctx, &arg, NULL); | |
137 | if (ret < 0 || arg.ret) { | |
138 | dev_err(dev, "Can't open tee session: %d / %#x\n", ret, arg.ret); | |
139 | return -EOPNOTSUPP; | |
140 | } | |
141 | ||
142 | *tee_session = arg.session; | |
143 | ||
144 | return 0; | |
145 | } | |
146 | ||
147 | static void close_session(struct scmi_optee_agent *agent, u32 tee_session) | |
148 | { | |
149 | tee_client_close_session(agent->tee_ctx, tee_session); | |
150 | } | |
151 | ||
152 | static int get_capabilities(struct scmi_optee_agent *agent) | |
153 | { | |
154 | struct tee_ioctl_invoke_arg arg = { }; | |
155 | struct tee_param param[1] = { }; | |
156 | u32 caps; | |
157 | u32 tee_session; | |
158 | int ret; | |
159 | ||
160 | ret = open_session(agent, &tee_session); | |
161 | if (ret) | |
162 | return ret; | |
163 | ||
164 | arg.func = PTA_SCMI_CMD_CAPABILITIES; | |
165 | arg.session = tee_session; | |
166 | arg.num_params = 1; | |
167 | ||
168 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT; | |
169 | ||
170 | ret = tee_client_invoke_func(agent->tee_ctx, &arg, param); | |
171 | ||
172 | close_session(agent, tee_session); | |
173 | ||
174 | if (ret < 0 || arg.ret) { | |
175 | dev_err(agent->dev, "Can't get capabilities: %d / %#x\n", ret, arg.ret); | |
176 | return -EOPNOTSUPP; | |
177 | } | |
178 | ||
179 | caps = param[0].u.value.a; | |
180 | ||
181 | if (!(caps & PTA_SCMI_CAPS_SMT_HEADER)) { | |
182 | dev_err(agent->dev, "OP-TEE SCMI PTA doesn't support SMT\n"); | |
183 | return -EOPNOTSUPP; | |
184 | } | |
185 | ||
186 | agent->caps = caps; | |
187 | ||
188 | return 0; | |
189 | } | |
190 | ||
191 | static int get_channel(struct scmi_optee_channel *channel) | |
192 | { | |
193 | struct device *dev = scmi_optee_private->dev; | |
194 | struct tee_ioctl_invoke_arg arg = { }; | |
195 | struct tee_param param[1] = { }; | |
196 | unsigned int caps = PTA_SCMI_CAPS_SMT_HEADER; | |
197 | int ret; | |
198 | ||
199 | arg.func = PTA_SCMI_CMD_GET_CHANNEL; | |
200 | arg.session = channel->tee_session; | |
201 | arg.num_params = 1; | |
202 | ||
203 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT; | |
204 | param[0].u.value.a = channel->channel_id; | |
205 | param[0].u.value.b = caps; | |
206 | ||
207 | ret = tee_client_invoke_func(scmi_optee_private->tee_ctx, &arg, param); | |
208 | ||
209 | if (ret || arg.ret) { | |
210 | dev_err(dev, "Can't get channel with caps %#x: %d / %#x\n", caps, ret, arg.ret); | |
211 | return -EOPNOTSUPP; | |
212 | } | |
213 | ||
214 | /* From now on use channel identifer provided by OP-TEE SCMI service */ | |
215 | channel->channel_id = param[0].u.value.a; | |
216 | channel->caps = caps; | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
221 | static int invoke_process_smt_channel(struct scmi_optee_channel *channel) | |
222 | { | |
223 | struct tee_ioctl_invoke_arg arg = { }; | |
224 | struct tee_param param[2] = { }; | |
225 | int ret; | |
226 | ||
227 | arg.session = channel->tee_session; | |
228 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT; | |
229 | param[0].u.value.a = channel->channel_id; | |
230 | ||
231 | if (channel->tee_shm) { | |
232 | param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT; | |
233 | param[1].u.memref.shm = channel->tee_shm; | |
234 | param[1].u.memref.size = SCMI_OPTEE_MAX_MSG_SIZE; | |
235 | arg.num_params = 2; | |
236 | arg.func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE; | |
237 | } else { | |
238 | arg.num_params = 1; | |
239 | arg.func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL; | |
240 | } | |
241 | ||
242 | ret = tee_client_invoke_func(scmi_optee_private->tee_ctx, &arg, param); | |
243 | if (ret < 0 || arg.ret) { | |
244 | dev_err(scmi_optee_private->dev, "Can't invoke channel %u: %d / %#x\n", | |
245 | channel->channel_id, ret, arg.ret); | |
246 | return -EIO; | |
247 | } | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | static int scmi_optee_link_supplier(struct device *dev) | |
253 | { | |
254 | if (!scmi_optee_private) { | |
255 | if (scmi_optee_init()) | |
256 | dev_dbg(dev, "Optee bus not yet ready\n"); | |
257 | ||
258 | /* Wait for optee bus */ | |
259 | return -EPROBE_DEFER; | |
260 | } | |
261 | ||
262 | if (!device_link_add(dev, scmi_optee_private->dev, DL_FLAG_AUTOREMOVE_CONSUMER)) { | |
263 | dev_err(dev, "Adding link to supplier optee device failed\n"); | |
264 | return -ECANCELED; | |
265 | } | |
266 | ||
267 | return 0; | |
268 | } | |
269 | ||
270 | static bool scmi_optee_chan_available(struct device *dev, int idx) | |
271 | { | |
272 | u32 channel_id; | |
273 | ||
274 | return !of_property_read_u32_index(dev->of_node, "linaro,optee-channel-id", | |
275 | idx, &channel_id); | |
276 | } | |
277 | ||
278 | static void scmi_optee_clear_channel(struct scmi_chan_info *cinfo) | |
279 | { | |
280 | struct scmi_optee_channel *channel = cinfo->transport_info; | |
281 | ||
282 | shmem_clear_channel(channel->shmem); | |
283 | } | |
284 | ||
5f90f189 EC |
285 | static int setup_static_shmem(struct device *dev, struct scmi_chan_info *cinfo, |
286 | struct scmi_optee_channel *channel) | |
287 | { | |
288 | struct device_node *np; | |
289 | resource_size_t size; | |
290 | struct resource res; | |
291 | int ret; | |
292 | ||
293 | np = of_parse_phandle(cinfo->dev->of_node, "shmem", 0); | |
294 | if (!of_device_is_compatible(np, "arm,scmi-shmem")) { | |
295 | ret = -ENXIO; | |
296 | goto out; | |
297 | } | |
298 | ||
299 | ret = of_address_to_resource(np, 0, &res); | |
300 | if (ret) { | |
301 | dev_err(dev, "Failed to get SCMI Tx shared memory\n"); | |
302 | goto out; | |
303 | } | |
304 | ||
305 | size = resource_size(&res); | |
306 | ||
307 | channel->shmem = devm_ioremap(dev, res.start, size); | |
308 | if (!channel->shmem) { | |
309 | dev_err(dev, "Failed to ioremap SCMI Tx shared memory\n"); | |
310 | ret = -EADDRNOTAVAIL; | |
311 | goto out; | |
312 | } | |
313 | ||
314 | ret = 0; | |
315 | ||
316 | out: | |
317 | of_node_put(np); | |
318 | ||
319 | return ret; | |
320 | } | |
321 | ||
322 | static int setup_shmem(struct device *dev, struct scmi_chan_info *cinfo, | |
323 | struct scmi_optee_channel *channel) | |
324 | { | |
325 | if (of_find_property(cinfo->dev->of_node, "shmem", NULL)) | |
326 | return setup_static_shmem(dev, cinfo, channel); | |
327 | else | |
afc9c1e2 | 328 | return -ENOMEM; |
5f90f189 EC |
329 | } |
330 | ||
331 | static int scmi_optee_chan_setup(struct scmi_chan_info *cinfo, struct device *dev, bool tx) | |
332 | { | |
333 | struct scmi_optee_channel *channel; | |
334 | uint32_t channel_id; | |
335 | int ret; | |
336 | ||
337 | if (!tx) | |
338 | return -ENODEV; | |
339 | ||
340 | channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL); | |
341 | if (!channel) | |
342 | return -ENOMEM; | |
343 | ||
344 | ret = of_property_read_u32_index(cinfo->dev->of_node, "linaro,optee-channel-id", | |
345 | 0, &channel_id); | |
346 | if (ret) | |
347 | return ret; | |
348 | ||
349 | cinfo->transport_info = channel; | |
350 | channel->cinfo = cinfo; | |
351 | channel->channel_id = channel_id; | |
352 | mutex_init(&channel->mu); | |
353 | ||
354 | ret = setup_shmem(dev, cinfo, channel); | |
355 | if (ret) | |
356 | return ret; | |
357 | ||
358 | ret = open_session(scmi_optee_private, &channel->tee_session); | |
359 | if (ret) | |
360 | goto err_free_shm; | |
361 | ||
362 | ret = get_channel(channel); | |
363 | if (ret) | |
364 | goto err_close_sess; | |
365 | ||
366 | mutex_lock(&scmi_optee_private->mu); | |
367 | list_add(&channel->link, &scmi_optee_private->channel_list); | |
368 | mutex_unlock(&scmi_optee_private->mu); | |
369 | ||
370 | return 0; | |
371 | ||
372 | err_close_sess: | |
373 | close_session(scmi_optee_private, channel->tee_session); | |
374 | err_free_shm: | |
375 | if (channel->tee_shm) | |
376 | tee_shm_free(channel->tee_shm); | |
377 | ||
378 | return ret; | |
379 | } | |
380 | ||
381 | static int scmi_optee_chan_free(int id, void *p, void *data) | |
382 | { | |
383 | struct scmi_chan_info *cinfo = p; | |
384 | struct scmi_optee_channel *channel = cinfo->transport_info; | |
385 | ||
386 | mutex_lock(&scmi_optee_private->mu); | |
387 | list_del(&channel->link); | |
388 | mutex_unlock(&scmi_optee_private->mu); | |
389 | ||
390 | close_session(scmi_optee_private, channel->tee_session); | |
391 | ||
392 | if (channel->tee_shm) { | |
393 | tee_shm_free(channel->tee_shm); | |
394 | channel->tee_shm = NULL; | |
395 | } | |
396 | ||
397 | cinfo->transport_info = NULL; | |
398 | channel->cinfo = NULL; | |
399 | ||
400 | scmi_free_channel(cinfo, data, id); | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
405 | static struct scmi_shared_mem *get_channel_shm(struct scmi_optee_channel *chan, | |
406 | struct scmi_xfer *xfer) | |
407 | { | |
408 | if (!chan) | |
409 | return NULL; | |
410 | ||
411 | return chan->shmem; | |
412 | } | |
413 | ||
414 | ||
415 | static int scmi_optee_send_message(struct scmi_chan_info *cinfo, | |
416 | struct scmi_xfer *xfer) | |
417 | { | |
418 | struct scmi_optee_channel *channel = cinfo->transport_info; | |
419 | struct scmi_shared_mem *shmem = get_channel_shm(channel, xfer); | |
420 | int ret; | |
421 | ||
422 | mutex_lock(&channel->mu); | |
423 | shmem_tx_prepare(shmem, xfer); | |
424 | ||
425 | ret = invoke_process_smt_channel(channel); | |
426 | ||
427 | scmi_rx_callback(cinfo, shmem_read_header(shmem), NULL); | |
428 | mutex_unlock(&channel->mu); | |
429 | ||
430 | return ret; | |
431 | } | |
432 | ||
433 | static void scmi_optee_fetch_response(struct scmi_chan_info *cinfo, | |
434 | struct scmi_xfer *xfer) | |
435 | { | |
436 | struct scmi_optee_channel *channel = cinfo->transport_info; | |
437 | struct scmi_shared_mem *shmem = get_channel_shm(channel, xfer); | |
438 | ||
439 | shmem_fetch_response(shmem, xfer); | |
440 | } | |
441 | ||
442 | static bool scmi_optee_poll_done(struct scmi_chan_info *cinfo, | |
443 | struct scmi_xfer *xfer) | |
444 | { | |
445 | struct scmi_optee_channel *channel = cinfo->transport_info; | |
446 | struct scmi_shared_mem *shmem = get_channel_shm(channel, xfer); | |
447 | ||
448 | return shmem_poll_done(shmem, xfer); | |
449 | } | |
450 | ||
451 | static struct scmi_transport_ops scmi_optee_ops = { | |
452 | .link_supplier = scmi_optee_link_supplier, | |
453 | .chan_available = scmi_optee_chan_available, | |
454 | .chan_setup = scmi_optee_chan_setup, | |
455 | .chan_free = scmi_optee_chan_free, | |
456 | .send_message = scmi_optee_send_message, | |
457 | .fetch_response = scmi_optee_fetch_response, | |
458 | .clear_channel = scmi_optee_clear_channel, | |
459 | .poll_done = scmi_optee_poll_done, | |
460 | }; | |
461 | ||
462 | static int scmi_optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data) | |
463 | { | |
464 | return ver->impl_id == TEE_IMPL_ID_OPTEE; | |
465 | } | |
466 | ||
467 | static int scmi_optee_service_probe(struct device *dev) | |
468 | { | |
469 | struct scmi_optee_agent *agent; | |
470 | struct tee_context *tee_ctx; | |
471 | int ret; | |
472 | ||
473 | /* Only one SCMI OP-TEE device allowed */ | |
474 | if (scmi_optee_private) { | |
475 | dev_err(dev, "An SCMI OP-TEE device was already initialized: only one allowed\n"); | |
476 | return -EBUSY; | |
477 | } | |
478 | ||
479 | tee_ctx = tee_client_open_context(NULL, scmi_optee_ctx_match, NULL, NULL); | |
480 | if (IS_ERR(tee_ctx)) | |
481 | return -ENODEV; | |
482 | ||
483 | agent = devm_kzalloc(dev, sizeof(*agent), GFP_KERNEL); | |
484 | if (!agent) { | |
485 | ret = -ENOMEM; | |
486 | goto err; | |
487 | } | |
488 | ||
489 | agent->dev = dev; | |
490 | agent->tee_ctx = tee_ctx; | |
491 | INIT_LIST_HEAD(&agent->channel_list); | |
61bc76be | 492 | mutex_init(&agent->mu); |
5f90f189 EC |
493 | |
494 | ret = get_capabilities(agent); | |
495 | if (ret) | |
496 | goto err; | |
497 | ||
498 | /* Ensure agent resources are all visible before scmi_optee_private is */ | |
499 | smp_mb(); | |
500 | scmi_optee_private = agent; | |
501 | ||
502 | return 0; | |
503 | ||
504 | err: | |
505 | tee_client_close_context(tee_ctx); | |
506 | ||
507 | return ret; | |
508 | } | |
509 | ||
510 | static int scmi_optee_service_remove(struct device *dev) | |
511 | { | |
512 | struct scmi_optee_agent *agent = scmi_optee_private; | |
513 | ||
514 | if (!scmi_optee_private) | |
515 | return -EINVAL; | |
516 | ||
517 | if (!list_empty(&scmi_optee_private->channel_list)) | |
518 | return -EBUSY; | |
519 | ||
520 | /* Ensure cleared reference is visible before resources are released */ | |
521 | smp_store_mb(scmi_optee_private, NULL); | |
522 | ||
523 | tee_client_close_context(agent->tee_ctx); | |
524 | ||
525 | return 0; | |
526 | } | |
527 | ||
528 | static const struct tee_client_device_id scmi_optee_service_id[] = { | |
529 | { | |
530 | UUID_INIT(0xa8cfe406, 0xd4f5, 0x4a2e, | |
531 | 0x9f, 0x8d, 0xa2, 0x5d, 0xc7, 0x54, 0xc0, 0x99) | |
532 | }, | |
533 | { } | |
534 | }; | |
535 | ||
536 | MODULE_DEVICE_TABLE(tee, scmi_optee_service_id); | |
537 | ||
538 | static struct tee_client_driver scmi_optee_driver = { | |
539 | .id_table = scmi_optee_service_id, | |
540 | .driver = { | |
541 | .name = "scmi-optee", | |
542 | .bus = &tee_bus_type, | |
543 | .probe = scmi_optee_service_probe, | |
544 | .remove = scmi_optee_service_remove, | |
545 | }, | |
546 | }; | |
547 | ||
548 | static int scmi_optee_init(void) | |
549 | { | |
550 | return driver_register(&scmi_optee_driver.driver); | |
551 | } | |
552 | ||
553 | static void scmi_optee_exit(void) | |
554 | { | |
555 | if (scmi_optee_private) | |
556 | driver_unregister(&scmi_optee_driver.driver); | |
557 | } | |
558 | ||
559 | const struct scmi_desc scmi_optee_desc = { | |
560 | .transport_exit = scmi_optee_exit, | |
561 | .ops = &scmi_optee_ops, | |
562 | .max_rx_timeout_ms = 30, | |
563 | .max_msg = 20, | |
564 | .max_msg_size = SCMI_OPTEE_MAX_MSG_SIZE, | |
565 | }; |