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