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