drm/i915: move and split audio under display.audio and display.funcs
[linux-block.git] / drivers / gpu / drm / i915 / i915_driver.c
CommitLineData
1da177e4
LT
1/* i915_drv.c -- i830,i845,i855,i865,i915 driver -*- linux-c -*-
2 */
0d6aa60b 3/*
bc54fd1a 4 *
1da177e4
LT
5 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * All Rights Reserved.
bc54fd1a
DA
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
0d6aa60b 28 */
1da177e4 29
e5747e3a 30#include <linux/acpi.h>
0673ad47 31#include <linux/device.h>
e0cd3608 32#include <linux/module.h>
58471f63 33#include <linux/oom.h>
0673ad47
CW
34#include <linux/pci.h>
35#include <linux/pm.h>
d6102977 36#include <linux/pm_runtime.h>
0673ad47
CW
37#include <linux/pnp.h>
38#include <linux/slab.h>
ff9fbe7c 39#include <linux/string_helpers.h>
704ab614 40#include <linux/vga_switcheroo.h>
0673ad47 41#include <linux/vt.h>
0673ad47 42
6848c291 43#include <drm/drm_aperture.h>
a667fb40 44#include <drm/drm_atomic_helper.h>
d0e93599 45#include <drm/drm_ioctl.h>
7fb81e9d 46#include <drm/drm_managed.h>
d0e93599 47#include <drm/drm_probe_helper.h>
0673ad47 48
df0566a6 49#include "display/intel_acpi.h"
df0566a6
JN
50#include "display/intel_bw.h"
51#include "display/intel_cdclk.h"
1d455f8d 52#include "display/intel_display_types.h"
58471f63 53#include "display/intel_dmc.h"
379bc100 54#include "display/intel_dp.h"
9755f055 55#include "display/intel_dpt.h"
df0566a6 56#include "display/intel_fbdev.h"
df0566a6
JN
57#include "display/intel_hotplug.h"
58#include "display/intel_overlay.h"
ae880cd0 59#include "display/intel_pch_refclk.h"
df0566a6 60#include "display/intel_pipe_crc.h"
0bf1e5a8 61#include "display/intel_pps.h"
df0566a6 62#include "display/intel_sprite.h"
4fb87831 63#include "display/intel_vga.h"
379bc100 64
10be98a7 65#include "gem/i915_gem_context.h"
be137d79 66#include "gem/i915_gem_create.h"
c8eb426d 67#include "gem/i915_gem_dmabuf.h"
afa13085 68#include "gem/i915_gem_ioctls.h"
cc662126 69#include "gem/i915_gem_mman.h"
29d88083 70#include "gem/i915_gem_pm.h"
24635c51 71#include "gt/intel_gt.h"
79ffac85 72#include "gt/intel_gt_pm.h"
2248a283 73#include "gt/intel_rc6.h"
112ed2d3 74
0cfab4cb
HS
75#include "pxp/intel_pxp_pm.h"
76
5472b3f2 77#include "i915_file_private.h"
2126d3e9 78#include "i915_debugfs.h"
58471f63 79#include "i915_driver.h"
5f0d4d14 80#include "i915_drm_client.h"
0673ad47 81#include "i915_drv.h"
2564c35d 82#include "i915_getparam.h"
062705be 83#include "i915_ioc32.h"
198bca93 84#include "i915_ioctl.h"
440e2b3d 85#include "i915_irq.h"
9c9082b9 86#include "i915_memcpy.h"
db94e9f1 87#include "i915_perf.h"
a446ae2c 88#include "i915_query.h"
bdd1510c 89#include "i915_suspend.h"
63bf8301 90#include "i915_switcheroo.h"
be68261d 91#include "i915_sysfs.h"
a7f46d5b 92#include "i915_utils.h"
0673ad47 93#include "i915_vgpu.h"
d28ae3b2 94#include "intel_dram.h"
6e482b96 95#include "intel_gvt.h"
3fc794f2 96#include "intel_memory_region.h"
7e470f10 97#include "intel_pci_config.h"
4dd4375b 98#include "intel_pcode.h"
696173b0 99#include "intel_pm.h"
d1487389 100#include "intel_region_ttm.h"
fb5f432a 101#include "vlv_suspend.h"
79e53945 102
2ef6efa7
TH
103/* Intel Rapid Start Technology ACPI device name */
104static const char irst_name[] = "INT3392";
105
4588d7eb 106static const struct drm_driver i915_drm_driver;
112b715e 107
da5f53bf 108static int i915_get_bridge_dev(struct drm_i915_private *dev_priv)
0673ad47 109{
8ff5446a 110 int domain = pci_domain_nr(to_pci_dev(dev_priv->drm.dev)->bus);
57b29646
SK
111
112 dev_priv->bridge_dev =
113 pci_get_domain_bus_and_slot(domain, 0, PCI_DEVFN(0, 0));
0673ad47 114 if (!dev_priv->bridge_dev) {
00376ccf 115 drm_err(&dev_priv->drm, "bridge device not found\n");
5e9a0200 116 return -EIO;
0673ad47
CW
117 }
118 return 0;
119}
120
121/* Allocate space for the MCH regs if needed, return nonzero on error */
122static int
da5f53bf 123intel_alloc_mchbar_resource(struct drm_i915_private *dev_priv)
0673ad47 124{
651e7d48 125 int reg = GRAPHICS_VER(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
0673ad47
CW
126 u32 temp_lo, temp_hi = 0;
127 u64 mchbar_addr;
128 int ret;
129
651e7d48 130 if (GRAPHICS_VER(dev_priv) >= 4)
0673ad47
CW
131 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
132 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
133 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
134
135 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
136#ifdef CONFIG_PNP
137 if (mchbar_addr &&
138 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
139 return 0;
140#endif
141
142 /* Get some space for it */
143 dev_priv->mch_res.name = "i915 MCHBAR";
144 dev_priv->mch_res.flags = IORESOURCE_MEM;
145 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
146 &dev_priv->mch_res,
147 MCHBAR_SIZE, MCHBAR_SIZE,
148 PCIBIOS_MIN_MEM,
149 0, pcibios_align_resource,
150 dev_priv->bridge_dev);
151 if (ret) {
00376ccf 152 drm_dbg(&dev_priv->drm, "failed bus alloc: %d\n", ret);
0673ad47
CW
153 dev_priv->mch_res.start = 0;
154 return ret;
155 }
156
651e7d48 157 if (GRAPHICS_VER(dev_priv) >= 4)
0673ad47
CW
158 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
159 upper_32_bits(dev_priv->mch_res.start));
160
161 pci_write_config_dword(dev_priv->bridge_dev, reg,
162 lower_32_bits(dev_priv->mch_res.start));
163 return 0;
164}
165
166/* Setup MCHBAR if possible, return true if we should disable it again */
167static void
da5f53bf 168intel_setup_mchbar(struct drm_i915_private *dev_priv)
0673ad47 169{
651e7d48 170 int mchbar_reg = GRAPHICS_VER(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
0673ad47
CW
171 u32 temp;
172 bool enabled;
173
920a14b2 174 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
0673ad47
CW
175 return;
176
177 dev_priv->mchbar_need_disable = false;
178
50a0bc90 179 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
0673ad47
CW
180 pci_read_config_dword(dev_priv->bridge_dev, DEVEN, &temp);
181 enabled = !!(temp & DEVEN_MCHBAR_EN);
182 } else {
183 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
184 enabled = temp & 1;
185 }
186
187 /* If it's already enabled, don't have to do anything */
188 if (enabled)
189 return;
190
da5f53bf 191 if (intel_alloc_mchbar_resource(dev_priv))
0673ad47
CW
192 return;
193
194 dev_priv->mchbar_need_disable = true;
195
196 /* Space is allocated or reserved, so enable it. */
50a0bc90 197 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
0673ad47
CW
198 pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
199 temp | DEVEN_MCHBAR_EN);
200 } else {
201 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
202 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
203 }
204}
205
206static void
da5f53bf 207intel_teardown_mchbar(struct drm_i915_private *dev_priv)
0673ad47 208{
651e7d48 209 int mchbar_reg = GRAPHICS_VER(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
0673ad47
CW
210
211 if (dev_priv->mchbar_need_disable) {
50a0bc90 212 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
0673ad47
CW
213 u32 deven_val;
214
215 pci_read_config_dword(dev_priv->bridge_dev, DEVEN,
216 &deven_val);
217 deven_val &= ~DEVEN_MCHBAR_EN;
218 pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
219 deven_val);
220 } else {
221 u32 mchbar_val;
222
223 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg,
224 &mchbar_val);
225 mchbar_val &= ~1;
226 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg,
227 mchbar_val);
228 }
229 }
230
231 if (dev_priv->mch_res.start)
232 release_resource(&dev_priv->mch_res);
233}
234
0673ad47
CW
235static int i915_workqueues_init(struct drm_i915_private *dev_priv)
236{
237 /*
238 * The i915 workqueue is primarily used for batched retirement of
239 * requests (and thus managing bo) once the task has been completed
e61e0f51 240 * by the GPU. i915_retire_requests() is called directly when we
0673ad47
CW
241 * need high-priority retirement, such as waiting for an explicit
242 * bo.
243 *
244 * It is also used for periodic low-priority events, such as
245 * idle-timers and recording error state.
246 *
247 * All tasks on the workqueue are expected to acquire the dev mutex
248 * so there is no point in running more than one instance of the
249 * workqueue at any time. Use an ordered one.
250 */
251 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
252 if (dev_priv->wq == NULL)
253 goto out_err;
254
255 dev_priv->hotplug.dp_wq = alloc_ordered_workqueue("i915-dp", 0);
256 if (dev_priv->hotplug.dp_wq == NULL)
257 goto out_free_wq;
258
0673ad47
CW
259 return 0;
260
0673ad47
CW
261out_free_wq:
262 destroy_workqueue(dev_priv->wq);
263out_err:
00376ccf 264 drm_err(&dev_priv->drm, "Failed to allocate workqueues.\n");
0673ad47
CW
265
266 return -ENOMEM;
267}
268
269static void i915_workqueues_cleanup(struct drm_i915_private *dev_priv)
270{
0673ad47
CW
271 destroy_workqueue(dev_priv->hotplug.dp_wq);
272 destroy_workqueue(dev_priv->wq);
273}
274
4fc7e845
PZ
275/*
276 * We don't keep the workarounds for pre-production hardware, so we expect our
277 * driver to fail on these machines in one way or another. A little warning on
278 * dmesg may help both the user and the bug triagers.
6a7a6a98
CW
279 *
280 * Our policy for removing pre-production workarounds is to keep the
281 * current gen workarounds as a guide to the bring-up of the next gen
282 * (workarounds have a habit of persisting!). Anything older than that
283 * should be removed along with the complications they introduce.
4fc7e845
PZ
284 */
285static void intel_detect_preproduction_hw(struct drm_i915_private *dev_priv)
286{
248a124d
CW
287 bool pre = false;
288
289 pre |= IS_HSW_EARLY_SDV(dev_priv);
c314b693
MR
290 pre |= IS_SKYLAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x6;
291 pre |= IS_BROXTON(dev_priv) && INTEL_REVID(dev_priv) < 0xA;
292 pre |= IS_KABYLAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x1;
293 pre |= IS_GEMINILAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x3;
cc7a3393 294 pre |= IS_ICELAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x7;
248a124d 295
7c5ff4a2 296 if (pre) {
00376ccf 297 drm_err(&dev_priv->drm, "This is a pre-production stepping. "
4fc7e845 298 "It may not be fully functional.\n");
7c5ff4a2
CW
299 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK);
300 }
4fc7e845
PZ
301}
302
640b50fa
CW
303static void sanitize_gpu(struct drm_i915_private *i915)
304{
305 if (!INTEL_INFO(i915)->gpu_reset_clobbers_display)
2cbc876d 306 __intel_gt_reset(to_gt(i915), ALL_ENGINES);
640b50fa
CW
307}
308
0673ad47 309/**
0b61b8b0 310 * i915_driver_early_probe - setup state not requiring device access
0673ad47
CW
311 * @dev_priv: device private
312 *
313 * Initialize everything that is a "SW-only" state, that is state not
314 * requiring accessing the device or exposing the driver via kernel internal
315 * or userspace interfaces. Example steps belonging here: lock initialization,
316 * system memory allocation, setting up device specific attributes and
317 * function hooks not requiring accessing the device.
318 */
0b61b8b0 319static int i915_driver_early_probe(struct drm_i915_private *dev_priv)
0673ad47 320{
0673ad47
CW
321 int ret = 0;
322
50d84418 323 if (i915_inject_probe_failure(dev_priv))
0673ad47
CW
324 return -ENODEV;
325
805446c8 326 intel_device_info_subplatform_init(dev_priv);
ef47b7ab 327 intel_step_init(dev_priv);
805446c8 328
0a9b2630 329 intel_uncore_mmio_debug_init_early(&dev_priv->mmio_debug);
6cbe8830 330
0673ad47
CW
331 spin_lock_init(&dev_priv->irq_lock);
332 spin_lock_init(&dev_priv->gpu_error.lock);
333 mutex_init(&dev_priv->backlight_lock);
317eaa95 334
0673ad47 335 mutex_init(&dev_priv->sb_lock);
4d4dda48 336 cpu_latency_qos_add_request(&dev_priv->sb_qos, PM_QOS_DEFAULT_VALUE);
a75d035f 337
4be1c12c 338 mutex_init(&dev_priv->display.audio.mutex);
0673ad47 339 mutex_init(&dev_priv->wm.wm_mutex);
12dc5082 340 mutex_init(&dev_priv->display.pps.mutex);
9055aac7 341 mutex_init(&dev_priv->hdcp_comp_mutex);
0673ad47 342
0b1de5d5 343 i915_memcpy_init_early(dev_priv);
69c66355 344 intel_runtime_pm_init_early(&dev_priv->runtime_pm);
0b1de5d5 345
0673ad47
CW
346 ret = i915_workqueues_init(dev_priv);
347 if (ret < 0)
f3bcb0cc 348 return ret;
0673ad47 349
fb5f432a 350 ret = vlv_suspend_init(dev_priv);
1bcd8688
DCS
351 if (ret < 0)
352 goto err_workqueues;
353
d1487389
TH
354 ret = intel_region_ttm_device_init(dev_priv);
355 if (ret)
356 goto err_ttm;
357
6f76098f
DCS
358 intel_wopcm_init_early(&dev_priv->wopcm);
359
bec68cc9 360 intel_root_gt_init_early(dev_priv);
24635c51 361
5f0d4d14
TU
362 i915_drm_clients_init(&dev_priv->clients, dev_priv);
363
a3f356b2 364 i915_gem_init_early(dev_priv);
a0de908d 365
0673ad47 366 /* This must be called before any calls to HAS_PCH_* */
da5f53bf 367 intel_detect_pch(dev_priv);
0673ad47 368
192aa181 369 intel_pm_setup(dev_priv);
f28ec6f4
ID
370 ret = intel_power_domains_init(dev_priv);
371 if (ret < 0)
6f76098f 372 goto err_gem;
0673ad47
CW
373 intel_irq_init(dev_priv);
374 intel_init_display_hooks(dev_priv);
375 intel_init_clock_gating_hooks(dev_priv);
0673ad47 376
4fc7e845 377 intel_detect_preproduction_hw(dev_priv);
0673ad47
CW
378
379 return 0;
380
6f76098f 381err_gem:
f28ec6f4 382 i915_gem_cleanup_early(dev_priv);
bec68cc9 383 intel_gt_driver_late_release_all(dev_priv);
5f0d4d14 384 i915_drm_clients_fini(&dev_priv->clients);
d1487389
TH
385 intel_region_ttm_device_fini(dev_priv);
386err_ttm:
fb5f432a 387 vlv_suspend_cleanup(dev_priv);
1bcd8688 388err_workqueues:
0673ad47
CW
389 i915_workqueues_cleanup(dev_priv);
390 return ret;
391}
392
393/**
3b58a945 394 * i915_driver_late_release - cleanup the setup done in
0b61b8b0 395 * i915_driver_early_probe()
0673ad47
CW
396 * @dev_priv: device private
397 */
3b58a945 398static void i915_driver_late_release(struct drm_i915_private *dev_priv)
0673ad47 399{
cefcff8f 400 intel_irq_fini(dev_priv);
f28ec6f4 401 intel_power_domains_cleanup(dev_priv);
a0de908d 402 i915_gem_cleanup_early(dev_priv);
bec68cc9 403 intel_gt_driver_late_release_all(dev_priv);
5f0d4d14 404 i915_drm_clients_fini(&dev_priv->clients);
d1487389 405 intel_region_ttm_device_fini(dev_priv);
fb5f432a 406 vlv_suspend_cleanup(dev_priv);
0673ad47 407 i915_workqueues_cleanup(dev_priv);
a75d035f 408
4d4dda48 409 cpu_latency_qos_remove_request(&dev_priv->sb_qos);
a75d035f 410 mutex_destroy(&dev_priv->sb_lock);
8a25c4be
JN
411
412 i915_params_free(&dev_priv->params);
0673ad47
CW
413}
414
0673ad47 415/**
0b61b8b0 416 * i915_driver_mmio_probe - setup device MMIO
0673ad47
CW
417 * @dev_priv: device private
418 *
419 * Setup minimal device state necessary for MMIO accesses later in the
420 * initialization sequence. The setup here should avoid any other device-wide
421 * side effects or exposing the driver via kernel internal or user space
422 * interfaces.
423 */
0b61b8b0 424static int i915_driver_mmio_probe(struct drm_i915_private *dev_priv)
0673ad47 425{
0673ad47
CW
426 int ret;
427
50d84418 428 if (i915_inject_probe_failure(dev_priv))
0673ad47
CW
429 return -ENODEV;
430
5e9a0200
JN
431 ret = i915_get_bridge_dev(dev_priv);
432 if (ret < 0)
433 return ret;
0673ad47 434
498f02b6
DCS
435 ret = intel_uncore_init_mmio(&dev_priv->uncore);
436 if (ret)
bec68cc9 437 return ret;
498f02b6 438
25286aac
DCS
439 /* Try to make sure MCHBAR is enabled before poking at it */
440 intel_setup_mchbar(dev_priv);
c864e9ab 441 intel_device_info_runtime_init(dev_priv);
63ffbcda 442
2cbc876d 443 ret = intel_gt_init_mmio(to_gt(dev_priv));
63ffbcda
JL
444 if (ret)
445 goto err_uncore;
446
640b50fa
CW
447 /* As early as possible, scrub existing GPU state before clobbering */
448 sanitize_gpu(dev_priv);
449
0673ad47
CW
450 return 0;
451
63ffbcda 452err_uncore:
25286aac 453 intel_teardown_mchbar(dev_priv);
3de6f852 454 intel_uncore_fini_mmio(&dev_priv->uncore);
0673ad47
CW
455 pci_dev_put(dev_priv->bridge_dev);
456
457 return ret;
458}
459
460/**
0b61b8b0 461 * i915_driver_mmio_release - cleanup the setup done in i915_driver_mmio_probe()
0673ad47
CW
462 * @dev_priv: device private
463 */
3b58a945 464static void i915_driver_mmio_release(struct drm_i915_private *dev_priv)
0673ad47 465{
25286aac 466 intel_teardown_mchbar(dev_priv);
3de6f852 467 intel_uncore_fini_mmio(&dev_priv->uncore);
0673ad47
CW
468 pci_dev_put(dev_priv->bridge_dev);
469}
470
31a02eb7
MR
471/**
472 * i915_set_dma_info - set all relevant PCI dma info as configured for the
473 * platform
474 * @i915: valid i915 instance
475 *
476 * Set the dma max segment size, device and coherent masks. The dma mask set
477 * needs to occur before i915_ggtt_probe_hw.
478 *
479 * A couple of platforms have special needs. Address them as well.
480 *
481 */
482static int i915_set_dma_info(struct drm_i915_private *i915)
483{
31a02eb7
MR
484 unsigned int mask_size = INTEL_INFO(i915)->dma_mask_size;
485 int ret;
486
487 GEM_BUG_ON(!mask_size);
488
489 /*
490 * We don't have a max segment size, so set it to the max so sg's
491 * debugging layer doesn't complain
492 */
8ff5446a 493 dma_set_max_seg_size(i915->drm.dev, UINT_MAX);
31a02eb7 494
8ff5446a 495 ret = dma_set_mask(i915->drm.dev, DMA_BIT_MASK(mask_size));
31a02eb7
MR
496 if (ret)
497 goto mask_err;
498
499 /* overlay on gen2 is broken and can't address above 1G */
651e7d48 500 if (GRAPHICS_VER(i915) == 2)
31a02eb7
MR
501 mask_size = 30;
502
503 /*
504 * 965GM sometimes incorrectly writes to hardware status page (HWS)
505 * using 32bit addressing, overwriting memory if HWS is located
506 * above 4GB.
507 *
508 * The documentation also mentions an issue with undefined
509 * behaviour if any general state is accessed within a page above 4GB,
510 * which also needs to be handled carefully.
511 */
512 if (IS_I965G(i915) || IS_I965GM(i915))
513 mask_size = 32;
514
8ff5446a 515 ret = dma_set_coherent_mask(i915->drm.dev, DMA_BIT_MASK(mask_size));
31a02eb7
MR
516 if (ret)
517 goto mask_err;
518
519 return 0;
520
521mask_err:
522 drm_err(&i915->drm, "Can't set DMA mask/consistent mask (%d)\n", ret);
523 return ret;
524}
525
6a735552
AD
526static int i915_pcode_init(struct drm_i915_private *i915)
527{
528 struct intel_gt *gt;
529 int id, ret;
530
531 for_each_gt(gt, i915, id) {
532 ret = intel_pcode_init(gt->uncore);
533 if (ret) {
534 drm_err(&gt->i915->drm, "gt%d: intel_pcode_init failed %d\n", id, ret);
535 return ret;
536 }
537 }
538
539 return 0;
540}
541
0673ad47 542/**
0b61b8b0 543 * i915_driver_hw_probe - setup state requiring device access
0673ad47
CW
544 * @dev_priv: device private
545 *
546 * Setup state that requires accessing the device, but doesn't require
547 * exposing the driver via kernel internal or userspace interfaces.
548 */
0b61b8b0 549static int i915_driver_hw_probe(struct drm_i915_private *dev_priv)
0673ad47 550{
8ff5446a 551 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
138c2fca 552 struct pci_dev *root_pdev;
0673ad47
CW
553 int ret;
554
50d84418 555 if (i915_inject_probe_failure(dev_priv))
0673ad47
CW
556 return -ENODEV;
557
4bdafb9d
CW
558 if (HAS_PPGTT(dev_priv)) {
559 if (intel_vgpu_active(dev_priv) &&
ca6ac684 560 !intel_vgpu_has_full_ppgtt(dev_priv)) {
4bdafb9d
CW
561 i915_report_error(dev_priv,
562 "incompatible vGPU found, support for isolated ppGTT required\n");
563 return -ENXIO;
564 }
565 }
566
46592892
CW
567 if (HAS_EXECLISTS(dev_priv)) {
568 /*
569 * Older GVT emulation depends upon intercepting CSB mmio,
570 * which we no longer use, preferring to use the HWSP cache
571 * instead.
572 */
573 if (intel_vgpu_active(dev_priv) &&
574 !intel_vgpu_has_hwsp_emulation(dev_priv)) {
575 i915_report_error(dev_priv,
576 "old vGPU host found, support for HWSP emulation required\n");
577 return -ENXIO;
578 }
579 }
580
f6ac993f 581 /* needs to be done before ggtt probe */
d28ae3b2 582 intel_dram_edram_detect(dev_priv);
f6ac993f 583
31a02eb7
MR
584 ret = i915_set_dma_info(dev_priv);
585 if (ret)
586 return ret;
587
9f9b2792
LL
588 i915_perf_init(dev_priv);
589
cdeea858
AS
590 ret = intel_gt_assign_ggtt(to_gt(dev_priv));
591 if (ret)
592 goto err_perf;
848915c3 593
97d6d7ab 594 ret = i915_ggtt_probe_hw(dev_priv);
0673ad47 595 if (ret)
9f172f6f 596 goto err_perf;
0673ad47 597
97c9bfe3 598 ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, dev_priv->drm.driver);
f2521f77 599 if (ret)
9f172f6f 600 goto err_ggtt;
0673ad47 601
97d6d7ab 602 ret = i915_ggtt_init_hw(dev_priv);
0088e522 603 if (ret)
9f172f6f 604 goto err_ggtt;
0088e522 605
3fc794f2
CW
606 ret = intel_memory_regions_hw_probe(dev_priv);
607 if (ret)
608 goto err_ggtt;
609
bec68cc9 610 ret = intel_gt_tiles_init(dev_priv);
2dfcc7f4
MA
611 if (ret)
612 goto err_mem_regions;
613
97d6d7ab 614 ret = i915_ggtt_enable_hw(dev_priv);
0088e522 615 if (ret) {
00376ccf 616 drm_err(&dev_priv->drm, "failed to enable GGTT\n");
3fc794f2 617 goto err_mem_regions;
0088e522
CW
618 }
619
52a05c30 620 pci_set_master(pdev);
0673ad47 621
0673ad47
CW
622 /* On the 945G/GM, the chipset reports the MSI capability on the
623 * integrated graphics even though the support isn't actually there
624 * according to the published specs. It doesn't appear to function
625 * correctly in testing on 945G.
626 * This may be a side effect of MSI having been made available for PEG
627 * and the registers being closely associated.
628 *
629 * According to chipset errata, on the 965GM, MSI interrupts may
e38c2da0
VS
630 * be lost or delayed, and was defeatured. MSI interrupts seem to
631 * get lost on g4x as well, and interrupt delivery seems to stay
632 * properly dead afterwards. So we'll just disable them for all
633 * pre-gen5 chipsets.
8a29c778
LDM
634 *
635 * dp aux and gmbus irq on gen4 seems to be able to generate legacy
636 * interrupts even when in MSI mode. This results in spurious
637 * interrupt warnings if the legacy irq no. is shared with another
638 * device. The kernel then disables that interrupt source and so
639 * prevents the other device from working properly.
0673ad47 640 */
651e7d48 641 if (GRAPHICS_VER(dev_priv) >= 5) {
52a05c30 642 if (pci_enable_msi(pdev) < 0)
00376ccf 643 drm_dbg(&dev_priv->drm, "can't enable MSI");
0673ad47
CW
644 }
645
26f837e8
ZW
646 ret = intel_gvt_init(dev_priv);
647 if (ret)
7ab87ede
CW
648 goto err_msi;
649
650 intel_opregion_setup(dev_priv);
5d0c938e 651
6a735552 652 ret = i915_pcode_init(dev_priv);
41c791fc
BN
653 if (ret)
654 goto err_msi;
5d0c938e 655
cbfa59d4 656 /*
f0b29707
JRS
657 * Fill the dram structure to get the system dram info. This will be
658 * used for memory latency calculation.
cbfa59d4 659 */
d28ae3b2 660 intel_dram_detect(dev_priv);
cbfa59d4 661
c457d9cf 662 intel_bw_init_hw(dev_priv);
26f837e8 663
138c2fca
AG
664 /*
665 * FIXME: Temporary hammer to avoid freezing the machine on our DGFX
666 * This should be totally removed when we handle the pci states properly
667 * on runtime PM and on s2idle cases.
668 */
669 root_pdev = pcie_find_root_port(pdev);
670 if (root_pdev)
671 pci_d3cold_disable(root_pdev);
672
0673ad47
CW
673 return 0;
674
7ab87ede
CW
675err_msi:
676 if (pdev->msi_enabled)
677 pci_disable_msi(pdev);
3fc794f2
CW
678err_mem_regions:
679 intel_memory_regions_driver_release(dev_priv);
9f172f6f 680err_ggtt:
3b58a945 681 i915_ggtt_driver_release(dev_priv);
4d8151ae
TH
682 i915_gem_drain_freed_objects(dev_priv);
683 i915_ggtt_driver_late_release(dev_priv);
9f172f6f
CW
684err_perf:
685 i915_perf_fini(dev_priv);
0673ad47
CW
686 return ret;
687}
688
689/**
78dae1ac 690 * i915_driver_hw_remove - cleanup the setup done in i915_driver_hw_probe()
0673ad47
CW
691 * @dev_priv: device private
692 */
78dae1ac 693static void i915_driver_hw_remove(struct drm_i915_private *dev_priv)
0673ad47 694{
8ff5446a 695 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
138c2fca 696 struct pci_dev *root_pdev;
0673ad47 697
9f9b2792
LL
698 i915_perf_fini(dev_priv);
699
52a05c30
DW
700 if (pdev->msi_enabled)
701 pci_disable_msi(pdev);
138c2fca
AG
702
703 root_pdev = pcie_find_root_port(pdev);
704 if (root_pdev)
705 pci_d3cold_enable(root_pdev);
0673ad47
CW
706}
707
708/**
709 * i915_driver_register - register the driver with the rest of the system
710 * @dev_priv: device private
711 *
712 * Perform any steps necessary to make the driver available via kernel
713 * internal or userspace interfaces.
714 */
715static void i915_driver_register(struct drm_i915_private *dev_priv)
716{
91c8a326 717 struct drm_device *dev = &dev_priv->drm;
0673ad47 718
c29579d2 719 i915_gem_driver_register(dev_priv);
b46a33e2 720 i915_pmu_register(dev_priv);
0673ad47 721
9e859eb9 722 intel_vgpu_register(dev_priv);
0673ad47
CW
723
724 /* Reveal our presence to userspace */
ec3e00b4 725 if (drm_dev_register(dev, 0)) {
00376ccf
WK
726 drm_err(&dev_priv->drm,
727 "Failed to register driver for userspace access!\n");
ec3e00b4 728 return;
0673ad47
CW
729 }
730
ec3e00b4 731 i915_debugfs_register(dev_priv);
ec3e00b4 732 i915_setup_sysfs(dev_priv);
0673ad47 733
ec3e00b4
LDM
734 /* Depends on sysfs having been initialized */
735 i915_perf_register(dev_priv);
0673ad47 736
2cbc876d 737 intel_gt_driver_register(to_gt(dev_priv));
448aa911 738
141b415f 739 intel_display_driver_register(dev_priv);
07d80572 740
2cd9a689 741 intel_power_domains_enable(dev_priv);
69c66355 742 intel_runtime_pm_enable(&dev_priv->runtime_pm);
46edcdbd
JN
743
744 intel_register_dsm_handler();
745
746 if (i915_switcheroo_register(dev_priv))
747 drm_err(&dev_priv->drm, "Failed to register vga switcheroo!\n");
0673ad47
CW
748}
749
750/**
751 * i915_driver_unregister - cleanup the registration done in i915_driver_regiser()
752 * @dev_priv: device private
753 */
754static void i915_driver_unregister(struct drm_i915_private *dev_priv)
755{
46edcdbd
JN
756 i915_switcheroo_unregister(dev_priv);
757
758 intel_unregister_dsm_handler();
759
69c66355 760 intel_runtime_pm_disable(&dev_priv->runtime_pm);
2cd9a689 761 intel_power_domains_disable(dev_priv);
07d80572 762
141b415f 763 intel_display_driver_unregister(dev_priv);
448aa911 764
2cbc876d 765 intel_gt_driver_unregister(to_gt(dev_priv));
0673ad47 766
442b8c06 767 i915_perf_unregister(dev_priv);
b46a33e2 768 i915_pmu_unregister(dev_priv);
442b8c06 769
694c2828 770 i915_teardown_sysfs(dev_priv);
d69990e0 771 drm_dev_unplug(&dev_priv->drm);
0673ad47 772
c29579d2 773 i915_gem_driver_unregister(dev_priv);
0673ad47
CW
774}
775
cca08469
TU
776void
777i915_print_iommu_status(struct drm_i915_private *i915, struct drm_printer *p)
778{
ff9fbe7c 779 drm_printf(p, "iommu: %s\n",
a7f46d5b 780 str_enabled_disabled(i915_vtd_active(i915)));
cca08469
TU
781}
782
27d558a1
MW
783static void i915_welcome_messages(struct drm_i915_private *dev_priv)
784{
bdbf43d7 785 if (drm_debug_enabled(DRM_UT_DRIVER)) {
27d558a1
MW
786 struct drm_printer p = drm_debug_printer("i915 device info:");
787
805446c8 788 drm_printf(&p, "pciid=0x%04x rev=0x%02x platform=%s (subplatform=0x%x) gen=%i\n",
1787a984
JN
789 INTEL_DEVID(dev_priv),
790 INTEL_REVID(dev_priv),
791 intel_platform_name(INTEL_INFO(dev_priv)->platform),
805446c8
TU
792 intel_subplatform(RUNTIME_INFO(dev_priv),
793 INTEL_INFO(dev_priv)->platform),
651e7d48 794 GRAPHICS_VER(dev_priv));
1787a984 795
c7d3c844
JN
796 intel_device_info_print(INTEL_INFO(dev_priv),
797 RUNTIME_INFO(dev_priv), &p);
cca08469 798 i915_print_iommu_status(dev_priv, &p);
2cbc876d 799 intel_gt_info_print(&to_gt(dev_priv)->info, &p);
27d558a1
MW
800 }
801
802 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG))
00376ccf 803 drm_info(&dev_priv->drm, "DRM_I915_DEBUG enabled\n");
27d558a1 804 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
00376ccf 805 drm_info(&dev_priv->drm, "DRM_I915_DEBUG_GEM enabled\n");
6dfc4a8f 806 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM))
00376ccf
WK
807 drm_info(&dev_priv->drm,
808 "DRM_I915_DEBUG_RUNTIME_PM enabled\n");
27d558a1
MW
809}
810
55ac5a16
CW
811static struct drm_i915_private *
812i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent)
813{
814 const struct intel_device_info *match_info =
815 (struct intel_device_info *)ent->driver_data;
816 struct intel_device_info *device_info;
2c93e7b7 817 struct intel_runtime_info *runtime;
55ac5a16
CW
818 struct drm_i915_private *i915;
819
4588d7eb 820 i915 = devm_drm_dev_alloc(&pdev->dev, &i915_drm_driver,
274ed9e9
DV
821 struct drm_i915_private, drm);
822 if (IS_ERR(i915))
823 return i915;
55ac5a16 824
361f9dc2 825 pci_set_drvdata(pdev, i915);
55ac5a16 826
8a25c4be
JN
827 /* Device parameters start as a copy of module parameters. */
828 i915_params_copy(&i915->params, &i915_modparams);
829
55ac5a16
CW
830 /* Setup the write-once "constant" device info */
831 device_info = mkwrite_device_info(i915);
832 memcpy(device_info, match_info, sizeof(*device_info));
2c93e7b7
JN
833
834 /* Initialize initial runtime info from static const data and pdev. */
835 runtime = RUNTIME_INFO(i915);
836 memcpy(runtime, &INTEL_INFO(i915)->__runtime, sizeof(*runtime));
837 runtime->device_id = pdev->device;
55ac5a16 838
55ac5a16
CW
839 return i915;
840}
841
0673ad47 842/**
b01558e5 843 * i915_driver_probe - setup chip and create an initial config
d2ad3ae4
JL
844 * @pdev: PCI device
845 * @ent: matching PCI ID entry
0673ad47 846 *
b01558e5 847 * The driver probe routine has to do several things:
0673ad47
CW
848 * - drive output discovery via intel_modeset_init()
849 * - initialize the memory manager
850 * - allocate initial config memory
851 * - setup the DRM framebuffer with the allocated memory
852 */
b01558e5 853int i915_driver_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
0673ad47 854{
8eecfb39 855 struct drm_i915_private *i915;
0673ad47 856 int ret;
7d87a7f7 857
8eecfb39
JN
858 i915 = i915_driver_create(pdev, ent);
859 if (IS_ERR(i915))
860 return PTR_ERR(i915);
719388e1 861
1feb64c4 862 /* Disable nuclear pageflip by default on pre-ILK */
0f36d074 863 if (!i915->params.nuclear_pageflip && DISPLAY_VER(i915) < 5)
8eecfb39 864 i915->drm.driver_features &= ~DRIVER_ATOMIC;
1feb64c4 865
0673ad47
CW
866 ret = pci_enable_device(pdev);
867 if (ret)
cad3688f 868 goto out_fini;
1347f5b4 869
8eecfb39 870 ret = i915_driver_early_probe(i915);
0673ad47
CW
871 if (ret < 0)
872 goto out_pci_disable;
ef11bdb3 873
8eecfb39 874 disable_rpm_wakeref_asserts(&i915->runtime_pm);
1da177e4 875
9e859eb9 876 intel_vgpu_detect(i915);
9e138ea1 877
bec68cc9 878 ret = intel_gt_probe_all(i915);
0673ad47
CW
879 if (ret < 0)
880 goto out_runtime_pm_put;
79e53945 881
bec68cc9
TU
882 ret = i915_driver_mmio_probe(i915);
883 if (ret < 0)
884 goto out_tiles_cleanup;
885
8eecfb39 886 ret = i915_driver_hw_probe(i915);
0673ad47
CW
887 if (ret < 0)
888 goto out_cleanup_mmio;
30c964a6 889
d6843dda 890 ret = intel_modeset_init_noirq(i915);
0673ad47 891 if (ret < 0)
baf54385 892 goto out_cleanup_hw;
0673ad47 893
b664259f
JN
894 ret = intel_irq_install(i915);
895 if (ret)
896 goto out_cleanup_modeset;
897
d6843dda
JN
898 ret = intel_modeset_init_nogem(i915);
899 if (ret)
b664259f
JN
900 goto out_cleanup_irq;
901
d6843dda
JN
902 ret = i915_gem_init(i915);
903 if (ret)
904 goto out_cleanup_modeset2;
905
906 ret = intel_modeset_init(i915);
907 if (ret)
908 goto out_cleanup_gem;
909
8eecfb39 910 i915_driver_register(i915);
0673ad47 911
8eecfb39 912 enable_rpm_wakeref_asserts(&i915->runtime_pm);
0673ad47 913
8eecfb39 914 i915_welcome_messages(i915);
27d558a1 915
7fb81e9d
DV
916 i915->do_release = true;
917
0673ad47
CW
918 return 0;
919
d6843dda
JN
920out_cleanup_gem:
921 i915_gem_suspend(i915);
922 i915_gem_driver_remove(i915);
923 i915_gem_driver_release(i915);
924out_cleanup_modeset2:
925 /* FIXME clean up the error path */
926 intel_modeset_driver_remove(i915);
927 intel_irq_uninstall(i915);
928 intel_modeset_driver_remove_noirq(i915);
929 goto out_cleanup_modeset;
b664259f
JN
930out_cleanup_irq:
931 intel_irq_uninstall(i915);
932out_cleanup_modeset:
eb4612d8 933 intel_modeset_driver_remove_nogem(i915);
0673ad47 934out_cleanup_hw:
8eecfb39
JN
935 i915_driver_hw_remove(i915);
936 intel_memory_regions_driver_release(i915);
937 i915_ggtt_driver_release(i915);
4d8151ae
TH
938 i915_gem_drain_freed_objects(i915);
939 i915_ggtt_driver_late_release(i915);
0673ad47 940out_cleanup_mmio:
8eecfb39 941 i915_driver_mmio_release(i915);
bec68cc9
TU
942out_tiles_cleanup:
943 intel_gt_release_all(i915);
0673ad47 944out_runtime_pm_put:
8eecfb39
JN
945 enable_rpm_wakeref_asserts(&i915->runtime_pm);
946 i915_driver_late_release(i915);
0673ad47
CW
947out_pci_disable:
948 pci_disable_device(pdev);
cad3688f 949out_fini:
8eecfb39 950 i915_probe_error(i915, "Device initialization failed (%d)\n", ret);
30c964a6
RB
951 return ret;
952}
953
361f9dc2 954void i915_driver_remove(struct drm_i915_private *i915)
3bad0781 955{
361f9dc2 956 disable_rpm_wakeref_asserts(&i915->runtime_pm);
07d80572 957
361f9dc2 958 i915_driver_unregister(i915);
99c539be 959
4a8ab5ea
CW
960 /* Flush any external code that still may be under the RCU lock */
961 synchronize_rcu();
962
361f9dc2 963 i915_gem_suspend(i915);
ce1bb329 964
361f9dc2 965 intel_gvt_driver_remove(i915);
26f837e8 966
eb4612d8 967 intel_modeset_driver_remove(i915);
bcdb72ac 968
f20a60fb
JN
969 intel_irq_uninstall(i915);
970
c0ff9e5e 971 intel_modeset_driver_remove_noirq(i915);
f20a60fb 972
361f9dc2 973 i915_reset_error_state(i915);
361f9dc2 974 i915_gem_driver_remove(i915);
0673ad47 975
eb4612d8 976 intel_modeset_driver_remove_nogem(i915);
0673ad47 977
361f9dc2 978 i915_driver_hw_remove(i915);
0673ad47 979
361f9dc2 980 enable_rpm_wakeref_asserts(&i915->runtime_pm);
cad3688f
CW
981}
982
983static void i915_driver_release(struct drm_device *dev)
984{
985 struct drm_i915_private *dev_priv = to_i915(dev);
69c66355 986 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
0673ad47 987
7fb81e9d
DV
988 if (!dev_priv->do_release)
989 return;
990
69c66355 991 disable_rpm_wakeref_asserts(rpm);
47bc28d7 992
3b58a945 993 i915_gem_driver_release(dev_priv);
47bc28d7 994
3fc794f2 995 intel_memory_regions_driver_release(dev_priv);
3b58a945 996 i915_ggtt_driver_release(dev_priv);
89351925 997 i915_gem_drain_freed_objects(dev_priv);
4d8151ae 998 i915_ggtt_driver_late_release(dev_priv);
19e0a8d4 999
3b58a945 1000 i915_driver_mmio_release(dev_priv);
47bc28d7 1001
69c66355 1002 enable_rpm_wakeref_asserts(rpm);
3b58a945 1003 intel_runtime_pm_driver_release(rpm);
47bc28d7 1004
3b58a945 1005 i915_driver_late_release(dev_priv);
3bad0781
ZW
1006}
1007
0673ad47 1008static int i915_driver_open(struct drm_device *dev, struct drm_file *file)
2911a35b 1009{
829a0af2 1010 struct drm_i915_private *i915 = to_i915(dev);
0673ad47 1011 int ret;
2911a35b 1012
829a0af2 1013 ret = i915_gem_open(i915, file);
0673ad47
CW
1014 if (ret)
1015 return ret;
2911a35b 1016
0673ad47
CW
1017 return 0;
1018}
71386ef9 1019
0673ad47
CW
1020/**
1021 * i915_driver_lastclose - clean up after all DRM clients have exited
1022 * @dev: DRM device
1023 *
1024 * Take care of cleaning up after all DRM clients have exited. In the
1025 * mode setting case, we want to restore the kernel's initial mode (just
1026 * in case the last client left us in a bad state).
1027 *
1028 * Additionally, in the non-mode setting case, we'll tear down the GTT
1029 * and DMA structures, since the kernel won't be using them, and clea
1030 * up any GEM state.
1031 */
1032static void i915_driver_lastclose(struct drm_device *dev)
1033{
5df7bd13
JRS
1034 struct drm_i915_private *i915 = to_i915(dev);
1035
0673ad47 1036 intel_fbdev_restore_mode(dev);
5df7bd13
JRS
1037
1038 if (HAS_DISPLAY(i915))
1039 vga_switcheroo_process_delayed_switch();
0673ad47 1040}
2911a35b 1041
7d2ec881 1042static void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
0673ad47 1043{
7d2ec881
DV
1044 struct drm_i915_file_private *file_priv = file->driver_priv;
1045
829a0af2 1046 i915_gem_context_close(file);
5f0d4d14 1047 i915_drm_client_put(file_priv->client);
0673ad47 1048
77715906 1049 kfree_rcu(file_priv, rcu);
515b8b7e
CW
1050
1051 /* Catch up with all the deferred frees from "this" client */
1052 i915_gem_flush_free_objects(to_i915(dev));
2911a35b
BW
1053}
1054
07f9cd0b
ID
1055static void intel_suspend_encoders(struct drm_i915_private *dev_priv)
1056{
91c8a326 1057 struct drm_device *dev = &dev_priv->drm;
19c8054c 1058 struct intel_encoder *encoder;
07f9cd0b 1059
5df7bd13
JRS
1060 if (!HAS_DISPLAY(dev_priv))
1061 return;
1062
07f9cd0b 1063 drm_modeset_lock_all(dev);
19c8054c
JN
1064 for_each_intel_encoder(dev, encoder)
1065 if (encoder->suspend)
1066 encoder->suspend(encoder);
07f9cd0b
ID
1067 drm_modeset_unlock_all(dev);
1068}
1069
100fe4c0
VS
1070static void intel_shutdown_encoders(struct drm_i915_private *dev_priv)
1071{
1072 struct drm_device *dev = &dev_priv->drm;
1073 struct intel_encoder *encoder;
1074
5df7bd13
JRS
1075 if (!HAS_DISPLAY(dev_priv))
1076 return;
1077
100fe4c0
VS
1078 drm_modeset_lock_all(dev);
1079 for_each_intel_encoder(dev, encoder)
1080 if (encoder->shutdown)
1081 encoder->shutdown(encoder);
1082 drm_modeset_unlock_all(dev);
1083}
1084
fe0f1e3b
VS
1085void i915_driver_shutdown(struct drm_i915_private *i915)
1086{
19fe4ac6 1087 disable_rpm_wakeref_asserts(&i915->runtime_pm);
7962893e
ID
1088 intel_runtime_pm_disable(&i915->runtime_pm);
1089 intel_power_domains_disable(i915);
19fe4ac6 1090
5df7bd13
JRS
1091 if (HAS_DISPLAY(i915)) {
1092 drm_kms_helper_poll_disable(&i915->drm);
fe0f1e3b 1093
5df7bd13
JRS
1094 drm_atomic_helper_shutdown(&i915->drm);
1095 }
fe0f1e3b
VS
1096
1097 intel_dp_mst_suspend(i915);
1098
1099 intel_runtime_pm_disable_interrupts(i915);
1100 intel_hpd_cancel_work(i915);
1101
1102 intel_suspend_encoders(i915);
100fe4c0 1103 intel_shutdown_encoders(i915);
19fe4ac6 1104
74ff150d 1105 intel_dmc_ucode_suspend(i915);
d339ef1c 1106
421f5410
JRS
1107 i915_gem_suspend(i915);
1108
7962893e
ID
1109 /*
1110 * The only requirement is to reboot with display DC states disabled,
1111 * for now leaving all display power wells in the INIT power domain
d339ef1c
ID
1112 * enabled.
1113 *
1114 * TODO:
1115 * - unify the pci_driver::shutdown sequence here with the
1116 * pci_driver.driver.pm.poweroff,poweroff_late sequence.
1117 * - unify the driver remove and system/runtime suspend sequences with
1118 * the above unified shutdown/poweroff sequence.
7962893e
ID
1119 */
1120 intel_power_domains_driver_remove(i915);
19fe4ac6 1121 enable_rpm_wakeref_asserts(&i915->runtime_pm);
7962893e
ID
1122
1123 intel_runtime_pm_driver_release(&i915->runtime_pm);
fe0f1e3b
VS
1124}
1125
bc87229f
ID
1126static bool suspend_to_idle(struct drm_i915_private *dev_priv)
1127{
1128#if IS_ENABLED(CONFIG_ACPI_SLEEP)
1129 if (acpi_target_system_state() < ACPI_STATE_S3)
1130 return true;
1131#endif
1132 return false;
1133}
ebc32824 1134
73b66f87
CW
1135static int i915_drm_prepare(struct drm_device *dev)
1136{
1137 struct drm_i915_private *i915 = to_i915(dev);
73b66f87
CW
1138
1139 /*
1140 * NB intel_display_suspend() may issue new requests after we've
1141 * ostensibly marked the GPU as ready-to-sleep here. We need to
1142 * split out that work and pull it forward so that after point,
1143 * the GPU is not woken again.
1144 */
c56ce956 1145 return i915_gem_backup_suspend(i915);
73b66f87
CW
1146}
1147
5e365c39 1148static int i915_drm_suspend(struct drm_device *dev)
ba8bbcf6 1149{
fac5e23e 1150 struct drm_i915_private *dev_priv = to_i915(dev);
8ff5446a 1151 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
e5747e3a 1152 pci_power_t opregion_target_state;
61caf87c 1153
9102650f 1154 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1f814dac 1155
c67a470b
PZ
1156 /* We do a lot of poking in a lot of registers, make sure they work
1157 * properly. */
2cd9a689 1158 intel_power_domains_disable(dev_priv);
5df7bd13
JRS
1159 if (HAS_DISPLAY(dev_priv))
1160 drm_kms_helper_poll_disable(dev);
5bcf719b 1161
52a05c30 1162 pci_save_state(pdev);
ba8bbcf6 1163
6b72d486 1164 intel_display_suspend(dev);
2eb5252e 1165
1a4313d1 1166 intel_dp_mst_suspend(dev_priv);
7d708ee4 1167
d5818938
DV
1168 intel_runtime_pm_disable_interrupts(dev_priv);
1169 intel_hpd_cancel_work(dev_priv);
09b64267 1170
d5818938 1171 intel_suspend_encoders(dev_priv);
0e32b39c 1172
712bf364 1173 intel_suspend_hw(dev_priv);
5669fcac 1174
9755f055
ID
1175 /* Must be called before GGTT is suspended. */
1176 intel_dpt_suspend(dev_priv);
204129a2 1177 i915_ggtt_suspend(to_gt(dev_priv)->ggtt);
828c7908 1178
0f8d2a2b 1179 i915_save_display(dev_priv);
9e06dd39 1180
bc87229f 1181 opregion_target_state = suspend_to_idle(dev_priv) ? PCI_D1 : PCI_D3cold;
a950adc6 1182 intel_opregion_suspend(dev_priv, opregion_target_state);
8ee1c3db 1183
82e3b8c1 1184 intel_fbdev_set_suspend(dev, FBINFO_STATE_SUSPENDED, true);
3fa016a0 1185
62d5d69b
MK
1186 dev_priv->suspend_count++;
1187
74ff150d 1188 intel_dmc_ucode_suspend(dev_priv);
f514c2d8 1189
9102650f 1190 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1f814dac 1191
87a7d535
JRS
1192 i915_gem_drain_freed_objects(dev_priv);
1193
73b66f87 1194 return 0;
84b79f8d
RW
1195}
1196
2cd9a689
ID
1197static enum i915_drm_suspend_mode
1198get_suspend_mode(struct drm_i915_private *dev_priv, bool hibernate)
1199{
1200 if (hibernate)
1201 return I915_DRM_SUSPEND_HIBERNATE;
1202
1203 if (suspend_to_idle(dev_priv))
1204 return I915_DRM_SUSPEND_IDLE;
1205
1206 return I915_DRM_SUSPEND_MEM;
1207}
1208
c49d13ee 1209static int i915_drm_suspend_late(struct drm_device *dev, bool hibernation)
c3c09c95 1210{
c49d13ee 1211 struct drm_i915_private *dev_priv = to_i915(dev);
8ff5446a 1212 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
69c66355 1213 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
fb5f432a 1214 int ret;
c3c09c95 1215
69c66355 1216 disable_rpm_wakeref_asserts(rpm);
1f814dac 1217
ec92ad00
CW
1218 i915_gem_suspend_late(dev_priv);
1219
f7de5027 1220 intel_uncore_suspend(&dev_priv->uncore);
4c494a57 1221
2cd9a689
ID
1222 intel_power_domains_suspend(dev_priv,
1223 get_suspend_mode(dev_priv, hibernation));
73dfc227 1224
071b68cc
RV
1225 intel_display_power_suspend_late(dev_priv);
1226
fb5f432a 1227 ret = vlv_suspend_complete(dev_priv);
c3c09c95 1228 if (ret) {
00376ccf 1229 drm_err(&dev_priv->drm, "Suspend complete failed: %d\n", ret);
2cd9a689 1230 intel_power_domains_resume(dev_priv);
c3c09c95 1231
1f814dac 1232 goto out;
c3c09c95
ID
1233 }
1234
52a05c30 1235 pci_disable_device(pdev);
ab3be73f 1236 /*
54875571 1237 * During hibernation on some platforms the BIOS may try to access
ab3be73f
ID
1238 * the device even though it's already in D3 and hang the machine. So
1239 * leave the device in D0 on those platforms and hope the BIOS will
54875571
ID
1240 * power down the device properly. The issue was seen on multiple old
1241 * GENs with different BIOS vendors, so having an explicit blacklist
1242 * is inpractical; apply the workaround on everything pre GEN6. The
1243 * platforms where the issue was seen:
1244 * Lenovo Thinkpad X301, X61s, X60, T60, X41
1245 * Fujitsu FSC S7110
1246 * Acer Aspire 1830T
ab3be73f 1247 */
651e7d48 1248 if (!(hibernation && GRAPHICS_VER(dev_priv) < 6))
52a05c30 1249 pci_set_power_state(pdev, PCI_D3hot);
c3c09c95 1250
1f814dac 1251out:
69c66355 1252 enable_rpm_wakeref_asserts(rpm);
0a9b2630 1253 if (!dev_priv->uncore.user_forcewake_count)
3b58a945 1254 intel_runtime_pm_driver_release(rpm);
1f814dac
ID
1255
1256 return ret;
c3c09c95
ID
1257}
1258
b8d65b8a
JN
1259int i915_driver_suspend_switcheroo(struct drm_i915_private *i915,
1260 pm_message_t state)
84b79f8d
RW
1261{
1262 int error;
1263
48a1b8d4
PB
1264 if (drm_WARN_ON_ONCE(&i915->drm, state.event != PM_EVENT_SUSPEND &&
1265 state.event != PM_EVENT_FREEZE))
0b14cbd2 1266 return -EINVAL;
5bcf719b 1267
361f9dc2 1268 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
5bcf719b 1269 return 0;
6eecba33 1270
361f9dc2 1271 error = i915_drm_suspend(&i915->drm);
84b79f8d
RW
1272 if (error)
1273 return error;
1274
361f9dc2 1275 return i915_drm_suspend_late(&i915->drm, false);
ba8bbcf6
JB
1276}
1277
5e365c39 1278static int i915_drm_resume(struct drm_device *dev)
76c4b250 1279{
fac5e23e 1280 struct drm_i915_private *dev_priv = to_i915(dev);
ac840ae5 1281 int ret;
9d49c0ef 1282
9102650f 1283 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1f814dac 1284
6a735552 1285 ret = i915_pcode_init(dev_priv);
41c791fc
BN
1286 if (ret)
1287 return ret;
1288
640b50fa
CW
1289 sanitize_gpu(dev_priv);
1290
97d6d7ab 1291 ret = i915_ggtt_enable_hw(dev_priv);
ac840ae5 1292 if (ret)
00376ccf 1293 drm_err(&dev_priv->drm, "failed to re-enable GGTT\n");
ac840ae5 1294
204129a2 1295 i915_ggtt_resume(to_gt(dev_priv)->ggtt);
9755f055
ID
1296 /* Must be called after GGTT is resumed. */
1297 intel_dpt_resume(dev_priv);
cec5ca08 1298
74ff150d 1299 intel_dmc_ucode_resume(dev_priv);
f74ed08d 1300
0f8d2a2b 1301 i915_restore_display(dev_priv);
8090ba8c 1302 intel_pps_unlock_regs_wa(dev_priv);
61caf87c 1303
c39055b0 1304 intel_init_pch_refclk(dev_priv);
1833b134 1305
364aece0
PA
1306 /*
1307 * Interrupts have to be enabled before any batches are run. If not the
1308 * GPU will hang. i915_gem_init_hw() will initiate batches to
1309 * update/restore the context.
1310 *
908764f6
ID
1311 * drm_mode_config_reset() needs AUX interrupts.
1312 *
364aece0
PA
1313 * Modeset enabling in intel_modeset_init_hw() also needs working
1314 * interrupts.
1315 */
1316 intel_runtime_pm_enable_interrupts(dev_priv);
1317
5df7bd13
JRS
1318 if (HAS_DISPLAY(dev_priv))
1319 drm_mode_config_reset(dev);
908764f6 1320
37cd3300 1321 i915_gem_resume(dev_priv);
226485e9 1322
6cd02e77 1323 intel_modeset_init_hw(dev_priv);
675f7ff3 1324 intel_init_clock_gating(dev_priv);
4c8d4651 1325 intel_hpd_init(dev_priv);
24576d23 1326
4c8d4651 1327 /* MST sideband requires HPD interrupts enabled */
1a4313d1 1328 intel_dp_mst_resume(dev_priv);
a16b7658
L
1329 intel_display_resume(dev);
1330
4c8d4651 1331 intel_hpd_poll_disable(dev_priv);
5df7bd13
JRS
1332 if (HAS_DISPLAY(dev_priv))
1333 drm_kms_helper_poll_enable(dev);
e0b70061 1334
a950adc6 1335 intel_opregion_resume(dev_priv);
44834a67 1336
82e3b8c1 1337 intel_fbdev_set_suspend(dev, FBINFO_STATE_RUNNING, false);
073f34d9 1338
2cd9a689
ID
1339 intel_power_domains_enable(dev_priv);
1340
385fc38c
CX
1341 intel_gvt_resume(dev_priv);
1342
9102650f 1343 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1f814dac 1344
074c6ada 1345 return 0;
84b79f8d
RW
1346}
1347
5e365c39 1348static int i915_drm_resume_early(struct drm_device *dev)
84b79f8d 1349{
fac5e23e 1350 struct drm_i915_private *dev_priv = to_i915(dev);
8ff5446a 1351 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
44410cd0 1352 int ret;
36d61e67 1353
76c4b250
ID
1354 /*
1355 * We have a resume ordering issue with the snd-hda driver also
1356 * requiring our device to be power up. Due to the lack of a
1357 * parent/child relationship we currently solve this with an early
1358 * resume hook.
1359 *
1360 * FIXME: This should be solved with a special hdmi sink device or
1361 * similar so that power domains can be employed.
1362 */
44410cd0
ID
1363
1364 /*
1365 * Note that we need to set the power state explicitly, since we
1366 * powered off the device during freeze and the PCI core won't power
1367 * it back up for us during thaw. Powering off the device during
1368 * freeze is not a hard requirement though, and during the
1369 * suspend/resume phases the PCI core makes sure we get here with the
1370 * device powered on. So in case we change our freeze logic and keep
1371 * the device powered we can also remove the following set power state
1372 * call.
1373 */
52a05c30 1374 ret = pci_set_power_state(pdev, PCI_D0);
44410cd0 1375 if (ret) {
00376ccf
WK
1376 drm_err(&dev_priv->drm,
1377 "failed to set PCI D0 power state (%d)\n", ret);
2cd9a689 1378 return ret;
44410cd0
ID
1379 }
1380
1381 /*
1382 * Note that pci_enable_device() first enables any parent bridge
1383 * device and only then sets the power state for this device. The
1384 * bridge enabling is a nop though, since bridge devices are resumed
1385 * first. The order of enabling power and enabling the device is
1386 * imposed by the PCI core as described above, so here we preserve the
1387 * same order for the freeze/thaw phases.
1388 *
1389 * TODO: eventually we should remove pci_disable_device() /
1390 * pci_enable_enable_device() from suspend/resume. Due to how they
1391 * depend on the device enable refcount we can't anyway depend on them
1392 * disabling/enabling the device.
1393 */
2cd9a689
ID
1394 if (pci_enable_device(pdev))
1395 return -EIO;
84b79f8d 1396
52a05c30 1397 pci_set_master(pdev);
84b79f8d 1398
9102650f 1399 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1f814dac 1400
fb5f432a 1401 ret = vlv_resume_prepare(dev_priv, false);
36d61e67 1402 if (ret)
00376ccf 1403 drm_err(&dev_priv->drm,
fb5f432a 1404 "Resume prepare failed: %d, continuing anyway\n", ret);
36d61e67 1405
f7de5027
DCS
1406 intel_uncore_resume_early(&dev_priv->uncore);
1407
2cbc876d 1408 intel_gt_check_and_clear_faults(to_gt(dev_priv));
efee833a 1409
071b68cc 1410 intel_display_power_resume_early(dev_priv);
efee833a 1411
2cd9a689 1412 intel_power_domains_resume(dev_priv);
bc87229f 1413
9102650f 1414 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
6e35e8ab 1415
36d61e67 1416 return ret;
76c4b250
ID
1417}
1418
b8d65b8a 1419int i915_driver_resume_switcheroo(struct drm_i915_private *i915)
76c4b250 1420{
50a0072f 1421 int ret;
76c4b250 1422
361f9dc2 1423 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
097dd837
ID
1424 return 0;
1425
361f9dc2 1426 ret = i915_drm_resume_early(&i915->drm);
50a0072f
ID
1427 if (ret)
1428 return ret;
1429
361f9dc2 1430 return i915_drm_resume(&i915->drm);
5a17514e
ID
1431}
1432
73b66f87
CW
1433static int i915_pm_prepare(struct device *kdev)
1434{
361f9dc2 1435 struct drm_i915_private *i915 = kdev_to_i915(kdev);
73b66f87 1436
361f9dc2 1437 if (!i915) {
73b66f87
CW
1438 dev_err(kdev, "DRM not initialized, aborting suspend.\n");
1439 return -ENODEV;
1440 }
1441
361f9dc2 1442 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
73b66f87
CW
1443 return 0;
1444
361f9dc2 1445 return i915_drm_prepare(&i915->drm);
73b66f87
CW
1446}
1447
c49d13ee 1448static int i915_pm_suspend(struct device *kdev)
112b715e 1449{
361f9dc2 1450 struct drm_i915_private *i915 = kdev_to_i915(kdev);
112b715e 1451
361f9dc2 1452 if (!i915) {
c49d13ee 1453 dev_err(kdev, "DRM not initialized, aborting suspend.\n");
84b79f8d
RW
1454 return -ENODEV;
1455 }
112b715e 1456
2ef6efa7
TH
1457 i915_ggtt_mark_pte_lost(i915, false);
1458
361f9dc2 1459 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
5bcf719b
DA
1460 return 0;
1461
361f9dc2 1462 return i915_drm_suspend(&i915->drm);
76c4b250
ID
1463}
1464
c49d13ee 1465static int i915_pm_suspend_late(struct device *kdev)
76c4b250 1466{
361f9dc2 1467 struct drm_i915_private *i915 = kdev_to_i915(kdev);
76c4b250
ID
1468
1469 /*
c965d995 1470 * We have a suspend ordering issue with the snd-hda driver also
76c4b250
ID
1471 * requiring our device to be power up. Due to the lack of a
1472 * parent/child relationship we currently solve this with an late
1473 * suspend hook.
1474 *
1475 * FIXME: This should be solved with a special hdmi sink device or
1476 * similar so that power domains can be employed.
1477 */
361f9dc2 1478 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
76c4b250 1479 return 0;
112b715e 1480
361f9dc2 1481 return i915_drm_suspend_late(&i915->drm, false);
ab3be73f
ID
1482}
1483
c49d13ee 1484static int i915_pm_poweroff_late(struct device *kdev)
ab3be73f 1485{
361f9dc2 1486 struct drm_i915_private *i915 = kdev_to_i915(kdev);
ab3be73f 1487
361f9dc2 1488 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
ab3be73f
ID
1489 return 0;
1490
361f9dc2 1491 return i915_drm_suspend_late(&i915->drm, true);
cbda12d7
ZW
1492}
1493
c49d13ee 1494static int i915_pm_resume_early(struct device *kdev)
76c4b250 1495{
361f9dc2 1496 struct drm_i915_private *i915 = kdev_to_i915(kdev);
76c4b250 1497
361f9dc2 1498 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
097dd837
ID
1499 return 0;
1500
361f9dc2 1501 return i915_drm_resume_early(&i915->drm);
76c4b250
ID
1502}
1503
c49d13ee 1504static int i915_pm_resume(struct device *kdev)
cbda12d7 1505{
361f9dc2 1506 struct drm_i915_private *i915 = kdev_to_i915(kdev);
84b79f8d 1507
361f9dc2 1508 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
097dd837
ID
1509 return 0;
1510
2ef6efa7
TH
1511 /*
1512 * If IRST is enabled, or if we can't detect whether it's enabled,
1513 * then we must assume we lost the GGTT page table entries, since
1514 * they are not retained if IRST decided to enter S4.
1515 */
1516 if (!IS_ENABLED(CONFIG_ACPI) || acpi_dev_present(irst_name, NULL, -1))
1517 i915_ggtt_mark_pte_lost(i915, true);
1518
361f9dc2 1519 return i915_drm_resume(&i915->drm);
cbda12d7
ZW
1520}
1521
1f19ac2a 1522/* freeze: before creating the hibernation_image */
c49d13ee 1523static int i915_pm_freeze(struct device *kdev)
1f19ac2a 1524{
361f9dc2 1525 struct drm_i915_private *i915 = kdev_to_i915(kdev);
6a800eab
CW
1526 int ret;
1527
361f9dc2
CW
1528 if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) {
1529 ret = i915_drm_suspend(&i915->drm);
dd9f31c7
ID
1530 if (ret)
1531 return ret;
1532 }
6a800eab 1533
361f9dc2 1534 ret = i915_gem_freeze(i915);
6a800eab
CW
1535 if (ret)
1536 return ret;
1537
1538 return 0;
1f19ac2a
CW
1539}
1540
c49d13ee 1541static int i915_pm_freeze_late(struct device *kdev)
1f19ac2a 1542{
361f9dc2 1543 struct drm_i915_private *i915 = kdev_to_i915(kdev);
461fb99c
CW
1544 int ret;
1545
361f9dc2
CW
1546 if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) {
1547 ret = i915_drm_suspend_late(&i915->drm, true);
dd9f31c7
ID
1548 if (ret)
1549 return ret;
1550 }
461fb99c 1551
361f9dc2 1552 ret = i915_gem_freeze_late(i915);
461fb99c
CW
1553 if (ret)
1554 return ret;
1555
1556 return 0;
1f19ac2a
CW
1557}
1558
1559/* thaw: called after creating the hibernation image, but before turning off. */
c49d13ee 1560static int i915_pm_thaw_early(struct device *kdev)
1f19ac2a 1561{
c49d13ee 1562 return i915_pm_resume_early(kdev);
1f19ac2a
CW
1563}
1564
c49d13ee 1565static int i915_pm_thaw(struct device *kdev)
1f19ac2a 1566{
c49d13ee 1567 return i915_pm_resume(kdev);
1f19ac2a
CW
1568}
1569
1570/* restore: called after loading the hibernation image. */
c49d13ee 1571static int i915_pm_restore_early(struct device *kdev)
1f19ac2a 1572{
c49d13ee 1573 return i915_pm_resume_early(kdev);
1f19ac2a
CW
1574}
1575
c49d13ee 1576static int i915_pm_restore(struct device *kdev)
1f19ac2a 1577{
2ef6efa7
TH
1578 struct drm_i915_private *i915 = kdev_to_i915(kdev);
1579
1580 i915_ggtt_mark_pte_lost(i915, true);
c49d13ee 1581 return i915_pm_resume(kdev);
1f19ac2a
CW
1582}
1583
c49d13ee 1584static int intel_runtime_suspend(struct device *kdev)
8a187455 1585{
361f9dc2 1586 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1bf676cc 1587 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
fb5f432a 1588 int ret;
8a187455 1589
48a1b8d4 1590 if (drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_RUNTIME_PM(dev_priv)))
604effb7
ID
1591 return -ENODEV;
1592
c3e57159 1593 drm_dbg(&dev_priv->drm, "Suspending device\n");
8a187455 1594
9102650f 1595 disable_rpm_wakeref_asserts(rpm);
1f814dac 1596
d6102977
ID
1597 /*
1598 * We are safe here against re-faults, since the fault handler takes
1599 * an RPM reference.
1600 */
7c108fd8 1601 i915_gem_runtime_suspend(dev_priv);
d6102977 1602
2cbc876d 1603 intel_gt_runtime_suspend(to_gt(dev_priv));
a1c41994 1604
2eb5252e 1605 intel_runtime_pm_disable_interrupts(dev_priv);
b5478bcd 1606
f7de5027 1607 intel_uncore_suspend(&dev_priv->uncore);
01c799c9 1608
071b68cc
RV
1609 intel_display_power_suspend(dev_priv);
1610
fb5f432a 1611 ret = vlv_suspend_complete(dev_priv);
0ab9cfeb 1612 if (ret) {
00376ccf
WK
1613 drm_err(&dev_priv->drm,
1614 "Runtime suspend failed, disabling it (%d)\n", ret);
f7de5027 1615 intel_uncore_runtime_resume(&dev_priv->uncore);
01c799c9 1616
b963291c 1617 intel_runtime_pm_enable_interrupts(dev_priv);
0ab9cfeb 1618
2cbc876d 1619 intel_gt_runtime_resume(to_gt(dev_priv));
1ed21cb4 1620
9102650f 1621 enable_rpm_wakeref_asserts(rpm);
1f814dac 1622
0ab9cfeb
ID
1623 return ret;
1624 }
a8a8bd54 1625
9102650f 1626 enable_rpm_wakeref_asserts(rpm);
3b58a945 1627 intel_runtime_pm_driver_release(rpm);
55ec45c2 1628
2cf7bf6f 1629 if (intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore))
00376ccf
WK
1630 drm_err(&dev_priv->drm,
1631 "Unclaimed access detected prior to suspending\n");
55ec45c2 1632
9102650f 1633 rpm->suspended = true;
1fb2362b
KCA
1634
1635 /*
c8a0bd42
PZ
1636 * FIXME: We really should find a document that references the arguments
1637 * used below!
1fb2362b 1638 */
6f9f4b7a 1639 if (IS_BROADWELL(dev_priv)) {
d37ae19a
PZ
1640 /*
1641 * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop
1642 * being detected, and the call we do at intel_runtime_resume()
1643 * won't be able to restore them. Since PCI_D3hot matches the
1644 * actual specification and appears to be working, use it.
1645 */
6f9f4b7a 1646 intel_opregion_notify_adapter(dev_priv, PCI_D3hot);
d37ae19a 1647 } else {
c8a0bd42
PZ
1648 /*
1649 * current versions of firmware which depend on this opregion
1650 * notification have repurposed the D1 definition to mean
1651 * "runtime suspended" vs. what you would normally expect (D3)
1652 * to distinguish it from notifications that might be sent via
1653 * the suspend path.
1654 */
6f9f4b7a 1655 intel_opregion_notify_adapter(dev_priv, PCI_D1);
c8a0bd42 1656 }
8a187455 1657
f568eeee 1658 assert_forcewakes_inactive(&dev_priv->uncore);
dc9fb09c 1659
21d6e0bd 1660 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
4c8d4651 1661 intel_hpd_poll_enable(dev_priv);
19625e85 1662
c3e57159 1663 drm_dbg(&dev_priv->drm, "Device suspended\n");
8a187455
PZ
1664 return 0;
1665}
1666
c49d13ee 1667static int intel_runtime_resume(struct device *kdev)
8a187455 1668{
361f9dc2 1669 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1bf676cc 1670 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
fb5f432a 1671 int ret;
8a187455 1672
48a1b8d4 1673 if (drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_RUNTIME_PM(dev_priv)))
604effb7 1674 return -ENODEV;
8a187455 1675
c3e57159 1676 drm_dbg(&dev_priv->drm, "Resuming device\n");
8a187455 1677
48a1b8d4 1678 drm_WARN_ON_ONCE(&dev_priv->drm, atomic_read(&rpm->wakeref_count));
9102650f 1679 disable_rpm_wakeref_asserts(rpm);
1f814dac 1680
6f9f4b7a 1681 intel_opregion_notify_adapter(dev_priv, PCI_D0);
9102650f 1682 rpm->suspended = false;
2cf7bf6f 1683 if (intel_uncore_unclaimed_mmio(&dev_priv->uncore))
00376ccf
WK
1684 drm_dbg(&dev_priv->drm,
1685 "Unclaimed access during suspend, bios?\n");
8a187455 1686
071b68cc
RV
1687 intel_display_power_resume(dev_priv);
1688
fb5f432a 1689 ret = vlv_resume_prepare(dev_priv, true);
1a5df187 1690
f7de5027 1691 intel_uncore_runtime_resume(&dev_priv->uncore);
bedf4d79 1692
1ed21cb4
SAK
1693 intel_runtime_pm_enable_interrupts(dev_priv);
1694
0ab9cfeb
ID
1695 /*
1696 * No point of rolling back things in case of an error, as the best
1697 * we can do is to hope that things will still work (and disable RPM).
1698 */
2cbc876d 1699 intel_gt_runtime_resume(to_gt(dev_priv));
92b806d3 1700
08d8a232
VS
1701 /*
1702 * On VLV/CHV display interrupts are part of the display
1703 * power well, so hpd is reinitialized from there. For
1704 * everyone else do it here.
1705 */
4c8d4651 1706 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
08d8a232 1707 intel_hpd_init(dev_priv);
4c8d4651
VS
1708 intel_hpd_poll_disable(dev_priv);
1709 }
08d8a232 1710
2503a0fe
KM
1711 intel_enable_ipc(dev_priv);
1712
9102650f 1713 enable_rpm_wakeref_asserts(rpm);
1f814dac 1714
0ab9cfeb 1715 if (ret)
00376ccf
WK
1716 drm_err(&dev_priv->drm,
1717 "Runtime resume failed, disabling it (%d)\n", ret);
0ab9cfeb 1718 else
c3e57159 1719 drm_dbg(&dev_priv->drm, "Device resumed\n");
0ab9cfeb
ID
1720
1721 return ret;
8a187455
PZ
1722}
1723
42f5551d 1724const struct dev_pm_ops i915_pm_ops = {
5545dbbf
ID
1725 /*
1726 * S0ix (via system suspend) and S3 event handlers [PMSG_SUSPEND,
1727 * PMSG_RESUME]
1728 */
73b66f87 1729 .prepare = i915_pm_prepare,
0206e353 1730 .suspend = i915_pm_suspend,
76c4b250
ID
1731 .suspend_late = i915_pm_suspend_late,
1732 .resume_early = i915_pm_resume_early,
0206e353 1733 .resume = i915_pm_resume,
5545dbbf
ID
1734
1735 /*
1736 * S4 event handlers
1737 * @freeze, @freeze_late : called (1) before creating the
1738 * hibernation image [PMSG_FREEZE] and
1739 * (2) after rebooting, before restoring
1740 * the image [PMSG_QUIESCE]
1741 * @thaw, @thaw_early : called (1) after creating the hibernation
1742 * image, before writing it [PMSG_THAW]
1743 * and (2) after failing to create or
1744 * restore the image [PMSG_RECOVER]
1745 * @poweroff, @poweroff_late: called after writing the hibernation
1746 * image, before rebooting [PMSG_HIBERNATE]
1747 * @restore, @restore_early : called after rebooting and restoring the
1748 * hibernation image [PMSG_RESTORE]
1749 */
1f19ac2a
CW
1750 .freeze = i915_pm_freeze,
1751 .freeze_late = i915_pm_freeze_late,
1752 .thaw_early = i915_pm_thaw_early,
1753 .thaw = i915_pm_thaw,
36d61e67 1754 .poweroff = i915_pm_suspend,
ab3be73f 1755 .poweroff_late = i915_pm_poweroff_late,
1f19ac2a
CW
1756 .restore_early = i915_pm_restore_early,
1757 .restore = i915_pm_restore,
5545dbbf
ID
1758
1759 /* S0ix (via runtime suspend) event handlers */
97bea207
PZ
1760 .runtime_suspend = intel_runtime_suspend,
1761 .runtime_resume = intel_runtime_resume,
cbda12d7
ZW
1762};
1763
e08e96de
AV
1764static const struct file_operations i915_driver_fops = {
1765 .owner = THIS_MODULE,
1766 .open = drm_open,
7a2c65dd 1767 .release = drm_release_noglobal,
e08e96de 1768 .unlocked_ioctl = drm_ioctl,
cc662126 1769 .mmap = i915_gem_mmap,
e08e96de 1770 .poll = drm_poll,
e08e96de 1771 .read = drm_read,
062705be 1772 .compat_ioctl = i915_ioc32_compat_ioctl,
e08e96de 1773 .llseek = noop_llseek,
055634e4
TU
1774#ifdef CONFIG_PROC_FS
1775 .show_fdinfo = i915_drm_client_fdinfo,
1776#endif
e08e96de
AV
1777};
1778
0673ad47
CW
1779static int
1780i915_gem_reject_pin_ioctl(struct drm_device *dev, void *data,
1781 struct drm_file *file)
1782{
1783 return -ENODEV;
1784}
1785
1786static const struct drm_ioctl_desc i915_ioctls[] = {
1787 DRM_IOCTL_DEF_DRV(I915_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1788 DRM_IOCTL_DEF_DRV(I915_FLUSH, drm_noop, DRM_AUTH),
1789 DRM_IOCTL_DEF_DRV(I915_FLIP, drm_noop, DRM_AUTH),
1790 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, drm_noop, DRM_AUTH),
1791 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, drm_noop, DRM_AUTH),
1792 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, drm_noop, DRM_AUTH),
b972fffa 1793 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam_ioctl, DRM_RENDER_ALLOW),
0673ad47
CW
1794 DRM_IOCTL_DEF_DRV(I915_SETPARAM, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1795 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
1796 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
1797 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1798 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, drm_noop, DRM_AUTH),
1799 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1800 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1801 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, drm_noop, DRM_AUTH),
1802 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, drm_noop, DRM_AUTH),
1803 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1804 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
b5b6f6a6 1805 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, drm_invalid_op, DRM_AUTH),
b972fffa 1806 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2_WR, i915_gem_execbuffer2_ioctl, DRM_RENDER_ALLOW),
0673ad47
CW
1807 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1808 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
b972fffa 1809 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_RENDER_ALLOW),
0673ad47
CW
1810 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_RENDER_ALLOW),
1811 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_RENDER_ALLOW),
b972fffa 1812 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_RENDER_ALLOW),
0673ad47
CW
1813 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1814 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1815 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_RENDER_ALLOW),
ebcb4029 1816 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE_EXT, i915_gem_create_ext_ioctl, DRM_RENDER_ALLOW),
0673ad47
CW
1817 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW),
1818 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW),
1819 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW),
cc662126 1820 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_OFFSET, i915_gem_mmap_offset_ioctl, DRM_RENDER_ALLOW),
0673ad47
CW
1821 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW),
1822 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW),
111dbcab
CW
1823 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling_ioctl, DRM_RENDER_ALLOW),
1824 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling_ioctl, DRM_RENDER_ALLOW),
0673ad47 1825 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_RENDER_ALLOW),
6a20fe7b 1826 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id_ioctl, 0),
0673ad47 1827 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_RENDER_ALLOW),
0cd54b03
DV
1828 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image_ioctl, DRM_MASTER),
1829 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs_ioctl, DRM_MASTER),
1830 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey_ioctl, DRM_MASTER),
1831 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, drm_noop, DRM_MASTER),
b972fffa 1832 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_RENDER_ALLOW),
b9171541 1833 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE_EXT, i915_gem_context_create_ioctl, DRM_RENDER_ALLOW),
0673ad47
CW
1834 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_RENDER_ALLOW),
1835 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_RENDER_ALLOW),
1836 DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_gem_context_reset_stats_ioctl, DRM_RENDER_ALLOW),
1837 DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
1838 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
1839 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
eec688e1 1840 DRM_IOCTL_DEF_DRV(I915_PERF_OPEN, i915_perf_open_ioctl, DRM_RENDER_ALLOW),
b4023756
EV
1841 DRM_IOCTL_DEF_DRV(I915_PERF_ADD_CONFIG, i915_perf_add_config_ioctl, DRM_RENDER_ALLOW),
1842 DRM_IOCTL_DEF_DRV(I915_PERF_REMOVE_CONFIG, i915_perf_remove_config_ioctl, DRM_RENDER_ALLOW),
1843 DRM_IOCTL_DEF_DRV(I915_QUERY, i915_query_ioctl, DRM_RENDER_ALLOW),
7f3f317a
CW
1844 DRM_IOCTL_DEF_DRV(I915_GEM_VM_CREATE, i915_gem_vm_create_ioctl, DRM_RENDER_ALLOW),
1845 DRM_IOCTL_DEF_DRV(I915_GEM_VM_DESTROY, i915_gem_vm_destroy_ioctl, DRM_RENDER_ALLOW),
0673ad47
CW
1846};
1847
24524e3f
JN
1848/*
1849 * Interface history:
1850 *
1851 * 1.1: Original.
1852 * 1.2: Add Power Management
1853 * 1.3: Add vblank support
1854 * 1.4: Fix cmdbuffer path, add heap destroy
1855 * 1.5: Add vblank pipe configuration
1856 * 1.6: - New ioctl for scheduling buffer swaps on vertical blank
1857 * - Support vertical blank on secondary display pipe
1858 */
1859#define DRIVER_MAJOR 1
1860#define DRIVER_MINOR 6
1861#define DRIVER_PATCHLEVEL 0
1862
4588d7eb 1863static const struct drm_driver i915_drm_driver = {
0c54781b
MW
1864 /* Don't use MTRRs here; the Xserver or userspace app should
1865 * deal with them for Intel hardware.
792d2b9a 1866 */
673a394b 1867 .driver_features =
0424fdaf 1868 DRIVER_GEM |
13149e8b
LL
1869 DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_SYNCOBJ |
1870 DRIVER_SYNCOBJ_TIMELINE,
cad3688f 1871 .release = i915_driver_release,
673a394b 1872 .open = i915_driver_open,
22eae947 1873 .lastclose = i915_driver_lastclose,
673a394b 1874 .postclose = i915_driver_postclose,
d8e29209 1875
1286ff73
DV
1876 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
1877 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
1286ff73
DV
1878 .gem_prime_import = i915_gem_prime_import,
1879
ff72145b 1880 .dumb_create = i915_gem_dumb_create,
cc662126
AJ
1881 .dumb_map_offset = i915_gem_dumb_mmap_offset,
1882
1da177e4 1883 .ioctls = i915_ioctls,
0673ad47 1884 .num_ioctls = ARRAY_SIZE(i915_ioctls),
e08e96de 1885 .fops = &i915_driver_fops,
22eae947
DA
1886 .name = DRIVER_NAME,
1887 .desc = DRIVER_DESC,
1888 .date = DRIVER_DATE,
1889 .major = DRIVER_MAJOR,
1890 .minor = DRIVER_MINOR,
1891 .patchlevel = DRIVER_PATCHLEVEL,
1da177e4 1892};