Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * ACPI support
0004  *
0005  * Copyright (C) 2020, Intel Corporation
0006  * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
0007  */
0008 
0009 #include <linux/acpi.h>
0010 #include <linux/pm_runtime.h>
0011 
0012 #include "tb.h"
0013 
0014 static acpi_status tb_acpi_add_link(acpi_handle handle, u32 level, void *data,
0015                     void **return_value)
0016 {
0017     struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
0018     struct fwnode_reference_args args;
0019     struct fwnode_handle *fwnode;
0020     struct tb_nhi *nhi = data;
0021     struct pci_dev *pdev;
0022     struct device *dev;
0023     int ret;
0024 
0025     if (!adev)
0026         return AE_OK;
0027 
0028     fwnode = acpi_fwnode_handle(adev);
0029     ret = fwnode_property_get_reference_args(fwnode, "usb4-host-interface",
0030                          NULL, 0, 0, &args);
0031     if (ret)
0032         return AE_OK;
0033 
0034     /* It needs to reference this NHI */
0035     if (dev_fwnode(&nhi->pdev->dev) != args.fwnode)
0036         goto out_put;
0037 
0038     /*
0039      * Try to find physical device walking upwards to the hierarcy.
0040      * We need to do this because the xHCI driver might not yet be
0041      * bound so the USB3 SuperSpeed ports are not yet created.
0042      */
0043     dev = acpi_get_first_physical_node(adev);
0044     while (!dev) {
0045         adev = adev->parent;
0046         if (!adev)
0047             break;
0048         dev = acpi_get_first_physical_node(adev);
0049     }
0050 
0051     if (!dev)
0052         goto out_put;
0053 
0054     /*
0055      * Check that the device is PCIe. This is because USB3
0056      * SuperSpeed ports have this property and they are not power
0057      * managed with the xHCI and the SuperSpeed hub so we create the
0058      * link from xHCI instead.
0059      */
0060     while (dev && !dev_is_pci(dev))
0061         dev = dev->parent;
0062 
0063     if (!dev)
0064         goto out_put;
0065 
0066     /*
0067      * Check that this actually matches the type of device we
0068      * expect. It should either be xHCI or PCIe root/downstream
0069      * port.
0070      */
0071     pdev = to_pci_dev(dev);
0072     if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI ||
0073         (pci_is_pcie(pdev) &&
0074         (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
0075          pci_pcie_type(pdev) == PCI_EXP_TYPE_DOWNSTREAM))) {
0076         const struct device_link *link;
0077 
0078         /*
0079          * Make them both active first to make sure the NHI does
0080          * not runtime suspend before the consumer. The
0081          * pm_runtime_put() below then allows the consumer to
0082          * runtime suspend again (which then allows NHI runtime
0083          * suspend too now that the device link is established).
0084          */
0085         pm_runtime_get_sync(&pdev->dev);
0086 
0087         link = device_link_add(&pdev->dev, &nhi->pdev->dev,
0088                        DL_FLAG_AUTOREMOVE_SUPPLIER |
0089                        DL_FLAG_RPM_ACTIVE |
0090                        DL_FLAG_PM_RUNTIME);
0091         if (link) {
0092             dev_dbg(&nhi->pdev->dev, "created link from %s\n",
0093                 dev_name(&pdev->dev));
0094         } else {
0095             dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
0096                  dev_name(&pdev->dev));
0097         }
0098 
0099         pm_runtime_put(&pdev->dev);
0100     }
0101 
0102 out_put:
0103     fwnode_handle_put(args.fwnode);
0104     return AE_OK;
0105 }
0106 
0107 /**
0108  * tb_acpi_add_links() - Add device links based on ACPI description
0109  * @nhi: Pointer to NHI
0110  *
0111  * Goes over ACPI namespace finding tunneled ports that reference to
0112  * @nhi ACPI node. For each reference a device link is added. The link
0113  * is automatically removed by the driver core.
0114  */
0115 void tb_acpi_add_links(struct tb_nhi *nhi)
0116 {
0117     acpi_status status;
0118 
0119     if (!has_acpi_companion(&nhi->pdev->dev))
0120         return;
0121 
0122     /*
0123      * Find all devices that have usb4-host-controller interface
0124      * property that references to this NHI.
0125      */
0126     status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 32,
0127                      tb_acpi_add_link, NULL, nhi, NULL);
0128     if (ACPI_FAILURE(status))
0129         dev_warn(&nhi->pdev->dev, "failed to enumerate tunneled ports\n");
0130 }
0131 
0132 /**
0133  * tb_acpi_is_native() - Did the platform grant native TBT/USB4 control
0134  *
0135  * Returns %true if the platform granted OS native control over
0136  * TBT/USB4. In this case software based connection manager can be used,
0137  * otherwise there is firmware based connection manager running.
0138  */
0139 bool tb_acpi_is_native(void)
0140 {
0141     return osc_sb_native_usb4_support_confirmed &&
0142            osc_sb_native_usb4_control;
0143 }
0144 
0145 /**
0146  * tb_acpi_may_tunnel_usb3() - Is USB3 tunneling allowed by the platform
0147  *
0148  * When software based connection manager is used, this function
0149  * returns %true if platform allows native USB3 tunneling.
0150  */
0151 bool tb_acpi_may_tunnel_usb3(void)
0152 {
0153     if (tb_acpi_is_native())
0154         return osc_sb_native_usb4_control & OSC_USB_USB3_TUNNELING;
0155     return true;
0156 }
0157 
0158 /**
0159  * tb_acpi_may_tunnel_dp() - Is DisplayPort tunneling allowed by the platform
0160  *
0161  * When software based connection manager is used, this function
0162  * returns %true if platform allows native DP tunneling.
0163  */
0164 bool tb_acpi_may_tunnel_dp(void)
0165 {
0166     if (tb_acpi_is_native())
0167         return osc_sb_native_usb4_control & OSC_USB_DP_TUNNELING;
0168     return true;
0169 }
0170 
0171 /**
0172  * tb_acpi_may_tunnel_pcie() - Is PCIe tunneling allowed by the platform
0173  *
0174  * When software based connection manager is used, this function
0175  * returns %true if platform allows native PCIe tunneling.
0176  */
0177 bool tb_acpi_may_tunnel_pcie(void)
0178 {
0179     if (tb_acpi_is_native())
0180         return osc_sb_native_usb4_control & OSC_USB_PCIE_TUNNELING;
0181     return true;
0182 }
0183 
0184 /**
0185  * tb_acpi_is_xdomain_allowed() - Are XDomain connections allowed
0186  *
0187  * When software based connection manager is used, this function
0188  * returns %true if platform allows XDomain connections.
0189  */
0190 bool tb_acpi_is_xdomain_allowed(void)
0191 {
0192     if (tb_acpi_is_native())
0193         return osc_sb_native_usb4_control & OSC_USB_XDOMAIN;
0194     return true;
0195 }
0196 
0197 /* UUID for retimer _DSM: e0053122-795b-4122-8a5e-57be1d26acb3 */
0198 static const guid_t retimer_dsm_guid =
0199     GUID_INIT(0xe0053122, 0x795b, 0x4122,
0200           0x8a, 0x5e, 0x57, 0xbe, 0x1d, 0x26, 0xac, 0xb3);
0201 
0202 #define RETIMER_DSM_QUERY_ONLINE_STATE  1
0203 #define RETIMER_DSM_SET_ONLINE_STATE    2
0204 
0205 static int tb_acpi_retimer_set_power(struct tb_port *port, bool power)
0206 {
0207     struct usb4_port *usb4 = port->usb4;
0208     union acpi_object argv4[2];
0209     struct acpi_device *adev;
0210     union acpi_object *obj;
0211     int ret;
0212 
0213     if (!usb4->can_offline)
0214         return 0;
0215 
0216     adev = ACPI_COMPANION(&usb4->dev);
0217     if (WARN_ON(!adev))
0218         return 0;
0219 
0220     /* Check if we are already powered on (and in correct mode) */
0221     obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
0222                       RETIMER_DSM_QUERY_ONLINE_STATE, NULL,
0223                       ACPI_TYPE_INTEGER);
0224     if (!obj) {
0225         tb_port_warn(port, "ACPI: query online _DSM failed\n");
0226         return -EIO;
0227     }
0228 
0229     ret = obj->integer.value;
0230     ACPI_FREE(obj);
0231 
0232     if (power == ret)
0233         return 0;
0234 
0235     tb_port_dbg(port, "ACPI: calling _DSM to power %s retimers\n",
0236             power ? "on" : "off");
0237 
0238     argv4[0].type = ACPI_TYPE_PACKAGE;
0239     argv4[0].package.count = 1;
0240     argv4[0].package.elements = &argv4[1];
0241     argv4[1].integer.type = ACPI_TYPE_INTEGER;
0242     argv4[1].integer.value = power;
0243 
0244     obj = acpi_evaluate_dsm_typed(adev->handle, &retimer_dsm_guid, 1,
0245                       RETIMER_DSM_SET_ONLINE_STATE, argv4,
0246                       ACPI_TYPE_INTEGER);
0247     if (!obj) {
0248         tb_port_warn(port,
0249                  "ACPI: set online state _DSM evaluation failed\n");
0250         return -EIO;
0251     }
0252 
0253     ret = obj->integer.value;
0254     ACPI_FREE(obj);
0255 
0256     if (ret >= 0) {
0257         if (power)
0258             return ret == 1 ? 0 : -EBUSY;
0259         return 0;
0260     }
0261 
0262     tb_port_warn(port, "ACPI: set online state _DSM failed with error %d\n", ret);
0263     return -EIO;
0264 }
0265 
0266 /**
0267  * tb_acpi_power_on_retimers() - Call platform to power on retimers
0268  * @port: USB4 port
0269  *
0270  * Calls platform to turn on power to all retimers behind this USB4
0271  * port. After this function returns successfully the caller can
0272  * continue with the normal retimer flows (as specified in the USB4
0273  * spec). Note if this returns %-EBUSY it means the type-C port is in
0274  * non-USB4/TBT mode (there is non-USB4/TBT device connected).
0275  *
0276  * This should only be called if the USB4/TBT link is not up.
0277  *
0278  * Returns %0 on success.
0279  */
0280 int tb_acpi_power_on_retimers(struct tb_port *port)
0281 {
0282     return tb_acpi_retimer_set_power(port, true);
0283 }
0284 
0285 /**
0286  * tb_acpi_power_off_retimers() - Call platform to power off retimers
0287  * @port: USB4 port
0288  *
0289  * This is the opposite of tb_acpi_power_on_retimers(). After returning
0290  * successfully the normal operations with the @port can continue.
0291  *
0292  * Returns %0 on success.
0293  */
0294 int tb_acpi_power_off_retimers(struct tb_port *port)
0295 {
0296     return tb_acpi_retimer_set_power(port, false);
0297 }
0298 
0299 static bool tb_acpi_bus_match(struct device *dev)
0300 {
0301     return tb_is_switch(dev) || tb_is_usb4_port_device(dev);
0302 }
0303 
0304 static struct acpi_device *tb_acpi_switch_find_companion(struct tb_switch *sw)
0305 {
0306     struct acpi_device *adev = NULL;
0307     struct tb_switch *parent_sw;
0308 
0309     /*
0310      * Device routers exists under the downstream facing USB4 port
0311      * of the parent router. Their _ADR is always 0.
0312      */
0313     parent_sw = tb_switch_parent(sw);
0314     if (parent_sw) {
0315         struct tb_port *port = tb_port_at(tb_route(sw), parent_sw);
0316         struct acpi_device *port_adev;
0317 
0318         port_adev = acpi_find_child_by_adr(ACPI_COMPANION(&parent_sw->dev),
0319                            port->port);
0320         if (port_adev)
0321             adev = acpi_find_child_device(port_adev, 0, false);
0322     } else {
0323         struct tb_nhi *nhi = sw->tb->nhi;
0324         struct acpi_device *parent_adev;
0325 
0326         parent_adev = ACPI_COMPANION(&nhi->pdev->dev);
0327         if (parent_adev)
0328             adev = acpi_find_child_device(parent_adev, 0, false);
0329     }
0330 
0331     return adev;
0332 }
0333 
0334 static struct acpi_device *tb_acpi_find_companion(struct device *dev)
0335 {
0336     /*
0337      * The Thunderbolt/USB4 hierarchy looks like following:
0338      *
0339      * Device (NHI)
0340      *   Device (HR)        // Host router _ADR == 0
0341      *      Device (DFP0)       // Downstream port _ADR == lane 0 adapter
0342      *        Device (DR)       // Device router _ADR == 0
0343      *          Device (UFP)    // Upstream port _ADR == lane 0 adapter
0344      *      Device (DFP1)       // Downstream port _ADR == lane 0 adapter number
0345      *
0346      * At the moment we bind the host router to the corresponding
0347      * Linux device.
0348      */
0349     if (tb_is_switch(dev))
0350         return tb_acpi_switch_find_companion(tb_to_switch(dev));
0351     else if (tb_is_usb4_port_device(dev))
0352         return acpi_find_child_by_adr(ACPI_COMPANION(dev->parent),
0353                           tb_to_usb4_port_device(dev)->port->port);
0354     return NULL;
0355 }
0356 
0357 static void tb_acpi_setup(struct device *dev)
0358 {
0359     struct acpi_device *adev = ACPI_COMPANION(dev);
0360     struct usb4_port *usb4 = tb_to_usb4_port_device(dev);
0361 
0362     if (!adev || !usb4)
0363         return;
0364 
0365     if (acpi_check_dsm(adev->handle, &retimer_dsm_guid, 1,
0366                BIT(RETIMER_DSM_QUERY_ONLINE_STATE) |
0367                BIT(RETIMER_DSM_SET_ONLINE_STATE)))
0368         usb4->can_offline = true;
0369 }
0370 
0371 static struct acpi_bus_type tb_acpi_bus = {
0372     .name = "thunderbolt",
0373     .match = tb_acpi_bus_match,
0374     .find_companion = tb_acpi_find_companion,
0375     .setup = tb_acpi_setup,
0376 };
0377 
0378 int tb_acpi_init(void)
0379 {
0380     return register_acpi_bus_type(&tb_acpi_bus);
0381 }
0382 
0383 void tb_acpi_exit(void)
0384 {
0385     unregister_acpi_bus_type(&tb_acpi_bus);
0386 }