Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * PCI Bus Services, see include/linux/pci.h for further explanation. |
| 3 | * |
| 4 | * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter, |
| 5 | * David Mosberger-Tang |
| 6 | * |
| 7 | * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/pci.h> |
David Brownell | 075c177 | 2007-04-26 00:12:06 -0700 | [diff] [blame] | 14 | #include <linux/pm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/spinlock.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 17 | #include <linux/string.h> |
vignesh babu | 229f5af | 2007-08-13 18:23:14 +0530 | [diff] [blame] | 18 | #include <linux/log2.h> |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 19 | #include <linux/pci-aspm.h> |
Stephen Rothwell | c300bd2fb | 2008-07-10 02:16:44 +0200 | [diff] [blame] | 20 | #include <linux/pm_wakeup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/dma.h> /* isa_dma_bridge_buggy */ |
Greg KH | bc56b9e | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 22 | #include "pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Kristen Carlson Accardi | ffadcc2 | 2006-07-12 08:59:00 -0700 | [diff] [blame] | 24 | unsigned int pci_pm_d3_delay = 10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Jeff Garzik | 32a2eea | 2007-10-11 16:57:27 -0400 | [diff] [blame] | 26 | #ifdef CONFIG_PCI_DOMAINS |
| 27 | int pci_domains_supported = 1; |
| 28 | #endif |
| 29 | |
Atsushi Nemoto | 4516a61 | 2007-02-05 16:36:06 -0800 | [diff] [blame] | 30 | #define DEFAULT_CARDBUS_IO_SIZE (256) |
| 31 | #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024) |
| 32 | /* pci=cbmemsize=nnM,cbiosize=nn can override this */ |
| 33 | unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE; |
| 34 | unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE; |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /** |
| 37 | * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children |
| 38 | * @bus: pointer to PCI bus structure to search |
| 39 | * |
| 40 | * Given a PCI bus, returns the highest PCI bus number present in the set |
| 41 | * including the given PCI bus and its list of child PCI buses. |
| 42 | */ |
Sam Ravnborg | 96bde06 | 2007-03-26 21:53:30 -0800 | [diff] [blame] | 43 | unsigned char pci_bus_max_busnr(struct pci_bus* bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | struct list_head *tmp; |
| 46 | unsigned char max, n; |
| 47 | |
Kristen Accardi | b82db5c | 2006-01-17 16:56:56 -0800 | [diff] [blame] | 48 | max = bus->subordinate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | list_for_each(tmp, &bus->children) { |
| 50 | n = pci_bus_max_busnr(pci_bus_b(tmp)); |
| 51 | if(n > max) |
| 52 | max = n; |
| 53 | } |
| 54 | return max; |
| 55 | } |
Kristen Accardi | b82db5c | 2006-01-17 16:56:56 -0800 | [diff] [blame] | 56 | EXPORT_SYMBOL_GPL(pci_bus_max_busnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Kristen Accardi | b82db5c | 2006-01-17 16:56:56 -0800 | [diff] [blame] | 58 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | /** |
| 60 | * pci_max_busnr - returns maximum PCI bus number |
| 61 | * |
| 62 | * Returns the highest PCI bus number present in the system global list of |
| 63 | * PCI buses. |
| 64 | */ |
| 65 | unsigned char __devinit |
| 66 | pci_max_busnr(void) |
| 67 | { |
| 68 | struct pci_bus *bus = NULL; |
| 69 | unsigned char max, n; |
| 70 | |
| 71 | max = 0; |
| 72 | while ((bus = pci_find_next_bus(bus)) != NULL) { |
| 73 | n = pci_bus_max_busnr(bus); |
| 74 | if(n > max) |
| 75 | max = n; |
| 76 | } |
| 77 | return max; |
| 78 | } |
| 79 | |
Adrian Bunk | 54c762f | 2005-12-22 01:08:52 +0100 | [diff] [blame] | 80 | #endif /* 0 */ |
| 81 | |
Michael Ellerman | 687d5fe | 2006-11-22 18:26:18 +1100 | [diff] [blame] | 82 | #define PCI_FIND_CAP_TTL 48 |
| 83 | |
| 84 | static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn, |
| 85 | u8 pos, int cap, int *ttl) |
Roland Dreier | 24a4e37 | 2005-10-28 17:35:34 -0700 | [diff] [blame] | 86 | { |
| 87 | u8 id; |
Roland Dreier | 24a4e37 | 2005-10-28 17:35:34 -0700 | [diff] [blame] | 88 | |
Michael Ellerman | 687d5fe | 2006-11-22 18:26:18 +1100 | [diff] [blame] | 89 | while ((*ttl)--) { |
Roland Dreier | 24a4e37 | 2005-10-28 17:35:34 -0700 | [diff] [blame] | 90 | pci_bus_read_config_byte(bus, devfn, pos, &pos); |
| 91 | if (pos < 0x40) |
| 92 | break; |
| 93 | pos &= ~3; |
| 94 | pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID, |
| 95 | &id); |
| 96 | if (id == 0xff) |
| 97 | break; |
| 98 | if (id == cap) |
| 99 | return pos; |
| 100 | pos += PCI_CAP_LIST_NEXT; |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | |
Michael Ellerman | 687d5fe | 2006-11-22 18:26:18 +1100 | [diff] [blame] | 105 | static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn, |
| 106 | u8 pos, int cap) |
| 107 | { |
| 108 | int ttl = PCI_FIND_CAP_TTL; |
| 109 | |
| 110 | return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl); |
| 111 | } |
| 112 | |
Roland Dreier | 24a4e37 | 2005-10-28 17:35:34 -0700 | [diff] [blame] | 113 | int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap) |
| 114 | { |
| 115 | return __pci_find_next_cap(dev->bus, dev->devfn, |
| 116 | pos + PCI_CAP_LIST_NEXT, cap); |
| 117 | } |
| 118 | EXPORT_SYMBOL_GPL(pci_find_next_capability); |
| 119 | |
Michael Ellerman | d3bac11 | 2006-11-22 18:26:16 +1100 | [diff] [blame] | 120 | static int __pci_bus_find_cap_start(struct pci_bus *bus, |
| 121 | unsigned int devfn, u8 hdr_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | u16 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status); |
| 126 | if (!(status & PCI_STATUS_CAP_LIST)) |
| 127 | return 0; |
| 128 | |
| 129 | switch (hdr_type) { |
| 130 | case PCI_HEADER_TYPE_NORMAL: |
| 131 | case PCI_HEADER_TYPE_BRIDGE: |
Michael Ellerman | d3bac11 | 2006-11-22 18:26:16 +1100 | [diff] [blame] | 132 | return PCI_CAPABILITY_LIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | case PCI_HEADER_TYPE_CARDBUS: |
Michael Ellerman | d3bac11 | 2006-11-22 18:26:16 +1100 | [diff] [blame] | 134 | return PCI_CB_CAPABILITY_LIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | default: |
| 136 | return 0; |
| 137 | } |
Michael Ellerman | d3bac11 | 2006-11-22 18:26:16 +1100 | [diff] [blame] | 138 | |
| 139 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * pci_find_capability - query for devices' capabilities |
| 144 | * @dev: PCI device to query |
| 145 | * @cap: capability code |
| 146 | * |
| 147 | * Tell if a device supports a given PCI capability. |
| 148 | * Returns the address of the requested capability structure within the |
| 149 | * device's PCI configuration space or 0 in case the device does not |
| 150 | * support it. Possible values for @cap: |
| 151 | * |
| 152 | * %PCI_CAP_ID_PM Power Management |
| 153 | * %PCI_CAP_ID_AGP Accelerated Graphics Port |
| 154 | * %PCI_CAP_ID_VPD Vital Product Data |
| 155 | * %PCI_CAP_ID_SLOTID Slot Identification |
| 156 | * %PCI_CAP_ID_MSI Message Signalled Interrupts |
| 157 | * %PCI_CAP_ID_CHSWP CompactPCI HotSwap |
| 158 | * %PCI_CAP_ID_PCIX PCI-X |
| 159 | * %PCI_CAP_ID_EXP PCI Express |
| 160 | */ |
| 161 | int pci_find_capability(struct pci_dev *dev, int cap) |
| 162 | { |
Michael Ellerman | d3bac11 | 2006-11-22 18:26:16 +1100 | [diff] [blame] | 163 | int pos; |
| 164 | |
| 165 | pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type); |
| 166 | if (pos) |
| 167 | pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap); |
| 168 | |
| 169 | return pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * pci_bus_find_capability - query for devices' capabilities |
| 174 | * @bus: the PCI bus to query |
| 175 | * @devfn: PCI device to query |
| 176 | * @cap: capability code |
| 177 | * |
| 178 | * Like pci_find_capability() but works for pci devices that do not have a |
| 179 | * pci_dev structure set up yet. |
| 180 | * |
| 181 | * Returns the address of the requested capability structure within the |
| 182 | * device's PCI configuration space or 0 in case the device does not |
| 183 | * support it. |
| 184 | */ |
| 185 | int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap) |
| 186 | { |
Michael Ellerman | d3bac11 | 2006-11-22 18:26:16 +1100 | [diff] [blame] | 187 | int pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | u8 hdr_type; |
| 189 | |
| 190 | pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type); |
| 191 | |
Michael Ellerman | d3bac11 | 2006-11-22 18:26:16 +1100 | [diff] [blame] | 192 | pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f); |
| 193 | if (pos) |
| 194 | pos = __pci_find_next_cap(bus, devfn, pos, cap); |
| 195 | |
| 196 | return pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * pci_find_ext_capability - Find an extended capability |
| 201 | * @dev: PCI device to query |
| 202 | * @cap: capability code |
| 203 | * |
| 204 | * Returns the address of the requested extended capability structure |
| 205 | * within the device's PCI configuration space or 0 if the device does |
| 206 | * not support it. Possible values for @cap: |
| 207 | * |
| 208 | * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting |
| 209 | * %PCI_EXT_CAP_ID_VC Virtual Channel |
| 210 | * %PCI_EXT_CAP_ID_DSN Device Serial Number |
| 211 | * %PCI_EXT_CAP_ID_PWR Power Budgeting |
| 212 | */ |
| 213 | int pci_find_ext_capability(struct pci_dev *dev, int cap) |
| 214 | { |
| 215 | u32 header; |
Zhao, Yu | 557848c | 2008-10-13 19:18:07 +0800 | [diff] [blame] | 216 | int ttl; |
| 217 | int pos = PCI_CFG_SPACE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Zhao, Yu | 557848c | 2008-10-13 19:18:07 +0800 | [diff] [blame] | 219 | /* minimum 8 bytes per capability */ |
| 220 | ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8; |
| 221 | |
| 222 | if (dev->cfg_size <= PCI_CFG_SPACE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | return 0; |
| 224 | |
| 225 | if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL) |
| 226 | return 0; |
| 227 | |
| 228 | /* |
| 229 | * If we have no capabilities, this is indicated by cap ID, |
| 230 | * cap version and next pointer all being 0. |
| 231 | */ |
| 232 | if (header == 0) |
| 233 | return 0; |
| 234 | |
| 235 | while (ttl-- > 0) { |
| 236 | if (PCI_EXT_CAP_ID(header) == cap) |
| 237 | return pos; |
| 238 | |
| 239 | pos = PCI_EXT_CAP_NEXT(header); |
Zhao, Yu | 557848c | 2008-10-13 19:18:07 +0800 | [diff] [blame] | 240 | if (pos < PCI_CFG_SPACE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | break; |
| 242 | |
| 243 | if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL) |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
Brice Goglin | 3a720d7 | 2006-05-23 06:10:01 -0400 | [diff] [blame] | 249 | EXPORT_SYMBOL_GPL(pci_find_ext_capability); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Michael Ellerman | 687d5fe | 2006-11-22 18:26:18 +1100 | [diff] [blame] | 251 | static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap) |
| 252 | { |
| 253 | int rc, ttl = PCI_FIND_CAP_TTL; |
| 254 | u8 cap, mask; |
| 255 | |
| 256 | if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST) |
| 257 | mask = HT_3BIT_CAP_MASK; |
| 258 | else |
| 259 | mask = HT_5BIT_CAP_MASK; |
| 260 | |
| 261 | pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos, |
| 262 | PCI_CAP_ID_HT, &ttl); |
| 263 | while (pos) { |
| 264 | rc = pci_read_config_byte(dev, pos + 3, &cap); |
| 265 | if (rc != PCIBIOS_SUCCESSFUL) |
| 266 | return 0; |
| 267 | |
| 268 | if ((cap & mask) == ht_cap) |
| 269 | return pos; |
| 270 | |
Brice Goglin | 47a4d5b | 2007-01-10 23:15:29 -0800 | [diff] [blame] | 271 | pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, |
| 272 | pos + PCI_CAP_LIST_NEXT, |
Michael Ellerman | 687d5fe | 2006-11-22 18:26:18 +1100 | [diff] [blame] | 273 | PCI_CAP_ID_HT, &ttl); |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | /** |
| 279 | * pci_find_next_ht_capability - query a device's Hypertransport capabilities |
| 280 | * @dev: PCI device to query |
| 281 | * @pos: Position from which to continue searching |
| 282 | * @ht_cap: Hypertransport capability code |
| 283 | * |
| 284 | * To be used in conjunction with pci_find_ht_capability() to search for |
| 285 | * all capabilities matching @ht_cap. @pos should always be a value returned |
| 286 | * from pci_find_ht_capability(). |
| 287 | * |
| 288 | * NB. To be 100% safe against broken PCI devices, the caller should take |
| 289 | * steps to avoid an infinite loop. |
| 290 | */ |
| 291 | int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap) |
| 292 | { |
| 293 | return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap); |
| 294 | } |
| 295 | EXPORT_SYMBOL_GPL(pci_find_next_ht_capability); |
| 296 | |
| 297 | /** |
| 298 | * pci_find_ht_capability - query a device's Hypertransport capabilities |
| 299 | * @dev: PCI device to query |
| 300 | * @ht_cap: Hypertransport capability code |
| 301 | * |
| 302 | * Tell if a device supports a given Hypertransport capability. |
| 303 | * Returns an address within the device's PCI configuration space |
| 304 | * or 0 in case the device does not support the request capability. |
| 305 | * The address points to the PCI capability, of type PCI_CAP_ID_HT, |
| 306 | * which has a Hypertransport capability matching @ht_cap. |
| 307 | */ |
| 308 | int pci_find_ht_capability(struct pci_dev *dev, int ht_cap) |
| 309 | { |
| 310 | int pos; |
| 311 | |
| 312 | pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type); |
| 313 | if (pos) |
| 314 | pos = __pci_find_next_ht_cap(dev, pos, ht_cap); |
| 315 | |
| 316 | return pos; |
| 317 | } |
| 318 | EXPORT_SYMBOL_GPL(pci_find_ht_capability); |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | /** |
| 321 | * pci_find_parent_resource - return resource region of parent bus of given region |
| 322 | * @dev: PCI device structure contains resources to be searched |
| 323 | * @res: child resource record for which parent is sought |
| 324 | * |
| 325 | * For given resource region of given device, return the resource |
| 326 | * region of parent bus the given region is contained in or where |
| 327 | * it should be allocated from. |
| 328 | */ |
| 329 | struct resource * |
| 330 | pci_find_parent_resource(const struct pci_dev *dev, struct resource *res) |
| 331 | { |
| 332 | const struct pci_bus *bus = dev->bus; |
| 333 | int i; |
| 334 | struct resource *best = NULL; |
| 335 | |
| 336 | for(i = 0; i < PCI_BUS_NUM_RESOURCES; i++) { |
| 337 | struct resource *r = bus->resource[i]; |
| 338 | if (!r) |
| 339 | continue; |
| 340 | if (res->start && !(res->start >= r->start && res->end <= r->end)) |
| 341 | continue; /* Not contained */ |
| 342 | if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM)) |
| 343 | continue; /* Wrong type */ |
| 344 | if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH)) |
| 345 | return r; /* Exact match */ |
| 346 | if ((res->flags & IORESOURCE_PREFETCH) && !(r->flags & IORESOURCE_PREFETCH)) |
| 347 | best = r; /* Approximating prefetchable by non-prefetchable */ |
| 348 | } |
| 349 | return best; |
| 350 | } |
| 351 | |
| 352 | /** |
John W. Linville | 064b53db | 2005-07-27 10:19:44 -0400 | [diff] [blame] | 353 | * pci_restore_bars - restore a devices BAR values (e.g. after wake-up) |
| 354 | * @dev: PCI device to have its BARs restored |
| 355 | * |
| 356 | * Restore the BAR values for a given device, so as to make it |
| 357 | * accessible by its driver. |
| 358 | */ |
Adrian Bunk | ad668599 | 2007-10-27 03:06:22 +0200 | [diff] [blame] | 359 | static void |
John W. Linville | 064b53db | 2005-07-27 10:19:44 -0400 | [diff] [blame] | 360 | pci_restore_bars(struct pci_dev *dev) |
| 361 | { |
| 362 | int i, numres; |
| 363 | |
| 364 | switch (dev->hdr_type) { |
| 365 | case PCI_HEADER_TYPE_NORMAL: |
| 366 | numres = 6; |
| 367 | break; |
| 368 | case PCI_HEADER_TYPE_BRIDGE: |
| 369 | numres = 2; |
| 370 | break; |
| 371 | case PCI_HEADER_TYPE_CARDBUS: |
| 372 | numres = 1; |
| 373 | break; |
| 374 | default: |
| 375 | /* Should never get here, but just in case... */ |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | for (i = 0; i < numres; i ++) |
| 380 | pci_update_resource(dev, &dev->resource[i], i); |
| 381 | } |
| 382 | |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 383 | static struct pci_platform_pm_ops *pci_platform_pm; |
| 384 | |
| 385 | int pci_set_platform_pm(struct pci_platform_pm_ops *ops) |
| 386 | { |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 387 | if (!ops->is_manageable || !ops->set_state || !ops->choose_state |
| 388 | || !ops->sleep_wake || !ops->can_wakeup) |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 389 | return -EINVAL; |
| 390 | pci_platform_pm = ops; |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static inline bool platform_pci_power_manageable(struct pci_dev *dev) |
| 395 | { |
| 396 | return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false; |
| 397 | } |
| 398 | |
| 399 | static inline int platform_pci_set_power_state(struct pci_dev *dev, |
| 400 | pci_power_t t) |
| 401 | { |
| 402 | return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS; |
| 403 | } |
| 404 | |
| 405 | static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev) |
| 406 | { |
| 407 | return pci_platform_pm ? |
| 408 | pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR; |
| 409 | } |
Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 410 | |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 411 | static inline bool platform_pci_can_wakeup(struct pci_dev *dev) |
| 412 | { |
| 413 | return pci_platform_pm ? pci_platform_pm->can_wakeup(dev) : false; |
| 414 | } |
| 415 | |
| 416 | static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable) |
| 417 | { |
| 418 | return pci_platform_pm ? |
| 419 | pci_platform_pm->sleep_wake(dev, enable) : -ENODEV; |
| 420 | } |
| 421 | |
John W. Linville | 064b53db | 2005-07-27 10:19:44 -0400 | [diff] [blame] | 422 | /** |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 423 | * pci_raw_set_power_state - Use PCI PM registers to set the power state of |
| 424 | * given PCI device |
| 425 | * @dev: PCI device to handle. |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 426 | * @state: PCI power state (D0, D1, D2, D3hot) to put the device into. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | * |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 428 | * RETURN VALUE: |
| 429 | * -EINVAL if the requested state is invalid. |
| 430 | * -EIO if device does not support PCI PM or its PM capabilities register has a |
| 431 | * wrong version, or device doesn't support the requested state. |
| 432 | * 0 if device already is in the requested state. |
| 433 | * 0 if device's power state has been successfully changed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | */ |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 435 | static int |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 436 | pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | { |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 438 | u16 pmcsr; |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 439 | bool need_restore = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 441 | if (!dev->pm_cap) |
Andrew Lunn | cca03de | 2007-07-09 11:55:58 -0700 | [diff] [blame] | 442 | return -EIO; |
| 443 | |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 444 | if (state < PCI_D0 || state > PCI_D3hot) |
| 445 | return -EINVAL; |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | /* Validate current state: |
| 448 | * Can enter D0 from any state, but if we can only go deeper |
| 449 | * to sleep if we're already in a low power state |
| 450 | */ |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 451 | if (dev->current_state == state) { |
| 452 | /* we're already there */ |
| 453 | return 0; |
| 454 | } else if (state != PCI_D0 && dev->current_state <= PCI_D3cold |
| 455 | && dev->current_state > state) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 456 | dev_err(&dev->dev, "invalid power transition " |
| 457 | "(from state %d to %d)\n", dev->current_state, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | return -EINVAL; |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | /* check if this device supports the desired state */ |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 462 | if ((state == PCI_D1 && !dev->d1_support) |
| 463 | || (state == PCI_D2 && !dev->d2_support)) |
Daniel Ritz | 3fe9d19 | 2005-08-17 15:32:19 -0700 | [diff] [blame] | 464 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 466 | pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); |
John W. Linville | 064b53db | 2005-07-27 10:19:44 -0400 | [diff] [blame] | 467 | |
John W. Linville | 32a3658 | 2005-09-14 09:52:42 -0400 | [diff] [blame] | 468 | /* If we're (effectively) in D3, force entire word to 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | * This doesn't affect PME_Status, disables PME_En, and |
| 470 | * sets PowerState to 0. |
| 471 | */ |
John W. Linville | 32a3658 | 2005-09-14 09:52:42 -0400 | [diff] [blame] | 472 | switch (dev->current_state) { |
John W. Linville | d3535fb | 2005-09-28 17:50:51 -0400 | [diff] [blame] | 473 | case PCI_D0: |
| 474 | case PCI_D1: |
| 475 | case PCI_D2: |
| 476 | pmcsr &= ~PCI_PM_CTRL_STATE_MASK; |
| 477 | pmcsr |= state; |
| 478 | break; |
John W. Linville | 32a3658 | 2005-09-14 09:52:42 -0400 | [diff] [blame] | 479 | case PCI_UNKNOWN: /* Boot-up */ |
| 480 | if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot |
| 481 | && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET)) |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 482 | need_restore = true; |
John W. Linville | 32a3658 | 2005-09-14 09:52:42 -0400 | [diff] [blame] | 483 | /* Fall-through: force to D0 */ |
John W. Linville | 32a3658 | 2005-09-14 09:52:42 -0400 | [diff] [blame] | 484 | default: |
John W. Linville | d3535fb | 2005-09-28 17:50:51 -0400 | [diff] [blame] | 485 | pmcsr = 0; |
John W. Linville | 32a3658 | 2005-09-14 09:52:42 -0400 | [diff] [blame] | 486 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* enter specified state */ |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 490 | pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | /* Mandatory power management transition delays */ |
| 493 | /* see PCI PM 1.1 5.6.1 table 18 */ |
| 494 | if (state == PCI_D3hot || dev->current_state == PCI_D3hot) |
Kristen Carlson Accardi | ffadcc2 | 2006-07-12 08:59:00 -0700 | [diff] [blame] | 495 | msleep(pci_pm_d3_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | else if (state == PCI_D2 || dev->current_state == PCI_D2) |
| 497 | udelay(200); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
David Shaohua Li | b913100 | 2005-03-19 00:16:18 -0500 | [diff] [blame] | 499 | dev->current_state = state; |
John W. Linville | 064b53db | 2005-07-27 10:19:44 -0400 | [diff] [blame] | 500 | |
| 501 | /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT |
| 502 | * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning |
| 503 | * from D3hot to D0 _may_ perform an internal reset, thereby |
| 504 | * going to "D0 Uninitialized" rather than "D0 Initialized". |
| 505 | * For example, at least some versions of the 3c905B and the |
| 506 | * 3c556B exhibit this behaviour. |
| 507 | * |
| 508 | * At least some laptop BIOSen (e.g. the Thinkpad T21) leave |
| 509 | * devices in a D3hot state at boot. Consequently, we need to |
| 510 | * restore at least the BARs so that the device will be |
| 511 | * accessible to its driver. |
| 512 | */ |
| 513 | if (need_restore) |
| 514 | pci_restore_bars(dev); |
| 515 | |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 516 | if (dev->bus->self) |
| 517 | pcie_aspm_pm_state_change(dev->bus->self); |
| 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | /** |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 523 | * pci_update_current_state - Read PCI power state of given device from its |
| 524 | * PCI PM registers and cache it |
| 525 | * @dev: PCI device to handle. |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 526 | */ |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 527 | static void pci_update_current_state(struct pci_dev *dev) |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 528 | { |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 529 | if (dev->pm_cap) { |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 530 | u16 pmcsr; |
| 531 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 532 | pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 533 | dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * pci_set_power_state - Set the power state of a PCI device |
| 539 | * @dev: PCI device to handle. |
| 540 | * @state: PCI power state (D0, D1, D2, D3hot) to put the device into. |
| 541 | * |
| 542 | * Transition a device to a new power state, using the platform formware and/or |
| 543 | * the device's PCI PM registers. |
| 544 | * |
| 545 | * RETURN VALUE: |
| 546 | * -EINVAL if the requested state is invalid. |
| 547 | * -EIO if device does not support PCI PM or its PM capabilities register has a |
| 548 | * wrong version, or device doesn't support the requested state. |
| 549 | * 0 if device already is in the requested state. |
| 550 | * 0 if device's power state has been successfully changed. |
| 551 | */ |
| 552 | int pci_set_power_state(struct pci_dev *dev, pci_power_t state) |
| 553 | { |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 554 | int error; |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 555 | |
| 556 | /* bound the state we're entering */ |
| 557 | if (state > PCI_D3hot) |
| 558 | state = PCI_D3hot; |
| 559 | else if (state < PCI_D0) |
| 560 | state = PCI_D0; |
| 561 | else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev)) |
| 562 | /* |
| 563 | * If the device or the parent bridge do not support PCI PM, |
| 564 | * ignore the request if we're doing anything other than putting |
| 565 | * it into D0 (which would only happen on boot). |
| 566 | */ |
| 567 | return 0; |
| 568 | |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 569 | if (state == PCI_D0 && platform_pci_power_manageable(dev)) { |
| 570 | /* |
| 571 | * Allow the platform to change the state, for example via ACPI |
| 572 | * _PR0, _PS0 and some such, but do not trust it. |
| 573 | */ |
| 574 | int ret = platform_pci_set_power_state(dev, PCI_D0); |
| 575 | if (!ret) |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 576 | pci_update_current_state(dev); |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 577 | } |
Alan Cox | 979b179 | 2008-07-24 17:18:38 +0100 | [diff] [blame] | 578 | /* This device is quirked not to be put into D3, so |
| 579 | don't put it in D3 */ |
| 580 | if (state == PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3)) |
| 581 | return 0; |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 582 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 583 | error = pci_raw_set_power_state(dev, state); |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 584 | |
| 585 | if (state > PCI_D0 && platform_pci_power_manageable(dev)) { |
| 586 | /* Allow the platform to finalize the transition */ |
| 587 | int ret = platform_pci_set_power_state(dev, state); |
| 588 | if (!ret) { |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 589 | pci_update_current_state(dev); |
Rafael J. Wysocki | 44e4e66 | 2008-07-07 03:32:52 +0200 | [diff] [blame] | 590 | error = 0; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | return error; |
| 595 | } |
| 596 | |
| 597 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | * pci_choose_state - Choose the power state of a PCI device |
| 599 | * @dev: PCI device to be suspended |
| 600 | * @state: target sleep state for the whole system. This is the value |
| 601 | * that is passed to suspend() function. |
| 602 | * |
| 603 | * Returns PCI power state suitable for given device and given system |
| 604 | * message. |
| 605 | */ |
| 606 | |
| 607 | pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state) |
| 608 | { |
Shaohua Li | ab826ca | 2007-07-20 10:03:22 +0800 | [diff] [blame] | 609 | pci_power_t ret; |
David Shaohua Li | 0f64474 | 2005-03-19 00:15:48 -0500 | [diff] [blame] | 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | if (!pci_find_capability(dev, PCI_CAP_ID_PM)) |
| 612 | return PCI_D0; |
| 613 | |
Rafael J. Wysocki | 961d912 | 2008-07-07 03:32:02 +0200 | [diff] [blame] | 614 | ret = platform_pci_choose_state(dev); |
| 615 | if (ret != PCI_POWER_ERROR) |
| 616 | return ret; |
Pavel Machek | ca078ba | 2005-09-03 15:56:57 -0700 | [diff] [blame] | 617 | |
| 618 | switch (state.event) { |
| 619 | case PM_EVENT_ON: |
| 620 | return PCI_D0; |
| 621 | case PM_EVENT_FREEZE: |
David Brownell | b887d2e | 2006-08-14 23:11:05 -0700 | [diff] [blame] | 622 | case PM_EVENT_PRETHAW: |
| 623 | /* REVISIT both freeze and pre-thaw "should" use D0 */ |
Pavel Machek | ca078ba | 2005-09-03 15:56:57 -0700 | [diff] [blame] | 624 | case PM_EVENT_SUSPEND: |
Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 625 | case PM_EVENT_HIBERNATE: |
Pavel Machek | ca078ba | 2005-09-03 15:56:57 -0700 | [diff] [blame] | 626 | return PCI_D3hot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | default: |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 628 | dev_info(&dev->dev, "unrecognized suspend event %d\n", |
| 629 | state.event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | BUG(); |
| 631 | } |
| 632 | return PCI_D0; |
| 633 | } |
| 634 | |
| 635 | EXPORT_SYMBOL(pci_choose_state); |
| 636 | |
Michael S. Tsirkin | b56a5a2 | 2006-08-21 16:22:22 +0300 | [diff] [blame] | 637 | static int pci_save_pcie_state(struct pci_dev *dev) |
| 638 | { |
| 639 | int pos, i = 0; |
| 640 | struct pci_cap_saved_state *save_state; |
| 641 | u16 *cap; |
Shaohua Li | 017fc48 | 2007-12-18 09:57:09 +0800 | [diff] [blame] | 642 | int found = 0; |
Michael S. Tsirkin | b56a5a2 | 2006-08-21 16:22:22 +0300 | [diff] [blame] | 643 | |
| 644 | pos = pci_find_capability(dev, PCI_CAP_ID_EXP); |
| 645 | if (pos <= 0) |
| 646 | return 0; |
| 647 | |
Eric W. Biederman | 9f35575 | 2007-03-08 13:06:13 -0700 | [diff] [blame] | 648 | save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP); |
| 649 | if (!save_state) |
| 650 | save_state = kzalloc(sizeof(*save_state) + sizeof(u16) * 4, GFP_KERNEL); |
Shaohua Li | 017fc48 | 2007-12-18 09:57:09 +0800 | [diff] [blame] | 651 | else |
| 652 | found = 1; |
Michael S. Tsirkin | b56a5a2 | 2006-08-21 16:22:22 +0300 | [diff] [blame] | 653 | if (!save_state) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 654 | dev_err(&dev->dev, "out of memory in pci_save_pcie_state\n"); |
Michael S. Tsirkin | b56a5a2 | 2006-08-21 16:22:22 +0300 | [diff] [blame] | 655 | return -ENOMEM; |
| 656 | } |
| 657 | cap = (u16 *)&save_state->data[0]; |
| 658 | |
| 659 | pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &cap[i++]); |
| 660 | pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]); |
| 661 | pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]); |
| 662 | pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]); |
Shaohua Li | ec0a3a2 | 2007-12-18 09:56:56 +0800 | [diff] [blame] | 663 | save_state->cap_nr = PCI_CAP_ID_EXP; |
Shaohua Li | 017fc48 | 2007-12-18 09:57:09 +0800 | [diff] [blame] | 664 | if (!found) |
| 665 | pci_add_saved_cap(dev, save_state); |
Michael S. Tsirkin | b56a5a2 | 2006-08-21 16:22:22 +0300 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static void pci_restore_pcie_state(struct pci_dev *dev) |
| 670 | { |
| 671 | int i = 0, pos; |
| 672 | struct pci_cap_saved_state *save_state; |
| 673 | u16 *cap; |
| 674 | |
| 675 | save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP); |
| 676 | pos = pci_find_capability(dev, PCI_CAP_ID_EXP); |
| 677 | if (!save_state || pos <= 0) |
| 678 | return; |
| 679 | cap = (u16 *)&save_state->data[0]; |
| 680 | |
| 681 | pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, cap[i++]); |
| 682 | pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, cap[i++]); |
| 683 | pci_write_config_word(dev, pos + PCI_EXP_SLTCTL, cap[i++]); |
| 684 | pci_write_config_word(dev, pos + PCI_EXP_RTCTL, cap[i++]); |
Michael S. Tsirkin | b56a5a2 | 2006-08-21 16:22:22 +0300 | [diff] [blame] | 685 | } |
| 686 | |
Stephen Hemminger | cc692a5 | 2006-11-08 16:17:15 -0800 | [diff] [blame] | 687 | |
| 688 | static int pci_save_pcix_state(struct pci_dev *dev) |
| 689 | { |
| 690 | int pos, i = 0; |
| 691 | struct pci_cap_saved_state *save_state; |
| 692 | u16 *cap; |
Shaohua Li | 017fc48 | 2007-12-18 09:57:09 +0800 | [diff] [blame] | 693 | int found = 0; |
Stephen Hemminger | cc692a5 | 2006-11-08 16:17:15 -0800 | [diff] [blame] | 694 | |
| 695 | pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); |
| 696 | if (pos <= 0) |
| 697 | return 0; |
| 698 | |
Shaohua Li | f34303d | 2007-12-18 09:56:47 +0800 | [diff] [blame] | 699 | save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX); |
Eric W. Biederman | 9f35575 | 2007-03-08 13:06:13 -0700 | [diff] [blame] | 700 | if (!save_state) |
| 701 | save_state = kzalloc(sizeof(*save_state) + sizeof(u16), GFP_KERNEL); |
Shaohua Li | 017fc48 | 2007-12-18 09:57:09 +0800 | [diff] [blame] | 702 | else |
| 703 | found = 1; |
Stephen Hemminger | cc692a5 | 2006-11-08 16:17:15 -0800 | [diff] [blame] | 704 | if (!save_state) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 705 | dev_err(&dev->dev, "out of memory in pci_save_pcie_state\n"); |
Stephen Hemminger | cc692a5 | 2006-11-08 16:17:15 -0800 | [diff] [blame] | 706 | return -ENOMEM; |
| 707 | } |
| 708 | cap = (u16 *)&save_state->data[0]; |
| 709 | |
| 710 | pci_read_config_word(dev, pos + PCI_X_CMD, &cap[i++]); |
Shaohua Li | ec0a3a2 | 2007-12-18 09:56:56 +0800 | [diff] [blame] | 711 | save_state->cap_nr = PCI_CAP_ID_PCIX; |
Shaohua Li | 017fc48 | 2007-12-18 09:57:09 +0800 | [diff] [blame] | 712 | if (!found) |
| 713 | pci_add_saved_cap(dev, save_state); |
Stephen Hemminger | cc692a5 | 2006-11-08 16:17:15 -0800 | [diff] [blame] | 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | static void pci_restore_pcix_state(struct pci_dev *dev) |
| 718 | { |
| 719 | int i = 0, pos; |
| 720 | struct pci_cap_saved_state *save_state; |
| 721 | u16 *cap; |
| 722 | |
| 723 | save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX); |
| 724 | pos = pci_find_capability(dev, PCI_CAP_ID_PCIX); |
| 725 | if (!save_state || pos <= 0) |
| 726 | return; |
| 727 | cap = (u16 *)&save_state->data[0]; |
| 728 | |
| 729 | pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]); |
Stephen Hemminger | cc692a5 | 2006-11-08 16:17:15 -0800 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | /** |
| 734 | * pci_save_state - save the PCI configuration space of a device before suspending |
| 735 | * @dev: - PCI device that we're dealing with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | */ |
| 737 | int |
| 738 | pci_save_state(struct pci_dev *dev) |
| 739 | { |
| 740 | int i; |
| 741 | /* XXX: 100% dword access ok here? */ |
| 742 | for (i = 0; i < 16; i++) |
| 743 | pci_read_config_dword(dev, i * 4,&dev->saved_config_space[i]); |
Michael S. Tsirkin | b56a5a2 | 2006-08-21 16:22:22 +0300 | [diff] [blame] | 744 | if ((i = pci_save_pcie_state(dev)) != 0) |
| 745 | return i; |
Stephen Hemminger | cc692a5 | 2006-11-08 16:17:15 -0800 | [diff] [blame] | 746 | if ((i = pci_save_pcix_state(dev)) != 0) |
| 747 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * pci_restore_state - Restore the saved state of a PCI device |
| 753 | * @dev: - PCI device that we're dealing with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | */ |
| 755 | int |
| 756 | pci_restore_state(struct pci_dev *dev) |
| 757 | { |
| 758 | int i; |
Al Viro | b4482a4 | 2007-10-14 19:35:40 +0100 | [diff] [blame] | 759 | u32 val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
Michael S. Tsirkin | b56a5a2 | 2006-08-21 16:22:22 +0300 | [diff] [blame] | 761 | /* PCI Express register must be restored first */ |
| 762 | pci_restore_pcie_state(dev); |
| 763 | |
Yu, Luming | 8b8c8d2 | 2006-04-25 00:00:34 -0700 | [diff] [blame] | 764 | /* |
| 765 | * The Base Address register should be programmed before the command |
| 766 | * register(s) |
| 767 | */ |
| 768 | for (i = 15; i >= 0; i--) { |
Dave Jones | 04d9c1a | 2006-04-18 21:06:51 -0700 | [diff] [blame] | 769 | pci_read_config_dword(dev, i * 4, &val); |
| 770 | if (val != dev->saved_config_space[i]) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 771 | dev_printk(KERN_DEBUG, &dev->dev, "restoring config " |
| 772 | "space at offset %#x (was %#x, writing %#x)\n", |
| 773 | i, val, (int)dev->saved_config_space[i]); |
Dave Jones | 04d9c1a | 2006-04-18 21:06:51 -0700 | [diff] [blame] | 774 | pci_write_config_dword(dev,i * 4, |
| 775 | dev->saved_config_space[i]); |
| 776 | } |
| 777 | } |
Stephen Hemminger | cc692a5 | 2006-11-08 16:17:15 -0800 | [diff] [blame] | 778 | pci_restore_pcix_state(dev); |
Shaohua Li | 41017f0 | 2006-02-08 17:11:38 +0800 | [diff] [blame] | 779 | pci_restore_msi_state(dev); |
Michael Ellerman | 8fed4b6 | 2007-01-25 19:34:08 +1100 | [diff] [blame] | 780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
Hidetoshi Seto | 38cc130 | 2006-12-18 10:30:00 +0900 | [diff] [blame] | 784 | static int do_pci_enable_device(struct pci_dev *dev, int bars) |
| 785 | { |
| 786 | int err; |
| 787 | |
| 788 | err = pci_set_power_state(dev, PCI_D0); |
| 789 | if (err < 0 && err != -EIO) |
| 790 | return err; |
| 791 | err = pcibios_enable_device(dev, bars); |
| 792 | if (err < 0) |
| 793 | return err; |
| 794 | pci_fixup_device(pci_fixup_enable, dev); |
| 795 | |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | /** |
Tejun Heo | 0b62e13 | 2007-07-27 14:43:35 +0900 | [diff] [blame] | 800 | * pci_reenable_device - Resume abandoned device |
Hidetoshi Seto | 38cc130 | 2006-12-18 10:30:00 +0900 | [diff] [blame] | 801 | * @dev: PCI device to be resumed |
| 802 | * |
| 803 | * Note this function is a backend of pci_default_resume and is not supposed |
| 804 | * to be called by normal code, write proper resume handler and use it instead. |
| 805 | */ |
Tejun Heo | 0b62e13 | 2007-07-27 14:43:35 +0900 | [diff] [blame] | 806 | int pci_reenable_device(struct pci_dev *dev) |
Hidetoshi Seto | 38cc130 | 2006-12-18 10:30:00 +0900 | [diff] [blame] | 807 | { |
| 808 | if (atomic_read(&dev->enable_cnt)) |
| 809 | return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1); |
| 810 | return 0; |
| 811 | } |
| 812 | |
Benjamin Herrenschmidt | b718989 | 2007-12-20 15:28:08 +1100 | [diff] [blame] | 813 | static int __pci_enable_device_flags(struct pci_dev *dev, |
| 814 | resource_size_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
| 816 | int err; |
Benjamin Herrenschmidt | b718989 | 2007-12-20 15:28:08 +1100 | [diff] [blame] | 817 | int i, bars = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
Hidetoshi Seto | 9fb625c | 2006-12-18 10:28:43 +0900 | [diff] [blame] | 819 | if (atomic_add_return(1, &dev->enable_cnt) > 1) |
| 820 | return 0; /* already enabled */ |
| 821 | |
Benjamin Herrenschmidt | b718989 | 2007-12-20 15:28:08 +1100 | [diff] [blame] | 822 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) |
| 823 | if (dev->resource[i].flags & flags) |
| 824 | bars |= (1 << i); |
| 825 | |
Hidetoshi Seto | 38cc130 | 2006-12-18 10:30:00 +0900 | [diff] [blame] | 826 | err = do_pci_enable_device(dev, bars); |
Greg Kroah-Hartman | 95a6296 | 2005-07-28 11:37:33 -0700 | [diff] [blame] | 827 | if (err < 0) |
Hidetoshi Seto | 38cc130 | 2006-12-18 10:30:00 +0900 | [diff] [blame] | 828 | atomic_dec(&dev->enable_cnt); |
Hidetoshi Seto | 9fb625c | 2006-12-18 10:28:43 +0900 | [diff] [blame] | 829 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | /** |
Benjamin Herrenschmidt | b718989 | 2007-12-20 15:28:08 +1100 | [diff] [blame] | 833 | * pci_enable_device_io - Initialize a device for use with IO space |
| 834 | * @dev: PCI device to be initialized |
| 835 | * |
| 836 | * Initialize device before it's used by a driver. Ask low-level code |
| 837 | * to enable I/O resources. Wake up the device if it was suspended. |
| 838 | * Beware, this function can fail. |
| 839 | */ |
| 840 | int pci_enable_device_io(struct pci_dev *dev) |
| 841 | { |
| 842 | return __pci_enable_device_flags(dev, IORESOURCE_IO); |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * pci_enable_device_mem - Initialize a device for use with Memory space |
| 847 | * @dev: PCI device to be initialized |
| 848 | * |
| 849 | * Initialize device before it's used by a driver. Ask low-level code |
| 850 | * to enable Memory resources. Wake up the device if it was suspended. |
| 851 | * Beware, this function can fail. |
| 852 | */ |
| 853 | int pci_enable_device_mem(struct pci_dev *dev) |
| 854 | { |
| 855 | return __pci_enable_device_flags(dev, IORESOURCE_MEM); |
| 856 | } |
| 857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | /** |
| 859 | * pci_enable_device - Initialize device before it's used by a driver. |
| 860 | * @dev: PCI device to be initialized |
| 861 | * |
| 862 | * Initialize device before it's used by a driver. Ask low-level code |
| 863 | * to enable I/O and memory. Wake up the device if it was suspended. |
| 864 | * Beware, this function can fail. |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 865 | * |
| 866 | * Note we don't actually enable the device many times if we call |
| 867 | * this function repeatedly (we just increment the count). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | */ |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 869 | int pci_enable_device(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | { |
Benjamin Herrenschmidt | b718989 | 2007-12-20 15:28:08 +1100 | [diff] [blame] | 871 | return __pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 874 | /* |
| 875 | * Managed PCI resources. This manages device on/off, intx/msi/msix |
| 876 | * on/off and BAR regions. pci_dev itself records msi/msix status, so |
| 877 | * there's no need to track it separately. pci_devres is initialized |
| 878 | * when a device is enabled using managed PCI device enable interface. |
| 879 | */ |
| 880 | struct pci_devres { |
Tejun Heo | 7f375f3 | 2007-02-25 04:36:01 -0800 | [diff] [blame] | 881 | unsigned int enabled:1; |
| 882 | unsigned int pinned:1; |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 883 | unsigned int orig_intx:1; |
| 884 | unsigned int restore_intx:1; |
| 885 | u32 region_mask; |
| 886 | }; |
| 887 | |
| 888 | static void pcim_release(struct device *gendev, void *res) |
| 889 | { |
| 890 | struct pci_dev *dev = container_of(gendev, struct pci_dev, dev); |
| 891 | struct pci_devres *this = res; |
| 892 | int i; |
| 893 | |
| 894 | if (dev->msi_enabled) |
| 895 | pci_disable_msi(dev); |
| 896 | if (dev->msix_enabled) |
| 897 | pci_disable_msix(dev); |
| 898 | |
| 899 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) |
| 900 | if (this->region_mask & (1 << i)) |
| 901 | pci_release_region(dev, i); |
| 902 | |
| 903 | if (this->restore_intx) |
| 904 | pci_intx(dev, this->orig_intx); |
| 905 | |
Tejun Heo | 7f375f3 | 2007-02-25 04:36:01 -0800 | [diff] [blame] | 906 | if (this->enabled && !this->pinned) |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 907 | pci_disable_device(dev); |
| 908 | } |
| 909 | |
| 910 | static struct pci_devres * get_pci_dr(struct pci_dev *pdev) |
| 911 | { |
| 912 | struct pci_devres *dr, *new_dr; |
| 913 | |
| 914 | dr = devres_find(&pdev->dev, pcim_release, NULL, NULL); |
| 915 | if (dr) |
| 916 | return dr; |
| 917 | |
| 918 | new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL); |
| 919 | if (!new_dr) |
| 920 | return NULL; |
| 921 | return devres_get(&pdev->dev, new_dr, NULL, NULL); |
| 922 | } |
| 923 | |
| 924 | static struct pci_devres * find_pci_dr(struct pci_dev *pdev) |
| 925 | { |
| 926 | if (pci_is_managed(pdev)) |
| 927 | return devres_find(&pdev->dev, pcim_release, NULL, NULL); |
| 928 | return NULL; |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * pcim_enable_device - Managed pci_enable_device() |
| 933 | * @pdev: PCI device to be initialized |
| 934 | * |
| 935 | * Managed pci_enable_device(). |
| 936 | */ |
| 937 | int pcim_enable_device(struct pci_dev *pdev) |
| 938 | { |
| 939 | struct pci_devres *dr; |
| 940 | int rc; |
| 941 | |
| 942 | dr = get_pci_dr(pdev); |
| 943 | if (unlikely(!dr)) |
| 944 | return -ENOMEM; |
Tejun Heo | b95d58e | 2008-01-30 18:20:04 +0900 | [diff] [blame] | 945 | if (dr->enabled) |
| 946 | return 0; |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 947 | |
| 948 | rc = pci_enable_device(pdev); |
| 949 | if (!rc) { |
| 950 | pdev->is_managed = 1; |
Tejun Heo | 7f375f3 | 2007-02-25 04:36:01 -0800 | [diff] [blame] | 951 | dr->enabled = 1; |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 952 | } |
| 953 | return rc; |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * pcim_pin_device - Pin managed PCI device |
| 958 | * @pdev: PCI device to pin |
| 959 | * |
| 960 | * Pin managed PCI device @pdev. Pinned device won't be disabled on |
| 961 | * driver detach. @pdev must have been enabled with |
| 962 | * pcim_enable_device(). |
| 963 | */ |
| 964 | void pcim_pin_device(struct pci_dev *pdev) |
| 965 | { |
| 966 | struct pci_devres *dr; |
| 967 | |
| 968 | dr = find_pci_dr(pdev); |
Tejun Heo | 7f375f3 | 2007-02-25 04:36:01 -0800 | [diff] [blame] | 969 | WARN_ON(!dr || !dr->enabled); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 970 | if (dr) |
Tejun Heo | 7f375f3 | 2007-02-25 04:36:01 -0800 | [diff] [blame] | 971 | dr->pinned = 1; |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 972 | } |
| 973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | /** |
| 975 | * pcibios_disable_device - disable arch specific PCI resources for device dev |
| 976 | * @dev: the PCI device to disable |
| 977 | * |
| 978 | * Disables architecture specific PCI resources for the device. This |
| 979 | * is the default implementation. Architecture implementations can |
| 980 | * override this. |
| 981 | */ |
| 982 | void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {} |
| 983 | |
| 984 | /** |
| 985 | * pci_disable_device - Disable PCI device after use |
| 986 | * @dev: PCI device to be disabled |
| 987 | * |
| 988 | * Signal to the system that the PCI device is not in use by the system |
| 989 | * anymore. This only involves disabling PCI bus-mastering, if active. |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 990 | * |
| 991 | * Note we don't actually disable the device until all callers of |
| 992 | * pci_device_enable() have called pci_device_disable(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | */ |
| 994 | void |
| 995 | pci_disable_device(struct pci_dev *dev) |
| 996 | { |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 997 | struct pci_devres *dr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | u16 pci_command; |
Shaohua Li | 99dc804 | 2006-05-26 10:58:27 +0800 | [diff] [blame] | 999 | |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 1000 | dr = find_pci_dr(dev); |
| 1001 | if (dr) |
Tejun Heo | 7f375f3 | 2007-02-25 04:36:01 -0800 | [diff] [blame] | 1002 | dr->enabled = 0; |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 1003 | |
Inaky Perez-Gonzalez | bae94d0 | 2006-11-22 12:40:31 -0800 | [diff] [blame] | 1004 | if (atomic_sub_return(1, &dev->enable_cnt) != 0) |
| 1005 | return; |
| 1006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | pci_read_config_word(dev, PCI_COMMAND, &pci_command); |
| 1008 | if (pci_command & PCI_COMMAND_MASTER) { |
| 1009 | pci_command &= ~PCI_COMMAND_MASTER; |
| 1010 | pci_write_config_word(dev, PCI_COMMAND, pci_command); |
| 1011 | } |
Kenji Kaneshige | ceb4374 | 2005-04-08 14:53:31 +0900 | [diff] [blame] | 1012 | dev->is_busmaster = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
| 1014 | pcibios_disable_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /** |
Brian King | f7bdd12 | 2007-04-06 16:39:36 -0500 | [diff] [blame] | 1018 | * pcibios_set_pcie_reset_state - set reset state for device dev |
| 1019 | * @dev: the PCI-E device reset |
| 1020 | * @state: Reset state to enter into |
| 1021 | * |
| 1022 | * |
| 1023 | * Sets the PCI-E reset state for the device. This is the default |
| 1024 | * implementation. Architecture implementations can override this. |
| 1025 | */ |
| 1026 | int __attribute__ ((weak)) pcibios_set_pcie_reset_state(struct pci_dev *dev, |
| 1027 | enum pcie_reset_state state) |
| 1028 | { |
| 1029 | return -EINVAL; |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * pci_set_pcie_reset_state - set reset state for device dev |
| 1034 | * @dev: the PCI-E device reset |
| 1035 | * @state: Reset state to enter into |
| 1036 | * |
| 1037 | * |
| 1038 | * Sets the PCI reset state for the device. |
| 1039 | */ |
| 1040 | int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state) |
| 1041 | { |
| 1042 | return pcibios_set_pcie_reset_state(dev, state); |
| 1043 | } |
| 1044 | |
| 1045 | /** |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1046 | * pci_pme_capable - check the capability of PCI device to generate PME# |
| 1047 | * @dev: PCI device to handle. |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1048 | * @state: PCI state from which device will issue PME#. |
| 1049 | */ |
Rafael J. Wysocki | e5899e1 | 2008-07-19 14:39:24 +0200 | [diff] [blame] | 1050 | bool pci_pme_capable(struct pci_dev *dev, pci_power_t state) |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1051 | { |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1052 | if (!dev->pm_cap) |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1053 | return false; |
| 1054 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1055 | return !!(dev->pme_support & (1 << state)); |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /** |
| 1059 | * pci_pme_active - enable or disable PCI device's PME# function |
| 1060 | * @dev: PCI device to handle. |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1061 | * @enable: 'true' to enable PME# generation; 'false' to disable it. |
| 1062 | * |
| 1063 | * The caller must verify that the device is capable of generating PME# before |
| 1064 | * calling this function with @enable equal to 'true'. |
| 1065 | */ |
Rafael J. Wysocki | 5a6c9b6 | 2008-08-08 00:14:24 +0200 | [diff] [blame] | 1066 | void pci_pme_active(struct pci_dev *dev, bool enable) |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1067 | { |
| 1068 | u16 pmcsr; |
| 1069 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1070 | if (!dev->pm_cap) |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1071 | return; |
| 1072 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1073 | pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1074 | /* Clear PME_Status by writing 1 to it and enable PME# */ |
| 1075 | pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE; |
| 1076 | if (!enable) |
| 1077 | pmcsr &= ~PCI_PM_CTRL_PME_ENABLE; |
| 1078 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1079 | pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr); |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1080 | |
| 1081 | dev_printk(KERN_INFO, &dev->dev, "PME# %s\n", |
| 1082 | enable ? "enabled" : "disabled"); |
| 1083 | } |
| 1084 | |
| 1085 | /** |
David Brownell | 075c177 | 2007-04-26 00:12:06 -0700 | [diff] [blame] | 1086 | * pci_enable_wake - enable PCI device as wakeup event source |
| 1087 | * @dev: PCI device affected |
| 1088 | * @state: PCI state from which device will issue wakeup events |
| 1089 | * @enable: True to enable event generation; false to disable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | * |
David Brownell | 075c177 | 2007-04-26 00:12:06 -0700 | [diff] [blame] | 1091 | * This enables the device as a wakeup event source, or disables it. |
| 1092 | * When such events involves platform-specific hooks, those hooks are |
| 1093 | * called automatically by this routine. |
| 1094 | * |
| 1095 | * Devices with legacy power management (no standard PCI PM capabilities) |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1096 | * always require such platform hooks. |
David Brownell | 075c177 | 2007-04-26 00:12:06 -0700 | [diff] [blame] | 1097 | * |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1098 | * RETURN VALUE: |
| 1099 | * 0 is returned on success |
| 1100 | * -EINVAL is returned if device is not supposed to wake up the system |
| 1101 | * Error code depending on the platform is returned if both the platform and |
| 1102 | * the native mechanism fail to enable the generation of wake-up events |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | */ |
| 1104 | int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable) |
| 1105 | { |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1106 | int error = 0; |
| 1107 | bool pme_done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1109 | if (!device_may_wakeup(&dev->dev)) |
| 1110 | return -EINVAL; |
| 1111 | |
| 1112 | /* |
| 1113 | * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don |
| 1114 | * Anderson we should be doing PME# wake enable followed by ACPI wake |
| 1115 | * enable. To disable wake-up we call the platform first, for symmetry. |
David Brownell | 075c177 | 2007-04-26 00:12:06 -0700 | [diff] [blame] | 1116 | */ |
| 1117 | |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1118 | if (!enable && platform_pci_can_wakeup(dev)) |
| 1119 | error = platform_pci_sleep_wake(dev, false); |
| 1120 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1121 | if (!enable || pci_pme_capable(dev, state)) { |
| 1122 | pci_pme_active(dev, enable); |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1123 | pme_done = true; |
| 1124 | } |
| 1125 | |
| 1126 | if (enable && platform_pci_can_wakeup(dev)) |
| 1127 | error = platform_pci_sleep_wake(dev, true); |
| 1128 | |
| 1129 | return pme_done ? 0 : error; |
| 1130 | } |
| 1131 | |
| 1132 | /** |
Rafael J. Wysocki | 0235c4f | 2008-08-18 21:38:00 +0200 | [diff] [blame] | 1133 | * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold |
| 1134 | * @dev: PCI device to prepare |
| 1135 | * @enable: True to enable wake-up event generation; false to disable |
| 1136 | * |
| 1137 | * Many drivers want the device to wake up the system from D3_hot or D3_cold |
| 1138 | * and this function allows them to set that up cleanly - pci_enable_wake() |
| 1139 | * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI |
| 1140 | * ordering constraints. |
| 1141 | * |
| 1142 | * This function only returns error code if the device is not capable of |
| 1143 | * generating PME# from both D3_hot and D3_cold, and the platform is unable to |
| 1144 | * enable wake-up power for it. |
| 1145 | */ |
| 1146 | int pci_wake_from_d3(struct pci_dev *dev, bool enable) |
| 1147 | { |
| 1148 | return pci_pme_capable(dev, PCI_D3cold) ? |
| 1149 | pci_enable_wake(dev, PCI_D3cold, enable) : |
| 1150 | pci_enable_wake(dev, PCI_D3hot, enable); |
| 1151 | } |
| 1152 | |
| 1153 | /** |
Jesse Barnes | 3713907 | 2008-07-28 11:49:26 -0700 | [diff] [blame] | 1154 | * pci_target_state - find an appropriate low power state for a given PCI dev |
| 1155 | * @dev: PCI device |
| 1156 | * |
| 1157 | * Use underlying platform code to find a supported low power state for @dev. |
| 1158 | * If the platform can't manage @dev, return the deepest state from which it |
| 1159 | * can generate wake events, based on any available PME info. |
Rafael J. Wysocki | 404cc2d | 2008-07-07 03:35:26 +0200 | [diff] [blame] | 1160 | */ |
Rafael J. Wysocki | e5899e1 | 2008-07-19 14:39:24 +0200 | [diff] [blame] | 1161 | pci_power_t pci_target_state(struct pci_dev *dev) |
Rafael J. Wysocki | 404cc2d | 2008-07-07 03:35:26 +0200 | [diff] [blame] | 1162 | { |
| 1163 | pci_power_t target_state = PCI_D3hot; |
Rafael J. Wysocki | 404cc2d | 2008-07-07 03:35:26 +0200 | [diff] [blame] | 1164 | |
| 1165 | if (platform_pci_power_manageable(dev)) { |
| 1166 | /* |
| 1167 | * Call the platform to choose the target state of the device |
| 1168 | * and enable wake-up from this state if supported. |
| 1169 | */ |
| 1170 | pci_power_t state = platform_pci_choose_state(dev); |
| 1171 | |
| 1172 | switch (state) { |
| 1173 | case PCI_POWER_ERROR: |
| 1174 | case PCI_UNKNOWN: |
| 1175 | break; |
| 1176 | case PCI_D1: |
| 1177 | case PCI_D2: |
| 1178 | if (pci_no_d1d2(dev)) |
| 1179 | break; |
| 1180 | default: |
| 1181 | target_state = state; |
Rafael J. Wysocki | 404cc2d | 2008-07-07 03:35:26 +0200 | [diff] [blame] | 1182 | } |
| 1183 | } else if (device_may_wakeup(&dev->dev)) { |
| 1184 | /* |
| 1185 | * Find the deepest state from which the device can generate |
| 1186 | * wake-up events, make it the target state and enable device |
| 1187 | * to generate PME#. |
| 1188 | */ |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1189 | if (!dev->pm_cap) |
Rafael J. Wysocki | e5899e1 | 2008-07-19 14:39:24 +0200 | [diff] [blame] | 1190 | return PCI_POWER_ERROR; |
Rafael J. Wysocki | 404cc2d | 2008-07-07 03:35:26 +0200 | [diff] [blame] | 1191 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1192 | if (dev->pme_support) { |
| 1193 | while (target_state |
| 1194 | && !(dev->pme_support & (1 << target_state))) |
| 1195 | target_state--; |
Rafael J. Wysocki | 404cc2d | 2008-07-07 03:35:26 +0200 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
Rafael J. Wysocki | e5899e1 | 2008-07-19 14:39:24 +0200 | [diff] [blame] | 1199 | return target_state; |
| 1200 | } |
| 1201 | |
| 1202 | /** |
| 1203 | * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state |
| 1204 | * @dev: Device to handle. |
| 1205 | * |
| 1206 | * Choose the power state appropriate for the device depending on whether |
| 1207 | * it can wake up the system and/or is power manageable by the platform |
| 1208 | * (PCI_D3hot is the default) and put the device into that state. |
| 1209 | */ |
| 1210 | int pci_prepare_to_sleep(struct pci_dev *dev) |
| 1211 | { |
| 1212 | pci_power_t target_state = pci_target_state(dev); |
| 1213 | int error; |
| 1214 | |
| 1215 | if (target_state == PCI_POWER_ERROR) |
| 1216 | return -EIO; |
| 1217 | |
Rafael J. Wysocki | c157dfa | 2008-07-13 22:45:06 +0200 | [diff] [blame] | 1218 | pci_enable_wake(dev, target_state, true); |
| 1219 | |
Rafael J. Wysocki | 404cc2d | 2008-07-07 03:35:26 +0200 | [diff] [blame] | 1220 | error = pci_set_power_state(dev, target_state); |
| 1221 | |
| 1222 | if (error) |
| 1223 | pci_enable_wake(dev, target_state, false); |
| 1224 | |
| 1225 | return error; |
| 1226 | } |
| 1227 | |
| 1228 | /** |
Randy Dunlap | 443bd1c | 2008-07-21 09:27:18 -0700 | [diff] [blame] | 1229 | * pci_back_from_sleep - turn PCI device on during system-wide transition into working state |
Rafael J. Wysocki | 404cc2d | 2008-07-07 03:35:26 +0200 | [diff] [blame] | 1230 | * @dev: Device to handle. |
| 1231 | * |
| 1232 | * Disable device's sytem wake-up capability and put it into D0. |
| 1233 | */ |
| 1234 | int pci_back_from_sleep(struct pci_dev *dev) |
| 1235 | { |
| 1236 | pci_enable_wake(dev, PCI_D0, false); |
| 1237 | return pci_set_power_state(dev, PCI_D0); |
| 1238 | } |
| 1239 | |
| 1240 | /** |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1241 | * pci_pm_init - Initialize PM functions of given PCI device |
| 1242 | * @dev: PCI device to handle. |
| 1243 | */ |
| 1244 | void pci_pm_init(struct pci_dev *dev) |
| 1245 | { |
| 1246 | int pm; |
| 1247 | u16 pmc; |
David Brownell | 075c177 | 2007-04-26 00:12:06 -0700 | [diff] [blame] | 1248 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1249 | dev->pm_cap = 0; |
| 1250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | /* find PCI PM capability in list */ |
| 1252 | pm = pci_find_capability(dev, PCI_CAP_ID_PM); |
David Brownell | 075c177 | 2007-04-26 00:12:06 -0700 | [diff] [blame] | 1253 | if (!pm) |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1254 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | /* Check device's ability to generate PME# */ |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1256 | pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1258 | if ((pmc & PCI_PM_CAP_VER_MASK) > 3) { |
| 1259 | dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n", |
| 1260 | pmc & PCI_PM_CAP_VER_MASK); |
| 1261 | return; |
David Brownell | 075c177 | 2007-04-26 00:12:06 -0700 | [diff] [blame] | 1262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1264 | dev->pm_cap = pm; |
| 1265 | |
| 1266 | dev->d1_support = false; |
| 1267 | dev->d2_support = false; |
| 1268 | if (!pci_no_d1d2(dev)) { |
Bjorn Helgaas | c9ed77e | 2008-08-22 09:37:02 -0600 | [diff] [blame] | 1269 | if (pmc & PCI_PM_CAP_D1) |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1270 | dev->d1_support = true; |
Bjorn Helgaas | c9ed77e | 2008-08-22 09:37:02 -0600 | [diff] [blame] | 1271 | if (pmc & PCI_PM_CAP_D2) |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1272 | dev->d2_support = true; |
Bjorn Helgaas | c9ed77e | 2008-08-22 09:37:02 -0600 | [diff] [blame] | 1273 | |
| 1274 | if (dev->d1_support || dev->d2_support) |
| 1275 | dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n", |
Jesse Barnes | ec84f12 | 2008-09-23 11:43:34 -0700 | [diff] [blame] | 1276 | dev->d1_support ? " D1" : "", |
| 1277 | dev->d2_support ? " D2" : ""); |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | pmc &= PCI_PM_CAP_PME_MASK; |
| 1281 | if (pmc) { |
Bjorn Helgaas | c9ed77e | 2008-08-22 09:37:02 -0600 | [diff] [blame] | 1282 | dev_info(&dev->dev, "PME# supported from%s%s%s%s%s\n", |
| 1283 | (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "", |
| 1284 | (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "", |
| 1285 | (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "", |
| 1286 | (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "", |
| 1287 | (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : ""); |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1288 | dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT; |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1289 | /* |
| 1290 | * Make device's PM flags reflect the wake-up capability, but |
| 1291 | * let the user space enable it to wake up the system as needed. |
| 1292 | */ |
| 1293 | device_set_wakeup_capable(&dev->dev, true); |
| 1294 | device_set_wakeup_enable(&dev->dev, false); |
| 1295 | /* Disable the PME# generation functionality */ |
Rafael J. Wysocki | 337001b | 2008-07-07 03:36:24 +0200 | [diff] [blame] | 1296 | pci_pme_active(dev, false); |
| 1297 | } else { |
| 1298 | dev->pme_support = 0; |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 1299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Yu Zhao | 58c3a72 | 2008-10-14 14:02:53 +0800 | [diff] [blame] | 1302 | /** |
| 1303 | * pci_enable_ari - enable ARI forwarding if hardware support it |
| 1304 | * @dev: the PCI device |
| 1305 | */ |
| 1306 | void pci_enable_ari(struct pci_dev *dev) |
| 1307 | { |
| 1308 | int pos; |
| 1309 | u32 cap; |
| 1310 | u16 ctrl; |
| 1311 | |
| 1312 | if (!dev->is_pcie) |
| 1313 | return; |
| 1314 | |
| 1315 | if (dev->pcie_type != PCI_EXP_TYPE_ROOT_PORT && |
| 1316 | dev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) |
| 1317 | return; |
| 1318 | |
| 1319 | pos = pci_find_capability(dev, PCI_CAP_ID_EXP); |
| 1320 | if (!pos) |
| 1321 | return; |
| 1322 | |
| 1323 | pci_read_config_dword(dev, pos + PCI_EXP_DEVCAP2, &cap); |
| 1324 | if (!(cap & PCI_EXP_DEVCAP2_ARI)) |
| 1325 | return; |
| 1326 | |
| 1327 | pci_read_config_word(dev, pos + PCI_EXP_DEVCTL2, &ctrl); |
| 1328 | ctrl |= PCI_EXP_DEVCTL2_ARI; |
| 1329 | pci_write_config_word(dev, pos + PCI_EXP_DEVCTL2, ctrl); |
| 1330 | |
| 1331 | dev->ari_enabled = 1; |
| 1332 | } |
| 1333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | int |
| 1335 | pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge) |
| 1336 | { |
| 1337 | u8 pin; |
| 1338 | |
Kristen Accardi | 514d207 | 2005-11-02 16:24:39 -0800 | [diff] [blame] | 1339 | pin = dev->pin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | if (!pin) |
| 1341 | return -1; |
| 1342 | pin--; |
| 1343 | while (dev->bus->self) { |
| 1344 | pin = (pin + PCI_SLOT(dev->devfn)) % 4; |
| 1345 | dev = dev->bus->self; |
| 1346 | } |
| 1347 | *bridge = dev; |
| 1348 | return pin; |
| 1349 | } |
| 1350 | |
| 1351 | /** |
| 1352 | * pci_release_region - Release a PCI bar |
| 1353 | * @pdev: PCI device whose resources were previously reserved by pci_request_region |
| 1354 | * @bar: BAR to release |
| 1355 | * |
| 1356 | * Releases the PCI I/O and memory resources previously reserved by a |
| 1357 | * successful call to pci_request_region. Call this function only |
| 1358 | * after all use of the PCI regions has ceased. |
| 1359 | */ |
| 1360 | void pci_release_region(struct pci_dev *pdev, int bar) |
| 1361 | { |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 1362 | struct pci_devres *dr; |
| 1363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | if (pci_resource_len(pdev, bar) == 0) |
| 1365 | return; |
| 1366 | if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) |
| 1367 | release_region(pci_resource_start(pdev, bar), |
| 1368 | pci_resource_len(pdev, bar)); |
| 1369 | else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) |
| 1370 | release_mem_region(pci_resource_start(pdev, bar), |
| 1371 | pci_resource_len(pdev, bar)); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 1372 | |
| 1373 | dr = find_pci_dr(pdev); |
| 1374 | if (dr) |
| 1375 | dr->region_mask &= ~(1 << bar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | /** |
| 1379 | * pci_request_region - Reserved PCI I/O and memory resource |
| 1380 | * @pdev: PCI device whose resources are to be reserved |
| 1381 | * @bar: BAR to be reserved |
| 1382 | * @res_name: Name to be associated with resource. |
| 1383 | * |
| 1384 | * Mark the PCI region associated with PCI device @pdev BR @bar as |
| 1385 | * being reserved by owner @res_name. Do not access any |
| 1386 | * address inside the PCI regions unless this call returns |
| 1387 | * successfully. |
| 1388 | * |
| 1389 | * Returns 0 on success, or %EBUSY on error. A warning |
| 1390 | * message is also printed on failure. |
| 1391 | */ |
Jeff Garzik | 3c990e9 | 2006-03-04 21:52:42 -0500 | [diff] [blame] | 1392 | int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | { |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 1394 | struct pci_devres *dr; |
| 1395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | if (pci_resource_len(pdev, bar) == 0) |
| 1397 | return 0; |
| 1398 | |
| 1399 | if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) { |
| 1400 | if (!request_region(pci_resource_start(pdev, bar), |
| 1401 | pci_resource_len(pdev, bar), res_name)) |
| 1402 | goto err_out; |
| 1403 | } |
| 1404 | else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) { |
| 1405 | if (!request_mem_region(pci_resource_start(pdev, bar), |
| 1406 | pci_resource_len(pdev, bar), res_name)) |
| 1407 | goto err_out; |
| 1408 | } |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 1409 | |
| 1410 | dr = find_pci_dr(pdev); |
| 1411 | if (dr) |
| 1412 | dr->region_mask |= 1 << bar; |
| 1413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | return 0; |
| 1415 | |
| 1416 | err_out: |
Benjamin Herrenschmidt | 096e6f6 | 2008-10-20 15:07:37 +1100 | [diff] [blame] | 1417 | dev_warn(&pdev->dev, "BAR %d: can't reserve %s region %pR\n", |
Jesse Barnes | e4ec7a0 | 2008-06-25 16:12:25 -0700 | [diff] [blame] | 1418 | bar, |
| 1419 | pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem", |
Benjamin Herrenschmidt | 096e6f6 | 2008-10-20 15:07:37 +1100 | [diff] [blame] | 1420 | &pdev->resource[bar]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | return -EBUSY; |
| 1422 | } |
| 1423 | |
Hidetoshi Seto | c87deff | 2006-12-18 10:31:06 +0900 | [diff] [blame] | 1424 | /** |
| 1425 | * pci_release_selected_regions - Release selected PCI I/O and memory resources |
| 1426 | * @pdev: PCI device whose resources were previously reserved |
| 1427 | * @bars: Bitmask of BARs to be released |
| 1428 | * |
| 1429 | * Release selected PCI I/O and memory resources previously reserved. |
| 1430 | * Call this function only after all use of the PCI regions has ceased. |
| 1431 | */ |
| 1432 | void pci_release_selected_regions(struct pci_dev *pdev, int bars) |
| 1433 | { |
| 1434 | int i; |
| 1435 | |
| 1436 | for (i = 0; i < 6; i++) |
| 1437 | if (bars & (1 << i)) |
| 1438 | pci_release_region(pdev, i); |
| 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * pci_request_selected_regions - Reserve selected PCI I/O and memory resources |
| 1443 | * @pdev: PCI device whose resources are to be reserved |
| 1444 | * @bars: Bitmask of BARs to be requested |
| 1445 | * @res_name: Name to be associated with resource |
| 1446 | */ |
| 1447 | int pci_request_selected_regions(struct pci_dev *pdev, int bars, |
| 1448 | const char *res_name) |
| 1449 | { |
| 1450 | int i; |
| 1451 | |
| 1452 | for (i = 0; i < 6; i++) |
| 1453 | if (bars & (1 << i)) |
| 1454 | if(pci_request_region(pdev, i, res_name)) |
| 1455 | goto err_out; |
| 1456 | return 0; |
| 1457 | |
| 1458 | err_out: |
| 1459 | while(--i >= 0) |
| 1460 | if (bars & (1 << i)) |
| 1461 | pci_release_region(pdev, i); |
| 1462 | |
| 1463 | return -EBUSY; |
| 1464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | |
| 1466 | /** |
| 1467 | * pci_release_regions - Release reserved PCI I/O and memory resources |
| 1468 | * @pdev: PCI device whose resources were previously reserved by pci_request_regions |
| 1469 | * |
| 1470 | * Releases all PCI I/O and memory resources previously reserved by a |
| 1471 | * successful call to pci_request_regions. Call this function only |
| 1472 | * after all use of the PCI regions has ceased. |
| 1473 | */ |
| 1474 | |
| 1475 | void pci_release_regions(struct pci_dev *pdev) |
| 1476 | { |
Hidetoshi Seto | c87deff | 2006-12-18 10:31:06 +0900 | [diff] [blame] | 1477 | pci_release_selected_regions(pdev, (1 << 6) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | /** |
| 1481 | * pci_request_regions - Reserved PCI I/O and memory resources |
| 1482 | * @pdev: PCI device whose resources are to be reserved |
| 1483 | * @res_name: Name to be associated with resource. |
| 1484 | * |
| 1485 | * Mark all PCI regions associated with PCI device @pdev as |
| 1486 | * being reserved by owner @res_name. Do not access any |
| 1487 | * address inside the PCI regions unless this call returns |
| 1488 | * successfully. |
| 1489 | * |
| 1490 | * Returns 0 on success, or %EBUSY on error. A warning |
| 1491 | * message is also printed on failure. |
| 1492 | */ |
Jeff Garzik | 3c990e9 | 2006-03-04 21:52:42 -0500 | [diff] [blame] | 1493 | int pci_request_regions(struct pci_dev *pdev, const char *res_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | { |
Hidetoshi Seto | c87deff | 2006-12-18 10:31:06 +0900 | [diff] [blame] | 1495 | return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | /** |
| 1499 | * pci_set_master - enables bus-mastering for device dev |
| 1500 | * @dev: the PCI device to enable |
| 1501 | * |
| 1502 | * Enables bus-mastering on the device and calls pcibios_set_master() |
| 1503 | * to do the needed arch specific settings. |
| 1504 | */ |
| 1505 | void |
| 1506 | pci_set_master(struct pci_dev *dev) |
| 1507 | { |
| 1508 | u16 cmd; |
| 1509 | |
| 1510 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 1511 | if (! (cmd & PCI_COMMAND_MASTER)) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 1512 | dev_dbg(&dev->dev, "enabling bus mastering\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | cmd |= PCI_COMMAND_MASTER; |
| 1514 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 1515 | } |
| 1516 | dev->is_busmaster = 1; |
| 1517 | pcibios_set_master(dev); |
| 1518 | } |
| 1519 | |
Matthew Wilcox | edb2d97 | 2006-10-10 08:01:21 -0600 | [diff] [blame] | 1520 | #ifdef PCI_DISABLE_MWI |
| 1521 | int pci_set_mwi(struct pci_dev *dev) |
| 1522 | { |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
Randy Dunlap | 694625c | 2007-07-09 11:55:54 -0700 | [diff] [blame] | 1526 | int pci_try_set_mwi(struct pci_dev *dev) |
| 1527 | { |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
Matthew Wilcox | edb2d97 | 2006-10-10 08:01:21 -0600 | [diff] [blame] | 1531 | void pci_clear_mwi(struct pci_dev *dev) |
| 1532 | { |
| 1533 | } |
| 1534 | |
| 1535 | #else |
Matthew Wilcox | ebf5a24 | 2006-10-10 08:01:20 -0600 | [diff] [blame] | 1536 | |
| 1537 | #ifndef PCI_CACHE_LINE_BYTES |
| 1538 | #define PCI_CACHE_LINE_BYTES L1_CACHE_BYTES |
| 1539 | #endif |
| 1540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | /* This can be overridden by arch code. */ |
Matthew Wilcox | ebf5a24 | 2006-10-10 08:01:20 -0600 | [diff] [blame] | 1542 | /* Don't forget this is measured in 32-bit words, not bytes */ |
| 1543 | u8 pci_cache_line_size = PCI_CACHE_LINE_BYTES / 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
| 1545 | /** |
Matthew Wilcox | edb2d97 | 2006-10-10 08:01:21 -0600 | [diff] [blame] | 1546 | * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed |
| 1547 | * @dev: the PCI device for which MWI is to be enabled |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | * |
Matthew Wilcox | edb2d97 | 2006-10-10 08:01:21 -0600 | [diff] [blame] | 1549 | * Helper function for pci_set_mwi. |
| 1550 | * Originally copied from drivers/net/acenic.c. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>. |
| 1552 | * |
| 1553 | * RETURNS: An appropriate -ERRNO error value on error, or zero for success. |
| 1554 | */ |
| 1555 | static int |
Matthew Wilcox | edb2d97 | 2006-10-10 08:01:21 -0600 | [diff] [blame] | 1556 | pci_set_cacheline_size(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | { |
| 1558 | u8 cacheline_size; |
| 1559 | |
| 1560 | if (!pci_cache_line_size) |
| 1561 | return -EINVAL; /* The system doesn't support MWI. */ |
| 1562 | |
| 1563 | /* Validate current setting: the PCI_CACHE_LINE_SIZE must be |
| 1564 | equal to or multiple of the right value. */ |
| 1565 | pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size); |
| 1566 | if (cacheline_size >= pci_cache_line_size && |
| 1567 | (cacheline_size % pci_cache_line_size) == 0) |
| 1568 | return 0; |
| 1569 | |
| 1570 | /* Write the correct value. */ |
| 1571 | pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size); |
| 1572 | /* Read it back. */ |
| 1573 | pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size); |
| 1574 | if (cacheline_size == pci_cache_line_size) |
| 1575 | return 0; |
| 1576 | |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 1577 | dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not " |
| 1578 | "supported\n", pci_cache_line_size << 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | |
| 1580 | return -EINVAL; |
| 1581 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | |
| 1583 | /** |
| 1584 | * pci_set_mwi - enables memory-write-invalidate PCI transaction |
| 1585 | * @dev: the PCI device for which MWI is enabled |
| 1586 | * |
Randy Dunlap | 694625c | 2007-07-09 11:55:54 -0700 | [diff] [blame] | 1587 | * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | * |
| 1589 | * RETURNS: An appropriate -ERRNO error value on error, or zero for success. |
| 1590 | */ |
| 1591 | int |
| 1592 | pci_set_mwi(struct pci_dev *dev) |
| 1593 | { |
| 1594 | int rc; |
| 1595 | u16 cmd; |
| 1596 | |
Matthew Wilcox | edb2d97 | 2006-10-10 08:01:21 -0600 | [diff] [blame] | 1597 | rc = pci_set_cacheline_size(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | if (rc) |
| 1599 | return rc; |
| 1600 | |
| 1601 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 1602 | if (! (cmd & PCI_COMMAND_INVALIDATE)) { |
Bjorn Helgaas | 80ccba1 | 2008-06-13 10:52:11 -0600 | [diff] [blame] | 1603 | dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | cmd |= PCI_COMMAND_INVALIDATE; |
| 1605 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 1606 | } |
| 1607 | |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
| 1611 | /** |
Randy Dunlap | 694625c | 2007-07-09 11:55:54 -0700 | [diff] [blame] | 1612 | * pci_try_set_mwi - enables memory-write-invalidate PCI transaction |
| 1613 | * @dev: the PCI device for which MWI is enabled |
| 1614 | * |
| 1615 | * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND. |
| 1616 | * Callers are not required to check the return value. |
| 1617 | * |
| 1618 | * RETURNS: An appropriate -ERRNO error value on error, or zero for success. |
| 1619 | */ |
| 1620 | int pci_try_set_mwi(struct pci_dev *dev) |
| 1621 | { |
| 1622 | int rc = pci_set_mwi(dev); |
| 1623 | return rc; |
| 1624 | } |
| 1625 | |
| 1626 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | * pci_clear_mwi - disables Memory-Write-Invalidate for device dev |
| 1628 | * @dev: the PCI device to disable |
| 1629 | * |
| 1630 | * Disables PCI Memory-Write-Invalidate transaction on the device |
| 1631 | */ |
| 1632 | void |
| 1633 | pci_clear_mwi(struct pci_dev *dev) |
| 1634 | { |
| 1635 | u16 cmd; |
| 1636 | |
| 1637 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 1638 | if (cmd & PCI_COMMAND_INVALIDATE) { |
| 1639 | cmd &= ~PCI_COMMAND_INVALIDATE; |
| 1640 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 1641 | } |
| 1642 | } |
Matthew Wilcox | edb2d97 | 2006-10-10 08:01:21 -0600 | [diff] [blame] | 1643 | #endif /* ! PCI_DISABLE_MWI */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | |
Brett M Russ | a04ce0f | 2005-08-15 15:23:41 -0400 | [diff] [blame] | 1645 | /** |
| 1646 | * pci_intx - enables/disables PCI INTx for device dev |
Randy Dunlap | 8f7020d | 2005-10-23 11:57:38 -0700 | [diff] [blame] | 1647 | * @pdev: the PCI device to operate on |
| 1648 | * @enable: boolean: whether to enable or disable PCI INTx |
Brett M Russ | a04ce0f | 2005-08-15 15:23:41 -0400 | [diff] [blame] | 1649 | * |
| 1650 | * Enables/disables PCI INTx for device dev |
| 1651 | */ |
| 1652 | void |
| 1653 | pci_intx(struct pci_dev *pdev, int enable) |
| 1654 | { |
| 1655 | u16 pci_command, new; |
| 1656 | |
| 1657 | pci_read_config_word(pdev, PCI_COMMAND, &pci_command); |
| 1658 | |
| 1659 | if (enable) { |
| 1660 | new = pci_command & ~PCI_COMMAND_INTX_DISABLE; |
| 1661 | } else { |
| 1662 | new = pci_command | PCI_COMMAND_INTX_DISABLE; |
| 1663 | } |
| 1664 | |
| 1665 | if (new != pci_command) { |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 1666 | struct pci_devres *dr; |
| 1667 | |
Brett M Russ | 2fd9d74 | 2005-09-09 10:02:22 -0700 | [diff] [blame] | 1668 | pci_write_config_word(pdev, PCI_COMMAND, new); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 1669 | |
| 1670 | dr = find_pci_dr(pdev); |
| 1671 | if (dr && !dr->restore_intx) { |
| 1672 | dr->restore_intx = 1; |
| 1673 | dr->orig_intx = !enable; |
| 1674 | } |
Brett M Russ | a04ce0f | 2005-08-15 15:23:41 -0400 | [diff] [blame] | 1675 | } |
| 1676 | } |
| 1677 | |
Eric W. Biederman | f5f2b13 | 2007-03-05 00:30:07 -0800 | [diff] [blame] | 1678 | /** |
| 1679 | * pci_msi_off - disables any msi or msix capabilities |
Randy Dunlap | 8d7d86e | 2007-03-16 19:55:52 -0700 | [diff] [blame] | 1680 | * @dev: the PCI device to operate on |
Eric W. Biederman | f5f2b13 | 2007-03-05 00:30:07 -0800 | [diff] [blame] | 1681 | * |
| 1682 | * If you want to use msi see pci_enable_msi and friends. |
| 1683 | * This is a lower level primitive that allows us to disable |
| 1684 | * msi operation at the device level. |
| 1685 | */ |
| 1686 | void pci_msi_off(struct pci_dev *dev) |
| 1687 | { |
| 1688 | int pos; |
| 1689 | u16 control; |
| 1690 | |
| 1691 | pos = pci_find_capability(dev, PCI_CAP_ID_MSI); |
| 1692 | if (pos) { |
| 1693 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control); |
| 1694 | control &= ~PCI_MSI_FLAGS_ENABLE; |
| 1695 | pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control); |
| 1696 | } |
| 1697 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 1698 | if (pos) { |
| 1699 | pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control); |
| 1700 | control &= ~PCI_MSIX_FLAGS_ENABLE; |
| 1701 | pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control); |
| 1702 | } |
| 1703 | } |
| 1704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | #ifndef HAVE_ARCH_PCI_SET_DMA_MASK |
| 1706 | /* |
| 1707 | * These can be overridden by arch-specific implementations |
| 1708 | */ |
| 1709 | int |
| 1710 | pci_set_dma_mask(struct pci_dev *dev, u64 mask) |
| 1711 | { |
| 1712 | if (!pci_dma_supported(dev, mask)) |
| 1713 | return -EIO; |
| 1714 | |
| 1715 | dev->dma_mask = mask; |
| 1716 | |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
| 1720 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask) |
| 1722 | { |
| 1723 | if (!pci_dma_supported(dev, mask)) |
| 1724 | return -EIO; |
| 1725 | |
| 1726 | dev->dev.coherent_dma_mask = mask; |
| 1727 | |
| 1728 | return 0; |
| 1729 | } |
| 1730 | #endif |
Hidetoshi Seto | c87deff | 2006-12-18 10:31:06 +0900 | [diff] [blame] | 1731 | |
FUJITA Tomonori | 4d57cdf | 2008-02-04 22:27:55 -0800 | [diff] [blame] | 1732 | #ifndef HAVE_ARCH_PCI_SET_DMA_MAX_SEGMENT_SIZE |
| 1733 | int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size) |
| 1734 | { |
| 1735 | return dma_set_max_seg_size(&dev->dev, size); |
| 1736 | } |
| 1737 | EXPORT_SYMBOL(pci_set_dma_max_seg_size); |
| 1738 | #endif |
| 1739 | |
FUJITA Tomonori | 59fc67d | 2008-02-04 22:28:14 -0800 | [diff] [blame] | 1740 | #ifndef HAVE_ARCH_PCI_SET_DMA_SEGMENT_BOUNDARY |
| 1741 | int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask) |
| 1742 | { |
| 1743 | return dma_set_seg_boundary(&dev->dev, mask); |
| 1744 | } |
| 1745 | EXPORT_SYMBOL(pci_set_dma_seg_boundary); |
| 1746 | #endif |
| 1747 | |
Hidetoshi Seto | c87deff | 2006-12-18 10:31:06 +0900 | [diff] [blame] | 1748 | /** |
Peter Oruba | d556ad4 | 2007-05-15 13:59:13 +0200 | [diff] [blame] | 1749 | * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count |
| 1750 | * @dev: PCI device to query |
| 1751 | * |
| 1752 | * Returns mmrbc: maximum designed memory read count in bytes |
| 1753 | * or appropriate error value. |
| 1754 | */ |
| 1755 | int pcix_get_max_mmrbc(struct pci_dev *dev) |
| 1756 | { |
Andrew Morton | b7b095c | 2007-07-09 11:55:50 -0700 | [diff] [blame] | 1757 | int err, cap; |
Peter Oruba | d556ad4 | 2007-05-15 13:59:13 +0200 | [diff] [blame] | 1758 | u32 stat; |
| 1759 | |
| 1760 | cap = pci_find_capability(dev, PCI_CAP_ID_PCIX); |
| 1761 | if (!cap) |
| 1762 | return -EINVAL; |
| 1763 | |
| 1764 | err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat); |
| 1765 | if (err) |
| 1766 | return -EINVAL; |
| 1767 | |
Andrew Morton | b7b095c | 2007-07-09 11:55:50 -0700 | [diff] [blame] | 1768 | return (stat & PCI_X_STATUS_MAX_READ) >> 12; |
Peter Oruba | d556ad4 | 2007-05-15 13:59:13 +0200 | [diff] [blame] | 1769 | } |
| 1770 | EXPORT_SYMBOL(pcix_get_max_mmrbc); |
| 1771 | |
| 1772 | /** |
| 1773 | * pcix_get_mmrbc - get PCI-X maximum memory read byte count |
| 1774 | * @dev: PCI device to query |
| 1775 | * |
| 1776 | * Returns mmrbc: maximum memory read count in bytes |
| 1777 | * or appropriate error value. |
| 1778 | */ |
| 1779 | int pcix_get_mmrbc(struct pci_dev *dev) |
| 1780 | { |
| 1781 | int ret, cap; |
| 1782 | u32 cmd; |
| 1783 | |
| 1784 | cap = pci_find_capability(dev, PCI_CAP_ID_PCIX); |
| 1785 | if (!cap) |
| 1786 | return -EINVAL; |
| 1787 | |
| 1788 | ret = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd); |
| 1789 | if (!ret) |
| 1790 | ret = 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2); |
| 1791 | |
| 1792 | return ret; |
| 1793 | } |
| 1794 | EXPORT_SYMBOL(pcix_get_mmrbc); |
| 1795 | |
| 1796 | /** |
| 1797 | * pcix_set_mmrbc - set PCI-X maximum memory read byte count |
| 1798 | * @dev: PCI device to query |
| 1799 | * @mmrbc: maximum memory read count in bytes |
| 1800 | * valid values are 512, 1024, 2048, 4096 |
| 1801 | * |
| 1802 | * If possible sets maximum memory read byte count, some bridges have erratas |
| 1803 | * that prevent this. |
| 1804 | */ |
| 1805 | int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc) |
| 1806 | { |
| 1807 | int cap, err = -EINVAL; |
| 1808 | u32 stat, cmd, v, o; |
| 1809 | |
vignesh babu | 229f5af | 2007-08-13 18:23:14 +0530 | [diff] [blame] | 1810 | if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc)) |
Peter Oruba | d556ad4 | 2007-05-15 13:59:13 +0200 | [diff] [blame] | 1811 | goto out; |
| 1812 | |
| 1813 | v = ffs(mmrbc) - 10; |
| 1814 | |
| 1815 | cap = pci_find_capability(dev, PCI_CAP_ID_PCIX); |
| 1816 | if (!cap) |
| 1817 | goto out; |
| 1818 | |
| 1819 | err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat); |
| 1820 | if (err) |
| 1821 | goto out; |
| 1822 | |
| 1823 | if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21) |
| 1824 | return -E2BIG; |
| 1825 | |
| 1826 | err = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd); |
| 1827 | if (err) |
| 1828 | goto out; |
| 1829 | |
| 1830 | o = (cmd & PCI_X_CMD_MAX_READ) >> 2; |
| 1831 | if (o != v) { |
| 1832 | if (v > o && dev->bus && |
| 1833 | (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC)) |
| 1834 | return -EIO; |
| 1835 | |
| 1836 | cmd &= ~PCI_X_CMD_MAX_READ; |
| 1837 | cmd |= v << 2; |
| 1838 | err = pci_write_config_dword(dev, cap + PCI_X_CMD, cmd); |
| 1839 | } |
| 1840 | out: |
| 1841 | return err; |
| 1842 | } |
| 1843 | EXPORT_SYMBOL(pcix_set_mmrbc); |
| 1844 | |
| 1845 | /** |
| 1846 | * pcie_get_readrq - get PCI Express read request size |
| 1847 | * @dev: PCI device to query |
| 1848 | * |
| 1849 | * Returns maximum memory read request in bytes |
| 1850 | * or appropriate error value. |
| 1851 | */ |
| 1852 | int pcie_get_readrq(struct pci_dev *dev) |
| 1853 | { |
| 1854 | int ret, cap; |
| 1855 | u16 ctl; |
| 1856 | |
| 1857 | cap = pci_find_capability(dev, PCI_CAP_ID_EXP); |
| 1858 | if (!cap) |
| 1859 | return -EINVAL; |
| 1860 | |
| 1861 | ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl); |
| 1862 | if (!ret) |
| 1863 | ret = 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12); |
| 1864 | |
| 1865 | return ret; |
| 1866 | } |
| 1867 | EXPORT_SYMBOL(pcie_get_readrq); |
| 1868 | |
| 1869 | /** |
| 1870 | * pcie_set_readrq - set PCI Express maximum memory read request |
| 1871 | * @dev: PCI device to query |
Randy Dunlap | 42e61f4a | 2007-07-23 21:42:11 -0700 | [diff] [blame] | 1872 | * @rq: maximum memory read count in bytes |
Peter Oruba | d556ad4 | 2007-05-15 13:59:13 +0200 | [diff] [blame] | 1873 | * valid values are 128, 256, 512, 1024, 2048, 4096 |
| 1874 | * |
| 1875 | * If possible sets maximum read byte count |
| 1876 | */ |
| 1877 | int pcie_set_readrq(struct pci_dev *dev, int rq) |
| 1878 | { |
| 1879 | int cap, err = -EINVAL; |
| 1880 | u16 ctl, v; |
| 1881 | |
vignesh babu | 229f5af | 2007-08-13 18:23:14 +0530 | [diff] [blame] | 1882 | if (rq < 128 || rq > 4096 || !is_power_of_2(rq)) |
Peter Oruba | d556ad4 | 2007-05-15 13:59:13 +0200 | [diff] [blame] | 1883 | goto out; |
| 1884 | |
| 1885 | v = (ffs(rq) - 8) << 12; |
| 1886 | |
| 1887 | cap = pci_find_capability(dev, PCI_CAP_ID_EXP); |
| 1888 | if (!cap) |
| 1889 | goto out; |
| 1890 | |
| 1891 | err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl); |
| 1892 | if (err) |
| 1893 | goto out; |
| 1894 | |
| 1895 | if ((ctl & PCI_EXP_DEVCTL_READRQ) != v) { |
| 1896 | ctl &= ~PCI_EXP_DEVCTL_READRQ; |
| 1897 | ctl |= v; |
| 1898 | err = pci_write_config_dword(dev, cap + PCI_EXP_DEVCTL, ctl); |
| 1899 | } |
| 1900 | |
| 1901 | out: |
| 1902 | return err; |
| 1903 | } |
| 1904 | EXPORT_SYMBOL(pcie_set_readrq); |
| 1905 | |
| 1906 | /** |
Hidetoshi Seto | c87deff | 2006-12-18 10:31:06 +0900 | [diff] [blame] | 1907 | * pci_select_bars - Make BAR mask from the type of resource |
Randy Dunlap | f95d882 | 2007-02-10 14:41:56 -0800 | [diff] [blame] | 1908 | * @dev: the PCI device for which BAR mask is made |
Hidetoshi Seto | c87deff | 2006-12-18 10:31:06 +0900 | [diff] [blame] | 1909 | * @flags: resource type mask to be selected |
| 1910 | * |
| 1911 | * This helper routine makes bar mask from the type of resource. |
| 1912 | */ |
| 1913 | int pci_select_bars(struct pci_dev *dev, unsigned long flags) |
| 1914 | { |
| 1915 | int i, bars = 0; |
| 1916 | for (i = 0; i < PCI_NUM_RESOURCES; i++) |
| 1917 | if (pci_resource_flags(dev, i) & flags) |
| 1918 | bars |= (1 << i); |
| 1919 | return bars; |
| 1920 | } |
| 1921 | |
Jeff Garzik | 32a2eea | 2007-10-11 16:57:27 -0400 | [diff] [blame] | 1922 | static void __devinit pci_no_domains(void) |
| 1923 | { |
| 1924 | #ifdef CONFIG_PCI_DOMAINS |
| 1925 | pci_domains_supported = 0; |
| 1926 | #endif |
| 1927 | } |
| 1928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | static int __devinit pci_init(void) |
| 1930 | { |
| 1931 | struct pci_dev *dev = NULL; |
| 1932 | |
| 1933 | while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) { |
| 1934 | pci_fixup_device(pci_fixup_final, dev); |
| 1935 | } |
Taku Izumi | d389fec | 2008-10-17 13:52:51 +0900 | [diff] [blame^] | 1936 | |
| 1937 | msi_init(); |
| 1938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | return 0; |
| 1940 | } |
| 1941 | |
| 1942 | static int __devinit pci_setup(char *str) |
| 1943 | { |
| 1944 | while (str) { |
| 1945 | char *k = strchr(str, ','); |
| 1946 | if (k) |
| 1947 | *k++ = 0; |
| 1948 | if (*str && (str = pcibios_setup(str)) && *str) { |
Matthew Wilcox | 309e57d | 2006-03-05 22:33:34 -0700 | [diff] [blame] | 1949 | if (!strcmp(str, "nomsi")) { |
| 1950 | pci_no_msi(); |
Randy Dunlap | 7f78576 | 2007-10-05 13:17:58 -0700 | [diff] [blame] | 1951 | } else if (!strcmp(str, "noaer")) { |
| 1952 | pci_no_aer(); |
Jeff Garzik | 32a2eea | 2007-10-11 16:57:27 -0400 | [diff] [blame] | 1953 | } else if (!strcmp(str, "nodomains")) { |
| 1954 | pci_no_domains(); |
Atsushi Nemoto | 4516a61 | 2007-02-05 16:36:06 -0800 | [diff] [blame] | 1955 | } else if (!strncmp(str, "cbiosize=", 9)) { |
| 1956 | pci_cardbus_io_size = memparse(str + 9, &str); |
| 1957 | } else if (!strncmp(str, "cbmemsize=", 10)) { |
| 1958 | pci_cardbus_mem_size = memparse(str + 10, &str); |
Matthew Wilcox | 309e57d | 2006-03-05 22:33:34 -0700 | [diff] [blame] | 1959 | } else { |
| 1960 | printk(KERN_ERR "PCI: Unknown option `%s'\n", |
| 1961 | str); |
| 1962 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | } |
| 1964 | str = k; |
| 1965 | } |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 1966 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | } |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 1968 | early_param("pci", pci_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | |
| 1970 | device_initcall(pci_init); |
| 1971 | |
Tejun Heo | 0b62e13 | 2007-07-27 14:43:35 +0900 | [diff] [blame] | 1972 | EXPORT_SYMBOL(pci_reenable_device); |
Benjamin Herrenschmidt | b718989 | 2007-12-20 15:28:08 +1100 | [diff] [blame] | 1973 | EXPORT_SYMBOL(pci_enable_device_io); |
| 1974 | EXPORT_SYMBOL(pci_enable_device_mem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | EXPORT_SYMBOL(pci_enable_device); |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 1976 | EXPORT_SYMBOL(pcim_enable_device); |
| 1977 | EXPORT_SYMBOL(pcim_pin_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | EXPORT_SYMBOL(pci_disable_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | EXPORT_SYMBOL(pci_find_capability); |
| 1980 | EXPORT_SYMBOL(pci_bus_find_capability); |
| 1981 | EXPORT_SYMBOL(pci_release_regions); |
| 1982 | EXPORT_SYMBOL(pci_request_regions); |
| 1983 | EXPORT_SYMBOL(pci_release_region); |
| 1984 | EXPORT_SYMBOL(pci_request_region); |
Hidetoshi Seto | c87deff | 2006-12-18 10:31:06 +0900 | [diff] [blame] | 1985 | EXPORT_SYMBOL(pci_release_selected_regions); |
| 1986 | EXPORT_SYMBOL(pci_request_selected_regions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | EXPORT_SYMBOL(pci_set_master); |
| 1988 | EXPORT_SYMBOL(pci_set_mwi); |
Randy Dunlap | 694625c | 2007-07-09 11:55:54 -0700 | [diff] [blame] | 1989 | EXPORT_SYMBOL(pci_try_set_mwi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | EXPORT_SYMBOL(pci_clear_mwi); |
Brett M Russ | a04ce0f | 2005-08-15 15:23:41 -0400 | [diff] [blame] | 1991 | EXPORT_SYMBOL_GPL(pci_intx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | EXPORT_SYMBOL(pci_set_dma_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | EXPORT_SYMBOL(pci_set_consistent_dma_mask); |
| 1994 | EXPORT_SYMBOL(pci_assign_resource); |
| 1995 | EXPORT_SYMBOL(pci_find_parent_resource); |
Hidetoshi Seto | c87deff | 2006-12-18 10:31:06 +0900 | [diff] [blame] | 1996 | EXPORT_SYMBOL(pci_select_bars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | |
| 1998 | EXPORT_SYMBOL(pci_set_power_state); |
| 1999 | EXPORT_SYMBOL(pci_save_state); |
| 2000 | EXPORT_SYMBOL(pci_restore_state); |
Rafael J. Wysocki | e5899e1 | 2008-07-19 14:39:24 +0200 | [diff] [blame] | 2001 | EXPORT_SYMBOL(pci_pme_capable); |
Rafael J. Wysocki | 5a6c9b6 | 2008-08-08 00:14:24 +0200 | [diff] [blame] | 2002 | EXPORT_SYMBOL(pci_pme_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | EXPORT_SYMBOL(pci_enable_wake); |
Rafael J. Wysocki | 0235c4f | 2008-08-18 21:38:00 +0200 | [diff] [blame] | 2004 | EXPORT_SYMBOL(pci_wake_from_d3); |
Rafael J. Wysocki | e5899e1 | 2008-07-19 14:39:24 +0200 | [diff] [blame] | 2005 | EXPORT_SYMBOL(pci_target_state); |
Rafael J. Wysocki | 404cc2d | 2008-07-07 03:35:26 +0200 | [diff] [blame] | 2006 | EXPORT_SYMBOL(pci_prepare_to_sleep); |
| 2007 | EXPORT_SYMBOL(pci_back_from_sleep); |
Brian King | f7bdd12 | 2007-04-06 16:39:36 -0500 | [diff] [blame] | 2008 | EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | |