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