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 bool pcie_retrain_link(struct pcie_link_state *link)
208 struct pci_dev *parent = link->pdev;
209 unsigned long start_jiffies;
212 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16);
213 reg16 |= PCI_EXP_LNKCTL_RL;
214 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
215 if (parent->clear_retrain_link) {
217 * Due to an erratum in some devices the Retrain Link bit
218 * needs to be cleared again manually to allow the link
219 * training to succeed.
221 reg16 &= ~PCI_EXP_LNKCTL_RL;
222 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
225 /* Wait for link training end. Break out after waiting for timeout */
226 start_jiffies = jiffies;
228 pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16);
229 if (!(reg16 & PCI_EXP_LNKSTA_LT))
231 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
235 return !(reg16 & PCI_EXP_LNKSTA_LT);
239 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
240 * could use common clock. If they are, configure them to use the
241 * common clock. That will reduce the ASPM state exit latency.
243 static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
246 u16 reg16, parent_reg, child_reg[8];
247 struct pci_dev *child, *parent = link->pdev;
248 struct pci_bus *linkbus = parent->subordinate;
250 * All functions of a slot should have the same Slot Clock
251 * Configuration, so just check one function
253 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
254 BUG_ON(!pci_is_pcie(child));
256 /* Check downstream component if bit Slot Clock Configuration is 1 */
257 pcie_capability_read_word(child, PCI_EXP_LNKSTA, ®16);
258 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
261 /* Check upstream component if bit Slot Clock Configuration is 1 */
262 pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16);
263 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
266 /* Port might be already in common clock mode */
267 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16);
268 if (same_clock && (reg16 & PCI_EXP_LNKCTL_CCC)) {
269 bool consistent = true;
271 list_for_each_entry(child, &linkbus->devices, bus_list) {
272 pcie_capability_read_word(child, PCI_EXP_LNKCTL,
274 if (!(reg16 & PCI_EXP_LNKCTL_CCC)) {
281 pci_warn(parent, "ASPM: current common clock configuration is broken, reconfiguring\n");
284 /* Configure downstream component, all functions */
285 list_for_each_entry(child, &linkbus->devices, bus_list) {
286 pcie_capability_read_word(child, PCI_EXP_LNKCTL, ®16);
287 child_reg[PCI_FUNC(child->devfn)] = reg16;
289 reg16 |= PCI_EXP_LNKCTL_CCC;
291 reg16 &= ~PCI_EXP_LNKCTL_CCC;
292 pcie_capability_write_word(child, PCI_EXP_LNKCTL, reg16);
295 /* Configure upstream component */
296 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16);
299 reg16 |= PCI_EXP_LNKCTL_CCC;
301 reg16 &= ~PCI_EXP_LNKCTL_CCC;
302 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
304 if (pcie_retrain_link(link))
307 /* Training failed. Restore common clock configurations */
308 pci_err(parent, "ASPM: Could not configure common clock\n");
309 list_for_each_entry(child, &linkbus->devices, bus_list)
310 pcie_capability_write_word(child, PCI_EXP_LNKCTL,
311 child_reg[PCI_FUNC(child->devfn)]);
312 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, parent_reg);
315 /* Convert L0s latency encoding to ns */
316 static u32 calc_l0s_latency(u32 encoding)
319 return (5 * 1000); /* > 4us */
320 return (64 << encoding);
323 /* Convert L0s acceptable latency encoding to ns */
324 static u32 calc_l0s_acceptable(u32 encoding)
328 return (64 << encoding);
331 /* Convert L1 latency encoding to ns */
332 static u32 calc_l1_latency(u32 encoding)
335 return (65 * 1000); /* > 64us */
336 return (1000 << encoding);
339 /* Convert L1 acceptable latency encoding to ns */
340 static u32 calc_l1_acceptable(u32 encoding)
344 return (1000 << encoding);
347 /* Convert L1SS T_pwr encoding to usec */
348 static u32 calc_l1ss_pwron(struct pci_dev *pdev, u32 scale, u32 val)
358 pci_err(pdev, "%s: Invalid T_PwrOn scale: %u\n", __func__, scale);
362 static void encode_l12_threshold(u32 threshold_us, u32 *scale, u32 *value)
364 u32 threshold_ns = threshold_us * 1000;
366 /* See PCIe r3.1, sec 7.33.3 and sec 6.18 */
367 if (threshold_ns < 32) {
369 *value = threshold_ns;
370 } else if (threshold_ns < 1024) {
372 *value = threshold_ns >> 5;
373 } else if (threshold_ns < 32768) {
375 *value = threshold_ns >> 10;
376 } else if (threshold_ns < 1048576) {
378 *value = threshold_ns >> 15;
379 } else if (threshold_ns < 33554432) {
381 *value = threshold_ns >> 20;
384 *value = threshold_ns >> 25;
388 struct aspm_register_info {
391 u32 latency_encoding_l0s;
392 u32 latency_encoding_l1;
401 static void pcie_get_aspm_reg(struct pci_dev *pdev,
402 struct aspm_register_info *info)
407 pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, ®32);
408 info->support = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
409 info->latency_encoding_l0s = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
410 info->latency_encoding_l1 = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
411 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, ®16);
412 info->enabled = reg16 & PCI_EXP_LNKCTL_ASPMC;
414 /* Read L1 PM substate capabilities */
415 info->l1ss_cap = info->l1ss_ctl1 = info->l1ss_ctl2 = 0;
416 info->l1ss_cap_ptr = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS);
417 if (!info->l1ss_cap_ptr)
419 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CAP,
421 if (!(info->l1ss_cap & PCI_L1SS_CAP_L1_PM_SS)) {
427 * If we don't have LTR for the entire path from the Root Complex
428 * to this device, we can't use ASPM L1.2 because it relies on the
429 * LTR_L1.2_THRESHOLD. See PCIe r4.0, secs 5.5.4, 6.18.
432 info->l1ss_cap &= ~PCI_L1SS_CAP_ASPM_L1_2;
434 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL1,
436 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL2,
440 static void pcie_aspm_check_latency(struct pci_dev *endpoint)
442 u32 latency, l1_switch_latency = 0;
443 struct aspm_latency *acceptable;
444 struct pcie_link_state *link;
446 /* Device not in D0 doesn't need latency check */
447 if ((endpoint->current_state != PCI_D0) &&
448 (endpoint->current_state != PCI_UNKNOWN))
451 link = endpoint->bus->self->link_state;
452 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
455 /* Check upstream direction L0s latency */
456 if ((link->aspm_capable & ASPM_STATE_L0S_UP) &&
457 (link->latency_up.l0s > acceptable->l0s))
458 link->aspm_capable &= ~ASPM_STATE_L0S_UP;
460 /* Check downstream direction L0s latency */
461 if ((link->aspm_capable & ASPM_STATE_L0S_DW) &&
462 (link->latency_dw.l0s > acceptable->l0s))
463 link->aspm_capable &= ~ASPM_STATE_L0S_DW;
466 * Every switch on the path to root complex need 1
467 * more microsecond for L1. Spec doesn't mention L0s.
469 * The exit latencies for L1 substates are not advertised
470 * by a device. Since the spec also doesn't mention a way
471 * to determine max latencies introduced by enabling L1
472 * substates on the components, it is not clear how to do
473 * a L1 substate exit latency check. We assume that the
474 * L1 exit latencies advertised by a device include L1
475 * substate latencies (and hence do not do any check).
477 latency = max_t(u32, link->latency_up.l1, link->latency_dw.l1);
478 if ((link->aspm_capable & ASPM_STATE_L1) &&
479 (latency + l1_switch_latency > acceptable->l1))
480 link->aspm_capable &= ~ASPM_STATE_L1;
481 l1_switch_latency += 1000;
488 * The L1 PM substate capability is only implemented in function 0 in a
489 * multi function device.
491 static struct pci_dev *pci_function_0(struct pci_bus *linkbus)
493 struct pci_dev *child;
495 list_for_each_entry(child, &linkbus->devices, bus_list)
496 if (PCI_FUNC(child->devfn) == 0)
501 /* Calculate L1.2 PM substate timing parameters */
502 static void aspm_calc_l1ss_info(struct pcie_link_state *link,
503 struct aspm_register_info *upreg,
504 struct aspm_register_info *dwreg)
506 u32 val1, val2, scale1, scale2;
507 u32 t_common_mode, t_power_on, l1_2_threshold, scale, value;
509 link->l1ss.up_cap_ptr = upreg->l1ss_cap_ptr;
510 link->l1ss.dw_cap_ptr = dwreg->l1ss_cap_ptr;
511 link->l1ss.ctl1 = link->l1ss.ctl2 = 0;
513 if (!(link->aspm_support & ASPM_STATE_L1_2_MASK))
516 /* Choose the greater of the two Port Common_Mode_Restore_Times */
517 val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
518 val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
519 t_common_mode = max(val1, val2);
521 /* Choose the greater of the two Port T_POWER_ON times */
522 val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
523 scale1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
524 val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
525 scale2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
527 if (calc_l1ss_pwron(link->pdev, scale1, val1) >
528 calc_l1ss_pwron(link->downstream, scale2, val2)) {
529 link->l1ss.ctl2 |= scale1 | (val1 << 3);
530 t_power_on = calc_l1ss_pwron(link->pdev, scale1, val1);
532 link->l1ss.ctl2 |= scale2 | (val2 << 3);
533 t_power_on = calc_l1ss_pwron(link->downstream, scale2, val2);
537 * Set LTR_L1.2_THRESHOLD to the time required to transition the
538 * Link from L0 to L1.2 and back to L0 so we enter L1.2 only if
539 * downstream devices report (via LTR) that they can tolerate at
540 * least that much latency.
542 * Based on PCIe r3.1, sec 5.5.3.3.1, Figures 5-16 and 5-17, and
543 * Table 5-11. T(POWER_OFF) is at most 2us and T(L1.2) is at
546 l1_2_threshold = 2 + 4 + t_common_mode + t_power_on;
547 encode_l12_threshold(l1_2_threshold, &scale, &value);
548 link->l1ss.ctl1 |= t_common_mode << 8 | scale << 29 | value << 16;
551 static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
553 struct pci_dev *child = link->downstream, *parent = link->pdev;
554 struct pci_bus *linkbus = parent->subordinate;
555 struct aspm_register_info upreg, dwreg;
558 /* Set enabled/disable so that we will disable ASPM later */
559 link->aspm_enabled = ASPM_STATE_ALL;
560 link->aspm_disable = ASPM_STATE_ALL;
564 /* Get upstream/downstream components' register state */
565 pcie_get_aspm_reg(parent, &upreg);
566 pcie_get_aspm_reg(child, &dwreg);
569 * If ASPM not supported, don't mess with the clocks and link,
572 if (!(upreg.support & dwreg.support))
575 /* Configure common clock before checking latencies */
576 pcie_aspm_configure_common_clock(link);
579 * Re-read upstream/downstream components' register state
580 * after clock configuration
582 pcie_get_aspm_reg(parent, &upreg);
583 pcie_get_aspm_reg(child, &dwreg);
588 * Note that we must not enable L0s in either direction on a
589 * given link unless components on both sides of the link each
592 if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S)
593 link->aspm_support |= ASPM_STATE_L0S;
594 if (dwreg.enabled & PCIE_LINK_STATE_L0S)
595 link->aspm_enabled |= ASPM_STATE_L0S_UP;
596 if (upreg.enabled & PCIE_LINK_STATE_L0S)
597 link->aspm_enabled |= ASPM_STATE_L0S_DW;
598 link->latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s);
599 link->latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s);
602 if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1)
603 link->aspm_support |= ASPM_STATE_L1;
604 if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1)
605 link->aspm_enabled |= ASPM_STATE_L1;
606 link->latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1);
607 link->latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1);
609 /* Setup L1 substate */
610 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1)
611 link->aspm_support |= ASPM_STATE_L1_1;
612 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2)
613 link->aspm_support |= ASPM_STATE_L1_2;
614 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1)
615 link->aspm_support |= ASPM_STATE_L1_1_PCIPM;
616 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2)
617 link->aspm_support |= ASPM_STATE_L1_2_PCIPM;
619 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1)
620 link->aspm_enabled |= ASPM_STATE_L1_1;
621 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2)
622 link->aspm_enabled |= ASPM_STATE_L1_2;
623 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1)
624 link->aspm_enabled |= ASPM_STATE_L1_1_PCIPM;
625 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2)
626 link->aspm_enabled |= ASPM_STATE_L1_2_PCIPM;
628 if (link->aspm_support & ASPM_STATE_L1SS)
629 aspm_calc_l1ss_info(link, &upreg, &dwreg);
631 /* Save default state */
632 link->aspm_default = link->aspm_enabled;
634 /* Setup initial capable state. Will be updated later */
635 link->aspm_capable = link->aspm_support;
637 /* Get and check endpoint acceptable latencies */
638 list_for_each_entry(child, &linkbus->devices, bus_list) {
640 struct aspm_latency *acceptable =
641 &link->acceptable[PCI_FUNC(child->devfn)];
643 if (pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT &&
644 pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END)
647 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, ®32);
648 /* Calculate endpoint L0s acceptable latency */
649 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
650 acceptable->l0s = calc_l0s_acceptable(encoding);
651 /* Calculate endpoint L1 acceptable latency */
652 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
653 acceptable->l1 = calc_l1_acceptable(encoding);
655 pcie_aspm_check_latency(child);
659 static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos,
664 pci_read_config_dword(pdev, pos, &val);
667 pci_write_config_dword(pdev, pos, val);
670 /* Configure the ASPM L1 substates */
671 static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state)
674 struct pci_dev *child = link->downstream, *parent = link->pdev;
675 u32 up_cap_ptr = link->l1ss.up_cap_ptr;
676 u32 dw_cap_ptr = link->l1ss.dw_cap_ptr;
678 enable_req = (link->aspm_enabled ^ state) & state;
681 * Here are the rules specified in the PCIe spec for enabling L1SS:
682 * - When enabling L1.x, enable bit at parent first, then at child
683 * - When disabling L1.x, disable bit at child first, then at parent
684 * - When enabling ASPM L1.x, need to disable L1
685 * (at child followed by parent).
686 * - The ASPM/PCIPM L1.2 must be disabled while programming timing
689 * To keep it simple, disable all L1SS bits first, and later enable
693 /* Disable all L1 substates */
694 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
695 PCI_L1SS_CTL1_L1SS_MASK, 0);
696 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
697 PCI_L1SS_CTL1_L1SS_MASK, 0);
699 * If needed, disable L1, and it gets enabled later
700 * in pcie_config_aspm_link().
702 if (enable_req & (ASPM_STATE_L1_1 | ASPM_STATE_L1_2)) {
703 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
704 PCI_EXP_LNKCTL_ASPM_L1, 0);
705 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
706 PCI_EXP_LNKCTL_ASPM_L1, 0);
709 if (enable_req & ASPM_STATE_L1_2_MASK) {
711 /* Program T_POWER_ON times in both ports */
712 pci_write_config_dword(parent, up_cap_ptr + PCI_L1SS_CTL2,
714 pci_write_config_dword(child, dw_cap_ptr + PCI_L1SS_CTL2,
717 /* Program Common_Mode_Restore_Time in upstream device */
718 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
719 PCI_L1SS_CTL1_CM_RESTORE_TIME,
722 /* Program LTR_L1.2_THRESHOLD time in both ports */
723 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
724 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
725 PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
727 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
728 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
729 PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
734 if (state & ASPM_STATE_L1_1)
735 val |= PCI_L1SS_CTL1_ASPM_L1_1;
736 if (state & ASPM_STATE_L1_2)
737 val |= PCI_L1SS_CTL1_ASPM_L1_2;
738 if (state & ASPM_STATE_L1_1_PCIPM)
739 val |= PCI_L1SS_CTL1_PCIPM_L1_1;
740 if (state & ASPM_STATE_L1_2_PCIPM)
741 val |= PCI_L1SS_CTL1_PCIPM_L1_2;
743 /* Enable what we need to enable */
744 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
745 PCI_L1SS_CTL1_L1SS_MASK, val);
746 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
747 PCI_L1SS_CTL1_L1SS_MASK, val);
750 static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
752 pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
753 PCI_EXP_LNKCTL_ASPMC, val);
756 static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
758 u32 upstream = 0, dwstream = 0;
759 struct pci_dev *child = link->downstream, *parent = link->pdev;
760 struct pci_bus *linkbus = parent->subordinate;
762 /* Enable only the states that were not explicitly disabled */
763 state &= (link->aspm_capable & ~link->aspm_disable);
765 /* Can't enable any substates if L1 is not enabled */
766 if (!(state & ASPM_STATE_L1))
767 state &= ~ASPM_STATE_L1SS;
769 /* Spec says both ports must be in D0 before enabling PCI PM substates*/
770 if (parent->current_state != PCI_D0 || child->current_state != PCI_D0) {
771 state &= ~ASPM_STATE_L1_SS_PCIPM;
772 state |= (link->aspm_enabled & ASPM_STATE_L1_SS_PCIPM);
775 /* Nothing to do if the link is already in the requested state */
776 if (link->aspm_enabled == state)
778 /* Convert ASPM state to upstream/downstream ASPM register state */
779 if (state & ASPM_STATE_L0S_UP)
780 dwstream |= PCI_EXP_LNKCTL_ASPM_L0S;
781 if (state & ASPM_STATE_L0S_DW)
782 upstream |= PCI_EXP_LNKCTL_ASPM_L0S;
783 if (state & ASPM_STATE_L1) {
784 upstream |= PCI_EXP_LNKCTL_ASPM_L1;
785 dwstream |= PCI_EXP_LNKCTL_ASPM_L1;
788 if (link->aspm_capable & ASPM_STATE_L1SS)
789 pcie_config_aspm_l1ss(link, state);
792 * Spec 2.0 suggests all functions should be configured the
793 * same setting for ASPM. Enabling ASPM L1 should be done in
794 * upstream component first and then downstream, and vice
795 * versa for disabling ASPM L1. Spec doesn't mention L0S.
797 if (state & ASPM_STATE_L1)
798 pcie_config_aspm_dev(parent, upstream);
799 list_for_each_entry(child, &linkbus->devices, bus_list)
800 pcie_config_aspm_dev(child, dwstream);
801 if (!(state & ASPM_STATE_L1))
802 pcie_config_aspm_dev(parent, upstream);
804 link->aspm_enabled = state;
807 static void pcie_config_aspm_path(struct pcie_link_state *link)
810 pcie_config_aspm_link(link, policy_to_aspm_state(link));
815 static void free_link_state(struct pcie_link_state *link)
817 link->pdev->link_state = NULL;
821 static int pcie_aspm_sanity_check(struct pci_dev *pdev)
823 struct pci_dev *child;
827 * Some functions in a slot might not all be PCIe functions,
828 * very strange. Disable ASPM for the whole slot
830 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
831 if (!pci_is_pcie(child))
835 * If ASPM is disabled then we're not going to change
836 * the BIOS state. It's safe to continue even if it's a
844 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
845 * RBER bit to determine if a function is 1.1 version device
847 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, ®32);
848 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
849 pci_info(child, "disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force'\n");
856 static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
858 struct pcie_link_state *link;
860 link = kzalloc(sizeof(*link), GFP_KERNEL);
864 INIT_LIST_HEAD(&link->sibling);
865 INIT_LIST_HEAD(&link->children);
866 INIT_LIST_HEAD(&link->link);
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;
892 list_add(&link->link, &parent->children);
895 list_add(&link->sibling, &link_list);
896 pdev->link_state = link;
901 * pcie_aspm_init_link_state: Initiate PCI express link state.
902 * It is called after the pcie and its children devices are scanned.
903 * @pdev: the root port or switch downstream port
905 void pcie_aspm_init_link_state(struct pci_dev *pdev)
907 struct pcie_link_state *link;
908 int blacklist = !!pcie_aspm_sanity_check(pdev);
910 if (!aspm_support_enabled)
913 if (pdev->link_state)
917 * We allocate pcie_link_state for the component on the upstream
918 * end of a Link, so there's nothing to do unless this device has a
919 * Link on its secondary side.
921 if (!pdev->has_secondary_link)
924 /* VIA has a strange chipset, root port is under a bridge */
925 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT &&
929 down_read(&pci_bus_sem);
930 if (list_empty(&pdev->subordinate->devices))
933 mutex_lock(&aspm_lock);
934 link = alloc_pcie_link_state(pdev);
938 * Setup initial ASPM state. Note that we need to configure
939 * upstream links also because capable state of them can be
940 * update through pcie_aspm_cap_init().
942 pcie_aspm_cap_init(link, blacklist);
944 /* Setup initial Clock PM state */
945 pcie_clkpm_cap_init(link, blacklist);
948 * At this stage drivers haven't had an opportunity to change the
949 * link policy setting. Enabling ASPM on broken hardware can cripple
950 * it even before the driver has had a chance to disable ASPM, so
951 * default to a safe level right now. If we're enabling ASPM beyond
952 * the BIOS's expectation, we'll do so once pci_enable_device() is
955 if (aspm_policy != POLICY_POWERSAVE &&
956 aspm_policy != POLICY_POWER_SUPERSAVE) {
957 pcie_config_aspm_path(link);
958 pcie_set_clkpm(link, policy_to_clkpm_state(link));
962 mutex_unlock(&aspm_lock);
964 up_read(&pci_bus_sem);
967 /* Recheck latencies and update aspm_capable for links under the root */
968 static void pcie_update_aspm_capable(struct pcie_link_state *root)
970 struct pcie_link_state *link;
971 BUG_ON(root->parent);
972 list_for_each_entry(link, &link_list, sibling) {
973 if (link->root != root)
975 link->aspm_capable = link->aspm_support;
977 list_for_each_entry(link, &link_list, sibling) {
978 struct pci_dev *child;
979 struct pci_bus *linkbus = link->pdev->subordinate;
980 if (link->root != root)
982 list_for_each_entry(child, &linkbus->devices, bus_list) {
983 if ((pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT) &&
984 (pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END))
986 pcie_aspm_check_latency(child);
991 /* @pdev: the endpoint device */
992 void pcie_aspm_exit_link_state(struct pci_dev *pdev)
994 struct pci_dev *parent = pdev->bus->self;
995 struct pcie_link_state *link, *root, *parent_link;
997 if (!parent || !parent->link_state)
1000 down_read(&pci_bus_sem);
1001 mutex_lock(&aspm_lock);
1003 * All PCIe functions are in one slot, remove one function will remove
1004 * the whole slot, so just wait until we are the last function left.
1006 if (!list_empty(&parent->subordinate->devices))
1009 link = parent->link_state;
1011 parent_link = link->parent;
1013 /* All functions are removed, so just disable ASPM for the link */
1014 pcie_config_aspm_link(link, 0);
1015 list_del(&link->sibling);
1016 list_del(&link->link);
1017 /* Clock PM is for endpoint device */
1018 free_link_state(link);
1020 /* Recheck latencies and configure upstream links */
1022 pcie_update_aspm_capable(root);
1023 pcie_config_aspm_path(parent_link);
1026 mutex_unlock(&aspm_lock);
1027 up_read(&pci_bus_sem);
1030 /* @pdev: the root port or switch downstream port */
1031 void pcie_aspm_pm_state_change(struct pci_dev *pdev)
1033 struct pcie_link_state *link = pdev->link_state;
1035 if (aspm_disabled || !link)
1038 * Devices changed PM state, we should recheck if latency
1039 * meets all functions' requirement
1041 down_read(&pci_bus_sem);
1042 mutex_lock(&aspm_lock);
1043 pcie_update_aspm_capable(link->root);
1044 pcie_config_aspm_path(link);
1045 mutex_unlock(&aspm_lock);
1046 up_read(&pci_bus_sem);
1049 void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
1051 struct pcie_link_state *link = pdev->link_state;
1053 if (aspm_disabled || !link)
1056 if (aspm_policy != POLICY_POWERSAVE &&
1057 aspm_policy != POLICY_POWER_SUPERSAVE)
1060 down_read(&pci_bus_sem);
1061 mutex_lock(&aspm_lock);
1062 pcie_config_aspm_path(link);
1063 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1064 mutex_unlock(&aspm_lock);
1065 up_read(&pci_bus_sem);
1068 static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
1070 struct pci_dev *parent = pdev->bus->self;
1071 struct pcie_link_state *link;
1073 if (!pci_is_pcie(pdev))
1076 if (pdev->has_secondary_link)
1078 if (!parent || !parent->link_state)
1082 * A driver requested that ASPM be disabled on this device, but
1083 * if we don't have permission to manage ASPM (e.g., on ACPI
1084 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
1085 * the _OSC method), we can't honor that request. Windows has
1086 * a similar mechanism using "PciASPMOptOut", which is also
1087 * ignored in this situation.
1089 if (aspm_disabled) {
1090 pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n");
1095 down_read(&pci_bus_sem);
1096 mutex_lock(&aspm_lock);
1097 link = parent->link_state;
1098 if (state & PCIE_LINK_STATE_L0S)
1099 link->aspm_disable |= ASPM_STATE_L0S;
1100 if (state & PCIE_LINK_STATE_L1)
1101 link->aspm_disable |= ASPM_STATE_L1;
1102 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1104 if (state & PCIE_LINK_STATE_CLKPM)
1105 link->clkpm_disable = 1;
1106 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1107 mutex_unlock(&aspm_lock);
1109 up_read(&pci_bus_sem);
1112 void pci_disable_link_state_locked(struct pci_dev *pdev, int state)
1114 __pci_disable_link_state(pdev, state, false);
1116 EXPORT_SYMBOL(pci_disable_link_state_locked);
1119 * pci_disable_link_state - Disable device's link state, so the link will
1120 * never enter specific states. Note that if the BIOS didn't grant ASPM
1121 * control to the OS, this does nothing because we can't touch the LNKCTL
1125 * @state: ASPM link state to disable
1127 void pci_disable_link_state(struct pci_dev *pdev, int state)
1129 __pci_disable_link_state(pdev, state, true);
1131 EXPORT_SYMBOL(pci_disable_link_state);
1133 static int pcie_aspm_set_policy(const char *val,
1134 const struct kernel_param *kp)
1137 struct pcie_link_state *link;
1141 i = sysfs_match_string(policy_str, val);
1144 if (i == aspm_policy)
1147 down_read(&pci_bus_sem);
1148 mutex_lock(&aspm_lock);
1150 list_for_each_entry(link, &link_list, sibling) {
1151 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1152 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1154 mutex_unlock(&aspm_lock);
1155 up_read(&pci_bus_sem);
1159 static int pcie_aspm_get_policy(char *buffer, const struct kernel_param *kp)
1162 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
1163 if (i == aspm_policy)
1164 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
1166 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
1167 cnt += sprintf(buffer + cnt, "\n");
1171 module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
1174 #ifdef CONFIG_PCIEASPM_DEBUG
1175 static ssize_t link_state_show(struct device *dev,
1176 struct device_attribute *attr,
1179 struct pci_dev *pci_device = to_pci_dev(dev);
1180 struct pcie_link_state *link_state = pci_device->link_state;
1182 return sprintf(buf, "%d\n", link_state->aspm_enabled);
1185 static ssize_t link_state_store(struct device *dev,
1186 struct device_attribute *attr,
1190 struct pci_dev *pdev = to_pci_dev(dev);
1191 struct pcie_link_state *link, *root = pdev->link_state->root;
1197 if (kstrtouint(buf, 10, &state))
1199 if ((state & ~ASPM_STATE_ALL) != 0)
1202 down_read(&pci_bus_sem);
1203 mutex_lock(&aspm_lock);
1204 list_for_each_entry(link, &link_list, sibling) {
1205 if (link->root != root)
1207 pcie_config_aspm_link(link, state);
1209 mutex_unlock(&aspm_lock);
1210 up_read(&pci_bus_sem);
1214 static ssize_t clk_ctl_show(struct device *dev,
1215 struct device_attribute *attr,
1218 struct pci_dev *pci_device = to_pci_dev(dev);
1219 struct pcie_link_state *link_state = pci_device->link_state;
1221 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
1224 static ssize_t clk_ctl_store(struct device *dev,
1225 struct device_attribute *attr,
1229 struct pci_dev *pdev = to_pci_dev(dev);
1232 if (strtobool(buf, &state))
1235 down_read(&pci_bus_sem);
1236 mutex_lock(&aspm_lock);
1237 pcie_set_clkpm_nocheck(pdev->link_state, state);
1238 mutex_unlock(&aspm_lock);
1239 up_read(&pci_bus_sem);
1244 static DEVICE_ATTR_RW(link_state);
1245 static DEVICE_ATTR_RW(clk_ctl);
1247 static char power_group[] = "power";
1248 void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
1250 struct pcie_link_state *link_state = pdev->link_state;
1255 if (link_state->aspm_support)
1256 sysfs_add_file_to_group(&pdev->dev.kobj,
1257 &dev_attr_link_state.attr, power_group);
1258 if (link_state->clkpm_capable)
1259 sysfs_add_file_to_group(&pdev->dev.kobj,
1260 &dev_attr_clk_ctl.attr, power_group);
1263 void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
1265 struct pcie_link_state *link_state = pdev->link_state;
1270 if (link_state->aspm_support)
1271 sysfs_remove_file_from_group(&pdev->dev.kobj,
1272 &dev_attr_link_state.attr, power_group);
1273 if (link_state->clkpm_capable)
1274 sysfs_remove_file_from_group(&pdev->dev.kobj,
1275 &dev_attr_clk_ctl.attr, power_group);
1279 static int __init pcie_aspm_disable(char *str)
1281 if (!strcmp(str, "off")) {
1282 aspm_policy = POLICY_DEFAULT;
1284 aspm_support_enabled = false;
1285 printk(KERN_INFO "PCIe ASPM is disabled\n");
1286 } else if (!strcmp(str, "force")) {
1288 printk(KERN_INFO "PCIe ASPM is forcibly enabled\n");
1293 __setup("pcie_aspm=", pcie_aspm_disable);
1295 void pcie_no_aspm(void)
1298 * Disabling ASPM is intended to prevent the kernel from modifying
1299 * existing hardware state, not to clear existing state. To that end:
1300 * (a) set policy to POLICY_DEFAULT in order to avoid changing state
1301 * (b) prevent userspace from changing policy
1304 aspm_policy = POLICY_DEFAULT;
1309 bool pcie_aspm_support_enabled(void)
1311 return aspm_support_enabled;
1313 EXPORT_SYMBOL(pcie_aspm_support_enabled);