1 /* i915_drv.c -- i830,i845,i855,i865,i915 driver -*- linux-c -*- 2 */ 3 /* 4 * 5 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas. 6 * All Rights Reserved. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the 10 * "Software"), to deal in the Software without restriction, including 11 * without limitation the rights to use, copy, modify, merge, publish, 12 * distribute, sub license, and/or sell copies of the Software, and to 13 * permit persons to whom the Software is furnished to do so, subject to 14 * the following conditions: 15 * 16 * The above copyright notice and this permission notice (including the 17 * next paragraph) shall be included in all copies or substantial portions 18 * of the Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 23 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 * 28 */ 29 30 #include <linux/acpi.h> 31 #include <linux/device.h> 32 #include <linux/module.h> 33 #include <linux/oom.h> 34 #include <linux/pci.h> 35 #include <linux/pm.h> 36 #include <linux/pm_runtime.h> 37 #include <linux/slab.h> 38 #include <linux/string_helpers.h> 39 #include <linux/vga_switcheroo.h> 40 #include <linux/vt.h> 41 42 #include <drm/drm_aperture.h> 43 #include <drm/drm_atomic_helper.h> 44 #include <drm/drm_ioctl.h> 45 #include <drm/drm_managed.h> 46 #include <drm/drm_probe_helper.h> 47 48 #include "display/intel_acpi.h" 49 #include "display/intel_bw.h" 50 #include "display/intel_cdclk.h" 51 #include "display/intel_display_driver.h" 52 #include "display/intel_display_types.h" 53 #include "display/intel_dmc.h" 54 #include "display/intel_dp.h" 55 #include "display/intel_dpt.h" 56 #include "display/intel_fbdev.h" 57 #include "display/intel_hotplug.h" 58 #include "display/intel_overlay.h" 59 #include "display/intel_pch_refclk.h" 60 #include "display/intel_pipe_crc.h" 61 #include "display/intel_pps.h" 62 #include "display/intel_sprite.h" 63 #include "display/intel_vga.h" 64 #include "display/skl_watermark.h" 65 66 #include "gem/i915_gem_context.h" 67 #include "gem/i915_gem_create.h" 68 #include "gem/i915_gem_dmabuf.h" 69 #include "gem/i915_gem_ioctls.h" 70 #include "gem/i915_gem_mman.h" 71 #include "gem/i915_gem_pm.h" 72 #include "gt/intel_gt.h" 73 #include "gt/intel_gt_pm.h" 74 #include "gt/intel_rc6.h" 75 76 #include "pxp/intel_pxp.h" 77 #include "pxp/intel_pxp_debugfs.h" 78 #include "pxp/intel_pxp_pm.h" 79 80 #include "soc/intel_dram.h" 81 #include "soc/intel_gmch.h" 82 83 #include "i915_debugfs.h" 84 #include "i915_driver.h" 85 #include "i915_drm_client.h" 86 #include "i915_drv.h" 87 #include "i915_file_private.h" 88 #include "i915_getparam.h" 89 #include "i915_hwmon.h" 90 #include "i915_ioc32.h" 91 #include "i915_ioctl.h" 92 #include "i915_irq.h" 93 #include "i915_memcpy.h" 94 #include "i915_perf.h" 95 #include "i915_query.h" 96 #include "i915_suspend.h" 97 #include "i915_switcheroo.h" 98 #include "i915_sysfs.h" 99 #include "i915_utils.h" 100 #include "i915_vgpu.h" 101 #include "intel_clock_gating.h" 102 #include "intel_gvt.h" 103 #include "intel_memory_region.h" 104 #include "intel_pci_config.h" 105 #include "intel_pcode.h" 106 #include "intel_region_ttm.h" 107 #include "vlv_suspend.h" 108 109 static const struct drm_driver i915_drm_driver; 110 111 static int i915_workqueues_init(struct drm_i915_private *dev_priv) 112 { 113 /* 114 * The i915 workqueue is primarily used for batched retirement of 115 * requests (and thus managing bo) once the task has been completed 116 * by the GPU. i915_retire_requests() is called directly when we 117 * need high-priority retirement, such as waiting for an explicit 118 * bo. 119 * 120 * It is also used for periodic low-priority events, such as 121 * idle-timers and recording error state. 122 * 123 * All tasks on the workqueue are expected to acquire the dev mutex 124 * so there is no point in running more than one instance of the 125 * workqueue at any time. Use an ordered one. 126 */ 127 dev_priv->wq = alloc_ordered_workqueue("i915", 0); 128 if (dev_priv->wq == NULL) 129 goto out_err; 130 131 dev_priv->display.hotplug.dp_wq = alloc_ordered_workqueue("i915-dp", 0); 132 if (dev_priv->display.hotplug.dp_wq == NULL) 133 goto out_free_wq; 134 135 /* 136 * The unordered i915 workqueue should be used for all work 137 * scheduling that do not require running in order, which used 138 * to be scheduled on the system_wq before moving to a driver 139 * instance due deprecation of flush_scheduled_work(). 140 */ 141 dev_priv->unordered_wq = alloc_workqueue("i915-unordered", 0, 0); 142 if (dev_priv->unordered_wq == NULL) 143 goto out_free_dp_wq; 144 145 return 0; 146 147 out_free_dp_wq: 148 destroy_workqueue(dev_priv->display.hotplug.dp_wq); 149 out_free_wq: 150 destroy_workqueue(dev_priv->wq); 151 out_err: 152 drm_err(&dev_priv->drm, "Failed to allocate workqueues.\n"); 153 154 return -ENOMEM; 155 } 156 157 static void i915_workqueues_cleanup(struct drm_i915_private *dev_priv) 158 { 159 destroy_workqueue(dev_priv->unordered_wq); 160 destroy_workqueue(dev_priv->display.hotplug.dp_wq); 161 destroy_workqueue(dev_priv->wq); 162 } 163 164 /* 165 * We don't keep the workarounds for pre-production hardware, so we expect our 166 * driver to fail on these machines in one way or another. A little warning on 167 * dmesg may help both the user and the bug triagers. 168 * 169 * Our policy for removing pre-production workarounds is to keep the 170 * current gen workarounds as a guide to the bring-up of the next gen 171 * (workarounds have a habit of persisting!). Anything older than that 172 * should be removed along with the complications they introduce. 173 */ 174 static void intel_detect_preproduction_hw(struct drm_i915_private *dev_priv) 175 { 176 bool pre = false; 177 178 pre |= IS_HASWELL_EARLY_SDV(dev_priv); 179 pre |= IS_SKYLAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x6; 180 pre |= IS_BROXTON(dev_priv) && INTEL_REVID(dev_priv) < 0xA; 181 pre |= IS_KABYLAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x1; 182 pre |= IS_GEMINILAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x3; 183 pre |= IS_ICELAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x7; 184 pre |= IS_TIGERLAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x1; 185 pre |= IS_DG1(dev_priv) && INTEL_REVID(dev_priv) < 0x1; 186 187 if (pre) { 188 drm_err(&dev_priv->drm, "This is a pre-production stepping. " 189 "It may not be fully functional.\n"); 190 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK); 191 } 192 } 193 194 static void sanitize_gpu(struct drm_i915_private *i915) 195 { 196 if (!INTEL_INFO(i915)->gpu_reset_clobbers_display) { 197 struct intel_gt *gt; 198 unsigned int i; 199 200 for_each_gt(gt, i915, i) 201 __intel_gt_reset(gt, ALL_ENGINES); 202 } 203 } 204 205 /** 206 * i915_driver_early_probe - setup state not requiring device access 207 * @dev_priv: device private 208 * 209 * Initialize everything that is a "SW-only" state, that is state not 210 * requiring accessing the device or exposing the driver via kernel internal 211 * or userspace interfaces. Example steps belonging here: lock initialization, 212 * system memory allocation, setting up device specific attributes and 213 * function hooks not requiring accessing the device. 214 */ 215 static int i915_driver_early_probe(struct drm_i915_private *dev_priv) 216 { 217 int ret = 0; 218 219 if (i915_inject_probe_failure(dev_priv)) 220 return -ENODEV; 221 222 intel_device_info_runtime_init_early(dev_priv); 223 224 intel_step_init(dev_priv); 225 226 intel_uncore_mmio_debug_init_early(dev_priv); 227 228 spin_lock_init(&dev_priv->irq_lock); 229 spin_lock_init(&dev_priv->gpu_error.lock); 230 mutex_init(&dev_priv->display.backlight.lock); 231 232 mutex_init(&dev_priv->sb_lock); 233 cpu_latency_qos_add_request(&dev_priv->sb_qos, PM_QOS_DEFAULT_VALUE); 234 235 mutex_init(&dev_priv->display.audio.mutex); 236 mutex_init(&dev_priv->display.wm.wm_mutex); 237 mutex_init(&dev_priv->display.pps.mutex); 238 mutex_init(&dev_priv->display.hdcp.hdcp_mutex); 239 240 i915_memcpy_init_early(dev_priv); 241 intel_runtime_pm_init_early(&dev_priv->runtime_pm); 242 243 ret = i915_workqueues_init(dev_priv); 244 if (ret < 0) 245 return ret; 246 247 ret = vlv_suspend_init(dev_priv); 248 if (ret < 0) 249 goto err_workqueues; 250 251 ret = intel_region_ttm_device_init(dev_priv); 252 if (ret) 253 goto err_ttm; 254 255 ret = intel_root_gt_init_early(dev_priv); 256 if (ret < 0) 257 goto err_rootgt; 258 259 i915_gem_init_early(dev_priv); 260 261 /* This must be called before any calls to HAS_PCH_* */ 262 intel_detect_pch(dev_priv); 263 264 intel_irq_init(dev_priv); 265 intel_display_driver_early_probe(dev_priv); 266 intel_clock_gating_hooks_init(dev_priv); 267 268 intel_detect_preproduction_hw(dev_priv); 269 270 return 0; 271 272 err_rootgt: 273 intel_region_ttm_device_fini(dev_priv); 274 err_ttm: 275 vlv_suspend_cleanup(dev_priv); 276 err_workqueues: 277 i915_workqueues_cleanup(dev_priv); 278 return ret; 279 } 280 281 /** 282 * i915_driver_late_release - cleanup the setup done in 283 * i915_driver_early_probe() 284 * @dev_priv: device private 285 */ 286 static void i915_driver_late_release(struct drm_i915_private *dev_priv) 287 { 288 intel_irq_fini(dev_priv); 289 intel_power_domains_cleanup(dev_priv); 290 i915_gem_cleanup_early(dev_priv); 291 intel_gt_driver_late_release_all(dev_priv); 292 intel_region_ttm_device_fini(dev_priv); 293 vlv_suspend_cleanup(dev_priv); 294 i915_workqueues_cleanup(dev_priv); 295 296 cpu_latency_qos_remove_request(&dev_priv->sb_qos); 297 mutex_destroy(&dev_priv->sb_lock); 298 299 i915_params_free(&dev_priv->params); 300 } 301 302 /** 303 * i915_driver_mmio_probe - setup device MMIO 304 * @dev_priv: device private 305 * 306 * Setup minimal device state necessary for MMIO accesses later in the 307 * initialization sequence. The setup here should avoid any other device-wide 308 * side effects or exposing the driver via kernel internal or user space 309 * interfaces. 310 */ 311 static int i915_driver_mmio_probe(struct drm_i915_private *dev_priv) 312 { 313 struct intel_gt *gt; 314 int ret, i; 315 316 if (i915_inject_probe_failure(dev_priv)) 317 return -ENODEV; 318 319 ret = intel_gmch_bridge_setup(dev_priv); 320 if (ret < 0) 321 return ret; 322 323 for_each_gt(gt, dev_priv, i) { 324 ret = intel_uncore_init_mmio(gt->uncore); 325 if (ret) 326 return ret; 327 328 ret = drmm_add_action_or_reset(&dev_priv->drm, 329 intel_uncore_fini_mmio, 330 gt->uncore); 331 if (ret) 332 return ret; 333 } 334 335 /* Try to make sure MCHBAR is enabled before poking at it */ 336 intel_gmch_bar_setup(dev_priv); 337 intel_device_info_runtime_init(dev_priv); 338 339 for_each_gt(gt, dev_priv, i) { 340 ret = intel_gt_init_mmio(gt); 341 if (ret) 342 goto err_uncore; 343 } 344 345 /* As early as possible, scrub existing GPU state before clobbering */ 346 sanitize_gpu(dev_priv); 347 348 return 0; 349 350 err_uncore: 351 intel_gmch_bar_teardown(dev_priv); 352 353 return ret; 354 } 355 356 /** 357 * i915_driver_mmio_release - cleanup the setup done in i915_driver_mmio_probe() 358 * @dev_priv: device private 359 */ 360 static void i915_driver_mmio_release(struct drm_i915_private *dev_priv) 361 { 362 intel_gmch_bar_teardown(dev_priv); 363 } 364 365 /** 366 * i915_set_dma_info - set all relevant PCI dma info as configured for the 367 * platform 368 * @i915: valid i915 instance 369 * 370 * Set the dma max segment size, device and coherent masks. The dma mask set 371 * needs to occur before i915_ggtt_probe_hw. 372 * 373 * A couple of platforms have special needs. Address them as well. 374 * 375 */ 376 static int i915_set_dma_info(struct drm_i915_private *i915) 377 { 378 unsigned int mask_size = INTEL_INFO(i915)->dma_mask_size; 379 int ret; 380 381 GEM_BUG_ON(!mask_size); 382 383 /* 384 * We don't have a max segment size, so set it to the max so sg's 385 * debugging layer doesn't complain 386 */ 387 dma_set_max_seg_size(i915->drm.dev, UINT_MAX); 388 389 ret = dma_set_mask(i915->drm.dev, DMA_BIT_MASK(mask_size)); 390 if (ret) 391 goto mask_err; 392 393 /* overlay on gen2 is broken and can't address above 1G */ 394 if (GRAPHICS_VER(i915) == 2) 395 mask_size = 30; 396 397 /* 398 * 965GM sometimes incorrectly writes to hardware status page (HWS) 399 * using 32bit addressing, overwriting memory if HWS is located 400 * above 4GB. 401 * 402 * The documentation also mentions an issue with undefined 403 * behaviour if any general state is accessed within a page above 4GB, 404 * which also needs to be handled carefully. 405 */ 406 if (IS_I965G(i915) || IS_I965GM(i915)) 407 mask_size = 32; 408 409 ret = dma_set_coherent_mask(i915->drm.dev, DMA_BIT_MASK(mask_size)); 410 if (ret) 411 goto mask_err; 412 413 return 0; 414 415 mask_err: 416 drm_err(&i915->drm, "Can't set DMA mask/consistent mask (%d)\n", ret); 417 return ret; 418 } 419 420 static int i915_pcode_init(struct drm_i915_private *i915) 421 { 422 struct intel_gt *gt; 423 int id, ret; 424 425 for_each_gt(gt, i915, id) { 426 ret = intel_pcode_init(gt->uncore); 427 if (ret) { 428 drm_err(>->i915->drm, "gt%d: intel_pcode_init failed %d\n", id, ret); 429 return ret; 430 } 431 } 432 433 return 0; 434 } 435 436 /** 437 * i915_driver_hw_probe - setup state requiring device access 438 * @dev_priv: device private 439 * 440 * Setup state that requires accessing the device, but doesn't require 441 * exposing the driver via kernel internal or userspace interfaces. 442 */ 443 static int i915_driver_hw_probe(struct drm_i915_private *dev_priv) 444 { 445 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev); 446 struct pci_dev *root_pdev; 447 int ret; 448 449 if (i915_inject_probe_failure(dev_priv)) 450 return -ENODEV; 451 452 if (HAS_PPGTT(dev_priv)) { 453 if (intel_vgpu_active(dev_priv) && 454 !intel_vgpu_has_full_ppgtt(dev_priv)) { 455 i915_report_error(dev_priv, 456 "incompatible vGPU found, support for isolated ppGTT required\n"); 457 return -ENXIO; 458 } 459 } 460 461 if (HAS_EXECLISTS(dev_priv)) { 462 /* 463 * Older GVT emulation depends upon intercepting CSB mmio, 464 * which we no longer use, preferring to use the HWSP cache 465 * instead. 466 */ 467 if (intel_vgpu_active(dev_priv) && 468 !intel_vgpu_has_hwsp_emulation(dev_priv)) { 469 i915_report_error(dev_priv, 470 "old vGPU host found, support for HWSP emulation required\n"); 471 return -ENXIO; 472 } 473 } 474 475 /* needs to be done before ggtt probe */ 476 intel_dram_edram_detect(dev_priv); 477 478 ret = i915_set_dma_info(dev_priv); 479 if (ret) 480 return ret; 481 482 ret = i915_perf_init(dev_priv); 483 if (ret) 484 return ret; 485 486 ret = i915_ggtt_probe_hw(dev_priv); 487 if (ret) 488 goto err_perf; 489 490 ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, dev_priv->drm.driver); 491 if (ret) 492 goto err_ggtt; 493 494 ret = i915_ggtt_init_hw(dev_priv); 495 if (ret) 496 goto err_ggtt; 497 498 /* 499 * Make sure we probe lmem before we probe stolen-lmem. The BAR size 500 * might be different due to bar resizing. 501 */ 502 ret = intel_gt_tiles_init(dev_priv); 503 if (ret) 504 goto err_ggtt; 505 506 ret = intel_memory_regions_hw_probe(dev_priv); 507 if (ret) 508 goto err_ggtt; 509 510 ret = i915_ggtt_enable_hw(dev_priv); 511 if (ret) { 512 drm_err(&dev_priv->drm, "failed to enable GGTT\n"); 513 goto err_mem_regions; 514 } 515 516 pci_set_master(pdev); 517 518 /* On the 945G/GM, the chipset reports the MSI capability on the 519 * integrated graphics even though the support isn't actually there 520 * according to the published specs. It doesn't appear to function 521 * correctly in testing on 945G. 522 * This may be a side effect of MSI having been made available for PEG 523 * and the registers being closely associated. 524 * 525 * According to chipset errata, on the 965GM, MSI interrupts may 526 * be lost or delayed, and was defeatured. MSI interrupts seem to 527 * get lost on g4x as well, and interrupt delivery seems to stay 528 * properly dead afterwards. So we'll just disable them for all 529 * pre-gen5 chipsets. 530 * 531 * dp aux and gmbus irq on gen4 seems to be able to generate legacy 532 * interrupts even when in MSI mode. This results in spurious 533 * interrupt warnings if the legacy irq no. is shared with another 534 * device. The kernel then disables that interrupt source and so 535 * prevents the other device from working properly. 536 */ 537 if (GRAPHICS_VER(dev_priv) >= 5) { 538 if (pci_enable_msi(pdev) < 0) 539 drm_dbg(&dev_priv->drm, "can't enable MSI"); 540 } 541 542 ret = intel_gvt_init(dev_priv); 543 if (ret) 544 goto err_msi; 545 546 intel_opregion_setup(dev_priv); 547 548 ret = i915_pcode_init(dev_priv); 549 if (ret) 550 goto err_opregion; 551 552 /* 553 * Fill the dram structure to get the system dram info. This will be 554 * used for memory latency calculation. 555 */ 556 intel_dram_detect(dev_priv); 557 558 intel_bw_init_hw(dev_priv); 559 560 /* 561 * FIXME: Temporary hammer to avoid freezing the machine on our DGFX 562 * This should be totally removed when we handle the pci states properly 563 * on runtime PM and on s2idle cases. 564 */ 565 root_pdev = pcie_find_root_port(pdev); 566 if (root_pdev) 567 pci_d3cold_disable(root_pdev); 568 569 return 0; 570 571 err_opregion: 572 intel_opregion_cleanup(dev_priv); 573 err_msi: 574 if (pdev->msi_enabled) 575 pci_disable_msi(pdev); 576 err_mem_regions: 577 intel_memory_regions_driver_release(dev_priv); 578 err_ggtt: 579 i915_ggtt_driver_release(dev_priv); 580 i915_gem_drain_freed_objects(dev_priv); 581 i915_ggtt_driver_late_release(dev_priv); 582 err_perf: 583 i915_perf_fini(dev_priv); 584 return ret; 585 } 586 587 /** 588 * i915_driver_hw_remove - cleanup the setup done in i915_driver_hw_probe() 589 * @dev_priv: device private 590 */ 591 static void i915_driver_hw_remove(struct drm_i915_private *dev_priv) 592 { 593 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev); 594 struct pci_dev *root_pdev; 595 596 i915_perf_fini(dev_priv); 597 598 intel_opregion_cleanup(dev_priv); 599 600 if (pdev->msi_enabled) 601 pci_disable_msi(pdev); 602 603 root_pdev = pcie_find_root_port(pdev); 604 if (root_pdev) 605 pci_d3cold_enable(root_pdev); 606 } 607 608 /** 609 * i915_driver_register - register the driver with the rest of the system 610 * @dev_priv: device private 611 * 612 * Perform any steps necessary to make the driver available via kernel 613 * internal or userspace interfaces. 614 */ 615 static void i915_driver_register(struct drm_i915_private *dev_priv) 616 { 617 struct intel_gt *gt; 618 unsigned int i; 619 620 i915_gem_driver_register(dev_priv); 621 i915_pmu_register(dev_priv); 622 623 intel_vgpu_register(dev_priv); 624 625 /* Reveal our presence to userspace */ 626 if (drm_dev_register(&dev_priv->drm, 0)) { 627 drm_err(&dev_priv->drm, 628 "Failed to register driver for userspace access!\n"); 629 return; 630 } 631 632 i915_debugfs_register(dev_priv); 633 i915_setup_sysfs(dev_priv); 634 635 /* Depends on sysfs having been initialized */ 636 i915_perf_register(dev_priv); 637 638 for_each_gt(gt, dev_priv, i) 639 intel_gt_driver_register(gt); 640 641 intel_pxp_debugfs_register(dev_priv->pxp); 642 643 i915_hwmon_register(dev_priv); 644 645 intel_display_driver_register(dev_priv); 646 647 intel_power_domains_enable(dev_priv); 648 intel_runtime_pm_enable(&dev_priv->runtime_pm); 649 650 intel_register_dsm_handler(); 651 652 if (i915_switcheroo_register(dev_priv)) 653 drm_err(&dev_priv->drm, "Failed to register vga switcheroo!\n"); 654 } 655 656 /** 657 * i915_driver_unregister - cleanup the registration done in i915_driver_regiser() 658 * @dev_priv: device private 659 */ 660 static void i915_driver_unregister(struct drm_i915_private *dev_priv) 661 { 662 struct intel_gt *gt; 663 unsigned int i; 664 665 i915_switcheroo_unregister(dev_priv); 666 667 intel_unregister_dsm_handler(); 668 669 intel_runtime_pm_disable(&dev_priv->runtime_pm); 670 intel_power_domains_disable(dev_priv); 671 672 intel_display_driver_unregister(dev_priv); 673 674 intel_pxp_fini(dev_priv); 675 676 for_each_gt(gt, dev_priv, i) 677 intel_gt_driver_unregister(gt); 678 679 i915_hwmon_unregister(dev_priv); 680 681 i915_perf_unregister(dev_priv); 682 i915_pmu_unregister(dev_priv); 683 684 i915_teardown_sysfs(dev_priv); 685 drm_dev_unplug(&dev_priv->drm); 686 687 i915_gem_driver_unregister(dev_priv); 688 } 689 690 void 691 i915_print_iommu_status(struct drm_i915_private *i915, struct drm_printer *p) 692 { 693 drm_printf(p, "iommu: %s\n", 694 str_enabled_disabled(i915_vtd_active(i915))); 695 } 696 697 static void i915_welcome_messages(struct drm_i915_private *dev_priv) 698 { 699 if (drm_debug_enabled(DRM_UT_DRIVER)) { 700 struct drm_printer p = drm_debug_printer("i915 device info:"); 701 struct intel_gt *gt; 702 unsigned int i; 703 704 drm_printf(&p, "pciid=0x%04x rev=0x%02x platform=%s (subplatform=0x%x) gen=%i\n", 705 INTEL_DEVID(dev_priv), 706 INTEL_REVID(dev_priv), 707 intel_platform_name(INTEL_INFO(dev_priv)->platform), 708 intel_subplatform(RUNTIME_INFO(dev_priv), 709 INTEL_INFO(dev_priv)->platform), 710 GRAPHICS_VER(dev_priv)); 711 712 intel_device_info_print(INTEL_INFO(dev_priv), 713 RUNTIME_INFO(dev_priv), &p); 714 intel_display_device_info_print(DISPLAY_INFO(dev_priv), 715 DISPLAY_RUNTIME_INFO(dev_priv), &p); 716 i915_print_iommu_status(dev_priv, &p); 717 for_each_gt(gt, dev_priv, i) 718 intel_gt_info_print(>->info, &p); 719 } 720 721 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG)) 722 drm_info(&dev_priv->drm, "DRM_I915_DEBUG enabled\n"); 723 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) 724 drm_info(&dev_priv->drm, "DRM_I915_DEBUG_GEM enabled\n"); 725 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM)) 726 drm_info(&dev_priv->drm, 727 "DRM_I915_DEBUG_RUNTIME_PM enabled\n"); 728 } 729 730 static struct drm_i915_private * 731 i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent) 732 { 733 const struct intel_device_info *match_info = 734 (struct intel_device_info *)ent->driver_data; 735 struct drm_i915_private *i915; 736 737 i915 = devm_drm_dev_alloc(&pdev->dev, &i915_drm_driver, 738 struct drm_i915_private, drm); 739 if (IS_ERR(i915)) 740 return i915; 741 742 pci_set_drvdata(pdev, i915); 743 744 /* Device parameters start as a copy of module parameters. */ 745 i915_params_copy(&i915->params, &i915_modparams); 746 747 /* Set up device info and initial runtime info. */ 748 intel_device_info_driver_create(i915, pdev->device, match_info); 749 750 return i915; 751 } 752 753 /** 754 * i915_driver_probe - setup chip and create an initial config 755 * @pdev: PCI device 756 * @ent: matching PCI ID entry 757 * 758 * The driver probe routine has to do several things: 759 * - drive output discovery via intel_display_driver_probe() 760 * - initialize the memory manager 761 * - allocate initial config memory 762 * - setup the DRM framebuffer with the allocated memory 763 */ 764 int i915_driver_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 765 { 766 struct drm_i915_private *i915; 767 int ret; 768 769 ret = pci_enable_device(pdev); 770 if (ret) { 771 pr_err("Failed to enable graphics device: %pe\n", ERR_PTR(ret)); 772 return ret; 773 } 774 775 i915 = i915_driver_create(pdev, ent); 776 if (IS_ERR(i915)) { 777 pci_disable_device(pdev); 778 return PTR_ERR(i915); 779 } 780 781 ret = i915_driver_early_probe(i915); 782 if (ret < 0) 783 goto out_pci_disable; 784 785 disable_rpm_wakeref_asserts(&i915->runtime_pm); 786 787 intel_vgpu_detect(i915); 788 789 ret = intel_gt_probe_all(i915); 790 if (ret < 0) 791 goto out_runtime_pm_put; 792 793 ret = i915_driver_mmio_probe(i915); 794 if (ret < 0) 795 goto out_tiles_cleanup; 796 797 ret = i915_driver_hw_probe(i915); 798 if (ret < 0) 799 goto out_cleanup_mmio; 800 801 ret = intel_display_driver_probe_noirq(i915); 802 if (ret < 0) 803 goto out_cleanup_hw; 804 805 ret = intel_irq_install(i915); 806 if (ret) 807 goto out_cleanup_modeset; 808 809 ret = intel_display_driver_probe_nogem(i915); 810 if (ret) 811 goto out_cleanup_irq; 812 813 ret = i915_gem_init(i915); 814 if (ret) 815 goto out_cleanup_modeset2; 816 817 intel_pxp_init(i915); 818 819 ret = intel_display_driver_probe(i915); 820 if (ret) 821 goto out_cleanup_gem; 822 823 i915_driver_register(i915); 824 825 enable_rpm_wakeref_asserts(&i915->runtime_pm); 826 827 i915_welcome_messages(i915); 828 829 i915->do_release = true; 830 831 return 0; 832 833 out_cleanup_gem: 834 i915_gem_suspend(i915); 835 i915_gem_driver_remove(i915); 836 i915_gem_driver_release(i915); 837 out_cleanup_modeset2: 838 /* FIXME clean up the error path */ 839 intel_display_driver_remove(i915); 840 intel_irq_uninstall(i915); 841 intel_display_driver_remove_noirq(i915); 842 goto out_cleanup_modeset; 843 out_cleanup_irq: 844 intel_irq_uninstall(i915); 845 out_cleanup_modeset: 846 intel_display_driver_remove_nogem(i915); 847 out_cleanup_hw: 848 i915_driver_hw_remove(i915); 849 intel_memory_regions_driver_release(i915); 850 i915_ggtt_driver_release(i915); 851 i915_gem_drain_freed_objects(i915); 852 i915_ggtt_driver_late_release(i915); 853 out_cleanup_mmio: 854 i915_driver_mmio_release(i915); 855 out_tiles_cleanup: 856 intel_gt_release_all(i915); 857 out_runtime_pm_put: 858 enable_rpm_wakeref_asserts(&i915->runtime_pm); 859 i915_driver_late_release(i915); 860 out_pci_disable: 861 pci_disable_device(pdev); 862 i915_probe_error(i915, "Device initialization failed (%d)\n", ret); 863 return ret; 864 } 865 866 void i915_driver_remove(struct drm_i915_private *i915) 867 { 868 intel_wakeref_t wakeref; 869 870 wakeref = intel_runtime_pm_get(&i915->runtime_pm); 871 872 i915_driver_unregister(i915); 873 874 /* Flush any external code that still may be under the RCU lock */ 875 synchronize_rcu(); 876 877 i915_gem_suspend(i915); 878 879 intel_gvt_driver_remove(i915); 880 881 intel_display_driver_remove(i915); 882 883 intel_irq_uninstall(i915); 884 885 intel_display_driver_remove_noirq(i915); 886 887 i915_reset_error_state(i915); 888 i915_gem_driver_remove(i915); 889 890 intel_display_driver_remove_nogem(i915); 891 892 i915_driver_hw_remove(i915); 893 894 intel_runtime_pm_put(&i915->runtime_pm, wakeref); 895 } 896 897 static void i915_driver_release(struct drm_device *dev) 898 { 899 struct drm_i915_private *dev_priv = to_i915(dev); 900 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm; 901 intel_wakeref_t wakeref; 902 903 if (!dev_priv->do_release) 904 return; 905 906 wakeref = intel_runtime_pm_get(rpm); 907 908 i915_gem_driver_release(dev_priv); 909 910 intel_memory_regions_driver_release(dev_priv); 911 i915_ggtt_driver_release(dev_priv); 912 i915_gem_drain_freed_objects(dev_priv); 913 i915_ggtt_driver_late_release(dev_priv); 914 915 i915_driver_mmio_release(dev_priv); 916 917 intel_runtime_pm_put(rpm, wakeref); 918 919 intel_runtime_pm_driver_release(rpm); 920 921 i915_driver_late_release(dev_priv); 922 } 923 924 static int i915_driver_open(struct drm_device *dev, struct drm_file *file) 925 { 926 struct drm_i915_private *i915 = to_i915(dev); 927 int ret; 928 929 ret = i915_gem_open(i915, file); 930 if (ret) 931 return ret; 932 933 return 0; 934 } 935 936 /** 937 * i915_driver_lastclose - clean up after all DRM clients have exited 938 * @dev: DRM device 939 * 940 * Take care of cleaning up after all DRM clients have exited. In the 941 * mode setting case, we want to restore the kernel's initial mode (just 942 * in case the last client left us in a bad state). 943 * 944 * Additionally, in the non-mode setting case, we'll tear down the GTT 945 * and DMA structures, since the kernel won't be using them, and clea 946 * up any GEM state. 947 */ 948 static void i915_driver_lastclose(struct drm_device *dev) 949 { 950 struct drm_i915_private *i915 = to_i915(dev); 951 952 intel_fbdev_restore_mode(i915); 953 954 vga_switcheroo_process_delayed_switch(); 955 } 956 957 static void i915_driver_postclose(struct drm_device *dev, struct drm_file *file) 958 { 959 struct drm_i915_file_private *file_priv = file->driver_priv; 960 961 i915_gem_context_close(file); 962 i915_drm_client_put(file_priv->client); 963 964 kfree_rcu(file_priv, rcu); 965 966 /* Catch up with all the deferred frees from "this" client */ 967 i915_gem_flush_free_objects(to_i915(dev)); 968 } 969 970 static void intel_suspend_encoders(struct drm_i915_private *dev_priv) 971 { 972 struct intel_encoder *encoder; 973 974 if (!HAS_DISPLAY(dev_priv)) 975 return; 976 977 /* 978 * TODO: check and remove holding the modeset locks if none of 979 * the encoders depends on this. 980 */ 981 drm_modeset_lock_all(&dev_priv->drm); 982 for_each_intel_encoder(&dev_priv->drm, encoder) 983 if (encoder->suspend) 984 encoder->suspend(encoder); 985 drm_modeset_unlock_all(&dev_priv->drm); 986 987 for_each_intel_encoder(&dev_priv->drm, encoder) 988 if (encoder->suspend_complete) 989 encoder->suspend_complete(encoder); 990 } 991 992 static void intel_shutdown_encoders(struct drm_i915_private *dev_priv) 993 { 994 struct intel_encoder *encoder; 995 996 if (!HAS_DISPLAY(dev_priv)) 997 return; 998 999 /* 1000 * TODO: check and remove holding the modeset locks if none of 1001 * the encoders depends on this. 1002 */ 1003 drm_modeset_lock_all(&dev_priv->drm); 1004 for_each_intel_encoder(&dev_priv->drm, encoder) 1005 if (encoder->shutdown) 1006 encoder->shutdown(encoder); 1007 drm_modeset_unlock_all(&dev_priv->drm); 1008 1009 for_each_intel_encoder(&dev_priv->drm, encoder) 1010 if (encoder->shutdown_complete) 1011 encoder->shutdown_complete(encoder); 1012 } 1013 1014 void i915_driver_shutdown(struct drm_i915_private *i915) 1015 { 1016 disable_rpm_wakeref_asserts(&i915->runtime_pm); 1017 intel_runtime_pm_disable(&i915->runtime_pm); 1018 intel_power_domains_disable(i915); 1019 1020 if (HAS_DISPLAY(i915)) { 1021 drm_kms_helper_poll_disable(&i915->drm); 1022 1023 drm_atomic_helper_shutdown(&i915->drm); 1024 } 1025 1026 intel_dp_mst_suspend(i915); 1027 1028 intel_runtime_pm_disable_interrupts(i915); 1029 intel_hpd_cancel_work(i915); 1030 1031 intel_suspend_encoders(i915); 1032 intel_shutdown_encoders(i915); 1033 1034 intel_dmc_suspend(i915); 1035 1036 i915_gem_suspend(i915); 1037 1038 /* 1039 * The only requirement is to reboot with display DC states disabled, 1040 * for now leaving all display power wells in the INIT power domain 1041 * enabled. 1042 * 1043 * TODO: 1044 * - unify the pci_driver::shutdown sequence here with the 1045 * pci_driver.driver.pm.poweroff,poweroff_late sequence. 1046 * - unify the driver remove and system/runtime suspend sequences with 1047 * the above unified shutdown/poweroff sequence. 1048 */ 1049 intel_power_domains_driver_remove(i915); 1050 enable_rpm_wakeref_asserts(&i915->runtime_pm); 1051 1052 intel_runtime_pm_driver_release(&i915->runtime_pm); 1053 } 1054 1055 static bool suspend_to_idle(struct drm_i915_private *dev_priv) 1056 { 1057 #if IS_ENABLED(CONFIG_ACPI_SLEEP) 1058 if (acpi_target_system_state() < ACPI_STATE_S3) 1059 return true; 1060 #endif 1061 return false; 1062 } 1063 1064 static void i915_drm_complete(struct drm_device *dev) 1065 { 1066 struct drm_i915_private *i915 = to_i915(dev); 1067 1068 intel_pxp_resume_complete(i915->pxp); 1069 } 1070 1071 static int i915_drm_prepare(struct drm_device *dev) 1072 { 1073 struct drm_i915_private *i915 = to_i915(dev); 1074 1075 intel_pxp_suspend_prepare(i915->pxp); 1076 1077 /* 1078 * NB intel_display_driver_suspend() may issue new requests after we've 1079 * ostensibly marked the GPU as ready-to-sleep here. We need to 1080 * split out that work and pull it forward so that after point, 1081 * the GPU is not woken again. 1082 */ 1083 return i915_gem_backup_suspend(i915); 1084 } 1085 1086 static int i915_drm_suspend(struct drm_device *dev) 1087 { 1088 struct drm_i915_private *dev_priv = to_i915(dev); 1089 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev); 1090 pci_power_t opregion_target_state; 1091 1092 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm); 1093 1094 /* We do a lot of poking in a lot of registers, make sure they work 1095 * properly. */ 1096 intel_power_domains_disable(dev_priv); 1097 if (HAS_DISPLAY(dev_priv)) 1098 drm_kms_helper_poll_disable(dev); 1099 1100 pci_save_state(pdev); 1101 1102 intel_display_driver_suspend(dev_priv); 1103 1104 intel_dp_mst_suspend(dev_priv); 1105 1106 intel_runtime_pm_disable_interrupts(dev_priv); 1107 intel_hpd_cancel_work(dev_priv); 1108 1109 intel_suspend_encoders(dev_priv); 1110 1111 /* Must be called before GGTT is suspended. */ 1112 intel_dpt_suspend(dev_priv); 1113 i915_ggtt_suspend(to_gt(dev_priv)->ggtt); 1114 1115 i915_save_display(dev_priv); 1116 1117 opregion_target_state = suspend_to_idle(dev_priv) ? PCI_D1 : PCI_D3cold; 1118 intel_opregion_suspend(dev_priv, opregion_target_state); 1119 1120 intel_fbdev_set_suspend(dev, FBINFO_STATE_SUSPENDED, true); 1121 1122 dev_priv->suspend_count++; 1123 1124 intel_dmc_suspend(dev_priv); 1125 1126 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm); 1127 1128 i915_gem_drain_freed_objects(dev_priv); 1129 1130 return 0; 1131 } 1132 1133 static int i915_drm_suspend_late(struct drm_device *dev, bool hibernation) 1134 { 1135 struct drm_i915_private *dev_priv = to_i915(dev); 1136 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev); 1137 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm; 1138 struct intel_gt *gt; 1139 int ret, i; 1140 bool s2idle = !hibernation && suspend_to_idle(dev_priv); 1141 1142 disable_rpm_wakeref_asserts(rpm); 1143 1144 intel_pxp_suspend(dev_priv->pxp); 1145 1146 i915_gem_suspend_late(dev_priv); 1147 1148 for_each_gt(gt, dev_priv, i) 1149 intel_uncore_suspend(gt->uncore); 1150 1151 intel_power_domains_suspend(dev_priv, s2idle); 1152 1153 intel_display_power_suspend_late(dev_priv); 1154 1155 ret = vlv_suspend_complete(dev_priv); 1156 if (ret) { 1157 drm_err(&dev_priv->drm, "Suspend complete failed: %d\n", ret); 1158 intel_power_domains_resume(dev_priv); 1159 1160 goto out; 1161 } 1162 1163 pci_disable_device(pdev); 1164 /* 1165 * During hibernation on some platforms the BIOS may try to access 1166 * the device even though it's already in D3 and hang the machine. So 1167 * leave the device in D0 on those platforms and hope the BIOS will 1168 * power down the device properly. The issue was seen on multiple old 1169 * GENs with different BIOS vendors, so having an explicit blacklist 1170 * is inpractical; apply the workaround on everything pre GEN6. The 1171 * platforms where the issue was seen: 1172 * Lenovo Thinkpad X301, X61s, X60, T60, X41 1173 * Fujitsu FSC S7110 1174 * Acer Aspire 1830T 1175 */ 1176 if (!(hibernation && GRAPHICS_VER(dev_priv) < 6)) 1177 pci_set_power_state(pdev, PCI_D3hot); 1178 1179 out: 1180 enable_rpm_wakeref_asserts(rpm); 1181 if (!dev_priv->uncore.user_forcewake_count) 1182 intel_runtime_pm_driver_release(rpm); 1183 1184 return ret; 1185 } 1186 1187 int i915_driver_suspend_switcheroo(struct drm_i915_private *i915, 1188 pm_message_t state) 1189 { 1190 int error; 1191 1192 if (drm_WARN_ON_ONCE(&i915->drm, state.event != PM_EVENT_SUSPEND && 1193 state.event != PM_EVENT_FREEZE)) 1194 return -EINVAL; 1195 1196 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF) 1197 return 0; 1198 1199 error = i915_drm_suspend(&i915->drm); 1200 if (error) 1201 return error; 1202 1203 return i915_drm_suspend_late(&i915->drm, false); 1204 } 1205 1206 static int i915_drm_resume(struct drm_device *dev) 1207 { 1208 struct drm_i915_private *dev_priv = to_i915(dev); 1209 struct intel_gt *gt; 1210 int ret, i; 1211 1212 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm); 1213 1214 ret = i915_pcode_init(dev_priv); 1215 if (ret) 1216 return ret; 1217 1218 sanitize_gpu(dev_priv); 1219 1220 ret = i915_ggtt_enable_hw(dev_priv); 1221 if (ret) 1222 drm_err(&dev_priv->drm, "failed to re-enable GGTT\n"); 1223 1224 i915_ggtt_resume(to_gt(dev_priv)->ggtt); 1225 1226 for_each_gt(gt, dev_priv, i) 1227 if (GRAPHICS_VER(gt->i915) >= 8) 1228 setup_private_pat(gt); 1229 1230 /* Must be called after GGTT is resumed. */ 1231 intel_dpt_resume(dev_priv); 1232 1233 intel_dmc_resume(dev_priv); 1234 1235 i915_restore_display(dev_priv); 1236 intel_pps_unlock_regs_wa(dev_priv); 1237 1238 intel_init_pch_refclk(dev_priv); 1239 1240 /* 1241 * Interrupts have to be enabled before any batches are run. If not the 1242 * GPU will hang. i915_gem_init_hw() will initiate batches to 1243 * update/restore the context. 1244 * 1245 * drm_mode_config_reset() needs AUX interrupts. 1246 * 1247 * Modeset enabling in intel_display_driver_init_hw() also needs working 1248 * interrupts. 1249 */ 1250 intel_runtime_pm_enable_interrupts(dev_priv); 1251 1252 if (HAS_DISPLAY(dev_priv)) 1253 drm_mode_config_reset(dev); 1254 1255 i915_gem_resume(dev_priv); 1256 1257 intel_display_driver_init_hw(dev_priv); 1258 1259 intel_clock_gating_init(dev_priv); 1260 intel_hpd_init(dev_priv); 1261 1262 /* MST sideband requires HPD interrupts enabled */ 1263 intel_dp_mst_resume(dev_priv); 1264 intel_display_driver_resume(dev_priv); 1265 1266 intel_hpd_poll_disable(dev_priv); 1267 if (HAS_DISPLAY(dev_priv)) 1268 drm_kms_helper_poll_enable(dev); 1269 1270 intel_opregion_resume(dev_priv); 1271 1272 intel_fbdev_set_suspend(dev, FBINFO_STATE_RUNNING, false); 1273 1274 intel_power_domains_enable(dev_priv); 1275 1276 intel_gvt_resume(dev_priv); 1277 1278 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm); 1279 1280 return 0; 1281 } 1282 1283 static int i915_drm_resume_early(struct drm_device *dev) 1284 { 1285 struct drm_i915_private *dev_priv = to_i915(dev); 1286 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev); 1287 struct intel_gt *gt; 1288 int ret, i; 1289 1290 /* 1291 * We have a resume ordering issue with the snd-hda driver also 1292 * requiring our device to be power up. Due to the lack of a 1293 * parent/child relationship we currently solve this with an early 1294 * resume hook. 1295 * 1296 * FIXME: This should be solved with a special hdmi sink device or 1297 * similar so that power domains can be employed. 1298 */ 1299 1300 /* 1301 * Note that we need to set the power state explicitly, since we 1302 * powered off the device during freeze and the PCI core won't power 1303 * it back up for us during thaw. Powering off the device during 1304 * freeze is not a hard requirement though, and during the 1305 * suspend/resume phases the PCI core makes sure we get here with the 1306 * device powered on. So in case we change our freeze logic and keep 1307 * the device powered we can also remove the following set power state 1308 * call. 1309 */ 1310 ret = pci_set_power_state(pdev, PCI_D0); 1311 if (ret) { 1312 drm_err(&dev_priv->drm, 1313 "failed to set PCI D0 power state (%d)\n", ret); 1314 return ret; 1315 } 1316 1317 /* 1318 * Note that pci_enable_device() first enables any parent bridge 1319 * device and only then sets the power state for this device. The 1320 * bridge enabling is a nop though, since bridge devices are resumed 1321 * first. The order of enabling power and enabling the device is 1322 * imposed by the PCI core as described above, so here we preserve the 1323 * same order for the freeze/thaw phases. 1324 * 1325 * TODO: eventually we should remove pci_disable_device() / 1326 * pci_enable_enable_device() from suspend/resume. Due to how they 1327 * depend on the device enable refcount we can't anyway depend on them 1328 * disabling/enabling the device. 1329 */ 1330 if (pci_enable_device(pdev)) 1331 return -EIO; 1332 1333 pci_set_master(pdev); 1334 1335 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm); 1336 1337 ret = vlv_resume_prepare(dev_priv, false); 1338 if (ret) 1339 drm_err(&dev_priv->drm, 1340 "Resume prepare failed: %d, continuing anyway\n", ret); 1341 1342 for_each_gt(gt, dev_priv, i) { 1343 intel_uncore_resume_early(gt->uncore); 1344 intel_gt_check_and_clear_faults(gt); 1345 } 1346 1347 intel_display_power_resume_early(dev_priv); 1348 1349 intel_power_domains_resume(dev_priv); 1350 1351 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm); 1352 1353 return ret; 1354 } 1355 1356 int i915_driver_resume_switcheroo(struct drm_i915_private *i915) 1357 { 1358 int ret; 1359 1360 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF) 1361 return 0; 1362 1363 ret = i915_drm_resume_early(&i915->drm); 1364 if (ret) 1365 return ret; 1366 1367 return i915_drm_resume(&i915->drm); 1368 } 1369 1370 static int i915_pm_prepare(struct device *kdev) 1371 { 1372 struct drm_i915_private *i915 = kdev_to_i915(kdev); 1373 1374 if (!i915) { 1375 dev_err(kdev, "DRM not initialized, aborting suspend.\n"); 1376 return -ENODEV; 1377 } 1378 1379 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF) 1380 return 0; 1381 1382 return i915_drm_prepare(&i915->drm); 1383 } 1384 1385 static int i915_pm_suspend(struct device *kdev) 1386 { 1387 struct drm_i915_private *i915 = kdev_to_i915(kdev); 1388 1389 if (!i915) { 1390 dev_err(kdev, "DRM not initialized, aborting suspend.\n"); 1391 return -ENODEV; 1392 } 1393 1394 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF) 1395 return 0; 1396 1397 return i915_drm_suspend(&i915->drm); 1398 } 1399 1400 static int i915_pm_suspend_late(struct device *kdev) 1401 { 1402 struct drm_i915_private *i915 = kdev_to_i915(kdev); 1403 1404 /* 1405 * We have a suspend ordering issue with the snd-hda driver also 1406 * requiring our device to be power up. Due to the lack of a 1407 * parent/child relationship we currently solve this with an late 1408 * suspend hook. 1409 * 1410 * FIXME: This should be solved with a special hdmi sink device or 1411 * similar so that power domains can be employed. 1412 */ 1413 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF) 1414 return 0; 1415 1416 return i915_drm_suspend_late(&i915->drm, false); 1417 } 1418 1419 static int i915_pm_poweroff_late(struct device *kdev) 1420 { 1421 struct drm_i915_private *i915 = kdev_to_i915(kdev); 1422 1423 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF) 1424 return 0; 1425 1426 return i915_drm_suspend_late(&i915->drm, true); 1427 } 1428 1429 static int i915_pm_resume_early(struct device *kdev) 1430 { 1431 struct drm_i915_private *i915 = kdev_to_i915(kdev); 1432 1433 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF) 1434 return 0; 1435 1436 return i915_drm_resume_early(&i915->drm); 1437 } 1438 1439 static int i915_pm_resume(struct device *kdev) 1440 { 1441 struct drm_i915_private *i915 = kdev_to_i915(kdev); 1442 1443 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF) 1444 return 0; 1445 1446 return i915_drm_resume(&i915->drm); 1447 } 1448 1449 static void i915_pm_complete(struct device *kdev) 1450 { 1451 struct drm_i915_private *i915 = kdev_to_i915(kdev); 1452 1453 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF) 1454 return; 1455 1456 i915_drm_complete(&i915->drm); 1457 } 1458 1459 /* freeze: before creating the hibernation_image */ 1460 static int i915_pm_freeze(struct device *kdev) 1461 { 1462 struct drm_i915_private *i915 = kdev_to_i915(kdev); 1463 int ret; 1464 1465 if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) { 1466 ret = i915_drm_suspend(&i915->drm); 1467 if (ret) 1468 return ret; 1469 } 1470 1471 ret = i915_gem_freeze(i915); 1472 if (ret) 1473 return ret; 1474 1475 return 0; 1476 } 1477 1478 static int i915_pm_freeze_late(struct device *kdev) 1479 { 1480 struct drm_i915_private *i915 = kdev_to_i915(kdev); 1481 int ret; 1482 1483 if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) { 1484 ret = i915_drm_suspend_late(&i915->drm, true); 1485 if (ret) 1486 return ret; 1487 } 1488 1489 ret = i915_gem_freeze_late(i915); 1490 if (ret) 1491 return ret; 1492 1493 return 0; 1494 } 1495 1496 /* thaw: called after creating the hibernation image, but before turning off. */ 1497 static int i915_pm_thaw_early(struct device *kdev) 1498 { 1499 return i915_pm_resume_early(kdev); 1500 } 1501 1502 static int i915_pm_thaw(struct device *kdev) 1503 { 1504 return i915_pm_resume(kdev); 1505 } 1506 1507 /* restore: called after loading the hibernation image. */ 1508 static int i915_pm_restore_early(struct device *kdev) 1509 { 1510 return i915_pm_resume_early(kdev); 1511 } 1512 1513 static int i915_pm_restore(struct device *kdev) 1514 { 1515 return i915_pm_resume(kdev); 1516 } 1517 1518 static int intel_runtime_suspend(struct device *kdev) 1519 { 1520 struct drm_i915_private *dev_priv = kdev_to_i915(kdev); 1521 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm; 1522 struct intel_gt *gt; 1523 int ret, i; 1524 1525 if (drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_RUNTIME_PM(dev_priv))) 1526 return -ENODEV; 1527 1528 drm_dbg(&dev_priv->drm, "Suspending device\n"); 1529 1530 disable_rpm_wakeref_asserts(rpm); 1531 1532 /* 1533 * We are safe here against re-faults, since the fault handler takes 1534 * an RPM reference. 1535 */ 1536 i915_gem_runtime_suspend(dev_priv); 1537 1538 intel_pxp_runtime_suspend(dev_priv->pxp); 1539 1540 for_each_gt(gt, dev_priv, i) 1541 intel_gt_runtime_suspend(gt); 1542 1543 intel_runtime_pm_disable_interrupts(dev_priv); 1544 1545 for_each_gt(gt, dev_priv, i) 1546 intel_uncore_suspend(gt->uncore); 1547 1548 intel_display_power_suspend(dev_priv); 1549 1550 ret = vlv_suspend_complete(dev_priv); 1551 if (ret) { 1552 drm_err(&dev_priv->drm, 1553 "Runtime suspend failed, disabling it (%d)\n", ret); 1554 intel_uncore_runtime_resume(&dev_priv->uncore); 1555 1556 intel_runtime_pm_enable_interrupts(dev_priv); 1557 1558 for_each_gt(gt, dev_priv, i) 1559 intel_gt_runtime_resume(gt); 1560 1561 enable_rpm_wakeref_asserts(rpm); 1562 1563 return ret; 1564 } 1565 1566 enable_rpm_wakeref_asserts(rpm); 1567 intel_runtime_pm_driver_release(rpm); 1568 1569 if (intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore)) 1570 drm_err(&dev_priv->drm, 1571 "Unclaimed access detected prior to suspending\n"); 1572 1573 rpm->suspended = true; 1574 1575 /* 1576 * FIXME: We really should find a document that references the arguments 1577 * used below! 1578 */ 1579 if (IS_BROADWELL(dev_priv)) { 1580 /* 1581 * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop 1582 * being detected, and the call we do at intel_runtime_resume() 1583 * won't be able to restore them. Since PCI_D3hot matches the 1584 * actual specification and appears to be working, use it. 1585 */ 1586 intel_opregion_notify_adapter(dev_priv, PCI_D3hot); 1587 } else { 1588 /* 1589 * current versions of firmware which depend on this opregion 1590 * notification have repurposed the D1 definition to mean 1591 * "runtime suspended" vs. what you would normally expect (D3) 1592 * to distinguish it from notifications that might be sent via 1593 * the suspend path. 1594 */ 1595 intel_opregion_notify_adapter(dev_priv, PCI_D1); 1596 } 1597 1598 assert_forcewakes_inactive(&dev_priv->uncore); 1599 1600 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) 1601 intel_hpd_poll_enable(dev_priv); 1602 1603 drm_dbg(&dev_priv->drm, "Device suspended\n"); 1604 return 0; 1605 } 1606 1607 static int intel_runtime_resume(struct device *kdev) 1608 { 1609 struct drm_i915_private *dev_priv = kdev_to_i915(kdev); 1610 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm; 1611 struct intel_gt *gt; 1612 int ret, i; 1613 1614 if (drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_RUNTIME_PM(dev_priv))) 1615 return -ENODEV; 1616 1617 drm_dbg(&dev_priv->drm, "Resuming device\n"); 1618 1619 drm_WARN_ON_ONCE(&dev_priv->drm, atomic_read(&rpm->wakeref_count)); 1620 disable_rpm_wakeref_asserts(rpm); 1621 1622 intel_opregion_notify_adapter(dev_priv, PCI_D0); 1623 rpm->suspended = false; 1624 if (intel_uncore_unclaimed_mmio(&dev_priv->uncore)) 1625 drm_dbg(&dev_priv->drm, 1626 "Unclaimed access during suspend, bios?\n"); 1627 1628 intel_display_power_resume(dev_priv); 1629 1630 ret = vlv_resume_prepare(dev_priv, true); 1631 1632 for_each_gt(gt, dev_priv, i) 1633 intel_uncore_runtime_resume(gt->uncore); 1634 1635 intel_runtime_pm_enable_interrupts(dev_priv); 1636 1637 /* 1638 * No point of rolling back things in case of an error, as the best 1639 * we can do is to hope that things will still work (and disable RPM). 1640 */ 1641 for_each_gt(gt, dev_priv, i) 1642 intel_gt_runtime_resume(gt); 1643 1644 intel_pxp_runtime_resume(dev_priv->pxp); 1645 1646 /* 1647 * On VLV/CHV display interrupts are part of the display 1648 * power well, so hpd is reinitialized from there. For 1649 * everyone else do it here. 1650 */ 1651 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) { 1652 intel_hpd_init(dev_priv); 1653 intel_hpd_poll_disable(dev_priv); 1654 } 1655 1656 skl_watermark_ipc_update(dev_priv); 1657 1658 enable_rpm_wakeref_asserts(rpm); 1659 1660 if (ret) 1661 drm_err(&dev_priv->drm, 1662 "Runtime resume failed, disabling it (%d)\n", ret); 1663 else 1664 drm_dbg(&dev_priv->drm, "Device resumed\n"); 1665 1666 return ret; 1667 } 1668 1669 const struct dev_pm_ops i915_pm_ops = { 1670 /* 1671 * S0ix (via system suspend) and S3 event handlers [PMSG_SUSPEND, 1672 * PMSG_RESUME] 1673 */ 1674 .prepare = i915_pm_prepare, 1675 .suspend = i915_pm_suspend, 1676 .suspend_late = i915_pm_suspend_late, 1677 .resume_early = i915_pm_resume_early, 1678 .resume = i915_pm_resume, 1679 .complete = i915_pm_complete, 1680 1681 /* 1682 * S4 event handlers 1683 * @freeze, @freeze_late : called (1) before creating the 1684 * hibernation image [PMSG_FREEZE] and 1685 * (2) after rebooting, before restoring 1686 * the image [PMSG_QUIESCE] 1687 * @thaw, @thaw_early : called (1) after creating the hibernation 1688 * image, before writing it [PMSG_THAW] 1689 * and (2) after failing to create or 1690 * restore the image [PMSG_RECOVER] 1691 * @poweroff, @poweroff_late: called after writing the hibernation 1692 * image, before rebooting [PMSG_HIBERNATE] 1693 * @restore, @restore_early : called after rebooting and restoring the 1694 * hibernation image [PMSG_RESTORE] 1695 */ 1696 .freeze = i915_pm_freeze, 1697 .freeze_late = i915_pm_freeze_late, 1698 .thaw_early = i915_pm_thaw_early, 1699 .thaw = i915_pm_thaw, 1700 .poweroff = i915_pm_suspend, 1701 .poweroff_late = i915_pm_poweroff_late, 1702 .restore_early = i915_pm_restore_early, 1703 .restore = i915_pm_restore, 1704 1705 /* S0ix (via runtime suspend) event handlers */ 1706 .runtime_suspend = intel_runtime_suspend, 1707 .runtime_resume = intel_runtime_resume, 1708 }; 1709 1710 static const struct file_operations i915_driver_fops = { 1711 .owner = THIS_MODULE, 1712 .open = drm_open, 1713 .release = drm_release_noglobal, 1714 .unlocked_ioctl = drm_ioctl, 1715 .mmap = i915_gem_mmap, 1716 .poll = drm_poll, 1717 .read = drm_read, 1718 .compat_ioctl = i915_ioc32_compat_ioctl, 1719 .llseek = noop_llseek, 1720 #ifdef CONFIG_PROC_FS 1721 .show_fdinfo = drm_show_fdinfo, 1722 #endif 1723 }; 1724 1725 static int 1726 i915_gem_reject_pin_ioctl(struct drm_device *dev, void *data, 1727 struct drm_file *file) 1728 { 1729 return -ENODEV; 1730 } 1731 1732 static const struct drm_ioctl_desc i915_ioctls[] = { 1733 DRM_IOCTL_DEF_DRV(I915_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 1734 DRM_IOCTL_DEF_DRV(I915_FLUSH, drm_noop, DRM_AUTH), 1735 DRM_IOCTL_DEF_DRV(I915_FLIP, drm_noop, DRM_AUTH), 1736 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, drm_noop, DRM_AUTH), 1737 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, drm_noop, DRM_AUTH), 1738 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, drm_noop, DRM_AUTH), 1739 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam_ioctl, DRM_RENDER_ALLOW), 1740 DRM_IOCTL_DEF_DRV(I915_SETPARAM, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 1741 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH), 1742 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH), 1743 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 1744 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, drm_noop, DRM_AUTH), 1745 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 1746 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 1747 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, drm_noop, DRM_AUTH), 1748 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, drm_noop, DRM_AUTH), 1749 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 1750 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 1751 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, drm_invalid_op, DRM_AUTH), 1752 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2_WR, i915_gem_execbuffer2_ioctl, DRM_RENDER_ALLOW), 1753 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY), 1754 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY), 1755 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_RENDER_ALLOW), 1756 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_RENDER_ALLOW), 1757 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_RENDER_ALLOW), 1758 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_RENDER_ALLOW), 1759 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 1760 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 1761 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_RENDER_ALLOW), 1762 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE_EXT, i915_gem_create_ext_ioctl, DRM_RENDER_ALLOW), 1763 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW), 1764 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW), 1765 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW), 1766 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_OFFSET, i915_gem_mmap_offset_ioctl, DRM_RENDER_ALLOW), 1767 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW), 1768 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW), 1769 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling_ioctl, DRM_RENDER_ALLOW), 1770 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling_ioctl, DRM_RENDER_ALLOW), 1771 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_RENDER_ALLOW), 1772 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id_ioctl, 0), 1773 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_RENDER_ALLOW), 1774 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image_ioctl, DRM_MASTER), 1775 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs_ioctl, DRM_MASTER), 1776 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey_ioctl, DRM_MASTER), 1777 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, drm_noop, DRM_MASTER), 1778 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_RENDER_ALLOW), 1779 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE_EXT, i915_gem_context_create_ioctl, DRM_RENDER_ALLOW), 1780 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_RENDER_ALLOW), 1781 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_RENDER_ALLOW), 1782 DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_gem_context_reset_stats_ioctl, DRM_RENDER_ALLOW), 1783 DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW), 1784 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW), 1785 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW), 1786 DRM_IOCTL_DEF_DRV(I915_PERF_OPEN, i915_perf_open_ioctl, DRM_RENDER_ALLOW), 1787 DRM_IOCTL_DEF_DRV(I915_PERF_ADD_CONFIG, i915_perf_add_config_ioctl, DRM_RENDER_ALLOW), 1788 DRM_IOCTL_DEF_DRV(I915_PERF_REMOVE_CONFIG, i915_perf_remove_config_ioctl, DRM_RENDER_ALLOW), 1789 DRM_IOCTL_DEF_DRV(I915_QUERY, i915_query_ioctl, DRM_RENDER_ALLOW), 1790 DRM_IOCTL_DEF_DRV(I915_GEM_VM_CREATE, i915_gem_vm_create_ioctl, DRM_RENDER_ALLOW), 1791 DRM_IOCTL_DEF_DRV(I915_GEM_VM_DESTROY, i915_gem_vm_destroy_ioctl, DRM_RENDER_ALLOW), 1792 }; 1793 1794 /* 1795 * Interface history: 1796 * 1797 * 1.1: Original. 1798 * 1.2: Add Power Management 1799 * 1.3: Add vblank support 1800 * 1.4: Fix cmdbuffer path, add heap destroy 1801 * 1.5: Add vblank pipe configuration 1802 * 1.6: - New ioctl for scheduling buffer swaps on vertical blank 1803 * - Support vertical blank on secondary display pipe 1804 */ 1805 #define DRIVER_MAJOR 1 1806 #define DRIVER_MINOR 6 1807 #define DRIVER_PATCHLEVEL 0 1808 1809 static const struct drm_driver i915_drm_driver = { 1810 /* Don't use MTRRs here; the Xserver or userspace app should 1811 * deal with them for Intel hardware. 1812 */ 1813 .driver_features = 1814 DRIVER_GEM | 1815 DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_SYNCOBJ | 1816 DRIVER_SYNCOBJ_TIMELINE, 1817 .release = i915_driver_release, 1818 .open = i915_driver_open, 1819 .lastclose = i915_driver_lastclose, 1820 .postclose = i915_driver_postclose, 1821 .show_fdinfo = PTR_IF(IS_ENABLED(CONFIG_PROC_FS), i915_drm_client_fdinfo), 1822 1823 .gem_prime_import = i915_gem_prime_import, 1824 1825 .dumb_create = i915_gem_dumb_create, 1826 .dumb_map_offset = i915_gem_dumb_mmap_offset, 1827 1828 .ioctls = i915_ioctls, 1829 .num_ioctls = ARRAY_SIZE(i915_ioctls), 1830 .fops = &i915_driver_fops, 1831 .name = DRIVER_NAME, 1832 .desc = DRIVER_DESC, 1833 .date = DRIVER_DATE, 1834 .major = DRIVER_MAJOR, 1835 .minor = DRIVER_MINOR, 1836 .patchlevel = DRIVER_PATCHLEVEL, 1837 }; 1838