| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/pci.h> | 
|  | 2 | #include <linux/module.h> | 
| Al Viro | f6a5703 | 2006-10-18 01:47:25 -0400 | [diff] [blame] | 3 | #include <linux/sched.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/ioport.h> | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 5 | #include <linux/wait.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 |  | 
| Adrian Bunk | 48b1914 | 2005-11-06 01:45:08 +0100 | [diff] [blame] | 7 | #include "pci.h" | 
|  | 8 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | /* | 
|  | 10 | * This interrupt-safe spinlock protects all accesses to PCI | 
|  | 11 | * configuration space. | 
|  | 12 | */ | 
|  | 13 |  | 
|  | 14 | static DEFINE_SPINLOCK(pci_lock); | 
|  | 15 |  | 
|  | 16 | /* | 
|  | 17 | *  Wrappers for all PCI configuration access functions.  They just check | 
|  | 18 | *  alignment, do locking and call the low-level functions pointed to | 
|  | 19 | *  by pci_dev->ops. | 
|  | 20 | */ | 
|  | 21 |  | 
|  | 22 | #define PCI_byte_BAD 0 | 
|  | 23 | #define PCI_word_BAD (pos & 1) | 
|  | 24 | #define PCI_dword_BAD (pos & 3) | 
|  | 25 |  | 
|  | 26 | #define PCI_OP_READ(size,type,len) \ | 
|  | 27 | int pci_bus_read_config_##size \ | 
|  | 28 | (struct pci_bus *bus, unsigned int devfn, int pos, type *value)	\ | 
|  | 29 | {									\ | 
|  | 30 | int res;							\ | 
|  | 31 | unsigned long flags;						\ | 
|  | 32 | u32 data = 0;							\ | 
|  | 33 | if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER;	\ | 
|  | 34 | spin_lock_irqsave(&pci_lock, flags);				\ | 
|  | 35 | res = bus->ops->read(bus, devfn, pos, len, &data);		\ | 
|  | 36 | *value = (type)data;						\ | 
|  | 37 | spin_unlock_irqrestore(&pci_lock, flags);			\ | 
|  | 38 | return res;							\ | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | #define PCI_OP_WRITE(size,type,len) \ | 
|  | 42 | int pci_bus_write_config_##size \ | 
|  | 43 | (struct pci_bus *bus, unsigned int devfn, int pos, type value)	\ | 
|  | 44 | {									\ | 
|  | 45 | int res;							\ | 
|  | 46 | unsigned long flags;						\ | 
|  | 47 | if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER;	\ | 
|  | 48 | spin_lock_irqsave(&pci_lock, flags);				\ | 
|  | 49 | res = bus->ops->write(bus, devfn, pos, len, value);		\ | 
|  | 50 | spin_unlock_irqrestore(&pci_lock, flags);			\ | 
|  | 51 | return res;							\ | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | PCI_OP_READ(byte, u8, 1) | 
|  | 55 | PCI_OP_READ(word, u16, 2) | 
|  | 56 | PCI_OP_READ(dword, u32, 4) | 
|  | 57 | PCI_OP_WRITE(byte, u8, 1) | 
|  | 58 | PCI_OP_WRITE(word, u16, 2) | 
|  | 59 | PCI_OP_WRITE(dword, u32, 4) | 
|  | 60 |  | 
|  | 61 | EXPORT_SYMBOL(pci_bus_read_config_byte); | 
|  | 62 | EXPORT_SYMBOL(pci_bus_read_config_word); | 
|  | 63 | EXPORT_SYMBOL(pci_bus_read_config_dword); | 
|  | 64 | EXPORT_SYMBOL(pci_bus_write_config_byte); | 
|  | 65 | EXPORT_SYMBOL(pci_bus_write_config_word); | 
|  | 66 | EXPORT_SYMBOL(pci_bus_write_config_dword); | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 67 |  | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 68 | /* | 
|  | 69 | * The following routines are to prevent the user from accessing PCI config | 
|  | 70 | * space when it's unsafe to do so.  Some devices require this during BIST and | 
|  | 71 | * we're required to prevent it during D-state transitions. | 
|  | 72 | * | 
|  | 73 | * We have a bit per device to indicate it's blocked and a global wait queue | 
|  | 74 | * for callers to sleep on until devices are unblocked. | 
|  | 75 | */ | 
|  | 76 | static DECLARE_WAIT_QUEUE_HEAD(pci_ucfg_wait); | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 77 |  | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 78 | static noinline void pci_wait_ucfg(struct pci_dev *dev) | 
|  | 79 | { | 
|  | 80 | DECLARE_WAITQUEUE(wait, current); | 
|  | 81 |  | 
|  | 82 | __add_wait_queue(&pci_ucfg_wait, &wait); | 
|  | 83 | do { | 
|  | 84 | set_current_state(TASK_UNINTERRUPTIBLE); | 
|  | 85 | spin_unlock_irq(&pci_lock); | 
|  | 86 | schedule(); | 
|  | 87 | spin_lock_irq(&pci_lock); | 
|  | 88 | } while (dev->block_ucfg_access); | 
|  | 89 | __remove_wait_queue(&pci_ucfg_wait, &wait); | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
|  | 92 | #define PCI_USER_READ_CONFIG(size,type)					\ | 
|  | 93 | int pci_user_read_config_##size						\ | 
|  | 94 | (struct pci_dev *dev, int pos, type *val)			\ | 
|  | 95 | {									\ | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 96 | int ret = 0;							\ | 
|  | 97 | u32 data = -1;							\ | 
|  | 98 | if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER;	\ | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 99 | spin_lock_irq(&pci_lock);					\ | 
|  | 100 | if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev);	\ | 
|  | 101 | ret = dev->bus->ops->read(dev->bus, dev->devfn,			\ | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 102 | pos, sizeof(type), &data);	\ | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 103 | spin_unlock_irq(&pci_lock);					\ | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 104 | *val = (type)data;						\ | 
|  | 105 | return ret;							\ | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | #define PCI_USER_WRITE_CONFIG(size,type)				\ | 
|  | 109 | int pci_user_write_config_##size					\ | 
|  | 110 | (struct pci_dev *dev, int pos, type val)			\ | 
|  | 111 | {									\ | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 112 | int ret = -EIO;							\ | 
|  | 113 | if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER;	\ | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 114 | spin_lock_irq(&pci_lock);					\ | 
|  | 115 | if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev);	\ | 
|  | 116 | ret = dev->bus->ops->write(dev->bus, dev->devfn,		\ | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 117 | pos, sizeof(type), val);	\ | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 118 | spin_unlock_irq(&pci_lock);					\ | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 119 | return ret;							\ | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | PCI_USER_READ_CONFIG(byte, u8) | 
|  | 123 | PCI_USER_READ_CONFIG(word, u16) | 
|  | 124 | PCI_USER_READ_CONFIG(dword, u32) | 
|  | 125 | PCI_USER_WRITE_CONFIG(byte, u8) | 
|  | 126 | PCI_USER_WRITE_CONFIG(word, u16) | 
|  | 127 | PCI_USER_WRITE_CONFIG(dword, u32) | 
|  | 128 |  | 
|  | 129 | /** | 
|  | 130 | * pci_block_user_cfg_access - Block userspace PCI config reads/writes | 
|  | 131 | * @dev:	pci device struct | 
|  | 132 | * | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 133 | * When user access is blocked, any reads or writes to config space will | 
|  | 134 | * sleep until access is unblocked again.  We don't allow nesting of | 
|  | 135 | * block/unblock calls. | 
|  | 136 | */ | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 137 | void pci_block_user_cfg_access(struct pci_dev *dev) | 
|  | 138 | { | 
|  | 139 | unsigned long flags; | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 140 | int was_blocked; | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 141 |  | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 142 | spin_lock_irqsave(&pci_lock, flags); | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 143 | was_blocked = dev->block_ucfg_access; | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 144 | dev->block_ucfg_access = 1; | 
|  | 145 | spin_unlock_irqrestore(&pci_lock, flags); | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 146 |  | 
|  | 147 | /* If we BUG() inside the pci_lock, we're guaranteed to hose | 
|  | 148 | * the machine */ | 
|  | 149 | BUG_ON(was_blocked); | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 150 | } | 
|  | 151 | EXPORT_SYMBOL_GPL(pci_block_user_cfg_access); | 
|  | 152 |  | 
|  | 153 | /** | 
|  | 154 | * pci_unblock_user_cfg_access - Unblock userspace PCI config reads/writes | 
|  | 155 | * @dev:	pci device struct | 
|  | 156 | * | 
|  | 157 | * This function allows userspace PCI config accesses to resume. | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 158 | */ | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 159 | void pci_unblock_user_cfg_access(struct pci_dev *dev) | 
|  | 160 | { | 
|  | 161 | unsigned long flags; | 
|  | 162 |  | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 163 | spin_lock_irqsave(&pci_lock, flags); | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 164 |  | 
|  | 165 | /* This indicates a problem in the caller, but we don't need | 
|  | 166 | * to kill them, unlike a double-block above. */ | 
|  | 167 | WARN_ON(!dev->block_ucfg_access); | 
|  | 168 |  | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 169 | dev->block_ucfg_access = 0; | 
| Matthew Wilcox | 7ea7e98 | 2006-10-19 09:41:28 -0600 | [diff] [blame] | 170 | wake_up_all(&pci_ucfg_wait); | 
| Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 171 | spin_unlock_irqrestore(&pci_lock, flags); | 
|  | 172 | } | 
|  | 173 | EXPORT_SYMBOL_GPL(pci_unblock_user_cfg_access); |