Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 1 | /* |
| 2 | * File: drivers/pci/pcie/aspm.c |
| 3 | * Enabling PCIE link L0s/L1 state and Clock Power Management |
| 4 | * |
| 5 | * Copyright (C) 2007 Intel |
| 6 | * Copyright (C) Zhang Yanmin (yanmin.zhang@intel.com) |
| 7 | * Copyright (C) Shaohua Li (shaohua.li@intel.com) |
| 8 | */ |
| 9 | |
| 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> |
| 16 | #include <linux/pm.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/slab.h> |
Thomas Renninger | 2a42d9d | 2008-12-09 13:05:09 +0100 | [diff] [blame] | 19 | #include <linux/jiffies.h> |
Andrew Patterson | 987a4c7 | 2009-01-05 16:21:04 -0700 | [diff] [blame] | 20 | #include <linux/delay.h> |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 21 | #include <linux/pci-aspm.h> |
| 22 | #include "../pci.h" |
| 23 | |
| 24 | #ifdef MODULE_PARAM_PREFIX |
| 25 | #undef MODULE_PARAM_PREFIX |
| 26 | #endif |
| 27 | #define MODULE_PARAM_PREFIX "pcie_aspm." |
| 28 | |
Kenji Kaneshige | b6c2e54 | 2009-05-13 12:14:58 +0900 | [diff] [blame] | 29 | struct aspm_latency { |
| 30 | u32 l0s; /* L0s latency (nsec) */ |
| 31 | u32 l1; /* L1 latency (nsec) */ |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | struct pcie_link_state { |
Kenji Kaneshige | 5cde89d | 2009-05-13 12:17:04 +0900 | [diff] [blame] | 35 | struct pci_dev *pdev; /* Upstream component of the Link */ |
| 36 | struct pcie_link_state *parent; /* pointer to the parent Link state */ |
| 37 | struct list_head sibling; /* node in link_list */ |
| 38 | struct list_head children; /* list of child link states */ |
| 39 | struct list_head link; /* node in parent's children list */ |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 40 | |
| 41 | /* ASPM state */ |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 42 | u32 aspm_support:2; /* Supported ASPM state */ |
| 43 | u32 aspm_enabled:2; /* Enabled ASPM state */ |
| 44 | u32 aspm_default:2; /* Default ASPM state by BIOS */ |
| 45 | |
Kenji Kaneshige | 4d246e4 | 2009-05-13 12:15:38 +0900 | [diff] [blame] | 46 | /* Clock PM state */ |
| 47 | u32 clkpm_capable:1; /* Clock PM capable? */ |
| 48 | u32 clkpm_enabled:1; /* Current Clock PM state */ |
| 49 | u32 clkpm_default:1; /* Default Clock PM state by BIOS */ |
| 50 | |
Kenji Kaneshige | 5cde89d | 2009-05-13 12:17:04 +0900 | [diff] [blame] | 51 | u32 has_switch:1; /* Downstream has switches? */ |
| 52 | |
Kenji Kaneshige | b6c2e54 | 2009-05-13 12:14:58 +0900 | [diff] [blame] | 53 | /* Latencies */ |
| 54 | struct aspm_latency latency; /* Exit latency */ |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 55 | /* |
Kenji Kaneshige | b6c2e54 | 2009-05-13 12:14:58 +0900 | [diff] [blame] | 56 | * Endpoint acceptable latencies. A pcie downstream port only |
| 57 | * has one slot under it, so at most there are 8 functions. |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 58 | */ |
Kenji Kaneshige | b6c2e54 | 2009-05-13 12:14:58 +0900 | [diff] [blame] | 59 | struct aspm_latency acceptable[8]; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 60 | }; |
| 61 | |
Shaohua Li | d6d3857 | 2008-07-23 10:32:42 +0800 | [diff] [blame] | 62 | static int aspm_disabled, aspm_force; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 63 | static DEFINE_MUTEX(aspm_lock); |
| 64 | static LIST_HEAD(link_list); |
| 65 | |
| 66 | #define POLICY_DEFAULT 0 /* BIOS default setting */ |
| 67 | #define POLICY_PERFORMANCE 1 /* high performance */ |
| 68 | #define POLICY_POWERSAVE 2 /* high power saving */ |
| 69 | static int aspm_policy; |
| 70 | static const char *policy_str[] = { |
| 71 | [POLICY_DEFAULT] = "default", |
| 72 | [POLICY_PERFORMANCE] = "performance", |
| 73 | [POLICY_POWERSAVE] = "powersave" |
| 74 | }; |
| 75 | |
Andrew Patterson | 987a4c7 | 2009-01-05 16:21:04 -0700 | [diff] [blame] | 76 | #define LINK_RETRAIN_TIMEOUT HZ |
| 77 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 78 | static int policy_to_aspm_state(struct pcie_link_state *link) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 79 | { |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 80 | switch (aspm_policy) { |
| 81 | case POLICY_PERFORMANCE: |
| 82 | /* Disable ASPM and Clock PM */ |
| 83 | return 0; |
| 84 | case POLICY_POWERSAVE: |
| 85 | /* Enable ASPM L0s/L1 */ |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 86 | return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 87 | case POLICY_DEFAULT: |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 88 | return link->aspm_default; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 93 | static int policy_to_clkpm_state(struct pcie_link_state *link) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 94 | { |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 95 | switch (aspm_policy) { |
| 96 | case POLICY_PERFORMANCE: |
| 97 | /* Disable ASPM and Clock PM */ |
| 98 | return 0; |
| 99 | case POLICY_POWERSAVE: |
| 100 | /* Disable Clock PM */ |
| 101 | return 1; |
| 102 | case POLICY_DEFAULT: |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 103 | return link->clkpm_default; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 108 | static void pcie_set_clock_pm(struct pcie_link_state *link, int enable) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 109 | { |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 110 | int pos; |
| 111 | u16 reg16; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 112 | struct pci_dev *child; |
| 113 | struct pci_bus *linkbus = link->pdev->subordinate; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 114 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 115 | list_for_each_entry(child, &linkbus->devices, bus_list) { |
| 116 | pos = pci_find_capability(child, PCI_CAP_ID_EXP); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 117 | if (!pos) |
| 118 | return; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 119 | pci_read_config_word(child, pos + PCI_EXP_LNKCTL, ®16); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 120 | if (enable) |
| 121 | reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN; |
| 122 | else |
| 123 | reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 124 | pci_write_config_word(child, pos + PCI_EXP_LNKCTL, reg16); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 125 | } |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 126 | link->clkpm_enabled = !!enable; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 127 | } |
| 128 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 129 | static void pcie_check_clock_pm(struct pcie_link_state *link, int blacklist) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 130 | { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 131 | int pos, capable = 1, enabled = 1; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 132 | u32 reg32; |
| 133 | u16 reg16; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 134 | struct pci_dev *child; |
| 135 | struct pci_bus *linkbus = link->pdev->subordinate; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 136 | |
| 137 | /* All functions should have the same cap and state, take the worst */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 138 | list_for_each_entry(child, &linkbus->devices, bus_list) { |
| 139 | pos = pci_find_capability(child, PCI_CAP_ID_EXP); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 140 | if (!pos) |
| 141 | return; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 142 | pci_read_config_dword(child, pos + PCI_EXP_LNKCAP, ®32); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 143 | if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) { |
| 144 | capable = 0; |
| 145 | enabled = 0; |
| 146 | break; |
| 147 | } |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 148 | pci_read_config_word(child, pos + PCI_EXP_LNKCTL, ®16); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 149 | if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN)) |
| 150 | enabled = 0; |
| 151 | } |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 152 | link->clkpm_enabled = enabled; |
| 153 | link->clkpm_default = enabled; |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 154 | if (!blacklist) { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 155 | link->clkpm_capable = capable; |
| 156 | pcie_set_clock_pm(link, policy_to_clkpm_state(link)); |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 157 | } else { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 158 | link->clkpm_capable = 0; |
| 159 | pcie_set_clock_pm(link, 0); |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 163 | static bool pcie_aspm_downstream_has_switch(struct pcie_link_state *link) |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 164 | { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 165 | struct pci_dev *child; |
| 166 | struct pci_bus *linkbus = link->pdev->subordinate; |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 167 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 168 | list_for_each_entry(child, &linkbus->devices, bus_list) { |
| 169 | if (child->pcie_type == PCI_EXP_TYPE_UPSTREAM) |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 170 | return true; |
| 171 | } |
| 172 | return false; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* |
| 176 | * pcie_aspm_configure_common_clock: check if the 2 ends of a link |
| 177 | * could use common clock. If they are, configure them to use the |
| 178 | * common clock. That will reduce the ASPM state exit latency. |
| 179 | */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 180 | static void pcie_aspm_configure_common_clock(struct pcie_link_state *link) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 181 | { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 182 | int ppos, cpos, same_clock = 1; |
| 183 | u16 reg16, parent_reg, child_reg[8]; |
Thomas Renninger | 2a42d9d | 2008-12-09 13:05:09 +0100 | [diff] [blame] | 184 | unsigned long start_jiffies; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 185 | struct pci_dev *child, *parent = link->pdev; |
| 186 | struct pci_bus *linkbus = parent->subordinate; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 187 | /* |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 188 | * All functions of a slot should have the same Slot Clock |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 189 | * Configuration, so just check one function |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 190 | */ |
| 191 | child = list_entry(linkbus->devices.next, struct pci_dev, bus_list); |
| 192 | BUG_ON(!child->is_pcie); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 193 | |
| 194 | /* Check downstream component if bit Slot Clock Configuration is 1 */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 195 | cpos = pci_find_capability(child, PCI_CAP_ID_EXP); |
| 196 | pci_read_config_word(child, cpos + PCI_EXP_LNKSTA, ®16); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 197 | if (!(reg16 & PCI_EXP_LNKSTA_SLC)) |
| 198 | same_clock = 0; |
| 199 | |
| 200 | /* Check upstream component if bit Slot Clock Configuration is 1 */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 201 | ppos = pci_find_capability(parent, PCI_CAP_ID_EXP); |
| 202 | pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, ®16); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 203 | if (!(reg16 & PCI_EXP_LNKSTA_SLC)) |
| 204 | same_clock = 0; |
| 205 | |
| 206 | /* Configure downstream component, all functions */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 207 | list_for_each_entry(child, &linkbus->devices, bus_list) { |
| 208 | cpos = pci_find_capability(child, PCI_CAP_ID_EXP); |
| 209 | pci_read_config_word(child, cpos + PCI_EXP_LNKCTL, ®16); |
| 210 | child_reg[PCI_FUNC(child->devfn)] = reg16; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 211 | if (same_clock) |
| 212 | reg16 |= PCI_EXP_LNKCTL_CCC; |
| 213 | else |
| 214 | reg16 &= ~PCI_EXP_LNKCTL_CCC; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 215 | pci_write_config_word(child, cpos + PCI_EXP_LNKCTL, reg16); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* Configure upstream component */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 219 | pci_read_config_word(parent, ppos + PCI_EXP_LNKCTL, ®16); |
Thomas Renninger | 2a42d9d | 2008-12-09 13:05:09 +0100 | [diff] [blame] | 220 | parent_reg = reg16; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 221 | if (same_clock) |
| 222 | reg16 |= PCI_EXP_LNKCTL_CCC; |
| 223 | else |
| 224 | reg16 &= ~PCI_EXP_LNKCTL_CCC; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 225 | pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 226 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 227 | /* Retrain link */ |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 228 | reg16 |= PCI_EXP_LNKCTL_RL; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 229 | pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 230 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 231 | /* Wait for link training end. Break out after waiting for timeout */ |
Thomas Renninger | 2a42d9d | 2008-12-09 13:05:09 +0100 | [diff] [blame] | 232 | start_jiffies = jiffies; |
Andrew Patterson | 987a4c7 | 2009-01-05 16:21:04 -0700 | [diff] [blame] | 233 | for (;;) { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 234 | pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, ®16); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 235 | if (!(reg16 & PCI_EXP_LNKSTA_LT)) |
| 236 | break; |
Andrew Patterson | 987a4c7 | 2009-01-05 16:21:04 -0700 | [diff] [blame] | 237 | if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) |
| 238 | break; |
| 239 | msleep(1); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 240 | } |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 241 | if (!(reg16 & PCI_EXP_LNKSTA_LT)) |
| 242 | return; |
| 243 | |
| 244 | /* Training failed. Restore common clock configurations */ |
| 245 | dev_printk(KERN_ERR, &parent->dev, |
| 246 | "ASPM: Could not configure common clock\n"); |
| 247 | list_for_each_entry(child, &linkbus->devices, bus_list) { |
| 248 | cpos = pci_find_capability(child, PCI_CAP_ID_EXP); |
| 249 | pci_write_config_word(child, cpos + PCI_EXP_LNKCTL, |
| 250 | child_reg[PCI_FUNC(child->devfn)]); |
Thomas Renninger | 2a42d9d | 2008-12-09 13:05:09 +0100 | [diff] [blame] | 251 | } |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 252 | pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, parent_reg); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
| 256 | * calc_L0S_latency: Convert L0s latency encoding to ns |
| 257 | */ |
| 258 | static unsigned int calc_L0S_latency(unsigned int latency_encoding, int ac) |
| 259 | { |
| 260 | unsigned int ns = 64; |
| 261 | |
| 262 | if (latency_encoding == 0x7) { |
| 263 | if (ac) |
| 264 | ns = -1U; |
| 265 | else |
| 266 | ns = 5*1000; /* > 4us */ |
| 267 | } else |
| 268 | ns *= (1 << latency_encoding); |
| 269 | return ns; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * calc_L1_latency: Convert L1 latency encoding to ns |
| 274 | */ |
| 275 | static unsigned int calc_L1_latency(unsigned int latency_encoding, int ac) |
| 276 | { |
| 277 | unsigned int ns = 1000; |
| 278 | |
| 279 | if (latency_encoding == 0x7) { |
| 280 | if (ac) |
| 281 | ns = -1U; |
| 282 | else |
| 283 | ns = 65*1000; /* > 64us */ |
| 284 | } else |
| 285 | ns *= (1 << latency_encoding); |
| 286 | return ns; |
| 287 | } |
| 288 | |
| 289 | static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state, |
| 290 | unsigned int *l0s, unsigned int *l1, unsigned int *enabled) |
| 291 | { |
| 292 | int pos; |
| 293 | u16 reg16; |
| 294 | u32 reg32; |
| 295 | unsigned int latency; |
| 296 | |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 297 | *l0s = *l1 = *enabled = 0; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 298 | pos = pci_find_capability(pdev, PCI_CAP_ID_EXP); |
| 299 | pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, ®32); |
| 300 | *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10; |
| 301 | if (*state != PCIE_LINK_STATE_L0S && |
| 302 | *state != (PCIE_LINK_STATE_L1|PCIE_LINK_STATE_L0S)) |
| 303 | *state = 0; |
| 304 | if (*state == 0) |
| 305 | return; |
| 306 | |
| 307 | latency = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12; |
| 308 | *l0s = calc_L0S_latency(latency, 0); |
| 309 | if (*state & PCIE_LINK_STATE_L1) { |
| 310 | latency = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15; |
| 311 | *l1 = calc_L1_latency(latency, 0); |
| 312 | } |
| 313 | pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, ®16); |
| 314 | *enabled = reg16 & (PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1); |
| 315 | } |
| 316 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 317 | static void pcie_aspm_cap_init(struct pcie_link_state *link) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 318 | { |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 319 | u32 support, l0s, l1, enabled; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 320 | struct pci_dev *child, *parent = link->pdev; |
| 321 | struct pci_bus *linkbus = parent->subordinate; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 322 | |
| 323 | /* upstream component states */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 324 | pcie_aspm_get_cap_device(parent, &support, &l0s, &l1, &enabled); |
| 325 | link->aspm_support = support; |
| 326 | link->latency.l0s = l0s; |
| 327 | link->latency.l1 = l1; |
| 328 | link->aspm_enabled = enabled; |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 329 | |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 330 | /* downstream component states, all functions have the same setting */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 331 | child = list_entry(linkbus->devices.next, struct pci_dev, bus_list); |
| 332 | pcie_aspm_get_cap_device(child, &support, &l0s, &l1, &enabled); |
| 333 | link->aspm_support &= support; |
| 334 | link->latency.l0s = max_t(u32, link->latency.l0s, l0s); |
| 335 | link->latency.l1 = max_t(u32, link->latency.l1, l1); |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 336 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 337 | if (!link->aspm_support) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 338 | return; |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 339 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 340 | link->aspm_enabled &= link->aspm_support; |
| 341 | link->aspm_default = link->aspm_enabled; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 342 | |
| 343 | /* ENDPOINT states*/ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 344 | list_for_each_entry(child, &linkbus->devices, bus_list) { |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 345 | int pos; |
| 346 | u32 reg32; |
| 347 | unsigned int latency; |
Kenji Kaneshige | b6c2e54 | 2009-05-13 12:14:58 +0900 | [diff] [blame] | 348 | struct aspm_latency *acceptable = |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 349 | &link->acceptable[PCI_FUNC(child->devfn)]; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 350 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 351 | if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT && |
| 352 | child->pcie_type != PCI_EXP_TYPE_LEG_END) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 353 | continue; |
| 354 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 355 | pos = pci_find_capability(child, PCI_CAP_ID_EXP); |
| 356 | pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, ®32); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 357 | latency = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6; |
| 358 | latency = calc_L0S_latency(latency, 1); |
Kenji Kaneshige | b6c2e54 | 2009-05-13 12:14:58 +0900 | [diff] [blame] | 359 | acceptable->l0s = latency; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 360 | if (link->aspm_support & PCIE_LINK_STATE_L1) { |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 361 | latency = (reg32 & PCI_EXP_DEVCAP_L1) >> 9; |
| 362 | latency = calc_L1_latency(latency, 1); |
Kenji Kaneshige | b6c2e54 | 2009-05-13 12:14:58 +0900 | [diff] [blame] | 363 | acceptable->l1 = latency; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | static unsigned int __pcie_aspm_check_state_one(struct pci_dev *pdev, |
| 369 | unsigned int state) |
| 370 | { |
| 371 | struct pci_dev *parent_dev, *tmp_dev; |
Kenji Kaneshige | b6c2e54 | 2009-05-13 12:14:58 +0900 | [diff] [blame] | 372 | unsigned int l1_latency = 0; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 373 | struct pcie_link_state *link_state; |
Kenji Kaneshige | b6c2e54 | 2009-05-13 12:14:58 +0900 | [diff] [blame] | 374 | struct aspm_latency *acceptable; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 375 | |
| 376 | parent_dev = pdev->bus->self; |
| 377 | link_state = parent_dev->link_state; |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 378 | state &= link_state->aspm_support; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 379 | if (state == 0) |
| 380 | return 0; |
Kenji Kaneshige | b6c2e54 | 2009-05-13 12:14:58 +0900 | [diff] [blame] | 381 | acceptable = &link_state->acceptable[PCI_FUNC(pdev->devfn)]; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 382 | |
| 383 | /* |
| 384 | * Check latency for endpoint device. |
| 385 | * TBD: The latency from the endpoint to root complex vary per |
| 386 | * switch's upstream link state above the device. Here we just do a |
| 387 | * simple check which assumes all links above the device can be in L1 |
| 388 | * state, that is we just consider the worst case. If switch's upstream |
| 389 | * link can't be put into L0S/L1, then our check is too strictly. |
| 390 | */ |
| 391 | tmp_dev = pdev; |
| 392 | while (state & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1)) { |
| 393 | parent_dev = tmp_dev->bus->self; |
| 394 | link_state = parent_dev->link_state; |
Kenji Kaneshige | b6c2e54 | 2009-05-13 12:14:58 +0900 | [diff] [blame] | 395 | if ((state & PCIE_LINK_STATE_L0S) && |
| 396 | (link_state->latency.l0s > acceptable->l0s)) |
| 397 | state &= ~PCIE_LINK_STATE_L0S; |
| 398 | |
| 399 | if ((state & PCIE_LINK_STATE_L1) && |
| 400 | (link_state->latency.l1 + l1_latency > acceptable->l1)) |
| 401 | state &= ~PCIE_LINK_STATE_L1; |
| 402 | |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 403 | if (!parent_dev->bus->self) /* parent_dev is a root port */ |
| 404 | break; |
| 405 | else { |
| 406 | /* |
| 407 | * parent_dev is the downstream port of a switch, make |
| 408 | * tmp_dev the upstream port of the switch |
| 409 | */ |
| 410 | tmp_dev = parent_dev->bus->self; |
| 411 | /* |
| 412 | * every switch on the path to root complex need 1 more |
| 413 | * microsecond for L1. Spec doesn't mention L0S. |
| 414 | */ |
| 415 | if (state & PCIE_LINK_STATE_L1) |
| 416 | l1_latency += 1000; |
| 417 | } |
| 418 | } |
| 419 | return state; |
| 420 | } |
| 421 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 422 | static u32 pcie_aspm_check_state(struct pcie_link_state *link, u32 state) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 423 | { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 424 | pci_power_t power_state; |
| 425 | struct pci_dev *child; |
| 426 | struct pci_bus *linkbus = link->pdev->subordinate; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 427 | |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 428 | /* If no child, ignore the link */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 429 | if (list_empty(&linkbus->devices)) |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 430 | return state; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 431 | |
| 432 | list_for_each_entry(child, &linkbus->devices, bus_list) { |
| 433 | /* |
| 434 | * If downstream component of a link is pci bridge, we |
| 435 | * disable ASPM for now for the link |
| 436 | */ |
| 437 | if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) |
| 438 | return 0; |
| 439 | |
| 440 | if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT && |
| 441 | child->pcie_type != PCI_EXP_TYPE_LEG_END)) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 442 | continue; |
| 443 | /* Device not in D0 doesn't need check latency */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 444 | power_state = child->current_state; |
| 445 | if (power_state == PCI_D1 || power_state == PCI_D2 || |
| 446 | power_state == PCI_D3hot || power_state == PCI_D3cold) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 447 | continue; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 448 | state = __pcie_aspm_check_state_one(child, state); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 449 | } |
| 450 | return state; |
| 451 | } |
| 452 | |
| 453 | static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state) |
| 454 | { |
| 455 | u16 reg16; |
| 456 | int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP); |
| 457 | |
| 458 | pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, ®16); |
| 459 | reg16 &= ~0x3; |
| 460 | reg16 |= state; |
| 461 | pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16); |
| 462 | } |
| 463 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 464 | static void __pcie_aspm_config_link(struct pcie_link_state *link, u32 state) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 465 | { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 466 | struct pci_dev *child, *parent = link->pdev; |
| 467 | struct pci_bus *linkbus = parent->subordinate; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 468 | |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 469 | /* If no child, disable the link */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 470 | if (list_empty(&linkbus->devices)) |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 471 | state = 0; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 472 | /* |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 473 | * If the downstream component has pci bridge function, don't |
| 474 | * do ASPM now. |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 475 | */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 476 | list_for_each_entry(child, &linkbus->devices, bus_list) { |
| 477 | if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) |
| 478 | return; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 479 | } |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 480 | /* |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 481 | * Spec 2.0 suggests all functions should be configured the |
| 482 | * same setting for ASPM. Enabling ASPM L1 should be done in |
| 483 | * upstream component first and then downstream, and vice |
| 484 | * versa for disabling ASPM L1. Spec doesn't mention L0S. |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 485 | */ |
| 486 | if (state & PCIE_LINK_STATE_L1) |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 487 | __pcie_aspm_config_one_dev(parent, state); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 488 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 489 | list_for_each_entry(child, &linkbus->devices, bus_list) |
| 490 | __pcie_aspm_config_one_dev(child, state); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 491 | |
| 492 | if (!(state & PCIE_LINK_STATE_L1)) |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 493 | __pcie_aspm_config_one_dev(parent, state); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 494 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 495 | link->aspm_enabled = state; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 496 | } |
| 497 | |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 498 | static struct pcie_link_state *get_root_port_link(struct pcie_link_state *link) |
| 499 | { |
| 500 | struct pcie_link_state *root_port_link = link; |
| 501 | while (root_port_link->parent) |
| 502 | root_port_link = root_port_link->parent; |
| 503 | return root_port_link; |
| 504 | } |
| 505 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 506 | /* Check the whole hierarchy, and configure each link in the hierarchy */ |
| 507 | static void __pcie_aspm_configure_link_state(struct pcie_link_state *link, |
| 508 | u32 state) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 509 | { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 510 | struct pcie_link_state *leaf, *root = get_root_port_link(link); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 511 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 512 | state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 513 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 514 | /* Check all links who have specific root port link */ |
Kenji Kaneshige | dc64cd1 | 2009-05-13 12:11:33 +0900 | [diff] [blame] | 515 | list_for_each_entry(leaf, &link_list, sibling) { |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 516 | if (!list_empty(&leaf->children) || |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 517 | get_root_port_link(leaf) != root) |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 518 | continue; |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 519 | state = pcie_aspm_check_state(leaf, state); |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 520 | } |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 521 | /* Check root port link too in case it hasn't children */ |
| 522 | state = pcie_aspm_check_state(root, state); |
| 523 | if (link->aspm_enabled == state) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 524 | return; |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 525 | /* |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 526 | * We must change the hierarchy. See comments in |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 527 | * __pcie_aspm_config_link for the order |
| 528 | **/ |
| 529 | if (state & PCIE_LINK_STATE_L1) { |
Kenji Kaneshige | dc64cd1 | 2009-05-13 12:11:33 +0900 | [diff] [blame] | 530 | list_for_each_entry(leaf, &link_list, sibling) { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 531 | if (get_root_port_link(leaf) == root) |
| 532 | __pcie_aspm_config_link(leaf, state); |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 533 | } |
| 534 | } else { |
Kenji Kaneshige | dc64cd1 | 2009-05-13 12:11:33 +0900 | [diff] [blame] | 535 | list_for_each_entry_reverse(leaf, &link_list, sibling) { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 536 | if (get_root_port_link(leaf) == root) |
| 537 | __pcie_aspm_config_link(leaf, state); |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 538 | } |
| 539 | } |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* |
| 543 | * pcie_aspm_configure_link_state: enable/disable PCI express link state |
| 544 | * @pdev: the root port or switch downstream port |
| 545 | */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 546 | static void pcie_aspm_configure_link_state(struct pcie_link_state *link, |
| 547 | u32 state) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 548 | { |
| 549 | down_read(&pci_bus_sem); |
| 550 | mutex_lock(&aspm_lock); |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 551 | __pcie_aspm_configure_link_state(link, state); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 552 | mutex_unlock(&aspm_lock); |
| 553 | up_read(&pci_bus_sem); |
| 554 | } |
| 555 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 556 | static void free_link_state(struct pcie_link_state *link) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 557 | { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 558 | link->pdev->link_state = NULL; |
| 559 | kfree(link); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 560 | } |
| 561 | |
Shaohua Li | ddc9753 | 2008-05-21 16:58:40 +0800 | [diff] [blame] | 562 | static int pcie_aspm_sanity_check(struct pci_dev *pdev) |
| 563 | { |
| 564 | struct pci_dev *child_dev; |
| 565 | int child_pos; |
Shaohua Li | 149e163 | 2008-07-23 10:32:31 +0800 | [diff] [blame] | 566 | u32 reg32; |
Shaohua Li | ddc9753 | 2008-05-21 16:58:40 +0800 | [diff] [blame] | 567 | |
| 568 | /* |
| 569 | * Some functions in a slot might not all be PCIE functions, very |
| 570 | * strange. Disable ASPM for the whole slot |
| 571 | */ |
| 572 | list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) { |
| 573 | child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP); |
| 574 | if (!child_pos) |
| 575 | return -EINVAL; |
Shaohua Li | 149e163 | 2008-07-23 10:32:31 +0800 | [diff] [blame] | 576 | |
| 577 | /* |
| 578 | * Disable ASPM for pre-1.1 PCIe device, we follow MS to use |
| 579 | * RBER bit to determine if a function is 1.1 version device |
| 580 | */ |
| 581 | pci_read_config_dword(child_dev, child_pos + PCI_EXP_DEVCAP, |
| 582 | ®32); |
Sitsofe Wheeler | e1f4f59 | 2008-09-16 14:27:13 +0100 | [diff] [blame] | 583 | if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) { |
Vincent Legoll | f393d9b | 2008-10-12 12:26:12 +0200 | [diff] [blame] | 584 | dev_printk(KERN_INFO, &child_dev->dev, "disabling ASPM" |
| 585 | " on pre-1.1 PCIe device. You can enable it" |
| 586 | " with 'pcie_aspm=force'\n"); |
Shaohua Li | 149e163 | 2008-07-23 10:32:31 +0800 | [diff] [blame] | 587 | return -EINVAL; |
| 588 | } |
Shaohua Li | ddc9753 | 2008-05-21 16:58:40 +0800 | [diff] [blame] | 589 | } |
| 590 | return 0; |
| 591 | } |
| 592 | |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 593 | /* |
| 594 | * pcie_aspm_init_link_state: Initiate PCI express link state. |
| 595 | * It is called after the pcie and its children devices are scaned. |
| 596 | * @pdev: the root port or switch downstream port |
| 597 | */ |
| 598 | void pcie_aspm_init_link_state(struct pci_dev *pdev) |
| 599 | { |
| 600 | unsigned int state; |
| 601 | struct pcie_link_state *link_state; |
| 602 | int error = 0; |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 603 | int blacklist; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 604 | |
| 605 | if (aspm_disabled || !pdev->is_pcie || pdev->link_state) |
| 606 | return; |
| 607 | if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT && |
| 608 | pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) |
| 609 | return; |
Shaohua Li | 8e822df | 2009-06-08 09:27:25 +0800 | [diff] [blame] | 610 | /* VIA has a strange chipset, root port is under a bridge */ |
| 611 | if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT && |
| 612 | pdev->bus->self) |
| 613 | return; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 614 | down_read(&pci_bus_sem); |
| 615 | if (list_empty(&pdev->subordinate->devices)) |
| 616 | goto out; |
| 617 | |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 618 | blacklist = !!pcie_aspm_sanity_check(pdev); |
Shaohua Li | ddc9753 | 2008-05-21 16:58:40 +0800 | [diff] [blame] | 619 | |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 620 | mutex_lock(&aspm_lock); |
| 621 | |
| 622 | link_state = kzalloc(sizeof(*link_state), GFP_KERNEL); |
| 623 | if (!link_state) |
| 624 | goto unlock_out; |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 625 | |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 626 | INIT_LIST_HEAD(&link_state->children); |
| 627 | INIT_LIST_HEAD(&link_state->link); |
| 628 | if (pdev->bus->self) {/* this is a switch */ |
| 629 | struct pcie_link_state *parent_link_state; |
| 630 | |
| 631 | parent_link_state = pdev->bus->parent->self->link_state; |
| 632 | if (!parent_link_state) { |
| 633 | kfree(link_state); |
| 634 | goto unlock_out; |
| 635 | } |
| 636 | list_add(&link_state->link, &parent_link_state->children); |
| 637 | link_state->parent = parent_link_state; |
| 638 | } |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 639 | link_state->pdev = pdev; |
| 640 | link_state->has_switch = pcie_aspm_downstream_has_switch(link_state); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 641 | pdev->link_state = link_state; |
| 642 | |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 643 | if (!blacklist) { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 644 | pcie_aspm_configure_common_clock(link_state); |
| 645 | pcie_aspm_cap_init(link_state); |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 646 | } else { |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 647 | link_state->aspm_enabled = |
| 648 | (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1); |
| 649 | link_state->aspm_default = 0; |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 650 | /* Set support state to 0, so we will disable ASPM later */ |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 651 | link_state->aspm_support = 0; |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 652 | } |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 653 | |
Kenji Kaneshige | dc64cd1 | 2009-05-13 12:11:33 +0900 | [diff] [blame] | 654 | list_add(&link_state->sibling, &link_list); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 655 | |
Kenji Kaneshige | 5cde89d | 2009-05-13 12:17:04 +0900 | [diff] [blame] | 656 | if (link_state->has_switch) { |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 657 | /* |
| 658 | * If link has switch, delay the link config. The leaf link |
| 659 | * initialization will config the whole hierarchy. but we must |
| 660 | * make sure BIOS doesn't set unsupported link state |
| 661 | **/ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 662 | state = pcie_aspm_check_state(link_state, |
| 663 | link_state->aspm_default); |
| 664 | __pcie_aspm_config_link(link_state, state); |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 665 | } else |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 666 | __pcie_aspm_configure_link_state(link_state, |
| 667 | policy_to_aspm_state(link_state)); |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 668 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 669 | pcie_check_clock_pm(link_state, blacklist); |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 670 | |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 671 | unlock_out: |
| 672 | if (error) |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 673 | free_link_state(link_state); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 674 | mutex_unlock(&aspm_lock); |
| 675 | out: |
| 676 | up_read(&pci_bus_sem); |
| 677 | } |
| 678 | |
| 679 | /* @pdev: the endpoint device */ |
| 680 | void pcie_aspm_exit_link_state(struct pci_dev *pdev) |
| 681 | { |
| 682 | struct pci_dev *parent = pdev->bus->self; |
| 683 | struct pcie_link_state *link_state = parent->link_state; |
| 684 | |
| 685 | if (aspm_disabled || !pdev->is_pcie || !parent || !link_state) |
| 686 | return; |
| 687 | if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT && |
| 688 | parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) |
| 689 | return; |
| 690 | down_read(&pci_bus_sem); |
| 691 | mutex_lock(&aspm_lock); |
| 692 | |
| 693 | /* |
| 694 | * All PCIe functions are in one slot, remove one function will remove |
Alex Chiang | 3419c75 | 2009-01-28 14:59:18 -0700 | [diff] [blame] | 695 | * the whole slot, so just wait until we are the last function left. |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 696 | */ |
Alex Chiang | 3419c75 | 2009-01-28 14:59:18 -0700 | [diff] [blame] | 697 | if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices)) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 698 | goto out; |
| 699 | |
| 700 | /* All functions are removed, so just disable ASPM for the link */ |
| 701 | __pcie_aspm_config_one_dev(parent, 0); |
Kenji Kaneshige | dc64cd1 | 2009-05-13 12:11:33 +0900 | [diff] [blame] | 702 | list_del(&link_state->sibling); |
Shaohua Li | 46bbdfa | 2008-12-19 09:27:42 +0800 | [diff] [blame] | 703 | list_del(&link_state->link); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 704 | /* Clock PM is for endpoint device */ |
| 705 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 706 | free_link_state(link_state); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 707 | out: |
| 708 | mutex_unlock(&aspm_lock); |
| 709 | up_read(&pci_bus_sem); |
| 710 | } |
| 711 | |
| 712 | /* @pdev: the root port or switch downstream port */ |
| 713 | void pcie_aspm_pm_state_change(struct pci_dev *pdev) |
| 714 | { |
| 715 | struct pcie_link_state *link_state = pdev->link_state; |
| 716 | |
| 717 | if (aspm_disabled || !pdev->is_pcie || !pdev->link_state) |
| 718 | return; |
| 719 | if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT && |
| 720 | pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) |
| 721 | return; |
| 722 | /* |
| 723 | * devices changed PM state, we should recheck if latency meets all |
| 724 | * functions' requirement |
| 725 | */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 726 | pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /* |
| 730 | * pci_disable_link_state - disable pci device's link state, so the link will |
| 731 | * never enter specific states |
| 732 | */ |
| 733 | void pci_disable_link_state(struct pci_dev *pdev, int state) |
| 734 | { |
| 735 | struct pci_dev *parent = pdev->bus->self; |
| 736 | struct pcie_link_state *link_state; |
| 737 | |
| 738 | if (aspm_disabled || !pdev->is_pcie) |
| 739 | return; |
| 740 | if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT || |
| 741 | pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) |
| 742 | parent = pdev; |
| 743 | if (!parent || !parent->link_state) |
| 744 | return; |
| 745 | |
| 746 | down_read(&pci_bus_sem); |
| 747 | mutex_lock(&aspm_lock); |
| 748 | link_state = parent->link_state; |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 749 | link_state->aspm_support &= ~state; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 750 | if (state & PCIE_LINK_STATE_CLKPM) |
Kenji Kaneshige | 4d246e4 | 2009-05-13 12:15:38 +0900 | [diff] [blame] | 751 | link_state->clkpm_capable = 0; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 752 | |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 753 | __pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled); |
Kenji Kaneshige | 4d246e4 | 2009-05-13 12:15:38 +0900 | [diff] [blame] | 754 | if (!link_state->clkpm_capable && link_state->clkpm_enabled) |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 755 | pcie_set_clock_pm(link_state, 0); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 756 | mutex_unlock(&aspm_lock); |
| 757 | up_read(&pci_bus_sem); |
| 758 | } |
| 759 | EXPORT_SYMBOL(pci_disable_link_state); |
| 760 | |
| 761 | static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp) |
| 762 | { |
| 763 | int i; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 764 | struct pcie_link_state *link_state; |
| 765 | |
| 766 | for (i = 0; i < ARRAY_SIZE(policy_str); i++) |
| 767 | if (!strncmp(val, policy_str[i], strlen(policy_str[i]))) |
| 768 | break; |
| 769 | if (i >= ARRAY_SIZE(policy_str)) |
| 770 | return -EINVAL; |
| 771 | if (i == aspm_policy) |
| 772 | return 0; |
| 773 | |
| 774 | down_read(&pci_bus_sem); |
| 775 | mutex_lock(&aspm_lock); |
| 776 | aspm_policy = i; |
Kenji Kaneshige | dc64cd1 | 2009-05-13 12:11:33 +0900 | [diff] [blame] | 777 | list_for_each_entry(link_state, &link_list, sibling) { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 778 | __pcie_aspm_configure_link_state(link_state, |
| 779 | policy_to_aspm_state(link_state)); |
Kenji Kaneshige | 4d246e4 | 2009-05-13 12:15:38 +0900 | [diff] [blame] | 780 | if (link_state->clkpm_capable && |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 781 | link_state->clkpm_enabled != policy_to_clkpm_state(link_state)) |
| 782 | pcie_set_clock_pm(link_state, |
| 783 | policy_to_clkpm_state(link_state)); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 784 | |
| 785 | } |
| 786 | mutex_unlock(&aspm_lock); |
| 787 | up_read(&pci_bus_sem); |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp) |
| 792 | { |
| 793 | int i, cnt = 0; |
| 794 | for (i = 0; i < ARRAY_SIZE(policy_str); i++) |
| 795 | if (i == aspm_policy) |
| 796 | cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]); |
| 797 | else |
| 798 | cnt += sprintf(buffer + cnt, "%s ", policy_str[i]); |
| 799 | return cnt; |
| 800 | } |
| 801 | |
| 802 | module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy, |
| 803 | NULL, 0644); |
| 804 | |
| 805 | #ifdef CONFIG_PCIEASPM_DEBUG |
| 806 | static ssize_t link_state_show(struct device *dev, |
| 807 | struct device_attribute *attr, |
| 808 | char *buf) |
| 809 | { |
| 810 | struct pci_dev *pci_device = to_pci_dev(dev); |
| 811 | struct pcie_link_state *link_state = pci_device->link_state; |
| 812 | |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 813 | return sprintf(buf, "%d\n", link_state->aspm_enabled); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static ssize_t link_state_store(struct device *dev, |
| 817 | struct device_attribute *attr, |
| 818 | const char *buf, |
| 819 | size_t n) |
| 820 | { |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 821 | struct pci_dev *pdev = to_pci_dev(dev); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 822 | int state; |
| 823 | |
| 824 | if (n < 1) |
| 825 | return -EINVAL; |
| 826 | state = buf[0]-'0'; |
| 827 | if (state >= 0 && state <= 3) { |
| 828 | /* setup link aspm state */ |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 829 | pcie_aspm_configure_link_state(pdev->link_state, state); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 830 | return n; |
| 831 | } |
| 832 | |
| 833 | return -EINVAL; |
| 834 | } |
| 835 | |
| 836 | static ssize_t clk_ctl_show(struct device *dev, |
| 837 | struct device_attribute *attr, |
| 838 | char *buf) |
| 839 | { |
| 840 | struct pci_dev *pci_device = to_pci_dev(dev); |
| 841 | struct pcie_link_state *link_state = pci_device->link_state; |
| 842 | |
Kenji Kaneshige | 4d246e4 | 2009-05-13 12:15:38 +0900 | [diff] [blame] | 843 | return sprintf(buf, "%d\n", link_state->clkpm_enabled); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static ssize_t clk_ctl_store(struct device *dev, |
| 847 | struct device_attribute *attr, |
| 848 | const char *buf, |
| 849 | size_t n) |
| 850 | { |
| 851 | struct pci_dev *pci_device = to_pci_dev(dev); |
| 852 | int state; |
| 853 | |
| 854 | if (n < 1) |
| 855 | return -EINVAL; |
| 856 | state = buf[0]-'0'; |
| 857 | |
| 858 | down_read(&pci_bus_sem); |
| 859 | mutex_lock(&aspm_lock); |
Kenji Kaneshige | 5aa6358 | 2009-05-13 12:17:44 +0900 | [diff] [blame^] | 860 | pcie_set_clock_pm(pci_device->link_state, !!state); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 861 | mutex_unlock(&aspm_lock); |
| 862 | up_read(&pci_bus_sem); |
| 863 | |
| 864 | return n; |
| 865 | } |
| 866 | |
| 867 | static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store); |
| 868 | static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store); |
| 869 | |
| 870 | static char power_group[] = "power"; |
| 871 | void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev) |
| 872 | { |
| 873 | struct pcie_link_state *link_state = pdev->link_state; |
| 874 | |
| 875 | if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT && |
| 876 | pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state) |
| 877 | return; |
| 878 | |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 879 | if (link_state->aspm_support) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 880 | sysfs_add_file_to_group(&pdev->dev.kobj, |
| 881 | &dev_attr_link_state.attr, power_group); |
Kenji Kaneshige | 4d246e4 | 2009-05-13 12:15:38 +0900 | [diff] [blame] | 882 | if (link_state->clkpm_capable) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 883 | sysfs_add_file_to_group(&pdev->dev.kobj, |
| 884 | &dev_attr_clk_ctl.attr, power_group); |
| 885 | } |
| 886 | |
| 887 | void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev) |
| 888 | { |
| 889 | struct pcie_link_state *link_state = pdev->link_state; |
| 890 | |
| 891 | if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT && |
| 892 | pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state) |
| 893 | return; |
| 894 | |
Kenji Kaneshige | 80bfdbe | 2009-05-13 12:12:43 +0900 | [diff] [blame] | 895 | if (link_state->aspm_support) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 896 | sysfs_remove_file_from_group(&pdev->dev.kobj, |
| 897 | &dev_attr_link_state.attr, power_group); |
Kenji Kaneshige | 4d246e4 | 2009-05-13 12:15:38 +0900 | [diff] [blame] | 898 | if (link_state->clkpm_capable) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 899 | sysfs_remove_file_from_group(&pdev->dev.kobj, |
| 900 | &dev_attr_clk_ctl.attr, power_group); |
| 901 | } |
| 902 | #endif |
| 903 | |
| 904 | static int __init pcie_aspm_disable(char *str) |
| 905 | { |
Shaohua Li | d6d3857 | 2008-07-23 10:32:42 +0800 | [diff] [blame] | 906 | if (!strcmp(str, "off")) { |
| 907 | aspm_disabled = 1; |
| 908 | printk(KERN_INFO "PCIe ASPM is disabled\n"); |
| 909 | } else if (!strcmp(str, "force")) { |
| 910 | aspm_force = 1; |
| 911 | printk(KERN_INFO "PCIe ASPM is forcedly enabled\n"); |
| 912 | } |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 913 | return 1; |
| 914 | } |
| 915 | |
Shaohua Li | d6d3857 | 2008-07-23 10:32:42 +0800 | [diff] [blame] | 916 | __setup("pcie_aspm=", pcie_aspm_disable); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 917 | |
Shaohua Li | 5fde244 | 2008-07-23 10:32:24 +0800 | [diff] [blame] | 918 | void pcie_no_aspm(void) |
| 919 | { |
Shaohua Li | d6d3857 | 2008-07-23 10:32:42 +0800 | [diff] [blame] | 920 | if (!aspm_force) |
| 921 | aspm_disabled = 1; |
Shaohua Li | 5fde244 | 2008-07-23 10:32:24 +0800 | [diff] [blame] | 922 | } |
| 923 | |
Andrew Patterson | 3e1b160 | 2008-11-10 15:30:55 -0700 | [diff] [blame] | 924 | /** |
| 925 | * pcie_aspm_enabled - is PCIe ASPM enabled? |
| 926 | * |
| 927 | * Returns true if ASPM has not been disabled by the command-line option |
| 928 | * pcie_aspm=off. |
| 929 | **/ |
| 930 | int pcie_aspm_enabled(void) |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 931 | { |
Andrew Patterson | 3e1b160 | 2008-11-10 15:30:55 -0700 | [diff] [blame] | 932 | return !aspm_disabled; |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 933 | } |
Andrew Patterson | 3e1b160 | 2008-11-10 15:30:55 -0700 | [diff] [blame] | 934 | EXPORT_SYMBOL(pcie_aspm_enabled); |
Shaohua Li | 7d715a6 | 2008-02-25 09:46:41 +0800 | [diff] [blame] | 935 | |