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