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