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