blob: 694ba56577588309f94ca76e644b1ab9a33daf11 [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 Kaneshige5aa63582009-05-13 12:17:44 +0900108static void pcie_set_clock_pm(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 Kaneshige8d349ac2009-05-13 12:18:22 +0900129static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800130{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900131 int pos, capable = 1, enabled = 1;
Shaohua Li7d715a62008-02-25 09:46:41 +0800132 u32 reg32;
133 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900134 struct pci_dev *child;
135 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800136
137 /* All functions should have the same cap and state, take the worst */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900138 list_for_each_entry(child, &linkbus->devices, bus_list) {
139 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800140 if (!pos)
141 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900142 pci_read_config_dword(child, pos + PCI_EXP_LNKCAP, &reg32);
Shaohua Li7d715a62008-02-25 09:46:41 +0800143 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
144 capable = 0;
145 enabled = 0;
146 break;
147 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900148 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800149 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
150 enabled = 0;
151 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900152 link->clkpm_enabled = enabled;
153 link->clkpm_default = enabled;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900154 link->clkpm_capable = (blacklist) ? 0 : capable;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800155}
156
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900157static bool pcie_aspm_downstream_has_switch(struct pcie_link_state *link)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800158{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900159 struct pci_dev *child;
160 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800161
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900162 list_for_each_entry(child, &linkbus->devices, bus_list) {
163 if (child->pcie_type == PCI_EXP_TYPE_UPSTREAM)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800164 return true;
165 }
166 return false;
Shaohua Li7d715a62008-02-25 09:46:41 +0800167}
168
169/*
170 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
171 * could use common clock. If they are, configure them to use the
172 * common clock. That will reduce the ASPM state exit latency.
173 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900174static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800175{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900176 int ppos, cpos, same_clock = 1;
177 u16 reg16, parent_reg, child_reg[8];
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100178 unsigned long start_jiffies;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900179 struct pci_dev *child, *parent = link->pdev;
180 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800181 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900182 * All functions of a slot should have the same Slot Clock
Shaohua Li7d715a62008-02-25 09:46:41 +0800183 * Configuration, so just check one function
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900184 */
185 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
186 BUG_ON(!child->is_pcie);
Shaohua Li7d715a62008-02-25 09:46:41 +0800187
188 /* Check downstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900189 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
190 pci_read_config_word(child, cpos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800191 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
192 same_clock = 0;
193
194 /* Check upstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900195 ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
196 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800197 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
198 same_clock = 0;
199
200 /* Configure downstream component, all functions */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900201 list_for_each_entry(child, &linkbus->devices, bus_list) {
202 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
203 pci_read_config_word(child, cpos + PCI_EXP_LNKCTL, &reg16);
204 child_reg[PCI_FUNC(child->devfn)] = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800205 if (same_clock)
206 reg16 |= PCI_EXP_LNKCTL_CCC;
207 else
208 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900209 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800210 }
211
212 /* Configure upstream component */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900213 pci_read_config_word(parent, ppos + PCI_EXP_LNKCTL, &reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100214 parent_reg = 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(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800220
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900221 /* Retrain link */
Shaohua Li7d715a62008-02-25 09:46:41 +0800222 reg16 |= PCI_EXP_LNKCTL_RL;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900223 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800224
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900225 /* Wait for link training end. Break out after waiting for timeout */
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100226 start_jiffies = jiffies;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700227 for (;;) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900228 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800229 if (!(reg16 & PCI_EXP_LNKSTA_LT))
230 break;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700231 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
232 break;
233 msleep(1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800234 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900235 if (!(reg16 & PCI_EXP_LNKSTA_LT))
236 return;
237
238 /* Training failed. Restore common clock configurations */
239 dev_printk(KERN_ERR, &parent->dev,
240 "ASPM: Could not configure common clock\n");
241 list_for_each_entry(child, &linkbus->devices, bus_list) {
242 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
243 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL,
244 child_reg[PCI_FUNC(child->devfn)]);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100245 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900246 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, parent_reg);
Shaohua Li7d715a62008-02-25 09:46:41 +0800247}
248
249/*
250 * calc_L0S_latency: Convert L0s latency encoding to ns
251 */
252static unsigned int calc_L0S_latency(unsigned int latency_encoding, int ac)
253{
254 unsigned int ns = 64;
255
256 if (latency_encoding == 0x7) {
257 if (ac)
258 ns = -1U;
259 else
260 ns = 5*1000; /* > 4us */
261 } else
262 ns *= (1 << latency_encoding);
263 return ns;
264}
265
266/*
267 * calc_L1_latency: Convert L1 latency encoding to ns
268 */
269static unsigned int calc_L1_latency(unsigned int latency_encoding, int ac)
270{
271 unsigned int ns = 1000;
272
273 if (latency_encoding == 0x7) {
274 if (ac)
275 ns = -1U;
276 else
277 ns = 65*1000; /* > 64us */
278 } else
279 ns *= (1 << latency_encoding);
280 return ns;
281}
282
283static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
284 unsigned int *l0s, unsigned int *l1, unsigned int *enabled)
285{
286 int pos;
287 u16 reg16;
288 u32 reg32;
289 unsigned int latency;
290
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900291 *l0s = *l1 = *enabled = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800292 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
293 pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
294 *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
295 if (*state != PCIE_LINK_STATE_L0S &&
296 *state != (PCIE_LINK_STATE_L1|PCIE_LINK_STATE_L0S))
297 *state = 0;
298 if (*state == 0)
299 return;
300
301 latency = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
302 *l0s = calc_L0S_latency(latency, 0);
303 if (*state & PCIE_LINK_STATE_L1) {
304 latency = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
305 *l1 = calc_L1_latency(latency, 0);
306 }
307 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
308 *enabled = reg16 & (PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1);
309}
310
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900311static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800312{
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900313 u32 support, l0s, l1, enabled;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900314 struct pci_dev *child, *parent = link->pdev;
315 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800316
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900317 if (blacklist) {
318 /* Set support state to 0, so we will disable ASPM later */
319 link->aspm_support = 0;
320 link->aspm_default = 0;
321 link->aspm_enabled = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
322 return;
323 }
324
325 /* Configure common clock before checking latencies */
326 pcie_aspm_configure_common_clock(link);
327
Shaohua Li7d715a62008-02-25 09:46:41 +0800328 /* upstream component states */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900329 pcie_aspm_get_cap_device(parent, &support, &l0s, &l1, &enabled);
330 link->aspm_support = support;
331 link->latency.l0s = l0s;
332 link->latency.l1 = l1;
333 link->aspm_enabled = enabled;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900334
Shaohua Li7d715a62008-02-25 09:46:41 +0800335 /* downstream component states, all functions have the same setting */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900336 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
337 pcie_aspm_get_cap_device(child, &support, &l0s, &l1, &enabled);
338 link->aspm_support &= support;
339 link->latency.l0s = max_t(u32, link->latency.l0s, l0s);
340 link->latency.l1 = max_t(u32, link->latency.l1, l1);
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900341
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900342 if (!link->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800343 return;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900344
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900345 link->aspm_enabled &= link->aspm_support;
346 link->aspm_default = link->aspm_enabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800347
348 /* ENDPOINT states*/
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900349 list_for_each_entry(child, &linkbus->devices, bus_list) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800350 int pos;
351 u32 reg32;
352 unsigned int latency;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900353 struct aspm_latency *acceptable =
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900354 &link->acceptable[PCI_FUNC(child->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800355
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900356 if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
357 child->pcie_type != PCI_EXP_TYPE_LEG_END)
Shaohua Li7d715a62008-02-25 09:46:41 +0800358 continue;
359
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900360 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
361 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Shaohua Li7d715a62008-02-25 09:46:41 +0800362 latency = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
363 latency = calc_L0S_latency(latency, 1);
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900364 acceptable->l0s = latency;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900365 if (link->aspm_support & PCIE_LINK_STATE_L1) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800366 latency = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
367 latency = calc_L1_latency(latency, 1);
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900368 acceptable->l1 = latency;
Shaohua Li7d715a62008-02-25 09:46:41 +0800369 }
370 }
371}
372
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900373/**
374 * __pcie_aspm_check_state_one - check latency for endpoint device.
375 * @endpoint: pointer to the struct pci_dev of endpoint device
376 *
377 * TBD: The latency from the endpoint to root complex vary per switch's
378 * upstream link state above the device. Here we just do a simple check
379 * which assumes all links above the device can be in L1 state, that
380 * is we just consider the worst case. If switch's upstream link can't
381 * be put into L0S/L1, then our check is too strictly.
382 */
383static u32 __pcie_aspm_check_state_one(struct pci_dev *endpoint, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800384{
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900385 u32 l1_switch_latency = 0;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900386 struct aspm_latency *acceptable;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900387 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800388
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900389 link = endpoint->bus->self->link_state;
390 state &= link->aspm_support;
391 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800392
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900393 while (link && state) {
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900394 if ((state & PCIE_LINK_STATE_L0S) &&
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900395 (link->latency.l0s > acceptable->l0s))
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900396 state &= ~PCIE_LINK_STATE_L0S;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900397 if ((state & PCIE_LINK_STATE_L1) &&
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900398 (link->latency.l1 + l1_switch_latency > acceptable->l1))
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900399 state &= ~PCIE_LINK_STATE_L1;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900400 link = link->parent;
401 /*
402 * Every switch on the path to root complex need 1
403 * more microsecond for L1. Spec doesn't mention L0s.
404 */
405 l1_switch_latency += 1000;
Shaohua Li7d715a62008-02-25 09:46:41 +0800406 }
407 return state;
408}
409
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900410static u32 pcie_aspm_check_state(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800411{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900412 pci_power_t power_state;
413 struct pci_dev *child;
414 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800415
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800416 /* If no child, ignore the link */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900417 if (list_empty(&linkbus->devices))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800418 return state;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900419
420 list_for_each_entry(child, &linkbus->devices, bus_list) {
421 /*
422 * If downstream component of a link is pci bridge, we
423 * disable ASPM for now for the link
424 */
425 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
426 return 0;
427
428 if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
429 child->pcie_type != PCI_EXP_TYPE_LEG_END))
Shaohua Li7d715a62008-02-25 09:46:41 +0800430 continue;
431 /* Device not in D0 doesn't need check latency */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900432 power_state = child->current_state;
433 if (power_state == PCI_D1 || power_state == PCI_D2 ||
434 power_state == PCI_D3hot || power_state == PCI_D3cold)
Shaohua Li7d715a62008-02-25 09:46:41 +0800435 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900436 state = __pcie_aspm_check_state_one(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800437 }
438 return state;
439}
440
441static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
442{
443 u16 reg16;
444 int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
445
446 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
447 reg16 &= ~0x3;
448 reg16 |= state;
449 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
450}
451
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900452static void __pcie_aspm_config_link(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800453{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900454 struct pci_dev *child, *parent = link->pdev;
455 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800456
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800457 /* If no child, disable the link */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900458 if (list_empty(&linkbus->devices))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800459 state = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800460 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900461 * If the downstream component has pci bridge function, don't
462 * do ASPM now.
Shaohua Li7d715a62008-02-25 09:46:41 +0800463 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900464 list_for_each_entry(child, &linkbus->devices, bus_list) {
465 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
466 return;
Shaohua Li7d715a62008-02-25 09:46:41 +0800467 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800468 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900469 * Spec 2.0 suggests all functions should be configured the
470 * same setting for ASPM. Enabling ASPM L1 should be done in
471 * upstream component first and then downstream, and vice
472 * versa for disabling ASPM L1. Spec doesn't mention L0S.
Shaohua Li7d715a62008-02-25 09:46:41 +0800473 */
474 if (state & PCIE_LINK_STATE_L1)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900475 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800476
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900477 list_for_each_entry(child, &linkbus->devices, bus_list)
478 __pcie_aspm_config_one_dev(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800479
480 if (!(state & PCIE_LINK_STATE_L1))
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900481 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800482
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900483 link->aspm_enabled = state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800484}
485
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800486static struct pcie_link_state *get_root_port_link(struct pcie_link_state *link)
487{
488 struct pcie_link_state *root_port_link = link;
489 while (root_port_link->parent)
490 root_port_link = root_port_link->parent;
491 return root_port_link;
492}
493
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900494/* Check the whole hierarchy, and configure each link in the hierarchy */
495static void __pcie_aspm_configure_link_state(struct pcie_link_state *link,
496 u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800497{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900498 struct pcie_link_state *leaf, *root = get_root_port_link(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800499
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900500 state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800501
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900502 /* Check all links who have specific root port link */
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900503 list_for_each_entry(leaf, &link_list, sibling) {
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800504 if (!list_empty(&leaf->children) ||
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900505 get_root_port_link(leaf) != root)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800506 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900507 state = pcie_aspm_check_state(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800508 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900509 /* Check root port link too in case it hasn't children */
510 state = pcie_aspm_check_state(root, state);
511 if (link->aspm_enabled == state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800512 return;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800513 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900514 * We must change the hierarchy. See comments in
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800515 * __pcie_aspm_config_link for the order
516 **/
517 if (state & PCIE_LINK_STATE_L1) {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900518 list_for_each_entry(leaf, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900519 if (get_root_port_link(leaf) == root)
520 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800521 }
522 } else {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900523 list_for_each_entry_reverse(leaf, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900524 if (get_root_port_link(leaf) == root)
525 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800526 }
527 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800528}
529
530/*
531 * pcie_aspm_configure_link_state: enable/disable PCI express link state
532 * @pdev: the root port or switch downstream port
533 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900534static void pcie_aspm_configure_link_state(struct pcie_link_state *link,
535 u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800536{
537 down_read(&pci_bus_sem);
538 mutex_lock(&aspm_lock);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900539 __pcie_aspm_configure_link_state(link, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800540 mutex_unlock(&aspm_lock);
541 up_read(&pci_bus_sem);
542}
543
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900544static void free_link_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800545{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900546 link->pdev->link_state = NULL;
547 kfree(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800548}
549
Shaohua Liddc97532008-05-21 16:58:40 +0800550static int pcie_aspm_sanity_check(struct pci_dev *pdev)
551{
552 struct pci_dev *child_dev;
553 int child_pos;
Shaohua Li149e1632008-07-23 10:32:31 +0800554 u32 reg32;
Shaohua Liddc97532008-05-21 16:58:40 +0800555
556 /*
557 * Some functions in a slot might not all be PCIE functions, very
558 * strange. Disable ASPM for the whole slot
559 */
560 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
561 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
562 if (!child_pos)
563 return -EINVAL;
Shaohua Li149e1632008-07-23 10:32:31 +0800564
565 /*
566 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
567 * RBER bit to determine if a function is 1.1 version device
568 */
569 pci_read_config_dword(child_dev, child_pos + PCI_EXP_DEVCAP,
570 &reg32);
Sitsofe Wheelere1f4f592008-09-16 14:27:13 +0100571 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
Vincent Legollf393d9b2008-10-12 12:26:12 +0200572 dev_printk(KERN_INFO, &child_dev->dev, "disabling ASPM"
573 " on pre-1.1 PCIe device. You can enable it"
574 " with 'pcie_aspm=force'\n");
Shaohua Li149e1632008-07-23 10:32:31 +0800575 return -EINVAL;
576 }
Shaohua Liddc97532008-05-21 16:58:40 +0800577 }
578 return 0;
579}
580
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900581static struct pcie_link_state *pcie_aspm_setup_link_state(struct pci_dev *pdev)
582{
583 struct pcie_link_state *link;
584 int blacklist = !!pcie_aspm_sanity_check(pdev);
585
586 link = kzalloc(sizeof(*link), GFP_KERNEL);
587 if (!link)
588 return NULL;
589 INIT_LIST_HEAD(&link->sibling);
590 INIT_LIST_HEAD(&link->children);
591 INIT_LIST_HEAD(&link->link);
592 link->pdev = pdev;
593 link->has_switch = pcie_aspm_downstream_has_switch(link);
594 if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
595 struct pcie_link_state *parent;
596 parent = pdev->bus->parent->self->link_state;
597 if (!parent) {
598 kfree(link);
599 return NULL;
600 }
601 link->parent = parent;
602 list_add(&link->link, &parent->children);
603 }
604 list_add(&link->sibling, &link_list);
605
606 pdev->link_state = link;
607
608 /* Check ASPM capability */
609 pcie_aspm_cap_init(link, blacklist);
610
611 /* Check Clock PM capability */
612 pcie_clkpm_cap_init(link, blacklist);
613
614 return link;
615}
616
Shaohua Li7d715a62008-02-25 09:46:41 +0800617/*
618 * pcie_aspm_init_link_state: Initiate PCI express link state.
619 * It is called after the pcie and its children devices are scaned.
620 * @pdev: the root port or switch downstream port
621 */
622void pcie_aspm_init_link_state(struct pci_dev *pdev)
623{
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900624 u32 state;
625 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800626
627 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
628 return;
629 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900630 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
Shaohua Li7d715a62008-02-25 09:46:41 +0800631 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900632
Shaohua Li8e822df2009-06-08 09:27:25 +0800633 /* VIA has a strange chipset, root port is under a bridge */
634 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900635 pdev->bus->self)
Shaohua Li8e822df2009-06-08 09:27:25 +0800636 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900637
Shaohua Li7d715a62008-02-25 09:46:41 +0800638 down_read(&pci_bus_sem);
639 if (list_empty(&pdev->subordinate->devices))
640 goto out;
641
Shaohua Li7d715a62008-02-25 09:46:41 +0800642 mutex_lock(&aspm_lock);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900643 link = pcie_aspm_setup_link_state(pdev);
644 if (!link)
645 goto unlock;
646 /*
647 * Setup initial ASPM state
648 *
649 * If link has switch, delay the link config. The leaf link
650 * initialization will config the whole hierarchy. But we must
651 * make sure BIOS doesn't set unsupported link state.
652 */
653 if (link->has_switch) {
654 state = pcie_aspm_check_state(link, link->aspm_default);
655 __pcie_aspm_config_link(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800656 } else {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900657 state = policy_to_aspm_state(link);
658 __pcie_aspm_configure_link_state(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800659 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800660
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900661 /* Setup initial Clock PM state */
662 state = (link->clkpm_capable) ? policy_to_clkpm_state(link) : 0;
663 pcie_set_clock_pm(link, state);
664unlock:
Shaohua Li7d715a62008-02-25 09:46:41 +0800665 mutex_unlock(&aspm_lock);
666out:
667 up_read(&pci_bus_sem);
668}
669
670/* @pdev: the endpoint device */
671void pcie_aspm_exit_link_state(struct pci_dev *pdev)
672{
673 struct pci_dev *parent = pdev->bus->self;
674 struct pcie_link_state *link_state = parent->link_state;
675
676 if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
677 return;
678 if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
679 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
680 return;
681 down_read(&pci_bus_sem);
682 mutex_lock(&aspm_lock);
683
684 /*
685 * All PCIe functions are in one slot, remove one function will remove
Alex Chiang3419c752009-01-28 14:59:18 -0700686 * the whole slot, so just wait until we are the last function left.
Shaohua Li7d715a62008-02-25 09:46:41 +0800687 */
Alex Chiang3419c752009-01-28 14:59:18 -0700688 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
Shaohua Li7d715a62008-02-25 09:46:41 +0800689 goto out;
690
691 /* All functions are removed, so just disable ASPM for the link */
692 __pcie_aspm_config_one_dev(parent, 0);
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900693 list_del(&link_state->sibling);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800694 list_del(&link_state->link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800695 /* Clock PM is for endpoint device */
696
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900697 free_link_state(link_state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800698out:
699 mutex_unlock(&aspm_lock);
700 up_read(&pci_bus_sem);
701}
702
703/* @pdev: the root port or switch downstream port */
704void pcie_aspm_pm_state_change(struct pci_dev *pdev)
705{
706 struct pcie_link_state *link_state = pdev->link_state;
707
708 if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
709 return;
710 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
711 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
712 return;
713 /*
714 * devices changed PM state, we should recheck if latency meets all
715 * functions' requirement
716 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900717 pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800718}
719
720/*
721 * pci_disable_link_state - disable pci device's link state, so the link will
722 * never enter specific states
723 */
724void pci_disable_link_state(struct pci_dev *pdev, int state)
725{
726 struct pci_dev *parent = pdev->bus->self;
727 struct pcie_link_state *link_state;
728
729 if (aspm_disabled || !pdev->is_pcie)
730 return;
731 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
732 pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
733 parent = pdev;
734 if (!parent || !parent->link_state)
735 return;
736
737 down_read(&pci_bus_sem);
738 mutex_lock(&aspm_lock);
739 link_state = parent->link_state;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900740 link_state->aspm_support &= ~state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800741 if (state & PCIE_LINK_STATE_CLKPM)
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900742 link_state->clkpm_capable = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800743
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900744 __pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900745 if (!link_state->clkpm_capable && link_state->clkpm_enabled)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900746 pcie_set_clock_pm(link_state, 0);
Shaohua Li7d715a62008-02-25 09:46:41 +0800747 mutex_unlock(&aspm_lock);
748 up_read(&pci_bus_sem);
749}
750EXPORT_SYMBOL(pci_disable_link_state);
751
752static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
753{
754 int i;
Shaohua Li7d715a62008-02-25 09:46:41 +0800755 struct pcie_link_state *link_state;
756
757 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
758 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
759 break;
760 if (i >= ARRAY_SIZE(policy_str))
761 return -EINVAL;
762 if (i == aspm_policy)
763 return 0;
764
765 down_read(&pci_bus_sem);
766 mutex_lock(&aspm_lock);
767 aspm_policy = i;
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900768 list_for_each_entry(link_state, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900769 __pcie_aspm_configure_link_state(link_state,
770 policy_to_aspm_state(link_state));
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900771 if (link_state->clkpm_capable &&
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900772 link_state->clkpm_enabled != policy_to_clkpm_state(link_state))
773 pcie_set_clock_pm(link_state,
774 policy_to_clkpm_state(link_state));
Shaohua Li7d715a62008-02-25 09:46:41 +0800775
776 }
777 mutex_unlock(&aspm_lock);
778 up_read(&pci_bus_sem);
779 return 0;
780}
781
782static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
783{
784 int i, cnt = 0;
785 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
786 if (i == aspm_policy)
787 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
788 else
789 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
790 return cnt;
791}
792
793module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
794 NULL, 0644);
795
796#ifdef CONFIG_PCIEASPM_DEBUG
797static ssize_t link_state_show(struct device *dev,
798 struct device_attribute *attr,
799 char *buf)
800{
801 struct pci_dev *pci_device = to_pci_dev(dev);
802 struct pcie_link_state *link_state = pci_device->link_state;
803
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900804 return sprintf(buf, "%d\n", link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800805}
806
807static ssize_t link_state_store(struct device *dev,
808 struct device_attribute *attr,
809 const char *buf,
810 size_t n)
811{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900812 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800813 int state;
814
815 if (n < 1)
816 return -EINVAL;
817 state = buf[0]-'0';
818 if (state >= 0 && state <= 3) {
819 /* setup link aspm state */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900820 pcie_aspm_configure_link_state(pdev->link_state, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800821 return n;
822 }
823
824 return -EINVAL;
825}
826
827static ssize_t clk_ctl_show(struct device *dev,
828 struct device_attribute *attr,
829 char *buf)
830{
831 struct pci_dev *pci_device = to_pci_dev(dev);
832 struct pcie_link_state *link_state = pci_device->link_state;
833
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900834 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800835}
836
837static ssize_t clk_ctl_store(struct device *dev,
838 struct device_attribute *attr,
839 const char *buf,
840 size_t n)
841{
842 struct pci_dev *pci_device = to_pci_dev(dev);
843 int state;
844
845 if (n < 1)
846 return -EINVAL;
847 state = buf[0]-'0';
848
849 down_read(&pci_bus_sem);
850 mutex_lock(&aspm_lock);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900851 pcie_set_clock_pm(pci_device->link_state, !!state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800852 mutex_unlock(&aspm_lock);
853 up_read(&pci_bus_sem);
854
855 return n;
856}
857
858static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
859static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
860
861static char power_group[] = "power";
862void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
863{
864 struct pcie_link_state *link_state = pdev->link_state;
865
866 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
867 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
868 return;
869
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900870 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800871 sysfs_add_file_to_group(&pdev->dev.kobj,
872 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900873 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800874 sysfs_add_file_to_group(&pdev->dev.kobj,
875 &dev_attr_clk_ctl.attr, power_group);
876}
877
878void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
879{
880 struct pcie_link_state *link_state = pdev->link_state;
881
882 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
883 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
884 return;
885
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900886 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800887 sysfs_remove_file_from_group(&pdev->dev.kobj,
888 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900889 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800890 sysfs_remove_file_from_group(&pdev->dev.kobj,
891 &dev_attr_clk_ctl.attr, power_group);
892}
893#endif
894
895static int __init pcie_aspm_disable(char *str)
896{
Shaohua Lid6d38572008-07-23 10:32:42 +0800897 if (!strcmp(str, "off")) {
898 aspm_disabled = 1;
899 printk(KERN_INFO "PCIe ASPM is disabled\n");
900 } else if (!strcmp(str, "force")) {
901 aspm_force = 1;
902 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
903 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800904 return 1;
905}
906
Shaohua Lid6d38572008-07-23 10:32:42 +0800907__setup("pcie_aspm=", pcie_aspm_disable);
Shaohua Li7d715a62008-02-25 09:46:41 +0800908
Shaohua Li5fde2442008-07-23 10:32:24 +0800909void pcie_no_aspm(void)
910{
Shaohua Lid6d38572008-07-23 10:32:42 +0800911 if (!aspm_force)
912 aspm_disabled = 1;
Shaohua Li5fde2442008-07-23 10:32:24 +0800913}
914
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700915/**
916 * pcie_aspm_enabled - is PCIe ASPM enabled?
917 *
918 * Returns true if ASPM has not been disabled by the command-line option
919 * pcie_aspm=off.
920 **/
921int pcie_aspm_enabled(void)
Shaohua Li7d715a62008-02-25 09:46:41 +0800922{
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700923 return !aspm_disabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800924}
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700925EXPORT_SYMBOL(pcie_aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800926