2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Authors: Dave Airlie
30 * DOC: Interrupt Handling
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.
38 * If MSI functionality is not disabled by module parameter then MSI
39 * support will be enabled.
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).
45 #include <linux/irq.h>
46 #include <linux/pci.h>
48 #include <drm/drm_vblank.h>
49 #include <drm/amdgpu_drm.h>
50 #include <drm/drm_drv.h>
52 #include "amdgpu_ih.h"
54 #include "amdgpu_connectors.h"
55 #include "amdgpu_trace.h"
56 #include "amdgpu_amdkfd.h"
57 #include "amdgpu_ras.h"
59 #include <linux/pm_runtime.h>
61 #ifdef CONFIG_DRM_AMD_DC
62 #include "amdgpu_dm_irq.h"
65 #define AMDGPU_WAIT_IDLE_TIMEOUT 200
67 const char *soc15_ih_clientid_name[] = {
103 * amdgpu_irq_disable_all - disable *all* interrupts
105 * @adev: amdgpu device pointer
107 * Disable all types of interrupts from all sources.
109 void amdgpu_irq_disable_all(struct amdgpu_device *adev)
111 unsigned long irqflags;
115 spin_lock_irqsave(&adev->irq.lock, irqflags);
116 for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
117 if (!adev->irq.client[i].sources)
120 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
121 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
123 if (!src || !src->funcs->set || !src->num_types)
126 for (k = 0; k < src->num_types; ++k) {
127 atomic_set(&src->enabled_types[k], 0);
128 r = src->funcs->set(adev, src, k,
129 AMDGPU_IRQ_STATE_DISABLE);
131 DRM_ERROR("error disabling interrupt (%d)\n",
136 spin_unlock_irqrestore(&adev->irq.lock, irqflags);
140 * amdgpu_irq_handler - IRQ handler
142 * @irq: IRQ number (unused)
143 * @arg: pointer to DRM device
145 * IRQ handler for amdgpu driver (all ASICs).
148 * result of handling the IRQ, as defined by &irqreturn_t
150 static irqreturn_t amdgpu_irq_handler(int irq, void *arg)
152 struct drm_device *dev = (struct drm_device *) arg;
153 struct amdgpu_device *adev = drm_to_adev(dev);
156 ret = amdgpu_ih_process(adev, &adev->irq.ih);
157 if (ret == IRQ_HANDLED)
158 pm_runtime_mark_last_busy(dev->dev);
160 amdgpu_ras_interrupt_fatal_error_handler(adev);
166 * amdgpu_irq_handle_ih1 - kick of processing for IH1
168 * @work: work structure in struct amdgpu_irq
170 * Kick of processing IH ring 1.
172 static void amdgpu_irq_handle_ih1(struct work_struct *work)
174 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
177 amdgpu_ih_process(adev, &adev->irq.ih1);
181 * amdgpu_irq_handle_ih2 - kick of processing for IH2
183 * @work: work structure in struct amdgpu_irq
185 * Kick of processing IH ring 2.
187 static void amdgpu_irq_handle_ih2(struct work_struct *work)
189 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
192 amdgpu_ih_process(adev, &adev->irq.ih2);
196 * amdgpu_irq_handle_ih_soft - kick of processing for ih_soft
198 * @work: work structure in struct amdgpu_irq
200 * Kick of processing IH soft ring.
202 static void amdgpu_irq_handle_ih_soft(struct work_struct *work)
204 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
207 amdgpu_ih_process(adev, &adev->irq.ih_soft);
211 * amdgpu_msi_ok - check whether MSI functionality is enabled
213 * @adev: amdgpu device pointer (unused)
215 * Checks whether MSI functionality has been disabled via module parameter
219 * *true* if MSIs are allowed to be enabled or *false* otherwise
221 static bool amdgpu_msi_ok(struct amdgpu_device *adev)
225 else if (amdgpu_msi == 0)
231 static void amdgpu_restore_msix(struct amdgpu_device *adev)
235 pci_read_config_word(adev->pdev, adev->pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
236 if (!(ctrl & PCI_MSIX_FLAGS_ENABLE))
240 ctrl &= ~PCI_MSIX_FLAGS_ENABLE;
241 pci_write_config_word(adev->pdev, adev->pdev->msix_cap + PCI_MSIX_FLAGS, ctrl);
242 ctrl |= PCI_MSIX_FLAGS_ENABLE;
243 pci_write_config_word(adev->pdev, adev->pdev->msix_cap + PCI_MSIX_FLAGS, ctrl);
247 * amdgpu_irq_init - initialize interrupt handling
249 * @adev: amdgpu device pointer
251 * Sets up work functions for hotplug and reset interrupts, enables MSI
252 * functionality, initializes vblank, hotplug and reset interrupt handling.
255 * 0 on success or error code on failure
257 int amdgpu_irq_init(struct amdgpu_device *adev)
262 spin_lock_init(&adev->irq.lock);
264 /* Enable MSI if not disabled by module parameter */
265 adev->irq.msi_enabled = false;
267 if (amdgpu_msi_ok(adev)) {
268 int nvec = pci_msix_vec_count(adev->pdev);
274 flags = PCI_IRQ_MSI | PCI_IRQ_MSIX;
276 /* we only need one vector */
277 nvec = pci_alloc_irq_vectors(adev->pdev, 1, 1, flags);
279 adev->irq.msi_enabled = true;
280 dev_dbg(adev->dev, "using MSI/MSI-X.\n");
284 INIT_WORK(&adev->irq.ih1_work, amdgpu_irq_handle_ih1);
285 INIT_WORK(&adev->irq.ih2_work, amdgpu_irq_handle_ih2);
286 INIT_WORK(&adev->irq.ih_soft_work, amdgpu_irq_handle_ih_soft);
288 /* Use vector 0 for MSI-X. */
289 r = pci_irq_vector(adev->pdev, 0);
294 /* PCI devices require shared interrupts. */
295 r = request_irq(irq, amdgpu_irq_handler, IRQF_SHARED, adev_to_drm(adev)->driver->name,
299 adev->irq.installed = true;
301 adev_to_drm(adev)->max_vblank_count = 0x00ffffff;
303 DRM_DEBUG("amdgpu: irq initialized.\n");
308 void amdgpu_irq_fini_hw(struct amdgpu_device *adev)
310 if (adev->irq.installed) {
311 free_irq(adev->irq.irq, adev_to_drm(adev));
312 adev->irq.installed = false;
313 if (adev->irq.msi_enabled)
314 pci_free_irq_vectors(adev->pdev);
317 amdgpu_ih_ring_fini(adev, &adev->irq.ih_soft);
318 amdgpu_ih_ring_fini(adev, &adev->irq.ih);
319 amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
320 amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
324 * amdgpu_irq_fini_sw - shut down interrupt handling
326 * @adev: amdgpu device pointer
328 * Tears down work functions for hotplug and reset interrupts, disables MSI
329 * functionality, shuts down vblank, hotplug and reset interrupt handling,
330 * turns off interrupts from all sources (all ASICs).
332 void amdgpu_irq_fini_sw(struct amdgpu_device *adev)
336 for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
337 if (!adev->irq.client[i].sources)
340 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
341 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
346 kfree(src->enabled_types);
347 src->enabled_types = NULL;
349 kfree(adev->irq.client[i].sources);
350 adev->irq.client[i].sources = NULL;
355 * amdgpu_irq_add_id - register IRQ source
357 * @adev: amdgpu device pointer
358 * @client_id: client id
360 * @source: IRQ source pointer
362 * Registers IRQ source on a client.
365 * 0 on success or error code otherwise
367 int amdgpu_irq_add_id(struct amdgpu_device *adev,
368 unsigned client_id, unsigned src_id,
369 struct amdgpu_irq_src *source)
371 if (client_id >= AMDGPU_IRQ_CLIENTID_MAX)
374 if (src_id >= AMDGPU_MAX_IRQ_SRC_ID)
380 if (!adev->irq.client[client_id].sources) {
381 adev->irq.client[client_id].sources =
382 kcalloc(AMDGPU_MAX_IRQ_SRC_ID,
383 sizeof(struct amdgpu_irq_src *),
385 if (!adev->irq.client[client_id].sources)
389 if (adev->irq.client[client_id].sources[src_id] != NULL)
392 if (source->num_types && !source->enabled_types) {
395 types = kcalloc(source->num_types, sizeof(atomic_t),
400 source->enabled_types = types;
403 adev->irq.client[client_id].sources[src_id] = source;
408 * amdgpu_irq_dispatch - dispatch IRQ to IP blocks
410 * @adev: amdgpu device pointer
411 * @ih: interrupt ring instance
413 * Dispatches IRQ to IP blocks.
415 void amdgpu_irq_dispatch(struct amdgpu_device *adev,
416 struct amdgpu_ih_ring *ih)
418 u32 ring_index = ih->rptr >> 2;
419 struct amdgpu_iv_entry entry;
420 unsigned client_id, src_id;
421 struct amdgpu_irq_src *src;
422 bool handled = false;
426 entry.iv_entry = (const uint32_t *)&ih->ring[ring_index];
427 amdgpu_ih_decode_iv(adev, &entry);
429 trace_amdgpu_iv(ih - &adev->irq.ih, &entry);
431 client_id = entry.client_id;
432 src_id = entry.src_id;
434 if (client_id >= AMDGPU_IRQ_CLIENTID_MAX) {
435 DRM_DEBUG("Invalid client_id in IV: %d\n", client_id);
437 } else if (src_id >= AMDGPU_MAX_IRQ_SRC_ID) {
438 DRM_DEBUG("Invalid src_id in IV: %d\n", src_id);
440 } else if ((client_id == AMDGPU_IRQ_CLIENTID_LEGACY) &&
441 adev->irq.virq[src_id]) {
442 generic_handle_domain_irq(adev->irq.domain, src_id);
444 } else if (!adev->irq.client[client_id].sources) {
445 DRM_DEBUG("Unregistered interrupt client_id: %d src_id: %d\n",
448 } else if ((src = adev->irq.client[client_id].sources[src_id])) {
449 r = src->funcs->process(adev, src, &entry);
451 DRM_ERROR("error processing interrupt (%d)\n", r);
456 DRM_DEBUG("Unhandled interrupt src_id: %d\n", src_id);
459 /* Send it to amdkfd as well if it isn't already handled */
461 amdgpu_amdkfd_interrupt(adev, entry.iv_entry);
463 if (amdgpu_ih_ts_after(ih->processed_timestamp, entry.timestamp))
464 ih->processed_timestamp = entry.timestamp;
468 * amdgpu_irq_delegate - delegate IV to soft IH ring
470 * @adev: amdgpu device pointer
472 * @num_dw: size of IV
474 * Delegate the IV to the soft IH ring and schedule processing of it. Used
475 * if the hardware delegation to IH1 or IH2 doesn't work for some reason.
477 void amdgpu_irq_delegate(struct amdgpu_device *adev,
478 struct amdgpu_iv_entry *entry,
481 amdgpu_ih_ring_write(&adev->irq.ih_soft, entry->iv_entry, num_dw);
482 schedule_work(&adev->irq.ih_soft_work);
486 * amdgpu_irq_update - update hardware interrupt state
488 * @adev: amdgpu device pointer
489 * @src: interrupt source pointer
490 * @type: type of interrupt
492 * Updates interrupt state for the specific source (all ASICs).
494 int amdgpu_irq_update(struct amdgpu_device *adev,
495 struct amdgpu_irq_src *src, unsigned type)
497 unsigned long irqflags;
498 enum amdgpu_interrupt_state state;
501 spin_lock_irqsave(&adev->irq.lock, irqflags);
503 /* We need to determine after taking the lock, otherwise
504 we might disable just enabled interrupts again */
505 if (amdgpu_irq_enabled(adev, src, type))
506 state = AMDGPU_IRQ_STATE_ENABLE;
508 state = AMDGPU_IRQ_STATE_DISABLE;
510 r = src->funcs->set(adev, src, type, state);
511 spin_unlock_irqrestore(&adev->irq.lock, irqflags);
516 * amdgpu_irq_gpu_reset_resume_helper - update interrupt states on all sources
518 * @adev: amdgpu device pointer
520 * Updates state of all types of interrupts on all sources on resume after
523 void amdgpu_irq_gpu_reset_resume_helper(struct amdgpu_device *adev)
527 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
528 amdgpu_restore_msix(adev);
530 for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
531 if (!adev->irq.client[i].sources)
534 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
535 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
537 if (!src || !src->funcs || !src->funcs->set)
539 for (k = 0; k < src->num_types; k++)
540 amdgpu_irq_update(adev, src, k);
546 * amdgpu_irq_get - enable interrupt
548 * @adev: amdgpu device pointer
549 * @src: interrupt source pointer
550 * @type: type of interrupt
552 * Enables specified type of interrupt on the specified source (all ASICs).
555 * 0 on success or error code otherwise
557 int amdgpu_irq_get(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
560 if (!adev->irq.installed)
563 if (type >= src->num_types)
566 if (!src->enabled_types || !src->funcs->set)
569 if (atomic_inc_return(&src->enabled_types[type]) == 1)
570 return amdgpu_irq_update(adev, src, type);
576 * amdgpu_irq_put - disable interrupt
578 * @adev: amdgpu device pointer
579 * @src: interrupt source pointer
580 * @type: type of interrupt
582 * Enables specified type of interrupt on the specified source (all ASICs).
585 * 0 on success or error code otherwise
587 int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
590 if (!adev->irq.installed)
593 if (type >= src->num_types)
596 if (!src->enabled_types || !src->funcs->set)
599 if (WARN_ON(!amdgpu_irq_enabled(adev, src, type)))
602 if (atomic_dec_and_test(&src->enabled_types[type]))
603 return amdgpu_irq_update(adev, src, type);
609 * amdgpu_irq_enabled - check whether interrupt is enabled or not
611 * @adev: amdgpu device pointer
612 * @src: interrupt source pointer
613 * @type: type of interrupt
615 * Checks whether the given type of interrupt is enabled on the given source.
618 * *true* if interrupt is enabled, *false* if interrupt is disabled or on
621 bool amdgpu_irq_enabled(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
624 if (!adev->irq.installed)
627 if (type >= src->num_types)
630 if (!src->enabled_types || !src->funcs->set)
633 return !!atomic_read(&src->enabled_types[type]);
636 /* XXX: Generic IRQ handling */
637 static void amdgpu_irq_mask(struct irq_data *irqd)
642 static void amdgpu_irq_unmask(struct irq_data *irqd)
647 /* amdgpu hardware interrupt chip descriptor */
648 static struct irq_chip amdgpu_irq_chip = {
650 .irq_mask = amdgpu_irq_mask,
651 .irq_unmask = amdgpu_irq_unmask,
655 * amdgpu_irqdomain_map - create mapping between virtual and hardware IRQ numbers
657 * @d: amdgpu IRQ domain pointer (unused)
658 * @irq: virtual IRQ number
659 * @hwirq: hardware irq number
661 * Current implementation assigns simple interrupt handler to the given virtual
665 * 0 on success or error code otherwise
667 static int amdgpu_irqdomain_map(struct irq_domain *d,
668 unsigned int irq, irq_hw_number_t hwirq)
670 if (hwirq >= AMDGPU_MAX_IRQ_SRC_ID)
673 irq_set_chip_and_handler(irq,
674 &amdgpu_irq_chip, handle_simple_irq);
678 /* Implementation of methods for amdgpu IRQ domain */
679 static const struct irq_domain_ops amdgpu_hw_irqdomain_ops = {
680 .map = amdgpu_irqdomain_map,
684 * amdgpu_irq_add_domain - create a linear IRQ domain
686 * @adev: amdgpu device pointer
688 * Creates an IRQ domain for GPU interrupt sources
689 * that may be driven by another driver (e.g., ACP).
692 * 0 on success or error code otherwise
694 int amdgpu_irq_add_domain(struct amdgpu_device *adev)
696 adev->irq.domain = irq_domain_add_linear(NULL, AMDGPU_MAX_IRQ_SRC_ID,
697 &amdgpu_hw_irqdomain_ops, adev);
698 if (!adev->irq.domain) {
699 DRM_ERROR("GPU irq add domain failed\n");
707 * amdgpu_irq_remove_domain - remove the IRQ domain
709 * @adev: amdgpu device pointer
711 * Removes the IRQ domain for GPU interrupt sources
712 * that may be driven by another driver (e.g., ACP).
714 void amdgpu_irq_remove_domain(struct amdgpu_device *adev)
716 if (adev->irq.domain) {
717 irq_domain_remove(adev->irq.domain);
718 adev->irq.domain = NULL;
723 * amdgpu_irq_create_mapping - create mapping between domain Linux IRQs
725 * @adev: amdgpu device pointer
726 * @src_id: IH source id
728 * Creates mapping between a domain IRQ (GPU IH src id) and a Linux IRQ
729 * Use this for components that generate a GPU interrupt, but are driven
730 * by a different driver (e.g., ACP).
735 unsigned amdgpu_irq_create_mapping(struct amdgpu_device *adev, unsigned src_id)
737 adev->irq.virq[src_id] = irq_create_mapping(adev->irq.domain, src_id);
739 return adev->irq.virq[src_id];