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