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