2 * File: drivers/pci/pcie/aspm.c
3 * Enabling PCIE link L0s/L1 state and Clock Power Management
5 * Copyright (C) 2007 Intel
6 * Copyright (C) Zhang Yanmin (yanmin.zhang@intel.com)
7 * Copyright (C) Shaohua Li (shaohua.li@intel.com)
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>
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>
24 #ifdef MODULE_PARAM_PREFIX
25 #undef MODULE_PARAM_PREFIX
27 #define MODULE_PARAM_PREFIX "pcie_aspm."
30 u32 l0s; /* L0s latency (nsec) */
31 u32 l1; /* L1 latency (nsec) */
34 struct pcie_link_state {
35 struct pci_dev *pdev; /* Upstream component of the Link */
36 struct pcie_link_state *parent; /* pointer to the parent Link state */
37 struct list_head sibling; /* node in link_list */
38 struct list_head children; /* list of child link states */
39 struct list_head link; /* node in parent's children list */
42 u32 aspm_support:2; /* Supported ASPM state */
43 u32 aspm_enabled:2; /* Enabled ASPM state */
44 u32 aspm_default:2; /* Default ASPM state by BIOS */
47 u32 clkpm_capable:1; /* Clock PM capable? */
48 u32 clkpm_enabled:1; /* Current Clock PM state */
49 u32 clkpm_default:1; /* Default Clock PM state by BIOS */
51 u32 has_switch:1; /* Downstream has switches? */
54 struct aspm_latency latency; /* Exit latency */
56 * Endpoint acceptable latencies. A pcie downstream port only
57 * has one slot under it, so at most there are 8 functions.
59 struct aspm_latency acceptable[8];
62 static int aspm_disabled, aspm_force;
63 static DEFINE_MUTEX(aspm_lock);
64 static LIST_HEAD(link_list);
66 #define POLICY_DEFAULT 0 /* BIOS default setting */
67 #define POLICY_PERFORMANCE 1 /* high performance */
68 #define POLICY_POWERSAVE 2 /* high power saving */
69 static int aspm_policy;
70 static const char *policy_str[] = {
71 [POLICY_DEFAULT] = "default",
72 [POLICY_PERFORMANCE] = "performance",
73 [POLICY_POWERSAVE] = "powersave"
76 #define LINK_RETRAIN_TIMEOUT HZ
78 static int policy_to_aspm_state(struct pci_dev *pdev)
80 struct pcie_link_state *link_state = pdev->link_state;
82 switch (aspm_policy) {
83 case POLICY_PERFORMANCE:
84 /* Disable ASPM and Clock PM */
86 case POLICY_POWERSAVE:
87 /* Enable ASPM L0s/L1 */
88 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
90 return link_state->aspm_default;
95 static int policy_to_clkpm_state(struct pci_dev *pdev)
97 struct pcie_link_state *link_state = pdev->link_state;
99 switch (aspm_policy) {
100 case POLICY_PERFORMANCE:
101 /* Disable ASPM and Clock PM */
103 case POLICY_POWERSAVE:
104 /* Disable Clock PM */
107 return link_state->clkpm_default;
112 static void pcie_set_clock_pm(struct pci_dev *pdev, int enable)
114 struct pci_dev *child_dev;
117 struct pcie_link_state *link_state = pdev->link_state;
119 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
120 pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
123 pci_read_config_word(child_dev, pos + PCI_EXP_LNKCTL, ®16);
125 reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
127 reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
128 pci_write_config_word(child_dev, pos + PCI_EXP_LNKCTL, reg16);
130 link_state->clkpm_enabled = !!enable;
133 static void pcie_check_clock_pm(struct pci_dev *pdev, int blacklist)
138 int capable = 1, enabled = 1;
139 struct pci_dev *child_dev;
140 struct pcie_link_state *link_state = pdev->link_state;
142 /* All functions should have the same cap and state, take the worst */
143 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
144 pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
147 pci_read_config_dword(child_dev, pos + PCI_EXP_LNKCAP, ®32);
148 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
153 pci_read_config_word(child_dev, pos + PCI_EXP_LNKCTL, ®16);
154 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
157 link_state->clkpm_enabled = enabled;
158 link_state->clkpm_default = enabled;
160 link_state->clkpm_capable = capable;
161 pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
163 link_state->clkpm_capable = 0;
164 pcie_set_clock_pm(pdev, 0);
168 static bool pcie_aspm_downstream_has_switch(struct pci_dev *pdev)
170 struct pci_dev *child_dev;
172 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
173 if (child_dev->pcie_type == PCI_EXP_TYPE_UPSTREAM)
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.
184 static void pcie_aspm_configure_common_clock(struct pci_dev *pdev)
186 int pos, child_pos, i = 0;
188 struct pci_dev *child_dev;
190 unsigned long start_jiffies;
191 u16 child_regs[8], parent_reg;
193 * all functions of a slot should have the same Slot Clock
194 * Configuration, so just check one function
196 child_dev = list_entry(pdev->subordinate->devices.next, struct pci_dev,
198 BUG_ON(!child_dev->is_pcie);
200 /* Check downstream component if bit Slot Clock Configuration is 1 */
201 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
202 pci_read_config_word(child_dev, child_pos + PCI_EXP_LNKSTA, ®16);
203 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
206 /* Check upstream component if bit Slot Clock Configuration is 1 */
207 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
208 pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, ®16);
209 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
212 /* Configure downstream component, all functions */
213 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
214 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
215 pci_read_config_word(child_dev, child_pos + PCI_EXP_LNKCTL,
217 child_regs[i] = reg16;
219 reg16 |= PCI_EXP_LNKCTL_CCC;
221 reg16 &= ~PCI_EXP_LNKCTL_CCC;
222 pci_write_config_word(child_dev, child_pos + PCI_EXP_LNKCTL,
227 /* Configure upstream component */
228 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, ®16);
231 reg16 |= PCI_EXP_LNKCTL_CCC;
233 reg16 &= ~PCI_EXP_LNKCTL_CCC;
234 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
237 reg16 |= PCI_EXP_LNKCTL_RL;
238 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
240 /* Wait for link training end */
241 /* break out after waiting for timeout */
242 start_jiffies = jiffies;
244 pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, ®16);
245 if (!(reg16 & PCI_EXP_LNKSTA_LT))
247 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
251 /* training failed -> recover */
252 if (reg16 & PCI_EXP_LNKSTA_LT) {
253 dev_printk (KERN_ERR, &pdev->dev, "ASPM: Could not configure"
256 list_for_each_entry(child_dev, &pdev->subordinate->devices,
258 child_pos = pci_find_capability(child_dev,
260 pci_write_config_word(child_dev,
261 child_pos + PCI_EXP_LNKCTL,
265 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, parent_reg);
270 * calc_L0S_latency: Convert L0s latency encoding to ns
272 static unsigned int calc_L0S_latency(unsigned int latency_encoding, int ac)
274 unsigned int ns = 64;
276 if (latency_encoding == 0x7) {
280 ns = 5*1000; /* > 4us */
282 ns *= (1 << latency_encoding);
287 * calc_L1_latency: Convert L1 latency encoding to ns
289 static unsigned int calc_L1_latency(unsigned int latency_encoding, int ac)
291 unsigned int ns = 1000;
293 if (latency_encoding == 0x7) {
297 ns = 65*1000; /* > 64us */
299 ns *= (1 << latency_encoding);
303 static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
304 unsigned int *l0s, unsigned int *l1, unsigned int *enabled)
309 unsigned int latency;
311 *l0s = *l1 = *enabled = 0;
312 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
313 pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, ®32);
314 *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
315 if (*state != PCIE_LINK_STATE_L0S &&
316 *state != (PCIE_LINK_STATE_L1|PCIE_LINK_STATE_L0S))
321 latency = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
322 *l0s = calc_L0S_latency(latency, 0);
323 if (*state & PCIE_LINK_STATE_L1) {
324 latency = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
325 *l1 = calc_L1_latency(latency, 0);
327 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, ®16);
328 *enabled = reg16 & (PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1);
331 static void pcie_aspm_cap_init(struct pci_dev *pdev)
333 struct pci_dev *child_dev;
334 u32 support, l0s, l1, enabled;
335 struct pcie_link_state *link_state = pdev->link_state;
337 /* upstream component states */
338 pcie_aspm_get_cap_device(pdev, &support, &l0s, &l1, &enabled);
339 link_state->aspm_support = support;
340 link_state->latency.l0s = l0s;
341 link_state->latency.l1 = l1;
342 link_state->aspm_enabled = enabled;
344 /* downstream component states, all functions have the same setting */
345 child_dev = list_entry(pdev->subordinate->devices.next, struct pci_dev,
347 pcie_aspm_get_cap_device(child_dev, &support, &l0s, &l1, &enabled);
348 link_state->aspm_support &= support;
349 link_state->latency.l0s = max_t(u32, link_state->latency.l0s, l0s);
350 link_state->latency.l1 = max_t(u32, link_state->latency.l1, l1);
352 if (!link_state->aspm_support)
355 link_state->aspm_enabled &= link_state->aspm_support;
356 link_state->aspm_default = link_state->aspm_enabled;
359 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
362 unsigned int latency;
363 struct aspm_latency *acceptable =
364 &link_state->acceptable[PCI_FUNC(child_dev->devfn)];
366 if (child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
367 child_dev->pcie_type != PCI_EXP_TYPE_LEG_END)
370 pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
371 pci_read_config_dword(child_dev, pos + PCI_EXP_DEVCAP, ®32);
372 latency = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
373 latency = calc_L0S_latency(latency, 1);
374 acceptable->l0s = latency;
375 if (link_state->aspm_support & PCIE_LINK_STATE_L1) {
376 latency = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
377 latency = calc_L1_latency(latency, 1);
378 acceptable->l1 = latency;
383 static unsigned int __pcie_aspm_check_state_one(struct pci_dev *pdev,
386 struct pci_dev *parent_dev, *tmp_dev;
387 unsigned int l1_latency = 0;
388 struct pcie_link_state *link_state;
389 struct aspm_latency *acceptable;
391 parent_dev = pdev->bus->self;
392 link_state = parent_dev->link_state;
393 state &= link_state->aspm_support;
396 acceptable = &link_state->acceptable[PCI_FUNC(pdev->devfn)];
399 * Check latency for endpoint device.
400 * TBD: The latency from the endpoint to root complex vary per
401 * switch's upstream link state above the device. Here we just do a
402 * simple check which assumes all links above the device can be in L1
403 * state, that is we just consider the worst case. If switch's upstream
404 * link can't be put into L0S/L1, then our check is too strictly.
407 while (state & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1)) {
408 parent_dev = tmp_dev->bus->self;
409 link_state = parent_dev->link_state;
410 if ((state & PCIE_LINK_STATE_L0S) &&
411 (link_state->latency.l0s > acceptable->l0s))
412 state &= ~PCIE_LINK_STATE_L0S;
414 if ((state & PCIE_LINK_STATE_L1) &&
415 (link_state->latency.l1 + l1_latency > acceptable->l1))
416 state &= ~PCIE_LINK_STATE_L1;
418 if (!parent_dev->bus->self) /* parent_dev is a root port */
422 * parent_dev is the downstream port of a switch, make
423 * tmp_dev the upstream port of the switch
425 tmp_dev = parent_dev->bus->self;
427 * every switch on the path to root complex need 1 more
428 * microsecond for L1. Spec doesn't mention L0S.
430 if (state & PCIE_LINK_STATE_L1)
437 static unsigned int pcie_aspm_check_state(struct pci_dev *pdev,
440 struct pci_dev *child_dev;
442 /* If no child, ignore the link */
443 if (list_empty(&pdev->subordinate->devices))
445 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
446 if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
448 * If downstream component of a link is pci bridge, we
449 * disable ASPM for now for the link
454 if ((child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
455 child_dev->pcie_type != PCI_EXP_TYPE_LEG_END))
457 /* Device not in D0 doesn't need check latency */
458 if (child_dev->current_state == PCI_D1 ||
459 child_dev->current_state == PCI_D2 ||
460 child_dev->current_state == PCI_D3hot ||
461 child_dev->current_state == PCI_D3cold)
463 state = __pcie_aspm_check_state_one(child_dev, state);
468 static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
471 int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
473 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, ®16);
476 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
479 static void __pcie_aspm_config_link(struct pci_dev *pdev, unsigned int state)
481 struct pci_dev *child_dev;
483 struct pcie_link_state *link_state = pdev->link_state;
485 /* If no child, disable the link */
486 if (list_empty(&pdev->subordinate->devices))
489 * if the downstream component has pci bridge function, don't do ASPM
492 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
493 if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
502 * spec 2.0 suggests all functions should be configured the same
503 * setting for ASPM. Enabling ASPM L1 should be done in upstream
504 * component first and then downstream, and vice versa for disabling
505 * ASPM L1. Spec doesn't mention L0S.
507 if (state & PCIE_LINK_STATE_L1)
508 __pcie_aspm_config_one_dev(pdev, state);
510 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list)
511 __pcie_aspm_config_one_dev(child_dev, state);
513 if (!(state & PCIE_LINK_STATE_L1))
514 __pcie_aspm_config_one_dev(pdev, state);
516 link_state->aspm_enabled = state;
519 static struct pcie_link_state *get_root_port_link(struct pcie_link_state *link)
521 struct pcie_link_state *root_port_link = link;
522 while (root_port_link->parent)
523 root_port_link = root_port_link->parent;
524 return root_port_link;
527 /* check the whole hierarchy, and configure each link in the hierarchy */
528 static void __pcie_aspm_configure_link_state(struct pci_dev *pdev,
531 struct pcie_link_state *link_state = pdev->link_state;
532 struct pcie_link_state *root_port_link = get_root_port_link(link_state);
533 struct pcie_link_state *leaf;
535 state &= PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1;
537 /* check all links who have specific root port link */
538 list_for_each_entry(leaf, &link_list, sibling) {
539 if (!list_empty(&leaf->children) ||
540 get_root_port_link(leaf) != root_port_link)
542 state = pcie_aspm_check_state(leaf->pdev, state);
544 /* check root port link too in case it hasn't children */
545 state = pcie_aspm_check_state(root_port_link->pdev, state);
547 if (link_state->aspm_enabled == state)
551 * we must change the hierarchy. See comments in
552 * __pcie_aspm_config_link for the order
554 if (state & PCIE_LINK_STATE_L1) {
555 list_for_each_entry(leaf, &link_list, sibling) {
556 if (get_root_port_link(leaf) == root_port_link)
557 __pcie_aspm_config_link(leaf->pdev, state);
560 list_for_each_entry_reverse(leaf, &link_list, sibling) {
561 if (get_root_port_link(leaf) == root_port_link)
562 __pcie_aspm_config_link(leaf->pdev, state);
568 * pcie_aspm_configure_link_state: enable/disable PCI express link state
569 * @pdev: the root port or switch downstream port
571 static void pcie_aspm_configure_link_state(struct pci_dev *pdev,
574 down_read(&pci_bus_sem);
575 mutex_lock(&aspm_lock);
576 __pcie_aspm_configure_link_state(pdev, state);
577 mutex_unlock(&aspm_lock);
578 up_read(&pci_bus_sem);
581 static void free_link_state(struct pci_dev *pdev)
583 kfree(pdev->link_state);
584 pdev->link_state = NULL;
587 static int pcie_aspm_sanity_check(struct pci_dev *pdev)
589 struct pci_dev *child_dev;
594 * Some functions in a slot might not all be PCIE functions, very
595 * strange. Disable ASPM for the whole slot
597 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
598 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
603 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
604 * RBER bit to determine if a function is 1.1 version device
606 pci_read_config_dword(child_dev, child_pos + PCI_EXP_DEVCAP,
608 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
609 dev_printk(KERN_INFO, &child_dev->dev, "disabling ASPM"
610 " on pre-1.1 PCIe device. You can enable it"
611 " with 'pcie_aspm=force'\n");
619 * pcie_aspm_init_link_state: Initiate PCI express link state.
620 * It is called after the pcie and its children devices are scaned.
621 * @pdev: the root port or switch downstream port
623 void pcie_aspm_init_link_state(struct pci_dev *pdev)
626 struct pcie_link_state *link_state;
630 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
632 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
633 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
635 /* VIA has a strange chipset, root port is under a bridge */
636 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
639 down_read(&pci_bus_sem);
640 if (list_empty(&pdev->subordinate->devices))
643 blacklist = !!pcie_aspm_sanity_check(pdev);
645 mutex_lock(&aspm_lock);
647 link_state = kzalloc(sizeof(*link_state), GFP_KERNEL);
651 link_state->has_switch = pcie_aspm_downstream_has_switch(pdev);
652 INIT_LIST_HEAD(&link_state->children);
653 INIT_LIST_HEAD(&link_state->link);
654 if (pdev->bus->self) {/* this is a switch */
655 struct pcie_link_state *parent_link_state;
657 parent_link_state = pdev->bus->parent->self->link_state;
658 if (!parent_link_state) {
662 list_add(&link_state->link, &parent_link_state->children);
663 link_state->parent = parent_link_state;
666 pdev->link_state = link_state;
669 pcie_aspm_configure_common_clock(pdev);
670 pcie_aspm_cap_init(pdev);
672 link_state->aspm_enabled =
673 (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
674 link_state->aspm_default = 0;
675 /* Set support state to 0, so we will disable ASPM later */
676 link_state->aspm_support = 0;
679 link_state->pdev = pdev;
680 list_add(&link_state->sibling, &link_list);
682 if (link_state->has_switch) {
684 * If link has switch, delay the link config. The leaf link
685 * initialization will config the whole hierarchy. but we must
686 * make sure BIOS doesn't set unsupported link state
688 state = pcie_aspm_check_state(pdev, link_state->aspm_default);
689 __pcie_aspm_config_link(pdev, state);
691 __pcie_aspm_configure_link_state(pdev,
692 policy_to_aspm_state(pdev));
694 pcie_check_clock_pm(pdev, blacklist);
698 free_link_state(pdev);
699 mutex_unlock(&aspm_lock);
701 up_read(&pci_bus_sem);
704 /* @pdev: the endpoint device */
705 void pcie_aspm_exit_link_state(struct pci_dev *pdev)
707 struct pci_dev *parent = pdev->bus->self;
708 struct pcie_link_state *link_state = parent->link_state;
710 if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
712 if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
713 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
715 down_read(&pci_bus_sem);
716 mutex_lock(&aspm_lock);
719 * All PCIe functions are in one slot, remove one function will remove
720 * the whole slot, so just wait until we are the last function left.
722 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
725 /* All functions are removed, so just disable ASPM for the link */
726 __pcie_aspm_config_one_dev(parent, 0);
727 list_del(&link_state->sibling);
728 list_del(&link_state->link);
729 /* Clock PM is for endpoint device */
731 free_link_state(parent);
733 mutex_unlock(&aspm_lock);
734 up_read(&pci_bus_sem);
737 /* @pdev: the root port or switch downstream port */
738 void pcie_aspm_pm_state_change(struct pci_dev *pdev)
740 struct pcie_link_state *link_state = pdev->link_state;
742 if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
744 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
745 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
748 * devices changed PM state, we should recheck if latency meets all
749 * functions' requirement
751 pcie_aspm_configure_link_state(pdev, link_state->aspm_enabled);
755 * pci_disable_link_state - disable pci device's link state, so the link will
756 * never enter specific states
758 void pci_disable_link_state(struct pci_dev *pdev, int state)
760 struct pci_dev *parent = pdev->bus->self;
761 struct pcie_link_state *link_state;
763 if (aspm_disabled || !pdev->is_pcie)
765 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
766 pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
768 if (!parent || !parent->link_state)
771 down_read(&pci_bus_sem);
772 mutex_lock(&aspm_lock);
773 link_state = parent->link_state;
774 link_state->aspm_support &= ~state;
775 if (state & PCIE_LINK_STATE_CLKPM)
776 link_state->clkpm_capable = 0;
778 __pcie_aspm_configure_link_state(parent, link_state->aspm_enabled);
779 if (!link_state->clkpm_capable && link_state->clkpm_enabled)
780 pcie_set_clock_pm(parent, 0);
781 mutex_unlock(&aspm_lock);
782 up_read(&pci_bus_sem);
784 EXPORT_SYMBOL(pci_disable_link_state);
786 static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
789 struct pci_dev *pdev;
790 struct pcie_link_state *link_state;
792 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
793 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
795 if (i >= ARRAY_SIZE(policy_str))
797 if (i == aspm_policy)
800 down_read(&pci_bus_sem);
801 mutex_lock(&aspm_lock);
803 list_for_each_entry(link_state, &link_list, sibling) {
804 pdev = link_state->pdev;
805 __pcie_aspm_configure_link_state(pdev,
806 policy_to_aspm_state(pdev));
807 if (link_state->clkpm_capable &&
808 link_state->clkpm_enabled != policy_to_clkpm_state(pdev))
809 pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
812 mutex_unlock(&aspm_lock);
813 up_read(&pci_bus_sem);
817 static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
820 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
821 if (i == aspm_policy)
822 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
824 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
828 module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
831 #ifdef CONFIG_PCIEASPM_DEBUG
832 static ssize_t link_state_show(struct device *dev,
833 struct device_attribute *attr,
836 struct pci_dev *pci_device = to_pci_dev(dev);
837 struct pcie_link_state *link_state = pci_device->link_state;
839 return sprintf(buf, "%d\n", link_state->aspm_enabled);
842 static ssize_t link_state_store(struct device *dev,
843 struct device_attribute *attr,
847 struct pci_dev *pci_device = to_pci_dev(dev);
853 if (state >= 0 && state <= 3) {
854 /* setup link aspm state */
855 pcie_aspm_configure_link_state(pci_device, state);
862 static ssize_t clk_ctl_show(struct device *dev,
863 struct device_attribute *attr,
866 struct pci_dev *pci_device = to_pci_dev(dev);
867 struct pcie_link_state *link_state = pci_device->link_state;
869 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
872 static ssize_t clk_ctl_store(struct device *dev,
873 struct device_attribute *attr,
877 struct pci_dev *pci_device = to_pci_dev(dev);
884 down_read(&pci_bus_sem);
885 mutex_lock(&aspm_lock);
886 pcie_set_clock_pm(pci_device, !!state);
887 mutex_unlock(&aspm_lock);
888 up_read(&pci_bus_sem);
893 static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
894 static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
896 static char power_group[] = "power";
897 void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
899 struct pcie_link_state *link_state = pdev->link_state;
901 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
902 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
905 if (link_state->aspm_support)
906 sysfs_add_file_to_group(&pdev->dev.kobj,
907 &dev_attr_link_state.attr, power_group);
908 if (link_state->clkpm_capable)
909 sysfs_add_file_to_group(&pdev->dev.kobj,
910 &dev_attr_clk_ctl.attr, power_group);
913 void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
915 struct pcie_link_state *link_state = pdev->link_state;
917 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
918 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
921 if (link_state->aspm_support)
922 sysfs_remove_file_from_group(&pdev->dev.kobj,
923 &dev_attr_link_state.attr, power_group);
924 if (link_state->clkpm_capable)
925 sysfs_remove_file_from_group(&pdev->dev.kobj,
926 &dev_attr_clk_ctl.attr, power_group);
930 static int __init pcie_aspm_disable(char *str)
932 if (!strcmp(str, "off")) {
934 printk(KERN_INFO "PCIe ASPM is disabled\n");
935 } else if (!strcmp(str, "force")) {
937 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
942 __setup("pcie_aspm=", pcie_aspm_disable);
944 void pcie_no_aspm(void)
951 * pcie_aspm_enabled - is PCIe ASPM enabled?
953 * Returns true if ASPM has not been disabled by the command-line option
956 int pcie_aspm_enabled(void)
958 return !aspm_disabled;
960 EXPORT_SYMBOL(pcie_aspm_enabled);