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