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