Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * ACPI support
4 *
5 * Copyright (C) 2020, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
7 */
8
9#include <linux/acpi.h>
10#include <linux/pm_runtime.h>
11
12#include "tb.h"
13
14static acpi_status tb_acpi_add_link(acpi_handle handle, u32 level, void *data,
15 void **return_value)
16{
17 struct fwnode_reference_args args;
18 struct fwnode_handle *fwnode;
19 struct tb_nhi *nhi = data;
20 struct acpi_device *adev;
21 struct pci_dev *pdev;
22 struct device *dev;
23 int ret;
24
25 if (acpi_bus_get_device(handle, &adev))
26 return AE_OK;
27
28 fwnode = acpi_fwnode_handle(adev);
29 ret = fwnode_property_get_reference_args(fwnode, "usb4-host-interface",
30 NULL, 0, 0, &args);
31 if (ret)
32 return AE_OK;
33
34 /* It needs to reference this NHI */
35 if (dev_fwnode(&nhi->pdev->dev) != args.fwnode)
36 goto out_put;
37
38 /*
39 * Try to find physical device walking upwards to the hierarcy.
40 * We need to do this because the xHCI driver might not yet be
41 * bound so the USB3 SuperSpeed ports are not yet created.
42 */
43 dev = acpi_get_first_physical_node(adev);
44 while (!dev) {
45 adev = adev->parent;
46 if (!adev)
47 break;
48 dev = acpi_get_first_physical_node(adev);
49 }
50
51 if (!dev)
52 goto out_put;
53
54 /*
55 * Check that the device is PCIe. This is because USB3
56 * SuperSpeed ports have this property and they are not power
57 * managed with the xHCI and the SuperSpeed hub so we create the
58 * link from xHCI instead.
59 */
60 while (dev && !dev_is_pci(dev))
61 dev = dev->parent;
62
63 if (!dev)
64 goto out_put;
65
66 /*
67 * Check that this actually matches the type of device we
68 * expect. It should either be xHCI or PCIe root/downstream
69 * port.
70 */
71 pdev = to_pci_dev(dev);
72 if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI ||
73 (pci_is_pcie(pdev) &&
74 (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
75 pci_pcie_type(pdev) == PCI_EXP_TYPE_DOWNSTREAM))) {
76 const struct device_link *link;
77
78 /*
79 * Make them both active first to make sure the NHI does
80 * not runtime suspend before the consumer. The
81 * pm_runtime_put() below then allows the consumer to
82 * runtime suspend again (which then allows NHI runtime
83 * suspend too now that the device link is established).
84 */
85 pm_runtime_get_sync(&pdev->dev);
86
87 link = device_link_add(&pdev->dev, &nhi->pdev->dev,
88 DL_FLAG_AUTOREMOVE_SUPPLIER |
89 DL_FLAG_RPM_ACTIVE |
90 DL_FLAG_PM_RUNTIME);
91 if (link) {
92 dev_dbg(&nhi->pdev->dev, "created link from %s\n",
93 dev_name(&pdev->dev));
94 } else {
95 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
96 dev_name(&pdev->dev));
97 }
98
99 pm_runtime_put(&pdev->dev);
100 }
101
102out_put:
103 fwnode_handle_put(args.fwnode);
104 return AE_OK;
105}
106
107/**
108 * tb_acpi_add_links() - Add device links based on ACPI description
109 * @nhi: Pointer to NHI
110 *
111 * Goes over ACPI namespace finding tunneled ports that reference to
112 * @nhi ACPI node. For each reference a device link is added. The link
113 * is automatically removed by the driver core.
114 */
115void tb_acpi_add_links(struct tb_nhi *nhi)
116{
117 acpi_status status;
118
119 if (!has_acpi_companion(&nhi->pdev->dev))
120 return;
121
122 /*
123 * Find all devices that have usb4-host-controller interface
124 * property that references to this NHI.
125 */
126 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 32,
127 tb_acpi_add_link, NULL, nhi, NULL);
128 if (ACPI_FAILURE(status))
129 dev_warn(&nhi->pdev->dev, "failed to enumerate tunneled ports\n");
130}
131
132/**
133 * tb_acpi_is_native() - Did the platform grant native TBT/USB4 control
134 *
135 * Returns %true if the platform granted OS native control over
136 * TBT/USB4. In this case software based connection manager can be used,
137 * otherwise there is firmware based connection manager running.
138 */
139bool tb_acpi_is_native(void)
140{
141 return osc_sb_native_usb4_support_confirmed &&
142 osc_sb_native_usb4_control;
143}
144
145/**
146 * tb_acpi_may_tunnel_usb3() - Is USB3 tunneling allowed by the platform
147 *
148 * When software based connection manager is used, this function
149 * returns %true if platform allows native USB3 tunneling.
150 */
151bool tb_acpi_may_tunnel_usb3(void)
152{
153 if (tb_acpi_is_native())
154 return osc_sb_native_usb4_control & OSC_USB_USB3_TUNNELING;
155 return true;
156}
157
158/**
159 * tb_acpi_may_tunnel_dp() - Is DisplayPort tunneling allowed by the platform
160 *
161 * When software based connection manager is used, this function
162 * returns %true if platform allows native DP tunneling.
163 */
164bool tb_acpi_may_tunnel_dp(void)
165{
166 if (tb_acpi_is_native())
167 return osc_sb_native_usb4_control & OSC_USB_DP_TUNNELING;
168 return true;
169}
170
171/**
172 * tb_acpi_may_tunnel_pcie() - Is PCIe tunneling allowed by the platform
173 *
174 * When software based connection manager is used, this function
175 * returns %true if platform allows native PCIe tunneling.
176 */
177bool tb_acpi_may_tunnel_pcie(void)
178{
179 if (tb_acpi_is_native())
180 return osc_sb_native_usb4_control & OSC_USB_PCIE_TUNNELING;
181 return true;
182}
183
184/**
185 * tb_acpi_is_xdomain_allowed() - Are XDomain connections allowed
186 *
187 * When software based connection manager is used, this function
188 * returns %true if platform allows XDomain connections.
189 */
190bool tb_acpi_is_xdomain_allowed(void)
191{
192 if (tb_acpi_is_native())
193 return osc_sb_native_usb4_control & OSC_USB_XDOMAIN;
194 return true;
195}
196
197/* UUID for retimer _DSM: e0053122-795b-4122-8a5e-57be1d26acb3 */
198static const guid_t retimer_dsm_guid =
199 GUID_INIT(0xe0053122, 0x795b, 0x4122,
200 0x8a, 0x5e, 0x57, 0xbe, 0x1d, 0x26, 0xac, 0xb3);
201
202#define RETIMER_DSM_QUERY_ONLINE_STATE 1
203#define RETIMER_DSM_SET_ONLINE_STATE 2
204
205static int tb_acpi_retimer_set_power(struct tb_port *port, bool power)
206{
207 struct usb4_port *usb4 = port->usb4;
208 union acpi_object argv4[2];
209 struct acpi_device *adev;
210 union acpi_object *obj;
211 int ret;
212
213 if (!usb4->can_offline)
214 return 0;
215
216 adev = ACPI_COMPANION(&usb4->dev);
217 if (WARN_ON(!adev))
218 return 0;
219
220 /* Check if we are already powered on (and in correct mode) */
221 obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
222 RETIMER_DSM_QUERY_ONLINE_STATE, NULL,
223 ACPI_TYPE_INTEGER);
224 if (!obj) {
225 tb_port_warn(port, "ACPI: query online _DSM failed\n");
226 return -EIO;
227 }
228
229 ret = obj->integer.value;
230 ACPI_FREE(obj);
231
232 if (power == ret)
233 return 0;
234
235 tb_port_dbg(port, "ACPI: calling _DSM to power %s retimers\n",
236 power ? "on" : "off");
237
238 argv4[0].type = ACPI_TYPE_PACKAGE;
239 argv4[0].package.count = 1;
240 argv4[0].package.elements = &argv4[1];
241 argv4[1].integer.type = ACPI_TYPE_INTEGER;
242 argv4[1].integer.value = power;
243
244 obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
245 RETIMER_DSM_SET_ONLINE_STATE, argv4,
246 ACPI_TYPE_INTEGER);
247 if (!obj) {
248 tb_port_warn(port,
249 "ACPI: set online state _DSM evaluation failed\n");
250 return -EIO;
251 }
252
253 ret = obj->integer.value;
254 ACPI_FREE(obj);
255
256 if (ret >= 0) {
257 if (power)
258 return ret == 1 ? 0 : -EBUSY;
259 return 0;
260 }
261
262 tb_port_warn(port, "ACPI: set online state _DSM failed with error %d\n", ret);
263 return -EIO;
264}
265
266/**
267 * tb_acpi_power_on_retimers() - Call platform to power on retimers
268 * @port: USB4 port
269 *
270 * Calls platform to turn on power to all retimers behind this USB4
271 * port. After this function returns successfully the caller can
272 * continue with the normal retimer flows (as specified in the USB4
273 * spec). Note if this returns %-EBUSY it means the type-C port is in
274 * non-USB4/TBT mode (there is non-USB4/TBT device connected).
275 *
276 * This should only be called if the USB4/TBT link is not up.
277 *
278 * Returns %0 on success.
279 */
280int tb_acpi_power_on_retimers(struct tb_port *port)
281{
282 return tb_acpi_retimer_set_power(port, true);
283}
284
285/**
286 * tb_acpi_power_off_retimers() - Call platform to power off retimers
287 * @port: USB4 port
288 *
289 * This is the opposite of tb_acpi_power_on_retimers(). After returning
290 * successfully the normal operations with the @port can continue.
291 *
292 * Returns %0 on success.
293 */
294int tb_acpi_power_off_retimers(struct tb_port *port)
295{
296 return tb_acpi_retimer_set_power(port, false);
297}
298
299static bool tb_acpi_bus_match(struct device *dev)
300{
301 return tb_is_switch(dev) || tb_is_usb4_port_device(dev);
302}
303
304static struct acpi_device *tb_acpi_find_port(struct acpi_device *adev,
305 const struct tb_port *port)
306{
307 struct acpi_device *port_adev;
308
309 if (!adev)
310 return NULL;
311
312 /*
313 * Device routers exists under the downstream facing USB4 port
314 * of the parent router. Their _ADR is always 0.
315 */
316 list_for_each_entry(port_adev, &adev->children, node) {
317 if (acpi_device_adr(port_adev) == port->port)
318 return port_adev;
319 }
320
321 return NULL;
322}
323
324static struct acpi_device *tb_acpi_switch_find_companion(struct tb_switch *sw)
325{
326 struct acpi_device *adev = NULL;
327 struct tb_switch *parent_sw;
328
329 parent_sw = tb_switch_parent(sw);
330 if (parent_sw) {
331 struct tb_port *port = tb_port_at(tb_route(sw), parent_sw);
332 struct acpi_device *port_adev;
333
334 port_adev = tb_acpi_find_port(ACPI_COMPANION(&parent_sw->dev), port);
335 if (port_adev)
336 adev = acpi_find_child_device(port_adev, 0, false);
337 } else {
338 struct tb_nhi *nhi = sw->tb->nhi;
339 struct acpi_device *parent_adev;
340
341 parent_adev = ACPI_COMPANION(&nhi->pdev->dev);
342 if (parent_adev)
343 adev = acpi_find_child_device(parent_adev, 0, false);
344 }
345
346 return adev;
347}
348
349static struct acpi_device *tb_acpi_find_companion(struct device *dev)
350{
351 /*
352 * The Thunderbolt/USB4 hierarchy looks like following:
353 *
354 * Device (NHI)
355 * Device (HR) // Host router _ADR == 0
356 * Device (DFP0) // Downstream port _ADR == lane 0 adapter
357 * Device (DR) // Device router _ADR == 0
358 * Device (UFP) // Upstream port _ADR == lane 0 adapter
359 * Device (DFP1) // Downstream port _ADR == lane 0 adapter number
360 *
361 * At the moment we bind the host router to the corresponding
362 * Linux device.
363 */
364 if (tb_is_switch(dev))
365 return tb_acpi_switch_find_companion(tb_to_switch(dev));
366 else if (tb_is_usb4_port_device(dev))
367 return tb_acpi_find_port(ACPI_COMPANION(dev->parent),
368 tb_to_usb4_port_device(dev)->port);
369 return NULL;
370}
371
372static void tb_acpi_setup(struct device *dev)
373{
374 struct acpi_device *adev = ACPI_COMPANION(dev);
375 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
376
377 if (!adev || !usb4)
378 return;
379
380 if (acpi_check_dsm(adev->handle, &retimer_dsm_guid, 1,
381 BIT(RETIMER_DSM_QUERY_ONLINE_STATE) |
382 BIT(RETIMER_DSM_SET_ONLINE_STATE)))
383 usb4->can_offline = true;
384}
385
386static struct acpi_bus_type tb_acpi_bus = {
387 .name = "thunderbolt",
388 .match = tb_acpi_bus_match,
389 .find_companion = tb_acpi_find_companion,
390 .setup = tb_acpi_setup,
391};
392
393int tb_acpi_init(void)
394{
395 return register_acpi_bus_type(&tb_acpi_bus);
396}
397
398void tb_acpi_exit(void)
399{
400 unregister_acpi_bus_type(&tb_acpi_bus);
401}