Home | History | Annotate | Line # | Download | only in radeon
radeon_irq_kms.c revision 1.3
      1 /*	$NetBSD: radeon_irq_kms.c,v 1.3 2018/08/27 04:58:36 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 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: radeon_irq_kms.c,v 1.3 2018/08/27 04:58:36 riastradh 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 	r = drm_irq_install(rdev->ddev, rdev->ddev->pdev->irq);
    316 	if (r) {
    317 		rdev->irq.installed = false;
    318 		flush_delayed_work(&rdev->hotplug_work);
    319 		return r;
    320 	}
    321 
    322 	DRM_INFO("radeon: irq initialized.\n");
    323 	return 0;
    324 }
    325 
    326 /**
    327  * radeon_irq_kms_fini - tear down driver interrupt info
    328  *
    329  * @rdev: radeon device pointer
    330  *
    331  * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
    332  */
    333 void radeon_irq_kms_fini(struct radeon_device *rdev)
    334 {
    335 	drm_vblank_cleanup(rdev->ddev);
    336 	if (rdev->irq.installed) {
    337 		drm_irq_uninstall(rdev->ddev);
    338 		rdev->irq.installed = false;
    339 		if (rdev->msi_enabled)
    340 			pci_disable_msi(rdev->pdev);
    341 		flush_delayed_work(&rdev->hotplug_work);
    342 	}
    343 }
    344 
    345 /**
    346  * radeon_irq_kms_sw_irq_get - enable software interrupt
    347  *
    348  * @rdev: radeon device pointer
    349  * @ring: ring whose interrupt you want to enable
    350  *
    351  * Enables the software interrupt for a specific ring (all asics).
    352  * The software interrupt is generally used to signal a fence on
    353  * a particular ring.
    354  */
    355 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
    356 {
    357 	unsigned long irqflags;
    358 
    359 	if (!rdev->ddev->irq_enabled)
    360 		return;
    361 
    362 	if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
    363 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    364 		radeon_irq_set(rdev);
    365 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    366 	}
    367 }
    368 
    369 /**
    370  * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
    371  *
    372  * @rdev: radeon device pointer
    373  * @ring: ring whose interrupt you want to enable
    374  *
    375  * Enables the software interrupt for a specific ring (all asics).
    376  * The software interrupt is generally used to signal a fence on
    377  * a particular ring.
    378  */
    379 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
    380 {
    381 	return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
    382 }
    383 
    384 /**
    385  * radeon_irq_kms_sw_irq_put - disable software interrupt
    386  *
    387  * @rdev: radeon device pointer
    388  * @ring: ring whose interrupt you want to disable
    389  *
    390  * Disables the software interrupt for a specific ring (all asics).
    391  * The software interrupt is generally used to signal a fence on
    392  * a particular ring.
    393  */
    394 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
    395 {
    396 	unsigned long irqflags;
    397 
    398 	if (!rdev->ddev->irq_enabled)
    399 		return;
    400 
    401 	if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
    402 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    403 		radeon_irq_set(rdev);
    404 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    405 	}
    406 }
    407 
    408 /**
    409  * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
    410  *
    411  * @rdev: radeon device pointer
    412  * @crtc: crtc whose interrupt you want to enable
    413  *
    414  * Enables the pageflip interrupt for a specific crtc (all asics).
    415  * For pageflips we use the vblank interrupt source.
    416  */
    417 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
    418 {
    419 	unsigned long irqflags;
    420 
    421 	if (crtc < 0 || crtc >= rdev->num_crtc)
    422 		return;
    423 
    424 	if (!rdev->ddev->irq_enabled)
    425 		return;
    426 
    427 	if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
    428 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    429 		radeon_irq_set(rdev);
    430 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    431 	}
    432 }
    433 
    434 /**
    435  * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
    436  *
    437  * @rdev: radeon device pointer
    438  * @crtc: crtc whose interrupt you want to disable
    439  *
    440  * Disables the pageflip interrupt for a specific crtc (all asics).
    441  * For pageflips we use the vblank interrupt source.
    442  */
    443 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
    444 {
    445 	unsigned long irqflags;
    446 
    447 	if (crtc < 0 || crtc >= rdev->num_crtc)
    448 		return;
    449 
    450 	if (!rdev->ddev->irq_enabled)
    451 		return;
    452 
    453 	if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
    454 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    455 		radeon_irq_set(rdev);
    456 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    457 	}
    458 }
    459 
    460 /**
    461  * radeon_irq_kms_enable_afmt - enable audio format change interrupt
    462  *
    463  * @rdev: radeon device pointer
    464  * @block: afmt block whose interrupt you want to enable
    465  *
    466  * Enables the afmt change interrupt for a specific afmt block (all asics).
    467  */
    468 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
    469 {
    470 	unsigned long irqflags;
    471 
    472 	if (!rdev->ddev->irq_enabled)
    473 		return;
    474 
    475 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    476 	rdev->irq.afmt[block] = true;
    477 	radeon_irq_set(rdev);
    478 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    479 
    480 }
    481 
    482 /**
    483  * radeon_irq_kms_disable_afmt - disable audio format change interrupt
    484  *
    485  * @rdev: radeon device pointer
    486  * @block: afmt block whose interrupt you want to disable
    487  *
    488  * Disables the afmt change interrupt for a specific afmt block (all asics).
    489  */
    490 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
    491 {
    492 	unsigned long irqflags;
    493 
    494 	if (!rdev->ddev->irq_enabled)
    495 		return;
    496 
    497 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    498 	rdev->irq.afmt[block] = false;
    499 	radeon_irq_set(rdev);
    500 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    501 }
    502 
    503 /**
    504  * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
    505  *
    506  * @rdev: radeon device pointer
    507  * @hpd_mask: mask of hpd pins you want to enable.
    508  *
    509  * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
    510  */
    511 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
    512 {
    513 	unsigned long irqflags;
    514 	int i;
    515 
    516 	if (!rdev->ddev->irq_enabled)
    517 		return;
    518 
    519 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    520 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
    521 		rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
    522 	radeon_irq_set(rdev);
    523 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    524 }
    525 
    526 /**
    527  * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
    528  *
    529  * @rdev: radeon device pointer
    530  * @hpd_mask: mask of hpd pins you want to disable.
    531  *
    532  * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
    533  */
    534 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
    535 {
    536 	unsigned long irqflags;
    537 	int i;
    538 
    539 	if (!rdev->ddev->irq_enabled)
    540 		return;
    541 
    542 	spin_lock_irqsave(&rdev->irq.lock, irqflags);
    543 	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
    544 		rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
    545 	radeon_irq_set(rdev);
    546 	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    547 }
    548 
    549