xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c (revision b0a3a811634aa7422f8a92196db47a06a70bda16)
1 /*
2  * Copyright 2008 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  * Copyright 2009 Jerome Glisse.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Dave Airlie
25  *          Alex Deucher
26  *          Jerome Glisse
27  */
28 
29 /**
30  * DOC: Interrupt Handling
31  *
32  * Interrupts generated within GPU hardware raise interrupt requests that are
33  * passed to amdgpu IRQ handler which is responsible for detecting source and
34  * type of the interrupt and dispatching matching handlers. If handling an
35  * interrupt requires calling kernel functions that may sleep processing is
36  * dispatched to work handlers.
37  *
38  * If MSI functionality is not disabled by module parameter then MSI
39  * support will be enabled.
40  *
41  * For GPU interrupt sources that may be driven by another driver, IRQ domain
42  * support is used (with mapping between virtual and hardware IRQs).
43  */
44 
45 #include <linux/irq.h>
46 #include <linux/pci.h>
47 
48 #include <drm/drm_vblank.h>
49 #include <drm/amdgpu_drm.h>
50 #include <drm/drm_drv.h>
51 #include "amdgpu.h"
52 #include "amdgpu_ih.h"
53 #include "atom.h"
54 #include "amdgpu_connectors.h"
55 #include "amdgpu_trace.h"
56 #include "amdgpu_amdkfd.h"
57 #include "amdgpu_ras.h"
58 
59 #include <linux/pm_runtime.h>
60 
61 #ifdef CONFIG_DRM_AMD_DC
62 #include "amdgpu_dm_irq.h"
63 #endif
64 
65 #define AMDGPU_WAIT_IDLE_TIMEOUT 200
66 
67 const char *soc15_ih_clientid_name[] = {
68 	"IH",
69 	"SDMA2 or ACP",
70 	"ATHUB",
71 	"BIF",
72 	"SDMA3 or DCE",
73 	"SDMA4 or ISP",
74 	"VMC1 or PCIE0",
75 	"RLC",
76 	"SDMA0",
77 	"SDMA1",
78 	"SE0SH",
79 	"SE1SH",
80 	"SE2SH",
81 	"SE3SH",
82 	"VCN1 or UVD1",
83 	"THM",
84 	"VCN or UVD",
85 	"SDMA5 or VCE0",
86 	"VMC",
87 	"SDMA6 or XDMA",
88 	"GRBM_CP",
89 	"ATS",
90 	"ROM_SMUIO",
91 	"DF",
92 	"SDMA7 or VCE1",
93 	"PWR",
94 	"reserved",
95 	"UTCL2",
96 	"EA",
97 	"UTCL2LOG",
98 	"MP0",
99 	"MP1"
100 };
101 
102 const char *soc_v1_0_ih_clientid_name[] = {
103 	"IH",
104 	"Reserved",
105 	"ATHUB",
106 	"BIF",
107 	"Reserved",
108 	"Reserved",
109 	"Reserved",
110 	"RLC",
111 	"Reserved",
112 	"Reserved",
113 	"GFX",
114 	"IMU",
115 	"Reserved",
116 	"Reserved",
117 	"VCN1 or UVD1",
118 	"THM",
119 	"VCN or UVD",
120 	"Reserved",
121 	"VMC",
122 	"Reserved",
123 	"GRBM_CP",
124 	"GC_AID",
125 	"ROM_SMUIO",
126 	"DF",
127 	"Reserved",
128 	"PWR",
129 	"LSDMA",
130 	"GC_UTCL2",
131 	"nHT",
132 	"Reserved",
133 	"MP0",
134 	"MP1",
135 };
136 
137 const int node_id_to_phys_map[NODEID_MAX] = {
138 	[AID0_NODEID] = 0,
139 	[XCD0_NODEID] = 0,
140 	[XCD1_NODEID] = 1,
141 	[AID1_NODEID] = 1,
142 	[XCD2_NODEID] = 2,
143 	[XCD3_NODEID] = 3,
144 	[AID2_NODEID] = 2,
145 	[XCD4_NODEID] = 4,
146 	[XCD5_NODEID] = 5,
147 	[AID3_NODEID] = 3,
148 	[XCD6_NODEID] = 6,
149 	[XCD7_NODEID] = 7,
150 };
151 
152 /**
153  * amdgpu_irq_disable_all - disable *all* interrupts
154  *
155  * @adev: amdgpu device pointer
156  *
157  * Disable all types of interrupts from all sources.
158  */
159 void amdgpu_irq_disable_all(struct amdgpu_device *adev)
160 {
161 	unsigned long irqflags;
162 	unsigned int i, j, k;
163 	int r;
164 
165 	spin_lock_irqsave(&adev->irq.lock, irqflags);
166 	for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
167 		if (!adev->irq.client[i].sources)
168 			continue;
169 
170 		for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
171 			struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
172 
173 			if (!src || !src->funcs->set || !src->num_types)
174 				continue;
175 
176 			for (k = 0; k < src->num_types; ++k) {
177 				r = src->funcs->set(adev, src, k,
178 						    AMDGPU_IRQ_STATE_DISABLE);
179 				if (r)
180 					dev_err(adev->dev,
181 						"error disabling interrupt (%d)\n",
182 						r);
183 			}
184 		}
185 	}
186 	spin_unlock_irqrestore(&adev->irq.lock, irqflags);
187 }
188 
189 /**
190  * amdgpu_irq_handler - IRQ handler
191  *
192  * @irq: IRQ number (unused)
193  * @arg: pointer to DRM device
194  *
195  * IRQ handler for amdgpu driver (all ASICs).
196  *
197  * Returns:
198  * result of handling the IRQ, as defined by &irqreturn_t
199  */
200 static irqreturn_t amdgpu_irq_handler(int irq, void *arg)
201 {
202 	struct drm_device *dev = (struct drm_device *) arg;
203 	struct amdgpu_device *adev = drm_to_adev(dev);
204 	irqreturn_t ret;
205 
206 	ret = amdgpu_ih_process(adev, &adev->irq.ih);
207 	if (ret == IRQ_HANDLED)
208 		pm_runtime_mark_last_busy(dev->dev);
209 
210 	amdgpu_ras_interrupt_fatal_error_handler(adev);
211 
212 	return ret;
213 }
214 
215 /**
216  * amdgpu_irq_handle_ih1 - kick of processing for IH1
217  *
218  * @work: work structure in struct amdgpu_irq
219  *
220  * Kick of processing IH ring 1.
221  */
222 static void amdgpu_irq_handle_ih1(struct work_struct *work)
223 {
224 	struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
225 						  irq.ih1_work);
226 
227 	amdgpu_ih_process(adev, &adev->irq.ih1);
228 }
229 
230 /**
231  * amdgpu_irq_handle_ih2 - kick of processing for IH2
232  *
233  * @work: work structure in struct amdgpu_irq
234  *
235  * Kick of processing IH ring 2.
236  */
237 static void amdgpu_irq_handle_ih2(struct work_struct *work)
238 {
239 	struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
240 						  irq.ih2_work);
241 
242 	amdgpu_ih_process(adev, &adev->irq.ih2);
243 }
244 
245 /**
246  * amdgpu_irq_handle_ih_soft - kick of processing for ih_soft
247  *
248  * @work: work structure in struct amdgpu_irq
249  *
250  * Kick of processing IH soft ring.
251  */
252 static void amdgpu_irq_handle_ih_soft(struct work_struct *work)
253 {
254 	struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
255 						  irq.ih_soft_work);
256 
257 	amdgpu_ih_process(adev, &adev->irq.ih_soft);
258 }
259 
260 static void amdgpu_irq_handle_ih_psp(struct work_struct *work)
261 {
262 	struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
263 						  irq.ih_psp_work);
264 
265 	amdgpu_ih_process(adev, &adev->irq.ih_psp);
266 }
267 
268 static void amdgpu_irq_handle_ih_ualink(struct work_struct *work)
269 {
270 	struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
271 						  irq.ih_ualink_work);
272 
273 	amdgpu_ih_process(adev, &adev->irq.ih_ualink);
274 }
275 
276 /**
277  * amdgpu_msi_ok - check whether MSI functionality is enabled
278  *
279  * @adev: amdgpu device pointer (unused)
280  *
281  * Checks whether MSI functionality has been disabled via module parameter
282  * (all ASICs).
283  *
284  * Returns:
285  * *true* if MSIs are allowed to be enabled or *false* otherwise
286  */
287 static bool amdgpu_msi_ok(struct amdgpu_device *adev)
288 {
289 	if (amdgpu_msi == 1)
290 		return true;
291 	else if (amdgpu_msi == 0)
292 		return false;
293 
294 	return true;
295 }
296 
297 void amdgpu_restore_msix(struct amdgpu_device *adev)
298 {
299 	u16 ctrl;
300 
301 	pci_read_config_word(adev->pdev, adev->pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
302 	if (!(ctrl & PCI_MSIX_FLAGS_ENABLE))
303 		return;
304 
305 	/* VF FLR */
306 	ctrl &= ~PCI_MSIX_FLAGS_ENABLE;
307 	pci_write_config_word(adev->pdev, adev->pdev->msix_cap + PCI_MSIX_FLAGS, ctrl);
308 	ctrl |= PCI_MSIX_FLAGS_ENABLE;
309 	pci_write_config_word(adev->pdev, adev->pdev->msix_cap + PCI_MSIX_FLAGS, ctrl);
310 }
311 
312 /**
313  * amdgpu_irq_init - initialize interrupt handling
314  *
315  * @adev: amdgpu device pointer
316  *
317  * Sets up work functions for hotplug and reset interrupts, enables MSI
318  * functionality, initializes vblank, hotplug and reset interrupt handling.
319  *
320  * Returns:
321  * 0 on success or error code on failure
322  */
323 int amdgpu_irq_init(struct amdgpu_device *adev)
324 {
325 	unsigned int irq, flags;
326 	int r;
327 
328 	/* Enable MSI if not disabled by module parameter */
329 	adev->irq.msi_enabled = false;
330 
331 	if (!amdgpu_msi_ok(adev))
332 		flags = PCI_IRQ_INTX;
333 	else
334 		flags = PCI_IRQ_ALL_TYPES;
335 
336 	/* we only need one vector */
337 	r = pci_alloc_irq_vectors(adev->pdev, 1, 1, flags);
338 	if (r < 0) {
339 		dev_err(adev->dev, "Failed to alloc msi vectors\n");
340 		return r;
341 	}
342 
343 	if (amdgpu_msi_ok(adev)) {
344 		adev->irq.msi_enabled = true;
345 		dev_dbg(adev->dev, "using MSI/MSI-X.\n");
346 	}
347 
348 	INIT_WORK(&adev->irq.ih1_work, amdgpu_irq_handle_ih1);
349 	INIT_WORK(&adev->irq.ih2_work, amdgpu_irq_handle_ih2);
350 	INIT_WORK(&adev->irq.ih_soft_work, amdgpu_irq_handle_ih_soft);
351 	INIT_WORK(&adev->irq.ih_psp_work, amdgpu_irq_handle_ih_psp);
352 	INIT_WORK(&adev->irq.ih_ualink_work, amdgpu_irq_handle_ih_ualink);
353 
354 	/* Use vector 0 for MSI-X. */
355 	r = pci_irq_vector(adev->pdev, 0);
356 	if (r < 0)
357 		goto free_vectors;
358 	irq = r;
359 
360 	/* PCI devices require shared interrupts. */
361 	r = request_irq(irq, amdgpu_irq_handler, IRQF_SHARED, adev_to_drm(adev)->driver->name,
362 			adev_to_drm(adev));
363 	if (r)
364 		goto free_vectors;
365 
366 	adev->irq.installed = true;
367 	adev->irq.irq = irq;
368 	adev_to_drm(adev)->max_vblank_count = 0x00ffffff;
369 
370 	dev_dbg(adev->dev, "irq initialized.\n");
371 	return 0;
372 
373 free_vectors:
374 	if (adev->irq.msi_enabled)
375 		pci_free_irq_vectors(adev->pdev);
376 
377 	adev->irq.msi_enabled = false;
378 	return r;
379 }
380 
381 void amdgpu_irq_fini_hw(struct amdgpu_device *adev)
382 {
383 	if (adev->irq.installed) {
384 		free_irq(adev->irq.irq, adev_to_drm(adev));
385 		adev->irq.installed = false;
386 		if (adev->irq.msi_enabled)
387 			pci_free_irq_vectors(adev->pdev);
388 	}
389 
390 	amdgpu_ih_ring_fini(adev, &adev->irq.ih_soft);
391 	amdgpu_ih_ring_fini(adev, &adev->irq.ih_psp);
392 	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
393 	amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
394 	amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
395 }
396 
397 /**
398  * amdgpu_irq_fini_sw - shut down interrupt handling
399  *
400  * @adev: amdgpu device pointer
401  *
402  * Tears down work functions for hotplug and reset interrupts, disables MSI
403  * functionality, shuts down vblank, hotplug and reset interrupt handling,
404  * turns off interrupts from all sources (all ASICs).
405  */
406 void amdgpu_irq_fini_sw(struct amdgpu_device *adev)
407 {
408 	unsigned int i, j;
409 
410 	for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
411 		if (!adev->irq.client[i].sources)
412 			continue;
413 
414 		for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
415 			struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
416 
417 			if (!src)
418 				continue;
419 
420 			kfree(src->enabled_types);
421 			src->enabled_types = NULL;
422 		}
423 		kfree(adev->irq.client[i].sources);
424 		adev->irq.client[i].sources = NULL;
425 	}
426 }
427 
428 /**
429  * amdgpu_irq_add_id - register IRQ source
430  *
431  * @adev: amdgpu device pointer
432  * @client_id: client id
433  * @src_id: source id
434  * @source: IRQ source pointer
435  *
436  * Registers IRQ source on a client.
437  *
438  * Returns:
439  * 0 on success or error code otherwise
440  */
441 int amdgpu_irq_add_id(struct amdgpu_device *adev,
442 		      unsigned int client_id, unsigned int src_id,
443 		      struct amdgpu_irq_src *source)
444 {
445 	if (client_id >= AMDGPU_IRQ_CLIENTID_MAX)
446 		return -EINVAL;
447 
448 	if (src_id >= AMDGPU_MAX_IRQ_SRC_ID)
449 		return -EINVAL;
450 
451 	if (!source->funcs)
452 		return -EINVAL;
453 
454 	if (!adev->irq.client[client_id].sources) {
455 		adev->irq.client[client_id].sources =
456 			kzalloc_objs(struct amdgpu_irq_src *,
457 				     AMDGPU_MAX_IRQ_SRC_ID);
458 		if (!adev->irq.client[client_id].sources)
459 			return -ENOMEM;
460 	}
461 
462 	if (adev->irq.client[client_id].sources[src_id] != NULL)
463 		return -EINVAL;
464 
465 	if (source->num_types && !source->enabled_types) {
466 		atomic_t *types;
467 
468 		types = kzalloc_objs(atomic_t, source->num_types);
469 		if (!types)
470 			return -ENOMEM;
471 
472 		source->enabled_types = types;
473 	}
474 
475 	adev->irq.client[client_id].sources[src_id] = source;
476 	return 0;
477 }
478 
479 /**
480  * amdgpu_irq_dispatch - dispatch IRQ to IP blocks
481  *
482  * @adev: amdgpu device pointer
483  * @ih: interrupt ring instance
484  *
485  * Dispatches IRQ to IP blocks.
486  */
487 void amdgpu_irq_dispatch(struct amdgpu_device *adev,
488 			 struct amdgpu_ih_ring *ih)
489 {
490 	u32 ring_index = ih->rptr >> 2;
491 	struct amdgpu_iv_entry entry;
492 	unsigned int client_id, src_id;
493 	struct amdgpu_irq_src *src;
494 	bool handled = false;
495 	int r;
496 
497 	entry.ih = ih;
498 	entry.iv_entry = (const uint32_t *)&ih->ring[ring_index];
499 
500 	/*
501 	 * timestamp is not supported on some legacy SOCs (cik, cz, iceland,
502 	 * si and tonga), so initialize timestamp and timestamp_src to 0
503 	 */
504 	entry.timestamp = 0;
505 	entry.timestamp_src = 0;
506 
507 	amdgpu_ih_decode_iv(adev, &entry);
508 
509 	trace_amdgpu_iv(ih - &adev->irq.ih, &entry);
510 
511 	client_id = entry.client_id;
512 	src_id = entry.src_id;
513 
514 	if (client_id >= AMDGPU_IRQ_CLIENTID_MAX) {
515 		dev_dbg(adev->dev, "Invalid client_id in IV: %d\n", client_id);
516 
517 	} else	if (src_id >= AMDGPU_MAX_IRQ_SRC_ID) {
518 		dev_dbg(adev->dev, "Invalid src_id in IV: %d\n", src_id);
519 
520 	} else if (((client_id == AMDGPU_IRQ_CLIENTID_LEGACY) ||
521 		    (client_id == SOC15_IH_CLIENTID_ISP)) &&
522 		   adev->irq.virq[src_id]) {
523 		generic_handle_domain_irq(adev->irq.domain, src_id);
524 
525 	} else if (!adev->irq.client[client_id].sources) {
526 		dev_dbg(adev->dev,
527 			"Unregistered interrupt client_id: %d src_id: %d\n",
528 			client_id, src_id);
529 
530 	} else if ((src = adev->irq.client[client_id].sources[src_id])) {
531 		r = src->funcs->process(adev, src, &entry);
532 		if (r < 0)
533 			dev_err(adev->dev, "error processing interrupt (%d)\n",
534 				r);
535 		else if (r)
536 			handled = true;
537 
538 	} else {
539 		dev_dbg(adev->dev,
540 			"Unregistered interrupt src_id: %d of client_id:%d\n",
541 			src_id, client_id);
542 	}
543 
544 	/* Send it to amdkfd as well if it isn't already handled */
545 	if (!handled)
546 		amdgpu_amdkfd_interrupt(adev, entry.iv_entry);
547 
548 	if (amdgpu_ih_ts_after(ih->processed_timestamp, entry.timestamp))
549 		ih->processed_timestamp = entry.timestamp;
550 }
551 
552 /**
553  * amdgpu_irq_delegate - delegate IV to soft IH ring
554  *
555  * @adev: amdgpu device pointer
556  * @entry: IV entry
557  * @num_dw: size of IV
558  *
559  * Delegate the IV to the soft IH ring and schedule processing of it. Used
560  * if the hardware delegation to IH1 or IH2 doesn't work for some reason.
561  */
562 void amdgpu_irq_delegate(struct amdgpu_device *adev,
563 			 struct amdgpu_iv_entry *entry,
564 			 unsigned int num_dw)
565 {
566 	amdgpu_ih_ring_write(adev, &adev->irq.ih_soft, entry->iv_entry, num_dw);
567 	queue_work(system_dfl_wq, &adev->irq.ih_soft_work);
568 }
569 
570 void amdgpu_irq_psp_delegate(struct amdgpu_device *adev,
571 			     struct amdgpu_iv_entry *entry,
572 			     unsigned int num_dw)
573 {
574 	amdgpu_ih_ring_write(adev, &adev->irq.ih_psp, entry->iv_entry, num_dw);
575 	schedule_work(&adev->irq.ih_psp_work);
576 }
577 
578 void amdgpu_irq_ualink_delegate(struct amdgpu_device *adev,
579 				struct amdgpu_iv_entry *entry,
580 				unsigned int num_dw)
581 {
582 	amdgpu_ih_ring_write(adev, &adev->irq.ih_ualink, entry->iv_entry, num_dw);
583 	schedule_work(&adev->irq.ih_ualink_work);
584 }
585 
586 /**
587  * amdgpu_irq_update - update hardware interrupt state
588  *
589  * @adev: amdgpu device pointer
590  * @src: interrupt source pointer
591  * @type: type of interrupt
592  *
593  * Updates interrupt state for the specific source (all ASICs).
594  */
595 int amdgpu_irq_update(struct amdgpu_device *adev,
596 			     struct amdgpu_irq_src *src, unsigned int type)
597 {
598 	unsigned long irqflags;
599 	enum amdgpu_interrupt_state state;
600 	int r;
601 
602 	spin_lock_irqsave(&adev->irq.lock, irqflags);
603 
604 	/* We need to determine after taking the lock, otherwise
605 	 * we might disable just enabled interrupts again
606 	 */
607 	if (amdgpu_irq_enabled(adev, src, type))
608 		state = AMDGPU_IRQ_STATE_ENABLE;
609 	else
610 		state = AMDGPU_IRQ_STATE_DISABLE;
611 
612 	r = src->funcs->set(adev, src, type, state);
613 	spin_unlock_irqrestore(&adev->irq.lock, irqflags);
614 	return r;
615 }
616 
617 /**
618  * amdgpu_irq_gpu_reset_resume_helper - update interrupt states on all sources
619  *
620  * @adev: amdgpu device pointer
621  *
622  * Updates state of all types of interrupts on all sources on resume after
623  * reset.
624  */
625 void amdgpu_irq_gpu_reset_resume_helper(struct amdgpu_device *adev)
626 {
627 	int i, j, k;
628 
629 	if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
630 		amdgpu_restore_msix(adev);
631 
632 	for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
633 		if (!adev->irq.client[i].sources)
634 			continue;
635 
636 		for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
637 			struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
638 
639 			if (!src || !src->funcs || !src->funcs->set)
640 				continue;
641 			for (k = 0; k < src->num_types; k++)
642 				amdgpu_irq_update(adev, src, k);
643 		}
644 	}
645 }
646 
647 /**
648  * amdgpu_irq_get - enable interrupt
649  *
650  * @adev: amdgpu device pointer
651  * @src: interrupt source pointer
652  * @type: type of interrupt
653  *
654  * Enables specified type of interrupt on the specified source (all ASICs).
655  *
656  * Returns:
657  * 0 on success or error code otherwise
658  */
659 int amdgpu_irq_get(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
660 		   unsigned int type)
661 {
662 	if (!adev->irq.installed)
663 		return -ENOENT;
664 
665 	if (type >= src->num_types)
666 		return -EINVAL;
667 
668 	if (!src->enabled_types || !src->funcs->set)
669 		return -EINVAL;
670 
671 	if (atomic_inc_return(&src->enabled_types[type]) == 1)
672 		return amdgpu_irq_update(adev, src, type);
673 
674 	return 0;
675 }
676 
677 /**
678  * amdgpu_irq_put - disable interrupt
679  *
680  * @adev: amdgpu device pointer
681  * @src: interrupt source pointer
682  * @type: type of interrupt
683  *
684  * Enables specified type of interrupt on the specified source (all ASICs).
685  *
686  * Returns:
687  * 0 on success or error code otherwise
688  */
689 int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
690 		   unsigned int type)
691 {
692 	/* When the threshold is reached,the interrupt source may not be enabled.return -EINVAL */
693 	if (amdgpu_ras_is_rma(adev) && !amdgpu_irq_enabled(adev, src, type))
694 		return -EINVAL;
695 
696 	if (!adev->irq.installed)
697 		return -ENOENT;
698 
699 	if (type >= src->num_types)
700 		return -EINVAL;
701 
702 	if (!src->enabled_types || !src->funcs->set)
703 		return -EINVAL;
704 
705 	if (WARN_ON(!amdgpu_irq_enabled(adev, src, type)))
706 		return -EINVAL;
707 
708 	if (atomic_dec_and_test(&src->enabled_types[type]))
709 		return amdgpu_irq_update(adev, src, type);
710 
711 	return 0;
712 }
713 
714 /**
715  * amdgpu_irq_enabled - check whether interrupt is enabled or not
716  *
717  * @adev: amdgpu device pointer
718  * @src: interrupt source pointer
719  * @type: type of interrupt
720  *
721  * Checks whether the given type of interrupt is enabled on the given source.
722  *
723  * Returns:
724  * *true* if interrupt is enabled, *false* if interrupt is disabled or on
725  * invalid parameters
726  */
727 bool amdgpu_irq_enabled(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
728 			unsigned int type)
729 {
730 	if (!adev->irq.installed)
731 		return false;
732 
733 	if (type >= src->num_types)
734 		return false;
735 
736 	if (!src->enabled_types || !src->funcs->set)
737 		return false;
738 
739 	return !!atomic_read(&src->enabled_types[type]);
740 }
741 
742 /* XXX: Generic IRQ handling */
743 static void amdgpu_irq_mask(struct irq_data *irqd)
744 {
745 	/* XXX */
746 }
747 
748 static void amdgpu_irq_unmask(struct irq_data *irqd)
749 {
750 	/* XXX */
751 }
752 
753 /* amdgpu hardware interrupt chip descriptor */
754 static struct irq_chip amdgpu_irq_chip = {
755 	.name = "amdgpu-ih",
756 	.irq_mask = amdgpu_irq_mask,
757 	.irq_unmask = amdgpu_irq_unmask,
758 };
759 
760 /**
761  * amdgpu_irqdomain_map - create mapping between virtual and hardware IRQ numbers
762  *
763  * @d: amdgpu IRQ domain pointer (unused)
764  * @irq: virtual IRQ number
765  * @hwirq: hardware irq number
766  *
767  * Current implementation assigns simple interrupt handler to the given virtual
768  * IRQ.
769  *
770  * Returns:
771  * 0 on success or error code otherwise
772  */
773 static int amdgpu_irqdomain_map(struct irq_domain *d,
774 				unsigned int irq, irq_hw_number_t hwirq)
775 {
776 	if (hwirq >= AMDGPU_MAX_IRQ_SRC_ID)
777 		return -EPERM;
778 
779 	irq_set_chip_and_handler(irq,
780 				 &amdgpu_irq_chip, handle_simple_irq);
781 	return 0;
782 }
783 
784 /* Implementation of methods for amdgpu IRQ domain */
785 static const struct irq_domain_ops amdgpu_hw_irqdomain_ops = {
786 	.map = amdgpu_irqdomain_map,
787 };
788 
789 /**
790  * amdgpu_irq_add_domain - create a linear IRQ domain
791  *
792  * @adev: amdgpu device pointer
793  *
794  * Creates an IRQ domain for GPU interrupt sources
795  * that may be driven by another driver (e.g., ACP).
796  *
797  * Returns:
798  * 0 on success or error code otherwise
799  */
800 int amdgpu_irq_add_domain(struct amdgpu_device *adev)
801 {
802 	adev->irq.domain = irq_domain_create_linear(NULL, AMDGPU_MAX_IRQ_SRC_ID,
803 						    &amdgpu_hw_irqdomain_ops, adev);
804 	if (!adev->irq.domain) {
805 		dev_err(adev->dev, "GPU irq add domain failed\n");
806 		return -ENODEV;
807 	}
808 
809 	return 0;
810 }
811 
812 /**
813  * amdgpu_irq_remove_domain - remove the IRQ domain
814  *
815  * @adev: amdgpu device pointer
816  *
817  * Removes the IRQ domain for GPU interrupt sources
818  * that may be driven by another driver (e.g., ACP).
819  */
820 void amdgpu_irq_remove_domain(struct amdgpu_device *adev)
821 {
822 	if (adev->irq.domain) {
823 		irq_domain_remove(adev->irq.domain);
824 		adev->irq.domain = NULL;
825 	}
826 }
827 
828 /**
829  * amdgpu_irq_create_mapping - create mapping between domain Linux IRQs
830  *
831  * @adev: amdgpu device pointer
832  * @src_id: IH source id
833  *
834  * Creates mapping between a domain IRQ (GPU IH src id) and a Linux IRQ
835  * Use this for components that generate a GPU interrupt, but are driven
836  * by a different driver (e.g., ACP).
837  *
838  * Returns:
839  * Linux IRQ
840  */
841 unsigned int amdgpu_irq_create_mapping(struct amdgpu_device *adev, unsigned int src_id)
842 {
843 	adev->irq.virq[src_id] = irq_create_mapping(adev->irq.domain, src_id);
844 
845 	return adev->irq.virq[src_id];
846 }
847