1 #include <linux/delay.h>
3 #include <linux/module.h>
4 #include <linux/sched.h>
5 #include <linux/slab.h>
6 #include <linux/ioport.h>
7 #include <linux/wait.h>
12 * This interrupt-safe spinlock protects all accesses to PCI
13 * configuration space.
16 DEFINE_RAW_SPINLOCK(pci_lock
);
19 * Wrappers for all PCI configuration access functions. They just check
20 * alignment, do locking and call the low-level functions pointed to
24 #define PCI_byte_BAD 0
25 #define PCI_word_BAD (pos & 1)
26 #define PCI_dword_BAD (pos & 3)
28 #define PCI_OP_READ(size, type, len) \
29 int pci_bus_read_config_##size \
30 (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \
33 unsigned long flags; \
35 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
36 raw_spin_lock_irqsave(&pci_lock, flags); \
37 res = bus->ops->read(bus, devfn, pos, len, &data); \
38 *value = (type)data; \
39 raw_spin_unlock_irqrestore(&pci_lock, flags); \
43 #define PCI_OP_WRITE(size, type, len) \
44 int pci_bus_write_config_##size \
45 (struct pci_bus *bus, unsigned int devfn, int pos, type value) \
48 unsigned long flags; \
49 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
50 raw_spin_lock_irqsave(&pci_lock, flags); \
51 res = bus->ops->write(bus, devfn, pos, len, value); \
52 raw_spin_unlock_irqrestore(&pci_lock, flags); \
56 PCI_OP_READ(byte
, u8
, 1)
57 PCI_OP_READ(word
, u16
, 2)
58 PCI_OP_READ(dword
, u32
, 4)
59 PCI_OP_WRITE(byte
, u8
, 1)
60 PCI_OP_WRITE(word
, u16
, 2)
61 PCI_OP_WRITE(dword
, u32
, 4)
63 EXPORT_SYMBOL(pci_bus_read_config_byte
);
64 EXPORT_SYMBOL(pci_bus_read_config_word
);
65 EXPORT_SYMBOL(pci_bus_read_config_dword
);
66 EXPORT_SYMBOL(pci_bus_write_config_byte
);
67 EXPORT_SYMBOL(pci_bus_write_config_word
);
68 EXPORT_SYMBOL(pci_bus_write_config_dword
);
70 int pci_generic_config_read(struct pci_bus
*bus
, unsigned int devfn
,
71 int where
, int size
, u32
*val
)
75 addr
= bus
->ops
->map_bus(bus
, devfn
, where
);
78 return PCIBIOS_DEVICE_NOT_FOUND
;
88 return PCIBIOS_SUCCESSFUL
;
90 EXPORT_SYMBOL_GPL(pci_generic_config_read
);
92 int pci_generic_config_write(struct pci_bus
*bus
, unsigned int devfn
,
93 int where
, int size
, u32 val
)
97 addr
= bus
->ops
->map_bus(bus
, devfn
, where
);
99 return PCIBIOS_DEVICE_NOT_FOUND
;
108 return PCIBIOS_SUCCESSFUL
;
110 EXPORT_SYMBOL_GPL(pci_generic_config_write
);
112 int pci_generic_config_read32(struct pci_bus
*bus
, unsigned int devfn
,
113 int where
, int size
, u32
*val
)
117 addr
= bus
->ops
->map_bus(bus
, devfn
, where
& ~0x3);
120 return PCIBIOS_DEVICE_NOT_FOUND
;
126 *val
= (*val
>> (8 * (where
& 3))) & ((1 << (size
* 8)) - 1);
128 return PCIBIOS_SUCCESSFUL
;
130 EXPORT_SYMBOL_GPL(pci_generic_config_read32
);
132 int pci_generic_config_write32(struct pci_bus
*bus
, unsigned int devfn
,
133 int where
, int size
, u32 val
)
138 addr
= bus
->ops
->map_bus(bus
, devfn
, where
& ~0x3);
140 return PCIBIOS_DEVICE_NOT_FOUND
;
144 return PCIBIOS_SUCCESSFUL
;
146 mask
= ~(((1 << (size
* 8)) - 1) << ((where
& 0x3) * 8));
149 tmp
= readl(addr
) & mask
;
150 tmp
|= val
<< ((where
& 0x3) * 8);
153 return PCIBIOS_SUCCESSFUL
;
155 EXPORT_SYMBOL_GPL(pci_generic_config_write32
);
158 * pci_bus_set_ops - Set raw operations of pci bus
159 * @bus: pci bus struct
160 * @ops: new raw operations
162 * Return previous raw operations
164 struct pci_ops
*pci_bus_set_ops(struct pci_bus
*bus
, struct pci_ops
*ops
)
166 struct pci_ops
*old_ops
;
169 raw_spin_lock_irqsave(&pci_lock
, flags
);
172 raw_spin_unlock_irqrestore(&pci_lock
, flags
);
175 EXPORT_SYMBOL(pci_bus_set_ops
);
178 * The following routines are to prevent the user from accessing PCI config
179 * space when it's unsafe to do so. Some devices require this during BIST and
180 * we're required to prevent it during D-state transitions.
182 * We have a bit per device to indicate it's blocked and a global wait queue
183 * for callers to sleep on until devices are unblocked.
185 static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait
);
187 static noinline
void pci_wait_cfg(struct pci_dev
*dev
)
188 __must_hold(&pci_lock
)
191 raw_spin_unlock_irq(&pci_lock
);
192 wait_event(pci_cfg_wait
, !dev
->block_cfg_access
);
193 raw_spin_lock_irq(&pci_lock
);
194 } while (dev
->block_cfg_access
);
197 /* Returns 0 on success, negative values indicate error. */
198 #define PCI_USER_READ_CONFIG(size, type) \
199 int pci_user_read_config_##size \
200 (struct pci_dev *dev, int pos, type *val) \
202 int ret = PCIBIOS_SUCCESSFUL; \
204 if (PCI_##size##_BAD) \
206 raw_spin_lock_irq(&pci_lock); \
207 if (unlikely(dev->block_cfg_access)) \
209 ret = dev->bus->ops->read(dev->bus, dev->devfn, \
210 pos, sizeof(type), &data); \
211 raw_spin_unlock_irq(&pci_lock); \
213 return pcibios_err_to_errno(ret); \
215 EXPORT_SYMBOL_GPL(pci_user_read_config_##size);
217 /* Returns 0 on success, negative values indicate error. */
218 #define PCI_USER_WRITE_CONFIG(size, type) \
219 int pci_user_write_config_##size \
220 (struct pci_dev *dev, int pos, type val) \
222 int ret = PCIBIOS_SUCCESSFUL; \
223 if (PCI_##size##_BAD) \
225 raw_spin_lock_irq(&pci_lock); \
226 if (unlikely(dev->block_cfg_access)) \
228 ret = dev->bus->ops->write(dev->bus, dev->devfn, \
229 pos, sizeof(type), val); \
230 raw_spin_unlock_irq(&pci_lock); \
231 return pcibios_err_to_errno(ret); \
233 EXPORT_SYMBOL_GPL(pci_user_write_config_##size);
235 PCI_USER_READ_CONFIG(byte
, u8
)
236 PCI_USER_READ_CONFIG(word
, u16
)
237 PCI_USER_READ_CONFIG(dword
, u32
)
238 PCI_USER_WRITE_CONFIG(byte
, u8
)
239 PCI_USER_WRITE_CONFIG(word
, u16
)
240 PCI_USER_WRITE_CONFIG(dword
, u32
)
242 /* VPD access through PCI 2.2+ VPD capability */
245 * pci_read_vpd - Read one entry from Vital Product Data
246 * @dev: pci device struct
247 * @pos: offset in vpd space
248 * @count: number of bytes to read
249 * @buf: pointer to where to store result
251 ssize_t
pci_read_vpd(struct pci_dev
*dev
, loff_t pos
, size_t count
, void *buf
)
253 if (!dev
->vpd
|| !dev
->vpd
->ops
)
255 return dev
->vpd
->ops
->read(dev
, pos
, count
, buf
);
257 EXPORT_SYMBOL(pci_read_vpd
);
260 * pci_write_vpd - Write entry to Vital Product Data
261 * @dev: pci device struct
262 * @pos: offset in vpd space
263 * @count: number of bytes to write
264 * @buf: buffer containing write data
266 ssize_t
pci_write_vpd(struct pci_dev
*dev
, loff_t pos
, size_t count
, const void *buf
)
268 if (!dev
->vpd
|| !dev
->vpd
->ops
)
270 return dev
->vpd
->ops
->write(dev
, pos
, count
, buf
);
272 EXPORT_SYMBOL(pci_write_vpd
);
275 * pci_set_vpd_size - Set size of Vital Product Data space
276 * @dev: pci device struct
277 * @len: size of vpd space
279 int pci_set_vpd_size(struct pci_dev
*dev
, size_t len
)
281 if (!dev
->vpd
|| !dev
->vpd
->ops
)
283 return dev
->vpd
->ops
->set_size(dev
, len
);
285 EXPORT_SYMBOL(pci_set_vpd_size
);
287 #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1)
290 * pci_vpd_size - determine actual size of Vital Product Data
291 * @dev: pci device struct
292 * @old_size: current assumed size, also maximum allowed size
294 static size_t pci_vpd_size(struct pci_dev
*dev
, size_t old_size
)
297 unsigned char header
[1+2]; /* 1 byte tag, 2 bytes length */
299 while (off
< old_size
&&
300 pci_read_vpd(dev
, off
, 1, header
) == 1) {
303 if (header
[0] & PCI_VPD_LRDT
) {
304 /* Large Resource Data Type Tag */
305 tag
= pci_vpd_lrdt_tag(header
);
306 /* Only read length from known tag items */
307 if ((tag
== PCI_VPD_LTIN_ID_STRING
) ||
308 (tag
== PCI_VPD_LTIN_RO_DATA
) ||
309 (tag
== PCI_VPD_LTIN_RW_DATA
)) {
310 if (pci_read_vpd(dev
, off
+1, 2,
313 "invalid large VPD tag %02x size at offset %zu",
317 off
+= PCI_VPD_LRDT_TAG_SIZE
+
318 pci_vpd_lrdt_size(header
);
321 /* Short Resource Data Type Tag */
322 off
+= PCI_VPD_SRDT_TAG_SIZE
+
323 pci_vpd_srdt_size(header
);
324 tag
= pci_vpd_srdt_tag(header
);
327 if (tag
== PCI_VPD_STIN_END
) /* End tag descriptor */
330 if ((tag
!= PCI_VPD_LTIN_ID_STRING
) &&
331 (tag
!= PCI_VPD_LTIN_RO_DATA
) &&
332 (tag
!= PCI_VPD_LTIN_RW_DATA
)) {
334 "invalid %s VPD tag %02x at offset %zu",
335 (header
[0] & PCI_VPD_LRDT
) ? "large" : "short",
344 * Wait for last operation to complete.
345 * This code has to spin since there is no other notification from the PCI
346 * hardware. Since the VPD is often implemented by serial attachment to an
347 * EEPROM, it may take many milliseconds to complete.
349 * Returns 0 on success, negative values indicate error.
351 static int pci_vpd_wait(struct pci_dev
*dev
)
353 struct pci_vpd
*vpd
= dev
->vpd
;
354 unsigned long timeout
= jiffies
+ msecs_to_jiffies(50);
355 unsigned long max_sleep
= 16;
362 while (time_before(jiffies
, timeout
)) {
363 ret
= pci_user_read_config_word(dev
, vpd
->cap
+ PCI_VPD_ADDR
,
368 if ((status
& PCI_VPD_ADDR_F
) == vpd
->flag
) {
373 if (fatal_signal_pending(current
))
376 usleep_range(10, max_sleep
);
377 if (max_sleep
< 1024)
381 dev_warn(&dev
->dev
, "VPD access failed. This is likely a firmware bug on this device. Contact the card vendor for a firmware update\n");
385 static ssize_t
pci_vpd_read(struct pci_dev
*dev
, loff_t pos
, size_t count
,
388 struct pci_vpd
*vpd
= dev
->vpd
;
390 loff_t end
= pos
+ count
;
398 vpd
->len
= pci_vpd_size(dev
, vpd
->len
);
407 if (end
> vpd
->len
) {
412 if (mutex_lock_killable(&vpd
->lock
))
415 ret
= pci_vpd_wait(dev
);
421 unsigned int i
, skip
;
423 ret
= pci_user_write_config_word(dev
, vpd
->cap
+ PCI_VPD_ADDR
,
428 vpd
->flag
= PCI_VPD_ADDR_F
;
429 ret
= pci_vpd_wait(dev
);
433 ret
= pci_user_read_config_dword(dev
, vpd
->cap
+ PCI_VPD_DATA
, &val
);
438 for (i
= 0; i
< sizeof(u32
); i
++) {
448 mutex_unlock(&vpd
->lock
);
449 return ret
? ret
: count
;
452 static ssize_t
pci_vpd_write(struct pci_dev
*dev
, loff_t pos
, size_t count
,
455 struct pci_vpd
*vpd
= dev
->vpd
;
457 loff_t end
= pos
+ count
;
460 if (pos
< 0 || (pos
& 3) || (count
& 3))
465 vpd
->len
= pci_vpd_size(dev
, vpd
->len
);
474 if (mutex_lock_killable(&vpd
->lock
))
477 ret
= pci_vpd_wait(dev
);
489 ret
= pci_user_write_config_dword(dev
, vpd
->cap
+ PCI_VPD_DATA
, val
);
492 ret
= pci_user_write_config_word(dev
, vpd
->cap
+ PCI_VPD_ADDR
,
493 pos
| PCI_VPD_ADDR_F
);
499 ret
= pci_vpd_wait(dev
);
506 mutex_unlock(&vpd
->lock
);
507 return ret
? ret
: count
;
510 static int pci_vpd_set_size(struct pci_dev
*dev
, size_t len
)
512 struct pci_vpd
*vpd
= dev
->vpd
;
514 if (len
== 0 || len
> PCI_VPD_MAX_SIZE
)
523 static const struct pci_vpd_ops pci_vpd_ops
= {
524 .read
= pci_vpd_read
,
525 .write
= pci_vpd_write
,
526 .set_size
= pci_vpd_set_size
,
529 static ssize_t
pci_vpd_f0_read(struct pci_dev
*dev
, loff_t pos
, size_t count
,
532 struct pci_dev
*tdev
= pci_get_slot(dev
->bus
,
533 PCI_DEVFN(PCI_SLOT(dev
->devfn
), 0));
539 ret
= pci_read_vpd(tdev
, pos
, count
, arg
);
544 static ssize_t
pci_vpd_f0_write(struct pci_dev
*dev
, loff_t pos
, size_t count
,
547 struct pci_dev
*tdev
= pci_get_slot(dev
->bus
,
548 PCI_DEVFN(PCI_SLOT(dev
->devfn
), 0));
554 ret
= pci_write_vpd(tdev
, pos
, count
, arg
);
559 static int pci_vpd_f0_set_size(struct pci_dev
*dev
, size_t len
)
561 struct pci_dev
*tdev
= pci_get_slot(dev
->bus
,
562 PCI_DEVFN(PCI_SLOT(dev
->devfn
), 0));
568 ret
= pci_set_vpd_size(tdev
, len
);
573 static const struct pci_vpd_ops pci_vpd_f0_ops
= {
574 .read
= pci_vpd_f0_read
,
575 .write
= pci_vpd_f0_write
,
576 .set_size
= pci_vpd_f0_set_size
,
579 int pci_vpd_init(struct pci_dev
*dev
)
584 cap
= pci_find_capability(dev
, PCI_CAP_ID_VPD
);
588 vpd
= kzalloc(sizeof(*vpd
), GFP_ATOMIC
);
592 vpd
->len
= PCI_VPD_MAX_SIZE
;
593 if (dev
->dev_flags
& PCI_DEV_FLAGS_VPD_REF_F0
)
594 vpd
->ops
= &pci_vpd_f0_ops
;
596 vpd
->ops
= &pci_vpd_ops
;
597 mutex_init(&vpd
->lock
);
605 void pci_vpd_release(struct pci_dev
*dev
)
611 * pci_cfg_access_lock - Lock PCI config reads/writes
612 * @dev: pci device struct
614 * When access is locked, any userspace reads or writes to config
615 * space and concurrent lock requests will sleep until access is
616 * allowed via pci_cfg_access_unlocked again.
618 void pci_cfg_access_lock(struct pci_dev
*dev
)
622 raw_spin_lock_irq(&pci_lock
);
623 if (dev
->block_cfg_access
)
625 dev
->block_cfg_access
= 1;
626 raw_spin_unlock_irq(&pci_lock
);
628 EXPORT_SYMBOL_GPL(pci_cfg_access_lock
);
631 * pci_cfg_access_trylock - try to lock PCI config reads/writes
632 * @dev: pci device struct
634 * Same as pci_cfg_access_lock, but will return 0 if access is
635 * already locked, 1 otherwise. This function can be used from
638 bool pci_cfg_access_trylock(struct pci_dev
*dev
)
643 raw_spin_lock_irqsave(&pci_lock
, flags
);
644 if (dev
->block_cfg_access
)
647 dev
->block_cfg_access
= 1;
648 raw_spin_unlock_irqrestore(&pci_lock
, flags
);
652 EXPORT_SYMBOL_GPL(pci_cfg_access_trylock
);
655 * pci_cfg_access_unlock - Unlock PCI config reads/writes
656 * @dev: pci device struct
658 * This function allows PCI config accesses to resume.
660 void pci_cfg_access_unlock(struct pci_dev
*dev
)
664 raw_spin_lock_irqsave(&pci_lock
, flags
);
666 /* This indicates a problem in the caller, but we don't need
667 * to kill them, unlike a double-block above. */
668 WARN_ON(!dev
->block_cfg_access
);
670 dev
->block_cfg_access
= 0;
671 raw_spin_unlock_irqrestore(&pci_lock
, flags
);
673 wake_up_all(&pci_cfg_wait
);
675 EXPORT_SYMBOL_GPL(pci_cfg_access_unlock
);
677 static inline int pcie_cap_version(const struct pci_dev
*dev
)
679 return pcie_caps_reg(dev
) & PCI_EXP_FLAGS_VERS
;
682 static bool pcie_downstream_port(const struct pci_dev
*dev
)
684 int type
= pci_pcie_type(dev
);
686 return type
== PCI_EXP_TYPE_ROOT_PORT
||
687 type
== PCI_EXP_TYPE_DOWNSTREAM
;
690 bool pcie_cap_has_lnkctl(const struct pci_dev
*dev
)
692 int type
= pci_pcie_type(dev
);
694 return type
== PCI_EXP_TYPE_ENDPOINT
||
695 type
== PCI_EXP_TYPE_LEG_END
||
696 type
== PCI_EXP_TYPE_ROOT_PORT
||
697 type
== PCI_EXP_TYPE_UPSTREAM
||
698 type
== PCI_EXP_TYPE_DOWNSTREAM
||
699 type
== PCI_EXP_TYPE_PCI_BRIDGE
||
700 type
== PCI_EXP_TYPE_PCIE_BRIDGE
;
703 static inline bool pcie_cap_has_sltctl(const struct pci_dev
*dev
)
705 return pcie_downstream_port(dev
) &&
706 pcie_caps_reg(dev
) & PCI_EXP_FLAGS_SLOT
;
709 static inline bool pcie_cap_has_rtctl(const struct pci_dev
*dev
)
711 int type
= pci_pcie_type(dev
);
713 return type
== PCI_EXP_TYPE_ROOT_PORT
||
714 type
== PCI_EXP_TYPE_RC_EC
;
717 static bool pcie_capability_reg_implemented(struct pci_dev
*dev
, int pos
)
719 if (!pci_is_pcie(dev
))
732 return pcie_cap_has_lnkctl(dev
);
736 return pcie_cap_has_sltctl(dev
);
740 return pcie_cap_has_rtctl(dev
);
741 case PCI_EXP_DEVCAP2
:
742 case PCI_EXP_DEVCTL2
:
743 case PCI_EXP_LNKCAP2
:
744 case PCI_EXP_LNKCTL2
:
745 case PCI_EXP_LNKSTA2
:
746 return pcie_cap_version(dev
) > 1;
753 * Note that these accessor functions are only for the "PCI Express
754 * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the
755 * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.)
757 int pcie_capability_read_word(struct pci_dev
*dev
, int pos
, u16
*val
)
765 if (pcie_capability_reg_implemented(dev
, pos
)) {
766 ret
= pci_read_config_word(dev
, pci_pcie_cap(dev
) + pos
, val
);
768 * Reset *val to 0 if pci_read_config_word() fails, it may
769 * have been written as 0xFFFF if hardware error happens
770 * during pci_read_config_word().
778 * For Functions that do not implement the Slot Capabilities,
779 * Slot Status, and Slot Control registers, these spaces must
780 * be hardwired to 0b, with the exception of the Presence Detect
781 * State bit in the Slot Status register of Downstream Ports,
782 * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8)
784 if (pci_is_pcie(dev
) && pcie_downstream_port(dev
) &&
785 pos
== PCI_EXP_SLTSTA
)
786 *val
= PCI_EXP_SLTSTA_PDS
;
790 EXPORT_SYMBOL(pcie_capability_read_word
);
792 int pcie_capability_read_dword(struct pci_dev
*dev
, int pos
, u32
*val
)
800 if (pcie_capability_reg_implemented(dev
, pos
)) {
801 ret
= pci_read_config_dword(dev
, pci_pcie_cap(dev
) + pos
, val
);
803 * Reset *val to 0 if pci_read_config_dword() fails, it may
804 * have been written as 0xFFFFFFFF if hardware error happens
805 * during pci_read_config_dword().
812 if (pci_is_pcie(dev
) && pcie_downstream_port(dev
) &&
813 pos
== PCI_EXP_SLTSTA
)
814 *val
= PCI_EXP_SLTSTA_PDS
;
818 EXPORT_SYMBOL(pcie_capability_read_dword
);
820 int pcie_capability_write_word(struct pci_dev
*dev
, int pos
, u16 val
)
825 if (!pcie_capability_reg_implemented(dev
, pos
))
828 return pci_write_config_word(dev
, pci_pcie_cap(dev
) + pos
, val
);
830 EXPORT_SYMBOL(pcie_capability_write_word
);
832 int pcie_capability_write_dword(struct pci_dev
*dev
, int pos
, u32 val
)
837 if (!pcie_capability_reg_implemented(dev
, pos
))
840 return pci_write_config_dword(dev
, pci_pcie_cap(dev
) + pos
, val
);
842 EXPORT_SYMBOL(pcie_capability_write_dword
);
844 int pcie_capability_clear_and_set_word(struct pci_dev
*dev
, int pos
,
850 ret
= pcie_capability_read_word(dev
, pos
, &val
);
854 ret
= pcie_capability_write_word(dev
, pos
, val
);
859 EXPORT_SYMBOL(pcie_capability_clear_and_set_word
);
861 int pcie_capability_clear_and_set_dword(struct pci_dev
*dev
, int pos
,
867 ret
= pcie_capability_read_dword(dev
, pos
, &val
);
871 ret
= pcie_capability_write_dword(dev
, pos
, val
);
876 EXPORT_SYMBOL(pcie_capability_clear_and_set_dword
);