Skip to contentSkip to conversation

usb: acpi: Get correct location data for Apple macs

PATCH In review Atharva Tiwari 3 participants 4 messages Nov 23 USB

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 SSP 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 SSP is only available
on internal USB controller, and even only new intel macs has SSP method, so we check for SSP and XHC1 before using apple's methods.

---
Changes in v2:
- Nulled location provided by _PLD in apple_get_location
- Fixed typos.

Link to v1:
https://lore.kernel.org/all/[email protected]/

 drivers/usb/core/usb-acpi.c | 74 +++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
drivers/usb/core/usb-acpi.c +74 -0
index 489dbdc96..403c2ff44 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,69 @@ 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;
+
+	/* Location provided by _PLD is garbage. */
+	port_dev->location = 0;
+
+	/* 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 +353,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 +445,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
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] usb: acpi: unnecessary static is_apple_location in usb_acpi_register() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] usb: acpi: unnecessary static is_apple_location in usb_acpi_register()
--

commit e06f84629b0b4d6fa8b40243e2606ac6c367b974
Author: Atharva Tiwari <[email protected]>

usb: acpi: Get correct location data for Apple macs

This commit overrides the ACPI _PLD port location on x86 Apple Macs whose internal XHC1 controller exposes an SSP method, using SSP for USB 2.0 ports and _ADR for USB 3.x ports.

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

[ ... ]

Quoted 10 lines
drivers/usb/core/usb-acpi.c +6 -0
@@ -199,6 +200,69 @@ 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;

[ ... ]

Quoted 13 lines
drivers/usb/core/usb-acpi.c +4 -0
@@ -289,6 +353,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 +445,12 @@ static struct acpi_bus_type usb_acpi_bus = {
 

drivers/usb/core/usb-acpi.c static struct acpi_bus_type usb_acpi_bus = { · line 455

Quoted 10 lines
drivers/usb/core/usb-acpi.c +6 -0
 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);
 }

[Severity: Low]
As reported by Greg Kroah-Hartman on v1 [1], can the static is_apple_location variable and the registration-time lookup in usb_acpi_register() be dropped by performing the check directly at the call site?

usb_acpi_apple_get_location() already resolves the host controller ACPI device via hc = ACPI_COMPANION(hdev->bus->sysdev) and checks that its bus ID is XHC1. Because all child port objects and SSP methods under hc->handle exist in the ACPI namespace before any USB 2.0 or USB 3.0 port is enumerated, checking x86_apple_machine and scanning hc->handle in usb_acpi_apple_get_location() does not depend on port enumeration order and avoids hardcoding \_SB.PCI0.XHC1 in usb_acpi_register().

[1] https://lore.kernel.org/r/2026112338-pummel-hyperlink-2957@gregkh

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Atharva Tiwari
Quoted 22 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 SSP 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 SSP is only available
on internal USB controller, and even only new intel macs has SSP method, so we check for SSP and XHC1 before using apple's methods.

Quoted 8 lines

---
Changes in v2:
- Nulled location provided by _PLD in apple_get_location
- Fixed typos.

Link to v1:
https://lore.kernel.org/all/[email protected]/

Please slow down, there is no rush. One patch per 24 hours max.

Quoted 17 lines
 drivers/usb/core/usb-acpi.c | 74 +++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
drivers/usb/core/usb-acpi.c +1 -0
index 489dbdc96..403c2ff44 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"
 

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

Quoted 9 lines
drivers/usb/core/usb-acpi.c +6 -0
@@ -199,6 +200,69 @@ 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;

Again, why is this needed and not just calculated at the one place you actually use it?

thanks,

greg k-h

#3 lore raw
Quoted 1 line

Please slow down, there is no rush. One patch per 24 hours max.

K, i did not know this, sorry.

Quoted 1 line

Again, why is this needed and not just calculated at the one place you

actually use it?

So i use it here:

static struct acpi_device * usb_acpi_find_companion_for_port(struct usb_port *port_dev)

{
        struct acpi_device *adev;
        
        adev = usb_acpi_get_companion_for_port(port_dev);
        if (!adev)
                return NULL; 
        
        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;
}

And its calculated it here:
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;
}

so its used PER port, so if there are 7 HS and 6 SS ports

And you suggest that i calculated at the one place i use it, so i would have to walk the acpi namespace 11 times in that scenario

instead i can just cache it in a global variable like is_apple_location so i only have to walk the acpi namespace 1 time.