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>
21 #include <linux/pci-aspm.h>
24 #ifdef MODULE_PARAM_PREFIX
25 #undef MODULE_PARAM_PREFIX
27 #define MODULE_PARAM_PREFIX "pcie_aspm."
29 /* Note: those are not register definitions */
30 #define ASPM_STATE_L0S_UP (1) /* Upstream direction L0s state */
31 #define ASPM_STATE_L0S_DW (2) /* Downstream direction L0s state */
32 #define ASPM_STATE_L1 (4) /* L1 state */
33 #define ASPM_STATE_L1_1 (8) /* ASPM L1.1 state */
34 #define ASPM_STATE_L1_2 (0x10) /* ASPM L1.2 state */
35 #define ASPM_STATE_L1_1_PCIPM (0x20) /* PCI PM L1.1 state */
36 #define ASPM_STATE_L1_2_PCIPM (0x40) /* PCI PM L1.2 state */
37 #define ASPM_STATE_L1_SS_PCIPM (ASPM_STATE_L1_1_PCIPM | ASPM_STATE_L1_2_PCIPM)
38 #define ASPM_STATE_L1_2_MASK (ASPM_STATE_L1_2 | ASPM_STATE_L1_2_PCIPM)
39 #define ASPM_STATE_L1SS (ASPM_STATE_L1_1 | ASPM_STATE_L1_1_PCIPM |\
41 #define ASPM_STATE_L0S (ASPM_STATE_L0S_UP | ASPM_STATE_L0S_DW)
42 #define ASPM_STATE_ALL (ASPM_STATE_L0S | ASPM_STATE_L1 | \
46 u32 l0s; /* L0s latency (nsec) */
47 u32 l1; /* L1 latency (nsec) */
50 struct pcie_link_state {
51 struct pci_dev *pdev; /* Upstream component of the Link */
52 struct pci_dev *downstream; /* Downstream component, function 0 */
53 struct pcie_link_state *root; /* pointer to the root port link */
54 struct pcie_link_state *parent; /* pointer to the parent Link state */
55 struct list_head sibling; /* node in link_list */
56 struct list_head children; /* list of child link states */
57 struct list_head link; /* node in parent's children list */
60 u32 aspm_support:7; /* Supported ASPM state */
61 u32 aspm_enabled:7; /* Enabled ASPM state */
62 u32 aspm_capable:7; /* Capable ASPM state with latency */
63 u32 aspm_default:7; /* Default ASPM state by BIOS */
64 u32 aspm_disable:7; /* Disabled ASPM state */
67 u32 clkpm_capable:1; /* Clock PM capable? */
68 u32 clkpm_enabled:1; /* Current Clock PM state */
69 u32 clkpm_default:1; /* Default Clock PM state by BIOS */
70 u32 clkpm_disable:1; /* Clock PM disabled */
73 struct aspm_latency latency_up; /* Upstream direction exit latency */
74 struct aspm_latency latency_dw; /* Downstream direction exit latency */
76 * Endpoint acceptable latencies. A pcie downstream port only
77 * has one slot under it, so at most there are 8 functions.
79 struct aspm_latency acceptable[8];
81 /* L1 PM Substate info */
83 u32 up_cap_ptr; /* L1SS cap ptr in upstream dev */
84 u32 dw_cap_ptr; /* L1SS cap ptr in downstream dev */
85 u32 ctl1; /* value to be programmed in ctl1 */
86 u32 ctl2; /* value to be programmed in ctl2 */
90 static int aspm_disabled, aspm_force;
91 static bool aspm_support_enabled = true;
92 static DEFINE_MUTEX(aspm_lock);
93 static LIST_HEAD(link_list);
95 #define POLICY_DEFAULT 0 /* BIOS default setting */
96 #define POLICY_PERFORMANCE 1 /* high performance */
97 #define POLICY_POWERSAVE 2 /* high power saving */
98 #define POLICY_POWER_SUPERSAVE 3 /* possibly even more power saving */
100 #ifdef CONFIG_PCIEASPM_PERFORMANCE
101 static int aspm_policy = POLICY_PERFORMANCE;
102 #elif defined CONFIG_PCIEASPM_POWERSAVE
103 static int aspm_policy = POLICY_POWERSAVE;
104 #elif defined CONFIG_PCIEASPM_POWER_SUPERSAVE
105 static int aspm_policy = POLICY_POWER_SUPERSAVE;
107 static int aspm_policy;
110 static const char *policy_str[] = {
111 [POLICY_DEFAULT] = "default",
112 [POLICY_PERFORMANCE] = "performance",
113 [POLICY_POWERSAVE] = "powersave",
114 [POLICY_POWER_SUPERSAVE] = "powersupersave"
117 #define LINK_RETRAIN_TIMEOUT HZ
119 static int policy_to_aspm_state(struct pcie_link_state *link)
121 switch (aspm_policy) {
122 case POLICY_PERFORMANCE:
123 /* Disable ASPM and Clock PM */
125 case POLICY_POWERSAVE:
126 /* Enable ASPM L0s/L1 */
127 return (ASPM_STATE_L0S | ASPM_STATE_L1);
128 case POLICY_POWER_SUPERSAVE:
129 /* Enable Everything */
130 return ASPM_STATE_ALL;
132 return link->aspm_default;
137 static int policy_to_clkpm_state(struct pcie_link_state *link)
139 switch (aspm_policy) {
140 case POLICY_PERFORMANCE:
141 /* Disable ASPM and Clock PM */
143 case POLICY_POWERSAVE:
144 case POLICY_POWER_SUPERSAVE:
145 /* Enable Clock PM */
148 return link->clkpm_default;
153 static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
155 struct pci_dev *child;
156 struct pci_bus *linkbus = link->pdev->subordinate;
157 u32 val = enable ? PCI_EXP_LNKCTL_CLKREQ_EN : 0;
159 list_for_each_entry(child, &linkbus->devices, bus_list)
160 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
161 PCI_EXP_LNKCTL_CLKREQ_EN,
163 link->clkpm_enabled = !!enable;
166 static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
169 * Don't enable Clock PM if the link is not Clock PM capable
170 * or Clock PM is disabled
172 if (!link->clkpm_capable || link->clkpm_disable)
174 /* Need nothing if the specified equals to current state */
175 if (link->clkpm_enabled == enable)
177 pcie_set_clkpm_nocheck(link, enable);
180 static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
182 int capable = 1, enabled = 1;
185 struct pci_dev *child;
186 struct pci_bus *linkbus = link->pdev->subordinate;
188 /* All functions should have the same cap and state, take the worst */
189 list_for_each_entry(child, &linkbus->devices, bus_list) {
190 pcie_capability_read_dword(child, PCI_EXP_LNKCAP, ®32);
191 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
196 pcie_capability_read_word(child, PCI_EXP_LNKCTL, ®16);
197 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
200 link->clkpm_enabled = enabled;
201 link->clkpm_default = enabled;
202 link->clkpm_capable = capable;
203 link->clkpm_disable = blacklist ? 1 : 0;
206 static int pcie_wait_for_retrain(struct pci_dev *pdev)
208 unsigned long end_jiffies;
211 /* Wait for Link Training to be cleared by hardware */
212 end_jiffies = jiffies + LINK_RETRAIN_TIMEOUT;
214 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, ®16);
215 if (!(reg16 & PCI_EXP_LNKSTA_LT))
218 } while (time_before(jiffies, end_jiffies));
223 static int pcie_retrain_link(struct pcie_link_state *link)
225 struct pci_dev *parent = link->pdev;
230 * Ensure the updated LNKCTL parameters are used during link
231 * training by checking that there is no ongoing link training to
232 * avoid LTSSM race as recommended in Implementation Note at the
233 * end of PCIe r6.0.1 sec 7.5.3.7.
235 rc = pcie_wait_for_retrain(parent);
239 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16);
240 reg16 |= PCI_EXP_LNKCTL_RL;
241 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
242 if (parent->clear_retrain_link) {
244 * Due to an erratum in some devices the Retrain Link bit
245 * needs to be cleared again manually to allow the link
246 * training to succeed.
248 reg16 &= ~PCI_EXP_LNKCTL_RL;
249 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
252 return pcie_wait_for_retrain(parent);
256 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
257 * could use common clock. If they are, configure them to use the
258 * common clock. That will reduce the ASPM state exit latency.
260 static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
263 u16 reg16, ccc, parent_old_ccc, child_old_ccc[8];
264 struct pci_dev *child, *parent = link->pdev;
265 struct pci_bus *linkbus = parent->subordinate;
267 * All functions of a slot should have the same Slot Clock
268 * Configuration, so just check one function
270 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
271 BUG_ON(!pci_is_pcie(child));
273 /* Check downstream component if bit Slot Clock Configuration is 1 */
274 pcie_capability_read_word(child, PCI_EXP_LNKSTA, ®16);
275 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
278 /* Check upstream component if bit Slot Clock Configuration is 1 */
279 pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16);
280 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
283 /* Port might be already in common clock mode */
284 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16);
285 parent_old_ccc = reg16 & PCI_EXP_LNKCTL_CCC;
286 if (same_clock && (reg16 & PCI_EXP_LNKCTL_CCC)) {
287 bool consistent = true;
289 list_for_each_entry(child, &linkbus->devices, bus_list) {
290 pcie_capability_read_word(child, PCI_EXP_LNKCTL,
292 if (!(reg16 & PCI_EXP_LNKCTL_CCC)) {
299 pci_warn(parent, "ASPM: current common clock configuration is broken, reconfiguring\n");
302 ccc = same_clock ? PCI_EXP_LNKCTL_CCC : 0;
303 /* Configure downstream component, all functions */
304 list_for_each_entry(child, &linkbus->devices, bus_list) {
305 pcie_capability_read_word(child, PCI_EXP_LNKCTL, ®16);
306 child_old_ccc[PCI_FUNC(child->devfn)] = reg16 & PCI_EXP_LNKCTL_CCC;
307 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
308 PCI_EXP_LNKCTL_CCC, ccc);
311 /* Configure upstream component */
312 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
313 PCI_EXP_LNKCTL_CCC, ccc);
315 if (pcie_retrain_link(link)) {
317 /* Training failed. Restore common clock configurations */
318 pci_err(parent, "ASPM: Could not configure common clock\n");
319 list_for_each_entry(child, &linkbus->devices, bus_list)
320 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
322 child_old_ccc[PCI_FUNC(child->devfn)]);
323 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
324 PCI_EXP_LNKCTL_CCC, parent_old_ccc);
328 /* Convert L0s latency encoding to ns */
329 static u32 calc_l0s_latency(u32 encoding)
332 return (5 * 1000); /* > 4us */
333 return (64 << encoding);
336 /* Convert L0s acceptable latency encoding to ns */
337 static u32 calc_l0s_acceptable(u32 encoding)
341 return (64 << encoding);
344 /* Convert L1 latency encoding to ns */
345 static u32 calc_l1_latency(u32 encoding)
348 return (65 * 1000); /* > 64us */
349 return (1000 << encoding);
352 /* Convert L1 acceptable latency encoding to ns */
353 static u32 calc_l1_acceptable(u32 encoding)
357 return (1000 << encoding);
360 /* Convert L1SS T_pwr encoding to usec */
361 static u32 calc_l1ss_pwron(struct pci_dev *pdev, u32 scale, u32 val)
371 pci_err(pdev, "%s: Invalid T_PwrOn scale: %u\n", __func__, scale);
375 static void encode_l12_threshold(u32 threshold_us, u32 *scale, u32 *value)
377 u32 threshold_ns = threshold_us * 1000;
379 /* See PCIe r3.1, sec 7.33.3 and sec 6.18 */
380 if (threshold_ns < 32) {
382 *value = threshold_ns;
383 } else if (threshold_ns < 1024) {
385 *value = threshold_ns >> 5;
386 } else if (threshold_ns < 32768) {
388 *value = threshold_ns >> 10;
389 } else if (threshold_ns < 1048576) {
391 *value = threshold_ns >> 15;
392 } else if (threshold_ns < 33554432) {
394 *value = threshold_ns >> 20;
397 *value = threshold_ns >> 25;
401 struct aspm_register_info {
404 u32 latency_encoding_l0s;
405 u32 latency_encoding_l1;
414 static void pcie_get_aspm_reg(struct pci_dev *pdev,
415 struct aspm_register_info *info)
420 pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, ®32);
421 info->support = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
422 info->latency_encoding_l0s = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
423 info->latency_encoding_l1 = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
424 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, ®16);
425 info->enabled = reg16 & PCI_EXP_LNKCTL_ASPMC;
427 /* Read L1 PM substate capabilities */
428 info->l1ss_cap = info->l1ss_ctl1 = info->l1ss_ctl2 = 0;
429 info->l1ss_cap_ptr = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS);
430 if (!info->l1ss_cap_ptr)
432 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CAP,
434 if (!(info->l1ss_cap & PCI_L1SS_CAP_L1_PM_SS)) {
440 * If we don't have LTR for the entire path from the Root Complex
441 * to this device, we can't use ASPM L1.2 because it relies on the
442 * LTR_L1.2_THRESHOLD. See PCIe r4.0, secs 5.5.4, 6.18.
445 info->l1ss_cap &= ~PCI_L1SS_CAP_ASPM_L1_2;
447 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL1,
449 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL2,
453 static void pcie_aspm_check_latency(struct pci_dev *endpoint)
455 u32 latency, l1_switch_latency = 0;
456 struct aspm_latency *acceptable;
457 struct pcie_link_state *link;
459 /* Device not in D0 doesn't need latency check */
460 if ((endpoint->current_state != PCI_D0) &&
461 (endpoint->current_state != PCI_UNKNOWN))
464 link = endpoint->bus->self->link_state;
465 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
468 /* Check upstream direction L0s latency */
469 if ((link->aspm_capable & ASPM_STATE_L0S_UP) &&
470 (link->latency_up.l0s > acceptable->l0s))
471 link->aspm_capable &= ~ASPM_STATE_L0S_UP;
473 /* Check downstream direction L0s latency */
474 if ((link->aspm_capable & ASPM_STATE_L0S_DW) &&
475 (link->latency_dw.l0s > acceptable->l0s))
476 link->aspm_capable &= ~ASPM_STATE_L0S_DW;
479 * Every switch on the path to root complex need 1
480 * more microsecond for L1. Spec doesn't mention L0s.
482 * The exit latencies for L1 substates are not advertised
483 * by a device. Since the spec also doesn't mention a way
484 * to determine max latencies introduced by enabling L1
485 * substates on the components, it is not clear how to do
486 * a L1 substate exit latency check. We assume that the
487 * L1 exit latencies advertised by a device include L1
488 * substate latencies (and hence do not do any check).
490 latency = max_t(u32, link->latency_up.l1, link->latency_dw.l1);
491 if ((link->aspm_capable & ASPM_STATE_L1) &&
492 (latency + l1_switch_latency > acceptable->l1))
493 link->aspm_capable &= ~ASPM_STATE_L1;
494 l1_switch_latency += 1000;
501 * The L1 PM substate capability is only implemented in function 0 in a
502 * multi function device.
504 static struct pci_dev *pci_function_0(struct pci_bus *linkbus)
506 struct pci_dev *child;
508 list_for_each_entry(child, &linkbus->devices, bus_list)
509 if (PCI_FUNC(child->devfn) == 0)
514 /* Calculate L1.2 PM substate timing parameters */
515 static void aspm_calc_l1ss_info(struct pcie_link_state *link,
516 struct aspm_register_info *upreg,
517 struct aspm_register_info *dwreg)
519 u32 val1, val2, scale1, scale2;
520 u32 t_common_mode, t_power_on, l1_2_threshold, scale, value;
522 link->l1ss.up_cap_ptr = upreg->l1ss_cap_ptr;
523 link->l1ss.dw_cap_ptr = dwreg->l1ss_cap_ptr;
524 link->l1ss.ctl1 = link->l1ss.ctl2 = 0;
526 if (!(link->aspm_support & ASPM_STATE_L1_2_MASK))
529 /* Choose the greater of the two Port Common_Mode_Restore_Times */
530 val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
531 val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
532 t_common_mode = max(val1, val2);
534 /* Choose the greater of the two Port T_POWER_ON times */
535 val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
536 scale1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
537 val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
538 scale2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
540 if (calc_l1ss_pwron(link->pdev, scale1, val1) >
541 calc_l1ss_pwron(link->downstream, scale2, val2)) {
542 link->l1ss.ctl2 |= scale1 | (val1 << 3);
543 t_power_on = calc_l1ss_pwron(link->pdev, scale1, val1);
545 link->l1ss.ctl2 |= scale2 | (val2 << 3);
546 t_power_on = calc_l1ss_pwron(link->downstream, scale2, val2);
550 * Set LTR_L1.2_THRESHOLD to the time required to transition the
551 * Link from L0 to L1.2 and back to L0 so we enter L1.2 only if
552 * downstream devices report (via LTR) that they can tolerate at
553 * least that much latency.
555 * Based on PCIe r3.1, sec 5.5.3.3.1, Figures 5-16 and 5-17, and
556 * Table 5-11. T(POWER_OFF) is at most 2us and T(L1.2) is at
559 l1_2_threshold = 2 + 4 + t_common_mode + t_power_on;
560 encode_l12_threshold(l1_2_threshold, &scale, &value);
561 link->l1ss.ctl1 |= t_common_mode << 8 | scale << 29 | value << 16;
564 static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
566 struct pci_dev *child = link->downstream, *parent = link->pdev;
567 struct pci_bus *linkbus = parent->subordinate;
568 struct aspm_register_info upreg, dwreg;
571 /* Set enabled/disable so that we will disable ASPM later */
572 link->aspm_enabled = ASPM_STATE_ALL;
573 link->aspm_disable = ASPM_STATE_ALL;
577 /* Get upstream/downstream components' register state */
578 pcie_get_aspm_reg(parent, &upreg);
579 pcie_get_aspm_reg(child, &dwreg);
582 * If ASPM not supported, don't mess with the clocks and link,
585 if (!(upreg.support & dwreg.support))
588 /* Configure common clock before checking latencies */
589 pcie_aspm_configure_common_clock(link);
592 * Re-read upstream/downstream components' register state
593 * after clock configuration
595 pcie_get_aspm_reg(parent, &upreg);
596 pcie_get_aspm_reg(child, &dwreg);
601 * Note that we must not enable L0s in either direction on a
602 * given link unless components on both sides of the link each
605 if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S)
606 link->aspm_support |= ASPM_STATE_L0S;
607 if (dwreg.enabled & PCIE_LINK_STATE_L0S)
608 link->aspm_enabled |= ASPM_STATE_L0S_UP;
609 if (upreg.enabled & PCIE_LINK_STATE_L0S)
610 link->aspm_enabled |= ASPM_STATE_L0S_DW;
611 link->latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s);
612 link->latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s);
615 if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1)
616 link->aspm_support |= ASPM_STATE_L1;
617 if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1)
618 link->aspm_enabled |= ASPM_STATE_L1;
619 link->latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1);
620 link->latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1);
622 /* Setup L1 substate */
623 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1)
624 link->aspm_support |= ASPM_STATE_L1_1;
625 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2)
626 link->aspm_support |= ASPM_STATE_L1_2;
627 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1)
628 link->aspm_support |= ASPM_STATE_L1_1_PCIPM;
629 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2)
630 link->aspm_support |= ASPM_STATE_L1_2_PCIPM;
632 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1)
633 link->aspm_enabled |= ASPM_STATE_L1_1;
634 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2)
635 link->aspm_enabled |= ASPM_STATE_L1_2;
636 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1)
637 link->aspm_enabled |= ASPM_STATE_L1_1_PCIPM;
638 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2)
639 link->aspm_enabled |= ASPM_STATE_L1_2_PCIPM;
641 if (link->aspm_support & ASPM_STATE_L1SS)
642 aspm_calc_l1ss_info(link, &upreg, &dwreg);
644 /* Save default state */
645 link->aspm_default = link->aspm_enabled;
647 /* Setup initial capable state. Will be updated later */
648 link->aspm_capable = link->aspm_support;
650 /* Get and check endpoint acceptable latencies */
651 list_for_each_entry(child, &linkbus->devices, bus_list) {
653 struct aspm_latency *acceptable =
654 &link->acceptable[PCI_FUNC(child->devfn)];
656 if (pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT &&
657 pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END)
660 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, ®32);
661 /* Calculate endpoint L0s acceptable latency */
662 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
663 acceptable->l0s = calc_l0s_acceptable(encoding);
664 /* Calculate endpoint L1 acceptable latency */
665 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
666 acceptable->l1 = calc_l1_acceptable(encoding);
668 pcie_aspm_check_latency(child);
672 static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos,
677 pci_read_config_dword(pdev, pos, &val);
680 pci_write_config_dword(pdev, pos, val);
683 /* Configure the ASPM L1 substates */
684 static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state)
687 struct pci_dev *child = link->downstream, *parent = link->pdev;
688 u32 up_cap_ptr = link->l1ss.up_cap_ptr;
689 u32 dw_cap_ptr = link->l1ss.dw_cap_ptr;
691 enable_req = (link->aspm_enabled ^ state) & state;
694 * Here are the rules specified in the PCIe spec for enabling L1SS:
695 * - When enabling L1.x, enable bit at parent first, then at child
696 * - When disabling L1.x, disable bit at child first, then at parent
697 * - When enabling ASPM L1.x, need to disable L1
698 * (at child followed by parent).
699 * - The ASPM/PCIPM L1.2 must be disabled while programming timing
702 * To keep it simple, disable all L1SS bits first, and later enable
706 /* Disable all L1 substates */
707 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
708 PCI_L1SS_CTL1_L1SS_MASK, 0);
709 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
710 PCI_L1SS_CTL1_L1SS_MASK, 0);
712 * If needed, disable L1, and it gets enabled later
713 * in pcie_config_aspm_link().
715 if (enable_req & (ASPM_STATE_L1_1 | ASPM_STATE_L1_2)) {
716 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
717 PCI_EXP_LNKCTL_ASPM_L1, 0);
718 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
719 PCI_EXP_LNKCTL_ASPM_L1, 0);
722 if (enable_req & ASPM_STATE_L1_2_MASK) {
724 /* Program T_POWER_ON times in both ports */
725 pci_write_config_dword(parent, up_cap_ptr + PCI_L1SS_CTL2,
727 pci_write_config_dword(child, dw_cap_ptr + PCI_L1SS_CTL2,
730 /* Program Common_Mode_Restore_Time in upstream device */
731 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
732 PCI_L1SS_CTL1_CM_RESTORE_TIME,
735 /* Program LTR_L1.2_THRESHOLD time in both ports */
736 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
737 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
738 PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
740 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
741 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
742 PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
747 if (state & ASPM_STATE_L1_1)
748 val |= PCI_L1SS_CTL1_ASPM_L1_1;
749 if (state & ASPM_STATE_L1_2)
750 val |= PCI_L1SS_CTL1_ASPM_L1_2;
751 if (state & ASPM_STATE_L1_1_PCIPM)
752 val |= PCI_L1SS_CTL1_PCIPM_L1_1;
753 if (state & ASPM_STATE_L1_2_PCIPM)
754 val |= PCI_L1SS_CTL1_PCIPM_L1_2;
756 /* Enable what we need to enable */
757 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
758 PCI_L1SS_CTL1_L1SS_MASK, val);
759 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
760 PCI_L1SS_CTL1_L1SS_MASK, val);
763 static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
765 pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
766 PCI_EXP_LNKCTL_ASPMC, val);
769 static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
771 u32 upstream = 0, dwstream = 0;
772 struct pci_dev *child = link->downstream, *parent = link->pdev;
773 struct pci_bus *linkbus = parent->subordinate;
775 /* Enable only the states that were not explicitly disabled */
776 state &= (link->aspm_capable & ~link->aspm_disable);
778 /* Can't enable any substates if L1 is not enabled */
779 if (!(state & ASPM_STATE_L1))
780 state &= ~ASPM_STATE_L1SS;
782 /* Spec says both ports must be in D0 before enabling PCI PM substates*/
783 if (parent->current_state != PCI_D0 || child->current_state != PCI_D0) {
784 state &= ~ASPM_STATE_L1_SS_PCIPM;
785 state |= (link->aspm_enabled & ASPM_STATE_L1_SS_PCIPM);
788 /* Nothing to do if the link is already in the requested state */
789 if (link->aspm_enabled == state)
791 /* Convert ASPM state to upstream/downstream ASPM register state */
792 if (state & ASPM_STATE_L0S_UP)
793 dwstream |= PCI_EXP_LNKCTL_ASPM_L0S;
794 if (state & ASPM_STATE_L0S_DW)
795 upstream |= PCI_EXP_LNKCTL_ASPM_L0S;
796 if (state & ASPM_STATE_L1) {
797 upstream |= PCI_EXP_LNKCTL_ASPM_L1;
798 dwstream |= PCI_EXP_LNKCTL_ASPM_L1;
801 if (link->aspm_capable & ASPM_STATE_L1SS)
802 pcie_config_aspm_l1ss(link, state);
805 * Spec 2.0 suggests all functions should be configured the
806 * same setting for ASPM. Enabling ASPM L1 should be done in
807 * upstream component first and then downstream, and vice
808 * versa for disabling ASPM L1. Spec doesn't mention L0S.
810 if (state & ASPM_STATE_L1)
811 pcie_config_aspm_dev(parent, upstream);
812 list_for_each_entry(child, &linkbus->devices, bus_list)
813 pcie_config_aspm_dev(child, dwstream);
814 if (!(state & ASPM_STATE_L1))
815 pcie_config_aspm_dev(parent, upstream);
817 link->aspm_enabled = state;
820 static void pcie_config_aspm_path(struct pcie_link_state *link)
823 pcie_config_aspm_link(link, policy_to_aspm_state(link));
828 static void free_link_state(struct pcie_link_state *link)
830 link->pdev->link_state = NULL;
834 static int pcie_aspm_sanity_check(struct pci_dev *pdev)
836 struct pci_dev *child;
840 * Some functions in a slot might not all be PCIe functions,
841 * very strange. Disable ASPM for the whole slot
843 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
844 if (!pci_is_pcie(child))
848 * If ASPM is disabled then we're not going to change
849 * the BIOS state. It's safe to continue even if it's a
857 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
858 * RBER bit to determine if a function is 1.1 version device
860 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, ®32);
861 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
862 pci_info(child, "disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force'\n");
869 static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
871 struct pcie_link_state *link;
873 link = kzalloc(sizeof(*link), GFP_KERNEL);
877 INIT_LIST_HEAD(&link->sibling);
878 INIT_LIST_HEAD(&link->children);
879 INIT_LIST_HEAD(&link->link);
881 link->downstream = pci_function_0(pdev->subordinate);
884 * Root Ports and PCI/PCI-X to PCIe Bridges are roots of PCIe
885 * hierarchies. Note that some PCIe host implementations omit
886 * the root ports entirely, in which case a downstream port on
887 * a switch may become the root of the link state chain for all
888 * its subordinate endpoints.
890 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
891 pci_pcie_type(pdev) == PCI_EXP_TYPE_PCIE_BRIDGE ||
892 !pdev->bus->parent->self) {
895 struct pcie_link_state *parent;
897 parent = pdev->bus->parent->self->link_state;
903 link->parent = parent;
904 link->root = link->parent->root;
905 list_add(&link->link, &parent->children);
908 list_add(&link->sibling, &link_list);
909 pdev->link_state = link;
914 * pcie_aspm_init_link_state: Initiate PCI express link state.
915 * It is called after the pcie and its children devices are scanned.
916 * @pdev: the root port or switch downstream port
918 void pcie_aspm_init_link_state(struct pci_dev *pdev)
920 struct pcie_link_state *link;
921 int blacklist = !!pcie_aspm_sanity_check(pdev);
923 if (!aspm_support_enabled)
926 if (pdev->link_state)
930 * We allocate pcie_link_state for the component on the upstream
931 * end of a Link, so there's nothing to do unless this device has a
932 * Link on its secondary side.
934 if (!pdev->has_secondary_link)
937 /* VIA has a strange chipset, root port is under a bridge */
938 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT &&
942 down_read(&pci_bus_sem);
943 if (list_empty(&pdev->subordinate->devices))
946 mutex_lock(&aspm_lock);
947 link = alloc_pcie_link_state(pdev);
951 * Setup initial ASPM state. Note that we need to configure
952 * upstream links also because capable state of them can be
953 * update through pcie_aspm_cap_init().
955 pcie_aspm_cap_init(link, blacklist);
957 /* Setup initial Clock PM state */
958 pcie_clkpm_cap_init(link, blacklist);
961 * At this stage drivers haven't had an opportunity to change the
962 * link policy setting. Enabling ASPM on broken hardware can cripple
963 * it even before the driver has had a chance to disable ASPM, so
964 * default to a safe level right now. If we're enabling ASPM beyond
965 * the BIOS's expectation, we'll do so once pci_enable_device() is
968 if (aspm_policy != POLICY_POWERSAVE &&
969 aspm_policy != POLICY_POWER_SUPERSAVE) {
970 pcie_config_aspm_path(link);
971 pcie_set_clkpm(link, policy_to_clkpm_state(link));
975 mutex_unlock(&aspm_lock);
977 up_read(&pci_bus_sem);
980 /* Recheck latencies and update aspm_capable for links under the root */
981 static void pcie_update_aspm_capable(struct pcie_link_state *root)
983 struct pcie_link_state *link;
984 BUG_ON(root->parent);
985 list_for_each_entry(link, &link_list, sibling) {
986 if (link->root != root)
988 link->aspm_capable = link->aspm_support;
990 list_for_each_entry(link, &link_list, sibling) {
991 struct pci_dev *child;
992 struct pci_bus *linkbus = link->pdev->subordinate;
993 if (link->root != root)
995 list_for_each_entry(child, &linkbus->devices, bus_list) {
996 if ((pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT) &&
997 (pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END))
999 pcie_aspm_check_latency(child);
1004 /* @pdev: the endpoint device */
1005 void pcie_aspm_exit_link_state(struct pci_dev *pdev)
1007 struct pci_dev *parent = pdev->bus->self;
1008 struct pcie_link_state *link, *root, *parent_link;
1010 if (!parent || !parent->link_state)
1013 down_read(&pci_bus_sem);
1014 mutex_lock(&aspm_lock);
1016 * All PCIe functions are in one slot, remove one function will remove
1017 * the whole slot, so just wait until we are the last function left.
1019 if (!list_empty(&parent->subordinate->devices))
1022 link = parent->link_state;
1024 parent_link = link->parent;
1026 /* All functions are removed, so just disable ASPM for the link */
1027 pcie_config_aspm_link(link, 0);
1028 list_del(&link->sibling);
1029 list_del(&link->link);
1030 /* Clock PM is for endpoint device */
1031 free_link_state(link);
1033 /* Recheck latencies and configure upstream links */
1035 pcie_update_aspm_capable(root);
1036 pcie_config_aspm_path(parent_link);
1039 mutex_unlock(&aspm_lock);
1040 up_read(&pci_bus_sem);
1043 /* @pdev: the root port or switch downstream port */
1044 void pcie_aspm_pm_state_change(struct pci_dev *pdev)
1046 struct pcie_link_state *link = pdev->link_state;
1048 if (aspm_disabled || !link)
1051 * Devices changed PM state, we should recheck if latency
1052 * meets all functions' requirement
1054 down_read(&pci_bus_sem);
1055 mutex_lock(&aspm_lock);
1056 pcie_update_aspm_capable(link->root);
1057 pcie_config_aspm_path(link);
1058 mutex_unlock(&aspm_lock);
1059 up_read(&pci_bus_sem);
1062 void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
1064 struct pcie_link_state *link = pdev->link_state;
1066 if (aspm_disabled || !link)
1069 if (aspm_policy != POLICY_POWERSAVE &&
1070 aspm_policy != POLICY_POWER_SUPERSAVE)
1073 down_read(&pci_bus_sem);
1074 mutex_lock(&aspm_lock);
1075 pcie_config_aspm_path(link);
1076 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1077 mutex_unlock(&aspm_lock);
1078 up_read(&pci_bus_sem);
1081 static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
1083 struct pci_dev *parent = pdev->bus->self;
1084 struct pcie_link_state *link;
1086 if (!pci_is_pcie(pdev))
1089 if (pdev->has_secondary_link)
1091 if (!parent || !parent->link_state)
1095 * A driver requested that ASPM be disabled on this device, but
1096 * if we don't have permission to manage ASPM (e.g., on ACPI
1097 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
1098 * the _OSC method), we can't honor that request. Windows has
1099 * a similar mechanism using "PciASPMOptOut", which is also
1100 * ignored in this situation.
1102 if (aspm_disabled) {
1103 pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n");
1108 down_read(&pci_bus_sem);
1109 mutex_lock(&aspm_lock);
1110 link = parent->link_state;
1111 if (state & PCIE_LINK_STATE_L0S)
1112 link->aspm_disable |= ASPM_STATE_L0S;
1113 if (state & PCIE_LINK_STATE_L1)
1114 link->aspm_disable |= ASPM_STATE_L1;
1115 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1117 if (state & PCIE_LINK_STATE_CLKPM)
1118 link->clkpm_disable = 1;
1119 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1120 mutex_unlock(&aspm_lock);
1122 up_read(&pci_bus_sem);
1125 void pci_disable_link_state_locked(struct pci_dev *pdev, int state)
1127 __pci_disable_link_state(pdev, state, false);
1129 EXPORT_SYMBOL(pci_disable_link_state_locked);
1132 * pci_disable_link_state - Disable device's link state, so the link will
1133 * never enter specific states. Note that if the BIOS didn't grant ASPM
1134 * control to the OS, this does nothing because we can't touch the LNKCTL
1138 * @state: ASPM link state to disable
1140 void pci_disable_link_state(struct pci_dev *pdev, int state)
1142 __pci_disable_link_state(pdev, state, true);
1144 EXPORT_SYMBOL(pci_disable_link_state);
1146 static int pcie_aspm_set_policy(const char *val,
1147 const struct kernel_param *kp)
1150 struct pcie_link_state *link;
1154 i = sysfs_match_string(policy_str, val);
1157 if (i == aspm_policy)
1160 down_read(&pci_bus_sem);
1161 mutex_lock(&aspm_lock);
1163 list_for_each_entry(link, &link_list, sibling) {
1164 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1165 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1167 mutex_unlock(&aspm_lock);
1168 up_read(&pci_bus_sem);
1172 static int pcie_aspm_get_policy(char *buffer, const struct kernel_param *kp)
1175 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
1176 if (i == aspm_policy)
1177 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
1179 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
1180 cnt += sprintf(buffer + cnt, "\n");
1184 module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
1187 #ifdef CONFIG_PCIEASPM_DEBUG
1188 static ssize_t link_state_show(struct device *dev,
1189 struct device_attribute *attr,
1192 struct pci_dev *pci_device = to_pci_dev(dev);
1193 struct pcie_link_state *link_state = pci_device->link_state;
1195 return sprintf(buf, "%d\n", link_state->aspm_enabled);
1198 static ssize_t link_state_store(struct device *dev,
1199 struct device_attribute *attr,
1203 struct pci_dev *pdev = to_pci_dev(dev);
1204 struct pcie_link_state *link, *root = pdev->link_state->root;
1210 if (kstrtouint(buf, 10, &state))
1212 if ((state & ~ASPM_STATE_ALL) != 0)
1215 down_read(&pci_bus_sem);
1216 mutex_lock(&aspm_lock);
1217 list_for_each_entry(link, &link_list, sibling) {
1218 if (link->root != root)
1220 pcie_config_aspm_link(link, state);
1222 mutex_unlock(&aspm_lock);
1223 up_read(&pci_bus_sem);
1227 static ssize_t clk_ctl_show(struct device *dev,
1228 struct device_attribute *attr,
1231 struct pci_dev *pci_device = to_pci_dev(dev);
1232 struct pcie_link_state *link_state = pci_device->link_state;
1234 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
1237 static ssize_t clk_ctl_store(struct device *dev,
1238 struct device_attribute *attr,
1242 struct pci_dev *pdev = to_pci_dev(dev);
1245 if (strtobool(buf, &state))
1248 down_read(&pci_bus_sem);
1249 mutex_lock(&aspm_lock);
1250 pcie_set_clkpm_nocheck(pdev->link_state, state);
1251 mutex_unlock(&aspm_lock);
1252 up_read(&pci_bus_sem);
1257 static DEVICE_ATTR_RW(link_state);
1258 static DEVICE_ATTR_RW(clk_ctl);
1260 static char power_group[] = "power";
1261 void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
1263 struct pcie_link_state *link_state = pdev->link_state;
1268 if (link_state->aspm_support)
1269 sysfs_add_file_to_group(&pdev->dev.kobj,
1270 &dev_attr_link_state.attr, power_group);
1271 if (link_state->clkpm_capable)
1272 sysfs_add_file_to_group(&pdev->dev.kobj,
1273 &dev_attr_clk_ctl.attr, power_group);
1276 void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
1278 struct pcie_link_state *link_state = pdev->link_state;
1283 if (link_state->aspm_support)
1284 sysfs_remove_file_from_group(&pdev->dev.kobj,
1285 &dev_attr_link_state.attr, power_group);
1286 if (link_state->clkpm_capable)
1287 sysfs_remove_file_from_group(&pdev->dev.kobj,
1288 &dev_attr_clk_ctl.attr, power_group);
1292 static int __init pcie_aspm_disable(char *str)
1294 if (!strcmp(str, "off")) {
1295 aspm_policy = POLICY_DEFAULT;
1297 aspm_support_enabled = false;
1298 printk(KERN_INFO "PCIe ASPM is disabled\n");
1299 } else if (!strcmp(str, "force")) {
1301 printk(KERN_INFO "PCIe ASPM is forcibly enabled\n");
1306 __setup("pcie_aspm=", pcie_aspm_disable);
1308 void pcie_no_aspm(void)
1311 * Disabling ASPM is intended to prevent the kernel from modifying
1312 * existing hardware state, not to clear existing state. To that end:
1313 * (a) set policy to POLICY_DEFAULT in order to avoid changing state
1314 * (b) prevent userspace from changing policy
1317 aspm_policy = POLICY_DEFAULT;
1322 bool pcie_aspm_support_enabled(void)
1324 return aspm_support_enabled;
1326 EXPORT_SYMBOL(pcie_aspm_support_enabled);