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