1 /* $OpenBSD: radeon_irq_kms.c,v 1.4 2014/04/07 06:43:11 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 int radeon_driver_irq_handler_kms(void *); 39 void radeon_driver_irq_preinstall_kms(struct drm_device *); 40 int radeon_driver_irq_postinstall_kms(struct drm_device *); 41 void radeon_driver_irq_uninstall_kms(struct drm_device *); 42 43 /** 44 * radeon_driver_irq_handler_kms - irq handler for KMS 45 * 46 * @DRM_IRQ_ARGS: args 47 * 48 * This is the irq handler for the radeon KMS driver (all asics). 49 * radeon_irq_process is a macro that points to the per-asic 50 * irq handler callback. 51 */ 52 int 53 radeon_driver_irq_handler_kms(void *arg) 54 { 55 struct drm_device *dev = arg; 56 struct radeon_device *rdev = dev->dev_private; 57 58 if (!rdev->irq.installed) 59 return (0); 60 61 return radeon_irq_process(rdev); 62 } 63 64 /* 65 * Handle hotplug events outside the interrupt handler proper. 66 */ 67 /** 68 * radeon_hotplug_work_func - display hotplug work handler 69 * 70 * @work: work struct 71 * 72 * This is the hot plug event work handler (all asics). 73 * The work gets scheduled from the irq handler if there 74 * was a hot plug interrupt. It walks the connector table 75 * and calls the hotplug handler for each one, then sends 76 * a drm hotplug event to alert userspace. 77 */ 78 void 79 radeon_hotplug_work_func(void *arg1, void *arg2) 80 { 81 struct radeon_device *rdev = arg1; 82 struct drm_device *dev = rdev->ddev; 83 struct drm_mode_config *mode_config = &dev->mode_config; 84 struct drm_connector *connector; 85 86 if (mode_config->num_connector) { 87 list_for_each_entry(connector, &mode_config->connector_list, head) 88 radeon_connector_hotplug(connector); 89 } 90 /* Just fire off a uevent and let userspace tell us what to do */ 91 drm_helper_hpd_irq_event(dev); 92 } 93 94 /** 95 * radeon_driver_irq_preinstall_kms - drm irq preinstall callback 96 * 97 * @dev: drm dev pointer 98 * 99 * Gets the hw ready to enable irqs (all asics). 100 * This function disables all interrupt sources on the GPU. 101 */ 102 void radeon_driver_irq_preinstall_kms(struct drm_device *dev) 103 { 104 struct radeon_device *rdev = dev->dev_private; 105 unsigned i; 106 107 mtx_enter(&rdev->irq.lock); 108 /* Disable *all* interrupts */ 109 for (i = 0; i < RADEON_NUM_RINGS; i++) 110 atomic_set(&rdev->irq.ring_int[i], 0); 111 for (i = 0; i < RADEON_MAX_HPD_PINS; i++) 112 rdev->irq.hpd[i] = false; 113 for (i = 0; i < RADEON_MAX_CRTCS; i++) { 114 rdev->irq.crtc_vblank_int[i] = false; 115 atomic_set(&rdev->irq.pflip[i], 0); 116 rdev->irq.afmt[i] = false; 117 } 118 radeon_irq_set(rdev); 119 mtx_leave(&rdev->irq.lock); 120 /* Clear bits */ 121 radeon_irq_process(rdev); 122 } 123 124 /** 125 * radeon_driver_irq_postinstall_kms - drm irq preinstall callback 126 * 127 * @dev: drm dev pointer 128 * 129 * Handles stuff to be done after enabling irqs (all asics). 130 * Returns 0 on success. 131 */ 132 int radeon_driver_irq_postinstall_kms(struct drm_device *dev) 133 { 134 dev->max_vblank_count = 0x001fffff; 135 return 0; 136 } 137 138 /** 139 * radeon_driver_irq_uninstall_kms - drm irq uninstall callback 140 * 141 * @dev: drm dev pointer 142 * 143 * This function disables all interrupt sources on the GPU (all asics). 144 */ 145 void radeon_driver_irq_uninstall_kms(struct drm_device *dev) 146 { 147 struct radeon_device *rdev = dev->dev_private; 148 unsigned i; 149 150 if (rdev == NULL) { 151 return; 152 } 153 mtx_enter(&rdev->irq.lock); 154 /* Disable *all* interrupts */ 155 for (i = 0; i < RADEON_NUM_RINGS; i++) 156 atomic_set(&rdev->irq.ring_int[i], 0); 157 for (i = 0; i < RADEON_MAX_HPD_PINS; i++) 158 rdev->irq.hpd[i] = false; 159 for (i = 0; i < RADEON_MAX_CRTCS; i++) { 160 rdev->irq.crtc_vblank_int[i] = false; 161 atomic_set(&rdev->irq.pflip[i], 0); 162 rdev->irq.afmt[i] = false; 163 } 164 radeon_irq_set(rdev); 165 mtx_leave(&rdev->irq.lock); 166 } 167 168 /** 169 * radeon_msi_ok - asic specific msi checks 170 * 171 * @rdev: radeon device pointer 172 * 173 * Handles asic specific MSI checks to determine if 174 * MSIs should be enabled on a particular chip (all asics). 175 * Returns true if MSIs should be enabled, false if MSIs 176 * should not be enabled. 177 */ 178 bool 179 radeon_msi_ok(struct radeon_device *rdev) 180 { 181 /* RV370/RV380 was first asic with MSI support */ 182 if (rdev->family < CHIP_RV380) 183 return false; 184 185 /* MSIs don't work on AGP */ 186 if (rdev->flags & RADEON_IS_AGP) 187 return false; 188 189 /* force MSI on */ 190 if (radeon_msi == 1) 191 return true; 192 else if (radeon_msi == 0) 193 return false; 194 195 /* Quirks */ 196 /* HP RS690 only seems to work with MSIs. */ 197 if ((rdev->pdev->device == 0x791f) && 198 (rdev->pdev->subsystem_vendor == 0x103c) && 199 (rdev->pdev->subsystem_device == 0x30c2)) 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 == 0x01fc)) 206 return true; 207 208 /* Dell RS690 only seems to work with MSIs. */ 209 if ((rdev->pdev->device == 0x791f) && 210 (rdev->pdev->subsystem_vendor == 0x1028) && 211 (rdev->pdev->subsystem_device == 0x01fd)) 212 return true; 213 214 /* Gateway RS690 only seems to work with MSIs. */ 215 if ((rdev->pdev->device == 0x791f) && 216 (rdev->pdev->subsystem_vendor == 0x107b) && 217 (rdev->pdev->subsystem_device == 0x0185)) 218 return true; 219 220 /* try and enable MSIs by default on all RS690s */ 221 if (rdev->family == CHIP_RS690) 222 return true; 223 224 /* RV515 seems to have MSI issues where it loses 225 * MSI rearms occasionally. This leads to lockups and freezes. 226 * disable it by default. 227 */ 228 if (rdev->family == CHIP_RV515) 229 return false; 230 if (rdev->flags & RADEON_IS_IGP) { 231 /* APUs work fine with MSIs */ 232 if (rdev->family >= CHIP_PALM) 233 return true; 234 /* lots of IGPs have problems with MSIs */ 235 return false; 236 } 237 238 return true; 239 } 240 241 /** 242 * radeon_irq_kms_init - init driver interrupt info 243 * 244 * @rdev: radeon device pointer 245 * 246 * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics). 247 * Returns 0 for success, error for failure. 248 */ 249 int radeon_irq_kms_init(struct radeon_device *rdev) 250 { 251 int r = 0; 252 253 task_set(&rdev->hotplug_task, radeon_hotplug_work_func, rdev, NULL); 254 task_set(&rdev->audio_task, r600_audio_update_hdmi, rdev, NULL); 255 256 mtx_init(&rdev->irq.lock, IPL_TTY); 257 r = drm_vblank_init(rdev->ddev, rdev->num_crtc); 258 if (r) { 259 return r; 260 } 261 #ifdef notyet 262 /* enable msi */ 263 rdev->msi_enabled = 0; 264 265 if (radeon_msi_ok(rdev)) { 266 int ret = pci_enable_msi(rdev->pdev); 267 if (!ret) { 268 rdev->msi_enabled = 1; 269 dev_info(rdev->ddev, "radeon: using MSI.\n"); 270 } 271 } 272 #endif 273 rdev->irq.installed = true; 274 r = drm_irq_install(rdev->ddev); 275 if (r) { 276 rdev->irq.installed = false; 277 return r; 278 } 279 DRM_DEBUG("radeon: irq initialized.\n"); 280 return 0; 281 } 282 283 /** 284 * radeon_irq_kms_fini - tear down driver interrrupt info 285 * 286 * @rdev: radeon device pointer 287 * 288 * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics). 289 */ 290 void radeon_irq_kms_fini(struct radeon_device *rdev) 291 { 292 drm_vblank_cleanup(rdev->ddev); 293 if (rdev->irq.installed) { 294 drm_irq_uninstall(rdev->ddev); 295 rdev->irq.installed = false; 296 #ifdef notyet 297 if (rdev->msi_enabled) 298 pci_disable_msi(rdev->pdev); 299 #endif 300 } 301 #ifdef notyet 302 flush_work(&rdev->hotplug_work); 303 #endif 304 } 305 306 /** 307 * radeon_irq_kms_sw_irq_get - enable software interrupt 308 * 309 * @rdev: radeon device pointer 310 * @ring: ring whose interrupt you want to enable 311 * 312 * Enables the software interrupt for a specific ring (all asics). 313 * The software interrupt is generally used to signal a fence on 314 * a particular ring. 315 */ 316 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring) 317 { 318 if (!rdev->ddev->irq_enabled) 319 return; 320 321 if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) { 322 mtx_enter(&rdev->irq.lock); 323 radeon_irq_set(rdev); 324 mtx_leave(&rdev->irq.lock); 325 } 326 } 327 328 /** 329 * radeon_irq_kms_sw_irq_put - disable software interrupt 330 * 331 * @rdev: radeon device pointer 332 * @ring: ring whose interrupt you want to disable 333 * 334 * Disables the software interrupt for a specific ring (all asics). 335 * The software interrupt is generally used to signal a fence on 336 * a particular ring. 337 */ 338 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring) 339 { 340 if (!rdev->ddev->irq_enabled) 341 return; 342 343 if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) { 344 mtx_enter(&rdev->irq.lock); 345 radeon_irq_set(rdev); 346 mtx_leave(&rdev->irq.lock); 347 } 348 } 349 350 /** 351 * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt 352 * 353 * @rdev: radeon device pointer 354 * @crtc: crtc whose interrupt you want to enable 355 * 356 * Enables the pageflip interrupt for a specific crtc (all asics). 357 * For pageflips we use the vblank interrupt source. 358 */ 359 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc) 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 mtx_enter(&rdev->irq.lock); 369 radeon_irq_set(rdev); 370 mtx_leave(&rdev->irq.lock); 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 if (crtc < 0 || crtc >= rdev->num_crtc) 386 return; 387 388 if (!rdev->ddev->irq_enabled) 389 return; 390 391 if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) { 392 mtx_enter(&rdev->irq.lock); 393 radeon_irq_set(rdev); 394 mtx_leave(&rdev->irq.lock); 395 } 396 } 397 398 /** 399 * radeon_irq_kms_enable_afmt - enable audio format change interrupt 400 * 401 * @rdev: radeon device pointer 402 * @block: afmt block whose interrupt you want to enable 403 * 404 * Enables the afmt change interrupt for a specific afmt block (all asics). 405 */ 406 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block) 407 { 408 if (!rdev->ddev->irq_enabled) 409 return; 410 411 mtx_enter(&rdev->irq.lock); 412 rdev->irq.afmt[block] = true; 413 radeon_irq_set(rdev); 414 mtx_leave(&rdev->irq.lock); 415 416 } 417 418 /** 419 * radeon_irq_kms_disable_afmt - disable audio format change interrupt 420 * 421 * @rdev: radeon device pointer 422 * @block: afmt block whose interrupt you want to disable 423 * 424 * Disables the afmt change interrupt for a specific afmt block (all asics). 425 */ 426 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block) 427 { 428 if (!rdev->ddev->irq_enabled) 429 return; 430 431 mtx_enter(&rdev->irq.lock); 432 rdev->irq.afmt[block] = false; 433 radeon_irq_set(rdev); 434 mtx_leave(&rdev->irq.lock); 435 } 436 437 /** 438 * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt 439 * 440 * @rdev: radeon device pointer 441 * @hpd_mask: mask of hpd pins you want to enable. 442 * 443 * Enables the hotplug detect interrupt for a specific hpd pin (all asics). 444 */ 445 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask) 446 { 447 int i; 448 449 if (!rdev->ddev->irq_enabled) 450 return; 451 452 mtx_enter(&rdev->irq.lock); 453 for (i = 0; i < RADEON_MAX_HPD_PINS; ++i) 454 rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i)); 455 radeon_irq_set(rdev); 456 mtx_leave(&rdev->irq.lock); 457 } 458 459 /** 460 * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt 461 * 462 * @rdev: radeon device pointer 463 * @hpd_mask: mask of hpd pins you want to disable. 464 * 465 * Disables the hotplug detect interrupt for a specific hpd pin (all asics). 466 */ 467 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask) 468 { 469 int i; 470 471 if (!rdev->ddev->irq_enabled) 472 return; 473 474 mtx_enter(&rdev->irq.lock); 475 for (i = 0; i < RADEON_MAX_HPD_PINS; ++i) 476 rdev->irq.hpd[i] &= !(hpd_mask & (1 << i)); 477 radeon_irq_set(rdev); 478 mtx_leave(&rdev->irq.lock); 479 } 480 481