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