|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | + |
| 3 | +#include "chip.h" |
| 4 | +#include <acpi/acpigen.h> |
| 5 | +#include <console/console.h> |
| 6 | +#include <delay.h> |
| 7 | +#include <device/device.h> |
| 8 | +#include <device/pci.h> |
| 9 | +#include <device/pciexp.h> |
| 10 | +#include <device/pci_ids.h> |
| 11 | +#include <timer.h> |
| 12 | + |
| 13 | +#define PCIE2TBT 0x54C |
| 14 | +#define PCIE2TBT_VALID BIT(0) |
| 15 | +#define PCIE2TBT_GO2SX 2 |
| 16 | +#define PCIE2TBT_GO2SX_NO_WAKE 3 |
| 17 | +#define PCIE2TBT_SX_EXIT_TBT_CONNECTED 4 |
| 18 | +#define PCIE2TBT_OS_UP 6 |
| 19 | +#define PCIE2TBT_SET_SECURITY_LEVEL 8 |
| 20 | +#define PCIE2TBT_GET_SECURITY_LEVEL 9 |
| 21 | +#define PCIE2TBT_BOOT_ON 24 |
| 22 | +#define PCIE2TBT_USB_ON 25 |
| 23 | +#define PCIE2TBT_GET_ENUMERATION_METHOD 26 |
| 24 | +#define PCIE2TBT_SET_ENUMERATION_METHOD 27 |
| 25 | +#define PCIE2TBT_POWER_CYCLE 28 |
| 26 | +#define PCIE2TBT_SX_START 29 |
| 27 | +#define PCIE2TBT_ACL_BOOT 30 |
| 28 | +#define PCIE2TBT_CONNECT_TOPOLOGY 31 |
| 29 | + |
| 30 | +#define TBT2PCIE 0x548 |
| 31 | +#define TBT2PCIE_DONE BIT(0) |
| 32 | + |
| 33 | +// Default timeout for mailbox commands unless otherwise specified. |
| 34 | +#define TIMEOUT_MS 1000 |
| 35 | +// Default timeout for controller to ack GO2SX/GO2SX_NO_WAKE mailbox command. |
| 36 | +#define GO2SX_TIMEOUT_MS 600 |
| 37 | + |
| 38 | +static void dtbt_cmd(struct device *dev, u32 command, u32 data, u32 timeout) |
| 39 | +{ |
| 40 | + u32 reg = (data << 8) | (command << 1) | PCIE2TBT_VALID; |
| 41 | + u32 status; |
| 42 | + |
| 43 | + printk(BIOS_DEBUG, "dTBT send command %08x\n", command); |
| 44 | + pci_write_config32(dev, PCIE2TBT, reg); |
| 45 | + |
| 46 | + if (!wait_ms(timeout, (status = pci_read_config32(dev, TBT2PCIE)) & TBT2PCIE_DONE)) { |
| 47 | + printk(BIOS_ERR, "dTBT command %08x send timeout %08x\n", command, status); |
| 48 | + } |
| 49 | + |
| 50 | + pci_write_config32(dev, PCIE2TBT, 0); |
| 51 | + if (!wait_ms(timeout, !(pci_read_config32(dev, TBT2PCIE) & TBT2PCIE_DONE))) { |
| 52 | + printk(BIOS_ERR, "dTBT command %08x clear timeout\n", command); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +static void dtbt_write_dsd(void) |
| 57 | +{ |
| 58 | + struct acpi_dp *dsd = acpi_dp_new_table("_DSD"); |
| 59 | + |
| 60 | + acpi_device_add_hotplug_support_in_d3(dsd); |
| 61 | + acpi_device_add_external_facing_port(dsd); |
| 62 | + acpi_dp_write(dsd); |
| 63 | +} |
| 64 | + |
| 65 | +static void dtbt_write_opregion(const struct bus *bus) |
| 66 | +{ |
| 67 | + uintptr_t mmconf_base = (uintptr_t)CONFIG_ECAM_MMCONF_BASE_ADDRESS |
| 68 | + + (((uintptr_t)(bus->secondary)) << 20); |
| 69 | + const struct opregion opregion = OPREGION("PXCS", SYSTEMMEMORY, mmconf_base, 0x1000); |
| 70 | + const struct fieldlist fieldlist[] = { |
| 71 | + FIELDLIST_OFFSET(TBT2PCIE), |
| 72 | + FIELDLIST_NAMESTR("TB2P", 32), |
| 73 | + FIELDLIST_OFFSET(PCIE2TBT), |
| 74 | + FIELDLIST_NAMESTR("P2TB", 32), |
| 75 | + }; |
| 76 | + |
| 77 | + acpigen_write_opregion(&opregion); |
| 78 | + acpigen_write_field("PXCS", fieldlist, ARRAY_SIZE(fieldlist), |
| 79 | + FIELD_DWORDACC | FIELD_NOLOCK | FIELD_PRESERVE); |
| 80 | +} |
| 81 | + |
| 82 | +static void dtbt_fill_ssdt(const struct device *dev) |
| 83 | +{ |
| 84 | + struct bus *bus; |
| 85 | + struct device *parent; |
| 86 | + const char *parent_scope; |
| 87 | + const char *dev_name = acpi_device_name(dev); |
| 88 | + |
| 89 | + bus = dev->upstream; |
| 90 | + if (!bus) { |
| 91 | + printk(BIOS_ERR, "dTBT bus invalid\n"); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + parent = bus->dev; |
| 96 | + if (!parent || parent->path.type != DEVICE_PATH_PCI) { |
| 97 | + printk(BIOS_ERR, "dTBT parent invalid\n"); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + parent_scope = acpi_device_path(parent); |
| 102 | + if (!parent_scope) { |
| 103 | + printk(BIOS_ERR, "dTBT parent scope not valid\n"); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + /* Scope */ |
| 108 | + acpigen_write_scope(parent_scope); |
| 109 | + dtbt_write_dsd(); |
| 110 | + |
| 111 | + /* Device */ |
| 112 | + acpigen_write_device(dev_name); |
| 113 | + acpigen_write_name_integer("_ADR", 0); |
| 114 | + dtbt_write_opregion(bus); |
| 115 | + |
| 116 | + /* Method */ |
| 117 | + acpigen_write_method_serialized("PTS", 0); |
| 118 | + |
| 119 | + acpigen_write_debug_string("dTBT prepare to sleep"); |
| 120 | + acpigen_write_store_int_to_namestr(PCIE2TBT_GO2SX_NO_WAKE << 1, "P2TB"); |
| 121 | + acpigen_write_delay_until_namestr_int(GO2SX_TIMEOUT_MS, "TB2P", PCIE2TBT_GO2SX_NO_WAKE << 1); |
| 122 | + |
| 123 | + acpigen_write_debug_namestr("TB2P"); |
| 124 | + acpigen_write_store_int_to_namestr(0, "P2TB"); |
| 125 | + acpigen_write_delay_until_namestr_int(GO2SX_TIMEOUT_MS, "TB2P", 0); |
| 126 | + acpigen_write_debug_namestr("TB2P"); |
| 127 | + |
| 128 | + acpigen_write_method_end(); |
| 129 | + acpigen_write_device_end(); |
| 130 | + acpigen_write_scope_end(); |
| 131 | + |
| 132 | + printk(BIOS_DEBUG, "dTBT fill SSDT\n"); |
| 133 | + printk(BIOS_DEBUG, " Dev %s\n", dev_path(dev)); |
| 134 | + //printk(BIOS_DEBUG, " Bus %s\n", bus_path(bus)); |
| 135 | + printk(BIOS_DEBUG, " Parent %s\n", dev_path(parent)); |
| 136 | + printk(BIOS_DEBUG, " Scope %s\n", parent_scope); |
| 137 | + printk(BIOS_DEBUG, " Device %s\n", dev_name); |
| 138 | + |
| 139 | + // \.TBTS Method |
| 140 | + acpigen_write_scope("\\"); |
| 141 | + acpigen_write_method("TBTS", 0); |
| 142 | + acpigen_emit_namestring(acpi_device_path_join(dev, "PTS")); |
| 143 | + acpigen_write_method_end(); |
| 144 | + acpigen_write_scope_end(); |
| 145 | +} |
| 146 | + |
| 147 | +static const char *dtbt_acpi_name(const struct device *dev) |
| 148 | +{ |
| 149 | + return "DTBT"; |
| 150 | +} |
| 151 | + |
| 152 | +static struct pci_operations dtbt_device_ops_pci = { |
| 153 | + .set_subsystem = 0, |
| 154 | +}; |
| 155 | + |
| 156 | +static struct device_operations dtbt_device_ops = { |
| 157 | + .read_resources = pci_bus_read_resources, |
| 158 | + .set_resources = pci_dev_set_resources, |
| 159 | + .enable_resources = pci_bus_enable_resources, |
| 160 | + .acpi_fill_ssdt = dtbt_fill_ssdt, |
| 161 | + .acpi_name = dtbt_acpi_name, |
| 162 | + .scan_bus = pciexp_scan_bridge, |
| 163 | + .reset_bus = pci_bus_reset, |
| 164 | + .ops_pci = &dtbt_device_ops_pci, |
| 165 | +}; |
| 166 | + |
| 167 | +static void dtbt_enable(struct device *dev) |
| 168 | +{ |
| 169 | + if (!is_dev_enabled(dev) || dev->path.type != DEVICE_PATH_PCI) |
| 170 | + return; |
| 171 | + |
| 172 | + if (pci_read_config16(dev, PCI_VENDOR_ID) != PCI_VID_INTEL) |
| 173 | + return; |
| 174 | + |
| 175 | + // TODO: check device ID |
| 176 | + |
| 177 | + dev->ops = &dtbt_device_ops; |
| 178 | + |
| 179 | + printk(BIOS_INFO, "dTBT controller found at %s\n", dev_path(dev)); |
| 180 | + |
| 181 | + // XXX: Recommendation is to set SL1 ("User Authorization") |
| 182 | + printk(BIOS_DEBUG, "dTBT set security level SL0\n"); |
| 183 | + dtbt_cmd(dev, PCIE2TBT_SET_SECURITY_LEVEL, 0, TIMEOUT_MS); |
| 184 | + // XXX: Must verify change or rollback all controllers |
| 185 | + |
| 186 | + if (acpi_is_wakeup_s3()) { |
| 187 | + printk(BIOS_DEBUG, "dTBT SX exit\n"); |
| 188 | + dtbt_cmd(dev, PCIE2TBT_SX_EXIT_TBT_CONNECTED, 0, TIMEOUT_MS); |
| 189 | + // TODO: "wait for fast link bring-up" loop (timeout: 5s) |
| 190 | + } else { |
| 191 | + printk(BIOS_DEBUG, "dTBT boot on\n"); |
| 192 | + dtbt_cmd(dev, PCIE2TBT_BOOT_ON, 0, TIMEOUT_MS); |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +struct chip_operations drivers_intel_dtbt_ops = { |
| 197 | + .name = "Intel Discrete Thunderbolt", |
| 198 | + .enable_dev = dtbt_enable, |
| 199 | +}; |
0 commit comments