acpi.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.
  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. * Returns %true if the platform granted OS native control over
  100. * TBT/USB4. In this case software based connection manager can be used,
  101. * otherwise there is firmware based connection manager running.
  102. */
  103. bool tb_acpi_is_native(void)
  104. {
  105. return osc_sb_native_usb4_support_confirmed &&
  106. osc_sb_native_usb4_control;
  107. }
  108. /**
  109. * tb_acpi_may_tunnel_usb3() - Is USB3 tunneling allowed by the platform
  110. *
  111. * When software based connection manager is used, this function
  112. * returns %true if platform allows native USB3 tunneling.
  113. */
  114. bool tb_acpi_may_tunnel_usb3(void)
  115. {
  116. if (tb_acpi_is_native())
  117. return osc_sb_native_usb4_control & OSC_USB_USB3_TUNNELING;
  118. return true;
  119. }
  120. /**
  121. * tb_acpi_may_tunnel_dp() - Is DisplayPort tunneling allowed by the platform
  122. *
  123. * When software based connection manager is used, this function
  124. * returns %true if platform allows native DP tunneling.
  125. */
  126. bool tb_acpi_may_tunnel_dp(void)
  127. {
  128. if (tb_acpi_is_native())
  129. return osc_sb_native_usb4_control & OSC_USB_DP_TUNNELING;
  130. return true;
  131. }
  132. /**
  133. * tb_acpi_may_tunnel_pcie() - Is PCIe tunneling allowed by the platform
  134. *
  135. * When software based connection manager is used, this function
  136. * returns %true if platform allows native PCIe tunneling.
  137. */
  138. bool tb_acpi_may_tunnel_pcie(void)
  139. {
  140. if (tb_acpi_is_native())
  141. return osc_sb_native_usb4_control & OSC_USB_PCIE_TUNNELING;
  142. return true;
  143. }
  144. /**
  145. * tb_acpi_is_xdomain_allowed() - Are XDomain connections allowed
  146. *
  147. * When software based connection manager is used, this function
  148. * returns %true if platform allows XDomain connections.
  149. */
  150. bool tb_acpi_is_xdomain_allowed(void)
  151. {
  152. if (tb_acpi_is_native())
  153. return osc_sb_native_usb4_control & OSC_USB_XDOMAIN;
  154. return true;
  155. }
  156. /* UUID for retimer _DSM: e0053122-795b-4122-8a5e-57be1d26acb3 */
  157. static const guid_t retimer_dsm_guid =
  158. GUID_INIT(0xe0053122, 0x795b, 0x4122,
  159. 0x8a, 0x5e, 0x57, 0xbe, 0x1d, 0x26, 0xac, 0xb3);
  160. #define RETIMER_DSM_QUERY_ONLINE_STATE 1
  161. #define RETIMER_DSM_SET_ONLINE_STATE 2
  162. static int tb_acpi_retimer_set_power(struct tb_port *port, bool power)
  163. {
  164. struct usb4_port *usb4 = port->usb4;
  165. union acpi_object argv4[2];
  166. struct acpi_device *adev;
  167. union acpi_object *obj;
  168. int ret;
  169. if (!usb4->can_offline)
  170. return 0;
  171. adev = ACPI_COMPANION(&usb4->dev);
  172. if (WARN_ON(!adev))
  173. return 0;
  174. /* Check if we are already powered on (and in correct mode) */
  175. obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
  176. RETIMER_DSM_QUERY_ONLINE_STATE, NULL,
  177. ACPI_TYPE_INTEGER);
  178. if (!obj) {
  179. tb_port_warn(port, "ACPI: query online _DSM failed\n");
  180. return -EIO;
  181. }
  182. ret = obj->integer.value;
  183. ACPI_FREE(obj);
  184. if (power == ret)
  185. return 0;
  186. tb_port_dbg(port, "ACPI: calling _DSM to power %s retimers\n",
  187. power ? "on" : "off");
  188. argv4[0].type = ACPI_TYPE_PACKAGE;
  189. argv4[0].package.count = 1;
  190. argv4[0].package.elements = &argv4[1];
  191. argv4[1].integer.type = ACPI_TYPE_INTEGER;
  192. argv4[1].integer.value = power;
  193. obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
  194. RETIMER_DSM_SET_ONLINE_STATE, argv4,
  195. ACPI_TYPE_INTEGER);
  196. if (!obj) {
  197. tb_port_warn(port,
  198. "ACPI: set online state _DSM evaluation failed\n");
  199. return -EIO;
  200. }
  201. ret = obj->integer.value;
  202. ACPI_FREE(obj);
  203. if (ret >= 0) {
  204. if (power)
  205. return ret == 1 ? 0 : -EBUSY;
  206. return 0;
  207. }
  208. tb_port_warn(port, "ACPI: set online state _DSM failed with error %d\n", ret);
  209. return -EIO;
  210. }
  211. /**
  212. * tb_acpi_power_on_retimers() - Call platform to power on retimers
  213. * @port: USB4 port
  214. *
  215. * Calls platform to turn on power to all retimers behind this USB4
  216. * port. After this function returns successfully the caller can
  217. * continue with the normal retimer flows (as specified in the USB4
  218. * spec). Note if this returns %-EBUSY it means the type-C port is in
  219. * non-USB4/TBT mode (there is non-USB4/TBT device connected).
  220. *
  221. * This should only be called if the USB4/TBT link is not up.
  222. *
  223. * Returns %0 on success.
  224. */
  225. int tb_acpi_power_on_retimers(struct tb_port *port)
  226. {
  227. return tb_acpi_retimer_set_power(port, true);
  228. }
  229. /**
  230. * tb_acpi_power_off_retimers() - Call platform to power off retimers
  231. * @port: USB4 port
  232. *
  233. * This is the opposite of tb_acpi_power_on_retimers(). After returning
  234. * successfully the normal operations with the @port can continue.
  235. *
  236. * Returns %0 on success.
  237. */
  238. int tb_acpi_power_off_retimers(struct tb_port *port)
  239. {
  240. return tb_acpi_retimer_set_power(port, false);
  241. }
  242. static bool tb_acpi_bus_match(struct device *dev)
  243. {
  244. return tb_is_switch(dev) || tb_is_usb4_port_device(dev);
  245. }
  246. static struct acpi_device *tb_acpi_switch_find_companion(struct tb_switch *sw)
  247. {
  248. struct tb_switch *parent_sw = tb_switch_parent(sw);
  249. struct acpi_device *adev = NULL;
  250. /*
  251. * Device routers exists under the downstream facing USB4 port
  252. * of the parent router. Their _ADR is always 0.
  253. */
  254. if (parent_sw) {
  255. struct tb_port *port = tb_switch_downstream_port(sw);
  256. struct acpi_device *port_adev;
  257. port_adev = acpi_find_child_by_adr(ACPI_COMPANION(&parent_sw->dev),
  258. port->port);
  259. if (port_adev)
  260. adev = acpi_find_child_device(port_adev, 0, false);
  261. } else {
  262. struct tb_nhi *nhi = sw->tb->nhi;
  263. struct acpi_device *parent_adev;
  264. parent_adev = ACPI_COMPANION(&nhi->pdev->dev);
  265. if (parent_adev)
  266. adev = acpi_find_child_device(parent_adev, 0, false);
  267. }
  268. return adev;
  269. }
  270. static struct acpi_device *tb_acpi_find_companion(struct device *dev)
  271. {
  272. /*
  273. * The Thunderbolt/USB4 hierarchy looks like following:
  274. *
  275. * Device (NHI)
  276. * Device (HR) // Host router _ADR == 0
  277. * Device (DFP0) // Downstream port _ADR == lane 0 adapter
  278. * Device (DR) // Device router _ADR == 0
  279. * Device (UFP) // Upstream port _ADR == lane 0 adapter
  280. * Device (DFP1) // Downstream port _ADR == lane 0 adapter number
  281. *
  282. * At the moment we bind the host router to the corresponding
  283. * Linux device.
  284. */
  285. if (tb_is_switch(dev))
  286. return tb_acpi_switch_find_companion(tb_to_switch(dev));
  287. if (tb_is_usb4_port_device(dev))
  288. return acpi_find_child_by_adr(ACPI_COMPANION(dev->parent),
  289. tb_to_usb4_port_device(dev)->port->port);
  290. return NULL;
  291. }
  292. static void tb_acpi_setup(struct device *dev)
  293. {
  294. struct acpi_device *adev = ACPI_COMPANION(dev);
  295. struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
  296. if (!adev || !usb4)
  297. return;
  298. if (acpi_check_dsm(adev->handle, &retimer_dsm_guid, 1,
  299. BIT(RETIMER_DSM_QUERY_ONLINE_STATE) |
  300. BIT(RETIMER_DSM_SET_ONLINE_STATE)))
  301. usb4->can_offline = true;
  302. }
  303. static struct acpi_bus_type tb_acpi_bus = {
  304. .name = "thunderbolt",
  305. .match = tb_acpi_bus_match,
  306. .find_companion = tb_acpi_find_companion,
  307. .setup = tb_acpi_setup,
  308. };
  309. int tb_acpi_init(void)
  310. {
  311. return register_acpi_bus_type(&tb_acpi_bus);
  312. }
  313. void tb_acpi_exit(void)
  314. {
  315. unregister_acpi_bus_type(&tb_acpi_bus);
  316. }