| .. | .. |
|---|
| 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; |
|---|
| .. | .. |
|---|
| 203 | 192 | link->clkpm_disable = blacklist ? 1 : 0; |
|---|
| 204 | 193 | } |
|---|
| 205 | 194 | |
|---|
| 206 | | -static bool pcie_retrain_link(struct pcie_link_state *link) |
|---|
| 195 | +static int pcie_wait_for_retrain(struct pci_dev *pdev) |
|---|
| 196 | +{ |
|---|
| 197 | + unsigned long end_jiffies; |
|---|
| 198 | + u16 reg16; |
|---|
| 199 | + |
|---|
| 200 | + /* Wait for Link Training to be cleared by hardware */ |
|---|
| 201 | + end_jiffies = jiffies + LINK_RETRAIN_TIMEOUT; |
|---|
| 202 | + do { |
|---|
| 203 | + pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, ®16); |
|---|
| 204 | + if (!(reg16 & PCI_EXP_LNKSTA_LT)) |
|---|
| 205 | + return 0; |
|---|
| 206 | + msleep(1); |
|---|
| 207 | + } while (time_before(jiffies, end_jiffies)); |
|---|
| 208 | + |
|---|
| 209 | + return -ETIMEDOUT; |
|---|
| 210 | +} |
|---|
| 211 | + |
|---|
| 212 | +static int pcie_retrain_link(struct pcie_link_state *link) |
|---|
| 207 | 213 | { |
|---|
| 208 | 214 | struct pci_dev *parent = link->pdev; |
|---|
| 209 | | - unsigned long start_jiffies; |
|---|
| 215 | + int rc; |
|---|
| 210 | 216 | u16 reg16; |
|---|
| 217 | + |
|---|
| 218 | + /* |
|---|
| 219 | + * Ensure the updated LNKCTL parameters are used during link |
|---|
| 220 | + * training by checking that there is no ongoing link training to |
|---|
| 221 | + * avoid LTSSM race as recommended in Implementation Note at the |
|---|
| 222 | + * end of PCIe r6.0.1 sec 7.5.3.7. |
|---|
| 223 | + */ |
|---|
| 224 | + rc = pcie_wait_for_retrain(parent); |
|---|
| 225 | + if (rc) |
|---|
| 226 | + return rc; |
|---|
| 211 | 227 | |
|---|
| 212 | 228 | pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16); |
|---|
| 213 | 229 | reg16 |= PCI_EXP_LNKCTL_RL; |
|---|
| .. | .. |
|---|
| 222 | 238 | pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16); |
|---|
| 223 | 239 | } |
|---|
| 224 | 240 | |
|---|
| 225 | | - /* Wait for link training end. Break out after waiting for timeout */ |
|---|
| 226 | | - start_jiffies = jiffies; |
|---|
| 227 | | - for (;;) { |
|---|
| 228 | | - pcie_capability_read_word(parent, PCI_EXP_LNKSTA, ®16); |
|---|
| 229 | | - if (!(reg16 & PCI_EXP_LNKSTA_LT)) |
|---|
| 230 | | - break; |
|---|
| 231 | | - if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) |
|---|
| 232 | | - break; |
|---|
| 233 | | - msleep(1); |
|---|
| 234 | | - } |
|---|
| 235 | | - return !(reg16 & PCI_EXP_LNKSTA_LT); |
|---|
| 241 | + return pcie_wait_for_retrain(parent); |
|---|
| 236 | 242 | } |
|---|
| 237 | 243 | |
|---|
| 238 | 244 | /* |
|---|
| .. | .. |
|---|
| 243 | 249 | static void pcie_aspm_configure_common_clock(struct pcie_link_state *link) |
|---|
| 244 | 250 | { |
|---|
| 245 | 251 | int same_clock = 1; |
|---|
| 246 | | - u16 reg16, parent_reg, child_reg[8]; |
|---|
| 252 | + u16 reg16, ccc, parent_old_ccc, child_old_ccc[8]; |
|---|
| 247 | 253 | struct pci_dev *child, *parent = link->pdev; |
|---|
| 248 | 254 | struct pci_bus *linkbus = parent->subordinate; |
|---|
| 249 | 255 | /* |
|---|
| .. | .. |
|---|
| 265 | 271 | |
|---|
| 266 | 272 | /* Port might be already in common clock mode */ |
|---|
| 267 | 273 | pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16); |
|---|
| 274 | + parent_old_ccc = reg16 & PCI_EXP_LNKCTL_CCC; |
|---|
| 268 | 275 | if (same_clock && (reg16 & PCI_EXP_LNKCTL_CCC)) { |
|---|
| 269 | 276 | bool consistent = true; |
|---|
| 270 | 277 | |
|---|
| .. | .. |
|---|
| 278 | 285 | } |
|---|
| 279 | 286 | if (consistent) |
|---|
| 280 | 287 | return; |
|---|
| 281 | | - pci_warn(parent, "ASPM: current common clock configuration is broken, reconfiguring\n"); |
|---|
| 288 | + pci_info(parent, "ASPM: current common clock configuration is inconsistent, reconfiguring\n"); |
|---|
| 282 | 289 | } |
|---|
| 283 | 290 | |
|---|
| 291 | + ccc = same_clock ? PCI_EXP_LNKCTL_CCC : 0; |
|---|
| 284 | 292 | /* Configure downstream component, all functions */ |
|---|
| 285 | 293 | list_for_each_entry(child, &linkbus->devices, bus_list) { |
|---|
| 286 | 294 | pcie_capability_read_word(child, PCI_EXP_LNKCTL, ®16); |
|---|
| 287 | | - child_reg[PCI_FUNC(child->devfn)] = reg16; |
|---|
| 288 | | - if (same_clock) |
|---|
| 289 | | - reg16 |= PCI_EXP_LNKCTL_CCC; |
|---|
| 290 | | - else |
|---|
| 291 | | - reg16 &= ~PCI_EXP_LNKCTL_CCC; |
|---|
| 292 | | - pcie_capability_write_word(child, PCI_EXP_LNKCTL, reg16); |
|---|
| 295 | + child_old_ccc[PCI_FUNC(child->devfn)] = reg16 & PCI_EXP_LNKCTL_CCC; |
|---|
| 296 | + pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL, |
|---|
| 297 | + PCI_EXP_LNKCTL_CCC, ccc); |
|---|
| 293 | 298 | } |
|---|
| 294 | 299 | |
|---|
| 295 | 300 | /* Configure upstream component */ |
|---|
| 296 | | - pcie_capability_read_word(parent, PCI_EXP_LNKCTL, ®16); |
|---|
| 297 | | - parent_reg = reg16; |
|---|
| 298 | | - if (same_clock) |
|---|
| 299 | | - reg16 |= PCI_EXP_LNKCTL_CCC; |
|---|
| 300 | | - else |
|---|
| 301 | | - reg16 &= ~PCI_EXP_LNKCTL_CCC; |
|---|
| 302 | | - pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16); |
|---|
| 301 | + pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL, |
|---|
| 302 | + PCI_EXP_LNKCTL_CCC, ccc); |
|---|
| 303 | 303 | |
|---|
| 304 | | - if (pcie_retrain_link(link)) |
|---|
| 305 | | - return; |
|---|
| 304 | + if (pcie_retrain_link(link)) { |
|---|
| 306 | 305 | |
|---|
| 307 | | - /* Training failed. Restore common clock configurations */ |
|---|
| 308 | | - pci_err(parent, "ASPM: Could not configure common clock\n"); |
|---|
| 309 | | - list_for_each_entry(child, &linkbus->devices, bus_list) |
|---|
| 310 | | - pcie_capability_write_word(child, PCI_EXP_LNKCTL, |
|---|
| 311 | | - child_reg[PCI_FUNC(child->devfn)]); |
|---|
| 312 | | - pcie_capability_write_word(parent, PCI_EXP_LNKCTL, parent_reg); |
|---|
| 306 | + /* Training failed. Restore common clock configurations */ |
|---|
| 307 | + pci_err(parent, "ASPM: Could not configure common clock\n"); |
|---|
| 308 | + list_for_each_entry(child, &linkbus->devices, bus_list) |
|---|
| 309 | + pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL, |
|---|
| 310 | + PCI_EXP_LNKCTL_CCC, |
|---|
| 311 | + child_old_ccc[PCI_FUNC(child->devfn)]); |
|---|
| 312 | + pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL, |
|---|
| 313 | + PCI_EXP_LNKCTL_CCC, parent_old_ccc); |
|---|
| 314 | + } |
|---|
| 313 | 315 | } |
|---|
| 314 | 316 | |
|---|
| 315 | 317 | /* Convert L0s latency encoding to ns */ |
|---|
| 316 | | -static u32 calc_l0s_latency(u32 encoding) |
|---|
| 318 | +static u32 calc_l0s_latency(u32 lnkcap) |
|---|
| 317 | 319 | { |
|---|
| 320 | + u32 encoding = (lnkcap & PCI_EXP_LNKCAP_L0SEL) >> 12; |
|---|
| 321 | + |
|---|
| 318 | 322 | if (encoding == 0x7) |
|---|
| 319 | 323 | return (5 * 1000); /* > 4us */ |
|---|
| 320 | 324 | return (64 << encoding); |
|---|
| .. | .. |
|---|
| 329 | 333 | } |
|---|
| 330 | 334 | |
|---|
| 331 | 335 | /* Convert L1 latency encoding to ns */ |
|---|
| 332 | | -static u32 calc_l1_latency(u32 encoding) |
|---|
| 336 | +static u32 calc_l1_latency(u32 lnkcap) |
|---|
| 333 | 337 | { |
|---|
| 338 | + u32 encoding = (lnkcap & PCI_EXP_LNKCAP_L1EL) >> 15; |
|---|
| 339 | + |
|---|
| 334 | 340 | if (encoding == 0x7) |
|---|
| 335 | 341 | return (65 * 1000); /* > 64us */ |
|---|
| 336 | 342 | return (1000 << encoding); |
|---|
| .. | .. |
|---|
| 383 | 389 | *scale = 5; |
|---|
| 384 | 390 | *value = threshold_ns >> 25; |
|---|
| 385 | 391 | } |
|---|
| 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 | 392 | } |
|---|
| 439 | 393 | |
|---|
| 440 | 394 | static void pcie_aspm_check_latency(struct pci_dev *endpoint) |
|---|
| .. | .. |
|---|
| 498 | 452 | return NULL; |
|---|
| 499 | 453 | } |
|---|
| 500 | 454 | |
|---|
| 455 | +static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos, |
|---|
| 456 | + u32 clear, u32 set) |
|---|
| 457 | +{ |
|---|
| 458 | + u32 val; |
|---|
| 459 | + |
|---|
| 460 | + pci_read_config_dword(pdev, pos, &val); |
|---|
| 461 | + val &= ~clear; |
|---|
| 462 | + val |= set; |
|---|
| 463 | + pci_write_config_dword(pdev, pos, val); |
|---|
| 464 | +} |
|---|
| 465 | + |
|---|
| 501 | 466 | /* Calculate L1.2 PM substate timing parameters */ |
|---|
| 502 | 467 | static void aspm_calc_l1ss_info(struct pcie_link_state *link, |
|---|
| 503 | | - struct aspm_register_info *upreg, |
|---|
| 504 | | - struct aspm_register_info *dwreg) |
|---|
| 468 | + u32 parent_l1ss_cap, u32 child_l1ss_cap) |
|---|
| 505 | 469 | { |
|---|
| 470 | + struct pci_dev *child = link->downstream, *parent = link->pdev; |
|---|
| 506 | 471 | u32 val1, val2, scale1, scale2; |
|---|
| 507 | 472 | 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; |
|---|
| 473 | + u32 ctl1 = 0, ctl2 = 0; |
|---|
| 474 | + u32 pctl1, pctl2, cctl1, cctl2; |
|---|
| 475 | + u32 pl1_2_enables, cl1_2_enables; |
|---|
| 512 | 476 | |
|---|
| 513 | 477 | if (!(link->aspm_support & ASPM_STATE_L1_2_MASK)) |
|---|
| 514 | 478 | return; |
|---|
| 515 | 479 | |
|---|
| 516 | 480 | /* 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; |
|---|
| 481 | + val1 = (parent_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8; |
|---|
| 482 | + val2 = (child_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8; |
|---|
| 519 | 483 | t_common_mode = max(val1, val2); |
|---|
| 520 | 484 | |
|---|
| 521 | 485 | /* 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; |
|---|
| 486 | + val1 = (parent_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19; |
|---|
| 487 | + scale1 = (parent_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16; |
|---|
| 488 | + val2 = (child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19; |
|---|
| 489 | + scale2 = (child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16; |
|---|
| 526 | 490 | |
|---|
| 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); |
|---|
| 491 | + if (calc_l1ss_pwron(parent, scale1, val1) > |
|---|
| 492 | + calc_l1ss_pwron(child, scale2, val2)) { |
|---|
| 493 | + ctl2 |= scale1 | (val1 << 3); |
|---|
| 494 | + t_power_on = calc_l1ss_pwron(parent, scale1, val1); |
|---|
| 531 | 495 | } else { |
|---|
| 532 | | - link->l1ss.ctl2 |= scale2 | (val2 << 3); |
|---|
| 533 | | - t_power_on = calc_l1ss_pwron(link->downstream, scale2, val2); |
|---|
| 496 | + ctl2 |= scale2 | (val2 << 3); |
|---|
| 497 | + t_power_on = calc_l1ss_pwron(child, scale2, val2); |
|---|
| 534 | 498 | } |
|---|
| 535 | 499 | |
|---|
| 536 | 500 | /* |
|---|
| .. | .. |
|---|
| 545 | 509 | */ |
|---|
| 546 | 510 | l1_2_threshold = 2 + 4 + t_common_mode + t_power_on; |
|---|
| 547 | 511 | encode_l12_threshold(l1_2_threshold, &scale, &value); |
|---|
| 548 | | - link->l1ss.ctl1 |= t_common_mode << 8 | scale << 29 | value << 16; |
|---|
| 512 | + ctl1 |= t_common_mode << 8 | scale << 29 | value << 16; |
|---|
| 513 | + |
|---|
| 514 | + pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, &pctl1); |
|---|
| 515 | + pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, &pctl2); |
|---|
| 516 | + pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL1, &cctl1); |
|---|
| 517 | + pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL2, &cctl2); |
|---|
| 518 | + |
|---|
| 519 | + if (ctl1 == pctl1 && ctl1 == cctl1 && |
|---|
| 520 | + ctl2 == pctl2 && ctl2 == cctl2) |
|---|
| 521 | + return; |
|---|
| 522 | + |
|---|
| 523 | + /* Disable L1.2 while updating. See PCIe r5.0, sec 5.5.4, 7.8.3.3 */ |
|---|
| 524 | + pl1_2_enables = pctl1 & PCI_L1SS_CTL1_L1_2_MASK; |
|---|
| 525 | + cl1_2_enables = cctl1 & PCI_L1SS_CTL1_L1_2_MASK; |
|---|
| 526 | + |
|---|
| 527 | + if (pl1_2_enables || cl1_2_enables) { |
|---|
| 528 | + pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, |
|---|
| 529 | + PCI_L1SS_CTL1_L1_2_MASK, 0); |
|---|
| 530 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 531 | + PCI_L1SS_CTL1_L1_2_MASK, 0); |
|---|
| 532 | + } |
|---|
| 533 | + |
|---|
| 534 | + /* Program T_POWER_ON times in both ports */ |
|---|
| 535 | + pci_write_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, ctl2); |
|---|
| 536 | + pci_write_config_dword(child, child->l1ss + PCI_L1SS_CTL2, ctl2); |
|---|
| 537 | + |
|---|
| 538 | + /* Program Common_Mode_Restore_Time in upstream device */ |
|---|
| 539 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 540 | + PCI_L1SS_CTL1_CM_RESTORE_TIME, ctl1); |
|---|
| 541 | + |
|---|
| 542 | + /* Program LTR_L1.2_THRESHOLD time in both ports */ |
|---|
| 543 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 544 | + PCI_L1SS_CTL1_LTR_L12_TH_VALUE | |
|---|
| 545 | + PCI_L1SS_CTL1_LTR_L12_TH_SCALE, ctl1); |
|---|
| 546 | + pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, |
|---|
| 547 | + PCI_L1SS_CTL1_LTR_L12_TH_VALUE | |
|---|
| 548 | + PCI_L1SS_CTL1_LTR_L12_TH_SCALE, ctl1); |
|---|
| 549 | + |
|---|
| 550 | + if (pl1_2_enables || cl1_2_enables) { |
|---|
| 551 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, 0, |
|---|
| 552 | + pl1_2_enables); |
|---|
| 553 | + pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, 0, |
|---|
| 554 | + cl1_2_enables); |
|---|
| 555 | + } |
|---|
| 549 | 556 | } |
|---|
| 550 | 557 | |
|---|
| 551 | 558 | static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist) |
|---|
| 552 | 559 | { |
|---|
| 553 | 560 | struct pci_dev *child = link->downstream, *parent = link->pdev; |
|---|
| 561 | + u32 parent_lnkcap, child_lnkcap; |
|---|
| 562 | + u16 parent_lnkctl, child_lnkctl; |
|---|
| 563 | + u32 parent_l1ss_cap, child_l1ss_cap; |
|---|
| 564 | + u32 parent_l1ss_ctl1 = 0, child_l1ss_ctl1 = 0; |
|---|
| 554 | 565 | struct pci_bus *linkbus = parent->subordinate; |
|---|
| 555 | | - struct aspm_register_info upreg, dwreg; |
|---|
| 556 | 566 | |
|---|
| 557 | 567 | if (blacklist) { |
|---|
| 558 | 568 | /* Set enabled/disable so that we will disable ASPM later */ |
|---|
| .. | .. |
|---|
| 561 | 571 | return; |
|---|
| 562 | 572 | } |
|---|
| 563 | 573 | |
|---|
| 564 | | - /* Get upstream/downstream components' register state */ |
|---|
| 565 | | - pcie_get_aspm_reg(parent, &upreg); |
|---|
| 566 | | - pcie_get_aspm_reg(child, &dwreg); |
|---|
| 567 | | - |
|---|
| 568 | 574 | /* |
|---|
| 569 | 575 | * If ASPM not supported, don't mess with the clocks and link, |
|---|
| 570 | 576 | * bail out now. |
|---|
| 571 | 577 | */ |
|---|
| 572 | | - if (!(upreg.support & dwreg.support)) |
|---|
| 578 | + pcie_capability_read_dword(parent, PCI_EXP_LNKCAP, &parent_lnkcap); |
|---|
| 579 | + pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &child_lnkcap); |
|---|
| 580 | + if (!(parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPMS)) |
|---|
| 573 | 581 | return; |
|---|
| 574 | 582 | |
|---|
| 575 | 583 | /* Configure common clock before checking latencies */ |
|---|
| 576 | 584 | pcie_aspm_configure_common_clock(link); |
|---|
| 577 | 585 | |
|---|
| 578 | 586 | /* |
|---|
| 579 | | - * Re-read upstream/downstream components' register state |
|---|
| 580 | | - * after clock configuration |
|---|
| 587 | + * Re-read upstream/downstream components' register state after |
|---|
| 588 | + * clock configuration. L0s & L1 exit latencies in the otherwise |
|---|
| 589 | + * read-only Link Capabilities may change depending on common clock |
|---|
| 590 | + * configuration (PCIe r5.0, sec 7.5.3.6). |
|---|
| 581 | 591 | */ |
|---|
| 582 | | - pcie_get_aspm_reg(parent, &upreg); |
|---|
| 583 | | - pcie_get_aspm_reg(child, &dwreg); |
|---|
| 592 | + pcie_capability_read_dword(parent, PCI_EXP_LNKCAP, &parent_lnkcap); |
|---|
| 593 | + pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &child_lnkcap); |
|---|
| 594 | + pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &parent_lnkctl); |
|---|
| 595 | + pcie_capability_read_word(child, PCI_EXP_LNKCTL, &child_lnkctl); |
|---|
| 584 | 596 | |
|---|
| 585 | 597 | /* |
|---|
| 586 | 598 | * Setup L0s state |
|---|
| .. | .. |
|---|
| 589 | 601 | * given link unless components on both sides of the link each |
|---|
| 590 | 602 | * support L0s. |
|---|
| 591 | 603 | */ |
|---|
| 592 | | - if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S) |
|---|
| 604 | + if (parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPM_L0S) |
|---|
| 593 | 605 | link->aspm_support |= ASPM_STATE_L0S; |
|---|
| 594 | | - if (dwreg.enabled & PCIE_LINK_STATE_L0S) |
|---|
| 606 | + |
|---|
| 607 | + if (child_lnkctl & PCI_EXP_LNKCTL_ASPM_L0S) |
|---|
| 595 | 608 | link->aspm_enabled |= ASPM_STATE_L0S_UP; |
|---|
| 596 | | - if (upreg.enabled & PCIE_LINK_STATE_L0S) |
|---|
| 609 | + if (parent_lnkctl & PCI_EXP_LNKCTL_ASPM_L0S) |
|---|
| 597 | 610 | 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); |
|---|
| 611 | + link->latency_up.l0s = calc_l0s_latency(parent_lnkcap); |
|---|
| 612 | + link->latency_dw.l0s = calc_l0s_latency(child_lnkcap); |
|---|
| 600 | 613 | |
|---|
| 601 | 614 | /* Setup L1 state */ |
|---|
| 602 | | - if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1) |
|---|
| 615 | + if (parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPM_L1) |
|---|
| 603 | 616 | link->aspm_support |= ASPM_STATE_L1; |
|---|
| 604 | | - if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1) |
|---|
| 617 | + |
|---|
| 618 | + if (parent_lnkctl & child_lnkctl & PCI_EXP_LNKCTL_ASPM_L1) |
|---|
| 605 | 619 | 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); |
|---|
| 620 | + link->latency_up.l1 = calc_l1_latency(parent_lnkcap); |
|---|
| 621 | + link->latency_dw.l1 = calc_l1_latency(child_lnkcap); |
|---|
| 608 | 622 | |
|---|
| 609 | 623 | /* Setup L1 substate */ |
|---|
| 610 | | - if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1) |
|---|
| 624 | + pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CAP, |
|---|
| 625 | + &parent_l1ss_cap); |
|---|
| 626 | + pci_read_config_dword(child, child->l1ss + PCI_L1SS_CAP, |
|---|
| 627 | + &child_l1ss_cap); |
|---|
| 628 | + |
|---|
| 629 | + if (!(parent_l1ss_cap & PCI_L1SS_CAP_L1_PM_SS)) |
|---|
| 630 | + parent_l1ss_cap = 0; |
|---|
| 631 | + if (!(child_l1ss_cap & PCI_L1SS_CAP_L1_PM_SS)) |
|---|
| 632 | + child_l1ss_cap = 0; |
|---|
| 633 | + |
|---|
| 634 | + /* |
|---|
| 635 | + * If we don't have LTR for the entire path from the Root Complex |
|---|
| 636 | + * to this device, we can't use ASPM L1.2 because it relies on the |
|---|
| 637 | + * LTR_L1.2_THRESHOLD. See PCIe r4.0, secs 5.5.4, 6.18. |
|---|
| 638 | + */ |
|---|
| 639 | + if (!child->ltr_path) |
|---|
| 640 | + child_l1ss_cap &= ~PCI_L1SS_CAP_ASPM_L1_2; |
|---|
| 641 | + |
|---|
| 642 | + if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1) |
|---|
| 611 | 643 | link->aspm_support |= ASPM_STATE_L1_1; |
|---|
| 612 | | - if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2) |
|---|
| 644 | + if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2) |
|---|
| 613 | 645 | link->aspm_support |= ASPM_STATE_L1_2; |
|---|
| 614 | | - if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1) |
|---|
| 646 | + if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1) |
|---|
| 615 | 647 | link->aspm_support |= ASPM_STATE_L1_1_PCIPM; |
|---|
| 616 | | - if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2) |
|---|
| 648 | + if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2) |
|---|
| 617 | 649 | link->aspm_support |= ASPM_STATE_L1_2_PCIPM; |
|---|
| 618 | 650 | |
|---|
| 619 | | - if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1) |
|---|
| 651 | + if (parent_l1ss_cap) |
|---|
| 652 | + pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 653 | + &parent_l1ss_ctl1); |
|---|
| 654 | + if (child_l1ss_cap) |
|---|
| 655 | + pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL1, |
|---|
| 656 | + &child_l1ss_ctl1); |
|---|
| 657 | + |
|---|
| 658 | + if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1) |
|---|
| 620 | 659 | link->aspm_enabled |= ASPM_STATE_L1_1; |
|---|
| 621 | | - if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2) |
|---|
| 660 | + if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2) |
|---|
| 622 | 661 | link->aspm_enabled |= ASPM_STATE_L1_2; |
|---|
| 623 | | - if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1) |
|---|
| 662 | + if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1) |
|---|
| 624 | 663 | link->aspm_enabled |= ASPM_STATE_L1_1_PCIPM; |
|---|
| 625 | | - if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2) |
|---|
| 664 | + if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2) |
|---|
| 626 | 665 | link->aspm_enabled |= ASPM_STATE_L1_2_PCIPM; |
|---|
| 627 | 666 | |
|---|
| 628 | 667 | if (link->aspm_support & ASPM_STATE_L1SS) |
|---|
| 629 | | - aspm_calc_l1ss_info(link, &upreg, &dwreg); |
|---|
| 668 | + aspm_calc_l1ss_info(link, parent_l1ss_cap, child_l1ss_cap); |
|---|
| 630 | 669 | |
|---|
| 631 | 670 | /* Save default state */ |
|---|
| 632 | 671 | link->aspm_default = link->aspm_enabled; |
|---|
| .. | .. |
|---|
| 656 | 695 | } |
|---|
| 657 | 696 | } |
|---|
| 658 | 697 | |
|---|
| 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 | 698 | /* Configure the ASPM L1 substates */ |
|---|
| 671 | 699 | static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state) |
|---|
| 672 | 700 | { |
|---|
| 673 | 701 | u32 val, enable_req; |
|---|
| 674 | 702 | 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 | 703 | |
|---|
| 678 | 704 | enable_req = (link->aspm_enabled ^ state) & state; |
|---|
| 679 | 705 | |
|---|
| .. | .. |
|---|
| 691 | 717 | */ |
|---|
| 692 | 718 | |
|---|
| 693 | 719 | /* Disable all L1 substates */ |
|---|
| 694 | | - pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 720 | + pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, |
|---|
| 695 | 721 | PCI_L1SS_CTL1_L1SS_MASK, 0); |
|---|
| 696 | | - pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 722 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 697 | 723 | PCI_L1SS_CTL1_L1SS_MASK, 0); |
|---|
| 698 | 724 | /* |
|---|
| 699 | 725 | * If needed, disable L1, and it gets enabled later |
|---|
| .. | .. |
|---|
| 704 | 730 | PCI_EXP_LNKCTL_ASPM_L1, 0); |
|---|
| 705 | 731 | pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL, |
|---|
| 706 | 732 | 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 | 733 | } |
|---|
| 732 | 734 | |
|---|
| 733 | 735 | val = 0; |
|---|
| .. | .. |
|---|
| 741 | 743 | val |= PCI_L1SS_CTL1_PCIPM_L1_2; |
|---|
| 742 | 744 | |
|---|
| 743 | 745 | /* Enable what we need to enable */ |
|---|
| 744 | | - pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 746 | + pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, |
|---|
| 745 | 747 | PCI_L1SS_CTL1_L1SS_MASK, val); |
|---|
| 746 | | - pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1, |
|---|
| 748 | + pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, |
|---|
| 747 | 749 | PCI_L1SS_CTL1_L1SS_MASK, val); |
|---|
| 748 | 750 | } |
|---|
| 749 | 751 | |
|---|
| .. | .. |
|---|
| 862 | 864 | return NULL; |
|---|
| 863 | 865 | |
|---|
| 864 | 866 | INIT_LIST_HEAD(&link->sibling); |
|---|
| 865 | | - INIT_LIST_HEAD(&link->children); |
|---|
| 866 | | - INIT_LIST_HEAD(&link->link); |
|---|
| 867 | 867 | link->pdev = pdev; |
|---|
| 868 | 868 | link->downstream = pci_function_0(pdev->subordinate); |
|---|
| 869 | 869 | |
|---|
| .. | .. |
|---|
| 889 | 889 | |
|---|
| 890 | 890 | link->parent = parent; |
|---|
| 891 | 891 | link->root = link->parent->root; |
|---|
| 892 | | - list_add(&link->link, &parent->children); |
|---|
| 893 | 892 | } |
|---|
| 894 | 893 | |
|---|
| 895 | 894 | list_add(&link->sibling, &link_list); |
|---|
| 896 | 895 | pdev->link_state = link; |
|---|
| 897 | 896 | return link; |
|---|
| 897 | +} |
|---|
| 898 | + |
|---|
| 899 | +static void pcie_aspm_update_sysfs_visibility(struct pci_dev *pdev) |
|---|
| 900 | +{ |
|---|
| 901 | + struct pci_dev *child; |
|---|
| 902 | + |
|---|
| 903 | + list_for_each_entry(child, &pdev->subordinate->devices, bus_list) |
|---|
| 904 | + sysfs_update_group(&child->dev.kobj, &aspm_ctrl_attr_group); |
|---|
| 898 | 905 | } |
|---|
| 899 | 906 | |
|---|
| 900 | 907 | /* |
|---|
| .. | .. |
|---|
| 915 | 922 | |
|---|
| 916 | 923 | /* |
|---|
| 917 | 924 | * 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. |
|---|
| 925 | + * end of a Link, so there's nothing to do unless this device is |
|---|
| 926 | + * downstream port. |
|---|
| 920 | 927 | */ |
|---|
| 921 | | - if (!pdev->has_secondary_link) |
|---|
| 928 | + if (!pcie_downstream_port(pdev)) |
|---|
| 922 | 929 | return; |
|---|
| 923 | 930 | |
|---|
| 924 | 931 | /* VIA has a strange chipset, root port is under a bridge */ |
|---|
| .. | .. |
|---|
| 957 | 964 | pcie_config_aspm_path(link); |
|---|
| 958 | 965 | pcie_set_clkpm(link, policy_to_clkpm_state(link)); |
|---|
| 959 | 966 | } |
|---|
| 967 | + |
|---|
| 968 | + pcie_aspm_update_sysfs_visibility(pdev); |
|---|
| 960 | 969 | |
|---|
| 961 | 970 | unlock: |
|---|
| 962 | 971 | mutex_unlock(&aspm_lock); |
|---|
| .. | .. |
|---|
| 999 | 1008 | |
|---|
| 1000 | 1009 | down_read(&pci_bus_sem); |
|---|
| 1001 | 1010 | mutex_lock(&aspm_lock); |
|---|
| 1002 | | - /* |
|---|
| 1003 | | - * All PCIe functions are in one slot, remove one function will remove |
|---|
| 1004 | | - * the whole slot, so just wait until we are the last function left. |
|---|
| 1005 | | - */ |
|---|
| 1006 | | - if (!list_empty(&parent->subordinate->devices)) |
|---|
| 1007 | | - goto out; |
|---|
| 1008 | 1011 | |
|---|
| 1009 | 1012 | link = parent->link_state; |
|---|
| 1010 | 1013 | root = link->root; |
|---|
| 1011 | 1014 | parent_link = link->parent; |
|---|
| 1012 | 1015 | |
|---|
| 1013 | | - /* All functions are removed, so just disable ASPM for the link */ |
|---|
| 1016 | + /* |
|---|
| 1017 | + * link->downstream is a pointer to the pci_dev of function 0. If |
|---|
| 1018 | + * we remove that function, the pci_dev is about to be deallocated, |
|---|
| 1019 | + * so we can't use link->downstream again. Free the link state to |
|---|
| 1020 | + * avoid this. |
|---|
| 1021 | + * |
|---|
| 1022 | + * If we're removing a non-0 function, it's possible we could |
|---|
| 1023 | + * retain the link state, but PCIe r6.0, sec 7.5.3.7, recommends |
|---|
| 1024 | + * programming the same ASPM Control value for all functions of |
|---|
| 1025 | + * multi-function devices, so disable ASPM for all of them. |
|---|
| 1026 | + */ |
|---|
| 1014 | 1027 | pcie_config_aspm_link(link, 0); |
|---|
| 1015 | 1028 | list_del(&link->sibling); |
|---|
| 1016 | | - list_del(&link->link); |
|---|
| 1017 | | - /* Clock PM is for endpoint device */ |
|---|
| 1018 | 1029 | free_link_state(link); |
|---|
| 1019 | 1030 | |
|---|
| 1020 | 1031 | /* Recheck latencies and configure upstream links */ |
|---|
| .. | .. |
|---|
| 1022 | 1033 | pcie_update_aspm_capable(root); |
|---|
| 1023 | 1034 | pcie_config_aspm_path(parent_link); |
|---|
| 1024 | 1035 | } |
|---|
| 1025 | | -out: |
|---|
| 1036 | + |
|---|
| 1026 | 1037 | mutex_unlock(&aspm_lock); |
|---|
| 1027 | 1038 | up_read(&pci_bus_sem); |
|---|
| 1028 | 1039 | } |
|---|
| .. | .. |
|---|
| 1065 | 1076 | up_read(&pci_bus_sem); |
|---|
| 1066 | 1077 | } |
|---|
| 1067 | 1078 | |
|---|
| 1068 | | -static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem) |
|---|
| 1079 | +static struct pcie_link_state *pcie_aspm_get_link(struct pci_dev *pdev) |
|---|
| 1069 | 1080 | { |
|---|
| 1070 | | - struct pci_dev *parent = pdev->bus->self; |
|---|
| 1071 | | - struct pcie_link_state *link; |
|---|
| 1081 | + struct pci_dev *bridge; |
|---|
| 1072 | 1082 | |
|---|
| 1073 | 1083 | if (!pci_is_pcie(pdev)) |
|---|
| 1074 | | - return; |
|---|
| 1084 | + return NULL; |
|---|
| 1075 | 1085 | |
|---|
| 1076 | | - if (pdev->has_secondary_link) |
|---|
| 1077 | | - parent = pdev; |
|---|
| 1078 | | - if (!parent || !parent->link_state) |
|---|
| 1079 | | - return; |
|---|
| 1086 | + bridge = pci_upstream_bridge(pdev); |
|---|
| 1087 | + if (!bridge || !pci_is_pcie(bridge)) |
|---|
| 1088 | + return NULL; |
|---|
| 1080 | 1089 | |
|---|
| 1090 | + return bridge->link_state; |
|---|
| 1091 | +} |
|---|
| 1092 | + |
|---|
| 1093 | +static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem) |
|---|
| 1094 | +{ |
|---|
| 1095 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1096 | + |
|---|
| 1097 | + if (!link) |
|---|
| 1098 | + return -EINVAL; |
|---|
| 1081 | 1099 | /* |
|---|
| 1082 | 1100 | * A driver requested that ASPM be disabled on this device, but |
|---|
| 1083 | 1101 | * if we don't have permission to manage ASPM (e.g., on ACPI |
|---|
| .. | .. |
|---|
| 1088 | 1106 | */ |
|---|
| 1089 | 1107 | if (aspm_disabled) { |
|---|
| 1090 | 1108 | pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n"); |
|---|
| 1091 | | - return; |
|---|
| 1109 | + return -EPERM; |
|---|
| 1092 | 1110 | } |
|---|
| 1093 | 1111 | |
|---|
| 1094 | 1112 | if (sem) |
|---|
| 1095 | 1113 | down_read(&pci_bus_sem); |
|---|
| 1096 | 1114 | mutex_lock(&aspm_lock); |
|---|
| 1097 | | - link = parent->link_state; |
|---|
| 1098 | 1115 | if (state & PCIE_LINK_STATE_L0S) |
|---|
| 1099 | 1116 | link->aspm_disable |= ASPM_STATE_L0S; |
|---|
| 1100 | 1117 | if (state & PCIE_LINK_STATE_L1) |
|---|
| 1101 | | - link->aspm_disable |= ASPM_STATE_L1; |
|---|
| 1118 | + /* L1 PM substates require L1 */ |
|---|
| 1119 | + link->aspm_disable |= ASPM_STATE_L1 | ASPM_STATE_L1SS; |
|---|
| 1120 | + if (state & PCIE_LINK_STATE_L1_1) |
|---|
| 1121 | + link->aspm_disable |= ASPM_STATE_L1_1; |
|---|
| 1122 | + if (state & PCIE_LINK_STATE_L1_2) |
|---|
| 1123 | + link->aspm_disable |= ASPM_STATE_L1_2; |
|---|
| 1124 | + if (state & PCIE_LINK_STATE_L1_1_PCIPM) |
|---|
| 1125 | + link->aspm_disable |= ASPM_STATE_L1_1_PCIPM; |
|---|
| 1126 | + if (state & PCIE_LINK_STATE_L1_2_PCIPM) |
|---|
| 1127 | + link->aspm_disable |= ASPM_STATE_L1_2_PCIPM; |
|---|
| 1102 | 1128 | pcie_config_aspm_link(link, policy_to_aspm_state(link)); |
|---|
| 1103 | 1129 | |
|---|
| 1104 | 1130 | if (state & PCIE_LINK_STATE_CLKPM) |
|---|
| .. | .. |
|---|
| 1107 | 1133 | mutex_unlock(&aspm_lock); |
|---|
| 1108 | 1134 | if (sem) |
|---|
| 1109 | 1135 | up_read(&pci_bus_sem); |
|---|
| 1136 | + |
|---|
| 1137 | + return 0; |
|---|
| 1110 | 1138 | } |
|---|
| 1111 | 1139 | |
|---|
| 1112 | | -void pci_disable_link_state_locked(struct pci_dev *pdev, int state) |
|---|
| 1140 | +int pci_disable_link_state_locked(struct pci_dev *pdev, int state) |
|---|
| 1113 | 1141 | { |
|---|
| 1114 | | - __pci_disable_link_state(pdev, state, false); |
|---|
| 1142 | + return __pci_disable_link_state(pdev, state, false); |
|---|
| 1115 | 1143 | } |
|---|
| 1116 | 1144 | EXPORT_SYMBOL(pci_disable_link_state_locked); |
|---|
| 1117 | 1145 | |
|---|
| .. | .. |
|---|
| 1119 | 1147 | * pci_disable_link_state - Disable device's link state, so the link will |
|---|
| 1120 | 1148 | * never enter specific states. Note that if the BIOS didn't grant ASPM |
|---|
| 1121 | 1149 | * control to the OS, this does nothing because we can't touch the LNKCTL |
|---|
| 1122 | | - * register. |
|---|
| 1150 | + * register. Returns 0 or a negative errno. |
|---|
| 1123 | 1151 | * |
|---|
| 1124 | 1152 | * @pdev: PCI device |
|---|
| 1125 | 1153 | * @state: ASPM link state to disable |
|---|
| 1126 | 1154 | */ |
|---|
| 1127 | | -void pci_disable_link_state(struct pci_dev *pdev, int state) |
|---|
| 1155 | +int pci_disable_link_state(struct pci_dev *pdev, int state) |
|---|
| 1128 | 1156 | { |
|---|
| 1129 | | - __pci_disable_link_state(pdev, state, true); |
|---|
| 1157 | + return __pci_disable_link_state(pdev, state, true); |
|---|
| 1130 | 1158 | } |
|---|
| 1131 | 1159 | EXPORT_SYMBOL(pci_disable_link_state); |
|---|
| 1132 | 1160 | |
|---|
| .. | .. |
|---|
| 1171 | 1199 | module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy, |
|---|
| 1172 | 1200 | NULL, 0644); |
|---|
| 1173 | 1201 | |
|---|
| 1174 | | -#ifdef CONFIG_PCIEASPM_DEBUG |
|---|
| 1175 | | -static ssize_t link_state_show(struct device *dev, |
|---|
| 1176 | | - struct device_attribute *attr, |
|---|
| 1177 | | - char *buf) |
|---|
| 1202 | +/** |
|---|
| 1203 | + * pcie_aspm_enabled - Check if PCIe ASPM has been enabled for a device. |
|---|
| 1204 | + * @pdev: Target device. |
|---|
| 1205 | + * |
|---|
| 1206 | + * Relies on the upstream bridge's link_state being valid. The link_state |
|---|
| 1207 | + * is deallocated only when the last child of the bridge (i.e., @pdev or a |
|---|
| 1208 | + * sibling) is removed, and the caller should be holding a reference to |
|---|
| 1209 | + * @pdev, so this should be safe. |
|---|
| 1210 | + */ |
|---|
| 1211 | +bool pcie_aspm_enabled(struct pci_dev *pdev) |
|---|
| 1178 | 1212 | { |
|---|
| 1179 | | - struct pci_dev *pci_device = to_pci_dev(dev); |
|---|
| 1180 | | - struct pcie_link_state *link_state = pci_device->link_state; |
|---|
| 1213 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1181 | 1214 | |
|---|
| 1182 | | - return sprintf(buf, "%d\n", link_state->aspm_enabled); |
|---|
| 1215 | + if (!link) |
|---|
| 1216 | + return false; |
|---|
| 1217 | + |
|---|
| 1218 | + return link->aspm_enabled; |
|---|
| 1183 | 1219 | } |
|---|
| 1220 | +EXPORT_SYMBOL_GPL(pcie_aspm_enabled); |
|---|
| 1184 | 1221 | |
|---|
| 1185 | | -static ssize_t link_state_store(struct device *dev, |
|---|
| 1186 | | - struct device_attribute *attr, |
|---|
| 1187 | | - const char *buf, |
|---|
| 1188 | | - size_t n) |
|---|
| 1222 | +static ssize_t aspm_attr_show_common(struct device *dev, |
|---|
| 1223 | + struct device_attribute *attr, |
|---|
| 1224 | + char *buf, u8 state) |
|---|
| 1189 | 1225 | { |
|---|
| 1190 | 1226 | struct pci_dev *pdev = to_pci_dev(dev); |
|---|
| 1191 | | - struct pcie_link_state *link, *root = pdev->link_state->root; |
|---|
| 1192 | | - u32 state; |
|---|
| 1227 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1193 | 1228 | |
|---|
| 1194 | | - if (aspm_disabled) |
|---|
| 1195 | | - return -EPERM; |
|---|
| 1229 | + return sprintf(buf, "%d\n", (link->aspm_enabled & state) ? 1 : 0); |
|---|
| 1230 | +} |
|---|
| 1196 | 1231 | |
|---|
| 1197 | | - if (kstrtouint(buf, 10, &state)) |
|---|
| 1198 | | - return -EINVAL; |
|---|
| 1199 | | - if ((state & ~ASPM_STATE_ALL) != 0) |
|---|
| 1232 | +static ssize_t aspm_attr_store_common(struct device *dev, |
|---|
| 1233 | + struct device_attribute *attr, |
|---|
| 1234 | + const char *buf, size_t len, u8 state) |
|---|
| 1235 | +{ |
|---|
| 1236 | + struct pci_dev *pdev = to_pci_dev(dev); |
|---|
| 1237 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1238 | + bool state_enable; |
|---|
| 1239 | + |
|---|
| 1240 | + if (strtobool(buf, &state_enable) < 0) |
|---|
| 1200 | 1241 | return -EINVAL; |
|---|
| 1201 | 1242 | |
|---|
| 1202 | 1243 | down_read(&pci_bus_sem); |
|---|
| 1203 | 1244 | 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); |
|---|
| 1245 | + |
|---|
| 1246 | + if (state_enable) { |
|---|
| 1247 | + link->aspm_disable &= ~state; |
|---|
| 1248 | + /* need to enable L1 for substates */ |
|---|
| 1249 | + if (state & ASPM_STATE_L1SS) |
|---|
| 1250 | + link->aspm_disable &= ~ASPM_STATE_L1; |
|---|
| 1251 | + } else { |
|---|
| 1252 | + link->aspm_disable |= state; |
|---|
| 1208 | 1253 | } |
|---|
| 1254 | + |
|---|
| 1255 | + pcie_config_aspm_link(link, policy_to_aspm_state(link)); |
|---|
| 1256 | + |
|---|
| 1209 | 1257 | mutex_unlock(&aspm_lock); |
|---|
| 1210 | 1258 | up_read(&pci_bus_sem); |
|---|
| 1211 | | - return n; |
|---|
| 1259 | + |
|---|
| 1260 | + return len; |
|---|
| 1212 | 1261 | } |
|---|
| 1213 | 1262 | |
|---|
| 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; |
|---|
| 1263 | +#define ASPM_ATTR(_f, _s) \ |
|---|
| 1264 | +static ssize_t _f##_show(struct device *dev, \ |
|---|
| 1265 | + struct device_attribute *attr, char *buf) \ |
|---|
| 1266 | +{ return aspm_attr_show_common(dev, attr, buf, ASPM_STATE_##_s); } \ |
|---|
| 1267 | + \ |
|---|
| 1268 | +static ssize_t _f##_store(struct device *dev, \ |
|---|
| 1269 | + struct device_attribute *attr, \ |
|---|
| 1270 | + const char *buf, size_t len) \ |
|---|
| 1271 | +{ return aspm_attr_store_common(dev, attr, buf, len, ASPM_STATE_##_s); } |
|---|
| 1220 | 1272 | |
|---|
| 1221 | | - return sprintf(buf, "%d\n", link_state->clkpm_enabled); |
|---|
| 1222 | | -} |
|---|
| 1273 | +ASPM_ATTR(l0s_aspm, L0S) |
|---|
| 1274 | +ASPM_ATTR(l1_aspm, L1) |
|---|
| 1275 | +ASPM_ATTR(l1_1_aspm, L1_1) |
|---|
| 1276 | +ASPM_ATTR(l1_2_aspm, L1_2) |
|---|
| 1277 | +ASPM_ATTR(l1_1_pcipm, L1_1_PCIPM) |
|---|
| 1278 | +ASPM_ATTR(l1_2_pcipm, L1_2_PCIPM) |
|---|
| 1223 | 1279 | |
|---|
| 1224 | | -static ssize_t clk_ctl_store(struct device *dev, |
|---|
| 1225 | | - struct device_attribute *attr, |
|---|
| 1226 | | - const char *buf, |
|---|
| 1227 | | - size_t n) |
|---|
| 1280 | +static ssize_t clkpm_show(struct device *dev, |
|---|
| 1281 | + struct device_attribute *attr, char *buf) |
|---|
| 1228 | 1282 | { |
|---|
| 1229 | 1283 | struct pci_dev *pdev = to_pci_dev(dev); |
|---|
| 1230 | | - bool state; |
|---|
| 1284 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1231 | 1285 | |
|---|
| 1232 | | - if (strtobool(buf, &state)) |
|---|
| 1286 | + return sprintf(buf, "%d\n", link->clkpm_enabled); |
|---|
| 1287 | +} |
|---|
| 1288 | + |
|---|
| 1289 | +static ssize_t clkpm_store(struct device *dev, |
|---|
| 1290 | + struct device_attribute *attr, |
|---|
| 1291 | + const char *buf, size_t len) |
|---|
| 1292 | +{ |
|---|
| 1293 | + struct pci_dev *pdev = to_pci_dev(dev); |
|---|
| 1294 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1295 | + bool state_enable; |
|---|
| 1296 | + |
|---|
| 1297 | + if (strtobool(buf, &state_enable) < 0) |
|---|
| 1233 | 1298 | return -EINVAL; |
|---|
| 1234 | 1299 | |
|---|
| 1235 | 1300 | down_read(&pci_bus_sem); |
|---|
| 1236 | 1301 | mutex_lock(&aspm_lock); |
|---|
| 1237 | | - pcie_set_clkpm_nocheck(pdev->link_state, state); |
|---|
| 1302 | + |
|---|
| 1303 | + link->clkpm_disable = !state_enable; |
|---|
| 1304 | + pcie_set_clkpm(link, policy_to_clkpm_state(link)); |
|---|
| 1305 | + |
|---|
| 1238 | 1306 | mutex_unlock(&aspm_lock); |
|---|
| 1239 | 1307 | up_read(&pci_bus_sem); |
|---|
| 1240 | 1308 | |
|---|
| 1241 | | - return n; |
|---|
| 1309 | + return len; |
|---|
| 1242 | 1310 | } |
|---|
| 1243 | 1311 | |
|---|
| 1244 | | -static DEVICE_ATTR_RW(link_state); |
|---|
| 1245 | | -static DEVICE_ATTR_RW(clk_ctl); |
|---|
| 1312 | +static DEVICE_ATTR_RW(clkpm); |
|---|
| 1313 | +static DEVICE_ATTR_RW(l0s_aspm); |
|---|
| 1314 | +static DEVICE_ATTR_RW(l1_aspm); |
|---|
| 1315 | +static DEVICE_ATTR_RW(l1_1_aspm); |
|---|
| 1316 | +static DEVICE_ATTR_RW(l1_2_aspm); |
|---|
| 1317 | +static DEVICE_ATTR_RW(l1_1_pcipm); |
|---|
| 1318 | +static DEVICE_ATTR_RW(l1_2_pcipm); |
|---|
| 1246 | 1319 | |
|---|
| 1247 | | -static char power_group[] = "power"; |
|---|
| 1248 | | -void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev) |
|---|
| 1320 | +static struct attribute *aspm_ctrl_attrs[] = { |
|---|
| 1321 | + &dev_attr_clkpm.attr, |
|---|
| 1322 | + &dev_attr_l0s_aspm.attr, |
|---|
| 1323 | + &dev_attr_l1_aspm.attr, |
|---|
| 1324 | + &dev_attr_l1_1_aspm.attr, |
|---|
| 1325 | + &dev_attr_l1_2_aspm.attr, |
|---|
| 1326 | + &dev_attr_l1_1_pcipm.attr, |
|---|
| 1327 | + &dev_attr_l1_2_pcipm.attr, |
|---|
| 1328 | + NULL |
|---|
| 1329 | +}; |
|---|
| 1330 | + |
|---|
| 1331 | +static umode_t aspm_ctrl_attrs_are_visible(struct kobject *kobj, |
|---|
| 1332 | + struct attribute *a, int n) |
|---|
| 1249 | 1333 | { |
|---|
| 1250 | | - struct pcie_link_state *link_state = pdev->link_state; |
|---|
| 1334 | + struct device *dev = kobj_to_dev(kobj); |
|---|
| 1335 | + struct pci_dev *pdev = to_pci_dev(dev); |
|---|
| 1336 | + struct pcie_link_state *link = pcie_aspm_get_link(pdev); |
|---|
| 1337 | + static const u8 aspm_state_map[] = { |
|---|
| 1338 | + ASPM_STATE_L0S, |
|---|
| 1339 | + ASPM_STATE_L1, |
|---|
| 1340 | + ASPM_STATE_L1_1, |
|---|
| 1341 | + ASPM_STATE_L1_2, |
|---|
| 1342 | + ASPM_STATE_L1_1_PCIPM, |
|---|
| 1343 | + ASPM_STATE_L1_2_PCIPM, |
|---|
| 1344 | + }; |
|---|
| 1251 | 1345 | |
|---|
| 1252 | | - if (!link_state) |
|---|
| 1253 | | - return; |
|---|
| 1346 | + if (aspm_disabled || !link) |
|---|
| 1347 | + return 0; |
|---|
| 1254 | 1348 | |
|---|
| 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); |
|---|
| 1349 | + if (n == 0) |
|---|
| 1350 | + return link->clkpm_capable ? a->mode : 0; |
|---|
| 1351 | + |
|---|
| 1352 | + return link->aspm_capable & aspm_state_map[n - 1] ? a->mode : 0; |
|---|
| 1261 | 1353 | } |
|---|
| 1262 | 1354 | |
|---|
| 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 |
|---|
| 1355 | +const struct attribute_group aspm_ctrl_attr_group = { |
|---|
| 1356 | + .name = "link", |
|---|
| 1357 | + .attrs = aspm_ctrl_attrs, |
|---|
| 1358 | + .is_visible = aspm_ctrl_attrs_are_visible, |
|---|
| 1359 | +}; |
|---|
| 1278 | 1360 | |
|---|
| 1279 | 1361 | static int __init pcie_aspm_disable(char *str) |
|---|
| 1280 | 1362 | { |
|---|