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