blob: 79f6d61798fa900b3c006260fba8f0e1f6485bde [file] [log] [blame]
Shaohua Li7d715a62008-02-25 09:46:41 +08001/*
2 * File: drivers/pci/pcie/aspm.c
3 * Enabling PCIE link L0s/L1 state and Clock Power Management
4 *
5 * Copyright (C) 2007 Intel
6 * Copyright (C) Zhang Yanmin (yanmin.zhang@intel.com)
7 * Copyright (C) Shaohua Li (shaohua.li@intel.com)
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/pci_regs.h>
15#include <linux/errno.h>
16#include <linux/pm.h>
17#include <linux/init.h>
18#include <linux/slab.h>
Thomas Renninger2a42d9d2008-12-09 13:05:09 +010019#include <linux/jiffies.h>
Andrew Patterson987a4c72009-01-05 16:21:04 -070020#include <linux/delay.h>
Shaohua Li7d715a62008-02-25 09:46:41 +080021#include <linux/pci-aspm.h>
22#include "../pci.h"
23
24#ifdef MODULE_PARAM_PREFIX
25#undef MODULE_PARAM_PREFIX
26#endif
27#define MODULE_PARAM_PREFIX "pcie_aspm."
28
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090029struct aspm_latency {
30 u32 l0s; /* L0s latency (nsec) */
31 u32 l1; /* L1 latency (nsec) */
Shaohua Li7d715a62008-02-25 09:46:41 +080032};
33
34struct pcie_link_state {
Kenji Kaneshige5cde89d2009-05-13 12:17:04 +090035 struct pci_dev *pdev; /* Upstream component of the Link */
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +090036 struct pcie_link_state *root; /* pointer to the root port link */
Kenji Kaneshige5cde89d2009-05-13 12:17:04 +090037 struct pcie_link_state *parent; /* pointer to the parent Link state */
38 struct list_head sibling; /* node in link_list */
39 struct list_head children; /* list of child link states */
40 struct list_head link; /* node in parent's children list */
Shaohua Li7d715a62008-02-25 09:46:41 +080041
42 /* ASPM state */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090043 u32 aspm_support:2; /* Supported ASPM state */
44 u32 aspm_enabled:2; /* Enabled ASPM state */
Kenji Kaneshige07d92762009-08-19 11:00:25 +090045 u32 aspm_capable:2; /* Capable ASPM state with latency */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090046 u32 aspm_default:2; /* Default ASPM state by BIOS */
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +090047 u32 aspm_disable:2; /* Disabled ASPM state */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090048
Kenji Kaneshige4d246e42009-05-13 12:15:38 +090049 /* Clock PM state */
50 u32 clkpm_capable:1; /* Clock PM capable? */
51 u32 clkpm_enabled:1; /* Current Clock PM state */
52 u32 clkpm_default:1; /* Default Clock PM state by BIOS */
53
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090054 /* Latencies */
55 struct aspm_latency latency; /* Exit latency */
Shaohua Li7d715a62008-02-25 09:46:41 +080056 /*
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090057 * Endpoint acceptable latencies. A pcie downstream port only
58 * has one slot under it, so at most there are 8 functions.
Shaohua Li7d715a62008-02-25 09:46:41 +080059 */
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090060 struct aspm_latency acceptable[8];
Shaohua Li7d715a62008-02-25 09:46:41 +080061};
62
Shaohua Lid6d38572008-07-23 10:32:42 +080063static int aspm_disabled, aspm_force;
Shaohua Li7d715a62008-02-25 09:46:41 +080064static DEFINE_MUTEX(aspm_lock);
65static LIST_HEAD(link_list);
66
67#define POLICY_DEFAULT 0 /* BIOS default setting */
68#define POLICY_PERFORMANCE 1 /* high performance */
69#define POLICY_POWERSAVE 2 /* high power saving */
70static int aspm_policy;
71static const char *policy_str[] = {
72 [POLICY_DEFAULT] = "default",
73 [POLICY_PERFORMANCE] = "performance",
74 [POLICY_POWERSAVE] = "powersave"
75};
76
Andrew Patterson987a4c72009-01-05 16:21:04 -070077#define LINK_RETRAIN_TIMEOUT HZ
78
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090079static int policy_to_aspm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080080{
Shaohua Li7d715a62008-02-25 09:46:41 +080081 switch (aspm_policy) {
82 case POLICY_PERFORMANCE:
83 /* Disable ASPM and Clock PM */
84 return 0;
85 case POLICY_POWERSAVE:
86 /* Enable ASPM L0s/L1 */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090087 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Shaohua Li7d715a62008-02-25 09:46:41 +080088 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090089 return link->aspm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +080090 }
91 return 0;
92}
93
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090094static int policy_to_clkpm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080095{
Shaohua Li7d715a62008-02-25 09:46:41 +080096 switch (aspm_policy) {
97 case POLICY_PERFORMANCE:
98 /* Disable ASPM and Clock PM */
99 return 0;
100 case POLICY_POWERSAVE:
101 /* Disable Clock PM */
102 return 1;
103 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900104 return link->clkpm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +0800105 }
106 return 0;
107}
108
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900109static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800110{
Shaohua Li7d715a62008-02-25 09:46:41 +0800111 int pos;
112 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900113 struct pci_dev *child;
114 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800115
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900116 list_for_each_entry(child, &linkbus->devices, bus_list) {
117 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800118 if (!pos)
119 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900120 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800121 if (enable)
122 reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
123 else
124 reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900125 pci_write_config_word(child, pos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800126 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900127 link->clkpm_enabled = !!enable;
Shaohua Li7d715a62008-02-25 09:46:41 +0800128}
129
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900130static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
131{
132 /* Don't enable Clock PM if the link is not Clock PM capable */
133 if (!link->clkpm_capable && enable)
134 return;
135 /* Need nothing if the specified equals to current state */
136 if (link->clkpm_enabled == enable)
137 return;
138 pcie_set_clkpm_nocheck(link, enable);
139}
140
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900141static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800142{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900143 int pos, capable = 1, enabled = 1;
Shaohua Li7d715a62008-02-25 09:46:41 +0800144 u32 reg32;
145 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900146 struct pci_dev *child;
147 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800148
149 /* All functions should have the same cap and state, take the worst */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900150 list_for_each_entry(child, &linkbus->devices, bus_list) {
151 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800152 if (!pos)
153 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900154 pci_read_config_dword(child, pos + PCI_EXP_LNKCAP, &reg32);
Shaohua Li7d715a62008-02-25 09:46:41 +0800155 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
156 capable = 0;
157 enabled = 0;
158 break;
159 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900160 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800161 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
162 enabled = 0;
163 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900164 link->clkpm_enabled = enabled;
165 link->clkpm_default = enabled;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900166 link->clkpm_capable = (blacklist) ? 0 : capable;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800167}
168
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900169static bool pcie_aspm_downstream_has_switch(struct pcie_link_state *link)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800170{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900171 struct pci_dev *child;
172 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800173
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900174 list_for_each_entry(child, &linkbus->devices, bus_list) {
175 if (child->pcie_type == PCI_EXP_TYPE_UPSTREAM)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800176 return true;
177 }
178 return false;
Shaohua Li7d715a62008-02-25 09:46:41 +0800179}
180
181/*
182 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
183 * could use common clock. If they are, configure them to use the
184 * common clock. That will reduce the ASPM state exit latency.
185 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900186static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800187{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900188 int ppos, cpos, same_clock = 1;
189 u16 reg16, parent_reg, child_reg[8];
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100190 unsigned long start_jiffies;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900191 struct pci_dev *child, *parent = link->pdev;
192 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800193 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900194 * All functions of a slot should have the same Slot Clock
Shaohua Li7d715a62008-02-25 09:46:41 +0800195 * Configuration, so just check one function
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900196 */
197 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
198 BUG_ON(!child->is_pcie);
Shaohua Li7d715a62008-02-25 09:46:41 +0800199
200 /* Check downstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900201 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
202 pci_read_config_word(child, cpos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800203 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
204 same_clock = 0;
205
206 /* Check upstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900207 ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
208 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800209 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
210 same_clock = 0;
211
212 /* Configure downstream component, all functions */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900213 list_for_each_entry(child, &linkbus->devices, bus_list) {
214 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
215 pci_read_config_word(child, cpos + PCI_EXP_LNKCTL, &reg16);
216 child_reg[PCI_FUNC(child->devfn)] = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800217 if (same_clock)
218 reg16 |= PCI_EXP_LNKCTL_CCC;
219 else
220 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900221 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800222 }
223
224 /* Configure upstream component */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900225 pci_read_config_word(parent, ppos + PCI_EXP_LNKCTL, &reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100226 parent_reg = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800227 if (same_clock)
228 reg16 |= PCI_EXP_LNKCTL_CCC;
229 else
230 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900231 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800232
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900233 /* Retrain link */
Shaohua Li7d715a62008-02-25 09:46:41 +0800234 reg16 |= PCI_EXP_LNKCTL_RL;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900235 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800236
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900237 /* Wait for link training end. Break out after waiting for timeout */
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100238 start_jiffies = jiffies;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700239 for (;;) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900240 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800241 if (!(reg16 & PCI_EXP_LNKSTA_LT))
242 break;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700243 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
244 break;
245 msleep(1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800246 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900247 if (!(reg16 & PCI_EXP_LNKSTA_LT))
248 return;
249
250 /* Training failed. Restore common clock configurations */
251 dev_printk(KERN_ERR, &parent->dev,
252 "ASPM: Could not configure common clock\n");
253 list_for_each_entry(child, &linkbus->devices, bus_list) {
254 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
255 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL,
256 child_reg[PCI_FUNC(child->devfn)]);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100257 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900258 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, parent_reg);
Shaohua Li7d715a62008-02-25 09:46:41 +0800259}
260
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900261/* Convert L0s latency encoding to ns */
262static u32 calc_l0s_latency(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800263{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900264 if (encoding == 0x7)
265 return (5 * 1000); /* > 4us */
266 return (64 << encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800267}
268
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900269/* Convert L0s acceptable latency encoding to ns */
270static u32 calc_l0s_acceptable(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800271{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900272 if (encoding == 0x7)
273 return -1U;
274 return (64 << encoding);
275}
Shaohua Li7d715a62008-02-25 09:46:41 +0800276
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900277/* Convert L1 latency encoding to ns */
278static u32 calc_l1_latency(u32 encoding)
279{
280 if (encoding == 0x7)
281 return (65 * 1000); /* > 64us */
282 return (1000 << encoding);
283}
284
285/* Convert L1 acceptable latency encoding to ns */
286static u32 calc_l1_acceptable(u32 encoding)
287{
288 if (encoding == 0x7)
289 return -1U;
290 return (1000 << encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800291}
292
293static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900294 u32 *l0s, u32 *l1, u32 *enabled)
Shaohua Li7d715a62008-02-25 09:46:41 +0800295{
296 int pos;
297 u16 reg16;
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900298 u32 reg32, encoding;
Shaohua Li7d715a62008-02-25 09:46:41 +0800299
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900300 *l0s = *l1 = *enabled = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800301 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
302 pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
303 *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
304 if (*state != PCIE_LINK_STATE_L0S &&
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900305 *state != (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L0S))
Shaohua Li7d715a62008-02-25 09:46:41 +0800306 *state = 0;
307 if (*state == 0)
308 return;
309
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900310 encoding = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
311 *l0s = calc_l0s_latency(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800312 if (*state & PCIE_LINK_STATE_L1) {
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900313 encoding = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
314 *l1 = calc_l1_latency(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800315 }
316 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900317 *enabled = reg16 & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800318}
319
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900320static void pcie_aspm_check_latency(struct pci_dev *endpoint)
321{
322 u32 l1_switch_latency = 0;
323 struct aspm_latency *acceptable;
324 struct pcie_link_state *link;
325
326 /* Device not in D0 doesn't need latency check */
327 if ((endpoint->current_state != PCI_D0) &&
328 (endpoint->current_state != PCI_UNKNOWN))
329 return;
330
331 link = endpoint->bus->self->link_state;
332 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
333
334 while (link) {
335 /* Check L0s latency */
336 if ((link->aspm_capable & PCIE_LINK_STATE_L0S) &&
337 (link->latency.l0s > acceptable->l0s))
338 link->aspm_capable &= ~PCIE_LINK_STATE_L0S;
339 /*
340 * Check L1 latency.
341 * Every switch on the path to root complex need 1
342 * more microsecond for L1. Spec doesn't mention L0s.
343 */
344 if ((link->aspm_capable & PCIE_LINK_STATE_L1) &&
345 (link->latency.l1 + l1_switch_latency > acceptable->l1))
346 link->aspm_capable &= ~PCIE_LINK_STATE_L1;
347 l1_switch_latency += 1000;
348
349 link = link->parent;
350 }
351}
352
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900353static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800354{
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900355 u32 support, l0s, l1, enabled;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900356 struct pci_dev *child, *parent = link->pdev;
357 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800358
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900359 if (blacklist) {
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900360 /* Set enabled/disable so that we will disable ASPM later */
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900361 link->aspm_enabled = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900362 link->aspm_disable = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900363 return;
364 }
365
366 /* Configure common clock before checking latencies */
367 pcie_aspm_configure_common_clock(link);
368
Shaohua Li7d715a62008-02-25 09:46:41 +0800369 /* upstream component states */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900370 pcie_aspm_get_cap_device(parent, &support, &l0s, &l1, &enabled);
371 link->aspm_support = support;
372 link->latency.l0s = l0s;
373 link->latency.l1 = l1;
374 link->aspm_enabled = enabled;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900375
Shaohua Li7d715a62008-02-25 09:46:41 +0800376 /* downstream component states, all functions have the same setting */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900377 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
378 pcie_aspm_get_cap_device(child, &support, &l0s, &l1, &enabled);
379 link->aspm_support &= support;
380 link->latency.l0s = max_t(u32, link->latency.l0s, l0s);
381 link->latency.l1 = max_t(u32, link->latency.l1, l1);
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900382
Kenji Kaneshigeb127bd52009-08-19 10:57:31 +0900383 /* Save default state */
384 link->aspm_default = link->aspm_enabled;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900385
386 /* Setup initial capable state. Will be updated later */
387 link->aspm_capable = link->aspm_support;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900388 /*
389 * If the downstream component has pci bridge function, don't
390 * do ASPM for now.
391 */
392 list_for_each_entry(child, &linkbus->devices, bus_list) {
393 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
394 link->aspm_disable =
395 PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
396 break;
397 }
398 }
Kenji Kaneshigeb127bd52009-08-19 10:57:31 +0900399
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900400 if (!link->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800401 return;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900402
Shaohua Li7d715a62008-02-25 09:46:41 +0800403 /* ENDPOINT states*/
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900404 list_for_each_entry(child, &linkbus->devices, bus_list) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800405 int pos;
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900406 u32 reg32, encoding;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900407 struct aspm_latency *acceptable =
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900408 &link->acceptable[PCI_FUNC(child->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800409
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900410 if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
411 child->pcie_type != PCI_EXP_TYPE_LEG_END)
Shaohua Li7d715a62008-02-25 09:46:41 +0800412 continue;
413
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900414 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
415 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900416 /* Calculate endpoint L0s acceptable latency */
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900417 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
418 acceptable->l0s = calc_l0s_acceptable(encoding);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900419 /* Calculate endpoint L1 acceptable latency */
420 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
421 acceptable->l1 = calc_l1_acceptable(encoding);
422
423 pcie_aspm_check_latency(child);
Shaohua Li7d715a62008-02-25 09:46:41 +0800424 }
425}
426
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900427/**
428 * __pcie_aspm_check_state_one - check latency for endpoint device.
429 * @endpoint: pointer to the struct pci_dev of endpoint device
430 *
431 * TBD: The latency from the endpoint to root complex vary per switch's
432 * upstream link state above the device. Here we just do a simple check
433 * which assumes all links above the device can be in L1 state, that
434 * is we just consider the worst case. If switch's upstream link can't
435 * be put into L0S/L1, then our check is too strictly.
436 */
437static u32 __pcie_aspm_check_state_one(struct pci_dev *endpoint, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800438{
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900439 struct pcie_link_state *link = endpoint->bus->self->link_state;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900440 while (link && state) {
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900441 state &= link->aspm_capable;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900442 link = link->parent;
Shaohua Li7d715a62008-02-25 09:46:41 +0800443 }
444 return state;
445}
446
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900447static u32 pcie_aspm_check_state(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800448{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900449 pci_power_t power_state;
450 struct pci_dev *child;
451 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800452
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800453 /* If no child, ignore the link */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900454 if (list_empty(&linkbus->devices))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800455 return state;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900456
457 list_for_each_entry(child, &linkbus->devices, bus_list) {
458 /*
459 * If downstream component of a link is pci bridge, we
460 * disable ASPM for now for the link
461 */
462 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
463 return 0;
464
465 if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
466 child->pcie_type != PCI_EXP_TYPE_LEG_END))
Shaohua Li7d715a62008-02-25 09:46:41 +0800467 continue;
468 /* Device not in D0 doesn't need check latency */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900469 power_state = child->current_state;
470 if (power_state == PCI_D1 || power_state == PCI_D2 ||
471 power_state == PCI_D3hot || power_state == PCI_D3cold)
Shaohua Li7d715a62008-02-25 09:46:41 +0800472 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900473 state = __pcie_aspm_check_state_one(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800474 }
475 return state;
476}
477
478static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
479{
480 u16 reg16;
481 int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
482
483 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
484 reg16 &= ~0x3;
485 reg16 |= state;
486 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
487}
488
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900489static void __pcie_aspm_config_link(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800490{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900491 struct pci_dev *child, *parent = link->pdev;
492 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800493
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900494 state &= ~link->aspm_disable;
495 /* Nothing to do if the link is already in the requested state */
496 if (link->aspm_enabled == state)
497 return;
Shaohua Li7d715a62008-02-25 09:46:41 +0800498 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900499 * Spec 2.0 suggests all functions should be configured the
500 * same setting for ASPM. Enabling ASPM L1 should be done in
501 * upstream component first and then downstream, and vice
502 * versa for disabling ASPM L1. Spec doesn't mention L0S.
Shaohua Li7d715a62008-02-25 09:46:41 +0800503 */
504 if (state & PCIE_LINK_STATE_L1)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900505 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800506
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900507 list_for_each_entry(child, &linkbus->devices, bus_list)
508 __pcie_aspm_config_one_dev(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800509
510 if (!(state & PCIE_LINK_STATE_L1))
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900511 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800512
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900513 link->aspm_enabled = state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800514}
515
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900516/* Check the whole hierarchy, and configure each link in the hierarchy */
517static void __pcie_aspm_configure_link_state(struct pcie_link_state *link,
518 u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800519{
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900520 struct pcie_link_state *leaf, *root = link->root;
Shaohua Li7d715a62008-02-25 09:46:41 +0800521
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900522 state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800523
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900524 /* Check all links who have specific root port link */
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900525 list_for_each_entry(leaf, &link_list, sibling) {
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900526 if (!list_empty(&leaf->children) || (leaf->root != root))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800527 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900528 state = pcie_aspm_check_state(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800529 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900530 /* Check root port link too in case it hasn't children */
531 state = pcie_aspm_check_state(root, state);
532 if (link->aspm_enabled == state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800533 return;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800534 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900535 * We must change the hierarchy. See comments in
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800536 * __pcie_aspm_config_link for the order
537 **/
538 if (state & PCIE_LINK_STATE_L1) {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900539 list_for_each_entry(leaf, &link_list, sibling) {
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900540 if (leaf->root == root)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900541 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800542 }
543 } else {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900544 list_for_each_entry_reverse(leaf, &link_list, sibling) {
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900545 if (leaf->root == root)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900546 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800547 }
548 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800549}
550
551/*
552 * pcie_aspm_configure_link_state: enable/disable PCI express link state
553 * @pdev: the root port or switch downstream port
554 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900555static void pcie_aspm_configure_link_state(struct pcie_link_state *link,
556 u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800557{
558 down_read(&pci_bus_sem);
559 mutex_lock(&aspm_lock);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900560 __pcie_aspm_configure_link_state(link, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800561 mutex_unlock(&aspm_lock);
562 up_read(&pci_bus_sem);
563}
564
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900565static void free_link_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800566{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900567 link->pdev->link_state = NULL;
568 kfree(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800569}
570
Shaohua Liddc97532008-05-21 16:58:40 +0800571static int pcie_aspm_sanity_check(struct pci_dev *pdev)
572{
Kenji Kaneshige36475842009-05-13 12:23:09 +0900573 struct pci_dev *child;
574 int pos;
Shaohua Li149e1632008-07-23 10:32:31 +0800575 u32 reg32;
Shaohua Liddc97532008-05-21 16:58:40 +0800576 /*
Kenji Kaneshige36475842009-05-13 12:23:09 +0900577 * Some functions in a slot might not all be PCIE functions,
578 * very strange. Disable ASPM for the whole slot
Shaohua Liddc97532008-05-21 16:58:40 +0800579 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900580 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
581 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
582 if (!pos)
Shaohua Liddc97532008-05-21 16:58:40 +0800583 return -EINVAL;
Shaohua Li149e1632008-07-23 10:32:31 +0800584 /*
585 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
586 * RBER bit to determine if a function is 1.1 version device
587 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900588 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Sitsofe Wheelere1f4f592008-09-16 14:27:13 +0100589 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
Kenji Kaneshige36475842009-05-13 12:23:09 +0900590 dev_printk(KERN_INFO, &child->dev, "disabling ASPM"
Vincent Legollf393d9b2008-10-12 12:26:12 +0200591 " on pre-1.1 PCIe device. You can enable it"
592 " with 'pcie_aspm=force'\n");
Shaohua Li149e1632008-07-23 10:32:31 +0800593 return -EINVAL;
594 }
Shaohua Liddc97532008-05-21 16:58:40 +0800595 }
596 return 0;
597}
598
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900599static struct pcie_link_state *pcie_aspm_setup_link_state(struct pci_dev *pdev)
600{
601 struct pcie_link_state *link;
602 int blacklist = !!pcie_aspm_sanity_check(pdev);
603
604 link = kzalloc(sizeof(*link), GFP_KERNEL);
605 if (!link)
606 return NULL;
607 INIT_LIST_HEAD(&link->sibling);
608 INIT_LIST_HEAD(&link->children);
609 INIT_LIST_HEAD(&link->link);
610 link->pdev = pdev;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900611 if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
612 struct pcie_link_state *parent;
613 parent = pdev->bus->parent->self->link_state;
614 if (!parent) {
615 kfree(link);
616 return NULL;
617 }
618 link->parent = parent;
619 list_add(&link->link, &parent->children);
620 }
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900621 /* Setup a pointer to the root port link */
622 if (!link->parent)
623 link->root = link;
624 else
625 link->root = link->parent->root;
626
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900627 list_add(&link->sibling, &link_list);
628
629 pdev->link_state = link;
630
631 /* Check ASPM capability */
632 pcie_aspm_cap_init(link, blacklist);
633
634 /* Check Clock PM capability */
635 pcie_clkpm_cap_init(link, blacklist);
636
637 return link;
638}
639
Shaohua Li7d715a62008-02-25 09:46:41 +0800640/*
641 * pcie_aspm_init_link_state: Initiate PCI express link state.
642 * It is called after the pcie and its children devices are scaned.
643 * @pdev: the root port or switch downstream port
644 */
645void pcie_aspm_init_link_state(struct pci_dev *pdev)
646{
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900647 u32 state;
648 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800649
650 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
651 return;
652 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900653 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
Shaohua Li7d715a62008-02-25 09:46:41 +0800654 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900655
Shaohua Li8e822df2009-06-08 09:27:25 +0800656 /* VIA has a strange chipset, root port is under a bridge */
657 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900658 pdev->bus->self)
Shaohua Li8e822df2009-06-08 09:27:25 +0800659 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900660
Shaohua Li7d715a62008-02-25 09:46:41 +0800661 down_read(&pci_bus_sem);
662 if (list_empty(&pdev->subordinate->devices))
663 goto out;
664
Shaohua Li7d715a62008-02-25 09:46:41 +0800665 mutex_lock(&aspm_lock);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900666 link = pcie_aspm_setup_link_state(pdev);
667 if (!link)
668 goto unlock;
669 /*
670 * Setup initial ASPM state
671 *
672 * If link has switch, delay the link config. The leaf link
673 * initialization will config the whole hierarchy. But we must
674 * make sure BIOS doesn't set unsupported link state.
675 */
Kenji Kaneshigeefdf82882009-05-13 12:22:26 +0900676 if (pcie_aspm_downstream_has_switch(link)) {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900677 state = pcie_aspm_check_state(link, link->aspm_default);
678 __pcie_aspm_config_link(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800679 } else {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900680 state = policy_to_aspm_state(link);
681 __pcie_aspm_configure_link_state(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800682 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800683
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900684 /* Setup initial Clock PM state */
685 state = (link->clkpm_capable) ? policy_to_clkpm_state(link) : 0;
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900686 pcie_set_clkpm(link, state);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900687unlock:
Shaohua Li7d715a62008-02-25 09:46:41 +0800688 mutex_unlock(&aspm_lock);
689out:
690 up_read(&pci_bus_sem);
691}
692
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900693/* Recheck latencies and update aspm_capable for links under the root */
694static void pcie_update_aspm_capable(struct pcie_link_state *root)
695{
696 struct pcie_link_state *link;
697 BUG_ON(root->parent);
698 list_for_each_entry(link, &link_list, sibling) {
699 if (link->root != root)
700 continue;
701 link->aspm_capable = link->aspm_support;
702 }
703 list_for_each_entry(link, &link_list, sibling) {
704 struct pci_dev *child;
705 struct pci_bus *linkbus = link->pdev->subordinate;
706 if (link->root != root)
707 continue;
708 list_for_each_entry(child, &linkbus->devices, bus_list) {
709 if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT) &&
710 (child->pcie_type != PCI_EXP_TYPE_LEG_END))
711 continue;
712 pcie_aspm_check_latency(child);
713 }
714 }
715}
716
Shaohua Li7d715a62008-02-25 09:46:41 +0800717/* @pdev: the endpoint device */
718void pcie_aspm_exit_link_state(struct pci_dev *pdev)
719{
720 struct pci_dev *parent = pdev->bus->self;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900721 struct pcie_link_state *link, *root;
Shaohua Li7d715a62008-02-25 09:46:41 +0800722
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900723 if (aspm_disabled || !pdev->is_pcie || !parent || !parent->link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800724 return;
725 if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900726 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
Shaohua Li7d715a62008-02-25 09:46:41 +0800727 return;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900728
Shaohua Li7d715a62008-02-25 09:46:41 +0800729 down_read(&pci_bus_sem);
730 mutex_lock(&aspm_lock);
Shaohua Li7d715a62008-02-25 09:46:41 +0800731 /*
732 * All PCIe functions are in one slot, remove one function will remove
Alex Chiang3419c752009-01-28 14:59:18 -0700733 * the whole slot, so just wait until we are the last function left.
Shaohua Li7d715a62008-02-25 09:46:41 +0800734 */
Alex Chiang3419c752009-01-28 14:59:18 -0700735 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
Shaohua Li7d715a62008-02-25 09:46:41 +0800736 goto out;
737
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900738 link = parent->link_state;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900739 root = link->root;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900740
Shaohua Li7d715a62008-02-25 09:46:41 +0800741 /* All functions are removed, so just disable ASPM for the link */
742 __pcie_aspm_config_one_dev(parent, 0);
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900743 list_del(&link->sibling);
744 list_del(&link->link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800745 /* Clock PM is for endpoint device */
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900746 free_link_state(link);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900747
748 /* Recheck latencies and configure upstream links */
749 pcie_update_aspm_capable(root);
Shaohua Li7d715a62008-02-25 09:46:41 +0800750out:
751 mutex_unlock(&aspm_lock);
752 up_read(&pci_bus_sem);
753}
754
755/* @pdev: the root port or switch downstream port */
756void pcie_aspm_pm_state_change(struct pci_dev *pdev)
757{
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900758 struct pcie_link_state *link = pdev->link_state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800759
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900760 if (aspm_disabled || !pdev->is_pcie || !link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800761 return;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900762 if ((pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT) &&
763 (pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM))
Shaohua Li7d715a62008-02-25 09:46:41 +0800764 return;
765 /*
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900766 * Devices changed PM state, we should recheck if latency
767 * meets all functions' requirement
Shaohua Li7d715a62008-02-25 09:46:41 +0800768 */
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900769 down_read(&pci_bus_sem);
770 mutex_lock(&aspm_lock);
771 pcie_update_aspm_capable(link->root);
772 __pcie_aspm_configure_link_state(link, link->aspm_enabled);
773 mutex_unlock(&aspm_lock);
774 up_read(&pci_bus_sem);
Shaohua Li7d715a62008-02-25 09:46:41 +0800775}
776
777/*
778 * pci_disable_link_state - disable pci device's link state, so the link will
779 * never enter specific states
780 */
781void pci_disable_link_state(struct pci_dev *pdev, int state)
782{
783 struct pci_dev *parent = pdev->bus->self;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900784 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800785
786 if (aspm_disabled || !pdev->is_pcie)
787 return;
788 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
789 pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
790 parent = pdev;
791 if (!parent || !parent->link_state)
792 return;
793
794 down_read(&pci_bus_sem);
795 mutex_lock(&aspm_lock);
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900796 link = parent->link_state;
797 link->aspm_disable |= state;
798 __pcie_aspm_configure_link_state(link, link->aspm_enabled);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900799 if (state & PCIE_LINK_STATE_CLKPM) {
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900800 link->clkpm_capable = 0;
801 pcie_set_clkpm(link, 0);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900802 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800803 mutex_unlock(&aspm_lock);
804 up_read(&pci_bus_sem);
805}
806EXPORT_SYMBOL(pci_disable_link_state);
807
808static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
809{
810 int i;
Shaohua Li7d715a62008-02-25 09:46:41 +0800811 struct pcie_link_state *link_state;
812
813 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
814 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
815 break;
816 if (i >= ARRAY_SIZE(policy_str))
817 return -EINVAL;
818 if (i == aspm_policy)
819 return 0;
820
821 down_read(&pci_bus_sem);
822 mutex_lock(&aspm_lock);
823 aspm_policy = i;
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900824 list_for_each_entry(link_state, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900825 __pcie_aspm_configure_link_state(link_state,
826 policy_to_aspm_state(link_state));
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900827 pcie_set_clkpm(link_state, policy_to_clkpm_state(link_state));
Shaohua Li7d715a62008-02-25 09:46:41 +0800828 }
829 mutex_unlock(&aspm_lock);
830 up_read(&pci_bus_sem);
831 return 0;
832}
833
834static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
835{
836 int i, cnt = 0;
837 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
838 if (i == aspm_policy)
839 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
840 else
841 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
842 return cnt;
843}
844
845module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
846 NULL, 0644);
847
848#ifdef CONFIG_PCIEASPM_DEBUG
849static ssize_t link_state_show(struct device *dev,
850 struct device_attribute *attr,
851 char *buf)
852{
853 struct pci_dev *pci_device = to_pci_dev(dev);
854 struct pcie_link_state *link_state = pci_device->link_state;
855
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900856 return sprintf(buf, "%d\n", link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800857}
858
859static ssize_t link_state_store(struct device *dev,
860 struct device_attribute *attr,
861 const char *buf,
862 size_t n)
863{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900864 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800865 int state;
866
867 if (n < 1)
868 return -EINVAL;
869 state = buf[0]-'0';
870 if (state >= 0 && state <= 3) {
871 /* setup link aspm state */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900872 pcie_aspm_configure_link_state(pdev->link_state, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800873 return n;
874 }
875
876 return -EINVAL;
877}
878
879static ssize_t clk_ctl_show(struct device *dev,
880 struct device_attribute *attr,
881 char *buf)
882{
883 struct pci_dev *pci_device = to_pci_dev(dev);
884 struct pcie_link_state *link_state = pci_device->link_state;
885
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900886 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800887}
888
889static ssize_t clk_ctl_store(struct device *dev,
890 struct device_attribute *attr,
891 const char *buf,
892 size_t n)
893{
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900894 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800895 int state;
896
897 if (n < 1)
898 return -EINVAL;
899 state = buf[0]-'0';
900
901 down_read(&pci_bus_sem);
902 mutex_lock(&aspm_lock);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900903 pcie_set_clkpm_nocheck(pdev->link_state, !!state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800904 mutex_unlock(&aspm_lock);
905 up_read(&pci_bus_sem);
906
907 return n;
908}
909
910static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
911static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
912
913static char power_group[] = "power";
914void pcie_aspm_create_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_add_file_to_group(&pdev->dev.kobj,
924 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900925 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800926 sysfs_add_file_to_group(&pdev->dev.kobj,
927 &dev_attr_clk_ctl.attr, power_group);
928}
929
930void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
931{
932 struct pcie_link_state *link_state = pdev->link_state;
933
934 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
935 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
936 return;
937
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900938 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800939 sysfs_remove_file_from_group(&pdev->dev.kobj,
940 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900941 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800942 sysfs_remove_file_from_group(&pdev->dev.kobj,
943 &dev_attr_clk_ctl.attr, power_group);
944}
945#endif
946
947static int __init pcie_aspm_disable(char *str)
948{
Shaohua Lid6d38572008-07-23 10:32:42 +0800949 if (!strcmp(str, "off")) {
950 aspm_disabled = 1;
951 printk(KERN_INFO "PCIe ASPM is disabled\n");
952 } else if (!strcmp(str, "force")) {
953 aspm_force = 1;
954 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
955 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800956 return 1;
957}
958
Shaohua Lid6d38572008-07-23 10:32:42 +0800959__setup("pcie_aspm=", pcie_aspm_disable);
Shaohua Li7d715a62008-02-25 09:46:41 +0800960
Shaohua Li5fde2442008-07-23 10:32:24 +0800961void pcie_no_aspm(void)
962{
Shaohua Lid6d38572008-07-23 10:32:42 +0800963 if (!aspm_force)
964 aspm_disabled = 1;
Shaohua Li5fde2442008-07-23 10:32:24 +0800965}
966
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700967/**
968 * pcie_aspm_enabled - is PCIe ASPM enabled?
969 *
970 * Returns true if ASPM has not been disabled by the command-line option
971 * pcie_aspm=off.
972 **/
973int pcie_aspm_enabled(void)
Shaohua Li7d715a62008-02-25 09:46:41 +0800974{
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700975 return !aspm_disabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800976}
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700977EXPORT_SYMBOL(pcie_aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800978