1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2020, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
9 #include <linux/acpi.h>
13 static acpi_status tb_acpi_add_link(acpi_handle handle, u32 level, void *data,
16 struct fwnode_reference_args args;
17 struct fwnode_handle *fwnode;
18 struct tb_nhi *nhi = data;
19 struct acpi_device *adev;
24 if (acpi_bus_get_device(handle, &adev))
27 fwnode = acpi_fwnode_handle(adev);
28 ret = fwnode_property_get_reference_args(fwnode, "usb4-host-interface",
33 /* It needs to reference this NHI */
34 if (nhi->pdev->dev.fwnode != args.fwnode)
38 * Try to find physical device walking upwards to the hierarcy.
39 * We need to do this because the xHCI driver might not yet be
40 * bound so the USB3 SuperSpeed ports are not yet created.
42 dev = acpi_get_first_physical_node(adev);
47 dev = acpi_get_first_physical_node(adev);
54 * Check that the device is PCIe. This is because USB3
55 * SuperSpeed ports have this property and they are not power
56 * managed with the xHCI and the SuperSpeed hub so we create the
57 * link from xHCI instead.
59 while (dev && !dev_is_pci(dev))
66 * Check that this actually matches the type of device we
67 * expect. It should either be xHCI or PCIe root/downstream
70 pdev = to_pci_dev(dev);
71 if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI ||
73 (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
74 pci_pcie_type(pdev) == PCI_EXP_TYPE_DOWNSTREAM))) {
75 const struct device_link *link;
77 link = device_link_add(&pdev->dev, &nhi->pdev->dev,
78 DL_FLAG_AUTOREMOVE_SUPPLIER |
81 dev_dbg(&nhi->pdev->dev, "created link from %s\n",
82 dev_name(&pdev->dev));
84 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
85 dev_name(&pdev->dev));
90 fwnode_handle_put(args.fwnode);
95 * tb_acpi_add_links() - Add device links based on ACPI description
96 * @nhi: Pointer to NHI
98 * Goes over ACPI namespace finding tunneled ports that reference to
99 * @nhi ACPI node. For each reference a device link is added. The link
100 * is automatically removed by the driver core.
102 void tb_acpi_add_links(struct tb_nhi *nhi)
106 if (!has_acpi_companion(&nhi->pdev->dev))
110 * Find all devices that have usb4-host-controller interface
111 * property that references to this NHI.
113 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 32,
114 tb_acpi_add_link, NULL, nhi, NULL);
115 if (ACPI_FAILURE(status))
116 dev_warn(&nhi->pdev->dev, "failed to enumerate tunneled ports\n");
120 * tb_acpi_is_native() - Did the platform grant native TBT/USB4 control
122 * Returns %true if the platform granted OS native control over
123 * TBT/USB4. In this case software based connection manager can be used,
124 * otherwise there is firmware based connection manager running.
126 bool tb_acpi_is_native(void)
128 return osc_sb_native_usb4_support_confirmed &&
129 osc_sb_native_usb4_control;
133 * tb_acpi_may_tunnel_usb3() - Is USB3 tunneling allowed by the platform
135 * When software based connection manager is used, this function
136 * returns %true if platform allows native USB3 tunneling.
138 bool tb_acpi_may_tunnel_usb3(void)
140 if (tb_acpi_is_native())
141 return osc_sb_native_usb4_control & OSC_USB_USB3_TUNNELING;
146 * tb_acpi_may_tunnel_dp() - Is DisplayPort tunneling allowed by the platform
148 * When software based connection manager is used, this function
149 * returns %true if platform allows native DP tunneling.
151 bool tb_acpi_may_tunnel_dp(void)
153 if (tb_acpi_is_native())
154 return osc_sb_native_usb4_control & OSC_USB_DP_TUNNELING;
159 * tb_acpi_may_tunnel_pcie() - Is PCIe tunneling allowed by the platform
161 * When software based connection manager is used, this function
162 * returns %true if platform allows native PCIe tunneling.
164 bool tb_acpi_may_tunnel_pcie(void)
166 if (tb_acpi_is_native())
167 return osc_sb_native_usb4_control & OSC_USB_PCIE_TUNNELING;
172 * tb_acpi_is_xdomain_allowed() - Are XDomain connections allowed
174 * When software based connection manager is used, this function
175 * returns %true if platform allows XDomain connections.
177 bool tb_acpi_is_xdomain_allowed(void)
179 if (tb_acpi_is_native())
180 return osc_sb_native_usb4_control & OSC_USB_XDOMAIN;
184 /* UUID for retimer _DSM: e0053122-795b-4122-8a5e-57be1d26acb3 */
185 static const guid_t retimer_dsm_guid =
186 GUID_INIT(0xe0053122, 0x795b, 0x4122,
187 0x8a, 0x5e, 0x57, 0xbe, 0x1d, 0x26, 0xac, 0xb3);
189 #define RETIMER_DSM_QUERY_ONLINE_STATE 1
190 #define RETIMER_DSM_SET_ONLINE_STATE 2
192 static int tb_acpi_retimer_set_power(struct tb_port *port, bool power)
194 struct usb4_port *usb4 = port->usb4;
195 union acpi_object argv4[2];
196 struct acpi_device *adev;
197 union acpi_object *obj;
200 if (!usb4->can_offline)
203 adev = ACPI_COMPANION(&usb4->dev);
207 /* Check if we are already powered on (and in correct mode) */
208 obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
209 RETIMER_DSM_QUERY_ONLINE_STATE, NULL,
212 tb_port_warn(port, "ACPI: query online _DSM failed\n");
216 ret = obj->integer.value;
222 tb_port_dbg(port, "ACPI: calling _DSM to power %s retimers\n",
223 power ? "on" : "off");
225 argv4[0].type = ACPI_TYPE_PACKAGE;
226 argv4[0].package.count = 1;
227 argv4[0].package.elements = &argv4[1];
228 argv4[1].integer.type = ACPI_TYPE_INTEGER;
229 argv4[1].integer.value = power;
231 obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
232 RETIMER_DSM_SET_ONLINE_STATE, argv4,
236 "ACPI: set online state _DSM evaluation failed\n");
240 ret = obj->integer.value;
245 return ret == 1 ? 0 : -EBUSY;
249 tb_port_warn(port, "ACPI: set online state _DSM failed with error %d\n", ret);
254 * tb_acpi_power_on_retimers() - Call platform to power on retimers
257 * Calls platform to turn on power to all retimers behind this USB4
258 * port. After this function returns successfully the caller can
259 * continue with the normal retimer flows (as specified in the USB4
260 * spec). Note if this returns %-EBUSY it means the type-C port is in
261 * non-USB4/TBT mode (there is non-USB4/TBT device connected).
263 * This should only be called if the USB4/TBT link is not up.
265 * Returns %0 on success.
267 int tb_acpi_power_on_retimers(struct tb_port *port)
269 return tb_acpi_retimer_set_power(port, true);
273 * tb_acpi_power_off_retimers() - Call platform to power off retimers
276 * This is the opposite of tb_acpi_power_on_retimers(). After returning
277 * successfully the normal operations with the @port can continue.
279 * Returns %0 on success.
281 int tb_acpi_power_off_retimers(struct tb_port *port)
283 return tb_acpi_retimer_set_power(port, false);
286 static bool tb_acpi_bus_match(struct device *dev)
288 return tb_is_switch(dev) || tb_is_usb4_port_device(dev);
291 static struct acpi_device *tb_acpi_find_port(struct acpi_device *adev,
292 const struct tb_port *port)
294 struct acpi_device *port_adev;
300 * Device routers exists under the downstream facing USB4 port
301 * of the parent router. Their _ADR is always 0.
303 list_for_each_entry(port_adev, &adev->children, node) {
304 if (acpi_device_adr(port_adev) == port->port)
311 static struct acpi_device *tb_acpi_switch_find_companion(struct tb_switch *sw)
313 struct acpi_device *adev = NULL;
314 struct tb_switch *parent_sw;
316 parent_sw = tb_switch_parent(sw);
318 struct tb_port *port = tb_port_at(tb_route(sw), parent_sw);
319 struct acpi_device *port_adev;
321 port_adev = tb_acpi_find_port(ACPI_COMPANION(&parent_sw->dev), port);
323 adev = acpi_find_child_device(port_adev, 0, false);
325 struct tb_nhi *nhi = sw->tb->nhi;
326 struct acpi_device *parent_adev;
328 parent_adev = ACPI_COMPANION(&nhi->pdev->dev);
330 adev = acpi_find_child_device(parent_adev, 0, false);
336 static struct acpi_device *tb_acpi_find_companion(struct device *dev)
339 * The Thunderbolt/USB4 hierarchy looks like following:
342 * Device (HR) // Host router _ADR == 0
343 * Device (DFP0) // Downstream port _ADR == lane 0 adapter
344 * Device (DR) // Device router _ADR == 0
345 * Device (UFP) // Upstream port _ADR == lane 0 adapter
346 * Device (DFP1) // Downstream port _ADR == lane 0 adapter number
348 * At the moment we bind the host router to the corresponding
351 if (tb_is_switch(dev))
352 return tb_acpi_switch_find_companion(tb_to_switch(dev));
353 else if (tb_is_usb4_port_device(dev))
354 return tb_acpi_find_port(ACPI_COMPANION(dev->parent),
355 tb_to_usb4_port_device(dev)->port);
359 static void tb_acpi_setup(struct device *dev)
361 struct acpi_device *adev = ACPI_COMPANION(dev);
362 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
367 if (acpi_check_dsm(adev->handle, &retimer_dsm_guid, 1,
368 BIT(RETIMER_DSM_QUERY_ONLINE_STATE) |
369 BIT(RETIMER_DSM_SET_ONLINE_STATE)))
370 usb4->can_offline = true;
373 static struct acpi_bus_type tb_acpi_bus = {
374 .name = "thunderbolt",
375 .match = tb_acpi_bus_match,
376 .find_companion = tb_acpi_find_companion,
377 .setup = tb_acpi_setup,
380 int tb_acpi_init(void)
382 return register_acpi_bus_type(&tb_acpi_bus);
385 void tb_acpi_exit(void)
387 unregister_acpi_bus_type(&tb_acpi_bus);