Skip to contentSkip to conversation

usb: xhci-dwc3: Add DM_FLAG_OS_PREPARE flag

PATCH In review Romain Beauxis 4 participants 11 messages Sep 12 – Oct 11

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: xhci-dwc3: Add DM_FLAG_OS_PREPARE flag

Neither bootm nor EFI ExitBootServices() stops USB controllers explicitly. Both only remove devices whose driver sets DM_FLAG_ACTIVE_DMA or DM_FLAG_OS_PREPARE. xhci-dwc3 sets neither, so a controller brought up by "usb start", e.g. from preboot for a USB keyboard, is handed to the OS still running.

Set DM_FLAG_OS_PREPARE so that xhci_dwc3_remove() runs at handoff. This is the path "usb stop" already takes: the xHCI is reset, its PHYs are shut down and its clocks and resets are released. Child devices such as hubs, storage and keyboards are removed first, and a controller already stopped by "usb stop" is skipped. xhci-pci has done the same since commit e3bbc1f74e05 ("usb: xhci-pci: Add DM_FLAG_OS_PREPARE flag"), as have ohci-da8xx and the cdns3 wrappers.

On Apple silicon machines the running controller is user visible: Linux does not enumerate a USB device attached to a Type-C port at boot until it is unplugged and plugged back in.

---
Tested on an M1 Mac mini running Fedora Asahi Remix with Linux 7.1.13, booting through EFI and GRUB, with a Samsung T7 USB 3 SSD attached to a Type-C port. U-Boot is v2026.07 plus the Asahi downstream patches.

  U-Boot                                SSD enumerated by Linux
  stock                                 only after a replug, at 83s
  stock, bootcmd "usb stop; <default>"  10.1s, no replug
  stock plus this patch                 10.2s, no replug

In the stock boot, the replug made Linux tear down and re-create the Type-C xHCI instance, and the SSD appeared right after. In all three boots a disk on the USB-A ports, which sit behind xhci-pci on the same machine, enumerated at 6s. The linked issue reports the same failure on M1 and M2 MacBook Airs with Linux 7.0 and 7.1.

Only the EFI handoff was tested. bootm reaches the same dm_remove_devices_active() call. No non-Apple xhci-dwc3 board was tested.

 drivers/usb/host/xhci-dwc3.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/host/xhci-dwc3.c +1 -1
index 3504a7cf4..92b311d42 100644
@@ -256,6 +256,6 @@ U_BOOT_DRIVER(xhci_dwc3) = {
 	.ops = &xhci_usb_ops,
 	.priv_auto	= sizeof(struct xhci_ctrl),
 	.plat_auto	= sizeof(struct xhci_dwc3_plat),
-	.flags = DM_FLAG_ALLOC_PRIV_DMA,
+	.flags = DM_FLAG_OS_PREPARE | DM_FLAG_ALLOC_PRIV_DMA,
 };
 #endif
Signature · 1 line
2.55.0
#1 lore raw
Quoted 14 lines · Romain Beauxis

Neither bootm nor EFI ExitBootServices() stops USB controllers explicitly. Both only remove devices whose driver sets DM_FLAG_ACTIVE_DMA or DM_FLAG_OS_PREPARE. xhci-dwc3 sets neither, so a controller brought up by "usb start", e.g. from preboot for a USB keyboard, is handed to the OS still running.

Set DM_FLAG_OS_PREPARE so that xhci_dwc3_remove() runs at handoff. This is the path "usb stop" already takes: the xHCI is reset, its PHYs are shut down and its clocks and resets are released. Child devices such as hubs, storage and keyboards are removed first, and a controller already stopped by "usb stop" is skipped. xhci-pci has done the same since commit e3bbc1f74e05 ("usb: xhci-pci: Add DM_FLAG_OS_PREPARE flag"), as have ohci-da8xx and the cdns3 wrappers.

Quoted 3 lines

On Apple silicon machines the running controller is user visible: Linux does not enumerate a USB device attached to a Type-C port at boot until it is unplugged and plugged back in.

boot/bootm.c bootm_final() does call dm_remove_devices_active() , is that not sufficient to tear the controllers down before booting Linux ?

#2 lore raw

Le sam. 12 sept. 2026 à 22:04, Marek Vasut <[email protected]> a écrit :

Quoted 16 lines · Romain Beauxis
Quoted 14 lines

Neither bootm nor EFI ExitBootServices() stops USB controllers explicitly. Both only remove devices whose driver sets DM_FLAG_ACTIVE_DMA or DM_FLAG_OS_PREPARE. xhci-dwc3 sets neither, so a controller brought up by "usb start", e.g. from preboot for a USB keyboard, is handed to the OS still running.

Set DM_FLAG_OS_PREPARE so that xhci_dwc3_remove() runs at handoff. This is the path "usb stop" already takes: the xHCI is reset, its PHYs are shut down and its clocks and resets are released. Child devices such as hubs, storage and keyboards are removed first, and a controller already stopped by "usb stop" is skipped. xhci-pci has done the same since commit e3bbc1f74e05 ("usb: xhci-pci: Add DM_FLAG_OS_PREPARE flag"), as have ohci-da8xx and the cdns3 wrappers.

Quoted 6 lines
Quoted 3 lines

On Apple silicon machines the running controller is user visible: Linux does not enumerate a USB device attached to a Type-C port at boot until it is unplugged and plugged back in.

boot/bootm.c bootm_final() does call dm_remove_devices_active() , is that not sufficient to tear the controllers down before booting Linux ?

Any idea how to test this?

I can confirm that normal boot does not work without the patch and works with it, that's about as far as I've been

Also tested passing an explicit usb stop without the patch and it also works.

#3 lore raw
Quoted 18 lines · Romain Beauxis

Le sam. 12 sept. 2026 à 22:04, Marek Vasut <[email protected]> a écrit :

Quoted 16 lines · Romain Beauxis
Quoted 14 lines

Neither bootm nor EFI ExitBootServices() stops USB controllers explicitly. Both only remove devices whose driver sets DM_FLAG_ACTIVE_DMA or DM_FLAG_OS_PREPARE. xhci-dwc3 sets neither, so a controller brought up by "usb start", e.g. from preboot for a USB keyboard, is handed to the OS still running.

Set DM_FLAG_OS_PREPARE so that xhci_dwc3_remove() runs at handoff. This is the path "usb stop" already takes: the xHCI is reset, its PHYs are shut down and its clocks and resets are released. Child devices such as hubs, storage and keyboards are removed first, and a controller already stopped by "usb stop" is skipped. xhci-pci has done the same since commit e3bbc1f74e05 ("usb: xhci-pci: Add DM_FLAG_OS_PREPARE flag"), as have ohci-da8xx and the cdns3 wrappers.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

On Apple silicon machines the running controller is user visible: Linux does not enumerate a USB device attached to a Type-C port at boot until it is unplugged and plugged back in.

boot/bootm.c bootm_final() does call dm_remove_devices_active() , is that not sufficient to tear the controllers down before booting Linux ?

Any idea how to test this?

Add a printf() into the controller and uclass .remove/.unbind callbacks ? :)

Quoted 5 lines

I can confirm that normal boot does not work without the patch and works with it, that's about as far as I've been

Also tested passing an explicit usb stop without the patch and it also works.

#4 lore raw

Le mer. 16 sept. 2026 à 23:47, Marek Vasut <[email protected]> a écrit :

Quoted 13 lines · Romain Beauxis
Quoted 11 lines

Le sam. 12 sept. 2026 à 22:04, Marek Vasut <[email protected]> a écrit :

Quoted 9 lines · Romain Beauxis
Quoted 7 lines

Neither bootm nor EFI ExitBootServices() stops USB controllers explicitly. Both only remove devices whose driver sets DM_FLAG_ACTIVE_DMA or DM_FLAG_OS_PREPARE. xhci-dwc3 sets neither, so a controller brought up by "usb start", e.g. from preboot for a USB keyboard, is handed to the OS still running.

Set DM_FLAG_OS_PREPARE so that xhci_dwc3_remove() runs at handoff.

This

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

is the path "usb stop" already takes: the xHCI is reset, its PHYs are shut down and its clocks and resets are released. Child devices such

as

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

hubs, storage and keyboards are removed first, and a controller

already

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

stopped by "usb stop" is skipped. xhci-pci has done the same since commit e3bbc1f74e05 ("usb: xhci-pci: Add DM_FLAG_OS_PREPARE flag"), as have ohci-da8xx and the cdns3 wrappers.

On Apple silicon machines the running controller is user visible:

Linux

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

does not enumerate a USB device attached to a Type-C port at boot

until

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

it is unplugged and plugged back in.

boot/bootm.c bootm_final() does call dm_remove_devices_active() , is that not sufficient to tear the controllers down before booting Linux ?

Any idea how to test this?

Add a printf() into the controller and uclass .remove/.unbind callbacks ?

:)

Ok I can try that but I'm not sure if your question covers a part that's been untested. What I did confirm:
* Stock u-boot: kernel does not pick up the USB device on boot
* With explicit usb stop: kernel does pick up the USB device on boot
* With proposed patch: kernel does pick up the USB device on boot

Is your question related to a different scenario that has not been covered yet?

Quoted 5 lines
Quoted 5 lines

I can confirm that normal boot does not work without the patch and works with it, that's about as far as I've been

Also tested passing an explicit usb stop without the patch and it also works.

#5 lore raw
Quoted 39 lines · Romain Beauxis

Le mer. 16 sept. 2026 à 23:47, Marek Vasut <[email protected]> a écrit :

Quoted 13 lines · Romain Beauxis
Quoted 11 lines

Le sam. 12 sept. 2026 à 22:04, Marek Vasut <[email protected]> a écrit :

Quoted 9 lines · Romain Beauxis
Quoted 7 lines

Neither bootm nor EFI ExitBootServices() stops USB controllers explicitly. Both only remove devices whose driver sets DM_FLAG_ACTIVE_DMA or DM_FLAG_OS_PREPARE. xhci-dwc3 sets neither, so a controller brought up by "usb start", e.g. from preboot for a USB keyboard, is handed to the OS still running.

Set DM_FLAG_OS_PREPARE so that xhci_dwc3_remove() runs at handoff.

This

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

is the path "usb stop" already takes: the xHCI is reset, its PHYs are shut down and its clocks and resets are released. Child devices such

as

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

hubs, storage and keyboards are removed first, and a controller

already

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

stopped by "usb stop" is skipped. xhci-pci has done the same since commit e3bbc1f74e05 ("usb: xhci-pci: Add DM_FLAG_OS_PREPARE flag"), as have ohci-da8xx and the cdns3 wrappers.

On Apple silicon machines the running controller is user visible:

Linux

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

does not enumerate a USB device attached to a Type-C port at boot

until

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

it is unplugged and plugged back in.

boot/bootm.c bootm_final() does call dm_remove_devices_active() , is that not sufficient to tear the controllers down before booting Linux ?

Any idea how to test this?

Add a printf() into the controller and uclass .remove/.unbind callbacks ?

:)

Quoted 8 lines

Ok I can try that but I'm not sure if your question covers a part that's been untested. What I did confirm:
* Stock u-boot: kernel does not pick up the USB device on boot
* With explicit usb stop: kernel does pick up the USB device on boot
* With proposed patch: kernel does pick up the USB device on boot

Is your question related to a different scenario that has not been covered yet?

boot/bootm.c bootm_final() does call dm_remove_devices_active() , that should be enough to shut the controller down. Why is it not enough ?

What does "the USB device" and "pick up" mean here, does the controller fail to enumerate devices attached to it when Linux boots ? Could the problem be related to some clock or some such instead ?

#6 lore raw
Quoted 47 lines · Marek Vasut
Quoted 45 lines · Romain Beauxis

Le mer. 16 sept. 2026 à 23:47, Marek Vasut <[email protected]> a écrit :

Quoted 13 lines · Romain Beauxis
Quoted 11 lines

Le sam. 12 sept. 2026 à 22:04, Marek Vasut <[email protected]> a écrit :

Quoted 9 lines · Romain Beauxis
Quoted 7 lines

Neither bootm nor EFI ExitBootServices() stops USB controllers explicitly. Both only remove devices whose driver sets DM_FLAG_ACTIVE_DMA or DM_FLAG_OS_PREPARE. xhci-dwc3 sets neither, so a controller brought up by "usb start", e.g. from preboot for a USB keyboard, is handed to the OS still running.

Set DM_FLAG_OS_PREPARE so that xhci_dwc3_remove() runs at handoff.

This

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

is the path "usb stop" already takes: the xHCI is reset, its PHYs are shut down and its clocks and resets are released. Child devices such

as

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

hubs, storage and keyboards are removed first, and a controller

already

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

stopped by "usb stop" is skipped. xhci-pci has done the same since commit e3bbc1f74e05 ("usb: xhci-pci: Add DM_FLAG_OS_PREPARE flag"), as have ohci-da8xx and the cdns3 wrappers.

On Apple silicon machines the running controller is user visible:

Linux

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

does not enumerate a USB device attached to a Type-C port at boot

until

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

it is unplugged and plugged back in.

boot/bootm.c bootm_final() does call dm_remove_devices_active() , is that not sufficient to tear the controllers down before booting Linux ?

Any idea how to test this?

Add a printf() into the controller and uclass .remove/.unbind callbacks ?

:)

Ok I can try that but I'm not sure if your question covers a part that's been untested. What I did confirm:
* Stock u-boot: kernel does not pick up the USB device on boot
* With explicit usb stop: kernel does pick up the USB device on boot
* With proposed patch: kernel does pick up the USB device on boot

Quoted 4 lines
Quoted 2 lines

Is your question related to a different scenario that has not been covered yet?

boot/bootm.c bootm_final() does call dm_remove_devices_active() , that should be enough to shut the controller down. Why is it not enough ?

No idea. I'm barely above user level here. Trying to help with reporting and proposing a patch based on what other drivers seems to be doing.

Quoted 3 lines

What does "the USB device" and "pick up" mean here, does the controller fail to enumerate devices attached to it when Linux boots ? Could the problem be related to some clock or some such instead ?

Sorry for the confusion here. I have a hard drive connected to the USB hub that I want to mount at startup. It fails to enumerate when booting using the stock u-boot but enumerates with the two solutions posted above. Also enumerates when unplugged/plugged back.

I'm happy to help test to find the right root cause for this issue but I'm relying on the machine for my daily use so I would appreciate as much details as possible to the part of the code that should be debugged so I can give you/y'all as much details in one shot.

Thanks!
-- Romain

#7 lore raw
In reply to Marek Vasut
Quoted 15 lines · Marek Vasut
Quoted 14 lines · Romain Beauxis

Neither bootm nor EFI ExitBootServices() stops USB controllers explicitly. Both only remove devices whose driver sets DM_FLAG_ACTIVE_DMA or DM_FLAG_OS_PREPARE. xhci-dwc3 sets neither, so a controller brought up by "usb start", e.g. from preboot for a USB keyboard, is handed to the OS still running.

Set DM_FLAG_OS_PREPARE so that xhci_dwc3_remove() runs at handoff. This is the path "usb stop" already takes: the xHCI is reset, its PHYs are shut down and its clocks and resets are released. Child devices such as hubs, storage and keyboards are removed first, and a controller already stopped by "usb stop" is skipped. xhci-pci has done the same since commit e3bbc1f74e05 ("usb: xhci-pci: Add DM_FLAG_OS_PREPARE flag"), as have ohci-da8xx and the cdns3 wrappers.

Quoted 5 lines
Quoted 3 lines

On Apple silicon machines the running controller is user visible: Linux does not enumerate a USB device attached to a Type-C port at boot until it is unplugged and plugged back in.

boot/bootm.c bootm_final() does call dm_remove_devices_active() , is that not sufficient to tear the controllers down before booting Linux ?

Jumping back up to this part of the thread. I believe the answer is that it is only enough once we set DM_FLAG_OS_PREPARE so that we know to find and tear this down. Without digging through all of the discussions of the other places we've used this flag to fix a problem like this over the years, I believe it's a feature not a bug of how we do things that we need this flag.

Signature · 1 line
Tom
#8 lore raw
Quoted 22 lines · Tom Rini
Quoted 15 lines · Marek Vasut
Quoted 14 lines · Romain Beauxis

Neither bootm nor EFI ExitBootServices() stops USB controllers explicitly. Both only remove devices whose driver sets DM_FLAG_ACTIVE_DMA or DM_FLAG_OS_PREPARE. xhci-dwc3 sets neither, so a controller brought up by "usb start", e.g. from preboot for a USB keyboard, is handed to the OS still running.

Set DM_FLAG_OS_PREPARE so that xhci_dwc3_remove() runs at handoff. This is the path "usb stop" already takes: the xHCI is reset, its PHYs are shut down and its clocks and resets are released. Child devices such as hubs, storage and keyboards are removed first, and a controller already stopped by "usb stop" is skipped. xhci-pci has done the same since commit e3bbc1f74e05 ("usb: xhci-pci: Add DM_FLAG_OS_PREPARE flag"), as have ohci-da8xx and the cdns3 wrappers.

Quoted 5 lines
Quoted 3 lines

On Apple silicon machines the running controller is user visible: Linux does not enumerate a USB device attached to a Type-C port at boot until it is unplugged and plugged back in.

boot/bootm.c bootm_final() does call dm_remove_devices_active() , is that not sufficient to tear the controllers down before booting Linux ?

Quoted 6 lines

Jumping back up to this part of the thread. I believe the answer is that it is only enough once we set DM_FLAG_OS_PREPARE so that we know to find and tear this down. Without digging through all of the discussions of the other places we've used this flag to fix a problem like this over the years, I believe it's a feature not a bug of how we do things that we need this flag.

I am not convinced this is the right fix, but I asked Mark to dig into this, since I don't have any apple silicon hardware. Maybe they can help here.

#9 lore raw
Quoted 32 lines

Date: Mon, 28 Sep 2026 23:34:42 +0200
From: Marek Vasut <[email protected]>

Quoted 22 lines · Tom Rini
Quoted 15 lines · Marek Vasut
Quoted 14 lines · Romain Beauxis

Neither bootm nor EFI ExitBootServices() stops USB controllers explicitly. Both only remove devices whose driver sets DM_FLAG_ACTIVE_DMA or DM_FLAG_OS_PREPARE. xhci-dwc3 sets neither, so a controller brought up by "usb start", e.g. from preboot for a USB keyboard, is handed to the OS still running.

Set DM_FLAG_OS_PREPARE so that xhci_dwc3_remove() runs at handoff. This is the path "usb stop" already takes: the xHCI is reset, its PHYs are shut down and its clocks and resets are released. Child devices such as hubs, storage and keyboards are removed first, and a controller already stopped by "usb stop" is skipped. xhci-pci has done the same since commit e3bbc1f74e05 ("usb: xhci-pci: Add DM_FLAG_OS_PREPARE flag"), as have ohci-da8xx and the cdns3 wrappers.

Quoted 5 lines
Quoted 3 lines

On Apple silicon machines the running controller is user visible: Linux does not enumerate a USB device attached to a Type-C port at boot until it is unplugged and plugged back in.

boot/bootm.c bootm_final() does call dm_remove_devices_active() , is that not sufficient to tear the controllers down before booting Linux ?

Quoted 6 lines

Jumping back up to this part of the thread. I believe the answer is that it is only enough once we set DM_FLAG_OS_PREPARE so that we know to find and tear this down. Without digging through all of the discussions of the other places we've used this flag to fix a problem like this over the years, I believe it's a feature not a bug of how we do things that we need this flag.

Quoted 3 lines

I am not convinced this is the right fix, but I asked Mark to dig into this, since I don't have any apple silicon hardware. Maybe they can help here.

Sorry, I've been a bit busy over the last few weeks.

I've had a few reports of spurious USB DMA activity during early boot of OpenBSD. I recently made some changes there to enable the IOMMU properly (instead using bypass mode), and just received another report that definitely points at the USB controller doing DMA when it shouldn't. I haven't been able to reproduce the issue myself, but I think there is enough evidence for U-Boot not properly disabling DMA when handing control to the OS.

I think that adding DM_FLAG_ACTIVE_DMA as Tom suggested in a different USB-related thread better reflects the intent.

Cheers,

Mark

#10 lore raw
Quoted 46 lines · Mark Kettenis
Quoted 32 lines

Date: Mon, 28 Sep 2026 23:34:42 +0200
From: Marek Vasut <[email protected]>

Quoted 22 lines · Tom Rini
Quoted 15 lines · Marek Vasut
Quoted 14 lines · Romain Beauxis

Neither bootm nor EFI ExitBootServices() stops USB controllers explicitly. Both only remove devices whose driver sets DM_FLAG_ACTIVE_DMA or DM_FLAG_OS_PREPARE. xhci-dwc3 sets neither, so a controller brought up by "usb start", e.g. from preboot for a USB keyboard, is handed to the OS still running.

Set DM_FLAG_OS_PREPARE so that xhci_dwc3_remove() runs at handoff. This is the path "usb stop" already takes: the xHCI is reset, its PHYs are shut down and its clocks and resets are released. Child devices such as hubs, storage and keyboards are removed first, and a controller already stopped by "usb stop" is skipped. xhci-pci has done the same since commit e3bbc1f74e05 ("usb: xhci-pci: Add DM_FLAG_OS_PREPARE flag"), as have ohci-da8xx and the cdns3 wrappers.

Quoted 5 lines
Quoted 3 lines

On Apple silicon machines the running controller is user visible: Linux does not enumerate a USB device attached to a Type-C port at boot until it is unplugged and plugged back in.

boot/bootm.c bootm_final() does call dm_remove_devices_active() , is that not sufficient to tear the controllers down before booting Linux ?

Quoted 6 lines

Jumping back up to this part of the thread. I believe the answer is that it is only enough once we set DM_FLAG_OS_PREPARE so that we know to find and tear this down. Without digging through all of the discussions of the other places we've used this flag to fix a problem like this over the years, I believe it's a feature not a bug of how we do things that we need this flag.

Quoted 3 lines

I am not convinced this is the right fix, but I asked Mark to dig into this, since I don't have any apple silicon hardware. Maybe they can help here.

Sorry, I've been a bit busy over the last few weeks.

I've had a few reports of spurious USB DMA activity during early boot of OpenBSD. I recently made some changes there to enable the IOMMU properly (instead using bypass mode), and just received another report that definitely points at the USB controller doing DMA when it shouldn't. I haven't been able to reproduce the issue myself, but I think there is enough evidence for U-Boot not properly disabling DMA when handing control to the OS.

Quoted 2 lines

I think that adding DM_FLAG_ACTIVE_DMA as Tom suggested in a different USB-related thread better reflects the intent.

ACK