blob: 8a2674013aef24da8c418458b2d8a0523762df4a [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>
David Howells760285e2012-10-02 18:01:07 +010046#include <drm/i915_drm.h>
Chris Wilson0673ad42016-06-24 14:00:22 +010047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "i915_drv.h"
Chris Wilson990bbda2012-07-02 11:51:02 -030049#include "i915_trace.h"
Chris Wilson0673ad42016-06-24 14:00:22 +010050#include "i915_vgpu.h"
Kenneth Graunkef49f0582010-09-11 01:19:14 -070051#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Kristian Høgsberg112b7152009-01-04 16:55:33 -050053static struct drm_driver driver;
54
Chris Wilson0673ad42016-06-24 14:00:22 +010055static unsigned int i915_load_fail_count;
56
57bool __i915_inject_load_failure(const char *func, int line)
58{
59 if (i915_load_fail_count >= i915.inject_load_failure)
60 return false;
61
62 if (++i915_load_fail_count == i915.inject_load_failure) {
63 DRM_INFO("Injecting failure at checkpoint %u [%s:%d]\n",
64 i915.inject_load_failure, func, line);
65 return true;
66 }
67
68 return false;
69}
70
71#define FDO_BUG_URL "https://bugs.freedesktop.org/enter_bug.cgi?product=DRI"
72#define FDO_BUG_MSG "Please file a bug at " FDO_BUG_URL " against DRM/Intel " \
73 "providing the dmesg log by booting with drm.debug=0xf"
74
75void
76__i915_printk(struct drm_i915_private *dev_priv, const char *level,
77 const char *fmt, ...)
78{
79 static bool shown_bug_once;
80 struct device *dev = dev_priv->dev->dev;
81 bool is_error = level[1] <= KERN_ERR[1];
82 bool is_debug = level[1] == KERN_DEBUG[1];
83 struct va_format vaf;
84 va_list args;
85
86 if (is_debug && !(drm_debug & DRM_UT_DRIVER))
87 return;
88
89 va_start(args, fmt);
90
91 vaf.fmt = fmt;
92 vaf.va = &args;
93
94 dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV",
95 __builtin_return_address(0), &vaf);
96
97 if (is_error && !shown_bug_once) {
98 dev_notice(dev, "%s", FDO_BUG_MSG);
99 shown_bug_once = true;
100 }
101
102 va_end(args);
103}
104
105static bool i915_error_injected(struct drm_i915_private *dev_priv)
106{
107 return i915.inject_load_failure &&
108 i915_load_fail_count == i915.inject_load_failure;
109}
110
111#define i915_load_error(dev_priv, fmt, ...) \
112 __i915_printk(dev_priv, \
113 i915_error_injected(dev_priv) ? KERN_DEBUG : KERN_ERR, \
114 fmt, ##__VA_ARGS__)
115
116
Robert Beckett30c964a2015-08-28 13:10:22 +0100117static enum intel_pch intel_virt_detect_pch(struct drm_device *dev)
118{
119 enum intel_pch ret = PCH_NOP;
120
121 /*
122 * In a virtualized passthrough environment we can be in a
123 * setup where the ISA bridge is not able to be passed through.
124 * In this case, a south bridge can be emulated and we have to
125 * make an educated guess as to which PCH is really there.
126 */
127
128 if (IS_GEN5(dev)) {
129 ret = PCH_IBX;
130 DRM_DEBUG_KMS("Assuming Ibex Peak PCH\n");
131 } else if (IS_GEN6(dev) || IS_IVYBRIDGE(dev)) {
132 ret = PCH_CPT;
133 DRM_DEBUG_KMS("Assuming CouarPoint PCH\n");
134 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
135 ret = PCH_LPT;
136 DRM_DEBUG_KMS("Assuming LynxPoint PCH\n");
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700137 } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Robert Beckett30c964a2015-08-28 13:10:22 +0100138 ret = PCH_SPT;
139 DRM_DEBUG_KMS("Assuming SunrisePoint PCH\n");
140 }
141
142 return ret;
143}
144
Chris Wilson0673ad42016-06-24 14:00:22 +0100145static void intel_detect_pch(struct drm_device *dev)
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800146{
147 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deakbcdb72a2014-02-14 20:23:54 +0200148 struct pci_dev *pch = NULL;
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800149
Ben Widawskyce1bb322013-04-05 13:12:44 -0700150 /* In all current cases, num_pipes is equivalent to the PCH_NOP setting
151 * (which really amounts to a PCH but no South Display).
152 */
153 if (INTEL_INFO(dev)->num_pipes == 0) {
154 dev_priv->pch_type = PCH_NOP;
Ben Widawskyce1bb322013-04-05 13:12:44 -0700155 return;
156 }
157
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800158 /*
159 * The reason to probe ISA bridge instead of Dev31:Fun0 is to
160 * make graphics device passthrough work easy for VMM, that only
161 * need to expose ISA bridge to let driver know the real hardware
162 * underneath. This is a requirement from virtualization team.
Rui Guo6a9c4b32013-06-19 21:10:23 +0800163 *
164 * In some virtualized environments (e.g. XEN), there is irrelevant
165 * ISA bridge in the system. To work reliably, we should scan trhough
166 * all the ISA bridge devices and check for the first match, instead
167 * of only checking the first one.
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800168 */
Imre Deakbcdb72a2014-02-14 20:23:54 +0200169 while ((pch = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, pch))) {
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800170 if (pch->vendor == PCI_VENDOR_ID_INTEL) {
Imre Deakbcdb72a2014-02-14 20:23:54 +0200171 unsigned short id = pch->device & INTEL_PCH_DEVICE_ID_MASK;
Paulo Zanoni17a303e2012-11-20 15:12:07 -0200172 dev_priv->pch_id = id;
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800173
Jesse Barnes90711d52011-04-28 14:48:02 -0700174 if (id == INTEL_PCH_IBX_DEVICE_ID_TYPE) {
175 dev_priv->pch_type = PCH_IBX;
176 DRM_DEBUG_KMS("Found Ibex Peak PCH\n");
Daniel Vetter7fcb83c2012-10-31 22:52:27 +0100177 WARN_ON(!IS_GEN5(dev));
Jesse Barnes90711d52011-04-28 14:48:02 -0700178 } else if (id == INTEL_PCH_CPT_DEVICE_ID_TYPE) {
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800179 dev_priv->pch_type = PCH_CPT;
180 DRM_DEBUG_KMS("Found CougarPoint PCH\n");
Daniel Vetter7fcb83c2012-10-31 22:52:27 +0100181 WARN_ON(!(IS_GEN6(dev) || IS_IVYBRIDGE(dev)));
Jesse Barnesc7925132011-04-07 12:33:56 -0700182 } else if (id == INTEL_PCH_PPT_DEVICE_ID_TYPE) {
183 /* PantherPoint is CPT compatible */
184 dev_priv->pch_type = PCH_CPT;
Jani Nikula492ab662013-10-01 12:12:33 +0300185 DRM_DEBUG_KMS("Found PantherPoint PCH\n");
Daniel Vetter7fcb83c2012-10-31 22:52:27 +0100186 WARN_ON(!(IS_GEN6(dev) || IS_IVYBRIDGE(dev)));
Eugeni Dodonoveb877eb2012-03-29 12:32:20 -0300187 } else if (id == INTEL_PCH_LPT_DEVICE_ID_TYPE) {
188 dev_priv->pch_type = PCH_LPT;
189 DRM_DEBUG_KMS("Found LynxPoint PCH\n");
Rodrigo Vivia35cc9d02015-01-21 10:33:53 -0800190 WARN_ON(!IS_HASWELL(dev) && !IS_BROADWELL(dev));
191 WARN_ON(IS_HSW_ULT(dev) || IS_BDW_ULT(dev));
Ben Widawskye76e0632013-11-07 21:40:41 -0800192 } else if (id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
193 dev_priv->pch_type = PCH_LPT;
194 DRM_DEBUG_KMS("Found LynxPoint LP PCH\n");
Rodrigo Vivia35cc9d02015-01-21 10:33:53 -0800195 WARN_ON(!IS_HASWELL(dev) && !IS_BROADWELL(dev));
196 WARN_ON(!IS_HSW_ULT(dev) && !IS_BDW_ULT(dev));
Satheeshakrishna Me7e7ea22014-04-09 11:08:57 +0530197 } else if (id == INTEL_PCH_SPT_DEVICE_ID_TYPE) {
198 dev_priv->pch_type = PCH_SPT;
199 DRM_DEBUG_KMS("Found SunrisePoint PCH\n");
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700200 WARN_ON(!IS_SKYLAKE(dev) &&
201 !IS_KABYLAKE(dev));
Satheeshakrishna Me7e7ea22014-04-09 11:08:57 +0530202 } else if (id == INTEL_PCH_SPT_LP_DEVICE_ID_TYPE) {
203 dev_priv->pch_type = PCH_SPT;
204 DRM_DEBUG_KMS("Found SunrisePoint LP PCH\n");
Rodrigo Vivief11bdb2015-10-28 04:16:45 -0700205 WARN_ON(!IS_SKYLAKE(dev) &&
206 !IS_KABYLAKE(dev));
Gerd Hoffmann39bfcd522015-11-26 12:03:51 +0100207 } else if ((id == INTEL_PCH_P2X_DEVICE_ID_TYPE) ||
Jesse Barnes1844a662016-03-16 13:31:30 -0700208 (id == INTEL_PCH_P3X_DEVICE_ID_TYPE) ||
Gerd Hoffmannf2e30512016-01-25 12:02:28 +0100209 ((id == INTEL_PCH_QEMU_DEVICE_ID_TYPE) &&
Gerd Hoffmann94bb4892016-06-13 14:38:56 +0200210 pch->subsystem_vendor ==
211 PCI_SUBVENDOR_ID_REDHAT_QUMRANET &&
212 pch->subsystem_device ==
213 PCI_SUBDEVICE_ID_QEMU)) {
Robert Beckett30c964a2015-08-28 13:10:22 +0100214 dev_priv->pch_type = intel_virt_detect_pch(dev);
Imre Deakbcdb72a2014-02-14 20:23:54 +0200215 } else
216 continue;
217
Rui Guo6a9c4b32013-06-19 21:10:23 +0800218 break;
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800219 }
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800220 }
Rui Guo6a9c4b32013-06-19 21:10:23 +0800221 if (!pch)
Imre Deakbcdb72a2014-02-14 20:23:54 +0200222 DRM_DEBUG_KMS("No PCH found.\n");
223
224 pci_dev_put(pch);
Zhenyu Wang3bad0782010-04-07 16:15:53 +0800225}
226
Chris Wilsonc0336662016-05-06 15:40:21 +0100227bool i915_semaphore_is_enabled(struct drm_i915_private *dev_priv)
Ben Widawsky2911a352012-04-05 14:47:36 -0700228{
Chris Wilsonc0336662016-05-06 15:40:21 +0100229 if (INTEL_GEN(dev_priv) < 6)
Daniel Vettera08acaf2013-12-17 09:56:53 +0100230 return false;
Ben Widawsky2911a352012-04-05 14:47:36 -0700231
Jani Nikulad330a952014-01-21 11:24:25 +0200232 if (i915.semaphores >= 0)
233 return i915.semaphores;
Ben Widawsky2911a352012-04-05 14:47:36 -0700234
Oscar Mateo71386ef2014-07-24 17:04:44 +0100235 /* TODO: make semaphores and Execlists play nicely together */
236 if (i915.enable_execlists)
237 return false;
238
Daniel Vetter59de3292012-04-02 20:48:43 +0200239#ifdef CONFIG_INTEL_IOMMU
Ben Widawsky2911a352012-04-05 14:47:36 -0700240 /* Enable semaphores on SNB when IO remapping is off */
Chris Wilsonc0336662016-05-06 15:40:21 +0100241 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped)
Daniel Vetter59de3292012-04-02 20:48:43 +0200242 return false;
243#endif
Ben Widawsky2911a352012-04-05 14:47:36 -0700244
Daniel Vettera08acaf2013-12-17 09:56:53 +0100245 return true;
Ben Widawsky2911a352012-04-05 14:47:36 -0700246}
247
Chris Wilson0673ad42016-06-24 14:00:22 +0100248static int i915_getparam(struct drm_device *dev, void *data,
249 struct drm_file *file_priv)
250{
251 struct drm_i915_private *dev_priv = dev->dev_private;
252 drm_i915_getparam_t *param = data;
253 int value;
254
255 switch (param->param) {
256 case I915_PARAM_IRQ_ACTIVE:
257 case I915_PARAM_ALLOW_BATCHBUFFER:
258 case I915_PARAM_LAST_DISPATCH:
259 /* Reject all old ums/dri params. */
260 return -ENODEV;
261 case I915_PARAM_CHIPSET_ID:
262 value = dev->pdev->device;
263 break;
264 case I915_PARAM_REVISION:
265 value = dev->pdev->revision;
266 break;
267 case I915_PARAM_HAS_GEM:
268 value = 1;
269 break;
270 case I915_PARAM_NUM_FENCES_AVAIL:
271 value = dev_priv->num_fence_regs;
272 break;
273 case I915_PARAM_HAS_OVERLAY:
274 value = dev_priv->overlay ? 1 : 0;
275 break;
276 case I915_PARAM_HAS_PAGEFLIPPING:
277 value = 1;
278 break;
279 case I915_PARAM_HAS_EXECBUF2:
280 /* depends on GEM */
281 value = 1;
282 break;
283 case I915_PARAM_HAS_BSD:
284 value = intel_engine_initialized(&dev_priv->engine[VCS]);
285 break;
286 case I915_PARAM_HAS_BLT:
287 value = intel_engine_initialized(&dev_priv->engine[BCS]);
288 break;
289 case I915_PARAM_HAS_VEBOX:
290 value = intel_engine_initialized(&dev_priv->engine[VECS]);
291 break;
292 case I915_PARAM_HAS_BSD2:
293 value = intel_engine_initialized(&dev_priv->engine[VCS2]);
294 break;
295 case I915_PARAM_HAS_RELAXED_FENCING:
296 value = 1;
297 break;
298 case I915_PARAM_HAS_COHERENT_RINGS:
299 value = 1;
300 break;
301 case I915_PARAM_HAS_EXEC_CONSTANTS:
302 value = INTEL_INFO(dev)->gen >= 4;
303 break;
304 case I915_PARAM_HAS_RELAXED_DELTA:
305 value = 1;
306 break;
307 case I915_PARAM_HAS_GEN7_SOL_RESET:
308 value = 1;
309 break;
310 case I915_PARAM_HAS_LLC:
311 value = HAS_LLC(dev);
312 break;
313 case I915_PARAM_HAS_WT:
314 value = HAS_WT(dev);
315 break;
316 case I915_PARAM_HAS_ALIASING_PPGTT:
317 value = USES_PPGTT(dev);
318 break;
319 case I915_PARAM_HAS_WAIT_TIMEOUT:
320 value = 1;
321 break;
322 case I915_PARAM_HAS_SEMAPHORES:
323 value = i915_semaphore_is_enabled(dev_priv);
324 break;
325 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
326 value = 1;
327 break;
328 case I915_PARAM_HAS_SECURE_BATCHES:
329 value = capable(CAP_SYS_ADMIN);
330 break;
331 case I915_PARAM_HAS_PINNED_BATCHES:
332 value = 1;
333 break;
334 case I915_PARAM_HAS_EXEC_NO_RELOC:
335 value = 1;
336 break;
337 case I915_PARAM_HAS_EXEC_HANDLE_LUT:
338 value = 1;
339 break;
340 case I915_PARAM_CMD_PARSER_VERSION:
341 value = i915_cmd_parser_get_version(dev_priv);
342 break;
343 case I915_PARAM_HAS_COHERENT_PHYS_GTT:
344 value = 1;
345 break;
346 case I915_PARAM_MMAP_VERSION:
347 value = 1;
348 break;
349 case I915_PARAM_SUBSLICE_TOTAL:
350 value = INTEL_INFO(dev)->subslice_total;
351 if (!value)
352 return -ENODEV;
353 break;
354 case I915_PARAM_EU_TOTAL:
355 value = INTEL_INFO(dev)->eu_total;
356 if (!value)
357 return -ENODEV;
358 break;
359 case I915_PARAM_HAS_GPU_RESET:
360 value = i915.enable_hangcheck && intel_has_gpu_reset(dev_priv);
361 break;
362 case I915_PARAM_HAS_RESOURCE_STREAMER:
363 value = HAS_RESOURCE_STREAMER(dev);
364 break;
365 case I915_PARAM_HAS_EXEC_SOFTPIN:
366 value = 1;
367 break;
arun.siluvery@linux.intel.com37f501a2016-07-01 11:43:02 +0100368 case I915_PARAM_HAS_POOLED_EU:
369 value = HAS_POOLED_EU(dev);
370 break;
371 case I915_PARAM_MIN_EU_IN_POOL:
372 value = INTEL_INFO(dev)->min_eu_in_pool;
373 break;
Chris Wilson0673ad42016-06-24 14:00:22 +0100374 default:
375 DRM_DEBUG("Unknown parameter %d\n", param->param);
376 return -EINVAL;
377 }
378
Chris Wilsondda33002016-06-24 14:00:23 +0100379 if (put_user(value, param->value))
Chris Wilson0673ad42016-06-24 14:00:22 +0100380 return -EFAULT;
Chris Wilson0673ad42016-06-24 14:00:22 +0100381
382 return 0;
383}
384
385static int i915_get_bridge_dev(struct drm_device *dev)
386{
387 struct drm_i915_private *dev_priv = dev->dev_private;
388
389 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
390 if (!dev_priv->bridge_dev) {
391 DRM_ERROR("bridge device not found\n");
392 return -1;
393 }
394 return 0;
395}
396
397/* Allocate space for the MCH regs if needed, return nonzero on error */
398static int
399intel_alloc_mchbar_resource(struct drm_device *dev)
400{
401 struct drm_i915_private *dev_priv = dev->dev_private;
402 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
403 u32 temp_lo, temp_hi = 0;
404 u64 mchbar_addr;
405 int ret;
406
407 if (INTEL_INFO(dev)->gen >= 4)
408 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
409 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
410 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
411
412 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
413#ifdef CONFIG_PNP
414 if (mchbar_addr &&
415 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
416 return 0;
417#endif
418
419 /* Get some space for it */
420 dev_priv->mch_res.name = "i915 MCHBAR";
421 dev_priv->mch_res.flags = IORESOURCE_MEM;
422 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
423 &dev_priv->mch_res,
424 MCHBAR_SIZE, MCHBAR_SIZE,
425 PCIBIOS_MIN_MEM,
426 0, pcibios_align_resource,
427 dev_priv->bridge_dev);
428 if (ret) {
429 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
430 dev_priv->mch_res.start = 0;
431 return ret;
432 }
433
434 if (INTEL_INFO(dev)->gen >= 4)
435 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
436 upper_32_bits(dev_priv->mch_res.start));
437
438 pci_write_config_dword(dev_priv->bridge_dev, reg,
439 lower_32_bits(dev_priv->mch_res.start));
440 return 0;
441}
442
443/* Setup MCHBAR if possible, return true if we should disable it again */
444static void
445intel_setup_mchbar(struct drm_device *dev)
446{
447 struct drm_i915_private *dev_priv = dev->dev_private;
448 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
449 u32 temp;
450 bool enabled;
451
452 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
453 return;
454
455 dev_priv->mchbar_need_disable = false;
456
457 if (IS_I915G(dev) || IS_I915GM(dev)) {
458 pci_read_config_dword(dev_priv->bridge_dev, DEVEN, &temp);
459 enabled = !!(temp & DEVEN_MCHBAR_EN);
460 } else {
461 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
462 enabled = temp & 1;
463 }
464
465 /* If it's already enabled, don't have to do anything */
466 if (enabled)
467 return;
468
469 if (intel_alloc_mchbar_resource(dev))
470 return;
471
472 dev_priv->mchbar_need_disable = true;
473
474 /* Space is allocated or reserved, so enable it. */
475 if (IS_I915G(dev) || IS_I915GM(dev)) {
476 pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
477 temp | DEVEN_MCHBAR_EN);
478 } else {
479 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
480 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
481 }
482}
483
484static void
485intel_teardown_mchbar(struct drm_device *dev)
486{
487 struct drm_i915_private *dev_priv = dev->dev_private;
488 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
489
490 if (dev_priv->mchbar_need_disable) {
491 if (IS_I915G(dev) || IS_I915GM(dev)) {
492 u32 deven_val;
493
494 pci_read_config_dword(dev_priv->bridge_dev, DEVEN,
495 &deven_val);
496 deven_val &= ~DEVEN_MCHBAR_EN;
497 pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
498 deven_val);
499 } else {
500 u32 mchbar_val;
501
502 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg,
503 &mchbar_val);
504 mchbar_val &= ~1;
505 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg,
506 mchbar_val);
507 }
508 }
509
510 if (dev_priv->mch_res.start)
511 release_resource(&dev_priv->mch_res);
512}
513
514/* true = enable decode, false = disable decoder */
515static unsigned int i915_vga_set_decode(void *cookie, bool state)
516{
517 struct drm_device *dev = cookie;
518
519 intel_modeset_vga_set_state(dev, state);
520 if (state)
521 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
522 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
523 else
524 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
525}
526
527static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
528{
529 struct drm_device *dev = pci_get_drvdata(pdev);
530 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
531
532 if (state == VGA_SWITCHEROO_ON) {
533 pr_info("switched on\n");
534 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
535 /* i915 resume handler doesn't set to D0 */
536 pci_set_power_state(dev->pdev, PCI_D0);
537 i915_resume_switcheroo(dev);
538 dev->switch_power_state = DRM_SWITCH_POWER_ON;
539 } else {
540 pr_info("switched off\n");
541 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
542 i915_suspend_switcheroo(dev, pmm);
543 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
544 }
545}
546
547static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
548{
549 struct drm_device *dev = pci_get_drvdata(pdev);
550
551 /*
552 * FIXME: open_count is protected by drm_global_mutex but that would lead to
553 * locking inversion with the driver load path. And the access here is
554 * completely racy anyway. So don't bother with locking for now.
555 */
556 return dev->open_count == 0;
557}
558
559static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
560 .set_gpu_state = i915_switcheroo_set_state,
561 .reprobe = NULL,
562 .can_switch = i915_switcheroo_can_switch,
563};
564
565static void i915_gem_fini(struct drm_device *dev)
566{
567 struct drm_i915_private *dev_priv = to_i915(dev);
568
569 /*
570 * Neither the BIOS, ourselves or any other kernel
571 * expects the system to be in execlists mode on startup,
572 * so we need to reset the GPU back to legacy mode. And the only
573 * known way to disable logical contexts is through a GPU reset.
574 *
575 * So in order to leave the system in a known default configuration,
576 * always reset the GPU upon unload. Afterwards we then clean up the
577 * GEM state tracking, flushing off the requests and leaving the
578 * system in a known idle state.
579 *
580 * Note that is of the upmost importance that the GPU is idle and
581 * all stray writes are flushed *before* we dismantle the backing
582 * storage for the pinned objects.
583 *
584 * However, since we are uncertain that reseting the GPU on older
585 * machines is a good idea, we don't - just in case it leaves the
586 * machine in an unusable condition.
587 */
588 if (HAS_HW_CONTEXTS(dev)) {
589 int reset = intel_gpu_reset(dev_priv, ALL_ENGINES);
590 WARN_ON(reset && reset != -ENODEV);
591 }
592
593 mutex_lock(&dev->struct_mutex);
594 i915_gem_reset(dev);
595 i915_gem_cleanup_engines(dev);
596 i915_gem_context_fini(dev);
597 mutex_unlock(&dev->struct_mutex);
598
599 WARN_ON(!list_empty(&to_i915(dev)->context_list));
600}
601
602static int i915_load_modeset_init(struct drm_device *dev)
603{
604 struct drm_i915_private *dev_priv = dev->dev_private;
605 int ret;
606
607 if (i915_inject_load_failure())
608 return -ENODEV;
609
610 ret = intel_bios_init(dev_priv);
611 if (ret)
612 DRM_INFO("failed to find VBIOS tables\n");
613
614 /* If we have > 1 VGA cards, then we need to arbitrate access
615 * to the common VGA resources.
616 *
617 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
618 * then we do not take part in VGA arbitration and the
619 * vga_client_register() fails with -ENODEV.
620 */
621 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
622 if (ret && ret != -ENODEV)
623 goto out;
624
625 intel_register_dsm_handler();
626
627 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops, false);
628 if (ret)
629 goto cleanup_vga_client;
630
631 /* must happen before intel_power_domains_init_hw() on VLV/CHV */
632 intel_update_rawclk(dev_priv);
633
634 intel_power_domains_init_hw(dev_priv, false);
635
636 intel_csr_ucode_init(dev_priv);
637
638 ret = intel_irq_install(dev_priv);
639 if (ret)
640 goto cleanup_csr;
641
642 intel_setup_gmbus(dev);
643
644 /* Important: The output setup functions called by modeset_init need
645 * working irqs for e.g. gmbus and dp aux transfers. */
646 intel_modeset_init(dev);
647
648 intel_guc_init(dev);
649
650 ret = i915_gem_init(dev);
651 if (ret)
652 goto cleanup_irq;
653
654 intel_modeset_gem_init(dev);
655
656 if (INTEL_INFO(dev)->num_pipes == 0)
657 return 0;
658
659 ret = intel_fbdev_init(dev);
660 if (ret)
661 goto cleanup_gem;
662
663 /* Only enable hotplug handling once the fbdev is fully set up. */
664 intel_hpd_init(dev_priv);
665
666 drm_kms_helper_poll_init(dev);
667
668 return 0;
669
670cleanup_gem:
671 i915_gem_fini(dev);
672cleanup_irq:
673 intel_guc_fini(dev);
674 drm_irq_uninstall(dev);
675 intel_teardown_gmbus(dev);
676cleanup_csr:
677 intel_csr_ucode_fini(dev_priv);
678 intel_power_domains_fini(dev_priv);
679 vga_switcheroo_unregister_client(dev->pdev);
680cleanup_vga_client:
681 vga_client_register(dev->pdev, NULL, NULL, NULL);
682out:
683 return ret;
684}
685
686#if IS_ENABLED(CONFIG_FB)
687static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
688{
689 struct apertures_struct *ap;
690 struct pci_dev *pdev = dev_priv->dev->pdev;
691 struct i915_ggtt *ggtt = &dev_priv->ggtt;
692 bool primary;
693 int ret;
694
695 ap = alloc_apertures(1);
696 if (!ap)
697 return -ENOMEM;
698
699 ap->ranges[0].base = ggtt->mappable_base;
700 ap->ranges[0].size = ggtt->mappable_end;
701
702 primary =
703 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
704
705 ret = remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
706
707 kfree(ap);
708
709 return ret;
710}
711#else
712static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
713{
714 return 0;
715}
716#endif
717
718#if !defined(CONFIG_VGA_CONSOLE)
719static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
720{
721 return 0;
722}
723#elif !defined(CONFIG_DUMMY_CONSOLE)
724static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
725{
726 return -ENODEV;
727}
728#else
729static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
730{
731 int ret = 0;
732
733 DRM_INFO("Replacing VGA console driver\n");
734
735 console_lock();
736 if (con_is_bound(&vga_con))
737 ret = do_take_over_console(&dummy_con, 0, MAX_NR_CONSOLES - 1, 1);
738 if (ret == 0) {
739 ret = do_unregister_con_driver(&vga_con);
740
741 /* Ignore "already unregistered". */
742 if (ret == -ENODEV)
743 ret = 0;
744 }
745 console_unlock();
746
747 return ret;
748}
749#endif
750
751static void i915_dump_device_info(struct drm_i915_private *dev_priv)
752{
753 const struct intel_device_info *info = &dev_priv->info;
754
755#define PRINT_S(name) "%s"
756#define SEP_EMPTY
757#define PRINT_FLAG(name) info->name ? #name "," : ""
758#define SEP_COMMA ,
759 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x rev=0x%02x flags="
760 DEV_INFO_FOR_EACH_FLAG(PRINT_S, SEP_EMPTY),
761 info->gen,
762 dev_priv->dev->pdev->device,
763 dev_priv->dev->pdev->revision,
764 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG, SEP_COMMA));
765#undef PRINT_S
766#undef SEP_EMPTY
767#undef PRINT_FLAG
768#undef SEP_COMMA
769}
770
771static void cherryview_sseu_info_init(struct drm_device *dev)
772{
773 struct drm_i915_private *dev_priv = dev->dev_private;
774 struct intel_device_info *info;
775 u32 fuse, eu_dis;
776
777 info = (struct intel_device_info *)&dev_priv->info;
778 fuse = I915_READ(CHV_FUSE_GT);
779
780 info->slice_total = 1;
781
782 if (!(fuse & CHV_FGT_DISABLE_SS0)) {
783 info->subslice_per_slice++;
784 eu_dis = fuse & (CHV_FGT_EU_DIS_SS0_R0_MASK |
785 CHV_FGT_EU_DIS_SS0_R1_MASK);
786 info->eu_total += 8 - hweight32(eu_dis);
787 }
788
789 if (!(fuse & CHV_FGT_DISABLE_SS1)) {
790 info->subslice_per_slice++;
791 eu_dis = fuse & (CHV_FGT_EU_DIS_SS1_R0_MASK |
792 CHV_FGT_EU_DIS_SS1_R1_MASK);
793 info->eu_total += 8 - hweight32(eu_dis);
794 }
795
796 info->subslice_total = info->subslice_per_slice;
797 /*
798 * CHV expected to always have a uniform distribution of EU
799 * across subslices.
800 */
801 info->eu_per_subslice = info->subslice_total ?
802 info->eu_total / info->subslice_total :
803 0;
804 /*
805 * CHV supports subslice power gating on devices with more than
806 * one subslice, and supports EU power gating on devices with
807 * more than one EU pair per subslice.
808 */
809 info->has_slice_pg = 0;
810 info->has_subslice_pg = (info->subslice_total > 1);
811 info->has_eu_pg = (info->eu_per_subslice > 2);
812}
813
814static void gen9_sseu_info_init(struct drm_device *dev)
815{
816 struct drm_i915_private *dev_priv = dev->dev_private;
817 struct intel_device_info *info;
818 int s_max = 3, ss_max = 4, eu_max = 8;
819 int s, ss;
820 u32 fuse2, s_enable, ss_disable, eu_disable;
821 u8 eu_mask = 0xff;
822
823 info = (struct intel_device_info *)&dev_priv->info;
824 fuse2 = I915_READ(GEN8_FUSE2);
825 s_enable = (fuse2 & GEN8_F2_S_ENA_MASK) >>
826 GEN8_F2_S_ENA_SHIFT;
827 ss_disable = (fuse2 & GEN9_F2_SS_DIS_MASK) >>
828 GEN9_F2_SS_DIS_SHIFT;
829
830 info->slice_total = hweight32(s_enable);
831 /*
832 * The subslice disable field is global, i.e. it applies
833 * to each of the enabled slices.
834 */
835 info->subslice_per_slice = ss_max - hweight32(ss_disable);
836 info->subslice_total = info->slice_total *
837 info->subslice_per_slice;
838
839 /*
840 * Iterate through enabled slices and subslices to
841 * count the total enabled EU.
842 */
843 for (s = 0; s < s_max; s++) {
844 if (!(s_enable & (0x1 << s)))
845 /* skip disabled slice */
846 continue;
847
848 eu_disable = I915_READ(GEN9_EU_DISABLE(s));
849 for (ss = 0; ss < ss_max; ss++) {
850 int eu_per_ss;
851
852 if (ss_disable & (0x1 << ss))
853 /* skip disabled subslice */
854 continue;
855
856 eu_per_ss = eu_max - hweight8((eu_disable >> (ss*8)) &
857 eu_mask);
858
859 /*
860 * Record which subslice(s) has(have) 7 EUs. we
861 * can tune the hash used to spread work among
862 * subslices if they are unbalanced.
863 */
864 if (eu_per_ss == 7)
865 info->subslice_7eu[s] |= 1 << ss;
866
867 info->eu_total += eu_per_ss;
868 }
869 }
870
871 /*
872 * SKL is expected to always have a uniform distribution
873 * of EU across subslices with the exception that any one
874 * EU in any one subslice may be fused off for die
875 * recovery. BXT is expected to be perfectly uniform in EU
876 * distribution.
877 */
878 info->eu_per_subslice = info->subslice_total ?
879 DIV_ROUND_UP(info->eu_total,
880 info->subslice_total) : 0;
881 /*
882 * SKL supports slice power gating on devices with more than
883 * one slice, and supports EU power gating on devices with
884 * more than one EU pair per subslice. BXT supports subslice
885 * power gating on devices with more than one subslice, and
886 * supports EU power gating on devices with more than one EU
887 * pair per subslice.
888 */
889 info->has_slice_pg = ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
890 (info->slice_total > 1));
891 info->has_subslice_pg = (IS_BROXTON(dev) && (info->subslice_total > 1));
892 info->has_eu_pg = (info->eu_per_subslice > 2);
893
894 if (IS_BROXTON(dev)) {
895#define IS_SS_DISABLED(_ss_disable, ss) (_ss_disable & (0x1 << ss))
896 /*
897 * There is a HW issue in 2x6 fused down parts that requires
898 * Pooled EU to be enabled as a WA. The pool configuration
899 * changes depending upon which subslice is fused down. This
900 * doesn't affect if the device has all 3 subslices enabled.
901 */
902 /* WaEnablePooledEuFor2x6:bxt */
903 info->has_pooled_eu = ((info->subslice_per_slice == 3) ||
904 (info->subslice_per_slice == 2 &&
905 INTEL_REVID(dev) < BXT_REVID_C0));
906
907 info->min_eu_in_pool = 0;
908 if (info->has_pooled_eu) {
909 if (IS_SS_DISABLED(ss_disable, 0) ||
910 IS_SS_DISABLED(ss_disable, 2))
911 info->min_eu_in_pool = 3;
912 else if (IS_SS_DISABLED(ss_disable, 1))
913 info->min_eu_in_pool = 6;
914 else
915 info->min_eu_in_pool = 9;
916 }
917#undef IS_SS_DISABLED
918 }
919}
920
921static void broadwell_sseu_info_init(struct drm_device *dev)
922{
923 struct drm_i915_private *dev_priv = dev->dev_private;
924 struct intel_device_info *info;
925 const int s_max = 3, ss_max = 3, eu_max = 8;
926 int s, ss;
927 u32 fuse2, eu_disable[s_max], s_enable, ss_disable;
928
929 fuse2 = I915_READ(GEN8_FUSE2);
930 s_enable = (fuse2 & GEN8_F2_S_ENA_MASK) >> GEN8_F2_S_ENA_SHIFT;
931 ss_disable = (fuse2 & GEN8_F2_SS_DIS_MASK) >> GEN8_F2_SS_DIS_SHIFT;
932
933 eu_disable[0] = I915_READ(GEN8_EU_DISABLE0) & GEN8_EU_DIS0_S0_MASK;
934 eu_disable[1] = (I915_READ(GEN8_EU_DISABLE0) >> GEN8_EU_DIS0_S1_SHIFT) |
935 ((I915_READ(GEN8_EU_DISABLE1) & GEN8_EU_DIS1_S1_MASK) <<
936 (32 - GEN8_EU_DIS0_S1_SHIFT));
937 eu_disable[2] = (I915_READ(GEN8_EU_DISABLE1) >> GEN8_EU_DIS1_S2_SHIFT) |
938 ((I915_READ(GEN8_EU_DISABLE2) & GEN8_EU_DIS2_S2_MASK) <<
939 (32 - GEN8_EU_DIS1_S2_SHIFT));
940
941
942 info = (struct intel_device_info *)&dev_priv->info;
943 info->slice_total = hweight32(s_enable);
944
945 /*
946 * The subslice disable field is global, i.e. it applies
947 * to each of the enabled slices.
948 */
949 info->subslice_per_slice = ss_max - hweight32(ss_disable);
950 info->subslice_total = info->slice_total * info->subslice_per_slice;
951
952 /*
953 * Iterate through enabled slices and subslices to
954 * count the total enabled EU.
955 */
956 for (s = 0; s < s_max; s++) {
957 if (!(s_enable & (0x1 << s)))
958 /* skip disabled slice */
959 continue;
960
961 for (ss = 0; ss < ss_max; ss++) {
962 u32 n_disabled;
963
964 if (ss_disable & (0x1 << ss))
965 /* skip disabled subslice */
966 continue;
967
968 n_disabled = hweight8(eu_disable[s] >> (ss * eu_max));
969
970 /*
971 * Record which subslices have 7 EUs.
972 */
973 if (eu_max - n_disabled == 7)
974 info->subslice_7eu[s] |= 1 << ss;
975
976 info->eu_total += eu_max - n_disabled;
977 }
978 }
979
980 /*
981 * BDW is expected to always have a uniform distribution of EU across
982 * subslices with the exception that any one EU in any one subslice may
983 * be fused off for die recovery.
984 */
985 info->eu_per_subslice = info->subslice_total ?
986 DIV_ROUND_UP(info->eu_total, info->subslice_total) : 0;
987
988 /*
989 * BDW supports slice power gating on devices with more than
990 * one slice.
991 */
992 info->has_slice_pg = (info->slice_total > 1);
993 info->has_subslice_pg = 0;
994 info->has_eu_pg = 0;
995}
996
997/*
998 * Determine various intel_device_info fields at runtime.
999 *
1000 * Use it when either:
1001 * - it's judged too laborious to fill n static structures with the limit
1002 * when a simple if statement does the job,
1003 * - run-time checks (eg read fuse/strap registers) are needed.
1004 *
1005 * This function needs to be called:
1006 * - after the MMIO has been setup as we are reading registers,
1007 * - after the PCH has been detected,
1008 * - before the first usage of the fields it can tweak.
1009 */
1010static void intel_device_info_runtime_init(struct drm_device *dev)
1011{
1012 struct drm_i915_private *dev_priv = dev->dev_private;
1013 struct intel_device_info *info;
1014 enum pipe pipe;
1015
1016 info = (struct intel_device_info *)&dev_priv->info;
1017
1018 /*
1019 * Skylake and Broxton currently don't expose the topmost plane as its
1020 * use is exclusive with the legacy cursor and we only want to expose
1021 * one of those, not both. Until we can safely expose the topmost plane
1022 * as a DRM_PLANE_TYPE_CURSOR with all the features exposed/supported,
1023 * we don't expose the topmost plane at all to prevent ABI breakage
1024 * down the line.
1025 */
1026 if (IS_BROXTON(dev)) {
1027 info->num_sprites[PIPE_A] = 2;
1028 info->num_sprites[PIPE_B] = 2;
1029 info->num_sprites[PIPE_C] = 1;
1030 } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
1031 for_each_pipe(dev_priv, pipe)
1032 info->num_sprites[pipe] = 2;
1033 else
1034 for_each_pipe(dev_priv, pipe)
1035 info->num_sprites[pipe] = 1;
1036
1037 if (i915.disable_display) {
1038 DRM_INFO("Display disabled (module parameter)\n");
1039 info->num_pipes = 0;
1040 } else if (info->num_pipes > 0 &&
1041 (IS_GEN7(dev_priv) || IS_GEN8(dev_priv)) &&
1042 HAS_PCH_SPLIT(dev)) {
1043 u32 fuse_strap = I915_READ(FUSE_STRAP);
1044 u32 sfuse_strap = I915_READ(SFUSE_STRAP);
1045
1046 /*
1047 * SFUSE_STRAP is supposed to have a bit signalling the display
1048 * is fused off. Unfortunately it seems that, at least in
1049 * certain cases, fused off display means that PCH display
1050 * reads don't land anywhere. In that case, we read 0s.
1051 *
1052 * On CPT/PPT, we can detect this case as SFUSE_STRAP_FUSE_LOCK
1053 * should be set when taking over after the firmware.
1054 */
1055 if (fuse_strap & ILK_INTERNAL_DISPLAY_DISABLE ||
1056 sfuse_strap & SFUSE_STRAP_DISPLAY_DISABLED ||
1057 (dev_priv->pch_type == PCH_CPT &&
1058 !(sfuse_strap & SFUSE_STRAP_FUSE_LOCK))) {
1059 DRM_INFO("Display fused off, disabling\n");
1060 info->num_pipes = 0;
1061 } else if (fuse_strap & IVB_PIPE_C_DISABLE) {
1062 DRM_INFO("PipeC fused off\n");
1063 info->num_pipes -= 1;
1064 }
1065 } else if (info->num_pipes > 0 && IS_GEN9(dev_priv)) {
1066 u32 dfsm = I915_READ(SKL_DFSM);
1067 u8 disabled_mask = 0;
1068 bool invalid;
1069 int num_bits;
1070
1071 if (dfsm & SKL_DFSM_PIPE_A_DISABLE)
1072 disabled_mask |= BIT(PIPE_A);
1073 if (dfsm & SKL_DFSM_PIPE_B_DISABLE)
1074 disabled_mask |= BIT(PIPE_B);
1075 if (dfsm & SKL_DFSM_PIPE_C_DISABLE)
1076 disabled_mask |= BIT(PIPE_C);
1077
1078 num_bits = hweight8(disabled_mask);
1079
1080 switch (disabled_mask) {
1081 case BIT(PIPE_A):
1082 case BIT(PIPE_B):
1083 case BIT(PIPE_A) | BIT(PIPE_B):
1084 case BIT(PIPE_A) | BIT(PIPE_C):
1085 invalid = true;
1086 break;
1087 default:
1088 invalid = false;
1089 }
1090
1091 if (num_bits > info->num_pipes || invalid)
1092 DRM_ERROR("invalid pipe fuse configuration: 0x%x\n",
1093 disabled_mask);
1094 else
1095 info->num_pipes -= num_bits;
1096 }
1097
1098 /* Initialize slice/subslice/EU info */
1099 if (IS_CHERRYVIEW(dev))
1100 cherryview_sseu_info_init(dev);
1101 else if (IS_BROADWELL(dev))
1102 broadwell_sseu_info_init(dev);
1103 else if (INTEL_INFO(dev)->gen >= 9)
1104 gen9_sseu_info_init(dev);
1105
1106 info->has_snoop = !info->has_llc;
1107
1108 /* Snooping is broken on BXT A stepping. */
1109 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1))
1110 info->has_snoop = false;
1111
1112 DRM_DEBUG_DRIVER("slice total: %u\n", info->slice_total);
1113 DRM_DEBUG_DRIVER("subslice total: %u\n", info->subslice_total);
1114 DRM_DEBUG_DRIVER("subslice per slice: %u\n", info->subslice_per_slice);
1115 DRM_DEBUG_DRIVER("EU total: %u\n", info->eu_total);
1116 DRM_DEBUG_DRIVER("EU per subslice: %u\n", info->eu_per_subslice);
1117 DRM_DEBUG_DRIVER("has slice power gating: %s\n",
1118 info->has_slice_pg ? "y" : "n");
1119 DRM_DEBUG_DRIVER("has subslice power gating: %s\n",
1120 info->has_subslice_pg ? "y" : "n");
1121 DRM_DEBUG_DRIVER("has EU power gating: %s\n",
1122 info->has_eu_pg ? "y" : "n");
1123
1124 i915.enable_execlists =
1125 intel_sanitize_enable_execlists(dev_priv,
1126 i915.enable_execlists);
1127
1128 /*
1129 * i915.enable_ppgtt is read-only, so do an early pass to validate the
1130 * user's requested state against the hardware/driver capabilities. We
1131 * do this now so that we can print out any log messages once rather
1132 * than every time we check intel_enable_ppgtt().
1133 */
1134 i915.enable_ppgtt =
1135 intel_sanitize_enable_ppgtt(dev_priv, i915.enable_ppgtt);
1136 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
1137}
1138
1139static void intel_init_dpio(struct drm_i915_private *dev_priv)
1140{
1141 /*
1142 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1143 * CHV x1 PHY (DP/HDMI D)
1144 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1145 */
1146 if (IS_CHERRYVIEW(dev_priv)) {
1147 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1148 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1149 } else if (IS_VALLEYVIEW(dev_priv)) {
1150 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1151 }
1152}
1153
1154static int i915_workqueues_init(struct drm_i915_private *dev_priv)
1155{
1156 /*
1157 * The i915 workqueue is primarily used for batched retirement of
1158 * requests (and thus managing bo) once the task has been completed
1159 * by the GPU. i915_gem_retire_requests() is called directly when we
1160 * need high-priority retirement, such as waiting for an explicit
1161 * bo.
1162 *
1163 * It is also used for periodic low-priority events, such as
1164 * idle-timers and recording error state.
1165 *
1166 * All tasks on the workqueue are expected to acquire the dev mutex
1167 * so there is no point in running more than one instance of the
1168 * workqueue at any time. Use an ordered one.
1169 */
1170 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
1171 if (dev_priv->wq == NULL)
1172 goto out_err;
1173
1174 dev_priv->hotplug.dp_wq = alloc_ordered_workqueue("i915-dp", 0);
1175 if (dev_priv->hotplug.dp_wq == NULL)
1176 goto out_free_wq;
1177
1178 dev_priv->gpu_error.hangcheck_wq =
1179 alloc_ordered_workqueue("i915-hangcheck", 0);
1180 if (dev_priv->gpu_error.hangcheck_wq == NULL)
1181 goto out_free_dp_wq;
1182
1183 return 0;
1184
1185out_free_dp_wq:
1186 destroy_workqueue(dev_priv->hotplug.dp_wq);
1187out_free_wq:
1188 destroy_workqueue(dev_priv->wq);
1189out_err:
1190 DRM_ERROR("Failed to allocate workqueues.\n");
1191
1192 return -ENOMEM;
1193}
1194
1195static void i915_workqueues_cleanup(struct drm_i915_private *dev_priv)
1196{
1197 destroy_workqueue(dev_priv->gpu_error.hangcheck_wq);
1198 destroy_workqueue(dev_priv->hotplug.dp_wq);
1199 destroy_workqueue(dev_priv->wq);
1200}
1201
1202/**
1203 * i915_driver_init_early - setup state not requiring device access
1204 * @dev_priv: device private
1205 *
1206 * Initialize everything that is a "SW-only" state, that is state not
1207 * requiring accessing the device or exposing the driver via kernel internal
1208 * or userspace interfaces. Example steps belonging here: lock initialization,
1209 * system memory allocation, setting up device specific attributes and
1210 * function hooks not requiring accessing the device.
1211 */
1212static int i915_driver_init_early(struct drm_i915_private *dev_priv,
1213 const struct pci_device_id *ent)
1214{
1215 const struct intel_device_info *match_info =
1216 (struct intel_device_info *)ent->driver_data;
1217 struct intel_device_info *device_info;
1218 int ret = 0;
1219
1220 if (i915_inject_load_failure())
1221 return -ENODEV;
1222
1223 /* Setup the write-once "constant" device info */
1224 device_info = (struct intel_device_info *)&dev_priv->info;
1225 memcpy(device_info, match_info, sizeof(*device_info));
1226 device_info->device_id = dev_priv->drm.pdev->device;
1227
1228 BUG_ON(device_info->gen > sizeof(device_info->gen_mask) * BITS_PER_BYTE);
1229 device_info->gen_mask = BIT(device_info->gen - 1);
1230
1231 spin_lock_init(&dev_priv->irq_lock);
1232 spin_lock_init(&dev_priv->gpu_error.lock);
1233 mutex_init(&dev_priv->backlight_lock);
1234 spin_lock_init(&dev_priv->uncore.lock);
1235 spin_lock_init(&dev_priv->mm.object_stat_lock);
1236 spin_lock_init(&dev_priv->mmio_flip_lock);
1237 mutex_init(&dev_priv->sb_lock);
1238 mutex_init(&dev_priv->modeset_restore_lock);
1239 mutex_init(&dev_priv->av_mutex);
1240 mutex_init(&dev_priv->wm.wm_mutex);
1241 mutex_init(&dev_priv->pps_mutex);
1242
1243 ret = i915_workqueues_init(dev_priv);
1244 if (ret < 0)
1245 return ret;
1246
1247 ret = intel_gvt_init(dev_priv);
1248 if (ret < 0)
1249 goto err_workqueues;
1250
1251 /* This must be called before any calls to HAS_PCH_* */
1252 intel_detect_pch(&dev_priv->drm);
1253
1254 intel_pm_setup(&dev_priv->drm);
1255 intel_init_dpio(dev_priv);
1256 intel_power_domains_init(dev_priv);
1257 intel_irq_init(dev_priv);
1258 intel_init_display_hooks(dev_priv);
1259 intel_init_clock_gating_hooks(dev_priv);
1260 intel_init_audio_hooks(dev_priv);
1261 i915_gem_load_init(&dev_priv->drm);
1262
1263 intel_display_crc_init(&dev_priv->drm);
1264
1265 i915_dump_device_info(dev_priv);
1266
1267 /* Not all pre-production machines fall into this category, only the
1268 * very first ones. Almost everything should work, except for maybe
1269 * suspend/resume. And we don't implement workarounds that affect only
1270 * pre-production machines. */
1271 if (IS_HSW_EARLY_SDV(dev_priv))
1272 DRM_INFO("This is an early pre-production Haswell machine. "
1273 "It may not be fully functional.\n");
1274
1275 return 0;
1276
1277err_workqueues:
1278 i915_workqueues_cleanup(dev_priv);
1279 return ret;
1280}
1281
1282/**
1283 * i915_driver_cleanup_early - cleanup the setup done in i915_driver_init_early()
1284 * @dev_priv: device private
1285 */
1286static void i915_driver_cleanup_early(struct drm_i915_private *dev_priv)
1287{
1288 i915_gem_load_cleanup(dev_priv->dev);
1289 i915_workqueues_cleanup(dev_priv);
1290}
1291
1292static int i915_mmio_setup(struct drm_device *dev)
1293{
1294 struct drm_i915_private *dev_priv = to_i915(dev);
1295 int mmio_bar;
1296 int mmio_size;
1297
1298 mmio_bar = IS_GEN2(dev) ? 1 : 0;
1299 /*
1300 * Before gen4, the registers and the GTT are behind different BARs.
1301 * However, from gen4 onwards, the registers and the GTT are shared
1302 * in the same BAR, so we want to restrict this ioremap from
1303 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1304 * the register BAR remains the same size for all the earlier
1305 * generations up to Ironlake.
1306 */
1307 if (INTEL_INFO(dev)->gen < 5)
1308 mmio_size = 512 * 1024;
1309 else
1310 mmio_size = 2 * 1024 * 1024;
1311 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
1312 if (dev_priv->regs == NULL) {
1313 DRM_ERROR("failed to map registers\n");
1314
1315 return -EIO;
1316 }
1317
1318 /* Try to make sure MCHBAR is enabled before poking at it */
1319 intel_setup_mchbar(dev);
1320
1321 return 0;
1322}
1323
1324static void i915_mmio_cleanup(struct drm_device *dev)
1325{
1326 struct drm_i915_private *dev_priv = to_i915(dev);
1327
1328 intel_teardown_mchbar(dev);
1329 pci_iounmap(dev->pdev, dev_priv->regs);
1330}
1331
1332/**
1333 * i915_driver_init_mmio - setup device MMIO
1334 * @dev_priv: device private
1335 *
1336 * Setup minimal device state necessary for MMIO accesses later in the
1337 * initialization sequence. The setup here should avoid any other device-wide
1338 * side effects or exposing the driver via kernel internal or user space
1339 * interfaces.
1340 */
1341static int i915_driver_init_mmio(struct drm_i915_private *dev_priv)
1342{
1343 struct drm_device *dev = dev_priv->dev;
1344 int ret;
1345
1346 if (i915_inject_load_failure())
1347 return -ENODEV;
1348
1349 if (i915_get_bridge_dev(dev))
1350 return -EIO;
1351
1352 ret = i915_mmio_setup(dev);
1353 if (ret < 0)
1354 goto put_bridge;
1355
1356 intel_uncore_init(dev_priv);
1357
1358 return 0;
1359
1360put_bridge:
1361 pci_dev_put(dev_priv->bridge_dev);
1362
1363 return ret;
1364}
1365
1366/**
1367 * i915_driver_cleanup_mmio - cleanup the setup done in i915_driver_init_mmio()
1368 * @dev_priv: device private
1369 */
1370static void i915_driver_cleanup_mmio(struct drm_i915_private *dev_priv)
1371{
1372 struct drm_device *dev = dev_priv->dev;
1373
1374 intel_uncore_fini(dev_priv);
1375 i915_mmio_cleanup(dev);
1376 pci_dev_put(dev_priv->bridge_dev);
1377}
1378
1379/**
1380 * i915_driver_init_hw - setup state requiring device access
1381 * @dev_priv: device private
1382 *
1383 * Setup state that requires accessing the device, but doesn't require
1384 * exposing the driver via kernel internal or userspace interfaces.
1385 */
1386static int i915_driver_init_hw(struct drm_i915_private *dev_priv)
1387{
1388 struct drm_device *dev = dev_priv->dev;
1389 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1390 uint32_t aperture_size;
1391 int ret;
1392
1393 if (i915_inject_load_failure())
1394 return -ENODEV;
1395
1396 intel_device_info_runtime_init(dev);
1397
1398 ret = i915_ggtt_init_hw(dev);
1399 if (ret)
1400 return ret;
1401
1402 ret = i915_ggtt_enable_hw(dev);
1403 if (ret) {
1404 DRM_ERROR("failed to enable GGTT\n");
1405 goto out_ggtt;
1406 }
1407
1408 /* WARNING: Apparently we must kick fbdev drivers before vgacon,
1409 * otherwise the vga fbdev driver falls over. */
1410 ret = i915_kick_out_firmware_fb(dev_priv);
1411 if (ret) {
1412 DRM_ERROR("failed to remove conflicting framebuffer drivers\n");
1413 goto out_ggtt;
1414 }
1415
1416 ret = i915_kick_out_vgacon(dev_priv);
1417 if (ret) {
1418 DRM_ERROR("failed to remove conflicting VGA console\n");
1419 goto out_ggtt;
1420 }
1421
1422 pci_set_master(dev->pdev);
1423
1424 /* overlay on gen2 is broken and can't address above 1G */
1425 if (IS_GEN2(dev)) {
1426 ret = dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1427 if (ret) {
1428 DRM_ERROR("failed to set DMA mask\n");
1429
1430 goto out_ggtt;
1431 }
1432 }
1433
1434
1435 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1436 * using 32bit addressing, overwriting memory if HWS is located
1437 * above 4GB.
1438 *
1439 * The documentation also mentions an issue with undefined
1440 * behaviour if any general state is accessed within a page above 4GB,
1441 * which also needs to be handled carefully.
1442 */
1443 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev)) {
1444 ret = dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1445
1446 if (ret) {
1447 DRM_ERROR("failed to set DMA mask\n");
1448
1449 goto out_ggtt;
1450 }
1451 }
1452
1453 aperture_size = ggtt->mappable_end;
1454
1455 ggtt->mappable =
1456 io_mapping_create_wc(ggtt->mappable_base,
1457 aperture_size);
1458 if (!ggtt->mappable) {
1459 ret = -EIO;
1460 goto out_ggtt;
1461 }
1462
1463 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base,
1464 aperture_size);
1465
1466 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY,
1467 PM_QOS_DEFAULT_VALUE);
1468
1469 intel_uncore_sanitize(dev_priv);
1470
1471 intel_opregion_setup(dev_priv);
1472
1473 i915_gem_load_init_fences(dev_priv);
1474
1475 /* On the 945G/GM, the chipset reports the MSI capability on the
1476 * integrated graphics even though the support isn't actually there
1477 * according to the published specs. It doesn't appear to function
1478 * correctly in testing on 945G.
1479 * This may be a side effect of MSI having been made available for PEG
1480 * and the registers being closely associated.
1481 *
1482 * According to chipset errata, on the 965GM, MSI interrupts may
1483 * be lost or delayed, but we use them anyways to avoid
1484 * stuck interrupts on some machines.
1485 */
1486 if (!IS_I945G(dev) && !IS_I945GM(dev)) {
1487 if (pci_enable_msi(dev->pdev) < 0)
1488 DRM_DEBUG_DRIVER("can't enable MSI");
1489 }
1490
1491 return 0;
1492
1493out_ggtt:
1494 i915_ggtt_cleanup_hw(dev);
1495
1496 return ret;
1497}
1498
1499/**
1500 * i915_driver_cleanup_hw - cleanup the setup done in i915_driver_init_hw()
1501 * @dev_priv: device private
1502 */
1503static void i915_driver_cleanup_hw(struct drm_i915_private *dev_priv)
1504{
1505 struct drm_device *dev = dev_priv->dev;
1506 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1507
1508 if (dev->pdev->msi_enabled)
1509 pci_disable_msi(dev->pdev);
1510
1511 pm_qos_remove_request(&dev_priv->pm_qos);
1512 arch_phys_wc_del(ggtt->mtrr);
1513 io_mapping_free(ggtt->mappable);
1514 i915_ggtt_cleanup_hw(dev);
1515}
1516
1517/**
1518 * i915_driver_register - register the driver with the rest of the system
1519 * @dev_priv: device private
1520 *
1521 * Perform any steps necessary to make the driver available via kernel
1522 * internal or userspace interfaces.
1523 */
1524static void i915_driver_register(struct drm_i915_private *dev_priv)
1525{
1526 struct drm_device *dev = dev_priv->dev;
1527
1528 i915_gem_shrinker_init(dev_priv);
1529
1530 /*
1531 * Notify a valid surface after modesetting,
1532 * when running inside a VM.
1533 */
1534 if (intel_vgpu_active(dev_priv))
1535 I915_WRITE(vgtif_reg(display_ready), VGT_DRV_DISPLAY_READY);
1536
1537 /* Reveal our presence to userspace */
1538 if (drm_dev_register(dev, 0) == 0) {
1539 i915_debugfs_register(dev_priv);
1540 i915_setup_sysfs(dev);
1541 } else
1542 DRM_ERROR("Failed to register driver for userspace access!\n");
1543
1544 if (INTEL_INFO(dev_priv)->num_pipes) {
1545 /* Must be done after probing outputs */
1546 intel_opregion_register(dev_priv);
1547 acpi_video_register();
1548 }
1549
1550 if (IS_GEN5(dev_priv))
1551 intel_gpu_ips_init(dev_priv);
1552
1553 i915_audio_component_init(dev_priv);
1554
1555 /*
1556 * Some ports require correctly set-up hpd registers for detection to
1557 * work properly (leading to ghost connected connector status), e.g. VGA
1558 * on gm45. Hence we can only set up the initial fbdev config after hpd
1559 * irqs are fully enabled. We do it last so that the async config
1560 * cannot run before the connectors are registered.
1561 */
1562 intel_fbdev_initial_config_async(dev);
1563}
1564
1565/**
1566 * i915_driver_unregister - cleanup the registration done in i915_driver_regiser()
1567 * @dev_priv: device private
1568 */
1569static void i915_driver_unregister(struct drm_i915_private *dev_priv)
1570{
1571 i915_audio_component_cleanup(dev_priv);
1572
1573 intel_gpu_ips_teardown();
1574 acpi_video_unregister();
1575 intel_opregion_unregister(dev_priv);
1576
1577 i915_teardown_sysfs(dev_priv->dev);
1578 i915_debugfs_unregister(dev_priv);
1579 drm_dev_unregister(dev_priv->dev);
1580
1581 i915_gem_shrinker_cleanup(dev_priv);
1582}
1583
1584/**
1585 * i915_driver_load - setup chip and create an initial config
1586 * @dev: DRM device
1587 * @flags: startup flags
1588 *
1589 * The driver load routine has to do several things:
1590 * - drive output discovery via intel_modeset_init()
1591 * - initialize the memory manager
1592 * - allocate initial config memory
1593 * - setup the DRM framebuffer with the allocated memory
1594 */
Chris Wilson42f55512016-06-24 14:00:26 +01001595int i915_driver_load(struct pci_dev *pdev, const struct pci_device_id *ent)
Chris Wilson0673ad42016-06-24 14:00:22 +01001596{
1597 struct drm_i915_private *dev_priv;
1598 int ret;
1599
Chris Wilsona09d0ba2016-06-24 14:00:27 +01001600 if (i915.nuclear_pageflip)
1601 driver.driver_features |= DRIVER_ATOMIC;
1602
Chris Wilson0673ad42016-06-24 14:00:22 +01001603 ret = -ENOMEM;
1604 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
1605 if (dev_priv)
1606 ret = drm_dev_init(&dev_priv->drm, &driver, &pdev->dev);
1607 if (ret) {
1608 dev_printk(KERN_ERR, &pdev->dev,
1609 "[" DRM_NAME ":%s] allocation failed\n", __func__);
1610 kfree(dev_priv);
1611 return ret;
1612 }
1613
1614 /* Must be set before calling __i915_printk */
1615 dev_priv->drm.pdev = pdev;
1616 dev_priv->drm.dev_private = dev_priv;
1617 dev_priv->dev = &dev_priv->drm;
1618
1619 ret = pci_enable_device(pdev);
1620 if (ret)
1621 goto out_free_priv;
1622
1623 pci_set_drvdata(pdev, &dev_priv->drm);
1624
1625 ret = i915_driver_init_early(dev_priv, ent);
1626 if (ret < 0)
1627 goto out_pci_disable;
1628
1629 intel_runtime_pm_get(dev_priv);
1630
1631 ret = i915_driver_init_mmio(dev_priv);
1632 if (ret < 0)
1633 goto out_runtime_pm_put;
1634
1635 ret = i915_driver_init_hw(dev_priv);
1636 if (ret < 0)
1637 goto out_cleanup_mmio;
1638
1639 /*
1640 * TODO: move the vblank init and parts of modeset init steps into one
1641 * of the i915_driver_init_/i915_driver_register functions according
1642 * to the role/effect of the given init step.
1643 */
1644 if (INTEL_INFO(dev_priv)->num_pipes) {
1645 ret = drm_vblank_init(dev_priv->dev,
1646 INTEL_INFO(dev_priv)->num_pipes);
1647 if (ret)
1648 goto out_cleanup_hw;
1649 }
1650
1651 ret = i915_load_modeset_init(dev_priv->dev);
1652 if (ret < 0)
1653 goto out_cleanup_vblank;
1654
1655 i915_driver_register(dev_priv);
1656
1657 intel_runtime_pm_enable(dev_priv);
1658
1659 intel_runtime_pm_put(dev_priv);
1660
1661 return 0;
1662
1663out_cleanup_vblank:
1664 drm_vblank_cleanup(dev_priv->dev);
1665out_cleanup_hw:
1666 i915_driver_cleanup_hw(dev_priv);
1667out_cleanup_mmio:
1668 i915_driver_cleanup_mmio(dev_priv);
1669out_runtime_pm_put:
1670 intel_runtime_pm_put(dev_priv);
1671 i915_driver_cleanup_early(dev_priv);
1672out_pci_disable:
1673 pci_disable_device(pdev);
1674out_free_priv:
1675 i915_load_error(dev_priv, "Device initialization failed (%d)\n", ret);
1676 drm_dev_unref(&dev_priv->drm);
1677 return ret;
1678}
1679
Chris Wilson42f55512016-06-24 14:00:26 +01001680void i915_driver_unload(struct drm_device *dev)
Chris Wilson0673ad42016-06-24 14:00:22 +01001681{
1682 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson0673ad42016-06-24 14:00:22 +01001683
1684 intel_fbdev_fini(dev);
1685
Chris Wilson42f55512016-06-24 14:00:26 +01001686 if (i915_gem_suspend(dev))
1687 DRM_ERROR("failed to idle hardware; continuing to unload!\n");
Chris Wilson0673ad42016-06-24 14:00:22 +01001688
1689 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
1690
1691 i915_driver_unregister(dev_priv);
1692
1693 drm_vblank_cleanup(dev);
1694
1695 intel_modeset_cleanup(dev);
1696
1697 /*
1698 * free the memory space allocated for the child device
1699 * config parsed from VBT
1700 */
1701 if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
1702 kfree(dev_priv->vbt.child_dev);
1703 dev_priv->vbt.child_dev = NULL;
1704 dev_priv->vbt.child_dev_num = 0;
1705 }
1706 kfree(dev_priv->vbt.sdvo_lvds_vbt_mode);
1707 dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
1708 kfree(dev_priv->vbt.lfp_lvds_vbt_mode);
1709 dev_priv->vbt.lfp_lvds_vbt_mode = NULL;
1710
1711 vga_switcheroo_unregister_client(dev->pdev);
1712 vga_client_register(dev->pdev, NULL, NULL, NULL);
1713
1714 intel_csr_ucode_fini(dev_priv);
1715
1716 /* Free error state after interrupts are fully disabled. */
1717 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
1718 i915_destroy_error_state(dev);
1719
1720 /* Flush any outstanding unpin_work. */
1721 flush_workqueue(dev_priv->wq);
1722
1723 intel_guc_fini(dev);
1724 i915_gem_fini(dev);
1725 intel_fbc_cleanup_cfb(dev_priv);
1726
1727 intel_power_domains_fini(dev_priv);
1728
1729 i915_driver_cleanup_hw(dev_priv);
1730 i915_driver_cleanup_mmio(dev_priv);
1731
1732 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
1733
1734 i915_driver_cleanup_early(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001735}
1736
1737static int i915_driver_open(struct drm_device *dev, struct drm_file *file)
1738{
1739 int ret;
1740
1741 ret = i915_gem_open(dev, file);
1742 if (ret)
1743 return ret;
1744
1745 return 0;
1746}
1747
1748/**
1749 * i915_driver_lastclose - clean up after all DRM clients have exited
1750 * @dev: DRM device
1751 *
1752 * Take care of cleaning up after all DRM clients have exited. In the
1753 * mode setting case, we want to restore the kernel's initial mode (just
1754 * in case the last client left us in a bad state).
1755 *
1756 * Additionally, in the non-mode setting case, we'll tear down the GTT
1757 * and DMA structures, since the kernel won't be using them, and clea
1758 * up any GEM state.
1759 */
1760static void i915_driver_lastclose(struct drm_device *dev)
1761{
1762 intel_fbdev_restore_mode(dev);
1763 vga_switcheroo_process_delayed_switch();
1764}
1765
1766static void i915_driver_preclose(struct drm_device *dev, struct drm_file *file)
1767{
1768 mutex_lock(&dev->struct_mutex);
1769 i915_gem_context_close(dev, file);
1770 i915_gem_release(dev, file);
1771 mutex_unlock(&dev->struct_mutex);
1772}
1773
1774static void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
1775{
1776 struct drm_i915_file_private *file_priv = file->driver_priv;
1777
1778 kfree(file_priv);
1779}
1780
Imre Deak07f9cd02014-08-18 14:42:45 +03001781static void intel_suspend_encoders(struct drm_i915_private *dev_priv)
1782{
1783 struct drm_device *dev = dev_priv->dev;
Jani Nikula19c80542015-12-16 12:48:16 +02001784 struct intel_encoder *encoder;
Imre Deak07f9cd02014-08-18 14:42:45 +03001785
1786 drm_modeset_lock_all(dev);
Jani Nikula19c80542015-12-16 12:48:16 +02001787 for_each_intel_encoder(dev, encoder)
1788 if (encoder->suspend)
1789 encoder->suspend(encoder);
Imre Deak07f9cd02014-08-18 14:42:45 +03001790 drm_modeset_unlock_all(dev);
1791}
1792
Paulo Zanoni1a5df182014-10-27 17:54:32 -02001793static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
1794 bool rpm_resume);
Imre Deak507e1262016-04-20 20:27:54 +03001795static int vlv_suspend_complete(struct drm_i915_private *dev_priv);
Suketu Shahf75a1982015-04-16 14:22:11 +05301796
Imre Deakbc872292015-11-18 17:32:30 +02001797static bool suspend_to_idle(struct drm_i915_private *dev_priv)
1798{
1799#if IS_ENABLED(CONFIG_ACPI_SLEEP)
1800 if (acpi_target_system_state() < ACPI_STATE_S3)
1801 return true;
1802#endif
1803 return false;
1804}
Sagar Kambleebc32822014-08-13 23:07:05 +05301805
Imre Deak5e365c32014-10-23 19:23:25 +03001806static int i915_drm_suspend(struct drm_device *dev)
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001807{
Rafael J. Wysocki61caf872010-02-18 23:06:27 +01001808 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnese5747e32014-06-12 08:35:47 -07001809 pci_power_t opregion_target_state;
Daniel Vetterd5818932015-02-23 12:03:26 +01001810 int error;
Rafael J. Wysocki61caf872010-02-18 23:06:27 +01001811
Zhang Ruib8efb172013-02-05 15:41:53 +08001812 /* ignore lid events during suspend */
1813 mutex_lock(&dev_priv->modeset_restore_lock);
1814 dev_priv->modeset_restore = MODESET_SUSPENDED;
1815 mutex_unlock(&dev_priv->modeset_restore_lock);
1816
Imre Deak1f814da2015-12-16 02:52:19 +02001817 disable_rpm_wakeref_asserts(dev_priv);
1818
Paulo Zanonic67a4702013-08-19 13:18:09 -03001819 /* We do a lot of poking in a lot of registers, make sure they work
1820 * properly. */
Imre Deakda7e29b2014-02-18 00:02:02 +02001821 intel_display_set_init_power(dev_priv, true);
Paulo Zanonicb107992013-01-25 16:59:15 -02001822
Dave Airlie5bcf7192010-12-07 09:20:40 +10001823 drm_kms_helper_poll_disable(dev);
1824
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001825 pci_save_state(dev->pdev);
1826
Daniel Vetterd5818932015-02-23 12:03:26 +01001827 error = i915_gem_suspend(dev);
1828 if (error) {
1829 dev_err(&dev->pdev->dev,
1830 "GEM idle failed, resume might fail\n");
Imre Deak1f814da2015-12-16 02:52:19 +02001831 goto out;
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001832 }
1833
Alex Daia1c41992015-09-30 09:46:37 -07001834 intel_guc_suspend(dev);
1835
Chris Wilsondc979972016-05-10 14:10:04 +01001836 intel_suspend_gt_powersave(dev_priv);
Daniel Vetterd5818932015-02-23 12:03:26 +01001837
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02001838 intel_display_suspend(dev);
Daniel Vetterd5818932015-02-23 12:03:26 +01001839
1840 intel_dp_mst_suspend(dev);
1841
1842 intel_runtime_pm_disable_interrupts(dev_priv);
1843 intel_hpd_cancel_work(dev_priv);
1844
1845 intel_suspend_encoders(dev_priv);
1846
1847 intel_suspend_hw(dev);
1848
Ben Widawsky828c7902013-10-16 09:21:30 -07001849 i915_gem_suspend_gtt_mappings(dev);
1850
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001851 i915_save_state(dev);
1852
Imre Deakbc872292015-11-18 17:32:30 +02001853 opregion_target_state = suspend_to_idle(dev_priv) ? PCI_D1 : PCI_D3cold;
Chris Wilson6f9f4b72016-05-23 15:08:09 +01001854 intel_opregion_notify_adapter(dev_priv, opregion_target_state);
Jesse Barnese5747e32014-06-12 08:35:47 -07001855
Chris Wilsondc979972016-05-10 14:10:04 +01001856 intel_uncore_forcewake_reset(dev_priv, false);
Chris Wilson03d92e42016-05-23 15:08:10 +01001857 intel_opregion_unregister(dev_priv);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001858
Chris Wilson82e3b8c2014-08-13 13:09:46 +01001859 intel_fbdev_set_suspend(dev, FBINFO_STATE_SUSPENDED, true);
Dave Airlie3fa016a2012-03-28 10:48:49 +01001860
Mika Kuoppala62d5d692014-02-25 17:11:28 +02001861 dev_priv->suspend_count++;
1862
Kristen Carlson Accardi85e90672014-06-12 08:35:44 -07001863 intel_display_set_init_power(dev_priv, false);
1864
Imre Deakf74ed082016-04-18 14:48:21 +03001865 intel_csr_ucode_suspend(dev_priv);
Imre Deakf514c2d2015-10-28 23:59:06 +02001866
Imre Deak1f814da2015-12-16 02:52:19 +02001867out:
1868 enable_rpm_wakeref_asserts(dev_priv);
1869
1870 return error;
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001871}
1872
Imre Deakab3be732015-03-02 13:04:41 +02001873static int i915_drm_suspend_late(struct drm_device *drm_dev, bool hibernation)
Imre Deakc3c09c92014-10-23 19:23:15 +03001874{
1875 struct drm_i915_private *dev_priv = drm_dev->dev_private;
Imre Deakbc872292015-11-18 17:32:30 +02001876 bool fw_csr;
Imre Deakc3c09c92014-10-23 19:23:15 +03001877 int ret;
1878
Imre Deak1f814da2015-12-16 02:52:19 +02001879 disable_rpm_wakeref_asserts(dev_priv);
1880
Imre Deaka7c81252016-04-01 16:02:38 +03001881 fw_csr = !IS_BROXTON(dev_priv) &&
1882 suspend_to_idle(dev_priv) && dev_priv->csr.dmc_payload;
Imre Deakbc872292015-11-18 17:32:30 +02001883 /*
1884 * In case of firmware assisted context save/restore don't manually
1885 * deinit the power domains. This also means the CSR/DMC firmware will
1886 * stay active, it will power down any HW resources as required and
1887 * also enable deeper system power states that would be blocked if the
1888 * firmware was inactive.
1889 */
1890 if (!fw_csr)
1891 intel_power_domains_suspend(dev_priv);
Imre Deak73dfc222015-11-17 17:33:53 +02001892
Imre Deak507e1262016-04-20 20:27:54 +03001893 ret = 0;
Imre Deakb8aea3d12016-04-20 20:27:55 +03001894 if (IS_BROXTON(dev_priv))
Imre Deak507e1262016-04-20 20:27:54 +03001895 bxt_enable_dc9(dev_priv);
Imre Deakb8aea3d12016-04-20 20:27:55 +03001896 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Imre Deak507e1262016-04-20 20:27:54 +03001897 hsw_enable_pc8(dev_priv);
1898 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
1899 ret = vlv_suspend_complete(dev_priv);
Imre Deakc3c09c92014-10-23 19:23:15 +03001900
1901 if (ret) {
1902 DRM_ERROR("Suspend complete failed: %d\n", ret);
Imre Deakbc872292015-11-18 17:32:30 +02001903 if (!fw_csr)
1904 intel_power_domains_init_hw(dev_priv, true);
Imre Deakc3c09c92014-10-23 19:23:15 +03001905
Imre Deak1f814da2015-12-16 02:52:19 +02001906 goto out;
Imre Deakc3c09c92014-10-23 19:23:15 +03001907 }
1908
1909 pci_disable_device(drm_dev->pdev);
Imre Deakab3be732015-03-02 13:04:41 +02001910 /*
Imre Deak54875572015-06-30 17:06:47 +03001911 * During hibernation on some platforms the BIOS may try to access
Imre Deakab3be732015-03-02 13:04:41 +02001912 * the device even though it's already in D3 and hang the machine. So
1913 * leave the device in D0 on those platforms and hope the BIOS will
Imre Deak54875572015-06-30 17:06:47 +03001914 * power down the device properly. The issue was seen on multiple old
1915 * GENs with different BIOS vendors, so having an explicit blacklist
1916 * is inpractical; apply the workaround on everything pre GEN6. The
1917 * platforms where the issue was seen:
1918 * Lenovo Thinkpad X301, X61s, X60, T60, X41
1919 * Fujitsu FSC S7110
1920 * Acer Aspire 1830T
Imre Deakab3be732015-03-02 13:04:41 +02001921 */
Imre Deak54875572015-06-30 17:06:47 +03001922 if (!(hibernation && INTEL_INFO(dev_priv)->gen < 6))
Imre Deakab3be732015-03-02 13:04:41 +02001923 pci_set_power_state(drm_dev->pdev, PCI_D3hot);
Imre Deakc3c09c92014-10-23 19:23:15 +03001924
Imre Deakbc872292015-11-18 17:32:30 +02001925 dev_priv->suspended_to_idle = suspend_to_idle(dev_priv);
1926
Imre Deak1f814da2015-12-16 02:52:19 +02001927out:
1928 enable_rpm_wakeref_asserts(dev_priv);
1929
1930 return ret;
Imre Deakc3c09c92014-10-23 19:23:15 +03001931}
1932
Maarten Lankhorst1751fcf2015-08-27 15:15:15 +02001933int i915_suspend_switcheroo(struct drm_device *dev, pm_message_t state)
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001934{
1935 int error;
1936
1937 if (!dev || !dev->dev_private) {
1938 DRM_ERROR("dev: %p\n", dev);
Keith Packard1ae8c0a2009-06-28 15:42:17 -07001939 DRM_ERROR("DRM not initialized, aborting suspend.\n");
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001940 return -ENODEV;
1941 }
1942
Imre Deak0b14cbd2014-09-10 18:16:55 +03001943 if (WARN_ON_ONCE(state.event != PM_EVENT_SUSPEND &&
1944 state.event != PM_EVENT_FREEZE))
1945 return -EINVAL;
Dave Airlie5bcf7192010-12-07 09:20:40 +10001946
1947 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1948 return 0;
Chris Wilson6eecba32010-09-08 09:45:11 +01001949
Imre Deak5e365c32014-10-23 19:23:25 +03001950 error = i915_drm_suspend(dev);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001951 if (error)
1952 return error;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001953
Imre Deakab3be732015-03-02 13:04:41 +02001954 return i915_drm_suspend_late(dev, false);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001955}
1956
Imre Deak5e365c32014-10-23 19:23:25 +03001957static int i915_drm_resume(struct drm_device *dev)
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001958{
Jesse Barnes5669fca2009-02-17 15:13:31 -08001959 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläac840ae2016-05-06 21:35:55 +03001960 int ret;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001961
Imre Deak1f814da2015-12-16 02:52:19 +02001962 disable_rpm_wakeref_asserts(dev_priv);
1963
Ville Syrjäläac840ae2016-05-06 21:35:55 +03001964 ret = i915_ggtt_enable_hw(dev);
1965 if (ret)
1966 DRM_ERROR("failed to re-enable GGTT\n");
1967
Imre Deakf74ed082016-04-18 14:48:21 +03001968 intel_csr_ucode_resume(dev_priv);
1969
Daniel Vetterd5818932015-02-23 12:03:26 +01001970 mutex_lock(&dev->struct_mutex);
1971 i915_gem_restore_gtt_mappings(dev);
1972 mutex_unlock(&dev->struct_mutex);
Paulo Zanoni9d49c0e2013-09-12 18:06:43 -03001973
Rafael J. Wysocki61caf872010-02-18 23:06:27 +01001974 i915_restore_state(dev);
Chris Wilson6f9f4b72016-05-23 15:08:09 +01001975 intel_opregion_setup(dev_priv);
Rafael J. Wysocki61caf872010-02-18 23:06:27 +01001976
Daniel Vetterd5818932015-02-23 12:03:26 +01001977 intel_init_pch_refclk(dev);
1978 drm_mode_config_reset(dev);
Chris Wilson1833b132012-05-09 11:56:28 +01001979
Peter Antoine364aece2015-05-11 08:50:45 +01001980 /*
1981 * Interrupts have to be enabled before any batches are run. If not the
1982 * GPU will hang. i915_gem_init_hw() will initiate batches to
1983 * update/restore the context.
1984 *
1985 * Modeset enabling in intel_modeset_init_hw() also needs working
1986 * interrupts.
1987 */
1988 intel_runtime_pm_enable_interrupts(dev_priv);
1989
Daniel Vetterd5818932015-02-23 12:03:26 +01001990 mutex_lock(&dev->struct_mutex);
1991 if (i915_gem_init_hw(dev)) {
1992 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001993 atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08001994 }
Daniel Vetterd5818932015-02-23 12:03:26 +01001995 mutex_unlock(&dev->struct_mutex);
1996
Alex Daia1c41992015-09-30 09:46:37 -07001997 intel_guc_resume(dev);
1998
Daniel Vetterd5818932015-02-23 12:03:26 +01001999 intel_modeset_init_hw(dev);
2000
2001 spin_lock_irq(&dev_priv->irq_lock);
2002 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01002003 dev_priv->display.hpd_irq_setup(dev_priv);
Daniel Vetterd5818932015-02-23 12:03:26 +01002004 spin_unlock_irq(&dev_priv->irq_lock);
2005
Daniel Vetterd5818932015-02-23 12:03:26 +01002006 intel_dp_mst_resume(dev);
2007
Lyudea16b7652016-03-11 10:57:01 -05002008 intel_display_resume(dev);
2009
Daniel Vetterd5818932015-02-23 12:03:26 +01002010 /*
2011 * ... but also need to make sure that hotplug processing
2012 * doesn't cause havoc. Like in the driver load code we don't
2013 * bother with the tiny race here where we might loose hotplug
2014 * notifications.
2015 * */
2016 intel_hpd_init(dev_priv);
2017 /* Config may have changed between suspend and resume */
2018 drm_helper_hpd_irq_event(dev);
Jesse Barnes1daed3f2011-01-05 12:01:25 -08002019
Chris Wilson03d92e42016-05-23 15:08:10 +01002020 intel_opregion_register(dev_priv);
Chris Wilson44834a62010-08-19 16:09:23 +01002021
Chris Wilson82e3b8c2014-08-13 13:09:46 +01002022 intel_fbdev_set_suspend(dev, FBINFO_STATE_RUNNING, false);
Jesse Barnes073f34d2012-11-02 11:13:59 -07002023
Zhang Ruib8efb172013-02-05 15:41:53 +08002024 mutex_lock(&dev_priv->modeset_restore_lock);
2025 dev_priv->modeset_restore = MODESET_DONE;
2026 mutex_unlock(&dev_priv->modeset_restore_lock);
Paulo Zanoni8a187452013-12-06 20:32:13 -02002027
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002028 intel_opregion_notify_adapter(dev_priv, PCI_D0);
Jesse Barnese5747e32014-06-12 08:35:47 -07002029
Imre Deakee6f2802014-10-23 19:23:22 +03002030 drm_kms_helper_poll_enable(dev);
2031
Imre Deak1f814da2015-12-16 02:52:19 +02002032 enable_rpm_wakeref_asserts(dev_priv);
2033
Chris Wilson074c6ad2014-04-09 09:19:43 +01002034 return 0;
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01002035}
2036
Imre Deak5e365c32014-10-23 19:23:25 +03002037static int i915_drm_resume_early(struct drm_device *dev)
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01002038{
Imre Deak36d61e62014-10-23 19:23:24 +03002039 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak44410cd2016-04-18 14:45:54 +03002040 int ret;
Imre Deak36d61e62014-10-23 19:23:24 +03002041
Imre Deak76c4b252014-04-01 19:55:22 +03002042 /*
2043 * We have a resume ordering issue with the snd-hda driver also
2044 * requiring our device to be power up. Due to the lack of a
2045 * parent/child relationship we currently solve this with an early
2046 * resume hook.
2047 *
2048 * FIXME: This should be solved with a special hdmi sink device or
2049 * similar so that power domains can be employed.
2050 */
Imre Deak44410cd2016-04-18 14:45:54 +03002051
2052 /*
2053 * Note that we need to set the power state explicitly, since we
2054 * powered off the device during freeze and the PCI core won't power
2055 * it back up for us during thaw. Powering off the device during
2056 * freeze is not a hard requirement though, and during the
2057 * suspend/resume phases the PCI core makes sure we get here with the
2058 * device powered on. So in case we change our freeze logic and keep
2059 * the device powered we can also remove the following set power state
2060 * call.
2061 */
2062 ret = pci_set_power_state(dev->pdev, PCI_D0);
2063 if (ret) {
2064 DRM_ERROR("failed to set PCI D0 power state (%d)\n", ret);
2065 goto out;
2066 }
2067
2068 /*
2069 * Note that pci_enable_device() first enables any parent bridge
2070 * device and only then sets the power state for this device. The
2071 * bridge enabling is a nop though, since bridge devices are resumed
2072 * first. The order of enabling power and enabling the device is
2073 * imposed by the PCI core as described above, so here we preserve the
2074 * same order for the freeze/thaw phases.
2075 *
2076 * TODO: eventually we should remove pci_disable_device() /
2077 * pci_enable_enable_device() from suspend/resume. Due to how they
2078 * depend on the device enable refcount we can't anyway depend on them
2079 * disabling/enabling the device.
2080 */
Imre Deakbc872292015-11-18 17:32:30 +02002081 if (pci_enable_device(dev->pdev)) {
2082 ret = -EIO;
2083 goto out;
2084 }
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01002085
2086 pci_set_master(dev->pdev);
2087
Imre Deak1f814da2015-12-16 02:52:19 +02002088 disable_rpm_wakeref_asserts(dev_priv);
2089
Wayne Boyer666a4532015-12-09 12:29:35 -08002090 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002091 ret = vlv_resume_prepare(dev_priv, false);
Imre Deak36d61e62014-10-23 19:23:24 +03002092 if (ret)
Damien Lespiauff0b1872015-05-20 14:45:15 +01002093 DRM_ERROR("Resume prepare failed: %d, continuing anyway\n",
2094 ret);
Imre Deak36d61e62014-10-23 19:23:24 +03002095
Chris Wilsondc979972016-05-10 14:10:04 +01002096 intel_uncore_early_sanitize(dev_priv, true);
Paulo Zanoniefee8332014-10-27 17:54:33 -02002097
Chris Wilsondc979972016-05-10 14:10:04 +01002098 if (IS_BROXTON(dev_priv)) {
Imre Deakda2f41d2016-04-20 20:27:56 +03002099 if (!dev_priv->suspended_to_idle)
2100 gen9_sanitize_dc_state(dev_priv);
Imre Deak507e1262016-04-20 20:27:54 +03002101 bxt_disable_dc9(dev_priv);
Imre Deakda2f41d2016-04-20 20:27:56 +03002102 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Damien Lespiaua9a6b732015-05-20 14:45:14 +01002103 hsw_disable_pc8(dev_priv);
Imre Deakda2f41d2016-04-20 20:27:56 +03002104 }
Paulo Zanoniefee8332014-10-27 17:54:33 -02002105
Chris Wilsondc979972016-05-10 14:10:04 +01002106 intel_uncore_sanitize(dev_priv);
Imre Deakbc872292015-11-18 17:32:30 +02002107
Imre Deaka7c81252016-04-01 16:02:38 +03002108 if (IS_BROXTON(dev_priv) ||
2109 !(dev_priv->suspended_to_idle && dev_priv->csr.dmc_payload))
Imre Deakbc872292015-11-18 17:32:30 +02002110 intel_power_domains_init_hw(dev_priv, true);
2111
Imre Deak6e35e8a2016-04-18 10:04:19 +03002112 enable_rpm_wakeref_asserts(dev_priv);
2113
Imre Deakbc872292015-11-18 17:32:30 +02002114out:
2115 dev_priv->suspended_to_idle = false;
Imre Deak36d61e62014-10-23 19:23:24 +03002116
2117 return ret;
Imre Deak76c4b252014-04-01 19:55:22 +03002118}
2119
Maarten Lankhorst1751fcf2015-08-27 15:15:15 +02002120int i915_resume_switcheroo(struct drm_device *dev)
Imre Deak76c4b252014-04-01 19:55:22 +03002121{
Imre Deak50a00722014-10-23 19:23:17 +03002122 int ret;
Imre Deak76c4b252014-04-01 19:55:22 +03002123
Imre Deak097dd832014-10-23 19:23:19 +03002124 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2125 return 0;
2126
Imre Deak5e365c32014-10-23 19:23:25 +03002127 ret = i915_drm_resume_early(dev);
Imre Deak50a00722014-10-23 19:23:17 +03002128 if (ret)
2129 return ret;
2130
Imre Deak5a175142014-10-23 19:23:18 +03002131 return i915_drm_resume(dev);
2132}
2133
Ben Gamari11ed50e2009-09-14 17:48:45 -04002134/**
Eugeni Dodonovf3953dc2011-11-28 16:15:17 -02002135 * i915_reset - reset chip after a hang
Ben Gamari11ed50e2009-09-14 17:48:45 -04002136 * @dev: drm device to reset
Ben Gamari11ed50e2009-09-14 17:48:45 -04002137 *
2138 * Reset the chip. Useful if a hang is detected. Returns zero on successful
2139 * reset or otherwise an error code.
2140 *
2141 * Procedure is fairly simple:
2142 * - reset the chip using the reset reg
2143 * - re-init context state
2144 * - re-init hardware status page
2145 * - re-init ring buffer
2146 * - re-init interrupt state
2147 * - re-init display
2148 */
Chris Wilsonc0336662016-05-06 15:40:21 +01002149int i915_reset(struct drm_i915_private *dev_priv)
Ben Gamari11ed50e2009-09-14 17:48:45 -04002150{
Chris Wilsonc0336662016-05-06 15:40:21 +01002151 struct drm_device *dev = dev_priv->dev;
Chris Wilsond98c52c2016-04-13 17:35:05 +01002152 struct i915_gpu_error *error = &dev_priv->gpu_error;
2153 unsigned reset_counter;
Kenneth Graunke0573ed42010-09-11 03:17:19 -07002154 int ret;
Ben Gamari11ed50e2009-09-14 17:48:45 -04002155
Chris Wilsondc979972016-05-10 14:10:04 +01002156 intel_reset_gt_powersave(dev_priv);
Imre Deakdbea3ce2014-12-15 18:59:28 +02002157
Daniel Vetterd54a02c2012-07-04 22:18:39 +02002158 mutex_lock(&dev->struct_mutex);
Ben Gamari11ed50e2009-09-14 17:48:45 -04002159
Chris Wilsond98c52c2016-04-13 17:35:05 +01002160 /* Clear any previous failed attempts at recovery. Time to try again. */
2161 atomic_andnot(I915_WEDGED, &error->reset_counter);
Ben Gamari11ed50e2009-09-14 17:48:45 -04002162
Chris Wilsond98c52c2016-04-13 17:35:05 +01002163 /* Clear the reset-in-progress flag and increment the reset epoch. */
2164 reset_counter = atomic_inc_return(&error->reset_counter);
2165 if (WARN_ON(__i915_reset_in_progress(reset_counter))) {
2166 ret = -EIO;
2167 goto error;
2168 }
2169
2170 i915_gem_reset(dev);
Chris Wilson2e7c8ee2013-05-28 10:38:44 +01002171
Chris Wilsondc979972016-05-10 14:10:04 +01002172 ret = intel_gpu_reset(dev_priv, ALL_ENGINES);
Daniel Vetter350d2702012-04-27 15:17:42 +02002173
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002174 /* Also reset the gpu hangman. */
Chris Wilsond98c52c2016-04-13 17:35:05 +01002175 if (error->stop_rings != 0) {
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002176 DRM_INFO("Simulated gpu hang, resetting stop_rings\n");
Chris Wilsond98c52c2016-04-13 17:35:05 +01002177 error->stop_rings = 0;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002178 if (ret == -ENODEV) {
Daniel Vetterf2d91a22013-11-07 09:48:57 +01002179 DRM_INFO("Reset not implemented, but ignoring "
2180 "error for simulated gpu hangs\n");
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002181 ret = 0;
2182 }
Chris Wilson2e7c8ee2013-05-28 10:38:44 +01002183 }
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002184
Daniel Vetterd8f27162014-10-01 01:02:04 +02002185 if (i915_stop_ring_allow_warn(dev_priv))
2186 pr_notice("drm/i915: Resetting chip after gpu hang\n");
2187
Kenneth Graunke0573ed42010-09-11 03:17:19 -07002188 if (ret) {
Chris Wilson804e59a2016-04-13 17:35:09 +01002189 if (ret != -ENODEV)
2190 DRM_ERROR("Failed to reset chip: %i\n", ret);
2191 else
2192 DRM_DEBUG_DRIVER("GPU reset disabled\n");
Chris Wilsond98c52c2016-04-13 17:35:05 +01002193 goto error;
Ben Gamari11ed50e2009-09-14 17:48:45 -04002194 }
2195
Ville Syrjälä1362b772014-11-26 17:07:29 +02002196 intel_overlay_reset(dev_priv);
2197
Ben Gamari11ed50e2009-09-14 17:48:45 -04002198 /* Ok, now get things going again... */
2199
2200 /*
2201 * Everything depends on having the GTT running, so we need to start
2202 * there. Fortunately we don't need to do this unless we reset the
2203 * chip at a PCI level.
2204 *
2205 * Next we need to restore the context, but we don't use those
2206 * yet either...
2207 *
2208 * Ring buffer needs to be re-initialized in the KMS case, or if X
2209 * was running at the time of the reset (i.e. we weren't VT
2210 * switched away).
2211 */
Daniel Vetter33d30a92015-02-23 12:03:27 +01002212 ret = i915_gem_init_hw(dev);
Daniel Vetter33d30a92015-02-23 12:03:27 +01002213 if (ret) {
2214 DRM_ERROR("Failed hw init on reset %d\n", ret);
Chris Wilsond98c52c2016-04-13 17:35:05 +01002215 goto error;
Ben Gamari11ed50e2009-09-14 17:48:45 -04002216 }
2217
Chris Wilsond98c52c2016-04-13 17:35:05 +01002218 mutex_unlock(&dev->struct_mutex);
2219
Daniel Vetter33d30a92015-02-23 12:03:27 +01002220 /*
Daniel Vetter33d30a92015-02-23 12:03:27 +01002221 * rps/rc6 re-init is necessary to restore state lost after the
2222 * reset and the re-install of gt irqs. Skip for ironlake per
2223 * previous concerns that it doesn't respond well to some forms
2224 * of re-init after reset.
2225 */
2226 if (INTEL_INFO(dev)->gen > 5)
Chris Wilsondc979972016-05-10 14:10:04 +01002227 intel_enable_gt_powersave(dev_priv);
Daniel Vetter33d30a92015-02-23 12:03:27 +01002228
Ben Gamari11ed50e2009-09-14 17:48:45 -04002229 return 0;
Chris Wilsond98c52c2016-04-13 17:35:05 +01002230
2231error:
2232 atomic_or(I915_WEDGED, &error->reset_counter);
2233 mutex_unlock(&dev->struct_mutex);
2234 return ret;
Ben Gamari11ed50e2009-09-14 17:48:45 -04002235}
2236
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01002237static int i915_pm_suspend(struct device *dev)
Kristian Høgsberg112b7152009-01-04 16:55:33 -05002238{
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01002239 struct pci_dev *pdev = to_pci_dev(dev);
2240 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Kristian Høgsberg112b7152009-01-04 16:55:33 -05002241
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01002242 if (!drm_dev || !drm_dev->dev_private) {
2243 dev_err(dev, "DRM not initialized, aborting suspend.\n");
2244 return -ENODEV;
2245 }
Kristian Høgsberg112b7152009-01-04 16:55:33 -05002246
Dave Airlie5bcf7192010-12-07 09:20:40 +10002247 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2248 return 0;
2249
Imre Deak5e365c32014-10-23 19:23:25 +03002250 return i915_drm_suspend(drm_dev);
Imre Deak76c4b252014-04-01 19:55:22 +03002251}
2252
2253static int i915_pm_suspend_late(struct device *dev)
2254{
Imre Deak888d0d42015-01-08 17:54:13 +02002255 struct drm_device *drm_dev = dev_to_i915(dev)->dev;
Imre Deak76c4b252014-04-01 19:55:22 +03002256
2257 /*
Damien Lespiauc965d9952015-05-18 19:53:48 +01002258 * We have a suspend ordering issue with the snd-hda driver also
Imre Deak76c4b252014-04-01 19:55:22 +03002259 * requiring our device to be power up. Due to the lack of a
2260 * parent/child relationship we currently solve this with an late
2261 * suspend hook.
2262 *
2263 * FIXME: This should be solved with a special hdmi sink device or
2264 * similar so that power domains can be employed.
2265 */
2266 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2267 return 0;
Kristian Høgsberg112b7152009-01-04 16:55:33 -05002268
Imre Deakab3be732015-03-02 13:04:41 +02002269 return i915_drm_suspend_late(drm_dev, false);
2270}
2271
2272static int i915_pm_poweroff_late(struct device *dev)
2273{
2274 struct drm_device *drm_dev = dev_to_i915(dev)->dev;
2275
2276 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2277 return 0;
2278
2279 return i915_drm_suspend_late(drm_dev, true);
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002280}
2281
Imre Deak76c4b252014-04-01 19:55:22 +03002282static int i915_pm_resume_early(struct device *dev)
2283{
Imre Deak888d0d42015-01-08 17:54:13 +02002284 struct drm_device *drm_dev = dev_to_i915(dev)->dev;
Imre Deak76c4b252014-04-01 19:55:22 +03002285
Imre Deak097dd832014-10-23 19:23:19 +03002286 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2287 return 0;
2288
Imre Deak5e365c32014-10-23 19:23:25 +03002289 return i915_drm_resume_early(drm_dev);
Imre Deak76c4b252014-04-01 19:55:22 +03002290}
2291
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01002292static int i915_pm_resume(struct device *dev)
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002293{
Imre Deak888d0d42015-01-08 17:54:13 +02002294 struct drm_device *drm_dev = dev_to_i915(dev)->dev;
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01002295
Imre Deak097dd832014-10-23 19:23:19 +03002296 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2297 return 0;
2298
Imre Deak5a175142014-10-23 19:23:18 +03002299 return i915_drm_resume(drm_dev);
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002300}
2301
Chris Wilson1f19ac22016-05-14 07:26:32 +01002302/* freeze: before creating the hibernation_image */
2303static int i915_pm_freeze(struct device *dev)
2304{
2305 return i915_pm_suspend(dev);
2306}
2307
2308static int i915_pm_freeze_late(struct device *dev)
2309{
Chris Wilson461fb992016-05-14 07:26:33 +01002310 int ret;
2311
2312 ret = i915_pm_suspend_late(dev);
2313 if (ret)
2314 return ret;
2315
2316 ret = i915_gem_freeze_late(dev_to_i915(dev));
2317 if (ret)
2318 return ret;
2319
2320 return 0;
Chris Wilson1f19ac22016-05-14 07:26:32 +01002321}
2322
2323/* thaw: called after creating the hibernation image, but before turning off. */
2324static int i915_pm_thaw_early(struct device *dev)
2325{
2326 return i915_pm_resume_early(dev);
2327}
2328
2329static int i915_pm_thaw(struct device *dev)
2330{
2331 return i915_pm_resume(dev);
2332}
2333
2334/* restore: called after loading the hibernation image. */
2335static int i915_pm_restore_early(struct device *dev)
2336{
2337 return i915_pm_resume_early(dev);
2338}
2339
2340static int i915_pm_restore(struct device *dev)
2341{
2342 return i915_pm_resume(dev);
2343}
2344
Imre Deakddeea5b2014-05-05 15:19:56 +03002345/*
2346 * Save all Gunit registers that may be lost after a D3 and a subsequent
2347 * S0i[R123] transition. The list of registers needing a save/restore is
2348 * defined in the VLV2_S0IXRegs document. This documents marks all Gunit
2349 * registers in the following way:
2350 * - Driver: saved/restored by the driver
2351 * - Punit : saved/restored by the Punit firmware
2352 * - No, w/o marking: no need to save/restore, since the register is R/O or
2353 * used internally by the HW in a way that doesn't depend
2354 * keeping the content across a suspend/resume.
2355 * - Debug : used for debugging
2356 *
2357 * We save/restore all registers marked with 'Driver', with the following
2358 * exceptions:
2359 * - Registers out of use, including also registers marked with 'Debug'.
2360 * These have no effect on the driver's operation, so we don't save/restore
2361 * them to reduce the overhead.
2362 * - Registers that are fully setup by an initialization function called from
2363 * the resume path. For example many clock gating and RPS/RC6 registers.
2364 * - Registers that provide the right functionality with their reset defaults.
2365 *
2366 * TODO: Except for registers that based on the above 3 criteria can be safely
2367 * ignored, we save/restore all others, practically treating the HW context as
2368 * a black-box for the driver. Further investigation is needed to reduce the
2369 * saved/restored registers even further, by following the same 3 criteria.
2370 */
2371static void vlv_save_gunit_s0ix_state(struct drm_i915_private *dev_priv)
2372{
2373 struct vlv_s0ix_state *s = &dev_priv->vlv_s0ix_state;
2374 int i;
2375
2376 /* GAM 0x4000-0x4770 */
2377 s->wr_watermark = I915_READ(GEN7_WR_WATERMARK);
2378 s->gfx_prio_ctrl = I915_READ(GEN7_GFX_PRIO_CTRL);
2379 s->arb_mode = I915_READ(ARB_MODE);
2380 s->gfx_pend_tlb0 = I915_READ(GEN7_GFX_PEND_TLB0);
2381 s->gfx_pend_tlb1 = I915_READ(GEN7_GFX_PEND_TLB1);
2382
2383 for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002384 s->lra_limits[i] = I915_READ(GEN7_LRA_LIMITS(i));
Imre Deakddeea5b2014-05-05 15:19:56 +03002385
2386 s->media_max_req_count = I915_READ(GEN7_MEDIA_MAX_REQ_COUNT);
Imre Deakb5f1c972015-04-15 16:52:30 -07002387 s->gfx_max_req_count = I915_READ(GEN7_GFX_MAX_REQ_COUNT);
Imre Deakddeea5b2014-05-05 15:19:56 +03002388
2389 s->render_hwsp = I915_READ(RENDER_HWS_PGA_GEN7);
2390 s->ecochk = I915_READ(GAM_ECOCHK);
2391 s->bsd_hwsp = I915_READ(BSD_HWS_PGA_GEN7);
2392 s->blt_hwsp = I915_READ(BLT_HWS_PGA_GEN7);
2393
2394 s->tlb_rd_addr = I915_READ(GEN7_TLB_RD_ADDR);
2395
2396 /* MBC 0x9024-0x91D0, 0x8500 */
2397 s->g3dctl = I915_READ(VLV_G3DCTL);
2398 s->gsckgctl = I915_READ(VLV_GSCKGCTL);
2399 s->mbctl = I915_READ(GEN6_MBCTL);
2400
2401 /* GCP 0x9400-0x9424, 0x8100-0x810C */
2402 s->ucgctl1 = I915_READ(GEN6_UCGCTL1);
2403 s->ucgctl3 = I915_READ(GEN6_UCGCTL3);
2404 s->rcgctl1 = I915_READ(GEN6_RCGCTL1);
2405 s->rcgctl2 = I915_READ(GEN6_RCGCTL2);
2406 s->rstctl = I915_READ(GEN6_RSTCTL);
2407 s->misccpctl = I915_READ(GEN7_MISCCPCTL);
2408
2409 /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
2410 s->gfxpause = I915_READ(GEN6_GFXPAUSE);
2411 s->rpdeuhwtc = I915_READ(GEN6_RPDEUHWTC);
2412 s->rpdeuc = I915_READ(GEN6_RPDEUC);
2413 s->ecobus = I915_READ(ECOBUS);
2414 s->pwrdwnupctl = I915_READ(VLV_PWRDWNUPCTL);
2415 s->rp_down_timeout = I915_READ(GEN6_RP_DOWN_TIMEOUT);
2416 s->rp_deucsw = I915_READ(GEN6_RPDEUCSW);
2417 s->rcubmabdtmr = I915_READ(GEN6_RCUBMABDTMR);
2418 s->rcedata = I915_READ(VLV_RCEDATA);
2419 s->spare2gh = I915_READ(VLV_SPAREG2H);
2420
2421 /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
2422 s->gt_imr = I915_READ(GTIMR);
2423 s->gt_ier = I915_READ(GTIER);
2424 s->pm_imr = I915_READ(GEN6_PMIMR);
2425 s->pm_ier = I915_READ(GEN6_PMIER);
2426
2427 for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002428 s->gt_scratch[i] = I915_READ(GEN7_GT_SCRATCH(i));
Imre Deakddeea5b2014-05-05 15:19:56 +03002429
2430 /* GT SA CZ domain, 0x100000-0x138124 */
2431 s->tilectl = I915_READ(TILECTL);
2432 s->gt_fifoctl = I915_READ(GTFIFOCTL);
2433 s->gtlc_wake_ctrl = I915_READ(VLV_GTLC_WAKE_CTRL);
2434 s->gtlc_survive = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2435 s->pmwgicz = I915_READ(VLV_PMWGICZ);
2436
2437 /* Gunit-Display CZ domain, 0x182028-0x1821CF */
2438 s->gu_ctl0 = I915_READ(VLV_GU_CTL0);
2439 s->gu_ctl1 = I915_READ(VLV_GU_CTL1);
Jesse Barnes9c252102015-04-01 14:22:57 -07002440 s->pcbr = I915_READ(VLV_PCBR);
Imre Deakddeea5b2014-05-05 15:19:56 +03002441 s->clock_gate_dis2 = I915_READ(VLV_GUNIT_CLOCK_GATE2);
2442
2443 /*
2444 * Not saving any of:
2445 * DFT, 0x9800-0x9EC0
2446 * SARB, 0xB000-0xB1FC
2447 * GAC, 0x5208-0x524C, 0x14000-0x14C000
2448 * PCI CFG
2449 */
2450}
2451
2452static void vlv_restore_gunit_s0ix_state(struct drm_i915_private *dev_priv)
2453{
2454 struct vlv_s0ix_state *s = &dev_priv->vlv_s0ix_state;
2455 u32 val;
2456 int i;
2457
2458 /* GAM 0x4000-0x4770 */
2459 I915_WRITE(GEN7_WR_WATERMARK, s->wr_watermark);
2460 I915_WRITE(GEN7_GFX_PRIO_CTRL, s->gfx_prio_ctrl);
2461 I915_WRITE(ARB_MODE, s->arb_mode | (0xffff << 16));
2462 I915_WRITE(GEN7_GFX_PEND_TLB0, s->gfx_pend_tlb0);
2463 I915_WRITE(GEN7_GFX_PEND_TLB1, s->gfx_pend_tlb1);
2464
2465 for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002466 I915_WRITE(GEN7_LRA_LIMITS(i), s->lra_limits[i]);
Imre Deakddeea5b2014-05-05 15:19:56 +03002467
2468 I915_WRITE(GEN7_MEDIA_MAX_REQ_COUNT, s->media_max_req_count);
Imre Deakb5f1c972015-04-15 16:52:30 -07002469 I915_WRITE(GEN7_GFX_MAX_REQ_COUNT, s->gfx_max_req_count);
Imre Deakddeea5b2014-05-05 15:19:56 +03002470
2471 I915_WRITE(RENDER_HWS_PGA_GEN7, s->render_hwsp);
2472 I915_WRITE(GAM_ECOCHK, s->ecochk);
2473 I915_WRITE(BSD_HWS_PGA_GEN7, s->bsd_hwsp);
2474 I915_WRITE(BLT_HWS_PGA_GEN7, s->blt_hwsp);
2475
2476 I915_WRITE(GEN7_TLB_RD_ADDR, s->tlb_rd_addr);
2477
2478 /* MBC 0x9024-0x91D0, 0x8500 */
2479 I915_WRITE(VLV_G3DCTL, s->g3dctl);
2480 I915_WRITE(VLV_GSCKGCTL, s->gsckgctl);
2481 I915_WRITE(GEN6_MBCTL, s->mbctl);
2482
2483 /* GCP 0x9400-0x9424, 0x8100-0x810C */
2484 I915_WRITE(GEN6_UCGCTL1, s->ucgctl1);
2485 I915_WRITE(GEN6_UCGCTL3, s->ucgctl3);
2486 I915_WRITE(GEN6_RCGCTL1, s->rcgctl1);
2487 I915_WRITE(GEN6_RCGCTL2, s->rcgctl2);
2488 I915_WRITE(GEN6_RSTCTL, s->rstctl);
2489 I915_WRITE(GEN7_MISCCPCTL, s->misccpctl);
2490
2491 /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
2492 I915_WRITE(GEN6_GFXPAUSE, s->gfxpause);
2493 I915_WRITE(GEN6_RPDEUHWTC, s->rpdeuhwtc);
2494 I915_WRITE(GEN6_RPDEUC, s->rpdeuc);
2495 I915_WRITE(ECOBUS, s->ecobus);
2496 I915_WRITE(VLV_PWRDWNUPCTL, s->pwrdwnupctl);
2497 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,s->rp_down_timeout);
2498 I915_WRITE(GEN6_RPDEUCSW, s->rp_deucsw);
2499 I915_WRITE(GEN6_RCUBMABDTMR, s->rcubmabdtmr);
2500 I915_WRITE(VLV_RCEDATA, s->rcedata);
2501 I915_WRITE(VLV_SPAREG2H, s->spare2gh);
2502
2503 /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
2504 I915_WRITE(GTIMR, s->gt_imr);
2505 I915_WRITE(GTIER, s->gt_ier);
2506 I915_WRITE(GEN6_PMIMR, s->pm_imr);
2507 I915_WRITE(GEN6_PMIER, s->pm_ier);
2508
2509 for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002510 I915_WRITE(GEN7_GT_SCRATCH(i), s->gt_scratch[i]);
Imre Deakddeea5b2014-05-05 15:19:56 +03002511
2512 /* GT SA CZ domain, 0x100000-0x138124 */
2513 I915_WRITE(TILECTL, s->tilectl);
2514 I915_WRITE(GTFIFOCTL, s->gt_fifoctl);
2515 /*
2516 * Preserve the GT allow wake and GFX force clock bit, they are not
2517 * be restored, as they are used to control the s0ix suspend/resume
2518 * sequence by the caller.
2519 */
2520 val = I915_READ(VLV_GTLC_WAKE_CTRL);
2521 val &= VLV_GTLC_ALLOWWAKEREQ;
2522 val |= s->gtlc_wake_ctrl & ~VLV_GTLC_ALLOWWAKEREQ;
2523 I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
2524
2525 val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2526 val &= VLV_GFX_CLK_FORCE_ON_BIT;
2527 val |= s->gtlc_survive & ~VLV_GFX_CLK_FORCE_ON_BIT;
2528 I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
2529
2530 I915_WRITE(VLV_PMWGICZ, s->pmwgicz);
2531
2532 /* Gunit-Display CZ domain, 0x182028-0x1821CF */
2533 I915_WRITE(VLV_GU_CTL0, s->gu_ctl0);
2534 I915_WRITE(VLV_GU_CTL1, s->gu_ctl1);
Jesse Barnes9c252102015-04-01 14:22:57 -07002535 I915_WRITE(VLV_PCBR, s->pcbr);
Imre Deakddeea5b2014-05-05 15:19:56 +03002536 I915_WRITE(VLV_GUNIT_CLOCK_GATE2, s->clock_gate_dis2);
2537}
2538
Imre Deak650ad972014-04-18 16:35:02 +03002539int vlv_force_gfx_clock(struct drm_i915_private *dev_priv, bool force_on)
2540{
2541 u32 val;
2542 int err;
2543
Imre Deak650ad972014-04-18 16:35:02 +03002544 val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2545 val &= ~VLV_GFX_CLK_FORCE_ON_BIT;
2546 if (force_on)
2547 val |= VLV_GFX_CLK_FORCE_ON_BIT;
2548 I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
2549
2550 if (!force_on)
2551 return 0;
2552
Chris Wilsonc6ddc5f2016-06-30 15:32:46 +01002553 err = intel_wait_for_register(dev_priv,
2554 VLV_GTLC_SURVIVABILITY_REG,
2555 VLV_GFX_CLK_STATUS_BIT,
2556 VLV_GFX_CLK_STATUS_BIT,
2557 20);
Imre Deak650ad972014-04-18 16:35:02 +03002558 if (err)
2559 DRM_ERROR("timeout waiting for GFX clock force-on (%08x)\n",
2560 I915_READ(VLV_GTLC_SURVIVABILITY_REG));
2561
2562 return err;
Imre Deak650ad972014-04-18 16:35:02 +03002563}
2564
Imre Deakddeea5b2014-05-05 15:19:56 +03002565static int vlv_allow_gt_wake(struct drm_i915_private *dev_priv, bool allow)
2566{
2567 u32 val;
2568 int err = 0;
2569
2570 val = I915_READ(VLV_GTLC_WAKE_CTRL);
2571 val &= ~VLV_GTLC_ALLOWWAKEREQ;
2572 if (allow)
2573 val |= VLV_GTLC_ALLOWWAKEREQ;
2574 I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
2575 POSTING_READ(VLV_GTLC_WAKE_CTRL);
2576
Chris Wilsonb2736692016-06-30 15:32:47 +01002577 err = intel_wait_for_register(dev_priv,
2578 VLV_GTLC_PW_STATUS,
2579 VLV_GTLC_ALLOWWAKEACK,
2580 allow,
2581 1);
Imre Deakddeea5b2014-05-05 15:19:56 +03002582 if (err)
2583 DRM_ERROR("timeout disabling GT waking\n");
Chris Wilsonb2736692016-06-30 15:32:47 +01002584
Imre Deakddeea5b2014-05-05 15:19:56 +03002585 return err;
Imre Deakddeea5b2014-05-05 15:19:56 +03002586}
2587
2588static int vlv_wait_for_gt_wells(struct drm_i915_private *dev_priv,
2589 bool wait_for_on)
2590{
2591 u32 mask;
2592 u32 val;
2593 int err;
2594
2595 mask = VLV_GTLC_PW_MEDIA_STATUS_MASK | VLV_GTLC_PW_RENDER_STATUS_MASK;
2596 val = wait_for_on ? mask : 0;
Chris Wilson41ce4052016-06-30 15:32:48 +01002597 if ((I915_READ(VLV_GTLC_PW_STATUS) & mask) == val)
Imre Deakddeea5b2014-05-05 15:19:56 +03002598 return 0;
2599
2600 DRM_DEBUG_KMS("waiting for GT wells to go %s (%08x)\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02002601 onoff(wait_for_on),
2602 I915_READ(VLV_GTLC_PW_STATUS));
Imre Deakddeea5b2014-05-05 15:19:56 +03002603
2604 /*
2605 * RC6 transitioning can be delayed up to 2 msec (see
2606 * valleyview_enable_rps), use 3 msec for safety.
2607 */
Chris Wilson41ce4052016-06-30 15:32:48 +01002608 err = intel_wait_for_register(dev_priv,
2609 VLV_GTLC_PW_STATUS, mask, val,
2610 3);
Imre Deakddeea5b2014-05-05 15:19:56 +03002611 if (err)
2612 DRM_ERROR("timeout waiting for GT wells to go %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02002613 onoff(wait_for_on));
Imre Deakddeea5b2014-05-05 15:19:56 +03002614
2615 return err;
Imre Deakddeea5b2014-05-05 15:19:56 +03002616}
2617
2618static void vlv_check_no_gt_access(struct drm_i915_private *dev_priv)
2619{
2620 if (!(I915_READ(VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEERR))
2621 return;
2622
Daniel Vetter6fa283b2016-01-19 21:00:56 +01002623 DRM_DEBUG_DRIVER("GT register access while GT waking disabled\n");
Imre Deakddeea5b2014-05-05 15:19:56 +03002624 I915_WRITE(VLV_GTLC_PW_STATUS, VLV_GTLC_ALLOWWAKEERR);
2625}
2626
Sagar Kambleebc32822014-08-13 23:07:05 +05302627static int vlv_suspend_complete(struct drm_i915_private *dev_priv)
Imre Deakddeea5b2014-05-05 15:19:56 +03002628{
2629 u32 mask;
2630 int err;
2631
2632 /*
2633 * Bspec defines the following GT well on flags as debug only, so
2634 * don't treat them as hard failures.
2635 */
2636 (void)vlv_wait_for_gt_wells(dev_priv, false);
2637
2638 mask = VLV_GTLC_RENDER_CTX_EXISTS | VLV_GTLC_MEDIA_CTX_EXISTS;
2639 WARN_ON((I915_READ(VLV_GTLC_WAKE_CTRL) & mask) != mask);
2640
2641 vlv_check_no_gt_access(dev_priv);
2642
2643 err = vlv_force_gfx_clock(dev_priv, true);
2644 if (err)
2645 goto err1;
2646
2647 err = vlv_allow_gt_wake(dev_priv, false);
2648 if (err)
2649 goto err2;
Deepak S98711162014-12-12 14:18:16 +05302650
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002651 if (!IS_CHERRYVIEW(dev_priv))
Deepak S98711162014-12-12 14:18:16 +05302652 vlv_save_gunit_s0ix_state(dev_priv);
Imre Deakddeea5b2014-05-05 15:19:56 +03002653
2654 err = vlv_force_gfx_clock(dev_priv, false);
2655 if (err)
2656 goto err2;
2657
2658 return 0;
2659
2660err2:
2661 /* For safety always re-enable waking and disable gfx clock forcing */
2662 vlv_allow_gt_wake(dev_priv, true);
2663err1:
2664 vlv_force_gfx_clock(dev_priv, false);
2665
2666 return err;
2667}
2668
Sagar Kamble016970b2014-08-13 23:07:06 +05302669static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
2670 bool rpm_resume)
Imre Deakddeea5b2014-05-05 15:19:56 +03002671{
2672 struct drm_device *dev = dev_priv->dev;
2673 int err;
2674 int ret;
2675
2676 /*
2677 * If any of the steps fail just try to continue, that's the best we
2678 * can do at this point. Return the first error code (which will also
2679 * leave RPM permanently disabled).
2680 */
2681 ret = vlv_force_gfx_clock(dev_priv, true);
2682
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002683 if (!IS_CHERRYVIEW(dev_priv))
Deepak S98711162014-12-12 14:18:16 +05302684 vlv_restore_gunit_s0ix_state(dev_priv);
Imre Deakddeea5b2014-05-05 15:19:56 +03002685
2686 err = vlv_allow_gt_wake(dev_priv, true);
2687 if (!ret)
2688 ret = err;
2689
2690 err = vlv_force_gfx_clock(dev_priv, false);
2691 if (!ret)
2692 ret = err;
2693
2694 vlv_check_no_gt_access(dev_priv);
2695
Sagar Kamble016970b2014-08-13 23:07:06 +05302696 if (rpm_resume) {
2697 intel_init_clock_gating(dev);
2698 i915_gem_restore_fences(dev);
2699 }
Imre Deakddeea5b2014-05-05 15:19:56 +03002700
2701 return ret;
2702}
2703
Paulo Zanoni97bea202014-03-07 20:12:33 -03002704static int intel_runtime_suspend(struct device *device)
Paulo Zanoni8a187452013-12-06 20:32:13 -02002705{
2706 struct pci_dev *pdev = to_pci_dev(device);
2707 struct drm_device *dev = pci_get_drvdata(pdev);
2708 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002709 int ret;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002710
Chris Wilsondc979972016-05-10 14:10:04 +01002711 if (WARN_ON_ONCE(!(dev_priv->rps.enabled && intel_enable_rc6())))
Imre Deakc6df39b2014-04-14 20:24:29 +03002712 return -ENODEV;
2713
Imre Deak604effb2014-08-26 13:26:56 +03002714 if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev)))
2715 return -ENODEV;
2716
Paulo Zanoni8a187452013-12-06 20:32:13 -02002717 DRM_DEBUG_KMS("Suspending device\n");
2718
Imre Deak9486db62014-04-22 20:21:07 +03002719 /*
Imre Deakd6102972014-05-07 19:57:49 +03002720 * We could deadlock here in case another thread holding struct_mutex
2721 * calls RPM suspend concurrently, since the RPM suspend will wait
2722 * first for this RPM suspend to finish. In this case the concurrent
2723 * RPM resume will be followed by its RPM suspend counterpart. Still
2724 * for consistency return -EAGAIN, which will reschedule this suspend.
2725 */
2726 if (!mutex_trylock(&dev->struct_mutex)) {
2727 DRM_DEBUG_KMS("device lock contention, deffering suspend\n");
2728 /*
2729 * Bump the expiration timestamp, otherwise the suspend won't
2730 * be rescheduled.
2731 */
2732 pm_runtime_mark_last_busy(device);
2733
2734 return -EAGAIN;
2735 }
Imre Deak1f814da2015-12-16 02:52:19 +02002736
2737 disable_rpm_wakeref_asserts(dev_priv);
2738
Imre Deakd6102972014-05-07 19:57:49 +03002739 /*
2740 * We are safe here against re-faults, since the fault handler takes
2741 * an RPM reference.
2742 */
2743 i915_gem_release_all_mmaps(dev_priv);
2744 mutex_unlock(&dev->struct_mutex);
2745
Joonas Lahtinen825f2722015-12-09 15:56:13 +02002746 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
2747
Alex Daia1c41992015-09-30 09:46:37 -07002748 intel_guc_suspend(dev);
2749
Chris Wilsondc979972016-05-10 14:10:04 +01002750 intel_suspend_gt_powersave(dev_priv);
Imre Deak2eb52522014-11-19 15:30:05 +02002751 intel_runtime_pm_disable_interrupts(dev_priv);
Imre Deakb5478bc2014-04-14 20:24:37 +03002752
Imre Deak507e1262016-04-20 20:27:54 +03002753 ret = 0;
2754 if (IS_BROXTON(dev_priv)) {
2755 bxt_display_core_uninit(dev_priv);
2756 bxt_enable_dc9(dev_priv);
2757 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
2758 hsw_enable_pc8(dev_priv);
2759 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
2760 ret = vlv_suspend_complete(dev_priv);
2761 }
2762
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002763 if (ret) {
2764 DRM_ERROR("Runtime suspend failed, disabling it (%d)\n", ret);
Daniel Vetterb9632912014-09-30 10:56:44 +02002765 intel_runtime_pm_enable_interrupts(dev_priv);
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002766
Imre Deak1f814da2015-12-16 02:52:19 +02002767 enable_rpm_wakeref_asserts(dev_priv);
2768
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002769 return ret;
2770 }
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03002771
Chris Wilsondc979972016-05-10 14:10:04 +01002772 intel_uncore_forcewake_reset(dev_priv, false);
Imre Deak1f814da2015-12-16 02:52:19 +02002773
2774 enable_rpm_wakeref_asserts(dev_priv);
2775 WARN_ON_ONCE(atomic_read(&dev_priv->pm.wakeref_count));
Mika Kuoppala55ec45c2015-12-15 16:25:08 +02002776
Mika Kuoppalabc3b9342016-01-08 15:51:20 +02002777 if (intel_uncore_arm_unclaimed_mmio_detection(dev_priv))
Mika Kuoppala55ec45c2015-12-15 16:25:08 +02002778 DRM_ERROR("Unclaimed access detected prior to suspending\n");
2779
Paulo Zanoni8a187452013-12-06 20:32:13 -02002780 dev_priv->pm.suspended = true;
Kristen Carlson Accardi1fb23622014-01-14 15:36:15 -08002781
2782 /*
Paulo Zanonic8a0bd42014-08-21 17:09:38 -03002783 * FIXME: We really should find a document that references the arguments
2784 * used below!
Kristen Carlson Accardi1fb23622014-01-14 15:36:15 -08002785 */
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002786 if (IS_BROADWELL(dev_priv)) {
Paulo Zanonid37ae192015-07-30 18:20:29 -03002787 /*
2788 * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop
2789 * being detected, and the call we do at intel_runtime_resume()
2790 * won't be able to restore them. Since PCI_D3hot matches the
2791 * actual specification and appears to be working, use it.
2792 */
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002793 intel_opregion_notify_adapter(dev_priv, PCI_D3hot);
Paulo Zanonid37ae192015-07-30 18:20:29 -03002794 } else {
Paulo Zanonic8a0bd42014-08-21 17:09:38 -03002795 /*
2796 * current versions of firmware which depend on this opregion
2797 * notification have repurposed the D1 definition to mean
2798 * "runtime suspended" vs. what you would normally expect (D3)
2799 * to distinguish it from notifications that might be sent via
2800 * the suspend path.
2801 */
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002802 intel_opregion_notify_adapter(dev_priv, PCI_D1);
Paulo Zanonic8a0bd42014-08-21 17:09:38 -03002803 }
Paulo Zanoni8a187452013-12-06 20:32:13 -02002804
Mika Kuoppala59bad942015-01-16 11:34:40 +02002805 assert_forcewakes_inactive(dev_priv);
Chris Wilsondc9fb092015-01-16 11:34:34 +02002806
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03002807 DRM_DEBUG_KMS("Device suspended\n");
Paulo Zanoni8a187452013-12-06 20:32:13 -02002808 return 0;
2809}
2810
Paulo Zanoni97bea202014-03-07 20:12:33 -03002811static int intel_runtime_resume(struct device *device)
Paulo Zanoni8a187452013-12-06 20:32:13 -02002812{
2813 struct pci_dev *pdev = to_pci_dev(device);
2814 struct drm_device *dev = pci_get_drvdata(pdev);
2815 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002816 int ret = 0;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002817
Imre Deak604effb2014-08-26 13:26:56 +03002818 if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev)))
2819 return -ENODEV;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002820
2821 DRM_DEBUG_KMS("Resuming device\n");
2822
Imre Deak1f814da2015-12-16 02:52:19 +02002823 WARN_ON_ONCE(atomic_read(&dev_priv->pm.wakeref_count));
2824 disable_rpm_wakeref_asserts(dev_priv);
2825
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002826 intel_opregion_notify_adapter(dev_priv, PCI_D0);
Paulo Zanoni8a187452013-12-06 20:32:13 -02002827 dev_priv->pm.suspended = false;
Mika Kuoppala55ec45c2015-12-15 16:25:08 +02002828 if (intel_uncore_unclaimed_mmio(dev_priv))
2829 DRM_DEBUG_DRIVER("Unclaimed access during suspend, bios?\n");
Paulo Zanoni8a187452013-12-06 20:32:13 -02002830
Alex Daia1c41992015-09-30 09:46:37 -07002831 intel_guc_resume(dev);
2832
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002833 if (IS_GEN6(dev_priv))
2834 intel_init_pch_refclk(dev);
Suketu Shah31335ce2014-11-24 13:37:45 +05302835
Imre Deak507e1262016-04-20 20:27:54 +03002836 if (IS_BROXTON(dev)) {
2837 bxt_disable_dc9(dev_priv);
2838 bxt_display_core_init(dev_priv, true);
Imre Deakf62c79b2016-04-20 20:27:57 +03002839 if (dev_priv->csr.dmc_payload &&
2840 (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC5))
2841 gen9_enable_dc5(dev_priv);
Imre Deak507e1262016-04-20 20:27:54 +03002842 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002843 hsw_disable_pc8(dev_priv);
Imre Deak507e1262016-04-20 20:27:54 +03002844 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002845 ret = vlv_resume_prepare(dev_priv, true);
Imre Deak507e1262016-04-20 20:27:54 +03002846 }
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002847
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002848 /*
2849 * No point of rolling back things in case of an error, as the best
2850 * we can do is to hope that things will still work (and disable RPM).
2851 */
Imre Deak92b806d2014-04-14 20:24:39 +03002852 i915_gem_init_swizzling(dev);
Chris Wilsondc979972016-05-10 14:10:04 +01002853 gen6_update_ring_freq(dev_priv);
Imre Deak92b806d2014-04-14 20:24:39 +03002854
Daniel Vetterb9632912014-09-30 10:56:44 +02002855 intel_runtime_pm_enable_interrupts(dev_priv);
Ville Syrjälä08d8a232015-08-27 23:56:08 +03002856
2857 /*
2858 * On VLV/CHV display interrupts are part of the display
2859 * power well, so hpd is reinitialized from there. For
2860 * everyone else do it here.
2861 */
Wayne Boyer666a4532015-12-09 12:29:35 -08002862 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
Ville Syrjälä08d8a232015-08-27 23:56:08 +03002863 intel_hpd_init(dev_priv);
2864
Chris Wilsondc979972016-05-10 14:10:04 +01002865 intel_enable_gt_powersave(dev_priv);
Imre Deakb5478bc2014-04-14 20:24:37 +03002866
Imre Deak1f814da2015-12-16 02:52:19 +02002867 enable_rpm_wakeref_asserts(dev_priv);
2868
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002869 if (ret)
2870 DRM_ERROR("Runtime resume failed, disabling it (%d)\n", ret);
2871 else
2872 DRM_DEBUG_KMS("Device resumed\n");
2873
2874 return ret;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002875}
2876
Chris Wilson42f55512016-06-24 14:00:26 +01002877const struct dev_pm_ops i915_pm_ops = {
Imre Deak5545dbb2014-10-23 19:23:28 +03002878 /*
2879 * S0ix (via system suspend) and S3 event handlers [PMSG_SUSPEND,
2880 * PMSG_RESUME]
2881 */
Akshay Joshi0206e352011-08-16 15:34:10 -04002882 .suspend = i915_pm_suspend,
Imre Deak76c4b252014-04-01 19:55:22 +03002883 .suspend_late = i915_pm_suspend_late,
2884 .resume_early = i915_pm_resume_early,
Akshay Joshi0206e352011-08-16 15:34:10 -04002885 .resume = i915_pm_resume,
Imre Deak5545dbb2014-10-23 19:23:28 +03002886
2887 /*
2888 * S4 event handlers
2889 * @freeze, @freeze_late : called (1) before creating the
2890 * hibernation image [PMSG_FREEZE] and
2891 * (2) after rebooting, before restoring
2892 * the image [PMSG_QUIESCE]
2893 * @thaw, @thaw_early : called (1) after creating the hibernation
2894 * image, before writing it [PMSG_THAW]
2895 * and (2) after failing to create or
2896 * restore the image [PMSG_RECOVER]
2897 * @poweroff, @poweroff_late: called after writing the hibernation
2898 * image, before rebooting [PMSG_HIBERNATE]
2899 * @restore, @restore_early : called after rebooting and restoring the
2900 * hibernation image [PMSG_RESTORE]
2901 */
Chris Wilson1f19ac22016-05-14 07:26:32 +01002902 .freeze = i915_pm_freeze,
2903 .freeze_late = i915_pm_freeze_late,
2904 .thaw_early = i915_pm_thaw_early,
2905 .thaw = i915_pm_thaw,
Imre Deak36d61e62014-10-23 19:23:24 +03002906 .poweroff = i915_pm_suspend,
Imre Deakab3be732015-03-02 13:04:41 +02002907 .poweroff_late = i915_pm_poweroff_late,
Chris Wilson1f19ac22016-05-14 07:26:32 +01002908 .restore_early = i915_pm_restore_early,
2909 .restore = i915_pm_restore,
Imre Deak5545dbb2014-10-23 19:23:28 +03002910
2911 /* S0ix (via runtime suspend) event handlers */
Paulo Zanoni97bea202014-03-07 20:12:33 -03002912 .runtime_suspend = intel_runtime_suspend,
2913 .runtime_resume = intel_runtime_resume,
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002914};
2915
Laurent Pinchart78b68552012-05-17 13:27:22 +02002916static const struct vm_operations_struct i915_gem_vm_ops = {
Jesse Barnesde151cf2008-11-12 10:03:55 -08002917 .fault = i915_gem_fault,
Jesse Barnesab00b3e2009-02-11 14:01:46 -08002918 .open = drm_gem_vm_open,
2919 .close = drm_gem_vm_close,
Jesse Barnesde151cf2008-11-12 10:03:55 -08002920};
2921
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002922static const struct file_operations i915_driver_fops = {
2923 .owner = THIS_MODULE,
2924 .open = drm_open,
2925 .release = drm_release,
2926 .unlocked_ioctl = drm_ioctl,
2927 .mmap = drm_gem_mmap,
2928 .poll = drm_poll,
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002929 .read = drm_read,
2930#ifdef CONFIG_COMPAT
2931 .compat_ioctl = i915_compat_ioctl,
2932#endif
2933 .llseek = noop_llseek,
2934};
2935
Chris Wilson0673ad42016-06-24 14:00:22 +01002936static int
2937i915_gem_reject_pin_ioctl(struct drm_device *dev, void *data,
2938 struct drm_file *file)
2939{
2940 return -ENODEV;
2941}
2942
2943static const struct drm_ioctl_desc i915_ioctls[] = {
2944 DRM_IOCTL_DEF_DRV(I915_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2945 DRM_IOCTL_DEF_DRV(I915_FLUSH, drm_noop, DRM_AUTH),
2946 DRM_IOCTL_DEF_DRV(I915_FLIP, drm_noop, DRM_AUTH),
2947 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, drm_noop, DRM_AUTH),
2948 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, drm_noop, DRM_AUTH),
2949 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, drm_noop, DRM_AUTH),
2950 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
2951 DRM_IOCTL_DEF_DRV(I915_SETPARAM, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2952 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
2953 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
2954 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2955 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, drm_noop, DRM_AUTH),
2956 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2957 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2958 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, drm_noop, DRM_AUTH),
2959 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, drm_noop, DRM_AUTH),
2960 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2961 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2962 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
2963 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_RENDER_ALLOW),
2964 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
2965 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
2966 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
2967 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_RENDER_ALLOW),
2968 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_RENDER_ALLOW),
2969 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
2970 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2971 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2972 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_RENDER_ALLOW),
2973 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW),
2974 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW),
2975 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW),
2976 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_RENDER_ALLOW),
2977 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW),
2978 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW),
2979 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_RENDER_ALLOW),
2980 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_RENDER_ALLOW),
2981 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_RENDER_ALLOW),
2982 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, 0),
2983 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_RENDER_ALLOW),
2984 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW),
2985 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW),
2986 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW),
2987 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW),
2988 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
2989 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_RENDER_ALLOW),
2990 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_RENDER_ALLOW),
2991 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_RENDER_ALLOW),
2992 DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_gem_context_reset_stats_ioctl, DRM_RENDER_ALLOW),
2993 DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
2994 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
2995 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
2996};
2997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998static struct drm_driver driver = {
Michael Witten0c547812011-08-25 17:55:54 +00002999 /* Don't use MTRRs here; the Xserver or userspace app should
3000 * deal with them for Intel hardware.
Dave Airlie792d2b92005-11-11 23:30:27 +11003001 */
Eric Anholt673a3942008-07-30 12:06:12 -07003002 .driver_features =
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02003003 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM | DRIVER_PRIME |
Maarten Lankhorst1751fcf2015-08-27 15:15:15 +02003004 DRIVER_RENDER | DRIVER_MODESET,
Eric Anholt673a3942008-07-30 12:06:12 -07003005 .open = i915_driver_open,
Dave Airlie22eae942005-11-10 22:16:34 +11003006 .lastclose = i915_driver_lastclose,
3007 .preclose = i915_driver_preclose,
Eric Anholt673a3942008-07-30 12:06:12 -07003008 .postclose = i915_driver_postclose,
David Herrmann915b4d12014-08-29 12:12:43 +02003009 .set_busid = drm_pci_set_busid,
Rafael J. Wysockid8e29202010-01-09 00:45:33 +01003010
Eric Anholt673a3942008-07-30 12:06:12 -07003011 .gem_free_object = i915_gem_free_object,
Jesse Barnesde151cf2008-11-12 10:03:55 -08003012 .gem_vm_ops = &i915_gem_vm_ops,
Daniel Vetter1286ff72012-05-10 15:25:09 +02003013
3014 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
3015 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
3016 .gem_prime_export = i915_gem_prime_export,
3017 .gem_prime_import = i915_gem_prime_import,
3018
Dave Airlieff72145b2011-02-07 12:16:14 +10003019 .dumb_create = i915_gem_dumb_create,
Dave Airlieda6b51d2014-12-24 13:11:17 +10003020 .dumb_map_offset = i915_gem_mmap_gtt,
Daniel Vetter43387b32013-07-16 09:12:04 +02003021 .dumb_destroy = drm_gem_dumb_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 .ioctls = i915_ioctls,
Chris Wilson0673ad42016-06-24 14:00:22 +01003023 .num_ioctls = ARRAY_SIZE(i915_ioctls),
Arjan van de Vene08e96d2011-10-31 07:28:57 -07003024 .fops = &i915_driver_fops,
Dave Airlie22eae942005-11-10 22:16:34 +11003025 .name = DRIVER_NAME,
3026 .desc = DRIVER_DESC,
3027 .date = DRIVER_DATE,
3028 .major = DRIVER_MAJOR,
3029 .minor = DRIVER_MINOR,
3030 .patchlevel = DRIVER_PATCHLEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031};