1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Enable 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> 19 #include <linux/jiffies.h> 20 #include <linux/delay.h> 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 29 /* Note: those are not register definitions */ 30 #define ASPM_STATE_L0S_UP (1) /* Upstream direction L0s state */ 31 #define ASPM_STATE_L0S_DW (2) /* Downstream direction L0s state */ 32 #define ASPM_STATE_L1 (4) /* L1 state */ 33 #define ASPM_STATE_L1_1 (8) /* ASPM L1.1 state */ 34 #define ASPM_STATE_L1_2 (0x10) /* ASPM L1.2 state */ 35 #define ASPM_STATE_L1_1_PCIPM (0x20) /* PCI PM L1.1 state */ 36 #define ASPM_STATE_L1_2_PCIPM (0x40) /* PCI PM L1.2 state */ 37 #define ASPM_STATE_L1_SS_PCIPM (ASPM_STATE_L1_1_PCIPM | ASPM_STATE_L1_2_PCIPM) 38 #define ASPM_STATE_L1_2_MASK (ASPM_STATE_L1_2 | ASPM_STATE_L1_2_PCIPM) 39 #define ASPM_STATE_L1SS (ASPM_STATE_L1_1 | ASPM_STATE_L1_1_PCIPM |\ 40 ASPM_STATE_L1_2_MASK) 41 #define ASPM_STATE_L0S (ASPM_STATE_L0S_UP | ASPM_STATE_L0S_DW) 42 #define ASPM_STATE_ALL (ASPM_STATE_L0S | ASPM_STATE_L1 | \ 43 ASPM_STATE_L1SS) 44 45 struct aspm_latency { 46 u32 l0s; /* L0s latency (nsec) */ 47 u32 l1; /* L1 latency (nsec) */ 48 }; 49 50 struct pcie_link_state { 51 struct pci_dev *pdev; /* Upstream component of the Link */ 52 struct pci_dev *downstream; /* Downstream component, function 0 */ 53 struct pcie_link_state *root; /* pointer to the root port link */ 54 struct pcie_link_state *parent; /* pointer to the parent Link state */ 55 struct list_head sibling; /* node in link_list */ 56 57 /* ASPM state */ 58 u32 aspm_support:7; /* Supported ASPM state */ 59 u32 aspm_enabled:7; /* Enabled ASPM state */ 60 u32 aspm_capable:7; /* Capable ASPM state with latency */ 61 u32 aspm_default:7; /* Default ASPM state by BIOS */ 62 u32 aspm_disable:7; /* Disabled ASPM state */ 63 64 /* Clock PM state */ 65 u32 clkpm_capable:1; /* Clock PM capable? */ 66 u32 clkpm_enabled:1; /* Current Clock PM state */ 67 u32 clkpm_default:1; /* Default Clock PM state by BIOS */ 68 69 /* Exit latencies */ 70 struct aspm_latency latency_up; /* Upstream direction exit latency */ 71 struct aspm_latency latency_dw; /* Downstream direction exit latency */ 72 /* 73 * Endpoint acceptable latencies. A pcie downstream port only 74 * has one slot under it, so at most there are 8 functions. 75 */ 76 struct aspm_latency acceptable[8]; 77 78 /* L1 PM Substate info */ 79 struct { 80 u32 up_cap_ptr; /* L1SS cap ptr in upstream dev */ 81 u32 dw_cap_ptr; /* L1SS cap ptr in downstream dev */ 82 u32 ctl1; /* value to be programmed in ctl1 */ 83 u32 ctl2; /* value to be programmed in ctl2 */ 84 } l1ss; 85 }; 86 87 static int aspm_disabled, aspm_force; 88 static bool aspm_support_enabled = true; 89 static DEFINE_MUTEX(aspm_lock); 90 static LIST_HEAD(link_list); 91 92 #define POLICY_DEFAULT 0 /* BIOS default setting */ 93 #define POLICY_PERFORMANCE 1 /* high performance */ 94 #define POLICY_POWERSAVE 2 /* high power saving */ 95 #define POLICY_POWER_SUPERSAVE 3 /* possibly even more power saving */ 96 97 #ifdef CONFIG_PCIEASPM_PERFORMANCE 98 static int aspm_policy = POLICY_PERFORMANCE; 99 #elif defined CONFIG_PCIEASPM_POWERSAVE 100 static int aspm_policy = POLICY_POWERSAVE; 101 #elif defined CONFIG_PCIEASPM_POWER_SUPERSAVE 102 static int aspm_policy = POLICY_POWER_SUPERSAVE; 103 #else 104 static int aspm_policy; 105 #endif 106 107 static const char *policy_str[] = { 108 [POLICY_DEFAULT] = "default", 109 [POLICY_PERFORMANCE] = "performance", 110 [POLICY_POWERSAVE] = "powersave", 111 [POLICY_POWER_SUPERSAVE] = "powersupersave" 112 }; 113 114 #define LINK_RETRAIN_TIMEOUT HZ 115 116 static int policy_to_aspm_state(struct pcie_link_state *link) 117 { 118 switch (aspm_policy) { 119 case POLICY_PERFORMANCE: 120 /* Disable ASPM and Clock PM */ 121 return 0; 122 case POLICY_POWERSAVE: 123 /* Enable ASPM L0s/L1 */ 124 return (ASPM_STATE_L0S | ASPM_STATE_L1); 125 case POLICY_POWER_SUPERSAVE: 126 /* Enable Everything */ 127 return ASPM_STATE_ALL; 128 case POLICY_DEFAULT: 129 return link->aspm_default; 130 } 131 return 0; 132 } 133 134 static int policy_to_clkpm_state(struct pcie_link_state *link) 135 { 136 switch (aspm_policy) { 137 case POLICY_PERFORMANCE: 138 /* Disable ASPM and Clock PM */ 139 return 0; 140 case POLICY_POWERSAVE: 141 case POLICY_POWER_SUPERSAVE: 142 /* Enable Clock PM */ 143 return 1; 144 case POLICY_DEFAULT: 145 return link->clkpm_default; 146 } 147 return 0; 148 } 149 150 static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable) 151 { 152 struct pci_dev *child; 153 struct pci_bus *linkbus = link->pdev->subordinate; 154 u32 val = enable ? PCI_EXP_LNKCTL_CLKREQ_EN : 0; 155 156 list_for_each_entry(child, &linkbus->devices, bus_list) 157 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL, 158 PCI_EXP_LNKCTL_CLKREQ_EN, 159 val); 160 link->clkpm_enabled = !!enable; 161 } 162 163 static void pcie_set_clkpm(struct pcie_link_state *link, int enable) 164 { 165 /* Don't enable Clock PM if the link is not Clock PM capable */ 166 if (!link->clkpm_capable) 167 enable = 0; 168 /* Need nothing if the specified equals to current state */ 169 if (link->clkpm_enabled == enable) 170 return; 171 pcie_set_clkpm_nocheck(link, enable); 172 } 173 174 static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist) 175 { 176 int capable = 1, enabled = 1; 177 u32 reg32; 178 u16 reg16; 179 struct pci_dev *child; 180 struct pci_bus *linkbus = link->pdev->subordinate; 181 182 /* All functions should have the same cap and state, take the worst */ 183 list_for_each_entry(child, &linkbus->devices, bus_list) { 184 pcie_capability_read_dword(child, PCI_EXP_LNKCAP, ®32); 185 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) { 186 capable = 0; 187 enabled = 0; 188 break; 189 } 190 pcie_capability_read_word(child, PCI_EXP_LNKCTL, ®16); 191 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN)) 192 enabled = 0; 193 } 194 link->clkpm_enabled = enabled; 195 link->clkpm_default = enabled; 196 link->clkpm_capable = (blacklist) ? 0 : capable; 197 } 198 199 static bool pcie_retrain_link(struct pcie_link_state *link) 200 { 201 struct pci_dev *parent = link->pdev; 202 unsigned long start_jiffies; 203 u16 reg16; 204 205 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16); 206 reg16 |= PCI_EXP_LNKCTL_RL; 207 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16); 208 209 /* Wait for link training end. Break out after waiting for timeout */ 210 start_jiffies = jiffies; 211 for (;;) { 212 pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16); 213 if (!(reg16 & PCI_EXP_LNKSTA_LT)) 214 break; 215 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) 216 break; 217 msleep(1); 218 } 219 return !(reg16 & PCI_EXP_LNKSTA_LT); 220 } 221 222 /* 223 * pcie_aspm_configure_common_clock: check if the 2 ends of a link 224 * could use common clock. If they are, configure them to use the 225 * common clock. That will reduce the ASPM state exit latency. 226 */ 227 static void pcie_aspm_configure_common_clock(struct pcie_link_state *link) 228 { 229 int same_clock = 1; 230 u16 reg16, parent_reg, child_reg[8]; 231 struct pci_dev *child, *parent = link->pdev; 232 struct pci_bus *linkbus = parent->subordinate; 233 /* 234 * All functions of a slot should have the same Slot Clock 235 * Configuration, so just check one function 236 */ 237 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list); 238 BUG_ON(!pci_is_pcie(child)); 239 240 /* Check downstream component if bit Slot Clock Configuration is 1 */ 241 pcie_capability_read_word(child, PCI_EXP_LNKSTA, ®16); 242 if (!(reg16 & PCI_EXP_LNKSTA_SLC)) 243 same_clock = 0; 244 245 /* Check upstream component if bit Slot Clock Configuration is 1 */ 246 pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16); 247 if (!(reg16 & PCI_EXP_LNKSTA_SLC)) 248 same_clock = 0; 249 250 /* Port might be already in common clock mode */ 251 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16); 252 if (same_clock && (reg16 & PCI_EXP_LNKCTL_CCC)) { 253 bool consistent = true; 254 255 list_for_each_entry(child, &linkbus->devices, bus_list) { 256 pcie_capability_read_word(child, PCI_EXP_LNKCTL, 257 ®16); 258 if (!(reg16 & PCI_EXP_LNKCTL_CCC)) { 259 consistent = false; 260 break; 261 } 262 } 263 if (consistent) 264 return; 265 pci_warn(parent, "ASPM: current common clock configuration is broken, reconfiguring\n"); 266 } 267 268 /* Configure downstream component, all functions */ 269 list_for_each_entry(child, &linkbus->devices, bus_list) { 270 pcie_capability_read_word(child, PCI_EXP_LNKCTL, ®16); 271 child_reg[PCI_FUNC(child->devfn)] = reg16; 272 if (same_clock) 273 reg16 |= PCI_EXP_LNKCTL_CCC; 274 else 275 reg16 &= ~PCI_EXP_LNKCTL_CCC; 276 pcie_capability_write_word(child, PCI_EXP_LNKCTL, reg16); 277 } 278 279 /* Configure upstream component */ 280 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16); 281 parent_reg = reg16; 282 if (same_clock) 283 reg16 |= PCI_EXP_LNKCTL_CCC; 284 else 285 reg16 &= ~PCI_EXP_LNKCTL_CCC; 286 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16); 287 288 if (pcie_retrain_link(link)) 289 return; 290 291 /* Training failed. Restore common clock configurations */ 292 pci_err(parent, "ASPM: Could not configure common clock\n"); 293 list_for_each_entry(child, &linkbus->devices, bus_list) 294 pcie_capability_write_word(child, PCI_EXP_LNKCTL, 295 child_reg[PCI_FUNC(child->devfn)]); 296 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, parent_reg); 297 } 298 299 /* Convert L0s latency encoding to ns */ 300 static u32 calc_l0s_latency(u32 encoding) 301 { 302 if (encoding == 0x7) 303 return (5 * 1000); /* > 4us */ 304 return (64 << encoding); 305 } 306 307 /* Convert L0s acceptable latency encoding to ns */ 308 static u32 calc_l0s_acceptable(u32 encoding) 309 { 310 if (encoding == 0x7) 311 return -1U; 312 return (64 << encoding); 313 } 314 315 /* Convert L1 latency encoding to ns */ 316 static u32 calc_l1_latency(u32 encoding) 317 { 318 if (encoding == 0x7) 319 return (65 * 1000); /* > 64us */ 320 return (1000 << encoding); 321 } 322 323 /* Convert L1 acceptable latency encoding to ns */ 324 static u32 calc_l1_acceptable(u32 encoding) 325 { 326 if (encoding == 0x7) 327 return -1U; 328 return (1000 << encoding); 329 } 330 331 /* Convert L1SS T_pwr encoding to usec */ 332 static u32 calc_l1ss_pwron(struct pci_dev *pdev, u32 scale, u32 val) 333 { 334 switch (scale) { 335 case 0: 336 return val * 2; 337 case 1: 338 return val * 10; 339 case 2: 340 return val * 100; 341 } 342 pci_err(pdev, "%s: Invalid T_PwrOn scale: %u\n", __func__, scale); 343 return 0; 344 } 345 346 static void encode_l12_threshold(u32 threshold_us, u32 *scale, u32 *value) 347 { 348 u32 threshold_ns = threshold_us * 1000; 349 350 /* See PCIe r3.1, sec 7.33.3 and sec 6.18 */ 351 if (threshold_ns < 32) { 352 *scale = 0; 353 *value = threshold_ns; 354 } else if (threshold_ns < 1024) { 355 *scale = 1; 356 *value = threshold_ns >> 5; 357 } else if (threshold_ns < 32768) { 358 *scale = 2; 359 *value = threshold_ns >> 10; 360 } else if (threshold_ns < 1048576) { 361 *scale = 3; 362 *value = threshold_ns >> 15; 363 } else if (threshold_ns < 33554432) { 364 *scale = 4; 365 *value = threshold_ns >> 20; 366 } else { 367 *scale = 5; 368 *value = threshold_ns >> 25; 369 } 370 } 371 372 struct aspm_register_info { 373 u32 support:2; 374 u32 enabled:2; 375 u32 latency_encoding_l0s; 376 u32 latency_encoding_l1; 377 378 /* L1 substates */ 379 u32 l1ss_cap_ptr; 380 u32 l1ss_cap; 381 u32 l1ss_ctl1; 382 u32 l1ss_ctl2; 383 }; 384 385 static void pcie_get_aspm_reg(struct pci_dev *pdev, 386 struct aspm_register_info *info) 387 { 388 u16 reg16; 389 u32 reg32; 390 391 pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, ®32); 392 info->support = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10; 393 info->latency_encoding_l0s = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12; 394 info->latency_encoding_l1 = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15; 395 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, ®16); 396 info->enabled = reg16 & PCI_EXP_LNKCTL_ASPMC; 397 398 /* Read L1 PM substate capabilities */ 399 info->l1ss_cap = info->l1ss_ctl1 = info->l1ss_ctl2 = 0; 400 info->l1ss_cap_ptr = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS); 401 if (!info->l1ss_cap_ptr) 402 return; 403 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CAP, 404 &info->l1ss_cap); 405 if (!(info->l1ss_cap & PCI_L1SS_CAP_L1_PM_SS)) { 406 info->l1ss_cap = 0; 407 return; 408 } 409 410 /* 411 * If we don't have LTR for the entire path from the Root Complex 412 * to this device, we can't use ASPM L1.2 because it relies on the 413 * LTR_L1.2_THRESHOLD. See PCIe r4.0, secs 5.5.4, 6.18. 414 */ 415 if (!pdev->ltr_path) 416 info->l1ss_cap &= ~PCI_L1SS_CAP_ASPM_L1_2; 417 418 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL1, 419 &info->l1ss_ctl1); 420 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL2, 421 &info->l1ss_ctl2); 422 } 423 424 static void pcie_aspm_check_latency(struct pci_dev *endpoint) 425 { 426 u32 latency, l1_switch_latency = 0; 427 struct aspm_latency *acceptable; 428 struct pcie_link_state *link; 429 430 /* Device not in D0 doesn't need latency check */ 431 if ((endpoint->current_state != PCI_D0) && 432 (endpoint->current_state != PCI_UNKNOWN)) 433 return; 434 435 link = endpoint->bus->self->link_state; 436 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)]; 437 438 while (link) { 439 /* Check upstream direction L0s latency */ 440 if ((link->aspm_capable & ASPM_STATE_L0S_UP) && 441 (link->latency_up.l0s > acceptable->l0s)) 442 link->aspm_capable &= ~ASPM_STATE_L0S_UP; 443 444 /* Check downstream direction L0s latency */ 445 if ((link->aspm_capable & ASPM_STATE_L0S_DW) && 446 (link->latency_dw.l0s > acceptable->l0s)) 447 link->aspm_capable &= ~ASPM_STATE_L0S_DW; 448 /* 449 * Check L1 latency. 450 * Every switch on the path to root complex need 1 451 * more microsecond for L1. Spec doesn't mention L0s. 452 * 453 * The exit latencies for L1 substates are not advertised 454 * by a device. Since the spec also doesn't mention a way 455 * to determine max latencies introduced by enabling L1 456 * substates on the components, it is not clear how to do 457 * a L1 substate exit latency check. We assume that the 458 * L1 exit latencies advertised by a device include L1 459 * substate latencies (and hence do not do any check). 460 */ 461 latency = max_t(u32, link->latency_up.l1, link->latency_dw.l1); 462 if ((link->aspm_capable & ASPM_STATE_L1) && 463 (latency + l1_switch_latency > acceptable->l1)) 464 link->aspm_capable &= ~ASPM_STATE_L1; 465 l1_switch_latency += 1000; 466 467 link = link->parent; 468 } 469 } 470 471 /* 472 * The L1 PM substate capability is only implemented in function 0 in a 473 * multi function device. 474 */ 475 static struct pci_dev *pci_function_0(struct pci_bus *linkbus) 476 { 477 struct pci_dev *child; 478 479 list_for_each_entry(child, &linkbus->devices, bus_list) 480 if (PCI_FUNC(child->devfn) == 0) 481 return child; 482 return NULL; 483 } 484 485 /* Calculate L1.2 PM substate timing parameters */ 486 static void aspm_calc_l1ss_info(struct pcie_link_state *link, 487 struct aspm_register_info *upreg, 488 struct aspm_register_info *dwreg) 489 { 490 u32 val1, val2, scale1, scale2; 491 u32 t_common_mode, t_power_on, l1_2_threshold, scale, value; 492 493 link->l1ss.up_cap_ptr = upreg->l1ss_cap_ptr; 494 link->l1ss.dw_cap_ptr = dwreg->l1ss_cap_ptr; 495 link->l1ss.ctl1 = link->l1ss.ctl2 = 0; 496 497 if (!(link->aspm_support & ASPM_STATE_L1_2_MASK)) 498 return; 499 500 /* Choose the greater of the two Port Common_Mode_Restore_Times */ 501 val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8; 502 val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8; 503 t_common_mode = max(val1, val2); 504 505 /* Choose the greater of the two Port T_POWER_ON times */ 506 val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19; 507 scale1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16; 508 val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19; 509 scale2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16; 510 511 if (calc_l1ss_pwron(link->pdev, scale1, val1) > 512 calc_l1ss_pwron(link->downstream, scale2, val2)) { 513 link->l1ss.ctl2 |= scale1 | (val1 << 3); 514 t_power_on = calc_l1ss_pwron(link->pdev, scale1, val1); 515 } else { 516 link->l1ss.ctl2 |= scale2 | (val2 << 3); 517 t_power_on = calc_l1ss_pwron(link->downstream, scale2, val2); 518 } 519 520 /* 521 * Set LTR_L1.2_THRESHOLD to the time required to transition the 522 * Link from L0 to L1.2 and back to L0 so we enter L1.2 only if 523 * downstream devices report (via LTR) that they can tolerate at 524 * least that much latency. 525 * 526 * Based on PCIe r3.1, sec 5.5.3.3.1, Figures 5-16 and 5-17, and 527 * Table 5-11. T(POWER_OFF) is at most 2us and T(L1.2) is at 528 * least 4us. 529 */ 530 l1_2_threshold = 2 + 4 + t_common_mode + t_power_on; 531 encode_l12_threshold(l1_2_threshold, &scale, &value); 532 link->l1ss.ctl1 |= t_common_mode << 8 | scale << 29 | value << 16; 533 } 534 535 static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist) 536 { 537 struct pci_dev *child = link->downstream, *parent = link->pdev; 538 struct pci_bus *linkbus = parent->subordinate; 539 struct aspm_register_info upreg, dwreg; 540 541 if (blacklist) { 542 /* Set enabled/disable so that we will disable ASPM later */ 543 link->aspm_enabled = ASPM_STATE_ALL; 544 link->aspm_disable = ASPM_STATE_ALL; 545 return; 546 } 547 548 /* Get upstream/downstream components' register state */ 549 pcie_get_aspm_reg(parent, &upreg); 550 pcie_get_aspm_reg(child, &dwreg); 551 552 /* 553 * If ASPM not supported, don't mess with the clocks and link, 554 * bail out now. 555 */ 556 if (!(upreg.support & dwreg.support)) 557 return; 558 559 /* Configure common clock before checking latencies */ 560 pcie_aspm_configure_common_clock(link); 561 562 /* 563 * Re-read upstream/downstream components' register state 564 * after clock configuration 565 */ 566 pcie_get_aspm_reg(parent, &upreg); 567 pcie_get_aspm_reg(child, &dwreg); 568 569 /* 570 * Setup L0s state 571 * 572 * Note that we must not enable L0s in either direction on a 573 * given link unless components on both sides of the link each 574 * support L0s. 575 */ 576 if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S) 577 link->aspm_support |= ASPM_STATE_L0S; 578 if (dwreg.enabled & PCIE_LINK_STATE_L0S) 579 link->aspm_enabled |= ASPM_STATE_L0S_UP; 580 if (upreg.enabled & PCIE_LINK_STATE_L0S) 581 link->aspm_enabled |= ASPM_STATE_L0S_DW; 582 link->latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s); 583 link->latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s); 584 585 /* Setup L1 state */ 586 if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1) 587 link->aspm_support |= ASPM_STATE_L1; 588 if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1) 589 link->aspm_enabled |= ASPM_STATE_L1; 590 link->latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1); 591 link->latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1); 592 593 /* Setup L1 substate */ 594 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1) 595 link->aspm_support |= ASPM_STATE_L1_1; 596 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2) 597 link->aspm_support |= ASPM_STATE_L1_2; 598 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1) 599 link->aspm_support |= ASPM_STATE_L1_1_PCIPM; 600 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2) 601 link->aspm_support |= ASPM_STATE_L1_2_PCIPM; 602 603 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1) 604 link->aspm_enabled |= ASPM_STATE_L1_1; 605 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2) 606 link->aspm_enabled |= ASPM_STATE_L1_2; 607 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1) 608 link->aspm_enabled |= ASPM_STATE_L1_1_PCIPM; 609 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2) 610 link->aspm_enabled |= ASPM_STATE_L1_2_PCIPM; 611 612 if (link->aspm_support & ASPM_STATE_L1SS) 613 aspm_calc_l1ss_info(link, &upreg, &dwreg); 614 615 /* Save default state */ 616 link->aspm_default = link->aspm_enabled; 617 618 /* Setup initial capable state. Will be updated later */ 619 link->aspm_capable = link->aspm_support; 620 /* 621 * If the downstream component has pci bridge function, don't 622 * do ASPM for now. 623 */ 624 list_for_each_entry(child, &linkbus->devices, bus_list) { 625 if (pci_pcie_type(child) == PCI_EXP_TYPE_PCI_BRIDGE) { 626 link->aspm_disable = ASPM_STATE_ALL; 627 break; 628 } 629 } 630 631 /* Get and check endpoint acceptable latencies */ 632 list_for_each_entry(child, &linkbus->devices, bus_list) { 633 u32 reg32, encoding; 634 struct aspm_latency *acceptable = 635 &link->acceptable[PCI_FUNC(child->devfn)]; 636 637 if (pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT && 638 pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END) 639 continue; 640 641 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, ®32); 642 /* Calculate endpoint L0s acceptable latency */ 643 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6; 644 acceptable->l0s = calc_l0s_acceptable(encoding); 645 /* Calculate endpoint L1 acceptable latency */ 646 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9; 647 acceptable->l1 = calc_l1_acceptable(encoding); 648 649 pcie_aspm_check_latency(child); 650 } 651 } 652 653 static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos, 654 u32 clear, u32 set) 655 { 656 u32 val; 657 658 pci_read_config_dword(pdev, pos, &val); 659 val &= ~clear; 660 val |= set; 661 pci_write_config_dword(pdev, pos, val); 662 } 663 664 /* Configure the ASPM L1 substates */ 665 static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state) 666 { 667 u32 val, enable_req; 668 struct pci_dev *child = link->downstream, *parent = link->pdev; 669 u32 up_cap_ptr = link->l1ss.up_cap_ptr; 670 u32 dw_cap_ptr = link->l1ss.dw_cap_ptr; 671 672 enable_req = (link->aspm_enabled ^ state) & state; 673 674 /* 675 * Here are the rules specified in the PCIe spec for enabling L1SS: 676 * - When enabling L1.x, enable bit at parent first, then at child 677 * - When disabling L1.x, disable bit at child first, then at parent 678 * - When enabling ASPM L1.x, need to disable L1 679 * (at child followed by parent). 680 * - The ASPM/PCIPM L1.2 must be disabled while programming timing 681 * parameters 682 * 683 * To keep it simple, disable all L1SS bits first, and later enable 684 * what is needed. 685 */ 686 687 /* Disable all L1 substates */ 688 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1, 689 PCI_L1SS_CTL1_L1SS_MASK, 0); 690 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1, 691 PCI_L1SS_CTL1_L1SS_MASK, 0); 692 /* 693 * If needed, disable L1, and it gets enabled later 694 * in pcie_config_aspm_link(). 695 */ 696 if (enable_req & (ASPM_STATE_L1_1 | ASPM_STATE_L1_2)) { 697 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL, 698 PCI_EXP_LNKCTL_ASPM_L1, 0); 699 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL, 700 PCI_EXP_LNKCTL_ASPM_L1, 0); 701 } 702 703 if (enable_req & ASPM_STATE_L1_2_MASK) { 704 705 /* Program T_POWER_ON times in both ports */ 706 pci_write_config_dword(parent, up_cap_ptr + PCI_L1SS_CTL2, 707 link->l1ss.ctl2); 708 pci_write_config_dword(child, dw_cap_ptr + PCI_L1SS_CTL2, 709 link->l1ss.ctl2); 710 711 /* Program Common_Mode_Restore_Time in upstream device */ 712 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1, 713 PCI_L1SS_CTL1_CM_RESTORE_TIME, 714 link->l1ss.ctl1); 715 716 /* Program LTR_L1.2_THRESHOLD time in both ports */ 717 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1, 718 PCI_L1SS_CTL1_LTR_L12_TH_VALUE | 719 PCI_L1SS_CTL1_LTR_L12_TH_SCALE, 720 link->l1ss.ctl1); 721 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1, 722 PCI_L1SS_CTL1_LTR_L12_TH_VALUE | 723 PCI_L1SS_CTL1_LTR_L12_TH_SCALE, 724 link->l1ss.ctl1); 725 } 726 727 val = 0; 728 if (state & ASPM_STATE_L1_1) 729 val |= PCI_L1SS_CTL1_ASPM_L1_1; 730 if (state & ASPM_STATE_L1_2) 731 val |= PCI_L1SS_CTL1_ASPM_L1_2; 732 if (state & ASPM_STATE_L1_1_PCIPM) 733 val |= PCI_L1SS_CTL1_PCIPM_L1_1; 734 if (state & ASPM_STATE_L1_2_PCIPM) 735 val |= PCI_L1SS_CTL1_PCIPM_L1_2; 736 737 /* Enable what we need to enable */ 738 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1, 739 PCI_L1SS_CAP_L1_PM_SS, val); 740 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1, 741 PCI_L1SS_CAP_L1_PM_SS, val); 742 } 743 744 static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val) 745 { 746 pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL, 747 PCI_EXP_LNKCTL_ASPMC, val); 748 } 749 750 static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state) 751 { 752 u32 upstream = 0, dwstream = 0; 753 struct pci_dev *child = link->downstream, *parent = link->pdev; 754 struct pci_bus *linkbus = parent->subordinate; 755 756 /* Enable only the states that were not explicitly disabled */ 757 state &= (link->aspm_capable & ~link->aspm_disable); 758 759 /* Can't enable any substates if L1 is not enabled */ 760 if (!(state & ASPM_STATE_L1)) 761 state &= ~ASPM_STATE_L1SS; 762 763 /* Spec says both ports must be in D0 before enabling PCI PM substates*/ 764 if (parent->current_state != PCI_D0 || child->current_state != PCI_D0) { 765 state &= ~ASPM_STATE_L1_SS_PCIPM; 766 state |= (link->aspm_enabled & ASPM_STATE_L1_SS_PCIPM); 767 } 768 769 /* Nothing to do if the link is already in the requested state */ 770 if (link->aspm_enabled == state) 771 return; 772 /* Convert ASPM state to upstream/downstream ASPM register state */ 773 if (state & ASPM_STATE_L0S_UP) 774 dwstream |= PCI_EXP_LNKCTL_ASPM_L0S; 775 if (state & ASPM_STATE_L0S_DW) 776 upstream |= PCI_EXP_LNKCTL_ASPM_L0S; 777 if (state & ASPM_STATE_L1) { 778 upstream |= PCI_EXP_LNKCTL_ASPM_L1; 779 dwstream |= PCI_EXP_LNKCTL_ASPM_L1; 780 } 781 782 if (link->aspm_capable & ASPM_STATE_L1SS) 783 pcie_config_aspm_l1ss(link, state); 784 785 /* 786 * Spec 2.0 suggests all functions should be configured the 787 * same setting for ASPM. Enabling ASPM L1 should be done in 788 * upstream component first and then downstream, and vice 789 * versa for disabling ASPM L1. Spec doesn't mention L0S. 790 */ 791 if (state & ASPM_STATE_L1) 792 pcie_config_aspm_dev(parent, upstream); 793 list_for_each_entry(child, &linkbus->devices, bus_list) 794 pcie_config_aspm_dev(child, dwstream); 795 if (!(state & ASPM_STATE_L1)) 796 pcie_config_aspm_dev(parent, upstream); 797 798 link->aspm_enabled = state; 799 } 800 801 static void pcie_config_aspm_path(struct pcie_link_state *link) 802 { 803 while (link) { 804 pcie_config_aspm_link(link, policy_to_aspm_state(link)); 805 link = link->parent; 806 } 807 } 808 809 static void free_link_state(struct pcie_link_state *link) 810 { 811 link->pdev->link_state = NULL; 812 kfree(link); 813 } 814 815 static int pcie_aspm_sanity_check(struct pci_dev *pdev) 816 { 817 struct pci_dev *child; 818 u32 reg32; 819 820 /* 821 * Some functions in a slot might not all be PCIe functions, 822 * very strange. Disable ASPM for the whole slot 823 */ 824 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) { 825 if (!pci_is_pcie(child)) 826 return -EINVAL; 827 828 /* 829 * If ASPM is disabled then we're not going to change 830 * the BIOS state. It's safe to continue even if it's a 831 * pre-1.1 device 832 */ 833 834 if (aspm_disabled) 835 continue; 836 837 /* 838 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use 839 * RBER bit to determine if a function is 1.1 version device 840 */ 841 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, ®32); 842 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) { 843 pci_info(child, "disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force'\n"); 844 return -EINVAL; 845 } 846 } 847 return 0; 848 } 849 850 static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev) 851 { 852 struct pcie_link_state *link; 853 854 link = kzalloc(sizeof(*link), GFP_KERNEL); 855 if (!link) 856 return NULL; 857 858 INIT_LIST_HEAD(&link->sibling); 859 link->pdev = pdev; 860 link->downstream = pci_function_0(pdev->subordinate); 861 862 /* 863 * Root Ports and PCI/PCI-X to PCIe Bridges are roots of PCIe 864 * hierarchies. Note that some PCIe host implementations omit 865 * the root ports entirely, in which case a downstream port on 866 * a switch may become the root of the link state chain for all 867 * its subordinate endpoints. 868 */ 869 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT || 870 pci_pcie_type(pdev) == PCI_EXP_TYPE_PCIE_BRIDGE || 871 !pdev->bus->parent->self) { 872 link->root = link; 873 } else { 874 struct pcie_link_state *parent; 875 876 parent = pdev->bus->parent->self->link_state; 877 if (!parent) { 878 kfree(link); 879 return NULL; 880 } 881 882 link->parent = parent; 883 link->root = link->parent->root; 884 } 885 886 list_add(&link->sibling, &link_list); 887 pdev->link_state = link; 888 return link; 889 } 890 891 /* 892 * pcie_aspm_init_link_state: Initiate PCI express link state. 893 * It is called after the pcie and its children devices are scanned. 894 * @pdev: the root port or switch downstream port 895 */ 896 void pcie_aspm_init_link_state(struct pci_dev *pdev) 897 { 898 struct pcie_link_state *link; 899 int blacklist = !!pcie_aspm_sanity_check(pdev); 900 901 if (!aspm_support_enabled) 902 return; 903 904 if (pdev->link_state) 905 return; 906 907 /* 908 * We allocate pcie_link_state for the component on the upstream 909 * end of a Link, so there's nothing to do unless this device has a 910 * Link on its secondary side. 911 */ 912 if (!pdev->has_secondary_link) 913 return; 914 915 /* VIA has a strange chipset, root port is under a bridge */ 916 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT && 917 pdev->bus->self) 918 return; 919 920 down_read(&pci_bus_sem); 921 if (list_empty(&pdev->subordinate->devices)) 922 goto out; 923 924 mutex_lock(&aspm_lock); 925 link = alloc_pcie_link_state(pdev); 926 if (!link) 927 goto unlock; 928 /* 929 * Setup initial ASPM state. Note that we need to configure 930 * upstream links also because capable state of them can be 931 * update through pcie_aspm_cap_init(). 932 */ 933 pcie_aspm_cap_init(link, blacklist); 934 935 /* Setup initial Clock PM state */ 936 pcie_clkpm_cap_init(link, blacklist); 937 938 /* 939 * At this stage drivers haven't had an opportunity to change the 940 * link policy setting. Enabling ASPM on broken hardware can cripple 941 * it even before the driver has had a chance to disable ASPM, so 942 * default to a safe level right now. If we're enabling ASPM beyond 943 * the BIOS's expectation, we'll do so once pci_enable_device() is 944 * called. 945 */ 946 if (aspm_policy != POLICY_POWERSAVE && 947 aspm_policy != POLICY_POWER_SUPERSAVE) { 948 pcie_config_aspm_path(link); 949 pcie_set_clkpm(link, policy_to_clkpm_state(link)); 950 } 951 952 unlock: 953 mutex_unlock(&aspm_lock); 954 out: 955 up_read(&pci_bus_sem); 956 } 957 958 /* Recheck latencies and update aspm_capable for links under the root */ 959 static void pcie_update_aspm_capable(struct pcie_link_state *root) 960 { 961 struct pcie_link_state *link; 962 BUG_ON(root->parent); 963 list_for_each_entry(link, &link_list, sibling) { 964 if (link->root != root) 965 continue; 966 link->aspm_capable = link->aspm_support; 967 } 968 list_for_each_entry(link, &link_list, sibling) { 969 struct pci_dev *child; 970 struct pci_bus *linkbus = link->pdev->subordinate; 971 if (link->root != root) 972 continue; 973 list_for_each_entry(child, &linkbus->devices, bus_list) { 974 if ((pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT) && 975 (pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END)) 976 continue; 977 pcie_aspm_check_latency(child); 978 } 979 } 980 } 981 982 /* @pdev: the endpoint device */ 983 void pcie_aspm_exit_link_state(struct pci_dev *pdev) 984 { 985 struct pci_dev *parent = pdev->bus->self; 986 struct pcie_link_state *link, *root, *parent_link; 987 988 if (!parent || !parent->link_state) 989 return; 990 991 down_read(&pci_bus_sem); 992 mutex_lock(&aspm_lock); 993 /* 994 * All PCIe functions are in one slot, remove one function will remove 995 * the whole slot, so just wait until we are the last function left. 996 */ 997 if (!list_empty(&parent->subordinate->devices)) 998 goto out; 999 1000 link = parent->link_state; 1001 root = link->root; 1002 parent_link = link->parent; 1003 1004 /* All functions are removed, so just disable ASPM for the link */ 1005 pcie_config_aspm_link(link, 0); 1006 list_del(&link->sibling); 1007 /* Clock PM is for endpoint device */ 1008 free_link_state(link); 1009 1010 /* Recheck latencies and configure upstream links */ 1011 if (parent_link) { 1012 pcie_update_aspm_capable(root); 1013 pcie_config_aspm_path(parent_link); 1014 } 1015 out: 1016 mutex_unlock(&aspm_lock); 1017 up_read(&pci_bus_sem); 1018 } 1019 1020 /* @pdev: the root port or switch downstream port */ 1021 void pcie_aspm_pm_state_change(struct pci_dev *pdev) 1022 { 1023 struct pcie_link_state *link = pdev->link_state; 1024 1025 if (aspm_disabled || !link) 1026 return; 1027 /* 1028 * Devices changed PM state, we should recheck if latency 1029 * meets all functions' requirement 1030 */ 1031 down_read(&pci_bus_sem); 1032 mutex_lock(&aspm_lock); 1033 pcie_update_aspm_capable(link->root); 1034 pcie_config_aspm_path(link); 1035 mutex_unlock(&aspm_lock); 1036 up_read(&pci_bus_sem); 1037 } 1038 1039 void pcie_aspm_powersave_config_link(struct pci_dev *pdev) 1040 { 1041 struct pcie_link_state *link = pdev->link_state; 1042 1043 if (aspm_disabled || !link) 1044 return; 1045 1046 if (aspm_policy != POLICY_POWERSAVE && 1047 aspm_policy != POLICY_POWER_SUPERSAVE) 1048 return; 1049 1050 down_read(&pci_bus_sem); 1051 mutex_lock(&aspm_lock); 1052 pcie_config_aspm_path(link); 1053 pcie_set_clkpm(link, policy_to_clkpm_state(link)); 1054 mutex_unlock(&aspm_lock); 1055 up_read(&pci_bus_sem); 1056 } 1057 1058 static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem) 1059 { 1060 struct pci_dev *parent = pdev->bus->self; 1061 struct pcie_link_state *link; 1062 1063 if (!pci_is_pcie(pdev)) 1064 return; 1065 1066 if (pdev->has_secondary_link) 1067 parent = pdev; 1068 if (!parent || !parent->link_state) 1069 return; 1070 1071 /* 1072 * A driver requested that ASPM be disabled on this device, but 1073 * if we don't have permission to manage ASPM (e.g., on ACPI 1074 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and 1075 * the _OSC method), we can't honor that request. Windows has 1076 * a similar mechanism using "PciASPMOptOut", which is also 1077 * ignored in this situation. 1078 */ 1079 if (aspm_disabled) { 1080 pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n"); 1081 return; 1082 } 1083 1084 if (sem) 1085 down_read(&pci_bus_sem); 1086 mutex_lock(&aspm_lock); 1087 link = parent->link_state; 1088 if (state & PCIE_LINK_STATE_L0S) 1089 link->aspm_disable |= ASPM_STATE_L0S; 1090 if (state & PCIE_LINK_STATE_L1) 1091 link->aspm_disable |= ASPM_STATE_L1; 1092 pcie_config_aspm_link(link, policy_to_aspm_state(link)); 1093 1094 if (state & PCIE_LINK_STATE_CLKPM) { 1095 link->clkpm_capable = 0; 1096 pcie_set_clkpm(link, 0); 1097 } 1098 mutex_unlock(&aspm_lock); 1099 if (sem) 1100 up_read(&pci_bus_sem); 1101 } 1102 1103 void pci_disable_link_state_locked(struct pci_dev *pdev, int state) 1104 { 1105 __pci_disable_link_state(pdev, state, false); 1106 } 1107 EXPORT_SYMBOL(pci_disable_link_state_locked); 1108 1109 /** 1110 * pci_disable_link_state - Disable device's link state, so the link will 1111 * never enter specific states. Note that if the BIOS didn't grant ASPM 1112 * control to the OS, this does nothing because we can't touch the LNKCTL 1113 * register. 1114 * 1115 * @pdev: PCI device 1116 * @state: ASPM link state to disable 1117 */ 1118 void pci_disable_link_state(struct pci_dev *pdev, int state) 1119 { 1120 __pci_disable_link_state(pdev, state, true); 1121 } 1122 EXPORT_SYMBOL(pci_disable_link_state); 1123 1124 static int pcie_aspm_set_policy(const char *val, 1125 const struct kernel_param *kp) 1126 { 1127 int i; 1128 struct pcie_link_state *link; 1129 1130 if (aspm_disabled) 1131 return -EPERM; 1132 i = sysfs_match_string(policy_str, val); 1133 if (i < 0) 1134 return i; 1135 if (i == aspm_policy) 1136 return 0; 1137 1138 down_read(&pci_bus_sem); 1139 mutex_lock(&aspm_lock); 1140 aspm_policy = i; 1141 list_for_each_entry(link, &link_list, sibling) { 1142 pcie_config_aspm_link(link, policy_to_aspm_state(link)); 1143 pcie_set_clkpm(link, policy_to_clkpm_state(link)); 1144 } 1145 mutex_unlock(&aspm_lock); 1146 up_read(&pci_bus_sem); 1147 return 0; 1148 } 1149 1150 static int pcie_aspm_get_policy(char *buffer, const struct kernel_param *kp) 1151 { 1152 int i, cnt = 0; 1153 for (i = 0; i < ARRAY_SIZE(policy_str); i++) 1154 if (i == aspm_policy) 1155 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]); 1156 else 1157 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]); 1158 return cnt; 1159 } 1160 1161 module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy, 1162 NULL, 0644); 1163 1164 #ifdef CONFIG_PCIEASPM_DEBUG 1165 static ssize_t link_state_show(struct device *dev, 1166 struct device_attribute *attr, 1167 char *buf) 1168 { 1169 struct pci_dev *pci_device = to_pci_dev(dev); 1170 struct pcie_link_state *link_state = pci_device->link_state; 1171 1172 return sprintf(buf, "%d\n", link_state->aspm_enabled); 1173 } 1174 1175 static ssize_t link_state_store(struct device *dev, 1176 struct device_attribute *attr, 1177 const char *buf, 1178 size_t n) 1179 { 1180 struct pci_dev *pdev = to_pci_dev(dev); 1181 struct pcie_link_state *link, *root = pdev->link_state->root; 1182 u32 state; 1183 1184 if (aspm_disabled) 1185 return -EPERM; 1186 1187 if (kstrtouint(buf, 10, &state)) 1188 return -EINVAL; 1189 if ((state & ~ASPM_STATE_ALL) != 0) 1190 return -EINVAL; 1191 1192 down_read(&pci_bus_sem); 1193 mutex_lock(&aspm_lock); 1194 list_for_each_entry(link, &link_list, sibling) { 1195 if (link->root != root) 1196 continue; 1197 pcie_config_aspm_link(link, state); 1198 } 1199 mutex_unlock(&aspm_lock); 1200 up_read(&pci_bus_sem); 1201 return n; 1202 } 1203 1204 static ssize_t clk_ctl_show(struct device *dev, 1205 struct device_attribute *attr, 1206 char *buf) 1207 { 1208 struct pci_dev *pci_device = to_pci_dev(dev); 1209 struct pcie_link_state *link_state = pci_device->link_state; 1210 1211 return sprintf(buf, "%d\n", link_state->clkpm_enabled); 1212 } 1213 1214 static ssize_t clk_ctl_store(struct device *dev, 1215 struct device_attribute *attr, 1216 const char *buf, 1217 size_t n) 1218 { 1219 struct pci_dev *pdev = to_pci_dev(dev); 1220 bool state; 1221 1222 if (strtobool(buf, &state)) 1223 return -EINVAL; 1224 1225 down_read(&pci_bus_sem); 1226 mutex_lock(&aspm_lock); 1227 pcie_set_clkpm_nocheck(pdev->link_state, state); 1228 mutex_unlock(&aspm_lock); 1229 up_read(&pci_bus_sem); 1230 1231 return n; 1232 } 1233 1234 static DEVICE_ATTR_RW(link_state); 1235 static DEVICE_ATTR_RW(clk_ctl); 1236 1237 static char power_group[] = "power"; 1238 void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev) 1239 { 1240 struct pcie_link_state *link_state = pdev->link_state; 1241 1242 if (!link_state) 1243 return; 1244 1245 if (link_state->aspm_support) 1246 sysfs_add_file_to_group(&pdev->dev.kobj, 1247 &dev_attr_link_state.attr, power_group); 1248 if (link_state->clkpm_capable) 1249 sysfs_add_file_to_group(&pdev->dev.kobj, 1250 &dev_attr_clk_ctl.attr, power_group); 1251 } 1252 1253 void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev) 1254 { 1255 struct pcie_link_state *link_state = pdev->link_state; 1256 1257 if (!link_state) 1258 return; 1259 1260 if (link_state->aspm_support) 1261 sysfs_remove_file_from_group(&pdev->dev.kobj, 1262 &dev_attr_link_state.attr, power_group); 1263 if (link_state->clkpm_capable) 1264 sysfs_remove_file_from_group(&pdev->dev.kobj, 1265 &dev_attr_clk_ctl.attr, power_group); 1266 } 1267 #endif 1268 1269 static int __init pcie_aspm_disable(char *str) 1270 { 1271 if (!strcmp(str, "off")) { 1272 aspm_policy = POLICY_DEFAULT; 1273 aspm_disabled = 1; 1274 aspm_support_enabled = false; 1275 printk(KERN_INFO "PCIe ASPM is disabled\n"); 1276 } else if (!strcmp(str, "force")) { 1277 aspm_force = 1; 1278 printk(KERN_INFO "PCIe ASPM is forcibly enabled\n"); 1279 } 1280 return 1; 1281 } 1282 1283 __setup("pcie_aspm=", pcie_aspm_disable); 1284 1285 void pcie_no_aspm(void) 1286 { 1287 /* 1288 * Disabling ASPM is intended to prevent the kernel from modifying 1289 * existing hardware state, not to clear existing state. To that end: 1290 * (a) set policy to POLICY_DEFAULT in order to avoid changing state 1291 * (b) prevent userspace from changing policy 1292 */ 1293 if (!aspm_force) { 1294 aspm_policy = POLICY_DEFAULT; 1295 aspm_disabled = 1; 1296 } 1297 } 1298 1299 bool pcie_aspm_support_enabled(void) 1300 { 1301 return aspm_support_enabled; 1302 } 1303 EXPORT_SYMBOL(pcie_aspm_support_enabled); 1304