1 // SPDX-License-Identifier: GPL-2.0
3 * Enable 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>
23 #ifdef MODULE_PARAM_PREFIX
24 #undef MODULE_PARAM_PREFIX
26 #define MODULE_PARAM_PREFIX "pcie_aspm."
28 /* Note: those are not register definitions */
29 #define ASPM_STATE_L0S_UP (1) /* Upstream direction L0s state */
30 #define ASPM_STATE_L0S_DW (2) /* Downstream direction L0s state */
31 #define ASPM_STATE_L1 (4) /* L1 state */
32 #define ASPM_STATE_L1_1 (8) /* ASPM L1.1 state */
33 #define ASPM_STATE_L1_2 (0x10) /* ASPM L1.2 state */
34 #define ASPM_STATE_L1_1_PCIPM (0x20) /* PCI PM L1.1 state */
35 #define ASPM_STATE_L1_2_PCIPM (0x40) /* PCI PM L1.2 state */
36 #define ASPM_STATE_L1_SS_PCIPM (ASPM_STATE_L1_1_PCIPM | ASPM_STATE_L1_2_PCIPM)
37 #define ASPM_STATE_L1_2_MASK (ASPM_STATE_L1_2 | ASPM_STATE_L1_2_PCIPM)
38 #define ASPM_STATE_L1SS (ASPM_STATE_L1_1 | ASPM_STATE_L1_1_PCIPM |\
40 #define ASPM_STATE_L0S (ASPM_STATE_L0S_UP | ASPM_STATE_L0S_DW)
41 #define ASPM_STATE_ALL (ASPM_STATE_L0S | ASPM_STATE_L1 | \
45 u32 l0s; /* L0s latency (nsec) */
46 u32 l1; /* L1 latency (nsec) */
49 struct pcie_link_state {
50 struct pci_dev *pdev; /* Upstream component of the Link */
51 struct pci_dev *downstream; /* Downstream component, function 0 */
52 struct pcie_link_state *root; /* pointer to the root port link */
53 struct pcie_link_state *parent; /* pointer to the parent Link state */
54 struct list_head sibling; /* node in link_list */
57 u32 aspm_support:7; /* Supported ASPM state */
58 u32 aspm_enabled:7; /* Enabled ASPM state */
59 u32 aspm_capable:7; /* Capable ASPM state with latency */
60 u32 aspm_default:7; /* Default ASPM state by BIOS */
61 u32 aspm_disable:7; /* Disabled ASPM state */
64 u32 clkpm_capable:1; /* Clock PM capable? */
65 u32 clkpm_enabled:1; /* Current Clock PM state */
66 u32 clkpm_default:1; /* Default Clock PM state by BIOS */
67 u32 clkpm_disable:1; /* Clock PM disabled */
70 struct aspm_latency latency_up; /* Upstream direction exit latency */
71 struct aspm_latency latency_dw; /* Downstream direction exit latency */
73 * Endpoint acceptable latencies. A pcie downstream port only
74 * has one slot under it, so at most there are 8 functions.
76 struct aspm_latency acceptable[8];
79 static int aspm_disabled, aspm_force;
80 static bool aspm_support_enabled = true;
81 static DEFINE_MUTEX(aspm_lock);
82 static LIST_HEAD(link_list);
84 #define POLICY_DEFAULT 0 /* BIOS default setting */
85 #define POLICY_PERFORMANCE 1 /* high performance */
86 #define POLICY_POWERSAVE 2 /* high power saving */
87 #define POLICY_POWER_SUPERSAVE 3 /* possibly even more power saving */
89 #ifdef CONFIG_PCIEASPM_PERFORMANCE
90 static int aspm_policy = POLICY_PERFORMANCE;
91 #elif defined CONFIG_PCIEASPM_POWERSAVE
92 static int aspm_policy = POLICY_POWERSAVE;
93 #elif defined CONFIG_PCIEASPM_POWER_SUPERSAVE
94 static int aspm_policy = POLICY_POWER_SUPERSAVE;
96 static int aspm_policy;
99 static const char *policy_str[] = {
100 [POLICY_DEFAULT] = "default",
101 [POLICY_PERFORMANCE] = "performance",
102 [POLICY_POWERSAVE] = "powersave",
103 [POLICY_POWER_SUPERSAVE] = "powersupersave"
106 #define LINK_RETRAIN_TIMEOUT HZ
108 static int policy_to_aspm_state(struct pcie_link_state *link)
110 switch (aspm_policy) {
111 case POLICY_PERFORMANCE:
112 /* Disable ASPM and Clock PM */
114 case POLICY_POWERSAVE:
115 /* Enable ASPM L0s/L1 */
116 return (ASPM_STATE_L0S | ASPM_STATE_L1);
117 case POLICY_POWER_SUPERSAVE:
118 /* Enable Everything */
119 return ASPM_STATE_ALL;
121 return link->aspm_default;
126 static int policy_to_clkpm_state(struct pcie_link_state *link)
128 switch (aspm_policy) {
129 case POLICY_PERFORMANCE:
130 /* Disable ASPM and Clock PM */
132 case POLICY_POWERSAVE:
133 case POLICY_POWER_SUPERSAVE:
134 /* Enable Clock PM */
137 return link->clkpm_default;
142 static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
144 struct pci_dev *child;
145 struct pci_bus *linkbus = link->pdev->subordinate;
146 u32 val = enable ? PCI_EXP_LNKCTL_CLKREQ_EN : 0;
148 list_for_each_entry(child, &linkbus->devices, bus_list)
149 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
150 PCI_EXP_LNKCTL_CLKREQ_EN,
152 link->clkpm_enabled = !!enable;
155 static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
158 * Don't enable Clock PM if the link is not Clock PM capable
159 * or Clock PM is disabled
161 if (!link->clkpm_capable || link->clkpm_disable)
163 /* Need nothing if the specified equals to current state */
164 if (link->clkpm_enabled == enable)
166 pcie_set_clkpm_nocheck(link, enable);
169 static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
171 int capable = 1, enabled = 1;
174 struct pci_dev *child;
175 struct pci_bus *linkbus = link->pdev->subordinate;
177 /* All functions should have the same cap and state, take the worst */
178 list_for_each_entry(child, &linkbus->devices, bus_list) {
179 pcie_capability_read_dword(child, PCI_EXP_LNKCAP, ®32);
180 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
185 pcie_capability_read_word(child, PCI_EXP_LNKCTL, ®16);
186 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
189 link->clkpm_enabled = enabled;
190 link->clkpm_default = enabled;
191 link->clkpm_capable = capable;
192 link->clkpm_disable = blacklist ? 1 : 0;
195 static int pcie_wait_for_retrain(struct pci_dev *pdev)
197 unsigned long end_jiffies;
200 /* Wait for Link Training to be cleared by hardware */
201 end_jiffies = jiffies + LINK_RETRAIN_TIMEOUT;
203 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, ®16);
204 if (!(reg16 & PCI_EXP_LNKSTA_LT))
207 } while (time_before(jiffies, end_jiffies));
212 static int pcie_retrain_link(struct pcie_link_state *link)
214 struct pci_dev *parent = link->pdev;
219 * Ensure the updated LNKCTL parameters are used during link
220 * training by checking that there is no ongoing link training to
221 * avoid LTSSM race as recommended in Implementation Note at the
222 * end of PCIe r6.0.1 sec 7.5.3.7.
224 rc = pcie_wait_for_retrain(parent);
228 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16);
229 reg16 |= PCI_EXP_LNKCTL_RL;
230 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
231 if (parent->clear_retrain_link) {
233 * Due to an erratum in some devices the Retrain Link bit
234 * needs to be cleared again manually to allow the link
235 * training to succeed.
237 reg16 &= ~PCI_EXP_LNKCTL_RL;
238 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
241 return pcie_wait_for_retrain(parent);
245 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
246 * could use common clock. If they are, configure them to use the
247 * common clock. That will reduce the ASPM state exit latency.
249 static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
252 u16 reg16, ccc, parent_old_ccc, child_old_ccc[8];
253 struct pci_dev *child, *parent = link->pdev;
254 struct pci_bus *linkbus = parent->subordinate;
256 * All functions of a slot should have the same Slot Clock
257 * Configuration, so just check one function
259 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
260 BUG_ON(!pci_is_pcie(child));
262 /* Check downstream component if bit Slot Clock Configuration is 1 */
263 pcie_capability_read_word(child, PCI_EXP_LNKSTA, ®16);
264 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
267 /* Check upstream component if bit Slot Clock Configuration is 1 */
268 pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16);
269 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
272 /* Port might be already in common clock mode */
273 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16);
274 parent_old_ccc = reg16 & PCI_EXP_LNKCTL_CCC;
275 if (same_clock && (reg16 & PCI_EXP_LNKCTL_CCC)) {
276 bool consistent = true;
278 list_for_each_entry(child, &linkbus->devices, bus_list) {
279 pcie_capability_read_word(child, PCI_EXP_LNKCTL,
281 if (!(reg16 & PCI_EXP_LNKCTL_CCC)) {
288 pci_info(parent, "ASPM: current common clock configuration is inconsistent, reconfiguring\n");
291 ccc = same_clock ? PCI_EXP_LNKCTL_CCC : 0;
292 /* Configure downstream component, all functions */
293 list_for_each_entry(child, &linkbus->devices, bus_list) {
294 pcie_capability_read_word(child, PCI_EXP_LNKCTL, ®16);
295 child_old_ccc[PCI_FUNC(child->devfn)] = reg16 & PCI_EXP_LNKCTL_CCC;
296 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
297 PCI_EXP_LNKCTL_CCC, ccc);
300 /* Configure upstream component */
301 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
302 PCI_EXP_LNKCTL_CCC, ccc);
304 if (pcie_retrain_link(link)) {
306 /* Training failed. Restore common clock configurations */
307 pci_err(parent, "ASPM: Could not configure common clock\n");
308 list_for_each_entry(child, &linkbus->devices, bus_list)
309 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
311 child_old_ccc[PCI_FUNC(child->devfn)]);
312 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
313 PCI_EXP_LNKCTL_CCC, parent_old_ccc);
317 /* Convert L0s latency encoding to ns */
318 static u32 calc_l0s_latency(u32 lnkcap)
320 u32 encoding = (lnkcap & PCI_EXP_LNKCAP_L0SEL) >> 12;
323 return (5 * 1000); /* > 4us */
324 return (64 << encoding);
327 /* Convert L0s acceptable latency encoding to ns */
328 static u32 calc_l0s_acceptable(u32 encoding)
332 return (64 << encoding);
335 /* Convert L1 latency encoding to ns */
336 static u32 calc_l1_latency(u32 lnkcap)
338 u32 encoding = (lnkcap & PCI_EXP_LNKCAP_L1EL) >> 15;
341 return (65 * 1000); /* > 64us */
342 return (1000 << encoding);
345 /* Convert L1 acceptable latency encoding to ns */
346 static u32 calc_l1_acceptable(u32 encoding)
350 return (1000 << encoding);
353 /* Convert L1SS T_pwr encoding to usec */
354 static u32 calc_l1ss_pwron(struct pci_dev *pdev, u32 scale, u32 val)
364 pci_err(pdev, "%s: Invalid T_PwrOn scale: %u\n", __func__, scale);
368 static void encode_l12_threshold(u32 threshold_us, u32 *scale, u32 *value)
370 u32 threshold_ns = threshold_us * 1000;
372 /* See PCIe r3.1, sec 7.33.3 and sec 6.18 */
373 if (threshold_ns < 32) {
375 *value = threshold_ns;
376 } else if (threshold_ns < 1024) {
378 *value = threshold_ns >> 5;
379 } else if (threshold_ns < 32768) {
381 *value = threshold_ns >> 10;
382 } else if (threshold_ns < 1048576) {
384 *value = threshold_ns >> 15;
385 } else if (threshold_ns < 33554432) {
387 *value = threshold_ns >> 20;
390 *value = threshold_ns >> 25;
394 static void pcie_aspm_check_latency(struct pci_dev *endpoint)
396 u32 latency, l1_switch_latency = 0;
397 struct aspm_latency *acceptable;
398 struct pcie_link_state *link;
400 /* Device not in D0 doesn't need latency check */
401 if ((endpoint->current_state != PCI_D0) &&
402 (endpoint->current_state != PCI_UNKNOWN))
405 link = endpoint->bus->self->link_state;
406 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
409 /* Check upstream direction L0s latency */
410 if ((link->aspm_capable & ASPM_STATE_L0S_UP) &&
411 (link->latency_up.l0s > acceptable->l0s))
412 link->aspm_capable &= ~ASPM_STATE_L0S_UP;
414 /* Check downstream direction L0s latency */
415 if ((link->aspm_capable & ASPM_STATE_L0S_DW) &&
416 (link->latency_dw.l0s > acceptable->l0s))
417 link->aspm_capable &= ~ASPM_STATE_L0S_DW;
420 * Every switch on the path to root complex need 1
421 * more microsecond for L1. Spec doesn't mention L0s.
423 * The exit latencies for L1 substates are not advertised
424 * by a device. Since the spec also doesn't mention a way
425 * to determine max latencies introduced by enabling L1
426 * substates on the components, it is not clear how to do
427 * a L1 substate exit latency check. We assume that the
428 * L1 exit latencies advertised by a device include L1
429 * substate latencies (and hence do not do any check).
431 latency = max_t(u32, link->latency_up.l1, link->latency_dw.l1);
432 if ((link->aspm_capable & ASPM_STATE_L1) &&
433 (latency + l1_switch_latency > acceptable->l1))
434 link->aspm_capable &= ~ASPM_STATE_L1;
435 l1_switch_latency += 1000;
442 * The L1 PM substate capability is only implemented in function 0 in a
443 * multi function device.
445 static struct pci_dev *pci_function_0(struct pci_bus *linkbus)
447 struct pci_dev *child;
449 list_for_each_entry(child, &linkbus->devices, bus_list)
450 if (PCI_FUNC(child->devfn) == 0)
455 static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos,
460 pci_read_config_dword(pdev, pos, &val);
463 pci_write_config_dword(pdev, pos, val);
466 /* Calculate L1.2 PM substate timing parameters */
467 static void aspm_calc_l1ss_info(struct pcie_link_state *link,
468 u32 parent_l1ss_cap, u32 child_l1ss_cap)
470 struct pci_dev *child = link->downstream, *parent = link->pdev;
471 u32 val1, val2, scale1, scale2;
472 u32 t_common_mode, t_power_on, l1_2_threshold, scale, value;
473 u32 ctl1 = 0, ctl2 = 0;
474 u32 pctl1, pctl2, cctl1, cctl2;
475 u32 pl1_2_enables, cl1_2_enables;
477 if (!(link->aspm_support & ASPM_STATE_L1_2_MASK))
480 /* Choose the greater of the two Port Common_Mode_Restore_Times */
481 val1 = (parent_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
482 val2 = (child_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
483 t_common_mode = max(val1, val2);
485 /* Choose the greater of the two Port T_POWER_ON times */
486 val1 = (parent_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
487 scale1 = (parent_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
488 val2 = (child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
489 scale2 = (child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
491 if (calc_l1ss_pwron(parent, scale1, val1) >
492 calc_l1ss_pwron(child, scale2, val2)) {
493 ctl2 |= scale1 | (val1 << 3);
494 t_power_on = calc_l1ss_pwron(parent, scale1, val1);
496 ctl2 |= scale2 | (val2 << 3);
497 t_power_on = calc_l1ss_pwron(child, scale2, val2);
501 * Set LTR_L1.2_THRESHOLD to the time required to transition the
502 * Link from L0 to L1.2 and back to L0 so we enter L1.2 only if
503 * downstream devices report (via LTR) that they can tolerate at
504 * least that much latency.
506 * Based on PCIe r3.1, sec 5.5.3.3.1, Figures 5-16 and 5-17, and
507 * Table 5-11. T(POWER_OFF) is at most 2us and T(L1.2) is at
510 l1_2_threshold = 2 + 4 + t_common_mode + t_power_on;
511 encode_l12_threshold(l1_2_threshold, &scale, &value);
512 ctl1 |= t_common_mode << 8 | scale << 29 | value << 16;
514 pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, &pctl1);
515 pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, &pctl2);
516 pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL1, &cctl1);
517 pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL2, &cctl2);
519 if (ctl1 == pctl1 && ctl1 == cctl1 &&
520 ctl2 == pctl2 && ctl2 == cctl2)
523 /* Disable L1.2 while updating. See PCIe r5.0, sec 5.5.4, 7.8.3.3 */
524 pl1_2_enables = pctl1 & PCI_L1SS_CTL1_L1_2_MASK;
525 cl1_2_enables = cctl1 & PCI_L1SS_CTL1_L1_2_MASK;
527 if (pl1_2_enables || cl1_2_enables) {
528 pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1,
529 PCI_L1SS_CTL1_L1_2_MASK, 0);
530 pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
531 PCI_L1SS_CTL1_L1_2_MASK, 0);
534 /* Program T_POWER_ON times in both ports */
535 pci_write_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, ctl2);
536 pci_write_config_dword(child, child->l1ss + PCI_L1SS_CTL2, ctl2);
538 /* Program Common_Mode_Restore_Time in upstream device */
539 pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
540 PCI_L1SS_CTL1_CM_RESTORE_TIME, ctl1);
542 /* Program LTR_L1.2_THRESHOLD time in both ports */
543 pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
544 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
545 PCI_L1SS_CTL1_LTR_L12_TH_SCALE, ctl1);
546 pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1,
547 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
548 PCI_L1SS_CTL1_LTR_L12_TH_SCALE, ctl1);
550 if (pl1_2_enables || cl1_2_enables) {
551 pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, 0,
553 pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, 0,
558 static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
560 struct pci_dev *child = link->downstream, *parent = link->pdev;
561 u32 parent_lnkcap, child_lnkcap;
562 u16 parent_lnkctl, child_lnkctl;
563 u32 parent_l1ss_cap, child_l1ss_cap;
564 u32 parent_l1ss_ctl1 = 0, child_l1ss_ctl1 = 0;
565 struct pci_bus *linkbus = parent->subordinate;
568 /* Set enabled/disable so that we will disable ASPM later */
569 link->aspm_enabled = ASPM_STATE_ALL;
570 link->aspm_disable = ASPM_STATE_ALL;
575 * If ASPM not supported, don't mess with the clocks and link,
578 pcie_capability_read_dword(parent, PCI_EXP_LNKCAP, &parent_lnkcap);
579 pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &child_lnkcap);
580 if (!(parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPMS))
583 /* Configure common clock before checking latencies */
584 pcie_aspm_configure_common_clock(link);
587 * Re-read upstream/downstream components' register state after
588 * clock configuration. L0s & L1 exit latencies in the otherwise
589 * read-only Link Capabilities may change depending on common clock
590 * configuration (PCIe r5.0, sec 7.5.3.6).
592 pcie_capability_read_dword(parent, PCI_EXP_LNKCAP, &parent_lnkcap);
593 pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &child_lnkcap);
594 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &parent_lnkctl);
595 pcie_capability_read_word(child, PCI_EXP_LNKCTL, &child_lnkctl);
600 * Note that we must not enable L0s in either direction on a
601 * given link unless components on both sides of the link each
604 if (parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPM_L0S)
605 link->aspm_support |= ASPM_STATE_L0S;
607 if (child_lnkctl & PCI_EXP_LNKCTL_ASPM_L0S)
608 link->aspm_enabled |= ASPM_STATE_L0S_UP;
609 if (parent_lnkctl & PCI_EXP_LNKCTL_ASPM_L0S)
610 link->aspm_enabled |= ASPM_STATE_L0S_DW;
611 link->latency_up.l0s = calc_l0s_latency(parent_lnkcap);
612 link->latency_dw.l0s = calc_l0s_latency(child_lnkcap);
615 if (parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPM_L1)
616 link->aspm_support |= ASPM_STATE_L1;
618 if (parent_lnkctl & child_lnkctl & PCI_EXP_LNKCTL_ASPM_L1)
619 link->aspm_enabled |= ASPM_STATE_L1;
620 link->latency_up.l1 = calc_l1_latency(parent_lnkcap);
621 link->latency_dw.l1 = calc_l1_latency(child_lnkcap);
623 /* Setup L1 substate */
624 pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CAP,
626 pci_read_config_dword(child, child->l1ss + PCI_L1SS_CAP,
629 if (!(parent_l1ss_cap & PCI_L1SS_CAP_L1_PM_SS))
631 if (!(child_l1ss_cap & PCI_L1SS_CAP_L1_PM_SS))
635 * If we don't have LTR for the entire path from the Root Complex
636 * to this device, we can't use ASPM L1.2 because it relies on the
637 * LTR_L1.2_THRESHOLD. See PCIe r4.0, secs 5.5.4, 6.18.
639 if (!child->ltr_path)
640 child_l1ss_cap &= ~PCI_L1SS_CAP_ASPM_L1_2;
642 if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1)
643 link->aspm_support |= ASPM_STATE_L1_1;
644 if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2)
645 link->aspm_support |= ASPM_STATE_L1_2;
646 if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1)
647 link->aspm_support |= ASPM_STATE_L1_1_PCIPM;
648 if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2)
649 link->aspm_support |= ASPM_STATE_L1_2_PCIPM;
652 pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
655 pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL1,
658 if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1)
659 link->aspm_enabled |= ASPM_STATE_L1_1;
660 if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2)
661 link->aspm_enabled |= ASPM_STATE_L1_2;
662 if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1)
663 link->aspm_enabled |= ASPM_STATE_L1_1_PCIPM;
664 if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2)
665 link->aspm_enabled |= ASPM_STATE_L1_2_PCIPM;
667 if (link->aspm_support & ASPM_STATE_L1SS)
668 aspm_calc_l1ss_info(link, parent_l1ss_cap, child_l1ss_cap);
670 /* Save default state */
671 link->aspm_default = link->aspm_enabled;
673 /* Setup initial capable state. Will be updated later */
674 link->aspm_capable = link->aspm_support;
676 /* Get and check endpoint acceptable latencies */
677 list_for_each_entry(child, &linkbus->devices, bus_list) {
679 struct aspm_latency *acceptable =
680 &link->acceptable[PCI_FUNC(child->devfn)];
682 if (pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT &&
683 pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END)
686 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, ®32);
687 /* Calculate endpoint L0s acceptable latency */
688 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
689 acceptable->l0s = calc_l0s_acceptable(encoding);
690 /* Calculate endpoint L1 acceptable latency */
691 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
692 acceptable->l1 = calc_l1_acceptable(encoding);
694 pcie_aspm_check_latency(child);
698 /* Configure the ASPM L1 substates */
699 static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state)
702 struct pci_dev *child = link->downstream, *parent = link->pdev;
704 enable_req = (link->aspm_enabled ^ state) & state;
707 * Here are the rules specified in the PCIe spec for enabling L1SS:
708 * - When enabling L1.x, enable bit at parent first, then at child
709 * - When disabling L1.x, disable bit at child first, then at parent
710 * - When enabling ASPM L1.x, need to disable L1
711 * (at child followed by parent).
712 * - The ASPM/PCIPM L1.2 must be disabled while programming timing
715 * To keep it simple, disable all L1SS bits first, and later enable
719 /* Disable all L1 substates */
720 pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1,
721 PCI_L1SS_CTL1_L1SS_MASK, 0);
722 pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
723 PCI_L1SS_CTL1_L1SS_MASK, 0);
725 * If needed, disable L1, and it gets enabled later
726 * in pcie_config_aspm_link().
728 if (enable_req & (ASPM_STATE_L1_1 | ASPM_STATE_L1_2)) {
729 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
730 PCI_EXP_LNKCTL_ASPM_L1, 0);
731 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
732 PCI_EXP_LNKCTL_ASPM_L1, 0);
736 if (state & ASPM_STATE_L1_1)
737 val |= PCI_L1SS_CTL1_ASPM_L1_1;
738 if (state & ASPM_STATE_L1_2)
739 val |= PCI_L1SS_CTL1_ASPM_L1_2;
740 if (state & ASPM_STATE_L1_1_PCIPM)
741 val |= PCI_L1SS_CTL1_PCIPM_L1_1;
742 if (state & ASPM_STATE_L1_2_PCIPM)
743 val |= PCI_L1SS_CTL1_PCIPM_L1_2;
745 /* Enable what we need to enable */
746 pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
747 PCI_L1SS_CTL1_L1SS_MASK, val);
748 pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1,
749 PCI_L1SS_CTL1_L1SS_MASK, val);
752 static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
754 pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
755 PCI_EXP_LNKCTL_ASPMC, val);
758 static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
760 u32 upstream = 0, dwstream = 0;
761 struct pci_dev *child = link->downstream, *parent = link->pdev;
762 struct pci_bus *linkbus = parent->subordinate;
764 /* Enable only the states that were not explicitly disabled */
765 state &= (link->aspm_capable & ~link->aspm_disable);
767 /* Can't enable any substates if L1 is not enabled */
768 if (!(state & ASPM_STATE_L1))
769 state &= ~ASPM_STATE_L1SS;
771 /* Spec says both ports must be in D0 before enabling PCI PM substates*/
772 if (parent->current_state != PCI_D0 || child->current_state != PCI_D0) {
773 state &= ~ASPM_STATE_L1_SS_PCIPM;
774 state |= (link->aspm_enabled & ASPM_STATE_L1_SS_PCIPM);
777 /* Nothing to do if the link is already in the requested state */
778 if (link->aspm_enabled == state)
780 /* Convert ASPM state to upstream/downstream ASPM register state */
781 if (state & ASPM_STATE_L0S_UP)
782 dwstream |= PCI_EXP_LNKCTL_ASPM_L0S;
783 if (state & ASPM_STATE_L0S_DW)
784 upstream |= PCI_EXP_LNKCTL_ASPM_L0S;
785 if (state & ASPM_STATE_L1) {
786 upstream |= PCI_EXP_LNKCTL_ASPM_L1;
787 dwstream |= PCI_EXP_LNKCTL_ASPM_L1;
790 if (link->aspm_capable & ASPM_STATE_L1SS)
791 pcie_config_aspm_l1ss(link, state);
794 * Spec 2.0 suggests all functions should be configured the
795 * same setting for ASPM. Enabling ASPM L1 should be done in
796 * upstream component first and then downstream, and vice
797 * versa for disabling ASPM L1. Spec doesn't mention L0S.
799 if (state & ASPM_STATE_L1)
800 pcie_config_aspm_dev(parent, upstream);
801 list_for_each_entry(child, &linkbus->devices, bus_list)
802 pcie_config_aspm_dev(child, dwstream);
803 if (!(state & ASPM_STATE_L1))
804 pcie_config_aspm_dev(parent, upstream);
806 link->aspm_enabled = state;
809 static void pcie_config_aspm_path(struct pcie_link_state *link)
812 pcie_config_aspm_link(link, policy_to_aspm_state(link));
817 static void free_link_state(struct pcie_link_state *link)
819 link->pdev->link_state = NULL;
823 static int pcie_aspm_sanity_check(struct pci_dev *pdev)
825 struct pci_dev *child;
829 * Some functions in a slot might not all be PCIe functions,
830 * very strange. Disable ASPM for the whole slot
832 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
833 if (!pci_is_pcie(child))
837 * If ASPM is disabled then we're not going to change
838 * the BIOS state. It's safe to continue even if it's a
846 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
847 * RBER bit to determine if a function is 1.1 version device
849 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, ®32);
850 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
851 pci_info(child, "disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force'\n");
858 static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
860 struct pcie_link_state *link;
862 link = kzalloc(sizeof(*link), GFP_KERNEL);
866 INIT_LIST_HEAD(&link->sibling);
868 link->downstream = pci_function_0(pdev->subordinate);
871 * Root Ports and PCI/PCI-X to PCIe Bridges are roots of PCIe
872 * hierarchies. Note that some PCIe host implementations omit
873 * the root ports entirely, in which case a downstream port on
874 * a switch may become the root of the link state chain for all
875 * its subordinate endpoints.
877 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
878 pci_pcie_type(pdev) == PCI_EXP_TYPE_PCIE_BRIDGE ||
879 !pdev->bus->parent->self) {
882 struct pcie_link_state *parent;
884 parent = pdev->bus->parent->self->link_state;
890 link->parent = parent;
891 link->root = link->parent->root;
894 list_add(&link->sibling, &link_list);
895 pdev->link_state = link;
899 static void pcie_aspm_update_sysfs_visibility(struct pci_dev *pdev)
901 struct pci_dev *child;
903 list_for_each_entry(child, &pdev->subordinate->devices, bus_list)
904 sysfs_update_group(&child->dev.kobj, &aspm_ctrl_attr_group);
908 * pcie_aspm_init_link_state: Initiate PCI express link state.
909 * It is called after the pcie and its children devices are scanned.
910 * @pdev: the root port or switch downstream port
912 void pcie_aspm_init_link_state(struct pci_dev *pdev)
914 struct pcie_link_state *link;
915 int blacklist = !!pcie_aspm_sanity_check(pdev);
917 if (!aspm_support_enabled)
920 if (pdev->link_state)
924 * We allocate pcie_link_state for the component on the upstream
925 * end of a Link, so there's nothing to do unless this device is
928 if (!pcie_downstream_port(pdev))
931 /* VIA has a strange chipset, root port is under a bridge */
932 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT &&
936 down_read(&pci_bus_sem);
937 if (list_empty(&pdev->subordinate->devices))
940 mutex_lock(&aspm_lock);
941 link = alloc_pcie_link_state(pdev);
945 * Setup initial ASPM state. Note that we need to configure
946 * upstream links also because capable state of them can be
947 * update through pcie_aspm_cap_init().
949 pcie_aspm_cap_init(link, blacklist);
951 /* Setup initial Clock PM state */
952 pcie_clkpm_cap_init(link, blacklist);
955 * At this stage drivers haven't had an opportunity to change the
956 * link policy setting. Enabling ASPM on broken hardware can cripple
957 * it even before the driver has had a chance to disable ASPM, so
958 * default to a safe level right now. If we're enabling ASPM beyond
959 * the BIOS's expectation, we'll do so once pci_enable_device() is
962 if (aspm_policy != POLICY_POWERSAVE &&
963 aspm_policy != POLICY_POWER_SUPERSAVE) {
964 pcie_config_aspm_path(link);
965 pcie_set_clkpm(link, policy_to_clkpm_state(link));
968 pcie_aspm_update_sysfs_visibility(pdev);
971 mutex_unlock(&aspm_lock);
973 up_read(&pci_bus_sem);
976 /* Recheck latencies and update aspm_capable for links under the root */
977 static void pcie_update_aspm_capable(struct pcie_link_state *root)
979 struct pcie_link_state *link;
980 BUG_ON(root->parent);
981 list_for_each_entry(link, &link_list, sibling) {
982 if (link->root != root)
984 link->aspm_capable = link->aspm_support;
986 list_for_each_entry(link, &link_list, sibling) {
987 struct pci_dev *child;
988 struct pci_bus *linkbus = link->pdev->subordinate;
989 if (link->root != root)
991 list_for_each_entry(child, &linkbus->devices, bus_list) {
992 if ((pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT) &&
993 (pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END))
995 pcie_aspm_check_latency(child);
1000 /* @pdev: the endpoint device */
1001 void pcie_aspm_exit_link_state(struct pci_dev *pdev)
1003 struct pci_dev *parent = pdev->bus->self;
1004 struct pcie_link_state *link, *root, *parent_link;
1006 if (!parent || !parent->link_state)
1009 down_read(&pci_bus_sem);
1010 mutex_lock(&aspm_lock);
1012 link = parent->link_state;
1014 parent_link = link->parent;
1017 * link->downstream is a pointer to the pci_dev of function 0. If
1018 * we remove that function, the pci_dev is about to be deallocated,
1019 * so we can't use link->downstream again. Free the link state to
1022 * If we're removing a non-0 function, it's possible we could
1023 * retain the link state, but PCIe r6.0, sec 7.5.3.7, recommends
1024 * programming the same ASPM Control value for all functions of
1025 * multi-function devices, so disable ASPM for all of them.
1027 pcie_config_aspm_link(link, 0);
1028 list_del(&link->sibling);
1029 free_link_state(link);
1031 /* Recheck latencies and configure upstream links */
1033 pcie_update_aspm_capable(root);
1034 pcie_config_aspm_path(parent_link);
1037 mutex_unlock(&aspm_lock);
1038 up_read(&pci_bus_sem);
1041 /* @pdev: the root port or switch downstream port */
1042 void pcie_aspm_pm_state_change(struct pci_dev *pdev)
1044 struct pcie_link_state *link = pdev->link_state;
1046 if (aspm_disabled || !link)
1049 * Devices changed PM state, we should recheck if latency
1050 * meets all functions' requirement
1052 down_read(&pci_bus_sem);
1053 mutex_lock(&aspm_lock);
1054 pcie_update_aspm_capable(link->root);
1055 pcie_config_aspm_path(link);
1056 mutex_unlock(&aspm_lock);
1057 up_read(&pci_bus_sem);
1060 void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
1062 struct pcie_link_state *link = pdev->link_state;
1064 if (aspm_disabled || !link)
1067 if (aspm_policy != POLICY_POWERSAVE &&
1068 aspm_policy != POLICY_POWER_SUPERSAVE)
1071 down_read(&pci_bus_sem);
1072 mutex_lock(&aspm_lock);
1073 pcie_config_aspm_path(link);
1074 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1075 mutex_unlock(&aspm_lock);
1076 up_read(&pci_bus_sem);
1079 static struct pcie_link_state *pcie_aspm_get_link(struct pci_dev *pdev)
1081 struct pci_dev *bridge;
1083 if (!pci_is_pcie(pdev))
1086 bridge = pci_upstream_bridge(pdev);
1087 if (!bridge || !pci_is_pcie(bridge))
1090 return bridge->link_state;
1093 static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
1095 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1100 * A driver requested that ASPM be disabled on this device, but
1101 * if we don't have permission to manage ASPM (e.g., on ACPI
1102 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
1103 * the _OSC method), we can't honor that request. Windows has
1104 * a similar mechanism using "PciASPMOptOut", which is also
1105 * ignored in this situation.
1107 if (aspm_disabled) {
1108 pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n");
1113 down_read(&pci_bus_sem);
1114 mutex_lock(&aspm_lock);
1115 if (state & PCIE_LINK_STATE_L0S)
1116 link->aspm_disable |= ASPM_STATE_L0S;
1117 if (state & PCIE_LINK_STATE_L1)
1118 /* L1 PM substates require L1 */
1119 link->aspm_disable |= ASPM_STATE_L1 | ASPM_STATE_L1SS;
1120 if (state & PCIE_LINK_STATE_L1_1)
1121 link->aspm_disable |= ASPM_STATE_L1_1;
1122 if (state & PCIE_LINK_STATE_L1_2)
1123 link->aspm_disable |= ASPM_STATE_L1_2;
1124 if (state & PCIE_LINK_STATE_L1_1_PCIPM)
1125 link->aspm_disable |= ASPM_STATE_L1_1_PCIPM;
1126 if (state & PCIE_LINK_STATE_L1_2_PCIPM)
1127 link->aspm_disable |= ASPM_STATE_L1_2_PCIPM;
1128 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1130 if (state & PCIE_LINK_STATE_CLKPM)
1131 link->clkpm_disable = 1;
1132 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1133 mutex_unlock(&aspm_lock);
1135 up_read(&pci_bus_sem);
1140 int pci_disable_link_state_locked(struct pci_dev *pdev, int state)
1142 return __pci_disable_link_state(pdev, state, false);
1144 EXPORT_SYMBOL(pci_disable_link_state_locked);
1147 * pci_disable_link_state - Disable device's link state, so the link will
1148 * never enter specific states. Note that if the BIOS didn't grant ASPM
1149 * control to the OS, this does nothing because we can't touch the LNKCTL
1150 * register. Returns 0 or a negative errno.
1153 * @state: ASPM link state to disable
1155 int pci_disable_link_state(struct pci_dev *pdev, int state)
1157 return __pci_disable_link_state(pdev, state, true);
1159 EXPORT_SYMBOL(pci_disable_link_state);
1161 static int pcie_aspm_set_policy(const char *val,
1162 const struct kernel_param *kp)
1165 struct pcie_link_state *link;
1169 i = sysfs_match_string(policy_str, val);
1172 if (i == aspm_policy)
1175 down_read(&pci_bus_sem);
1176 mutex_lock(&aspm_lock);
1178 list_for_each_entry(link, &link_list, sibling) {
1179 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1180 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1182 mutex_unlock(&aspm_lock);
1183 up_read(&pci_bus_sem);
1187 static int pcie_aspm_get_policy(char *buffer, const struct kernel_param *kp)
1190 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
1191 if (i == aspm_policy)
1192 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
1194 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
1195 cnt += sprintf(buffer + cnt, "\n");
1199 module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
1203 * pcie_aspm_enabled - Check if PCIe ASPM has been enabled for a device.
1204 * @pdev: Target device.
1206 * Relies on the upstream bridge's link_state being valid. The link_state
1207 * is deallocated only when the last child of the bridge (i.e., @pdev or a
1208 * sibling) is removed, and the caller should be holding a reference to
1209 * @pdev, so this should be safe.
1211 bool pcie_aspm_enabled(struct pci_dev *pdev)
1213 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1218 return link->aspm_enabled;
1220 EXPORT_SYMBOL_GPL(pcie_aspm_enabled);
1222 static ssize_t aspm_attr_show_common(struct device *dev,
1223 struct device_attribute *attr,
1224 char *buf, u8 state)
1226 struct pci_dev *pdev = to_pci_dev(dev);
1227 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1229 return sprintf(buf, "%d\n", (link->aspm_enabled & state) ? 1 : 0);
1232 static ssize_t aspm_attr_store_common(struct device *dev,
1233 struct device_attribute *attr,
1234 const char *buf, size_t len, u8 state)
1236 struct pci_dev *pdev = to_pci_dev(dev);
1237 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1240 if (strtobool(buf, &state_enable) < 0)
1243 down_read(&pci_bus_sem);
1244 mutex_lock(&aspm_lock);
1247 link->aspm_disable &= ~state;
1248 /* need to enable L1 for substates */
1249 if (state & ASPM_STATE_L1SS)
1250 link->aspm_disable &= ~ASPM_STATE_L1;
1252 link->aspm_disable |= state;
1253 if (state & ASPM_STATE_L1)
1254 link->aspm_disable |= ASPM_STATE_L1SS;
1257 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1259 mutex_unlock(&aspm_lock);
1260 up_read(&pci_bus_sem);
1265 #define ASPM_ATTR(_f, _s) \
1266 static ssize_t _f##_show(struct device *dev, \
1267 struct device_attribute *attr, char *buf) \
1268 { return aspm_attr_show_common(dev, attr, buf, ASPM_STATE_##_s); } \
1270 static ssize_t _f##_store(struct device *dev, \
1271 struct device_attribute *attr, \
1272 const char *buf, size_t len) \
1273 { return aspm_attr_store_common(dev, attr, buf, len, ASPM_STATE_##_s); }
1275 ASPM_ATTR(l0s_aspm, L0S)
1276 ASPM_ATTR(l1_aspm, L1)
1277 ASPM_ATTR(l1_1_aspm, L1_1)
1278 ASPM_ATTR(l1_2_aspm, L1_2)
1279 ASPM_ATTR(l1_1_pcipm, L1_1_PCIPM)
1280 ASPM_ATTR(l1_2_pcipm, L1_2_PCIPM)
1282 static ssize_t clkpm_show(struct device *dev,
1283 struct device_attribute *attr, char *buf)
1285 struct pci_dev *pdev = to_pci_dev(dev);
1286 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1288 return sprintf(buf, "%d\n", link->clkpm_enabled);
1291 static ssize_t clkpm_store(struct device *dev,
1292 struct device_attribute *attr,
1293 const char *buf, size_t len)
1295 struct pci_dev *pdev = to_pci_dev(dev);
1296 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1299 if (strtobool(buf, &state_enable) < 0)
1302 down_read(&pci_bus_sem);
1303 mutex_lock(&aspm_lock);
1305 link->clkpm_disable = !state_enable;
1306 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1308 mutex_unlock(&aspm_lock);
1309 up_read(&pci_bus_sem);
1314 static DEVICE_ATTR_RW(clkpm);
1315 static DEVICE_ATTR_RW(l0s_aspm);
1316 static DEVICE_ATTR_RW(l1_aspm);
1317 static DEVICE_ATTR_RW(l1_1_aspm);
1318 static DEVICE_ATTR_RW(l1_2_aspm);
1319 static DEVICE_ATTR_RW(l1_1_pcipm);
1320 static DEVICE_ATTR_RW(l1_2_pcipm);
1322 static struct attribute *aspm_ctrl_attrs[] = {
1323 &dev_attr_clkpm.attr,
1324 &dev_attr_l0s_aspm.attr,
1325 &dev_attr_l1_aspm.attr,
1326 &dev_attr_l1_1_aspm.attr,
1327 &dev_attr_l1_2_aspm.attr,
1328 &dev_attr_l1_1_pcipm.attr,
1329 &dev_attr_l1_2_pcipm.attr,
1333 static umode_t aspm_ctrl_attrs_are_visible(struct kobject *kobj,
1334 struct attribute *a, int n)
1336 struct device *dev = kobj_to_dev(kobj);
1337 struct pci_dev *pdev = to_pci_dev(dev);
1338 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1339 static const u8 aspm_state_map[] = {
1344 ASPM_STATE_L1_1_PCIPM,
1345 ASPM_STATE_L1_2_PCIPM,
1348 if (aspm_disabled || !link)
1352 return link->clkpm_capable ? a->mode : 0;
1354 return link->aspm_capable & aspm_state_map[n - 1] ? a->mode : 0;
1357 const struct attribute_group aspm_ctrl_attr_group = {
1359 .attrs = aspm_ctrl_attrs,
1360 .is_visible = aspm_ctrl_attrs_are_visible,
1363 static int __init pcie_aspm_disable(char *str)
1365 if (!strcmp(str, "off")) {
1366 aspm_policy = POLICY_DEFAULT;
1368 aspm_support_enabled = false;
1369 printk(KERN_INFO "PCIe ASPM is disabled\n");
1370 } else if (!strcmp(str, "force")) {
1372 printk(KERN_INFO "PCIe ASPM is forcibly enabled\n");
1377 __setup("pcie_aspm=", pcie_aspm_disable);
1379 void pcie_no_aspm(void)
1382 * Disabling ASPM is intended to prevent the kernel from modifying
1383 * existing hardware state, not to clear existing state. To that end:
1384 * (a) set policy to POLICY_DEFAULT in order to avoid changing state
1385 * (b) prevent userspace from changing policy
1388 aspm_policy = POLICY_DEFAULT;
1393 bool pcie_aspm_support_enabled(void)
1395 return aspm_support_enabled;
1397 EXPORT_SYMBOL(pcie_aspm_support_enabled);