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