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