Home | History | Annotate | Line # | Download | only in i915
i915_drv.c revision 1.16.4.1
      1 /*	$NetBSD: i915_drv.c,v 1.16.4.1 2019/12/12 21:00:32 martin 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.16.4.1 2019/12/12 21:00:32 martin 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 	i915_rc6_ctx_wa_suspend(dev_priv);
    769 
    770 #ifndef __NetBSD__		/* pmf handles this for us.  */
    771 	pci_disable_device(drm_dev->pdev);
    772 	/*
    773 	 * During hibernation on some platforms the BIOS may try to access
    774 	 * the device even though it's already in D3 and hang the machine. So
    775 	 * leave the device in D0 on those platforms and hope the BIOS will
    776 	 * power down the device properly. The issue was seen on multiple old
    777 	 * GENs with different BIOS vendors, so having an explicit blacklist
    778 	 * is inpractical; apply the workaround on everything pre GEN6. The
    779 	 * platforms where the issue was seen:
    780 	 * Lenovo Thinkpad X301, X61s, X60, T60, X41
    781 	 * Fujitsu FSC S7110
    782 	 * Acer Aspire 1830T
    783 	 */
    784 	if (!(hibernation && INTEL_INFO(dev_priv)->gen < 6))
    785 		pci_set_power_state(drm_dev->pdev, PCI_D3hot);
    786 #endif
    787 
    788 	return 0;
    789 }
    790 
    791 int i915_suspend_switcheroo(struct drm_device *dev, pm_message_t state)
    792 {
    793 	int error;
    794 
    795 	if (!dev || !dev->dev_private) {
    796 		DRM_ERROR("dev: %p\n", dev);
    797 		DRM_ERROR("DRM not initialized, aborting suspend.\n");
    798 		return -ENODEV;
    799 	}
    800 
    801 	if (WARN_ON_ONCE(state.event != PM_EVENT_SUSPEND &&
    802 			 state.event != PM_EVENT_FREEZE))
    803 		return -EINVAL;
    804 
    805 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
    806 		return 0;
    807 
    808 	error = i915_drm_suspend(dev);
    809 	if (error)
    810 		return error;
    811 
    812 	return i915_drm_suspend_late(dev, false);
    813 }
    814 
    815 int i915_drm_resume(struct drm_device *dev)
    816 {
    817 	struct drm_i915_private *dev_priv = dev->dev_private;
    818 
    819 	mutex_lock(&dev->struct_mutex);
    820 	i915_gem_restore_gtt_mappings(dev);
    821 	mutex_unlock(&dev->struct_mutex);
    822 
    823 	i915_restore_state(dev);
    824 	intel_opregion_setup(dev);
    825 
    826 	intel_init_pch_refclk(dev);
    827 	drm_mode_config_reset(dev);
    828 
    829 	/*
    830 	 * Interrupts have to be enabled before any batches are run. If not the
    831 	 * GPU will hang. i915_gem_init_hw() will initiate batches to
    832 	 * update/restore the context.
    833 	 *
    834 	 * Modeset enabling in intel_modeset_init_hw() also needs working
    835 	 * interrupts.
    836 	 */
    837 	intel_runtime_pm_enable_interrupts(dev_priv);
    838 
    839 	mutex_lock(&dev->struct_mutex);
    840 	if (i915_gem_init_hw(dev)) {
    841 		DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
    842 			atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
    843 	}
    844 	mutex_unlock(&dev->struct_mutex);
    845 
    846 	intel_guc_resume(dev);
    847 
    848 	intel_modeset_init_hw(dev);
    849 
    850 	spin_lock_irq(&dev_priv->irq_lock);
    851 	if (dev_priv->display.hpd_irq_setup)
    852 		dev_priv->display.hpd_irq_setup(dev);
    853 	spin_unlock_irq(&dev_priv->irq_lock);
    854 
    855 	drm_modeset_lock_all(dev);
    856 	intel_display_resume(dev);
    857 	drm_modeset_unlock_all(dev);
    858 
    859 	intel_dp_mst_resume(dev);
    860 
    861 	/*
    862 	 * ... but also need to make sure that hotplug processing
    863 	 * doesn't cause havoc. Like in the driver load code we don't
    864 	 * bother with the tiny race here where we might loose hotplug
    865 	 * notifications.
    866 	 * */
    867 	intel_hpd_init(dev_priv);
    868 	/* Config may have changed between suspend and resume */
    869 	drm_helper_hpd_irq_event(dev);
    870 
    871 	intel_opregion_init(dev);
    872 
    873 #ifndef __NetBSD__		/* XXX fb */
    874 	intel_fbdev_set_suspend(dev, FBINFO_STATE_RUNNING, false);
    875 #endif
    876 
    877 	mutex_lock(&dev_priv->modeset_restore_lock);
    878 	dev_priv->modeset_restore = MODESET_DONE;
    879 	mutex_unlock(&dev_priv->modeset_restore_lock);
    880 
    881 	intel_opregion_notify_adapter(dev, PCI_D0);
    882 
    883 	drm_kms_helper_poll_enable(dev);
    884 
    885 	return 0;
    886 }
    887 
    888 int i915_drm_resume_early(struct drm_device *dev)
    889 {
    890 	struct drm_i915_private *dev_priv = dev->dev_private;
    891 	int ret = 0;
    892 
    893 #ifndef __NetBSD__		/* pmf handles this for us.  */
    894 	/*
    895 	 * We have a resume ordering issue with the snd-hda driver also
    896 	 * requiring our device to be power up. Due to the lack of a
    897 	 * parent/child relationship we currently solve this with an early
    898 	 * resume hook.
    899 	 *
    900 	 * FIXME: This should be solved with a special hdmi sink device or
    901 	 * similar so that power domains can be employed.
    902 	 */
    903 	if (pci_enable_device(dev->pdev))
    904 		return -EIO;
    905 #endif
    906 
    907 	/* XXX pmf probably handles this for us too.  */
    908 	pci_set_master(dev->pdev);
    909 
    910 	if (IS_VALLEYVIEW(dev_priv))
    911 		ret = vlv_resume_prepare(dev_priv, false);
    912 	if (ret)
    913 		DRM_ERROR("Resume prepare failed: %d, continuing anyway\n",
    914 			  ret);
    915 
    916 	intel_uncore_early_sanitize(dev, true);
    917 
    918 	if (IS_BROXTON(dev))
    919 		ret = bxt_resume_prepare(dev_priv);
    920 	else if (IS_SKYLAKE(dev_priv))
    921 		ret = skl_resume_prepare(dev_priv);
    922 	else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
    923 		hsw_disable_pc8(dev_priv);
    924 
    925 	intel_uncore_sanitize(dev);
    926 	intel_power_domains_init_hw(dev_priv);
    927 
    928 	i915_rc6_ctx_wa_resume(dev_priv);
    929 
    930 	return ret;
    931 }
    932 
    933 int i915_resume_switcheroo(struct drm_device *dev)
    934 {
    935 	int ret;
    936 
    937 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
    938 		return 0;
    939 
    940 	ret = i915_drm_resume_early(dev);
    941 	if (ret)
    942 		return ret;
    943 
    944 	return i915_drm_resume(dev);
    945 }
    946 
    947 /**
    948  * i915_reset - reset chip after a hang
    949  * @dev: drm device to reset
    950  *
    951  * Reset the chip.  Useful if a hang is detected. Returns zero on successful
    952  * reset or otherwise an error code.
    953  *
    954  * Procedure is fairly simple:
    955  *   - reset the chip using the reset reg
    956  *   - re-init context state
    957  *   - re-init hardware status page
    958  *   - re-init ring buffer
    959  *   - re-init interrupt state
    960  *   - re-init display
    961  */
    962 int i915_reset(struct drm_device *dev)
    963 {
    964 	struct drm_i915_private *dev_priv = dev->dev_private;
    965 	bool simulated;
    966 	int ret;
    967 
    968 	intel_reset_gt_powersave(dev);
    969 
    970 	mutex_lock(&dev->struct_mutex);
    971 
    972 	i915_gem_reset(dev);
    973 
    974 	simulated = dev_priv->gpu_error.stop_rings != 0;
    975 
    976 	ret = intel_gpu_reset(dev);
    977 
    978 	/* Also reset the gpu hangman. */
    979 	if (simulated) {
    980 		DRM_INFO("Simulated gpu hang, resetting stop_rings\n");
    981 		dev_priv->gpu_error.stop_rings = 0;
    982 		if (ret == -ENODEV) {
    983 			DRM_INFO("Reset not implemented, but ignoring "
    984 				 "error for simulated gpu hangs\n");
    985 			ret = 0;
    986 		}
    987 	}
    988 
    989 	if (i915_stop_ring_allow_warn(dev_priv))
    990 		pr_notice("drm/i915: Resetting chip after gpu hang\n");
    991 
    992 	if (ret) {
    993 		DRM_ERROR("Failed to reset chip: %i\n", ret);
    994 		mutex_unlock(&dev->struct_mutex);
    995 		return ret;
    996 	}
    997 
    998 	intel_overlay_reset(dev_priv);
    999 
   1000 	/* Ok, now get things going again... */
   1001 
   1002 	/*
   1003 	 * Everything depends on having the GTT running, so we need to start
   1004 	 * there.  Fortunately we don't need to do this unless we reset the
   1005 	 * chip at a PCI level.
   1006 	 *
   1007 	 * Next we need to restore the context, but we don't use those
   1008 	 * yet either...
   1009 	 *
   1010 	 * Ring buffer needs to be re-initialized in the KMS case, or if X
   1011 	 * was running at the time of the reset (i.e. we weren't VT
   1012 	 * switched away).
   1013 	 */
   1014 
   1015 	/* Used to prevent gem_check_wedged returning -EAGAIN during gpu reset */
   1016 	dev_priv->gpu_error.reload_in_reset = true;
   1017 
   1018 	ret = i915_gem_init_hw(dev);
   1019 
   1020 	dev_priv->gpu_error.reload_in_reset = false;
   1021 
   1022 	mutex_unlock(&dev->struct_mutex);
   1023 	if (ret) {
   1024 		DRM_ERROR("Failed hw init on reset %d\n", ret);
   1025 		return ret;
   1026 	}
   1027 
   1028 	/*
   1029 	 * rps/rc6 re-init is necessary to restore state lost after the
   1030 	 * reset and the re-install of gt irqs. Skip for ironlake per
   1031 	 * previous concerns that it doesn't respond well to some forms
   1032 	 * of re-init after reset.
   1033 	 */
   1034 	if (INTEL_INFO(dev)->gen > 5)
   1035 		intel_enable_gt_powersave(dev);
   1036 
   1037 	return 0;
   1038 }
   1039 
   1040 #ifdef __NetBSD__
   1041 
   1042 static const struct uvm_pagerops i915_gem_uvm_ops = {
   1043 	.pgo_reference = drm_gem_pager_reference,
   1044 	.pgo_detach = drm_gem_pager_detach,
   1045 	.pgo_fault = i915_gem_fault,
   1046 };
   1047 
   1048 #else
   1049 
   1050 static int i915_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
   1051 {
   1052 	struct intel_device_info *intel_info =
   1053 		(struct intel_device_info *) ent->driver_data;
   1054 
   1055 	if (IS_PRELIMINARY_HW(intel_info) && !i915.preliminary_hw_support) {
   1056 		DRM_INFO("This hardware requires preliminary hardware support.\n"
   1057 			 "See CONFIG_DRM_I915_PRELIMINARY_HW_SUPPORT, and/or modparam preliminary_hw_support\n");
   1058 		return -ENODEV;
   1059 	}
   1060 
   1061 	/* Only bind to function 0 of the device. Early generations
   1062 	 * used function 1 as a placeholder for multi-head. This causes
   1063 	 * us confusion instead, especially on the systems where both
   1064 	 * functions have the same PCI-ID!
   1065 	 */
   1066 	if (PCI_FUNC(pdev->devfn))
   1067 		return -ENODEV;
   1068 
   1069 	return drm_get_pci_dev(pdev, ent, &driver);
   1070 }
   1071 
   1072 static void
   1073 i915_pci_remove(struct pci_dev *pdev)
   1074 {
   1075 	struct drm_device *dev = pci_get_drvdata(pdev);
   1076 
   1077 	drm_put_dev(dev);
   1078 }
   1079 #endif
   1080 
   1081 #ifndef __NetBSD__
   1082 static int i915_pm_suspend(struct device *dev)
   1083 {
   1084 	struct pci_dev *pdev = to_pci_dev(dev);
   1085 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
   1086 
   1087 	if (!drm_dev || !drm_dev->dev_private) {
   1088 		dev_err(dev, "DRM not initialized, aborting suspend.\n");
   1089 		return -ENODEV;
   1090 	}
   1091 
   1092 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1093 		return 0;
   1094 
   1095 	return i915_drm_suspend(drm_dev);
   1096 }
   1097 
   1098 static int i915_pm_suspend_late(struct device *dev)
   1099 {
   1100 	struct drm_device *drm_dev = dev_to_i915(dev)->dev;
   1101 
   1102 	/*
   1103 	 * We have a suspend ordering issue with the snd-hda driver also
   1104 	 * requiring our device to be power up. Due to the lack of a
   1105 	 * parent/child relationship we currently solve this with an late
   1106 	 * suspend hook.
   1107 	 *
   1108 	 * FIXME: This should be solved with a special hdmi sink device or
   1109 	 * similar so that power domains can be employed.
   1110 	 */
   1111 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1112 		return 0;
   1113 
   1114 	return i915_drm_suspend_late(drm_dev, false);
   1115 }
   1116 
   1117 static int i915_pm_poweroff_late(struct device *dev)
   1118 {
   1119 	struct drm_device *drm_dev = dev_to_i915(dev)->dev;
   1120 
   1121 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1122 		return 0;
   1123 
   1124 	return i915_drm_suspend_late(drm_dev, true);
   1125 }
   1126 
   1127 static int i915_pm_resume_early(struct device *dev)
   1128 {
   1129 	struct drm_device *drm_dev = dev_to_i915(dev)->dev;
   1130 
   1131 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1132 		return 0;
   1133 
   1134 	return i915_drm_resume_early(drm_dev);
   1135 }
   1136 
   1137 static int i915_pm_resume(struct device *dev)
   1138 {
   1139 	struct drm_device *drm_dev = dev_to_i915(dev)->dev;
   1140 
   1141 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
   1142 		return 0;
   1143 
   1144 	return i915_drm_resume(drm_dev);
   1145 }
   1146 #endif
   1147 
   1148 static int skl_suspend_complete(struct drm_i915_private *dev_priv)
   1149 {
   1150 	/* Enabling DC6 is not a hard requirement to enter runtime D3 */
   1151 
   1152 	skl_uninit_cdclk(dev_priv);
   1153 
   1154 	return 0;
   1155 }
   1156 
   1157 static int hsw_suspend_complete(struct drm_i915_private *dev_priv)
   1158 {
   1159 	hsw_enable_pc8(dev_priv);
   1160 
   1161 	return 0;
   1162 }
   1163 
   1164 static int bxt_suspend_complete(struct drm_i915_private *dev_priv)
   1165 {
   1166 	struct drm_device *dev = dev_priv->dev;
   1167 
   1168 	/* TODO: when DC5 support is added disable DC5 here. */
   1169 
   1170 	broxton_ddi_phy_uninit(dev);
   1171 	broxton_uninit_cdclk(dev);
   1172 	bxt_enable_dc9(dev_priv);
   1173 
   1174 	return 0;
   1175 }
   1176 
   1177 static int bxt_resume_prepare(struct drm_i915_private *dev_priv)
   1178 {
   1179 	struct drm_device *dev = dev_priv->dev;
   1180 
   1181 	/* TODO: when CSR FW support is added make sure the FW is loaded */
   1182 
   1183 	bxt_disable_dc9(dev_priv);
   1184 
   1185 	/*
   1186 	 * TODO: when DC5 support is added enable DC5 here if the CSR FW
   1187 	 * is available.
   1188 	 */
   1189 	broxton_init_cdclk(dev);
   1190 	broxton_ddi_phy_init(dev);
   1191 	intel_prepare_ddi(dev);
   1192 
   1193 	return 0;
   1194 }
   1195 
   1196 static int skl_resume_prepare(struct drm_i915_private *dev_priv)
   1197 {
   1198 	struct drm_device *dev = dev_priv->dev;
   1199 
   1200 	skl_init_cdclk(dev_priv);
   1201 	intel_csr_load_program(dev);
   1202 
   1203 	return 0;
   1204 }
   1205 
   1206 /*
   1207  * Save all Gunit registers that may be lost after a D3 and a subsequent
   1208  * S0i[R123] transition. The list of registers needing a save/restore is
   1209  * defined in the VLV2_S0IXRegs document. This documents marks all Gunit
   1210  * registers in the following way:
   1211  * - Driver: saved/restored by the driver
   1212  * - Punit : saved/restored by the Punit firmware
   1213  * - No, w/o marking: no need to save/restore, since the register is R/O or
   1214  *                    used internally by the HW in a way that doesn't depend
   1215  *                    keeping the content across a suspend/resume.
   1216  * - Debug : used for debugging
   1217  *
   1218  * We save/restore all registers marked with 'Driver', with the following
   1219  * exceptions:
   1220  * - Registers out of use, including also registers marked with 'Debug'.
   1221  *   These have no effect on the driver's operation, so we don't save/restore
   1222  *   them to reduce the overhead.
   1223  * - Registers that are fully setup by an initialization function called from
   1224  *   the resume path. For example many clock gating and RPS/RC6 registers.
   1225  * - Registers that provide the right functionality with their reset defaults.
   1226  *
   1227  * TODO: Except for registers that based on the above 3 criteria can be safely
   1228  * ignored, we save/restore all others, practically treating the HW context as
   1229  * a black-box for the driver. Further investigation is needed to reduce the
   1230  * saved/restored registers even further, by following the same 3 criteria.
   1231  */
   1232 static void vlv_save_gunit_s0ix_state(struct drm_i915_private *dev_priv)
   1233 {
   1234 	struct vlv_s0ix_state *s = &dev_priv->vlv_s0ix_state;
   1235 	int i;
   1236 
   1237 	/* GAM 0x4000-0x4770 */
   1238 	s->wr_watermark		= I915_READ(GEN7_WR_WATERMARK);
   1239 	s->gfx_prio_ctrl	= I915_READ(GEN7_GFX_PRIO_CTRL);
   1240 	s->arb_mode		= I915_READ(ARB_MODE);
   1241 	s->gfx_pend_tlb0	= I915_READ(GEN7_GFX_PEND_TLB0);
   1242 	s->gfx_pend_tlb1	= I915_READ(GEN7_GFX_PEND_TLB1);
   1243 
   1244 	for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
   1245 		s->lra_limits[i] = I915_READ(GEN7_LRA_LIMITS(i));
   1246 
   1247 	s->media_max_req_count	= I915_READ(GEN7_MEDIA_MAX_REQ_COUNT);
   1248 	s->gfx_max_req_count	= I915_READ(GEN7_GFX_MAX_REQ_COUNT);
   1249 
   1250 	s->render_hwsp		= I915_READ(RENDER_HWS_PGA_GEN7);
   1251 	s->ecochk		= I915_READ(GAM_ECOCHK);
   1252 	s->bsd_hwsp		= I915_READ(BSD_HWS_PGA_GEN7);
   1253 	s->blt_hwsp		= I915_READ(BLT_HWS_PGA_GEN7);
   1254 
   1255 	s->tlb_rd_addr		= I915_READ(GEN7_TLB_RD_ADDR);
   1256 
   1257 	/* MBC 0x9024-0x91D0, 0x8500 */
   1258 	s->g3dctl		= I915_READ(VLV_G3DCTL);
   1259 	s->gsckgctl		= I915_READ(VLV_GSCKGCTL);
   1260 	s->mbctl		= I915_READ(GEN6_MBCTL);
   1261 
   1262 	/* GCP 0x9400-0x9424, 0x8100-0x810C */
   1263 	s->ucgctl1		= I915_READ(GEN6_UCGCTL1);
   1264 	s->ucgctl3		= I915_READ(GEN6_UCGCTL3);
   1265 	s->rcgctl1		= I915_READ(GEN6_RCGCTL1);
   1266 	s->rcgctl2		= I915_READ(GEN6_RCGCTL2);
   1267 	s->rstctl		= I915_READ(GEN6_RSTCTL);
   1268 	s->misccpctl		= I915_READ(GEN7_MISCCPCTL);
   1269 
   1270 	/* GPM 0xA000-0xAA84, 0x8000-0x80FC */
   1271 	s->gfxpause		= I915_READ(GEN6_GFXPAUSE);
   1272 	s->rpdeuhwtc		= I915_READ(GEN6_RPDEUHWTC);
   1273 	s->rpdeuc		= I915_READ(GEN6_RPDEUC);
   1274 	s->ecobus		= I915_READ(ECOBUS);
   1275 	s->pwrdwnupctl		= I915_READ(VLV_PWRDWNUPCTL);
   1276 	s->rp_down_timeout	= I915_READ(GEN6_RP_DOWN_TIMEOUT);
   1277 	s->rp_deucsw		= I915_READ(GEN6_RPDEUCSW);
   1278 	s->rcubmabdtmr		= I915_READ(GEN6_RCUBMABDTMR);
   1279 	s->rcedata		= I915_READ(VLV_RCEDATA);
   1280 	s->spare2gh		= I915_READ(VLV_SPAREG2H);
   1281 
   1282 	/* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
   1283 	s->gt_imr		= I915_READ(GTIMR);
   1284 	s->gt_ier		= I915_READ(GTIER);
   1285 	s->pm_imr		= I915_READ(GEN6_PMIMR);
   1286 	s->pm_ier		= I915_READ(GEN6_PMIER);
   1287 
   1288 	for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
   1289 		s->gt_scratch[i] = I915_READ(GEN7_GT_SCRATCH(i));
   1290 
   1291 	/* GT SA CZ domain, 0x100000-0x138124 */
   1292 	s->tilectl		= I915_READ(TILECTL);
   1293 	s->gt_fifoctl		= I915_READ(GTFIFOCTL);
   1294 	s->gtlc_wake_ctrl	= I915_READ(VLV_GTLC_WAKE_CTRL);
   1295 	s->gtlc_survive		= I915_READ(VLV_GTLC_SURVIVABILITY_REG);
   1296 	s->pmwgicz		= I915_READ(VLV_PMWGICZ);
   1297 
   1298 	/* Gunit-Display CZ domain, 0x182028-0x1821CF */
   1299 	s->gu_ctl0		= I915_READ(VLV_GU_CTL0);
   1300 	s->gu_ctl1		= I915_READ(VLV_GU_CTL1);
   1301 	s->pcbr			= I915_READ(VLV_PCBR);
   1302 	s->clock_gate_dis2	= I915_READ(VLV_GUNIT_CLOCK_GATE2);
   1303 
   1304 	/*
   1305 	 * Not saving any of:
   1306 	 * DFT,		0x9800-0x9EC0
   1307 	 * SARB,	0xB000-0xB1FC
   1308 	 * GAC,		0x5208-0x524C, 0x14000-0x14C000
   1309 	 * PCI CFG
   1310 	 */
   1311 }
   1312 
   1313 static void vlv_restore_gunit_s0ix_state(struct drm_i915_private *dev_priv)
   1314 {
   1315 	struct vlv_s0ix_state *s = &dev_priv->vlv_s0ix_state;
   1316 	u32 val;
   1317 	int i;
   1318 
   1319 	/* GAM 0x4000-0x4770 */
   1320 	I915_WRITE(GEN7_WR_WATERMARK,	s->wr_watermark);
   1321 	I915_WRITE(GEN7_GFX_PRIO_CTRL,	s->gfx_prio_ctrl);
   1322 	I915_WRITE(ARB_MODE,		s->arb_mode | (0xffff << 16));
   1323 	I915_WRITE(GEN7_GFX_PEND_TLB0,	s->gfx_pend_tlb0);
   1324 	I915_WRITE(GEN7_GFX_PEND_TLB1,	s->gfx_pend_tlb1);
   1325 
   1326 	for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
   1327 		I915_WRITE(GEN7_LRA_LIMITS(i), s->lra_limits[i]);
   1328 
   1329 	I915_WRITE(GEN7_MEDIA_MAX_REQ_COUNT, s->media_max_req_count);
   1330 	I915_WRITE(GEN7_GFX_MAX_REQ_COUNT, s->gfx_max_req_count);
   1331 
   1332 	I915_WRITE(RENDER_HWS_PGA_GEN7,	s->render_hwsp);
   1333 	I915_WRITE(GAM_ECOCHK,		s->ecochk);
   1334 	I915_WRITE(BSD_HWS_PGA_GEN7,	s->bsd_hwsp);
   1335 	I915_WRITE(BLT_HWS_PGA_GEN7,	s->blt_hwsp);
   1336 
   1337 	I915_WRITE(GEN7_TLB_RD_ADDR,	s->tlb_rd_addr);
   1338 
   1339 	/* MBC 0x9024-0x91D0, 0x8500 */
   1340 	I915_WRITE(VLV_G3DCTL,		s->g3dctl);
   1341 	I915_WRITE(VLV_GSCKGCTL,	s->gsckgctl);
   1342 	I915_WRITE(GEN6_MBCTL,		s->mbctl);
   1343 
   1344 	/* GCP 0x9400-0x9424, 0x8100-0x810C */
   1345 	I915_WRITE(GEN6_UCGCTL1,	s->ucgctl1);
   1346 	I915_WRITE(GEN6_UCGCTL3,	s->ucgctl3);
   1347 	I915_WRITE(GEN6_RCGCTL1,	s->rcgctl1);
   1348 	I915_WRITE(GEN6_RCGCTL2,	s->rcgctl2);
   1349 	I915_WRITE(GEN6_RSTCTL,		s->rstctl);
   1350 	I915_WRITE(GEN7_MISCCPCTL,	s->misccpctl);
   1351 
   1352 	/* GPM 0xA000-0xAA84, 0x8000-0x80FC */
   1353 	I915_WRITE(GEN6_GFXPAUSE,	s->gfxpause);
   1354 	I915_WRITE(GEN6_RPDEUHWTC,	s->rpdeuhwtc);
   1355 	I915_WRITE(GEN6_RPDEUC,		s->rpdeuc);
   1356 	I915_WRITE(ECOBUS,		s->ecobus);
   1357 	I915_WRITE(VLV_PWRDWNUPCTL,	s->pwrdwnupctl);
   1358 	I915_WRITE(GEN6_RP_DOWN_TIMEOUT,s->rp_down_timeout);
   1359 	I915_WRITE(GEN6_RPDEUCSW,	s->rp_deucsw);
   1360 	I915_WRITE(GEN6_RCUBMABDTMR,	s->rcubmabdtmr);
   1361 	I915_WRITE(VLV_RCEDATA,		s->rcedata);
   1362 	I915_WRITE(VLV_SPAREG2H,	s->spare2gh);
   1363 
   1364 	/* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
   1365 	I915_WRITE(GTIMR,		s->gt_imr);
   1366 	I915_WRITE(GTIER,		s->gt_ier);
   1367 	I915_WRITE(GEN6_PMIMR,		s->pm_imr);
   1368 	I915_WRITE(GEN6_PMIER,		s->pm_ier);
   1369 
   1370 	for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
   1371 		I915_WRITE(GEN7_GT_SCRATCH(i), s->gt_scratch[i]);
   1372 
   1373 	/* GT SA CZ domain, 0x100000-0x138124 */
   1374 	I915_WRITE(TILECTL,			s->tilectl);
   1375 	I915_WRITE(GTFIFOCTL,			s->gt_fifoctl);
   1376 	/*
   1377 	 * Preserve the GT allow wake and GFX force clock bit, they are not
   1378 	 * be restored, as they are used to control the s0ix suspend/resume
   1379 	 * sequence by the caller.
   1380 	 */
   1381 	val = I915_READ(VLV_GTLC_WAKE_CTRL);
   1382 	val &= VLV_GTLC_ALLOWWAKEREQ;
   1383 	val |= s->gtlc_wake_ctrl & ~VLV_GTLC_ALLOWWAKEREQ;
   1384 	I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
   1385 
   1386 	val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
   1387 	val &= VLV_GFX_CLK_FORCE_ON_BIT;
   1388 	val |= s->gtlc_survive & ~VLV_GFX_CLK_FORCE_ON_BIT;
   1389 	I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
   1390 
   1391 	I915_WRITE(VLV_PMWGICZ,			s->pmwgicz);
   1392 
   1393 	/* Gunit-Display CZ domain, 0x182028-0x1821CF */
   1394 	I915_WRITE(VLV_GU_CTL0,			s->gu_ctl0);
   1395 	I915_WRITE(VLV_GU_CTL1,			s->gu_ctl1);
   1396 	I915_WRITE(VLV_PCBR,			s->pcbr);
   1397 	I915_WRITE(VLV_GUNIT_CLOCK_GATE2,	s->clock_gate_dis2);
   1398 }
   1399 
   1400 int vlv_force_gfx_clock(struct drm_i915_private *dev_priv, bool force_on)
   1401 {
   1402 	u32 val;
   1403 	int err;
   1404 
   1405 #define COND (I915_READ(VLV_GTLC_SURVIVABILITY_REG) & VLV_GFX_CLK_STATUS_BIT)
   1406 
   1407 	val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
   1408 	val &= ~VLV_GFX_CLK_FORCE_ON_BIT;
   1409 	if (force_on)
   1410 		val |= VLV_GFX_CLK_FORCE_ON_BIT;
   1411 	I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
   1412 
   1413 	if (!force_on)
   1414 		return 0;
   1415 
   1416 	err = wait_for(COND, 20);
   1417 	if (err)
   1418 		DRM_ERROR("timeout waiting for GFX clock force-on (%08x)\n",
   1419 			  I915_READ(VLV_GTLC_SURVIVABILITY_REG));
   1420 
   1421 	return err;
   1422 #undef COND
   1423 }
   1424 
   1425 static int vlv_allow_gt_wake(struct drm_i915_private *dev_priv, bool allow)
   1426 {
   1427 	u32 val;
   1428 	int err = 0;
   1429 
   1430 	val = I915_READ(VLV_GTLC_WAKE_CTRL);
   1431 	val &= ~VLV_GTLC_ALLOWWAKEREQ;
   1432 	if (allow)
   1433 		val |= VLV_GTLC_ALLOWWAKEREQ;
   1434 	I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
   1435 	POSTING_READ(VLV_GTLC_WAKE_CTRL);
   1436 
   1437 #define COND (!!(I915_READ(VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEACK) == \
   1438 	      allow)
   1439 	err = wait_for(COND, 1);
   1440 	if (err)
   1441 		DRM_ERROR("timeout disabling GT waking\n");
   1442 	return err;
   1443 #undef COND
   1444 }
   1445 
   1446 static int vlv_wait_for_gt_wells(struct drm_i915_private *dev_priv,
   1447 				 bool wait_for_on)
   1448 {
   1449 	u32 mask;
   1450 	u32 val;
   1451 	int err;
   1452 
   1453 	mask = VLV_GTLC_PW_MEDIA_STATUS_MASK | VLV_GTLC_PW_RENDER_STATUS_MASK;
   1454 	val = wait_for_on ? mask : 0;
   1455 #define COND ((I915_READ(VLV_GTLC_PW_STATUS) & mask) == val)
   1456 	if (COND)
   1457 		return 0;
   1458 
   1459 	DRM_DEBUG_KMS("waiting for GT wells to go %s (%08x)\n",
   1460 			wait_for_on ? "on" : "off",
   1461 			I915_READ(VLV_GTLC_PW_STATUS));
   1462 
   1463 	/*
   1464 	 * RC6 transitioning can be delayed up to 2 msec (see
   1465 	 * valleyview_enable_rps), use 3 msec for safety.
   1466 	 */
   1467 	err = wait_for(COND, 3);
   1468 	if (err)
   1469 		DRM_ERROR("timeout waiting for GT wells to go %s\n",
   1470 			  wait_for_on ? "on" : "off");
   1471 
   1472 	return err;
   1473 #undef COND
   1474 }
   1475 
   1476 static void vlv_check_no_gt_access(struct drm_i915_private *dev_priv)
   1477 {
   1478 	if (!(I915_READ(VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEERR))
   1479 		return;
   1480 
   1481 	DRM_ERROR("GT register access while GT waking disabled\n");
   1482 	I915_WRITE(VLV_GTLC_PW_STATUS, VLV_GTLC_ALLOWWAKEERR);
   1483 }
   1484 
   1485 static int vlv_suspend_complete(struct drm_i915_private *dev_priv)
   1486 {
   1487 	u32 mask;
   1488 	int err;
   1489 
   1490 	/*
   1491 	 * Bspec defines the following GT well on flags as debug only, so
   1492 	 * don't treat them as hard failures.
   1493 	 */
   1494 	(void)vlv_wait_for_gt_wells(dev_priv, false);
   1495 
   1496 	mask = VLV_GTLC_RENDER_CTX_EXISTS | VLV_GTLC_MEDIA_CTX_EXISTS;
   1497 	WARN_ON((I915_READ(VLV_GTLC_WAKE_CTRL) & mask) != mask);
   1498 
   1499 	vlv_check_no_gt_access(dev_priv);
   1500 
   1501 	err = vlv_force_gfx_clock(dev_priv, true);
   1502 	if (err)
   1503 		goto err1;
   1504 
   1505 	err = vlv_allow_gt_wake(dev_priv, false);
   1506 	if (err)
   1507 		goto err2;
   1508 
   1509 	if (!IS_CHERRYVIEW(dev_priv->dev))
   1510 		vlv_save_gunit_s0ix_state(dev_priv);
   1511 
   1512 	err = vlv_force_gfx_clock(dev_priv, false);
   1513 	if (err)
   1514 		goto err2;
   1515 
   1516 	return 0;
   1517 
   1518 err2:
   1519 	/* For safety always re-enable waking and disable gfx clock forcing */
   1520 	vlv_allow_gt_wake(dev_priv, true);
   1521 err1:
   1522 	vlv_force_gfx_clock(dev_priv, false);
   1523 
   1524 	return err;
   1525 }
   1526 
   1527 static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
   1528 				bool rpm_resume)
   1529 {
   1530 	struct drm_device *dev = dev_priv->dev;
   1531 	int err;
   1532 	int ret;
   1533 
   1534 	/*
   1535 	 * If any of the steps fail just try to continue, that's the best we
   1536 	 * can do at this point. Return the first error code (which will also
   1537 	 * leave RPM permanently disabled).
   1538 	 */
   1539 	ret = vlv_force_gfx_clock(dev_priv, true);
   1540 
   1541 	if (!IS_CHERRYVIEW(dev_priv->dev))
   1542 		vlv_restore_gunit_s0ix_state(dev_priv);
   1543 
   1544 	err = vlv_allow_gt_wake(dev_priv, true);
   1545 	if (!ret)
   1546 		ret = err;
   1547 
   1548 	err = vlv_force_gfx_clock(dev_priv, false);
   1549 	if (!ret)
   1550 		ret = err;
   1551 
   1552 	vlv_check_no_gt_access(dev_priv);
   1553 
   1554 	if (rpm_resume) {
   1555 		intel_init_clock_gating(dev);
   1556 		i915_gem_restore_fences(dev);
   1557 	}
   1558 
   1559 	return ret;
   1560 }
   1561 
   1562 #ifndef __NetBSD__		/* XXX runtime pm */
   1563 static int intel_runtime_suspend(struct device *device)
   1564 {
   1565 	struct pci_dev *pdev = to_pci_dev(device);
   1566 	struct drm_device *dev = pci_get_drvdata(pdev);
   1567 	struct drm_i915_private *dev_priv = dev->dev_private;
   1568 	int ret;
   1569 
   1570 	if (WARN_ON_ONCE(!(dev_priv->rps.enabled && intel_enable_rc6(dev))))
   1571 		return -ENODEV;
   1572 
   1573 	if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev)))
   1574 		return -ENODEV;
   1575 
   1576 	DRM_DEBUG_KMS("Suspending device\n");
   1577 
   1578 	/*
   1579 	 * We could deadlock here in case another thread holding struct_mutex
   1580 	 * calls RPM suspend concurrently, since the RPM suspend will wait
   1581 	 * first for this RPM suspend to finish. In this case the concurrent
   1582 	 * RPM resume will be followed by its RPM suspend counterpart. Still
   1583 	 * for consistency return -EAGAIN, which will reschedule this suspend.
   1584 	 */
   1585 	if (!mutex_trylock(&dev->struct_mutex)) {
   1586 		DRM_DEBUG_KMS("device lock contention, deffering suspend\n");
   1587 		/*
   1588 		 * Bump the expiration timestamp, otherwise the suspend won't
   1589 		 * be rescheduled.
   1590 		 */
   1591 		pm_runtime_mark_last_busy(device);
   1592 
   1593 		return -EAGAIN;
   1594 	}
   1595 	/*
   1596 	 * We are safe here against re-faults, since the fault handler takes
   1597 	 * an RPM reference.
   1598 	 */
   1599 	i915_gem_release_all_mmaps(dev_priv);
   1600 	mutex_unlock(&dev->struct_mutex);
   1601 
   1602 	intel_guc_suspend(dev);
   1603 
   1604 	intel_suspend_gt_powersave(dev);
   1605 	intel_runtime_pm_disable_interrupts(dev_priv);
   1606 
   1607 	ret = intel_suspend_complete(dev_priv);
   1608 	if (ret) {
   1609 		DRM_ERROR("Runtime suspend failed, disabling it (%d)\n", ret);
   1610 		intel_runtime_pm_enable_interrupts(dev_priv);
   1611 
   1612 		return ret;
   1613 	}
   1614 
   1615 	cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
   1616 	intel_uncore_forcewake_reset(dev, false);
   1617 	dev_priv->pm.suspended = true;
   1618 
   1619 	/*
   1620 	 * FIXME: We really should find a document that references the arguments
   1621 	 * used below!
   1622 	 */
   1623 	if (IS_BROADWELL(dev)) {
   1624 		/*
   1625 		 * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop
   1626 		 * being detected, and the call we do at intel_runtime_resume()
   1627 		 * won't be able to restore them. Since PCI_D3hot matches the
   1628 		 * actual specification and appears to be working, use it.
   1629 		 */
   1630 		intel_opregion_notify_adapter(dev, PCI_D3hot);
   1631 	} else {
   1632 		/*
   1633 		 * current versions of firmware which depend on this opregion
   1634 		 * notification have repurposed the D1 definition to mean
   1635 		 * "runtime suspended" vs. what you would normally expect (D3)
   1636 		 * to distinguish it from notifications that might be sent via
   1637 		 * the suspend path.
   1638 		 */
   1639 		intel_opregion_notify_adapter(dev, PCI_D1);
   1640 	}
   1641 
   1642 	assert_forcewakes_inactive(dev_priv);
   1643 
   1644 	DRM_DEBUG_KMS("Device suspended\n");
   1645 	return 0;
   1646 }
   1647 
   1648 static int intel_runtime_resume(struct device *device)
   1649 {
   1650 	struct pci_dev *pdev = to_pci_dev(device);
   1651 	struct drm_device *dev = pci_get_drvdata(pdev);
   1652 	struct drm_i915_private *dev_priv = dev->dev_private;
   1653 	int ret = 0;
   1654 
   1655 	if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev)))
   1656 		return -ENODEV;
   1657 
   1658 	DRM_DEBUG_KMS("Resuming device\n");
   1659 
   1660 	intel_opregion_notify_adapter(dev, PCI_D0);
   1661 	dev_priv->pm.suspended = false;
   1662 
   1663 	intel_guc_resume(dev);
   1664 
   1665 	if (IS_GEN6(dev_priv))
   1666 		intel_init_pch_refclk(dev);
   1667 
   1668 	if (IS_BROXTON(dev))
   1669 		ret = bxt_resume_prepare(dev_priv);
   1670 	else if (IS_SKYLAKE(dev))
   1671 		ret = skl_resume_prepare(dev_priv);
   1672 	else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
   1673 		hsw_disable_pc8(dev_priv);
   1674 	else if (IS_VALLEYVIEW(dev_priv))
   1675 		ret = vlv_resume_prepare(dev_priv, true);
   1676 
   1677 	/*
   1678 	 * No point of rolling back things in case of an error, as the best
   1679 	 * we can do is to hope that things will still work (and disable RPM).
   1680 	 */
   1681 	i915_gem_init_swizzling(dev);
   1682 	gen6_update_ring_freq(dev);
   1683 
   1684 	intel_runtime_pm_enable_interrupts(dev_priv);
   1685 
   1686 	/*
   1687 	 * On VLV/CHV display interrupts are part of the display
   1688 	 * power well, so hpd is reinitialized from there. For
   1689 	 * everyone else do it here.
   1690 	 */
   1691 	if (!IS_VALLEYVIEW(dev_priv))
   1692 		intel_hpd_init(dev_priv);
   1693 
   1694 	intel_enable_gt_powersave(dev);
   1695 
   1696 	if (ret)
   1697 		DRM_ERROR("Runtime resume failed, disabling it (%d)\n", ret);
   1698 	else
   1699 		DRM_DEBUG_KMS("Device resumed\n");
   1700 
   1701 	return ret;
   1702 }
   1703 #endif
   1704 
   1705 /*
   1706  * This function implements common functionality of runtime and system
   1707  * suspend sequence.
   1708  */
   1709 static int intel_suspend_complete(struct drm_i915_private *dev_priv)
   1710 {
   1711 	int ret;
   1712 
   1713 	if (IS_BROXTON(dev_priv))
   1714 		ret = bxt_suspend_complete(dev_priv);
   1715 	else if (IS_SKYLAKE(dev_priv))
   1716 		ret = skl_suspend_complete(dev_priv);
   1717 	else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
   1718 		ret = hsw_suspend_complete(dev_priv);
   1719 	else if (IS_VALLEYVIEW(dev_priv))
   1720 		ret = vlv_suspend_complete(dev_priv);
   1721 	else
   1722 		ret = 0;
   1723 
   1724 	return ret;
   1725 }
   1726 
   1727 #ifndef __NetBSD__
   1728 
   1729 static const struct dev_pm_ops i915_pm_ops = {
   1730 	/*
   1731 	 * S0ix (via system suspend) and S3 event handlers [PMSG_SUSPEND,
   1732 	 * PMSG_RESUME]
   1733 	 */
   1734 	.suspend = i915_pm_suspend,
   1735 	.suspend_late = i915_pm_suspend_late,
   1736 	.resume_early = i915_pm_resume_early,
   1737 	.resume = i915_pm_resume,
   1738 
   1739 	/*
   1740 	 * S4 event handlers
   1741 	 * @freeze, @freeze_late    : called (1) before creating the
   1742 	 *                            hibernation image [PMSG_FREEZE] and
   1743 	 *                            (2) after rebooting, before restoring
   1744 	 *                            the image [PMSG_QUIESCE]
   1745 	 * @thaw, @thaw_early       : called (1) after creating the hibernation
   1746 	 *                            image, before writing it [PMSG_THAW]
   1747 	 *                            and (2) after failing to create or
   1748 	 *                            restore the image [PMSG_RECOVER]
   1749 	 * @poweroff, @poweroff_late: called after writing the hibernation
   1750 	 *                            image, before rebooting [PMSG_HIBERNATE]
   1751 	 * @restore, @restore_early : called after rebooting and restoring the
   1752 	 *                            hibernation image [PMSG_RESTORE]
   1753 	 */
   1754 	.freeze = i915_pm_suspend,
   1755 	.freeze_late = i915_pm_suspend_late,
   1756 	.thaw_early = i915_pm_resume_early,
   1757 	.thaw = i915_pm_resume,
   1758 	.poweroff = i915_pm_suspend,
   1759 	.poweroff_late = i915_pm_poweroff_late,
   1760 	.restore_early = i915_pm_resume_early,
   1761 	.restore = i915_pm_resume,
   1762 
   1763 	/* S0ix (via runtime suspend) event handlers */
   1764 	.runtime_suspend = intel_runtime_suspend,
   1765 	.runtime_resume = intel_runtime_resume,
   1766 };
   1767 
   1768 static const struct vm_operations_struct i915_gem_vm_ops = {
   1769 	.fault = i915_gem_fault,
   1770 	.open = drm_gem_vm_open,
   1771 	.close = drm_gem_vm_close,
   1772 };
   1773 
   1774 static const struct file_operations i915_driver_fops = {
   1775 	.owner = THIS_MODULE,
   1776 	.open = drm_open,
   1777 	.release = drm_release,
   1778 	.unlocked_ioctl = drm_ioctl,
   1779 	.mmap = drm_gem_mmap,
   1780 	.poll = drm_poll,
   1781 	.read = drm_read,
   1782 #ifdef CONFIG_COMPAT
   1783 	.compat_ioctl = i915_compat_ioctl,
   1784 #endif
   1785 	.llseek = noop_llseek,
   1786 };
   1787 
   1788 #endif	/* defined(__NetBSD__) */
   1789 
   1790 static struct drm_driver driver = {
   1791 	/* Don't use MTRRs here; the Xserver or userspace app should
   1792 	 * deal with them for Intel hardware.
   1793 	 */
   1794 	.driver_features =
   1795 	    DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM | DRIVER_PRIME |
   1796 	    DRIVER_RENDER | DRIVER_MODESET,
   1797 	.load = i915_driver_load,
   1798 	.unload = i915_driver_unload,
   1799 	.open = i915_driver_open,
   1800 	.lastclose = i915_driver_lastclose,
   1801 	.preclose = i915_driver_preclose,
   1802 	.postclose = i915_driver_postclose,
   1803 	.set_busid = drm_pci_set_busid,
   1804 #ifdef __NetBSD__
   1805 	.request_irq = drm_pci_request_irq,
   1806 	.free_irq = drm_pci_free_irq,
   1807 #endif
   1808 
   1809 #if defined(CONFIG_DEBUG_FS)
   1810 	.debugfs_init = i915_debugfs_init,
   1811 	.debugfs_cleanup = i915_debugfs_cleanup,
   1812 #endif
   1813 	.gem_free_object = i915_gem_free_object,
   1814 #ifdef __NetBSD__
   1815 	/* XXX Not clear the `or legacy' part is important here.  */
   1816 	.mmap_object = &drm_gem_mmap_object,
   1817 	.gem_uvm_ops = &i915_gem_uvm_ops,
   1818 #else
   1819 	.gem_vm_ops = &i915_gem_vm_ops,
   1820 #endif
   1821 
   1822 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
   1823 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
   1824 	.gem_prime_export = i915_gem_prime_export,
   1825 	.gem_prime_import = i915_gem_prime_import,
   1826 
   1827 	.dumb_create = i915_gem_dumb_create,
   1828 	.dumb_map_offset = i915_gem_mmap_gtt,
   1829 	.dumb_destroy = drm_gem_dumb_destroy,
   1830 	.ioctls = i915_ioctls,
   1831 #ifdef __NetBSD__
   1832 	.fops = NULL,
   1833 #else
   1834 	.fops = &i915_driver_fops,
   1835 #endif
   1836 	.name = DRIVER_NAME,
   1837 	.desc = DRIVER_DESC,
   1838 	.date = DRIVER_DATE,
   1839 	.major = DRIVER_MAJOR,
   1840 	.minor = DRIVER_MINOR,
   1841 	.patchlevel = DRIVER_PATCHLEVEL,
   1842 };
   1843 
   1844 #ifndef __NetBSD__
   1845 static struct pci_driver i915_pci_driver = {
   1846 	.name = DRIVER_NAME,
   1847 	.id_table = pciidlist,
   1848 	.probe = i915_pci_probe,
   1849 	.remove = i915_pci_remove,
   1850 	.driver.pm = &i915_pm_ops,
   1851 };
   1852 #endif
   1853 
   1854 #ifndef __NetBSD__
   1855 static int __init i915_init(void)
   1856 {
   1857 	driver.num_ioctls = i915_max_ioctl;
   1858 
   1859 	/*
   1860 	 * Enable KMS by default, unless explicitly overriden by
   1861 	 * either the i915.modeset prarameter or by the
   1862 	 * vga_text_mode_force boot option.
   1863 	 */
   1864 
   1865 	if (i915.modeset == 0)
   1866 		driver.driver_features &= ~DRIVER_MODESET;
   1867 
   1868 #ifdef CONFIG_VGA_CONSOLE
   1869 	if (vgacon_text_force() && i915.modeset == -1)
   1870 		driver.driver_features &= ~DRIVER_MODESET;
   1871 #endif
   1872 
   1873 	if (!(driver.driver_features & DRIVER_MODESET)) {
   1874 		/* Silently fail loading to not upset userspace. */
   1875 		DRM_DEBUG_DRIVER("KMS and UMS disabled.\n");
   1876 		return 0;
   1877 	}
   1878 
   1879 	if (i915.nuclear_pageflip)
   1880 		driver.driver_features |= DRIVER_ATOMIC;
   1881 
   1882 	return drm_pci_init(&driver, &i915_pci_driver);
   1883 }
   1884 
   1885 static void __exit i915_exit(void)
   1886 {
   1887 	if (!(driver.driver_features & DRIVER_MODESET))
   1888 		return; /* Never loaded a driver. */
   1889 
   1890 	drm_pci_exit(&driver, &i915_pci_driver);
   1891 }
   1892 
   1893 module_init(i915_init);
   1894 module_exit(i915_exit);
   1895 #endif
   1896 
   1897 MODULE_AUTHOR("Tungsten Graphics, Inc.");
   1898 MODULE_AUTHOR("Intel Corporation");
   1899 
   1900 MODULE_DESCRIPTION(DRIVER_DESC);
   1901 MODULE_LICENSE("GPL and additional rights");
   1902