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