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