0001
0002
0003
0004
0005
0006
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
0035 if (dev_fwnode(&nhi->pdev->dev) != args.fwnode)
0036 goto out_put;
0037
0038
0039
0040
0041
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
0056
0057
0058
0059
0060 while (dev && !dev_is_pci(dev))
0061 dev = dev->parent;
0062
0063 if (!dev)
0064 goto out_put;
0065
0066
0067
0068
0069
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
0080
0081
0082
0083
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
0109
0110
0111
0112
0113
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
0124
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
0134
0135
0136
0137
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
0147
0148
0149
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
0160
0161
0162
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
0173
0174
0175
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
0186
0187
0188
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
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
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
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
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
0287
0288
0289
0290
0291
0292
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
0311
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
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
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 }