2 * PCI Bus Services, see include/linux/pci.h for further explanation.
4 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
7 * Copyright 1997 -- 2000 Martin Mares <mj@ucw.cz>
10 #include <linux/kernel.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/pci.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/spinlock.h>
18 #include <linux/string.h>
19 #include <linux/log2.h>
20 #include <linux/pci-aspm.h>
21 #include <linux/pm_wakeup.h>
22 #include <linux/interrupt.h>
23 #include <linux/device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/pci_hotplug.h>
26 #include <asm-generic/pci-bridge.h>
27 #include <asm/setup.h>
28 #include <linux/aer.h>
31 const char *pci_power_names
[] = {
32 "error", "D0", "D1", "D2", "D3hot", "D3cold", "unknown",
34 EXPORT_SYMBOL_GPL(pci_power_names
);
36 int isa_dma_bridge_buggy
;
37 EXPORT_SYMBOL(isa_dma_bridge_buggy
);
40 EXPORT_SYMBOL(pci_pci_problems
);
42 unsigned int pci_pm_d3_delay
;
44 static void pci_pme_list_scan(struct work_struct
*work
);
46 static LIST_HEAD(pci_pme_list
);
47 static DEFINE_MUTEX(pci_pme_list_mutex
);
48 static DECLARE_DELAYED_WORK(pci_pme_work
, pci_pme_list_scan
);
50 struct pci_pme_device
{
51 struct list_head list
;
55 #define PME_TIMEOUT 1000 /* How long between PME checks */
57 static void pci_dev_d3_sleep(struct pci_dev
*dev
)
59 unsigned int delay
= dev
->d3_delay
;
61 if (delay
< pci_pm_d3_delay
)
62 delay
= pci_pm_d3_delay
;
67 #ifdef CONFIG_PCI_DOMAINS
68 int pci_domains_supported
= 1;
71 #define DEFAULT_CARDBUS_IO_SIZE (256)
72 #define DEFAULT_CARDBUS_MEM_SIZE (64*1024*1024)
73 /* pci=cbmemsize=nnM,cbiosize=nn can override this */
74 unsigned long pci_cardbus_io_size
= DEFAULT_CARDBUS_IO_SIZE
;
75 unsigned long pci_cardbus_mem_size
= DEFAULT_CARDBUS_MEM_SIZE
;
77 #define DEFAULT_HOTPLUG_IO_SIZE (256)
78 #define DEFAULT_HOTPLUG_MEM_SIZE (2*1024*1024)
79 /* pci=hpmemsize=nnM,hpiosize=nn can override this */
80 unsigned long pci_hotplug_io_size
= DEFAULT_HOTPLUG_IO_SIZE
;
81 unsigned long pci_hotplug_mem_size
= DEFAULT_HOTPLUG_MEM_SIZE
;
83 enum pcie_bus_config_types pcie_bus_config
= PCIE_BUS_TUNE_OFF
;
86 * The default CLS is used if arch didn't set CLS explicitly and not
87 * all pci devices agree on the same value. Arch can override either
88 * the dfl or actual value as it sees fit. Don't forget this is
89 * measured in 32-bit words, not bytes.
91 u8 pci_dfl_cache_line_size
= L1_CACHE_BYTES
>> 2;
92 u8 pci_cache_line_size
;
95 * If we set up a device for bus mastering, we need to check the latency
96 * timer as certain BIOSes forget to set it properly.
98 unsigned int pcibios_max_latency
= 255;
100 /* If set, the PCIe ARI capability will not be used. */
101 static bool pcie_ari_disabled
;
104 * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
105 * @bus: pointer to PCI bus structure to search
107 * Given a PCI bus, returns the highest PCI bus number present in the set
108 * including the given PCI bus and its list of child PCI buses.
110 unsigned char pci_bus_max_busnr(struct pci_bus
* bus
)
112 struct list_head
*tmp
;
113 unsigned char max
, n
;
115 max
= bus
->busn_res
.end
;
116 list_for_each(tmp
, &bus
->children
) {
117 n
= pci_bus_max_busnr(pci_bus_b(tmp
));
123 EXPORT_SYMBOL_GPL(pci_bus_max_busnr
);
125 #ifdef CONFIG_HAS_IOMEM
126 void __iomem
*pci_ioremap_bar(struct pci_dev
*pdev
, int bar
)
129 * Make sure the BAR is actually a memory resource, not an IO resource
131 if (!(pci_resource_flags(pdev
, bar
) & IORESOURCE_MEM
)) {
135 return ioremap_nocache(pci_resource_start(pdev
, bar
),
136 pci_resource_len(pdev
, bar
));
138 EXPORT_SYMBOL_GPL(pci_ioremap_bar
);
141 #define PCI_FIND_CAP_TTL 48
143 static int __pci_find_next_cap_ttl(struct pci_bus
*bus
, unsigned int devfn
,
144 u8 pos
, int cap
, int *ttl
)
149 pci_bus_read_config_byte(bus
, devfn
, pos
, &pos
);
153 pci_bus_read_config_byte(bus
, devfn
, pos
+ PCI_CAP_LIST_ID
,
159 pos
+= PCI_CAP_LIST_NEXT
;
164 static int __pci_find_next_cap(struct pci_bus
*bus
, unsigned int devfn
,
167 int ttl
= PCI_FIND_CAP_TTL
;
169 return __pci_find_next_cap_ttl(bus
, devfn
, pos
, cap
, &ttl
);
172 int pci_find_next_capability(struct pci_dev
*dev
, u8 pos
, int cap
)
174 return __pci_find_next_cap(dev
->bus
, dev
->devfn
,
175 pos
+ PCI_CAP_LIST_NEXT
, cap
);
177 EXPORT_SYMBOL_GPL(pci_find_next_capability
);
179 static int __pci_bus_find_cap_start(struct pci_bus
*bus
,
180 unsigned int devfn
, u8 hdr_type
)
184 pci_bus_read_config_word(bus
, devfn
, PCI_STATUS
, &status
);
185 if (!(status
& PCI_STATUS_CAP_LIST
))
189 case PCI_HEADER_TYPE_NORMAL
:
190 case PCI_HEADER_TYPE_BRIDGE
:
191 return PCI_CAPABILITY_LIST
;
192 case PCI_HEADER_TYPE_CARDBUS
:
193 return PCI_CB_CAPABILITY_LIST
;
202 * pci_find_capability - query for devices' capabilities
203 * @dev: PCI device to query
204 * @cap: capability code
206 * Tell if a device supports a given PCI capability.
207 * Returns the address of the requested capability structure within the
208 * device's PCI configuration space or 0 in case the device does not
209 * support it. Possible values for @cap:
211 * %PCI_CAP_ID_PM Power Management
212 * %PCI_CAP_ID_AGP Accelerated Graphics Port
213 * %PCI_CAP_ID_VPD Vital Product Data
214 * %PCI_CAP_ID_SLOTID Slot Identification
215 * %PCI_CAP_ID_MSI Message Signalled Interrupts
216 * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
217 * %PCI_CAP_ID_PCIX PCI-X
218 * %PCI_CAP_ID_EXP PCI Express
220 int pci_find_capability(struct pci_dev
*dev
, int cap
)
224 pos
= __pci_bus_find_cap_start(dev
->bus
, dev
->devfn
, dev
->hdr_type
);
226 pos
= __pci_find_next_cap(dev
->bus
, dev
->devfn
, pos
, cap
);
232 * pci_bus_find_capability - query for devices' capabilities
233 * @bus: the PCI bus to query
234 * @devfn: PCI device to query
235 * @cap: capability code
237 * Like pci_find_capability() but works for pci devices that do not have a
238 * pci_dev structure set up yet.
240 * Returns the address of the requested capability structure within the
241 * device's PCI configuration space or 0 in case the device does not
244 int pci_bus_find_capability(struct pci_bus
*bus
, unsigned int devfn
, int cap
)
249 pci_bus_read_config_byte(bus
, devfn
, PCI_HEADER_TYPE
, &hdr_type
);
251 pos
= __pci_bus_find_cap_start(bus
, devfn
, hdr_type
& 0x7f);
253 pos
= __pci_find_next_cap(bus
, devfn
, pos
, cap
);
259 * pci_find_next_ext_capability - Find an extended capability
260 * @dev: PCI device to query
261 * @start: address at which to start looking (0 to start at beginning of list)
262 * @cap: capability code
264 * Returns the address of the next matching extended capability structure
265 * within the device's PCI configuration space or 0 if the device does
266 * not support it. Some capabilities can occur several times, e.g., the
267 * vendor-specific capability, and this provides a way to find them all.
269 int pci_find_next_ext_capability(struct pci_dev
*dev
, int start
, int cap
)
273 int pos
= PCI_CFG_SPACE_SIZE
;
275 /* minimum 8 bytes per capability */
276 ttl
= (PCI_CFG_SPACE_EXP_SIZE
- PCI_CFG_SPACE_SIZE
) / 8;
278 if (dev
->cfg_size
<= PCI_CFG_SPACE_SIZE
)
284 if (pci_read_config_dword(dev
, pos
, &header
) != PCIBIOS_SUCCESSFUL
)
288 * If we have no capabilities, this is indicated by cap ID,
289 * cap version and next pointer all being 0.
295 if (PCI_EXT_CAP_ID(header
) == cap
&& pos
!= start
)
298 pos
= PCI_EXT_CAP_NEXT(header
);
299 if (pos
< PCI_CFG_SPACE_SIZE
)
302 if (pci_read_config_dword(dev
, pos
, &header
) != PCIBIOS_SUCCESSFUL
)
308 EXPORT_SYMBOL_GPL(pci_find_next_ext_capability
);
311 * pci_find_ext_capability - Find an extended capability
312 * @dev: PCI device to query
313 * @cap: capability code
315 * Returns the address of the requested extended capability structure
316 * within the device's PCI configuration space or 0 if the device does
317 * not support it. Possible values for @cap:
319 * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
320 * %PCI_EXT_CAP_ID_VC Virtual Channel
321 * %PCI_EXT_CAP_ID_DSN Device Serial Number
322 * %PCI_EXT_CAP_ID_PWR Power Budgeting
324 int pci_find_ext_capability(struct pci_dev
*dev
, int cap
)
326 return pci_find_next_ext_capability(dev
, 0, cap
);
328 EXPORT_SYMBOL_GPL(pci_find_ext_capability
);
330 static int __pci_find_next_ht_cap(struct pci_dev
*dev
, int pos
, int ht_cap
)
332 int rc
, ttl
= PCI_FIND_CAP_TTL
;
335 if (ht_cap
== HT_CAPTYPE_SLAVE
|| ht_cap
== HT_CAPTYPE_HOST
)
336 mask
= HT_3BIT_CAP_MASK
;
338 mask
= HT_5BIT_CAP_MASK
;
340 pos
= __pci_find_next_cap_ttl(dev
->bus
, dev
->devfn
, pos
,
341 PCI_CAP_ID_HT
, &ttl
);
343 rc
= pci_read_config_byte(dev
, pos
+ 3, &cap
);
344 if (rc
!= PCIBIOS_SUCCESSFUL
)
347 if ((cap
& mask
) == ht_cap
)
350 pos
= __pci_find_next_cap_ttl(dev
->bus
, dev
->devfn
,
351 pos
+ PCI_CAP_LIST_NEXT
,
352 PCI_CAP_ID_HT
, &ttl
);
358 * pci_find_next_ht_capability - query a device's Hypertransport capabilities
359 * @dev: PCI device to query
360 * @pos: Position from which to continue searching
361 * @ht_cap: Hypertransport capability code
363 * To be used in conjunction with pci_find_ht_capability() to search for
364 * all capabilities matching @ht_cap. @pos should always be a value returned
365 * from pci_find_ht_capability().
367 * NB. To be 100% safe against broken PCI devices, the caller should take
368 * steps to avoid an infinite loop.
370 int pci_find_next_ht_capability(struct pci_dev
*dev
, int pos
, int ht_cap
)
372 return __pci_find_next_ht_cap(dev
, pos
+ PCI_CAP_LIST_NEXT
, ht_cap
);
374 EXPORT_SYMBOL_GPL(pci_find_next_ht_capability
);
377 * pci_find_ht_capability - query a device's Hypertransport capabilities
378 * @dev: PCI device to query
379 * @ht_cap: Hypertransport capability code
381 * Tell if a device supports a given Hypertransport capability.
382 * Returns an address within the device's PCI configuration space
383 * or 0 in case the device does not support the request capability.
384 * The address points to the PCI capability, of type PCI_CAP_ID_HT,
385 * which has a Hypertransport capability matching @ht_cap.
387 int pci_find_ht_capability(struct pci_dev
*dev
, int ht_cap
)
391 pos
= __pci_bus_find_cap_start(dev
->bus
, dev
->devfn
, dev
->hdr_type
);
393 pos
= __pci_find_next_ht_cap(dev
, pos
, ht_cap
);
397 EXPORT_SYMBOL_GPL(pci_find_ht_capability
);
400 * pci_find_parent_resource - return resource region of parent bus of given region
401 * @dev: PCI device structure contains resources to be searched
402 * @res: child resource record for which parent is sought
404 * For given resource region of given device, return the resource
405 * region of parent bus the given region is contained in or where
406 * it should be allocated from.
409 pci_find_parent_resource(const struct pci_dev
*dev
, struct resource
*res
)
411 const struct pci_bus
*bus
= dev
->bus
;
413 struct resource
*best
= NULL
, *r
;
415 pci_bus_for_each_resource(bus
, r
, i
) {
418 if (res
->start
&& !(res
->start
>= r
->start
&& res
->end
<= r
->end
))
419 continue; /* Not contained */
420 if ((res
->flags
^ r
->flags
) & (IORESOURCE_IO
| IORESOURCE_MEM
))
421 continue; /* Wrong type */
422 if (!((res
->flags
^ r
->flags
) & IORESOURCE_PREFETCH
))
423 return r
; /* Exact match */
424 /* We can't insert a non-prefetch resource inside a prefetchable parent .. */
425 if (r
->flags
& IORESOURCE_PREFETCH
)
427 /* .. but we can put a prefetchable resource inside a non-prefetchable one */
435 * pci_restore_bars - restore a devices BAR values (e.g. after wake-up)
436 * @dev: PCI device to have its BARs restored
438 * Restore the BAR values for a given device, so as to make it
439 * accessible by its driver.
442 pci_restore_bars(struct pci_dev
*dev
)
446 for (i
= 0; i
< PCI_BRIDGE_RESOURCES
; i
++)
447 pci_update_resource(dev
, i
);
450 static struct pci_platform_pm_ops
*pci_platform_pm
;
452 int pci_set_platform_pm(struct pci_platform_pm_ops
*ops
)
454 if (!ops
->is_manageable
|| !ops
->set_state
|| !ops
->choose_state
457 pci_platform_pm
= ops
;
461 static inline bool platform_pci_power_manageable(struct pci_dev
*dev
)
463 return pci_platform_pm
? pci_platform_pm
->is_manageable(dev
) : false;
466 static inline int platform_pci_set_power_state(struct pci_dev
*dev
,
469 return pci_platform_pm
? pci_platform_pm
->set_state(dev
, t
) : -ENOSYS
;
472 static inline pci_power_t
platform_pci_choose_state(struct pci_dev
*dev
)
474 return pci_platform_pm
?
475 pci_platform_pm
->choose_state(dev
) : PCI_POWER_ERROR
;
478 static inline int platform_pci_sleep_wake(struct pci_dev
*dev
, bool enable
)
480 return pci_platform_pm
?
481 pci_platform_pm
->sleep_wake(dev
, enable
) : -ENODEV
;
484 static inline int platform_pci_run_wake(struct pci_dev
*dev
, bool enable
)
486 return pci_platform_pm
?
487 pci_platform_pm
->run_wake(dev
, enable
) : -ENODEV
;
491 * pci_raw_set_power_state - Use PCI PM registers to set the power state of
493 * @dev: PCI device to handle.
494 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
497 * -EINVAL if the requested state is invalid.
498 * -EIO if device does not support PCI PM or its PM capabilities register has a
499 * wrong version, or device doesn't support the requested state.
500 * 0 if device already is in the requested state.
501 * 0 if device's power state has been successfully changed.
503 static int pci_raw_set_power_state(struct pci_dev
*dev
, pci_power_t state
)
506 bool need_restore
= false;
508 /* Check if we're already there */
509 if (dev
->current_state
== state
)
515 if (state
< PCI_D0
|| state
> PCI_D3hot
)
518 /* Validate current state:
519 * Can enter D0 from any state, but if we can only go deeper
520 * to sleep if we're already in a low power state
522 if (state
!= PCI_D0
&& dev
->current_state
<= PCI_D3cold
523 && dev
->current_state
> state
) {
524 dev_err(&dev
->dev
, "invalid power transition "
525 "(from state %d to %d)\n", dev
->current_state
, state
);
529 /* check if this device supports the desired state */
530 if ((state
== PCI_D1
&& !dev
->d1_support
)
531 || (state
== PCI_D2
&& !dev
->d2_support
))
534 pci_read_config_word(dev
, dev
->pm_cap
+ PCI_PM_CTRL
, &pmcsr
);
536 /* If we're (effectively) in D3, force entire word to 0.
537 * This doesn't affect PME_Status, disables PME_En, and
538 * sets PowerState to 0.
540 switch (dev
->current_state
) {
544 pmcsr
&= ~PCI_PM_CTRL_STATE_MASK
;
549 case PCI_UNKNOWN
: /* Boot-up */
550 if ((pmcsr
& PCI_PM_CTRL_STATE_MASK
) == PCI_D3hot
551 && !(pmcsr
& PCI_PM_CTRL_NO_SOFT_RESET
))
553 /* Fall-through: force to D0 */
559 /* enter specified state */
560 pci_write_config_word(dev
, dev
->pm_cap
+ PCI_PM_CTRL
, pmcsr
);
562 /* Mandatory power management transition delays */
563 /* see PCI PM 1.1 5.6.1 table 18 */
564 if (state
== PCI_D3hot
|| dev
->current_state
== PCI_D3hot
)
565 pci_dev_d3_sleep(dev
);
566 else if (state
== PCI_D2
|| dev
->current_state
== PCI_D2
)
567 udelay(PCI_PM_D2_DELAY
);
569 pci_read_config_word(dev
, dev
->pm_cap
+ PCI_PM_CTRL
, &pmcsr
);
570 dev
->current_state
= (pmcsr
& PCI_PM_CTRL_STATE_MASK
);
571 if (dev
->current_state
!= state
&& printk_ratelimit())
572 dev_info(&dev
->dev
, "Refused to change power state, "
573 "currently in D%d\n", dev
->current_state
);
576 * According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
577 * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
578 * from D3hot to D0 _may_ perform an internal reset, thereby
579 * going to "D0 Uninitialized" rather than "D0 Initialized".
580 * For example, at least some versions of the 3c905B and the
581 * 3c556B exhibit this behaviour.
583 * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
584 * devices in a D3hot state at boot. Consequently, we need to
585 * restore at least the BARs so that the device will be
586 * accessible to its driver.
589 pci_restore_bars(dev
);
592 pcie_aspm_pm_state_change(dev
->bus
->self
);
598 * pci_update_current_state - Read PCI power state of given device from its
599 * PCI PM registers and cache it
600 * @dev: PCI device to handle.
601 * @state: State to cache in case the device doesn't have the PM capability
603 void pci_update_current_state(struct pci_dev
*dev
, pci_power_t state
)
609 * Configuration space is not accessible for device in
610 * D3cold, so just keep or set D3cold for safety
612 if (dev
->current_state
== PCI_D3cold
)
614 if (state
== PCI_D3cold
) {
615 dev
->current_state
= PCI_D3cold
;
618 pci_read_config_word(dev
, dev
->pm_cap
+ PCI_PM_CTRL
, &pmcsr
);
619 dev
->current_state
= (pmcsr
& PCI_PM_CTRL_STATE_MASK
);
621 dev
->current_state
= state
;
626 * pci_power_up - Put the given device into D0 forcibly
627 * @dev: PCI device to power up
629 void pci_power_up(struct pci_dev
*dev
)
631 if (platform_pci_power_manageable(dev
))
632 platform_pci_set_power_state(dev
, PCI_D0
);
634 pci_raw_set_power_state(dev
, PCI_D0
);
635 pci_update_current_state(dev
, PCI_D0
);
639 * pci_platform_power_transition - Use platform to change device power state
640 * @dev: PCI device to handle.
641 * @state: State to put the device into.
643 static int pci_platform_power_transition(struct pci_dev
*dev
, pci_power_t state
)
647 if (platform_pci_power_manageable(dev
)) {
648 error
= platform_pci_set_power_state(dev
, state
);
650 pci_update_current_state(dev
, state
);
654 if (error
&& !dev
->pm_cap
) /* Fall back to PCI_D0 */
655 dev
->current_state
= PCI_D0
;
661 * __pci_start_power_transition - Start power transition of a PCI device
662 * @dev: PCI device to handle.
663 * @state: State to put the device into.
665 static void __pci_start_power_transition(struct pci_dev
*dev
, pci_power_t state
)
667 if (state
== PCI_D0
) {
668 pci_platform_power_transition(dev
, PCI_D0
);
670 * Mandatory power management transition delays, see
671 * PCI Express Base Specification Revision 2.0 Section
672 * 6.6.1: Conventional Reset. Do not delay for
673 * devices powered on/off by corresponding bridge,
674 * because have already delayed for the bridge.
676 if (dev
->runtime_d3cold
) {
677 msleep(dev
->d3cold_delay
);
679 * When powering on a bridge from D3cold, the
680 * whole hierarchy may be powered on into
681 * D0uninitialized state, resume them to give
682 * them a chance to suspend again
684 pci_wakeup_bus(dev
->subordinate
);
690 * __pci_dev_set_current_state - Set current state of a PCI device
691 * @dev: Device to handle
692 * @data: pointer to state to be set
694 static int __pci_dev_set_current_state(struct pci_dev
*dev
, void *data
)
696 pci_power_t state
= *(pci_power_t
*)data
;
698 dev
->current_state
= state
;
703 * __pci_bus_set_current_state - Walk given bus and set current state of devices
704 * @bus: Top bus of the subtree to walk.
705 * @state: state to be set
707 static void __pci_bus_set_current_state(struct pci_bus
*bus
, pci_power_t state
)
710 pci_walk_bus(bus
, __pci_dev_set_current_state
, &state
);
714 * __pci_complete_power_transition - Complete power transition of a PCI device
715 * @dev: PCI device to handle.
716 * @state: State to put the device into.
718 * This function should not be called directly by device drivers.
720 int __pci_complete_power_transition(struct pci_dev
*dev
, pci_power_t state
)
726 ret
= pci_platform_power_transition(dev
, state
);
727 /* Power off the bridge may power off the whole hierarchy */
728 if (!ret
&& state
== PCI_D3cold
)
729 __pci_bus_set_current_state(dev
->subordinate
, PCI_D3cold
);
732 EXPORT_SYMBOL_GPL(__pci_complete_power_transition
);
735 * pci_set_power_state - Set the power state of a PCI device
736 * @dev: PCI device to handle.
737 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
739 * Transition a device to a new power state, using the platform firmware and/or
740 * the device's PCI PM registers.
743 * -EINVAL if the requested state is invalid.
744 * -EIO if device does not support PCI PM or its PM capabilities register has a
745 * wrong version, or device doesn't support the requested state.
746 * 0 if device already is in the requested state.
747 * 0 if device's power state has been successfully changed.
749 int pci_set_power_state(struct pci_dev
*dev
, pci_power_t state
)
753 /* bound the state we're entering */
754 if (state
> PCI_D3cold
)
756 else if (state
< PCI_D0
)
758 else if ((state
== PCI_D1
|| state
== PCI_D2
) && pci_no_d1d2(dev
))
760 * If the device or the parent bridge do not support PCI PM,
761 * ignore the request if we're doing anything other than putting
762 * it into D0 (which would only happen on boot).
766 /* Check if we're already there */
767 if (dev
->current_state
== state
)
770 __pci_start_power_transition(dev
, state
);
772 /* This device is quirked not to be put into D3, so
773 don't put it in D3 */
774 if (state
>= PCI_D3hot
&& (dev
->dev_flags
& PCI_DEV_FLAGS_NO_D3
))
778 * To put device in D3cold, we put device into D3hot in native
779 * way, then put device into D3cold with platform ops
781 error
= pci_raw_set_power_state(dev
, state
> PCI_D3hot
?
784 if (!__pci_complete_power_transition(dev
, state
))
791 * pci_choose_state - Choose the power state of a PCI device
792 * @dev: PCI device to be suspended
793 * @state: target sleep state for the whole system. This is the value
794 * that is passed to suspend() function.
796 * Returns PCI power state suitable for given device and given system
800 pci_power_t
pci_choose_state(struct pci_dev
*dev
, pm_message_t state
)
807 ret
= platform_pci_choose_state(dev
);
808 if (ret
!= PCI_POWER_ERROR
)
811 switch (state
.event
) {
814 case PM_EVENT_FREEZE
:
815 case PM_EVENT_PRETHAW
:
816 /* REVISIT both freeze and pre-thaw "should" use D0 */
817 case PM_EVENT_SUSPEND
:
818 case PM_EVENT_HIBERNATE
:
821 dev_info(&dev
->dev
, "unrecognized suspend event %d\n",
828 EXPORT_SYMBOL(pci_choose_state
);
830 #define PCI_EXP_SAVE_REGS 7
833 static struct pci_cap_saved_state
*pci_find_saved_cap(
834 struct pci_dev
*pci_dev
, char cap
)
836 struct pci_cap_saved_state
*tmp
;
838 hlist_for_each_entry(tmp
, &pci_dev
->saved_cap_space
, next
) {
839 if (tmp
->cap
.cap_nr
== cap
)
845 static int pci_save_pcie_state(struct pci_dev
*dev
)
848 struct pci_cap_saved_state
*save_state
;
851 if (!pci_is_pcie(dev
))
854 save_state
= pci_find_saved_cap(dev
, PCI_CAP_ID_EXP
);
856 dev_err(&dev
->dev
, "buffer not found in %s\n", __func__
);
860 cap
= (u16
*)&save_state
->cap
.data
[0];
861 pcie_capability_read_word(dev
, PCI_EXP_DEVCTL
, &cap
[i
++]);
862 pcie_capability_read_word(dev
, PCI_EXP_LNKCTL
, &cap
[i
++]);
863 pcie_capability_read_word(dev
, PCI_EXP_SLTCTL
, &cap
[i
++]);
864 pcie_capability_read_word(dev
, PCI_EXP_RTCTL
, &cap
[i
++]);
865 pcie_capability_read_word(dev
, PCI_EXP_DEVCTL2
, &cap
[i
++]);
866 pcie_capability_read_word(dev
, PCI_EXP_LNKCTL2
, &cap
[i
++]);
867 pcie_capability_read_word(dev
, PCI_EXP_SLTCTL2
, &cap
[i
++]);
872 static void pci_restore_pcie_state(struct pci_dev
*dev
)
875 struct pci_cap_saved_state
*save_state
;
878 save_state
= pci_find_saved_cap(dev
, PCI_CAP_ID_EXP
);
882 cap
= (u16
*)&save_state
->cap
.data
[0];
883 pcie_capability_write_word(dev
, PCI_EXP_DEVCTL
, cap
[i
++]);
884 pcie_capability_write_word(dev
, PCI_EXP_LNKCTL
, cap
[i
++]);
885 pcie_capability_write_word(dev
, PCI_EXP_SLTCTL
, cap
[i
++]);
886 pcie_capability_write_word(dev
, PCI_EXP_RTCTL
, cap
[i
++]);
887 pcie_capability_write_word(dev
, PCI_EXP_DEVCTL2
, cap
[i
++]);
888 pcie_capability_write_word(dev
, PCI_EXP_LNKCTL2
, cap
[i
++]);
889 pcie_capability_write_word(dev
, PCI_EXP_SLTCTL2
, cap
[i
++]);
893 static int pci_save_pcix_state(struct pci_dev
*dev
)
896 struct pci_cap_saved_state
*save_state
;
898 pos
= pci_find_capability(dev
, PCI_CAP_ID_PCIX
);
902 save_state
= pci_find_saved_cap(dev
, PCI_CAP_ID_PCIX
);
904 dev_err(&dev
->dev
, "buffer not found in %s\n", __func__
);
908 pci_read_config_word(dev
, pos
+ PCI_X_CMD
,
909 (u16
*)save_state
->cap
.data
);
914 static void pci_restore_pcix_state(struct pci_dev
*dev
)
917 struct pci_cap_saved_state
*save_state
;
920 save_state
= pci_find_saved_cap(dev
, PCI_CAP_ID_PCIX
);
921 pos
= pci_find_capability(dev
, PCI_CAP_ID_PCIX
);
922 if (!save_state
|| pos
<= 0)
924 cap
= (u16
*)&save_state
->cap
.data
[0];
926 pci_write_config_word(dev
, pos
+ PCI_X_CMD
, cap
[i
++]);
931 * pci_save_state - save the PCI configuration space of a device before suspending
932 * @dev: - PCI device that we're dealing with
935 pci_save_state(struct pci_dev
*dev
)
938 /* XXX: 100% dword access ok here? */
939 for (i
= 0; i
< 16; i
++)
940 pci_read_config_dword(dev
, i
* 4, &dev
->saved_config_space
[i
]);
941 dev
->state_saved
= true;
942 if ((i
= pci_save_pcie_state(dev
)) != 0)
944 if ((i
= pci_save_pcix_state(dev
)) != 0)
949 static void pci_restore_config_dword(struct pci_dev
*pdev
, int offset
,
950 u32 saved_val
, int retry
)
954 pci_read_config_dword(pdev
, offset
, &val
);
955 if (val
== saved_val
)
959 dev_dbg(&pdev
->dev
, "restoring config space at offset "
960 "%#x (was %#x, writing %#x)\n", offset
, val
, saved_val
);
961 pci_write_config_dword(pdev
, offset
, saved_val
);
965 pci_read_config_dword(pdev
, offset
, &val
);
966 if (val
== saved_val
)
973 static void pci_restore_config_space_range(struct pci_dev
*pdev
,
974 int start
, int end
, int retry
)
978 for (index
= end
; index
>= start
; index
--)
979 pci_restore_config_dword(pdev
, 4 * index
,
980 pdev
->saved_config_space
[index
],
984 static void pci_restore_config_space(struct pci_dev
*pdev
)
986 if (pdev
->hdr_type
== PCI_HEADER_TYPE_NORMAL
) {
987 pci_restore_config_space_range(pdev
, 10, 15, 0);
988 /* Restore BARs before the command register. */
989 pci_restore_config_space_range(pdev
, 4, 9, 10);
990 pci_restore_config_space_range(pdev
, 0, 3, 0);
992 pci_restore_config_space_range(pdev
, 0, 15, 0);
997 * pci_restore_state - Restore the saved state of a PCI device
998 * @dev: - PCI device that we're dealing with
1000 void pci_restore_state(struct pci_dev
*dev
)
1002 if (!dev
->state_saved
)
1005 /* PCI Express register must be restored first */
1006 pci_restore_pcie_state(dev
);
1007 pci_restore_ats_state(dev
);
1009 pci_cleanup_aer_error_status_regs(dev
);
1011 pci_restore_config_space(dev
);
1013 pci_restore_pcix_state(dev
);
1014 pci_restore_msi_state(dev
);
1015 pci_restore_iov_state(dev
);
1017 dev
->state_saved
= false;
1020 struct pci_saved_state
{
1021 u32 config_space
[16];
1022 struct pci_cap_saved_data cap
[0];
1026 * pci_store_saved_state - Allocate and return an opaque struct containing
1027 * the device saved state.
1028 * @dev: PCI device that we're dealing with
1030 * Rerturn NULL if no state or error.
1032 struct pci_saved_state
*pci_store_saved_state(struct pci_dev
*dev
)
1034 struct pci_saved_state
*state
;
1035 struct pci_cap_saved_state
*tmp
;
1036 struct pci_cap_saved_data
*cap
;
1039 if (!dev
->state_saved
)
1042 size
= sizeof(*state
) + sizeof(struct pci_cap_saved_data
);
1044 hlist_for_each_entry(tmp
, &dev
->saved_cap_space
, next
)
1045 size
+= sizeof(struct pci_cap_saved_data
) + tmp
->cap
.size
;
1047 state
= kzalloc(size
, GFP_KERNEL
);
1051 memcpy(state
->config_space
, dev
->saved_config_space
,
1052 sizeof(state
->config_space
));
1055 hlist_for_each_entry(tmp
, &dev
->saved_cap_space
, next
) {
1056 size_t len
= sizeof(struct pci_cap_saved_data
) + tmp
->cap
.size
;
1057 memcpy(cap
, &tmp
->cap
, len
);
1058 cap
= (struct pci_cap_saved_data
*)((u8
*)cap
+ len
);
1060 /* Empty cap_save terminates list */
1064 EXPORT_SYMBOL_GPL(pci_store_saved_state
);
1067 * pci_load_saved_state - Reload the provided save state into struct pci_dev.
1068 * @dev: PCI device that we're dealing with
1069 * @state: Saved state returned from pci_store_saved_state()
1071 int pci_load_saved_state(struct pci_dev
*dev
, struct pci_saved_state
*state
)
1073 struct pci_cap_saved_data
*cap
;
1075 dev
->state_saved
= false;
1080 memcpy(dev
->saved_config_space
, state
->config_space
,
1081 sizeof(state
->config_space
));
1085 struct pci_cap_saved_state
*tmp
;
1087 tmp
= pci_find_saved_cap(dev
, cap
->cap_nr
);
1088 if (!tmp
|| tmp
->cap
.size
!= cap
->size
)
1091 memcpy(tmp
->cap
.data
, cap
->data
, tmp
->cap
.size
);
1092 cap
= (struct pci_cap_saved_data
*)((u8
*)cap
+
1093 sizeof(struct pci_cap_saved_data
) + cap
->size
);
1096 dev
->state_saved
= true;
1099 EXPORT_SYMBOL_GPL(pci_load_saved_state
);
1102 * pci_load_and_free_saved_state - Reload the save state pointed to by state,
1103 * and free the memory allocated for it.
1104 * @dev: PCI device that we're dealing with
1105 * @state: Pointer to saved state returned from pci_store_saved_state()
1107 int pci_load_and_free_saved_state(struct pci_dev
*dev
,
1108 struct pci_saved_state
**state
)
1110 int ret
= pci_load_saved_state(dev
, *state
);
1115 EXPORT_SYMBOL_GPL(pci_load_and_free_saved_state
);
1117 static int do_pci_enable_device(struct pci_dev
*dev
, int bars
)
1120 struct pci_dev
*bridge
;
1124 err
= pci_set_power_state(dev
, PCI_D0
);
1125 if (err
< 0 && err
!= -EIO
)
1128 bridge
= pci_upstream_bridge(dev
);
1130 pcie_aspm_powersave_config_link(bridge
);
1132 err
= pcibios_enable_device(dev
, bars
);
1135 pci_fixup_device(pci_fixup_enable
, dev
);
1137 if (dev
->msi_enabled
|| dev
->msix_enabled
)
1140 pci_read_config_byte(dev
, PCI_INTERRUPT_PIN
, &pin
);
1142 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
1143 if (cmd
& PCI_COMMAND_INTX_DISABLE
)
1144 pci_write_config_word(dev
, PCI_COMMAND
,
1145 cmd
& ~PCI_COMMAND_INTX_DISABLE
);
1152 * pci_reenable_device - Resume abandoned device
1153 * @dev: PCI device to be resumed
1155 * Note this function is a backend of pci_default_resume and is not supposed
1156 * to be called by normal code, write proper resume handler and use it instead.
1158 int pci_reenable_device(struct pci_dev
*dev
)
1160 if (pci_is_enabled(dev
))
1161 return do_pci_enable_device(dev
, (1 << PCI_NUM_RESOURCES
) - 1);
1165 static void pci_enable_bridge(struct pci_dev
*dev
)
1172 pci_enable_bridge(dev
->bus
->self
);
1174 if (pci_is_enabled(dev
)) {
1175 if (!dev
->is_busmaster
)
1176 pci_set_master(dev
);
1180 retval
= pci_enable_device(dev
);
1182 dev_err(&dev
->dev
, "Error enabling bridge (%d), continuing\n",
1184 pci_set_master(dev
);
1187 static int pci_enable_device_flags(struct pci_dev
*dev
, unsigned long flags
)
1193 * Power state could be unknown at this point, either due to a fresh
1194 * boot or a device removal call. So get the current power state
1195 * so that things like MSI message writing will behave as expected
1196 * (e.g. if the device really is in D0 at enable time).
1200 pci_read_config_word(dev
, dev
->pm_cap
+ PCI_PM_CTRL
, &pmcsr
);
1201 dev
->current_state
= (pmcsr
& PCI_PM_CTRL_STATE_MASK
);
1204 if (atomic_inc_return(&dev
->enable_cnt
) > 1)
1205 return 0; /* already enabled */
1207 pci_enable_bridge(dev
->bus
->self
);
1209 /* only skip sriov related */
1210 for (i
= 0; i
<= PCI_ROM_RESOURCE
; i
++)
1211 if (dev
->resource
[i
].flags
& flags
)
1213 for (i
= PCI_BRIDGE_RESOURCES
; i
< DEVICE_COUNT_RESOURCE
; i
++)
1214 if (dev
->resource
[i
].flags
& flags
)
1217 err
= do_pci_enable_device(dev
, bars
);
1219 atomic_dec(&dev
->enable_cnt
);
1224 * pci_enable_device_io - Initialize a device for use with IO space
1225 * @dev: PCI device to be initialized
1227 * Initialize device before it's used by a driver. Ask low-level code
1228 * to enable I/O resources. Wake up the device if it was suspended.
1229 * Beware, this function can fail.
1231 int pci_enable_device_io(struct pci_dev
*dev
)
1233 return pci_enable_device_flags(dev
, IORESOURCE_IO
);
1237 * pci_enable_device_mem - Initialize a device for use with Memory space
1238 * @dev: PCI device to be initialized
1240 * Initialize device before it's used by a driver. Ask low-level code
1241 * to enable Memory resources. Wake up the device if it was suspended.
1242 * Beware, this function can fail.
1244 int pci_enable_device_mem(struct pci_dev
*dev
)
1246 return pci_enable_device_flags(dev
, IORESOURCE_MEM
);
1250 * pci_enable_device - Initialize device before it's used by a driver.
1251 * @dev: PCI device to be initialized
1253 * Initialize device before it's used by a driver. Ask low-level code
1254 * to enable I/O and memory. Wake up the device if it was suspended.
1255 * Beware, this function can fail.
1257 * Note we don't actually enable the device many times if we call
1258 * this function repeatedly (we just increment the count).
1260 int pci_enable_device(struct pci_dev
*dev
)
1262 return pci_enable_device_flags(dev
, IORESOURCE_MEM
| IORESOURCE_IO
);
1266 * Managed PCI resources. This manages device on/off, intx/msi/msix
1267 * on/off and BAR regions. pci_dev itself records msi/msix status, so
1268 * there's no need to track it separately. pci_devres is initialized
1269 * when a device is enabled using managed PCI device enable interface.
1272 unsigned int enabled
:1;
1273 unsigned int pinned
:1;
1274 unsigned int orig_intx
:1;
1275 unsigned int restore_intx
:1;
1279 static void pcim_release(struct device
*gendev
, void *res
)
1281 struct pci_dev
*dev
= container_of(gendev
, struct pci_dev
, dev
);
1282 struct pci_devres
*this = res
;
1285 if (dev
->msi_enabled
)
1286 pci_disable_msi(dev
);
1287 if (dev
->msix_enabled
)
1288 pci_disable_msix(dev
);
1290 for (i
= 0; i
< DEVICE_COUNT_RESOURCE
; i
++)
1291 if (this->region_mask
& (1 << i
))
1292 pci_release_region(dev
, i
);
1294 if (this->restore_intx
)
1295 pci_intx(dev
, this->orig_intx
);
1297 if (this->enabled
&& !this->pinned
)
1298 pci_disable_device(dev
);
1301 static struct pci_devres
* get_pci_dr(struct pci_dev
*pdev
)
1303 struct pci_devres
*dr
, *new_dr
;
1305 dr
= devres_find(&pdev
->dev
, pcim_release
, NULL
, NULL
);
1309 new_dr
= devres_alloc(pcim_release
, sizeof(*new_dr
), GFP_KERNEL
);
1312 return devres_get(&pdev
->dev
, new_dr
, NULL
, NULL
);
1315 static struct pci_devres
* find_pci_dr(struct pci_dev
*pdev
)
1317 if (pci_is_managed(pdev
))
1318 return devres_find(&pdev
->dev
, pcim_release
, NULL
, NULL
);
1323 * pcim_enable_device - Managed pci_enable_device()
1324 * @pdev: PCI device to be initialized
1326 * Managed pci_enable_device().
1328 int pcim_enable_device(struct pci_dev
*pdev
)
1330 struct pci_devres
*dr
;
1333 dr
= get_pci_dr(pdev
);
1339 rc
= pci_enable_device(pdev
);
1341 pdev
->is_managed
= 1;
1348 * pcim_pin_device - Pin managed PCI device
1349 * @pdev: PCI device to pin
1351 * Pin managed PCI device @pdev. Pinned device won't be disabled on
1352 * driver detach. @pdev must have been enabled with
1353 * pcim_enable_device().
1355 void pcim_pin_device(struct pci_dev
*pdev
)
1357 struct pci_devres
*dr
;
1359 dr
= find_pci_dr(pdev
);
1360 WARN_ON(!dr
|| !dr
->enabled
);
1366 * pcibios_add_device - provide arch specific hooks when adding device dev
1367 * @dev: the PCI device being added
1369 * Permits the platform to provide architecture specific functionality when
1370 * devices are added. This is the default implementation. Architecture
1371 * implementations can override this.
1373 int __weak
pcibios_add_device (struct pci_dev
*dev
)
1379 * pcibios_release_device - provide arch specific hooks when releasing device dev
1380 * @dev: the PCI device being released
1382 * Permits the platform to provide architecture specific functionality when
1383 * devices are released. This is the default implementation. Architecture
1384 * implementations can override this.
1386 void __weak
pcibios_release_device(struct pci_dev
*dev
) {}
1389 * pcibios_disable_device - disable arch specific PCI resources for device dev
1390 * @dev: the PCI device to disable
1392 * Disables architecture specific PCI resources for the device. This
1393 * is the default implementation. Architecture implementations can
1396 void __weak
pcibios_disable_device (struct pci_dev
*dev
) {}
1398 static void do_pci_disable_device(struct pci_dev
*dev
)
1402 pci_read_config_word(dev
, PCI_COMMAND
, &pci_command
);
1403 if (pci_command
& PCI_COMMAND_MASTER
) {
1404 pci_command
&= ~PCI_COMMAND_MASTER
;
1405 pci_write_config_word(dev
, PCI_COMMAND
, pci_command
);
1408 pcibios_disable_device(dev
);
1412 * pci_disable_enabled_device - Disable device without updating enable_cnt
1413 * @dev: PCI device to disable
1415 * NOTE: This function is a backend of PCI power management routines and is
1416 * not supposed to be called drivers.
1418 void pci_disable_enabled_device(struct pci_dev
*dev
)
1420 if (pci_is_enabled(dev
))
1421 do_pci_disable_device(dev
);
1425 * pci_disable_device - Disable PCI device after use
1426 * @dev: PCI device to be disabled
1428 * Signal to the system that the PCI device is not in use by the system
1429 * anymore. This only involves disabling PCI bus-mastering, if active.
1431 * Note we don't actually disable the device until all callers of
1432 * pci_enable_device() have called pci_disable_device().
1435 pci_disable_device(struct pci_dev
*dev
)
1437 struct pci_devres
*dr
;
1439 dr
= find_pci_dr(dev
);
1443 dev_WARN_ONCE(&dev
->dev
, atomic_read(&dev
->enable_cnt
) <= 0,
1444 "disabling already-disabled device");
1446 if (atomic_dec_return(&dev
->enable_cnt
) != 0)
1449 do_pci_disable_device(dev
);
1451 dev
->is_busmaster
= 0;
1455 * pcibios_set_pcie_reset_state - set reset state for device dev
1456 * @dev: the PCIe device reset
1457 * @state: Reset state to enter into
1460 * Sets the PCIe reset state for the device. This is the default
1461 * implementation. Architecture implementations can override this.
1463 int __weak
pcibios_set_pcie_reset_state(struct pci_dev
*dev
,
1464 enum pcie_reset_state state
)
1470 * pci_set_pcie_reset_state - set reset state for device dev
1471 * @dev: the PCIe device reset
1472 * @state: Reset state to enter into
1475 * Sets the PCI reset state for the device.
1477 int pci_set_pcie_reset_state(struct pci_dev
*dev
, enum pcie_reset_state state
)
1479 return pcibios_set_pcie_reset_state(dev
, state
);
1483 * pci_check_pme_status - Check if given device has generated PME.
1484 * @dev: Device to check.
1486 * Check the PME status of the device and if set, clear it and clear PME enable
1487 * (if set). Return 'true' if PME status and PME enable were both set or
1488 * 'false' otherwise.
1490 bool pci_check_pme_status(struct pci_dev
*dev
)
1499 pmcsr_pos
= dev
->pm_cap
+ PCI_PM_CTRL
;
1500 pci_read_config_word(dev
, pmcsr_pos
, &pmcsr
);
1501 if (!(pmcsr
& PCI_PM_CTRL_PME_STATUS
))
1504 /* Clear PME status. */
1505 pmcsr
|= PCI_PM_CTRL_PME_STATUS
;
1506 if (pmcsr
& PCI_PM_CTRL_PME_ENABLE
) {
1507 /* Disable PME to avoid interrupt flood. */
1508 pmcsr
&= ~PCI_PM_CTRL_PME_ENABLE
;
1512 pci_write_config_word(dev
, pmcsr_pos
, pmcsr
);
1518 * pci_pme_wakeup - Wake up a PCI device if its PME Status bit is set.
1519 * @dev: Device to handle.
1520 * @pme_poll_reset: Whether or not to reset the device's pme_poll flag.
1522 * Check if @dev has generated PME and queue a resume request for it in that
1525 static int pci_pme_wakeup(struct pci_dev
*dev
, void *pme_poll_reset
)
1527 if (pme_poll_reset
&& dev
->pme_poll
)
1528 dev
->pme_poll
= false;
1530 if (pci_check_pme_status(dev
)) {
1531 pci_wakeup_event(dev
);
1532 pm_request_resume(&dev
->dev
);
1538 * pci_pme_wakeup_bus - Walk given bus and wake up devices on it, if necessary.
1539 * @bus: Top bus of the subtree to walk.
1541 void pci_pme_wakeup_bus(struct pci_bus
*bus
)
1544 pci_walk_bus(bus
, pci_pme_wakeup
, (void *)true);
1548 * pci_wakeup - Wake up a PCI device
1549 * @pci_dev: Device to handle.
1550 * @ign: ignored parameter
1552 static int pci_wakeup(struct pci_dev
*pci_dev
, void *ign
)
1554 pci_wakeup_event(pci_dev
);
1555 pm_request_resume(&pci_dev
->dev
);
1560 * pci_wakeup_bus - Walk given bus and wake up devices on it
1561 * @bus: Top bus of the subtree to walk.
1563 void pci_wakeup_bus(struct pci_bus
*bus
)
1566 pci_walk_bus(bus
, pci_wakeup
, NULL
);
1570 * pci_pme_capable - check the capability of PCI device to generate PME#
1571 * @dev: PCI device to handle.
1572 * @state: PCI state from which device will issue PME#.
1574 bool pci_pme_capable(struct pci_dev
*dev
, pci_power_t state
)
1579 return !!(dev
->pme_support
& (1 << state
));
1582 static void pci_pme_list_scan(struct work_struct
*work
)
1584 struct pci_pme_device
*pme_dev
, *n
;
1586 mutex_lock(&pci_pme_list_mutex
);
1587 if (!list_empty(&pci_pme_list
)) {
1588 list_for_each_entry_safe(pme_dev
, n
, &pci_pme_list
, list
) {
1589 if (pme_dev
->dev
->pme_poll
) {
1590 struct pci_dev
*bridge
;
1592 bridge
= pme_dev
->dev
->bus
->self
;
1594 * If bridge is in low power state, the
1595 * configuration space of subordinate devices
1596 * may be not accessible
1598 if (bridge
&& bridge
->current_state
!= PCI_D0
)
1600 pci_pme_wakeup(pme_dev
->dev
, NULL
);
1602 list_del(&pme_dev
->list
);
1606 if (!list_empty(&pci_pme_list
))
1607 schedule_delayed_work(&pci_pme_work
,
1608 msecs_to_jiffies(PME_TIMEOUT
));
1610 mutex_unlock(&pci_pme_list_mutex
);
1614 * pci_pme_active - enable or disable PCI device's PME# function
1615 * @dev: PCI device to handle.
1616 * @enable: 'true' to enable PME# generation; 'false' to disable it.
1618 * The caller must verify that the device is capable of generating PME# before
1619 * calling this function with @enable equal to 'true'.
1621 void pci_pme_active(struct pci_dev
*dev
, bool enable
)
1625 if (!dev
->pme_support
)
1628 pci_read_config_word(dev
, dev
->pm_cap
+ PCI_PM_CTRL
, &pmcsr
);
1629 /* Clear PME_Status by writing 1 to it and enable PME# */
1630 pmcsr
|= PCI_PM_CTRL_PME_STATUS
| PCI_PM_CTRL_PME_ENABLE
;
1632 pmcsr
&= ~PCI_PM_CTRL_PME_ENABLE
;
1634 pci_write_config_word(dev
, dev
->pm_cap
+ PCI_PM_CTRL
, pmcsr
);
1637 * PCI (as opposed to PCIe) PME requires that the device have
1638 * its PME# line hooked up correctly. Not all hardware vendors
1639 * do this, so the PME never gets delivered and the device
1640 * remains asleep. The easiest way around this is to
1641 * periodically walk the list of suspended devices and check
1642 * whether any have their PME flag set. The assumption is that
1643 * we'll wake up often enough anyway that this won't be a huge
1644 * hit, and the power savings from the devices will still be a
1647 * Although PCIe uses in-band PME message instead of PME# line
1648 * to report PME, PME does not work for some PCIe devices in
1649 * reality. For example, there are devices that set their PME
1650 * status bits, but don't really bother to send a PME message;
1651 * there are PCI Express Root Ports that don't bother to
1652 * trigger interrupts when they receive PME messages from the
1653 * devices below. So PME poll is used for PCIe devices too.
1656 if (dev
->pme_poll
) {
1657 struct pci_pme_device
*pme_dev
;
1659 pme_dev
= kmalloc(sizeof(struct pci_pme_device
),
1664 mutex_lock(&pci_pme_list_mutex
);
1665 list_add(&pme_dev
->list
, &pci_pme_list
);
1666 if (list_is_singular(&pci_pme_list
))
1667 schedule_delayed_work(&pci_pme_work
,
1668 msecs_to_jiffies(PME_TIMEOUT
));
1669 mutex_unlock(&pci_pme_list_mutex
);
1671 mutex_lock(&pci_pme_list_mutex
);
1672 list_for_each_entry(pme_dev
, &pci_pme_list
, list
) {
1673 if (pme_dev
->dev
== dev
) {
1674 list_del(&pme_dev
->list
);
1679 mutex_unlock(&pci_pme_list_mutex
);
1684 dev_dbg(&dev
->dev
, "PME# %s\n", enable
? "enabled" : "disabled");
1688 * __pci_enable_wake - enable PCI device as wakeup event source
1689 * @dev: PCI device affected
1690 * @state: PCI state from which device will issue wakeup events
1691 * @runtime: True if the events are to be generated at run time
1692 * @enable: True to enable event generation; false to disable
1694 * This enables the device as a wakeup event source, or disables it.
1695 * When such events involves platform-specific hooks, those hooks are
1696 * called automatically by this routine.
1698 * Devices with legacy power management (no standard PCI PM capabilities)
1699 * always require such platform hooks.
1702 * 0 is returned on success
1703 * -EINVAL is returned if device is not supposed to wake up the system
1704 * Error code depending on the platform is returned if both the platform and
1705 * the native mechanism fail to enable the generation of wake-up events
1707 int __pci_enable_wake(struct pci_dev
*dev
, pci_power_t state
,
1708 bool runtime
, bool enable
)
1712 if (enable
&& !runtime
&& !device_may_wakeup(&dev
->dev
))
1715 /* Don't do the same thing twice in a row for one device. */
1716 if (!!enable
== !!dev
->wakeup_prepared
)
1720 * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
1721 * Anderson we should be doing PME# wake enable followed by ACPI wake
1722 * enable. To disable wake-up we call the platform first, for symmetry.
1728 if (pci_pme_capable(dev
, state
))
1729 pci_pme_active(dev
, true);
1732 error
= runtime
? platform_pci_run_wake(dev
, true) :
1733 platform_pci_sleep_wake(dev
, true);
1737 dev
->wakeup_prepared
= true;
1740 platform_pci_run_wake(dev
, false);
1742 platform_pci_sleep_wake(dev
, false);
1743 pci_pme_active(dev
, false);
1744 dev
->wakeup_prepared
= false;
1749 EXPORT_SYMBOL(__pci_enable_wake
);
1752 * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold
1753 * @dev: PCI device to prepare
1754 * @enable: True to enable wake-up event generation; false to disable
1756 * Many drivers want the device to wake up the system from D3_hot or D3_cold
1757 * and this function allows them to set that up cleanly - pci_enable_wake()
1758 * should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
1759 * ordering constraints.
1761 * This function only returns error code if the device is not capable of
1762 * generating PME# from both D3_hot and D3_cold, and the platform is unable to
1763 * enable wake-up power for it.
1765 int pci_wake_from_d3(struct pci_dev
*dev
, bool enable
)
1767 return pci_pme_capable(dev
, PCI_D3cold
) ?
1768 pci_enable_wake(dev
, PCI_D3cold
, enable
) :
1769 pci_enable_wake(dev
, PCI_D3hot
, enable
);
1773 * pci_target_state - find an appropriate low power state for a given PCI dev
1776 * Use underlying platform code to find a supported low power state for @dev.
1777 * If the platform can't manage @dev, return the deepest state from which it
1778 * can generate wake events, based on any available PME info.
1780 pci_power_t
pci_target_state(struct pci_dev
*dev
)
1782 pci_power_t target_state
= PCI_D3hot
;
1784 if (platform_pci_power_manageable(dev
)) {
1786 * Call the platform to choose the target state of the device
1787 * and enable wake-up from this state if supported.
1789 pci_power_t state
= platform_pci_choose_state(dev
);
1792 case PCI_POWER_ERROR
:
1797 if (pci_no_d1d2(dev
))
1800 target_state
= state
;
1802 } else if (!dev
->pm_cap
) {
1803 target_state
= PCI_D0
;
1804 } else if (device_may_wakeup(&dev
->dev
)) {
1806 * Find the deepest state from which the device can generate
1807 * wake-up events, make it the target state and enable device
1810 if (dev
->pme_support
) {
1812 && !(dev
->pme_support
& (1 << target_state
)))
1817 return target_state
;
1821 * pci_prepare_to_sleep - prepare PCI device for system-wide transition into a sleep state
1822 * @dev: Device to handle.
1824 * Choose the power state appropriate for the device depending on whether
1825 * it can wake up the system and/or is power manageable by the platform
1826 * (PCI_D3hot is the default) and put the device into that state.
1828 int pci_prepare_to_sleep(struct pci_dev
*dev
)
1830 pci_power_t target_state
= pci_target_state(dev
);
1833 if (target_state
== PCI_POWER_ERROR
)
1836 /* D3cold during system suspend/hibernate is not supported */
1837 if (target_state
> PCI_D3hot
)
1838 target_state
= PCI_D3hot
;
1840 pci_enable_wake(dev
, target_state
, device_may_wakeup(&dev
->dev
));
1842 error
= pci_set_power_state(dev
, target_state
);
1845 pci_enable_wake(dev
, target_state
, false);
1851 * pci_back_from_sleep - turn PCI device on during system-wide transition into working state
1852 * @dev: Device to handle.
1854 * Disable device's system wake-up capability and put it into D0.
1856 int pci_back_from_sleep(struct pci_dev
*dev
)
1858 pci_enable_wake(dev
, PCI_D0
, false);
1859 return pci_set_power_state(dev
, PCI_D0
);
1863 * pci_finish_runtime_suspend - Carry out PCI-specific part of runtime suspend.
1864 * @dev: PCI device being suspended.
1866 * Prepare @dev to generate wake-up events at run time and put it into a low
1869 int pci_finish_runtime_suspend(struct pci_dev
*dev
)
1871 pci_power_t target_state
= pci_target_state(dev
);
1874 if (target_state
== PCI_POWER_ERROR
)
1877 dev
->runtime_d3cold
= target_state
== PCI_D3cold
;
1879 __pci_enable_wake(dev
, target_state
, true, pci_dev_run_wake(dev
));
1881 error
= pci_set_power_state(dev
, target_state
);
1884 __pci_enable_wake(dev
, target_state
, true, false);
1885 dev
->runtime_d3cold
= false;
1892 * pci_dev_run_wake - Check if device can generate run-time wake-up events.
1893 * @dev: Device to check.
1895 * Return true if the device itself is cabable of generating wake-up events
1896 * (through the platform or using the native PCIe PME) or if the device supports
1897 * PME and one of its upstream bridges can generate wake-up events.
1899 bool pci_dev_run_wake(struct pci_dev
*dev
)
1901 struct pci_bus
*bus
= dev
->bus
;
1903 if (device_run_wake(&dev
->dev
))
1906 if (!dev
->pme_support
)
1909 while (bus
->parent
) {
1910 struct pci_dev
*bridge
= bus
->self
;
1912 if (device_run_wake(&bridge
->dev
))
1918 /* We have reached the root bus. */
1920 return device_run_wake(bus
->bridge
);
1924 EXPORT_SYMBOL_GPL(pci_dev_run_wake
);
1926 void pci_config_pm_runtime_get(struct pci_dev
*pdev
)
1928 struct device
*dev
= &pdev
->dev
;
1929 struct device
*parent
= dev
->parent
;
1932 pm_runtime_get_sync(parent
);
1933 pm_runtime_get_noresume(dev
);
1935 * pdev->current_state is set to PCI_D3cold during suspending,
1936 * so wait until suspending completes
1938 pm_runtime_barrier(dev
);
1940 * Only need to resume devices in D3cold, because config
1941 * registers are still accessible for devices suspended but
1944 if (pdev
->current_state
== PCI_D3cold
)
1945 pm_runtime_resume(dev
);
1948 void pci_config_pm_runtime_put(struct pci_dev
*pdev
)
1950 struct device
*dev
= &pdev
->dev
;
1951 struct device
*parent
= dev
->parent
;
1953 pm_runtime_put(dev
);
1955 pm_runtime_put_sync(parent
);
1959 * pci_pm_init - Initialize PM functions of given PCI device
1960 * @dev: PCI device to handle.
1962 void pci_pm_init(struct pci_dev
*dev
)
1967 pm_runtime_forbid(&dev
->dev
);
1968 pm_runtime_set_active(&dev
->dev
);
1969 pm_runtime_enable(&dev
->dev
);
1970 device_enable_async_suspend(&dev
->dev
);
1971 dev
->wakeup_prepared
= false;
1974 dev
->pme_support
= 0;
1976 /* find PCI PM capability in list */
1977 pm
= pci_find_capability(dev
, PCI_CAP_ID_PM
);
1980 /* Check device's ability to generate PME# */
1981 pci_read_config_word(dev
, pm
+ PCI_PM_PMC
, &pmc
);
1983 if ((pmc
& PCI_PM_CAP_VER_MASK
) > 3) {
1984 dev_err(&dev
->dev
, "unsupported PM cap regs version (%u)\n",
1985 pmc
& PCI_PM_CAP_VER_MASK
);
1990 dev
->d3_delay
= PCI_PM_D3_WAIT
;
1991 dev
->d3cold_delay
= PCI_PM_D3COLD_WAIT
;
1992 dev
->d3cold_allowed
= true;
1994 dev
->d1_support
= false;
1995 dev
->d2_support
= false;
1996 if (!pci_no_d1d2(dev
)) {
1997 if (pmc
& PCI_PM_CAP_D1
)
1998 dev
->d1_support
= true;
1999 if (pmc
& PCI_PM_CAP_D2
)
2000 dev
->d2_support
= true;
2002 if (dev
->d1_support
|| dev
->d2_support
)
2003 dev_printk(KERN_DEBUG
, &dev
->dev
, "supports%s%s\n",
2004 dev
->d1_support
? " D1" : "",
2005 dev
->d2_support
? " D2" : "");
2008 pmc
&= PCI_PM_CAP_PME_MASK
;
2010 dev_printk(KERN_DEBUG
, &dev
->dev
,
2011 "PME# supported from%s%s%s%s%s\n",
2012 (pmc
& PCI_PM_CAP_PME_D0
) ? " D0" : "",
2013 (pmc
& PCI_PM_CAP_PME_D1
) ? " D1" : "",
2014 (pmc
& PCI_PM_CAP_PME_D2
) ? " D2" : "",
2015 (pmc
& PCI_PM_CAP_PME_D3
) ? " D3hot" : "",
2016 (pmc
& PCI_PM_CAP_PME_D3cold
) ? " D3cold" : "");
2017 dev
->pme_support
= pmc
>> PCI_PM_CAP_PME_SHIFT
;
2018 dev
->pme_poll
= true;
2020 * Make device's PM flags reflect the wake-up capability, but
2021 * let the user space enable it to wake up the system as needed.
2023 device_set_wakeup_capable(&dev
->dev
, true);
2024 /* Disable the PME# generation functionality */
2025 pci_pme_active(dev
, false);
2029 static void pci_add_saved_cap(struct pci_dev
*pci_dev
,
2030 struct pci_cap_saved_state
*new_cap
)
2032 hlist_add_head(&new_cap
->next
, &pci_dev
->saved_cap_space
);
2036 * pci_add_cap_save_buffer - allocate buffer for saving given capability registers
2037 * @dev: the PCI device
2038 * @cap: the capability to allocate the buffer for
2039 * @size: requested size of the buffer
2041 static int pci_add_cap_save_buffer(
2042 struct pci_dev
*dev
, char cap
, unsigned int size
)
2045 struct pci_cap_saved_state
*save_state
;
2047 pos
= pci_find_capability(dev
, cap
);
2051 save_state
= kzalloc(sizeof(*save_state
) + size
, GFP_KERNEL
);
2055 save_state
->cap
.cap_nr
= cap
;
2056 save_state
->cap
.size
= size
;
2057 pci_add_saved_cap(dev
, save_state
);
2063 * pci_allocate_cap_save_buffers - allocate buffers for saving capabilities
2064 * @dev: the PCI device
2066 void pci_allocate_cap_save_buffers(struct pci_dev
*dev
)
2070 error
= pci_add_cap_save_buffer(dev
, PCI_CAP_ID_EXP
,
2071 PCI_EXP_SAVE_REGS
* sizeof(u16
));
2074 "unable to preallocate PCI Express save buffer\n");
2076 error
= pci_add_cap_save_buffer(dev
, PCI_CAP_ID_PCIX
, sizeof(u16
));
2079 "unable to preallocate PCI-X save buffer\n");
2082 void pci_free_cap_save_buffers(struct pci_dev
*dev
)
2084 struct pci_cap_saved_state
*tmp
;
2085 struct hlist_node
*n
;
2087 hlist_for_each_entry_safe(tmp
, n
, &dev
->saved_cap_space
, next
)
2092 * pci_configure_ari - enable or disable ARI forwarding
2093 * @dev: the PCI device
2095 * If @dev and its upstream bridge both support ARI, enable ARI in the
2096 * bridge. Otherwise, disable ARI in the bridge.
2098 void pci_configure_ari(struct pci_dev
*dev
)
2101 struct pci_dev
*bridge
;
2103 if (pcie_ari_disabled
|| !pci_is_pcie(dev
) || dev
->devfn
)
2106 bridge
= dev
->bus
->self
;
2110 pcie_capability_read_dword(bridge
, PCI_EXP_DEVCAP2
, &cap
);
2111 if (!(cap
& PCI_EXP_DEVCAP2_ARI
))
2114 if (pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ARI
)) {
2115 pcie_capability_set_word(bridge
, PCI_EXP_DEVCTL2
,
2116 PCI_EXP_DEVCTL2_ARI
);
2117 bridge
->ari_enabled
= 1;
2119 pcie_capability_clear_word(bridge
, PCI_EXP_DEVCTL2
,
2120 PCI_EXP_DEVCTL2_ARI
);
2121 bridge
->ari_enabled
= 0;
2126 * pci_enable_ido - enable ID-based Ordering on a device
2127 * @dev: the PCI device
2128 * @type: which types of IDO to enable
2130 * Enable ID-based ordering on @dev. @type can contain the bits
2131 * %PCI_EXP_IDO_REQUEST and/or %PCI_EXP_IDO_COMPLETION to indicate
2132 * which types of transactions are allowed to be re-ordered.
2134 void pci_enable_ido(struct pci_dev
*dev
, unsigned long type
)
2138 if (type
& PCI_EXP_IDO_REQUEST
)
2139 ctrl
|= PCI_EXP_DEVCTL2_IDO_REQ_EN
;
2140 if (type
& PCI_EXP_IDO_COMPLETION
)
2141 ctrl
|= PCI_EXP_DEVCTL2_IDO_CMP_EN
;
2143 pcie_capability_set_word(dev
, PCI_EXP_DEVCTL2
, ctrl
);
2145 EXPORT_SYMBOL(pci_enable_ido
);
2148 * pci_disable_ido - disable ID-based ordering on a device
2149 * @dev: the PCI device
2150 * @type: which types of IDO to disable
2152 void pci_disable_ido(struct pci_dev
*dev
, unsigned long type
)
2156 if (type
& PCI_EXP_IDO_REQUEST
)
2157 ctrl
|= PCI_EXP_DEVCTL2_IDO_REQ_EN
;
2158 if (type
& PCI_EXP_IDO_COMPLETION
)
2159 ctrl
|= PCI_EXP_DEVCTL2_IDO_CMP_EN
;
2161 pcie_capability_clear_word(dev
, PCI_EXP_DEVCTL2
, ctrl
);
2163 EXPORT_SYMBOL(pci_disable_ido
);
2166 * pci_enable_obff - enable optimized buffer flush/fill
2168 * @type: type of signaling to use
2170 * Try to enable @type OBFF signaling on @dev. It will try using WAKE#
2171 * signaling if possible, falling back to message signaling only if
2172 * WAKE# isn't supported. @type should indicate whether the PCIe link
2173 * be brought out of L0s or L1 to send the message. It should be either
2174 * %PCI_EXP_OBFF_SIGNAL_ALWAYS or %PCI_OBFF_SIGNAL_L0.
2176 * If your device can benefit from receiving all messages, even at the
2177 * power cost of bringing the link back up from a low power state, use
2178 * %PCI_EXP_OBFF_SIGNAL_ALWAYS. Otherwise, use %PCI_OBFF_SIGNAL_L0 (the
2182 * Zero on success, appropriate error number on failure.
2184 int pci_enable_obff(struct pci_dev
*dev
, enum pci_obff_signal_type type
)
2190 pcie_capability_read_dword(dev
, PCI_EXP_DEVCAP2
, &cap
);
2191 if (!(cap
& PCI_EXP_DEVCAP2_OBFF_MASK
))
2192 return -ENOTSUPP
; /* no OBFF support at all */
2194 /* Make sure the topology supports OBFF as well */
2195 if (dev
->bus
->self
) {
2196 ret
= pci_enable_obff(dev
->bus
->self
, type
);
2201 pcie_capability_read_word(dev
, PCI_EXP_DEVCTL2
, &ctrl
);
2202 if (cap
& PCI_EXP_DEVCAP2_OBFF_WAKE
)
2203 ctrl
|= PCI_EXP_DEVCTL2_OBFF_WAKE_EN
;
2206 case PCI_EXP_OBFF_SIGNAL_L0
:
2207 if (!(ctrl
& PCI_EXP_DEVCTL2_OBFF_WAKE_EN
))
2208 ctrl
|= PCI_EXP_DEVCTL2_OBFF_MSGA_EN
;
2210 case PCI_EXP_OBFF_SIGNAL_ALWAYS
:
2211 ctrl
&= ~PCI_EXP_DEVCTL2_OBFF_WAKE_EN
;
2212 ctrl
|= PCI_EXP_DEVCTL2_OBFF_MSGB_EN
;
2215 WARN(1, "bad OBFF signal type\n");
2219 pcie_capability_write_word(dev
, PCI_EXP_DEVCTL2
, ctrl
);
2223 EXPORT_SYMBOL(pci_enable_obff
);
2226 * pci_disable_obff - disable optimized buffer flush/fill
2229 * Disable OBFF on @dev.
2231 void pci_disable_obff(struct pci_dev
*dev
)
2233 pcie_capability_clear_word(dev
, PCI_EXP_DEVCTL2
,
2234 PCI_EXP_DEVCTL2_OBFF_WAKE_EN
);
2236 EXPORT_SYMBOL(pci_disable_obff
);
2239 * pci_ltr_supported - check whether a device supports LTR
2243 * True if @dev supports latency tolerance reporting, false otherwise.
2245 static bool pci_ltr_supported(struct pci_dev
*dev
)
2249 pcie_capability_read_dword(dev
, PCI_EXP_DEVCAP2
, &cap
);
2251 return cap
& PCI_EXP_DEVCAP2_LTR
;
2255 * pci_enable_ltr - enable latency tolerance reporting
2258 * Enable LTR on @dev if possible, which means enabling it first on
2262 * Zero on success, errno on failure.
2264 int pci_enable_ltr(struct pci_dev
*dev
)
2268 /* Only primary function can enable/disable LTR */
2269 if (PCI_FUNC(dev
->devfn
) != 0)
2272 if (!pci_ltr_supported(dev
))
2275 /* Enable upstream ports first */
2276 if (dev
->bus
->self
) {
2277 ret
= pci_enable_ltr(dev
->bus
->self
);
2282 return pcie_capability_set_word(dev
, PCI_EXP_DEVCTL2
,
2283 PCI_EXP_DEVCTL2_LTR_EN
);
2285 EXPORT_SYMBOL(pci_enable_ltr
);
2288 * pci_disable_ltr - disable latency tolerance reporting
2291 void pci_disable_ltr(struct pci_dev
*dev
)
2293 /* Only primary function can enable/disable LTR */
2294 if (PCI_FUNC(dev
->devfn
) != 0)
2297 if (!pci_ltr_supported(dev
))
2300 pcie_capability_clear_word(dev
, PCI_EXP_DEVCTL2
,
2301 PCI_EXP_DEVCTL2_LTR_EN
);
2303 EXPORT_SYMBOL(pci_disable_ltr
);
2305 static int __pci_ltr_scale(int *val
)
2309 while (*val
> 1023) {
2310 *val
= (*val
+ 31) / 32;
2317 * pci_set_ltr - set LTR latency values
2319 * @snoop_lat_ns: snoop latency in nanoseconds
2320 * @nosnoop_lat_ns: nosnoop latency in nanoseconds
2322 * Figure out the scale and set the LTR values accordingly.
2324 int pci_set_ltr(struct pci_dev
*dev
, int snoop_lat_ns
, int nosnoop_lat_ns
)
2326 int pos
, ret
, snoop_scale
, nosnoop_scale
;
2329 if (!pci_ltr_supported(dev
))
2332 snoop_scale
= __pci_ltr_scale(&snoop_lat_ns
);
2333 nosnoop_scale
= __pci_ltr_scale(&nosnoop_lat_ns
);
2335 if (snoop_lat_ns
> PCI_LTR_VALUE_MASK
||
2336 nosnoop_lat_ns
> PCI_LTR_VALUE_MASK
)
2339 if ((snoop_scale
> (PCI_LTR_SCALE_MASK
>> PCI_LTR_SCALE_SHIFT
)) ||
2340 (nosnoop_scale
> (PCI_LTR_SCALE_MASK
>> PCI_LTR_SCALE_SHIFT
)))
2343 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_LTR
);
2347 val
= (snoop_scale
<< PCI_LTR_SCALE_SHIFT
) | snoop_lat_ns
;
2348 ret
= pci_write_config_word(dev
, pos
+ PCI_LTR_MAX_SNOOP_LAT
, val
);
2352 val
= (nosnoop_scale
<< PCI_LTR_SCALE_SHIFT
) | nosnoop_lat_ns
;
2353 ret
= pci_write_config_word(dev
, pos
+ PCI_LTR_MAX_NOSNOOP_LAT
, val
);
2359 EXPORT_SYMBOL(pci_set_ltr
);
2361 static int pci_acs_enable
;
2364 * pci_request_acs - ask for ACS to be enabled if supported
2366 void pci_request_acs(void)
2372 * pci_enable_acs - enable ACS if hardware support it
2373 * @dev: the PCI device
2375 void pci_enable_acs(struct pci_dev
*dev
)
2381 if (!pci_acs_enable
)
2384 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ACS
);
2388 pci_read_config_word(dev
, pos
+ PCI_ACS_CAP
, &cap
);
2389 pci_read_config_word(dev
, pos
+ PCI_ACS_CTRL
, &ctrl
);
2391 /* Source Validation */
2392 ctrl
|= (cap
& PCI_ACS_SV
);
2394 /* P2P Request Redirect */
2395 ctrl
|= (cap
& PCI_ACS_RR
);
2397 /* P2P Completion Redirect */
2398 ctrl
|= (cap
& PCI_ACS_CR
);
2400 /* Upstream Forwarding */
2401 ctrl
|= (cap
& PCI_ACS_UF
);
2403 pci_write_config_word(dev
, pos
+ PCI_ACS_CTRL
, ctrl
);
2406 static bool pci_acs_flags_enabled(struct pci_dev
*pdev
, u16 acs_flags
)
2411 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_ACS
);
2416 * Except for egress control, capabilities are either required
2417 * or only required if controllable. Features missing from the
2418 * capability field can therefore be assumed as hard-wired enabled.
2420 pci_read_config_word(pdev
, pos
+ PCI_ACS_CAP
, &cap
);
2421 acs_flags
&= (cap
| PCI_ACS_EC
);
2423 pci_read_config_word(pdev
, pos
+ PCI_ACS_CTRL
, &ctrl
);
2424 return (ctrl
& acs_flags
) == acs_flags
;
2428 * pci_acs_enabled - test ACS against required flags for a given device
2429 * @pdev: device to test
2430 * @acs_flags: required PCI ACS flags
2432 * Return true if the device supports the provided flags. Automatically
2433 * filters out flags that are not implemented on multifunction devices.
2435 * Note that this interface checks the effective ACS capabilities of the
2436 * device rather than the actual capabilities. For instance, most single
2437 * function endpoints are not required to support ACS because they have no
2438 * opportunity for peer-to-peer access. We therefore return 'true'
2439 * regardless of whether the device exposes an ACS capability. This makes
2440 * it much easier for callers of this function to ignore the actual type
2441 * or topology of the device when testing ACS support.
2443 bool pci_acs_enabled(struct pci_dev
*pdev
, u16 acs_flags
)
2447 ret
= pci_dev_specific_acs_enabled(pdev
, acs_flags
);
2452 * Conventional PCI and PCI-X devices never support ACS, either
2453 * effectively or actually. The shared bus topology implies that
2454 * any device on the bus can receive or snoop DMA.
2456 if (!pci_is_pcie(pdev
))
2459 switch (pci_pcie_type(pdev
)) {
2461 * PCI/X-to-PCIe bridges are not specifically mentioned by the spec,
2462 * but since their primary inteface is PCI/X, we conservatively
2463 * handle them as we would a non-PCIe device.
2465 case PCI_EXP_TYPE_PCIE_BRIDGE
:
2467 * PCIe 3.0, 6.12.1 excludes ACS on these devices. "ACS is never
2468 * applicable... must never implement an ACS Extended Capability...".
2469 * This seems arbitrary, but we take a conservative interpretation
2470 * of this statement.
2472 case PCI_EXP_TYPE_PCI_BRIDGE
:
2473 case PCI_EXP_TYPE_RC_EC
:
2476 * PCIe 3.0, 6.12.1.1 specifies that downstream and root ports should
2477 * implement ACS in order to indicate their peer-to-peer capabilities,
2478 * regardless of whether they are single- or multi-function devices.
2480 case PCI_EXP_TYPE_DOWNSTREAM
:
2481 case PCI_EXP_TYPE_ROOT_PORT
:
2482 return pci_acs_flags_enabled(pdev
, acs_flags
);
2484 * PCIe 3.0, 6.12.1.2 specifies ACS capabilities that should be
2485 * implemented by the remaining PCIe types to indicate peer-to-peer
2486 * capabilities, but only when they are part of a multifunciton
2487 * device. The footnote for section 6.12 indicates the specific
2488 * PCIe types included here.
2490 case PCI_EXP_TYPE_ENDPOINT
:
2491 case PCI_EXP_TYPE_UPSTREAM
:
2492 case PCI_EXP_TYPE_LEG_END
:
2493 case PCI_EXP_TYPE_RC_END
:
2494 if (!pdev
->multifunction
)
2497 return pci_acs_flags_enabled(pdev
, acs_flags
);
2501 * PCIe 3.0, 6.12.1.3 specifies no ACS capabilties are applicable
2502 * to single function devices with the exception of downstream ports.
2508 * pci_acs_path_enable - test ACS flags from start to end in a hierarchy
2509 * @start: starting downstream device
2510 * @end: ending upstream device or NULL to search to the root bus
2511 * @acs_flags: required flags
2513 * Walk up a device tree from start to end testing PCI ACS support. If
2514 * any step along the way does not support the required flags, return false.
2516 bool pci_acs_path_enabled(struct pci_dev
*start
,
2517 struct pci_dev
*end
, u16 acs_flags
)
2519 struct pci_dev
*pdev
, *parent
= start
;
2524 if (!pci_acs_enabled(pdev
, acs_flags
))
2527 if (pci_is_root_bus(pdev
->bus
))
2528 return (end
== NULL
);
2530 parent
= pdev
->bus
->self
;
2531 } while (pdev
!= end
);
2537 * pci_swizzle_interrupt_pin - swizzle INTx for device behind bridge
2538 * @dev: the PCI device
2539 * @pin: the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD)
2541 * Perform INTx swizzling for a device behind one level of bridge. This is
2542 * required by section 9.1 of the PCI-to-PCI bridge specification for devices
2543 * behind bridges on add-in cards. For devices with ARI enabled, the slot
2544 * number is always 0 (see the Implementation Note in section 2.2.8.1 of
2545 * the PCI Express Base Specification, Revision 2.1)
2547 u8
pci_swizzle_interrupt_pin(const struct pci_dev
*dev
, u8 pin
)
2551 if (pci_ari_enabled(dev
->bus
))
2554 slot
= PCI_SLOT(dev
->devfn
);
2556 return (((pin
- 1) + slot
) % 4) + 1;
2560 pci_get_interrupt_pin(struct pci_dev
*dev
, struct pci_dev
**bridge
)
2568 while (!pci_is_root_bus(dev
->bus
)) {
2569 pin
= pci_swizzle_interrupt_pin(dev
, pin
);
2570 dev
= dev
->bus
->self
;
2577 * pci_common_swizzle - swizzle INTx all the way to root bridge
2578 * @dev: the PCI device
2579 * @pinp: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)
2581 * Perform INTx swizzling for a device. This traverses through all PCI-to-PCI
2582 * bridges all the way up to a PCI root bus.
2584 u8
pci_common_swizzle(struct pci_dev
*dev
, u8
*pinp
)
2588 while (!pci_is_root_bus(dev
->bus
)) {
2589 pin
= pci_swizzle_interrupt_pin(dev
, pin
);
2590 dev
= dev
->bus
->self
;
2593 return PCI_SLOT(dev
->devfn
);
2597 * pci_release_region - Release a PCI bar
2598 * @pdev: PCI device whose resources were previously reserved by pci_request_region
2599 * @bar: BAR to release
2601 * Releases the PCI I/O and memory resources previously reserved by a
2602 * successful call to pci_request_region. Call this function only
2603 * after all use of the PCI regions has ceased.
2605 void pci_release_region(struct pci_dev
*pdev
, int bar
)
2607 struct pci_devres
*dr
;
2609 if (pci_resource_len(pdev
, bar
) == 0)
2611 if (pci_resource_flags(pdev
, bar
) & IORESOURCE_IO
)
2612 release_region(pci_resource_start(pdev
, bar
),
2613 pci_resource_len(pdev
, bar
));
2614 else if (pci_resource_flags(pdev
, bar
) & IORESOURCE_MEM
)
2615 release_mem_region(pci_resource_start(pdev
, bar
),
2616 pci_resource_len(pdev
, bar
));
2618 dr
= find_pci_dr(pdev
);
2620 dr
->region_mask
&= ~(1 << bar
);
2624 * __pci_request_region - Reserved PCI I/O and memory resource
2625 * @pdev: PCI device whose resources are to be reserved
2626 * @bar: BAR to be reserved
2627 * @res_name: Name to be associated with resource.
2628 * @exclusive: whether the region access is exclusive or not
2630 * Mark the PCI region associated with PCI device @pdev BR @bar as
2631 * being reserved by owner @res_name. Do not access any
2632 * address inside the PCI regions unless this call returns
2635 * If @exclusive is set, then the region is marked so that userspace
2636 * is explicitly not allowed to map the resource via /dev/mem or
2637 * sysfs MMIO access.
2639 * Returns 0 on success, or %EBUSY on error. A warning
2640 * message is also printed on failure.
2642 static int __pci_request_region(struct pci_dev
*pdev
, int bar
, const char *res_name
,
2645 struct pci_devres
*dr
;
2647 if (pci_resource_len(pdev
, bar
) == 0)
2650 if (pci_resource_flags(pdev
, bar
) & IORESOURCE_IO
) {
2651 if (!request_region(pci_resource_start(pdev
, bar
),
2652 pci_resource_len(pdev
, bar
), res_name
))
2655 else if (pci_resource_flags(pdev
, bar
) & IORESOURCE_MEM
) {
2656 if (!__request_mem_region(pci_resource_start(pdev
, bar
),
2657 pci_resource_len(pdev
, bar
), res_name
,
2662 dr
= find_pci_dr(pdev
);
2664 dr
->region_mask
|= 1 << bar
;
2669 dev_warn(&pdev
->dev
, "BAR %d: can't reserve %pR\n", bar
,
2670 &pdev
->resource
[bar
]);
2675 * pci_request_region - Reserve PCI I/O and memory resource
2676 * @pdev: PCI device whose resources are to be reserved
2677 * @bar: BAR to be reserved
2678 * @res_name: Name to be associated with resource
2680 * Mark the PCI region associated with PCI device @pdev BAR @bar as
2681 * being reserved by owner @res_name. Do not access any
2682 * address inside the PCI regions unless this call returns
2685 * Returns 0 on success, or %EBUSY on error. A warning
2686 * message is also printed on failure.
2688 int pci_request_region(struct pci_dev
*pdev
, int bar
, const char *res_name
)
2690 return __pci_request_region(pdev
, bar
, res_name
, 0);
2694 * pci_request_region_exclusive - Reserved PCI I/O and memory resource
2695 * @pdev: PCI device whose resources are to be reserved
2696 * @bar: BAR to be reserved
2697 * @res_name: Name to be associated with resource.
2699 * Mark the PCI region associated with PCI device @pdev BR @bar as
2700 * being reserved by owner @res_name. Do not access any
2701 * address inside the PCI regions unless this call returns
2704 * Returns 0 on success, or %EBUSY on error. A warning
2705 * message is also printed on failure.
2707 * The key difference that _exclusive makes it that userspace is
2708 * explicitly not allowed to map the resource via /dev/mem or
2711 int pci_request_region_exclusive(struct pci_dev
*pdev
, int bar
, const char *res_name
)
2713 return __pci_request_region(pdev
, bar
, res_name
, IORESOURCE_EXCLUSIVE
);
2716 * pci_release_selected_regions - Release selected PCI I/O and memory resources
2717 * @pdev: PCI device whose resources were previously reserved
2718 * @bars: Bitmask of BARs to be released
2720 * Release selected PCI I/O and memory resources previously reserved.
2721 * Call this function only after all use of the PCI regions has ceased.
2723 void pci_release_selected_regions(struct pci_dev
*pdev
, int bars
)
2727 for (i
= 0; i
< 6; i
++)
2728 if (bars
& (1 << i
))
2729 pci_release_region(pdev
, i
);
2732 static int __pci_request_selected_regions(struct pci_dev
*pdev
, int bars
,
2733 const char *res_name
, int excl
)
2737 for (i
= 0; i
< 6; i
++)
2738 if (bars
& (1 << i
))
2739 if (__pci_request_region(pdev
, i
, res_name
, excl
))
2745 if (bars
& (1 << i
))
2746 pci_release_region(pdev
, i
);
2753 * pci_request_selected_regions - Reserve selected PCI I/O and memory resources
2754 * @pdev: PCI device whose resources are to be reserved
2755 * @bars: Bitmask of BARs to be requested
2756 * @res_name: Name to be associated with resource
2758 int pci_request_selected_regions(struct pci_dev
*pdev
, int bars
,
2759 const char *res_name
)
2761 return __pci_request_selected_regions(pdev
, bars
, res_name
, 0);
2764 int pci_request_selected_regions_exclusive(struct pci_dev
*pdev
,
2765 int bars
, const char *res_name
)
2767 return __pci_request_selected_regions(pdev
, bars
, res_name
,
2768 IORESOURCE_EXCLUSIVE
);
2772 * pci_release_regions - Release reserved PCI I/O and memory resources
2773 * @pdev: PCI device whose resources were previously reserved by pci_request_regions
2775 * Releases all PCI I/O and memory resources previously reserved by a
2776 * successful call to pci_request_regions. Call this function only
2777 * after all use of the PCI regions has ceased.
2780 void pci_release_regions(struct pci_dev
*pdev
)
2782 pci_release_selected_regions(pdev
, (1 << 6) - 1);
2786 * pci_request_regions - Reserved PCI I/O and memory resources
2787 * @pdev: PCI device whose resources are to be reserved
2788 * @res_name: Name to be associated with resource.
2790 * Mark all PCI regions associated with PCI device @pdev as
2791 * being reserved by owner @res_name. Do not access any
2792 * address inside the PCI regions unless this call returns
2795 * Returns 0 on success, or %EBUSY on error. A warning
2796 * message is also printed on failure.
2798 int pci_request_regions(struct pci_dev
*pdev
, const char *res_name
)
2800 return pci_request_selected_regions(pdev
, ((1 << 6) - 1), res_name
);
2804 * pci_request_regions_exclusive - Reserved PCI I/O and memory resources
2805 * @pdev: PCI device whose resources are to be reserved
2806 * @res_name: Name to be associated with resource.
2808 * Mark all PCI regions associated with PCI device @pdev as
2809 * being reserved by owner @res_name. Do not access any
2810 * address inside the PCI regions unless this call returns
2813 * pci_request_regions_exclusive() will mark the region so that
2814 * /dev/mem and the sysfs MMIO access will not be allowed.
2816 * Returns 0 on success, or %EBUSY on error. A warning
2817 * message is also printed on failure.
2819 int pci_request_regions_exclusive(struct pci_dev
*pdev
, const char *res_name
)
2821 return pci_request_selected_regions_exclusive(pdev
,
2822 ((1 << 6) - 1), res_name
);
2825 static void __pci_set_master(struct pci_dev
*dev
, bool enable
)
2829 pci_read_config_word(dev
, PCI_COMMAND
, &old_cmd
);
2831 cmd
= old_cmd
| PCI_COMMAND_MASTER
;
2833 cmd
= old_cmd
& ~PCI_COMMAND_MASTER
;
2834 if (cmd
!= old_cmd
) {
2835 dev_dbg(&dev
->dev
, "%s bus mastering\n",
2836 enable
? "enabling" : "disabling");
2837 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
2839 dev
->is_busmaster
= enable
;
2843 * pcibios_setup - process "pci=" kernel boot arguments
2844 * @str: string used to pass in "pci=" kernel boot arguments
2846 * Process kernel boot arguments. This is the default implementation.
2847 * Architecture specific implementations can override this as necessary.
2849 char * __weak __init
pcibios_setup(char *str
)
2855 * pcibios_set_master - enable PCI bus-mastering for device dev
2856 * @dev: the PCI device to enable
2858 * Enables PCI bus-mastering for the device. This is the default
2859 * implementation. Architecture specific implementations can override
2860 * this if necessary.
2862 void __weak
pcibios_set_master(struct pci_dev
*dev
)
2866 /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
2867 if (pci_is_pcie(dev
))
2870 pci_read_config_byte(dev
, PCI_LATENCY_TIMER
, &lat
);
2872 lat
= (64 <= pcibios_max_latency
) ? 64 : pcibios_max_latency
;
2873 else if (lat
> pcibios_max_latency
)
2874 lat
= pcibios_max_latency
;
2878 pci_write_config_byte(dev
, PCI_LATENCY_TIMER
, lat
);
2882 * pci_set_master - enables bus-mastering for device dev
2883 * @dev: the PCI device to enable
2885 * Enables bus-mastering on the device and calls pcibios_set_master()
2886 * to do the needed arch specific settings.
2888 void pci_set_master(struct pci_dev
*dev
)
2890 __pci_set_master(dev
, true);
2891 pcibios_set_master(dev
);
2895 * pci_clear_master - disables bus-mastering for device dev
2896 * @dev: the PCI device to disable
2898 void pci_clear_master(struct pci_dev
*dev
)
2900 __pci_set_master(dev
, false);
2904 * pci_set_cacheline_size - ensure the CACHE_LINE_SIZE register is programmed
2905 * @dev: the PCI device for which MWI is to be enabled
2907 * Helper function for pci_set_mwi.
2908 * Originally copied from drivers/net/acenic.c.
2909 * Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>.
2911 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2913 int pci_set_cacheline_size(struct pci_dev
*dev
)
2917 if (!pci_cache_line_size
)
2920 /* Validate current setting: the PCI_CACHE_LINE_SIZE must be
2921 equal to or multiple of the right value. */
2922 pci_read_config_byte(dev
, PCI_CACHE_LINE_SIZE
, &cacheline_size
);
2923 if (cacheline_size
>= pci_cache_line_size
&&
2924 (cacheline_size
% pci_cache_line_size
) == 0)
2927 /* Write the correct value. */
2928 pci_write_config_byte(dev
, PCI_CACHE_LINE_SIZE
, pci_cache_line_size
);
2930 pci_read_config_byte(dev
, PCI_CACHE_LINE_SIZE
, &cacheline_size
);
2931 if (cacheline_size
== pci_cache_line_size
)
2934 dev_printk(KERN_DEBUG
, &dev
->dev
, "cache line size of %d is not "
2935 "supported\n", pci_cache_line_size
<< 2);
2939 EXPORT_SYMBOL_GPL(pci_set_cacheline_size
);
2941 #ifdef PCI_DISABLE_MWI
2942 int pci_set_mwi(struct pci_dev
*dev
)
2947 int pci_try_set_mwi(struct pci_dev
*dev
)
2952 void pci_clear_mwi(struct pci_dev
*dev
)
2959 * pci_set_mwi - enables memory-write-invalidate PCI transaction
2960 * @dev: the PCI device for which MWI is enabled
2962 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
2964 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2967 pci_set_mwi(struct pci_dev
*dev
)
2972 rc
= pci_set_cacheline_size(dev
);
2976 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
2977 if (! (cmd
& PCI_COMMAND_INVALIDATE
)) {
2978 dev_dbg(&dev
->dev
, "enabling Mem-Wr-Inval\n");
2979 cmd
|= PCI_COMMAND_INVALIDATE
;
2980 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
2987 * pci_try_set_mwi - enables memory-write-invalidate PCI transaction
2988 * @dev: the PCI device for which MWI is enabled
2990 * Enables the Memory-Write-Invalidate transaction in %PCI_COMMAND.
2991 * Callers are not required to check the return value.
2993 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2995 int pci_try_set_mwi(struct pci_dev
*dev
)
2997 int rc
= pci_set_mwi(dev
);
3002 * pci_clear_mwi - disables Memory-Write-Invalidate for device dev
3003 * @dev: the PCI device to disable
3005 * Disables PCI Memory-Write-Invalidate transaction on the device
3008 pci_clear_mwi(struct pci_dev
*dev
)
3012 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
3013 if (cmd
& PCI_COMMAND_INVALIDATE
) {
3014 cmd
&= ~PCI_COMMAND_INVALIDATE
;
3015 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
3018 #endif /* ! PCI_DISABLE_MWI */
3021 * pci_intx - enables/disables PCI INTx for device dev
3022 * @pdev: the PCI device to operate on
3023 * @enable: boolean: whether to enable or disable PCI INTx
3025 * Enables/disables PCI INTx for device dev
3028 pci_intx(struct pci_dev
*pdev
, int enable
)
3030 u16 pci_command
, new;
3032 pci_read_config_word(pdev
, PCI_COMMAND
, &pci_command
);
3035 new = pci_command
& ~PCI_COMMAND_INTX_DISABLE
;
3037 new = pci_command
| PCI_COMMAND_INTX_DISABLE
;
3040 if (new != pci_command
) {
3041 struct pci_devres
*dr
;
3043 pci_write_config_word(pdev
, PCI_COMMAND
, new);
3045 dr
= find_pci_dr(pdev
);
3046 if (dr
&& !dr
->restore_intx
) {
3047 dr
->restore_intx
= 1;
3048 dr
->orig_intx
= !enable
;
3054 * pci_intx_mask_supported - probe for INTx masking support
3055 * @dev: the PCI device to operate on
3057 * Check if the device dev support INTx masking via the config space
3060 bool pci_intx_mask_supported(struct pci_dev
*dev
)
3062 bool mask_supported
= false;
3065 if (dev
->broken_intx_masking
)
3068 pci_cfg_access_lock(dev
);
3070 pci_read_config_word(dev
, PCI_COMMAND
, &orig
);
3071 pci_write_config_word(dev
, PCI_COMMAND
,
3072 orig
^ PCI_COMMAND_INTX_DISABLE
);
3073 pci_read_config_word(dev
, PCI_COMMAND
, &new);
3076 * There's no way to protect against hardware bugs or detect them
3077 * reliably, but as long as we know what the value should be, let's
3078 * go ahead and check it.
3080 if ((new ^ orig
) & ~PCI_COMMAND_INTX_DISABLE
) {
3081 dev_err(&dev
->dev
, "Command register changed from "
3082 "0x%x to 0x%x: driver or hardware bug?\n", orig
, new);
3083 } else if ((new ^ orig
) & PCI_COMMAND_INTX_DISABLE
) {
3084 mask_supported
= true;
3085 pci_write_config_word(dev
, PCI_COMMAND
, orig
);
3088 pci_cfg_access_unlock(dev
);
3089 return mask_supported
;
3091 EXPORT_SYMBOL_GPL(pci_intx_mask_supported
);
3093 static bool pci_check_and_set_intx_mask(struct pci_dev
*dev
, bool mask
)
3095 struct pci_bus
*bus
= dev
->bus
;
3096 bool mask_updated
= true;
3097 u32 cmd_status_dword
;
3098 u16 origcmd
, newcmd
;
3099 unsigned long flags
;
3103 * We do a single dword read to retrieve both command and status.
3104 * Document assumptions that make this possible.
3106 BUILD_BUG_ON(PCI_COMMAND
% 4);
3107 BUILD_BUG_ON(PCI_COMMAND
+ 2 != PCI_STATUS
);
3109 raw_spin_lock_irqsave(&pci_lock
, flags
);
3111 bus
->ops
->read(bus
, dev
->devfn
, PCI_COMMAND
, 4, &cmd_status_dword
);
3113 irq_pending
= (cmd_status_dword
>> 16) & PCI_STATUS_INTERRUPT
;
3116 * Check interrupt status register to see whether our device
3117 * triggered the interrupt (when masking) or the next IRQ is
3118 * already pending (when unmasking).
3120 if (mask
!= irq_pending
) {
3121 mask_updated
= false;
3125 origcmd
= cmd_status_dword
;
3126 newcmd
= origcmd
& ~PCI_COMMAND_INTX_DISABLE
;
3128 newcmd
|= PCI_COMMAND_INTX_DISABLE
;
3129 if (newcmd
!= origcmd
)
3130 bus
->ops
->write(bus
, dev
->devfn
, PCI_COMMAND
, 2, newcmd
);
3133 raw_spin_unlock_irqrestore(&pci_lock
, flags
);
3135 return mask_updated
;
3139 * pci_check_and_mask_intx - mask INTx on pending interrupt
3140 * @dev: the PCI device to operate on
3142 * Check if the device dev has its INTx line asserted, mask it and
3143 * return true in that case. False is returned if not interrupt was
3146 bool pci_check_and_mask_intx(struct pci_dev
*dev
)
3148 return pci_check_and_set_intx_mask(dev
, true);
3150 EXPORT_SYMBOL_GPL(pci_check_and_mask_intx
);
3153 * pci_check_and_mask_intx - unmask INTx of no interrupt is pending
3154 * @dev: the PCI device to operate on
3156 * Check if the device dev has its INTx line asserted, unmask it if not
3157 * and return true. False is returned and the mask remains active if
3158 * there was still an interrupt pending.
3160 bool pci_check_and_unmask_intx(struct pci_dev
*dev
)
3162 return pci_check_and_set_intx_mask(dev
, false);
3164 EXPORT_SYMBOL_GPL(pci_check_and_unmask_intx
);
3167 * pci_msi_off - disables any MSI or MSI-X capabilities
3168 * @dev: the PCI device to operate on
3170 * If you want to use MSI, see pci_enable_msi() and friends.
3171 * This is a lower-level primitive that allows us to disable
3172 * MSI operation at the device level.
3174 void pci_msi_off(struct pci_dev
*dev
)
3180 * This looks like it could go in msi.c, but we need it even when
3181 * CONFIG_PCI_MSI=n. For the same reason, we can't use
3182 * dev->msi_cap or dev->msix_cap here.
3184 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
3186 pci_read_config_word(dev
, pos
+ PCI_MSI_FLAGS
, &control
);
3187 control
&= ~PCI_MSI_FLAGS_ENABLE
;
3188 pci_write_config_word(dev
, pos
+ PCI_MSI_FLAGS
, control
);
3190 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
3192 pci_read_config_word(dev
, pos
+ PCI_MSIX_FLAGS
, &control
);
3193 control
&= ~PCI_MSIX_FLAGS_ENABLE
;
3194 pci_write_config_word(dev
, pos
+ PCI_MSIX_FLAGS
, control
);
3197 EXPORT_SYMBOL_GPL(pci_msi_off
);
3199 int pci_set_dma_max_seg_size(struct pci_dev
*dev
, unsigned int size
)
3201 return dma_set_max_seg_size(&dev
->dev
, size
);
3203 EXPORT_SYMBOL(pci_set_dma_max_seg_size
);
3205 int pci_set_dma_seg_boundary(struct pci_dev
*dev
, unsigned long mask
)
3207 return dma_set_seg_boundary(&dev
->dev
, mask
);
3209 EXPORT_SYMBOL(pci_set_dma_seg_boundary
);
3212 * pci_wait_for_pending_transaction - waits for pending transaction
3213 * @dev: the PCI device to operate on
3215 * Return 0 if transaction is pending 1 otherwise.
3217 int pci_wait_for_pending_transaction(struct pci_dev
*dev
)
3222 /* Wait for Transaction Pending bit clean */
3223 for (i
= 0; i
< 4; i
++) {
3225 msleep((1 << (i
- 1)) * 100);
3227 pcie_capability_read_word(dev
, PCI_EXP_DEVSTA
, &status
);
3228 if (!(status
& PCI_EXP_DEVSTA_TRPND
))
3234 EXPORT_SYMBOL(pci_wait_for_pending_transaction
);
3236 static int pcie_flr(struct pci_dev
*dev
, int probe
)
3240 pcie_capability_read_dword(dev
, PCI_EXP_DEVCAP
, &cap
);
3241 if (!(cap
& PCI_EXP_DEVCAP_FLR
))
3247 if (!pci_wait_for_pending_transaction(dev
))
3248 dev_err(&dev
->dev
, "transaction is not cleared; proceeding with reset anyway\n");
3250 pcie_capability_set_word(dev
, PCI_EXP_DEVCTL
, PCI_EXP_DEVCTL_BCR_FLR
);
3257 static int pci_af_flr(struct pci_dev
*dev
, int probe
)
3264 pos
= pci_find_capability(dev
, PCI_CAP_ID_AF
);
3268 pci_read_config_byte(dev
, pos
+ PCI_AF_CAP
, &cap
);
3269 if (!(cap
& PCI_AF_CAP_TP
) || !(cap
& PCI_AF_CAP_FLR
))
3275 /* Wait for Transaction Pending bit clean */
3276 for (i
= 0; i
< 4; i
++) {
3278 msleep((1 << (i
- 1)) * 100);
3280 pci_read_config_byte(dev
, pos
+ PCI_AF_STATUS
, &status
);
3281 if (!(status
& PCI_AF_STATUS_TP
))
3285 dev_err(&dev
->dev
, "transaction is not cleared; "
3286 "proceeding with reset anyway\n");
3289 pci_write_config_byte(dev
, pos
+ PCI_AF_CTRL
, PCI_AF_CTRL_FLR
);
3296 * pci_pm_reset - Put device into PCI_D3 and back into PCI_D0.
3297 * @dev: Device to reset.
3298 * @probe: If set, only check if the device can be reset this way.
3300 * If @dev supports native PCI PM and its PCI_PM_CTRL_NO_SOFT_RESET flag is
3301 * unset, it will be reinitialized internally when going from PCI_D3hot to
3302 * PCI_D0. If that's the case and the device is not in a low-power state
3303 * already, force it into PCI_D3hot and back to PCI_D0, causing it to be reset.
3305 * NOTE: This causes the caller to sleep for twice the device power transition
3306 * cooldown period, which for the D0->D3hot and D3hot->D0 transitions is 10 ms
3307 * by devault (i.e. unless the @dev's d3_delay field has a different value).
3308 * Moreover, only devices in D0 can be reset by this function.
3310 static int pci_pm_reset(struct pci_dev
*dev
, int probe
)
3317 pci_read_config_word(dev
, dev
->pm_cap
+ PCI_PM_CTRL
, &csr
);
3318 if (csr
& PCI_PM_CTRL_NO_SOFT_RESET
)
3324 if (dev
->current_state
!= PCI_D0
)
3327 csr
&= ~PCI_PM_CTRL_STATE_MASK
;
3329 pci_write_config_word(dev
, dev
->pm_cap
+ PCI_PM_CTRL
, csr
);
3330 pci_dev_d3_sleep(dev
);
3332 csr
&= ~PCI_PM_CTRL_STATE_MASK
;
3334 pci_write_config_word(dev
, dev
->pm_cap
+ PCI_PM_CTRL
, csr
);
3335 pci_dev_d3_sleep(dev
);
3341 * pci_reset_bridge_secondary_bus - Reset the secondary bus on a PCI bridge.
3342 * @dev: Bridge device
3344 * Use the bridge control register to assert reset on the secondary bus.
3345 * Devices on the secondary bus are left in power-on state.
3347 void pci_reset_bridge_secondary_bus(struct pci_dev
*dev
)
3351 pci_read_config_word(dev
, PCI_BRIDGE_CONTROL
, &ctrl
);
3352 ctrl
|= PCI_BRIDGE_CTL_BUS_RESET
;
3353 pci_write_config_word(dev
, PCI_BRIDGE_CONTROL
, ctrl
);
3355 * PCI spec v3.0 7.6.4.2 requires minimum Trst of 1ms. Double
3356 * this to 2ms to ensure that we meet the minium requirement.
3360 ctrl
&= ~PCI_BRIDGE_CTL_BUS_RESET
;
3361 pci_write_config_word(dev
, PCI_BRIDGE_CONTROL
, ctrl
);
3364 * Trhfa for conventional PCI is 2^25 clock cycles.
3365 * Assuming a minimum 33MHz clock this results in a 1s
3366 * delay before we can consider subordinate devices to
3367 * be re-initialized. PCIe has some ways to shorten this,
3368 * but we don't make use of them yet.
3372 EXPORT_SYMBOL_GPL(pci_reset_bridge_secondary_bus
);
3374 static int pci_parent_bus_reset(struct pci_dev
*dev
, int probe
)
3376 struct pci_dev
*pdev
;
3378 if (pci_is_root_bus(dev
->bus
) || dev
->subordinate
|| !dev
->bus
->self
)
3381 list_for_each_entry(pdev
, &dev
->bus
->devices
, bus_list
)
3388 pci_reset_bridge_secondary_bus(dev
->bus
->self
);
3393 static int pci_reset_hotplug_slot(struct hotplug_slot
*hotplug
, int probe
)
3397 if (!hotplug
|| !try_module_get(hotplug
->ops
->owner
))
3400 if (hotplug
->ops
->reset_slot
)
3401 rc
= hotplug
->ops
->reset_slot(hotplug
, probe
);
3403 module_put(hotplug
->ops
->owner
);
3408 static int pci_dev_reset_slot_function(struct pci_dev
*dev
, int probe
)
3410 struct pci_dev
*pdev
;
3412 if (dev
->subordinate
|| !dev
->slot
)
3415 list_for_each_entry(pdev
, &dev
->bus
->devices
, bus_list
)
3416 if (pdev
!= dev
&& pdev
->slot
== dev
->slot
)
3419 return pci_reset_hotplug_slot(dev
->slot
->hotplug
, probe
);
3422 static int __pci_dev_reset(struct pci_dev
*dev
, int probe
)
3428 rc
= pci_dev_specific_reset(dev
, probe
);
3432 rc
= pcie_flr(dev
, probe
);
3436 rc
= pci_af_flr(dev
, probe
);
3440 rc
= pci_pm_reset(dev
, probe
);
3444 rc
= pci_dev_reset_slot_function(dev
, probe
);
3448 rc
= pci_parent_bus_reset(dev
, probe
);
3453 static void pci_dev_lock(struct pci_dev
*dev
)
3455 pci_cfg_access_lock(dev
);
3456 /* block PM suspend, driver probe, etc. */
3457 device_lock(&dev
->dev
);
3460 static void pci_dev_unlock(struct pci_dev
*dev
)
3462 device_unlock(&dev
->dev
);
3463 pci_cfg_access_unlock(dev
);
3466 static void pci_dev_save_and_disable(struct pci_dev
*dev
)
3469 * Wake-up device prior to save. PM registers default to D0 after
3470 * reset and a simple register restore doesn't reliably return
3471 * to a non-D0 state anyway.
3473 pci_set_power_state(dev
, PCI_D0
);
3475 pci_save_state(dev
);
3477 * Disable the device by clearing the Command register, except for
3478 * INTx-disable which is set. This not only disables MMIO and I/O port
3479 * BARs, but also prevents the device from being Bus Master, preventing
3480 * DMA from the device including MSI/MSI-X interrupts. For PCI 2.3
3481 * compliant devices, INTx-disable prevents legacy interrupts.
3483 pci_write_config_word(dev
, PCI_COMMAND
, PCI_COMMAND_INTX_DISABLE
);
3486 static void pci_dev_restore(struct pci_dev
*dev
)
3488 pci_restore_state(dev
);
3491 static int pci_dev_reset(struct pci_dev
*dev
, int probe
)
3498 rc
= __pci_dev_reset(dev
, probe
);
3501 pci_dev_unlock(dev
);
3506 * __pci_reset_function - reset a PCI device function
3507 * @dev: PCI device to reset
3509 * Some devices allow an individual function to be reset without affecting
3510 * other functions in the same device. The PCI device must be responsive
3511 * to PCI config space in order to use this function.
3513 * The device function is presumed to be unused when this function is called.
3514 * Resetting the device will make the contents of PCI configuration space
3515 * random, so any caller of this must be prepared to reinitialise the
3516 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
3519 * Returns 0 if the device function was successfully reset or negative if the
3520 * device doesn't support resetting a single function.
3522 int __pci_reset_function(struct pci_dev
*dev
)
3524 return pci_dev_reset(dev
, 0);
3526 EXPORT_SYMBOL_GPL(__pci_reset_function
);
3529 * __pci_reset_function_locked - reset a PCI device function while holding
3530 * the @dev mutex lock.
3531 * @dev: PCI device to reset
3533 * Some devices allow an individual function to be reset without affecting
3534 * other functions in the same device. The PCI device must be responsive
3535 * to PCI config space in order to use this function.
3537 * The device function is presumed to be unused and the caller is holding
3538 * the device mutex lock when this function is called.
3539 * Resetting the device will make the contents of PCI configuration space
3540 * random, so any caller of this must be prepared to reinitialise the
3541 * device including MSI, bus mastering, BARs, decoding IO and memory spaces,
3544 * Returns 0 if the device function was successfully reset or negative if the
3545 * device doesn't support resetting a single function.
3547 int __pci_reset_function_locked(struct pci_dev
*dev
)
3549 return __pci_dev_reset(dev
, 0);
3551 EXPORT_SYMBOL_GPL(__pci_reset_function_locked
);
3554 * pci_probe_reset_function - check whether the device can be safely reset
3555 * @dev: PCI device to reset
3557 * Some devices allow an individual function to be reset without affecting
3558 * other functions in the same device. The PCI device must be responsive
3559 * to PCI config space in order to use this function.
3561 * Returns 0 if the device function can be reset or negative if the
3562 * device doesn't support resetting a single function.
3564 int pci_probe_reset_function(struct pci_dev
*dev
)
3566 return pci_dev_reset(dev
, 1);
3570 * pci_reset_function - quiesce and reset a PCI device function
3571 * @dev: PCI device to reset
3573 * Some devices allow an individual function to be reset without affecting
3574 * other functions in the same device. The PCI device must be responsive
3575 * to PCI config space in order to use this function.
3577 * This function does not just reset the PCI portion of a device, but
3578 * clears all the state associated with the device. This function differs
3579 * from __pci_reset_function in that it saves and restores device state
3582 * Returns 0 if the device function was successfully reset or negative if the
3583 * device doesn't support resetting a single function.
3585 int pci_reset_function(struct pci_dev
*dev
)
3589 rc
= pci_dev_reset(dev
, 1);
3593 pci_dev_save_and_disable(dev
);
3595 rc
= pci_dev_reset(dev
, 0);
3597 pci_dev_restore(dev
);
3601 EXPORT_SYMBOL_GPL(pci_reset_function
);
3603 /* Lock devices from the top of the tree down */
3604 static void pci_bus_lock(struct pci_bus
*bus
)
3606 struct pci_dev
*dev
;
3608 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
3610 if (dev
->subordinate
)
3611 pci_bus_lock(dev
->subordinate
);
3615 /* Unlock devices from the bottom of the tree up */
3616 static void pci_bus_unlock(struct pci_bus
*bus
)
3618 struct pci_dev
*dev
;
3620 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
3621 if (dev
->subordinate
)
3622 pci_bus_unlock(dev
->subordinate
);
3623 pci_dev_unlock(dev
);
3627 /* Lock devices from the top of the tree down */
3628 static void pci_slot_lock(struct pci_slot
*slot
)
3630 struct pci_dev
*dev
;
3632 list_for_each_entry(dev
, &slot
->bus
->devices
, bus_list
) {
3633 if (!dev
->slot
|| dev
->slot
!= slot
)
3636 if (dev
->subordinate
)
3637 pci_bus_lock(dev
->subordinate
);
3641 /* Unlock devices from the bottom of the tree up */
3642 static void pci_slot_unlock(struct pci_slot
*slot
)
3644 struct pci_dev
*dev
;
3646 list_for_each_entry(dev
, &slot
->bus
->devices
, bus_list
) {
3647 if (!dev
->slot
|| dev
->slot
!= slot
)
3649 if (dev
->subordinate
)
3650 pci_bus_unlock(dev
->subordinate
);
3651 pci_dev_unlock(dev
);
3655 /* Save and disable devices from the top of the tree down */
3656 static void pci_bus_save_and_disable(struct pci_bus
*bus
)
3658 struct pci_dev
*dev
;
3660 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
3661 pci_dev_save_and_disable(dev
);
3662 if (dev
->subordinate
)
3663 pci_bus_save_and_disable(dev
->subordinate
);
3668 * Restore devices from top of the tree down - parent bridges need to be
3669 * restored before we can get to subordinate devices.
3671 static void pci_bus_restore(struct pci_bus
*bus
)
3673 struct pci_dev
*dev
;
3675 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
3676 pci_dev_restore(dev
);
3677 if (dev
->subordinate
)
3678 pci_bus_restore(dev
->subordinate
);
3682 /* Save and disable devices from the top of the tree down */
3683 static void pci_slot_save_and_disable(struct pci_slot
*slot
)
3685 struct pci_dev
*dev
;
3687 list_for_each_entry(dev
, &slot
->bus
->devices
, bus_list
) {
3688 if (!dev
->slot
|| dev
->slot
!= slot
)
3690 pci_dev_save_and_disable(dev
);
3691 if (dev
->subordinate
)
3692 pci_bus_save_and_disable(dev
->subordinate
);
3697 * Restore devices from top of the tree down - parent bridges need to be
3698 * restored before we can get to subordinate devices.
3700 static void pci_slot_restore(struct pci_slot
*slot
)
3702 struct pci_dev
*dev
;
3704 list_for_each_entry(dev
, &slot
->bus
->devices
, bus_list
) {
3705 if (!dev
->slot
|| dev
->slot
!= slot
)
3707 pci_dev_restore(dev
);
3708 if (dev
->subordinate
)
3709 pci_bus_restore(dev
->subordinate
);
3713 static int pci_slot_reset(struct pci_slot
*slot
, int probe
)
3721 pci_slot_lock(slot
);
3725 rc
= pci_reset_hotplug_slot(slot
->hotplug
, probe
);
3728 pci_slot_unlock(slot
);
3734 * pci_probe_reset_slot - probe whether a PCI slot can be reset
3735 * @slot: PCI slot to probe
3737 * Return 0 if slot can be reset, negative if a slot reset is not supported.
3739 int pci_probe_reset_slot(struct pci_slot
*slot
)
3741 return pci_slot_reset(slot
, 1);
3743 EXPORT_SYMBOL_GPL(pci_probe_reset_slot
);
3746 * pci_reset_slot - reset a PCI slot
3747 * @slot: PCI slot to reset
3749 * A PCI bus may host multiple slots, each slot may support a reset mechanism
3750 * independent of other slots. For instance, some slots may support slot power
3751 * control. In the case of a 1:1 bus to slot architecture, this function may
3752 * wrap the bus reset to avoid spurious slot related events such as hotplug.
3753 * Generally a slot reset should be attempted before a bus reset. All of the
3754 * function of the slot and any subordinate buses behind the slot are reset
3755 * through this function. PCI config space of all devices in the slot and
3756 * behind the slot is saved before and restored after reset.
3758 * Return 0 on success, non-zero on error.
3760 int pci_reset_slot(struct pci_slot
*slot
)
3764 rc
= pci_slot_reset(slot
, 1);
3768 pci_slot_save_and_disable(slot
);
3770 rc
= pci_slot_reset(slot
, 0);
3772 pci_slot_restore(slot
);
3776 EXPORT_SYMBOL_GPL(pci_reset_slot
);
3778 static int pci_bus_reset(struct pci_bus
*bus
, int probe
)
3790 pci_reset_bridge_secondary_bus(bus
->self
);
3792 pci_bus_unlock(bus
);
3798 * pci_probe_reset_bus - probe whether a PCI bus can be reset
3799 * @bus: PCI bus to probe
3801 * Return 0 if bus can be reset, negative if a bus reset is not supported.
3803 int pci_probe_reset_bus(struct pci_bus
*bus
)
3805 return pci_bus_reset(bus
, 1);
3807 EXPORT_SYMBOL_GPL(pci_probe_reset_bus
);
3810 * pci_reset_bus - reset a PCI bus
3811 * @bus: top level PCI bus to reset
3813 * Do a bus reset on the given bus and any subordinate buses, saving
3814 * and restoring state of all devices.
3816 * Return 0 on success, non-zero on error.
3818 int pci_reset_bus(struct pci_bus
*bus
)
3822 rc
= pci_bus_reset(bus
, 1);
3826 pci_bus_save_and_disable(bus
);
3828 rc
= pci_bus_reset(bus
, 0);
3830 pci_bus_restore(bus
);
3834 EXPORT_SYMBOL_GPL(pci_reset_bus
);
3837 * pcix_get_max_mmrbc - get PCI-X maximum designed memory read byte count
3838 * @dev: PCI device to query
3840 * Returns mmrbc: maximum designed memory read count in bytes
3841 * or appropriate error value.
3843 int pcix_get_max_mmrbc(struct pci_dev
*dev
)
3848 cap
= pci_find_capability(dev
, PCI_CAP_ID_PCIX
);
3852 if (pci_read_config_dword(dev
, cap
+ PCI_X_STATUS
, &stat
))
3855 return 512 << ((stat
& PCI_X_STATUS_MAX_READ
) >> 21);
3857 EXPORT_SYMBOL(pcix_get_max_mmrbc
);
3860 * pcix_get_mmrbc - get PCI-X maximum memory read byte count
3861 * @dev: PCI device to query
3863 * Returns mmrbc: maximum memory read count in bytes
3864 * or appropriate error value.
3866 int pcix_get_mmrbc(struct pci_dev
*dev
)
3871 cap
= pci_find_capability(dev
, PCI_CAP_ID_PCIX
);
3875 if (pci_read_config_word(dev
, cap
+ PCI_X_CMD
, &cmd
))
3878 return 512 << ((cmd
& PCI_X_CMD_MAX_READ
) >> 2);
3880 EXPORT_SYMBOL(pcix_get_mmrbc
);
3883 * pcix_set_mmrbc - set PCI-X maximum memory read byte count
3884 * @dev: PCI device to query
3885 * @mmrbc: maximum memory read count in bytes
3886 * valid values are 512, 1024, 2048, 4096
3888 * If possible sets maximum memory read byte count, some bridges have erratas
3889 * that prevent this.
3891 int pcix_set_mmrbc(struct pci_dev
*dev
, int mmrbc
)
3897 if (mmrbc
< 512 || mmrbc
> 4096 || !is_power_of_2(mmrbc
))
3900 v
= ffs(mmrbc
) - 10;
3902 cap
= pci_find_capability(dev
, PCI_CAP_ID_PCIX
);
3906 if (pci_read_config_dword(dev
, cap
+ PCI_X_STATUS
, &stat
))
3909 if (v
> (stat
& PCI_X_STATUS_MAX_READ
) >> 21)
3912 if (pci_read_config_word(dev
, cap
+ PCI_X_CMD
, &cmd
))
3915 o
= (cmd
& PCI_X_CMD_MAX_READ
) >> 2;
3917 if (v
> o
&& (dev
->bus
->bus_flags
& PCI_BUS_FLAGS_NO_MMRBC
))
3920 cmd
&= ~PCI_X_CMD_MAX_READ
;
3922 if (pci_write_config_word(dev
, cap
+ PCI_X_CMD
, cmd
))
3927 EXPORT_SYMBOL(pcix_set_mmrbc
);
3930 * pcie_get_readrq - get PCI Express read request size
3931 * @dev: PCI device to query
3933 * Returns maximum memory read request in bytes
3934 * or appropriate error value.
3936 int pcie_get_readrq(struct pci_dev
*dev
)
3940 pcie_capability_read_word(dev
, PCI_EXP_DEVCTL
, &ctl
);
3942 return 128 << ((ctl
& PCI_EXP_DEVCTL_READRQ
) >> 12);
3944 EXPORT_SYMBOL(pcie_get_readrq
);
3947 * pcie_set_readrq - set PCI Express maximum memory read request
3948 * @dev: PCI device to query
3949 * @rq: maximum memory read count in bytes
3950 * valid values are 128, 256, 512, 1024, 2048, 4096
3952 * If possible sets maximum memory read request in bytes
3954 int pcie_set_readrq(struct pci_dev
*dev
, int rq
)
3958 if (rq
< 128 || rq
> 4096 || !is_power_of_2(rq
))
3962 * If using the "performance" PCIe config, we clamp the
3963 * read rq size to the max packet size to prevent the
3964 * host bridge generating requests larger than we can
3967 if (pcie_bus_config
== PCIE_BUS_PERFORMANCE
) {
3968 int mps
= pcie_get_mps(dev
);
3974 v
= (ffs(rq
) - 8) << 12;
3976 return pcie_capability_clear_and_set_word(dev
, PCI_EXP_DEVCTL
,
3977 PCI_EXP_DEVCTL_READRQ
, v
);
3979 EXPORT_SYMBOL(pcie_set_readrq
);
3982 * pcie_get_mps - get PCI Express maximum payload size
3983 * @dev: PCI device to query
3985 * Returns maximum payload size in bytes
3987 int pcie_get_mps(struct pci_dev
*dev
)
3991 pcie_capability_read_word(dev
, PCI_EXP_DEVCTL
, &ctl
);
3993 return 128 << ((ctl
& PCI_EXP_DEVCTL_PAYLOAD
) >> 5);
3997 * pcie_set_mps - set PCI Express maximum payload size
3998 * @dev: PCI device to query
3999 * @mps: maximum payload size in bytes
4000 * valid values are 128, 256, 512, 1024, 2048, 4096
4002 * If possible sets maximum payload size
4004 int pcie_set_mps(struct pci_dev
*dev
, int mps
)
4008 if (mps
< 128 || mps
> 4096 || !is_power_of_2(mps
))
4012 if (v
> dev
->pcie_mpss
)
4016 return pcie_capability_clear_and_set_word(dev
, PCI_EXP_DEVCTL
,
4017 PCI_EXP_DEVCTL_PAYLOAD
, v
);
4021 * pcie_get_minimum_link - determine minimum link settings of a PCI device
4022 * @dev: PCI device to query
4023 * @speed: storage for minimum speed
4024 * @width: storage for minimum width
4026 * This function will walk up the PCI device chain and determine the minimum
4027 * link width and speed of the device.
4029 int pcie_get_minimum_link(struct pci_dev
*dev
, enum pci_bus_speed
*speed
,
4030 enum pcie_link_width
*width
)
4034 *speed
= PCI_SPEED_UNKNOWN
;
4035 *width
= PCIE_LNK_WIDTH_UNKNOWN
;
4039 enum pci_bus_speed next_speed
;
4040 enum pcie_link_width next_width
;
4042 ret
= pcie_capability_read_word(dev
, PCI_EXP_LNKSTA
, &lnksta
);
4046 next_speed
= pcie_link_speed
[lnksta
& PCI_EXP_LNKSTA_CLS
];
4047 next_width
= (lnksta
& PCI_EXP_LNKSTA_NLW
) >>
4048 PCI_EXP_LNKSTA_NLW_SHIFT
;
4050 if (next_speed
< *speed
)
4051 *speed
= next_speed
;
4053 if (next_width
< *width
)
4054 *width
= next_width
;
4056 dev
= dev
->bus
->self
;
4061 EXPORT_SYMBOL(pcie_get_minimum_link
);
4064 * pci_select_bars - Make BAR mask from the type of resource
4065 * @dev: the PCI device for which BAR mask is made
4066 * @flags: resource type mask to be selected
4068 * This helper routine makes bar mask from the type of resource.
4070 int pci_select_bars(struct pci_dev
*dev
, unsigned long flags
)
4073 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++)
4074 if (pci_resource_flags(dev
, i
) & flags
)
4080 * pci_resource_bar - get position of the BAR associated with a resource
4081 * @dev: the PCI device
4082 * @resno: the resource number
4083 * @type: the BAR type to be filled in
4085 * Returns BAR position in config space, or 0 if the BAR is invalid.
4087 int pci_resource_bar(struct pci_dev
*dev
, int resno
, enum pci_bar_type
*type
)
4091 if (resno
< PCI_ROM_RESOURCE
) {
4092 *type
= pci_bar_unknown
;
4093 return PCI_BASE_ADDRESS_0
+ 4 * resno
;
4094 } else if (resno
== PCI_ROM_RESOURCE
) {
4095 *type
= pci_bar_mem32
;
4096 return dev
->rom_base_reg
;
4097 } else if (resno
< PCI_BRIDGE_RESOURCES
) {
4098 /* device specific resource */
4099 reg
= pci_iov_resource_bar(dev
, resno
, type
);
4104 dev_err(&dev
->dev
, "BAR %d: invalid resource\n", resno
);
4108 /* Some architectures require additional programming to enable VGA */
4109 static arch_set_vga_state_t arch_set_vga_state
;
4111 void __init
pci_register_set_vga_state(arch_set_vga_state_t func
)
4113 arch_set_vga_state
= func
; /* NULL disables */
4116 static int pci_set_vga_state_arch(struct pci_dev
*dev
, bool decode
,
4117 unsigned int command_bits
, u32 flags
)
4119 if (arch_set_vga_state
)
4120 return arch_set_vga_state(dev
, decode
, command_bits
,
4126 * pci_set_vga_state - set VGA decode state on device and parents if requested
4127 * @dev: the PCI device
4128 * @decode: true = enable decoding, false = disable decoding
4129 * @command_bits: PCI_COMMAND_IO and/or PCI_COMMAND_MEMORY
4130 * @flags: traverse ancestors and change bridges
4131 * CHANGE_BRIDGE_ONLY / CHANGE_BRIDGE
4133 int pci_set_vga_state(struct pci_dev
*dev
, bool decode
,
4134 unsigned int command_bits
, u32 flags
)
4136 struct pci_bus
*bus
;
4137 struct pci_dev
*bridge
;
4141 WARN_ON((flags
& PCI_VGA_STATE_CHANGE_DECODES
) && (command_bits
& ~(PCI_COMMAND_IO
|PCI_COMMAND_MEMORY
)));
4143 /* ARCH specific VGA enables */
4144 rc
= pci_set_vga_state_arch(dev
, decode
, command_bits
, flags
);
4148 if (flags
& PCI_VGA_STATE_CHANGE_DECODES
) {
4149 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
4151 cmd
|= command_bits
;
4153 cmd
&= ~command_bits
;
4154 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
4157 if (!(flags
& PCI_VGA_STATE_CHANGE_BRIDGE
))
4164 pci_read_config_word(bridge
, PCI_BRIDGE_CONTROL
,
4167 cmd
|= PCI_BRIDGE_CTL_VGA
;
4169 cmd
&= ~PCI_BRIDGE_CTL_VGA
;
4170 pci_write_config_word(bridge
, PCI_BRIDGE_CONTROL
,
4178 #define RESOURCE_ALIGNMENT_PARAM_SIZE COMMAND_LINE_SIZE
4179 static char resource_alignment_param
[RESOURCE_ALIGNMENT_PARAM_SIZE
] = {0};
4180 static DEFINE_SPINLOCK(resource_alignment_lock
);
4183 * pci_specified_resource_alignment - get resource alignment specified by user.
4184 * @dev: the PCI device to get
4186 * RETURNS: Resource alignment if it is specified.
4187 * Zero if it is not specified.
4189 static resource_size_t
pci_specified_resource_alignment(struct pci_dev
*dev
)
4191 int seg
, bus
, slot
, func
, align_order
, count
;
4192 resource_size_t align
= 0;
4195 spin_lock(&resource_alignment_lock
);
4196 p
= resource_alignment_param
;
4199 if (sscanf(p
, "%d%n", &align_order
, &count
) == 1 &&
4205 if (sscanf(p
, "%x:%x:%x.%x%n",
4206 &seg
, &bus
, &slot
, &func
, &count
) != 4) {
4208 if (sscanf(p
, "%x:%x.%x%n",
4209 &bus
, &slot
, &func
, &count
) != 3) {
4210 /* Invalid format */
4211 printk(KERN_ERR
"PCI: Can't parse resource_alignment parameter: %s\n",
4217 if (seg
== pci_domain_nr(dev
->bus
) &&
4218 bus
== dev
->bus
->number
&&
4219 slot
== PCI_SLOT(dev
->devfn
) &&
4220 func
== PCI_FUNC(dev
->devfn
)) {
4221 if (align_order
== -1) {
4224 align
= 1 << align_order
;
4229 if (*p
!= ';' && *p
!= ',') {
4230 /* End of param or invalid format */
4235 spin_unlock(&resource_alignment_lock
);
4240 * This function disables memory decoding and releases memory resources
4241 * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
4242 * It also rounds up size to specified alignment.
4243 * Later on, the kernel will assign page-aligned memory resource back
4246 void pci_reassigndev_resource_alignment(struct pci_dev
*dev
)
4250 resource_size_t align
, size
;
4253 /* check if specified PCI is target device to reassign */
4254 align
= pci_specified_resource_alignment(dev
);
4258 if (dev
->hdr_type
== PCI_HEADER_TYPE_NORMAL
&&
4259 (dev
->class >> 8) == PCI_CLASS_BRIDGE_HOST
) {
4261 "Can't reassign resources to host bridge.\n");
4266 "Disabling memory decoding and releasing memory resources.\n");
4267 pci_read_config_word(dev
, PCI_COMMAND
, &command
);
4268 command
&= ~PCI_COMMAND_MEMORY
;
4269 pci_write_config_word(dev
, PCI_COMMAND
, command
);
4271 for (i
= 0; i
< PCI_BRIDGE_RESOURCES
; i
++) {
4272 r
= &dev
->resource
[i
];
4273 if (!(r
->flags
& IORESOURCE_MEM
))
4275 size
= resource_size(r
);
4279 "Rounding up size of resource #%d to %#llx.\n",
4280 i
, (unsigned long long)size
);
4285 /* Need to disable bridge's resource window,
4286 * to enable the kernel to reassign new resource
4289 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
&&
4290 (dev
->class >> 8) == PCI_CLASS_BRIDGE_PCI
) {
4291 for (i
= PCI_BRIDGE_RESOURCES
; i
< PCI_NUM_RESOURCES
; i
++) {
4292 r
= &dev
->resource
[i
];
4293 if (!(r
->flags
& IORESOURCE_MEM
))
4295 r
->end
= resource_size(r
) - 1;
4298 pci_disable_bridge_window(dev
);
4302 static ssize_t
pci_set_resource_alignment_param(const char *buf
, size_t count
)
4304 if (count
> RESOURCE_ALIGNMENT_PARAM_SIZE
- 1)
4305 count
= RESOURCE_ALIGNMENT_PARAM_SIZE
- 1;
4306 spin_lock(&resource_alignment_lock
);
4307 strncpy(resource_alignment_param
, buf
, count
);
4308 resource_alignment_param
[count
] = '\0';
4309 spin_unlock(&resource_alignment_lock
);
4313 static ssize_t
pci_get_resource_alignment_param(char *buf
, size_t size
)
4316 spin_lock(&resource_alignment_lock
);
4317 count
= snprintf(buf
, size
, "%s", resource_alignment_param
);
4318 spin_unlock(&resource_alignment_lock
);
4322 static ssize_t
pci_resource_alignment_show(struct bus_type
*bus
, char *buf
)
4324 return pci_get_resource_alignment_param(buf
, PAGE_SIZE
);
4327 static ssize_t
pci_resource_alignment_store(struct bus_type
*bus
,
4328 const char *buf
, size_t count
)
4330 return pci_set_resource_alignment_param(buf
, count
);
4333 BUS_ATTR(resource_alignment
, 0644, pci_resource_alignment_show
,
4334 pci_resource_alignment_store
);
4336 static int __init
pci_resource_alignment_sysfs_init(void)
4338 return bus_create_file(&pci_bus_type
,
4339 &bus_attr_resource_alignment
);
4342 late_initcall(pci_resource_alignment_sysfs_init
);
4344 static void pci_no_domains(void)
4346 #ifdef CONFIG_PCI_DOMAINS
4347 pci_domains_supported
= 0;
4352 * pci_ext_cfg_avail - can we access extended PCI config space?
4354 * Returns 1 if we can access PCI extended config space (offsets
4355 * greater than 0xff). This is the default implementation. Architecture
4356 * implementations can override this.
4358 int __weak
pci_ext_cfg_avail(void)
4363 void __weak
pci_fixup_cardbus(struct pci_bus
*bus
)
4366 EXPORT_SYMBOL(pci_fixup_cardbus
);
4368 static int __init
pci_setup(char *str
)
4371 char *k
= strchr(str
, ',');
4374 if (*str
&& (str
= pcibios_setup(str
)) && *str
) {
4375 if (!strcmp(str
, "nomsi")) {
4377 } else if (!strcmp(str
, "noaer")) {
4379 } else if (!strncmp(str
, "realloc=", 8)) {
4380 pci_realloc_get_opt(str
+ 8);
4381 } else if (!strncmp(str
, "realloc", 7)) {
4382 pci_realloc_get_opt("on");
4383 } else if (!strcmp(str
, "nodomains")) {
4385 } else if (!strncmp(str
, "noari", 5)) {
4386 pcie_ari_disabled
= true;
4387 } else if (!strncmp(str
, "cbiosize=", 9)) {
4388 pci_cardbus_io_size
= memparse(str
+ 9, &str
);
4389 } else if (!strncmp(str
, "cbmemsize=", 10)) {
4390 pci_cardbus_mem_size
= memparse(str
+ 10, &str
);
4391 } else if (!strncmp(str
, "resource_alignment=", 19)) {
4392 pci_set_resource_alignment_param(str
+ 19,
4394 } else if (!strncmp(str
, "ecrc=", 5)) {
4395 pcie_ecrc_get_policy(str
+ 5);
4396 } else if (!strncmp(str
, "hpiosize=", 9)) {
4397 pci_hotplug_io_size
= memparse(str
+ 9, &str
);
4398 } else if (!strncmp(str
, "hpmemsize=", 10)) {
4399 pci_hotplug_mem_size
= memparse(str
+ 10, &str
);
4400 } else if (!strncmp(str
, "pcie_bus_tune_off", 17)) {
4401 pcie_bus_config
= PCIE_BUS_TUNE_OFF
;
4402 } else if (!strncmp(str
, "pcie_bus_safe", 13)) {
4403 pcie_bus_config
= PCIE_BUS_SAFE
;
4404 } else if (!strncmp(str
, "pcie_bus_perf", 13)) {
4405 pcie_bus_config
= PCIE_BUS_PERFORMANCE
;
4406 } else if (!strncmp(str
, "pcie_bus_peer2peer", 18)) {
4407 pcie_bus_config
= PCIE_BUS_PEER2PEER
;
4408 } else if (!strncmp(str
, "pcie_scan_all", 13)) {
4409 pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS
);
4411 printk(KERN_ERR
"PCI: Unknown option `%s'\n",
4419 early_param("pci", pci_setup
);
4421 EXPORT_SYMBOL(pci_reenable_device
);
4422 EXPORT_SYMBOL(pci_enable_device_io
);
4423 EXPORT_SYMBOL(pci_enable_device_mem
);
4424 EXPORT_SYMBOL(pci_enable_device
);
4425 EXPORT_SYMBOL(pcim_enable_device
);
4426 EXPORT_SYMBOL(pcim_pin_device
);
4427 EXPORT_SYMBOL(pci_disable_device
);
4428 EXPORT_SYMBOL(pci_find_capability
);
4429 EXPORT_SYMBOL(pci_bus_find_capability
);
4430 EXPORT_SYMBOL(pci_release_regions
);
4431 EXPORT_SYMBOL(pci_request_regions
);
4432 EXPORT_SYMBOL(pci_request_regions_exclusive
);
4433 EXPORT_SYMBOL(pci_release_region
);
4434 EXPORT_SYMBOL(pci_request_region
);
4435 EXPORT_SYMBOL(pci_request_region_exclusive
);
4436 EXPORT_SYMBOL(pci_release_selected_regions
);
4437 EXPORT_SYMBOL(pci_request_selected_regions
);
4438 EXPORT_SYMBOL(pci_request_selected_regions_exclusive
);
4439 EXPORT_SYMBOL(pci_set_master
);
4440 EXPORT_SYMBOL(pci_clear_master
);
4441 EXPORT_SYMBOL(pci_set_mwi
);
4442 EXPORT_SYMBOL(pci_try_set_mwi
);
4443 EXPORT_SYMBOL(pci_clear_mwi
);
4444 EXPORT_SYMBOL_GPL(pci_intx
);
4445 EXPORT_SYMBOL(pci_assign_resource
);
4446 EXPORT_SYMBOL(pci_find_parent_resource
);
4447 EXPORT_SYMBOL(pci_select_bars
);
4449 EXPORT_SYMBOL(pci_set_power_state
);
4450 EXPORT_SYMBOL(pci_save_state
);
4451 EXPORT_SYMBOL(pci_restore_state
);
4452 EXPORT_SYMBOL(pci_pme_capable
);
4453 EXPORT_SYMBOL(pci_pme_active
);
4454 EXPORT_SYMBOL(pci_wake_from_d3
);
4455 EXPORT_SYMBOL(pci_target_state
);
4456 EXPORT_SYMBOL(pci_prepare_to_sleep
);
4457 EXPORT_SYMBOL(pci_back_from_sleep
);
4458 EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state
);