usb: dwc3: qcom: Defer VBUS override update during system suspend
Subsystems (3)
PATCH v2 1/1 usb: dwc3: qcom: Defer VBUS override update during system suspend
dwc3_qcom_set_role_notifier() updates the VBUS override in QSCRATCH synchronously, from the context of the usb_role_switch_set_role() caller. The glue assumes that role switches only happen from a freezable workqueue, but that holds for the dwc3 core's mode switch, not for this notifier: the UCSI connector work, for one, runs on the non-freezable system workqueue.
On SM8750, when the USB cable is unplugged while the system is in s2idle, the UCSI connector work for the change runs before the devices have resumed. It switches the role to USB_ROLE_NONE, and the notifier accesses QSCRATCH while the glue clocks are still off, before dwc3_qcom_pm_resume() turns them back on. The access does not complete and the SoC resets. pm_runtime_resume_and_get() does not prevent this:
runtime PM is disabled at that point and the device is still marked active, so it returns success without resuming anything.
While the glue is suspended for system sleep, only record the VBUS override update and apply it from dwc3_qcom_pm_resume() once the clocks are on. Apply it after the core has resumed, since resuming the gadget enables the override and the role switch is the more recent event. Serialize the notifier against the system suspend and resume callbacks with a mutex, so that a role switch can't run while the core or the glue is being suspended or resumed.
---
Changes in v2:
- Hold role_lock across the whole of dwc3_qcom_pm_suspend() and dwc3_qcom_pm_resume(). In v1 a role switch could still run during dwc3_pm_suspend() or dwc3_pm_resume() and bypass the deferral, as the Sashiko review pointed out [4].
- Link to v1: https://lore.kernel.org/all/[email protected]/
The review also asked about the direct_complete path, which skips these callbacks. It is not taken for this driver: every qcom,snps-dwc3 node in the tree has a power domain, and genpd_prepare() never returns 1. Where it is taken, the role switch is dropped as it is without this patch, and the hardware is not touched.
Tested on SM8750 (OnePlus Pad 3; the board DT is not upstream yet), v7.3-rc5 plus board patches, with the UCSI connector work still on system_wq and without the pmic_glink RFC [5]. The test kernel also has local ucsi_glink and qcom_battmgr changes that make a cable plug or unplug wake the system from s2idle, similar to Jishnu Prakash's ucsi_glink patch [3]:
- cable unplugged during s2idle: resumed in 2 of 2 runs; without this patch the SoC reset in all 6 earlier runs. The switch to USB_ROLE_NONE came 15 ms after the start of the resume, during the noirq phase, and was only recorded; dwc3_qcom_pm_resume() cleared the override about a second later.
- cable plugged in during s2idle, s2idle with the cable connected, and unplug/replug while awake: no change in behaviour.
- with a test-only msleep() under role_lock, so that a role switch comes in while the lock is held:
- 3 s in dwc3_qcom_pm_resume() after dwc3_pm_resume(): the switch from a cable plug during s2idle waited for the lock and then set the override directly;
- 20 s in dwc3_qcom_pm_suspend() before dwc3_pm_suspend(): the switch from an unplug waited for the lock and was deferred. The unplug aborted the suspend, and dwc3_qcom_pm_resume() applied the update. No reset.
I have not tested this patch without the wakeup changes.
The UCSI side is discussed in Abel Vesa's RFC to queue the connector work on system_freezable_wq [1]. Since v1, I have also sent an RFC that holds back the PMIC GLINK notifications over system sleep [5]. With it, an unplug during s2idle on SM8750 no longer reaches this notifier. This patch is needed either way: the RFC does not hold back a role switch that is already in flight when the system suspend starts, and callers of usb_role_switch_set_role() outside pmic_glink, TCPM's kthread worker for one, are not frozen.
Faisal Hassan's SA8255P series [2] also changes dwc3_qcom_pm_suspend() and dwc3_qcom_pm_resume() (the wakeup argument becomes a pm_message_t); whichever goes in second needs a small rebase.
I used an AI assistant (Claude) for the analysis and to draft this patch and its description; I reviewed the result and ran the tests on my device.
[1] https://lore.kernel.org/all/20250205-ucsi-schedule-conn-worker-on-freezable-wq-v1-1-107d1356b77b@linaro.org/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
drivers/usb/dwc3/dwc3-qcom.c | 61 +++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 11 deletions(-)
drivers/usb/dwc3/dwc3-qcom.c +50 -11
@@ -10,6 +10,7 @@#include <linux/irq.h> #include <linux/of_clk.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/kernel.h> #include <linux/interconnect.h> #include <linux/platform_device.h>
@@ -85,6 +86,11 @@ struct dwc3_qcom {struct icc_path *icc_path_apps;enum usb_role current_role; + +/* Serializes the role notifier against system suspend and resume */ +struct mutex role_lock; +bool vbus_override_pending; +bool vbus_override; };#define to_dwc3_qcom (d) container_of((d), struct dwc3_qcom, dwc)
@@ -561,27 +567,40 @@ static int dwc3_qcom_setup_irq(struct dwc3_qcom *qcom, struct platform_device *pstatic void dwc3_qcom_set_role_notifier (struct dwc3 *dwc,enum usb_role next_role) {struct dwc3_qcom *qcom =to_dwc3_qcom (dwc); +bool enable; + +guard (mutex)(&qcom->role_lock);if (qcom->current_role == next_role)return ; + enable = qcom->current_role !=USB_ROLE_DEVICE ; + +/* +* Not every caller of usb_role_switch_set_role() is frozen during +* system sleep, e.g. the UCSI connector work can run in the noirq +* resume phase. The glue clocks are off then and runtime PM can't +* turn them on, so leave QSCRATCH alone and let +* dwc3_qcom_pm_resume() apply the update. +*/ +if (qcom->pm_suspended) { + qcom->vbus_override_pending = true; + qcom->vbus_override = enable; + qcom->current_role = next_role; +return ; + } +if (pm_runtime_resume_and_get (qcom->dev)) {dev_dbg (qcom->dev,"Failed to resume device\n" );return ; } -if (qcom->current_role ==USB_ROLE_DEVICE ) -dwc3_qcom_vbus_override_enable (qcom, false); -else if (qcom->current_role !=USB_ROLE_DEVICE ) -dwc3_qcom_vbus_override_enable (qcom, true); + qcom->vbus_override_pending = false; +dwc3_qcom_vbus_override_enable (qcom, enable);pm_runtime_mark_last_busy (qcom->dev);pm_runtime_put_sync (qcom->dev); -/* -* Current role changes via usb_role_switch_set_role callback protected -* internally by mutex lock. -*/ qcom->current_role = next_role; }
@@ -624,6 +643,10 @@ static int dwc3_qcom_probe(struct platform_device *pdev) qcom->dev = &pdev->dev; + ret =devm_mutex_init (dev, &qcom->role_lock); +if (ret) +return ret; + qcom->resets =devm_reset_control_array_get_optional_exclusive (dev);if (IS_ERR (qcom->resets)) { return dev_err_probe(&pdev->dev, PTR_ERR(qcom->resets),
@@ -752,6 +775,12 @@ static int dwc3_qcom_pm_suspend(struct device *dev)bool wakeup =device_may_wakeup (dev);int ret; +/* +* A role switch that comes in now waits until the glue is suspended +* and is then deferred, see dwc3_qcom_set_role_notifier(). +*/ +guard (mutex)(&qcom->role_lock); + ret =dwc3_pm_suspend (&qcom->dwc);if (ret)return ret;
@@ -772,6 +801,8 @@ static int dwc3_qcom_pm_resume(struct device *dev)bool wakeup =device_may_wakeup (dev);int ret; +guard (mutex)(&qcom->role_lock); + ret =dwc3_qcom_resume (qcom, wakeup);if (ret)return ret;
@@ -779,10 +810,18 @@ static int dwc3_qcom_pm_resume(struct device *dev) qcom->pm_suspended = false; ret =dwc3_pm_resume (&qcom->dwc); -if (ret) -return ret; -return 0 ; +/* +* Apply a VBUS override update from a role switch that came in while +* suspended. Do it after the core has resumed, as resuming the gadget +* enables the override and the role switch is the more recent event. +*/ +if (qcom->vbus_override_pending) { +dwc3_qcom_vbus_override_enable (qcom, qcom->vbus_override); + qcom->vbus_override_pending = false; + } + +return ret; }static void dwc3_qcom_complete (struct device *dev)
base-commit: a90ee4305c4a5df72c11b31dacfdc76e00fcf78a
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 24 lines
dwc3_qcom_set_role_notifier()updates the VBUS override in QSCRATCH synchronously, from the context of theusb_role_switch_set_role()caller. The glue assumes that role switches only happen from a freezable workqueue, but that holds for the dwc3 core's mode switch, not for this notifier: the UCSI connector work, for one, runs on the non-freezable system workqueue.On SM8750, when the USB cable is unplugged while the system is in s2idle, the UCSI connector work for the change runs before the devices have resumed. It switches the role to
USB_ROLE_NONE, and the notifier accesses QSCRATCH while the glue clocks are still off, beforedwc3_qcom_pm_resume()turns them back on. The access does not complete and the SoC resets.pm_runtime_resume_and_get()does not prevent this:
runtime PM is disabled at that point and the device is still marked active, so it returns success without resuming anything.While the glue is suspended for system sleep, only record the VBUS override update and apply it from
dwc3_qcom_pm_resume()once the clocks are on. Apply it after the core has resumed, since resuming the gadget enables the override and the role switch is the more recent event. Serialize the notifier against the system suspend and resume callbacks with a mutex, so that a role switch can't run while the core or the glue is being suspended or resumed.
Quoted 3 lines
Fixes 21188e8d6d75 ("usb: dwc3: qcom: Implement glue callbacks to facilitate runtime suspend")Signed-off-by Vladyslav Tymoshchuk <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 24 lines · Vladyslav Tymoshchuk
dwc3_qcom_set_role_notifier()updates the VBUS override in QSCRATCH synchronously, from the context of theusb_role_switch_set_role()caller. The glue assumes that role switches only happen from a freezable workqueue, but that holds for the dwc3 core's mode switch, not for this notifier: the UCSI connector work, for one, runs on the non-freezable system workqueue.On SM8750, when the USB cable is unplugged while the system is in s2idle, the UCSI connector work for the change runs before the devices have resumed. It switches the role to
USB_ROLE_NONE, and the notifier accesses QSCRATCH while the glue clocks are still off, beforedwc3_qcom_pm_resume()turns them back on. The access does not complete and the SoC resets.pm_runtime_resume_and_get()does not prevent this:
runtime PM is disabled at that point and the device is still marked active, so it returns success without resuming anything.While the glue is suspended for system sleep, only record the VBUS override update and apply it from
dwc3_qcom_pm_resume()once the clocks are on. Apply it after the core has resumed, since resuming the gadget enables the override and the role switch is the more recent event. Serialize the notifier against the system suspend and resume callbacks with a mutex, so that a role switch can't run while the core or the glue is being suspended or resumed.
Quoted 3 lines
Fixes 21188e8d6d75 ("usb: dwc3: qcom: Implement glue callbacks to facilitate runtime suspend")Signed-off-by Vladyslav Tymoshchuk <[email protected]>
Regards,
Krishna,
Quoted 196 lines
---
Changes in v2:
- Holdrole_lockacross the whole ofdwc3_qcom_pm_suspend()anddwc3_qcom_pm_resume(). In v1 a role switch could still run duringdwc3_pm_suspend()ordwc3_pm_resume()and bypass the deferral, as the Sashiko review pointed out [4].
- Link to v1: https://lore.kernel.org/all/[email protected]/The review also asked about the
direct_completepath, which skips these callbacks. It is not taken for this driver: every qcom,snps-dwc3 node in the tree has a power domain, andgenpd_prepare()never returns 1. Where it is taken, the role switch is dropped as it is without this patch, and the hardware is not touched.Tested on SM8750 (OnePlus Pad 3; the board DT is not upstream yet), v7.3-rc5 plus board patches, with the UCSI connector work still on
system_wqand without thepmic_glinkRFC [5]. The test kernel also has localucsi_glinkandqcom_battmgrchanges that make a cable plug or unplug wake the system from s2idle, similar to Jishnu Prakash'sucsi_glinkpatch [3]:
- cable unplugged during s2idle: resumed in 2 of 2 runs; without this patch the SoC reset in all 6 earlier runs. The switch toUSB_ROLE_NONEcame 15 ms after the start of the resume, during the noirq phase, and was only recorded;dwc3_qcom_pm_resume()cleared the override about a second later.
- cable plugged in during s2idle, s2idle with the cable connected, and unplug/replug while awake: no change in behaviour.
- with a test-onlymsleep()underrole_lock, so that a role switch comes in while the lock is held:
- 3 s indwc3_qcom_pm_resume()afterdwc3_pm_resume(): the switch from a cable plug during s2idle waited for the lock and then set the override directly;
- 20 s indwc3_qcom_pm_suspend()beforedwc3_pm_suspend(): the switch from an unplug waited for the lock and was deferred. The unplug aborted the suspend, anddwc3_qcom_pm_resume()applied the update. No reset.I have not tested this patch without the wakeup changes.
The UCSI side is discussed in Abel Vesa's RFC to queue the connector work on
system_freezable_wq[1]. Since v1, I have also sent an RFC that holds back the PMIC GLINK notifications over system sleep [5]. With it, an unplug during s2idle on SM8750 no longer reaches this notifier. This patch is needed either way: the RFC does not hold back a role switch that is already in flight when the system suspend starts, and callers ofusb_role_switch_set_role()outsidepmic_glink, TCPM's kthread worker for one, are not frozen.Faisal Hassan's SA8255P series [2] also changes
dwc3_qcom_pm_suspend()anddwc3_qcom_pm_resume()(the wakeup argument becomes apm_message_t); whichever goes in second needs a small rebase.I used an AI assistant (Claude) for the analysis and to draft this patch and its description; I reviewed the result and ran the tests on my device.
[1] https://lore.kernel.org/all/20250205-ucsi-schedule-conn-worker-on-freezable-wq-v1-1-107d1356b77b@linaro.org/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/drivers/usb/dwc3/dwc3-qcom.c | 61 +++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 11 deletions(-)drivers/usb/dwc3/dwc3-qcom.c +50
-11@@ -10,6 +10,7 @@#include <linux/irq.h> #include <linux/of_clk.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/kernel.h> #include <linux/interconnect.h> #include <linux/platform_device.h> @@ -85,6 +86,11 @@ struct dwc3_qcom {struct icc_path *icc_path_apps;enum usb_role current_role; + +/* Serializes the role notifier against system suspend and resume */ +struct mutex role_lock; +bool vbus_override_pending; +bool vbus_override; };#define to_dwc3_qcom (d) container_of((d), struct dwc3_qcom, dwc)@@ -561,27 +567,40 @@ static int dwc3_qcom_setup_irq(struct dwc3_qcom *qcom, struct platform_device *pstatic void dwc3_qcom_set_role_notifier (struct dwc3 *dwc,enum usb_role next_role) {struct dwc3_qcom *qcom =to_dwc3_qcom (dwc); +bool enable; + +guard (mutex)(&qcom->role_lock);if (qcom->current_role == next_role)return ; + enable = qcom->current_role !=USB_ROLE_DEVICE ; + +/* +* Not every caller of usb_role_switch_set_role() is frozen during +* system sleep, e.g. the UCSI connector work can run in the noirq +* resume phase. The glue clocks are off then and runtime PM can't +* turn them on, so leave QSCRATCH alone and let +* dwc3_qcom_pm_resume() apply the update. +*/ +if (qcom->pm_suspended) { + qcom->vbus_override_pending = true; + qcom->vbus_override = enable; + qcom->current_role = next_role; +return ; + } +if (pm_runtime_resume_and_get (qcom->dev)) {dev_dbg (qcom->dev,"Failed to resume device\n" );return ; } -if (qcom->current_role ==USB_ROLE_DEVICE ) -dwc3_qcom_vbus_override_enable (qcom, false); -else if (qcom->current_role !=USB_ROLE_DEVICE ) -dwc3_qcom_vbus_override_enable (qcom, true); + qcom->vbus_override_pending = false; +dwc3_qcom_vbus_override_enable (qcom, enable);pm_runtime_mark_last_busy (qcom->dev);pm_runtime_put_sync (qcom->dev); -/* -* Current role changes via usb_role_switch_set_role callback protected -* internally by mutex lock. -*/ qcom->current_role = next_role; }@@ -624,6 +643,10 @@ static int dwc3_qcom_probe(struct platform_device *pdev) qcom->dev = &pdev->dev; + ret =devm_mutex_init (dev, &qcom->role_lock); +if (ret) +return ret; + qcom->resets =devm_reset_control_array_get_optional_exclusive (dev);if (IS_ERR (qcom->resets)) { return dev_err_probe(&pdev->dev, PTR_ERR(qcom->resets),@@ -752,6 +775,12 @@ static int dwc3_qcom_pm_suspend(struct device *dev)bool wakeup =device_may_wakeup (dev);int ret; +/* +* A role switch that comes in now waits until the glue is suspended +* and is then deferred, see dwc3_qcom_set_role_notifier(). +*/ +guard (mutex)(&qcom->role_lock); + ret =dwc3_pm_suspend (&qcom->dwc);if (ret)return ret;@@ -772,6 +801,8 @@ static int dwc3_qcom_pm_resume(struct device *dev)bool wakeup =device_may_wakeup (dev);int ret; +guard (mutex)(&qcom->role_lock); + ret =dwc3_qcom_resume (qcom, wakeup);if (ret)return ret;@@ -779,10 +810,18 @@ static int dwc3_qcom_pm_resume(struct device *dev) qcom->pm_suspended = false; ret =dwc3_pm_resume (&qcom->dwc); -if (ret) -return ret; -return 0 ; +/* +* Apply a VBUS override update from a role switch that came in while +* suspended. Do it after the core has resumed, as resuming the gadget +* enables the override and the role switch is the more recent event. +*/ +if (qcom->vbus_override_pending) { +dwc3_qcom_vbus_override_enable (qcom, qcom->vbus_override); + qcom->vbus_override_pending = false; + } + +return ret; }static void dwc3_qcom_complete (struct device *dev)base-commit:
a90ee4305c4a5df72c11b31dacfdc76e00fcf78a
Thinh Nguyen
maintainerQuoted 7 lines · Vladyslav Tymoshchuk
dwc3_qcom_set_role_notifier()updates the VBUS override in QSCRATCH synchronously, from the context of theusb_role_switch_set_role()caller. The glue assumes that role switches only happen from a freezable workqueue, but that holds for the dwc3 core's mode switch, not for this notifier: the UCSI connector work, for one, runs on the non-freezable system workqueue.
....
Good catch. However, this should be fixed in the dwc3 core. The pre_set_role notifier can be called from the freezable __dwc3_set_mode() work instead of from the role switch caller. Then glue drivers won't be notified while suspended. This is simpler and applies to all glue drivers.
Would you like to give this a try and send a v3?
Thanks,
Thinh
Quoted 9 lines · Thinh Nguyen
Quoted 7 lines · Vladyslav Tymoshchuk
dwc3_qcom_set_role_notifier()updates the VBUS override in QSCRATCH synchronously, from the context of theusb_role_switch_set_role()caller. The glue assumes that role switches only happen from a freezable workqueue, but that holds for the dwc3 core's mode switch, not for this notifier: the UCSI connector work, for one, runs on the non-freezable system workqueue.
Quoted 7 lines
....
Good catch. However, this should be fixed in the dwc3 core. The
pre_set_rolenotifier can be called from the freezable__dwc3_set_mode()work instead of from the role switch caller. Then glue drivers won't be notified while suspended. This is simpler and applies to all glue drivers.
I think the problem with that is, dwc3_set_mode doesn't know USB_ROLE (especially NONE mode).
It only knows the mode which is gadget/host/otg. Thats why vendor hooks are used in drd.c instead. Perhaps we can cache USB_ROLE into dwc3 struct and use it later on __dwc3_set_mode.
Regards,
Prashanth K
Thinh Nguyen
maintainerHi,
Quoted 20 lines · Prashanth K
Quoted 9 lines · Thinh Nguyen
Quoted 7 lines · Vladyslav Tymoshchuk
dwc3_qcom_set_role_notifier()updates the VBUS override in QSCRATCH synchronously, from the context of theusb_role_switch_set_role()caller. The glue assumes that role switches only happen from a freezable workqueue, but that holds for the dwc3 core's mode switch, not for this notifier: the UCSI connector work, for one, runs on the non-freezable system workqueue.Quoted 7 lines
....
Good catch. However, this should be fixed in the dwc3 core. The
pre_set_rolenotifier can be called from the freezable__dwc3_set_mode()work instead of from the role switch caller. Then glue drivers won't be notified while suspended. This is simpler and applies to all glue drivers.
Quoted 6 lines
I think the problem with that is,
dwc3_set_modedoesn't knowUSB_ROLE(especially NONE mode).
It only knows the mode which is gadget/host/otg. Thats why vendor hooks are used indrd.cinstead. Perhaps we can cacheUSB_ROLEinto dwc3 struct and use it later on__dwc3_set_mode.
Yes, the role needs to be cached. Something like the change below.
Hi Vladyslav,
Could you test this change instead to see if it resolves the issue?
Thanks,
Thinh
drivers/usb/dwc3/core.c +24 -1
@@ -169,15 +169,23 @@ static void __dwc3_set_mode(struct work_struct *work)int ret;u32 reg;u32 desired_dr_role; +enum usb_role requested_role; +bool notify_glue;int i;mutex_lock (&dwc->mutex);spin_lock_irqsave (&dwc->lock, flags); desired_dr_role = dwc->desired_dr_role; + requested_role = dwc->requested_role; + notify_glue = dwc->requested_role_pending; + dwc->requested_role_pending = false;spin_unlock_irqrestore (&dwc->lock, flags);pm_runtime_get_sync (dwc->dev); +if (notify_glue) +dwc3_pre_set_role (dwc, requested_role); +if (dwc->current_dr_role ==DWC3_GCTL_PRTCAP_OTG )dwc3_otg_update (dwc,0 );
@@ -287,7 +295,8 @@ static void __dwc3_set_mode(struct work_struct *work)mutex_unlock (&dwc->mutex); } -void dwc3_set_mode (struct dwc3 *dwc,u32 mode) +static void __dwc3_queue_mode (struct dwc3 *dwc,u32 mode,enum usb_role role, +bool notify_glue) {unsigned long flags;
@@ -296,11 +305,25 @@ void dwc3_set_mode(struct dwc3 *dwc, u32 mode)spin_lock_irqsave (&dwc->lock, flags); dwc->desired_dr_role = mode; +if (notify_glue) { + dwc->requested_role = role; + dwc->requested_role_pending = true; + }spin_unlock_irqrestore (&dwc->lock, flags);queue_work (system_freezable_wq, &dwc->drd_work); } +void dwc3_set_mode (struct dwc3 *dwc,u32 mode) +{ +__dwc3_queue_mode (dwc, mode,USB_ROLE_NONE , false); +} + +void dwc3_set_role (struct dwc3 *dwc,u32 mode,enum usb_role role) +{ +__dwc3_queue_mode (dwc, mode, role, true); +} +u32 dwc3_core_fifo_space (struct dwc3_ep *dep,u8 type) {struct dwc3 *dwc = dep->dwc;
drivers/usb/dwc3/core.h +5 -0
@@ -1058,6 +1058,8 @@ struct dwc3_glue_ops {* @dr_mode: requested mode of operation * @current_dr_role: current role of operation when in dual-role mode * @desired_dr_role: desired role of operation when in dual-role mode +* @requested_role: role requested +* @requested_role_pending: @requested_role is yet to be passed to the glue * @edev: extcon handle * @edev_nb: extcon notifier * @hsphy_mode: UTMI phy mode, one of following:
@@ -1261,6 +1263,8 @@ struct dwc3 {enum usb_dr_mode dr_mode;u32 current_dr_role;u32 desired_dr_role; +enum usb_role requested_role; +bool requested_role_pending;struct extcon_dev *edev;struct notifier_block edev_nb;enum usb_phy_interface hsphy_mode;
@@ -1614,6 +1618,7 @@ struct dwc3_gadget_ep_cmd_params {/* prototypes */ void dwc3_set_prtcap (struct dwc3 *dwc,u32 mode,bool ignore_susphy);void dwc3_set_mode (struct dwc3 *dwc,u32 mode); +void dwc3_set_role (struct dwc3 *dwc,u32 mode,enum usb_role role);u32 dwc3_core_fifo_space (struct dwc3_ep *dep,u8 type);#define DWC3_IP_IS (_ip) \
drivers/usb/dwc3/drd.c +2 -4
@@ -439,8 +439,7 @@ static int dwc3_drd_notifier(struct notifier_block *nb,enum usb_role role = mode ==DWC3_GCTL_PRTCAP_HOST ?USB_ROLE_HOST :USB_ROLE_DEVICE ; -dwc3_pre_set_role (dwc, role); -dwc3_set_mode (dwc, mode); +dwc3_set_role (dwc, mode, role);return NOTIFY_DONE ; }
@@ -468,8 +467,7 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw,break ; } -dwc3_pre_set_role (dwc, role); -dwc3_set_mode (dwc, mode); +dwc3_set_role (dwc, mode, role);return 0 ; }
Vladyslav Tymoshchuk
Tested-byQuoted 3 lines · Thinh Nguyen
Hi Vladyslav,
Could you test this change instead to see if it resolves the issue?
It does. OnePlus Pad 3 (SM8750, UCSI over pmic_glink), v7.3-rc5 plus board patches, with my dwc3-qcom v2 reverted and nothing else holding back pmic_glink notifications during suspend. I added debug prints to dwc3_usb_role_switch_set(), to __dwc3_set_mode() before the glue call, and to dwc3_qcom_set_role_notifier(), with the device's PM state.
The same kernel without your change resets the SoC when the cable is unplugged in s2idle. The last lines in the ramoops console:
PM: suspend-to-idle
role_switch_set ->0 (pm suspended 1 late 1 noirq 1) qcom notifier: role 2->0, vbus override 0 (pm suspended 1 noirq 1)
With your change there was no reset. Each suspend run was 150 s of s2idle ended by an RTC alarm, with the cable changed by hand during it:
unplug 2 runs plug in 1 run unplug, plug back in 1 run cable left plugged in 1 run
plus unplug and plug in while awake. In the suspend runs every role switch call came with dwc3 in the noirq phase and only stored the role. The glue was called once, with the last role, when the freezable workqueue was thawed:
PM: suspend-to-idle
role_switch_set ->0 (pm suspended 1 late 1 noirq 1)
PM: resume of devices complete after 108.388 msecs Restarting tasks: Starting
drd_work passes role 0 to the glue
qcom notifier: role 2->0, vbus override 0 (pm suspended 0 noirq 0) Restarting tasks: Done
Unplug and plug in during one suspend came down to a single DEVICE call, which dwc3-qcom skips since its role did not change. After every resume with the cable in, the NCM gadget was configured again within 5 s. While awake, the glue still gets NONE and DEVICE although the dr mode stays peripheral, so the call has to stay ahead of the early returns in __dwc3_set_mode(), where you put it.
Host mode, with a USB stick on the port instead of the cable: plugging and unplugging the stick while awake works as before, and so did two suspends with the stick left in. With the stick unplugged in s2idle (3 runs) the NONE call again came in noirq and was only stored, and drd_work passed it on after thaw:
PM: suspend-to-idle
role_switch_set ->0 (pm suspended 1 late 1 noirq 1)
PM: resume of devices complete after 2554.135 msecs Restarting tasks: Starting
drd_work passes role 0 to the glue
qcom notifier: role 1->0, vbus override 1 (pm suspended 0 noirq 0) xhci-hcd xhci-hcd.3.auto: remove, state 1
Restarting tasks: Done
The host-mode suspends were on a kernel that also keeps the USB GDSCs of gcc-sm8750 in retention. Without that the xHCI came back from s2idle with an SMMU fault and "HC died" in all three runs I tried, with the stick unplugged or left in. The dwc3 code was the same in both kernels, so that is a separate problem. I have no result for plugging the stick in during s2idle: the port did not detect the stick every time, also while awake, and I have not found out why yet.
Two comments go stale with this change: dwc3_qcom_set_role_notifier() says role changes are protected by the usb_role_switch mutex, while now they are serialized by dwc->mutex in drd_work, and the @pre_set_role kerneldoc could say that it is called from drd_work, so not during system sleep.
Will you post it, or would you like me to send it as v3 in place of my dwc3-qcom patch?
Thinh Nguyen
maintainerQuoted 69 lines · Vladyslav Tymoshchuk
Quoted 3 lines · Thinh Nguyen
Hi Vladyslav,
Could you test this change instead to see if it resolves the issue?
It does. OnePlus Pad 3 (SM8750, UCSI over
pmic_glink), v7.3-rc5 plus board patches, with my dwc3-qcom v2 reverted and nothing else holding backpmic_glinknotifications during suspend. I added debug prints todwc3_usb_role_switch_set(), to__dwc3_set_mode()before the glue call, and todwc3_qcom_set_role_notifier(), with the device's PM state.The same kernel without your change resets the SoC when the cable is unplugged in s2idle. The last lines in the ramoops console:
PM: suspend-to-idle
role_switch_set->0 (pm suspended 1 late 1 noirq 1) qcom notifier: role 2->0, vbus override 0 (pm suspended 1 noirq 1)With your change there was no reset. Each suspend run was 150 s of s2idle ended by an RTC alarm, with the cable changed by hand during it:
unplug 2 runs plug in 1 run unplug, plug back in 1 run cable left plugged in 1 runplus unplug and plug in while awake. In the suspend runs every role switch call came with dwc3 in the noirq phase and only stored the role. The glue was called once, with the last role, when the freezable workqueue was thawed:
PM: suspend-to-idle
role_switch_set->0 (pm suspended 1 late 1 noirq 1)
PM: resume of devices complete after 108.388 msecs Restarting tasks: Starting
drd_workpasses role 0 to the glue
qcom notifier: role 2->0, vbus override 0 (pm suspended 0 noirq 0) Restarting tasks: DoneUnplug and plug in during one suspend came down to a single DEVICE call, which dwc3-qcom skips since its role did not change. After every resume with the cable in, the NCM gadget was configured again within 5 s. While awake, the glue still gets NONE and DEVICE although the dr mode stays peripheral, so the call has to stay ahead of the early returns in
__dwc3_set_mode(), where you put it.Host mode, with a USB stick on the port instead of the cable: plugging and unplugging the stick while awake works as before, and so did two suspends with the stick left in. With the stick unplugged in s2idle (3 runs) the NONE call again came in noirq and was only stored, and
drd_workpassed it on after thaw:PM: suspend-to-idle
role_switch_set->0 (pm suspended 1 late 1 noirq 1)
PM: resume of devices complete after 2554.135 msecs Restarting tasks: Starting
drd_workpasses role 0 to the glue
qcom notifier: role 1->0, vbus override 1 (pm suspended 0 noirq 0) xhci-hcd xhci-hcd.3.auto: remove, state 1
Restarting tasks: DoneThe host-mode suspends were on a kernel that also keeps the USB GDSCs of gcc-sm8750 in retention. Without that the xHCI came back from s2idle with an SMMU fault and "HC died" in all three runs I tried, with the stick unplugged or left in. The dwc3 code was the same in both kernels, so that is a separate problem. I have no result for plugging the stick in during s2idle: the port did not detect the stick every time, also while awake, and I have not found out why yet.
Quoted 5 lines
Two comments go stale with this change:
dwc3_qcom_set_role_notifier()says role changes are protected by theusb_role_switchmutex, while now they are serialized bydwc->mutexindrd_work, and the @pre_set_role kerneldoc could say that it is called fromdrd_work, so not during system sleep.
Please update the comments.
Quoted 5 lines
Will you post it, or would you like me to send it as v3 in place of my dwc3-qcom patch?
Thanks for the details and testing. Can you send it as v3 with my Suggested-by tag?
Thanks,
Thinh