2 * File: drivers/pci/pcie/aspm.c
3 * Enabling PCIE link L0s/L1 state and Clock Power Management
5 * Copyright (C) 2007 Intel
6 * Copyright (C) Zhang Yanmin (yanmin.zhang@intel.com)
7 * Copyright (C) Shaohua Li (shaohua.li@intel.com)
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/pci.h>
14 #include <linux/pci_regs.h>
15 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/jiffies.h>
20 #include <linux/delay.h>
21 #include <linux/pci-aspm.h>
24 #ifdef MODULE_PARAM_PREFIX
25 #undef MODULE_PARAM_PREFIX
27 #define MODULE_PARAM_PREFIX "pcie_aspm."
30 u32 l0s
; /* L0s latency (nsec) */
31 u32 l1
; /* L1 latency (nsec) */
34 struct pcie_link_state
{
35 struct pci_dev
*pdev
; /* Upstream component of the Link */
36 struct pcie_link_state
*root
; /* pointer to the root port link */
37 struct pcie_link_state
*parent
; /* pointer to the parent Link state */
38 struct list_head sibling
; /* node in link_list */
39 struct list_head children
; /* list of child link states */
40 struct list_head link
; /* node in parent's children list */
43 u32 aspm_support
:2; /* Supported ASPM state */
44 u32 aspm_enabled
:2; /* Enabled ASPM state */
45 u32 aspm_default
:2; /* Default ASPM state by BIOS */
48 u32 clkpm_capable
:1; /* Clock PM capable? */
49 u32 clkpm_enabled
:1; /* Current Clock PM state */
50 u32 clkpm_default
:1; /* Default Clock PM state by BIOS */
53 struct aspm_latency latency
; /* Exit latency */
55 * Endpoint acceptable latencies. A pcie downstream port only
56 * has one slot under it, so at most there are 8 functions.
58 struct aspm_latency acceptable
[8];
61 static int aspm_disabled
, aspm_force
;
62 static DEFINE_MUTEX(aspm_lock
);
63 static LIST_HEAD(link_list
);
65 #define POLICY_DEFAULT 0 /* BIOS default setting */
66 #define POLICY_PERFORMANCE 1 /* high performance */
67 #define POLICY_POWERSAVE 2 /* high power saving */
68 static int aspm_policy
;
69 static const char *policy_str
[] = {
70 [POLICY_DEFAULT
] = "default",
71 [POLICY_PERFORMANCE
] = "performance",
72 [POLICY_POWERSAVE
] = "powersave"
75 #define LINK_RETRAIN_TIMEOUT HZ
77 static int policy_to_aspm_state(struct pcie_link_state
*link
)
79 switch (aspm_policy
) {
80 case POLICY_PERFORMANCE
:
81 /* Disable ASPM and Clock PM */
83 case POLICY_POWERSAVE
:
84 /* Enable ASPM L0s/L1 */
85 return PCIE_LINK_STATE_L0S
| PCIE_LINK_STATE_L1
;
87 return link
->aspm_default
;
92 static int policy_to_clkpm_state(struct pcie_link_state
*link
)
94 switch (aspm_policy
) {
95 case POLICY_PERFORMANCE
:
96 /* Disable ASPM and Clock PM */
98 case POLICY_POWERSAVE
:
99 /* Disable Clock PM */
102 return link
->clkpm_default
;
107 static void pcie_set_clkpm_nocheck(struct pcie_link_state
*link
, int enable
)
111 struct pci_dev
*child
;
112 struct pci_bus
*linkbus
= link
->pdev
->subordinate
;
114 list_for_each_entry(child
, &linkbus
->devices
, bus_list
) {
115 pos
= pci_find_capability(child
, PCI_CAP_ID_EXP
);
118 pci_read_config_word(child
, pos
+ PCI_EXP_LNKCTL
, ®16
);
120 reg16
|= PCI_EXP_LNKCTL_CLKREQ_EN
;
122 reg16
&= ~PCI_EXP_LNKCTL_CLKREQ_EN
;
123 pci_write_config_word(child
, pos
+ PCI_EXP_LNKCTL
, reg16
);
125 link
->clkpm_enabled
= !!enable
;
128 static void pcie_set_clkpm(struct pcie_link_state
*link
, int enable
)
130 /* Don't enable Clock PM if the link is not Clock PM capable */
131 if (!link
->clkpm_capable
&& enable
)
133 /* Need nothing if the specified equals to current state */
134 if (link
->clkpm_enabled
== enable
)
136 pcie_set_clkpm_nocheck(link
, enable
);
139 static void pcie_clkpm_cap_init(struct pcie_link_state
*link
, int blacklist
)
141 int pos
, capable
= 1, enabled
= 1;
144 struct pci_dev
*child
;
145 struct pci_bus
*linkbus
= link
->pdev
->subordinate
;
147 /* All functions should have the same cap and state, take the worst */
148 list_for_each_entry(child
, &linkbus
->devices
, bus_list
) {
149 pos
= pci_find_capability(child
, PCI_CAP_ID_EXP
);
152 pci_read_config_dword(child
, pos
+ PCI_EXP_LNKCAP
, ®32
);
153 if (!(reg32
& PCI_EXP_LNKCAP_CLKPM
)) {
158 pci_read_config_word(child
, pos
+ PCI_EXP_LNKCTL
, ®16
);
159 if (!(reg16
& PCI_EXP_LNKCTL_CLKREQ_EN
))
162 link
->clkpm_enabled
= enabled
;
163 link
->clkpm_default
= enabled
;
164 link
->clkpm_capable
= (blacklist
) ? 0 : capable
;
167 static bool pcie_aspm_downstream_has_switch(struct pcie_link_state
*link
)
169 struct pci_dev
*child
;
170 struct pci_bus
*linkbus
= link
->pdev
->subordinate
;
172 list_for_each_entry(child
, &linkbus
->devices
, bus_list
) {
173 if (child
->pcie_type
== PCI_EXP_TYPE_UPSTREAM
)
180 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
181 * could use common clock. If they are, configure them to use the
182 * common clock. That will reduce the ASPM state exit latency.
184 static void pcie_aspm_configure_common_clock(struct pcie_link_state
*link
)
186 int ppos
, cpos
, same_clock
= 1;
187 u16 reg16
, parent_reg
, child_reg
[8];
188 unsigned long start_jiffies
;
189 struct pci_dev
*child
, *parent
= link
->pdev
;
190 struct pci_bus
*linkbus
= parent
->subordinate
;
192 * All functions of a slot should have the same Slot Clock
193 * Configuration, so just check one function
195 child
= list_entry(linkbus
->devices
.next
, struct pci_dev
, bus_list
);
196 BUG_ON(!child
->is_pcie
);
198 /* Check downstream component if bit Slot Clock Configuration is 1 */
199 cpos
= pci_find_capability(child
, PCI_CAP_ID_EXP
);
200 pci_read_config_word(child
, cpos
+ PCI_EXP_LNKSTA
, ®16
);
201 if (!(reg16
& PCI_EXP_LNKSTA_SLC
))
204 /* Check upstream component if bit Slot Clock Configuration is 1 */
205 ppos
= pci_find_capability(parent
, PCI_CAP_ID_EXP
);
206 pci_read_config_word(parent
, ppos
+ PCI_EXP_LNKSTA
, ®16
);
207 if (!(reg16
& PCI_EXP_LNKSTA_SLC
))
210 /* Configure downstream component, all functions */
211 list_for_each_entry(child
, &linkbus
->devices
, bus_list
) {
212 cpos
= pci_find_capability(child
, PCI_CAP_ID_EXP
);
213 pci_read_config_word(child
, cpos
+ PCI_EXP_LNKCTL
, ®16
);
214 child_reg
[PCI_FUNC(child
->devfn
)] = reg16
;
216 reg16
|= PCI_EXP_LNKCTL_CCC
;
218 reg16
&= ~PCI_EXP_LNKCTL_CCC
;
219 pci_write_config_word(child
, cpos
+ PCI_EXP_LNKCTL
, reg16
);
222 /* Configure upstream component */
223 pci_read_config_word(parent
, ppos
+ PCI_EXP_LNKCTL
, ®16
);
226 reg16
|= PCI_EXP_LNKCTL_CCC
;
228 reg16
&= ~PCI_EXP_LNKCTL_CCC
;
229 pci_write_config_word(parent
, ppos
+ PCI_EXP_LNKCTL
, reg16
);
232 reg16
|= PCI_EXP_LNKCTL_RL
;
233 pci_write_config_word(parent
, ppos
+ PCI_EXP_LNKCTL
, reg16
);
235 /* Wait for link training end. Break out after waiting for timeout */
236 start_jiffies
= jiffies
;
238 pci_read_config_word(parent
, ppos
+ PCI_EXP_LNKSTA
, ®16
);
239 if (!(reg16
& PCI_EXP_LNKSTA_LT
))
241 if (time_after(jiffies
, start_jiffies
+ LINK_RETRAIN_TIMEOUT
))
245 if (!(reg16
& PCI_EXP_LNKSTA_LT
))
248 /* Training failed. Restore common clock configurations */
249 dev_printk(KERN_ERR
, &parent
->dev
,
250 "ASPM: Could not configure common clock\n");
251 list_for_each_entry(child
, &linkbus
->devices
, bus_list
) {
252 cpos
= pci_find_capability(child
, PCI_CAP_ID_EXP
);
253 pci_write_config_word(child
, cpos
+ PCI_EXP_LNKCTL
,
254 child_reg
[PCI_FUNC(child
->devfn
)]);
256 pci_write_config_word(parent
, ppos
+ PCI_EXP_LNKCTL
, parent_reg
);
259 /* Convert L0s latency encoding to ns */
260 static u32
calc_l0s_latency(u32 encoding
)
263 return (5 * 1000); /* > 4us */
264 return (64 << encoding
);
267 /* Convert L0s acceptable latency encoding to ns */
268 static u32
calc_l0s_acceptable(u32 encoding
)
272 return (64 << encoding
);
275 /* Convert L1 latency encoding to ns */
276 static u32
calc_l1_latency(u32 encoding
)
279 return (65 * 1000); /* > 64us */
280 return (1000 << encoding
);
283 /* Convert L1 acceptable latency encoding to ns */
284 static u32
calc_l1_acceptable(u32 encoding
)
288 return (1000 << encoding
);
291 static void pcie_aspm_get_cap_device(struct pci_dev
*pdev
, u32
*state
,
292 u32
*l0s
, u32
*l1
, u32
*enabled
)
298 *l0s
= *l1
= *enabled
= 0;
299 pos
= pci_find_capability(pdev
, PCI_CAP_ID_EXP
);
300 pci_read_config_dword(pdev
, pos
+ PCI_EXP_LNKCAP
, ®32
);
301 *state
= (reg32
& PCI_EXP_LNKCAP_ASPMS
) >> 10;
302 if (*state
!= PCIE_LINK_STATE_L0S
&&
303 *state
!= (PCIE_LINK_STATE_L1
| PCIE_LINK_STATE_L0S
))
308 encoding
= (reg32
& PCI_EXP_LNKCAP_L0SEL
) >> 12;
309 *l0s
= calc_l0s_latency(encoding
);
310 if (*state
& PCIE_LINK_STATE_L1
) {
311 encoding
= (reg32
& PCI_EXP_LNKCAP_L1EL
) >> 15;
312 *l1
= calc_l1_latency(encoding
);
314 pci_read_config_word(pdev
, pos
+ PCI_EXP_LNKCTL
, ®16
);
315 *enabled
= reg16
& (PCIE_LINK_STATE_L0S
| PCIE_LINK_STATE_L1
);
318 static void pcie_aspm_cap_init(struct pcie_link_state
*link
, int blacklist
)
320 u32 support
, l0s
, l1
, enabled
;
321 struct pci_dev
*child
, *parent
= link
->pdev
;
322 struct pci_bus
*linkbus
= parent
->subordinate
;
325 /* Set support state to 0, so we will disable ASPM later */
326 link
->aspm_support
= 0;
327 link
->aspm_default
= 0;
328 link
->aspm_enabled
= PCIE_LINK_STATE_L0S
| PCIE_LINK_STATE_L1
;
332 /* Configure common clock before checking latencies */
333 pcie_aspm_configure_common_clock(link
);
335 /* upstream component states */
336 pcie_aspm_get_cap_device(parent
, &support
, &l0s
, &l1
, &enabled
);
337 link
->aspm_support
= support
;
338 link
->latency
.l0s
= l0s
;
339 link
->latency
.l1
= l1
;
340 link
->aspm_enabled
= enabled
;
342 /* downstream component states, all functions have the same setting */
343 child
= list_entry(linkbus
->devices
.next
, struct pci_dev
, bus_list
);
344 pcie_aspm_get_cap_device(child
, &support
, &l0s
, &l1
, &enabled
);
345 link
->aspm_support
&= support
;
346 link
->latency
.l0s
= max_t(u32
, link
->latency
.l0s
, l0s
);
347 link
->latency
.l1
= max_t(u32
, link
->latency
.l1
, l1
);
349 if (!link
->aspm_support
)
352 link
->aspm_enabled
&= link
->aspm_support
;
353 link
->aspm_default
= link
->aspm_enabled
;
356 list_for_each_entry(child
, &linkbus
->devices
, bus_list
) {
359 struct aspm_latency
*acceptable
=
360 &link
->acceptable
[PCI_FUNC(child
->devfn
)];
362 if (child
->pcie_type
!= PCI_EXP_TYPE_ENDPOINT
&&
363 child
->pcie_type
!= PCI_EXP_TYPE_LEG_END
)
366 pos
= pci_find_capability(child
, PCI_CAP_ID_EXP
);
367 pci_read_config_dword(child
, pos
+ PCI_EXP_DEVCAP
, ®32
);
368 encoding
= (reg32
& PCI_EXP_DEVCAP_L0S
) >> 6;
369 acceptable
->l0s
= calc_l0s_acceptable(encoding
);
370 if (link
->aspm_support
& PCIE_LINK_STATE_L1
) {
371 encoding
= (reg32
& PCI_EXP_DEVCAP_L1
) >> 9;
372 acceptable
->l1
= calc_l1_acceptable(encoding
);
378 * __pcie_aspm_check_state_one - check latency for endpoint device.
379 * @endpoint: pointer to the struct pci_dev of endpoint device
381 * TBD: The latency from the endpoint to root complex vary per switch's
382 * upstream link state above the device. Here we just do a simple check
383 * which assumes all links above the device can be in L1 state, that
384 * is we just consider the worst case. If switch's upstream link can't
385 * be put into L0S/L1, then our check is too strictly.
387 static u32
__pcie_aspm_check_state_one(struct pci_dev
*endpoint
, u32 state
)
389 u32 l1_switch_latency
= 0;
390 struct aspm_latency
*acceptable
;
391 struct pcie_link_state
*link
;
393 link
= endpoint
->bus
->self
->link_state
;
394 state
&= link
->aspm_support
;
395 acceptable
= &link
->acceptable
[PCI_FUNC(endpoint
->devfn
)];
397 while (link
&& state
) {
398 if ((state
& PCIE_LINK_STATE_L0S
) &&
399 (link
->latency
.l0s
> acceptable
->l0s
))
400 state
&= ~PCIE_LINK_STATE_L0S
;
401 if ((state
& PCIE_LINK_STATE_L1
) &&
402 (link
->latency
.l1
+ l1_switch_latency
> acceptable
->l1
))
403 state
&= ~PCIE_LINK_STATE_L1
;
406 * Every switch on the path to root complex need 1
407 * more microsecond for L1. Spec doesn't mention L0s.
409 l1_switch_latency
+= 1000;
414 static u32
pcie_aspm_check_state(struct pcie_link_state
*link
, u32 state
)
416 pci_power_t power_state
;
417 struct pci_dev
*child
;
418 struct pci_bus
*linkbus
= link
->pdev
->subordinate
;
420 /* If no child, ignore the link */
421 if (list_empty(&linkbus
->devices
))
424 list_for_each_entry(child
, &linkbus
->devices
, bus_list
) {
426 * If downstream component of a link is pci bridge, we
427 * disable ASPM for now for the link
429 if (child
->pcie_type
== PCI_EXP_TYPE_PCI_BRIDGE
)
432 if ((child
->pcie_type
!= PCI_EXP_TYPE_ENDPOINT
&&
433 child
->pcie_type
!= PCI_EXP_TYPE_LEG_END
))
435 /* Device not in D0 doesn't need check latency */
436 power_state
= child
->current_state
;
437 if (power_state
== PCI_D1
|| power_state
== PCI_D2
||
438 power_state
== PCI_D3hot
|| power_state
== PCI_D3cold
)
440 state
= __pcie_aspm_check_state_one(child
, state
);
445 static void __pcie_aspm_config_one_dev(struct pci_dev
*pdev
, unsigned int state
)
448 int pos
= pci_find_capability(pdev
, PCI_CAP_ID_EXP
);
450 pci_read_config_word(pdev
, pos
+ PCI_EXP_LNKCTL
, ®16
);
453 pci_write_config_word(pdev
, pos
+ PCI_EXP_LNKCTL
, reg16
);
456 static void __pcie_aspm_config_link(struct pcie_link_state
*link
, u32 state
)
458 struct pci_dev
*child
, *parent
= link
->pdev
;
459 struct pci_bus
*linkbus
= parent
->subordinate
;
461 /* If no child, disable the link */
462 if (list_empty(&linkbus
->devices
))
465 * If the downstream component has pci bridge function, don't
468 list_for_each_entry(child
, &linkbus
->devices
, bus_list
) {
469 if (child
->pcie_type
== PCI_EXP_TYPE_PCI_BRIDGE
)
473 * Spec 2.0 suggests all functions should be configured the
474 * same setting for ASPM. Enabling ASPM L1 should be done in
475 * upstream component first and then downstream, and vice
476 * versa for disabling ASPM L1. Spec doesn't mention L0S.
478 if (state
& PCIE_LINK_STATE_L1
)
479 __pcie_aspm_config_one_dev(parent
, state
);
481 list_for_each_entry(child
, &linkbus
->devices
, bus_list
)
482 __pcie_aspm_config_one_dev(child
, state
);
484 if (!(state
& PCIE_LINK_STATE_L1
))
485 __pcie_aspm_config_one_dev(parent
, state
);
487 link
->aspm_enabled
= state
;
490 /* Check the whole hierarchy, and configure each link in the hierarchy */
491 static void __pcie_aspm_configure_link_state(struct pcie_link_state
*link
,
494 struct pcie_link_state
*leaf
, *root
= link
->root
;
496 state
&= (PCIE_LINK_STATE_L0S
| PCIE_LINK_STATE_L1
);
498 /* Check all links who have specific root port link */
499 list_for_each_entry(leaf
, &link_list
, sibling
) {
500 if (!list_empty(&leaf
->children
) || (leaf
->root
!= root
))
502 state
= pcie_aspm_check_state(leaf
, state
);
504 /* Check root port link too in case it hasn't children */
505 state
= pcie_aspm_check_state(root
, state
);
506 if (link
->aspm_enabled
== state
)
509 * We must change the hierarchy. See comments in
510 * __pcie_aspm_config_link for the order
512 if (state
& PCIE_LINK_STATE_L1
) {
513 list_for_each_entry(leaf
, &link_list
, sibling
) {
514 if (leaf
->root
== root
)
515 __pcie_aspm_config_link(leaf
, state
);
518 list_for_each_entry_reverse(leaf
, &link_list
, sibling
) {
519 if (leaf
->root
== root
)
520 __pcie_aspm_config_link(leaf
, state
);
526 * pcie_aspm_configure_link_state: enable/disable PCI express link state
527 * @pdev: the root port or switch downstream port
529 static void pcie_aspm_configure_link_state(struct pcie_link_state
*link
,
532 down_read(&pci_bus_sem
);
533 mutex_lock(&aspm_lock
);
534 __pcie_aspm_configure_link_state(link
, state
);
535 mutex_unlock(&aspm_lock
);
536 up_read(&pci_bus_sem
);
539 static void free_link_state(struct pcie_link_state
*link
)
541 link
->pdev
->link_state
= NULL
;
545 static int pcie_aspm_sanity_check(struct pci_dev
*pdev
)
547 struct pci_dev
*child
;
551 * Some functions in a slot might not all be PCIE functions,
552 * very strange. Disable ASPM for the whole slot
554 list_for_each_entry(child
, &pdev
->subordinate
->devices
, bus_list
) {
555 pos
= pci_find_capability(child
, PCI_CAP_ID_EXP
);
559 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
560 * RBER bit to determine if a function is 1.1 version device
562 pci_read_config_dword(child
, pos
+ PCI_EXP_DEVCAP
, ®32
);
563 if (!(reg32
& PCI_EXP_DEVCAP_RBER
) && !aspm_force
) {
564 dev_printk(KERN_INFO
, &child
->dev
, "disabling ASPM"
565 " on pre-1.1 PCIe device. You can enable it"
566 " with 'pcie_aspm=force'\n");
573 static struct pcie_link_state
*pcie_aspm_setup_link_state(struct pci_dev
*pdev
)
575 struct pcie_link_state
*link
;
576 int blacklist
= !!pcie_aspm_sanity_check(pdev
);
578 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
581 INIT_LIST_HEAD(&link
->sibling
);
582 INIT_LIST_HEAD(&link
->children
);
583 INIT_LIST_HEAD(&link
->link
);
585 if (pdev
->pcie_type
== PCI_EXP_TYPE_DOWNSTREAM
) {
586 struct pcie_link_state
*parent
;
587 parent
= pdev
->bus
->parent
->self
->link_state
;
592 link
->parent
= parent
;
593 list_add(&link
->link
, &parent
->children
);
595 /* Setup a pointer to the root port link */
599 link
->root
= link
->parent
->root
;
601 list_add(&link
->sibling
, &link_list
);
603 pdev
->link_state
= link
;
605 /* Check ASPM capability */
606 pcie_aspm_cap_init(link
, blacklist
);
608 /* Check Clock PM capability */
609 pcie_clkpm_cap_init(link
, blacklist
);
615 * pcie_aspm_init_link_state: Initiate PCI express link state.
616 * It is called after the pcie and its children devices are scaned.
617 * @pdev: the root port or switch downstream port
619 void pcie_aspm_init_link_state(struct pci_dev
*pdev
)
622 struct pcie_link_state
*link
;
624 if (aspm_disabled
|| !pdev
->is_pcie
|| pdev
->link_state
)
626 if (pdev
->pcie_type
!= PCI_EXP_TYPE_ROOT_PORT
&&
627 pdev
->pcie_type
!= PCI_EXP_TYPE_DOWNSTREAM
)
630 /* VIA has a strange chipset, root port is under a bridge */
631 if (pdev
->pcie_type
== PCI_EXP_TYPE_ROOT_PORT
&&
635 down_read(&pci_bus_sem
);
636 if (list_empty(&pdev
->subordinate
->devices
))
639 mutex_lock(&aspm_lock
);
640 link
= pcie_aspm_setup_link_state(pdev
);
644 * Setup initial ASPM state
646 * If link has switch, delay the link config. The leaf link
647 * initialization will config the whole hierarchy. But we must
648 * make sure BIOS doesn't set unsupported link state.
650 if (pcie_aspm_downstream_has_switch(link
)) {
651 state
= pcie_aspm_check_state(link
, link
->aspm_default
);
652 __pcie_aspm_config_link(link
, state
);
654 state
= policy_to_aspm_state(link
);
655 __pcie_aspm_configure_link_state(link
, state
);
658 /* Setup initial Clock PM state */
659 state
= (link
->clkpm_capable
) ? policy_to_clkpm_state(link
) : 0;
660 pcie_set_clkpm(link
, state
);
662 mutex_unlock(&aspm_lock
);
664 up_read(&pci_bus_sem
);
667 /* @pdev: the endpoint device */
668 void pcie_aspm_exit_link_state(struct pci_dev
*pdev
)
670 struct pci_dev
*parent
= pdev
->bus
->self
;
671 struct pcie_link_state
*link_state
= parent
->link_state
;
673 if (aspm_disabled
|| !pdev
->is_pcie
|| !parent
|| !link_state
)
675 if (parent
->pcie_type
!= PCI_EXP_TYPE_ROOT_PORT
&&
676 parent
->pcie_type
!= PCI_EXP_TYPE_DOWNSTREAM
)
678 down_read(&pci_bus_sem
);
679 mutex_lock(&aspm_lock
);
682 * All PCIe functions are in one slot, remove one function will remove
683 * the whole slot, so just wait until we are the last function left.
685 if (!list_is_last(&pdev
->bus_list
, &parent
->subordinate
->devices
))
688 /* All functions are removed, so just disable ASPM for the link */
689 __pcie_aspm_config_one_dev(parent
, 0);
690 list_del(&link_state
->sibling
);
691 list_del(&link_state
->link
);
692 /* Clock PM is for endpoint device */
694 free_link_state(link_state
);
696 mutex_unlock(&aspm_lock
);
697 up_read(&pci_bus_sem
);
700 /* @pdev: the root port or switch downstream port */
701 void pcie_aspm_pm_state_change(struct pci_dev
*pdev
)
703 struct pcie_link_state
*link_state
= pdev
->link_state
;
705 if (aspm_disabled
|| !pdev
->is_pcie
|| !pdev
->link_state
)
707 if (pdev
->pcie_type
!= PCI_EXP_TYPE_ROOT_PORT
&&
708 pdev
->pcie_type
!= PCI_EXP_TYPE_DOWNSTREAM
)
711 * devices changed PM state, we should recheck if latency meets all
712 * functions' requirement
714 pcie_aspm_configure_link_state(link_state
, link_state
->aspm_enabled
);
718 * pci_disable_link_state - disable pci device's link state, so the link will
719 * never enter specific states
721 void pci_disable_link_state(struct pci_dev
*pdev
, int state
)
723 struct pci_dev
*parent
= pdev
->bus
->self
;
724 struct pcie_link_state
*link_state
;
726 if (aspm_disabled
|| !pdev
->is_pcie
)
728 if (pdev
->pcie_type
== PCI_EXP_TYPE_ROOT_PORT
||
729 pdev
->pcie_type
== PCI_EXP_TYPE_DOWNSTREAM
)
731 if (!parent
|| !parent
->link_state
)
734 down_read(&pci_bus_sem
);
735 mutex_lock(&aspm_lock
);
736 link_state
= parent
->link_state
;
737 link_state
->aspm_support
&= ~state
;
738 __pcie_aspm_configure_link_state(link_state
, link_state
->aspm_enabled
);
739 if (state
& PCIE_LINK_STATE_CLKPM
) {
740 link_state
->clkpm_capable
= 0;
741 pcie_set_clkpm(link_state
, 0);
743 mutex_unlock(&aspm_lock
);
744 up_read(&pci_bus_sem
);
746 EXPORT_SYMBOL(pci_disable_link_state
);
748 static int pcie_aspm_set_policy(const char *val
, struct kernel_param
*kp
)
751 struct pcie_link_state
*link_state
;
753 for (i
= 0; i
< ARRAY_SIZE(policy_str
); i
++)
754 if (!strncmp(val
, policy_str
[i
], strlen(policy_str
[i
])))
756 if (i
>= ARRAY_SIZE(policy_str
))
758 if (i
== aspm_policy
)
761 down_read(&pci_bus_sem
);
762 mutex_lock(&aspm_lock
);
764 list_for_each_entry(link_state
, &link_list
, sibling
) {
765 __pcie_aspm_configure_link_state(link_state
,
766 policy_to_aspm_state(link_state
));
767 pcie_set_clkpm(link_state
, policy_to_clkpm_state(link_state
));
769 mutex_unlock(&aspm_lock
);
770 up_read(&pci_bus_sem
);
774 static int pcie_aspm_get_policy(char *buffer
, struct kernel_param
*kp
)
777 for (i
= 0; i
< ARRAY_SIZE(policy_str
); i
++)
778 if (i
== aspm_policy
)
779 cnt
+= sprintf(buffer
+ cnt
, "[%s] ", policy_str
[i
]);
781 cnt
+= sprintf(buffer
+ cnt
, "%s ", policy_str
[i
]);
785 module_param_call(policy
, pcie_aspm_set_policy
, pcie_aspm_get_policy
,
788 #ifdef CONFIG_PCIEASPM_DEBUG
789 static ssize_t
link_state_show(struct device
*dev
,
790 struct device_attribute
*attr
,
793 struct pci_dev
*pci_device
= to_pci_dev(dev
);
794 struct pcie_link_state
*link_state
= pci_device
->link_state
;
796 return sprintf(buf
, "%d\n", link_state
->aspm_enabled
);
799 static ssize_t
link_state_store(struct device
*dev
,
800 struct device_attribute
*attr
,
804 struct pci_dev
*pdev
= to_pci_dev(dev
);
810 if (state
>= 0 && state
<= 3) {
811 /* setup link aspm state */
812 pcie_aspm_configure_link_state(pdev
->link_state
, state
);
819 static ssize_t
clk_ctl_show(struct device
*dev
,
820 struct device_attribute
*attr
,
823 struct pci_dev
*pci_device
= to_pci_dev(dev
);
824 struct pcie_link_state
*link_state
= pci_device
->link_state
;
826 return sprintf(buf
, "%d\n", link_state
->clkpm_enabled
);
829 static ssize_t
clk_ctl_store(struct device
*dev
,
830 struct device_attribute
*attr
,
834 struct pci_dev
*pdev
= to_pci_dev(dev
);
841 down_read(&pci_bus_sem
);
842 mutex_lock(&aspm_lock
);
843 pcie_set_clkpm_nocheck(pdev
->link_state
, !!state
);
844 mutex_unlock(&aspm_lock
);
845 up_read(&pci_bus_sem
);
850 static DEVICE_ATTR(link_state
, 0644, link_state_show
, link_state_store
);
851 static DEVICE_ATTR(clk_ctl
, 0644, clk_ctl_show
, clk_ctl_store
);
853 static char power_group
[] = "power";
854 void pcie_aspm_create_sysfs_dev_files(struct pci_dev
*pdev
)
856 struct pcie_link_state
*link_state
= pdev
->link_state
;
858 if (!pdev
->is_pcie
|| (pdev
->pcie_type
!= PCI_EXP_TYPE_ROOT_PORT
&&
859 pdev
->pcie_type
!= PCI_EXP_TYPE_DOWNSTREAM
) || !link_state
)
862 if (link_state
->aspm_support
)
863 sysfs_add_file_to_group(&pdev
->dev
.kobj
,
864 &dev_attr_link_state
.attr
, power_group
);
865 if (link_state
->clkpm_capable
)
866 sysfs_add_file_to_group(&pdev
->dev
.kobj
,
867 &dev_attr_clk_ctl
.attr
, power_group
);
870 void pcie_aspm_remove_sysfs_dev_files(struct pci_dev
*pdev
)
872 struct pcie_link_state
*link_state
= pdev
->link_state
;
874 if (!pdev
->is_pcie
|| (pdev
->pcie_type
!= PCI_EXP_TYPE_ROOT_PORT
&&
875 pdev
->pcie_type
!= PCI_EXP_TYPE_DOWNSTREAM
) || !link_state
)
878 if (link_state
->aspm_support
)
879 sysfs_remove_file_from_group(&pdev
->dev
.kobj
,
880 &dev_attr_link_state
.attr
, power_group
);
881 if (link_state
->clkpm_capable
)
882 sysfs_remove_file_from_group(&pdev
->dev
.kobj
,
883 &dev_attr_clk_ctl
.attr
, power_group
);
887 static int __init
pcie_aspm_disable(char *str
)
889 if (!strcmp(str
, "off")) {
891 printk(KERN_INFO
"PCIe ASPM is disabled\n");
892 } else if (!strcmp(str
, "force")) {
894 printk(KERN_INFO
"PCIe ASPM is forcedly enabled\n");
899 __setup("pcie_aspm=", pcie_aspm_disable
);
901 void pcie_no_aspm(void)
908 * pcie_aspm_enabled - is PCIe ASPM enabled?
910 * Returns true if ASPM has not been disabled by the command-line option
913 int pcie_aspm_enabled(void)
915 return !aspm_disabled
;
917 EXPORT_SYMBOL(pcie_aspm_enabled
);