| .. | .. |
|---|
| 18 | 18 | #include <linux/slab.h> |
|---|
| 19 | 19 | #include <linux/jiffies.h> |
|---|
| 20 | 20 | #include <linux/delay.h> |
|---|
| 21 | | -#include <linux/pci-aspm.h> |
|---|
| 22 | 21 | #include "../pci.h" |
|---|
| 23 | 22 | |
|---|
| 24 | 23 | #ifdef MODULE_PARAM_PREFIX |
|---|
| .. | .. |
|---|
| 53 | 52 | struct pcie_link_state *root; /* pointer to the root port link */ |
|---|
| 54 | 53 | struct pcie_link_state *parent; /* pointer to the parent Link state */ |
|---|
| 55 | 54 | struct list_head sibling; /* node in link_list */ |
|---|
| 56 | | - struct list_head children; /* list of child link states */ |
|---|
| 57 | | - struct list_head link; /* node in parent's children list */ |
|---|
| 58 | 55 | |
|---|
| 59 | 56 | /* ASPM state */ |
|---|
| 60 | 57 | u32 aspm_support:7; /* Supported ASPM state */ |
|---|
| .. | .. |
|---|
| 77 | 74 | * has one slot under it, so at most there are 8 functions. |
|---|
| 78 | 75 | */ |
|---|
| 79 | 76 | struct aspm_latency acceptable[8]; |
|---|
| 80 | | - |
|---|
| 81 | | - /* L1 PM Substate info */ |
|---|
| 82 | | - struct { |
|---|
| 83 | | - u32 up_cap_ptr; /* L1SS cap ptr in upstream dev */ |
|---|
| 84 | | - u32 dw_cap_ptr; /* L1SS cap ptr in downstream dev */ |
|---|
| 85 | | - u32 ctl1; /* value to be programmed in ctl1 */ |
|---|
| 86 | | - u32 ctl2; /* value to be programmed in ctl2 */ |
|---|
| 87 | | - } l1ss; |
|---|
| 88 | 77 | }; |
|---|
| 89 | 78 | |
|---|
| 90 | 79 | static int aspm_disabled, aspm_force; |
|---|
| .. | .. |
|---|
| 206 | 195 | static bool pcie_retrain_link(struct pcie_link_state *link) |
|---|
| 207 | 196 | { |
|---|
| 208 | 197 | struct pci_dev *parent = link->pdev; |
|---|
| 209 | | - unsigned long start_jiffies; |
|---|
| 198 | + unsigned long end_jiffies; |
|---|
| 210 | 199 | u16 reg16; |
|---|
| 211 | 200 | |
|---|
| 212 | 201 | pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16); |
|---|
| .. | .. |
|---|
| 223 | 212 | } |
|---|
| 224 | 213 | |
|---|
| 225 | 214 | /* Wait for link training end. Break out after waiting for timeout */ |
|---|
| 226 | | - start_jiffies = jiffies; |
|---|
| 227 | | - for (;;) { |
|---|
| 215 | + end_jiffies = jiffies + LINK_RETRAIN_TIMEOUT; |
|---|
| 216 | + do { |
|---|
| 228 | 217 | pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16); |
|---|
| 229 | 218 | if (!(reg16 & PCI_EXP_LNKSTA_LT)) |
|---|
| 230 | 219 | break; |
|---|
| 231 | | - if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) |
|---|
| 232 | | - break; |
|---|
| 233 | 220 | msleep(1); |
|---|
| 234 | | - } |
|---|
| 221 | + } while (time_before(jiffies, end_jiffies)); |
|---|
| 235 | 222 | return !(reg16 & PCI_EXP_LNKSTA_LT); |
|---|
| 236 | 223 | } |
|---|
| 237 | 224 | |
|---|
| .. | .. |
|---|
| 278 | 265 | } |
|---|
| 279 | 266 | if (consistent) |
|---|
| 280 | 267 | return; |
|---|
| 281 | | - pci_warn(parent, "ASPM: current common clock configuration is broken, reconfiguring\n"); |
|---|
| 268 | + pci_info(parent, "ASPM: current common clock configuration is inconsistent, reconfiguring\n"); |
|---|
| 282 | 269 | } |
|---|
| 283 | 270 | |
|---|
| 284 | 271 | /* Configure downstream component, all functions */ |
|---|
| .. | .. |
|---|
| 313 | 300 | } |
|---|
| 314 | 301 | |
|---|
| 315 | 302 | /* Convert L0s latency encoding to ns */ |
|---|
| 316 | | -static u32 calc_l0s_latency(u32 encoding) |
|---|
| 303 | +static u32 calc_l0s_latency(u32 lnkcap) |
|---|
| 317 | 304 | { |
|---|
| 305 | + u32 encoding = (lnkcap & PCI_EXP_LNKCAP_L0SEL) >> 12; |
|---|
| 306 | + |
|---|
| 318 | 307 | if (encoding == 0x7) |
|---|
| 319 | 308 | return (5 * 1000); /* > 4us */ |
|---|
| 320 | 309 | return (64 << encoding); |
|---|
| .. | .. |
|---|
| 329 | 318 | } |
|---|
| 330 | 319 | |
|---|
| 331 | 320 | /* Convert L1 latency encoding to ns */ |
|---|
| 332 | | -static u32 calc_l1_latency(u32 encoding) |
|---|
| 321 | +static u32 calc_l1_latency(u32 lnkcap) |
|---|
| 333 | 322 | { |
|---|
| 323 | + u32 encoding = (lnkcap & PCI_EXP_LNKCAP_L1EL) >> 15; |
|---|
| 324 | + |
|---|
| 334 | 325 | if (encoding == 0x7) |
|---|
| 335 | 326 | return (65 * 1000); /* > 64us */ |
|---|
| 336 | 327 | return (1000 << encoding); |
|---|
| .. | .. |
|---|
| 383 | 374 | *scale = 5; |
|---|
| 384 | 375 | *value = threshold_ns >> 25; |
|---|
| 385 | 376 | } |
|---|
| 386 | | -} |
|---|
| 387 | | - |
|---|
| 388 | | -struct aspm_register_info { |
|---|
| 389 | | - u32 support:2; |
|---|
| 390 | | - u32 enabled:2; |
|---|
| 391 | | - u32 latency_encoding_l0s; |
|---|
| 392 | | - u32 latency_encoding_l1; |
|---|
| 393 | | - |
|---|
| 394 | | - /* L1 substates */ |
|---|
| 395 | | - u32 l1ss_cap_ptr; |
|---|
| 396 | | - u32 l1ss_cap; |
|---|
| 397 | | - u32 l1ss_ctl1; |
|---|
| 398 | | - u32 l1ss_ctl2; |
|---|
| 399 | | -}; |
|---|
| 400 | | - |
|---|
| 401 | | -static void pcie_get_aspm_reg(struct pci_dev *pdev, |
|---|
| 402 | | - struct aspm_register_info *info) |
|---|
| 403 | | -{ |
|---|
| 404 | | - u16 reg16; |
|---|
| 405 | | - u32 reg32; |
|---|
| 406 | | - |
|---|
| 407 | | - pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, ®32); |
|---|
| 408 | | - info->support = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10; |
|---|
| 409 | | - info->latency_encoding_l0s = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12; |
|---|
| 410 | | - info->latency_encoding_l1 = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15; |
|---|
| 411 | | - pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, ®16); |
|---|
| 412 | | - info->enabled = reg16 & PCI_EXP_LNKCTL_ASPMC; |
|---|
| 413 | | - |
|---|
| 414 | | - /* Read L1 PM substate capabilities */ |
|---|
| 415 | | - info->l1ss_cap = info->l1ss_ctl1 = info->l1ss_ctl2 = 0; |
|---|
| 416 | | - info->l1ss_cap_ptr = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS); |
|---|
| 417 | | - if (!info->l1ss_cap_ptr) |
|---|
| 418 | | - return; |
|---|
| 419 | | - pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CAP, |
|---|
| 420 | | - &info->l1ss_cap); |
|---|
| 421 | | - if (!(info->l1ss_cap & PCI_L1SS_CAP_L1_PM_SS)) { |
|---|
| 422 | | - info->l1ss_cap = 0; |
|---|
| 423 | | - return; |
|---|
| 424 | | - } |
|---|
| 425 | | - |
|---|
| 426 | | - /* |
|---|
| 427 | | - * If we don't have LTR for the entire path from the Root Complex |
|---|
| 428 | | - * to this device, we can't use ASPM L1.2 because it relies on the |
|---|
| 429 | | - * LTR_L1.2_THRESHOLD. See PCIe r4.0, secs 5.5.4, 6.18. |
|---|
| 430 | | - */ |
|---|
| 431 | | - if (!pdev->ltr_path) |
|---|
| 432 | | - info->l1ss_cap &= ~PCI_L1SS_CAP_ASPM_L1_2; |
|---|
| 433 | | - |
|---|
| 434 | | - pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 435 | | - &info->l1ss_ctl1); |
|---|
| 436 | | - pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL2, |
|---|
| 437 | | - &info->l1ss_ctl2); |
|---|
| 438 | 377 | } |
|---|
| 439 | 378 | |
|---|
| 440 | 379 | static void pcie_aspm_check_latency(struct pci_dev *endpoint) |
|---|
| .. | .. |
|---|
| 498 | 437 | return NULL; |
|---|
| 499 | 438 | } |
|---|
| 500 | 439 | |
|---|
| 440 | +static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos, |
|---|
| 441 | + u32 clear, u32 set) |
|---|
| 442 | +{ |
|---|
| 443 | + u32 val; |
|---|
| 444 | + |
|---|
| 445 | + pci_read_config_dword(pdev, pos, &val); |
|---|
| 446 | + val &= ~clear; |
|---|
| 447 | + val |= set; |
|---|
| 448 | + pci_write_config_dword(pdev, pos, val); |
|---|
| 449 | +} |
|---|
| 450 | + |
|---|
| 501 | 451 | /* Calculate L1.2 PM substate timing parameters */ |
|---|
| 502 | 452 | static void aspm_calc_l1ss_info(struct pcie_link_state *link, |
|---|
| 503 | | - struct aspm_register_info *upreg, |
|---|
| 504 | | - struct aspm_register_info *dwreg) |
|---|
| 453 | + u32 parent_l1ss_cap, u32 child_l1ss_cap) |
|---|
| 505 | 454 | { |
|---|
| 455 | + struct pci_dev *child = link->downstream, *parent = link->pdev; |
|---|
| 506 | 456 | u32 val1, val2, scale1, scale2; |
|---|
| 507 | 457 | u32 t_common_mode, t_power_on, l1_2_threshold, scale, value; |
|---|
| 508 | | - |
|---|
| 509 | | - link->l1ss.up_cap_ptr = upreg->l1ss_cap_ptr; |
|---|
| 510 | | - link->l1ss.dw_cap_ptr = dwreg->l1ss_cap_ptr; |
|---|
| 511 | | - link->l1ss.ctl1 = link->l1ss.ctl2 = 0; |
|---|
| 458 | + u32 ctl1 = 0, ctl2 = 0; |
|---|
| 459 | + u32 pctl1, pctl2, cctl1, cctl2; |
|---|
| 460 | + u32 pl1_2_enables, cl1_2_enables; |
|---|
| 512 | 461 | |
|---|
| 513 | 462 | if (!(link->aspm_support & ASPM_STATE_L1_2_MASK)) |
|---|
| 514 | 463 | return; |
|---|
| 515 | 464 | |
|---|
| 516 | 465 | /* Choose the greater of the two Port Common_Mode_Restore_Times */ |
|---|
| 517 | | - val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8; |
|---|
| 518 | | - val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8; |
|---|
| 466 | + val1 = (parent_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8; |
|---|
| 467 | + val2 = (child_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8; |
|---|
| 519 | 468 | t_common_mode = max(val1, val2); |
|---|
| 520 | 469 | |
|---|
| 521 | 470 | /* Choose the greater of the two Port T_POWER_ON times */ |
|---|
| 522 | | - val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19; |
|---|
| 523 | | - scale1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16; |
|---|
| 524 | | - val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19; |
|---|
| 525 | | - scale2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16; |
|---|
| 471 | + val1 = (parent_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19; |
|---|
| 472 | + scale1 = (parent_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16; |
|---|
| 473 | + val2 = (child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19; |
|---|
| 474 | + scale2 = (child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16; |
|---|
| 526 | 475 | |
|---|
| 527 | | - if (calc_l1ss_pwron(link->pdev, scale1, val1) > |
|---|
| 528 | | - calc_l1ss_pwron(link->downstream, scale2, val2)) { |
|---|
| 529 | | - link->l1ss.ctl2 |= scale1 | (val1 << 3); |
|---|
| 530 | | - t_power_on = calc_l1ss_pwron(link->pdev, scale1, val1); |
|---|
| 476 | + if (calc_l1ss_pwron(parent, scale1, val1) > |
|---|
| 477 | + calc_l1ss_pwron(child, scale2, val2)) { |
|---|
| 478 | + ctl2 |= scale1 | (val1 << 3); |
|---|
| 479 | + t_power_on = calc_l1ss_pwron(parent, scale1, val1); |
|---|
| 531 | 480 | } else { |
|---|
| 532 | | - link->l1ss.ctl2 |= scale2 | (val2 << 3); |
|---|
| 533 | | - t_power_on = calc_l1ss_pwron(link->downstream, scale2, val2); |
|---|
| 481 | + ctl2 |= scale2 | (val2 << 3); |
|---|
| 482 | + t_power_on = calc_l1ss_pwron(child, scale2, val2); |
|---|
| 534 | 483 | } |
|---|
| 535 | 484 | |
|---|
| 536 | 485 | /* |
|---|
| .. | .. |
|---|
| 545 | 494 | */ |
|---|
| 546 | 495 | l1_2_threshold = 2 + 4 + t_common_mode + t_power_on; |
|---|
| 547 | 496 | encode_l12_threshold(l1_2_threshold, &scale, &value); |
|---|
| 548 | | - link->l1ss.ctl1 |= t_common_mode << 8 | scale << 29 | value << 16; |
|---|
| 497 | + ctl1 |= t_common_mode << 8 | scale << 29 | value << 16; |
|---|
| 498 | + |
|---|
| 499 | + pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, &pctl1); |
|---|
| 500 | + pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, &pctl2); |
|---|
| 501 | + pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL1, &cctl1); |
|---|
| 502 | + pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL2, &cctl2); |
|---|
| 503 | + |
|---|
| 504 | + if (ctl1 == pctl1 && ctl1 == cctl1 && |
|---|
| 505 | + ctl2 == pctl2 && ctl2 == cctl2) |
|---|
| 506 | + return; |
|---|
| 507 | + |
|---|
| 508 | + /* Disable L1.2 while updating. See PCIe r5.0, sec 5.5.4, 7.8.3.3 */ |
|---|
| 509 | + pl1_2_enables = pctl1 & PCI_L1SS_CTL1_L1_2_MASK; |
|---|
| 510 | + cl1_2_enables = cctl1 & PCI_L1SS_CTL1_L1_2_MASK; |
|---|
| 511 | + |
|---|
| 512 | + if (pl1_2_enables || cl1_2_enables) { |
|---|
| 513 | + pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, |
|---|
| 514 | + PCI_L1SS_CTL1_L1_2_MASK, 0); |
|---|
| 515 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 516 | + PCI_L1SS_CTL1_L1_2_MASK, 0); |
|---|
| 517 | + } |
|---|
| 518 | + |
|---|
| 519 | + /* Program T_POWER_ON times in both ports */ |
|---|
| 520 | + pci_write_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, ctl2); |
|---|
| 521 | + pci_write_config_dword(child, child->l1ss + PCI_L1SS_CTL2, ctl2); |
|---|
| 522 | + |
|---|
| 523 | + /* Program Common_Mode_Restore_Time in upstream device */ |
|---|
| 524 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 525 | + PCI_L1SS_CTL1_CM_RESTORE_TIME, ctl1); |
|---|
| 526 | + |
|---|
| 527 | + /* Program LTR_L1.2_THRESHOLD time in both ports */ |
|---|
| 528 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 529 | + PCI_L1SS_CTL1_LTR_L12_TH_VALUE | |
|---|
| 530 | + PCI_L1SS_CTL1_LTR_L12_TH_SCALE, ctl1); |
|---|
| 531 | + pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, |
|---|
| 532 | + PCI_L1SS_CTL1_LTR_L12_TH_VALUE | |
|---|
| 533 | + PCI_L1SS_CTL1_LTR_L12_TH_SCALE, ctl1); |
|---|
| 534 | + |
|---|
| 535 | + if (pl1_2_enables || cl1_2_enables) { |
|---|
| 536 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, 0, |
|---|
| 537 | + pl1_2_enables); |
|---|
| 538 | + pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, 0, |
|---|
| 539 | + cl1_2_enables); |
|---|
| 540 | + } |
|---|
| 549 | 541 | } |
|---|
| 550 | 542 | |
|---|
| 551 | 543 | static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist) |
|---|
| 552 | 544 | { |
|---|
| 553 | 545 | struct pci_dev *child = link->downstream, *parent = link->pdev; |
|---|
| 546 | + u32 parent_lnkcap, child_lnkcap; |
|---|
| 547 | + u16 parent_lnkctl, child_lnkctl; |
|---|
| 548 | + u32 parent_l1ss_cap, child_l1ss_cap; |
|---|
| 549 | + u32 parent_l1ss_ctl1 = 0, child_l1ss_ctl1 = 0; |
|---|
| 554 | 550 | struct pci_bus *linkbus = parent->subordinate; |
|---|
| 555 | | - struct aspm_register_info upreg, dwreg; |
|---|
| 556 | 551 | |
|---|
| 557 | 552 | if (blacklist) { |
|---|
| 558 | 553 | /* Set enabled/disable so that we will disable ASPM later */ |
|---|
| .. | .. |
|---|
| 561 | 556 | return; |
|---|
| 562 | 557 | } |
|---|
| 563 | 558 | |
|---|
| 564 | | - /* Get upstream/downstream components' register state */ |
|---|
| 565 | | - pcie_get_aspm_reg(parent, &upreg); |
|---|
| 566 | | - pcie_get_aspm_reg(child, &dwreg); |
|---|
| 567 | | - |
|---|
| 568 | 559 | /* |
|---|
| 569 | 560 | * If ASPM not supported, don't mess with the clocks and link, |
|---|
| 570 | 561 | * bail out now. |
|---|
| 571 | 562 | */ |
|---|
| 572 | | - if (!(upreg.support & dwreg.support)) |
|---|
| 563 | + pcie_capability_read_dword(parent, PCI_EXP_LNKCAP, &parent_lnkcap); |
|---|
| 564 | + pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &child_lnkcap); |
|---|
| 565 | + if (!(parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPMS)) |
|---|
| 573 | 566 | return; |
|---|
| 574 | 567 | |
|---|
| 575 | 568 | /* Configure common clock before checking latencies */ |
|---|
| 576 | 569 | pcie_aspm_configure_common_clock(link); |
|---|
| 577 | 570 | |
|---|
| 578 | 571 | /* |
|---|
| 579 | | - * Re-read upstream/downstream components' register state |
|---|
| 580 | | - * after clock configuration |
|---|
| 572 | + * Re-read upstream/downstream components' register state after |
|---|
| 573 | + * clock configuration. L0s & L1 exit latencies in the otherwise |
|---|
| 574 | + * read-only Link Capabilities may change depending on common clock |
|---|
| 575 | + * configuration (PCIe r5.0, sec 7.5.3.6). |
|---|
| 581 | 576 | */ |
|---|
| 582 | | - pcie_get_aspm_reg(parent, &upreg); |
|---|
| 583 | | - pcie_get_aspm_reg(child, &dwreg); |
|---|
| 577 | + pcie_capability_read_dword(parent, PCI_EXP_LNKCAP, &parent_lnkcap); |
|---|
| 578 | + pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &child_lnkcap); |
|---|
| 579 | + pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &parent_lnkctl); |
|---|
| 580 | + pcie_capability_read_word(child, PCI_EXP_LNKCTL, &child_lnkctl); |
|---|
| 584 | 581 | |
|---|
| 585 | 582 | /* |
|---|
| 586 | 583 | * Setup L0s state |
|---|
| .. | .. |
|---|
| 589 | 586 | * given link unless components on both sides of the link each |
|---|
| 590 | 587 | * support L0s. |
|---|
| 591 | 588 | */ |
|---|
| 592 | | - if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S) |
|---|
| 589 | + if (parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPM_L0S) |
|---|
| 593 | 590 | link->aspm_support |= ASPM_STATE_L0S; |
|---|
| 594 | | - if (dwreg.enabled & PCIE_LINK_STATE_L0S) |
|---|
| 591 | + |
|---|
| 592 | + if (child_lnkctl & PCI_EXP_LNKCTL_ASPM_L0S) |
|---|
| 595 | 593 | link->aspm_enabled |= ASPM_STATE_L0S_UP; |
|---|
| 596 | | - if (upreg.enabled & PCIE_LINK_STATE_L0S) |
|---|
| 594 | + if (parent_lnkctl & PCI_EXP_LNKCTL_ASPM_L0S) |
|---|
| 597 | 595 | link->aspm_enabled |= ASPM_STATE_L0S_DW; |
|---|
| 598 | | - link->latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s); |
|---|
| 599 | | - link->latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s); |
|---|
| 596 | + link->latency_up.l0s = calc_l0s_latency(parent_lnkcap); |
|---|
| 597 | + link->latency_dw.l0s = calc_l0s_latency(child_lnkcap); |
|---|
| 600 | 598 | |
|---|
| 601 | 599 | /* Setup L1 state */ |
|---|
| 602 | | - if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1) |
|---|
| 600 | + if (parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPM_L1) |
|---|
| 603 | 601 | link->aspm_support |= ASPM_STATE_L1; |
|---|
| 604 | | - if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1) |
|---|
| 602 | + |
|---|
| 603 | + if (parent_lnkctl & child_lnkctl & PCI_EXP_LNKCTL_ASPM_L1) |
|---|
| 605 | 604 | link->aspm_enabled |= ASPM_STATE_L1; |
|---|
| 606 | | - link->latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1); |
|---|
| 607 | | - link->latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1); |
|---|
| 605 | + link->latency_up.l1 = calc_l1_latency(parent_lnkcap); |
|---|
| 606 | + link->latency_dw.l1 = calc_l1_latency(child_lnkcap); |
|---|
| 608 | 607 | |
|---|
| 609 | 608 | /* Setup L1 substate */ |
|---|
| 610 | | - if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1) |
|---|
| 609 | + pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CAP, |
|---|
| 610 | + &parent_l1ss_cap); |
|---|
| 611 | + pci_read_config_dword(child, child->l1ss + PCI_L1SS_CAP, |
|---|
| 612 | + &child_l1ss_cap); |
|---|
| 613 | + |
|---|
| 614 | + if (!(parent_l1ss_cap & PCI_L1SS_CAP_L1_PM_SS)) |
|---|
| 615 | + parent_l1ss_cap = 0; |
|---|
| 616 | + if (!(child_l1ss_cap & PCI_L1SS_CAP_L1_PM_SS)) |
|---|
| 617 | + child_l1ss_cap = 0; |
|---|
| 618 | + |
|---|
| 619 | + /* |
|---|
| 620 | + * If we don't have LTR for the entire path from the Root Complex |
|---|
| 621 | + * to this device, we can't use ASPM L1.2 because it relies on the |
|---|
| 622 | + * LTR_L1.2_THRESHOLD. See PCIe r4.0, secs 5.5.4, 6.18. |
|---|
| 623 | + */ |
|---|
| 624 | + if (!child->ltr_path) |
|---|
| 625 | + child_l1ss_cap &= ~PCI_L1SS_CAP_ASPM_L1_2; |
|---|
| 626 | + |
|---|
| 627 | + if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1) |
|---|
| 611 | 628 | link->aspm_support |= ASPM_STATE_L1_1; |
|---|
| 612 | | - if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2) |
|---|
| 629 | + if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2) |
|---|
| 613 | 630 | link->aspm_support |= ASPM_STATE_L1_2; |
|---|
| 614 | | - if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1) |
|---|
| 631 | + if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1) |
|---|
| 615 | 632 | link->aspm_support |= ASPM_STATE_L1_1_PCIPM; |
|---|
| 616 | | - if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2) |
|---|
| 633 | + if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2) |
|---|
| 617 | 634 | link->aspm_support |= ASPM_STATE_L1_2_PCIPM; |
|---|
| 618 | 635 | |
|---|
| 619 | | - if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1) |
|---|
| 636 | + if (parent_l1ss_cap) |
|---|
| 637 | + pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 638 | + &parent_l1ss_ctl1); |
|---|
| 639 | + if (child_l1ss_cap) |
|---|
| 640 | + pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL1, |
|---|
| 641 | + &child_l1ss_ctl1); |
|---|
| 642 | + |
|---|
| 643 | + if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1) |
|---|
| 620 | 644 | link->aspm_enabled |= ASPM_STATE_L1_1; |
|---|
| 621 | | - if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2) |
|---|
| 645 | + if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2) |
|---|
| 622 | 646 | link->aspm_enabled |= ASPM_STATE_L1_2; |
|---|
| 623 | | - if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1) |
|---|
| 647 | + if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1) |
|---|
| 624 | 648 | link->aspm_enabled |= ASPM_STATE_L1_1_PCIPM; |
|---|
| 625 | | - if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2) |
|---|
| 649 | + if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2) |
|---|
| 626 | 650 | link->aspm_enabled |= ASPM_STATE_L1_2_PCIPM; |
|---|
| 627 | 651 | |
|---|
| 628 | 652 | if (link->aspm_support & ASPM_STATE_L1SS) |
|---|
| 629 | | - aspm_calc_l1ss_info(link, &upreg, &dwreg); |
|---|
| 653 | + aspm_calc_l1ss_info(link, parent_l1ss_cap, child_l1ss_cap); |
|---|
| 630 | 654 | |
|---|
| 631 | 655 | /* Save default state */ |
|---|
| 632 | 656 | link->aspm_default = link->aspm_enabled; |
|---|
| .. | .. |
|---|
| 656 | 680 | } |
|---|
| 657 | 681 | } |
|---|
| 658 | 682 | |
|---|
| 659 | | -static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos, |
|---|
| 660 | | - u32 clear, u32 set) |
|---|
| 661 | | -{ |
|---|
| 662 | | - u32 val; |
|---|
| 663 | | - |
|---|
| 664 | | - pci_read_config_dword(pdev, pos, &val); |
|---|
| 665 | | - val &= ~clear; |
|---|
| 666 | | - val |= set; |
|---|
| 667 | | - pci_write_config_dword(pdev, pos, val); |
|---|
| 668 | | -} |
|---|
| 669 | | - |
|---|
| 670 | 683 | /* Configure the ASPM L1 substates */ |
|---|
| 671 | 684 | static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state) |
|---|
| 672 | 685 | { |
|---|
| 673 | 686 | u32 val, enable_req; |
|---|
| 674 | 687 | struct pci_dev *child = link->downstream, *parent = link->pdev; |
|---|
| 675 | | - u32 up_cap_ptr = link->l1ss.up_cap_ptr; |
|---|
| 676 | | - u32 dw_cap_ptr = link->l1ss.dw_cap_ptr; |
|---|
| 677 | 688 | |
|---|
| 678 | 689 | enable_req = (link->aspm_enabled ^ state) & state; |
|---|
| 679 | 690 | |
|---|
| .. | .. |
|---|
| 691 | 702 | */ |
|---|
| 692 | 703 | |
|---|
| 693 | 704 | /* Disable all L1 substates */ |
|---|
| 694 | | - pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 705 | + pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, |
|---|
| 695 | 706 | PCI_L1SS_CTL1_L1SS_MASK, 0); |
|---|
| 696 | | - pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 707 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 697 | 708 | PCI_L1SS_CTL1_L1SS_MASK, 0); |
|---|
| 698 | 709 | /* |
|---|
| 699 | 710 | * If needed, disable L1, and it gets enabled later |
|---|
| .. | .. |
|---|
| 704 | 715 | PCI_EXP_LNKCTL_ASPM_L1, 0); |
|---|
| 705 | 716 | pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL, |
|---|
| 706 | 717 | PCI_EXP_LNKCTL_ASPM_L1, 0); |
|---|
| 707 | | - } |
|---|
| 708 | | - |
|---|
| 709 | | - if (enable_req & ASPM_STATE_L1_2_MASK) { |
|---|
| 710 | | - |
|---|
| 711 | | - /* Program T_POWER_ON times in both ports */ |
|---|
| 712 | | - pci_write_config_dword(parent, up_cap_ptr + PCI_L1SS_CTL2, |
|---|
| 713 | | - link->l1ss.ctl2); |
|---|
| 714 | | - pci_write_config_dword(child, dw_cap_ptr + PCI_L1SS_CTL2, |
|---|
| 715 | | - link->l1ss.ctl2); |
|---|
| 716 | | - |
|---|
| 717 | | - /* Program Common_Mode_Restore_Time in upstream device */ |
|---|
| 718 | | - pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 719 | | - PCI_L1SS_CTL1_CM_RESTORE_TIME, |
|---|
| 720 | | - link->l1ss.ctl1); |
|---|
| 721 | | - |
|---|
| 722 | | - /* Program LTR_L1.2_THRESHOLD time in both ports */ |
|---|
| 723 | | - pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 724 | | - PCI_L1SS_CTL1_LTR_L12_TH_VALUE | |
|---|
| 725 | | - PCI_L1SS_CTL1_LTR_L12_TH_SCALE, |
|---|
| 726 | | - link->l1ss.ctl1); |
|---|
| 727 | | - pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 728 | | - PCI_L1SS_CTL1_LTR_L12_TH_VALUE | |
|---|
| 729 | | - PCI_L1SS_CTL1_LTR_L12_TH_SCALE, |
|---|
| 730 | | - link->l1ss.ctl1); |
|---|
| 731 | 718 | } |
|---|
| 732 | 719 | |
|---|
| 733 | 720 | val = 0; |
|---|
| .. | .. |
|---|
| 741 | 728 | val |= PCI_L1SS_CTL1_PCIPM_L1_2; |
|---|
| 742 | 729 | |
|---|
| 743 | 730 | /* Enable what we need to enable */ |
|---|
| 744 | | - pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 731 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 745 | 732 | PCI_L1SS_CTL1_L1SS_MASK, val); |
|---|
| 746 | | - pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 733 | + pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, |
|---|
| 747 | 734 | PCI_L1SS_CTL1_L1SS_MASK, val); |
|---|
| 748 | 735 | } |
|---|
| 749 | 736 | |
|---|
| .. | .. |
|---|
| 862 | 849 | return NULL; |
|---|
| 863 | 850 | |
|---|
| 864 | 851 | INIT_LIST_HEAD(&link->sibling); |
|---|
| 865 | | - INIT_LIST_HEAD(&link->children); |
|---|
| 866 | | - INIT_LIST_HEAD(&link->link); |
|---|
| 867 | 852 | link->pdev = pdev; |
|---|
| 868 | 853 | link->downstream = pci_function_0(pdev->subordinate); |
|---|
| 869 | 854 | |
|---|
| .. | .. |
|---|
| 889 | 874 | |
|---|
| 890 | 875 | link->parent = parent; |
|---|
| 891 | 876 | link->root = link->parent->root; |
|---|
| 892 | | - list_add(&link->link, &parent->children); |
|---|
| 893 | 877 | } |
|---|
| 894 | 878 | |
|---|
| 895 | 879 | list_add(&link->sibling, &link_list); |
|---|
| 896 | 880 | pdev->link_state = link; |
|---|
| 897 | 881 | return link; |
|---|
| 882 | +} |
|---|
| 883 | + |
|---|
| 884 | +static void pcie_aspm_update_sysfs_visibility(struct pci_dev *pdev) |
|---|
| 885 | +{ |
|---|
| 886 | + struct pci_dev *child; |
|---|
| 887 | + |
|---|
| 888 | + list_for_each_entry(child, &pdev->subordinate->devices, bus_list) |
|---|
| 889 | + sysfs_update_group(&child->dev.kobj, &aspm_ctrl_attr_group); |
|---|
| 898 | 890 | } |
|---|
| 899 | 891 | |
|---|
| 900 | 892 | /* |
|---|
| .. | .. |
|---|
| 915 | 907 | |
|---|
| 916 | 908 | /* |
|---|
| 917 | 909 | * We allocate pcie_link_state for the component on the upstream |
|---|
| 918 | | - * end of a Link, so there's nothing to do unless this device has a |
|---|
| 919 | | - * Link on its secondary side. |
|---|
| 910 | + * end of a Link, so there's nothing to do unless this device is |
|---|
| 911 | + * downstream port. |
|---|
| 920 | 912 | */ |
|---|
| 921 | | - if (!pdev->has_secondary_link) |
|---|
| 913 | + if (!pcie_downstream_port(pdev)) |
|---|
| 922 | 914 | return; |
|---|
| 923 | 915 | |
|---|
| 924 | 916 | /* VIA has a strange chipset, root port is under a bridge */ |
|---|
| .. | .. |
|---|
| 957 | 949 | pcie_config_aspm_path(link); |
|---|
| 958 | 950 | pcie_set_clkpm(link, policy_to_clkpm_state(link)); |
|---|
| 959 | 951 | } |
|---|
| 952 | + |
|---|
| 953 | + pcie_aspm_update_sysfs_visibility(pdev); |
|---|
| 960 | 954 | |
|---|
| 961 | 955 | unlock: |
|---|
| 962 | 956 | mutex_unlock(&aspm_lock); |
|---|
| .. | .. |
|---|
| 1013 | 1007 | /* All functions are removed, so just disable ASPM for the link */ |
|---|
| 1014 | 1008 | pcie_config_aspm_link(link, 0); |
|---|
| 1015 | 1009 | list_del(&link->sibling); |
|---|
| 1016 | | - list_del(&link->link); |
|---|
| 1017 | 1010 | /* Clock PM is for endpoint device */ |
|---|
| 1018 | 1011 | free_link_state(link); |
|---|
| 1019 | 1012 | |
|---|
| .. | .. |
|---|
| 1065 | 1058 | up_read(&pci_bus_sem); |
|---|
| 1066 | 1059 | } |
|---|
| 1067 | 1060 | |
|---|
| 1068 | | -static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem) |
|---|
| 1061 | +static struct pcie_link_state *pcie_aspm_get_link(struct pci_dev *pdev) |
|---|
| 1069 | 1062 | { |
|---|
| 1070 | | - struct pci_dev *parent = pdev->bus->self; |
|---|
| 1071 | | - struct pcie_link_state *link; |
|---|
| 1063 | + struct pci_dev *bridge; |
|---|
| 1072 | 1064 | |
|---|
| 1073 | 1065 | if (!pci_is_pcie(pdev)) |
|---|
| 1074 | | - return; |
|---|
| 1066 | + return NULL; |
|---|
| 1075 | 1067 | |
|---|
| 1076 | | - if (pdev->has_secondary_link) |
|---|
| 1077 | | - parent = pdev; |
|---|
| 1078 | | - if (!parent || !parent->link_state) |
|---|
| 1079 | | - return; |
|---|
| 1068 | + bridge = pci_upstream_bridge(pdev); |
|---|
| 1069 | + if (!bridge || !pci_is_pcie(bridge)) |
|---|
| 1070 | + return NULL; |
|---|
| 1080 | 1071 | |
|---|
| 1072 | + return bridge->link_state; |
|---|
| 1073 | +} |
|---|
| 1074 | + |
|---|
| 1075 | +static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem) |
|---|
| 1076 | +{ |
|---|
| 1077 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1078 | + |
|---|
| 1079 | + if (!link) |
|---|
| 1080 | + return -EINVAL; |
|---|
| 1081 | 1081 | /* |
|---|
| 1082 | 1082 | * A driver requested that ASPM be disabled on this device, but |
|---|
| 1083 | 1083 | * if we don't have permission to manage ASPM (e.g., on ACPI |
|---|
| .. | .. |
|---|
| 1088 | 1088 | */ |
|---|
| 1089 | 1089 | if (aspm_disabled) { |
|---|
| 1090 | 1090 | pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n"); |
|---|
| 1091 | | - return; |
|---|
| 1091 | + return -EPERM; |
|---|
| 1092 | 1092 | } |
|---|
| 1093 | 1093 | |
|---|
| 1094 | 1094 | if (sem) |
|---|
| 1095 | 1095 | down_read(&pci_bus_sem); |
|---|
| 1096 | 1096 | mutex_lock(&aspm_lock); |
|---|
| 1097 | | - link = parent->link_state; |
|---|
| 1098 | 1097 | if (state & PCIE_LINK_STATE_L0S) |
|---|
| 1099 | 1098 | link->aspm_disable |= ASPM_STATE_L0S; |
|---|
| 1100 | 1099 | if (state & PCIE_LINK_STATE_L1) |
|---|
| 1101 | | - link->aspm_disable |= ASPM_STATE_L1; |
|---|
| 1100 | + /* L1 PM substates require L1 */ |
|---|
| 1101 | + link->aspm_disable |= ASPM_STATE_L1 | ASPM_STATE_L1SS; |
|---|
| 1102 | + if (state & PCIE_LINK_STATE_L1_1) |
|---|
| 1103 | + link->aspm_disable |= ASPM_STATE_L1_1; |
|---|
| 1104 | + if (state & PCIE_LINK_STATE_L1_2) |
|---|
| 1105 | + link->aspm_disable |= ASPM_STATE_L1_2; |
|---|
| 1106 | + if (state & PCIE_LINK_STATE_L1_1_PCIPM) |
|---|
| 1107 | + link->aspm_disable |= ASPM_STATE_L1_1_PCIPM; |
|---|
| 1108 | + if (state & PCIE_LINK_STATE_L1_2_PCIPM) |
|---|
| 1109 | + link->aspm_disable |= ASPM_STATE_L1_2_PCIPM; |
|---|
| 1102 | 1110 | pcie_config_aspm_link(link, policy_to_aspm_state(link)); |
|---|
| 1103 | 1111 | |
|---|
| 1104 | 1112 | if (state & PCIE_LINK_STATE_CLKPM) |
|---|
| .. | .. |
|---|
| 1107 | 1115 | mutex_unlock(&aspm_lock); |
|---|
| 1108 | 1116 | if (sem) |
|---|
| 1109 | 1117 | up_read(&pci_bus_sem); |
|---|
| 1118 | + |
|---|
| 1119 | + return 0; |
|---|
| 1110 | 1120 | } |
|---|
| 1111 | 1121 | |
|---|
| 1112 | | -void pci_disable_link_state_locked(struct pci_dev *pdev, int state) |
|---|
| 1122 | +int pci_disable_link_state_locked(struct pci_dev *pdev, int state) |
|---|
| 1113 | 1123 | { |
|---|
| 1114 | | - __pci_disable_link_state(pdev, state, false); |
|---|
| 1124 | + return __pci_disable_link_state(pdev, state, false); |
|---|
| 1115 | 1125 | } |
|---|
| 1116 | 1126 | EXPORT_SYMBOL(pci_disable_link_state_locked); |
|---|
| 1117 | 1127 | |
|---|
| .. | .. |
|---|
| 1119 | 1129 | * pci_disable_link_state - Disable device's link state, so the link will |
|---|
| 1120 | 1130 | * never enter specific states. Note that if the BIOS didn't grant ASPM |
|---|
| 1121 | 1131 | * control to the OS, this does nothing because we can't touch the LNKCTL |
|---|
| 1122 | | - * register. |
|---|
| 1132 | + * register. Returns 0 or a negative errno. |
|---|
| 1123 | 1133 | * |
|---|
| 1124 | 1134 | * @pdev: PCI device |
|---|
| 1125 | 1135 | * @state: ASPM link state to disable |
|---|
| 1126 | 1136 | */ |
|---|
| 1127 | | -void pci_disable_link_state(struct pci_dev *pdev, int state) |
|---|
| 1137 | +int pci_disable_link_state(struct pci_dev *pdev, int state) |
|---|
| 1128 | 1138 | { |
|---|
| 1129 | | - __pci_disable_link_state(pdev, state, true); |
|---|
| 1139 | + return __pci_disable_link_state(pdev, state, true); |
|---|
| 1130 | 1140 | } |
|---|
| 1131 | 1141 | EXPORT_SYMBOL(pci_disable_link_state); |
|---|
| 1132 | 1142 | |
|---|
| .. | .. |
|---|
| 1171 | 1181 | module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy, |
|---|
| 1172 | 1182 | NULL, 0644); |
|---|
| 1173 | 1183 | |
|---|
| 1174 | | -#ifdef CONFIG_PCIEASPM_DEBUG |
|---|
| 1175 | | -static ssize_t link_state_show(struct device *dev, |
|---|
| 1176 | | - struct device_attribute *attr, |
|---|
| 1177 | | - char *buf) |
|---|
| 1184 | +/** |
|---|
| 1185 | + * pcie_aspm_enabled - Check if PCIe ASPM has been enabled for a device. |
|---|
| 1186 | + * @pdev: Target device. |
|---|
| 1187 | + * |
|---|
| 1188 | + * Relies on the upstream bridge's link_state being valid. The link_state |
|---|
| 1189 | + * is deallocated only when the last child of the bridge (i.e., @pdev or a |
|---|
| 1190 | + * sibling) is removed, and the caller should be holding a reference to |
|---|
| 1191 | + * @pdev, so this should be safe. |
|---|
| 1192 | + */ |
|---|
| 1193 | +bool pcie_aspm_enabled(struct pci_dev *pdev) |
|---|
| 1178 | 1194 | { |
|---|
| 1179 | | - struct pci_dev *pci_device = to_pci_dev(dev); |
|---|
| 1180 | | - struct pcie_link_state *link_state = pci_device->link_state; |
|---|
| 1195 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1181 | 1196 | |
|---|
| 1182 | | - return sprintf(buf, "%d\n", link_state->aspm_enabled); |
|---|
| 1197 | + if (!link) |
|---|
| 1198 | + return false; |
|---|
| 1199 | + |
|---|
| 1200 | + return link->aspm_enabled; |
|---|
| 1183 | 1201 | } |
|---|
| 1202 | +EXPORT_SYMBOL_GPL(pcie_aspm_enabled); |
|---|
| 1184 | 1203 | |
|---|
| 1185 | | -static ssize_t link_state_store(struct device *dev, |
|---|
| 1186 | | - struct device_attribute *attr, |
|---|
| 1187 | | - const char *buf, |
|---|
| 1188 | | - size_t n) |
|---|
| 1204 | +static ssize_t aspm_attr_show_common(struct device *dev, |
|---|
| 1205 | + struct device_attribute *attr, |
|---|
| 1206 | + char *buf, u8 state) |
|---|
| 1189 | 1207 | { |
|---|
| 1190 | 1208 | struct pci_dev *pdev = to_pci_dev(dev); |
|---|
| 1191 | | - struct pcie_link_state *link, *root = pdev->link_state->root; |
|---|
| 1192 | | - u32 state; |
|---|
| 1209 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1193 | 1210 | |
|---|
| 1194 | | - if (aspm_disabled) |
|---|
| 1195 | | - return -EPERM; |
|---|
| 1211 | + return sprintf(buf, "%d\n", (link->aspm_enabled & state) ? 1 : 0); |
|---|
| 1212 | +} |
|---|
| 1196 | 1213 | |
|---|
| 1197 | | - if (kstrtouint(buf, 10, &state)) |
|---|
| 1198 | | - return -EINVAL; |
|---|
| 1199 | | - if ((state & ~ASPM_STATE_ALL) != 0) |
|---|
| 1214 | +static ssize_t aspm_attr_store_common(struct device *dev, |
|---|
| 1215 | + struct device_attribute *attr, |
|---|
| 1216 | + const char *buf, size_t len, u8 state) |
|---|
| 1217 | +{ |
|---|
| 1218 | + struct pci_dev *pdev = to_pci_dev(dev); |
|---|
| 1219 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1220 | + bool state_enable; |
|---|
| 1221 | + |
|---|
| 1222 | + if (strtobool(buf, &state_enable) < 0) |
|---|
| 1200 | 1223 | return -EINVAL; |
|---|
| 1201 | 1224 | |
|---|
| 1202 | 1225 | down_read(&pci_bus_sem); |
|---|
| 1203 | 1226 | mutex_lock(&aspm_lock); |
|---|
| 1204 | | - list_for_each_entry(link, &link_list, sibling) { |
|---|
| 1205 | | - if (link->root != root) |
|---|
| 1206 | | - continue; |
|---|
| 1207 | | - pcie_config_aspm_link(link, state); |
|---|
| 1227 | + |
|---|
| 1228 | + if (state_enable) { |
|---|
| 1229 | + link->aspm_disable &= ~state; |
|---|
| 1230 | + /* need to enable L1 for substates */ |
|---|
| 1231 | + if (state & ASPM_STATE_L1SS) |
|---|
| 1232 | + link->aspm_disable &= ~ASPM_STATE_L1; |
|---|
| 1233 | + } else { |
|---|
| 1234 | + link->aspm_disable |= state; |
|---|
| 1208 | 1235 | } |
|---|
| 1236 | + |
|---|
| 1237 | + pcie_config_aspm_link(link, policy_to_aspm_state(link)); |
|---|
| 1238 | + |
|---|
| 1209 | 1239 | mutex_unlock(&aspm_lock); |
|---|
| 1210 | 1240 | up_read(&pci_bus_sem); |
|---|
| 1211 | | - return n; |
|---|
| 1241 | + |
|---|
| 1242 | + return len; |
|---|
| 1212 | 1243 | } |
|---|
| 1213 | 1244 | |
|---|
| 1214 | | -static ssize_t clk_ctl_show(struct device *dev, |
|---|
| 1215 | | - struct device_attribute *attr, |
|---|
| 1216 | | - char *buf) |
|---|
| 1217 | | -{ |
|---|
| 1218 | | - struct pci_dev *pci_device = to_pci_dev(dev); |
|---|
| 1219 | | - struct pcie_link_state *link_state = pci_device->link_state; |
|---|
| 1245 | +#define ASPM_ATTR(_f, _s) \ |
|---|
| 1246 | +static ssize_t _f##_show(struct device *dev, \ |
|---|
| 1247 | + struct device_attribute *attr, char *buf) \ |
|---|
| 1248 | +{ return aspm_attr_show_common(dev, attr, buf, ASPM_STATE_##_s); } \ |
|---|
| 1249 | + \ |
|---|
| 1250 | +static ssize_t _f##_store(struct device *dev, \ |
|---|
| 1251 | + struct device_attribute *attr, \ |
|---|
| 1252 | + const char *buf, size_t len) \ |
|---|
| 1253 | +{ return aspm_attr_store_common(dev, attr, buf, len, ASPM_STATE_##_s); } |
|---|
| 1220 | 1254 | |
|---|
| 1221 | | - return sprintf(buf, "%d\n", link_state->clkpm_enabled); |
|---|
| 1222 | | -} |
|---|
| 1255 | +ASPM_ATTR(l0s_aspm, L0S) |
|---|
| 1256 | +ASPM_ATTR(l1_aspm, L1) |
|---|
| 1257 | +ASPM_ATTR(l1_1_aspm, L1_1) |
|---|
| 1258 | +ASPM_ATTR(l1_2_aspm, L1_2) |
|---|
| 1259 | +ASPM_ATTR(l1_1_pcipm, L1_1_PCIPM) |
|---|
| 1260 | +ASPM_ATTR(l1_2_pcipm, L1_2_PCIPM) |
|---|
| 1223 | 1261 | |
|---|
| 1224 | | -static ssize_t clk_ctl_store(struct device *dev, |
|---|
| 1225 | | - struct device_attribute *attr, |
|---|
| 1226 | | - const char *buf, |
|---|
| 1227 | | - size_t n) |
|---|
| 1262 | +static ssize_t clkpm_show(struct device *dev, |
|---|
| 1263 | + struct device_attribute *attr, char *buf) |
|---|
| 1228 | 1264 | { |
|---|
| 1229 | 1265 | struct pci_dev *pdev = to_pci_dev(dev); |
|---|
| 1230 | | - bool state; |
|---|
| 1266 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1231 | 1267 | |
|---|
| 1232 | | - if (strtobool(buf, &state)) |
|---|
| 1268 | + return sprintf(buf, "%d\n", link->clkpm_enabled); |
|---|
| 1269 | +} |
|---|
| 1270 | + |
|---|
| 1271 | +static ssize_t clkpm_store(struct device *dev, |
|---|
| 1272 | + struct device_attribute *attr, |
|---|
| 1273 | + const char *buf, size_t len) |
|---|
| 1274 | +{ |
|---|
| 1275 | + struct pci_dev *pdev = to_pci_dev(dev); |
|---|
| 1276 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1277 | + bool state_enable; |
|---|
| 1278 | + |
|---|
| 1279 | + if (strtobool(buf, &state_enable) < 0) |
|---|
| 1233 | 1280 | return -EINVAL; |
|---|
| 1234 | 1281 | |
|---|
| 1235 | 1282 | down_read(&pci_bus_sem); |
|---|
| 1236 | 1283 | mutex_lock(&aspm_lock); |
|---|
| 1237 | | - pcie_set_clkpm_nocheck(pdev->link_state, state); |
|---|
| 1284 | + |
|---|
| 1285 | + link->clkpm_disable = !state_enable; |
|---|
| 1286 | + pcie_set_clkpm(link, policy_to_clkpm_state(link)); |
|---|
| 1287 | + |
|---|
| 1238 | 1288 | mutex_unlock(&aspm_lock); |
|---|
| 1239 | 1289 | up_read(&pci_bus_sem); |
|---|
| 1240 | 1290 | |
|---|
| 1241 | | - return n; |
|---|
| 1291 | + return len; |
|---|
| 1242 | 1292 | } |
|---|
| 1243 | 1293 | |
|---|
| 1244 | | -static DEVICE_ATTR_RW(link_state); |
|---|
| 1245 | | -static DEVICE_ATTR_RW(clk_ctl); |
|---|
| 1294 | +static DEVICE_ATTR_RW(clkpm); |
|---|
| 1295 | +static DEVICE_ATTR_RW(l0s_aspm); |
|---|
| 1296 | +static DEVICE_ATTR_RW(l1_aspm); |
|---|
| 1297 | +static DEVICE_ATTR_RW(l1_1_aspm); |
|---|
| 1298 | +static DEVICE_ATTR_RW(l1_2_aspm); |
|---|
| 1299 | +static DEVICE_ATTR_RW(l1_1_pcipm); |
|---|
| 1300 | +static DEVICE_ATTR_RW(l1_2_pcipm); |
|---|
| 1246 | 1301 | |
|---|
| 1247 | | -static char power_group[] = "power"; |
|---|
| 1248 | | -void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev) |
|---|
| 1302 | +static struct attribute *aspm_ctrl_attrs[] = { |
|---|
| 1303 | + &dev_attr_clkpm.attr, |
|---|
| 1304 | + &dev_attr_l0s_aspm.attr, |
|---|
| 1305 | + &dev_attr_l1_aspm.attr, |
|---|
| 1306 | + &dev_attr_l1_1_aspm.attr, |
|---|
| 1307 | + &dev_attr_l1_2_aspm.attr, |
|---|
| 1308 | + &dev_attr_l1_1_pcipm.attr, |
|---|
| 1309 | + &dev_attr_l1_2_pcipm.attr, |
|---|
| 1310 | + NULL |
|---|
| 1311 | +}; |
|---|
| 1312 | + |
|---|
| 1313 | +static umode_t aspm_ctrl_attrs_are_visible(struct kobject *kobj, |
|---|
| 1314 | + struct attribute *a, int n) |
|---|
| 1249 | 1315 | { |
|---|
| 1250 | | - struct pcie_link_state *link_state = pdev->link_state; |
|---|
| 1316 | + struct device *dev = kobj_to_dev(kobj); |
|---|
| 1317 | + struct pci_dev *pdev = to_pci_dev(dev); |
|---|
| 1318 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1319 | + static const u8 aspm_state_map[] = { |
|---|
| 1320 | + ASPM_STATE_L0S, |
|---|
| 1321 | + ASPM_STATE_L1, |
|---|
| 1322 | + ASPM_STATE_L1_1, |
|---|
| 1323 | + ASPM_STATE_L1_2, |
|---|
| 1324 | + ASPM_STATE_L1_1_PCIPM, |
|---|
| 1325 | + ASPM_STATE_L1_2_PCIPM, |
|---|
| 1326 | + }; |
|---|
| 1251 | 1327 | |
|---|
| 1252 | | - if (!link_state) |
|---|
| 1253 | | - return; |
|---|
| 1328 | + if (aspm_disabled || !link) |
|---|
| 1329 | + return 0; |
|---|
| 1254 | 1330 | |
|---|
| 1255 | | - if (link_state->aspm_support) |
|---|
| 1256 | | - sysfs_add_file_to_group(&pdev->dev.kobj, |
|---|
| 1257 | | - &dev_attr_link_state.attr, power_group); |
|---|
| 1258 | | - if (link_state->clkpm_capable) |
|---|
| 1259 | | - sysfs_add_file_to_group(&pdev->dev.kobj, |
|---|
| 1260 | | - &dev_attr_clk_ctl.attr, power_group); |
|---|
| 1331 | + if (n == 0) |
|---|
| 1332 | + return link->clkpm_capable ? a->mode : 0; |
|---|
| 1333 | + |
|---|
| 1334 | + return link->aspm_capable & aspm_state_map[n - 1] ? a->mode : 0; |
|---|
| 1261 | 1335 | } |
|---|
| 1262 | 1336 | |
|---|
| 1263 | | -void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev) |
|---|
| 1264 | | -{ |
|---|
| 1265 | | - struct pcie_link_state *link_state = pdev->link_state; |
|---|
| 1266 | | - |
|---|
| 1267 | | - if (!link_state) |
|---|
| 1268 | | - return; |
|---|
| 1269 | | - |
|---|
| 1270 | | - if (link_state->aspm_support) |
|---|
| 1271 | | - sysfs_remove_file_from_group(&pdev->dev.kobj, |
|---|
| 1272 | | - &dev_attr_link_state.attr, power_group); |
|---|
| 1273 | | - if (link_state->clkpm_capable) |
|---|
| 1274 | | - sysfs_remove_file_from_group(&pdev->dev.kobj, |
|---|
| 1275 | | - &dev_attr_clk_ctl.attr, power_group); |
|---|
| 1276 | | -} |
|---|
| 1277 | | -#endif |
|---|
| 1337 | +const struct attribute_group aspm_ctrl_attr_group = { |
|---|
| 1338 | + .name = "link", |
|---|
| 1339 | + .attrs = aspm_ctrl_attrs, |
|---|
| 1340 | + .is_visible = aspm_ctrl_attrs_are_visible, |
|---|
| 1341 | +}; |
|---|
| 1278 | 1342 | |
|---|
| 1279 | 1343 | static int __init pcie_aspm_disable(char *str) |
|---|
| 1280 | 1344 | { |
|---|