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