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