Home | History | Annotate | Line # | Download | only in amdgpu
      1  1.9  riastrad /*	$NetBSD: amdgpu_irq.c,v 1.9 2021/12/19 12:38:49 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2008 Advanced Micro Devices, Inc.
      5  1.1  riastrad  * Copyright 2008 Red Hat Inc.
      6  1.1  riastrad  * Copyright 2009 Jerome Glisse.
      7  1.1  riastrad  *
      8  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      9  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     10  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     11  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     13  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     14  1.1  riastrad  *
     15  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     16  1.1  riastrad  * all copies or substantial portions of the Software.
     17  1.1  riastrad  *
     18  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     24  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     25  1.1  riastrad  *
     26  1.1  riastrad  * Authors: Dave Airlie
     27  1.1  riastrad  *          Alex Deucher
     28  1.1  riastrad  *          Jerome Glisse
     29  1.1  riastrad  */
     30  1.5  riastrad 
     31  1.5  riastrad /**
     32  1.5  riastrad  * DOC: Interrupt Handling
     33  1.5  riastrad  *
     34  1.5  riastrad  * Interrupts generated within GPU hardware raise interrupt requests that are
     35  1.5  riastrad  * passed to amdgpu IRQ handler which is responsible for detecting source and
     36  1.5  riastrad  * type of the interrupt and dispatching matching handlers. If handling an
     37  1.5  riastrad  * interrupt requires calling kernel functions that may sleep processing is
     38  1.5  riastrad  * dispatched to work handlers.
     39  1.5  riastrad  *
     40  1.5  riastrad  * If MSI functionality is not disabled by module parameter then MSI
     41  1.5  riastrad  * support will be enabled.
     42  1.5  riastrad  *
     43  1.5  riastrad  * For GPU interrupt sources that may be driven by another driver, IRQ domain
     44  1.5  riastrad  * support is used (with mapping between virtual and hardware IRQs).
     45  1.5  riastrad  */
     46  1.5  riastrad 
     47  1.1  riastrad #include <sys/cdefs.h>
     48  1.9  riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_irq.c,v 1.9 2021/12/19 12:38:49 riastradh Exp $");
     49  1.1  riastrad 
     50  1.1  riastrad #include <linux/irq.h>
     51  1.5  riastrad #include <linux/pci.h>
     52  1.5  riastrad 
     53  1.1  riastrad #include <drm/drm_crtc_helper.h>
     54  1.5  riastrad #include <drm/drm_irq.h>
     55  1.5  riastrad #include <drm/drm_vblank.h>
     56  1.1  riastrad #include <drm/amdgpu_drm.h>
     57  1.1  riastrad #include "amdgpu.h"
     58  1.1  riastrad #include "amdgpu_ih.h"
     59  1.1  riastrad #include "atom.h"
     60  1.1  riastrad #include "amdgpu_connectors.h"
     61  1.5  riastrad #include "amdgpu_trace.h"
     62  1.5  riastrad #include "amdgpu_amdkfd.h"
     63  1.5  riastrad #include "amdgpu_ras.h"
     64  1.1  riastrad 
     65  1.1  riastrad #include <linux/pm_runtime.h>
     66  1.1  riastrad 
     67  1.5  riastrad #ifdef CONFIG_DRM_AMD_DC
     68  1.5  riastrad #include "amdgpu_dm_irq.h"
     69  1.5  riastrad #endif
     70  1.5  riastrad 
     71  1.1  riastrad #define AMDGPU_WAIT_IDLE_TIMEOUT 200
     72  1.1  riastrad 
     73  1.1  riastrad /**
     74  1.5  riastrad  * amdgpu_hotplug_work_func - work handler for display hotplug event
     75  1.1  riastrad  *
     76  1.5  riastrad  * @work: work struct pointer
     77  1.1  riastrad  *
     78  1.5  riastrad  * This is the hotplug event work handler (all ASICs).
     79  1.5  riastrad  * The work gets scheduled from the IRQ handler if there
     80  1.5  riastrad  * was a hotplug interrupt.  It walks through the connector table
     81  1.5  riastrad  * and calls hotplug handler for each connector. After this, it sends
     82  1.5  riastrad  * a DRM hotplug event to alert userspace.
     83  1.5  riastrad  *
     84  1.5  riastrad  * This design approach is required in order to defer hotplug event handling
     85  1.5  riastrad  * from the IRQ handler to a work handler because hotplug handler has to use
     86  1.5  riastrad  * mutexes which cannot be locked in an IRQ handler (since &mutex_lock may
     87  1.5  riastrad  * sleep).
     88  1.1  riastrad  */
     89  1.1  riastrad static void amdgpu_hotplug_work_func(struct work_struct *work)
     90  1.1  riastrad {
     91  1.1  riastrad 	struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
     92  1.1  riastrad 						  hotplug_work);
     93  1.1  riastrad 	struct drm_device *dev = adev->ddev;
     94  1.1  riastrad 	struct drm_mode_config *mode_config = &dev->mode_config;
     95  1.1  riastrad 	struct drm_connector *connector;
     96  1.5  riastrad 	struct drm_connector_list_iter iter;
     97  1.1  riastrad 
     98  1.1  riastrad 	mutex_lock(&mode_config->mutex);
     99  1.5  riastrad 	drm_connector_list_iter_begin(dev, &iter);
    100  1.5  riastrad 	drm_for_each_connector_iter(connector, &iter)
    101  1.5  riastrad 		amdgpu_connector_hotplug(connector);
    102  1.5  riastrad 	drm_connector_list_iter_end(&iter);
    103  1.1  riastrad 	mutex_unlock(&mode_config->mutex);
    104  1.1  riastrad 	/* Just fire off a uevent and let userspace tell us what to do */
    105  1.1  riastrad 	drm_helper_hpd_irq_event(dev);
    106  1.1  riastrad }
    107  1.1  riastrad 
    108  1.1  riastrad /**
    109  1.5  riastrad  * amdgpu_irq_disable_all - disable *all* interrupts
    110  1.1  riastrad  *
    111  1.5  riastrad  * @adev: amdgpu device pointer
    112  1.1  riastrad  *
    113  1.5  riastrad  * Disable all types of interrupts from all sources.
    114  1.1  riastrad  */
    115  1.5  riastrad void amdgpu_irq_disable_all(struct amdgpu_device *adev)
    116  1.1  riastrad {
    117  1.1  riastrad 	unsigned long irqflags;
    118  1.5  riastrad 	unsigned i, j, k;
    119  1.1  riastrad 	int r;
    120  1.1  riastrad 
    121  1.1  riastrad 	spin_lock_irqsave(&adev->irq.lock, irqflags);
    122  1.5  riastrad 	for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
    123  1.5  riastrad 		if (!adev->irq.client[i].sources)
    124  1.5  riastrad 			continue;
    125  1.5  riastrad 
    126  1.5  riastrad 		for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
    127  1.5  riastrad 			struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
    128  1.1  riastrad 
    129  1.5  riastrad 			if (!src || !src->funcs->set || !src->num_types)
    130  1.5  riastrad 				continue;
    131  1.1  riastrad 
    132  1.5  riastrad 			for (k = 0; k < src->num_types; ++k) {
    133  1.5  riastrad 				atomic_set(&src->enabled_types[k], 0);
    134  1.5  riastrad 				r = src->funcs->set(adev, src, k,
    135  1.5  riastrad 						    AMDGPU_IRQ_STATE_DISABLE);
    136  1.5  riastrad 				if (r)
    137  1.5  riastrad 					DRM_ERROR("error disabling interrupt (%d)\n",
    138  1.5  riastrad 						  r);
    139  1.5  riastrad 			}
    140  1.1  riastrad 		}
    141  1.1  riastrad 	}
    142  1.1  riastrad 	spin_unlock_irqrestore(&adev->irq.lock, irqflags);
    143  1.1  riastrad }
    144  1.1  riastrad 
    145  1.1  riastrad /**
    146  1.5  riastrad  * amdgpu_irq_handler - IRQ handler
    147  1.1  riastrad  *
    148  1.5  riastrad  * @irq: IRQ number (unused)
    149  1.5  riastrad  * @arg: pointer to DRM device
    150  1.1  riastrad  *
    151  1.5  riastrad  * IRQ handler for amdgpu driver (all ASICs).
    152  1.5  riastrad  *
    153  1.5  riastrad  * Returns:
    154  1.5  riastrad  * result of handling the IRQ, as defined by &irqreturn_t
    155  1.1  riastrad  */
    156  1.5  riastrad irqreturn_t amdgpu_irq_handler(DRM_IRQ_ARGS)
    157  1.1  riastrad {
    158  1.5  riastrad 	struct drm_device *dev = (struct drm_device *) arg;
    159  1.1  riastrad 	struct amdgpu_device *adev = dev->dev_private;
    160  1.5  riastrad 	irqreturn_t ret;
    161  1.5  riastrad 
    162  1.5  riastrad 	ret = amdgpu_ih_process(adev, &adev->irq.ih);
    163  1.5  riastrad 	if (ret == IRQ_HANDLED)
    164  1.5  riastrad 		pm_runtime_mark_last_busy(dev->dev);
    165  1.1  riastrad 
    166  1.5  riastrad 	/* For the hardware that cannot enable bif ring for both ras_controller_irq
    167  1.5  riastrad          * and ras_err_evnet_athub_irq ih cookies, the driver has to poll status
    168  1.5  riastrad 	 * register to check whether the interrupt is triggered or not, and properly
    169  1.5  riastrad 	 * ack the interrupt if it is there
    170  1.5  riastrad 	 */
    171  1.5  riastrad 	if (amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__PCIE_BIF)) {
    172  1.5  riastrad 		if (adev->nbio.funcs &&
    173  1.5  riastrad 		    adev->nbio.funcs->handle_ras_controller_intr_no_bifring)
    174  1.5  riastrad 			adev->nbio.funcs->handle_ras_controller_intr_no_bifring(adev);
    175  1.5  riastrad 
    176  1.5  riastrad 		if (adev->nbio.funcs &&
    177  1.5  riastrad 		    adev->nbio.funcs->handle_ras_err_event_athub_intr_no_bifring)
    178  1.5  riastrad 			adev->nbio.funcs->handle_ras_err_event_athub_intr_no_bifring(adev);
    179  1.5  riastrad 	}
    180  1.1  riastrad 
    181  1.5  riastrad 	return ret;
    182  1.1  riastrad }
    183  1.1  riastrad 
    184  1.1  riastrad /**
    185  1.5  riastrad  * amdgpu_irq_handle_ih1 - kick of processing for IH1
    186  1.1  riastrad  *
    187  1.5  riastrad  * @work: work structure in struct amdgpu_irq
    188  1.1  riastrad  *
    189  1.5  riastrad  * Kick of processing IH ring 1.
    190  1.1  riastrad  */
    191  1.5  riastrad static void amdgpu_irq_handle_ih1(struct work_struct *work)
    192  1.1  riastrad {
    193  1.5  riastrad 	struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
    194  1.5  riastrad 						  irq.ih1_work);
    195  1.1  riastrad 
    196  1.5  riastrad 	amdgpu_ih_process(adev, &adev->irq.ih1);
    197  1.1  riastrad }
    198  1.1  riastrad 
    199  1.1  riastrad /**
    200  1.5  riastrad  * amdgpu_irq_handle_ih2 - kick of processing for IH2
    201  1.1  riastrad  *
    202  1.5  riastrad  * @work: work structure in struct amdgpu_irq
    203  1.1  riastrad  *
    204  1.5  riastrad  * Kick of processing IH ring 2.
    205  1.1  riastrad  */
    206  1.5  riastrad static void amdgpu_irq_handle_ih2(struct work_struct *work)
    207  1.1  riastrad {
    208  1.5  riastrad 	struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
    209  1.5  riastrad 						  irq.ih2_work);
    210  1.1  riastrad 
    211  1.5  riastrad 	amdgpu_ih_process(adev, &adev->irq.ih2);
    212  1.1  riastrad }
    213  1.1  riastrad 
    214  1.1  riastrad /**
    215  1.5  riastrad  * amdgpu_msi_ok - check whether MSI functionality is enabled
    216  1.5  riastrad  *
    217  1.5  riastrad  * @adev: amdgpu device pointer (unused)
    218  1.1  riastrad  *
    219  1.5  riastrad  * Checks whether MSI functionality has been disabled via module parameter
    220  1.5  riastrad  * (all ASICs).
    221  1.1  riastrad  *
    222  1.5  riastrad  * Returns:
    223  1.5  riastrad  * *true* if MSIs are allowed to be enabled or *false* otherwise
    224  1.1  riastrad  */
    225  1.1  riastrad static bool amdgpu_msi_ok(struct amdgpu_device *adev)
    226  1.1  riastrad {
    227  1.1  riastrad 	if (amdgpu_msi == 1)
    228  1.1  riastrad 		return true;
    229  1.1  riastrad 	else if (amdgpu_msi == 0)
    230  1.1  riastrad 		return false;
    231  1.1  riastrad 
    232  1.1  riastrad 	return true;
    233  1.1  riastrad }
    234  1.1  riastrad 
    235  1.1  riastrad /**
    236  1.5  riastrad  * amdgpu_irq_init - initialize interrupt handling
    237  1.1  riastrad  *
    238  1.1  riastrad  * @adev: amdgpu device pointer
    239  1.1  riastrad  *
    240  1.5  riastrad  * Sets up work functions for hotplug and reset interrupts, enables MSI
    241  1.5  riastrad  * functionality, initializes vblank, hotplug and reset interrupt handling.
    242  1.5  riastrad  *
    243  1.5  riastrad  * Returns:
    244  1.5  riastrad  * 0 on success or error code on failure
    245  1.1  riastrad  */
    246  1.1  riastrad int amdgpu_irq_init(struct amdgpu_device *adev)
    247  1.1  riastrad {
    248  1.1  riastrad 	int r = 0;
    249  1.1  riastrad 
    250  1.1  riastrad 	spin_lock_init(&adev->irq.lock);
    251  1.5  riastrad 
    252  1.5  riastrad 	/* Enable MSI if not disabled by module parameter */
    253  1.1  riastrad 	adev->irq.msi_enabled = false;
    254  1.1  riastrad 
    255  1.1  riastrad 	if (amdgpu_msi_ok(adev)) {
    256  1.9  riastrad #ifdef __NetBSD__		/* XXX amdgpu msix */
    257  1.9  riastrad 		if (pci_enable_msi(adev->pdev) == 0) {
    258  1.9  riastrad 			adev->irq.msi_enabled = true;
    259  1.9  riastrad 			dev_dbg(adev->dev, "amdgpu: using MSI/MSI-X.\n");
    260  1.9  riastrad 		} else {
    261  1.9  riastrad 			dev_err(adev->dev, "amdgpu: failed to enable MSI\n");
    262  1.9  riastrad 		}
    263  1.9  riastrad #else
    264  1.5  riastrad 		int nvec = pci_msix_vec_count(adev->pdev);
    265  1.5  riastrad 		unsigned int flags;
    266  1.5  riastrad 
    267  1.5  riastrad 		if (nvec <= 0) {
    268  1.5  riastrad 			flags = PCI_IRQ_MSI;
    269  1.5  riastrad 		} else {
    270  1.5  riastrad 			flags = PCI_IRQ_MSI | PCI_IRQ_MSIX;
    271  1.5  riastrad 		}
    272  1.5  riastrad 		/* we only need one vector */
    273  1.5  riastrad 		nvec = pci_alloc_irq_vectors(adev->pdev, 1, 1, flags);
    274  1.5  riastrad 		if (nvec > 0) {
    275  1.1  riastrad 			adev->irq.msi_enabled = true;
    276  1.5  riastrad 			dev_dbg(adev->dev, "amdgpu: using MSI/MSI-X.\n");
    277  1.1  riastrad 		}
    278  1.6  riastrad #endif
    279  1.1  riastrad 	}
    280  1.1  riastrad 
    281  1.5  riastrad 	if (!amdgpu_device_has_dc_support(adev)) {
    282  1.5  riastrad 		if (!adev->enable_virtual_display)
    283  1.5  riastrad 			/* Disable vblank IRQs aggressively for power-saving */
    284  1.5  riastrad 			/* XXX: can this be enabled for DC? */
    285  1.5  riastrad 			adev->ddev->vblank_disable_immediate = true;
    286  1.5  riastrad 
    287  1.5  riastrad 		r = drm_vblank_init(adev->ddev, adev->mode_info.num_crtc);
    288  1.5  riastrad 		if (r)
    289  1.5  riastrad 			return r;
    290  1.5  riastrad 
    291  1.5  riastrad 		/* Pre-DCE11 */
    292  1.5  riastrad 		INIT_WORK(&adev->hotplug_work,
    293  1.5  riastrad 				amdgpu_hotplug_work_func);
    294  1.5  riastrad 	}
    295  1.5  riastrad 
    296  1.5  riastrad 	INIT_WORK(&adev->irq.ih1_work, amdgpu_irq_handle_ih1);
    297  1.5  riastrad 	INIT_WORK(&adev->irq.ih2_work, amdgpu_irq_handle_ih2);
    298  1.1  riastrad 
    299  1.1  riastrad 	adev->irq.installed = true;
    300  1.5  riastrad #ifdef __NetBSD__	/* XXX post-merge address comment below */
    301  1.3  riastrad 	r = drm_irq_install(adev->ddev);
    302  1.3  riastrad #else
    303  1.5  riastrad 	/* Use vector 0 for MSI-X */
    304  1.5  riastrad 	r = drm_irq_install(adev->ddev, pci_irq_vector(adev->pdev, 0));
    305  1.3  riastrad #endif
    306  1.1  riastrad 	if (r) {
    307  1.1  riastrad 		adev->irq.installed = false;
    308  1.5  riastrad 		if (!amdgpu_device_has_dc_support(adev))
    309  1.5  riastrad 			flush_work(&adev->hotplug_work);
    310  1.1  riastrad 		return r;
    311  1.1  riastrad 	}
    312  1.5  riastrad 	adev->ddev->max_vblank_count = 0x00ffffff;
    313  1.1  riastrad 
    314  1.5  riastrad 	DRM_DEBUG("amdgpu: irq initialized.\n");
    315  1.1  riastrad 	return 0;
    316  1.1  riastrad }
    317  1.1  riastrad 
    318  1.1  riastrad /**
    319  1.5  riastrad  * amdgpu_irq_fini - shut down interrupt handling
    320  1.1  riastrad  *
    321  1.1  riastrad  * @adev: amdgpu device pointer
    322  1.1  riastrad  *
    323  1.5  riastrad  * Tears down work functions for hotplug and reset interrupts, disables MSI
    324  1.5  riastrad  * functionality, shuts down vblank, hotplug and reset interrupt handling,
    325  1.5  riastrad  * turns off interrupts from all sources (all ASICs).
    326  1.1  riastrad  */
    327  1.1  riastrad void amdgpu_irq_fini(struct amdgpu_device *adev)
    328  1.1  riastrad {
    329  1.5  riastrad 	unsigned i, j;
    330  1.1  riastrad 
    331  1.1  riastrad 	if (adev->irq.installed) {
    332  1.1  riastrad 		drm_irq_uninstall(adev->ddev);
    333  1.1  riastrad 		adev->irq.installed = false;
    334  1.6  riastrad #ifndef __NetBSD__		/* XXX amdgpu msix */
    335  1.1  riastrad 		if (adev->irq.msi_enabled)
    336  1.5  riastrad 			pci_free_irq_vectors(adev->pdev);
    337  1.6  riastrad #endif
    338  1.5  riastrad 		if (!amdgpu_device_has_dc_support(adev))
    339  1.5  riastrad 			flush_work(&adev->hotplug_work);
    340  1.1  riastrad 	}
    341  1.1  riastrad 
    342  1.5  riastrad 	for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
    343  1.5  riastrad 		if (!adev->irq.client[i].sources)
    344  1.5  riastrad 			continue;
    345  1.5  riastrad 
    346  1.5  riastrad 		for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
    347  1.5  riastrad 			struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
    348  1.1  riastrad 
    349  1.5  riastrad 			if (!src)
    350  1.5  riastrad 				continue;
    351  1.1  riastrad 
    352  1.5  riastrad 			kfree(src->enabled_types);
    353  1.5  riastrad 			src->enabled_types = NULL;
    354  1.5  riastrad 			if (src->data) {
    355  1.5  riastrad 				kfree(src->data);
    356  1.5  riastrad 				kfree(src);
    357  1.5  riastrad 				adev->irq.client[i].sources[j] = NULL;
    358  1.5  riastrad 			}
    359  1.1  riastrad 		}
    360  1.5  riastrad 		kfree(adev->irq.client[i].sources);
    361  1.5  riastrad 		adev->irq.client[i].sources = NULL;
    362  1.1  riastrad 	}
    363  1.8  riastrad 
    364  1.8  riastrad 	spin_lock_destroy(&adev->irq.lock);
    365  1.1  riastrad }
    366  1.1  riastrad 
    367  1.1  riastrad /**
    368  1.5  riastrad  * amdgpu_irq_add_id - register IRQ source
    369  1.1  riastrad  *
    370  1.1  riastrad  * @adev: amdgpu device pointer
    371  1.5  riastrad  * @client_id: client id
    372  1.5  riastrad  * @src_id: source id
    373  1.5  riastrad  * @source: IRQ source pointer
    374  1.5  riastrad  *
    375  1.5  riastrad  * Registers IRQ source on a client.
    376  1.1  riastrad  *
    377  1.5  riastrad  * Returns:
    378  1.5  riastrad  * 0 on success or error code otherwise
    379  1.1  riastrad  */
    380  1.5  riastrad int amdgpu_irq_add_id(struct amdgpu_device *adev,
    381  1.5  riastrad 		      unsigned client_id, unsigned src_id,
    382  1.1  riastrad 		      struct amdgpu_irq_src *source)
    383  1.1  riastrad {
    384  1.5  riastrad 	if (client_id >= AMDGPU_IRQ_CLIENTID_MAX)
    385  1.5  riastrad 		return -EINVAL;
    386  1.5  riastrad 
    387  1.1  riastrad 	if (src_id >= AMDGPU_MAX_IRQ_SRC_ID)
    388  1.1  riastrad 		return -EINVAL;
    389  1.1  riastrad 
    390  1.5  riastrad 	if (!source->funcs)
    391  1.1  riastrad 		return -EINVAL;
    392  1.1  riastrad 
    393  1.5  riastrad 	if (!adev->irq.client[client_id].sources) {
    394  1.5  riastrad 		adev->irq.client[client_id].sources =
    395  1.5  riastrad 			kcalloc(AMDGPU_MAX_IRQ_SRC_ID,
    396  1.5  riastrad 				sizeof(struct amdgpu_irq_src *),
    397  1.5  riastrad 				GFP_KERNEL);
    398  1.5  riastrad 		if (!adev->irq.client[client_id].sources)
    399  1.5  riastrad 			return -ENOMEM;
    400  1.5  riastrad 	}
    401  1.5  riastrad 
    402  1.5  riastrad 	if (adev->irq.client[client_id].sources[src_id] != NULL)
    403  1.1  riastrad 		return -EINVAL;
    404  1.1  riastrad 
    405  1.1  riastrad 	if (source->num_types && !source->enabled_types) {
    406  1.1  riastrad 		atomic_t *types;
    407  1.1  riastrad 
    408  1.1  riastrad 		types = kcalloc(source->num_types, sizeof(atomic_t),
    409  1.1  riastrad 				GFP_KERNEL);
    410  1.1  riastrad 		if (!types)
    411  1.1  riastrad 			return -ENOMEM;
    412  1.1  riastrad 
    413  1.1  riastrad 		source->enabled_types = types;
    414  1.1  riastrad 	}
    415  1.1  riastrad 
    416  1.5  riastrad 	adev->irq.client[client_id].sources[src_id] = source;
    417  1.1  riastrad 	return 0;
    418  1.1  riastrad }
    419  1.1  riastrad 
    420  1.1  riastrad /**
    421  1.5  riastrad  * amdgpu_irq_dispatch - dispatch IRQ to IP blocks
    422  1.1  riastrad  *
    423  1.1  riastrad  * @adev: amdgpu device pointer
    424  1.5  riastrad  * @ih: interrupt ring instance
    425  1.1  riastrad  *
    426  1.5  riastrad  * Dispatches IRQ to IP blocks.
    427  1.1  riastrad  */
    428  1.1  riastrad void amdgpu_irq_dispatch(struct amdgpu_device *adev,
    429  1.5  riastrad 			 struct amdgpu_ih_ring *ih)
    430  1.1  riastrad {
    431  1.5  riastrad 	u32 ring_index = ih->rptr >> 2;
    432  1.5  riastrad 	struct amdgpu_iv_entry entry;
    433  1.5  riastrad 	unsigned client_id, src_id;
    434  1.1  riastrad 	struct amdgpu_irq_src *src;
    435  1.5  riastrad 	bool handled = false;
    436  1.1  riastrad 	int r;
    437  1.1  riastrad 
    438  1.6  riastrad 	entry.iv_entry = (const uint32_t *)__UNVOLATILE(&ih->ring[ring_index]);
    439  1.5  riastrad 	amdgpu_ih_decode_iv(adev, &entry);
    440  1.5  riastrad 
    441  1.5  riastrad 	trace_amdgpu_iv(ih - &adev->irq.ih, &entry);
    442  1.5  riastrad 
    443  1.5  riastrad 	client_id = entry.client_id;
    444  1.5  riastrad 	src_id = entry.src_id;
    445  1.5  riastrad 
    446  1.5  riastrad 	if (client_id >= AMDGPU_IRQ_CLIENTID_MAX) {
    447  1.5  riastrad 		DRM_DEBUG("Invalid client_id in IV: %d\n", client_id);
    448  1.5  riastrad 
    449  1.5  riastrad 	} else	if (src_id >= AMDGPU_MAX_IRQ_SRC_ID) {
    450  1.1  riastrad 		DRM_DEBUG("Invalid src_id in IV: %d\n", src_id);
    451  1.1  riastrad 
    452  1.6  riastrad #ifndef __NetBSD__		/* XXX amdgpu irq */
    453  1.5  riastrad 	} else if (adev->irq.virq[src_id]) {
    454  1.5  riastrad 		generic_handle_irq(irq_find_mapping(adev->irq.domain, src_id));
    455  1.6  riastrad #endif
    456  1.5  riastrad 
    457  1.5  riastrad 	} else if (!adev->irq.client[client_id].sources) {
    458  1.5  riastrad 		DRM_DEBUG("Unregistered interrupt client_id: %d src_id: %d\n",
    459  1.5  riastrad 			  client_id, src_id);
    460  1.5  riastrad 
    461  1.5  riastrad 	} else if ((src = adev->irq.client[client_id].sources[src_id])) {
    462  1.5  riastrad 		r = src->funcs->process(adev, src, &entry);
    463  1.5  riastrad 		if (r < 0)
    464  1.5  riastrad 			DRM_ERROR("error processing interrupt (%d)\n", r);
    465  1.5  riastrad 		else if (r)
    466  1.5  riastrad 			handled = true;
    467  1.5  riastrad 
    468  1.5  riastrad 	} else {
    469  1.1  riastrad 		DRM_DEBUG("Unhandled interrupt src_id: %d\n", src_id);
    470  1.1  riastrad 	}
    471  1.1  riastrad 
    472  1.5  riastrad 	/* Send it to amdkfd as well if it isn't already handled */
    473  1.5  riastrad 	if (!handled)
    474  1.5  riastrad 		amdgpu_amdkfd_interrupt(adev, entry.iv_entry);
    475  1.1  riastrad }
    476  1.1  riastrad 
    477  1.1  riastrad /**
    478  1.5  riastrad  * amdgpu_irq_update - update hardware interrupt state
    479  1.1  riastrad  *
    480  1.1  riastrad  * @adev: amdgpu device pointer
    481  1.5  riastrad  * @src: interrupt source pointer
    482  1.5  riastrad  * @type: type of interrupt
    483  1.1  riastrad  *
    484  1.5  riastrad  * Updates interrupt state for the specific source (all ASICs).
    485  1.1  riastrad  */
    486  1.1  riastrad int amdgpu_irq_update(struct amdgpu_device *adev,
    487  1.1  riastrad 			     struct amdgpu_irq_src *src, unsigned type)
    488  1.1  riastrad {
    489  1.1  riastrad 	unsigned long irqflags;
    490  1.1  riastrad 	enum amdgpu_interrupt_state state;
    491  1.1  riastrad 	int r;
    492  1.1  riastrad 
    493  1.1  riastrad 	spin_lock_irqsave(&adev->irq.lock, irqflags);
    494  1.1  riastrad 
    495  1.5  riastrad 	/* We need to determine after taking the lock, otherwise
    496  1.1  riastrad 	   we might disable just enabled interrupts again */
    497  1.1  riastrad 	if (amdgpu_irq_enabled(adev, src, type))
    498  1.1  riastrad 		state = AMDGPU_IRQ_STATE_ENABLE;
    499  1.1  riastrad 	else
    500  1.1  riastrad 		state = AMDGPU_IRQ_STATE_DISABLE;
    501  1.1  riastrad 
    502  1.1  riastrad 	r = src->funcs->set(adev, src, type, state);
    503  1.1  riastrad 	spin_unlock_irqrestore(&adev->irq.lock, irqflags);
    504  1.1  riastrad 	return r;
    505  1.1  riastrad }
    506  1.1  riastrad 
    507  1.1  riastrad /**
    508  1.5  riastrad  * amdgpu_irq_gpu_reset_resume_helper - update interrupt states on all sources
    509  1.5  riastrad  *
    510  1.5  riastrad  * @adev: amdgpu device pointer
    511  1.5  riastrad  *
    512  1.5  riastrad  * Updates state of all types of interrupts on all sources on resume after
    513  1.5  riastrad  * reset.
    514  1.5  riastrad  */
    515  1.5  riastrad void amdgpu_irq_gpu_reset_resume_helper(struct amdgpu_device *adev)
    516  1.5  riastrad {
    517  1.5  riastrad 	int i, j, k;
    518  1.5  riastrad 
    519  1.5  riastrad 	for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
    520  1.5  riastrad 		if (!adev->irq.client[i].sources)
    521  1.5  riastrad 			continue;
    522  1.5  riastrad 
    523  1.5  riastrad 		for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
    524  1.5  riastrad 			struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
    525  1.5  riastrad 
    526  1.5  riastrad 			if (!src)
    527  1.5  riastrad 				continue;
    528  1.5  riastrad 			for (k = 0; k < src->num_types; k++)
    529  1.5  riastrad 				amdgpu_irq_update(adev, src, k);
    530  1.5  riastrad 		}
    531  1.5  riastrad 	}
    532  1.5  riastrad }
    533  1.5  riastrad 
    534  1.5  riastrad /**
    535  1.1  riastrad  * amdgpu_irq_get - enable interrupt
    536  1.1  riastrad  *
    537  1.1  riastrad  * @adev: amdgpu device pointer
    538  1.5  riastrad  * @src: interrupt source pointer
    539  1.5  riastrad  * @type: type of interrupt
    540  1.5  riastrad  *
    541  1.5  riastrad  * Enables specified type of interrupt on the specified source (all ASICs).
    542  1.1  riastrad  *
    543  1.5  riastrad  * Returns:
    544  1.5  riastrad  * 0 on success or error code otherwise
    545  1.1  riastrad  */
    546  1.1  riastrad int amdgpu_irq_get(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
    547  1.1  riastrad 		   unsigned type)
    548  1.1  riastrad {
    549  1.1  riastrad 	if (!adev->ddev->irq_enabled)
    550  1.1  riastrad 		return -ENOENT;
    551  1.1  riastrad 
    552  1.1  riastrad 	if (type >= src->num_types)
    553  1.1  riastrad 		return -EINVAL;
    554  1.1  riastrad 
    555  1.1  riastrad 	if (!src->enabled_types || !src->funcs->set)
    556  1.1  riastrad 		return -EINVAL;
    557  1.1  riastrad 
    558  1.1  riastrad 	if (atomic_inc_return(&src->enabled_types[type]) == 1)
    559  1.1  riastrad 		return amdgpu_irq_update(adev, src, type);
    560  1.1  riastrad 
    561  1.1  riastrad 	return 0;
    562  1.1  riastrad }
    563  1.1  riastrad 
    564  1.1  riastrad /**
    565  1.1  riastrad  * amdgpu_irq_put - disable interrupt
    566  1.1  riastrad  *
    567  1.1  riastrad  * @adev: amdgpu device pointer
    568  1.5  riastrad  * @src: interrupt source pointer
    569  1.5  riastrad  * @type: type of interrupt
    570  1.1  riastrad  *
    571  1.5  riastrad  * Enables specified type of interrupt on the specified source (all ASICs).
    572  1.5  riastrad  *
    573  1.5  riastrad  * Returns:
    574  1.5  riastrad  * 0 on success or error code otherwise
    575  1.1  riastrad  */
    576  1.1  riastrad int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
    577  1.1  riastrad 		   unsigned type)
    578  1.1  riastrad {
    579  1.1  riastrad 	if (!adev->ddev->irq_enabled)
    580  1.1  riastrad 		return -ENOENT;
    581  1.1  riastrad 
    582  1.1  riastrad 	if (type >= src->num_types)
    583  1.1  riastrad 		return -EINVAL;
    584  1.1  riastrad 
    585  1.1  riastrad 	if (!src->enabled_types || !src->funcs->set)
    586  1.1  riastrad 		return -EINVAL;
    587  1.1  riastrad 
    588  1.1  riastrad 	if (atomic_dec_and_test(&src->enabled_types[type]))
    589  1.1  riastrad 		return amdgpu_irq_update(adev, src, type);
    590  1.1  riastrad 
    591  1.1  riastrad 	return 0;
    592  1.1  riastrad }
    593  1.1  riastrad 
    594  1.1  riastrad /**
    595  1.5  riastrad  * amdgpu_irq_enabled - check whether interrupt is enabled or not
    596  1.1  riastrad  *
    597  1.1  riastrad  * @adev: amdgpu device pointer
    598  1.5  riastrad  * @src: interrupt source pointer
    599  1.5  riastrad  * @type: type of interrupt
    600  1.5  riastrad  *
    601  1.5  riastrad  * Checks whether the given type of interrupt is enabled on the given source.
    602  1.1  riastrad  *
    603  1.5  riastrad  * Returns:
    604  1.5  riastrad  * *true* if interrupt is enabled, *false* if interrupt is disabled or on
    605  1.5  riastrad  * invalid parameters
    606  1.1  riastrad  */
    607  1.1  riastrad bool amdgpu_irq_enabled(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
    608  1.1  riastrad 			unsigned type)
    609  1.1  riastrad {
    610  1.1  riastrad 	if (!adev->ddev->irq_enabled)
    611  1.1  riastrad 		return false;
    612  1.1  riastrad 
    613  1.1  riastrad 	if (type >= src->num_types)
    614  1.1  riastrad 		return false;
    615  1.1  riastrad 
    616  1.1  riastrad 	if (!src->enabled_types || !src->funcs->set)
    617  1.1  riastrad 		return false;
    618  1.1  riastrad 
    619  1.1  riastrad 	return !!atomic_read(&src->enabled_types[type]);
    620  1.1  riastrad }
    621  1.5  riastrad 
    622  1.7  riastrad #ifndef __NetBSD__		/* XXX amdgpu irq domain */
    623  1.6  riastrad 
    624  1.5  riastrad /* XXX: Generic IRQ handling */
    625  1.5  riastrad static void amdgpu_irq_mask(struct irq_data *irqd)
    626  1.5  riastrad {
    627  1.5  riastrad 	/* XXX */
    628  1.5  riastrad }
    629  1.5  riastrad 
    630  1.5  riastrad static void amdgpu_irq_unmask(struct irq_data *irqd)
    631  1.5  riastrad {
    632  1.5  riastrad 	/* XXX */
    633  1.5  riastrad }
    634  1.5  riastrad 
    635  1.5  riastrad /* amdgpu hardware interrupt chip descriptor */
    636  1.5  riastrad static struct irq_chip amdgpu_irq_chip = {
    637  1.5  riastrad 	.name = "amdgpu-ih",
    638  1.5  riastrad 	.irq_mask = amdgpu_irq_mask,
    639  1.5  riastrad 	.irq_unmask = amdgpu_irq_unmask,
    640  1.5  riastrad };
    641  1.5  riastrad 
    642  1.5  riastrad /**
    643  1.5  riastrad  * amdgpu_irqdomain_map - create mapping between virtual and hardware IRQ numbers
    644  1.5  riastrad  *
    645  1.5  riastrad  * @d: amdgpu IRQ domain pointer (unused)
    646  1.5  riastrad  * @irq: virtual IRQ number
    647  1.5  riastrad  * @hwirq: hardware irq number
    648  1.5  riastrad  *
    649  1.5  riastrad  * Current implementation assigns simple interrupt handler to the given virtual
    650  1.5  riastrad  * IRQ.
    651  1.5  riastrad  *
    652  1.5  riastrad  * Returns:
    653  1.5  riastrad  * 0 on success or error code otherwise
    654  1.5  riastrad  */
    655  1.5  riastrad static int amdgpu_irqdomain_map(struct irq_domain *d,
    656  1.5  riastrad 				unsigned int irq, irq_hw_number_t hwirq)
    657  1.5  riastrad {
    658  1.5  riastrad 	if (hwirq >= AMDGPU_MAX_IRQ_SRC_ID)
    659  1.5  riastrad 		return -EPERM;
    660  1.5  riastrad 
    661  1.5  riastrad 	irq_set_chip_and_handler(irq,
    662  1.5  riastrad 				 &amdgpu_irq_chip, handle_simple_irq);
    663  1.5  riastrad 	return 0;
    664  1.5  riastrad }
    665  1.5  riastrad 
    666  1.5  riastrad /* Implementation of methods for amdgpu IRQ domain */
    667  1.5  riastrad static const struct irq_domain_ops amdgpu_hw_irqdomain_ops = {
    668  1.5  riastrad 	.map = amdgpu_irqdomain_map,
    669  1.5  riastrad };
    670  1.5  riastrad 
    671  1.7  riastrad #endif	/* __NetBSD__ */
    672  1.7  riastrad 
    673  1.5  riastrad /**
    674  1.5  riastrad  * amdgpu_irq_add_domain - create a linear IRQ domain
    675  1.5  riastrad  *
    676  1.5  riastrad  * @adev: amdgpu device pointer
    677  1.5  riastrad  *
    678  1.5  riastrad  * Creates an IRQ domain for GPU interrupt sources
    679  1.5  riastrad  * that may be driven by another driver (e.g., ACP).
    680  1.5  riastrad  *
    681  1.5  riastrad  * Returns:
    682  1.5  riastrad  * 0 on success or error code otherwise
    683  1.5  riastrad  */
    684  1.5  riastrad int amdgpu_irq_add_domain(struct amdgpu_device *adev)
    685  1.5  riastrad {
    686  1.7  riastrad #ifndef __NetBSD__		/* XXX amdgpu irq domain */
    687  1.5  riastrad 	adev->irq.domain = irq_domain_add_linear(NULL, AMDGPU_MAX_IRQ_SRC_ID,
    688  1.5  riastrad 						 &amdgpu_hw_irqdomain_ops, adev);
    689  1.5  riastrad 	if (!adev->irq.domain) {
    690  1.5  riastrad 		DRM_ERROR("GPU irq add domain failed\n");
    691  1.5  riastrad 		return -ENODEV;
    692  1.5  riastrad 	}
    693  1.7  riastrad #endif
    694  1.5  riastrad 
    695  1.5  riastrad 	return 0;
    696  1.5  riastrad }
    697  1.5  riastrad 
    698  1.5  riastrad /**
    699  1.5  riastrad  * amdgpu_irq_remove_domain - remove the IRQ domain
    700  1.5  riastrad  *
    701  1.5  riastrad  * @adev: amdgpu device pointer
    702  1.5  riastrad  *
    703  1.5  riastrad  * Removes the IRQ domain for GPU interrupt sources
    704  1.5  riastrad  * that may be driven by another driver (e.g., ACP).
    705  1.5  riastrad  */
    706  1.5  riastrad void amdgpu_irq_remove_domain(struct amdgpu_device *adev)
    707  1.5  riastrad {
    708  1.7  riastrad #ifndef __NetBSD__		/* XXX amdgpu irq domain */
    709  1.5  riastrad 	if (adev->irq.domain) {
    710  1.5  riastrad 		irq_domain_remove(adev->irq.domain);
    711  1.5  riastrad 		adev->irq.domain = NULL;
    712  1.5  riastrad 	}
    713  1.7  riastrad #endif
    714  1.5  riastrad }
    715  1.5  riastrad 
    716  1.5  riastrad /**
    717  1.5  riastrad  * amdgpu_irq_create_mapping - create mapping between domain Linux IRQs
    718  1.5  riastrad  *
    719  1.5  riastrad  * @adev: amdgpu device pointer
    720  1.5  riastrad  * @src_id: IH source id
    721  1.5  riastrad  *
    722  1.5  riastrad  * Creates mapping between a domain IRQ (GPU IH src id) and a Linux IRQ
    723  1.5  riastrad  * Use this for components that generate a GPU interrupt, but are driven
    724  1.5  riastrad  * by a different driver (e.g., ACP).
    725  1.5  riastrad  *
    726  1.5  riastrad  * Returns:
    727  1.5  riastrad  * Linux IRQ
    728  1.5  riastrad  */
    729  1.5  riastrad unsigned amdgpu_irq_create_mapping(struct amdgpu_device *adev, unsigned src_id)
    730  1.5  riastrad {
    731  1.7  riastrad #ifdef __NetBSD__		/* XXX amdgpu irq domain */
    732  1.7  riastrad 	return 0;
    733  1.7  riastrad #else
    734  1.5  riastrad 	adev->irq.virq[src_id] = irq_create_mapping(adev->irq.domain, src_id);
    735  1.5  riastrad 
    736  1.5  riastrad 	return adev->irq.virq[src_id];
    737  1.7  riastrad #endif
    738  1.5  riastrad }
    739