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