blob: 04b6a30985059b7a809b64acf81e52b4f0317f04 [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 */
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 */
Shaohua Li7d715a62008-02-25 09:46:41 +080040
41 /* ASPM state */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090042 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
Kenji Kaneshige4d246e42009-05-13 12:15:38 +090046 /* 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
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090051 /* Latencies */
52 struct aspm_latency latency; /* Exit latency */
Shaohua Li7d715a62008-02-25 09:46:41 +080053 /*
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090054 * Endpoint acceptable latencies. A pcie downstream port only
55 * has one slot under it, so at most there are 8 functions.
Shaohua Li7d715a62008-02-25 09:46:41 +080056 */
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090057 struct aspm_latency acceptable[8];
Shaohua Li7d715a62008-02-25 09:46:41 +080058};
59
Shaohua Lid6d38572008-07-23 10:32:42 +080060static int aspm_disabled, aspm_force;
Shaohua Li7d715a62008-02-25 09:46:41 +080061static 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
Andrew Patterson987a4c72009-01-05 16:21:04 -070074#define LINK_RETRAIN_TIMEOUT HZ
75
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090076static int policy_to_aspm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080077{
Shaohua Li7d715a62008-02-25 09:46:41 +080078 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 */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090084 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Shaohua Li7d715a62008-02-25 09:46:41 +080085 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090086 return link->aspm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +080087 }
88 return 0;
89}
90
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090091static int policy_to_clkpm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080092{
Shaohua Li7d715a62008-02-25 09:46:41 +080093 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:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900101 return link->clkpm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +0800102 }
103 return 0;
104}
105
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900106static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800107{
Shaohua Li7d715a62008-02-25 09:46:41 +0800108 int pos;
109 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900110 struct pci_dev *child;
111 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800112
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900113 list_for_each_entry(child, &linkbus->devices, bus_list) {
114 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800115 if (!pos)
116 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900117 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800118 if (enable)
119 reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
120 else
121 reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900122 pci_write_config_word(child, pos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800123 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900124 link->clkpm_enabled = !!enable;
Shaohua Li7d715a62008-02-25 09:46:41 +0800125}
126
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900127static 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
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900138static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800139{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900140 int pos, capable = 1, enabled = 1;
Shaohua Li7d715a62008-02-25 09:46:41 +0800141 u32 reg32;
142 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900143 struct pci_dev *child;
144 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800145
146 /* All functions should have the same cap and state, take the worst */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900147 list_for_each_entry(child, &linkbus->devices, bus_list) {
148 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800149 if (!pos)
150 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900151 pci_read_config_dword(child, pos + PCI_EXP_LNKCAP, &reg32);
Shaohua Li7d715a62008-02-25 09:46:41 +0800152 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
153 capable = 0;
154 enabled = 0;
155 break;
156 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900157 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800158 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
159 enabled = 0;
160 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900161 link->clkpm_enabled = enabled;
162 link->clkpm_default = enabled;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900163 link->clkpm_capable = (blacklist) ? 0 : capable;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800164}
165
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900166static bool pcie_aspm_downstream_has_switch(struct pcie_link_state *link)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800167{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900168 struct pci_dev *child;
169 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800170
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900171 list_for_each_entry(child, &linkbus->devices, bus_list) {
172 if (child->pcie_type == PCI_EXP_TYPE_UPSTREAM)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800173 return true;
174 }
175 return false;
Shaohua Li7d715a62008-02-25 09:46:41 +0800176}
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 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900183static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800184{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900185 int ppos, cpos, same_clock = 1;
186 u16 reg16, parent_reg, child_reg[8];
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100187 unsigned long start_jiffies;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900188 struct pci_dev *child, *parent = link->pdev;
189 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800190 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900191 * All functions of a slot should have the same Slot Clock
Shaohua Li7d715a62008-02-25 09:46:41 +0800192 * Configuration, so just check one function
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900193 */
194 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
195 BUG_ON(!child->is_pcie);
Shaohua Li7d715a62008-02-25 09:46:41 +0800196
197 /* Check downstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900198 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
199 pci_read_config_word(child, cpos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800200 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
201 same_clock = 0;
202
203 /* Check upstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900204 ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
205 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800206 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
207 same_clock = 0;
208
209 /* Configure downstream component, all functions */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900210 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;
Shaohua Li7d715a62008-02-25 09:46:41 +0800214 if (same_clock)
215 reg16 |= PCI_EXP_LNKCTL_CCC;
216 else
217 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900218 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800219 }
220
221 /* Configure upstream component */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900222 pci_read_config_word(parent, ppos + PCI_EXP_LNKCTL, &reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100223 parent_reg = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800224 if (same_clock)
225 reg16 |= PCI_EXP_LNKCTL_CCC;
226 else
227 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900228 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800229
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900230 /* Retrain link */
Shaohua Li7d715a62008-02-25 09:46:41 +0800231 reg16 |= PCI_EXP_LNKCTL_RL;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900232 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800233
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900234 /* Wait for link training end. Break out after waiting for timeout */
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100235 start_jiffies = jiffies;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700236 for (;;) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900237 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800238 if (!(reg16 & PCI_EXP_LNKSTA_LT))
239 break;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700240 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
241 break;
242 msleep(1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800243 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900244 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)]);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100254 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900255 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, parent_reg);
Shaohua Li7d715a62008-02-25 09:46:41 +0800256}
257
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900258/* Convert L0s latency encoding to ns */
259static u32 calc_l0s_latency(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800260{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900261 if (encoding == 0x7)
262 return (5 * 1000); /* > 4us */
263 return (64 << encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800264}
265
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900266/* Convert L0s acceptable latency encoding to ns */
267static u32 calc_l0s_acceptable(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800268{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900269 if (encoding == 0x7)
270 return -1U;
271 return (64 << encoding);
272}
Shaohua Li7d715a62008-02-25 09:46:41 +0800273
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900274/* Convert L1 latency encoding to ns */
275static u32 calc_l1_latency(u32 encoding)
276{
277 if (encoding == 0x7)
278 return (65 * 1000); /* > 64us */
279 return (1000 << encoding);
280}
281
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);
Shaohua Li7d715a62008-02-25 09:46:41 +0800288}
289
290static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900291 u32 *l0s, u32 *l1, u32 *enabled)
Shaohua Li7d715a62008-02-25 09:46:41 +0800292{
293 int pos;
294 u16 reg16;
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900295 u32 reg32, encoding;
Shaohua Li7d715a62008-02-25 09:46:41 +0800296
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900297 *l0s = *l1 = *enabled = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800298 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 &&
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900302 *state != (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L0S))
Shaohua Li7d715a62008-02-25 09:46:41 +0800303 *state = 0;
304 if (*state == 0)
305 return;
306
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900307 encoding = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
308 *l0s = calc_l0s_latency(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800309 if (*state & PCIE_LINK_STATE_L1) {
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900310 encoding = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
311 *l1 = calc_l1_latency(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800312 }
313 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900314 *enabled = reg16 & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800315}
316
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900317static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800318{
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900319 u32 support, l0s, l1, enabled;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900320 struct pci_dev *child, *parent = link->pdev;
321 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800322
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900323 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
Shaohua Li7d715a62008-02-25 09:46:41 +0800334 /* upstream component states */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900335 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;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900340
Shaohua Li7d715a62008-02-25 09:46:41 +0800341 /* downstream component states, all functions have the same setting */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900342 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);
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900347
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900348 if (!link->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800349 return;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900350
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900351 link->aspm_enabled &= link->aspm_support;
352 link->aspm_default = link->aspm_enabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800353
354 /* ENDPOINT states*/
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900355 list_for_each_entry(child, &linkbus->devices, bus_list) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800356 int pos;
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900357 u32 reg32, encoding;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900358 struct aspm_latency *acceptable =
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900359 &link->acceptable[PCI_FUNC(child->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800360
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900361 if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
362 child->pcie_type != PCI_EXP_TYPE_LEG_END)
Shaohua Li7d715a62008-02-25 09:46:41 +0800363 continue;
364
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900365 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
366 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900367 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
368 acceptable->l0s = calc_l0s_acceptable(encoding);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900369 if (link->aspm_support & PCIE_LINK_STATE_L1) {
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900370 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
371 acceptable->l1 = calc_l1_acceptable(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800372 }
373 }
374}
375
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900376/**
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)
Shaohua Li7d715a62008-02-25 09:46:41 +0800387{
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900388 u32 l1_switch_latency = 0;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900389 struct aspm_latency *acceptable;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900390 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800391
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900392 link = endpoint->bus->self->link_state;
393 state &= link->aspm_support;
394 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800395
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900396 while (link && state) {
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900397 if ((state & PCIE_LINK_STATE_L0S) &&
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900398 (link->latency.l0s > acceptable->l0s))
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900399 state &= ~PCIE_LINK_STATE_L0S;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900400 if ((state & PCIE_LINK_STATE_L1) &&
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900401 (link->latency.l1 + l1_switch_latency > acceptable->l1))
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900402 state &= ~PCIE_LINK_STATE_L1;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900403 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;
Shaohua Li7d715a62008-02-25 09:46:41 +0800409 }
410 return state;
411}
412
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900413static u32 pcie_aspm_check_state(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800414{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900415 pci_power_t power_state;
416 struct pci_dev *child;
417 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800418
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800419 /* If no child, ignore the link */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900420 if (list_empty(&linkbus->devices))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800421 return state;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900422
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))
Shaohua Li7d715a62008-02-25 09:46:41 +0800433 continue;
434 /* Device not in D0 doesn't need check latency */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900435 power_state = child->current_state;
436 if (power_state == PCI_D1 || power_state == PCI_D2 ||
437 power_state == PCI_D3hot || power_state == PCI_D3cold)
Shaohua Li7d715a62008-02-25 09:46:41 +0800438 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900439 state = __pcie_aspm_check_state_one(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800440 }
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
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900455static void __pcie_aspm_config_link(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800456{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900457 struct pci_dev *child, *parent = link->pdev;
458 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800459
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800460 /* If no child, disable the link */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900461 if (list_empty(&linkbus->devices))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800462 state = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800463 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900464 * If the downstream component has pci bridge function, don't
465 * do ASPM now.
Shaohua Li7d715a62008-02-25 09:46:41 +0800466 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900467 list_for_each_entry(child, &linkbus->devices, bus_list) {
468 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
469 return;
Shaohua Li7d715a62008-02-25 09:46:41 +0800470 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800471 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900472 * 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.
Shaohua Li7d715a62008-02-25 09:46:41 +0800476 */
477 if (state & PCIE_LINK_STATE_L1)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900478 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800479
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900480 list_for_each_entry(child, &linkbus->devices, bus_list)
481 __pcie_aspm_config_one_dev(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800482
483 if (!(state & PCIE_LINK_STATE_L1))
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900484 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800485
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900486 link->aspm_enabled = state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800487}
488
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800489static 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
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900497/* 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)
Shaohua Li7d715a62008-02-25 09:46:41 +0800500{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900501 struct pcie_link_state *leaf, *root = get_root_port_link(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800502
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900503 state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800504
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900505 /* Check all links who have specific root port link */
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900506 list_for_each_entry(leaf, &link_list, sibling) {
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800507 if (!list_empty(&leaf->children) ||
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900508 get_root_port_link(leaf) != root)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800509 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900510 state = pcie_aspm_check_state(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800511 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900512 /* 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)
Shaohua Li7d715a62008-02-25 09:46:41 +0800515 return;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800516 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900517 * We must change the hierarchy. See comments in
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800518 * __pcie_aspm_config_link for the order
519 **/
520 if (state & PCIE_LINK_STATE_L1) {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900521 list_for_each_entry(leaf, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900522 if (get_root_port_link(leaf) == root)
523 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800524 }
525 } else {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900526 list_for_each_entry_reverse(leaf, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900527 if (get_root_port_link(leaf) == root)
528 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800529 }
530 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800531}
532
533/*
534 * pcie_aspm_configure_link_state: enable/disable PCI express link state
535 * @pdev: the root port or switch downstream port
536 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900537static void pcie_aspm_configure_link_state(struct pcie_link_state *link,
538 u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800539{
540 down_read(&pci_bus_sem);
541 mutex_lock(&aspm_lock);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900542 __pcie_aspm_configure_link_state(link, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800543 mutex_unlock(&aspm_lock);
544 up_read(&pci_bus_sem);
545}
546
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900547static void free_link_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800548{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900549 link->pdev->link_state = NULL;
550 kfree(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800551}
552
Shaohua Liddc97532008-05-21 16:58:40 +0800553static int pcie_aspm_sanity_check(struct pci_dev *pdev)
554{
Kenji Kaneshige36475842009-05-13 12:23:09 +0900555 struct pci_dev *child;
556 int pos;
Shaohua Li149e1632008-07-23 10:32:31 +0800557 u32 reg32;
Shaohua Liddc97532008-05-21 16:58:40 +0800558 /*
Kenji Kaneshige36475842009-05-13 12:23:09 +0900559 * Some functions in a slot might not all be PCIE functions,
560 * very strange. Disable ASPM for the whole slot
Shaohua Liddc97532008-05-21 16:58:40 +0800561 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900562 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
563 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
564 if (!pos)
Shaohua Liddc97532008-05-21 16:58:40 +0800565 return -EINVAL;
Shaohua Li149e1632008-07-23 10:32:31 +0800566 /*
567 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
568 * RBER bit to determine if a function is 1.1 version device
569 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900570 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Sitsofe Wheelere1f4f592008-09-16 14:27:13 +0100571 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
Kenji Kaneshige36475842009-05-13 12:23:09 +0900572 dev_printk(KERN_INFO, &child->dev, "disabling ASPM"
Vincent Legollf393d9b2008-10-12 12:26:12 +0200573 " on pre-1.1 PCIe device. You can enable it"
574 " with 'pcie_aspm=force'\n");
Shaohua Li149e1632008-07-23 10:32:31 +0800575 return -EINVAL;
576 }
Shaohua Liddc97532008-05-21 16:58:40 +0800577 }
578 return 0;
579}
580
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900581static struct pcie_link_state *pcie_aspm_setup_link_state(struct pci_dev *pdev)
582{
583 struct pcie_link_state *link;
584 int blacklist = !!pcie_aspm_sanity_check(pdev);
585
586 link = kzalloc(sizeof(*link), GFP_KERNEL);
587 if (!link)
588 return NULL;
589 INIT_LIST_HEAD(&link->sibling);
590 INIT_LIST_HEAD(&link->children);
591 INIT_LIST_HEAD(&link->link);
592 link->pdev = pdev;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900593 if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
594 struct pcie_link_state *parent;
595 parent = pdev->bus->parent->self->link_state;
596 if (!parent) {
597 kfree(link);
598 return NULL;
599 }
600 link->parent = parent;
601 list_add(&link->link, &parent->children);
602 }
603 list_add(&link->sibling, &link_list);
604
605 pdev->link_state = link;
606
607 /* Check ASPM capability */
608 pcie_aspm_cap_init(link, blacklist);
609
610 /* Check Clock PM capability */
611 pcie_clkpm_cap_init(link, blacklist);
612
613 return link;
614}
615
Shaohua Li7d715a62008-02-25 09:46:41 +0800616/*
617 * pcie_aspm_init_link_state: Initiate PCI express link state.
618 * It is called after the pcie and its children devices are scaned.
619 * @pdev: the root port or switch downstream port
620 */
621void pcie_aspm_init_link_state(struct pci_dev *pdev)
622{
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900623 u32 state;
624 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800625
626 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
627 return;
628 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900629 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
Shaohua Li7d715a62008-02-25 09:46:41 +0800630 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900631
Shaohua Li8e822df2009-06-08 09:27:25 +0800632 /* VIA has a strange chipset, root port is under a bridge */
633 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900634 pdev->bus->self)
Shaohua Li8e822df2009-06-08 09:27:25 +0800635 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900636
Shaohua Li7d715a62008-02-25 09:46:41 +0800637 down_read(&pci_bus_sem);
638 if (list_empty(&pdev->subordinate->devices))
639 goto out;
640
Shaohua Li7d715a62008-02-25 09:46:41 +0800641 mutex_lock(&aspm_lock);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900642 link = pcie_aspm_setup_link_state(pdev);
643 if (!link)
644 goto unlock;
645 /*
646 * Setup initial ASPM state
647 *
648 * If link has switch, delay the link config. The leaf link
649 * initialization will config the whole hierarchy. But we must
650 * make sure BIOS doesn't set unsupported link state.
651 */
Kenji Kaneshigeefdf8282009-05-13 12:22:26 +0900652 if (pcie_aspm_downstream_has_switch(link)) {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900653 state = pcie_aspm_check_state(link, link->aspm_default);
654 __pcie_aspm_config_link(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800655 } else {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900656 state = policy_to_aspm_state(link);
657 __pcie_aspm_configure_link_state(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800658 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800659
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900660 /* Setup initial Clock PM state */
661 state = (link->clkpm_capable) ? policy_to_clkpm_state(link) : 0;
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900662 pcie_set_clkpm(link, state);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900663unlock:
Shaohua Li7d715a62008-02-25 09:46:41 +0800664 mutex_unlock(&aspm_lock);
665out:
666 up_read(&pci_bus_sem);
667}
668
669/* @pdev: the endpoint device */
670void pcie_aspm_exit_link_state(struct pci_dev *pdev)
671{
672 struct pci_dev *parent = pdev->bus->self;
673 struct pcie_link_state *link_state = parent->link_state;
674
675 if (aspm_disabled || !pdev->is_pcie || !parent || !link_state)
676 return;
677 if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
678 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
679 return;
680 down_read(&pci_bus_sem);
681 mutex_lock(&aspm_lock);
682
683 /*
684 * All PCIe functions are in one slot, remove one function will remove
Alex Chiang3419c752009-01-28 14:59:18 -0700685 * the whole slot, so just wait until we are the last function left.
Shaohua Li7d715a62008-02-25 09:46:41 +0800686 */
Alex Chiang3419c752009-01-28 14:59:18 -0700687 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
Shaohua Li7d715a62008-02-25 09:46:41 +0800688 goto out;
689
690 /* All functions are removed, so just disable ASPM for the link */
691 __pcie_aspm_config_one_dev(parent, 0);
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900692 list_del(&link_state->sibling);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800693 list_del(&link_state->link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800694 /* Clock PM is for endpoint device */
695
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900696 free_link_state(link_state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800697out:
698 mutex_unlock(&aspm_lock);
699 up_read(&pci_bus_sem);
700}
701
702/* @pdev: the root port or switch downstream port */
703void pcie_aspm_pm_state_change(struct pci_dev *pdev)
704{
705 struct pcie_link_state *link_state = pdev->link_state;
706
707 if (aspm_disabled || !pdev->is_pcie || !pdev->link_state)
708 return;
709 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
710 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
711 return;
712 /*
713 * devices changed PM state, we should recheck if latency meets all
714 * functions' requirement
715 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900716 pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800717}
718
719/*
720 * pci_disable_link_state - disable pci device's link state, so the link will
721 * never enter specific states
722 */
723void pci_disable_link_state(struct pci_dev *pdev, int state)
724{
725 struct pci_dev *parent = pdev->bus->self;
726 struct pcie_link_state *link_state;
727
728 if (aspm_disabled || !pdev->is_pcie)
729 return;
730 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
731 pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
732 parent = pdev;
733 if (!parent || !parent->link_state)
734 return;
735
736 down_read(&pci_bus_sem);
737 mutex_lock(&aspm_lock);
738 link_state = parent->link_state;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900739 link_state->aspm_support &= ~state;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900740 __pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900741 if (state & PCIE_LINK_STATE_CLKPM) {
742 link_state->clkpm_capable = 0;
743 pcie_set_clkpm(link_state, 0);
744 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800745 mutex_unlock(&aspm_lock);
746 up_read(&pci_bus_sem);
747}
748EXPORT_SYMBOL(pci_disable_link_state);
749
750static int pcie_aspm_set_policy(const char *val, struct kernel_param *kp)
751{
752 int i;
Shaohua Li7d715a62008-02-25 09:46:41 +0800753 struct pcie_link_state *link_state;
754
755 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
756 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
757 break;
758 if (i >= ARRAY_SIZE(policy_str))
759 return -EINVAL;
760 if (i == aspm_policy)
761 return 0;
762
763 down_read(&pci_bus_sem);
764 mutex_lock(&aspm_lock);
765 aspm_policy = i;
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900766 list_for_each_entry(link_state, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900767 __pcie_aspm_configure_link_state(link_state,
768 policy_to_aspm_state(link_state));
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900769 pcie_set_clkpm(link_state, policy_to_clkpm_state(link_state));
Shaohua Li7d715a62008-02-25 09:46:41 +0800770 }
771 mutex_unlock(&aspm_lock);
772 up_read(&pci_bus_sem);
773 return 0;
774}
775
776static int pcie_aspm_get_policy(char *buffer, struct kernel_param *kp)
777{
778 int i, cnt = 0;
779 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
780 if (i == aspm_policy)
781 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
782 else
783 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
784 return cnt;
785}
786
787module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
788 NULL, 0644);
789
790#ifdef CONFIG_PCIEASPM_DEBUG
791static ssize_t link_state_show(struct device *dev,
792 struct device_attribute *attr,
793 char *buf)
794{
795 struct pci_dev *pci_device = to_pci_dev(dev);
796 struct pcie_link_state *link_state = pci_device->link_state;
797
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900798 return sprintf(buf, "%d\n", link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800799}
800
801static ssize_t link_state_store(struct device *dev,
802 struct device_attribute *attr,
803 const char *buf,
804 size_t n)
805{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900806 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800807 int state;
808
809 if (n < 1)
810 return -EINVAL;
811 state = buf[0]-'0';
812 if (state >= 0 && state <= 3) {
813 /* setup link aspm state */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900814 pcie_aspm_configure_link_state(pdev->link_state, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800815 return n;
816 }
817
818 return -EINVAL;
819}
820
821static ssize_t clk_ctl_show(struct device *dev,
822 struct device_attribute *attr,
823 char *buf)
824{
825 struct pci_dev *pci_device = to_pci_dev(dev);
826 struct pcie_link_state *link_state = pci_device->link_state;
827
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900828 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800829}
830
831static ssize_t clk_ctl_store(struct device *dev,
832 struct device_attribute *attr,
833 const char *buf,
834 size_t n)
835{
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900836 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800837 int state;
838
839 if (n < 1)
840 return -EINVAL;
841 state = buf[0]-'0';
842
843 down_read(&pci_bus_sem);
844 mutex_lock(&aspm_lock);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900845 pcie_set_clkpm_nocheck(pdev->link_state, !!state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800846 mutex_unlock(&aspm_lock);
847 up_read(&pci_bus_sem);
848
849 return n;
850}
851
852static DEVICE_ATTR(link_state, 0644, link_state_show, link_state_store);
853static DEVICE_ATTR(clk_ctl, 0644, clk_ctl_show, clk_ctl_store);
854
855static char power_group[] = "power";
856void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
857{
858 struct pcie_link_state *link_state = pdev->link_state;
859
860 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
861 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
862 return;
863
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900864 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800865 sysfs_add_file_to_group(&pdev->dev.kobj,
866 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900867 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800868 sysfs_add_file_to_group(&pdev->dev.kobj,
869 &dev_attr_clk_ctl.attr, power_group);
870}
871
872void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
873{
874 struct pcie_link_state *link_state = pdev->link_state;
875
876 if (!pdev->is_pcie || (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
877 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM) || !link_state)
878 return;
879
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900880 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800881 sysfs_remove_file_from_group(&pdev->dev.kobj,
882 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900883 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800884 sysfs_remove_file_from_group(&pdev->dev.kobj,
885 &dev_attr_clk_ctl.attr, power_group);
886}
887#endif
888
889static int __init pcie_aspm_disable(char *str)
890{
Shaohua Lid6d38572008-07-23 10:32:42 +0800891 if (!strcmp(str, "off")) {
892 aspm_disabled = 1;
893 printk(KERN_INFO "PCIe ASPM is disabled\n");
894 } else if (!strcmp(str, "force")) {
895 aspm_force = 1;
896 printk(KERN_INFO "PCIe ASPM is forcedly enabled\n");
897 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800898 return 1;
899}
900
Shaohua Lid6d38572008-07-23 10:32:42 +0800901__setup("pcie_aspm=", pcie_aspm_disable);
Shaohua Li7d715a62008-02-25 09:46:41 +0800902
Shaohua Li5fde2442008-07-23 10:32:24 +0800903void pcie_no_aspm(void)
904{
Shaohua Lid6d38572008-07-23 10:32:42 +0800905 if (!aspm_force)
906 aspm_disabled = 1;
Shaohua Li5fde2442008-07-23 10:32:24 +0800907}
908
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700909/**
910 * pcie_aspm_enabled - is PCIe ASPM enabled?
911 *
912 * Returns true if ASPM has not been disabled by the command-line option
913 * pcie_aspm=off.
914 **/
915int pcie_aspm_enabled(void)
Shaohua Li7d715a62008-02-25 09:46:41 +0800916{
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700917 return !aspm_disabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800918}
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700919EXPORT_SYMBOL(pcie_aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800920