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