1 /* $OpenBSD: radeon_irq_kms.c,v 1.9 2015/04/18 14:47:35 jsg Exp $ */ 2 /* 3 * Copyright 2008 Advanced Micro Devices, Inc. 4 * Copyright 2008 Red Hat Inc. 5 * Copyright 2009 Jerome Glisse. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 * OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: Dave Airlie 26 * Alex Deucher 27 * Jerome Glisse 28 */ 29 #include <dev/pci/drm/drmP.h> 30 #include <dev/pci/drm/drm_crtc_helper.h> 31 #include <dev/pci/drm/radeon_drm.h> 32 #include "radeon_reg.h" 33 #include "radeon.h" 34 #include "atom.h" 35 36 #define RADEON_WAIT_IDLE_TIMEOUT 200 37 38 /** 39 * radeon_driver_irq_handler_kms - irq handler for KMS 40 * 41 * @DRM_IRQ_ARGS: args 42 * 43 * This is the irq handler for the radeon KMS driver (all asics). 44 * radeon_irq_process is a macro that points to the per-asic 45 * irq handler callback. 46 */ 47 irqreturn_t radeon_driver_irq_handler_kms(void *arg) 48 { 49 struct drm_device *dev = arg; 50 struct radeon_device *rdev = dev->dev_private; 51 52 if (!rdev->irq.installed) 53 return (0); 54 55 return radeon_irq_process(rdev); 56 } 57 58 /* 59 * Handle hotplug events outside the interrupt handler proper. 60 */ 61 /** 62 * radeon_hotplug_work_func - display hotplug work handler 63 * 64 * @work: work struct 65 * 66 * This is the hot plug event work handler (all asics). 67 * The work gets scheduled from the irq handler if there 68 * was a hot plug interrupt. It walks the connector table 69 * and calls the hotplug handler for each one, then sends 70 * a drm hotplug event to alert userspace. 71 */ 72 static void radeon_hotplug_work_func(void *arg1) 73 { 74 struct radeon_device *rdev = arg1; 75 struct drm_device *dev = rdev->ddev; 76 struct drm_mode_config *mode_config = &dev->mode_config; 77 struct drm_connector *connector; 78 79 if (mode_config->num_connector) { 80 list_for_each_entry(connector, &mode_config->connector_list, head) 81 radeon_connector_hotplug(connector); 82 } 83 /* Just fire off a uevent and let userspace tell us what to do */ 84 drm_helper_hpd_irq_event(dev); 85 } 86 87 /** 88 * radeon_driver_irq_preinstall_kms - drm irq preinstall callback 89 * 90 * @dev: drm dev pointer 91 * 92 * Gets the hw ready to enable irqs (all asics). 93 * This function disables all interrupt sources on the GPU. 94 */ 95 void radeon_driver_irq_preinstall_kms(struct drm_device *dev) 96 { 97 struct radeon_device *rdev = dev->dev_private; 98 unsigned long irqflags; 99 unsigned i; 100 101 spin_lock_irqsave(&rdev->irq.lock, irqflags); 102 /* Disable *all* interrupts */ 103 for (i = 0; i < RADEON_NUM_RINGS; i++) 104 atomic_set(&rdev->irq.ring_int[i], 0); 105 for (i = 0; i < RADEON_MAX_HPD_PINS; i++) 106 rdev->irq.hpd[i] = false; 107 for (i = 0; i < RADEON_MAX_CRTCS; i++) { 108 rdev->irq.crtc_vblank_int[i] = false; 109 atomic_set(&rdev->irq.pflip[i], 0); 110 rdev->irq.afmt[i] = false; 111 } 112 radeon_irq_set(rdev); 113 spin_unlock_irqrestore(&rdev->irq.lock, irqflags); 114 /* Clear bits */ 115 radeon_irq_process(rdev); 116 } 117 118 /** 119 * radeon_driver_irq_postinstall_kms - drm irq preinstall callback 120 * 121 * @dev: drm dev pointer 122 * 123 * Handles stuff to be done after enabling irqs (all asics). 124 * Returns 0 on success. 125 */ 126 int radeon_driver_irq_postinstall_kms(struct drm_device *dev) 127 { 128 dev->max_vblank_count = 0x001fffff; 129 return 0; 130 } 131 132 /** 133 * radeon_driver_irq_uninstall_kms - drm irq uninstall callback 134 * 135 * @dev: drm dev pointer 136 * 137 * This function disables all interrupt sources on the GPU (all asics). 138 */ 139 void radeon_driver_irq_uninstall_kms(struct drm_device *dev) 140 { 141 struct radeon_device *rdev = dev->dev_private; 142 unsigned long irqflags; 143 unsigned i; 144 145 if (rdev == NULL) { 146 return; 147 } 148 spin_lock_irqsave(&rdev->irq.lock, irqflags); 149 /* Disable *all* interrupts */ 150 for (i = 0; i < RADEON_NUM_RINGS; i++) 151 atomic_set(&rdev->irq.ring_int[i], 0); 152 for (i = 0; i < RADEON_MAX_HPD_PINS; i++) 153 rdev->irq.hpd[i] = false; 154 for (i = 0; i < RADEON_MAX_CRTCS; i++) { 155 rdev->irq.crtc_vblank_int[i] = false; 156 atomic_set(&rdev->irq.pflip[i], 0); 157 rdev->irq.afmt[i] = false; 158 } 159 radeon_irq_set(rdev); 160 spin_unlock_irqrestore(&rdev->irq.lock, irqflags); 161 } 162 163 /** 164 * radeon_msi_ok - asic specific msi checks 165 * 166 * @rdev: radeon device pointer 167 * 168 * Handles asic specific MSI checks to determine if 169 * MSIs should be enabled on a particular chip (all asics). 170 * Returns true if MSIs should be enabled, false if MSIs 171 * should not be enabled. 172 */ 173 bool radeon_msi_ok(struct radeon_device *rdev) 174 { 175 /* RV370/RV380 was first asic with MSI support */ 176 if (rdev->family < CHIP_RV380) 177 return false; 178 179 /* MSIs don't work on AGP */ 180 if (rdev->flags & RADEON_IS_AGP) 181 return false; 182 183 /* force MSI on */ 184 if (radeon_msi == 1) 185 return true; 186 else if (radeon_msi == 0) 187 return false; 188 189 /* Quirks */ 190 /* HP RS690 only seems to work with MSIs. */ 191 if ((rdev->pdev->device == 0x791f) && 192 (rdev->pdev->subsystem_vendor == 0x103c) && 193 (rdev->pdev->subsystem_device == 0x30c2)) 194 return true; 195 196 /* Dell RS690 only seems to work with MSIs. */ 197 if ((rdev->pdev->device == 0x791f) && 198 (rdev->pdev->subsystem_vendor == 0x1028) && 199 (rdev->pdev->subsystem_device == 0x01fc)) 200 return true; 201 202 /* Dell RS690 only seems to work with MSIs. */ 203 if ((rdev->pdev->device == 0x791f) && 204 (rdev->pdev->subsystem_vendor == 0x1028) && 205 (rdev->pdev->subsystem_device == 0x01fd)) 206 return true; 207 208 /* Gateway RS690 only seems to work with MSIs. */ 209 if ((rdev->pdev->device == 0x791f) && 210 (rdev->pdev->subsystem_vendor == 0x107b) && 211 (rdev->pdev->subsystem_device == 0x0185)) 212 return true; 213 214 /* try and enable MSIs by default on all RS690s */ 215 if (rdev->family == CHIP_RS690) 216 return true; 217 218 /* RV515 seems to have MSI issues where it loses 219 * MSI rearms occasionally. This leads to lockups and freezes. 220 * disable it by default. 221 */ 222 if (rdev->family == CHIP_RV515) 223 return false; 224 if (rdev->flags & RADEON_IS_IGP) { 225 /* APUs work fine with MSIs */ 226 if (rdev->family >= CHIP_PALM) 227 return true; 228 /* lots of IGPs have problems with MSIs */ 229 return false; 230 } 231 232 return true; 233 } 234 235 /** 236 * radeon_irq_kms_init - init driver interrupt info 237 * 238 * @rdev: radeon device pointer 239 * 240 * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics). 241 * Returns 0 for success, error for failure. 242 */ 243 int radeon_irq_kms_init(struct radeon_device *rdev) 244 { 245 int r = 0; 246 247 task_set(&rdev->hotplug_task, radeon_hotplug_work_func, rdev); 248 task_set(&rdev->audio_task, r600_audio_update_hdmi, rdev); 249 250 mtx_init(&rdev->irq.lock, IPL_TTY); 251 r = drm_vblank_init(rdev->ddev, rdev->num_crtc); 252 if (r) { 253 return r; 254 } 255 #ifdef notyet 256 /* enable msi */ 257 rdev->msi_enabled = 0; 258 259 if (radeon_msi_ok(rdev)) { 260 int ret = pci_enable_msi(rdev->pdev); 261 if (!ret) { 262 rdev->msi_enabled = 1; 263 dev_info(rdev->dev, "radeon: using MSI.\n"); 264 } 265 } 266 #endif 267 rdev->irq.installed = true; 268 r = drm_irq_install(rdev->ddev); 269 if (r) { 270 rdev->irq.installed = false; 271 return r; 272 } 273 DRM_INFO("radeon: irq initialized.\n"); 274 return 0; 275 } 276 277 /** 278 * radeon_irq_kms_fini - tear down driver interrrupt info 279 * 280 * @rdev: radeon device pointer 281 * 282 * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics). 283 */ 284 void radeon_irq_kms_fini(struct radeon_device *rdev) 285 { 286 drm_vblank_cleanup(rdev->ddev); 287 if (rdev->irq.installed) { 288 drm_irq_uninstall(rdev->ddev); 289 rdev->irq.installed = false; 290 #ifdef notyet 291 if (rdev->msi_enabled) 292 pci_disable_msi(rdev->pdev); 293 #endif 294 } 295 #ifdef notyet 296 flush_work(&rdev->hotplug_work); 297 #endif 298 } 299 300 /** 301 * radeon_irq_kms_sw_irq_get - enable software interrupt 302 * 303 * @rdev: radeon device pointer 304 * @ring: ring whose interrupt you want to enable 305 * 306 * Enables the software interrupt for a specific ring (all asics). 307 * The software interrupt is generally used to signal a fence on 308 * a particular ring. 309 */ 310 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring) 311 { 312 unsigned long irqflags; 313 314 if (!rdev->ddev->irq_enabled) 315 return; 316 317 if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) { 318 spin_lock_irqsave(&rdev->irq.lock, irqflags); 319 radeon_irq_set(rdev); 320 spin_unlock_irqrestore(&rdev->irq.lock, irqflags); 321 } 322 } 323 324 /** 325 * radeon_irq_kms_sw_irq_put - disable software interrupt 326 * 327 * @rdev: radeon device pointer 328 * @ring: ring whose interrupt you want to disable 329 * 330 * Disables the software interrupt for a specific ring (all asics). 331 * The software interrupt is generally used to signal a fence on 332 * a particular ring. 333 */ 334 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring) 335 { 336 unsigned long irqflags; 337 338 if (!rdev->ddev->irq_enabled) 339 return; 340 341 if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) { 342 spin_lock_irqsave(&rdev->irq.lock, irqflags); 343 radeon_irq_set(rdev); 344 spin_unlock_irqrestore(&rdev->irq.lock, irqflags); 345 } 346 } 347 348 /** 349 * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt 350 * 351 * @rdev: radeon device pointer 352 * @crtc: crtc whose interrupt you want to enable 353 * 354 * Enables the pageflip interrupt for a specific crtc (all asics). 355 * For pageflips we use the vblank interrupt source. 356 */ 357 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc) 358 { 359 unsigned long irqflags; 360 361 if (crtc < 0 || crtc >= rdev->num_crtc) 362 return; 363 364 if (!rdev->ddev->irq_enabled) 365 return; 366 367 if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 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_pflip_irq_put - disable pageflip interrupt 376 * 377 * @rdev: radeon device pointer 378 * @crtc: crtc whose interrupt you want to disable 379 * 380 * Disables the pageflip interrupt for a specific crtc (all asics). 381 * For pageflips we use the vblank interrupt source. 382 */ 383 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc) 384 { 385 unsigned long irqflags; 386 387 if (crtc < 0 || crtc >= rdev->num_crtc) 388 return; 389 390 if (!rdev->ddev->irq_enabled) 391 return; 392 393 if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) { 394 spin_lock_irqsave(&rdev->irq.lock, irqflags); 395 radeon_irq_set(rdev); 396 spin_unlock_irqrestore(&rdev->irq.lock, irqflags); 397 } 398 } 399 400 /** 401 * radeon_irq_kms_enable_afmt - enable audio format change interrupt 402 * 403 * @rdev: radeon device pointer 404 * @block: afmt block whose interrupt you want to enable 405 * 406 * Enables the afmt change interrupt for a specific afmt block (all asics). 407 */ 408 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block) 409 { 410 unsigned long irqflags; 411 412 if (!rdev->ddev->irq_enabled) 413 return; 414 415 spin_lock_irqsave(&rdev->irq.lock, irqflags); 416 rdev->irq.afmt[block] = true; 417 radeon_irq_set(rdev); 418 spin_unlock_irqrestore(&rdev->irq.lock, irqflags); 419 420 } 421 422 /** 423 * radeon_irq_kms_disable_afmt - disable audio format change interrupt 424 * 425 * @rdev: radeon device pointer 426 * @block: afmt block whose interrupt you want to disable 427 * 428 * Disables the afmt change interrupt for a specific afmt block (all asics). 429 */ 430 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block) 431 { 432 unsigned long irqflags; 433 434 if (!rdev->ddev->irq_enabled) 435 return; 436 437 spin_lock_irqsave(&rdev->irq.lock, irqflags); 438 rdev->irq.afmt[block] = false; 439 radeon_irq_set(rdev); 440 spin_unlock_irqrestore(&rdev->irq.lock, irqflags); 441 } 442 443 /** 444 * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt 445 * 446 * @rdev: radeon device pointer 447 * @hpd_mask: mask of hpd pins you want to enable. 448 * 449 * Enables the hotplug detect interrupt for a specific hpd pin (all asics). 450 */ 451 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask) 452 { 453 unsigned long irqflags; 454 int i; 455 456 if (!rdev->ddev->irq_enabled) 457 return; 458 459 spin_lock_irqsave(&rdev->irq.lock, irqflags); 460 for (i = 0; i < RADEON_MAX_HPD_PINS; ++i) 461 rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i)); 462 radeon_irq_set(rdev); 463 spin_unlock_irqrestore(&rdev->irq.lock, irqflags); 464 } 465 466 /** 467 * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt 468 * 469 * @rdev: radeon device pointer 470 * @hpd_mask: mask of hpd pins you want to disable. 471 * 472 * Disables the hotplug detect interrupt for a specific hpd pin (all asics). 473 */ 474 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask) 475 { 476 unsigned long irqflags; 477 int i; 478 479 if (!rdev->ddev->irq_enabled) 480 return; 481 482 spin_lock_irqsave(&rdev->irq.lock, irqflags); 483 for (i = 0; i < RADEON_MAX_HPD_PINS; ++i) 484 rdev->irq.hpd[i] &= !(hpd_mask & (1 << i)); 485 radeon_irq_set(rdev); 486 spin_unlock_irqrestore(&rdev->irq.lock, irqflags); 487 } 488 489