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_crtc_helper.h>
49 #include <drm/drm_vblank.h>
50 #include <drm/amdgpu_drm.h>
51 #include <drm/drm_drv.h>
52 #include "amdgpu.h"
53 #include "amdgpu_ih.h"
54 #include "atom.h"
55 #include "amdgpu_connectors.h"
56 #include "amdgpu_trace.h"
57 #include "amdgpu_amdkfd.h"
58 #include "amdgpu_ras.h"
59
60 #include <linux/pm_runtime.h>
61
62 #ifdef CONFIG_DRM_AMD_DC
63 #include "amdgpu_dm_irq.h"
64 #endif
65
66 #define AMDGPU_WAIT_IDLE_TIMEOUT 200
67
68 const char *soc15_ih_clientid_name[] = {
69 "IH",
70 "SDMA2 or ACP",
71 "ATHUB",
72 "BIF",
73 "SDMA3 or DCE",
74 "SDMA4 or ISP",
75 "VMC1 or PCIE0",
76 "RLC",
77 "SDMA0",
78 "SDMA1",
79 "SE0SH",
80 "SE1SH",
81 "SE2SH",
82 "SE3SH",
83 "VCN1 or UVD1",
84 "THM",
85 "VCN or UVD",
86 "SDMA5 or VCE0",
87 "VMC",
88 "SDMA6 or XDMA",
89 "GRBM_CP",
90 "ATS",
91 "ROM_SMUIO",
92 "DF",
93 "SDMA7 or VCE1",
94 "PWR",
95 "reserved",
96 "UTCL2",
97 "EA",
98 "UTCL2LOG",
99 "MP0",
100 "MP1"
101 };
102
103 /**
104 * amdgpu_hotplug_work_func - work handler for display hotplug event
105 *
106 * @work: work struct pointer
107 *
108 * This is the hotplug event work handler (all ASICs).
109 * The work gets scheduled from the IRQ handler if there
110 * was a hotplug interrupt. It walks through the connector table
111 * and calls hotplug handler for each connector. After this, it sends
112 * a DRM hotplug event to alert userspace.
113 *
114 * This design approach is required in order to defer hotplug event handling
115 * from the IRQ handler to a work handler because hotplug handler has to use
116 * mutexes which cannot be locked in an IRQ handler (since &mutex_lock may
117 * sleep).
118 */
amdgpu_hotplug_work_func(struct work_struct * work)119 static void amdgpu_hotplug_work_func(struct work_struct *work)
120 {
121 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
122 hotplug_work);
123 struct drm_device *dev = adev_to_drm(adev);
124 struct drm_mode_config *mode_config = &dev->mode_config;
125 struct drm_connector *connector;
126 struct drm_connector_list_iter iter;
127
128 mutex_lock(&mode_config->mutex);
129 drm_connector_list_iter_begin(dev, &iter);
130 drm_for_each_connector_iter(connector, &iter)
131 amdgpu_connector_hotplug(connector);
132 drm_connector_list_iter_end(&iter);
133 mutex_unlock(&mode_config->mutex);
134 /* Just fire off a uevent and let userspace tell us what to do */
135 drm_helper_hpd_irq_event(dev);
136 }
137
138 /**
139 * amdgpu_irq_disable_all - disable *all* interrupts
140 *
141 * @adev: amdgpu device pointer
142 *
143 * Disable all types of interrupts from all sources.
144 */
amdgpu_irq_disable_all(struct amdgpu_device * adev)145 void amdgpu_irq_disable_all(struct amdgpu_device *adev)
146 {
147 unsigned long irqflags;
148 unsigned i, j, k;
149 int r;
150
151 spin_lock_irqsave(&adev->irq.lock, irqflags);
152 for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
153 if (!adev->irq.client[i].sources)
154 continue;
155
156 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
157 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
158
159 if (!src || !src->funcs->set || !src->num_types)
160 continue;
161
162 for (k = 0; k < src->num_types; ++k) {
163 r = src->funcs->set(adev, src, k,
164 AMDGPU_IRQ_STATE_DISABLE);
165 if (r)
166 DRM_ERROR("error disabling interrupt (%d)\n",
167 r);
168 }
169 }
170 }
171 spin_unlock_irqrestore(&adev->irq.lock, irqflags);
172 }
173
174 /**
175 * amdgpu_irq_handler - IRQ handler
176 *
177 * @irq: IRQ number (unused)
178 * @arg: pointer to DRM device
179 *
180 * IRQ handler for amdgpu driver (all ASICs).
181 *
182 * Returns:
183 * result of handling the IRQ, as defined by &irqreturn_t
184 */
amdgpu_irq_handler(int irq,void * arg)185 static irqreturn_t amdgpu_irq_handler(int irq, void *arg)
186 {
187 struct drm_device *dev = (struct drm_device *) arg;
188 struct amdgpu_device *adev = drm_to_adev(dev);
189 irqreturn_t ret;
190
191 ret = amdgpu_ih_process(adev, &adev->irq.ih);
192 if (ret == IRQ_HANDLED)
193 pm_runtime_mark_last_busy(dev->dev);
194
195 amdgpu_ras_interrupt_fatal_error_handler(adev);
196
197 return ret;
198 }
199
200 /**
201 * amdgpu_irq_handle_ih1 - kick of processing for IH1
202 *
203 * @work: work structure in struct amdgpu_irq
204 *
205 * Kick of processing IH ring 1.
206 */
amdgpu_irq_handle_ih1(struct work_struct * work)207 static void amdgpu_irq_handle_ih1(struct work_struct *work)
208 {
209 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
210 irq.ih1_work);
211
212 amdgpu_ih_process(adev, &adev->irq.ih1);
213 }
214
215 /**
216 * amdgpu_irq_handle_ih2 - kick of processing for IH2
217 *
218 * @work: work structure in struct amdgpu_irq
219 *
220 * Kick of processing IH ring 2.
221 */
amdgpu_irq_handle_ih2(struct work_struct * work)222 static void amdgpu_irq_handle_ih2(struct work_struct *work)
223 {
224 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
225 irq.ih2_work);
226
227 amdgpu_ih_process(adev, &adev->irq.ih2);
228 }
229
230 /**
231 * amdgpu_irq_handle_ih_soft - kick of processing for ih_soft
232 *
233 * @work: work structure in struct amdgpu_irq
234 *
235 * Kick of processing IH soft ring.
236 */
amdgpu_irq_handle_ih_soft(struct work_struct * work)237 static void amdgpu_irq_handle_ih_soft(struct work_struct *work)
238 {
239 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
240 irq.ih_soft_work);
241
242 amdgpu_ih_process(adev, &adev->irq.ih_soft);
243 }
244
245 /**
246 * amdgpu_msi_ok - check whether MSI functionality is enabled
247 *
248 * @adev: amdgpu device pointer (unused)
249 *
250 * Checks whether MSI functionality has been disabled via module parameter
251 * (all ASICs).
252 *
253 * Returns:
254 * *true* if MSIs are allowed to be enabled or *false* otherwise
255 */
amdgpu_msi_ok(struct amdgpu_device * adev)256 static bool amdgpu_msi_ok(struct amdgpu_device *adev)
257 {
258 if (amdgpu_msi == 1)
259 return true;
260 else if (amdgpu_msi == 0)
261 return false;
262
263 return true;
264 }
265
amdgpu_restore_msix(struct amdgpu_device * adev)266 static void amdgpu_restore_msix(struct amdgpu_device *adev)
267 {
268 u16 ctrl;
269
270 pci_read_config_word(adev->pdev, adev->pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
271 if (!(ctrl & PCI_MSIX_FLAGS_ENABLE))
272 return;
273
274 /* VF FLR */
275 ctrl &= ~PCI_MSIX_FLAGS_ENABLE;
276 pci_write_config_word(adev->pdev, adev->pdev->msix_cap + PCI_MSIX_FLAGS, ctrl);
277 ctrl |= PCI_MSIX_FLAGS_ENABLE;
278 pci_write_config_word(adev->pdev, adev->pdev->msix_cap + PCI_MSIX_FLAGS, ctrl);
279 }
280
281 /**
282 * amdgpu_irq_init - initialize interrupt handling
283 *
284 * @adev: amdgpu device pointer
285 *
286 * Sets up work functions for hotplug and reset interrupts, enables MSI
287 * functionality, initializes vblank, hotplug and reset interrupt handling.
288 *
289 * Returns:
290 * 0 on success or error code on failure
291 */
amdgpu_irq_init(struct amdgpu_device * adev)292 int amdgpu_irq_init(struct amdgpu_device *adev)
293 {
294 int r = 0;
295 unsigned int irq;
296
297 spin_lock_init(&adev->irq.lock);
298
299 /* Enable MSI if not disabled by module parameter */
300 adev->irq.msi_enabled = false;
301
302 if (amdgpu_msi_ok(adev)) {
303 int nvec = pci_msix_vec_count(adev->pdev);
304 unsigned int flags;
305
306 if (nvec <= 0) {
307 flags = PCI_IRQ_MSI;
308 } else {
309 flags = PCI_IRQ_MSI | PCI_IRQ_MSIX;
310 }
311 /* we only need one vector */
312 nvec = pci_alloc_irq_vectors(adev->pdev, 1, 1, flags);
313 if (nvec > 0) {
314 adev->irq.msi_enabled = true;
315 dev_dbg(adev->dev, "using MSI/MSI-X.\n");
316 }
317 }
318
319 if (!amdgpu_device_has_dc_support(adev)) {
320 if (!adev->enable_virtual_display)
321 /* Disable vblank IRQs aggressively for power-saving */
322 /* XXX: can this be enabled for DC? */
323 adev_to_drm(adev)->vblank_disable_immediate = true;
324
325 r = drm_vblank_init(adev_to_drm(adev), adev->mode_info.num_crtc);
326 if (r)
327 return r;
328
329 /* Pre-DCE11 */
330 INIT_WORK(&adev->hotplug_work,
331 amdgpu_hotplug_work_func);
332 }
333
334 INIT_WORK(&adev->irq.ih1_work, amdgpu_irq_handle_ih1);
335 INIT_WORK(&adev->irq.ih2_work, amdgpu_irq_handle_ih2);
336 INIT_WORK(&adev->irq.ih_soft_work, amdgpu_irq_handle_ih_soft);
337
338 /* Use vector 0 for MSI-X. */
339 r = pci_irq_vector(adev->pdev, 0);
340 if (r < 0)
341 return r;
342 irq = r;
343
344 /* PCI devices require shared interrupts. */
345 r = request_irq(irq, amdgpu_irq_handler, IRQF_SHARED, adev_to_drm(adev)->driver->name,
346 adev_to_drm(adev));
347 if (r) {
348 if (!amdgpu_device_has_dc_support(adev))
349 flush_work(&adev->hotplug_work);
350 return r;
351 }
352 adev->irq.installed = true;
353 adev->irq.irq = irq;
354 adev_to_drm(adev)->max_vblank_count = 0x00ffffff;
355
356 DRM_DEBUG("amdgpu: irq initialized.\n");
357 return 0;
358 }
359
360
amdgpu_irq_fini_hw(struct amdgpu_device * adev)361 void amdgpu_irq_fini_hw(struct amdgpu_device *adev)
362 {
363 if (adev->irq.installed) {
364 free_irq(adev->irq.irq, adev_to_drm(adev));
365 adev->irq.installed = false;
366 if (adev->irq.msi_enabled)
367 pci_free_irq_vectors(adev->pdev);
368
369 if (!amdgpu_device_has_dc_support(adev))
370 flush_work(&adev->hotplug_work);
371 }
372
373 amdgpu_ih_ring_fini(adev, &adev->irq.ih_soft);
374 amdgpu_ih_ring_fini(adev, &adev->irq.ih);
375 amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
376 amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
377 }
378
379 /**
380 * amdgpu_irq_fini_sw - shut down interrupt handling
381 *
382 * @adev: amdgpu device pointer
383 *
384 * Tears down work functions for hotplug and reset interrupts, disables MSI
385 * functionality, shuts down vblank, hotplug and reset interrupt handling,
386 * turns off interrupts from all sources (all ASICs).
387 */
amdgpu_irq_fini_sw(struct amdgpu_device * adev)388 void amdgpu_irq_fini_sw(struct amdgpu_device *adev)
389 {
390 unsigned i, j;
391
392 for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
393 if (!adev->irq.client[i].sources)
394 continue;
395
396 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
397 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
398
399 if (!src)
400 continue;
401
402 kfree(src->enabled_types);
403 src->enabled_types = NULL;
404 }
405 kfree(adev->irq.client[i].sources);
406 adev->irq.client[i].sources = NULL;
407 }
408 }
409
410 /**
411 * amdgpu_irq_add_id - register IRQ source
412 *
413 * @adev: amdgpu device pointer
414 * @client_id: client id
415 * @src_id: source id
416 * @source: IRQ source pointer
417 *
418 * Registers IRQ source on a client.
419 *
420 * Returns:
421 * 0 on success or error code otherwise
422 */
amdgpu_irq_add_id(struct amdgpu_device * adev,unsigned client_id,unsigned src_id,struct amdgpu_irq_src * source)423 int amdgpu_irq_add_id(struct amdgpu_device *adev,
424 unsigned client_id, unsigned src_id,
425 struct amdgpu_irq_src *source)
426 {
427 if (client_id >= AMDGPU_IRQ_CLIENTID_MAX)
428 return -EINVAL;
429
430 if (src_id >= AMDGPU_MAX_IRQ_SRC_ID)
431 return -EINVAL;
432
433 if (!source->funcs)
434 return -EINVAL;
435
436 if (!adev->irq.client[client_id].sources) {
437 adev->irq.client[client_id].sources =
438 kcalloc(AMDGPU_MAX_IRQ_SRC_ID,
439 sizeof(struct amdgpu_irq_src *),
440 GFP_KERNEL);
441 if (!adev->irq.client[client_id].sources)
442 return -ENOMEM;
443 }
444
445 if (adev->irq.client[client_id].sources[src_id] != NULL)
446 return -EINVAL;
447
448 if (source->num_types && !source->enabled_types) {
449 atomic_t *types;
450
451 types = kcalloc(source->num_types, sizeof(atomic_t),
452 GFP_KERNEL);
453 if (!types)
454 return -ENOMEM;
455
456 source->enabled_types = types;
457 }
458
459 adev->irq.client[client_id].sources[src_id] = source;
460 return 0;
461 }
462
463 /**
464 * amdgpu_irq_dispatch - dispatch IRQ to IP blocks
465 *
466 * @adev: amdgpu device pointer
467 * @ih: interrupt ring instance
468 *
469 * Dispatches IRQ to IP blocks.
470 */
amdgpu_irq_dispatch(struct amdgpu_device * adev,struct amdgpu_ih_ring * ih)471 void amdgpu_irq_dispatch(struct amdgpu_device *adev,
472 struct amdgpu_ih_ring *ih)
473 {
474 u32 ring_index = ih->rptr >> 2;
475 struct amdgpu_iv_entry entry;
476 unsigned client_id, src_id;
477 struct amdgpu_irq_src *src;
478 bool handled = false;
479 int r;
480
481 entry.ih = ih;
482 entry.iv_entry = (const uint32_t *)&ih->ring[ring_index];
483 amdgpu_ih_decode_iv(adev, &entry);
484
485 trace_amdgpu_iv(ih - &adev->irq.ih, &entry);
486
487 client_id = entry.client_id;
488 src_id = entry.src_id;
489
490 if (client_id >= AMDGPU_IRQ_CLIENTID_MAX) {
491 DRM_DEBUG("Invalid client_id in IV: %d\n", client_id);
492
493 } else if (src_id >= AMDGPU_MAX_IRQ_SRC_ID) {
494 DRM_DEBUG("Invalid src_id in IV: %d\n", src_id);
495
496 } else if ((client_id == AMDGPU_IRQ_CLIENTID_LEGACY) &&
497 adev->irq.virq[src_id]) {
498 generic_handle_domain_irq(adev->irq.domain, src_id);
499
500 } else if (!adev->irq.client[client_id].sources) {
501 DRM_DEBUG("Unregistered interrupt client_id: %d src_id: %d\n",
502 client_id, src_id);
503
504 } else if ((src = adev->irq.client[client_id].sources[src_id])) {
505 r = src->funcs->process(adev, src, &entry);
506 if (r < 0)
507 DRM_ERROR("error processing interrupt (%d)\n", r);
508 else if (r)
509 handled = true;
510
511 } else {
512 DRM_DEBUG("Unhandled interrupt src_id: %d\n", src_id);
513 }
514
515 /* Send it to amdkfd as well if it isn't already handled */
516 if (!handled)
517 amdgpu_amdkfd_interrupt(adev, entry.iv_entry);
518
519 if (amdgpu_ih_ts_after(ih->processed_timestamp, entry.timestamp))
520 ih->processed_timestamp = entry.timestamp;
521 }
522
523 /**
524 * amdgpu_irq_delegate - delegate IV to soft IH ring
525 *
526 * @adev: amdgpu device pointer
527 * @entry: IV entry
528 * @num_dw: size of IV
529 *
530 * Delegate the IV to the soft IH ring and schedule processing of it. Used
531 * if the hardware delegation to IH1 or IH2 doesn't work for some reason.
532 */
amdgpu_irq_delegate(struct amdgpu_device * adev,struct amdgpu_iv_entry * entry,unsigned int num_dw)533 void amdgpu_irq_delegate(struct amdgpu_device *adev,
534 struct amdgpu_iv_entry *entry,
535 unsigned int num_dw)
536 {
537 amdgpu_ih_ring_write(&adev->irq.ih_soft, entry->iv_entry, num_dw);
538 schedule_work(&adev->irq.ih_soft_work);
539 }
540
541 /**
542 * amdgpu_irq_update - update hardware interrupt state
543 *
544 * @adev: amdgpu device pointer
545 * @src: interrupt source pointer
546 * @type: type of interrupt
547 *
548 * Updates interrupt state for the specific source (all ASICs).
549 */
amdgpu_irq_update(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned type)550 int amdgpu_irq_update(struct amdgpu_device *adev,
551 struct amdgpu_irq_src *src, unsigned type)
552 {
553 unsigned long irqflags;
554 enum amdgpu_interrupt_state state;
555 int r;
556
557 spin_lock_irqsave(&adev->irq.lock, irqflags);
558
559 /* We need to determine after taking the lock, otherwise
560 we might disable just enabled interrupts again */
561 if (amdgpu_irq_enabled(adev, src, type))
562 state = AMDGPU_IRQ_STATE_ENABLE;
563 else
564 state = AMDGPU_IRQ_STATE_DISABLE;
565
566 r = src->funcs->set(adev, src, type, state);
567 spin_unlock_irqrestore(&adev->irq.lock, irqflags);
568 return r;
569 }
570
571 /**
572 * amdgpu_irq_gpu_reset_resume_helper - update interrupt states on all sources
573 *
574 * @adev: amdgpu device pointer
575 *
576 * Updates state of all types of interrupts on all sources on resume after
577 * reset.
578 */
amdgpu_irq_gpu_reset_resume_helper(struct amdgpu_device * adev)579 void amdgpu_irq_gpu_reset_resume_helper(struct amdgpu_device *adev)
580 {
581 int i, j, k;
582
583 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
584 amdgpu_restore_msix(adev);
585
586 for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
587 if (!adev->irq.client[i].sources)
588 continue;
589
590 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
591 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
592
593 if (!src || !src->funcs || !src->funcs->set)
594 continue;
595 for (k = 0; k < src->num_types; k++)
596 amdgpu_irq_update(adev, src, k);
597 }
598 }
599 }
600
601 /**
602 * amdgpu_irq_get - enable interrupt
603 *
604 * @adev: amdgpu device pointer
605 * @src: interrupt source pointer
606 * @type: type of interrupt
607 *
608 * Enables specified type of interrupt on the specified source (all ASICs).
609 *
610 * Returns:
611 * 0 on success or error code otherwise
612 */
amdgpu_irq_get(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned type)613 int amdgpu_irq_get(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
614 unsigned type)
615 {
616 if (!adev->irq.installed)
617 return -ENOENT;
618
619 if (type >= src->num_types)
620 return -EINVAL;
621
622 if (!src->enabled_types || !src->funcs->set)
623 return -EINVAL;
624
625 if (atomic_inc_return(&src->enabled_types[type]) == 1)
626 return amdgpu_irq_update(adev, src, type);
627
628 return 0;
629 }
630
631 /**
632 * amdgpu_irq_put - disable interrupt
633 *
634 * @adev: amdgpu device pointer
635 * @src: interrupt source pointer
636 * @type: type of interrupt
637 *
638 * Enables specified type of interrupt on the specified source (all ASICs).
639 *
640 * Returns:
641 * 0 on success or error code otherwise
642 */
amdgpu_irq_put(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned type)643 int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
644 unsigned type)
645 {
646 if (!adev->irq.installed)
647 return -ENOENT;
648
649 if (type >= src->num_types)
650 return -EINVAL;
651
652 if (!src->enabled_types || !src->funcs->set)
653 return -EINVAL;
654
655 if (WARN_ON(!amdgpu_irq_enabled(adev, src, type)))
656 return -EINVAL;
657
658 if (atomic_dec_and_test(&src->enabled_types[type]))
659 return amdgpu_irq_update(adev, src, type);
660
661 return 0;
662 }
663
664 /**
665 * amdgpu_irq_enabled - check whether interrupt is enabled or not
666 *
667 * @adev: amdgpu device pointer
668 * @src: interrupt source pointer
669 * @type: type of interrupt
670 *
671 * Checks whether the given type of interrupt is enabled on the given source.
672 *
673 * Returns:
674 * *true* if interrupt is enabled, *false* if interrupt is disabled or on
675 * invalid parameters
676 */
amdgpu_irq_enabled(struct amdgpu_device * adev,struct amdgpu_irq_src * src,unsigned type)677 bool amdgpu_irq_enabled(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
678 unsigned type)
679 {
680 if (!adev->irq.installed)
681 return false;
682
683 if (type >= src->num_types)
684 return false;
685
686 if (!src->enabled_types || !src->funcs->set)
687 return false;
688
689 return !!atomic_read(&src->enabled_types[type]);
690 }
691
692 /* XXX: Generic IRQ handling */
amdgpu_irq_mask(struct irq_data * irqd)693 static void amdgpu_irq_mask(struct irq_data *irqd)
694 {
695 /* XXX */
696 }
697
amdgpu_irq_unmask(struct irq_data * irqd)698 static void amdgpu_irq_unmask(struct irq_data *irqd)
699 {
700 /* XXX */
701 }
702
703 /* amdgpu hardware interrupt chip descriptor */
704 static struct irq_chip amdgpu_irq_chip = {
705 .name = "amdgpu-ih",
706 .irq_mask = amdgpu_irq_mask,
707 .irq_unmask = amdgpu_irq_unmask,
708 };
709
710 /**
711 * amdgpu_irqdomain_map - create mapping between virtual and hardware IRQ numbers
712 *
713 * @d: amdgpu IRQ domain pointer (unused)
714 * @irq: virtual IRQ number
715 * @hwirq: hardware irq number
716 *
717 * Current implementation assigns simple interrupt handler to the given virtual
718 * IRQ.
719 *
720 * Returns:
721 * 0 on success or error code otherwise
722 */
amdgpu_irqdomain_map(struct irq_domain * d,unsigned int irq,irq_hw_number_t hwirq)723 static int amdgpu_irqdomain_map(struct irq_domain *d,
724 unsigned int irq, irq_hw_number_t hwirq)
725 {
726 if (hwirq >= AMDGPU_MAX_IRQ_SRC_ID)
727 return -EPERM;
728
729 irq_set_chip_and_handler(irq,
730 &amdgpu_irq_chip, handle_simple_irq);
731 return 0;
732 }
733
734 /* Implementation of methods for amdgpu IRQ domain */
735 static const struct irq_domain_ops amdgpu_hw_irqdomain_ops = {
736 .map = amdgpu_irqdomain_map,
737 };
738
739 /**
740 * amdgpu_irq_add_domain - create a linear IRQ domain
741 *
742 * @adev: amdgpu device pointer
743 *
744 * Creates an IRQ domain for GPU interrupt sources
745 * that may be driven by another driver (e.g., ACP).
746 *
747 * Returns:
748 * 0 on success or error code otherwise
749 */
amdgpu_irq_add_domain(struct amdgpu_device * adev)750 int amdgpu_irq_add_domain(struct amdgpu_device *adev)
751 {
752 adev->irq.domain = irq_domain_add_linear(NULL, AMDGPU_MAX_IRQ_SRC_ID,
753 &amdgpu_hw_irqdomain_ops, adev);
754 if (!adev->irq.domain) {
755 DRM_ERROR("GPU irq add domain failed\n");
756 return -ENODEV;
757 }
758
759 return 0;
760 }
761
762 /**
763 * amdgpu_irq_remove_domain - remove the IRQ domain
764 *
765 * @adev: amdgpu device pointer
766 *
767 * Removes the IRQ domain for GPU interrupt sources
768 * that may be driven by another driver (e.g., ACP).
769 */
amdgpu_irq_remove_domain(struct amdgpu_device * adev)770 void amdgpu_irq_remove_domain(struct amdgpu_device *adev)
771 {
772 if (adev->irq.domain) {
773 irq_domain_remove(adev->irq.domain);
774 adev->irq.domain = NULL;
775 }
776 }
777
778 /**
779 * amdgpu_irq_create_mapping - create mapping between domain Linux IRQs
780 *
781 * @adev: amdgpu device pointer
782 * @src_id: IH source id
783 *
784 * Creates mapping between a domain IRQ (GPU IH src id) and a Linux IRQ
785 * Use this for components that generate a GPU interrupt, but are driven
786 * by a different driver (e.g., ACP).
787 *
788 * Returns:
789 * Linux IRQ
790 */
amdgpu_irq_create_mapping(struct amdgpu_device * adev,unsigned src_id)791 unsigned amdgpu_irq_create_mapping(struct amdgpu_device *adev, unsigned src_id)
792 {
793 adev->irq.virq[src_id] = irq_create_mapping(adev->irq.domain, src_id);
794
795 return adev->irq.virq[src_id];
796 }
797