xref: /openbmc/linux/drivers/pci/pcie/aspm.c (revision 911afb9f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Enable PCIe link L0s/L1 state and Clock Power Management
4  *
5  * Copyright (C) 2007 Intel
6  * Copyright (C) Zhang Yanmin (yanmin.zhang@intel.com)
7  * Copyright (C) Shaohua Li (shaohua.li@intel.com)
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/math.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/pci.h>
15 #include <linux/pci_regs.h>
16 #include <linux/errno.h>
17 #include <linux/pm.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/jiffies.h>
21 #include <linux/delay.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 
29 /* 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 */
33 #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)
41 #define ASPM_STATE_L0S		(ASPM_STATE_L0S_UP | ASPM_STATE_L0S_DW)
42 #define ASPM_STATE_ALL		(ASPM_STATE_L0S | ASPM_STATE_L1 |	\
43 				 ASPM_STATE_L1SS)
44 
45 struct pcie_link_state {
46 	struct pci_dev *pdev;		/* Upstream component of the Link */
47 	struct pci_dev *downstream;	/* Downstream component, function 0 */
48 	struct pcie_link_state *root;	/* pointer to the root port link */
49 	struct pcie_link_state *parent;	/* pointer to the parent Link state */
50 	struct list_head sibling;	/* node in link_list */
51 
52 	/* ASPM state */
53 	u32 aspm_support:7;		/* Supported ASPM state */
54 	u32 aspm_enabled:7;		/* Enabled ASPM state */
55 	u32 aspm_capable:7;		/* Capable ASPM state with latency */
56 	u32 aspm_default:7;		/* Default ASPM state by BIOS */
57 	u32 aspm_disable:7;		/* Disabled ASPM state */
58 
59 	/* Clock PM state */
60 	u32 clkpm_capable:1;		/* Clock PM capable? */
61 	u32 clkpm_enabled:1;		/* Current Clock PM state */
62 	u32 clkpm_default:1;		/* Default Clock PM state by BIOS */
63 	u32 clkpm_disable:1;		/* Clock PM disabled */
64 };
65 
66 static int aspm_disabled, aspm_force;
67 static bool aspm_support_enabled = true;
68 static DEFINE_MUTEX(aspm_lock);
69 static LIST_HEAD(link_list);
70 
71 #define POLICY_DEFAULT 0	/* BIOS default setting */
72 #define POLICY_PERFORMANCE 1	/* high performance */
73 #define POLICY_POWERSAVE 2	/* high power saving */
74 #define POLICY_POWER_SUPERSAVE 3 /* possibly even more power saving */
75 
76 #ifdef CONFIG_PCIEASPM_PERFORMANCE
77 static int aspm_policy = POLICY_PERFORMANCE;
78 #elif defined CONFIG_PCIEASPM_POWERSAVE
79 static int aspm_policy = POLICY_POWERSAVE;
80 #elif defined CONFIG_PCIEASPM_POWER_SUPERSAVE
81 static int aspm_policy = POLICY_POWER_SUPERSAVE;
82 #else
83 static int aspm_policy;
84 #endif
85 
86 static const char *policy_str[] = {
87 	[POLICY_DEFAULT] = "default",
88 	[POLICY_PERFORMANCE] = "performance",
89 	[POLICY_POWERSAVE] = "powersave",
90 	[POLICY_POWER_SUPERSAVE] = "powersupersave"
91 };
92 
93 #define LINK_RETRAIN_TIMEOUT HZ
94 
95 /*
96  * The L1 PM substate capability is only implemented in function 0 in a
97  * multi function device.
98  */
99 static struct pci_dev *pci_function_0(struct pci_bus *linkbus)
100 {
101 	struct pci_dev *child;
102 
103 	list_for_each_entry(child, &linkbus->devices, bus_list)
104 		if (PCI_FUNC(child->devfn) == 0)
105 			return child;
106 	return NULL;
107 }
108 
109 static int policy_to_aspm_state(struct pcie_link_state *link)
110 {
111 	switch (aspm_policy) {
112 	case POLICY_PERFORMANCE:
113 		/* Disable ASPM and Clock PM */
114 		return 0;
115 	case POLICY_POWERSAVE:
116 		/* Enable ASPM L0s/L1 */
117 		return (ASPM_STATE_L0S | ASPM_STATE_L1);
118 	case POLICY_POWER_SUPERSAVE:
119 		/* Enable Everything */
120 		return ASPM_STATE_ALL;
121 	case POLICY_DEFAULT:
122 		return link->aspm_default;
123 	}
124 	return 0;
125 }
126 
127 static int policy_to_clkpm_state(struct pcie_link_state *link)
128 {
129 	switch (aspm_policy) {
130 	case POLICY_PERFORMANCE:
131 		/* Disable ASPM and Clock PM */
132 		return 0;
133 	case POLICY_POWERSAVE:
134 	case POLICY_POWER_SUPERSAVE:
135 		/* Enable Clock PM */
136 		return 1;
137 	case POLICY_DEFAULT:
138 		return link->clkpm_default;
139 	}
140 	return 0;
141 }
142 
143 static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
144 {
145 	struct pci_dev *child;
146 	struct pci_bus *linkbus = link->pdev->subordinate;
147 	u32 val = enable ? PCI_EXP_LNKCTL_CLKREQ_EN : 0;
148 
149 	list_for_each_entry(child, &linkbus->devices, bus_list)
150 		pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
151 						   PCI_EXP_LNKCTL_CLKREQ_EN,
152 						   val);
153 	link->clkpm_enabled = !!enable;
154 }
155 
156 static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
157 {
158 	/*
159 	 * Don't enable Clock PM if the link is not Clock PM capable
160 	 * or Clock PM is disabled
161 	 */
162 	if (!link->clkpm_capable || link->clkpm_disable)
163 		enable = 0;
164 	/* Need nothing if the specified equals to current state */
165 	if (link->clkpm_enabled == enable)
166 		return;
167 	pcie_set_clkpm_nocheck(link, enable);
168 }
169 
170 static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
171 {
172 	int capable = 1, enabled = 1;
173 	u32 reg32;
174 	u16 reg16;
175 	struct pci_dev *child;
176 	struct pci_bus *linkbus = link->pdev->subordinate;
177 
178 	/* All functions should have the same cap and state, take the worst */
179 	list_for_each_entry(child, &linkbus->devices, bus_list) {
180 		pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &reg32);
181 		if (!(reg32 & PCI_EXP_LNKCAP_CLKPM)) {
182 			capable = 0;
183 			enabled = 0;
184 			break;
185 		}
186 		pcie_capability_read_word(child, PCI_EXP_LNKCTL, &reg16);
187 		if (!(reg16 & PCI_EXP_LNKCTL_CLKREQ_EN))
188 			enabled = 0;
189 	}
190 	link->clkpm_enabled = enabled;
191 	link->clkpm_default = enabled;
192 	link->clkpm_capable = capable;
193 	link->clkpm_disable = blacklist ? 1 : 0;
194 }
195 
196 static bool pcie_retrain_link(struct pcie_link_state *link)
197 {
198 	struct pci_dev *parent = link->pdev;
199 	unsigned long end_jiffies;
200 	u16 reg16;
201 
202 	pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &reg16);
203 	reg16 |= PCI_EXP_LNKCTL_RL;
204 	pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
205 	if (parent->clear_retrain_link) {
206 		/*
207 		 * Due to an erratum in some devices the Retrain Link bit
208 		 * needs to be cleared again manually to allow the link
209 		 * training to succeed.
210 		 */
211 		reg16 &= ~PCI_EXP_LNKCTL_RL;
212 		pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
213 	}
214 
215 	/* Wait for link training end. Break out after waiting for timeout */
216 	end_jiffies = jiffies + LINK_RETRAIN_TIMEOUT;
217 	do {
218 		pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &reg16);
219 		if (!(reg16 & PCI_EXP_LNKSTA_LT))
220 			break;
221 		msleep(1);
222 	} while (time_before(jiffies, end_jiffies));
223 	return !(reg16 & PCI_EXP_LNKSTA_LT);
224 }
225 
226 /*
227  * pcie_aspm_configure_common_clock: check if the 2 ends of a link
228  *   could use common clock. If they are, configure them to use the
229  *   common clock. That will reduce the ASPM state exit latency.
230  */
231 static void pcie_aspm_configure_common_clock(struct pcie_link_state *link)
232 {
233 	int same_clock = 1;
234 	u16 reg16, parent_reg, child_reg[8];
235 	struct pci_dev *child, *parent = link->pdev;
236 	struct pci_bus *linkbus = parent->subordinate;
237 	/*
238 	 * All functions of a slot should have the same Slot Clock
239 	 * Configuration, so just check one function
240 	 */
241 	child = list_entry(linkbus->devices.next, struct pci_dev, bus_list);
242 	BUG_ON(!pci_is_pcie(child));
243 
244 	/* Check downstream component if bit Slot Clock Configuration is 1 */
245 	pcie_capability_read_word(child, PCI_EXP_LNKSTA, &reg16);
246 	if (!(reg16 & PCI_EXP_LNKSTA_SLC))
247 		same_clock = 0;
248 
249 	/* Check upstream component if bit Slot Clock Configuration is 1 */
250 	pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &reg16);
251 	if (!(reg16 & PCI_EXP_LNKSTA_SLC))
252 		same_clock = 0;
253 
254 	/* Port might be already in common clock mode */
255 	pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &reg16);
256 	if (same_clock && (reg16 & PCI_EXP_LNKCTL_CCC)) {
257 		bool consistent = true;
258 
259 		list_for_each_entry(child, &linkbus->devices, bus_list) {
260 			pcie_capability_read_word(child, PCI_EXP_LNKCTL,
261 						  &reg16);
262 			if (!(reg16 & PCI_EXP_LNKCTL_CCC)) {
263 				consistent = false;
264 				break;
265 			}
266 		}
267 		if (consistent)
268 			return;
269 		pci_info(parent, "ASPM: current common clock configuration is inconsistent, reconfiguring\n");
270 	}
271 
272 	/* Configure downstream component, all functions */
273 	list_for_each_entry(child, &linkbus->devices, bus_list) {
274 		pcie_capability_read_word(child, PCI_EXP_LNKCTL, &reg16);
275 		child_reg[PCI_FUNC(child->devfn)] = reg16;
276 		if (same_clock)
277 			reg16 |= PCI_EXP_LNKCTL_CCC;
278 		else
279 			reg16 &= ~PCI_EXP_LNKCTL_CCC;
280 		pcie_capability_write_word(child, PCI_EXP_LNKCTL, reg16);
281 	}
282 
283 	/* Configure upstream component */
284 	pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &reg16);
285 	parent_reg = reg16;
286 	if (same_clock)
287 		reg16 |= PCI_EXP_LNKCTL_CCC;
288 	else
289 		reg16 &= ~PCI_EXP_LNKCTL_CCC;
290 	pcie_capability_write_word(parent, PCI_EXP_LNKCTL, reg16);
291 
292 	if (pcie_retrain_link(link))
293 		return;
294 
295 	/* Training failed. Restore common clock configurations */
296 	pci_err(parent, "ASPM: Could not configure common clock\n");
297 	list_for_each_entry(child, &linkbus->devices, bus_list)
298 		pcie_capability_write_word(child, PCI_EXP_LNKCTL,
299 					   child_reg[PCI_FUNC(child->devfn)]);
300 	pcie_capability_write_word(parent, PCI_EXP_LNKCTL, parent_reg);
301 }
302 
303 /* Convert L0s latency encoding to ns */
304 static u32 calc_l0s_latency(u32 lnkcap)
305 {
306 	u32 encoding = (lnkcap & PCI_EXP_LNKCAP_L0SEL) >> 12;
307 
308 	if (encoding == 0x7)
309 		return (5 * 1000);	/* > 4us */
310 	return (64 << encoding);
311 }
312 
313 /* Convert L0s acceptable latency encoding to ns */
314 static u32 calc_l0s_acceptable(u32 encoding)
315 {
316 	if (encoding == 0x7)
317 		return -1U;
318 	return (64 << encoding);
319 }
320 
321 /* Convert L1 latency encoding to ns */
322 static u32 calc_l1_latency(u32 lnkcap)
323 {
324 	u32 encoding = (lnkcap & PCI_EXP_LNKCAP_L1EL) >> 15;
325 
326 	if (encoding == 0x7)
327 		return (65 * 1000);	/* > 64us */
328 	return (1000 << encoding);
329 }
330 
331 /* Convert L1 acceptable latency encoding to ns */
332 static u32 calc_l1_acceptable(u32 encoding)
333 {
334 	if (encoding == 0x7)
335 		return -1U;
336 	return (1000 << encoding);
337 }
338 
339 /* Convert L1SS T_pwr encoding to usec */
340 static u32 calc_l12_pwron(struct pci_dev *pdev, u32 scale, u32 val)
341 {
342 	switch (scale) {
343 	case 0:
344 		return val * 2;
345 	case 1:
346 		return val * 10;
347 	case 2:
348 		return val * 100;
349 	}
350 	pci_err(pdev, "%s: Invalid T_PwrOn scale: %u\n", __func__, scale);
351 	return 0;
352 }
353 
354 /*
355  * Encode an LTR_L1.2_THRESHOLD value for the L1 PM Substates Control 1
356  * register.  Ports enter L1.2 when the most recent LTR value is greater
357  * than or equal to LTR_L1.2_THRESHOLD, so we round up to make sure we
358  * don't enter L1.2 too aggressively.
359  *
360  * See PCIe r6.0, sec 5.5.1, 6.18, 7.8.3.3.
361  */
362 static void encode_l12_threshold(u32 threshold_us, u32 *scale, u32 *value)
363 {
364 	u64 threshold_ns = (u64) threshold_us * 1000;
365 
366 	/*
367 	 * LTR_L1.2_THRESHOLD_Value ("value") is a 10-bit field with max
368 	 * value of 0x3ff.
369 	 */
370 	if (threshold_ns <= 0x3ff * 1) {
371 		*scale = 0;		/* Value times 1ns */
372 		*value = threshold_ns;
373 	} else if (threshold_ns <= 0x3ff * 32) {
374 		*scale = 1;		/* Value times 32ns */
375 		*value = roundup(threshold_ns, 32) / 32;
376 	} else if (threshold_ns <= 0x3ff * 1024) {
377 		*scale = 2;		/* Value times 1024ns */
378 		*value = roundup(threshold_ns, 1024) / 1024;
379 	} else if (threshold_ns <= 0x3ff * 32768) {
380 		*scale = 3;		/* Value times 32768ns */
381 		*value = roundup(threshold_ns, 32768) / 32768;
382 	} else if (threshold_ns <= 0x3ff * 1048576) {
383 		*scale = 4;		/* Value times 1048576ns */
384 		*value = roundup(threshold_ns, 1048576) / 1048576;
385 	} else if (threshold_ns <= 0x3ff * (u64) 33554432) {
386 		*scale = 5;		/* Value times 33554432ns */
387 		*value = roundup(threshold_ns, 33554432) / 33554432;
388 	} else {
389 		*scale = 5;
390 		*value = 0x3ff;		/* Max representable value */
391 	}
392 }
393 
394 static void pcie_aspm_check_latency(struct pci_dev *endpoint)
395 {
396 	u32 latency, encoding, lnkcap_up, lnkcap_dw;
397 	u32 l1_switch_latency = 0, latency_up_l0s;
398 	u32 latency_up_l1, latency_dw_l0s, latency_dw_l1;
399 	u32 acceptable_l0s, acceptable_l1;
400 	struct pcie_link_state *link;
401 
402 	/* Device not in D0 doesn't need latency check */
403 	if ((endpoint->current_state != PCI_D0) &&
404 	    (endpoint->current_state != PCI_UNKNOWN))
405 		return;
406 
407 	link = endpoint->bus->self->link_state;
408 
409 	/* Calculate endpoint L0s acceptable latency */
410 	encoding = (endpoint->devcap & PCI_EXP_DEVCAP_L0S) >> 6;
411 	acceptable_l0s = calc_l0s_acceptable(encoding);
412 
413 	/* Calculate endpoint L1 acceptable latency */
414 	encoding = (endpoint->devcap & PCI_EXP_DEVCAP_L1) >> 9;
415 	acceptable_l1 = calc_l1_acceptable(encoding);
416 
417 	while (link) {
418 		struct pci_dev *dev = pci_function_0(link->pdev->subordinate);
419 
420 		/* Read direction exit latencies */
421 		pcie_capability_read_dword(link->pdev, PCI_EXP_LNKCAP,
422 					   &lnkcap_up);
423 		pcie_capability_read_dword(dev, PCI_EXP_LNKCAP,
424 					   &lnkcap_dw);
425 		latency_up_l0s = calc_l0s_latency(lnkcap_up);
426 		latency_up_l1 = calc_l1_latency(lnkcap_up);
427 		latency_dw_l0s = calc_l0s_latency(lnkcap_dw);
428 		latency_dw_l1 = calc_l1_latency(lnkcap_dw);
429 
430 		/* Check upstream direction L0s latency */
431 		if ((link->aspm_capable & ASPM_STATE_L0S_UP) &&
432 		    (latency_up_l0s > acceptable_l0s))
433 			link->aspm_capable &= ~ASPM_STATE_L0S_UP;
434 
435 		/* Check downstream direction L0s latency */
436 		if ((link->aspm_capable & ASPM_STATE_L0S_DW) &&
437 		    (latency_dw_l0s > acceptable_l0s))
438 			link->aspm_capable &= ~ASPM_STATE_L0S_DW;
439 		/*
440 		 * Check L1 latency.
441 		 * Every switch on the path to root complex need 1
442 		 * more microsecond for L1. Spec doesn't mention L0s.
443 		 *
444 		 * The exit latencies for L1 substates are not advertised
445 		 * by a device.  Since the spec also doesn't mention a way
446 		 * to determine max latencies introduced by enabling L1
447 		 * substates on the components, it is not clear how to do
448 		 * a L1 substate exit latency check.  We assume that the
449 		 * L1 exit latencies advertised by a device include L1
450 		 * substate latencies (and hence do not do any check).
451 		 */
452 		latency = max_t(u32, latency_up_l1, latency_dw_l1);
453 		if ((link->aspm_capable & ASPM_STATE_L1) &&
454 		    (latency + l1_switch_latency > acceptable_l1))
455 			link->aspm_capable &= ~ASPM_STATE_L1;
456 		l1_switch_latency += 1000;
457 
458 		link = link->parent;
459 	}
460 }
461 
462 static void pci_clear_and_set_dword(struct pci_dev *pdev, int pos,
463 				    u32 clear, u32 set)
464 {
465 	u32 val;
466 
467 	pci_read_config_dword(pdev, pos, &val);
468 	val &= ~clear;
469 	val |= set;
470 	pci_write_config_dword(pdev, pos, val);
471 }
472 
473 /* Calculate L1.2 PM substate timing parameters */
474 static void aspm_calc_l12_info(struct pcie_link_state *link,
475 				u32 parent_l1ss_cap, u32 child_l1ss_cap)
476 {
477 	struct pci_dev *child = link->downstream, *parent = link->pdev;
478 	u32 val1, val2, scale1, scale2;
479 	u32 t_common_mode, t_power_on, l1_2_threshold, scale, value;
480 	u32 ctl1 = 0, ctl2 = 0;
481 	u32 pctl1, pctl2, cctl1, cctl2;
482 	u32 pl1_2_enables, cl1_2_enables;
483 
484 	/* Choose the greater of the two Port Common_Mode_Restore_Times */
485 	val1 = (parent_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
486 	val2 = (child_l1ss_cap & PCI_L1SS_CAP_CM_RESTORE_TIME) >> 8;
487 	t_common_mode = max(val1, val2);
488 
489 	/* Choose the greater of the two Port T_POWER_ON times */
490 	val1   = (parent_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
491 	scale1 = (parent_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
492 	val2   = (child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_VALUE) >> 19;
493 	scale2 = (child_l1ss_cap & PCI_L1SS_CAP_P_PWR_ON_SCALE) >> 16;
494 
495 	if (calc_l12_pwron(parent, scale1, val1) >
496 	    calc_l12_pwron(child, scale2, val2)) {
497 		ctl2 |= scale1 | (val1 << 3);
498 		t_power_on = calc_l12_pwron(parent, scale1, val1);
499 	} else {
500 		ctl2 |= scale2 | (val2 << 3);
501 		t_power_on = calc_l12_pwron(child, scale2, val2);
502 	}
503 
504 	/*
505 	 * Set LTR_L1.2_THRESHOLD to the time required to transition the
506 	 * Link from L0 to L1.2 and back to L0 so we enter L1.2 only if
507 	 * downstream devices report (via LTR) that they can tolerate at
508 	 * least that much latency.
509 	 *
510 	 * Based on PCIe r3.1, sec 5.5.3.3.1, Figures 5-16 and 5-17, and
511 	 * Table 5-11.  T(POWER_OFF) is at most 2us and T(L1.2) is at
512 	 * least 4us.
513 	 */
514 	l1_2_threshold = 2 + 4 + t_common_mode + t_power_on;
515 	encode_l12_threshold(l1_2_threshold, &scale, &value);
516 	ctl1 |= t_common_mode << 8 | scale << 29 | value << 16;
517 
518 	/* Some broken devices only support dword access to L1 SS */
519 	pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1, &pctl1);
520 	pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, &pctl2);
521 	pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL1, &cctl1);
522 	pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL2, &cctl2);
523 
524 	if (ctl1 == pctl1 && ctl1 == cctl1 &&
525 	    ctl2 == pctl2 && ctl2 == cctl2)
526 		return;
527 
528 	/* Disable L1.2 while updating.  See PCIe r5.0, sec 5.5.4, 7.8.3.3 */
529 	pl1_2_enables = pctl1 & PCI_L1SS_CTL1_L1_2_MASK;
530 	cl1_2_enables = cctl1 & PCI_L1SS_CTL1_L1_2_MASK;
531 
532 	if (pl1_2_enables || cl1_2_enables) {
533 		pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1,
534 					PCI_L1SS_CTL1_L1_2_MASK, 0);
535 		pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
536 					PCI_L1SS_CTL1_L1_2_MASK, 0);
537 	}
538 
539 	/* Program T_POWER_ON times in both ports */
540 	pci_write_config_dword(parent, parent->l1ss + PCI_L1SS_CTL2, ctl2);
541 	pci_write_config_dword(child, child->l1ss + PCI_L1SS_CTL2, ctl2);
542 
543 	/* Program Common_Mode_Restore_Time in upstream device */
544 	pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
545 				PCI_L1SS_CTL1_CM_RESTORE_TIME, ctl1);
546 
547 	/* Program LTR_L1.2_THRESHOLD time in both ports */
548 	pci_clear_and_set_dword(parent,	parent->l1ss + PCI_L1SS_CTL1,
549 				PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
550 				PCI_L1SS_CTL1_LTR_L12_TH_SCALE, ctl1);
551 	pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1,
552 				PCI_L1SS_CTL1_LTR_L12_TH_VALUE |
553 				PCI_L1SS_CTL1_LTR_L12_TH_SCALE, ctl1);
554 
555 	if (pl1_2_enables || cl1_2_enables) {
556 		pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1, 0,
557 					pl1_2_enables);
558 		pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1, 0,
559 					cl1_2_enables);
560 	}
561 }
562 
563 static void aspm_l1ss_init(struct pcie_link_state *link)
564 {
565 	struct pci_dev *child = link->downstream, *parent = link->pdev;
566 	u32 parent_l1ss_cap, child_l1ss_cap;
567 	u32 parent_l1ss_ctl1 = 0, child_l1ss_ctl1 = 0;
568 
569 	if (!parent->l1ss || !child->l1ss)
570 		return;
571 
572 	/* Setup L1 substate */
573 	pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CAP,
574 			      &parent_l1ss_cap);
575 	pci_read_config_dword(child, child->l1ss + PCI_L1SS_CAP,
576 			      &child_l1ss_cap);
577 
578 	if (!(parent_l1ss_cap & PCI_L1SS_CAP_L1_PM_SS))
579 		parent_l1ss_cap = 0;
580 	if (!(child_l1ss_cap & PCI_L1SS_CAP_L1_PM_SS))
581 		child_l1ss_cap = 0;
582 
583 	/*
584 	 * If we don't have LTR for the entire path from the Root Complex
585 	 * to this device, we can't use ASPM L1.2 because it relies on the
586 	 * LTR_L1.2_THRESHOLD.  See PCIe r4.0, secs 5.5.4, 6.18.
587 	 */
588 	if (!child->ltr_path)
589 		child_l1ss_cap &= ~PCI_L1SS_CAP_ASPM_L1_2;
590 
591 	if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_ASPM_L1_1)
592 		link->aspm_support |= ASPM_STATE_L1_1;
593 	if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_ASPM_L1_2)
594 		link->aspm_support |= ASPM_STATE_L1_2;
595 	if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_1)
596 		link->aspm_support |= ASPM_STATE_L1_1_PCIPM;
597 	if (parent_l1ss_cap & child_l1ss_cap & PCI_L1SS_CAP_PCIPM_L1_2)
598 		link->aspm_support |= ASPM_STATE_L1_2_PCIPM;
599 
600 	if (parent_l1ss_cap)
601 		pci_read_config_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
602 				      &parent_l1ss_ctl1);
603 	if (child_l1ss_cap)
604 		pci_read_config_dword(child, child->l1ss + PCI_L1SS_CTL1,
605 				      &child_l1ss_ctl1);
606 
607 	if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_1)
608 		link->aspm_enabled |= ASPM_STATE_L1_1;
609 	if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_ASPM_L1_2)
610 		link->aspm_enabled |= ASPM_STATE_L1_2;
611 	if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_1)
612 		link->aspm_enabled |= ASPM_STATE_L1_1_PCIPM;
613 	if (parent_l1ss_ctl1 & child_l1ss_ctl1 & PCI_L1SS_CTL1_PCIPM_L1_2)
614 		link->aspm_enabled |= ASPM_STATE_L1_2_PCIPM;
615 
616 	if (link->aspm_support & ASPM_STATE_L1_2_MASK)
617 		aspm_calc_l12_info(link, parent_l1ss_cap, child_l1ss_cap);
618 }
619 
620 static void pcie_aspm_cap_init(struct pcie_link_state *link, int blacklist)
621 {
622 	struct pci_dev *child = link->downstream, *parent = link->pdev;
623 	u32 parent_lnkcap, child_lnkcap;
624 	u16 parent_lnkctl, child_lnkctl;
625 	struct pci_bus *linkbus = parent->subordinate;
626 
627 	if (blacklist) {
628 		/* Set enabled/disable so that we will disable ASPM later */
629 		link->aspm_enabled = ASPM_STATE_ALL;
630 		link->aspm_disable = ASPM_STATE_ALL;
631 		return;
632 	}
633 
634 	/*
635 	 * If ASPM not supported, don't mess with the clocks and link,
636 	 * bail out now.
637 	 */
638 	pcie_capability_read_dword(parent, PCI_EXP_LNKCAP, &parent_lnkcap);
639 	pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &child_lnkcap);
640 	if (!(parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPMS))
641 		return;
642 
643 	/* Configure common clock before checking latencies */
644 	pcie_aspm_configure_common_clock(link);
645 
646 	/*
647 	 * Re-read upstream/downstream components' register state after
648 	 * clock configuration.  L0s & L1 exit latencies in the otherwise
649 	 * read-only Link Capabilities may change depending on common clock
650 	 * configuration (PCIe r5.0, sec 7.5.3.6).
651 	 */
652 	pcie_capability_read_dword(parent, PCI_EXP_LNKCAP, &parent_lnkcap);
653 	pcie_capability_read_dword(child, PCI_EXP_LNKCAP, &child_lnkcap);
654 	pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &parent_lnkctl);
655 	pcie_capability_read_word(child, PCI_EXP_LNKCTL, &child_lnkctl);
656 
657 	/*
658 	 * Setup L0s state
659 	 *
660 	 * Note that we must not enable L0s in either direction on a
661 	 * given link unless components on both sides of the link each
662 	 * support L0s.
663 	 */
664 	if (parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPM_L0S)
665 		link->aspm_support |= ASPM_STATE_L0S;
666 
667 	if (child_lnkctl & PCI_EXP_LNKCTL_ASPM_L0S)
668 		link->aspm_enabled |= ASPM_STATE_L0S_UP;
669 	if (parent_lnkctl & PCI_EXP_LNKCTL_ASPM_L0S)
670 		link->aspm_enabled |= ASPM_STATE_L0S_DW;
671 
672 	/* Setup L1 state */
673 	if (parent_lnkcap & child_lnkcap & PCI_EXP_LNKCAP_ASPM_L1)
674 		link->aspm_support |= ASPM_STATE_L1;
675 
676 	if (parent_lnkctl & child_lnkctl & PCI_EXP_LNKCTL_ASPM_L1)
677 		link->aspm_enabled |= ASPM_STATE_L1;
678 
679 	aspm_l1ss_init(link);
680 
681 	/* Save default state */
682 	link->aspm_default = link->aspm_enabled;
683 
684 	/* Setup initial capable state. Will be updated later */
685 	link->aspm_capable = link->aspm_support;
686 
687 	/* Get and check endpoint acceptable latencies */
688 	list_for_each_entry(child, &linkbus->devices, bus_list) {
689 		if (pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT &&
690 		    pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END)
691 			continue;
692 
693 		pcie_aspm_check_latency(child);
694 	}
695 }
696 
697 /* Configure the ASPM L1 substates */
698 static void pcie_config_aspm_l1ss(struct pcie_link_state *link, u32 state)
699 {
700 	u32 val, enable_req;
701 	struct pci_dev *child = link->downstream, *parent = link->pdev;
702 
703 	enable_req = (link->aspm_enabled ^ state) & state;
704 
705 	/*
706 	 * Here are the rules specified in the PCIe spec for enabling L1SS:
707 	 * - When enabling L1.x, enable bit at parent first, then at child
708 	 * - When disabling L1.x, disable bit at child first, then at parent
709 	 * - When enabling ASPM L1.x, need to disable L1
710 	 *   (at child followed by parent).
711 	 * - The ASPM/PCIPM L1.2 must be disabled while programming timing
712 	 *   parameters
713 	 *
714 	 * To keep it simple, disable all L1SS bits first, and later enable
715 	 * what is needed.
716 	 */
717 
718 	/* Disable all L1 substates */
719 	pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1,
720 				PCI_L1SS_CTL1_L1SS_MASK, 0);
721 	pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
722 				PCI_L1SS_CTL1_L1SS_MASK, 0);
723 	/*
724 	 * If needed, disable L1, and it gets enabled later
725 	 * in pcie_config_aspm_link().
726 	 */
727 	if (enable_req & (ASPM_STATE_L1_1 | ASPM_STATE_L1_2)) {
728 		pcie_capability_clear_and_set_word(child, PCI_EXP_LNKCTL,
729 						   PCI_EXP_LNKCTL_ASPM_L1, 0);
730 		pcie_capability_clear_and_set_word(parent, PCI_EXP_LNKCTL,
731 						   PCI_EXP_LNKCTL_ASPM_L1, 0);
732 	}
733 
734 	val = 0;
735 	if (state & ASPM_STATE_L1_1)
736 		val |= PCI_L1SS_CTL1_ASPM_L1_1;
737 	if (state & ASPM_STATE_L1_2)
738 		val |= PCI_L1SS_CTL1_ASPM_L1_2;
739 	if (state & ASPM_STATE_L1_1_PCIPM)
740 		val |= PCI_L1SS_CTL1_PCIPM_L1_1;
741 	if (state & ASPM_STATE_L1_2_PCIPM)
742 		val |= PCI_L1SS_CTL1_PCIPM_L1_2;
743 
744 	/* Enable what we need to enable */
745 	pci_clear_and_set_dword(parent, parent->l1ss + PCI_L1SS_CTL1,
746 				PCI_L1SS_CTL1_L1SS_MASK, val);
747 	pci_clear_and_set_dword(child, child->l1ss + PCI_L1SS_CTL1,
748 				PCI_L1SS_CTL1_L1SS_MASK, val);
749 }
750 
751 static void pcie_config_aspm_dev(struct pci_dev *pdev, u32 val)
752 {
753 	pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
754 					   PCI_EXP_LNKCTL_ASPMC, val);
755 }
756 
757 static void pcie_config_aspm_link(struct pcie_link_state *link, u32 state)
758 {
759 	u32 upstream = 0, dwstream = 0;
760 	struct pci_dev *child = link->downstream, *parent = link->pdev;
761 	struct pci_bus *linkbus = parent->subordinate;
762 
763 	/* Enable only the states that were not explicitly disabled */
764 	state &= (link->aspm_capable & ~link->aspm_disable);
765 
766 	/* Can't enable any substates if L1 is not enabled */
767 	if (!(state & ASPM_STATE_L1))
768 		state &= ~ASPM_STATE_L1SS;
769 
770 	/* Spec says both ports must be in D0 before enabling PCI PM substates*/
771 	if (parent->current_state != PCI_D0 || child->current_state != PCI_D0) {
772 		state &= ~ASPM_STATE_L1_SS_PCIPM;
773 		state |= (link->aspm_enabled & ASPM_STATE_L1_SS_PCIPM);
774 	}
775 
776 	/* Nothing to do if the link is already in the requested state */
777 	if (link->aspm_enabled == state)
778 		return;
779 	/* Convert ASPM state to upstream/downstream ASPM register state */
780 	if (state & ASPM_STATE_L0S_UP)
781 		dwstream |= PCI_EXP_LNKCTL_ASPM_L0S;
782 	if (state & ASPM_STATE_L0S_DW)
783 		upstream |= PCI_EXP_LNKCTL_ASPM_L0S;
784 	if (state & ASPM_STATE_L1) {
785 		upstream |= PCI_EXP_LNKCTL_ASPM_L1;
786 		dwstream |= PCI_EXP_LNKCTL_ASPM_L1;
787 	}
788 
789 	if (link->aspm_capable & ASPM_STATE_L1SS)
790 		pcie_config_aspm_l1ss(link, state);
791 
792 	/*
793 	 * Spec 2.0 suggests all functions should be configured the
794 	 * same setting for ASPM. Enabling ASPM L1 should be done in
795 	 * upstream component first and then downstream, and vice
796 	 * versa for disabling ASPM L1. Spec doesn't mention L0S.
797 	 */
798 	if (state & ASPM_STATE_L1)
799 		pcie_config_aspm_dev(parent, upstream);
800 	list_for_each_entry(child, &linkbus->devices, bus_list)
801 		pcie_config_aspm_dev(child, dwstream);
802 	if (!(state & ASPM_STATE_L1))
803 		pcie_config_aspm_dev(parent, upstream);
804 
805 	link->aspm_enabled = state;
806 }
807 
808 static void pcie_config_aspm_path(struct pcie_link_state *link)
809 {
810 	while (link) {
811 		pcie_config_aspm_link(link, policy_to_aspm_state(link));
812 		link = link->parent;
813 	}
814 }
815 
816 static void free_link_state(struct pcie_link_state *link)
817 {
818 	link->pdev->link_state = NULL;
819 	kfree(link);
820 }
821 
822 static int pcie_aspm_sanity_check(struct pci_dev *pdev)
823 {
824 	struct pci_dev *child;
825 	u32 reg32;
826 
827 	/*
828 	 * Some functions in a slot might not all be PCIe functions,
829 	 * very strange. Disable ASPM for the whole slot
830 	 */
831 	list_for_each_entry(child, &pdev->subordinate->devices, bus_list) {
832 		if (!pci_is_pcie(child))
833 			return -EINVAL;
834 
835 		/*
836 		 * If ASPM is disabled then we're not going to change
837 		 * the BIOS state. It's safe to continue even if it's a
838 		 * pre-1.1 device
839 		 */
840 
841 		if (aspm_disabled)
842 			continue;
843 
844 		/*
845 		 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
846 		 * RBER bit to determine if a function is 1.1 version device
847 		 */
848 		pcie_capability_read_dword(child, PCI_EXP_DEVCAP, &reg32);
849 		if (!(reg32 & PCI_EXP_DEVCAP_RBER) && !aspm_force) {
850 			pci_info(child, "disabling ASPM on pre-1.1 PCIe device.  You can enable it with 'pcie_aspm=force'\n");
851 			return -EINVAL;
852 		}
853 	}
854 	return 0;
855 }
856 
857 static struct pcie_link_state *alloc_pcie_link_state(struct pci_dev *pdev)
858 {
859 	struct pcie_link_state *link;
860 
861 	link = kzalloc(sizeof(*link), GFP_KERNEL);
862 	if (!link)
863 		return NULL;
864 
865 	INIT_LIST_HEAD(&link->sibling);
866 	link->pdev = pdev;
867 	link->downstream = pci_function_0(pdev->subordinate);
868 
869 	/*
870 	 * Root Ports and PCI/PCI-X to PCIe Bridges are roots of PCIe
871 	 * hierarchies.  Note that some PCIe host implementations omit
872 	 * the root ports entirely, in which case a downstream port on
873 	 * a switch may become the root of the link state chain for all
874 	 * its subordinate endpoints.
875 	 */
876 	if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT ||
877 	    pci_pcie_type(pdev) == PCI_EXP_TYPE_PCIE_BRIDGE ||
878 	    !pdev->bus->parent->self) {
879 		link->root = link;
880 	} else {
881 		struct pcie_link_state *parent;
882 
883 		parent = pdev->bus->parent->self->link_state;
884 		if (!parent) {
885 			kfree(link);
886 			return NULL;
887 		}
888 
889 		link->parent = parent;
890 		link->root = link->parent->root;
891 	}
892 
893 	list_add(&link->sibling, &link_list);
894 	pdev->link_state = link;
895 	return link;
896 }
897 
898 static void pcie_aspm_update_sysfs_visibility(struct pci_dev *pdev)
899 {
900 	struct pci_dev *child;
901 
902 	list_for_each_entry(child, &pdev->subordinate->devices, bus_list)
903 		sysfs_update_group(&child->dev.kobj, &aspm_ctrl_attr_group);
904 }
905 
906 /*
907  * pcie_aspm_init_link_state: Initiate PCI express link state.
908  * It is called after the pcie and its children devices are scanned.
909  * @pdev: the root port or switch downstream port
910  */
911 void pcie_aspm_init_link_state(struct pci_dev *pdev)
912 {
913 	struct pcie_link_state *link;
914 	int blacklist = !!pcie_aspm_sanity_check(pdev);
915 
916 	if (!aspm_support_enabled)
917 		return;
918 
919 	if (pdev->link_state)
920 		return;
921 
922 	/*
923 	 * We allocate pcie_link_state for the component on the upstream
924 	 * end of a Link, so there's nothing to do unless this device is
925 	 * downstream port.
926 	 */
927 	if (!pcie_downstream_port(pdev))
928 		return;
929 
930 	/* VIA has a strange chipset, root port is under a bridge */
931 	if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT &&
932 	    pdev->bus->self)
933 		return;
934 
935 	down_read(&pci_bus_sem);
936 	if (list_empty(&pdev->subordinate->devices))
937 		goto out;
938 
939 	mutex_lock(&aspm_lock);
940 	link = alloc_pcie_link_state(pdev);
941 	if (!link)
942 		goto unlock;
943 	/*
944 	 * Setup initial ASPM state. Note that we need to configure
945 	 * upstream links also because capable state of them can be
946 	 * update through pcie_aspm_cap_init().
947 	 */
948 	pcie_aspm_cap_init(link, blacklist);
949 
950 	/* Setup initial Clock PM state */
951 	pcie_clkpm_cap_init(link, blacklist);
952 
953 	/*
954 	 * At this stage drivers haven't had an opportunity to change the
955 	 * link policy setting. Enabling ASPM on broken hardware can cripple
956 	 * it even before the driver has had a chance to disable ASPM, so
957 	 * default to a safe level right now. If we're enabling ASPM beyond
958 	 * the BIOS's expectation, we'll do so once pci_enable_device() is
959 	 * called.
960 	 */
961 	if (aspm_policy != POLICY_POWERSAVE &&
962 	    aspm_policy != POLICY_POWER_SUPERSAVE) {
963 		pcie_config_aspm_path(link);
964 		pcie_set_clkpm(link, policy_to_clkpm_state(link));
965 	}
966 
967 	pcie_aspm_update_sysfs_visibility(pdev);
968 
969 unlock:
970 	mutex_unlock(&aspm_lock);
971 out:
972 	up_read(&pci_bus_sem);
973 }
974 
975 /* Recheck latencies and update aspm_capable for links under the root */
976 static void pcie_update_aspm_capable(struct pcie_link_state *root)
977 {
978 	struct pcie_link_state *link;
979 	BUG_ON(root->parent);
980 	list_for_each_entry(link, &link_list, sibling) {
981 		if (link->root != root)
982 			continue;
983 		link->aspm_capable = link->aspm_support;
984 	}
985 	list_for_each_entry(link, &link_list, sibling) {
986 		struct pci_dev *child;
987 		struct pci_bus *linkbus = link->pdev->subordinate;
988 		if (link->root != root)
989 			continue;
990 		list_for_each_entry(child, &linkbus->devices, bus_list) {
991 			if ((pci_pcie_type(child) != PCI_EXP_TYPE_ENDPOINT) &&
992 			    (pci_pcie_type(child) != PCI_EXP_TYPE_LEG_END))
993 				continue;
994 			pcie_aspm_check_latency(child);
995 		}
996 	}
997 }
998 
999 /* @pdev: the endpoint device */
1000 void pcie_aspm_exit_link_state(struct pci_dev *pdev)
1001 {
1002 	struct pci_dev *parent = pdev->bus->self;
1003 	struct pcie_link_state *link, *root, *parent_link;
1004 
1005 	if (!parent || !parent->link_state)
1006 		return;
1007 
1008 	down_read(&pci_bus_sem);
1009 	mutex_lock(&aspm_lock);
1010 
1011 	link = parent->link_state;
1012 	root = link->root;
1013 	parent_link = link->parent;
1014 
1015 	/*
1016 	 * link->downstream is a pointer to the pci_dev of function 0.  If
1017 	 * we remove that function, the pci_dev is about to be deallocated,
1018 	 * so we can't use link->downstream again.  Free the link state to
1019 	 * avoid this.
1020 	 *
1021 	 * If we're removing a non-0 function, it's possible we could
1022 	 * retain the link state, but PCIe r6.0, sec 7.5.3.7, recommends
1023 	 * programming the same ASPM Control value for all functions of
1024 	 * multi-function devices, so disable ASPM for all of them.
1025 	 */
1026 	pcie_config_aspm_link(link, 0);
1027 	list_del(&link->sibling);
1028 	free_link_state(link);
1029 
1030 	/* Recheck latencies and configure upstream links */
1031 	if (parent_link) {
1032 		pcie_update_aspm_capable(root);
1033 		pcie_config_aspm_path(parent_link);
1034 	}
1035 
1036 	mutex_unlock(&aspm_lock);
1037 	up_read(&pci_bus_sem);
1038 }
1039 
1040 void pcie_aspm_powersave_config_link(struct pci_dev *pdev)
1041 {
1042 	struct pcie_link_state *link = pdev->link_state;
1043 
1044 	if (aspm_disabled || !link)
1045 		return;
1046 
1047 	if (aspm_policy != POLICY_POWERSAVE &&
1048 	    aspm_policy != POLICY_POWER_SUPERSAVE)
1049 		return;
1050 
1051 	down_read(&pci_bus_sem);
1052 	mutex_lock(&aspm_lock);
1053 	pcie_config_aspm_path(link);
1054 	pcie_set_clkpm(link, policy_to_clkpm_state(link));
1055 	mutex_unlock(&aspm_lock);
1056 	up_read(&pci_bus_sem);
1057 }
1058 
1059 static struct pcie_link_state *pcie_aspm_get_link(struct pci_dev *pdev)
1060 {
1061 	struct pci_dev *bridge;
1062 
1063 	if (!pci_is_pcie(pdev))
1064 		return NULL;
1065 
1066 	bridge = pci_upstream_bridge(pdev);
1067 	if (!bridge || !pci_is_pcie(bridge))
1068 		return NULL;
1069 
1070 	return bridge->link_state;
1071 }
1072 
1073 static int __pci_disable_link_state(struct pci_dev *pdev, int state, bool sem)
1074 {
1075 	struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1076 
1077 	if (!link)
1078 		return -EINVAL;
1079 	/*
1080 	 * A driver requested that ASPM be disabled on this device, but
1081 	 * if we don't have permission to manage ASPM (e.g., on ACPI
1082 	 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
1083 	 * the _OSC method), we can't honor that request.  Windows has
1084 	 * a similar mechanism using "PciASPMOptOut", which is also
1085 	 * ignored in this situation.
1086 	 */
1087 	if (aspm_disabled) {
1088 		pci_warn(pdev, "can't disable ASPM; OS doesn't have ASPM control\n");
1089 		return -EPERM;
1090 	}
1091 
1092 	if (sem)
1093 		down_read(&pci_bus_sem);
1094 	mutex_lock(&aspm_lock);
1095 	if (state & PCIE_LINK_STATE_L0S)
1096 		link->aspm_disable |= ASPM_STATE_L0S;
1097 	if (state & PCIE_LINK_STATE_L1)
1098 		link->aspm_disable |= ASPM_STATE_L1;
1099 	if (state & PCIE_LINK_STATE_L1_1)
1100 		link->aspm_disable |= ASPM_STATE_L1_1;
1101 	if (state & PCIE_LINK_STATE_L1_2)
1102 		link->aspm_disable |= ASPM_STATE_L1_2;
1103 	if (state & PCIE_LINK_STATE_L1_1_PCIPM)
1104 		link->aspm_disable |= ASPM_STATE_L1_1_PCIPM;
1105 	if (state & PCIE_LINK_STATE_L1_2_PCIPM)
1106 		link->aspm_disable |= ASPM_STATE_L1_2_PCIPM;
1107 	pcie_config_aspm_link(link, policy_to_aspm_state(link));
1108 
1109 	if (state & PCIE_LINK_STATE_CLKPM)
1110 		link->clkpm_disable = 1;
1111 	pcie_set_clkpm(link, policy_to_clkpm_state(link));
1112 	mutex_unlock(&aspm_lock);
1113 	if (sem)
1114 		up_read(&pci_bus_sem);
1115 
1116 	return 0;
1117 }
1118 
1119 int pci_disable_link_state_locked(struct pci_dev *pdev, int state)
1120 {
1121 	return __pci_disable_link_state(pdev, state, false);
1122 }
1123 EXPORT_SYMBOL(pci_disable_link_state_locked);
1124 
1125 /**
1126  * pci_disable_link_state - Disable device's link state, so the link will
1127  * never enter specific states.  Note that if the BIOS didn't grant ASPM
1128  * control to the OS, this does nothing because we can't touch the LNKCTL
1129  * register. Returns 0 or a negative errno.
1130  *
1131  * @pdev: PCI device
1132  * @state: ASPM link state to disable
1133  */
1134 int pci_disable_link_state(struct pci_dev *pdev, int state)
1135 {
1136 	return __pci_disable_link_state(pdev, state, true);
1137 }
1138 EXPORT_SYMBOL(pci_disable_link_state);
1139 
1140 /**
1141  * pci_enable_link_state - Clear and set the default device link state so that
1142  * the link may be allowed to enter the specified states. Note that if the
1143  * BIOS didn't grant ASPM control to the OS, this does nothing because we can't
1144  * touch the LNKCTL register. Also note that this does not enable states
1145  * disabled by pci_disable_link_state(). Return 0 or a negative errno.
1146  *
1147  * @pdev: PCI device
1148  * @state: Mask of ASPM link states to enable
1149  */
1150 int pci_enable_link_state(struct pci_dev *pdev, int state)
1151 {
1152 	struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1153 
1154 	if (!link)
1155 		return -EINVAL;
1156 	/*
1157 	 * A driver requested that ASPM be enabled on this device, but
1158 	 * if we don't have permission to manage ASPM (e.g., on ACPI
1159 	 * systems we have to observe the FADT ACPI_FADT_NO_ASPM bit and
1160 	 * the _OSC method), we can't honor that request.
1161 	 */
1162 	if (aspm_disabled) {
1163 		pci_warn(pdev, "can't override BIOS ASPM; OS doesn't have ASPM control\n");
1164 		return -EPERM;
1165 	}
1166 
1167 	down_read(&pci_bus_sem);
1168 	mutex_lock(&aspm_lock);
1169 	link->aspm_default = 0;
1170 	if (state & PCIE_LINK_STATE_L0S)
1171 		link->aspm_default |= ASPM_STATE_L0S;
1172 	if (state & PCIE_LINK_STATE_L1)
1173 		link->aspm_default |= ASPM_STATE_L1;
1174 	/* L1 PM substates require L1 */
1175 	if (state & PCIE_LINK_STATE_L1_1)
1176 		link->aspm_default |= ASPM_STATE_L1_1 | ASPM_STATE_L1;
1177 	if (state & PCIE_LINK_STATE_L1_2)
1178 		link->aspm_default |= ASPM_STATE_L1_2 | ASPM_STATE_L1;
1179 	if (state & PCIE_LINK_STATE_L1_1_PCIPM)
1180 		link->aspm_default |= ASPM_STATE_L1_1_PCIPM | ASPM_STATE_L1;
1181 	if (state & PCIE_LINK_STATE_L1_2_PCIPM)
1182 		link->aspm_default |= ASPM_STATE_L1_2_PCIPM | ASPM_STATE_L1;
1183 	pcie_config_aspm_link(link, policy_to_aspm_state(link));
1184 
1185 	link->clkpm_default = (state & PCIE_LINK_STATE_CLKPM) ? 1 : 0;
1186 	pcie_set_clkpm(link, policy_to_clkpm_state(link));
1187 	mutex_unlock(&aspm_lock);
1188 	up_read(&pci_bus_sem);
1189 
1190 	return 0;
1191 }
1192 EXPORT_SYMBOL(pci_enable_link_state);
1193 
1194 static int pcie_aspm_set_policy(const char *val,
1195 				const struct kernel_param *kp)
1196 {
1197 	int i;
1198 	struct pcie_link_state *link;
1199 
1200 	if (aspm_disabled)
1201 		return -EPERM;
1202 	i = sysfs_match_string(policy_str, val);
1203 	if (i < 0)
1204 		return i;
1205 	if (i == aspm_policy)
1206 		return 0;
1207 
1208 	down_read(&pci_bus_sem);
1209 	mutex_lock(&aspm_lock);
1210 	aspm_policy = i;
1211 	list_for_each_entry(link, &link_list, sibling) {
1212 		pcie_config_aspm_link(link, policy_to_aspm_state(link));
1213 		pcie_set_clkpm(link, policy_to_clkpm_state(link));
1214 	}
1215 	mutex_unlock(&aspm_lock);
1216 	up_read(&pci_bus_sem);
1217 	return 0;
1218 }
1219 
1220 static int pcie_aspm_get_policy(char *buffer, const struct kernel_param *kp)
1221 {
1222 	int i, cnt = 0;
1223 	for (i = 0; i < ARRAY_SIZE(policy_str); i++)
1224 		if (i == aspm_policy)
1225 			cnt += sprintf(buffer + cnt, "[%s] ", policy_str[i]);
1226 		else
1227 			cnt += sprintf(buffer + cnt, "%s ", policy_str[i]);
1228 	cnt += sprintf(buffer + cnt, "\n");
1229 	return cnt;
1230 }
1231 
1232 module_param_call(policy, pcie_aspm_set_policy, pcie_aspm_get_policy,
1233 	NULL, 0644);
1234 
1235 /**
1236  * pcie_aspm_enabled - Check if PCIe ASPM has been enabled for a device.
1237  * @pdev: Target device.
1238  *
1239  * Relies on the upstream bridge's link_state being valid.  The link_state
1240  * is deallocated only when the last child of the bridge (i.e., @pdev or a
1241  * sibling) is removed, and the caller should be holding a reference to
1242  * @pdev, so this should be safe.
1243  */
1244 bool pcie_aspm_enabled(struct pci_dev *pdev)
1245 {
1246 	struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1247 
1248 	if (!link)
1249 		return false;
1250 
1251 	return link->aspm_enabled;
1252 }
1253 EXPORT_SYMBOL_GPL(pcie_aspm_enabled);
1254 
1255 static ssize_t aspm_attr_show_common(struct device *dev,
1256 				     struct device_attribute *attr,
1257 				     char *buf, u8 state)
1258 {
1259 	struct pci_dev *pdev = to_pci_dev(dev);
1260 	struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1261 
1262 	return sysfs_emit(buf, "%d\n", (link->aspm_enabled & state) ? 1 : 0);
1263 }
1264 
1265 static ssize_t aspm_attr_store_common(struct device *dev,
1266 				      struct device_attribute *attr,
1267 				      const char *buf, size_t len, u8 state)
1268 {
1269 	struct pci_dev *pdev = to_pci_dev(dev);
1270 	struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1271 	bool state_enable;
1272 
1273 	if (kstrtobool(buf, &state_enable) < 0)
1274 		return -EINVAL;
1275 
1276 	down_read(&pci_bus_sem);
1277 	mutex_lock(&aspm_lock);
1278 
1279 	if (state_enable) {
1280 		link->aspm_disable &= ~state;
1281 		/* need to enable L1 for substates */
1282 		if (state & ASPM_STATE_L1SS)
1283 			link->aspm_disable &= ~ASPM_STATE_L1;
1284 	} else {
1285 		link->aspm_disable |= state;
1286 	}
1287 
1288 	pcie_config_aspm_link(link, policy_to_aspm_state(link));
1289 
1290 	mutex_unlock(&aspm_lock);
1291 	up_read(&pci_bus_sem);
1292 
1293 	return len;
1294 }
1295 
1296 #define ASPM_ATTR(_f, _s)						\
1297 static ssize_t _f##_show(struct device *dev,				\
1298 			 struct device_attribute *attr, char *buf)	\
1299 { return aspm_attr_show_common(dev, attr, buf, ASPM_STATE_##_s); }	\
1300 									\
1301 static ssize_t _f##_store(struct device *dev,				\
1302 			  struct device_attribute *attr,		\
1303 			  const char *buf, size_t len)			\
1304 { return aspm_attr_store_common(dev, attr, buf, len, ASPM_STATE_##_s); }
1305 
1306 ASPM_ATTR(l0s_aspm, L0S)
1307 ASPM_ATTR(l1_aspm, L1)
1308 ASPM_ATTR(l1_1_aspm, L1_1)
1309 ASPM_ATTR(l1_2_aspm, L1_2)
1310 ASPM_ATTR(l1_1_pcipm, L1_1_PCIPM)
1311 ASPM_ATTR(l1_2_pcipm, L1_2_PCIPM)
1312 
1313 static ssize_t clkpm_show(struct device *dev,
1314 			  struct device_attribute *attr, char *buf)
1315 {
1316 	struct pci_dev *pdev = to_pci_dev(dev);
1317 	struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1318 
1319 	return sysfs_emit(buf, "%d\n", link->clkpm_enabled);
1320 }
1321 
1322 static ssize_t clkpm_store(struct device *dev,
1323 			   struct device_attribute *attr,
1324 			   const char *buf, size_t len)
1325 {
1326 	struct pci_dev *pdev = to_pci_dev(dev);
1327 	struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1328 	bool state_enable;
1329 
1330 	if (kstrtobool(buf, &state_enable) < 0)
1331 		return -EINVAL;
1332 
1333 	down_read(&pci_bus_sem);
1334 	mutex_lock(&aspm_lock);
1335 
1336 	link->clkpm_disable = !state_enable;
1337 	pcie_set_clkpm(link, policy_to_clkpm_state(link));
1338 
1339 	mutex_unlock(&aspm_lock);
1340 	up_read(&pci_bus_sem);
1341 
1342 	return len;
1343 }
1344 
1345 static DEVICE_ATTR_RW(clkpm);
1346 static DEVICE_ATTR_RW(l0s_aspm);
1347 static DEVICE_ATTR_RW(l1_aspm);
1348 static DEVICE_ATTR_RW(l1_1_aspm);
1349 static DEVICE_ATTR_RW(l1_2_aspm);
1350 static DEVICE_ATTR_RW(l1_1_pcipm);
1351 static DEVICE_ATTR_RW(l1_2_pcipm);
1352 
1353 static struct attribute *aspm_ctrl_attrs[] = {
1354 	&dev_attr_clkpm.attr,
1355 	&dev_attr_l0s_aspm.attr,
1356 	&dev_attr_l1_aspm.attr,
1357 	&dev_attr_l1_1_aspm.attr,
1358 	&dev_attr_l1_2_aspm.attr,
1359 	&dev_attr_l1_1_pcipm.attr,
1360 	&dev_attr_l1_2_pcipm.attr,
1361 	NULL
1362 };
1363 
1364 static umode_t aspm_ctrl_attrs_are_visible(struct kobject *kobj,
1365 					   struct attribute *a, int n)
1366 {
1367 	struct device *dev = kobj_to_dev(kobj);
1368 	struct pci_dev *pdev = to_pci_dev(dev);
1369 	struct pcie_link_state *link = pcie_aspm_get_link(pdev);
1370 	static const u8 aspm_state_map[] = {
1371 		ASPM_STATE_L0S,
1372 		ASPM_STATE_L1,
1373 		ASPM_STATE_L1_1,
1374 		ASPM_STATE_L1_2,
1375 		ASPM_STATE_L1_1_PCIPM,
1376 		ASPM_STATE_L1_2_PCIPM,
1377 	};
1378 
1379 	if (aspm_disabled || !link)
1380 		return 0;
1381 
1382 	if (n == 0)
1383 		return link->clkpm_capable ? a->mode : 0;
1384 
1385 	return link->aspm_capable & aspm_state_map[n - 1] ? a->mode : 0;
1386 }
1387 
1388 const struct attribute_group aspm_ctrl_attr_group = {
1389 	.name = "link",
1390 	.attrs = aspm_ctrl_attrs,
1391 	.is_visible = aspm_ctrl_attrs_are_visible,
1392 };
1393 
1394 static int __init pcie_aspm_disable(char *str)
1395 {
1396 	if (!strcmp(str, "off")) {
1397 		aspm_policy = POLICY_DEFAULT;
1398 		aspm_disabled = 1;
1399 		aspm_support_enabled = false;
1400 		printk(KERN_INFO "PCIe ASPM is disabled\n");
1401 	} else if (!strcmp(str, "force")) {
1402 		aspm_force = 1;
1403 		printk(KERN_INFO "PCIe ASPM is forcibly enabled\n");
1404 	}
1405 	return 1;
1406 }
1407 
1408 __setup("pcie_aspm=", pcie_aspm_disable);
1409 
1410 void pcie_no_aspm(void)
1411 {
1412 	/*
1413 	 * Disabling ASPM is intended to prevent the kernel from modifying
1414 	 * existing hardware state, not to clear existing state. To that end:
1415 	 * (a) set policy to POLICY_DEFAULT in order to avoid changing state
1416 	 * (b) prevent userspace from changing policy
1417 	 */
1418 	if (!aspm_force) {
1419 		aspm_policy = POLICY_DEFAULT;
1420 		aspm_disabled = 1;
1421 	}
1422 }
1423 
1424 bool pcie_aspm_support_enabled(void)
1425 {
1426 	return aspm_support_enabled;
1427 }
1428