1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
3 * Copyright(c) 2019 Intel Corporation.
9 /* Time after which the timer interrupt will re-enable ASPM */
10 #define ASPM_TIMER_MS 1000
11 /* Time for which interrupts are ignored after a timer has been scheduled */
12 #define ASPM_RESCHED_TIMER_MS (ASPM_TIMER_MS / 2)
13 /* Two interrupts within this time trigger ASPM disable */
14 #define ASPM_TRIGGER_MS 1
15 #define ASPM_TRIGGER_NS (ASPM_TRIGGER_MS * 1000 * 1000ull)
16 #define ASPM_L1_SUPPORTED(reg) \
17 ((((reg) & PCI_EXP_LNKCAP_ASPMS) >> 10) & 0x2)
19 uint aspm_mode
= ASPM_MODE_DISABLED
;
20 module_param_named(aspm
, aspm_mode
, uint
, 0444);
21 MODULE_PARM_DESC(aspm
, "PCIe ASPM: 0: disable, 1: enable, 2: dynamic");
23 static bool aspm_hw_l1_supported(struct hfi1_devdata
*dd
)
25 struct pci_dev
*parent
= dd
->pcidev
->bus
->self
;
29 * If the driver does not have access to the upstream component,
30 * it cannot support ASPM L1 at all.
35 pcie_capability_read_dword(dd
->pcidev
, PCI_EXP_LNKCAP
, &dn
);
36 dn
= ASPM_L1_SUPPORTED(dn
);
38 pcie_capability_read_dword(parent
, PCI_EXP_LNKCAP
, &up
);
39 up
= ASPM_L1_SUPPORTED(up
);
41 /* ASPM works on A-step but is reported as not supported */
42 return (!!dn
|| is_ax(dd
)) && !!up
;
45 /* Set L1 entrance latency for slower entry to L1 */
46 static void aspm_hw_set_l1_ent_latency(struct hfi1_devdata
*dd
)
48 u32 l1_ent_lat
= 0x4u
;
51 pci_read_config_dword(dd
->pcidev
, PCIE_CFG_REG_PL3
, ®32
);
52 reg32
&= ~PCIE_CFG_REG_PL3_L1_ENT_LATENCY_SMASK
;
53 reg32
|= l1_ent_lat
<< PCIE_CFG_REG_PL3_L1_ENT_LATENCY_SHIFT
;
54 pci_write_config_dword(dd
->pcidev
, PCIE_CFG_REG_PL3
, reg32
);
57 static void aspm_hw_enable_l1(struct hfi1_devdata
*dd
)
59 struct pci_dev
*parent
= dd
->pcidev
->bus
->self
;
62 * If the driver does not have access to the upstream component,
63 * it cannot support ASPM L1 at all.
68 /* Enable ASPM L1 first in upstream component and then downstream */
69 pcie_capability_clear_and_set_word(parent
, PCI_EXP_LNKCTL
,
71 PCI_EXP_LNKCTL_ASPM_L1
);
72 pcie_capability_clear_and_set_word(dd
->pcidev
, PCI_EXP_LNKCTL
,
74 PCI_EXP_LNKCTL_ASPM_L1
);
77 void aspm_hw_disable_l1(struct hfi1_devdata
*dd
)
79 struct pci_dev
*parent
= dd
->pcidev
->bus
->self
;
81 /* Disable ASPM L1 first in downstream component and then upstream */
82 pcie_capability_clear_and_set_word(dd
->pcidev
, PCI_EXP_LNKCTL
,
83 PCI_EXP_LNKCTL_ASPMC
, 0x0);
85 pcie_capability_clear_and_set_word(parent
, PCI_EXP_LNKCTL
,
86 PCI_EXP_LNKCTL_ASPMC
, 0x0);
89 static void aspm_enable(struct hfi1_devdata
*dd
)
91 if (dd
->aspm_enabled
|| aspm_mode
== ASPM_MODE_DISABLED
||
95 aspm_hw_enable_l1(dd
);
96 dd
->aspm_enabled
= true;
99 static void aspm_disable(struct hfi1_devdata
*dd
)
101 if (!dd
->aspm_enabled
|| aspm_mode
== ASPM_MODE_ENABLED
)
104 aspm_hw_disable_l1(dd
);
105 dd
->aspm_enabled
= false;
108 static void aspm_disable_inc(struct hfi1_devdata
*dd
)
112 spin_lock_irqsave(&dd
->aspm_lock
, flags
);
114 atomic_inc(&dd
->aspm_disabled_cnt
);
115 spin_unlock_irqrestore(&dd
->aspm_lock
, flags
);
118 static void aspm_enable_dec(struct hfi1_devdata
*dd
)
122 spin_lock_irqsave(&dd
->aspm_lock
, flags
);
123 if (atomic_dec_and_test(&dd
->aspm_disabled_cnt
))
125 spin_unlock_irqrestore(&dd
->aspm_lock
, flags
);
128 /* ASPM processing for each receive context interrupt */
129 void __aspm_ctx_disable(struct hfi1_ctxtdata
*rcd
)
132 bool close_interrupts
;
136 spin_lock_irqsave(&rcd
->aspm_lock
, flags
);
137 /* PSM contexts are open */
138 if (!rcd
->aspm_intr_enable
)
141 prev
= rcd
->aspm_ts_last_intr
;
143 rcd
->aspm_ts_last_intr
= now
;
145 /* An interrupt pair close together in time */
146 close_interrupts
= ktime_to_ns(ktime_sub(now
, prev
)) < ASPM_TRIGGER_NS
;
148 /* Don't push out our timer till this much time has elapsed */
149 restart_timer
= ktime_to_ns(ktime_sub(now
, rcd
->aspm_ts_timer_sched
)) >
150 ASPM_RESCHED_TIMER_MS
* NSEC_PER_MSEC
;
151 restart_timer
= restart_timer
&& close_interrupts
;
153 /* Disable ASPM and schedule timer */
154 if (rcd
->aspm_enabled
&& close_interrupts
) {
155 aspm_disable_inc(rcd
->dd
);
156 rcd
->aspm_enabled
= false;
157 restart_timer
= true;
161 mod_timer(&rcd
->aspm_timer
,
162 jiffies
+ msecs_to_jiffies(ASPM_TIMER_MS
));
163 rcd
->aspm_ts_timer_sched
= now
;
166 spin_unlock_irqrestore(&rcd
->aspm_lock
, flags
);
169 /* Timer function for re-enabling ASPM in the absence of interrupt activity */
170 static void aspm_ctx_timer_function(struct timer_list
*t
)
172 struct hfi1_ctxtdata
*rcd
= from_timer(rcd
, t
, aspm_timer
);
175 spin_lock_irqsave(&rcd
->aspm_lock
, flags
);
176 aspm_enable_dec(rcd
->dd
);
177 rcd
->aspm_enabled
= true;
178 spin_unlock_irqrestore(&rcd
->aspm_lock
, flags
);
182 * Disable interrupt processing for verbs contexts when PSM or VNIC contexts
185 void aspm_disable_all(struct hfi1_devdata
*dd
)
187 struct hfi1_ctxtdata
*rcd
;
191 for (i
= 0; i
< dd
->first_dyn_alloc_ctxt
; i
++) {
192 rcd
= hfi1_rcd_get_by_index(dd
, i
);
194 del_timer_sync(&rcd
->aspm_timer
);
195 spin_lock_irqsave(&rcd
->aspm_lock
, flags
);
196 rcd
->aspm_intr_enable
= false;
197 spin_unlock_irqrestore(&rcd
->aspm_lock
, flags
);
203 atomic_set(&dd
->aspm_disabled_cnt
, 0);
206 /* Re-enable interrupt processing for verbs contexts */
207 void aspm_enable_all(struct hfi1_devdata
*dd
)
209 struct hfi1_ctxtdata
*rcd
;
215 if (aspm_mode
!= ASPM_MODE_DYNAMIC
)
218 for (i
= 0; i
< dd
->first_dyn_alloc_ctxt
; i
++) {
219 rcd
= hfi1_rcd_get_by_index(dd
, i
);
221 spin_lock_irqsave(&rcd
->aspm_lock
, flags
);
222 rcd
->aspm_intr_enable
= true;
223 rcd
->aspm_enabled
= true;
224 spin_unlock_irqrestore(&rcd
->aspm_lock
, flags
);
230 static void aspm_ctx_init(struct hfi1_ctxtdata
*rcd
)
232 spin_lock_init(&rcd
->aspm_lock
);
233 timer_setup(&rcd
->aspm_timer
, aspm_ctx_timer_function
, 0);
234 rcd
->aspm_intr_supported
= rcd
->dd
->aspm_supported
&&
235 aspm_mode
== ASPM_MODE_DYNAMIC
&&
236 rcd
->ctxt
< rcd
->dd
->first_dyn_alloc_ctxt
;
239 void aspm_init(struct hfi1_devdata
*dd
)
241 struct hfi1_ctxtdata
*rcd
;
244 spin_lock_init(&dd
->aspm_lock
);
245 dd
->aspm_supported
= aspm_hw_l1_supported(dd
);
247 for (i
= 0; i
< dd
->first_dyn_alloc_ctxt
; i
++) {
248 rcd
= hfi1_rcd_get_by_index(dd
, i
);
254 /* Start with ASPM disabled */
255 aspm_hw_set_l1_ent_latency(dd
);
256 dd
->aspm_enabled
= false;
257 aspm_hw_disable_l1(dd
);
259 /* Now turn on ASPM if configured */
263 void aspm_exit(struct hfi1_devdata
*dd
)
265 aspm_disable_all(dd
);
267 /* Turn on ASPM on exit to conserve power */