acpi.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 **return_value)
  13. {
  14. struct fwnode_reference_args args;
  15. struct fwnode_handle *fwnode;
  16. struct tb_nhi *nhi = data;
  17. struct acpi_device *adev;
  18. struct pci_dev *pdev;
  19. struct device *dev;
  20. int ret;
  21. if (acpi_bus_get_device(handle, &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 (nhi->pdev->dev.fwnode != 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 = adev->parent;
  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. }