Skip to contentSkip to conversation

usb: acpi: Get correct location data for Apple macs

PATCH Superseded Atharva Tiwari 3 participants 6 messages Nov 23 USB

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: acpi: Get correct location data for Apple macs

In All macs, the _PLD values in ACPI is just garbage values and provide no real data, and that may cause errors like:

usb: port power management may be unreliable

On Internal controllers, with multiple USB2.0 and USB3.0.

To fix this we use Apple's own location methods called SSD for USB2.0, and they use _ADR for USB3.0.

With this patch we get proper location and peering:

[    0.280281] usb usb2-port2: peered to usb1-port10
[    0.280298] usb usb2-port3: peered to usb1-port3
[    0.280311] usb usb2-port4: peered to usb1-port14
[    0.280325] usb usb2-port5: peered to usb1-port8
[    0.280354] usb usb2-port9: peered to usb1-port9

NOTE: All USB controller has bad _PLD values but SSD is only available
on internal USB controller, and even only new intel macs has SSD method, so we check for SSD and XHC1 before using apple's methods.

---
 drivers/usb/core/usb-acpi.c | 71 +++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)
drivers/usb/core/usb-acpi.c +71 -0
index 489dbdc96..b5c61a2ea 100644
@@ -12,6 +12,7 @@
 #include <linux/acpi.h>
 #include <linux/pci.h>
 #include <linux/usb/hcd.h>
+#include <linux/platform_data/x86/apple.h>
 
 #include "hub.h"
 
@@ -199,6 +200,66 @@ static int usb_acpi_add_usb4_devlink(struct usb_device *udev)
  */
 #define USB_ACPI_LOCATION_VALID (1 << 31)
 
+/*
+ * Set at registration time on x86 Apple Macs whose internal XHC1 controller
+ * has ports exposing an SSP object. Apple reports the port location through
+ * SSP (USB 2.0) and _ADR (USB 3.x) instead of _PLD.
+ */
+static bool is_apple_location;
+
+static acpi_status
+usb_acpi_apple_ssp_cb(acpi_handle handle, u32 level, void *ctx, void **ret)
+{
+	if (!acpi_has_method(handle, "SSP"))
+		return AE_OK;
+
+	*(bool *)ctx = true;
+	return AE_CTRL_TERMINATE;
+}
+
+static void usb_acpi_apple_detect_ssp(acpi_handle xhc1)
+{
+	bool found = false;
+
+	acpi_walk_namespace(ACPI_TYPE_DEVICE, xhc1, ACPI_UINT32_MAX,
+			    usb_acpi_apple_ssp_cb, NULL, &found, NULL);
+	is_apple_location = found;
+}
+
+static void
+usb_acpi_apple_get_location(struct usb_port *port_dev, struct acpi_device *adev)
+{
+	struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
+	struct acpi_device *hc = ACPI_COMPANION(hdev->bus->sysdev);
+	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
+	union acpi_object *ssp;
+	acpi_status status;
+
+	/* Only applies to the CPU's internal XHCI controller. */
+	if (!hc || strncmp(acpi_device_bid(hc), "XHC1", ACPI_NAMESEG_SIZE))
+		return;
+
+	/* USB 3.x ports use _ADR as the location. */
+	if (hdev->speed >= USB_SPEED_SUPER) {
+		port_dev->location = USB_ACPI_LOCATION_VALID |
+				     adev->pnp.bus_address;
+		return;
+	}
+
+	/* USB 2.0 ports: SSP = { "XHCn", <companion SuperSpeed port> } */
+	status = acpi_evaluate_object_typed(adev->handle, "SSP", NULL, &buffer,
+					    ACPI_TYPE_PACKAGE);
+	if (ACPI_FAILURE(status))
+		return;
+
+	ssp = buffer.pointer;
+	if (ssp->package.count >= 2 &&
+	    ssp->package.elements[1].type == ACPI_TYPE_INTEGER)
+		port_dev->location = USB_ACPI_LOCATION_VALID |
+				     ssp->package.elements[1].integer.value;
+	kfree(buffer.pointer);
+}
+
 static void
 usb_acpi_get_connect_type(struct usb_port *port_dev, acpi_handle *handle)
 {
@@ -289,6 +350,10 @@ usb_acpi_find_companion_for_port(struct usb_port *port_dev)
 
 	usb_acpi_get_connect_type(port_dev, adev->handle);
 
+	/* Apple's location overrides the _PLD derived one */
+	if (is_apple_location)
+		usb_acpi_apple_get_location(port_dev, adev);
+
 	return adev;
 }
 
@@ -377,6 +442,12 @@ static struct acpi_bus_type usb_acpi_bus = {
 
 int usb_acpi_register(void)
 {
+	acpi_handle xhc1;
+
+	if (x86_apple_machine &&
+	    ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB.PCI0.XHC1", &xhc1)))
+		usb_acpi_apple_detect_ssp(xhc1);
+
 	return register_acpi_bus_type(&usb_acpi_bus);
 }
 
Signature · 1 line
2.55.0
#1 lore raw
Quoted 32 lines · Atharva Tiwari

In All macs, the _PLD values in ACPI is just garbage values and provide no real data, and that may cause errors like:

usb: port power management may be unreliable

On Internal controllers, with multiple USB2.0 and USB3.0.

To fix this we use Apple's own location methods called SSD for USB2.0, and they use _ADR for USB3.0.

With this patch we get proper location and peering:

[    0.280281] usb usb2-port2: peered to usb1-port10
[    0.280298] usb usb2-port3: peered to usb1-port3
[    0.280311] usb usb2-port4: peered to usb1-port14
[    0.280325] usb usb2-port5: peered to usb1-port8
[    0.280354] usb usb2-port9: peered to usb1-port9

NOTE: All USB controller has bad _PLD values but SSD is only available
on internal USB controller, and even only new intel macs has SSD method, so we check for SSD and XHC1 before using apple's methods.

---
 drivers/usb/core/usb-acpi.c | 71 +++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)
drivers/usb/core/usb-acpi.c +0 -0
index 489dbdc96..b5c61a2ea 100644

drivers/usb/core/usb-acpi.c line 15

Quoted 4 lines
drivers/usb/core/usb-acpi.c +1 -0
@@ -12,6 +12,7 @@
 #include <linux/acpi.h>
 #include <linux/pci.h>
 #include <linux/usb/hcd.h>
+#include <linux/platform_data/x86/apple.h>

Again, platform specific files for a generic .c file?

drivers/usb/core/usb-acpi.c static int usb_acpi_add_usb4_devlink(struct usb_device *udev) · line 208

Quoted 13 lines
drivers/usb/core/usb-acpi.c +6 -0
 #include "hub.h"
 
@@ -199,6 +200,66 @@ static int usb_acpi_add_usb4_devlink(struct usb_device *udev)
  */
 #define USB_ACPI_LOCATION_VALID (1 << 31)
 
+/*
+ * Set at registration time on x86 Apple Macs whose internal XHC1 controller
+ * has ports exposing an SSP object. Apple reports the port location through
+ * SSP (USB 2.0) and _ADR (USB 3.x) instead of _PLD.
+ */
+static bool is_apple_location;

Local static variables are usually not a good idea. Why is this needed as it's only referenced once?

thanks,

greg k-h

#2 lore raw
Quoted 2 lines

Local static variables are usually not a good idea. Why is this needed as it's only referenced once?

is_apple_location signifies that SSD methods are available in the ACPI bus, if we dont check this there might be a situation like:

Is internal USB Controller = true
x86_apple_machine = true
is_apple_machine (Are there any SSD methods in the hub) = false

So all USB2.0 ports will get a error, and all USB3.0 ports will succeed and provide wrong info.

And btw the SSD methods are only used in new intel macs (2016+), so this becomes necessary.

Thanks,

Atharva.

maintainer
#3 lore raw
Quoted 6 lines · Atharva Tiwari
Quoted 2 lines

Local static variables are usually not a good idea. Why is this needed as it's only referenced once?

is_apple_location signifies that SSD methods are available in the ACPI bus, if we dont check this there might be a situation like:

Quoted 6 lines

Is internal USB Controller = true
x86_apple_machine = true
is_apple_machine (Are there any SSD methods in the hub) = false

So all USB2.0 ports will get a error, and all USB3.0 ports will succeed and provide wrong info.

Sure, but again, you only check it once, why not just do the logic there then and not have any local variables?

Quoted 1 line

And btw the SSD methods are only used in new intel macs (2016+), so this becomes necessary.

So how have these old systems been working so far with Linux?

greg k-h

#4 lore raw
Quoted 2 lines

Sure, but again, you only check it once, why not just do the logic there then and not have any local variables?

I thought of that, but i did not do that because it creates a order dependency that being USB2.0 has to come before USB3.0 otherwise the code would not work.

Quoted 1 line

So how have these old systems been working so far with Linux?

The feature itself is not that big, that systems would break without it just warns about it:

usb: port power management may be unreliable

but new macs provide location info, so i thought to add support for it.

#5 lore raw
In reply to Atharva Tiwari
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] usb: acpi: stale _PLD location left on XHC1 USB2 ports in usb_acpi_apple_get_location() - [Low]…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] usb: acpi: stale _PLD location left on XHC1 USB2 ports in usb_acpi_apple_get_location()
- [Low] usb: acpi: commit message refers to ACPI method SSD instead of SSP
--

commit 092f7ae240ffb60b57f520dfbc02f53417ace8ae
Author: Atharva Tiwari <[email protected]>

usb: acpi: Get correct location data for Apple macs

This commit overrides the _PLD-derived USB port location on x86 Apple Macs by reading Apple's SSP ACPI object for USB 2.0 ports and _ADR for USB 3.x ports on the internal XHC1 controller.

Quoted 2 lines

To fix this we use Apple's own location methods called SSD for USB2.0, and they use _ADR for USB3.0.

[ ... ]

Quoted 3 lines

NOTE: All USB controller has bad _PLD values but SSD is only available
on internal USB controller, and even only new intel macs has SSD method, so we check for SSD and XHC1 before using apple's methods.

[Severity: Low]
The commit message refers to the ACPI method as "SSD", whereas the code checks for and evaluates "SSP".

Quoted 4 lines
drivers/usb/core/usb-acpi.c +0 -0
index 489dbdc96f94a..b5c61a2eae2bc 100644

[ ... ]

Quoted 1 line
drivers/usb/core/usb-acpi.c +0 -0
@@ -199,6 +200,66 @@ static int usb_acpi_add_usb4_devlink(struct usb_device *udev)

[ ... ]

Quoted 23 lines
drivers/usb/core/usb-acpi.c +23 -0
+static void
+usb_acpi_apple_get_location(struct usb_port *port_dev, struct acpi_device *adev)
+{
+	struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
+	struct acpi_device *hc = ACPI_COMPANION(hdev->bus->sysdev);
+	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
+	union acpi_object *ssp;
+	acpi_status status;
+
+	/* Only applies to the CPU's internal XHCI controller. */
+	if (!hc || strncmp(acpi_device_bid(hc), "XHC1", ACPI_NAMESEG_SIZE))
+		return;
+
+	/* USB 3.x ports use _ADR as the location. */
+	if (hdev->speed >= USB_SPEED_SUPER) {
+		port_dev->location = USB_ACPI_LOCATION_VALID |
+				     adev->pnp.bus_address;
+		return;
+	}
+
+	/* USB 2.0 ports: SSP = { "XHCn", <companion SuperSpeed port> } */
+	status = acpi_evaluate_object_typed(adev->handle, "SSP", NULL, &buffer,
+					    ACPI_TYPE_PACKAGE);

drivers/usb/core/usb-acpi.c static int usb_acpi_add_usb4_devlink(struct usb_device *udev) · line 261

Quoted 10 lines
drivers/usb/core/usb-acpi.c +10 -0
+	if (ACPI_FAILURE(status))
+		return;
+
+	ssp = buffer.pointer;
+	if (ssp->package.count >= 2 &&
+	    ssp->package.elements[1].type == ACPI_TYPE_INTEGER)
+		port_dev->location = USB_ACPI_LOCATION_VALID |
+				     ssp->package.elements[1].integer.value;
+	kfree(buffer.pointer);
+}

[Severity: Medium]
Does returning here without clearing port_dev->location leave the garbage _PLD location on XHC1 USB 2.0 ports that do not have an SSP method?

usb_acpi_get_connect_type() runs before usb_acpi_apple_get_location() and sets port_dev->location from _PLD:

drivers/usb/core/usb-acpi.c:usb_acpi_get_connect_type() {
	if (acpi_get_physical_device_location(handle, &pld) && pld)
		port_dev->location = USB_ACPI_LOCATION_VALID |
			pld->group_token << 8 | pld->group_position;

On USB 2.0-only ports on XHC1 that lack a SuperSpeed companion (and thus have no SSP object), acpi_evaluate_object_typed() fails and leaves that _PLD value in port_dev->location, mixing raw _PLD values into the same location namespace as USB 3.x _ADR values where they can falsely match in find_and_link_peer(). Resetting port_dev->location = 0 right after confirming the controller is XHC1 would clear the stale _PLD data even when SSP is absent.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1