1
2
3
4
5
6
7
8
9#include <linux/acpi.h>
10
11#include "tb.h"
12
13static acpi_status tb_acpi_add_link(acpi_handle handle, u32 level, void *data,
14 void **return_value)
15{
16 struct fwnode_reference_args args;
17 struct fwnode_handle *fwnode;
18 struct tb_nhi *nhi = data;
19 struct acpi_device *adev;
20 struct pci_dev *pdev;
21 struct device *dev;
22 int ret;
23
24 if (acpi_bus_get_device(handle, &adev))
25 return AE_OK;
26
27 fwnode = acpi_fwnode_handle(adev);
28 ret = fwnode_property_get_reference_args(fwnode, "usb4-host-interface",
29 NULL, 0, 0, &args);
30 if (ret)
31 return AE_OK;
32
33
34 if (nhi->pdev->dev.fwnode != args.fwnode)
35 goto out_put;
36
37
38
39
40
41
42 dev = acpi_get_first_physical_node(adev);
43 while (!dev) {
44 adev = adev->parent;
45 if (!adev)
46 break;
47 dev = acpi_get_first_physical_node(adev);
48 }
49
50 if (!dev)
51 goto out_put;
52
53
54
55
56
57
58
59 while (dev && !dev_is_pci(dev))
60 dev = dev->parent;
61
62 if (!dev)
63 goto out_put;
64
65
66
67
68
69
70 pdev = to_pci_dev(dev);
71 if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI ||
72 (pci_is_pcie(pdev) &&
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;
76
77 link = device_link_add(&pdev->dev, &nhi->pdev->dev,
78 DL_FLAG_AUTOREMOVE_SUPPLIER |
79 DL_FLAG_PM_RUNTIME);
80 if (link) {
81 dev_dbg(&nhi->pdev->dev, "created link from %s\n",
82 dev_name(&pdev->dev));
83 } else {
84 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
85 dev_name(&pdev->dev));
86 }
87 }
88
89out_put:
90 fwnode_handle_put(args.fwnode);
91 return AE_OK;
92}
93
94
95
96
97
98
99
100
101
102void tb_acpi_add_links(struct tb_nhi *nhi)
103{
104 acpi_status status;
105
106 if (!has_acpi_companion(&nhi->pdev->dev))
107 return;
108
109
110
111
112
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");
117}
118
119
120
121
122
123
124
125
126bool tb_acpi_is_native(void)
127{
128 return osc_sb_native_usb4_support_confirmed &&
129 osc_sb_native_usb4_control;
130}
131
132
133
134
135
136
137
138bool tb_acpi_may_tunnel_usb3(void)
139{
140 if (tb_acpi_is_native())
141 return osc_sb_native_usb4_control & OSC_USB_USB3_TUNNELING;
142 return true;
143}
144
145
146
147
148
149
150
151bool tb_acpi_may_tunnel_dp(void)
152{
153 if (tb_acpi_is_native())
154 return osc_sb_native_usb4_control & OSC_USB_DP_TUNNELING;
155 return true;
156}
157
158
159
160
161
162
163
164bool tb_acpi_may_tunnel_pcie(void)
165{
166 if (tb_acpi_is_native())
167 return osc_sb_native_usb4_control & OSC_USB_PCIE_TUNNELING;
168 return true;
169}
170
171
172
173
174
175
176
177bool tb_acpi_is_xdomain_allowed(void)
178{
179 if (tb_acpi_is_native())
180 return osc_sb_native_usb4_control & OSC_USB_XDOMAIN;
181 return true;
182}
183
184
185static const guid_t retimer_dsm_guid =
186 GUID_INIT(0xe0053122, 0x795b, 0x4122,
187 0x8a, 0x5e, 0x57, 0xbe, 0x1d, 0x26, 0xac, 0xb3);
188
189#define RETIMER_DSM_QUERY_ONLINE_STATE 1
190#define RETIMER_DSM_SET_ONLINE_STATE 2
191
192static int tb_acpi_retimer_set_power(struct tb_port *port, bool power)
193{
194 struct usb4_port *usb4 = port->usb4;
195 union acpi_object argv4[2];
196 struct acpi_device *adev;
197 union acpi_object *obj;
198 int ret;
199
200 if (!usb4->can_offline)
201 return 0;
202
203 adev = ACPI_COMPANION(&usb4->dev);
204 if (WARN_ON(!adev))
205 return 0;
206
207
208 obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
209 RETIMER_DSM_QUERY_ONLINE_STATE, NULL,
210 ACPI_TYPE_INTEGER);
211 if (!obj) {
212 tb_port_warn(port, "ACPI: query online _DSM failed\n");
213 return -EIO;
214 }
215
216 ret = obj->integer.value;
217 ACPI_FREE(obj);
218
219 if (power == ret)
220 return 0;
221
222 tb_port_dbg(port, "ACPI: calling _DSM to power %s retimers\n",
223 power ? "on" : "off");
224
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;
230
231 obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
232 RETIMER_DSM_SET_ONLINE_STATE, argv4,
233 ACPI_TYPE_INTEGER);
234 if (!obj) {
235 tb_port_warn(port,
236 "ACPI: set online state _DSM evaluation failed\n");
237 return -EIO;
238 }
239
240 ret = obj->integer.value;
241 ACPI_FREE(obj);
242
243 if (ret >= 0) {
244 if (power)
245 return ret == 1 ? 0 : -EBUSY;
246 return 0;
247 }
248
249 tb_port_warn(port, "ACPI: set online state _DSM failed with error %d\n", ret);
250 return -EIO;
251}
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267int tb_acpi_power_on_retimers(struct tb_port *port)
268{
269 return tb_acpi_retimer_set_power(port, true);
270}
271
272
273
274
275
276
277
278
279
280
281int tb_acpi_power_off_retimers(struct tb_port *port)
282{
283 return tb_acpi_retimer_set_power(port, false);
284}
285
286static bool tb_acpi_bus_match(struct device *dev)
287{
288 return tb_is_switch(dev) || tb_is_usb4_port_device(dev);
289}
290
291static struct acpi_device *tb_acpi_find_port(struct acpi_device *adev,
292 const struct tb_port *port)
293{
294 struct acpi_device *port_adev;
295
296 if (!adev)
297 return NULL;
298
299
300
301
302
303 list_for_each_entry(port_adev, &adev->children, node) {
304 if (acpi_device_adr(port_adev) == port->port)
305 return port_adev;
306 }
307
308 return NULL;
309}
310
311static struct acpi_device *tb_acpi_switch_find_companion(struct tb_switch *sw)
312{
313 struct acpi_device *adev = NULL;
314 struct tb_switch *parent_sw;
315
316 parent_sw = tb_switch_parent(sw);
317 if (parent_sw) {
318 struct tb_port *port = tb_port_at(tb_route(sw), parent_sw);
319 struct acpi_device *port_adev;
320
321 port_adev = tb_acpi_find_port(ACPI_COMPANION(&parent_sw->dev), port);
322 if (port_adev)
323 adev = acpi_find_child_device(port_adev, 0, false);
324 } else {
325 struct tb_nhi *nhi = sw->tb->nhi;
326 struct acpi_device *parent_adev;
327
328 parent_adev = ACPI_COMPANION(&nhi->pdev->dev);
329 if (parent_adev)
330 adev = acpi_find_child_device(parent_adev, 0, false);
331 }
332
333 return adev;
334}
335
336static struct acpi_device *tb_acpi_find_companion(struct device *dev)
337{
338
339
340
341
342
343
344
345
346
347
348
349
350
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);
356 return NULL;
357}
358
359static void tb_acpi_setup(struct device *dev)
360{
361 struct acpi_device *adev = ACPI_COMPANION(dev);
362 struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
363
364 if (!adev || !usb4)
365 return;
366
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;
371}
372
373static 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,
378};
379
380int tb_acpi_init(void)
381{
382 return register_acpi_bus_type(&tb_acpi_bus);
383}
384
385void tb_acpi_exit(void)
386{
387 unregister_acpi_bus_type(&tb_acpi_bus);
388}
389