Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 136 additions & 31 deletions drivers/usb/dwc3/dwc3-apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <linux/spinlock.h>
#include <linux/suspend.h>
#include <linux/usb.h>
#include <linux/usb/gadget.h>
#include <linux/usb/hcd.h>
#include <linux/usb/phy.h>
#include <linux/workqueue.h>
Expand Down Expand Up @@ -230,6 +231,8 @@ struct dwc3_apple {
bool sleep_pm_online;
bool sleep_role_pending;
enum dwc3_apple_state sleep_role;
/* A device role left its host for a suspend-to-idle and connects again after it. */
bool sleep_gadget_off;
struct work_struct sleep_stop_work;
struct completion sleep_stop_done;
int sleep_stop_ret;
Expand Down Expand Up @@ -1536,6 +1539,8 @@ static int dwc3_apple_exit(struct dwc3_apple *appledwc)
case DWC3_APPLE_NO_CABLE:
break;
case DWC3_APPLE_DEVICE:
/* A gadget set up again connects when its function driver binds. */
appledwc->sleep_gadget_off = false;
dwc3_gadget_exit(&appledwc->dwc);
appledwc->state = DWC3_APPLE_NO_CABLE;
break;
Expand Down Expand Up @@ -1875,9 +1880,64 @@ static int dwc3_apple_sleep_kept(struct dwc3_apple *appledwc, bool *kept)
/* The notifier never waits. This existing bound belongs after the chain and freezer. */
#define DWC3_APPLE_SLEEP_STOP_PREPARE_MS 5000

/*
* A device role stays started through suspend-to-idle, but with the system frozen nothing
* services its endpoints: the host's OUT transfers pile up behind it and the link need not
* recover after the wake. Leave the bus for the sleep, as dwc3's own system suspend does
* (dwc3_gadget_suspend()), and connect again after it. A deep sleep stops the controller.
* The gadget stays registered meanwhile: role changes are deferred from the moment a sleep
* is admitted until its start work has connected the gadget again and published IDLE, and
* the teardown paths cancel both works before they exit the role.
*/
static struct usb_gadget *dwc3_apple_sleep_gadget(struct dwc3_apple *appledwc)
{
lockdep_assert_held(&appledwc->lock);
if (!IS_ENABLED(CONFIG_USB_DWC3_GADGET) && !IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE))
return NULL;
if (appledwc->state != DWC3_APPLE_DEVICE || !appledwc->core_active ||
!appledwc->dwc.gadget_driver)
return NULL;
return appledwc->dwc.gadget;
}

/*
* Called without the glue lock: dwc3_gadget_exit() takes the gadget's device lock under it.
* The PM core prepares the gadget, a child of the controller, only after the controller's
* prepare has waited for this work, so its device lock is not held by the sleep here.
*/
static void dwc3_apple_sleep_gadget_off(struct dwc3_apple *appledwc, struct usb_gadget *gadget)
{
bool off = false;

/* Soft-connect writes and function driver (un)binding hold the gadget's device lock. */
device_lock(&gadget->dev);
if (gadget->connected)
off = !usb_gadget_disconnect(gadget);
device_unlock(&gadget->dev);
if (!off)
return;
scoped_guard(mutex, &appledwc->lock)
appledwc->sleep_gadget_off = true;
dev_dbg(appledwc->dev, "USB device off its host for suspend-to-idle\n");
}

/* The gadget this sleep took off its host, if the role it ran in is still there. */
static struct usb_gadget *dwc3_apple_sleep_gadget_back(struct dwc3_apple *appledwc)
{
lockdep_assert_held(&appledwc->lock);
if (!appledwc->sleep_gadget_off)
return NULL;
appledwc->sleep_gadget_off = false;
if (appledwc->shutting_down || appledwc->state != DWC3_APPLE_DEVICE ||
!appledwc->core_active || !appledwc->dwc.gadget_driver)
return NULL;
return appledwc->dwc.gadget;
}

static int dwc3_apple_sleep_prepare_work(struct dwc3_apple *appledwc)
{
bool in_place;
struct usb_gadget *gadget = NULL;
bool in_place = false, s2idle;
int ret;

/* A restart left to the work after the last router sleep ends first. */
Expand All @@ -1894,12 +1954,22 @@ static int dwc3_apple_sleep_prepare_work(struct dwc3_apple *appledwc)
ret = dwc3_apple_sleep_start(appledwc);
if (ret)
return ret;
if (!appledwc->sleep_deep)
return 0;
in_place = appledwc->fixed_hub && appledwc->state == DWC3_APPLE_HOST;
/* A J714s host on a USB4 router's USB3 tunnel stays up for its router's sleep. */
if (!in_place && appledwc->state == DWC3_APPLE_HOST && dwc3_apple_tunnel_keep(appledwc))
return 0;
/* Suspend-to-idle keeps the controller started: only a gadget leaves its host. */
s2idle = !appledwc->sleep_deep;
if (s2idle) {
gadget = dwc3_apple_sleep_gadget(appledwc);
} else {
in_place = appledwc->fixed_hub && appledwc->state == DWC3_APPLE_HOST;
/* A J714s host on a USB4 router's USB3 tunnel stays up for its sleep. */
if (!in_place && appledwc->state == DWC3_APPLE_HOST &&
dwc3_apple_tunnel_keep(appledwc))
return 0;
}
}
if (s2idle) {
if (gadget)
dwc3_apple_sleep_gadget_off(appledwc, gadget);
return 0;
}
/* This only records a PHY sleep token; its hardware work runs at noirq. */
in_place = in_place && !apple_atcphy_sleep_prepare(appledwc->usb2_phy);
Expand Down Expand Up @@ -1928,12 +1998,51 @@ static void dwc3_apple_sleep_stop_work(struct work_struct *work)
complete(&appledwc->sleep_stop_done);
}

/* A role change deferred by the sleep, applied once the controller is back. */
static int dwc3_apple_sleep_role(struct dwc3_apple *appledwc)
{
int ret;

lockdep_assert_held(&appledwc->lock);
if (appledwc->shutting_down || !appledwc->sleep_role_pending)
return 0;
if (appledwc->sleep_role == appledwc->state && appledwc->core_active &&
!appledwc->core_exit_pending && !appledwc->sleep_stopped && !appledwc->sleep_dead) {
/* The latest request still matches the live role. */
appledwc->sleep_role_pending = false;
return 0;
}
ret = dwc3_apple_sleep_stop(appledwc);
dwc3_apple_sleep_take_role(appledwc);
if (!ret)
ret = dwc3_apple_sleep_start(appledwc);
return ret;
}

/* Ends the sleep's epoch: role changes apply directly again once IDLE is published. */
static void dwc3_apple_sleep_idle(struct dwc3_apple *appledwc, bool finish, bool kept)
{
unsigned long flags;

lockdep_assert_held(&appledwc->lock);
/* A failed exit must retain power for all remaining PHY/reset ownership. */
dwc3_apple_syscore(appledwc, appledwc->core_active || appledwc->reset_pending);
/* Publish IDLE with the role lock held, after queuing the PHY's finish. */
if (finish)
apple_atcphy_sleep_finish_async(appledwc->usb2_phy, kept);
spin_lock_irqsave(&appledwc->sleep_pm_lock, flags);
if (appledwc->sleep_phase != DWC3_APPLE_SLEEP_REMOVING)
appledwc->sleep_phase = DWC3_APPLE_SLEEP_IDLE;
spin_unlock_irqrestore(&appledwc->sleep_pm_lock, flags);
dwc3_apple_check_hub_later(appledwc);
}

static void dwc3_apple_sleep_start_work(struct work_struct *work)
{
struct dwc3_apple *appledwc = container_of(to_delayed_work(work), struct dwc3_apple,
sleep_start_work);
struct usb_gadget *gadget;
bool finish = false, kept = false;
unsigned long flags;
int ret = 0;

/* May still need notifier unregister: never joined by a notifier callback. */
Expand All @@ -1956,29 +2065,25 @@ static void dwc3_apple_sleep_start_work(struct work_struct *work)
else
ret = dwc3_apple_sleep_start(appledwc);
}
if (!ret && !appledwc->shutting_down && appledwc->sleep_role_pending) {
if (appledwc->sleep_role == appledwc->state && appledwc->core_active &&
!appledwc->core_exit_pending && !appledwc->sleep_stopped &&
!appledwc->sleep_dead) {
/* The latest request still matches the live role. */
appledwc->sleep_role_pending = false;
} else {
ret = dwc3_apple_sleep_stop(appledwc);
dwc3_apple_sleep_take_role(appledwc);
if (!ret)
ret = dwc3_apple_sleep_start(appledwc);
}
}
/* A failed exit must retain power for all remaining PHY/reset ownership. */
dwc3_apple_syscore(appledwc, appledwc->core_active || appledwc->reset_pending);
/* Publish IDLE with the role lock held, after queuing the PHY's finish. */
if (finish)
apple_atcphy_sleep_finish_async(appledwc->usb2_phy, kept);
spin_lock_irqsave(&appledwc->sleep_pm_lock, flags);
if (appledwc->sleep_phase != DWC3_APPLE_SLEEP_REMOVING)
appledwc->sleep_phase = DWC3_APPLE_SLEEP_IDLE;
spin_unlock_irqrestore(&appledwc->sleep_pm_lock, flags);
dwc3_apple_check_hub_later(appledwc);
if (!ret)
ret = dwc3_apple_sleep_role(appledwc);
/*
* Connect a gadget again before IDLE is published: until then role changes stay
* deferred, so it is not torn down under the connect, and no new sleep is admitted
* before it is back on the bus.
*/
gadget = dwc3_apple_sleep_gadget_back(appledwc);
if (!gadget)
dwc3_apple_sleep_idle(appledwc, finish, kept);
}
if (gadget) {
if (usb_gadget_connect(gadget))
dev_warn(appledwc->dev, "USB device not connected again after suspend-to-idle\n");
guard(mutex)(&appledwc->lock);
/* A role change requested during the connect. */
if (!ret)
ret = dwc3_apple_sleep_role(appledwc);
dwc3_apple_sleep_idle(appledwc, finish, kept);
}
if (ret)
dev_err(appledwc->dev, "USB host recovery failed: %d\n", ret);
Expand Down
2 changes: 1 addition & 1 deletion tools/asahi/neo-stability-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def function(source, name):
enum dwc3_apple_state {DWC3_APPLE_PROBE_PENDING,DWC3_APPLE_NO_CABLE,DWC3_APPLE_HOST,DWC3_APPLE_DEVICE,DWC3_APPLE_SUSPENDED};
#define DWC3_GCTL_PRTCAP_HOST 1
struct dwc3 {int num_usb3_ports,num_usb2_ports;int usb3_generic_phy[2],usb2_generic_phy[2],usb3_phy,usb2_phy;int current_dr_role;bool dis_u3_susphy_quirk;};
struct dwc3_apple {struct device*dev;struct dwc3 dwc;int lock,reset;enum dwc3_apple_state state;bool fixed_hub,host_only,rpm_held,core_active,core_exit_pending,reset_pending,dt_dis_u3_susphy,sleep_ddr;unsigned usb3_power_off_pending,usb2_power_off_pending;int usb2_phy;
struct dwc3_apple {struct device*dev;struct dwc3 dwc;int lock,reset;enum dwc3_apple_state state;bool fixed_hub,host_only,rpm_held,core_active,core_exit_pending,reset_pending,dt_dis_u3_susphy,sleep_ddr,sleep_gadget_off;unsigned usb3_power_off_pending,usb2_power_off_pending;int usb2_phy;
int sleep_pm_lock;enum dwc3_apple_sleep_phase sleep_phase;bool sleep_syscore;};
static void apple_sleep_usb_slp_ddr(bool on){(void)on;}
static int core_error,host_error,gadget_error,reset_error,role_error,start_role;
Expand Down
2 changes: 1 addition & 1 deletion tools/asahi/run-host-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def mutations(flag, names, base, expect=KILL):
APPLE_TESTS = ['aop-las.py', 'aop-motion.py', 'atc-t8132-usb3.py', 'bcm4377-dma-window.py',
'bcm4377-radio-sids.py', 'bcm4377-rti-address.py', 'bcm4377-state-wait.py',
'centauri-t6050-probe.py', 'dart-s2idle-retention.py', 'dwc3-initial-role.py',
'macsmc-j713.py', 'pmgr-inherited.py', 'sn201202x-transport.py']
'dwc3-sleep-gadget.py', 'macsmc-j713.py', 'pmgr-inherited.py', 'sn201202x-transport.py']
APPLE_HARDWARE = ['j616s-parity.py', 'sep-ioctl-fuzz.py']


Expand Down
3 changes: 2 additions & 1 deletion tools/testing/selftests/drivers/apple/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# atc-t8132-usb3.py runs on the build host against a CPU MMIO model.
# pmgr-inherited.py checks admission and lifecycle against host register mocks.
# dwc3-initial-role.py checks initial host-role admission before core reset.
# dwc3-sleep-gadget.py checks the device-role disconnect/reconnect around suspend-to-idle.
# sn201202x-transport.py checks the production SPMI selection and payload path.
# bcm4377 tests exercise DMA ownership, context addresses and firmware handshakes.
# aop-las.py checks the production angle decoder and IIO callback on the host.
Expand All @@ -14,7 +15,7 @@ TEST_PROGS := macsmc-j713.py atc-t8132-usb3.py pmgr-inherited.py dwc3-initial-ro
sn201202x-transport.py bcm4377-dma-window.py bcm4377-radio-sids.py \
bcm4377-state-wait.py bcm4377-rti-address.py aop-las.py \
aop-motion.py centauri-t6050-probe.py dart-s2idle-retention.py \
sep-ioctl-fuzz.py
sep-ioctl-fuzz.py dwc3-sleep-gadget.py
TEST_FILES := j813-peripherals.sh

include ../../lib.mk
2 changes: 1 addition & 1 deletion tools/testing/selftests/drivers/apple/atc-t8132-usb3.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ def main():
struct dwc3 dwc; struct device *dev; void *mmio_resource,*reset,*usb2_phy;
int lock,state; bool core_active,core_exit_pending,reset_pending,shutting_down,fixed_hub;
unsigned usb2_power_off_pending,usb3_power_off_pending,hub_port;
bool host_only,idle_rpm,rpm_held,dt_dis_u3_susphy,sleep_ddr,sleep_in_place;
bool host_only,idle_rpm,rpm_held,dt_dis_u3_susphy,sleep_ddr,sleep_in_place,sleep_gadget_off;
int sleep_pm_lock,sleep_start_work,sleep_stop_work,hub_check;
};
#define to_dwc3_apple(d) ((struct dwc3_apple *)(d))
Expand Down
Loading
Loading