Merge branch 'for-2.6.31' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / pci / pcie / aspm.c
1 /*
2  * File:        drivers/pci/pcie/aspm.c
3  * Enabling 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 struct aspm_latency {
30         u32 l0s;                        /* L0s latency (nsec) */
31         u32 l1;                         /* L1 latency (nsec) */
32 };
33
34 struct pcie_link_state {
35         struct pci_dev *pdev;           /* Upstream component of the Link */
36         struct pcie_link_state *root;   /* pointer to the root port link */
37         struct pcie_link_state *parent; /* pointer to the parent Link state */
38         struct list_head sibling;       /* node in link_list */
39         struct list_head children;      /* list of child link states */
40         struct list_head link;          /* node in parent's children list */
41
42         /* ASPM state */
43         u32 aspm_support:2;             /* Supported ASPM state */
44         u32 aspm_enabled:2;             /* Enabled ASPM state */
45         u32 aspm_default:2;             /* Default ASPM state by BIOS */
46
47         /* Clock PM state */
48         u32 clkpm_capable:1;            /* Clock PM capable? */
49         u32 clkpm_enabled:1;            /* Current Clock PM state */
50         u32 clkpm_default:1;            /* Default Clock PM state by BIOS */
51
52         /* Latencies */
53         struct aspm_latency latency;    /* Exit latency */
54         /*
55          * Endpoint acceptable latencies. A pcie downstream port only
56          * has one slot under it, so at most there are 8 functions.
57          */
58         struct aspm_latency acceptable[8];
59 };
60
61 static int aspm_disabled, aspm_force;
62 static DEFINE_MUTEX(aspm_lock);
63 static LIST_HEAD(link_list);
64
65 #define POLICY_DEFAULT 0        /* BIOS default setting */
66 #define POLICY_PERFORMANCE 1    /* high performance */
67 #define POLICY_POWERSAVE 2      /* high power saving */
68 static int aspm_policy;
69 static const char *policy_str[] = {
70         [POLICY_DEFAULT] = "default",
71         [POLICY_PERFORMANCE] = "performance",
72         [POLICY_POWERSAVE] = "powersave"
73 };
74
75 #define LINK_RETRAIN_TIMEOUT HZ
76
77 static int policy_to_aspm_state(struct pcie_link_state *link)
78 {
79         switch (aspm_policy) {
80         case POLICY_PERFORMANCE:
81                 /* Disable ASPM and Clock PM */
82                 return 0;
83         case POLICY_POWERSAVE:
84                 /* Enable ASPM L0s/L1 */
85                 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
86         case POLICY_DEFAULT:
87                 return link->aspm_default;
88         }
89         return 0;
90 }
91
92 static int policy_to_clkpm_state(struct pcie_link_state *link)
93 {
94         switch (aspm_policy) {
95         case POLICY_PERFORMANCE:
96                 /* Disable ASPM and Clock PM */
97                 return 0;
98         case POLICY_POWERSAVE:
99                 /* Disable Clock PM */
100                 return 1;
101         case POLICY_DEFAULT:
102                 return link->clkpm_default;
103         }
104         return 0;
105 }
106
107 static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
108 {
109         int pos;
110         u16 reg16;
111         struct pci_dev *child;
112         struct pci_bus *linkbus = link->pdev->subordinate;
113
114         list_for_each_entry(child, &linkbus->devices, bus_list) {
115                 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
116                 if (!pos)
117                         return;
118                 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
119                 if (enable)
120                         reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
121                 else
122                         reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
123                 pci_write_config_word(child, pos + PCI_EXP_LNKCTL, reg16);
124         }
125         link->clkpm_enabled = !!enable;
126 }
127
128 static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
129 {
130         /* Don't enable Clock PM if the link is not Clock PM capable */
131         if (!link->clkpm_capable && enable)
132                 return;
133         /* Need nothing if the specified equals to current state */
134         if (link->clkpm_enabled == enable)
135                 return;
136         pcie_set_clkpm_nocheck(link, enable);
137 }
138
139 static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
140 {
141         int pos, capable = 1, enabled = 1;
142         u32 reg32;
143         u16 reg16;
144         struct pci_dev *child;
145         struct pci_bus *linkbus = link->pdev->subordinate;
146
147         /* All functions should have the same cap and state, take the worst */
148         list_for_each_entry(child, &linkbus->devices, bus_list) {
149                 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
150                 if (!pos)
151                         return;
152                 pci_read_config_dword(child, pos + PCI_EXP_LNKCAP, &reg32);
153                 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
154                         capable = 0;
155                         enabled = 0;
156                         break;
157                 }
158                 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
159                 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
160                         enabled = 0;
161         }
162         link->clkpm_enabled = enabled;
163         link->clkpm_default = enabled;
164         link->clkpm_capable = (blacklist) ? 0 : capable;
165 }
166
167 static bool pcie_aspm_downstream_has_switch(struct pcie_link_state *link)
168 {
169         struct pci_dev *child;
170         struct pci_bus *linkbus = link->pdev->subordinate;
171
172         list_for_each_entry(child, &linkbus->devices, bus_list) {
173                 if (child->pcie_type == PCI_EXP_TYPE_UPSTREAM)
174                         return true;
175         }
176         return false;
177 }
178
179 /*
180  * pcie_aspm_configure_common_clock: check if the 2 ends of a link
181  *   could use common clock. If they are, configure them to use the
182  *   common clock. That will reduce the ASPM state exit latency.
183  */
184 static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
185 {
186         int ppos, cpos, same_clock = 1;
187         u16 reg16, parent_reg, child_reg[8];
188         unsigned long start_jiffies;
189         struct pci_dev *child, *parent = link->pdev;
190         struct pci_bus *linkbus = parent->subordinate;
191         /*
192          * All functions of a slot should have the same Slot Clock
193          * Configuration, so just check one function
194          */
195         child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
196         BUG_ON(!child->is_pcie);
197
198         /* Check downstream component if bit Slot Clock Configuration is 1 */
199         cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
200         pci_read_config_word(child, cpos + PCI_EXP_LNKSTA, &reg16);
201         if (!(reg16 & PCI_EXP_LNKSTA_SLC))
202                 same_clock = 0;
203
204         /* Check upstream component if bit Slot Clock Configuration is 1 */
205         ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
206         pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
207         if (!(reg16 & PCI_EXP_LNKSTA_SLC))
208                 same_clock = 0;
209
210         /* Configure downstream component, all functions */
211         list_for_each_entry(child, &linkbus->devices, bus_list) {
212                 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
213                 pci_read_config_word(child, cpos + PCI_EXP_LNKCTL, &reg16);
214                 child_reg[PCI_FUNC(child->devfn)] = reg16;
215                 if (same_clock)
216                         reg16 |= PCI_EXP_LNKCTL_CCC;
217                 else
218                         reg16 &= ~PCI_EXP_LNKCTL_CCC;
219                 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL, reg16);
220         }
221
222         /* Configure upstream component */
223         pci_read_config_word(parent, ppos + PCI_EXP_LNKCTL, &reg16);
224         parent_reg = reg16;
225         if (same_clock)
226                 reg16 |= PCI_EXP_LNKCTL_CCC;
227         else
228                 reg16 &= ~PCI_EXP_LNKCTL_CCC;
229         pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
230
231         /* Retrain link */
232         reg16 |= PCI_EXP_LNKCTL_RL;
233         pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
234
235         /* Wait for link training end. Break out after waiting for timeout */
236         start_jiffies = jiffies;
237         for (;;) {
238                 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
239                 if (!(reg16 & PCI_EXP_LNKSTA_LT))
240                         break;
241                 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
242                         break;
243                 msleep(1);
244         }
245         if (!(reg16 & PCI_EXP_LNKSTA_LT))
246                 return;
247
248         /* Training failed. Restore common clock configurations */
249         dev_printk(KERN_ERR, &parent->dev,
250                    "ASPM: Could not configure common clock\n");
251         list_for_each_entry(child, &linkbus->devices, bus_list) {
252                 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
253                 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL,
254                                       child_reg[PCI_FUNC(child->devfn)]);
255         }
256         pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, parent_reg);
257 }
258
259 /* Convert L0s latency encoding to ns */
260 static u32 calc_l0s_latency(u32 encoding)
261 {
262         if (encoding == 0x7)
263                 return (5 * 1000);      /* > 4us */
264         return (64 << encoding);
265 }
266
267 /* Convert L0s acceptable latency encoding to ns */
268 static u32 calc_l0s_acceptable(u32 encoding)
269 {
270         if (encoding == 0x7)
271                 return -1U;
272         return (64 << encoding);
273 }
274
275 /* Convert L1 latency encoding to ns */
276 static u32 calc_l1_latency(u32 encoding)
277 {
278         if (encoding == 0x7)
279                 return (65 * 1000);     /* > 64us */
280         return (1000 << encoding);
281 }
282
283 /* Convert L1 acceptable latency encoding to ns */
284 static u32 calc_l1_acceptable(u32 encoding)
285 {
286         if (encoding == 0x7)
287                 return -1U;
288         return (1000 << encoding);
289 }
290
291 static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
292                                      u32 *l0s, u32 *l1, u32 *enabled)
293 {
294         int pos;
295         u16 reg16;
296         u32 reg32, encoding;
297
298         *l0s = *l1 = *enabled = 0;
299         pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
300         pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
301         *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
302         if (*state != PCIE_LINK_STATE_L0S &&
303             *state != (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L0S))
304                 *state = 0;
305         if (*state == 0)
306                 return;
307
308         encoding = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
309         *l0s = calc_l0s_latency(encoding);
310         if (*state & PCIE_LINK_STATE_L1) {
311                 encoding = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
312                 *l1 = calc_l1_latency(encoding);
313         }
314         pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
315         *enabled = reg16 & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
316 }
317
318 static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
319 {
320         u32 support, l0s, l1, enabled;
321         struct pci_dev *child, *parent = link->pdev;
322         struct pci_bus *linkbus = parent->subordinate;
323
324         if (blacklist) {
325                 /* Set support state to 0, so we will disable ASPM later */
326                 link->aspm_support = 0;
327                 link->aspm_default = 0;
328                 link->aspm_enabled = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
329                 return;
330         }
331
332         /* Configure common clock before checking latencies */
333         pcie_aspm_configure_common_clock(link);
334
335         /* upstream component states */
336         pcie_aspm_get_cap_device(parent, &support, &l0s, &l1, &enabled);
337         link->aspm_support = support;
338         link->latency.l0s = l0s;
339         link->latency.l1 = l1;
340         link->aspm_enabled = enabled;
341
342         /* downstream component states, all functions have the same setting */
343         child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
344         pcie_aspm_get_cap_device(child, &support, &l0s, &l1, &enabled);
345         link->aspm_support &= support;
346         link->latency.l0s = max_t(u32, link->latency.l0s, l0s);
347         link->latency.l1 = max_t(u32, link->latency.l1, l1);
348
349         if (!link->aspm_support)
350                 return;
351
352         link->aspm_enabled &= link->aspm_support;
353         link->aspm_default = link->aspm_enabled;
354
355         /* ENDPOINT states*/
356         list_for_each_entry(child, &linkbus->devices, bus_list) {
357                 int pos;
358                 u32 reg32, encoding;
359                 struct aspm_latency *acceptable =
360                         &link->acceptable[PCI_FUNC(child->devfn)];
361
362                 if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
363                     child->pcie_type != PCI_EXP_TYPE_LEG_END)
364                         continue;
365
366                 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
367                 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
368                 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
369                 acceptable->l0s = calc_l0s_acceptable(encoding);
370                 if (link->aspm_support & PCIE_LINK_STATE_L1) {
371                         encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
372                         acceptable->l1 = calc_l1_acceptable(encoding);
373                 }
374         }
375 }
376
377 /**
378  * __pcie_aspm_check_state_one - check latency for endpoint device.
379  * @endpoint: pointer to the struct pci_dev of endpoint device
380  *
381  * TBD: The latency from the endpoint to root complex vary per switch's
382  * upstream link state above the device. Here we just do a simple check
383  * which assumes all links above the device can be in L1 state, that
384  * is we just consider the worst case. If switch's upstream link can't
385  * be put into L0S/L1, then our check is too strictly.
386  */
387 static u32 __pcie_aspm_check_state_one(struct pci_dev *endpoint, u32 state)
388 {
389         u32 l1_switch_latency = 0;
390         struct aspm_latency *acceptable;
391         struct pcie_link_state *link;
392
393         link = endpoint->bus->self->link_state;
394         state &= link->aspm_support;
395         acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
396
397         while (link && state) {
398                 if ((state & PCIE_LINK_STATE_L0S) &&
399                     (link->latency.l0s > acceptable->l0s))
400                         state &= ~PCIE_LINK_STATE_L0S;
401                 if ((state & PCIE_LINK_STATE_L1) &&
402                     (link->latency.l1 + l1_switch_latency > acceptable->l1))
403                         state &= ~PCIE_LINK_STATE_L1;
404                 link = link->parent;
405                 /*
406                  * Every switch on the path to root complex need 1
407                  * more microsecond for L1. Spec doesn't mention L0s.
408                  */
409                 l1_switch_latency += 1000;
410         }
411         return state;
412 }
413
414 static u32 pcie_aspm_check_state(struct pcie_link_state *link, u32 state)
415 {
416         pci_power_t power_state;
417         struct pci_dev *child;
418         struct pci_bus *linkbus = link->pdev->subordinate;
419
420         /* If no child, ignore the link */
421         if (list_empty(&linkbus->devices))
422                 return state;
423
424         list_for_each_entry(child, &linkbus->devices, bus_list) {
425                 /*
426                  * If downstream component of a link is pci bridge, we
427                  * disable ASPM for now for the link
428                  */
429                 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
430                         return 0;
431
432                 if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
433                      child->pcie_type != PCI_EXP_TYPE_LEG_END))
434                         continue;
435                 /* Device not in D0 doesn't need check latency */
436                 power_state = child->current_state;
437                 if (power_state == PCI_D1 || power_state == PCI_D2 ||
438                     power_state == PCI_D3hot || power_state == PCI_D3cold)
439                         continue;
440                 state = __pcie_aspm_check_state_one(child, state);
441         }
442         return state;
443 }
444
445 static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
446 {
447         u16 reg16;
448         int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
449
450         pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
451         reg16 &= ~0x3;
452         reg16 |= state;
453         pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
454 }
455
456 static void __pcie_aspm_config_link(struct pcie_link_state *link, u32 state)
457 {
458         struct pci_dev *child, *parent = link->pdev;
459         struct pci_bus *linkbus = parent->subordinate;
460
461         /* If no child, disable the link */
462         if (list_empty(&linkbus->devices))
463                 state = 0;
464         /*
465          * If the downstream component has pci bridge function, don't
466          * do ASPM now.
467          */
468         list_for_each_entry(child, &linkbus->devices, bus_list) {
469                 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
470                         return;
471         }
472         /*
473          * Spec 2.0 suggests all functions should be configured the
474          * same setting for ASPM. Enabling ASPM L1 should be done in
475          * upstream component first and then downstream, and vice
476          * versa for disabling ASPM L1. Spec doesn't mention L0S.
477          */
478         if (state & PCIE_LINK_STATE_L1)
479                 __pcie_aspm_config_one_dev(parent, state);
480
481         list_for_each_entry(child, &linkbus->devices, bus_list)
482                 __pcie_aspm_config_one_dev(child, state);
483
484         if (!(state & PCIE_LINK_STATE_L1))
485                 __pcie_aspm_config_one_dev(parent, state);
486
487         link->aspm_enabled = state;
488 }
489
490 /* Check the whole hierarchy, and configure each link in the hierarchy */
491 static void __pcie_aspm_configure_link_state(struct pcie_link_state *link,
492                                              u32 state)
493 {
494         struct pcie_link_state *leaf, *root = link->root;
495
496         state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
497
498         /* Check all links who have specific root port link */
499         list_for_each_entry(leaf, &link_list, sibling) {
500                 if (!list_empty(&leaf->children) || (leaf->root != root))
501                         continue;
502                 state = pcie_aspm_check_state(leaf, state);
503         }
504         /* Check root port link too in case it hasn't children */
505         state = pcie_aspm_check_state(root, state);
506         if (link->aspm_enabled == state)
507                 return;
508         /*
509          * We must change the hierarchy. See comments in
510          * __pcie_aspm_config_link for the order
511          **/
512         if (state & PCIE_LINK_STATE_L1) {
513                 list_for_each_entry(leaf, &link_list, sibling) {
514                         if (leaf->root == root)
515                                 __pcie_aspm_config_link(leaf, state);
516                 }
517         } else {
518                 list_for_each_entry_reverse(leaf, &link_list, sibling) {
519                         if (leaf->root == root)
520                                 __pcie_aspm_config_link(leaf, state);
521                 }
522         }
523 }
524
525 /*
526  * pcie_aspm_configure_link_state: enable/disable PCI express link state
527  * @pdev: the root port or switch downstream port
528  */
529 static void pcie_aspm_configure_link_state(struct pcie_link_state *link,
530                                            u32 state)
531 {
532         down_read(&pci_bus_sem);
533         mutex_lock(&aspm_lock);
534         __pcie_aspm_configure_link_state(link, state);
535         mutex_unlock(&aspm_lock);
536         up_read(&pci_bus_sem);
537 }
538
539 static void free_link_state(struct pcie_link_state *link)
540 {
541         link->pdev->link_state = NULL;
542         kfree(link);
543 }
544
545 static int pcie_aspm_sanity_check(struct pci_dev *pdev)
546 {
547         struct pci_dev *child;
548         int pos;
549         u32 reg32;
550         /*
551          * Some functions in a slot might not all be PCIE functions,
552          * very strange. Disable ASPM for the whole slot
553          */
554         list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
555                 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
556                 if (!pos)
557                         return -EINVAL;
558                 /*
559                  * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
560                  * RBER bit to determine if a function is 1.1 version device
561                  */
562                 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
563                 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
564                         dev_printk(KERN_INFO, &child->dev, "disabling ASPM"
565                                 " on pre-1.1 PCIe device.  You can enable it"
566                                 " with 'pcie_aspm=force'\n");
567                         return -EINVAL;
568                 }
569         }
570         return 0;
571 }
572
573 static struct pcie_link_state *pcie_aspm_setup_link_state(struct pci_dev *pdev)
574 {
575         struct pcie_link_state *link;
576         int blacklist = !!pcie_aspm_sanity_check(pdev);
577
578         link = kzalloc(sizeof(*link), GFP_KERNEL);
579         if (!link)
580                 return NULL;
581         INIT_LIST_HEAD(&link->sibling);
582         INIT_LIST_HEAD(&link->children);
583         INIT_LIST_HEAD(&link->link);
584         link->pdev = pdev;
585         if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
586                 struct pcie_link_state *parent;
587                 parent = pdev->bus->parent->self->link_state;
588                 if (!parent) {
589                         kfree(link);
590                         return NULL;
591                 }
592                 link->parent = parent;
593                 list_add(&link->link, &parent->children);
594         }
595         /* Setup a pointer to the root port link */
596         if (!link->parent)
597                 link->root = link;
598         else
599                 link->root = link->parent->root;
600
601         list_add(&link->sibling, &link_list);
602
603         pdev->link_state = link;
604
605         /* Check ASPM capability */
606         pcie_aspm_cap_init(link, blacklist);
607
608         /* Check Clock PM capability */
609         pcie_clkpm_cap_init(link, blacklist);
610
611         return link;
612 }
613
614 /*
615  * pcie_aspm_init_link_state: Initiate PCI express link state.
616  * It is called after the pcie and its children devices are scaned.
617  * @pdev: the root port or switch downstream port
618  */
619 void pcie_aspm_init_link_state(struct pci_dev *pdev)
620 {
621         u32 state;
622         struct pcie_link_state *link;
623
624         if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
625                 return;
626         if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
627             pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
628                 return;
629
630         /* VIA has a strange chipset, root port is under a bridge */
631         if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
632             pdev->bus->self)
633                 return;
634
635         down_read(&pci_bus_sem);
636         if (list_empty(&pdev->subordinate->devices))
637                 goto out;
638
639         mutex_lock(&aspm_lock);
640         link = pcie_aspm_setup_link_state(pdev);
641         if (!link)
642                 goto unlock;
643         /*
644          * Setup initial ASPM state
645          *
646          * If link has switch, delay the link config. The leaf link
647          * initialization will config the whole hierarchy. But we must
648          * make sure BIOS doesn't set unsupported link state.
649          */
650         if (pcie_aspm_downstream_has_switch(link)) {
651                 state = pcie_aspm_check_state(link, link->aspm_default);
652                 __pcie_aspm_config_link(link, state);
653         } else {
654                 state = policy_to_aspm_state(link);
655                 __pcie_aspm_configure_link_state(link, state);
656         }
657
658         /* Setup initial Clock PM state */
659         state = (link->clkpm_capable) ? policy_to_clkpm_state(link) : 0;
660         pcie_set_clkpm(link, state);
661 unlock:
662         mutex_unlock(&aspm_lock);
663 out:
664         up_read(&pci_bus_sem);
665 }
666
667 /* @pdev: the endpoint device */
668 void pcie_aspm_exit_link_state(struct pci_dev *pdev)
669 {
670         struct pci_dev *parent = pdev->bus->self;
671         struct pcie_link_state *link_state = parent->link_state;
672
673         if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
674                 return;
675         if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
676                 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
677                 return;
678         down_read(&pci_bus_sem);
679         mutex_lock(&aspm_lock);
680
681         /*
682          * All PCIe functions are in one slot, remove one function will remove
683          * the whole slot, so just wait until we are the last function left.
684          */
685         if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
686                 goto out;
687
688         /* All functions are removed, so just disable ASPM for the link */
689         __pcie_aspm_config_one_dev(parent, 0);
690         list_del(&link_state->sibling);
691         list_del(&link_state->link);
692         /* Clock PM is for endpoint device */
693
694         free_link_state(link_state);
695 out:
696         mutex_unlock(&aspm_lock);
697         up_read(&pci_bus_sem);
698 }
699
700 /* @pdev: the root port or switch downstream port */
701 void pcie_aspm_pm_state_change(struct pci_dev *pdev)
702 {
703         struct pcie_link_state *link_state = pdev->link_state;
704
705         if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
706                 return;
707         if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
708                 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
709                 return;
710         /*
711          * devices changed PM state, we should recheck if latency meets all
712          * functions' requirement
713          */
714         pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
715 }
716
717 /*
718  * pci_disable_link_state - disable pci device's link state, so the link will
719  * never enter specific states
720  */
721 void pci_disable_link_state(struct pci_dev *pdev, int state)
722 {
723         struct pci_dev *parent = pdev->bus->self;
724         struct pcie_link_state *link_state;
725
726         if (aspm_disabled || !pdev->is_pcie)
727                 return;
728         if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
729             pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
730                 parent = pdev;
731         if (!parent || !parent->link_state)
732                 return;
733
734         down_read(&pci_bus_sem);
735         mutex_lock(&aspm_lock);
736         link_state = parent->link_state;
737         link_state->aspm_support &= ~state;
738         __pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
739         if (state & PCIE_LINK_STATE_CLKPM) {
740                 link_state->clkpm_capable = 0;
741                 pcie_set_clkpm(link_state, 0);
742         }
743         mutex_unlock(&aspm_lock);
744         up_read(&pci_bus_sem);
745 }
746 EXPORT_SYMBOL(pci_disable_link_state);
747
748 static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
749 {
750         int i;
751         struct pcie_link_state *link_state;
752
753         for (i = 0; i < ARRAY_SIZE(policy_str); i++)
754                 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
755                         break;
756         if (i >= ARRAY_SIZE(policy_str))
757                 return -EINVAL;
758         if (i == aspm_policy)
759                 return 0;
760
761         down_read(&pci_bus_sem);
762         mutex_lock(&aspm_lock);
763         aspm_policy = i;
764         list_for_each_entry(link_state, &link_list, sibling) {
765                 __pcie_aspm_configure_link_state(link_state,
766                         policy_to_aspm_state(link_state));
767                 pcie_set_clkpm(link_state, policy_to_clkpm_state(link_state));
768         }
769         mutex_unlock(&aspm_lock);
770         up_read(&pci_bus_sem);
771         return 0;
772 }
773
774 static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
775 {
776         int i, cnt = 0;
777         for (i = 0; i < ARRAY_SIZE(policy_str); i++)
778                 if (i == aspm_policy)
779                         cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
780                 else
781                         cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
782         return cnt;
783 }
784
785 module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
786         NULL, 0644);
787
788 #ifdef CONFIG_PCIEASPM_DEBUG
789 static ssize_t link_state_show(struct device *dev,
790                 struct device_attribute *attr,
791                 char *buf)
792 {
793         struct pci_dev *pci_device = to_pci_dev(dev);
794         struct pcie_link_state *link_state = pci_device->link_state;
795
796         return sprintf(buf, "%d\n", link_state->aspm_enabled);
797 }
798
799 static ssize_t link_state_store(struct device *dev,
800                 struct device_attribute *attr,
801                 const char *buf,
802                 size_t n)
803 {
804         struct pci_dev *pdev = to_pci_dev(dev);
805         int state;
806
807         if (n < 1)
808                 return -EINVAL;
809         state = buf[0]-'0';
810         if (state >= 0 && state <= 3) {
811                 /* setup link aspm state */
812                 pcie_aspm_configure_link_state(pdev->link_state, state);
813                 return n;
814         }
815
816         return -EINVAL;
817 }
818
819 static ssize_t clk_ctl_show(struct device *dev,
820                 struct device_attribute *attr,
821                 char *buf)
822 {
823         struct pci_dev *pci_device = to_pci_dev(dev);
824         struct pcie_link_state *link_state = pci_device->link_state;
825
826         return sprintf(buf, "%d\n", link_state->clkpm_enabled);
827 }
828
829 static ssize_t clk_ctl_store(struct device *dev,
830                 struct device_attribute *attr,
831                 const char *buf,
832                 size_t n)
833 {
834         struct pci_dev *pdev = to_pci_dev(dev);
835         int state;
836
837         if (n < 1)
838                 return -EINVAL;
839         state = buf[0]-'0';
840
841         down_read(&pci_bus_sem);
842         mutex_lock(&aspm_lock);
843         pcie_set_clkpm_nocheck(pdev->link_state, !!state);
844         mutex_unlock(&aspm_lock);
845         up_read(&pci_bus_sem);
846
847         return n;
848 }
849
850 static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
851 static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
852
853 static char power_group[] = "power";
854 void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
855 {
856         struct pcie_link_state *link_state = pdev->link_state;
857
858         if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
859                 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
860                 return;
861
862         if (link_state->aspm_support)
863                 sysfs_add_file_to_group(&pdev->dev.kobj,
864                         &dev_attr_link_state.attr, power_group);
865         if (link_state->clkpm_capable)
866                 sysfs_add_file_to_group(&pdev->dev.kobj,
867                         &dev_attr_clk_ctl.attr, power_group);
868 }
869
870 void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
871 {
872         struct pcie_link_state *link_state = pdev->link_state;
873
874         if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
875                 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
876                 return;
877
878         if (link_state->aspm_support)
879                 sysfs_remove_file_from_group(&pdev->dev.kobj,
880                         &dev_attr_link_state.attr, power_group);
881         if (link_state->clkpm_capable)
882                 sysfs_remove_file_from_group(&pdev->dev.kobj,
883                         &dev_attr_clk_ctl.attr, power_group);
884 }
885 #endif
886
887 static int __init pcie_aspm_disable(char *str)
888 {
889         if (!strcmp(str, "off")) {
890                 aspm_disabled = 1;
891                 printk(KERN_INFO "PCIe ASPM is disabled\n");
892         } else if (!strcmp(str, "force")) {
893                 aspm_force = 1;
894                 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
895         }
896         return 1;
897 }
898
899 __setup("pcie_aspm=", pcie_aspm_disable);
900
901 void pcie_no_aspm(void)
902 {
903         if (!aspm_force)
904                 aspm_disabled = 1;
905 }
906
907 /**
908  * pcie_aspm_enabled - is PCIe ASPM enabled?
909  *
910  * Returns true if ASPM has not been disabled by the command-line option
911  * pcie_aspm=off.
912  **/
913 int pcie_aspm_enabled(void)
914 {
915        return !aspm_disabled;
916 }
917 EXPORT_SYMBOL(pcie_aspm_enabled);
918