Skip to contentSkip to conversation

Support system sleep with offloaded usb transfers

PATCH Superseded Guan-Yu Lin 2 participants 13 messages Aug 1, 2025 – Sep 11, 2025

Subsystems (2)

v15 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v15 cover Support system sleep with offloaded usb transfers

Wesley Cheng and Mathias Nyman's USB offload design enables a co-processor to handle some USB transfers, potentially allowing the system to sleep (suspend-to-RAM) and save power. However, Linux's System Sleep model halts the USB host controller when the main system isn't managing any USB transfers. To address this, the proposal modifies the system to recognize offloaded USB transfers and manage power accordingly. This way, offloaded USB transfers could still happen during system sleep (Suspend-to-RAM).

This involves two key steps:
1. Transfer Status Tracking: Propose offload_usage and corresponding apis drivers could track USB transfers on the co-processor, ensuring the system is aware of any ongoing activity.
2. Power Management Adjustment: Modifications to the USB driver stack (xhci host controller driver, and USB device drivers) allow the system to sleep (Suspend-to-RAM) without disrupting co-processor managed USB transfers. This involves adding conditional checks to bypass some power management operations in the System Sleep model.

changelog
----------
Changes in v15:
- Rebase on TOT.

Changes in v14:
- Introduce CONFIG_USB_OFFLOAD for control usb offload related features.
- Specify __must_hold marco on usb_offload_check().
- Cosmetics changes on coding style.

Changes in v13:
- Ensure offload_usage is modified only when the device is neither suspended nor marked as "offload_at_suspend".
- Move lock manipulations into usb_offload_get()/usb_offload_put().
- Cosmetics changes on coding style.

Changes in v12:
- Rebase on TOT.
- Cosmetics changes on coding style.

Changes in v11:
- Use USB subsystem wrappers in usb_offload_get()/usb_offload_put().
- Refine logics and add comment in usb_suspend_both()/usb_resume_both().

Changes in v10:
- Remove unnecessary operations in dwc3 driver.
- Introduce CONFIG_USB_XHCI_SIDEBAND_SUSPEND to enable/disable offloaded usb transfers during system Suspend-to-RAM.
- Modify the approach to detect offloaded USB transfers when the system resumes from Suspend-to-RAM.
- Mark sideband activity when sideband interrupters are created/removed.
- Cosmetics changes on coding style.

Changes in v9:
- Remove unnecessary white space change.

Changes in v8:
- Change the runtime pm api to correct the error handling flow.
- Not flushing endpoints of actively offloaded USB devices to avoid possible USB transfer conflicts.

Changes in v7:
- Remove counting mechanism in struct usb_hcd. The USB device's offload status will be solely recorded in each related struct usb_device.
- Utilizes needs_remote_wakeup attribute in struct usb_interface to control the suspend flow of USB interfaces and associated USB endpoints. This addresses the need to support interrupt transfers generated by offloaded USB devices while the system is suspended.
- Block any offload_usage change during USB device suspend period.

Changes in v6:
- Fix build errors when CONFIG_USB_XHCI_SIDEBAND is disabled.
- Explicitly specify the data structure of the drvdata refereced in dwc3_suspend(), dwc3_resume().
- Move the initialization of counters to the patches introducing them.

Changes in v5:
- Walk through the USB children in usb_sideband_check() to determine the sideband activity under the specific USB device.
- Replace atomic_t by refcount_t.
- Reduce logs by using dev_dbg & remove __func__.

Changes in v4:
- Isolate the feature into USB driver stack.
- Integrate with series "Introduce QC USB SND audio offloading support"

Changes in v3:
- Integrate the feature with the pm core framework.

Changes in v2:
- Cosmetics changes on coding style.

[v3] PM / core: conditionally skip system pm in device/driver model [v2] usb: host: enable suspend-to-RAM control in userspace [v1] [RFC] usb: host: Allow userspace to control usb suspend flows
---

Guan-Yu Lin (4):
usb: xhci-plat: separate dev_pm_ops for each pm_event
usb: offload: add apis for offload usage tracking
xhci: sideband: add api to trace sideband usage
usb: host: enable USB offload during system sleep

 drivers/usb/core/Kconfig          |  10 +++
 drivers/usb/core/Makefile         |   1 +
 drivers/usb/core/driver.c         |  50 +++++++++--
 drivers/usb/core/offload.c        | 136 ++++++++++++++++++++++++++++++
 drivers/usb/core/usb.c            |   1 +
 drivers/usb/host/Kconfig          |  11 +++
 drivers/usb/host/xhci-plat.c      |  42 ++++++++-
 drivers/usb/host/xhci-plat.h      |   1 +
 drivers/usb/host/xhci-sideband.c  |  38 +++++++++
 include/linux/usb.h               |  18 ++++
 include/linux/usb/xhci-sideband.h |   9 ++
 11 files changed, 307 insertions(+), 10 deletions(-)
 create mode 100644 drivers/usb/core/offload.c
Signature · 1 line
2.50.1.565.gc32cd1483b-goog
#1 lore raw

PATCH v15 1/4 usb: xhci-plat: separate dev_pm_ops for each pm_event

Separate dev_pm_ops for different power events such as suspend, thaw, and hibernation. This is crucial when xhci-plat driver needs to adapt its behavior based on different power state changes.

---
 drivers/usb/host/xhci-plat.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
drivers/usb/host/xhci-plat.c +19 -4
index 5eb51797de32..df2e942ad5f7 100644
@@ -454,7 +454,7 @@ void xhci_plat_remove(struct platform_device *dev)
 }
 EXPORT_SYMBOL_GPL(xhci_plat_remove);
 
-static int xhci_plat_suspend(struct device *dev)
+static int xhci_plat_suspend_common(struct device *dev)
 {
 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
 	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
@@ -482,6 +482,16 @@ static int xhci_plat_suspend(struct device *dev)
 	return 0;
 }
 
+static int xhci_plat_suspend(struct device *dev)
+{
+	return xhci_plat_suspend_common(dev);
+}
+
+static int xhci_plat_freeze(struct device *dev)
+{
+	return xhci_plat_suspend_common(dev);
+}
+
 static int xhci_plat_resume_common(struct device *dev, bool power_lost)
 {
 	struct usb_hcd	*hcd = dev_get_drvdata(dev);
@@ -529,6 +539,11 @@ static int xhci_plat_resume(struct device *dev)
 	return xhci_plat_resume_common(dev, false);
 }
 
+static int xhci_plat_thaw(struct device *dev)
+{
+	return xhci_plat_resume_common(dev, false);
+}
+
 static int xhci_plat_restore(struct device *dev)
 {
 	return xhci_plat_resume_common(dev, true);
@@ -558,9 +573,9 @@ static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
 const struct dev_pm_ops xhci_plat_pm_ops = {
 	.suspend = pm_sleep_ptr(xhci_plat_suspend),
 	.resume = pm_sleep_ptr(xhci_plat_resume),
-	.freeze = pm_sleep_ptr(xhci_plat_suspend),
-	.thaw = pm_sleep_ptr(xhci_plat_resume),
-	.poweroff = pm_sleep_ptr(xhci_plat_suspend),
+	.freeze = pm_sleep_ptr(xhci_plat_freeze),
+	.thaw = pm_sleep_ptr(xhci_plat_thaw),
+	.poweroff = pm_sleep_ptr(xhci_plat_freeze),
 	.restore = pm_sleep_ptr(xhci_plat_restore),
 
 	SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
Signature · 1 line
2.50.1.565.gc32cd1483b-goog
#2 lore raw

PATCH v15 2/4 usb: offload: add apis for offload usage tracking

In reply to Guan-Yu Lin

Introduce offload_usage and corresponding apis to track offload usage on each USB device. Offload denotes that there is another co-processor accessing the USB device via the same USB host controller. To optimize power usage, it's essential to monitor whether the USB device is actively used by other co-processor. This information is vital when determining if a USB device can be safely suspended during system power state transitions.

---
 drivers/usb/core/Kconfig   |  10 +++
 drivers/usb/core/Makefile  |   1 +
 drivers/usb/core/offload.c | 136 +++++++++++++++++++++++++++++++++++++
 drivers/usb/core/usb.c     |   1 +
 include/linux/usb.h        |  18 +++++
 5 files changed, 166 insertions(+)
 create mode 100644 drivers/usb/core/offload.c
drivers/usb/core/Kconfig +10 -0
index 58e3ca7e4793..d5d38657f929 100644
@@ -143,3 +143,13 @@ config USB_DEFAULT_AUTHORIZATION_MODE
 	  ACPI selecting value 2 is analogous to selecting value 0.
 
 	  If unsure, keep the default value.
+
+config USB_OFFLOAD
+	bool "Enable USB offload feature"
+	depends on USB
+	depends on USB_XHCI_SIDEBAND_SUSPEND
+	help
+	  Offload denotes that there is another co-processor accessing the
+	  USB device via the same USB host controller, creating the
+	  "offloaded USB transfers". Say Y to allow offloaded USB
+	  transfers during system sleep (Suspend-to-RAM).
drivers/usb/core/Makefile +1 -0
index ac006abd13b3..df26c2885dd8 100644
@@ -9,6 +9,7 @@ usbcore-y += devio.o notify.o generic.o quirks.o devices.o
 usbcore-y += phy.o port.o
 
 usbcore-$(CONFIG_OF)		+= of.o
+usbcore-$(CONFIG_USB_OFFLOAD)		+= offload.o
 usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
 usbcore-$(CONFIG_ACPI)		+= usb-acpi.o
 
drivers/usb/core/offload.c new file +136 -0

136 lines not on this page · raw message

drivers/usb/core/usb.c +1 -0
index fca7735fc660..e6b9ab8b4a34 100644
@@ -670,6 +670,7 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
 	set_dev_node(&dev->dev, dev_to_node(bus->sysdev));
 	dev->state = USB_STATE_ATTACHED;
 	dev->lpm_disable_count = 1;
+	dev->offload_usage = 0;
 	atomic_set(&dev->urbnum, 0);
 
 	INIT_LIST_HEAD(&dev->ep0.urb_list);
include/linux/usb.h +18 -0
index 9d662c6abb4d..81fc0d93783f 100644
@@ -636,6 +636,8 @@ struct usb3_lpm_parameters {
  * @do_remote_wakeup:  remote wakeup should be enabled
  * @reset_resume: needs reset instead of resume
  * @port_is_suspended: the upstream port is suspended (L2 or U3)
+ * @offload_at_suspend: offload activities during suspend is enabled.
+ * @offload_usage: number of offload activities happening on this usb device.
  * @slot_id: Slot ID assigned by xHCI
  * @l1_params: best effor service latency for USB2 L1 LPM state, and L1 timeout.
  * @u1_params: exit latencies for USB3 U1 LPM state, and hub-initiated timeout.
@@ -724,6 +726,8 @@ struct usb_device {
 	unsigned do_remote_wakeup:1;
 	unsigned reset_resume:1;
 	unsigned port_is_suspended:1;
+	unsigned offload_at_suspend:1;
+	int offload_usage;
 	enum usb_link_tunnel_mode tunnel_mode;
 	struct device_link *usb4_link;
 
@@ -841,6 +845,20 @@ static inline void usb_mark_last_busy(struct usb_device *udev)
 { }
 #endif
 
+#if IS_ENABLED(CONFIG_USB_OFFLOAD)
+int usb_offload_get(struct usb_device *udev);
+int usb_offload_put(struct usb_device *udev);
+bool usb_offload_check(struct usb_device *udev);
+#else
+
+static inline int usb_offload_get(struct usb_device *udev)
+{ return 0; }
+static inline int usb_offload_put(struct usb_device *udev)
+{ return 0; }
+static inline bool usb_offload_check(struct usb_device *udev)
+{ return false; }
+#endif
+
 extern int usb_disable_lpm(struct usb_device *udev);
 extern void usb_enable_lpm(struct usb_device *udev);
 /* Same as above, but these functions lock/unlock the bandwidth_mutex. */
Signature · 1 line
2.50.1.565.gc32cd1483b-goog
maintainer
#3 lore raw

Re: [PATCH v15 2/4] usb: offload: add apis for offload usage tracking

Quoted 23 lines · Guan-Yu Lin

Introduce offload_usage and corresponding apis to track offload usage on each USB device. Offload denotes that there is another co-processor accessing the USB device via the same USB host controller. To optimize power usage, it's essential to monitor whether the USB device is actively used by other co-processor. This information is vital when determining if a USB device can be safely suspended during system power state transitions.

---
 drivers/usb/core/Kconfig   |  10 +++
 drivers/usb/core/Makefile  |   1 +
 drivers/usb/core/offload.c | 136 +++++++++++++++++++++++++++++++++++++
 drivers/usb/core/usb.c     |   1 +
 include/linux/usb.h        |  18 +++++
 5 files changed, 166 insertions(+)
 create mode 100644 drivers/usb/core/offload.c
drivers/usb/core/Kconfig +0 -0
index 58e3ca7e4793..d5d38657f929 100644

drivers/usb/core/Kconfig config USB_DEFAULT_AUTHORIZATION_MODE · line 148

Quoted 6 lines
drivers/usb/core/Kconfig +3 -0
@@ -143,3 +143,13 @@ config USB_DEFAULT_AUTHORIZATION_MODE
 	  ACPI selecting value 2 is analogous to selecting value 0.
 
 	  If unsure, keep the default value.
+
+config USB_OFFLOAD
+	bool "Enable USB offload feature"

I'm confused, we already have a "USB offload feature" that went into the last kernel release, why do we need a separate config option for this as well? Shouldn't this code only get built if the drivers that need it select it automatically? Forcing distros to configure this isn't generally a good idea if at all possible.

drivers/usb/core/Kconfig config USB_DEFAULT_AUTHORIZATION_MODE · line 155

Quoted 7 lines
drivers/usb/core/Kconfig +7 -0
+	depends on USB
+	depends on USB_XHCI_SIDEBAND_SUSPEND
+	help
+	  Offload denotes that there is another co-processor accessing the
+	  USB device via the same USB host controller, creating the
+	  "offloaded USB transfers". Say Y to allow offloaded USB
+	  transfers during system sleep (Suspend-to-RAM).

Especially because all "desktops" do not want this code selected, so having it in all distros feels like a waste to me.

thanks,

greg k-h

#4 lore raw

Re: [PATCH v15 2/4] usb: offload: add apis for offload usage tracking

Quoted 12 lines · Greg KH

drivers/usb/core/Kconfig config USB_DEFAULT_AUTHORIZATION_MODE · line 148

Quoted 3 lines · Guan-Yu Lin
drivers/usb/core/Kconfig +3 -0
+
+config USB_OFFLOAD
+     bool "Enable USB offload feature"

I'm confused, we already have a "USB offload feature" that went into the last kernel release, why do we need a separate config option for this as well? Shouldn't this code only get built if the drivers that need it select it automatically? Forcing distros to configure this isn't generally a good idea if at all possible.

Based on the discussion in v13, a new, separate USB configuration option is required to avoid core USB functions being enabled or disabled via an xhci-specific option. The USB offload feature involves a sideband entity that can access xhci resources, which, from the USB driver's viewpoint, means USB transfers are offloaded to this other entity. Therefore, the name "USB_OFFLOAD" was chosen to reflect this functionality.

Quoted 10 lines

drivers/usb/core/Kconfig config USB_DEFAULT_AUTHORIZATION_MODE · line 150

Quoted 3 lines
drivers/usb/core/Kconfig +2 -0
+     depends on USB
+     depends on USB_XHCI_SIDEBAND_SUSPEND

Especially because all "desktops" do not want this code selected, so having it in all distros feels like a waste to me.

thanks,

greg k-h

For the config keywords, we could automatically select USB_OFFLOAD once USB_XHCI_SIDEBAND_SUSPEND is selected to reduce configuration efforts.

Regards,
Guan-Yu

maintainer
#5 lore raw

Re: [PATCH v15 2/4] usb: offload: add apis for offload usage tracking

Quoted 14 lines · Guan-Yu Lin
Quoted 12 lines · Greg KH

drivers/usb/core/Kconfig config USB_DEFAULT_AUTHORIZATION_MODE · line 148

Quoted 3 lines · Guan-Yu Lin
drivers/usb/core/Kconfig +3 -0
+
+config USB_OFFLOAD
+     bool "Enable USB offload feature"

I'm confused, we already have a "USB offload feature" that went into the last kernel release, why do we need a separate config option for this as well? Shouldn't this code only get built if the drivers that need it select it automatically? Forcing distros to configure this isn't generally a good idea if at all possible.

Quoted 7 lines

Based on the discussion in v13, a new, separate USB configuration option is required to avoid core USB functions being enabled or disabled via an xhci-specific option. The USB offload feature involves a sideband entity that can access xhci resources, which, from the USB driver's viewpoint, means USB transfers are offloaded to this other entity. Therefore, the name "USB_OFFLOAD" was chosen to reflect this functionality.

Again, you are increasing the number of config options here, which does not make sense. Why would anyone only want a subset, and not just the whole thing?

Yes, USB_OFFLOAD only works today on xhci, and that's fine, so let's just keep it that way.

Quoted 6 lines
Quoted 6 lines

drivers/usb/core/Kconfig config USB_DEFAULT_AUTHORIZATION_MODE · line 150

Quoted 2 lines
drivers/usb/core/Kconfig +2 -0
+     depends on USB
+     depends on USB_XHCI_SIDEBAND_SUSPEND

Especially because all "desktops" do not want this code selected, so having it in all distros feels like a waste to me.

Quoted 7 lines
Quoted 3 lines

thanks,

greg k-h

For the config keywords, we could automatically select USB_OFFLOAD once USB_XHCI_SIDEBAND_SUSPEND is selected to reduce configuration efforts.

select is a nightmare to maintain and understand. Please just reduce configuration efforts by not adding new options at all :)

thanks,

greg k-h

#6 lore raw

Re: [PATCH v15 2/4] usb: offload: add apis for offload usage tracking

Quoted 45 lines · Greg KH
Quoted 14 lines · Guan-Yu Lin
Quoted 12 lines · Greg KH

drivers/usb/core/Kconfig config USB_DEFAULT_AUTHORIZATION_MODE · line 148

Quoted 3 lines · Guan-Yu Lin
drivers/usb/core/Kconfig +3 -0
+
+config USB_OFFLOAD
+     bool "Enable USB offload feature"

I'm confused, we already have a "USB offload feature" that went into the last kernel release, why do we need a separate config option for this as well? Shouldn't this code only get built if the drivers that need it select it automatically? Forcing distros to configure this isn't generally a good idea if at all possible.

Quoted 7 lines

Based on the discussion in v13, a new, separate USB configuration option is required to avoid core USB functions being enabled or disabled via an xhci-specific option. The USB offload feature involves a sideband entity that can access xhci resources, which, from the USB driver's viewpoint, means USB transfers are offloaded to this other entity. Therefore, the name "USB_OFFLOAD" was chosen to reflect this functionality.

Again, you are increasing the number of config options here, which does not make sense. Why would anyone only want a subset, and not just the whole thing?

Yes, USB_OFFLOAD only works today on xhci, and that's fine, so let's just keep it that way.

Quoted 6 lines
Quoted 6 lines

drivers/usb/core/Kconfig config USB_DEFAULT_AUTHORIZATION_MODE · line 150

Quoted 2 lines
drivers/usb/core/Kconfig +2 -0
+     depends on USB
+     depends on USB_XHCI_SIDEBAND_SUSPEND

Especially because all "desktops" do not want this code selected, so having it in all distros feels like a waste to me.

Quoted 7 lines
Quoted 3 lines

thanks,

greg k-h

For the config keywords, we could automatically select USB_OFFLOAD once USB_XHCI_SIDEBAND_SUSPEND is selected to reduce configuration efforts.

Quoted 6 lines

select is a nightmare to maintain and understand. Please just reduce configuration efforts by not adding new options at all :)

thanks,

greg k-h

Thanks for the suggestions. I'll use only USB_XHCI_SIDEBAND_SUSPEND to control all the features, including the core USB functions.

Regards,
Guan-Yu

#7 lore raw

PATCH v15 3/4 xhci: sideband: add api to trace sideband usage

In reply to Guan-Yu Lin

The existing sideband driver only registers sidebands without tracking their active usage. To address this, sideband will now record its active usage when it creates/removes interrupters. In addition, a new api is introduced to provide a means for other dirvers to fetch sideband activity information on a USB host controller.

---
 drivers/usb/host/Kconfig          | 11 +++++++++
 drivers/usb/host/xhci-sideband.c  | 38 +++++++++++++++++++++++++++++++
 include/linux/usb/xhci-sideband.h |  9 ++++++++
 3 files changed, 58 insertions(+)
drivers/usb/host/Kconfig +11 -0
index 109100cc77a3..49b9cdc11298 100644
@@ -113,6 +113,17 @@ config USB_XHCI_SIDEBAND
 	  xHCI USB endpoints directly, allowing CPU to sleep while playing
 	  audio.
 
+config USB_XHCI_SIDEBAND_SUSPEND
+	  bool "xHCI support for sideband during system suspend"
+	  depends on USB_XHCI_SIDEBAND
+	  depends on USB_XHCI_PLATFORM
+	  depends on SUSPEND
+	  help
+	    Say 'Y' to enable the support for the xHCI sideband capability
+	    after system suspended. In addition to USB_XHCI_SIDEBAND, this
+	    config allows endpoints and interrupters associated with the
+	    sideband function when system is suspended.
+
 config USB_XHCI_TEGRA
 	tristate "xHCI support for NVIDIA Tegra SoCs"
 	depends on PHY_TEGRA_XUSB
drivers/usb/host/xhci-sideband.c +38 -0
index d49f9886dd84..15d72c23c0e0 100644
@@ -266,6 +266,33 @@ xhci_sideband_get_event_buffer(struct xhci_sideband *sb)
 }
 EXPORT_SYMBOL_GPL(xhci_sideband_get_event_buffer);
 
+#if IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND_SUSPEND)
+/**
+ * xhci_sideband_check - check the existence of active sidebands
+ * @hcd: the host controller driver associated with the target host controller
+ *
+ * Allow other drivers, such as usb controller driver, to check if there are
+ * any sideband activity on the host controller. This information could be used
+ * for power management or other forms of resource management. The caller should
+ * ensure downstream usb devices are all either suspended or marked as
+ * "offload_at_suspend" to ensure the correctness of the return value.
+ *
+ * Returns true on any active sideband existence, false otherwise.
+ */
+bool xhci_sideband_check(struct usb_hcd *hcd)
+{
+	struct usb_device *udev = hcd->self.root_hub;
+	bool active;
+
+	usb_lock_device(udev);
+	active = usb_offload_check(udev);
+	usb_unlock_device(udev);
+
+	return active;
+}
+EXPORT_SYMBOL_GPL(xhci_sideband_check);
+#endif /* IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND_SUSPEND) */
+
 /**
  * xhci_sideband_create_interrupter - creates a new interrupter for this sideband
  * @sb: sideband instance for this usb device
@@ -286,6 +313,7 @@ xhci_sideband_create_interrupter(struct xhci_sideband *sb, int num_seg,
 				 bool ip_autoclear, u32 imod_interval, int intr_num)
 {
 	int ret = 0;
+	struct usb_device *udev;
 
 	if (!sb || !sb->xhci)
 		return -ENODEV;
@@ -304,6 +332,9 @@ xhci_sideband_create_interrupter(struct xhci_sideband *sb, int num_seg,
 		goto out;
 	}
 
+	udev = sb->vdev->udev;
+	ret = usb_offload_get(udev);
+
 	sb->ir->ip_autoclear = ip_autoclear;
 
 out:
@@ -323,6 +354,8 @@ EXPORT_SYMBOL_GPL(xhci_sideband_create_interrupter);
 void
 xhci_sideband_remove_interrupter(struct xhci_sideband *sb)
 {
+	struct usb_device *udev;
+
 	if (!sb || !sb->ir)
 		return;
 
@@ -330,6 +363,11 @@ xhci_sideband_remove_interrupter(struct xhci_sideband *sb)
 	xhci_remove_secondary_interrupter(xhci_to_hcd(sb->xhci), sb->ir);
 
 	sb->ir = NULL;
+	udev = sb->vdev->udev;
+
+	if (udev->state != USB_STATE_NOTATTACHED)
+		usb_offload_put(udev);
+
 	mutex_unlock(&sb->mutex);
 }
 EXPORT_SYMBOL_GPL(xhci_sideband_remove_interrupter);
include/linux/usb/xhci-sideband.h +9 -0
index 45288c392f6e..5174cc5afc98 100644
@@ -11,6 +11,7 @@
 
 #include <linux/scatterlist.h>
 #include <linux/usb.h>
+#include <linux/usb/hcd.h>
 
 #define	EP_CTX_PER_DEV		31	/* FIXME defined twice, from xhci.h */
 
@@ -83,6 +84,14 @@ xhci_sideband_get_endpoint_buffer(struct xhci_sideband *sb,
 				  struct usb_host_endpoint *host_ep);
 struct sg_table *
 xhci_sideband_get_event_buffer(struct xhci_sideband *sb);
+
+#if IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND_SUSPEND)
+bool xhci_sideband_check(struct usb_hcd *hcd);
+#else
+static inline bool xhci_sideband_check(struct usb_hcd *hcd)
+{ return false; }
+#endif /* IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND_SUSPEND) */
+
 int
 xhci_sideband_create_interrupter(struct xhci_sideband *sb, int num_seg,
 				 bool ip_autoclear, u32 imod_interval, int intr_num);
Signature · 1 line
2.50.1.565.gc32cd1483b-goog
maintainer
#8 lore raw

Re: [PATCH v15 3/4] xhci: sideband: add api to trace sideband usage

Quoted 21 lines · Guan-Yu Lin

The existing sideband driver only registers sidebands without tracking their active usage. To address this, sideband will now record its active usage when it creates/removes interrupters. In addition, a new api is introduced to provide a means for other dirvers to fetch sideband activity information on a USB host controller.

---
 drivers/usb/host/Kconfig          | 11 +++++++++
 drivers/usb/host/xhci-sideband.c  | 38 +++++++++++++++++++++++++++++++
 include/linux/usb/xhci-sideband.h |  9 ++++++++
 3 files changed, 58 insertions(+)
drivers/usb/host/Kconfig +0 -0
index 109100cc77a3..49b9cdc11298 100644
@@ -113,6 +113,17 @@ config USB_XHCI_SIDEBAND
 	  xHCI USB endpoints directly, allowing CPU to sleep while playing
 	  audio.
 

drivers/usb/host/Kconfig config USB_XHCI_SIDEBAND · line 125

Quoted 10 lines
drivers/usb/host/Kconfig +10 -0
+config USB_XHCI_SIDEBAND_SUSPEND
+	  bool "xHCI support for sideband during system suspend"
+	  depends on USB_XHCI_SIDEBAND
+	  depends on USB_XHCI_PLATFORM
+	  depends on SUSPEND
+	  help
+	    Say 'Y' to enable the support for the xHCI sideband capability
+	    after system suspended. In addition to USB_XHCI_SIDEBAND, this
+	    config allows endpoints and interrupters associated with the
+	    sideband function when system is suspended.

Again, why is a new config option needed here? Why can't we just use the existing one? Why would you ever want one and not the other?

Quoted 28 lines
drivers/usb/host/Kconfig +1 -0
+
 config USB_XHCI_TEGRA
 	tristate "xHCI support for NVIDIA Tegra SoCs"
 	depends on PHY_TEGRA_XUSB
drivers/usb/host/xhci-sideband.c +16 -0
index d49f9886dd84..15d72c23c0e0 100644
@@ -266,6 +266,33 @@ xhci_sideband_get_event_buffer(struct xhci_sideband *sb)
 }
 EXPORT_SYMBOL_GPL(xhci_sideband_get_event_buffer);
 
+#if IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND_SUSPEND)
+/**
+ * xhci_sideband_check - check the existence of active sidebands
+ * @hcd: the host controller driver associated with the target host controller
+ *
+ * Allow other drivers, such as usb controller driver, to check if there are
+ * any sideband activity on the host controller. This information could be used
+ * for power management or other forms of resource management. The caller should
+ * ensure downstream usb devices are all either suspended or marked as
+ * "offload_at_suspend" to ensure the correctness of the return value.
+ *
+ * Returns true on any active sideband existence, false otherwise.
+ */
+bool xhci_sideband_check(struct usb_hcd *hcd)
+{
+	struct usb_device *udev = hcd->self.root_hub;

drivers/usb/host/xhci-sideband.c xhci_sideband_get_event_buffer(struct xhci_sideband *sb) · line 294

Quoted 10 lines
drivers/usb/host/xhci-sideband.c +10 -0
+	bool active;
+
+	usb_lock_device(udev);
+	active = usb_offload_check(udev);
+	usb_unlock_device(udev);
+
+	return active;
+}
+EXPORT_SYMBOL_GPL(xhci_sideband_check);
+#endif /* IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND_SUSPEND) */

#ifdef in .c files is generally not a good idea, is it really needed here? Maybe it is, it's hard to unwind...

thanks,

greg k-h

#9 lore raw

Re: [PATCH v15 3/4] xhci: sideband: add api to trace sideband usage

Quoted 7 lines · Greg KH
Quoted 1 line · Guan-Yu Lin

+config USB_XHCI_SIDEBAND_SUSPEND

Again, why is a new config option needed here? Why can't we just use the existing one? Why would you ever want one and not the other?

USB_XHCI_SIDEBAND focuses on the normal use case where the system is active, while USB_XHCI_SIDEBAND_SUSPEND enables the sideband during system sleep (Suspend-to-RAM). The design allows the user to determine the degree of support for the sideband in the system. We could add the "select" keyword to automatically enable USB_XHCI_SIDEBAND once USB_XHCI_SIDEBAND_SUSPEND is enabled.

Quoted 10 lines

drivers/usb/host/xhci-sideband.c xhci_sideband_get_event_buffer(struct xhci_sideband *sb) · line 294

Quoted 3 lines
drivers/usb/host/xhci-sideband.c +2 -0
+EXPORT_SYMBOL_GPL(xhci_sideband_check);
+#endif /* IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND_SUSPEND) */

#ifdef in .c files is generally not a good idea, is it really needed here? Maybe it is, it's hard to unwind...

thanks,

greg k-h

Given that CONFIG_USB_XHCI_SIDEBAND_SUSPEND depends on CONFIG_USB_XHCI_SIDEBAND and adds only a single function, I think it is preferable to place the new code in the same file. This approach prevents unnecessary code fragmentation and improves maintainability by keeping related functions together.

Regards,
Guan-Yu

maintainer
#10 lore raw

Re: [PATCH v15 3/4] xhci: sideband: add api to trace sideband usage

Quoted 8 lines · Guan-Yu Lin
Quoted 7 lines · Greg KH
Quoted 1 line · Guan-Yu Lin

+config USB_XHCI_SIDEBAND_SUSPEND

Again, why is a new config option needed here? Why can't we just use the existing one? Why would you ever want one and not the other?

Quoted 6 lines

USB_XHCI_SIDEBAND focuses on the normal use case where the system is active, while USB_XHCI_SIDEBAND_SUSPEND enables the sideband during system sleep (Suspend-to-RAM). The design allows the user to determine the degree of support for the sideband in the system. We could add the "select" keyword to automatically enable USB_XHCI_SIDEBAND once USB_XHCI_SIDEBAND_SUSPEND is enabled.

But why would you want only one of these options and not both? The whole goal of this feature is for power savings, which means that suspend is needed by everyone. Don't increase the config variable combinations for no good reason.

Quoted 8 lines
Quoted 8 lines

drivers/usb/host/xhci-sideband.c xhci_sideband_get_event_buffer(struct xhci_sideband *sb) · line 294

Quoted 2 lines
drivers/usb/host/xhci-sideband.c +2 -0
+EXPORT_SYMBOL_GPL(xhci_sideband_check);
+#endif /* IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND_SUSPEND) */

#ifdef in .c files is generally not a good idea, is it really needed here? Maybe it is, it's hard to unwind...

thanks,

Quoted 7 lines
Quoted 1 line

greg k-h

Given that CONFIG_USB_XHCI_SIDEBAND_SUSPEND depends on CONFIG_USB_XHCI_SIDEBAND and adds only a single function, I think it is preferable to place the new code in the same file. This approach prevents unnecessary code fragmentation and improves maintainability by keeping related functions together.

We put #ifdefs in .h files. That's a long-time-rule for decades to ensure that we can maintain this codebase for even more decades to come. Please do not break that rule just to keep things close if it's not required.

thanks,

greg k-h

#11 lore raw

Re: [PATCH v15 3/4] xhci: sideband: add api to trace sideband usage

Quoted 17 lines · Greg KH
Quoted 8 lines · Guan-Yu Lin
Quoted 7 lines · Greg KH
Quoted 1 line · Guan-Yu Lin

+config USB_XHCI_SIDEBAND_SUSPEND

Again, why is a new config option needed here? Why can't we just use the existing one? Why would you ever want one and not the other?

Quoted 6 lines

USB_XHCI_SIDEBAND focuses on the normal use case where the system is active, while USB_XHCI_SIDEBAND_SUSPEND enables the sideband during system sleep (Suspend-to-RAM). The design allows the user to determine the degree of support for the sideband in the system. We could add the "select" keyword to automatically enable USB_XHCI_SIDEBAND once USB_XHCI_SIDEBAND_SUSPEND is enabled.

Quoted 5 lines

But why would you want only one of these options and not both? The whole goal of this feature is for power savings, which means that suspend is needed by everyone. Don't increase the config variable combinations for no good reason.

Thanks for the suggestions. I'll remove USB_XHCI_SIDEBAND_SUSPEND in the next version.

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines

drivers/usb/host/xhci-sideband.c xhci_sideband_get_event_buffer(struct xhci_sideband *sb) · line 294

Quoted 2 lines
drivers/usb/host/xhci-sideband.c +2 -0
+EXPORT_SYMBOL_GPL(xhci_sideband_check);
+#endif /* IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND_SUSPEND) */

#ifdef in .c files is generally not a good idea, is it really needed here? Maybe it is, it's hard to unwind...

thanks,

Quoted 7 lines
Quoted 1 line

greg k-h

Given that CONFIG_USB_XHCI_SIDEBAND_SUSPEND depends on CONFIG_USB_XHCI_SIDEBAND and adds only a single function, I think it is preferable to place the new code in the same file. This approach prevents unnecessary code fragmentation and improves maintainability by keeping related functions together.

Quoted 8 lines

We put #ifdefs in .h files. That's a long-time-rule for decades to ensure that we can maintain this codebase for even more decades to come. Please do not break that rule just to keep things close if it's not required.

thanks,

greg k-h

Thanks for the suggestions. This concern would be addressed once we use only CONFIG_USB_XHCI_SIDEBAND to control all features.

Regards,
Guan-Yu

#12 lore raw

PATCH v15 4/4 usb: host: enable USB offload during system sleep

In reply to Guan-Yu Lin

Sharing a USB controller with another entity via xhci-sideband driver creates power management complexities. To prevent the USB controller from being inadvertently deactivated while in use by the other entity, a usage-count based mechanism is implemented. This allows the system to manage power effectively, ensuring the controller remains available whenever needed.
In order to maintain full functionality of an offloaded USB devices, several changes are made within the suspend flow of such devices:
- skip usb_suspend_device() so that the port/hub are still active for USB transfers via offloaded path.
- not suspending the endpoints which are used by USB interfaces marked with needs_remote_wakeup. Namely, skip usb_suspend_interface() and usb_hcd_flush_endpoint() on associated USB interfaces. This reserves a pending interrupt urb during system suspend for handling the interrupt transfer, which is necessary since remote wakeup doesn't apply in the offloaded USB devices when controller is still active.
- not flushing the endpoints of actively offloaded USB devices. Given that the USB devices is used by another entity, unilaterally flush the endpoint might lead to unexpected behavior on another entity.
- not suspending the xhci controller. This is done by skipping the suspend/resume callbacks in the xhci platform driver.

---
 drivers/usb/core/driver.c    | 50 +++++++++++++++++++++++++++++++-----
 drivers/usb/host/xhci-plat.c | 19 ++++++++++++++
 drivers/usb/host/xhci-plat.h |  1 +
 3 files changed, 64 insertions(+), 6 deletions(-)
drivers/usb/core/driver.c +44 -6
index c3177034b779..275c0fd573d4 100644
@@ -1420,11 +1420,28 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
 			udev->state == USB_STATE_SUSPENDED)
 		goto done;
 
+	if (msg.event == PM_EVENT_SUSPEND && usb_offload_check(udev)) {
+		dev_dbg(&udev->dev, "device offloaded, skip suspend.\n");
+		udev->offload_at_suspend = 1;
+	}
+
 	/* Suspend all the interfaces and then udev itself */
 	if (udev->actconfig) {
 		n = udev->actconfig->desc.bNumInterfaces;
 		for (i = n - 1; i >= 0; --i) {
 			intf = udev->actconfig->interface[i];
+			/*
+			 * Don't suspend interfaces with remote wakeup while
+			 * the controller is active. This preserves pending
+			 * interrupt urbs, allowing interrupt events to be
+			 * handled during system suspend.
+			 */
+			if (udev->offload_at_suspend &&
+			    intf->needs_remote_wakeup) {
+				dev_dbg(&intf->dev,
+					"device offloaded, skip suspend.\n");
+				continue;
+			}
 			status = usb_suspend_interface(udev, intf, msg);
 
 			/* Ignore errors during system sleep transitions */
@@ -1435,7 +1452,8 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
 		}
 	}
 	if (status == 0) {
-		status = usb_suspend_device(udev, msg);
+		if (!udev->offload_at_suspend)
+			status = usb_suspend_device(udev, msg);
 
 		/*
 		 * Ignore errors from non-root-hub devices during
@@ -1480,9 +1498,11 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
 	 */
 	} else {
 		udev->can_submit = 0;
-		for (i = 0; i < 16; ++i) {
-			usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
-			usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
+		if (!udev->offload_at_suspend) {
+			for (i = 0; i < 16; ++i) {
+				usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
+				usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
+			}
 		}
 	}
 
@@ -1524,17 +1544,35 @@ static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
 	udev->can_submit = 1;
 
 	/* Resume the device */
-	if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
-		status = usb_resume_device(udev, msg);
+	if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume) {
+		if (!udev->offload_at_suspend)
+			status = usb_resume_device(udev, msg);
+		else
+			dev_dbg(&udev->dev,
+				"device offloaded, skip resume.\n");
+	}
 
 	/* Resume the interfaces */
 	if (status == 0 && udev->actconfig) {
 		for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
 			intf = udev->actconfig->interface[i];
+			/*
+			 * Interfaces with remote wakeup aren't suspended
+			 * while the controller is active. This preserves
+			 * pending interrupt urbs, allowing interrupt events
+			 * to be handled during system suspend.
+			 */
+			if (udev->offload_at_suspend &&
+			    intf->needs_remote_wakeup) {
+				dev_dbg(&intf->dev,
+					"device offloaded, skip resume.\n");
+				continue;
+			}
 			usb_resume_interface(udev, intf, msg,
 					udev->reset_resume);
 		}
 	}
+	udev->offload_at_suspend = 0;
 	usb_mark_last_busy(udev);
 
  done:
drivers/usb/host/xhci-plat.c +19 -0
index df2e942ad5f7..3a56d8f94519 100644
@@ -20,6 +20,7 @@
 #include <linux/acpi.h>
 #include <linux/usb/of.h>
 #include <linux/reset.h>
+#include <linux/usb/xhci-sideband.h>
 
 #include "xhci.h"
 #include "xhci-plat.h"
@@ -484,6 +485,15 @@ static int xhci_plat_suspend_common(struct device *dev)
 
 static int xhci_plat_suspend(struct device *dev)
 {
+	struct usb_hcd	*hcd = dev_get_drvdata(dev);
+	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
+
+	if (xhci_sideband_check(hcd)) {
+		priv->sideband_at_suspend = 1;
+		dev_dbg(dev, "sideband instance active, skip suspend.\n");
+		return 0;
+	}
+
 	return xhci_plat_suspend_common(dev);
 }
 
@@ -536,6 +546,15 @@ static int xhci_plat_resume_common(struct device *dev, bool power_lost)
 
 static int xhci_plat_resume(struct device *dev)
 {
+	struct usb_hcd	*hcd = dev_get_drvdata(dev);
+	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
+
+	if (priv->sideband_at_suspend) {
+		priv->sideband_at_suspend = 0;
+		dev_dbg(dev, "sideband instance active, skip resume.\n");
+		return 0;
+	}
+
 	return xhci_plat_resume_common(dev, false);
 }
 
drivers/usb/host/xhci-plat.h +1 -0
index fe4f95e690fa..2b32a93d2b76 100644
@@ -16,6 +16,7 @@ struct xhci_plat_priv {
 	const char *firmware_name;
 	unsigned long long quirks;
 	bool power_lost;
+	unsigned sideband_at_suspend:1;
 	void (*plat_start)(struct usb_hcd *);
 	int (*init_quirk)(struct usb_hcd *);
 	int (*suspend_quirk)(struct usb_hcd *);
Signature · 1 line
2.50.1.565.gc32cd1483b-goog