2 #include <linux/module.h>
3 #include <linux/ioport.h>
4 #include <linux/wait.h>
9 * This interrupt-safe spinlock protects all accesses to PCI
10 * configuration space.
13 static DEFINE_SPINLOCK(pci_lock
);
16 * Wrappers for all PCI configuration access functions. They just check
17 * alignment, do locking and call the low-level functions pointed to
21 #define PCI_byte_BAD 0
22 #define PCI_word_BAD (pos & 1)
23 #define PCI_dword_BAD (pos & 3)
25 #define PCI_OP_READ(size,type,len) \
26 int pci_bus_read_config_##size \
27 (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \
30 unsigned long flags; \
32 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
33 spin_lock_irqsave(&pci_lock, flags); \
34 res = bus->ops->read(bus, devfn, pos, len, &data); \
35 *value = (type)data; \
36 spin_unlock_irqrestore(&pci_lock, flags); \
40 #define PCI_OP_WRITE(size,type,len) \
41 int pci_bus_write_config_##size \
42 (struct pci_bus *bus, unsigned int devfn, int pos, type value) \
45 unsigned long flags; \
46 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
47 spin_lock_irqsave(&pci_lock, flags); \
48 res = bus->ops->write(bus, devfn, pos, len, value); \
49 spin_unlock_irqrestore(&pci_lock, flags); \
53 PCI_OP_READ(byte
, u8
, 1)
54 PCI_OP_READ(word
, u16
, 2)
55 PCI_OP_READ(dword
, u32
, 4)
56 PCI_OP_WRITE(byte
, u8
, 1)
57 PCI_OP_WRITE(word
, u16
, 2)
58 PCI_OP_WRITE(dword
, u32
, 4)
60 EXPORT_SYMBOL(pci_bus_read_config_byte
);
61 EXPORT_SYMBOL(pci_bus_read_config_word
);
62 EXPORT_SYMBOL(pci_bus_read_config_dword
);
63 EXPORT_SYMBOL(pci_bus_write_config_byte
);
64 EXPORT_SYMBOL(pci_bus_write_config_word
);
65 EXPORT_SYMBOL(pci_bus_write_config_dword
);
68 * The following routines are to prevent the user from accessing PCI config
69 * space when it's unsafe to do so. Some devices require this during BIST and
70 * we're required to prevent it during D-state transitions.
72 * We have a bit per device to indicate it's blocked and a global wait queue
73 * for callers to sleep on until devices are unblocked.
75 static DECLARE_WAIT_QUEUE_HEAD(pci_ucfg_wait
);
77 static noinline
void pci_wait_ucfg(struct pci_dev
*dev
)
79 DECLARE_WAITQUEUE(wait
, current
);
81 __add_wait_queue(&pci_ucfg_wait
, &wait
);
83 set_current_state(TASK_UNINTERRUPTIBLE
);
84 spin_unlock_irq(&pci_lock
);
86 spin_lock_irq(&pci_lock
);
87 } while (dev
->block_ucfg_access
);
88 __remove_wait_queue(&pci_ucfg_wait
, &wait
);
91 #define PCI_USER_READ_CONFIG(size,type) \
92 int pci_user_read_config_##size \
93 (struct pci_dev *dev, int pos, type *val) \
97 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
98 spin_lock_irq(&pci_lock); \
99 if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev); \
100 ret = dev->bus->ops->read(dev->bus, dev->devfn, \
101 pos, sizeof(type), &data); \
102 spin_unlock_irq(&pci_lock); \
107 #define PCI_USER_WRITE_CONFIG(size,type) \
108 int pci_user_write_config_##size \
109 (struct pci_dev *dev, int pos, type val) \
112 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
113 spin_lock_irq(&pci_lock); \
114 if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev); \
115 ret = dev->bus->ops->write(dev->bus, dev->devfn, \
116 pos, sizeof(type), val); \
117 spin_unlock_irq(&pci_lock); \
121 PCI_USER_READ_CONFIG(byte
, u8
)
122 PCI_USER_READ_CONFIG(word
, u16
)
123 PCI_USER_READ_CONFIG(dword
, u32
)
124 PCI_USER_WRITE_CONFIG(byte
, u8
)
125 PCI_USER_WRITE_CONFIG(word
, u16
)
126 PCI_USER_WRITE_CONFIG(dword
, u32
)
129 * pci_block_user_cfg_access - Block userspace PCI config reads/writes
130 * @dev: pci device struct
132 * When user access is blocked, any reads or writes to config space will
133 * sleep until access is unblocked again. We don't allow nesting of
134 * block/unblock calls.
136 void pci_block_user_cfg_access(struct pci_dev
*dev
)
141 spin_lock_irqsave(&pci_lock
, flags
);
142 was_blocked
= dev
->block_ucfg_access
;
143 dev
->block_ucfg_access
= 1;
144 spin_unlock_irqrestore(&pci_lock
, flags
);
146 /* If we BUG() inside the pci_lock, we're guaranteed to hose
150 EXPORT_SYMBOL_GPL(pci_block_user_cfg_access
);
153 * pci_unblock_user_cfg_access - Unblock userspace PCI config reads/writes
154 * @dev: pci device struct
156 * This function allows userspace PCI config accesses to resume.
158 void pci_unblock_user_cfg_access(struct pci_dev
*dev
)
162 spin_lock_irqsave(&pci_lock
, flags
);
164 /* This indicates a problem in the caller, but we don't need
165 * to kill them, unlike a double-block above. */
166 WARN_ON(!dev
->block_ucfg_access
);
168 dev
->block_ucfg_access
= 0;
169 wake_up_all(&pci_ucfg_wait
);
170 spin_unlock_irqrestore(&pci_lock
, flags
);
172 EXPORT_SYMBOL_GPL(pci_unblock_user_cfg_access
);