History

Merge tag 'optee-async-notif-for-v5.17' of https://git.linaro.org/people/jens.wiklander/linux-tee into arm/drivers

OP-TEE Asynchronous notifications from secure world Adds support in the SMC based OP-TEE driver to receive asynchronous notifications from secure world using an edge-triggered interrupt as delivery mechanism. * tag 'optee-async-notif-for-v5.17' of https://git.linaro.org/people/jens.wiklander/linux-tee: optee: Fix NULL but dereferenced coccicheck error optee: add asynchronous notifications optee: separate notification functions tee: export teedev_open() and teedev_close_context() tee: fix put order in teedev_close_context() dt-bindings: arm: optee: add interrupt property docs: staging/tee.rst: add a section on OP-TEE notifications

Arnd Bergmann committed 4 years ago

Commit c6e5bda2 parents 5213313 b98aee414 files · +523 −124Browse files

Unified · Split

14 files
  1. Documentation/devicetree/bindings/arm/firmware/linaro,optee-tz.yaml +8 −0
  2. Documentation/staging/tee.rst +30 −0
  3. drivers/tee/optee/Makefile +1 −0
  4. drivers/tee/optee/core.c +1 −1
  5. drivers/tee/optee/ffa_abi.c +5 −1
  6. drivers/tee/optee/notif.c +125 −0
  7. drivers/tee/optee/optee_msg.h +9 −0
  8. drivers/tee/optee/optee_private.h +20 −8
  9. drivers/tee/optee/optee_rpc_cmd.h +17 −14
  10. drivers/tee/optee/optee_smc.h +73 −2
  11. drivers/tee/optee/rpc.c +7 −64
  12. drivers/tee/optee/smc_abi.c +206 −31
  13. drivers/tee/tee_core.c +7 −3
  14. include/linux/tee_drv.h +14 −0
Documentation/devicetree/bindings/arm/firmware/linaro,optee-tz.yaml+8−0
@@ -24,6 +24,12 @@ properties: · 23 unmodified lines
24 compatible:24 compatible:
25 const: linaro,optee-tz25 const: linaro,optee-tz
26 26
27 interrupts:
28 maxItems: 1
29 description: |
30 This interrupt which is used to signal an event by the secure world
31 software is expected to be edge-triggered.
32
27 method:33 method:
28 enum: [smc, hvc]34 enum: [smc, hvc]
29 description: |35 description: |
@@ -42,10 +48,12 @@ additionalProperties: false · 12 unmodified lines
42 48
43examples:49examples:
44 - |50 - |
51 #include <dt-bindings/interrupt-controller/arm-gic.h>
45 firmware {52 firmware {
46 optee {53 optee {
47 compatible = "linaro,optee-tz";54 compatible = "linaro,optee-tz";
48 method = "smc";55 method = "smc";
56 interrupts = <GIC_SPI 187 IRQ_TYPE_EDGE_RISING>;
49 };57 };
50 };58 };
51 59
Documentation/staging/tee.rst+30−0
@@ -184,6 +184,36 @@ order to support device enumeration. In other words, OP-TEE driver invokes this · 183 unmodified lines
184application to retrieve a list of Trusted Applications which can be registered184application to retrieve a list of Trusted Applications which can be registered
185as devices on the TEE bus.185as devices on the TEE bus.
186 186
187OP-TEE notifications
188--------------------
189
190There are two kinds of notifications that secure world can use to make
191normal world aware of some event.
192
1931. Synchronous notifications delivered with ``OPTEE_RPC_CMD_NOTIFICATION``
194 using the ``OPTEE_RPC_NOTIFICATION_SEND`` parameter.
1952. Asynchronous notifications delivered with a combination of a non-secure
196 edge-triggered interrupt and a fast call from the non-secure interrupt
197 handler.
198
199Synchronous notifications are limited by depending on RPC for delivery,
200this is only usable when secure world is entered with a yielding call via
201``OPTEE_SMC_CALL_WITH_ARG``. This excludes such notifications from secure
202world interrupt handlers.
203
204An asynchronous notification is delivered via a non-secure edge-triggered
205interrupt to an interrupt handler registered in the OP-TEE driver. The
206actual notification value are retrieved with the fast call
207``OPTEE_SMC_GET_ASYNC_NOTIF_VALUE``. Note that one interrupt can represent
208multiple notifications.
209
210One notification value ``OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_BOTTOM_HALF`` has a
211special meaning. When this value is received it means that normal world is
212supposed to make a yielding call ``OPTEE_MSG_CMD_DO_BOTTOM_HALF``. This
213call is done from the thread assisting the interrupt handler. This is a
214building block for OP-TEE OS in secure world to implement the top half and
215bottom half style of device drivers.
216
187AMD-TEE driver217AMD-TEE driver
188==============218==============
189 219
drivers/tee/optee/Makefile+1−0
@@ -2,6 +2,7 @@ · 1 unmodified line
2obj-$(CONFIG_OPTEE) += optee.o2obj-$(CONFIG_OPTEE) += optee.o
3optee-objs += core.o3optee-objs += core.o
4optee-objs += call.o4optee-objs += call.o
5optee-objs += notif.o
5optee-objs += rpc.o6optee-objs += rpc.o
6optee-objs += supp.o7optee-objs += supp.o
7optee-objs += device.o8optee-objs += device.o
drivers/tee/optee/core.c+1−1
@@ -159,6 +159,7 @@ void optee_remove_common(struct optee *optee) · 158 unmodified lines
159 /* Unregister OP-TEE specific client devices on TEE bus */159 /* Unregister OP-TEE specific client devices on TEE bus */
160 optee_unregister_devices();160 optee_unregister_devices();
161 161
162 optee_notif_uninit(optee);
162 /*163 /*
163 * The two devices have to be unregistered before we can free the164 * The two devices have to be unregistered before we can free the
164 * other resources.165 * other resources.
@@ -167,7 +168,6 @@ void optee_remove_common(struct optee *optee) · 2 unmodified lines
167 tee_device_unregister(optee->teedev);168 tee_device_unregister(optee->teedev);
168 169
169 tee_shm_pool_free(optee->pool);170 tee_shm_pool_free(optee->pool);
170 optee_wait_queue_exit(&optee->wait_queue);
171 optee_supp_uninit(&optee->supp);171 optee_supp_uninit(&optee->supp);
172 mutex_destroy(&optee->call_queue.mutex);172 mutex_destroy(&optee->call_queue.mutex);
173}173}
drivers/tee/optee/ffa_abi.c+5−1
@@ -856,9 +856,13 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) · 855 unmodified lines
856 mutex_init(&optee->ffa.mutex);856 mutex_init(&optee->ffa.mutex);
857 mutex_init(&optee->call_queue.mutex);857 mutex_init(&optee->call_queue.mutex);
858 INIT_LIST_HEAD(&optee->call_queue.waiters);858 INIT_LIST_HEAD(&optee->call_queue.waiters);
859 optee_wait_queue_init(&optee->wait_queue);
860 optee_supp_init(&optee->supp);859 optee_supp_init(&optee->supp);
861 ffa_dev_set_drvdata(ffa_dev, optee);860 ffa_dev_set_drvdata(ffa_dev, optee);
861 rc = optee_notif_init(optee, OPTEE_DEFAULT_MAX_NOTIF_VALUE);
862 if (rc) {
863 optee_ffa_remove(ffa_dev);
864 return rc;
865 }
862 866
863 rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);867 rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
864 if (rc) {868 if (rc) {
drivers/tee/optee/notif.c+125−0
@@ -0,0 +1,125 @@
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2015-2021, Linaro Limited
4 */
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8#include <linux/arm-smccc.h>
9#include <linux/errno.h>
10#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/tee_drv.h>
13#include "optee_private.h"
14
15struct notif_entry {
16 struct list_head link;
17 struct completion c;
18 u_int key;
19};
20
21static bool have_key(struct optee *optee, u_int key)
22{
23 struct notif_entry *entry;
24
25 list_for_each_entry(entry, &optee->notif.db, link)
26 if (entry->key == key)
27 return true;
28
29 return false;
30}
31
32int optee_notif_wait(struct optee *optee, u_int key)
33{
34 unsigned long flags;
35 struct notif_entry *entry;
36 int rc = 0;
37
38 if (key > optee->notif.max_key)
39 return -EINVAL;
40
41 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
42 if (!entry)
43 return -ENOMEM;
44 init_completion(&entry->c);
45 entry->key = key;
46
47 spin_lock_irqsave(&optee->notif.lock, flags);
48
49 /*
50 * If the bit is already set it means that the key has already
51 * been posted and we must not wait.
52 */
53 if (test_bit(key, optee->notif.bitmap)) {
54 clear_bit(key, optee->notif.bitmap);
55 goto out;
56 }
57
58 /*
59 * Check if someone is already waiting for this key. If there is
60 * it's a programming error.
61 */
62 if (have_key(optee, key)) {
63 rc = -EBUSY;
64 goto out;
65 }
66
67 list_add_tail(&entry->link, &optee->notif.db);
68
69 /*
70 * Unlock temporarily and wait for completion.
71 */
72 spin_unlock_irqrestore(&optee->notif.lock, flags);
73 wait_for_completion(&entry->c);
74 spin_lock_irqsave(&optee->notif.lock, flags);
75
76 list_del(&entry->link);
77out:
78 spin_unlock_irqrestore(&optee->notif.lock, flags);
79
80 kfree(entry);
81
82 return rc;
83}
84
85int optee_notif_send(struct optee *optee, u_int key)
86{
87 unsigned long flags;
88 struct notif_entry *entry;
89
90 if (key > optee->notif.max_key)
91 return -EINVAL;
92
93 spin_lock_irqsave(&optee->notif.lock, flags);
94
95 list_for_each_entry(entry, &optee->notif.db, link)
96 if (entry->key == key) {
97 complete(&entry->c);
98 goto out;
99 }
100
101 /* Only set the bit in case there where nobody waiting */
102 set_bit(key, optee->notif.bitmap);
103out:
104 spin_unlock_irqrestore(&optee->notif.lock, flags);
105
106 return 0;
107}
108
109int optee_notif_init(struct optee *optee, u_int max_key)
110{
111 spin_lock_init(&optee->notif.lock);
112 INIT_LIST_HEAD(&optee->notif.db);
113 optee->notif.bitmap = bitmap_zalloc(max_key, GFP_KERNEL);
114 if (!optee->notif.bitmap)
115 return -ENOMEM;
116
117 optee->notif.max_key = max_key;
118
119 return 0;
120}
121
122void optee_notif_uninit(struct optee *optee)
123{
124 kfree(optee->notif.bitmap);
125}
drivers/tee/optee/optee_msg.h+9−0
@@ -318,6 +318,13 @@ struct optee_msg_arg { · 317 unmodified lines
318 * [in] param[0].u.rmem.shm_ref holds shared memory reference318 * [in] param[0].u.rmem.shm_ref holds shared memory reference
319 * [in] param[0].u.rmem.offs 0319 * [in] param[0].u.rmem.offs 0
320 * [in] param[0].u.rmem.size 0320 * [in] param[0].u.rmem.size 0
321 *
322 * OPTEE_MSG_CMD_DO_BOTTOM_HALF does the scheduled bottom half processing
323 * of a driver.
324 *
325 * OPTEE_MSG_CMD_STOP_ASYNC_NOTIF informs secure world that from now is
326 * normal world unable to process asynchronous notifications. Typically
327 * used when the driver is shut down.
321 */328 */
322#define OPTEE_MSG_CMD_OPEN_SESSION 0329#define OPTEE_MSG_CMD_OPEN_SESSION 0
323#define OPTEE_MSG_CMD_INVOKE_COMMAND 1330#define OPTEE_MSG_CMD_INVOKE_COMMAND 1
@@ -325,6 +332,8 @@ struct optee_msg_arg { · 1 unmodified line
325#define OPTEE_MSG_CMD_CANCEL 3332#define OPTEE_MSG_CMD_CANCEL 3
326#define OPTEE_MSG_CMD_REGISTER_SHM 4333#define OPTEE_MSG_CMD_REGISTER_SHM 4
327#define OPTEE_MSG_CMD_UNREGISTER_SHM 5334#define OPTEE_MSG_CMD_UNREGISTER_SHM 5
335#define OPTEE_MSG_CMD_DO_BOTTOM_HALF 6
336#define OPTEE_MSG_CMD_STOP_ASYNC_NOTIF 7
328#define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004337#define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004
329 338
330#endif /* _OPTEE_MSG_H */339#endif /* _OPTEE_MSG_H */
drivers/tee/optee/optee_private.h+20−8
@@ -28,6 +28,13 @@ · 27 unmodified lines
28 28
29#define TEEC_ORIGIN_COMMS 0x0000000229#define TEEC_ORIGIN_COMMS 0x00000002
30 30
31/*
32 * This value should be larger than the number threads in secure world to
33 * meet the need from secure world. The number of threads in secure world
34 * are usually not even close to 255 so we should be safe for now.
35 */
36#define OPTEE_DEFAULT_MAX_NOTIF_VALUE 255
37
31typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,38typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
32 unsigned long, unsigned long, unsigned long,39 unsigned long, unsigned long, unsigned long,
33 unsigned long, unsigned long,40 unsigned long, unsigned long,
@@ -44,10 +51,13 @@ struct optee_call_queue { · 10 unmodified lines
44 struct list_head waiters;51 struct list_head waiters;
45};52};
46 53
47struct optee_wait_queue {54struct optee_notif {
48 /* Serializes access to this struct */55 u_int max_key;
49 struct mutex mu;56 struct tee_context *ctx;
57 /* Serializes access to the elements below in this struct */
58 spinlock_t lock;
50 struct list_head db;59 struct list_head db;
60 u_long *bitmap;
51};61};
52 62
53/**63/**
@@ -79,6 +89,7 @@ struct optee_smc { · 25 unmodified lines
79 optee_invoke_fn *invoke_fn;89 optee_invoke_fn *invoke_fn;
80 void *memremaped_shm;90 void *memremaped_shm;
81 u32 sec_caps;91 u32 sec_caps;
92 unsigned int notif_irq;
82};93};
83 94
84/**95/**
@@ -129,8 +140,7 @@ struct optee_ops { · 44 unmodified lines
129 * @smc: specific to SMC ABI140 * @smc: specific to SMC ABI
130 * @ffa: specific to FF-A ABI141 * @ffa: specific to FF-A ABI
131 * @call_queue: queue of threads waiting to call @invoke_fn142 * @call_queue: queue of threads waiting to call @invoke_fn
132 * @wait_queue: queue of threads from secure world waiting for a143 * @notif: notification synchronization struct
133 * secure world sync object
134 * @supp: supplicant synchronization struct for RPC to supplicant144 * @supp: supplicant synchronization struct for RPC to supplicant
135 * @pool: shared memory pool145 * @pool: shared memory pool
136 * @rpc_arg_count: If > 0 number of RPC parameters to make room for146 * @rpc_arg_count: If > 0 number of RPC parameters to make room for
@@ -147,7 +157,7 @@ struct optee { · 10 unmodified lines
147 struct optee_ffa ffa;157 struct optee_ffa ffa;
148 };158 };
149 struct optee_call_queue call_queue;159 struct optee_call_queue call_queue;
150 struct optee_wait_queue wait_queue;160 struct optee_notif notif;
151 struct optee_supp supp;161 struct optee_supp supp;
152 struct tee_shm_pool *pool;162 struct tee_shm_pool *pool;
153 unsigned int rpc_arg_count;163 unsigned int rpc_arg_count;
@@ -185,8 +195,10 @@ struct optee_call_ctx { · 31 unmodified lines
185 size_t num_entries;195 size_t num_entries;
186};196};
187 197
188void optee_wait_queue_init(struct optee_wait_queue *wq);198int optee_notif_init(struct optee *optee, u_int max_key);
189void optee_wait_queue_exit(struct optee_wait_queue *wq);199void optee_notif_uninit(struct optee *optee);
200int optee_notif_wait(struct optee *optee, u_int key);
201int optee_notif_send(struct optee *optee, u_int key);
190 202
191u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,203u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
192 struct tee_param *param);204 struct tee_param *param);
drivers/tee/optee/optee_rpc_cmd.h+17−14
@@ -28,24 +28,27 @@ · 27 unmodified lines
28#define OPTEE_RPC_CMD_GET_TIME 328#define OPTEE_RPC_CMD_GET_TIME 3
29 29
30/*30/*
31 * Wait queue primitive, helper for secure world to implement a wait queue.31 * Notification from/to secure world.
32 *32 *
33 * If secure world needs to wait for a secure world mutex it issues a sleep33 * If secure world needs to wait for something, for instance a mutex, it
34 * request instead of spinning in secure world. Conversely is a wakeup34 * does a notification wait request instead of spinning in secure world.
35 * request issued when a secure world mutex with a thread waiting thread is35 * Conversely can a synchronous notification can be sent when a secure
36 * unlocked.36 * world mutex with a thread waiting thread is unlocked.
37 *37 *
38 * Waiting on a key38 * This interface can also be used to wait for a asynchronous notification
39 * [in] value[0].a OPTEE_RPC_WAIT_QUEUE_SLEEP39 * which instead is sent via a non-secure interrupt.
40 * [in] value[0].b Wait key
41 *40 *
42 * Waking up a key41 * Waiting on notification
43 * [in] value[0].a OPTEE_RPC_WAIT_QUEUE_WAKEUP42 * [in] value[0].a OPTEE_RPC_NOTIFICATION_WAIT
44 * [in] value[0].b Wakeup key43 * [in] value[0].b notification value
44 *
45 * Sending a synchronous notification
46 * [in] value[0].a OPTEE_RPC_NOTIFICATION_SEND
47 * [in] value[0].b notification value
45 */48 */
46#define OPTEE_RPC_CMD_WAIT_QUEUE 449#define OPTEE_RPC_CMD_NOTIFICATION 4
47#define OPTEE_RPC_WAIT_QUEUE_SLEEP 050#define OPTEE_RPC_NOTIFICATION_WAIT 0
48#define OPTEE_RPC_WAIT_QUEUE_WAKEUP 151#define OPTEE_RPC_NOTIFICATION_SEND 1
49 52
50/*53/*
51 * Suspend execution54 * Suspend execution
drivers/tee/optee/optee_smc.h+73−2
@@ -107,6 +107,12 @@ struct optee_smc_call_get_os_revision_result { · 106 unmodified lines
107/*107/*
108 * Call with struct optee_msg_arg as argument108 * Call with struct optee_msg_arg as argument
109 *109 *
110 * When calling this function normal world has a few responsibilities:
111 * 1. It must be able to handle eventual RPCs
112 * 2. Non-secure interrupts should not be masked
113 * 3. If asynchronous notifications has been negotiated successfully, then
114 * asynchronous notifications should be unmasked during this call.
115 *
110 * Call register usage:116 * Call register usage:
111 * a0 SMC Function ID, OPTEE_SMC*CALL_WITH_ARG117 * a0 SMC Function ID, OPTEE_SMC*CALL_WITH_ARG
112 * a1 Upper 32 bits of a 64-bit physical pointer to a struct optee_msg_arg118 * a1 Upper 32 bits of a 64-bit physical pointer to a struct optee_msg_arg
@@ -195,7 +201,8 @@ struct optee_smc_get_shm_config_result { · 82 unmodified lines
195 * Normal return register usage:201 * Normal return register usage:
196 * a0 OPTEE_SMC_RETURN_OK202 * a0 OPTEE_SMC_RETURN_OK
197 * a1 bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*203 * a1 bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
198 * a2-7 Preserved204 * a2 The maximum secure world notification number
205 * a3-7 Preserved
199 *206 *
200 * Error return register usage:207 * Error return register usage:
201 * a0 OPTEE_SMC_RETURN_ENOTAVAIL, can't use the capabilities from normal world208 * a0 OPTEE_SMC_RETURN_ENOTAVAIL, can't use the capabilities from normal world
@@ -218,6 +225,8 @@ struct optee_smc_get_shm_config_result { · 16 unmodified lines
218#define OPTEE_SMC_SEC_CAP_VIRTUALIZATION BIT(3)225#define OPTEE_SMC_SEC_CAP_VIRTUALIZATION BIT(3)
219/* Secure world supports Shared Memory with a NULL reference */226/* Secure world supports Shared Memory with a NULL reference */
220#define OPTEE_SMC_SEC_CAP_MEMREF_NULL BIT(4)227#define OPTEE_SMC_SEC_CAP_MEMREF_NULL BIT(4)
228/* Secure world supports asynchronous notification of normal world */
229#define OPTEE_SMC_SEC_CAP_ASYNC_NOTIF BIT(5)
221 230
222#define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9231#define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9
223#define OPTEE_SMC_EXCHANGE_CAPABILITIES \232#define OPTEE_SMC_EXCHANGE_CAPABILITIES \
@@ -226,8 +235,8 @@ struct optee_smc_get_shm_config_result { · 2 unmodified lines
226struct optee_smc_exchange_capabilities_result {235struct optee_smc_exchange_capabilities_result {
227 unsigned long status;236 unsigned long status;
228 unsigned long capabilities;237 unsigned long capabilities;
238 unsigned long max_notif_value;
229 unsigned long reserved0;239 unsigned long reserved0;
230 unsigned long reserved1;
231};240};
232 241
233/*242/*
@@ -319,6 +328,68 @@ struct optee_smc_disable_shm_cache_result { · 85 unmodified lines
319#define OPTEE_SMC_GET_THREAD_COUNT \328#define OPTEE_SMC_GET_THREAD_COUNT \
320 OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_THREAD_COUNT)329 OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_THREAD_COUNT)
321 330
331/*
332 * Inform OP-TEE that normal world is able to receive asynchronous
333 * notifications.
334 *
335 * Call requests usage:
336 * a0 SMC Function ID, OPTEE_SMC_ENABLE_ASYNC_NOTIF
337 * a1-6 Not used
338 * a7 Hypervisor Client ID register
339 *
340 * Normal return register usage:
341 * a0 OPTEE_SMC_RETURN_OK
342 * a1-7 Preserved
343 *
344 * Not supported return register usage:
345 * a0 OPTEE_SMC_RETURN_ENOTAVAIL
346 * a1-7 Preserved
347 */
348#define OPTEE_SMC_FUNCID_ENABLE_ASYNC_NOTIF 16
349#define OPTEE_SMC_ENABLE_ASYNC_NOTIF \
350 OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_ENABLE_ASYNC_NOTIF)
351
352/*
353 * Retrieve a value of notifications pending since the last call of this
354 * function.
355 *
356 * OP-TEE keeps a record of all posted values. When an interrupt is
357 * received which indicates that there are posted values this function
358 * should be called until all pended values have been retrieved. When a
359 * value is retrieved, it's cleared from the record in secure world.
360 *
361 * Call requests usage:
362 * a0 SMC Function ID, OPTEE_SMC_GET_ASYNC_NOTIF_VALUE
363 * a1-6 Not used
364 * a7 Hypervisor Client ID register
365 *
366 * Normal return register usage:
367 * a0 OPTEE_SMC_RETURN_OK
368 * a1 value
369 * a2 Bit[0]: OPTEE_SMC_ASYNC_NOTIF_VALUE_VALID if the value in a1 is
370 * valid, else 0 if no values where pending
371 * a2 Bit[1]: OPTEE_SMC_ASYNC_NOTIF_VALUE_PENDING if another value is
372 * pending, else 0.
373 * Bit[31:2]: MBZ
374 * a3-7 Preserved
375 *
376 * Not supported return register usage:
377 * a0 OPTEE_SMC_RETURN_ENOTAVAIL
378 * a1-7 Preserved
379 */
380#define OPTEE_SMC_ASYNC_NOTIF_VALUE_VALID BIT(0)
381#define OPTEE_SMC_ASYNC_NOTIF_VALUE_PENDING BIT(1)
382
383/*
384 * Notification that OP-TEE expects a yielding call to do some bottom half
385 * work in a driver.
386 */
387#define OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_BOTTOM_HALF 0
388
389#define OPTEE_SMC_FUNCID_GET_ASYNC_NOTIF_VALUE 17
390#define OPTEE_SMC_GET_ASYNC_NOTIF_VALUE \
391 OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_ASYNC_NOTIF_VALUE)
392
322/*393/*
323 * Resume from RPC (for example after processing a foreign interrupt)394 * Resume from RPC (for example after processing a foreign interrupt)
324 *395 *
drivers/tee/optee/rpc.c+7−64
@@ -12,23 +12,6 @@ · 11 unmodified lines
12#include "optee_private.h"12#include "optee_private.h"
13#include "optee_rpc_cmd.h"13#include "optee_rpc_cmd.h"
14 14
15struct wq_entry {
16 struct list_head link;
17 struct completion c;
18 u32 key;
19};
20
21void optee_wait_queue_init(struct optee_wait_queue *priv)
22{
23 mutex_init(&priv->mu);
24 INIT_LIST_HEAD(&priv->db);
25}
26
27void optee_wait_queue_exit(struct optee_wait_queue *priv)
28{
29 mutex_destroy(&priv->mu);
30}
31
32static void handle_rpc_func_cmd_get_time(struct optee_msg_arg *arg)15static void handle_rpc_func_cmd_get_time(struct optee_msg_arg *arg)
33{16{
34 struct timespec64 ts;17 struct timespec64 ts;
@@ -144,48 +127,6 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx, · 109 unmodified lines
144}127}
145#endif128#endif
146 129
147static struct wq_entry *wq_entry_get(struct optee_wait_queue *wq, u32 key)
148{
149 struct wq_entry *w;
150
151 mutex_lock(&wq->mu);
152
153 list_for_each_entry(w, &wq->db, link)
154 if (w->key == key)
155 goto out;
156
157 w = kmalloc(sizeof(*w), GFP_KERNEL);
158 if (w) {
159 init_completion(&w->c);
160 w->key = key;
161 list_add_tail(&w->link, &wq->db);
162 }
163out:
164 mutex_unlock(&wq->mu);
165 return w;
166}
167
168static void wq_sleep(struct optee_wait_queue *wq, u32 key)
169{
170 struct wq_entry *w = wq_entry_get(wq, key);
171
172 if (w) {
173 wait_for_completion(&w->c);
174 mutex_lock(&wq->mu);
175 list_del(&w->link);
176 mutex_unlock(&wq->mu);
177 kfree(w);
178 }
179}
180
181static void wq_wakeup(struct optee_wait_queue *wq, u32 key)
182{
183 struct wq_entry *w = wq_entry_get(wq, key);
184
185 if (w)
186 complete(&w->c);
187}
188
189static void handle_rpc_func_cmd_wq(struct optee *optee,130static void handle_rpc_func_cmd_wq(struct optee *optee,
190 struct optee_msg_arg *arg)131 struct optee_msg_arg *arg)
191{132{
@@ -197,11 +138,13 @@ static void handle_rpc_func_cmd_wq(struct optee *optee, · 5 unmodified lines
197 goto bad;138 goto bad;
198 139
199 switch (arg->params[0].u.value.a) {140 switch (arg->params[0].u.value.a) {
200 case OPTEE_RPC_WAIT_QUEUE_SLEEP:141 case OPTEE_RPC_NOTIFICATION_WAIT:
201 wq_sleep(&optee->wait_queue, arg->params[0].u.value.b);142 if (optee_notif_wait(optee, arg->params[0].u.value.b))
143 goto bad;
202 break;144 break;
203 case OPTEE_RPC_WAIT_QUEUE_WAKEUP:145 case OPTEE_RPC_NOTIFICATION_SEND:
204 wq_wakeup(&optee->wait_queue, arg->params[0].u.value.b);146 if (optee_notif_send(optee, arg->params[0].u.value.b))
147 goto bad;
205 break;148 break;
206 default:149 default:
207 goto bad;150 goto bad;
@@ -319,7 +262,7 @@ void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee, · 111 unmodified lines
319 case OPTEE_RPC_CMD_GET_TIME:262 case OPTEE_RPC_CMD_GET_TIME:
320 handle_rpc_func_cmd_get_time(arg);263 handle_rpc_func_cmd_get_time(arg);
321 break;264 break;
322 case OPTEE_RPC_CMD_WAIT_QUEUE:265 case OPTEE_RPC_CMD_NOTIFICATION:
323 handle_rpc_func_cmd_wq(optee, arg);266 handle_rpc_func_cmd_wq(optee, arg);
324 break;267 break;
325 case OPTEE_RPC_CMD_SUSPEND:268 case OPTEE_RPC_CMD_SUSPEND:
drivers/tee/optee/smc_abi.c+206−31
@@ -8,13 +8,16 @@ · 7 unmodified lines
8 8
9#include <linux/arm-smccc.h>9#include <linux/arm-smccc.h>
10#include <linux/errno.h>10#include <linux/errno.h>
11#include <linux/interrupt.h>
11#include <linux/io.h>12#include <linux/io.h>
12#include <linux/sched.h>13#include <linux/irqdomain.h>
13#include <linux/mm.h>14#include <linux/mm.h>
14#include <linux/module.h>15#include <linux/module.h>
15#include <linux/of.h>16#include <linux/of.h>
17#include <linux/of_irq.h>
16#include <linux/of_platform.h>18#include <linux/of_platform.h>
17#include <linux/platform_device.h>19#include <linux/platform_device.h>
20#include <linux/sched.h>
18#include <linux/slab.h>21#include <linux/slab.h>
19#include <linux/string.h>22#include <linux/string.h>
20#include <linux/tee_drv.h>23#include <linux/tee_drv.h>
@@ -34,7 +37,8 @@ · 13 unmodified lines
34 * 2. Low level support functions to register shared memory in secure world37 * 2. Low level support functions to register shared memory in secure world
35 * 3. Dynamic shared memory pool based on alloc_pages()38 * 3. Dynamic shared memory pool based on alloc_pages()
36 * 4. Do a normal scheduled call into secure world39 * 4. Do a normal scheduled call into secure world
37 * 5. Driver initialization.40 * 5. Asynchronous notification
41 * 6. Driver initialization.
38 */42 */
39 43
40#define OPTEE_SHM_NUM_PRIV_PAGES CONFIG_OPTEE_SHM_NUM_PRIV_PAGES44#define OPTEE_SHM_NUM_PRIV_PAGES CONFIG_OPTEE_SHM_NUM_PRIV_PAGES
@@ -875,10 +879,137 @@ static int optee_smc_do_call_with_arg(struct tee_context *ctx, · 834 unmodified lines
875 return rc;879 return rc;
876}880}
877 881
882static int simple_call_with_arg(struct tee_context *ctx, u32 cmd)
883{
884 struct optee_msg_arg *msg_arg;
885 struct tee_shm *shm;
886
887 shm = optee_get_msg_arg(ctx, 0, &msg_arg);
888 if (IS_ERR(shm))
889 return PTR_ERR(shm);
890
891 msg_arg->cmd = cmd;
892 optee_smc_do_call_with_arg(ctx, shm);
893
894 tee_shm_free(shm);
895 return 0;
896}
897
898static int optee_smc_do_bottom_half(struct tee_context *ctx)
899{
900 return simple_call_with_arg(ctx, OPTEE_MSG_CMD_DO_BOTTOM_HALF);
901}
902
903static int optee_smc_stop_async_notif(struct tee_context *ctx)
904{
905 return simple_call_with_arg(ctx, OPTEE_MSG_CMD_STOP_ASYNC_NOTIF);
906}
907
878/*908/*
879 * 5. Driver initialization909 * 5. Asynchronous notification
910 */
911
912static u32 get_async_notif_value(optee_invoke_fn *invoke_fn, bool *value_valid,
913 bool *value_pending)
914{
915 struct arm_smccc_res res;
916
917 invoke_fn(OPTEE_SMC_GET_ASYNC_NOTIF_VALUE, 0, 0, 0, 0, 0, 0, 0, &res);
918
919 if (res.a0)
920 return 0;
921 *value_valid = (res.a2 & OPTEE_SMC_ASYNC_NOTIF_VALUE_VALID);
922 *value_pending = (res.a2 & OPTEE_SMC_ASYNC_NOTIF_VALUE_PENDING);
923 return res.a1;
924}
925
926static irqreturn_t notif_irq_handler(int irq, void *dev_id)
927{
928 struct optee *optee = dev_id;
929 bool do_bottom_half = false;
930 bool value_valid;
931 bool value_pending;
932 u32 value;
933
934 do {
935 value = get_async_notif_value(optee->smc.invoke_fn,
936 &value_valid, &value_pending);
937 if (!value_valid)
938 break;
939
940 if (value == OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_BOTTOM_HALF)
941 do_bottom_half = true;
942 else
943 optee_notif_send(optee, value);
944 } while (value_pending);
945
946 if (do_bottom_half)
947 return IRQ_WAKE_THREAD;
948 return IRQ_HANDLED;
949}
950
951static irqreturn_t notif_irq_thread_fn(int irq, void *dev_id)
952{
953 struct optee *optee = dev_id;
954
955 optee_smc_do_bottom_half(optee->notif.ctx);
956
957 return IRQ_HANDLED;
958}
959
960static int optee_smc_notif_init_irq(struct optee *optee, u_int irq)
961{
962 struct tee_context *ctx;
963 int rc;
964
965 ctx = teedev_open(optee->teedev);
966 if (IS_ERR(ctx))
967 return PTR_ERR(ctx);
968
969 optee->notif.ctx = ctx;
970 rc = request_threaded_irq(irq, notif_irq_handler,
971 notif_irq_thread_fn,
972 0, "optee_notification", optee);
973 if (rc)
974 goto err_close_ctx;
975
976 optee->smc.notif_irq = irq;
977
978 return 0;
979
980err_close_ctx:
981 teedev_close_context(optee->notif.ctx);
982 optee->notif.ctx = NULL;
983
984 return rc;
985}
986
987static void optee_smc_notif_uninit_irq(struct optee *optee)
988{
989 if (optee->notif.ctx) {
990 optee_smc_stop_async_notif(optee->notif.ctx);
991 if (optee->smc.notif_irq) {
992 free_irq(optee->smc.notif_irq, optee);
993 irq_dispose_mapping(optee->smc.notif_irq);
994 }
995
996 /*
997 * The thread normally working with optee->notif.ctx was
998 * stopped with free_irq() above.
999 *
1000 * Note we're not using teedev_close_context() or
1001 * tee_client_close_context() since we have already called
1002 * tee_device_put() while initializing to avoid a circular
1003 * reference counting.
1004 */
1005 teedev_close_context(optee->notif.ctx);
1006 }
1007}
1008
1009/*
1010 * 6. Driver initialization
880 *1011 *
881 * During driver inititialization is secure world probed to find out which1012 * During driver initialization is secure world probed to find out which
882 * features it supports so the driver can be initialized with a matching1013 * features it supports so the driver can be initialized with a matching
883 * configuration. This involves for instance support for dynamic shared1014 * configuration. This involves for instance support for dynamic shared
884 * memory instead of a static memory carvout.1015 * memory instead of a static memory carvout.
@@ -950,6 +1081,17 @@ static const struct optee_ops optee_ops = { · 65 unmodified lines
950 .from_msg_param = optee_from_msg_param,1081 .from_msg_param = optee_from_msg_param,
951};1082};
952 1083
1084static int enable_async_notif(optee_invoke_fn *invoke_fn)
1085{
1086 struct arm_smccc_res res;
1087
1088 invoke_fn(OPTEE_SMC_ENABLE_ASYNC_NOTIF, 0, 0, 0, 0, 0, 0, 0, &res);
1089
1090 if (res.a0)
1091 return -EINVAL;
1092 return 0;
1093}
1094
953static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn)1095static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn)
954{1096{
955 struct arm_smccc_res res;1097 struct arm_smccc_res res;
@@ -999,7 +1141,7 @@ static bool optee_msg_api_revision_is_compatible(optee_invoke_fn *invoke_fn) · 43 unmodified lines
999}1141}
1000 1142
1001static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,1143static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
1002 u32 *sec_caps)1144 u32 *sec_caps, u32 *max_notif_value)
1003{1145{
1004 union {1146 union {
1005 struct arm_smccc_res smccc;1147 struct arm_smccc_res smccc;
@@ -1022,6 +1164,11 @@ static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn, · 16 unmodified lines
1022 return false;1164 return false;
1023 1165
1024 *sec_caps = res.result.capabilities;1166 *sec_caps = res.result.capabilities;
1167 if (*sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF)
1168 *max_notif_value = res.result.max_notif_value;
1169 else
1170 *max_notif_value = OPTEE_DEFAULT_MAX_NOTIF_VALUE;
1171
1025 return true;1172 return true;
1026}1173}
1027 1174
@@ -1186,6 +1333,8 @@ static int optee_smc_remove(struct platform_device *pdev) · 158 unmodified lines
1186 */1333 */
1187 optee_disable_shm_cache(optee);1334 optee_disable_shm_cache(optee);
1188 1335
1336 optee_smc_notif_uninit_irq(optee);
1337
1189 optee_remove_common(optee);1338 optee_remove_common(optee);
1190 1339
1191 if (optee->smc.memremaped_shm)1340 if (optee->smc.memremaped_shm)
@@ -1215,6 +1364,7 @@ static int optee_probe(struct platform_device *pdev) · 23 unmodified lines
1215 struct optee *optee = NULL;1364 struct optee *optee = NULL;
1216 void *memremaped_shm = NULL;1365 void *memremaped_shm = NULL;
1217 struct tee_device *teedev;1366 struct tee_device *teedev;
1367 u32 max_notif_value;
1218 u32 sec_caps;1368 u32 sec_caps;
1219 int rc;1369 int rc;
1220 1370
@@ -1234,7 +1384,8 @@ static int optee_probe(struct platform_device *pdev) · 13 unmodified lines
1234 return -EINVAL;1384 return -EINVAL;
1235 }1385 }
1236 1386
1237 if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps)) {1387 if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps,
1388 &max_notif_value)) {
1238 pr_warn("capabilities mismatch\n");1389 pr_warn("capabilities mismatch\n");
1239 return -EINVAL;1390 return -EINVAL;
1240 }1391 }
@@ -1257,7 +1408,7 @@ static int optee_probe(struct platform_device *pdev) · 16 unmodified lines
1257 optee = kzalloc(sizeof(*optee), GFP_KERNEL);1408 optee = kzalloc(sizeof(*optee), GFP_KERNEL);
1258 if (!optee) {1409 if (!optee) {
1259 rc = -ENOMEM;1410 rc = -ENOMEM;
1260 goto err;1411 goto err_free_pool;
1261 }1412 }
1262 1413
1263 optee->ops = &optee_ops;1414 optee->ops = &optee_ops;
@@ -1267,32 +1418,55 @@ static int optee_probe(struct platform_device *pdev) · 3 unmodified lines
1267 teedev = tee_device_alloc(&optee_clnt_desc, NULL, pool, optee);1418 teedev = tee_device_alloc(&optee_clnt_desc, NULL, pool, optee);
1268 if (IS_ERR(teedev)) {1419 if (IS_ERR(teedev)) {
1269 rc = PTR_ERR(teedev);1420 rc = PTR_ERR(teedev);
1270 goto err;1421 goto err_free_optee;
1271 }1422 }
1272 optee->teedev = teedev;1423 optee->teedev = teedev;
1273 1424
1274 teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee);1425 teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee);
1275 if (IS_ERR(teedev)) {1426 if (IS_ERR(teedev)) {
1276 rc = PTR_ERR(teedev);1427 rc = PTR_ERR(teedev);
1277 goto err;1428 goto err_unreg_teedev;
1278 }1429 }
1279 optee->supp_teedev = teedev;1430 optee->supp_teedev = teedev;
1280 1431
1281 rc = tee_device_register(optee->teedev);1432 rc = tee_device_register(optee->teedev);
1282 if (rc)1433 if (rc)
1283 goto err;1434 goto err_unreg_supp_teedev;
1284 1435
1285 rc = tee_device_register(optee->supp_teedev);1436 rc = tee_device_register(optee->supp_teedev);
1286 if (rc)1437 if (rc)
1287 goto err;1438 goto err_unreg_supp_teedev;
1288 1439
1289 mutex_init(&optee->call_queue.mutex);1440 mutex_init(&optee->call_queue.mutex);
1290 INIT_LIST_HEAD(&optee->call_queue.waiters);1441 INIT_LIST_HEAD(&optee->call_queue.waiters);
1291 optee_wait_queue_init(&optee->wait_queue);
1292 optee_supp_init(&optee->supp);1442 optee_supp_init(&optee->supp);
1293 optee->smc.memremaped_shm = memremaped_shm;1443 optee->smc.memremaped_shm = memremaped_shm;
1294 optee->pool = pool;1444 optee->pool = pool;
1295 1445
1446 platform_set_drvdata(pdev, optee);
1447 rc = optee_notif_init(optee, max_notif_value);
1448 if (rc)
1449 goto err_supp_uninit;
1450
1451 if (sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF) {
1452 unsigned int irq;
1453
1454 rc = platform_get_irq(pdev, 0);
1455 if (rc < 0) {
1456 pr_err("platform_get_irq: ret %d\n", rc);
1457 goto err_notif_uninit;
1458 }
1459 irq = rc;
1460
1461 rc = optee_smc_notif_init_irq(optee, irq);
1462 if (rc) {
1463 irq_dispose_mapping(irq);
1464 goto err_notif_uninit;
1465 }
1466 enable_async_notif(optee->smc.invoke_fn);
1467 pr_info("Asynchronous notifications enabled\n");
1468 }
1469
1296 /*1470 /*
1297 * Ensure that there are no pre-existing shm objects before enabling1471 * Ensure that there are no pre-existing shm objects before enabling
1298 * the shm cache so that there's no chance of receiving an invalid1472 * the shm cache so that there's no chance of receiving an invalid
@@ -1307,29 +1481,30 @@ static int optee_probe(struct platform_device *pdev) · 8 unmodified lines
1307 if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)1481 if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
1308 pr_info("dynamic shared memory is enabled\n");1482 pr_info("dynamic shared memory is enabled\n");
1309 1483
1310 platform_set_drvdata(pdev, optee);
1311
1312 rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);1484 rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
1313 if (rc) {1485 if (rc)
1314 optee_smc_remove(pdev);1486 goto err_disable_shm_cache;
1315 return rc;
1316 }
1317 1487
1318 pr_info("initialized driver\n");1488 pr_info("initialized driver\n");
1319 return 0;1489 return 0;
1320err:1490
1321 if (optee) {1491err_disable_shm_cache:
1322 /*1492 optee_disable_shm_cache(optee);
1323 * tee_device_unregister() is safe to call even if the1493 optee_smc_notif_uninit_irq(optee);
1324 * devices hasn't been registered with1494 optee_unregister_devices();
1325 * tee_device_register() yet.1495err_notif_uninit:
1326 */1496 optee_notif_uninit(optee);
1327 tee_device_unregister(optee->supp_teedev);1497err_supp_uninit:
1328 tee_device_unregister(optee->teedev);1498 optee_supp_uninit(&optee->supp);
1329 kfree(optee);1499 mutex_destroy(&optee->call_queue.mutex);
1330 }1500err_unreg_supp_teedev:
1331 if (pool)1501 tee_device_unregister(optee->supp_teedev);
1332 tee_shm_pool_free(pool);1502err_unreg_teedev:
1503 tee_device_unregister(optee->teedev);
1504err_free_optee:
1505 kfree(optee);
1506err_free_pool:
1507 tee_shm_pool_free(pool);
1333 if (memremaped_shm)1508 if (memremaped_shm)
1334 memunmap(memremaped_shm);1509 memunmap(memremaped_shm);
1335 return rc;1510 return rc;
drivers/tee/tee_core.c+7−3
@@ -43,7 +43,7 @@ static DEFINE_SPINLOCK(driver_lock); · 42 unmodified lines
43static struct class *tee_class;43static struct class *tee_class;
44static dev_t tee_devt;44static dev_t tee_devt;
45 45
46static struct tee_context *teedev_open(struct tee_device *teedev)46struct tee_context *teedev_open(struct tee_device *teedev)
47{47{
48 int rc;48 int rc;
49 struct tee_context *ctx;49 struct tee_context *ctx;
@@ -70,6 +70,7 @@ err: · 20 unmodified lines
70 return ERR_PTR(rc);70 return ERR_PTR(rc);
71 71
72}72}
73EXPORT_SYMBOL_GPL(teedev_open);
73 74
74void teedev_ctx_get(struct tee_context *ctx)75void teedev_ctx_get(struct tee_context *ctx)
75{76{
@@ -96,11 +97,14 @@ void teedev_ctx_put(struct tee_context *ctx) · 20 unmodified lines
96 kref_put(&ctx->refcount, teedev_ctx_release);97 kref_put(&ctx->refcount, teedev_ctx_release);
97}98}
98 99
99static void teedev_close_context(struct tee_context *ctx)100void teedev_close_context(struct tee_context *ctx)
100{101{
101 tee_device_put(ctx->teedev);102 struct tee_device *teedev = ctx->teedev;
103
102 teedev_ctx_put(ctx);104 teedev_ctx_put(ctx);
105 tee_device_put(teedev);
103}106}
107EXPORT_SYMBOL_GPL(teedev_close_context);
104 108
105static int tee_open(struct inode *inode, struct file *filp)109static int tee_open(struct inode *inode, struct file *filp)
106{110{
include/linux/tee_drv.h+14−0
@@ -587,4 +587,18 @@ struct tee_client_driver { · 586 unmodified lines
587#define to_tee_client_driver(d) \587#define to_tee_client_driver(d) \
588 container_of(d, struct tee_client_driver, driver)588 container_of(d, struct tee_client_driver, driver)
589 589
590/**
591 * teedev_open() - Open a struct tee_device
592 * @teedev: Device to open
593 *
594 * @return a pointer to struct tee_context on success or an ERR_PTR on failure.
595 */
596struct tee_context *teedev_open(struct tee_device *teedev);
597
598/**
599 * teedev_close_context() - closes a struct tee_context
600 * @ctx: The struct tee_context to close
601 */
602void teedev_close_context(struct tee_context *ctx);
603
590#endif /*__TEE_DRV_H*/604#endif /*__TEE_DRV_H*/

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t