PCI PM: Follow PCI_PM_CTRL_NO_SOFT_RESET during transitions from D3
[linux-2.6/mini2440.git] / drivers / pci / pci.c
blobe07434362a5d4aeec7624b72e06c5e769a211afc
1 /*
2 * PCI Bus Services, see include/linux/pci.h for further explanation.
4 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
5 * David Mosberger-Tang
7 * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
8 */
10 #include <linux/kernel.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/pci.h>
14 #include <linux/pm.h>
15 #include <linux/module.h>
16 #include <linux/spinlock.h>
17 #include <linux/string.h>
18 #include <linux/log2.h>
19 #include <linux/pci-aspm.h>
20 #include <linux/pm_wakeup.h>
21 #include <linux/interrupt.h>
22 #include <asm/dma.h> /* isa_dma_bridge_buggy */
23 #include "pci.h"
25 unsigned int pci_pm_d3_delay = PCI_PM_D3_WAIT;
27 #ifdef CONFIG_PCI_DOMAINS
28 int pci_domains_supported = 1;
29 #endif
31 #define DEFAULT_CARDBUS_IO_SIZE (256)
32 #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
33 /* pci=cbmemsize=nnM,cbiosize=nn can override this */
34 unsigned long pci_cardbus_io_size = DEFAULT_CARDBUS_IO_SIZE;
35 unsigned long pci_cardbus_mem_size = DEFAULT_CARDBUS_MEM_SIZE;
37 /**
38 * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
39 * @bus: pointer to PCI bus structure to search
41 * Given a PCI bus, returns the highest PCI bus number present in the set
42 * including the given PCI bus and its list of child PCI buses.
44 unsigned char pci_bus_max_busnr(struct pci_bus* bus)
46 struct list_head *tmp;
47 unsigned char max, n;
49 max = bus->subordinate;
50 list_for_each(tmp, &bus->children) {
51 n = pci_bus_max_busnr(pci_bus_b(tmp));
52 if(n > max)
53 max = n;
55 return max;
57 EXPORT_SYMBOL_GPL(pci_bus_max_busnr);
59 #ifdef CONFIG_HAS_IOMEM
60 void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
63 * Make sure the BAR is actually a memory resource, not an IO resource
65 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
66 WARN_ON(1);
67 return NULL;
69 return ioremap_nocache(pci_resource_start(pdev, bar),
70 pci_resource_len(pdev, bar));
72 EXPORT_SYMBOL_GPL(pci_ioremap_bar);
73 #endif
75 #if 0
76 /**
77 * pci_max_busnr - returns maximum PCI bus number
79 * Returns the highest PCI bus number present in the system global list of
80 * PCI buses.
82 unsigned char __devinit
83 pci_max_busnr(void)
85 struct pci_bus *bus = NULL;
86 unsigned char max, n;
88 max = 0;
89 while ((bus = pci_find_next_bus(bus)) != NULL) {
90 n = pci_bus_max_busnr(bus);
91 if(n > max)
92 max = n;
94 return max;
97 #endif /* 0 */
99 #define PCI_FIND_CAP_TTL 48
101 static int __pci_find_next_cap_ttl(struct pci_bus *bus, unsigned int devfn,
102 u8 pos, int cap, int *ttl)
104 u8 id;
106 while ((*ttl)--) {
107 pci_bus_read_config_byte(bus, devfn, pos, &pos);
108 if (pos < 0x40)
109 break;
110 pos &= ~3;
111 pci_bus_read_config_byte(bus, devfn, pos + PCI_CAP_LIST_ID,
112 &id);
113 if (id == 0xff)
114 break;
115 if (id == cap)
116 return pos;
117 pos += PCI_CAP_LIST_NEXT;
119 return 0;
122 static int __pci_find_next_cap(struct pci_bus *bus, unsigned int devfn,
123 u8 pos, int cap)
125 int ttl = PCI_FIND_CAP_TTL;
127 return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
130 int pci_find_next_capability(struct pci_dev *dev, u8 pos, int cap)
132 return __pci_find_next_cap(dev->bus, dev->devfn,
133 pos + PCI_CAP_LIST_NEXT, cap);
135 EXPORT_SYMBOL_GPL(pci_find_next_capability);
137 static int __pci_bus_find_cap_start(struct pci_bus *bus,
138 unsigned int devfn, u8 hdr_type)
140 u16 status;
142 pci_bus_read_config_word(bus, devfn, PCI_STATUS, &status);
143 if (!(status & PCI_STATUS_CAP_LIST))
144 return 0;
146 switch (hdr_type) {
147 case PCI_HEADER_TYPE_NORMAL:
148 case PCI_HEADER_TYPE_BRIDGE:
149 return PCI_CAPABILITY_LIST;
150 case PCI_HEADER_TYPE_CARDBUS:
151 return PCI_CB_CAPABILITY_LIST;
152 default:
153 return 0;
156 return 0;
160 * pci_find_capability - query for devices' capabilities
161 * @dev: PCI device to query
162 * @cap: capability code
164 * Tell if a device supports a given PCI capability.
165 * Returns the address of the requested capability structure within the
166 * device's PCI configuration space or 0 in case the device does not
167 * support it. Possible values for @cap:
169 * %PCI_CAP_ID_PM Power Management
170 * %PCI_CAP_ID_AGP Accelerated Graphics Port
171 * %PCI_CAP_ID_VPD Vital Product Data
172 * %PCI_CAP_ID_SLOTID Slot Identification
173 * %PCI_CAP_ID_MSI Message Signalled Interrupts
174 * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
175 * %PCI_CAP_ID_PCIX PCI-X
176 * %PCI_CAP_ID_EXP PCI Express
178 int pci_find_capability(struct pci_dev *dev, int cap)
180 int pos;
182 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
183 if (pos)
184 pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
186 return pos;
190 * pci_bus_find_capability - query for devices' capabilities
191 * @bus: the PCI bus to query
192 * @devfn: PCI device to query
193 * @cap: capability code
195 * Like pci_find_capability() but works for pci devices that do not have a
196 * pci_dev structure set up yet.
198 * Returns the address of the requested capability structure within the
199 * device's PCI configuration space or 0 in case the device does not
200 * support it.
202 int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
204 int pos;
205 u8 hdr_type;
207 pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
209 pos = __pci_bus_find_cap_start(bus, devfn, hdr_type & 0x7f);
210 if (pos)
211 pos = __pci_find_next_cap(bus, devfn, pos, cap);
213 return pos;
217 * pci_find_ext_capability - Find an extended capability
218 * @dev: PCI device to query
219 * @cap: capability code
221 * Returns the address of the requested extended capability structure
222 * within the device's PCI configuration space or 0 if the device does
223 * not support it. Possible values for @cap:
225 * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
226 * %PCI_EXT_CAP_ID_VC Virtual Channel
227 * %PCI_EXT_CAP_ID_DSN Device Serial Number
228 * %PCI_EXT_CAP_ID_PWR Power Budgeting
230 int pci_find_ext_capability(struct pci_dev *dev, int cap)
232 u32 header;
233 int ttl;
234 int pos = PCI_CFG_SPACE_SIZE;
236 /* minimum 8 bytes per capability */
237 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
239 if (dev->cfg_size <= PCI_CFG_SPACE_SIZE)
240 return 0;
242 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
243 return 0;
246 * If we have no capabilities, this is indicated by cap ID,
247 * cap version and next pointer all being 0.
249 if (header == 0)
250 return 0;
252 while (ttl-- > 0) {
253 if (PCI_EXT_CAP_ID(header) == cap)
254 return pos;
256 pos = PCI_EXT_CAP_NEXT(header);
257 if (pos < PCI_CFG_SPACE_SIZE)
258 break;
260 if (pci_read_config_dword(dev, pos, &header) != PCIBIOS_SUCCESSFUL)
261 break;
264 return 0;
266 EXPORT_SYMBOL_GPL(pci_find_ext_capability);
268 static int __pci_find_next_ht_cap(struct pci_dev *dev, int pos, int ht_cap)
270 int rc, ttl = PCI_FIND_CAP_TTL;
271 u8 cap, mask;
273 if (ht_cap == HT_CAPTYPE_SLAVE || ht_cap == HT_CAPTYPE_HOST)
274 mask = HT_3BIT_CAP_MASK;
275 else
276 mask = HT_5BIT_CAP_MASK;
278 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn, pos,
279 PCI_CAP_ID_HT, &ttl);
280 while (pos) {
281 rc = pci_read_config_byte(dev, pos + 3, &cap);
282 if (rc != PCIBIOS_SUCCESSFUL)
283 return 0;
285 if ((cap & mask) == ht_cap)
286 return pos;
288 pos = __pci_find_next_cap_ttl(dev->bus, dev->devfn,
289 pos + PCI_CAP_LIST_NEXT,
290 PCI_CAP_ID_HT, &ttl);
293 return 0;
296 * pci_find_next_ht_capability - query a device's Hypertransport capabilities
297 * @dev: PCI device to query
298 * @pos: Position from which to continue searching
299 * @ht_cap: Hypertransport capability code
301 * To be used in conjunction with pci_find_ht_capability() to search for
302 * all capabilities matching @ht_cap. @pos should always be a value returned
303 * from pci_find_ht_capability().
305 * NB. To be 100% safe against broken PCI devices, the caller should take
306 * steps to avoid an infinite loop.
308 int pci_find_next_ht_capability(struct pci_dev *dev, int pos, int ht_cap)
310 return __pci_find_next_ht_cap(dev, pos + PCI_CAP_LIST_NEXT, ht_cap);
312 EXPORT_SYMBOL_GPL(pci_find_next_ht_capability);
315 * pci_find_ht_capability - query a device's Hypertransport capabilities
316 * @dev: PCI device to query
317 * @ht_cap: Hypertransport capability code
319 * Tell if a device supports a given Hypertransport capability.
320 * Returns an address within the device's PCI configuration space
321 * or 0 in case the device does not support the request capability.
322 * The address points to the PCI capability, of type PCI_CAP_ID_HT,
323 * which has a Hypertransport capability matching @ht_cap.
325 int pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
327 int pos;
329 pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
330 if (pos)
331 pos = __pci_find_next_ht_cap(dev, pos, ht_cap);
333 return pos;
335 EXPORT_SYMBOL_GPL(pci_find_ht_capability);
338 * pci_find_parent_resource - return resource region of parent bus of given region
339 * @dev: PCI device structure contains resources to be searched
340 * @res: child resource record for which parent is sought
342 * For given resource region of given device, return the resource
343 * region of parent bus the given region is contained in or where
344 * it should be allocated from.
346 struct resource *
347 pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
349 const struct pci_bus *bus = dev->bus;
350 int i;
351 struct resource *best = NULL;
353 for(i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
354 struct resource *r = bus->resource[i];
355 if (!r)
356 continue;
357 if (res->start && !(res->start >= r->start && res->end <= r->end))
358 continue; /* Not contained */
359 if ((res->flags ^ r->flags) & (IORESOURCE_IO | IORESOURCE_MEM))
360 continue; /* Wrong type */
361 if (!((res->flags ^ r->flags) & IORESOURCE_PREFETCH))
362 return r; /* Exact match */
363 if ((res->flags & IORESOURCE_PREFETCH) && !(r->flags & IORESOURCE_PREFETCH))
364 best = r; /* Approximating prefetchable by non-prefetchable */
366 return best;
370 * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
371 * @dev: PCI device to have its BARs restored
373 * Restore the BAR values for a given device, so as to make it
374 * accessible by its driver.
376 static void
377 pci_restore_bars(struct pci_dev *dev)
379 int i;
381 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++)
382 pci_update_resource(dev, i);
385 static struct pci_platform_pm_ops *pci_platform_pm;
387 int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
389 if (!ops->is_manageable || !ops->set_state || !ops->choose_state
390 || !ops->sleep_wake || !ops->can_wakeup)
391 return -EINVAL;
392 pci_platform_pm = ops;
393 return 0;
396 static inline bool platform_pci_power_manageable(struct pci_dev *dev)
398 return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
401 static inline int platform_pci_set_power_state(struct pci_dev *dev,
402 pci_power_t t)
404 return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
407 static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
409 return pci_platform_pm ?
410 pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
413 static inline bool platform_pci_can_wakeup(struct pci_dev *dev)
415 return pci_platform_pm ? pci_platform_pm->can_wakeup(dev) : false;
418 static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
420 return pci_platform_pm ?
421 pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
425 * pci_raw_set_power_state - Use PCI PM registers to set the power state of
426 * given PCI device
427 * @dev: PCI device to handle.
428 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
429 * @wait: If 'true', wait for the device to change its power state
431 * RETURN VALUE:
432 * -EINVAL if the requested state is invalid.
433 * -EIO if device does not support PCI PM or its PM capabilities register has a
434 * wrong version, or device doesn't support the requested state.
435 * 0 if device already is in the requested state.
436 * 0 if device's power state has been successfully changed.
438 static int
439 pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state, bool wait)
441 u16 pmcsr;
442 bool need_restore = false;
444 if (!dev->pm_cap)
445 return -EIO;
447 if (state < PCI_D0 || state > PCI_D3hot)
448 return -EINVAL;
450 /* Validate current state:
451 * Can enter D0 from any state, but if we can only go deeper
452 * to sleep if we're already in a low power state
454 if (dev->current_state == state) {
455 /* we're already there */
456 return 0;
457 } else if (state != PCI_D0 && dev->current_state <= PCI_D3cold
458 && dev->current_state > state) {
459 dev_err(&dev->dev, "invalid power transition "
460 "(from state %d to %d)\n", dev->current_state, state);
461 return -EINVAL;
464 /* check if this device supports the desired state */
465 if ((state == PCI_D1 && !dev->d1_support)
466 || (state == PCI_D2 && !dev->d2_support))
467 return -EIO;
469 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
471 /* If we're (effectively) in D3, force entire word to 0.
472 * This doesn't affect PME_Status, disables PME_En, and
473 * sets PowerState to 0.
475 switch (dev->current_state) {
476 case PCI_D0:
477 case PCI_D1:
478 case PCI_D2:
479 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
480 pmcsr |= state;
481 break;
482 case PCI_D3hot:
483 case PCI_D3cold:
484 case PCI_UNKNOWN: /* Boot-up */
485 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
486 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET)) {
487 need_restore = true;
488 wait = true;
490 /* Fall-through: force to D0 */
491 default:
492 pmcsr = 0;
493 break;
496 /* enter specified state */
497 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
499 if (!wait)
500 return 0;
502 /* Mandatory power management transition delays */
503 /* see PCI PM 1.1 5.6.1 table 18 */
504 if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
505 msleep(pci_pm_d3_delay);
506 else if (state == PCI_D2 || dev->current_state == PCI_D2)
507 udelay(PCI_PM_D2_DELAY);
509 dev->current_state = state;
511 /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
512 * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
513 * from D3hot to D0 _may_ perform an internal reset, thereby
514 * going to "D0 Uninitialized" rather than "D0 Initialized".
515 * For example, at least some versions of the 3c905B and the
516 * 3c556B exhibit this behaviour.
518 * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
519 * devices in a D3hot state at boot. Consequently, we need to
520 * restore at least the BARs so that the device will be
521 * accessible to its driver.
523 if (need_restore)
524 pci_restore_bars(dev);
526 if (wait && dev->bus->self)
527 pcie_aspm_pm_state_change(dev->bus->self);
529 return 0;
533 * pci_update_current_state - Read PCI power state of given device from its
534 * PCI PM registers and cache it
535 * @dev: PCI device to handle.
536 * @state: State to cache in case the device doesn't have the PM capability
538 void pci_update_current_state(struct pci_dev *dev, pci_power_t state)
540 if (dev->pm_cap) {
541 u16 pmcsr;
543 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
544 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
545 } else {
546 dev->current_state = state;
551 * pci_set_power_state - Set the power state of a PCI device
552 * @dev: PCI device to handle.
553 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
555 * Transition a device to a new power state, using the platform formware and/or
556 * the device's PCI PM registers.
558 * RETURN VALUE:
559 * -EINVAL if the requested state is invalid.
560 * -EIO if device does not support PCI PM or its PM capabilities register has a
561 * wrong version, or device doesn't support the requested state.
562 * 0 if device already is in the requested state.
563 * 0 if device's power state has been successfully changed.
565 int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
567 int error;
569 /* bound the state we're entering */
570 if (state > PCI_D3hot)
571 state = PCI_D3hot;
572 else if (state < PCI_D0)
573 state = PCI_D0;
574 else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
576 * If the device or the parent bridge do not support PCI PM,
577 * ignore the request if we're doing anything other than putting
578 * it into D0 (which would only happen on boot).
580 return 0;
582 if (state == PCI_D0 && platform_pci_power_manageable(dev)) {
584 * Allow the platform to change the state, for example via ACPI
585 * _PR0, _PS0 and some such, but do not trust it.
587 int ret = platform_pci_set_power_state(dev, PCI_D0);
588 if (!ret)
589 pci_update_current_state(dev, PCI_D0);
591 /* This device is quirked not to be put into D3, so
592 don't put it in D3 */
593 if (state == PCI_D3hot && (dev->dev_flags & PCI_DEV_FLAGS_NO_D3))
594 return 0;
596 error = pci_raw_set_power_state(dev, state, true);
598 if (state > PCI_D0 && platform_pci_power_manageable(dev)) {
599 /* Allow the platform to finalize the transition */
600 int ret = platform_pci_set_power_state(dev, state);
601 if (!ret) {
602 pci_update_current_state(dev, state);
603 error = 0;
607 return error;
611 * pci_choose_state - Choose the power state of a PCI device
612 * @dev: PCI device to be suspended
613 * @state: target sleep state for the whole system. This is the value
614 * that is passed to suspend() function.
616 * Returns PCI power state suitable for given device and given system
617 * message.
620 pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
622 pci_power_t ret;
624 if (!pci_find_capability(dev, PCI_CAP_ID_PM))
625 return PCI_D0;
627 ret = platform_pci_choose_state(dev);
628 if (ret != PCI_POWER_ERROR)
629 return ret;
631 switch (state.event) {
632 case PM_EVENT_ON:
633 return PCI_D0;
634 case PM_EVENT_FREEZE:
635 case PM_EVENT_PRETHAW:
636 /* REVISIT both freeze and pre-thaw "should" use D0 */
637 case PM_EVENT_SUSPEND:
638 case PM_EVENT_HIBERNATE:
639 return PCI_D3hot;
640 default:
641 dev_info(&dev->dev, "unrecognized suspend event %d\n",
642 state.event);
643 BUG();
645 return PCI_D0;
648 EXPORT_SYMBOL(pci_choose_state);
650 static int pci_save_pcie_state(struct pci_dev *dev)
652 int pos, i = 0;
653 struct pci_cap_saved_state *save_state;
654 u16 *cap;
656 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
657 if (pos <= 0)
658 return 0;
660 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
661 if (!save_state) {
662 dev_err(&dev->dev, "buffer not found in %s\n", __FUNCTION__);
663 return -ENOMEM;
665 cap = (u16 *)&save_state->data[0];
667 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, &cap[i++]);
668 pci_read_config_word(dev, pos + PCI_EXP_LNKCTL, &cap[i++]);
669 pci_read_config_word(dev, pos + PCI_EXP_SLTCTL, &cap[i++]);
670 pci_read_config_word(dev, pos + PCI_EXP_RTCTL, &cap[i++]);
672 return 0;
675 static void pci_restore_pcie_state(struct pci_dev *dev)
677 int i = 0, pos;
678 struct pci_cap_saved_state *save_state;
679 u16 *cap;
681 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
682 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
683 if (!save_state || pos <= 0)
684 return;
685 cap = (u16 *)&save_state->data[0];
687 pci_write_config_word(dev, pos + PCI_EXP_DEVCTL, cap[i++]);
688 pci_write_config_word(dev, pos + PCI_EXP_LNKCTL, cap[i++]);
689 pci_write_config_word(dev, pos + PCI_EXP_SLTCTL, cap[i++]);
690 pci_write_config_word(dev, pos + PCI_EXP_RTCTL, cap[i++]);
694 static int pci_save_pcix_state(struct pci_dev *dev)
696 int pos;
697 struct pci_cap_saved_state *save_state;
699 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
700 if (pos <= 0)
701 return 0;
703 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
704 if (!save_state) {
705 dev_err(&dev->dev, "buffer not found in %s\n", __FUNCTION__);
706 return -ENOMEM;
709 pci_read_config_word(dev, pos + PCI_X_CMD, (u16 *)save_state->data);
711 return 0;
714 static void pci_restore_pcix_state(struct pci_dev *dev)
716 int i = 0, pos;
717 struct pci_cap_saved_state *save_state;
718 u16 *cap;
720 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_PCIX);
721 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
722 if (!save_state || pos <= 0)
723 return;
724 cap = (u16 *)&save_state->data[0];
726 pci_write_config_word(dev, pos + PCI_X_CMD, cap[i++]);
731 * pci_save_state - save the PCI configuration space of a device before suspending
732 * @dev: - PCI device that we're dealing with
735 pci_save_state(struct pci_dev *dev)
737 int i;
738 /* XXX: 100% dword access ok here? */
739 for (i = 0; i < 16; i++)
740 pci_read_config_dword(dev, i * 4,&dev->saved_config_space[i]);
741 dev->state_saved = true;
742 if ((i = pci_save_pcie_state(dev)) != 0)
743 return i;
744 if ((i = pci_save_pcix_state(dev)) != 0)
745 return i;
746 return 0;
749 /**
750 * pci_restore_state - Restore the saved state of a PCI device
751 * @dev: - PCI device that we're dealing with
753 int
754 pci_restore_state(struct pci_dev *dev)
756 int i;
757 u32 val;
759 /* PCI Express register must be restored first */
760 pci_restore_pcie_state(dev);
763 * The Base Address register should be programmed before the command
764 * register(s)
766 for (i = 15; i >= 0; i--) {
767 pci_read_config_dword(dev, i * 4, &val);
768 if (val != dev->saved_config_space[i]) {
769 dev_printk(KERN_DEBUG, &dev->dev, "restoring config "
770 "space at offset %#x (was %#x, writing %#x)\n",
771 i, val, (int)dev->saved_config_space[i]);
772 pci_write_config_dword(dev,i * 4,
773 dev->saved_config_space[i]);
776 pci_restore_pcix_state(dev);
777 pci_restore_msi_state(dev);
779 return 0;
782 static int do_pci_enable_device(struct pci_dev *dev, int bars)
784 int err;
786 err = pci_set_power_state(dev, PCI_D0);
787 if (err < 0 && err != -EIO)
788 return err;
789 err = pcibios_enable_device(dev, bars);
790 if (err < 0)
791 return err;
792 pci_fixup_device(pci_fixup_enable, dev);
794 return 0;
798 * pci_reenable_device - Resume abandoned device
799 * @dev: PCI device to be resumed
801 * Note this function is a backend of pci_default_resume and is not supposed
802 * to be called by normal code, write proper resume handler and use it instead.
804 int pci_reenable_device(struct pci_dev *dev)
806 if (atomic_read(&dev->enable_cnt))
807 return do_pci_enable_device(dev, (1 << PCI_NUM_RESOURCES) - 1);
808 return 0;
811 static int __pci_enable_device_flags(struct pci_dev *dev,
812 resource_size_t flags)
814 int err;
815 int i, bars = 0;
817 if (atomic_add_return(1, &dev->enable_cnt) > 1)
818 return 0; /* already enabled */
820 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
821 if (dev->resource[i].flags & flags)
822 bars |= (1 << i);
824 err = do_pci_enable_device(dev, bars);
825 if (err < 0)
826 atomic_dec(&dev->enable_cnt);
827 return err;
831 * pci_enable_device_io - Initialize a device for use with IO space
832 * @dev: PCI device to be initialized
834 * Initialize device before it's used by a driver. Ask low-level code
835 * to enable I/O resources. Wake up the device if it was suspended.
836 * Beware, this function can fail.
838 int pci_enable_device_io(struct pci_dev *dev)
840 return __pci_enable_device_flags(dev, IORESOURCE_IO);
844 * pci_enable_device_mem - Initialize a device for use with Memory space
845 * @dev: PCI device to be initialized
847 * Initialize device before it's used by a driver. Ask low-level code
848 * to enable Memory resources. Wake up the device if it was suspended.
849 * Beware, this function can fail.
851 int pci_enable_device_mem(struct pci_dev *dev)
853 return __pci_enable_device_flags(dev, IORESOURCE_MEM);
857 * pci_enable_device - Initialize device before it's used by a driver.
858 * @dev: PCI device to be initialized
860 * Initialize device before it's used by a driver. Ask low-level code
861 * to enable I/O and memory. Wake up the device if it was suspended.
862 * Beware, this function can fail.
864 * Note we don't actually enable the device many times if we call
865 * this function repeatedly (we just increment the count).
867 int pci_enable_device(struct pci_dev *dev)
869 return __pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
873 * Managed PCI resources. This manages device on/off, intx/msi/msix
874 * on/off and BAR regions. pci_dev itself records msi/msix status, so
875 * there's no need to track it separately. pci_devres is initialized
876 * when a device is enabled using managed PCI device enable interface.
878 struct pci_devres {
879 unsigned int enabled:1;
880 unsigned int pinned:1;
881 unsigned int orig_intx:1;
882 unsigned int restore_intx:1;
883 u32 region_mask;
886 static void pcim_release(struct device *gendev, void *res)
888 struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
889 struct pci_devres *this = res;
890 int i;
892 if (dev->msi_enabled)
893 pci_disable_msi(dev);
894 if (dev->msix_enabled)
895 pci_disable_msix(dev);
897 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
898 if (this->region_mask & (1 << i))
899 pci_release_region(dev, i);
901 if (this->restore_intx)
902 pci_intx(dev, this->orig_intx);
904 if (this->enabled && !this->pinned)
905 pci_disable_device(dev);
908 static struct pci_devres * get_pci_dr(struct pci_dev *pdev)
910 struct pci_devres *dr, *new_dr;
912 dr = devres_find(&pdev->dev, pcim_release, NULL, NULL);
913 if (dr)
914 return dr;
916 new_dr = devres_alloc(pcim_release, sizeof(*new_dr), GFP_KERNEL);
917 if (!new_dr)
918 return NULL;
919 return devres_get(&pdev->dev, new_dr, NULL, NULL);
922 static struct pci_devres * find_pci_dr(struct pci_dev *pdev)
924 if (pci_is_managed(pdev))
925 return devres_find(&pdev->dev, pcim_release, NULL, NULL);
926 return NULL;
930 * pcim_enable_device - Managed pci_enable_device()
931 * @pdev: PCI device to be initialized
933 * Managed pci_enable_device().
935 int pcim_enable_device(struct pci_dev *pdev)
937 struct pci_devres *dr;
938 int rc;
940 dr = get_pci_dr(pdev);
941 if (unlikely(!dr))
942 return -ENOMEM;
943 if (dr->enabled)
944 return 0;
946 rc = pci_enable_device(pdev);
947 if (!rc) {
948 pdev->is_managed = 1;
949 dr->enabled = 1;
951 return rc;
955 * pcim_pin_device - Pin managed PCI device
956 * @pdev: PCI device to pin
958 * Pin managed PCI device @pdev. Pinned device won't be disabled on
959 * driver detach. @pdev must have been enabled with
960 * pcim_enable_device().
962 void pcim_pin_device(struct pci_dev *pdev)
964 struct pci_devres *dr;
966 dr = find_pci_dr(pdev);
967 WARN_ON(!dr || !dr->enabled);
968 if (dr)
969 dr->pinned = 1;
973 * pcibios_disable_device - disable arch specific PCI resources for device dev
974 * @dev: the PCI device to disable
976 * Disables architecture specific PCI resources for the device. This
977 * is the default implementation. Architecture implementations can
978 * override this.
980 void __attribute__ ((weak)) pcibios_disable_device (struct pci_dev *dev) {}
982 static void do_pci_disable_device(struct pci_dev *dev)
984 u16 pci_command;
986 pci_read_config_word(dev, PCI_COMMAND, &pci_command);
987 if (pci_command & PCI_COMMAND_MASTER) {
988 pci_command &= ~PCI_COMMAND_MASTER;
989 pci_write_config_word(dev, PCI_COMMAND, pci_command);
992 pcibios_disable_device(dev);
996 * pci_disable_enabled_device - Disable device without updating enable_cnt
997 * @dev: PCI device to disable
999 * NOTE: This function is a backend of PCI power management routines and is
1000 * not supposed to be called drivers.
1002 void pci_disable_enabled_device(struct pci_dev *dev)
1004 if (atomic_read(&dev->enable_cnt))
1005 do_pci_disable_device(dev);
1009 * pci_disable_device - Disable PCI device after use
1010 * @dev: PCI device to be disabled
1012 * Signal to the system that the PCI device is not in use by the system
1013 * anymore. This only involves disabling PCI bus-mastering, if active.
1015 * Note we don't actually disable the device until all callers of
1016 * pci_device_enable() have called pci_device_disable().
1018 void
1019 pci_disable_device(struct pci_dev *dev)
1021 struct pci_devres *dr;
1023 dr = find_pci_dr(dev);
1024 if (dr)
1025 dr->enabled = 0;
1027 if (atomic_sub_return(1, &dev->enable_cnt) != 0)
1028 return;
1030 do_pci_disable_device(dev);
1032 dev->is_busmaster = 0;
1036 * pcibios_set_pcie_reset_state - set reset state for device dev
1037 * @dev: the PCI-E device reset
1038 * @state: Reset state to enter into
1041 * Sets the PCI-E reset state for the device. This is the default
1042 * implementation. Architecture implementations can override this.
1044 int __attribute__ ((weak)) pcibios_set_pcie_reset_state(struct pci_dev *dev,
1045 enum pcie_reset_state state)
1047 return -EINVAL;
1051 * pci_set_pcie_reset_state - set reset state for device dev
1052 * @dev: the PCI-E device reset
1053 * @state: Reset state to enter into
1056 * Sets the PCI reset state for the device.
1058 int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
1060 return pcibios_set_pcie_reset_state(dev, state);
1064 * pci_pme_capable - check the capability of PCI device to generate PME#
1065 * @dev: PCI device to handle.
1066 * @state: PCI state from which device will issue PME#.
1068 bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
1070 if (!dev->pm_cap)
1071 return false;
1073 return !!(dev->pme_support & (1 << state));
1077 * pci_pme_active - enable or disable PCI device's PME# function
1078 * @dev: PCI device to handle.
1079 * @enable: 'true' to enable PME# generation; 'false' to disable it.
1081 * The caller must verify that the device is capable of generating PME# before
1082 * calling this function with @enable equal to 'true'.
1084 void pci_pme_active(struct pci_dev *dev, bool enable)
1086 u16 pmcsr;
1088 if (!dev->pm_cap)
1089 return;
1091 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1092 /* Clear PME_Status by writing 1 to it and enable PME# */
1093 pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
1094 if (!enable)
1095 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1097 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
1099 dev_printk(KERN_INFO, &dev->dev, "PME# %s\n",
1100 enable ? "enabled" : "disabled");
1104 * pci_enable_wake - enable PCI device as wakeup event source
1105 * @dev: PCI device affected
1106 * @state: PCI state from which device will issue wakeup events
1107 * @enable: True to enable event generation; false to disable
1109 * This enables the device as a wakeup event source, or disables it.
1110 * When such events involves platform-specific hooks, those hooks are
1111 * called automatically by this routine.
1113 * Devices with legacy power management (no standard PCI PM capabilities)
1114 * always require such platform hooks.
1116 * RETURN VALUE:
1117 * 0 is returned on success
1118 * -EINVAL is returned if device is not supposed to wake up the system
1119 * Error code depending on the platform is returned if both the platform and
1120 * the native mechanism fail to enable the generation of wake-up events
1122 int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable)
1124 int error = 0;
1125 bool pme_done = false;
1127 if (enable && !device_may_wakeup(&dev->dev))
1128 return -EINVAL;
1131 * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
1132 * Anderson we should be doing PME# wake enable followed by ACPI wake
1133 * enable. To disable wake-up we call the platform first, for symmetry.
1136 if (!enable && platform_pci_can_wakeup(dev))
1137 error = platform_pci_sleep_wake(dev, false);
1139 if (!enable || pci_pme_capable(dev, state)) {
1140 pci_pme_active(dev, enable);
1141 pme_done = true;
1144 if (enable && platform_pci_can_wakeup(dev))
1145 error = platform_pci_sleep_wake(dev, true);
1147 return pme_done ? 0 : error;
1151 * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
1152 * @dev: PCI device to prepare
1153 * @enable: True to enable wake-up event generation; false to disable
1155 * Many drivers want the device to wake up the system from D3_hot or D3_cold
1156 * and this function allows them to set that up cleanly - pci_enable_wake()
1157 * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
1158 * ordering constraints.
1160 * This function only returns error code if the device is not capable of
1161 * generating PME# from both D3_hot and D3_cold, and the platform is unable to
1162 * enable wake-up power for it.
1164 int pci_wake_from_d3(struct pci_dev *dev, bool enable)
1166 return pci_pme_capable(dev, PCI_D3cold) ?
1167 pci_enable_wake(dev, PCI_D3cold, enable) :
1168 pci_enable_wake(dev, PCI_D3hot, enable);
1172 * pci_target_state - find an appropriate low power state for a given PCI dev
1173 * @dev: PCI device
1175 * Use underlying platform code to find a supported low power state for @dev.
1176 * If the platform can't manage @dev, return the deepest state from which it
1177 * can generate wake events, based on any available PME info.
1179 pci_power_t pci_target_state(struct pci_dev *dev)
1181 pci_power_t target_state = PCI_D3hot;
1183 if (platform_pci_power_manageable(dev)) {
1185 * Call the platform to choose the target state of the device
1186 * and enable wake-up from this state if supported.
1188 pci_power_t state = platform_pci_choose_state(dev);
1190 switch (state) {
1191 case PCI_POWER_ERROR:
1192 case PCI_UNKNOWN:
1193 break;
1194 case PCI_D1:
1195 case PCI_D2:
1196 if (pci_no_d1d2(dev))
1197 break;
1198 default:
1199 target_state = state;
1201 } else if (!dev->pm_cap) {
1202 target_state = PCI_D0;
1203 } else if (device_may_wakeup(&dev->dev)) {
1205 * Find the deepest state from which the device can generate
1206 * wake-up events, make it the target state and enable device
1207 * to generate PME#.
1209 if (dev->pme_support) {
1210 while (target_state
1211 && !(dev->pme_support & (1 << target_state)))
1212 target_state--;
1216 return target_state;
1220 * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
1221 * @dev: Device to handle.
1223 * Choose the power state appropriate for the device depending on whether
1224 * it can wake up the system and/or is power manageable by the platform
1225 * (PCI_D3hot is the default) and put the device into that state.
1227 int pci_prepare_to_sleep(struct pci_dev *dev)
1229 pci_power_t target_state = pci_target_state(dev);
1230 int error;
1232 if (target_state == PCI_POWER_ERROR)
1233 return -EIO;
1235 pci_enable_wake(dev, target_state, true);
1237 error = pci_set_power_state(dev, target_state);
1239 if (error)
1240 pci_enable_wake(dev, target_state, false);
1242 return error;
1246 * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
1247 * @dev: Device to handle.
1249 * Disable device's sytem wake-up capability and put it into D0.
1251 int pci_back_from_sleep(struct pci_dev *dev)
1253 pci_enable_wake(dev, PCI_D0, false);
1254 return pci_set_power_state(dev, PCI_D0);
1258 * pci_pm_init - Initialize PM functions of given PCI device
1259 * @dev: PCI device to handle.
1261 void pci_pm_init(struct pci_dev *dev)
1263 int pm;
1264 u16 pmc;
1266 dev->pm_cap = 0;
1268 /* find PCI PM capability in list */
1269 pm = pci_find_capability(dev, PCI_CAP_ID_PM);
1270 if (!pm)
1271 return;
1272 /* Check device's ability to generate PME# */
1273 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
1275 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
1276 dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
1277 pmc & PCI_PM_CAP_VER_MASK);
1278 return;
1281 dev->pm_cap = pm;
1283 dev->d1_support = false;
1284 dev->d2_support = false;
1285 if (!pci_no_d1d2(dev)) {
1286 if (pmc & PCI_PM_CAP_D1)
1287 dev->d1_support = true;
1288 if (pmc & PCI_PM_CAP_D2)
1289 dev->d2_support = true;
1291 if (dev->d1_support || dev->d2_support)
1292 dev_printk(KERN_DEBUG, &dev->dev, "supports%s%s\n",
1293 dev->d1_support ? " D1" : "",
1294 dev->d2_support ? " D2" : "");
1297 pmc &= PCI_PM_CAP_PME_MASK;
1298 if (pmc) {
1299 dev_info(&dev->dev, "PME# supported from%s%s%s%s%s\n",
1300 (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
1301 (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
1302 (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
1303 (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
1304 (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
1305 dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
1307 * Make device's PM flags reflect the wake-up capability, but
1308 * let the user space enable it to wake up the system as needed.
1310 device_set_wakeup_capable(&dev->dev, true);
1311 device_set_wakeup_enable(&dev->dev, false);
1312 /* Disable the PME# generation functionality */
1313 pci_pme_active(dev, false);
1314 } else {
1315 dev->pme_support = 0;
1320 * platform_pci_wakeup_init - init platform wakeup if present
1321 * @dev: PCI device
1323 * Some devices don't have PCI PM caps but can still generate wakeup
1324 * events through platform methods (like ACPI events). If @dev supports
1325 * platform wakeup events, set the device flag to indicate as much. This
1326 * may be redundant if the device also supports PCI PM caps, but double
1327 * initialization should be safe in that case.
1329 void platform_pci_wakeup_init(struct pci_dev *dev)
1331 if (!platform_pci_can_wakeup(dev))
1332 return;
1334 device_set_wakeup_capable(&dev->dev, true);
1335 device_set_wakeup_enable(&dev->dev, false);
1336 platform_pci_sleep_wake(dev, false);
1340 * pci_add_save_buffer - allocate buffer for saving given capability registers
1341 * @dev: the PCI device
1342 * @cap: the capability to allocate the buffer for
1343 * @size: requested size of the buffer
1345 static int pci_add_cap_save_buffer(
1346 struct pci_dev *dev, char cap, unsigned int size)
1348 int pos;
1349 struct pci_cap_saved_state *save_state;
1351 pos = pci_find_capability(dev, cap);
1352 if (pos <= 0)
1353 return 0;
1355 save_state = kzalloc(sizeof(*save_state) + size, GFP_KERNEL);
1356 if (!save_state)
1357 return -ENOMEM;
1359 save_state->cap_nr = cap;
1360 pci_add_saved_cap(dev, save_state);
1362 return 0;
1366 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
1367 * @dev: the PCI device
1369 void pci_allocate_cap_save_buffers(struct pci_dev *dev)
1371 int error;
1373 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_EXP, 4 * sizeof(u16));
1374 if (error)
1375 dev_err(&dev->dev,
1376 "unable to preallocate PCI Express save buffer\n");
1378 error = pci_add_cap_save_buffer(dev, PCI_CAP_ID_PCIX, sizeof(u16));
1379 if (error)
1380 dev_err(&dev->dev,
1381 "unable to preallocate PCI-X save buffer\n");
1385 * pci_restore_standard_config - restore standard config registers of PCI device
1386 * @dev: PCI device to handle
1388 * This function assumes that the device's configuration space is accessible.
1389 * If the device needs to be powered up, the function will wait for it to
1390 * change the state.
1392 int pci_restore_standard_config(struct pci_dev *dev)
1394 pci_power_t prev_state;
1395 int error;
1397 pci_update_current_state(dev, PCI_D0);
1399 prev_state = dev->current_state;
1400 if (prev_state == PCI_D0)
1401 goto Restore;
1403 error = pci_raw_set_power_state(dev, PCI_D0, false);
1404 if (error)
1405 return error;
1408 * This assumes that we won't get a bus in B2 or B3 from the BIOS, but
1409 * we've made this assumption forever and it appears to be universally
1410 * satisfied.
1412 switch(prev_state) {
1413 case PCI_D3cold:
1414 case PCI_D3hot:
1415 mdelay(pci_pm_d3_delay);
1416 break;
1417 case PCI_D2:
1418 udelay(PCI_PM_D2_DELAY);
1419 break;
1422 pci_update_current_state(dev, PCI_D0);
1424 Restore:
1425 return dev->state_saved ? pci_restore_state(dev) : 0;
1429 * pci_enable_ari - enable ARI forwarding if hardware support it
1430 * @dev: the PCI device
1432 void pci_enable_ari(struct pci_dev *dev)
1434 int pos;
1435 u32 cap;
1436 u16 ctrl;
1437 struct pci_dev *bridge;
1439 if (!dev->is_pcie || dev->devfn)
1440 return;
1442 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
1443 if (!pos)
1444 return;
1446 bridge = dev->bus->self;
1447 if (!bridge || !bridge->is_pcie)
1448 return;
1450 pos = pci_find_capability(bridge, PCI_CAP_ID_EXP);
1451 if (!pos)
1452 return;
1454 pci_read_config_dword(bridge, pos + PCI_EXP_DEVCAP2, &cap);
1455 if (!(cap & PCI_EXP_DEVCAP2_ARI))
1456 return;
1458 pci_read_config_word(bridge, pos + PCI_EXP_DEVCTL2, &ctrl);
1459 ctrl |= PCI_EXP_DEVCTL2_ARI;
1460 pci_write_config_word(bridge, pos + PCI_EXP_DEVCTL2, ctrl);
1462 bridge->ari_enabled = 1;
1466 * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
1467 * @dev: the PCI device
1468 * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTD, 4=INTD)
1470 * Perform INTx swizzling for a device behind one level of bridge. This is
1471 * required by section 9.1 of the PCI-to-PCI bridge specification for devices
1472 * behind bridges on add-in cards.
1474 u8 pci_swizzle_interrupt_pin(struct pci_dev *dev, u8 pin)
1476 return (((pin - 1) + PCI_SLOT(dev->devfn)) % 4) + 1;
1480 pci_get_interrupt_pin(struct pci_dev *dev, struct pci_dev **bridge)
1482 u8 pin;
1484 pin = dev->pin;
1485 if (!pin)
1486 return -1;
1488 while (dev->bus->self) {
1489 pin = pci_swizzle_interrupt_pin(dev, pin);
1490 dev = dev->bus->self;
1492 *bridge = dev;
1493 return pin;
1497 * pci_common_swizzle - swizzle INTx all the way to root bridge
1498 * @dev: the PCI device
1499 * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
1501 * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
1502 * bridges all the way up to a PCI root bus.
1504 u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
1506 u8 pin = *pinp;
1508 while (dev->bus->self) {
1509 pin = pci_swizzle_interrupt_pin(dev, pin);
1510 dev = dev->bus->self;
1512 *pinp = pin;
1513 return PCI_SLOT(dev->devfn);
1517 * pci_release_region - Release a PCI bar
1518 * @pdev: PCI device whose resources were previously reserved by pci_request_region
1519 * @bar: BAR to release
1521 * Releases the PCI I/O and memory resources previously reserved by a
1522 * successful call to pci_request_region. Call this function only
1523 * after all use of the PCI regions has ceased.
1525 void pci_release_region(struct pci_dev *pdev, int bar)
1527 struct pci_devres *dr;
1529 if (pci_resource_len(pdev, bar) == 0)
1530 return;
1531 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
1532 release_region(pci_resource_start(pdev, bar),
1533 pci_resource_len(pdev, bar));
1534 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
1535 release_mem_region(pci_resource_start(pdev, bar),
1536 pci_resource_len(pdev, bar));
1538 dr = find_pci_dr(pdev);
1539 if (dr)
1540 dr->region_mask &= ~(1 << bar);
1544 * __pci_request_region - Reserved PCI I/O and memory resource
1545 * @pdev: PCI device whose resources are to be reserved
1546 * @bar: BAR to be reserved
1547 * @res_name: Name to be associated with resource.
1548 * @exclusive: whether the region access is exclusive or not
1550 * Mark the PCI region associated with PCI device @pdev BR @bar as
1551 * being reserved by owner @res_name. Do not access any
1552 * address inside the PCI regions unless this call returns
1553 * successfully.
1555 * If @exclusive is set, then the region is marked so that userspace
1556 * is explicitly not allowed to map the resource via /dev/mem or
1557 * sysfs MMIO access.
1559 * Returns 0 on success, or %EBUSY on error. A warning
1560 * message is also printed on failure.
1562 static int __pci_request_region(struct pci_dev *pdev, int bar, const char *res_name,
1563 int exclusive)
1565 struct pci_devres *dr;
1567 if (pci_resource_len(pdev, bar) == 0)
1568 return 0;
1570 if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) {
1571 if (!request_region(pci_resource_start(pdev, bar),
1572 pci_resource_len(pdev, bar), res_name))
1573 goto err_out;
1575 else if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
1576 if (!__request_mem_region(pci_resource_start(pdev, bar),
1577 pci_resource_len(pdev, bar), res_name,
1578 exclusive))
1579 goto err_out;
1582 dr = find_pci_dr(pdev);
1583 if (dr)
1584 dr->region_mask |= 1 << bar;
1586 return 0;
1588 err_out:
1589 dev_warn(&pdev->dev, "BAR %d: can't reserve %s region %pR\n",
1590 bar,
1591 pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem",
1592 &pdev->resource[bar]);
1593 return -EBUSY;
1597 * pci_request_region - Reserve PCI I/O and memory resource
1598 * @pdev: PCI device whose resources are to be reserved
1599 * @bar: BAR to be reserved
1600 * @res_name: Name to be associated with resource
1602 * Mark the PCI region associated with PCI device @pdev BAR @bar as
1603 * being reserved by owner @res_name. Do not access any
1604 * address inside the PCI regions unless this call returns
1605 * successfully.
1607 * Returns 0 on success, or %EBUSY on error. A warning
1608 * message is also printed on failure.
1610 int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
1612 return __pci_request_region(pdev, bar, res_name, 0);
1616 * pci_request_region_exclusive - Reserved PCI I/O and memory resource
1617 * @pdev: PCI device whose resources are to be reserved
1618 * @bar: BAR to be reserved
1619 * @res_name: Name to be associated with resource.
1621 * Mark the PCI region associated with PCI device @pdev BR @bar as
1622 * being reserved by owner @res_name. Do not access any
1623 * address inside the PCI regions unless this call returns
1624 * successfully.
1626 * Returns 0 on success, or %EBUSY on error. A warning
1627 * message is also printed on failure.
1629 * The key difference that _exclusive makes it that userspace is
1630 * explicitly not allowed to map the resource via /dev/mem or
1631 * sysfs.
1633 int pci_request_region_exclusive(struct pci_dev *pdev, int bar, const char *res_name)
1635 return __pci_request_region(pdev, bar, res_name, IORESOURCE_EXCLUSIVE);
1638 * pci_release_selected_regions - Release selected PCI I/O and memory resources
1639 * @pdev: PCI device whose resources were previously reserved
1640 * @bars: Bitmask of BARs to be released
1642 * Release selected PCI I/O and memory resources previously reserved.
1643 * Call this function only after all use of the PCI regions has ceased.
1645 void pci_release_selected_regions(struct pci_dev *pdev, int bars)
1647 int i;
1649 for (i = 0; i < 6; i++)
1650 if (bars & (1 << i))
1651 pci_release_region(pdev, i);
1654 int __pci_request_selected_regions(struct pci_dev *pdev, int bars,
1655 const char *res_name, int excl)
1657 int i;
1659 for (i = 0; i < 6; i++)
1660 if (bars & (1 << i))
1661 if (__pci_request_region(pdev, i, res_name, excl))
1662 goto err_out;
1663 return 0;
1665 err_out:
1666 while(--i >= 0)
1667 if (bars & (1 << i))
1668 pci_release_region(pdev, i);
1670 return -EBUSY;
1675 * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
1676 * @pdev: PCI device whose resources are to be reserved
1677 * @bars: Bitmask of BARs to be requested
1678 * @res_name: Name to be associated with resource
1680 int pci_request_selected_regions(struct pci_dev *pdev, int bars,
1681 const char *res_name)
1683 return __pci_request_selected_regions(pdev, bars, res_name, 0);
1686 int pci_request_selected_regions_exclusive(struct pci_dev *pdev,
1687 int bars, const char *res_name)
1689 return __pci_request_selected_regions(pdev, bars, res_name,
1690 IORESOURCE_EXCLUSIVE);
1694 * pci_release_regions - Release reserved PCI I/O and memory resources
1695 * @pdev: PCI device whose resources were previously reserved by pci_request_regions
1697 * Releases all PCI I/O and memory resources previously reserved by a
1698 * successful call to pci_request_regions. Call this function only
1699 * after all use of the PCI regions has ceased.
1702 void pci_release_regions(struct pci_dev *pdev)
1704 pci_release_selected_regions(pdev, (1 << 6) - 1);
1708 * pci_request_regions - Reserved PCI I/O and memory resources
1709 * @pdev: PCI device whose resources are to be reserved
1710 * @res_name: Name to be associated with resource.
1712 * Mark all PCI regions associated with PCI device @pdev as
1713 * being reserved by owner @res_name. Do not access any
1714 * address inside the PCI regions unless this call returns
1715 * successfully.
1717 * Returns 0 on success, or %EBUSY on error. A warning
1718 * message is also printed on failure.
1720 int pci_request_regions(struct pci_dev *pdev, const char *res_name)
1722 return pci_request_selected_regions(pdev, ((1 << 6) - 1), res_name);
1726 * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
1727 * @pdev: PCI device whose resources are to be reserved
1728 * @res_name: Name to be associated with resource.
1730 * Mark all PCI regions associated with PCI device @pdev as
1731 * being reserved by owner @res_name. Do not access any
1732 * address inside the PCI regions unless this call returns
1733 * successfully.
1735 * pci_request_regions_exclusive() will mark the region so that
1736 * /dev/mem and the sysfs MMIO access will not be allowed.
1738 * Returns 0 on success, or %EBUSY on error. A warning
1739 * message is also printed on failure.
1741 int pci_request_regions_exclusive(struct pci_dev *pdev, const char *res_name)
1743 return pci_request_selected_regions_exclusive(pdev,
1744 ((1 << 6) - 1), res_name);
1747 static void __pci_set_master(struct pci_dev *dev, bool enable)
1749 u16 old_cmd, cmd;
1751 pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
1752 if (enable)
1753 cmd = old_cmd | PCI_COMMAND_MASTER;
1754 else
1755 cmd = old_cmd & ~PCI_COMMAND_MASTER;
1756 if (cmd != old_cmd) {
1757 dev_dbg(&dev->dev, "%s bus mastering\n",
1758 enable ? "enabling" : "disabling");
1759 pci_write_config_word(dev, PCI_COMMAND, cmd);
1761 dev->is_busmaster = enable;
1765 * pci_set_master - enables bus-mastering for device dev
1766 * @dev: the PCI device to enable
1768 * Enables bus-mastering on the device and calls pcibios_set_master()
1769 * to do the needed arch specific settings.
1771 void pci_set_master(struct pci_dev *dev)
1773 __pci_set_master(dev, true);
1774 pcibios_set_master(dev);
1778 * pci_clear_master - disables bus-mastering for device dev
1779 * @dev: the PCI device to disable
1781 void pci_clear_master(struct pci_dev *dev)
1783 __pci_set_master(dev, false);
1786 #ifdef PCI_DISABLE_MWI
1787 int pci_set_mwi(struct pci_dev *dev)
1789 return 0;
1792 int pci_try_set_mwi(struct pci_dev *dev)
1794 return 0;
1797 void pci_clear_mwi(struct pci_dev *dev)
1801 #else
1803 #ifndef PCI_CACHE_LINE_BYTES
1804 #define PCI_CACHE_LINE_BYTES L1_CACHE_BYTES
1805 #endif
1807 /* This can be overridden by arch code. */
1808 /* Don't forget this is measured in 32-bit words, not bytes */
1809 u8 pci_cache_line_size = PCI_CACHE_LINE_BYTES / 4;
1812 * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
1813 * @dev: the PCI device for which MWI is to be enabled
1815 * Helper function for pci_set_mwi.
1816 * Originally copied from drivers/net/acenic.c.
1817 * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
1819 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1821 static int
1822 pci_set_cacheline_size(struct pci_dev *dev)
1824 u8 cacheline_size;
1826 if (!pci_cache_line_size)
1827 return -EINVAL; /* The system doesn't support MWI. */
1829 /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
1830 equal to or multiple of the right value. */
1831 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
1832 if (cacheline_size >= pci_cache_line_size &&
1833 (cacheline_size % pci_cache_line_size) == 0)
1834 return 0;
1836 /* Write the correct value. */
1837 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, pci_cache_line_size);
1838 /* Read it back. */
1839 pci_read_config_byte(dev, PCI_CACHE_LINE_SIZE, &cacheline_size);
1840 if (cacheline_size == pci_cache_line_size)
1841 return 0;
1843 dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
1844 "supported\n", pci_cache_line_size << 2);
1846 return -EINVAL;
1850 * pci_set_mwi - enables memory-write-invalidate PCI transaction
1851 * @dev: the PCI device for which MWI is enabled
1853 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
1855 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1858 pci_set_mwi(struct pci_dev *dev)
1860 int rc;
1861 u16 cmd;
1863 rc = pci_set_cacheline_size(dev);
1864 if (rc)
1865 return rc;
1867 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1868 if (! (cmd & PCI_COMMAND_INVALIDATE)) {
1869 dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
1870 cmd |= PCI_COMMAND_INVALIDATE;
1871 pci_write_config_word(dev, PCI_COMMAND, cmd);
1874 return 0;
1878 * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
1879 * @dev: the PCI device for which MWI is enabled
1881 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
1882 * Callers are not required to check the return value.
1884 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1886 int pci_try_set_mwi(struct pci_dev *dev)
1888 int rc = pci_set_mwi(dev);
1889 return rc;
1893 * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
1894 * @dev: the PCI device to disable
1896 * Disables PCI Memory-Write-Invalidate transaction on the device
1898 void
1899 pci_clear_mwi(struct pci_dev *dev)
1901 u16 cmd;
1903 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1904 if (cmd & PCI_COMMAND_INVALIDATE) {
1905 cmd &= ~PCI_COMMAND_INVALIDATE;
1906 pci_write_config_word(dev, PCI_COMMAND, cmd);
1909 #endif /* ! PCI_DISABLE_MWI */
1912 * pci_intx - enables/disables PCI INTx for device dev
1913 * @pdev: the PCI device to operate on
1914 * @enable: boolean: whether to enable or disable PCI INTx
1916 * Enables/disables PCI INTx for device dev
1918 void
1919 pci_intx(struct pci_dev *pdev, int enable)
1921 u16 pci_command, new;
1923 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
1925 if (enable) {
1926 new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
1927 } else {
1928 new = pci_command | PCI_COMMAND_INTX_DISABLE;
1931 if (new != pci_command) {
1932 struct pci_devres *dr;
1934 pci_write_config_word(pdev, PCI_COMMAND, new);
1936 dr = find_pci_dr(pdev);
1937 if (dr && !dr->restore_intx) {
1938 dr->restore_intx = 1;
1939 dr->orig_intx = !enable;
1945 * pci_msi_off - disables any msi or msix capabilities
1946 * @dev: the PCI device to operate on
1948 * If you want to use msi see pci_enable_msi and friends.
1949 * This is a lower level primitive that allows us to disable
1950 * msi operation at the device level.
1952 void pci_msi_off(struct pci_dev *dev)
1954 int pos;
1955 u16 control;
1957 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
1958 if (pos) {
1959 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
1960 control &= ~PCI_MSI_FLAGS_ENABLE;
1961 pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
1963 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
1964 if (pos) {
1965 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
1966 control &= ~PCI_MSIX_FLAGS_ENABLE;
1967 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
1971 #ifndef HAVE_ARCH_PCI_SET_DMA_MASK
1973 * These can be overridden by arch-specific implementations
1976 pci_set_dma_mask(struct pci_dev *dev, u64 mask)
1978 if (!pci_dma_supported(dev, mask))
1979 return -EIO;
1981 dev->dma_mask = mask;
1983 return 0;
1987 pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
1989 if (!pci_dma_supported(dev, mask))
1990 return -EIO;
1992 dev->dev.coherent_dma_mask = mask;
1994 return 0;
1996 #endif
1998 #ifndef HAVE_ARCH_PCI_SET_DMA_MAX_SEGMENT_SIZE
1999 int pci_set_dma_max_seg_size(struct pci_dev *dev, unsigned int size)
2001 return dma_set_max_seg_size(&dev->dev, size);
2003 EXPORT_SYMBOL(pci_set_dma_max_seg_size);
2004 #endif
2006 #ifndef HAVE_ARCH_PCI_SET_DMA_SEGMENT_BOUNDARY
2007 int pci_set_dma_seg_boundary(struct pci_dev *dev, unsigned long mask)
2009 return dma_set_seg_boundary(&dev->dev, mask);
2011 EXPORT_SYMBOL(pci_set_dma_seg_boundary);
2012 #endif
2014 static int __pcie_flr(struct pci_dev *dev, int probe)
2016 u16 status;
2017 u32 cap;
2018 int exppos = pci_find_capability(dev, PCI_CAP_ID_EXP);
2020 if (!exppos)
2021 return -ENOTTY;
2022 pci_read_config_dword(dev, exppos + PCI_EXP_DEVCAP, &cap);
2023 if (!(cap & PCI_EXP_DEVCAP_FLR))
2024 return -ENOTTY;
2026 if (probe)
2027 return 0;
2029 pci_block_user_cfg_access(dev);
2031 /* Wait for Transaction Pending bit clean */
2032 msleep(100);
2033 pci_read_config_word(dev, exppos + PCI_EXP_DEVSTA, &status);
2034 if (status & PCI_EXP_DEVSTA_TRPND) {
2035 dev_info(&dev->dev, "Busy after 100ms while trying to reset; "
2036 "sleeping for 1 second\n");
2037 ssleep(1);
2038 pci_read_config_word(dev, exppos + PCI_EXP_DEVSTA, &status);
2039 if (status & PCI_EXP_DEVSTA_TRPND)
2040 dev_info(&dev->dev, "Still busy after 1s; "
2041 "proceeding with reset anyway\n");
2044 pci_write_config_word(dev, exppos + PCI_EXP_DEVCTL,
2045 PCI_EXP_DEVCTL_BCR_FLR);
2046 mdelay(100);
2048 pci_unblock_user_cfg_access(dev);
2049 return 0;
2052 static int __pci_af_flr(struct pci_dev *dev, int probe)
2054 int cappos = pci_find_capability(dev, PCI_CAP_ID_AF);
2055 u8 status;
2056 u8 cap;
2058 if (!cappos)
2059 return -ENOTTY;
2060 pci_read_config_byte(dev, cappos + PCI_AF_CAP, &cap);
2061 if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
2062 return -ENOTTY;
2064 if (probe)
2065 return 0;
2067 pci_block_user_cfg_access(dev);
2069 /* Wait for Transaction Pending bit clean */
2070 msleep(100);
2071 pci_read_config_byte(dev, cappos + PCI_AF_STATUS, &status);
2072 if (status & PCI_AF_STATUS_TP) {
2073 dev_info(&dev->dev, "Busy after 100ms while trying to"
2074 " reset; sleeping for 1 second\n");
2075 ssleep(1);
2076 pci_read_config_byte(dev,
2077 cappos + PCI_AF_STATUS, &status);
2078 if (status & PCI_AF_STATUS_TP)
2079 dev_info(&dev->dev, "Still busy after 1s; "
2080 "proceeding with reset anyway\n");
2082 pci_write_config_byte(dev, cappos + PCI_AF_CTRL, PCI_AF_CTRL_FLR);
2083 mdelay(100);
2085 pci_unblock_user_cfg_access(dev);
2086 return 0;
2089 static int __pci_reset_function(struct pci_dev *pdev, int probe)
2091 int res;
2093 res = __pcie_flr(pdev, probe);
2094 if (res != -ENOTTY)
2095 return res;
2097 res = __pci_af_flr(pdev, probe);
2098 if (res != -ENOTTY)
2099 return res;
2101 return res;
2105 * pci_execute_reset_function() - Reset a PCI device function
2106 * @dev: Device function to reset
2108 * Some devices allow an individual function to be reset without affecting
2109 * other functions in the same device. The PCI device must be responsive
2110 * to PCI config space in order to use this function.
2112 * The device function is presumed to be unused when this function is called.
2113 * Resetting the device will make the contents of PCI configuration space
2114 * random, so any caller of this must be prepared to reinitialise the
2115 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
2116 * etc.
2118 * Returns 0 if the device function was successfully reset or -ENOTTY if the
2119 * device doesn't support resetting a single function.
2121 int pci_execute_reset_function(struct pci_dev *dev)
2123 return __pci_reset_function(dev, 0);
2125 EXPORT_SYMBOL_GPL(pci_execute_reset_function);
2128 * pci_reset_function() - quiesce and reset a PCI device function
2129 * @dev: Device function to reset
2131 * Some devices allow an individual function to be reset without affecting
2132 * other functions in the same device. The PCI device must be responsive
2133 * to PCI config space in order to use this function.
2135 * This function does not just reset the PCI portion of a device, but
2136 * clears all the state associated with the device. This function differs
2137 * from pci_execute_reset_function in that it saves and restores device state
2138 * over the reset.
2140 * Returns 0 if the device function was successfully reset or -ENOTTY if the
2141 * device doesn't support resetting a single function.
2143 int pci_reset_function(struct pci_dev *dev)
2145 int r = __pci_reset_function(dev, 1);
2147 if (r < 0)
2148 return r;
2150 if (!dev->msi_enabled && !dev->msix_enabled && dev->irq != 0)
2151 disable_irq(dev->irq);
2152 pci_save_state(dev);
2154 pci_write_config_word(dev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
2156 r = pci_execute_reset_function(dev);
2158 pci_restore_state(dev);
2159 if (!dev->msi_enabled && !dev->msix_enabled && dev->irq != 0)
2160 enable_irq(dev->irq);
2162 return r;
2164 EXPORT_SYMBOL_GPL(pci_reset_function);
2167 * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
2168 * @dev: PCI device to query
2170 * Returns mmrbc: maximum designed memory read count in bytes
2171 * or appropriate error value.
2173 int pcix_get_max_mmrbc(struct pci_dev *dev)
2175 int err, cap;
2176 u32 stat;
2178 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
2179 if (!cap)
2180 return -EINVAL;
2182 err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
2183 if (err)
2184 return -EINVAL;
2186 return (stat & PCI_X_STATUS_MAX_READ) >> 12;
2188 EXPORT_SYMBOL(pcix_get_max_mmrbc);
2191 * pcix_get_mmrbc - get PCI-X maximum memory read byte count
2192 * @dev: PCI device to query
2194 * Returns mmrbc: maximum memory read count in bytes
2195 * or appropriate error value.
2197 int pcix_get_mmrbc(struct pci_dev *dev)
2199 int ret, cap;
2200 u32 cmd;
2202 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
2203 if (!cap)
2204 return -EINVAL;
2206 ret = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
2207 if (!ret)
2208 ret = 512 << ((cmd & PCI_X_CMD_MAX_READ) >> 2);
2210 return ret;
2212 EXPORT_SYMBOL(pcix_get_mmrbc);
2215 * pcix_set_mmrbc - set PCI-X maximum memory read byte count
2216 * @dev: PCI device to query
2217 * @mmrbc: maximum memory read count in bytes
2218 * valid values are 512, 1024, 2048, 4096
2220 * If possible sets maximum memory read byte count, some bridges have erratas
2221 * that prevent this.
2223 int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
2225 int cap, err = -EINVAL;
2226 u32 stat, cmd, v, o;
2228 if (mmrbc < 512 || mmrbc > 4096 || !is_power_of_2(mmrbc))
2229 goto out;
2231 v = ffs(mmrbc) - 10;
2233 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
2234 if (!cap)
2235 goto out;
2237 err = pci_read_config_dword(dev, cap + PCI_X_STATUS, &stat);
2238 if (err)
2239 goto out;
2241 if (v > (stat & PCI_X_STATUS_MAX_READ) >> 21)
2242 return -E2BIG;
2244 err = pci_read_config_dword(dev, cap + PCI_X_CMD, &cmd);
2245 if (err)
2246 goto out;
2248 o = (cmd & PCI_X_CMD_MAX_READ) >> 2;
2249 if (o != v) {
2250 if (v > o && dev->bus &&
2251 (dev->bus->bus_flags & PCI_BUS_FLAGS_NO_MMRBC))
2252 return -EIO;
2254 cmd &= ~PCI_X_CMD_MAX_READ;
2255 cmd |= v << 2;
2256 err = pci_write_config_dword(dev, cap + PCI_X_CMD, cmd);
2258 out:
2259 return err;
2261 EXPORT_SYMBOL(pcix_set_mmrbc);
2264 * pcie_get_readrq - get PCI Express read request size
2265 * @dev: PCI device to query
2267 * Returns maximum memory read request in bytes
2268 * or appropriate error value.
2270 int pcie_get_readrq(struct pci_dev *dev)
2272 int ret, cap;
2273 u16 ctl;
2275 cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
2276 if (!cap)
2277 return -EINVAL;
2279 ret = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
2280 if (!ret)
2281 ret = 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
2283 return ret;
2285 EXPORT_SYMBOL(pcie_get_readrq);
2288 * pcie_set_readrq - set PCI Express maximum memory read request
2289 * @dev: PCI device to query
2290 * @rq: maximum memory read count in bytes
2291 * valid values are 128, 256, 512, 1024, 2048, 4096
2293 * If possible sets maximum read byte count
2295 int pcie_set_readrq(struct pci_dev *dev, int rq)
2297 int cap, err = -EINVAL;
2298 u16 ctl, v;
2300 if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
2301 goto out;
2303 v = (ffs(rq) - 8) << 12;
2305 cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
2306 if (!cap)
2307 goto out;
2309 err = pci_read_config_word(dev, cap + PCI_EXP_DEVCTL, &ctl);
2310 if (err)
2311 goto out;
2313 if ((ctl & PCI_EXP_DEVCTL_READRQ) != v) {
2314 ctl &= ~PCI_EXP_DEVCTL_READRQ;
2315 ctl |= v;
2316 err = pci_write_config_dword(dev, cap + PCI_EXP_DEVCTL, ctl);
2319 out:
2320 return err;
2322 EXPORT_SYMBOL(pcie_set_readrq);
2325 * pci_select_bars - Make BAR mask from the type of resource
2326 * @dev: the PCI device for which BAR mask is made
2327 * @flags: resource type mask to be selected
2329 * This helper routine makes bar mask from the type of resource.
2331 int pci_select_bars(struct pci_dev *dev, unsigned long flags)
2333 int i, bars = 0;
2334 for (i = 0; i < PCI_NUM_RESOURCES; i++)
2335 if (pci_resource_flags(dev, i) & flags)
2336 bars |= (1 << i);
2337 return bars;
2341 * pci_resource_bar - get position of the BAR associated with a resource
2342 * @dev: the PCI device
2343 * @resno: the resource number
2344 * @type: the BAR type to be filled in
2346 * Returns BAR position in config space, or 0 if the BAR is invalid.
2348 int pci_resource_bar(struct pci_dev *dev, int resno, enum pci_bar_type *type)
2350 if (resno < PCI_ROM_RESOURCE) {
2351 *type = pci_bar_unknown;
2352 return PCI_BASE_ADDRESS_0 + 4 * resno;
2353 } else if (resno == PCI_ROM_RESOURCE) {
2354 *type = pci_bar_mem32;
2355 return dev->rom_base_reg;
2358 dev_err(&dev->dev, "BAR: invalid resource #%d\n", resno);
2359 return 0;
2362 static void __devinit pci_no_domains(void)
2364 #ifdef CONFIG_PCI_DOMAINS
2365 pci_domains_supported = 0;
2366 #endif
2370 * pci_ext_cfg_enabled - can we access extended PCI config space?
2371 * @dev: The PCI device of the root bridge.
2373 * Returns 1 if we can access PCI extended config space (offsets
2374 * greater than 0xff). This is the default implementation. Architecture
2375 * implementations can override this.
2377 int __attribute__ ((weak)) pci_ext_cfg_avail(struct pci_dev *dev)
2379 return 1;
2382 static int __devinit pci_init(void)
2384 struct pci_dev *dev = NULL;
2386 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
2387 pci_fixup_device(pci_fixup_final, dev);
2390 return 0;
2393 static int __init pci_setup(char *str)
2395 while (str) {
2396 char *k = strchr(str, ',');
2397 if (k)
2398 *k++ = 0;
2399 if (*str && (str = pcibios_setup(str)) && *str) {
2400 if (!strcmp(str, "nomsi")) {
2401 pci_no_msi();
2402 } else if (!strcmp(str, "noaer")) {
2403 pci_no_aer();
2404 } else if (!strcmp(str, "nodomains")) {
2405 pci_no_domains();
2406 } else if (!strncmp(str, "cbiosize=", 9)) {
2407 pci_cardbus_io_size = memparse(str + 9, &str);
2408 } else if (!strncmp(str, "cbmemsize=", 10)) {
2409 pci_cardbus_mem_size = memparse(str + 10, &str);
2410 } else {
2411 printk(KERN_ERR "PCI: Unknown option `%s'\n",
2412 str);
2415 str = k;
2417 return 0;
2419 early_param("pci", pci_setup);
2421 device_initcall(pci_init);
2423 EXPORT_SYMBOL(pci_reenable_device);
2424 EXPORT_SYMBOL(pci_enable_device_io);
2425 EXPORT_SYMBOL(pci_enable_device_mem);
2426 EXPORT_SYMBOL(pci_enable_device);
2427 EXPORT_SYMBOL(pcim_enable_device);
2428 EXPORT_SYMBOL(pcim_pin_device);
2429 EXPORT_SYMBOL(pci_disable_device);
2430 EXPORT_SYMBOL(pci_find_capability);
2431 EXPORT_SYMBOL(pci_bus_find_capability);
2432 EXPORT_SYMBOL(pci_release_regions);
2433 EXPORT_SYMBOL(pci_request_regions);
2434 EXPORT_SYMBOL(pci_request_regions_exclusive);
2435 EXPORT_SYMBOL(pci_release_region);
2436 EXPORT_SYMBOL(pci_request_region);
2437 EXPORT_SYMBOL(pci_request_region_exclusive);
2438 EXPORT_SYMBOL(pci_release_selected_regions);
2439 EXPORT_SYMBOL(pci_request_selected_regions);
2440 EXPORT_SYMBOL(pci_request_selected_regions_exclusive);
2441 EXPORT_SYMBOL(pci_set_master);
2442 EXPORT_SYMBOL(pci_clear_master);
2443 EXPORT_SYMBOL(pci_set_mwi);
2444 EXPORT_SYMBOL(pci_try_set_mwi);
2445 EXPORT_SYMBOL(pci_clear_mwi);
2446 EXPORT_SYMBOL_GPL(pci_intx);
2447 EXPORT_SYMBOL(pci_set_dma_mask);
2448 EXPORT_SYMBOL(pci_set_consistent_dma_mask);
2449 EXPORT_SYMBOL(pci_assign_resource);
2450 EXPORT_SYMBOL(pci_find_parent_resource);
2451 EXPORT_SYMBOL(pci_select_bars);
2453 EXPORT_SYMBOL(pci_set_power_state);
2454 EXPORT_SYMBOL(pci_save_state);
2455 EXPORT_SYMBOL(pci_restore_state);
2456 EXPORT_SYMBOL(pci_pme_capable);
2457 EXPORT_SYMBOL(pci_pme_active);
2458 EXPORT_SYMBOL(pci_enable_wake);
2459 EXPORT_SYMBOL(pci_wake_from_d3);
2460 EXPORT_SYMBOL(pci_target_state);
2461 EXPORT_SYMBOL(pci_prepare_to_sleep);
2462 EXPORT_SYMBOL(pci_back_from_sleep);
2463 EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);