blob: dcb29cb76dc69d1a958f4ae25732048b7532e549 [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 }
Bjorn Helgaas9ab105d2018-04-17 11:25:51 -0500403
404 /*
405 * If we don't have LTR for the entire path from the Root Complex
406 * to this device, we can't use ASPM L1.2 because it relies on the
407 * LTR_L1.2_THRESHOLD. See PCIe r4.0, secs 5.5.4, 6.18.
408 */
409 if (!pdev->ltr_path)
410 info->l1ss_cap &= ~PCI_L1SS_CAP_ASPM_L1_2;
411
Rajat Jainb5a0a9b2017-01-02 22:34:12 -0800412 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL1,
413 &info->l1ss_ctl1);
414 pci_read_config_dword(pdev, info->l1ss_cap_ptr + PCI_L1SS_CTL2,
415 &info->l1ss_ctl2);
Shaohua Li7d715a62008-02-25 09:46:41 +0800416}
417
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900418static void pcie_aspm_check_latency(struct pci_dev *endpoint)
419{
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900420 u32 latency, l1_switch_latency = 0;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900421 struct aspm_latency *acceptable;
422 struct pcie_link_state *link;
423
424 /* Device not in D0 doesn't need latency check */
425 if ((endpoint->current_state != PCI_D0) &&
426 (endpoint->current_state != PCI_UNKNOWN))
427 return;
428
429 link = endpoint->bus->self->link_state;
430 acceptable = &link->acceptable[PCI_FUNC(endpoint->devfn)];
431
432 while (link) {
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900433 /* Check upstream direction L0s latency */
434 if ((link->aspm_capable & ASPM_STATE_L0S_UP) &&
435 (link->latency_up.l0s > acceptable->l0s))
436 link->aspm_capable &= ~ASPM_STATE_L0S_UP;
437
438 /* Check downstream direction L0s latency */
439 if ((link->aspm_capable & ASPM_STATE_L0S_DW) &&
440 (link->latency_dw.l0s > acceptable->l0s))
441 link->aspm_capable &= ~ASPM_STATE_L0S_DW;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900442 /*
443 * Check L1 latency.
444 * Every switch on the path to root complex need 1
445 * more microsecond for L1. Spec doesn't mention L0s.
Rajat Jaina142f4d2017-01-02 22:34:15 -0800446 *
447 * The exit latencies for L1 substates are not advertised
448 * by a device. Since the spec also doesn't mention a way
449 * to determine max latencies introduced by enabling L1
450 * substates on the components, it is not clear how to do
451 * a L1 substate exit latency check. We assume that the
452 * L1 exit latencies advertised by a device include L1
453 * substate latencies (and hence do not do any check).
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900454 */
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900455 latency = max_t(u32, link->latency_up.l1, link->latency_dw.l1);
456 if ((link->aspm_capable & ASPM_STATE_L1) &&
457 (latency + l1_switch_latency > acceptable->l1))
458 link->aspm_capable &= ~ASPM_STATE_L1;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900459 l1_switch_latency += 1000;
460
461 link = link->parent;
462 }
463}
464
Rajat Jainb5a0a9b2017-01-02 22:34:12 -0800465/*
466 * The L1 PM substate capability is only implemented in function 0 in a
467 * multi function device.
468 */
469static struct pci_dev *pci_function_0(struct pci_bus *linkbus)
470{
471 struct pci_dev *child;
472
473 list_for_each_entry(child, &linkbus->devices, bus_list)
474 if (PCI_FUNC(child->devfn) == 0)
475 return child;
476 return NULL;
477}
478
Rajat Jainf1f03662017-01-02 22:34:13 -0800479/* Calculate L1.2 PM substate timing parameters */
480static void aspm_calc_l1ss_info(struct pcie_link_state *link,
481 struct aspm_register_info *upreg,
482 struct aspm_register_info *dwreg)
483{
484 u32 val1, val2, scale1, scale2;
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600485 u32 t_common_mode, t_power_on, l1_2_threshold, scale, value;
Rajat Jainf1f03662017-01-02 22:34:13 -0800486
487 link->l1ss.up_cap_ptr = upreg->l1ss_cap_ptr;
488 link->l1ss.dw_cap_ptr = dwreg->l1ss_cap_ptr;
489 link->l1ss.ctl1 = link->l1ss.ctl2 = 0;
490
491 if (!(link->aspm_support & ASPM_STATE_L1_2_MASK))
492 return;
493
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600494 /* Choose the greater of the two Port Common_Mode_Restore_Times */
495 val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
496 val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600497 t_common_mode = max(val1, val2);
Rajat Jainf1f03662017-01-02 22:34:13 -0800498
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600499 /* Choose the greater of the two Port T_POWER_ON times */
500 val1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
501 scale1 = (upreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
502 val2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
503 scale2 = (dwreg->l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
Rajat Jainf1f03662017-01-02 22:34:13 -0800504
505 if (calc_l1ss_pwron(link->pdev, scale1, val1) >
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600506 calc_l1ss_pwron(link->downstream, scale2, val2)) {
Rajat Jainf1f03662017-01-02 22:34:13 -0800507 link->l1ss.ctl2 |= scale1 | (val1 << 3);
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600508 t_power_on = calc_l1ss_pwron(link->pdev, scale1, val1);
509 } else {
Rajat Jainf1f03662017-01-02 22:34:13 -0800510 link->l1ss.ctl2 |= scale2 | (val2 << 3);
Bjorn Helgaas80d7d7a2017-11-17 14:26:42 -0600511 t_power_on = calc_l1ss_pwron(link->downstream, scale2, val2);
512 }
513
514 /*
515 * Set LTR_L1.2_THRESHOLD to the time required to transition the
516 * Link from L0 to L1.2 and back to L0 so we enter L1.2 only if
517 * downstream devices report (via LTR) that they can tolerate at
518 * least that much latency.
519 *
520 * Based on PCIe r3.1, sec 5.5.3.3.1, Figures 5-16 and 5-17, and
521 * Table 5-11. T(POWER_OFF) is at most 2us and T(L1.2) is at
522 * least 4us.
523 */
524 l1_2_threshold = 2 + 4 + t_common_mode + t_power_on;
525 encode_l12_threshold(l1_2_threshold, &scale, &value);
526 link->l1ss.ctl1 |= t_common_mode << 8 | scale << 29 | value << 16;
Rajat Jainf1f03662017-01-02 22:34:13 -0800527}
528
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900529static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
Shaohua Li7d715a62008-02-25 09:46:41 +0800530{
Yinghai Lu3bd7db62017-03-01 00:25:40 -0800531 struct pci_dev *child = link->downstream, *parent = link->pdev;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900532 struct pci_bus *linkbus = parent->subordinate;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900533 struct aspm_register_info upreg, dwreg;
Shaohua Li7d715a62008-02-25 09:46:41 +0800534
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900535 if (blacklist) {
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900536 /* Set enabled/disable so that we will disable ASPM later */
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900537 link->aspm_enabled = ASPM_STATE_ALL;
538 link->aspm_disable = ASPM_STATE_ALL;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900539 return;
540 }
541
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900542 /* Get upstream/downstream components' register state */
543 pcie_get_aspm_reg(parent, &upreg);
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900544 pcie_get_aspm_reg(child, &dwreg);
545
546 /*
David Daneye53f9a22016-11-17 14:25:01 -0800547 * If ASPM not supported, don't mess with the clocks and link,
548 * bail out now.
549 */
550 if (!(upreg.support & dwreg.support))
551 return;
552
553 /* Configure common clock before checking latencies */
554 pcie_aspm_configure_common_clock(link);
555
556 /*
557 * Re-read upstream/downstream components' register state
558 * after clock configuration
559 */
560 pcie_get_aspm_reg(parent, &upreg);
561 pcie_get_aspm_reg(child, &dwreg);
562
563 /*
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900564 * Setup L0s state
565 *
566 * Note that we must not enable L0s in either direction on a
567 * given link unless components on both sides of the link each
568 * support L0s.
569 */
570 if (dwreg.support & upreg.support & PCIE_LINK_STATE_L0S)
571 link->aspm_support |= ASPM_STATE_L0S;
572 if (dwreg.enabled & PCIE_LINK_STATE_L0S)
573 link->aspm_enabled |= ASPM_STATE_L0S_UP;
574 if (upreg.enabled & PCIE_LINK_STATE_L0S)
575 link->aspm_enabled |= ASPM_STATE_L0S_DW;
576 link->latency_up.l0s = calc_l0s_latency(upreg.latency_encoding_l0s);
577 link->latency_dw.l0s = calc_l0s_latency(dwreg.latency_encoding_l0s);
578
579 /* Setup L1 state */
580 if (upreg.support & dwreg.support & PCIE_LINK_STATE_L1)
581 link->aspm_support |= ASPM_STATE_L1;
582 if (upreg.enabled & dwreg.enabled & PCIE_LINK_STATE_L1)
583 link->aspm_enabled |= ASPM_STATE_L1;
584 link->latency_up.l1 = calc_l1_latency(upreg.latency_encoding_l1);
585 link->latency_dw.l1 = calc_l1_latency(dwreg.latency_encoding_l1);
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +0900586
Rajat Jainb5a0a9b2017-01-02 22:34:12 -0800587 /* Setup L1 substate */
588 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1)
589 link->aspm_support |= ASPM_STATE_L1_1;
590 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2)
591 link->aspm_support |= ASPM_STATE_L1_2;
592 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1)
593 link->aspm_support |= ASPM_STATE_L1_1_PCIPM;
594 if (upreg.l1ss_cap & dwreg.l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2)
595 link->aspm_support |= ASPM_STATE_L1_2_PCIPM;
596
597 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1)
598 link->aspm_enabled |= ASPM_STATE_L1_1;
599 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2)
600 link->aspm_enabled |= ASPM_STATE_L1_2;
601 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1)
602 link->aspm_enabled |= ASPM_STATE_L1_1_PCIPM;
603 if (upreg.l1ss_ctl1 & dwreg.l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2)
604 link->aspm_enabled |= ASPM_STATE_L1_2_PCIPM;
605
Rajat Jainf1f03662017-01-02 22:34:13 -0800606 if (link->aspm_support & ASPM_STATE_L1SS)
607 aspm_calc_l1ss_info(link, &upreg, &dwreg);
608
Kenji Kaneshigeb127bd52009-08-19 10:57:31 +0900609 /* Save default state */
610 link->aspm_default = link->aspm_enabled;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900611
612 /* Setup initial capable state. Will be updated later */
613 link->aspm_capable = link->aspm_support;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900614 /*
615 * If the downstream component has pci bridge function, don't
616 * do ASPM for now.
617 */
618 list_for_each_entry(child, &linkbus->devices, bus_list) {
Yijing Wang62f87c02012-07-24 17:20:03 +0800619 if (pci_pcie_type(child) == PCI_EXP_TYPE_PCI_BRIDGE) {
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900620 link->aspm_disable = ASPM_STATE_ALL;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900621 break;
622 }
623 }
Kenji Kaneshigeb127bd52009-08-19 10:57:31 +0900624
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900625 /* Get and check endpoint acceptable latencies */
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900626 list_for_each_entry(child, &linkbus->devices, bus_list) {
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900627 u32 reg32, encoding;
Kenji Kaneshigeb6c2e542009-05-13 12:14:58 +0900628 struct aspm_latency *acceptable =
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900629 &link->acceptable[PCI_FUNC(child->devfn)];
Shaohua Li7d715a62008-02-25 09:46:41 +0800630
Yijing Wang62f87c02012-07-24 17:20:03 +0800631 if (pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT &&
632 pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END)
Shaohua Li7d715a62008-02-25 09:46:41 +0800633 continue;
634
Jiang Liuf12eb722012-07-24 17:20:12 +0800635 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900636 /* Calculate endpoint L0s acceptable latency */
Kenji Kaneshige5e0eaa72009-05-13 12:21:48 +0900637 encoding = (reg32 & PCI_EXP_DEVCAP_L0S) >> 6;
638 acceptable->l0s = calc_l0s_acceptable(encoding);
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900639 /* Calculate endpoint L1 acceptable latency */
640 encoding = (reg32 & PCI_EXP_DEVCAP_L1) >> 9;
641 acceptable->l1 = calc_l1_acceptable(encoding);
642
643 pcie_aspm_check_latency(child);
Shaohua Li7d715a62008-02-25 09:46:41 +0800644 }
645}
646
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800647static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos,
648 u32 clear, u32 set)
649{
650 u32 val;
651
652 pci_read_config_dword(pdev, pos, &val);
653 val &= ~clear;
654 val |= set;
655 pci_write_config_dword(pdev, pos, val);
656}
657
658/* Configure the ASPM L1 substates */
659static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state)
660{
661 u32 val, enable_req;
662 struct pci_dev *child = link->downstream, *parent = link->pdev;
663 u32 up_cap_ptr = link->l1ss.up_cap_ptr;
664 u32 dw_cap_ptr = link->l1ss.dw_cap_ptr;
665
666 enable_req = (link->aspm_enabled ^ state) & state;
667
668 /*
669 * Here are the rules specified in the PCIe spec for enabling L1SS:
670 * - When enabling L1.x, enable bit at parent first, then at child
671 * - When disabling L1.x, disable bit at child first, then at parent
672 * - When enabling ASPM L1.x, need to disable L1
673 * (at child followed by parent).
674 * - The ASPM/PCIPM L1.2 must be disabled while programming timing
675 * parameters
676 *
677 * To keep it simple, disable all L1SS bits first, and later enable
678 * what is needed.
679 */
680
681 /* Disable all L1 substates */
682 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
683 PCI_L1SS_CTL1_L1SS_MASK, 0);
684 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
685 PCI_L1SS_CTL1_L1SS_MASK, 0);
686 /*
687 * If needed, disable L1, and it gets enabled later
688 * in pcie_config_aspm_link().
689 */
690 if (enable_req & (ASPM_STATE_L1_1 | ASPM_STATE_L1_2)) {
691 pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
692 PCI_EXP_LNKCTL_ASPM_L1, 0);
693 pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
694 PCI_EXP_LNKCTL_ASPM_L1, 0);
695 }
696
697 if (enable_req & ASPM_STATE_L1_2_MASK) {
698
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600699 /* Program T_POWER_ON times in both ports */
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800700 pci_write_config_dword(parent, up_cap_ptr + PCI_L1SS_CTL2,
701 link->l1ss.ctl2);
702 pci_write_config_dword(child, dw_cap_ptr + PCI_L1SS_CTL2,
703 link->l1ss.ctl2);
704
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600705 /* Program Common_Mode_Restore_Time in upstream device */
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800706 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600707 PCI_L1SS_CTL1_CM_RESTORE_TIME,
708 link->l1ss.ctl1);
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800709
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600710 /* Program LTR_L1.2_THRESHOLD time in both ports */
Bjorn Helgaasc00054f2017-11-13 15:05:50 -0600711 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600712 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
713 PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
714 link->l1ss.ctl1);
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800715 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
Bjorn Helgaasa48f3d52017-11-13 08:36:40 -0600716 PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
717 PCI_L1SS_CTL1_LTR_L12_TH_SCALE,
718 link->l1ss.ctl1);
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800719 }
720
721 val = 0;
722 if (state & ASPM_STATE_L1_1)
723 val |= PCI_L1SS_CTL1_ASPM_L1_1;
724 if (state & ASPM_STATE_L1_2)
725 val |= PCI_L1SS_CTL1_ASPM_L1_2;
726 if (state & ASPM_STATE_L1_1_PCIPM)
727 val |= PCI_L1SS_CTL1_PCIPM_L1_1;
728 if (state & ASPM_STATE_L1_2_PCIPM)
729 val |= PCI_L1SS_CTL1_PCIPM_L1_2;
730
731 /* Enable what we need to enable */
732 pci_clear_and_set_dword(parent, up_cap_ptr + PCI_L1SS_CTL1,
733 PCI_L1SS_CAP_L1_PM_SS, val);
734 pci_clear_and_set_dword(child, dw_cap_ptr + PCI_L1SS_CTL1,
735 PCI_L1SS_CAP_L1_PM_SS, val);
736}
737
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900738static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
Shaohua Li7d715a62008-02-25 09:46:41 +0800739{
Bjorn Helgaas75083202012-12-05 13:51:19 -0700740 pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
741 PCI_EXP_LNKCTL_ASPMC, val);
Shaohua Li7d715a62008-02-25 09:46:41 +0800742}
743
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900744static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800745{
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900746 u32 upstream = 0, dwstream = 0;
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800747 struct pci_dev *child = link->downstream, *parent = link->pdev;
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900748 struct pci_bus *linkbus = parent->subordinate;
Shaohua Li7d715a62008-02-25 09:46:41 +0800749
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800750 /* Enable only the states that were not explicitly disabled */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900751 state &= (link->aspm_capable & ~link->aspm_disable);
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800752
753 /* Can't enable any substates if L1 is not enabled */
754 if (!(state & ASPM_STATE_L1))
755 state &= ~ASPM_STATE_L1SS;
756
757 /* Spec says both ports must be in D0 before enabling PCI PM substates*/
758 if (parent->current_state != PCI_D0 || child->current_state != PCI_D0) {
759 state &= ~ASPM_STATE_L1_SS_PCIPM;
760 state |= (link->aspm_enabled & ASPM_STATE_L1_SS_PCIPM);
761 }
762
763 /* Nothing to do if the link is already in the requested state */
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +0900764 if (link->aspm_enabled == state)
765 return;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900766 /* Convert ASPM state to upstream/downstream ASPM register state */
767 if (state & ASPM_STATE_L0S_UP)
Bjorn Helgaas75083202012-12-05 13:51:19 -0700768 dwstream |= PCI_EXP_LNKCTL_ASPM_L0S;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900769 if (state & ASPM_STATE_L0S_DW)
Bjorn Helgaas75083202012-12-05 13:51:19 -0700770 upstream |= PCI_EXP_LNKCTL_ASPM_L0S;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900771 if (state & ASPM_STATE_L1) {
Bjorn Helgaas75083202012-12-05 13:51:19 -0700772 upstream |= PCI_EXP_LNKCTL_ASPM_L1;
773 dwstream |= PCI_EXP_LNKCTL_ASPM_L1;
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900774 }
Rajat Jainaeda9ad2017-01-02 22:34:14 -0800775
776 if (link->aspm_capable & ASPM_STATE_L1SS)
777 pcie_config_aspm_l1ss(link, state);
778
Shaohua Li7d715a62008-02-25 09:46:41 +0800779 /*
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900780 * Spec 2.0 suggests all functions should be configured the
781 * same setting for ASPM. Enabling ASPM L1 should be done in
782 * upstream component first and then downstream, and vice
783 * versa for disabling ASPM L1. Spec doesn't mention L0S.
Shaohua Li7d715a62008-02-25 09:46:41 +0800784 */
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900785 if (state & ASPM_STATE_L1)
786 pcie_config_aspm_dev(parent, upstream);
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900787 list_for_each_entry(child, &linkbus->devices, bus_list)
Kenji Kaneshigeac180182009-08-19 11:02:13 +0900788 pcie_config_aspm_dev(child, dwstream);
789 if (!(state & ASPM_STATE_L1))
790 pcie_config_aspm_dev(parent, upstream);
Shaohua Li7d715a62008-02-25 09:46:41 +0800791
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900792 link->aspm_enabled = state;
Shaohua Li7d715a62008-02-25 09:46:41 +0800793}
794
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900795static void pcie_config_aspm_path(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800796{
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900797 while (link) {
798 pcie_config_aspm_link(link, policy_to_aspm_state(link));
799 link = link->parent;
Shaohua Li46bbdfa2008-12-19 09:27:42 +0800800 }
Shaohua Li7d715a62008-02-25 09:46:41 +0800801}
802
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900803static void free_link_state(struct pcie_link_state *link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800804{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +0900805 link->pdev->link_state = NULL;
806 kfree(link);
Shaohua Li7d715a62008-02-25 09:46:41 +0800807}
808
Shaohua Liddc97532008-05-21 16:58:40 +0800809static int pcie_aspm_sanity_check(struct pci_dev *pdev)
810{
Kenji Kaneshige36475842009-05-13 12:23:09 +0900811 struct pci_dev *child;
Shaohua Li149e1632008-07-23 10:32:31 +0800812 u32 reg32;
Matthew Garrett2f671e22010-12-06 14:00:56 -0500813
Shaohua Liddc97532008-05-21 16:58:40 +0800814 /*
Stefan Assmann45e829e2009-12-03 06:49:24 -0500815 * Some functions in a slot might not all be PCIe functions,
Kenji Kaneshige36475842009-05-13 12:23:09 +0900816 * very strange. Disable ASPM for the whole slot
Shaohua Liddc97532008-05-21 16:58:40 +0800817 */
Kenji Kaneshige36475842009-05-13 12:23:09 +0900818 list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
Jiang Liuf12eb722012-07-24 17:20:12 +0800819 if (!pci_is_pcie(child))
Shaohua Liddc97532008-05-21 16:58:40 +0800820 return -EINVAL;
Matthew Garrettc9651e72012-03-27 10:17:41 -0400821
822 /*
823 * If ASPM is disabled then we're not going to change
824 * the BIOS state. It's safe to continue even if it's a
825 * pre-1.1 device
826 */
827
828 if (aspm_disabled)
829 continue;
830
Shaohua Li149e1632008-07-23 10:32:31 +0800831 /*
832 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
833 * RBER bit to determine if a function is 1.1 version device
834 */
Jiang Liuf12eb722012-07-24 17:20:12 +0800835 pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
Sitsofe Wheelere1f4f592008-09-16 14:27:13 +0100836 if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600837 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 +0800838 return -EINVAL;
839 }
Shaohua Liddc97532008-05-21 16:58:40 +0800840 }
841 return 0;
842}
843
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900844static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900845{
846 struct pcie_link_state *link;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900847
848 link = kzalloc(sizeof(*link), GFP_KERNEL);
849 if (!link)
850 return NULL;
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600851
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900852 INIT_LIST_HEAD(&link->sibling);
853 INIT_LIST_HEAD(&link->children);
854 INIT_LIST_HEAD(&link->link);
855 link->pdev = pdev;
Yinghai Lu3bd7db62017-03-01 00:25:40 -0800856 link->downstream = pci_function_0(pdev->subordinate);
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600857
858 /*
859 * Root Ports and PCI/PCI-X to PCIe Bridges are roots of PCIe
Ard Biesheuvelee8bdfb2017-10-02 15:08:40 +0100860 * hierarchies. Note that some PCIe host implementations omit
861 * the root ports entirely, in which case a downstream port on
862 * a switch may become the root of the link state chain for all
863 * its subordinate endpoints.
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600864 */
865 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
Ard Biesheuvelee8bdfb2017-10-02 15:08:40 +0100866 pci_pcie_type(pdev) == PCI_EXP_TYPE_PCIE_BRIDGE ||
867 !pdev->bus->parent->self) {
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600868 link->root = link;
869 } else {
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900870 struct pcie_link_state *parent;
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600871
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900872 parent = pdev->bus->parent->self->link_state;
873 if (!parent) {
874 kfree(link);
875 return NULL;
876 }
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600877
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900878 link->parent = parent;
Bjorn Helgaas030305d2017-01-27 15:00:45 -0600879 link->root = link->parent->root;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900880 list_add(&link->link, &parent->children);
881 }
Kenji Kaneshige5c92ffb2009-05-13 12:23:57 +0900882
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900883 list_add(&link->sibling, &link_list);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900884 pdev->link_state = link;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900885 return link;
886}
887
Shaohua Li7d715a62008-02-25 09:46:41 +0800888/*
889 * pcie_aspm_init_link_state: Initiate PCI express link state.
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700890 * It is called after the pcie and its children devices are scanned.
Shaohua Li7d715a62008-02-25 09:46:41 +0800891 * @pdev: the root port or switch downstream port
892 */
893void pcie_aspm_init_link_state(struct pci_dev *pdev)
894{
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900895 struct pcie_link_state *link;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900896 int blacklist = !!pcie_aspm_sanity_check(pdev);
Shaohua Li7d715a62008-02-25 09:46:41 +0800897
Patrick Talbert17c91482018-09-05 09:12:53 +0200898 if (!aspm_support_enabled || aspm_disabled)
Joe Lawrencea26d5ec2013-01-15 15:31:28 -0500899 return;
900
Yijing Wangc8fc9332015-05-21 15:05:03 +0800901 if (pdev->link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800902 return;
Yijing Wangc8fc9332015-05-21 15:05:03 +0800903
904 /*
905 * We allocate pcie_link_state for the component on the upstream
906 * end of a Link, so there's nothing to do unless this device has a
907 * Link on its secondary side.
908 */
909 if (!pdev->has_secondary_link)
Shaohua Li7d715a62008-02-25 09:46:41 +0800910 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900911
Shaohua Li8e822df2009-06-08 09:27:25 +0800912 /* VIA has a strange chipset, root port is under a bridge */
Yijing Wang62f87c02012-07-24 17:20:03 +0800913 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT &&
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900914 pdev->bus->self)
Shaohua Li8e822df2009-06-08 09:27:25 +0800915 return;
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900916
Shaohua Li7d715a62008-02-25 09:46:41 +0800917 down_read(&pci_bus_sem);
918 if (list_empty(&pdev->subordinate->devices))
919 goto out;
920
Shaohua Li7d715a62008-02-25 09:46:41 +0800921 mutex_lock(&aspm_lock);
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900922 link = alloc_pcie_link_state(pdev);
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900923 if (!link)
924 goto unlock;
925 /*
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900926 * Setup initial ASPM state. Note that we need to configure
927 * upstream links also because capable state of them can be
928 * update through pcie_aspm_cap_init().
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900929 */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900930 pcie_aspm_cap_init(link, blacklist);
Shaohua Li7d715a62008-02-25 09:46:41 +0800931
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900932 /* Setup initial Clock PM state */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900933 pcie_clkpm_cap_init(link, blacklist);
Matthew Garrett41cd7662010-06-09 16:05:07 -0400934
935 /*
936 * At this stage drivers haven't had an opportunity to change the
937 * link policy setting. Enabling ASPM on broken hardware can cripple
938 * it even before the driver has had a chance to disable ASPM, so
939 * default to a safe level right now. If we're enabling ASPM beyond
940 * the BIOS's expectation, we'll do so once pci_enable_device() is
941 * called.
942 */
Rajat Jainb2103cc2017-01-02 22:34:11 -0800943 if (aspm_policy != POLICY_POWERSAVE &&
944 aspm_policy != POLICY_POWER_SUPERSAVE) {
Matthew Garrett41cd7662010-06-09 16:05:07 -0400945 pcie_config_aspm_path(link);
946 pcie_set_clkpm(link, policy_to_clkpm_state(link));
947 }
948
Kenji Kaneshige8d349ac2009-05-13 12:18:22 +0900949unlock:
Shaohua Li7d715a62008-02-25 09:46:41 +0800950 mutex_unlock(&aspm_lock);
951out:
952 up_read(&pci_bus_sem);
953}
954
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900955/* Recheck latencies and update aspm_capable for links under the root */
956static void pcie_update_aspm_capable(struct pcie_link_state *root)
957{
958 struct pcie_link_state *link;
959 BUG_ON(root->parent);
960 list_for_each_entry(link, &link_list, sibling) {
961 if (link->root != root)
962 continue;
963 link->aspm_capable = link->aspm_support;
964 }
965 list_for_each_entry(link, &link_list, sibling) {
966 struct pci_dev *child;
967 struct pci_bus *linkbus = link->pdev->subordinate;
968 if (link->root != root)
969 continue;
970 list_for_each_entry(child, &linkbus->devices, bus_list) {
Yijing Wang62f87c02012-07-24 17:20:03 +0800971 if ((pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT) &&
972 (pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END))
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900973 continue;
974 pcie_aspm_check_latency(child);
975 }
976 }
977}
978
Shaohua Li7d715a62008-02-25 09:46:41 +0800979/* @pdev: the endpoint device */
980void pcie_aspm_exit_link_state(struct pci_dev *pdev)
981{
982 struct pci_dev *parent = pdev->bus->self;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900983 struct pcie_link_state *link, *root, *parent_link;
Shaohua Li7d715a62008-02-25 09:46:41 +0800984
Myron Stowe84fb9132013-01-31 16:29:25 -0700985 if (!parent || !parent->link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +0800986 return;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900987
Shaohua Li7d715a62008-02-25 09:46:41 +0800988 down_read(&pci_bus_sem);
989 mutex_lock(&aspm_lock);
Shaohua Li7d715a62008-02-25 09:46:41 +0800990 /*
991 * All PCIe functions are in one slot, remove one function will remove
Alex Chiang3419c752009-01-28 14:59:18 -0700992 * the whole slot, so just wait until we are the last function left.
Shaohua Li7d715a62008-02-25 09:46:41 +0800993 */
Lukas Wunneraeae4f32018-09-04 12:34:18 -0500994 if (!list_empty(&parent->subordinate->devices))
Shaohua Li7d715a62008-02-25 09:46:41 +0800995 goto out;
996
Kenji Kaneshigefc87e912009-08-19 10:58:46 +0900997 link = parent->link_state;
Kenji Kaneshige07d92762009-08-19 11:00:25 +0900998 root = link->root;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +0900999 parent_link = link->parent;
Kenji Kaneshigefc87e912009-08-19 10:58:46 +09001000
Shaohua Li7d715a62008-02-25 09:46:41 +08001001 /* All functions are removed, so just disable ASPM for the link */
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001002 pcie_config_aspm_link(link, 0);
Kenji Kaneshigefc87e912009-08-19 10:58:46 +09001003 list_del(&link->sibling);
1004 list_del(&link->link);
Shaohua Li7d715a62008-02-25 09:46:41 +08001005 /* Clock PM is for endpoint device */
Kenji Kaneshigefc87e912009-08-19 10:58:46 +09001006 free_link_state(link);
Kenji Kaneshige07d92762009-08-19 11:00:25 +09001007
1008 /* Recheck latencies and configure upstream links */
Kenji Kaneshigeb26a34a2009-11-06 11:25:13 +09001009 if (parent_link) {
1010 pcie_update_aspm_capable(root);
1011 pcie_config_aspm_path(parent_link);
1012 }
Shaohua Li7d715a62008-02-25 09:46:41 +08001013out:
1014 mutex_unlock(&aspm_lock);
1015 up_read(&pci_bus_sem);
1016}
1017
1018/* @pdev: the root port or switch downstream port */
1019void pcie_aspm_pm_state_change(struct pci_dev *pdev)
1020{
Kenji Kaneshige07d92762009-08-19 11:00:25 +09001021 struct pcie_link_state *link = pdev->link_state;
Shaohua Li7d715a62008-02-25 09:46:41 +08001022
Yijing Wangf9b8cd72015-05-19 11:41:34 +08001023 if (aspm_disabled || !link)
Shaohua Li7d715a62008-02-25 09:46:41 +08001024 return;
1025 /*
Kenji Kaneshige07d92762009-08-19 11:00:25 +09001026 * Devices changed PM state, we should recheck if latency
1027 * meets all functions' requirement
Shaohua Li7d715a62008-02-25 09:46:41 +08001028 */
Kenji Kaneshige07d92762009-08-19 11:00:25 +09001029 down_read(&pci_bus_sem);
1030 mutex_lock(&aspm_lock);
1031 pcie_update_aspm_capable(link->root);
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001032 pcie_config_aspm_path(link);
Kenji Kaneshige07d92762009-08-19 11:00:25 +09001033 mutex_unlock(&aspm_lock);
1034 up_read(&pci_bus_sem);
Shaohua Li7d715a62008-02-25 09:46:41 +08001035}
1036
Naga Chumbalkar1a680b72011-03-21 03:29:08 +00001037void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
1038{
1039 struct pcie_link_state *link = pdev->link_state;
1040
Yijing Wangf9b8cd72015-05-19 11:41:34 +08001041 if (aspm_disabled || !link)
Naga Chumbalkar1a680b72011-03-21 03:29:08 +00001042 return;
1043
Rajat Jainb2103cc2017-01-02 22:34:11 -08001044 if (aspm_policy != POLICY_POWERSAVE &&
1045 aspm_policy != POLICY_POWER_SUPERSAVE)
Naga Chumbalkar1a680b72011-03-21 03:29:08 +00001046 return;
1047
Naga Chumbalkar1a680b72011-03-21 03:29:08 +00001048 down_read(&pci_bus_sem);
1049 mutex_lock(&aspm_lock);
1050 pcie_config_aspm_path(link);
1051 pcie_set_clkpm(link, policy_to_clkpm_state(link));
1052 mutex_unlock(&aspm_lock);
1053 up_read(&pci_bus_sem);
1054}
1055
Bjorn Helgaase127a042015-05-20 12:13:05 -05001056static void __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
Shaohua Li7d715a62008-02-25 09:46:41 +08001057{
1058 struct pci_dev *parent = pdev->bus->self;
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +09001059 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +08001060
Matthew Garrett3c076352011-11-10 16:38:33 -05001061 if (!pci_is_pcie(pdev))
1062 return;
1063
Yijing Wangc8fc9332015-05-21 15:05:03 +08001064 if (pdev->has_secondary_link)
Shaohua Li7d715a62008-02-25 09:46:41 +08001065 parent = pdev;
1066 if (!parent || !parent->link_state)
1067 return;
1068
Bjorn Helgaas2add0ec2013-05-21 10:56:51 -06001069 /*
1070 * A driver requested that ASPM be disabled on this device, but
1071 * if we don't have permission to manage ASPM (e.g., on ACPI
1072 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
1073 * the _OSC method), we can't honor that request. Windows has
1074 * a similar mechanism using "PciASPMOptOut", which is also
1075 * ignored in this situation.
1076 */
Bjorn Helgaase127a042015-05-20 12:13:05 -05001077 if (aspm_disabled) {
Frederick Lawler7506dc72018-01-18 12:55:24 -06001078 pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n");
Bjorn Helgaas2add0ec2013-05-21 10:56:51 -06001079 return;
1080 }
1081
Yinghai Lu9f728f52011-05-12 17:11:47 -07001082 if (sem)
1083 down_read(&pci_bus_sem);
Shaohua Li7d715a62008-02-25 09:46:41 +08001084 mutex_lock(&aspm_lock);
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +09001085 link = parent->link_state;
Kenji Kaneshigeac180182009-08-19 11:02:13 +09001086 if (state & PCIE_LINK_STATE_L0S)
1087 link->aspm_disable |= ASPM_STATE_L0S;
1088 if (state & PCIE_LINK_STATE_L1)
1089 link->aspm_disable |= ASPM_STATE_L1;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001090 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1091
Kenji Kaneshige430842e2009-05-13 12:20:10 +09001092 if (state & PCIE_LINK_STATE_CLKPM) {
Kenji Kaneshigef1c0ca22009-08-19 10:59:52 +09001093 link->clkpm_capable = 0;
1094 pcie_set_clkpm(link, 0);
Kenji Kaneshige430842e2009-05-13 12:20:10 +09001095 }
Shaohua Li7d715a62008-02-25 09:46:41 +08001096 mutex_unlock(&aspm_lock);
Yinghai Lu9f728f52011-05-12 17:11:47 -07001097 if (sem)
1098 up_read(&pci_bus_sem);
1099}
1100
1101void pci_disable_link_state_locked(struct pci_dev *pdev, int state)
1102{
Bjorn Helgaase127a042015-05-20 12:13:05 -05001103 __pci_disable_link_state(pdev, state, false);
Yinghai Lu9f728f52011-05-12 17:11:47 -07001104}
1105EXPORT_SYMBOL(pci_disable_link_state_locked);
1106
Yijing Wang2dfca872013-05-28 16:03:22 +08001107/**
1108 * pci_disable_link_state - Disable device's link state, so the link will
1109 * never enter specific states. Note that if the BIOS didn't grant ASPM
1110 * control to the OS, this does nothing because we can't touch the LNKCTL
1111 * register.
1112 *
1113 * @pdev: PCI device
1114 * @state: ASPM link state to disable
1115 */
Yinghai Lu9f728f52011-05-12 17:11:47 -07001116void pci_disable_link_state(struct pci_dev *pdev, int state)
1117{
Bjorn Helgaase127a042015-05-20 12:13:05 -05001118 __pci_disable_link_state(pdev, state, true);
Shaohua Li7d715a62008-02-25 09:46:41 +08001119}
1120EXPORT_SYMBOL(pci_disable_link_state);
1121
Kees Cooke4dca7b2017-10-17 19:04:42 -07001122static int pcie_aspm_set_policy(const char *val,
1123 const struct kernel_param *kp)
Shaohua Li7d715a62008-02-25 09:46:41 +08001124{
1125 int i;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001126 struct pcie_link_state *link;
Shaohua Li7d715a62008-02-25 09:46:41 +08001127
Naga Chumbalkarbbfa3062011-03-21 03:29:14 +00001128 if (aspm_disabled)
1129 return -EPERM;
Andy Shevchenko36131ce2018-08-06 14:30:34 -05001130 i = sysfs_match_string(policy_str, val);
1131 if (i < 0)
1132 return i;
Shaohua Li7d715a62008-02-25 09:46:41 +08001133 if (i == aspm_policy)
1134 return 0;
1135
1136 down_read(&pci_bus_sem);
1137 mutex_lock(&aspm_lock);
1138 aspm_policy = i;
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001139 list_for_each_entry(link, &link_list, sibling) {
1140 pcie_config_aspm_link(link, policy_to_aspm_state(link));
1141 pcie_set_clkpm(link, policy_to_clkpm_state(link));
Shaohua Li7d715a62008-02-25 09:46:41 +08001142 }
1143 mutex_unlock(&aspm_lock);
1144 up_read(&pci_bus_sem);
1145 return 0;
1146}
1147
Kees Cooke4dca7b2017-10-17 19:04:42 -07001148static int pcie_aspm_get_policy(char *buffer, const struct kernel_param *kp)
Shaohua Li7d715a62008-02-25 09:46:41 +08001149{
1150 int i, cnt = 0;
1151 for (i = 0; i < ARRAY_SIZE(policy_str); i++)
1152 if (i == aspm_policy)
1153 cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
1154 else
1155 cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
1156 return cnt;
1157}
1158
1159module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
1160 NULL, 0644);
1161
1162#ifdef CONFIG_PCIEASPM_DEBUG
1163static ssize_t link_state_show(struct device *dev,
1164 struct device_attribute *attr,
1165 char *buf)
1166{
1167 struct pci_dev *pci_device = to_pci_dev(dev);
1168 struct pcie_link_state *link_state = pci_device->link_state;
1169
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +09001170 return sprintf(buf, "%d\n", link_state->aspm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +08001171}
1172
1173static ssize_t link_state_store(struct device *dev,
1174 struct device_attribute *attr,
1175 const char *buf,
1176 size_t n)
1177{
Kenji Kaneshige5aa63582009-05-13 12:17:44 +09001178 struct pci_dev *pdev = to_pci_dev(dev);
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001179 struct pcie_link_state *link, *root = pdev->link_state->root;
Andy Lutomirski57d86a02015-11-19 08:05:35 -08001180 u32 state;
Shaohua Li7d715a62008-02-25 09:46:41 +08001181
Naga Chumbalkarbbfa3062011-03-21 03:29:14 +00001182 if (aspm_disabled)
1183 return -EPERM;
Shaohua Li7d715a62008-02-25 09:46:41 +08001184
Andy Lutomirski57d86a02015-11-19 08:05:35 -08001185 if (kstrtouint(buf, 10, &state))
1186 return -EINVAL;
1187 if ((state & ~ASPM_STATE_ALL) != 0)
1188 return -EINVAL;
Kenji Kaneshigeac180182009-08-19 11:02:13 +09001189
Kenji Kaneshigeb7206cb2009-08-19 11:01:37 +09001190 down_read(&pci_bus_sem);
1191 mutex_lock(&aspm_lock);
1192 list_for_each_entry(link, &link_list, sibling) {
1193 if (link->root != root)
1194 continue;
1195 pcie_config_aspm_link(link, state);
1196 }
1197 mutex_unlock(&aspm_lock);
1198 up_read(&pci_bus_sem);
1199 return n;
Shaohua Li7d715a62008-02-25 09:46:41 +08001200}
1201
1202static ssize_t clk_ctl_show(struct device *dev,
1203 struct device_attribute *attr,
1204 char *buf)
1205{
1206 struct pci_dev *pci_device = to_pci_dev(dev);
1207 struct pcie_link_state *link_state = pci_device->link_state;
1208
Kenji Kaneshige4d246e42009-05-13 12:15:38 +09001209 return sprintf(buf, "%d\n", link_state->clkpm_enabled);
Shaohua Li7d715a62008-02-25 09:46:41 +08001210}
1211
1212static ssize_t clk_ctl_store(struct device *dev,
1213 struct device_attribute *attr,
1214 const char *buf,
1215 size_t n)
1216{
Kenji Kaneshige430842e2009-05-13 12:20:10 +09001217 struct pci_dev *pdev = to_pci_dev(dev);
Chris J Arges94a90312014-12-05 17:02:42 -06001218 bool state;
Shaohua Li7d715a62008-02-25 09:46:41 +08001219
Chris J Arges94a90312014-12-05 17:02:42 -06001220 if (strtobool(buf, &state))
Shaohua Li7d715a62008-02-25 09:46:41 +08001221 return -EINVAL;
Shaohua Li7d715a62008-02-25 09:46:41 +08001222
1223 down_read(&pci_bus_sem);
1224 mutex_lock(&aspm_lock);
Chris J Arges94a90312014-12-05 17:02:42 -06001225 pcie_set_clkpm_nocheck(pdev->link_state, state);
Shaohua Li7d715a62008-02-25 09:46:41 +08001226 mutex_unlock(&aspm_lock);
1227 up_read(&pci_bus_sem);
1228
1229 return n;
1230}
1231
Julia Lawallfc4f57f2016-10-29 21:37:07 +02001232static DEVICE_ATTR_RW(link_state);
1233static DEVICE_ATTR_RW(clk_ctl);
Shaohua Li7d715a62008-02-25 09:46:41 +08001234
1235static char power_group[] = "power";
1236void pcie_aspm_create_sysfs_dev_files(struct pci_dev *pdev)
1237{
1238 struct pcie_link_state *link_state = pdev->link_state;
1239
Yijing Wangf9b8cd72015-05-19 11:41:34 +08001240 if (!link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +08001241 return;
1242
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +09001243 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +08001244 sysfs_add_file_to_group(&pdev->dev.kobj,
1245 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +09001246 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +08001247 sysfs_add_file_to_group(&pdev->dev.kobj,
1248 &dev_attr_clk_ctl.attr, power_group);
1249}
1250
1251void pcie_aspm_remove_sysfs_dev_files(struct pci_dev *pdev)
1252{
1253 struct pcie_link_state *link_state = pdev->link_state;
1254
Yijing Wangf9b8cd72015-05-19 11:41:34 +08001255 if (!link_state)
Shaohua Li7d715a62008-02-25 09:46:41 +08001256 return;
1257
Kenji Kaneshige80bfdbe2009-05-13 12:12:43 +09001258 if (link_state->aspm_support)
Shaohua Li7d715a62008-02-25 09:46:41 +08001259 sysfs_remove_file_from_group(&pdev->dev.kobj,
1260 &dev_attr_link_state.attr, power_group);
Kenji Kaneshige4d246e42009-05-13 12:15:38 +09001261 if (link_state->clkpm_capable)
Shaohua Li7d715a62008-02-25 09:46:41 +08001262 sysfs_remove_file_from_group(&pdev->dev.kobj,
1263 &dev_attr_clk_ctl.attr, power_group);
1264}
1265#endif
1266
1267static int __init pcie_aspm_disable(char *str)
1268{
Shaohua Lid6d38572008-07-23 10:32:42 +08001269 if (!strcmp(str, "off")) {
Matthew Garrett3c076352011-11-10 16:38:33 -05001270 aspm_policy = POLICY_DEFAULT;
Shaohua Lid6d38572008-07-23 10:32:42 +08001271 aspm_disabled = 1;
Rafael J. Wysocki8b8bae92011-03-05 13:21:51 +01001272 aspm_support_enabled = false;
Shaohua Lid6d38572008-07-23 10:32:42 +08001273 printk(KERN_INFO "PCIe ASPM is disabled\n");
1274 } else if (!strcmp(str, "force")) {
1275 aspm_force = 1;
Michael Witten8072ba12011-06-28 06:15:05 +00001276 printk(KERN_INFO "PCIe ASPM is forcibly enabled\n");
Shaohua Lid6d38572008-07-23 10:32:42 +08001277 }
Shaohua Li7d715a62008-02-25 09:46:41 +08001278 return 1;
1279}
1280
Shaohua Lid6d38572008-07-23 10:32:42 +08001281__setup("pcie_aspm=", pcie_aspm_disable);
Shaohua Li7d715a62008-02-25 09:46:41 +08001282
Shaohua Li5fde2442008-07-23 10:32:24 +08001283void pcie_no_aspm(void)
1284{
Matthew Garrett3c076352011-11-10 16:38:33 -05001285 /*
1286 * Disabling ASPM is intended to prevent the kernel from modifying
1287 * existing hardware state, not to clear existing state. To that end:
1288 * (a) set policy to POLICY_DEFAULT in order to avoid changing state
1289 * (b) prevent userspace from changing policy
1290 */
1291 if (!aspm_force) {
1292 aspm_policy = POLICY_DEFAULT;
Shaohua Lid6d38572008-07-23 10:32:42 +08001293 aspm_disabled = 1;
Matthew Garrett3c076352011-11-10 16:38:33 -05001294 }
Shaohua Li5fde2442008-07-23 10:32:24 +08001295}
1296
Rafael J. Wysocki8b8bae92011-03-05 13:21:51 +01001297bool pcie_aspm_support_enabled(void)
1298{
1299 return aspm_support_enabled;
1300}
1301EXPORT_SYMBOL(pcie_aspm_support_enabled);