Bjorn Helgaas | 7328c8f | 2018-01-26 11:45:16 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #include <linux/pci.h> |
| 3 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 4 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/ioport.h> |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 6 | #include <linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
Adrian Bunk | 48b1914 | 2005-11-06 01:45:08 +0100 | [diff] [blame] | 8 | #include "pci.h" |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | /* |
| 11 | * This interrupt-safe spinlock protects all accesses to PCI |
| 12 | * configuration space. |
| 13 | */ |
| 14 | |
Jan Kiszka | a2e2778 | 2011-11-04 09:46:00 +0100 | [diff] [blame] | 15 | DEFINE_RAW_SPINLOCK(pci_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | /* |
Bjorn Helgaas | df62ab5 | 2018-03-09 16:36:33 -0600 | [diff] [blame] | 18 | * Wrappers for all PCI configuration access functions. They just check |
| 19 | * alignment, do locking and call the low-level functions pointed to |
| 20 | * by pci_dev->ops. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #define PCI_byte_BAD 0 |
| 24 | #define PCI_word_BAD (pos & 1) |
| 25 | #define PCI_dword_BAD (pos & 3) |
| 26 | |
Thomas Gleixner | 714fe38 | 2017-03-16 22:50:06 +0100 | [diff] [blame] | 27 | #ifdef CONFIG_PCI_LOCKLESS_CONFIG |
| 28 | # define pci_lock_config(f) do { (void)(f); } while (0) |
| 29 | # define pci_unlock_config(f) do { (void)(f); } while (0) |
| 30 | #else |
| 31 | # define pci_lock_config(f) raw_spin_lock_irqsave(&pci_lock, f) |
| 32 | # define pci_unlock_config(f) raw_spin_unlock_irqrestore(&pci_lock, f) |
| 33 | #endif |
| 34 | |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 35 | #define PCI_OP_READ(size, type, len) \ |
Keith Busch | 5180fd9 | 2018-09-18 17:58:37 -0600 | [diff] [blame] | 36 | int noinline pci_bus_read_config_##size \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \ |
| 38 | { \ |
| 39 | int res; \ |
| 40 | unsigned long flags; \ |
| 41 | u32 data = 0; \ |
| 42 | if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \ |
Thomas Gleixner | 714fe38 | 2017-03-16 22:50:06 +0100 | [diff] [blame] | 43 | pci_lock_config(flags); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | res = bus->ops->read(bus, devfn, pos, len, &data); \ |
Naveen Naidu | f4f7eb4 | 2021-11-18 19:33:12 +0530 | [diff] [blame] | 45 | if (res) \ |
| 46 | PCI_SET_ERROR_RESPONSE(value); \ |
| 47 | else \ |
| 48 | *value = (type)data; \ |
Thomas Gleixner | 714fe38 | 2017-03-16 22:50:06 +0100 | [diff] [blame] | 49 | pci_unlock_config(flags); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return res; \ |
| 51 | } |
| 52 | |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 53 | #define PCI_OP_WRITE(size, type, len) \ |
Keith Busch | 5180fd9 | 2018-09-18 17:58:37 -0600 | [diff] [blame] | 54 | int noinline pci_bus_write_config_##size \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | (struct pci_bus *bus, unsigned int devfn, int pos, type value) \ |
| 56 | { \ |
| 57 | int res; \ |
| 58 | unsigned long flags; \ |
| 59 | if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \ |
Thomas Gleixner | 714fe38 | 2017-03-16 22:50:06 +0100 | [diff] [blame] | 60 | pci_lock_config(flags); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | res = bus->ops->write(bus, devfn, pos, len, value); \ |
Thomas Gleixner | 714fe38 | 2017-03-16 22:50:06 +0100 | [diff] [blame] | 62 | pci_unlock_config(flags); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return res; \ |
| 64 | } |
| 65 | |
| 66 | PCI_OP_READ(byte, u8, 1) |
| 67 | PCI_OP_READ(word, u16, 2) |
| 68 | PCI_OP_READ(dword, u32, 4) |
| 69 | PCI_OP_WRITE(byte, u8, 1) |
| 70 | PCI_OP_WRITE(word, u16, 2) |
| 71 | PCI_OP_WRITE(dword, u32, 4) |
| 72 | |
| 73 | EXPORT_SYMBOL(pci_bus_read_config_byte); |
| 74 | EXPORT_SYMBOL(pci_bus_read_config_word); |
| 75 | EXPORT_SYMBOL(pci_bus_read_config_dword); |
| 76 | EXPORT_SYMBOL(pci_bus_write_config_byte); |
| 77 | EXPORT_SYMBOL(pci_bus_write_config_word); |
| 78 | EXPORT_SYMBOL(pci_bus_write_config_dword); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 79 | |
Rob Herring | 1f94a94 | 2015-01-09 20:34:39 -0600 | [diff] [blame] | 80 | int pci_generic_config_read(struct pci_bus *bus, unsigned int devfn, |
| 81 | int where, int size, u32 *val) |
| 82 | { |
| 83 | void __iomem *addr; |
| 84 | |
| 85 | addr = bus->ops->map_bus(bus, devfn, where); |
Naveen Naidu | 316df70 | 2021-11-18 19:33:14 +0530 | [diff] [blame] | 86 | if (!addr) |
Rob Herring | 1f94a94 | 2015-01-09 20:34:39 -0600 | [diff] [blame] | 87 | return PCIBIOS_DEVICE_NOT_FOUND; |
Rob Herring | 1f94a94 | 2015-01-09 20:34:39 -0600 | [diff] [blame] | 88 | |
| 89 | if (size == 1) |
| 90 | *val = readb(addr); |
| 91 | else if (size == 2) |
| 92 | *val = readw(addr); |
| 93 | else |
| 94 | *val = readl(addr); |
| 95 | |
| 96 | return PCIBIOS_SUCCESSFUL; |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(pci_generic_config_read); |
| 99 | |
| 100 | int pci_generic_config_write(struct pci_bus *bus, unsigned int devfn, |
| 101 | int where, int size, u32 val) |
| 102 | { |
| 103 | void __iomem *addr; |
| 104 | |
| 105 | addr = bus->ops->map_bus(bus, devfn, where); |
| 106 | if (!addr) |
| 107 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 108 | |
| 109 | if (size == 1) |
| 110 | writeb(val, addr); |
| 111 | else if (size == 2) |
| 112 | writew(val, addr); |
| 113 | else |
| 114 | writel(val, addr); |
| 115 | |
| 116 | return PCIBIOS_SUCCESSFUL; |
| 117 | } |
| 118 | EXPORT_SYMBOL_GPL(pci_generic_config_write); |
| 119 | |
| 120 | int pci_generic_config_read32(struct pci_bus *bus, unsigned int devfn, |
| 121 | int where, int size, u32 *val) |
| 122 | { |
| 123 | void __iomem *addr; |
| 124 | |
| 125 | addr = bus->ops->map_bus(bus, devfn, where & ~0x3); |
Naveen Naidu | 316df70 | 2021-11-18 19:33:14 +0530 | [diff] [blame] | 126 | if (!addr) |
Rob Herring | 1f94a94 | 2015-01-09 20:34:39 -0600 | [diff] [blame] | 127 | return PCIBIOS_DEVICE_NOT_FOUND; |
Rob Herring | 1f94a94 | 2015-01-09 20:34:39 -0600 | [diff] [blame] | 128 | |
| 129 | *val = readl(addr); |
| 130 | |
| 131 | if (size <= 2) |
| 132 | *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1); |
| 133 | |
| 134 | return PCIBIOS_SUCCESSFUL; |
| 135 | } |
| 136 | EXPORT_SYMBOL_GPL(pci_generic_config_read32); |
| 137 | |
| 138 | int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn, |
| 139 | int where, int size, u32 val) |
| 140 | { |
| 141 | void __iomem *addr; |
| 142 | u32 mask, tmp; |
| 143 | |
| 144 | addr = bus->ops->map_bus(bus, devfn, where & ~0x3); |
| 145 | if (!addr) |
| 146 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 147 | |
| 148 | if (size == 4) { |
| 149 | writel(val, addr); |
| 150 | return PCIBIOS_SUCCESSFUL; |
Rob Herring | 1f94a94 | 2015-01-09 20:34:39 -0600 | [diff] [blame] | 151 | } |
| 152 | |
Bjorn Helgaas | fb26592 | 2016-10-31 16:00:01 -0500 | [diff] [blame] | 153 | /* |
| 154 | * In general, hardware that supports only 32-bit writes on PCI is |
| 155 | * not spec-compliant. For example, software may perform a 16-bit |
| 156 | * write. If the hardware only supports 32-bit accesses, we must |
| 157 | * do a 32-bit read, merge in the 16 bits we intend to write, |
| 158 | * followed by a 32-bit write. If the 16 bits we *don't* intend to |
| 159 | * write happen to have any RW1C (write-one-to-clear) bits set, we |
| 160 | * just inadvertently cleared something we shouldn't have. |
| 161 | */ |
| 162 | dev_warn_ratelimited(&bus->dev, "%d-byte config write to %04x:%02x:%02x.%d offset %#x may corrupt adjacent RW1C bits\n", |
| 163 | size, pci_domain_nr(bus), bus->number, |
| 164 | PCI_SLOT(devfn), PCI_FUNC(devfn), where); |
| 165 | |
| 166 | mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8)); |
Rob Herring | 1f94a94 | 2015-01-09 20:34:39 -0600 | [diff] [blame] | 167 | tmp = readl(addr) & mask; |
| 168 | tmp |= val << ((where & 0x3) * 8); |
| 169 | writel(tmp, addr); |
| 170 | |
| 171 | return PCIBIOS_SUCCESSFUL; |
| 172 | } |
| 173 | EXPORT_SYMBOL_GPL(pci_generic_config_write32); |
| 174 | |
Huang Ying | a72b46c | 2009-04-24 10:45:17 +0800 | [diff] [blame] | 175 | /** |
| 176 | * pci_bus_set_ops - Set raw operations of pci bus |
| 177 | * @bus: pci bus struct |
| 178 | * @ops: new raw operations |
| 179 | * |
| 180 | * Return previous raw operations |
| 181 | */ |
| 182 | struct pci_ops *pci_bus_set_ops(struct pci_bus *bus, struct pci_ops *ops) |
| 183 | { |
| 184 | struct pci_ops *old_ops; |
| 185 | unsigned long flags; |
| 186 | |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 187 | raw_spin_lock_irqsave(&pci_lock, flags); |
Huang Ying | a72b46c | 2009-04-24 10:45:17 +0800 | [diff] [blame] | 188 | old_ops = bus->ops; |
| 189 | bus->ops = ops; |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 190 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
Huang Ying | a72b46c | 2009-04-24 10:45:17 +0800 | [diff] [blame] | 191 | return old_ops; |
| 192 | } |
| 193 | EXPORT_SYMBOL(pci_bus_set_ops); |
Stephen Hemminger | 287d19c | 2008-12-18 09:17:16 -0800 | [diff] [blame] | 194 | |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 195 | /* |
| 196 | * The following routines are to prevent the user from accessing PCI config |
| 197 | * space when it's unsafe to do so. Some devices require this during BIST and |
| 198 | * we're required to prevent it during D-state transitions. |
| 199 | * |
| 200 | * We have a bit per device to indicate it's blocked and a global wait queue |
| 201 | * for callers to sleep on until devices are unblocked. |
| 202 | */ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 203 | static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 204 | |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 205 | static noinline void pci_wait_cfg(struct pci_dev *dev) |
Bjorn Helgaas | 2a7e32d0 | 2020-06-25 18:14:55 -0500 | [diff] [blame] | 206 | __must_hold(&pci_lock) |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 207 | { |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 208 | do { |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 209 | raw_spin_unlock_irq(&pci_lock); |
Bjorn Helgaas | 2a7e32d0 | 2020-06-25 18:14:55 -0500 | [diff] [blame] | 210 | wait_event(pci_cfg_wait, !dev->block_cfg_access); |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 211 | raw_spin_lock_irq(&pci_lock); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 212 | } while (dev->block_cfg_access); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Greg Thelen | 34e3207 | 2011-04-17 08:20:32 -0700 | [diff] [blame] | 215 | /* Returns 0 on success, negative values indicate error. */ |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 216 | #define PCI_USER_READ_CONFIG(size, type) \ |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 217 | int pci_user_read_config_##size \ |
| 218 | (struct pci_dev *dev, int pos, type *val) \ |
| 219 | { \ |
Gavin Shan | d97ffe2 | 2014-05-21 15:23:30 +1000 | [diff] [blame] | 220 | int ret = PCIBIOS_SUCCESSFUL; \ |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 221 | u32 data = -1; \ |
Greg Thelen | 34e3207 | 2011-04-17 08:20:32 -0700 | [diff] [blame] | 222 | if (PCI_##size##_BAD) \ |
| 223 | return -EINVAL; \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 224 | raw_spin_lock_irq(&pci_lock); \ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 225 | if (unlikely(dev->block_cfg_access)) \ |
| 226 | pci_wait_cfg(dev); \ |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 227 | ret = dev->bus->ops->read(dev->bus, dev->devfn, \ |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 228 | pos, sizeof(type), &data); \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 229 | raw_spin_unlock_irq(&pci_lock); \ |
Naveen Naidu | f4f7eb4 | 2021-11-18 19:33:12 +0530 | [diff] [blame] | 230 | if (ret) \ |
| 231 | PCI_SET_ERROR_RESPONSE(val); \ |
| 232 | else \ |
| 233 | *val = (type)data; \ |
Gavin Shan | d97ffe2 | 2014-05-21 15:23:30 +1000 | [diff] [blame] | 234 | return pcibios_err_to_errno(ret); \ |
Alex Williamson | c63587d | 2012-06-11 05:27:19 +0000 | [diff] [blame] | 235 | } \ |
| 236 | EXPORT_SYMBOL_GPL(pci_user_read_config_##size); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 237 | |
Greg Thelen | 34e3207 | 2011-04-17 08:20:32 -0700 | [diff] [blame] | 238 | /* Returns 0 on success, negative values indicate error. */ |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 239 | #define PCI_USER_WRITE_CONFIG(size, type) \ |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 240 | int pci_user_write_config_##size \ |
| 241 | (struct pci_dev *dev, int pos, type val) \ |
| 242 | { \ |
Gavin Shan | d97ffe2 | 2014-05-21 15:23:30 +1000 | [diff] [blame] | 243 | int ret = PCIBIOS_SUCCESSFUL; \ |
Greg Thelen | 34e3207 | 2011-04-17 08:20:32 -0700 | [diff] [blame] | 244 | if (PCI_##size##_BAD) \ |
| 245 | return -EINVAL; \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 246 | raw_spin_lock_irq(&pci_lock); \ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 247 | if (unlikely(dev->block_cfg_access)) \ |
| 248 | pci_wait_cfg(dev); \ |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 249 | ret = dev->bus->ops->write(dev->bus, dev->devfn, \ |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 250 | pos, sizeof(type), val); \ |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 251 | raw_spin_unlock_irq(&pci_lock); \ |
Gavin Shan | d97ffe2 | 2014-05-21 15:23:30 +1000 | [diff] [blame] | 252 | return pcibios_err_to_errno(ret); \ |
Alex Williamson | c63587d | 2012-06-11 05:27:19 +0000 | [diff] [blame] | 253 | } \ |
| 254 | EXPORT_SYMBOL_GPL(pci_user_write_config_##size); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 255 | |
| 256 | PCI_USER_READ_CONFIG(byte, u8) |
| 257 | PCI_USER_READ_CONFIG(word, u16) |
| 258 | PCI_USER_READ_CONFIG(dword, u32) |
| 259 | PCI_USER_WRITE_CONFIG(byte, u8) |
| 260 | PCI_USER_WRITE_CONFIG(word, u16) |
| 261 | PCI_USER_WRITE_CONFIG(dword, u32) |
| 262 | |
| 263 | /** |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 264 | * pci_cfg_access_lock - Lock PCI config reads/writes |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 265 | * @dev: pci device struct |
| 266 | * |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 267 | * When access is locked, any userspace reads or writes to config |
| 268 | * space and concurrent lock requests will sleep until access is |
Brian Norris | 0b131b1 | 2017-03-27 17:46:14 -0700 | [diff] [blame] | 269 | * allowed via pci_cfg_access_unlock() again. |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 270 | */ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 271 | void pci_cfg_access_lock(struct pci_dev *dev) |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 272 | { |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 273 | might_sleep(); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 274 | |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 275 | raw_spin_lock_irq(&pci_lock); |
| 276 | if (dev->block_cfg_access) |
| 277 | pci_wait_cfg(dev); |
| 278 | dev->block_cfg_access = 1; |
| 279 | raw_spin_unlock_irq(&pci_lock); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 280 | } |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 281 | EXPORT_SYMBOL_GPL(pci_cfg_access_lock); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 282 | |
| 283 | /** |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 284 | * pci_cfg_access_trylock - try to lock PCI config reads/writes |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 285 | * @dev: pci device struct |
| 286 | * |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 287 | * Same as pci_cfg_access_lock, but will return 0 if access is |
| 288 | * already locked, 1 otherwise. This function can be used from |
| 289 | * atomic contexts. |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 290 | */ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 291 | bool pci_cfg_access_trylock(struct pci_dev *dev) |
| 292 | { |
| 293 | unsigned long flags; |
| 294 | bool locked = true; |
| 295 | |
| 296 | raw_spin_lock_irqsave(&pci_lock, flags); |
| 297 | if (dev->block_cfg_access) |
| 298 | locked = false; |
| 299 | else |
| 300 | dev->block_cfg_access = 1; |
| 301 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
| 302 | |
| 303 | return locked; |
| 304 | } |
| 305 | EXPORT_SYMBOL_GPL(pci_cfg_access_trylock); |
| 306 | |
| 307 | /** |
| 308 | * pci_cfg_access_unlock - Unlock PCI config reads/writes |
| 309 | * @dev: pci device struct |
| 310 | * |
| 311 | * This function allows PCI config accesses to resume. |
| 312 | */ |
| 313 | void pci_cfg_access_unlock(struct pci_dev *dev) |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 314 | { |
| 315 | unsigned long flags; |
| 316 | |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 317 | raw_spin_lock_irqsave(&pci_lock, flags); |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 318 | |
Bjorn Helgaas | df62ab5 | 2018-03-09 16:36:33 -0600 | [diff] [blame] | 319 | /* |
| 320 | * This indicates a problem in the caller, but we don't need |
| 321 | * to kill them, unlike a double-block above. |
| 322 | */ |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 323 | WARN_ON(!dev->block_cfg_access); |
Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 324 | |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 325 | dev->block_cfg_access = 0; |
Thomas Gleixner | 511dd98 | 2010-02-17 14:35:19 +0000 | [diff] [blame] | 326 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
Bjorn Helgaas | cdcb33f | 2017-01-13 18:05:12 -0600 | [diff] [blame] | 327 | |
| 328 | wake_up_all(&pci_cfg_wait); |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 329 | } |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 330 | EXPORT_SYMBOL_GPL(pci_cfg_access_unlock); |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 331 | |
| 332 | static inline int pcie_cap_version(const struct pci_dev *dev) |
| 333 | { |
Myron Stowe | 1c531d8 | 2013-01-25 17:55:45 -0700 | [diff] [blame] | 334 | return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 335 | } |
| 336 | |
Yinghai Lu | 7a1562d | 2014-11-11 12:09:46 -0800 | [diff] [blame] | 337 | bool pcie_cap_has_lnkctl(const struct pci_dev *dev) |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 338 | { |
| 339 | int type = pci_pcie_type(dev); |
| 340 | |
Bjorn Helgaas | c8b303d | 2013-08-28 11:33:53 -0600 | [diff] [blame] | 341 | return type == PCI_EXP_TYPE_ENDPOINT || |
Bjorn Helgaas | d3694d4 | 2013-08-27 09:54:40 -0600 | [diff] [blame] | 342 | type == PCI_EXP_TYPE_LEG_END || |
| 343 | type == PCI_EXP_TYPE_ROOT_PORT || |
| 344 | type == PCI_EXP_TYPE_UPSTREAM || |
| 345 | type == PCI_EXP_TYPE_DOWNSTREAM || |
| 346 | type == PCI_EXP_TYPE_PCI_BRIDGE || |
| 347 | type == PCI_EXP_TYPE_PCIE_BRIDGE; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev) |
| 351 | { |
Bjorn Helgaas | ffb4d60 | 2015-06-24 16:05:54 -0500 | [diff] [blame] | 352 | return pcie_downstream_port(dev) && |
Bjorn Helgaas | 6d3a174 | 2013-08-28 12:01:03 -0600 | [diff] [blame] | 353 | pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 354 | } |
| 355 | |
Patel, Mayurkumar | af65d1a | 2019-10-18 16:52:21 +0000 | [diff] [blame] | 356 | bool pcie_cap_has_rtctl(const struct pci_dev *dev) |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 357 | { |
| 358 | int type = pci_pcie_type(dev); |
| 359 | |
Bjorn Helgaas | c8b303d | 2013-08-28 11:33:53 -0600 | [diff] [blame] | 360 | return type == PCI_EXP_TYPE_ROOT_PORT || |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 361 | type == PCI_EXP_TYPE_RC_EC; |
| 362 | } |
| 363 | |
| 364 | static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos) |
| 365 | { |
| 366 | if (!pci_is_pcie(dev)) |
| 367 | return false; |
| 368 | |
| 369 | switch (pos) { |
Alex Williamson | 969daa3 | 2013-02-14 11:35:42 -0700 | [diff] [blame] | 370 | case PCI_EXP_FLAGS: |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 371 | return true; |
| 372 | case PCI_EXP_DEVCAP: |
| 373 | case PCI_EXP_DEVCTL: |
| 374 | case PCI_EXP_DEVSTA: |
Bjorn Helgaas | fed2451 | 2013-08-28 12:03:42 -0600 | [diff] [blame] | 375 | return true; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 376 | case PCI_EXP_LNKCAP: |
| 377 | case PCI_EXP_LNKCTL: |
| 378 | case PCI_EXP_LNKSTA: |
| 379 | return pcie_cap_has_lnkctl(dev); |
| 380 | case PCI_EXP_SLTCAP: |
| 381 | case PCI_EXP_SLTCTL: |
| 382 | case PCI_EXP_SLTSTA: |
| 383 | return pcie_cap_has_sltctl(dev); |
| 384 | case PCI_EXP_RTCTL: |
| 385 | case PCI_EXP_RTCAP: |
| 386 | case PCI_EXP_RTSTA: |
| 387 | return pcie_cap_has_rtctl(dev); |
| 388 | case PCI_EXP_DEVCAP2: |
| 389 | case PCI_EXP_DEVCTL2: |
| 390 | case PCI_EXP_LNKCAP2: |
| 391 | case PCI_EXP_LNKCTL2: |
| 392 | case PCI_EXP_LNKSTA2: |
| 393 | return pcie_cap_version(dev) > 1; |
| 394 | default: |
| 395 | return false; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Note that these accessor functions are only for the "PCI Express |
| 401 | * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the |
| 402 | * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.) |
| 403 | */ |
| 404 | int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val) |
| 405 | { |
| 406 | int ret; |
| 407 | |
| 408 | *val = 0; |
| 409 | if (pos & 1) |
Bolarinwa Olayemi Saheed | b915358 | 2020-06-15 09:32:25 +0200 | [diff] [blame] | 410 | return PCIBIOS_BAD_REGISTER_NUMBER; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 411 | |
| 412 | if (pcie_capability_reg_implemented(dev, pos)) { |
| 413 | ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val); |
| 414 | /* |
Naveen Naidu | 289e3ea | 2021-11-18 19:33:32 +0530 | [diff] [blame] | 415 | * Reset *val to 0 if pci_read_config_word() fails; it may |
| 416 | * have been written as 0xFFFF (PCI_ERROR_RESPONSE) if the |
| 417 | * config read failed on PCI. |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 418 | */ |
| 419 | if (ret) |
| 420 | *val = 0; |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * For Functions that do not implement the Slot Capabilities, |
| 426 | * Slot Status, and Slot Control registers, these spaces must |
| 427 | * be hardwired to 0b, with the exception of the Presence Detect |
| 428 | * State bit in the Slot Status register of Downstream Ports, |
| 429 | * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8) |
| 430 | */ |
Bjorn Helgaas | ffb4d60 | 2015-06-24 16:05:54 -0500 | [diff] [blame] | 431 | if (pci_is_pcie(dev) && pcie_downstream_port(dev) && |
| 432 | pos == PCI_EXP_SLTSTA) |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 433 | *val = PCI_EXP_SLTSTA_PDS; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | EXPORT_SYMBOL(pcie_capability_read_word); |
| 438 | |
| 439 | int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val) |
| 440 | { |
| 441 | int ret; |
| 442 | |
| 443 | *val = 0; |
| 444 | if (pos & 3) |
Bolarinwa Olayemi Saheed | b915358 | 2020-06-15 09:32:25 +0200 | [diff] [blame] | 445 | return PCIBIOS_BAD_REGISTER_NUMBER; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 446 | |
| 447 | if (pcie_capability_reg_implemented(dev, pos)) { |
| 448 | ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val); |
| 449 | /* |
Naveen Naidu | 289e3ea | 2021-11-18 19:33:32 +0530 | [diff] [blame] | 450 | * Reset *val to 0 if pci_read_config_dword() fails; it may |
| 451 | * have been written as 0xFFFFFFFF (PCI_ERROR_RESPONSE) if |
| 452 | * the config read failed on PCI. |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 453 | */ |
| 454 | if (ret) |
| 455 | *val = 0; |
| 456 | return ret; |
| 457 | } |
| 458 | |
Bjorn Helgaas | ffb4d60 | 2015-06-24 16:05:54 -0500 | [diff] [blame] | 459 | if (pci_is_pcie(dev) && pcie_downstream_port(dev) && |
| 460 | pos == PCI_EXP_SLTSTA) |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 461 | *val = PCI_EXP_SLTSTA_PDS; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | EXPORT_SYMBOL(pcie_capability_read_dword); |
| 466 | |
| 467 | int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val) |
| 468 | { |
| 469 | if (pos & 1) |
Bolarinwa Olayemi Saheed | b915358 | 2020-06-15 09:32:25 +0200 | [diff] [blame] | 470 | return PCIBIOS_BAD_REGISTER_NUMBER; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 471 | |
| 472 | if (!pcie_capability_reg_implemented(dev, pos)) |
| 473 | return 0; |
| 474 | |
| 475 | return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val); |
| 476 | } |
| 477 | EXPORT_SYMBOL(pcie_capability_write_word); |
| 478 | |
| 479 | int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val) |
| 480 | { |
| 481 | if (pos & 3) |
Bolarinwa Olayemi Saheed | b915358 | 2020-06-15 09:32:25 +0200 | [diff] [blame] | 482 | return PCIBIOS_BAD_REGISTER_NUMBER; |
Jiang Liu | 8c0d3a0 | 2012-07-24 17:20:05 +0800 | [diff] [blame] | 483 | |
| 484 | if (!pcie_capability_reg_implemented(dev, pos)) |
| 485 | return 0; |
| 486 | |
| 487 | return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val); |
| 488 | } |
| 489 | EXPORT_SYMBOL(pcie_capability_write_dword); |
| 490 | |
| 491 | int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos, |
| 492 | u16 clear, u16 set) |
| 493 | { |
| 494 | int ret; |
| 495 | u16 val; |
| 496 | |
| 497 | ret = pcie_capability_read_word(dev, pos, &val); |
| 498 | if (!ret) { |
| 499 | val &= ~clear; |
| 500 | val |= set; |
| 501 | ret = pcie_capability_write_word(dev, pos, val); |
| 502 | } |
| 503 | |
| 504 | return ret; |
| 505 | } |
| 506 | EXPORT_SYMBOL(pcie_capability_clear_and_set_word); |
| 507 | |
| 508 | int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos, |
| 509 | u32 clear, u32 set) |
| 510 | { |
| 511 | int ret; |
| 512 | u32 val; |
| 513 | |
| 514 | ret = pcie_capability_read_dword(dev, pos, &val); |
| 515 | if (!ret) { |
| 516 | val &= ~clear; |
| 517 | val |= set; |
| 518 | ret = pcie_capability_write_dword(dev, pos, val); |
| 519 | } |
| 520 | |
| 521 | return ret; |
| 522 | } |
| 523 | EXPORT_SYMBOL(pcie_capability_clear_and_set_dword); |
Keith Busch | d3881e5 | 2017-02-07 14:32:33 -0500 | [diff] [blame] | 524 | |
| 525 | int pci_read_config_byte(const struct pci_dev *dev, int where, u8 *val) |
| 526 | { |
Keith Busch | 4b10388 | 2017-03-29 22:49:06 -0500 | [diff] [blame] | 527 | if (pci_dev_is_disconnected(dev)) { |
Naveen Naidu | 9bc9310 | 2021-11-18 19:33:13 +0530 | [diff] [blame] | 528 | PCI_SET_ERROR_RESPONSE(val); |
Brian Norris | 449e2f9 | 2017-05-23 12:36:58 -0700 | [diff] [blame] | 529 | return PCIBIOS_DEVICE_NOT_FOUND; |
Keith Busch | 4b10388 | 2017-03-29 22:49:06 -0500 | [diff] [blame] | 530 | } |
Keith Busch | d3881e5 | 2017-02-07 14:32:33 -0500 | [diff] [blame] | 531 | return pci_bus_read_config_byte(dev->bus, dev->devfn, where, val); |
| 532 | } |
| 533 | EXPORT_SYMBOL(pci_read_config_byte); |
| 534 | |
| 535 | int pci_read_config_word(const struct pci_dev *dev, int where, u16 *val) |
| 536 | { |
Keith Busch | 4b10388 | 2017-03-29 22:49:06 -0500 | [diff] [blame] | 537 | if (pci_dev_is_disconnected(dev)) { |
Naveen Naidu | 9bc9310 | 2021-11-18 19:33:13 +0530 | [diff] [blame] | 538 | PCI_SET_ERROR_RESPONSE(val); |
Brian Norris | 449e2f9 | 2017-05-23 12:36:58 -0700 | [diff] [blame] | 539 | return PCIBIOS_DEVICE_NOT_FOUND; |
Keith Busch | 4b10388 | 2017-03-29 22:49:06 -0500 | [diff] [blame] | 540 | } |
Keith Busch | d3881e5 | 2017-02-07 14:32:33 -0500 | [diff] [blame] | 541 | return pci_bus_read_config_word(dev->bus, dev->devfn, where, val); |
| 542 | } |
| 543 | EXPORT_SYMBOL(pci_read_config_word); |
| 544 | |
| 545 | int pci_read_config_dword(const struct pci_dev *dev, int where, |
| 546 | u32 *val) |
| 547 | { |
Keith Busch | 4b10388 | 2017-03-29 22:49:06 -0500 | [diff] [blame] | 548 | if (pci_dev_is_disconnected(dev)) { |
Naveen Naidu | 9bc9310 | 2021-11-18 19:33:13 +0530 | [diff] [blame] | 549 | PCI_SET_ERROR_RESPONSE(val); |
Brian Norris | 449e2f9 | 2017-05-23 12:36:58 -0700 | [diff] [blame] | 550 | return PCIBIOS_DEVICE_NOT_FOUND; |
Keith Busch | 4b10388 | 2017-03-29 22:49:06 -0500 | [diff] [blame] | 551 | } |
Keith Busch | d3881e5 | 2017-02-07 14:32:33 -0500 | [diff] [blame] | 552 | return pci_bus_read_config_dword(dev->bus, dev->devfn, where, val); |
| 553 | } |
| 554 | EXPORT_SYMBOL(pci_read_config_dword); |
| 555 | |
| 556 | int pci_write_config_byte(const struct pci_dev *dev, int where, u8 val) |
| 557 | { |
Keith Busch | 4b10388 | 2017-03-29 22:49:06 -0500 | [diff] [blame] | 558 | if (pci_dev_is_disconnected(dev)) |
Brian Norris | 449e2f9 | 2017-05-23 12:36:58 -0700 | [diff] [blame] | 559 | return PCIBIOS_DEVICE_NOT_FOUND; |
Keith Busch | d3881e5 | 2017-02-07 14:32:33 -0500 | [diff] [blame] | 560 | return pci_bus_write_config_byte(dev->bus, dev->devfn, where, val); |
| 561 | } |
| 562 | EXPORT_SYMBOL(pci_write_config_byte); |
| 563 | |
| 564 | int pci_write_config_word(const struct pci_dev *dev, int where, u16 val) |
| 565 | { |
Keith Busch | 4b10388 | 2017-03-29 22:49:06 -0500 | [diff] [blame] | 566 | if (pci_dev_is_disconnected(dev)) |
Brian Norris | 449e2f9 | 2017-05-23 12:36:58 -0700 | [diff] [blame] | 567 | return PCIBIOS_DEVICE_NOT_FOUND; |
Keith Busch | d3881e5 | 2017-02-07 14:32:33 -0500 | [diff] [blame] | 568 | return pci_bus_write_config_word(dev->bus, dev->devfn, where, val); |
| 569 | } |
| 570 | EXPORT_SYMBOL(pci_write_config_word); |
| 571 | |
| 572 | int pci_write_config_dword(const struct pci_dev *dev, int where, |
| 573 | u32 val) |
| 574 | { |
Keith Busch | 4b10388 | 2017-03-29 22:49:06 -0500 | [diff] [blame] | 575 | if (pci_dev_is_disconnected(dev)) |
Brian Norris | 449e2f9 | 2017-05-23 12:36:58 -0700 | [diff] [blame] | 576 | return PCIBIOS_DEVICE_NOT_FOUND; |
Keith Busch | d3881e5 | 2017-02-07 14:32:33 -0500 | [diff] [blame] | 577 | return pci_bus_write_config_dword(dev->bus, dev->devfn, where, val); |
| 578 | } |
| 579 | EXPORT_SYMBOL(pci_write_config_dword); |