SCM: Add support for wait-queue aware firmware
v2 cover SCM: Add support for wait-queue aware firmware
This patch series enables the QCOM SCM driver to support firmware (FW) versions that expect the high-level OS (HLOS) to be tolerant of SCM call requests not being processed right away and, instead, being placed on a wait-queue in FW and processed accordingly.
The problem this feature is fixing is as follows. In a scenario where there is a VM in addition to HLOS (and an underlying hypervisor):
1. HLOS makes an SMC call on core 5
2. The hypervisor scheduling interrupt interrupts this SMC call.
3. The hypervisor schedules the VM on core 5.
4. The VM makes an SMC call on core 5.
5. The SMC call is non-interruptibly stuck on FW spinlock on core 5.
6. HLOS cannot reschedule since core 5 is not responding to Reschedule IPIs.
7. Watchdog timer expires waiting for core 5.
This problem is solved by FW returning a new return code SCM_WAITQ_SLEEP to HLOS right away when it is overwhelmed by the VM's SMC call. HLOS then places the call on a wait-queue and wakes it up when it receives an interrupt that signifies "all-clear".
This new design also supports scenarios involving only HLOS (and no other VMs). Such scenarios make use of a second new return code SCM_WAITQ_WAKE.
There are three new SMC calls also being defined in this design that, together with the two new return codes, form the handshake protocol between Linux and FW.
This design is also backwards-compatible with existing firmware versions that do not support this feature.
---
v2:
- Changes made to patches 4 and 5 are listed therein.
- Rebased dt-bindings on top of the YAML conversion patch [1].
[1] https://lore.kernel.org/lkml/[email protected]/
---
Guru Das Srinagesh (5):
dt-bindings: firmware: qcom-scm: Add "allow-multi-call" property
firmware: qcom: scm: Optionally remove SCM call serialization
dt-bindings: firmware: qcom-scm: Add optional interrupt
firmware: qcom: scm: Add wait-queue helper functions
firmware: qcom: scm: Add wait-queue handling logic
.../devicetree/bindings/firmware/qcom,scm.yaml | 10 ++ drivers/firmware/qcom_scm-smc.c | 140 +++++++++++++++++++-- drivers/firmware/qcom_scm.c | 125 +++++++++++++++++- drivers/firmware/qcom_scm.h | 14 +++ 4 files changed, 279 insertions(+), 10 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/5 dt-bindings: firmware: qcom-scm: Add "allow-multi-call" property
For firmware that supports it, allow multiple SCM calls to be passed down to it by removing the serialization lock in the SCM driver.
This patch is based on this YAML conversion patch [1] that is in-flight currently.
[1] https://lore.kernel.org/lkml/[email protected]/
--- Documentation/devicetree/bindings/firmware/qcom,scm.yaml | 5 +++++ 1 file changed, 5 insertions(+)
Documentation/devicetree/bindings/firmware/qcom,scm.yaml +5 -0
@@ -70,6 +70,11 @@ properties: '#reset-cells': const: 1 + allow-multi-call: + description: + Specify this flag to remove SCM call serialization. Need to ensure that + the firmware being used supports this feature first. + qcom,dload-mode: $ref: /schemas/types.yaml#/definitions/phandle-array items:
Signature · 1 line
2.7.4
Re: [PATCH v2 1/5] dt-bindings: firmware: qcom-scm: Add "allow-multi-call" property
Quoted 6 lines · Guru Das Srinagesh
For firmware that supports it, allow multiple SCM calls to be passed down to it by removing the serialization lock in the SCM driver.
This patch is based on this YAML conversion patch [1] that is in-flight currently.
Quoted 6 lines
[1] https://lore.kernel.org/lkml/[email protected]/
Signed-off-by Guru Das Srinagesh <[email protected]>--- Documentation/devicetree/bindings/firmware/qcom,scm.yaml | 5 +++++ 1 file changed, 5 insertions(+)
Please use scripts/get_maintainers.pl to get a list of necessary people and lists to CC. It might happen, that command when run on an older kernel, gives you outdated entries. Therefore please be sure you base your patches on recent Linux kernel.
Best regards,
Krzysztof
PATCH v2 2/5 firmware: qcom: scm: Optionally remove SCM call serialization
In reply to Guru Das SrinageshSome firmware versions support the handling of multiple SCM calls at the same time. Add a device tree boolean property which, when specified, allows this to happen.
--- drivers/firmware/qcom_scm-smc.c | 8 ++++++-- drivers/firmware/qcom_scm.c | 6 ++++++ drivers/firmware/qcom_scm.h | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-)
drivers/firmware/qcom_scm-smc.c +6 -2
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright (c) 2015,2019 The Linux Foundation. All rights reserved. +* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved. */ #include <linux/io.h>
@@ -63,11 +64,14 @@ static void __scm_smc_do(const struct arm_smccc_args *smc, } do { - mutex_lock(&qcom_scm_lock); + if (!qcom_scm_allow_multicall) + mutex_lock(&qcom_scm_lock); __scm_smc_do_quirk(smc, res); - mutex_unlock(&qcom_scm_lock); + if (!qcom_scm_allow_multicall) + mutex_unlock(&qcom_scm_lock); + if (res->a0 == QCOM_SCM_V2_EBUSY) { if (retry_count++ > QCOM_SCM_EBUSY_MAX_RETRY)
drivers/firmware/qcom_scm.c +6 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright (c) 2010,2015,2019 The Linux Foundation. All rights reserved. * Copyright (C) 2015 Linaro Ltd. +* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved. */ #include <linux/platform_device.h> #include <linux/init.h>
@@ -22,6 +23,8 @@static bool download_mode =IS_ENABLED (CONFIG_QCOM_SCM_DOWNLOAD_MODE_DEFAULT );module_param (download_mode, bool,0 ); +bool qcom_scm_allow_multicall = false; +#define SCM_HAS_CORE_CLK BIT(0)#define SCM_HAS_IFACE_CLK BIT(1)#define SCM_HAS_BUS_CLK BIT(2)
@@ -1333,6 +1336,9 @@ static int qcom_scm_probe(struct platform_device *pdev) __scm = scm; __scm->dev = &pdev->dev; + qcom_scm_allow_multicall =of_property_read_bool (__scm->dev->of_node, +"allow-multi-call" ); +__get_convention ();/*
drivers/firmware/qcom_scm.h +2 -0
@@ -1,5 +1,6 @@/* SPDX-License-Identifier: GPL-2.0-only */ /* Copyright (c) 2010-2015,2019 The Linux Foundation. All rights reserved. +* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved. */ #ifndef __QCOM_SCM_INT_H#define __QCOM_SCM_INT_H
@@ -12,6 +13,7 @@ enum qcom_scm_convention { };extern enum qcom_scm_convention qcom_scm_convention; +extern bool qcom_scm_allow_multicall;#define MAX_QCOM_SCM_ARGS 10#define MAX_QCOM_SCM_RETS 3
Signature · 1 line
2.7.4
PATCH v2 3/5 dt-bindings: firmware: qcom-scm: Add optional interrupt
In reply to Guru Das SrinageshAdd an interrupt specification to the bindings to support the wait-queue feature.
--- Documentation/devicetree/bindings/firmware/qcom,scm.yaml | 5 +++++ 1 file changed, 5 insertions(+)
Documentation/devicetree/bindings/firmware/qcom,scm.yaml +5 -0
@@ -75,6 +75,11 @@ properties: Specify this flag to remove SCM call serialization. Need to ensure that the firmware being used supports this feature first. + interrupts: + description: + The wait-queue interrupt that firmware raises as part of handshake + protocol to handle sleeping SCM calls. + qcom,dload-mode: $ref: /schemas/types.yaml#/definitions/phandle-array items:
Signature · 1 line
2.7.4
PATCH v2 4/5 firmware: qcom: scm: Add wait-queue helper functions
In reply to Guru Das SrinageshWhen the firmware (FW) supports multiple requests per VM, and the VM also supports it via the allow-multi-call device tree flag, the floodgates are thrown open for them to all reach the firmware at the same time.
Since the firmware currently being used has limited resources, it guards them with a resource lock and puts requests on a wait-queue internally and signals to HLOS that it is doing so. It does this by returning two new return values in addition to success or error: SCM_WAITQ_SLEEP and SCM_WAITQ_WAKE.
1) SCM_WAITQ_SLEEP:
When an SCM call receives this return value instead of success or error, FW has placed this call on a wait-queue and has signalled HLOS to put it to non-interruptible sleep. (The mechanism to wake it back up will be described in detail in the next patch for the sake of simplicity.)
Along with this return value, FW also passes to HLOS wq_ctx - a unique number (UID) identifying the wait-queue that it has put the call on, internally. This is to help HLOS with its own bookkeeping to wake this sleeping call later.
Additionally, FW also passes to HLOS smc_call_ctx - a UID identifying the SCM call thus being put to sleep. This is also for HLOS' bookkeeping to wake this call up later.
These two additional values are passed via the a1 and a2 registers.
N.B.: The "ctx" in the above UID names = "context".
2) SCM_WAITQ_WAKE:
When an SCM call receives this return value instead of success or error, FW wishes to signal HLOS to wake up a (different) previously sleeping call.
FW tells HLOS which call to wake up via the additional return values wq_ctx, smc_call_ctx and flags. The first two have already been explained above.
flags can be either WAKE_ONE or WAKE_ALL. Meaning, wake either one, or all, of the SCM calls that HLOS is associating with the given wq_ctx.
A sleeping SCM call can be woken up by either an interrupt that FW raises, or via a SCM_WAITQ_WAKE return value for a new SCM call.
The handshake mechanism that HLOS uses to talk to FW about wait-queue operations involves three new SMC calls. These are:
1) get_wq_ctx():
Arguments: None
Returns: wq_ctx, flags, more_pending
Get the wait-queue context, and wake up either one or all of the sleeping SCM calls associated with that wait-queue.
Additionally, repeat this if there are more wait-queues that are ready to have their requests woken up (more_pending).
2) wq_resume(smc_call_ctx):
Arguments: smc_call_ctx
HLOS needs to issue this in response to receiving an IRQ, passing to FW the same smc_call_ctx that FW receives from HLOS via the get_wq_ctx() call.
3) wq_wake_ack(smc_call_ctx):
Arguments: smc_call_ctx
HLOS needs to issue this in response to receiving an SCM_WAITQ_WAKE, passing to FW the same smc_call_ctx that FW passed to HLOS via the SMC_WAITQ_WAKE call.
(Reminder that the full handshake mechanism will be detailed in the subsequent patch.)
Also add the interrupt handler that wakes up a sleeping SCM call.
---
[v2]
- Corrected device tree property to "enable-multi-call" in commit message.
- Added idr_destroy() in the probe function.
drivers/firmware/qcom_scm-smc.c | 56 +++++++++++++++++++ drivers/firmware/qcom_scm.c | 119 +++++++++++++++++++++++++++++++++++++++- drivers/firmware/qcom_scm.h | 12 ++++ 3 files changed, 186 insertions(+), 1 deletion(-)
drivers/firmware/qcom_scm-smc.c +56 -0
@@ -53,6 +53,62 @@ static void __scm_smc_do_quirk(const struct arm_smccc_args *smc, }while (res->a0 ==QCOM_SCM_INTERRUPTED ); } +static void fill_wq_resume_args (struct arm_smccc_args *resume,u32 smc_call_ctx) +{ +memset (resume->args,0 ,ARRAY_SIZE (resume->args)); + + resume->args[0 ] =ARM_SMCCC_CALL_VAL (ARM_SMCCC_STD_CALL , +ARM_SMCCC_SMC_64 ,ARM_SMCCC_OWNER_SIP , +SCM_SMC_FNID (QCOM_SCM_SVC_WAITQ ,QCOM_SCM_WAITQ_RESUME )); + + resume->args[1 ] =QCOM_SCM_ARGS (1 ); + + resume->args[2 ] = smc_call_ctx; +} + +static void fill_wq_wake_ack_args (struct arm_smccc_args *wake_ack,u32 smc_call_ctx) +{ +memset (wake_ack->args,0 ,ARRAY_SIZE (wake_ack->args)); + + wake_ack->args[0 ] =ARM_SMCCC_CALL_VAL (ARM_SMCCC_STD_CALL , +ARM_SMCCC_SMC_64 ,ARM_SMCCC_OWNER_SIP , +SCM_SMC_FNID (QCOM_SCM_SVC_WAITQ ,QCOM_SCM_WAITQ_ACK )); + + wake_ack->args[1 ] =QCOM_SCM_ARGS (1 ); + + wake_ack->args[2 ] = smc_call_ctx; +} + +static void fill_get_wq_ctx_args (struct arm_smccc_args *get_wq_ctx) +{ +memset (get_wq_ctx->args,0 ,ARRAY_SIZE (get_wq_ctx->args)); + + get_wq_ctx->args[0 ] =ARM_SMCCC_CALL_VAL (ARM_SMCCC_STD_CALL , +ARM_SMCCC_SMC_64 ,ARM_SMCCC_OWNER_SIP , +SCM_SMC_FNID (QCOM_SCM_SVC_WAITQ ,QCOM_SCM_WAITQ_GET_WQ_CTX )); +} + +int scm_get_wq_ctx (u32 *wq_ctx,u32 *flags,u32 *more_pending) +{ +int ret; +struct arm_smccc_args get_wq_ctx = {0 }; +struct arm_smccc_res get_wq_res; + +fill_get_wq_ctx_args (&get_wq_ctx); + +__scm_smc_do_quirk (&get_wq_ctx, &get_wq_res); +/* Guaranteed to return only success or error, no WAITQ_* */ + ret = get_wq_res.a0; +if (ret) +return ret; + + *wq_ctx = get_wq_res.a1; + *flags = get_wq_res.a2; + *more_pending = get_wq_res.a3; + +return 0 ; +} +static void __scm_smc_do (const struct arm_smccc_args *smc,struct arm_smccc_res *res,bool atomic) {
drivers/firmware/qcom_scm.c +118 -1
187 lines not on this page · raw message
drivers/firmware/qcom_scm.h +12 -0
@@ -62,6 +62,11 @@ struct qcom_scm_res {u64 result[MAX_QCOM_SCM_RETS ]; }; +struct qcom_scm ; +extern struct completion *qcom_scm_lookup_wq (struct qcom_scm *scm,u32 wq_ctx); +extern void scm_waitq_flag_handler (struct completion *wq,u32 flags); +extern int scm_get_wq_ctx (u32 *wq_ctx,u32 *flags,u32 *more_pending); +#define SCM_SMC_FNID (s, c) ((((s) & 0xFF) << 8) | ((c) & 0xFF)) extern int __scm_smc_call(struct device *dev, const struct qcom_scm_desc *desc, enum qcom_scm_convention qcom_convention,
@@ -131,6 +136,11 @@ extern int scm_legacy_call(struct device *dev, const struct qcom_scm_desc *desc,#define QCOM_SCM_SMMU_CONFIG_ERRATA1 0x03#define QCOM_SCM_SMMU_CONFIG_ERRATA1_CLIENT_ALL 0x02 +#define QCOM_SCM_SVC_WAITQ 0x24 +#define QCOM_SCM_WAITQ_ACK 0x01 +#define QCOM_SCM_WAITQ_RESUME 0x02 +#define QCOM_SCM_WAITQ_GET_WQ_CTX 0x03 +extern void __qcom_scm_init (void );/* common error codes */
@@ -141,6 +151,8 @@ extern void __qcom_scm_init(void);#define QCOM_SCM_EINVAL_ARG -2#define QCOM_SCM_ERROR -1#define QCOM_SCM_INTERRUPTED 1 +#define QCOM_SCM_WAITQ_SLEEP 2 +#define QCOM_SCM_WAITQ_WAKE 3static inline int qcom_scm_remap_error (int err) {
Signature · 1 line
2.7.4
Re: [PATCH v2 4/5] firmware: qcom: scm: Add wait-queue helper functions
Quoted 43 lines · Guru Das Srinagesh
When the firmware (FW) supports multiple requests per VM, and the VM also supports it via the
allow-multi-calldevice tree flag, the floodgates are thrown open for them to all reach the firmware at the same time.Since the firmware currently being used has limited resources, it guards them with a resource lock and puts requests on a wait-queue internally and signals to HLOS that it is doing so. It does this by returning two new return values in addition to success or error:
SCM_WAITQ_SLEEPandSCM_WAITQ_WAKE.1)
SCM_WAITQ_SLEEP:When an SCM call receives this return value instead of success or error, FW has placed this call on a wait-queue and has signalled HLOS to put it to non-interruptible sleep. (The mechanism to wake it back up will be described in detail in the next patch for the sake of simplicity.)
Along with this return value, FW also passes to HLOS
wq_ctx- a unique number (UID) identifying the wait-queue that it has put the call on, internally. This is to help HLOS with its own bookkeeping to wake this sleeping call later.Additionally, FW also passes to HLOS
smc_call_ctx- a UID identifying the SCM call thus being put to sleep. This is also for HLOS' bookkeeping to wake this call up later.These two additional values are passed via the a1 and a2 registers.
N.B.: The "ctx" in the above UID names = "context".
2)
SCM_WAITQ_WAKE:When an SCM call receives this return value instead of success or error, FW wishes to signal HLOS to wake up a (different) previously sleeping call.
FW tells HLOS which call to wake up via the additional return values
wq_ctx,smc_call_ctxandflags. The first two have already been explained above.
Quoted 6 lines
flagscan be eitherWAKE_ONEorWAKE_ALL. Meaning, wake either one, or all, of the SCM calls that HLOS is associating with the givenwq_ctx.A sleeping SCM call can be woken up by either an interrupt that FW raises, or via a
SCM_WAITQ_WAKEreturn value for a new SCM call.
Do you know why the FW was not designed to always use an interrupt? That would have made the handling of this in kernel a lot less complicated.
Quoted 30 lines
The handshake mechanism that HLOS uses to talk to FW about wait-queue operations involves three new SMC calls. These are:
1)
get_wq_ctx():Arguments: None
Returns:wq_ctx, flags,more_pendingGet the wait-queue context, and wake up either one or all of the sleeping SCM calls associated with that wait-queue.
Additionally, repeat this if there are more wait-queues that are ready to have their requests woken up (
more_pending).2)
wq_resume(smc_call_ctx):Arguments:
smc_call_ctxHLOS needs to issue this in response to receiving an IRQ, passing to FW the same
smc_call_ctxthat FW receives from HLOS via theget_wq_ctx()call.3)
wq_wake_ack(smc_call_ctx):Arguments:
smc_call_ctxHLOS needs to issue this in response to receiving an
SCM_WAITQ_WAKE, passing to FW the samesmc_call_ctxthat FW passed to HLOS via theSMC_WAITQ_WAKEcall.
Quoted 7 lines
(Reminder that the full handshake mechanism will be detailed in the subsequent patch.)
Also add the interrupt handler that wakes up a sleeping SCM call.
Signed-off-by Guru Das Srinagesh <[email protected]>---
[]..
Quoted 56 lines
drivers/firmware/qcom_scm.c +56
-0+struct completion *qcom_scm_lookup_wq (struct qcom_scm *scm,u32 wq_ctx) +{ +struct completion *wq =NULL ; +u32 wq_ctx_idr = wq_ctx; +unsigned long flags; +int err; + +spin_lock_irqsave (&scm->waitq.idr_lock, flags); + wq =idr_find (&scm->waitq.idr, wq_ctx); +if (wq) + goto out; + + wq =devm_kzalloc (scm->dev,sizeof (*wq),GFP_ATOMIC ); +if (!wq) { + wq =ERR_PTR (-ENOMEM ); + goto out; + } + +init_completion (wq); + + err =idr_alloc_u32 (&scm->waitq.idr, wq, &wq_ctx_idr, +U32_MAX ,GFP_ATOMIC ); +if (err <0 ) { +devm_kfree (scm->dev, wq); + wq =ERR_PTR (err); + } + +out: +spin_unlock_irqrestore (&scm->waitq.idr_lock, flags); +return wq; +} + +void scm_waitq_flag_handler (struct completion *wq,u32 flags) +{ +switch (flags) { +case QCOM_SMC_WAITQ_FLAG_WAKE_ONE : +complete (wq); +break ; +case QCOM_SMC_WAITQ_FLAG_WAKE_ALL : +complete_all (wq); +break ; +default : +pr_err ("invalid flags: %u\n" , flags); + } +} + +static void scm_irq_work (struct work_struct *work) +{ +int ret; +u32 wq_ctx, flags, more_pending =0 ; +struct completion *wq_to_wake; + struct qcom_scm_waitq *w = container_of(work, struct qcom_scm_waitq, scm_irq_work); + struct qcom_scm *scm = container_of(w, struct qcom_scm, waitq); + + do { + ret = scm_get_wq_ctx(&wq_ctx, &flags, &more_pending);
drivers/firmware/qcom_scm.c bool qcom_scm_is_available(void) · line 1352
Quoted 10 lines
drivers/firmware/qcom_scm.c +10
-0+if (ret) { +pr_err ("GET_WQ_CTX SMC call failed: %d\n" , ret); +return ; + } + + wq_to_wake =qcom_scm_lookup_wq (scm, wq_ctx); +if (IS_ERR_OR_NULL (wq_to_wake)) { +pr_err ("No waitqueue found for wq_ctx %d: %ld\n" , + wq_ctx,PTR_ERR (wq_to_wake)); +return ;
What happens if at this point 'more_pending' was true? will the FW raise another interrupt?
Quoted 5 lines
drivers/firmware/qcom_scm.c +5
-0+ } + +scm_waitq_flag_handler (wq_to_wake, flags); + }while (more_pending); +}
Re: [PATCH v2 4/5] firmware: qcom: scm: Add wait-queue helper functions
Hey Rajendra,
Sorry for the delay in response. Needed to clarify with internal team members on these questions before responding.
Quoted 6 lines · Rajendra Nayak
Quoted 4 lines · Guru Das Srinagesh
When the firmware (FW) supports multiple requests per VM, and the VM also supports it via the
allow-multi-calldevice tree flag, the floodgates are thrown open for them to all reach the firmware at the same time.
[...]
Quoted 14 lines
Quoted 14 lines
2)
SCM_WAITQ_WAKE:When an SCM call receives this return value instead of success or error, FW wishes to signal HLOS to wake up a (different) previously sleeping call.
FW tells HLOS which call to wake up via the additional return values
wq_ctx,smc_call_ctxandflags. The first two have already been explained above.
flagscan be eitherWAKE_ONEorWAKE_ALL. Meaning, wake either one, or all, of the SCM calls that HLOS is associating with the givenwq_ctx.
Quoted 5 lines
Quoted 2 lines
A sleeping SCM call can be woken up by either an interrupt that FW raises, or via a
SCM_WAITQ_WAKEreturn value for a new SCM call.Do you know why the FW was not designed to always use an interrupt? That would have made the handling of this in kernel a lot less complicated.
Because:
1. Our firmware in TrustZone cannot raise interrupts on its own - it needs the hypervisor to do that.
2. Thus, in platforms where there is no hypervisor, there is no interrupt possible - only SMC_WAITQ_WAKE.
Therefore, relying only on an interrupt would render the driver unable to support platforms without a hypervisor, which we didn't want to do.
Quoted 3 lines
Quoted 3 lines
The handshake mechanism that HLOS uses to talk to FW about wait-queue operations involves three new SMC calls. These are:
[...]
Quoted 15 lines
Quoted 15 lines
drivers/firmware/qcom_scm.c +15
-0+static void scm_irq_work (struct work_struct *work) +{ +int ret; +u32 wq_ctx, flags, more_pending =0 ; +struct completion *wq_to_wake; + struct qcom_scm_waitq *w = container_of(work, struct qcom_scm_waitq, scm_irq_work); + struct qcom_scm *scm = container_of(w, struct qcom_scm, waitq); + + do { + ret = scm_get_wq_ctx(&wq_ctx, &flags, &more_pending); + if (ret) { + pr_err("GET_WQ_CTX SMC call failed: %d\n", ret); + return; + } +
Quoted 8 lines
drivers/firmware/qcom_scm.c bool qcom_scm_is_available(void) · line 1352
Quoted 5 lines
drivers/firmware/qcom_scm.c +5
-0+ wq_to_wake =qcom_scm_lookup_wq (scm, wq_ctx); +if (IS_ERR_OR_NULL (wq_to_wake)) { +pr_err ("No waitqueue found for wq_ctx %d: %ld\n" , + wq_ctx,PTR_ERR (wq_to_wake)); +return ;What happens if at this point '
more_pending' was true? will the FW raise another interrupt?
Hmm. At this point, the interrupt handler is early-exiting without waking up a sleeping call via the flag_handler() because firmware has goofed up and given it an invalid wq_ctx. We have bigger problems than more_pending being true.
Quoted 6 lines
drivers/firmware/qcom_scm.c bool qcom_scm_is_available(void) · line 1357
Quoted 6 lines
drivers/firmware/qcom_scm.c +5
-0+ } + +scm_waitq_flag_handler (wq_to_wake, flags); + }while (more_pending); +}
Thank you.
Guru Das.
Re: [PATCH v2 4/5] firmware: qcom: scm: Add wait-queue helper functions
Quoted 3 lines · Guru Das Srinagesh
Hey Rajendra,
Sorry for the delay in response. Needed to clarify with internal team members
no worries,
Quoted 34 lines
on these questions before responding.
Quoted 6 lines · Rajendra Nayak
Quoted 4 lines · Guru Das Srinagesh
When the firmware (FW) supports multiple requests per VM, and the VM also supports it via the
allow-multi-calldevice tree flag, the floodgates are thrown open for them to all reach the firmware at the same time.[...]
Quoted 14 lines
Quoted 14 lines
2)
SCM_WAITQ_WAKE:When an SCM call receives this return value instead of success or error, FW wishes to signal HLOS to wake up a (different) previously sleeping call.
FW tells HLOS which call to wake up via the additional return values
wq_ctx,smc_call_ctxandflags. The first two have already been explained above.
flagscan be eitherWAKE_ONEorWAKE_ALL. Meaning, wake either one, or all, of the SCM calls that HLOS is associating with the givenwq_ctx.Quoted 5 lines
Quoted 2 lines
A sleeping SCM call can be woken up by either an interrupt that FW raises, or via a
SCM_WAITQ_WAKEreturn value for a new SCM call.Do you know why the FW was not designed to always use an interrupt? That would have made the handling of this in kernel a lot less complicated.
Because:
Quoted 8 lines
1. Our firmware in TrustZone cannot raise interrupts on its own - it needs the hypervisor to do that.
2. Thus, in platforms where there is no hypervisor, there is no interrupt possible - only
SMC_WAITQ_WAKE.Therefore, relying only on an interrupt would render the driver unable to support platforms without a hypervisor, which we didn't want to do.
Thanks Guru for the clarification, however what problem are we really solving with this on platforms _without_ a hypervisor?
Your cover letter said
'The problem this feature is fixing is as follows. In a scenario where there is a VM in addition to HLOS (and an underlying hypervisor):'
So I assumed this was primarily for platforms _with_ a VM/Hypervisor?
I understand that even with just the HLOS and no VM, if we can get these requests processed concurrently it still adds value, but eventually Trustzone will still process these requests sequentially right?
Quoted 44 lines
Quoted 3 lines
Quoted 3 lines
The handshake mechanism that HLOS uses to talk to FW about wait-queue operations involves three new SMC calls. These are:
[...]
Quoted 15 lines
Quoted 15 lines
drivers/firmware/qcom_scm.c +15
-0+static void scm_irq_work (struct work_struct *work) +{ +int ret; +u32 wq_ctx, flags, more_pending =0 ; +struct completion *wq_to_wake; + struct qcom_scm_waitq *w = container_of(work, struct qcom_scm_waitq, scm_irq_work); + struct qcom_scm *scm = container_of(w, struct qcom_scm, waitq); + + do { + ret = scm_get_wq_ctx(&wq_ctx, &flags, &more_pending); + if (ret) { + pr_err("GET_WQ_CTX SMC call failed: %d\n", ret); + return; + } +Quoted 8 lines
drivers/firmware/qcom_scm.c bool qcom_scm_is_available(void) · line 1352
Quoted 5 lines
drivers/firmware/qcom_scm.c +5
-0+ wq_to_wake =qcom_scm_lookup_wq (scm, wq_ctx); +if (IS_ERR_OR_NULL (wq_to_wake)) { +pr_err ("No waitqueue found for wq_ctx %d: %ld\n" , + wq_ctx,PTR_ERR (wq_to_wake)); +return ;What happens if at this point '
more_pending' was true? will the FW raise another interrupt?Hmm. At this point, the interrupt handler is early-exiting without waking up a sleeping call via the
flag_handler()because firmware has goofed up and given it an invalidwq_ctx. We have bigger problems thanmore_pendingbeing true.Quoted 6 lines
drivers/firmware/qcom_scm.c bool qcom_scm_is_available(void) · line 1357
Quoted 6 lines
drivers/firmware/qcom_scm.c +5
-0+ } + +scm_waitq_flag_handler (wq_to_wake, flags); + }while (more_pending); +}Thank you.
Guru Das.
Re: [PATCH v2 4/5] firmware: qcom: scm: Add wait-queue helper functions
Quoted 50 lines · Rajendra Nayak
Quoted 32 lines · Guru Das Srinagesh
Quoted 6 lines · Rajendra Nayak
Quoted 4 lines · Guru Das Srinagesh
When the firmware (FW) supports multiple requests per VM, and the VM also supports it via the
allow-multi-calldevice tree flag, the floodgates are thrown open for them to all reach the firmware at the same time.[...]
Quoted 14 lines
Quoted 14 lines
2)
SCM_WAITQ_WAKE:When an SCM call receives this return value instead of success or error, FW wishes to signal HLOS to wake up a (different) previously sleeping call.
FW tells HLOS which call to wake up via the additional return values
wq_ctx,smc_call_ctxandflags. The first two have already been explained above.
flagscan be eitherWAKE_ONEorWAKE_ALL. Meaning, wake either one, or all, of the SCM calls that HLOS is associating with the givenwq_ctx.Quoted 5 lines
Quoted 2 lines
A sleeping SCM call can be woken up by either an interrupt that FW raises, or via a
SCM_WAITQ_WAKEreturn value for a new SCM call.Do you know why the FW was not designed to always use an interrupt? That would have made the handling of this in kernel a lot less complicated.
Because:
Quoted 8 lines
1. Our firmware in TrustZone cannot raise interrupts on its own - it needs the hypervisor to do that.
2. Thus, in platforms where there is no hypervisor, there is no interrupt possible - only
SMC_WAITQ_WAKE.Therefore, relying only on an interrupt would render the driver unable to support platforms without a hypervisor, which we didn't want to do.
Thanks Guru for the clarification, however what problem are we really solving with this on platforms _without_ a hypervisor?
Your cover letter said
'The problem this feature is fixing is as follows. In a scenario where there is a VM in addition to HLOS (and an underlying hypervisor):'
Quoted 5 lines
So I assumed this was primarily for platforms _with_ a VM/Hypervisor?
I understand that even with just the HLOS and no VM, if we can get these requests processed concurrently it still adds value, but eventually Trustzone will still process these requests sequentially right?
The Trustzone (TZ) firmware doesn't process all requests sequentially - there are a few that require a "callback" back to HLOS. In such cases, the original SCM call releases the TZ serialization lock, thereby allowing a new call to enter TZ. It is better to have requests sleep and wake via WAITQ_SLEEP and WAITQ_WAKE rather than the alternative - retrying an arbitrary amount of times via -EBUSY.
PATCH v2 5/5 firmware: qcom: scm: Add wait-queue handling logic
In reply to Guru Das SrinageshAdd logic to handle QCOM_SCM_WAITQ_SLEEP or QCOM_SCM_WAITQ_WAKE return codes.
Scenario 1: Requests made by 2 different VMs:
VM_1 VM_2 Firmware
│ │ │
│ │ │
│ │ │
│ │ │
│ REQUEST_1 │ │
├────────────────────────┼─────────────────────────────────┤
│ │ │
│ │ ┌──┼──┐
│ │ │ │ │
│ │ REQUEST_2 │ │ │
│ ├──────────────────────────────┼──┤ │
│ │ │ │ │Resource
│ │ │ │ │is busy
│ │ {WQ_SLEEP} │ │ │
│ │◄─────────────────────────────┼──┤ │
│ │ wq_ctx, smc_call_ctx │ │ │
│ │ └──┼──┘
│ REQUEST_1 COMPLETE │ │
│◄───────────────────────┼─────────────────────────────────┤
│ │ │
│ │ IRQ │
│ │◄─-------------------------------│
│ │ │
│ │ get_wq_ctx() │
│ ├────────────────────────────────►│
│ │ │
│ │ │
│ │◄────────────────────────────────┤
│ │ wq_ctx, flags, and │
│ │ more_pending │
│ │ │
│ │ │
│ │ wq_resume(smc_call_ctx) │
│ ├────────────────────────────────►│
│ │ │
│ │ │
│ │ REQUEST_2 COMPLETE │
│ │◄────────────────────────────────┤
│ │ │
│ │ │Scenario 2: Two Requests coming in from same VM:
VM_1 Firmware
│ │
│ │
│ │
│ │
│ REQUEST_1 │
├──────────────────────────────────────────────────────────┤
│ │
│ ┌────┼───┐
│ │ │ │
│ │ │ │
│ │ │ │
│ REQUEST_2 │ │ │
├─────────────────────────────────────────────────────┼───►│ │
│ │ │ │Resource
│ │ │ │is busy
│ {WQ_SLEEP} │ │ │
│◄────────────────────────────────────────────────────┼────┤ │
│ wq_ctx, req2_smc_call_ctx │ │ │
│ │ │ │
│ └────┼───┘
│ │
│ {WQ_WAKE} │
│◄─────────────────────────────────────────────────────────┤
│ wq_ctx, req1_smc_call_ctx, flags │
│ │
│ │
│ wq_wake_ack(req1_smc_call_ctx) │
├─────────────────────────────────────────────────────────►│
│ │
│ REQUEST_1 COMPLETE │
│◄─────────────────────────────────────────────────────────┤
│ │
│ │
│ wq_resume(req_2_smc_call_ctx) │
├─────────────────────────────────────────────────────────►│
│ │
│ REQUEST_2 COMPLETE │
│◄─────────────────────────────────────────────────────────┤
│ │With the exception of get_wq_ctx(), the other two newly-introduced SMC calls, wq_ack() and wq_resume() can themselves return WQ_SLEEP (these nested rounds of WQ_SLEEP are not shown in the above diagram for the sake of simplicity). Therefore, introduce a new do-while loop to handle multiple WQ_SLEEP return values for the same parent SCM call.
Request Completion in the above diagram refers to either a success return value (zero) or error (and not SMC_WAITQ_SLEEP or SMC_WAITQ_WAKE).
---
[v2]
- Combined the error and success cases in scm_smc_do_quirk().
drivers/firmware/qcom_scm-smc.c | 76 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 7 deletions(-)
drivers/firmware/qcom_scm-smc.c +69 -7
@@ -53,6 +53,9 @@ static void __scm_smc_do_quirk(const struct arm_smccc_args *smc, }while (res->a0 ==QCOM_SCM_INTERRUPTED ); } +#define IS_WAITQ_SLEEP_OR_WAKE (res) \ + (res->a0 == QCOM_SCM_WAITQ_SLEEP || res->a0 == QCOM_SCM_WAITQ_WAKE) +static void fill_wq_resume_args (struct arm_smccc_args *resume,u32 smc_call_ctx) {memset (resume->args,0 ,ARRAY_SIZE (resume->args));
@@ -109,25 +112,77 @@ int scm_get_wq_ctx(u32 *wq_ctx, u32 *flags, u32 *more_pending)return 0 ; } -static void __scm_smc_do (const struct arm_smccc_args *smc, +static int scm_smc_do_quirk (struct device *dev,struct arm_smccc_args *smc, +struct arm_smccc_res *res) +{ +struct completion *wq =NULL ; +struct qcom_scm *qscm; +u32 wq_ctx, smc_call_ctx, flags; + +do { +__scm_smc_do_quirk (smc, res); + +if (IS_WAITQ_SLEEP_OR_WAKE (res)) { + wq_ctx = res->a1; + smc_call_ctx = res->a2; + flags = res->a3; + +if (!dev) +return -EPROBE_DEFER ; + + qscm =dev_get_drvdata (dev); + wq =qcom_scm_lookup_wq (qscm, wq_ctx); +if (IS_ERR_OR_NULL (wq)) { +pr_err ("No waitqueue found for wq_ctx %d: %ld\n" , + wq_ctx,PTR_ERR (wq)); +return PTR_ERR (wq); + } + +if (res->a0 ==QCOM_SCM_WAITQ_SLEEP ) { +wait_for_completion (wq); +fill_wq_resume_args (smc, smc_call_ctx); + wq =NULL ; +continue ; + }else { +fill_wq_wake_ack_args (smc, smc_call_ctx); +continue ; + } + }else if (!res->a0 || (long )res->a0 <0 ) { +/* +* Success, or error. +* wq will be set only if a prior WAKE happened. +* Its value will be the one from the prior WAKE. +*/ +if (wq) +scm_waitq_flag_handler (wq, flags); +break ; + } + }while (IS_WAITQ_SLEEP_OR_WAKE (res)); + +return 0 ; +} + +static int __scm_smc_do (struct device *dev,struct arm_smccc_args *smc,struct arm_smccc_res *res,bool atomic) { -int retry_count =0 ; +int ret, retry_count =0 ;if (atomic) {__scm_smc_do_quirk (smc, res); -return ; +return 0 ; } do { if (!qcom_scm_allow_multicall) mutex_lock(&qcom_scm_lock); - __scm_smc_do_quirk(smc, res); + ret = scm_smc_do_quirk(dev, smc, res); if (!qcom_scm_allow_multicall) mutex_unlock(&qcom_scm_lock); + if (ret) + return ret; if (res->a0 == QCOM_SCM_V2_EBUSY) { if (retry_count++ > QCOM_SCM_EBUSY_MAX_RETRY)
@@ -135,6 +190,8 @@ static void __scm_smc_do(const struct arm_smccc_args *smc,msleep (QCOM_SCM_EBUSY_WAIT_MS ); } }while (res->a0 ==QCOM_SCM_V2_EBUSY ); + +return 0 ; }
@@ -143,7 +200,7 @@ int __scm_smc_call(struct device *dev, const struct qcom_scm_desc *desc, struct qcom_scm_res *res, bool atomic) { int arglen = desc->arginfo & 0xf; -int i; +int i, ret;dma_addr_t args_phys =0 ;void *args_virt =NULL ;size_t alloc_len;
@@ -195,19 +252,24 @@ int __scm_smc_call(struct device *dev, const struct qcom_scm_desc *desc, smc.args[SCM_SMC_LAST_REG_IDX ] = args_phys; } -__scm_smc_do (&smc, &smc_res, atomic); + ret =__scm_smc_do (dev, &smc, &smc_res, atomic); +/* ret error check follows after args_virt cleanup*/ if (args_virt) {dma_unmap_single (dev, args_phys, alloc_len,DMA_TO_DEVICE );kfree (args_virt); } +if (ret) +return ret; +if (res) { res->result[0 ] = smc_res.a1; res->result[1 ] = smc_res.a2; res->result[2 ] = smc_res.a3; } -return (long )smc_res.a0 ?qcom_scm_remap_error (smc_res.a0) :0 ; + ret = (long )smc_res.a0 ?qcom_scm_remap_error (smc_res.a0) :0 ; +return ret; }
Signature · 1 line
2.7.4