blob: f76eb7704f646853bb33e03e4d7719a695ea65c8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Shaohua Li7d715a62008-02-25 09:46:41 +08002/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * Enable PCIe link L0s/L1 state and Clock Power Management
Shaohua Li7d715a62008-02-25 09:46:41 +08004 *
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 Kaneshigeac180182009-08-19 11:02:13 +090029/* Note: those are not register definitions */
30#define ASPM_STATE_L0S_UP (1) /* Upstream direction L0s state */
31#define ASPM_STATE_L0S_DW (2) /* Downstream direction L0s state */
32#define ASPM_STATE_L1 (4) /* L1 state */
Rajat Jainb2103cc2017-01-02 22:34:11 -080033#define ASPM_STATE_L1_1 (8) /* ASPM L1.1 state */
34#define ASPM_STATE_L1_2 (0x10) /* ASPM L1.2 state */
35#define ASPM_STATE_L1_1_PCIPM (0x20) /* PCI PM L1.1 state */
36#define ASPM_STATE_L1_2_PCIPM (0x40) /* PCI PM L1.2 state */
37#define ASPM_STATE_L1_SS_PCIPM (ASPM_STATE_L1_1_PCIPM | ASPM_STATE_L1_2_PCIPM)
38#define ASPM_STATE_L1_2_MASK (ASPM_STATE_L1_2 | ASPM_STATE_L1_2_PCIPM)
39#define ASPM_STATE_L1SS (ASPM_STATE_L1_1 | ASPM_STATE_L1_1_PCIPM |\
40 ASPM_STATE_L1_2_MASK)
Kenji Kaneshigeac180182009-08-19 11:02:13 +090041#define ASPM_STATE_L0S (ASPM_STATE_L0S_UP | ASPM_STATE_L0S_DW)
Rajat Jainb2103cc2017-01-02 22:34:11 -080042#define ASPM_STATE_ALL (ASPM_STATE_L0S | ASPM_STATE_L1 | \
43 ASPM_STATE_L1SS)
Kenji Kaneshigeac180182009-08-19 11:02:13 +090044
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090045struct aspm_latency {
46 u32 l0s; /* L0s latency (nsec) */
47 u32 l1; /* L1 latency (nsec) */
Shaohua Li7d715a62008-02-25 09:46:41 +080048};
49
50struct pcie_link_state {
Kenji Kaneshige5cde89d2009-05-13 12:17:04 +090051 struct pci_dev *pdev; /* Upstream component of the Link */
Rajat Jainb5a0a9b2017-01-02 22:34:12 -080052 struct pci_dev *downstream; /* Downstream component, function 0 */
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +090053 struct pcie_link_state *root; /* pointer to the root port link */
Kenji Kaneshige5cde89d2009-05-13 12:17:04 +090054 struct pcie_link_state *parent; /* pointer to the parent Link state */
55 struct list_head sibling; /* node in link_list */
56 struct list_head children; /* list of child link states */
57 struct list_head link; /* node in parent's children list */
Shaohua Li7d715a62008-02-25 09:46:41 +080058
59 /* ASPM state */
Rajat Jainb2103cc2017-01-02 22:34:11 -080060 u32 aspm_support:7; /* Supported ASPM state */
61 u32 aspm_enabled:7; /* Enabled ASPM state */
62 u32 aspm_capable:7; /* Capable ASPM state with latency */
63 u32 aspm_default:7; /* Default ASPM state by BIOS */
64 u32 aspm_disable:7; /* Disabled ASPM state */
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +090065
Kenji Kaneshige4d246e42009-05-13 12:15:38 +090066 /* Clock PM state */
67 u32 clkpm_capable:1; /* Clock PM capable? */
68 u32 clkpm_enabled:1; /* Current Clock PM state */
69 u32 clkpm_default:1; /* Default Clock PM state by BIOS */
70
Kenji Kaneshigeac180182009-08-19 11:02:13 +090071 /* Exit latencies */
72 struct aspm_latency latency_up; /* Upstream direction exit latency */
73 struct aspm_latency latency_dw; /* Downstream direction exit latency */
Shaohua Li7d715a62008-02-25 09:46:41 +080074 /*
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090075 * Endpoint acceptable latencies. A pcie downstream port only
76 * has one slot under it, so at most there are 8 functions.
Shaohua Li7d715a62008-02-25 09:46:41 +080077 */
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +090078 struct aspm_latency acceptable[8];
Rajat Jainf1f03662017-01-02 22:34:13 -080079
80 /* L1 PM Substate info */
81 struct {
82 u32 up_cap_ptr; /* L1SS cap ptr in upstream dev */
83 u32 dw_cap_ptr; /* L1SS cap ptr in downstream dev */
84 u32 ctl1; /* value to be programmed in ctl1 */
85 u32 ctl2; /* value to be programmed in ctl2 */
86 } l1ss;
Shaohua Li7d715a62008-02-25 09:46:41 +080087};
88
Matthew Garrett3c076352011-11-10 16:38:33 -050089static int aspm_disabled, aspm_force;
Rafael J. Wysocki8b8bae92011-03-05 13:21:51 +010090static bool aspm_support_enabled = true;
Shaohua Li7d715a62008-02-25 09:46:41 +080091static DEFINE_MUTEX(aspm_lock);
92static LIST_HEAD(link_list);
93
94#define POLICY_DEFAULT 0 /* BIOS default setting */
95#define POLICY_PERFORMANCE 1 /* high performance */
96#define POLICY_POWERSAVE 2 /* high power saving */
Rajat Jainb2103cc2017-01-02 22:34:11 -080097#define POLICY_POWER_SUPERSAVE 3 /* possibly even more power saving */
Matthew Garrettad71c962012-02-03 10:18:13 -050098
99#ifdef CONFIG_PCIEASPM_PERFORMANCE
100static int aspm_policy = POLICY_PERFORMANCE;
101#elif defined CONFIG_PCIEASPM_POWERSAVE
102static int aspm_policy = POLICY_POWERSAVE;
Rajat Jainb2103cc2017-01-02 22:34:11 -0800103#elif defined CONFIG_PCIEASPM_POWER_SUPERSAVE
104static int aspm_policy = POLICY_POWER_SUPERSAVE;
Matthew Garrettad71c962012-02-03 10:18:13 -0500105#else
Shaohua Li7d715a62008-02-25 09:46:41 +0800106static int aspm_policy;
Matthew Garrettad71c962012-02-03 10:18:13 -0500107#endif
108
Shaohua Li7d715a62008-02-25 09:46:41 +0800109static const char *policy_str[] = {
110 [POLICY_DEFAULT] = "default",
111 [POLICY_PERFORMANCE] = "performance",
Rajat Jainb2103cc2017-01-02 22:34:11 -0800112 [POLICY_POWERSAVE] = "powersave",
113 [POLICY_POWER_SUPERSAVE] = "powersupersave"
Shaohua Li7d715a62008-02-25 09:46:41 +0800114};
115
Andrew Patterson987a4c72009-01-05 16:21:04 -0700116#define LINK_RETRAIN_TIMEOUT HZ
117
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900118static int policy_to_aspm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800119{
Shaohua Li7d715a62008-02-25 09:46:41 +0800120 switch (aspm_policy) {
121 case POLICY_PERFORMANCE:
122 /* Disable ASPM and Clock PM */
123 return 0;
124 case POLICY_POWERSAVE:
125 /* Enable ASPM L0s/L1 */
Rajat Jainb2103cc2017-01-02 22:34:11 -0800126 return (ASPM_STATE_L0S | ASPM_STATE_L1);
127 case POLICY_POWER_SUPERSAVE:
128 /* Enable Everything */
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900129 return ASPM_STATE_ALL;
Shaohua Li7d715a62008-02-25 09:46:41 +0800130 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900131 return link->aspm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +0800132 }
133 return 0;
134}
135
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900136static int policy_to_clkpm_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800137{
Shaohua Li7d715a62008-02-25 09:46:41 +0800138 switch (aspm_policy) {
139 case POLICY_PERFORMANCE:
140 /* Disable ASPM and Clock PM */
141 return 0;
142 case POLICY_POWERSAVE:
Rajat Jainb2103cc2017-01-02 22:34:11 -0800143 case POLICY_POWER_SUPERSAVE:
144 /* Enable Clock PM */
Shaohua Li7d715a62008-02-25 09:46:41 +0800145 return 1;
146 case POLICY_DEFAULT:
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900147 return link->clkpm_default;
Shaohua Li7d715a62008-02-25 09:46:41 +0800148 }
149 return 0;
150}
151
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900152static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
Shaohua Li7d715a62008-02-25 09:46:41 +0800153{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900154 struct pci_dev *child;
155 struct pci_bus *linkbus = link->pdev->subordinate;
Bjorn Helgaas0c0cbb62015-06-10 14:00:21 -0500156 u32 val = enable ? PCI_EXP_LNKCTL_CLKREQ_EN : 0;
Shaohua Li7d715a62008-02-25 09:46:41 +0800157
Bjorn Helgaas0c0cbb62015-06-10 14:00:21 -0500158 list_for_each_entry(child, &linkbus->devices, bus_list)
159 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
160 PCI_EXP_LNKCTL_CLKREQ_EN,
161 val);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900162 link->clkpm_enabled = !!enable;
Shaohua Li7d715a62008-02-25 09:46:41 +0800163}
164
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900165static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
166{
167 /* Don't enable Clock PM if the link is not Clock PM capable */
Shawn Lina6c1c6f2016-05-24 17:32:10 +0800168 if (!link->clkpm_capable)
Matthew Garrett2f671e22010-12-06 14:00:56 -0500169 enable = 0;
Kenji Kaneshige430842e2009-05-13 12:20:10 +0900170 /* Need nothing if the specified equals to current state */
171 if (link->clkpm_enabled == enable)
172 return;
173 pcie_set_clkpm_nocheck(link, enable);
174}
175
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900176static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800177{
Jiang Liuf12eb722012-07-24 17:20:12 +0800178 int capable = 1, enabled = 1;
Shaohua Li7d715a62008-02-25 09:46:41 +0800179 u32 reg32;
180 u16 reg16;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900181 struct pci_dev *child;
182 struct pci_bus *linkbus = link->pdev->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800183
184 /* All functions should have the same cap and state, take the worst */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900185 list_for_each_entry(child, &linkbus->devices, bus_list) {
Jiang Liuf12eb722012-07-24 17:20:12 +0800186 pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &reg32);
Shaohua Li7d715a62008-02-25 09:46:41 +0800187 if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
188 capable = 0;
189 enabled = 0;
190 break;
191 }
Jiang Liuf12eb722012-07-24 17:20:12 +0800192 pcie_capability_read_word(child, PCI_EXP_LNKCTL, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800193 if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
194 enabled = 0;
195 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900196 link->clkpm_enabled = enabled;
197 link->clkpm_default = enabled;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900198 link->clkpm_capable = (blacklist) ? 0 : capable;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800199}
200
Shaohua Li7d715a62008-02-25 09:46:41 +0800201/*
202 * pcie_aspm_configure_common_clock: check if the 2 ends of a link
203 * could use common clock. If they are, configure them to use the
204 * common clock. That will reduce the ASPM state exit latency.
205 */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900206static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800207{
Jiang Liuf12eb722012-07-24 17:20:12 +0800208 int same_clock = 1;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900209 u16 reg16, parent_reg, child_reg[8];
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100210 unsigned long start_jiffies;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900211 struct pci_dev *child, *parent = link->pdev;
212 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800213 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900214 * All functions of a slot should have the same Slot Clock
Shaohua Li7d715a62008-02-25 09:46:41 +0800215 * Configuration, so just check one function
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900216 */
217 child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
Kenji Kaneshige8b064772009-11-11 14:36:52 +0900218 BUG_ON(!pci_is_pcie(child));
Shaohua Li7d715a62008-02-25 09:46:41 +0800219
220 /* Check downstream component if bit Slot Clock Configuration is 1 */
Jiang Liuf12eb722012-07-24 17:20:12 +0800221 pcie_capability_read_word(child, PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800222 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
223 same_clock = 0;
224
225 /* Check upstream component if bit Slot Clock Configuration is 1 */
Jiang Liuf12eb722012-07-24 17:20:12 +0800226 pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800227 if (!(reg16 & PCI_EXP_LNKSTA_SLC))
228 same_clock = 0;
229
Sinan Kaya048751772018-01-22 15:12:01 -0500230 /* Port might be already in common clock mode */
231 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &reg16);
232 if (same_clock && (reg16 & PCI_EXP_LNKCTL_CCC)) {
233 bool consistent = true;
234
235 list_for_each_entry(child, &linkbus->devices, bus_list) {
236 pcie_capability_read_word(child, PCI_EXP_LNKCTL,
237 &reg16);
238 if (!(reg16 & PCI_EXP_LNKCTL_CCC)) {
239 consistent = false;
240 break;
241 }
242 }
243 if (consistent)
244 return;
245 pci_warn(parent, "ASPM: current common clock configuration is broken, reconfiguring\n");
246 }
247
Shaohua Li7d715a62008-02-25 09:46:41 +0800248 /* Configure downstream component, all functions */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900249 list_for_each_entry(child, &linkbus->devices, bus_list) {
Jiang Liuf12eb722012-07-24 17:20:12 +0800250 pcie_capability_read_word(child, PCI_EXP_LNKCTL, &reg16);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900251 child_reg[PCI_FUNC(child->devfn)] = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800252 if (same_clock)
253 reg16 |= PCI_EXP_LNKCTL_CCC;
254 else
255 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Jiang Liuf12eb722012-07-24 17:20:12 +0800256 pcie_capability_write_word(child, PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800257 }
258
259 /* Configure upstream component */
Jiang Liuf12eb722012-07-24 17:20:12 +0800260 pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &reg16);
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100261 parent_reg = reg16;
Shaohua Li7d715a62008-02-25 09:46:41 +0800262 if (same_clock)
263 reg16 |= PCI_EXP_LNKCTL_CCC;
264 else
265 reg16 &= ~PCI_EXP_LNKCTL_CCC;
Jiang Liuf12eb722012-07-24 17:20:12 +0800266 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800267
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900268 /* Retrain link */
Shaohua Li7d715a62008-02-25 09:46:41 +0800269 reg16 |= PCI_EXP_LNKCTL_RL;
Jiang Liuf12eb722012-07-24 17:20:12 +0800270 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800271
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900272 /* Wait for link training end. Break out after waiting for timeout */
Thomas Renninger2a42d9d2008-12-09 13:05:09 +0100273 start_jiffies = jiffies;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700274 for (;;) {
Jiang Liuf12eb722012-07-24 17:20:12 +0800275 pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &reg16);
Shaohua Li7d715a62008-02-25 09:46:41 +0800276 if (!(reg16 & PCI_EXP_LNKSTA_LT))
277 break;
Andrew Patterson987a4c72009-01-05 16:21:04 -0700278 if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT))
279 break;
280 msleep(1);
Shaohua Li7d715a62008-02-25 09:46:41 +0800281 }
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900282 if (!(reg16 & PCI_EXP_LNKSTA_LT))
283 return;
284
285 /* Training failed. Restore common clock configurations */
Frederick Lawler7506dc72018-01-18 12:55:24 -0600286 pci_err(parent, "ASPM: Could not configure common clock\n");
Jiang Liuf12eb722012-07-24 17:20:12 +0800287 list_for_each_entry(child, &linkbus->devices, bus_list)
288 pcie_capability_write_word(child, PCI_EXP_LNKCTL,
289 child_reg[PCI_FUNC(child->devfn)]);
290 pcie_capability_write_word(parent, PCI_EXP_LNKCTL, parent_reg);
Shaohua Li7d715a62008-02-25 09:46:41 +0800291}
292
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900293/* Convert L0s latency encoding to ns */
294static u32 calc_l0s_latency(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800295{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900296 if (encoding == 0x7)
297 return (5 * 1000); /* > 4us */
298 return (64 << encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800299}
300
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900301/* Convert L0s acceptable latency encoding to ns */
302static u32 calc_l0s_acceptable(u32 encoding)
Shaohua Li7d715a62008-02-25 09:46:41 +0800303{
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900304 if (encoding == 0x7)
305 return -1U;
306 return (64 << encoding);
307}
Shaohua Li7d715a62008-02-25 09:46:41 +0800308
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900309/* Convert L1 latency encoding to ns */
310static u32 calc_l1_latency(u32 encoding)
311{
312 if (encoding == 0x7)
313 return (65 * 1000); /* > 64us */
314 return (1000 << encoding);
315}
316
317/* Convert L1 acceptable latency encoding to ns */
318static u32 calc_l1_acceptable(u32 encoding)
319{
320 if (encoding == 0x7)
321 return -1U;
322 return (1000 << encoding);
Shaohua Li7d715a62008-02-25 09:46:41 +0800323}
324
Rajat Jainf1f03662017-01-02 22:34:13 -0800325/* Convert L1SS T_pwr encoding to usec */
326static u32 calc_l1ss_pwron(struct pci_dev *pdev, u32 scale, u32 val)
327{
328 switch (scale) {
329 case 0:
330 return val * 2;
331 case 1:
332 return val * 10;
333 case 2:
334 return val * 100;
335 }
Frederick Lawler7506dc72018-01-18 12:55:24 -0600336 pci_err(pdev, "%s: Invalid T_PwrOn scale: %u\n", __func__, scale);
Rajat Jainf1f03662017-01-02 22:34:13 -0800337 return 0;
338}
339
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600340static void encode_l12_threshold(u32 threshold_us, u32 *scale, u32 *value)
341{
Gustavo A. R. Silvaf51af8a2018-02-27 17:19:52 -0600342 u32 threshold_ns = threshold_us * 1000;
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600343
344 /* See PCIe r3.1, sec 7.33.3 and sec 6.18 */
345 if (threshold_ns < 32) {
346 *scale = 0;
347 *value = threshold_ns;
348 } else if (threshold_ns < 1024) {
349 *scale = 1;
350 *value = threshold_ns >> 5;
351 } else if (threshold_ns < 32768) {
352 *scale = 2;
353 *value = threshold_ns >> 10;
354 } else if (threshold_ns < 1048576) {
355 *scale = 3;
356 *value = threshold_ns >> 15;
357 } else if (threshold_ns < 33554432) {
358 *scale = 4;
359 *value = threshold_ns >> 20;
360 } else {
361 *scale = 5;
362 *value = threshold_ns >> 25;
363 }
364}
365
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900366struct aspm_register_info {
367 u32 support:2;
368 u32 enabled:2;
369 u32 latency_encoding_l0s;
370 u32 latency_encoding_l1;
Rajat Jainb5a0a9b2017-01-02 22:34:12 -0800371
372 /* L1 substates */
373 u32 l1ss_cap_ptr;
374 u32 l1ss_cap;
375 u32 l1ss_ctl1;
376 u32 l1ss_ctl2;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900377};
378
379static void pcie_get_aspm_reg(struct pci_dev *pdev,
380 struct aspm_register_info *info)
Shaohua Li7d715a62008-02-25 09:46:41 +0800381{
Shaohua Li7d715a62008-02-25 09:46:41 +0800382 u16 reg16;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900383 u32 reg32;
Shaohua Li7d715a62008-02-25 09:46:41 +0800384
Jiang Liuf12eb722012-07-24 17:20:12 +0800385 pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &reg32);
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900386 info->support = (reg32 & PCI_EXP_LNKCAP_ASPMS) >> 10;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900387 info->latency_encoding_l0s = (reg32 & PCI_EXP_LNKCAP_L0SEL) >> 12;
388 info->latency_encoding_l1 = (reg32 & PCI_EXP_LNKCAP_L1EL) >> 15;
Jiang Liuf12eb722012-07-24 17:20:12 +0800389 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &reg16);
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900390 info->enabled = reg16 & PCI_EXP_LNKCTL_ASPMC;
Rajat Jainb5a0a9b2017-01-02 22:34:12 -0800391
392 /* Read L1 PM substate capabilities */
393 info->l1ss_cap = info->l1ss_ctl1 = info->l1ss_ctl2 = 0;
394 info->l1ss_cap_ptr = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS);
395 if (!info->l1ss_cap_ptr)
396 return;
397 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CAP,
398 &info->l1ss_cap);
399 if (!(info->l1ss_cap & PCI_L1SS_CAP_L1_PM_SS)) {
400 info->l1ss_cap = 0;
401 return;
402 }
403 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL1,
404 &info->l1ss_ctl1);
405 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL2,
406 &info->l1ss_ctl2);
Shaohua Li7d715a62008-02-25 09:46:41 +0800407}
408
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900409static void pcie_aspm_check_latency(struct pci_dev *endpoint)
410{
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900411 u32 latency, l1_switch_latency = 0;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900412 struct aspm_latency *acceptable;
413 struct pcie_link_state *link;
414
415 /* Device not in D0 doesn't need latency check */
416 if ((endpoint->current_state != PCI_D0) &&
417 (endpoint->current_state != PCI_UNKNOWN))
418 return;
419
420 link = endpoint->bus->self->link_state;
421 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
422
423 while (link) {
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900424 /* Check upstream direction L0s latency */
425 if ((link->aspm_capable & ASPM_STATE_L0S_UP) &&
426 (link->latency_up.l0s > acceptable->l0s))
427 link->aspm_capable &= ~ASPM_STATE_L0S_UP;
428
429 /* Check downstream direction L0s latency */
430 if ((link->aspm_capable & ASPM_STATE_L0S_DW) &&
431 (link->latency_dw.l0s > acceptable->l0s))
432 link->aspm_capable &= ~ASPM_STATE_L0S_DW;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900433 /*
434 * Check L1 latency.
435 * Every switch on the path to root complex need 1
436 * more microsecond for L1. Spec doesn't mention L0s.
Rajat Jaina142f4d2017-01-02 22:34:15 -0800437 *
438 * The exit latencies for L1 substates are not advertised
439 * by a device. Since the spec also doesn't mention a way
440 * to determine max latencies introduced by enabling L1
441 * substates on the components, it is not clear how to do
442 * a L1 substate exit latency check. We assume that the
443 * L1 exit latencies advertised by a device include L1
444 * substate latencies (and hence do not do any check).
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900445 */
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900446 latency = max_t(u32, link->latency_up.l1, link->latency_dw.l1);
447 if ((link->aspm_capable & ASPM_STATE_L1) &&
448 (latency + l1_switch_latency > acceptable->l1))
449 link->aspm_capable &= ~ASPM_STATE_L1;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900450 l1_switch_latency += 1000;
451
452 link = link->parent;
453 }
454}
455
Rajat Jainb5a0a9b2017-01-02 22:34:12 -0800456/*
457 * The L1 PM substate capability is only implemented in function 0 in a
458 * multi function device.
459 */
460static struct pci_dev *pci_function_0(struct pci_bus *linkbus)
461{
462 struct pci_dev *child;
463
464 list_for_each_entry(child, &linkbus->devices, bus_list)
465 if (PCI_FUNC(child->devfn) == 0)
466 return child;
467 return NULL;
468}
469
Rajat Jainf1f03662017-01-02 22:34:13 -0800470/* Calculate L1.2 PM substate timing parameters */
471static void aspm_calc_l1ss_info(struct pcie_link_state *link,
472 struct aspm_register_info *upreg,
473 struct aspm_register_info *dwreg)
474{
475 u32 val1, val2, scale1, scale2;
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600476 u32 t_common_mode, t_power_on, l1_2_threshold, scale, value;
Rajat Jainf1f03662017-01-02 22:34:13 -0800477
478 link->l1ss.up_cap_ptr = upreg->l1ss_cap_ptr;
479 link->l1ss.dw_cap_ptr = dwreg->l1ss_cap_ptr;
480 link->l1ss.ctl1 = link->l1ss.ctl2 = 0;
481
482 if (!(link->aspm_support & ASPM_STATE_L1_2_MASK))
483 return;
484
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600485 /* Choose the greater of the two Port Common_Mode_Restore_Times */
486 val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
487 val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600488 t_common_mode = max(val1, val2);
Rajat Jainf1f03662017-01-02 22:34:13 -0800489
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600490 /* Choose the greater of the two Port T_POWER_ON times */
491 val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
492 scale1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
493 val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
494 scale2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
Rajat Jainf1f03662017-01-02 22:34:13 -0800495
496 if (calc_l1ss_pwron(link->pdev, scale1, val1) >
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600497 calc_l1ss_pwron(link->downstream, scale2, val2)) {
Rajat Jainf1f03662017-01-02 22:34:13 -0800498 link->l1ss.ctl2 |= scale1 | (val1 << 3);
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600499 t_power_on = calc_l1ss_pwron(link->pdev, scale1, val1);
500 } else {
Rajat Jainf1f03662017-01-02 22:34:13 -0800501 link->l1ss.ctl2 |= scale2 | (val2 << 3);
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600502 t_power_on = calc_l1ss_pwron(link->downstream, scale2, val2);
503 }
504
505 /*
506 * Set LTR_L1.2_THRESHOLD to the time required to transition the
507 * Link from L0 to L1.2 and back to L0 so we enter L1.2 only if
508 * downstream devices report (via LTR) that they can tolerate at
509 * least that much latency.
510 *
511 * Based on PCIe r3.1, sec 5.5.3.3.1, Figures 5-16 and 5-17, and
512 * Table 5-11. T(POWER_OFF) is at most 2us and T(L1.2) is at
513 * least 4us.
514 */
515 l1_2_threshold = 2 + 4 + t_common_mode + t_power_on;
516 encode_l12_threshold(l1_2_threshold, &scale, &value);
517 link->l1ss.ctl1 |= t_common_mode << 8 | scale << 29 | value << 16;
Rajat Jainf1f03662017-01-02 22:34:13 -0800518}
519
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900520static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800521{
Yinghai Lu3bd7db62017-03-01 00:25:40 -0800522 struct pci_dev *child = link->downstream, *parent = link->pdev;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900523 struct pci_bus *linkbus = parent->subordinate;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900524 struct aspm_register_info upreg, dwreg;
Shaohua Li7d715a62008-02-25 09:46:41 +0800525
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900526 if (blacklist) {
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900527 /* Set enabled/disable so that we will disable ASPM later */
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900528 link->aspm_enabled = ASPM_STATE_ALL;
529 link->aspm_disable = ASPM_STATE_ALL;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900530 return;
531 }
532
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900533 /* Get upstream/downstream components' register state */
534 pcie_get_aspm_reg(parent, &upreg);
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900535 pcie_get_aspm_reg(child, &dwreg);
536
537 /*
David Daneye53f9a22016-11-17 14:25:01 -0800538 * If ASPM not supported, don't mess with the clocks and link,
539 * bail out now.
540 */
541 if (!(upreg.support & dwreg.support))
542 return;
543
544 /* Configure common clock before checking latencies */
545 pcie_aspm_configure_common_clock(link);
546
547 /*
548 * Re-read upstream/downstream components' register state
549 * after clock configuration
550 */
551 pcie_get_aspm_reg(parent, &upreg);
552 pcie_get_aspm_reg(child, &dwreg);
553
554 /*
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900555 * Setup L0s state
556 *
557 * Note that we must not enable L0s in either direction on a
558 * given link unless components on both sides of the link each
559 * support L0s.
560 */
561 if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S)
562 link->aspm_support |= ASPM_STATE_L0S;
563 if (dwreg.enabled & PCIE_LINK_STATE_L0S)
564 link->aspm_enabled |= ASPM_STATE_L0S_UP;
565 if (upreg.enabled & PCIE_LINK_STATE_L0S)
566 link->aspm_enabled |= ASPM_STATE_L0S_DW;
567 link->latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s);
568 link->latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s);
569
570 /* Setup L1 state */
571 if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1)
572 link->aspm_support |= ASPM_STATE_L1;
573 if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1)
574 link->aspm_enabled |= ASPM_STATE_L1;
575 link->latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1);
576 link->latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1);
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900577
Rajat Jainb5a0a9b2017-01-02 22:34:12 -0800578 /* Setup L1 substate */
579 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1)
580 link->aspm_support |= ASPM_STATE_L1_1;
581 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2)
582 link->aspm_support |= ASPM_STATE_L1_2;
583 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1)
584 link->aspm_support |= ASPM_STATE_L1_1_PCIPM;
585 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2)
586 link->aspm_support |= ASPM_STATE_L1_2_PCIPM;
587
588 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1)
589 link->aspm_enabled |= ASPM_STATE_L1_1;
590 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2)
591 link->aspm_enabled |= ASPM_STATE_L1_2;
592 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1)
593 link->aspm_enabled |= ASPM_STATE_L1_1_PCIPM;
594 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2)
595 link->aspm_enabled |= ASPM_STATE_L1_2_PCIPM;
596
Rajat Jainf1f03662017-01-02 22:34:13 -0800597 if (link->aspm_support & ASPM_STATE_L1SS)
598 aspm_calc_l1ss_info(link, &upreg, &dwreg);
599
Kenji Kaneshigeb127bd52009-08-19 10:57:31 +0900600 /* Save default state */
601 link->aspm_default = link->aspm_enabled;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900602
603 /* Setup initial capable state. Will be updated later */
604 link->aspm_capable = link->aspm_support;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900605 /*
606 * If the downstream component has pci bridge function, don't
607 * do ASPM for now.
608 */
609 list_for_each_entry(child, &linkbus->devices, bus_list) {
Yijing Wang62f87c02012-07-24 17:20:03 +0800610 if (pci_pcie_type(child) == PCI_EXP_TYPE_PCI_BRIDGE) {
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900611 link->aspm_disable = ASPM_STATE_ALL;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900612 break;
613 }
614 }
Kenji Kaneshigeb127bd52009-08-19 10:57:31 +0900615
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900616 /* Get and check endpoint acceptable latencies */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900617 list_for_each_entry(child, &linkbus->devices, bus_list) {
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900618 u32 reg32, encoding;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900619 struct aspm_latency *acceptable =
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900620 &link->acceptable[PCI_FUNC(child->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800621
Yijing Wang62f87c02012-07-24 17:20:03 +0800622 if (pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT &&
623 pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END)
Shaohua Li7d715a62008-02-25 09:46:41 +0800624 continue;
625
Jiang Liuf12eb722012-07-24 17:20:12 +0800626 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900627 /* Calculate endpoint L0s acceptable latency */
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900628 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
629 acceptable->l0s = calc_l0s_acceptable(encoding);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900630 /* Calculate endpoint L1 acceptable latency */
631 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
632 acceptable->l1 = calc_l1_acceptable(encoding);
633
634 pcie_aspm_check_latency(child);
Shaohua Li7d715a62008-02-25 09:46:41 +0800635 }
636}
637
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800638static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos,
639 u32 clear, u32 set)
640{
641 u32 val;
642
643 pci_read_config_dword(pdev, pos, &val);
644 val &= ~clear;
645 val |= set;
646 pci_write_config_dword(pdev, pos, val);
647}
648
649/* Configure the ASPM L1 substates */
650static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state)
651{
652 u32 val, enable_req;
653 struct pci_dev *child = link->downstream, *parent = link->pdev;
654 u32 up_cap_ptr = link->l1ss.up_cap_ptr;
655 u32 dw_cap_ptr = link->l1ss.dw_cap_ptr;
656
657 enable_req = (link->aspm_enabled ^ state) & state;
658
659 /*
660 * Here are the rules specified in the PCIe spec for enabling L1SS:
661 * - When enabling L1.x, enable bit at parent first, then at child
662 * - When disabling L1.x, disable bit at child first, then at parent
663 * - When enabling ASPM L1.x, need to disable L1
664 * (at child followed by parent).
665 * - The ASPM/PCIPM L1.2 must be disabled while programming timing
666 * parameters
667 *
668 * To keep it simple, disable all L1SS bits first, and later enable
669 * what is needed.
670 */
671
672 /* Disable all L1 substates */
673 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
674 PCI_L1SS_CTL1_L1SS_MASK, 0);
675 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
676 PCI_L1SS_CTL1_L1SS_MASK, 0);
677 /*
678 * If needed, disable L1, and it gets enabled later
679 * in pcie_config_aspm_link().
680 */
681 if (enable_req & (ASPM_STATE_L1_1 | ASPM_STATE_L1_2)) {
682 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
683 PCI_EXP_LNKCTL_ASPM_L1, 0);
684 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
685 PCI_EXP_LNKCTL_ASPM_L1, 0);
686 }
687
688 if (enable_req & ASPM_STATE_L1_2_MASK) {
689
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600690 /* Program T_POWER_ON times in both ports */
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800691 pci_write_config_dword(parent, up_cap_ptr + PCI_L1SS_CTL2,
692 link->l1ss.ctl2);
693 pci_write_config_dword(child, dw_cap_ptr + PCI_L1SS_CTL2,
694 link->l1ss.ctl2);
695
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600696 /* Program Common_Mode_Restore_Time in upstream device */
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800697 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600698 PCI_L1SS_CTL1_CM_RESTORE_TIME,
699 link->l1ss.ctl1);
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800700
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600701 /* Program LTR_L1.2_THRESHOLD time in both ports */
Bjorn Helgaasc00054f2017-11-13 15:05:50 -0600702 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600703 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
704 PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
705 link->l1ss.ctl1);
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800706 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600707 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
708 PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
709 link->l1ss.ctl1);
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800710 }
711
712 val = 0;
713 if (state & ASPM_STATE_L1_1)
714 val |= PCI_L1SS_CTL1_ASPM_L1_1;
715 if (state & ASPM_STATE_L1_2)
716 val |= PCI_L1SS_CTL1_ASPM_L1_2;
717 if (state & ASPM_STATE_L1_1_PCIPM)
718 val |= PCI_L1SS_CTL1_PCIPM_L1_1;
719 if (state & ASPM_STATE_L1_2_PCIPM)
720 val |= PCI_L1SS_CTL1_PCIPM_L1_2;
721
722 /* Enable what we need to enable */
723 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
724 PCI_L1SS_CAP_L1_PM_SS, val);
725 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
726 PCI_L1SS_CAP_L1_PM_SS, val);
727}
728
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900729static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
Shaohua Li7d715a62008-02-25 09:46:41 +0800730{
Bjorn Helgaas75083202012-12-05 13:51:19 -0700731 pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
732 PCI_EXP_LNKCTL_ASPMC, val);
Shaohua Li7d715a62008-02-25 09:46:41 +0800733}
734
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900735static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800736{
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900737 u32 upstream = 0, dwstream = 0;
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800738 struct pci_dev *child = link->downstream, *parent = link->pdev;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900739 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800740
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800741 /* Enable only the states that were not explicitly disabled */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900742 state &= (link->aspm_capable & ~link->aspm_disable);
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800743
744 /* Can't enable any substates if L1 is not enabled */
745 if (!(state & ASPM_STATE_L1))
746 state &= ~ASPM_STATE_L1SS;
747
748 /* Spec says both ports must be in D0 before enabling PCI PM substates*/
749 if (parent->current_state != PCI_D0 || child->current_state != PCI_D0) {
750 state &= ~ASPM_STATE_L1_SS_PCIPM;
751 state |= (link->aspm_enabled & ASPM_STATE_L1_SS_PCIPM);
752 }
753
754 /* Nothing to do if the link is already in the requested state */
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900755 if (link->aspm_enabled == state)
756 return;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900757 /* Convert ASPM state to upstream/downstream ASPM register state */
758 if (state & ASPM_STATE_L0S_UP)
Bjorn Helgaas75083202012-12-05 13:51:19 -0700759 dwstream |= PCI_EXP_LNKCTL_ASPM_L0S;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900760 if (state & ASPM_STATE_L0S_DW)
Bjorn Helgaas75083202012-12-05 13:51:19 -0700761 upstream |= PCI_EXP_LNKCTL_ASPM_L0S;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900762 if (state & ASPM_STATE_L1) {
Bjorn Helgaas75083202012-12-05 13:51:19 -0700763 upstream |= PCI_EXP_LNKCTL_ASPM_L1;
764 dwstream |= PCI_EXP_LNKCTL_ASPM_L1;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900765 }
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800766
767 if (link->aspm_capable & ASPM_STATE_L1SS)
768 pcie_config_aspm_l1ss(link, state);
769
Shaohua Li7d715a62008-02-25 09:46:41 +0800770 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900771 * Spec 2.0 suggests all functions should be configured the
772 * same setting for ASPM. Enabling ASPM L1 should be done in
773 * upstream component first and then downstream, and vice
774 * versa for disabling ASPM L1. Spec doesn't mention L0S.
Shaohua Li7d715a62008-02-25 09:46:41 +0800775 */
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900776 if (state & ASPM_STATE_L1)
777 pcie_config_aspm_dev(parent, upstream);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900778 list_for_each_entry(child, &linkbus->devices, bus_list)
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900779 pcie_config_aspm_dev(child, dwstream);
780 if (!(state & ASPM_STATE_L1))
781 pcie_config_aspm_dev(parent, upstream);
Shaohua Li7d715a62008-02-25 09:46:41 +0800782
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900783 link->aspm_enabled = state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800784}
785
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900786static void pcie_config_aspm_path(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800787{
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900788 while (link) {
789 pcie_config_aspm_link(link, policy_to_aspm_state(link));
790 link = link->parent;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800791 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800792}
793
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900794static void free_link_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800795{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900796 link->pdev->link_state = NULL;
797 kfree(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800798}
799
Shaohua Liddc97532008-05-21 16:58:40 +0800800static int pcie_aspm_sanity_check(struct pci_dev *pdev)
801{
Kenji Kaneshige36475842009-05-13 12:23:09 +0900802 struct pci_dev *child;
Shaohua Li149e1632008-07-23 10:32:31 +0800803 u32 reg32;
Matthew Garrett2f671e22010-12-06 14:00:56 -0500804
Shaohua Liddc97532008-05-21 16:58:40 +0800805 /*
Stefan Assmann45e829e2009-12-03 06:49:24 -0500806 * Some functions in a slot might not all be PCIe functions,
Kenji Kaneshige36475842009-05-13 12:23:09 +0900807 * very strange. Disable ASPM for the whole slot
Shaohua Liddc97532008-05-21 16:58:40 +0800808 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900809 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
Jiang Liuf12eb722012-07-24 17:20:12 +0800810 if (!pci_is_pcie(child))
Shaohua Liddc97532008-05-21 16:58:40 +0800811 return -EINVAL;
Matthew Garrettc9651e72012-03-27 10:17:41 -0400812
813 /*
814 * If ASPM is disabled then we're not going to change
815 * the BIOS state. It's safe to continue even if it's a
816 * pre-1.1 device
817 */
818
819 if (aspm_disabled)
820 continue;
821
Shaohua Li149e1632008-07-23 10:32:31 +0800822 /*
823 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
824 * RBER bit to determine if a function is 1.1 version device
825 */
Jiang Liuf12eb722012-07-24 17:20:12 +0800826 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
Sitsofe Wheelere1f4f592008-09-16 14:27:13 +0100827 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600828 pci_info(child, "disabling ASPM on pre-1.1 PCIe device. You can enable it with 'pcie_aspm=force'\n");
Shaohua Li149e1632008-07-23 10:32:31 +0800829 return -EINVAL;
830 }
Shaohua Liddc97532008-05-21 16:58:40 +0800831 }
832 return 0;
833}
834
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900835static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900836{
837 struct pcie_link_state *link;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900838
839 link = kzalloc(sizeof(*link), GFP_KERNEL);
840 if (!link)
841 return NULL;
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600842
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900843 INIT_LIST_HEAD(&link->sibling);
844 INIT_LIST_HEAD(&link->children);
845 INIT_LIST_HEAD(&link->link);
846 link->pdev = pdev;
Yinghai Lu3bd7db62017-03-01 00:25:40 -0800847 link->downstream = pci_function_0(pdev->subordinate);
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600848
849 /*
850 * Root Ports and PCI/PCI-X to PCIe Bridges are roots of PCIe
Ard Biesheuvelee8bdfb2017-10-02 15:08:40 +0100851 * hierarchies. Note that some PCIe host implementations omit
852 * the root ports entirely, in which case a downstream port on
853 * a switch may become the root of the link state chain for all
854 * its subordinate endpoints.
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600855 */
856 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
Ard Biesheuvelee8bdfb2017-10-02 15:08:40 +0100857 pci_pcie_type(pdev) == PCI_EXP_TYPE_PCIE_BRIDGE ||
858 !pdev->bus->parent->self) {
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600859 link->root = link;
860 } else {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900861 struct pcie_link_state *parent;
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600862
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900863 parent = pdev->bus->parent->self->link_state;
864 if (!parent) {
865 kfree(link);
866 return NULL;
867 }
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600868
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900869 link->parent = parent;
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600870 link->root = link->parent->root;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900871 list_add(&link->link, &parent->children);
872 }
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900873
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900874 list_add(&link->sibling, &link_list);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900875 pdev->link_state = link;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900876 return link;
877}
878
Shaohua Li7d715a62008-02-25 09:46:41 +0800879/*
880 * pcie_aspm_init_link_state: Initiate PCI express link state.
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700881 * It is called after the pcie and its children devices are scanned.
Shaohua Li7d715a62008-02-25 09:46:41 +0800882 * @pdev: the root port or switch downstream port
883 */
884void pcie_aspm_init_link_state(struct pci_dev *pdev)
885{
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900886 struct pcie_link_state *link;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900887 int blacklist = !!pcie_aspm_sanity_check(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800888
Joe Lawrencea26d5ec2013-01-15 15:31:28 -0500889 if (!aspm_support_enabled)
890 return;
891
Yijing Wangc8fc9332015-05-21 15:05:03 +0800892 if (pdev->link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800893 return;
Yijing Wangc8fc9332015-05-21 15:05:03 +0800894
895 /*
896 * We allocate pcie_link_state for the component on the upstream
897 * end of a Link, so there's nothing to do unless this device has a
898 * Link on its secondary side.
899 */
900 if (!pdev->has_secondary_link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800901 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900902
Shaohua Li8e822df2009-06-08 09:27:25 +0800903 /* VIA has a strange chipset, root port is under a bridge */
Yijing Wang62f87c02012-07-24 17:20:03 +0800904 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900905 pdev->bus->self)
Shaohua Li8e822df2009-06-08 09:27:25 +0800906 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900907
Shaohua Li7d715a62008-02-25 09:46:41 +0800908 down_read(&pci_bus_sem);
909 if (list_empty(&pdev->subordinate->devices))
910 goto out;
911
Shaohua Li7d715a62008-02-25 09:46:41 +0800912 mutex_lock(&aspm_lock);
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900913 link = alloc_pcie_link_state(pdev);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900914 if (!link)
915 goto unlock;
916 /*
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900917 * Setup initial ASPM state. Note that we need to configure
918 * upstream links also because capable state of them can be
919 * update through pcie_aspm_cap_init().
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900920 */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900921 pcie_aspm_cap_init(link, blacklist);
Shaohua Li7d715a62008-02-25 09:46:41 +0800922
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900923 /* Setup initial Clock PM state */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900924 pcie_clkpm_cap_init(link, blacklist);
Matthew Garrett41cd7662010-06-09 16:05:07 -0400925
926 /*
927 * At this stage drivers haven't had an opportunity to change the
928 * link policy setting. Enabling ASPM on broken hardware can cripple
929 * it even before the driver has had a chance to disable ASPM, so
930 * default to a safe level right now. If we're enabling ASPM beyond
931 * the BIOS's expectation, we'll do so once pci_enable_device() is
932 * called.
933 */
Rajat Jainb2103cc2017-01-02 22:34:11 -0800934 if (aspm_policy != POLICY_POWERSAVE &&
935 aspm_policy != POLICY_POWER_SUPERSAVE) {
Matthew Garrett41cd7662010-06-09 16:05:07 -0400936 pcie_config_aspm_path(link);
937 pcie_set_clkpm(link, policy_to_clkpm_state(link));
938 }
939
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900940unlock:
Shaohua Li7d715a62008-02-25 09:46:41 +0800941 mutex_unlock(&aspm_lock);
942out:
943 up_read(&pci_bus_sem);
944}
945
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900946/* Recheck latencies and update aspm_capable for links under the root */
947static void pcie_update_aspm_capable(struct pcie_link_state *root)
948{
949 struct pcie_link_state *link;
950 BUG_ON(root->parent);
951 list_for_each_entry(link, &link_list, sibling) {
952 if (link->root != root)
953 continue;
954 link->aspm_capable = link->aspm_support;
955 }
956 list_for_each_entry(link, &link_list, sibling) {
957 struct pci_dev *child;
958 struct pci_bus *linkbus = link->pdev->subordinate;
959 if (link->root != root)
960 continue;
961 list_for_each_entry(child, &linkbus->devices, bus_list) {
Yijing Wang62f87c02012-07-24 17:20:03 +0800962 if ((pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT) &&
963 (pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END))
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900964 continue;
965 pcie_aspm_check_latency(child);
966 }
967 }
968}
969
Shaohua Li7d715a62008-02-25 09:46:41 +0800970/* @pdev: the endpoint device */
971void pcie_aspm_exit_link_state(struct pci_dev *pdev)
972{
973 struct pci_dev *parent = pdev->bus->self;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900974 struct pcie_link_state *link, *root, *parent_link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800975
Myron Stowe84fb9132013-01-31 16:29:25 -0700976 if (!parent || !parent->link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800977 return;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900978
Shaohua Li7d715a62008-02-25 09:46:41 +0800979 down_read(&pci_bus_sem);
980 mutex_lock(&aspm_lock);
Shaohua Li7d715a62008-02-25 09:46:41 +0800981 /*
982 * All PCIe functions are in one slot, remove one function will remove
Alex Chiang3419c752009-01-28 14:59:18 -0700983 * the whole slot, so just wait until we are the last function left.
Shaohua Li7d715a62008-02-25 09:46:41 +0800984 */
Alex Chiang3419c752009-01-28 14:59:18 -0700985 if (!list_is_last(&pdev->bus_list, &parent->subordinate->devices))
Shaohua Li7d715a62008-02-25 09:46:41 +0800986 goto out;
987
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900988 link = parent->link_state;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900989 root = link->root;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900990 parent_link = link->parent;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900991
Shaohua Li7d715a62008-02-25 09:46:41 +0800992 /* All functions are removed, so just disable ASPM for the link */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900993 pcie_config_aspm_link(link, 0);
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900994 list_del(&link->sibling);
995 list_del(&link->link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800996 /* Clock PM is for endpoint device */
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900997 free_link_state(link);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900998
999 /* Recheck latencies and configure upstream links */
Kenji Kaneshigeb26a34a2009-11-06 11:25:13 +09001000 if (parent_link) {
1001 pcie_update_aspm_capable(root);
1002 pcie_config_aspm_path(parent_link);
1003 }
Shaohua Li7d715a62008-02-25 09:46:41 +08001004out:
1005 mutex_unlock(&aspm_lock);
1006 up_read(&pci_bus_sem);
1007}
1008
1009/* @pdev: the root port or switch downstream port */
1010void pcie_aspm_pm_state_change(struct pci_dev *pdev)
1011{
Kenji Kaneshige07d92762009-08-19 11:00:25 +09001012 struct pcie_link_state *link = pdev->link_state;
Shaohua Li7d715a62008-02-25 09:46:41 +08001013
Yijing Wangf9b8cd72015-05-19 11:41:34 +08001014 if (aspm_disabled || !link)
Shaohua Li7d715a62008-02-25 09:46:41 +08001015 return;
1016 /*
Kenji Kaneshige07d92762009-08-19 11:00:25 +09001017 * Devices changed PM state, we should recheck if latency
1018 * meets all functions' requirement
Shaohua Li7d715a62008-02-25 09:46:41 +08001019 */
Kenji Kaneshige07d92762009-08-19 11:00:25 +09001020 down_read(&pci_bus_sem);
1021 mutex_lock(&aspm_lock);
1022 pcie_update_aspm_capable(link->root);
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001023 pcie_config_aspm_path(link);
Kenji Kaneshige07d92762009-08-19 11:00:25 +09001024 mutex_unlock(&aspm_lock);
1025 up_read(&pci_bus_sem);
Shaohua Li7d715a62008-02-25 09:46:41 +08001026}
1027
Naga Chumbalkar1a680b72011-03-21 03:29:08 +00001028void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
1029{
1030 struct pcie_link_state *link = pdev->link_state;
1031
Yijing Wangf9b8cd72015-05-19 11:41:34 +08001032 if (aspm_disabled || !link)
Naga Chumbalkar1a680b72011-03-21 03:29:08 +00001033 return;
1034
Rajat Jainb2103cc2017-01-02 22:34:11 -08001035 if (aspm_policy != POLICY_POWERSAVE &&
1036 aspm_policy != POLICY_POWER_SUPERSAVE)
Naga Chumbalkar1a680b72011-03-21 03:29:08 +00001037 return;
1038
Naga Chumbalkar1a680b72011-03-21 03:29:08 +00001039 down_read(&pci_bus_sem);
1040 mutex_lock(&aspm_lock);
1041 pcie_config_aspm_path(link);
1042 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1043 mutex_unlock(&aspm_lock);
1044 up_read(&pci_bus_sem);
1045}
1046
Bjorn Helgaase127a042015-05-20 12:13:05 -05001047static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
Shaohua Li7d715a62008-02-25 09:46:41 +08001048{
1049 struct pci_dev *parent = pdev->bus->self;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +09001050 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +08001051
Matthew Garrett3c076352011-11-10 16:38:33 -05001052 if (!pci_is_pcie(pdev))
1053 return;
1054
Yijing Wangc8fc9332015-05-21 15:05:03 +08001055 if (pdev->has_secondary_link)
Shaohua Li7d715a62008-02-25 09:46:41 +08001056 parent = pdev;
1057 if (!parent || !parent->link_state)
1058 return;
1059
Bjorn Helgaas2add0ec2013-05-21 10:56:51 -06001060 /*
1061 * A driver requested that ASPM be disabled on this device, but
1062 * if we don't have permission to manage ASPM (e.g., on ACPI
1063 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
1064 * the _OSC method), we can't honor that request. Windows has
1065 * a similar mechanism using "PciASPMOptOut", which is also
1066 * ignored in this situation.
1067 */
Bjorn Helgaase127a042015-05-20 12:13:05 -05001068 if (aspm_disabled) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001069 pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n");
Bjorn Helgaas2add0ec2013-05-21 10:56:51 -06001070 return;
1071 }
1072
Yinghai Lu9f728f52011-05-12 17:11:47 -07001073 if (sem)
1074 down_read(&pci_bus_sem);
Shaohua Li7d715a62008-02-25 09:46:41 +08001075 mutex_lock(&aspm_lock);
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +09001076 link = parent->link_state;
Kenji Kaneshigeac180182009-08-19 11:02:13 +09001077 if (state & PCIE_LINK_STATE_L0S)
1078 link->aspm_disable |= ASPM_STATE_L0S;
1079 if (state & PCIE_LINK_STATE_L1)
1080 link->aspm_disable |= ASPM_STATE_L1;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001081 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1082
Kenji Kaneshige430842e2009-05-13 12:20:10 +09001083 if (state & PCIE_LINK_STATE_CLKPM) {
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +09001084 link->clkpm_capable = 0;
1085 pcie_set_clkpm(link, 0);
Kenji Kaneshige430842e2009-05-13 12:20:10 +09001086 }
Shaohua Li7d715a62008-02-25 09:46:41 +08001087 mutex_unlock(&aspm_lock);
Yinghai Lu9f728f52011-05-12 17:11:47 -07001088 if (sem)
1089 up_read(&pci_bus_sem);
1090}
1091
1092void pci_disable_link_state_locked(struct pci_dev *pdev, int state)
1093{
Bjorn Helgaase127a042015-05-20 12:13:05 -05001094 __pci_disable_link_state(pdev, state, false);
Yinghai Lu9f728f52011-05-12 17:11:47 -07001095}
1096EXPORT_SYMBOL(pci_disable_link_state_locked);
1097
Yijing Wang2dfca872013-05-28 16:03:22 +08001098/**
1099 * pci_disable_link_state - Disable device's link state, so the link will
1100 * never enter specific states. Note that if the BIOS didn't grant ASPM
1101 * control to the OS, this does nothing because we can't touch the LNKCTL
1102 * register.
1103 *
1104 * @pdev: PCI device
1105 * @state: ASPM link state to disable
1106 */
Yinghai Lu9f728f52011-05-12 17:11:47 -07001107void pci_disable_link_state(struct pci_dev *pdev, int state)
1108{
Bjorn Helgaase127a042015-05-20 12:13:05 -05001109 __pci_disable_link_state(pdev, state, true);
Shaohua Li7d715a62008-02-25 09:46:41 +08001110}
1111EXPORT_SYMBOL(pci_disable_link_state);
1112
Kees Cooke4dca7b2017-10-17 19:04:42 -07001113static int pcie_aspm_set_policy(const char *val,
1114 const struct kernel_param *kp)
Shaohua Li7d715a62008-02-25 09:46:41 +08001115{
1116 int i;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001117 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +08001118
Naga Chumbalkarbbfa3062011-03-21 03:29:14 +00001119 if (aspm_disabled)
1120 return -EPERM;
Shaohua Li7d715a62008-02-25 09:46:41 +08001121 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
1122 if (!strncmp(val, policy_str[i], strlen(policy_str[i])))
1123 break;
1124 if (i >= ARRAY_SIZE(policy_str))
1125 return -EINVAL;
1126 if (i == aspm_policy)
1127 return 0;
1128
1129 down_read(&pci_bus_sem);
1130 mutex_lock(&aspm_lock);
1131 aspm_policy = i;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001132 list_for_each_entry(link, &link_list, sibling) {
1133 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1134 pcie_set_clkpm(link, policy_to_clkpm_state(link));
Shaohua Li7d715a62008-02-25 09:46:41 +08001135 }
1136 mutex_unlock(&aspm_lock);
1137 up_read(&pci_bus_sem);
1138 return 0;
1139}
1140
Kees Cooke4dca7b2017-10-17 19:04:42 -07001141static int pcie_aspm_get_policy(char *buffer, const struct kernel_param *kp)
Shaohua Li7d715a62008-02-25 09:46:41 +08001142{
1143 int i, cnt = 0;
1144 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
1145 if (i == aspm_policy)
1146 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
1147 else
1148 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
1149 return cnt;
1150}
1151
1152module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
1153 NULL, 0644);
1154
1155#ifdef CONFIG_PCIEASPM_DEBUG
1156static ssize_t link_state_show(struct device *dev,
1157 struct device_attribute *attr,
1158 char *buf)
1159{
1160 struct pci_dev *pci_device = to_pci_dev(dev);
1161 struct pcie_link_state *link_state = pci_device->link_state;
1162
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +09001163 return sprintf(buf, "%d\n", link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +08001164}
1165
1166static ssize_t link_state_store(struct device *dev,
1167 struct device_attribute *attr,
1168 const char *buf,
1169 size_t n)
1170{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +09001171 struct pci_dev *pdev = to_pci_dev(dev);
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001172 struct pcie_link_state *link, *root = pdev->link_state->root;
Andy Lutomirski57d86a02015-11-19 08:05:35 -08001173 u32 state;
Shaohua Li7d715a62008-02-25 09:46:41 +08001174
Naga Chumbalkarbbfa3062011-03-21 03:29:14 +00001175 if (aspm_disabled)
1176 return -EPERM;
Shaohua Li7d715a62008-02-25 09:46:41 +08001177
Andy Lutomirski57d86a02015-11-19 08:05:35 -08001178 if (kstrtouint(buf, 10, &state))
1179 return -EINVAL;
1180 if ((state & ~ASPM_STATE_ALL) != 0)
1181 return -EINVAL;
Kenji Kaneshigeac180182009-08-19 11:02:13 +09001182
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001183 down_read(&pci_bus_sem);
1184 mutex_lock(&aspm_lock);
1185 list_for_each_entry(link, &link_list, sibling) {
1186 if (link->root != root)
1187 continue;
1188 pcie_config_aspm_link(link, state);
1189 }
1190 mutex_unlock(&aspm_lock);
1191 up_read(&pci_bus_sem);
1192 return n;
Shaohua Li7d715a62008-02-25 09:46:41 +08001193}
1194
1195static ssize_t clk_ctl_show(struct device *dev,
1196 struct device_attribute *attr,
1197 char *buf)
1198{
1199 struct pci_dev *pci_device = to_pci_dev(dev);
1200 struct pcie_link_state *link_state = pci_device->link_state;
1201
Kenji Kaneshige4d246e42009-05-13 12:15:38 +09001202 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +08001203}
1204
1205static ssize_t clk_ctl_store(struct device *dev,
1206 struct device_attribute *attr,
1207 const char *buf,
1208 size_t n)
1209{
Kenji Kaneshige430842e2009-05-13 12:20:10 +09001210 struct pci_dev *pdev = to_pci_dev(dev);
Chris J Arges94a90312014-12-05 17:02:42 -06001211 bool state;
Shaohua Li7d715a62008-02-25 09:46:41 +08001212
Chris J Arges94a90312014-12-05 17:02:42 -06001213 if (strtobool(buf, &state))
Shaohua Li7d715a62008-02-25 09:46:41 +08001214 return -EINVAL;
Shaohua Li7d715a62008-02-25 09:46:41 +08001215
1216 down_read(&pci_bus_sem);
1217 mutex_lock(&aspm_lock);
Chris J Arges94a90312014-12-05 17:02:42 -06001218 pcie_set_clkpm_nocheck(pdev->link_state, state);
Shaohua Li7d715a62008-02-25 09:46:41 +08001219 mutex_unlock(&aspm_lock);
1220 up_read(&pci_bus_sem);
1221
1222 return n;
1223}
1224
Julia Lawallfc4f57f2016-10-29 21:37:07 +02001225static DEVICE_ATTR_RW(link_state);
1226static DEVICE_ATTR_RW(clk_ctl);
Shaohua Li7d715a62008-02-25 09:46:41 +08001227
1228static char power_group[] = "power";
1229void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
1230{
1231 struct pcie_link_state *link_state = pdev->link_state;
1232
Yijing Wangf9b8cd72015-05-19 11:41:34 +08001233 if (!link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +08001234 return;
1235
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +09001236 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +08001237 sysfs_add_file_to_group(&pdev->dev.kobj,
1238 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +09001239 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +08001240 sysfs_add_file_to_group(&pdev->dev.kobj,
1241 &dev_attr_clk_ctl.attr, power_group);
1242}
1243
1244void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
1245{
1246 struct pcie_link_state *link_state = pdev->link_state;
1247
Yijing Wangf9b8cd72015-05-19 11:41:34 +08001248 if (!link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +08001249 return;
1250
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +09001251 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +08001252 sysfs_remove_file_from_group(&pdev->dev.kobj,
1253 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +09001254 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +08001255 sysfs_remove_file_from_group(&pdev->dev.kobj,
1256 &dev_attr_clk_ctl.attr, power_group);
1257}
1258#endif
1259
1260static int __init pcie_aspm_disable(char *str)
1261{
Shaohua Lid6d38572008-07-23 10:32:42 +08001262 if (!strcmp(str, "off")) {
Matthew Garrett3c076352011-11-10 16:38:33 -05001263 aspm_policy = POLICY_DEFAULT;
Shaohua Lid6d38572008-07-23 10:32:42 +08001264 aspm_disabled = 1;
Rafael J. Wysocki8b8bae92011-03-05 13:21:51 +01001265 aspm_support_enabled = false;
Shaohua Lid6d38572008-07-23 10:32:42 +08001266 printk(KERN_INFO "PCIe ASPM is disabled\n");
1267 } else if (!strcmp(str, "force")) {
1268 aspm_force = 1;
Michael Witten8072ba12011-06-28 06:15:05 +00001269 printk(KERN_INFO "PCIe ASPM is forcibly enabled\n");
Shaohua Lid6d38572008-07-23 10:32:42 +08001270 }
Shaohua Li7d715a62008-02-25 09:46:41 +08001271 return 1;
1272}
1273
Shaohua Lid6d38572008-07-23 10:32:42 +08001274__setup("pcie_aspm=", pcie_aspm_disable);
Shaohua Li7d715a62008-02-25 09:46:41 +08001275
Shaohua Li5fde2442008-07-23 10:32:24 +08001276void pcie_no_aspm(void)
1277{
Matthew Garrett3c076352011-11-10 16:38:33 -05001278 /*
1279 * Disabling ASPM is intended to prevent the kernel from modifying
1280 * existing hardware state, not to clear existing state. To that end:
1281 * (a) set policy to POLICY_DEFAULT in order to avoid changing state
1282 * (b) prevent userspace from changing policy
1283 */
1284 if (!aspm_force) {
1285 aspm_policy = POLICY_DEFAULT;
Shaohua Lid6d38572008-07-23 10:32:42 +08001286 aspm_disabled = 1;
Matthew Garrett3c076352011-11-10 16:38:33 -05001287 }
Shaohua Li5fde2442008-07-23 10:32:24 +08001288}
1289
Rafael J. Wysocki8b8bae92011-03-05 13:21:51 +01001290bool pcie_aspm_support_enabled(void)
1291{
1292 return aspm_support_enabled;
1293}
1294EXPORT_SYMBOL(pcie_aspm_support_enabled);