Debian Codegraph @

drivers/pci/pcie/aspm.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Enable PCIe link L0s/L1 state and Clock Power Management
4 *
5 * Copyright (C) 2007 Intel
6 * Copyright (C) Zhang Yanmin ([email protected])
7 * Copyright (C) Shaohua Li ([email protected])
8 */
9
10 #include <linux/bitfield.h>
11 #include <linux/bits.h>
12 #include <linux/build_bug.h>
13 #include <linux/kernel.h>
14 #include <linux/limits.h>
15 #include <linux/math.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/of.h>
19 #include <linux/pci.h>
20 #include <linux/pci_regs.h>
21 #include <linux/errno.h>
22 #include <linux/pm.h>
23 #include <linux/init.h>
24 #include <linux/printk.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27
28 #include "../pci.h"
29
30 void pci_save_ltr_state(struct pci_dev *dev)
31 {
32 int ltr;
33 struct pci_cap_saved_state *save_state;
34 u32 *cap;
35
36 if (!pci_is_pcie(dev))
37 return;
38
39 ltr = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_LTR);
40 if (!ltr)
41 return;
42
43 save_state = pci_find_saved_ext_cap(dev, PCI_EXT_CAP_ID_LTR);
44 if (!save_state) {
45 pci_err(dev, "no suspend buffer for LTR; ASPM issues possible after resume\n");
46 return;
47 }
48
49 /* Some broken devices only support dword access to LTR */
50 cap = &save_state->cap.data[0];
51 pci_read_config_dword(dev, ltr + PCI_LTR_MAX_SNOOP_LAT, cap);
52 }
53
54 void pci_restore_ltr_state(struct pci_dev *dev)
55 {
56 struct pci_cap_saved_state *save_state;
57 int ltr;
58 u32 *cap;
59
60 save_state = pci_find_saved_ext_cap(dev, PCI_EXT_CAP_ID_LTR);
61 ltr = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_LTR);
62 if (!save_state || !ltr)
63 return;
64
65 /* Some broken devices only support dword access to LTR */
66 cap = &save_state->cap.data[0];
67 pci_write_config_dword(dev, ltr + PCI_LTR_MAX_SNOOP_LAT, *cap);
68 }
69
70 void pci_configure_aspm_l1ss(struct pci_dev *pdev)
71 {
72 int rc;
73
74 pdev->l1ss = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS);
75
76 rc = pci_add_ext_cap_save_buffer(pdev, PCI_EXT_CAP_ID_L1SS,
77 2 * sizeof(u32));
78 if (rc)
79 pci_err(pdev, "unable to allocate ASPM L1SS save buffer (%pe)\n",
80 ERR_PTR(rc));
81 }
82
83 void pci_save_aspm_l1ss_state(struct pci_dev *pdev)
84 {
85 struct pci_dev *parent = pdev->bus->self;
86 struct pci_cap_saved_state *save_state;
87 u32 *cap;
88
89 /*
90 * If this is a Downstream Port, we never restore the L1SS state
91 * directly; we only restore it when we restore the state of the
92 * Upstream Port below it.
93 */
94 if (pcie_downstream_port(pdev) || !parent)
95 return;
96
97 if (!pdev->l1ss || !parent->l1ss)
98 return;
99
100 /*
101 * Save L1 substate configuration. The ASPM L0s/L1 configuration
102 * in PCI_EXP_LNKCTL_ASPMC is saved by pci_save_pcie_state().
103 */
104 save_state = pci_find_saved_ext_cap(pdev, PCI_EXT_CAP_ID_L1SS);
105 if (!save_state)
106 return;
107
108 cap = &save_state->cap.data[0];
109 pci_read_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL2, cap++);
110 pci_read_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL1, cap++);
111
112 /*
113 * Save parent's L1 substate configuration so we have it for
114 * pci_restore_aspm_l1ss_state(pdev) to restore.
115 */
116 save_state = pci_find_saved_ext_cap(parent, PCI_EXT_CAP_ID_L1SS);
117 if (!save_state)
118 return;
119
120 cap = &save_state->cap.data[0];
121 pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, cap++);
122 pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, cap++);
123 }
124
125 void pci_restore_aspm_l1ss_state(struct pci_dev *pdev)
126 {
127 struct pci_cap_saved_state *pl_save_state, *cl_save_state;
128 struct pci_dev *parent = pdev->bus->self;
129 u32 *cap, pl_ctl1, pl_ctl2, pl_l1_2_enable;
130 u32 cl_ctl1, cl_ctl2, cl_l1_2_enable;
131 u16 clnkctl, plnkctl;
132
133 /*
134 * In case BIOS enabled L1.2 when resuming, we need to disable it first
135 * on the downstream component before the upstream. So, don't attempt to
136 * restore either until we are at the downstream component.
137 */
138 if (pcie_downstream_port(pdev) || !parent)
139 return;
140
141 if (!pdev->l1ss || !parent->l1ss)
142 return;
143
144 cl_save_state = pci_find_saved_ext_cap(pdev, PCI_EXT_CAP_ID_L1SS);
145 pl_save_state = pci_find_saved_ext_cap(parent, PCI_EXT_CAP_ID_L1SS);
146 if (!cl_save_state || !pl_save_state)
147 return;
148
149 cap = &cl_save_state->cap.data[0];
150 cl_ctl2 = *cap++;
151 cl_ctl1 = *cap;
152 cap = &pl_save_state->cap.data[0];
153 pl_ctl2 = *cap++;
154 pl_ctl1 = *cap;
155
156 /* Make sure L0s/L1 are disabled before updating L1SS config */
157 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &clnkctl);
158 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &plnkctl);
159 if (FIELD_GET(PCI_EXP_LNKCTL_ASPMC, clnkctl) ||
160 FIELD_GET(PCI_EXP_LNKCTL_ASPMC, plnkctl)) {
161 pcie_capability_write_word(pdev, PCI_EXP_LNKCTL,
162 clnkctl & ~PCI_EXP_LNKCTL_ASPMC);
163 pcie_capability_write_word(parent, PCI_EXP_LNKCTL,
164 plnkctl & ~PCI_EXP_LNKCTL_ASPMC);
165 }
166
167 /*
168 * Disable L1.2 on this downstream endpoint device first, followed
169 * by the upstream
170 */
171 pci_clear_and_set_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL1,
172 PCI_L1SS_CTL1_L1_2_MASK, 0);
173 pci_clear_and_set_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
174 PCI_L1SS_CTL1_L1_2_MASK, 0);
175
176 /*
177 * In addition, Common_Mode_Restore_Time and LTR_L1.2_THRESHOLD
178 * in PCI_L1SS_CTL1 must be programmed *before* setting the L1.2
179 * enable bits, even though they're all in PCI_L1SS_CTL1.
180 */
181 pl_l1_2_enable = pl_ctl1 & PCI_L1SS_CTL1_L1_2_MASK;
182 pl_ctl1 &= ~PCI_L1SS_CTL1_L1_2_MASK;
183 cl_l1_2_enable = cl_ctl1 & PCI_L1SS_CTL1_L1_2_MASK;
184 cl_ctl1 &= ~PCI_L1SS_CTL1_L1_2_MASK;
185
186 /* Write back without enables first (above we cleared them in ctl1) */
187 pci_write_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, pl_ctl2);
188 pci_write_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL2, cl_ctl2);
189 pci_write_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, pl_ctl1);
190 pci_write_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL1, cl_ctl1);
191
192 /* Then write back the enables */
193 if (pl_l1_2_enable || cl_l1_2_enable) {
194 pci_write_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
195 pl_ctl1 | pl_l1_2_enable);
196 pci_write_config_dword(pdev, pdev->l1ss + PCI_L1SS_CTL1,
197 cl_ctl1 | cl_l1_2_enable);
198 }
199
200 /* Restore L0s/L1 if they were enabled */
201 if (FIELD_GET(PCI_EXP_LNKCTL_ASPMC, clnkctl) ||
202 FIELD_GET(PCI_EXP_LNKCTL_ASPMC, plnkctl)) {
203 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, plnkctl);
204 pcie_capability_write_word(pdev, PCI_EXP_LNKCTL, clnkctl);
205 }
206 }
207
208 #ifdef CONFIG_PCIEASPM
209
210 #ifdef MODULE_PARAM_PREFIX
211 #undef MODULE_PARAM_PREFIX
212 #endif
213 #define MODULE_PARAM_PREFIX "pcie_aspm."
214
215 /* Note: these are not register definitions */
216 #define PCIE_LINK_STATE_L0S_UP BIT(0) /* Upstream direction L0s state */
217 #define PCIE_LINK_STATE_L0S_DW BIT(1) /* Downstream direction L0s state */
218 static_assert(PCIE_LINK_STATE_L0S == (PCIE_LINK_STATE_L0S_UP | PCIE_LINK_STATE_L0S_DW));
219
220 #define PCIE_LINK_STATE_L1_SS_PCIPM (PCIE_LINK_STATE_L1_1_PCIPM |\
221 PCIE_LINK_STATE_L1_2_PCIPM)
222 #define PCIE_LINK_STATE_L1_2_MASK (PCIE_LINK_STATE_L1_2 |\
223 PCIE_LINK_STATE_L1_2_PCIPM)
224 #define PCIE_LINK_STATE_L1SS (PCIE_LINK_STATE_L1_1 |\
225 PCIE_LINK_STATE_L1_1_PCIPM |\
226 PCIE_LINK_STATE_L1_2_MASK)
227
228 struct pcie_link_state {
229 struct pci_dev *pdev; /* Upstream component of the Link */
230 struct pci_dev *downstream; /* Downstream component, function 0 */
231 struct pcie_link_state *root; /* pointer to the root port link */
232 struct pcie_link_state *parent; /* pointer to the parent Link state */
233 struct list_head sibling; /* node in link_list */
234
235 /* ASPM state */
236 u32 aspm_support:7; /* Supported ASPM state */
237 u32 aspm_enabled:7; /* Enabled ASPM state */
238 u32 aspm_capable:7; /* Capable ASPM state with latency */
239 u32 aspm_default:7; /* Default ASPM state by BIOS or
240 override */
241 u32 aspm_disable:7; /* Disabled ASPM state */
242
243 /* Clock PM state */
244 u32 clkpm_capable:1; /* Clock PM capable? */
245 u32 clkpm_enabled:1; /* Current Clock PM state */
246 u32 clkpm_default:1; /* Default Clock PM state by BIOS */
247 u32 clkpm_disable:1; /* Clock PM disabled */
248 };
249
250 static bool aspm_disabled, aspm_force;
251 static bool aspm_support_enabled = true;
252 static DEFINE_MUTEX(aspm_lock);
253 static LIST_HEAD(link_list);
254
255 #define POLICY_DEFAULT 0 /* BIOS default setting */
256 #define POLICY_PERFORMANCE 1 /* high performance */
257 #define POLICY_POWERSAVE 2 /* high power saving */
258 #define POLICY_POWER_SUPERSAVE 3 /* possibly even more power saving */
259
260 #ifdef CONFIG_PCIEASPM_PERFORMANCE
261 static int aspm_policy = POLICY_PERFORMANCE;
262 #elif defined CONFIG_PCIEASPM_POWERSAVE
263 static int aspm_policy = POLICY_POWERSAVE;
264 #elif defined CONFIG_PCIEASPM_POWER_SUPERSAVE
265 static int aspm_policy = POLICY_POWER_SUPERSAVE;
266 #else
267 static int aspm_policy;
268 #endif
269
270 static const char *policy_str[] = {
271 [POLICY_DEFAULT] = "default",
272 [POLICY_PERFORMANCE] = "performance",
273 [POLICY_POWERSAVE] = "powersave",
274 [POLICY_POWER_SUPERSAVE] = "powersupersave"
275 };
276
277 /*
278 * The L1 PM substate capability is only implemented in function 0 in a
279 * multi function device.
280 */
281 static struct pci_dev *pci_function_0(struct pci_bus *linkbus)
282 {
283 struct pci_dev *child;
284
285 list_for_each_entry(child, &linkbus->devices, bus_list)
286 if (PCI_FUNC(child->devfn) == 0)
287 return child;
288 return NULL;
289 }
290
291 static int policy_to_aspm_state(struct pcie_link_state *link)
292 {
293 switch (aspm_policy) {
294 case POLICY_PERFORMANCE:
295 /* Disable ASPM and Clock PM */
296 return 0;
297 case POLICY_POWERSAVE:
298 /* Enable ASPM L0s/L1 */
299 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
300 case POLICY_POWER_SUPERSAVE:
301 /* Enable Everything */
302 return PCIE_LINK_STATE_ASPM_ALL;
303 case POLICY_DEFAULT:
304 return link->aspm_default;
305 }
306 return 0;
307 }
308
309 static int policy_to_clkpm_state(struct pcie_link_state *link)
310 {
311 switch (aspm_policy) {
312 case POLICY_PERFORMANCE:
313 /* Disable ASPM and Clock PM */
314 return 0;
315 case POLICY_POWERSAVE:
316 case POLICY_POWER_SUPERSAVE:
317 /* Enable Clock PM */
318 return 1;
319 case POLICY_DEFAULT:
320 return link->clkpm_default;
321 }
322 return 0;
323 }
324
325 static void pci_update_aspm_saved_state(struct pci_dev *dev)
326 {
327 struct pci_cap_saved_state *save_state;
328 u16 *cap, lnkctl, aspm_ctl;
329
330 save_state = pci_find_saved_cap(dev, PCI_CAP_ID_EXP);
331 if (!save_state)
332 return;
333
334 pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &lnkctl);
335
336 /*
337 * Update ASPM and CLKREQ bits of LNKCTL in save_state. We only
338 * write PCI_EXP_LNKCTL_CCC during enumeration, so it shouldn't
339 * change after being captured in save_state.
340 */
341 aspm_ctl = lnkctl & (PCI_EXP_LNKCTL_ASPMC | PCI_EXP_LNKCTL_CLKREQ_EN);
342 lnkctl &= ~(PCI_EXP_LNKCTL_ASPMC | PCI_EXP_LNKCTL_CLKREQ_EN);
343
344 /* Depends on pci_save_pcie_state(): cap[1] is LNKCTL */
345 cap = (u16 *)&save_state->cap.data[0];
346 cap[1] = lnkctl | aspm_ctl;
347 }
348
349 static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
350 {
351 struct pci_dev *child;
352 struct pci_bus *linkbus = link->pdev->subordinate;
353 u32 val = enable ? PCI_EXP_LNKCTL_CLKREQ_EN : 0;
354
355 list_for_each_entry(child, &linkbus->devices, bus_list) {
356 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
357 PCI_EXP_LNKCTL_CLKREQ_EN,
358 val);
359 pci_update_aspm_saved_state(child);
360 }
361 link->clkpm_enabled = !!enable;
362 }
363
364 static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
365 {
366 /*
367 * Don't enable Clock PM if the link is not Clock PM capable
368 * or Clock PM is disabled
369 */
370 if (!link->clkpm_capable || link->clkpm_disable)
371 enable = 0;
372 /* Need nothing if the specified equals to current state */
373 if (link->clkpm_enabled == enable)
374 return;
375 pcie_set_clkpm_nocheck(link, enable);
376 }
377
378 static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
379 {
380 int capable = 1, enabled = 1;
381 u32 reg32;
382 u16 reg16;
383 struct pci_dev *child;
384 struct pci_bus *linkbus = link->pdev->subordinate;
385
386 /* All functions should have the same cap and state, take the worst */
387 list_for_each_entry(child, &linkbus->devices, bus_list) {
388 pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &reg32);
389 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
390 capable = 0;
391 enabled = 0;
392 break;
393 }
394 pcie_capability_read_word(child, PCI_EXP_LNKCTL, &reg16);
395 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
396 enabled = 0;
397 }
398 link->clkpm_enabled = enabled;
399 link->clkpm_default = enabled;
400 link->clkpm_capable = capable;
401 link->clkpm_disable = blacklist ? 1 : 0;
402 }
403
404 /*
405 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
406 * could use common clock. If they are, configure them to use the
407 * common clock. That will reduce the ASPM state exit latency.
408 */
409 static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
410 {
411 int same_clock = 1;
412 u16 reg16, ccc, parent_old_ccc, child_old_ccc[8];
413 struct pci_dev *child, *parent = link->pdev;
414 struct pci_bus *linkbus = parent->subordinate;
415 /*
416 * All functions of a slot should have the same Slot Clock
417 * Configuration, so just check one function
418 */
419 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
420 BUG_ON(!pci_is_pcie(child));
421
422 /* Check downstream component if bit Slot Clock Configuration is 1 */
423 pcie_capability_read_word(child, PCI_EXP_LNKSTA, &reg16);
424 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
425 same_clock = 0;
426
427 /* Check upstream component if bit Slot Clock Configuration is 1 */
428 pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &reg16);
429 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
430 same_clock = 0;
431
432 /* Port might be already in common clock mode */
433 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &reg16);
434 parent_old_ccc = reg16 & PCI_EXP_LNKCTL_CCC;
435 if (same_clock && (reg16 & PCI_EXP_LNKCTL_CCC)) {
436 bool consistent = true;
437
438 list_for_each_entry(child, &linkbus->devices, bus_list) {
439 pcie_capability_read_word(child, PCI_EXP_LNKCTL,
440 &reg16);
441 if (!(reg16 & PCI_EXP_LNKCTL_CCC)) {
442 consistent = false;
443 break;
444 }
445 }
446 if (consistent)
447 return;
448 pci_info(parent, "ASPM: current common clock configuration is inconsistent, reconfiguring\n");
449 }
450
451 ccc = same_clock ? PCI_EXP_LNKCTL_CCC : 0;
452 /* Configure downstream component, all functions */
453 list_for_each_entry(child, &linkbus->devices, bus_list) {
454 pcie_capability_read_word(child, PCI_EXP_LNKCTL, &reg16);
455 child_old_ccc[PCI_FUNC(child->devfn)] = reg16 & PCI_EXP_LNKCTL_CCC;
456 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
457 PCI_EXP_LNKCTL_CCC, ccc);
458 }
459
460 /* Configure upstream component */
461 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
462 PCI_EXP_LNKCTL_CCC, ccc);
463
464 if (pcie_retrain_link(link->pdev, true)) {
465
466 /* Training failed. Restore common clock configurations */
467 pci_err(parent, "ASPM: Could not configure common clock\n");
468 list_for_each_entry(child, &linkbus->devices, bus_list)
469 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
470 PCI_EXP_LNKCTL_CCC,
471 child_old_ccc[PCI_FUNC(child->devfn)]);
472 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
473 PCI_EXP_LNKCTL_CCC, parent_old_ccc);
474 }
475 }
476
477 /* Convert L0s latency encoding to ns */
478 static u32 calc_l0s_latency(u32 lnkcap)
479 {
480 u32 encoding = FIELD_GET(PCI_EXP_LNKCAP_L0SEL, lnkcap);
481
482 if (encoding == 0x7)
483 return 5 * NSEC_PER_USEC; /* > 4us */
484 return (64 << encoding);
485 }
486
487 /* Convert L0s acceptable latency encoding to ns */
488 static u32 calc_l0s_acceptable(u32 encoding)
489 {
490 if (encoding == 0x7)
491 return U32_MAX;
492 return (64 << encoding);
493 }
494
495 /* Convert L1 latency encoding to ns */
496 static u32 calc_l1_latency(u32 lnkcap)
497 {
498 u32 encoding = FIELD_GET(PCI_EXP_LNKCAP_L1EL, lnkcap);
499
500 if (encoding == 0x7)
501 return 65 * NSEC_PER_USEC; /* > 64us */
502 return NSEC_PER_USEC << encoding;
503 }
504
505 /* Convert L1 acceptable latency encoding to ns */
506 static u32 calc_l1_acceptable(u32 encoding)
507 {
508 if (encoding == 0x7)
509 return U32_MAX;
510 return NSEC_PER_USEC << encoding;
511 }
512
513 /* Convert L1SS T_pwr encoding to usec */
514 static u32 calc_l12_pwron(struct pci_dev *pdev, u32 scale, u32 val)
515 {
516 switch (scale) {
517 case 0:
518 return val * 2;
519 case 1:
520 return val * 10;
521 case 2:
522 return val * 100;
523 }
524 pci_err(pdev, "%s: Invalid T_PwrOn scale: %u\n", __func__, scale);
525 return 0;
526 }
527
528 /*
529 * Encode an LTR_L1.2_THRESHOLD value for the L1 PM Substates Control 1
530 * register. Ports enter L1.2 when the most recent LTR value is greater
531 * than or equal to LTR_L1.2_THRESHOLD, so we round up to make sure we
532 * don't enter L1.2 too aggressively.
533 *
534 * See PCIe r6.0, sec 5.5.1, 6.18, 7.8.3.3.
535 */
536 static void encode_l12_threshold(u32 threshold_us, u32 *scale, u32 *value)
537 {
538 u64 threshold_ns = (u64)threshold_us * NSEC_PER_USEC;
539
540 /*
541 * LTR_L1.2_THRESHOLD_Value ("value") is a 10-bit field with max
542 * value of 0x3ff.
543 */
544 if (threshold_ns <= 1 * FIELD_MAX(PCI_L1SS_CTL1_LTR_L12_TH_VALUE)) {
545 *scale = 0; /* Value times 1ns */
546 *value = threshold_ns;
547 } else if (threshold_ns <= 32 * FIELD_MAX(PCI_L1SS_CTL1_LTR_L12_TH_VALUE)) {
548 *scale = 1; /* Value times 32ns */
549 *value = roundup(threshold_ns, 32) / 32;
550 } else if (threshold_ns <= 1024 * FIELD_MAX(PCI_L1SS_CTL1_LTR_L12_TH_VALUE)) {
551 *scale = 2; /* Value times 1024ns */
552 *value = roundup(threshold_ns, 1024) / 1024;
553 } else if (threshold_ns <= 32768 * FIELD_MAX(PCI_L1SS_CTL1_LTR_L12_TH_VALUE)) {
554 *scale = 3; /* Value times 32768ns */
555 *value = roundup(threshold_ns, 32768) / 32768;
556 } else if (threshold_ns <= 1048576 * FIELD_MAX(PCI_L1SS_CTL1_LTR_L12_TH_VALUE)) {
557 *scale = 4; /* Value times 1048576ns */
558 *value = roundup(threshold_ns, 1048576) / 1048576;
559 } else if (threshold_ns <= (u64)33554432 * FIELD_MAX(PCI_L1SS_CTL1_LTR_L12_TH_VALUE)) {
560 *scale = 5; /* Value times 33554432ns */
561 *value = roundup(threshold_ns, 33554432) / 33554432;
562 } else {
563 *scale = 5;
564 *value = FIELD_MAX(PCI_L1SS_CTL1_LTR_L12_TH_VALUE);
565 }
566 }
567
568 static void pcie_aspm_check_latency(struct pci_dev *endpoint)
569 {
570 u32 latency, encoding, lnkcap_up, lnkcap_dw;
571 u32 l1_switch_latency = 0, latency_up_l0s;
572 u32 latency_up_l1, latency_dw_l0s, latency_dw_l1;
573 u32 acceptable_l0s, acceptable_l1;
574 struct pcie_link_state *link;
575
576 /* Device not in D0 doesn't need latency check */
577 if ((endpoint->current_state != PCI_D0) &&
578 (endpoint->current_state != PCI_UNKNOWN))
579 return;
580
581 link = endpoint->bus->self->link_state;
582
583 /* Calculate endpoint L0s acceptable latency */
584 encoding = FIELD_GET(PCI_EXP_DEVCAP_L0S, endpoint->devcap);
585 acceptable_l0s = calc_l0s_acceptable(encoding);
586
587 /* Calculate endpoint L1 acceptable latency */
588 encoding = FIELD_GET(PCI_EXP_DEVCAP_L1, endpoint->devcap);
589 acceptable_l1 = calc_l1_acceptable(encoding);
590
591 while (link) {
592 struct pci_dev *dev = pci_function_0(link->pdev->subordinate);
593
594 /* Read direction exit latencies */
595 pcie_capability_read_dword(link->pdev, PCI_EXP_LNKCAP,
596 &lnkcap_up);
597 pcie_capability_read_dword(dev, PCI_EXP_LNKCAP,
598 &lnkcap_dw);
599 latency_up_l0s = calc_l0s_latency(lnkcap_up);
600 latency_up_l1 = calc_l1_latency(lnkcap_up);
601 latency_dw_l0s = calc_l0s_latency(lnkcap_dw);
602 latency_dw_l1 = calc_l1_latency(lnkcap_dw);
603
604 /* Check upstream direction L0s latency */
605 if ((link->aspm_capable & PCIE_LINK_STATE_L0S_UP) &&
606 (latency_up_l0s > acceptable_l0s))
607 link->aspm_capable &= ~PCIE_LINK_STATE_L0S_UP;
608
609 /* Check downstream direction L0s latency */
610 if ((link->aspm_capable & PCIE_LINK_STATE_L0S_DW) &&
611 (latency_dw_l0s > acceptable_l0s))
612 link->aspm_capable &= ~PCIE_LINK_STATE_L0S_DW;
613 /*
614 * Check L1 latency.
615 * Every switch on the path to root complex need 1
616 * more microsecond for L1. Spec doesn't mention L0s.
617 *
618 * The exit latencies for L1 substates are not advertised
619 * by a device. Since the spec also doesn't mention a way
620 * to determine max latencies introduced by enabling L1
621 * substates on the components, it is not clear how to do
622 * a L1 substate exit latency check. We assume that the
623 * L1 exit latencies advertised by a device include L1
624 * substate latencies (and hence do not do any check).
625 */
626 latency = max_t(u32, latency_up_l1, latency_dw_l1);
627 if ((link->aspm_capable & PCIE_LINK_STATE_L1) &&
628 (latency + l1_switch_latency > acceptable_l1))
629 link->aspm_capable &= ~PCIE_LINK_STATE_L1;
630 l1_switch_latency += NSEC_PER_USEC;
631
632 link = link->parent;
633 }
634 }
635
636 /* Calculate L1.2 PM substate timing parameters */
637 static void aspm_calc_l12_info(struct pcie_link_state *link,
638 u32 parent_l1ss_cap, u32 child_l1ss_cap)
639 {
640 struct pci_dev *child = link->downstream, *parent = link->pdev;
641 u32 val1, val2, scale1, scale2;
642 u32 t_common_mode, t_power_on, l1_2_threshold, scale, value;
643 u32 ctl1 = 0, ctl2 = 0;
644 u32 pctl1, pctl2, cctl1, cctl2;
645 u32 pl1_2_enables, cl1_2_enables;
646
647 /* Choose the greater of the two Port Common_Mode_Restore_Times */
648 val1 = FIELD_GET(PCI_L1SS_CAP_CM_RESTORE_TIME, parent_l1ss_cap);
649 val2 = FIELD_GET(PCI_L1SS_CAP_CM_RESTORE_TIME, child_l1ss_cap);
650 t_common_mode = max(val1, val2);
651
652 /* Choose the greater of the two Port T_POWER_ON times */
653 val1 = FIELD_GET(PCI_L1SS_CAP_P_PWR_ON_VALUE, parent_l1ss_cap);
654 scale1 = FIELD_GET(PCI_L1SS_CAP_P_PWR_ON_SCALE, parent_l1ss_cap);
655 val2 = FIELD_GET(PCI_L1SS_CAP_P_PWR_ON_VALUE, child_l1ss_cap);
656 scale2 = FIELD_GET(PCI_L1SS_CAP_P_PWR_ON_SCALE, child_l1ss_cap);
657
658 if (calc_l12_pwron(parent, scale1, val1) >
659 calc_l12_pwron(child, scale2, val2)) {
660 ctl2 |= FIELD_PREP(PCI_L1SS_CTL2_T_PWR_ON_SCALE, scale1) |
661 FIELD_PREP(PCI_L1SS_CTL2_T_PWR_ON_VALUE, val1);
662 t_power_on = calc_l12_pwron(parent, scale1, val1);
663 } else {
664 ctl2 |= FIELD_PREP(PCI_L1SS_CTL2_T_PWR_ON_SCALE, scale2) |
665 FIELD_PREP(PCI_L1SS_CTL2_T_PWR_ON_VALUE, val2);
666 t_power_on = calc_l12_pwron(child, scale2, val2);
667 }
668
669 /*
670 * Set LTR_L1.2_THRESHOLD to the time required to transition the
671 * Link from L0 to L1.2 and back to L0 so we enter L1.2 only if
672 * downstream devices report (via LTR) that they can tolerate at
673 * least that much latency.
674 *
675 * Based on PCIe r3.1, sec 5.5.3.3.1, Figures 5-16 and 5-17, and
676 * Table 5-11. T(POWER_OFF) is at most 2us and T(L1.2) is at
677 * least 4us.
678 */
679 l1_2_threshold = 2 + 4 + t_common_mode + t_power_on;
680 encode_l12_threshold(l1_2_threshold, &scale, &value);
681 ctl1 |= FIELD_PREP(PCI_L1SS_CTL1_CM_RESTORE_TIME, t_common_mode) |
682 FIELD_PREP(PCI_L1SS_CTL1_LTR_L12_TH_VALUE, value) |
683 FIELD_PREP(PCI_L1SS_CTL1_LTR_L12_TH_SCALE, scale);
684
685 /* Some broken devices only support dword access to L1 SS */
686 pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, &pctl1);
687 pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, &pctl2);
688 pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL1, &cctl1);
689 pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL2, &cctl2);
690
691 if (ctl1 == pctl1 && ctl1 == cctl1 &&
692 ctl2 == pctl2 && ctl2 == cctl2)
693 return;
694
695 /* Disable L1.2 while updating. See PCIe r5.0, sec 5.5.4, 7.8.3.3 */
696 pl1_2_enables = pctl1 & PCI_L1SS_CTL1_L1_2_MASK;
697 cl1_2_enables = cctl1 & PCI_L1SS_CTL1_L1_2_MASK;
698
699 if (pl1_2_enables || cl1_2_enables) {
700 pci_clear_and_set_config_dword(child,
701 child->l1ss + PCI_L1SS_CTL1,
702 PCI_L1SS_CTL1_L1_2_MASK, 0);
703 pci_clear_and_set_config_dword(parent,
704 parent->l1ss + PCI_L1SS_CTL1,
705 PCI_L1SS_CTL1_L1_2_MASK, 0);
706 }
707
708 /* Program T_POWER_ON times in both ports */
709 pci_clear_and_set_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2,
710 PCI_L1SS_CTL2_T_PWR_ON_VALUE |
711 PCI_L1SS_CTL2_T_PWR_ON_SCALE, ctl2);
712 pci_clear_and_set_config_dword(child, child->l1ss + PCI_L1SS_CTL2,
713 PCI_L1SS_CTL2_T_PWR_ON_VALUE |
714 PCI_L1SS_CTL2_T_PWR_ON_SCALE, ctl2);
715
716 /* Program Common_Mode_Restore_Time in upstream device */
717 pci_clear_and_set_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
718 PCI_L1SS_CTL1_CM_RESTORE_TIME,
719 ctl1 & PCI_L1SS_CTL1_CM_RESTORE_TIME);
720
721 /* Program LTR_L1.2_THRESHOLD time in both ports */
722 pci_clear_and_set_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
723 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
724 PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
725 ctl1 & (PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
726 PCI_L1SS_CTL1_LTR_L12_TH_SCALE));
727 pci_clear_and_set_config_dword(child, child->l1ss + PCI_L1SS_CTL1,
728 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
729 PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
730 ctl1 & (PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
731 PCI_L1SS_CTL1_LTR_L12_TH_SCALE));
732
733 if (pl1_2_enables || cl1_2_enables) {
734 pci_clear_and_set_config_dword(parent,
735 parent->l1ss + PCI_L1SS_CTL1, 0,
736 pl1_2_enables);
737 pci_clear_and_set_config_dword(child,
738 child->l1ss + PCI_L1SS_CTL1, 0,
739 cl1_2_enables);
740 }
741 }
742
743 static void aspm_l1ss_init(struct pcie_link_state *link)
744 {
745 struct pci_dev *child = link->downstream, *parent = link->pdev;
746 u32 parent_l1ss_cap, child_l1ss_cap;
747 u32 parent_l1ss_ctl1 = 0, child_l1ss_ctl1 = 0;
748
749 if (!parent->l1ss || !child->l1ss)
750 return;
751
752 /* Setup L1 substate */
753 pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CAP,
754 &parent_l1ss_cap);
755 pci_read_config_dword(child, child->l1ss + PCI_L1SS_CAP,
756 &child_l1ss_cap);
757
758 if (!(parent_l1ss_cap & PCI_L1SS_CAP_L1_PM_SS))
759 parent_l1ss_cap = 0;
760 if (!(child_l1ss_cap & PCI_L1SS_CAP_L1_PM_SS))
761 child_l1ss_cap = 0;
762
763 /*
764 * If we don't have LTR for the entire path from the Root Complex
765 * to this device, we can't use ASPM L1.2 because it relies on the
766 * LTR_L1.2_THRESHOLD. See PCIe r4.0, secs 5.5.4, 6.18.
767 */
768 if (!child->ltr_path)
769 child_l1ss_cap &= ~PCI_L1SS_CAP_ASPM_L1_2;
770
771 if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1)
772 link->aspm_support |= PCIE_LINK_STATE_L1_1;
773 if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2)
774 link->aspm_support |= PCIE_LINK_STATE_L1_2;
775 if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1)
776 link->aspm_support |= PCIE_LINK_STATE_L1_1_PCIPM;
777 if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2)
778 link->aspm_support |= PCIE_LINK_STATE_L1_2_PCIPM;
779
780 if (parent_l1ss_cap)
781 pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
782 &parent_l1ss_ctl1);
783 if (child_l1ss_cap)
784 pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL1,
785 &child_l1ss_ctl1);
786
787 if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1)
788 link->aspm_enabled |= PCIE_LINK_STATE_L1_1;
789 if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2)
790 link->aspm_enabled |= PCIE_LINK_STATE_L1_2;
791 if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1)
792 link->aspm_enabled |= PCIE_LINK_STATE_L1_1_PCIPM;
793 if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2)
794 link->aspm_enabled |= PCIE_LINK_STATE_L1_2_PCIPM;
795
796 if (link->aspm_support & PCIE_LINK_STATE_L1_2_MASK)
797 aspm_calc_l12_info(link, parent_l1ss_cap, child_l1ss_cap);
798 }
799
800 #define FLAG(x, y, d) (((x) & (PCIE_LINK_STATE_##y)) ? d : "")
801
802 static void pcie_aspm_override_default_link_state(struct pcie_link_state *link)
803 {
804 struct pci_dev *pdev = link->downstream;
805 u32 override;
806
807 /* For devicetree platforms, enable L0s and L1 by default */
808 if (of_have_populated_dt()) {
809 if (link->aspm_support & PCIE_LINK_STATE_L0S)
810 link->aspm_default |= PCIE_LINK_STATE_L0S;
811 if (link->aspm_support & PCIE_LINK_STATE_L1)
812 link->aspm_default |= PCIE_LINK_STATE_L1;
813 override = link->aspm_default & ~link->aspm_enabled;
814 if (override)
815 pci_info(pdev, "ASPM: default states%s%s\n",
816 FLAG(override, L0S, " L0s"),
817 FLAG(override, L1, " L1"));
818 }
819 }
820
821 static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
822 {
823 struct pci_dev *child = link->downstream, *parent = link->pdev;
824 u16 parent_lnkctl, child_lnkctl;
825 struct pci_bus *linkbus = parent->subordinate;
826
827 if (blacklist) {
828 /* Set enabled/disable so that we will disable ASPM later */
829 link->aspm_enabled = PCIE_LINK_STATE_ASPM_ALL;
830 link->aspm_disable = PCIE_LINK_STATE_ASPM_ALL;
831 return;
832 }
833
834 /*
835 * If ASPM not supported, don't mess with the clocks and link,
836 * bail out now.
837 */
838 if (!(parent->aspm_l0s_support && child->aspm_l0s_support) &&
839 !(parent->aspm_l1_support && child->aspm_l1_support))
840 return;
841
842 /* Configure common clock before checking latencies */
843 pcie_aspm_configure_common_clock(link);
844
845 /*
846 * Re-read upstream/downstream components' register state after
847 * clock configuration. L0s & L1 exit latencies in the otherwise
848 * read-only Link Capabilities may change depending on common clock
849 * configuration (PCIe r5.0, sec 7.5.3.6).
850 */
851 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &parent_lnkctl);
852 pcie_capability_read_word(child, PCI_EXP_LNKCTL, &child_lnkctl);
853
854 /* Disable L0s/L1 before updating L1SS config */
855 if (FIELD_GET(PCI_EXP_LNKCTL_ASPMC, child_lnkctl) ||
856 FIELD_GET(PCI_EXP_LNKCTL_ASPMC, parent_lnkctl)) {
857 pcie_capability_write_word(child, PCI_EXP_LNKCTL,
858 child_lnkctl & ~PCI_EXP_LNKCTL_ASPMC);
859 pcie_capability_write_word(parent, PCI_EXP_LNKCTL,
860 parent_lnkctl & ~PCI_EXP_LNKCTL_ASPMC);
861 }
862
863 /*
864 * Setup L0s state
865 *
866 * Note that we must not enable L0s in either direction on a
867 * given link unless components on both sides of the link each
868 * support L0s.
869 */
870 if (parent->aspm_l0s_support && child->aspm_l0s_support)
871 link->aspm_support |= PCIE_LINK_STATE_L0S;
872
873 if (child_lnkctl & PCI_EXP_LNKCTL_ASPM_L0S)
874 link->aspm_enabled |= PCIE_LINK_STATE_L0S_UP;
875 if (parent_lnkctl & PCI_EXP_LNKCTL_ASPM_L0S)
876 link->aspm_enabled |= PCIE_LINK_STATE_L0S_DW;
877
878 /* Setup L1 state */
879 if (parent->aspm_l1_support && child->aspm_l1_support)
880 link->aspm_support |= PCIE_LINK_STATE_L1;
881
882 if (parent_lnkctl & child_lnkctl & PCI_EXP_LNKCTL_ASPM_L1)
883 link->aspm_enabled |= PCIE_LINK_STATE_L1;
884
885 aspm_l1ss_init(link);
886
887 /* Restore L0s/L1 if they were enabled */
888 if (FIELD_GET(PCI_EXP_LNKCTL_ASPMC, child_lnkctl) ||
889 FIELD_GET(PCI_EXP_LNKCTL_ASPMC, parent_lnkctl)) {
890 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, parent_lnkctl);
891 pcie_capability_write_word(child, PCI_EXP_LNKCTL, child_lnkctl);
892 }
893
894 /* Save default state */
895 link->aspm_default = link->aspm_enabled;
896
897 pcie_aspm_override_default_link_state(link);
898
899 /* Setup initial capable state. Will be updated later */
900 link->aspm_capable = link->aspm_support;
901
902 /* Get and check endpoint acceptable latencies */
903 list_for_each_entry(child, &linkbus->devices, bus_list) {
904 if (pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT &&
905 pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END)
906 continue;
907
908 pcie_aspm_check_latency(child);
909 }
910 }
911
912 /* Configure the ASPM L1 substates. Caller must disable L1 first. */
913 static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state)
914 {
915 u32 val = 0;
916 struct pci_dev *child = link->downstream, *parent = link->pdev;
917
918 if (state & PCIE_LINK_STATE_L1_1)
919 val |= PCI_L1SS_CTL1_ASPM_L1_1;
920 if (state & PCIE_LINK_STATE_L1_2)
921 val |= PCI_L1SS_CTL1_ASPM_L1_2;
922 if (state & PCIE_LINK_STATE_L1_1_PCIPM)
923 val |= PCI_L1SS_CTL1_PCIPM_L1_1;
924 if (state & PCIE_LINK_STATE_L1_2_PCIPM)
925 val |= PCI_L1SS_CTL1_PCIPM_L1_2;
926
927 /*
928 * PCIe r6.2, sec 5.5.4, rules for enabling L1 PM Substates:
929 * - Clear L1.x enable bits at child first, then at parent
930 * - Set L1.x enable bits at parent first, then at child
931 * - ASPM/PCIPM L1.2 must be disabled while programming timing
932 * parameters
933 */
934
935 /* Disable all L1 substates */
936 pci_clear_and_set_config_dword(child, child->l1ss + PCI_L1SS_CTL1,
937 PCI_L1SS_CTL1_L1SS_MASK, 0);
938 pci_clear_and_set_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
939 PCI_L1SS_CTL1_L1SS_MASK, 0);
940
941 /* Enable what we need to enable */
942 pci_clear_and_set_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
943 PCI_L1SS_CTL1_L1SS_MASK, val);
944 pci_clear_and_set_config_dword(child, child->l1ss + PCI_L1SS_CTL1,
945 PCI_L1SS_CTL1_L1SS_MASK, val);
946 }
947
948 static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
949 {
950 pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
951 PCI_EXP_LNKCTL_ASPMC, val);
952 }
953
954 static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
955 {
956 u32 upstream = 0, dwstream = 0;
957 struct pci_dev *child = link->downstream, *parent = link->pdev;
958 struct pci_bus *linkbus = parent->subordinate;
959
960 /* Enable only the states that were not explicitly disabled */
961 state &= (link->aspm_capable & ~link->aspm_disable);
962
963 /* Can't enable any substates if L1 is not enabled */
964 if (!(state & PCIE_LINK_STATE_L1))
965 state &= ~PCIE_LINK_STATE_L1SS;
966
967 /* Spec says both ports must be in D0 before enabling PCI PM substates*/
968 if (parent->current_state != PCI_D0 || child->current_state != PCI_D0) {
969 state &= ~PCIE_LINK_STATE_L1_SS_PCIPM;
970 state |= (link->aspm_enabled & PCIE_LINK_STATE_L1_SS_PCIPM);
971 }
972
973 /* Nothing to do if the link is already in the requested state */
974 if (link->aspm_enabled == state)
975 return;
976 /* Convert ASPM state to upstream/downstream ASPM register state */
977 if (state & PCIE_LINK_STATE_L0S_UP)
978 dwstream |= PCI_EXP_LNKCTL_ASPM_L0S;
979 if (state & PCIE_LINK_STATE_L0S_DW)
980 upstream |= PCI_EXP_LNKCTL_ASPM_L0S;
981 if (state & PCIE_LINK_STATE_L1) {
982 upstream |= PCI_EXP_LNKCTL_ASPM_L1;
983 dwstream |= PCI_EXP_LNKCTL_ASPM_L1;
984 }
985
986 /*
987 * Per PCIe r6.2, sec 5.5.4, setting either or both of the enable
988 * bits for ASPM L1 PM Substates must be done while ASPM L1 is
989 * disabled. Disable L1 here and apply new configuration after L1SS
990 * configuration has been completed.
991 *
992 * Per sec 7.5.3.7, when disabling ASPM L1, software must disable
993 * it in the Downstream component prior to disabling it in the
994 * Upstream component, and ASPM L1 must be enabled in the Upstream
995 * component prior to enabling it in the Downstream component.
996 *
997 * Sec 7.5.3.7 also recommends programming the same ASPM Control
998 * value for all functions of a multi-function device.
999 */
1000 list_for_each_entry(child, &linkbus->devices, bus_list)
1001 pcie_config_aspm_dev(child, 0);
1002 pcie_config_aspm_dev(parent, 0);
1003
1004 if (link->aspm_capable & PCIE_LINK_STATE_L1SS)
1005 pcie_config_aspm_l1ss(link, state);
1006
1007 pcie_config_aspm_dev(parent, upstream);
1008 list_for_each_entry(child, &linkbus->devices, bus_list)
1009 pcie_config_aspm_dev(child, dwstream);
1010
1011 link->aspm_enabled = state;
1012
1013 /* Update latest ASPM configuration in saved context */
1014 pci_save_aspm_l1ss_state(link->downstream);
1015 pci_update_aspm_saved_state(link->downstream);
1016 pci_save_aspm_l1ss_state(parent);
1017 pci_update_aspm_saved_state(parent);
1018 }
1019
1020 static void pcie_config_aspm_path(struct pcie_link_state *link)
1021 {
1022 while (link) {
1023 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1024 link = link->parent;
1025 }
1026 }
1027
1028 static void free_link_state(struct pcie_link_state *link)
1029 {
1030 link->pdev->link_state = NULL;
1031 kfree(link);
1032 }
1033
1034 static int pcie_aspm_sanity_check(struct pci_dev *pdev)
1035 {
1036 struct pci_dev *child;
1037 u32 reg32;
1038
1039 /*
1040 * Some functions in a slot might not all be PCIe functions,
1041 * very strange. Disable ASPM for the whole slot
1042 */
1043 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
1044 if (!pci_is_pcie(child))
1045 return -EINVAL;
1046
1047 /*
1048 * If ASPM is disabled then we're not going to change
1049 * the BIOS state. It's safe to continue even if it's a
1050 * pre-1.1 device
1051 */
1052
1053 if (aspm_disabled)
1054 continue;
1055
1056 /*
1057 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
1058 * RBER bit to determine if a function is 1.1 version device
1059 */
1060 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
1061 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
1062 pci_info(child, "disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force'\n");
1063 return -EINVAL;
1064 }
1065 }
1066 return 0;
1067 }
1068
1069 static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
1070 {
1071 struct pcie_link_state *link;
1072
1073 link = kzalloc_obj(*link);
1074 if (!link)
1075 return NULL;
1076
1077 INIT_LIST_HEAD(&link->sibling);
1078 link->pdev = pdev;
1079 link->downstream = pci_function_0(pdev->subordinate);
1080
1081 /*
1082 * Root Ports and PCI/PCI-X to PCIe Bridges are roots of PCIe
1083 * hierarchies. Note that some PCIe host implementations omit
1084 * the root ports entirely, in which case a downstream port on
1085 * a switch may become the root of the link state chain for all
1086 * its subordinate endpoints.
1087 */
1088 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
1089 pci_pcie_type(pdev) == PCI_EXP_TYPE_PCIE_BRIDGE ||
1090 !pdev->bus->parent->self) {
1091 link->root = link;
1092 } else {
1093 struct pcie_link_state *parent;
1094
1095 parent = pdev->bus->parent->self->link_state;
1096 if (!parent) {
1097 kfree(link);
1098 return NULL;
1099 }
1100
1101 link->parent = parent;
1102 link->root = link->parent->root;
1103 }
1104
1105 list_add(&link->sibling, &link_list);
1106 pdev->link_state = link;
1107 return link;
1108 }
1109
1110 static void pcie_aspm_update_sysfs_visibility(struct pci_dev *pdev)
1111 {
1112 struct pci_dev *child;
1113
1114 list_for_each_entry(child, &pdev->subordinate->devices, bus_list)
1115 sysfs_update_group(&child->dev.kobj, &aspm_ctrl_attr_group);
1116 }
1117
1118 /*
1119 * pcie_aspm_init_link_state: Initiate PCI express link state.
1120 * It is called after the pcie and its children devices are scanned.
1121 * @pdev: the root port or switch downstream port
1122 */
1123 void pcie_aspm_init_link_state(struct pci_dev *pdev)
1124 {
1125 struct pcie_link_state *link;
1126 int blacklist = !!pcie_aspm_sanity_check(pdev);
1127
1128 if (!aspm_support_enabled)
1129 return;
1130
1131 if (pdev->link_state)
1132 return;
1133
1134 /*
1135 * We allocate pcie_link_state for the component on the upstream
1136 * end of a Link, so there's nothing to do unless this device is
1137 * downstream port.
1138 */
1139 if (!pcie_downstream_port(pdev))
1140 return;
1141
1142 /* VIA has a strange chipset, root port is under a bridge */
1143 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT &&
1144 pdev->bus->self)
1145 return;
1146
1147 down_read(&pci_bus_sem);
1148 if (list_empty(&pdev->subordinate->devices))
1149 goto out;
1150
1151 mutex_lock(&aspm_lock);
1152 link = alloc_pcie_link_state(pdev);
1153 if (!link)
1154 goto unlock;
1155 /*
1156 * Setup initial ASPM state. Note that we need to configure
1157 * upstream links also because capable state of them can be
1158 * update through pcie_aspm_cap_init().
1159 */
1160 pcie_aspm_cap_init(link, blacklist);
1161
1162 /* Setup initial Clock PM state */
1163 pcie_clkpm_cap_init(link, blacklist);
1164
1165 /*
1166 * At this stage drivers haven't had an opportunity to change the
1167 * link policy setting. Enabling ASPM on broken hardware can cripple
1168 * it even before the driver has had a chance to disable ASPM, so
1169 * default to a safe level right now. If we're enabling ASPM beyond
1170 * the BIOS's expectation, we'll do so once pci_enable_device() is
1171 * called.
1172 */
1173 if (aspm_policy != POLICY_POWERSAVE &&
1174 aspm_policy != POLICY_POWER_SUPERSAVE) {
1175 pcie_config_aspm_path(link);
1176 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1177 }
1178
1179 pcie_aspm_update_sysfs_visibility(pdev);
1180
1181 unlock:
1182 mutex_unlock(&aspm_lock);
1183 out:
1184 up_read(&pci_bus_sem);
1185 }
1186
1187 void pci_bridge_reconfigure_ltr(struct pci_dev *pdev)
1188 {
1189 struct pci_dev *bridge;
1190 u32 ctl;
1191
1192 bridge = pci_upstream_bridge(pdev);
1193 if (bridge && bridge->ltr_path) {
1194 pcie_capability_read_dword(bridge, PCI_EXP_DEVCTL2, &ctl);
1195 if (!(ctl & PCI_EXP_DEVCTL2_LTR_EN)) {
1196 pci_dbg(bridge, "re-enabling LTR\n");
1197 pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
1198 PCI_EXP_DEVCTL2_LTR_EN);
1199 }
1200 }
1201 }
1202
1203 void pci_configure_ltr(struct pci_dev *pdev)
1204 {
1205 struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus);
1206 struct pci_dev *bridge;
1207 u32 cap, ctl;
1208
1209 if (!pci_is_pcie(pdev))
1210 return;
1211
1212 pcie_capability_read_dword(pdev, PCI_EXP_DEVCAP2, &cap);
1213 if (!(cap & PCI_EXP_DEVCAP2_LTR))
1214 return;
1215
1216 pcie_capability_read_dword(pdev, PCI_EXP_DEVCTL2, &ctl);
1217 if (ctl & PCI_EXP_DEVCTL2_LTR_EN) {
1218 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT) {
1219 pdev->ltr_path = 1;
1220 return;
1221 }
1222
1223 bridge = pci_upstream_bridge(pdev);
1224 if (bridge && bridge->ltr_path)
1225 pdev->ltr_path = 1;
1226
1227 return;
1228 }
1229
1230 if (!host->native_ltr)
1231 return;
1232
1233 /*
1234 * Software must not enable LTR in an Endpoint unless the Root
1235 * Complex and all intermediate Switches indicate support for LTR.
1236 * PCIe r4.0, sec 6.18.
1237 */
1238 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT) {
1239 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL2,
1240 PCI_EXP_DEVCTL2_LTR_EN);
1241 pdev->ltr_path = 1;
1242 return;
1243 }
1244
1245 /*
1246 * If we're configuring a hot-added device, LTR was likely
1247 * disabled in the upstream bridge, so re-enable it before enabling
1248 * it in the new device.
1249 */
1250 bridge = pci_upstream_bridge(pdev);
1251 if (bridge && bridge->ltr_path) {
1252 pci_bridge_reconfigure_ltr(pdev);
1253 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL2,
1254 PCI_EXP_DEVCTL2_LTR_EN);
1255 pdev->ltr_path = 1;
1256 }
1257 }
1258
1259 /* Recheck latencies and update aspm_capable for links under the root */
1260 static void pcie_update_aspm_capable(struct pcie_link_state *root)
1261 {
1262 struct pcie_link_state *link;
1263 BUG_ON(root->parent);
1264 list_for_each_entry(link, &link_list, sibling) {
1265 if (link->root != root)
1266 continue;
1267 link->aspm_capable = link->aspm_support;
1268 }
1269 list_for_each_entry(link, &link_list, sibling) {
1270 struct pci_dev *child;
1271 struct pci_bus *linkbus = link->pdev->subordinate;
1272 if (link->root != root)
1273 continue;
1274 list_for_each_entry(child, &linkbus->devices, bus_list) {
1275 if ((pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT) &&
1276 (pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END))
1277 continue;
1278 pcie_aspm_check_latency(child);
1279 }
1280 }
1281 }
1282
1283 /* @pdev: the endpoint device */
1284 void pcie_aspm_exit_link_state(struct pci_dev *pdev)
1285 {
1286 struct pci_dev *parent = pdev->bus->self;
1287 struct pcie_link_state *link, *root, *parent_link;
1288
1289 if (!parent || !parent->link_state)
1290 return;
1291
1292 down_read(&pci_bus_sem);
1293 mutex_lock(&aspm_lock);
1294
1295 link = parent->link_state;
1296 root = link->root;
1297 parent_link = link->parent;
1298
1299 /*
1300 * Free the parent link state, no later than function 0 (i.e.
1301 * link->downstream) being removed.
1302 *
1303 * Do not free the link state any earlier. If function 0 is a
1304 * switch upstream port, this link state is parent_link to all
1305 * subordinate ones.
1306 */
1307 if (pdev != link->downstream)
1308 goto out;
1309
1310 pcie_config_aspm_link(link, 0);
1311 list_del(&link->sibling);
1312 free_link_state(link);
1313
1314 /* Recheck latencies and configure upstream links */
1315 if (parent_link) {
1316 pcie_update_aspm_capable(root);
1317 pcie_config_aspm_path(parent_link);
1318 }
1319
1320 out:
1321 mutex_unlock(&aspm_lock);
1322 up_read(&pci_bus_sem);
1323 }
1324
1325 /*
1326 * @pdev: the root port or switch downstream port
1327 * @locked: whether pci_bus_sem is held
1328 */
1329 void pcie_aspm_pm_state_change(struct pci_dev *pdev, bool locked)
1330 {
1331 struct pcie_link_state *link = pdev->link_state;
1332
1333 if (aspm_disabled || !link)
1334 return;
1335 /*
1336 * Devices changed PM state, we should recheck if latency
1337 * meets all functions' requirement
1338 */
1339 if (!locked)
1340 down_read(&pci_bus_sem);
1341 mutex_lock(&aspm_lock);
1342 pcie_update_aspm_capable(link->root);
1343 pcie_config_aspm_path(link);
1344 mutex_unlock(&aspm_lock);
1345 if (!locked)
1346 up_read(&pci_bus_sem);
1347 }
1348
1349 void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
1350 {
1351 struct pcie_link_state *link = pdev->link_state;
1352
1353 if (aspm_disabled || !link)
1354 return;
1355
1356 if (aspm_policy != POLICY_POWERSAVE &&
1357 aspm_policy != POLICY_POWER_SUPERSAVE)
1358 return;
1359
1360 down_read(&pci_bus_sem);
1361 mutex_lock(&aspm_lock);
1362 pcie_config_aspm_path(link);
1363 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1364 mutex_unlock(&aspm_lock);
1365 up_read(&pci_bus_sem);
1366 }
1367
1368 static struct pcie_link_state *pcie_aspm_get_link(struct pci_dev *pdev)
1369 {
1370 struct pci_dev *bridge;
1371
1372 if (!pci_is_pcie(pdev))
1373 return NULL;
1374
1375 bridge = pci_upstream_bridge(pdev);
1376 if (!bridge || !pci_is_pcie(bridge))
1377 return NULL;
1378
1379 return bridge->link_state;
1380 }
1381
1382 static u8 pci_calc_aspm_disable_mask(int state)
1383 {
1384 state &= ~PCIE_LINK_STATE_CLKPM;
1385
1386 /* L1 PM substates require L1 */
1387 if (state & PCIE_LINK_STATE_L1)
1388 state |= PCIE_LINK_STATE_L1SS;
1389
1390 return state;
1391 }
1392
1393 static u8 pci_calc_aspm_enable_mask(int state)
1394 {
1395 state &= ~PCIE_LINK_STATE_CLKPM;
1396
1397 /* L1 PM substates require L1 */
1398 if (state & PCIE_LINK_STATE_L1SS)
1399 state |= PCIE_LINK_STATE_L1;
1400
1401 return state;
1402 }
1403
1404 static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool locked)
1405 {
1406 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1407
1408 if (!link)
1409 return -EINVAL;
1410 /*
1411 * A driver requested that ASPM be disabled on this device, but
1412 * if we don't have permission to manage ASPM (e.g., on ACPI
1413 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
1414 * the _OSC method), we can't honor that request. Windows has
1415 * a similar mechanism using "PciASPMOptOut", which is also
1416 * ignored in this situation.
1417 */
1418 if (aspm_disabled) {
1419 pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n");
1420 return -EPERM;
1421 }
1422
1423 if (!locked)
1424 down_read(&pci_bus_sem);
1425 mutex_lock(&aspm_lock);
1426 link->aspm_disable |= pci_calc_aspm_disable_mask(state);
1427 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1428
1429 if (state & PCIE_LINK_STATE_CLKPM)
1430 link->clkpm_disable = 1;
1431 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1432 mutex_unlock(&aspm_lock);
1433 if (!locked)
1434 up_read(&pci_bus_sem);
1435
1436 return 0;
1437 }
1438
1439 int pci_disable_link_state_locked(struct pci_dev *pdev, int state)
1440 {
1441 lockdep_assert_held_read(&pci_bus_sem);
1442
1443 return __pci_disable_link_state(pdev, state, true);
1444 }
1445 EXPORT_SYMBOL(pci_disable_link_state_locked);
1446
1447 /**
1448 * pci_disable_link_state - Disable device's link state, so the link will
1449 * never enter specific states. Note that if the BIOS didn't grant ASPM
1450 * control to the OS, this does nothing because we can't touch the LNKCTL
1451 * register. Returns 0 or a negative errno.
1452 *
1453 * @pdev: PCI device
1454 * @state: ASPM link state to disable
1455 */
1456 int pci_disable_link_state(struct pci_dev *pdev, int state)
1457 {
1458 return __pci_disable_link_state(pdev, state, false);
1459 }
1460 EXPORT_SYMBOL(pci_disable_link_state);
1461
1462 static int __pci_enable_link_state(struct pci_dev *pdev, int state, bool locked)
1463 {
1464 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1465
1466 if (!link)
1467 return -EINVAL;
1468 /*
1469 * A driver requested that ASPM be enabled on this device, but
1470 * if we don't have permission to manage ASPM (e.g., on ACPI
1471 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
1472 * the _OSC method), we can't honor that request.
1473 */
1474 if (aspm_disabled) {
1475 pci_warn(pdev, "can't override BIOS ASPM; OS doesn't have ASPM control\n");
1476 return -EPERM;
1477 }
1478
1479 if (!locked)
1480 down_read(&pci_bus_sem);
1481 mutex_lock(&aspm_lock);
1482 link->aspm_default = pci_calc_aspm_enable_mask(state);
1483 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1484
1485 link->clkpm_default = (state & PCIE_LINK_STATE_CLKPM) ? 1 : 0;
1486 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1487 mutex_unlock(&aspm_lock);
1488 if (!locked)
1489 up_read(&pci_bus_sem);
1490
1491 return 0;
1492 }
1493
1494 /**
1495 * pci_enable_link_state - Clear and set the default device link state so that
1496 * the link may be allowed to enter the specified states. Note that if the
1497 * BIOS didn't grant ASPM control to the OS, this does nothing because we can't
1498 * touch the LNKCTL register. Also note that this does not enable states
1499 * disabled by pci_disable_link_state(). Return 0 or a negative errno.
1500 *
1501 * Note: Ensure devices are in D0 before enabling PCI-PM L1 PM Substates, per
1502 * PCIe r6.0, sec 5.5.4.
1503 *
1504 * @pdev: PCI device
1505 * @state: Mask of ASPM link states to enable
1506 */
1507 int pci_enable_link_state(struct pci_dev *pdev, int state)
1508 {
1509 return __pci_enable_link_state(pdev, state, false);
1510 }
1511 EXPORT_SYMBOL(pci_enable_link_state);
1512
1513 /**
1514 * pci_enable_link_state_locked - Clear and set the default device link state
1515 * so that the link may be allowed to enter the specified states. Note that if
1516 * the BIOS didn't grant ASPM control to the OS, this does nothing because we
1517 * can't touch the LNKCTL register. Also note that this does not enable states
1518 * disabled by pci_disable_link_state(). Return 0 or a negative errno.
1519 *
1520 * Note: Ensure devices are in D0 before enabling PCI-PM L1 PM Substates, per
1521 * PCIe r6.0, sec 5.5.4.
1522 *
1523 * @pdev: PCI device
1524 * @state: Mask of ASPM link states to enable
1525 *
1526 * Context: Caller holds pci_bus_sem read lock.
1527 */
1528 int pci_enable_link_state_locked(struct pci_dev *pdev, int state)
1529 {
1530 lockdep_assert_held_read(&pci_bus_sem);
1531
1532 return __pci_enable_link_state(pdev, state, true);
1533 }
1534 EXPORT_SYMBOL(pci_enable_link_state_locked);
1535
1536 void pcie_aspm_remove_cap(struct pci_dev *pdev, u32 lnkcap)
1537 {
1538 if (lnkcap & PCI_EXP_LNKCAP_ASPM_L0S)
1539 pdev->aspm_l0s_support = 0;
1540 if (lnkcap & PCI_EXP_LNKCAP_ASPM_L1)
1541 pdev->aspm_l1_support = 0;
1542
1543 pci_info(pdev, "ASPM: Link Capabilities%s%s treated as unsupported to avoid device defect\n",
1544 lnkcap & PCI_EXP_LNKCAP_ASPM_L0S ? " L0s" : "",
1545 lnkcap & PCI_EXP_LNKCAP_ASPM_L1 ? " L1" : "");
1546
1547 }
1548
1549 static int pcie_aspm_set_policy(const char *val,
1550 const struct kernel_param *kp)
1551 {
1552 int i;
1553 struct pcie_link_state *link;
1554
1555 if (aspm_disabled)
1556 return -EPERM;
1557 i = sysfs_match_string(policy_str, val);
1558 if (i < 0)
1559 return i;
1560 if (i == aspm_policy)
1561 return 0;
1562
1563 down_read(&pci_bus_sem);
1564 mutex_lock(&aspm_lock);
1565 aspm_policy = i;
1566 list_for_each_entry(link, &link_list, sibling) {
1567 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1568 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1569 }
1570 mutex_unlock(&aspm_lock);
1571 up_read(&pci_bus_sem);
1572 return 0;
1573 }
1574
1575 static int pcie_aspm_get_policy(char *buffer, const struct kernel_param *kp)
1576 {
1577 int i, cnt = 0;
1578 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
1579 if (i == aspm_policy)
1580 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
1581 else
1582 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
1583 cnt += sprintf(buffer + cnt, "\n");
1584 return cnt;
1585 }
1586
1587 module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
1588 NULL, 0644);
1589
1590 /**
1591 * pcie_aspm_enabled - Check if PCIe ASPM has been enabled for a device.
1592 * @pdev: Target device.
1593 *
1594 * Relies on the upstream bridge's link_state being valid. The link_state
1595 * is deallocated only when the last child of the bridge (i.e., @pdev or a
1596 * sibling) is removed, and the caller should be holding a reference to
1597 * @pdev, so this should be safe.
1598 */
1599 bool pcie_aspm_enabled(struct pci_dev *pdev)
1600 {
1601 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1602
1603 if (!link)
1604 return false;
1605
1606 return link->aspm_enabled;
1607 }
1608 EXPORT_SYMBOL_GPL(pcie_aspm_enabled);
1609
1610 static ssize_t aspm_attr_show_common(struct device *dev,
1611 struct device_attribute *attr,
1612 char *buf, u8 state)
1613 {
1614 struct pci_dev *pdev = to_pci_dev(dev);
1615 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1616
1617 return sysfs_emit(buf, "%d\n", (link->aspm_enabled & state) ? 1 : 0);
1618 }
1619
1620 static ssize_t aspm_attr_store_common(struct device *dev,
1621 struct device_attribute *attr,
1622 const char *buf, size_t len, u8 state)
1623 {
1624 struct pci_dev *pdev = to_pci_dev(dev);
1625 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1626 bool state_enable;
1627
1628 if (kstrtobool(buf, &state_enable) < 0)
1629 return -EINVAL;
1630
1631 down_read(&pci_bus_sem);
1632 mutex_lock(&aspm_lock);
1633
1634 if (state_enable) {
1635 link->aspm_disable &= ~state;
1636 /* need to enable L1 for substates */
1637 if (state & PCIE_LINK_STATE_L1SS)
1638 link->aspm_disable &= ~PCIE_LINK_STATE_L1;
1639 } else {
1640 link->aspm_disable |= state;
1641 if (state & PCIE_LINK_STATE_L1)
1642 link->aspm_disable |= PCIE_LINK_STATE_L1SS;
1643 }
1644
1645 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1646
1647 mutex_unlock(&aspm_lock);
1648 up_read(&pci_bus_sem);
1649
1650 return len;
1651 }
1652
1653 #define ASPM_ATTR(_f, _s) \
1654 static ssize_t _f##_show(struct device *dev, \
1655 struct device_attribute *attr, char *buf) \
1656 { return aspm_attr_show_common(dev, attr, buf, PCIE_LINK_STATE_##_s); } \
1657 \
1658 static ssize_t _f##_store(struct device *dev, \
1659 struct device_attribute *attr, \
1660 const char *buf, size_t len) \
1661 { return aspm_attr_store_common(dev, attr, buf, len, PCIE_LINK_STATE_##_s); }
1662
1663 ASPM_ATTR(l0s_aspm, L0S)
1664 ASPM_ATTR(l1_aspm, L1)
1665 ASPM_ATTR(l1_1_aspm, L1_1)
1666 ASPM_ATTR(l1_2_aspm, L1_2)
1667 ASPM_ATTR(l1_1_pcipm, L1_1_PCIPM)
1668 ASPM_ATTR(l1_2_pcipm, L1_2_PCIPM)
1669
1670 static ssize_t clkpm_show(struct device *dev,
1671 struct device_attribute *attr, char *buf)
1672 {
1673 struct pci_dev *pdev = to_pci_dev(dev);
1674 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1675
1676 return sysfs_emit(buf, "%d\n", link->clkpm_enabled);
1677 }
1678
1679 static ssize_t clkpm_store(struct device *dev,
1680 struct device_attribute *attr,
1681 const char *buf, size_t len)
1682 {
1683 struct pci_dev *pdev = to_pci_dev(dev);
1684 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1685 bool state_enable;
1686
1687 if (kstrtobool(buf, &state_enable) < 0)
1688 return -EINVAL;
1689
1690 down_read(&pci_bus_sem);
1691 mutex_lock(&aspm_lock);
1692
1693 link->clkpm_disable = !state_enable;
1694 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1695
1696 mutex_unlock(&aspm_lock);
1697 up_read(&pci_bus_sem);
1698
1699 return len;
1700 }
1701
1702 static DEVICE_ATTR_RW(clkpm);
1703 static DEVICE_ATTR_RW(l0s_aspm);
1704 static DEVICE_ATTR_RW(l1_aspm);
1705 static DEVICE_ATTR_RW(l1_1_aspm);
1706 static DEVICE_ATTR_RW(l1_2_aspm);
1707 static DEVICE_ATTR_RW(l1_1_pcipm);
1708 static DEVICE_ATTR_RW(l1_2_pcipm);
1709
1710 static struct attribute *aspm_ctrl_attrs[] = {
1711 &dev_attr_clkpm.attr,
1712 &dev_attr_l0s_aspm.attr,
1713 &dev_attr_l1_aspm.attr,
1714 &dev_attr_l1_1_aspm.attr,
1715 &dev_attr_l1_2_aspm.attr,
1716 &dev_attr_l1_1_pcipm.attr,
1717 &dev_attr_l1_2_pcipm.attr,
1718 NULL
1719 };
1720
1721 static umode_t aspm_ctrl_attrs_are_visible(struct kobject *kobj,
1722 struct attribute *a, int n)
1723 {
1724 struct device *dev = kobj_to_dev(kobj);
1725 struct pci_dev *pdev = to_pci_dev(dev);
1726 struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1727 static const u8 aspm_state_map[] = {
1728 PCIE_LINK_STATE_L0S,
1729 PCIE_LINK_STATE_L1,
1730 PCIE_LINK_STATE_L1_1,
1731 PCIE_LINK_STATE_L1_2,
1732 PCIE_LINK_STATE_L1_1_PCIPM,
1733 PCIE_LINK_STATE_L1_2_PCIPM,
1734 };
1735
1736 if (aspm_disabled || !link)
1737 return 0;
1738
1739 if (n == 0)
1740 return link->clkpm_capable ? a->mode : 0;
1741
1742 return link->aspm_capable & aspm_state_map[n - 1] ? a->mode : 0;
1743 }
1744
1745 const struct attribute_group aspm_ctrl_attr_group = {
1746 .name = "link",
1747 .attrs = aspm_ctrl_attrs,
1748 .is_visible = aspm_ctrl_attrs_are_visible,
1749 };
1750
1751 static int __init pcie_aspm_disable(char *str)
1752 {
1753 if (!strcmp(str, "off")) {
1754 aspm_policy = POLICY_DEFAULT;
1755 aspm_disabled = true;
1756 aspm_support_enabled = false;
1757 pr_info("PCIe ASPM is disabled\n");
1758 } else if (!strcmp(str, "force")) {
1759 aspm_force = true;
1760 pr_info("PCIe ASPM is forcibly enabled\n");
1761 }
1762 return 1;
1763 }
1764
1765 __setup("pcie_aspm=", pcie_aspm_disable);
1766
1767 void pcie_no_aspm(void)
1768 {
1769 /*
1770 * Disabling ASPM is intended to prevent the kernel from modifying
1771 * existing hardware state, not to clear existing state. To that end:
1772 * (a) set policy to POLICY_DEFAULT in order to avoid changing state
1773 * (b) prevent userspace from changing policy
1774 */
1775 if (!aspm_force) {
1776 aspm_policy = POLICY_DEFAULT;
1777 aspm_disabled = true;
1778 }
1779 }
1780
1781 bool pcie_aspm_support_enabled(void)
1782 {
1783 return aspm_support_enabled;
1784 }
1785
1786 #endif /* CONFIG_PCIEASPM */