blob: fdb3b7da7383292f5a3c4e94ec7762378debb615 [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
29struct endpoint_state {
30 unsigned int l0s_acceptable_latency;
31 unsigned int l1_acceptable_latency;
32};
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
Shaohua Li7d715a62008-02-25 09:46:41 +080048 /* upstream component */
49 unsigned int l0s_upper_latency;
50 unsigned int l1_upper_latency;
51 /* downstream component */
52 unsigned int l0s_down_latency;
53 unsigned int l1_down_latency;
54 /* Clock PM state*/
55 unsigned int clk_pm_capable;
56 unsigned int clk_pm_enabled;
57 unsigned int bios_clk_state;
58
59 /*
60 * A pcie downstream port only has one slot under it, so at most there
61 * are 8 functions
62 */
63 struct endpoint_state endpoints[8];
64};
65
Shaohua Lid6d38572008-07-23 10:32:42 +080066static int aspm_disabled, aspm_force;
Shaohua Li7d715a62008-02-25 09:46:41 +080067static DEFINE_MUTEX(aspm_lock);
68static LIST_HEAD(link_list);
69
70#define POLICY_DEFAULT 0 /* BIOS default setting */
71#define POLICY_PERFORMANCE 1 /* high performance */
72#define POLICY_POWERSAVE 2 /* high power saving */
73static int aspm_policy;
74static const char *policy_str[] = {
75 [POLICY_DEFAULT] = "default",
76 [POLICY_PERFORMANCE] = "performance",
77 [POLICY_POWERSAVE] = "powersave"
78};
79
Andrew Patterson987a4c72009-01-05 16:21:04 -070080#define LINK_RETRAIN_TIMEOUT HZ
81
Shaohua Li7d715a62008-02-25 09:46:41 +080082static int policy_to_aspm_state(struct pci_dev *pdev)
83{
84 struct pcie_link_state *link_state = pdev->link_state;
85
86 switch (aspm_policy) {
87 case POLICY_PERFORMANCE:
88 /* Disable ASPM and Clock PM */
89 return 0;
90 case POLICY_POWERSAVE:
91 /* Enable ASPM L0s/L1 */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090092 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Shaohua Li7d715a62008-02-25 09:46:41 +080093 case POLICY_DEFAULT:
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090094 return link_state->aspm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +080095 }
96 return 0;
97}
98
99static int policy_to_clkpm_state(struct pci_dev *pdev)
100{
101 struct pcie_link_state *link_state = pdev->link_state;
102
103 switch (aspm_policy) {
104 case POLICY_PERFORMANCE:
105 /* Disable ASPM and Clock PM */
106 return 0;
107 case POLICY_POWERSAVE:
108 /* Disable Clock PM */
109 return 1;
110 case POLICY_DEFAULT:
111 return link_state->bios_clk_state;
112 }
113 return 0;
114}
115
116static void pcie_set_clock_pm(struct pci_dev *pdev, int enable)
117{
118 struct pci_dev *child_dev;
119 int pos;
120 u16 reg16;
121 struct pcie_link_state *link_state = pdev->link_state;
122
123 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
124 pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
125 if (!pos)
126 return;
127 pci_read_config_word(child_dev, pos + PCI_EXP_LNKCTL, &reg16);
128 if (enable)
129 reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
130 else
131 reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
132 pci_write_config_word(child_dev, pos + PCI_EXP_LNKCTL, reg16);
133 }
134 link_state->clk_pm_enabled = !!enable;
135}
136
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800137static void pcie_check_clock_pm(struct pci_dev *pdev, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800138{
139 int pos;
140 u32 reg32;
141 u16 reg16;
142 int capable = 1, enabled = 1;
143 struct pci_dev *child_dev;
144 struct pcie_link_state *link_state = pdev->link_state;
145
146 /* All functions should have the same cap and state, take the worst */
147 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
148 pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
149 if (!pos)
150 return;
151 pci_read_config_dword(child_dev, pos + PCI_EXP_LNKCAP, &reg32);
152 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
153 capable = 0;
154 enabled = 0;
155 break;
156 }
157 pci_read_config_word(child_dev, pos + PCI_EXP_LNKCTL, &reg16);
158 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
159 enabled = 0;
160 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800161 link_state->clk_pm_enabled = enabled;
162 link_state->bios_clk_state = enabled;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800163 if (!blacklist) {
164 link_state->clk_pm_capable = capable;
165 pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
166 } else {
167 link_state->clk_pm_capable = 0;
168 pcie_set_clock_pm(pdev, 0);
169 }
170}
171
172static bool pcie_aspm_downstream_has_switch(struct pci_dev *pdev)
173{
174 struct pci_dev *child_dev;
175
176 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
177 if (child_dev->pcie_type == PCI_EXP_TYPE_UPSTREAM)
178 return true;
179 }
180 return false;
Shaohua Li7d715a62008-02-25 09:46:41 +0800181}
182
183/*
184 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
185 * could use common clock. If they are, configure them to use the
186 * common clock. That will reduce the ASPM state exit latency.
187 */
188static void pcie_aspm_configure_common_clock(struct pci_dev *pdev)
189{
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100190 int pos, child_pos, i = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800191 u16 reg16 = 0;
192 struct pci_dev *child_dev;
193 int same_clock = 1;
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100194 unsigned long start_jiffies;
195 u16 child_regs[8], parent_reg;
Shaohua Li7d715a62008-02-25 09:46:41 +0800196 /*
197 * all functions of a slot should have the same Slot Clock
198 * Configuration, so just check one function
199 * */
200 child_dev = list_entry(pdev->subordinate->devices.next, struct pci_dev,
201 bus_list);
202 BUG_ON(!child_dev->is_pcie);
203
204 /* Check downstream component if bit Slot Clock Configuration is 1 */
205 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
206 pci_read_config_word(child_dev, child_pos + PCI_EXP_LNKSTA, &reg16);
207 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
208 same_clock = 0;
209
210 /* Check upstream component if bit Slot Clock Configuration is 1 */
211 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
212 pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, &reg16);
213 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
214 same_clock = 0;
215
216 /* Configure downstream component, all functions */
217 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
218 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
219 pci_read_config_word(child_dev, child_pos + PCI_EXP_LNKCTL,
220 &reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100221 child_regs[i] = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800222 if (same_clock)
223 reg16 |= PCI_EXP_LNKCTL_CCC;
224 else
225 reg16 &= ~PCI_EXP_LNKCTL_CCC;
226 pci_write_config_word(child_dev, child_pos + PCI_EXP_LNKCTL,
227 reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100228 i++;
Shaohua Li7d715a62008-02-25 09:46:41 +0800229 }
230
231 /* Configure upstream component */
232 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100233 parent_reg = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800234 if (same_clock)
235 reg16 |= PCI_EXP_LNKCTL_CCC;
236 else
237 reg16 &= ~PCI_EXP_LNKCTL_CCC;
238 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
239
240 /* retrain link */
241 reg16 |= PCI_EXP_LNKCTL_RL;
242 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
243
244 /* Wait for link training end */
Andrew Patterson987a4c72009-01-05 16:21:04 -0700245 /* break out after waiting for timeout */
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100246 start_jiffies = jiffies;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700247 for (;;) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800248 pci_read_config_word(pdev, pos + PCI_EXP_LNKSTA, &reg16);
249 if (!(reg16 & PCI_EXP_LNKSTA_LT))
250 break;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700251 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
252 break;
253 msleep(1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800254 }
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100255 /* training failed -> recover */
Andrew Patterson987a4c72009-01-05 16:21:04 -0700256 if (reg16 & PCI_EXP_LNKSTA_LT) {
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100257 dev_printk (KERN_ERR, &pdev->dev, "ASPM: Could not configure"
258 " common clock\n");
259 i = 0;
260 list_for_each_entry(child_dev, &pdev->subordinate->devices,
261 bus_list) {
262 child_pos = pci_find_capability(child_dev,
263 PCI_CAP_ID_EXP);
264 pci_write_config_word(child_dev,
265 child_pos + PCI_EXP_LNKCTL,
266 child_regs[i]);
267 i++;
268 }
269 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, parent_reg);
270 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800271}
272
273/*
274 * calc_L0S_latency: Convert L0s latency encoding to ns
275 */
276static unsigned int calc_L0S_latency(unsigned int latency_encoding, int ac)
277{
278 unsigned int ns = 64;
279
280 if (latency_encoding == 0x7) {
281 if (ac)
282 ns = -1U;
283 else
284 ns = 5*1000; /* > 4us */
285 } else
286 ns *= (1 << latency_encoding);
287 return ns;
288}
289
290/*
291 * calc_L1_latency: Convert L1 latency encoding to ns
292 */
293static unsigned int calc_L1_latency(unsigned int latency_encoding, int ac)
294{
295 unsigned int ns = 1000;
296
297 if (latency_encoding == 0x7) {
298 if (ac)
299 ns = -1U;
300 else
301 ns = 65*1000; /* > 64us */
302 } else
303 ns *= (1 << latency_encoding);
304 return ns;
305}
306
307static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
308 unsigned int *l0s, unsigned int *l1, unsigned int *enabled)
309{
310 int pos;
311 u16 reg16;
312 u32 reg32;
313 unsigned int latency;
314
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900315 *l0s = *l1 = *enabled = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800316 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
317 pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
318 *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
319 if (*state != PCIE_LINK_STATE_L0S &&
320 *state != (PCIE_LINK_STATE_L1|PCIE_LINK_STATE_L0S))
321 *state = 0;
322 if (*state == 0)
323 return;
324
325 latency = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
326 *l0s = calc_L0S_latency(latency, 0);
327 if (*state & PCIE_LINK_STATE_L1) {
328 latency = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
329 *l1 = calc_L1_latency(latency, 0);
330 }
331 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
332 *enabled = reg16 & (PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1);
333}
334
335static void pcie_aspm_cap_init(struct pci_dev *pdev)
336{
337 struct pci_dev *child_dev;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900338 u32 support, l0s, l1, enabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800339 struct pcie_link_state *link_state = pdev->link_state;
340
341 /* upstream component states */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900342 pcie_aspm_get_cap_device(pdev, &support, &l0s, &l1, &enabled);
343 link_state->aspm_support = support;
344 link_state->l0s_upper_latency = l0s;
345 link_state->l1_upper_latency = l1;
346 link_state->aspm_enabled = enabled;
347
Shaohua Li7d715a62008-02-25 09:46:41 +0800348 /* downstream component states, all functions have the same setting */
349 child_dev = list_entry(pdev->subordinate->devices.next, struct pci_dev,
350 bus_list);
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900351 pcie_aspm_get_cap_device(child_dev, &support, &l0s, &l1, &enabled);
352 link_state->aspm_support &= support;
353 link_state->l0s_down_latency = l0s;
354 link_state->l1_down_latency = l1;
355
356 if (!link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800357 return;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900358
359 link_state->aspm_enabled &= link_state->aspm_support;
360 link_state->aspm_default = link_state->aspm_enabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800361
362 /* ENDPOINT states*/
363 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
364 int pos;
365 u32 reg32;
366 unsigned int latency;
367 struct endpoint_state *ep_state =
368 &link_state->endpoints[PCI_FUNC(child_dev->devfn)];
369
370 if (child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
371 child_dev->pcie_type != PCI_EXP_TYPE_LEG_END)
372 continue;
373
374 pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
375 pci_read_config_dword(child_dev, pos + PCI_EXP_DEVCAP, &reg32);
376 latency = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
377 latency = calc_L0S_latency(latency, 1);
378 ep_state->l0s_acceptable_latency = latency;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900379 if (link_state->aspm_support & PCIE_LINK_STATE_L1) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800380 latency = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
381 latency = calc_L1_latency(latency, 1);
382 ep_state->l1_acceptable_latency = latency;
383 }
384 }
385}
386
387static unsigned int __pcie_aspm_check_state_one(struct pci_dev *pdev,
388 unsigned int state)
389{
390 struct pci_dev *parent_dev, *tmp_dev;
391 unsigned int latency, l1_latency = 0;
392 struct pcie_link_state *link_state;
393 struct endpoint_state *ep_state;
394
395 parent_dev = pdev->bus->self;
396 link_state = parent_dev->link_state;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900397 state &= link_state->aspm_support;
Shaohua Li7d715a62008-02-25 09:46:41 +0800398 if (state == 0)
399 return 0;
400 ep_state = &link_state->endpoints[PCI_FUNC(pdev->devfn)];
401
402 /*
403 * Check latency for endpoint device.
404 * TBD: The latency from the endpoint to root complex vary per
405 * switch's upstream link state above the device. Here we just do a
406 * simple check which assumes all links above the device can be in L1
407 * state, that is we just consider the worst case. If switch's upstream
408 * link can't be put into L0S/L1, then our check is too strictly.
409 */
410 tmp_dev = pdev;
411 while (state & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1)) {
412 parent_dev = tmp_dev->bus->self;
413 link_state = parent_dev->link_state;
414 if (state & PCIE_LINK_STATE_L0S) {
415 latency = max_t(unsigned int,
416 link_state->l0s_upper_latency,
417 link_state->l0s_down_latency);
418 if (latency > ep_state->l0s_acceptable_latency)
419 state &= ~PCIE_LINK_STATE_L0S;
420 }
421 if (state & PCIE_LINK_STATE_L1) {
422 latency = max_t(unsigned int,
423 link_state->l1_upper_latency,
424 link_state->l1_down_latency);
425 if (latency + l1_latency >
426 ep_state->l1_acceptable_latency)
427 state &= ~PCIE_LINK_STATE_L1;
428 }
429 if (!parent_dev->bus->self) /* parent_dev is a root port */
430 break;
431 else {
432 /*
433 * parent_dev is the downstream port of a switch, make
434 * tmp_dev the upstream port of the switch
435 */
436 tmp_dev = parent_dev->bus->self;
437 /*
438 * every switch on the path to root complex need 1 more
439 * microsecond for L1. Spec doesn't mention L0S.
440 */
441 if (state & PCIE_LINK_STATE_L1)
442 l1_latency += 1000;
443 }
444 }
445 return state;
446}
447
448static unsigned int pcie_aspm_check_state(struct pci_dev *pdev,
449 unsigned int state)
450{
451 struct pci_dev *child_dev;
452
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800453 /* If no child, ignore the link */
Shaohua Li7d715a62008-02-25 09:46:41 +0800454 if (list_empty(&pdev->subordinate->devices))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800455 return state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800456 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
457 if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
458 /*
459 * If downstream component of a link is pci bridge, we
460 * disable ASPM for now for the link
461 * */
462 state = 0;
463 break;
464 }
465 if ((child_dev->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
466 child_dev->pcie_type != PCI_EXP_TYPE_LEG_END))
467 continue;
468 /* Device not in D0 doesn't need check latency */
469 if (child_dev->current_state == PCI_D1 ||
470 child_dev->current_state == PCI_D2 ||
471 child_dev->current_state == PCI_D3hot ||
472 child_dev->current_state == PCI_D3cold)
473 continue;
474 state = __pcie_aspm_check_state_one(child_dev, state);
475 }
476 return state;
477}
478
479static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
480{
481 u16 reg16;
482 int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
483
484 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
485 reg16 &= ~0x3;
486 reg16 |= state;
487 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
488}
489
490static void __pcie_aspm_config_link(struct pci_dev *pdev, unsigned int state)
491{
492 struct pci_dev *child_dev;
493 int valid = 1;
494 struct pcie_link_state *link_state = pdev->link_state;
495
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800496 /* If no child, disable the link */
497 if (list_empty(&pdev->subordinate->devices))
498 state = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800499 /*
500 * if the downstream component has pci bridge function, don't do ASPM
501 * now
502 */
503 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
504 if (child_dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
505 valid = 0;
506 break;
507 }
508 }
509 if (!valid)
510 return;
511
512 /*
513 * spec 2.0 suggests all functions should be configured the same
514 * setting for ASPM. Enabling ASPM L1 should be done in upstream
515 * component first and then downstream, and vice versa for disabling
516 * ASPM L1. Spec doesn't mention L0S.
517 */
518 if (state & PCIE_LINK_STATE_L1)
519 __pcie_aspm_config_one_dev(pdev, state);
520
521 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list)
522 __pcie_aspm_config_one_dev(child_dev, state);
523
524 if (!(state & PCIE_LINK_STATE_L1))
525 __pcie_aspm_config_one_dev(pdev, state);
526
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900527 link_state->aspm_enabled = state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800528}
529
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800530static struct pcie_link_state *get_root_port_link(struct pcie_link_state *link)
531{
532 struct pcie_link_state *root_port_link = link;
533 while (root_port_link->parent)
534 root_port_link = root_port_link->parent;
535 return root_port_link;
536}
537
538/* check the whole hierarchy, and configure each link in the hierarchy */
Shaohua Li7d715a62008-02-25 09:46:41 +0800539static void __pcie_aspm_configure_link_state(struct pci_dev *pdev,
540 unsigned int state)
541{
542 struct pcie_link_state *link_state = pdev->link_state;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800543 struct pcie_link_state *root_port_link = get_root_port_link(link_state);
544 struct pcie_link_state *leaf;
Shaohua Li7d715a62008-02-25 09:46:41 +0800545
Shaohua Li7d715a62008-02-25 09:46:41 +0800546 state &= PCIE_LINK_STATE_L0S|PCIE_LINK_STATE_L1;
547
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800548 /* check all links who have specific root port link */
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900549 list_for_each_entry(leaf, &link_list, sibling) {
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800550 if (!list_empty(&leaf->children) ||
551 get_root_port_link(leaf) != root_port_link)
552 continue;
553 state = pcie_aspm_check_state(leaf->pdev, state);
554 }
555 /* check root port link too in case it hasn't children */
556 state = pcie_aspm_check_state(root_port_link->pdev, state);
557
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900558 if (link_state->aspm_enabled == state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800559 return;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800560
561 /*
562 * we must change the hierarchy. See comments in
563 * __pcie_aspm_config_link for the order
564 **/
565 if (state & PCIE_LINK_STATE_L1) {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900566 list_for_each_entry(leaf, &link_list, sibling) {
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800567 if (get_root_port_link(leaf) == root_port_link)
568 __pcie_aspm_config_link(leaf->pdev, state);
569 }
570 } else {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900571 list_for_each_entry_reverse(leaf, &link_list, sibling) {
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800572 if (get_root_port_link(leaf) == root_port_link)
573 __pcie_aspm_config_link(leaf->pdev, state);
574 }
575 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800576}
577
578/*
579 * pcie_aspm_configure_link_state: enable/disable PCI express link state
580 * @pdev: the root port or switch downstream port
581 */
582static void pcie_aspm_configure_link_state(struct pci_dev *pdev,
583 unsigned int state)
584{
585 down_read(&pci_bus_sem);
586 mutex_lock(&aspm_lock);
587 __pcie_aspm_configure_link_state(pdev, state);
588 mutex_unlock(&aspm_lock);
589 up_read(&pci_bus_sem);
590}
591
592static void free_link_state(struct pci_dev *pdev)
593{
594 kfree(pdev->link_state);
595 pdev->link_state = NULL;
596}
597
Shaohua Liddc97532008-05-21 16:58:40 +0800598static int pcie_aspm_sanity_check(struct pci_dev *pdev)
599{
600 struct pci_dev *child_dev;
601 int child_pos;
Shaohua Li149e1632008-07-23 10:32:31 +0800602 u32 reg32;
Shaohua Liddc97532008-05-21 16:58:40 +0800603
604 /*
605 * Some functions in a slot might not all be PCIE functions, very
606 * strange. Disable ASPM for the whole slot
607 */
608 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
609 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
610 if (!child_pos)
611 return -EINVAL;
Shaohua Li149e1632008-07-23 10:32:31 +0800612
613 /*
614 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
615 * RBER bit to determine if a function is 1.1 version device
616 */
617 pci_read_config_dword(child_dev, child_pos + PCI_EXP_DEVCAP,
618 &reg32);
Sitsofe Wheelere1f4f592008-09-16 14:27:13 +0100619 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
Vincent Legollf393d9b2008-10-12 12:26:12 +0200620 dev_printk(KERN_INFO, &child_dev->dev, "disabling ASPM"
621 " on pre-1.1 PCIe device. You can enable it"
622 " with 'pcie_aspm=force'\n");
Shaohua Li149e1632008-07-23 10:32:31 +0800623 return -EINVAL;
624 }
Shaohua Liddc97532008-05-21 16:58:40 +0800625 }
626 return 0;
627}
628
Shaohua Li7d715a62008-02-25 09:46:41 +0800629/*
630 * pcie_aspm_init_link_state: Initiate PCI express link state.
631 * It is called after the pcie and its children devices are scaned.
632 * @pdev: the root port or switch downstream port
633 */
634void pcie_aspm_init_link_state(struct pci_dev *pdev)
635{
636 unsigned int state;
637 struct pcie_link_state *link_state;
638 int error = 0;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800639 int blacklist;
Shaohua Li7d715a62008-02-25 09:46:41 +0800640
641 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
642 return;
643 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
644 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
645 return;
Shaohua Li8e822df2009-06-08 09:27:25 +0800646 /* VIA has a strange chipset, root port is under a bridge */
647 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
648 pdev->bus->self)
649 return;
Shaohua Li7d715a62008-02-25 09:46:41 +0800650 down_read(&pci_bus_sem);
651 if (list_empty(&pdev->subordinate->devices))
652 goto out;
653
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800654 blacklist = !!pcie_aspm_sanity_check(pdev);
Shaohua Liddc97532008-05-21 16:58:40 +0800655
Shaohua Li7d715a62008-02-25 09:46:41 +0800656 mutex_lock(&aspm_lock);
657
658 link_state = kzalloc(sizeof(*link_state), GFP_KERNEL);
659 if (!link_state)
660 goto unlock_out;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800661
662 link_state->downstream_has_switch = pcie_aspm_downstream_has_switch(pdev);
663 INIT_LIST_HEAD(&link_state->children);
664 INIT_LIST_HEAD(&link_state->link);
665 if (pdev->bus->self) {/* this is a switch */
666 struct pcie_link_state *parent_link_state;
667
668 parent_link_state = pdev->bus->parent->self->link_state;
669 if (!parent_link_state) {
670 kfree(link_state);
671 goto unlock_out;
672 }
673 list_add(&link_state->link, &parent_link_state->children);
674 link_state->parent = parent_link_state;
675 }
676
Shaohua Li7d715a62008-02-25 09:46:41 +0800677 pdev->link_state = link_state;
678
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800679 if (!blacklist) {
680 pcie_aspm_configure_common_clock(pdev);
681 pcie_aspm_cap_init(pdev);
682 } else {
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900683 link_state->aspm_enabled =
684 (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
685 link_state->aspm_default = 0;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800686 /* Set support state to 0, so we will disable ASPM later */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900687 link_state->aspm_support = 0;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800688 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800689
690 link_state->pdev = pdev;
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900691 list_add(&link_state->sibling, &link_list);
Shaohua Li7d715a62008-02-25 09:46:41 +0800692
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800693 if (link_state->downstream_has_switch) {
694 /*
695 * If link has switch, delay the link config. The leaf link
696 * initialization will config the whole hierarchy. but we must
697 * make sure BIOS doesn't set unsupported link state
698 **/
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900699 state = pcie_aspm_check_state(pdev, link_state->aspm_default);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800700 __pcie_aspm_config_link(pdev, state);
701 } else
702 __pcie_aspm_configure_link_state(pdev,
703 policy_to_aspm_state(pdev));
704
705 pcie_check_clock_pm(pdev, blacklist);
706
Shaohua Li7d715a62008-02-25 09:46:41 +0800707unlock_out:
708 if (error)
709 free_link_state(pdev);
710 mutex_unlock(&aspm_lock);
711out:
712 up_read(&pci_bus_sem);
713}
714
715/* @pdev: the endpoint device */
716void pcie_aspm_exit_link_state(struct pci_dev *pdev)
717{
718 struct pci_dev *parent = pdev->bus->self;
719 struct pcie_link_state *link_state = parent->link_state;
720
721 if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
722 return;
723 if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
724 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
725 return;
726 down_read(&pci_bus_sem);
727 mutex_lock(&aspm_lock);
728
729 /*
730 * All PCIe functions are in one slot, remove one function will remove
Alex Chiang3419c752009-01-28 14:59:18 -0700731 * the whole slot, so just wait until we are the last function left.
Shaohua Li7d715a62008-02-25 09:46:41 +0800732 */
Alex Chiang3419c752009-01-28 14:59:18 -0700733 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
Shaohua Li7d715a62008-02-25 09:46:41 +0800734 goto out;
735
736 /* All functions are removed, so just disable ASPM for the link */
737 __pcie_aspm_config_one_dev(parent, 0);
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900738 list_del(&link_state->sibling);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800739 list_del(&link_state->link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800740 /* Clock PM is for endpoint device */
741
742 free_link_state(parent);
743out:
744 mutex_unlock(&aspm_lock);
745 up_read(&pci_bus_sem);
746}
747
748/* @pdev: the root port or switch downstream port */
749void pcie_aspm_pm_state_change(struct pci_dev *pdev)
750{
751 struct pcie_link_state *link_state = pdev->link_state;
752
753 if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
754 return;
755 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
756 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
757 return;
758 /*
759 * devices changed PM state, we should recheck if latency meets all
760 * functions' requirement
761 */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900762 pcie_aspm_configure_link_state(pdev, link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800763}
764
765/*
766 * pci_disable_link_state - disable pci device's link state, so the link will
767 * never enter specific states
768 */
769void pci_disable_link_state(struct pci_dev *pdev, int state)
770{
771 struct pci_dev *parent = pdev->bus->self;
772 struct pcie_link_state *link_state;
773
774 if (aspm_disabled || !pdev->is_pcie)
775 return;
776 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
777 pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
778 parent = pdev;
779 if (!parent || !parent->link_state)
780 return;
781
782 down_read(&pci_bus_sem);
783 mutex_lock(&aspm_lock);
784 link_state = parent->link_state;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900785 link_state->aspm_support &= ~state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800786 if (state & PCIE_LINK_STATE_CLKPM)
787 link_state->clk_pm_capable = 0;
788
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900789 __pcie_aspm_configure_link_state(parent, link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800790 if (!link_state->clk_pm_capable && link_state->clk_pm_enabled)
791 pcie_set_clock_pm(parent, 0);
792 mutex_unlock(&aspm_lock);
793 up_read(&pci_bus_sem);
794}
795EXPORT_SYMBOL(pci_disable_link_state);
796
797static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
798{
799 int i;
800 struct pci_dev *pdev;
801 struct pcie_link_state *link_state;
802
803 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
804 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
805 break;
806 if (i >= ARRAY_SIZE(policy_str))
807 return -EINVAL;
808 if (i == aspm_policy)
809 return 0;
810
811 down_read(&pci_bus_sem);
812 mutex_lock(&aspm_lock);
813 aspm_policy = i;
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900814 list_for_each_entry(link_state, &link_list, sibling) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800815 pdev = link_state->pdev;
816 __pcie_aspm_configure_link_state(pdev,
817 policy_to_aspm_state(pdev));
818 if (link_state->clk_pm_capable &&
819 link_state->clk_pm_enabled != policy_to_clkpm_state(pdev))
820 pcie_set_clock_pm(pdev, policy_to_clkpm_state(pdev));
821
822 }
823 mutex_unlock(&aspm_lock);
824 up_read(&pci_bus_sem);
825 return 0;
826}
827
828static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
829{
830 int i, cnt = 0;
831 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
832 if (i == aspm_policy)
833 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
834 else
835 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
836 return cnt;
837}
838
839module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
840 NULL, 0644);
841
842#ifdef CONFIG_PCIEASPM_DEBUG
843static ssize_t link_state_show(struct device *dev,
844 struct device_attribute *attr,
845 char *buf)
846{
847 struct pci_dev *pci_device = to_pci_dev(dev);
848 struct pcie_link_state *link_state = pci_device->link_state;
849
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900850 return sprintf(buf, "%d\n", link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800851}
852
853static ssize_t link_state_store(struct device *dev,
854 struct device_attribute *attr,
855 const char *buf,
856 size_t n)
857{
858 struct pci_dev *pci_device = to_pci_dev(dev);
859 int state;
860
861 if (n < 1)
862 return -EINVAL;
863 state = buf[0]-'0';
864 if (state >= 0 && state <= 3) {
865 /* setup link aspm state */
866 pcie_aspm_configure_link_state(pci_device, state);
867 return n;
868 }
869
870 return -EINVAL;
871}
872
873static ssize_t clk_ctl_show(struct device *dev,
874 struct device_attribute *attr,
875 char *buf)
876{
877 struct pci_dev *pci_device = to_pci_dev(dev);
878 struct pcie_link_state *link_state = pci_device->link_state;
879
880 return sprintf(buf, "%d\n", link_state->clk_pm_enabled);
881}
882
883static ssize_t clk_ctl_store(struct device *dev,
884 struct device_attribute *attr,
885 const char *buf,
886 size_t n)
887{
888 struct pci_dev *pci_device = to_pci_dev(dev);
889 int state;
890
891 if (n < 1)
892 return -EINVAL;
893 state = buf[0]-'0';
894
895 down_read(&pci_bus_sem);
896 mutex_lock(&aspm_lock);
897 pcie_set_clock_pm(pci_device, !!state);
898 mutex_unlock(&aspm_lock);
899 up_read(&pci_bus_sem);
900
901 return n;
902}
903
904static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
905static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
906
907static char power_group[] = "power";
908void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
909{
910 struct pcie_link_state *link_state = pdev->link_state;
911
912 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
913 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
914 return;
915
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900916 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800917 sysfs_add_file_to_group(&pdev->dev.kobj,
918 &dev_attr_link_state.attr, power_group);
919 if (link_state->clk_pm_capable)
920 sysfs_add_file_to_group(&pdev->dev.kobj,
921 &dev_attr_clk_ctl.attr, power_group);
922}
923
924void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
925{
926 struct pcie_link_state *link_state = pdev->link_state;
927
928 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
929 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
930 return;
931
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900932 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800933 sysfs_remove_file_from_group(&pdev->dev.kobj,
934 &dev_attr_link_state.attr, power_group);
935 if (link_state->clk_pm_capable)
936 sysfs_remove_file_from_group(&pdev->dev.kobj,
937 &dev_attr_clk_ctl.attr, power_group);
938}
939#endif
940
941static int __init pcie_aspm_disable(char *str)
942{
Shaohua Lid6d38572008-07-23 10:32:42 +0800943 if (!strcmp(str, "off")) {
944 aspm_disabled = 1;
945 printk(KERN_INFO "PCIe ASPM is disabled\n");
946 } else if (!strcmp(str, "force")) {
947 aspm_force = 1;
948 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
949 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800950 return 1;
951}
952
Shaohua Lid6d38572008-07-23 10:32:42 +0800953__setup("pcie_aspm=", pcie_aspm_disable);
Shaohua Li7d715a62008-02-25 09:46:41 +0800954
Shaohua Li5fde2442008-07-23 10:32:24 +0800955void pcie_no_aspm(void)
956{
Shaohua Lid6d38572008-07-23 10:32:42 +0800957 if (!aspm_force)
958 aspm_disabled = 1;
Shaohua Li5fde2442008-07-23 10:32:24 +0800959}
960
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700961/**
962 * pcie_aspm_enabled - is PCIe ASPM enabled?
963 *
964 * Returns true if ASPM has not been disabled by the command-line option
965 * pcie_aspm=off.
966 **/
967int pcie_aspm_enabled(void)
Shaohua Li7d715a62008-02-25 09:46:41 +0800968{
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700969 return !aspm_disabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800970}
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700971EXPORT_SYMBOL(pcie_aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800972