acpi.c 10 KB

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