]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pci/pcie/aspm.c
PCI ASPM: remove has_switch field
[mirror_ubuntu-bionic-kernel.git] / drivers / pci / pcie / aspm.c
CommitLineData
7d715a6c
SL
1/*
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>
2a42d9db 19#include <linux/jiffies.h>
987a4c78 20#include <linux/delay.h>
7d715a6c
SL
21#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
b6c2e54d
KK
29struct aspm_latency {
30 u32 l0s; /* L0s latency (nsec) */
31 u32 l1; /* L1 latency (nsec) */
7d715a6c
SL
32};
33
34struct pcie_link_state {
5cde89d8
KK
35 struct pci_dev *pdev; /* Upstream component of the Link */
36 struct pcie_link_state *parent; /* pointer to the parent Link state */
37 struct list_head sibling; /* node in link_list */
38 struct list_head children; /* list of child link states */
39 struct list_head link; /* node in parent's children list */
7d715a6c
SL
40
41 /* ASPM state */
80bfdbe3
KK
42 u32 aspm_support:2; /* Supported ASPM state */
43 u32 aspm_enabled:2; /* Enabled ASPM state */
44 u32 aspm_default:2; /* Default ASPM state by BIOS */
45
4d246e45
KK
46 /* Clock PM state */
47 u32 clkpm_capable:1; /* Clock PM capable? */
48 u32 clkpm_enabled:1; /* Current Clock PM state */
49 u32 clkpm_default:1; /* Default Clock PM state by BIOS */
50
b6c2e54d
KK
51 /* Latencies */
52 struct aspm_latency latency; /* Exit latency */
7d715a6c 53 /*
b6c2e54d
KK
54 * Endpoint acceptable latencies. A pcie downstream port only
55 * has one slot under it, so at most there are 8 functions.
7d715a6c 56 */
b6c2e54d 57 struct aspm_latency acceptable[8];
7d715a6c
SL
58};
59
d6d38574 60static int aspm_disabled, aspm_force;
7d715a6c
SL
61static DEFINE_MUTEX(aspm_lock);
62static LIST_HEAD(link_list);
63
64#define POLICY_DEFAULT 0 /* BIOS default setting */
65#define POLICY_PERFORMANCE 1 /* high performance */
66#define POLICY_POWERSAVE 2 /* high power saving */
67static int aspm_policy;
68static const char *policy_str[] = {
69 [POLICY_DEFAULT] = "default",
70 [POLICY_PERFORMANCE] = "performance",
71 [POLICY_POWERSAVE] = "powersave"
72};
73
987a4c78
AP
74#define LINK_RETRAIN_TIMEOUT HZ
75
5aa63583 76static int policy_to_aspm_state(struct pcie_link_state *link)
7d715a6c 77{
7d715a6c
SL
78 switch (aspm_policy) {
79 case POLICY_PERFORMANCE:
80 /* Disable ASPM and Clock PM */
81 return 0;
82 case POLICY_POWERSAVE:
83 /* Enable ASPM L0s/L1 */
80bfdbe3 84 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
7d715a6c 85 case POLICY_DEFAULT:
5aa63583 86 return link->aspm_default;
7d715a6c
SL
87 }
88 return 0;
89}
90
5aa63583 91static int policy_to_clkpm_state(struct pcie_link_state *link)
7d715a6c 92{
7d715a6c
SL
93 switch (aspm_policy) {
94 case POLICY_PERFORMANCE:
95 /* Disable ASPM and Clock PM */
96 return 0;
97 case POLICY_POWERSAVE:
98 /* Disable Clock PM */
99 return 1;
100 case POLICY_DEFAULT:
5aa63583 101 return link->clkpm_default;
7d715a6c
SL
102 }
103 return 0;
104}
105
430842e2 106static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
7d715a6c 107{
7d715a6c
SL
108 int pos;
109 u16 reg16;
5aa63583
KK
110 struct pci_dev *child;
111 struct pci_bus *linkbus = link->pdev->subordinate;
7d715a6c 112
5aa63583
KK
113 list_for_each_entry(child, &linkbus->devices, bus_list) {
114 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
7d715a6c
SL
115 if (!pos)
116 return;
5aa63583 117 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
7d715a6c
SL
118 if (enable)
119 reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
120 else
121 reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
5aa63583 122 pci_write_config_word(child, pos + PCI_EXP_LNKCTL, reg16);
7d715a6c 123 }
5aa63583 124 link->clkpm_enabled = !!enable;
7d715a6c
SL
125}
126
430842e2
KK
127static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
128{
129 /* Don't enable Clock PM if the link is not Clock PM capable */
130 if (!link->clkpm_capable && enable)
131 return;
132 /* Need nothing if the specified equals to current state */
133 if (link->clkpm_enabled == enable)
134 return;
135 pcie_set_clkpm_nocheck(link, enable);
136}
137
8d349ace 138static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
7d715a6c 139{
5aa63583 140 int pos, capable = 1, enabled = 1;
7d715a6c
SL
141 u32 reg32;
142 u16 reg16;
5aa63583
KK
143 struct pci_dev *child;
144 struct pci_bus *linkbus = link->pdev->subordinate;
7d715a6c
SL
145
146 /* All functions should have the same cap and state, take the worst */
5aa63583
KK
147 list_for_each_entry(child, &linkbus->devices, bus_list) {
148 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
7d715a6c
SL
149 if (!pos)
150 return;
5aa63583 151 pci_read_config_dword(child, pos + PCI_EXP_LNKCAP, &reg32);
7d715a6c
SL
152 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
153 capable = 0;
154 enabled = 0;
155 break;
156 }
5aa63583 157 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
7d715a6c
SL
158 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
159 enabled = 0;
160 }
5aa63583
KK
161 link->clkpm_enabled = enabled;
162 link->clkpm_default = enabled;
8d349ace 163 link->clkpm_capable = (blacklist) ? 0 : capable;
46bbdfa4
SL
164}
165
5aa63583 166static bool pcie_aspm_downstream_has_switch(struct pcie_link_state *link)
46bbdfa4 167{
5aa63583
KK
168 struct pci_dev *child;
169 struct pci_bus *linkbus = link->pdev->subordinate;
46bbdfa4 170
5aa63583
KK
171 list_for_each_entry(child, &linkbus->devices, bus_list) {
172 if (child->pcie_type == PCI_EXP_TYPE_UPSTREAM)
46bbdfa4
SL
173 return true;
174 }
175 return false;
7d715a6c
SL
176}
177
178/*
179 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
180 * could use common clock. If they are, configure them to use the
181 * common clock. That will reduce the ASPM state exit latency.
182 */
5aa63583 183static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
7d715a6c 184{
5aa63583
KK
185 int ppos, cpos, same_clock = 1;
186 u16 reg16, parent_reg, child_reg[8];
2a42d9db 187 unsigned long start_jiffies;
5aa63583
KK
188 struct pci_dev *child, *parent = link->pdev;
189 struct pci_bus *linkbus = parent->subordinate;
7d715a6c 190 /*
5aa63583 191 * All functions of a slot should have the same Slot Clock
7d715a6c 192 * Configuration, so just check one function
5aa63583
KK
193 */
194 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
195 BUG_ON(!child->is_pcie);
7d715a6c
SL
196
197 /* Check downstream component if bit Slot Clock Configuration is 1 */
5aa63583
KK
198 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
199 pci_read_config_word(child, cpos + PCI_EXP_LNKSTA, &reg16);
7d715a6c
SL
200 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
201 same_clock = 0;
202
203 /* Check upstream component if bit Slot Clock Configuration is 1 */
5aa63583
KK
204 ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
205 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
7d715a6c
SL
206 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
207 same_clock = 0;
208
209 /* Configure downstream component, all functions */
5aa63583
KK
210 list_for_each_entry(child, &linkbus->devices, bus_list) {
211 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
212 pci_read_config_word(child, cpos + PCI_EXP_LNKCTL, &reg16);
213 child_reg[PCI_FUNC(child->devfn)] = reg16;
7d715a6c
SL
214 if (same_clock)
215 reg16 |= PCI_EXP_LNKCTL_CCC;
216 else
217 reg16 &= ~PCI_EXP_LNKCTL_CCC;
5aa63583 218 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL, reg16);
7d715a6c
SL
219 }
220
221 /* Configure upstream component */
5aa63583 222 pci_read_config_word(parent, ppos + PCI_EXP_LNKCTL, &reg16);
2a42d9db 223 parent_reg = reg16;
7d715a6c
SL
224 if (same_clock)
225 reg16 |= PCI_EXP_LNKCTL_CCC;
226 else
227 reg16 &= ~PCI_EXP_LNKCTL_CCC;
5aa63583 228 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
7d715a6c 229
5aa63583 230 /* Retrain link */
7d715a6c 231 reg16 |= PCI_EXP_LNKCTL_RL;
5aa63583 232 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
7d715a6c 233
5aa63583 234 /* Wait for link training end. Break out after waiting for timeout */
2a42d9db 235 start_jiffies = jiffies;
987a4c78 236 for (;;) {
5aa63583 237 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
7d715a6c
SL
238 if (!(reg16 & PCI_EXP_LNKSTA_LT))
239 break;
987a4c78
AP
240 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
241 break;
242 msleep(1);
7d715a6c 243 }
5aa63583
KK
244 if (!(reg16 & PCI_EXP_LNKSTA_LT))
245 return;
246
247 /* Training failed. Restore common clock configurations */
248 dev_printk(KERN_ERR, &parent->dev,
249 "ASPM: Could not configure common clock\n");
250 list_for_each_entry(child, &linkbus->devices, bus_list) {
251 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
252 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL,
253 child_reg[PCI_FUNC(child->devfn)]);
2a42d9db 254 }
5aa63583 255 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, parent_reg);
7d715a6c
SL
256}
257
5e0eaa7d
KK
258/* Convert L0s latency encoding to ns */
259static u32 calc_l0s_latency(u32 encoding)
7d715a6c 260{
5e0eaa7d
KK
261 if (encoding == 0x7)
262 return (5 * 1000); /* > 4us */
263 return (64 << encoding);
264}
7d715a6c 265
5e0eaa7d
KK
266/* Convert L0s acceptable latency encoding to ns */
267static u32 calc_l0s_acceptable(u32 encoding)
268{
269 if (encoding == 0x7)
270 return -1U;
271 return (64 << encoding);
7d715a6c
SL
272}
273
5e0eaa7d
KK
274/* Convert L1 latency encoding to ns */
275static u32 calc_l1_latency(u32 encoding)
7d715a6c 276{
5e0eaa7d
KK
277 if (encoding == 0x7)
278 return (65 * 1000); /* > 64us */
279 return (1000 << encoding);
280}
7d715a6c 281
5e0eaa7d
KK
282/* Convert L1 acceptable latency encoding to ns */
283static u32 calc_l1_acceptable(u32 encoding)
284{
285 if (encoding == 0x7)
286 return -1U;
287 return (1000 << encoding);
7d715a6c
SL
288}
289
290static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
7ab70991 291 u32 *l0s, u32 *l1, u32 *enabled)
7d715a6c
SL
292{
293 int pos;
294 u16 reg16;
5e0eaa7d 295 u32 reg32, encoding;
7d715a6c 296
80bfdbe3 297 *l0s = *l1 = *enabled = 0;
7d715a6c
SL
298 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
299 pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
300 *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
301 if (*state != PCIE_LINK_STATE_L0S &&
7ab70991 302 *state != (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L0S))
7d715a6c
SL
303 *state = 0;
304 if (*state == 0)
305 return;
306
5e0eaa7d
KK
307 encoding = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
308 *l0s = calc_l0s_latency(encoding);
7d715a6c 309 if (*state & PCIE_LINK_STATE_L1) {
5e0eaa7d
KK
310 encoding = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
311 *l1 = calc_l1_latency(encoding);
7d715a6c
SL
312 }
313 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
7ab70991 314 *enabled = reg16 & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
7d715a6c
SL
315}
316
8d349ace 317static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
7d715a6c 318{
80bfdbe3 319 u32 support, l0s, l1, enabled;
5aa63583
KK
320 struct pci_dev *child, *parent = link->pdev;
321 struct pci_bus *linkbus = parent->subordinate;
7d715a6c 322
8d349ace
KK
323 if (blacklist) {
324 /* Set support state to 0, so we will disable ASPM later */
325 link->aspm_support = 0;
326 link->aspm_default = 0;
327 link->aspm_enabled = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
328 return;
329 }
330
331 /* Configure common clock before checking latencies */
332 pcie_aspm_configure_common_clock(link);
333
7d715a6c 334 /* upstream component states */
5aa63583
KK
335 pcie_aspm_get_cap_device(parent, &support, &l0s, &l1, &enabled);
336 link->aspm_support = support;
337 link->latency.l0s = l0s;
338 link->latency.l1 = l1;
339 link->aspm_enabled = enabled;
80bfdbe3 340
7d715a6c 341 /* downstream component states, all functions have the same setting */
5aa63583
KK
342 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
343 pcie_aspm_get_cap_device(child, &support, &l0s, &l1, &enabled);
344 link->aspm_support &= support;
345 link->latency.l0s = max_t(u32, link->latency.l0s, l0s);
346 link->latency.l1 = max_t(u32, link->latency.l1, l1);
347
348 if (!link->aspm_support)
7d715a6c 349 return;
80bfdbe3 350
5aa63583
KK
351 link->aspm_enabled &= link->aspm_support;
352 link->aspm_default = link->aspm_enabled;
7d715a6c
SL
353
354 /* ENDPOINT states*/
5aa63583 355 list_for_each_entry(child, &linkbus->devices, bus_list) {
7d715a6c 356 int pos;
5e0eaa7d 357 u32 reg32, encoding;
b6c2e54d 358 struct aspm_latency *acceptable =
5aa63583 359 &link->acceptable[PCI_FUNC(child->devfn)];
7d715a6c 360
5aa63583
KK
361 if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
362 child->pcie_type != PCI_EXP_TYPE_LEG_END)
7d715a6c
SL
363 continue;
364
5aa63583
KK
365 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
366 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
5e0eaa7d
KK
367 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
368 acceptable->l0s = calc_l0s_acceptable(encoding);
5aa63583 369 if (link->aspm_support & PCIE_LINK_STATE_L1) {
5e0eaa7d
KK
370 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
371 acceptable->l1 = calc_l1_acceptable(encoding);
7d715a6c
SL
372 }
373 }
374}
375
f7ea3d7f
KK
376/**
377 * __pcie_aspm_check_state_one - check latency for endpoint device.
378 * @endpoint: pointer to the struct pci_dev of endpoint device
379 *
380 * TBD: The latency from the endpoint to root complex vary per switch's
381 * upstream link state above the device. Here we just do a simple check
382 * which assumes all links above the device can be in L1 state, that
383 * is we just consider the worst case. If switch's upstream link can't
384 * be put into L0S/L1, then our check is too strictly.
385 */
386static u32 __pcie_aspm_check_state_one(struct pci_dev *endpoint, u32 state)
7d715a6c 387{
f7ea3d7f 388 u32 l1_switch_latency = 0;
b6c2e54d 389 struct aspm_latency *acceptable;
f7ea3d7f 390 struct pcie_link_state *link;
7d715a6c 391
f7ea3d7f
KK
392 link = endpoint->bus->self->link_state;
393 state &= link->aspm_support;
394 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
7d715a6c 395
f7ea3d7f 396 while (link && state) {
b6c2e54d 397 if ((state & PCIE_LINK_STATE_L0S) &&
f7ea3d7f 398 (link->latency.l0s > acceptable->l0s))
b6c2e54d 399 state &= ~PCIE_LINK_STATE_L0S;
b6c2e54d 400 if ((state & PCIE_LINK_STATE_L1) &&
f7ea3d7f 401 (link->latency.l1 + l1_switch_latency > acceptable->l1))
b6c2e54d 402 state &= ~PCIE_LINK_STATE_L1;
f7ea3d7f
KK
403 link = link->parent;
404 /*
405 * Every switch on the path to root complex need 1
406 * more microsecond for L1. Spec doesn't mention L0s.
407 */
408 l1_switch_latency += 1000;
7d715a6c
SL
409 }
410 return state;
411}
412
5aa63583 413static u32 pcie_aspm_check_state(struct pcie_link_state *link, u32 state)
7d715a6c 414{
5aa63583
KK
415 pci_power_t power_state;
416 struct pci_dev *child;
417 struct pci_bus *linkbus = link->pdev->subordinate;
7d715a6c 418
46bbdfa4 419 /* If no child, ignore the link */
5aa63583 420 if (list_empty(&linkbus->devices))
46bbdfa4 421 return state;
5aa63583
KK
422
423 list_for_each_entry(child, &linkbus->devices, bus_list) {
424 /*
425 * If downstream component of a link is pci bridge, we
426 * disable ASPM for now for the link
427 */
428 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
429 return 0;
430
431 if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
432 child->pcie_type != PCI_EXP_TYPE_LEG_END))
7d715a6c
SL
433 continue;
434 /* Device not in D0 doesn't need check latency */
5aa63583
KK
435 power_state = child->current_state;
436 if (power_state == PCI_D1 || power_state == PCI_D2 ||
437 power_state == PCI_D3hot || power_state == PCI_D3cold)
7d715a6c 438 continue;
5aa63583 439 state = __pcie_aspm_check_state_one(child, state);
7d715a6c
SL
440 }
441 return state;
442}
443
444static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
445{
446 u16 reg16;
447 int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
448
449 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
450 reg16 &= ~0x3;
451 reg16 |= state;
452 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
453}
454
5aa63583 455static void __pcie_aspm_config_link(struct pcie_link_state *link, u32 state)
7d715a6c 456{
5aa63583
KK
457 struct pci_dev *child, *parent = link->pdev;
458 struct pci_bus *linkbus = parent->subordinate;
7d715a6c 459
46bbdfa4 460 /* If no child, disable the link */
5aa63583 461 if (list_empty(&linkbus->devices))
46bbdfa4 462 state = 0;
7d715a6c 463 /*
5aa63583
KK
464 * If the downstream component has pci bridge function, don't
465 * do ASPM now.
7d715a6c 466 */
5aa63583
KK
467 list_for_each_entry(child, &linkbus->devices, bus_list) {
468 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
469 return;
7d715a6c 470 }
7d715a6c 471 /*
5aa63583
KK
472 * Spec 2.0 suggests all functions should be configured the
473 * same setting for ASPM. Enabling ASPM L1 should be done in
474 * upstream component first and then downstream, and vice
475 * versa for disabling ASPM L1. Spec doesn't mention L0S.
7d715a6c
SL
476 */
477 if (state & PCIE_LINK_STATE_L1)
5aa63583 478 __pcie_aspm_config_one_dev(parent, state);
7d715a6c 479
5aa63583
KK
480 list_for_each_entry(child, &linkbus->devices, bus_list)
481 __pcie_aspm_config_one_dev(child, state);
7d715a6c
SL
482
483 if (!(state & PCIE_LINK_STATE_L1))
5aa63583 484 __pcie_aspm_config_one_dev(parent, state);
7d715a6c 485
5aa63583 486 link->aspm_enabled = state;
7d715a6c
SL
487}
488
46bbdfa4
SL
489static struct pcie_link_state *get_root_port_link(struct pcie_link_state *link)
490{
491 struct pcie_link_state *root_port_link = link;
492 while (root_port_link->parent)
493 root_port_link = root_port_link->parent;
494 return root_port_link;
495}
496
5aa63583
KK
497/* Check the whole hierarchy, and configure each link in the hierarchy */
498static void __pcie_aspm_configure_link_state(struct pcie_link_state *link,
499 u32 state)
7d715a6c 500{
5aa63583 501 struct pcie_link_state *leaf, *root = get_root_port_link(link);
7d715a6c 502
5aa63583 503 state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
7d715a6c 504
5aa63583 505 /* Check all links who have specific root port link */
dc64cd11 506 list_for_each_entry(leaf, &link_list, sibling) {
46bbdfa4 507 if (!list_empty(&leaf->children) ||
5aa63583 508 get_root_port_link(leaf) != root)
46bbdfa4 509 continue;
5aa63583 510 state = pcie_aspm_check_state(leaf, state);
46bbdfa4 511 }
5aa63583
KK
512 /* Check root port link too in case it hasn't children */
513 state = pcie_aspm_check_state(root, state);
514 if (link->aspm_enabled == state)
7d715a6c 515 return;
46bbdfa4 516 /*
5aa63583 517 * We must change the hierarchy. See comments in
46bbdfa4
SL
518 * __pcie_aspm_config_link for the order
519 **/
520 if (state & PCIE_LINK_STATE_L1) {
dc64cd11 521 list_for_each_entry(leaf, &link_list, sibling) {
5aa63583
KK
522 if (get_root_port_link(leaf) == root)
523 __pcie_aspm_config_link(leaf, state);
46bbdfa4
SL
524 }
525 } else {
dc64cd11 526 list_for_each_entry_reverse(leaf, &link_list, sibling) {
5aa63583
KK
527 if (get_root_port_link(leaf) == root)
528 __pcie_aspm_config_link(leaf, state);
46bbdfa4
SL
529 }
530 }
7d715a6c
SL
531}
532
533/*
534 * pcie_aspm_configure_link_state: enable/disable PCI express link state
535 * @pdev: the root port or switch downstream port
536 */
5aa63583
KK
537static void pcie_aspm_configure_link_state(struct pcie_link_state *link,
538 u32 state)
7d715a6c
SL
539{
540 down_read(&pci_bus_sem);
541 mutex_lock(&aspm_lock);
5aa63583 542 __pcie_aspm_configure_link_state(link, state);
7d715a6c
SL
543 mutex_unlock(&aspm_lock);
544 up_read(&pci_bus_sem);
545}
546
5aa63583 547static void free_link_state(struct pcie_link_state *link)
7d715a6c 548{
5aa63583
KK
549 link->pdev->link_state = NULL;
550 kfree(link);
7d715a6c
SL
551}
552
ddc9753f
SL
553static int pcie_aspm_sanity_check(struct pci_dev *pdev)
554{
555 struct pci_dev *child_dev;
556 int child_pos;
149e1637 557 u32 reg32;
ddc9753f
SL
558
559 /*
560 * Some functions in a slot might not all be PCIE functions, very
561 * strange. Disable ASPM for the whole slot
562 */
563 list_for_each_entry(child_dev, &pdev->subordinate->devices, bus_list) {
564 child_pos = pci_find_capability(child_dev, PCI_CAP_ID_EXP);
565 if (!child_pos)
566 return -EINVAL;
149e1637
SL
567
568 /*
569 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
570 * RBER bit to determine if a function is 1.1 version device
571 */
572 pci_read_config_dword(child_dev, child_pos + PCI_EXP_DEVCAP,
573 &reg32);
e1f4f59d 574 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
f393d9b1
VL
575 dev_printk(KERN_INFO, &child_dev->dev, "disabling ASPM"
576 " on pre-1.1 PCIe device. You can enable it"
577 " with 'pcie_aspm=force'\n");
149e1637
SL
578 return -EINVAL;
579 }
ddc9753f
SL
580 }
581 return 0;
582}
583
8d349ace
KK
584static struct pcie_link_state *pcie_aspm_setup_link_state(struct pci_dev *pdev)
585{
586 struct pcie_link_state *link;
587 int blacklist = !!pcie_aspm_sanity_check(pdev);
588
589 link = kzalloc(sizeof(*link), GFP_KERNEL);
590 if (!link)
591 return NULL;
592 INIT_LIST_HEAD(&link->sibling);
593 INIT_LIST_HEAD(&link->children);
594 INIT_LIST_HEAD(&link->link);
595 link->pdev = pdev;
8d349ace
KK
596 if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
597 struct pcie_link_state *parent;
598 parent = pdev->bus->parent->self->link_state;
599 if (!parent) {
600 kfree(link);
601 return NULL;
602 }
603 link->parent = parent;
604 list_add(&link->link, &parent->children);
605 }
606 list_add(&link->sibling, &link_list);
607
608 pdev->link_state = link;
609
610 /* Check ASPM capability */
611 pcie_aspm_cap_init(link, blacklist);
612
613 /* Check Clock PM capability */
614 pcie_clkpm_cap_init(link, blacklist);
615
616 return link;
617}
618
7d715a6c
SL
619/*
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{
8d349ace
KK
626 u32 state;
627 struct pcie_link_state *link;
7d715a6c
SL
628
629 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
630 return;
631 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
8d349ace 632 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
7d715a6c 633 return;
8d349ace 634
8e822df7
SL
635 /* VIA has a strange chipset, root port is under a bridge */
636 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
8d349ace 637 pdev->bus->self)
8e822df7 638 return;
8d349ace 639
7d715a6c
SL
640 down_read(&pci_bus_sem);
641 if (list_empty(&pdev->subordinate->devices))
642 goto out;
643
644 mutex_lock(&aspm_lock);
8d349ace
KK
645 link = pcie_aspm_setup_link_state(pdev);
646 if (!link)
647 goto unlock;
648 /*
649 * Setup initial ASPM state
650 *
651 * If link has switch, delay the link config. The leaf link
652 * initialization will config the whole hierarchy. But we must
653 * make sure BIOS doesn't set unsupported link state.
654 */
efdf8288 655 if (pcie_aspm_downstream_has_switch(link)) {
8d349ace
KK
656 state = pcie_aspm_check_state(link, link->aspm_default);
657 __pcie_aspm_config_link(link, state);
46bbdfa4 658 } else {
8d349ace
KK
659 state = policy_to_aspm_state(link);
660 __pcie_aspm_configure_link_state(link, state);
46bbdfa4 661 }
7d715a6c 662
8d349ace
KK
663 /* Setup initial Clock PM state */
664 state = (link->clkpm_capable) ? policy_to_clkpm_state(link) : 0;
430842e2 665 pcie_set_clkpm(link, state);
8d349ace 666unlock:
7d715a6c
SL
667 mutex_unlock(&aspm_lock);
668out:
669 up_read(&pci_bus_sem);
670}
671
672/* @pdev: the endpoint device */
673void pcie_aspm_exit_link_state(struct pci_dev *pdev)
674{
675 struct pci_dev *parent = pdev->bus->self;
676 struct pcie_link_state *link_state = parent->link_state;
677
678 if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
679 return;
680 if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
681 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
682 return;
683 down_read(&pci_bus_sem);
684 mutex_lock(&aspm_lock);
685
686 /*
687 * All PCIe functions are in one slot, remove one function will remove
3419c75e 688 * the whole slot, so just wait until we are the last function left.
7d715a6c 689 */
3419c75e 690 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
7d715a6c
SL
691 goto out;
692
693 /* All functions are removed, so just disable ASPM for the link */
694 __pcie_aspm_config_one_dev(parent, 0);
dc64cd11 695 list_del(&link_state->sibling);
46bbdfa4 696 list_del(&link_state->link);
7d715a6c
SL
697 /* Clock PM is for endpoint device */
698
5aa63583 699 free_link_state(link_state);
7d715a6c
SL
700out:
701 mutex_unlock(&aspm_lock);
702 up_read(&pci_bus_sem);
703}
704
705/* @pdev: the root port or switch downstream port */
706void pcie_aspm_pm_state_change(struct pci_dev *pdev)
707{
708 struct pcie_link_state *link_state = pdev->link_state;
709
710 if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
711 return;
712 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
713 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
714 return;
715 /*
716 * devices changed PM state, we should recheck if latency meets all
717 * functions' requirement
718 */
5aa63583 719 pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
7d715a6c
SL
720}
721
722/*
723 * pci_disable_link_state - disable pci device's link state, so the link will
724 * never enter specific states
725 */
726void pci_disable_link_state(struct pci_dev *pdev, int state)
727{
728 struct pci_dev *parent = pdev->bus->self;
729 struct pcie_link_state *link_state;
730
731 if (aspm_disabled || !pdev->is_pcie)
732 return;
733 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
734 pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
735 parent = pdev;
736 if (!parent || !parent->link_state)
737 return;
738
739 down_read(&pci_bus_sem);
740 mutex_lock(&aspm_lock);
741 link_state = parent->link_state;
80bfdbe3 742 link_state->aspm_support &= ~state;
5aa63583 743 __pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
430842e2
KK
744 if (state & PCIE_LINK_STATE_CLKPM) {
745 link_state->clkpm_capable = 0;
746 pcie_set_clkpm(link_state, 0);
747 }
7d715a6c
SL
748 mutex_unlock(&aspm_lock);
749 up_read(&pci_bus_sem);
750}
751EXPORT_SYMBOL(pci_disable_link_state);
752
753static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
754{
755 int i;
7d715a6c
SL
756 struct pcie_link_state *link_state;
757
758 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
759 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
760 break;
761 if (i >= ARRAY_SIZE(policy_str))
762 return -EINVAL;
763 if (i == aspm_policy)
764 return 0;
765
766 down_read(&pci_bus_sem);
767 mutex_lock(&aspm_lock);
768 aspm_policy = i;
dc64cd11 769 list_for_each_entry(link_state, &link_list, sibling) {
5aa63583
KK
770 __pcie_aspm_configure_link_state(link_state,
771 policy_to_aspm_state(link_state));
430842e2 772 pcie_set_clkpm(link_state, policy_to_clkpm_state(link_state));
7d715a6c
SL
773 }
774 mutex_unlock(&aspm_lock);
775 up_read(&pci_bus_sem);
776 return 0;
777}
778
779static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
780{
781 int i, cnt = 0;
782 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
783 if (i == aspm_policy)
784 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
785 else
786 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
787 return cnt;
788}
789
790module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
791 NULL, 0644);
792
793#ifdef CONFIG_PCIEASPM_DEBUG
794static ssize_t link_state_show(struct device *dev,
795 struct device_attribute *attr,
796 char *buf)
797{
798 struct pci_dev *pci_device = to_pci_dev(dev);
799 struct pcie_link_state *link_state = pci_device->link_state;
800
80bfdbe3 801 return sprintf(buf, "%d\n", link_state->aspm_enabled);
7d715a6c
SL
802}
803
804static ssize_t link_state_store(struct device *dev,
805 struct device_attribute *attr,
806 const char *buf,
807 size_t n)
808{
5aa63583 809 struct pci_dev *pdev = to_pci_dev(dev);
7d715a6c
SL
810 int state;
811
812 if (n < 1)
813 return -EINVAL;
814 state = buf[0]-'0';
815 if (state >= 0 && state <= 3) {
816 /* setup link aspm state */
5aa63583 817 pcie_aspm_configure_link_state(pdev->link_state, state);
7d715a6c
SL
818 return n;
819 }
820
821 return -EINVAL;
822}
823
824static ssize_t clk_ctl_show(struct device *dev,
825 struct device_attribute *attr,
826 char *buf)
827{
828 struct pci_dev *pci_device = to_pci_dev(dev);
829 struct pcie_link_state *link_state = pci_device->link_state;
830
4d246e45 831 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
7d715a6c
SL
832}
833
834static ssize_t clk_ctl_store(struct device *dev,
835 struct device_attribute *attr,
836 const char *buf,
837 size_t n)
838{
430842e2 839 struct pci_dev *pdev = to_pci_dev(dev);
7d715a6c
SL
840 int state;
841
842 if (n < 1)
843 return -EINVAL;
844 state = buf[0]-'0';
845
846 down_read(&pci_bus_sem);
847 mutex_lock(&aspm_lock);
430842e2 848 pcie_set_clkpm_nocheck(pdev->link_state, !!state);
7d715a6c
SL
849 mutex_unlock(&aspm_lock);
850 up_read(&pci_bus_sem);
851
852 return n;
853}
854
855static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
856static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
857
858static char power_group[] = "power";
859void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
860{
861 struct pcie_link_state *link_state = pdev->link_state;
862
863 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
864 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
865 return;
866
80bfdbe3 867 if (link_state->aspm_support)
7d715a6c
SL
868 sysfs_add_file_to_group(&pdev->dev.kobj,
869 &dev_attr_link_state.attr, power_group);
4d246e45 870 if (link_state->clkpm_capable)
7d715a6c
SL
871 sysfs_add_file_to_group(&pdev->dev.kobj,
872 &dev_attr_clk_ctl.attr, power_group);
873}
874
875void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
876{
877 struct pcie_link_state *link_state = pdev->link_state;
878
879 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
880 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
881 return;
882
80bfdbe3 883 if (link_state->aspm_support)
7d715a6c
SL
884 sysfs_remove_file_from_group(&pdev->dev.kobj,
885 &dev_attr_link_state.attr, power_group);
4d246e45 886 if (link_state->clkpm_capable)
7d715a6c
SL
887 sysfs_remove_file_from_group(&pdev->dev.kobj,
888 &dev_attr_clk_ctl.attr, power_group);
889}
890#endif
891
892static int __init pcie_aspm_disable(char *str)
893{
d6d38574
SL
894 if (!strcmp(str, "off")) {
895 aspm_disabled = 1;
896 printk(KERN_INFO "PCIe ASPM is disabled\n");
897 } else if (!strcmp(str, "force")) {
898 aspm_force = 1;
899 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
900 }
7d715a6c
SL
901 return 1;
902}
903
d6d38574 904__setup("pcie_aspm=", pcie_aspm_disable);
7d715a6c 905
5fde244d
SL
906void pcie_no_aspm(void)
907{
d6d38574
SL
908 if (!aspm_force)
909 aspm_disabled = 1;
5fde244d
SL
910}
911
3e1b1600
AP
912/**
913 * pcie_aspm_enabled - is PCIe ASPM enabled?
914 *
915 * Returns true if ASPM has not been disabled by the command-line option
916 * pcie_aspm=off.
917 **/
918int pcie_aspm_enabled(void)
7d715a6c 919{
3e1b1600 920 return !aspm_disabled;
7d715a6c 921}
3e1b1600 922EXPORT_SYMBOL(pcie_aspm_enabled);
7d715a6c 923