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