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