xref: /linux/drivers/gpu/drm/i915/display/intel_hotplug_irq.c (revision bcfe43f0ea77c42c2154fb79b99b7d1d82ac3231)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include "i915_drv.h"
7 #include "i915_reg.h"
8 #include "intel_de.h"
9 #include "intel_display_irq.h"
10 #include "intel_display_types.h"
11 #include "intel_dp_aux.h"
12 #include "intel_gmbus.h"
13 #include "intel_hotplug.h"
14 #include "intel_hotplug_irq.h"
15 
16 typedef bool (*long_pulse_detect_func)(enum hpd_pin pin, u32 val);
17 typedef u32 (*hotplug_enables_func)(struct intel_encoder *encoder);
18 typedef u32 (*hotplug_mask_func)(enum hpd_pin pin);
19 
20 static const u32 hpd_ilk[HPD_NUM_PINS] = {
21 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
22 };
23 
24 static const u32 hpd_ivb[HPD_NUM_PINS] = {
25 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
26 };
27 
28 static const u32 hpd_bdw[HPD_NUM_PINS] = {
29 	[HPD_PORT_A] = GEN8_DE_PORT_HOTPLUG(HPD_PORT_A),
30 };
31 
32 static const u32 hpd_ibx[HPD_NUM_PINS] = {
33 	[HPD_CRT] = SDE_CRT_HOTPLUG,
34 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
35 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
36 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
37 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG,
38 };
39 
40 static const u32 hpd_cpt[HPD_NUM_PINS] = {
41 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
42 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
43 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
44 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
45 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
46 };
47 
48 static const u32 hpd_spt[HPD_NUM_PINS] = {
49 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
50 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
51 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
52 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
53 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT,
54 };
55 
56 static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
57 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
58 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
59 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
60 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
61 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
62 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN,
63 };
64 
65 static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
66 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
67 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
68 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
69 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
70 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
71 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS,
72 };
73 
74 static const u32 hpd_status_i915[HPD_NUM_PINS] = {
75 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
76 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
77 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
78 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
79 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
80 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS,
81 };
82 
83 static const u32 hpd_bxt[HPD_NUM_PINS] = {
84 	[HPD_PORT_A] = GEN8_DE_PORT_HOTPLUG(HPD_PORT_A),
85 	[HPD_PORT_B] = GEN8_DE_PORT_HOTPLUG(HPD_PORT_B),
86 	[HPD_PORT_C] = GEN8_DE_PORT_HOTPLUG(HPD_PORT_C),
87 };
88 
89 static const u32 hpd_gen11[HPD_NUM_PINS] = {
90 	[HPD_PORT_TC1] = GEN11_TC_HOTPLUG(HPD_PORT_TC1) | GEN11_TBT_HOTPLUG(HPD_PORT_TC1),
91 	[HPD_PORT_TC2] = GEN11_TC_HOTPLUG(HPD_PORT_TC2) | GEN11_TBT_HOTPLUG(HPD_PORT_TC2),
92 	[HPD_PORT_TC3] = GEN11_TC_HOTPLUG(HPD_PORT_TC3) | GEN11_TBT_HOTPLUG(HPD_PORT_TC3),
93 	[HPD_PORT_TC4] = GEN11_TC_HOTPLUG(HPD_PORT_TC4) | GEN11_TBT_HOTPLUG(HPD_PORT_TC4),
94 	[HPD_PORT_TC5] = GEN11_TC_HOTPLUG(HPD_PORT_TC5) | GEN11_TBT_HOTPLUG(HPD_PORT_TC5),
95 	[HPD_PORT_TC6] = GEN11_TC_HOTPLUG(HPD_PORT_TC6) | GEN11_TBT_HOTPLUG(HPD_PORT_TC6),
96 };
97 
98 static const u32 hpd_xelpdp[HPD_NUM_PINS] = {
99 	[HPD_PORT_TC1] = XELPDP_TBT_HOTPLUG(HPD_PORT_TC1) | XELPDP_DP_ALT_HOTPLUG(HPD_PORT_TC1),
100 	[HPD_PORT_TC2] = XELPDP_TBT_HOTPLUG(HPD_PORT_TC2) | XELPDP_DP_ALT_HOTPLUG(HPD_PORT_TC2),
101 	[HPD_PORT_TC3] = XELPDP_TBT_HOTPLUG(HPD_PORT_TC3) | XELPDP_DP_ALT_HOTPLUG(HPD_PORT_TC3),
102 	[HPD_PORT_TC4] = XELPDP_TBT_HOTPLUG(HPD_PORT_TC4) | XELPDP_DP_ALT_HOTPLUG(HPD_PORT_TC4),
103 };
104 
105 static const u32 hpd_icp[HPD_NUM_PINS] = {
106 	[HPD_PORT_A] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_A),
107 	[HPD_PORT_B] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_B),
108 	[HPD_PORT_C] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_C),
109 	[HPD_PORT_TC1] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC1),
110 	[HPD_PORT_TC2] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC2),
111 	[HPD_PORT_TC3] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC3),
112 	[HPD_PORT_TC4] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC4),
113 	[HPD_PORT_TC5] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC5),
114 	[HPD_PORT_TC6] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC6),
115 };
116 
117 static const u32 hpd_sde_dg1[HPD_NUM_PINS] = {
118 	[HPD_PORT_A] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_A),
119 	[HPD_PORT_B] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_B),
120 	[HPD_PORT_C] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_C),
121 	[HPD_PORT_D] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_D),
122 	[HPD_PORT_TC1] = SDE_TC_HOTPLUG_DG2(HPD_PORT_TC1),
123 };
124 
125 static const u32 hpd_mtp[HPD_NUM_PINS] = {
126 	[HPD_PORT_A] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_A),
127 	[HPD_PORT_B] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_B),
128 	[HPD_PORT_TC1] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC1),
129 	[HPD_PORT_TC2] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC2),
130 	[HPD_PORT_TC3] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC3),
131 	[HPD_PORT_TC4] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC4),
132 };
133 
134 static void intel_hpd_init_pins(struct drm_i915_private *dev_priv)
135 {
136 	struct intel_hotplug *hpd = &dev_priv->display.hotplug;
137 
138 	if (HAS_GMCH(dev_priv)) {
139 		if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
140 		    IS_CHERRYVIEW(dev_priv))
141 			hpd->hpd = hpd_status_g4x;
142 		else
143 			hpd->hpd = hpd_status_i915;
144 		return;
145 	}
146 
147 	if (DISPLAY_VER(dev_priv) >= 14)
148 		hpd->hpd = hpd_xelpdp;
149 	else if (DISPLAY_VER(dev_priv) >= 11)
150 		hpd->hpd = hpd_gen11;
151 	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
152 		hpd->hpd = hpd_bxt;
153 	else if (DISPLAY_VER(dev_priv) == 9)
154 		hpd->hpd = NULL; /* no north HPD on SKL */
155 	else if (DISPLAY_VER(dev_priv) >= 8)
156 		hpd->hpd = hpd_bdw;
157 	else if (DISPLAY_VER(dev_priv) >= 7)
158 		hpd->hpd = hpd_ivb;
159 	else
160 		hpd->hpd = hpd_ilk;
161 
162 	if ((INTEL_PCH_TYPE(dev_priv) < PCH_DG1) &&
163 	    (!HAS_PCH_SPLIT(dev_priv) || HAS_PCH_NOP(dev_priv)))
164 		return;
165 
166 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_MTL)
167 		hpd->pch_hpd = hpd_mtp;
168 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_DG1)
169 		hpd->pch_hpd = hpd_sde_dg1;
170 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
171 		hpd->pch_hpd = hpd_icp;
172 	else if (HAS_PCH_CNP(dev_priv) || HAS_PCH_SPT(dev_priv))
173 		hpd->pch_hpd = hpd_spt;
174 	else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_CPT(dev_priv))
175 		hpd->pch_hpd = hpd_cpt;
176 	else if (HAS_PCH_IBX(dev_priv))
177 		hpd->pch_hpd = hpd_ibx;
178 	else
179 		MISSING_CASE(INTEL_PCH_TYPE(dev_priv));
180 }
181 
182 /* For display hotplug interrupt */
183 void i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
184 					  u32 mask, u32 bits)
185 {
186 	lockdep_assert_held(&dev_priv->irq_lock);
187 	drm_WARN_ON(&dev_priv->drm, bits & ~mask);
188 
189 	intel_uncore_rmw(&dev_priv->uncore, PORT_HOTPLUG_EN(dev_priv), mask,
190 			 bits);
191 }
192 
193 /**
194  * i915_hotplug_interrupt_update - update hotplug interrupt enable
195  * @dev_priv: driver private
196  * @mask: bits to update
197  * @bits: bits to enable
198  * NOTE: the HPD enable bits are modified both inside and outside
199  * of an interrupt context. To avoid that read-modify-write cycles
200  * interfer, these bits are protected by a spinlock. Since this
201  * function is usually not called from a context where the lock is
202  * held already, this function acquires the lock itself. A non-locking
203  * version is also available.
204  */
205 void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
206 				   u32 mask,
207 				   u32 bits)
208 {
209 	spin_lock_irq(&dev_priv->irq_lock);
210 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
211 	spin_unlock_irq(&dev_priv->irq_lock);
212 }
213 
214 static bool gen11_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
215 {
216 	switch (pin) {
217 	case HPD_PORT_TC1:
218 	case HPD_PORT_TC2:
219 	case HPD_PORT_TC3:
220 	case HPD_PORT_TC4:
221 	case HPD_PORT_TC5:
222 	case HPD_PORT_TC6:
223 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(pin);
224 	default:
225 		return false;
226 	}
227 }
228 
229 static bool bxt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
230 {
231 	switch (pin) {
232 	case HPD_PORT_A:
233 		return val & PORTA_HOTPLUG_LONG_DETECT;
234 	case HPD_PORT_B:
235 		return val & PORTB_HOTPLUG_LONG_DETECT;
236 	case HPD_PORT_C:
237 		return val & PORTC_HOTPLUG_LONG_DETECT;
238 	default:
239 		return false;
240 	}
241 }
242 
243 static bool icp_ddi_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
244 {
245 	switch (pin) {
246 	case HPD_PORT_A:
247 	case HPD_PORT_B:
248 	case HPD_PORT_C:
249 	case HPD_PORT_D:
250 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(pin);
251 	default:
252 		return false;
253 	}
254 }
255 
256 static bool icp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
257 {
258 	switch (pin) {
259 	case HPD_PORT_TC1:
260 	case HPD_PORT_TC2:
261 	case HPD_PORT_TC3:
262 	case HPD_PORT_TC4:
263 	case HPD_PORT_TC5:
264 	case HPD_PORT_TC6:
265 		return val & ICP_TC_HPD_LONG_DETECT(pin);
266 	default:
267 		return false;
268 	}
269 }
270 
271 static bool spt_port_hotplug2_long_detect(enum hpd_pin pin, u32 val)
272 {
273 	switch (pin) {
274 	case HPD_PORT_E:
275 		return val & PORTE_HOTPLUG_LONG_DETECT;
276 	default:
277 		return false;
278 	}
279 }
280 
281 static bool spt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
282 {
283 	switch (pin) {
284 	case HPD_PORT_A:
285 		return val & PORTA_HOTPLUG_LONG_DETECT;
286 	case HPD_PORT_B:
287 		return val & PORTB_HOTPLUG_LONG_DETECT;
288 	case HPD_PORT_C:
289 		return val & PORTC_HOTPLUG_LONG_DETECT;
290 	case HPD_PORT_D:
291 		return val & PORTD_HOTPLUG_LONG_DETECT;
292 	default:
293 		return false;
294 	}
295 }
296 
297 static bool ilk_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
298 {
299 	switch (pin) {
300 	case HPD_PORT_A:
301 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
302 	default:
303 		return false;
304 	}
305 }
306 
307 static bool pch_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
308 {
309 	switch (pin) {
310 	case HPD_PORT_B:
311 		return val & PORTB_HOTPLUG_LONG_DETECT;
312 	case HPD_PORT_C:
313 		return val & PORTC_HOTPLUG_LONG_DETECT;
314 	case HPD_PORT_D:
315 		return val & PORTD_HOTPLUG_LONG_DETECT;
316 	default:
317 		return false;
318 	}
319 }
320 
321 static bool i9xx_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
322 {
323 	switch (pin) {
324 	case HPD_PORT_B:
325 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
326 	case HPD_PORT_C:
327 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
328 	case HPD_PORT_D:
329 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
330 	default:
331 		return false;
332 	}
333 }
334 
335 /*
336  * Get a bit mask of pins that have triggered, and which ones may be long.
337  * This can be called multiple times with the same masks to accumulate
338  * hotplug detection results from several registers.
339  *
340  * Note that the caller is expected to zero out the masks initially.
341  */
342 static void intel_get_hpd_pins(struct drm_i915_private *dev_priv,
343 			       u32 *pin_mask, u32 *long_mask,
344 			       u32 hotplug_trigger, u32 dig_hotplug_reg,
345 			       const u32 hpd[HPD_NUM_PINS],
346 			       bool long_pulse_detect(enum hpd_pin pin, u32 val))
347 {
348 	enum hpd_pin pin;
349 
350 	BUILD_BUG_ON(BITS_PER_TYPE(*pin_mask) < HPD_NUM_PINS);
351 
352 	for_each_hpd_pin(pin) {
353 		if ((hpd[pin] & hotplug_trigger) == 0)
354 			continue;
355 
356 		*pin_mask |= BIT(pin);
357 
358 		if (long_pulse_detect(pin, dig_hotplug_reg))
359 			*long_mask |= BIT(pin);
360 	}
361 
362 	drm_dbg(&dev_priv->drm,
363 		"hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x, long 0x%08x\n",
364 		hotplug_trigger, dig_hotplug_reg, *pin_mask, *long_mask);
365 }
366 
367 static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
368 				  const u32 hpd[HPD_NUM_PINS])
369 {
370 	struct intel_encoder *encoder;
371 	u32 enabled_irqs = 0;
372 
373 	for_each_intel_encoder(&dev_priv->drm, encoder)
374 		if (dev_priv->display.hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
375 			enabled_irqs |= hpd[encoder->hpd_pin];
376 
377 	return enabled_irqs;
378 }
379 
380 static u32 intel_hpd_hotplug_irqs(struct drm_i915_private *dev_priv,
381 				  const u32 hpd[HPD_NUM_PINS])
382 {
383 	struct intel_encoder *encoder;
384 	u32 hotplug_irqs = 0;
385 
386 	for_each_intel_encoder(&dev_priv->drm, encoder)
387 		hotplug_irqs |= hpd[encoder->hpd_pin];
388 
389 	return hotplug_irqs;
390 }
391 
392 static u32 intel_hpd_hotplug_mask(struct drm_i915_private *i915,
393 				  hotplug_mask_func hotplug_mask)
394 {
395 	enum hpd_pin pin;
396 	u32 hotplug = 0;
397 
398 	for_each_hpd_pin(pin)
399 		hotplug |= hotplug_mask(pin);
400 
401 	return hotplug;
402 }
403 
404 static u32 intel_hpd_hotplug_enables(struct drm_i915_private *i915,
405 				     hotplug_enables_func hotplug_enables)
406 {
407 	struct intel_encoder *encoder;
408 	u32 hotplug = 0;
409 
410 	for_each_intel_encoder(&i915->drm, encoder)
411 		hotplug |= hotplug_enables(encoder);
412 
413 	return hotplug;
414 }
415 
416 u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
417 {
418 	u32 hotplug_status = 0, hotplug_status_mask;
419 	int i;
420 
421 	if (IS_G4X(dev_priv) ||
422 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
423 		hotplug_status_mask = HOTPLUG_INT_STATUS_G4X |
424 			DP_AUX_CHANNEL_MASK_INT_STATUS_G4X;
425 	else
426 		hotplug_status_mask = HOTPLUG_INT_STATUS_I915;
427 
428 	/*
429 	 * We absolutely have to clear all the pending interrupt
430 	 * bits in PORT_HOTPLUG_STAT. Otherwise the ISR port
431 	 * interrupt bit won't have an edge, and the i965/g4x
432 	 * edge triggered IIR will not notice that an interrupt
433 	 * is still pending. We can't use PORT_HOTPLUG_EN to
434 	 * guarantee the edge as the act of toggling the enable
435 	 * bits can itself generate a new hotplug interrupt :(
436 	 */
437 	for (i = 0; i < 10; i++) {
438 		u32 tmp = intel_uncore_read(&dev_priv->uncore,
439 					    PORT_HOTPLUG_STAT(dev_priv)) & hotplug_status_mask;
440 
441 		if (tmp == 0)
442 			return hotplug_status;
443 
444 		hotplug_status |= tmp;
445 		intel_uncore_write(&dev_priv->uncore,
446 				   PORT_HOTPLUG_STAT(dev_priv),
447 				   hotplug_status);
448 	}
449 
450 	drm_WARN_ONCE(&dev_priv->drm, 1,
451 		      "PORT_HOTPLUG_STAT did not clear (0x%08x)\n",
452 		      intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_STAT(dev_priv)));
453 
454 	return hotplug_status;
455 }
456 
457 void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 hotplug_status)
458 {
459 	struct intel_display *display = &dev_priv->display;
460 	u32 pin_mask = 0, long_mask = 0;
461 	u32 hotplug_trigger;
462 
463 	if (IS_G4X(dev_priv) ||
464 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
465 		hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
466 	else
467 		hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
468 
469 	if (hotplug_trigger) {
470 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
471 				   hotplug_trigger, hotplug_trigger,
472 				   dev_priv->display.hotplug.hpd,
473 				   i9xx_port_hotplug_long_detect);
474 
475 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
476 	}
477 
478 	if ((IS_G4X(dev_priv) ||
479 	     IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
480 	    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
481 		intel_dp_aux_irq_handler(display);
482 }
483 
484 void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 hotplug_trigger)
485 {
486 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
487 
488 	/*
489 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
490 	 * unless we touch the hotplug register, even if hotplug_trigger is
491 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
492 	 * errors.
493 	 */
494 	dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
495 	if (!hotplug_trigger) {
496 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
497 			PORTD_HOTPLUG_STATUS_MASK |
498 			PORTC_HOTPLUG_STATUS_MASK |
499 			PORTB_HOTPLUG_STATUS_MASK;
500 		dig_hotplug_reg &= ~mask;
501 	}
502 
503 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, dig_hotplug_reg);
504 	if (!hotplug_trigger)
505 		return;
506 
507 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
508 			   hotplug_trigger, dig_hotplug_reg,
509 			   dev_priv->display.hotplug.pch_hpd,
510 			   pch_port_hotplug_long_detect);
511 
512 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
513 }
514 
515 void xelpdp_pica_irq_handler(struct drm_i915_private *i915, u32 iir)
516 {
517 	struct intel_display *display = &i915->display;
518 	enum hpd_pin pin;
519 	u32 hotplug_trigger = iir & (XELPDP_DP_ALT_HOTPLUG_MASK | XELPDP_TBT_HOTPLUG_MASK);
520 	u32 trigger_aux = iir & XELPDP_AUX_TC_MASK;
521 	u32 pin_mask = 0, long_mask = 0;
522 
523 	if (DISPLAY_VER(i915) >= 20)
524 		trigger_aux |= iir & XE2LPD_AUX_DDI_MASK;
525 
526 	for (pin = HPD_PORT_TC1; pin <= HPD_PORT_TC4; pin++) {
527 		u32 val;
528 
529 		if (!(i915->display.hotplug.hpd[pin] & hotplug_trigger))
530 			continue;
531 
532 		pin_mask |= BIT(pin);
533 
534 		val = intel_de_read(i915, XELPDP_PORT_HOTPLUG_CTL(pin));
535 		intel_de_write(i915, XELPDP_PORT_HOTPLUG_CTL(pin), val);
536 
537 		if (val & (XELPDP_DP_ALT_HPD_LONG_DETECT | XELPDP_TBT_HPD_LONG_DETECT))
538 			long_mask |= BIT(pin);
539 	}
540 
541 	if (pin_mask) {
542 		drm_dbg(&i915->drm,
543 			"pica hotplug event received, stat 0x%08x, pins 0x%08x, long 0x%08x\n",
544 			hotplug_trigger, pin_mask, long_mask);
545 
546 		intel_hpd_irq_handler(i915, pin_mask, long_mask);
547 	}
548 
549 	if (trigger_aux)
550 		intel_dp_aux_irq_handler(display);
551 
552 	if (!pin_mask && !trigger_aux)
553 		drm_err(&i915->drm,
554 			"Unexpected DE HPD/AUX interrupt 0x%08x\n", iir);
555 }
556 
557 void icp_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
558 {
559 	u32 ddi_hotplug_trigger = pch_iir & SDE_DDI_HOTPLUG_MASK_ICP;
560 	u32 tc_hotplug_trigger = pch_iir & SDE_TC_HOTPLUG_MASK_ICP;
561 	u32 pin_mask = 0, long_mask = 0;
562 
563 	if (ddi_hotplug_trigger) {
564 		u32 dig_hotplug_reg;
565 
566 		/* Locking due to DSI native GPIO sequences */
567 		spin_lock(&dev_priv->irq_lock);
568 		dig_hotplug_reg = intel_uncore_rmw(&dev_priv->uncore, SHOTPLUG_CTL_DDI, 0, 0);
569 		spin_unlock(&dev_priv->irq_lock);
570 
571 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
572 				   ddi_hotplug_trigger, dig_hotplug_reg,
573 				   dev_priv->display.hotplug.pch_hpd,
574 				   icp_ddi_port_hotplug_long_detect);
575 	}
576 
577 	if (tc_hotplug_trigger) {
578 		u32 dig_hotplug_reg;
579 
580 		dig_hotplug_reg = intel_uncore_rmw(&dev_priv->uncore, SHOTPLUG_CTL_TC, 0, 0);
581 
582 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
583 				   tc_hotplug_trigger, dig_hotplug_reg,
584 				   dev_priv->display.hotplug.pch_hpd,
585 				   icp_tc_port_hotplug_long_detect);
586 	}
587 
588 	if (pin_mask)
589 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
590 
591 	if (pch_iir & SDE_GMBUS_ICP)
592 		intel_gmbus_irq_handler(dev_priv);
593 }
594 
595 void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
596 {
597 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
598 		~SDE_PORTE_HOTPLUG_SPT;
599 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
600 	u32 pin_mask = 0, long_mask = 0;
601 
602 	if (hotplug_trigger) {
603 		u32 dig_hotplug_reg;
604 
605 		dig_hotplug_reg = intel_uncore_rmw(&dev_priv->uncore, PCH_PORT_HOTPLUG, 0, 0);
606 
607 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
608 				   hotplug_trigger, dig_hotplug_reg,
609 				   dev_priv->display.hotplug.pch_hpd,
610 				   spt_port_hotplug_long_detect);
611 	}
612 
613 	if (hotplug2_trigger) {
614 		u32 dig_hotplug_reg;
615 
616 		dig_hotplug_reg = intel_uncore_rmw(&dev_priv->uncore, PCH_PORT_HOTPLUG2, 0, 0);
617 
618 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
619 				   hotplug2_trigger, dig_hotplug_reg,
620 				   dev_priv->display.hotplug.pch_hpd,
621 				   spt_port_hotplug2_long_detect);
622 	}
623 
624 	if (pin_mask)
625 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
626 
627 	if (pch_iir & SDE_GMBUS_CPT)
628 		intel_gmbus_irq_handler(dev_priv);
629 }
630 
631 void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 hotplug_trigger)
632 {
633 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
634 
635 	dig_hotplug_reg = intel_uncore_rmw(&dev_priv->uncore, DIGITAL_PORT_HOTPLUG_CNTRL, 0, 0);
636 
637 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
638 			   hotplug_trigger, dig_hotplug_reg,
639 			   dev_priv->display.hotplug.hpd,
640 			   ilk_port_hotplug_long_detect);
641 
642 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
643 }
644 
645 void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 hotplug_trigger)
646 {
647 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
648 
649 	dig_hotplug_reg = intel_uncore_rmw(&dev_priv->uncore, PCH_PORT_HOTPLUG, 0, 0);
650 
651 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
652 			   hotplug_trigger, dig_hotplug_reg,
653 			   dev_priv->display.hotplug.hpd,
654 			   bxt_port_hotplug_long_detect);
655 
656 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
657 }
658 
659 void gen11_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
660 {
661 	u32 pin_mask = 0, long_mask = 0;
662 	u32 trigger_tc = iir & GEN11_DE_TC_HOTPLUG_MASK;
663 	u32 trigger_tbt = iir & GEN11_DE_TBT_HOTPLUG_MASK;
664 
665 	if (trigger_tc) {
666 		u32 dig_hotplug_reg;
667 
668 		dig_hotplug_reg = intel_uncore_rmw(&dev_priv->uncore, GEN11_TC_HOTPLUG_CTL, 0, 0);
669 
670 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
671 				   trigger_tc, dig_hotplug_reg,
672 				   dev_priv->display.hotplug.hpd,
673 				   gen11_port_hotplug_long_detect);
674 	}
675 
676 	if (trigger_tbt) {
677 		u32 dig_hotplug_reg;
678 
679 		dig_hotplug_reg = intel_uncore_rmw(&dev_priv->uncore, GEN11_TBT_HOTPLUG_CTL, 0, 0);
680 
681 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
682 				   trigger_tbt, dig_hotplug_reg,
683 				   dev_priv->display.hotplug.hpd,
684 				   gen11_port_hotplug_long_detect);
685 	}
686 
687 	if (pin_mask)
688 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
689 	else
690 		drm_err(&dev_priv->drm,
691 			"Unexpected DE HPD interrupt 0x%08x\n", iir);
692 }
693 
694 static u32 ibx_hotplug_mask(enum hpd_pin hpd_pin)
695 {
696 	switch (hpd_pin) {
697 	case HPD_PORT_A:
698 		return PORTA_HOTPLUG_ENABLE;
699 	case HPD_PORT_B:
700 		return PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_MASK;
701 	case HPD_PORT_C:
702 		return PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_MASK;
703 	case HPD_PORT_D:
704 		return PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_MASK;
705 	default:
706 		return 0;
707 	}
708 }
709 
710 static u32 ibx_hotplug_enables(struct intel_encoder *encoder)
711 {
712 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
713 
714 	switch (encoder->hpd_pin) {
715 	case HPD_PORT_A:
716 		/*
717 		 * When CPU and PCH are on the same package, port A
718 		 * HPD must be enabled in both north and south.
719 		 */
720 		return HAS_PCH_LPT_LP(i915) ?
721 			PORTA_HOTPLUG_ENABLE : 0;
722 	case HPD_PORT_B:
723 		return PORTB_HOTPLUG_ENABLE |
724 			PORTB_PULSE_DURATION_2ms;
725 	case HPD_PORT_C:
726 		return PORTC_HOTPLUG_ENABLE |
727 			PORTC_PULSE_DURATION_2ms;
728 	case HPD_PORT_D:
729 		return PORTD_HOTPLUG_ENABLE |
730 			PORTD_PULSE_DURATION_2ms;
731 	default:
732 		return 0;
733 	}
734 }
735 
736 static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
737 {
738 	/*
739 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
740 	 * duration to 2ms (which is the minimum in the Display Port spec).
741 	 * The pulse duration bits are reserved on LPT+.
742 	 */
743 	intel_uncore_rmw(&dev_priv->uncore, PCH_PORT_HOTPLUG,
744 			 intel_hpd_hotplug_mask(dev_priv, ibx_hotplug_mask),
745 			 intel_hpd_hotplug_enables(dev_priv, ibx_hotplug_enables));
746 }
747 
748 static void ibx_hpd_enable_detection(struct intel_encoder *encoder)
749 {
750 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
751 
752 	intel_uncore_rmw(&i915->uncore, PCH_PORT_HOTPLUG,
753 			 ibx_hotplug_mask(encoder->hpd_pin),
754 			 ibx_hotplug_enables(encoder));
755 }
756 
757 static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
758 {
759 	u32 hotplug_irqs, enabled_irqs;
760 
761 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->display.hotplug.pch_hpd);
762 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->display.hotplug.pch_hpd);
763 
764 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
765 
766 	ibx_hpd_detection_setup(dev_priv);
767 }
768 
769 static u32 icp_ddi_hotplug_mask(enum hpd_pin hpd_pin)
770 {
771 	switch (hpd_pin) {
772 	case HPD_PORT_A:
773 	case HPD_PORT_B:
774 	case HPD_PORT_C:
775 	case HPD_PORT_D:
776 		return SHOTPLUG_CTL_DDI_HPD_ENABLE(hpd_pin);
777 	default:
778 		return 0;
779 	}
780 }
781 
782 static u32 icp_ddi_hotplug_enables(struct intel_encoder *encoder)
783 {
784 	return icp_ddi_hotplug_mask(encoder->hpd_pin);
785 }
786 
787 static u32 icp_tc_hotplug_mask(enum hpd_pin hpd_pin)
788 {
789 	switch (hpd_pin) {
790 	case HPD_PORT_TC1:
791 	case HPD_PORT_TC2:
792 	case HPD_PORT_TC3:
793 	case HPD_PORT_TC4:
794 	case HPD_PORT_TC5:
795 	case HPD_PORT_TC6:
796 		return ICP_TC_HPD_ENABLE(hpd_pin);
797 	default:
798 		return 0;
799 	}
800 }
801 
802 static u32 icp_tc_hotplug_enables(struct intel_encoder *encoder)
803 {
804 	return icp_tc_hotplug_mask(encoder->hpd_pin);
805 }
806 
807 static void icp_ddi_hpd_detection_setup(struct drm_i915_private *dev_priv)
808 {
809 	intel_uncore_rmw(&dev_priv->uncore, SHOTPLUG_CTL_DDI,
810 			 intel_hpd_hotplug_mask(dev_priv, icp_ddi_hotplug_mask),
811 			 intel_hpd_hotplug_enables(dev_priv, icp_ddi_hotplug_enables));
812 }
813 
814 static void icp_ddi_hpd_enable_detection(struct intel_encoder *encoder)
815 {
816 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
817 
818 	intel_uncore_rmw(&i915->uncore, SHOTPLUG_CTL_DDI,
819 			 icp_ddi_hotplug_mask(encoder->hpd_pin),
820 			 icp_ddi_hotplug_enables(encoder));
821 }
822 
823 static void icp_tc_hpd_detection_setup(struct drm_i915_private *dev_priv)
824 {
825 	intel_uncore_rmw(&dev_priv->uncore, SHOTPLUG_CTL_TC,
826 			 intel_hpd_hotplug_mask(dev_priv, icp_tc_hotplug_mask),
827 			 intel_hpd_hotplug_enables(dev_priv, icp_tc_hotplug_enables));
828 }
829 
830 static void icp_tc_hpd_enable_detection(struct intel_encoder *encoder)
831 {
832 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
833 
834 	intel_uncore_rmw(&i915->uncore, SHOTPLUG_CTL_TC,
835 			 icp_tc_hotplug_mask(encoder->hpd_pin),
836 			 icp_tc_hotplug_enables(encoder));
837 }
838 
839 static void icp_hpd_enable_detection(struct intel_encoder *encoder)
840 {
841 	icp_ddi_hpd_enable_detection(encoder);
842 	icp_tc_hpd_enable_detection(encoder);
843 }
844 
845 static void icp_hpd_irq_setup(struct drm_i915_private *dev_priv)
846 {
847 	u32 hotplug_irqs, enabled_irqs;
848 
849 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->display.hotplug.pch_hpd);
850 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->display.hotplug.pch_hpd);
851 
852 	/*
853 	 * We reduce the value to 250us to be able to detect SHPD when an external display
854 	 * is connected. This is also expected of us as stated in DP1.4a Table 3-4.
855 	 */
856 	intel_uncore_write(&dev_priv->uncore, SHPD_FILTER_CNT, SHPD_FILTER_CNT_250);
857 
858 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
859 
860 	icp_ddi_hpd_detection_setup(dev_priv);
861 	icp_tc_hpd_detection_setup(dev_priv);
862 }
863 
864 static u32 gen11_hotplug_mask(enum hpd_pin hpd_pin)
865 {
866 	switch (hpd_pin) {
867 	case HPD_PORT_TC1:
868 	case HPD_PORT_TC2:
869 	case HPD_PORT_TC3:
870 	case HPD_PORT_TC4:
871 	case HPD_PORT_TC5:
872 	case HPD_PORT_TC6:
873 		return GEN11_HOTPLUG_CTL_ENABLE(hpd_pin);
874 	default:
875 		return 0;
876 	}
877 }
878 
879 static u32 gen11_hotplug_enables(struct intel_encoder *encoder)
880 {
881 	return gen11_hotplug_mask(encoder->hpd_pin);
882 }
883 
884 static void dg1_hpd_invert(struct drm_i915_private *i915)
885 {
886 	u32 val = (INVERT_DDIA_HPD |
887 		   INVERT_DDIB_HPD |
888 		   INVERT_DDIC_HPD |
889 		   INVERT_DDID_HPD);
890 	intel_uncore_rmw(&i915->uncore, SOUTH_CHICKEN1, 0, val);
891 }
892 
893 static void dg1_hpd_enable_detection(struct intel_encoder *encoder)
894 {
895 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
896 
897 	dg1_hpd_invert(i915);
898 	icp_hpd_enable_detection(encoder);
899 }
900 
901 static void dg1_hpd_irq_setup(struct drm_i915_private *dev_priv)
902 {
903 	dg1_hpd_invert(dev_priv);
904 	icp_hpd_irq_setup(dev_priv);
905 }
906 
907 static void gen11_tc_hpd_detection_setup(struct drm_i915_private *dev_priv)
908 {
909 	intel_uncore_rmw(&dev_priv->uncore, GEN11_TC_HOTPLUG_CTL,
910 			 intel_hpd_hotplug_mask(dev_priv, gen11_hotplug_mask),
911 			 intel_hpd_hotplug_enables(dev_priv, gen11_hotplug_enables));
912 }
913 
914 static void gen11_tc_hpd_enable_detection(struct intel_encoder *encoder)
915 {
916 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
917 
918 	intel_uncore_rmw(&i915->uncore, GEN11_TC_HOTPLUG_CTL,
919 			 gen11_hotplug_mask(encoder->hpd_pin),
920 			 gen11_hotplug_enables(encoder));
921 }
922 
923 static void gen11_tbt_hpd_detection_setup(struct drm_i915_private *dev_priv)
924 {
925 	intel_uncore_rmw(&dev_priv->uncore, GEN11_TBT_HOTPLUG_CTL,
926 			 intel_hpd_hotplug_mask(dev_priv, gen11_hotplug_mask),
927 			 intel_hpd_hotplug_enables(dev_priv, gen11_hotplug_enables));
928 }
929 
930 static void gen11_tbt_hpd_enable_detection(struct intel_encoder *encoder)
931 {
932 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
933 
934 	intel_uncore_rmw(&i915->uncore, GEN11_TBT_HOTPLUG_CTL,
935 			 gen11_hotplug_mask(encoder->hpd_pin),
936 			 gen11_hotplug_enables(encoder));
937 }
938 
939 static void gen11_hpd_enable_detection(struct intel_encoder *encoder)
940 {
941 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
942 
943 	gen11_tc_hpd_enable_detection(encoder);
944 	gen11_tbt_hpd_enable_detection(encoder);
945 
946 	if (INTEL_PCH_TYPE(i915) >= PCH_ICP)
947 		icp_hpd_enable_detection(encoder);
948 }
949 
950 static void gen11_hpd_irq_setup(struct drm_i915_private *dev_priv)
951 {
952 	u32 hotplug_irqs, enabled_irqs;
953 
954 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->display.hotplug.hpd);
955 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->display.hotplug.hpd);
956 
957 	intel_uncore_rmw(&dev_priv->uncore, GEN11_DE_HPD_IMR, hotplug_irqs,
958 			 ~enabled_irqs & hotplug_irqs);
959 	intel_uncore_posting_read(&dev_priv->uncore, GEN11_DE_HPD_IMR);
960 
961 	gen11_tc_hpd_detection_setup(dev_priv);
962 	gen11_tbt_hpd_detection_setup(dev_priv);
963 
964 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
965 		icp_hpd_irq_setup(dev_priv);
966 }
967 
968 static u32 mtp_ddi_hotplug_mask(enum hpd_pin hpd_pin)
969 {
970 	switch (hpd_pin) {
971 	case HPD_PORT_A:
972 	case HPD_PORT_B:
973 		return SHOTPLUG_CTL_DDI_HPD_ENABLE(hpd_pin);
974 	default:
975 		return 0;
976 	}
977 }
978 
979 static u32 mtp_ddi_hotplug_enables(struct intel_encoder *encoder)
980 {
981 	return mtp_ddi_hotplug_mask(encoder->hpd_pin);
982 }
983 
984 static u32 mtp_tc_hotplug_mask(enum hpd_pin hpd_pin)
985 {
986 	switch (hpd_pin) {
987 	case HPD_PORT_TC1:
988 	case HPD_PORT_TC2:
989 	case HPD_PORT_TC3:
990 	case HPD_PORT_TC4:
991 		return ICP_TC_HPD_ENABLE(hpd_pin);
992 	default:
993 		return 0;
994 	}
995 }
996 
997 static u32 mtp_tc_hotplug_enables(struct intel_encoder *encoder)
998 {
999 	return mtp_tc_hotplug_mask(encoder->hpd_pin);
1000 }
1001 
1002 static void mtp_ddi_hpd_detection_setup(struct drm_i915_private *i915)
1003 {
1004 	intel_de_rmw(i915, SHOTPLUG_CTL_DDI,
1005 		     intel_hpd_hotplug_mask(i915, mtp_ddi_hotplug_mask),
1006 		     intel_hpd_hotplug_enables(i915, mtp_ddi_hotplug_enables));
1007 }
1008 
1009 static void mtp_ddi_hpd_enable_detection(struct intel_encoder *encoder)
1010 {
1011 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1012 
1013 	intel_de_rmw(i915, SHOTPLUG_CTL_DDI,
1014 		     mtp_ddi_hotplug_mask(encoder->hpd_pin),
1015 		     mtp_ddi_hotplug_enables(encoder));
1016 }
1017 
1018 static void mtp_tc_hpd_detection_setup(struct drm_i915_private *i915)
1019 {
1020 	intel_de_rmw(i915, SHOTPLUG_CTL_TC,
1021 		     intel_hpd_hotplug_mask(i915, mtp_tc_hotplug_mask),
1022 		     intel_hpd_hotplug_enables(i915, mtp_tc_hotplug_enables));
1023 }
1024 
1025 static void mtp_tc_hpd_enable_detection(struct intel_encoder *encoder)
1026 {
1027 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1028 
1029 	intel_de_rmw(i915, SHOTPLUG_CTL_DDI,
1030 		     mtp_tc_hotplug_mask(encoder->hpd_pin),
1031 		     mtp_tc_hotplug_enables(encoder));
1032 }
1033 
1034 static void mtp_hpd_invert(struct drm_i915_private *i915)
1035 {
1036 	u32 val = (INVERT_DDIA_HPD |
1037 		   INVERT_DDIB_HPD |
1038 		   INVERT_DDIC_HPD |
1039 		   INVERT_TC1_HPD |
1040 		   INVERT_TC2_HPD |
1041 		   INVERT_TC3_HPD |
1042 		   INVERT_TC4_HPD |
1043 		   INVERT_DDID_HPD_MTP |
1044 		   INVERT_DDIE_HPD);
1045 	intel_de_rmw(i915, SOUTH_CHICKEN1, 0, val);
1046 }
1047 
1048 static void mtp_hpd_enable_detection(struct intel_encoder *encoder)
1049 {
1050 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1051 
1052 	mtp_hpd_invert(i915);
1053 	mtp_ddi_hpd_enable_detection(encoder);
1054 	mtp_tc_hpd_enable_detection(encoder);
1055 }
1056 
1057 static void mtp_hpd_irq_setup(struct drm_i915_private *i915)
1058 {
1059 	u32 hotplug_irqs, enabled_irqs;
1060 
1061 	enabled_irqs = intel_hpd_enabled_irqs(i915, i915->display.hotplug.pch_hpd);
1062 	hotplug_irqs = intel_hpd_hotplug_irqs(i915, i915->display.hotplug.pch_hpd);
1063 
1064 	/*
1065 	 * Use 250us here to align with the DP1.4a(Table 3-4) spec as to what the
1066 	 * SHPD_FILTER_CNT value should be.
1067 	 */
1068 	intel_de_write(i915, SHPD_FILTER_CNT, SHPD_FILTER_CNT_250);
1069 
1070 	mtp_hpd_invert(i915);
1071 	ibx_display_interrupt_update(i915, hotplug_irqs, enabled_irqs);
1072 
1073 	mtp_ddi_hpd_detection_setup(i915);
1074 	mtp_tc_hpd_detection_setup(i915);
1075 }
1076 
1077 static void xe2lpd_sde_hpd_irq_setup(struct drm_i915_private *i915)
1078 {
1079 	u32 hotplug_irqs, enabled_irqs;
1080 
1081 	enabled_irqs = intel_hpd_enabled_irqs(i915, i915->display.hotplug.pch_hpd);
1082 	hotplug_irqs = intel_hpd_hotplug_irqs(i915, i915->display.hotplug.pch_hpd);
1083 
1084 	ibx_display_interrupt_update(i915, hotplug_irqs, enabled_irqs);
1085 
1086 	mtp_ddi_hpd_detection_setup(i915);
1087 	mtp_tc_hpd_detection_setup(i915);
1088 }
1089 
1090 static bool is_xelpdp_pica_hpd_pin(enum hpd_pin hpd_pin)
1091 {
1092 	return hpd_pin >= HPD_PORT_TC1 && hpd_pin <= HPD_PORT_TC4;
1093 }
1094 
1095 static void _xelpdp_pica_hpd_detection_setup(struct drm_i915_private *i915,
1096 					     enum hpd_pin hpd_pin, bool enable)
1097 {
1098 	u32 mask = XELPDP_TBT_HOTPLUG_ENABLE |
1099 		XELPDP_DP_ALT_HOTPLUG_ENABLE;
1100 
1101 	if (!is_xelpdp_pica_hpd_pin(hpd_pin))
1102 		return;
1103 
1104 	intel_de_rmw(i915, XELPDP_PORT_HOTPLUG_CTL(hpd_pin),
1105 		     mask, enable ? mask : 0);
1106 }
1107 
1108 static void xelpdp_pica_hpd_enable_detection(struct intel_encoder *encoder)
1109 {
1110 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1111 
1112 	_xelpdp_pica_hpd_detection_setup(i915, encoder->hpd_pin, true);
1113 }
1114 
1115 static void xelpdp_pica_hpd_detection_setup(struct drm_i915_private *i915)
1116 {
1117 	struct intel_encoder *encoder;
1118 	u32 available_pins = 0;
1119 	enum hpd_pin pin;
1120 
1121 	BUILD_BUG_ON(BITS_PER_TYPE(available_pins) < HPD_NUM_PINS);
1122 
1123 	for_each_intel_encoder(&i915->drm, encoder)
1124 		available_pins |= BIT(encoder->hpd_pin);
1125 
1126 	for_each_hpd_pin(pin)
1127 		_xelpdp_pica_hpd_detection_setup(i915, pin, available_pins & BIT(pin));
1128 }
1129 
1130 static void xelpdp_hpd_enable_detection(struct intel_encoder *encoder)
1131 {
1132 	xelpdp_pica_hpd_enable_detection(encoder);
1133 	mtp_hpd_enable_detection(encoder);
1134 }
1135 
1136 static void xelpdp_hpd_irq_setup(struct drm_i915_private *i915)
1137 {
1138 	u32 hotplug_irqs, enabled_irqs;
1139 
1140 	enabled_irqs = intel_hpd_enabled_irqs(i915, i915->display.hotplug.hpd);
1141 	hotplug_irqs = intel_hpd_hotplug_irqs(i915, i915->display.hotplug.hpd);
1142 
1143 	intel_de_rmw(i915, PICAINTERRUPT_IMR, hotplug_irqs,
1144 		     ~enabled_irqs & hotplug_irqs);
1145 	intel_uncore_posting_read(&i915->uncore, PICAINTERRUPT_IMR);
1146 
1147 	xelpdp_pica_hpd_detection_setup(i915);
1148 
1149 	if (INTEL_PCH_TYPE(i915) >= PCH_LNL)
1150 		xe2lpd_sde_hpd_irq_setup(i915);
1151 	else if (INTEL_PCH_TYPE(i915) >= PCH_MTL)
1152 		mtp_hpd_irq_setup(i915);
1153 }
1154 
1155 static u32 spt_hotplug_mask(enum hpd_pin hpd_pin)
1156 {
1157 	switch (hpd_pin) {
1158 	case HPD_PORT_A:
1159 		return PORTA_HOTPLUG_ENABLE;
1160 	case HPD_PORT_B:
1161 		return PORTB_HOTPLUG_ENABLE;
1162 	case HPD_PORT_C:
1163 		return PORTC_HOTPLUG_ENABLE;
1164 	case HPD_PORT_D:
1165 		return PORTD_HOTPLUG_ENABLE;
1166 	default:
1167 		return 0;
1168 	}
1169 }
1170 
1171 static u32 spt_hotplug_enables(struct intel_encoder *encoder)
1172 {
1173 	return spt_hotplug_mask(encoder->hpd_pin);
1174 }
1175 
1176 static u32 spt_hotplug2_mask(enum hpd_pin hpd_pin)
1177 {
1178 	switch (hpd_pin) {
1179 	case HPD_PORT_E:
1180 		return PORTE_HOTPLUG_ENABLE;
1181 	default:
1182 		return 0;
1183 	}
1184 }
1185 
1186 static u32 spt_hotplug2_enables(struct intel_encoder *encoder)
1187 {
1188 	return spt_hotplug2_mask(encoder->hpd_pin);
1189 }
1190 
1191 static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
1192 {
1193 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
1194 	if (HAS_PCH_CNP(dev_priv)) {
1195 		intel_uncore_rmw(&dev_priv->uncore, SOUTH_CHICKEN1, CHASSIS_CLK_REQ_DURATION_MASK,
1196 				 CHASSIS_CLK_REQ_DURATION(0xf));
1197 	}
1198 
1199 	/* Enable digital hotplug on the PCH */
1200 	intel_uncore_rmw(&dev_priv->uncore, PCH_PORT_HOTPLUG,
1201 			 intel_hpd_hotplug_mask(dev_priv, spt_hotplug_mask),
1202 			 intel_hpd_hotplug_enables(dev_priv, spt_hotplug_enables));
1203 
1204 	intel_uncore_rmw(&dev_priv->uncore, PCH_PORT_HOTPLUG2,
1205 			 intel_hpd_hotplug_mask(dev_priv, spt_hotplug2_mask),
1206 			 intel_hpd_hotplug_enables(dev_priv, spt_hotplug2_enables));
1207 }
1208 
1209 static void spt_hpd_enable_detection(struct intel_encoder *encoder)
1210 {
1211 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1212 
1213 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
1214 	if (HAS_PCH_CNP(i915)) {
1215 		intel_uncore_rmw(&i915->uncore, SOUTH_CHICKEN1,
1216 				 CHASSIS_CLK_REQ_DURATION_MASK,
1217 				 CHASSIS_CLK_REQ_DURATION(0xf));
1218 	}
1219 
1220 	intel_uncore_rmw(&i915->uncore, PCH_PORT_HOTPLUG,
1221 			 spt_hotplug_mask(encoder->hpd_pin),
1222 			 spt_hotplug_enables(encoder));
1223 
1224 	intel_uncore_rmw(&i915->uncore, PCH_PORT_HOTPLUG2,
1225 			 spt_hotplug2_mask(encoder->hpd_pin),
1226 			 spt_hotplug2_enables(encoder));
1227 }
1228 
1229 static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
1230 {
1231 	u32 hotplug_irqs, enabled_irqs;
1232 
1233 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
1234 		intel_uncore_write(&dev_priv->uncore, SHPD_FILTER_CNT, SHPD_FILTER_CNT_500_ADJ);
1235 
1236 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->display.hotplug.pch_hpd);
1237 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->display.hotplug.pch_hpd);
1238 
1239 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
1240 
1241 	spt_hpd_detection_setup(dev_priv);
1242 }
1243 
1244 static u32 ilk_hotplug_mask(enum hpd_pin hpd_pin)
1245 {
1246 	switch (hpd_pin) {
1247 	case HPD_PORT_A:
1248 		return DIGITAL_PORTA_HOTPLUG_ENABLE |
1249 			DIGITAL_PORTA_PULSE_DURATION_MASK;
1250 	default:
1251 		return 0;
1252 	}
1253 }
1254 
1255 static u32 ilk_hotplug_enables(struct intel_encoder *encoder)
1256 {
1257 	switch (encoder->hpd_pin) {
1258 	case HPD_PORT_A:
1259 		return DIGITAL_PORTA_HOTPLUG_ENABLE |
1260 			DIGITAL_PORTA_PULSE_DURATION_2ms;
1261 	default:
1262 		return 0;
1263 	}
1264 }
1265 
1266 static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
1267 {
1268 	/*
1269 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
1270 	 * duration to 2ms (which is the minimum in the Display Port spec)
1271 	 * The pulse duration bits are reserved on HSW+.
1272 	 */
1273 	intel_uncore_rmw(&dev_priv->uncore, DIGITAL_PORT_HOTPLUG_CNTRL,
1274 			 intel_hpd_hotplug_mask(dev_priv, ilk_hotplug_mask),
1275 			 intel_hpd_hotplug_enables(dev_priv, ilk_hotplug_enables));
1276 }
1277 
1278 static void ilk_hpd_enable_detection(struct intel_encoder *encoder)
1279 {
1280 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1281 
1282 	intel_uncore_rmw(&i915->uncore, DIGITAL_PORT_HOTPLUG_CNTRL,
1283 			 ilk_hotplug_mask(encoder->hpd_pin),
1284 			 ilk_hotplug_enables(encoder));
1285 
1286 	ibx_hpd_enable_detection(encoder);
1287 }
1288 
1289 static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
1290 {
1291 	u32 hotplug_irqs, enabled_irqs;
1292 
1293 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->display.hotplug.hpd);
1294 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->display.hotplug.hpd);
1295 
1296 	if (DISPLAY_VER(dev_priv) >= 8)
1297 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
1298 	else
1299 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
1300 
1301 	ilk_hpd_detection_setup(dev_priv);
1302 
1303 	ibx_hpd_irq_setup(dev_priv);
1304 }
1305 
1306 static u32 bxt_hotplug_mask(enum hpd_pin hpd_pin)
1307 {
1308 	switch (hpd_pin) {
1309 	case HPD_PORT_A:
1310 		return PORTA_HOTPLUG_ENABLE | BXT_DDIA_HPD_INVERT;
1311 	case HPD_PORT_B:
1312 		return PORTB_HOTPLUG_ENABLE | BXT_DDIB_HPD_INVERT;
1313 	case HPD_PORT_C:
1314 		return PORTC_HOTPLUG_ENABLE | BXT_DDIC_HPD_INVERT;
1315 	default:
1316 		return 0;
1317 	}
1318 }
1319 
1320 static u32 bxt_hotplug_enables(struct intel_encoder *encoder)
1321 {
1322 	u32 hotplug;
1323 
1324 	switch (encoder->hpd_pin) {
1325 	case HPD_PORT_A:
1326 		hotplug = PORTA_HOTPLUG_ENABLE;
1327 		if (intel_bios_encoder_hpd_invert(encoder->devdata))
1328 			hotplug |= BXT_DDIA_HPD_INVERT;
1329 		return hotplug;
1330 	case HPD_PORT_B:
1331 		hotplug = PORTB_HOTPLUG_ENABLE;
1332 		if (intel_bios_encoder_hpd_invert(encoder->devdata))
1333 			hotplug |= BXT_DDIB_HPD_INVERT;
1334 		return hotplug;
1335 	case HPD_PORT_C:
1336 		hotplug = PORTC_HOTPLUG_ENABLE;
1337 		if (intel_bios_encoder_hpd_invert(encoder->devdata))
1338 			hotplug |= BXT_DDIC_HPD_INVERT;
1339 		return hotplug;
1340 	default:
1341 		return 0;
1342 	}
1343 }
1344 
1345 static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
1346 {
1347 	intel_uncore_rmw(&dev_priv->uncore, PCH_PORT_HOTPLUG,
1348 			 intel_hpd_hotplug_mask(dev_priv, bxt_hotplug_mask),
1349 			 intel_hpd_hotplug_enables(dev_priv, bxt_hotplug_enables));
1350 }
1351 
1352 static void bxt_hpd_enable_detection(struct intel_encoder *encoder)
1353 {
1354 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1355 
1356 	intel_uncore_rmw(&i915->uncore, PCH_PORT_HOTPLUG,
1357 			 bxt_hotplug_mask(encoder->hpd_pin),
1358 			 bxt_hotplug_enables(encoder));
1359 }
1360 
1361 static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
1362 {
1363 	u32 hotplug_irqs, enabled_irqs;
1364 
1365 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->display.hotplug.hpd);
1366 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->display.hotplug.hpd);
1367 
1368 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
1369 
1370 	bxt_hpd_detection_setup(dev_priv);
1371 }
1372 
1373 static void g45_hpd_peg_band_gap_wa(struct drm_i915_private *i915)
1374 {
1375 	/*
1376 	 * For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
1377 	 * 0xd.  Failure to do so will result in spurious interrupts being
1378 	 * generated on the port when a cable is not attached.
1379 	 */
1380 	intel_de_rmw(i915, PEG_BAND_GAP_DATA, 0xf, 0xd);
1381 }
1382 
1383 static void i915_hpd_enable_detection(struct intel_encoder *encoder)
1384 {
1385 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1386 	u32 hotplug_en = hpd_mask_i915[encoder->hpd_pin];
1387 
1388 	if (IS_G45(i915))
1389 		g45_hpd_peg_band_gap_wa(i915);
1390 
1391 	/* HPD sense and interrupt enable are one and the same */
1392 	i915_hotplug_interrupt_update(i915, hotplug_en, hotplug_en);
1393 }
1394 
1395 static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
1396 {
1397 	u32 hotplug_en;
1398 
1399 	lockdep_assert_held(&dev_priv->irq_lock);
1400 
1401 	/*
1402 	 * Note HDMI and DP share hotplug bits. Enable bits are the same for all
1403 	 * generations.
1404 	 */
1405 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
1406 	/*
1407 	 * Programming the CRT detection parameters tends to generate a spurious
1408 	 * hotplug event about three seconds later. So just do it once.
1409 	 */
1410 	if (IS_G4X(dev_priv))
1411 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
1412 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
1413 
1414 	if (IS_G45(dev_priv))
1415 		g45_hpd_peg_band_gap_wa(dev_priv);
1416 
1417 	/* Ignore TV since it's buggy */
1418 	i915_hotplug_interrupt_update_locked(dev_priv,
1419 					     HOTPLUG_INT_EN_MASK |
1420 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
1421 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
1422 					     hotplug_en);
1423 }
1424 
1425 struct intel_hotplug_funcs {
1426 	/* Enable HPD sense and interrupts for all present encoders */
1427 	void (*hpd_irq_setup)(struct drm_i915_private *i915);
1428 	/* Enable HPD sense for a single encoder */
1429 	void (*hpd_enable_detection)(struct intel_encoder *encoder);
1430 };
1431 
1432 #define HPD_FUNCS(platform)					 \
1433 static const struct intel_hotplug_funcs platform##_hpd_funcs = { \
1434 	.hpd_irq_setup = platform##_hpd_irq_setup,		 \
1435 	.hpd_enable_detection = platform##_hpd_enable_detection, \
1436 }
1437 
1438 HPD_FUNCS(i915);
1439 HPD_FUNCS(xelpdp);
1440 HPD_FUNCS(dg1);
1441 HPD_FUNCS(gen11);
1442 HPD_FUNCS(bxt);
1443 HPD_FUNCS(icp);
1444 HPD_FUNCS(spt);
1445 HPD_FUNCS(ilk);
1446 #undef HPD_FUNCS
1447 
1448 void intel_hpd_enable_detection(struct intel_encoder *encoder)
1449 {
1450 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1451 
1452 	if (i915->display.funcs.hotplug)
1453 		i915->display.funcs.hotplug->hpd_enable_detection(encoder);
1454 }
1455 
1456 void intel_hpd_irq_setup(struct drm_i915_private *i915)
1457 {
1458 	if (i915->display.irq.display_irqs_enabled && i915->display.funcs.hotplug)
1459 		i915->display.funcs.hotplug->hpd_irq_setup(i915);
1460 }
1461 
1462 void intel_hotplug_irq_init(struct drm_i915_private *i915)
1463 {
1464 	intel_hpd_init_pins(i915);
1465 
1466 	intel_hpd_init_early(i915);
1467 
1468 	if (HAS_GMCH(i915)) {
1469 		if (I915_HAS_HOTPLUG(i915))
1470 			i915->display.funcs.hotplug = &i915_hpd_funcs;
1471 	} else {
1472 		if (HAS_PCH_DG2(i915))
1473 			i915->display.funcs.hotplug = &icp_hpd_funcs;
1474 		else if (HAS_PCH_DG1(i915))
1475 			i915->display.funcs.hotplug = &dg1_hpd_funcs;
1476 		else if (DISPLAY_VER(i915) >= 14)
1477 			i915->display.funcs.hotplug = &xelpdp_hpd_funcs;
1478 		else if (DISPLAY_VER(i915) >= 11)
1479 			i915->display.funcs.hotplug = &gen11_hpd_funcs;
1480 		else if (IS_GEMINILAKE(i915) || IS_BROXTON(i915))
1481 			i915->display.funcs.hotplug = &bxt_hpd_funcs;
1482 		else if (INTEL_PCH_TYPE(i915) >= PCH_ICP)
1483 			i915->display.funcs.hotplug = &icp_hpd_funcs;
1484 		else if (INTEL_PCH_TYPE(i915) >= PCH_SPT)
1485 			i915->display.funcs.hotplug = &spt_hpd_funcs;
1486 		else
1487 			i915->display.funcs.hotplug = &ilk_hpd_funcs;
1488 	}
1489 }
1490