0001
0002 #include <linux/pci.h>
0003 #include <linux/module.h>
0004 #include <linux/slab.h>
0005 #include <linux/ioport.h>
0006 #include <linux/wait.h>
0007
0008 #include "pci.h"
0009
0010
0011
0012
0013
0014
0015 DEFINE_RAW_SPINLOCK(pci_lock);
0016
0017
0018
0019
0020
0021
0022
0023 #define PCI_byte_BAD 0
0024 #define PCI_word_BAD (pos & 1)
0025 #define PCI_dword_BAD (pos & 3)
0026
0027 #ifdef CONFIG_PCI_LOCKLESS_CONFIG
0028 # define pci_lock_config(f) do { (void)(f); } while (0)
0029 # define pci_unlock_config(f) do { (void)(f); } while (0)
0030 #else
0031 # define pci_lock_config(f) raw_spin_lock_irqsave(&pci_lock, f)
0032 # define pci_unlock_config(f) raw_spin_unlock_irqrestore(&pci_lock, f)
0033 #endif
0034
0035 #define PCI_OP_READ(size, type, len) \
0036 int noinline pci_bus_read_config_##size \
0037 (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \
0038 { \
0039 int res; \
0040 unsigned long flags; \
0041 u32 data = 0; \
0042 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
0043 pci_lock_config(flags); \
0044 res = bus->ops->read(bus, devfn, pos, len, &data); \
0045 if (res) \
0046 PCI_SET_ERROR_RESPONSE(value); \
0047 else \
0048 *value = (type)data; \
0049 pci_unlock_config(flags); \
0050 return res; \
0051 }
0052
0053 #define PCI_OP_WRITE(size, type, len) \
0054 int noinline pci_bus_write_config_##size \
0055 (struct pci_bus *bus, unsigned int devfn, int pos, type value) \
0056 { \
0057 int res; \
0058 unsigned long flags; \
0059 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
0060 pci_lock_config(flags); \
0061 res = bus->ops->write(bus, devfn, pos, len, value); \
0062 pci_unlock_config(flags); \
0063 return res; \
0064 }
0065
0066 PCI_OP_READ(byte, u8, 1)
0067 PCI_OP_READ(word, u16, 2)
0068 PCI_OP_READ(dword, u32, 4)
0069 PCI_OP_WRITE(byte, u8, 1)
0070 PCI_OP_WRITE(word, u16, 2)
0071 PCI_OP_WRITE(dword, u32, 4)
0072
0073 EXPORT_SYMBOL(pci_bus_read_config_byte);
0074 EXPORT_SYMBOL(pci_bus_read_config_word);
0075 EXPORT_SYMBOL(pci_bus_read_config_dword);
0076 EXPORT_SYMBOL(pci_bus_write_config_byte);
0077 EXPORT_SYMBOL(pci_bus_write_config_word);
0078 EXPORT_SYMBOL(pci_bus_write_config_dword);
0079
0080 int pci_generic_config_read(struct pci_bus *bus, unsigned int devfn,
0081 int where, int size, u32 *val)
0082 {
0083 void __iomem *addr;
0084
0085 addr = bus->ops->map_bus(bus, devfn, where);
0086 if (!addr)
0087 return PCIBIOS_DEVICE_NOT_FOUND;
0088
0089 if (size == 1)
0090 *val = readb(addr);
0091 else if (size == 2)
0092 *val = readw(addr);
0093 else
0094 *val = readl(addr);
0095
0096 return PCIBIOS_SUCCESSFUL;
0097 }
0098 EXPORT_SYMBOL_GPL(pci_generic_config_read);
0099
0100 int pci_generic_config_write(struct pci_bus *bus, unsigned int devfn,
0101 int where, int size, u32 val)
0102 {
0103 void __iomem *addr;
0104
0105 addr = bus->ops->map_bus(bus, devfn, where);
0106 if (!addr)
0107 return PCIBIOS_DEVICE_NOT_FOUND;
0108
0109 if (size == 1)
0110 writeb(val, addr);
0111 else if (size == 2)
0112 writew(val, addr);
0113 else
0114 writel(val, addr);
0115
0116 return PCIBIOS_SUCCESSFUL;
0117 }
0118 EXPORT_SYMBOL_GPL(pci_generic_config_write);
0119
0120 int pci_generic_config_read32(struct pci_bus *bus, unsigned int devfn,
0121 int where, int size, u32 *val)
0122 {
0123 void __iomem *addr;
0124
0125 addr = bus->ops->map_bus(bus, devfn, where & ~0x3);
0126 if (!addr)
0127 return PCIBIOS_DEVICE_NOT_FOUND;
0128
0129 *val = readl(addr);
0130
0131 if (size <= 2)
0132 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
0133
0134 return PCIBIOS_SUCCESSFUL;
0135 }
0136 EXPORT_SYMBOL_GPL(pci_generic_config_read32);
0137
0138 int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn,
0139 int where, int size, u32 val)
0140 {
0141 void __iomem *addr;
0142 u32 mask, tmp;
0143
0144 addr = bus->ops->map_bus(bus, devfn, where & ~0x3);
0145 if (!addr)
0146 return PCIBIOS_DEVICE_NOT_FOUND;
0147
0148 if (size == 4) {
0149 writel(val, addr);
0150 return PCIBIOS_SUCCESSFUL;
0151 }
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162 if (!bus->unsafe_warn) {
0163 dev_warn(&bus->dev, "%d-byte config write to %04x:%02x:%02x.%d offset %#x may corrupt adjacent RW1C bits\n",
0164 size, pci_domain_nr(bus), bus->number,
0165 PCI_SLOT(devfn), PCI_FUNC(devfn), where);
0166 bus->unsafe_warn = 1;
0167 }
0168
0169 mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
0170 tmp = readl(addr) & mask;
0171 tmp |= val << ((where & 0x3) * 8);
0172 writel(tmp, addr);
0173
0174 return PCIBIOS_SUCCESSFUL;
0175 }
0176 EXPORT_SYMBOL_GPL(pci_generic_config_write32);
0177
0178
0179
0180
0181
0182
0183
0184
0185 struct pci_ops *pci_bus_set_ops(struct pci_bus *bus, struct pci_ops *ops)
0186 {
0187 struct pci_ops *old_ops;
0188 unsigned long flags;
0189
0190 raw_spin_lock_irqsave(&pci_lock, flags);
0191 old_ops = bus->ops;
0192 bus->ops = ops;
0193 raw_spin_unlock_irqrestore(&pci_lock, flags);
0194 return old_ops;
0195 }
0196 EXPORT_SYMBOL(pci_bus_set_ops);
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206 static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait);
0207
0208 static noinline void pci_wait_cfg(struct pci_dev *dev)
0209 __must_hold(&pci_lock)
0210 {
0211 do {
0212 raw_spin_unlock_irq(&pci_lock);
0213 wait_event(pci_cfg_wait, !dev->block_cfg_access);
0214 raw_spin_lock_irq(&pci_lock);
0215 } while (dev->block_cfg_access);
0216 }
0217
0218
0219 #define PCI_USER_READ_CONFIG(size, type) \
0220 int pci_user_read_config_##size \
0221 (struct pci_dev *dev, int pos, type *val) \
0222 { \
0223 int ret = PCIBIOS_SUCCESSFUL; \
0224 u32 data = -1; \
0225 if (PCI_##size##_BAD) \
0226 return -EINVAL; \
0227 raw_spin_lock_irq(&pci_lock); \
0228 if (unlikely(dev->block_cfg_access)) \
0229 pci_wait_cfg(dev); \
0230 ret = dev->bus->ops->read(dev->bus, dev->devfn, \
0231 pos, sizeof(type), &data); \
0232 raw_spin_unlock_irq(&pci_lock); \
0233 if (ret) \
0234 PCI_SET_ERROR_RESPONSE(val); \
0235 else \
0236 *val = (type)data; \
0237 return pcibios_err_to_errno(ret); \
0238 } \
0239 EXPORT_SYMBOL_GPL(pci_user_read_config_##size);
0240
0241
0242 #define PCI_USER_WRITE_CONFIG(size, type) \
0243 int pci_user_write_config_##size \
0244 (struct pci_dev *dev, int pos, type val) \
0245 { \
0246 int ret = PCIBIOS_SUCCESSFUL; \
0247 if (PCI_##size##_BAD) \
0248 return -EINVAL; \
0249 raw_spin_lock_irq(&pci_lock); \
0250 if (unlikely(dev->block_cfg_access)) \
0251 pci_wait_cfg(dev); \
0252 ret = dev->bus->ops->write(dev->bus, dev->devfn, \
0253 pos, sizeof(type), val); \
0254 raw_spin_unlock_irq(&pci_lock); \
0255 return pcibios_err_to_errno(ret); \
0256 } \
0257 EXPORT_SYMBOL_GPL(pci_user_write_config_##size);
0258
0259 PCI_USER_READ_CONFIG(byte, u8)
0260 PCI_USER_READ_CONFIG(word, u16)
0261 PCI_USER_READ_CONFIG(dword, u32)
0262 PCI_USER_WRITE_CONFIG(byte, u8)
0263 PCI_USER_WRITE_CONFIG(word, u16)
0264 PCI_USER_WRITE_CONFIG(dword, u32)
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274 void pci_cfg_access_lock(struct pci_dev *dev)
0275 {
0276 might_sleep();
0277
0278 raw_spin_lock_irq(&pci_lock);
0279 if (dev->block_cfg_access)
0280 pci_wait_cfg(dev);
0281 dev->block_cfg_access = 1;
0282 raw_spin_unlock_irq(&pci_lock);
0283 }
0284 EXPORT_SYMBOL_GPL(pci_cfg_access_lock);
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294 bool pci_cfg_access_trylock(struct pci_dev *dev)
0295 {
0296 unsigned long flags;
0297 bool locked = true;
0298
0299 raw_spin_lock_irqsave(&pci_lock, flags);
0300 if (dev->block_cfg_access)
0301 locked = false;
0302 else
0303 dev->block_cfg_access = 1;
0304 raw_spin_unlock_irqrestore(&pci_lock, flags);
0305
0306 return locked;
0307 }
0308 EXPORT_SYMBOL_GPL(pci_cfg_access_trylock);
0309
0310
0311
0312
0313
0314
0315
0316 void pci_cfg_access_unlock(struct pci_dev *dev)
0317 {
0318 unsigned long flags;
0319
0320 raw_spin_lock_irqsave(&pci_lock, flags);
0321
0322
0323
0324
0325
0326 WARN_ON(!dev->block_cfg_access);
0327
0328 dev->block_cfg_access = 0;
0329 raw_spin_unlock_irqrestore(&pci_lock, flags);
0330
0331 wake_up_all(&pci_cfg_wait);
0332 }
0333 EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
0334
0335 static inline int pcie_cap_version(const struct pci_dev *dev)
0336 {
0337 return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS;
0338 }
0339
0340 bool pcie_cap_has_lnkctl(const struct pci_dev *dev)
0341 {
0342 int type = pci_pcie_type(dev);
0343
0344 return type == PCI_EXP_TYPE_ENDPOINT ||
0345 type == PCI_EXP_TYPE_LEG_END ||
0346 type == PCI_EXP_TYPE_ROOT_PORT ||
0347 type == PCI_EXP_TYPE_UPSTREAM ||
0348 type == PCI_EXP_TYPE_DOWNSTREAM ||
0349 type == PCI_EXP_TYPE_PCI_BRIDGE ||
0350 type == PCI_EXP_TYPE_PCIE_BRIDGE;
0351 }
0352
0353 static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev)
0354 {
0355 return pcie_downstream_port(dev) &&
0356 pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT;
0357 }
0358
0359 bool pcie_cap_has_rtctl(const struct pci_dev *dev)
0360 {
0361 int type = pci_pcie_type(dev);
0362
0363 return type == PCI_EXP_TYPE_ROOT_PORT ||
0364 type == PCI_EXP_TYPE_RC_EC;
0365 }
0366
0367 static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
0368 {
0369 if (!pci_is_pcie(dev))
0370 return false;
0371
0372 switch (pos) {
0373 case PCI_EXP_FLAGS:
0374 return true;
0375 case PCI_EXP_DEVCAP:
0376 case PCI_EXP_DEVCTL:
0377 case PCI_EXP_DEVSTA:
0378 return true;
0379 case PCI_EXP_LNKCAP:
0380 case PCI_EXP_LNKCTL:
0381 case PCI_EXP_LNKSTA:
0382 return pcie_cap_has_lnkctl(dev);
0383 case PCI_EXP_SLTCAP:
0384 case PCI_EXP_SLTCTL:
0385 case PCI_EXP_SLTSTA:
0386 return pcie_cap_has_sltctl(dev);
0387 case PCI_EXP_RTCTL:
0388 case PCI_EXP_RTCAP:
0389 case PCI_EXP_RTSTA:
0390 return pcie_cap_has_rtctl(dev);
0391 case PCI_EXP_DEVCAP2:
0392 case PCI_EXP_DEVCTL2:
0393 case PCI_EXP_LNKCAP2:
0394 case PCI_EXP_LNKCTL2:
0395 case PCI_EXP_LNKSTA2:
0396 return pcie_cap_version(dev) > 1;
0397 default:
0398 return false;
0399 }
0400 }
0401
0402
0403
0404
0405
0406
0407 int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val)
0408 {
0409 int ret;
0410
0411 *val = 0;
0412 if (pos & 1)
0413 return PCIBIOS_BAD_REGISTER_NUMBER;
0414
0415 if (pcie_capability_reg_implemented(dev, pos)) {
0416 ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val);
0417
0418
0419
0420
0421
0422 if (ret)
0423 *val = 0;
0424 return ret;
0425 }
0426
0427
0428
0429
0430
0431
0432
0433
0434 if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
0435 pos == PCI_EXP_SLTSTA)
0436 *val = PCI_EXP_SLTSTA_PDS;
0437
0438 return 0;
0439 }
0440 EXPORT_SYMBOL(pcie_capability_read_word);
0441
0442 int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val)
0443 {
0444 int ret;
0445
0446 *val = 0;
0447 if (pos & 3)
0448 return PCIBIOS_BAD_REGISTER_NUMBER;
0449
0450 if (pcie_capability_reg_implemented(dev, pos)) {
0451 ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val);
0452
0453
0454
0455
0456
0457 if (ret)
0458 *val = 0;
0459 return ret;
0460 }
0461
0462 if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
0463 pos == PCI_EXP_SLTSTA)
0464 *val = PCI_EXP_SLTSTA_PDS;
0465
0466 return 0;
0467 }
0468 EXPORT_SYMBOL(pcie_capability_read_dword);
0469
0470 int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
0471 {
0472 if (pos & 1)
0473 return PCIBIOS_BAD_REGISTER_NUMBER;
0474
0475 if (!pcie_capability_reg_implemented(dev, pos))
0476 return 0;
0477
0478 return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
0479 }
0480 EXPORT_SYMBOL(pcie_capability_write_word);
0481
0482 int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val)
0483 {
0484 if (pos & 3)
0485 return PCIBIOS_BAD_REGISTER_NUMBER;
0486
0487 if (!pcie_capability_reg_implemented(dev, pos))
0488 return 0;
0489
0490 return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val);
0491 }
0492 EXPORT_SYMBOL(pcie_capability_write_dword);
0493
0494 int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
0495 u16 clear, u16 set)
0496 {
0497 int ret;
0498 u16 val;
0499
0500 ret = pcie_capability_read_word(dev, pos, &val);
0501 if (!ret) {
0502 val &= ~clear;
0503 val |= set;
0504 ret = pcie_capability_write_word(dev, pos, val);
0505 }
0506
0507 return ret;
0508 }
0509 EXPORT_SYMBOL(pcie_capability_clear_and_set_word);
0510
0511 int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos,
0512 u32 clear, u32 set)
0513 {
0514 int ret;
0515 u32 val;
0516
0517 ret = pcie_capability_read_dword(dev, pos, &val);
0518 if (!ret) {
0519 val &= ~clear;
0520 val |= set;
0521 ret = pcie_capability_write_dword(dev, pos, val);
0522 }
0523
0524 return ret;
0525 }
0526 EXPORT_SYMBOL(pcie_capability_clear_and_set_dword);
0527
0528 int pci_read_config_byte(const struct pci_dev *dev, int where, u8 *val)
0529 {
0530 if (pci_dev_is_disconnected(dev)) {
0531 PCI_SET_ERROR_RESPONSE(val);
0532 return PCIBIOS_DEVICE_NOT_FOUND;
0533 }
0534 return pci_bus_read_config_byte(dev->bus, dev->devfn, where, val);
0535 }
0536 EXPORT_SYMBOL(pci_read_config_byte);
0537
0538 int pci_read_config_word(const struct pci_dev *dev, int where, u16 *val)
0539 {
0540 if (pci_dev_is_disconnected(dev)) {
0541 PCI_SET_ERROR_RESPONSE(val);
0542 return PCIBIOS_DEVICE_NOT_FOUND;
0543 }
0544 return pci_bus_read_config_word(dev->bus, dev->devfn, where, val);
0545 }
0546 EXPORT_SYMBOL(pci_read_config_word);
0547
0548 int pci_read_config_dword(const struct pci_dev *dev, int where,
0549 u32 *val)
0550 {
0551 if (pci_dev_is_disconnected(dev)) {
0552 PCI_SET_ERROR_RESPONSE(val);
0553 return PCIBIOS_DEVICE_NOT_FOUND;
0554 }
0555 return pci_bus_read_config_dword(dev->bus, dev->devfn, where, val);
0556 }
0557 EXPORT_SYMBOL(pci_read_config_dword);
0558
0559 int pci_write_config_byte(const struct pci_dev *dev, int where, u8 val)
0560 {
0561 if (pci_dev_is_disconnected(dev))
0562 return PCIBIOS_DEVICE_NOT_FOUND;
0563 return pci_bus_write_config_byte(dev->bus, dev->devfn, where, val);
0564 }
0565 EXPORT_SYMBOL(pci_write_config_byte);
0566
0567 int pci_write_config_word(const struct pci_dev *dev, int where, u16 val)
0568 {
0569 if (pci_dev_is_disconnected(dev))
0570 return PCIBIOS_DEVICE_NOT_FOUND;
0571 return pci_bus_write_config_word(dev->bus, dev->devfn, where, val);
0572 }
0573 EXPORT_SYMBOL(pci_write_config_word);
0574
0575 int pci_write_config_dword(const struct pci_dev *dev, int where,
0576 u32 val)
0577 {
0578 if (pci_dev_is_disconnected(dev))
0579 return PCIBIOS_DEVICE_NOT_FOUND;
0580 return pci_bus_write_config_dword(dev->bus, dev->devfn, where, val);
0581 }
0582 EXPORT_SYMBOL(pci_write_config_dword);