Home | History | Annotate | Line # | Download | only in radeon
radeon_irq_kms.c revision 1.2.32.2
      1 /*	$NetBSD: radeon_irq_kms.c,v 1.2.32.2 2020/04/08 14:08:26 martin 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 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: radeon_irq_kms.c,v 1.2.32.2 2020/04/08 14:08:26 martin Exp $");
     32 
     33 #include <drm/drmP.h>
     34 #include <drm/drm_crtc_helper.h>
     35 #include <drm/radeon_drm.h>
     36 #include "radeon_reg.h"
     37 #include "radeon.h"
     38 #include "atom.h"
     39 
     40 #include <linux/pm_runtime.h>
     41 
     42 #define RADEON_WAIT_IDLE_TIMEOUT 200
     43 
     44 /**
     45  * radeon_driver_irq_handler_kms - irq handler for KMS
     46  *
     47  * @int irq, void *arg: args
     48  *
     49  * This is the irq handler for the radeon KMS driver (all asics).
     50  * radeon_irq_process is a macro that points to the per-asic
     51  * irq handler callback.
     52  */
     53 irqreturn_t radeon_driver_irq_handler_kms(DRM_IRQ_ARGS)
     54 {
     55 	struct drm_device *dev = (struct drm_device *) arg;
     56 	struct radeon_device *rdev = dev->dev_private;
     57 	irqreturn_t ret;
     58 
     59 	ret = radeon_irq_process(rdev);
     60 	if (ret == IRQ_HANDLED)
     61 		pm_runtime_mark_last_busy(dev->dev);
     62 	return ret;
     63 }
     64 
     65 /*
     66  * Handle hotplug events outside the interrupt handler proper.
     67  */
     68 /**
     69  * radeon_hotplug_work_func - display hotplug work handler
     70  *
     71  * @work: work struct
     72  *
     73  * This is the hot plug event work handler (all asics).
     74  * The work gets scheduled from the irq handler if there
     75  * was a hot plug interrupt.  It walks the connector table
     76  * and calls the hotplug handler for each one, then sends
     77  * a drm hotplug event to alert userspace.
     78  */
     79 static void radeon_hotplug_work_func(struct work_struct *work)
     80 {
     81 	struct radeon_device *rdev = container_of(work, struct radeon_device,
     82 						  hotplug_work.work);
     83 	struct drm_device *dev = rdev->ddev;
     84 	struct drm_mode_config *mode_config = &dev->mode_config;
     85 	struct drm_connector *connector;
     86 
     87 	/* we can race here at startup, some boards seem to trigger
     88 	 * hotplug irqs when they shouldn't. */
     89 	if (!rdev->mode_info.mode_config_initialized)
     90 		return;
     91 
     92 	mutex_lock(&mode_config->mutex);
     93 	if (mode_config->num_connector) {
     94 		list_for_each_entry(connector, &mode_config->connector_list, head)
     95 			radeon_connector_hotplug(connector);
     96 	}
     97 	mutex_unlock(&mode_config->mutex);
     98 	/* Just fire off a uevent and let userspace tell us what to do */
     99 	drm_helper_hpd_irq_event(dev);
    100 }
    101 
    102 static void radeon_dp_work_func(struct work_struct *work)
    103 {
    104 	struct radeon_device *rdev = container_of(work, struct radeon_device,
    105 						  dp_work);
    106 	struct drm_device *dev = rdev->ddev;
    107 	struct drm_mode_config *mode_config = &dev->mode_config;
    108 	struct drm_connector *connector;
    109 
    110 	mutex_lock(&mode_config->mutex);
    111 	if (mode_config->num_connector) {
    112 		list_for_each_entry(connector, &mode_config->connector_list, head)
    113 			radeon_connector_hotplug(connector);
    114 	}
    115 	mutex_unlock(&mode_config->mutex);
    116 }
    117 /**
    118  * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
    119  *
    120  * @dev: drm dev pointer
    121  *
    122  * Gets the hw ready to enable irqs (all asics).
    123  * This function disables all interrupt sources on the GPU.
    124  */
    125 void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
    126 {
    127 	struct radeon_device *rdev = dev->dev_private;
    128 	unsigned long irqflags;
    129 	unsigned i;
    130 
    131 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    132 	/* Disable *all* interrupts */
    133 	for (i = 0; i < RADEON_NUM_RINGS; i++)
    134 		atomic_set(&rdev->irq.ring_int[i], 0);
    135 	rdev->irq.dpm_thermal = false;
    136 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
    137 		rdev->irq.hpd[i] = false;
    138 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
    139 		rdev->irq.crtc_vblank_int[i] = false;
    140 		atomic_set(&rdev->irq.pflip[i], 0);
    141 		rdev->irq.afmt[i] = false;
    142 	}
    143 	radeon_irq_set(rdev);
    144 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    145 	/* Clear bits */
    146 	radeon_irq_process(rdev);
    147 }
    148 
    149 /**
    150  * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
    151  *
    152  * @dev: drm dev pointer
    153  *
    154  * Handles stuff to be done after enabling irqs (all asics).
    155  * Returns 0 on success.
    156  */
    157 int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
    158 {
    159 	struct radeon_device *rdev = dev->dev_private;
    160 
    161 	if (ASIC_IS_AVIVO(rdev))
    162 		dev->max_vblank_count = 0x00ffffff;
    163 	else
    164 		dev->max_vblank_count = 0x001fffff;
    165 
    166 	return 0;
    167 }
    168 
    169 /**
    170  * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
    171  *
    172  * @dev: drm dev pointer
    173  *
    174  * This function disables all interrupt sources on the GPU (all asics).
    175  */
    176 void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
    177 {
    178 	struct radeon_device *rdev = dev->dev_private;
    179 	unsigned long irqflags;
    180 	unsigned i;
    181 
    182 	if (rdev == NULL) {
    183 		return;
    184 	}
    185 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    186 	/* Disable *all* interrupts */
    187 	for (i = 0; i < RADEON_NUM_RINGS; i++)
    188 		atomic_set(&rdev->irq.ring_int[i], 0);
    189 	rdev->irq.dpm_thermal = false;
    190 	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
    191 		rdev->irq.hpd[i] = false;
    192 	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
    193 		rdev->irq.crtc_vblank_int[i] = false;
    194 		atomic_set(&rdev->irq.pflip[i], 0);
    195 		rdev->irq.afmt[i] = false;
    196 	}
    197 	radeon_irq_set(rdev);
    198 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    199 }
    200 
    201 /**
    202  * radeon_msi_ok - asic specific msi checks
    203  *
    204  * @rdev: radeon device pointer
    205  *
    206  * Handles asic specific MSI checks to determine if
    207  * MSIs should be enabled on a particular chip (all asics).
    208  * Returns true if MSIs should be enabled, false if MSIs
    209  * should not be enabled.
    210  */
    211 static bool radeon_msi_ok(struct radeon_device *rdev)
    212 {
    213 	/* RV370/RV380 was first asic with MSI support */
    214 	if (rdev->family < CHIP_RV380)
    215 		return false;
    216 
    217 	/* MSIs don't work on AGP */
    218 	if (rdev->flags & RADEON_IS_AGP)
    219 		return false;
    220 
    221 	/*
    222 	 * Older chips have a HW limitation, they can only generate 40 bits
    223 	 * of address for "64-bit" MSIs which breaks on some platforms, notably
    224 	 * IBM POWER servers, so we limit them
    225 	 */
    226 	if (rdev->family < CHIP_BONAIRE) {
    227 		dev_info(rdev->dev, "radeon: MSI limited to 32-bit\n");
    228 		rdev->pdev->no_64bit_msi = 1;
    229 	}
    230 
    231 	/* force MSI on */
    232 	if (radeon_msi == 1)
    233 		return true;
    234 	else if (radeon_msi == 0)
    235 		return false;
    236 
    237 	/* Quirks */
    238 	/* HP RS690 only seems to work with MSIs. */
    239 	if ((rdev->pdev->device == 0x791f) &&
    240 	    (rdev->pdev->subsystem_vendor == 0x103c) &&
    241 	    (rdev->pdev->subsystem_device == 0x30c2))
    242 		return true;
    243 
    244 	/* Dell RS690 only seems to work with MSIs. */
    245 	if ((rdev->pdev->device == 0x791f) &&
    246 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
    247 	    (rdev->pdev->subsystem_device == 0x01fc))
    248 		return true;
    249 
    250 	/* Dell RS690 only seems to work with MSIs. */
    251 	if ((rdev->pdev->device == 0x791f) &&
    252 	    (rdev->pdev->subsystem_vendor == 0x1028) &&
    253 	    (rdev->pdev->subsystem_device == 0x01fd))
    254 		return true;
    255 
    256 	/* Gateway RS690 only seems to work with MSIs. */
    257 	if ((rdev->pdev->device == 0x791f) &&
    258 	    (rdev->pdev->subsystem_vendor == 0x107b) &&
    259 	    (rdev->pdev->subsystem_device == 0x0185))
    260 		return true;
    261 
    262 	/* try and enable MSIs by default on all RS690s */
    263 	if (rdev->family == CHIP_RS690)
    264 		return true;
    265 
    266 	/* RV515 seems to have MSI issues where it loses
    267 	 * MSI rearms occasionally. This leads to lockups and freezes.
    268 	 * disable it by default.
    269 	 */
    270 	if (rdev->family == CHIP_RV515)
    271 		return false;
    272 	if (rdev->flags & RADEON_IS_IGP) {
    273 		/* APUs work fine with MSIs */
    274 		if (rdev->family >= CHIP_PALM)
    275 			return true;
    276 		/* lots of IGPs have problems with MSIs */
    277 		return false;
    278 	}
    279 
    280 	return true;
    281 }
    282 
    283 /**
    284  * radeon_irq_kms_init - init driver interrupt info
    285  *
    286  * @rdev: radeon device pointer
    287  *
    288  * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
    289  * Returns 0 for success, error for failure.
    290  */
    291 int radeon_irq_kms_init(struct radeon_device *rdev)
    292 {
    293 	int r = 0;
    294 
    295 	spin_lock_init(&rdev->irq.lock);
    296 	r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
    297 	if (r) {
    298 		return r;
    299 	}
    300 	/* enable msi */
    301 	rdev->msi_enabled = 0;
    302 
    303 	if (radeon_msi_ok(rdev)) {
    304 		int ret = pci_enable_msi(rdev->pdev);
    305 		if (!ret) {
    306 			rdev->msi_enabled = 1;
    307 			dev_info(rdev->dev, "radeon: using MSI.\n");
    308 		}
    309 	}
    310 
    311 	INIT_DELAYED_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
    312 	INIT_WORK(&rdev->dp_work, radeon_dp_work_func);
    313 	INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
    314 
    315 	rdev->irq.installed = true;
    316 #ifdef __NetBSD__
    317 	r = drm_irq_install(rdev->ddev);
    318 #else
    319 	r = drm_irq_install(rdev->ddev, rdev->ddev->pdev->irq);
    320 #endif
    321 	if (r) {
    322 		rdev->irq.installed = false;
    323 		flush_delayed_work(&rdev->hotplug_work);
    324 		return r;
    325 	}
    326 
    327 	DRM_INFO("radeon: irq initialized.\n");
    328 	return 0;
    329 }
    330 
    331 /**
    332  * radeon_irq_kms_fini - tear down driver interrupt info
    333  *
    334  * @rdev: radeon device pointer
    335  *
    336  * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
    337  */
    338 void radeon_irq_kms_fini(struct radeon_device *rdev)
    339 {
    340 	drm_vblank_cleanup(rdev->ddev);
    341 	if (rdev->irq.installed) {
    342 		drm_irq_uninstall(rdev->ddev);
    343 		rdev->irq.installed = false;
    344 		if (rdev->msi_enabled)
    345 			pci_disable_msi(rdev->pdev);
    346 		flush_delayed_work(&rdev->hotplug_work);
    347 	}
    348 }
    349 
    350 /**
    351  * radeon_irq_kms_sw_irq_get - enable software interrupt
    352  *
    353  * @rdev: radeon device pointer
    354  * @ring: ring whose interrupt you want to enable
    355  *
    356  * Enables the software interrupt for a specific ring (all asics).
    357  * The software interrupt is generally used to signal a fence on
    358  * a particular ring.
    359  */
    360 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
    361 {
    362 	unsigned long irqflags;
    363 
    364 	if (!rdev->ddev->irq_enabled)
    365 		return;
    366 
    367 	if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
    368 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    369 		radeon_irq_set(rdev);
    370 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    371 	}
    372 }
    373 
    374 /**
    375  * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
    376  *
    377  * @rdev: radeon device pointer
    378  * @ring: ring whose interrupt you want to enable
    379  *
    380  * Enables the software interrupt for a specific ring (all asics).
    381  * The software interrupt is generally used to signal a fence on
    382  * a particular ring.
    383  */
    384 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
    385 {
    386 	return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
    387 }
    388 
    389 /**
    390  * radeon_irq_kms_sw_irq_put - disable software interrupt
    391  *
    392  * @rdev: radeon device pointer
    393  * @ring: ring whose interrupt you want to disable
    394  *
    395  * Disables the software interrupt for a specific ring (all asics).
    396  * The software interrupt is generally used to signal a fence on
    397  * a particular ring.
    398  */
    399 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
    400 {
    401 	unsigned long irqflags;
    402 
    403 	if (!rdev->ddev->irq_enabled)
    404 		return;
    405 
    406 	if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
    407 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    408 		radeon_irq_set(rdev);
    409 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    410 	}
    411 }
    412 
    413 /**
    414  * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
    415  *
    416  * @rdev: radeon device pointer
    417  * @crtc: crtc whose interrupt you want to enable
    418  *
    419  * Enables the pageflip interrupt for a specific crtc (all asics).
    420  * For pageflips we use the vblank interrupt source.
    421  */
    422 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
    423 {
    424 	unsigned long irqflags;
    425 
    426 	if (crtc < 0 || crtc >= rdev->num_crtc)
    427 		return;
    428 
    429 	if (!rdev->ddev->irq_enabled)
    430 		return;
    431 
    432 	if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
    433 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    434 		radeon_irq_set(rdev);
    435 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    436 	}
    437 }
    438 
    439 /**
    440  * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
    441  *
    442  * @rdev: radeon device pointer
    443  * @crtc: crtc whose interrupt you want to disable
    444  *
    445  * Disables the pageflip interrupt for a specific crtc (all asics).
    446  * For pageflips we use the vblank interrupt source.
    447  */
    448 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
    449 {
    450 	unsigned long irqflags;
    451 
    452 	if (crtc < 0 || crtc >= rdev->num_crtc)
    453 		return;
    454 
    455 	if (!rdev->ddev->irq_enabled)
    456 		return;
    457 
    458 	if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
    459 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    460 		radeon_irq_set(rdev);
    461 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    462 	}
    463 }
    464 
    465 /**
    466  * radeon_irq_kms_enable_afmt - enable audio format change interrupt
    467  *
    468  * @rdev: radeon device pointer
    469  * @block: afmt block whose interrupt you want to enable
    470  *
    471  * Enables the afmt change interrupt for a specific afmt block (all asics).
    472  */
    473 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
    474 {
    475 	unsigned long irqflags;
    476 
    477 	if (!rdev->ddev->irq_enabled)
    478 		return;
    479 
    480 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    481 	rdev->irq.afmt[block] = true;
    482 	radeon_irq_set(rdev);
    483 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    484 
    485 }
    486 
    487 /**
    488  * radeon_irq_kms_disable_afmt - disable audio format change interrupt
    489  *
    490  * @rdev: radeon device pointer
    491  * @block: afmt block whose interrupt you want to disable
    492  *
    493  * Disables the afmt change interrupt for a specific afmt block (all asics).
    494  */
    495 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
    496 {
    497 	unsigned long irqflags;
    498 
    499 	if (!rdev->ddev->irq_enabled)
    500 		return;
    501 
    502 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    503 	rdev->irq.afmt[block] = false;
    504 	radeon_irq_set(rdev);
    505 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    506 }
    507 
    508 /**
    509  * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
    510  *
    511  * @rdev: radeon device pointer
    512  * @hpd_mask: mask of hpd pins you want to enable.
    513  *
    514  * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
    515  */
    516 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
    517 {
    518 	unsigned long irqflags;
    519 	int i;
    520 
    521 	if (!rdev->ddev->irq_enabled)
    522 		return;
    523 
    524 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    525 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
    526 		rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
    527 	radeon_irq_set(rdev);
    528 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    529 }
    530 
    531 /**
    532  * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
    533  *
    534  * @rdev: radeon device pointer
    535  * @hpd_mask: mask of hpd pins you want to disable.
    536  *
    537  * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
    538  */
    539 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
    540 {
    541 	unsigned long irqflags;
    542 	int i;
    543 
    544 	if (!rdev->ddev->irq_enabled)
    545 		return;
    546 
    547 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    548 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
    549 		rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
    550 	radeon_irq_set(rdev);
    551 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    552 }
    553 
    554