blob: 8f7884a8bc2e97fcd0f3fddd04c4c43f2075bbe0 [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 */
45 u32 aspm_default:2; /* Default ASPM state by BIOS */
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +090046 u32 aspm_disable:2; /* Disabled ASPM state */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090047
Kenji Kaneshige4d246e42009-05-13 12:15:38 +090048 /* Clock PM state */
49 u32 clkpm_capable:1; /* Clock PM capable? */
50 u32 clkpm_enabled:1; /* Current Clock PM state */
51 u32 clkpm_default:1; /* Default Clock PM state by BIOS */
52
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090053 /* Latencies */
54 struct aspm_latency latency; /* Exit latency */
Shaohua Li7d715a62008-02-25 09:46:41 +080055 /*
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090056 * Endpoint acceptable latencies. A pcie downstream port only
57 * has one slot under it, so at most there are 8 functions.
Shaohua Li7d715a62008-02-25 09:46:41 +080058 */
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090059 struct aspm_latency acceptable[8];
Shaohua Li7d715a62008-02-25 09:46:41 +080060};
61
Shaohua Lid6d38572008-07-23 10:32:42 +080062static int aspm_disabled, aspm_force;
Shaohua Li7d715a62008-02-25 09:46:41 +080063static DEFINE_MUTEX(aspm_lock);
64static LIST_HEAD(link_list);
65
66#define POLICY_DEFAULT 0 /* BIOS default setting */
67#define POLICY_PERFORMANCE 1 /* high performance */
68#define POLICY_POWERSAVE 2 /* high power saving */
69static int aspm_policy;
70static const char *policy_str[] = {
71 [POLICY_DEFAULT] = "default",
72 [POLICY_PERFORMANCE] = "performance",
73 [POLICY_POWERSAVE] = "powersave"
74};
75
Andrew Patterson987a4c72009-01-05 16:21:04 -070076#define LINK_RETRAIN_TIMEOUT HZ
77
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090078static int policy_to_aspm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080079{
Shaohua Li7d715a62008-02-25 09:46:41 +080080 switch (aspm_policy) {
81 case POLICY_PERFORMANCE:
82 /* Disable ASPM and Clock PM */
83 return 0;
84 case POLICY_POWERSAVE:
85 /* Enable ASPM L0s/L1 */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090086 return PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Shaohua Li7d715a62008-02-25 09:46:41 +080087 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090088 return link->aspm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +080089 }
90 return 0;
91}
92
Kenji Kaneshige5aa63582009-05-13 12:17:44 +090093static int policy_to_clkpm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +080094{
Shaohua Li7d715a62008-02-25 09:46:41 +080095 switch (aspm_policy) {
96 case POLICY_PERFORMANCE:
97 /* Disable ASPM and Clock PM */
98 return 0;
99 case POLICY_POWERSAVE:
100 /* Disable Clock PM */
101 return 1;
102 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900103 return link->clkpm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +0800104 }
105 return 0;
106}
107
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900108static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800109{
Shaohua Li7d715a62008-02-25 09:46:41 +0800110 int pos;
111 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900112 struct pci_dev *child;
113 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800114
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900115 list_for_each_entry(child, &linkbus->devices, bus_list) {
116 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800117 if (!pos)
118 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900119 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800120 if (enable)
121 reg16 |= PCI_EXP_LNKCTL_CLKREQ_EN;
122 else
123 reg16 &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900124 pci_write_config_word(child, pos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800125 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900126 link->clkpm_enabled = !!enable;
Shaohua Li7d715a62008-02-25 09:46:41 +0800127}
128
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900129static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
130{
131 /* Don't enable Clock PM if the link is not Clock PM capable */
132 if (!link->clkpm_capable && enable)
133 return;
134 /* Need nothing if the specified equals to current state */
135 if (link->clkpm_enabled == enable)
136 return;
137 pcie_set_clkpm_nocheck(link, enable);
138}
139
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900140static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800141{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900142 int pos, capable = 1, enabled = 1;
Shaohua Li7d715a62008-02-25 09:46:41 +0800143 u32 reg32;
144 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900145 struct pci_dev *child;
146 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800147
148 /* All functions should have the same cap and state, take the worst */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900149 list_for_each_entry(child, &linkbus->devices, bus_list) {
150 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
Shaohua Li7d715a62008-02-25 09:46:41 +0800151 if (!pos)
152 return;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900153 pci_read_config_dword(child, pos + PCI_EXP_LNKCAP, &reg32);
Shaohua Li7d715a62008-02-25 09:46:41 +0800154 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
155 capable = 0;
156 enabled = 0;
157 break;
158 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900159 pci_read_config_word(child, pos + PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800160 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
161 enabled = 0;
162 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900163 link->clkpm_enabled = enabled;
164 link->clkpm_default = enabled;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900165 link->clkpm_capable = (blacklist) ? 0 : capable;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800166}
167
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900168static bool pcie_aspm_downstream_has_switch(struct pcie_link_state *link)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800169{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900170 struct pci_dev *child;
171 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800172
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900173 list_for_each_entry(child, &linkbus->devices, bus_list) {
174 if (child->pcie_type == PCI_EXP_TYPE_UPSTREAM)
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800175 return true;
176 }
177 return false;
Shaohua Li7d715a62008-02-25 09:46:41 +0800178}
179
180/*
181 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
182 * could use common clock. If they are, configure them to use the
183 * common clock. That will reduce the ASPM state exit latency.
184 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900185static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800186{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900187 int ppos, cpos, same_clock = 1;
188 u16 reg16, parent_reg, child_reg[8];
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100189 unsigned long start_jiffies;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900190 struct pci_dev *child, *parent = link->pdev;
191 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800192 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900193 * All functions of a slot should have the same Slot Clock
Shaohua Li7d715a62008-02-25 09:46:41 +0800194 * Configuration, so just check one function
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900195 */
196 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
197 BUG_ON(!child->is_pcie);
Shaohua Li7d715a62008-02-25 09:46:41 +0800198
199 /* Check downstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900200 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
201 pci_read_config_word(child, cpos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800202 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
203 same_clock = 0;
204
205 /* Check upstream component if bit Slot Clock Configuration is 1 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900206 ppos = pci_find_capability(parent, PCI_CAP_ID_EXP);
207 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800208 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
209 same_clock = 0;
210
211 /* Configure downstream component, all functions */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900212 list_for_each_entry(child, &linkbus->devices, bus_list) {
213 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
214 pci_read_config_word(child, cpos + PCI_EXP_LNKCTL, &reg16);
215 child_reg[PCI_FUNC(child->devfn)] = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800216 if (same_clock)
217 reg16 |= PCI_EXP_LNKCTL_CCC;
218 else
219 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900220 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800221 }
222
223 /* Configure upstream component */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900224 pci_read_config_word(parent, ppos + PCI_EXP_LNKCTL, &reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100225 parent_reg = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800226 if (same_clock)
227 reg16 |= PCI_EXP_LNKCTL_CCC;
228 else
229 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900230 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800231
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900232 /* Retrain link */
Shaohua Li7d715a62008-02-25 09:46:41 +0800233 reg16 |= PCI_EXP_LNKCTL_RL;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900234 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800235
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900236 /* Wait for link training end. Break out after waiting for timeout */
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100237 start_jiffies = jiffies;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700238 for (;;) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900239 pci_read_config_word(parent, ppos + PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800240 if (!(reg16 & PCI_EXP_LNKSTA_LT))
241 break;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700242 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
243 break;
244 msleep(1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800245 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900246 if (!(reg16 & PCI_EXP_LNKSTA_LT))
247 return;
248
249 /* Training failed. Restore common clock configurations */
250 dev_printk(KERN_ERR, &parent->dev,
251 "ASPM: Could not configure common clock\n");
252 list_for_each_entry(child, &linkbus->devices, bus_list) {
253 cpos = pci_find_capability(child, PCI_CAP_ID_EXP);
254 pci_write_config_word(child, cpos + PCI_EXP_LNKCTL,
255 child_reg[PCI_FUNC(child->devfn)]);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100256 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900257 pci_write_config_word(parent, ppos + PCI_EXP_LNKCTL, parent_reg);
Shaohua Li7d715a62008-02-25 09:46:41 +0800258}
259
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900260/* Convert L0s latency encoding to ns */
261static u32 calc_l0s_latency(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800262{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900263 if (encoding == 0x7)
264 return (5 * 1000); /* > 4us */
265 return (64 << encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800266}
267
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900268/* Convert L0s acceptable latency encoding to ns */
269static u32 calc_l0s_acceptable(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800270{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900271 if (encoding == 0x7)
272 return -1U;
273 return (64 << encoding);
274}
Shaohua Li7d715a62008-02-25 09:46:41 +0800275
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900276/* Convert L1 latency encoding to ns */
277static u32 calc_l1_latency(u32 encoding)
278{
279 if (encoding == 0x7)
280 return (65 * 1000); /* > 64us */
281 return (1000 << encoding);
282}
283
284/* Convert L1 acceptable latency encoding to ns */
285static u32 calc_l1_acceptable(u32 encoding)
286{
287 if (encoding == 0x7)
288 return -1U;
289 return (1000 << encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800290}
291
292static void pcie_aspm_get_cap_device(struct pci_dev *pdev, u32 *state,
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900293 u32 *l0s, u32 *l1, u32 *enabled)
Shaohua Li7d715a62008-02-25 09:46:41 +0800294{
295 int pos;
296 u16 reg16;
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900297 u32 reg32, encoding;
Shaohua Li7d715a62008-02-25 09:46:41 +0800298
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900299 *l0s = *l1 = *enabled = 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800300 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
301 pci_read_config_dword(pdev, pos + PCI_EXP_LNKCAP, &reg32);
302 *state = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
303 if (*state != PCIE_LINK_STATE_L0S &&
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900304 *state != (PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_L0S))
Shaohua Li7d715a62008-02-25 09:46:41 +0800305 *state = 0;
306 if (*state == 0)
307 return;
308
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900309 encoding = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
310 *l0s = calc_l0s_latency(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800311 if (*state & PCIE_LINK_STATE_L1) {
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900312 encoding = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
313 *l1 = calc_l1_latency(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800314 }
315 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
Kenji Kaneshige7ab70992009-05-13 12:20:48 +0900316 *enabled = reg16 & (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800317}
318
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900319static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800320{
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900321 u32 support, l0s, l1, enabled;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900322 struct pci_dev *child, *parent = link->pdev;
323 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800324
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900325 if (blacklist) {
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900326 /* Set enabled/disable so that we will disable ASPM later */
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900327 link->aspm_enabled = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900328 link->aspm_disable = PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900329 return;
330 }
331
332 /* Configure common clock before checking latencies */
333 pcie_aspm_configure_common_clock(link);
334
Shaohua Li7d715a62008-02-25 09:46:41 +0800335 /* upstream component states */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900336 pcie_aspm_get_cap_device(parent, &support, &l0s, &l1, &enabled);
337 link->aspm_support = support;
338 link->latency.l0s = l0s;
339 link->latency.l1 = l1;
340 link->aspm_enabled = enabled;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900341
Shaohua Li7d715a62008-02-25 09:46:41 +0800342 /* downstream component states, all functions have the same setting */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900343 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
344 pcie_aspm_get_cap_device(child, &support, &l0s, &l1, &enabled);
345 link->aspm_support &= support;
346 link->latency.l0s = max_t(u32, link->latency.l0s, l0s);
347 link->latency.l1 = max_t(u32, link->latency.l1, l1);
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900348
Kenji Kaneshigeb127bd52009-08-19 10:57:31 +0900349 /* Save default state */
350 link->aspm_default = link->aspm_enabled;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900351 /*
352 * If the downstream component has pci bridge function, don't
353 * do ASPM for now.
354 */
355 list_for_each_entry(child, &linkbus->devices, bus_list) {
356 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) {
357 link->aspm_disable =
358 PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1;
359 break;
360 }
361 }
Kenji Kaneshigeb127bd52009-08-19 10:57:31 +0900362
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900363 if (!link->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800364 return;
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900365
Shaohua Li7d715a62008-02-25 09:46:41 +0800366 /* ENDPOINT states*/
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900367 list_for_each_entry(child, &linkbus->devices, bus_list) {
Shaohua Li7d715a62008-02-25 09:46:41 +0800368 int pos;
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900369 u32 reg32, encoding;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900370 struct aspm_latency *acceptable =
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900371 &link->acceptable[PCI_FUNC(child->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800372
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900373 if (child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
374 child->pcie_type != PCI_EXP_TYPE_LEG_END)
Shaohua Li7d715a62008-02-25 09:46:41 +0800375 continue;
376
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900377 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
378 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900379 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
380 acceptable->l0s = calc_l0s_acceptable(encoding);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900381 if (link->aspm_support & PCIE_LINK_STATE_L1) {
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900382 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
383 acceptable->l1 = calc_l1_acceptable(encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800384 }
385 }
386}
387
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900388/**
389 * __pcie_aspm_check_state_one - check latency for endpoint device.
390 * @endpoint: pointer to the struct pci_dev of endpoint device
391 *
392 * TBD: The latency from the endpoint to root complex vary per switch's
393 * upstream link state above the device. Here we just do a simple check
394 * which assumes all links above the device can be in L1 state, that
395 * is we just consider the worst case. If switch's upstream link can't
396 * be put into L0S/L1, then our check is too strictly.
397 */
398static u32 __pcie_aspm_check_state_one(struct pci_dev *endpoint, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800399{
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900400 u32 l1_switch_latency = 0;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900401 struct aspm_latency *acceptable;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900402 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800403
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900404 link = endpoint->bus->self->link_state;
405 state &= link->aspm_support;
406 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800407
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900408 while (link && state) {
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900409 if ((state & PCIE_LINK_STATE_L0S) &&
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900410 (link->latency.l0s > acceptable->l0s))
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900411 state &= ~PCIE_LINK_STATE_L0S;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900412 if ((state & PCIE_LINK_STATE_L1) &&
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900413 (link->latency.l1 + l1_switch_latency > acceptable->l1))
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900414 state &= ~PCIE_LINK_STATE_L1;
Kenji Kaneshigef7ea3d72009-05-13 12:19:00 +0900415 link = link->parent;
416 /*
417 * Every switch on the path to root complex need 1
418 * more microsecond for L1. Spec doesn't mention L0s.
419 */
420 l1_switch_latency += 1000;
Shaohua Li7d715a62008-02-25 09:46:41 +0800421 }
422 return state;
423}
424
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900425static u32 pcie_aspm_check_state(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800426{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900427 pci_power_t power_state;
428 struct pci_dev *child;
429 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800430
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800431 /* If no child, ignore the link */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900432 if (list_empty(&linkbus->devices))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800433 return state;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900434
435 list_for_each_entry(child, &linkbus->devices, bus_list) {
436 /*
437 * If downstream component of a link is pci bridge, we
438 * disable ASPM for now for the link
439 */
440 if (child->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
441 return 0;
442
443 if ((child->pcie_type != PCI_EXP_TYPE_ENDPOINT &&
444 child->pcie_type != PCI_EXP_TYPE_LEG_END))
Shaohua Li7d715a62008-02-25 09:46:41 +0800445 continue;
446 /* Device not in D0 doesn't need check latency */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900447 power_state = child->current_state;
448 if (power_state == PCI_D1 || power_state == PCI_D2 ||
449 power_state == PCI_D3hot || power_state == PCI_D3cold)
Shaohua Li7d715a62008-02-25 09:46:41 +0800450 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900451 state = __pcie_aspm_check_state_one(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800452 }
453 return state;
454}
455
456static void __pcie_aspm_config_one_dev(struct pci_dev *pdev, unsigned int state)
457{
458 u16 reg16;
459 int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
460
461 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
462 reg16 &= ~0x3;
463 reg16 |= state;
464 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
465}
466
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900467static void __pcie_aspm_config_link(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800468{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900469 struct pci_dev *child, *parent = link->pdev;
470 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800471
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900472 state &= ~link->aspm_disable;
473 /* Nothing to do if the link is already in the requested state */
474 if (link->aspm_enabled == state)
475 return;
Shaohua Li7d715a62008-02-25 09:46:41 +0800476 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900477 * Spec 2.0 suggests all functions should be configured the
478 * same setting for ASPM. Enabling ASPM L1 should be done in
479 * upstream component first and then downstream, and vice
480 * versa for disabling ASPM L1. Spec doesn't mention L0S.
Shaohua Li7d715a62008-02-25 09:46:41 +0800481 */
482 if (state & PCIE_LINK_STATE_L1)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900483 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800484
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900485 list_for_each_entry(child, &linkbus->devices, bus_list)
486 __pcie_aspm_config_one_dev(child, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800487
488 if (!(state & PCIE_LINK_STATE_L1))
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900489 __pcie_aspm_config_one_dev(parent, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800490
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900491 link->aspm_enabled = state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800492}
493
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900494/* Check the whole hierarchy, and configure each link in the hierarchy */
495static void __pcie_aspm_configure_link_state(struct pcie_link_state *link,
496 u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800497{
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900498 struct pcie_link_state *leaf, *root = link->root;
Shaohua Li7d715a62008-02-25 09:46:41 +0800499
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900500 state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800501
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900502 /* Check all links who have specific root port link */
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900503 list_for_each_entry(leaf, &link_list, sibling) {
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900504 if (!list_empty(&leaf->children) || (leaf->root != root))
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800505 continue;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900506 state = pcie_aspm_check_state(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800507 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900508 /* Check root port link too in case it hasn't children */
509 state = pcie_aspm_check_state(root, state);
510 if (link->aspm_enabled == state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800511 return;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800512 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900513 * We must change the hierarchy. See comments in
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800514 * __pcie_aspm_config_link for the order
515 **/
516 if (state & PCIE_LINK_STATE_L1) {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900517 list_for_each_entry(leaf, &link_list, sibling) {
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900518 if (leaf->root == root)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900519 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800520 }
521 } else {
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900522 list_for_each_entry_reverse(leaf, &link_list, sibling) {
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900523 if (leaf->root == root)
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900524 __pcie_aspm_config_link(leaf, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800525 }
526 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800527}
528
529/*
530 * pcie_aspm_configure_link_state: enable/disable PCI express link state
531 * @pdev: the root port or switch downstream port
532 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900533static void pcie_aspm_configure_link_state(struct pcie_link_state *link,
534 u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800535{
536 down_read(&pci_bus_sem);
537 mutex_lock(&aspm_lock);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900538 __pcie_aspm_configure_link_state(link, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800539 mutex_unlock(&aspm_lock);
540 up_read(&pci_bus_sem);
541}
542
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900543static void free_link_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800544{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900545 link->pdev->link_state = NULL;
546 kfree(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800547}
548
Shaohua Liddc97532008-05-21 16:58:40 +0800549static int pcie_aspm_sanity_check(struct pci_dev *pdev)
550{
Kenji Kaneshige36475842009-05-13 12:23:09 +0900551 struct pci_dev *child;
552 int pos;
Shaohua Li149e1632008-07-23 10:32:31 +0800553 u32 reg32;
Shaohua Liddc97532008-05-21 16:58:40 +0800554 /*
Kenji Kaneshige36475842009-05-13 12:23:09 +0900555 * Some functions in a slot might not all be PCIE functions,
556 * very strange. Disable ASPM for the whole slot
Shaohua Liddc97532008-05-21 16:58:40 +0800557 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900558 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
559 pos = pci_find_capability(child, PCI_CAP_ID_EXP);
560 if (!pos)
Shaohua Liddc97532008-05-21 16:58:40 +0800561 return -EINVAL;
Shaohua Li149e1632008-07-23 10:32:31 +0800562 /*
563 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
564 * RBER bit to determine if a function is 1.1 version device
565 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900566 pci_read_config_dword(child, pos + PCI_EXP_DEVCAP, &reg32);
Sitsofe Wheelere1f4f592008-09-16 14:27:13 +0100567 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
Kenji Kaneshige36475842009-05-13 12:23:09 +0900568 dev_printk(KERN_INFO, &child->dev, "disabling ASPM"
Vincent Legollf393d9b2008-10-12 12:26:12 +0200569 " on pre-1.1 PCIe device. You can enable it"
570 " with 'pcie_aspm=force'\n");
Shaohua Li149e1632008-07-23 10:32:31 +0800571 return -EINVAL;
572 }
Shaohua Liddc97532008-05-21 16:58:40 +0800573 }
574 return 0;
575}
576
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900577static struct pcie_link_state *pcie_aspm_setup_link_state(struct pci_dev *pdev)
578{
579 struct pcie_link_state *link;
580 int blacklist = !!pcie_aspm_sanity_check(pdev);
581
582 link = kzalloc(sizeof(*link), GFP_KERNEL);
583 if (!link)
584 return NULL;
585 INIT_LIST_HEAD(&link->sibling);
586 INIT_LIST_HEAD(&link->children);
587 INIT_LIST_HEAD(&link->link);
588 link->pdev = pdev;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900589 if (pdev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) {
590 struct pcie_link_state *parent;
591 parent = pdev->bus->parent->self->link_state;
592 if (!parent) {
593 kfree(link);
594 return NULL;
595 }
596 link->parent = parent;
597 list_add(&link->link, &parent->children);
598 }
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900599 /* Setup a pointer to the root port link */
600 if (!link->parent)
601 link->root = link;
602 else
603 link->root = link->parent->root;
604
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900605 list_add(&link->sibling, &link_list);
606
607 pdev->link_state = link;
608
609 /* Check ASPM capability */
610 pcie_aspm_cap_init(link, blacklist);
611
612 /* Check Clock PM capability */
613 pcie_clkpm_cap_init(link, blacklist);
614
615 return link;
616}
617
Shaohua Li7d715a62008-02-25 09:46:41 +0800618/*
619 * pcie_aspm_init_link_state: Initiate PCI express link state.
620 * It is called after the pcie and its children devices are scaned.
621 * @pdev: the root port or switch downstream port
622 */
623void pcie_aspm_init_link_state(struct pci_dev *pdev)
624{
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900625 u32 state;
626 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800627
628 if (aspm_disabled || !pdev->is_pcie || pdev->link_state)
629 return;
630 if (pdev->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900631 pdev->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
Shaohua Li7d715a62008-02-25 09:46:41 +0800632 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900633
Shaohua Li8e822df2009-06-08 09:27:25 +0800634 /* VIA has a strange chipset, root port is under a bridge */
635 if (pdev->pcie_type == PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900636 pdev->bus->self)
Shaohua Li8e822df2009-06-08 09:27:25 +0800637 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900638
Shaohua Li7d715a62008-02-25 09:46:41 +0800639 down_read(&pci_bus_sem);
640 if (list_empty(&pdev->subordinate->devices))
641 goto out;
642
Shaohua Li7d715a62008-02-25 09:46:41 +0800643 mutex_lock(&aspm_lock);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900644 link = pcie_aspm_setup_link_state(pdev);
645 if (!link)
646 goto unlock;
647 /*
648 * Setup initial ASPM state
649 *
650 * If link has switch, delay the link config. The leaf link
651 * initialization will config the whole hierarchy. But we must
652 * make sure BIOS doesn't set unsupported link state.
653 */
Kenji Kaneshigeefdf82882009-05-13 12:22:26 +0900654 if (pcie_aspm_downstream_has_switch(link)) {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900655 state = pcie_aspm_check_state(link, link->aspm_default);
656 __pcie_aspm_config_link(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800657 } else {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900658 state = policy_to_aspm_state(link);
659 __pcie_aspm_configure_link_state(link, state);
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800660 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800661
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900662 /* Setup initial Clock PM state */
663 state = (link->clkpm_capable) ? policy_to_clkpm_state(link) : 0;
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900664 pcie_set_clkpm(link, state);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900665unlock:
Shaohua Li7d715a62008-02-25 09:46:41 +0800666 mutex_unlock(&aspm_lock);
667out:
668 up_read(&pci_bus_sem);
669}
670
671/* @pdev: the endpoint device */
672void pcie_aspm_exit_link_state(struct pci_dev *pdev)
673{
674 struct pci_dev *parent = pdev->bus->self;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900675 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800676
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900677 if (aspm_disabled || !pdev->is_pcie || !parent || !parent->link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800678 return;
679 if (parent->pcie_type != PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900680 parent->pcie_type != PCI_EXP_TYPE_DOWNSTREAM)
Shaohua Li7d715a62008-02-25 09:46:41 +0800681 return;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900682
Shaohua Li7d715a62008-02-25 09:46:41 +0800683 down_read(&pci_bus_sem);
684 mutex_lock(&aspm_lock);
Shaohua Li7d715a62008-02-25 09:46:41 +0800685 /*
686 * All PCIe functions are in one slot, remove one function will remove
Alex Chiang3419c752009-01-28 14:59:18 -0700687 * the whole slot, so just wait until we are the last function left.
Shaohua Li7d715a62008-02-25 09:46:41 +0800688 */
Alex Chiang3419c752009-01-28 14:59:18 -0700689 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
Shaohua Li7d715a62008-02-25 09:46:41 +0800690 goto out;
691
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900692 link = parent->link_state;
693
Shaohua Li7d715a62008-02-25 09:46:41 +0800694 /* All functions are removed, so just disable ASPM for the link */
695 __pcie_aspm_config_one_dev(parent, 0);
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900696 list_del(&link->sibling);
697 list_del(&link->link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800698 /* Clock PM is for endpoint device */
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900699 free_link_state(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800700out:
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 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900719 pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800720}
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;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900729 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800730
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);
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900741 link = parent->link_state;
742 link->aspm_disable |= state;
743 __pcie_aspm_configure_link_state(link, link->aspm_enabled);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900744 if (state & PCIE_LINK_STATE_CLKPM) {
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900745 link->clkpm_capable = 0;
746 pcie_set_clkpm(link, 0);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900747 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800748 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;
Shaohua Li7d715a62008-02-25 09:46:41 +0800756 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;
Kenji Kaneshigedc64cd12009-05-13 12:11:33 +0900769 list_for_each_entry(link_state, &link_list, sibling) {
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900770 __pcie_aspm_configure_link_state(link_state,
771 policy_to_aspm_state(link_state));
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900772 pcie_set_clkpm(link_state, policy_to_clkpm_state(link_state));
Shaohua Li7d715a62008-02-25 09:46:41 +0800773 }
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
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900801 return sprintf(buf, "%d\n", link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800802}
803
804static ssize_t link_state_store(struct device *dev,
805 struct device_attribute *attr,
806 const char *buf,
807 size_t n)
808{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900809 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800810 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 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900817 pcie_aspm_configure_link_state(pdev->link_state, state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800818 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
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900831 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800832}
833
834static ssize_t clk_ctl_store(struct device *dev,
835 struct device_attribute *attr,
836 const char *buf,
837 size_t n)
838{
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900839 struct pci_dev *pdev = to_pci_dev(dev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800840 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);
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900848 pcie_set_clkpm_nocheck(pdev->link_state, !!state);
Shaohua Li7d715a62008-02-25 09:46:41 +0800849 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
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900867 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800868 sysfs_add_file_to_group(&pdev->dev.kobj,
869 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900870 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800871 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
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900883 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +0800884 sysfs_remove_file_from_group(&pdev->dev.kobj,
885 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +0900886 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800887 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{
Shaohua Lid6d38572008-07-23 10:32:42 +0800894 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 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800901 return 1;
902}
903
Shaohua Lid6d38572008-07-23 10:32:42 +0800904__setup("pcie_aspm=", pcie_aspm_disable);
Shaohua Li7d715a62008-02-25 09:46:41 +0800905
Shaohua Li5fde2442008-07-23 10:32:24 +0800906void pcie_no_aspm(void)
907{
Shaohua Lid6d38572008-07-23 10:32:42 +0800908 if (!aspm_force)
909 aspm_disabled = 1;
Shaohua Li5fde2442008-07-23 10:32:24 +0800910}
911
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700912/**
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)
Shaohua Li7d715a62008-02-25 09:46:41 +0800919{
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700920 return !aspm_disabled;
Shaohua Li7d715a62008-02-25 09:46:41 +0800921}
Andrew Patterson3e1b1602008-11-10 15:30:55 -0700922EXPORT_SYMBOL(pcie_aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +0800923