blob: 08d293f60fe888e3238d5cec3ffb68f8324bf73a [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 */
Kenji Kaneshige07d92762009-08-19 11:00:25 +090045 u32 aspm_capable:2; /* Capable ASPM state with latency */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090046 u32 aspm_default:2; /* Default ASPM state by BIOS */
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +090047 u32 aspm_disable:2; /* Disabled ASPM state */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090048
Kenji Kaneshige4d246e42009-05-13 12:15:38 +090049 /* Clock PM state */
50 u32 clkpm_capable:1; /* Clock PM capable? */
51 u32 clkpm_enabled:1; /* Current Clock PM state */
52 u32 clkpm_default:1; /* Default Clock PM state by BIOS */
53
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090054 /* Latencies */
55 struct aspm_latency latency; /* Exit latency */
Shaohua Li7d715a62008-02-25 09:46:41 +080056 /*
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090057 * Endpoint acceptable latencies. A pcie downstream port only
58 * has one slot under it, so at most there are 8 functions.
Shaohua Li7d715a62008-02-25 09:46:41 +080059 */
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090060 struct aspm_latency acceptable[8];
Shaohua Li7d715a62008-02-25 09:46:41 +080061};
62
Shaohua Lid6d38572008-07-23 10:32:42 +080063static int aspm_disabled, aspm_force;
Shaohua Li7d715a62008-02-25 09:46:41 +080064static DEFINE_MUTEX(aspm_lock);
65static LIST_HEAD(link_list);
66
67#define POLICY_DEFAULT 0 /* BIOS default setting */
68#define POLICY_PERFORMANCE 1 /* high performance */
69#define POLICY_POWERSAVE 2 /* high power saving */
70static int aspm_policy;
71static const char *policy_str[] = {
72 [POLICY_DEFAULT] = "default",
73 [POLICY_PERFORMANCE] = "performance",
74 [POLICY_POWERSAVE] = "powersave"
75};
76
Andrew Patterson987a4c72009-01-05 16:21:04 -070077#define LINK_RETRAIN_TIMEOUT HZ
78
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090079static int policy_to_aspm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080080{
Shaohua Li7d715a62008-02-25 09:46:41 +080081 switch (aspm_policy) {
82 case POLICY_PERFORMANCE:
83 /* Disable ASPM and Clock PM */
84 return 0;
85 case POLICY_POWERSAVE:
86 /* Enable ASPM L0s/L1 */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090087 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Shaohua Li7d715a62008-02-25 09:46:41 +080088 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090089 return link->aspm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +080090 }
91 return 0;
92}
93
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090094static int policy_to_clkpm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080095{
Shaohua Li7d715a62008-02-25 09:46:41 +080096 switch (aspm_policy) {
97 case POLICY_PERFORMANCE:
98 /* Disable ASPM and Clock PM */
99 return 0;
100 case POLICY_POWERSAVE:
101 /* Disable Clock PM */
102 return 1;
103 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900104 return link->clkpm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +0800105 }
106 return 0;
107}
108
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900109static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800110{
Shaohua Li7d715a62008-02-25 09:46:41 +0800111 int pos;
112 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900113 struct pci_dev *child;
114 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800115
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900116 list_for_each_entry(child, &linkbus->devices, bus_list) {
117 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800118 if (!pos)
119 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900120 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800121 if (enable)
122 reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
123 else
124 reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900125 pci_write_config_word(child, pos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800126 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900127 link->clkpm_enabled = !!enable;
Shaohua Li7d715a62008-02-25 09:46:41 +0800128}
129
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900130static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
131{
132 /* Don't enable Clock PM if the link is not Clock PM capable */
133 if (!link->clkpm_capable && enable)
134 return;
135 /* Need nothing if the specified equals to current state */
136 if (link->clkpm_enabled == enable)
137 return;
138 pcie_set_clkpm_nocheck(link, enable);
139}
140
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900141static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800142{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900143 int pos, capable = 1, enabled = 1;
Shaohua Li7d715a62008-02-25 09:46:41 +0800144 u32 reg32;
145 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900146 struct pci_dev *child;
147 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800148
149 /* All functions should have the same cap and state, take the worst */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900150 list_for_each_entry(child, &linkbus->devices, bus_list) {
151 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800152 if (!pos)
153 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900154 pci_read_config_dword(child, pos + PCI_EXP_LNKCAP, &reg32);
Shaohua Li7d715a62008-02-25 09:46:41 +0800155 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
156 capable = 0;
157 enabled = 0;
158 break;
159 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900160 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800161 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
162 enabled = 0;
163 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900164 link->clkpm_enabled = enabled;
165 link->clkpm_default = enabled;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900166 link->clkpm_capable = (blacklist) ? 0 : capable;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800167}
168
Shaohua Li7d715a62008-02-25 09:46:41 +0800169/*
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
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900249/* Convert L0s latency encoding to ns */
250static u32 calc_l0s_latency(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800251{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900252 if (encoding == 0x7)
253 return (5 * 1000); /* > 4us */
254 return (64 << encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800255}
256
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900257/* Convert L0s acceptable latency encoding to ns */
258static u32 calc_l0s_acceptable(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800259{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900260 if (encoding == 0x7)
261 return -1U;
262 return (64 << encoding);
263}
Shaohua Li7d715a62008-02-25 09:46:41 +0800264
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900265/* Convert L1 latency encoding to ns */
266static u32 calc_l1_latency(u32 encoding)
267{
268 if (encoding == 0x7)
269 return (65 * 1000); /* > 64us */
270 return (1000 << encoding);
271}
272
273/* Convert L1 acceptable latency encoding to ns */
274static u32 calc_l1_acceptable(u32 encoding)
275{
276 if (encoding == 0x7)
277 return -1U;
278 return (1000 << encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800279}
280
281static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900282 u32 *l0s, u32 *l1, u32 *enabled)
Shaohua Li7d715a62008-02-25 09:46:41 +0800283{
284 int pos;
285 u16 reg16;
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900286 u32 reg32, encoding;
Shaohua Li7d715a62008-02-25 09:46:41 +0800287
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900288 *l0s = *l1 = *enabled = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800289 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
290 pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
291 *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
292 if (*state != PCIE_LINK_STATE_L0S &&
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900293 *state != (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L0S))
Shaohua Li7d715a62008-02-25 09:46:41 +0800294 *state = 0;
295 if (*state == 0)
296 return;
297
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900298 encoding = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
299 *l0s = calc_l0s_latency(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800300 if (*state & PCIE_LINK_STATE_L1) {
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900301 encoding = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
302 *l1 = calc_l1_latency(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800303 }
304 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900305 *enabled = reg16 & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800306}
307
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900308static void pcie_aspm_check_latency(struct pci_dev *endpoint)
309{
310 u32 l1_switch_latency = 0;
311 struct aspm_latency *acceptable;
312 struct pcie_link_state *link;
313
314 /* Device not in D0 doesn't need latency check */
315 if ((endpoint->current_state != PCI_D0) &&
316 (endpoint->current_state != PCI_UNKNOWN))
317 return;
318
319 link = endpoint->bus->self->link_state;
320 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
321
322 while (link) {
323 /* Check L0s latency */
324 if ((link->aspm_capable & PCIE_LINK_STATE_L0S) &&
325 (link->latency.l0s > acceptable->l0s))
326 link->aspm_capable &= ~PCIE_LINK_STATE_L0S;
327 /*
328 * Check L1 latency.
329 * Every switch on the path to root complex need 1
330 * more microsecond for L1. Spec doesn't mention L0s.
331 */
332 if ((link->aspm_capable & PCIE_LINK_STATE_L1) &&
333 (link->latency.l1 + l1_switch_latency > acceptable->l1))
334 link->aspm_capable &= ~PCIE_LINK_STATE_L1;
335 l1_switch_latency += 1000;
336
337 link = link->parent;
338 }
339}
340
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900341static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800342{
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900343 u32 support, l0s, l1, enabled;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900344 struct pci_dev *child, *parent = link->pdev;
345 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800346
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900347 if (blacklist) {
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900348 /* Set enabled/disable so that we will disable ASPM later */
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900349 link->aspm_enabled = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900350 link->aspm_disable = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900351 return;
352 }
353
354 /* Configure common clock before checking latencies */
355 pcie_aspm_configure_common_clock(link);
356
Shaohua Li7d715a62008-02-25 09:46:41 +0800357 /* upstream component states */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900358 pcie_aspm_get_cap_device(parent, &support, &l0s, &l1, &enabled);
359 link->aspm_support = support;
360 link->latency.l0s = l0s;
361 link->latency.l1 = l1;
362 link->aspm_enabled = enabled;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900363
Shaohua Li7d715a62008-02-25 09:46:41 +0800364 /* downstream component states, all functions have the same setting */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900365 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
366 pcie_aspm_get_cap_device(child, &support, &l0s, &l1, &enabled);
367 link->aspm_support &= support;
368 link->latency.l0s = max_t(u32, link->latency.l0s, l0s);
369 link->latency.l1 = max_t(u32, link->latency.l1, l1);
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900370
Kenji Kaneshigeb127bd52009-08-19 10:57:31 +0900371 /* Save default state */
372 link->aspm_default = link->aspm_enabled;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900373
374 /* Setup initial capable state. Will be updated later */
375 link->aspm_capable = link->aspm_support;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900376 /*
377 * If the downstream component has pci bridge function, don't
378 * do ASPM for now.
379 */
380 list_for_each_entry(child, &linkbus->devices, bus_list) {
381 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
382 link->aspm_disable =
383 PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
384 break;
385 }
386 }
Kenji Kaneshigeb127bd52009-08-19 10:57:31 +0900387
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900388 /* Get and check endpoint acceptable latencies */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900389 list_for_each_entry(child, &linkbus->devices, bus_list) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800390 int pos;
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900391 u32 reg32, encoding;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900392 struct aspm_latency *acceptable =
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900393 &link->acceptable[PCI_FUNC(child->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800394
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900395 if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
396 child->pcie_type != PCI_EXP_TYPE_LEG_END)
Shaohua Li7d715a62008-02-25 09:46:41 +0800397 continue;
398
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900399 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
400 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900401 /* Calculate endpoint L0s acceptable latency */
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900402 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
403 acceptable->l0s = calc_l0s_acceptable(encoding);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900404 /* Calculate endpoint L1 acceptable latency */
405 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
406 acceptable->l1 = calc_l1_acceptable(encoding);
407
408 pcie_aspm_check_latency(child);
Shaohua Li7d715a62008-02-25 09:46:41 +0800409 }
410}
411
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900412static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800413{
414 u16 reg16;
415 int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
416
417 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
418 reg16 &= ~0x3;
419 reg16 |= state;
420 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
421}
422
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900423static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800424{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900425 struct pci_dev *child, *parent = link->pdev;
426 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800427
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900428 /* Nothing to do if the link is already in the requested state */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900429 state &= (link->aspm_capable & ~link->aspm_disable);
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900430 if (link->aspm_enabled == state)
431 return;
Shaohua Li7d715a62008-02-25 09:46:41 +0800432 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900433 * Spec 2.0 suggests all functions should be configured the
434 * same setting for ASPM. Enabling ASPM L1 should be done in
435 * upstream component first and then downstream, and vice
436 * versa for disabling ASPM L1. Spec doesn't mention L0S.
Shaohua Li7d715a62008-02-25 09:46:41 +0800437 */
438 if (state & PCIE_LINK_STATE_L1)
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900439 pcie_config_aspm_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800440
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900441 list_for_each_entry(child, &linkbus->devices, bus_list)
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900442 pcie_config_aspm_dev(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800443
444 if (!(state & PCIE_LINK_STATE_L1))
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900445 pcie_config_aspm_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800446
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900447 link->aspm_enabled = state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800448}
449
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900450static void pcie_config_aspm_path(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800451{
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900452 while (link) {
453 pcie_config_aspm_link(link, policy_to_aspm_state(link));
454 link = link->parent;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800455 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800456}
457
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900458static void free_link_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800459{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900460 link->pdev->link_state = NULL;
461 kfree(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800462}
463
Shaohua Liddc97532008-05-21 16:58:40 +0800464static int pcie_aspm_sanity_check(struct pci_dev *pdev)
465{
Kenji Kaneshige36475842009-05-13 12:23:09 +0900466 struct pci_dev *child;
467 int pos;
Shaohua Li149e1632008-07-23 10:32:31 +0800468 u32 reg32;
Shaohua Liddc97532008-05-21 16:58:40 +0800469 /*
Kenji Kaneshige36475842009-05-13 12:23:09 +0900470 * Some functions in a slot might not all be PCIE functions,
471 * very strange. Disable ASPM for the whole slot
Shaohua Liddc97532008-05-21 16:58:40 +0800472 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900473 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
474 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
475 if (!pos)
Shaohua Liddc97532008-05-21 16:58:40 +0800476 return -EINVAL;
Shaohua Li149e1632008-07-23 10:32:31 +0800477 /*
478 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
479 * RBER bit to determine if a function is 1.1 version device
480 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900481 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Sitsofe Wheelere1f4f592008-09-16 14:27:13 +0100482 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
Kenji Kaneshige36475842009-05-13 12:23:09 +0900483 dev_printk(KERN_INFO, &child->dev, "disabling ASPM"
Vincent Legollf393d9b2008-10-12 12:26:12 +0200484 " on pre-1.1 PCIe device. You can enable it"
485 " with 'pcie_aspm=force'\n");
Shaohua Li149e1632008-07-23 10:32:31 +0800486 return -EINVAL;
487 }
Shaohua Liddc97532008-05-21 16:58:40 +0800488 }
489 return 0;
490}
491
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900492static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900493{
494 struct pcie_link_state *link;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900495
496 link = kzalloc(sizeof(*link), GFP_KERNEL);
497 if (!link)
498 return NULL;
499 INIT_LIST_HEAD(&link->sibling);
500 INIT_LIST_HEAD(&link->children);
501 INIT_LIST_HEAD(&link->link);
502 link->pdev = pdev;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900503 if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
504 struct pcie_link_state *parent;
505 parent = pdev->bus->parent->self->link_state;
506 if (!parent) {
507 kfree(link);
508 return NULL;
509 }
510 link->parent = parent;
511 list_add(&link->link, &parent->children);
512 }
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900513 /* Setup a pointer to the root port link */
514 if (!link->parent)
515 link->root = link;
516 else
517 link->root = link->parent->root;
518
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900519 list_add(&link->sibling, &link_list);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900520 pdev->link_state = link;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900521 return link;
522}
523
Shaohua Li7d715a62008-02-25 09:46:41 +0800524/*
525 * pcie_aspm_init_link_state: Initiate PCI express link state.
526 * It is called after the pcie and its children devices are scaned.
527 * @pdev: the root port or switch downstream port
528 */
529void pcie_aspm_init_link_state(struct pci_dev *pdev)
530{
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900531 struct pcie_link_state *link;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900532 int blacklist = !!pcie_aspm_sanity_check(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800533
534 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
535 return;
536 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900537 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
Shaohua Li7d715a62008-02-25 09:46:41 +0800538 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900539
Shaohua Li8e822df2009-06-08 09:27:25 +0800540 /* VIA has a strange chipset, root port is under a bridge */
541 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900542 pdev->bus->self)
Shaohua Li8e822df2009-06-08 09:27:25 +0800543 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900544
Shaohua Li7d715a62008-02-25 09:46:41 +0800545 down_read(&pci_bus_sem);
546 if (list_empty(&pdev->subordinate->devices))
547 goto out;
548
Shaohua Li7d715a62008-02-25 09:46:41 +0800549 mutex_lock(&aspm_lock);
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900550 link = alloc_pcie_link_state(pdev);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900551 if (!link)
552 goto unlock;
553 /*
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900554 * Setup initial ASPM state. Note that we need to configure
555 * upstream links also because capable state of them can be
556 * update through pcie_aspm_cap_init().
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900557 */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900558 pcie_aspm_cap_init(link, blacklist);
559 pcie_config_aspm_path(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800560
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900561 /* Setup initial Clock PM state */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900562 pcie_clkpm_cap_init(link, blacklist);
563 pcie_set_clkpm(link, policy_to_clkpm_state(link));
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900564unlock:
Shaohua Li7d715a62008-02-25 09:46:41 +0800565 mutex_unlock(&aspm_lock);
566out:
567 up_read(&pci_bus_sem);
568}
569
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900570/* Recheck latencies and update aspm_capable for links under the root */
571static void pcie_update_aspm_capable(struct pcie_link_state *root)
572{
573 struct pcie_link_state *link;
574 BUG_ON(root->parent);
575 list_for_each_entry(link, &link_list, sibling) {
576 if (link->root != root)
577 continue;
578 link->aspm_capable = link->aspm_support;
579 }
580 list_for_each_entry(link, &link_list, sibling) {
581 struct pci_dev *child;
582 struct pci_bus *linkbus = link->pdev->subordinate;
583 if (link->root != root)
584 continue;
585 list_for_each_entry(child, &linkbus->devices, bus_list) {
586 if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT) &&
587 (child->pcie_type != PCI_EXP_TYPE_LEG_END))
588 continue;
589 pcie_aspm_check_latency(child);
590 }
591 }
592}
593
Shaohua Li7d715a62008-02-25 09:46:41 +0800594/* @pdev: the endpoint device */
595void pcie_aspm_exit_link_state(struct pci_dev *pdev)
596{
597 struct pci_dev *parent = pdev->bus->self;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900598 struct pcie_link_state *link, *root, *parent_link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800599
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900600 if (aspm_disabled || !pdev->is_pcie || !parent || !parent->link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800601 return;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900602 if ((parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT) &&
603 (parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM))
Shaohua Li7d715a62008-02-25 09:46:41 +0800604 return;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900605
Shaohua Li7d715a62008-02-25 09:46:41 +0800606 down_read(&pci_bus_sem);
607 mutex_lock(&aspm_lock);
Shaohua Li7d715a62008-02-25 09:46:41 +0800608 /*
609 * All PCIe functions are in one slot, remove one function will remove
Alex Chiang3419c752009-01-28 14:59:18 -0700610 * the whole slot, so just wait until we are the last function left.
Shaohua Li7d715a62008-02-25 09:46:41 +0800611 */
Alex Chiang3419c752009-01-28 14:59:18 -0700612 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
Shaohua Li7d715a62008-02-25 09:46:41 +0800613 goto out;
614
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900615 link = parent->link_state;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900616 root = link->root;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900617 parent_link = link->parent;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900618
Shaohua Li7d715a62008-02-25 09:46:41 +0800619 /* All functions are removed, so just disable ASPM for the link */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900620 pcie_config_aspm_link(link, 0);
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900621 list_del(&link->sibling);
622 list_del(&link->link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800623 /* Clock PM is for endpoint device */
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900624 free_link_state(link);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900625
626 /* Recheck latencies and configure upstream links */
627 pcie_update_aspm_capable(root);
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900628 pcie_config_aspm_path(parent_link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800629out:
630 mutex_unlock(&aspm_lock);
631 up_read(&pci_bus_sem);
632}
633
634/* @pdev: the root port or switch downstream port */
635void pcie_aspm_pm_state_change(struct pci_dev *pdev)
636{
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900637 struct pcie_link_state *link = pdev->link_state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800638
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900639 if (aspm_disabled || !pdev->is_pcie || !link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800640 return;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900641 if ((pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT) &&
642 (pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM))
Shaohua Li7d715a62008-02-25 09:46:41 +0800643 return;
644 /*
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900645 * Devices changed PM state, we should recheck if latency
646 * meets all functions' requirement
Shaohua Li7d715a62008-02-25 09:46:41 +0800647 */
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900648 down_read(&pci_bus_sem);
649 mutex_lock(&aspm_lock);
650 pcie_update_aspm_capable(link->root);
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900651 pcie_config_aspm_path(link);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900652 mutex_unlock(&aspm_lock);
653 up_read(&pci_bus_sem);
Shaohua Li7d715a62008-02-25 09:46:41 +0800654}
655
656/*
657 * pci_disable_link_state - disable pci device's link state, so the link will
658 * never enter specific states
659 */
660void pci_disable_link_state(struct pci_dev *pdev, int state)
661{
662 struct pci_dev *parent = pdev->bus->self;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900663 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800664
665 if (aspm_disabled || !pdev->is_pcie)
666 return;
667 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
668 pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
669 parent = pdev;
670 if (!parent || !parent->link_state)
671 return;
672
673 down_read(&pci_bus_sem);
674 mutex_lock(&aspm_lock);
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900675 link = parent->link_state;
676 link->aspm_disable |= state;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900677 pcie_config_aspm_link(link, policy_to_aspm_state(link));
678
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900679 if (state & PCIE_LINK_STATE_CLKPM) {
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900680 link->clkpm_capable = 0;
681 pcie_set_clkpm(link, 0);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900682 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800683 mutex_unlock(&aspm_lock);
684 up_read(&pci_bus_sem);
685}
686EXPORT_SYMBOL(pci_disable_link_state);
687
688static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
689{
690 int i;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900691 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800692
693 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
694 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
695 break;
696 if (i >= ARRAY_SIZE(policy_str))
697 return -EINVAL;
698 if (i == aspm_policy)
699 return 0;
700
701 down_read(&pci_bus_sem);
702 mutex_lock(&aspm_lock);
703 aspm_policy = i;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900704 list_for_each_entry(link, &link_list, sibling) {
705 pcie_config_aspm_link(link, policy_to_aspm_state(link));
706 pcie_set_clkpm(link, policy_to_clkpm_state(link));
Shaohua Li7d715a62008-02-25 09:46:41 +0800707 }
708 mutex_unlock(&aspm_lock);
709 up_read(&pci_bus_sem);
710 return 0;
711}
712
713static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
714{
715 int i, cnt = 0;
716 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
717 if (i == aspm_policy)
718 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
719 else
720 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
721 return cnt;
722}
723
724module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
725 NULL, 0644);
726
727#ifdef CONFIG_PCIEASPM_DEBUG
728static ssize_t link_state_show(struct device *dev,
729 struct device_attribute *attr,
730 char *buf)
731{
732 struct pci_dev *pci_device = to_pci_dev(dev);
733 struct pcie_link_state *link_state = pci_device->link_state;
734
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900735 return sprintf(buf, "%d\n", link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800736}
737
738static ssize_t link_state_store(struct device *dev,
739 struct device_attribute *attr,
740 const char *buf,
741 size_t n)
742{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900743 struct pci_dev *pdev = to_pci_dev(dev);
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900744 struct pcie_link_state *link, *root = pdev->link_state->root;
745 u32 state = buf[0] - '0';
Shaohua Li7d715a62008-02-25 09:46:41 +0800746
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900747 if (n < 1 || state > 3)
Shaohua Li7d715a62008-02-25 09:46:41 +0800748 return -EINVAL;
Shaohua Li7d715a62008-02-25 09:46:41 +0800749
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900750 down_read(&pci_bus_sem);
751 mutex_lock(&aspm_lock);
752 list_for_each_entry(link, &link_list, sibling) {
753 if (link->root != root)
754 continue;
755 pcie_config_aspm_link(link, state);
756 }
757 mutex_unlock(&aspm_lock);
758 up_read(&pci_bus_sem);
759 return n;
Shaohua Li7d715a62008-02-25 09:46:41 +0800760}
761
762static ssize_t clk_ctl_show(struct device *dev,
763 struct device_attribute *attr,
764 char *buf)
765{
766 struct pci_dev *pci_device = to_pci_dev(dev);
767 struct pcie_link_state *link_state = pci_device->link_state;
768
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900769 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800770}
771
772static ssize_t clk_ctl_store(struct device *dev,
773 struct device_attribute *attr,
774 const char *buf,
775 size_t n)
776{
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900777 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800778 int state;
779
780 if (n < 1)
781 return -EINVAL;
782 state = buf[0]-'0';
783
784 down_read(&pci_bus_sem);
785 mutex_lock(&aspm_lock);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900786 pcie_set_clkpm_nocheck(pdev->link_state, !!state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800787 mutex_unlock(&aspm_lock);
788 up_read(&pci_bus_sem);
789
790 return n;
791}
792
793static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
794static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
795
796static char power_group[] = "power";
797void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
798{
799 struct pcie_link_state *link_state = pdev->link_state;
800
801 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
802 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
803 return;
804
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900805 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800806 sysfs_add_file_to_group(&pdev->dev.kobj,
807 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900808 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800809 sysfs_add_file_to_group(&pdev->dev.kobj,
810 &dev_attr_clk_ctl.attr, power_group);
811}
812
813void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
814{
815 struct pcie_link_state *link_state = pdev->link_state;
816
817 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
818 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
819 return;
820
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900821 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800822 sysfs_remove_file_from_group(&pdev->dev.kobj,
823 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900824 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800825 sysfs_remove_file_from_group(&pdev->dev.kobj,
826 &dev_attr_clk_ctl.attr, power_group);
827}
828#endif
829
830static int __init pcie_aspm_disable(char *str)
831{
Shaohua Lid6d38572008-07-23 10:32:42 +0800832 if (!strcmp(str, "off")) {
833 aspm_disabled = 1;
834 printk(KERN_INFO "PCIe ASPM is disabled\n");
835 } else if (!strcmp(str, "force")) {
836 aspm_force = 1;
837 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
838 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800839 return 1;
840}
841
Shaohua Lid6d38572008-07-23 10:32:42 +0800842__setup("pcie_aspm=", pcie_aspm_disable);
Shaohua Li7d715a62008-02-25 09:46:41 +0800843
Shaohua Li5fde2442008-07-23 10:32:24 +0800844void pcie_no_aspm(void)
845{
Shaohua Lid6d38572008-07-23 10:32:42 +0800846 if (!aspm_force)
847 aspm_disabled = 1;
Shaohua Li5fde2442008-07-23 10:32:24 +0800848}
849
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700850/**
851 * pcie_aspm_enabled - is PCIe ASPM enabled?
852 *
853 * Returns true if ASPM has not been disabled by the command-line option
854 * pcie_aspm=off.
855 **/
856int pcie_aspm_enabled(void)
Shaohua Li7d715a62008-02-25 09:46:41 +0800857{
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700858 return !aspm_disabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800859}
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700860EXPORT_SYMBOL(pcie_aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800861