1 #include <linux/delay.h>
3 #include <linux/module.h>
4 #include <linux/sched.h>
5 #include <linux/ioport.h>
6 #include <linux/wait.h>
11 * This interrupt-safe spinlock protects all accesses to PCI
12 * configuration space.
15 static DEFINE_SPINLOCK(pci_lock
);
18 * Wrappers for all PCI configuration access functions. They just check
19 * alignment, do locking and call the low-level functions pointed to
23 #define PCI_byte_BAD 0
24 #define PCI_word_BAD (pos & 1)
25 #define PCI_dword_BAD (pos & 3)
27 #define PCI_OP_READ(size,type,len) \
28 int pci_bus_read_config_##size \
29 (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \
32 unsigned long flags; \
34 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
35 spin_lock_irqsave(&pci_lock, flags); \
36 res = bus->ops->read(bus, devfn, pos, len, &data); \
37 *value = (type)data; \
38 spin_unlock_irqrestore(&pci_lock, flags); \
42 #define PCI_OP_WRITE(size,type,len) \
43 int pci_bus_write_config_##size \
44 (struct pci_bus *bus, unsigned int devfn, int pos, type value) \
47 unsigned long flags; \
48 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
49 spin_lock_irqsave(&pci_lock, flags); \
50 res = bus->ops->write(bus, devfn, pos, len, value); \
51 spin_unlock_irqrestore(&pci_lock, flags); \
55 PCI_OP_READ(byte
, u8
, 1)
56 PCI_OP_READ(word
, u16
, 2)
57 PCI_OP_READ(dword
, u32
, 4)
58 PCI_OP_WRITE(byte
, u8
, 1)
59 PCI_OP_WRITE(word
, u16
, 2)
60 PCI_OP_WRITE(dword
, u32
, 4)
62 EXPORT_SYMBOL(pci_bus_read_config_byte
);
63 EXPORT_SYMBOL(pci_bus_read_config_word
);
64 EXPORT_SYMBOL(pci_bus_read_config_dword
);
65 EXPORT_SYMBOL(pci_bus_write_config_byte
);
66 EXPORT_SYMBOL(pci_bus_write_config_word
);
67 EXPORT_SYMBOL(pci_bus_write_config_dword
);
70 * pci_bus_set_ops - Set raw operations of pci bus
71 * @bus: pci bus struct
72 * @ops: new raw operations
74 * Return previous raw operations
76 struct pci_ops
*pci_bus_set_ops(struct pci_bus
*bus
, struct pci_ops
*ops
)
78 struct pci_ops
*old_ops
;
81 spin_lock_irqsave(&pci_lock
, flags
);
84 spin_unlock_irqrestore(&pci_lock
, flags
);
87 EXPORT_SYMBOL(pci_bus_set_ops
);
90 * pci_read_vpd - Read one entry from Vital Product Data
91 * @dev: pci device struct
92 * @pos: offset in vpd space
93 * @count: number of bytes to read
94 * @buf: pointer to where to store result
97 ssize_t
pci_read_vpd(struct pci_dev
*dev
, loff_t pos
, size_t count
, void *buf
)
99 if (!dev
->vpd
|| !dev
->vpd
->ops
)
101 return dev
->vpd
->ops
->read(dev
, pos
, count
, buf
);
103 EXPORT_SYMBOL(pci_read_vpd
);
106 * pci_write_vpd - Write entry to Vital Product Data
107 * @dev: pci device struct
108 * @pos: offset in vpd space
109 * @count: number of bytes to write
110 * @buf: buffer containing write data
113 ssize_t
pci_write_vpd(struct pci_dev
*dev
, loff_t pos
, size_t count
, const void *buf
)
115 if (!dev
->vpd
|| !dev
->vpd
->ops
)
117 return dev
->vpd
->ops
->write(dev
, pos
, count
, buf
);
119 EXPORT_SYMBOL(pci_write_vpd
);
122 * The following routines are to prevent the user from accessing PCI config
123 * space when it's unsafe to do so. Some devices require this during BIST and
124 * we're required to prevent it during D-state transitions.
126 * We have a bit per device to indicate it's blocked and a global wait queue
127 * for callers to sleep on until devices are unblocked.
129 static DECLARE_WAIT_QUEUE_HEAD(pci_ucfg_wait
);
131 static noinline
void pci_wait_ucfg(struct pci_dev
*dev
)
133 DECLARE_WAITQUEUE(wait
, current
);
135 __add_wait_queue(&pci_ucfg_wait
, &wait
);
137 set_current_state(TASK_UNINTERRUPTIBLE
);
138 spin_unlock_irq(&pci_lock
);
140 spin_lock_irq(&pci_lock
);
141 } while (dev
->block_ucfg_access
);
142 __remove_wait_queue(&pci_ucfg_wait
, &wait
);
145 #define PCI_USER_READ_CONFIG(size,type) \
146 int pci_user_read_config_##size \
147 (struct pci_dev *dev, int pos, type *val) \
151 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
152 spin_lock_irq(&pci_lock); \
153 if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev); \
154 ret = dev->bus->ops->read(dev->bus, dev->devfn, \
155 pos, sizeof(type), &data); \
156 spin_unlock_irq(&pci_lock); \
161 #define PCI_USER_WRITE_CONFIG(size,type) \
162 int pci_user_write_config_##size \
163 (struct pci_dev *dev, int pos, type val) \
166 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
167 spin_lock_irq(&pci_lock); \
168 if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev); \
169 ret = dev->bus->ops->write(dev->bus, dev->devfn, \
170 pos, sizeof(type), val); \
171 spin_unlock_irq(&pci_lock); \
175 PCI_USER_READ_CONFIG(byte
, u8
)
176 PCI_USER_READ_CONFIG(word
, u16
)
177 PCI_USER_READ_CONFIG(dword
, u32
)
178 PCI_USER_WRITE_CONFIG(byte
, u8
)
179 PCI_USER_WRITE_CONFIG(word
, u16
)
180 PCI_USER_WRITE_CONFIG(dword
, u32
)
182 /* VPD access through PCI 2.2+ VPD capability */
184 #define PCI_VPD_PCI22_SIZE (PCI_VPD_ADDR_MASK + 1)
186 struct pci_vpd_pci22
{
195 * Wait for last operation to complete.
196 * This code has to spin since there is no other notification from the PCI
197 * hardware. Since the VPD is often implemented by serial attachment to an
198 * EEPROM, it may take many milliseconds to complete.
200 static int pci_vpd_pci22_wait(struct pci_dev
*dev
)
202 struct pci_vpd_pci22
*vpd
=
203 container_of(dev
->vpd
, struct pci_vpd_pci22
, base
);
204 unsigned long timeout
= jiffies
+ HZ
/20 + 2;
212 ret
= pci_user_read_config_word(dev
, vpd
->cap
+ PCI_VPD_ADDR
,
217 if ((status
& PCI_VPD_ADDR_F
) == vpd
->flag
) {
222 if (time_after(jiffies
, timeout
))
224 if (fatal_signal_pending(current
))
231 static ssize_t
pci_vpd_pci22_read(struct pci_dev
*dev
, loff_t pos
, size_t count
,
234 struct pci_vpd_pci22
*vpd
=
235 container_of(dev
->vpd
, struct pci_vpd_pci22
, base
);
237 loff_t end
= pos
+ count
;
240 if (pos
< 0 || pos
> vpd
->base
.len
|| end
> vpd
->base
.len
)
243 if (mutex_lock_killable(&vpd
->lock
))
246 ret
= pci_vpd_pci22_wait(dev
);
252 unsigned int i
, skip
;
254 ret
= pci_user_write_config_word(dev
, vpd
->cap
+ PCI_VPD_ADDR
,
259 vpd
->flag
= PCI_VPD_ADDR_F
;
260 ret
= pci_vpd_pci22_wait(dev
);
264 ret
= pci_user_read_config_dword(dev
, vpd
->cap
+ PCI_VPD_DATA
, &val
);
269 for (i
= 0; i
< sizeof(u32
); i
++) {
279 mutex_unlock(&vpd
->lock
);
280 return ret
? ret
: count
;
283 static ssize_t
pci_vpd_pci22_write(struct pci_dev
*dev
, loff_t pos
, size_t count
,
286 struct pci_vpd_pci22
*vpd
=
287 container_of(dev
->vpd
, struct pci_vpd_pci22
, base
);
289 loff_t end
= pos
+ count
;
292 if (pos
< 0 || (pos
& 3) || (count
& 3) || end
> vpd
->base
.len
)
295 if (mutex_lock_killable(&vpd
->lock
))
298 ret
= pci_vpd_pci22_wait(dev
);
310 ret
= pci_user_write_config_dword(dev
, vpd
->cap
+ PCI_VPD_DATA
, val
);
313 ret
= pci_user_write_config_word(dev
, vpd
->cap
+ PCI_VPD_ADDR
,
314 pos
| PCI_VPD_ADDR_F
);
320 ret
= pci_vpd_pci22_wait(dev
);
325 mutex_unlock(&vpd
->lock
);
326 return ret
? ret
: count
;
329 static void pci_vpd_pci22_release(struct pci_dev
*dev
)
331 kfree(container_of(dev
->vpd
, struct pci_vpd_pci22
, base
));
334 static const struct pci_vpd_ops pci_vpd_pci22_ops
= {
335 .read
= pci_vpd_pci22_read
,
336 .write
= pci_vpd_pci22_write
,
337 .release
= pci_vpd_pci22_release
,
340 int pci_vpd_pci22_init(struct pci_dev
*dev
)
342 struct pci_vpd_pci22
*vpd
;
345 cap
= pci_find_capability(dev
, PCI_CAP_ID_VPD
);
348 vpd
= kzalloc(sizeof(*vpd
), GFP_ATOMIC
);
352 vpd
->base
.len
= PCI_VPD_PCI22_SIZE
;
353 vpd
->base
.ops
= &pci_vpd_pci22_ops
;
354 mutex_init(&vpd
->lock
);
357 dev
->vpd
= &vpd
->base
;
362 * pci_vpd_truncate - Set available Vital Product Data size
363 * @dev: pci device struct
364 * @size: available memory in bytes
366 * Adjust size of available VPD area.
368 int pci_vpd_truncate(struct pci_dev
*dev
, size_t size
)
373 /* limited by the access method */
374 if (size
> dev
->vpd
->len
)
377 dev
->vpd
->len
= size
;
379 dev
->vpd
->attr
->size
= size
;
383 EXPORT_SYMBOL(pci_vpd_truncate
);
386 * pci_block_user_cfg_access - Block userspace PCI config reads/writes
387 * @dev: pci device struct
389 * When user access is blocked, any reads or writes to config space will
390 * sleep until access is unblocked again. We don't allow nesting of
391 * block/unblock calls.
393 void pci_block_user_cfg_access(struct pci_dev
*dev
)
398 spin_lock_irqsave(&pci_lock
, flags
);
399 was_blocked
= dev
->block_ucfg_access
;
400 dev
->block_ucfg_access
= 1;
401 spin_unlock_irqrestore(&pci_lock
, flags
);
403 /* If we BUG() inside the pci_lock, we're guaranteed to hose
407 EXPORT_SYMBOL_GPL(pci_block_user_cfg_access
);
410 * pci_unblock_user_cfg_access - Unblock userspace PCI config reads/writes
411 * @dev: pci device struct
413 * This function allows userspace PCI config accesses to resume.
415 void pci_unblock_user_cfg_access(struct pci_dev
*dev
)
419 spin_lock_irqsave(&pci_lock
, flags
);
421 /* This indicates a problem in the caller, but we don't need
422 * to kill them, unlike a double-block above. */
423 WARN_ON(!dev
->block_ucfg_access
);
425 dev
->block_ucfg_access
= 0;
426 wake_up_all(&pci_ucfg_wait
);
427 spin_unlock_irqrestore(&pci_lock
, flags
);
429 EXPORT_SYMBOL_GPL(pci_unblock_user_cfg_access
);