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