blob: d85f77ff150cfffbb6d8109b2ccd26468598a261 [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 */
36 struct pcie_link_state *parent; /* pointer to the parent Link state */
37 struct list_head sibling; /* node in link_list */
38 struct list_head children; /* list of child link states */
39 struct list_head link; /* node in parent's children list */
Shaohua Li7d715a62008-02-25 09:46:41 +080040
41 /* ASPM state */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090042 u32 aspm_support:2; /* Supported ASPM state */
43 u32 aspm_enabled:2; /* Enabled ASPM state */
44 u32 aspm_default:2; /* Default ASPM state by BIOS */
45
Kenji Kaneshige4d246e42009-05-13 12:15:38 +090046 /* Clock PM state */
47 u32 clkpm_capable:1; /* Clock PM capable? */
48 u32 clkpm_enabled:1; /* Current Clock PM state */
49 u32 clkpm_default:1; /* Default Clock PM state by BIOS */
50
Kenji Kaneshige5cde89d2009-05-13 12:17:04 +090051 u32 has_switch:1; /* Downstream has switches? */
52
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090053 /* Latencies */
54 struct aspm_latency latency; /* Exit latency */
Shaohua Li7d715a62008-02-25 09:46:41 +080055 /*
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090056 * Endpoint acceptable latencies. A pcie downstream port only
57 * has one slot under it, so at most there are 8 functions.
Shaohua Li7d715a62008-02-25 09:46:41 +080058 */
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090059 struct aspm_latency acceptable[8];
Shaohua Li7d715a62008-02-25 09:46:41 +080060};
61
Shaohua Lid6d38572008-07-23 10:32:42 +080062static int aspm_disabled, aspm_force;
Shaohua Li7d715a62008-02-25 09:46:41 +080063static DEFINE_MUTEX(aspm_lock);
64static LIST_HEAD(link_list);
65
66#define POLICY_DEFAULT 0 /* BIOS default setting */
67#define POLICY_PERFORMANCE 1 /* high performance */
68#define POLICY_POWERSAVE 2 /* high power saving */
69static int aspm_policy;
70static const char *policy_str[] = {
71 [POLICY_DEFAULT] = "default",
72 [POLICY_PERFORMANCE] = "performance",
73 [POLICY_POWERSAVE] = "powersave"
74};
75
Andrew Patterson987a4c72009-01-05 16:21:04 -070076#define LINK_RETRAIN_TIMEOUT HZ
77
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090078static int policy_to_aspm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080079{
Shaohua Li7d715a62008-02-25 09:46:41 +080080 switch (aspm_policy) {
81 case POLICY_PERFORMANCE:
82 /* Disable ASPM and Clock PM */
83 return 0;
84 case POLICY_POWERSAVE:
85 /* Enable ASPM L0s/L1 */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090086 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Shaohua Li7d715a62008-02-25 09:46:41 +080087 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090088 return link->aspm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +080089 }
90 return 0;
91}
92
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090093static int policy_to_clkpm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080094{
Shaohua Li7d715a62008-02-25 09:46:41 +080095 switch (aspm_policy) {
96 case POLICY_PERFORMANCE:
97 /* Disable ASPM and Clock PM */
98 return 0;
99 case POLICY_POWERSAVE:
100 /* Disable Clock PM */
101 return 1;
102 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900103 return link->clkpm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +0800104 }
105 return 0;
106}
107
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900108static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800109{
Shaohua Li7d715a62008-02-25 09:46:41 +0800110 int pos;
111 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900112 struct pci_dev *child;
113 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800114
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900115 list_for_each_entry(child, &linkbus->devices, bus_list) {
116 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800117 if (!pos)
118 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900119 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800120 if (enable)
121 reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
122 else
123 reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900124 pci_write_config_word(child, pos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800125 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900126 link->clkpm_enabled = !!enable;
Shaohua Li7d715a62008-02-25 09:46:41 +0800127}
128
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900129static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
130{
131 /* Don't enable Clock PM if the link is not Clock PM capable */
132 if (!link->clkpm_capable && enable)
133 return;
134 /* Need nothing if the specified equals to current state */
135 if (link->clkpm_enabled == enable)
136 return;
137 pcie_set_clkpm_nocheck(link, enable);
138}
139
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900140static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800141{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900142 int pos, capable = 1, enabled = 1;
Shaohua Li7d715a62008-02-25 09:46:41 +0800143 u32 reg32;
144 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900145 struct pci_dev *child;
146 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800147
148 /* All functions should have the same cap and state, take the worst */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900149 list_for_each_entry(child, &linkbus->devices, bus_list) {
150 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800151 if (!pos)
152 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900153 pci_read_config_dword(child, pos + PCI_EXP_LNKCAP, &reg32);
Shaohua Li7d715a62008-02-25 09:46:41 +0800154 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
155 capable = 0;
156 enabled = 0;
157 break;
158 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900159 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800160 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
161 enabled = 0;
162 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900163 link->clkpm_enabled = enabled;
164 link->clkpm_default = enabled;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900165 link->clkpm_capable = (blacklist) ? 0 : capable;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800166}
167
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900168static bool pcie_aspm_downstream_has_switch(struct pcie_link_state *link)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800169{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900170 struct pci_dev *child;
171 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800172
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900173 list_for_each_entry(child, &linkbus->devices, bus_list) {
174 if (child->pcie_type == PCI_EXP_TYPE_UPSTREAM)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800175 return true;
176 }
177 return false;
Shaohua Li7d715a62008-02-25 09:46:41 +0800178}
179
180/*
181 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
182 * could use common clock. If they are, configure them to use the
183 * common clock. That will reduce the ASPM state exit latency.
184 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900185static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800186{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900187 int ppos, cpos, same_clock = 1;
188 u16 reg16, parent_reg, child_reg[8];
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100189 unsigned long start_jiffies;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900190 struct pci_dev *child, *parent = link->pdev;
191 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800192 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900193 * All functions of a slot should have the same Slot Clock
Shaohua Li7d715a62008-02-25 09:46:41 +0800194 * Configuration, so just check one function
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900195 */
196 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
197 BUG_ON(!child->is_pcie);
Shaohua Li7d715a62008-02-25 09:46:41 +0800198
199 /* Check downstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900200 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
201 pci_read_config_word(child, cpos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800202 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
203 same_clock = 0;
204
205 /* Check upstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900206 ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
207 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800208 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
209 same_clock = 0;
210
211 /* Configure downstream component, all functions */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900212 list_for_each_entry(child, &linkbus->devices, bus_list) {
213 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
214 pci_read_config_word(child, cpos + PCI_EXP_LNKCTL, &reg16);
215 child_reg[PCI_FUNC(child->devfn)] = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800216 if (same_clock)
217 reg16 |= PCI_EXP_LNKCTL_CCC;
218 else
219 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900220 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800221 }
222
223 /* Configure upstream component */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900224 pci_read_config_word(parent, ppos + PCI_EXP_LNKCTL, &reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100225 parent_reg = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800226 if (same_clock)
227 reg16 |= PCI_EXP_LNKCTL_CCC;
228 else
229 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900230 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800231
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900232 /* Retrain link */
Shaohua Li7d715a62008-02-25 09:46:41 +0800233 reg16 |= PCI_EXP_LNKCTL_RL;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900234 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800235
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900236 /* Wait for link training end. Break out after waiting for timeout */
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100237 start_jiffies = jiffies;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700238 for (;;) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900239 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800240 if (!(reg16 & PCI_EXP_LNKSTA_LT))
241 break;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700242 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
243 break;
244 msleep(1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800245 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900246 if (!(reg16 & PCI_EXP_LNKSTA_LT))
247 return;
248
249 /* Training failed. Restore common clock configurations */
250 dev_printk(KERN_ERR, &parent->dev,
251 "ASPM: Could not configure common clock\n");
252 list_for_each_entry(child, &linkbus->devices, bus_list) {
253 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
254 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL,
255 child_reg[PCI_FUNC(child->devfn)]);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100256 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900257 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, parent_reg);
Shaohua Li7d715a62008-02-25 09:46:41 +0800258}
259
260/*
261 * calc_L0S_latency: Convert L0s latency encoding to ns
262 */
263static unsigned int calc_L0S_latency(unsigned int latency_encoding, int ac)
264{
265 unsigned int ns = 64;
266
267 if (latency_encoding == 0x7) {
268 if (ac)
269 ns = -1U;
270 else
271 ns = 5*1000; /* > 4us */
272 } else
273 ns *= (1 << latency_encoding);
274 return ns;
275}
276
277/*
278 * calc_L1_latency: Convert L1 latency encoding to ns
279 */
280static unsigned int calc_L1_latency(unsigned int latency_encoding, int ac)
281{
282 unsigned int ns = 1000;
283
284 if (latency_encoding == 0x7) {
285 if (ac)
286 ns = -1U;
287 else
288 ns = 65*1000; /* > 64us */
289 } else
290 ns *= (1 << latency_encoding);
291 return ns;
292}
293
294static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900295 u32 *l0s, u32 *l1, u32 *enabled)
Shaohua Li7d715a62008-02-25 09:46:41 +0800296{
297 int pos;
298 u16 reg16;
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900299 u32 reg32, latency;
Shaohua Li7d715a62008-02-25 09:46:41 +0800300
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900301 *l0s = *l1 = *enabled = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800302 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
303 pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
304 *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
305 if (*state != PCIE_LINK_STATE_L0S &&
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900306 *state != (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L0S))
Shaohua Li7d715a62008-02-25 09:46:41 +0800307 *state = 0;
308 if (*state == 0)
309 return;
310
311 latency = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
312 *l0s = calc_L0S_latency(latency, 0);
313 if (*state & PCIE_LINK_STATE_L1) {
314 latency = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
315 *l1 = calc_L1_latency(latency, 0);
316 }
317 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900318 *enabled = reg16 & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800319}
320
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900321static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800322{
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900323 u32 support, l0s, l1, enabled;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900324 struct pci_dev *child, *parent = link->pdev;
325 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800326
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900327 if (blacklist) {
328 /* Set support state to 0, so we will disable ASPM later */
329 link->aspm_support = 0;
330 link->aspm_default = 0;
331 link->aspm_enabled = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
332 return;
333 }
334
335 /* Configure common clock before checking latencies */
336 pcie_aspm_configure_common_clock(link);
337
Shaohua Li7d715a62008-02-25 09:46:41 +0800338 /* upstream component states */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900339 pcie_aspm_get_cap_device(parent, &support, &l0s, &l1, &enabled);
340 link->aspm_support = support;
341 link->latency.l0s = l0s;
342 link->latency.l1 = l1;
343 link->aspm_enabled = enabled;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900344
Shaohua Li7d715a62008-02-25 09:46:41 +0800345 /* downstream component states, all functions have the same setting */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900346 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
347 pcie_aspm_get_cap_device(child, &support, &l0s, &l1, &enabled);
348 link->aspm_support &= support;
349 link->latency.l0s = max_t(u32, link->latency.l0s, l0s);
350 link->latency.l1 = max_t(u32, link->latency.l1, l1);
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900351
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
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900355 link->aspm_enabled &= link->aspm_support;
356 link->aspm_default = link->aspm_enabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800357
358 /* ENDPOINT states*/
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900359 list_for_each_entry(child, &linkbus->devices, bus_list) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800360 int pos;
361 u32 reg32;
362 unsigned int latency;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900363 struct aspm_latency *acceptable =
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900364 &link->acceptable[PCI_FUNC(child->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800365
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900366 if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
367 child->pcie_type != PCI_EXP_TYPE_LEG_END)
Shaohua Li7d715a62008-02-25 09:46:41 +0800368 continue;
369
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900370 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
371 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Shaohua Li7d715a62008-02-25 09:46:41 +0800372 latency = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
373 latency = calc_L0S_latency(latency, 1);
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900374 acceptable->l0s = latency;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900375 if (link->aspm_support & PCIE_LINK_STATE_L1) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800376 latency = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
377 latency = calc_L1_latency(latency, 1);
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900378 acceptable->l1 = latency;
Shaohua Li7d715a62008-02-25 09:46:41 +0800379 }
380 }
381}
382
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900383/**
384 * __pcie_aspm_check_state_one - check latency for endpoint device.
385 * @endpoint: pointer to the struct pci_dev of endpoint device
386 *
387 * TBD: The latency from the endpoint to root complex vary per switch's
388 * upstream link state above the device. Here we just do a simple check
389 * which assumes all links above the device can be in L1 state, that
390 * is we just consider the worst case. If switch's upstream link can't
391 * be put into L0S/L1, then our check is too strictly.
392 */
393static u32 __pcie_aspm_check_state_one(struct pci_dev *endpoint, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800394{
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900395 u32 l1_switch_latency = 0;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900396 struct aspm_latency *acceptable;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900397 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800398
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900399 link = endpoint->bus->self->link_state;
400 state &= link->aspm_support;
401 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800402
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900403 while (link && state) {
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900404 if ((state & PCIE_LINK_STATE_L0S) &&
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900405 (link->latency.l0s > acceptable->l0s))
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900406 state &= ~PCIE_LINK_STATE_L0S;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900407 if ((state & PCIE_LINK_STATE_L1) &&
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900408 (link->latency.l1 + l1_switch_latency > acceptable->l1))
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900409 state &= ~PCIE_LINK_STATE_L1;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900410 link = link->parent;
411 /*
412 * Every switch on the path to root complex need 1
413 * more microsecond for L1. Spec doesn't mention L0s.
414 */
415 l1_switch_latency += 1000;
Shaohua Li7d715a62008-02-25 09:46:41 +0800416 }
417 return state;
418}
419
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900420static u32 pcie_aspm_check_state(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800421{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900422 pci_power_t power_state;
423 struct pci_dev *child;
424 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800425
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800426 /* If no child, ignore the link */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900427 if (list_empty(&linkbus->devices))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800428 return state;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900429
430 list_for_each_entry(child, &linkbus->devices, bus_list) {
431 /*
432 * If downstream component of a link is pci bridge, we
433 * disable ASPM for now for the link
434 */
435 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
436 return 0;
437
438 if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
439 child->pcie_type != PCI_EXP_TYPE_LEG_END))
Shaohua Li7d715a62008-02-25 09:46:41 +0800440 continue;
441 /* Device not in D0 doesn't need check latency */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900442 power_state = child->current_state;
443 if (power_state == PCI_D1 || power_state == PCI_D2 ||
444 power_state == PCI_D3hot || power_state == PCI_D3cold)
Shaohua Li7d715a62008-02-25 09:46:41 +0800445 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900446 state = __pcie_aspm_check_state_one(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800447 }
448 return state;
449}
450
451static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
452{
453 u16 reg16;
454 int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
455
456 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
457 reg16 &= ~0x3;
458 reg16 |= state;
459 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
460}
461
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900462static void __pcie_aspm_config_link(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800463{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900464 struct pci_dev *child, *parent = link->pdev;
465 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800466
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800467 /* If no child, disable the link */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900468 if (list_empty(&linkbus->devices))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800469 state = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800470 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900471 * If the downstream component has pci bridge function, don't
472 * do ASPM now.
Shaohua Li7d715a62008-02-25 09:46:41 +0800473 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900474 list_for_each_entry(child, &linkbus->devices, bus_list) {
475 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
476 return;
Shaohua Li7d715a62008-02-25 09:46:41 +0800477 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800478 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900479 * Spec 2.0 suggests all functions should be configured the
480 * same setting for ASPM. Enabling ASPM L1 should be done in
481 * upstream component first and then downstream, and vice
482 * versa for disabling ASPM L1. Spec doesn't mention L0S.
Shaohua Li7d715a62008-02-25 09:46:41 +0800483 */
484 if (state & PCIE_LINK_STATE_L1)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900485 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800486
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900487 list_for_each_entry(child, &linkbus->devices, bus_list)
488 __pcie_aspm_config_one_dev(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800489
490 if (!(state & PCIE_LINK_STATE_L1))
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900491 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800492
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900493 link->aspm_enabled = state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800494}
495
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800496static struct pcie_link_state *get_root_port_link(struct pcie_link_state *link)
497{
498 struct pcie_link_state *root_port_link = link;
499 while (root_port_link->parent)
500 root_port_link = root_port_link->parent;
501 return root_port_link;
502}
503
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900504/* Check the whole hierarchy, and configure each link in the hierarchy */
505static void __pcie_aspm_configure_link_state(struct pcie_link_state *link,
506 u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800507{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900508 struct pcie_link_state *leaf, *root = get_root_port_link(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800509
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900510 state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800511
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900512 /* Check all links who have specific root port link */
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900513 list_for_each_entry(leaf, &link_list, sibling) {
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800514 if (!list_empty(&leaf->children) ||
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900515 get_root_port_link(leaf) != root)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800516 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900517 state = pcie_aspm_check_state(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800518 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900519 /* Check root port link too in case it hasn't children */
520 state = pcie_aspm_check_state(root, state);
521 if (link->aspm_enabled == state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800522 return;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800523 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900524 * We must change the hierarchy. See comments in
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800525 * __pcie_aspm_config_link for the order
526 **/
527 if (state & PCIE_LINK_STATE_L1) {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900528 list_for_each_entry(leaf, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900529 if (get_root_port_link(leaf) == root)
530 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800531 }
532 } else {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900533 list_for_each_entry_reverse(leaf, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900534 if (get_root_port_link(leaf) == root)
535 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800536 }
537 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800538}
539
540/*
541 * pcie_aspm_configure_link_state: enable/disable PCI express link state
542 * @pdev: the root port or switch downstream port
543 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900544static void pcie_aspm_configure_link_state(struct pcie_link_state *link,
545 u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800546{
547 down_read(&pci_bus_sem);
548 mutex_lock(&aspm_lock);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900549 __pcie_aspm_configure_link_state(link, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800550 mutex_unlock(&aspm_lock);
551 up_read(&pci_bus_sem);
552}
553
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900554static void free_link_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800555{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900556 link->pdev->link_state = NULL;
557 kfree(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800558}
559
Shaohua Liddc97532008-05-21 16:58:40 +0800560static int pcie_aspm_sanity_check(struct pci_dev *pdev)
561{
562 struct pci_dev *child_dev;
563 int child_pos;
Shaohua Li149e1632008-07-23 10:32:31 +0800564 u32 reg32;
Shaohua Liddc97532008-05-21 16:58:40 +0800565
566 /*
567 * Some functions in a slot might not all be PCIE functions, very
568 * strange. Disable ASPM for the whole slot
569 */
570 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
571 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
572 if (!child_pos)
573 return -EINVAL;
Shaohua Li149e1632008-07-23 10:32:31 +0800574
575 /*
576 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
577 * RBER bit to determine if a function is 1.1 version device
578 */
579 pci_read_config_dword(child_dev, child_pos + PCI_EXP_DEVCAP,
580 &reg32);
Sitsofe Wheelere1f4f592008-09-16 14:27:13 +0100581 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
Vincent Legollf393d9b2008-10-12 12:26:12 +0200582 dev_printk(KERN_INFO, &child_dev->dev, "disabling ASPM"
583 " on pre-1.1 PCIe device. You can enable it"
584 " with 'pcie_aspm=force'\n");
Shaohua Li149e1632008-07-23 10:32:31 +0800585 return -EINVAL;
586 }
Shaohua Liddc97532008-05-21 16:58:40 +0800587 }
588 return 0;
589}
590
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900591static struct pcie_link_state *pcie_aspm_setup_link_state(struct pci_dev *pdev)
592{
593 struct pcie_link_state *link;
594 int blacklist = !!pcie_aspm_sanity_check(pdev);
595
596 link = kzalloc(sizeof(*link), GFP_KERNEL);
597 if (!link)
598 return NULL;
599 INIT_LIST_HEAD(&link->sibling);
600 INIT_LIST_HEAD(&link->children);
601 INIT_LIST_HEAD(&link->link);
602 link->pdev = pdev;
603 link->has_switch = pcie_aspm_downstream_has_switch(link);
604 if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
605 struct pcie_link_state *parent;
606 parent = pdev->bus->parent->self->link_state;
607 if (!parent) {
608 kfree(link);
609 return NULL;
610 }
611 link->parent = parent;
612 list_add(&link->link, &parent->children);
613 }
614 list_add(&link->sibling, &link_list);
615
616 pdev->link_state = link;
617
618 /* Check ASPM capability */
619 pcie_aspm_cap_init(link, blacklist);
620
621 /* Check Clock PM capability */
622 pcie_clkpm_cap_init(link, blacklist);
623
624 return link;
625}
626
Shaohua Li7d715a62008-02-25 09:46:41 +0800627/*
628 * pcie_aspm_init_link_state: Initiate PCI express link state.
629 * It is called after the pcie and its children devices are scaned.
630 * @pdev: the root port or switch downstream port
631 */
632void pcie_aspm_init_link_state(struct pci_dev *pdev)
633{
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900634 u32 state;
635 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800636
637 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
638 return;
639 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900640 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
Shaohua Li7d715a62008-02-25 09:46:41 +0800641 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900642
Shaohua Li8e822df2009-06-08 09:27:25 +0800643 /* VIA has a strange chipset, root port is under a bridge */
644 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900645 pdev->bus->self)
Shaohua Li8e822df2009-06-08 09:27:25 +0800646 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900647
Shaohua Li7d715a62008-02-25 09:46:41 +0800648 down_read(&pci_bus_sem);
649 if (list_empty(&pdev->subordinate->devices))
650 goto out;
651
Shaohua Li7d715a62008-02-25 09:46:41 +0800652 mutex_lock(&aspm_lock);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900653 link = pcie_aspm_setup_link_state(pdev);
654 if (!link)
655 goto unlock;
656 /*
657 * Setup initial ASPM state
658 *
659 * If link has switch, delay the link config. The leaf link
660 * initialization will config the whole hierarchy. But we must
661 * make sure BIOS doesn't set unsupported link state.
662 */
663 if (link->has_switch) {
664 state = pcie_aspm_check_state(link, link->aspm_default);
665 __pcie_aspm_config_link(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800666 } else {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900667 state = policy_to_aspm_state(link);
668 __pcie_aspm_configure_link_state(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800669 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800670
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900671 /* Setup initial Clock PM state */
672 state = (link->clkpm_capable) ? policy_to_clkpm_state(link) : 0;
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900673 pcie_set_clkpm(link, state);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900674unlock:
Shaohua Li7d715a62008-02-25 09:46:41 +0800675 mutex_unlock(&aspm_lock);
676out:
677 up_read(&pci_bus_sem);
678}
679
680/* @pdev: the endpoint device */
681void pcie_aspm_exit_link_state(struct pci_dev *pdev)
682{
683 struct pci_dev *parent = pdev->bus->self;
684 struct pcie_link_state *link_state = parent->link_state;
685
686 if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
687 return;
688 if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
689 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
690 return;
691 down_read(&pci_bus_sem);
692 mutex_lock(&aspm_lock);
693
694 /*
695 * All PCIe functions are in one slot, remove one function will remove
Alex Chiang3419c752009-01-28 14:59:18 -0700696 * the whole slot, so just wait until we are the last function left.
Shaohua Li7d715a62008-02-25 09:46:41 +0800697 */
Alex Chiang3419c752009-01-28 14:59:18 -0700698 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
Shaohua Li7d715a62008-02-25 09:46:41 +0800699 goto out;
700
701 /* All functions are removed, so just disable ASPM for the link */
702 __pcie_aspm_config_one_dev(parent, 0);
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900703 list_del(&link_state->sibling);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800704 list_del(&link_state->link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800705 /* Clock PM is for endpoint device */
706
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900707 free_link_state(link_state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800708out:
709 mutex_unlock(&aspm_lock);
710 up_read(&pci_bus_sem);
711}
712
713/* @pdev: the root port or switch downstream port */
714void pcie_aspm_pm_state_change(struct pci_dev *pdev)
715{
716 struct pcie_link_state *link_state = pdev->link_state;
717
718 if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
719 return;
720 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
721 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
722 return;
723 /*
724 * devices changed PM state, we should recheck if latency meets all
725 * functions' requirement
726 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900727 pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800728}
729
730/*
731 * pci_disable_link_state - disable pci device's link state, so the link will
732 * never enter specific states
733 */
734void pci_disable_link_state(struct pci_dev *pdev, int state)
735{
736 struct pci_dev *parent = pdev->bus->self;
737 struct pcie_link_state *link_state;
738
739 if (aspm_disabled || !pdev->is_pcie)
740 return;
741 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
742 pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
743 parent = pdev;
744 if (!parent || !parent->link_state)
745 return;
746
747 down_read(&pci_bus_sem);
748 mutex_lock(&aspm_lock);
749 link_state = parent->link_state;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900750 link_state->aspm_support &= ~state;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900751 __pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900752 if (state & PCIE_LINK_STATE_CLKPM) {
753 link_state->clkpm_capable = 0;
754 pcie_set_clkpm(link_state, 0);
755 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800756 mutex_unlock(&aspm_lock);
757 up_read(&pci_bus_sem);
758}
759EXPORT_SYMBOL(pci_disable_link_state);
760
761static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
762{
763 int i;
Shaohua Li7d715a62008-02-25 09:46:41 +0800764 struct pcie_link_state *link_state;
765
766 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
767 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
768 break;
769 if (i >= ARRAY_SIZE(policy_str))
770 return -EINVAL;
771 if (i == aspm_policy)
772 return 0;
773
774 down_read(&pci_bus_sem);
775 mutex_lock(&aspm_lock);
776 aspm_policy = i;
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900777 list_for_each_entry(link_state, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900778 __pcie_aspm_configure_link_state(link_state,
779 policy_to_aspm_state(link_state));
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900780 pcie_set_clkpm(link_state, policy_to_clkpm_state(link_state));
Shaohua Li7d715a62008-02-25 09:46:41 +0800781 }
782 mutex_unlock(&aspm_lock);
783 up_read(&pci_bus_sem);
784 return 0;
785}
786
787static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
788{
789 int i, cnt = 0;
790 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
791 if (i == aspm_policy)
792 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
793 else
794 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
795 return cnt;
796}
797
798module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
799 NULL, 0644);
800
801#ifdef CONFIG_PCIEASPM_DEBUG
802static ssize_t link_state_show(struct device *dev,
803 struct device_attribute *attr,
804 char *buf)
805{
806 struct pci_dev *pci_device = to_pci_dev(dev);
807 struct pcie_link_state *link_state = pci_device->link_state;
808
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900809 return sprintf(buf, "%d\n", link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800810}
811
812static ssize_t link_state_store(struct device *dev,
813 struct device_attribute *attr,
814 const char *buf,
815 size_t n)
816{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900817 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800818 int state;
819
820 if (n < 1)
821 return -EINVAL;
822 state = buf[0]-'0';
823 if (state >= 0 && state <= 3) {
824 /* setup link aspm state */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900825 pcie_aspm_configure_link_state(pdev->link_state, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800826 return n;
827 }
828
829 return -EINVAL;
830}
831
832static ssize_t clk_ctl_show(struct device *dev,
833 struct device_attribute *attr,
834 char *buf)
835{
836 struct pci_dev *pci_device = to_pci_dev(dev);
837 struct pcie_link_state *link_state = pci_device->link_state;
838
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900839 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800840}
841
842static ssize_t clk_ctl_store(struct device *dev,
843 struct device_attribute *attr,
844 const char *buf,
845 size_t n)
846{
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900847 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800848 int state;
849
850 if (n < 1)
851 return -EINVAL;
852 state = buf[0]-'0';
853
854 down_read(&pci_bus_sem);
855 mutex_lock(&aspm_lock);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900856 pcie_set_clkpm_nocheck(pdev->link_state, !!state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800857 mutex_unlock(&aspm_lock);
858 up_read(&pci_bus_sem);
859
860 return n;
861}
862
863static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
864static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
865
866static char power_group[] = "power";
867void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
868{
869 struct pcie_link_state *link_state = pdev->link_state;
870
871 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
872 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
873 return;
874
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900875 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800876 sysfs_add_file_to_group(&pdev->dev.kobj,
877 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900878 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800879 sysfs_add_file_to_group(&pdev->dev.kobj,
880 &dev_attr_clk_ctl.attr, power_group);
881}
882
883void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
884{
885 struct pcie_link_state *link_state = pdev->link_state;
886
887 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
888 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
889 return;
890
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900891 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800892 sysfs_remove_file_from_group(&pdev->dev.kobj,
893 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900894 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800895 sysfs_remove_file_from_group(&pdev->dev.kobj,
896 &dev_attr_clk_ctl.attr, power_group);
897}
898#endif
899
900static int __init pcie_aspm_disable(char *str)
901{
Shaohua Lid6d38572008-07-23 10:32:42 +0800902 if (!strcmp(str, "off")) {
903 aspm_disabled = 1;
904 printk(KERN_INFO "PCIe ASPM is disabled\n");
905 } else if (!strcmp(str, "force")) {
906 aspm_force = 1;
907 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
908 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800909 return 1;
910}
911
Shaohua Lid6d38572008-07-23 10:32:42 +0800912__setup("pcie_aspm=", pcie_aspm_disable);
Shaohua Li7d715a62008-02-25 09:46:41 +0800913
Shaohua Li5fde2442008-07-23 10:32:24 +0800914void pcie_no_aspm(void)
915{
Shaohua Lid6d38572008-07-23 10:32:42 +0800916 if (!aspm_force)
917 aspm_disabled = 1;
Shaohua Li5fde2442008-07-23 10:32:24 +0800918}
919
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700920/**
921 * pcie_aspm_enabled - is PCIe ASPM enabled?
922 *
923 * Returns true if ASPM has not been disabled by the command-line option
924 * pcie_aspm=off.
925 **/
926int pcie_aspm_enabled(void)
Shaohua Li7d715a62008-02-25 09:46:41 +0800927{
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700928 return !aspm_disabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800929}
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700930EXPORT_SYMBOL(pcie_aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800931