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