Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

thunderbolt: Add support for ACPI _DSM to power on/off retimers

Typically retimers can be accessed only when the USB4 link is up (e.g
there is a cable connected). However, sometimes it is useful to be able
to access retimers even if there is nothing connected to the USB4 port.
For instance we may still want to be able to upgrade the retimer NVM
firmware even if the user does not have any USB4 devices. This is
something that USB4 spec leaves to implementers.

In case of ACPI based systems, we can support this by providing a
special _DSM method under each USB4 port. This _DSM can be used to turn
on power to on-board retimers (and cycle it through different modes so
that the sideband becomes usable).

This patch adds support for this _DSM and makes the functionality
available to the rest of the driver through tb_acpi_power_[on|off]_retimers().

Signed-off-by: Rajmohan Mani <rajmohan.mani@intel.com>
Co-developed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Rajmohan Mani and committed by
Mika Westerberg
ccc5cb8a cae5f515

+225 -3
+206
drivers/thunderbolt/acpi.c
··· 180 180 return osc_sb_native_usb4_control & OSC_USB_XDOMAIN; 181 181 return true; 182 182 } 183 + 184 + /* UUID for retimer _DSM: e0053122-795b-4122-8a5e-57be1d26acb3 */ 185 + static 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 + 192 + static 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 + /* Check if we are already powered on (and in correct mode) */ 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 + * tb_acpi_power_on_retimers() - Call platform to power on retimers 255 + * @port: USB4 port 256 + * 257 + * Calls platform to turn on power to all retimers behind this USB4 258 + * port. After this function returns successfully the caller can 259 + * continue with the normal retimer flows (as specified in the USB4 260 + * spec). Note if this returns %-EBUSY it means the type-C port is in 261 + * non-USB4/TBT mode (there is non-USB4/TBT device connected). 262 + * 263 + * This should only be called if the USB4/TBT link is not up. 264 + * 265 + * Returns %0 on success. 266 + */ 267 + int tb_acpi_power_on_retimers(struct tb_port *port) 268 + { 269 + return tb_acpi_retimer_set_power(port, true); 270 + } 271 + 272 + /** 273 + * tb_acpi_power_off_retimers() - Call platform to power off retimers 274 + * @port: USB4 port 275 + * 276 + * This is the opposite of tb_acpi_power_on_retimers(). After returning 277 + * successfully the normal operations with the @port can continue. 278 + * 279 + * Returns %0 on success. 280 + */ 281 + int tb_acpi_power_off_retimers(struct tb_port *port) 282 + { 283 + return tb_acpi_retimer_set_power(port, false); 284 + } 285 + 286 + static bool tb_acpi_bus_match(struct device *dev) 287 + { 288 + return tb_is_switch(dev) || tb_is_usb4_port_device(dev); 289 + } 290 + 291 + static 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 + * Device routers exists under the downstream facing USB4 port 301 + * of the parent router. Their _ADR is always 0. 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 + 311 + static 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 + 336 + static struct acpi_device *tb_acpi_find_companion(struct device *dev) 337 + { 338 + /* 339 + * The Thunderbolt/USB4 hierarchy looks like following: 340 + * 341 + * Device (NHI) 342 + * Device (HR) // Host router _ADR == 0 343 + * Device (DFP0) // Downstream port _ADR == lane 0 adapter 344 + * Device (DR) // Device router _ADR == 0 345 + * Device (UFP) // Upstream port _ADR == lane 0 adapter 346 + * Device (DFP1) // Downstream port _ADR == lane 0 adapter number 347 + * 348 + * At the moment we bind the host router to the corresponding 349 + * Linux device. 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 + 359 + static 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 + 373 + static 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 + 380 + int tb_acpi_init(void) 381 + { 382 + return register_acpi_bus_type(&tb_acpi_bus); 383 + } 384 + 385 + void tb_acpi_exit(void) 386 + { 387 + unregister_acpi_bus_type(&tb_acpi_bus); 388 + }
+6 -3
drivers/thunderbolt/domain.c
··· 881 881 int ret; 882 882 883 883 tb_test_init(); 884 - 885 884 tb_debugfs_init(); 885 + tb_acpi_init(); 886 + 886 887 ret = tb_xdomain_init(); 887 888 if (ret) 888 - goto err_debugfs; 889 + goto err_acpi; 889 890 ret = bus_register(&tb_bus_type); 890 891 if (ret) 891 892 goto err_xdomain; ··· 895 894 896 895 err_xdomain: 897 896 tb_xdomain_exit(); 898 - err_debugfs: 897 + err_acpi: 898 + tb_acpi_exit(); 899 899 tb_debugfs_exit(); 900 900 tb_test_exit(); 901 901 ··· 909 907 ida_destroy(&tb_domain_ida); 910 908 tb_nvm_exit(); 911 909 tb_xdomain_exit(); 910 + tb_acpi_exit(); 912 911 tb_debugfs_exit(); 913 912 tb_test_exit(); 914 913 }
+13
drivers/thunderbolt/tb.h
··· 247 247 * struct usb4_port - USB4 port device 248 248 * @dev: Device for the port 249 249 * @port: Pointer to the lane 0 adapter 250 + * @can_offline: Does the port have necessary platform support to moved 251 + * it into offline mode and back 250 252 */ 251 253 struct usb4_port { 252 254 struct device dev; 253 255 struct tb_port *port; 256 + bool can_offline; 254 257 }; 255 258 256 259 /** ··· 1116 1113 bool tb_acpi_may_tunnel_dp(void); 1117 1114 bool tb_acpi_may_tunnel_pcie(void); 1118 1115 bool tb_acpi_is_xdomain_allowed(void); 1116 + 1117 + int tb_acpi_init(void); 1118 + void tb_acpi_exit(void); 1119 + int tb_acpi_power_on_retimers(struct tb_port *port); 1120 + int tb_acpi_power_off_retimers(struct tb_port *port); 1119 1121 #else 1120 1122 static inline void tb_acpi_add_links(struct tb_nhi *nhi) { } 1121 1123 ··· 1129 1121 static inline bool tb_acpi_may_tunnel_dp(void) { return true; } 1130 1122 static inline bool tb_acpi_may_tunnel_pcie(void) { return true; } 1131 1123 static inline bool tb_acpi_is_xdomain_allowed(void) { return true; } 1124 + 1125 + static inline int tb_acpi_init(void) { return 0; } 1126 + static inline void tb_acpi_exit(void) { } 1127 + static inline int tb_acpi_power_on_retimers(struct tb_port *port) { return 0; } 1128 + static inline int tb_acpi_power_off_retimers(struct tb_port *port) { return 0; } 1132 1129 #endif 1133 1130 1134 1131 #ifdef CONFIG_DEBUG_FS