Home | History | Annotate | Line # | Download | only in radeon
radeon_irq_kms.c revision 1.1.1.3
      1 /*	$NetBSD: radeon_irq_kms.c,v 1.1.1.3 2021/12/18 20:15:48 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2008 Advanced Micro Devices, Inc.
      5  * Copyright 2008 Red Hat Inc.
      6  * Copyright 2009 Jerome Glisse.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the "Software"),
     10  * to deal in the Software without restriction, including without limitation
     11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  * and/or sell copies of the Software, and to permit persons to whom the
     13  * Software is furnished to do so, subject to the following conditions:
     14  *
     15  * The above copyright notice and this permission notice shall be included in
     16  * all copies or substantial portions of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     24  * OTHER DEALINGS IN THE SOFTWARE.
     25  *
     26  * Authors: Dave Airlie
     27  *          Alex Deucher
     28  *          Jerome Glisse
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: radeon_irq_kms.c,v 1.1.1.3 2021/12/18 20:15:48 riastradh Exp $");
     33 
     34 #include <linux/pci.h>
     35 #include <linux/pm_runtime.h>
     36 
     37 #include <drm/drm_crtc_helper.h>
     38 #include <drm/drm_device.h>
     39 #include <drm/drm_irq.h>
     40 #include <drm/drm_probe_helper.h>
     41 #include <drm/drm_vblank.h>
     42 #include <drm/radeon_drm.h>
     43 
     44 #include "atom.h"
     45 #include "radeon.h"
     46 #include "radeon_reg.h"
     47 
     48 
     49 #define RADEON_WAIT_IDLE_TIMEOUT 200
     50 
     51 /**
     52  * radeon_driver_irq_handler_kms - irq handler for KMS
     53  *
     54  * @int irq, void *arg: args
     55  *
     56  * This is the irq handler for the radeon KMS driver (all asics).
     57  * radeon_irq_process is a macro that points to the per-asic
     58  * irq handler callback.
     59  */
     60 irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg)
     61 {
     62 	struct drm_device *dev = (struct drm_device *) arg;
     63 	struct radeon_device *rdev = dev->dev_private;
     64 	irqreturn_t ret;
     65 
     66 	ret = radeon_irq_process(rdev);
     67 	if (ret == IRQ_HANDLED)
     68 		pm_runtime_mark_last_busy(dev->dev);
     69 	return ret;
     70 }
     71 
     72 /*
     73  * Handle hotplug events outside the interrupt handler proper.
     74  */
     75 /**
     76  * radeon_hotplug_work_func - display hotplug work handler
     77  *
     78  * @work: work struct
     79  *
     80  * This is the hot plug event work handler (all asics).
     81  * The work gets scheduled from the irq handler if there
     82  * was a hot plug interrupt.  It walks the connector table
     83  * and calls the hotplug handler for each one, then sends
     84  * a drm hotplug event to alert userspace.
     85  */
     86 static void radeon_hotplug_work_func(struct work_struct *work)
     87 {
     88 	struct radeon_device *rdev = container_of(work, struct radeon_device,
     89 						  hotplug_work.work);
     90 	struct drm_device *dev = rdev->ddev;
     91 	struct drm_mode_config *mode_config = &dev->mode_config;
     92 	struct drm_connector *connector;
     93 
     94 	/* we can race here at startup, some boards seem to trigger
     95 	 * hotplug irqs when they shouldn't. */
     96 	if (!rdev->mode_info.mode_config_initialized)
     97 		return;
     98 
     99 	mutex_lock(&mode_config->mutex);
    100 	list_for_each_entry(connector, &mode_config->connector_list, head)
    101 		radeon_connector_hotplug(connector);
    102 	mutex_unlock(&mode_config->mutex);
    103 	/* Just fire off a uevent and let userspace tell us what to do */
    104 	drm_helper_hpd_irq_event(dev);
    105 }
    106 
    107 static void radeon_dp_work_func(struct work_struct *work)
    108 {
    109 	struct radeon_device *rdev = container_of(work, struct radeon_device,
    110 						  dp_work);
    111 	struct drm_device *dev = rdev->ddev;
    112 	struct drm_mode_config *mode_config = &dev->mode_config;
    113 	struct drm_connector *connector;
    114 
    115 	/* this should take a mutex */
    116 	list_for_each_entry(connector, &mode_config->connector_list, head)
    117 		radeon_connector_hotplug(connector);
    118 }
    119 /**
    120  * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
    121  *
    122  * @dev: drm dev pointer
    123  *
    124  * Gets the hw ready to enable irqs (all asics).
    125  * This function disables all interrupt sources on the GPU.
    126  */
    127 void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
    128 {
    129 	struct radeon_device *rdev = dev->dev_private;
    130 	unsigned long irqflags;
    131 	unsigned i;
    132 
    133 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    134 	/* Disable *all* interrupts */
    135 	for (i = 0; i < RADEON_NUM_RINGS; i++)
    136 		atomic_set(&rdev->irq.ring_int[i], 0);
    137 	rdev->irq.dpm_thermal = false;
    138 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
    139 		rdev->irq.hpd[i] = false;
    140 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
    141 		rdev->irq.crtc_vblank_int[i] = false;
    142 		atomic_set(&rdev->irq.pflip[i], 0);
    143 		rdev->irq.afmt[i] = false;
    144 	}
    145 	radeon_irq_set(rdev);
    146 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    147 	/* Clear bits */
    148 	radeon_irq_process(rdev);
    149 }
    150 
    151 /**
    152  * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
    153  *
    154  * @dev: drm dev pointer
    155  *
    156  * Handles stuff to be done after enabling irqs (all asics).
    157  * Returns 0 on success.
    158  */
    159 int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
    160 {
    161 	struct radeon_device *rdev = dev->dev_private;
    162 
    163 	if (ASIC_IS_AVIVO(rdev))
    164 		dev->max_vblank_count = 0x00ffffff;
    165 	else
    166 		dev->max_vblank_count = 0x001fffff;
    167 
    168 	return 0;
    169 }
    170 
    171 /**
    172  * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
    173  *
    174  * @dev: drm dev pointer
    175  *
    176  * This function disables all interrupt sources on the GPU (all asics).
    177  */
    178 void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
    179 {
    180 	struct radeon_device *rdev = dev->dev_private;
    181 	unsigned long irqflags;
    182 	unsigned i;
    183 
    184 	if (rdev == NULL) {
    185 		return;
    186 	}
    187 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    188 	/* Disable *all* interrupts */
    189 	for (i = 0; i < RADEON_NUM_RINGS; i++)
    190 		atomic_set(&rdev->irq.ring_int[i], 0);
    191 	rdev->irq.dpm_thermal = false;
    192 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
    193 		rdev->irq.hpd[i] = false;
    194 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
    195 		rdev->irq.crtc_vblank_int[i] = false;
    196 		atomic_set(&rdev->irq.pflip[i], 0);
    197 		rdev->irq.afmt[i] = false;
    198 	}
    199 	radeon_irq_set(rdev);
    200 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    201 }
    202 
    203 /**
    204  * radeon_msi_ok - asic specific msi checks
    205  *
    206  * @rdev: radeon device pointer
    207  *
    208  * Handles asic specific MSI checks to determine if
    209  * MSIs should be enabled on a particular chip (all asics).
    210  * Returns true if MSIs should be enabled, false if MSIs
    211  * should not be enabled.
    212  */
    213 static bool radeon_msi_ok(struct radeon_device *rdev)
    214 {
    215 	/* RV370/RV380 was first asic with MSI support */
    216 	if (rdev->family < CHIP_RV380)
    217 		return false;
    218 
    219 	/* MSIs don't work on AGP */
    220 	if (rdev->flags & RADEON_IS_AGP)
    221 		return false;
    222 
    223 	/*
    224 	 * Older chips have a HW limitation, they can only generate 40 bits
    225 	 * of address for "64-bit" MSIs which breaks on some platforms, notably
    226 	 * IBM POWER servers, so we limit them
    227 	 */
    228 	if (rdev->family < CHIP_BONAIRE) {
    229 		dev_info(rdev->dev, "radeon: MSI limited to 32-bit\n");
    230 		rdev->pdev->no_64bit_msi = 1;
    231 	}
    232 
    233 	/* force MSI on */
    234 	if (radeon_msi == 1)
    235 		return true;
    236 	else if (radeon_msi == 0)
    237 		return false;
    238 
    239 	/* Quirks */
    240 	/* HP RS690 only seems to work with MSIs. */
    241 	if ((rdev->pdev->device == 0x791f) &&
    242 	    (rdev->pdev->subsystem_vendor == 0x103c) &&
    243 	    (rdev->pdev->subsystem_device == 0x30c2))
    244 		return true;
    245 
    246 	/* Dell RS690 only seems to work with MSIs. */
    247 	if ((rdev->pdev->device == 0x791f) &&
    248 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
    249 	    (rdev->pdev->subsystem_device == 0x01fc))
    250 		return true;
    251 
    252 	/* Dell RS690 only seems to work with MSIs. */
    253 	if ((rdev->pdev->device == 0x791f) &&
    254 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
    255 	    (rdev->pdev->subsystem_device == 0x01fd))
    256 		return true;
    257 
    258 	/* Gateway RS690 only seems to work with MSIs. */
    259 	if ((rdev->pdev->device == 0x791f) &&
    260 	    (rdev->pdev->subsystem_vendor == 0x107b) &&
    261 	    (rdev->pdev->subsystem_device == 0x0185))
    262 		return true;
    263 
    264 	/* try and enable MSIs by default on all RS690s */
    265 	if (rdev->family == CHIP_RS690)
    266 		return true;
    267 
    268 	/* RV515 seems to have MSI issues where it loses
    269 	 * MSI rearms occasionally. This leads to lockups and freezes.
    270 	 * disable it by default.
    271 	 */
    272 	if (rdev->family == CHIP_RV515)
    273 		return false;
    274 	if (rdev->flags & RADEON_IS_IGP) {
    275 		/* APUs work fine with MSIs */
    276 		if (rdev->family >= CHIP_PALM)
    277 			return true;
    278 		/* lots of IGPs have problems with MSIs */
    279 		return false;
    280 	}
    281 
    282 	return true;
    283 }
    284 
    285 /**
    286  * radeon_irq_kms_init - init driver interrupt info
    287  *
    288  * @rdev: radeon device pointer
    289  *
    290  * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
    291  * Returns 0 for success, error for failure.
    292  */
    293 int radeon_irq_kms_init(struct radeon_device *rdev)
    294 {
    295 	int r = 0;
    296 
    297 	spin_lock_init(&rdev->irq.lock);
    298 
    299 	/* Disable vblank irqs aggressively for power-saving */
    300 	rdev->ddev->vblank_disable_immediate = true;
    301 
    302 	r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
    303 	if (r) {
    304 		return r;
    305 	}
    306 
    307 	/* enable msi */
    308 	rdev->msi_enabled = 0;
    309 
    310 	if (radeon_msi_ok(rdev)) {
    311 		int ret = pci_enable_msi(rdev->pdev);
    312 		if (!ret) {
    313 			rdev->msi_enabled = 1;
    314 			dev_info(rdev->dev, "radeon: using MSI.\n");
    315 		}
    316 	}
    317 
    318 	INIT_DELAYED_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
    319 	INIT_WORK(&rdev->dp_work, radeon_dp_work_func);
    320 	INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
    321 
    322 	rdev->irq.installed = true;
    323 	r = drm_irq_install(rdev->ddev, rdev->ddev->pdev->irq);
    324 	if (r) {
    325 		rdev->irq.installed = false;
    326 		flush_delayed_work(&rdev->hotplug_work);
    327 		return r;
    328 	}
    329 
    330 	DRM_INFO("radeon: irq initialized.\n");
    331 	return 0;
    332 }
    333 
    334 /**
    335  * radeon_irq_kms_fini - tear down driver interrupt info
    336  *
    337  * @rdev: radeon device pointer
    338  *
    339  * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
    340  */
    341 void radeon_irq_kms_fini(struct radeon_device *rdev)
    342 {
    343 	if (rdev->irq.installed) {
    344 		drm_irq_uninstall(rdev->ddev);
    345 		rdev->irq.installed = false;
    346 		if (rdev->msi_enabled)
    347 			pci_disable_msi(rdev->pdev);
    348 		flush_delayed_work(&rdev->hotplug_work);
    349 	}
    350 }
    351 
    352 /**
    353  * radeon_irq_kms_sw_irq_get - enable software interrupt
    354  *
    355  * @rdev: radeon device pointer
    356  * @ring: ring whose interrupt you want to enable
    357  *
    358  * Enables the software interrupt for a specific ring (all asics).
    359  * The software interrupt is generally used to signal a fence on
    360  * a particular ring.
    361  */
    362 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
    363 {
    364 	unsigned long irqflags;
    365 
    366 	if (!rdev->ddev->irq_enabled)
    367 		return;
    368 
    369 	if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
    370 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    371 		radeon_irq_set(rdev);
    372 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    373 	}
    374 }
    375 
    376 /**
    377  * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
    378  *
    379  * @rdev: radeon device pointer
    380  * @ring: ring whose interrupt you want to enable
    381  *
    382  * Enables the software interrupt for a specific ring (all asics).
    383  * The software interrupt is generally used to signal a fence on
    384  * a particular ring.
    385  */
    386 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
    387 {
    388 	return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
    389 }
    390 
    391 /**
    392  * radeon_irq_kms_sw_irq_put - disable software interrupt
    393  *
    394  * @rdev: radeon device pointer
    395  * @ring: ring whose interrupt you want to disable
    396  *
    397  * Disables the software interrupt for a specific ring (all asics).
    398  * The software interrupt is generally used to signal a fence on
    399  * a particular ring.
    400  */
    401 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
    402 {
    403 	unsigned long irqflags;
    404 
    405 	if (!rdev->ddev->irq_enabled)
    406 		return;
    407 
    408 	if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
    409 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    410 		radeon_irq_set(rdev);
    411 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    412 	}
    413 }
    414 
    415 /**
    416  * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
    417  *
    418  * @rdev: radeon device pointer
    419  * @crtc: crtc whose interrupt you want to enable
    420  *
    421  * Enables the pageflip interrupt for a specific crtc (all asics).
    422  * For pageflips we use the vblank interrupt source.
    423  */
    424 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
    425 {
    426 	unsigned long irqflags;
    427 
    428 	if (crtc < 0 || crtc >= rdev->num_crtc)
    429 		return;
    430 
    431 	if (!rdev->ddev->irq_enabled)
    432 		return;
    433 
    434 	if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
    435 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    436 		radeon_irq_set(rdev);
    437 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    438 	}
    439 }
    440 
    441 /**
    442  * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
    443  *
    444  * @rdev: radeon device pointer
    445  * @crtc: crtc whose interrupt you want to disable
    446  *
    447  * Disables the pageflip interrupt for a specific crtc (all asics).
    448  * For pageflips we use the vblank interrupt source.
    449  */
    450 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
    451 {
    452 	unsigned long irqflags;
    453 
    454 	if (crtc < 0 || crtc >= rdev->num_crtc)
    455 		return;
    456 
    457 	if (!rdev->ddev->irq_enabled)
    458 		return;
    459 
    460 	if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
    461 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    462 		radeon_irq_set(rdev);
    463 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    464 	}
    465 }
    466 
    467 /**
    468  * radeon_irq_kms_enable_afmt - enable audio format change interrupt
    469  *
    470  * @rdev: radeon device pointer
    471  * @block: afmt block whose interrupt you want to enable
    472  *
    473  * Enables the afmt change interrupt for a specific afmt block (all asics).
    474  */
    475 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
    476 {
    477 	unsigned long irqflags;
    478 
    479 	if (!rdev->ddev->irq_enabled)
    480 		return;
    481 
    482 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    483 	rdev->irq.afmt[block] = true;
    484 	radeon_irq_set(rdev);
    485 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    486 
    487 }
    488 
    489 /**
    490  * radeon_irq_kms_disable_afmt - disable audio format change interrupt
    491  *
    492  * @rdev: radeon device pointer
    493  * @block: afmt block whose interrupt you want to disable
    494  *
    495  * Disables the afmt change interrupt for a specific afmt block (all asics).
    496  */
    497 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
    498 {
    499 	unsigned long irqflags;
    500 
    501 	if (!rdev->ddev->irq_enabled)
    502 		return;
    503 
    504 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    505 	rdev->irq.afmt[block] = false;
    506 	radeon_irq_set(rdev);
    507 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    508 }
    509 
    510 /**
    511  * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
    512  *
    513  * @rdev: radeon device pointer
    514  * @hpd_mask: mask of hpd pins you want to enable.
    515  *
    516  * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
    517  */
    518 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
    519 {
    520 	unsigned long irqflags;
    521 	int i;
    522 
    523 	if (!rdev->ddev->irq_enabled)
    524 		return;
    525 
    526 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    527 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
    528 		rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
    529 	radeon_irq_set(rdev);
    530 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    531 }
    532 
    533 /**
    534  * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
    535  *
    536  * @rdev: radeon device pointer
    537  * @hpd_mask: mask of hpd pins you want to disable.
    538  *
    539  * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
    540  */
    541 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
    542 {
    543 	unsigned long irqflags;
    544 	int i;
    545 
    546 	if (!rdev->ddev->irq_enabled)
    547 		return;
    548 
    549 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    550 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
    551 		rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
    552 	radeon_irq_set(rdev);
    553 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    554 }
    555 
    556 /**
    557  * radeon_irq_kms_update_int_n - helper for updating interrupt enable registers
    558  *
    559  * @rdev: radeon device pointer
    560  * @reg: the register to write to enable/disable interrupts
    561  * @mask: the mask that enables the interrupts
    562  * @enable: whether to enable or disable the interrupt register
    563  * @name: the name of the interrupt register to print to the kernel log
    564  * @num: the number of the interrupt register to print to the kernel log
    565  *
    566  * Helper for updating the enable state of interrupt registers. Checks whether
    567  * or not the interrupt matches the enable state we want. If it doesn't, then
    568  * we update it and print a debugging message to the kernel log indicating the
    569  * new state of the interrupt register.
    570  *
    571  * Used for updating sequences of interrupts registers like HPD1, HPD2, etc.
    572  */
    573 void radeon_irq_kms_set_irq_n_enabled(struct radeon_device *rdev,
    574 				      u32 reg, u32 mask,
    575 				      bool enable, const char *name, unsigned n)
    576 {
    577 	u32 tmp = RREG32(reg);
    578 
    579 	/* Interrupt state didn't change */
    580 	if (!!(tmp & mask) == enable)
    581 		return;
    582 
    583 	if (enable) {
    584 		DRM_DEBUG("%s%d interrupts enabled\n", name, n);
    585 		WREG32(reg, tmp |= mask);
    586 	} else {
    587 		DRM_DEBUG("%s%d interrupts disabled\n", name, n);
    588 		WREG32(reg, tmp & ~mask);
    589 	}
    590 }
    591