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