blob: b790896ed809bbedee0c8ddd80653edec81e27f7 [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>
Chris Wilson0673ad42016-06-24 14:00:22 +010039#include <linux/vga_switcheroo.h>
40#include <linux/vt.h>
41#include <acpi/video.h>
42
Maarten Lankhorsta667fb42016-12-15 15:29:44 +010043#include <drm/drm_atomic_helper.h>
Sam Ravnborgd0e93592019-01-26 13:25:24 +010044#include <drm/drm_ioctl.h>
45#include <drm/drm_irq.h>
46#include <drm/drm_probe_helper.h>
David Howells760285e2012-10-02 18:01:07 +010047#include <drm/i915_drm.h>
Chris Wilson0673ad42016-06-24 14:00:22 +010048
Jani Nikuladf0566a2019-06-13 11:44:16 +030049#include "display/intel_acpi.h"
50#include "display/intel_audio.h"
51#include "display/intel_bw.h"
52#include "display/intel_cdclk.h"
Jani Nikula1d455f82019-08-06 14:39:33 +030053#include "display/intel_display_types.h"
Jani Nikula379bc102019-06-13 11:44:15 +030054#include "display/intel_dp.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030055#include "display/intel_fbdev.h"
Jani Nikuladf0566a2019-06-13 11:44:16 +030056#include "display/intel_hotplug.h"
57#include "display/intel_overlay.h"
58#include "display/intel_pipe_crc.h"
59#include "display/intel_sprite.h"
Jani Nikula4fb87832019-10-01 18:25:06 +030060#include "display/intel_vga.h"
Jani Nikula379bc102019-06-13 11:44:15 +030061
Chris Wilson10be98a2019-05-28 10:29:49 +010062#include "gem/i915_gem_context.h"
Chris Wilsonafa13082019-05-28 10:29:43 +010063#include "gem/i915_gem_ioctls.h"
Abdiel Janulguecc662122019-12-04 12:00:32 +000064#include "gem/i915_gem_mman.h"
Tvrtko Ursulin24635c52019-06-21 08:07:41 +010065#include "gt/intel_gt.h"
Chris Wilson79ffac852019-04-24 21:07:17 +010066#include "gt/intel_gt_pm.h"
Imre Deak2248a282019-10-17 16:38:31 +030067#include "gt/intel_rc6.h"
Chris Wilson112ed2d2019-04-24 18:48:39 +010068
Jani Nikula2126d3e2019-05-02 18:02:43 +030069#include "i915_debugfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include "i915_drv.h"
Jani Nikula440e2b32019-04-29 15:29:27 +030071#include "i915_irq.h"
Jani Nikula9c9082b2019-08-08 16:42:47 +030072#include "i915_memcpy.h"
Jani Nikuladb94e9f2019-08-08 16:42:44 +030073#include "i915_perf.h"
Lionel Landwerlina446ae22018-03-06 12:28:56 +000074#include "i915_query.h"
Jani Nikulabdd15102019-08-08 16:42:46 +030075#include "i915_suspend.h"
Jani Nikula63bf8302019-10-04 15:20:18 +030076#include "i915_switcheroo.h"
Jani Nikulabe682612019-08-08 16:42:45 +030077#include "i915_sysfs.h"
Jani Nikula331c2012019-04-05 14:00:03 +030078#include "i915_trace.h"
Chris Wilson0673ad42016-06-24 14:00:22 +010079#include "i915_vgpu.h"
Jani Nikula174594d2019-04-05 14:00:07 +030080#include "intel_csr.h"
Chris Wilson3fc794f2019-10-26 21:20:32 +010081#include "intel_memory_region.h"
Jani Nikula696173b2019-04-05 14:00:15 +030082#include "intel_pm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Kristian Høgsberg112b7152009-01-04 16:55:33 -050084static struct drm_driver driver;
85
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -070086struct vlv_s0ix_state {
87 /* GAM */
88 u32 wr_watermark;
89 u32 gfx_prio_ctrl;
90 u32 arb_mode;
91 u32 gfx_pend_tlb0;
92 u32 gfx_pend_tlb1;
93 u32 lra_limits[GEN7_LRA_LIMITS_REG_NUM];
94 u32 media_max_req_count;
95 u32 gfx_max_req_count;
96 u32 render_hwsp;
97 u32 ecochk;
98 u32 bsd_hwsp;
99 u32 blt_hwsp;
100 u32 tlb_rd_addr;
Chris Wilson0673ad42016-06-24 14:00:22 +0100101
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -0700102 /* MBC */
103 u32 g3dctl;
104 u32 gsckgctl;
105 u32 mbctl;
Chris Wilson0673ad42016-06-24 14:00:22 +0100106
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -0700107 /* GCP */
108 u32 ucgctl1;
109 u32 ucgctl3;
110 u32 rcgctl1;
111 u32 rcgctl2;
112 u32 rstctl;
113 u32 misccpctl;
Chris Wilson0673ad42016-06-24 14:00:22 +0100114
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -0700115 /* GPM */
116 u32 gfxpause;
117 u32 rpdeuhwtc;
118 u32 rpdeuc;
119 u32 ecobus;
120 u32 pwrdwnupctl;
121 u32 rp_down_timeout;
122 u32 rp_deucsw;
123 u32 rcubmabdtmr;
124 u32 rcedata;
125 u32 spare2gh;
Chris Wilson51c18bf2018-06-09 12:10:58 +0100126
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -0700127 /* Display 1 CZ domain */
128 u32 gt_imr;
129 u32 gt_ier;
130 u32 pm_imr;
131 u32 pm_ier;
132 u32 gt_scratch[GEN7_GT_SCRATCH_REG_NUM];
Chris Wilson51c18bf2018-06-09 12:10:58 +0100133
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -0700134 /* GT SA CZ domain */
135 u32 tilectl;
136 u32 gt_fifoctl;
137 u32 gtlc_wake_ctrl;
138 u32 gtlc_survive;
139 u32 pmwgicz;
Chris Wilson0673ad42016-06-24 14:00:22 +0100140
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -0700141 /* Display 2 CZ domain */
142 u32 gu_ctl0;
143 u32 gu_ctl1;
144 u32 pcbr;
145 u32 clock_gate_dis2;
146};
Chris Wilson0673ad42016-06-24 14:00:22 +0100147
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000148static int i915_get_bridge_dev(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100149{
Sinan Kaya57b296462017-11-27 11:57:46 -0500150 int domain = pci_domain_nr(dev_priv->drm.pdev->bus);
151
152 dev_priv->bridge_dev =
153 pci_get_domain_bus_and_slot(domain, 0, PCI_DEVFN(0, 0));
Chris Wilson0673ad42016-06-24 14:00:22 +0100154 if (!dev_priv->bridge_dev) {
155 DRM_ERROR("bridge device not found\n");
156 return -1;
157 }
158 return 0;
159}
160
161/* Allocate space for the MCH regs if needed, return nonzero on error */
162static int
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000163intel_alloc_mchbar_resource(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100164{
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +0000165 int reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Chris Wilson0673ad42016-06-24 14:00:22 +0100166 u32 temp_lo, temp_hi = 0;
167 u64 mchbar_addr;
168 int ret;
169
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +0000170 if (INTEL_GEN(dev_priv) >= 4)
Chris Wilson0673ad42016-06-24 14:00:22 +0100171 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
172 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
173 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
174
175 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
176#ifdef CONFIG_PNP
177 if (mchbar_addr &&
178 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
179 return 0;
180#endif
181
182 /* Get some space for it */
183 dev_priv->mch_res.name = "i915 MCHBAR";
184 dev_priv->mch_res.flags = IORESOURCE_MEM;
185 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
186 &dev_priv->mch_res,
187 MCHBAR_SIZE, MCHBAR_SIZE,
188 PCIBIOS_MIN_MEM,
189 0, pcibios_align_resource,
190 dev_priv->bridge_dev);
191 if (ret) {
192 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
193 dev_priv->mch_res.start = 0;
194 return ret;
195 }
196
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +0000197 if (INTEL_GEN(dev_priv) >= 4)
Chris Wilson0673ad42016-06-24 14:00:22 +0100198 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
199 upper_32_bits(dev_priv->mch_res.start));
200
201 pci_write_config_dword(dev_priv->bridge_dev, reg,
202 lower_32_bits(dev_priv->mch_res.start));
203 return 0;
204}
205
206/* Setup MCHBAR if possible, return true if we should disable it again */
207static void
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000208intel_setup_mchbar(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100209{
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +0000210 int mchbar_reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Chris Wilson0673ad42016-06-24 14:00:22 +0100211 u32 temp;
212 bool enabled;
213
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100214 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson0673ad42016-06-24 14:00:22 +0100215 return;
216
217 dev_priv->mchbar_need_disable = false;
218
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100219 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
Chris Wilson0673ad42016-06-24 14:00:22 +0100220 pci_read_config_dword(dev_priv->bridge_dev, DEVEN, &temp);
221 enabled = !!(temp & DEVEN_MCHBAR_EN);
222 } else {
223 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
224 enabled = temp & 1;
225 }
226
227 /* If it's already enabled, don't have to do anything */
228 if (enabled)
229 return;
230
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000231 if (intel_alloc_mchbar_resource(dev_priv))
Chris Wilson0673ad42016-06-24 14:00:22 +0100232 return;
233
234 dev_priv->mchbar_need_disable = true;
235
236 /* Space is allocated or reserved, so enable it. */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100237 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
Chris Wilson0673ad42016-06-24 14:00:22 +0100238 pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
239 temp | DEVEN_MCHBAR_EN);
240 } else {
241 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
242 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
243 }
244}
245
246static void
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000247intel_teardown_mchbar(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100248{
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +0000249 int mchbar_reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Chris Wilson0673ad42016-06-24 14:00:22 +0100250
251 if (dev_priv->mchbar_need_disable) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100252 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
Chris Wilson0673ad42016-06-24 14:00:22 +0100253 u32 deven_val;
254
255 pci_read_config_dword(dev_priv->bridge_dev, DEVEN,
256 &deven_val);
257 deven_val &= ~DEVEN_MCHBAR_EN;
258 pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
259 deven_val);
260 } else {
261 u32 mchbar_val;
262
263 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg,
264 &mchbar_val);
265 mchbar_val &= ~1;
266 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg,
267 mchbar_val);
268 }
269 }
270
271 if (dev_priv->mch_res.start)
272 release_resource(&dev_priv->mch_res);
273}
274
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300275static int i915_driver_modeset_probe(struct drm_i915_private *i915)
Chris Wilson0673ad42016-06-24 14:00:22 +0100276{
Chris Wilson0673ad42016-06-24 14:00:22 +0100277 int ret;
278
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300279 if (i915_inject_probe_failure(i915))
Chris Wilson0673ad42016-06-24 14:00:22 +0100280 return -ENODEV;
281
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300282 if (HAS_DISPLAY(i915) && INTEL_DISPLAY_ENABLED(i915)) {
283 ret = drm_vblank_init(&i915->drm,
284 INTEL_NUM_PIPES(i915));
José Roberto de Souza8d3bf1a2018-11-07 16:16:44 -0800285 if (ret)
286 goto out;
287 }
288
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300289 intel_bios_init(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100290
Jani Nikula4fb87832019-10-01 18:25:06 +0300291 ret = intel_vga_register(i915);
292 if (ret)
Chris Wilson0673ad42016-06-24 14:00:22 +0100293 goto out;
294
295 intel_register_dsm_handler();
296
Jani Nikula63bf8302019-10-04 15:20:18 +0300297 ret = i915_switcheroo_register(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100298 if (ret)
299 goto cleanup_vga_client;
300
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300301 intel_power_domains_init_hw(i915, false);
Chris Wilson0673ad42016-06-24 14:00:22 +0100302
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300303 intel_csr_ucode_init(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100304
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300305 ret = intel_irq_install(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100306 if (ret)
307 goto cleanup_csr;
308
Chris Wilson0673ad42016-06-24 14:00:22 +0100309 /* Important: The output setup functions called by modeset_init need
310 * working irqs for e.g. gmbus and dp aux transfers. */
Jani Nikula6cd02e72019-09-20 21:54:21 +0300311 ret = intel_modeset_init(i915);
Ville Syrjäläb079bd172016-10-25 18:58:02 +0300312 if (ret)
313 goto cleanup_irq;
Chris Wilson0673ad42016-06-24 14:00:22 +0100314
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300315 ret = i915_gem_init(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100316 if (ret)
Chris Wilson73bad7c2018-07-10 10:44:21 +0100317 goto cleanup_modeset;
Chris Wilson0673ad42016-06-24 14:00:22 +0100318
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300319 intel_overlay_setup(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100320
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300321 if (!HAS_DISPLAY(i915) || !INTEL_DISPLAY_ENABLED(i915))
Chris Wilson0673ad42016-06-24 14:00:22 +0100322 return 0;
323
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300324 ret = intel_fbdev_init(&i915->drm);
Chris Wilson0673ad42016-06-24 14:00:22 +0100325 if (ret)
326 goto cleanup_gem;
327
328 /* Only enable hotplug handling once the fbdev is fully set up. */
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300329 intel_hpd_init(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100330
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300331 intel_init_ipc(i915);
José Roberto de Souzaa8147d02018-11-07 16:16:46 -0800332
Chris Wilson0673ad42016-06-24 14:00:22 +0100333 return 0;
334
335cleanup_gem:
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300336 i915_gem_suspend(i915);
337 i915_gem_driver_remove(i915);
338 i915_gem_driver_release(i915);
Chris Wilson73bad7c2018-07-10 10:44:21 +0100339cleanup_modeset:
Jani Nikula9980c3c2019-09-20 21:54:18 +0300340 intel_modeset_driver_remove(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100341cleanup_irq:
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300342 intel_irq_uninstall(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100343cleanup_csr:
Jani Nikula5bcd53a2019-09-20 21:54:17 +0300344 intel_csr_ucode_fini(i915);
345 intel_power_domains_driver_remove(i915);
Jani Nikula63bf8302019-10-04 15:20:18 +0300346 i915_switcheroo_unregister(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100347cleanup_vga_client:
Jani Nikula4fb87832019-10-01 18:25:06 +0300348 intel_vga_unregister(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100349out:
350 return ret;
351}
352
Jani Nikula2d6f6f32019-09-20 21:54:16 +0300353static void i915_driver_modeset_remove(struct drm_i915_private *i915)
Chris Wilson0673ad42016-06-24 14:00:22 +0100354{
Jani Nikula9980c3c2019-09-20 21:54:18 +0300355 intel_modeset_driver_remove(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100356
Janusz Krzysztofik789fa872019-10-18 12:07:10 +0200357 intel_irq_uninstall(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100358
Jani Nikula2d6f6f32019-09-20 21:54:16 +0300359 intel_bios_driver_remove(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100360
Jani Nikula63bf8302019-10-04 15:20:18 +0300361 i915_switcheroo_unregister(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100362
Jani Nikula4fb87832019-10-01 18:25:06 +0300363 intel_vga_unregister(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100364
Jani Nikula2d6f6f32019-09-20 21:54:16 +0300365 intel_csr_ucode_fini(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +0100366}
Chris Wilson0673ad42016-06-24 14:00:22 +0100367
Chris Wilson0673ad42016-06-24 14:00:22 +0100368static void intel_init_dpio(struct drm_i915_private *dev_priv)
369{
370 /*
371 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
372 * CHV x1 PHY (DP/HDMI D)
373 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
374 */
375 if (IS_CHERRYVIEW(dev_priv)) {
376 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
377 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
378 } else if (IS_VALLEYVIEW(dev_priv)) {
379 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
380 }
381}
382
383static int i915_workqueues_init(struct drm_i915_private *dev_priv)
384{
385 /*
386 * The i915 workqueue is primarily used for batched retirement of
387 * requests (and thus managing bo) once the task has been completed
Chris Wilsone61e0f52018-02-21 09:56:36 +0000388 * by the GPU. i915_retire_requests() is called directly when we
Chris Wilson0673ad42016-06-24 14:00:22 +0100389 * need high-priority retirement, such as waiting for an explicit
390 * bo.
391 *
392 * It is also used for periodic low-priority events, such as
393 * idle-timers and recording error state.
394 *
395 * All tasks on the workqueue are expected to acquire the dev mutex
396 * so there is no point in running more than one instance of the
397 * workqueue at any time. Use an ordered one.
398 */
399 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
400 if (dev_priv->wq == NULL)
401 goto out_err;
402
403 dev_priv->hotplug.dp_wq = alloc_ordered_workqueue("i915-dp", 0);
404 if (dev_priv->hotplug.dp_wq == NULL)
405 goto out_free_wq;
406
Chris Wilson0673ad42016-06-24 14:00:22 +0100407 return 0;
408
Chris Wilson0673ad42016-06-24 14:00:22 +0100409out_free_wq:
410 destroy_workqueue(dev_priv->wq);
411out_err:
412 DRM_ERROR("Failed to allocate workqueues.\n");
413
414 return -ENOMEM;
415}
416
417static void i915_workqueues_cleanup(struct drm_i915_private *dev_priv)
418{
Chris Wilson0673ad42016-06-24 14:00:22 +0100419 destroy_workqueue(dev_priv->hotplug.dp_wq);
420 destroy_workqueue(dev_priv->wq);
421}
422
Paulo Zanoni4fc7e842016-09-26 15:07:52 +0300423/*
424 * We don't keep the workarounds for pre-production hardware, so we expect our
425 * driver to fail on these machines in one way or another. A little warning on
426 * dmesg may help both the user and the bug triagers.
Chris Wilson6a7a6a92017-11-17 10:26:35 +0000427 *
428 * Our policy for removing pre-production workarounds is to keep the
429 * current gen workarounds as a guide to the bring-up of the next gen
430 * (workarounds have a habit of persisting!). Anything older than that
431 * should be removed along with the complications they introduce.
Paulo Zanoni4fc7e842016-09-26 15:07:52 +0300432 */
433static void intel_detect_preproduction_hw(struct drm_i915_private *dev_priv)
434{
Chris Wilson248a1242017-01-30 10:44:56 +0000435 bool pre = false;
436
437 pre |= IS_HSW_EARLY_SDV(dev_priv);
438 pre |= IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0);
Chris Wilson0102ba12017-01-30 10:44:58 +0000439 pre |= IS_BXT_REVID(dev_priv, 0, BXT_REVID_B_LAST);
Chris Wilson1aca96c2018-11-28 13:53:25 +0000440 pre |= IS_KBL_REVID(dev_priv, 0, KBL_REVID_A0);
Chris Wilson248a1242017-01-30 10:44:56 +0000441
Chris Wilson7c5ff4a2017-01-30 10:44:57 +0000442 if (pre) {
Paulo Zanoni4fc7e842016-09-26 15:07:52 +0300443 DRM_ERROR("This is a pre-production stepping. "
444 "It may not be fully functional.\n");
Chris Wilson7c5ff4a2017-01-30 10:44:57 +0000445 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK);
446 }
Paulo Zanoni4fc7e842016-09-26 15:07:52 +0300447}
448
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -0700449static int vlv_alloc_s0ix_state(struct drm_i915_private *i915)
450{
451 if (!IS_VALLEYVIEW(i915))
452 return 0;
453
454 /* we write all the values in the struct, so no need to zero it out */
455 i915->vlv_s0ix_state = kmalloc(sizeof(*i915->vlv_s0ix_state),
456 GFP_KERNEL);
457 if (!i915->vlv_s0ix_state)
458 return -ENOMEM;
459
460 return 0;
461}
462
463static void vlv_free_s0ix_state(struct drm_i915_private *i915)
464{
465 if (!i915->vlv_s0ix_state)
466 return;
467
468 kfree(i915->vlv_s0ix_state);
469 i915->vlv_s0ix_state = NULL;
470}
471
Chris Wilson0673ad42016-06-24 14:00:22 +0100472/**
Janusz Krzysztofik0b61b8b2019-07-12 13:24:30 +0200473 * i915_driver_early_probe - setup state not requiring device access
Chris Wilson0673ad42016-06-24 14:00:22 +0100474 * @dev_priv: device private
475 *
476 * Initialize everything that is a "SW-only" state, that is state not
477 * requiring accessing the device or exposing the driver via kernel internal
478 * or userspace interfaces. Example steps belonging here: lock initialization,
479 * system memory allocation, setting up device specific attributes and
480 * function hooks not requiring accessing the device.
481 */
Janusz Krzysztofik0b61b8b2019-07-12 13:24:30 +0200482static int i915_driver_early_probe(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100483{
Chris Wilson0673ad42016-06-24 14:00:22 +0100484 int ret = 0;
485
Michal Wajdeczko50d84412019-08-02 18:40:50 +0000486 if (i915_inject_probe_failure(dev_priv))
Chris Wilson0673ad42016-06-24 14:00:22 +0100487 return -ENODEV;
488
Tvrtko Ursulin805446c2019-03-27 14:23:28 +0000489 intel_device_info_subplatform_init(dev_priv);
490
Daniele Ceraolo Spurio0a9b2632019-08-09 07:31:16 +0100491 intel_uncore_mmio_debug_init_early(&dev_priv->mmio_debug);
Daniele Ceraolo Spurio01385752019-06-19 18:00:18 -0700492 intel_uncore_init_early(&dev_priv->uncore, dev_priv);
Daniele Ceraolo Spurio6cbe88302019-04-02 13:10:31 -0700493
Chris Wilson0673ad42016-06-24 14:00:22 +0100494 spin_lock_init(&dev_priv->irq_lock);
495 spin_lock_init(&dev_priv->gpu_error.lock);
496 mutex_init(&dev_priv->backlight_lock);
Lyude317eaa92017-02-03 21:18:25 -0500497
Chris Wilson0673ad42016-06-24 14:00:22 +0100498 mutex_init(&dev_priv->sb_lock);
Chris Wilsona75d0352019-04-26 09:17:18 +0100499 pm_qos_add_request(&dev_priv->sb_qos,
500 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
501
Chris Wilson0673ad42016-06-24 14:00:22 +0100502 mutex_init(&dev_priv->av_mutex);
503 mutex_init(&dev_priv->wm.wm_mutex);
504 mutex_init(&dev_priv->pps_mutex);
Ramalingam C9055aac2019-02-16 23:06:51 +0530505 mutex_init(&dev_priv->hdcp_comp_mutex);
Chris Wilson0673ad42016-06-24 14:00:22 +0100506
Chris Wilson0b1de5d2016-08-12 12:39:59 +0100507 i915_memcpy_init_early(dev_priv);
Daniele Ceraolo Spurio69c66352019-06-13 16:21:53 -0700508 intel_runtime_pm_init_early(&dev_priv->runtime_pm);
Chris Wilson0b1de5d2016-08-12 12:39:59 +0100509
Chris Wilson0673ad42016-06-24 14:00:22 +0100510 ret = i915_workqueues_init(dev_priv);
511 if (ret < 0)
Chris Wilsonf3bcb0c2019-07-18 08:00:10 +0100512 return ret;
Chris Wilson0673ad42016-06-24 14:00:22 +0100513
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -0700514 ret = vlv_alloc_s0ix_state(dev_priv);
515 if (ret < 0)
516 goto err_workqueues;
517
Daniele Ceraolo Spurio6f760982019-07-31 17:57:08 -0700518 intel_wopcm_init_early(&dev_priv->wopcm);
519
Tvrtko Ursulin724e9562019-06-21 08:07:42 +0100520 intel_gt_init_early(&dev_priv->gt, dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100521
Matthew Aulda3f356b2019-09-27 18:33:49 +0100522 i915_gem_init_early(dev_priv);
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +0000523
Chris Wilson0673ad42016-06-24 14:00:22 +0100524 /* This must be called before any calls to HAS_PCH_* */
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000525 intel_detect_pch(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100526
Tvrtko Ursulin192aa182016-12-01 14:16:45 +0000527 intel_pm_setup(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100528 intel_init_dpio(dev_priv);
Imre Deakf28ec6f2018-08-06 12:58:37 +0300529 ret = intel_power_domains_init(dev_priv);
530 if (ret < 0)
Daniele Ceraolo Spurio6f760982019-07-31 17:57:08 -0700531 goto err_gem;
Chris Wilson0673ad42016-06-24 14:00:22 +0100532 intel_irq_init(dev_priv);
533 intel_init_display_hooks(dev_priv);
534 intel_init_clock_gating_hooks(dev_priv);
535 intel_init_audio_hooks(dev_priv);
David Weinehall36cdd012016-08-22 13:59:31 +0300536 intel_display_crc_init(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100537
Paulo Zanoni4fc7e842016-09-26 15:07:52 +0300538 intel_detect_preproduction_hw(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100539
540 return 0;
541
Daniele Ceraolo Spurio6f760982019-07-31 17:57:08 -0700542err_gem:
Imre Deakf28ec6f2018-08-06 12:58:37 +0300543 i915_gem_cleanup_early(dev_priv);
Daniele Ceraolo Spurio6cf72db2019-07-31 17:57:07 -0700544 intel_gt_driver_late_release(&dev_priv->gt);
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -0700545 vlv_free_s0ix_state(dev_priv);
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +0000546err_workqueues:
Chris Wilson0673ad42016-06-24 14:00:22 +0100547 i915_workqueues_cleanup(dev_priv);
548 return ret;
549}
550
551/**
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +0200552 * i915_driver_late_release - cleanup the setup done in
Janusz Krzysztofik0b61b8b2019-07-12 13:24:30 +0200553 * i915_driver_early_probe()
Chris Wilson0673ad42016-06-24 14:00:22 +0100554 * @dev_priv: device private
555 */
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +0200556static void i915_driver_late_release(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100557{
Joonas Lahtinencefcff82017-04-28 10:58:39 +0300558 intel_irq_fini(dev_priv);
Imre Deakf28ec6f2018-08-06 12:58:37 +0300559 intel_power_domains_cleanup(dev_priv);
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +0000560 i915_gem_cleanup_early(dev_priv);
Daniele Ceraolo Spurio6cf72db2019-07-31 17:57:07 -0700561 intel_gt_driver_late_release(&dev_priv->gt);
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -0700562 vlv_free_s0ix_state(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +0100563 i915_workqueues_cleanup(dev_priv);
Chris Wilsona75d0352019-04-26 09:17:18 +0100564
565 pm_qos_remove_request(&dev_priv->sb_qos);
566 mutex_destroy(&dev_priv->sb_lock);
Chris Wilson0673ad42016-06-24 14:00:22 +0100567}
568
Chris Wilson0673ad42016-06-24 14:00:22 +0100569/**
Janusz Krzysztofik0b61b8b2019-07-12 13:24:30 +0200570 * i915_driver_mmio_probe - setup device MMIO
Chris Wilson0673ad42016-06-24 14:00:22 +0100571 * @dev_priv: device private
572 *
573 * Setup minimal device state necessary for MMIO accesses later in the
574 * initialization sequence. The setup here should avoid any other device-wide
575 * side effects or exposing the driver via kernel internal or user space
576 * interfaces.
577 */
Janusz Krzysztofik0b61b8b2019-07-12 13:24:30 +0200578static int i915_driver_mmio_probe(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100579{
Chris Wilson0673ad42016-06-24 14:00:22 +0100580 int ret;
581
Michal Wajdeczko50d84412019-08-02 18:40:50 +0000582 if (i915_inject_probe_failure(dev_priv))
Chris Wilson0673ad42016-06-24 14:00:22 +0100583 return -ENODEV;
584
Tvrtko Ursulinda5f53b2016-12-01 14:16:40 +0000585 if (i915_get_bridge_dev(dev_priv))
Chris Wilson0673ad42016-06-24 14:00:22 +0100586 return -EIO;
587
Daniele Ceraolo Spurio3de6f852019-04-02 13:10:32 -0700588 ret = intel_uncore_init_mmio(&dev_priv->uncore);
Chris Wilson0673ad42016-06-24 14:00:22 +0100589 if (ret < 0)
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300590 goto err_bridge;
Chris Wilson0673ad42016-06-24 14:00:22 +0100591
Daniele Ceraolo Spurio25286aa2019-03-19 11:35:40 -0700592 /* Try to make sure MCHBAR is enabled before poking at it */
593 intel_setup_mchbar(dev_priv);
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300594
Oscar Mateo26376a72018-03-16 14:14:49 +0200595 intel_device_info_init_mmio(dev_priv);
596
Daniele Ceraolo Spurio3de6f852019-04-02 13:10:32 -0700597 intel_uncore_prune_mmio_domains(&dev_priv->uncore);
Oscar Mateo26376a72018-03-16 14:14:49 +0200598
Daniele Ceraolo Spurioca7b2c12019-07-13 11:00:13 +0100599 intel_uc_init_mmio(&dev_priv->gt.uc);
Sagar Arun Kamble1fc556f2017-10-04 15:33:24 +0000600
Tvrtko Ursulinadcb5262019-10-22 10:47:15 +0100601 ret = intel_engines_init_mmio(&dev_priv->gt);
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300602 if (ret)
603 goto err_uncore;
604
Chris Wilson0673ad42016-06-24 14:00:22 +0100605 return 0;
606
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300607err_uncore:
Daniele Ceraolo Spurio25286aa2019-03-19 11:35:40 -0700608 intel_teardown_mchbar(dev_priv);
Daniele Ceraolo Spurio3de6f852019-04-02 13:10:32 -0700609 intel_uncore_fini_mmio(&dev_priv->uncore);
Joonas Lahtinen63ffbcd2017-04-28 10:53:36 +0300610err_bridge:
Chris Wilson0673ad42016-06-24 14:00:22 +0100611 pci_dev_put(dev_priv->bridge_dev);
612
613 return ret;
614}
615
616/**
Janusz Krzysztofik0b61b8b2019-07-12 13:24:30 +0200617 * i915_driver_mmio_release - cleanup the setup done in i915_driver_mmio_probe()
Chris Wilson0673ad42016-06-24 14:00:22 +0100618 * @dev_priv: device private
619 */
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +0200620static void i915_driver_mmio_release(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +0100621{
Daniele Ceraolo Spurio25286aa2019-03-19 11:35:40 -0700622 intel_teardown_mchbar(dev_priv);
Daniele Ceraolo Spurio3de6f852019-04-02 13:10:32 -0700623 intel_uncore_fini_mmio(&dev_priv->uncore);
Chris Wilson0673ad42016-06-24 14:00:22 +0100624 pci_dev_put(dev_priv->bridge_dev);
625}
626
Chris Wilson94b4f3b2016-07-05 10:40:20 +0100627static void intel_sanitize_options(struct drm_i915_private *dev_priv)
628{
Chuanxiao Dong67b7f332017-05-27 17:44:17 +0800629 intel_gvt_sanitize_options(dev_priv);
Chris Wilson94b4f3b2016-07-05 10:40:20 +0100630}
631
Ville Syrjäläb185a352019-03-06 22:35:51 +0200632#define DRAM_TYPE_STR(type) [INTEL_DRAM_ ## type] = #type
633
634static const char *intel_dram_type_str(enum intel_dram_type type)
635{
636 static const char * const str[] = {
637 DRAM_TYPE_STR(UNKNOWN),
638 DRAM_TYPE_STR(DDR3),
639 DRAM_TYPE_STR(DDR4),
640 DRAM_TYPE_STR(LPDDR3),
641 DRAM_TYPE_STR(LPDDR4),
642 };
643
644 if (type >= ARRAY_SIZE(str))
645 type = INTEL_DRAM_UNKNOWN;
646
647 return str[type];
648}
649
650#undef DRAM_TYPE_STR
651
Ville Syrjälä54561b22019-03-06 22:35:42 +0200652static int intel_dimm_num_devices(const struct dram_dimm_info *dimm)
653{
654 return dimm->ranks * 64 / (dimm->width ?: 1);
655}
656
Ville Syrjäläea411e62019-03-06 22:35:41 +0200657/* Returns total GB for the whole DIMM */
658static int skl_get_dimm_size(u16 val)
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530659{
Ville Syrjäläea411e62019-03-06 22:35:41 +0200660 return val & SKL_DRAM_SIZE_MASK;
661}
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530662
Ville Syrjäläea411e62019-03-06 22:35:41 +0200663static int skl_get_dimm_width(u16 val)
664{
665 if (skl_get_dimm_size(val) == 0)
666 return 0;
667
668 switch (val & SKL_DRAM_WIDTH_MASK) {
669 case SKL_DRAM_WIDTH_X8:
670 case SKL_DRAM_WIDTH_X16:
671 case SKL_DRAM_WIDTH_X32:
672 val = (val & SKL_DRAM_WIDTH_MASK) >> SKL_DRAM_WIDTH_SHIFT;
673 return 8 << val;
674 default:
675 MISSING_CASE(val);
676 return 0;
677 }
678}
679
680static int skl_get_dimm_ranks(u16 val)
681{
682 if (skl_get_dimm_size(val) == 0)
683 return 0;
684
685 val = (val & SKL_DRAM_RANK_MASK) >> SKL_DRAM_RANK_SHIFT;
686
687 return val + 1;
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530688}
689
Ville Syrjälä6d9c1e92019-03-06 22:35:50 +0200690/* Returns total GB for the whole DIMM */
691static int cnl_get_dimm_size(u16 val)
692{
693 return (val & CNL_DRAM_SIZE_MASK) / 2;
694}
695
696static int cnl_get_dimm_width(u16 val)
697{
698 if (cnl_get_dimm_size(val) == 0)
699 return 0;
700
701 switch (val & CNL_DRAM_WIDTH_MASK) {
702 case CNL_DRAM_WIDTH_X8:
703 case CNL_DRAM_WIDTH_X16:
704 case CNL_DRAM_WIDTH_X32:
705 val = (val & CNL_DRAM_WIDTH_MASK) >> CNL_DRAM_WIDTH_SHIFT;
706 return 8 << val;
707 default:
708 MISSING_CASE(val);
709 return 0;
710 }
711}
712
713static int cnl_get_dimm_ranks(u16 val)
714{
715 if (cnl_get_dimm_size(val) == 0)
716 return 0;
717
718 val = (val & CNL_DRAM_RANK_MASK) >> CNL_DRAM_RANK_SHIFT;
719
720 return val + 1;
721}
722
Mahesh Kumar86b59282018-08-31 16:39:42 +0530723static bool
Ville Syrjälä54561b22019-03-06 22:35:42 +0200724skl_is_16gb_dimm(const struct dram_dimm_info *dimm)
Mahesh Kumar86b59282018-08-31 16:39:42 +0530725{
Ville Syrjälä54561b22019-03-06 22:35:42 +0200726 /* Convert total GB to Gb per DRAM device */
727 return 8 * dimm->size / (intel_dimm_num_devices(dimm) ?: 1) == 16;
Mahesh Kumar86b59282018-08-31 16:39:42 +0530728}
729
Ville Syrjälä198b8dd2019-03-06 22:35:46 +0200730static void
Ville Syrjälä6d9c1e92019-03-06 22:35:50 +0200731skl_dram_get_dimm_info(struct drm_i915_private *dev_priv,
732 struct dram_dimm_info *dimm,
Ville Syrjälä198b8dd2019-03-06 22:35:46 +0200733 int channel, char dimm_name, u16 val)
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530734{
Ville Syrjälä6d9c1e92019-03-06 22:35:50 +0200735 if (INTEL_GEN(dev_priv) >= 10) {
736 dimm->size = cnl_get_dimm_size(val);
737 dimm->width = cnl_get_dimm_width(val);
738 dimm->ranks = cnl_get_dimm_ranks(val);
739 } else {
740 dimm->size = skl_get_dimm_size(val);
741 dimm->width = skl_get_dimm_width(val);
742 dimm->ranks = skl_get_dimm_ranks(val);
743 }
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530744
Ville Syrjälä198b8dd2019-03-06 22:35:46 +0200745 DRM_DEBUG_KMS("CH%u DIMM %c size: %u GB, width: X%u, ranks: %u, 16Gb DIMMs: %s\n",
746 channel, dimm_name, dimm->size, dimm->width, dimm->ranks,
747 yesno(skl_is_16gb_dimm(dimm)));
748}
Ville Syrjäläea411e62019-03-06 22:35:41 +0200749
Ville Syrjälä198b8dd2019-03-06 22:35:46 +0200750static int
Ville Syrjälä6d9c1e92019-03-06 22:35:50 +0200751skl_dram_get_channel_info(struct drm_i915_private *dev_priv,
752 struct dram_channel_info *ch,
Ville Syrjälä198b8dd2019-03-06 22:35:46 +0200753 int channel, u32 val)
754{
Ville Syrjälä6d9c1e92019-03-06 22:35:50 +0200755 skl_dram_get_dimm_info(dev_priv, &ch->dimm_l,
756 channel, 'L', val & 0xffff);
757 skl_dram_get_dimm_info(dev_priv, &ch->dimm_s,
758 channel, 'S', val >> 16);
Ville Syrjäläea411e62019-03-06 22:35:41 +0200759
Ville Syrjälä1d559672019-03-06 22:35:48 +0200760 if (ch->dimm_l.size == 0 && ch->dimm_s.size == 0) {
Ville Syrjälä198b8dd2019-03-06 22:35:46 +0200761 DRM_DEBUG_KMS("CH%u not populated\n", channel);
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530762 return -EINVAL;
Ville Syrjälä198b8dd2019-03-06 22:35:46 +0200763 }
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530764
Ville Syrjälä1d559672019-03-06 22:35:48 +0200765 if (ch->dimm_l.ranks == 2 || ch->dimm_s.ranks == 2)
Ville Syrjälä80373fb2019-03-06 22:35:40 +0200766 ch->ranks = 2;
Ville Syrjälä1d559672019-03-06 22:35:48 +0200767 else if (ch->dimm_l.ranks == 1 && ch->dimm_s.ranks == 1)
Ville Syrjälä80373fb2019-03-06 22:35:40 +0200768 ch->ranks = 2;
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530769 else
Ville Syrjälä80373fb2019-03-06 22:35:40 +0200770 ch->ranks = 1;
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530771
Ville Syrjälä54561b22019-03-06 22:35:42 +0200772 ch->is_16gb_dimm =
Ville Syrjälä1d559672019-03-06 22:35:48 +0200773 skl_is_16gb_dimm(&ch->dimm_l) ||
774 skl_is_16gb_dimm(&ch->dimm_s);
Mahesh Kumar86b59282018-08-31 16:39:42 +0530775
Ville Syrjälä198b8dd2019-03-06 22:35:46 +0200776 DRM_DEBUG_KMS("CH%u ranks: %u, 16Gb DIMMs: %s\n",
777 channel, ch->ranks, yesno(ch->is_16gb_dimm));
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530778
779 return 0;
780}
781
Mahesh Kumar8a6c5442018-08-24 15:02:25 +0530782static bool
Ville Syrjäläd75434b2019-03-06 22:35:47 +0200783intel_is_dram_symmetric(const struct dram_channel_info *ch0,
784 const struct dram_channel_info *ch1)
Mahesh Kumar8a6c5442018-08-24 15:02:25 +0530785{
Ville Syrjäläd75434b2019-03-06 22:35:47 +0200786 return !memcmp(ch0, ch1, sizeof(*ch0)) &&
Ville Syrjälä1d559672019-03-06 22:35:48 +0200787 (ch0->dimm_s.size == 0 ||
788 !memcmp(&ch0->dimm_l, &ch0->dimm_s, sizeof(ch0->dimm_l)));
Mahesh Kumar8a6c5442018-08-24 15:02:25 +0530789}
790
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530791static int
792skl_dram_get_channels_info(struct drm_i915_private *dev_priv)
793{
794 struct dram_info *dram_info = &dev_priv->dram_info;
Ville Syrjälä198b8dd2019-03-06 22:35:46 +0200795 struct dram_channel_info ch0 = {}, ch1 = {};
Ville Syrjäläd75434b2019-03-06 22:35:47 +0200796 u32 val;
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530797 int ret;
798
Ville Syrjäläd75434b2019-03-06 22:35:47 +0200799 val = I915_READ(SKL_MAD_DIMM_CH0_0_0_0_MCHBAR_MCMAIN);
Ville Syrjälä6d9c1e92019-03-06 22:35:50 +0200800 ret = skl_dram_get_channel_info(dev_priv, &ch0, 0, val);
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530801 if (ret == 0)
802 dram_info->num_channels++;
803
Ville Syrjäläd75434b2019-03-06 22:35:47 +0200804 val = I915_READ(SKL_MAD_DIMM_CH1_0_0_0_MCHBAR_MCMAIN);
Ville Syrjälä6d9c1e92019-03-06 22:35:50 +0200805 ret = skl_dram_get_channel_info(dev_priv, &ch1, 1, val);
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530806 if (ret == 0)
807 dram_info->num_channels++;
808
809 if (dram_info->num_channels == 0) {
810 DRM_INFO("Number of memory channels is zero\n");
811 return -EINVAL;
812 }
813
814 /*
815 * If any of the channel is single rank channel, worst case output
816 * will be same as if single rank memory, so consider single rank
817 * memory.
818 */
Ville Syrjälä80373fb2019-03-06 22:35:40 +0200819 if (ch0.ranks == 1 || ch1.ranks == 1)
820 dram_info->ranks = 1;
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530821 else
Ville Syrjälä80373fb2019-03-06 22:35:40 +0200822 dram_info->ranks = max(ch0.ranks, ch1.ranks);
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530823
Ville Syrjälä80373fb2019-03-06 22:35:40 +0200824 if (dram_info->ranks == 0) {
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530825 DRM_INFO("couldn't get memory rank information\n");
826 return -EINVAL;
827 }
Mahesh Kumar86b59282018-08-31 16:39:42 +0530828
Ville Syrjälä5d6f36b2018-10-23 21:21:02 +0300829 dram_info->is_16gb_dimm = ch0.is_16gb_dimm || ch1.is_16gb_dimm;
Mahesh Kumar86b59282018-08-31 16:39:42 +0530830
Ville Syrjäläd75434b2019-03-06 22:35:47 +0200831 dram_info->symmetric_memory = intel_is_dram_symmetric(&ch0, &ch1);
Mahesh Kumar8a6c5442018-08-24 15:02:25 +0530832
Ville Syrjäläd75434b2019-03-06 22:35:47 +0200833 DRM_DEBUG_KMS("Memory configuration is symmetric? %s\n",
834 yesno(dram_info->symmetric_memory));
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530835 return 0;
836}
837
Ville Syrjäläb185a352019-03-06 22:35:51 +0200838static enum intel_dram_type
839skl_get_dram_type(struct drm_i915_private *dev_priv)
840{
841 u32 val;
842
843 val = I915_READ(SKL_MAD_INTER_CHANNEL_0_0_0_MCHBAR_MCMAIN);
844
845 switch (val & SKL_DRAM_DDR_TYPE_MASK) {
846 case SKL_DRAM_DDR_TYPE_DDR3:
847 return INTEL_DRAM_DDR3;
848 case SKL_DRAM_DDR_TYPE_DDR4:
849 return INTEL_DRAM_DDR4;
850 case SKL_DRAM_DDR_TYPE_LPDDR3:
851 return INTEL_DRAM_LPDDR3;
852 case SKL_DRAM_DDR_TYPE_LPDDR4:
853 return INTEL_DRAM_LPDDR4;
854 default:
855 MISSING_CASE(val);
856 return INTEL_DRAM_UNKNOWN;
857 }
858}
859
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530860static int
861skl_get_dram_info(struct drm_i915_private *dev_priv)
862{
863 struct dram_info *dram_info = &dev_priv->dram_info;
864 u32 mem_freq_khz, val;
865 int ret;
866
Ville Syrjäläb185a352019-03-06 22:35:51 +0200867 dram_info->type = skl_get_dram_type(dev_priv);
868 DRM_DEBUG_KMS("DRAM type: %s\n", intel_dram_type_str(dram_info->type));
869
Mahesh Kumar5771caf2018-08-24 15:02:22 +0530870 ret = skl_dram_get_channels_info(dev_priv);
871 if (ret)
872 return ret;
873
874 val = I915_READ(SKL_MC_BIOS_DATA_0_0_0_MCHBAR_PCU);
875 mem_freq_khz = DIV_ROUND_UP((val & SKL_REQ_DATA_MASK) *
876 SKL_MEMORY_FREQ_MULTIPLIER_HZ, 1000);
877
878 dram_info->bandwidth_kbps = dram_info->num_channels *
879 mem_freq_khz * 8;
880
881 if (dram_info->bandwidth_kbps == 0) {
882 DRM_INFO("Couldn't get system memory bandwidth\n");
883 return -EINVAL;
884 }
885
886 dram_info->valid = true;
887 return 0;
888}
889
Ville Syrjäläa62819a2019-03-06 22:35:43 +0200890/* Returns Gb per DRAM device */
891static int bxt_get_dimm_size(u32 val)
892{
893 switch (val & BXT_DRAM_SIZE_MASK) {
Ville Syrjälä88603432019-03-06 22:35:44 +0200894 case BXT_DRAM_SIZE_4GBIT:
Ville Syrjäläa62819a2019-03-06 22:35:43 +0200895 return 4;
Ville Syrjälä88603432019-03-06 22:35:44 +0200896 case BXT_DRAM_SIZE_6GBIT:
Ville Syrjäläa62819a2019-03-06 22:35:43 +0200897 return 6;
Ville Syrjälä88603432019-03-06 22:35:44 +0200898 case BXT_DRAM_SIZE_8GBIT:
Ville Syrjäläa62819a2019-03-06 22:35:43 +0200899 return 8;
Ville Syrjälä88603432019-03-06 22:35:44 +0200900 case BXT_DRAM_SIZE_12GBIT:
Ville Syrjäläa62819a2019-03-06 22:35:43 +0200901 return 12;
Ville Syrjälä88603432019-03-06 22:35:44 +0200902 case BXT_DRAM_SIZE_16GBIT:
Ville Syrjäläa62819a2019-03-06 22:35:43 +0200903 return 16;
904 default:
905 MISSING_CASE(val);
906 return 0;
907 }
908}
909
910static int bxt_get_dimm_width(u32 val)
911{
912 if (!bxt_get_dimm_size(val))
913 return 0;
914
915 val = (val & BXT_DRAM_WIDTH_MASK) >> BXT_DRAM_WIDTH_SHIFT;
916
917 return 8 << val;
918}
919
920static int bxt_get_dimm_ranks(u32 val)
921{
922 if (!bxt_get_dimm_size(val))
923 return 0;
924
925 switch (val & BXT_DRAM_RANK_MASK) {
926 case BXT_DRAM_RANK_SINGLE:
927 return 1;
928 case BXT_DRAM_RANK_DUAL:
929 return 2;
930 default:
931 MISSING_CASE(val);
932 return 0;
933 }
934}
935
Ville Syrjäläb185a352019-03-06 22:35:51 +0200936static enum intel_dram_type bxt_get_dimm_type(u32 val)
937{
938 if (!bxt_get_dimm_size(val))
939 return INTEL_DRAM_UNKNOWN;
940
941 switch (val & BXT_DRAM_TYPE_MASK) {
942 case BXT_DRAM_TYPE_DDR3:
943 return INTEL_DRAM_DDR3;
944 case BXT_DRAM_TYPE_LPDDR3:
945 return INTEL_DRAM_LPDDR3;
946 case BXT_DRAM_TYPE_DDR4:
947 return INTEL_DRAM_DDR4;
948 case BXT_DRAM_TYPE_LPDDR4:
949 return INTEL_DRAM_LPDDR4;
950 default:
951 MISSING_CASE(val);
952 return INTEL_DRAM_UNKNOWN;
953 }
954}
955
Ville Syrjäläa62819a2019-03-06 22:35:43 +0200956static void bxt_get_dimm_info(struct dram_dimm_info *dimm,
957 u32 val)
958{
Ville Syrjäläa62819a2019-03-06 22:35:43 +0200959 dimm->width = bxt_get_dimm_width(val);
960 dimm->ranks = bxt_get_dimm_ranks(val);
Ville Syrjälä88603432019-03-06 22:35:44 +0200961
962 /*
963 * Size in register is Gb per DRAM device. Convert to total
964 * GB to match the way we report this for non-LP platforms.
965 */
966 dimm->size = bxt_get_dimm_size(val) * intel_dimm_num_devices(dimm) / 8;
Ville Syrjäläa62819a2019-03-06 22:35:43 +0200967}
968
Mahesh Kumarcbfa59d2018-08-24 15:02:21 +0530969static int
970bxt_get_dram_info(struct drm_i915_private *dev_priv)
971{
972 struct dram_info *dram_info = &dev_priv->dram_info;
973 u32 dram_channels;
974 u32 mem_freq_khz, val;
975 u8 num_active_channels;
976 int i;
977
978 val = I915_READ(BXT_P_CR_MC_BIOS_REQ_0_0_0);
979 mem_freq_khz = DIV_ROUND_UP((val & BXT_REQ_DATA_MASK) *
980 BXT_MEMORY_FREQ_MULTIPLIER_HZ, 1000);
981
982 dram_channels = val & BXT_DRAM_CHANNEL_ACTIVE_MASK;
983 num_active_channels = hweight32(dram_channels);
984
985 /* Each active bit represents 4-byte channel */
986 dram_info->bandwidth_kbps = (mem_freq_khz * num_active_channels * 4);
987
988 if (dram_info->bandwidth_kbps == 0) {
989 DRM_INFO("Couldn't get system memory bandwidth\n");
990 return -EINVAL;
991 }
992
993 /*
994 * Now read each DUNIT8/9/10/11 to check the rank of each dimms.
995 */
996 for (i = BXT_D_CR_DRP0_DUNIT_START; i <= BXT_D_CR_DRP0_DUNIT_END; i++) {
Ville Syrjäläa62819a2019-03-06 22:35:43 +0200997 struct dram_dimm_info dimm;
Ville Syrjäläb185a352019-03-06 22:35:51 +0200998 enum intel_dram_type type;
Mahesh Kumarcbfa59d2018-08-24 15:02:21 +0530999
1000 val = I915_READ(BXT_D_CR_DRP0_DUNIT(i));
1001 if (val == 0xFFFFFFFF)
1002 continue;
1003
1004 dram_info->num_channels++;
Mahesh Kumarcbfa59d2018-08-24 15:02:21 +05301005
Ville Syrjäläa62819a2019-03-06 22:35:43 +02001006 bxt_get_dimm_info(&dimm, val);
Ville Syrjäläb185a352019-03-06 22:35:51 +02001007 type = bxt_get_dimm_type(val);
Mahesh Kumarcbfa59d2018-08-24 15:02:21 +05301008
Ville Syrjäläb185a352019-03-06 22:35:51 +02001009 WARN_ON(type != INTEL_DRAM_UNKNOWN &&
1010 dram_info->type != INTEL_DRAM_UNKNOWN &&
1011 dram_info->type != type);
1012
1013 DRM_DEBUG_KMS("CH%u DIMM size: %u GB, width: X%u, ranks: %u, type: %s\n",
Ville Syrjäläa62819a2019-03-06 22:35:43 +02001014 i - BXT_D_CR_DRP0_DUNIT_START,
Ville Syrjäläb185a352019-03-06 22:35:51 +02001015 dimm.size, dimm.width, dimm.ranks,
1016 intel_dram_type_str(type));
Mahesh Kumarcbfa59d2018-08-24 15:02:21 +05301017
1018 /*
1019 * If any of the channel is single rank channel,
1020 * worst case output will be same as if single rank
1021 * memory, so consider single rank memory.
1022 */
Ville Syrjälä80373fb2019-03-06 22:35:40 +02001023 if (dram_info->ranks == 0)
Ville Syrjäläa62819a2019-03-06 22:35:43 +02001024 dram_info->ranks = dimm.ranks;
1025 else if (dimm.ranks == 1)
Ville Syrjälä80373fb2019-03-06 22:35:40 +02001026 dram_info->ranks = 1;
Ville Syrjäläb185a352019-03-06 22:35:51 +02001027
1028 if (type != INTEL_DRAM_UNKNOWN)
1029 dram_info->type = type;
Mahesh Kumarcbfa59d2018-08-24 15:02:21 +05301030 }
1031
Ville Syrjäläb185a352019-03-06 22:35:51 +02001032 if (dram_info->type == INTEL_DRAM_UNKNOWN ||
1033 dram_info->ranks == 0) {
1034 DRM_INFO("couldn't get memory information\n");
Mahesh Kumarcbfa59d2018-08-24 15:02:21 +05301035 return -EINVAL;
1036 }
1037
1038 dram_info->valid = true;
1039 return 0;
1040}
1041
1042static void
1043intel_get_dram_info(struct drm_i915_private *dev_priv)
1044{
1045 struct dram_info *dram_info = &dev_priv->dram_info;
1046 int ret;
1047
Ville Syrjälä5d6f36b2018-10-23 21:21:02 +03001048 /*
1049 * Assume 16Gb DIMMs are present until proven otherwise.
1050 * This is only used for the level 0 watermark latency
1051 * w/a which does not apply to bxt/glk.
1052 */
1053 dram_info->is_16gb_dimm = !IS_GEN9_LP(dev_priv);
1054
Stuart Summerse7862f42019-11-19 16:45:05 -08001055 if (INTEL_GEN(dev_priv) < 9 || !HAS_DISPLAY(dev_priv))
Mahesh Kumarcbfa59d2018-08-24 15:02:21 +05301056 return;
1057
Ville Syrjälä331ecde2019-03-06 22:35:45 +02001058 if (IS_GEN9_LP(dev_priv))
Mahesh Kumar5771caf2018-08-24 15:02:22 +05301059 ret = bxt_get_dram_info(dev_priv);
Mahesh Kumar5771caf2018-08-24 15:02:22 +05301060 else
Ville Syrjälä6d9c1e92019-03-06 22:35:50 +02001061 ret = skl_get_dram_info(dev_priv);
Mahesh Kumarcbfa59d2018-08-24 15:02:21 +05301062 if (ret)
1063 return;
1064
Ville Syrjälä30a533e2019-03-06 22:35:49 +02001065 DRM_DEBUG_KMS("DRAM bandwidth: %u kBps, channels: %u\n",
1066 dram_info->bandwidth_kbps,
1067 dram_info->num_channels);
1068
Ville Syrjälä54561b22019-03-06 22:35:42 +02001069 DRM_DEBUG_KMS("DRAM ranks: %u, 16Gb DIMMs: %s\n",
Ville Syrjälä80373fb2019-03-06 22:35:40 +02001070 dram_info->ranks, yesno(dram_info->is_16gb_dimm));
Mahesh Kumarcbfa59d2018-08-24 15:02:21 +05301071}
1072
Daniele Ceraolo Spuriof6ac9932019-03-28 10:45:32 -07001073static u32 gen9_edram_size_mb(struct drm_i915_private *dev_priv, u32 cap)
1074{
Ville Syrjälä2edb3de2019-10-10 17:51:23 +03001075 static const u8 ways[8] = { 4, 8, 12, 16, 16, 16, 16, 16 };
1076 static const u8 sets[4] = { 1, 1, 2, 2 };
Daniele Ceraolo Spuriof6ac9932019-03-28 10:45:32 -07001077
1078 return EDRAM_NUM_BANKS(cap) *
1079 ways[EDRAM_WAYS_IDX(cap)] *
1080 sets[EDRAM_SETS_IDX(cap)];
1081}
1082
1083static void edram_detect(struct drm_i915_private *dev_priv)
1084{
1085 u32 edram_cap = 0;
1086
1087 if (!(IS_HASWELL(dev_priv) ||
1088 IS_BROADWELL(dev_priv) ||
1089 INTEL_GEN(dev_priv) >= 9))
1090 return;
1091
1092 edram_cap = __raw_uncore_read32(&dev_priv->uncore, HSW_EDRAM_CAP);
1093
1094 /* NB: We can't write IDICR yet because we don't have gt funcs set up */
1095
1096 if (!(edram_cap & EDRAM_ENABLED))
1097 return;
1098
1099 /*
1100 * The needed capability bits for size calculation are not there with
1101 * pre gen9 so return 128MB always.
1102 */
1103 if (INTEL_GEN(dev_priv) < 9)
1104 dev_priv->edram_size_mb = 128;
1105 else
1106 dev_priv->edram_size_mb =
1107 gen9_edram_size_mb(dev_priv, edram_cap);
1108
Chris Wilson88f80652019-08-15 10:36:04 +01001109 dev_info(dev_priv->drm.dev,
1110 "Found %uMB of eDRAM\n", dev_priv->edram_size_mb);
Daniele Ceraolo Spuriof6ac9932019-03-28 10:45:32 -07001111}
1112
Chris Wilson0673ad42016-06-24 14:00:22 +01001113/**
Janusz Krzysztofik0b61b8b2019-07-12 13:24:30 +02001114 * i915_driver_hw_probe - setup state requiring device access
Chris Wilson0673ad42016-06-24 14:00:22 +01001115 * @dev_priv: device private
1116 *
1117 * Setup state that requires accessing the device, but doesn't require
1118 * exposing the driver via kernel internal or userspace interfaces.
1119 */
Janusz Krzysztofik0b61b8b2019-07-12 13:24:30 +02001120static int i915_driver_hw_probe(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +01001121{
David Weinehall52a05c32016-08-22 13:32:44 +03001122 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson0673ad42016-06-24 14:00:22 +01001123 int ret;
1124
Michal Wajdeczko50d84412019-08-02 18:40:50 +00001125 if (i915_inject_probe_failure(dev_priv))
Chris Wilson0673ad42016-06-24 14:00:22 +01001126 return -ENODEV;
1127
Jani Nikula1400cc72018-12-31 16:56:43 +02001128 intel_device_info_runtime_init(dev_priv);
Chris Wilson94b4f3b2016-07-05 10:40:20 +01001129
Chris Wilson4bdafb92018-09-26 21:12:22 +01001130 if (HAS_PPGTT(dev_priv)) {
1131 if (intel_vgpu_active(dev_priv) &&
Chris Wilsonca6ac682019-03-14 22:38:35 +00001132 !intel_vgpu_has_full_ppgtt(dev_priv)) {
Chris Wilson4bdafb92018-09-26 21:12:22 +01001133 i915_report_error(dev_priv,
1134 "incompatible vGPU found, support for isolated ppGTT required\n");
1135 return -ENXIO;
1136 }
1137 }
1138
Chris Wilson46592892018-11-30 12:59:54 +00001139 if (HAS_EXECLISTS(dev_priv)) {
1140 /*
1141 * Older GVT emulation depends upon intercepting CSB mmio,
1142 * which we no longer use, preferring to use the HWSP cache
1143 * instead.
1144 */
1145 if (intel_vgpu_active(dev_priv) &&
1146 !intel_vgpu_has_hwsp_emulation(dev_priv)) {
1147 i915_report_error(dev_priv,
1148 "old vGPU host found, support for HWSP emulation required\n");
1149 return -ENXIO;
1150 }
1151 }
1152
Chris Wilson94b4f3b2016-07-05 10:40:20 +01001153 intel_sanitize_options(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001154
Daniele Ceraolo Spuriof6ac9932019-03-28 10:45:32 -07001155 /* needs to be done before ggtt probe */
1156 edram_detect(dev_priv);
1157
Lionel Landwerlin9f9b2792017-10-27 15:59:31 +01001158 i915_perf_init(dev_priv);
1159
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001160 ret = i915_ggtt_probe_hw(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001161 if (ret)
Chris Wilson9f172f62018-04-14 10:12:33 +01001162 goto err_perf;
Chris Wilson0673ad42016-06-24 14:00:22 +01001163
Gerd Hoffmannf2521f72019-08-22 11:06:45 +02001164 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "inteldrmfb");
1165 if (ret)
Chris Wilson9f172f62018-04-14 10:12:33 +01001166 goto err_ggtt;
Chris Wilson0673ad42016-06-24 14:00:22 +01001167
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001168 ret = i915_ggtt_init_hw(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01001169 if (ret)
Chris Wilson9f172f62018-04-14 10:12:33 +01001170 goto err_ggtt;
Chris Wilson0088e522016-08-04 07:52:21 +01001171
Chris Wilson3fc794f2019-10-26 21:20:32 +01001172 ret = intel_memory_regions_hw_probe(dev_priv);
1173 if (ret)
1174 goto err_ggtt;
1175
Chris Wilsonfd6fe082019-11-01 14:10:06 +00001176 intel_gt_init_hw_early(&dev_priv->gt, &dev_priv->ggtt);
Tvrtko Ursulind8a44242019-06-21 08:08:06 +01001177
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001178 ret = i915_ggtt_enable_hw(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01001179 if (ret) {
1180 DRM_ERROR("failed to enable GGTT\n");
Chris Wilson3fc794f2019-10-26 21:20:32 +01001181 goto err_mem_regions;
Chris Wilson0088e522016-08-04 07:52:21 +01001182 }
1183
David Weinehall52a05c32016-08-22 13:32:44 +03001184 pci_set_master(pdev);
Chris Wilson0673ad42016-06-24 14:00:22 +01001185
Lyude Paul32f0a982019-08-23 16:52:51 -04001186 /*
1187 * We don't have a max segment size, so set it to the max so sg's
1188 * debugging layer doesn't complain
1189 */
1190 dma_set_max_seg_size(&pdev->dev, UINT_MAX);
1191
Chris Wilson0673ad42016-06-24 14:00:22 +01001192 /* overlay on gen2 is broken and can't address above 1G */
Lucas De Marchicf819ef2018-12-12 10:10:43 -08001193 if (IS_GEN(dev_priv, 2)) {
David Weinehall52a05c32016-08-22 13:32:44 +03001194 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(30));
Chris Wilson0673ad42016-06-24 14:00:22 +01001195 if (ret) {
1196 DRM_ERROR("failed to set DMA mask\n");
1197
Chris Wilson3fc794f2019-10-26 21:20:32 +01001198 goto err_mem_regions;
Chris Wilson0673ad42016-06-24 14:00:22 +01001199 }
1200 }
1201
Chris Wilson0673ad42016-06-24 14:00:22 +01001202 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1203 * using 32bit addressing, overwriting memory if HWS is located
1204 * above 4GB.
1205 *
1206 * The documentation also mentions an issue with undefined
1207 * behaviour if any general state is accessed within a page above 4GB,
1208 * which also needs to be handled carefully.
1209 */
Jani Nikulac0f86832016-12-07 12:13:04 +02001210 if (IS_I965G(dev_priv) || IS_I965GM(dev_priv)) {
David Weinehall52a05c32016-08-22 13:32:44 +03001211 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Chris Wilson0673ad42016-06-24 14:00:22 +01001212
1213 if (ret) {
1214 DRM_ERROR("failed to set DMA mask\n");
1215
Chris Wilson3fc794f2019-10-26 21:20:32 +01001216 goto err_mem_regions;
Chris Wilson0673ad42016-06-24 14:00:22 +01001217 }
1218 }
1219
Chris Wilson0673ad42016-06-24 14:00:22 +01001220 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY,
1221 PM_QOS_DEFAULT_VALUE);
1222
Tvrtko Ursulin25d140f2018-12-03 13:33:19 +00001223 intel_gt_init_workarounds(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001224
1225 /* On the 945G/GM, the chipset reports the MSI capability on the
1226 * integrated graphics even though the support isn't actually there
1227 * according to the published specs. It doesn't appear to function
1228 * correctly in testing on 945G.
1229 * This may be a side effect of MSI having been made available for PEG
1230 * and the registers being closely associated.
1231 *
1232 * According to chipset errata, on the 965GM, MSI interrupts may
Ville Syrjäläe38c2da2017-06-26 23:30:51 +03001233 * be lost or delayed, and was defeatured. MSI interrupts seem to
1234 * get lost on g4x as well, and interrupt delivery seems to stay
1235 * properly dead afterwards. So we'll just disable them for all
1236 * pre-gen5 chipsets.
Lucas De Marchi8a29c772018-05-23 11:04:35 -07001237 *
1238 * dp aux and gmbus irq on gen4 seems to be able to generate legacy
1239 * interrupts even when in MSI mode. This results in spurious
1240 * interrupt warnings if the legacy irq no. is shared with another
1241 * device. The kernel then disables that interrupt source and so
1242 * prevents the other device from working properly.
Chris Wilson0673ad42016-06-24 14:00:22 +01001243 */
Ville Syrjäläe38c2da2017-06-26 23:30:51 +03001244 if (INTEL_GEN(dev_priv) >= 5) {
David Weinehall52a05c32016-08-22 13:32:44 +03001245 if (pci_enable_msi(pdev) < 0)
Chris Wilson0673ad42016-06-24 14:00:22 +01001246 DRM_DEBUG_DRIVER("can't enable MSI");
1247 }
1248
Zhenyu Wang26f837e2017-01-13 10:46:09 +08001249 ret = intel_gvt_init(dev_priv);
1250 if (ret)
Chris Wilson7ab87ed2018-07-10 15:38:21 +01001251 goto err_msi;
1252
1253 intel_opregion_setup(dev_priv);
Mahesh Kumarcbfa59d2018-08-24 15:02:21 +05301254 /*
1255 * Fill the dram structure to get the system raw bandwidth and
1256 * dram info. This will be used for memory latency calculation.
1257 */
1258 intel_get_dram_info(dev_priv);
1259
Ville Syrjäläc457d9c2019-05-24 18:36:14 +03001260 intel_bw_init_hw(dev_priv);
Zhenyu Wang26f837e2017-01-13 10:46:09 +08001261
Chris Wilson0673ad42016-06-24 14:00:22 +01001262 return 0;
1263
Chris Wilson7ab87ed2018-07-10 15:38:21 +01001264err_msi:
1265 if (pdev->msi_enabled)
1266 pci_disable_msi(pdev);
1267 pm_qos_remove_request(&dev_priv->pm_qos);
Chris Wilson3fc794f2019-10-26 21:20:32 +01001268err_mem_regions:
1269 intel_memory_regions_driver_release(dev_priv);
Chris Wilson9f172f62018-04-14 10:12:33 +01001270err_ggtt:
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001271 i915_ggtt_driver_release(dev_priv);
Chris Wilson9f172f62018-04-14 10:12:33 +01001272err_perf:
1273 i915_perf_fini(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001274 return ret;
1275}
1276
1277/**
Janusz Krzysztofik78dae1a2019-07-12 13:24:29 +02001278 * i915_driver_hw_remove - cleanup the setup done in i915_driver_hw_probe()
Chris Wilson0673ad42016-06-24 14:00:22 +01001279 * @dev_priv: device private
1280 */
Janusz Krzysztofik78dae1a2019-07-12 13:24:29 +02001281static void i915_driver_hw_remove(struct drm_i915_private *dev_priv)
Chris Wilson0673ad42016-06-24 14:00:22 +01001282{
David Weinehall52a05c32016-08-22 13:32:44 +03001283 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson0673ad42016-06-24 14:00:22 +01001284
Lionel Landwerlin9f9b2792017-10-27 15:59:31 +01001285 i915_perf_fini(dev_priv);
1286
David Weinehall52a05c32016-08-22 13:32:44 +03001287 if (pdev->msi_enabled)
1288 pci_disable_msi(pdev);
Chris Wilson0673ad42016-06-24 14:00:22 +01001289
1290 pm_qos_remove_request(&dev_priv->pm_qos);
Chris Wilson0673ad42016-06-24 14:00:22 +01001291}
1292
1293/**
1294 * i915_driver_register - register the driver with the rest of the system
1295 * @dev_priv: device private
1296 *
1297 * Perform any steps necessary to make the driver available via kernel
1298 * internal or userspace interfaces.
1299 */
1300static void i915_driver_register(struct drm_i915_private *dev_priv)
1301{
Chris Wilson91c8a322016-07-05 10:40:23 +01001302 struct drm_device *dev = &dev_priv->drm;
Chris Wilson0673ad42016-06-24 14:00:22 +01001303
Chris Wilsonc29579d2019-08-06 13:42:59 +01001304 i915_gem_driver_register(dev_priv);
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +00001305 i915_pmu_register(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001306
1307 /*
1308 * Notify a valid surface after modesetting,
1309 * when running inside a VM.
1310 */
1311 if (intel_vgpu_active(dev_priv))
1312 I915_WRITE(vgtif_reg(display_ready), VGT_DRV_DISPLAY_READY);
1313
1314 /* Reveal our presence to userspace */
1315 if (drm_dev_register(dev, 0) == 0) {
1316 i915_debugfs_register(dev_priv);
David Weinehall694c2822016-08-22 13:32:43 +03001317 i915_setup_sysfs(dev_priv);
Robert Bragg442b8c02016-11-07 19:49:53 +00001318
1319 /* Depends on sysfs having been initialized */
1320 i915_perf_register(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001321 } else
1322 DRM_ERROR("Failed to register driver for userspace access!\n");
1323
Jani Nikulaa2b69ea2019-09-13 13:04:07 +03001324 if (HAS_DISPLAY(dev_priv) && INTEL_DISPLAY_ENABLED(dev_priv)) {
Chris Wilson0673ad42016-06-24 14:00:22 +01001325 /* Must be done after probing outputs */
1326 intel_opregion_register(dev_priv);
1327 acpi_video_register();
1328 }
1329
Andi Shyti42014f62019-09-05 14:14:03 +03001330 intel_gt_driver_register(&dev_priv->gt);
Chris Wilson0673ad42016-06-24 14:00:22 +01001331
Jerome Anandeef57322017-01-25 04:27:49 +05301332 intel_audio_init(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001333
1334 /*
1335 * Some ports require correctly set-up hpd registers for detection to
1336 * work properly (leading to ghost connected connector status), e.g. VGA
1337 * on gm45. Hence we can only set up the initial fbdev config after hpd
1338 * irqs are fully enabled. We do it last so that the async config
1339 * cannot run before the connectors are registered.
1340 */
1341 intel_fbdev_initial_config_async(dev);
Chris Wilson448aa912017-11-28 11:01:47 +00001342
1343 /*
1344 * We need to coordinate the hotplugs with the asynchronous fbdev
1345 * configuration, for which we use the fbdev->async_cookie.
1346 */
Jani Nikulaa2b69ea2019-09-13 13:04:07 +03001347 if (HAS_DISPLAY(dev_priv) && INTEL_DISPLAY_ENABLED(dev_priv))
Chris Wilson448aa912017-11-28 11:01:47 +00001348 drm_kms_helper_poll_init(dev);
Chris Wilson07d80572018-08-16 15:37:56 +03001349
Imre Deak2cd9a682018-08-16 15:37:57 +03001350 intel_power_domains_enable(dev_priv);
Daniele Ceraolo Spurio69c66352019-06-13 16:21:53 -07001351 intel_runtime_pm_enable(&dev_priv->runtime_pm);
Chris Wilson0673ad42016-06-24 14:00:22 +01001352}
1353
1354/**
1355 * i915_driver_unregister - cleanup the registration done in i915_driver_regiser()
1356 * @dev_priv: device private
1357 */
1358static void i915_driver_unregister(struct drm_i915_private *dev_priv)
1359{
Daniele Ceraolo Spurio69c66352019-06-13 16:21:53 -07001360 intel_runtime_pm_disable(&dev_priv->runtime_pm);
Imre Deak2cd9a682018-08-16 15:37:57 +03001361 intel_power_domains_disable(dev_priv);
Chris Wilson07d80572018-08-16 15:37:56 +03001362
Daniel Vetter4f256d82017-07-15 00:46:55 +02001363 intel_fbdev_unregister(dev_priv);
Jerome Anandeef57322017-01-25 04:27:49 +05301364 intel_audio_deinit(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001365
Chris Wilson448aa912017-11-28 11:01:47 +00001366 /*
1367 * After flushing the fbdev (incl. a late async config which will
1368 * have delayed queuing of a hotplug event), then flush the hotplug
1369 * events.
1370 */
1371 drm_kms_helper_poll_fini(&dev_priv->drm);
1372
Andi Shyti42014f62019-09-05 14:14:03 +03001373 intel_gt_driver_unregister(&dev_priv->gt);
Chris Wilson0673ad42016-06-24 14:00:22 +01001374 acpi_video_unregister();
1375 intel_opregion_unregister(dev_priv);
1376
Robert Bragg442b8c02016-11-07 19:49:53 +00001377 i915_perf_unregister(dev_priv);
Tvrtko Ursulinb46a33e2017-11-21 18:18:45 +00001378 i915_pmu_unregister(dev_priv);
Robert Bragg442b8c02016-11-07 19:49:53 +00001379
David Weinehall694c2822016-08-22 13:32:43 +03001380 i915_teardown_sysfs(dev_priv);
Janusz Krzysztofikd69990e2019-04-05 15:02:34 +02001381 drm_dev_unplug(&dev_priv->drm);
Chris Wilson0673ad42016-06-24 14:00:22 +01001382
Chris Wilsonc29579d2019-08-06 13:42:59 +01001383 i915_gem_driver_unregister(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001384}
1385
Michal Wajdeczko27d558a2017-12-21 21:57:35 +00001386static void i915_welcome_messages(struct drm_i915_private *dev_priv)
1387{
Jani Nikulabdbf43d2019-10-28 12:38:15 +02001388 if (drm_debug_enabled(DRM_UT_DRIVER)) {
Michal Wajdeczko27d558a2017-12-21 21:57:35 +00001389 struct drm_printer p = drm_debug_printer("i915 device info:");
1390
Tvrtko Ursulin805446c2019-03-27 14:23:28 +00001391 drm_printf(&p, "pciid=0x%04x rev=0x%02x platform=%s (subplatform=0x%x) gen=%i\n",
Jani Nikula1787a982018-12-31 16:56:45 +02001392 INTEL_DEVID(dev_priv),
1393 INTEL_REVID(dev_priv),
1394 intel_platform_name(INTEL_INFO(dev_priv)->platform),
Tvrtko Ursulin805446c2019-03-27 14:23:28 +00001395 intel_subplatform(RUNTIME_INFO(dev_priv),
1396 INTEL_INFO(dev_priv)->platform),
Jani Nikula1787a982018-12-31 16:56:45 +02001397 INTEL_GEN(dev_priv));
1398
Chris Wilson72404972019-12-07 18:29:37 +00001399 intel_device_info_print_static(INTEL_INFO(dev_priv), &p);
1400 intel_device_info_print_runtime(RUNTIME_INFO(dev_priv), &p);
Michal Wajdeczko27d558a2017-12-21 21:57:35 +00001401 }
1402
1403 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG))
1404 DRM_INFO("DRM_I915_DEBUG enabled\n");
1405 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
1406 DRM_INFO("DRM_I915_DEBUG_GEM enabled\n");
Imre Deak6dfc4a82018-08-16 22:34:14 +03001407 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM))
1408 DRM_INFO("DRM_I915_DEBUG_RUNTIME_PM enabled\n");
Michal Wajdeczko27d558a2017-12-21 21:57:35 +00001409}
1410
Chris Wilson55ac5a12018-09-05 15:09:20 +01001411static struct drm_i915_private *
1412i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent)
1413{
1414 const struct intel_device_info *match_info =
1415 (struct intel_device_info *)ent->driver_data;
1416 struct intel_device_info *device_info;
1417 struct drm_i915_private *i915;
Andi Shyti2ddcc982018-10-02 12:20:47 +03001418 int err;
Chris Wilson55ac5a12018-09-05 15:09:20 +01001419
1420 i915 = kzalloc(sizeof(*i915), GFP_KERNEL);
1421 if (!i915)
Andi Shyti2ddcc982018-10-02 12:20:47 +03001422 return ERR_PTR(-ENOMEM);
Chris Wilson55ac5a12018-09-05 15:09:20 +01001423
Andi Shyti2ddcc982018-10-02 12:20:47 +03001424 err = drm_dev_init(&i915->drm, &driver, &pdev->dev);
1425 if (err) {
Chris Wilson55ac5a12018-09-05 15:09:20 +01001426 kfree(i915);
Andi Shyti2ddcc982018-10-02 12:20:47 +03001427 return ERR_PTR(err);
Chris Wilson55ac5a12018-09-05 15:09:20 +01001428 }
1429
Chris Wilson55ac5a12018-09-05 15:09:20 +01001430 i915->drm.dev_private = i915;
Chris Wilson361f9dc2019-08-06 08:42:19 +01001431
1432 i915->drm.pdev = pdev;
1433 pci_set_drvdata(pdev, i915);
Chris Wilson55ac5a12018-09-05 15:09:20 +01001434
1435 /* Setup the write-once "constant" device info */
1436 device_info = mkwrite_device_info(i915);
1437 memcpy(device_info, match_info, sizeof(*device_info));
Jani Nikula02584042018-12-31 16:56:41 +02001438 RUNTIME_INFO(i915)->device_id = pdev->device;
Chris Wilson55ac5a12018-09-05 15:09:20 +01001439
Chris Wilson74f6e182018-09-26 11:47:07 +01001440 BUG_ON(device_info->gen > BITS_PER_TYPE(device_info->gen_mask));
Chris Wilson55ac5a12018-09-05 15:09:20 +01001441
1442 return i915;
1443}
1444
Chris Wilson31962ca2018-09-05 15:09:21 +01001445static void i915_driver_destroy(struct drm_i915_private *i915)
1446{
1447 struct pci_dev *pdev = i915->drm.pdev;
1448
1449 drm_dev_fini(&i915->drm);
1450 kfree(i915);
1451
1452 /* And make sure we never chase our dangling pointer from pci_dev */
1453 pci_set_drvdata(pdev, NULL);
1454}
1455
Chris Wilson0673ad42016-06-24 14:00:22 +01001456/**
Janusz Krzysztofikb01558e2019-07-12 13:24:26 +02001457 * i915_driver_probe - setup chip and create an initial config
Joonas Lahtinend2ad3ae2016-11-10 15:36:34 +02001458 * @pdev: PCI device
1459 * @ent: matching PCI ID entry
Chris Wilson0673ad42016-06-24 14:00:22 +01001460 *
Janusz Krzysztofikb01558e2019-07-12 13:24:26 +02001461 * The driver probe routine has to do several things:
Chris Wilson0673ad42016-06-24 14:00:22 +01001462 * - drive output discovery via intel_modeset_init()
1463 * - initialize the memory manager
1464 * - allocate initial config memory
1465 * - setup the DRM framebuffer with the allocated memory
1466 */
Janusz Krzysztofikb01558e2019-07-12 13:24:26 +02001467int i915_driver_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Chris Wilson0673ad42016-06-24 14:00:22 +01001468{
Maarten Lankhorst8d2b47d2017-02-02 08:41:42 +01001469 const struct intel_device_info *match_info =
1470 (struct intel_device_info *)ent->driver_data;
Chris Wilson0673ad42016-06-24 14:00:22 +01001471 struct drm_i915_private *dev_priv;
1472 int ret;
1473
Chris Wilson55ac5a12018-09-05 15:09:20 +01001474 dev_priv = i915_driver_create(pdev, ent);
Andi Shyti2ddcc982018-10-02 12:20:47 +03001475 if (IS_ERR(dev_priv))
1476 return PTR_ERR(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001477
Ville Syrjälä1feb64c2018-09-13 16:16:22 +03001478 /* Disable nuclear pageflip by default on pre-ILK */
1479 if (!i915_modparams.nuclear_pageflip && match_info->gen < 5)
1480 dev_priv->drm.driver_features &= ~DRIVER_ATOMIC;
1481
Matthew Auld16292242019-10-30 17:33:20 +00001482 /*
1483 * Check if we support fake LMEM -- for now we only unleash this for
1484 * the live selftests(test-and-exit).
1485 */
Chris Wilson292a27b2019-11-01 09:51:47 +00001486#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
Matthew Auld16292242019-10-30 17:33:20 +00001487 if (IS_ENABLED(CONFIG_DRM_I915_UNSTABLE_FAKE_LMEM)) {
1488 if (INTEL_GEN(dev_priv) >= 9 && i915_selftest.live < 0 &&
1489 i915_modparams.fake_lmem_start) {
1490 mkwrite_device_info(dev_priv)->memory_regions =
1491 REGION_SMEM | REGION_LMEM | REGION_STOLEN;
1492 mkwrite_device_info(dev_priv)->is_dgfx = true;
1493 GEM_BUG_ON(!HAS_LMEM(dev_priv));
1494 GEM_BUG_ON(!IS_DGFX(dev_priv));
1495 }
1496 }
Chris Wilson292a27b2019-11-01 09:51:47 +00001497#endif
Matthew Auld16292242019-10-30 17:33:20 +00001498
Chris Wilson0673ad42016-06-24 14:00:22 +01001499 ret = pci_enable_device(pdev);
1500 if (ret)
Chris Wilsoncad36882017-02-10 16:35:21 +00001501 goto out_fini;
Chris Wilson0673ad42016-06-24 14:00:22 +01001502
Janusz Krzysztofik0b61b8b2019-07-12 13:24:30 +02001503 ret = i915_driver_early_probe(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001504 if (ret < 0)
1505 goto out_pci_disable;
1506
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07001507 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
Chris Wilson0673ad42016-06-24 14:00:22 +01001508
Daniele Ceraolo Spurio9e138ea2019-06-19 18:00:21 -07001509 i915_detect_vgpu(dev_priv);
1510
Janusz Krzysztofik0b61b8b2019-07-12 13:24:30 +02001511 ret = i915_driver_mmio_probe(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001512 if (ret < 0)
1513 goto out_runtime_pm_put;
1514
Janusz Krzysztofik0b61b8b2019-07-12 13:24:30 +02001515 ret = i915_driver_hw_probe(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001516 if (ret < 0)
1517 goto out_cleanup_mmio;
1518
Jani Nikula5bcd53a2019-09-20 21:54:17 +03001519 ret = i915_driver_modeset_probe(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001520 if (ret < 0)
Daniel Vetterbaf54382017-06-21 10:28:41 +02001521 goto out_cleanup_hw;
Chris Wilson0673ad42016-06-24 14:00:22 +01001522
1523 i915_driver_register(dev_priv);
1524
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07001525 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
Chris Wilson0673ad42016-06-24 14:00:22 +01001526
Michal Wajdeczko27d558a2017-12-21 21:57:35 +00001527 i915_welcome_messages(dev_priv);
1528
Chris Wilson0673ad42016-06-24 14:00:22 +01001529 return 0;
1530
Chris Wilson0673ad42016-06-24 14:00:22 +01001531out_cleanup_hw:
Janusz Krzysztofik78dae1a2019-07-12 13:24:29 +02001532 i915_driver_hw_remove(dev_priv);
Chris Wilson3fc794f2019-10-26 21:20:32 +01001533 intel_memory_regions_driver_release(dev_priv);
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001534 i915_ggtt_driver_release(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001535out_cleanup_mmio:
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001536 i915_driver_mmio_release(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001537out_runtime_pm_put:
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07001538 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001539 i915_driver_late_release(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001540out_pci_disable:
1541 pci_disable_device(pdev);
Chris Wilsoncad36882017-02-10 16:35:21 +00001542out_fini:
Janusz Krzysztofikf2db53f2019-07-12 13:24:27 +02001543 i915_probe_error(dev_priv, "Device initialization failed (%d)\n", ret);
Chris Wilson31962ca2018-09-05 15:09:21 +01001544 i915_driver_destroy(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001545 return ret;
1546}
1547
Chris Wilson361f9dc2019-08-06 08:42:19 +01001548void i915_driver_remove(struct drm_i915_private *i915)
Chris Wilson0673ad42016-06-24 14:00:22 +01001549{
Chris Wilson361f9dc2019-08-06 08:42:19 +01001550 disable_rpm_wakeref_asserts(&i915->runtime_pm);
Chris Wilson07d80572018-08-16 15:37:56 +03001551
Chris Wilson361f9dc2019-08-06 08:42:19 +01001552 i915_driver_unregister(i915);
Daniel Vetter99c539b2017-07-15 00:46:56 +02001553
Janusz Krzysztofik141f3762019-04-06 11:40:34 +01001554 /*
1555 * After unregistering the device to prevent any new users, cancel
1556 * all in-flight requests so that we can quickly unbind the active
1557 * resources.
1558 */
Chris Wilson361f9dc2019-08-06 08:42:19 +01001559 intel_gt_set_wedged(&i915->gt);
Janusz Krzysztofik141f3762019-04-06 11:40:34 +01001560
Chris Wilson4a8ab5e2019-01-14 14:21:29 +00001561 /* Flush any external code that still may be under the RCU lock */
1562 synchronize_rcu();
1563
Chris Wilson361f9dc2019-08-06 08:42:19 +01001564 i915_gem_suspend(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +01001565
Chris Wilson361f9dc2019-08-06 08:42:19 +01001566 drm_atomic_helper_shutdown(&i915->drm);
Maarten Lankhorsta667fb42016-12-15 15:29:44 +01001567
Chris Wilson361f9dc2019-08-06 08:42:19 +01001568 intel_gvt_driver_remove(i915);
Zhenyu Wang26f837e2017-01-13 10:46:09 +08001569
Jani Nikula2d6f6f32019-09-20 21:54:16 +03001570 i915_driver_modeset_remove(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +01001571
Chris Wilson361f9dc2019-08-06 08:42:19 +01001572 i915_reset_error_state(i915);
Chris Wilson361f9dc2019-08-06 08:42:19 +01001573 i915_gem_driver_remove(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +01001574
Chris Wilson361f9dc2019-08-06 08:42:19 +01001575 intel_power_domains_driver_remove(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +01001576
Chris Wilson361f9dc2019-08-06 08:42:19 +01001577 i915_driver_hw_remove(i915);
Chris Wilson0673ad42016-06-24 14:00:22 +01001578
Chris Wilson361f9dc2019-08-06 08:42:19 +01001579 enable_rpm_wakeref_asserts(&i915->runtime_pm);
Chris Wilsoncad36882017-02-10 16:35:21 +00001580}
1581
1582static void i915_driver_release(struct drm_device *dev)
1583{
1584 struct drm_i915_private *dev_priv = to_i915(dev);
Daniele Ceraolo Spurio69c66352019-06-13 16:21:53 -07001585 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
Chris Wilson0673ad42016-06-24 14:00:22 +01001586
Daniele Ceraolo Spurio69c66352019-06-13 16:21:53 -07001587 disable_rpm_wakeref_asserts(rpm);
Janusz Krzysztofik47bc28d2019-05-30 15:31:05 +02001588
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001589 i915_gem_driver_release(dev_priv);
Janusz Krzysztofik47bc28d2019-05-30 15:31:05 +02001590
Chris Wilson3fc794f2019-10-26 21:20:32 +01001591 intel_memory_regions_driver_release(dev_priv);
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001592 i915_ggtt_driver_release(dev_priv);
Daniele Ceraolo Spurio19e0a8d2019-06-19 18:00:17 -07001593
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001594 i915_driver_mmio_release(dev_priv);
Janusz Krzysztofik47bc28d2019-05-30 15:31:05 +02001595
Daniele Ceraolo Spurio69c66352019-06-13 16:21:53 -07001596 enable_rpm_wakeref_asserts(rpm);
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001597 intel_runtime_pm_driver_release(rpm);
Janusz Krzysztofik47bc28d2019-05-30 15:31:05 +02001598
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001599 i915_driver_late_release(dev_priv);
Chris Wilson31962ca2018-09-05 15:09:21 +01001600 i915_driver_destroy(dev_priv);
Chris Wilson0673ad42016-06-24 14:00:22 +01001601}
1602
1603static int i915_driver_open(struct drm_device *dev, struct drm_file *file)
1604{
Chris Wilson829a0af2017-06-20 12:05:45 +01001605 struct drm_i915_private *i915 = to_i915(dev);
Chris Wilson0673ad42016-06-24 14:00:22 +01001606 int ret;
1607
Chris Wilson829a0af2017-06-20 12:05:45 +01001608 ret = i915_gem_open(i915, file);
Chris Wilson0673ad42016-06-24 14:00:22 +01001609 if (ret)
1610 return ret;
1611
1612 return 0;
1613}
1614
1615/**
1616 * i915_driver_lastclose - clean up after all DRM clients have exited
1617 * @dev: DRM device
1618 *
1619 * Take care of cleaning up after all DRM clients have exited. In the
1620 * mode setting case, we want to restore the kernel's initial mode (just
1621 * in case the last client left us in a bad state).
1622 *
1623 * Additionally, in the non-mode setting case, we'll tear down the GTT
1624 * and DMA structures, since the kernel won't be using them, and clea
1625 * up any GEM state.
1626 */
1627static void i915_driver_lastclose(struct drm_device *dev)
1628{
1629 intel_fbdev_restore_mode(dev);
1630 vga_switcheroo_process_delayed_switch();
1631}
1632
Daniel Vetter7d2ec882017-03-08 15:12:45 +01001633static void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Chris Wilson0673ad42016-06-24 14:00:22 +01001634{
Daniel Vetter7d2ec882017-03-08 15:12:45 +01001635 struct drm_i915_file_private *file_priv = file->driver_priv;
1636
Chris Wilson829a0af2017-06-20 12:05:45 +01001637 i915_gem_context_close(file);
Chris Wilson0673ad42016-06-24 14:00:22 +01001638 i915_gem_release(dev, file);
Chris Wilson0673ad42016-06-24 14:00:22 +01001639
Chris Wilson77715902019-08-23 19:14:55 +01001640 kfree_rcu(file_priv, rcu);
Chris Wilson515b8b72019-08-02 22:21:37 +01001641
1642 /* Catch up with all the deferred frees from "this" client */
1643 i915_gem_flush_free_objects(to_i915(dev));
Chris Wilson0673ad42016-06-24 14:00:22 +01001644}
1645
Imre Deak07f9cd02014-08-18 14:42:45 +03001646static void intel_suspend_encoders(struct drm_i915_private *dev_priv)
1647{
Chris Wilson91c8a322016-07-05 10:40:23 +01001648 struct drm_device *dev = &dev_priv->drm;
Jani Nikula19c80542015-12-16 12:48:16 +02001649 struct intel_encoder *encoder;
Imre Deak07f9cd02014-08-18 14:42:45 +03001650
1651 drm_modeset_lock_all(dev);
Jani Nikula19c80542015-12-16 12:48:16 +02001652 for_each_intel_encoder(dev, encoder)
1653 if (encoder->suspend)
1654 encoder->suspend(encoder);
Imre Deak07f9cd02014-08-18 14:42:45 +03001655 drm_modeset_unlock_all(dev);
1656}
1657
Paulo Zanoni1a5df182014-10-27 17:54:32 -02001658static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
1659 bool rpm_resume);
Imre Deak507e1262016-04-20 20:27:54 +03001660static int vlv_suspend_complete(struct drm_i915_private *dev_priv);
Suketu Shahf75a1982015-04-16 14:22:11 +05301661
Imre Deakbc872292015-11-18 17:32:30 +02001662static bool suspend_to_idle(struct drm_i915_private *dev_priv)
1663{
1664#if IS_ENABLED(CONFIG_ACPI_SLEEP)
1665 if (acpi_target_system_state() < ACPI_STATE_S3)
1666 return true;
1667#endif
1668 return false;
1669}
Sagar Kambleebc32822014-08-13 23:07:05 +05301670
Chris Wilson73b66f82018-05-25 10:26:29 +01001671static int i915_drm_prepare(struct drm_device *dev)
1672{
1673 struct drm_i915_private *i915 = to_i915(dev);
Chris Wilson73b66f82018-05-25 10:26:29 +01001674
1675 /*
1676 * NB intel_display_suspend() may issue new requests after we've
1677 * ostensibly marked the GPU as ready-to-sleep here. We need to
1678 * split out that work and pull it forward so that after point,
1679 * the GPU is not woken again.
1680 */
Chris Wilson5861b012019-03-08 09:36:54 +00001681 i915_gem_suspend(i915);
Chris Wilson73b66f82018-05-25 10:26:29 +01001682
Chris Wilson5861b012019-03-08 09:36:54 +00001683 return 0;
Chris Wilson73b66f82018-05-25 10:26:29 +01001684}
1685
Imre Deak5e365c32014-10-23 19:23:25 +03001686static int i915_drm_suspend(struct drm_device *dev)
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001687{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001688 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +03001689 struct pci_dev *pdev = dev_priv->drm.pdev;
Jesse Barnese5747e32014-06-12 08:35:47 -07001690 pci_power_t opregion_target_state;
Rafael J. Wysocki61caf872010-02-18 23:06:27 +01001691
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07001692 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
Imre Deak1f814da2015-12-16 02:52:19 +02001693
Paulo Zanonic67a4702013-08-19 13:18:09 -03001694 /* We do a lot of poking in a lot of registers, make sure they work
1695 * properly. */
Imre Deak2cd9a682018-08-16 15:37:57 +03001696 intel_power_domains_disable(dev_priv);
Paulo Zanonicb107992013-01-25 16:59:15 -02001697
Dave Airlie5bcf7192010-12-07 09:20:40 +10001698 drm_kms_helper_poll_disable(dev);
1699
David Weinehall52a05c32016-08-22 13:32:44 +03001700 pci_save_state(pdev);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001701
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02001702 intel_display_suspend(dev);
Daniel Vetterd5818932015-02-23 12:03:26 +01001703
Ville Syrjälä1a4313d2018-07-05 19:43:52 +03001704 intel_dp_mst_suspend(dev_priv);
Daniel Vetterd5818932015-02-23 12:03:26 +01001705
1706 intel_runtime_pm_disable_interrupts(dev_priv);
1707 intel_hpd_cancel_work(dev_priv);
1708
1709 intel_suspend_encoders(dev_priv);
1710
Ville Syrjälä712bf362016-10-31 22:37:23 +02001711 intel_suspend_hw(dev_priv);
Daniel Vetterd5818932015-02-23 12:03:26 +01001712
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001713 i915_gem_suspend_gtt_mappings(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07001714
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +00001715 i915_save_state(dev_priv);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001716
Imre Deakbc872292015-11-18 17:32:30 +02001717 opregion_target_state = suspend_to_idle(dev_priv) ? PCI_D1 : PCI_D3cold;
Chris Wilsona950adc2018-10-30 11:05:54 +00001718 intel_opregion_suspend(dev_priv, opregion_target_state);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001719
Chris Wilson82e3b8c2014-08-13 13:09:46 +01001720 intel_fbdev_set_suspend(dev, FBINFO_STATE_SUSPENDED, true);
Dave Airlie3fa016a2012-03-28 10:48:49 +01001721
Mika Kuoppala62d5d692014-02-25 17:11:28 +02001722 dev_priv->suspend_count++;
1723
Imre Deakf74ed082016-04-18 14:48:21 +03001724 intel_csr_ucode_suspend(dev_priv);
Imre Deakf514c2d2015-10-28 23:59:06 +02001725
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07001726 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
Imre Deak1f814da2015-12-16 02:52:19 +02001727
Chris Wilson73b66f82018-05-25 10:26:29 +01001728 return 0;
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001729}
1730
Imre Deak2cd9a682018-08-16 15:37:57 +03001731static enum i915_drm_suspend_mode
1732get_suspend_mode(struct drm_i915_private *dev_priv, bool hibernate)
1733{
1734 if (hibernate)
1735 return I915_DRM_SUSPEND_HIBERNATE;
1736
1737 if (suspend_to_idle(dev_priv))
1738 return I915_DRM_SUSPEND_IDLE;
1739
1740 return I915_DRM_SUSPEND_MEM;
1741}
1742
David Weinehallc49d13e2016-08-22 13:32:42 +03001743static int i915_drm_suspend_late(struct drm_device *dev, bool hibernation)
Imre Deakc3c09c92014-10-23 19:23:15 +03001744{
David Weinehallc49d13e2016-08-22 13:32:42 +03001745 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +03001746 struct pci_dev *pdev = dev_priv->drm.pdev;
Daniele Ceraolo Spurio69c66352019-06-13 16:21:53 -07001747 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
Rodrigo Vivi071b68c2019-08-06 15:22:08 +03001748 int ret = 0;
Imre Deakc3c09c92014-10-23 19:23:15 +03001749
Daniele Ceraolo Spurio69c66352019-06-13 16:21:53 -07001750 disable_rpm_wakeref_asserts(rpm);
Imre Deak1f814da2015-12-16 02:52:19 +02001751
Chris Wilsonec92ad02018-05-31 09:22:46 +01001752 i915_gem_suspend_late(dev_priv);
1753
Daniele Ceraolo Spuriof7de5022019-03-19 11:35:37 -07001754 intel_uncore_suspend(&dev_priv->uncore);
Imre Deak4c494a52016-10-13 14:34:06 +03001755
Imre Deak2cd9a682018-08-16 15:37:57 +03001756 intel_power_domains_suspend(dev_priv,
1757 get_suspend_mode(dev_priv, hibernation));
Imre Deak73dfc222015-11-17 17:33:53 +02001758
Rodrigo Vivi071b68c2019-08-06 15:22:08 +03001759 intel_display_power_suspend_late(dev_priv);
1760
1761 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Imre Deak507e1262016-04-20 20:27:54 +03001762 ret = vlv_suspend_complete(dev_priv);
Imre Deakc3c09c92014-10-23 19:23:15 +03001763
1764 if (ret) {
1765 DRM_ERROR("Suspend complete failed: %d\n", ret);
Imre Deak2cd9a682018-08-16 15:37:57 +03001766 intel_power_domains_resume(dev_priv);
Imre Deakc3c09c92014-10-23 19:23:15 +03001767
Imre Deak1f814da2015-12-16 02:52:19 +02001768 goto out;
Imre Deakc3c09c92014-10-23 19:23:15 +03001769 }
1770
David Weinehall52a05c32016-08-22 13:32:44 +03001771 pci_disable_device(pdev);
Imre Deakab3be732015-03-02 13:04:41 +02001772 /*
Imre Deak54875572015-06-30 17:06:47 +03001773 * During hibernation on some platforms the BIOS may try to access
Imre Deakab3be732015-03-02 13:04:41 +02001774 * the device even though it's already in D3 and hang the machine. So
1775 * leave the device in D0 on those platforms and hope the BIOS will
Imre Deak54875572015-06-30 17:06:47 +03001776 * power down the device properly. The issue was seen on multiple old
1777 * GENs with different BIOS vendors, so having an explicit blacklist
1778 * is inpractical; apply the workaround on everything pre GEN6. The
1779 * platforms where the issue was seen:
1780 * Lenovo Thinkpad X301, X61s, X60, T60, X41
1781 * Fujitsu FSC S7110
1782 * Acer Aspire 1830T
Imre Deakab3be732015-03-02 13:04:41 +02001783 */
Tvrtko Ursulin514e1d62016-11-04 14:42:48 +00001784 if (!(hibernation && INTEL_GEN(dev_priv) < 6))
David Weinehall52a05c32016-08-22 13:32:44 +03001785 pci_set_power_state(pdev, PCI_D3hot);
Imre Deakc3c09c92014-10-23 19:23:15 +03001786
Imre Deak1f814da2015-12-16 02:52:19 +02001787out:
Daniele Ceraolo Spurio69c66352019-06-13 16:21:53 -07001788 enable_rpm_wakeref_asserts(rpm);
Daniele Ceraolo Spurio0a9b2632019-08-09 07:31:16 +01001789 if (!dev_priv->uncore.user_forcewake_count)
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02001790 intel_runtime_pm_driver_release(rpm);
Imre Deak1f814da2015-12-16 02:52:19 +02001791
1792 return ret;
Imre Deakc3c09c92014-10-23 19:23:15 +03001793}
1794
Jani Nikula63bf8302019-10-04 15:20:18 +03001795int i915_suspend_switcheroo(struct drm_i915_private *i915, pm_message_t state)
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001796{
1797 int error;
1798
Imre Deak0b14cbd2014-09-10 18:16:55 +03001799 if (WARN_ON_ONCE(state.event != PM_EVENT_SUSPEND &&
1800 state.event != PM_EVENT_FREEZE))
1801 return -EINVAL;
Dave Airlie5bcf7192010-12-07 09:20:40 +10001802
Chris Wilson361f9dc2019-08-06 08:42:19 +01001803 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airlie5bcf7192010-12-07 09:20:40 +10001804 return 0;
Chris Wilson6eecba32010-09-08 09:45:11 +01001805
Chris Wilson361f9dc2019-08-06 08:42:19 +01001806 error = i915_drm_suspend(&i915->drm);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001807 if (error)
1808 return error;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001809
Chris Wilson361f9dc2019-08-06 08:42:19 +01001810 return i915_drm_suspend_late(&i915->drm, false);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001811}
1812
Imre Deak5e365c32014-10-23 19:23:25 +03001813static int i915_drm_resume(struct drm_device *dev)
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001814{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001815 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläac840ae2016-05-06 21:35:55 +03001816 int ret;
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001817
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07001818 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
Imre Deak1f814da2015-12-16 02:52:19 +02001819
Chris Wilsonfd6fe082019-11-01 14:10:06 +00001820 intel_gt_sanitize(&dev_priv->gt, true);
Chris Wilson12887862018-06-14 10:40:59 +01001821
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001822 ret = i915_ggtt_enable_hw(dev_priv);
Ville Syrjäläac840ae2016-05-06 21:35:55 +03001823 if (ret)
1824 DRM_ERROR("failed to re-enable GGTT\n");
1825
Chris Wilson6c76a932019-09-09 12:00:08 +01001826 i915_gem_restore_gtt_mappings(dev_priv);
Chris Wilsone9d4c922019-10-16 15:32:33 +01001827 i915_gem_restore_fences(&dev_priv->ggtt);
Chris Wilson6c76a932019-09-09 12:00:08 +01001828
Imre Deakf74ed082016-04-18 14:48:21 +03001829 intel_csr_ucode_resume(dev_priv);
1830
Tvrtko Ursulinaf6dc742016-12-01 14:16:44 +00001831 i915_restore_state(dev_priv);
Imre Deak8090ba82016-08-10 14:07:33 +03001832 intel_pps_unlock_regs_wa(dev_priv);
Rafael J. Wysocki61caf872010-02-18 23:06:27 +01001833
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02001834 intel_init_pch_refclk(dev_priv);
Chris Wilson1833b132012-05-09 11:56:28 +01001835
Peter Antoine364aece2015-05-11 08:50:45 +01001836 /*
1837 * Interrupts have to be enabled before any batches are run. If not the
1838 * GPU will hang. i915_gem_init_hw() will initiate batches to
1839 * update/restore the context.
1840 *
Imre Deak908764f2016-11-29 21:40:29 +02001841 * drm_mode_config_reset() needs AUX interrupts.
1842 *
Peter Antoine364aece2015-05-11 08:50:45 +01001843 * Modeset enabling in intel_modeset_init_hw() also needs working
1844 * interrupts.
1845 */
1846 intel_runtime_pm_enable_interrupts(dev_priv);
1847
Imre Deak908764f2016-11-29 21:40:29 +02001848 drm_mode_config_reset(dev);
1849
Chris Wilson37cd3302017-11-12 11:27:38 +00001850 i915_gem_resume(dev_priv);
Daniel Vetterd5818932015-02-23 12:03:26 +01001851
Jani Nikula6cd02e72019-09-20 21:54:21 +03001852 intel_modeset_init_hw(dev_priv);
Ville Syrjälä675f7ff2017-11-16 18:02:15 +02001853 intel_init_clock_gating(dev_priv);
Daniel Vetterd5818932015-02-23 12:03:26 +01001854
1855 spin_lock_irq(&dev_priv->irq_lock);
1856 if (dev_priv->display.hpd_irq_setup)
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01001857 dev_priv->display.hpd_irq_setup(dev_priv);
Daniel Vetterd5818932015-02-23 12:03:26 +01001858 spin_unlock_irq(&dev_priv->irq_lock);
1859
Ville Syrjälä1a4313d2018-07-05 19:43:52 +03001860 intel_dp_mst_resume(dev_priv);
Daniel Vetterd5818932015-02-23 12:03:26 +01001861
Lyudea16b7652016-03-11 10:57:01 -05001862 intel_display_resume(dev);
1863
Lyudee0b70062016-11-01 21:06:30 -04001864 drm_kms_helper_poll_enable(dev);
1865
Daniel Vetterd5818932015-02-23 12:03:26 +01001866 /*
1867 * ... but also need to make sure that hotplug processing
1868 * doesn't cause havoc. Like in the driver load code we don't
Gwan-gyeong Munc444ad72018-08-03 19:41:50 +03001869 * bother with the tiny race here where we might lose hotplug
Daniel Vetterd5818932015-02-23 12:03:26 +01001870 * notifications.
1871 * */
1872 intel_hpd_init(dev_priv);
Jesse Barnes1daed3f2011-01-05 12:01:25 -08001873
Chris Wilsona950adc2018-10-30 11:05:54 +00001874 intel_opregion_resume(dev_priv);
Chris Wilson44834a62010-08-19 16:09:23 +01001875
Chris Wilson82e3b8c2014-08-13 13:09:46 +01001876 intel_fbdev_set_suspend(dev, FBINFO_STATE_RUNNING, false);
Jesse Barnes073f34d2012-11-02 11:13:59 -07001877
Imre Deak2cd9a682018-08-16 15:37:57 +03001878 intel_power_domains_enable(dev_priv);
1879
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07001880 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
Imre Deak1f814da2015-12-16 02:52:19 +02001881
Chris Wilson074c6ad2014-04-09 09:19:43 +01001882 return 0;
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001883}
1884
Imre Deak5e365c32014-10-23 19:23:25 +03001885static int i915_drm_resume_early(struct drm_device *dev)
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001886{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001887 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +03001888 struct pci_dev *pdev = dev_priv->drm.pdev;
Imre Deak44410cd2016-04-18 14:45:54 +03001889 int ret;
Imre Deak36d61e62014-10-23 19:23:24 +03001890
Imre Deak76c4b252014-04-01 19:55:22 +03001891 /*
1892 * We have a resume ordering issue with the snd-hda driver also
1893 * requiring our device to be power up. Due to the lack of a
1894 * parent/child relationship we currently solve this with an early
1895 * resume hook.
1896 *
1897 * FIXME: This should be solved with a special hdmi sink device or
1898 * similar so that power domains can be employed.
1899 */
Imre Deak44410cd2016-04-18 14:45:54 +03001900
1901 /*
1902 * Note that we need to set the power state explicitly, since we
1903 * powered off the device during freeze and the PCI core won't power
1904 * it back up for us during thaw. Powering off the device during
1905 * freeze is not a hard requirement though, and during the
1906 * suspend/resume phases the PCI core makes sure we get here with the
1907 * device powered on. So in case we change our freeze logic and keep
1908 * the device powered we can also remove the following set power state
1909 * call.
1910 */
David Weinehall52a05c32016-08-22 13:32:44 +03001911 ret = pci_set_power_state(pdev, PCI_D0);
Imre Deak44410cd2016-04-18 14:45:54 +03001912 if (ret) {
1913 DRM_ERROR("failed to set PCI D0 power state (%d)\n", ret);
Imre Deak2cd9a682018-08-16 15:37:57 +03001914 return ret;
Imre Deak44410cd2016-04-18 14:45:54 +03001915 }
1916
1917 /*
1918 * Note that pci_enable_device() first enables any parent bridge
1919 * device and only then sets the power state for this device. The
1920 * bridge enabling is a nop though, since bridge devices are resumed
1921 * first. The order of enabling power and enabling the device is
1922 * imposed by the PCI core as described above, so here we preserve the
1923 * same order for the freeze/thaw phases.
1924 *
1925 * TODO: eventually we should remove pci_disable_device() /
1926 * pci_enable_enable_device() from suspend/resume. Due to how they
1927 * depend on the device enable refcount we can't anyway depend on them
1928 * disabling/enabling the device.
1929 */
Imre Deak2cd9a682018-08-16 15:37:57 +03001930 if (pci_enable_device(pdev))
1931 return -EIO;
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001932
David Weinehall52a05c32016-08-22 13:32:44 +03001933 pci_set_master(pdev);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001934
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07001935 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
Imre Deak1f814da2015-12-16 02:52:19 +02001936
Wayne Boyer666a4532015-12-09 12:29:35 -08001937 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Paulo Zanoni1a5df182014-10-27 17:54:32 -02001938 ret = vlv_resume_prepare(dev_priv, false);
Imre Deak36d61e62014-10-23 19:23:24 +03001939 if (ret)
Damien Lespiauff0b1872015-05-20 14:45:15 +01001940 DRM_ERROR("Resume prepare failed: %d, continuing anyway\n",
1941 ret);
Imre Deak36d61e62014-10-23 19:23:24 +03001942
Daniele Ceraolo Spuriof7de5022019-03-19 11:35:37 -07001943 intel_uncore_resume_early(&dev_priv->uncore);
1944
Tvrtko Ursulineaf522f2019-06-21 08:07:44 +01001945 intel_gt_check_and_clear_faults(&dev_priv->gt);
Paulo Zanoniefee8332014-10-27 17:54:33 -02001946
Rodrigo Vivi071b68c2019-08-06 15:22:08 +03001947 intel_display_power_resume_early(dev_priv);
Paulo Zanoniefee8332014-10-27 17:54:33 -02001948
Imre Deak2cd9a682018-08-16 15:37:57 +03001949 intel_power_domains_resume(dev_priv);
Imre Deakbc872292015-11-18 17:32:30 +02001950
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07001951 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
Imre Deak6e35e8a2016-04-18 10:04:19 +03001952
Imre Deak36d61e62014-10-23 19:23:24 +03001953 return ret;
Imre Deak76c4b252014-04-01 19:55:22 +03001954}
1955
Jani Nikula63bf8302019-10-04 15:20:18 +03001956int i915_resume_switcheroo(struct drm_i915_private *i915)
Imre Deak76c4b252014-04-01 19:55:22 +03001957{
Imre Deak50a00722014-10-23 19:23:17 +03001958 int ret;
Imre Deak76c4b252014-04-01 19:55:22 +03001959
Chris Wilson361f9dc2019-08-06 08:42:19 +01001960 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
Imre Deak097dd832014-10-23 19:23:19 +03001961 return 0;
1962
Chris Wilson361f9dc2019-08-06 08:42:19 +01001963 ret = i915_drm_resume_early(&i915->drm);
Imre Deak50a00722014-10-23 19:23:17 +03001964 if (ret)
1965 return ret;
1966
Chris Wilson361f9dc2019-08-06 08:42:19 +01001967 return i915_drm_resume(&i915->drm);
Imre Deak5a175142014-10-23 19:23:18 +03001968}
1969
Chris Wilson73b66f82018-05-25 10:26:29 +01001970static int i915_pm_prepare(struct device *kdev)
1971{
Chris Wilson361f9dc2019-08-06 08:42:19 +01001972 struct drm_i915_private *i915 = kdev_to_i915(kdev);
Chris Wilson73b66f82018-05-25 10:26:29 +01001973
Chris Wilson361f9dc2019-08-06 08:42:19 +01001974 if (!i915) {
Chris Wilson73b66f82018-05-25 10:26:29 +01001975 dev_err(kdev, "DRM not initialized, aborting suspend.\n");
1976 return -ENODEV;
1977 }
1978
Chris Wilson361f9dc2019-08-06 08:42:19 +01001979 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
Chris Wilson73b66f82018-05-25 10:26:29 +01001980 return 0;
1981
Chris Wilson361f9dc2019-08-06 08:42:19 +01001982 return i915_drm_prepare(&i915->drm);
Chris Wilson73b66f82018-05-25 10:26:29 +01001983}
1984
David Weinehallc49d13e2016-08-22 13:32:42 +03001985static int i915_pm_suspend(struct device *kdev)
Kristian Høgsberg112b7152009-01-04 16:55:33 -05001986{
Chris Wilson361f9dc2019-08-06 08:42:19 +01001987 struct drm_i915_private *i915 = kdev_to_i915(kdev);
Kristian Høgsberg112b7152009-01-04 16:55:33 -05001988
Chris Wilson361f9dc2019-08-06 08:42:19 +01001989 if (!i915) {
David Weinehallc49d13e2016-08-22 13:32:42 +03001990 dev_err(kdev, "DRM not initialized, aborting suspend.\n");
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01001991 return -ENODEV;
1992 }
Kristian Høgsberg112b7152009-01-04 16:55:33 -05001993
Chris Wilson361f9dc2019-08-06 08:42:19 +01001994 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airlie5bcf7192010-12-07 09:20:40 +10001995 return 0;
1996
Chris Wilson361f9dc2019-08-06 08:42:19 +01001997 return i915_drm_suspend(&i915->drm);
Imre Deak76c4b252014-04-01 19:55:22 +03001998}
1999
David Weinehallc49d13e2016-08-22 13:32:42 +03002000static int i915_pm_suspend_late(struct device *kdev)
Imre Deak76c4b252014-04-01 19:55:22 +03002001{
Chris Wilson361f9dc2019-08-06 08:42:19 +01002002 struct drm_i915_private *i915 = kdev_to_i915(kdev);
Imre Deak76c4b252014-04-01 19:55:22 +03002003
2004 /*
Damien Lespiauc965d9952015-05-18 19:53:48 +01002005 * We have a suspend ordering issue with the snd-hda driver also
Imre Deak76c4b252014-04-01 19:55:22 +03002006 * requiring our device to be power up. Due to the lack of a
2007 * parent/child relationship we currently solve this with an late
2008 * suspend hook.
2009 *
2010 * FIXME: This should be solved with a special hdmi sink device or
2011 * similar so that power domains can be employed.
2012 */
Chris Wilson361f9dc2019-08-06 08:42:19 +01002013 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
Imre Deak76c4b252014-04-01 19:55:22 +03002014 return 0;
Kristian Høgsberg112b7152009-01-04 16:55:33 -05002015
Chris Wilson361f9dc2019-08-06 08:42:19 +01002016 return i915_drm_suspend_late(&i915->drm, false);
Imre Deakab3be732015-03-02 13:04:41 +02002017}
2018
David Weinehallc49d13e2016-08-22 13:32:42 +03002019static int i915_pm_poweroff_late(struct device *kdev)
Imre Deakab3be732015-03-02 13:04:41 +02002020{
Chris Wilson361f9dc2019-08-06 08:42:19 +01002021 struct drm_i915_private *i915 = kdev_to_i915(kdev);
Imre Deakab3be732015-03-02 13:04:41 +02002022
Chris Wilson361f9dc2019-08-06 08:42:19 +01002023 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
Imre Deakab3be732015-03-02 13:04:41 +02002024 return 0;
2025
Chris Wilson361f9dc2019-08-06 08:42:19 +01002026 return i915_drm_suspend_late(&i915->drm, true);
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002027}
2028
David Weinehallc49d13e2016-08-22 13:32:42 +03002029static int i915_pm_resume_early(struct device *kdev)
Imre Deak76c4b252014-04-01 19:55:22 +03002030{
Chris Wilson361f9dc2019-08-06 08:42:19 +01002031 struct drm_i915_private *i915 = kdev_to_i915(kdev);
Imre Deak76c4b252014-04-01 19:55:22 +03002032
Chris Wilson361f9dc2019-08-06 08:42:19 +01002033 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
Imre Deak097dd832014-10-23 19:23:19 +03002034 return 0;
2035
Chris Wilson361f9dc2019-08-06 08:42:19 +01002036 return i915_drm_resume_early(&i915->drm);
Imre Deak76c4b252014-04-01 19:55:22 +03002037}
2038
David Weinehallc49d13e2016-08-22 13:32:42 +03002039static int i915_pm_resume(struct device *kdev)
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002040{
Chris Wilson361f9dc2019-08-06 08:42:19 +01002041 struct drm_i915_private *i915 = kdev_to_i915(kdev);
Rafael J. Wysocki84b79f82010-02-07 21:48:24 +01002042
Chris Wilson361f9dc2019-08-06 08:42:19 +01002043 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
Imre Deak097dd832014-10-23 19:23:19 +03002044 return 0;
2045
Chris Wilson361f9dc2019-08-06 08:42:19 +01002046 return i915_drm_resume(&i915->drm);
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002047}
2048
Chris Wilson1f19ac22016-05-14 07:26:32 +01002049/* freeze: before creating the hibernation_image */
David Weinehallc49d13e2016-08-22 13:32:42 +03002050static int i915_pm_freeze(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002051{
Chris Wilson361f9dc2019-08-06 08:42:19 +01002052 struct drm_i915_private *i915 = kdev_to_i915(kdev);
Chris Wilson6a800ea2016-09-21 14:51:07 +01002053 int ret;
2054
Chris Wilson361f9dc2019-08-06 08:42:19 +01002055 if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) {
2056 ret = i915_drm_suspend(&i915->drm);
Imre Deakdd9f31c2017-08-16 17:46:07 +03002057 if (ret)
2058 return ret;
2059 }
Chris Wilson6a800ea2016-09-21 14:51:07 +01002060
Chris Wilson361f9dc2019-08-06 08:42:19 +01002061 ret = i915_gem_freeze(i915);
Chris Wilson6a800ea2016-09-21 14:51:07 +01002062 if (ret)
2063 return ret;
2064
2065 return 0;
Chris Wilson1f19ac22016-05-14 07:26:32 +01002066}
2067
David Weinehallc49d13e2016-08-22 13:32:42 +03002068static int i915_pm_freeze_late(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002069{
Chris Wilson361f9dc2019-08-06 08:42:19 +01002070 struct drm_i915_private *i915 = kdev_to_i915(kdev);
Chris Wilson461fb992016-05-14 07:26:33 +01002071 int ret;
2072
Chris Wilson361f9dc2019-08-06 08:42:19 +01002073 if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) {
2074 ret = i915_drm_suspend_late(&i915->drm, true);
Imre Deakdd9f31c2017-08-16 17:46:07 +03002075 if (ret)
2076 return ret;
2077 }
Chris Wilson461fb992016-05-14 07:26:33 +01002078
Chris Wilson361f9dc2019-08-06 08:42:19 +01002079 ret = i915_gem_freeze_late(i915);
Chris Wilson461fb992016-05-14 07:26:33 +01002080 if (ret)
2081 return ret;
2082
2083 return 0;
Chris Wilson1f19ac22016-05-14 07:26:32 +01002084}
2085
2086/* thaw: called after creating the hibernation image, but before turning off. */
David Weinehallc49d13e2016-08-22 13:32:42 +03002087static int i915_pm_thaw_early(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002088{
David Weinehallc49d13e2016-08-22 13:32:42 +03002089 return i915_pm_resume_early(kdev);
Chris Wilson1f19ac22016-05-14 07:26:32 +01002090}
2091
David Weinehallc49d13e2016-08-22 13:32:42 +03002092static int i915_pm_thaw(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002093{
David Weinehallc49d13e2016-08-22 13:32:42 +03002094 return i915_pm_resume(kdev);
Chris Wilson1f19ac22016-05-14 07:26:32 +01002095}
2096
2097/* restore: called after loading the hibernation image. */
David Weinehallc49d13e2016-08-22 13:32:42 +03002098static int i915_pm_restore_early(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002099{
David Weinehallc49d13e2016-08-22 13:32:42 +03002100 return i915_pm_resume_early(kdev);
Chris Wilson1f19ac22016-05-14 07:26:32 +01002101}
2102
David Weinehallc49d13e2016-08-22 13:32:42 +03002103static int i915_pm_restore(struct device *kdev)
Chris Wilson1f19ac22016-05-14 07:26:32 +01002104{
David Weinehallc49d13e2016-08-22 13:32:42 +03002105 return i915_pm_resume(kdev);
Chris Wilson1f19ac22016-05-14 07:26:32 +01002106}
2107
Imre Deakddeea5b2014-05-05 15:19:56 +03002108/*
2109 * Save all Gunit registers that may be lost after a D3 and a subsequent
2110 * S0i[R123] transition. The list of registers needing a save/restore is
2111 * defined in the VLV2_S0IXRegs document. This documents marks all Gunit
2112 * registers in the following way:
2113 * - Driver: saved/restored by the driver
2114 * - Punit : saved/restored by the Punit firmware
2115 * - No, w/o marking: no need to save/restore, since the register is R/O or
2116 * used internally by the HW in a way that doesn't depend
2117 * keeping the content across a suspend/resume.
2118 * - Debug : used for debugging
2119 *
2120 * We save/restore all registers marked with 'Driver', with the following
2121 * exceptions:
2122 * - Registers out of use, including also registers marked with 'Debug'.
2123 * These have no effect on the driver's operation, so we don't save/restore
2124 * them to reduce the overhead.
2125 * - Registers that are fully setup by an initialization function called from
2126 * the resume path. For example many clock gating and RPS/RC6 registers.
2127 * - Registers that provide the right functionality with their reset defaults.
2128 *
2129 * TODO: Except for registers that based on the above 3 criteria can be safely
2130 * ignored, we save/restore all others, practically treating the HW context as
2131 * a black-box for the driver. Further investigation is needed to reduce the
2132 * saved/restored registers even further, by following the same 3 criteria.
2133 */
2134static void vlv_save_gunit_s0ix_state(struct drm_i915_private *dev_priv)
2135{
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -07002136 struct vlv_s0ix_state *s = dev_priv->vlv_s0ix_state;
Imre Deakddeea5b2014-05-05 15:19:56 +03002137 int i;
2138
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -07002139 if (!s)
2140 return;
2141
Imre Deakddeea5b2014-05-05 15:19:56 +03002142 /* GAM 0x4000-0x4770 */
2143 s->wr_watermark = I915_READ(GEN7_WR_WATERMARK);
2144 s->gfx_prio_ctrl = I915_READ(GEN7_GFX_PRIO_CTRL);
2145 s->arb_mode = I915_READ(ARB_MODE);
2146 s->gfx_pend_tlb0 = I915_READ(GEN7_GFX_PEND_TLB0);
2147 s->gfx_pend_tlb1 = I915_READ(GEN7_GFX_PEND_TLB1);
2148
2149 for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002150 s->lra_limits[i] = I915_READ(GEN7_LRA_LIMITS(i));
Imre Deakddeea5b2014-05-05 15:19:56 +03002151
2152 s->media_max_req_count = I915_READ(GEN7_MEDIA_MAX_REQ_COUNT);
Imre Deakb5f1c972015-04-15 16:52:30 -07002153 s->gfx_max_req_count = I915_READ(GEN7_GFX_MAX_REQ_COUNT);
Imre Deakddeea5b2014-05-05 15:19:56 +03002154
2155 s->render_hwsp = I915_READ(RENDER_HWS_PGA_GEN7);
2156 s->ecochk = I915_READ(GAM_ECOCHK);
2157 s->bsd_hwsp = I915_READ(BSD_HWS_PGA_GEN7);
2158 s->blt_hwsp = I915_READ(BLT_HWS_PGA_GEN7);
2159
2160 s->tlb_rd_addr = I915_READ(GEN7_TLB_RD_ADDR);
2161
2162 /* MBC 0x9024-0x91D0, 0x8500 */
2163 s->g3dctl = I915_READ(VLV_G3DCTL);
2164 s->gsckgctl = I915_READ(VLV_GSCKGCTL);
2165 s->mbctl = I915_READ(GEN6_MBCTL);
2166
2167 /* GCP 0x9400-0x9424, 0x8100-0x810C */
2168 s->ucgctl1 = I915_READ(GEN6_UCGCTL1);
2169 s->ucgctl3 = I915_READ(GEN6_UCGCTL3);
2170 s->rcgctl1 = I915_READ(GEN6_RCGCTL1);
2171 s->rcgctl2 = I915_READ(GEN6_RCGCTL2);
2172 s->rstctl = I915_READ(GEN6_RSTCTL);
2173 s->misccpctl = I915_READ(GEN7_MISCCPCTL);
2174
2175 /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
2176 s->gfxpause = I915_READ(GEN6_GFXPAUSE);
2177 s->rpdeuhwtc = I915_READ(GEN6_RPDEUHWTC);
2178 s->rpdeuc = I915_READ(GEN6_RPDEUC);
2179 s->ecobus = I915_READ(ECOBUS);
2180 s->pwrdwnupctl = I915_READ(VLV_PWRDWNUPCTL);
2181 s->rp_down_timeout = I915_READ(GEN6_RP_DOWN_TIMEOUT);
2182 s->rp_deucsw = I915_READ(GEN6_RPDEUCSW);
2183 s->rcubmabdtmr = I915_READ(GEN6_RCUBMABDTMR);
2184 s->rcedata = I915_READ(VLV_RCEDATA);
2185 s->spare2gh = I915_READ(VLV_SPAREG2H);
2186
2187 /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
2188 s->gt_imr = I915_READ(GTIMR);
2189 s->gt_ier = I915_READ(GTIER);
2190 s->pm_imr = I915_READ(GEN6_PMIMR);
2191 s->pm_ier = I915_READ(GEN6_PMIER);
2192
2193 for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002194 s->gt_scratch[i] = I915_READ(GEN7_GT_SCRATCH(i));
Imre Deakddeea5b2014-05-05 15:19:56 +03002195
2196 /* GT SA CZ domain, 0x100000-0x138124 */
2197 s->tilectl = I915_READ(TILECTL);
2198 s->gt_fifoctl = I915_READ(GTFIFOCTL);
2199 s->gtlc_wake_ctrl = I915_READ(VLV_GTLC_WAKE_CTRL);
2200 s->gtlc_survive = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2201 s->pmwgicz = I915_READ(VLV_PMWGICZ);
2202
2203 /* Gunit-Display CZ domain, 0x182028-0x1821CF */
2204 s->gu_ctl0 = I915_READ(VLV_GU_CTL0);
2205 s->gu_ctl1 = I915_READ(VLV_GU_CTL1);
Jesse Barnes9c252102015-04-01 14:22:57 -07002206 s->pcbr = I915_READ(VLV_PCBR);
Imre Deakddeea5b2014-05-05 15:19:56 +03002207 s->clock_gate_dis2 = I915_READ(VLV_GUNIT_CLOCK_GATE2);
2208
2209 /*
2210 * Not saving any of:
2211 * DFT, 0x9800-0x9EC0
2212 * SARB, 0xB000-0xB1FC
2213 * GAC, 0x5208-0x524C, 0x14000-0x14C000
2214 * PCI CFG
2215 */
2216}
2217
2218static void vlv_restore_gunit_s0ix_state(struct drm_i915_private *dev_priv)
2219{
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -07002220 struct vlv_s0ix_state *s = dev_priv->vlv_s0ix_state;
Imre Deakddeea5b2014-05-05 15:19:56 +03002221 u32 val;
2222 int i;
2223
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -07002224 if (!s)
2225 return;
2226
Imre Deakddeea5b2014-05-05 15:19:56 +03002227 /* GAM 0x4000-0x4770 */
2228 I915_WRITE(GEN7_WR_WATERMARK, s->wr_watermark);
2229 I915_WRITE(GEN7_GFX_PRIO_CTRL, s->gfx_prio_ctrl);
2230 I915_WRITE(ARB_MODE, s->arb_mode | (0xffff << 16));
2231 I915_WRITE(GEN7_GFX_PEND_TLB0, s->gfx_pend_tlb0);
2232 I915_WRITE(GEN7_GFX_PEND_TLB1, s->gfx_pend_tlb1);
2233
2234 for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002235 I915_WRITE(GEN7_LRA_LIMITS(i), s->lra_limits[i]);
Imre Deakddeea5b2014-05-05 15:19:56 +03002236
2237 I915_WRITE(GEN7_MEDIA_MAX_REQ_COUNT, s->media_max_req_count);
Imre Deakb5f1c972015-04-15 16:52:30 -07002238 I915_WRITE(GEN7_GFX_MAX_REQ_COUNT, s->gfx_max_req_count);
Imre Deakddeea5b2014-05-05 15:19:56 +03002239
2240 I915_WRITE(RENDER_HWS_PGA_GEN7, s->render_hwsp);
2241 I915_WRITE(GAM_ECOCHK, s->ecochk);
2242 I915_WRITE(BSD_HWS_PGA_GEN7, s->bsd_hwsp);
2243 I915_WRITE(BLT_HWS_PGA_GEN7, s->blt_hwsp);
2244
2245 I915_WRITE(GEN7_TLB_RD_ADDR, s->tlb_rd_addr);
2246
2247 /* MBC 0x9024-0x91D0, 0x8500 */
2248 I915_WRITE(VLV_G3DCTL, s->g3dctl);
2249 I915_WRITE(VLV_GSCKGCTL, s->gsckgctl);
2250 I915_WRITE(GEN6_MBCTL, s->mbctl);
2251
2252 /* GCP 0x9400-0x9424, 0x8100-0x810C */
2253 I915_WRITE(GEN6_UCGCTL1, s->ucgctl1);
2254 I915_WRITE(GEN6_UCGCTL3, s->ucgctl3);
2255 I915_WRITE(GEN6_RCGCTL1, s->rcgctl1);
2256 I915_WRITE(GEN6_RCGCTL2, s->rcgctl2);
2257 I915_WRITE(GEN6_RSTCTL, s->rstctl);
2258 I915_WRITE(GEN7_MISCCPCTL, s->misccpctl);
2259
2260 /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
2261 I915_WRITE(GEN6_GFXPAUSE, s->gfxpause);
2262 I915_WRITE(GEN6_RPDEUHWTC, s->rpdeuhwtc);
2263 I915_WRITE(GEN6_RPDEUC, s->rpdeuc);
2264 I915_WRITE(ECOBUS, s->ecobus);
2265 I915_WRITE(VLV_PWRDWNUPCTL, s->pwrdwnupctl);
2266 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,s->rp_down_timeout);
2267 I915_WRITE(GEN6_RPDEUCSW, s->rp_deucsw);
2268 I915_WRITE(GEN6_RCUBMABDTMR, s->rcubmabdtmr);
2269 I915_WRITE(VLV_RCEDATA, s->rcedata);
2270 I915_WRITE(VLV_SPAREG2H, s->spare2gh);
2271
2272 /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
2273 I915_WRITE(GTIMR, s->gt_imr);
2274 I915_WRITE(GTIER, s->gt_ier);
2275 I915_WRITE(GEN6_PMIMR, s->pm_imr);
2276 I915_WRITE(GEN6_PMIER, s->pm_ier);
2277
2278 for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
Ville Syrjälä22dfe792015-09-18 20:03:16 +03002279 I915_WRITE(GEN7_GT_SCRATCH(i), s->gt_scratch[i]);
Imre Deakddeea5b2014-05-05 15:19:56 +03002280
2281 /* GT SA CZ domain, 0x100000-0x138124 */
2282 I915_WRITE(TILECTL, s->tilectl);
2283 I915_WRITE(GTFIFOCTL, s->gt_fifoctl);
2284 /*
2285 * Preserve the GT allow wake and GFX force clock bit, they are not
2286 * be restored, as they are used to control the s0ix suspend/resume
2287 * sequence by the caller.
2288 */
2289 val = I915_READ(VLV_GTLC_WAKE_CTRL);
2290 val &= VLV_GTLC_ALLOWWAKEREQ;
2291 val |= s->gtlc_wake_ctrl & ~VLV_GTLC_ALLOWWAKEREQ;
2292 I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
2293
2294 val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2295 val &= VLV_GFX_CLK_FORCE_ON_BIT;
2296 val |= s->gtlc_survive & ~VLV_GFX_CLK_FORCE_ON_BIT;
2297 I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
2298
2299 I915_WRITE(VLV_PMWGICZ, s->pmwgicz);
2300
2301 /* Gunit-Display CZ domain, 0x182028-0x1821CF */
2302 I915_WRITE(VLV_GU_CTL0, s->gu_ctl0);
2303 I915_WRITE(VLV_GU_CTL1, s->gu_ctl1);
Jesse Barnes9c252102015-04-01 14:22:57 -07002304 I915_WRITE(VLV_PCBR, s->pcbr);
Imre Deakddeea5b2014-05-05 15:19:56 +03002305 I915_WRITE(VLV_GUNIT_CLOCK_GATE2, s->clock_gate_dis2);
2306}
2307
Tvrtko Ursulin5a31d302019-06-11 11:45:47 +01002308static int vlv_wait_for_pw_status(struct drm_i915_private *i915,
Chris Wilson3dd14c02017-04-21 14:58:15 +01002309 u32 mask, u32 val)
2310{
Ville Syrjälä39806c3f2019-02-04 23:16:44 +02002311 i915_reg_t reg = VLV_GTLC_PW_STATUS;
2312 u32 reg_value;
2313 int ret;
2314
Chris Wilson3dd14c02017-04-21 14:58:15 +01002315 /* The HW does not like us polling for PW_STATUS frequently, so
2316 * use the sleeping loop rather than risk the busy spin within
2317 * intel_wait_for_register().
2318 *
2319 * Transitioning between RC6 states should be at most 2ms (see
2320 * valleyview_enable_rps) so use a 3ms timeout.
2321 */
Tvrtko Ursulin5a31d302019-06-11 11:45:47 +01002322 ret = wait_for(((reg_value =
2323 intel_uncore_read_notrace(&i915->uncore, reg)) & mask)
2324 == val, 3);
Ville Syrjälä39806c3f2019-02-04 23:16:44 +02002325
2326 /* just trace the final value */
2327 trace_i915_reg_rw(false, reg, reg_value, sizeof(reg_value), true);
2328
2329 return ret;
Chris Wilson3dd14c02017-04-21 14:58:15 +01002330}
2331
Imre Deak650ad972014-04-18 16:35:02 +03002332int vlv_force_gfx_clock(struct drm_i915_private *dev_priv, bool force_on)
2333{
2334 u32 val;
2335 int err;
2336
Imre Deak650ad972014-04-18 16:35:02 +03002337 val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2338 val &= ~VLV_GFX_CLK_FORCE_ON_BIT;
2339 if (force_on)
2340 val |= VLV_GFX_CLK_FORCE_ON_BIT;
2341 I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
2342
2343 if (!force_on)
2344 return 0;
2345
Daniele Ceraolo Spurio97a04e02019-03-25 14:49:39 -07002346 err = intel_wait_for_register(&dev_priv->uncore,
Chris Wilsonc6ddc5f2016-06-30 15:32:46 +01002347 VLV_GTLC_SURVIVABILITY_REG,
2348 VLV_GFX_CLK_STATUS_BIT,
2349 VLV_GFX_CLK_STATUS_BIT,
2350 20);
Imre Deak650ad972014-04-18 16:35:02 +03002351 if (err)
2352 DRM_ERROR("timeout waiting for GFX clock force-on (%08x)\n",
2353 I915_READ(VLV_GTLC_SURVIVABILITY_REG));
2354
2355 return err;
Imre Deak650ad972014-04-18 16:35:02 +03002356}
2357
Imre Deakddeea5b2014-05-05 15:19:56 +03002358static int vlv_allow_gt_wake(struct drm_i915_private *dev_priv, bool allow)
2359{
Chris Wilson3dd14c02017-04-21 14:58:15 +01002360 u32 mask;
Imre Deakddeea5b2014-05-05 15:19:56 +03002361 u32 val;
Chris Wilson3dd14c02017-04-21 14:58:15 +01002362 int err;
Imre Deakddeea5b2014-05-05 15:19:56 +03002363
2364 val = I915_READ(VLV_GTLC_WAKE_CTRL);
2365 val &= ~VLV_GTLC_ALLOWWAKEREQ;
2366 if (allow)
2367 val |= VLV_GTLC_ALLOWWAKEREQ;
2368 I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
2369 POSTING_READ(VLV_GTLC_WAKE_CTRL);
2370
Chris Wilson3dd14c02017-04-21 14:58:15 +01002371 mask = VLV_GTLC_ALLOWWAKEACK;
2372 val = allow ? mask : 0;
2373
2374 err = vlv_wait_for_pw_status(dev_priv, mask, val);
Imre Deakddeea5b2014-05-05 15:19:56 +03002375 if (err)
2376 DRM_ERROR("timeout disabling GT waking\n");
Chris Wilsonb2736692016-06-30 15:32:47 +01002377
Imre Deakddeea5b2014-05-05 15:19:56 +03002378 return err;
Imre Deakddeea5b2014-05-05 15:19:56 +03002379}
2380
Chris Wilson3dd14c02017-04-21 14:58:15 +01002381static void vlv_wait_for_gt_wells(struct drm_i915_private *dev_priv,
2382 bool wait_for_on)
Imre Deakddeea5b2014-05-05 15:19:56 +03002383{
2384 u32 mask;
2385 u32 val;
Imre Deakddeea5b2014-05-05 15:19:56 +03002386
2387 mask = VLV_GTLC_PW_MEDIA_STATUS_MASK | VLV_GTLC_PW_RENDER_STATUS_MASK;
2388 val = wait_for_on ? mask : 0;
Imre Deakddeea5b2014-05-05 15:19:56 +03002389
2390 /*
2391 * RC6 transitioning can be delayed up to 2 msec (see
2392 * valleyview_enable_rps), use 3 msec for safety.
Chris Wilsone01569a2018-04-09 10:49:05 +01002393 *
2394 * This can fail to turn off the rc6 if the GPU is stuck after a failed
2395 * reset and we are trying to force the machine to sleep.
Imre Deakddeea5b2014-05-05 15:19:56 +03002396 */
Chris Wilson3dd14c02017-04-21 14:58:15 +01002397 if (vlv_wait_for_pw_status(dev_priv, mask, val))
Chris Wilsone01569a2018-04-09 10:49:05 +01002398 DRM_DEBUG_DRIVER("timeout waiting for GT wells to go %s\n",
2399 onoff(wait_for_on));
Imre Deakddeea5b2014-05-05 15:19:56 +03002400}
2401
2402static void vlv_check_no_gt_access(struct drm_i915_private *dev_priv)
2403{
2404 if (!(I915_READ(VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEERR))
2405 return;
2406
Daniel Vetter6fa283b2016-01-19 21:00:56 +01002407 DRM_DEBUG_DRIVER("GT register access while GT waking disabled\n");
Imre Deakddeea5b2014-05-05 15:19:56 +03002408 I915_WRITE(VLV_GTLC_PW_STATUS, VLV_GTLC_ALLOWWAKEERR);
2409}
2410
Sagar Kambleebc32822014-08-13 23:07:05 +05302411static int vlv_suspend_complete(struct drm_i915_private *dev_priv)
Imre Deakddeea5b2014-05-05 15:19:56 +03002412{
2413 u32 mask;
2414 int err;
2415
2416 /*
2417 * Bspec defines the following GT well on flags as debug only, so
2418 * don't treat them as hard failures.
2419 */
Chris Wilson3dd14c02017-04-21 14:58:15 +01002420 vlv_wait_for_gt_wells(dev_priv, false);
Imre Deakddeea5b2014-05-05 15:19:56 +03002421
2422 mask = VLV_GTLC_RENDER_CTX_EXISTS | VLV_GTLC_MEDIA_CTX_EXISTS;
2423 WARN_ON((I915_READ(VLV_GTLC_WAKE_CTRL) & mask) != mask);
2424
2425 vlv_check_no_gt_access(dev_priv);
2426
2427 err = vlv_force_gfx_clock(dev_priv, true);
2428 if (err)
2429 goto err1;
2430
2431 err = vlv_allow_gt_wake(dev_priv, false);
2432 if (err)
2433 goto err2;
Deepak S98711162014-12-12 14:18:16 +05302434
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -07002435 vlv_save_gunit_s0ix_state(dev_priv);
Imre Deakddeea5b2014-05-05 15:19:56 +03002436
2437 err = vlv_force_gfx_clock(dev_priv, false);
2438 if (err)
2439 goto err2;
2440
2441 return 0;
2442
2443err2:
2444 /* For safety always re-enable waking and disable gfx clock forcing */
2445 vlv_allow_gt_wake(dev_priv, true);
2446err1:
2447 vlv_force_gfx_clock(dev_priv, false);
2448
2449 return err;
2450}
2451
Sagar Kamble016970b2014-08-13 23:07:06 +05302452static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
2453 bool rpm_resume)
Imre Deakddeea5b2014-05-05 15:19:56 +03002454{
Imre Deakddeea5b2014-05-05 15:19:56 +03002455 int err;
2456 int ret;
2457
2458 /*
2459 * If any of the steps fail just try to continue, that's the best we
2460 * can do at this point. Return the first error code (which will also
2461 * leave RPM permanently disabled).
2462 */
2463 ret = vlv_force_gfx_clock(dev_priv, true);
2464
Daniele Ceraolo Spurio1bcd8682019-08-19 19:01:46 -07002465 vlv_restore_gunit_s0ix_state(dev_priv);
Imre Deakddeea5b2014-05-05 15:19:56 +03002466
2467 err = vlv_allow_gt_wake(dev_priv, true);
2468 if (!ret)
2469 ret = err;
2470
2471 err = vlv_force_gfx_clock(dev_priv, false);
2472 if (!ret)
2473 ret = err;
2474
2475 vlv_check_no_gt_access(dev_priv);
2476
Chris Wilson7c108fd2016-10-24 13:42:18 +01002477 if (rpm_resume)
Ville Syrjälä46f16e62016-10-31 22:37:22 +02002478 intel_init_clock_gating(dev_priv);
Imre Deakddeea5b2014-05-05 15:19:56 +03002479
2480 return ret;
2481}
2482
David Weinehallc49d13e2016-08-22 13:32:42 +03002483static int intel_runtime_suspend(struct device *kdev)
Paulo Zanoni8a187452013-12-06 20:32:13 -02002484{
Chris Wilson361f9dc2019-08-06 08:42:19 +01002485 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
Daniele Ceraolo Spurio1bf676c2019-06-13 16:21:52 -07002486 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
Rodrigo Vivi071b68c2019-08-06 15:22:08 +03002487 int ret = 0;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002488
Tvrtko Ursulin6772ffe2016-10-13 11:02:55 +01002489 if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev_priv)))
Imre Deak604effb2014-08-26 13:26:56 +03002490 return -ENODEV;
2491
Paulo Zanoni8a187452013-12-06 20:32:13 -02002492 DRM_DEBUG_KMS("Suspending device\n");
2493
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07002494 disable_rpm_wakeref_asserts(rpm);
Imre Deak1f814da2015-12-16 02:52:19 +02002495
Imre Deakd6102972014-05-07 19:57:49 +03002496 /*
2497 * We are safe here against re-faults, since the fault handler takes
2498 * an RPM reference.
2499 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002500 i915_gem_runtime_suspend(dev_priv);
Imre Deakd6102972014-05-07 19:57:49 +03002501
Daniele Ceraolo Spurio9dfe3452019-07-31 17:57:09 -07002502 intel_gt_runtime_suspend(&dev_priv->gt);
Alex Daia1c41992015-09-30 09:46:37 -07002503
Imre Deak2eb52522014-11-19 15:30:05 +02002504 intel_runtime_pm_disable_interrupts(dev_priv);
Imre Deakb5478bc2014-04-14 20:24:37 +03002505
Daniele Ceraolo Spuriof7de5022019-03-19 11:35:37 -07002506 intel_uncore_suspend(&dev_priv->uncore);
Hans de Goede01c799c2017-11-14 14:55:18 +01002507
Rodrigo Vivi071b68c2019-08-06 15:22:08 +03002508 intel_display_power_suspend(dev_priv);
2509
2510 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Imre Deak507e1262016-04-20 20:27:54 +03002511 ret = vlv_suspend_complete(dev_priv);
Imre Deak507e1262016-04-20 20:27:54 +03002512
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002513 if (ret) {
2514 DRM_ERROR("Runtime suspend failed, disabling it (%d)\n", ret);
Daniele Ceraolo Spuriof7de5022019-03-19 11:35:37 -07002515 intel_uncore_runtime_resume(&dev_priv->uncore);
Hans de Goede01c799c2017-11-14 14:55:18 +01002516
Daniel Vetterb9632912014-09-30 10:56:44 +02002517 intel_runtime_pm_enable_interrupts(dev_priv);
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002518
Daniele Ceraolo Spurio9dfe3452019-07-31 17:57:09 -07002519 intel_gt_runtime_resume(&dev_priv->gt);
Sagar Arun Kamble1ed21cb2018-01-24 21:16:57 +05302520
Chris Wilsone9d4c922019-10-16 15:32:33 +01002521 i915_gem_restore_fences(&dev_priv->ggtt);
Sagar Arun Kamble1ed21cb2018-01-24 21:16:57 +05302522
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07002523 enable_rpm_wakeref_asserts(rpm);
Imre Deak1f814da2015-12-16 02:52:19 +02002524
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002525 return ret;
2526 }
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03002527
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07002528 enable_rpm_wakeref_asserts(rpm);
Janusz Krzysztofik3b58a942019-07-12 13:24:28 +02002529 intel_runtime_pm_driver_release(rpm);
Mika Kuoppala55ec45c2015-12-15 16:25:08 +02002530
Daniele Ceraolo Spurio2cf7bf62019-03-25 14:49:34 -07002531 if (intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore))
Mika Kuoppala55ec45c2015-12-15 16:25:08 +02002532 DRM_ERROR("Unclaimed access detected prior to suspending\n");
2533
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07002534 rpm->suspended = true;
Kristen Carlson Accardi1fb23622014-01-14 15:36:15 -08002535
2536 /*
Paulo Zanonic8a0bd42014-08-21 17:09:38 -03002537 * FIXME: We really should find a document that references the arguments
2538 * used below!
Kristen Carlson Accardi1fb23622014-01-14 15:36:15 -08002539 */
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002540 if (IS_BROADWELL(dev_priv)) {
Paulo Zanonid37ae192015-07-30 18:20:29 -03002541 /*
2542 * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop
2543 * being detected, and the call we do at intel_runtime_resume()
2544 * won't be able to restore them. Since PCI_D3hot matches the
2545 * actual specification and appears to be working, use it.
2546 */
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002547 intel_opregion_notify_adapter(dev_priv, PCI_D3hot);
Paulo Zanonid37ae192015-07-30 18:20:29 -03002548 } else {
Paulo Zanonic8a0bd42014-08-21 17:09:38 -03002549 /*
2550 * current versions of firmware which depend on this opregion
2551 * notification have repurposed the D1 definition to mean
2552 * "runtime suspended" vs. what you would normally expect (D3)
2553 * to distinguish it from notifications that might be sent via
2554 * the suspend path.
2555 */
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002556 intel_opregion_notify_adapter(dev_priv, PCI_D1);
Paulo Zanonic8a0bd42014-08-21 17:09:38 -03002557 }
Paulo Zanoni8a187452013-12-06 20:32:13 -02002558
Daniele Ceraolo Spuriof568eee2019-03-19 11:35:35 -07002559 assert_forcewakes_inactive(&dev_priv->uncore);
Chris Wilsondc9fb092015-01-16 11:34:34 +02002560
Ander Conselvan de Oliveira21d6e0b2017-01-20 16:28:43 +02002561 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
Lyude19625e82016-06-21 17:03:44 -04002562 intel_hpd_poll_init(dev_priv);
2563
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03002564 DRM_DEBUG_KMS("Device suspended\n");
Paulo Zanoni8a187452013-12-06 20:32:13 -02002565 return 0;
2566}
2567
David Weinehallc49d13e2016-08-22 13:32:42 +03002568static int intel_runtime_resume(struct device *kdev)
Paulo Zanoni8a187452013-12-06 20:32:13 -02002569{
Chris Wilson361f9dc2019-08-06 08:42:19 +01002570 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
Daniele Ceraolo Spurio1bf676c2019-06-13 16:21:52 -07002571 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002572 int ret = 0;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002573
Tvrtko Ursulin6772ffe2016-10-13 11:02:55 +01002574 if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev_priv)))
Imre Deak604effb2014-08-26 13:26:56 +03002575 return -ENODEV;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002576
2577 DRM_DEBUG_KMS("Resuming device\n");
2578
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07002579 WARN_ON_ONCE(atomic_read(&rpm->wakeref_count));
2580 disable_rpm_wakeref_asserts(rpm);
Imre Deak1f814da2015-12-16 02:52:19 +02002581
Chris Wilson6f9f4b72016-05-23 15:08:09 +01002582 intel_opregion_notify_adapter(dev_priv, PCI_D0);
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07002583 rpm->suspended = false;
Daniele Ceraolo Spurio2cf7bf62019-03-25 14:49:34 -07002584 if (intel_uncore_unclaimed_mmio(&dev_priv->uncore))
Mika Kuoppala55ec45c2015-12-15 16:25:08 +02002585 DRM_DEBUG_DRIVER("Unclaimed access during suspend, bios?\n");
Paulo Zanoni8a187452013-12-06 20:32:13 -02002586
Rodrigo Vivi071b68c2019-08-06 15:22:08 +03002587 intel_display_power_resume(dev_priv);
2588
2589 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Paulo Zanoni1a5df182014-10-27 17:54:32 -02002590 ret = vlv_resume_prepare(dev_priv, true);
2591
Daniele Ceraolo Spuriof7de5022019-03-19 11:35:37 -07002592 intel_uncore_runtime_resume(&dev_priv->uncore);
Hans de Goedebedf4d72017-11-14 14:55:17 +01002593
Sagar Arun Kamble1ed21cb2018-01-24 21:16:57 +05302594 intel_runtime_pm_enable_interrupts(dev_priv);
2595
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002596 /*
2597 * No point of rolling back things in case of an error, as the best
2598 * we can do is to hope that things will still work (and disable RPM).
2599 */
Daniele Ceraolo Spurio9dfe3452019-07-31 17:57:09 -07002600 intel_gt_runtime_resume(&dev_priv->gt);
Chris Wilsone9d4c922019-10-16 15:32:33 +01002601 i915_gem_restore_fences(&dev_priv->ggtt);
Imre Deak92b806d2014-04-14 20:24:39 +03002602
Ville Syrjälä08d8a232015-08-27 23:56:08 +03002603 /*
2604 * On VLV/CHV display interrupts are part of the display
2605 * power well, so hpd is reinitialized from there. For
2606 * everyone else do it here.
2607 */
Wayne Boyer666a4532015-12-09 12:29:35 -08002608 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
Ville Syrjälä08d8a232015-08-27 23:56:08 +03002609 intel_hpd_init(dev_priv);
2610
Kumar, Mahesh2503a0f2017-08-17 19:15:28 +05302611 intel_enable_ipc(dev_priv);
2612
Daniele Ceraolo Spurio91026502019-06-13 16:21:51 -07002613 enable_rpm_wakeref_asserts(rpm);
Imre Deak1f814da2015-12-16 02:52:19 +02002614
Imre Deak0ab9cfe2014-04-15 16:39:45 +03002615 if (ret)
2616 DRM_ERROR("Runtime resume failed, disabling it (%d)\n", ret);
2617 else
2618 DRM_DEBUG_KMS("Device resumed\n");
2619
2620 return ret;
Paulo Zanoni8a187452013-12-06 20:32:13 -02002621}
2622
Chris Wilson42f55512016-06-24 14:00:26 +01002623const struct dev_pm_ops i915_pm_ops = {
Imre Deak5545dbb2014-10-23 19:23:28 +03002624 /*
2625 * S0ix (via system suspend) and S3 event handlers [PMSG_SUSPEND,
2626 * PMSG_RESUME]
2627 */
Chris Wilson73b66f82018-05-25 10:26:29 +01002628 .prepare = i915_pm_prepare,
Akshay Joshi0206e352011-08-16 15:34:10 -04002629 .suspend = i915_pm_suspend,
Imre Deak76c4b252014-04-01 19:55:22 +03002630 .suspend_late = i915_pm_suspend_late,
2631 .resume_early = i915_pm_resume_early,
Akshay Joshi0206e352011-08-16 15:34:10 -04002632 .resume = i915_pm_resume,
Imre Deak5545dbb2014-10-23 19:23:28 +03002633
2634 /*
2635 * S4 event handlers
2636 * @freeze, @freeze_late : called (1) before creating the
2637 * hibernation image [PMSG_FREEZE] and
2638 * (2) after rebooting, before restoring
2639 * the image [PMSG_QUIESCE]
2640 * @thaw, @thaw_early : called (1) after creating the hibernation
2641 * image, before writing it [PMSG_THAW]
2642 * and (2) after failing to create or
2643 * restore the image [PMSG_RECOVER]
2644 * @poweroff, @poweroff_late: called after writing the hibernation
2645 * image, before rebooting [PMSG_HIBERNATE]
2646 * @restore, @restore_early : called after rebooting and restoring the
2647 * hibernation image [PMSG_RESTORE]
2648 */
Chris Wilson1f19ac22016-05-14 07:26:32 +01002649 .freeze = i915_pm_freeze,
2650 .freeze_late = i915_pm_freeze_late,
2651 .thaw_early = i915_pm_thaw_early,
2652 .thaw = i915_pm_thaw,
Imre Deak36d61e62014-10-23 19:23:24 +03002653 .poweroff = i915_pm_suspend,
Imre Deakab3be732015-03-02 13:04:41 +02002654 .poweroff_late = i915_pm_poweroff_late,
Chris Wilson1f19ac22016-05-14 07:26:32 +01002655 .restore_early = i915_pm_restore_early,
2656 .restore = i915_pm_restore,
Imre Deak5545dbb2014-10-23 19:23:28 +03002657
2658 /* S0ix (via runtime suspend) event handlers */
Paulo Zanoni97bea202014-03-07 20:12:33 -03002659 .runtime_suspend = intel_runtime_suspend,
2660 .runtime_resume = intel_runtime_resume,
Zhenyu Wangcbda12d2009-12-16 13:36:10 +08002661};
2662
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002663static const struct file_operations i915_driver_fops = {
2664 .owner = THIS_MODULE,
2665 .open = drm_open,
Chris Wilson7a2c65dd2020-01-24 12:56:26 +00002666 .release = drm_release_noglobal,
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002667 .unlocked_ioctl = drm_ioctl,
Abdiel Janulguecc662122019-12-04 12:00:32 +00002668 .mmap = i915_gem_mmap,
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002669 .poll = drm_poll,
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002670 .read = drm_read,
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002671 .compat_ioctl = i915_compat_ioctl,
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002672 .llseek = noop_llseek,
2673};
2674
Chris Wilson0673ad42016-06-24 14:00:22 +01002675static int
2676i915_gem_reject_pin_ioctl(struct drm_device *dev, void *data,
2677 struct drm_file *file)
2678{
2679 return -ENODEV;
2680}
2681
2682static const struct drm_ioctl_desc i915_ioctls[] = {
2683 DRM_IOCTL_DEF_DRV(I915_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2684 DRM_IOCTL_DEF_DRV(I915_FLUSH, drm_noop, DRM_AUTH),
2685 DRM_IOCTL_DEF_DRV(I915_FLIP, drm_noop, DRM_AUTH),
2686 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, drm_noop, DRM_AUTH),
2687 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, drm_noop, DRM_AUTH),
2688 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, drm_noop, DRM_AUTH),
Christian Königb972fff2019-04-17 13:25:24 +02002689 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam_ioctl, DRM_RENDER_ALLOW),
Chris Wilson0673ad42016-06-24 14:00:22 +01002690 DRM_IOCTL_DEF_DRV(I915_SETPARAM, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2691 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
2692 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
2693 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2694 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, drm_noop, DRM_AUTH),
2695 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2696 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2697 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, drm_noop, DRM_AUTH),
2698 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, drm_noop, DRM_AUTH),
2699 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2700 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Ville Syrjälä6a20fe72018-02-07 18:48:41 +02002701 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer_ioctl, DRM_AUTH),
Christian Königb972fff2019-04-17 13:25:24 +02002702 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2_WR, i915_gem_execbuffer2_ioctl, DRM_RENDER_ALLOW),
Chris Wilson0673ad42016-06-24 14:00:22 +01002703 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
2704 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
Christian Königb972fff2019-04-17 13:25:24 +02002705 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_RENDER_ALLOW),
Chris Wilson0673ad42016-06-24 14:00:22 +01002706 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_RENDER_ALLOW),
2707 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_RENDER_ALLOW),
Christian Königb972fff2019-04-17 13:25:24 +02002708 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_RENDER_ALLOW),
Chris Wilson0673ad42016-06-24 14:00:22 +01002709 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2710 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2711 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_RENDER_ALLOW),
2712 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW),
2713 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW),
2714 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW),
Abdiel Janulguecc662122019-12-04 12:00:32 +00002715 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_OFFSET, i915_gem_mmap_offset_ioctl, DRM_RENDER_ALLOW),
Chris Wilson0673ad42016-06-24 14:00:22 +01002716 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW),
2717 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW),
Chris Wilson111dbca2017-01-10 12:10:44 +00002718 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling_ioctl, DRM_RENDER_ALLOW),
2719 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling_ioctl, DRM_RENDER_ALLOW),
Chris Wilson0673ad42016-06-24 14:00:22 +01002720 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_RENDER_ALLOW),
Ville Syrjälä6a20fe72018-02-07 18:48:41 +02002721 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id_ioctl, 0),
Chris Wilson0673ad42016-06-24 14:00:22 +01002722 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_RENDER_ALLOW),
Daniel Vetter0cd54b02018-04-20 08:51:57 +02002723 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image_ioctl, DRM_MASTER),
2724 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs_ioctl, DRM_MASTER),
2725 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey_ioctl, DRM_MASTER),
2726 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, drm_noop, DRM_MASTER),
Christian Königb972fff2019-04-17 13:25:24 +02002727 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_RENDER_ALLOW),
Chris Wilsonb9171542019-03-22 09:23:24 +00002728 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE_EXT, i915_gem_context_create_ioctl, DRM_RENDER_ALLOW),
Chris Wilson0673ad42016-06-24 14:00:22 +01002729 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_RENDER_ALLOW),
2730 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_RENDER_ALLOW),
2731 DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_gem_context_reset_stats_ioctl, DRM_RENDER_ALLOW),
2732 DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
2733 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
2734 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
Robert Braggeec688e2016-11-07 19:49:47 +00002735 DRM_IOCTL_DEF_DRV(I915_PERF_OPEN, i915_perf_open_ioctl, DRM_RENDER_ALLOW),
Emil Velikovb40237562019-05-22 16:47:01 +01002736 DRM_IOCTL_DEF_DRV(I915_PERF_ADD_CONFIG, i915_perf_add_config_ioctl, DRM_RENDER_ALLOW),
2737 DRM_IOCTL_DEF_DRV(I915_PERF_REMOVE_CONFIG, i915_perf_remove_config_ioctl, DRM_RENDER_ALLOW),
2738 DRM_IOCTL_DEF_DRV(I915_QUERY, i915_query_ioctl, DRM_RENDER_ALLOW),
Chris Wilson7f3f317a2019-05-21 22:11:25 +01002739 DRM_IOCTL_DEF_DRV(I915_GEM_VM_CREATE, i915_gem_vm_create_ioctl, DRM_RENDER_ALLOW),
2740 DRM_IOCTL_DEF_DRV(I915_GEM_VM_DESTROY, i915_gem_vm_destroy_ioctl, DRM_RENDER_ALLOW),
Chris Wilson0673ad42016-06-24 14:00:22 +01002741};
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743static struct drm_driver driver = {
Michael Witten0c547812011-08-25 17:55:54 +00002744 /* Don't use MTRRs here; the Xserver or userspace app should
2745 * deal with them for Intel hardware.
Dave Airlie792d2b92005-11-11 23:30:27 +11002746 */
Eric Anholt673a3942008-07-30 12:06:12 -07002747 .driver_features =
Daniel Vetter0424fda2019-06-17 17:39:24 +02002748 DRIVER_GEM |
Jason Ekstrandcf6e7ba2017-08-15 15:57:33 +01002749 DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_SYNCOBJ,
Chris Wilsoncad36882017-02-10 16:35:21 +00002750 .release = i915_driver_release,
Eric Anholt673a3942008-07-30 12:06:12 -07002751 .open = i915_driver_open,
Dave Airlie22eae942005-11-10 22:16:34 +11002752 .lastclose = i915_driver_lastclose,
Eric Anholt673a3942008-07-30 12:06:12 -07002753 .postclose = i915_driver_postclose,
Rafael J. Wysockid8e29202010-01-09 00:45:33 +01002754
Chris Wilsonb1f788c2016-08-04 07:52:45 +01002755 .gem_close_object = i915_gem_close_object,
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002756 .gem_free_object_unlocked = i915_gem_free_object,
Daniel Vetter1286ff72012-05-10 15:25:09 +02002757
2758 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
2759 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
2760 .gem_prime_export = i915_gem_prime_export,
2761 .gem_prime_import = i915_gem_prime_import,
2762
Ville Syrjälä7d23e592019-06-19 20:08:42 +03002763 .get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos,
2764 .get_scanout_position = i915_get_crtc_scanoutpos,
2765
Dave Airlieff72145b2011-02-07 12:16:14 +10002766 .dumb_create = i915_gem_dumb_create,
Abdiel Janulguecc662122019-12-04 12:00:32 +00002767 .dumb_map_offset = i915_gem_dumb_mmap_offset,
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 .ioctls = i915_ioctls,
Chris Wilson0673ad42016-06-24 14:00:22 +01002770 .num_ioctls = ARRAY_SIZE(i915_ioctls),
Arjan van de Vene08e96d2011-10-31 07:28:57 -07002771 .fops = &i915_driver_fops,
Dave Airlie22eae942005-11-10 22:16:34 +11002772 .name = DRIVER_NAME,
2773 .desc = DRIVER_DESC,
2774 .date = DRIVER_DATE,
2775 .major = DRIVER_MAJOR,
2776 .minor = DRIVER_MINOR,
2777 .patchlevel = DRIVER_PATCHLEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778};