i915_drv.c revision 1.20 1 /* $NetBSD: i915_drv.c,v 1.20 2021/12/18 23:45:28 riastradh Exp $ */
2
3 /* i915_drv.c -- i830,i845,i855,i865,i915 driver -*- linux-c -*-
4 */
5 /*
6 *
7 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
8 * All Rights Reserved.
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the
12 * "Software"), to deal in the Software without restriction, including
13 * without limitation the rights to use, copy, modify, merge, publish,
14 * distribute, sub license, and/or sell copies of the Software, and to
15 * permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
17 *
18 * The above copyright notice and this permission notice (including the
19 * next paragraph) shall be included in all copies or substantial portions
20 * of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
26 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 *
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: i915_drv.c,v 1.20 2021/12/18 23:45:28 riastradh Exp $");
34
35 #include <linux/acpi.h>
36 #include <linux/device.h>
37 #include <linux/oom.h>
38 #include <linux/module.h>
39 #include <linux/pci.h>
40 #include <linux/pm.h>
41 #include <linux/pm_runtime.h>
42 #include <linux/pnp.h>
43 #include <linux/slab.h>
44 #include <linux/vga_switcheroo.h>
45 #include <linux/vt.h>
46 #include <acpi/video.h>
47
48 #include <drm/drm_atomic_helper.h>
49 #include <drm/drm_ioctl.h>
50 #include <drm/drm_irq.h>
51 #include <drm/drm_probe_helper.h>
52 #include <drm/i915_drm.h>
53
54 #include "display/intel_acpi.h"
55 #include "display/intel_audio.h"
56 #include "display/intel_bw.h"
57 #include "display/intel_cdclk.h"
58 #include "display/intel_display_types.h"
59 #include "display/intel_dp.h"
60 #include "display/intel_fbdev.h"
61 #include "display/intel_hotplug.h"
62 #include "display/intel_overlay.h"
63 #include "display/intel_pipe_crc.h"
64 #include "display/intel_sprite.h"
65 #include "display/intel_vga.h"
66
67 #include "gem/i915_gem_context.h"
68 #include "gem/i915_gem_ioctls.h"
69 #include "gem/i915_gem_mman.h"
70 #include "gt/intel_gt.h"
71 #include "gt/intel_gt_pm.h"
72 #include "gt/intel_rc6.h"
73
74 #include "i915_debugfs.h"
75 #include "i915_drv.h"
76 #include "i915_irq.h"
77 #include "i915_memcpy.h"
78 #include "i915_perf.h"
79 #include "i915_query.h"
80 #include "i915_suspend.h"
81 #include "i915_switcheroo.h"
82 #include "i915_sysfs.h"
83 #include "i915_trace.h"
84 #include "i915_vgpu.h"
85 #include "intel_csr.h"
86 #include "intel_memory_region.h"
87 #include "intel_pm.h"
88
89 static struct drm_driver driver;
90
91 #ifdef __NetBSD__
92 /* XXX Kludge to expose this to NetBSD driver attachment goop. */
93 struct drm_driver *const i915_drm_driver = &driver;
94 #endif
95 struct vlv_s0ix_state {
96 /* GAM */
97 u32 wr_watermark;
98 u32 gfx_prio_ctrl;
99 u32 arb_mode;
100 u32 gfx_pend_tlb0;
101 u32 gfx_pend_tlb1;
102 u32 lra_limits[GEN7_LRA_LIMITS_REG_NUM];
103 u32 media_max_req_count;
104 u32 gfx_max_req_count;
105 u32 render_hwsp;
106 u32 ecochk;
107 u32 bsd_hwsp;
108 u32 blt_hwsp;
109 u32 tlb_rd_addr;
110
111 /* MBC */
112 u32 g3dctl;
113 u32 gsckgctl;
114 u32 mbctl;
115
116 /* GCP */
117 u32 ucgctl1;
118 u32 ucgctl3;
119 u32 rcgctl1;
120 u32 rcgctl2;
121 u32 rstctl;
122 u32 misccpctl;
123
124 /* GPM */
125 u32 gfxpause;
126 u32 rpdeuhwtc;
127 u32 rpdeuc;
128 u32 ecobus;
129 u32 pwrdwnupctl;
130 u32 rp_down_timeout;
131 u32 rp_deucsw;
132 u32 rcubmabdtmr;
133 u32 rcedata;
134 u32 spare2gh;
135
136 /* Display 1 CZ domain */
137 u32 gt_imr;
138 u32 gt_ier;
139 u32 pm_imr;
140 u32 pm_ier;
141 u32 gt_scratch[GEN7_GT_SCRATCH_REG_NUM];
142
143 /* GT SA CZ domain */
144 u32 tilectl;
145 u32 gt_fifoctl;
146 u32 gtlc_wake_ctrl;
147 u32 gtlc_survive;
148 u32 pmwgicz;
149
150 /* Display 2 CZ domain */
151 u32 gu_ctl0;
152 u32 gu_ctl1;
153 u32 pcbr;
154 u32 clock_gate_dis2;
155 };
156
157 static int i915_get_bridge_dev(struct drm_i915_private *dev_priv)
158 {
159 int domain = pci_domain_nr(dev_priv->drm.pdev->bus);
160
161 dev_priv->bridge_dev =
162 pci_get_domain_bus_and_slot(domain, 0, PCI_DEVFN(0, 0));
163 if (!dev_priv->bridge_dev) {
164 DRM_ERROR("bridge device not found\n");
165 return -1;
166 }
167 return 0;
168 }
169
170 /* Allocate space for the MCH regs if needed, return nonzero on error */
171 static int
172 intel_alloc_mchbar_resource(struct drm_i915_private *dev_priv)
173 {
174 int reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
175 u32 temp_lo, temp_hi = 0;
176 u64 mchbar_addr;
177 int ret;
178
179 if (INTEL_GEN(dev_priv) >= 4)
180 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
181 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
182 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
183
184 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
185 #ifdef CONFIG_PNP
186 if (mchbar_addr &&
187 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
188 return 0;
189 #endif
190
191 /* Get some space for it */
192 dev_priv->mch_res.name = "i915 MCHBAR";
193 dev_priv->mch_res.flags = IORESOURCE_MEM;
194 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
195 &dev_priv->mch_res,
196 MCHBAR_SIZE, MCHBAR_SIZE,
197 PCIBIOS_MIN_MEM,
198 0, pcibios_align_resource,
199 dev_priv->bridge_dev);
200 if (ret) {
201 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
202 dev_priv->mch_res.start = 0;
203 return ret;
204 }
205
206 if (INTEL_GEN(dev_priv) >= 4)
207 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
208 upper_32_bits(dev_priv->mch_res.start));
209
210 pci_write_config_dword(dev_priv->bridge_dev, reg,
211 lower_32_bits(dev_priv->mch_res.start));
212 return 0;
213 }
214
215 /* Setup MCHBAR if possible, return true if we should disable it again */
216 static void
217 intel_setup_mchbar(struct drm_i915_private *dev_priv)
218 {
219 int mchbar_reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
220 u32 temp;
221 bool enabled;
222
223 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
224 return;
225
226 dev_priv->mchbar_need_disable = false;
227
228 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
229 pci_read_config_dword(dev_priv->bridge_dev, DEVEN, &temp);
230 enabled = !!(temp & DEVEN_MCHBAR_EN);
231 } else {
232 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
233 enabled = temp & 1;
234 }
235
236 /* If it's already enabled, don't have to do anything */
237 if (enabled)
238 return;
239
240 if (intel_alloc_mchbar_resource(dev_priv))
241 return;
242
243 dev_priv->mchbar_need_disable = true;
244
245 /* Space is allocated or reserved, so enable it. */
246 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
247 pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
248 temp | DEVEN_MCHBAR_EN);
249 } else {
250 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
251 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
252 }
253 }
254
255 static void
256 intel_teardown_mchbar(struct drm_i915_private *dev_priv)
257 {
258 int mchbar_reg = INTEL_GEN(dev_priv) >= 4 ? MCHBAR_I965 : MCHBAR_I915;
259
260 if (dev_priv->mchbar_need_disable) {
261 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
262 u32 deven_val;
263
264 pci_read_config_dword(dev_priv->bridge_dev, DEVEN,
265 &deven_val);
266 deven_val &= ~DEVEN_MCHBAR_EN;
267 pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
268 deven_val);
269 } else {
270 u32 mchbar_val;
271
272 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg,
273 &mchbar_val);
274 mchbar_val &= ~1;
275 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg,
276 mchbar_val);
277 }
278 }
279
280 if (dev_priv->mch_res.start)
281 release_resource(&dev_priv->mch_res);
282 }
283
284 static int i915_driver_modeset_probe(struct drm_i915_private *i915)
285 {
286 int ret;
287
288 if (i915_inject_probe_failure(i915))
289 return -ENODEV;
290
291 if (HAS_DISPLAY(i915) && INTEL_DISPLAY_ENABLED(i915)) {
292 ret = drm_vblank_init(&i915->drm,
293 INTEL_NUM_PIPES(i915));
294 if (ret)
295 goto out;
296 }
297
298 intel_bios_init(i915);
299
300 ret = intel_vga_register(i915);
301 if (ret)
302 goto out;
303
304 intel_register_dsm_handler();
305
306 ret = i915_switcheroo_register(i915);
307 if (ret)
308 goto cleanup_vga_client;
309
310 intel_power_domains_init_hw(i915, false);
311
312 intel_csr_ucode_init(i915);
313
314 ret = intel_irq_install(i915);
315 if (ret)
316 goto cleanup_csr;
317
318 /* Important: The output setup functions called by modeset_init need
319 * working irqs for e.g. gmbus and dp aux transfers. */
320 ret = intel_modeset_init(i915);
321 if (ret)
322 goto cleanup_irq;
323
324 ret = i915_gem_init(i915);
325 if (ret)
326 goto cleanup_modeset;
327
328 intel_overlay_setup(i915);
329
330 if (!HAS_DISPLAY(i915) || !INTEL_DISPLAY_ENABLED(i915))
331 return 0;
332
333 ret = intel_fbdev_init(&i915->drm);
334 if (ret)
335 goto cleanup_gem;
336
337 /* Only enable hotplug handling once the fbdev is fully set up. */
338 intel_hpd_init(i915);
339
340 intel_init_ipc(i915);
341
342 return 0;
343
344 cleanup_gem:
345 i915_gem_suspend(i915);
346 i915_gem_driver_remove(i915);
347 i915_gem_driver_release(i915);
348 cleanup_modeset:
349 intel_modeset_driver_remove(i915);
350 cleanup_irq:
351 intel_irq_uninstall(i915);
352 cleanup_csr:
353 intel_csr_ucode_fini(i915);
354 intel_power_domains_driver_remove(i915);
355 i915_switcheroo_unregister(i915);
356 cleanup_vga_client:
357 intel_vga_unregister(i915);
358 out:
359 return ret;
360 }
361
362 static void i915_driver_modeset_remove(struct drm_i915_private *i915)
363 {
364 intel_modeset_driver_remove(i915);
365
366 intel_irq_uninstall(i915);
367
368 intel_bios_driver_remove(i915);
369
370 i915_switcheroo_unregister(i915);
371
372 intel_vga_unregister(i915);
373
374 intel_csr_ucode_fini(i915);
375 }
376
377 static void intel_init_dpio(struct drm_i915_private *dev_priv)
378 {
379 /*
380 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
381 * CHV x1 PHY (DP/HDMI D)
382 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
383 */
384 if (IS_CHERRYVIEW(dev_priv)) {
385 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
386 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
387 } else if (IS_VALLEYVIEW(dev_priv)) {
388 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
389 }
390 }
391
392 static int i915_workqueues_init(struct drm_i915_private *dev_priv)
393 {
394 /*
395 * The i915 workqueue is primarily used for batched retirement of
396 * requests (and thus managing bo) once the task has been completed
397 * by the GPU. i915_retire_requests() is called directly when we
398 * need high-priority retirement, such as waiting for an explicit
399 * bo.
400 *
401 * It is also used for periodic low-priority events, such as
402 * idle-timers and recording error state.
403 *
404 * All tasks on the workqueue are expected to acquire the dev mutex
405 * so there is no point in running more than one instance of the
406 * workqueue at any time. Use an ordered one.
407 */
408 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
409 if (dev_priv->wq == NULL)
410 goto out_err;
411
412 dev_priv->hotplug.dp_wq = alloc_ordered_workqueue("i915-dp", 0);
413 if (dev_priv->hotplug.dp_wq == NULL)
414 goto out_free_wq;
415
416 return 0;
417
418 out_free_wq:
419 destroy_workqueue(dev_priv->wq);
420 out_err:
421 DRM_ERROR("Failed to allocate workqueues.\n");
422
423 return -ENOMEM;
424 }
425
426 static void i915_workqueues_cleanup(struct drm_i915_private *dev_priv)
427 {
428 destroy_workqueue(dev_priv->hotplug.dp_wq);
429 destroy_workqueue(dev_priv->wq);
430 }
431
432 static const struct intel_device_info intel_kabylake_info = {
433 .is_kabylake = 1,
434 .gen = 9,
435 .num_pipes = 3,
436 .need_gfx_hws = 1, .has_hotplug = 1,
437 .ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING,
438 .has_llc = 1,
439 .has_ddi = 1,
440 .has_fpga_dbg = 1,
441 .has_fbc = 1,
442 GEN_DEFAULT_PIPEOFFSETS,
443 IVB_CURSOR_OFFSETS,
444 };
445
446 static const struct intel_device_info intel_kabylake_gt3_info = {
447 .is_kabylake = 1,
448 .gen = 9,
449 .num_pipes = 3,
450 .need_gfx_hws = 1, .has_hotplug = 1,
451 .ring_mask = RENDER_RING | BSD_RING | BLT_RING | VEBOX_RING | BSD2_RING,
452 .has_llc = 1,
453 .has_ddi = 1,
454 .has_fpga_dbg = 1,
455 .has_fbc = 1,
456 GEN_DEFAULT_PIPEOFFSETS,
457 IVB_CURSOR_OFFSETS,
458 };
459
460 /*
461 * We don't keep the workarounds for pre-production hardware, so we expect our
462 * driver to fail on these machines in one way or another. A little warning on
463 * dmesg may help both the user and the bug triagers.
464 *
465 * Our policy for removing pre-production workarounds is to keep the
466 * current gen workarounds as a guide to the bring-up of the next gen
467 * (workarounds have a habit of persisting!). Anything older than that
468 * should be removed along with the complications they introduce.
469 */
470 static void intel_detect_preproduction_hw(struct drm_i915_private *dev_priv)
471 {
472 bool pre = false;
473
474 pre |= IS_HSW_EARLY_SDV(dev_priv);
475 pre |= IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0);
476 pre |= IS_BXT_REVID(dev_priv, 0, BXT_REVID_B_LAST);
477 pre |= IS_KBL_REVID(dev_priv, 0, KBL_REVID_A0);
478
479 if (pre) {
480 DRM_ERROR("This is a pre-production stepping. "
481 "It may not be fully functional.\n");
482 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK);
483 }
484 }
485
486 static int vlv_alloc_s0ix_state(struct drm_i915_private *i915)
487 {
488 if (!IS_VALLEYVIEW(i915))
489 return 0;
490
491 /* we write all the values in the struct, so no need to zero it out */
492 i915->vlv_s0ix_state = kmalloc(sizeof(*i915->vlv_s0ix_state),
493 GFP_KERNEL);
494 if (!i915->vlv_s0ix_state)
495 return -ENOMEM;
496
497 return 0;
498 }
499
500 static void vlv_free_s0ix_state(struct drm_i915_private *i915)
501 {
502 if (!i915->vlv_s0ix_state)
503 return;
504
505 kfree(i915->vlv_s0ix_state);
506 i915->vlv_s0ix_state = NULL;
507 }
508
509 static void sanitize_gpu(struct drm_i915_private *i915)
510 {
511 if (!INTEL_INFO(i915)->gpu_reset_clobbers_display)
512 __intel_gt_reset(&i915->gt, ALL_ENGINES);
513 }
514
515 /**
516 * i915_driver_early_probe - setup state not requiring device access
517 * @dev_priv: device private
518 *
519 * Initialize everything that is a "SW-only" state, that is state not
520 * requiring accessing the device or exposing the driver via kernel internal
521 * or userspace interfaces. Example steps belonging here: lock initialization,
522 * system memory allocation, setting up device specific attributes and
523 * function hooks not requiring accessing the device.
524 */
525 static int i915_driver_early_probe(struct drm_i915_private *dev_priv)
526 {
527 int ret = 0;
528
529 if (i915_inject_probe_failure(dev_priv))
530 return -ENODEV;
531
532 intel_device_info_subplatform_init(dev_priv);
533
534 intel_uncore_mmio_debug_init_early(&dev_priv->mmio_debug);
535 intel_uncore_init_early(&dev_priv->uncore, dev_priv);
536
537 spin_lock_init(&dev_priv->irq_lock);
538 spin_lock_init(&dev_priv->gpu_error.lock);
539 mutex_init(&dev_priv->backlight_lock);
540
541 mutex_init(&dev_priv->sb_lock);
542 pm_qos_add_request(&dev_priv->sb_qos,
543 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
544
545 mutex_init(&dev_priv->av_mutex);
546 mutex_init(&dev_priv->wm.wm_mutex);
547 mutex_init(&dev_priv->pps_mutex);
548 mutex_init(&dev_priv->hdcp_comp_mutex);
549
550 i915_memcpy_init_early(dev_priv);
551 intel_runtime_pm_init_early(&dev_priv->runtime_pm);
552
553 ret = i915_workqueues_init(dev_priv);
554 if (ret < 0)
555 return ret;
556
557 ret = vlv_alloc_s0ix_state(dev_priv);
558 if (ret < 0)
559 goto err_workqueues;
560
561 intel_wopcm_init_early(&dev_priv->wopcm);
562
563 intel_gt_init_early(&dev_priv->gt, dev_priv);
564
565 i915_gem_init_early(dev_priv);
566
567 /* This must be called before any calls to HAS_PCH_* */
568 intel_detect_pch(dev_priv);
569
570 intel_pm_setup(dev_priv);
571 intel_init_dpio(dev_priv);
572 ret = intel_power_domains_init(dev_priv);
573 if (ret < 0)
574 goto err_gem;
575 intel_irq_init(dev_priv);
576 intel_init_display_hooks(dev_priv);
577 intel_init_clock_gating_hooks(dev_priv);
578 intel_init_audio_hooks(dev_priv);
579 intel_display_crc_init(dev_priv);
580
581 intel_detect_preproduction_hw(dev_priv);
582
583 return 0;
584
585 err_gem:
586 i915_gem_cleanup_early(dev_priv);
587 intel_gt_driver_late_release(&dev_priv->gt);
588 vlv_free_s0ix_state(dev_priv);
589 err_workqueues:
590 i915_workqueues_cleanup(dev_priv);
591 return ret;
592 }
593
594 /**
595 * i915_driver_late_release - cleanup the setup done in
596 * i915_driver_early_probe()
597 * @dev_priv: device private
598 */
599 static void i915_driver_late_release(struct drm_i915_private *dev_priv)
600 {
601 intel_irq_fini(dev_priv);
602 intel_power_domains_cleanup(dev_priv);
603 i915_gem_cleanup_early(dev_priv);
604 intel_gt_driver_late_release(&dev_priv->gt);
605 vlv_free_s0ix_state(dev_priv);
606 i915_workqueues_cleanup(dev_priv);
607
608 pm_qos_remove_request(&dev_priv->sb_qos);
609 mutex_destroy(&dev_priv->sb_lock);
610 }
611
612 /**
613 * i915_driver_mmio_probe - setup device MMIO
614 * @dev_priv: device private
615 *
616 * Setup minimal device state necessary for MMIO accesses later in the
617 * initialization sequence. The setup here should avoid any other device-wide
618 * side effects or exposing the driver via kernel internal or user space
619 * interfaces.
620 */
621 static int i915_driver_mmio_probe(struct drm_i915_private *dev_priv)
622 {
623 int ret;
624
625 if (i915_inject_probe_failure(dev_priv))
626 return -ENODEV;
627
628 if (i915_get_bridge_dev(dev_priv))
629 return -EIO;
630
631 ret = intel_uncore_init_mmio(&dev_priv->uncore);
632 if (ret < 0)
633 goto err_bridge;
634
635 /* Try to make sure MCHBAR is enabled before poking at it */
636 intel_setup_mchbar(dev_priv);
637
638 intel_device_info_init_mmio(dev_priv);
639
640 intel_uncore_prune_mmio_domains(&dev_priv->uncore);
641
642 intel_uc_init_mmio(&dev_priv->gt.uc);
643
644 ret = intel_engines_init_mmio(&dev_priv->gt);
645 if (ret)
646 goto err_uncore;
647
648 /* As early as possible, scrub existing GPU state before clobbering */
649 sanitize_gpu(dev_priv);
650
651 return 0;
652
653 err_uncore:
654 intel_teardown_mchbar(dev_priv);
655 intel_uncore_fini_mmio(&dev_priv->uncore);
656 err_bridge:
657 pci_dev_put(dev_priv->bridge_dev);
658
659 return ret;
660 }
661
662 /**
663 * i915_driver_mmio_release - cleanup the setup done in i915_driver_mmio_probe()
664 * @dev_priv: device private
665 */
666 static void i915_driver_mmio_release(struct drm_i915_private *dev_priv)
667 {
668 intel_teardown_mchbar(dev_priv);
669 intel_uncore_fini_mmio(&dev_priv->uncore);
670 pci_dev_put(dev_priv->bridge_dev);
671 }
672
673 static void intel_sanitize_options(struct drm_i915_private *dev_priv)
674 {
675 intel_gvt_sanitize_options(dev_priv);
676 }
677
678 #define DRAM_TYPE_STR(type) [INTEL_DRAM_ ## type] = #type
679
680 static const char *intel_dram_type_str(enum intel_dram_type type)
681 {
682 static const char * const str[] = {
683 DRAM_TYPE_STR(UNKNOWN),
684 DRAM_TYPE_STR(DDR3),
685 DRAM_TYPE_STR(DDR4),
686 DRAM_TYPE_STR(LPDDR3),
687 DRAM_TYPE_STR(LPDDR4),
688 };
689
690 if (type >= ARRAY_SIZE(str))
691 type = INTEL_DRAM_UNKNOWN;
692
693 return str[type];
694 }
695
696 #undef DRAM_TYPE_STR
697
698 static int intel_dimm_num_devices(const struct dram_dimm_info *dimm)
699 {
700 return dimm->ranks * 64 / (dimm->width ?: 1);
701 }
702
703 /* Returns total GB for the whole DIMM */
704 static int skl_get_dimm_size(u16 val)
705 {
706 return val & SKL_DRAM_SIZE_MASK;
707 }
708
709 static int skl_get_dimm_width(u16 val)
710 {
711 if (skl_get_dimm_size(val) == 0)
712 return 0;
713
714 switch (val & SKL_DRAM_WIDTH_MASK) {
715 case SKL_DRAM_WIDTH_X8:
716 case SKL_DRAM_WIDTH_X16:
717 case SKL_DRAM_WIDTH_X32:
718 val = (val & SKL_DRAM_WIDTH_MASK) >> SKL_DRAM_WIDTH_SHIFT;
719 return 8 << val;
720 default:
721 MISSING_CASE(val);
722 return 0;
723 }
724 }
725
726 static int skl_get_dimm_ranks(u16 val)
727 {
728 if (skl_get_dimm_size(val) == 0)
729 return 0;
730
731 val = (val & SKL_DRAM_RANK_MASK) >> SKL_DRAM_RANK_SHIFT;
732
733 return val + 1;
734 }
735
736 /* Returns total GB for the whole DIMM */
737 static int cnl_get_dimm_size(u16 val)
738 {
739 return (val & CNL_DRAM_SIZE_MASK) / 2;
740 }
741
742 static int cnl_get_dimm_width(u16 val)
743 {
744 if (cnl_get_dimm_size(val) == 0)
745 return 0;
746
747 switch (val & CNL_DRAM_WIDTH_MASK) {
748 case CNL_DRAM_WIDTH_X8:
749 case CNL_DRAM_WIDTH_X16:
750 case CNL_DRAM_WIDTH_X32:
751 val = (val & CNL_DRAM_WIDTH_MASK) >> CNL_DRAM_WIDTH_SHIFT;
752 return 8 << val;
753 default:
754 MISSING_CASE(val);
755 return 0;
756 }
757 }
758
759 static int cnl_get_dimm_ranks(u16 val)
760 {
761 if (cnl_get_dimm_size(val) == 0)
762 return 0;
763
764 val = (val & CNL_DRAM_RANK_MASK) >> CNL_DRAM_RANK_SHIFT;
765
766 return val + 1;
767 }
768
769 static bool
770 skl_is_16gb_dimm(const struct dram_dimm_info *dimm)
771 {
772 /* Convert total GB to Gb per DRAM device */
773 return 8 * dimm->size / (intel_dimm_num_devices(dimm) ?: 1) == 16;
774 }
775
776 static void
777 skl_dram_get_dimm_info(struct drm_i915_private *dev_priv,
778 struct dram_dimm_info *dimm,
779 int channel, char dimm_name, u16 val)
780 {
781 if (INTEL_GEN(dev_priv) >= 10) {
782 dimm->size = cnl_get_dimm_size(val);
783 dimm->width = cnl_get_dimm_width(val);
784 dimm->ranks = cnl_get_dimm_ranks(val);
785 } else {
786 dimm->size = skl_get_dimm_size(val);
787 dimm->width = skl_get_dimm_width(val);
788 dimm->ranks = skl_get_dimm_ranks(val);
789 }
790
791 DRM_DEBUG_KMS("CH%u DIMM %c size: %u GB, width: X%u, ranks: %u, 16Gb DIMMs: %s\n",
792 channel, dimm_name, dimm->size, dimm->width, dimm->ranks,
793 yesno(skl_is_16gb_dimm(dimm)));
794 }
795
796 static int
797 skl_dram_get_channel_info(struct drm_i915_private *dev_priv,
798 struct dram_channel_info *ch,
799 int channel, u32 val)
800 {
801 skl_dram_get_dimm_info(dev_priv, &ch->dimm_l,
802 channel, 'L', val & 0xffff);
803 skl_dram_get_dimm_info(dev_priv, &ch->dimm_s,
804 channel, 'S', val >> 16);
805
806 if (ch->dimm_l.size == 0 && ch->dimm_s.size == 0) {
807 DRM_DEBUG_KMS("CH%u not populated\n", channel);
808 return -EINVAL;
809 }
810
811 if (ch->dimm_l.ranks == 2 || ch->dimm_s.ranks == 2)
812 ch->ranks = 2;
813 else if (ch->dimm_l.ranks == 1 && ch->dimm_s.ranks == 1)
814 ch->ranks = 2;
815 else
816 ch->ranks = 1;
817
818 ch->is_16gb_dimm =
819 skl_is_16gb_dimm(&ch->dimm_l) ||
820 skl_is_16gb_dimm(&ch->dimm_s);
821
822 DRM_DEBUG_KMS("CH%u ranks: %u, 16Gb DIMMs: %s\n",
823 channel, ch->ranks, yesno(ch->is_16gb_dimm));
824
825 return 0;
826 }
827
828 static bool
829 intel_is_dram_symmetric(const struct dram_channel_info *ch0,
830 const struct dram_channel_info *ch1)
831 {
832 return !memcmp(ch0, ch1, sizeof(*ch0)) &&
833 (ch0->dimm_s.size == 0 ||
834 !memcmp(&ch0->dimm_l, &ch0->dimm_s, sizeof(ch0->dimm_l)));
835 }
836
837 static int
838 skl_dram_get_channels_info(struct drm_i915_private *dev_priv)
839 {
840 struct dram_info *dram_info = &dev_priv->dram_info;
841 struct dram_channel_info ch0 = {}, ch1 = {};
842 u32 val;
843 int ret;
844
845 val = I915_READ(SKL_MAD_DIMM_CH0_0_0_0_MCHBAR_MCMAIN);
846 ret = skl_dram_get_channel_info(dev_priv, &ch0, 0, val);
847 if (ret == 0)
848 dram_info->num_channels++;
849
850 val = I915_READ(SKL_MAD_DIMM_CH1_0_0_0_MCHBAR_MCMAIN);
851 ret = skl_dram_get_channel_info(dev_priv, &ch1, 1, val);
852 if (ret == 0)
853 dram_info->num_channels++;
854
855 if (dram_info->num_channels == 0) {
856 DRM_INFO("Number of memory channels is zero\n");
857 return -EINVAL;
858 }
859
860 /*
861 * If any of the channel is single rank channel, worst case output
862 * will be same as if single rank memory, so consider single rank
863 * memory.
864 */
865 if (ch0.ranks == 1 || ch1.ranks == 1)
866 dram_info->ranks = 1;
867 else
868 dram_info->ranks = max(ch0.ranks, ch1.ranks);
869
870 if (dram_info->ranks == 0) {
871 DRM_INFO("couldn't get memory rank information\n");
872 return -EINVAL;
873 }
874
875 dram_info->is_16gb_dimm = ch0.is_16gb_dimm || ch1.is_16gb_dimm;
876
877 dram_info->symmetric_memory = intel_is_dram_symmetric(&ch0, &ch1);
878
879 DRM_DEBUG_KMS("Memory configuration is symmetric? %s\n",
880 yesno(dram_info->symmetric_memory));
881 return 0;
882 }
883
884 #ifdef __NetBSD__
885 /* XXX Kludge to expose this to NetBSD driver attachment goop. */
886 const struct pci_device_id *const i915_device_ids = pciidlist;
887 const size_t i915_n_device_ids = __arraycount(pciidlist);
888 #endif
889
890 static enum intel_dram_type
891 skl_get_dram_type(struct drm_i915_private *dev_priv)
892 {
893 u32 val;
894
895 val = I915_READ(SKL_MAD_INTER_CHANNEL_0_0_0_MCHBAR_MCMAIN);
896
897 switch (val & SKL_DRAM_DDR_TYPE_MASK) {
898 case SKL_DRAM_DDR_TYPE_DDR3:
899 return INTEL_DRAM_DDR3;
900 case SKL_DRAM_DDR_TYPE_DDR4:
901 return INTEL_DRAM_DDR4;
902 case SKL_DRAM_DDR_TYPE_LPDDR3:
903 return INTEL_DRAM_LPDDR3;
904 case SKL_DRAM_DDR_TYPE_LPDDR4:
905 return INTEL_DRAM_LPDDR4;
906 default:
907 MISSING_CASE(val);
908 return INTEL_DRAM_UNKNOWN;
909 }
910 }
911
912 static int
913 skl_get_dram_info(struct drm_i915_private *dev_priv)
914 {
915 struct dram_info *dram_info = &dev_priv->dram_info;
916 u32 mem_freq_khz, val;
917 int ret;
918
919 dram_info->type = skl_get_dram_type(dev_priv);
920 DRM_DEBUG_KMS("DRAM type: %s\n", intel_dram_type_str(dram_info->type));
921
922 ret = skl_dram_get_channels_info(dev_priv);
923 if (ret)
924 return ret;
925
926 val = I915_READ(SKL_MC_BIOS_DATA_0_0_0_MCHBAR_PCU);
927 mem_freq_khz = DIV_ROUND_UP((val & SKL_REQ_DATA_MASK) *
928 SKL_MEMORY_FREQ_MULTIPLIER_HZ, 1000);
929
930 dram_info->bandwidth_kbps = dram_info->num_channels *
931 mem_freq_khz * 8;
932
933 if (dram_info->bandwidth_kbps == 0) {
934 DRM_INFO("Couldn't get system memory bandwidth\n");
935 return -EINVAL;
936 }
937
938 dram_info->valid = true;
939 return 0;
940 }
941
942 /* Returns Gb per DRAM device */
943 static int bxt_get_dimm_size(u32 val)
944 {
945 switch (val & BXT_DRAM_SIZE_MASK) {
946 case BXT_DRAM_SIZE_4GBIT:
947 return 4;
948 case BXT_DRAM_SIZE_6GBIT:
949 return 6;
950 case BXT_DRAM_SIZE_8GBIT:
951 return 8;
952 case BXT_DRAM_SIZE_12GBIT:
953 return 12;
954 case BXT_DRAM_SIZE_16GBIT:
955 return 16;
956 default:
957 MISSING_CASE(val);
958 return 0;
959 }
960 }
961
962 static int bxt_get_dimm_width(u32 val)
963 {
964 if (!bxt_get_dimm_size(val))
965 return 0;
966
967 val = (val & BXT_DRAM_WIDTH_MASK) >> BXT_DRAM_WIDTH_SHIFT;
968
969 return 8 << val;
970 }
971
972 static int bxt_get_dimm_ranks(u32 val)
973 {
974 if (!bxt_get_dimm_size(val))
975 return 0;
976
977 switch (val & BXT_DRAM_RANK_MASK) {
978 case BXT_DRAM_RANK_SINGLE:
979 return 1;
980 case BXT_DRAM_RANK_DUAL:
981 return 2;
982 default:
983 MISSING_CASE(val);
984 return 0;
985 }
986 }
987
988 static enum intel_dram_type bxt_get_dimm_type(u32 val)
989 {
990 if (!bxt_get_dimm_size(val))
991 return INTEL_DRAM_UNKNOWN;
992
993 switch (val & BXT_DRAM_TYPE_MASK) {
994 case BXT_DRAM_TYPE_DDR3:
995 return INTEL_DRAM_DDR3;
996 case BXT_DRAM_TYPE_LPDDR3:
997 return INTEL_DRAM_LPDDR3;
998 case BXT_DRAM_TYPE_DDR4:
999 return INTEL_DRAM_DDR4;
1000 case BXT_DRAM_TYPE_LPDDR4:
1001 return INTEL_DRAM_LPDDR4;
1002 default:
1003 MISSING_CASE(val);
1004 return INTEL_DRAM_UNKNOWN;
1005 }
1006 }
1007
1008 static void bxt_get_dimm_info(struct dram_dimm_info *dimm,
1009 u32 val)
1010 {
1011 dimm->width = bxt_get_dimm_width(val);
1012 dimm->ranks = bxt_get_dimm_ranks(val);
1013
1014 /*
1015 * Size in register is Gb per DRAM device. Convert to total
1016 * GB to match the way we report this for non-LP platforms.
1017 */
1018 dimm->size = bxt_get_dimm_size(val) * intel_dimm_num_devices(dimm) / 8;
1019 }
1020
1021 static int
1022 bxt_get_dram_info(struct drm_i915_private *dev_priv)
1023 {
1024 struct dram_info *dram_info = &dev_priv->dram_info;
1025 u32 dram_channels;
1026 u32 mem_freq_khz, val;
1027 u8 num_active_channels;
1028 int i;
1029
1030 val = I915_READ(BXT_P_CR_MC_BIOS_REQ_0_0_0);
1031 mem_freq_khz = DIV_ROUND_UP((val & BXT_REQ_DATA_MASK) *
1032 BXT_MEMORY_FREQ_MULTIPLIER_HZ, 1000);
1033
1034 dram_channels = val & BXT_DRAM_CHANNEL_ACTIVE_MASK;
1035 num_active_channels = hweight32(dram_channels);
1036
1037 /* Each active bit represents 4-byte channel */
1038 dram_info->bandwidth_kbps = (mem_freq_khz * num_active_channels * 4);
1039
1040 if (dram_info->bandwidth_kbps == 0) {
1041 DRM_INFO("Couldn't get system memory bandwidth\n");
1042 return -EINVAL;
1043 }
1044
1045 /*
1046 * Now read each DUNIT8/9/10/11 to check the rank of each dimms.
1047 */
1048 for (i = BXT_D_CR_DRP0_DUNIT_START; i <= BXT_D_CR_DRP0_DUNIT_END; i++) {
1049 struct dram_dimm_info dimm;
1050 enum intel_dram_type type;
1051
1052 val = I915_READ(BXT_D_CR_DRP0_DUNIT(i));
1053 if (val == 0xFFFFFFFF)
1054 continue;
1055
1056 dram_info->num_channels++;
1057
1058 bxt_get_dimm_info(&dimm, val);
1059 type = bxt_get_dimm_type(val);
1060
1061 WARN_ON(type != INTEL_DRAM_UNKNOWN &&
1062 dram_info->type != INTEL_DRAM_UNKNOWN &&
1063 dram_info->type != type);
1064
1065 DRM_DEBUG_KMS("CH%u DIMM size: %u GB, width: X%u, ranks: %u, type: %s\n",
1066 i - BXT_D_CR_DRP0_DUNIT_START,
1067 dimm.size, dimm.width, dimm.ranks,
1068 intel_dram_type_str(type));
1069
1070 /*
1071 * If any of the channel is single rank channel,
1072 * worst case output will be same as if single rank
1073 * memory, so consider single rank memory.
1074 */
1075 if (dram_info->ranks == 0)
1076 dram_info->ranks = dimm.ranks;
1077 else if (dimm.ranks == 1)
1078 dram_info->ranks = 1;
1079
1080 if (type != INTEL_DRAM_UNKNOWN)
1081 dram_info->type = type;
1082 }
1083
1084 if (dram_info->type == INTEL_DRAM_UNKNOWN ||
1085 dram_info->ranks == 0) {
1086 DRM_INFO("couldn't get memory information\n");
1087 return -EINVAL;
1088 }
1089
1090 dram_info->valid = true;
1091 return 0;
1092 }
1093
1094 static void
1095 intel_get_dram_info(struct drm_i915_private *dev_priv)
1096 {
1097 struct dram_info *dram_info = &dev_priv->dram_info;
1098 int ret;
1099
1100 /*
1101 * Assume 16Gb DIMMs are present until proven otherwise.
1102 * This is only used for the level 0 watermark latency
1103 * w/a which does not apply to bxt/glk.
1104 */
1105 dram_info->is_16gb_dimm = !IS_GEN9_LP(dev_priv);
1106
1107 if (INTEL_GEN(dev_priv) < 9 || !HAS_DISPLAY(dev_priv))
1108 return;
1109
1110 if (IS_GEN9_LP(dev_priv))
1111 ret = bxt_get_dram_info(dev_priv);
1112 else
1113 ret = skl_get_dram_info(dev_priv);
1114 if (ret)
1115 return;
1116
1117 DRM_DEBUG_KMS("DRAM bandwidth: %u kBps, channels: %u\n",
1118 dram_info->bandwidth_kbps,
1119 dram_info->num_channels);
1120
1121 DRM_DEBUG_KMS("DRAM ranks: %u, 16Gb DIMMs: %s\n",
1122 dram_info->ranks, yesno(dram_info->is_16gb_dimm));
1123 }
1124
1125 static u32 gen9_edram_size_mb(struct drm_i915_private *dev_priv, u32 cap)
1126 {
1127 static const u8 ways[8] = { 4, 8, 12, 16, 16, 16, 16, 16 };
1128 static const u8 sets[4] = { 1, 1, 2, 2 };
1129
1130 return EDRAM_NUM_BANKS(cap) *
1131 ways[EDRAM_WAYS_IDX(cap)] *
1132 sets[EDRAM_SETS_IDX(cap)];
1133 }
1134
1135 static void edram_detect(struct drm_i915_private *dev_priv)
1136 {
1137 u32 edram_cap = 0;
1138
1139 if (!(IS_HASWELL(dev_priv) ||
1140 IS_BROADWELL(dev_priv) ||
1141 INTEL_GEN(dev_priv) >= 9))
1142 return;
1143
1144 edram_cap = __raw_uncore_read32(&dev_priv->uncore, HSW_EDRAM_CAP);
1145
1146 /* NB: We can't write IDICR yet because we don't have gt funcs set up */
1147
1148 if (!(edram_cap & EDRAM_ENABLED))
1149 return;
1150
1151 /*
1152 * The needed capability bits for size calculation are not there with
1153 * pre gen9 so return 128MB always.
1154 */
1155 if (INTEL_GEN(dev_priv) < 9)
1156 dev_priv->edram_size_mb = 128;
1157 else
1158 dev_priv->edram_size_mb =
1159 gen9_edram_size_mb(dev_priv, edram_cap);
1160
1161 dev_info(dev_priv->drm.dev,
1162 "Found %uMB of eDRAM\n", dev_priv->edram_size_mb);
1163 }
1164
1165 /**
1166 * i915_driver_hw_probe - setup state requiring device access
1167 * @dev_priv: device private
1168 *
1169 * Setup state that requires accessing the device, but doesn't require
1170 * exposing the driver via kernel internal or userspace interfaces.
1171 */
1172 static int i915_driver_hw_probe(struct drm_i915_private *dev_priv)
1173 {
1174 struct pci_dev *pdev = dev_priv->drm.pdev;
1175 int ret;
1176
1177 if (i915_inject_probe_failure(dev_priv))
1178 return -ENODEV;
1179
1180 intel_device_info_runtime_init(dev_priv);
1181
1182 if (HAS_PPGTT(dev_priv)) {
1183 if (intel_vgpu_active(dev_priv) &&
1184 !intel_vgpu_has_full_ppgtt(dev_priv)) {
1185 i915_report_error(dev_priv,
1186 "incompatible vGPU found, support for isolated ppGTT required\n");
1187 return -ENXIO;
1188 }
1189 }
1190
1191 if (HAS_EXECLISTS(dev_priv)) {
1192 /*
1193 * Older GVT emulation depends upon intercepting CSB mmio,
1194 * which we no longer use, preferring to use the HWSP cache
1195 * instead.
1196 */
1197 if (intel_vgpu_active(dev_priv) &&
1198 !intel_vgpu_has_hwsp_emulation(dev_priv)) {
1199 i915_report_error(dev_priv,
1200 "old vGPU host found, support for HWSP emulation required\n");
1201 return -ENXIO;
1202 }
1203 }
1204
1205 intel_sanitize_options(dev_priv);
1206
1207 /* needs to be done before ggtt probe */
1208 edram_detect(dev_priv);
1209
1210 i915_perf_init(dev_priv);
1211
1212 ret = i915_ggtt_probe_hw(dev_priv);
1213 if (ret)
1214 goto err_perf;
1215
1216 ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "inteldrmfb");
1217 if (ret)
1218 goto err_ggtt;
1219
1220 ret = i915_ggtt_init_hw(dev_priv);
1221 if (ret)
1222 goto err_ggtt;
1223
1224 ret = intel_memory_regions_hw_probe(dev_priv);
1225 if (ret)
1226 goto err_ggtt;
1227
1228 intel_gt_init_hw_early(&dev_priv->gt, &dev_priv->ggtt);
1229
1230 ret = i915_ggtt_enable_hw(dev_priv);
1231 if (ret) {
1232 DRM_ERROR("failed to enable GGTT\n");
1233 goto err_mem_regions;
1234 }
1235
1236 pci_set_master(pdev);
1237
1238 /*
1239 * We don't have a max segment size, so set it to the max so sg's
1240 * debugging layer doesn't complain
1241 */
1242 dma_set_max_seg_size(&pdev->dev, UINT_MAX);
1243
1244 /* overlay on gen2 is broken and can't address above 1G */
1245 if (IS_GEN(dev_priv, 2)) {
1246 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(30));
1247 if (ret) {
1248 DRM_ERROR("failed to set DMA mask\n");
1249
1250 goto err_mem_regions;
1251 }
1252 }
1253
1254 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1255 * using 32bit addressing, overwriting memory if HWS is located
1256 * above 4GB.
1257 *
1258 * The documentation also mentions an issue with undefined
1259 * behaviour if any general state is accessed within a page above 4GB,
1260 * which also needs to be handled carefully.
1261 */
1262 if (IS_I965G(dev_priv) || IS_I965GM(dev_priv)) {
1263 ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1264
1265 if (ret) {
1266 DRM_ERROR("failed to set DMA mask\n");
1267
1268 goto err_mem_regions;
1269 }
1270 }
1271
1272 pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY,
1273 PM_QOS_DEFAULT_VALUE);
1274
1275 intel_gt_init_workarounds(dev_priv);
1276
1277 /* On the 945G/GM, the chipset reports the MSI capability on the
1278 * integrated graphics even though the support isn't actually there
1279 * according to the published specs. It doesn't appear to function
1280 * correctly in testing on 945G.
1281 * This may be a side effect of MSI having been made available for PEG
1282 * and the registers being closely associated.
1283 *
1284 * According to chipset errata, on the 965GM, MSI interrupts may
1285 * be lost or delayed, and was defeatured. MSI interrupts seem to
1286 * get lost on g4x as well, and interrupt delivery seems to stay
1287 * properly dead afterwards. So we'll just disable them for all
1288 * pre-gen5 chipsets.
1289 *
1290 * dp aux and gmbus irq on gen4 seems to be able to generate legacy
1291 * interrupts even when in MSI mode. This results in spurious
1292 * interrupt warnings if the legacy irq no. is shared with another
1293 * device. The kernel then disables that interrupt source and so
1294 * prevents the other device from working properly.
1295 */
1296 if (INTEL_GEN(dev_priv) >= 5) {
1297 if (pci_enable_msi(pdev) < 0)
1298 DRM_DEBUG_DRIVER("can't enable MSI");
1299 }
1300
1301 ret = intel_gvt_init(dev_priv);
1302 if (ret)
1303 goto err_msi;
1304
1305 intel_opregion_setup(dev_priv);
1306 /*
1307 * Fill the dram structure to get the system raw bandwidth and
1308 * dram info. This will be used for memory latency calculation.
1309 */
1310 intel_get_dram_info(dev_priv);
1311
1312 intel_bw_init_hw(dev_priv);
1313
1314 return 0;
1315
1316 err_msi:
1317 if (pdev->msi_enabled)
1318 pci_disable_msi(pdev);
1319 pm_qos_remove_request(&dev_priv->pm_qos);
1320 err_mem_regions:
1321 intel_memory_regions_driver_release(dev_priv);
1322 err_ggtt:
1323 i915_ggtt_driver_release(dev_priv);
1324 err_perf:
1325 i915_perf_fini(dev_priv);
1326 return ret;
1327 }
1328
1329 /**
1330 * i915_driver_hw_remove - cleanup the setup done in i915_driver_hw_probe()
1331 * @dev_priv: device private
1332 */
1333 static void i915_driver_hw_remove(struct drm_i915_private *dev_priv)
1334 {
1335 struct pci_dev *pdev = dev_priv->drm.pdev;
1336
1337 i915_perf_fini(dev_priv);
1338
1339 if (pdev->msi_enabled)
1340 pci_disable_msi(pdev);
1341
1342 pm_qos_remove_request(&dev_priv->pm_qos);
1343 }
1344
1345 /**
1346 * i915_driver_register - register the driver with the rest of the system
1347 * @dev_priv: device private
1348 *
1349 * Perform any steps necessary to make the driver available via kernel
1350 * internal or userspace interfaces.
1351 */
1352 static void i915_driver_register(struct drm_i915_private *dev_priv)
1353 {
1354 struct drm_device *dev = &dev_priv->drm;
1355
1356 i915_gem_driver_register(dev_priv);
1357 i915_pmu_register(dev_priv);
1358
1359 /*
1360 * Notify a valid surface after modesetting,
1361 * when running inside a VM.
1362 */
1363 if (intel_vgpu_active(dev_priv))
1364 I915_WRITE(vgtif_reg(display_ready), VGT_DRV_DISPLAY_READY);
1365
1366 /* Reveal our presence to userspace */
1367 if (drm_dev_register(dev, 0) == 0) {
1368 i915_debugfs_register(dev_priv);
1369 i915_setup_sysfs(dev_priv);
1370
1371 /* Depends on sysfs having been initialized */
1372 i915_perf_register(dev_priv);
1373 } else
1374 DRM_ERROR("Failed to register driver for userspace access!\n");
1375
1376 if (HAS_DISPLAY(dev_priv) && INTEL_DISPLAY_ENABLED(dev_priv)) {
1377 /* Must be done after probing outputs */
1378 intel_opregion_register(dev_priv);
1379 acpi_video_register();
1380 }
1381
1382 intel_gt_driver_register(&dev_priv->gt);
1383
1384 intel_audio_init(dev_priv);
1385
1386 /*
1387 * Some ports require correctly set-up hpd registers for detection to
1388 * work properly (leading to ghost connected connector status), e.g. VGA
1389 * on gm45. Hence we can only set up the initial fbdev config after hpd
1390 * irqs are fully enabled. We do it last so that the async config
1391 * cannot run before the connectors are registered.
1392 */
1393 intel_fbdev_initial_config_async(dev);
1394
1395 /*
1396 * We need to coordinate the hotplugs with the asynchronous fbdev
1397 * configuration, for which we use the fbdev->async_cookie.
1398 */
1399 if (HAS_DISPLAY(dev_priv) && INTEL_DISPLAY_ENABLED(dev_priv))
1400 drm_kms_helper_poll_init(dev);
1401
1402 intel_power_domains_enable(dev_priv);
1403 intel_runtime_pm_enable(&dev_priv->runtime_pm);
1404 }
1405
1406 /**
1407 * i915_driver_unregister - cleanup the registration done in i915_driver_regiser()
1408 * @dev_priv: device private
1409 */
1410 static void i915_driver_unregister(struct drm_i915_private *dev_priv)
1411 {
1412 intel_runtime_pm_disable(&dev_priv->runtime_pm);
1413 intel_power_domains_disable(dev_priv);
1414
1415 intel_fbdev_unregister(dev_priv);
1416 intel_audio_deinit(dev_priv);
1417
1418 /*
1419 * After flushing the fbdev (incl. a late async config which will
1420 * have delayed queuing of a hotplug event), then flush the hotplug
1421 * events.
1422 */
1423 drm_kms_helper_poll_fini(&dev_priv->drm);
1424
1425 intel_gt_driver_unregister(&dev_priv->gt);
1426 acpi_video_unregister();
1427 intel_opregion_unregister(dev_priv);
1428
1429 i915_perf_unregister(dev_priv);
1430 i915_pmu_unregister(dev_priv);
1431
1432 i915_teardown_sysfs(dev_priv);
1433 drm_dev_unplug(&dev_priv->drm);
1434
1435 i915_gem_driver_unregister(dev_priv);
1436 }
1437
1438 static void i915_welcome_messages(struct drm_i915_private *dev_priv)
1439 {
1440 if (drm_debug_enabled(DRM_UT_DRIVER)) {
1441 struct drm_printer p = drm_debug_printer("i915 device info:");
1442
1443 drm_printf(&p, "pciid=0x%04x rev=0x%02x platform=%s (subplatform=0x%x) gen=%i\n",
1444 INTEL_DEVID(dev_priv),
1445 INTEL_REVID(dev_priv),
1446 intel_platform_name(INTEL_INFO(dev_priv)->platform),
1447 intel_subplatform(RUNTIME_INFO(dev_priv),
1448 INTEL_INFO(dev_priv)->platform),
1449 INTEL_GEN(dev_priv));
1450
1451 intel_device_info_print_static(INTEL_INFO(dev_priv), &p);
1452 intel_device_info_print_runtime(RUNTIME_INFO(dev_priv), &p);
1453 }
1454
1455 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG))
1456 DRM_INFO("DRM_I915_DEBUG enabled\n");
1457 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
1458 DRM_INFO("DRM_I915_DEBUG_GEM enabled\n");
1459 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM))
1460 DRM_INFO("DRM_I915_DEBUG_RUNTIME_PM enabled\n");
1461 }
1462
1463 static struct drm_i915_private *
1464 i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent)
1465 {
1466 const struct intel_device_info *match_info =
1467 (struct intel_device_info *)ent->driver_data;
1468 struct intel_device_info *device_info;
1469 struct drm_i915_private *i915;
1470 int err;
1471
1472 i915 = kzalloc(sizeof(*i915), GFP_KERNEL);
1473 if (!i915)
1474 return ERR_PTR(-ENOMEM);
1475
1476 err = drm_dev_init(&i915->drm, &driver, &pdev->dev);
1477 if (err) {
1478 kfree(i915);
1479 return ERR_PTR(err);
1480 }
1481
1482 i915->drm.dev_private = i915;
1483
1484 i915->drm.pdev = pdev;
1485 pci_set_drvdata(pdev, i915);
1486
1487 /* Setup the write-once "constant" device info */
1488 device_info = mkwrite_device_info(i915);
1489 memcpy(device_info, match_info, sizeof(*device_info));
1490 RUNTIME_INFO(i915)->device_id = pdev->device;
1491
1492 BUG_ON(device_info->gen > BITS_PER_TYPE(device_info->gen_mask));
1493
1494 return i915;
1495 }
1496
1497 static void i915_driver_destroy(struct drm_i915_private *i915)
1498 {
1499 struct pci_dev *pdev = i915->drm.pdev;
1500
1501 drm_dev_fini(&i915->drm);
1502 kfree(i915);
1503
1504 /* And make sure we never chase our dangling pointer from pci_dev */
1505 pci_set_drvdata(pdev, NULL);
1506 }
1507
1508 /**
1509 * i915_driver_probe - setup chip and create an initial config
1510 * @pdev: PCI device
1511 * @ent: matching PCI ID entry
1512 *
1513 * The driver probe routine has to do several things:
1514 * - drive output discovery via intel_modeset_init()
1515 * - initialize the memory manager
1516 * - allocate initial config memory
1517 * - setup the DRM framebuffer with the allocated memory
1518 */
1519 int i915_driver_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1520 {
1521 const struct intel_device_info *match_info =
1522 (struct intel_device_info *)ent->driver_data;
1523 struct drm_i915_private *dev_priv;
1524 int ret;
1525
1526 dev_priv = i915_driver_create(pdev, ent);
1527 if (IS_ERR(dev_priv))
1528 return PTR_ERR(dev_priv);
1529
1530 /* Disable nuclear pageflip by default on pre-ILK */
1531 if (!i915_modparams.nuclear_pageflip && match_info->gen < 5)
1532 dev_priv->drm.driver_features &= ~DRIVER_ATOMIC;
1533
1534 /*
1535 * Check if we support fake LMEM -- for now we only unleash this for
1536 * the live selftests(test-and-exit).
1537 */
1538 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1539 if (IS_ENABLED(CONFIG_DRM_I915_UNSTABLE_FAKE_LMEM)) {
1540 if (INTEL_GEN(dev_priv) >= 9 && i915_selftest.live < 0 &&
1541 i915_modparams.fake_lmem_start) {
1542 mkwrite_device_info(dev_priv)->memory_regions =
1543 REGION_SMEM | REGION_LMEM | REGION_STOLEN;
1544 mkwrite_device_info(dev_priv)->is_dgfx = true;
1545 GEM_BUG_ON(!HAS_LMEM(dev_priv));
1546 GEM_BUG_ON(!IS_DGFX(dev_priv));
1547 }
1548 }
1549 #endif
1550
1551 ret = pci_enable_device(pdev);
1552 if (ret)
1553 goto out_fini;
1554
1555 ret = i915_driver_early_probe(dev_priv);
1556 if (ret < 0)
1557 goto out_pci_disable;
1558
1559 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1560
1561 i915_detect_vgpu(dev_priv);
1562
1563 ret = i915_driver_mmio_probe(dev_priv);
1564 if (ret < 0)
1565 goto out_runtime_pm_put;
1566
1567 ret = i915_driver_hw_probe(dev_priv);
1568 if (ret < 0)
1569 goto out_cleanup_mmio;
1570
1571 ret = i915_driver_modeset_probe(dev_priv);
1572 if (ret < 0)
1573 goto out_cleanup_hw;
1574
1575 i915_driver_register(dev_priv);
1576
1577 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1578
1579 i915_welcome_messages(dev_priv);
1580
1581 return 0;
1582
1583 out_cleanup_hw:
1584 i915_driver_hw_remove(dev_priv);
1585 intel_memory_regions_driver_release(dev_priv);
1586 i915_ggtt_driver_release(dev_priv);
1587 out_cleanup_mmio:
1588 i915_driver_mmio_release(dev_priv);
1589 out_runtime_pm_put:
1590 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1591 i915_driver_late_release(dev_priv);
1592 out_pci_disable:
1593 pci_disable_device(pdev);
1594 out_fini:
1595 i915_probe_error(dev_priv, "Device initialization failed (%d)\n", ret);
1596 i915_driver_destroy(dev_priv);
1597 return ret;
1598 }
1599
1600 void i915_driver_remove(struct drm_i915_private *i915)
1601 {
1602 disable_rpm_wakeref_asserts(&i915->runtime_pm);
1603
1604 i915_driver_unregister(i915);
1605
1606 /*
1607 * After unregistering the device to prevent any new users, cancel
1608 * all in-flight requests so that we can quickly unbind the active
1609 * resources.
1610 */
1611 intel_gt_set_wedged(&i915->gt);
1612
1613 /* Flush any external code that still may be under the RCU lock */
1614 synchronize_rcu();
1615
1616 i915_gem_suspend(i915);
1617
1618 drm_atomic_helper_shutdown(&i915->drm);
1619
1620 intel_gvt_driver_remove(i915);
1621
1622 i915_driver_modeset_remove(i915);
1623
1624 i915_reset_error_state(i915);
1625 i915_gem_driver_remove(i915);
1626
1627 intel_power_domains_driver_remove(i915);
1628
1629 i915_driver_hw_remove(i915);
1630
1631 enable_rpm_wakeref_asserts(&i915->runtime_pm);
1632 }
1633
1634 static void i915_driver_release(struct drm_device *dev)
1635 {
1636 struct drm_i915_private *dev_priv = to_i915(dev);
1637 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
1638
1639 disable_rpm_wakeref_asserts(rpm);
1640
1641 i915_gem_driver_release(dev_priv);
1642
1643 intel_memory_regions_driver_release(dev_priv);
1644 i915_ggtt_driver_release(dev_priv);
1645
1646 i915_driver_mmio_release(dev_priv);
1647
1648 enable_rpm_wakeref_asserts(rpm);
1649 intel_runtime_pm_driver_release(rpm);
1650
1651 i915_driver_late_release(dev_priv);
1652 i915_driver_destroy(dev_priv);
1653 }
1654
1655 static int i915_driver_open(struct drm_device *dev, struct drm_file *file)
1656 {
1657 struct drm_i915_private *i915 = to_i915(dev);
1658 int ret;
1659
1660 ret = i915_gem_open(i915, file);
1661 if (ret)
1662 return ret;
1663
1664 return 0;
1665 }
1666
1667 /**
1668 * i915_driver_lastclose - clean up after all DRM clients have exited
1669 * @dev: DRM device
1670 *
1671 * Take care of cleaning up after all DRM clients have exited. In the
1672 * mode setting case, we want to restore the kernel's initial mode (just
1673 * in case the last client left us in a bad state).
1674 *
1675 * Additionally, in the non-mode setting case, we'll tear down the GTT
1676 * and DMA structures, since the kernel won't be using them, and clea
1677 * up any GEM state.
1678 */
1679 static void i915_driver_lastclose(struct drm_device *dev)
1680 {
1681 intel_fbdev_restore_mode(dev);
1682 vga_switcheroo_process_delayed_switch();
1683 }
1684
1685 static void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
1686 {
1687 struct drm_i915_file_private *file_priv = file->driver_priv;
1688
1689 i915_gem_context_close(file);
1690 i915_gem_release(dev, file);
1691
1692 kfree_rcu(file_priv, rcu);
1693
1694 /* Catch up with all the deferred frees from "this" client */
1695 i915_gem_flush_free_objects(to_i915(dev));
1696 }
1697
1698 static void intel_suspend_encoders(struct drm_i915_private *dev_priv)
1699 {
1700 struct drm_device *dev = &dev_priv->drm;
1701 struct intel_encoder *encoder;
1702
1703 drm_modeset_lock_all(dev);
1704 for_each_intel_encoder(dev, encoder)
1705 if (encoder->suspend)
1706 encoder->suspend(encoder);
1707 drm_modeset_unlock_all(dev);
1708 }
1709
1710 static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
1711 bool rpm_resume);
1712 static int vlv_suspend_complete(struct drm_i915_private *dev_priv);
1713
1714 static bool suspend_to_idle(struct drm_i915_private *dev_priv)
1715 {
1716 #if IS_ENABLED(CONFIG_ACPI_SLEEP)
1717 if (acpi_target_system_state() < ACPI_STATE_S3)
1718 return true;
1719 #endif
1720 return false;
1721 }
1722
1723 static int i915_drm_prepare(struct drm_device *dev)
1724 {
1725 struct drm_i915_private *i915 = to_i915(dev);
1726
1727 /*
1728 * NB intel_display_suspend() may issue new requests after we've
1729 * ostensibly marked the GPU as ready-to-sleep here. We need to
1730 * split out that work and pull it forward so that after point,
1731 * the GPU is not woken again.
1732 */
1733 i915_gem_suspend(i915);
1734
1735 return 0;
1736 }
1737
1738 int i915_drm_suspend(struct drm_device *dev)
1739 {
1740 struct drm_i915_private *dev_priv = to_i915(dev);
1741 struct pci_dev *pdev = dev_priv->drm.pdev;
1742 pci_power_t opregion_target_state;
1743
1744 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1745
1746 /* We do a lot of poking in a lot of registers, make sure they work
1747 * properly. */
1748 intel_power_domains_disable(dev_priv);
1749
1750 drm_kms_helper_poll_disable(dev);
1751
1752 #ifndef __NetBSD__ /* pmf handles this for us. */
1753 pci_save_state(pdev);
1754 #endif
1755
1756 intel_display_suspend(dev);
1757
1758 intel_dp_mst_suspend(dev_priv);
1759
1760 intel_runtime_pm_disable_interrupts(dev_priv);
1761 intel_hpd_cancel_work(dev_priv);
1762
1763 intel_suspend_encoders(dev_priv);
1764
1765 intel_suspend_hw(dev_priv);
1766
1767 i915_gem_suspend_gtt_mappings(dev_priv);
1768
1769 i915_save_state(dev_priv);
1770
1771 opregion_target_state = suspend_to_idle(dev_priv) ? PCI_D1 : PCI_D3cold;
1772 intel_opregion_suspend(dev_priv, opregion_target_state);
1773
1774 intel_fbdev_set_suspend(dev, FBINFO_STATE_SUSPENDED, true);
1775
1776 dev_priv->suspend_count++;
1777
1778 intel_csr_ucode_suspend(dev_priv);
1779
1780 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1781
1782 return 0;
1783 }
1784
1785 static enum i915_drm_suspend_mode
1786 get_suspend_mode(struct drm_i915_private *dev_priv, bool hibernate)
1787 {
1788 if (hibernate)
1789 return I915_DRM_SUSPEND_HIBERNATE;
1790
1791 if (suspend_to_idle(dev_priv))
1792 return I915_DRM_SUSPEND_IDLE;
1793
1794 return I915_DRM_SUSPEND_MEM;
1795 }
1796
1797 static int i915_drm_suspend_late(struct drm_device *dev, bool hibernation)
1798 {
1799 struct drm_i915_private *dev_priv = to_i915(dev);
1800 struct pci_dev *pdev = dev_priv->drm.pdev;
1801 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
1802 int ret = 0;
1803
1804 disable_rpm_wakeref_asserts(rpm);
1805
1806 i915_gem_suspend_late(dev_priv);
1807
1808 intel_uncore_suspend(&dev_priv->uncore);
1809
1810 intel_power_domains_suspend(dev_priv,
1811 get_suspend_mode(dev_priv, hibernation));
1812
1813 intel_display_power_suspend_late(dev_priv);
1814
1815 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
1816 ret = vlv_suspend_complete(dev_priv);
1817
1818 if (ret) {
1819 DRM_ERROR("Suspend complete failed: %d\n", ret);
1820 intel_power_domains_resume(dev_priv);
1821
1822 goto out;
1823 }
1824
1825 #ifndef __NetBSD__ /* pmf handles this for us. */
1826 pci_disable_device(pdev);
1827 /*
1828 * During hibernation on some platforms the BIOS may try to access
1829 * the device even though it's already in D3 and hang the machine. So
1830 * leave the device in D0 on those platforms and hope the BIOS will
1831 * power down the device properly. The issue was seen on multiple old
1832 * GENs with different BIOS vendors, so having an explicit blacklist
1833 * is inpractical; apply the workaround on everything pre GEN6. The
1834 * platforms where the issue was seen:
1835 * Lenovo Thinkpad X301, X61s, X60, T60, X41
1836 * Fujitsu FSC S7110
1837 * Acer Aspire 1830T
1838 */
1839 if (!(hibernation && INTEL_GEN(dev_priv) < 6))
1840 pci_set_power_state(pdev, PCI_D3hot);
1841 #endif
1842
1843 out:
1844 enable_rpm_wakeref_asserts(rpm);
1845 if (!dev_priv->uncore.user_forcewake_count)
1846 intel_runtime_pm_driver_release(rpm);
1847
1848 return ret;
1849 }
1850
1851 int i915_suspend_switcheroo(struct drm_i915_private *i915, pm_message_t state)
1852 {
1853 int error;
1854
1855 if (WARN_ON_ONCE(state.event != PM_EVENT_SUSPEND &&
1856 state.event != PM_EVENT_FREEZE))
1857 return -EINVAL;
1858
1859 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
1860 return 0;
1861
1862 error = i915_drm_suspend(&i915->drm);
1863 if (error)
1864 return error;
1865
1866 return i915_drm_suspend_late(&i915->drm, false);
1867 }
1868
1869 int i915_drm_resume(struct drm_device *dev)
1870 {
1871 struct drm_i915_private *dev_priv = to_i915(dev);
1872 int ret;
1873
1874 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1875
1876 sanitize_gpu(dev_priv);
1877
1878 ret = i915_ggtt_enable_hw(dev_priv);
1879 if (ret)
1880 DRM_ERROR("failed to re-enable GGTT\n");
1881
1882 i915_gem_restore_gtt_mappings(dev_priv);
1883 i915_gem_restore_fences(&dev_priv->ggtt);
1884
1885 intel_csr_ucode_resume(dev_priv);
1886
1887 i915_restore_state(dev_priv);
1888 intel_pps_unlock_regs_wa(dev_priv);
1889
1890 intel_init_pch_refclk(dev_priv);
1891
1892 /*
1893 * Interrupts have to be enabled before any batches are run. If not the
1894 * GPU will hang. i915_gem_init_hw() will initiate batches to
1895 * update/restore the context.
1896 *
1897 * drm_mode_config_reset() needs AUX interrupts.
1898 *
1899 * Modeset enabling in intel_modeset_init_hw() also needs working
1900 * interrupts.
1901 */
1902 intel_runtime_pm_enable_interrupts(dev_priv);
1903
1904 drm_mode_config_reset(dev);
1905
1906 i915_gem_resume(dev_priv);
1907
1908 intel_modeset_init_hw(dev_priv);
1909 intel_init_clock_gating(dev_priv);
1910
1911 spin_lock_irq(&dev_priv->irq_lock);
1912 if (dev_priv->display.hpd_irq_setup)
1913 dev_priv->display.hpd_irq_setup(dev_priv);
1914 spin_unlock_irq(&dev_priv->irq_lock);
1915
1916 intel_dp_mst_resume(dev_priv);
1917
1918 intel_display_resume(dev);
1919
1920 drm_kms_helper_poll_enable(dev);
1921
1922 /*
1923 * ... but also need to make sure that hotplug processing
1924 * doesn't cause havoc. Like in the driver load code we don't
1925 * bother with the tiny race here where we might lose hotplug
1926 * notifications.
1927 * */
1928 intel_hpd_init(dev_priv);
1929
1930 intel_opregion_resume(dev_priv);
1931
1932 intel_fbdev_set_suspend(dev, FBINFO_STATE_RUNNING, false);
1933
1934 intel_power_domains_enable(dev_priv);
1935
1936 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1937
1938 return 0;
1939 }
1940
1941 int i915_drm_resume_early(struct drm_device *dev)
1942 {
1943 struct drm_i915_private *dev_priv = to_i915(dev);
1944 struct pci_dev *pdev = dev_priv->drm.pdev;
1945 int ret;
1946
1947 #ifndef __NetBSD__ /* pmf handles this for us. */
1948 /*
1949 * We have a resume ordering issue with the snd-hda driver also
1950 * requiring our device to be power up. Due to the lack of a
1951 * parent/child relationship we currently solve this with an early
1952 * resume hook.
1953 *
1954 * FIXME: This should be solved with a special hdmi sink device or
1955 * similar so that power domains can be employed.
1956 */
1957
1958 /*
1959 * Note that we need to set the power state explicitly, since we
1960 * powered off the device during freeze and the PCI core won't power
1961 * it back up for us during thaw. Powering off the device during
1962 * freeze is not a hard requirement though, and during the
1963 * suspend/resume phases the PCI core makes sure we get here with the
1964 * device powered on. So in case we change our freeze logic and keep
1965 * the device powered we can also remove the following set power state
1966 * call.
1967 */
1968 ret = pci_set_power_state(pdev, PCI_D0);
1969 if (ret) {
1970 DRM_ERROR("failed to set PCI D0 power state (%d)\n", ret);
1971 return ret;
1972 }
1973
1974 /*
1975 * Note that pci_enable_device() first enables any parent bridge
1976 * device and only then sets the power state for this device. The
1977 * bridge enabling is a nop though, since bridge devices are resumed
1978 * first. The order of enabling power and enabling the device is
1979 * imposed by the PCI core as described above, so here we preserve the
1980 * same order for the freeze/thaw phases.
1981 *
1982 * TODO: eventually we should remove pci_disable_device() /
1983 * pci_enable_enable_device() from suspend/resume. Due to how they
1984 * depend on the device enable refcount we can't anyway depend on them
1985 * disabling/enabling the device.
1986 */
1987 if (pci_enable_device(pdev))
1988 return -EIO;
1989 #endif
1990
1991 /* XXX pmf probably handles this for us too. */
1992 pci_set_master(dev->pdev);
1993
1994 disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1995
1996 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
1997 ret = vlv_resume_prepare(dev_priv, false);
1998 if (ret)
1999 DRM_ERROR("Resume prepare failed: %d, continuing anyway\n",
2000 ret);
2001
2002 intel_uncore_resume_early(&dev_priv->uncore);
2003
2004 intel_gt_check_and_clear_faults(&dev_priv->gt);
2005
2006 intel_display_power_resume_early(dev_priv);
2007
2008 intel_power_domains_resume(dev_priv);
2009
2010 enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2011
2012 i915_rc6_ctx_wa_resume(dev_priv);
2013
2014 return ret;
2015 }
2016
2017 int i915_resume_switcheroo(struct drm_i915_private *i915)
2018 {
2019 int ret;
2020
2021 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
2022 return 0;
2023
2024 ret = i915_drm_resume_early(&i915->drm);
2025 if (ret)
2026 return ret;
2027
2028 return i915_drm_resume(&i915->drm);
2029 }
2030
2031 static int i915_pm_prepare(struct device *kdev)
2032 {
2033 struct drm_i915_private *i915 = kdev_to_i915(kdev);
2034
2035 if (!i915) {
2036 dev_err(kdev, "DRM not initialized, aborting suspend.\n");
2037 return -ENODEV;
2038 }
2039
2040 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
2041 return 0;
2042
2043 return i915_drm_prepare(&i915->drm);
2044 }
2045 #endif
2046
2047 #ifndef __NetBSD__
2048 static int i915_pm_suspend(struct device *kdev)
2049 {
2050 struct drm_i915_private *i915 = kdev_to_i915(kdev);
2051
2052 if (!i915) {
2053 dev_err(kdev, "DRM not initialized, aborting suspend.\n");
2054 return -ENODEV;
2055 }
2056
2057 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
2058 return 0;
2059
2060 return i915_drm_suspend(&i915->drm);
2061 }
2062
2063 static int i915_pm_suspend_late(struct device *kdev)
2064 {
2065 struct drm_i915_private *i915 = kdev_to_i915(kdev);
2066
2067 /*
2068 * We have a suspend ordering issue with the snd-hda driver also
2069 * requiring our device to be power up. Due to the lack of a
2070 * parent/child relationship we currently solve this with an late
2071 * suspend hook.
2072 *
2073 * FIXME: This should be solved with a special hdmi sink device or
2074 * similar so that power domains can be employed.
2075 */
2076 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
2077 return 0;
2078
2079 return i915_drm_suspend_late(&i915->drm, false);
2080 }
2081
2082 static int i915_pm_poweroff_late(struct device *kdev)
2083 {
2084 struct drm_i915_private *i915 = kdev_to_i915(kdev);
2085
2086 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
2087 return 0;
2088
2089 return i915_drm_suspend_late(&i915->drm, true);
2090 }
2091
2092 static int i915_pm_resume_early(struct device *kdev)
2093 {
2094 struct drm_i915_private *i915 = kdev_to_i915(kdev);
2095
2096 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
2097 return 0;
2098
2099 return i915_drm_resume_early(&i915->drm);
2100 }
2101
2102 static int i915_pm_resume(struct device *kdev)
2103 {
2104 struct drm_i915_private *i915 = kdev_to_i915(kdev);
2105
2106 if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
2107 return 0;
2108
2109 return i915_drm_resume(&i915->drm);
2110 }
2111 #endif
2112
2113 /* freeze: before creating the hibernation_image */
2114 static int i915_pm_freeze(struct device *kdev)
2115 {
2116 struct drm_i915_private *i915 = kdev_to_i915(kdev);
2117 int ret;
2118
2119 if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) {
2120 ret = i915_drm_suspend(&i915->drm);
2121 if (ret)
2122 return ret;
2123 }
2124
2125 ret = i915_gem_freeze(i915);
2126 if (ret)
2127 return ret;
2128
2129 return 0;
2130 }
2131
2132 static int i915_pm_freeze_late(struct device *kdev)
2133 {
2134 struct drm_i915_private *i915 = kdev_to_i915(kdev);
2135 int ret;
2136
2137 if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) {
2138 ret = i915_drm_suspend_late(&i915->drm, true);
2139 if (ret)
2140 return ret;
2141 }
2142
2143 ret = i915_gem_freeze_late(i915);
2144 if (ret)
2145 return ret;
2146
2147 return 0;
2148 }
2149
2150 /* thaw: called after creating the hibernation image, but before turning off. */
2151 static int i915_pm_thaw_early(struct device *kdev)
2152 {
2153 return i915_pm_resume_early(kdev);
2154 }
2155
2156 static int i915_pm_thaw(struct device *kdev)
2157 {
2158 return i915_pm_resume(kdev);
2159 }
2160
2161 /* restore: called after loading the hibernation image. */
2162 static int i915_pm_restore_early(struct device *kdev)
2163 {
2164 return i915_pm_resume_early(kdev);
2165 }
2166
2167 static int i915_pm_restore(struct device *kdev)
2168 {
2169 return i915_pm_resume(kdev);
2170 }
2171
2172 /*
2173 * Save all Gunit registers that may be lost after a D3 and a subsequent
2174 * S0i[R123] transition. The list of registers needing a save/restore is
2175 * defined in the VLV2_S0IXRegs document. This documents marks all Gunit
2176 * registers in the following way:
2177 * - Driver: saved/restored by the driver
2178 * - Punit : saved/restored by the Punit firmware
2179 * - No, w/o marking: no need to save/restore, since the register is R/O or
2180 * used internally by the HW in a way that doesn't depend
2181 * keeping the content across a suspend/resume.
2182 * - Debug : used for debugging
2183 *
2184 * We save/restore all registers marked with 'Driver', with the following
2185 * exceptions:
2186 * - Registers out of use, including also registers marked with 'Debug'.
2187 * These have no effect on the driver's operation, so we don't save/restore
2188 * them to reduce the overhead.
2189 * - Registers that are fully setup by an initialization function called from
2190 * the resume path. For example many clock gating and RPS/RC6 registers.
2191 * - Registers that provide the right functionality with their reset defaults.
2192 *
2193 * TODO: Except for registers that based on the above 3 criteria can be safely
2194 * ignored, we save/restore all others, practically treating the HW context as
2195 * a black-box for the driver. Further investigation is needed to reduce the
2196 * saved/restored registers even further, by following the same 3 criteria.
2197 */
2198 static void vlv_save_gunit_s0ix_state(struct drm_i915_private *dev_priv)
2199 {
2200 struct vlv_s0ix_state *s = dev_priv->vlv_s0ix_state;
2201 int i;
2202
2203 if (!s)
2204 return;
2205
2206 /* GAM 0x4000-0x4770 */
2207 s->wr_watermark = I915_READ(GEN7_WR_WATERMARK);
2208 s->gfx_prio_ctrl = I915_READ(GEN7_GFX_PRIO_CTRL);
2209 s->arb_mode = I915_READ(ARB_MODE);
2210 s->gfx_pend_tlb0 = I915_READ(GEN7_GFX_PEND_TLB0);
2211 s->gfx_pend_tlb1 = I915_READ(GEN7_GFX_PEND_TLB1);
2212
2213 for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
2214 s->lra_limits[i] = I915_READ(GEN7_LRA_LIMITS(i));
2215
2216 s->media_max_req_count = I915_READ(GEN7_MEDIA_MAX_REQ_COUNT);
2217 s->gfx_max_req_count = I915_READ(GEN7_GFX_MAX_REQ_COUNT);
2218
2219 s->render_hwsp = I915_READ(RENDER_HWS_PGA_GEN7);
2220 s->ecochk = I915_READ(GAM_ECOCHK);
2221 s->bsd_hwsp = I915_READ(BSD_HWS_PGA_GEN7);
2222 s->blt_hwsp = I915_READ(BLT_HWS_PGA_GEN7);
2223
2224 s->tlb_rd_addr = I915_READ(GEN7_TLB_RD_ADDR);
2225
2226 /* MBC 0x9024-0x91D0, 0x8500 */
2227 s->g3dctl = I915_READ(VLV_G3DCTL);
2228 s->gsckgctl = I915_READ(VLV_GSCKGCTL);
2229 s->mbctl = I915_READ(GEN6_MBCTL);
2230
2231 /* GCP 0x9400-0x9424, 0x8100-0x810C */
2232 s->ucgctl1 = I915_READ(GEN6_UCGCTL1);
2233 s->ucgctl3 = I915_READ(GEN6_UCGCTL3);
2234 s->rcgctl1 = I915_READ(GEN6_RCGCTL1);
2235 s->rcgctl2 = I915_READ(GEN6_RCGCTL2);
2236 s->rstctl = I915_READ(GEN6_RSTCTL);
2237 s->misccpctl = I915_READ(GEN7_MISCCPCTL);
2238
2239 /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
2240 s->gfxpause = I915_READ(GEN6_GFXPAUSE);
2241 s->rpdeuhwtc = I915_READ(GEN6_RPDEUHWTC);
2242 s->rpdeuc = I915_READ(GEN6_RPDEUC);
2243 s->ecobus = I915_READ(ECOBUS);
2244 s->pwrdwnupctl = I915_READ(VLV_PWRDWNUPCTL);
2245 s->rp_down_timeout = I915_READ(GEN6_RP_DOWN_TIMEOUT);
2246 s->rp_deucsw = I915_READ(GEN6_RPDEUCSW);
2247 s->rcubmabdtmr = I915_READ(GEN6_RCUBMABDTMR);
2248 s->rcedata = I915_READ(VLV_RCEDATA);
2249 s->spare2gh = I915_READ(VLV_SPAREG2H);
2250
2251 /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
2252 s->gt_imr = I915_READ(GTIMR);
2253 s->gt_ier = I915_READ(GTIER);
2254 s->pm_imr = I915_READ(GEN6_PMIMR);
2255 s->pm_ier = I915_READ(GEN6_PMIER);
2256
2257 for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
2258 s->gt_scratch[i] = I915_READ(GEN7_GT_SCRATCH(i));
2259
2260 /* GT SA CZ domain, 0x100000-0x138124 */
2261 s->tilectl = I915_READ(TILECTL);
2262 s->gt_fifoctl = I915_READ(GTFIFOCTL);
2263 s->gtlc_wake_ctrl = I915_READ(VLV_GTLC_WAKE_CTRL);
2264 s->gtlc_survive = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2265 s->pmwgicz = I915_READ(VLV_PMWGICZ);
2266
2267 /* Gunit-Display CZ domain, 0x182028-0x1821CF */
2268 s->gu_ctl0 = I915_READ(VLV_GU_CTL0);
2269 s->gu_ctl1 = I915_READ(VLV_GU_CTL1);
2270 s->pcbr = I915_READ(VLV_PCBR);
2271 s->clock_gate_dis2 = I915_READ(VLV_GUNIT_CLOCK_GATE2);
2272
2273 /*
2274 * Not saving any of:
2275 * DFT, 0x9800-0x9EC0
2276 * SARB, 0xB000-0xB1FC
2277 * GAC, 0x5208-0x524C, 0x14000-0x14C000
2278 * PCI CFG
2279 */
2280 }
2281
2282 static void vlv_restore_gunit_s0ix_state(struct drm_i915_private *dev_priv)
2283 {
2284 struct vlv_s0ix_state *s = dev_priv->vlv_s0ix_state;
2285 u32 val;
2286 int i;
2287
2288 if (!s)
2289 return;
2290
2291 /* GAM 0x4000-0x4770 */
2292 I915_WRITE(GEN7_WR_WATERMARK, s->wr_watermark);
2293 I915_WRITE(GEN7_GFX_PRIO_CTRL, s->gfx_prio_ctrl);
2294 I915_WRITE(ARB_MODE, s->arb_mode | (0xffff << 16));
2295 I915_WRITE(GEN7_GFX_PEND_TLB0, s->gfx_pend_tlb0);
2296 I915_WRITE(GEN7_GFX_PEND_TLB1, s->gfx_pend_tlb1);
2297
2298 for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
2299 I915_WRITE(GEN7_LRA_LIMITS(i), s->lra_limits[i]);
2300
2301 I915_WRITE(GEN7_MEDIA_MAX_REQ_COUNT, s->media_max_req_count);
2302 I915_WRITE(GEN7_GFX_MAX_REQ_COUNT, s->gfx_max_req_count);
2303
2304 I915_WRITE(RENDER_HWS_PGA_GEN7, s->render_hwsp);
2305 I915_WRITE(GAM_ECOCHK, s->ecochk);
2306 I915_WRITE(BSD_HWS_PGA_GEN7, s->bsd_hwsp);
2307 I915_WRITE(BLT_HWS_PGA_GEN7, s->blt_hwsp);
2308
2309 I915_WRITE(GEN7_TLB_RD_ADDR, s->tlb_rd_addr);
2310
2311 /* MBC 0x9024-0x91D0, 0x8500 */
2312 I915_WRITE(VLV_G3DCTL, s->g3dctl);
2313 I915_WRITE(VLV_GSCKGCTL, s->gsckgctl);
2314 I915_WRITE(GEN6_MBCTL, s->mbctl);
2315
2316 /* GCP 0x9400-0x9424, 0x8100-0x810C */
2317 I915_WRITE(GEN6_UCGCTL1, s->ucgctl1);
2318 I915_WRITE(GEN6_UCGCTL3, s->ucgctl3);
2319 I915_WRITE(GEN6_RCGCTL1, s->rcgctl1);
2320 I915_WRITE(GEN6_RCGCTL2, s->rcgctl2);
2321 I915_WRITE(GEN6_RSTCTL, s->rstctl);
2322 I915_WRITE(GEN7_MISCCPCTL, s->misccpctl);
2323
2324 /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
2325 I915_WRITE(GEN6_GFXPAUSE, s->gfxpause);
2326 I915_WRITE(GEN6_RPDEUHWTC, s->rpdeuhwtc);
2327 I915_WRITE(GEN6_RPDEUC, s->rpdeuc);
2328 I915_WRITE(ECOBUS, s->ecobus);
2329 I915_WRITE(VLV_PWRDWNUPCTL, s->pwrdwnupctl);
2330 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,s->rp_down_timeout);
2331 I915_WRITE(GEN6_RPDEUCSW, s->rp_deucsw);
2332 I915_WRITE(GEN6_RCUBMABDTMR, s->rcubmabdtmr);
2333 I915_WRITE(VLV_RCEDATA, s->rcedata);
2334 I915_WRITE(VLV_SPAREG2H, s->spare2gh);
2335
2336 /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
2337 I915_WRITE(GTIMR, s->gt_imr);
2338 I915_WRITE(GTIER, s->gt_ier);
2339 I915_WRITE(GEN6_PMIMR, s->pm_imr);
2340 I915_WRITE(GEN6_PMIER, s->pm_ier);
2341
2342 for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
2343 I915_WRITE(GEN7_GT_SCRATCH(i), s->gt_scratch[i]);
2344
2345 /* GT SA CZ domain, 0x100000-0x138124 */
2346 I915_WRITE(TILECTL, s->tilectl);
2347 I915_WRITE(GTFIFOCTL, s->gt_fifoctl);
2348 /*
2349 * Preserve the GT allow wake and GFX force clock bit, they are not
2350 * be restored, as they are used to control the s0ix suspend/resume
2351 * sequence by the caller.
2352 */
2353 val = I915_READ(VLV_GTLC_WAKE_CTRL);
2354 val &= VLV_GTLC_ALLOWWAKEREQ;
2355 val |= s->gtlc_wake_ctrl & ~VLV_GTLC_ALLOWWAKEREQ;
2356 I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
2357
2358 val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2359 val &= VLV_GFX_CLK_FORCE_ON_BIT;
2360 val |= s->gtlc_survive & ~VLV_GFX_CLK_FORCE_ON_BIT;
2361 I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
2362
2363 I915_WRITE(VLV_PMWGICZ, s->pmwgicz);
2364
2365 /* Gunit-Display CZ domain, 0x182028-0x1821CF */
2366 I915_WRITE(VLV_GU_CTL0, s->gu_ctl0);
2367 I915_WRITE(VLV_GU_CTL1, s->gu_ctl1);
2368 I915_WRITE(VLV_PCBR, s->pcbr);
2369 I915_WRITE(VLV_GUNIT_CLOCK_GATE2, s->clock_gate_dis2);
2370 }
2371
2372 static int vlv_wait_for_pw_status(struct drm_i915_private *i915,
2373 u32 mask, u32 val)
2374 {
2375 i915_reg_t reg = VLV_GTLC_PW_STATUS;
2376 u32 reg_value;
2377 int ret;
2378
2379 /* The HW does not like us polling for PW_STATUS frequently, so
2380 * use the sleeping loop rather than risk the busy spin within
2381 * intel_wait_for_register().
2382 *
2383 * Transitioning between RC6 states should be at most 2ms (see
2384 * valleyview_enable_rps) so use a 3ms timeout.
2385 */
2386 ret = wait_for(((reg_value =
2387 intel_uncore_read_notrace(&i915->uncore, reg)) & mask)
2388 == val, 3);
2389
2390 /* just trace the final value */
2391 trace_i915_reg_rw(false, reg, reg_value, sizeof(reg_value), true);
2392
2393 return ret;
2394 }
2395
2396 int vlv_force_gfx_clock(struct drm_i915_private *dev_priv, bool force_on)
2397 {
2398 u32 val;
2399 int err;
2400
2401 val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
2402 val &= ~VLV_GFX_CLK_FORCE_ON_BIT;
2403 if (force_on)
2404 val |= VLV_GFX_CLK_FORCE_ON_BIT;
2405 I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
2406
2407 if (!force_on)
2408 return 0;
2409
2410 err = intel_wait_for_register(&dev_priv->uncore,
2411 VLV_GTLC_SURVIVABILITY_REG,
2412 VLV_GFX_CLK_STATUS_BIT,
2413 VLV_GFX_CLK_STATUS_BIT,
2414 20);
2415 if (err)
2416 DRM_ERROR("timeout waiting for GFX clock force-on (%08x)\n",
2417 I915_READ(VLV_GTLC_SURVIVABILITY_REG));
2418
2419 return err;
2420 }
2421
2422 static int vlv_allow_gt_wake(struct drm_i915_private *dev_priv, bool allow)
2423 {
2424 u32 mask;
2425 u32 val;
2426 int err;
2427
2428 val = I915_READ(VLV_GTLC_WAKE_CTRL);
2429 val &= ~VLV_GTLC_ALLOWWAKEREQ;
2430 if (allow)
2431 val |= VLV_GTLC_ALLOWWAKEREQ;
2432 I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
2433 POSTING_READ(VLV_GTLC_WAKE_CTRL);
2434
2435 mask = VLV_GTLC_ALLOWWAKEACK;
2436 val = allow ? mask : 0;
2437
2438 err = vlv_wait_for_pw_status(dev_priv, mask, val);
2439 if (err)
2440 DRM_ERROR("timeout disabling GT waking\n");
2441
2442 return err;
2443 }
2444
2445 static void vlv_wait_for_gt_wells(struct drm_i915_private *dev_priv,
2446 bool wait_for_on)
2447 {
2448 u32 mask;
2449 u32 val;
2450
2451 mask = VLV_GTLC_PW_MEDIA_STATUS_MASK | VLV_GTLC_PW_RENDER_STATUS_MASK;
2452 val = wait_for_on ? mask : 0;
2453
2454 /*
2455 * RC6 transitioning can be delayed up to 2 msec (see
2456 * valleyview_enable_rps), use 3 msec for safety.
2457 *
2458 * This can fail to turn off the rc6 if the GPU is stuck after a failed
2459 * reset and we are trying to force the machine to sleep.
2460 */
2461 if (vlv_wait_for_pw_status(dev_priv, mask, val))
2462 DRM_DEBUG_DRIVER("timeout waiting for GT wells to go %s\n",
2463 onoff(wait_for_on));
2464 }
2465
2466 static void vlv_check_no_gt_access(struct drm_i915_private *dev_priv)
2467 {
2468 if (!(I915_READ(VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEERR))
2469 return;
2470
2471 DRM_DEBUG_DRIVER("GT register access while GT waking disabled\n");
2472 I915_WRITE(VLV_GTLC_PW_STATUS, VLV_GTLC_ALLOWWAKEERR);
2473 }
2474
2475 static int vlv_suspend_complete(struct drm_i915_private *dev_priv)
2476 {
2477 u32 mask;
2478 int err;
2479
2480 /*
2481 * Bspec defines the following GT well on flags as debug only, so
2482 * don't treat them as hard failures.
2483 */
2484 vlv_wait_for_gt_wells(dev_priv, false);
2485
2486 mask = VLV_GTLC_RENDER_CTX_EXISTS | VLV_GTLC_MEDIA_CTX_EXISTS;
2487 WARN_ON((I915_READ(VLV_GTLC_WAKE_CTRL) & mask) != mask);
2488
2489 vlv_check_no_gt_access(dev_priv);
2490
2491 err = vlv_force_gfx_clock(dev_priv, true);
2492 if (err)
2493 goto err1;
2494
2495 err = vlv_allow_gt_wake(dev_priv, false);
2496 if (err)
2497 goto err2;
2498
2499 vlv_save_gunit_s0ix_state(dev_priv);
2500
2501 err = vlv_force_gfx_clock(dev_priv, false);
2502 if (err)
2503 goto err2;
2504
2505 return 0;
2506
2507 err2:
2508 /* For safety always re-enable waking and disable gfx clock forcing */
2509 vlv_allow_gt_wake(dev_priv, true);
2510 err1:
2511 vlv_force_gfx_clock(dev_priv, false);
2512
2513 return err;
2514 }
2515
2516 static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
2517 bool rpm_resume)
2518 {
2519 int err;
2520 int ret;
2521
2522 /*
2523 * If any of the steps fail just try to continue, that's the best we
2524 * can do at this point. Return the first error code (which will also
2525 * leave RPM permanently disabled).
2526 */
2527 ret = vlv_force_gfx_clock(dev_priv, true);
2528
2529 vlv_restore_gunit_s0ix_state(dev_priv);
2530
2531 err = vlv_allow_gt_wake(dev_priv, true);
2532 if (!ret)
2533 ret = err;
2534
2535 err = vlv_force_gfx_clock(dev_priv, false);
2536 if (!ret)
2537 ret = err;
2538
2539 vlv_check_no_gt_access(dev_priv);
2540
2541 if (rpm_resume)
2542 intel_init_clock_gating(dev_priv);
2543
2544 return ret;
2545 }
2546
2547 #ifndef __NetBSD__ /* XXX runtime pm */
2548 static int intel_runtime_suspend(struct device *kdev)
2549 {
2550 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
2551 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
2552 int ret = 0;
2553
2554 if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev_priv)))
2555 return -ENODEV;
2556
2557 DRM_DEBUG_KMS("Suspending device\n");
2558
2559 disable_rpm_wakeref_asserts(rpm);
2560
2561 /*
2562 * We are safe here against re-faults, since the fault handler takes
2563 * an RPM reference.
2564 */
2565 i915_gem_runtime_suspend(dev_priv);
2566
2567 intel_gt_runtime_suspend(&dev_priv->gt);
2568
2569 intel_runtime_pm_disable_interrupts(dev_priv);
2570
2571 intel_uncore_suspend(&dev_priv->uncore);
2572
2573 intel_display_power_suspend(dev_priv);
2574
2575 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
2576 ret = vlv_suspend_complete(dev_priv);
2577
2578 if (ret) {
2579 DRM_ERROR("Runtime suspend failed, disabling it (%d)\n", ret);
2580 intel_uncore_runtime_resume(&dev_priv->uncore);
2581
2582 intel_runtime_pm_enable_interrupts(dev_priv);
2583
2584 intel_gt_runtime_resume(&dev_priv->gt);
2585
2586 i915_gem_restore_fences(&dev_priv->ggtt);
2587
2588 enable_rpm_wakeref_asserts(rpm);
2589
2590 return ret;
2591 }
2592
2593 enable_rpm_wakeref_asserts(rpm);
2594 intel_runtime_pm_driver_release(rpm);
2595
2596 if (intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore))
2597 DRM_ERROR("Unclaimed access detected prior to suspending\n");
2598
2599 rpm->suspended = true;
2600
2601 /*
2602 * FIXME: We really should find a document that references the arguments
2603 * used below!
2604 */
2605 if (IS_BROADWELL(dev_priv)) {
2606 /*
2607 * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop
2608 * being detected, and the call we do at intel_runtime_resume()
2609 * won't be able to restore them. Since PCI_D3hot matches the
2610 * actual specification and appears to be working, use it.
2611 */
2612 intel_opregion_notify_adapter(dev_priv, PCI_D3hot);
2613 } else {
2614 /*
2615 * current versions of firmware which depend on this opregion
2616 * notification have repurposed the D1 definition to mean
2617 * "runtime suspended" vs. what you would normally expect (D3)
2618 * to distinguish it from notifications that might be sent via
2619 * the suspend path.
2620 */
2621 intel_opregion_notify_adapter(dev_priv, PCI_D1);
2622 }
2623
2624 assert_forcewakes_inactive(&dev_priv->uncore);
2625
2626 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
2627 intel_hpd_poll_init(dev_priv);
2628
2629 DRM_DEBUG_KMS("Device suspended\n");
2630 return 0;
2631 }
2632
2633 static int intel_runtime_resume(struct device *kdev)
2634 {
2635 struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
2636 struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
2637 int ret = 0;
2638
2639 if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev_priv)))
2640 return -ENODEV;
2641
2642 DRM_DEBUG_KMS("Resuming device\n");
2643
2644 WARN_ON_ONCE(atomic_read(&rpm->wakeref_count));
2645 disable_rpm_wakeref_asserts(rpm);
2646
2647 intel_opregion_notify_adapter(dev_priv, PCI_D0);
2648 rpm->suspended = false;
2649 if (intel_uncore_unclaimed_mmio(&dev_priv->uncore))
2650 DRM_DEBUG_DRIVER("Unclaimed access during suspend, bios?\n");
2651
2652 intel_display_power_resume(dev_priv);
2653
2654 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
2655 ret = vlv_resume_prepare(dev_priv, true);
2656
2657 intel_uncore_runtime_resume(&dev_priv->uncore);
2658
2659 intel_runtime_pm_enable_interrupts(dev_priv);
2660
2661 /*
2662 * No point of rolling back things in case of an error, as the best
2663 * we can do is to hope that things will still work (and disable RPM).
2664 */
2665 intel_gt_runtime_resume(&dev_priv->gt);
2666 i915_gem_restore_fences(&dev_priv->ggtt);
2667
2668 /*
2669 * On VLV/CHV display interrupts are part of the display
2670 * power well, so hpd is reinitialized from there. For
2671 * everyone else do it here.
2672 */
2673 if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
2674 intel_hpd_init(dev_priv);
2675
2676 intel_enable_ipc(dev_priv);
2677
2678 enable_rpm_wakeref_asserts(rpm);
2679
2680 if (ret)
2681 DRM_ERROR("Runtime resume failed, disabling it (%d)\n", ret);
2682 else
2683 DRM_DEBUG_KMS("Device resumed\n");
2684
2685 return ret;
2686 }
2687 #endif
2688
2689 #ifndef __NetBSD__
2690 const struct dev_pm_ops i915_pm_ops = {
2691 /*
2692 * S0ix (via system suspend) and S3 event handlers [PMSG_SUSPEND,
2693 * PMSG_RESUME]
2694 */
2695 .prepare = i915_pm_prepare,
2696 .suspend = i915_pm_suspend,
2697 .suspend_late = i915_pm_suspend_late,
2698 .resume_early = i915_pm_resume_early,
2699 .resume = i915_pm_resume,
2700
2701 /*
2702 * S4 event handlers
2703 * @freeze, @freeze_late : called (1) before creating the
2704 * hibernation image [PMSG_FREEZE] and
2705 * (2) after rebooting, before restoring
2706 * the image [PMSG_QUIESCE]
2707 * @thaw, @thaw_early : called (1) after creating the hibernation
2708 * image, before writing it [PMSG_THAW]
2709 * and (2) after failing to create or
2710 * restore the image [PMSG_RECOVER]
2711 * @poweroff, @poweroff_late: called after writing the hibernation
2712 * image, before rebooting [PMSG_HIBERNATE]
2713 * @restore, @restore_early : called after rebooting and restoring the
2714 * hibernation image [PMSG_RESTORE]
2715 */
2716 .freeze = i915_pm_freeze,
2717 .freeze_late = i915_pm_freeze_late,
2718 .thaw_early = i915_pm_thaw_early,
2719 .thaw = i915_pm_thaw,
2720 .poweroff = i915_pm_suspend,
2721 .poweroff_late = i915_pm_poweroff_late,
2722 .restore_early = i915_pm_restore_early,
2723 .restore = i915_pm_restore,
2724
2725 /* S0ix (via runtime suspend) event handlers */
2726 .runtime_suspend = intel_runtime_suspend,
2727 .runtime_resume = intel_runtime_resume,
2728 };
2729
2730 static const struct file_operations i915_driver_fops = {
2731 .owner = THIS_MODULE,
2732 .open = drm_open,
2733 .release = drm_release,
2734 .unlocked_ioctl = drm_ioctl,
2735 .mmap = i915_gem_mmap,
2736 .poll = drm_poll,
2737 .read = drm_read,
2738 .compat_ioctl = i915_compat_ioctl,
2739 .llseek = noop_llseek,
2740 };
2741 #endif /* defined(__NetBSD__) */
2742
2743 static int
2744 i915_gem_reject_pin_ioctl(struct drm_device *dev, void *data,
2745 struct drm_file *file)
2746 {
2747 return -ENODEV;
2748 }
2749
2750 static const struct drm_ioctl_desc i915_ioctls[] = {
2751 DRM_IOCTL_DEF_DRV(I915_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2752 DRM_IOCTL_DEF_DRV(I915_FLUSH, drm_noop, DRM_AUTH),
2753 DRM_IOCTL_DEF_DRV(I915_FLIP, drm_noop, DRM_AUTH),
2754 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, drm_noop, DRM_AUTH),
2755 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, drm_noop, DRM_AUTH),
2756 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, drm_noop, DRM_AUTH),
2757 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam_ioctl, DRM_RENDER_ALLOW),
2758 DRM_IOCTL_DEF_DRV(I915_SETPARAM, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2759 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
2760 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
2761 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2762 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, drm_noop, DRM_AUTH),
2763 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2764 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2765 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, drm_noop, DRM_AUTH),
2766 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, drm_noop, DRM_AUTH),
2767 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2768 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2769 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer_ioctl, DRM_AUTH),
2770 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2_WR, i915_gem_execbuffer2_ioctl, DRM_RENDER_ALLOW),
2771 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
2772 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
2773 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_RENDER_ALLOW),
2774 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_RENDER_ALLOW),
2775 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_RENDER_ALLOW),
2776 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_RENDER_ALLOW),
2777 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2778 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2779 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_RENDER_ALLOW),
2780 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW),
2781 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW),
2782 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW),
2783 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_OFFSET, i915_gem_mmap_offset_ioctl, DRM_RENDER_ALLOW),
2784 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW),
2785 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW),
2786 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling_ioctl, DRM_RENDER_ALLOW),
2787 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling_ioctl, DRM_RENDER_ALLOW),
2788 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_RENDER_ALLOW),
2789 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id_ioctl, 0),
2790 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_RENDER_ALLOW),
2791 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image_ioctl, DRM_MASTER),
2792 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs_ioctl, DRM_MASTER),
2793 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey_ioctl, DRM_MASTER),
2794 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, drm_noop, DRM_MASTER),
2795 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_RENDER_ALLOW),
2796 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE_EXT, i915_gem_context_create_ioctl, DRM_RENDER_ALLOW),
2797 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_RENDER_ALLOW),
2798 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_RENDER_ALLOW),
2799 DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_gem_context_reset_stats_ioctl, DRM_RENDER_ALLOW),
2800 DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
2801 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
2802 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
2803 DRM_IOCTL_DEF_DRV(I915_PERF_OPEN, i915_perf_open_ioctl, DRM_RENDER_ALLOW),
2804 DRM_IOCTL_DEF_DRV(I915_PERF_ADD_CONFIG, i915_perf_add_config_ioctl, DRM_RENDER_ALLOW),
2805 DRM_IOCTL_DEF_DRV(I915_PERF_REMOVE_CONFIG, i915_perf_remove_config_ioctl, DRM_RENDER_ALLOW),
2806 DRM_IOCTL_DEF_DRV(I915_QUERY, i915_query_ioctl, DRM_RENDER_ALLOW),
2807 DRM_IOCTL_DEF_DRV(I915_GEM_VM_CREATE, i915_gem_vm_create_ioctl, DRM_RENDER_ALLOW),
2808 DRM_IOCTL_DEF_DRV(I915_GEM_VM_DESTROY, i915_gem_vm_destroy_ioctl, DRM_RENDER_ALLOW),
2809 };
2810
2811 static struct drm_driver driver = {
2812 /* Don't use MTRRs here; the Xserver or userspace app should
2813 * deal with them for Intel hardware.
2814 */
2815 .driver_features =
2816 DRIVER_GEM |
2817 DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_SYNCOBJ,
2818 .release = i915_driver_release,
2819 .open = i915_driver_open,
2820 .lastclose = i915_driver_lastclose,
2821 .postclose = i915_driver_postclose,
2822
2823 .gem_close_object = i915_gem_close_object,
2824 .gem_free_object_unlocked = i915_gem_free_object,
2825 #ifdef __NetBSD__
2826 .request_irq = drm_pci_request_irq,
2827 .free_irq = drm_pci_free_irq,
2828
2829 /* XXX Not clear the `or legacy' part is important here. */
2830 .mmap_object = &drm_gem_mmap_object,
2831 .gem_uvm_ops = &i915_gem_uvm_ops,
2832 #endif
2833
2834
2835 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
2836 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
2837 .gem_prime_export = i915_gem_prime_export,
2838 .gem_prime_import = i915_gem_prime_import,
2839
2840 .get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos,
2841 .get_scanout_position = i915_get_crtc_scanoutpos,
2842
2843 .dumb_create = i915_gem_dumb_create,
2844 .dumb_map_offset = i915_gem_dumb_mmap_offset,
2845
2846 .ioctls = i915_ioctls,
2847 .num_ioctls = ARRAY_SIZE(i915_ioctls),
2848 #ifdef __NetBSD__
2849 .fops = NULL,
2850 #else
2851 .fops = &i915_driver_fops,
2852 #endif
2853 .name = DRIVER_NAME,
2854 .desc = DRIVER_DESC,
2855 .date = DRIVER_DATE,
2856 .major = DRIVER_MAJOR,
2857 .minor = DRIVER_MINOR,
2858 .patchlevel = DRIVER_PATCHLEVEL,
2859 };
2860
2861 #ifdef __NetBSD__
2862
2863 static const struct uvm_pagerops i915_gem_uvm_ops = {
2864 .pgo_reference = drm_gem_pager_reference,
2865 .pgo_detach = drm_gem_pager_detach,
2866 .pgo_fault = i915_gem_fault,
2867 };
2868
2869 #endif
2870