blob: 73a58c73d526855fc93bdb6e26e4f6790be2ad41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/pci.h>
2#include <linux/module.h>
3#include <linux/ioport.h>
Matthew Wilcox7ea7e982006-10-19 09:41:28 -06004#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
Adrian Bunk48b19142005-11-06 01:45:08 +01006#include "pci.h"
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008/*
9 * This interrupt-safe spinlock protects all accesses to PCI
10 * configuration space.
11 */
12
13static DEFINE_SPINLOCK(pci_lock);
14
15/*
16 * Wrappers for all PCI configuration access functions. They just check
17 * alignment, do locking and call the low-level functions pointed to
18 * by pci_dev->ops.
19 */
20
21#define PCI_byte_BAD 0
22#define PCI_word_BAD (pos & 1)
23#define PCI_dword_BAD (pos & 3)
24
25#define PCI_OP_READ(size,type,len) \
26int pci_bus_read_config_##size \
27 (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \
28{ \
29 int res; \
30 unsigned long flags; \
31 u32 data = 0; \
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); \
37 return res; \
38}
39
40#define PCI_OP_WRITE(size,type,len) \
41int pci_bus_write_config_##size \
42 (struct pci_bus *bus, unsigned int devfn, int pos, type value) \
43{ \
44 int res; \
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); \
50 return res; \
51}
52
53PCI_OP_READ(byte, u8, 1)
54PCI_OP_READ(word, u16, 2)
55PCI_OP_READ(dword, u32, 4)
56PCI_OP_WRITE(byte, u8, 1)
57PCI_OP_WRITE(word, u16, 2)
58PCI_OP_WRITE(dword, u32, 4)
59
60EXPORT_SYMBOL(pci_bus_read_config_byte);
61EXPORT_SYMBOL(pci_bus_read_config_word);
62EXPORT_SYMBOL(pci_bus_read_config_dword);
63EXPORT_SYMBOL(pci_bus_write_config_byte);
64EXPORT_SYMBOL(pci_bus_write_config_word);
65EXPORT_SYMBOL(pci_bus_write_config_dword);
Brian Kinge04b0ea2005-09-27 01:21:55 -070066
Matthew Wilcox7ea7e982006-10-19 09:41:28 -060067/*
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.
71 *
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.
74 */
75static DECLARE_WAIT_QUEUE_HEAD(pci_ucfg_wait);
Brian Kinge04b0ea2005-09-27 01:21:55 -070076
Matthew Wilcox7ea7e982006-10-19 09:41:28 -060077static noinline void pci_wait_ucfg(struct pci_dev *dev)
78{
79 DECLARE_WAITQUEUE(wait, current);
80
81 __add_wait_queue(&pci_ucfg_wait, &wait);
82 do {
83 set_current_state(TASK_UNINTERRUPTIBLE);
84 spin_unlock_irq(&pci_lock);
85 schedule();
86 spin_lock_irq(&pci_lock);
87 } while (dev->block_ucfg_access);
88 __remove_wait_queue(&pci_ucfg_wait, &wait);
Brian Kinge04b0ea2005-09-27 01:21:55 -070089}
90
91#define PCI_USER_READ_CONFIG(size,type) \
92int pci_user_read_config_##size \
93 (struct pci_dev *dev, int pos, type *val) \
94{ \
Brian Kinge04b0ea2005-09-27 01:21:55 -070095 int ret = 0; \
96 u32 data = -1; \
97 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
Matthew Wilcox7ea7e982006-10-19 09:41:28 -060098 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, \
Brian Kinge04b0ea2005-09-27 01:21:55 -0700101 pos, sizeof(type), &data); \
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600102 spin_unlock_irq(&pci_lock); \
Brian Kinge04b0ea2005-09-27 01:21:55 -0700103 *val = (type)data; \
104 return ret; \
105}
106
107#define PCI_USER_WRITE_CONFIG(size,type) \
108int pci_user_write_config_##size \
109 (struct pci_dev *dev, int pos, type val) \
110{ \
Brian Kinge04b0ea2005-09-27 01:21:55 -0700111 int ret = -EIO; \
112 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600113 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, \
Brian Kinge04b0ea2005-09-27 01:21:55 -0700116 pos, sizeof(type), val); \
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600117 spin_unlock_irq(&pci_lock); \
Brian Kinge04b0ea2005-09-27 01:21:55 -0700118 return ret; \
119}
120
121PCI_USER_READ_CONFIG(byte, u8)
122PCI_USER_READ_CONFIG(word, u16)
123PCI_USER_READ_CONFIG(dword, u32)
124PCI_USER_WRITE_CONFIG(byte, u8)
125PCI_USER_WRITE_CONFIG(word, u16)
126PCI_USER_WRITE_CONFIG(dword, u32)
127
128/**
129 * pci_block_user_cfg_access - Block userspace PCI config reads/writes
130 * @dev: pci device struct
131 *
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600132 * 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.
135 */
Brian Kinge04b0ea2005-09-27 01:21:55 -0700136void pci_block_user_cfg_access(struct pci_dev *dev)
137{
138 unsigned long flags;
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600139 int was_blocked;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700140
Brian Kinge04b0ea2005-09-27 01:21:55 -0700141 spin_lock_irqsave(&pci_lock, flags);
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600142 was_blocked = dev->block_ucfg_access;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700143 dev->block_ucfg_access = 1;
144 spin_unlock_irqrestore(&pci_lock, flags);
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600145
146 /* If we BUG() inside the pci_lock, we're guaranteed to hose
147 * the machine */
148 BUG_ON(was_blocked);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700149}
150EXPORT_SYMBOL_GPL(pci_block_user_cfg_access);
151
152/**
153 * pci_unblock_user_cfg_access - Unblock userspace PCI config reads/writes
154 * @dev: pci device struct
155 *
156 * This function allows userspace PCI config accesses to resume.
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600157 */
Brian Kinge04b0ea2005-09-27 01:21:55 -0700158void pci_unblock_user_cfg_access(struct pci_dev *dev)
159{
160 unsigned long flags;
161
Brian Kinge04b0ea2005-09-27 01:21:55 -0700162 spin_lock_irqsave(&pci_lock, flags);
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600163
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);
167
Brian Kinge04b0ea2005-09-27 01:21:55 -0700168 dev->block_ucfg_access = 0;
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600169 wake_up_all(&pci_ucfg_wait);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700170 spin_unlock_irqrestore(&pci_lock, flags);
171}
172EXPORT_SYMBOL_GPL(pci_unblock_user_cfg_access);