Skip to content

Commit 0d3bece

Browse files
committed
Merge tag 'optee-per-cpu-irq-for-v6.4' of https://git.linaro.org/people/jens.wiklander/linux-tee into soc/drivers
Add OP-TEE per cpu asynchronous notification Adds support for signalling from secure world with per-cpu interrupts in addition to edge-triggered peripheral interrupts. * tag 'optee-per-cpu-irq-for-v6.4' of https://git.linaro.org/people/jens.wiklander/linux-tee: optee: add per cpu asynchronous notification dt-bindings: optee driver interrupt can be a per-cpu interrupt Link: https://lore.kernel.org/r/20230404062727.GA2765560@rayden Signed-off-by: Arnd Bergmann <arnd@arndb.de>
2 parents a5eb346 + b3b4ced commit 0d3bece

3 files changed

Lines changed: 144 additions & 5 deletions

File tree

Documentation/devicetree/bindings/arm/firmware/linaro,optee-tz.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ properties:
2828
maxItems: 1
2929
description: |
3030
This interrupt which is used to signal an event by the secure world
31-
software is expected to be edge-triggered.
31+
software is expected to be either a per-cpu interrupt or an
32+
edge-triggered peripheral interrupt.
3233
3334
method:
3435
enum: [smc, hvc]

drivers/tee/optee/optee_private.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,35 @@ struct optee_supp {
9494
struct completion reqs_c;
9595
};
9696

97+
/*
98+
* struct optee_pcpu - per cpu notif private struct passed to work functions
99+
* @optee optee device reference
100+
*/
101+
struct optee_pcpu {
102+
struct optee *optee;
103+
};
104+
105+
/*
106+
* struct optee_smc - optee smc communication struct
107+
* @invoke_fn handler function to invoke secure monitor
108+
* @memremaped_shm virtual address of memory in shared memory pool
109+
* @sec_caps: secure world capabilities defined by
110+
* OPTEE_SMC_SEC_CAP_* in optee_smc.h
111+
* @notif_irq interrupt used as async notification by OP-TEE or 0
112+
* @optee_pcpu per_cpu optee instance for per cpu work or NULL
113+
* @notif_pcpu_wq workqueue for per cpu asynchronous notification or NULL
114+
* @notif_pcpu_work work for per cpu asynchronous notification
115+
* @notif_cpuhp_state CPU hotplug state assigned for pcpu interrupt management
116+
*/
97117
struct optee_smc {
98118
optee_invoke_fn *invoke_fn;
99119
void *memremaped_shm;
100120
u32 sec_caps;
101121
unsigned int notif_irq;
122+
struct optee_pcpu __percpu *optee_pcpu;
123+
struct workqueue_struct *notif_pcpu_wq;
124+
struct work_struct notif_pcpu_work;
125+
unsigned int notif_cpuhp_state;
102126
};
103127

104128
/**

drivers/tee/optee/smc_abi.c

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@
5252
*/
5353
#define OPTEE_MIN_STATIC_POOL_ALIGN 9 /* 512 bytes aligned */
5454

55+
/* SMC ABI considers at most a single TEE firmware */
56+
static unsigned int pcpu_irq_num;
57+
58+
static int optee_cpuhp_enable_pcpu_irq(unsigned int cpu)
59+
{
60+
enable_percpu_irq(pcpu_irq_num, IRQ_TYPE_NONE);
61+
62+
return 0;
63+
}
64+
65+
static int optee_cpuhp_disable_pcpu_irq(unsigned int cpu)
66+
{
67+
disable_percpu_irq(pcpu_irq_num);
68+
69+
return 0;
70+
}
71+
5572
/*
5673
* 1. Convert between struct tee_param and struct optee_msg_param
5774
*
@@ -991,9 +1008,8 @@ static u32 get_async_notif_value(optee_invoke_fn *invoke_fn, bool *value_valid,
9911008
return res.a1;
9921009
}
9931010

994-
static irqreturn_t notif_irq_handler(int irq, void *dev_id)
1011+
static irqreturn_t irq_handler(struct optee *optee)
9951012
{
996-
struct optee *optee = dev_id;
9971013
bool do_bottom_half = false;
9981014
bool value_valid;
9991015
bool value_pending;
@@ -1016,6 +1032,13 @@ static irqreturn_t notif_irq_handler(int irq, void *dev_id)
10161032
return IRQ_HANDLED;
10171033
}
10181034

1035+
static irqreturn_t notif_irq_handler(int irq, void *dev_id)
1036+
{
1037+
struct optee *optee = dev_id;
1038+
1039+
return irq_handler(optee);
1040+
}
1041+
10191042
static irqreturn_t notif_irq_thread_fn(int irq, void *dev_id)
10201043
{
10211044
struct optee *optee = dev_id;
@@ -1025,7 +1048,7 @@ static irqreturn_t notif_irq_thread_fn(int irq, void *dev_id)
10251048
return IRQ_HANDLED;
10261049
}
10271050

1028-
static int optee_smc_notif_init_irq(struct optee *optee, u_int irq)
1051+
static int init_irq(struct optee *optee, u_int irq)
10291052
{
10301053
int rc;
10311054

@@ -1040,12 +1063,103 @@ static int optee_smc_notif_init_irq(struct optee *optee, u_int irq)
10401063
return 0;
10411064
}
10421065

1066+
static irqreturn_t notif_pcpu_irq_handler(int irq, void *dev_id)
1067+
{
1068+
struct optee_pcpu *pcpu = dev_id;
1069+
struct optee *optee = pcpu->optee;
1070+
1071+
if (irq_handler(optee) == IRQ_WAKE_THREAD)
1072+
queue_work(optee->smc.notif_pcpu_wq,
1073+
&optee->smc.notif_pcpu_work);
1074+
1075+
return IRQ_HANDLED;
1076+
}
1077+
1078+
static void notif_pcpu_irq_work_fn(struct work_struct *work)
1079+
{
1080+
struct optee_smc *optee_smc = container_of(work, struct optee_smc,
1081+
notif_pcpu_work);
1082+
struct optee *optee = container_of(optee_smc, struct optee, smc);
1083+
1084+
optee_smc_do_bottom_half(optee->ctx);
1085+
}
1086+
1087+
static int init_pcpu_irq(struct optee *optee, u_int irq)
1088+
{
1089+
struct optee_pcpu __percpu *optee_pcpu;
1090+
int cpu, rc;
1091+
1092+
optee_pcpu = alloc_percpu(struct optee_pcpu);
1093+
if (!optee_pcpu)
1094+
return -ENOMEM;
1095+
1096+
for_each_present_cpu(cpu)
1097+
per_cpu_ptr(optee_pcpu, cpu)->optee = optee;
1098+
1099+
rc = request_percpu_irq(irq, notif_pcpu_irq_handler,
1100+
"optee_pcpu_notification", optee_pcpu);
1101+
if (rc)
1102+
goto err_free_pcpu;
1103+
1104+
INIT_WORK(&optee->smc.notif_pcpu_work, notif_pcpu_irq_work_fn);
1105+
optee->smc.notif_pcpu_wq = create_workqueue("optee_pcpu_notification");
1106+
if (!optee->smc.notif_pcpu_wq) {
1107+
rc = -EINVAL;
1108+
goto err_free_pcpu_irq;
1109+
}
1110+
1111+
optee->smc.optee_pcpu = optee_pcpu;
1112+
optee->smc.notif_irq = irq;
1113+
1114+
pcpu_irq_num = irq;
1115+
rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "optee/pcpu-notif:starting",
1116+
optee_cpuhp_enable_pcpu_irq,
1117+
optee_cpuhp_disable_pcpu_irq);
1118+
if (!rc)
1119+
rc = -EINVAL;
1120+
if (rc < 0)
1121+
goto err_free_pcpu_irq;
1122+
1123+
optee->smc.notif_cpuhp_state = rc;
1124+
1125+
return 0;
1126+
1127+
err_free_pcpu_irq:
1128+
free_percpu_irq(irq, optee_pcpu);
1129+
err_free_pcpu:
1130+
free_percpu(optee_pcpu);
1131+
1132+
return rc;
1133+
}
1134+
1135+
static int optee_smc_notif_init_irq(struct optee *optee, u_int irq)
1136+
{
1137+
if (irq_is_percpu_devid(irq))
1138+
return init_pcpu_irq(optee, irq);
1139+
else
1140+
return init_irq(optee, irq);
1141+
}
1142+
1143+
static void uninit_pcpu_irq(struct optee *optee)
1144+
{
1145+
cpuhp_remove_state(optee->smc.notif_cpuhp_state);
1146+
1147+
destroy_workqueue(optee->smc.notif_pcpu_wq);
1148+
1149+
free_percpu_irq(optee->smc.notif_irq, optee->smc.optee_pcpu);
1150+
free_percpu(optee->smc.optee_pcpu);
1151+
}
1152+
10431153
static void optee_smc_notif_uninit_irq(struct optee *optee)
10441154
{
10451155
if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF) {
10461156
optee_smc_stop_async_notif(optee->ctx);
10471157
if (optee->smc.notif_irq) {
1048-
free_irq(optee->smc.notif_irq, optee);
1158+
if (irq_is_percpu_devid(optee->smc.notif_irq))
1159+
uninit_pcpu_irq(optee);
1160+
else
1161+
free_irq(optee->smc.notif_irq, optee);
1162+
10491163
irq_dispose_mapping(optee->smc.notif_irq);
10501164
}
10511165
}

0 commit comments

Comments
 (0)