Commit 44bbcba5 authored by Greg Kroah-Hartman's avatar Greg Kroah-Hartman
Browse files

Merge patch series "Support system sleep with offloaded usb transfers" into usb-next

Guan-Yu Lin <guanyulin@google.com> says:

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.

Link: https://lore.kernel.org/r/20250911142051.90822-1-guanyulin@google.com


Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parents daf16134 38d627bc
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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_XHCI_SIDEBAND)	+= offload.o
usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
usbcore-$(CONFIG_ACPI)		+= usb-acpi.o

+44 −6
Original line number Diff line number Diff line
@@ -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,6 +1452,7 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
		}
	}
	if (status == 0) {
		if (!udev->offload_at_suspend)
			status = usb_suspend_device(udev, msg);

		/*
@@ -1480,11 +1498,13 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
	 */
	} else {
		udev->can_submit = 0;
		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]);
			}
		}
	}

 done:
	dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
@@ -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)
	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:
+136 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0

/*
 * offload.c - USB offload related functions
 *
 * Copyright (c) 2025, Google LLC.
 *
 * Author: Guan-Yu Lin
 */

#include <linux/usb.h>

#include "usb.h"

/**
 * usb_offload_get - increment the offload_usage of a USB device
 * @udev: the USB device to increment its offload_usage
 *
 * Incrementing the offload_usage of a usb_device indicates that offload is
 * enabled on this usb_device; that is, another entity is actively handling USB
 * transfers. This information allows the USB driver to adjust its power
 * management policy based on offload activity.
 *
 * Return: 0 on success. A negative error code otherwise.
 */
int usb_offload_get(struct usb_device *udev)
{
	int ret;

	usb_lock_device(udev);
	if (udev->state == USB_STATE_NOTATTACHED) {
		usb_unlock_device(udev);
		return -ENODEV;
	}

	if (udev->state == USB_STATE_SUSPENDED ||
		   udev->offload_at_suspend) {
		usb_unlock_device(udev);
		return -EBUSY;
	}

	/*
	 * offload_usage could only be modified when the device is active, since
	 * it will alter the suspend flow of the device.
	 */
	ret = usb_autoresume_device(udev);
	if (ret < 0) {
		usb_unlock_device(udev);
		return ret;
	}

	udev->offload_usage++;
	usb_autosuspend_device(udev);
	usb_unlock_device(udev);

	return ret;
}
EXPORT_SYMBOL_GPL(usb_offload_get);

/**
 * usb_offload_put - drop the offload_usage of a USB device
 * @udev: the USB device to drop its offload_usage
 *
 * The inverse operation of usb_offload_get, which drops the offload_usage of
 * a USB device. This information allows the USB driver to adjust its power
 * management policy based on offload activity.
 *
 * Return: 0 on success. A negative error code otherwise.
 */
int usb_offload_put(struct usb_device *udev)
{
	int ret;

	usb_lock_device(udev);
	if (udev->state == USB_STATE_NOTATTACHED) {
		usb_unlock_device(udev);
		return -ENODEV;
	}

	if (udev->state == USB_STATE_SUSPENDED ||
		   udev->offload_at_suspend) {
		usb_unlock_device(udev);
		return -EBUSY;
	}

	/*
	 * offload_usage could only be modified when the device is active, since
	 * it will alter the suspend flow of the device.
	 */
	ret = usb_autoresume_device(udev);
	if (ret < 0) {
		usb_unlock_device(udev);
		return ret;
	}

	/* Drop the count when it wasn't 0, ignore the operation otherwise. */
	if (udev->offload_usage)
		udev->offload_usage--;
	usb_autosuspend_device(udev);
	usb_unlock_device(udev);

	return ret;
}
EXPORT_SYMBOL_GPL(usb_offload_put);

/**
 * usb_offload_check - check offload activities on a USB device
 * @udev: the USB device to check its offload activity.
 *
 * Check if there are any offload activity on the USB device right now. This
 * information could be used for power management or other forms of resource
 * management.
 *
 * The caller must hold @udev's device lock. In addition, 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 offload activity, false otherwise.
 */
bool usb_offload_check(struct usb_device *udev) __must_hold(&udev->dev->mutex)
{
	struct usb_device *child;
	bool active;
	int port1;

	usb_hub_for_each_child(udev, port1, child) {
		usb_lock_device(child);
		active = usb_offload_check(child);
		usb_unlock_device(child);
		if (active)
			return true;
	}

	return !!udev->offload_usage;
}
EXPORT_SYMBOL_GPL(usb_offload_check);
+1 −0
Original line number Diff line number Diff line
@@ -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);
+38 −4
Original line number Diff line number Diff line
@@ -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"
@@ -454,7 +455,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 +483,25 @@ static int xhci_plat_suspend(struct device *dev)
	return 0;
}

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);
}

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);
@@ -525,6 +545,20 @@ 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);
}

static int xhci_plat_thaw(struct device *dev)
{
	return xhci_plat_resume_common(dev, false);
}
@@ -558,9 +592,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,
Loading