blob: 88351c242a485ebdcb791bd7def7b2f574f33ab3 [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 Kaneshigedc64cd12009-05-13 12:11:33 +090035 struct list_head sibling;
Shaohua Li7d715a62008-02-25 09:46:41 +080036 struct pci_dev *pdev;
Shaohua Li46bbdfa2008-12-19 09:27:42 +080037 bool downstream_has_switch;
38
39 struct pcie_link_state *parent;
40 struct list_head children;
41 struct list_head link;
Shaohua Li7d715a62008-02-25 09:46:41 +080042
43 /* ASPM state */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090044 u32 aspm_support:2; /* Supported ASPM state */
45 u32 aspm_enabled:2; /* Enabled ASPM state */
46 u32 aspm_default:2; /* Default ASPM state by BIOS */
47
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090048 /* Latencies */
49 struct aspm_latency latency; /* Exit latency */
50
Shaohua Li7d715a62008-02-25 09:46:41 +080051 /* Clock PM state*/
52 unsigned int clk_pm_capable;
53 unsigned int clk_pm_enabled;
54 unsigned int bios_clk_state;
55
56 /*
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
Shaohua Li7d715a62008-02-25 09:46:41 +080079static int policy_to_aspm_state(struct pci_dev *pdev)
80{
81 struct pcie_link_state *link_state = pdev->link_state;
82
83 switch (aspm_policy) {
84 case POLICY_PERFORMANCE:
85 /* Disable ASPM and Clock PM */
86 return 0;
87 case POLICY_POWERSAVE:
88 /* Enable ASPM L0s/L1 */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090089 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Shaohua Li7d715a62008-02-25 09:46:41 +080090 case POLICY_DEFAULT:
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090091 return link_state->aspm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +080092 }
93 return 0;
94}
95
96static int policy_to_clkpm_state(struct pci_dev *pdev)
97{
98 struct pcie_link_state *link_state = pdev->link_state;
99
100 switch (aspm_policy) {
101 case POLICY_PERFORMANCE:
102 /* Disable ASPM and Clock PM */
103 return 0;
104 case POLICY_POWERSAVE:
105 /* Disable Clock PM */
106 return 1;
107 case POLICY_DEFAULT:
108 return link_state->bios_clk_state;
109 }
110 return 0;
111}
112
113static void pcie_set_clock_pm(struct pci_dev *pdev, int enable)
114{
115 struct pci_dev *child_dev;
116 int pos;
117 u16 reg16;
118 struct pcie_link_state *link_state = pdev->link_state;
119
120 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
121 pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
122 if (!pos)
123 return;
124 pci_read_config_word(child_dev, pos + PCI_EXP_LNKCTL, &reg16);
125 if (enable)
126 reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
127 else
128 reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
129 pci_write_config_word(child_dev, pos + PCI_EXP_LNKCTL, reg16);
130 }
131 link_state->clk_pm_enabled = !!enable;
132}
133
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800134static void pcie_check_clock_pm(struct pci_dev *pdev, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800135{
136 int pos;
137 u32 reg32;
138 u16 reg16;
139 int capable = 1, enabled = 1;
140 struct pci_dev *child_dev;
141 struct pcie_link_state *link_state = pdev->link_state;
142
143 /* All functions should have the same cap and state, take the worst */
144 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
145 pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
146 if (!pos)
147 return;
148 pci_read_config_dword(child_dev, pos + PCI_EXP_LNKCAP, &reg32);
149 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
150 capable = 0;
151 enabled = 0;
152 break;
153 }
154 pci_read_config_word(child_dev, pos + PCI_EXP_LNKCTL, &reg16);
155 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
156 enabled = 0;
157 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800158 link_state->clk_pm_enabled = enabled;
159 link_state->bios_clk_state = enabled;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800160 if (!blacklist) {
161 link_state->clk_pm_capable = capable;
162 pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
163 } else {
164 link_state->clk_pm_capable = 0;
165 pcie_set_clock_pm(pdev, 0);
166 }
167}
168
169static bool pcie_aspm_downstream_has_switch(struct pci_dev *pdev)
170{
171 struct pci_dev *child_dev;
172
173 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
174 if (child_dev->pcie_type == PCI_EXP_TYPE_UPSTREAM)
175 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 */
185static void pcie_aspm_configure_common_clock(struct pci_dev *pdev)
186{
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100187 int pos, child_pos, i = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800188 u16 reg16 = 0;
189 struct pci_dev *child_dev;
190 int same_clock = 1;
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100191 unsigned long start_jiffies;
192 u16 child_regs[8], parent_reg;
Shaohua Li7d715a62008-02-25 09:46:41 +0800193 /*
194 * all functions of a slot should have the same Slot Clock
195 * Configuration, so just check one function
196 * */
197 child_dev = list_entry(pdev->subordinate->devices.next, struct pci_dev,
198 bus_list);
199 BUG_ON(!child_dev->is_pcie);
200
201 /* Check downstream component if bit Slot Clock Configuration is 1 */
202 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
203 pci_read_config_word(child_dev, child_pos + PCI_EXP_LNKSTA, &reg16);
204 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
205 same_clock = 0;
206
207 /* Check upstream component if bit Slot Clock Configuration is 1 */
208 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
209 pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, &reg16);
210 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
211 same_clock = 0;
212
213 /* Configure downstream component, all functions */
214 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
215 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
216 pci_read_config_word(child_dev, child_pos + PCI_EXP_LNKCTL,
217 &reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100218 child_regs[i] = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800219 if (same_clock)
220 reg16 |= PCI_EXP_LNKCTL_CCC;
221 else
222 reg16 &= ~PCI_EXP_LNKCTL_CCC;
223 pci_write_config_word(child_dev, child_pos + PCI_EXP_LNKCTL,
224 reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100225 i++;
Shaohua Li7d715a62008-02-25 09:46:41 +0800226 }
227
228 /* Configure upstream component */
229 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100230 parent_reg = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800231 if (same_clock)
232 reg16 |= PCI_EXP_LNKCTL_CCC;
233 else
234 reg16 &= ~PCI_EXP_LNKCTL_CCC;
235 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
236
237 /* retrain link */
238 reg16 |= PCI_EXP_LNKCTL_RL;
239 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
240
241 /* Wait for link training end */
Andrew Patterson987a4c72009-01-05 16:21:04 -0700242 /* break out after waiting for timeout */
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100243 start_jiffies = jiffies;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700244 for (;;) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800245 pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, &reg16);
246 if (!(reg16 & PCI_EXP_LNKSTA_LT))
247 break;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700248 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
249 break;
250 msleep(1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800251 }
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100252 /* training failed -> recover */
Andrew Patterson987a4c72009-01-05 16:21:04 -0700253 if (reg16 & PCI_EXP_LNKSTA_LT) {
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100254 dev_printk (KERN_ERR, &pdev->dev, "ASPM: Could not configure"
255 " common clock\n");
256 i = 0;
257 list_for_each_entry(child_dev, &pdev->subordinate->devices,
258 bus_list) {
259 child_pos = pci_find_capability(child_dev,
260 PCI_CAP_ID_EXP);
261 pci_write_config_word(child_dev,
262 child_pos + PCI_EXP_LNKCTL,
263 child_regs[i]);
264 i++;
265 }
266 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, parent_reg);
267 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800268}
269
270/*
271 * calc_L0S_latency: Convert L0s latency encoding to ns
272 */
273static unsigned int calc_L0S_latency(unsigned int latency_encoding, int ac)
274{
275 unsigned int ns = 64;
276
277 if (latency_encoding == 0x7) {
278 if (ac)
279 ns = -1U;
280 else
281 ns = 5*1000; /* > 4us */
282 } else
283 ns *= (1 << latency_encoding);
284 return ns;
285}
286
287/*
288 * calc_L1_latency: Convert L1 latency encoding to ns
289 */
290static unsigned int calc_L1_latency(unsigned int latency_encoding, int ac)
291{
292 unsigned int ns = 1000;
293
294 if (latency_encoding == 0x7) {
295 if (ac)
296 ns = -1U;
297 else
298 ns = 65*1000; /* > 64us */
299 } else
300 ns *= (1 << latency_encoding);
301 return ns;
302}
303
304static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
305 unsigned int *l0s, unsigned int *l1, unsigned int *enabled)
306{
307 int pos;
308 u16 reg16;
309 u32 reg32;
310 unsigned int latency;
311
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900312 *l0s = *l1 = *enabled = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800313 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
314 pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
315 *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
316 if (*state != PCIE_LINK_STATE_L0S &&
317 *state != (PCIE_LINK_STATE_L1|PCIE_LINK_STATE_L0S))
318 *state = 0;
319 if (*state == 0)
320 return;
321
322 latency = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
323 *l0s = calc_L0S_latency(latency, 0);
324 if (*state & PCIE_LINK_STATE_L1) {
325 latency = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
326 *l1 = calc_L1_latency(latency, 0);
327 }
328 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
329 *enabled = reg16 & (PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1);
330}
331
332static void pcie_aspm_cap_init(struct pci_dev *pdev)
333{
334 struct pci_dev *child_dev;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900335 u32 support, l0s, l1, enabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800336 struct pcie_link_state *link_state = pdev->link_state;
337
338 /* upstream component states */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900339 pcie_aspm_get_cap_device(pdev, &support, &l0s, &l1, &enabled);
340 link_state->aspm_support = support;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900341 link_state->latency.l0s = l0s;
342 link_state->latency.l1 = l1;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900343 link_state->aspm_enabled = enabled;
344
Shaohua Li7d715a62008-02-25 09:46:41 +0800345 /* downstream component states, all functions have the same setting */
346 child_dev = list_entry(pdev->subordinate->devices.next, struct pci_dev,
347 bus_list);
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900348 pcie_aspm_get_cap_device(child_dev, &support, &l0s, &l1, &enabled);
349 link_state->aspm_support &= support;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900350 link_state->latency.l0s = max_t(u32, link_state->latency.l0s, l0s);
351 link_state->latency.l1 = max_t(u32, link_state->latency.l1, l1);
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900352
353 if (!link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800354 return;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900355
356 link_state->aspm_enabled &= link_state->aspm_support;
357 link_state->aspm_default = link_state->aspm_enabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800358
359 /* ENDPOINT states*/
360 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
361 int pos;
362 u32 reg32;
363 unsigned int latency;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900364 struct aspm_latency *acceptable =
365 &link_state->acceptable[PCI_FUNC(child_dev->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800366
367 if (child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
368 child_dev->pcie_type != PCI_EXP_TYPE_LEG_END)
369 continue;
370
371 pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
372 pci_read_config_dword(child_dev, pos + PCI_EXP_DEVCAP, &reg32);
373 latency = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
374 latency = calc_L0S_latency(latency, 1);
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900375 acceptable->l0s = latency;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900376 if (link_state->aspm_support & PCIE_LINK_STATE_L1) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800377 latency = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
378 latency = calc_L1_latency(latency, 1);
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900379 acceptable->l1 = latency;
Shaohua Li7d715a62008-02-25 09:46:41 +0800380 }
381 }
382}
383
384static unsigned int __pcie_aspm_check_state_one(struct pci_dev *pdev,
385 unsigned int state)
386{
387 struct pci_dev *parent_dev, *tmp_dev;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900388 unsigned int l1_latency = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800389 struct pcie_link_state *link_state;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900390 struct aspm_latency *acceptable;
Shaohua Li7d715a62008-02-25 09:46:41 +0800391
392 parent_dev = pdev->bus->self;
393 link_state = parent_dev->link_state;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900394 state &= link_state->aspm_support;
Shaohua Li7d715a62008-02-25 09:46:41 +0800395 if (state == 0)
396 return 0;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900397 acceptable = &link_state->acceptable[PCI_FUNC(pdev->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800398
399 /*
400 * Check latency for endpoint device.
401 * TBD: The latency from the endpoint to root complex vary per
402 * switch's upstream link state above the device. Here we just do a
403 * simple check which assumes all links above the device can be in L1
404 * state, that is we just consider the worst case. If switch's upstream
405 * link can't be put into L0S/L1, then our check is too strictly.
406 */
407 tmp_dev = pdev;
408 while (state & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1)) {
409 parent_dev = tmp_dev->bus->self;
410 link_state = parent_dev->link_state;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900411 if ((state & PCIE_LINK_STATE_L0S) &&
412 (link_state->latency.l0s > acceptable->l0s))
413 state &= ~PCIE_LINK_STATE_L0S;
414
415 if ((state & PCIE_LINK_STATE_L1) &&
416 (link_state->latency.l1 + l1_latency > acceptable->l1))
417 state &= ~PCIE_LINK_STATE_L1;
418
Shaohua Li7d715a62008-02-25 09:46:41 +0800419 if (!parent_dev->bus->self) /* parent_dev is a root port */
420 break;
421 else {
422 /*
423 * parent_dev is the downstream port of a switch, make
424 * tmp_dev the upstream port of the switch
425 */
426 tmp_dev = parent_dev->bus->self;
427 /*
428 * every switch on the path to root complex need 1 more
429 * microsecond for L1. Spec doesn't mention L0S.
430 */
431 if (state & PCIE_LINK_STATE_L1)
432 l1_latency += 1000;
433 }
434 }
435 return state;
436}
437
438static unsigned int pcie_aspm_check_state(struct pci_dev *pdev,
439 unsigned int state)
440{
441 struct pci_dev *child_dev;
442
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800443 /* If no child, ignore the link */
Shaohua Li7d715a62008-02-25 09:46:41 +0800444 if (list_empty(&pdev->subordinate->devices))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800445 return state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800446 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
447 if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
448 /*
449 * If downstream component of a link is pci bridge, we
450 * disable ASPM for now for the link
451 * */
452 state = 0;
453 break;
454 }
455 if ((child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
456 child_dev->pcie_type != PCI_EXP_TYPE_LEG_END))
457 continue;
458 /* Device not in D0 doesn't need check latency */
459 if (child_dev->current_state == PCI_D1 ||
460 child_dev->current_state == PCI_D2 ||
461 child_dev->current_state == PCI_D3hot ||
462 child_dev->current_state == PCI_D3cold)
463 continue;
464 state = __pcie_aspm_check_state_one(child_dev, state);
465 }
466 return state;
467}
468
469static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
470{
471 u16 reg16;
472 int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
473
474 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
475 reg16 &= ~0x3;
476 reg16 |= state;
477 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
478}
479
480static void __pcie_aspm_config_link(struct pci_dev *pdev, unsigned int state)
481{
482 struct pci_dev *child_dev;
483 int valid = 1;
484 struct pcie_link_state *link_state = pdev->link_state;
485
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800486 /* If no child, disable the link */
487 if (list_empty(&pdev->subordinate->devices))
488 state = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800489 /*
490 * if the downstream component has pci bridge function, don't do ASPM
491 * now
492 */
493 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
494 if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
495 valid = 0;
496 break;
497 }
498 }
499 if (!valid)
500 return;
501
502 /*
503 * spec 2.0 suggests all functions should be configured the same
504 * setting for ASPM. Enabling ASPM L1 should be done in upstream
505 * component first and then downstream, and vice versa for disabling
506 * ASPM L1. Spec doesn't mention L0S.
507 */
508 if (state & PCIE_LINK_STATE_L1)
509 __pcie_aspm_config_one_dev(pdev, state);
510
511 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list)
512 __pcie_aspm_config_one_dev(child_dev, state);
513
514 if (!(state & PCIE_LINK_STATE_L1))
515 __pcie_aspm_config_one_dev(pdev, state);
516
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900517 link_state->aspm_enabled = state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800518}
519
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800520static struct pcie_link_state *get_root_port_link(struct pcie_link_state *link)
521{
522 struct pcie_link_state *root_port_link = link;
523 while (root_port_link->parent)
524 root_port_link = root_port_link->parent;
525 return root_port_link;
526}
527
528/* check the whole hierarchy, and configure each link in the hierarchy */
Shaohua Li7d715a62008-02-25 09:46:41 +0800529static void __pcie_aspm_configure_link_state(struct pci_dev *pdev,
530 unsigned int state)
531{
532 struct pcie_link_state *link_state = pdev->link_state;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800533 struct pcie_link_state *root_port_link = get_root_port_link(link_state);
534 struct pcie_link_state *leaf;
Shaohua Li7d715a62008-02-25 09:46:41 +0800535
Shaohua Li7d715a62008-02-25 09:46:41 +0800536 state &= PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1;
537
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800538 /* check all links who have specific root port link */
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900539 list_for_each_entry(leaf, &link_list, sibling) {
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800540 if (!list_empty(&leaf->children) ||
541 get_root_port_link(leaf) != root_port_link)
542 continue;
543 state = pcie_aspm_check_state(leaf->pdev, state);
544 }
545 /* check root port link too in case it hasn't children */
546 state = pcie_aspm_check_state(root_port_link->pdev, state);
547
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900548 if (link_state->aspm_enabled == state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800549 return;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800550
551 /*
552 * we must change the hierarchy. See comments in
553 * __pcie_aspm_config_link for the order
554 **/
555 if (state & PCIE_LINK_STATE_L1) {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900556 list_for_each_entry(leaf, &link_list, sibling) {
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800557 if (get_root_port_link(leaf) == root_port_link)
558 __pcie_aspm_config_link(leaf->pdev, state);
559 }
560 } else {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900561 list_for_each_entry_reverse(leaf, &link_list, sibling) {
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800562 if (get_root_port_link(leaf) == root_port_link)
563 __pcie_aspm_config_link(leaf->pdev, state);
564 }
565 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800566}
567
568/*
569 * pcie_aspm_configure_link_state: enable/disable PCI express link state
570 * @pdev: the root port or switch downstream port
571 */
572static void pcie_aspm_configure_link_state(struct pci_dev *pdev,
573 unsigned int state)
574{
575 down_read(&pci_bus_sem);
576 mutex_lock(&aspm_lock);
577 __pcie_aspm_configure_link_state(pdev, state);
578 mutex_unlock(&aspm_lock);
579 up_read(&pci_bus_sem);
580}
581
582static void free_link_state(struct pci_dev *pdev)
583{
584 kfree(pdev->link_state);
585 pdev->link_state = NULL;
586}
587
Shaohua Liddc97532008-05-21 16:58:40 +0800588static int pcie_aspm_sanity_check(struct pci_dev *pdev)
589{
590 struct pci_dev *child_dev;
591 int child_pos;
Shaohua Li149e1632008-07-23 10:32:31 +0800592 u32 reg32;
Shaohua Liddc97532008-05-21 16:58:40 +0800593
594 /*
595 * Some functions in a slot might not all be PCIE functions, very
596 * strange. Disable ASPM for the whole slot
597 */
598 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
599 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
600 if (!child_pos)
601 return -EINVAL;
Shaohua Li149e1632008-07-23 10:32:31 +0800602
603 /*
604 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
605 * RBER bit to determine if a function is 1.1 version device
606 */
607 pci_read_config_dword(child_dev, child_pos + PCI_EXP_DEVCAP,
608 &reg32);
Sitsofe Wheelere1f4f592008-09-16 14:27:13 +0100609 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
Vincent Legollf393d9b2008-10-12 12:26:12 +0200610 dev_printk(KERN_INFO, &child_dev->dev, "disabling ASPM"
611 " on pre-1.1 PCIe device. You can enable it"
612 " with 'pcie_aspm=force'\n");
Shaohua Li149e1632008-07-23 10:32:31 +0800613 return -EINVAL;
614 }
Shaohua Liddc97532008-05-21 16:58:40 +0800615 }
616 return 0;
617}
618
Shaohua Li7d715a62008-02-25 09:46:41 +0800619/*
620 * pcie_aspm_init_link_state: Initiate PCI express link state.
621 * It is called after the pcie and its children devices are scaned.
622 * @pdev: the root port or switch downstream port
623 */
624void pcie_aspm_init_link_state(struct pci_dev *pdev)
625{
626 unsigned int state;
627 struct pcie_link_state *link_state;
628 int error = 0;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800629 int blacklist;
Shaohua Li7d715a62008-02-25 09:46:41 +0800630
631 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
632 return;
633 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
634 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
635 return;
Shaohua Li8e822df2009-06-08 09:27:25 +0800636 /* VIA has a strange chipset, root port is under a bridge */
637 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
638 pdev->bus->self)
639 return;
Shaohua Li7d715a62008-02-25 09:46:41 +0800640 down_read(&pci_bus_sem);
641 if (list_empty(&pdev->subordinate->devices))
642 goto out;
643
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800644 blacklist = !!pcie_aspm_sanity_check(pdev);
Shaohua Liddc97532008-05-21 16:58:40 +0800645
Shaohua Li7d715a62008-02-25 09:46:41 +0800646 mutex_lock(&aspm_lock);
647
648 link_state = kzalloc(sizeof(*link_state), GFP_KERNEL);
649 if (!link_state)
650 goto unlock_out;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800651
652 link_state->downstream_has_switch = pcie_aspm_downstream_has_switch(pdev);
653 INIT_LIST_HEAD(&link_state->children);
654 INIT_LIST_HEAD(&link_state->link);
655 if (pdev->bus->self) {/* this is a switch */
656 struct pcie_link_state *parent_link_state;
657
658 parent_link_state = pdev->bus->parent->self->link_state;
659 if (!parent_link_state) {
660 kfree(link_state);
661 goto unlock_out;
662 }
663 list_add(&link_state->link, &parent_link_state->children);
664 link_state->parent = parent_link_state;
665 }
666
Shaohua Li7d715a62008-02-25 09:46:41 +0800667 pdev->link_state = link_state;
668
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800669 if (!blacklist) {
670 pcie_aspm_configure_common_clock(pdev);
671 pcie_aspm_cap_init(pdev);
672 } else {
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900673 link_state->aspm_enabled =
674 (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
675 link_state->aspm_default = 0;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800676 /* Set support state to 0, so we will disable ASPM later */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900677 link_state->aspm_support = 0;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800678 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800679
680 link_state->pdev = pdev;
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900681 list_add(&link_state->sibling, &link_list);
Shaohua Li7d715a62008-02-25 09:46:41 +0800682
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800683 if (link_state->downstream_has_switch) {
684 /*
685 * If link has switch, delay the link config. The leaf link
686 * initialization will config the whole hierarchy. but we must
687 * make sure BIOS doesn't set unsupported link state
688 **/
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900689 state = pcie_aspm_check_state(pdev, link_state->aspm_default);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800690 __pcie_aspm_config_link(pdev, state);
691 } else
692 __pcie_aspm_configure_link_state(pdev,
693 policy_to_aspm_state(pdev));
694
695 pcie_check_clock_pm(pdev, blacklist);
696
Shaohua Li7d715a62008-02-25 09:46:41 +0800697unlock_out:
698 if (error)
699 free_link_state(pdev);
700 mutex_unlock(&aspm_lock);
701out:
702 up_read(&pci_bus_sem);
703}
704
705/* @pdev: the endpoint device */
706void pcie_aspm_exit_link_state(struct pci_dev *pdev)
707{
708 struct pci_dev *parent = pdev->bus->self;
709 struct pcie_link_state *link_state = parent->link_state;
710
711 if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
712 return;
713 if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
714 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
715 return;
716 down_read(&pci_bus_sem);
717 mutex_lock(&aspm_lock);
718
719 /*
720 * All PCIe functions are in one slot, remove one function will remove
Alex Chiang3419c752009-01-28 14:59:18 -0700721 * the whole slot, so just wait until we are the last function left.
Shaohua Li7d715a62008-02-25 09:46:41 +0800722 */
Alex Chiang3419c752009-01-28 14:59:18 -0700723 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
Shaohua Li7d715a62008-02-25 09:46:41 +0800724 goto out;
725
726 /* All functions are removed, so just disable ASPM for the link */
727 __pcie_aspm_config_one_dev(parent, 0);
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900728 list_del(&link_state->sibling);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800729 list_del(&link_state->link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800730 /* Clock PM is for endpoint device */
731
732 free_link_state(parent);
733out:
734 mutex_unlock(&aspm_lock);
735 up_read(&pci_bus_sem);
736}
737
738/* @pdev: the root port or switch downstream port */
739void pcie_aspm_pm_state_change(struct pci_dev *pdev)
740{
741 struct pcie_link_state *link_state = pdev->link_state;
742
743 if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
744 return;
745 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
746 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
747 return;
748 /*
749 * devices changed PM state, we should recheck if latency meets all
750 * functions' requirement
751 */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900752 pcie_aspm_configure_link_state(pdev, link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800753}
754
755/*
756 * pci_disable_link_state - disable pci device's link state, so the link will
757 * never enter specific states
758 */
759void pci_disable_link_state(struct pci_dev *pdev, int state)
760{
761 struct pci_dev *parent = pdev->bus->self;
762 struct pcie_link_state *link_state;
763
764 if (aspm_disabled || !pdev->is_pcie)
765 return;
766 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
767 pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
768 parent = pdev;
769 if (!parent || !parent->link_state)
770 return;
771
772 down_read(&pci_bus_sem);
773 mutex_lock(&aspm_lock);
774 link_state = parent->link_state;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900775 link_state->aspm_support &= ~state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800776 if (state & PCIE_LINK_STATE_CLKPM)
777 link_state->clk_pm_capable = 0;
778
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900779 __pcie_aspm_configure_link_state(parent, link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800780 if (!link_state->clk_pm_capable && link_state->clk_pm_enabled)
781 pcie_set_clock_pm(parent, 0);
782 mutex_unlock(&aspm_lock);
783 up_read(&pci_bus_sem);
784}
785EXPORT_SYMBOL(pci_disable_link_state);
786
787static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
788{
789 int i;
790 struct pci_dev *pdev;
791 struct pcie_link_state *link_state;
792
793 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
794 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
795 break;
796 if (i >= ARRAY_SIZE(policy_str))
797 return -EINVAL;
798 if (i == aspm_policy)
799 return 0;
800
801 down_read(&pci_bus_sem);
802 mutex_lock(&aspm_lock);
803 aspm_policy = i;
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900804 list_for_each_entry(link_state, &link_list, sibling) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800805 pdev = link_state->pdev;
806 __pcie_aspm_configure_link_state(pdev,
807 policy_to_aspm_state(pdev));
808 if (link_state->clk_pm_capable &&
809 link_state->clk_pm_enabled != policy_to_clkpm_state(pdev))
810 pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
811
812 }
813 mutex_unlock(&aspm_lock);
814 up_read(&pci_bus_sem);
815 return 0;
816}
817
818static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
819{
820 int i, cnt = 0;
821 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
822 if (i == aspm_policy)
823 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
824 else
825 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
826 return cnt;
827}
828
829module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
830 NULL, 0644);
831
832#ifdef CONFIG_PCIEASPM_DEBUG
833static ssize_t link_state_show(struct device *dev,
834 struct device_attribute *attr,
835 char *buf)
836{
837 struct pci_dev *pci_device = to_pci_dev(dev);
838 struct pcie_link_state *link_state = pci_device->link_state;
839
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900840 return sprintf(buf, "%d\n", link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800841}
842
843static ssize_t link_state_store(struct device *dev,
844 struct device_attribute *attr,
845 const char *buf,
846 size_t n)
847{
848 struct pci_dev *pci_device = to_pci_dev(dev);
849 int state;
850
851 if (n < 1)
852 return -EINVAL;
853 state = buf[0]-'0';
854 if (state >= 0 && state <= 3) {
855 /* setup link aspm state */
856 pcie_aspm_configure_link_state(pci_device, state);
857 return n;
858 }
859
860 return -EINVAL;
861}
862
863static ssize_t clk_ctl_show(struct device *dev,
864 struct device_attribute *attr,
865 char *buf)
866{
867 struct pci_dev *pci_device = to_pci_dev(dev);
868 struct pcie_link_state *link_state = pci_device->link_state;
869
870 return sprintf(buf, "%d\n", link_state->clk_pm_enabled);
871}
872
873static ssize_t clk_ctl_store(struct device *dev,
874 struct device_attribute *attr,
875 const char *buf,
876 size_t n)
877{
878 struct pci_dev *pci_device = to_pci_dev(dev);
879 int state;
880
881 if (n < 1)
882 return -EINVAL;
883 state = buf[0]-'0';
884
885 down_read(&pci_bus_sem);
886 mutex_lock(&aspm_lock);
887 pcie_set_clock_pm(pci_device, !!state);
888 mutex_unlock(&aspm_lock);
889 up_read(&pci_bus_sem);
890
891 return n;
892}
893
894static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
895static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
896
897static char power_group[] = "power";
898void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
899{
900 struct pcie_link_state *link_state = pdev->link_state;
901
902 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
903 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
904 return;
905
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900906 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800907 sysfs_add_file_to_group(&pdev->dev.kobj,
908 &dev_attr_link_state.attr, power_group);
909 if (link_state->clk_pm_capable)
910 sysfs_add_file_to_group(&pdev->dev.kobj,
911 &dev_attr_clk_ctl.attr, power_group);
912}
913
914void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
915{
916 struct pcie_link_state *link_state = pdev->link_state;
917
918 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
919 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
920 return;
921
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900922 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800923 sysfs_remove_file_from_group(&pdev->dev.kobj,
924 &dev_attr_link_state.attr, power_group);
925 if (link_state->clk_pm_capable)
926 sysfs_remove_file_from_group(&pdev->dev.kobj,
927 &dev_attr_clk_ctl.attr, power_group);
928}
929#endif
930
931static int __init pcie_aspm_disable(char *str)
932{
Shaohua Lid6d38572008-07-23 10:32:42 +0800933 if (!strcmp(str, "off")) {
934 aspm_disabled = 1;
935 printk(KERN_INFO "PCIe ASPM is disabled\n");
936 } else if (!strcmp(str, "force")) {
937 aspm_force = 1;
938 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
939 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800940 return 1;
941}
942
Shaohua Lid6d38572008-07-23 10:32:42 +0800943__setup("pcie_aspm=", pcie_aspm_disable);
Shaohua Li7d715a62008-02-25 09:46:41 +0800944
Shaohua Li5fde2442008-07-23 10:32:24 +0800945void pcie_no_aspm(void)
946{
Shaohua Lid6d38572008-07-23 10:32:42 +0800947 if (!aspm_force)
948 aspm_disabled = 1;
Shaohua Li5fde2442008-07-23 10:32:24 +0800949}
950
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700951/**
952 * pcie_aspm_enabled - is PCIe ASPM enabled?
953 *
954 * Returns true if ASPM has not been disabled by the command-line option
955 * pcie_aspm=off.
956 **/
957int pcie_aspm_enabled(void)
Shaohua Li7d715a62008-02-25 09:46:41 +0800958{
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700959 return !aspm_disabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800960}
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700961EXPORT_SYMBOL(pcie_aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800962