Home | History | Annotate | Line # | Download | only in i915
i915_drv.c revision 1.13
      1 /*	$NetBSD: i915_drv.c,v 1.13 2018/08/27 07:57:12 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.13 2018/08/27 07:57:12 riastradh Exp $");
     34 
     35 #include <linux/device.h>
     36 #include <linux/acpi.h>
     37 #include <linux/moduleparam.h>
     38 #include <linux/time.h>
     39 #include <drm/drmP.h>
     40 #include <drm/i915_drm.h>
     41 #include "i915_drv.h"
     42 #include "i915_trace.h"
     43 #include "intel_drv.h"
     44 
     45 #include <linux/console.h>
     46 #include <linux/module.h>
     47 #include <linux/pm_runtime.h>
     48 #include <drm/drm_crtc_helper.h>
     49 
     50 static struct drm_driver driver;
     51 
     52 #ifdef __NetBSD__
     53 /* XXX Kludge to expose this to NetBSD driver attachment goop.  */
     54 struct drm_driver *const i915_drm_driver = &driver;
     55 #endif
     56 
     57 #define GEN_DEFAULT_PIPEOFFSETS \
     58 	.pipe_offsets = { PIPE_A_OFFSET, PIPE_B_OFFSET, \
     59 			  PIPE_C_OFFSET, PIPE_EDP_OFFSET }, \
     60 	.trans_offsets = { TRANSCODER_A_OFFSET, TRANSCODER_B_OFFSET, \
     61 			   TRANSCODER_C_OFFSET, TRANSCODER_EDP_OFFSET }, \
     62 	.palette_offsets = { PALETTE_A_OFFSET, PALETTE_B_OFFSET }
     63 
     64 #define GEN_CHV_PIPEOFFSETS \
     65 	.pipe_offsets = { PIPE_A_OFFSET, PIPE_B_OFFSET, \
     66 			  CHV_PIPE_C_OFFSET }, \
     67 	.trans_offsets = { TRANSCODER_A_OFFSET, TRANSCODER_B_OFFSET, \
     68 			   CHV_TRANSCODER_C_OFFSET, }, \
     69 	.palette_offsets = { PALETTE_A_OFFSET, PALETTE_B_OFFSET, \
     70 			     CHV_PALETTE_C_OFFSET }
     71 
     72 #define CURSOR_OFFSETS \
     73 	.cursor_offsets = { CURSOR_A_OFFSET, CURSOR_B_OFFSET, CHV_CURSOR_C_OFFSET }
     74 
     75 #define IVB_CURSOR_OFFSETS \
     76 	.cursor_offsets = { CURSOR_A_OFFSET, IVB_CURSOR_B_OFFSET, IVB_CURSOR_C_OFFSET }
     77 
     78 static const struct intel_device_info intel_i830_info = {
     79 	.gen = 2, .is_mobile = 1, .cursor_needs_physical = 1, .num_pipes = 2,
     80 	.has_overlay = 1, .overlay_needs_physical = 1,
     81 	.ring_mask = RENDER_RING,
     82 	GEN_DEFAULT_PIPEOFFSETS,
     83 	CURSOR_OFFSETS,
     84 };
     85 
     86 static const struct intel_device_info intel_845g_info = {
     87 	.gen = 2, .num_pipes = 1,
     88 	.has_overlay = 1, .overlay_needs_physical = 1,
     89 	.ring_mask = RENDER_RING,
     90 	GEN_DEFAULT_PIPEOFFSETS,
     91 	CURSOR_OFFSETS,
     92 };
     93 
     94 static const struct intel_device_info intel_i85x_info = {
     95 	.gen = 2, .is_i85x = 1, .is_mobile = 1, .num_pipes = 2,
     96 	.cursor_needs_physical = 1,
     97 	.has_overlay = 1, .overlay_needs_physical = 1,
     98 	.has_fbc = 1,
     99 	.ring_mask = RENDER_RING,
    100 	GEN_DEFAULT_PIPEOFFSETS,
    101 	CURSOR_OFFSETS,
    102 };
    103 
    104 static const struct intel_device_info intel_i865g_info = {
    105 	.gen = 2, .num_pipes = 1,
    106 	.has_overlay = 1, .overlay_needs_physical = 1,
    107 	.ring_mask = RENDER_RING,
    108 	GEN_DEFAULT_PIPEOFFSETS,
    109 	CURSOR_OFFSETS,
    110 };
    111 
    112 static const struct intel_device_info intel_i915g_info = {
    113 	.gen = 3, .is_i915g = 1, .cursor_needs_physical = 1, .num_pipes = 2,
    114 	.has_overlay = 1, .overlay_needs_physical = 1,
    115 	.ring_mask = RENDER_RING,
    116 	GEN_DEFAULT_PIPEOFFSETS,
    117 	CURSOR_OFFSETS,
    118 };
    119 static const struct intel_device_info intel_i915gm_info = {
    120 	.gen = 3, .is_mobile = 1, .num_pipes = 2,
    121 	.cursor_needs_physical = 1,
    122 	.has_overlay = 1, .overlay_needs_physical = 1,
    123 	.supports_tv = 1,
    124 	.has_fbc = 1,
    125 	.ring_mask = RENDER_RING,
    126 	GEN_DEFAULT_PIPEOFFSETS,
    127 	CURSOR_OFFSETS,
    128 };
    129 static const struct intel_device_info intel_i945g_info = {
    130 	.gen = 3, .has_hotplug = 1, .cursor_needs_physical = 1, .num_pipes = 2,
    131 	.has_overlay = 1, .overlay_needs_physical = 1,
    132 	.ring_mask = RENDER_RING,
    133 	GEN_DEFAULT_PIPEOFFSETS,
    134 	CURSOR_OFFSETS,
    135 };
    136 static const struct intel_device_info intel_i945gm_info = {
    137 	.gen = 3, .is_i945gm = 1, .is_mobile = 1, .num_pipes = 2,
    138 	.has_hotplug = 1, .cursor_needs_physical = 1,
    139 	.has_overlay = 1, .overlay_needs_physical = 1,
    140 	.supports_tv = 1,
    141 	.has_fbc = 1,
    142 	.ring_mask = RENDER_RING,
    143 	GEN_DEFAULT_PIPEOFFSETS,
    144 	CURSOR_OFFSETS,
    145 };
    146 
    147 static const struct intel_device_info intel_i965g_info = {
    148 	.gen = 4, .is_broadwater = 1, .num_pipes = 2,
    149 	.has_hotplug = 1,
    150 	.has_overlay = 1,
    151 	.ring_mask = RENDER_RING,
    152 	GEN_DEFAULT_PIPEOFFSETS,
    153 	CURSOR_OFFSETS,
    154 };
    155 
    156 static const struct intel_device_info intel_i965gm_info = {
    157 	.gen = 4, .is_crestline = 1, .num_pipes = 2,
    158 	.is_mobile = 1, .has_fbc = 1, .has_hotplug = 1,
    159 	.has_overlay = 1,
    160 	.supports_tv = 1,
    161 	.ring_mask = RENDER_RING,
    162 	GEN_DEFAULT_PIPEOFFSETS,
    163 	CURSOR_OFFSETS,
    164 };
    165 
    166 static const struct intel_device_info intel_g33_info = {
    167 	.gen = 3, .is_g33 = 1, .num_pipes = 2,
    168 	.need_gfx_hws = 1, .has_hotplug = 1,
    169 	.has_overlay = 1,
    170 	.ring_mask = RENDER_RING,
    171 	GEN_DEFAULT_PIPEOFFSETS,
    172 	CURSOR_OFFSETS,
    173 };
    174 
    175 static const struct intel_device_info intel_g45_info = {
    176 	.gen = 4, .is_g4x = 1, .need_gfx_hws = 1, .num_pipes = 2,
    177 	.has_pipe_cxsr = 1, .has_hotplug = 1,
    178 	.ring_mask = RENDER_RING | BSD_RING,
    179 	GEN_DEFAULT_PIPEOFFSETS,
    180 	CURSOR_OFFSETS,
    181 };
    182 
    183 static const struct intel_device_info intel_gm45_info = {
    184 	.gen = 4, .is_g4x = 1, .num_pipes = 2,
    185 	.is_mobile = 1, .need_gfx_hws = 1, .has_fbc = 1,
    186 	.has_pipe_cxsr = 1, .has_hotplug = 1,
    187 	.supports_tv = 1,
    188 	.ring_mask = RENDER_RING | BSD_RING,
    189 	GEN_DEFAULT_PIPEOFFSETS,
    190 	CURSOR_OFFSETS,
    191 };
    192 
    193 static const struct intel_device_info intel_pineview_info = {
    194 	.gen = 3, .is_g33 = 1, .is_pineview = 1, .is_mobile = 1, .num_pipes = 2,
    195 	.need_gfx_hws = 1, .has_hotplug = 1,
    196 	.has_overlay = 1,
    197 	GEN_DEFAULT_PIPEOFFSETS,
    198 	CURSOR_OFFSETS,
    199 };
    200 
    201 static const struct intel_device_info intel_ironlake_d_info = {
    202 	.gen = 5, .num_pipes = 2,
    203 	.need_gfx_hws = 1, .has_hotplug = 1,
    204 	.ring_mask = RENDER_RING | BSD_RING,
    205 	GEN_DEFAULT_PIPEOFFSETS,
    206 	CURSOR_OFFSETS,
    207 };
    208 
    209 static const struct intel_device_info intel_ironlake_m_info = {
    210 	.gen = 5, .is_mobile = 1, .num_pipes = 2,
    211 	.need_gfx_hws = 1, .has_hotplug = 1,
    212 	.has_fbc = 1,
    213 	.ring_mask = RENDER_RING | BSD_RING,
    214 	GEN_DEFAULT_PIPEOFFSETS,
    215 	CURSOR_OFFSETS,
    216 };
    217 
    218 static const struct intel_device_info intel_sandybridge_d_info = {
    219 	.gen = 6, .num_pipes = 2,
    220 	.need_gfx_hws = 1, .has_hotplug = 1,
    221 	.has_fbc = 1,
    222 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING,
    223 	.has_llc = 1,
    224 	GEN_DEFAULT_PIPEOFFSETS,
    225 	CURSOR_OFFSETS,
    226 };
    227 
    228 static const struct intel_device_info intel_sandybridge_m_info = {
    229 	.gen = 6, .is_mobile = 1, .num_pipes = 2,
    230 	.need_gfx_hws = 1, .has_hotplug = 1,
    231 	.has_fbc = 1,
    232 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING,
    233 	.has_llc = 1,
    234 	GEN_DEFAULT_PIPEOFFSETS,
    235 	CURSOR_OFFSETS,
    236 };
    237 
    238 #define GEN7_FEATURES  \
    239 	.gen = 7, .num_pipes = 3, \
    240 	.need_gfx_hws = 1, .has_hotplug = 1, \
    241 	.has_fbc = 1, \
    242 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING, \
    243 	.has_llc = 1
    244 
    245 static const struct intel_device_info intel_ivybridge_d_info = {
    246 	GEN7_FEATURES,
    247 	.is_ivybridge = 1,
    248 	GEN_DEFAULT_PIPEOFFSETS,
    249 	IVB_CURSOR_OFFSETS,
    250 };
    251 
    252 static const struct intel_device_info intel_ivybridge_m_info = {
    253 	GEN7_FEATURES,
    254 	.is_ivybridge = 1,
    255 	.is_mobile = 1,
    256 	GEN_DEFAULT_PIPEOFFSETS,
    257 	IVB_CURSOR_OFFSETS,
    258 };
    259 
    260 static const struct intel_device_info intel_ivybridge_q_info = {
    261 	GEN7_FEATURES,
    262 	.is_ivybridge = 1,
    263 	.num_pipes = 0, /* legal, last one wins */
    264 	GEN_DEFAULT_PIPEOFFSETS,
    265 	IVB_CURSOR_OFFSETS,
    266 };
    267 
    268 static const struct intel_device_info intel_valleyview_m_info = {
    269 	GEN7_FEATURES,
    270 	.is_mobile = 1,
    271 	.num_pipes = 2,
    272 	.is_valleyview = 1,
    273 	.display_mmio_offset = VLV_DISPLAY_BASE,
    274 	.has_fbc = 0, /* legal, last one wins */
    275 	.has_llc = 0, /* legal, last one wins */
    276 	GEN_DEFAULT_PIPEOFFSETS,
    277 	CURSOR_OFFSETS,
    278 };
    279 
    280 static const struct intel_device_info intel_valleyview_d_info = {
    281 	GEN7_FEATURES,
    282 	.num_pipes = 2,
    283 	.is_valleyview = 1,
    284 	.display_mmio_offset = VLV_DISPLAY_BASE,
    285 	.has_fbc = 0, /* legal, last one wins */
    286 	.has_llc = 0, /* legal, last one wins */
    287 	GEN_DEFAULT_PIPEOFFSETS,
    288 	CURSOR_OFFSETS,
    289 };
    290 
    291 static const struct intel_device_info intel_haswell_d_info = {
    292 	GEN7_FEATURES,
    293 	.is_haswell = 1,
    294 	.has_ddi = 1,
    295 	.has_fpga_dbg = 1,
    296 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING,
    297 	GEN_DEFAULT_PIPEOFFSETS,
    298 	IVB_CURSOR_OFFSETS,
    299 };
    300 
    301 static const struct intel_device_info intel_haswell_m_info = {
    302 	GEN7_FEATURES,
    303 	.is_haswell = 1,
    304 	.is_mobile = 1,
    305 	.has_ddi = 1,
    306 	.has_fpga_dbg = 1,
    307 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING,
    308 	GEN_DEFAULT_PIPEOFFSETS,
    309 	IVB_CURSOR_OFFSETS,
    310 };
    311 
    312 static const struct intel_device_info intel_broadwell_d_info = {
    313 	.gen = 8, .num_pipes = 3,
    314 	.need_gfx_hws = 1, .has_hotplug = 1,
    315 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING,
    316 	.has_llc = 1,
    317 	.has_ddi = 1,
    318 	.has_fpga_dbg = 1,
    319 	.has_fbc = 1,
    320 	GEN_DEFAULT_PIPEOFFSETS,
    321 	IVB_CURSOR_OFFSETS,
    322 };
    323 
    324 static const struct intel_device_info intel_broadwell_m_info = {
    325 	.gen = 8, .is_mobile = 1, .num_pipes = 3,
    326 	.need_gfx_hws = 1, .has_hotplug = 1,
    327 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING,
    328 	.has_llc = 1,
    329 	.has_ddi = 1,
    330 	.has_fpga_dbg = 1,
    331 	.has_fbc = 1,
    332 	GEN_DEFAULT_PIPEOFFSETS,
    333 	IVB_CURSOR_OFFSETS,
    334 };
    335 
    336 static const struct intel_device_info intel_broadwell_gt3d_info = {
    337 	.gen = 8, .num_pipes = 3,
    338 	.need_gfx_hws = 1, .has_hotplug = 1,
    339 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING | BSD2_RING,
    340 	.has_llc = 1,
    341 	.has_ddi = 1,
    342 	.has_fpga_dbg = 1,
    343 	.has_fbc = 1,
    344 	GEN_DEFAULT_PIPEOFFSETS,
    345 	IVB_CURSOR_OFFSETS,
    346 };
    347 
    348 static const struct intel_device_info intel_broadwell_gt3m_info = {
    349 	.gen = 8, .is_mobile = 1, .num_pipes = 3,
    350 	.need_gfx_hws = 1, .has_hotplug = 1,
    351 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING | BSD2_RING,
    352 	.has_llc = 1,
    353 	.has_ddi = 1,
    354 	.has_fpga_dbg = 1,
    355 	.has_fbc = 1,
    356 	GEN_DEFAULT_PIPEOFFSETS,
    357 	IVB_CURSOR_OFFSETS,
    358 };
    359 
    360 static const struct intel_device_info intel_cherryview_info = {
    361 	.gen = 8, .num_pipes = 3,
    362 	.need_gfx_hws = 1, .has_hotplug = 1,
    363 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING,
    364 	.is_valleyview = 1,
    365 	.display_mmio_offset = VLV_DISPLAY_BASE,
    366 	GEN_CHV_PIPEOFFSETS,
    367 	CURSOR_OFFSETS,
    368 };
    369 
    370 static const struct intel_device_info intel_skylake_info = {
    371 	.is_skylake = 1,
    372 	.gen = 9, .num_pipes = 3,
    373 	.need_gfx_hws = 1, .has_hotplug = 1,
    374 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING,
    375 	.has_llc = 1,
    376 	.has_ddi = 1,
    377 	.has_fpga_dbg = 1,
    378 	.has_fbc = 1,
    379 	GEN_DEFAULT_PIPEOFFSETS,
    380 	IVB_CURSOR_OFFSETS,
    381 };
    382 
    383 static const struct intel_device_info intel_skylake_gt3_info = {
    384 	.is_skylake = 1,
    385 	.gen = 9, .num_pipes = 3,
    386 	.need_gfx_hws = 1, .has_hotplug = 1,
    387 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING | BSD2_RING,
    388 	.has_llc = 1,
    389 	.has_ddi = 1,
    390 	.has_fpga_dbg = 1,
    391 	.has_fbc = 1,
    392 	GEN_DEFAULT_PIPEOFFSETS,
    393 	IVB_CURSOR_OFFSETS,
    394 };
    395 
    396 static const struct intel_device_info intel_broxton_info = {
    397 	.is_preliminary = 1,
    398 	.gen = 9,
    399 	.need_gfx_hws = 1, .has_hotplug = 1,
    400 	.ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING,
    401 	.num_pipes = 3,
    402 	.has_ddi = 1,
    403 	.has_fpga_dbg = 1,
    404 	.has_fbc = 1,
    405 	GEN_DEFAULT_PIPEOFFSETS,
    406 	IVB_CURSOR_OFFSETS,
    407 };
    408 
    409 /*
    410  * Make sure any device matches here are from most specific to most
    411  * general.  For example, since the Quanta match is based on the subsystem
    412  * and subvendor IDs, we need it to come before the more general IVB
    413  * PCI ID matches, otherwise we'll use the wrong info struct above.
    414  */
    415 #define INTEL_PCI_IDS \
    416 	INTEL_I830_IDS(&intel_i830_info),	\
    417 	INTEL_I845G_IDS(&intel_845g_info),	\
    418 	INTEL_I85X_IDS(&intel_i85x_info),	\
    419 	INTEL_I865G_IDS(&intel_i865g_info),	\
    420 	INTEL_I915G_IDS(&intel_i915g_info),	\
    421 	INTEL_I915GM_IDS(&intel_i915gm_info),	\
    422 	INTEL_I945G_IDS(&intel_i945g_info),	\
    423 	INTEL_I945GM_IDS(&intel_i945gm_info),	\
    424 	INTEL_I965G_IDS(&intel_i965g_info),	\
    425 	INTEL_G33_IDS(&intel_g33_info),		\
    426 	INTEL_I965GM_IDS(&intel_i965gm_info),	\
    427 	INTEL_GM45_IDS(&intel_gm45_info), 	\
    428 	INTEL_G45_IDS(&intel_g45_info), 	\
    429 	INTEL_PINEVIEW_IDS(&intel_pineview_info),	\
    430 	INTEL_IRONLAKE_D_IDS(&intel_ironlake_d_info),	\
    431 	INTEL_IRONLAKE_M_IDS(&intel_ironlake_m_info),	\
    432 	INTEL_SNB_D_IDS(&intel_sandybridge_d_info),	\
    433 	INTEL_SNB_M_IDS(&intel_sandybridge_m_info),	\
    434 	INTEL_IVB_Q_IDS(&intel_ivybridge_q_info), /* must be first IVB */ \
    435 	INTEL_IVB_M_IDS(&intel_ivybridge_m_info),	\
    436 	INTEL_IVB_D_IDS(&intel_ivybridge_d_info),	\
    437 	INTEL_HSW_D_IDS(&intel_haswell_d_info), \
    438 	INTEL_HSW_M_IDS(&intel_haswell_m_info), \
    439 	INTEL_VLV_M_IDS(&intel_valleyview_m_info),	\
    440 	INTEL_VLV_D_IDS(&intel_valleyview_d_info),	\
    441 	INTEL_BDW_GT12M_IDS(&intel_broadwell_m_info),	\
    442 	INTEL_BDW_GT12D_IDS(&intel_broadwell_d_info),	\
    443 	INTEL_BDW_GT3M_IDS(&intel_broadwell_gt3m_info),	\
    444 	INTEL_BDW_GT3D_IDS(&intel_broadwell_gt3d_info), \
    445 	INTEL_CHV_IDS(&intel_cherryview_info),	\
    446 	INTEL_SKL_GT1_IDS(&intel_skylake_info),	\
    447 	INTEL_SKL_GT2_IDS(&intel_skylake_info),	\
    448 	INTEL_SKL_GT3_IDS(&intel_skylake_gt3_info),	\
    449 	INTEL_BXT_IDS(&intel_broxton_info)
    450 
    451 static const struct pci_device_id pciidlist[] = {		/* aka */
    452 	INTEL_PCI_IDS,
    453 	{0, 0, 0, 0, 0, 0, 0}
    454 };
    455 
    456 MODULE_DEVICE_TABLE(pci, pciidlist);
    457 
    458 static enum intel_pch intel_virt_detect_pch(struct drm_device *dev)
    459 {
    460 	enum intel_pch ret = PCH_NOP;
    461 
    462 	/*
    463 	 * In a virtualized passthrough environment we can be in a
    464 	 * setup where the ISA bridge is not able to be passed through.
    465 	 * In this case, a south bridge can be emulated and we have to
    466 	 * make an educated guess as to which PCH is really there.
    467 	 */
    468 
    469 	if (IS_GEN5(dev)) {
    470 		ret = PCH_IBX;
    471 		DRM_DEBUG_KMS("Assuming Ibex Peak PCH\n");
    472 	} else if (IS_GEN6(dev) || IS_IVYBRIDGE(dev)) {
    473 		ret = PCH_CPT;
    474 		DRM_DEBUG_KMS("Assuming CouarPoint PCH\n");
    475 	} else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
    476 		ret = PCH_LPT;
    477 		DRM_DEBUG_KMS("Assuming LynxPoint PCH\n");
    478 	} else if (IS_SKYLAKE(dev)) {
    479 		ret = PCH_SPT;
    480 		DRM_DEBUG_KMS("Assuming SunrisePoint PCH\n");
    481 	}
    482 
    483 	return ret;
    484 }
    485 
    486 #ifdef __NetBSD__
    487 /* XXX Kludge to expose this to NetBSD driver attachment goop.  */
    488 const struct pci_device_id *const i915_device_ids = pciidlist;
    489 const size_t i915_n_device_ids = __arraycount(pciidlist);
    490 #endif
    491 
    492 void intel_detect_pch(struct drm_device *dev)
    493 {
    494 	struct drm_i915_private *dev_priv = dev->dev_private;
    495 	struct pci_dev *pch = NULL;
    496 
    497 	/* In all current cases, num_pipes is equivalent to the PCH_NOP setting
    498 	 * (which really amounts to a PCH but no South Display).
    499 	 */
    500 	if (INTEL_INFO(dev)->num_pipes == 0) {
    501 		dev_priv->pch_type = PCH_NOP;
    502 		return;
    503 	}
    504 
    505 	/*
    506 	 * The reason to probe ISA bridge instead of Dev31:Fun0 is to
    507 	 * make graphics device passthrough work easy for VMM, that only
    508 	 * need to expose ISA bridge to let driver know the real hardware
    509 	 * underneath. This is a requirement from virtualization team.
    510 	 *
    511 	 * In some virtualized environments (e.g. XEN), there is irrelevant
    512 	 * ISA bridge in the system. To work reliably, we should scan trhough
    513 	 * all the ISA bridge devices and check for the first match, instead
    514 	 * of only checking the first one.
    515 	 */
    516 	while ((pch = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, pch))) {
    517 		if (pch->vendor == PCI_VENDOR_ID_INTEL) {
    518 			unsigned short id = pch->device & INTEL_PCH_DEVICE_ID_MASK;
    519 			dev_priv->pch_id = id;
    520 
    521 			if (id == INTEL_PCH_IBX_DEVICE_ID_TYPE) {
    522 				dev_priv->pch_type = PCH_IBX;
    523 				DRM_DEBUG_KMS("Found Ibex Peak PCH\n");
    524 				WARN_ON(!IS_GEN5(dev));
    525 			} else if (id == INTEL_PCH_CPT_DEVICE_ID_TYPE) {
    526 				dev_priv->pch_type = PCH_CPT;
    527 				DRM_DEBUG_KMS("Found CougarPoint PCH\n");
    528 				WARN_ON(!(IS_GEN6(dev) || IS_IVYBRIDGE(dev)));
    529 			} else if (id == INTEL_PCH_PPT_DEVICE_ID_TYPE) {
    530 				/* PantherPoint is CPT compatible */
    531 				dev_priv->pch_type = PCH_CPT;
    532 				DRM_DEBUG_KMS("Found PantherPoint PCH\n");
    533 				WARN_ON(!(IS_GEN6(dev) || IS_IVYBRIDGE(dev)));
    534 			} else if (id == INTEL_PCH_LPT_DEVICE_ID_TYPE) {
    535 				dev_priv->pch_type = PCH_LPT;
    536 				DRM_DEBUG_KMS("Found LynxPoint PCH\n");
    537 				WARN_ON(!IS_HASWELL(dev) && !IS_BROADWELL(dev));
    538 				WARN_ON(IS_HSW_ULT(dev) || IS_BDW_ULT(dev));
    539 			} else if (id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
    540 				dev_priv->pch_type = PCH_LPT;
    541 				DRM_DEBUG_KMS("Found LynxPoint LP PCH\n");
    542 				WARN_ON(!IS_HASWELL(dev) && !IS_BROADWELL(dev));
    543 				WARN_ON(!IS_HSW_ULT(dev) && !IS_BDW_ULT(dev));
    544 			} else if (id == INTEL_PCH_SPT_DEVICE_ID_TYPE) {
    545 				dev_priv->pch_type = PCH_SPT;
    546 				DRM_DEBUG_KMS("Found SunrisePoint PCH\n");
    547 				WARN_ON(!IS_SKYLAKE(dev));
    548 			} else if (id == INTEL_PCH_SPT_LP_DEVICE_ID_TYPE) {
    549 				dev_priv->pch_type = PCH_SPT;
    550 				DRM_DEBUG_KMS("Found SunrisePoint LP PCH\n");
    551 				WARN_ON(!IS_SKYLAKE(dev));
    552 			} else if ((id == INTEL_PCH_P2X_DEVICE_ID_TYPE) ||
    553 				   ((id == INTEL_PCH_QEMU_DEVICE_ID_TYPE) &&
    554 				    pch->subsystem_vendor == 0x1af4 &&
    555 				    pch->subsystem_device == 0x1100)) {
    556 				dev_priv->pch_type = intel_virt_detect_pch(dev);
    557 			} else
    558 				continue;
    559 
    560 			break;
    561 		}
    562 	}
    563 	if (!pch)
    564 		DRM_DEBUG_KMS("No PCH found.\n");
    565 
    566 	pci_dev_put(pch);
    567 }
    568 
    569 bool i915_semaphore_is_enabled(struct drm_device *dev)
    570 {
    571 	if (INTEL_INFO(dev)->gen < 6)
    572 		return false;
    573 
    574 	if (i915.semaphores >= 0)
    575 		return i915.semaphores;
    576 
    577 	/* TODO: make semaphores and Execlists play nicely together */
    578 	if (i915.enable_execlists)
    579 		return false;
    580 
    581 	/* Until we get further testing... */
    582 	if (IS_GEN8(dev))
    583 		return false;
    584 
    585 #ifdef CONFIG_INTEL_IOMMU
    586 	/* Enable semaphores on SNB when IO remapping is off */
    587 	if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
    588 		return false;
    589 #endif
    590 
    591 	return true;
    592 }
    593 
    594 void i915_firmware_load_error_print(const char *fw_path, int err)
    595 {
    596 	DRM_ERROR("failed to load firmware %s (%d)\n", fw_path, err);
    597 
    598 	/*
    599 	 * If the reason is not known assume -ENOENT since that's the most
    600 	 * usual failure mode.
    601 	 */
    602 	if (!err)
    603 		err = -ENOENT;
    604 
    605 	if (!(IS_BUILTIN(CONFIG_DRM_I915) && err == -ENOENT))
    606 		return;
    607 
    608 	DRM_ERROR(
    609 	  "The driver is built-in, so to load the firmware you need to\n"
    610 	  "include it either in the kernel (see CONFIG_EXTRA_FIRMWARE) or\n"
    611 	  "in your initrd/initramfs image.\n");
    612 }
    613 
    614 static void intel_suspend_encoders(struct drm_i915_private *dev_priv)
    615 {
    616 	struct drm_device *dev = dev_priv->dev;
    617 	struct drm_encoder *encoder;
    618 
    619 	drm_modeset_lock_all(dev);
    620 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
    621 		struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
    622 
    623 		if (intel_encoder->suspend)
    624 			intel_encoder->suspend(intel_encoder);
    625 	}
    626 	drm_modeset_unlock_all(dev);
    627 }
    628 
    629 static int intel_suspend_complete(struct drm_i915_private *dev_priv);
    630 static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
    631 			      bool rpm_resume);
    632 static int skl_resume_prepare(struct drm_i915_private *dev_priv);
    633 static int bxt_resume_prepare(struct drm_i915_private *dev_priv);
    634 
    635 
    636 int i915_drm_suspend(struct drm_device *dev)
    637 {
    638 	struct drm_i915_private *dev_priv = dev->dev_private;
    639 	pci_power_t opregion_target_state;
    640 	int error;
    641 
    642 	/* ignore lid events during suspend */
    643 	mutex_lock(&dev_priv->modeset_restore_lock);
    644 	dev_priv->modeset_restore = MODESET_SUSPENDED;
    645 	mutex_unlock(&dev_priv->modeset_restore_lock);
    646 
    647 	/* We do a lot of poking in a lot of registers, make sure they work
    648 	 * properly. */
    649 	intel_display_set_init_power(dev_priv, true);
    650 
    651 	drm_kms_helper_poll_disable(dev);
    652 
    653 #ifndef __NetBSD__		/* pmf handles this for us.  */
    654 	pci_save_state(dev->pdev);
    655 #endif
    656 
    657 	error = i915_gem_suspend(dev);
    658 	if (error) {
    659 #ifdef __NetBSD__
    660 		dev_err(pci_dev_dev(dev->pdev),
    661 		    "GEM idle failed, resume might fail\n");
    662 #else
    663 		dev_err(&dev->pdev->dev,
    664 			"GEM idle failed, resume might fail\n");
    665 #endif
    666 		return error;
    667 	}
    668 
    669 	intel_guc_suspend(dev);
    670 
    671 	intel_suspend_gt_powersave(dev);
    672 
    673 	/*
    674 	 * Disable CRTCs directly since we want to preserve sw state
    675 	 * for _thaw. Also, power gate the CRTC power wells.
    676 	 */
    677 	drm_modeset_lock_all(dev);
    678 	intel_display_suspend(dev);
    679 	drm_modeset_unlock_all(dev);
    680 
    681 	intel_dp_mst_suspend(dev);
    682 
    683 	intel_runtime_pm_disable_interrupts(dev_priv);
    684 	intel_hpd_cancel_work(dev_priv);
    685 
    686 	intel_suspend_encoders(dev_priv);
    687 
    688 	intel_suspend_hw(dev);
    689 
    690 	i915_gem_suspend_gtt_mappings(dev);
    691 
    692 	i915_save_state(dev);
    693 
    694 	opregion_target_state = PCI_D3cold;
    695 #if IS_ENABLED(CONFIG_ACPI_SLEEP)
    696 	if (acpi_target_system_state() < ACPI_STATE_S3)
    697 		opregion_target_state = PCI_D1;
    698 #endif
    699 	intel_opregion_notify_adapter(dev, opregion_target_state);
    700 
    701 	intel_uncore_forcewake_reset(dev, false);
    702 	intel_opregion_fini(dev);
    703 
    704 #ifndef __NetBSD__		/* XXX fb */
    705 	intel_fbdev_set_suspend(dev, FBINFO_STATE_SUSPENDED, true);
    706 #endif
    707 
    708 	dev_priv->suspend_count++;
    709 
    710 	intel_display_set_init_power(dev_priv, false);
    711 
    712 	return 0;
    713 }
    714 
    715 static int i915_drm_suspend_late(struct drm_device *drm_dev, bool hibernation)
    716 {
    717 	struct drm_i915_private *dev_priv = drm_dev->dev_private;
    718 	int ret;
    719 
    720 	ret = intel_suspend_complete(dev_priv);
    721 
    722 	if (ret) {
    723 		DRM_ERROR("Suspend complete failed: %d\n", ret);
    724 
    725 		return ret;
    726 	}
    727 
    728 #ifndef __NetBSD__		/* pmf handles this for us.  */
    729 	pci_disable_device(drm_dev->pdev);
    730 	/*
    731 	 * During hibernation on some platforms the BIOS may try to access
    732 	 * the device even though it's already in D3 and hang the machine. So
    733 	 * leave the device in D0 on those platforms and hope the BIOS will
    734 	 * power down the device properly. The issue was seen on multiple old
    735 	 * GENs with different BIOS vendors, so having an explicit blacklist
    736 	 * is inpractical; apply the workaround on everything pre GEN6. The
    737 	 * platforms where the issue was seen:
    738 	 * Lenovo Thinkpad X301, X61s, X60, T60, X41
    739 	 * Fujitsu FSC S7110
    740 	 * Acer Aspire 1830T
    741 	 */
    742 	if (!(hibernation && INTEL_INFO(dev_priv)->gen < 6))
    743 		pci_set_power_state(drm_dev->pdev, PCI_D3hot);
    744 #endif
    745 
    746 	return 0;
    747 }
    748 
    749 int i915_suspend_switcheroo(struct drm_device *dev, pm_message_t state)
    750 {
    751 	int error;
    752 
    753 	if (!dev || !dev->dev_private) {
    754 		DRM_ERROR("dev: %p\n", dev);
    755 		DRM_ERROR("DRM not initialized, aborting suspend.\n");
    756 		return -ENODEV;
    757 	}
    758 
    759 	if (WARN_ON_ONCE(state.event != PM_EVENT_SUSPEND &&
    760 			 state.event != PM_EVENT_FREEZE))
    761 		return -EINVAL;
    762 
    763 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
    764 		return 0;
    765 
    766 	error = i915_drm_suspend(dev);
    767 	if (error)
    768 		return error;
    769 
    770 	return i915_drm_suspend_late(dev, false);
    771 }
    772 
    773 int i915_drm_resume(struct drm_device *dev)
    774 {
    775 	struct drm_i915_private *dev_priv = dev->dev_private;
    776 
    777 	mutex_lock(&dev->struct_mutex);
    778 	i915_gem_restore_gtt_mappings(dev);
    779 	mutex_unlock(&dev->struct_mutex);
    780 
    781 	i915_restore_state(dev);
    782 	intel_opregion_setup(dev);
    783 
    784 	intel_init_pch_refclk(dev);
    785 	drm_mode_config_reset(dev);
    786 
    787 	/*
    788 	 * Interrupts have to be enabled before any batches are run. If not the
    789 	 * GPU will hang. i915_gem_init_hw() will initiate batches to
    790 	 * update/restore the context.
    791 	 *
    792 	 * Modeset enabling in intel_modeset_init_hw() also needs working
    793 	 * interrupts.
    794 	 */
    795 	intel_runtime_pm_enable_interrupts(dev_priv);
    796 
    797 	mutex_lock(&dev->struct_mutex);
    798 	if (i915_gem_init_hw(dev)) {
    799 		DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
    800 			atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
    801 	}
    802 	mutex_unlock(&dev->struct_mutex);
    803 
    804 	intel_guc_resume(dev);
    805 
    806 	intel_modeset_init_hw(dev);
    807 
    808 	spin_lock_irq(&dev_priv->irq_lock);
    809 	if (dev_priv->display.hpd_irq_setup)
    810 		dev_priv->display.hpd_irq_setup(dev);
    811 	spin_unlock_irq(&dev_priv->irq_lock);
    812 
    813 	drm_modeset_lock_all(dev);
    814 	intel_display_resume(dev);
    815 	drm_modeset_unlock_all(dev);
    816 
    817 	intel_dp_mst_resume(dev);
    818 
    819 	/*
    820 	 * ... but also need to make sure that hotplug processing
    821 	 * doesn't cause havoc. Like in the driver load code we don't
    822 	 * bother with the tiny race here where we might loose hotplug
    823 	 * notifications.
    824 	 * */
    825 	intel_hpd_init(dev_priv);
    826 	/* Config may have changed between suspend and resume */
    827 	drm_helper_hpd_irq_event(dev);
    828 
    829 	intel_opregion_init(dev);
    830 
    831 #ifndef __NetBSD__		/* XXX fb */
    832 	intel_fbdev_set_suspend(dev, FBINFO_STATE_RUNNING, false);
    833 #endif
    834 
    835 	mutex_lock(&dev_priv->modeset_restore_lock);
    836 	dev_priv->modeset_restore = MODESET_DONE;
    837 	mutex_unlock(&dev_priv->modeset_restore_lock);
    838 
    839 	intel_opregion_notify_adapter(dev, PCI_D0);
    840 
    841 	drm_kms_helper_poll_enable(dev);
    842 
    843 	return 0;
    844 }
    845 
    846 int i915_drm_resume_early(struct drm_device *dev)
    847 {
    848 	struct drm_i915_private *dev_priv = dev->dev_private;
    849 	int ret = 0;
    850 
    851 #ifndef __NetBSD__		/* pmf handles this for us.  */
    852 	/*
    853 	 * We have a resume ordering issue with the snd-hda driver also
    854 	 * requiring our device to be power up. Due to the lack of a
    855 	 * parent/child relationship we currently solve this with an early
    856 	 * resume hook.
    857 	 *
    858 	 * FIXME: This should be solved with a special hdmi sink device or
    859 	 * similar so that power domains can be employed.
    860 	 */
    861 	if (pci_enable_device(dev->pdev))
    862 		return -EIO;
    863 #endif
    864 
    865 	/* XXX pmf probably handles this for us too.  */
    866 	pci_set_master(dev->pdev);
    867 
    868 	if (IS_VALLEYVIEW(dev_priv))
    869 		ret = vlv_resume_prepare(dev_priv, false);
    870 	if (ret)
    871 		DRM_ERROR("Resume prepare failed: %d, continuing anyway\n",
    872 			  ret);
    873 
    874 	intel_uncore_early_sanitize(dev, true);
    875 
    876 	if (IS_BROXTON(dev))
    877 		ret = bxt_resume_prepare(dev_priv);
    878 	else if (IS_SKYLAKE(dev_priv))
    879 		ret = skl_resume_prepare(dev_priv);
    880 	else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
    881 		hsw_disable_pc8(dev_priv);
    882 
    883 	intel_uncore_sanitize(dev);
    884 	intel_power_domains_init_hw(dev_priv);
    885 
    886 	return ret;
    887 }
    888 
    889 int i915_resume_switcheroo(struct drm_device *dev)
    890 {
    891 	int ret;
    892 
    893 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
    894 		return 0;
    895 
    896 	ret = i915_drm_resume_early(dev);
    897 	if (ret)
    898 		return ret;
    899 
    900 	return i915_drm_resume(dev);
    901 }
    902 
    903 /**
    904  * i915_reset - reset chip after a hang
    905  * @dev: drm device to reset
    906  *
    907  * Reset the chip.  Useful if a hang is detected. Returns zero on successful
    908  * reset or otherwise an error code.
    909  *
    910  * Procedure is fairly simple:
    911  *   - reset the chip using the reset reg
    912  *   - re-init context state
    913  *   - re-init hardware status page
    914  *   - re-init ring buffer
    915  *   - re-init interrupt state
    916  *   - re-init display
    917  */
    918 int i915_reset(struct drm_device *dev)
    919 {
    920 	struct drm_i915_private *dev_priv = dev->dev_private;
    921 	bool simulated;
    922 	int ret;
    923 
    924 	intel_reset_gt_powersave(dev);
    925 
    926 	mutex_lock(&dev->struct_mutex);
    927 
    928 	i915_gem_reset(dev);
    929 
    930 	simulated = dev_priv->gpu_error.stop_rings != 0;
    931 
    932 	ret = intel_gpu_reset(dev);
    933 
    934 	/* Also reset the gpu hangman. */
    935 	if (simulated) {
    936 		DRM_INFO("Simulated gpu hang, resetting stop_rings\n");
    937 		dev_priv->gpu_error.stop_rings = 0;
    938 		if (ret == -ENODEV) {
    939 			DRM_INFO("Reset not implemented, but ignoring "
    940 				 "error for simulated gpu hangs\n");
    941 			ret = 0;
    942 		}
    943 	}
    944 
    945 	if (i915_stop_ring_allow_warn(dev_priv))
    946 		pr_notice("drm/i915: Resetting chip after gpu hang\n");
    947 
    948 	if (ret) {
    949 		DRM_ERROR("Failed to reset chip: %i\n", ret);
    950 		mutex_unlock(&dev->struct_mutex);
    951 		return ret;
    952 	}
    953 
    954 	intel_overlay_reset(dev_priv);
    955 
    956 	/* Ok, now get things going again... */
    957 
    958 	/*
    959 	 * Everything depends on having the GTT running, so we need to start
    960 	 * there.  Fortunately we don't need to do this unless we reset the
    961 	 * chip at a PCI level.
    962 	 *
    963 	 * Next we need to restore the context, but we don't use those
    964 	 * yet either...
    965 	 *
    966 	 * Ring buffer needs to be re-initialized in the KMS case, or if X
    967 	 * was running at the time of the reset (i.e. we weren't VT
    968 	 * switched away).
    969 	 */
    970 
    971 	/* Used to prevent gem_check_wedged returning -EAGAIN during gpu reset */
    972 	dev_priv->gpu_error.reload_in_reset = true;
    973 
    974 	ret = i915_gem_init_hw(dev);
    975 
    976 	dev_priv->gpu_error.reload_in_reset = false;
    977 
    978 	mutex_unlock(&dev->struct_mutex);
    979 	if (ret) {
    980 		DRM_ERROR("Failed hw init on reset %d\n", ret);
    981 		return ret;
    982 	}
    983 
    984 	/*
    985 	 * rps/rc6 re-init is necessary to restore state lost after the
    986 	 * reset and the re-install of gt irqs. Skip for ironlake per
    987 	 * previous concerns that it doesn't respond well to some forms
    988 	 * of re-init after reset.
    989 	 */
    990 	if (INTEL_INFO(dev)->gen > 5)
    991 		intel_enable_gt_powersave(dev);
    992 
    993 	return 0;
    994 }
    995 
    996 #ifdef __NetBSD__
    997 
    998 static const struct uvm_pagerops i915_gem_uvm_ops = {
    999 	.pgo_reference = drm_gem_pager_reference,
   1000 	.pgo_detach = drm_gem_pager_detach,
   1001 	.pgo_fault = i915_gem_fault,
   1002 };
   1003 
   1004 #else
   1005 
   1006 static int i915_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
   1007 {
   1008 	struct intel_device_info *intel_info =
   1009 		(struct intel_device_info *) ent->driver_data;
   1010 
   1011 	if (IS_PRELIMINARY_HW(intel_info) && !i915.preliminary_hw_support) {
   1012 		DRM_INFO("This hardware requires preliminary hardware support.\n"
   1013 			 "See CONFIG_DRM_I915_PRELIMINARY_HW_SUPPORT, and/or modparam preliminary_hw_support\n");
   1014 		return -ENODEV;
   1015 	}
   1016 
   1017 	/* Only bind to function 0 of the device. Early generations
   1018 	 * used function 1 as a placeholder for multi-head. This causes
   1019 	 * us confusion instead, especially on the systems where both
   1020 	 * functions have the same PCI-ID!
   1021 	 */
   1022 	if (PCI_FUNC(pdev->devfn))
   1023 		return -ENODEV;
   1024 
   1025 	return drm_get_pci_dev(pdev, ent, &driver);
   1026 }
   1027 
   1028 static void
   1029 i915_pci_remove(struct pci_dev *pdev)
   1030 {
   1031 	struct drm_device *dev = pci_get_drvdata(pdev);
   1032 
   1033 	drm_put_dev(dev);
   1034 }
   1035 #endif
   1036 
   1037 #ifndef __NetBSD__
   1038 static int i915_pm_suspend(struct device *dev)
   1039 {
   1040 	struct pci_dev *pdev = to_pci_dev(dev);
   1041 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
   1042 
   1043 	if (!drm_dev || !drm_dev->dev_private) {
   1044 		dev_err(dev, "DRM not initialized, aborting suspend.\n");
   1045 		return -ENODEV;
   1046 	}
   1047 
   1048 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1049 		return 0;
   1050 
   1051 	return i915_drm_suspend(drm_dev);
   1052 }
   1053 
   1054 static int i915_pm_suspend_late(struct device *dev)
   1055 {
   1056 	struct drm_device *drm_dev = dev_to_i915(dev)->dev;
   1057 
   1058 	/*
   1059 	 * We have a suspend ordering issue with the snd-hda driver also
   1060 	 * requiring our device to be power up. Due to the lack of a
   1061 	 * parent/child relationship we currently solve this with an late
   1062 	 * suspend hook.
   1063 	 *
   1064 	 * FIXME: This should be solved with a special hdmi sink device or
   1065 	 * similar so that power domains can be employed.
   1066 	 */
   1067 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1068 		return 0;
   1069 
   1070 	return i915_drm_suspend_late(drm_dev, false);
   1071 }
   1072 
   1073 static int i915_pm_poweroff_late(struct device *dev)
   1074 {
   1075 	struct drm_device *drm_dev = dev_to_i915(dev)->dev;
   1076 
   1077 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1078 		return 0;
   1079 
   1080 	return i915_drm_suspend_late(drm_dev, true);
   1081 }
   1082 
   1083 static int i915_pm_resume_early(struct device *dev)
   1084 {
   1085 	struct drm_device *drm_dev = dev_to_i915(dev)->dev;
   1086 
   1087 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1088 		return 0;
   1089 
   1090 	return i915_drm_resume_early(drm_dev);
   1091 }
   1092 
   1093 static int i915_pm_resume(struct device *dev)
   1094 {
   1095 	struct drm_device *drm_dev = dev_to_i915(dev)->dev;
   1096 
   1097 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1098 		return 0;
   1099 
   1100 	return i915_drm_resume(drm_dev);
   1101 }
   1102 #endif
   1103 
   1104 static int skl_suspend_complete(struct drm_i915_private *dev_priv)
   1105 {
   1106 	/* Enabling DC6 is not a hard requirement to enter runtime D3 */
   1107 
   1108 	skl_uninit_cdclk(dev_priv);
   1109 
   1110 	return 0;
   1111 }
   1112 
   1113 static int hsw_suspend_complete(struct drm_i915_private *dev_priv)
   1114 {
   1115 	hsw_enable_pc8(dev_priv);
   1116 
   1117 	return 0;
   1118 }
   1119 
   1120 static int bxt_suspend_complete(struct drm_i915_private *dev_priv)
   1121 {
   1122 	struct drm_device *dev = dev_priv->dev;
   1123 
   1124 	/* TODO: when DC5 support is added disable DC5 here. */
   1125 
   1126 	broxton_ddi_phy_uninit(dev);
   1127 	broxton_uninit_cdclk(dev);
   1128 	bxt_enable_dc9(dev_priv);
   1129 
   1130 	return 0;
   1131 }
   1132 
   1133 static int bxt_resume_prepare(struct drm_i915_private *dev_priv)
   1134 {
   1135 	struct drm_device *dev = dev_priv->dev;
   1136 
   1137 	/* TODO: when CSR FW support is added make sure the FW is loaded */
   1138 
   1139 	bxt_disable_dc9(dev_priv);
   1140 
   1141 	/*
   1142 	 * TODO: when DC5 support is added enable DC5 here if the CSR FW
   1143 	 * is available.
   1144 	 */
   1145 	broxton_init_cdclk(dev);
   1146 	broxton_ddi_phy_init(dev);
   1147 	intel_prepare_ddi(dev);
   1148 
   1149 	return 0;
   1150 }
   1151 
   1152 static int skl_resume_prepare(struct drm_i915_private *dev_priv)
   1153 {
   1154 	struct drm_device *dev = dev_priv->dev;
   1155 
   1156 	skl_init_cdclk(dev_priv);
   1157 	intel_csr_load_program(dev);
   1158 
   1159 	return 0;
   1160 }
   1161 
   1162 /*
   1163  * Save all Gunit registers that may be lost after a D3 and a subsequent
   1164  * S0i[R123] transition. The list of registers needing a save/restore is
   1165  * defined in the VLV2_S0IXRegs document. This documents marks all Gunit
   1166  * registers in the following way:
   1167  * - Driver: saved/restored by the driver
   1168  * - Punit : saved/restored by the Punit firmware
   1169  * - No, w/o marking: no need to save/restore, since the register is R/O or
   1170  *                    used internally by the HW in a way that doesn't depend
   1171  *                    keeping the content across a suspend/resume.
   1172  * - Debug : used for debugging
   1173  *
   1174  * We save/restore all registers marked with 'Driver', with the following
   1175  * exceptions:
   1176  * - Registers out of use, including also registers marked with 'Debug'.
   1177  *   These have no effect on the driver's operation, so we don't save/restore
   1178  *   them to reduce the overhead.
   1179  * - Registers that are fully setup by an initialization function called from
   1180  *   the resume path. For example many clock gating and RPS/RC6 registers.
   1181  * - Registers that provide the right functionality with their reset defaults.
   1182  *
   1183  * TODO: Except for registers that based on the above 3 criteria can be safely
   1184  * ignored, we save/restore all others, practically treating the HW context as
   1185  * a black-box for the driver. Further investigation is needed to reduce the
   1186  * saved/restored registers even further, by following the same 3 criteria.
   1187  */
   1188 static void vlv_save_gunit_s0ix_state(struct drm_i915_private *dev_priv)
   1189 {
   1190 	struct vlv_s0ix_state *s = &dev_priv->vlv_s0ix_state;
   1191 	int i;
   1192 
   1193 	/* GAM 0x4000-0x4770 */
   1194 	s->wr_watermark		= I915_READ(GEN7_WR_WATERMARK);
   1195 	s->gfx_prio_ctrl	= I915_READ(GEN7_GFX_PRIO_CTRL);
   1196 	s->arb_mode		= I915_READ(ARB_MODE);
   1197 	s->gfx_pend_tlb0	= I915_READ(GEN7_GFX_PEND_TLB0);
   1198 	s->gfx_pend_tlb1	= I915_READ(GEN7_GFX_PEND_TLB1);
   1199 
   1200 	for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
   1201 		s->lra_limits[i] = I915_READ(GEN7_LRA_LIMITS(i));
   1202 
   1203 	s->media_max_req_count	= I915_READ(GEN7_MEDIA_MAX_REQ_COUNT);
   1204 	s->gfx_max_req_count	= I915_READ(GEN7_GFX_MAX_REQ_COUNT);
   1205 
   1206 	s->render_hwsp		= I915_READ(RENDER_HWS_PGA_GEN7);
   1207 	s->ecochk		= I915_READ(GAM_ECOCHK);
   1208 	s->bsd_hwsp		= I915_READ(BSD_HWS_PGA_GEN7);
   1209 	s->blt_hwsp		= I915_READ(BLT_HWS_PGA_GEN7);
   1210 
   1211 	s->tlb_rd_addr		= I915_READ(GEN7_TLB_RD_ADDR);
   1212 
   1213 	/* MBC 0x9024-0x91D0, 0x8500 */
   1214 	s->g3dctl		= I915_READ(VLV_G3DCTL);
   1215 	s->gsckgctl		= I915_READ(VLV_GSCKGCTL);
   1216 	s->mbctl		= I915_READ(GEN6_MBCTL);
   1217 
   1218 	/* GCP 0x9400-0x9424, 0x8100-0x810C */
   1219 	s->ucgctl1		= I915_READ(GEN6_UCGCTL1);
   1220 	s->ucgctl3		= I915_READ(GEN6_UCGCTL3);
   1221 	s->rcgctl1		= I915_READ(GEN6_RCGCTL1);
   1222 	s->rcgctl2		= I915_READ(GEN6_RCGCTL2);
   1223 	s->rstctl		= I915_READ(GEN6_RSTCTL);
   1224 	s->misccpctl		= I915_READ(GEN7_MISCCPCTL);
   1225 
   1226 	/* GPM 0xA000-0xAA84, 0x8000-0x80FC */
   1227 	s->gfxpause		= I915_READ(GEN6_GFXPAUSE);
   1228 	s->rpdeuhwtc		= I915_READ(GEN6_RPDEUHWTC);
   1229 	s->rpdeuc		= I915_READ(GEN6_RPDEUC);
   1230 	s->ecobus		= I915_READ(ECOBUS);
   1231 	s->pwrdwnupctl		= I915_READ(VLV_PWRDWNUPCTL);
   1232 	s->rp_down_timeout	= I915_READ(GEN6_RP_DOWN_TIMEOUT);
   1233 	s->rp_deucsw		= I915_READ(GEN6_RPDEUCSW);
   1234 	s->rcubmabdtmr		= I915_READ(GEN6_RCUBMABDTMR);
   1235 	s->rcedata		= I915_READ(VLV_RCEDATA);
   1236 	s->spare2gh		= I915_READ(VLV_SPAREG2H);
   1237 
   1238 	/* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
   1239 	s->gt_imr		= I915_READ(GTIMR);
   1240 	s->gt_ier		= I915_READ(GTIER);
   1241 	s->pm_imr		= I915_READ(GEN6_PMIMR);
   1242 	s->pm_ier		= I915_READ(GEN6_PMIER);
   1243 
   1244 	for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
   1245 		s->gt_scratch[i] = I915_READ(GEN7_GT_SCRATCH(i));
   1246 
   1247 	/* GT SA CZ domain, 0x100000-0x138124 */
   1248 	s->tilectl		= I915_READ(TILECTL);
   1249 	s->gt_fifoctl		= I915_READ(GTFIFOCTL);
   1250 	s->gtlc_wake_ctrl	= I915_READ(VLV_GTLC_WAKE_CTRL);
   1251 	s->gtlc_survive		= I915_READ(VLV_GTLC_SURVIVABILITY_REG);
   1252 	s->pmwgicz		= I915_READ(VLV_PMWGICZ);
   1253 
   1254 	/* Gunit-Display CZ domain, 0x182028-0x1821CF */
   1255 	s->gu_ctl0		= I915_READ(VLV_GU_CTL0);
   1256 	s->gu_ctl1		= I915_READ(VLV_GU_CTL1);
   1257 	s->pcbr			= I915_READ(VLV_PCBR);
   1258 	s->clock_gate_dis2	= I915_READ(VLV_GUNIT_CLOCK_GATE2);
   1259 
   1260 	/*
   1261 	 * Not saving any of:
   1262 	 * DFT,		0x9800-0x9EC0
   1263 	 * SARB,	0xB000-0xB1FC
   1264 	 * GAC,		0x5208-0x524C, 0x14000-0x14C000
   1265 	 * PCI CFG
   1266 	 */
   1267 }
   1268 
   1269 static void vlv_restore_gunit_s0ix_state(struct drm_i915_private *dev_priv)
   1270 {
   1271 	struct vlv_s0ix_state *s = &dev_priv->vlv_s0ix_state;
   1272 	u32 val;
   1273 	int i;
   1274 
   1275 	/* GAM 0x4000-0x4770 */
   1276 	I915_WRITE(GEN7_WR_WATERMARK,	s->wr_watermark);
   1277 	I915_WRITE(GEN7_GFX_PRIO_CTRL,	s->gfx_prio_ctrl);
   1278 	I915_WRITE(ARB_MODE,		s->arb_mode | (0xffff << 16));
   1279 	I915_WRITE(GEN7_GFX_PEND_TLB0,	s->gfx_pend_tlb0);
   1280 	I915_WRITE(GEN7_GFX_PEND_TLB1,	s->gfx_pend_tlb1);
   1281 
   1282 	for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
   1283 		I915_WRITE(GEN7_LRA_LIMITS(i), s->lra_limits[i]);
   1284 
   1285 	I915_WRITE(GEN7_MEDIA_MAX_REQ_COUNT, s->media_max_req_count);
   1286 	I915_WRITE(GEN7_GFX_MAX_REQ_COUNT, s->gfx_max_req_count);
   1287 
   1288 	I915_WRITE(RENDER_HWS_PGA_GEN7,	s->render_hwsp);
   1289 	I915_WRITE(GAM_ECOCHK,		s->ecochk);
   1290 	I915_WRITE(BSD_HWS_PGA_GEN7,	s->bsd_hwsp);
   1291 	I915_WRITE(BLT_HWS_PGA_GEN7,	s->blt_hwsp);
   1292 
   1293 	I915_WRITE(GEN7_TLB_RD_ADDR,	s->tlb_rd_addr);
   1294 
   1295 	/* MBC 0x9024-0x91D0, 0x8500 */
   1296 	I915_WRITE(VLV_G3DCTL,		s->g3dctl);
   1297 	I915_WRITE(VLV_GSCKGCTL,	s->gsckgctl);
   1298 	I915_WRITE(GEN6_MBCTL,		s->mbctl);
   1299 
   1300 	/* GCP 0x9400-0x9424, 0x8100-0x810C */
   1301 	I915_WRITE(GEN6_UCGCTL1,	s->ucgctl1);
   1302 	I915_WRITE(GEN6_UCGCTL3,	s->ucgctl3);
   1303 	I915_WRITE(GEN6_RCGCTL1,	s->rcgctl1);
   1304 	I915_WRITE(GEN6_RCGCTL2,	s->rcgctl2);
   1305 	I915_WRITE(GEN6_RSTCTL,		s->rstctl);
   1306 	I915_WRITE(GEN7_MISCCPCTL,	s->misccpctl);
   1307 
   1308 	/* GPM 0xA000-0xAA84, 0x8000-0x80FC */
   1309 	I915_WRITE(GEN6_GFXPAUSE,	s->gfxpause);
   1310 	I915_WRITE(GEN6_RPDEUHWTC,	s->rpdeuhwtc);
   1311 	I915_WRITE(GEN6_RPDEUC,		s->rpdeuc);
   1312 	I915_WRITE(ECOBUS,		s->ecobus);
   1313 	I915_WRITE(VLV_PWRDWNUPCTL,	s->pwrdwnupctl);
   1314 	I915_WRITE(GEN6_RP_DOWN_TIMEOUT,s->rp_down_timeout);
   1315 	I915_WRITE(GEN6_RPDEUCSW,	s->rp_deucsw);
   1316 	I915_WRITE(GEN6_RCUBMABDTMR,	s->rcubmabdtmr);
   1317 	I915_WRITE(VLV_RCEDATA,		s->rcedata);
   1318 	I915_WRITE(VLV_SPAREG2H,	s->spare2gh);
   1319 
   1320 	/* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
   1321 	I915_WRITE(GTIMR,		s->gt_imr);
   1322 	I915_WRITE(GTIER,		s->gt_ier);
   1323 	I915_WRITE(GEN6_PMIMR,		s->pm_imr);
   1324 	I915_WRITE(GEN6_PMIER,		s->pm_ier);
   1325 
   1326 	for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
   1327 		I915_WRITE(GEN7_GT_SCRATCH(i), s->gt_scratch[i]);
   1328 
   1329 	/* GT SA CZ domain, 0x100000-0x138124 */
   1330 	I915_WRITE(TILECTL,			s->tilectl);
   1331 	I915_WRITE(GTFIFOCTL,			s->gt_fifoctl);
   1332 	/*
   1333 	 * Preserve the GT allow wake and GFX force clock bit, they are not
   1334 	 * be restored, as they are used to control the s0ix suspend/resume
   1335 	 * sequence by the caller.
   1336 	 */
   1337 	val = I915_READ(VLV_GTLC_WAKE_CTRL);
   1338 	val &= VLV_GTLC_ALLOWWAKEREQ;
   1339 	val |= s->gtlc_wake_ctrl & ~VLV_GTLC_ALLOWWAKEREQ;
   1340 	I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
   1341 
   1342 	val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
   1343 	val &= VLV_GFX_CLK_FORCE_ON_BIT;
   1344 	val |= s->gtlc_survive & ~VLV_GFX_CLK_FORCE_ON_BIT;
   1345 	I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
   1346 
   1347 	I915_WRITE(VLV_PMWGICZ,			s->pmwgicz);
   1348 
   1349 	/* Gunit-Display CZ domain, 0x182028-0x1821CF */
   1350 	I915_WRITE(VLV_GU_CTL0,			s->gu_ctl0);
   1351 	I915_WRITE(VLV_GU_CTL1,			s->gu_ctl1);
   1352 	I915_WRITE(VLV_PCBR,			s->pcbr);
   1353 	I915_WRITE(VLV_GUNIT_CLOCK_GATE2,	s->clock_gate_dis2);
   1354 }
   1355 
   1356 int vlv_force_gfx_clock(struct drm_i915_private *dev_priv, bool force_on)
   1357 {
   1358 	u32 val;
   1359 	int err;
   1360 
   1361 #define COND (I915_READ(VLV_GTLC_SURVIVABILITY_REG) & VLV_GFX_CLK_STATUS_BIT)
   1362 
   1363 	val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
   1364 	val &= ~VLV_GFX_CLK_FORCE_ON_BIT;
   1365 	if (force_on)
   1366 		val |= VLV_GFX_CLK_FORCE_ON_BIT;
   1367 	I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
   1368 
   1369 	if (!force_on)
   1370 		return 0;
   1371 
   1372 	err = wait_for(COND, 20);
   1373 	if (err)
   1374 		DRM_ERROR("timeout waiting for GFX clock force-on (%08x)\n",
   1375 			  I915_READ(VLV_GTLC_SURVIVABILITY_REG));
   1376 
   1377 	return err;
   1378 #undef COND
   1379 }
   1380 
   1381 static int vlv_allow_gt_wake(struct drm_i915_private *dev_priv, bool allow)
   1382 {
   1383 	u32 val;
   1384 	int err = 0;
   1385 
   1386 	val = I915_READ(VLV_GTLC_WAKE_CTRL);
   1387 	val &= ~VLV_GTLC_ALLOWWAKEREQ;
   1388 	if (allow)
   1389 		val |= VLV_GTLC_ALLOWWAKEREQ;
   1390 	I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
   1391 	POSTING_READ(VLV_GTLC_WAKE_CTRL);
   1392 
   1393 #define COND (!!(I915_READ(VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEACK) == \
   1394 	      allow)
   1395 	err = wait_for(COND, 1);
   1396 	if (err)
   1397 		DRM_ERROR("timeout disabling GT waking\n");
   1398 	return err;
   1399 #undef COND
   1400 }
   1401 
   1402 static int vlv_wait_for_gt_wells(struct drm_i915_private *dev_priv,
   1403 				 bool wait_for_on)
   1404 {
   1405 	u32 mask;
   1406 	u32 val;
   1407 	int err;
   1408 
   1409 	mask = VLV_GTLC_PW_MEDIA_STATUS_MASK | VLV_GTLC_PW_RENDER_STATUS_MASK;
   1410 	val = wait_for_on ? mask : 0;
   1411 #define COND ((I915_READ(VLV_GTLC_PW_STATUS) & mask) == val)
   1412 	if (COND)
   1413 		return 0;
   1414 
   1415 	DRM_DEBUG_KMS("waiting for GT wells to go %s (%08x)\n",
   1416 			wait_for_on ? "on" : "off",
   1417 			I915_READ(VLV_GTLC_PW_STATUS));
   1418 
   1419 	/*
   1420 	 * RC6 transitioning can be delayed up to 2 msec (see
   1421 	 * valleyview_enable_rps), use 3 msec for safety.
   1422 	 */
   1423 	err = wait_for(COND, 3);
   1424 	if (err)
   1425 		DRM_ERROR("timeout waiting for GT wells to go %s\n",
   1426 			  wait_for_on ? "on" : "off");
   1427 
   1428 	return err;
   1429 #undef COND
   1430 }
   1431 
   1432 static void vlv_check_no_gt_access(struct drm_i915_private *dev_priv)
   1433 {
   1434 	if (!(I915_READ(VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEERR))
   1435 		return;
   1436 
   1437 	DRM_ERROR("GT register access while GT waking disabled\n");
   1438 	I915_WRITE(VLV_GTLC_PW_STATUS, VLV_GTLC_ALLOWWAKEERR);
   1439 }
   1440 
   1441 static int vlv_suspend_complete(struct drm_i915_private *dev_priv)
   1442 {
   1443 	u32 mask;
   1444 	int err;
   1445 
   1446 	/*
   1447 	 * Bspec defines the following GT well on flags as debug only, so
   1448 	 * don't treat them as hard failures.
   1449 	 */
   1450 	(void)vlv_wait_for_gt_wells(dev_priv, false);
   1451 
   1452 	mask = VLV_GTLC_RENDER_CTX_EXISTS | VLV_GTLC_MEDIA_CTX_EXISTS;
   1453 	WARN_ON((I915_READ(VLV_GTLC_WAKE_CTRL) & mask) != mask);
   1454 
   1455 	vlv_check_no_gt_access(dev_priv);
   1456 
   1457 	err = vlv_force_gfx_clock(dev_priv, true);
   1458 	if (err)
   1459 		goto err1;
   1460 
   1461 	err = vlv_allow_gt_wake(dev_priv, false);
   1462 	if (err)
   1463 		goto err2;
   1464 
   1465 	if (!IS_CHERRYVIEW(dev_priv->dev))
   1466 		vlv_save_gunit_s0ix_state(dev_priv);
   1467 
   1468 	err = vlv_force_gfx_clock(dev_priv, false);
   1469 	if (err)
   1470 		goto err2;
   1471 
   1472 	return 0;
   1473 
   1474 err2:
   1475 	/* For safety always re-enable waking and disable gfx clock forcing */
   1476 	vlv_allow_gt_wake(dev_priv, true);
   1477 err1:
   1478 	vlv_force_gfx_clock(dev_priv, false);
   1479 
   1480 	return err;
   1481 }
   1482 
   1483 static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
   1484 				bool rpm_resume)
   1485 {
   1486 	struct drm_device *dev = dev_priv->dev;
   1487 	int err;
   1488 	int ret;
   1489 
   1490 	/*
   1491 	 * If any of the steps fail just try to continue, that's the best we
   1492 	 * can do at this point. Return the first error code (which will also
   1493 	 * leave RPM permanently disabled).
   1494 	 */
   1495 	ret = vlv_force_gfx_clock(dev_priv, true);
   1496 
   1497 	if (!IS_CHERRYVIEW(dev_priv->dev))
   1498 		vlv_restore_gunit_s0ix_state(dev_priv);
   1499 
   1500 	err = vlv_allow_gt_wake(dev_priv, true);
   1501 	if (!ret)
   1502 		ret = err;
   1503 
   1504 	err = vlv_force_gfx_clock(dev_priv, false);
   1505 	if (!ret)
   1506 		ret = err;
   1507 
   1508 	vlv_check_no_gt_access(dev_priv);
   1509 
   1510 	if (rpm_resume) {
   1511 		intel_init_clock_gating(dev);
   1512 		i915_gem_restore_fences(dev);
   1513 	}
   1514 
   1515 	return ret;
   1516 }
   1517 
   1518 #ifndef __NetBSD__		/* XXX runtime pm */
   1519 static int intel_runtime_suspend(struct device *device)
   1520 {
   1521 	struct pci_dev *pdev = to_pci_dev(device);
   1522 	struct drm_device *dev = pci_get_drvdata(pdev);
   1523 	struct drm_i915_private *dev_priv = dev->dev_private;
   1524 	int ret;
   1525 
   1526 	if (WARN_ON_ONCE(!(dev_priv->rps.enabled && intel_enable_rc6(dev))))
   1527 		return -ENODEV;
   1528 
   1529 	if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev)))
   1530 		return -ENODEV;
   1531 
   1532 	DRM_DEBUG_KMS("Suspending device\n");
   1533 
   1534 	/*
   1535 	 * We could deadlock here in case another thread holding struct_mutex
   1536 	 * calls RPM suspend concurrently, since the RPM suspend will wait
   1537 	 * first for this RPM suspend to finish. In this case the concurrent
   1538 	 * RPM resume will be followed by its RPM suspend counterpart. Still
   1539 	 * for consistency return -EAGAIN, which will reschedule this suspend.
   1540 	 */
   1541 	if (!mutex_trylock(&dev->struct_mutex)) {
   1542 		DRM_DEBUG_KMS("device lock contention, deffering suspend\n");
   1543 		/*
   1544 		 * Bump the expiration timestamp, otherwise the suspend won't
   1545 		 * be rescheduled.
   1546 		 */
   1547 		pm_runtime_mark_last_busy(device);
   1548 
   1549 		return -EAGAIN;
   1550 	}
   1551 	/*
   1552 	 * We are safe here against re-faults, since the fault handler takes
   1553 	 * an RPM reference.
   1554 	 */
   1555 	i915_gem_release_all_mmaps(dev_priv);
   1556 	mutex_unlock(&dev->struct_mutex);
   1557 
   1558 	intel_guc_suspend(dev);
   1559 
   1560 	intel_suspend_gt_powersave(dev);
   1561 	intel_runtime_pm_disable_interrupts(dev_priv);
   1562 
   1563 	ret = intel_suspend_complete(dev_priv);
   1564 	if (ret) {
   1565 		DRM_ERROR("Runtime suspend failed, disabling it (%d)\n", ret);
   1566 		intel_runtime_pm_enable_interrupts(dev_priv);
   1567 
   1568 		return ret;
   1569 	}
   1570 
   1571 	cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
   1572 	intel_uncore_forcewake_reset(dev, false);
   1573 	dev_priv->pm.suspended = true;
   1574 
   1575 	/*
   1576 	 * FIXME: We really should find a document that references the arguments
   1577 	 * used below!
   1578 	 */
   1579 	if (IS_BROADWELL(dev)) {
   1580 		/*
   1581 		 * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop
   1582 		 * being detected, and the call we do at intel_runtime_resume()
   1583 		 * won't be able to restore them. Since PCI_D3hot matches the
   1584 		 * actual specification and appears to be working, use it.
   1585 		 */
   1586 		intel_opregion_notify_adapter(dev, PCI_D3hot);
   1587 	} else {
   1588 		/*
   1589 		 * current versions of firmware which depend on this opregion
   1590 		 * notification have repurposed the D1 definition to mean
   1591 		 * "runtime suspended" vs. what you would normally expect (D3)
   1592 		 * to distinguish it from notifications that might be sent via
   1593 		 * the suspend path.
   1594 		 */
   1595 		intel_opregion_notify_adapter(dev, PCI_D1);
   1596 	}
   1597 
   1598 	assert_forcewakes_inactive(dev_priv);
   1599 
   1600 	DRM_DEBUG_KMS("Device suspended\n");
   1601 	return 0;
   1602 }
   1603 
   1604 static int intel_runtime_resume(struct device *device)
   1605 {
   1606 	struct pci_dev *pdev = to_pci_dev(device);
   1607 	struct drm_device *dev = pci_get_drvdata(pdev);
   1608 	struct drm_i915_private *dev_priv = dev->dev_private;
   1609 	int ret = 0;
   1610 
   1611 	if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev)))
   1612 		return -ENODEV;
   1613 
   1614 	DRM_DEBUG_KMS("Resuming device\n");
   1615 
   1616 	intel_opregion_notify_adapter(dev, PCI_D0);
   1617 	dev_priv->pm.suspended = false;
   1618 
   1619 	intel_guc_resume(dev);
   1620 
   1621 	if (IS_GEN6(dev_priv))
   1622 		intel_init_pch_refclk(dev);
   1623 
   1624 	if (IS_BROXTON(dev))
   1625 		ret = bxt_resume_prepare(dev_priv);
   1626 	else if (IS_SKYLAKE(dev))
   1627 		ret = skl_resume_prepare(dev_priv);
   1628 	else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
   1629 		hsw_disable_pc8(dev_priv);
   1630 	else if (IS_VALLEYVIEW(dev_priv))
   1631 		ret = vlv_resume_prepare(dev_priv, true);
   1632 
   1633 	/*
   1634 	 * No point of rolling back things in case of an error, as the best
   1635 	 * we can do is to hope that things will still work (and disable RPM).
   1636 	 */
   1637 	i915_gem_init_swizzling(dev);
   1638 	gen6_update_ring_freq(dev);
   1639 
   1640 	intel_runtime_pm_enable_interrupts(dev_priv);
   1641 
   1642 	/*
   1643 	 * On VLV/CHV display interrupts are part of the display
   1644 	 * power well, so hpd is reinitialized from there. For
   1645 	 * everyone else do it here.
   1646 	 */
   1647 	if (!IS_VALLEYVIEW(dev_priv))
   1648 		intel_hpd_init(dev_priv);
   1649 
   1650 	intel_enable_gt_powersave(dev);
   1651 
   1652 	if (ret)
   1653 		DRM_ERROR("Runtime resume failed, disabling it (%d)\n", ret);
   1654 	else
   1655 		DRM_DEBUG_KMS("Device resumed\n");
   1656 
   1657 	return ret;
   1658 }
   1659 #endif
   1660 
   1661 /*
   1662  * This function implements common functionality of runtime and system
   1663  * suspend sequence.
   1664  */
   1665 static int intel_suspend_complete(struct drm_i915_private *dev_priv)
   1666 {
   1667 	int ret;
   1668 
   1669 	if (IS_BROXTON(dev_priv))
   1670 		ret = bxt_suspend_complete(dev_priv);
   1671 	else if (IS_SKYLAKE(dev_priv))
   1672 		ret = skl_suspend_complete(dev_priv);
   1673 	else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
   1674 		ret = hsw_suspend_complete(dev_priv);
   1675 	else if (IS_VALLEYVIEW(dev_priv))
   1676 		ret = vlv_suspend_complete(dev_priv);
   1677 	else
   1678 		ret = 0;
   1679 
   1680 	return ret;
   1681 }
   1682 
   1683 #ifndef __NetBSD__
   1684 
   1685 static const struct dev_pm_ops i915_pm_ops = {
   1686 	/*
   1687 	 * S0ix (via system suspend) and S3 event handlers [PMSG_SUSPEND,
   1688 	 * PMSG_RESUME]
   1689 	 */
   1690 	.suspend = i915_pm_suspend,
   1691 	.suspend_late = i915_pm_suspend_late,
   1692 	.resume_early = i915_pm_resume_early,
   1693 	.resume = i915_pm_resume,
   1694 
   1695 	/*
   1696 	 * S4 event handlers
   1697 	 * @freeze, @freeze_late    : called (1) before creating the
   1698 	 *                            hibernation image [PMSG_FREEZE] and
   1699 	 *                            (2) after rebooting, before restoring
   1700 	 *                            the image [PMSG_QUIESCE]
   1701 	 * @thaw, @thaw_early       : called (1) after creating the hibernation
   1702 	 *                            image, before writing it [PMSG_THAW]
   1703 	 *                            and (2) after failing to create or
   1704 	 *                            restore the image [PMSG_RECOVER]
   1705 	 * @poweroff, @poweroff_late: called after writing the hibernation
   1706 	 *                            image, before rebooting [PMSG_HIBERNATE]
   1707 	 * @restore, @restore_early : called after rebooting and restoring the
   1708 	 *                            hibernation image [PMSG_RESTORE]
   1709 	 */
   1710 	.freeze = i915_pm_suspend,
   1711 	.freeze_late = i915_pm_suspend_late,
   1712 	.thaw_early = i915_pm_resume_early,
   1713 	.thaw = i915_pm_resume,
   1714 	.poweroff = i915_pm_suspend,
   1715 	.poweroff_late = i915_pm_poweroff_late,
   1716 	.restore_early = i915_pm_resume_early,
   1717 	.restore = i915_pm_resume,
   1718 
   1719 	/* S0ix (via runtime suspend) event handlers */
   1720 	.runtime_suspend = intel_runtime_suspend,
   1721 	.runtime_resume = intel_runtime_resume,
   1722 };
   1723 
   1724 static const struct vm_operations_struct i915_gem_vm_ops = {
   1725 	.fault = i915_gem_fault,
   1726 	.open = drm_gem_vm_open,
   1727 	.close = drm_gem_vm_close,
   1728 };
   1729 
   1730 static const struct file_operations i915_driver_fops = {
   1731 	.owner = THIS_MODULE,
   1732 	.open = drm_open,
   1733 	.release = drm_release,
   1734 	.unlocked_ioctl = drm_ioctl,
   1735 	.mmap = drm_gem_mmap,
   1736 	.poll = drm_poll,
   1737 	.read = drm_read,
   1738 #ifdef CONFIG_COMPAT
   1739 	.compat_ioctl = i915_compat_ioctl,
   1740 #endif
   1741 	.llseek = noop_llseek,
   1742 };
   1743 
   1744 #endif	/* defined(__NetBSD__) */
   1745 
   1746 static struct drm_driver driver = {
   1747 	/* Don't use MTRRs here; the Xserver or userspace app should
   1748 	 * deal with them for Intel hardware.
   1749 	 */
   1750 	.driver_features =
   1751 	    DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM | DRIVER_PRIME |
   1752 	    DRIVER_RENDER | DRIVER_MODESET,
   1753 	.load = i915_driver_load,
   1754 	.unload = i915_driver_unload,
   1755 	.open = i915_driver_open,
   1756 	.lastclose = i915_driver_lastclose,
   1757 	.preclose = i915_driver_preclose,
   1758 	.postclose = i915_driver_postclose,
   1759 	.set_busid = drm_pci_set_busid,
   1760 #ifdef __NetBSD__
   1761 	.request_irq = drm_pci_request_irq,
   1762 	.free_irq = drm_pci_free_irq,
   1763 #endif
   1764 
   1765 #if defined(CONFIG_DEBUG_FS)
   1766 	.debugfs_init = i915_debugfs_init,
   1767 	.debugfs_cleanup = i915_debugfs_cleanup,
   1768 #endif
   1769 	.gem_free_object = i915_gem_free_object,
   1770 #ifdef __NetBSD__
   1771 	/* XXX Not clear the `or legacy' part is important here.  */
   1772 	.mmap_object = &drm_gem_mmap_object,
   1773 	.gem_uvm_ops = &i915_gem_uvm_ops,
   1774 #else
   1775 	.gem_vm_ops = &i915_gem_vm_ops,
   1776 #endif
   1777 
   1778 #ifndef __NetBSD__		/* XXX drm prime */
   1779 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
   1780 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
   1781 	.gem_prime_export = i915_gem_prime_export,
   1782 	.gem_prime_import = i915_gem_prime_import,
   1783 #endif
   1784 
   1785 	.dumb_create = i915_gem_dumb_create,
   1786 	.dumb_map_offset = i915_gem_mmap_gtt,
   1787 	.dumb_destroy = drm_gem_dumb_destroy,
   1788 	.ioctls = i915_ioctls,
   1789 #ifdef __NetBSD__
   1790 	.fops = NULL,
   1791 #else
   1792 	.fops = &i915_driver_fops,
   1793 #endif
   1794 	.name = DRIVER_NAME,
   1795 	.desc = DRIVER_DESC,
   1796 	.date = DRIVER_DATE,
   1797 	.major = DRIVER_MAJOR,
   1798 	.minor = DRIVER_MINOR,
   1799 	.patchlevel = DRIVER_PATCHLEVEL,
   1800 };
   1801 
   1802 #ifndef __NetBSD__
   1803 static struct pci_driver i915_pci_driver = {
   1804 	.name = DRIVER_NAME,
   1805 	.id_table = pciidlist,
   1806 	.probe = i915_pci_probe,
   1807 	.remove = i915_pci_remove,
   1808 	.driver.pm = &i915_pm_ops,
   1809 };
   1810 #endif
   1811 
   1812 #ifndef __NetBSD__
   1813 static int __init i915_init(void)
   1814 {
   1815 	driver.num_ioctls = i915_max_ioctl;
   1816 
   1817 	/*
   1818 	 * Enable KMS by default, unless explicitly overriden by
   1819 	 * either the i915.modeset prarameter or by the
   1820 	 * vga_text_mode_force boot option.
   1821 	 */
   1822 
   1823 	if (i915.modeset == 0)
   1824 		driver.driver_features &= ~DRIVER_MODESET;
   1825 
   1826 #ifdef CONFIG_VGA_CONSOLE
   1827 	if (vgacon_text_force() && i915.modeset == -1)
   1828 		driver.driver_features &= ~DRIVER_MODESET;
   1829 #endif
   1830 
   1831 	if (!(driver.driver_features & DRIVER_MODESET)) {
   1832 		/* Silently fail loading to not upset userspace. */
   1833 		DRM_DEBUG_DRIVER("KMS and UMS disabled.\n");
   1834 		return 0;
   1835 	}
   1836 
   1837 	if (i915.nuclear_pageflip)
   1838 		driver.driver_features |= DRIVER_ATOMIC;
   1839 
   1840 	return drm_pci_init(&driver, &i915_pci_driver);
   1841 }
   1842 
   1843 static void __exit i915_exit(void)
   1844 {
   1845 	if (!(driver.driver_features & DRIVER_MODESET))
   1846 		return; /* Never loaded a driver. */
   1847 
   1848 	drm_pci_exit(&driver, &i915_pci_driver);
   1849 }
   1850 
   1851 module_init(i915_init);
   1852 module_exit(i915_exit);
   1853 #endif
   1854 
   1855 MODULE_AUTHOR("Tungsten Graphics, Inc.");
   1856 MODULE_AUTHOR("Intel Corporation");
   1857 
   1858 MODULE_DESCRIPTION(DRIVER_DESC);
   1859 MODULE_LICENSE("GPL and additional rights");
   1860