Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

thunderbolt: Implement CIO reset correctly for Titan Ridge

When starting ICM firmware on Apple systems we need to perform CIO reset
as part of the flow. However, it turns out that the reset register has
changed to another location in Titan Ridge.

Fix this by introducing ->cio_reset() callback with corresponding
implementations for Alpine and Titan Ridge.

Fixes: c4630d6ae6e3 ("thunderbolt: Start firmware on Titan Ridge Apple systems")
Reported-by: Peter Bowen <pzb@amazon.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>

+74 -60
+74 -60
drivers/thunderbolt/icm.c
··· 56 56 * @max_boot_acl: Maximum number of preboot ACL entries (%0 if not supported) 57 57 * @rpm: Does the controller support runtime PM (RTD3) 58 58 * @is_supported: Checks if we can support ICM on this controller 59 + * @cio_reset: Trigger CIO reset 59 60 * @get_mode: Read and return the ICM firmware mode (optional) 60 61 * @get_route: Find a route string for given switch 61 62 * @save_devices: Ask ICM to save devices to ACL when suspending (optional) ··· 75 74 bool safe_mode; 76 75 bool rpm; 77 76 bool (*is_supported)(struct tb *tb); 77 + int (*cio_reset)(struct tb *tb); 78 78 int (*get_mode)(struct tb *tb); 79 79 int (*get_route)(struct tb *tb, u8 link, u8 depth, u64 *route); 80 80 void (*save_devices)(struct tb *tb); ··· 166 164 { 167 165 int depth = tb_route_length(route); 168 166 return depth ? route & ~(0xffULL << (depth - 1) * TB_ROUTE_SHIFT) : 0; 167 + } 168 + 169 + static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec) 170 + { 171 + unsigned long end = jiffies + msecs_to_jiffies(timeout_msec); 172 + u32 cmd; 173 + 174 + do { 175 + pci_read_config_dword(icm->upstream_port, 176 + icm->vnd_cap + PCIE2CIO_CMD, &cmd); 177 + if (!(cmd & PCIE2CIO_CMD_START)) { 178 + if (cmd & PCIE2CIO_CMD_TIMEOUT) 179 + break; 180 + return 0; 181 + } 182 + 183 + msleep(50); 184 + } while (time_before(jiffies, end)); 185 + 186 + return -ETIMEDOUT; 187 + } 188 + 189 + static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs, 190 + unsigned int port, unsigned int index, u32 *data) 191 + { 192 + struct pci_dev *pdev = icm->upstream_port; 193 + int ret, vnd_cap = icm->vnd_cap; 194 + u32 cmd; 195 + 196 + cmd = index; 197 + cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK; 198 + cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK; 199 + cmd |= PCIE2CIO_CMD_START; 200 + pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd); 201 + 202 + ret = pci2cio_wait_completion(icm, 5000); 203 + if (ret) 204 + return ret; 205 + 206 + pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data); 207 + return 0; 208 + } 209 + 210 + static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs, 211 + unsigned int port, unsigned int index, u32 data) 212 + { 213 + struct pci_dev *pdev = icm->upstream_port; 214 + int vnd_cap = icm->vnd_cap; 215 + u32 cmd; 216 + 217 + pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data); 218 + 219 + cmd = index; 220 + cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK; 221 + cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK; 222 + cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START; 223 + pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd); 224 + 225 + return pci2cio_wait_completion(icm, 5000); 169 226 } 170 227 171 228 static bool icm_match(const struct tb_cfg_request *req, ··· 899 838 } 900 839 } 901 840 841 + static int icm_tr_cio_reset(struct tb *tb) 842 + { 843 + return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x777, BIT(1)); 844 + } 845 + 902 846 static int 903 847 icm_tr_driver_ready(struct tb *tb, enum tb_security_level *security_level, 904 848 size_t *nboot_acl, bool *rpm) ··· 1310 1244 return false; 1311 1245 } 1312 1246 1247 + static int icm_ar_cio_reset(struct tb *tb) 1248 + { 1249 + return pcie2cio_write(tb_priv(tb), TB_CFG_SWITCH, 0, 0x50, BIT(9)); 1250 + } 1251 + 1313 1252 static int icm_ar_get_mode(struct tb *tb) 1314 1253 { 1315 1254 struct tb_nhi *nhi = tb->nhi; ··· 1552 1481 return -ETIMEDOUT; 1553 1482 } 1554 1483 1555 - static int pci2cio_wait_completion(struct icm *icm, unsigned long timeout_msec) 1556 - { 1557 - unsigned long end = jiffies + msecs_to_jiffies(timeout_msec); 1558 - u32 cmd; 1559 - 1560 - do { 1561 - pci_read_config_dword(icm->upstream_port, 1562 - icm->vnd_cap + PCIE2CIO_CMD, &cmd); 1563 - if (!(cmd & PCIE2CIO_CMD_START)) { 1564 - if (cmd & PCIE2CIO_CMD_TIMEOUT) 1565 - break; 1566 - return 0; 1567 - } 1568 - 1569 - msleep(50); 1570 - } while (time_before(jiffies, end)); 1571 - 1572 - return -ETIMEDOUT; 1573 - } 1574 - 1575 - static int pcie2cio_read(struct icm *icm, enum tb_cfg_space cs, 1576 - unsigned int port, unsigned int index, u32 *data) 1577 - { 1578 - struct pci_dev *pdev = icm->upstream_port; 1579 - int ret, vnd_cap = icm->vnd_cap; 1580 - u32 cmd; 1581 - 1582 - cmd = index; 1583 - cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK; 1584 - cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK; 1585 - cmd |= PCIE2CIO_CMD_START; 1586 - pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd); 1587 - 1588 - ret = pci2cio_wait_completion(icm, 5000); 1589 - if (ret) 1590 - return ret; 1591 - 1592 - pci_read_config_dword(pdev, vnd_cap + PCIE2CIO_RDDATA, data); 1593 - return 0; 1594 - } 1595 - 1596 - static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs, 1597 - unsigned int port, unsigned int index, u32 data) 1598 - { 1599 - struct pci_dev *pdev = icm->upstream_port; 1600 - int vnd_cap = icm->vnd_cap; 1601 - u32 cmd; 1602 - 1603 - pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_WRDATA, data); 1604 - 1605 - cmd = index; 1606 - cmd |= (port << PCIE2CIO_CMD_PORT_SHIFT) & PCIE2CIO_CMD_PORT_MASK; 1607 - cmd |= (cs << PCIE2CIO_CMD_CS_SHIFT) & PCIE2CIO_CMD_CS_MASK; 1608 - cmd |= PCIE2CIO_CMD_WRITE | PCIE2CIO_CMD_START; 1609 - pci_write_config_dword(pdev, vnd_cap + PCIE2CIO_CMD, cmd); 1610 - 1611 - return pci2cio_wait_completion(icm, 5000); 1612 - } 1613 - 1614 1484 static int icm_firmware_reset(struct tb *tb, struct tb_nhi *nhi) 1615 1485 { 1616 1486 struct icm *icm = tb_priv(tb); ··· 1572 1560 iowrite32(val, nhi->iobase + REG_FW_STS); 1573 1561 1574 1562 /* Trigger CIO reset now */ 1575 - return pcie2cio_write(icm, TB_CFG_SWITCH, 0, 0x50, BIT(9)); 1563 + return icm->cio_reset(tb); 1576 1564 } 1577 1565 1578 1566 static int icm_firmware_start(struct tb *tb, struct tb_nhi *nhi) ··· 2039 2027 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI: 2040 2028 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES; 2041 2029 icm->is_supported = icm_ar_is_supported; 2030 + icm->cio_reset = icm_ar_cio_reset; 2042 2031 icm->get_mode = icm_ar_get_mode; 2043 2032 icm->get_route = icm_ar_get_route; 2044 2033 icm->save_devices = icm_fr_save_devices; ··· 2055 2042 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI: 2056 2043 icm->max_boot_acl = ICM_AR_PREBOOT_ACL_ENTRIES; 2057 2044 icm->is_supported = icm_ar_is_supported; 2045 + icm->cio_reset = icm_tr_cio_reset; 2058 2046 icm->get_mode = icm_ar_get_mode; 2059 2047 icm->driver_ready = icm_tr_driver_ready; 2060 2048 icm->device_connected = icm_tr_device_connected;