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