xref: /linux/drivers/gpu/drm/xe/xe_irq.c (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include "xe_irq.h"
7 
8 #include <linux/sched/clock.h>
9 
10 #include <drm/drm_managed.h>
11 
12 #include "display/xe_display.h"
13 #include "regs/xe_gt_regs.h"
14 #include "regs/xe_regs.h"
15 #include "xe_device.h"
16 #include "xe_drv.h"
17 #include "xe_gsc_proxy.h"
18 #include "xe_gt.h"
19 #include "xe_guc.h"
20 #include "xe_hw_engine.h"
21 #include "xe_memirq.h"
22 #include "xe_mmio.h"
23 #include "xe_sriov.h"
24 
25 /*
26  * Interrupt registers for a unit are always consecutive and ordered
27  * ISR, IMR, IIR, IER.
28  */
29 #define IMR(offset)				XE_REG(offset + 0x4)
30 #define IIR(offset)				XE_REG(offset + 0x8)
31 #define IER(offset)				XE_REG(offset + 0xc)
32 
33 static void assert_iir_is_zero(struct xe_gt *mmio, struct xe_reg reg)
34 {
35 	u32 val = xe_mmio_read32(mmio, reg);
36 
37 	if (val == 0)
38 		return;
39 
40 	drm_WARN(&gt_to_xe(mmio)->drm, 1,
41 		 "Interrupt register 0x%x is not zero: 0x%08x\n",
42 		 reg.addr, val);
43 	xe_mmio_write32(mmio, reg, 0xffffffff);
44 	xe_mmio_read32(mmio, reg);
45 	xe_mmio_write32(mmio, reg, 0xffffffff);
46 	xe_mmio_read32(mmio, reg);
47 }
48 
49 /*
50  * Unmask and enable the specified interrupts.  Does not check current state,
51  * so any bits not specified here will become masked and disabled.
52  */
53 static void unmask_and_enable(struct xe_tile *tile, u32 irqregs, u32 bits)
54 {
55 	struct xe_gt *mmio = tile->primary_gt;
56 
57 	/*
58 	 * If we're just enabling an interrupt now, it shouldn't already
59 	 * be raised in the IIR.
60 	 */
61 	assert_iir_is_zero(mmio, IIR(irqregs));
62 
63 	xe_mmio_write32(mmio, IER(irqregs), bits);
64 	xe_mmio_write32(mmio, IMR(irqregs), ~bits);
65 
66 	/* Posting read */
67 	xe_mmio_read32(mmio, IMR(irqregs));
68 }
69 
70 /* Mask and disable all interrupts. */
71 static void mask_and_disable(struct xe_tile *tile, u32 irqregs)
72 {
73 	struct xe_gt *mmio = tile->primary_gt;
74 
75 	xe_mmio_write32(mmio, IMR(irqregs), ~0);
76 	/* Posting read */
77 	xe_mmio_read32(mmio, IMR(irqregs));
78 
79 	xe_mmio_write32(mmio, IER(irqregs), 0);
80 
81 	/* IIR can theoretically queue up two events. Be paranoid. */
82 	xe_mmio_write32(mmio, IIR(irqregs), ~0);
83 	xe_mmio_read32(mmio, IIR(irqregs));
84 	xe_mmio_write32(mmio, IIR(irqregs), ~0);
85 	xe_mmio_read32(mmio, IIR(irqregs));
86 }
87 
88 static u32 xelp_intr_disable(struct xe_device *xe)
89 {
90 	struct xe_gt *mmio = xe_root_mmio_gt(xe);
91 
92 	xe_mmio_write32(mmio, GFX_MSTR_IRQ, 0);
93 
94 	/*
95 	 * Now with master disabled, get a sample of level indications
96 	 * for this interrupt. Indications will be cleared on related acks.
97 	 * New indications can and will light up during processing,
98 	 * and will generate new interrupt after enabling master.
99 	 */
100 	return xe_mmio_read32(mmio, GFX_MSTR_IRQ);
101 }
102 
103 static u32
104 gu_misc_irq_ack(struct xe_device *xe, const u32 master_ctl)
105 {
106 	struct xe_gt *mmio = xe_root_mmio_gt(xe);
107 	u32 iir;
108 
109 	if (!(master_ctl & GU_MISC_IRQ))
110 		return 0;
111 
112 	iir = xe_mmio_read32(mmio, IIR(GU_MISC_IRQ_OFFSET));
113 	if (likely(iir))
114 		xe_mmio_write32(mmio, IIR(GU_MISC_IRQ_OFFSET), iir);
115 
116 	return iir;
117 }
118 
119 static inline void xelp_intr_enable(struct xe_device *xe, bool stall)
120 {
121 	struct xe_gt *mmio = xe_root_mmio_gt(xe);
122 
123 	xe_mmio_write32(mmio, GFX_MSTR_IRQ, MASTER_IRQ);
124 	if (stall)
125 		xe_mmio_read32(mmio, GFX_MSTR_IRQ);
126 }
127 
128 /* Enable/unmask the HWE interrupts for a specific GT's engines. */
129 void xe_irq_enable_hwe(struct xe_gt *gt)
130 {
131 	struct xe_device *xe = gt_to_xe(gt);
132 	u32 ccs_mask, bcs_mask;
133 	u32 irqs, dmask, smask;
134 	u32 gsc_mask = 0;
135 	u32 heci_mask = 0;
136 
137 	if (xe_device_uc_enabled(xe)) {
138 		irqs = GT_RENDER_USER_INTERRUPT |
139 			GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
140 	} else {
141 		irqs = GT_RENDER_USER_INTERRUPT |
142 		       GT_CS_MASTER_ERROR_INTERRUPT |
143 		       GT_CONTEXT_SWITCH_INTERRUPT |
144 		       GT_WAIT_SEMAPHORE_INTERRUPT;
145 	}
146 
147 	ccs_mask = xe_hw_engine_mask_per_class(gt, XE_ENGINE_CLASS_COMPUTE);
148 	bcs_mask = xe_hw_engine_mask_per_class(gt, XE_ENGINE_CLASS_COPY);
149 
150 	dmask = irqs << 16 | irqs;
151 	smask = irqs << 16;
152 
153 	if (!xe_gt_is_media_type(gt)) {
154 		/* Enable interrupts for each engine class */
155 		xe_mmio_write32(gt, RENDER_COPY_INTR_ENABLE, dmask);
156 		if (ccs_mask)
157 			xe_mmio_write32(gt, CCS_RSVD_INTR_ENABLE, smask);
158 
159 		/* Unmask interrupts for each engine instance */
160 		xe_mmio_write32(gt, RCS0_RSVD_INTR_MASK, ~smask);
161 		xe_mmio_write32(gt, BCS_RSVD_INTR_MASK, ~smask);
162 		if (bcs_mask & (BIT(1)|BIT(2)))
163 			xe_mmio_write32(gt, XEHPC_BCS1_BCS2_INTR_MASK, ~dmask);
164 		if (bcs_mask & (BIT(3)|BIT(4)))
165 			xe_mmio_write32(gt, XEHPC_BCS3_BCS4_INTR_MASK, ~dmask);
166 		if (bcs_mask & (BIT(5)|BIT(6)))
167 			xe_mmio_write32(gt, XEHPC_BCS5_BCS6_INTR_MASK, ~dmask);
168 		if (bcs_mask & (BIT(7)|BIT(8)))
169 			xe_mmio_write32(gt, XEHPC_BCS7_BCS8_INTR_MASK, ~dmask);
170 		if (ccs_mask & (BIT(0)|BIT(1)))
171 			xe_mmio_write32(gt, CCS0_CCS1_INTR_MASK, ~dmask);
172 		if (ccs_mask & (BIT(2)|BIT(3)))
173 			xe_mmio_write32(gt,  CCS2_CCS3_INTR_MASK, ~dmask);
174 	}
175 
176 	if (xe_gt_is_media_type(gt) || MEDIA_VER(xe) < 13) {
177 		/* Enable interrupts for each engine class */
178 		xe_mmio_write32(gt, VCS_VECS_INTR_ENABLE, dmask);
179 
180 		/* Unmask interrupts for each engine instance */
181 		xe_mmio_write32(gt, VCS0_VCS1_INTR_MASK, ~dmask);
182 		xe_mmio_write32(gt, VCS2_VCS3_INTR_MASK, ~dmask);
183 		xe_mmio_write32(gt, VECS0_VECS1_INTR_MASK, ~dmask);
184 
185 		/*
186 		 * the heci2 interrupt is enabled via the same register as the
187 		 * GSCCS interrupts, but it has its own mask register.
188 		 */
189 		if (xe_hw_engine_mask_per_class(gt, XE_ENGINE_CLASS_OTHER)) {
190 			gsc_mask = irqs | GSC_ER_COMPLETE;
191 			heci_mask = GSC_IRQ_INTF(1);
192 		} else if (HAS_HECI_GSCFI(xe)) {
193 			gsc_mask = GSC_IRQ_INTF(1);
194 		}
195 
196 		if (gsc_mask) {
197 			xe_mmio_write32(gt, GUNIT_GSC_INTR_ENABLE, gsc_mask | heci_mask);
198 			xe_mmio_write32(gt, GUNIT_GSC_INTR_MASK, ~gsc_mask);
199 		}
200 		if (heci_mask)
201 			xe_mmio_write32(gt, HECI2_RSVD_INTR_MASK, ~(heci_mask << 16));
202 	}
203 }
204 
205 static u32
206 gt_engine_identity(struct xe_device *xe,
207 		   struct xe_gt *mmio,
208 		   const unsigned int bank,
209 		   const unsigned int bit)
210 {
211 	u32 timeout_ts;
212 	u32 ident;
213 
214 	lockdep_assert_held(&xe->irq.lock);
215 
216 	xe_mmio_write32(mmio, IIR_REG_SELECTOR(bank), BIT(bit));
217 
218 	/*
219 	 * NB: Specs do not specify how long to spin wait,
220 	 * so we do ~100us as an educated guess.
221 	 */
222 	timeout_ts = (local_clock() >> 10) + 100;
223 	do {
224 		ident = xe_mmio_read32(mmio, INTR_IDENTITY_REG(bank));
225 	} while (!(ident & INTR_DATA_VALID) &&
226 		 !time_after32(local_clock() >> 10, timeout_ts));
227 
228 	if (unlikely(!(ident & INTR_DATA_VALID))) {
229 		drm_err(&xe->drm, "INTR_IDENTITY_REG%u:%u 0x%08x not valid!\n",
230 			bank, bit, ident);
231 		return 0;
232 	}
233 
234 	xe_mmio_write32(mmio, INTR_IDENTITY_REG(bank), ident);
235 
236 	return ident;
237 }
238 
239 #define   OTHER_MEDIA_GUC_INSTANCE           16
240 
241 static void
242 gt_other_irq_handler(struct xe_gt *gt, const u8 instance, const u16 iir)
243 {
244 	if (instance == OTHER_GUC_INSTANCE && !xe_gt_is_media_type(gt))
245 		return xe_guc_irq_handler(&gt->uc.guc, iir);
246 	if (instance == OTHER_MEDIA_GUC_INSTANCE && xe_gt_is_media_type(gt))
247 		return xe_guc_irq_handler(&gt->uc.guc, iir);
248 	if (instance == OTHER_GSC_HECI2_INSTANCE && xe_gt_is_media_type(gt))
249 		return xe_gsc_proxy_irq_handler(&gt->uc.gsc, iir);
250 
251 	if (instance != OTHER_GUC_INSTANCE &&
252 	    instance != OTHER_MEDIA_GUC_INSTANCE) {
253 		WARN_ONCE(1, "unhandled other interrupt instance=0x%x, iir=0x%x\n",
254 			  instance, iir);
255 	}
256 }
257 
258 static struct xe_gt *pick_engine_gt(struct xe_tile *tile,
259 				    enum xe_engine_class class,
260 				    unsigned int instance)
261 {
262 	struct xe_device *xe = tile_to_xe(tile);
263 
264 	if (MEDIA_VER(xe) < 13)
265 		return tile->primary_gt;
266 
267 	switch (class) {
268 	case XE_ENGINE_CLASS_VIDEO_DECODE:
269 	case XE_ENGINE_CLASS_VIDEO_ENHANCE:
270 		return tile->media_gt;
271 	case XE_ENGINE_CLASS_OTHER:
272 		switch (instance) {
273 		case OTHER_MEDIA_GUC_INSTANCE:
274 		case OTHER_GSC_INSTANCE:
275 		case OTHER_GSC_HECI2_INSTANCE:
276 			return tile->media_gt;
277 		default:
278 			break;
279 		};
280 		fallthrough;
281 	default:
282 		return tile->primary_gt;
283 	}
284 }
285 
286 static void gt_irq_handler(struct xe_tile *tile,
287 			   u32 master_ctl, unsigned long *intr_dw,
288 			   u32 *identity)
289 {
290 	struct xe_device *xe = tile_to_xe(tile);
291 	struct xe_gt *mmio = tile->primary_gt;
292 	unsigned int bank, bit;
293 	u16 instance, intr_vec;
294 	enum xe_engine_class class;
295 	struct xe_hw_engine *hwe;
296 
297 	spin_lock(&xe->irq.lock);
298 
299 	for (bank = 0; bank < 2; bank++) {
300 		if (!(master_ctl & GT_DW_IRQ(bank)))
301 			continue;
302 
303 		intr_dw[bank] = xe_mmio_read32(mmio, GT_INTR_DW(bank));
304 		for_each_set_bit(bit, intr_dw + bank, 32)
305 			identity[bit] = gt_engine_identity(xe, mmio, bank, bit);
306 		xe_mmio_write32(mmio, GT_INTR_DW(bank), intr_dw[bank]);
307 
308 		for_each_set_bit(bit, intr_dw + bank, 32) {
309 			struct xe_gt *engine_gt;
310 
311 			class = INTR_ENGINE_CLASS(identity[bit]);
312 			instance = INTR_ENGINE_INSTANCE(identity[bit]);
313 			intr_vec = INTR_ENGINE_INTR(identity[bit]);
314 
315 			engine_gt = pick_engine_gt(tile, class, instance);
316 
317 			hwe = xe_gt_hw_engine(engine_gt, class, instance, false);
318 			if (hwe) {
319 				xe_hw_engine_handle_irq(hwe, intr_vec);
320 				continue;
321 			}
322 
323 			if (class == XE_ENGINE_CLASS_OTHER) {
324 				/* HECI GSCFI interrupts come from outside of GT */
325 				if (HAS_HECI_GSCFI(xe) && instance == OTHER_GSC_INSTANCE)
326 					xe_heci_gsc_irq_handler(xe, intr_vec);
327 				else
328 					gt_other_irq_handler(engine_gt, instance, intr_vec);
329 			}
330 		}
331 	}
332 
333 	spin_unlock(&xe->irq.lock);
334 }
335 
336 /*
337  * Top-level interrupt handler for Xe_LP platforms (which did not have
338  * a "master tile" interrupt register.
339  */
340 static irqreturn_t xelp_irq_handler(int irq, void *arg)
341 {
342 	struct xe_device *xe = arg;
343 	struct xe_tile *tile = xe_device_get_root_tile(xe);
344 	u32 master_ctl, gu_misc_iir;
345 	unsigned long intr_dw[2];
346 	u32 identity[32];
347 
348 	spin_lock(&xe->irq.lock);
349 	if (!xe->irq.enabled) {
350 		spin_unlock(&xe->irq.lock);
351 		return IRQ_NONE;
352 	}
353 	spin_unlock(&xe->irq.lock);
354 
355 	master_ctl = xelp_intr_disable(xe);
356 	if (!master_ctl) {
357 		xelp_intr_enable(xe, false);
358 		return IRQ_NONE;
359 	}
360 
361 	gt_irq_handler(tile, master_ctl, intr_dw, identity);
362 
363 	xe_display_irq_handler(xe, master_ctl);
364 
365 	gu_misc_iir = gu_misc_irq_ack(xe, master_ctl);
366 
367 	xelp_intr_enable(xe, false);
368 
369 	xe_display_irq_enable(xe, gu_misc_iir);
370 
371 	return IRQ_HANDLED;
372 }
373 
374 static u32 dg1_intr_disable(struct xe_device *xe)
375 {
376 	struct xe_gt *mmio = xe_root_mmio_gt(xe);
377 	u32 val;
378 
379 	/* First disable interrupts */
380 	xe_mmio_write32(mmio, DG1_MSTR_TILE_INTR, 0);
381 
382 	/* Get the indication levels and ack the master unit */
383 	val = xe_mmio_read32(mmio, DG1_MSTR_TILE_INTR);
384 	if (unlikely(!val))
385 		return 0;
386 
387 	xe_mmio_write32(mmio, DG1_MSTR_TILE_INTR, val);
388 
389 	return val;
390 }
391 
392 static void dg1_intr_enable(struct xe_device *xe, bool stall)
393 {
394 	struct xe_gt *mmio = xe_root_mmio_gt(xe);
395 
396 	xe_mmio_write32(mmio, DG1_MSTR_TILE_INTR, DG1_MSTR_IRQ);
397 	if (stall)
398 		xe_mmio_read32(mmio, DG1_MSTR_TILE_INTR);
399 }
400 
401 /*
402  * Top-level interrupt handler for Xe_LP+ and beyond.  These platforms have
403  * a "master tile" interrupt register which must be consulted before the
404  * "graphics master" interrupt register.
405  */
406 static irqreturn_t dg1_irq_handler(int irq, void *arg)
407 {
408 	struct xe_device *xe = arg;
409 	struct xe_tile *tile;
410 	u32 master_tile_ctl, master_ctl = 0, gu_misc_iir = 0;
411 	unsigned long intr_dw[2];
412 	u32 identity[32];
413 	u8 id;
414 
415 	/* TODO: This really shouldn't be copied+pasted */
416 
417 	spin_lock(&xe->irq.lock);
418 	if (!xe->irq.enabled) {
419 		spin_unlock(&xe->irq.lock);
420 		return IRQ_NONE;
421 	}
422 	spin_unlock(&xe->irq.lock);
423 
424 	master_tile_ctl = dg1_intr_disable(xe);
425 	if (!master_tile_ctl) {
426 		dg1_intr_enable(xe, false);
427 		return IRQ_NONE;
428 	}
429 
430 	for_each_tile(tile, xe, id) {
431 		struct xe_gt *mmio = tile->primary_gt;
432 
433 		if ((master_tile_ctl & DG1_MSTR_TILE(tile->id)) == 0)
434 			continue;
435 
436 		master_ctl = xe_mmio_read32(mmio, GFX_MSTR_IRQ);
437 
438 		/*
439 		 * We might be in irq handler just when PCIe DPC is initiated
440 		 * and all MMIO reads will be returned with all 1's. Ignore this
441 		 * irq as device is inaccessible.
442 		 */
443 		if (master_ctl == REG_GENMASK(31, 0)) {
444 			drm_dbg(&tile_to_xe(tile)->drm,
445 				"Ignore this IRQ as device might be in DPC containment.\n");
446 			return IRQ_HANDLED;
447 		}
448 
449 		xe_mmio_write32(mmio, GFX_MSTR_IRQ, master_ctl);
450 
451 		gt_irq_handler(tile, master_ctl, intr_dw, identity);
452 
453 		/*
454 		 * Display interrupts (including display backlight operations
455 		 * that get reported as Gunit GSE) would only be hooked up to
456 		 * the primary tile.
457 		 */
458 		if (id == 0) {
459 			xe_display_irq_handler(xe, master_ctl);
460 			gu_misc_iir = gu_misc_irq_ack(xe, master_ctl);
461 		}
462 	}
463 
464 	dg1_intr_enable(xe, false);
465 	xe_display_irq_enable(xe, gu_misc_iir);
466 
467 	return IRQ_HANDLED;
468 }
469 
470 static void gt_irq_reset(struct xe_tile *tile)
471 {
472 	struct xe_gt *mmio = tile->primary_gt;
473 
474 	u32 ccs_mask = xe_hw_engine_mask_per_class(tile->primary_gt,
475 						   XE_ENGINE_CLASS_COMPUTE);
476 	u32 bcs_mask = xe_hw_engine_mask_per_class(tile->primary_gt,
477 						   XE_ENGINE_CLASS_COPY);
478 
479 	/* Disable RCS, BCS, VCS and VECS class engines. */
480 	xe_mmio_write32(mmio, RENDER_COPY_INTR_ENABLE, 0);
481 	xe_mmio_write32(mmio, VCS_VECS_INTR_ENABLE, 0);
482 	if (ccs_mask)
483 		xe_mmio_write32(mmio, CCS_RSVD_INTR_ENABLE, 0);
484 
485 	/* Restore masks irqs on RCS, BCS, VCS and VECS engines. */
486 	xe_mmio_write32(mmio, RCS0_RSVD_INTR_MASK,	~0);
487 	xe_mmio_write32(mmio, BCS_RSVD_INTR_MASK,	~0);
488 	if (bcs_mask & (BIT(1)|BIT(2)))
489 		xe_mmio_write32(mmio, XEHPC_BCS1_BCS2_INTR_MASK, ~0);
490 	if (bcs_mask & (BIT(3)|BIT(4)))
491 		xe_mmio_write32(mmio, XEHPC_BCS3_BCS4_INTR_MASK, ~0);
492 	if (bcs_mask & (BIT(5)|BIT(6)))
493 		xe_mmio_write32(mmio, XEHPC_BCS5_BCS6_INTR_MASK, ~0);
494 	if (bcs_mask & (BIT(7)|BIT(8)))
495 		xe_mmio_write32(mmio, XEHPC_BCS7_BCS8_INTR_MASK, ~0);
496 	xe_mmio_write32(mmio, VCS0_VCS1_INTR_MASK,	~0);
497 	xe_mmio_write32(mmio, VCS2_VCS3_INTR_MASK,	~0);
498 	xe_mmio_write32(mmio, VECS0_VECS1_INTR_MASK,	~0);
499 	if (ccs_mask & (BIT(0)|BIT(1)))
500 		xe_mmio_write32(mmio, CCS0_CCS1_INTR_MASK, ~0);
501 	if (ccs_mask & (BIT(2)|BIT(3)))
502 		xe_mmio_write32(mmio,  CCS2_CCS3_INTR_MASK, ~0);
503 
504 	if ((tile->media_gt &&
505 	     xe_hw_engine_mask_per_class(tile->media_gt, XE_ENGINE_CLASS_OTHER)) ||
506 	    HAS_HECI_GSCFI(tile_to_xe(tile))) {
507 		xe_mmio_write32(mmio, GUNIT_GSC_INTR_ENABLE, 0);
508 		xe_mmio_write32(mmio, GUNIT_GSC_INTR_MASK, ~0);
509 		xe_mmio_write32(mmio, HECI2_RSVD_INTR_MASK, ~0);
510 	}
511 
512 	xe_mmio_write32(mmio, GPM_WGBOXPERF_INTR_ENABLE, 0);
513 	xe_mmio_write32(mmio, GPM_WGBOXPERF_INTR_MASK,  ~0);
514 	xe_mmio_write32(mmio, GUC_SG_INTR_ENABLE,	 0);
515 	xe_mmio_write32(mmio, GUC_SG_INTR_MASK,		~0);
516 }
517 
518 static void xelp_irq_reset(struct xe_tile *tile)
519 {
520 	xelp_intr_disable(tile_to_xe(tile));
521 
522 	gt_irq_reset(tile);
523 
524 	if (IS_SRIOV_VF(tile_to_xe(tile)))
525 		return;
526 
527 	mask_and_disable(tile, PCU_IRQ_OFFSET);
528 }
529 
530 static void dg1_irq_reset(struct xe_tile *tile)
531 {
532 	if (tile->id == 0)
533 		dg1_intr_disable(tile_to_xe(tile));
534 
535 	gt_irq_reset(tile);
536 
537 	if (IS_SRIOV_VF(tile_to_xe(tile)))
538 		return;
539 
540 	mask_and_disable(tile, PCU_IRQ_OFFSET);
541 }
542 
543 static void dg1_irq_reset_mstr(struct xe_tile *tile)
544 {
545 	struct xe_gt *mmio = tile->primary_gt;
546 
547 	xe_mmio_write32(mmio, GFX_MSTR_IRQ, ~0);
548 }
549 
550 static void vf_irq_reset(struct xe_device *xe)
551 {
552 	struct xe_tile *tile;
553 	unsigned int id;
554 
555 	xe_assert(xe, IS_SRIOV_VF(xe));
556 
557 	if (GRAPHICS_VERx100(xe) < 1210)
558 		xelp_intr_disable(xe);
559 	else
560 		xe_assert(xe, xe_device_has_memirq(xe));
561 
562 	for_each_tile(tile, xe, id) {
563 		if (xe_device_has_memirq(xe))
564 			xe_memirq_reset(&tile->sriov.vf.memirq);
565 		else
566 			gt_irq_reset(tile);
567 	}
568 }
569 
570 static void xe_irq_reset(struct xe_device *xe)
571 {
572 	struct xe_tile *tile;
573 	u8 id;
574 
575 	if (IS_SRIOV_VF(xe))
576 		return vf_irq_reset(xe);
577 
578 	for_each_tile(tile, xe, id) {
579 		if (GRAPHICS_VERx100(xe) >= 1210)
580 			dg1_irq_reset(tile);
581 		else
582 			xelp_irq_reset(tile);
583 	}
584 
585 	tile = xe_device_get_root_tile(xe);
586 	mask_and_disable(tile, GU_MISC_IRQ_OFFSET);
587 	xe_display_irq_reset(xe);
588 
589 	/*
590 	 * The tile's top-level status register should be the last one
591 	 * to be reset to avoid possible bit re-latching from lower
592 	 * level interrupts.
593 	 */
594 	if (GRAPHICS_VERx100(xe) >= 1210) {
595 		for_each_tile(tile, xe, id)
596 			dg1_irq_reset_mstr(tile);
597 	}
598 }
599 
600 static void vf_irq_postinstall(struct xe_device *xe)
601 {
602 	struct xe_tile *tile;
603 	unsigned int id;
604 
605 	for_each_tile(tile, xe, id)
606 		if (xe_device_has_memirq(xe))
607 			xe_memirq_postinstall(&tile->sriov.vf.memirq);
608 
609 	if (GRAPHICS_VERx100(xe) < 1210)
610 		xelp_intr_enable(xe, true);
611 	else
612 		xe_assert(xe, xe_device_has_memirq(xe));
613 }
614 
615 static void xe_irq_postinstall(struct xe_device *xe)
616 {
617 	if (IS_SRIOV_VF(xe))
618 		return vf_irq_postinstall(xe);
619 
620 	xe_display_irq_postinstall(xe, xe_root_mmio_gt(xe));
621 
622 	/*
623 	 * ASLE backlight operations are reported via GUnit GSE interrupts
624 	 * on the root tile.
625 	 */
626 	unmask_and_enable(xe_device_get_root_tile(xe),
627 			  GU_MISC_IRQ_OFFSET, GU_MISC_GSE);
628 
629 	/* Enable top-level interrupts */
630 	if (GRAPHICS_VERx100(xe) >= 1210)
631 		dg1_intr_enable(xe, true);
632 	else
633 		xelp_intr_enable(xe, true);
634 }
635 
636 static irqreturn_t vf_mem_irq_handler(int irq, void *arg)
637 {
638 	struct xe_device *xe = arg;
639 	struct xe_tile *tile;
640 	unsigned int id;
641 
642 	spin_lock(&xe->irq.lock);
643 	if (!xe->irq.enabled) {
644 		spin_unlock(&xe->irq.lock);
645 		return IRQ_NONE;
646 	}
647 	spin_unlock(&xe->irq.lock);
648 
649 	for_each_tile(tile, xe, id)
650 		xe_memirq_handler(&tile->sriov.vf.memirq);
651 
652 	return IRQ_HANDLED;
653 }
654 
655 static irq_handler_t xe_irq_handler(struct xe_device *xe)
656 {
657 	if (IS_SRIOV_VF(xe) && xe_device_has_memirq(xe))
658 		return vf_mem_irq_handler;
659 
660 	if (GRAPHICS_VERx100(xe) >= 1210)
661 		return dg1_irq_handler;
662 	else
663 		return xelp_irq_handler;
664 }
665 
666 static void irq_uninstall(struct drm_device *drm, void *arg)
667 {
668 	struct xe_device *xe = arg;
669 	struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
670 	int irq;
671 
672 	if (!xe->irq.enabled)
673 		return;
674 
675 	xe->irq.enabled = false;
676 	xe_irq_reset(xe);
677 
678 	irq = pci_irq_vector(pdev, 0);
679 	free_irq(irq, xe);
680 }
681 
682 int xe_irq_install(struct xe_device *xe)
683 {
684 	struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
685 	unsigned int irq_flags = PCI_IRQ_MSIX;
686 	irq_handler_t irq_handler;
687 	int err, irq, nvec;
688 
689 	irq_handler = xe_irq_handler(xe);
690 	if (!irq_handler) {
691 		drm_err(&xe->drm, "No supported interrupt handler");
692 		return -EINVAL;
693 	}
694 
695 	xe_irq_reset(xe);
696 
697 	nvec = pci_msix_vec_count(pdev);
698 	if (nvec <= 0) {
699 		if (nvec == -EINVAL) {
700 			/* MSIX capability is not supported in the device, using MSI */
701 			irq_flags = PCI_IRQ_MSI;
702 			nvec = 1;
703 		} else {
704 			drm_err(&xe->drm, "MSIX: Failed getting count\n");
705 			return nvec;
706 		}
707 	}
708 
709 	err = pci_alloc_irq_vectors(pdev, nvec, nvec, irq_flags);
710 	if (err < 0) {
711 		drm_err(&xe->drm, "MSI/MSIX: Failed to enable support %d\n", err);
712 		return err;
713 	}
714 
715 	irq = pci_irq_vector(pdev, 0);
716 	err = request_irq(irq, irq_handler, IRQF_SHARED, DRIVER_NAME, xe);
717 	if (err < 0) {
718 		drm_err(&xe->drm, "Failed to request MSI/MSIX IRQ %d\n", err);
719 		return err;
720 	}
721 
722 	xe->irq.enabled = true;
723 
724 	xe_irq_postinstall(xe);
725 
726 	err = drmm_add_action_or_reset(&xe->drm, irq_uninstall, xe);
727 	if (err)
728 		goto free_irq_handler;
729 
730 	return 0;
731 
732 free_irq_handler:
733 	free_irq(irq, xe);
734 
735 	return err;
736 }
737 
738 void xe_irq_shutdown(struct xe_device *xe)
739 {
740 	irq_uninstall(&xe->drm, xe);
741 }
742 
743 void xe_irq_suspend(struct xe_device *xe)
744 {
745 	int irq = to_pci_dev(xe->drm.dev)->irq;
746 
747 	spin_lock_irq(&xe->irq.lock);
748 	xe->irq.enabled = false; /* no new irqs */
749 	spin_unlock_irq(&xe->irq.lock);
750 
751 	synchronize_irq(irq); /* flush irqs */
752 	xe_irq_reset(xe); /* turn irqs off */
753 }
754 
755 void xe_irq_resume(struct xe_device *xe)
756 {
757 	struct xe_gt *gt;
758 	int id;
759 
760 	/*
761 	 * lock not needed:
762 	 * 1. no irq will arrive before the postinstall
763 	 * 2. display is not yet resumed
764 	 */
765 	xe->irq.enabled = true;
766 	xe_irq_reset(xe);
767 	xe_irq_postinstall(xe); /* turn irqs on */
768 
769 	for_each_gt(gt, xe, id)
770 		xe_irq_enable_hwe(gt);
771 }
772