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 * @int irq, 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(struct work_struct *work) 82 { 83 struct radeon_device *rdev = container_of(work, struct radeon_device, 84 hotplug_work); 85 struct drm_device *dev = rdev->ddev; 86 struct drm_mode_config *mode_config = &dev->mode_config; 87 struct drm_connector *connector; 88 89 if (mode_config->num_connector) { 90 list_for_each_entry(connector, &mode_config->connector_list, head) 91 radeon_connector_hotplug(connector); 92 } 93 /* Just fire off a uevent and let userspace tell us what to do */ 94 drm_helper_hpd_irq_event(dev); 95 } 96 97 /** 98 * radeon_driver_irq_preinstall_kms - drm irq preinstall callback 99 * 100 * @dev: drm dev pointer 101 * 102 * Gets the hw ready to enable irqs (all asics). 103 * This function disables all interrupt sources on the GPU. 104 */ 105 void radeon_driver_irq_preinstall_kms(struct drm_device *dev) 106 { 107 struct radeon_device *rdev = dev->dev_private; 108 unsigned i; 109 110 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE); 111 /* Disable *all* interrupts */ 112 for (i = 0; i < RADEON_NUM_RINGS; i++) 113 atomic_set(&rdev->irq.ring_int[i], 0); 114 rdev->irq.dpm_thermal = false; 115 for (i = 0; i < RADEON_MAX_HPD_PINS; i++) 116 rdev->irq.hpd[i] = false; 117 for (i = 0; i < RADEON_MAX_CRTCS; i++) { 118 rdev->irq.crtc_vblank_int[i] = false; 119 atomic_set(&rdev->irq.pflip[i], 0); 120 rdev->irq.afmt[i] = false; 121 } 122 radeon_irq_set(rdev); 123 lockmgr(&rdev->irq.lock, LK_RELEASE); 124 /* Clear bits */ 125 radeon_irq_process(rdev); 126 } 127 128 /** 129 * radeon_driver_irq_postinstall_kms - drm irq preinstall callback 130 * 131 * @dev: drm dev pointer 132 * 133 * Handles stuff to be done after enabling irqs (all asics). 134 * Returns 0 on success. 135 */ 136 int radeon_driver_irq_postinstall_kms(struct drm_device *dev) 137 { 138 dev->max_vblank_count = 0x001fffff; 139 return 0; 140 } 141 142 /** 143 * radeon_driver_irq_uninstall_kms - drm irq uninstall callback 144 * 145 * @dev: drm dev pointer 146 * 147 * This function disables all interrupt sources on the GPU (all asics). 148 */ 149 void radeon_driver_irq_uninstall_kms(struct drm_device *dev) 150 { 151 struct radeon_device *rdev = dev->dev_private; 152 unsigned i; 153 154 if (rdev == NULL) { 155 return; 156 } 157 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE); 158 /* Disable *all* interrupts */ 159 for (i = 0; i < RADEON_NUM_RINGS; i++) 160 atomic_set(&rdev->irq.ring_int[i], 0); 161 rdev->irq.dpm_thermal = false; 162 for (i = 0; i < RADEON_MAX_HPD_PINS; i++) 163 rdev->irq.hpd[i] = false; 164 for (i = 0; i < RADEON_MAX_CRTCS; i++) { 165 rdev->irq.crtc_vblank_int[i] = false; 166 atomic_set(&rdev->irq.pflip[i], 0); 167 rdev->irq.afmt[i] = false; 168 } 169 radeon_irq_set(rdev); 170 lockmgr(&rdev->irq.lock, LK_RELEASE); 171 } 172 173 /** 174 * radeon_msi_ok - asic specific msi checks 175 * 176 * @rdev: radeon device pointer 177 * 178 * Handles asic specific MSI checks to determine if 179 * MSIs should be enabled on a particular chip (all asics). 180 * Returns true if MSIs should be enabled, false if MSIs 181 * should not be enabled. 182 */ 183 int radeon_msi_ok(struct drm_device *rdev, unsigned long flags) 184 { 185 int family; 186 187 family = flags & RADEON_FAMILY_MASK; 188 189 /* RV370/RV380 was first asic with MSI support */ 190 if (family < CHIP_RV380) 191 return false; 192 193 /* MSIs don't work on AGP */ 194 if (drm_device_is_agp(rdev)) 195 return false; 196 197 /* force MSI on */ 198 if (radeon_msi == 1) 199 return true; 200 else if (radeon_msi == 0) 201 return false; 202 203 /* Quirks */ 204 /* HP RS690 only seems to work with MSIs. */ 205 if ((rdev->pdev->device == 0x791f) && 206 (rdev->pdev->subsystem_vendor == 0x103c) && 207 (rdev->pdev->subsystem_device == 0x30c2)) 208 return true; 209 210 /* Dell RS690 only seems to work with MSIs. */ 211 if ((rdev->pdev->device == 0x791f) && 212 (rdev->pdev->subsystem_vendor == 0x1028) && 213 (rdev->pdev->subsystem_device == 0x01fc)) 214 return true; 215 216 /* Dell RS690 only seems to work with MSIs. */ 217 if ((rdev->pdev->device == 0x791f) && 218 (rdev->pdev->subsystem_vendor == 0x1028) && 219 (rdev->pdev->subsystem_device == 0x01fd)) 220 return true; 221 222 /* Gateway RS690 only seems to work with MSIs. */ 223 if ((rdev->pdev->device == 0x791f) && 224 (rdev->pdev->subsystem_vendor == 0x107b) && 225 (rdev->pdev->subsystem_device == 0x0185)) 226 return true; 227 228 /* try and enable MSIs by default on all RS690s */ 229 if (family == CHIP_RS690) 230 return true; 231 232 /* RV515 seems to have MSI issues where it loses 233 * MSI rearms occasionally. This leads to lockups and freezes. 234 * disable it by default. 235 */ 236 if (family == CHIP_RV515) 237 return false; 238 if (flags & RADEON_IS_IGP) { 239 /* APUs work fine with MSIs */ 240 if (family >= CHIP_PALM) 241 return true; 242 /* lots of IGPs have problems with MSIs */ 243 return false; 244 } 245 246 return true; 247 } 248 249 /** 250 * radeon_irq_kms_init - init driver interrupt info 251 * 252 * @rdev: radeon device pointer 253 * 254 * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics). 255 * Returns 0 for success, error for failure. 256 */ 257 int radeon_irq_kms_init(struct radeon_device *rdev) 258 { 259 int r = 0; 260 261 lockinit(&rdev->irq.lock, "drm__radeon_device__irq__lock", 0, LK_CANRECURSE); 262 r = drm_vblank_init(rdev->ddev, rdev->num_crtc); 263 if (r) { 264 return r; 265 } 266 /* enable msi */ 267 rdev->msi_enabled = (rdev->ddev->irq_type == PCI_INTR_TYPE_MSI); 268 269 INIT_WORK(&rdev->hotplug_work, radeon_hotplug_work_func); 270 TASK_INIT(&rdev->audio_work, 0, r600_audio_update_hdmi, rdev); 271 272 rdev->irq.installed = true; 273 r = drm_irq_install(rdev->ddev, rdev->ddev->irq); 274 if (r) { 275 rdev->irq.installed = false; 276 flush_work(&rdev->hotplug_work); 277 return r; 278 } 279 280 DRM_INFO("radeon: irq initialized.\n"); 281 return 0; 282 } 283 284 /** 285 * radeon_irq_kms_fini - tear down driver interrupt info 286 * 287 * @rdev: radeon device pointer 288 * 289 * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics). 290 */ 291 void radeon_irq_kms_fini(struct radeon_device *rdev) 292 { 293 drm_vblank_cleanup(rdev->ddev); 294 if (rdev->irq.installed) { 295 drm_irq_uninstall(rdev->ddev); 296 rdev->irq.installed = false; 297 flush_work(&rdev->hotplug_work); 298 } 299 } 300 301 /** 302 * radeon_irq_kms_sw_irq_get - enable software interrupt 303 * 304 * @rdev: radeon device pointer 305 * @ring: ring whose interrupt you want to enable 306 * 307 * Enables the software interrupt for a specific ring (all asics). 308 * The software interrupt is generally used to signal a fence on 309 * a particular ring. 310 */ 311 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring) 312 { 313 if (!rdev->ddev->irq_enabled) 314 return; 315 316 if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) { 317 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE); 318 radeon_irq_set(rdev); 319 lockmgr(&rdev->irq.lock, LK_RELEASE); 320 } 321 } 322 323 /** 324 * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt 325 * 326 * @rdev: radeon device pointer 327 * @ring: ring whose interrupt you want to enable 328 * 329 * Enables the software interrupt for a specific ring (all asics). 330 * The software interrupt is generally used to signal a fence on 331 * a particular ring. 332 */ 333 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring) 334 { 335 return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1; 336 } 337 338 /** 339 * radeon_irq_kms_sw_irq_put - disable software interrupt 340 * 341 * @rdev: radeon device pointer 342 * @ring: ring whose interrupt you want to disable 343 * 344 * Disables the software interrupt for a specific ring (all asics). 345 * The software interrupt is generally used to signal a fence on 346 * a particular ring. 347 */ 348 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring) 349 { 350 if (!rdev->ddev->irq_enabled) 351 return; 352 353 if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) { 354 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE); 355 radeon_irq_set(rdev); 356 lockmgr(&rdev->irq.lock, LK_RELEASE); 357 } 358 } 359 360 /** 361 * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt 362 * 363 * @rdev: radeon device pointer 364 * @crtc: crtc whose interrupt you want to enable 365 * 366 * Enables the pageflip interrupt for a specific crtc (all asics). 367 * For pageflips we use the vblank interrupt source. 368 */ 369 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc) 370 { 371 if (crtc < 0 || crtc >= rdev->num_crtc) 372 return; 373 374 if (!rdev->ddev->irq_enabled) 375 return; 376 377 if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) { 378 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE); 379 radeon_irq_set(rdev); 380 lockmgr(&rdev->irq.lock, LK_RELEASE); 381 } 382 } 383 384 /** 385 * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt 386 * 387 * @rdev: radeon device pointer 388 * @crtc: crtc whose interrupt you want to disable 389 * 390 * Disables the pageflip interrupt for a specific crtc (all asics). 391 * For pageflips we use the vblank interrupt source. 392 */ 393 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc) 394 { 395 if (crtc < 0 || crtc >= rdev->num_crtc) 396 return; 397 398 if (!rdev->ddev->irq_enabled) 399 return; 400 401 if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) { 402 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE); 403 radeon_irq_set(rdev); 404 lockmgr(&rdev->irq.lock, LK_RELEASE); 405 } 406 } 407 408 /** 409 * radeon_irq_kms_enable_afmt - enable audio format change interrupt 410 * 411 * @rdev: radeon device pointer 412 * @block: afmt block whose interrupt you want to enable 413 * 414 * Enables the afmt change interrupt for a specific afmt block (all asics). 415 */ 416 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block) 417 { 418 if (!rdev->ddev->irq_enabled) 419 return; 420 421 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE); 422 rdev->irq.afmt[block] = true; 423 radeon_irq_set(rdev); 424 lockmgr(&rdev->irq.lock, LK_RELEASE); 425 } 426 427 /** 428 * radeon_irq_kms_disable_afmt - disable audio format change interrupt 429 * 430 * @rdev: radeon device pointer 431 * @block: afmt block whose interrupt you want to disable 432 * 433 * Disables the afmt change interrupt for a specific afmt block (all asics). 434 */ 435 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block) 436 { 437 if (!rdev->ddev->irq_enabled) 438 return; 439 440 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE); 441 rdev->irq.afmt[block] = false; 442 radeon_irq_set(rdev); 443 lockmgr(&rdev->irq.lock, LK_RELEASE); 444 } 445 446 /** 447 * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt 448 * 449 * @rdev: radeon device pointer 450 * @hpd_mask: mask of hpd pins you want to enable. 451 * 452 * Enables the hotplug detect interrupt for a specific hpd pin (all asics). 453 */ 454 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask) 455 { 456 int i; 457 458 if (!rdev->ddev->irq_enabled) 459 return; 460 461 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE); 462 for (i = 0; i < RADEON_MAX_HPD_PINS; ++i) 463 rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i)); 464 radeon_irq_set(rdev); 465 lockmgr(&rdev->irq.lock, LK_RELEASE); 466 } 467 468 /** 469 * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt 470 * 471 * @rdev: radeon device pointer 472 * @hpd_mask: mask of hpd pins you want to disable. 473 * 474 * Disables the hotplug detect interrupt for a specific hpd pin (all asics). 475 */ 476 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask) 477 { 478 int i; 479 480 if (!rdev->ddev->irq_enabled) 481 return; 482 483 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE); 484 for (i = 0; i < RADEON_MAX_HPD_PINS; ++i) 485 rdev->irq.hpd[i] &= !(hpd_mask & (1 << i)); 486 radeon_irq_set(rdev); 487 lockmgr(&rdev->irq.lock, LK_RELEASE); 488 } 489 490