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
2424 compatible:
2525 const: linaro,optee-tz
2626
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+
2733 method:
2834 enum: [smc, hvc]
2935 description: |
@@ -42,10 +48,12 @@ additionalProperties: false · 12 unmodified lines
4248
4349 examples:
4450 - |
51+ #include <dt-bindings/interrupt-controller/arm-gic.h>
4552 firmware {
4653 optee {
4754 compatible = "linaro,optee-tz";
4855 method = "smc";
56+ interrupts = <GIC_SPI 187 IRQ_TYPE_EDGE_RISING>;
4957 };
5058 };
5159
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
184184 application to retrieve a list of Trusted Applications which can be registered
185185 as devices on the TEE bus.
186186
187+OP-TEE notifications
188+--------------------
189+
190+There are two kinds of notifications that secure world can use to make
191+normal world aware of some event.
192+
193+1. Synchronous notifications delivered with ``OPTEE_RPC_CMD_NOTIFICATION``
194+ using the ``OPTEE_RPC_NOTIFICATION_SEND`` parameter.
195+2. 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+
199+Synchronous notifications are limited by depending on RPC for delivery,
200+this 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
202+world interrupt handlers.
203+
204+An asynchronous notification is delivered via a non-secure edge-triggered
205+interrupt to an interrupt handler registered in the OP-TEE driver. The
206+actual notification value are retrieved with the fast call
207+``OPTEE_SMC_GET_ASYNC_NOTIF_VALUE``. Note that one interrupt can represent
208+multiple notifications.
209+
210+One notification value ``OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_BOTTOM_HALF`` has a
211+special meaning. When this value is received it means that normal world is
212+supposed to make a yielding call ``OPTEE_MSG_CMD_DO_BOTTOM_HALF``. This
213+call is done from the thread assisting the interrupt handler. This is a
214+building block for OP-TEE OS in secure world to implement the top half and
215+bottom half style of device drivers.
216+
187217 AMD-TEE driver
188218 ==============
189219
drivers/tee/optee/Makefile+1−0
@@ -2,6 +2,7 @@ · 1 unmodified line
22 obj-$(CONFIG_OPTEE) += optee.o
33 optee-objs += core.o
44 optee-objs += call.o
5+optee-objs += notif.o
56 optee-objs += rpc.o
67 optee-objs += supp.o
78 optee-objs += device.o
drivers/tee/optee/core.c+1−1
@@ -159,6 +159,7 @@ void optee_remove_common(struct optee *optee) · 158 unmodified lines
159159 /* Unregister OP-TEE specific client devices on TEE bus */
160160 optee_unregister_devices();
161161
162+ optee_notif_uninit(optee);
162163 /*
163164 * The two devices have to be unregistered before we can free the
164165 * other resources.
@@ -167,7 +168,6 @@ void optee_remove_common(struct optee *optee) · 2 unmodified lines
167168 tee_device_unregister(optee->teedev);
168169
169170 tee_shm_pool_free(optee->pool);
170− optee_wait_queue_exit(&optee->wait_queue);
171171 optee_supp_uninit(&optee->supp);
172172 mutex_destroy(&optee->call_queue.mutex);
173173 }
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
856856 mutex_init(&optee->ffa.mutex);
857857 mutex_init(&optee->call_queue.mutex);
858858 INIT_LIST_HEAD(&optee->call_queue.waiters);
859− optee_wait_queue_init(&optee->wait_queue);
860859 optee_supp_init(&optee->supp);
861860 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+ }
862866
863867 rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
864868 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+
15+struct notif_entry {
16+ struct list_head link;
17+ struct completion c;
18+ u_int key;
19+};
20+
21+static 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+
32+int 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);
77+out:
78+ spin_unlock_irqrestore(&optee->notif.lock, flags);
79+
80+ kfree(entry);
81+
82+ return rc;
83+}
84+
85+int 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);
103+out:
104+ spin_unlock_irqrestore(&optee->notif.lock, flags);
105+
106+ return 0;
107+}
108+
109+int 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+
122+void 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
318318 * [in] param[0].u.rmem.shm_ref holds shared memory reference
319319 * [in] param[0].u.rmem.offs 0
320320 * [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.
321328 */
322329 #define OPTEE_MSG_CMD_OPEN_SESSION 0
323330 #define OPTEE_MSG_CMD_INVOKE_COMMAND 1
@@ -325,6 +332,8 @@ struct optee_msg_arg { · 1 unmodified line
325332 #define OPTEE_MSG_CMD_CANCEL 3
326333 #define OPTEE_MSG_CMD_REGISTER_SHM 4
327334 #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
328337 #define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004
329338
330339 #endif /* _OPTEE_MSG_H */
drivers/tee/optee/optee_private.h+20−8
@@ -28,6 +28,13 @@ · 27 unmodified lines
2828
2929 #define TEEC_ORIGIN_COMMS 0x00000002
3030
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+
3138 typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
3239 unsigned long, unsigned long, unsigned long,
3340 unsigned long, unsigned long,
@@ -44,10 +51,13 @@ struct optee_call_queue { · 10 unmodified lines
4451 struct list_head waiters;
4552 };
4653
47−struct optee_wait_queue {
48− /* Serializes access to this struct */
49− struct mutex mu;
54+struct optee_notif {
55+ u_int max_key;
56+ struct tee_context *ctx;
57+ /* Serializes access to the elements below in this struct */
58+ spinlock_t lock;
5059 struct list_head db;
60+ u_long *bitmap;
5161 };
5262
5363 /**
@@ -79,6 +89,7 @@ struct optee_smc { · 25 unmodified lines
7989 optee_invoke_fn *invoke_fn;
8090 void *memremaped_shm;
8191 u32 sec_caps;
92+ unsigned int notif_irq;
8293 };
8394
8495 /**
@@ -129,8 +140,7 @@ struct optee_ops { · 44 unmodified lines
129140 * @smc: specific to SMC ABI
130141 * @ffa: specific to FF-A ABI
131142 * @call_queue: queue of threads waiting to call @invoke_fn
132− * @wait_queue: queue of threads from secure world waiting for a
133− * secure world sync object
143+ * @notif: notification synchronization struct
134144 * @supp: supplicant synchronization struct for RPC to supplicant
135145 * @pool: shared memory pool
136146 * @rpc_arg_count: If > 0 number of RPC parameters to make room for
@@ -147,7 +157,7 @@ struct optee { · 10 unmodified lines
147157 struct optee_ffa ffa;
148158 };
149159 struct optee_call_queue call_queue;
150− struct optee_wait_queue wait_queue;
160+ struct optee_notif notif;
151161 struct optee_supp supp;
152162 struct tee_shm_pool *pool;
153163 unsigned int rpc_arg_count;
@@ -185,8 +195,10 @@ struct optee_call_ctx { · 31 unmodified lines
185195 size_t num_entries;
186196 };
187197
188−void optee_wait_queue_init(struct optee_wait_queue *wq);
189−void optee_wait_queue_exit(struct optee_wait_queue *wq);
198+int optee_notif_init(struct optee *optee, u_int max_key);
199+void optee_notif_uninit(struct optee *optee);
200+int optee_notif_wait(struct optee *optee, u_int key);
201+int optee_notif_send(struct optee *optee, u_int key);
190202
191203 u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
192204 struct tee_param *param);
drivers/tee/optee/optee_rpc_cmd.h+17−14
@@ -28,24 +28,27 @@ · 27 unmodified lines
2828 #define OPTEE_RPC_CMD_GET_TIME 3
2929
3030 /*
31− * Wait queue primitive, helper for secure world to implement a wait queue.
31+ * Notification from/to secure world.
3232 *
33− * If secure world needs to wait for a secure world mutex it issues a sleep
34− * request instead of spinning in secure world. Conversely is a wakeup
35− * request issued when a secure world mutex with a thread waiting thread is
36− * unlocked.
33+ * If secure world needs to wait for something, for instance a mutex, it
34+ * does a notification wait request instead of spinning in secure world.
35+ * Conversely can a synchronous notification can be sent when a secure
36+ * world mutex with a thread waiting thread is unlocked.
3737 *
38− * Waiting on a key
39− * [in] value[0].a OPTEE_RPC_WAIT_QUEUE_SLEEP
40− * [in] value[0].b Wait key
38+ * This interface can also be used to wait for a asynchronous notification
39+ * which instead is sent via a non-secure interrupt.
4140 *
42− * Waking up a key
43− * [in] value[0].a OPTEE_RPC_WAIT_QUEUE_WAKEUP
44− * [in] value[0].b Wakeup key
41+ * Waiting on notification
42+ * [in] value[0].a OPTEE_RPC_NOTIFICATION_WAIT
43+ * [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
4548 */
46−#define OPTEE_RPC_CMD_WAIT_QUEUE 4
47−#define OPTEE_RPC_WAIT_QUEUE_SLEEP 0
48−#define OPTEE_RPC_WAIT_QUEUE_WAKEUP 1
49+#define OPTEE_RPC_CMD_NOTIFICATION 4
50+#define OPTEE_RPC_NOTIFICATION_WAIT 0
51+#define OPTEE_RPC_NOTIFICATION_SEND 1
4952
5053 /*
5154 * 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
107107 /*
108108 * Call with struct optee_msg_arg as argument
109109 *
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+ *
110116 * Call register usage:
111117 * a0 SMC Function ID, OPTEE_SMC*CALL_WITH_ARG
112118 * 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
195201 * Normal return register usage:
196202 * a0 OPTEE_SMC_RETURN_OK
197203 * a1 bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
198− * a2-7 Preserved
204+ * a2 The maximum secure world notification number
205+ * a3-7 Preserved
199206 *
200207 * Error return register usage:
201208 * 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
218225 #define OPTEE_SMC_SEC_CAP_VIRTUALIZATION BIT(3)
219226 /* Secure world supports Shared Memory with a NULL reference */
220227 #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)
221230
222231 #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9
223232 #define OPTEE_SMC_EXCHANGE_CAPABILITIES \
@@ -226,8 +235,8 @@ struct optee_smc_get_shm_config_result { · 2 unmodified lines
226235 struct optee_smc_exchange_capabilities_result {
227236 unsigned long status;
228237 unsigned long capabilities;
238+ unsigned long max_notif_value;
229239 unsigned long reserved0;
230− unsigned long reserved1;
231240 };
232241
233242 /*
@@ -319,6 +328,68 @@ struct optee_smc_disable_shm_cache_result { · 85 unmodified lines
319328 #define OPTEE_SMC_GET_THREAD_COUNT \
320329 OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_THREAD_COUNT)
321330
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+
322393 /*
323394 * Resume from RPC (for example after processing a foreign interrupt)
324395 *
drivers/tee/optee/rpc.c+7−64
@@ -12,23 +12,6 @@ · 11 unmodified lines
1212 #include "optee_private.h"
1313 #include "optee_rpc_cmd.h"
1414
15−struct wq_entry {
16− struct list_head link;
17− struct completion c;
18− u32 key;
19−};
20−
21−void optee_wait_queue_init(struct optee_wait_queue *priv)
22−{
23− mutex_init(&priv->mu);
24− INIT_LIST_HEAD(&priv->db);
25−}
26−
27−void optee_wait_queue_exit(struct optee_wait_queue *priv)
28−{
29− mutex_destroy(&priv->mu);
30−}
31−
3215 static void handle_rpc_func_cmd_get_time(struct optee_msg_arg *arg)
3316 {
3417 struct timespec64 ts;
@@ -144,48 +127,6 @@ static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx, · 109 unmodified lines
144127 }
145128 #endif
146129
147−static 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− }
163−out:
164− mutex_unlock(&wq->mu);
165− return w;
166−}
167−
168−static 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−
181−static 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−
189130 static void handle_rpc_func_cmd_wq(struct optee *optee,
190131 struct optee_msg_arg *arg)
191132 {
@@ -197,11 +138,13 @@ static void handle_rpc_func_cmd_wq(struct optee *optee, · 5 unmodified lines
197138 goto bad;
198139
199140 switch (arg->params[0].u.value.a) {
200− case OPTEE_RPC_WAIT_QUEUE_SLEEP:
201− wq_sleep(&optee->wait_queue, arg->params[0].u.value.b);
141+ case OPTEE_RPC_NOTIFICATION_WAIT:
142+ if (optee_notif_wait(optee, arg->params[0].u.value.b))
143+ goto bad;
202144 break;
203− case OPTEE_RPC_WAIT_QUEUE_WAKEUP:
204− wq_wakeup(&optee->wait_queue, arg->params[0].u.value.b);
145+ case OPTEE_RPC_NOTIFICATION_SEND:
146+ if (optee_notif_send(optee, arg->params[0].u.value.b))
147+ goto bad;
205148 break;
206149 default:
207150 goto bad;
@@ -319,7 +262,7 @@ void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee, · 111 unmodified lines
319262 case OPTEE_RPC_CMD_GET_TIME:
320263 handle_rpc_func_cmd_get_time(arg);
321264 break;
322− case OPTEE_RPC_CMD_WAIT_QUEUE:
265+ case OPTEE_RPC_CMD_NOTIFICATION:
323266 handle_rpc_func_cmd_wq(optee, arg);
324267 break;
325268 case OPTEE_RPC_CMD_SUSPEND:
drivers/tee/optee/smc_abi.c+206−31
@@ -8,13 +8,16 @@ · 7 unmodified lines
88
99 #include <linux/arm-smccc.h>
1010 #include <linux/errno.h>
11+#include <linux/interrupt.h>
1112 #include <linux/io.h>
12−#include <linux/sched.h>
13+#include <linux/irqdomain.h>
1314 #include <linux/mm.h>
1415 #include <linux/module.h>
1516 #include <linux/of.h>
17+#include <linux/of_irq.h>
1618 #include <linux/of_platform.h>
1719 #include <linux/platform_device.h>
20+#include <linux/sched.h>
1821 #include <linux/slab.h>
1922 #include <linux/string.h>
2023 #include <linux/tee_drv.h>
@@ -34,7 +37,8 @@ · 13 unmodified lines
3437 * 2. Low level support functions to register shared memory in secure world
3538 * 3. Dynamic shared memory pool based on alloc_pages()
3639 * 4. Do a normal scheduled call into secure world
37− * 5. Driver initialization.
40+ * 5. Asynchronous notification
41+ * 6. Driver initialization.
3842 */
3943
4044 #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
875879 return rc;
876880 }
877881
882+static 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+
898+static 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+
903+static 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+
878908 /*
879− * 5. Driver initialization
909+ * 5. Asynchronous notification
910+ */
911+
912+static 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+
926+static 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+
951+static 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+
960+static 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+
980+err_close_ctx:
981+ teedev_close_context(optee->notif.ctx);
982+ optee->notif.ctx = NULL;
983+
984+ return rc;
985+}
986+
987+static 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
8801011 *
881− * During driver inititialization is secure world probed to find out which
1012+ * During driver initialization is secure world probed to find out which
8821013 * features it supports so the driver can be initialized with a matching
8831014 * configuration. This involves for instance support for dynamic shared
8841015 * memory instead of a static memory carvout.
@@ -950,6 +1081,17 @@ static const struct optee_ops optee_ops = { · 65 unmodified lines
9501081 .from_msg_param = optee_from_msg_param,
9511082 };
9521083
1084+static 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+
9531095 static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn)
9541096 {
9551097 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
9991141 }
10001142
10011143 static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
1002− u32 *sec_caps)
1144+ u32 *sec_caps, u32 *max_notif_value)
10031145 {
10041146 union {
10051147 struct arm_smccc_res smccc;
@@ -1022,6 +1164,11 @@ static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn, · 16 unmodified lines
10221164 return false;
10231165
10241166 *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+
10251172 return true;
10261173 }
10271174
@@ -1186,6 +1333,8 @@ static int optee_smc_remove(struct platform_device *pdev) · 158 unmodified lines
11861333 */
11871334 optee_disable_shm_cache(optee);
11881335
1336+ optee_smc_notif_uninit_irq(optee);
1337+
11891338 optee_remove_common(optee);
11901339
11911340 if (optee->smc.memremaped_shm)
@@ -1215,6 +1364,7 @@ static int optee_probe(struct platform_device *pdev) · 23 unmodified lines
12151364 struct optee *optee = NULL;
12161365 void *memremaped_shm = NULL;
12171366 struct tee_device *teedev;
1367+ u32 max_notif_value;
12181368 u32 sec_caps;
12191369 int rc;
12201370
@@ -1234,7 +1384,8 @@ static int optee_probe(struct platform_device *pdev) · 13 unmodified lines
12341384 return -EINVAL;
12351385 }
12361386
1237− if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps)) {
1387+ if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps,
1388+ &max_notif_value)) {
12381389 pr_warn("capabilities mismatch\n");
12391390 return -EINVAL;
12401391 }
@@ -1257,7 +1408,7 @@ static int optee_probe(struct platform_device *pdev) · 16 unmodified lines
12571408 optee = kzalloc(sizeof(*optee), GFP_KERNEL);
12581409 if (!optee) {
12591410 rc = -ENOMEM;
1260− goto err;
1411+ goto err_free_pool;
12611412 }
12621413
12631414 optee->ops = &optee_ops;
@@ -1267,32 +1418,55 @@ static int optee_probe(struct platform_device *pdev) · 3 unmodified lines
12671418 teedev = tee_device_alloc(&optee_clnt_desc, NULL, pool, optee);
12681419 if (IS_ERR(teedev)) {
12691420 rc = PTR_ERR(teedev);
1270− goto err;
1421+ goto err_free_optee;
12711422 }
12721423 optee->teedev = teedev;
12731424
12741425 teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee);
12751426 if (IS_ERR(teedev)) {
12761427 rc = PTR_ERR(teedev);
1277− goto err;
1428+ goto err_unreg_teedev;
12781429 }
12791430 optee->supp_teedev = teedev;
12801431
12811432 rc = tee_device_register(optee->teedev);
12821433 if (rc)
1283− goto err;
1434+ goto err_unreg_supp_teedev;
12841435
12851436 rc = tee_device_register(optee->supp_teedev);
12861437 if (rc)
1287− goto err;
1438+ goto err_unreg_supp_teedev;
12881439
12891440 mutex_init(&optee->call_queue.mutex);
12901441 INIT_LIST_HEAD(&optee->call_queue.waiters);
1291− optee_wait_queue_init(&optee->wait_queue);
12921442 optee_supp_init(&optee->supp);
12931443 optee->smc.memremaped_shm = memremaped_shm;
12941444 optee->pool = pool;
12951445
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+
12961470 /*
12971471 * Ensure that there are no pre-existing shm objects before enabling
12981472 * 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
13071481 if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
13081482 pr_info("dynamic shared memory is enabled\n");
13091483
1310− platform_set_drvdata(pdev, optee);
1311−
13121484 rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
1313− if (rc) {
1314− optee_smc_remove(pdev);
1315− return rc;
1316− }
1485+ if (rc)
1486+ goto err_disable_shm_cache;
13171487
13181488 pr_info("initialized driver\n");
13191489 return 0;
1320−err:
1321− if (optee) {
1322− /*
1323− * tee_device_unregister() is safe to call even if the
1324− * devices hasn't been registered with
1325− * tee_device_register() yet.
1326− */
1327− tee_device_unregister(optee->supp_teedev);
1328− tee_device_unregister(optee->teedev);
1329− kfree(optee);
1330− }
1331− if (pool)
1332− tee_shm_pool_free(pool);
1490+
1491+err_disable_shm_cache:
1492+ optee_disable_shm_cache(optee);
1493+ optee_smc_notif_uninit_irq(optee);
1494+ optee_unregister_devices();
1495+err_notif_uninit:
1496+ optee_notif_uninit(optee);
1497+err_supp_uninit:
1498+ optee_supp_uninit(&optee->supp);
1499+ mutex_destroy(&optee->call_queue.mutex);
1500+err_unreg_supp_teedev:
1501+ tee_device_unregister(optee->supp_teedev);
1502+err_unreg_teedev:
1503+ tee_device_unregister(optee->teedev);
1504+err_free_optee:
1505+ kfree(optee);
1506+err_free_pool:
1507+ tee_shm_pool_free(pool);
13331508 if (memremaped_shm)
13341509 memunmap(memremaped_shm);
13351510 return rc;
drivers/tee/tee_core.c+7−3
@@ -43,7 +43,7 @@ static DEFINE_SPINLOCK(driver_lock); · 42 unmodified lines
4343 static struct class *tee_class;
4444 static dev_t tee_devt;
4545
46−static struct tee_context *teedev_open(struct tee_device *teedev)
46+struct tee_context *teedev_open(struct tee_device *teedev)
4747 {
4848 int rc;
4949 struct tee_context *ctx;
@@ -70,6 +70,7 @@ err: · 20 unmodified lines
7070 return ERR_PTR(rc);
7171
7272 }
73+EXPORT_SYMBOL_GPL(teedev_open);
7374
7475 void teedev_ctx_get(struct tee_context *ctx)
7576 {
@@ -96,11 +97,14 @@ void teedev_ctx_put(struct tee_context *ctx) · 20 unmodified lines
9697 kref_put(&ctx->refcount, teedev_ctx_release);
9798 }
9899
99−static void teedev_close_context(struct tee_context *ctx)
100+void teedev_close_context(struct tee_context *ctx)
100101 {
101− tee_device_put(ctx->teedev);
102+ struct tee_device *teedev = ctx->teedev;
103+
102104 teedev_ctx_put(ctx);
105+ tee_device_put(teedev);
103106 }
107+EXPORT_SYMBOL_GPL(teedev_close_context);
104108
105109 static int tee_open(struct inode *inode, struct file *filp)
106110 {
include/linux/tee_drv.h+14−0
@@ -587,4 +587,18 @@ struct tee_client_driver { · 586 unmodified lines
587587 #define to_tee_client_driver(d) \
588588 container_of(d, struct tee_client_driver, driver)
589589
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+ */
596+struct 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+ */
602+void teedev_close_context(struct tee_context *ctx);
603+
590604 #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