blob: 0800c51468265ca42c8a93344d7b94692bc18ac8 [file] [log] [blame]
Shaohua Li7d715a62008-02-25 09:46:41 +08001/*
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 Renninger2a42d9d2008-12-09 13:05:09 +010019#include <linux/jiffies.h>
Andrew Patterson987a4c72009-01-05 16:21:04 -070020#include <linux/delay.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080021#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 Kaneshigeb6c2e542009-05-13 12:14:58 +090029struct aspm_latency {
30 u32 l0s; /* L0s latency (nsec) */
31 u32 l1; /* L1 latency (nsec) */
Shaohua Li7d715a62008-02-25 09:46:41 +080032};
33
34struct pcie_link_state {
Kenji Kaneshige5cde89d2009-05-13 12:17:04 +090035 struct pci_dev *pdev; /* Upstream component of the Link */
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +090036 struct pcie_link_state *root; /* pointer to the root port link */
Kenji Kaneshige5cde89d2009-05-13 12:17:04 +090037 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 Li7d715a62008-02-25 09:46:41 +080041
42 /* ASPM state */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090043 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 Kaneshige4d246e42009-05-13 12:15:38 +090047 /* 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 Kaneshigeb6c2e542009-05-13 12:14:58 +090052 /* Latencies */
53 struct aspm_latency latency; /* Exit latency */
Shaohua Li7d715a62008-02-25 09:46:41 +080054 /*
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090055 * Endpoint acceptable latencies. A pcie downstream port only
56 * has one slot under it, so at most there are 8 functions.
Shaohua Li7d715a62008-02-25 09:46:41 +080057 */
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090058 struct aspm_latency acceptable[8];
Shaohua Li7d715a62008-02-25 09:46:41 +080059};
60
Shaohua Lid6d38572008-07-23 10:32:42 +080061static int aspm_disabled, aspm_force;
Shaohua Li7d715a62008-02-25 09:46:41 +080062static DEFINE_MUTEX(aspm_lock);
63static 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 */
68static int aspm_policy;
69static const char *policy_str[] = {
70 [POLICY_DEFAULT] = "default",
71 [POLICY_PERFORMANCE] = "performance",
72 [POLICY_POWERSAVE] = "powersave"
73};
74
Andrew Patterson987a4c72009-01-05 16:21:04 -070075#define LINK_RETRAIN_TIMEOUT HZ
76
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090077static int policy_to_aspm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080078{
Shaohua Li7d715a62008-02-25 09:46:41 +080079 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 Kaneshige80bfdbe2009-05-13 12:12:43 +090085 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Shaohua Li7d715a62008-02-25 09:46:41 +080086 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090087 return link->aspm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +080088 }
89 return 0;
90}
91
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090092static int policy_to_clkpm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080093{
Shaohua Li7d715a62008-02-25 09:46:41 +080094 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 Kaneshige5aa63582009-05-13 12:17:44 +0900102 return link->clkpm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +0800103 }
104 return 0;
105}
106
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900107static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800108{
Shaohua Li7d715a62008-02-25 09:46:41 +0800109 int pos;
110 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900111 struct pci_dev *child;
112 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800113
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900114 list_for_each_entry(child, &linkbus->devices, bus_list) {
115 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800116 if (!pos)
117 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900118 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800119 if (enable)
120 reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
121 else
122 reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900123 pci_write_config_word(child, pos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800124 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900125 link->clkpm_enabled = !!enable;
Shaohua Li7d715a62008-02-25 09:46:41 +0800126}
127
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900128static 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 Kaneshige8d349ac2009-05-13 12:18:22 +0900139static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800140{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900141 int pos, capable = 1, enabled = 1;
Shaohua Li7d715a62008-02-25 09:46:41 +0800142 u32 reg32;
143 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900144 struct pci_dev *child;
145 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800146
147 /* All functions should have the same cap and state, take the worst */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900148 list_for_each_entry(child, &linkbus->devices, bus_list) {
149 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800150 if (!pos)
151 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900152 pci_read_config_dword(child, pos + PCI_EXP_LNKCAP, &reg32);
Shaohua Li7d715a62008-02-25 09:46:41 +0800153 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
154 capable = 0;
155 enabled = 0;
156 break;
157 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900158 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800159 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
160 enabled = 0;
161 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900162 link->clkpm_enabled = enabled;
163 link->clkpm_default = enabled;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900164 link->clkpm_capable = (blacklist) ? 0 : capable;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800165}
166
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900167static bool pcie_aspm_downstream_has_switch(struct pcie_link_state *link)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800168{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900169 struct pci_dev *child;
170 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800171
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900172 list_for_each_entry(child, &linkbus->devices, bus_list) {
173 if (child->pcie_type == PCI_EXP_TYPE_UPSTREAM)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800174 return true;
175 }
176 return false;
Shaohua Li7d715a62008-02-25 09:46:41 +0800177}
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 Kaneshige5aa63582009-05-13 12:17:44 +0900184static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800185{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900186 int ppos, cpos, same_clock = 1;
187 u16 reg16, parent_reg, child_reg[8];
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100188 unsigned long start_jiffies;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900189 struct pci_dev *child, *parent = link->pdev;
190 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800191 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900192 * All functions of a slot should have the same Slot Clock
Shaohua Li7d715a62008-02-25 09:46:41 +0800193 * Configuration, so just check one function
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900194 */
195 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
196 BUG_ON(!child->is_pcie);
Shaohua Li7d715a62008-02-25 09:46:41 +0800197
198 /* Check downstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900199 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
200 pci_read_config_word(child, cpos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800201 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
202 same_clock = 0;
203
204 /* Check upstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900205 ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
206 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800207 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
208 same_clock = 0;
209
210 /* Configure downstream component, all functions */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900211 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, &reg16);
214 child_reg[PCI_FUNC(child->devfn)] = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800215 if (same_clock)
216 reg16 |= PCI_EXP_LNKCTL_CCC;
217 else
218 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900219 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800220 }
221
222 /* Configure upstream component */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900223 pci_read_config_word(parent, ppos + PCI_EXP_LNKCTL, &reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100224 parent_reg = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800225 if (same_clock)
226 reg16 |= PCI_EXP_LNKCTL_CCC;
227 else
228 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900229 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800230
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900231 /* Retrain link */
Shaohua Li7d715a62008-02-25 09:46:41 +0800232 reg16 |= PCI_EXP_LNKCTL_RL;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900233 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800234
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900235 /* Wait for link training end. Break out after waiting for timeout */
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100236 start_jiffies = jiffies;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700237 for (;;) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900238 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800239 if (!(reg16 & PCI_EXP_LNKSTA_LT))
240 break;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700241 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
242 break;
243 msleep(1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800244 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900245 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 Renninger2a42d9d2008-12-09 13:05:09 +0100255 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900256 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, parent_reg);
Shaohua Li7d715a62008-02-25 09:46:41 +0800257}
258
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900259/* Convert L0s latency encoding to ns */
260static u32 calc_l0s_latency(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800261{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900262 if (encoding == 0x7)
263 return (5 * 1000); /* > 4us */
264 return (64 << encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800265}
266
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900267/* Convert L0s acceptable latency encoding to ns */
268static u32 calc_l0s_acceptable(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800269{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900270 if (encoding == 0x7)
271 return -1U;
272 return (64 << encoding);
273}
Shaohua Li7d715a62008-02-25 09:46:41 +0800274
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900275/* Convert L1 latency encoding to ns */
276static 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 */
284static u32 calc_l1_acceptable(u32 encoding)
285{
286 if (encoding == 0x7)
287 return -1U;
288 return (1000 << encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800289}
290
291static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900292 u32 *l0s, u32 *l1, u32 *enabled)
Shaohua Li7d715a62008-02-25 09:46:41 +0800293{
294 int pos;
295 u16 reg16;
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900296 u32 reg32, encoding;
Shaohua Li7d715a62008-02-25 09:46:41 +0800297
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900298 *l0s = *l1 = *enabled = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800299 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
300 pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
301 *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
302 if (*state != PCIE_LINK_STATE_L0S &&
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900303 *state != (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L0S))
Shaohua Li7d715a62008-02-25 09:46:41 +0800304 *state = 0;
305 if (*state == 0)
306 return;
307
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900308 encoding = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
309 *l0s = calc_l0s_latency(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800310 if (*state & PCIE_LINK_STATE_L1) {
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900311 encoding = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
312 *l1 = calc_l1_latency(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800313 }
314 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900315 *enabled = reg16 & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800316}
317
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900318static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800319{
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900320 u32 support, l0s, l1, enabled;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900321 struct pci_dev *child, *parent = link->pdev;
322 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800323
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900324 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 Li7d715a62008-02-25 09:46:41 +0800335 /* upstream component states */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900336 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 Kaneshige80bfdbe2009-05-13 12:12:43 +0900341
Shaohua Li7d715a62008-02-25 09:46:41 +0800342 /* downstream component states, all functions have the same setting */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900343 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 Kaneshige80bfdbe2009-05-13 12:12:43 +0900348
Kenji Kaneshigeb127bd52009-08-19 10:57:31 +0900349 /* Save default state */
350 link->aspm_default = link->aspm_enabled;
351
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900352 if (!link->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800353 return;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900354
Shaohua Li7d715a62008-02-25 09:46:41 +0800355 /* ENDPOINT states*/
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900356 list_for_each_entry(child, &linkbus->devices, bus_list) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800357 int pos;
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900358 u32 reg32, encoding;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900359 struct aspm_latency *acceptable =
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900360 &link->acceptable[PCI_FUNC(child->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800361
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900362 if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
363 child->pcie_type != PCI_EXP_TYPE_LEG_END)
Shaohua Li7d715a62008-02-25 09:46:41 +0800364 continue;
365
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900366 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
367 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900368 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
369 acceptable->l0s = calc_l0s_acceptable(encoding);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900370 if (link->aspm_support & PCIE_LINK_STATE_L1) {
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900371 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
372 acceptable->l1 = calc_l1_acceptable(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800373 }
374 }
375}
376
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900377/**
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 */
387static u32 __pcie_aspm_check_state_one(struct pci_dev *endpoint, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800388{
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900389 u32 l1_switch_latency = 0;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900390 struct aspm_latency *acceptable;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900391 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800392
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900393 link = endpoint->bus->self->link_state;
394 state &= link->aspm_support;
395 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800396
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900397 while (link && state) {
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900398 if ((state & PCIE_LINK_STATE_L0S) &&
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900399 (link->latency.l0s > acceptable->l0s))
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900400 state &= ~PCIE_LINK_STATE_L0S;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900401 if ((state & PCIE_LINK_STATE_L1) &&
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900402 (link->latency.l1 + l1_switch_latency > acceptable->l1))
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900403 state &= ~PCIE_LINK_STATE_L1;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900404 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 Li7d715a62008-02-25 09:46:41 +0800410 }
411 return state;
412}
413
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900414static u32 pcie_aspm_check_state(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800415{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900416 pci_power_t power_state;
417 struct pci_dev *child;
418 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800419
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800420 /* If no child, ignore the link */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900421 if (list_empty(&linkbus->devices))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800422 return state;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900423
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 Li7d715a62008-02-25 09:46:41 +0800434 continue;
435 /* Device not in D0 doesn't need check latency */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900436 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 Li7d715a62008-02-25 09:46:41 +0800439 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900440 state = __pcie_aspm_check_state_one(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800441 }
442 return state;
443}
444
445static 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, &reg16);
451 reg16 &= ~0x3;
452 reg16 |= state;
453 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
454}
455
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900456static void __pcie_aspm_config_link(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800457{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900458 struct pci_dev *child, *parent = link->pdev;
459 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800460
461 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900462 * If the downstream component has pci bridge function, don't
463 * do ASPM now.
Shaohua Li7d715a62008-02-25 09:46:41 +0800464 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900465 list_for_each_entry(child, &linkbus->devices, bus_list) {
466 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
467 return;
Shaohua Li7d715a62008-02-25 09:46:41 +0800468 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800469 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900470 * Spec 2.0 suggests all functions should be configured the
471 * same setting for ASPM. Enabling ASPM L1 should be done in
472 * upstream component first and then downstream, and vice
473 * versa for disabling ASPM L1. Spec doesn't mention L0S.
Shaohua Li7d715a62008-02-25 09:46:41 +0800474 */
475 if (state & PCIE_LINK_STATE_L1)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900476 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800477
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900478 list_for_each_entry(child, &linkbus->devices, bus_list)
479 __pcie_aspm_config_one_dev(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800480
481 if (!(state & PCIE_LINK_STATE_L1))
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900482 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800483
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900484 link->aspm_enabled = state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800485}
486
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900487/* Check the whole hierarchy, and configure each link in the hierarchy */
488static void __pcie_aspm_configure_link_state(struct pcie_link_state *link,
489 u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800490{
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900491 struct pcie_link_state *leaf, *root = link->root;
Shaohua Li7d715a62008-02-25 09:46:41 +0800492
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900493 state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800494
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900495 /* Check all links who have specific root port link */
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900496 list_for_each_entry(leaf, &link_list, sibling) {
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900497 if (!list_empty(&leaf->children) || (leaf->root != root))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800498 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900499 state = pcie_aspm_check_state(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800500 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900501 /* Check root port link too in case it hasn't children */
502 state = pcie_aspm_check_state(root, state);
503 if (link->aspm_enabled == state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800504 return;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800505 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900506 * We must change the hierarchy. See comments in
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800507 * __pcie_aspm_config_link for the order
508 **/
509 if (state & PCIE_LINK_STATE_L1) {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900510 list_for_each_entry(leaf, &link_list, sibling) {
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900511 if (leaf->root == root)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900512 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800513 }
514 } else {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900515 list_for_each_entry_reverse(leaf, &link_list, sibling) {
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900516 if (leaf->root == root)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900517 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800518 }
519 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800520}
521
522/*
523 * pcie_aspm_configure_link_state: enable/disable PCI express link state
524 * @pdev: the root port or switch downstream port
525 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900526static void pcie_aspm_configure_link_state(struct pcie_link_state *link,
527 u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800528{
529 down_read(&pci_bus_sem);
530 mutex_lock(&aspm_lock);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900531 __pcie_aspm_configure_link_state(link, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800532 mutex_unlock(&aspm_lock);
533 up_read(&pci_bus_sem);
534}
535
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900536static void free_link_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800537{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900538 link->pdev->link_state = NULL;
539 kfree(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800540}
541
Shaohua Liddc97532008-05-21 16:58:40 +0800542static int pcie_aspm_sanity_check(struct pci_dev *pdev)
543{
Kenji Kaneshige36475842009-05-13 12:23:09 +0900544 struct pci_dev *child;
545 int pos;
Shaohua Li149e1632008-07-23 10:32:31 +0800546 u32 reg32;
Shaohua Liddc97532008-05-21 16:58:40 +0800547 /*
Kenji Kaneshige36475842009-05-13 12:23:09 +0900548 * Some functions in a slot might not all be PCIE functions,
549 * very strange. Disable ASPM for the whole slot
Shaohua Liddc97532008-05-21 16:58:40 +0800550 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900551 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
552 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
553 if (!pos)
Shaohua Liddc97532008-05-21 16:58:40 +0800554 return -EINVAL;
Shaohua Li149e1632008-07-23 10:32:31 +0800555 /*
556 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
557 * RBER bit to determine if a function is 1.1 version device
558 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900559 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Sitsofe Wheelere1f4f592008-09-16 14:27:13 +0100560 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
Kenji Kaneshige36475842009-05-13 12:23:09 +0900561 dev_printk(KERN_INFO, &child->dev, "disabling ASPM"
Vincent Legollf393d9b2008-10-12 12:26:12 +0200562 " on pre-1.1 PCIe device. You can enable it"
563 " with 'pcie_aspm=force'\n");
Shaohua Li149e1632008-07-23 10:32:31 +0800564 return -EINVAL;
565 }
Shaohua Liddc97532008-05-21 16:58:40 +0800566 }
567 return 0;
568}
569
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900570static struct pcie_link_state *pcie_aspm_setup_link_state(struct pci_dev *pdev)
571{
572 struct pcie_link_state *link;
573 int blacklist = !!pcie_aspm_sanity_check(pdev);
574
575 link = kzalloc(sizeof(*link), GFP_KERNEL);
576 if (!link)
577 return NULL;
578 INIT_LIST_HEAD(&link->sibling);
579 INIT_LIST_HEAD(&link->children);
580 INIT_LIST_HEAD(&link->link);
581 link->pdev = pdev;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900582 if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
583 struct pcie_link_state *parent;
584 parent = pdev->bus->parent->self->link_state;
585 if (!parent) {
586 kfree(link);
587 return NULL;
588 }
589 link->parent = parent;
590 list_add(&link->link, &parent->children);
591 }
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900592 /* Setup a pointer to the root port link */
593 if (!link->parent)
594 link->root = link;
595 else
596 link->root = link->parent->root;
597
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900598 list_add(&link->sibling, &link_list);
599
600 pdev->link_state = link;
601
602 /* Check ASPM capability */
603 pcie_aspm_cap_init(link, blacklist);
604
605 /* Check Clock PM capability */
606 pcie_clkpm_cap_init(link, blacklist);
607
608 return link;
609}
610
Shaohua Li7d715a62008-02-25 09:46:41 +0800611/*
612 * pcie_aspm_init_link_state: Initiate PCI express link state.
613 * It is called after the pcie and its children devices are scaned.
614 * @pdev: the root port or switch downstream port
615 */
616void pcie_aspm_init_link_state(struct pci_dev *pdev)
617{
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900618 u32 state;
619 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800620
621 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
622 return;
623 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900624 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
Shaohua Li7d715a62008-02-25 09:46:41 +0800625 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900626
Shaohua Li8e822df2009-06-08 09:27:25 +0800627 /* VIA has a strange chipset, root port is under a bridge */
628 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900629 pdev->bus->self)
Shaohua Li8e822df2009-06-08 09:27:25 +0800630 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900631
Shaohua Li7d715a62008-02-25 09:46:41 +0800632 down_read(&pci_bus_sem);
633 if (list_empty(&pdev->subordinate->devices))
634 goto out;
635
Shaohua Li7d715a62008-02-25 09:46:41 +0800636 mutex_lock(&aspm_lock);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900637 link = pcie_aspm_setup_link_state(pdev);
638 if (!link)
639 goto unlock;
640 /*
641 * Setup initial ASPM state
642 *
643 * If link has switch, delay the link config. The leaf link
644 * initialization will config the whole hierarchy. But we must
645 * make sure BIOS doesn't set unsupported link state.
646 */
Kenji Kaneshigeefdf82882009-05-13 12:22:26 +0900647 if (pcie_aspm_downstream_has_switch(link)) {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900648 state = pcie_aspm_check_state(link, link->aspm_default);
649 __pcie_aspm_config_link(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800650 } else {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900651 state = policy_to_aspm_state(link);
652 __pcie_aspm_configure_link_state(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800653 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800654
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900655 /* Setup initial Clock PM state */
656 state = (link->clkpm_capable) ? policy_to_clkpm_state(link) : 0;
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900657 pcie_set_clkpm(link, state);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900658unlock:
Shaohua Li7d715a62008-02-25 09:46:41 +0800659 mutex_unlock(&aspm_lock);
660out:
661 up_read(&pci_bus_sem);
662}
663
664/* @pdev: the endpoint device */
665void pcie_aspm_exit_link_state(struct pci_dev *pdev)
666{
667 struct pci_dev *parent = pdev->bus->self;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900668 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800669
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900670 if (aspm_disabled || !pdev->is_pcie || !parent || !parent->link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800671 return;
672 if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900673 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
Shaohua Li7d715a62008-02-25 09:46:41 +0800674 return;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900675
Shaohua Li7d715a62008-02-25 09:46:41 +0800676 down_read(&pci_bus_sem);
677 mutex_lock(&aspm_lock);
Shaohua Li7d715a62008-02-25 09:46:41 +0800678 /*
679 * All PCIe functions are in one slot, remove one function will remove
Alex Chiang3419c752009-01-28 14:59:18 -0700680 * the whole slot, so just wait until we are the last function left.
Shaohua Li7d715a62008-02-25 09:46:41 +0800681 */
Alex Chiang3419c752009-01-28 14:59:18 -0700682 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
Shaohua Li7d715a62008-02-25 09:46:41 +0800683 goto out;
684
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900685 link = parent->link_state;
686
Shaohua Li7d715a62008-02-25 09:46:41 +0800687 /* All functions are removed, so just disable ASPM for the link */
688 __pcie_aspm_config_one_dev(parent, 0);
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900689 list_del(&link->sibling);
690 list_del(&link->link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800691 /* Clock PM is for endpoint device */
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900692 free_link_state(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800693out:
694 mutex_unlock(&aspm_lock);
695 up_read(&pci_bus_sem);
696}
697
698/* @pdev: the root port or switch downstream port */
699void pcie_aspm_pm_state_change(struct pci_dev *pdev)
700{
701 struct pcie_link_state *link_state = pdev->link_state;
702
703 if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
704 return;
705 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
706 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
707 return;
708 /*
709 * devices changed PM state, we should recheck if latency meets all
710 * functions' requirement
711 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900712 pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800713}
714
715/*
716 * pci_disable_link_state - disable pci device's link state, so the link will
717 * never enter specific states
718 */
719void pci_disable_link_state(struct pci_dev *pdev, int state)
720{
721 struct pci_dev *parent = pdev->bus->self;
722 struct pcie_link_state *link_state;
723
724 if (aspm_disabled || !pdev->is_pcie)
725 return;
726 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
727 pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
728 parent = pdev;
729 if (!parent || !parent->link_state)
730 return;
731
732 down_read(&pci_bus_sem);
733 mutex_lock(&aspm_lock);
734 link_state = parent->link_state;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900735 link_state->aspm_support &= ~state;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900736 __pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900737 if (state & PCIE_LINK_STATE_CLKPM) {
738 link_state->clkpm_capable = 0;
739 pcie_set_clkpm(link_state, 0);
740 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800741 mutex_unlock(&aspm_lock);
742 up_read(&pci_bus_sem);
743}
744EXPORT_SYMBOL(pci_disable_link_state);
745
746static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
747{
748 int i;
Shaohua Li7d715a62008-02-25 09:46:41 +0800749 struct pcie_link_state *link_state;
750
751 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
752 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
753 break;
754 if (i >= ARRAY_SIZE(policy_str))
755 return -EINVAL;
756 if (i == aspm_policy)
757 return 0;
758
759 down_read(&pci_bus_sem);
760 mutex_lock(&aspm_lock);
761 aspm_policy = i;
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900762 list_for_each_entry(link_state, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900763 __pcie_aspm_configure_link_state(link_state,
764 policy_to_aspm_state(link_state));
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900765 pcie_set_clkpm(link_state, policy_to_clkpm_state(link_state));
Shaohua Li7d715a62008-02-25 09:46:41 +0800766 }
767 mutex_unlock(&aspm_lock);
768 up_read(&pci_bus_sem);
769 return 0;
770}
771
772static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
773{
774 int i, cnt = 0;
775 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
776 if (i == aspm_policy)
777 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
778 else
779 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
780 return cnt;
781}
782
783module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
784 NULL, 0644);
785
786#ifdef CONFIG_PCIEASPM_DEBUG
787static ssize_t link_state_show(struct device *dev,
788 struct device_attribute *attr,
789 char *buf)
790{
791 struct pci_dev *pci_device = to_pci_dev(dev);
792 struct pcie_link_state *link_state = pci_device->link_state;
793
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900794 return sprintf(buf, "%d\n", link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800795}
796
797static ssize_t link_state_store(struct device *dev,
798 struct device_attribute *attr,
799 const char *buf,
800 size_t n)
801{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900802 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800803 int state;
804
805 if (n < 1)
806 return -EINVAL;
807 state = buf[0]-'0';
808 if (state >= 0 && state <= 3) {
809 /* setup link aspm state */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900810 pcie_aspm_configure_link_state(pdev->link_state, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800811 return n;
812 }
813
814 return -EINVAL;
815}
816
817static ssize_t clk_ctl_show(struct device *dev,
818 struct device_attribute *attr,
819 char *buf)
820{
821 struct pci_dev *pci_device = to_pci_dev(dev);
822 struct pcie_link_state *link_state = pci_device->link_state;
823
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900824 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800825}
826
827static ssize_t clk_ctl_store(struct device *dev,
828 struct device_attribute *attr,
829 const char *buf,
830 size_t n)
831{
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900832 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800833 int state;
834
835 if (n < 1)
836 return -EINVAL;
837 state = buf[0]-'0';
838
839 down_read(&pci_bus_sem);
840 mutex_lock(&aspm_lock);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900841 pcie_set_clkpm_nocheck(pdev->link_state, !!state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800842 mutex_unlock(&aspm_lock);
843 up_read(&pci_bus_sem);
844
845 return n;
846}
847
848static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
849static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
850
851static char power_group[] = "power";
852void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
853{
854 struct pcie_link_state *link_state = pdev->link_state;
855
856 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
857 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
858 return;
859
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900860 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800861 sysfs_add_file_to_group(&pdev->dev.kobj,
862 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900863 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800864 sysfs_add_file_to_group(&pdev->dev.kobj,
865 &dev_attr_clk_ctl.attr, power_group);
866}
867
868void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
869{
870 struct pcie_link_state *link_state = pdev->link_state;
871
872 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
873 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
874 return;
875
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900876 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800877 sysfs_remove_file_from_group(&pdev->dev.kobj,
878 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900879 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800880 sysfs_remove_file_from_group(&pdev->dev.kobj,
881 &dev_attr_clk_ctl.attr, power_group);
882}
883#endif
884
885static int __init pcie_aspm_disable(char *str)
886{
Shaohua Lid6d38572008-07-23 10:32:42 +0800887 if (!strcmp(str, "off")) {
888 aspm_disabled = 1;
889 printk(KERN_INFO "PCIe ASPM is disabled\n");
890 } else if (!strcmp(str, "force")) {
891 aspm_force = 1;
892 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
893 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800894 return 1;
895}
896
Shaohua Lid6d38572008-07-23 10:32:42 +0800897__setup("pcie_aspm=", pcie_aspm_disable);
Shaohua Li7d715a62008-02-25 09:46:41 +0800898
Shaohua Li5fde2442008-07-23 10:32:24 +0800899void pcie_no_aspm(void)
900{
Shaohua Lid6d38572008-07-23 10:32:42 +0800901 if (!aspm_force)
902 aspm_disabled = 1;
Shaohua Li5fde2442008-07-23 10:32:24 +0800903}
904
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700905/**
906 * pcie_aspm_enabled - is PCIe ASPM enabled?
907 *
908 * Returns true if ASPM has not been disabled by the command-line option
909 * pcie_aspm=off.
910 **/
911int pcie_aspm_enabled(void)
Shaohua Li7d715a62008-02-25 09:46:41 +0800912{
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700913 return !aspm_disabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800914}
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700915EXPORT_SYMBOL(pcie_aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800916