Home | History | Annotate | Line # | Download | only in i915
i915_drv.c revision 1.29
      1 /*	$NetBSD: i915_drv.c,v 1.29 2021/12/19 11:17:44 riastradh Exp $	*/
      2 
      3 /* i915_drv.c -- i830,i845,i855,i865,i915 driver -*- linux-c -*-
      4  */
      5 /*
      6  *
      7  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
      8  * All Rights Reserved.
      9  *
     10  * Permission is hereby granted, free of charge, to any person obtaining a
     11  * copy of this software and associated documentation files (the
     12  * "Software"), to deal in the Software without restriction, including
     13  * without limitation the rights to use, copy, modify, merge, publish,
     14  * distribute, sub license, and/or sell copies of the Software, and to
     15  * permit persons to whom the Software is furnished to do so, subject to
     16  * the following conditions:
     17  *
     18  * The above copyright notice and this permission notice (including the
     19  * next paragraph) shall be included in all copies or substantial portions
     20  * of the Software.
     21  *
     22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
     25  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
     26  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     27  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     28  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     29  *
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: i915_drv.c,v 1.29 2021/12/19 11:17:44 riastradh Exp $");
     34 
     35 #include <linux/acpi.h>
     36 #include <linux/device.h>
     37 #include <linux/oom.h>
     38 #include <linux/module.h>
     39 #include <linux/pci.h>
     40 #include <linux/pm.h>
     41 #include <linux/pm_runtime.h>
     42 #include <linux/pnp.h>
     43 #include <linux/slab.h>
     44 #include <linux/vga_switcheroo.h>
     45 #include <linux/vt.h>
     46 #include <acpi/video.h>
     47 
     48 #include <drm/drm_atomic_helper.h>
     49 #include <drm/drm_ioctl.h>
     50 #include <drm/drm_irq.h>
     51 #include <drm/drm_probe_helper.h>
     52 #include <drm/i915_drm.h>
     53 
     54 #include "display/intel_acpi.h"
     55 #include "display/intel_audio.h"
     56 #include "display/intel_bw.h"
     57 #include "display/intel_cdclk.h"
     58 #include "display/intel_display_types.h"
     59 #include "display/intel_dp.h"
     60 #include "display/intel_fbdev.h"
     61 #include "display/intel_hotplug.h"
     62 #include "display/intel_overlay.h"
     63 #include "display/intel_pipe_crc.h"
     64 #include "display/intel_sprite.h"
     65 #include "display/intel_vga.h"
     66 
     67 #include "gem/i915_gem_context.h"
     68 #include "gem/i915_gem_ioctls.h"
     69 #include "gem/i915_gem_mman.h"
     70 #include "gt/intel_gt.h"
     71 #include "gt/intel_gt_pm.h"
     72 #include "gt/intel_rc6.h"
     73 
     74 #include "i915_debugfs.h"
     75 #include "i915_drv.h"
     76 #include "i915_irq.h"
     77 #include "i915_memcpy.h"
     78 #include "i915_perf.h"
     79 #include "i915_query.h"
     80 #include "i915_suspend.h"
     81 #include "i915_switcheroo.h"
     82 #include "i915_sysfs.h"
     83 #include "i915_trace.h"
     84 #include "i915_vgpu.h"
     85 #include "intel_csr.h"
     86 #include "intel_memory_region.h"
     87 #include "intel_pm.h"
     88 
     89 #ifdef __NetBSD__
     90 #ifdef notyet
     91 #if defined(__i386__)
     92 #include "pnpbios.h"
     93 #endif
     94 #if NPNPBIOS > 0
     95 #define CONFIG_PNP
     96 #endif
     97 #endif
     98 #endif
     99 
    100 #include <linux/nbsd-namespace.h>
    101 
    102 static struct drm_driver driver;
    103 
    104 #ifdef __NetBSD__
    105 /* XXX Kludge to expose this to NetBSD driver attachment goop.  */
    106 struct drm_driver *const i915_drm_driver = &driver;
    107 #endif
    108 struct vlv_s0ix_state {
    109 	/* GAM */
    110 	u32 wr_watermark;
    111 	u32 gfx_prio_ctrl;
    112 	u32 arb_mode;
    113 	u32 gfx_pend_tlb0;
    114 	u32 gfx_pend_tlb1;
    115 	u32 lra_limits[GEN7_LRA_LIMITS_REG_NUM];
    116 	u32 media_max_req_count;
    117 	u32 gfx_max_req_count;
    118 	u32 render_hwsp;
    119 	u32 ecochk;
    120 	u32 bsd_hwsp;
    121 	u32 blt_hwsp;
    122 	u32 tlb_rd_addr;
    123 
    124 	/* MBC */
    125 	u32 g3dctl;
    126 	u32 gsckgctl;
    127 	u32 mbctl;
    128 
    129 	/* GCP */
    130 	u32 ucgctl1;
    131 	u32 ucgctl3;
    132 	u32 rcgctl1;
    133 	u32 rcgctl2;
    134 	u32 rstctl;
    135 	u32 misccpctl;
    136 
    137 	/* GPM */
    138 	u32 gfxpause;
    139 	u32 rpdeuhwtc;
    140 	u32 rpdeuc;
    141 	u32 ecobus;
    142 	u32 pwrdwnupctl;
    143 	u32 rp_down_timeout;
    144 	u32 rp_deucsw;
    145 	u32 rcubmabdtmr;
    146 	u32 rcedata;
    147 	u32 spare2gh;
    148 
    149 	/* Display 1 CZ domain */
    150 	u32 gt_imr;
    151 	u32 gt_ier;
    152 	u32 pm_imr;
    153 	u32 pm_ier;
    154 	u32 gt_scratch[GEN7_GT_SCRATCH_REG_NUM];
    155 
    156 	/* GT SA CZ domain */
    157 	u32 tilectl;
    158 	u32 gt_fifoctl;
    159 	u32 gtlc_wake_ctrl;
    160 	u32 gtlc_survive;
    161 	u32 pmwgicz;
    162 
    163 	/* Display 2 CZ domain */
    164 	u32 gu_ctl0;
    165 	u32 gu_ctl1;
    166 	u32 pcbr;
    167 	u32 clock_gate_dis2;
    168 };
    169 
    170 static int i915_get_bridge_dev(struct drm_i915_private *dev_priv)
    171 {
    172 	int domain = pci_domain_nr(dev_priv->drm.pdev->bus);
    173 
    174 	dev_priv->bridge_dev =
    175 		pci_get_domain_bus_and_slot(domain, 0, PCI_DEVFN(0, 0));
    176 	if (!dev_priv->bridge_dev) {
    177 		DRM_ERROR("bridge device not found\n");
    178 		return -1;
    179 	}
    180 	return 0;
    181 }
    182 
    183 /* Allocate space for the MCH regs if needed, return nonzero on error */
    184 static int
    185 intel_alloc_mchbar_resource(struct drm_i915_private *dev_priv)
    186 {
    187 	int reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
    188 #ifdef CONFIG_PNP
    189 	u32 temp_lo, temp_hi = 0;
    190 	u64 mchbar_addr;
    191 #endif
    192 	int ret;
    193 
    194 #ifdef CONFIG_PNP
    195 	if (INTEL_GEN(dev_priv) >= 4)
    196 		pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
    197 	pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
    198 	mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
    199 
    200 	/* If ACPI doesn't have it, assume we need to allocate it ourselves */
    201 	if (mchbar_addr &&
    202 	    pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
    203 		return 0;
    204 #endif
    205 
    206 	/* Get some space for it */
    207 	dev_priv->mch_res.name = "i915 MCHBAR";
    208 	dev_priv->mch_res.flags = IORESOURCE_MEM;
    209 	ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
    210 				     &dev_priv->mch_res,
    211 				     MCHBAR_SIZE, MCHBAR_SIZE,
    212 				     PCIBIOS_MIN_MEM,
    213 				     0, pcibios_align_resource,
    214 				     dev_priv->bridge_dev);
    215 	if (ret) {
    216 		DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
    217 		dev_priv->mch_res.start = 0;
    218 		return ret;
    219 	}
    220 
    221 	if (INTEL_GEN(dev_priv) >= 4)
    222 		pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
    223 				       upper_32_bits(dev_priv->mch_res.start));
    224 
    225 	pci_write_config_dword(dev_priv->bridge_dev, reg,
    226 			       lower_32_bits(dev_priv->mch_res.start));
    227 	return 0;
    228 }
    229 
    230 /* Setup MCHBAR if possible, return true if we should disable it again */
    231 static void
    232 intel_setup_mchbar(struct drm_i915_private *dev_priv)
    233 {
    234 	int mchbar_reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
    235 	u32 temp;
    236 	bool enabled;
    237 
    238 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
    239 		return;
    240 
    241 	dev_priv->mchbar_need_disable = false;
    242 
    243 	if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
    244 		pci_read_config_dword(dev_priv->bridge_dev, DEVEN, &temp);
    245 		enabled = !!(temp & DEVEN_MCHBAR_EN);
    246 	} else {
    247 		pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
    248 		enabled = temp & 1;
    249 	}
    250 
    251 	/* If it's already enabled, don't have to do anything */
    252 	if (enabled)
    253 		return;
    254 
    255 	if (intel_alloc_mchbar_resource(dev_priv))
    256 		return;
    257 
    258 	dev_priv->mchbar_need_disable = true;
    259 
    260 	/* Space is allocated or reserved, so enable it. */
    261 	if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
    262 		pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
    263 				       temp | DEVEN_MCHBAR_EN);
    264 	} else {
    265 		pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
    266 		pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
    267 	}
    268 }
    269 
    270 static void
    271 intel_teardown_mchbar(struct drm_i915_private *dev_priv)
    272 {
    273 	int mchbar_reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
    274 
    275 	if (dev_priv->mchbar_need_disable) {
    276 		if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
    277 			u32 deven_val;
    278 
    279 			pci_read_config_dword(dev_priv->bridge_dev, DEVEN,
    280 					      &deven_val);
    281 			deven_val &= ~DEVEN_MCHBAR_EN;
    282 			pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
    283 					       deven_val);
    284 		} else {
    285 			u32 mchbar_val;
    286 
    287 			pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg,
    288 					      &mchbar_val);
    289 			mchbar_val &= ~1;
    290 			pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg,
    291 					       mchbar_val);
    292 		}
    293 	}
    294 
    295 	if (dev_priv->mch_res.start)
    296 		release_resource(&dev_priv->mch_res);
    297 }
    298 
    299 static int i915_driver_modeset_probe(struct drm_i915_private *i915)
    300 {
    301 	int ret;
    302 
    303 	if (i915_inject_probe_failure(i915))
    304 		return -ENODEV;
    305 
    306 	if (HAS_DISPLAY(i915) && INTEL_DISPLAY_ENABLED(i915)) {
    307 		ret = drm_vblank_init(&i915->drm,
    308 				      INTEL_NUM_PIPES(i915));
    309 		if (ret)
    310 			goto out;
    311 	}
    312 
    313 #ifdef __NetBSD__		/* XXX vga */
    314 	__USE(i915);
    315 #else
    316 	intel_bios_init(i915);
    317 
    318 	ret = intel_vga_register(i915);
    319 	if (ret)
    320 		goto out;
    321 #endif
    322 
    323 #ifdef __NetBSD__
    324 	intel_register_dsm_handler(i915);
    325 #else
    326 	intel_register_dsm_handler();
    327 #endif
    328 
    329 	ret = i915_switcheroo_register(i915);
    330 	if (ret)
    331 		goto cleanup_vga_client;
    332 
    333 	intel_power_domains_init_hw(i915, false);
    334 
    335 	intel_csr_ucode_init(i915);
    336 
    337 	ret = intel_irq_install(i915);
    338 	if (ret)
    339 		goto cleanup_csr;
    340 
    341 	/* Important: The output setup functions called by modeset_init need
    342 	 * working irqs for e.g. gmbus and dp aux transfers. */
    343 	ret = intel_modeset_init(i915);
    344 	if (ret)
    345 		goto cleanup_irq;
    346 
    347 	ret = i915_gem_init(i915);
    348 	if (ret)
    349 		goto cleanup_modeset;
    350 
    351 	intel_overlay_setup(i915);
    352 
    353 	if (!HAS_DISPLAY(i915) || !INTEL_DISPLAY_ENABLED(i915))
    354 		return 0;
    355 
    356 	ret = intel_fbdev_init(&i915->drm);
    357 	if (ret)
    358 		goto cleanup_gem;
    359 
    360 	/* Only enable hotplug handling once the fbdev is fully set up. */
    361 	intel_hpd_init(i915);
    362 
    363 	intel_init_ipc(i915);
    364 
    365 	return 0;
    366 
    367 cleanup_gem:
    368 	i915_gem_suspend(i915);
    369 	i915_gem_driver_remove(i915);
    370 	i915_gem_driver_release(i915);
    371 cleanup_modeset:
    372 	intel_modeset_driver_remove(i915);
    373 cleanup_irq:
    374 	intel_irq_uninstall(i915);
    375 cleanup_csr:
    376 	intel_csr_ucode_fini(i915);
    377 	intel_power_domains_driver_remove(i915);
    378 	i915_switcheroo_unregister(i915);
    379 cleanup_vga_client:
    380 	intel_vga_unregister(i915);
    381 out:
    382 	return ret;
    383 }
    384 
    385 static void i915_driver_modeset_remove(struct drm_i915_private *i915)
    386 {
    387 	intel_modeset_driver_remove(i915);
    388 
    389 	intel_irq_uninstall(i915);
    390 
    391 	intel_bios_driver_remove(i915);
    392 
    393 	i915_switcheroo_unregister(i915);
    394 
    395 	intel_vga_unregister(i915);
    396 
    397 	intel_csr_ucode_fini(i915);
    398 }
    399 
    400 static void intel_init_dpio(struct drm_i915_private *dev_priv)
    401 {
    402 	/*
    403 	 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
    404 	 * CHV x1 PHY (DP/HDMI D)
    405 	 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
    406 	 */
    407 	if (IS_CHERRYVIEW(dev_priv)) {
    408 		DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
    409 		DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
    410 	} else if (IS_VALLEYVIEW(dev_priv)) {
    411 		DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
    412 	}
    413 }
    414 
    415 static int i915_workqueues_init(struct drm_i915_private *dev_priv)
    416 {
    417 	/*
    418 	 * The i915 workqueue is primarily used for batched retirement of
    419 	 * requests (and thus managing bo) once the task has been completed
    420 	 * by the GPU. i915_retire_requests() is called directly when we
    421 	 * need high-priority retirement, such as waiting for an explicit
    422 	 * bo.
    423 	 *
    424 	 * It is also used for periodic low-priority events, such as
    425 	 * idle-timers and recording error state.
    426 	 *
    427 	 * All tasks on the workqueue are expected to acquire the dev mutex
    428 	 * so there is no point in running more than one instance of the
    429 	 * workqueue at any time.  Use an ordered one.
    430 	 */
    431 	dev_priv->wq = alloc_ordered_workqueue("i915", 0);
    432 	if (dev_priv->wq == NULL)
    433 		goto out_err;
    434 
    435 	dev_priv->hotplug.dp_wq = alloc_ordered_workqueue("i915-dp", 0);
    436 	if (dev_priv->hotplug.dp_wq == NULL)
    437 		goto out_free_wq;
    438 
    439 	return 0;
    440 
    441 out_free_wq:
    442 	destroy_workqueue(dev_priv->wq);
    443 out_err:
    444 	DRM_ERROR("Failed to allocate workqueues.\n");
    445 
    446 	return -ENOMEM;
    447 }
    448 
    449 static void i915_workqueues_cleanup(struct drm_i915_private *dev_priv)
    450 {
    451 	destroy_workqueue(dev_priv->hotplug.dp_wq);
    452 	destroy_workqueue(dev_priv->wq);
    453 }
    454 
    455 /*
    456  * We don't keep the workarounds for pre-production hardware, so we expect our
    457  * driver to fail on these machines in one way or another. A little warning on
    458  * dmesg may help both the user and the bug triagers.
    459  *
    460  * Our policy for removing pre-production workarounds is to keep the
    461  * current gen workarounds as a guide to the bring-up of the next gen
    462  * (workarounds have a habit of persisting!). Anything older than that
    463  * should be removed along with the complications they introduce.
    464  */
    465 static void intel_detect_preproduction_hw(struct drm_i915_private *dev_priv)
    466 {
    467 	bool pre = false;
    468 
    469 	pre |= IS_HSW_EARLY_SDV(dev_priv);
    470 	pre |= IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0);
    471 	pre |= IS_BXT_REVID(dev_priv, 0, BXT_REVID_B_LAST);
    472 	pre |= IS_KBL_REVID(dev_priv, 0, KBL_REVID_A0);
    473 
    474 	if (pre) {
    475 		DRM_ERROR("This is a pre-production stepping. "
    476 			  "It may not be fully functional.\n");
    477 		add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK);
    478 	}
    479 }
    480 
    481 static int vlv_alloc_s0ix_state(struct drm_i915_private *i915)
    482 {
    483 	if (!IS_VALLEYVIEW(i915))
    484 		return 0;
    485 
    486 	/* we write all the values in the struct, so no need to zero it out */
    487 	i915->vlv_s0ix_state = kmalloc(sizeof(*i915->vlv_s0ix_state),
    488 				       GFP_KERNEL);
    489 	if (!i915->vlv_s0ix_state)
    490 		return -ENOMEM;
    491 
    492 	return 0;
    493 }
    494 
    495 static void vlv_free_s0ix_state(struct drm_i915_private *i915)
    496 {
    497 	if (!i915->vlv_s0ix_state)
    498 		return;
    499 
    500 	kfree(i915->vlv_s0ix_state);
    501 	i915->vlv_s0ix_state = NULL;
    502 }
    503 
    504 static void sanitize_gpu(struct drm_i915_private *i915)
    505 {
    506 	if (!INTEL_INFO(i915)->gpu_reset_clobbers_display)
    507 		__intel_gt_reset(&i915->gt, ALL_ENGINES);
    508 }
    509 
    510 /**
    511  * i915_driver_early_probe - setup state not requiring device access
    512  * @dev_priv: device private
    513  *
    514  * Initialize everything that is a "SW-only" state, that is state not
    515  * requiring accessing the device or exposing the driver via kernel internal
    516  * or userspace interfaces. Example steps belonging here: lock initialization,
    517  * system memory allocation, setting up device specific attributes and
    518  * function hooks not requiring accessing the device.
    519  */
    520 static int i915_driver_early_probe(struct drm_i915_private *dev_priv)
    521 {
    522 	int ret = 0;
    523 
    524 	if (i915_inject_probe_failure(dev_priv))
    525 		return -ENODEV;
    526 
    527 	intel_device_info_subplatform_init(dev_priv);
    528 
    529 	intel_uncore_mmio_debug_init_early(&dev_priv->mmio_debug);
    530 	intel_uncore_init_early(&dev_priv->uncore, dev_priv);
    531 
    532 	spin_lock_init(&dev_priv->irq_lock);
    533 	spin_lock_init(&dev_priv->gpu_error.lock);
    534 	mutex_init(&dev_priv->backlight_lock);
    535 
    536 	mutex_init(&dev_priv->sb_lock);
    537 	pm_qos_add_request(&dev_priv->sb_qos,
    538 			   PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
    539 
    540 	mutex_init(&dev_priv->av_mutex);
    541 	mutex_init(&dev_priv->wm.wm_mutex);
    542 	mutex_init(&dev_priv->pps_mutex);
    543 	mutex_init(&dev_priv->hdcp_comp_mutex);
    544 
    545 	i915_memcpy_init_early(dev_priv);
    546 	intel_runtime_pm_init_early(&dev_priv->runtime_pm);
    547 
    548 	ret = i915_workqueues_init(dev_priv);
    549 	if (ret < 0)
    550 		return ret;
    551 
    552 	ret = vlv_alloc_s0ix_state(dev_priv);
    553 	if (ret < 0)
    554 		goto err_workqueues;
    555 
    556 	intel_wopcm_init_early(&dev_priv->wopcm);
    557 
    558 	intel_gt_init_early(&dev_priv->gt, dev_priv);
    559 
    560 	i915_gem_init_early(dev_priv);
    561 
    562 	/* This must be called before any calls to HAS_PCH_* */
    563 	intel_detect_pch(dev_priv);
    564 
    565 	intel_pm_setup(dev_priv);
    566 	intel_init_dpio(dev_priv);
    567 	ret = intel_power_domains_init(dev_priv);
    568 	if (ret < 0)
    569 		goto err_gem;
    570 	intel_irq_init(dev_priv);
    571 	intel_init_display_hooks(dev_priv);
    572 	intel_init_clock_gating_hooks(dev_priv);
    573 	intel_init_audio_hooks(dev_priv);
    574 	intel_display_crc_init(dev_priv);
    575 
    576 	intel_detect_preproduction_hw(dev_priv);
    577 
    578 	return 0;
    579 
    580 err_gem:
    581 	i915_gem_cleanup_early(dev_priv);
    582 	intel_gt_driver_late_release(&dev_priv->gt);
    583 	vlv_free_s0ix_state(dev_priv);
    584 err_workqueues:
    585 	i915_workqueues_cleanup(dev_priv);
    586 	mutex_destroy(&dev_priv->hdcp_comp_mutex);
    587 	mutex_destroy(&dev_priv->pps_mutex);
    588 	mutex_destroy(&dev_priv->wm.wm_mutex);
    589 	mutex_destroy(&dev_priv->av_mutex);
    590 	mutex_destroy(&dev_priv->sb_lock);
    591 	mutex_destroy(&dev_priv->backlight_lock);
    592 	spin_lock_destroy(&dev_priv->gpu_error.lock);
    593 	spin_lock_destroy(&dev_priv->irq_lock);
    594 	return ret;
    595 }
    596 
    597 /**
    598  * i915_driver_late_release - cleanup the setup done in
    599  *			       i915_driver_early_probe()
    600  * @dev_priv: device private
    601  */
    602 static void i915_driver_late_release(struct drm_i915_private *dev_priv)
    603 {
    604 	intel_irq_fini(dev_priv);
    605 	intel_power_domains_cleanup(dev_priv);
    606 	i915_gem_cleanup_early(dev_priv);
    607 	intel_gt_driver_late_release(&dev_priv->gt);
    608 	vlv_free_s0ix_state(dev_priv);
    609 	i915_workqueues_cleanup(dev_priv);
    610 
    611 	pm_qos_remove_request(&dev_priv->sb_qos);
    612 	mutex_destroy(&dev_priv->hdcp_comp_mutex);
    613 	mutex_destroy(&dev_priv->pps_mutex);
    614 	mutex_destroy(&dev_priv->wm.wm_mutex);
    615 	mutex_destroy(&dev_priv->av_mutex);
    616 	mutex_destroy(&dev_priv->sb_lock);
    617 	mutex_destroy(&dev_priv->sb_lock);
    618 	mutex_destroy(&dev_priv->backlight_lock);
    619 	spin_lock_destroy(&dev_priv->gpu_error.lock);
    620 	spin_lock_destroy(&dev_priv->irq_lock);
    621 }
    622 
    623 /**
    624  * i915_driver_mmio_probe - setup device MMIO
    625  * @dev_priv: device private
    626  *
    627  * Setup minimal device state necessary for MMIO accesses later in the
    628  * initialization sequence. The setup here should avoid any other device-wide
    629  * side effects or exposing the driver via kernel internal or user space
    630  * interfaces.
    631  */
    632 static int i915_driver_mmio_probe(struct drm_i915_private *dev_priv)
    633 {
    634 	int ret;
    635 
    636 	if (i915_inject_probe_failure(dev_priv))
    637 		return -ENODEV;
    638 
    639 	if (i915_get_bridge_dev(dev_priv))
    640 		return -EIO;
    641 
    642 	ret = intel_uncore_init_mmio(&dev_priv->uncore);
    643 	if (ret < 0)
    644 		goto err_bridge;
    645 
    646 	/* Try to make sure MCHBAR is enabled before poking at it */
    647 	intel_setup_mchbar(dev_priv);
    648 
    649 	intel_device_info_init_mmio(dev_priv);
    650 
    651 	intel_uncore_prune_mmio_domains(&dev_priv->uncore);
    652 
    653 	intel_uc_init_mmio(&dev_priv->gt.uc);
    654 
    655 	ret = intel_engines_init_mmio(&dev_priv->gt);
    656 	if (ret)
    657 		goto err_uncore;
    658 
    659 	/* As early as possible, scrub existing GPU state before clobbering */
    660 	sanitize_gpu(dev_priv);
    661 
    662 	return 0;
    663 
    664 err_uncore:
    665 	intel_teardown_mchbar(dev_priv);
    666 	intel_uncore_fini_mmio(&dev_priv->uncore);
    667 err_bridge:
    668 	pci_dev_put(dev_priv->bridge_dev);
    669 
    670 	return ret;
    671 }
    672 
    673 /**
    674  * i915_driver_mmio_release - cleanup the setup done in i915_driver_mmio_probe()
    675  * @dev_priv: device private
    676  */
    677 static void i915_driver_mmio_release(struct drm_i915_private *dev_priv)
    678 {
    679 	intel_teardown_mchbar(dev_priv);
    680 	intel_uncore_fini_mmio(&dev_priv->uncore);
    681 	pci_dev_put(dev_priv->bridge_dev);
    682 }
    683 
    684 static void intel_sanitize_options(struct drm_i915_private *dev_priv)
    685 {
    686 	intel_gvt_sanitize_options(dev_priv);
    687 }
    688 
    689 #define DRAM_TYPE_STR(type) [INTEL_DRAM_ ## type] = #type
    690 
    691 static const char *intel_dram_type_str(enum intel_dram_type type)
    692 {
    693 	static const char * const str[] = {
    694 		DRAM_TYPE_STR(UNKNOWN),
    695 		DRAM_TYPE_STR(DDR3),
    696 		DRAM_TYPE_STR(DDR4),
    697 		DRAM_TYPE_STR(LPDDR3),
    698 		DRAM_TYPE_STR(LPDDR4),
    699 	};
    700 
    701 	if (type >= ARRAY_SIZE(str))
    702 		type = INTEL_DRAM_UNKNOWN;
    703 
    704 	return str[type];
    705 }
    706 
    707 #undef DRAM_TYPE_STR
    708 
    709 static int intel_dimm_num_devices(const struct dram_dimm_info *dimm)
    710 {
    711 	return dimm->ranks * 64 / (dimm->width ?: 1);
    712 }
    713 
    714 /* Returns total GB for the whole DIMM */
    715 static int skl_get_dimm_size(u16 val)
    716 {
    717 	return val & SKL_DRAM_SIZE_MASK;
    718 }
    719 
    720 static int skl_get_dimm_width(u16 val)
    721 {
    722 	if (skl_get_dimm_size(val) == 0)
    723 		return 0;
    724 
    725 	switch (val & SKL_DRAM_WIDTH_MASK) {
    726 	case SKL_DRAM_WIDTH_X8:
    727 	case SKL_DRAM_WIDTH_X16:
    728 	case SKL_DRAM_WIDTH_X32:
    729 		val = (val & SKL_DRAM_WIDTH_MASK) >> SKL_DRAM_WIDTH_SHIFT;
    730 		return 8 << val;
    731 	default:
    732 		MISSING_CASE(val);
    733 		return 0;
    734 	}
    735 }
    736 
    737 static int skl_get_dimm_ranks(u16 val)
    738 {
    739 	if (skl_get_dimm_size(val) == 0)
    740 		return 0;
    741 
    742 	val = (val & SKL_DRAM_RANK_MASK) >> SKL_DRAM_RANK_SHIFT;
    743 
    744 	return val + 1;
    745 }
    746 
    747 /* Returns total GB for the whole DIMM */
    748 static int cnl_get_dimm_size(u16 val)
    749 {
    750 	return (val & CNL_DRAM_SIZE_MASK) / 2;
    751 }
    752 
    753 static int cnl_get_dimm_width(u16 val)
    754 {
    755 	if (cnl_get_dimm_size(val) == 0)
    756 		return 0;
    757 
    758 	switch (val & CNL_DRAM_WIDTH_MASK) {
    759 	case CNL_DRAM_WIDTH_X8:
    760 	case CNL_DRAM_WIDTH_X16:
    761 	case CNL_DRAM_WIDTH_X32:
    762 		val = (val & CNL_DRAM_WIDTH_MASK) >> CNL_DRAM_WIDTH_SHIFT;
    763 		return 8 << val;
    764 	default:
    765 		MISSING_CASE(val);
    766 		return 0;
    767 	}
    768 }
    769 
    770 static int cnl_get_dimm_ranks(u16 val)
    771 {
    772 	if (cnl_get_dimm_size(val) == 0)
    773 		return 0;
    774 
    775 	val = (val & CNL_DRAM_RANK_MASK) >> CNL_DRAM_RANK_SHIFT;
    776 
    777 	return val + 1;
    778 }
    779 
    780 static bool
    781 skl_is_16gb_dimm(const struct dram_dimm_info *dimm)
    782 {
    783 	/* Convert total GB to Gb per DRAM device */
    784 	return 8 * dimm->size / (intel_dimm_num_devices(dimm) ?: 1) == 16;
    785 }
    786 
    787 static void
    788 skl_dram_get_dimm_info(struct drm_i915_private *dev_priv,
    789 		       struct dram_dimm_info *dimm,
    790 		       int channel, char dimm_name, u16 val)
    791 {
    792 	if (INTEL_GEN(dev_priv) >= 10) {
    793 		dimm->size = cnl_get_dimm_size(val);
    794 		dimm->width = cnl_get_dimm_width(val);
    795 		dimm->ranks = cnl_get_dimm_ranks(val);
    796 	} else {
    797 		dimm->size = skl_get_dimm_size(val);
    798 		dimm->width = skl_get_dimm_width(val);
    799 		dimm->ranks = skl_get_dimm_ranks(val);
    800 	}
    801 
    802 	DRM_DEBUG_KMS("CH%u DIMM %c size: %u GB, width: X%u, ranks: %u, 16Gb DIMMs: %s\n",
    803 		      channel, dimm_name, dimm->size, dimm->width, dimm->ranks,
    804 		      yesno(skl_is_16gb_dimm(dimm)));
    805 }
    806 
    807 static int
    808 skl_dram_get_channel_info(struct drm_i915_private *dev_priv,
    809 			  struct dram_channel_info *ch,
    810 			  int channel, u32 val)
    811 {
    812 	skl_dram_get_dimm_info(dev_priv, &ch->dimm_l,
    813 			       channel, 'L', val & 0xffff);
    814 	skl_dram_get_dimm_info(dev_priv, &ch->dimm_s,
    815 			       channel, 'S', val >> 16);
    816 
    817 	if (ch->dimm_l.size == 0 && ch->dimm_s.size == 0) {
    818 		DRM_DEBUG_KMS("CH%u not populated\n", channel);
    819 		return -EINVAL;
    820 	}
    821 
    822 	if (ch->dimm_l.ranks == 2 || ch->dimm_s.ranks == 2)
    823 		ch->ranks = 2;
    824 	else if (ch->dimm_l.ranks == 1 && ch->dimm_s.ranks == 1)
    825 		ch->ranks = 2;
    826 	else
    827 		ch->ranks = 1;
    828 
    829 	ch->is_16gb_dimm =
    830 		skl_is_16gb_dimm(&ch->dimm_l) ||
    831 		skl_is_16gb_dimm(&ch->dimm_s);
    832 
    833 	DRM_DEBUG_KMS("CH%u ranks: %u, 16Gb DIMMs: %s\n",
    834 		      channel, ch->ranks, yesno(ch->is_16gb_dimm));
    835 
    836 	return 0;
    837 }
    838 
    839 static bool
    840 intel_is_dram_symmetric(const struct dram_channel_info *ch0,
    841 			const struct dram_channel_info *ch1)
    842 {
    843 	return !memcmp(ch0, ch1, sizeof(*ch0)) &&
    844 		(ch0->dimm_s.size == 0 ||
    845 		 !memcmp(&ch0->dimm_l, &ch0->dimm_s, sizeof(ch0->dimm_l)));
    846 }
    847 
    848 static int
    849 skl_dram_get_channels_info(struct drm_i915_private *dev_priv)
    850 {
    851 	struct dram_info *dram_info = &dev_priv->dram_info;
    852 	struct dram_channel_info ch0 = {}, ch1 = {};
    853 	u32 val;
    854 	int ret;
    855 
    856 	val = I915_READ(SKL_MAD_DIMM_CH0_0_0_0_MCHBAR_MCMAIN);
    857 	ret = skl_dram_get_channel_info(dev_priv, &ch0, 0, val);
    858 	if (ret == 0)
    859 		dram_info->num_channels++;
    860 
    861 	val = I915_READ(SKL_MAD_DIMM_CH1_0_0_0_MCHBAR_MCMAIN);
    862 	ret = skl_dram_get_channel_info(dev_priv, &ch1, 1, val);
    863 	if (ret == 0)
    864 		dram_info->num_channels++;
    865 
    866 	if (dram_info->num_channels == 0) {
    867 		DRM_INFO("Number of memory channels is zero\n");
    868 		return -EINVAL;
    869 	}
    870 
    871 	/*
    872 	 * If any of the channel is single rank channel, worst case output
    873 	 * will be same as if single rank memory, so consider single rank
    874 	 * memory.
    875 	 */
    876 	if (ch0.ranks == 1 || ch1.ranks == 1)
    877 		dram_info->ranks = 1;
    878 	else
    879 		dram_info->ranks = max(ch0.ranks, ch1.ranks);
    880 
    881 	if (dram_info->ranks == 0) {
    882 		DRM_INFO("couldn't get memory rank information\n");
    883 		return -EINVAL;
    884 	}
    885 
    886 	dram_info->is_16gb_dimm = ch0.is_16gb_dimm || ch1.is_16gb_dimm;
    887 
    888 	dram_info->symmetric_memory = intel_is_dram_symmetric(&ch0, &ch1);
    889 
    890 	DRM_DEBUG_KMS("Memory configuration is symmetric? %s\n",
    891 		      yesno(dram_info->symmetric_memory));
    892 	return 0;
    893 }
    894 
    895 #ifdef __NetBSD__
    896 /* XXX Kludge to expose this to NetBSD driver attachment goop.  */
    897 const struct pci_device_id *const i915_device_ids = pciidlist;
    898 const size_t i915_n_device_ids = __arraycount(pciidlist);
    899 #endif
    900 
    901 static enum intel_dram_type
    902 skl_get_dram_type(struct drm_i915_private *dev_priv)
    903 {
    904 	u32 val;
    905 
    906 	val = I915_READ(SKL_MAD_INTER_CHANNEL_0_0_0_MCHBAR_MCMAIN);
    907 
    908 	switch (val & SKL_DRAM_DDR_TYPE_MASK) {
    909 	case SKL_DRAM_DDR_TYPE_DDR3:
    910 		return INTEL_DRAM_DDR3;
    911 	case SKL_DRAM_DDR_TYPE_DDR4:
    912 		return INTEL_DRAM_DDR4;
    913 	case SKL_DRAM_DDR_TYPE_LPDDR3:
    914 		return INTEL_DRAM_LPDDR3;
    915 	case SKL_DRAM_DDR_TYPE_LPDDR4:
    916 		return INTEL_DRAM_LPDDR4;
    917 	default:
    918 		MISSING_CASE(val);
    919 		return INTEL_DRAM_UNKNOWN;
    920 	}
    921 }
    922 
    923 static int
    924 skl_get_dram_info(struct drm_i915_private *dev_priv)
    925 {
    926 	struct dram_info *dram_info = &dev_priv->dram_info;
    927 	u32 mem_freq_khz, val;
    928 	int ret;
    929 
    930 	dram_info->type = skl_get_dram_type(dev_priv);
    931 	DRM_DEBUG_KMS("DRAM type: %s\n", intel_dram_type_str(dram_info->type));
    932 
    933 	ret = skl_dram_get_channels_info(dev_priv);
    934 	if (ret)
    935 		return ret;
    936 
    937 	val = I915_READ(SKL_MC_BIOS_DATA_0_0_0_MCHBAR_PCU);
    938 	mem_freq_khz = DIV_ROUND_UP((val & SKL_REQ_DATA_MASK) *
    939 				    SKL_MEMORY_FREQ_MULTIPLIER_HZ, 1000);
    940 
    941 	dram_info->bandwidth_kbps = dram_info->num_channels *
    942 							mem_freq_khz * 8;
    943 
    944 	if (dram_info->bandwidth_kbps == 0) {
    945 		DRM_INFO("Couldn't get system memory bandwidth\n");
    946 		return -EINVAL;
    947 	}
    948 
    949 	dram_info->valid = true;
    950 	return 0;
    951 }
    952 
    953 /* Returns Gb per DRAM device */
    954 static int bxt_get_dimm_size(u32 val)
    955 {
    956 	switch (val & BXT_DRAM_SIZE_MASK) {
    957 	case BXT_DRAM_SIZE_4GBIT:
    958 		return 4;
    959 	case BXT_DRAM_SIZE_6GBIT:
    960 		return 6;
    961 	case BXT_DRAM_SIZE_8GBIT:
    962 		return 8;
    963 	case BXT_DRAM_SIZE_12GBIT:
    964 		return 12;
    965 	case BXT_DRAM_SIZE_16GBIT:
    966 		return 16;
    967 	default:
    968 		MISSING_CASE(val);
    969 		return 0;
    970 	}
    971 }
    972 
    973 static int bxt_get_dimm_width(u32 val)
    974 {
    975 	if (!bxt_get_dimm_size(val))
    976 		return 0;
    977 
    978 	val = (val & BXT_DRAM_WIDTH_MASK) >> BXT_DRAM_WIDTH_SHIFT;
    979 
    980 	return 8 << val;
    981 }
    982 
    983 static int bxt_get_dimm_ranks(u32 val)
    984 {
    985 	if (!bxt_get_dimm_size(val))
    986 		return 0;
    987 
    988 	switch (val & BXT_DRAM_RANK_MASK) {
    989 	case BXT_DRAM_RANK_SINGLE:
    990 		return 1;
    991 	case BXT_DRAM_RANK_DUAL:
    992 		return 2;
    993 	default:
    994 		MISSING_CASE(val);
    995 		return 0;
    996 	}
    997 }
    998 
    999 static enum intel_dram_type bxt_get_dimm_type(u32 val)
   1000 {
   1001 	if (!bxt_get_dimm_size(val))
   1002 		return INTEL_DRAM_UNKNOWN;
   1003 
   1004 	switch (val & BXT_DRAM_TYPE_MASK) {
   1005 	case BXT_DRAM_TYPE_DDR3:
   1006 		return INTEL_DRAM_DDR3;
   1007 	case BXT_DRAM_TYPE_LPDDR3:
   1008 		return INTEL_DRAM_LPDDR3;
   1009 	case BXT_DRAM_TYPE_DDR4:
   1010 		return INTEL_DRAM_DDR4;
   1011 	case BXT_DRAM_TYPE_LPDDR4:
   1012 		return INTEL_DRAM_LPDDR4;
   1013 	default:
   1014 		MISSING_CASE(val);
   1015 		return INTEL_DRAM_UNKNOWN;
   1016 	}
   1017 }
   1018 
   1019 static void bxt_get_dimm_info(struct dram_dimm_info *dimm,
   1020 			      u32 val)
   1021 {
   1022 	dimm->width = bxt_get_dimm_width(val);
   1023 	dimm->ranks = bxt_get_dimm_ranks(val);
   1024 
   1025 	/*
   1026 	 * Size in register is Gb per DRAM device. Convert to total
   1027 	 * GB to match the way we report this for non-LP platforms.
   1028 	 */
   1029 	dimm->size = bxt_get_dimm_size(val) * intel_dimm_num_devices(dimm) / 8;
   1030 }
   1031 
   1032 static int
   1033 bxt_get_dram_info(struct drm_i915_private *dev_priv)
   1034 {
   1035 	struct dram_info *dram_info = &dev_priv->dram_info;
   1036 	u32 dram_channels;
   1037 	u32 mem_freq_khz, val;
   1038 	u8 num_active_channels;
   1039 	int i;
   1040 
   1041 	val = I915_READ(BXT_P_CR_MC_BIOS_REQ_0_0_0);
   1042 	mem_freq_khz = DIV_ROUND_UP((val & BXT_REQ_DATA_MASK) *
   1043 				    BXT_MEMORY_FREQ_MULTIPLIER_HZ, 1000);
   1044 
   1045 	dram_channels = val & BXT_DRAM_CHANNEL_ACTIVE_MASK;
   1046 	num_active_channels = hweight32(dram_channels);
   1047 
   1048 	/* Each active bit represents 4-byte channel */
   1049 	dram_info->bandwidth_kbps = (mem_freq_khz * num_active_channels * 4);
   1050 
   1051 	if (dram_info->bandwidth_kbps == 0) {
   1052 		DRM_INFO("Couldn't get system memory bandwidth\n");
   1053 		return -EINVAL;
   1054 	}
   1055 
   1056 	/*
   1057 	 * Now read each DUNIT8/9/10/11 to check the rank of each dimms.
   1058 	 */
   1059 	for (i = BXT_D_CR_DRP0_DUNIT_START; i <= BXT_D_CR_DRP0_DUNIT_END; i++) {
   1060 		struct dram_dimm_info dimm;
   1061 		enum intel_dram_type type;
   1062 
   1063 		val = I915_READ(BXT_D_CR_DRP0_DUNIT(i));
   1064 		if (val == 0xFFFFFFFF)
   1065 			continue;
   1066 
   1067 		dram_info->num_channels++;
   1068 
   1069 		bxt_get_dimm_info(&dimm, val);
   1070 		type = bxt_get_dimm_type(val);
   1071 
   1072 		WARN_ON(type != INTEL_DRAM_UNKNOWN &&
   1073 			dram_info->type != INTEL_DRAM_UNKNOWN &&
   1074 			dram_info->type != type);
   1075 
   1076 		DRM_DEBUG_KMS("CH%u DIMM size: %u GB, width: X%u, ranks: %u, type: %s\n",
   1077 			      i - BXT_D_CR_DRP0_DUNIT_START,
   1078 			      dimm.size, dimm.width, dimm.ranks,
   1079 			      intel_dram_type_str(type));
   1080 
   1081 		/*
   1082 		 * If any of the channel is single rank channel,
   1083 		 * worst case output will be same as if single rank
   1084 		 * memory, so consider single rank memory.
   1085 		 */
   1086 		if (dram_info->ranks == 0)
   1087 			dram_info->ranks = dimm.ranks;
   1088 		else if (dimm.ranks == 1)
   1089 			dram_info->ranks = 1;
   1090 
   1091 		if (type != INTEL_DRAM_UNKNOWN)
   1092 			dram_info->type = type;
   1093 	}
   1094 
   1095 	if (dram_info->type == INTEL_DRAM_UNKNOWN ||
   1096 	    dram_info->ranks == 0) {
   1097 		DRM_INFO("couldn't get memory information\n");
   1098 		return -EINVAL;
   1099 	}
   1100 
   1101 	dram_info->valid = true;
   1102 	return 0;
   1103 }
   1104 
   1105 static void
   1106 intel_get_dram_info(struct drm_i915_private *dev_priv)
   1107 {
   1108 	struct dram_info *dram_info = &dev_priv->dram_info;
   1109 	int ret;
   1110 
   1111 	/*
   1112 	 * Assume 16Gb DIMMs are present until proven otherwise.
   1113 	 * This is only used for the level 0 watermark latency
   1114 	 * w/a which does not apply to bxt/glk.
   1115 	 */
   1116 	dram_info->is_16gb_dimm = !IS_GEN9_LP(dev_priv);
   1117 
   1118 	if (INTEL_GEN(dev_priv) < 9 || !HAS_DISPLAY(dev_priv))
   1119 		return;
   1120 
   1121 	if (IS_GEN9_LP(dev_priv))
   1122 		ret = bxt_get_dram_info(dev_priv);
   1123 	else
   1124 		ret = skl_get_dram_info(dev_priv);
   1125 	if (ret)
   1126 		return;
   1127 
   1128 	DRM_DEBUG_KMS("DRAM bandwidth: %u kBps, channels: %u\n",
   1129 		      dram_info->bandwidth_kbps,
   1130 		      dram_info->num_channels);
   1131 
   1132 	DRM_DEBUG_KMS("DRAM ranks: %u, 16Gb DIMMs: %s\n",
   1133 		      dram_info->ranks, yesno(dram_info->is_16gb_dimm));
   1134 }
   1135 
   1136 static u32 gen9_edram_size_mb(struct drm_i915_private *dev_priv, u32 cap)
   1137 {
   1138 	static const u8 ways[8] = { 4, 8, 12, 16, 16, 16, 16, 16 };
   1139 	static const u8 sets[4] = { 1, 1, 2, 2 };
   1140 
   1141 	return EDRAM_NUM_BANKS(cap) *
   1142 		ways[EDRAM_WAYS_IDX(cap)] *
   1143 		sets[EDRAM_SETS_IDX(cap)];
   1144 }
   1145 
   1146 static void edram_detect(struct drm_i915_private *dev_priv)
   1147 {
   1148 	u32 edram_cap = 0;
   1149 
   1150 	if (!(IS_HASWELL(dev_priv) ||
   1151 	      IS_BROADWELL(dev_priv) ||
   1152 	      INTEL_GEN(dev_priv) >= 9))
   1153 		return;
   1154 
   1155 	edram_cap = __raw_uncore_read32(&dev_priv->uncore, HSW_EDRAM_CAP);
   1156 
   1157 	/* NB: We can't write IDICR yet because we don't have gt funcs set up */
   1158 
   1159 	if (!(edram_cap & EDRAM_ENABLED))
   1160 		return;
   1161 
   1162 	/*
   1163 	 * The needed capability bits for size calculation are not there with
   1164 	 * pre gen9 so return 128MB always.
   1165 	 */
   1166 	if (INTEL_GEN(dev_priv) < 9)
   1167 		dev_priv->edram_size_mb = 128;
   1168 	else
   1169 		dev_priv->edram_size_mb =
   1170 			gen9_edram_size_mb(dev_priv, edram_cap);
   1171 
   1172 	dev_info(dev_priv->drm.dev,
   1173 		 "Found %uMB of eDRAM\n", dev_priv->edram_size_mb);
   1174 }
   1175 
   1176 /**
   1177  * i915_driver_hw_probe - setup state requiring device access
   1178  * @dev_priv: device private
   1179  *
   1180  * Setup state that requires accessing the device, but doesn't require
   1181  * exposing the driver via kernel internal or userspace interfaces.
   1182  */
   1183 static int i915_driver_hw_probe(struct drm_i915_private *dev_priv)
   1184 {
   1185 	struct pci_dev *pdev = dev_priv->drm.pdev;
   1186 	int ret;
   1187 
   1188 	if (i915_inject_probe_failure(dev_priv))
   1189 		return -ENODEV;
   1190 
   1191 	intel_device_info_runtime_init(dev_priv);
   1192 
   1193 	if (HAS_PPGTT(dev_priv)) {
   1194 		if (intel_vgpu_active(dev_priv) &&
   1195 		    !intel_vgpu_has_full_ppgtt(dev_priv)) {
   1196 			i915_report_error(dev_priv,
   1197 					  "incompatible vGPU found, support for isolated ppGTT required\n");
   1198 			return -ENXIO;
   1199 		}
   1200 	}
   1201 
   1202 	if (HAS_EXECLISTS(dev_priv)) {
   1203 		/*
   1204 		 * Older GVT emulation depends upon intercepting CSB mmio,
   1205 		 * which we no longer use, preferring to use the HWSP cache
   1206 		 * instead.
   1207 		 */
   1208 		if (intel_vgpu_active(dev_priv) &&
   1209 		    !intel_vgpu_has_hwsp_emulation(dev_priv)) {
   1210 			i915_report_error(dev_priv,
   1211 					  "old vGPU host found, support for HWSP emulation required\n");
   1212 			return -ENXIO;
   1213 		}
   1214 	}
   1215 
   1216 	intel_sanitize_options(dev_priv);
   1217 
   1218 	/* needs to be done before ggtt probe */
   1219 	edram_detect(dev_priv);
   1220 
   1221 	i915_perf_init(dev_priv);
   1222 
   1223 	ret = i915_ggtt_probe_hw(dev_priv);
   1224 	if (ret)
   1225 		goto err_perf;
   1226 
   1227 	ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "inteldrmfb");
   1228 	if (ret)
   1229 		goto err_ggtt;
   1230 
   1231 	ret = i915_ggtt_init_hw(dev_priv);
   1232 	if (ret)
   1233 		goto err_ggtt;
   1234 
   1235 	ret = intel_memory_regions_hw_probe(dev_priv);
   1236 	if (ret)
   1237 		goto err_ggtt;
   1238 
   1239 	intel_gt_init_hw_early(&dev_priv->gt, &dev_priv->ggtt);
   1240 
   1241 	ret = i915_ggtt_enable_hw(dev_priv);
   1242 	if (ret) {
   1243 		DRM_ERROR("failed to enable GGTT\n");
   1244 		goto err_mem_regions;
   1245 	}
   1246 
   1247 	pci_set_master(pdev);
   1248 
   1249 	/*
   1250 	 * We don't have a max segment size, so set it to the max so sg's
   1251 	 * debugging layer doesn't complain
   1252 	 */
   1253 	dma_set_max_seg_size(&pdev->dev, UINT_MAX);
   1254 
   1255 #ifndef __NetBSD__		/* Handled in intel_ggtt.c.  */
   1256 	/* overlay on gen2 is broken and can't address above 1G */
   1257 	if (IS_GEN(dev_priv, 2)) {
   1258 		ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(30));
   1259 		if (ret) {
   1260 			DRM_ERROR("failed to set DMA mask\n");
   1261 
   1262 			goto err_mem_regions;
   1263 		}
   1264 	}
   1265 
   1266 	/* 965GM sometimes incorrectly writes to hardware status page (HWS)
   1267 	 * using 32bit addressing, overwriting memory if HWS is located
   1268 	 * above 4GB.
   1269 	 *
   1270 	 * The documentation also mentions an issue with undefined
   1271 	 * behaviour if any general state is accessed within a page above 4GB,
   1272 	 * which also needs to be handled carefully.
   1273 	 */
   1274 	if (IS_I965G(dev_priv) || IS_I965GM(dev_priv)) {
   1275 		ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
   1276 
   1277 		if (ret) {
   1278 			DRM_ERROR("failed to set DMA mask\n");
   1279 
   1280 			goto err_mem_regions;
   1281 		}
   1282 	}
   1283 #endif
   1284 
   1285 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY,
   1286 			   PM_QOS_DEFAULT_VALUE);
   1287 
   1288 	intel_gt_init_workarounds(dev_priv);
   1289 
   1290 	/* On the 945G/GM, the chipset reports the MSI capability on the
   1291 	 * integrated graphics even though the support isn't actually there
   1292 	 * according to the published specs.  It doesn't appear to function
   1293 	 * correctly in testing on 945G.
   1294 	 * This may be a side effect of MSI having been made available for PEG
   1295 	 * and the registers being closely associated.
   1296 	 *
   1297 	 * According to chipset errata, on the 965GM, MSI interrupts may
   1298 	 * be lost or delayed, and was defeatured. MSI interrupts seem to
   1299 	 * get lost on g4x as well, and interrupt delivery seems to stay
   1300 	 * properly dead afterwards. So we'll just disable them for all
   1301 	 * pre-gen5 chipsets.
   1302 	 *
   1303 	 * dp aux and gmbus irq on gen4 seems to be able to generate legacy
   1304 	 * interrupts even when in MSI mode. This results in spurious
   1305 	 * interrupt warnings if the legacy irq no. is shared with another
   1306 	 * device. The kernel then disables that interrupt source and so
   1307 	 * prevents the other device from working properly.
   1308 	 */
   1309 	if (INTEL_GEN(dev_priv) >= 5) {
   1310 		if (pci_enable_msi(pdev) < 0)
   1311 			DRM_DEBUG_DRIVER("can't enable MSI");
   1312 	}
   1313 
   1314 	ret = intel_gvt_init(dev_priv);
   1315 	if (ret)
   1316 		goto err_msi;
   1317 
   1318 	intel_opregion_setup(dev_priv);
   1319 	/*
   1320 	 * Fill the dram structure to get the system raw bandwidth and
   1321 	 * dram info. This will be used for memory latency calculation.
   1322 	 */
   1323 	intel_get_dram_info(dev_priv);
   1324 
   1325 	intel_bw_init_hw(dev_priv);
   1326 
   1327 	return 0;
   1328 
   1329 err_msi:
   1330 	if (pdev->msi_enabled)
   1331 		pci_disable_msi(pdev);
   1332 	pm_qos_remove_request(&dev_priv->pm_qos);
   1333 err_mem_regions:
   1334 	intel_memory_regions_driver_release(dev_priv);
   1335 err_ggtt:
   1336 	i915_ggtt_driver_release(dev_priv);
   1337 err_perf:
   1338 	i915_perf_fini(dev_priv);
   1339 	return ret;
   1340 }
   1341 
   1342 /**
   1343  * i915_driver_hw_remove - cleanup the setup done in i915_driver_hw_probe()
   1344  * @dev_priv: device private
   1345  */
   1346 static void i915_driver_hw_remove(struct drm_i915_private *dev_priv)
   1347 {
   1348 	struct pci_dev *pdev = dev_priv->drm.pdev;
   1349 
   1350 	i915_perf_fini(dev_priv);
   1351 
   1352 	if (pdev->msi_enabled)
   1353 		pci_disable_msi(pdev);
   1354 
   1355 	pm_qos_remove_request(&dev_priv->pm_qos);
   1356 }
   1357 
   1358 /**
   1359  * i915_driver_register - register the driver with the rest of the system
   1360  * @dev_priv: device private
   1361  *
   1362  * Perform any steps necessary to make the driver available via kernel
   1363  * internal or userspace interfaces.
   1364  */
   1365 static void i915_driver_register(struct drm_i915_private *dev_priv)
   1366 {
   1367 	struct drm_device *dev = &dev_priv->drm;
   1368 
   1369 	i915_gem_driver_register(dev_priv);
   1370 	i915_pmu_register(dev_priv);
   1371 
   1372 	/*
   1373 	 * Notify a valid surface after modesetting,
   1374 	 * when running inside a VM.
   1375 	 */
   1376 	if (intel_vgpu_active(dev_priv))
   1377 		I915_WRITE(vgtif_reg(display_ready), VGT_DRV_DISPLAY_READY);
   1378 
   1379 	/* Reveal our presence to userspace */
   1380 	if (drm_dev_register(dev, 0) == 0) {
   1381 		i915_debugfs_register(dev_priv);
   1382 		i915_setup_sysfs(dev_priv);
   1383 
   1384 		/* Depends on sysfs having been initialized */
   1385 		i915_perf_register(dev_priv);
   1386 	} else
   1387 		DRM_ERROR("Failed to register driver for userspace access!\n");
   1388 
   1389 	if (HAS_DISPLAY(dev_priv) && INTEL_DISPLAY_ENABLED(dev_priv)) {
   1390 		/* Must be done after probing outputs */
   1391 		intel_opregion_register(dev_priv);
   1392 		acpi_video_register();
   1393 	}
   1394 
   1395 	intel_gt_driver_register(&dev_priv->gt);
   1396 
   1397 	intel_audio_init(dev_priv);
   1398 
   1399 	/*
   1400 	 * Some ports require correctly set-up hpd registers for detection to
   1401 	 * work properly (leading to ghost connected connector status), e.g. VGA
   1402 	 * on gm45.  Hence we can only set up the initial fbdev config after hpd
   1403 	 * irqs are fully enabled. We do it last so that the async config
   1404 	 * cannot run before the connectors are registered.
   1405 	 */
   1406 	intel_fbdev_initial_config_async(dev);
   1407 
   1408 	/*
   1409 	 * We need to coordinate the hotplugs with the asynchronous fbdev
   1410 	 * configuration, for which we use the fbdev->async_cookie.
   1411 	 */
   1412 	if (HAS_DISPLAY(dev_priv) && INTEL_DISPLAY_ENABLED(dev_priv))
   1413 		drm_kms_helper_poll_init(dev);
   1414 
   1415 	intel_power_domains_enable(dev_priv);
   1416 	intel_runtime_pm_enable(&dev_priv->runtime_pm);
   1417 }
   1418 
   1419 /**
   1420  * i915_driver_unregister - cleanup the registration done in i915_driver_regiser()
   1421  * @dev_priv: device private
   1422  */
   1423 static void i915_driver_unregister(struct drm_i915_private *dev_priv)
   1424 {
   1425 	intel_runtime_pm_disable(&dev_priv->runtime_pm);
   1426 	intel_power_domains_disable(dev_priv);
   1427 
   1428 	intel_fbdev_unregister(dev_priv);
   1429 	intel_audio_deinit(dev_priv);
   1430 
   1431 	/*
   1432 	 * After flushing the fbdev (incl. a late async config which will
   1433 	 * have delayed queuing of a hotplug event), then flush the hotplug
   1434 	 * events.
   1435 	 */
   1436 	drm_kms_helper_poll_fini(&dev_priv->drm);
   1437 
   1438 	intel_gt_driver_unregister(&dev_priv->gt);
   1439 	acpi_video_unregister();
   1440 	intel_opregion_unregister(dev_priv);
   1441 
   1442 	i915_perf_unregister(dev_priv);
   1443 	i915_pmu_unregister(dev_priv);
   1444 
   1445 	i915_teardown_sysfs(dev_priv);
   1446 	drm_dev_unplug(&dev_priv->drm);
   1447 
   1448 	i915_gem_driver_unregister(dev_priv);
   1449 }
   1450 
   1451 static void i915_welcome_messages(struct drm_i915_private *dev_priv)
   1452 {
   1453 	if (drm_debug_enabled(DRM_UT_DRIVER)) {
   1454 		struct drm_printer p = drm_debug_printer("i915 device info:");
   1455 
   1456 		drm_printf(&p, "pciid=0x%04x rev=0x%02x platform=%s (subplatform=0x%x) gen=%i\n",
   1457 			   INTEL_DEVID(dev_priv),
   1458 			   INTEL_REVID(dev_priv),
   1459 			   intel_platform_name(INTEL_INFO(dev_priv)->platform),
   1460 			   intel_subplatform(RUNTIME_INFO(dev_priv),
   1461 					     INTEL_INFO(dev_priv)->platform),
   1462 			   INTEL_GEN(dev_priv));
   1463 
   1464 		intel_device_info_print_static(INTEL_INFO(dev_priv), &p);
   1465 		intel_device_info_print_runtime(RUNTIME_INFO(dev_priv), &p);
   1466 	}
   1467 
   1468 	if (IS_ENABLED(CONFIG_DRM_I915_DEBUG))
   1469 		DRM_INFO("DRM_I915_DEBUG enabled\n");
   1470 	if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
   1471 		DRM_INFO("DRM_I915_DEBUG_GEM enabled\n");
   1472 	if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM))
   1473 		DRM_INFO("DRM_I915_DEBUG_RUNTIME_PM enabled\n");
   1474 }
   1475 
   1476 static struct drm_i915_private *
   1477 i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent)
   1478 {
   1479 	const struct intel_device_info *match_info =
   1480 		(struct intel_device_info *)ent->driver_data;
   1481 	struct intel_device_info *device_info;
   1482 	struct drm_i915_private *i915;
   1483 	int err;
   1484 
   1485 	i915 = kzalloc(sizeof(*i915), GFP_KERNEL);
   1486 	if (!i915)
   1487 		return ERR_PTR(-ENOMEM);
   1488 
   1489 	err = drm_dev_init(&i915->drm, &driver, &pdev->dev);
   1490 	if (err) {
   1491 		kfree(i915);
   1492 		return ERR_PTR(err);
   1493 	}
   1494 
   1495 	i915->drm.dev_private = i915;
   1496 
   1497 	i915->drm.pdev = pdev;
   1498 	pci_set_drvdata(pdev, i915);
   1499 
   1500 	/* Setup the write-once "constant" device info */
   1501 	device_info = mkwrite_device_info(i915);
   1502 	memcpy(device_info, match_info, sizeof(*device_info));
   1503 	RUNTIME_INFO(i915)->device_id = pdev->device;
   1504 
   1505 	BUG_ON(device_info->gen > BITS_PER_TYPE(device_info->gen_mask));
   1506 
   1507 	return i915;
   1508 }
   1509 
   1510 static void i915_driver_destroy(struct drm_i915_private *i915)
   1511 {
   1512 	struct pci_dev *pdev = i915->drm.pdev;
   1513 
   1514 	drm_dev_fini(&i915->drm);
   1515 	kfree(i915);
   1516 
   1517 	/* And make sure we never chase our dangling pointer from pci_dev */
   1518 	pci_set_drvdata(pdev, NULL);
   1519 }
   1520 
   1521 /**
   1522  * i915_driver_probe - setup chip and create an initial config
   1523  * @pdev: PCI device
   1524  * @ent: matching PCI ID entry
   1525  *
   1526  * The driver probe routine has to do several things:
   1527  *   - drive output discovery via intel_modeset_init()
   1528  *   - initialize the memory manager
   1529  *   - allocate initial config memory
   1530  *   - setup the DRM framebuffer with the allocated memory
   1531  */
   1532 int i915_driver_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
   1533 {
   1534 	const struct intel_device_info *match_info =
   1535 		(struct intel_device_info *)ent->driver_data;
   1536 	struct drm_i915_private *dev_priv;
   1537 	int ret;
   1538 
   1539 	dev_priv = i915_driver_create(pdev, ent);
   1540 	if (IS_ERR(dev_priv))
   1541 		return PTR_ERR(dev_priv);
   1542 
   1543 	/* Disable nuclear pageflip by default on pre-ILK */
   1544 	if (!i915_modparams.nuclear_pageflip && match_info->gen < 5)
   1545 		dev_priv->drm.driver_features &= ~DRIVER_ATOMIC;
   1546 
   1547 	/*
   1548 	 * Check if we support fake LMEM -- for now we only unleash this for
   1549 	 * the live selftests(test-and-exit).
   1550 	 */
   1551 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
   1552 	if (IS_ENABLED(CONFIG_DRM_I915_UNSTABLE_FAKE_LMEM)) {
   1553 		if (INTEL_GEN(dev_priv) >= 9 && i915_selftest.live < 0 &&
   1554 		    i915_modparams.fake_lmem_start) {
   1555 			mkwrite_device_info(dev_priv)->memory_regions =
   1556 				REGION_SMEM | REGION_LMEM | REGION_STOLEN;
   1557 			mkwrite_device_info(dev_priv)->is_dgfx = true;
   1558 			GEM_BUG_ON(!HAS_LMEM(dev_priv));
   1559 			GEM_BUG_ON(!IS_DGFX(dev_priv));
   1560 		}
   1561 	}
   1562 #endif
   1563 
   1564 #ifndef __NetBSD__		/* XXX done for us */
   1565 	ret = pci_enable_device(pdev);
   1566 	if (ret)
   1567 		goto out_fini;
   1568 #endif
   1569 
   1570 	ret = i915_driver_early_probe(dev_priv);
   1571 	if (ret < 0)
   1572 		goto out_pci_disable;
   1573 
   1574 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
   1575 
   1576 	i915_detect_vgpu(dev_priv);
   1577 
   1578 	ret = i915_driver_mmio_probe(dev_priv);
   1579 	if (ret < 0)
   1580 		goto out_runtime_pm_put;
   1581 
   1582 	ret = i915_driver_hw_probe(dev_priv);
   1583 	if (ret < 0)
   1584 		goto out_cleanup_mmio;
   1585 
   1586 	ret = i915_driver_modeset_probe(dev_priv);
   1587 	if (ret < 0)
   1588 		goto out_cleanup_hw;
   1589 
   1590 	i915_driver_register(dev_priv);
   1591 
   1592 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
   1593 
   1594 	i915_welcome_messages(dev_priv);
   1595 
   1596 	return 0;
   1597 
   1598 out_cleanup_hw:
   1599 	i915_driver_hw_remove(dev_priv);
   1600 	intel_memory_regions_driver_release(dev_priv);
   1601 	i915_ggtt_driver_release(dev_priv);
   1602 out_cleanup_mmio:
   1603 	i915_driver_mmio_release(dev_priv);
   1604 out_runtime_pm_put:
   1605 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
   1606 	i915_driver_late_release(dev_priv);
   1607 out_pci_disable:
   1608 #ifndef __NetBSD__
   1609 	pci_disable_device(pdev);
   1610 out_fini:
   1611 	i915_probe_error(dev_priv, "Device initialization failed (%d)\n", ret);
   1612 	i915_driver_destroy(dev_priv);
   1613 #endif
   1614 	return ret;
   1615 }
   1616 
   1617 void i915_driver_remove(struct drm_i915_private *i915)
   1618 {
   1619 	disable_rpm_wakeref_asserts(&i915->runtime_pm);
   1620 
   1621 	i915_driver_unregister(i915);
   1622 
   1623 	/*
   1624 	 * After unregistering the device to prevent any new users, cancel
   1625 	 * all in-flight requests so that we can quickly unbind the active
   1626 	 * resources.
   1627 	 */
   1628 	intel_gt_set_wedged(&i915->gt);
   1629 
   1630 	/* Flush any external code that still may be under the RCU lock */
   1631 	synchronize_rcu();
   1632 
   1633 	i915_gem_suspend(i915);
   1634 
   1635 	drm_atomic_helper_shutdown(&i915->drm);
   1636 
   1637 	intel_gvt_driver_remove(i915);
   1638 
   1639 	i915_driver_modeset_remove(i915);
   1640 
   1641 	i915_reset_error_state(i915);
   1642 	i915_gem_driver_remove(i915);
   1643 
   1644 	intel_power_domains_driver_remove(i915);
   1645 
   1646 	i915_driver_hw_remove(i915);
   1647 
   1648 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
   1649 }
   1650 
   1651 static void i915_driver_release(struct drm_device *dev)
   1652 {
   1653 	struct drm_i915_private *dev_priv = to_i915(dev);
   1654 	struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
   1655 
   1656 	disable_rpm_wakeref_asserts(rpm);
   1657 
   1658 	i915_gem_driver_release(dev_priv);
   1659 
   1660 	intel_memory_regions_driver_release(dev_priv);
   1661 	i915_ggtt_driver_release(dev_priv);
   1662 
   1663 	i915_driver_mmio_release(dev_priv);
   1664 
   1665 	enable_rpm_wakeref_asserts(rpm);
   1666 	intel_runtime_pm_driver_release(rpm);
   1667 
   1668 	i915_driver_late_release(dev_priv);
   1669 	i915_driver_destroy(dev_priv);
   1670 }
   1671 
   1672 static int i915_driver_open(struct drm_device *dev, struct drm_file *file)
   1673 {
   1674 	struct drm_i915_private *i915 = to_i915(dev);
   1675 	int ret;
   1676 
   1677 	ret = i915_gem_open(i915, file);
   1678 	if (ret)
   1679 		return ret;
   1680 
   1681 	return 0;
   1682 }
   1683 
   1684 /**
   1685  * i915_driver_lastclose - clean up after all DRM clients have exited
   1686  * @dev: DRM device
   1687  *
   1688  * Take care of cleaning up after all DRM clients have exited.  In the
   1689  * mode setting case, we want to restore the kernel's initial mode (just
   1690  * in case the last client left us in a bad state).
   1691  *
   1692  * Additionally, in the non-mode setting case, we'll tear down the GTT
   1693  * and DMA structures, since the kernel won't be using them, and clea
   1694  * up any GEM state.
   1695  */
   1696 static void i915_driver_lastclose(struct drm_device *dev)
   1697 {
   1698 	intel_fbdev_restore_mode(dev);
   1699 	vga_switcheroo_process_delayed_switch();
   1700 }
   1701 
   1702 static void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
   1703 {
   1704 	struct drm_i915_file_private *file_priv = file->driver_priv;
   1705 
   1706 	i915_gem_context_close(file);
   1707 	i915_gem_release(dev, file);
   1708 
   1709 	kfree_rcu(file_priv, rcu);
   1710 
   1711 	/* Catch up with all the deferred frees from "this" client */
   1712 	i915_gem_flush_free_objects(to_i915(dev));
   1713 }
   1714 
   1715 static void intel_suspend_encoders(struct drm_i915_private *dev_priv)
   1716 {
   1717 	struct drm_device *dev = &dev_priv->drm;
   1718 	struct intel_encoder *encoder;
   1719 
   1720 	drm_modeset_lock_all(dev);
   1721 	for_each_intel_encoder(dev, encoder)
   1722 		if (encoder->suspend)
   1723 			encoder->suspend(encoder);
   1724 	drm_modeset_unlock_all(dev);
   1725 }
   1726 
   1727 #ifndef __NetBSD__		/* XXX vlv suspend/resume */
   1728 static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
   1729 			      bool rpm_resume);
   1730 static int vlv_suspend_complete(struct drm_i915_private *dev_priv);
   1731 #endif
   1732 
   1733 static bool suspend_to_idle(struct drm_i915_private *dev_priv)
   1734 {
   1735 #if IS_ENABLED(CONFIG_ACPI_SLEEP)
   1736 	if (acpi_target_system_state() < ACPI_STATE_S3)
   1737 		return true;
   1738 #endif
   1739 	return false;
   1740 }
   1741 
   1742 #ifndef __NetBSD__		/* XXX runtime pm */
   1743 static int i915_drm_prepare(struct drm_device *dev)
   1744 {
   1745 	struct drm_i915_private *i915 = to_i915(dev);
   1746 
   1747 	/*
   1748 	 * NB intel_display_suspend() may issue new requests after we've
   1749 	 * ostensibly marked the GPU as ready-to-sleep here. We need to
   1750 	 * split out that work and pull it forward so that after point,
   1751 	 * the GPU is not woken again.
   1752 	 */
   1753 	i915_gem_suspend(i915);
   1754 
   1755 	return 0;
   1756 }
   1757 #endif
   1758 
   1759 int i915_drm_suspend(struct drm_device *dev)
   1760 {
   1761 	struct drm_i915_private *dev_priv = to_i915(dev);
   1762 	struct pci_dev *pdev = dev_priv->drm.pdev;
   1763 	pci_power_t opregion_target_state;
   1764 
   1765 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
   1766 
   1767 	/* We do a lot of poking in a lot of registers, make sure they work
   1768 	 * properly. */
   1769 	intel_power_domains_disable(dev_priv);
   1770 
   1771 	drm_kms_helper_poll_disable(dev);
   1772 
   1773 #ifdef __NetBSD__		/* pmf handles this for us.  */
   1774 	__USE(pdev);
   1775 #else
   1776 	pci_save_state(pdev);
   1777 #endif
   1778 
   1779 	intel_display_suspend(dev);
   1780 
   1781 	intel_dp_mst_suspend(dev_priv);
   1782 
   1783 	intel_runtime_pm_disable_interrupts(dev_priv);
   1784 	intel_hpd_cancel_work(dev_priv);
   1785 
   1786 	intel_suspend_encoders(dev_priv);
   1787 
   1788 	intel_suspend_hw(dev_priv);
   1789 
   1790 	i915_gem_suspend_gtt_mappings(dev_priv);
   1791 
   1792 	i915_save_state(dev_priv);
   1793 
   1794 	opregion_target_state = suspend_to_idle(dev_priv) ? PCI_D1 : PCI_D3cold;
   1795 	intel_opregion_suspend(dev_priv, opregion_target_state);
   1796 
   1797 	intel_fbdev_set_suspend(dev, FBINFO_STATE_SUSPENDED, true);
   1798 
   1799 	dev_priv->suspend_count++;
   1800 
   1801 	intel_csr_ucode_suspend(dev_priv);
   1802 
   1803 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
   1804 
   1805 	return 0;
   1806 }
   1807 
   1808 static enum i915_drm_suspend_mode
   1809 get_suspend_mode(struct drm_i915_private *dev_priv, bool hibernate)
   1810 {
   1811 	if (hibernate)
   1812 		return I915_DRM_SUSPEND_HIBERNATE;
   1813 
   1814 	if (suspend_to_idle(dev_priv))
   1815 		return I915_DRM_SUSPEND_IDLE;
   1816 
   1817 	return I915_DRM_SUSPEND_MEM;
   1818 }
   1819 
   1820 static int i915_drm_suspend_late(struct drm_device *dev, bool hibernation)
   1821 {
   1822 	struct drm_i915_private *dev_priv = to_i915(dev);
   1823 	struct pci_dev *pdev = dev_priv->drm.pdev;
   1824 	struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
   1825 	int ret = 0;
   1826 
   1827 	disable_rpm_wakeref_asserts(rpm);
   1828 
   1829 	i915_gem_suspend_late(dev_priv);
   1830 
   1831 	intel_uncore_suspend(&dev_priv->uncore);
   1832 
   1833 	intel_power_domains_suspend(dev_priv,
   1834 				    get_suspend_mode(dev_priv, hibernation));
   1835 
   1836 	intel_display_power_suspend_late(dev_priv);
   1837 
   1838 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
   1839 #ifdef __NetBSD__
   1840 		ret = 0;
   1841 #else
   1842 		ret = vlv_suspend_complete(dev_priv);
   1843 #endif
   1844 
   1845 	if (ret) {
   1846 		DRM_ERROR("Suspend complete failed: %d\n", ret);
   1847 		intel_power_domains_resume(dev_priv);
   1848 
   1849 		goto out;
   1850 	}
   1851 
   1852 #ifdef __NetBSD__		/* pmf handles this for us.  */
   1853 	__USE(pdev);
   1854 #else
   1855 	pci_disable_device(pdev);
   1856 	/*
   1857 	 * During hibernation on some platforms the BIOS may try to access
   1858 	 * the device even though it's already in D3 and hang the machine. So
   1859 	 * leave the device in D0 on those platforms and hope the BIOS will
   1860 	 * power down the device properly. The issue was seen on multiple old
   1861 	 * GENs with different BIOS vendors, so having an explicit blacklist
   1862 	 * is inpractical; apply the workaround on everything pre GEN6. The
   1863 	 * platforms where the issue was seen:
   1864 	 * Lenovo Thinkpad X301, X61s, X60, T60, X41
   1865 	 * Fujitsu FSC S7110
   1866 	 * Acer Aspire 1830T
   1867 	 */
   1868 	if (!(hibernation && INTEL_GEN(dev_priv) < 6))
   1869 		pci_set_power_state(pdev, PCI_D3hot);
   1870 #endif
   1871 
   1872 out:
   1873 	enable_rpm_wakeref_asserts(rpm);
   1874 	if (!dev_priv->uncore.user_forcewake_count)
   1875 		intel_runtime_pm_driver_release(rpm);
   1876 
   1877 	return ret;
   1878 }
   1879 
   1880 #ifndef __NetBSD__		/* XXX vga switcheroo */
   1881 int i915_suspend_switcheroo(struct drm_i915_private *i915, pm_message_t state)
   1882 {
   1883 	int error;
   1884 
   1885 	if (WARN_ON_ONCE(state.event != PM_EVENT_SUSPEND &&
   1886 			 state.event != PM_EVENT_FREEZE))
   1887 		return -EINVAL;
   1888 
   1889 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
   1890 		return 0;
   1891 
   1892 	error = i915_drm_suspend(&i915->drm);
   1893 	if (error)
   1894 		return error;
   1895 
   1896 	return i915_drm_suspend_late(&i915->drm, false);
   1897 }
   1898 #endif
   1899 
   1900 int i915_drm_resume(struct drm_device *dev)
   1901 {
   1902 	struct drm_i915_private *dev_priv = to_i915(dev);
   1903 	int ret;
   1904 
   1905 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
   1906 
   1907 	sanitize_gpu(dev_priv);
   1908 
   1909 	ret = i915_ggtt_enable_hw(dev_priv);
   1910 	if (ret)
   1911 		DRM_ERROR("failed to re-enable GGTT\n");
   1912 
   1913 	i915_gem_restore_gtt_mappings(dev_priv);
   1914 	i915_gem_restore_fences(&dev_priv->ggtt);
   1915 
   1916 	intel_csr_ucode_resume(dev_priv);
   1917 
   1918 	i915_restore_state(dev_priv);
   1919 	intel_pps_unlock_regs_wa(dev_priv);
   1920 
   1921 	intel_init_pch_refclk(dev_priv);
   1922 
   1923 	/*
   1924 	 * Interrupts have to be enabled before any batches are run. If not the
   1925 	 * GPU will hang. i915_gem_init_hw() will initiate batches to
   1926 	 * update/restore the context.
   1927 	 *
   1928 	 * drm_mode_config_reset() needs AUX interrupts.
   1929 	 *
   1930 	 * Modeset enabling in intel_modeset_init_hw() also needs working
   1931 	 * interrupts.
   1932 	 */
   1933 	intel_runtime_pm_enable_interrupts(dev_priv);
   1934 
   1935 	drm_mode_config_reset(dev);
   1936 
   1937 	i915_gem_resume(dev_priv);
   1938 
   1939 	intel_modeset_init_hw(dev_priv);
   1940 	intel_init_clock_gating(dev_priv);
   1941 
   1942 	spin_lock_irq(&dev_priv->irq_lock);
   1943 	if (dev_priv->display.hpd_irq_setup)
   1944 		dev_priv->display.hpd_irq_setup(dev_priv);
   1945 	spin_unlock_irq(&dev_priv->irq_lock);
   1946 
   1947 	intel_dp_mst_resume(dev_priv);
   1948 
   1949 	intel_display_resume(dev);
   1950 
   1951 	drm_kms_helper_poll_enable(dev);
   1952 
   1953 	/*
   1954 	 * ... but also need to make sure that hotplug processing
   1955 	 * doesn't cause havoc. Like in the driver load code we don't
   1956 	 * bother with the tiny race here where we might lose hotplug
   1957 	 * notifications.
   1958 	 * */
   1959 	intel_hpd_init(dev_priv);
   1960 
   1961 	intel_opregion_resume(dev_priv);
   1962 
   1963 	intel_fbdev_set_suspend(dev, FBINFO_STATE_RUNNING, false);
   1964 
   1965 	intel_power_domains_enable(dev_priv);
   1966 
   1967 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
   1968 
   1969 	return 0;
   1970 }
   1971 
   1972 int i915_drm_resume_early(struct drm_device *dev)
   1973 {
   1974 	struct drm_i915_private *dev_priv = to_i915(dev);
   1975 	struct pci_dev *pdev = dev_priv->drm.pdev;
   1976 	int ret;
   1977 
   1978 	/*
   1979 	 * We have a resume ordering issue with the snd-hda driver also
   1980 	 * requiring our device to be power up. Due to the lack of a
   1981 	 * parent/child relationship we currently solve this with an early
   1982 	 * resume hook.
   1983 	 *
   1984 	 * FIXME: This should be solved with a special hdmi sink device or
   1985 	 * similar so that power domains can be employed.
   1986 	 */
   1987 
   1988 	/*
   1989 	 * Note that we need to set the power state explicitly, since we
   1990 	 * powered off the device during freeze and the PCI core won't power
   1991 	 * it back up for us during thaw. Powering off the device during
   1992 	 * freeze is not a hard requirement though, and during the
   1993 	 * suspend/resume phases the PCI core makes sure we get here with the
   1994 	 * device powered on. So in case we change our freeze logic and keep
   1995 	 * the device powered we can also remove the following set power state
   1996 	 * call.
   1997 	 */
   1998 #ifdef __NetBSD__		/* pmf handles this for us.  */
   1999 	if (0)
   2000 		goto out;
   2001 #else
   2002 	ret = pci_set_power_state(pdev, PCI_D0);
   2003 	if (ret) {
   2004 		DRM_ERROR("failed to set PCI D0 power state (%d)\n", ret);
   2005 		return ret;
   2006 	}
   2007 
   2008 	/*
   2009 	 * Note that pci_enable_device() first enables any parent bridge
   2010 	 * device and only then sets the power state for this device. The
   2011 	 * bridge enabling is a nop though, since bridge devices are resumed
   2012 	 * first. The order of enabling power and enabling the device is
   2013 	 * imposed by the PCI core as described above, so here we preserve the
   2014 	 * same order for the freeze/thaw phases.
   2015 	 *
   2016 	 * TODO: eventually we should remove pci_disable_device() /
   2017 	 * pci_enable_enable_device() from suspend/resume. Due to how they
   2018 	 * depend on the device enable refcount we can't anyway depend on them
   2019 	 * disabling/enabling the device.
   2020 	 */
   2021 	if (pci_enable_device(pdev))
   2022 		return -EIO;
   2023 #endif
   2024 
   2025 	/* XXX pmf probably handles this for us too.  */
   2026 	pci_set_master(dev->pdev);
   2027 
   2028 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
   2029 
   2030 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
   2031 #ifdef __NetBSD__		/* XXX vlv suspend/resume */
   2032 		ret = 0;
   2033 #else
   2034 		ret = vlv_resume_prepare(dev_priv, false);
   2035 #endif
   2036 	if (ret)
   2037 		DRM_ERROR("Resume prepare failed: %d, continuing anyway\n",
   2038 			  ret);
   2039 
   2040 	intel_uncore_resume_early(&dev_priv->uncore);
   2041 
   2042 	intel_gt_check_and_clear_faults(&dev_priv->gt);
   2043 
   2044 	intel_display_power_resume_early(dev_priv);
   2045 
   2046 	intel_power_domains_resume(dev_priv);
   2047 
   2048 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
   2049 
   2050 	i915_rc6_ctx_wa_resume(dev_priv);
   2051 
   2052 	return ret;
   2053 }
   2054 
   2055 #ifndef __NetBSD__		/* XXX vga switcheroo */
   2056 int i915_resume_switcheroo(struct drm_i915_private *i915)
   2057 {
   2058 	int ret;
   2059 
   2060 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
   2061 		return 0;
   2062 
   2063 	ret = i915_drm_resume_early(&i915->drm);
   2064 	if (ret)
   2065 		return ret;
   2066 
   2067 	return i915_drm_resume(&i915->drm);
   2068 }
   2069 
   2070 static int i915_pm_prepare(struct device *kdev)
   2071 {
   2072 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
   2073 
   2074 	if (!i915) {
   2075 		dev_err(kdev, "DRM not initialized, aborting suspend.\n");
   2076 		return -ENODEV;
   2077 	}
   2078 
   2079 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
   2080 		return 0;
   2081 
   2082 	return i915_drm_prepare(&i915->drm);
   2083 }
   2084 #endif
   2085 
   2086 #ifndef __NetBSD__
   2087 static int i915_pm_suspend(struct device *kdev)
   2088 {
   2089 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
   2090 
   2091 	if (!i915) {
   2092 		dev_err(kdev, "DRM not initialized, aborting suspend.\n");
   2093 		return -ENODEV;
   2094 	}
   2095 
   2096 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
   2097 		return 0;
   2098 
   2099 	return i915_drm_suspend(&i915->drm);
   2100 }
   2101 
   2102 static int i915_pm_suspend_late(struct device *kdev)
   2103 {
   2104 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
   2105 
   2106 	/*
   2107 	 * We have a suspend ordering issue with the snd-hda driver also
   2108 	 * requiring our device to be power up. Due to the lack of a
   2109 	 * parent/child relationship we currently solve this with an late
   2110 	 * suspend hook.
   2111 	 *
   2112 	 * FIXME: This should be solved with a special hdmi sink device or
   2113 	 * similar so that power domains can be employed.
   2114 	 */
   2115 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
   2116 		return 0;
   2117 
   2118 	return i915_drm_suspend_late(&i915->drm, false);
   2119 }
   2120 
   2121 static int i915_pm_poweroff_late(struct device *kdev)
   2122 {
   2123 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
   2124 
   2125 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
   2126 		return 0;
   2127 
   2128 	return i915_drm_suspend_late(&i915->drm, true);
   2129 }
   2130 
   2131 static int i915_pm_resume_early(struct device *kdev)
   2132 {
   2133 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
   2134 
   2135 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
   2136 		return 0;
   2137 
   2138 	return i915_drm_resume_early(&i915->drm);
   2139 }
   2140 
   2141 static int i915_pm_resume(struct device *kdev)
   2142 {
   2143 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
   2144 
   2145 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
   2146 		return 0;
   2147 
   2148 	return i915_drm_resume(&i915->drm);
   2149 }
   2150 #endif
   2151 
   2152 /* freeze: before creating the hibernation_image */
   2153 static int i915_pm_freeze(struct device *kdev)
   2154 {
   2155 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
   2156 	int ret;
   2157 
   2158 	if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) {
   2159 		ret = i915_drm_suspend(&i915->drm);
   2160 		if (ret)
   2161 			return ret;
   2162 	}
   2163 
   2164 	ret = i915_gem_freeze(i915);
   2165 	if (ret)
   2166 		return ret;
   2167 
   2168 	return 0;
   2169 }
   2170 
   2171 static int i915_pm_freeze_late(struct device *kdev)
   2172 {
   2173 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
   2174 	int ret;
   2175 
   2176 	if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) {
   2177 		ret = i915_drm_suspend_late(&i915->drm, true);
   2178 		if (ret)
   2179 			return ret;
   2180 	}
   2181 
   2182 	ret = i915_gem_freeze_late(i915);
   2183 	if (ret)
   2184 		return ret;
   2185 
   2186 	return 0;
   2187 }
   2188 
   2189 /* thaw: called after creating the hibernation image, but before turning off. */
   2190 static int i915_pm_thaw_early(struct device *kdev)
   2191 {
   2192 	return i915_pm_resume_early(kdev);
   2193 }
   2194 
   2195 static int i915_pm_thaw(struct device *kdev)
   2196 {
   2197 	return i915_pm_resume(kdev);
   2198 }
   2199 
   2200 /* restore: called after loading the hibernation image. */
   2201 static int i915_pm_restore_early(struct device *kdev)
   2202 {
   2203 	return i915_pm_resume_early(kdev);
   2204 }
   2205 
   2206 static int i915_pm_restore(struct device *kdev)
   2207 {
   2208 	return i915_pm_resume(kdev);
   2209 }
   2210 
   2211 /*
   2212  * Save all Gunit registers that may be lost after a D3 and a subsequent
   2213  * S0i[R123] transition. The list of registers needing a save/restore is
   2214  * defined in the VLV2_S0IXRegs document. This documents marks all Gunit
   2215  * registers in the following way:
   2216  * - Driver: saved/restored by the driver
   2217  * - Punit : saved/restored by the Punit firmware
   2218  * - No, w/o marking: no need to save/restore, since the register is R/O or
   2219  *                    used internally by the HW in a way that doesn't depend
   2220  *                    keeping the content across a suspend/resume.
   2221  * - Debug : used for debugging
   2222  *
   2223  * We save/restore all registers marked with 'Driver', with the following
   2224  * exceptions:
   2225  * - Registers out of use, including also registers marked with 'Debug'.
   2226  *   These have no effect on the driver's operation, so we don't save/restore
   2227  *   them to reduce the overhead.
   2228  * - Registers that are fully setup by an initialization function called from
   2229  *   the resume path. For example many clock gating and RPS/RC6 registers.
   2230  * - Registers that provide the right functionality with their reset defaults.
   2231  *
   2232  * TODO: Except for registers that based on the above 3 criteria can be safely
   2233  * ignored, we save/restore all others, practically treating the HW context as
   2234  * a black-box for the driver. Further investigation is needed to reduce the
   2235  * saved/restored registers even further, by following the same 3 criteria.
   2236  */
   2237 static void vlv_save_gunit_s0ix_state(struct drm_i915_private *dev_priv)
   2238 {
   2239 	struct vlv_s0ix_state *s = dev_priv->vlv_s0ix_state;
   2240 	int i;
   2241 
   2242 	if (!s)
   2243 		return;
   2244 
   2245 	/* GAM 0x4000-0x4770 */
   2246 	s->wr_watermark		= I915_READ(GEN7_WR_WATERMARK);
   2247 	s->gfx_prio_ctrl	= I915_READ(GEN7_GFX_PRIO_CTRL);
   2248 	s->arb_mode		= I915_READ(ARB_MODE);
   2249 	s->gfx_pend_tlb0	= I915_READ(GEN7_GFX_PEND_TLB0);
   2250 	s->gfx_pend_tlb1	= I915_READ(GEN7_GFX_PEND_TLB1);
   2251 
   2252 	for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
   2253 		s->lra_limits[i] = I915_READ(GEN7_LRA_LIMITS(i));
   2254 
   2255 	s->media_max_req_count	= I915_READ(GEN7_MEDIA_MAX_REQ_COUNT);
   2256 	s->gfx_max_req_count	= I915_READ(GEN7_GFX_MAX_REQ_COUNT);
   2257 
   2258 	s->render_hwsp		= I915_READ(RENDER_HWS_PGA_GEN7);
   2259 	s->ecochk		= I915_READ(GAM_ECOCHK);
   2260 	s->bsd_hwsp		= I915_READ(BSD_HWS_PGA_GEN7);
   2261 	s->blt_hwsp		= I915_READ(BLT_HWS_PGA_GEN7);
   2262 
   2263 	s->tlb_rd_addr		= I915_READ(GEN7_TLB_RD_ADDR);
   2264 
   2265 	/* MBC 0x9024-0x91D0, 0x8500 */
   2266 	s->g3dctl		= I915_READ(VLV_G3DCTL);
   2267 	s->gsckgctl		= I915_READ(VLV_GSCKGCTL);
   2268 	s->mbctl		= I915_READ(GEN6_MBCTL);
   2269 
   2270 	/* GCP 0x9400-0x9424, 0x8100-0x810C */
   2271 	s->ucgctl1		= I915_READ(GEN6_UCGCTL1);
   2272 	s->ucgctl3		= I915_READ(GEN6_UCGCTL3);
   2273 	s->rcgctl1		= I915_READ(GEN6_RCGCTL1);
   2274 	s->rcgctl2		= I915_READ(GEN6_RCGCTL2);
   2275 	s->rstctl		= I915_READ(GEN6_RSTCTL);
   2276 	s->misccpctl		= I915_READ(GEN7_MISCCPCTL);
   2277 
   2278 	/* GPM 0xA000-0xAA84, 0x8000-0x80FC */
   2279 	s->gfxpause		= I915_READ(GEN6_GFXPAUSE);
   2280 	s->rpdeuhwtc		= I915_READ(GEN6_RPDEUHWTC);
   2281 	s->rpdeuc		= I915_READ(GEN6_RPDEUC);
   2282 	s->ecobus		= I915_READ(ECOBUS);
   2283 	s->pwrdwnupctl		= I915_READ(VLV_PWRDWNUPCTL);
   2284 	s->rp_down_timeout	= I915_READ(GEN6_RP_DOWN_TIMEOUT);
   2285 	s->rp_deucsw		= I915_READ(GEN6_RPDEUCSW);
   2286 	s->rcubmabdtmr		= I915_READ(GEN6_RCUBMABDTMR);
   2287 	s->rcedata		= I915_READ(VLV_RCEDATA);
   2288 	s->spare2gh		= I915_READ(VLV_SPAREG2H);
   2289 
   2290 	/* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
   2291 	s->gt_imr		= I915_READ(GTIMR);
   2292 	s->gt_ier		= I915_READ(GTIER);
   2293 	s->pm_imr		= I915_READ(GEN6_PMIMR);
   2294 	s->pm_ier		= I915_READ(GEN6_PMIER);
   2295 
   2296 	for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
   2297 		s->gt_scratch[i] = I915_READ(GEN7_GT_SCRATCH(i));
   2298 
   2299 	/* GT SA CZ domain, 0x100000-0x138124 */
   2300 	s->tilectl		= I915_READ(TILECTL);
   2301 	s->gt_fifoctl		= I915_READ(GTFIFOCTL);
   2302 	s->gtlc_wake_ctrl	= I915_READ(VLV_GTLC_WAKE_CTRL);
   2303 	s->gtlc_survive		= I915_READ(VLV_GTLC_SURVIVABILITY_REG);
   2304 	s->pmwgicz		= I915_READ(VLV_PMWGICZ);
   2305 
   2306 	/* Gunit-Display CZ domain, 0x182028-0x1821CF */
   2307 	s->gu_ctl0		= I915_READ(VLV_GU_CTL0);
   2308 	s->gu_ctl1		= I915_READ(VLV_GU_CTL1);
   2309 	s->pcbr			= I915_READ(VLV_PCBR);
   2310 	s->clock_gate_dis2	= I915_READ(VLV_GUNIT_CLOCK_GATE2);
   2311 
   2312 	/*
   2313 	 * Not saving any of:
   2314 	 * DFT,		0x9800-0x9EC0
   2315 	 * SARB,	0xB000-0xB1FC
   2316 	 * GAC,		0x5208-0x524C, 0x14000-0x14C000
   2317 	 * PCI CFG
   2318 	 */
   2319 }
   2320 
   2321 static void vlv_restore_gunit_s0ix_state(struct drm_i915_private *dev_priv)
   2322 {
   2323 	struct vlv_s0ix_state *s = dev_priv->vlv_s0ix_state;
   2324 	u32 val;
   2325 	int i;
   2326 
   2327 	if (!s)
   2328 		return;
   2329 
   2330 	/* GAM 0x4000-0x4770 */
   2331 	I915_WRITE(GEN7_WR_WATERMARK,	s->wr_watermark);
   2332 	I915_WRITE(GEN7_GFX_PRIO_CTRL,	s->gfx_prio_ctrl);
   2333 	I915_WRITE(ARB_MODE,		s->arb_mode | (0xffff << 16));
   2334 	I915_WRITE(GEN7_GFX_PEND_TLB0,	s->gfx_pend_tlb0);
   2335 	I915_WRITE(GEN7_GFX_PEND_TLB1,	s->gfx_pend_tlb1);
   2336 
   2337 	for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
   2338 		I915_WRITE(GEN7_LRA_LIMITS(i), s->lra_limits[i]);
   2339 
   2340 	I915_WRITE(GEN7_MEDIA_MAX_REQ_COUNT, s->media_max_req_count);
   2341 	I915_WRITE(GEN7_GFX_MAX_REQ_COUNT, s->gfx_max_req_count);
   2342 
   2343 	I915_WRITE(RENDER_HWS_PGA_GEN7,	s->render_hwsp);
   2344 	I915_WRITE(GAM_ECOCHK,		s->ecochk);
   2345 	I915_WRITE(BSD_HWS_PGA_GEN7,	s->bsd_hwsp);
   2346 	I915_WRITE(BLT_HWS_PGA_GEN7,	s->blt_hwsp);
   2347 
   2348 	I915_WRITE(GEN7_TLB_RD_ADDR,	s->tlb_rd_addr);
   2349 
   2350 	/* MBC 0x9024-0x91D0, 0x8500 */
   2351 	I915_WRITE(VLV_G3DCTL,		s->g3dctl);
   2352 	I915_WRITE(VLV_GSCKGCTL,	s->gsckgctl);
   2353 	I915_WRITE(GEN6_MBCTL,		s->mbctl);
   2354 
   2355 	/* GCP 0x9400-0x9424, 0x8100-0x810C */
   2356 	I915_WRITE(GEN6_UCGCTL1,	s->ucgctl1);
   2357 	I915_WRITE(GEN6_UCGCTL3,	s->ucgctl3);
   2358 	I915_WRITE(GEN6_RCGCTL1,	s->rcgctl1);
   2359 	I915_WRITE(GEN6_RCGCTL2,	s->rcgctl2);
   2360 	I915_WRITE(GEN6_RSTCTL,		s->rstctl);
   2361 	I915_WRITE(GEN7_MISCCPCTL,	s->misccpctl);
   2362 
   2363 	/* GPM 0xA000-0xAA84, 0x8000-0x80FC */
   2364 	I915_WRITE(GEN6_GFXPAUSE,	s->gfxpause);
   2365 	I915_WRITE(GEN6_RPDEUHWTC,	s->rpdeuhwtc);
   2366 	I915_WRITE(GEN6_RPDEUC,		s->rpdeuc);
   2367 	I915_WRITE(ECOBUS,		s->ecobus);
   2368 	I915_WRITE(VLV_PWRDWNUPCTL,	s->pwrdwnupctl);
   2369 	I915_WRITE(GEN6_RP_DOWN_TIMEOUT,s->rp_down_timeout);
   2370 	I915_WRITE(GEN6_RPDEUCSW,	s->rp_deucsw);
   2371 	I915_WRITE(GEN6_RCUBMABDTMR,	s->rcubmabdtmr);
   2372 	I915_WRITE(VLV_RCEDATA,		s->rcedata);
   2373 	I915_WRITE(VLV_SPAREG2H,	s->spare2gh);
   2374 
   2375 	/* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
   2376 	I915_WRITE(GTIMR,		s->gt_imr);
   2377 	I915_WRITE(GTIER,		s->gt_ier);
   2378 	I915_WRITE(GEN6_PMIMR,		s->pm_imr);
   2379 	I915_WRITE(GEN6_PMIER,		s->pm_ier);
   2380 
   2381 	for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
   2382 		I915_WRITE(GEN7_GT_SCRATCH(i), s->gt_scratch[i]);
   2383 
   2384 	/* GT SA CZ domain, 0x100000-0x138124 */
   2385 	I915_WRITE(TILECTL,			s->tilectl);
   2386 	I915_WRITE(GTFIFOCTL,			s->gt_fifoctl);
   2387 	/*
   2388 	 * Preserve the GT allow wake and GFX force clock bit, they are not
   2389 	 * be restored, as they are used to control the s0ix suspend/resume
   2390 	 * sequence by the caller.
   2391 	 */
   2392 	val = I915_READ(VLV_GTLC_WAKE_CTRL);
   2393 	val &= VLV_GTLC_ALLOWWAKEREQ;
   2394 	val |= s->gtlc_wake_ctrl & ~VLV_GTLC_ALLOWWAKEREQ;
   2395 	I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
   2396 
   2397 	val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
   2398 	val &= VLV_GFX_CLK_FORCE_ON_BIT;
   2399 	val |= s->gtlc_survive & ~VLV_GFX_CLK_FORCE_ON_BIT;
   2400 	I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
   2401 
   2402 	I915_WRITE(VLV_PMWGICZ,			s->pmwgicz);
   2403 
   2404 	/* Gunit-Display CZ domain, 0x182028-0x1821CF */
   2405 	I915_WRITE(VLV_GU_CTL0,			s->gu_ctl0);
   2406 	I915_WRITE(VLV_GU_CTL1,			s->gu_ctl1);
   2407 	I915_WRITE(VLV_PCBR,			s->pcbr);
   2408 	I915_WRITE(VLV_GUNIT_CLOCK_GATE2,	s->clock_gate_dis2);
   2409 }
   2410 
   2411 static int vlv_wait_for_pw_status(struct drm_i915_private *i915,
   2412 				  u32 mask, u32 val)
   2413 {
   2414 	i915_reg_t reg = VLV_GTLC_PW_STATUS;
   2415 	u32 reg_value;
   2416 	int ret;
   2417 
   2418 	/* The HW does not like us polling for PW_STATUS frequently, so
   2419 	 * use the sleeping loop rather than risk the busy spin within
   2420 	 * intel_wait_for_register().
   2421 	 *
   2422 	 * Transitioning between RC6 states should be at most 2ms (see
   2423 	 * valleyview_enable_rps) so use a 3ms timeout.
   2424 	 */
   2425 	ret = wait_for(((reg_value =
   2426 			 intel_uncore_read_notrace(&i915->uncore, reg)) & mask)
   2427 		       == val, 3);
   2428 
   2429 	/* just trace the final value */
   2430 	trace_i915_reg_rw(false, reg, reg_value, sizeof(reg_value), true);
   2431 
   2432 	return ret;
   2433 }
   2434 
   2435 int vlv_force_gfx_clock(struct drm_i915_private *dev_priv, bool force_on)
   2436 {
   2437 	u32 val;
   2438 	int err;
   2439 
   2440 	val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
   2441 	val &= ~VLV_GFX_CLK_FORCE_ON_BIT;
   2442 	if (force_on)
   2443 		val |= VLV_GFX_CLK_FORCE_ON_BIT;
   2444 	I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
   2445 
   2446 	if (!force_on)
   2447 		return 0;
   2448 
   2449 	err = intel_wait_for_register(&dev_priv->uncore,
   2450 				      VLV_GTLC_SURVIVABILITY_REG,
   2451 				      VLV_GFX_CLK_STATUS_BIT,
   2452 				      VLV_GFX_CLK_STATUS_BIT,
   2453 				      20);
   2454 	if (err)
   2455 		DRM_ERROR("timeout waiting for GFX clock force-on (%08x)\n",
   2456 			  I915_READ(VLV_GTLC_SURVIVABILITY_REG));
   2457 
   2458 	return err;
   2459 }
   2460 
   2461 static int vlv_allow_gt_wake(struct drm_i915_private *dev_priv, bool allow)
   2462 {
   2463 	u32 mask;
   2464 	u32 val;
   2465 	int err;
   2466 
   2467 	val = I915_READ(VLV_GTLC_WAKE_CTRL);
   2468 	val &= ~VLV_GTLC_ALLOWWAKEREQ;
   2469 	if (allow)
   2470 		val |= VLV_GTLC_ALLOWWAKEREQ;
   2471 	I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
   2472 	POSTING_READ(VLV_GTLC_WAKE_CTRL);
   2473 
   2474 	mask = VLV_GTLC_ALLOWWAKEACK;
   2475 	val = allow ? mask : 0;
   2476 
   2477 	err = vlv_wait_for_pw_status(dev_priv, mask, val);
   2478 	if (err)
   2479 		DRM_ERROR("timeout disabling GT waking\n");
   2480 
   2481 	return err;
   2482 }
   2483 
   2484 static void vlv_wait_for_gt_wells(struct drm_i915_private *dev_priv,
   2485 				  bool wait_for_on)
   2486 {
   2487 	u32 mask;
   2488 	u32 val;
   2489 
   2490 	mask = VLV_GTLC_PW_MEDIA_STATUS_MASK | VLV_GTLC_PW_RENDER_STATUS_MASK;
   2491 	val = wait_for_on ? mask : 0;
   2492 
   2493 	/*
   2494 	 * RC6 transitioning can be delayed up to 2 msec (see
   2495 	 * valleyview_enable_rps), use 3 msec for safety.
   2496 	 *
   2497 	 * This can fail to turn off the rc6 if the GPU is stuck after a failed
   2498 	 * reset and we are trying to force the machine to sleep.
   2499 	 */
   2500 	if (vlv_wait_for_pw_status(dev_priv, mask, val))
   2501 		DRM_DEBUG_DRIVER("timeout waiting for GT wells to go %s\n",
   2502 				 onoff(wait_for_on));
   2503 }
   2504 
   2505 static void vlv_check_no_gt_access(struct drm_i915_private *dev_priv)
   2506 {
   2507 	if (!(I915_READ(VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEERR))
   2508 		return;
   2509 
   2510 	DRM_DEBUG_DRIVER("GT register access while GT waking disabled\n");
   2511 	I915_WRITE(VLV_GTLC_PW_STATUS, VLV_GTLC_ALLOWWAKEERR);
   2512 }
   2513 
   2514 static int vlv_suspend_complete(struct drm_i915_private *dev_priv)
   2515 {
   2516 	u32 mask;
   2517 	int err;
   2518 
   2519 	/*
   2520 	 * Bspec defines the following GT well on flags as debug only, so
   2521 	 * don't treat them as hard failures.
   2522 	 */
   2523 	vlv_wait_for_gt_wells(dev_priv, false);
   2524 
   2525 	mask = VLV_GTLC_RENDER_CTX_EXISTS | VLV_GTLC_MEDIA_CTX_EXISTS;
   2526 	WARN_ON((I915_READ(VLV_GTLC_WAKE_CTRL) & mask) != mask);
   2527 
   2528 	vlv_check_no_gt_access(dev_priv);
   2529 
   2530 	err = vlv_force_gfx_clock(dev_priv, true);
   2531 	if (err)
   2532 		goto err1;
   2533 
   2534 	err = vlv_allow_gt_wake(dev_priv, false);
   2535 	if (err)
   2536 		goto err2;
   2537 
   2538 	vlv_save_gunit_s0ix_state(dev_priv);
   2539 
   2540 	err = vlv_force_gfx_clock(dev_priv, false);
   2541 	if (err)
   2542 		goto err2;
   2543 
   2544 	return 0;
   2545 
   2546 err2:
   2547 	/* For safety always re-enable waking and disable gfx clock forcing */
   2548 	vlv_allow_gt_wake(dev_priv, true);
   2549 err1:
   2550 	vlv_force_gfx_clock(dev_priv, false);
   2551 
   2552 	return err;
   2553 }
   2554 
   2555 static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
   2556 				bool rpm_resume)
   2557 {
   2558 	int err;
   2559 	int ret;
   2560 
   2561 	/*
   2562 	 * If any of the steps fail just try to continue, that's the best we
   2563 	 * can do at this point. Return the first error code (which will also
   2564 	 * leave RPM permanently disabled).
   2565 	 */
   2566 	ret = vlv_force_gfx_clock(dev_priv, true);
   2567 
   2568 	vlv_restore_gunit_s0ix_state(dev_priv);
   2569 
   2570 	err = vlv_allow_gt_wake(dev_priv, true);
   2571 	if (!ret)
   2572 		ret = err;
   2573 
   2574 	err = vlv_force_gfx_clock(dev_priv, false);
   2575 	if (!ret)
   2576 		ret = err;
   2577 
   2578 	vlv_check_no_gt_access(dev_priv);
   2579 
   2580 	if (rpm_resume)
   2581 		intel_init_clock_gating(dev_priv);
   2582 
   2583 	return ret;
   2584 }
   2585 
   2586 #ifndef __NetBSD__		/* XXX runtime pm */
   2587 static int intel_runtime_suspend(struct device *kdev)
   2588 {
   2589 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
   2590 	struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
   2591 	int ret = 0;
   2592 
   2593 	if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev_priv)))
   2594 		return -ENODEV;
   2595 
   2596 	DRM_DEBUG_KMS("Suspending device\n");
   2597 
   2598 	disable_rpm_wakeref_asserts(rpm);
   2599 
   2600 	/*
   2601 	 * We are safe here against re-faults, since the fault handler takes
   2602 	 * an RPM reference.
   2603 	 */
   2604 	i915_gem_runtime_suspend(dev_priv);
   2605 
   2606 	intel_gt_runtime_suspend(&dev_priv->gt);
   2607 
   2608 	intel_runtime_pm_disable_interrupts(dev_priv);
   2609 
   2610 	intel_uncore_suspend(&dev_priv->uncore);
   2611 
   2612 	intel_display_power_suspend(dev_priv);
   2613 
   2614 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
   2615 #ifndef __NetBSD__		/* XXX vlv suspend/resume */
   2616 		ret = vlv_suspend_complete(dev_priv);
   2617 #endif
   2618 
   2619 	if (ret) {
   2620 		DRM_ERROR("Runtime suspend failed, disabling it (%d)\n", ret);
   2621 		intel_uncore_runtime_resume(&dev_priv->uncore);
   2622 
   2623 		intel_runtime_pm_enable_interrupts(dev_priv);
   2624 
   2625 		intel_gt_runtime_resume(&dev_priv->gt);
   2626 
   2627 		i915_gem_restore_fences(&dev_priv->ggtt);
   2628 
   2629 		enable_rpm_wakeref_asserts(rpm);
   2630 
   2631 		return ret;
   2632 	}
   2633 
   2634 	enable_rpm_wakeref_asserts(rpm);
   2635 	intel_runtime_pm_driver_release(rpm);
   2636 
   2637 	if (intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore))
   2638 		DRM_ERROR("Unclaimed access detected prior to suspending\n");
   2639 
   2640 	rpm->suspended = true;
   2641 
   2642 	/*
   2643 	 * FIXME: We really should find a document that references the arguments
   2644 	 * used below!
   2645 	 */
   2646 	if (IS_BROADWELL(dev_priv)) {
   2647 		/*
   2648 		 * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop
   2649 		 * being detected, and the call we do at intel_runtime_resume()
   2650 		 * won't be able to restore them. Since PCI_D3hot matches the
   2651 		 * actual specification and appears to be working, use it.
   2652 		 */
   2653 		intel_opregion_notify_adapter(dev_priv, PCI_D3hot);
   2654 	} else {
   2655 		/*
   2656 		 * current versions of firmware which depend on this opregion
   2657 		 * notification have repurposed the D1 definition to mean
   2658 		 * "runtime suspended" vs. what you would normally expect (D3)
   2659 		 * to distinguish it from notifications that might be sent via
   2660 		 * the suspend path.
   2661 		 */
   2662 		intel_opregion_notify_adapter(dev_priv, PCI_D1);
   2663 	}
   2664 
   2665 	assert_forcewakes_inactive(&dev_priv->uncore);
   2666 
   2667 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
   2668 		intel_hpd_poll_init(dev_priv);
   2669 
   2670 	DRM_DEBUG_KMS("Device suspended\n");
   2671 	return 0;
   2672 }
   2673 
   2674 static int intel_runtime_resume(struct device *kdev)
   2675 {
   2676 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
   2677 	struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
   2678 	int ret = 0;
   2679 
   2680 	if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev_priv)))
   2681 		return -ENODEV;
   2682 
   2683 	DRM_DEBUG_KMS("Resuming device\n");
   2684 
   2685 	WARN_ON_ONCE(atomic_read(&rpm->wakeref_count));
   2686 	disable_rpm_wakeref_asserts(rpm);
   2687 
   2688 	intel_opregion_notify_adapter(dev_priv, PCI_D0);
   2689 	rpm->suspended = false;
   2690 	if (intel_uncore_unclaimed_mmio(&dev_priv->uncore))
   2691 		DRM_DEBUG_DRIVER("Unclaimed access during suspend, bios?\n");
   2692 
   2693 	intel_display_power_resume(dev_priv);
   2694 
   2695 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
   2696 		ret = vlv_resume_prepare(dev_priv, true);
   2697 
   2698 	intel_uncore_runtime_resume(&dev_priv->uncore);
   2699 
   2700 	intel_runtime_pm_enable_interrupts(dev_priv);
   2701 
   2702 	/*
   2703 	 * No point of rolling back things in case of an error, as the best
   2704 	 * we can do is to hope that things will still work (and disable RPM).
   2705 	 */
   2706 	intel_gt_runtime_resume(&dev_priv->gt);
   2707 	i915_gem_restore_fences(&dev_priv->ggtt);
   2708 
   2709 	/*
   2710 	 * On VLV/CHV display interrupts are part of the display
   2711 	 * power well, so hpd is reinitialized from there. For
   2712 	 * everyone else do it here.
   2713 	 */
   2714 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
   2715 		intel_hpd_init(dev_priv);
   2716 
   2717 	intel_enable_ipc(dev_priv);
   2718 
   2719 	enable_rpm_wakeref_asserts(rpm);
   2720 
   2721 	if (ret)
   2722 		DRM_ERROR("Runtime resume failed, disabling it (%d)\n", ret);
   2723 	else
   2724 		DRM_DEBUG_KMS("Device resumed\n");
   2725 
   2726 	return ret;
   2727 }
   2728 #endif
   2729 
   2730 #ifndef __NetBSD__
   2731 const struct dev_pm_ops i915_pm_ops = {
   2732 	/*
   2733 	 * S0ix (via system suspend) and S3 event handlers [PMSG_SUSPEND,
   2734 	 * PMSG_RESUME]
   2735 	 */
   2736 	.prepare = i915_pm_prepare,
   2737 	.suspend = i915_pm_suspend,
   2738 	.suspend_late = i915_pm_suspend_late,
   2739 	.resume_early = i915_pm_resume_early,
   2740 	.resume = i915_pm_resume,
   2741 
   2742 	/*
   2743 	 * S4 event handlers
   2744 	 * @freeze, @freeze_late    : called (1) before creating the
   2745 	 *                            hibernation image [PMSG_FREEZE] and
   2746 	 *                            (2) after rebooting, before restoring
   2747 	 *                            the image [PMSG_QUIESCE]
   2748 	 * @thaw, @thaw_early       : called (1) after creating the hibernation
   2749 	 *                            image, before writing it [PMSG_THAW]
   2750 	 *                            and (2) after failing to create or
   2751 	 *                            restore the image [PMSG_RECOVER]
   2752 	 * @poweroff, @poweroff_late: called after writing the hibernation
   2753 	 *                            image, before rebooting [PMSG_HIBERNATE]
   2754 	 * @restore, @restore_early : called after rebooting and restoring the
   2755 	 *                            hibernation image [PMSG_RESTORE]
   2756 	 */
   2757 	.freeze = i915_pm_freeze,
   2758 	.freeze_late = i915_pm_freeze_late,
   2759 	.thaw_early = i915_pm_thaw_early,
   2760 	.thaw = i915_pm_thaw,
   2761 	.poweroff = i915_pm_suspend,
   2762 	.poweroff_late = i915_pm_poweroff_late,
   2763 	.restore_early = i915_pm_restore_early,
   2764 	.restore = i915_pm_restore,
   2765 
   2766 	/* S0ix (via runtime suspend) event handlers */
   2767 	.runtime_suspend = intel_runtime_suspend,
   2768 	.runtime_resume = intel_runtime_resume,
   2769 };
   2770 
   2771 static const struct file_operations i915_driver_fops = {
   2772 	.owner = THIS_MODULE,
   2773 	.open = drm_open,
   2774 	.release = drm_release,
   2775 	.unlocked_ioctl = drm_ioctl,
   2776 	.mmap = i915_gem_mmap,
   2777 	.poll = drm_poll,
   2778 	.read = drm_read,
   2779 	.compat_ioctl = i915_compat_ioctl,
   2780 	.llseek = noop_llseek,
   2781 };
   2782 #endif	/* defined(__NetBSD__) */
   2783 
   2784 static int
   2785 i915_gem_reject_pin_ioctl(struct drm_device *dev, void *data,
   2786 			  struct drm_file *file)
   2787 {
   2788 	return -ENODEV;
   2789 }
   2790 
   2791 static const struct drm_ioctl_desc i915_ioctls[] = {
   2792 	DRM_IOCTL_DEF_DRV(I915_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
   2793 	DRM_IOCTL_DEF_DRV(I915_FLUSH, drm_noop, DRM_AUTH),
   2794 	DRM_IOCTL_DEF_DRV(I915_FLIP, drm_noop, DRM_AUTH),
   2795 	DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, drm_noop, DRM_AUTH),
   2796 	DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, drm_noop, DRM_AUTH),
   2797 	DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, drm_noop, DRM_AUTH),
   2798 	DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam_ioctl, DRM_RENDER_ALLOW),
   2799 	DRM_IOCTL_DEF_DRV(I915_SETPARAM, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
   2800 	DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
   2801 	DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
   2802 	DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
   2803 	DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, drm_noop, DRM_AUTH),
   2804 	DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP,  drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
   2805 	DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE,  drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
   2806 	DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE,  drm_noop, DRM_AUTH),
   2807 	DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, drm_noop, DRM_AUTH),
   2808 	DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
   2809 	DRM_IOCTL_DEF_DRV(I915_GEM_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
   2810 	DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer_ioctl, DRM_AUTH),
   2811 	DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2_WR, i915_gem_execbuffer2_ioctl, DRM_RENDER_ALLOW),
   2812 	DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
   2813 	DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
   2814 	DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_RENDER_ALLOW),
   2815 	DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_RENDER_ALLOW),
   2816 	DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_RENDER_ALLOW),
   2817 	DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_RENDER_ALLOW),
   2818 	DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
   2819 	DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
   2820 	DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_RENDER_ALLOW),
   2821 	DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW),
   2822 	DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW),
   2823 	DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW),
   2824 	DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_OFFSET, i915_gem_mmap_offset_ioctl, DRM_RENDER_ALLOW),
   2825 	DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW),
   2826 	DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW),
   2827 	DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling_ioctl, DRM_RENDER_ALLOW),
   2828 	DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling_ioctl, DRM_RENDER_ALLOW),
   2829 	DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_RENDER_ALLOW),
   2830 	DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id_ioctl, 0),
   2831 	DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_RENDER_ALLOW),
   2832 	DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image_ioctl, DRM_MASTER),
   2833 	DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs_ioctl, DRM_MASTER),
   2834 	DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey_ioctl, DRM_MASTER),
   2835 	DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, drm_noop, DRM_MASTER),
   2836 	DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_RENDER_ALLOW),
   2837 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE_EXT, i915_gem_context_create_ioctl, DRM_RENDER_ALLOW),
   2838 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_RENDER_ALLOW),
   2839 	DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_RENDER_ALLOW),
   2840 	DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_gem_context_reset_stats_ioctl, DRM_RENDER_ALLOW),
   2841 	DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
   2842 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
   2843 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
   2844 	DRM_IOCTL_DEF_DRV(I915_PERF_OPEN, i915_perf_open_ioctl, DRM_RENDER_ALLOW),
   2845 	DRM_IOCTL_DEF_DRV(I915_PERF_ADD_CONFIG, i915_perf_add_config_ioctl, DRM_RENDER_ALLOW),
   2846 	DRM_IOCTL_DEF_DRV(I915_PERF_REMOVE_CONFIG, i915_perf_remove_config_ioctl, DRM_RENDER_ALLOW),
   2847 	DRM_IOCTL_DEF_DRV(I915_QUERY, i915_query_ioctl, DRM_RENDER_ALLOW),
   2848 	DRM_IOCTL_DEF_DRV(I915_GEM_VM_CREATE, i915_gem_vm_create_ioctl, DRM_RENDER_ALLOW),
   2849 	DRM_IOCTL_DEF_DRV(I915_GEM_VM_DESTROY, i915_gem_vm_destroy_ioctl, DRM_RENDER_ALLOW),
   2850 };
   2851 
   2852 static struct drm_driver driver = {
   2853 	/* Don't use MTRRs here; the Xserver or userspace app should
   2854 	 * deal with them for Intel hardware.
   2855 	 */
   2856 	.driver_features =
   2857 	    DRIVER_GEM |
   2858 	    DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_SYNCOBJ,
   2859 	.release = i915_driver_release,
   2860 	.open = i915_driver_open,
   2861 	.lastclose = i915_driver_lastclose,
   2862 	.postclose = i915_driver_postclose,
   2863 
   2864 	.gem_close_object = i915_gem_close_object,
   2865 	.gem_free_object_unlocked = i915_gem_free_object,
   2866 #ifdef __NetBSD__
   2867 	.request_irq = drm_pci_request_irq,
   2868 	.free_irq = drm_pci_free_irq,
   2869 
   2870 	/* XXX Not clear the `or legacy' part is important here.  */
   2871 	.mmap_object = &drm_gem_mmap_object,
   2872 	.gem_uvm_ops = &i915_gem_uvm_ops,
   2873 #endif
   2874 
   2875 
   2876 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
   2877 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
   2878 	.gem_prime_export = i915_gem_prime_export,
   2879 	.gem_prime_import = i915_gem_prime_import,
   2880 
   2881 	.get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos,
   2882 	.get_scanout_position = i915_get_crtc_scanoutpos,
   2883 
   2884 	.dumb_create = i915_gem_dumb_create,
   2885 	.dumb_map_offset = i915_gem_dumb_mmap_offset,
   2886 
   2887 	.ioctls = i915_ioctls,
   2888 	.num_ioctls = ARRAY_SIZE(i915_ioctls),
   2889 #ifdef __NetBSD__
   2890 	.fops = NULL,
   2891 #else
   2892 	.fops = &i915_driver_fops,
   2893 #endif
   2894 	.name = DRIVER_NAME,
   2895 	.desc = DRIVER_DESC,
   2896 	.date = DRIVER_DATE,
   2897 	.major = DRIVER_MAJOR,
   2898 	.minor = DRIVER_MINOR,
   2899 	.patchlevel = DRIVER_PATCHLEVEL,
   2900 };
   2901 
   2902 #ifdef __NetBSD__
   2903 
   2904 static const struct uvm_pagerops i915_gem_uvm_ops = {
   2905 	.pgo_reference = drm_gem_pager_reference,
   2906 	.pgo_detach = drm_gem_pager_detach,
   2907 	.pgo_fault = i915_gem_fault,
   2908 };
   2909 
   2910 #endif
   2911