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/pci-aspm.h>
23 #ifdef MODULE_PARAM_PREFIX
24 #undef MODULE_PARAM_PREFIX
26 #define MODULE_PARAM_PREFIX "pcie_aspm."
28 struct endpoint_state {
29 unsigned int l0s_acceptable_latency;
30 unsigned int l1_acceptable_latency;
33 struct pcie_link_state {
34 struct list_head sibiling;
36 bool downstream_has_switch;
38 struct pcie_link_state *parent;
39 struct list_head children;
40 struct list_head link;
43 unsigned int support_state;
44 unsigned int enabled_state;
45 unsigned int bios_aspm_state;
46 /* upstream component */
47 unsigned int l0s_upper_latency;
48 unsigned int l1_upper_latency;
49 /* downstream component */
50 unsigned int l0s_down_latency;
51 unsigned int l1_down_latency;
53 unsigned int clk_pm_capable;
54 unsigned int clk_pm_enabled;
55 unsigned int bios_clk_state;
58 * A pcie downstream port only has one slot under it, so at most there
61 struct endpoint_state endpoints[8];
64 static int aspm_disabled, aspm_force;
65 static DEFINE_MUTEX(aspm_lock);
66 static LIST_HEAD(link_list);
68 #define POLICY_DEFAULT 0 /* BIOS default setting */
69 #define POLICY_PERFORMANCE 1 /* high performance */
70 #define POLICY_POWERSAVE 2 /* high power saving */
71 static int aspm_policy;
72 static const char *policy_str[] = {
73 [POLICY_DEFAULT] = "default",
74 [POLICY_PERFORMANCE] = "performance",
75 [POLICY_POWERSAVE] = "powersave"
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->bios_aspm_state;
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->bios_clk_state;
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->clk_pm_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->clk_pm_enabled = enabled;
158 link_state->bios_clk_state = enabled;
160 link_state->clk_pm_capable = capable;
161 pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
163 link_state->clk_pm_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 1 second */
242 start_jiffies = jiffies;
243 while ((jiffies - start_jiffies) < HZ) {
244 pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, ®16);
245 if (!(reg16 & PCI_EXP_LNKSTA_LT))
249 /* training failed -> recover */
250 if ((jiffies - start_jiffies) >= HZ) {
251 dev_printk (KERN_ERR, &pdev->dev, "ASPM: Could not configure"
254 list_for_each_entry(child_dev, &pdev->subordinate->devices,
256 child_pos = pci_find_capability(child_dev,
258 pci_write_config_word(child_dev,
259 child_pos + PCI_EXP_LNKCTL,
263 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, parent_reg);
268 * calc_L0S_latency: Convert L0s latency encoding to ns
270 static unsigned int calc_L0S_latency(unsigned int latency_encoding, int ac)
272 unsigned int ns = 64;
274 if (latency_encoding == 0x7) {
278 ns = 5*1000; /* > 4us */
280 ns *= (1 << latency_encoding);
285 * calc_L1_latency: Convert L1 latency encoding to ns
287 static unsigned int calc_L1_latency(unsigned int latency_encoding, int ac)
289 unsigned int ns = 1000;
291 if (latency_encoding == 0x7) {
295 ns = 65*1000; /* > 64us */
297 ns *= (1 << latency_encoding);
301 static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
302 unsigned int *l0s, unsigned int *l1, unsigned int *enabled)
307 unsigned int latency;
309 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
310 pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, ®32);
311 *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
312 if (*state != PCIE_LINK_STATE_L0S &&
313 *state != (PCIE_LINK_STATE_L1|PCIE_LINK_STATE_L0S))
318 latency = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
319 *l0s = calc_L0S_latency(latency, 0);
320 if (*state & PCIE_LINK_STATE_L1) {
321 latency = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
322 *l1 = calc_L1_latency(latency, 0);
324 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, ®16);
325 *enabled = reg16 & (PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1);
328 static void pcie_aspm_cap_init(struct pci_dev *pdev)
330 struct pci_dev *child_dev;
332 struct pcie_link_state *link_state = pdev->link_state;
334 /* upstream component states */
335 pcie_aspm_get_cap_device(pdev, &link_state->support_state,
336 &link_state->l0s_upper_latency,
337 &link_state->l1_upper_latency,
338 &link_state->enabled_state);
339 /* downstream component states, all functions have the same setting */
340 child_dev = list_entry(pdev->subordinate->devices.next, struct pci_dev,
342 pcie_aspm_get_cap_device(child_dev, &state,
343 &link_state->l0s_down_latency,
344 &link_state->l1_down_latency,
346 link_state->support_state &= state;
347 if (!link_state->support_state)
349 link_state->enabled_state &= link_state->support_state;
350 link_state->bios_aspm_state = link_state->enabled_state;
353 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
356 unsigned int latency;
357 struct endpoint_state *ep_state =
358 &link_state->endpoints[PCI_FUNC(child_dev->devfn)];
360 if (child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
361 child_dev->pcie_type != PCI_EXP_TYPE_LEG_END)
364 pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
365 pci_read_config_dword(child_dev, pos + PCI_EXP_DEVCAP, ®32);
366 latency = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
367 latency = calc_L0S_latency(latency, 1);
368 ep_state->l0s_acceptable_latency = latency;
369 if (link_state->support_state & PCIE_LINK_STATE_L1) {
370 latency = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
371 latency = calc_L1_latency(latency, 1);
372 ep_state->l1_acceptable_latency = latency;
377 static unsigned int __pcie_aspm_check_state_one(struct pci_dev *pdev,
380 struct pci_dev *parent_dev, *tmp_dev;
381 unsigned int latency, l1_latency = 0;
382 struct pcie_link_state *link_state;
383 struct endpoint_state *ep_state;
385 parent_dev = pdev->bus->self;
386 link_state = parent_dev->link_state;
387 state &= link_state->support_state;
390 ep_state = &link_state->endpoints[PCI_FUNC(pdev->devfn)];
393 * Check latency for endpoint device.
394 * TBD: The latency from the endpoint to root complex vary per
395 * switch's upstream link state above the device. Here we just do a
396 * simple check which assumes all links above the device can be in L1
397 * state, that is we just consider the worst case. If switch's upstream
398 * link can't be put into L0S/L1, then our check is too strictly.
401 while (state & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1)) {
402 parent_dev = tmp_dev->bus->self;
403 link_state = parent_dev->link_state;
404 if (state & PCIE_LINK_STATE_L0S) {
405 latency = max_t(unsigned int,
406 link_state->l0s_upper_latency,
407 link_state->l0s_down_latency);
408 if (latency > ep_state->l0s_acceptable_latency)
409 state &= ~PCIE_LINK_STATE_L0S;
411 if (state & PCIE_LINK_STATE_L1) {
412 latency = max_t(unsigned int,
413 link_state->l1_upper_latency,
414 link_state->l1_down_latency);
415 if (latency + l1_latency >
416 ep_state->l1_acceptable_latency)
417 state &= ~PCIE_LINK_STATE_L1;
419 if (!parent_dev->bus->self) /* parent_dev is a root port */
423 * parent_dev is the downstream port of a switch, make
424 * tmp_dev the upstream port of the switch
426 tmp_dev = parent_dev->bus->self;
428 * every switch on the path to root complex need 1 more
429 * microsecond for L1. Spec doesn't mention L0S.
431 if (state & PCIE_LINK_STATE_L1)
438 static unsigned int pcie_aspm_check_state(struct pci_dev *pdev,
441 struct pci_dev *child_dev;
443 /* If no child, ignore the link */
444 if (list_empty(&pdev->subordinate->devices))
446 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
447 if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
449 * If downstream component of a link is pci bridge, we
450 * disable ASPM for now for the link
455 if ((child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
456 child_dev->pcie_type != PCI_EXP_TYPE_LEG_END))
458 /* Device not in D0 doesn't need check latency */
459 if (child_dev->current_state == PCI_D1 ||
460 child_dev->current_state == PCI_D2 ||
461 child_dev->current_state == PCI_D3hot ||
462 child_dev->current_state == PCI_D3cold)
464 state = __pcie_aspm_check_state_one(child_dev, state);
469 static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
472 int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
474 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, ®16);
477 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
480 static void __pcie_aspm_config_link(struct pci_dev *pdev, unsigned int state)
482 struct pci_dev *child_dev;
484 struct pcie_link_state *link_state = pdev->link_state;
486 /* If no child, disable the link */
487 if (list_empty(&pdev->subordinate->devices))
490 * if the downstream component has pci bridge function, don't do ASPM
493 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
494 if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
503 * spec 2.0 suggests all functions should be configured the same
504 * setting for ASPM. Enabling ASPM L1 should be done in upstream
505 * component first and then downstream, and vice versa for disabling
506 * ASPM L1. Spec doesn't mention L0S.
508 if (state & PCIE_LINK_STATE_L1)
509 __pcie_aspm_config_one_dev(pdev, state);
511 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list)
512 __pcie_aspm_config_one_dev(child_dev, state);
514 if (!(state & PCIE_LINK_STATE_L1))
515 __pcie_aspm_config_one_dev(pdev, state);
517 link_state->enabled_state = state;
520 static struct pcie_link_state *get_root_port_link(struct pcie_link_state *link)
522 struct pcie_link_state *root_port_link = link;
523 while (root_port_link->parent)
524 root_port_link = root_port_link->parent;
525 return root_port_link;
528 /* check the whole hierarchy, and configure each link in the hierarchy */
529 static void __pcie_aspm_configure_link_state(struct pci_dev *pdev,
532 struct pcie_link_state *link_state = pdev->link_state;
533 struct pcie_link_state *root_port_link = get_root_port_link(link_state);
534 struct pcie_link_state *leaf;
536 state &= PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1;
538 /* check all links who have specific root port link */
539 list_for_each_entry(leaf, &link_list, sibiling) {
540 if (!list_empty(&leaf->children) ||
541 get_root_port_link(leaf) != root_port_link)
543 state = pcie_aspm_check_state(leaf->pdev, state);
545 /* check root port link too in case it hasn't children */
546 state = pcie_aspm_check_state(root_port_link->pdev, state);
548 if (link_state->enabled_state == state)
552 * we must change the hierarchy. See comments in
553 * __pcie_aspm_config_link for the order
555 if (state & PCIE_LINK_STATE_L1) {
556 list_for_each_entry(leaf, &link_list, sibiling) {
557 if (get_root_port_link(leaf) == root_port_link)
558 __pcie_aspm_config_link(leaf->pdev, state);
561 list_for_each_entry_reverse(leaf, &link_list, sibiling) {
562 if (get_root_port_link(leaf) == root_port_link)
563 __pcie_aspm_config_link(leaf->pdev, state);
569 * pcie_aspm_configure_link_state: enable/disable PCI express link state
570 * @pdev: the root port or switch downstream port
572 static void pcie_aspm_configure_link_state(struct pci_dev *pdev,
575 down_read(&pci_bus_sem);
576 mutex_lock(&aspm_lock);
577 __pcie_aspm_configure_link_state(pdev, state);
578 mutex_unlock(&aspm_lock);
579 up_read(&pci_bus_sem);
582 static void free_link_state(struct pci_dev *pdev)
584 kfree(pdev->link_state);
585 pdev->link_state = NULL;
588 static int pcie_aspm_sanity_check(struct pci_dev *pdev)
590 struct pci_dev *child_dev;
595 * Some functions in a slot might not all be PCIE functions, very
596 * strange. Disable ASPM for the whole slot
598 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
599 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
604 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
605 * RBER bit to determine if a function is 1.1 version device
607 pci_read_config_dword(child_dev, child_pos + PCI_EXP_DEVCAP,
609 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
610 dev_printk(KERN_INFO, &child_dev->dev, "disabling ASPM"
611 " on pre-1.1 PCIe device. You can enable it"
612 " with 'pcie_aspm=force'\n");
620 * pcie_aspm_init_link_state: Initiate PCI express link state.
621 * It is called after the pcie and its children devices are scaned.
622 * @pdev: the root port or switch downstream port
624 void pcie_aspm_init_link_state(struct pci_dev *pdev)
627 struct pcie_link_state *link_state;
631 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
633 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
634 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
636 down_read(&pci_bus_sem);
637 if (list_empty(&pdev->subordinate->devices))
640 blacklist = !!pcie_aspm_sanity_check(pdev);
642 mutex_lock(&aspm_lock);
644 link_state = kzalloc(sizeof(*link_state), GFP_KERNEL);
648 link_state->downstream_has_switch = pcie_aspm_downstream_has_switch(pdev);
649 INIT_LIST_HEAD(&link_state->children);
650 INIT_LIST_HEAD(&link_state->link);
651 if (pdev->bus->self) {/* this is a switch */
652 struct pcie_link_state *parent_link_state;
654 parent_link_state = pdev->bus->parent->self->link_state;
655 if (!parent_link_state) {
659 list_add(&link_state->link, &parent_link_state->children);
660 link_state->parent = parent_link_state;
663 pdev->link_state = link_state;
666 pcie_aspm_configure_common_clock(pdev);
667 pcie_aspm_cap_init(pdev);
669 link_state->enabled_state = PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1;
670 link_state->bios_aspm_state = 0;
671 /* Set support state to 0, so we will disable ASPM later */
672 link_state->support_state = 0;
675 link_state->pdev = pdev;
676 list_add(&link_state->sibiling, &link_list);
678 if (link_state->downstream_has_switch) {
680 * If link has switch, delay the link config. The leaf link
681 * initialization will config the whole hierarchy. but we must
682 * make sure BIOS doesn't set unsupported link state
684 state = pcie_aspm_check_state(pdev, link_state->bios_aspm_state);
685 __pcie_aspm_config_link(pdev, state);
687 __pcie_aspm_configure_link_state(pdev,
688 policy_to_aspm_state(pdev));
690 pcie_check_clock_pm(pdev, blacklist);
694 free_link_state(pdev);
695 mutex_unlock(&aspm_lock);
697 up_read(&pci_bus_sem);
700 /* @pdev: the endpoint device */
701 void pcie_aspm_exit_link_state(struct pci_dev *pdev)
703 struct pci_dev *parent = pdev->bus->self;
704 struct pcie_link_state *link_state = parent->link_state;
706 if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
708 if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
709 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
711 down_read(&pci_bus_sem);
712 mutex_lock(&aspm_lock);
715 * All PCIe functions are in one slot, remove one function will remove
716 * the the whole slot, so just wait
718 if (!list_empty(&parent->subordinate->devices))
721 /* All functions are removed, so just disable ASPM for the link */
722 __pcie_aspm_config_one_dev(parent, 0);
723 list_del(&link_state->sibiling);
724 list_del(&link_state->link);
725 /* Clock PM is for endpoint device */
727 free_link_state(parent);
729 mutex_unlock(&aspm_lock);
730 up_read(&pci_bus_sem);
733 /* @pdev: the root port or switch downstream port */
734 void pcie_aspm_pm_state_change(struct pci_dev *pdev)
736 struct pcie_link_state *link_state = pdev->link_state;
738 if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
740 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
741 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
744 * devices changed PM state, we should recheck if latency meets all
745 * functions' requirement
747 pcie_aspm_configure_link_state(pdev, link_state->enabled_state);
751 * pci_disable_link_state - disable pci device's link state, so the link will
752 * never enter specific states
754 void pci_disable_link_state(struct pci_dev *pdev, int state)
756 struct pci_dev *parent = pdev->bus->self;
757 struct pcie_link_state *link_state;
759 if (aspm_disabled || !pdev->is_pcie)
761 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
762 pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
764 if (!parent || !parent->link_state)
767 down_read(&pci_bus_sem);
768 mutex_lock(&aspm_lock);
769 link_state = parent->link_state;
770 link_state->support_state &=
771 ~(state & (PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1));
772 if (state & PCIE_LINK_STATE_CLKPM)
773 link_state->clk_pm_capable = 0;
775 __pcie_aspm_configure_link_state(parent, link_state->enabled_state);
776 if (!link_state->clk_pm_capable && link_state->clk_pm_enabled)
777 pcie_set_clock_pm(parent, 0);
778 mutex_unlock(&aspm_lock);
779 up_read(&pci_bus_sem);
781 EXPORT_SYMBOL(pci_disable_link_state);
783 static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
786 struct pci_dev *pdev;
787 struct pcie_link_state *link_state;
789 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
790 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
792 if (i >= ARRAY_SIZE(policy_str))
794 if (i == aspm_policy)
797 down_read(&pci_bus_sem);
798 mutex_lock(&aspm_lock);
800 list_for_each_entry(link_state, &link_list, sibiling) {
801 pdev = link_state->pdev;
802 __pcie_aspm_configure_link_state(pdev,
803 policy_to_aspm_state(pdev));
804 if (link_state->clk_pm_capable &&
805 link_state->clk_pm_enabled != policy_to_clkpm_state(pdev))
806 pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
809 mutex_unlock(&aspm_lock);
810 up_read(&pci_bus_sem);
814 static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
817 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
818 if (i == aspm_policy)
819 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
821 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
825 module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
828 #ifdef CONFIG_PCIEASPM_DEBUG
829 static ssize_t link_state_show(struct device *dev,
830 struct device_attribute *attr,
833 struct pci_dev *pci_device = to_pci_dev(dev);
834 struct pcie_link_state *link_state = pci_device->link_state;
836 return sprintf(buf, "%d\n", link_state->enabled_state);
839 static ssize_t link_state_store(struct device *dev,
840 struct device_attribute *attr,
844 struct pci_dev *pci_device = to_pci_dev(dev);
850 if (state >= 0 && state <= 3) {
851 /* setup link aspm state */
852 pcie_aspm_configure_link_state(pci_device, state);
859 static ssize_t clk_ctl_show(struct device *dev,
860 struct device_attribute *attr,
863 struct pci_dev *pci_device = to_pci_dev(dev);
864 struct pcie_link_state *link_state = pci_device->link_state;
866 return sprintf(buf, "%d\n", link_state->clk_pm_enabled);
869 static ssize_t clk_ctl_store(struct device *dev,
870 struct device_attribute *attr,
874 struct pci_dev *pci_device = to_pci_dev(dev);
881 down_read(&pci_bus_sem);
882 mutex_lock(&aspm_lock);
883 pcie_set_clock_pm(pci_device, !!state);
884 mutex_unlock(&aspm_lock);
885 up_read(&pci_bus_sem);
890 static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
891 static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
893 static char power_group[] = "power";
894 void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
896 struct pcie_link_state *link_state = pdev->link_state;
898 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
899 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
902 if (link_state->support_state)
903 sysfs_add_file_to_group(&pdev->dev.kobj,
904 &dev_attr_link_state.attr, power_group);
905 if (link_state->clk_pm_capable)
906 sysfs_add_file_to_group(&pdev->dev.kobj,
907 &dev_attr_clk_ctl.attr, power_group);
910 void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
912 struct pcie_link_state *link_state = pdev->link_state;
914 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
915 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
918 if (link_state->support_state)
919 sysfs_remove_file_from_group(&pdev->dev.kobj,
920 &dev_attr_link_state.attr, power_group);
921 if (link_state->clk_pm_capable)
922 sysfs_remove_file_from_group(&pdev->dev.kobj,
923 &dev_attr_clk_ctl.attr, power_group);
927 static int __init pcie_aspm_disable(char *str)
929 if (!strcmp(str, "off")) {
931 printk(KERN_INFO "PCIe ASPM is disabled\n");
932 } else if (!strcmp(str, "force")) {
934 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
939 __setup("pcie_aspm=", pcie_aspm_disable);
941 void pcie_no_aspm(void)
948 * pcie_aspm_enabled - is PCIe ASPM enabled?
950 * Returns true if ASPM has not been disabled by the command-line option
953 int pcie_aspm_enabled(void)
955 return !aspm_disabled;
957 EXPORT_SYMBOL(pcie_aspm_enabled);