acpi.c 9.8 KB

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