10 #include <linux/kernel.h>
11 #include <linux/module.h>
13 #include <linux/pci.h>
15 #include <linux/errno.h>
18 #include <linux/slab.h>
24 #ifdef MODULE_PARAM_PREFIX
25 #undef MODULE_PARAM_PREFIX
27 #define MODULE_PARAM_PREFIX "pcie_aspm."
30 #define ASPM_STATE_L0S_UP (1)
31 #define ASPM_STATE_L0S_DW (2)
32 #define ASPM_STATE_L1 (4)
33 #define ASPM_STATE_L0S (ASPM_STATE_L0S_UP | ASPM_STATE_L0S_DW)
34 #define ASPM_STATE_ALL (ASPM_STATE_L0S | ASPM_STATE_L1)
71 static int aspm_disabled, aspm_force;
72 static bool aspm_support_enabled =
true;
76 #define POLICY_DEFAULT 0
77 #define POLICY_PERFORMANCE 1
78 #define POLICY_POWERSAVE 2
80 #ifdef CONFIG_PCIEASPM_PERFORMANCE
82 #elif defined CONFIG_PCIEASPM_POWERSAVE
85 static int aspm_policy;
88 static const char *policy_str[] = {
94 #define LINK_RETRAIN_TIMEOUT HZ
98 switch (aspm_policy) {
113 switch (aspm_policy) {
150 pcie_set_clkpm_nocheck(link, enable);
153 static void pcie_clkpm_cap_init(
struct pcie_link_state *link,
int blacklist)
183 static void pcie_aspm_configure_common_clock(
struct pcie_link_state *link)
186 u16 reg16, parent_reg, child_reg[8];
187 unsigned long start_jiffies;
195 BUG_ON(!pci_is_pcie(child));
204 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
241 if (!(reg16 & PCI_EXP_LNKSTA_LT))
246 "ASPM: Could not configure common clock\n");
254 static
u32 calc_l0s_latency(
u32 encoding)
258 return (64 << encoding);
262 static u32 calc_l0s_acceptable(
u32 encoding)
266 return (64 << encoding);
270 static u32 calc_l1_latency(
u32 encoding)
274 return (1000 << encoding);
278 static u32 calc_l1_acceptable(
u32 encoding)
282 return (1000 << encoding);
292 static void pcie_get_aspm_reg(
struct pci_dev *pdev,
317 link = endpoint->
bus->self->link_state;
337 (latency + l1_switch_latency > acceptable->
l1))
339 l1_switch_latency += 1000;
345 static void pcie_aspm_cap_init(
struct pcie_link_state *link,
int blacklist)
359 pcie_aspm_configure_common_clock(link);
362 pcie_get_aspm_reg(parent, &upreg);
364 pcie_get_aspm_reg(child, &dwreg);
379 link->
latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s);
380 link->
latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s);
387 link->
latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1);
388 link->
latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1);
419 acceptable->
l0s = calc_l0s_acceptable(encoding);
422 acceptable->
l1 = calc_l1_acceptable(encoding);
424 pcie_aspm_check_latency(child);
428 static void pcie_config_aspm_dev(
struct pci_dev *pdev,
u32 val)
435 u32 upstream = 0, dwstream = 0;
458 if (state & ASPM_STATE_L1)
459 pcie_config_aspm_dev(parent, upstream);
461 pcie_config_aspm_dev(child, dwstream);
462 if (!(state & ASPM_STATE_L1))
463 pcie_config_aspm_dev(parent, upstream);
465 link->aspm_enabled = state;
471 pcie_config_aspm_link(link, policy_to_aspm_state(link));
482 static int pcie_aspm_sanity_check(
struct pci_dev *pdev)
492 if (!pci_is_pcie(child))
511 " on pre-1.1 PCIe device. You can enable it"
512 " with 'pcie_aspm=force'\n");
526 INIT_LIST_HEAD(&link->
sibling);
528 INIT_LIST_HEAD(&link->
link);
532 parent = pdev->
bus->parent->self->link_state;
546 list_add(&link->
sibling, &link_list);
547 pdev->link_state =
link;
559 int blacklist = !!pcie_aspm_sanity_check(pdev);
561 if (!pci_is_pcie(pdev) || pdev->link_state)
577 link = alloc_pcie_link_state(pdev);
585 pcie_aspm_cap_init(link, blacklist);
588 pcie_clkpm_cap_init(link, blacklist);
599 pcie_config_aspm_path(link);
600 pcie_set_clkpm(link, policy_to_clkpm_state(link));
615 if (link->
root != root)
622 if (link->
root != root)
628 pcie_aspm_check_latency(child);
639 if (!pci_is_pcie(pdev) || !parent || !parent->link_state)
654 link = parent->link_state;
656 parent_link = link->
parent;
659 pcie_config_aspm_link(link, 0);
663 free_link_state(link);
667 pcie_update_aspm_capable(root);
668 pcie_config_aspm_path(parent_link);
680 if (aspm_disabled || !pci_is_pcie(pdev) || !link)
691 pcie_update_aspm_capable(link->
root);
692 pcie_config_aspm_path(link);
701 if (aspm_disabled || !pci_is_pcie(pdev) || !link)
713 pcie_config_aspm_path(link);
714 pcie_set_clkpm(link, policy_to_clkpm_state(link));
723 static void __pci_disable_link_state(
struct pci_dev *pdev,
int state,
bool sem,
729 if (aspm_disabled && !force)
732 if (!pci_is_pcie(pdev))
738 if (!parent || !parent->link_state)
744 link = parent->link_state;
749 pcie_config_aspm_link(link, policy_to_aspm_state(link));
753 pcie_set_clkpm(link, 0);
762 __pci_disable_link_state(pdev, state,
false,
false);
768 __pci_disable_link_state(pdev, state,
true,
false);
780 __pci_disable_link_state(child, PCIE_LINK_STATE_L0S |
782 PCIE_LINK_STATE_CLKPM,
787 static int pcie_aspm_set_policy(
const char *
val,
struct kernel_param *kp)
799 if (i == aspm_policy)
806 pcie_config_aspm_link(link, policy_to_aspm_state(link));
807 pcie_set_clkpm(link, policy_to_clkpm_state(link));
818 if (i == aspm_policy)
819 cnt +=
sprintf(buffer + cnt,
"[%s] ", policy_str[i]);
821 cnt +=
sprintf(buffer + cnt,
"%s ", policy_str[i]);
828 #ifdef CONFIG_PCIEASPM_DEBUG
846 u32 val = buf[0] -
'0', state = 0;
850 if (n < 1 || val > 3)
854 if (val & PCIE_LINK_STATE_L0S)
856 if (val & PCIE_LINK_STATE_L1)
862 if (link->
root != root)
864 pcie_config_aspm_link(link, state);
895 pcie_set_clkpm_nocheck(pdev->link_state, !!state);
903 static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
905 static char power_group[] =
"power";
906 void pcie_aspm_create_sysfs_dev_files(
struct pci_dev *pdev)
910 if (!pci_is_pcie(pdev) ||
917 &dev_attr_link_state.attr, power_group);
920 &dev_attr_clk_ctl.attr, power_group);
923 void pcie_aspm_remove_sysfs_dev_files(
struct pci_dev *pdev)
927 if (!pci_is_pcie(pdev) ||
934 &dev_attr_link_state.attr, power_group);
937 &dev_attr_clk_ctl.attr, power_group);
941 static int __init pcie_aspm_disable(
char *
str)
943 if (!
strcmp(str,
"off")) {
946 aspm_support_enabled =
false;
948 }
else if (!
strcmp(str,
"force")) {
955 __setup(
"pcie_aspm=", pcie_aspm_disable);
979 return !aspm_disabled;
985 return aspm_support_enabled;