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 29 /** 30 * DOC: Interrupt Handling 31 * 32 * Interrupts generated within GPU hardware raise interrupt requests that are 33 * passed to amdgpu IRQ handler which is responsible for detecting source and 34 * type of the interrupt and dispatching matching handlers. If handling an 35 * interrupt requires calling kernel functions that may sleep processing is 36 * dispatched to work handlers. 37 * 38 * If MSI functionality is not disabled by module parameter then MSI 39 * support will be enabled. 40 * 41 * For GPU interrupt sources that may be driven by another driver, IRQ domain 42 * support is used (with mapping between virtual and hardware IRQs). 43 */ 44 45 #include <linux/irq.h> 46 #include <linux/pci.h> 47 48 #include <drm/drm_crtc_helper.h> 49 #include <drm/drm_irq.h> 50 #include <drm/drm_vblank.h> 51 #include <drm/amdgpu_drm.h> 52 #include "amdgpu.h" 53 #include "amdgpu_ih.h" 54 #include "atom.h" 55 #include "amdgpu_connectors.h" 56 #include "amdgpu_trace.h" 57 #include "amdgpu_amdkfd.h" 58 59 #include <linux/pm_runtime.h> 60 61 #ifdef CONFIG_DRM_AMD_DC 62 #include "amdgpu_dm_irq.h" 63 #endif 64 65 #define AMDGPU_WAIT_IDLE_TIMEOUT 200 66 67 /** 68 * amdgpu_hotplug_work_func - work handler for display hotplug event 69 * 70 * @work: work struct pointer 71 * 72 * This is the hotplug event work handler (all ASICs). 73 * The work gets scheduled from the IRQ handler if there 74 * was a hotplug interrupt. It walks through the connector table 75 * and calls hotplug handler for each connector. After this, it sends 76 * a DRM hotplug event to alert userspace. 77 * 78 * This design approach is required in order to defer hotplug event handling 79 * from the IRQ handler to a work handler because hotplug handler has to use 80 * mutexes which cannot be locked in an IRQ handler (since &mutex_lock may 81 * sleep). 82 */ 83 static void amdgpu_hotplug_work_func(struct work_struct *work) 84 { 85 struct amdgpu_device *adev = container_of(work, struct amdgpu_device, 86 hotplug_work); 87 struct drm_device *dev = adev->ddev; 88 struct drm_mode_config *mode_config = &dev->mode_config; 89 struct drm_connector *connector; 90 struct drm_connector_list_iter iter; 91 92 mutex_lock(&mode_config->mutex); 93 drm_connector_list_iter_begin(dev, &iter); 94 drm_for_each_connector_iter(connector, &iter) 95 amdgpu_connector_hotplug(connector); 96 drm_connector_list_iter_end(&iter); 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 /** 103 * amdgpu_irq_disable_all - disable *all* interrupts 104 * 105 * @adev: amdgpu device pointer 106 * 107 * Disable all types of interrupts from all sources. 108 */ 109 void amdgpu_irq_disable_all(struct amdgpu_device *adev) 110 { 111 unsigned long irqflags; 112 unsigned i, j, k; 113 int r; 114 115 spin_lock_irqsave(&adev->irq.lock, irqflags); 116 for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) { 117 if (!adev->irq.client[i].sources) 118 continue; 119 120 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) { 121 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j]; 122 123 if (!src || !src->funcs->set || !src->num_types) 124 continue; 125 126 for (k = 0; k < src->num_types; ++k) { 127 atomic_set(&src->enabled_types[k], 0); 128 r = src->funcs->set(adev, src, k, 129 AMDGPU_IRQ_STATE_DISABLE); 130 if (r) 131 DRM_ERROR("error disabling interrupt (%d)\n", 132 r); 133 } 134 } 135 } 136 spin_unlock_irqrestore(&adev->irq.lock, irqflags); 137 } 138 139 /** 140 * amdgpu_irq_handler - IRQ handler 141 * 142 * @irq: IRQ number (unused) 143 * @arg: pointer to DRM device 144 * 145 * IRQ handler for amdgpu driver (all ASICs). 146 * 147 * Returns: 148 * result of handling the IRQ, as defined by &irqreturn_t 149 */ 150 irqreturn_t amdgpu_irq_handler(int irq, void *arg) 151 { 152 struct drm_device *dev = (struct drm_device *) arg; 153 struct amdgpu_device *adev = dev->dev_private; 154 irqreturn_t ret; 155 156 ret = amdgpu_ih_process(adev, &adev->irq.ih); 157 if (ret == IRQ_HANDLED) 158 pm_runtime_mark_last_busy(dev->dev); 159 160 /* For the hardware that cannot enable bif ring for both ras_controller_irq 161 * and ras_err_evnet_athub_irq ih cookies, the driver has to poll status 162 * register to check whether the interrupt is triggered or not, and properly 163 * ack the interrupt if it is there 164 */ 165 if (adev->nbio.funcs && 166 adev->nbio.funcs->handle_ras_controller_intr_no_bifring) 167 adev->nbio.funcs->handle_ras_controller_intr_no_bifring(adev); 168 169 if (adev->nbio.funcs && 170 adev->nbio.funcs->handle_ras_err_event_athub_intr_no_bifring) 171 adev->nbio.funcs->handle_ras_err_event_athub_intr_no_bifring(adev); 172 173 return ret; 174 } 175 176 /** 177 * amdgpu_irq_handle_ih1 - kick of processing for IH1 178 * 179 * @work: work structure in struct amdgpu_irq 180 * 181 * Kick of processing IH ring 1. 182 */ 183 static void amdgpu_irq_handle_ih1(struct work_struct *work) 184 { 185 struct amdgpu_device *adev = container_of(work, struct amdgpu_device, 186 irq.ih1_work); 187 188 amdgpu_ih_process(adev, &adev->irq.ih1); 189 } 190 191 /** 192 * amdgpu_irq_handle_ih2 - kick of processing for IH2 193 * 194 * @work: work structure in struct amdgpu_irq 195 * 196 * Kick of processing IH ring 2. 197 */ 198 static void amdgpu_irq_handle_ih2(struct work_struct *work) 199 { 200 struct amdgpu_device *adev = container_of(work, struct amdgpu_device, 201 irq.ih2_work); 202 203 amdgpu_ih_process(adev, &adev->irq.ih2); 204 } 205 206 /** 207 * amdgpu_msi_ok - check whether MSI functionality is enabled 208 * 209 * @adev: amdgpu device pointer (unused) 210 * 211 * Checks whether MSI functionality has been disabled via module parameter 212 * (all ASICs). 213 * 214 * Returns: 215 * *true* if MSIs are allowed to be enabled or *false* otherwise 216 */ 217 static bool amdgpu_msi_ok(struct amdgpu_device *adev) 218 { 219 if (amdgpu_msi == 1) 220 return true; 221 else if (amdgpu_msi == 0) 222 return false; 223 224 return true; 225 } 226 227 /** 228 * amdgpu_irq_init - initialize interrupt handling 229 * 230 * @adev: amdgpu device pointer 231 * 232 * Sets up work functions for hotplug and reset interrupts, enables MSI 233 * functionality, initializes vblank, hotplug and reset interrupt handling. 234 * 235 * Returns: 236 * 0 on success or error code on failure 237 */ 238 int amdgpu_irq_init(struct amdgpu_device *adev) 239 { 240 int r = 0; 241 242 spin_lock_init(&adev->irq.lock); 243 244 /* Enable MSI if not disabled by module parameter */ 245 adev->irq.msi_enabled = false; 246 247 if (amdgpu_msi_ok(adev)) { 248 int nvec = pci_msix_vec_count(adev->pdev); 249 unsigned int flags; 250 251 if (nvec <= 0) { 252 flags = PCI_IRQ_MSI; 253 } else { 254 flags = PCI_IRQ_MSI | PCI_IRQ_MSIX; 255 } 256 /* we only need one vector */ 257 nvec = pci_alloc_irq_vectors(adev->pdev, 1, 1, flags); 258 if (nvec > 0) { 259 adev->irq.msi_enabled = true; 260 dev_dbg(adev->dev, "amdgpu: using MSI/MSI-X.\n"); 261 } 262 } 263 264 if (!amdgpu_device_has_dc_support(adev)) { 265 if (!adev->enable_virtual_display) 266 /* Disable vblank IRQs aggressively for power-saving */ 267 /* XXX: can this be enabled for DC? */ 268 adev->ddev->vblank_disable_immediate = true; 269 270 r = drm_vblank_init(adev->ddev, adev->mode_info.num_crtc); 271 if (r) 272 return r; 273 274 /* Pre-DCE11 */ 275 INIT_WORK(&adev->hotplug_work, 276 amdgpu_hotplug_work_func); 277 } 278 279 INIT_WORK(&adev->irq.ih1_work, amdgpu_irq_handle_ih1); 280 INIT_WORK(&adev->irq.ih2_work, amdgpu_irq_handle_ih2); 281 282 adev->irq.installed = true; 283 /* Use vector 0 for MSI-X */ 284 r = drm_irq_install(adev->ddev, pci_irq_vector(adev->pdev, 0)); 285 if (r) { 286 adev->irq.installed = false; 287 if (!amdgpu_device_has_dc_support(adev)) 288 flush_work(&adev->hotplug_work); 289 return r; 290 } 291 adev->ddev->max_vblank_count = 0x00ffffff; 292 293 DRM_DEBUG("amdgpu: irq initialized.\n"); 294 return 0; 295 } 296 297 /** 298 * amdgpu_irq_fini - shut down interrupt handling 299 * 300 * @adev: amdgpu device pointer 301 * 302 * Tears down work functions for hotplug and reset interrupts, disables MSI 303 * functionality, shuts down vblank, hotplug and reset interrupt handling, 304 * turns off interrupts from all sources (all ASICs). 305 */ 306 void amdgpu_irq_fini(struct amdgpu_device *adev) 307 { 308 unsigned i, j; 309 310 if (adev->irq.installed) { 311 drm_irq_uninstall(adev->ddev); 312 adev->irq.installed = false; 313 if (adev->irq.msi_enabled) 314 pci_free_irq_vectors(adev->pdev); 315 if (!amdgpu_device_has_dc_support(adev)) 316 flush_work(&adev->hotplug_work); 317 } 318 319 for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) { 320 if (!adev->irq.client[i].sources) 321 continue; 322 323 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) { 324 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j]; 325 326 if (!src) 327 continue; 328 329 kfree(src->enabled_types); 330 src->enabled_types = NULL; 331 if (src->data) { 332 kfree(src->data); 333 kfree(src); 334 adev->irq.client[i].sources[j] = NULL; 335 } 336 } 337 kfree(adev->irq.client[i].sources); 338 adev->irq.client[i].sources = NULL; 339 } 340 } 341 342 /** 343 * amdgpu_irq_add_id - register IRQ source 344 * 345 * @adev: amdgpu device pointer 346 * @client_id: client id 347 * @src_id: source id 348 * @source: IRQ source pointer 349 * 350 * Registers IRQ source on a client. 351 * 352 * Returns: 353 * 0 on success or error code otherwise 354 */ 355 int amdgpu_irq_add_id(struct amdgpu_device *adev, 356 unsigned client_id, unsigned src_id, 357 struct amdgpu_irq_src *source) 358 { 359 if (client_id >= AMDGPU_IRQ_CLIENTID_MAX) 360 return -EINVAL; 361 362 if (src_id >= AMDGPU_MAX_IRQ_SRC_ID) 363 return -EINVAL; 364 365 if (!source->funcs) 366 return -EINVAL; 367 368 if (!adev->irq.client[client_id].sources) { 369 adev->irq.client[client_id].sources = 370 kcalloc(AMDGPU_MAX_IRQ_SRC_ID, 371 sizeof(struct amdgpu_irq_src *), 372 GFP_KERNEL); 373 if (!adev->irq.client[client_id].sources) 374 return -ENOMEM; 375 } 376 377 if (adev->irq.client[client_id].sources[src_id] != NULL) 378 return -EINVAL; 379 380 if (source->num_types && !source->enabled_types) { 381 atomic_t *types; 382 383 types = kcalloc(source->num_types, sizeof(atomic_t), 384 GFP_KERNEL); 385 if (!types) 386 return -ENOMEM; 387 388 source->enabled_types = types; 389 } 390 391 adev->irq.client[client_id].sources[src_id] = source; 392 return 0; 393 } 394 395 /** 396 * amdgpu_irq_dispatch - dispatch IRQ to IP blocks 397 * 398 * @adev: amdgpu device pointer 399 * @ih: interrupt ring instance 400 * 401 * Dispatches IRQ to IP blocks. 402 */ 403 void amdgpu_irq_dispatch(struct amdgpu_device *adev, 404 struct amdgpu_ih_ring *ih) 405 { 406 u32 ring_index = ih->rptr >> 2; 407 struct amdgpu_iv_entry entry; 408 unsigned client_id, src_id; 409 struct amdgpu_irq_src *src; 410 bool handled = false; 411 int r; 412 413 entry.iv_entry = (const uint32_t *)&ih->ring[ring_index]; 414 amdgpu_ih_decode_iv(adev, &entry); 415 416 trace_amdgpu_iv(ih - &adev->irq.ih, &entry); 417 418 client_id = entry.client_id; 419 src_id = entry.src_id; 420 421 if (client_id >= AMDGPU_IRQ_CLIENTID_MAX) { 422 DRM_DEBUG("Invalid client_id in IV: %d\n", client_id); 423 424 } else if (src_id >= AMDGPU_MAX_IRQ_SRC_ID) { 425 DRM_DEBUG("Invalid src_id in IV: %d\n", src_id); 426 427 } else if (adev->irq.virq[src_id]) { 428 generic_handle_irq(irq_find_mapping(adev->irq.domain, src_id)); 429 430 } else if (!adev->irq.client[client_id].sources) { 431 DRM_DEBUG("Unregistered interrupt client_id: %d src_id: %d\n", 432 client_id, src_id); 433 434 } else if ((src = adev->irq.client[client_id].sources[src_id])) { 435 r = src->funcs->process(adev, src, &entry); 436 if (r < 0) 437 DRM_ERROR("error processing interrupt (%d)\n", r); 438 else if (r) 439 handled = true; 440 441 } else { 442 DRM_DEBUG("Unhandled interrupt src_id: %d\n", src_id); 443 } 444 445 /* Send it to amdkfd as well if it isn't already handled */ 446 if (!handled) 447 amdgpu_amdkfd_interrupt(adev, entry.iv_entry); 448 } 449 450 /** 451 * amdgpu_irq_update - update hardware interrupt state 452 * 453 * @adev: amdgpu device pointer 454 * @src: interrupt source pointer 455 * @type: type of interrupt 456 * 457 * Updates interrupt state for the specific source (all ASICs). 458 */ 459 int amdgpu_irq_update(struct amdgpu_device *adev, 460 struct amdgpu_irq_src *src, unsigned type) 461 { 462 unsigned long irqflags; 463 enum amdgpu_interrupt_state state; 464 int r; 465 466 spin_lock_irqsave(&adev->irq.lock, irqflags); 467 468 /* We need to determine after taking the lock, otherwise 469 we might disable just enabled interrupts again */ 470 if (amdgpu_irq_enabled(adev, src, type)) 471 state = AMDGPU_IRQ_STATE_ENABLE; 472 else 473 state = AMDGPU_IRQ_STATE_DISABLE; 474 475 r = src->funcs->set(adev, src, type, state); 476 spin_unlock_irqrestore(&adev->irq.lock, irqflags); 477 return r; 478 } 479 480 /** 481 * amdgpu_irq_gpu_reset_resume_helper - update interrupt states on all sources 482 * 483 * @adev: amdgpu device pointer 484 * 485 * Updates state of all types of interrupts on all sources on resume after 486 * reset. 487 */ 488 void amdgpu_irq_gpu_reset_resume_helper(struct amdgpu_device *adev) 489 { 490 int i, j, k; 491 492 for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) { 493 if (!adev->irq.client[i].sources) 494 continue; 495 496 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) { 497 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j]; 498 499 if (!src) 500 continue; 501 for (k = 0; k < src->num_types; k++) 502 amdgpu_irq_update(adev, src, k); 503 } 504 } 505 } 506 507 /** 508 * amdgpu_irq_get - enable interrupt 509 * 510 * @adev: amdgpu device pointer 511 * @src: interrupt source pointer 512 * @type: type of interrupt 513 * 514 * Enables specified type of interrupt on the specified source (all ASICs). 515 * 516 * Returns: 517 * 0 on success or error code otherwise 518 */ 519 int amdgpu_irq_get(struct amdgpu_device *adev, struct amdgpu_irq_src *src, 520 unsigned type) 521 { 522 if (!adev->ddev->irq_enabled) 523 return -ENOENT; 524 525 if (type >= src->num_types) 526 return -EINVAL; 527 528 if (!src->enabled_types || !src->funcs->set) 529 return -EINVAL; 530 531 if (atomic_inc_return(&src->enabled_types[type]) == 1) 532 return amdgpu_irq_update(adev, src, type); 533 534 return 0; 535 } 536 537 /** 538 * amdgpu_irq_put - disable interrupt 539 * 540 * @adev: amdgpu device pointer 541 * @src: interrupt source pointer 542 * @type: type of interrupt 543 * 544 * Enables specified type of interrupt on the specified source (all ASICs). 545 * 546 * Returns: 547 * 0 on success or error code otherwise 548 */ 549 int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src, 550 unsigned type) 551 { 552 if (!adev->ddev->irq_enabled) 553 return -ENOENT; 554 555 if (type >= src->num_types) 556 return -EINVAL; 557 558 if (!src->enabled_types || !src->funcs->set) 559 return -EINVAL; 560 561 if (atomic_dec_and_test(&src->enabled_types[type])) 562 return amdgpu_irq_update(adev, src, type); 563 564 return 0; 565 } 566 567 /** 568 * amdgpu_irq_enabled - check whether interrupt is enabled or not 569 * 570 * @adev: amdgpu device pointer 571 * @src: interrupt source pointer 572 * @type: type of interrupt 573 * 574 * Checks whether the given type of interrupt is enabled on the given source. 575 * 576 * Returns: 577 * *true* if interrupt is enabled, *false* if interrupt is disabled or on 578 * invalid parameters 579 */ 580 bool amdgpu_irq_enabled(struct amdgpu_device *adev, struct amdgpu_irq_src *src, 581 unsigned type) 582 { 583 if (!adev->ddev->irq_enabled) 584 return false; 585 586 if (type >= src->num_types) 587 return false; 588 589 if (!src->enabled_types || !src->funcs->set) 590 return false; 591 592 return !!atomic_read(&src->enabled_types[type]); 593 } 594 595 /* XXX: Generic IRQ handling */ 596 static void amdgpu_irq_mask(struct irq_data *irqd) 597 { 598 /* XXX */ 599 } 600 601 static void amdgpu_irq_unmask(struct irq_data *irqd) 602 { 603 /* XXX */ 604 } 605 606 /* amdgpu hardware interrupt chip descriptor */ 607 static struct irq_chip amdgpu_irq_chip = { 608 .name = "amdgpu-ih", 609 .irq_mask = amdgpu_irq_mask, 610 .irq_unmask = amdgpu_irq_unmask, 611 }; 612 613 /** 614 * amdgpu_irqdomain_map - create mapping between virtual and hardware IRQ numbers 615 * 616 * @d: amdgpu IRQ domain pointer (unused) 617 * @irq: virtual IRQ number 618 * @hwirq: hardware irq number 619 * 620 * Current implementation assigns simple interrupt handler to the given virtual 621 * IRQ. 622 * 623 * Returns: 624 * 0 on success or error code otherwise 625 */ 626 static int amdgpu_irqdomain_map(struct irq_domain *d, 627 unsigned int irq, irq_hw_number_t hwirq) 628 { 629 if (hwirq >= AMDGPU_MAX_IRQ_SRC_ID) 630 return -EPERM; 631 632 irq_set_chip_and_handler(irq, 633 &amdgpu_irq_chip, handle_simple_irq); 634 return 0; 635 } 636 637 /* Implementation of methods for amdgpu IRQ domain */ 638 static const struct irq_domain_ops amdgpu_hw_irqdomain_ops = { 639 .map = amdgpu_irqdomain_map, 640 }; 641 642 /** 643 * amdgpu_irq_add_domain - create a linear IRQ domain 644 * 645 * @adev: amdgpu device pointer 646 * 647 * Creates an IRQ domain for GPU interrupt sources 648 * that may be driven by another driver (e.g., ACP). 649 * 650 * Returns: 651 * 0 on success or error code otherwise 652 */ 653 int amdgpu_irq_add_domain(struct amdgpu_device *adev) 654 { 655 adev->irq.domain = irq_domain_add_linear(NULL, AMDGPU_MAX_IRQ_SRC_ID, 656 &amdgpu_hw_irqdomain_ops, adev); 657 if (!adev->irq.domain) { 658 DRM_ERROR("GPU irq add domain failed\n"); 659 return -ENODEV; 660 } 661 662 return 0; 663 } 664 665 /** 666 * amdgpu_irq_remove_domain - remove the IRQ domain 667 * 668 * @adev: amdgpu device pointer 669 * 670 * Removes the IRQ domain for GPU interrupt sources 671 * that may be driven by another driver (e.g., ACP). 672 */ 673 void amdgpu_irq_remove_domain(struct amdgpu_device *adev) 674 { 675 if (adev->irq.domain) { 676 irq_domain_remove(adev->irq.domain); 677 adev->irq.domain = NULL; 678 } 679 } 680 681 /** 682 * amdgpu_irq_create_mapping - create mapping between domain Linux IRQs 683 * 684 * @adev: amdgpu device pointer 685 * @src_id: IH source id 686 * 687 * Creates mapping between a domain IRQ (GPU IH src id) and a Linux IRQ 688 * Use this for components that generate a GPU interrupt, but are driven 689 * by a different driver (e.g., ACP). 690 * 691 * Returns: 692 * Linux IRQ 693 */ 694 unsigned amdgpu_irq_create_mapping(struct amdgpu_device *adev, unsigned src_id) 695 { 696 adev->irq.virq[src_id] = irq_create_mapping(adev->irq.domain, src_id); 697 698 return adev->irq.virq[src_id]; 699 } 700