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