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