blob: 00633280570226ca255296503d1463739e1ab71d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i915_drv.c -- i830,i845,i855,i865,i915 driver -*- linux-c -*-
2 */
Dave Airlie0d6aa602006-01-02 20:14:23 +11003/*
Dave Airliebc54fd12005-06-23 22:46:46 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * All Rights Reserved.
Dave Airliebc54fd12005-06-23 22:46:46 +10007 *
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 *
Dave Airlie0d6aa602006-01-02 20:14:23 +110028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Jesse Barnese5747e32014-06-12 08:35:47 -070030#include <linux/acpi.h>
Chris Wilson0673ad42016-06-24 14:00:22 +010031#include <linux/device.h>
32#include <linux/oom.h>
33#include <linux/module.h>
34#include <linux/pci.h>
35#include <linux/pm.h>
36#include <linux/pm_runtime.h>
37#include <linux/pnp.h>
38#include <linux/slab.h>
39#include <linux/vgaarb.h>
40#include <linux/vga_switcheroo.h>
41#include <linux/vt.h>
42#include <acpi/video.h>
43
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/drmP.h>
Chris Wilson0673ad42016-06-24 14:00:22 +010045#include <drm/drm_crtc_helper.h>
Maarten Lankhorsta667fb42016-12-15 15:29:44 +010046#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010047#include <drm/i915_drm.h>
Chris Wilson0673ad42016-06-24 14:00:22 +010048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "i915_drv.h"
Chris Wilson990bbda2012-07-02 11:51:02 -030050#include "i915_trace.h"
Chris Wilson0673ad42016-06-24 14:00:22 +010051#include "i915_vgpu.h"
Kenneth Graunkef49f0582010-09-11 01:19:14 -070052#include "intel_drv.h"
Anusha Srivatsa5464cd62017-01-18 08:05:58 -080053#include "intel_uc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Kristian Høgsberg112b7152009-01-04 16:55:33 -050055static struct drm_driver driver;
56
Chris Wilson0673ad42016-06-24 14:00:22 +010057static unsigned int i915_load_fail_count;
58
59bool __i915_inject_load_failure(const char *func, int line)
60{
Michal Wajdeczko4f044a82017-09-19 19:38:44 +000061 if (i915_load_fail_count >= i915_modparams.inject_load_failure)
Chris Wilson0673ad42016-06-24 14:00:22 +010062 return false;
63
Michal Wajdeczko4f044a82017-09-19 19:38:44 +000064 if (++i915_load_fail_count == i915_modparams.inject_load_failure) {
Chris Wilson0673ad42016-06-24 14:00:22 +010065 DRM_INFO("Injecting failure at checkpoint %u [%s:%d]\n",
Michal Wajdeczko4f044a82017-09-19 19:38:44 +000066 i915_modparams.inject_load_failure, func, line);
Chris Wilson0673ad42016-06-24 14:00:22 +010067 return true;
68 }
69
70 return false;
71}
72
73#define FDO_BUG_URL "https://bugs.freedesktop.org/enter_bug.cgi?product=DRI"
74#define FDO_BUG_MSG "Please file a bug at " FDO_BUG_URL " against DRM/Intel " \
75 "providing the dmesg log by booting with drm.debug=0xf"
76
77void
78__i915_printk(struct drm_i915_private *dev_priv, const char *level,
79 const char *fmt, ...)
80{
81 static bool shown_bug_once;
David Weinehallc49d13e2016-08-22 13:32:42 +030082 struct device *kdev = dev_priv->drm.dev;
Chris Wilson0673ad42016-06-24 14:00:22 +010083 bool is_error = level[1] <= KERN_ERR[1];
84 bool is_debug = level[1] == KERN_DEBUG[1];
85 struct va_format vaf;
86 va_list args;
87
88 if (is_debug && !(drm_debug & DRM_UT_DRIVER))
89 return;
90
91 va_start(args, fmt);
92
93 vaf.fmt = fmt;
94 vaf.va = &args;
95
David Weinehallc49d13e2016-08-22 13:32:42 +030096 dev_printk(level, kdev, "[" DRM_NAME ":%ps] %pV",
Chris Wilson0673ad42016-06-24 14:00:22 +010097 __builtin_return_address(0), &vaf);
98
99 if (is_error && !shown_bug_once) {
David Weinehallc49d13e2016-08-22 13:32:42 +0300100 dev_notice(kdev, "%s", FDO_BUG_MSG);
Chris Wilson0673ad42016-06-24 14:00:22 +0100101 shown_bug_once = true;
102 }
103
104 va_end(args);
105}
106
107static bool i915_error_injected(struct drm_i915_private *dev_priv)
108{
Michal Wajdeczko4f044a82017-09-19 19:38:44 +0000109 return i915_modparams.inject_load_failure &&
110 i915_load_fail_count == i915_modparams.inject_load_failure;
Chris Wilson0673ad42016-06-24 14:00:22 +0100111}
112
113#define i915_load_error(dev_priv, fmt, ...) \
114 __i915_printk(dev_priv, \
115 i915_error_injected(dev_priv) ? KERN_DEBUG : KERN_ERR, \
116 fmt, ##__VA_ARGS__)
117
118
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +0100119static enum intel_pch intel_virt_detect_pch(struct drm_i915_private *dev_priv)
Robert Beckett30c964a2015-08-28 13:10:22 +0100120{
121 enum intel_pch ret = PCH_NOP;
122
123 /*
124 * In a virtualized passthrough environment we can be in a
125 * setup where the ISA bridge is not able to be passed through.
126 * In this case, a south bridge can be emulated and we have to
127 * make an educated guess as to which PCH is really there.
128 */
129
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +0100130 if (IS_GEN5(dev_priv)) {
Robert Beckett30c964a2015-08-28 13:10:22 +0100131 ret = PCH_IBX;
132 DRM_DEBUG_KMS("Assuming Ibex Peak PCH\n");
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +0100133 } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
Robert Beckett30c964a2015-08-28 13:10:22 +0100134 ret = PCH_CPT;
Ville Syrjäläaa032132017-06-20 16:03:07 +0300135 DRM_DEBUG_KMS("Assuming CougarPoint PCH\n");
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +0100136 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Robert Beckett30c964a2015-08-28 13:10:22 +0100137 ret = PCH_LPT;
Xiong Zhang817aef52017-06-15 11:11:45 +0800138 if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
139 dev_priv->pch_id = INTEL_PCH_LPT_LP_DEVICE_ID_TYPE;
140 else
141 dev_priv->pch_id = INTEL_PCH_LPT_DEVICE_ID_TYPE;
Robert Beckett30c964a2015-08-28 13:10:22 +0100142 DRM_DEBUG_KMS("Assuming LynxPoint PCH\n");
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +0100143 } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Robert Beckett30c964a2015-08-28 13:10:22 +0100144 ret = PCH_SPT;
145 DRM_DEBUG_KMS("Assuming SunrisePoint PCH\n");
Rodrigo Vivi80937812017-06-08 08:49:59 -0700146 } else if (IS_COFFEELAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) {
Rodrigo Viviacf1dba2017-06-06 13:30:31 -0700147 ret = PCH_CNP;
Rodrigo Vivi80937812017-06-08 08:49:59 -0700148 DRM_DEBUG_KMS("Assuming CannonPoint PCH\n");
Robert Beckett30c964a2015-08-28 13:10:22 +0100149 }
150
151 return ret;
152}
153
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000154static void intel_detect_pch(struct drm_i915_private *dev_priv)
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800155{
Imre Deakbcdb72a2014-02-14 20:23:54 +0200156 struct pci_dev *pch = NULL;
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800157
Ben Widawskyce1bb322013-04-05 13:12:44 -0700158 /* In all current cases, num_pipes is equivalent to the PCH_NOP setting
159 * (which really amounts to a PCH but no South Display).
160 */
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000161 if (INTEL_INFO(dev_priv)->num_pipes == 0) {
Ben Widawskyce1bb322013-04-05 13:12:44 -0700162 dev_priv->pch_type = PCH_NOP;
Ben Widawskyce1bb322013-04-05 13:12:44 -0700163 return;
164 }
165
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800166 /*
167 * The reason to probe ISA bridge instead of Dev31:Fun0 is to
168 * make graphics device passthrough work easy for VMM, that only
169 * need to expose ISA bridge to let driver know the real hardware
170 * underneath. This is a requirement from virtualization team.
Rui Guo6a9c4b32013-06-19 21:10:23 +0800171 *
172 * In some virtualized environments (e.g. XEN), there is irrelevant
173 * ISA bridge in the system. To work reliably, we should scan trhough
174 * all the ISA bridge devices and check for the first match, instead
175 * of only checking the first one.
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800176 */
Imre Deakbcdb72a2014-02-14 20:23:54 +0200177 while ((pch = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, pch))) {
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800178 if (pch->vendor == PCI_VENDOR_ID_INTEL) {
Imre Deakbcdb72a2014-02-14 20:23:54 +0200179 unsigned short id = pch->device & INTEL_PCH_DEVICE_ID_MASK;
Ville Syrjäläc5e855d2017-06-21 20:49:44 +0300180
181 dev_priv->pch_id = id;
Dhinakaran Pandiyanec7e0bb2017-06-02 13:06:40 -0700182
Jesse Barnes90711d52011-04-28 14:48:02 -0700183 if (id == INTEL_PCH_IBX_DEVICE_ID_TYPE) {
184 dev_priv->pch_type = PCH_IBX;
185 DRM_DEBUG_KMS("Found Ibex Peak PCH\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +0100186 WARN_ON(!IS_GEN5(dev_priv));
Jesse Barnes90711d52011-04-28 14:48:02 -0700187 } else if (id == INTEL_PCH_CPT_DEVICE_ID_TYPE) {
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800188 dev_priv->pch_type = PCH_CPT;
189 DRM_DEBUG_KMS("Found CougarPoint PCH\n");
Ville Syrjäläd4cdbf02017-06-20 16:03:09 +0300190 WARN_ON(!IS_GEN6(dev_priv) &&
191 !IS_IVYBRIDGE(dev_priv));
Jesse Barnesc7925132011-04-07 12:33:56 -0700192 } else if (id == INTEL_PCH_PPT_DEVICE_ID_TYPE) {
193 /* PantherPoint is CPT compatible */
194 dev_priv->pch_type = PCH_CPT;
Jani Nikula492ab662013-10-01 12:12:33 +0300195 DRM_DEBUG_KMS("Found PantherPoint PCH\n");
Ville Syrjäläd4cdbf02017-06-20 16:03:09 +0300196 WARN_ON(!IS_GEN6(dev_priv) &&
197 !IS_IVYBRIDGE(dev_priv));
Eugeni Dodonoveb877eb2012-03-29 12:32:20 -0300198 } else if (id == INTEL_PCH_LPT_DEVICE_ID_TYPE) {
199 dev_priv->pch_type = PCH_LPT;
200 DRM_DEBUG_KMS("Found LynxPoint PCH\n");
Tvrtko Ursulin86527442016-10-13 11:03:00 +0100201 WARN_ON(!IS_HASWELL(dev_priv) &&
202 !IS_BROADWELL(dev_priv));
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100203 WARN_ON(IS_HSW_ULT(dev_priv) ||
204 IS_BDW_ULT(dev_priv));
Ben Widawskye76e0632013-11-07 21:40:41 -0800205 } else if (id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
206 dev_priv->pch_type = PCH_LPT;
207 DRM_DEBUG_KMS("Found LynxPoint LP PCH\n");
Tvrtko Ursulin86527442016-10-13 11:03:00 +0100208 WARN_ON(!IS_HASWELL(dev_priv) &&
209 !IS_BROADWELL(dev_priv));
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100210 WARN_ON(!IS_HSW_ULT(dev_priv) &&
211 !IS_BDW_ULT(dev_priv));
Ville Syrjäläc5e855d2017-06-21 20:49:44 +0300212 } else if (id == INTEL_PCH_WPT_DEVICE_ID_TYPE) {
213 /* WildcatPoint is LPT compatible */
214 dev_priv->pch_type = PCH_LPT;
215 DRM_DEBUG_KMS("Found WildcatPoint PCH\n");
216 WARN_ON(!IS_HASWELL(dev_priv) &&
217 !IS_BROADWELL(dev_priv));
218 WARN_ON(IS_HSW_ULT(dev_priv) ||
219 IS_BDW_ULT(dev_priv));
220 } else if (id == INTEL_PCH_WPT_LP_DEVICE_ID_TYPE) {
221 /* WildcatPoint is LPT compatible */
222 dev_priv->pch_type = PCH_LPT;
223 DRM_DEBUG_KMS("Found WildcatPoint LP PCH\n");
224 WARN_ON(!IS_HASWELL(dev_priv) &&
225 !IS_BROADWELL(dev_priv));
226 WARN_ON(!IS_HSW_ULT(dev_priv) &&
227 !IS_BDW_ULT(dev_priv));
Satheeshakrishna Me7e7ea22014-04-09 11:08:57 +0530228 } else if (id == INTEL_PCH_SPT_DEVICE_ID_TYPE) {
229 dev_priv->pch_type = PCH_SPT;
230 DRM_DEBUG_KMS("Found SunrisePoint PCH\n");
Tvrtko Ursulin08537232016-10-13 11:03:02 +0100231 WARN_ON(!IS_SKYLAKE(dev_priv) &&
232 !IS_KABYLAKE(dev_priv));
Ville Syrjäläc5e855d2017-06-21 20:49:44 +0300233 } else if (id == INTEL_PCH_SPT_LP_DEVICE_ID_TYPE) {
Satheeshakrishna Me7e7ea22014-04-09 11:08:57 +0530234 dev_priv->pch_type = PCH_SPT;
235 DRM_DEBUG_KMS("Found SunrisePoint LP PCH\n");
Tvrtko Ursulin08537232016-10-13 11:03:02 +0100236 WARN_ON(!IS_SKYLAKE(dev_priv) &&
237 !IS_KABYLAKE(dev_priv));
Rodrigo Vivi22dea0b2016-07-01 17:07:12 -0700238 } else if (id == INTEL_PCH_KBP_DEVICE_ID_TYPE) {
239 dev_priv->pch_type = PCH_KBP;
Rodrigo Vivi23247d72017-07-31 11:52:20 -0700240 DRM_DEBUG_KMS("Found Kaby Lake PCH (KBP)\n");
Jani Nikula85327742017-02-01 15:46:09 +0200241 WARN_ON(!IS_SKYLAKE(dev_priv) &&
Rodrigo Vivieb371932017-08-21 16:50:56 -0700242 !IS_KABYLAKE(dev_priv) &&
243 !IS_COFFEELAKE(dev_priv));
Rodrigo Vivi7b22b8c2017-06-02 13:06:39 -0700244 } else if (id == INTEL_PCH_CNP_DEVICE_ID_TYPE) {
245 dev_priv->pch_type = PCH_CNP;
Rodrigo Vivi23247d72017-07-31 11:52:20 -0700246 DRM_DEBUG_KMS("Found Cannon Lake PCH (CNP)\n");
Rodrigo Vivi80937812017-06-08 08:49:59 -0700247 WARN_ON(!IS_CANNONLAKE(dev_priv) &&
248 !IS_COFFEELAKE(dev_priv));
Ville Syrjäläc5e855d2017-06-21 20:49:44 +0300249 } else if (id == INTEL_PCH_CNP_LP_DEVICE_ID_TYPE) {
Dhinakaran Pandiyanec7e0bb2017-06-02 13:06:40 -0700250 dev_priv->pch_type = PCH_CNP;
Rodrigo Vivi23247d72017-07-31 11:52:20 -0700251 DRM_DEBUG_KMS("Found Cannon Lake LP PCH (CNP-LP)\n");
Rodrigo Vivi80937812017-06-08 08:49:59 -0700252 WARN_ON(!IS_CANNONLAKE(dev_priv) &&
253 !IS_COFFEELAKE(dev_priv));
Ville Syrjäläd4cdbf02017-06-20 16:03:09 +0300254 } else if (id == INTEL_PCH_P2X_DEVICE_ID_TYPE ||
255 id == INTEL_PCH_P3X_DEVICE_ID_TYPE ||
256 (id == INTEL_PCH_QEMU_DEVICE_ID_TYPE &&
Gerd Hoffmann94bb4892016-06-13 14:38:56 +0200257 pch->subsystem_vendor ==
258 PCI_SUBVENDOR_ID_REDHAT_QUMRANET &&
259 pch->subsystem_device ==
260 PCI_SUBDEVICE_ID_QEMU)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +0100261 dev_priv->pch_type =
262 intel_virt_detect_pch(dev_priv);
Imre Deakbcdb72a2014-02-14 20:23:54 +0200263 } else
264 continue;
265
Rui Guo6a9c4b32013-06-19 21:10:23 +0800266 break;
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800267 }
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800268 }
Rui Guo6a9c4b32013-06-19 21:10:23 +0800269 if (!pch)
Imre Deakbcdb72a2014-02-14 20:23:54 +0200270 DRM_DEBUG_KMS("No PCH found.\n");
271
272 pci_dev_put(pch);
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800273}
274
Chris Wilson0673ad42016-06-24 14:00:22 +0100275static int i915_getparam(struct drm_device *dev, void *data,
276 struct drm_file *file_priv)
277{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100278 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +0300279 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson0673ad42016-06-24 14:00:22 +0100280 drm_i915_getparam_t *param = data;
281 int value;
282
283 switch (param->param) {
284 case I915_PARAM_IRQ_ACTIVE:
285 case I915_PARAM_ALLOW_BATCHBUFFER:
286 case I915_PARAM_LAST_DISPATCH:
Kenneth Graunkeef0f4112017-02-15 01:34:46 -0800287 case I915_PARAM_HAS_EXEC_CONSTANTS:
Chris Wilson0673ad42016-06-24 14:00:22 +0100288 /* Reject all old ums/dri params. */
289 return -ENODEV;
290 case I915_PARAM_CHIPSET_ID:
David Weinehall52a05c32016-08-22 13:32:44 +0300291 value = pdev->device;
Chris Wilson0673ad42016-06-24 14:00:22 +0100292 break;
293 case I915_PARAM_REVISION:
David Weinehall52a05c32016-08-22 13:32:44 +0300294 value = pdev->revision;
Chris Wilson0673ad42016-06-24 14:00:22 +0100295 break;
Chris Wilson0673ad42016-06-24 14:00:22 +0100296 case I915_PARAM_NUM_FENCES_AVAIL:
297 value = dev_priv->num_fence_regs;
298 break;
299 case I915_PARAM_HAS_OVERLAY:
300 value = dev_priv->overlay ? 1 : 0;
301 break;
Chris Wilson0673ad42016-06-24 14:00:22 +0100302 case I915_PARAM_HAS_BSD:
Akash Goel3b3f1652016-10-13 22:44:48 +0530303 value = !!dev_priv->engine[VCS];
Chris Wilson0673ad42016-06-24 14:00:22 +0100304 break;
305 case I915_PARAM_HAS_BLT:
Akash Goel3b3f1652016-10-13 22:44:48 +0530306 value = !!dev_priv->engine[BCS];
Chris Wilson0673ad42016-06-24 14:00:22 +0100307 break;
308 case I915_PARAM_HAS_VEBOX:
Akash Goel3b3f1652016-10-13 22:44:48 +0530309 value = !!dev_priv->engine[VECS];
Chris Wilson0673ad42016-06-24 14:00:22 +0100310 break;
311 case I915_PARAM_HAS_BSD2:
Akash Goel3b3f1652016-10-13 22:44:48 +0530312 value = !!dev_priv->engine[VCS2];
Chris Wilson0673ad42016-06-24 14:00:22 +0100313 break;
Chris Wilson0673ad42016-06-24 14:00:22 +0100314 case I915_PARAM_HAS_LLC:
David Weinehall16162472016-09-02 13:46:17 +0300315 value = HAS_LLC(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100316 break;
317 case I915_PARAM_HAS_WT:
David Weinehall16162472016-09-02 13:46:17 +0300318 value = HAS_WT(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100319 break;
320 case I915_PARAM_HAS_ALIASING_PPGTT:
David Weinehall16162472016-09-02 13:46:17 +0300321 value = USES_PPGTT(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100322 break;
323 case I915_PARAM_HAS_SEMAPHORES:
Michal Wajdeczko4f044a82017-09-19 19:38:44 +0000324 value = i915_modparams.semaphores;
Chris Wilson0673ad42016-06-24 14:00:22 +0100325 break;
Chris Wilson0673ad42016-06-24 14:00:22 +0100326 case I915_PARAM_HAS_SECURE_BATCHES:
327 value = capable(CAP_SYS_ADMIN);
328 break;
Chris Wilson0673ad42016-06-24 14:00:22 +0100329 case I915_PARAM_CMD_PARSER_VERSION:
330 value = i915_cmd_parser_get_version(dev_priv);
331 break;
Chris Wilson0673ad42016-06-24 14:00:22 +0100332 case I915_PARAM_SUBSLICE_TOTAL:
Imre Deak57ec1712016-08-31 19:13:05 +0300333 value = sseu_subslice_total(&INTEL_INFO(dev_priv)->sseu);
Chris Wilson0673ad42016-06-24 14:00:22 +0100334 if (!value)
335 return -ENODEV;
336 break;
337 case I915_PARAM_EU_TOTAL:
Imre Deak43b67992016-08-31 19:13:02 +0300338 value = INTEL_INFO(dev_priv)->sseu.eu_total;
Chris Wilson0673ad42016-06-24 14:00:22 +0100339 if (!value)
340 return -ENODEV;
341 break;
342 case I915_PARAM_HAS_GPU_RESET:
Michal Wajdeczko4f044a82017-09-19 19:38:44 +0000343 value = i915_modparams.enable_hangcheck &&
344 intel_has_gpu_reset(dev_priv);
Michel Thierry142bc7d2017-06-20 10:57:46 +0100345 if (value && intel_has_reset_engine(dev_priv))
346 value = 2;
Chris Wilson0673ad42016-06-24 14:00:22 +0100347 break;
348 case I915_PARAM_HAS_RESOURCE_STREAMER:
David Weinehall16162472016-09-02 13:46:17 +0300349 value = HAS_RESOURCE_STREAMER(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100350 break;
arun.siluvery@linux.intel.com37f501a2016-07-01 11:43:02 +0100351 case I915_PARAM_HAS_POOLED_EU:
David Weinehall16162472016-09-02 13:46:17 +0300352 value = HAS_POOLED_EU(dev_priv);
arun.siluvery@linux.intel.com37f501a2016-07-01 11:43:02 +0100353 break;
354 case I915_PARAM_MIN_EU_IN_POOL:
Imre Deak43b67992016-08-31 19:13:02 +0300355 value = INTEL_INFO(dev_priv)->sseu.min_eu_in_pool;
arun.siluvery@linux.intel.com37f501a2016-07-01 11:43:02 +0100356 break;
Anusha Srivatsa5464cd62017-01-18 08:05:58 -0800357 case I915_PARAM_HUC_STATUS:
sagar.a.kamble@intel.com3582ad12017-02-03 13:58:33 +0530358 intel_runtime_pm_get(dev_priv);
Anusha Srivatsa5464cd62017-01-18 08:05:58 -0800359 value = I915_READ(HUC_STATUS2) & HUC_FW_VERIFIED;
sagar.a.kamble@intel.com3582ad12017-02-03 13:58:33 +0530360 intel_runtime_pm_put(dev_priv);
Anusha Srivatsa5464cd62017-01-18 08:05:58 -0800361 break;
Chris Wilson4cc69072016-08-25 19:05:19 +0100362 case I915_PARAM_MMAP_GTT_VERSION:
363 /* Though we've started our numbering from 1, and so class all
364 * earlier versions as 0, in effect their value is undefined as
365 * the ioctl will report EINVAL for the unknown param!
366 */
367 value = i915_gem_mmap_gtt_version();
368 break;
Chris Wilson0de91362016-11-14 20:41:01 +0000369 case I915_PARAM_HAS_SCHEDULER:
Chris Wilsonbf64e0b2017-10-03 21:34:51 +0100370 value = 0;
371 if (dev_priv->engine[RCS] && dev_priv->engine[RCS]->schedule)
372 value |= I915_SCHEDULER_CAP_ENABLED;
Chris Wilson0de91362016-11-14 20:41:01 +0000373 break;
David Weinehall16162472016-09-02 13:46:17 +0300374 case I915_PARAM_MMAP_VERSION:
375 /* Remember to bump this if the version changes! */
376 case I915_PARAM_HAS_GEM:
377 case I915_PARAM_HAS_PAGEFLIPPING:
378 case I915_PARAM_HAS_EXECBUF2: /* depends on GEM */
379 case I915_PARAM_HAS_RELAXED_FENCING:
380 case I915_PARAM_HAS_COHERENT_RINGS:
381 case I915_PARAM_HAS_RELAXED_DELTA:
382 case I915_PARAM_HAS_GEN7_SOL_RESET:
383 case I915_PARAM_HAS_WAIT_TIMEOUT:
384 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
385 case I915_PARAM_HAS_PINNED_BATCHES:
386 case I915_PARAM_HAS_EXEC_NO_RELOC:
387 case I915_PARAM_HAS_EXEC_HANDLE_LUT:
388 case I915_PARAM_HAS_COHERENT_PHYS_GTT:
389 case I915_PARAM_HAS_EXEC_SOFTPIN:
Chris Wilson77ae9952017-01-27 09:40:07 +0000390 case I915_PARAM_HAS_EXEC_ASYNC:
Chris Wilsonfec04452017-01-27 09:40:08 +0000391 case I915_PARAM_HAS_EXEC_FENCE:
Chris Wilsonb0fd47a2017-04-15 10:39:02 +0100392 case I915_PARAM_HAS_EXEC_CAPTURE:
Chris Wilson1a71cf22017-06-16 15:05:23 +0100393 case I915_PARAM_HAS_EXEC_BATCH_FIRST:
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +0100394 case I915_PARAM_HAS_EXEC_FENCE_ARRAY:
David Weinehall16162472016-09-02 13:46:17 +0300395 /* For the time being all of these are always true;
396 * if some supported hardware does not have one of these
397 * features this value needs to be provided from
398 * INTEL_INFO(), a feature macro, or similar.
399 */
400 value = 1;
401 break;
Robert Bragg7fed5552017-06-13 12:22:59 +0100402 case I915_PARAM_SLICE_MASK:
403 value = INTEL_INFO(dev_priv)->sseu.slice_mask;
404 if (!value)
405 return -ENODEV;
406 break;
Robert Braggf5320232017-06-13 12:23:00 +0100407 case I915_PARAM_SUBSLICE_MASK:
408 value = INTEL_INFO(dev_priv)->sseu.subslice_mask;
409 if (!value)
410 return -ENODEV;
411 break;
Chris Wilson0673ad42016-06-24 14:00:22 +0100412 default:
413 DRM_DEBUG("Unknown parameter %d\n", param->param);
414 return -EINVAL;
415 }
416
Chris Wilsondda33002016-06-24 14:00:23 +0100417 if (put_user(value, param->value))
Chris Wilson0673ad42016-06-24 14:00:22 +0100418 return -EFAULT;
Chris Wilson0673ad42016-06-24 14:00:22 +0100419
420 return 0;
421}
422
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000423static int i915_get_bridge_dev(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100424{
Chris Wilson0673ad42016-06-24 14:00:22 +0100425 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
426 if (!dev_priv->bridge_dev) {
427 DRM_ERROR("bridge device not found\n");
428 return -1;
429 }
430 return 0;
431}
432
433/* Allocate space for the MCH regs if needed, return nonzero on error */
434static int
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000435intel_alloc_mchbar_resource(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100436{
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +0000437 int reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Chris Wilson0673ad42016-06-24 14:00:22 +0100438 u32 temp_lo, temp_hi = 0;
439 u64 mchbar_addr;
440 int ret;
441
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +0000442 if (INTEL_GEN(dev_priv) >= 4)
Chris Wilson0673ad42016-06-24 14:00:22 +0100443 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
444 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
445 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
446
447 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
448#ifdef CONFIG_PNP
449 if (mchbar_addr &&
450 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
451 return 0;
452#endif
453
454 /* Get some space for it */
455 dev_priv->mch_res.name = "i915 MCHBAR";
456 dev_priv->mch_res.flags = IORESOURCE_MEM;
457 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
458 &dev_priv->mch_res,
459 MCHBAR_SIZE, MCHBAR_SIZE,
460 PCIBIOS_MIN_MEM,
461 0, pcibios_align_resource,
462 dev_priv->bridge_dev);
463 if (ret) {
464 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
465 dev_priv->mch_res.start = 0;
466 return ret;
467 }
468
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +0000469 if (INTEL_GEN(dev_priv) >= 4)
Chris Wilson0673ad42016-06-24 14:00:22 +0100470 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
471 upper_32_bits(dev_priv->mch_res.start));
472
473 pci_write_config_dword(dev_priv->bridge_dev, reg,
474 lower_32_bits(dev_priv->mch_res.start));
475 return 0;
476}
477
478/* Setup MCHBAR if possible, return true if we should disable it again */
479static void
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000480intel_setup_mchbar(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100481{
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +0000482 int mchbar_reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Chris Wilson0673ad42016-06-24 14:00:22 +0100483 u32 temp;
484 bool enabled;
485
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100486 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson0673ad42016-06-24 14:00:22 +0100487 return;
488
489 dev_priv->mchbar_need_disable = false;
490
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100491 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
Chris Wilson0673ad42016-06-24 14:00:22 +0100492 pci_read_config_dword(dev_priv->bridge_dev, DEVEN, &temp);
493 enabled = !!(temp & DEVEN_MCHBAR_EN);
494 } else {
495 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
496 enabled = temp & 1;
497 }
498
499 /* If it's already enabled, don't have to do anything */
500 if (enabled)
501 return;
502
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000503 if (intel_alloc_mchbar_resource(dev_priv))
Chris Wilson0673ad42016-06-24 14:00:22 +0100504 return;
505
506 dev_priv->mchbar_need_disable = true;
507
508 /* Space is allocated or reserved, so enable it. */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100509 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
Chris Wilson0673ad42016-06-24 14:00:22 +0100510 pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
511 temp | DEVEN_MCHBAR_EN);
512 } else {
513 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
514 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
515 }
516}
517
518static void
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000519intel_teardown_mchbar(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100520{
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +0000521 int mchbar_reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Chris Wilson0673ad42016-06-24 14:00:22 +0100522
523 if (dev_priv->mchbar_need_disable) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100524 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
Chris Wilson0673ad42016-06-24 14:00:22 +0100525 u32 deven_val;
526
527 pci_read_config_dword(dev_priv->bridge_dev, DEVEN,
528 &deven_val);
529 deven_val &= ~DEVEN_MCHBAR_EN;
530 pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
531 deven_val);
532 } else {
533 u32 mchbar_val;
534
535 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg,
536 &mchbar_val);
537 mchbar_val &= ~1;
538 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg,
539 mchbar_val);
540 }
541 }
542
543 if (dev_priv->mch_res.start)
544 release_resource(&dev_priv->mch_res);
545}
546
547/* true = enable decode, false = disable decoder */
548static unsigned int i915_vga_set_decode(void *cookie, bool state)
549{
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000550 struct drm_i915_private *dev_priv = cookie;
Chris Wilson0673ad42016-06-24 14:00:22 +0100551
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000552 intel_modeset_vga_set_state(dev_priv, state);
Chris Wilson0673ad42016-06-24 14:00:22 +0100553 if (state)
554 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
555 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
556 else
557 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
558}
559
Tvrtko Ursulin7f26cb82016-12-01 14:16:41 +0000560static int i915_resume_switcheroo(struct drm_device *dev);
561static int i915_suspend_switcheroo(struct drm_device *dev, pm_message_t state);
562
Chris Wilson0673ad42016-06-24 14:00:22 +0100563static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
564{
565 struct drm_device *dev = pci_get_drvdata(pdev);
566 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
567
568 if (state == VGA_SWITCHEROO_ON) {
569 pr_info("switched on\n");
570 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
571 /* i915 resume handler doesn't set to D0 */
David Weinehall52a05c32016-08-22 13:32:44 +0300572 pci_set_power_state(pdev, PCI_D0);
Chris Wilson0673ad42016-06-24 14:00:22 +0100573 i915_resume_switcheroo(dev);
574 dev->switch_power_state = DRM_SWITCH_POWER_ON;
575 } else {
576 pr_info("switched off\n");
577 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
578 i915_suspend_switcheroo(dev, pmm);
579 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
580 }
581}
582
583static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
584{
585 struct drm_device *dev = pci_get_drvdata(pdev);
586
587 /*
588 * FIXME: open_count is protected by drm_global_mutex but that would lead to
589 * locking inversion with the driver load path. And the access here is
590 * completely racy anyway. So don't bother with locking for now.
591 */
592 return dev->open_count == 0;
593}
594
595static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
596 .set_gpu_state = i915_switcheroo_set_state,
597 .reprobe = NULL,
598 .can_switch = i915_switcheroo_can_switch,
599};
600
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100601static void i915_gem_fini(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100602{
Chris Wilson3b19f162017-07-18 14:41:24 +0100603 /* Flush any outstanding unpin_work. */
604 i915_gem_drain_workqueue(dev_priv);
Chris Wilson5f09a9c2017-06-20 12:05:46 +0100605
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100606 mutex_lock(&dev_priv->drm.struct_mutex);
Oscar Mateob8991402017-03-28 09:53:47 -0700607 intel_uc_fini_hw(dev_priv);
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +0000608 i915_gem_cleanup_engines(dev_priv);
Chris Wilson829a0af2017-06-20 12:05:45 +0100609 i915_gem_contexts_fini(dev_priv);
Chris Wilson8a2421b2017-06-16 15:05:22 +0100610 i915_gem_cleanup_userptr(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100611 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson0673ad42016-06-24 14:00:22 +0100612
Chris Wilsonbdeb9782016-12-23 14:57:56 +0000613 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100614
Chris Wilson829a0af2017-06-20 12:05:45 +0100615 WARN_ON(!list_empty(&dev_priv->contexts.list));
Chris Wilson0673ad42016-06-24 14:00:22 +0100616}
617
618static int i915_load_modeset_init(struct drm_device *dev)
619{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100620 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +0300621 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson0673ad42016-06-24 14:00:22 +0100622 int ret;
623
624 if (i915_inject_load_failure())
625 return -ENODEV;
626
Jani Nikula66578852017-03-10 15:27:57 +0200627 intel_bios_init(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100628
629 /* If we have > 1 VGA cards, then we need to arbitrate access
630 * to the common VGA resources.
631 *
632 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
633 * then we do not take part in VGA arbitration and the
634 * vga_client_register() fails with -ENODEV.
635 */
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000636 ret = vga_client_register(pdev, dev_priv, NULL, i915_vga_set_decode);
Chris Wilson0673ad42016-06-24 14:00:22 +0100637 if (ret && ret != -ENODEV)
638 goto out;
639
640 intel_register_dsm_handler();
641
David Weinehall52a05c32016-08-22 13:32:44 +0300642 ret = vga_switcheroo_register_client(pdev, &i915_switcheroo_ops, false);
Chris Wilson0673ad42016-06-24 14:00:22 +0100643 if (ret)
644 goto cleanup_vga_client;
645
646 /* must happen before intel_power_domains_init_hw() on VLV/CHV */
647 intel_update_rawclk(dev_priv);
648
649 intel_power_domains_init_hw(dev_priv, false);
650
651 intel_csr_ucode_init(dev_priv);
652
653 ret = intel_irq_install(dev_priv);
654 if (ret)
655 goto cleanup_csr;
656
Tvrtko Ursulin40196442016-12-01 14:16:42 +0000657 intel_setup_gmbus(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100658
659 /* Important: The output setup functions called by modeset_init need
660 * working irqs for e.g. gmbus and dp aux transfers. */
Ville Syrjäläb079bd172016-10-25 18:58:02 +0300661 ret = intel_modeset_init(dev);
662 if (ret)
663 goto cleanup_irq;
Chris Wilson0673ad42016-06-24 14:00:22 +0100664
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100665 intel_uc_init_fw(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100666
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +0000667 ret = i915_gem_init(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100668 if (ret)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700669 goto cleanup_uc;
Chris Wilson0673ad42016-06-24 14:00:22 +0100670
671 intel_modeset_gem_init(dev);
672
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000673 if (INTEL_INFO(dev_priv)->num_pipes == 0)
Chris Wilson0673ad42016-06-24 14:00:22 +0100674 return 0;
675
676 ret = intel_fbdev_init(dev);
677 if (ret)
678 goto cleanup_gem;
679
680 /* Only enable hotplug handling once the fbdev is fully set up. */
681 intel_hpd_init(dev_priv);
682
683 drm_kms_helper_poll_init(dev);
684
685 return 0;
686
687cleanup_gem:
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +0000688 if (i915_gem_suspend(dev_priv))
Imre Deak1c777c52016-10-12 17:46:37 +0300689 DRM_ERROR("failed to idle hardware; continuing to unload!\n");
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100690 i915_gem_fini(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700691cleanup_uc:
692 intel_uc_fini_fw(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100693cleanup_irq:
Chris Wilson0673ad42016-06-24 14:00:22 +0100694 drm_irq_uninstall(dev);
Tvrtko Ursulin40196442016-12-01 14:16:42 +0000695 intel_teardown_gmbus(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100696cleanup_csr:
697 intel_csr_ucode_fini(dev_priv);
698 intel_power_domains_fini(dev_priv);
David Weinehall52a05c32016-08-22 13:32:44 +0300699 vga_switcheroo_unregister_client(pdev);
Chris Wilson0673ad42016-06-24 14:00:22 +0100700cleanup_vga_client:
David Weinehall52a05c32016-08-22 13:32:44 +0300701 vga_client_register(pdev, NULL, NULL, NULL);
Chris Wilson0673ad42016-06-24 14:00:22 +0100702out:
703 return ret;
704}
705
Chris Wilson0673ad42016-06-24 14:00:22 +0100706static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
707{
708 struct apertures_struct *ap;
Chris Wilson91c8a322016-07-05 10:40:23 +0100709 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson0673ad42016-06-24 14:00:22 +0100710 struct i915_ggtt *ggtt = &dev_priv->ggtt;
711 bool primary;
712 int ret;
713
714 ap = alloc_apertures(1);
715 if (!ap)
716 return -ENOMEM;
717
718 ap->ranges[0].base = ggtt->mappable_base;
719 ap->ranges[0].size = ggtt->mappable_end;
720
721 primary =
722 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
723
Daniel Vetter44adece2016-08-10 18:52:34 +0200724 ret = drm_fb_helper_remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
Chris Wilson0673ad42016-06-24 14:00:22 +0100725
726 kfree(ap);
727
728 return ret;
729}
Chris Wilson0673ad42016-06-24 14:00:22 +0100730
731#if !defined(CONFIG_VGA_CONSOLE)
732static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
733{
734 return 0;
735}
736#elif !defined(CONFIG_DUMMY_CONSOLE)
737static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
738{
739 return -ENODEV;
740}
741#else
742static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
743{
744 int ret = 0;
745
746 DRM_INFO("Replacing VGA console driver\n");
747
748 console_lock();
749 if (con_is_bound(&vga_con))
750 ret = do_take_over_console(&dummy_con, 0, MAX_NR_CONSOLES - 1, 1);
751 if (ret == 0) {
752 ret = do_unregister_con_driver(&vga_con);
753
754 /* Ignore "already unregistered". */
755 if (ret == -ENODEV)
756 ret = 0;
757 }
758 console_unlock();
759
760 return ret;
761}
762#endif
763
Chris Wilson0673ad42016-06-24 14:00:22 +0100764static void intel_init_dpio(struct drm_i915_private *dev_priv)
765{
766 /*
767 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
768 * CHV x1 PHY (DP/HDMI D)
769 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
770 */
771 if (IS_CHERRYVIEW(dev_priv)) {
772 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
773 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
774 } else if (IS_VALLEYVIEW(dev_priv)) {
775 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
776 }
777}
778
779static int i915_workqueues_init(struct drm_i915_private *dev_priv)
780{
781 /*
782 * The i915 workqueue is primarily used for batched retirement of
783 * requests (and thus managing bo) once the task has been completed
784 * by the GPU. i915_gem_retire_requests() is called directly when we
785 * need high-priority retirement, such as waiting for an explicit
786 * bo.
787 *
788 * It is also used for periodic low-priority events, such as
789 * idle-timers and recording error state.
790 *
791 * All tasks on the workqueue are expected to acquire the dev mutex
792 * so there is no point in running more than one instance of the
793 * workqueue at any time. Use an ordered one.
794 */
795 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
796 if (dev_priv->wq == NULL)
797 goto out_err;
798
799 dev_priv->hotplug.dp_wq = alloc_ordered_workqueue("i915-dp", 0);
800 if (dev_priv->hotplug.dp_wq == NULL)
801 goto out_free_wq;
802
Chris Wilson0673ad42016-06-24 14:00:22 +0100803 return 0;
804
Chris Wilson0673ad42016-06-24 14:00:22 +0100805out_free_wq:
806 destroy_workqueue(dev_priv->wq);
807out_err:
808 DRM_ERROR("Failed to allocate workqueues.\n");
809
810 return -ENOMEM;
811}
812
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000813static void i915_engines_cleanup(struct drm_i915_private *i915)
814{
815 struct intel_engine_cs *engine;
816 enum intel_engine_id id;
817
818 for_each_engine(engine, i915, id)
819 kfree(engine);
820}
821
Chris Wilson0673ad42016-06-24 14:00:22 +0100822static void i915_workqueues_cleanup(struct drm_i915_private *dev_priv)
823{
Chris Wilson0673ad42016-06-24 14:00:22 +0100824 destroy_workqueue(dev_priv->hotplug.dp_wq);
825 destroy_workqueue(dev_priv->wq);
826}
827
Paulo Zanoni4fc7e842016-09-26 15:07:52 +0300828/*
829 * We don't keep the workarounds for pre-production hardware, so we expect our
830 * driver to fail on these machines in one way or another. A little warning on
831 * dmesg may help both the user and the bug triagers.
832 */
833static void intel_detect_preproduction_hw(struct drm_i915_private *dev_priv)
834{
Chris Wilson248a1242017-01-30 10:44:56 +0000835 bool pre = false;
836
837 pre |= IS_HSW_EARLY_SDV(dev_priv);
838 pre |= IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0);
Chris Wilson0102ba12017-01-30 10:44:58 +0000839 pre |= IS_BXT_REVID(dev_priv, 0, BXT_REVID_B_LAST);
Chris Wilson248a1242017-01-30 10:44:56 +0000840
Chris Wilson7c5ff4a2017-01-30 10:44:57 +0000841 if (pre) {
Paulo Zanoni4fc7e842016-09-26 15:07:52 +0300842 DRM_ERROR("This is a pre-production stepping. "
843 "It may not be fully functional.\n");
Chris Wilson7c5ff4a2017-01-30 10:44:57 +0000844 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK);
845 }
Paulo Zanoni4fc7e842016-09-26 15:07:52 +0300846}
847
Chris Wilson0673ad42016-06-24 14:00:22 +0100848/**
849 * i915_driver_init_early - setup state not requiring device access
850 * @dev_priv: device private
851 *
852 * Initialize everything that is a "SW-only" state, that is state not
853 * requiring accessing the device or exposing the driver via kernel internal
854 * or userspace interfaces. Example steps belonging here: lock initialization,
855 * system memory allocation, setting up device specific attributes and
856 * function hooks not requiring accessing the device.
857 */
858static int i915_driver_init_early(struct drm_i915_private *dev_priv,
859 const struct pci_device_id *ent)
860{
861 const struct intel_device_info *match_info =
862 (struct intel_device_info *)ent->driver_data;
863 struct intel_device_info *device_info;
864 int ret = 0;
865
866 if (i915_inject_load_failure())
867 return -ENODEV;
868
869 /* Setup the write-once "constant" device info */
Chris Wilson94b4f3b2016-07-05 10:40:20 +0100870 device_info = mkwrite_device_info(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100871 memcpy(device_info, match_info, sizeof(*device_info));
872 device_info->device_id = dev_priv->drm.pdev->device;
873
Tvrtko Ursulinae7617f2017-09-27 17:41:38 +0100874 BUILD_BUG_ON(INTEL_MAX_PLATFORMS >
875 sizeof(device_info->platform_mask) * BITS_PER_BYTE);
876 device_info->platform_mask = BIT(device_info->platform);
877
Chris Wilson0673ad42016-06-24 14:00:22 +0100878 BUG_ON(device_info->gen > sizeof(device_info->gen_mask) * BITS_PER_BYTE);
879 device_info->gen_mask = BIT(device_info->gen - 1);
880
881 spin_lock_init(&dev_priv->irq_lock);
882 spin_lock_init(&dev_priv->gpu_error.lock);
883 mutex_init(&dev_priv->backlight_lock);
884 spin_lock_init(&dev_priv->uncore.lock);
Lyude317eaa92017-02-03 21:18:25 -0500885
Chris Wilson0673ad42016-06-24 14:00:22 +0100886 spin_lock_init(&dev_priv->mm.object_stat_lock);
Chris Wilson0673ad42016-06-24 14:00:22 +0100887 mutex_init(&dev_priv->sb_lock);
888 mutex_init(&dev_priv->modeset_restore_lock);
889 mutex_init(&dev_priv->av_mutex);
890 mutex_init(&dev_priv->wm.wm_mutex);
891 mutex_init(&dev_priv->pps_mutex);
892
Arkadiusz Hiler413e8fd2016-11-25 18:59:36 +0100893 intel_uc_init_early(dev_priv);
Chris Wilson0b1de5d2016-08-12 12:39:59 +0100894 i915_memcpy_init_early(dev_priv);
895
Chris Wilson0673ad42016-06-24 14:00:22 +0100896 ret = i915_workqueues_init(dev_priv);
897 if (ret < 0)
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000898 goto err_engines;
Chris Wilson0673ad42016-06-24 14:00:22 +0100899
Chris Wilson0673ad42016-06-24 14:00:22 +0100900 /* This must be called before any calls to HAS_PCH_* */
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000901 intel_detect_pch(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100902
Tvrtko Ursulin192aa182016-12-01 14:16:45 +0000903 intel_pm_setup(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100904 intel_init_dpio(dev_priv);
905 intel_power_domains_init(dev_priv);
906 intel_irq_init(dev_priv);
Mika Kuoppala3ac168a2016-11-01 18:43:03 +0200907 intel_hangcheck_init(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100908 intel_init_display_hooks(dev_priv);
909 intel_init_clock_gating_hooks(dev_priv);
910 intel_init_audio_hooks(dev_priv);
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +0000911 ret = i915_gem_load_init(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +0100912 if (ret < 0)
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300913 goto err_irq;
Chris Wilson0673ad42016-06-24 14:00:22 +0100914
David Weinehall36cdd012016-08-22 13:59:31 +0300915 intel_display_crc_init(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100916
Chris Wilson94b4f3b2016-07-05 10:40:20 +0100917 intel_device_info_dump(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100918
Paulo Zanoni4fc7e842016-09-26 15:07:52 +0300919 intel_detect_preproduction_hw(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100920
Robert Braggeec688e2016-11-07 19:49:47 +0000921 i915_perf_init(dev_priv);
922
Chris Wilson0673ad42016-06-24 14:00:22 +0100923 return 0;
924
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300925err_irq:
926 intel_irq_fini(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100927 i915_workqueues_cleanup(dev_priv);
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000928err_engines:
929 i915_engines_cleanup(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100930 return ret;
931}
932
933/**
934 * i915_driver_cleanup_early - cleanup the setup done in i915_driver_init_early()
935 * @dev_priv: device private
936 */
937static void i915_driver_cleanup_early(struct drm_i915_private *dev_priv)
938{
Robert Braggeec688e2016-11-07 19:49:47 +0000939 i915_perf_fini(dev_priv);
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +0000940 i915_gem_load_cleanup(dev_priv);
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300941 intel_irq_fini(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100942 i915_workqueues_cleanup(dev_priv);
Chris Wilsonbb8f0f52017-01-24 11:01:34 +0000943 i915_engines_cleanup(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100944}
945
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000946static int i915_mmio_setup(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100947{
David Weinehall52a05c32016-08-22 13:32:44 +0300948 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson0673ad42016-06-24 14:00:22 +0100949 int mmio_bar;
950 int mmio_size;
951
Tvrtko Ursulin5db94012016-10-13 11:03:10 +0100952 mmio_bar = IS_GEN2(dev_priv) ? 1 : 0;
Chris Wilson0673ad42016-06-24 14:00:22 +0100953 /*
954 * Before gen4, the registers and the GTT are behind different BARs.
955 * However, from gen4 onwards, the registers and the GTT are shared
956 * in the same BAR, so we want to restrict this ioremap from
957 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
958 * the register BAR remains the same size for all the earlier
959 * generations up to Ironlake.
960 */
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +0000961 if (INTEL_GEN(dev_priv) < 5)
Chris Wilson0673ad42016-06-24 14:00:22 +0100962 mmio_size = 512 * 1024;
963 else
964 mmio_size = 2 * 1024 * 1024;
David Weinehall52a05c32016-08-22 13:32:44 +0300965 dev_priv->regs = pci_iomap(pdev, mmio_bar, mmio_size);
Chris Wilson0673ad42016-06-24 14:00:22 +0100966 if (dev_priv->regs == NULL) {
967 DRM_ERROR("failed to map registers\n");
968
969 return -EIO;
970 }
971
972 /* Try to make sure MCHBAR is enabled before poking at it */
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000973 intel_setup_mchbar(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100974
975 return 0;
976}
977
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000978static void i915_mmio_cleanup(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100979{
David Weinehall52a05c32016-08-22 13:32:44 +0300980 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson0673ad42016-06-24 14:00:22 +0100981
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000982 intel_teardown_mchbar(dev_priv);
David Weinehall52a05c32016-08-22 13:32:44 +0300983 pci_iounmap(pdev, dev_priv->regs);
Chris Wilson0673ad42016-06-24 14:00:22 +0100984}
985
986/**
987 * i915_driver_init_mmio - setup device MMIO
988 * @dev_priv: device private
989 *
990 * Setup minimal device state necessary for MMIO accesses later in the
991 * initialization sequence. The setup here should avoid any other device-wide
992 * side effects or exposing the driver via kernel internal or user space
993 * interfaces.
994 */
995static int i915_driver_init_mmio(struct drm_i915_private *dev_priv)
996{
Chris Wilson0673ad42016-06-24 14:00:22 +0100997 int ret;
998
999 if (i915_inject_load_failure())
1000 return -ENODEV;
1001
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +00001002 if (i915_get_bridge_dev(dev_priv))
Chris Wilson0673ad42016-06-24 14:00:22 +01001003 return -EIO;
1004
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +00001005 ret = i915_mmio_setup(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001006 if (ret < 0)
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03001007 goto err_bridge;
Chris Wilson0673ad42016-06-24 14:00:22 +01001008
1009 intel_uncore_init(dev_priv);
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03001010
Sagar Arun Kamble1fc556f2017-10-04 15:33:24 +00001011 intel_uc_init_mmio(dev_priv);
1012
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03001013 ret = intel_engines_init_mmio(dev_priv);
1014 if (ret)
1015 goto err_uncore;
1016
Chris Wilson24145512017-01-24 11:01:35 +00001017 i915_gem_init_mmio(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001018
1019 return 0;
1020
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +03001021err_uncore:
1022 intel_uncore_fini(dev_priv);
1023err_bridge:
Chris Wilson0673ad42016-06-24 14:00:22 +01001024 pci_dev_put(dev_priv->bridge_dev);
1025
1026 return ret;
1027}
1028
1029/**
1030 * i915_driver_cleanup_mmio - cleanup the setup done in i915_driver_init_mmio()
1031 * @dev_priv: device private
1032 */
1033static void i915_driver_cleanup_mmio(struct drm_i915_private *dev_priv)
1034{
Chris Wilson0673ad42016-06-24 14:00:22 +01001035 intel_uncore_fini(dev_priv);
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +00001036 i915_mmio_cleanup(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001037 pci_dev_put(dev_priv->bridge_dev);
1038}
1039
Chris Wilson94b4f3b2016-07-05 10:40:20 +01001040static void intel_sanitize_options(struct drm_i915_private *dev_priv)
1041{
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001042 i915_modparams.enable_execlists =
Chris Wilson94b4f3b2016-07-05 10:40:20 +01001043 intel_sanitize_enable_execlists(dev_priv,
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001044 i915_modparams.enable_execlists);
Chris Wilson94b4f3b2016-07-05 10:40:20 +01001045
1046 /*
1047 * i915.enable_ppgtt is read-only, so do an early pass to validate the
1048 * user's requested state against the hardware/driver capabilities. We
1049 * do this now so that we can print out any log messages once rather
1050 * than every time we check intel_enable_ppgtt().
1051 */
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001052 i915_modparams.enable_ppgtt =
1053 intel_sanitize_enable_ppgtt(dev_priv,
1054 i915_modparams.enable_ppgtt);
1055 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915_modparams.enable_ppgtt);
Chris Wilson39df9192016-07-20 13:31:57 +01001056
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001057 i915_modparams.semaphores =
1058 intel_sanitize_semaphores(dev_priv, i915_modparams.semaphores);
1059 DRM_DEBUG_DRIVER("use GPU semaphores? %s\n",
1060 yesno(i915_modparams.semaphores));
Arkadiusz Hilerd2be9f22017-03-14 15:28:10 +01001061
1062 intel_uc_sanitize_options(dev_priv);
Chuanxiao Dong67b7f332017-05-27 17:44:17 +08001063
1064 intel_gvt_sanitize_options(dev_priv);
Chris Wilson94b4f3b2016-07-05 10:40:20 +01001065}
1066
Chris Wilson0673ad42016-06-24 14:00:22 +01001067/**
1068 * i915_driver_init_hw - setup state requiring device access
1069 * @dev_priv: device private
1070 *
1071 * Setup state that requires accessing the device, but doesn't require
1072 * exposing the driver via kernel internal or userspace interfaces.
1073 */
1074static int i915_driver_init_hw(struct drm_i915_private *dev_priv)
1075{
David Weinehall52a05c32016-08-22 13:32:44 +03001076 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson0673ad42016-06-24 14:00:22 +01001077 int ret;
1078
1079 if (i915_inject_load_failure())
1080 return -ENODEV;
1081
Chris Wilson94b4f3b2016-07-05 10:40:20 +01001082 intel_device_info_runtime_init(dev_priv);
1083
1084 intel_sanitize_options(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001085
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001086 ret = i915_ggtt_probe_hw(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001087 if (ret)
1088 return ret;
1089
Chris Wilson0673ad42016-06-24 14:00:22 +01001090 /* WARNING: Apparently we must kick fbdev drivers before vgacon,
1091 * otherwise the vga fbdev driver falls over. */
1092 ret = i915_kick_out_firmware_fb(dev_priv);
1093 if (ret) {
1094 DRM_ERROR("failed to remove conflicting framebuffer drivers\n");
1095 goto out_ggtt;
1096 }
1097
1098 ret = i915_kick_out_vgacon(dev_priv);
1099 if (ret) {
1100 DRM_ERROR("failed to remove conflicting VGA console\n");
1101 goto out_ggtt;
1102 }
1103
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001104 ret = i915_ggtt_init_hw(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01001105 if (ret)
1106 return ret;
1107
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001108 ret = i915_ggtt_enable_hw(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01001109 if (ret) {
1110 DRM_ERROR("failed to enable GGTT\n");
1111 goto out_ggtt;
1112 }
1113
David Weinehall52a05c32016-08-22 13:32:44 +03001114 pci_set_master(pdev);
Chris Wilson0673ad42016-06-24 14:00:22 +01001115
1116 /* overlay on gen2 is broken and can't address above 1G */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001117 if (IS_GEN2(dev_priv)) {
David Weinehall52a05c32016-08-22 13:32:44 +03001118 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(30));
Chris Wilson0673ad42016-06-24 14:00:22 +01001119 if (ret) {
1120 DRM_ERROR("failed to set DMA mask\n");
1121
1122 goto out_ggtt;
1123 }
1124 }
1125
Chris Wilson0673ad42016-06-24 14:00:22 +01001126 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1127 * using 32bit addressing, overwriting memory if HWS is located
1128 * above 4GB.
1129 *
1130 * The documentation also mentions an issue with undefined
1131 * behaviour if any general state is accessed within a page above 4GB,
1132 * which also needs to be handled carefully.
1133 */
Jani Nikulac0f86832016-12-07 12:13:04 +02001134 if (IS_I965G(dev_priv) || IS_I965GM(dev_priv)) {
David Weinehall52a05c32016-08-22 13:32:44 +03001135 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Chris Wilson0673ad42016-06-24 14:00:22 +01001136
1137 if (ret) {
1138 DRM_ERROR("failed to set DMA mask\n");
1139
1140 goto out_ggtt;
1141 }
1142 }
1143
Chris Wilson0673ad42016-06-24 14:00:22 +01001144 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY,
1145 PM_QOS_DEFAULT_VALUE);
1146
1147 intel_uncore_sanitize(dev_priv);
1148
1149 intel_opregion_setup(dev_priv);
1150
1151 i915_gem_load_init_fences(dev_priv);
1152
1153 /* On the 945G/GM, the chipset reports the MSI capability on the
1154 * integrated graphics even though the support isn't actually there
1155 * according to the published specs. It doesn't appear to function
1156 * correctly in testing on 945G.
1157 * This may be a side effect of MSI having been made available for PEG
1158 * and the registers being closely associated.
1159 *
1160 * According to chipset errata, on the 965GM, MSI interrupts may
Ville Syrjäläe38c2da2017-06-26 23:30:51 +03001161 * be lost or delayed, and was defeatured. MSI interrupts seem to
1162 * get lost on g4x as well, and interrupt delivery seems to stay
1163 * properly dead afterwards. So we'll just disable them for all
1164 * pre-gen5 chipsets.
Chris Wilson0673ad42016-06-24 14:00:22 +01001165 */
Ville Syrjäläe38c2da2017-06-26 23:30:51 +03001166 if (INTEL_GEN(dev_priv) >= 5) {
David Weinehall52a05c32016-08-22 13:32:44 +03001167 if (pci_enable_msi(pdev) < 0)
Chris Wilson0673ad42016-06-24 14:00:22 +01001168 DRM_DEBUG_DRIVER("can't enable MSI");
1169 }
1170
Zhenyu Wang26f837e2017-01-13 10:46:09 +08001171 ret = intel_gvt_init(dev_priv);
1172 if (ret)
1173 goto out_ggtt;
1174
Chris Wilson0673ad42016-06-24 14:00:22 +01001175 return 0;
1176
1177out_ggtt:
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001178 i915_ggtt_cleanup_hw(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001179
1180 return ret;
1181}
1182
1183/**
1184 * i915_driver_cleanup_hw - cleanup the setup done in i915_driver_init_hw()
1185 * @dev_priv: device private
1186 */
1187static void i915_driver_cleanup_hw(struct drm_i915_private *dev_priv)
1188{
David Weinehall52a05c32016-08-22 13:32:44 +03001189 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson0673ad42016-06-24 14:00:22 +01001190
David Weinehall52a05c32016-08-22 13:32:44 +03001191 if (pdev->msi_enabled)
1192 pci_disable_msi(pdev);
Chris Wilson0673ad42016-06-24 14:00:22 +01001193
1194 pm_qos_remove_request(&dev_priv->pm_qos);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001195 i915_ggtt_cleanup_hw(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001196}
1197
1198/**
1199 * i915_driver_register - register the driver with the rest of the system
1200 * @dev_priv: device private
1201 *
1202 * Perform any steps necessary to make the driver available via kernel
1203 * internal or userspace interfaces.
1204 */
1205static void i915_driver_register(struct drm_i915_private *dev_priv)
1206{
Chris Wilson91c8a322016-07-05 10:40:23 +01001207 struct drm_device *dev = &dev_priv->drm;
Chris Wilson0673ad42016-06-24 14:00:22 +01001208
1209 i915_gem_shrinker_init(dev_priv);
1210
1211 /*
1212 * Notify a valid surface after modesetting,
1213 * when running inside a VM.
1214 */
1215 if (intel_vgpu_active(dev_priv))
1216 I915_WRITE(vgtif_reg(display_ready), VGT_DRV_DISPLAY_READY);
1217
1218 /* Reveal our presence to userspace */
1219 if (drm_dev_register(dev, 0) == 0) {
1220 i915_debugfs_register(dev_priv);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +00001221 i915_guc_log_register(dev_priv);
David Weinehall694c2822016-08-22 13:32:43 +03001222 i915_setup_sysfs(dev_priv);
Robert Bragg442b8c02016-11-07 19:49:53 +00001223
1224 /* Depends on sysfs having been initialized */
1225 i915_perf_register(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001226 } else
1227 DRM_ERROR("Failed to register driver for userspace access!\n");
1228
1229 if (INTEL_INFO(dev_priv)->num_pipes) {
1230 /* Must be done after probing outputs */
1231 intel_opregion_register(dev_priv);
1232 acpi_video_register();
1233 }
1234
1235 if (IS_GEN5(dev_priv))
1236 intel_gpu_ips_init(dev_priv);
1237
Jerome Anandeef57322017-01-25 04:27:49 +05301238 intel_audio_init(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001239
1240 /*
1241 * Some ports require correctly set-up hpd registers for detection to
1242 * work properly (leading to ghost connected connector status), e.g. VGA
1243 * on gm45. Hence we can only set up the initial fbdev config after hpd
1244 * irqs are fully enabled. We do it last so that the async config
1245 * cannot run before the connectors are registered.
1246 */
1247 intel_fbdev_initial_config_async(dev);
1248}
1249
1250/**
1251 * i915_driver_unregister - cleanup the registration done in i915_driver_regiser()
1252 * @dev_priv: device private
1253 */
1254static void i915_driver_unregister(struct drm_i915_private *dev_priv)
1255{
Daniel Vetter4f256d82017-07-15 00:46:55 +02001256 intel_fbdev_unregister(dev_priv);
Jerome Anandeef57322017-01-25 04:27:49 +05301257 intel_audio_deinit(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001258
1259 intel_gpu_ips_teardown();
1260 acpi_video_unregister();
1261 intel_opregion_unregister(dev_priv);
1262
Robert Bragg442b8c02016-11-07 19:49:53 +00001263 i915_perf_unregister(dev_priv);
1264
David Weinehall694c2822016-08-22 13:32:43 +03001265 i915_teardown_sysfs(dev_priv);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +00001266 i915_guc_log_unregister(dev_priv);
Chris Wilson91c8a322016-07-05 10:40:23 +01001267 drm_dev_unregister(&dev_priv->drm);
Chris Wilson0673ad42016-06-24 14:00:22 +01001268
1269 i915_gem_shrinker_cleanup(dev_priv);
1270}
1271
1272/**
1273 * i915_driver_load - setup chip and create an initial config
Joonas Lahtinend2ad3ae2016-11-10 15:36:34 +02001274 * @pdev: PCI device
1275 * @ent: matching PCI ID entry
Chris Wilson0673ad42016-06-24 14:00:22 +01001276 *
1277 * The driver load routine has to do several things:
1278 * - drive output discovery via intel_modeset_init()
1279 * - initialize the memory manager
1280 * - allocate initial config memory
1281 * - setup the DRM framebuffer with the allocated memory
1282 */
Chris Wilson42f55512016-06-24 14:00:26 +01001283int i915_driver_load(struct pci_dev *pdev, const struct pci_device_id *ent)
Chris Wilson0673ad42016-06-24 14:00:22 +01001284{
Maarten Lankhorst8d2b47d2017-02-02 08:41:42 +01001285 const struct intel_device_info *match_info =
1286 (struct intel_device_info *)ent->driver_data;
Chris Wilson0673ad42016-06-24 14:00:22 +01001287 struct drm_i915_private *dev_priv;
1288 int ret;
1289
Ville Syrjäläff4c3b72017-03-03 17:19:28 +02001290 /* Enable nuclear pageflip on ILK+ */
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00001291 if (!i915_modparams.nuclear_pageflip && match_info->gen < 5)
Maarten Lankhorst8d2b47d2017-02-02 08:41:42 +01001292 driver.driver_features &= ~DRIVER_ATOMIC;
Chris Wilsona09d0ba2016-06-24 14:00:27 +01001293
Chris Wilson0673ad42016-06-24 14:00:22 +01001294 ret = -ENOMEM;
1295 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
1296 if (dev_priv)
1297 ret = drm_dev_init(&dev_priv->drm, &driver, &pdev->dev);
1298 if (ret) {
Tvrtko Ursulin87a67522016-12-06 19:04:13 +00001299 DRM_DEV_ERROR(&pdev->dev, "allocation failed\n");
Chris Wilsoncad36882017-02-10 16:35:21 +00001300 goto out_free;
Chris Wilson0673ad42016-06-24 14:00:22 +01001301 }
1302
Chris Wilson0673ad42016-06-24 14:00:22 +01001303 dev_priv->drm.pdev = pdev;
1304 dev_priv->drm.dev_private = dev_priv;
Chris Wilson0673ad42016-06-24 14:00:22 +01001305
1306 ret = pci_enable_device(pdev);
1307 if (ret)
Chris Wilsoncad36882017-02-10 16:35:21 +00001308 goto out_fini;
Chris Wilson0673ad42016-06-24 14:00:22 +01001309
1310 pci_set_drvdata(pdev, &dev_priv->drm);
Imre Deakadfdf852017-05-02 15:04:09 +03001311 /*
1312 * Disable the system suspend direct complete optimization, which can
1313 * leave the device suspended skipping the driver's suspend handlers
1314 * if the device was already runtime suspended. This is needed due to
1315 * the difference in our runtime and system suspend sequence and
1316 * becaue the HDA driver may require us to enable the audio power
1317 * domain during system suspend.
1318 */
1319 pdev->dev_flags |= PCI_DEV_FLAGS_NEEDS_RESUME;
Chris Wilson0673ad42016-06-24 14:00:22 +01001320
1321 ret = i915_driver_init_early(dev_priv, ent);
1322 if (ret < 0)
1323 goto out_pci_disable;
1324
1325 intel_runtime_pm_get(dev_priv);
1326
1327 ret = i915_driver_init_mmio(dev_priv);
1328 if (ret < 0)
1329 goto out_runtime_pm_put;
1330
1331 ret = i915_driver_init_hw(dev_priv);
1332 if (ret < 0)
1333 goto out_cleanup_mmio;
1334
1335 /*
1336 * TODO: move the vblank init and parts of modeset init steps into one
1337 * of the i915_driver_init_/i915_driver_register functions according
1338 * to the role/effect of the given init step.
1339 */
1340 if (INTEL_INFO(dev_priv)->num_pipes) {
Chris Wilson91c8a322016-07-05 10:40:23 +01001341 ret = drm_vblank_init(&dev_priv->drm,
Chris Wilson0673ad42016-06-24 14:00:22 +01001342 INTEL_INFO(dev_priv)->num_pipes);
1343 if (ret)
1344 goto out_cleanup_hw;
1345 }
1346
Chris Wilson91c8a322016-07-05 10:40:23 +01001347 ret = i915_load_modeset_init(&dev_priv->drm);
Chris Wilson0673ad42016-06-24 14:00:22 +01001348 if (ret < 0)
Daniel Vetterbaf54382017-06-21 10:28:41 +02001349 goto out_cleanup_hw;
Chris Wilson0673ad42016-06-24 14:00:22 +01001350
1351 i915_driver_register(dev_priv);
1352
1353 intel_runtime_pm_enable(dev_priv);
1354
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05301355 intel_init_ipc(dev_priv);
Mahesh Kumara3a89862016-12-01 21:19:34 +05301356
Chris Wilson0525a062016-10-14 14:27:07 +01001357 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG))
1358 DRM_INFO("DRM_I915_DEBUG enabled\n");
1359 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
1360 DRM_INFO("DRM_I915_DEBUG_GEM enabled\n");
Chris Wilsonbc5ca472016-08-25 08:23:14 +01001361
Chris Wilson0673ad42016-06-24 14:00:22 +01001362 intel_runtime_pm_put(dev_priv);
1363
1364 return 0;
1365
Chris Wilson0673ad42016-06-24 14:00:22 +01001366out_cleanup_hw:
1367 i915_driver_cleanup_hw(dev_priv);
1368out_cleanup_mmio:
1369 i915_driver_cleanup_mmio(dev_priv);
1370out_runtime_pm_put:
1371 intel_runtime_pm_put(dev_priv);
1372 i915_driver_cleanup_early(dev_priv);
1373out_pci_disable:
1374 pci_disable_device(pdev);
Chris Wilsoncad36882017-02-10 16:35:21 +00001375out_fini:
Chris Wilson0673ad42016-06-24 14:00:22 +01001376 i915_load_error(dev_priv, "Device initialization failed (%d)\n", ret);
Chris Wilsoncad36882017-02-10 16:35:21 +00001377 drm_dev_fini(&dev_priv->drm);
1378out_free:
1379 kfree(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001380 return ret;
1381}
1382
Chris Wilson42f55512016-06-24 14:00:26 +01001383void i915_driver_unload(struct drm_device *dev)
Chris Wilson0673ad42016-06-24 14:00:22 +01001384{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001385 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +03001386 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson0673ad42016-06-24 14:00:22 +01001387
Daniel Vetter99c539b2017-07-15 00:46:56 +02001388 i915_driver_unregister(dev_priv);
1389
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001390 if (i915_gem_suspend(dev_priv))
Chris Wilson42f55512016-06-24 14:00:26 +01001391 DRM_ERROR("failed to idle hardware; continuing to unload!\n");
Chris Wilson0673ad42016-06-24 14:00:22 +01001392
1393 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
1394
Daniel Vetter18dddad2017-03-21 17:41:49 +01001395 drm_atomic_helper_shutdown(dev);
Maarten Lankhorsta667fb42016-12-15 15:29:44 +01001396
Zhenyu Wang26f837e2017-01-13 10:46:09 +08001397 intel_gvt_cleanup(dev_priv);
1398
Chris Wilson0673ad42016-06-24 14:00:22 +01001399 intel_modeset_cleanup(dev);
1400
1401 /*
1402 * free the memory space allocated for the child device
1403 * config parsed from VBT
1404 */
1405 if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
1406 kfree(dev_priv->vbt.child_dev);
1407 dev_priv->vbt.child_dev = NULL;
1408 dev_priv->vbt.child_dev_num = 0;
1409 }
1410 kfree(dev_priv->vbt.sdvo_lvds_vbt_mode);
1411 dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
1412 kfree(dev_priv->vbt.lfp_lvds_vbt_mode);
1413 dev_priv->vbt.lfp_lvds_vbt_mode = NULL;
1414
David Weinehall52a05c32016-08-22 13:32:44 +03001415 vga_switcheroo_unregister_client(pdev);
1416 vga_client_register(pdev, NULL, NULL, NULL);
Chris Wilson0673ad42016-06-24 14:00:22 +01001417
1418 intel_csr_ucode_fini(dev_priv);
1419
1420 /* Free error state after interrupts are fully disabled. */
1421 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson5a4c6f12017-02-14 16:46:11 +00001422 i915_reset_error_state(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001423
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01001424 i915_gem_fini(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -07001425 intel_uc_fini_fw(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001426 intel_fbc_cleanup_cfb(dev_priv);
1427
1428 intel_power_domains_fini(dev_priv);
1429
1430 i915_driver_cleanup_hw(dev_priv);
1431 i915_driver_cleanup_mmio(dev_priv);
1432
1433 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
Chris Wilsoncad36882017-02-10 16:35:21 +00001434}
1435
1436static void i915_driver_release(struct drm_device *dev)
1437{
1438 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson0673ad42016-06-24 14:00:22 +01001439
1440 i915_driver_cleanup_early(dev_priv);
Chris Wilsoncad36882017-02-10 16:35:21 +00001441 drm_dev_fini(&dev_priv->drm);
1442
1443 kfree(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001444}
1445
1446static int i915_driver_open(struct drm_device *dev, struct drm_file *file)
1447{
Chris Wilson829a0af2017-06-20 12:05:45 +01001448 struct drm_i915_private *i915 = to_i915(dev);
Chris Wilson0673ad42016-06-24 14:00:22 +01001449 int ret;
1450
Chris Wilson829a0af2017-06-20 12:05:45 +01001451 ret = i915_gem_open(i915, file);
Chris Wilson0673ad42016-06-24 14:00:22 +01001452 if (ret)
1453 return ret;
1454
1455 return 0;
1456}
1457
1458/**
1459 * i915_driver_lastclose - clean up after all DRM clients have exited
1460 * @dev: DRM device
1461 *
1462 * Take care of cleaning up after all DRM clients have exited. In the
1463 * mode setting case, we want to restore the kernel's initial mode (just
1464 * in case the last client left us in a bad state).
1465 *
1466 * Additionally, in the non-mode setting case, we'll tear down the GTT
1467 * and DMA structures, since the kernel won't be using them, and clea
1468 * up any GEM state.
1469 */
1470static void i915_driver_lastclose(struct drm_device *dev)
1471{
1472 intel_fbdev_restore_mode(dev);
1473 vga_switcheroo_process_delayed_switch();
1474}
1475
Daniel Vetter7d2ec882017-03-08 15:12:45 +01001476static void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Chris Wilson0673ad42016-06-24 14:00:22 +01001477{
Daniel Vetter7d2ec882017-03-08 15:12:45 +01001478 struct drm_i915_file_private *file_priv = file->driver_priv;
1479
Chris Wilson0673ad42016-06-24 14:00:22 +01001480 mutex_lock(&dev->struct_mutex);
Chris Wilson829a0af2017-06-20 12:05:45 +01001481 i915_gem_context_close(file);
Chris Wilson0673ad42016-06-24 14:00:22 +01001482 i915_gem_release(dev, file);
1483 mutex_unlock(&dev->struct_mutex);
Chris Wilson0673ad42016-06-24 14:00:22 +01001484
1485 kfree(file_priv);
1486}
1487
Imre Deak07f9cd02014-08-18 14:42:45 +03001488static void intel_suspend_encoders(struct drm_i915_private *dev_priv)
1489{
Chris Wilson91c8a322016-07-05 10:40:23 +01001490 struct drm_device *dev = &dev_priv->drm;
Jani Nikula19c80542015-12-16 12:48:16 +02001491 struct intel_encoder *encoder;
Imre Deak07f9cd02014-08-18 14:42:45 +03001492
1493 drm_modeset_lock_all(dev);
Jani Nikula19c80542015-12-16 12:48:16 +02001494 for_each_intel_encoder(dev, encoder)
1495 if (encoder->suspend)
1496 encoder->suspend(encoder);
Imre Deak07f9cd02014-08-18 14:42:45 +03001497 drm_modeset_unlock_all(dev);
1498}
1499
Paulo Zanoni1a5df182014-10-27 17:54:32 -02001500static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
1501 bool rpm_resume);
Imre Deak507e1262016-04-20 20:27:54 +03001502static int vlv_suspend_complete(struct drm_i915_private *dev_priv);
Suketu Shahf75a1982015-04-16 14:22:11 +05301503
Imre Deakbc872292015-11-18 17:32:30 +02001504static bool suspend_to_idle(struct drm_i915_private *dev_priv)
1505{
1506#if IS_ENABLED(CONFIG_ACPI_SLEEP)
1507 if (acpi_target_system_state() < ACPI_STATE_S3)
1508 return true;
1509#endif
1510 return false;
1511}
Sagar Kambleebc32822014-08-13 23:07:05 +05301512
Imre Deak5e365c32014-10-23 19:23:25 +03001513static int i915_drm_suspend(struct drm_device *dev)
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001514{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001515 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +03001516 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnese5747e32014-06-12 08:35:47 -07001517 pci_power_t opregion_target_state;
Daniel Vetterd5818932015-02-23 12:03:26 +01001518 int error;
Rafael J. Wysocki61caf872010-02-18 23:06:27 +01001519
Zhang Ruib8efb172013-02-05 15:41:53 +08001520 /* ignore lid events during suspend */
1521 mutex_lock(&dev_priv->modeset_restore_lock);
1522 dev_priv->modeset_restore = MODESET_SUSPENDED;
1523 mutex_unlock(&dev_priv->modeset_restore_lock);
1524
Imre Deak1f814da2015-12-16 02:52:19 +02001525 disable_rpm_wakeref_asserts(dev_priv);
1526
Paulo Zanonic67a4702013-08-19 13:18:09 -03001527 /* We do a lot of poking in a lot of registers, make sure they work
1528 * properly. */
Imre Deakda7e29b2014-02-18 00:02:02 +02001529 intel_display_set_init_power(dev_priv, true);
Paulo Zanonicb107992013-01-25 16:59:15 -02001530
Dave Airlie5bcf7192010-12-07 09:20:40 +10001531 drm_kms_helper_poll_disable(dev);
1532
David Weinehall52a05c32016-08-22 13:32:44 +03001533 pci_save_state(pdev);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001534
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001535 error = i915_gem_suspend(dev_priv);
Daniel Vetterd5818932015-02-23 12:03:26 +01001536 if (error) {
David Weinehall52a05c32016-08-22 13:32:44 +03001537 dev_err(&pdev->dev,
Daniel Vetterd5818932015-02-23 12:03:26 +01001538 "GEM idle failed, resume might fail\n");
Imre Deak1f814da2015-12-16 02:52:19 +02001539 goto out;
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001540 }
1541
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02001542 intel_display_suspend(dev);
Daniel Vetterd5818932015-02-23 12:03:26 +01001543
1544 intel_dp_mst_suspend(dev);
1545
1546 intel_runtime_pm_disable_interrupts(dev_priv);
1547 intel_hpd_cancel_work(dev_priv);
1548
1549 intel_suspend_encoders(dev_priv);
1550
Ville Syrjälä712bf362016-10-31 22:37:23 +02001551 intel_suspend_hw(dev_priv);
Daniel Vetterd5818932015-02-23 12:03:26 +01001552
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001553 i915_gem_suspend_gtt_mappings(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07001554
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +00001555 i915_save_state(dev_priv);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001556
Imre Deakbc872292015-11-18 17:32:30 +02001557 opregion_target_state = suspend_to_idle(dev_priv) ? PCI_D1 : PCI_D3cold;
Chris Wilson6f9f4b72016-05-23 15:08:09 +01001558 intel_opregion_notify_adapter(dev_priv, opregion_target_state);
Jesse Barnese5747e32014-06-12 08:35:47 -07001559
Hans de Goede68f60942017-02-10 11:28:01 +01001560 intel_uncore_suspend(dev_priv);
Chris Wilson03d92e42016-05-23 15:08:10 +01001561 intel_opregion_unregister(dev_priv);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001562
Chris Wilson82e3b8c2014-08-13 13:09:46 +01001563 intel_fbdev_set_suspend(dev, FBINFO_STATE_SUSPENDED, true);
Dave Airlie3fa016a2012-03-28 10:48:49 +01001564
Mika Kuoppala62d5d692014-02-25 17:11:28 +02001565 dev_priv->suspend_count++;
1566
Imre Deakf74ed082016-04-18 14:48:21 +03001567 intel_csr_ucode_suspend(dev_priv);
Imre Deakf514c2d2015-10-28 23:59:06 +02001568
Imre Deak1f814da2015-12-16 02:52:19 +02001569out:
1570 enable_rpm_wakeref_asserts(dev_priv);
1571
1572 return error;
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001573}
1574
David Weinehallc49d13e2016-08-22 13:32:42 +03001575static int i915_drm_suspend_late(struct drm_device *dev, bool hibernation)
Imre Deakc3c09c92014-10-23 19:23:15 +03001576{
David Weinehallc49d13e2016-08-22 13:32:42 +03001577 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +03001578 struct pci_dev *pdev = dev_priv->drm.pdev;
Imre Deakbc872292015-11-18 17:32:30 +02001579 bool fw_csr;
Imre Deakc3c09c92014-10-23 19:23:15 +03001580 int ret;
1581
Imre Deak1f814da2015-12-16 02:52:19 +02001582 disable_rpm_wakeref_asserts(dev_priv);
1583
Imre Deak4c494a52016-10-13 14:34:06 +03001584 intel_display_set_init_power(dev_priv, false);
1585
Imre Deakdd9f31c2017-08-16 17:46:07 +03001586 fw_csr = !IS_GEN9_LP(dev_priv) && !hibernation &&
Imre Deaka7c81252016-04-01 16:02:38 +03001587 suspend_to_idle(dev_priv) && dev_priv->csr.dmc_payload;
Imre Deakbc872292015-11-18 17:32:30 +02001588 /*
1589 * In case of firmware assisted context save/restore don't manually
1590 * deinit the power domains. This also means the CSR/DMC firmware will
1591 * stay active, it will power down any HW resources as required and
1592 * also enable deeper system power states that would be blocked if the
1593 * firmware was inactive.
1594 */
1595 if (!fw_csr)
1596 intel_power_domains_suspend(dev_priv);
Imre Deak73dfc222015-11-17 17:33:53 +02001597
Imre Deak507e1262016-04-20 20:27:54 +03001598 ret = 0;
Rodrigo Vivib9fd7992016-12-16 17:42:25 +02001599 if (IS_GEN9_LP(dev_priv))
Imre Deak507e1262016-04-20 20:27:54 +03001600 bxt_enable_dc9(dev_priv);
Imre Deakb8aea3d12016-04-20 20:27:55 +03001601 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Imre Deak507e1262016-04-20 20:27:54 +03001602 hsw_enable_pc8(dev_priv);
1603 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
1604 ret = vlv_suspend_complete(dev_priv);
Imre Deakc3c09c92014-10-23 19:23:15 +03001605
1606 if (ret) {
1607 DRM_ERROR("Suspend complete failed: %d\n", ret);
Imre Deakbc872292015-11-18 17:32:30 +02001608 if (!fw_csr)
1609 intel_power_domains_init_hw(dev_priv, true);
Imre Deakc3c09c92014-10-23 19:23:15 +03001610
Imre Deak1f814da2015-12-16 02:52:19 +02001611 goto out;
Imre Deakc3c09c92014-10-23 19:23:15 +03001612 }
1613
David Weinehall52a05c32016-08-22 13:32:44 +03001614 pci_disable_device(pdev);
Imre Deakab3be732015-03-02 13:04:41 +02001615 /*
Imre Deak54875572015-06-30 17:06:47 +03001616 * During hibernation on some platforms the BIOS may try to access
Imre Deakab3be732015-03-02 13:04:41 +02001617 * the device even though it's already in D3 and hang the machine. So
1618 * leave the device in D0 on those platforms and hope the BIOS will
Imre Deak54875572015-06-30 17:06:47 +03001619 * power down the device properly. The issue was seen on multiple old
1620 * GENs with different BIOS vendors, so having an explicit blacklist
1621 * is inpractical; apply the workaround on everything pre GEN6. The
1622 * platforms where the issue was seen:
1623 * Lenovo Thinkpad X301, X61s, X60, T60, X41
1624 * Fujitsu FSC S7110
1625 * Acer Aspire 1830T
Imre Deakab3be732015-03-02 13:04:41 +02001626 */
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +00001627 if (!(hibernation && INTEL_GEN(dev_priv) < 6))
David Weinehall52a05c32016-08-22 13:32:44 +03001628 pci_set_power_state(pdev, PCI_D3hot);
Imre Deakc3c09c92014-10-23 19:23:15 +03001629
Imre Deakbc872292015-11-18 17:32:30 +02001630 dev_priv->suspended_to_idle = suspend_to_idle(dev_priv);
1631
Imre Deak1f814da2015-12-16 02:52:19 +02001632out:
1633 enable_rpm_wakeref_asserts(dev_priv);
1634
1635 return ret;
Imre Deakc3c09c92014-10-23 19:23:15 +03001636}
1637
Matthew Aulda9a251c2016-12-02 10:24:11 +00001638static int i915_suspend_switcheroo(struct drm_device *dev, pm_message_t state)
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001639{
1640 int error;
1641
Chris Wilsonded8b072016-07-05 10:40:22 +01001642 if (!dev) {
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001643 DRM_ERROR("dev: %p\n", dev);
Keith Packard1ae8c0a2009-06-28 15:42:17 -07001644 DRM_ERROR("DRM not initialized, aborting suspend.\n");
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001645 return -ENODEV;
1646 }
1647
Imre Deak0b14cbd2014-09-10 18:16:55 +03001648 if (WARN_ON_ONCE(state.event != PM_EVENT_SUSPEND &&
1649 state.event != PM_EVENT_FREEZE))
1650 return -EINVAL;
Dave Airlie5bcf7192010-12-07 09:20:40 +10001651
1652 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1653 return 0;
Chris Wilson6eecba32010-09-08 09:45:11 +01001654
Imre Deak5e365c32014-10-23 19:23:25 +03001655 error = i915_drm_suspend(dev);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001656 if (error)
1657 return error;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001658
Imre Deakab3be732015-03-02 13:04:41 +02001659 return i915_drm_suspend_late(dev, false);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001660}
1661
Imre Deak5e365c32014-10-23 19:23:25 +03001662static int i915_drm_resume(struct drm_device *dev)
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001663{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001664 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläac840ae2016-05-06 21:35:55 +03001665 int ret;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001666
Imre Deak1f814da2015-12-16 02:52:19 +02001667 disable_rpm_wakeref_asserts(dev_priv);
Chris Wilsonabc80ab2016-08-24 10:27:01 +01001668 intel_sanitize_gt_powersave(dev_priv);
Imre Deak1f814da2015-12-16 02:52:19 +02001669
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001670 ret = i915_ggtt_enable_hw(dev_priv);
Ville Syrjäläac840ae2016-05-06 21:35:55 +03001671 if (ret)
1672 DRM_ERROR("failed to re-enable GGTT\n");
1673
Imre Deakf74ed082016-04-18 14:48:21 +03001674 intel_csr_ucode_resume(dev_priv);
1675
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001676 i915_gem_resume(dev_priv);
Paulo Zanoni9d49c0e2013-09-12 18:06:43 -03001677
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +00001678 i915_restore_state(dev_priv);
Imre Deak8090ba82016-08-10 14:07:33 +03001679 intel_pps_unlock_regs_wa(dev_priv);
Chris Wilson6f9f4b72016-05-23 15:08:09 +01001680 intel_opregion_setup(dev_priv);
Rafael J. Wysocki61caf872010-02-18 23:06:27 +01001681
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02001682 intel_init_pch_refclk(dev_priv);
Chris Wilson1833b132012-05-09 11:56:28 +01001683
Peter Antoine364aece2015-05-11 08:50:45 +01001684 /*
1685 * Interrupts have to be enabled before any batches are run. If not the
1686 * GPU will hang. i915_gem_init_hw() will initiate batches to
1687 * update/restore the context.
1688 *
Imre Deak908764f2016-11-29 21:40:29 +02001689 * drm_mode_config_reset() needs AUX interrupts.
1690 *
Peter Antoine364aece2015-05-11 08:50:45 +01001691 * Modeset enabling in intel_modeset_init_hw() also needs working
1692 * interrupts.
1693 */
1694 intel_runtime_pm_enable_interrupts(dev_priv);
1695
Imre Deak908764f2016-11-29 21:40:29 +02001696 drm_mode_config_reset(dev);
1697
Daniel Vetterd5818932015-02-23 12:03:26 +01001698 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001699 if (i915_gem_init_hw(dev_priv)) {
Daniel Vetterd5818932015-02-23 12:03:26 +01001700 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
Chris Wilson821ed7d2016-09-09 14:11:53 +01001701 i915_gem_set_wedged(dev_priv);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08001702 }
Daniel Vetterd5818932015-02-23 12:03:26 +01001703 mutex_unlock(&dev->struct_mutex);
1704
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00001705 intel_guc_resume(dev_priv);
Alex Daia1c41992015-09-30 09:46:37 -07001706
Daniel Vetterd5818932015-02-23 12:03:26 +01001707 intel_modeset_init_hw(dev);
1708
1709 spin_lock_irq(&dev_priv->irq_lock);
1710 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01001711 dev_priv->display.hpd_irq_setup(dev_priv);
Daniel Vetterd5818932015-02-23 12:03:26 +01001712 spin_unlock_irq(&dev_priv->irq_lock);
1713
Daniel Vetterd5818932015-02-23 12:03:26 +01001714 intel_dp_mst_resume(dev);
1715
Lyudea16b7652016-03-11 10:57:01 -05001716 intel_display_resume(dev);
1717
Lyudee0b70062016-11-01 21:06:30 -04001718 drm_kms_helper_poll_enable(dev);
1719
Daniel Vetterd5818932015-02-23 12:03:26 +01001720 /*
1721 * ... but also need to make sure that hotplug processing
1722 * doesn't cause havoc. Like in the driver load code we don't
1723 * bother with the tiny race here where we might loose hotplug
1724 * notifications.
1725 * */
1726 intel_hpd_init(dev_priv);
Jesse Barnes1daed3f2011-01-05 12:01:25 -08001727
Chris Wilson03d92e42016-05-23 15:08:10 +01001728 intel_opregion_register(dev_priv);
Chris Wilson44834a62010-08-19 16:09:23 +01001729
Chris Wilson82e3b8c2014-08-13 13:09:46 +01001730 intel_fbdev_set_suspend(dev, FBINFO_STATE_RUNNING, false);
Jesse Barnes073f34d2012-11-02 11:13:59 -07001731
Zhang Ruib8efb172013-02-05 15:41:53 +08001732 mutex_lock(&dev_priv->modeset_restore_lock);
1733 dev_priv->modeset_restore = MODESET_DONE;
1734 mutex_unlock(&dev_priv->modeset_restore_lock);
Paulo Zanoni8a187452013-12-06 20:32:13 -02001735
Chris Wilson6f9f4b72016-05-23 15:08:09 +01001736 intel_opregion_notify_adapter(dev_priv, PCI_D0);
Jesse Barnese5747e32014-06-12 08:35:47 -07001737
Chris Wilson54b4f682016-07-21 21:16:19 +01001738 intel_autoenable_gt_powersave(dev_priv);
Imre Deakee6f2802014-10-23 19:23:22 +03001739
Imre Deak1f814da2015-12-16 02:52:19 +02001740 enable_rpm_wakeref_asserts(dev_priv);
1741
Chris Wilson074c6ad2014-04-09 09:19:43 +01001742 return 0;
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001743}
1744
Imre Deak5e365c32014-10-23 19:23:25 +03001745static int i915_drm_resume_early(struct drm_device *dev)
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001746{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001747 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +03001748 struct pci_dev *pdev = dev_priv->drm.pdev;
Imre Deak44410cd2016-04-18 14:45:54 +03001749 int ret;
Imre Deak36d61e62014-10-23 19:23:24 +03001750
Imre Deak76c4b252014-04-01 19:55:22 +03001751 /*
1752 * We have a resume ordering issue with the snd-hda driver also
1753 * requiring our device to be power up. Due to the lack of a
1754 * parent/child relationship we currently solve this with an early
1755 * resume hook.
1756 *
1757 * FIXME: This should be solved with a special hdmi sink device or
1758 * similar so that power domains can be employed.
1759 */
Imre Deak44410cd2016-04-18 14:45:54 +03001760
1761 /*
1762 * Note that we need to set the power state explicitly, since we
1763 * powered off the device during freeze and the PCI core won't power
1764 * it back up for us during thaw. Powering off the device during
1765 * freeze is not a hard requirement though, and during the
1766 * suspend/resume phases the PCI core makes sure we get here with the
1767 * device powered on. So in case we change our freeze logic and keep
1768 * the device powered we can also remove the following set power state
1769 * call.
1770 */
David Weinehall52a05c32016-08-22 13:32:44 +03001771 ret = pci_set_power_state(pdev, PCI_D0);
Imre Deak44410cd2016-04-18 14:45:54 +03001772 if (ret) {
1773 DRM_ERROR("failed to set PCI D0 power state (%d)\n", ret);
1774 goto out;
1775 }
1776
1777 /*
1778 * Note that pci_enable_device() first enables any parent bridge
1779 * device and only then sets the power state for this device. The
1780 * bridge enabling is a nop though, since bridge devices are resumed
1781 * first. The order of enabling power and enabling the device is
1782 * imposed by the PCI core as described above, so here we preserve the
1783 * same order for the freeze/thaw phases.
1784 *
1785 * TODO: eventually we should remove pci_disable_device() /
1786 * pci_enable_enable_device() from suspend/resume. Due to how they
1787 * depend on the device enable refcount we can't anyway depend on them
1788 * disabling/enabling the device.
1789 */
David Weinehall52a05c32016-08-22 13:32:44 +03001790 if (pci_enable_device(pdev)) {
Imre Deakbc872292015-11-18 17:32:30 +02001791 ret = -EIO;
1792 goto out;
1793 }
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001794
David Weinehall52a05c32016-08-22 13:32:44 +03001795 pci_set_master(pdev);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001796
Imre Deak1f814da2015-12-16 02:52:19 +02001797 disable_rpm_wakeref_asserts(dev_priv);
1798
Wayne Boyer666a4532015-12-09 12:29:35 -08001799 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Paulo Zanoni1a5df182014-10-27 17:54:32 -02001800 ret = vlv_resume_prepare(dev_priv, false);
Imre Deak36d61e62014-10-23 19:23:24 +03001801 if (ret)
Damien Lespiauff0b1872015-05-20 14:45:15 +01001802 DRM_ERROR("Resume prepare failed: %d, continuing anyway\n",
1803 ret);
Imre Deak36d61e62014-10-23 19:23:24 +03001804
Hans de Goede68f60942017-02-10 11:28:01 +01001805 intel_uncore_resume_early(dev_priv);
Paulo Zanoniefee8332014-10-27 17:54:33 -02001806
Rodrigo Vivib9fd7992016-12-16 17:42:25 +02001807 if (IS_GEN9_LP(dev_priv)) {
Imre Deakda2f41d2016-04-20 20:27:56 +03001808 if (!dev_priv->suspended_to_idle)
1809 gen9_sanitize_dc_state(dev_priv);
Imre Deak507e1262016-04-20 20:27:54 +03001810 bxt_disable_dc9(dev_priv);
Imre Deakda2f41d2016-04-20 20:27:56 +03001811 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiaua9a6b732015-05-20 14:45:14 +01001812 hsw_disable_pc8(dev_priv);
Imre Deakda2f41d2016-04-20 20:27:56 +03001813 }
Paulo Zanoniefee8332014-10-27 17:54:33 -02001814
Chris Wilsondc979972016-05-10 14:10:04 +01001815 intel_uncore_sanitize(dev_priv);
Imre Deakbc872292015-11-18 17:32:30 +02001816
Rodrigo Vivib9fd7992016-12-16 17:42:25 +02001817 if (IS_GEN9_LP(dev_priv) ||
Imre Deaka7c81252016-04-01 16:02:38 +03001818 !(dev_priv->suspended_to_idle && dev_priv->csr.dmc_payload))
Imre Deakbc872292015-11-18 17:32:30 +02001819 intel_power_domains_init_hw(dev_priv, true);
1820
Chris Wilson24145512017-01-24 11:01:35 +00001821 i915_gem_sanitize(dev_priv);
1822
Imre Deak6e35e8a2016-04-18 10:04:19 +03001823 enable_rpm_wakeref_asserts(dev_priv);
1824
Imre Deakbc872292015-11-18 17:32:30 +02001825out:
1826 dev_priv->suspended_to_idle = false;
Imre Deak36d61e62014-10-23 19:23:24 +03001827
1828 return ret;
Imre Deak76c4b252014-04-01 19:55:22 +03001829}
1830
Tvrtko Ursulin7f26cb82016-12-01 14:16:41 +00001831static int i915_resume_switcheroo(struct drm_device *dev)
Imre Deak76c4b252014-04-01 19:55:22 +03001832{
Imre Deak50a00722014-10-23 19:23:17 +03001833 int ret;
Imre Deak76c4b252014-04-01 19:55:22 +03001834
Imre Deak097dd832014-10-23 19:23:19 +03001835 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1836 return 0;
1837
Imre Deak5e365c32014-10-23 19:23:25 +03001838 ret = i915_drm_resume_early(dev);
Imre Deak50a00722014-10-23 19:23:17 +03001839 if (ret)
1840 return ret;
1841
Imre Deak5a175142014-10-23 19:23:18 +03001842 return i915_drm_resume(dev);
1843}
1844
Ben Gamari11ed50e2009-09-14 17:48:45 -04001845/**
Eugeni Dodonovf3953dc2011-11-28 16:15:17 -02001846 * i915_reset - reset chip after a hang
Chris Wilson535275d2017-07-21 13:32:37 +01001847 * @i915: #drm_i915_private to reset
1848 * @flags: Instructions
Ben Gamari11ed50e2009-09-14 17:48:45 -04001849 *
Chris Wilson780f2622016-09-09 14:11:52 +01001850 * Reset the chip. Useful if a hang is detected. Marks the device as wedged
1851 * on failure.
Ben Gamari11ed50e2009-09-14 17:48:45 -04001852 *
Chris Wilson221fe792016-09-09 14:11:51 +01001853 * Caller must hold the struct_mutex.
1854 *
Ben Gamari11ed50e2009-09-14 17:48:45 -04001855 * Procedure is fairly simple:
1856 * - reset the chip using the reset reg
1857 * - re-init context state
1858 * - re-init hardware status page
1859 * - re-init ring buffer
1860 * - re-init interrupt state
1861 * - re-init display
1862 */
Chris Wilson535275d2017-07-21 13:32:37 +01001863void i915_reset(struct drm_i915_private *i915, unsigned int flags)
Ben Gamari11ed50e2009-09-14 17:48:45 -04001864{
Chris Wilson535275d2017-07-21 13:32:37 +01001865 struct i915_gpu_error *error = &i915->gpu_error;
Kenneth Graunke0573ed42010-09-11 03:17:19 -07001866 int ret;
Ben Gamari11ed50e2009-09-14 17:48:45 -04001867
Chris Wilson535275d2017-07-21 13:32:37 +01001868 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson8c185ec2017-03-16 17:13:02 +00001869 GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, &error->flags));
Chris Wilson221fe792016-09-09 14:11:51 +01001870
Chris Wilson8c185ec2017-03-16 17:13:02 +00001871 if (!test_bit(I915_RESET_HANDOFF, &error->flags))
Chris Wilson780f2622016-09-09 14:11:52 +01001872 return;
Ben Gamari11ed50e2009-09-14 17:48:45 -04001873
Chris Wilsond98c52c2016-04-13 17:35:05 +01001874 /* Clear any previous failed attempts at recovery. Time to try again. */
Chris Wilson535275d2017-07-21 13:32:37 +01001875 if (!i915_gem_unset_wedged(i915))
Chris Wilson2e8f9d32017-03-16 17:13:04 +00001876 goto wakeup;
1877
Chris Wilson535275d2017-07-21 13:32:37 +01001878 if (!(flags & I915_RESET_QUIET))
1879 dev_notice(i915->drm.dev, "Resetting chip after gpu hang\n");
Chris Wilson8af29b02016-09-09 14:11:47 +01001880 error->reset_count++;
Chris Wilsond98c52c2016-04-13 17:35:05 +01001881
Chris Wilson535275d2017-07-21 13:32:37 +01001882 disable_irq(i915->drm.irq);
1883 ret = i915_gem_reset_prepare(i915);
Chris Wilson0e178ae2017-01-17 17:59:06 +02001884 if (ret) {
1885 DRM_ERROR("GPU recovery failed\n");
Chris Wilson535275d2017-07-21 13:32:37 +01001886 intel_gpu_reset(i915, ALL_ENGINES);
Chris Wilson0e178ae2017-01-17 17:59:06 +02001887 goto error;
1888 }
Chris Wilson9e60ab02016-10-04 21:11:28 +01001889
Chris Wilson535275d2017-07-21 13:32:37 +01001890 ret = intel_gpu_reset(i915, ALL_ENGINES);
Kenneth Graunke0573ed42010-09-11 03:17:19 -07001891 if (ret) {
Chris Wilson804e59a2016-04-13 17:35:09 +01001892 if (ret != -ENODEV)
1893 DRM_ERROR("Failed to reset chip: %i\n", ret);
1894 else
1895 DRM_DEBUG_DRIVER("GPU reset disabled\n");
Chris Wilsond98c52c2016-04-13 17:35:05 +01001896 goto error;
Ben Gamari11ed50e2009-09-14 17:48:45 -04001897 }
1898
Chris Wilson535275d2017-07-21 13:32:37 +01001899 i915_gem_reset(i915);
1900 intel_overlay_reset(i915);
Ville Syrjälä1362b772014-11-26 17:07:29 +02001901
Ben Gamari11ed50e2009-09-14 17:48:45 -04001902 /* Ok, now get things going again... */
1903
1904 /*
1905 * Everything depends on having the GTT running, so we need to start
Chris Wilson0db8c962017-09-06 12:14:05 +01001906 * there.
1907 */
1908 ret = i915_ggtt_enable_hw(i915);
1909 if (ret) {
1910 DRM_ERROR("Failed to re-enable GGTT following reset %d\n", ret);
1911 goto error;
1912 }
1913
1914 /*
Ben Gamari11ed50e2009-09-14 17:48:45 -04001915 * Next we need to restore the context, but we don't use those
1916 * yet either...
1917 *
1918 * Ring buffer needs to be re-initialized in the KMS case, or if X
1919 * was running at the time of the reset (i.e. we weren't VT
1920 * switched away).
1921 */
Chris Wilson535275d2017-07-21 13:32:37 +01001922 ret = i915_gem_init_hw(i915);
Daniel Vetter33d30a92015-02-23 12:03:27 +01001923 if (ret) {
1924 DRM_ERROR("Failed hw init on reset %d\n", ret);
Chris Wilsond98c52c2016-04-13 17:35:05 +01001925 goto error;
Ben Gamari11ed50e2009-09-14 17:48:45 -04001926 }
1927
Chris Wilson535275d2017-07-21 13:32:37 +01001928 i915_queue_hangcheck(i915);
Chris Wilsonc2a126a2016-11-22 14:41:19 +00001929
Chris Wilson2e8f9d32017-03-16 17:13:04 +00001930finish:
Chris Wilson535275d2017-07-21 13:32:37 +01001931 i915_gem_reset_finish(i915);
1932 enable_irq(i915->drm.irq);
Chris Wilson8c185ec2017-03-16 17:13:02 +00001933
Chris Wilson2e8f9d32017-03-16 17:13:04 +00001934wakeup:
Chris Wilson8c185ec2017-03-16 17:13:02 +00001935 clear_bit(I915_RESET_HANDOFF, &error->flags);
1936 wake_up_bit(&error->flags, I915_RESET_HANDOFF);
Chris Wilson780f2622016-09-09 14:11:52 +01001937 return;
Chris Wilsond98c52c2016-04-13 17:35:05 +01001938
1939error:
Chris Wilson535275d2017-07-21 13:32:37 +01001940 i915_gem_set_wedged(i915);
1941 i915_gem_retire_requests(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00001942 goto finish;
Ben Gamari11ed50e2009-09-14 17:48:45 -04001943}
1944
Michel Thierry142bc7d2017-06-20 10:57:46 +01001945/**
1946 * i915_reset_engine - reset GPU engine to recover from a hang
1947 * @engine: engine to reset
Chris Wilson535275d2017-07-21 13:32:37 +01001948 * @flags: options
Michel Thierry142bc7d2017-06-20 10:57:46 +01001949 *
1950 * Reset a specific GPU engine. Useful if a hang is detected.
1951 * Returns zero on successful reset or otherwise an error code.
Michel Thierrya1ef70e2017-06-20 10:57:47 +01001952 *
1953 * Procedure is:
1954 * - identifies the request that caused the hang and it is dropped
1955 * - reset engine (which will force the engine to idle)
1956 * - re-init/configure engine
Michel Thierry142bc7d2017-06-20 10:57:46 +01001957 */
Chris Wilson535275d2017-07-21 13:32:37 +01001958int i915_reset_engine(struct intel_engine_cs *engine, unsigned int flags)
Michel Thierry142bc7d2017-06-20 10:57:46 +01001959{
Michel Thierrya1ef70e2017-06-20 10:57:47 +01001960 struct i915_gpu_error *error = &engine->i915->gpu_error;
1961 struct drm_i915_gem_request *active_request;
1962 int ret;
1963
1964 GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &error->flags));
1965
Chris Wilson535275d2017-07-21 13:32:37 +01001966 if (!(flags & I915_RESET_QUIET)) {
1967 dev_notice(engine->i915->drm.dev,
1968 "Resetting %s after gpu hang\n", engine->name);
1969 }
Chris Wilson73676122017-07-21 13:32:31 +01001970 error->reset_engine_count[engine->id]++;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01001971
1972 active_request = i915_gem_reset_prepare_engine(engine);
1973 if (IS_ERR(active_request)) {
1974 DRM_DEBUG_DRIVER("Previous reset failed, promote to full reset\n");
1975 ret = PTR_ERR(active_request);
1976 goto out;
1977 }
1978
Chris Wilsonb4f3e162017-07-21 13:32:20 +01001979 ret = intel_gpu_reset(engine->i915, intel_engine_flag(engine));
Chris Wilson0364cd12017-07-21 13:32:21 +01001980 if (ret) {
1981 /* If we fail here, we expect to fallback to a global reset */
1982 DRM_DEBUG_DRIVER("Failed to reset %s, ret=%d\n",
1983 engine->name, ret);
1984 goto out;
1985 }
Chris Wilsonb4f3e162017-07-21 13:32:20 +01001986
Michel Thierrya1ef70e2017-06-20 10:57:47 +01001987 /*
1988 * The request that caused the hang is stuck on elsp, we know the
1989 * active request and can drop it, adjust head to skip the offending
1990 * request to resume executing remaining requests in the queue.
1991 */
1992 i915_gem_reset_engine(engine, active_request);
1993
Michel Thierrya1ef70e2017-06-20 10:57:47 +01001994 /*
1995 * The engine and its registers (and workarounds in case of render)
1996 * have been reset to their default values. Follow the init_ring
1997 * process to program RING_MODE, HWSP and re-enable submission.
1998 */
1999 ret = engine->init_hw(engine);
Michel Thierry702c8f82017-06-20 10:57:48 +01002000 if (ret)
2001 goto out;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002002
2003out:
Chris Wilson0364cd12017-07-21 13:32:21 +01002004 i915_gem_reset_finish_engine(engine);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002005 return ret;
Michel Thierry142bc7d2017-06-20 10:57:46 +01002006}
2007
David Weinehallc49d13e2016-08-22 13:32:42 +03002008static int i915_pm_suspend(struct device *kdev)
Kristian Høgsberg112b7152009-01-04 16:55:33 -05002009{
David Weinehallc49d13e2016-08-22 13:32:42 +03002010 struct pci_dev *pdev = to_pci_dev(kdev);
2011 struct drm_device *dev = pci_get_drvdata(pdev);
Kristian Høgsberg112b7152009-01-04 16:55:33 -05002012
David Weinehallc49d13e2016-08-22 13:32:42 +03002013 if (!dev) {
2014 dev_err(kdev, "DRM not initialized, aborting suspend.\n");
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01002015 return -ENODEV;
2016 }
Kristian Høgsberg112b7152009-01-04 16:55:33 -05002017
David Weinehallc49d13e2016-08-22 13:32:42 +03002018 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airlie5bcf7192010-12-07 09:20:40 +10002019 return 0;
2020
David Weinehallc49d13e2016-08-22 13:32:42 +03002021 return i915_drm_suspend(dev);
Imre Deak76c4b252014-04-01 19:55:22 +03002022}
2023
David Weinehallc49d13e2016-08-22 13:32:42 +03002024static int i915_pm_suspend_late(struct device *kdev)
Imre Deak76c4b252014-04-01 19:55:22 +03002025{
David Weinehallc49d13e2016-08-22 13:32:42 +03002026 struct drm_device *dev = &kdev_to_i915(kdev)->drm;
Imre Deak76c4b252014-04-01 19:55:22 +03002027
2028 /*
Damien Lespiauc965d9952015-05-18 19:53:48 +01002029 * We have a suspend ordering issue with the snd-hda driver also
Imre Deak76c4b252014-04-01 19:55:22 +03002030 * requiring our device to be power up. Due to the lack of a
2031 * parent/child relationship we currently solve this with an late
2032 * suspend hook.
2033 *
2034 * FIXME: This should be solved with a special hdmi sink device or
2035 * similar so that power domains can be employed.
2036 */
David Weinehallc49d13e2016-08-22 13:32:42 +03002037 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Imre Deak76c4b252014-04-01 19:55:22 +03002038 return 0;
Kristian Høgsberg112b7152009-01-04 16:55:33 -05002039
David Weinehallc49d13e2016-08-22 13:32:42 +03002040 return i915_drm_suspend_late(dev, false);
Imre Deakab3be732015-03-02 13:04:41 +02002041}
2042
David Weinehallc49d13e2016-08-22 13:32:42 +03002043static int i915_pm_poweroff_late(struct device *kdev)
Imre Deakab3be732015-03-02 13:04:41 +02002044{
David Weinehallc49d13e2016-08-22 13:32:42 +03002045 struct drm_device *dev = &kdev_to_i915(kdev)->drm;
Imre Deakab3be732015-03-02 13:04:41 +02002046
David Weinehallc49d13e2016-08-22 13:32:42 +03002047 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Imre Deakab3be732015-03-02 13:04:41 +02002048 return 0;
2049
David Weinehallc49d13e2016-08-22 13:32:42 +03002050 return i915_drm_suspend_late(dev, true);
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002051}
2052
David Weinehallc49d13e2016-08-22 13:32:42 +03002053static int i915_pm_resume_early(struct device *kdev)
Imre Deak76c4b252014-04-01 19:55:22 +03002054{
David Weinehallc49d13e2016-08-22 13:32:42 +03002055 struct drm_device *dev = &kdev_to_i915(kdev)->drm;
Imre Deak76c4b252014-04-01 19:55:22 +03002056
David Weinehallc49d13e2016-08-22 13:32:42 +03002057 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Imre Deak097dd832014-10-23 19:23:19 +03002058 return 0;
2059
David Weinehallc49d13e2016-08-22 13:32:42 +03002060 return i915_drm_resume_early(dev);
Imre Deak76c4b252014-04-01 19:55:22 +03002061}
2062
David Weinehallc49d13e2016-08-22 13:32:42 +03002063static int i915_pm_resume(struct device *kdev)
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002064{
David Weinehallc49d13e2016-08-22 13:32:42 +03002065 struct drm_device *dev = &kdev_to_i915(kdev)->drm;
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01002066
David Weinehallc49d13e2016-08-22 13:32:42 +03002067 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Imre Deak097dd832014-10-23 19:23:19 +03002068 return 0;
2069
David Weinehallc49d13e2016-08-22 13:32:42 +03002070 return i915_drm_resume(dev);
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002071}
2072
Chris Wilson1f19ac22016-05-14 07:26:32 +01002073/* freeze: before creating the hibernation_image */
David Weinehallc49d13e2016-08-22 13:32:42 +03002074static int i915_pm_freeze(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002075{
Imre Deakdd9f31c2017-08-16 17:46:07 +03002076 struct drm_device *dev = &kdev_to_i915(kdev)->drm;
Chris Wilson6a800ea2016-09-21 14:51:07 +01002077 int ret;
2078
Imre Deakdd9f31c2017-08-16 17:46:07 +03002079 if (dev->switch_power_state != DRM_SWITCH_POWER_OFF) {
2080 ret = i915_drm_suspend(dev);
2081 if (ret)
2082 return ret;
2083 }
Chris Wilson6a800ea2016-09-21 14:51:07 +01002084
2085 ret = i915_gem_freeze(kdev_to_i915(kdev));
2086 if (ret)
2087 return ret;
2088
2089 return 0;
Chris Wilson1f19ac22016-05-14 07:26:32 +01002090}
2091
David Weinehallc49d13e2016-08-22 13:32:42 +03002092static int i915_pm_freeze_late(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002093{
Imre Deakdd9f31c2017-08-16 17:46:07 +03002094 struct drm_device *dev = &kdev_to_i915(kdev)->drm;
Chris Wilson461fb992016-05-14 07:26:33 +01002095 int ret;
2096
Imre Deakdd9f31c2017-08-16 17:46:07 +03002097 if (dev->switch_power_state != DRM_SWITCH_POWER_OFF) {
2098 ret = i915_drm_suspend_late(dev, true);
2099 if (ret)
2100 return ret;
2101 }
Chris Wilson461fb992016-05-14 07:26:33 +01002102
David Weinehallc49d13e2016-08-22 13:32:42 +03002103 ret = i915_gem_freeze_late(kdev_to_i915(kdev));
Chris Wilson461fb992016-05-14 07:26:33 +01002104 if (ret)
2105 return ret;
2106
2107 return 0;
Chris Wilson1f19ac22016-05-14 07:26:32 +01002108}
2109
2110/* thaw: called after creating the hibernation image, but before turning off. */
David Weinehallc49d13e2016-08-22 13:32:42 +03002111static int i915_pm_thaw_early(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002112{
David Weinehallc49d13e2016-08-22 13:32:42 +03002113 return i915_pm_resume_early(kdev);
Chris Wilson1f19ac22016-05-14 07:26:32 +01002114}
2115
David Weinehallc49d13e2016-08-22 13:32:42 +03002116static int i915_pm_thaw(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002117{
David Weinehallc49d13e2016-08-22 13:32:42 +03002118 return i915_pm_resume(kdev);
Chris Wilson1f19ac22016-05-14 07:26:32 +01002119}
2120
2121/* restore: called after loading the hibernation image. */
David Weinehallc49d13e2016-08-22 13:32:42 +03002122static int i915_pm_restore_early(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002123{
David Weinehallc49d13e2016-08-22 13:32:42 +03002124 return i915_pm_resume_early(kdev);
Chris Wilson1f19ac22016-05-14 07:26:32 +01002125}
2126
David Weinehallc49d13e2016-08-22 13:32:42 +03002127static int i915_pm_restore(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002128{
David Weinehallc49d13e2016-08-22 13:32:42 +03002129 return i915_pm_resume(kdev);
Chris Wilson1f19ac22016-05-14 07:26:32 +01002130}
2131
Imre Deakddeea5b2014-05-05 15:19:56 +03002132/*
2133 * Save all Gunit registers that may be lost after a D3 and a subsequent
2134 * S0i[R123] transition. The list of registers needing a save/restore is
2135 * defined in the VLV2_S0IXRegs document. This documents marks all Gunit
2136 * registers in the following way:
2137 * - Driver: saved/restored by the driver
2138 * - Punit : saved/restored by the Punit firmware
2139 * - No, w/o marking: no need to save/restore, since the register is R/O or
2140 * used internally by the HW in a way that doesn't depend
2141 * keeping the content across a suspend/resume.
2142 * - Debug : used for debugging
2143 *
2144 * We save/restore all registers marked with 'Driver', with the following
2145 * exceptions:
2146 * - Registers out of use, including also registers marked with 'Debug'.
2147 * These have no effect on the driver's operation, so we don't save/restore
2148 * them to reduce the overhead.
2149 * - Registers that are fully setup by an initialization function called from
2150 * the resume path. For example many clock gating and RPS/RC6 registers.
2151 * - Registers that provide the right functionality with their reset defaults.
2152 *
2153 * TODO: Except for registers that based on the above 3 criteria can be safely
2154 * ignored, we save/restore all others, practically treating the HW context as
2155 * a black-box for the driver. Further investigation is needed to reduce the
2156 * saved/restored registers even further, by following the same 3 criteria.
2157 */
2158static void vlv_save_gunit_s0ix_state(struct drm_i915_private *dev_priv)
2159{
2160 struct vlv_s0ix_state *s = &dev_priv->vlv_s0ix_state;
2161 int i;
2162
2163 /* GAM 0x4000-0x4770 */
2164 s->wr_watermark = I915_READ(GEN7_WR_WATERMARK);
2165 s->gfx_prio_ctrl = I915_READ(GEN7_GFX_PRIO_CTRL);
2166 s->arb_mode = I915_READ(ARB_MODE);
2167 s->gfx_pend_tlb0 = I915_READ(GEN7_GFX_PEND_TLB0);
2168 s->gfx_pend_tlb1 = I915_READ(GEN7_GFX_PEND_TLB1);
2169
2170 for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002171 s->lra_limits[i] = I915_READ(GEN7_LRA_LIMITS(i));
Imre Deakddeea5b2014-05-05 15:19:56 +03002172
2173 s->media_max_req_count = I915_READ(GEN7_MEDIA_MAX_REQ_COUNT);
Imre Deakb5f1c972015-04-15 16:52:30 -07002174 s->gfx_max_req_count = I915_READ(GEN7_GFX_MAX_REQ_COUNT);
Imre Deakddeea5b2014-05-05 15:19:56 +03002175
2176 s->render_hwsp = I915_READ(RENDER_HWS_PGA_GEN7);
2177 s->ecochk = I915_READ(GAM_ECOCHK);
2178 s->bsd_hwsp = I915_READ(BSD_HWS_PGA_GEN7);
2179 s->blt_hwsp = I915_READ(BLT_HWS_PGA_GEN7);
2180
2181 s->tlb_rd_addr = I915_READ(GEN7_TLB_RD_ADDR);
2182
2183 /* MBC 0x9024-0x91D0, 0x8500 */
2184 s->g3dctl = I915_READ(VLV_G3DCTL);
2185 s->gsckgctl = I915_READ(VLV_GSCKGCTL);
2186 s->mbctl = I915_READ(GEN6_MBCTL);
2187
2188 /* GCP 0x9400-0x9424, 0x8100-0x810C */
2189 s->ucgctl1 = I915_READ(GEN6_UCGCTL1);
2190 s->ucgctl3 = I915_READ(GEN6_UCGCTL3);
2191 s->rcgctl1 = I915_READ(GEN6_RCGCTL1);
2192 s->rcgctl2 = I915_READ(GEN6_RCGCTL2);
2193 s->rstctl = I915_READ(GEN6_RSTCTL);
2194 s->misccpctl = I915_READ(GEN7_MISCCPCTL);
2195
2196 /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
2197 s->gfxpause = I915_READ(GEN6_GFXPAUSE);
2198 s->rpdeuhwtc = I915_READ(GEN6_RPDEUHWTC);
2199 s->rpdeuc = I915_READ(GEN6_RPDEUC);
2200 s->ecobus = I915_READ(ECOBUS);
2201 s->pwrdwnupctl = I915_READ(VLV_PWRDWNUPCTL);
2202 s->rp_down_timeout = I915_READ(GEN6_RP_DOWN_TIMEOUT);
2203 s->rp_deucsw = I915_READ(GEN6_RPDEUCSW);
2204 s->rcubmabdtmr = I915_READ(GEN6_RCUBMABDTMR);
2205 s->rcedata = I915_READ(VLV_RCEDATA);
2206 s->spare2gh = I915_READ(VLV_SPAREG2H);
2207
2208 /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
2209 s->gt_imr = I915_READ(GTIMR);
2210 s->gt_ier = I915_READ(GTIER);
2211 s->pm_imr = I915_READ(GEN6_PMIMR);
2212 s->pm_ier = I915_READ(GEN6_PMIER);
2213
2214 for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002215 s->gt_scratch[i] = I915_READ(GEN7_GT_SCRATCH(i));
Imre Deakddeea5b2014-05-05 15:19:56 +03002216
2217 /* GT SA CZ domain, 0x100000-0x138124 */
2218 s->tilectl = I915_READ(TILECTL);
2219 s->gt_fifoctl = I915_READ(GTFIFOCTL);
2220 s->gtlc_wake_ctrl = I915_READ(VLV_GTLC_WAKE_CTRL);
2221 s->gtlc_survive = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2222 s->pmwgicz = I915_READ(VLV_PMWGICZ);
2223
2224 /* Gunit-Display CZ domain, 0x182028-0x1821CF */
2225 s->gu_ctl0 = I915_READ(VLV_GU_CTL0);
2226 s->gu_ctl1 = I915_READ(VLV_GU_CTL1);
Jesse Barnes9c252102015-04-01 14:22:57 -07002227 s->pcbr = I915_READ(VLV_PCBR);
Imre Deakddeea5b2014-05-05 15:19:56 +03002228 s->clock_gate_dis2 = I915_READ(VLV_GUNIT_CLOCK_GATE2);
2229
2230 /*
2231 * Not saving any of:
2232 * DFT, 0x9800-0x9EC0
2233 * SARB, 0xB000-0xB1FC
2234 * GAC, 0x5208-0x524C, 0x14000-0x14C000
2235 * PCI CFG
2236 */
2237}
2238
2239static void vlv_restore_gunit_s0ix_state(struct drm_i915_private *dev_priv)
2240{
2241 struct vlv_s0ix_state *s = &dev_priv->vlv_s0ix_state;
2242 u32 val;
2243 int i;
2244
2245 /* GAM 0x4000-0x4770 */
2246 I915_WRITE(GEN7_WR_WATERMARK, s->wr_watermark);
2247 I915_WRITE(GEN7_GFX_PRIO_CTRL, s->gfx_prio_ctrl);
2248 I915_WRITE(ARB_MODE, s->arb_mode | (0xffff << 16));
2249 I915_WRITE(GEN7_GFX_PEND_TLB0, s->gfx_pend_tlb0);
2250 I915_WRITE(GEN7_GFX_PEND_TLB1, s->gfx_pend_tlb1);
2251
2252 for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002253 I915_WRITE(GEN7_LRA_LIMITS(i), s->lra_limits[i]);
Imre Deakddeea5b2014-05-05 15:19:56 +03002254
2255 I915_WRITE(GEN7_MEDIA_MAX_REQ_COUNT, s->media_max_req_count);
Imre Deakb5f1c972015-04-15 16:52:30 -07002256 I915_WRITE(GEN7_GFX_MAX_REQ_COUNT, s->gfx_max_req_count);
Imre Deakddeea5b2014-05-05 15:19:56 +03002257
2258 I915_WRITE(RENDER_HWS_PGA_GEN7, s->render_hwsp);
2259 I915_WRITE(GAM_ECOCHK, s->ecochk);
2260 I915_WRITE(BSD_HWS_PGA_GEN7, s->bsd_hwsp);
2261 I915_WRITE(BLT_HWS_PGA_GEN7, s->blt_hwsp);
2262
2263 I915_WRITE(GEN7_TLB_RD_ADDR, s->tlb_rd_addr);
2264
2265 /* MBC 0x9024-0x91D0, 0x8500 */
2266 I915_WRITE(VLV_G3DCTL, s->g3dctl);
2267 I915_WRITE(VLV_GSCKGCTL, s->gsckgctl);
2268 I915_WRITE(GEN6_MBCTL, s->mbctl);
2269
2270 /* GCP 0x9400-0x9424, 0x8100-0x810C */
2271 I915_WRITE(GEN6_UCGCTL1, s->ucgctl1);
2272 I915_WRITE(GEN6_UCGCTL3, s->ucgctl3);
2273 I915_WRITE(GEN6_RCGCTL1, s->rcgctl1);
2274 I915_WRITE(GEN6_RCGCTL2, s->rcgctl2);
2275 I915_WRITE(GEN6_RSTCTL, s->rstctl);
2276 I915_WRITE(GEN7_MISCCPCTL, s->misccpctl);
2277
2278 /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
2279 I915_WRITE(GEN6_GFXPAUSE, s->gfxpause);
2280 I915_WRITE(GEN6_RPDEUHWTC, s->rpdeuhwtc);
2281 I915_WRITE(GEN6_RPDEUC, s->rpdeuc);
2282 I915_WRITE(ECOBUS, s->ecobus);
2283 I915_WRITE(VLV_PWRDWNUPCTL, s->pwrdwnupctl);
2284 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,s->rp_down_timeout);
2285 I915_WRITE(GEN6_RPDEUCSW, s->rp_deucsw);
2286 I915_WRITE(GEN6_RCUBMABDTMR, s->rcubmabdtmr);
2287 I915_WRITE(VLV_RCEDATA, s->rcedata);
2288 I915_WRITE(VLV_SPAREG2H, s->spare2gh);
2289
2290 /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
2291 I915_WRITE(GTIMR, s->gt_imr);
2292 I915_WRITE(GTIER, s->gt_ier);
2293 I915_WRITE(GEN6_PMIMR, s->pm_imr);
2294 I915_WRITE(GEN6_PMIER, s->pm_ier);
2295
2296 for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002297 I915_WRITE(GEN7_GT_SCRATCH(i), s->gt_scratch[i]);
Imre Deakddeea5b2014-05-05 15:19:56 +03002298
2299 /* GT SA CZ domain, 0x100000-0x138124 */
2300 I915_WRITE(TILECTL, s->tilectl);
2301 I915_WRITE(GTFIFOCTL, s->gt_fifoctl);
2302 /*
2303 * Preserve the GT allow wake and GFX force clock bit, they are not
2304 * be restored, as they are used to control the s0ix suspend/resume
2305 * sequence by the caller.
2306 */
2307 val = I915_READ(VLV_GTLC_WAKE_CTRL);
2308 val &= VLV_GTLC_ALLOWWAKEREQ;
2309 val |= s->gtlc_wake_ctrl & ~VLV_GTLC_ALLOWWAKEREQ;
2310 I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
2311
2312 val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2313 val &= VLV_GFX_CLK_FORCE_ON_BIT;
2314 val |= s->gtlc_survive & ~VLV_GFX_CLK_FORCE_ON_BIT;
2315 I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
2316
2317 I915_WRITE(VLV_PMWGICZ, s->pmwgicz);
2318
2319 /* Gunit-Display CZ domain, 0x182028-0x1821CF */
2320 I915_WRITE(VLV_GU_CTL0, s->gu_ctl0);
2321 I915_WRITE(VLV_GU_CTL1, s->gu_ctl1);
Jesse Barnes9c252102015-04-01 14:22:57 -07002322 I915_WRITE(VLV_PCBR, s->pcbr);
Imre Deakddeea5b2014-05-05 15:19:56 +03002323 I915_WRITE(VLV_GUNIT_CLOCK_GATE2, s->clock_gate_dis2);
2324}
2325
Chris Wilson3dd14c02017-04-21 14:58:15 +01002326static int vlv_wait_for_pw_status(struct drm_i915_private *dev_priv,
2327 u32 mask, u32 val)
2328{
2329 /* The HW does not like us polling for PW_STATUS frequently, so
2330 * use the sleeping loop rather than risk the busy spin within
2331 * intel_wait_for_register().
2332 *
2333 * Transitioning between RC6 states should be at most 2ms (see
2334 * valleyview_enable_rps) so use a 3ms timeout.
2335 */
2336 return wait_for((I915_READ_NOTRACE(VLV_GTLC_PW_STATUS) & mask) == val,
2337 3);
2338}
2339
Imre Deak650ad972014-04-18 16:35:02 +03002340int vlv_force_gfx_clock(struct drm_i915_private *dev_priv, bool force_on)
2341{
2342 u32 val;
2343 int err;
2344
Imre Deak650ad972014-04-18 16:35:02 +03002345 val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2346 val &= ~VLV_GFX_CLK_FORCE_ON_BIT;
2347 if (force_on)
2348 val |= VLV_GFX_CLK_FORCE_ON_BIT;
2349 I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
2350
2351 if (!force_on)
2352 return 0;
2353
Chris Wilsonc6ddc5f2016-06-30 15:32:46 +01002354 err = intel_wait_for_register(dev_priv,
2355 VLV_GTLC_SURVIVABILITY_REG,
2356 VLV_GFX_CLK_STATUS_BIT,
2357 VLV_GFX_CLK_STATUS_BIT,
2358 20);
Imre Deak650ad972014-04-18 16:35:02 +03002359 if (err)
2360 DRM_ERROR("timeout waiting for GFX clock force-on (%08x)\n",
2361 I915_READ(VLV_GTLC_SURVIVABILITY_REG));
2362
2363 return err;
Imre Deak650ad972014-04-18 16:35:02 +03002364}
2365
Imre Deakddeea5b2014-05-05 15:19:56 +03002366static int vlv_allow_gt_wake(struct drm_i915_private *dev_priv, bool allow)
2367{
Chris Wilson3dd14c02017-04-21 14:58:15 +01002368 u32 mask;
Imre Deakddeea5b2014-05-05 15:19:56 +03002369 u32 val;
Chris Wilson3dd14c02017-04-21 14:58:15 +01002370 int err;
Imre Deakddeea5b2014-05-05 15:19:56 +03002371
2372 val = I915_READ(VLV_GTLC_WAKE_CTRL);
2373 val &= ~VLV_GTLC_ALLOWWAKEREQ;
2374 if (allow)
2375 val |= VLV_GTLC_ALLOWWAKEREQ;
2376 I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
2377 POSTING_READ(VLV_GTLC_WAKE_CTRL);
2378
Chris Wilson3dd14c02017-04-21 14:58:15 +01002379 mask = VLV_GTLC_ALLOWWAKEACK;
2380 val = allow ? mask : 0;
2381
2382 err = vlv_wait_for_pw_status(dev_priv, mask, val);
Imre Deakddeea5b2014-05-05 15:19:56 +03002383 if (err)
2384 DRM_ERROR("timeout disabling GT waking\n");
Chris Wilsonb2736692016-06-30 15:32:47 +01002385
Imre Deakddeea5b2014-05-05 15:19:56 +03002386 return err;
Imre Deakddeea5b2014-05-05 15:19:56 +03002387}
2388
Chris Wilson3dd14c02017-04-21 14:58:15 +01002389static void vlv_wait_for_gt_wells(struct drm_i915_private *dev_priv,
2390 bool wait_for_on)
Imre Deakddeea5b2014-05-05 15:19:56 +03002391{
2392 u32 mask;
2393 u32 val;
Imre Deakddeea5b2014-05-05 15:19:56 +03002394
2395 mask = VLV_GTLC_PW_MEDIA_STATUS_MASK | VLV_GTLC_PW_RENDER_STATUS_MASK;
2396 val = wait_for_on ? mask : 0;
Imre Deakddeea5b2014-05-05 15:19:56 +03002397
2398 /*
2399 * RC6 transitioning can be delayed up to 2 msec (see
2400 * valleyview_enable_rps), use 3 msec for safety.
2401 */
Chris Wilson3dd14c02017-04-21 14:58:15 +01002402 if (vlv_wait_for_pw_status(dev_priv, mask, val))
Imre Deakddeea5b2014-05-05 15:19:56 +03002403 DRM_ERROR("timeout waiting for GT wells to go %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02002404 onoff(wait_for_on));
Imre Deakddeea5b2014-05-05 15:19:56 +03002405}
2406
2407static void vlv_check_no_gt_access(struct drm_i915_private *dev_priv)
2408{
2409 if (!(I915_READ(VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEERR))
2410 return;
2411
Daniel Vetter6fa283b2016-01-19 21:00:56 +01002412 DRM_DEBUG_DRIVER("GT register access while GT waking disabled\n");
Imre Deakddeea5b2014-05-05 15:19:56 +03002413 I915_WRITE(VLV_GTLC_PW_STATUS, VLV_GTLC_ALLOWWAKEERR);
2414}
2415
Sagar Kambleebc32822014-08-13 23:07:05 +05302416static int vlv_suspend_complete(struct drm_i915_private *dev_priv)
Imre Deakddeea5b2014-05-05 15:19:56 +03002417{
2418 u32 mask;
2419 int err;
2420
2421 /*
2422 * Bspec defines the following GT well on flags as debug only, so
2423 * don't treat them as hard failures.
2424 */
Chris Wilson3dd14c02017-04-21 14:58:15 +01002425 vlv_wait_for_gt_wells(dev_priv, false);
Imre Deakddeea5b2014-05-05 15:19:56 +03002426
2427 mask = VLV_GTLC_RENDER_CTX_EXISTS | VLV_GTLC_MEDIA_CTX_EXISTS;
2428 WARN_ON((I915_READ(VLV_GTLC_WAKE_CTRL) & mask) != mask);
2429
2430 vlv_check_no_gt_access(dev_priv);
2431
2432 err = vlv_force_gfx_clock(dev_priv, true);
2433 if (err)
2434 goto err1;
2435
2436 err = vlv_allow_gt_wake(dev_priv, false);
2437 if (err)
2438 goto err2;
Deepak S98711162014-12-12 14:18:16 +05302439
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002440 if (!IS_CHERRYVIEW(dev_priv))
Deepak S98711162014-12-12 14:18:16 +05302441 vlv_save_gunit_s0ix_state(dev_priv);
Imre Deakddeea5b2014-05-05 15:19:56 +03002442
2443 err = vlv_force_gfx_clock(dev_priv, false);
2444 if (err)
2445 goto err2;
2446
2447 return 0;
2448
2449err2:
2450 /* For safety always re-enable waking and disable gfx clock forcing */
2451 vlv_allow_gt_wake(dev_priv, true);
2452err1:
2453 vlv_force_gfx_clock(dev_priv, false);
2454
2455 return err;
2456}
2457
Sagar Kamble016970b2014-08-13 23:07:06 +05302458static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
2459 bool rpm_resume)
Imre Deakddeea5b2014-05-05 15:19:56 +03002460{
Imre Deakddeea5b2014-05-05 15:19:56 +03002461 int err;
2462 int ret;
2463
2464 /*
2465 * If any of the steps fail just try to continue, that's the best we
2466 * can do at this point. Return the first error code (which will also
2467 * leave RPM permanently disabled).
2468 */
2469 ret = vlv_force_gfx_clock(dev_priv, true);
2470
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002471 if (!IS_CHERRYVIEW(dev_priv))
Deepak S98711162014-12-12 14:18:16 +05302472 vlv_restore_gunit_s0ix_state(dev_priv);
Imre Deakddeea5b2014-05-05 15:19:56 +03002473
2474 err = vlv_allow_gt_wake(dev_priv, true);
2475 if (!ret)
2476 ret = err;
2477
2478 err = vlv_force_gfx_clock(dev_priv, false);
2479 if (!ret)
2480 ret = err;
2481
2482 vlv_check_no_gt_access(dev_priv);
2483
Chris Wilson7c108fd2016-10-24 13:42:18 +01002484 if (rpm_resume)
Ville Syrjälä46f16e62016-10-31 22:37:22 +02002485 intel_init_clock_gating(dev_priv);
Imre Deakddeea5b2014-05-05 15:19:56 +03002486
2487 return ret;
2488}
2489
David Weinehallc49d13e2016-08-22 13:32:42 +03002490static int intel_runtime_suspend(struct device *kdev)
Paulo Zanoni8a187452013-12-06 20:32:13 -02002491{
David Weinehallc49d13e2016-08-22 13:32:42 +03002492 struct pci_dev *pdev = to_pci_dev(kdev);
Paulo Zanoni8a187452013-12-06 20:32:13 -02002493 struct drm_device *dev = pci_get_drvdata(pdev);
Chris Wilsonfac5e232016-07-04 11:34:36 +01002494 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002495 int ret;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002496
Chris Wilsondc979972016-05-10 14:10:04 +01002497 if (WARN_ON_ONCE(!(dev_priv->rps.enabled && intel_enable_rc6())))
Imre Deakc6df39b2014-04-14 20:24:29 +03002498 return -ENODEV;
2499
Tvrtko Ursulin6772ffe2016-10-13 11:02:55 +01002500 if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev_priv)))
Imre Deak604effb2014-08-26 13:26:56 +03002501 return -ENODEV;
2502
Paulo Zanoni8a187452013-12-06 20:32:13 -02002503 DRM_DEBUG_KMS("Suspending device\n");
2504
Imre Deak1f814da2015-12-16 02:52:19 +02002505 disable_rpm_wakeref_asserts(dev_priv);
2506
Imre Deakd6102972014-05-07 19:57:49 +03002507 /*
2508 * We are safe here against re-faults, since the fault handler takes
2509 * an RPM reference.
2510 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002511 i915_gem_runtime_suspend(dev_priv);
Imre Deakd6102972014-05-07 19:57:49 +03002512
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00002513 intel_guc_suspend(dev_priv);
Alex Daia1c41992015-09-30 09:46:37 -07002514
Imre Deak2eb52522014-11-19 15:30:05 +02002515 intel_runtime_pm_disable_interrupts(dev_priv);
Imre Deakb5478bc2014-04-14 20:24:37 +03002516
Imre Deak507e1262016-04-20 20:27:54 +03002517 ret = 0;
Rodrigo Vivib9fd7992016-12-16 17:42:25 +02002518 if (IS_GEN9_LP(dev_priv)) {
Imre Deak507e1262016-04-20 20:27:54 +03002519 bxt_display_core_uninit(dev_priv);
2520 bxt_enable_dc9(dev_priv);
2521 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
2522 hsw_enable_pc8(dev_priv);
2523 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
2524 ret = vlv_suspend_complete(dev_priv);
2525 }
2526
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002527 if (ret) {
2528 DRM_ERROR("Runtime suspend failed, disabling it (%d)\n", ret);
Daniel Vetterb9632912014-09-30 10:56:44 +02002529 intel_runtime_pm_enable_interrupts(dev_priv);
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002530
Imre Deak1f814da2015-12-16 02:52:19 +02002531 enable_rpm_wakeref_asserts(dev_priv);
2532
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002533 return ret;
2534 }
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03002535
Hans de Goede68f60942017-02-10 11:28:01 +01002536 intel_uncore_suspend(dev_priv);
Imre Deak1f814da2015-12-16 02:52:19 +02002537
2538 enable_rpm_wakeref_asserts(dev_priv);
2539 WARN_ON_ONCE(atomic_read(&dev_priv->pm.wakeref_count));
Mika Kuoppala55ec45c2015-12-15 16:25:08 +02002540
Mika Kuoppalabc3b9342016-01-08 15:51:20 +02002541 if (intel_uncore_arm_unclaimed_mmio_detection(dev_priv))
Mika Kuoppala55ec45c2015-12-15 16:25:08 +02002542 DRM_ERROR("Unclaimed access detected prior to suspending\n");
2543
Paulo Zanoni8a187452013-12-06 20:32:13 -02002544 dev_priv->pm.suspended = true;
Kristen Carlson Accardi1fb23622014-01-14 15:36:15 -08002545
2546 /*
Paulo Zanonic8a0bd42014-08-21 17:09:38 -03002547 * FIXME: We really should find a document that references the arguments
2548 * used below!
Kristen Carlson Accardi1fb23622014-01-14 15:36:15 -08002549 */
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002550 if (IS_BROADWELL(dev_priv)) {
Paulo Zanonid37ae192015-07-30 18:20:29 -03002551 /*
2552 * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop
2553 * being detected, and the call we do at intel_runtime_resume()
2554 * won't be able to restore them. Since PCI_D3hot matches the
2555 * actual specification and appears to be working, use it.
2556 */
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002557 intel_opregion_notify_adapter(dev_priv, PCI_D3hot);
Paulo Zanonid37ae192015-07-30 18:20:29 -03002558 } else {
Paulo Zanonic8a0bd42014-08-21 17:09:38 -03002559 /*
2560 * current versions of firmware which depend on this opregion
2561 * notification have repurposed the D1 definition to mean
2562 * "runtime suspended" vs. what you would normally expect (D3)
2563 * to distinguish it from notifications that might be sent via
2564 * the suspend path.
2565 */
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002566 intel_opregion_notify_adapter(dev_priv, PCI_D1);
Paulo Zanonic8a0bd42014-08-21 17:09:38 -03002567 }
Paulo Zanoni8a187452013-12-06 20:32:13 -02002568
Mika Kuoppala59bad942015-01-16 11:34:40 +02002569 assert_forcewakes_inactive(dev_priv);
Chris Wilsondc9fb092015-01-16 11:34:34 +02002570
Ander Conselvan de Oliveira21d6e0b2017-01-20 16:28:43 +02002571 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
Lyude19625e82016-06-21 17:03:44 -04002572 intel_hpd_poll_init(dev_priv);
2573
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03002574 DRM_DEBUG_KMS("Device suspended\n");
Paulo Zanoni8a187452013-12-06 20:32:13 -02002575 return 0;
2576}
2577
David Weinehallc49d13e2016-08-22 13:32:42 +03002578static int intel_runtime_resume(struct device *kdev)
Paulo Zanoni8a187452013-12-06 20:32:13 -02002579{
David Weinehallc49d13e2016-08-22 13:32:42 +03002580 struct pci_dev *pdev = to_pci_dev(kdev);
Paulo Zanoni8a187452013-12-06 20:32:13 -02002581 struct drm_device *dev = pci_get_drvdata(pdev);
Chris Wilsonfac5e232016-07-04 11:34:36 +01002582 struct drm_i915_private *dev_priv = to_i915(dev);
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002583 int ret = 0;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002584
Tvrtko Ursulin6772ffe2016-10-13 11:02:55 +01002585 if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev_priv)))
Imre Deak604effb2014-08-26 13:26:56 +03002586 return -ENODEV;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002587
2588 DRM_DEBUG_KMS("Resuming device\n");
2589
Imre Deak1f814da2015-12-16 02:52:19 +02002590 WARN_ON_ONCE(atomic_read(&dev_priv->pm.wakeref_count));
2591 disable_rpm_wakeref_asserts(dev_priv);
2592
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002593 intel_opregion_notify_adapter(dev_priv, PCI_D0);
Paulo Zanoni8a187452013-12-06 20:32:13 -02002594 dev_priv->pm.suspended = false;
Mika Kuoppala55ec45c2015-12-15 16:25:08 +02002595 if (intel_uncore_unclaimed_mmio(dev_priv))
2596 DRM_DEBUG_DRIVER("Unclaimed access during suspend, bios?\n");
Paulo Zanoni8a187452013-12-06 20:32:13 -02002597
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00002598 intel_guc_resume(dev_priv);
Alex Daia1c41992015-09-30 09:46:37 -07002599
Rodrigo Vivib9fd7992016-12-16 17:42:25 +02002600 if (IS_GEN9_LP(dev_priv)) {
Imre Deak507e1262016-04-20 20:27:54 +03002601 bxt_disable_dc9(dev_priv);
2602 bxt_display_core_init(dev_priv, true);
Imre Deakf62c79b2016-04-20 20:27:57 +03002603 if (dev_priv->csr.dmc_payload &&
2604 (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC5))
2605 gen9_enable_dc5(dev_priv);
Imre Deak507e1262016-04-20 20:27:54 +03002606 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002607 hsw_disable_pc8(dev_priv);
Imre Deak507e1262016-04-20 20:27:54 +03002608 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002609 ret = vlv_resume_prepare(dev_priv, true);
Imre Deak507e1262016-04-20 20:27:54 +03002610 }
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002611
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002612 /*
2613 * No point of rolling back things in case of an error, as the best
2614 * we can do is to hope that things will still work (and disable RPM).
2615 */
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002616 i915_gem_init_swizzling(dev_priv);
Chris Wilson83bf6d52017-02-03 12:57:17 +00002617 i915_gem_restore_fences(dev_priv);
Imre Deak92b806d2014-04-14 20:24:39 +03002618
Daniel Vetterb9632912014-09-30 10:56:44 +02002619 intel_runtime_pm_enable_interrupts(dev_priv);
Ville Syrjälä08d8a232015-08-27 23:56:08 +03002620
2621 /*
2622 * On VLV/CHV display interrupts are part of the display
2623 * power well, so hpd is reinitialized from there. For
2624 * everyone else do it here.
2625 */
Wayne Boyer666a4532015-12-09 12:29:35 -08002626 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
Ville Syrjälä08d8a232015-08-27 23:56:08 +03002627 intel_hpd_init(dev_priv);
2628
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05302629 intel_enable_ipc(dev_priv);
2630
Imre Deak1f814da2015-12-16 02:52:19 +02002631 enable_rpm_wakeref_asserts(dev_priv);
2632
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002633 if (ret)
2634 DRM_ERROR("Runtime resume failed, disabling it (%d)\n", ret);
2635 else
2636 DRM_DEBUG_KMS("Device resumed\n");
2637
2638 return ret;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002639}
2640
Chris Wilson42f55512016-06-24 14:00:26 +01002641const struct dev_pm_ops i915_pm_ops = {
Imre Deak5545dbb2014-10-23 19:23:28 +03002642 /*
2643 * S0ix (via system suspend) and S3 event handlers [PMSG_SUSPEND,
2644 * PMSG_RESUME]
2645 */
Akshay Joshi0206e352011-08-16 15:34:10 -04002646 .suspend = i915_pm_suspend,
Imre Deak76c4b252014-04-01 19:55:22 +03002647 .suspend_late = i915_pm_suspend_late,
2648 .resume_early = i915_pm_resume_early,
Akshay Joshi0206e352011-08-16 15:34:10 -04002649 .resume = i915_pm_resume,
Imre Deak5545dbb2014-10-23 19:23:28 +03002650
2651 /*
2652 * S4 event handlers
2653 * @freeze, @freeze_late : called (1) before creating the
2654 * hibernation image [PMSG_FREEZE] and
2655 * (2) after rebooting, before restoring
2656 * the image [PMSG_QUIESCE]
2657 * @thaw, @thaw_early : called (1) after creating the hibernation
2658 * image, before writing it [PMSG_THAW]
2659 * and (2) after failing to create or
2660 * restore the image [PMSG_RECOVER]
2661 * @poweroff, @poweroff_late: called after writing the hibernation
2662 * image, before rebooting [PMSG_HIBERNATE]
2663 * @restore, @restore_early : called after rebooting and restoring the
2664 * hibernation image [PMSG_RESTORE]
2665 */
Chris Wilson1f19ac22016-05-14 07:26:32 +01002666 .freeze = i915_pm_freeze,
2667 .freeze_late = i915_pm_freeze_late,
2668 .thaw_early = i915_pm_thaw_early,
2669 .thaw = i915_pm_thaw,
Imre Deak36d61e62014-10-23 19:23:24 +03002670 .poweroff = i915_pm_suspend,
Imre Deakab3be732015-03-02 13:04:41 +02002671 .poweroff_late = i915_pm_poweroff_late,
Chris Wilson1f19ac22016-05-14 07:26:32 +01002672 .restore_early = i915_pm_restore_early,
2673 .restore = i915_pm_restore,
Imre Deak5545dbb2014-10-23 19:23:28 +03002674
2675 /* S0ix (via runtime suspend) event handlers */
Paulo Zanoni97bea202014-03-07 20:12:33 -03002676 .runtime_suspend = intel_runtime_suspend,
2677 .runtime_resume = intel_runtime_resume,
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002678};
2679
Laurent Pinchart78b68552012-05-17 13:27:22 +02002680static const struct vm_operations_struct i915_gem_vm_ops = {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002681 .fault = i915_gem_fault,
Jesse Barnesab00b3e2009-02-11 14:01:46 -08002682 .open = drm_gem_vm_open,
2683 .close = drm_gem_vm_close,
Jesse Barnesde151cf2008-11-12 10:03:55 -08002684};
2685
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002686static const struct file_operations i915_driver_fops = {
2687 .owner = THIS_MODULE,
2688 .open = drm_open,
2689 .release = drm_release,
2690 .unlocked_ioctl = drm_ioctl,
2691 .mmap = drm_gem_mmap,
2692 .poll = drm_poll,
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002693 .read = drm_read,
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002694 .compat_ioctl = i915_compat_ioctl,
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002695 .llseek = noop_llseek,
2696};
2697
Chris Wilson0673ad42016-06-24 14:00:22 +01002698static int
2699i915_gem_reject_pin_ioctl(struct drm_device *dev, void *data,
2700 struct drm_file *file)
2701{
2702 return -ENODEV;
2703}
2704
2705static const struct drm_ioctl_desc i915_ioctls[] = {
2706 DRM_IOCTL_DEF_DRV(I915_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2707 DRM_IOCTL_DEF_DRV(I915_FLUSH, drm_noop, DRM_AUTH),
2708 DRM_IOCTL_DEF_DRV(I915_FLIP, drm_noop, DRM_AUTH),
2709 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, drm_noop, DRM_AUTH),
2710 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, drm_noop, DRM_AUTH),
2711 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, drm_noop, DRM_AUTH),
2712 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
2713 DRM_IOCTL_DEF_DRV(I915_SETPARAM, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2714 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
2715 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
2716 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2717 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, drm_noop, DRM_AUTH),
2718 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2719 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2720 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, drm_noop, DRM_AUTH),
2721 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, drm_noop, DRM_AUTH),
2722 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2723 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2724 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
Chris Wilsonfec04452017-01-27 09:40:08 +00002725 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2_WR, i915_gem_execbuffer2, DRM_AUTH|DRM_RENDER_ALLOW),
Chris Wilson0673ad42016-06-24 14:00:22 +01002726 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
2727 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
2728 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
2729 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_RENDER_ALLOW),
2730 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_RENDER_ALLOW),
2731 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
2732 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2733 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2734 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_RENDER_ALLOW),
2735 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW),
2736 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW),
2737 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW),
2738 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_RENDER_ALLOW),
2739 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW),
2740 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW),
Chris Wilson111dbca2017-01-10 12:10:44 +00002741 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling_ioctl, DRM_RENDER_ALLOW),
2742 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling_ioctl, DRM_RENDER_ALLOW),
Chris Wilson0673ad42016-06-24 14:00:22 +01002743 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_RENDER_ALLOW),
2744 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, 0),
2745 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_RENDER_ALLOW),
2746 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW),
2747 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW),
2748 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW),
2749 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW),
2750 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
2751 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_RENDER_ALLOW),
2752 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_RENDER_ALLOW),
2753 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_RENDER_ALLOW),
2754 DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_gem_context_reset_stats_ioctl, DRM_RENDER_ALLOW),
2755 DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
2756 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
2757 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
Robert Braggeec688e2016-11-07 19:49:47 +00002758 DRM_IOCTL_DEF_DRV(I915_PERF_OPEN, i915_perf_open_ioctl, DRM_RENDER_ALLOW),
Lionel Landwerlinf89823c2017-08-03 18:05:50 +01002759 DRM_IOCTL_DEF_DRV(I915_PERF_ADD_CONFIG, i915_perf_add_config_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2760 DRM_IOCTL_DEF_DRV(I915_PERF_REMOVE_CONFIG, i915_perf_remove_config_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Chris Wilson0673ad42016-06-24 14:00:22 +01002761};
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763static struct drm_driver driver = {
Michael Witten0c547812011-08-25 17:55:54 +00002764 /* Don't use MTRRs here; the Xserver or userspace app should
2765 * deal with them for Intel hardware.
Dave Airlie792d2b92005-11-11 23:30:27 +11002766 */
Eric Anholt673a3942008-07-30 12:06:12 -07002767 .driver_features =
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002768 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM | DRIVER_PRIME |
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002769 DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_SYNCOBJ,
Chris Wilsoncad36882017-02-10 16:35:21 +00002770 .release = i915_driver_release,
Eric Anholt673a3942008-07-30 12:06:12 -07002771 .open = i915_driver_open,
Dave Airlie22eae942005-11-10 22:16:34 +11002772 .lastclose = i915_driver_lastclose,
Eric Anholt673a3942008-07-30 12:06:12 -07002773 .postclose = i915_driver_postclose,
Rafael J. Wysockid8e29202010-01-09 00:45:33 +01002774
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002775 .gem_close_object = i915_gem_close_object,
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002776 .gem_free_object_unlocked = i915_gem_free_object,
Jesse Barnesde151cf2008-11-12 10:03:55 -08002777 .gem_vm_ops = &i915_gem_vm_ops,
Daniel Vetter1286ff72012-05-10 15:25:09 +02002778
2779 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
2780 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
2781 .gem_prime_export = i915_gem_prime_export,
2782 .gem_prime_import = i915_gem_prime_import,
2783
Dave Airlieff72145b2011-02-07 12:16:14 +10002784 .dumb_create = i915_gem_dumb_create,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002785 .dumb_map_offset = i915_gem_mmap_gtt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 .ioctls = i915_ioctls,
Chris Wilson0673ad42016-06-24 14:00:22 +01002787 .num_ioctls = ARRAY_SIZE(i915_ioctls),
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002788 .fops = &i915_driver_fops,
Dave Airlie22eae942005-11-10 22:16:34 +11002789 .name = DRIVER_NAME,
2790 .desc = DRIVER_DESC,
2791 .date = DRIVER_DATE,
2792 .major = DRIVER_MAJOR,
2793 .minor = DRIVER_MINOR,
2794 .patchlevel = DRIVER_PATCHLEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795};
Chris Wilson66d9cb52017-02-13 17:15:17 +00002796
2797#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2798#include "selftests/mock_drm.c"
2799#endif