xref: /openbsd/sys/dev/pci/drm/i915/i915_driver.c (revision 4fb9ab68)
1 /* i915_drv.c -- i830,i845,i855,i865,i915 driver -*- linux-c -*-
2  */
3 /*
4  *
5  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
24  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  */
29 
30 #include <linux/acpi.h>
31 #include <linux/device.h>
32 #include <linux/module.h>
33 #include <linux/oom.h>
34 #include <linux/pci.h>
35 #include <linux/pm.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/slab.h>
38 #include <linux/string_helpers.h>
39 #include <linux/vga_switcheroo.h>
40 #include <linux/vt.h>
41 
42 #include <drm/drm_aperture.h>
43 #include <drm/drm_atomic_helper.h>
44 #include <drm/drm_ioctl.h>
45 #include <drm/drm_managed.h>
46 #include <drm/drm_probe_helper.h>
47 
48 #include "display/intel_acpi.h"
49 #include "display/intel_bw.h"
50 #include "display/intel_cdclk.h"
51 #include "display/intel_display_driver.h"
52 #include "display/intel_display_types.h"
53 #include "display/intel_dmc.h"
54 #include "display/intel_dp.h"
55 #include "display/intel_dpt.h"
56 #include "display/intel_fbdev.h"
57 #include "display/intel_hotplug.h"
58 #include "display/intel_overlay.h"
59 #include "display/intel_pch_refclk.h"
60 #include "display/intel_pipe_crc.h"
61 #include "display/intel_pps.h"
62 #include "display/intel_sprite.h"
63 #include "display/intel_vga.h"
64 #include "display/skl_watermark.h"
65 
66 #include "gem/i915_gem_context.h"
67 #include "gem/i915_gem_create.h"
68 #include "gem/i915_gem_dmabuf.h"
69 #include "gem/i915_gem_ioctls.h"
70 #include "gem/i915_gem_mman.h"
71 #include "gem/i915_gem_pm.h"
72 #include "gt/intel_gt.h"
73 #include "gt/intel_gt_pm.h"
74 #include "gt/intel_rc6.h"
75 
76 #include "pxp/intel_pxp.h"
77 #include "pxp/intel_pxp_debugfs.h"
78 #include "pxp/intel_pxp_pm.h"
79 
80 #include "soc/intel_dram.h"
81 #include "soc/intel_gmch.h"
82 
83 #include "i915_debugfs.h"
84 #include "i915_driver.h"
85 #include "i915_drm_client.h"
86 #include "i915_drv.h"
87 #include "i915_file_private.h"
88 #include "i915_getparam.h"
89 #include "i915_hwmon.h"
90 #include "i915_ioc32.h"
91 #include "i915_ioctl.h"
92 #include "i915_irq.h"
93 #include "i915_memcpy.h"
94 #include "i915_perf.h"
95 #include "i915_query.h"
96 #include "i915_suspend.h"
97 #include "i915_switcheroo.h"
98 #include "i915_sysfs.h"
99 #include "i915_utils.h"
100 #include "i915_vgpu.h"
101 #include "intel_clock_gating.h"
102 #include "intel_gvt.h"
103 #include "intel_memory_region.h"
104 #include "intel_pci_config.h"
105 #include "intel_pcode.h"
106 #include "intel_region_ttm.h"
107 #include "vlv_suspend.h"
108 
109 static const struct drm_driver i915_drm_driver;
110 
111 static int i915_workqueues_init(struct drm_i915_private *dev_priv)
112 {
113 	/*
114 	 * The i915 workqueue is primarily used for batched retirement of
115 	 * requests (and thus managing bo) once the task has been completed
116 	 * by the GPU. i915_retire_requests() is called directly when we
117 	 * need high-priority retirement, such as waiting for an explicit
118 	 * bo.
119 	 *
120 	 * It is also used for periodic low-priority events, such as
121 	 * idle-timers and recording error state.
122 	 *
123 	 * All tasks on the workqueue are expected to acquire the dev mutex
124 	 * so there is no point in running more than one instance of the
125 	 * workqueue at any time.  Use an ordered one.
126 	 */
127 	dev_priv->wq = alloc_ordered_workqueue("i915", 0);
128 	if (dev_priv->wq == NULL)
129 		goto out_err;
130 
131 	dev_priv->display.hotplug.dp_wq = alloc_ordered_workqueue("i915-dp", 0);
132 	if (dev_priv->display.hotplug.dp_wq == NULL)
133 		goto out_free_wq;
134 
135 	/*
136 	 * The unordered i915 workqueue should be used for all work
137 	 * scheduling that do not require running in order, which used
138 	 * to be scheduled on the system_wq before moving to a driver
139 	 * instance due deprecation of flush_scheduled_work().
140 	 */
141 	dev_priv->unordered_wq = alloc_workqueue("i915-unordered", 0, 0);
142 	if (dev_priv->unordered_wq == NULL)
143 		goto out_free_dp_wq;
144 
145 	return 0;
146 
147 out_free_dp_wq:
148 	destroy_workqueue(dev_priv->display.hotplug.dp_wq);
149 out_free_wq:
150 	destroy_workqueue(dev_priv->wq);
151 out_err:
152 	drm_err(&dev_priv->drm, "Failed to allocate workqueues.\n");
153 
154 	return -ENOMEM;
155 }
156 
157 static void i915_workqueues_cleanup(struct drm_i915_private *dev_priv)
158 {
159 	destroy_workqueue(dev_priv->unordered_wq);
160 	destroy_workqueue(dev_priv->display.hotplug.dp_wq);
161 	destroy_workqueue(dev_priv->wq);
162 }
163 
164 /*
165  * We don't keep the workarounds for pre-production hardware, so we expect our
166  * driver to fail on these machines in one way or another. A little warning on
167  * dmesg may help both the user and the bug triagers.
168  *
169  * Our policy for removing pre-production workarounds is to keep the
170  * current gen workarounds as a guide to the bring-up of the next gen
171  * (workarounds have a habit of persisting!). Anything older than that
172  * should be removed along with the complications they introduce.
173  */
174 static void intel_detect_preproduction_hw(struct drm_i915_private *dev_priv)
175 {
176 	bool pre = false;
177 
178 	pre |= IS_HASWELL_EARLY_SDV(dev_priv);
179 	pre |= IS_SKYLAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x6;
180 	pre |= IS_BROXTON(dev_priv) && INTEL_REVID(dev_priv) < 0xA;
181 	pre |= IS_KABYLAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x1;
182 	pre |= IS_GEMINILAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x3;
183 	pre |= IS_ICELAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x7;
184 	pre |= IS_TIGERLAKE(dev_priv) && INTEL_REVID(dev_priv) < 0x1;
185 	pre |= IS_DG1(dev_priv) && INTEL_REVID(dev_priv) < 0x1;
186 
187 	if (pre) {
188 		drm_err(&dev_priv->drm, "This is a pre-production stepping. "
189 			  "It may not be fully functional.\n");
190 		add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK);
191 	}
192 }
193 
194 static void sanitize_gpu(struct drm_i915_private *i915)
195 {
196 	if (!INTEL_INFO(i915)->gpu_reset_clobbers_display) {
197 		struct intel_gt *gt;
198 		unsigned int i;
199 
200 		for_each_gt(gt, i915, i)
201 			__intel_gt_reset(gt, ALL_ENGINES);
202 	}
203 }
204 
205 /**
206  * i915_driver_early_probe - setup state not requiring device access
207  * @dev_priv: device private
208  *
209  * Initialize everything that is a "SW-only" state, that is state not
210  * requiring accessing the device or exposing the driver via kernel internal
211  * or userspace interfaces. Example steps belonging here: lock initialization,
212  * system memory allocation, setting up device specific attributes and
213  * function hooks not requiring accessing the device.
214  */
215 static int i915_driver_early_probe(struct drm_i915_private *dev_priv)
216 {
217 	int ret = 0;
218 
219 	if (i915_inject_probe_failure(dev_priv))
220 		return -ENODEV;
221 
222 	intel_device_info_runtime_init_early(dev_priv);
223 
224 	intel_step_init(dev_priv);
225 
226 	intel_uncore_mmio_debug_init_early(dev_priv);
227 
228 	mtx_init(&dev_priv->irq_lock, IPL_TTY);
229 	mtx_init(&dev_priv->gpu_error.lock, IPL_TTY);
230 	rw_init(&dev_priv->display.backlight.lock, "blight");
231 
232 	rw_init(&dev_priv->sb_lock, "sb");
233 	cpu_latency_qos_add_request(&dev_priv->sb_qos, PM_QOS_DEFAULT_VALUE);
234 
235 	rw_init(&dev_priv->display.audio.mutex, "daud");
236 	rw_init(&dev_priv->display.wm.wm_mutex, "wmm");
237 	rw_init(&dev_priv->display.pps.mutex, "ppsm");
238 	rw_init(&dev_priv->display.hdcp.hdcp_mutex, "hdcpc");
239 
240 	i915_memcpy_init_early(dev_priv);
241 	intel_runtime_pm_init_early(&dev_priv->runtime_pm);
242 
243 	ret = i915_workqueues_init(dev_priv);
244 	if (ret < 0)
245 		return ret;
246 
247 	ret = vlv_suspend_init(dev_priv);
248 	if (ret < 0)
249 		goto err_workqueues;
250 
251 #ifdef __OpenBSD__
252 	dev_priv->bdev.iot = dev_priv->iot;
253 	dev_priv->bdev.memt = dev_priv->bst;
254 	dev_priv->bdev.dmat = dev_priv->dmat;
255 #endif
256 
257 	ret = intel_region_ttm_device_init(dev_priv);
258 	if (ret)
259 		goto err_ttm;
260 
261 	ret = intel_root_gt_init_early(dev_priv);
262 	if (ret < 0)
263 		goto err_rootgt;
264 
265 	i915_gem_init_early(dev_priv);
266 
267 	/* This must be called before any calls to HAS_PCH_* */
268 	intel_detect_pch(dev_priv);
269 
270 	intel_irq_init(dev_priv);
271 	intel_display_driver_early_probe(dev_priv);
272 	intel_clock_gating_hooks_init(dev_priv);
273 
274 	intel_detect_preproduction_hw(dev_priv);
275 
276 	return 0;
277 
278 err_rootgt:
279 	intel_region_ttm_device_fini(dev_priv);
280 err_ttm:
281 	vlv_suspend_cleanup(dev_priv);
282 err_workqueues:
283 	i915_workqueues_cleanup(dev_priv);
284 	return ret;
285 }
286 
287 /**
288  * i915_driver_late_release - cleanup the setup done in
289  *			       i915_driver_early_probe()
290  * @dev_priv: device private
291  */
292 static void i915_driver_late_release(struct drm_i915_private *dev_priv)
293 {
294 	intel_irq_fini(dev_priv);
295 	intel_power_domains_cleanup(dev_priv);
296 	i915_gem_cleanup_early(dev_priv);
297 	intel_gt_driver_late_release_all(dev_priv);
298 	intel_region_ttm_device_fini(dev_priv);
299 	vlv_suspend_cleanup(dev_priv);
300 	i915_workqueues_cleanup(dev_priv);
301 
302 	cpu_latency_qos_remove_request(&dev_priv->sb_qos);
303 	mutex_destroy(&dev_priv->sb_lock);
304 
305 	i915_params_free(&dev_priv->params);
306 }
307 
308 /**
309  * i915_driver_mmio_probe - setup device MMIO
310  * @dev_priv: device private
311  *
312  * Setup minimal device state necessary for MMIO accesses later in the
313  * initialization sequence. The setup here should avoid any other device-wide
314  * side effects or exposing the driver via kernel internal or user space
315  * interfaces.
316  */
317 static int i915_driver_mmio_probe(struct drm_i915_private *dev_priv)
318 {
319 	struct intel_gt *gt;
320 	int ret, i;
321 
322 	if (i915_inject_probe_failure(dev_priv))
323 		return -ENODEV;
324 
325 	ret = intel_gmch_bridge_setup(dev_priv);
326 	if (ret < 0)
327 		return ret;
328 
329 	for_each_gt(gt, dev_priv, i) {
330 		ret = intel_uncore_init_mmio(gt->uncore);
331 		if (ret)
332 			return ret;
333 
334 		ret = drmm_add_action_or_reset(&dev_priv->drm,
335 					       intel_uncore_fini_mmio,
336 					       gt->uncore);
337 		if (ret)
338 			return ret;
339 	}
340 
341 	/* Try to make sure MCHBAR is enabled before poking at it */
342 	intel_gmch_bar_setup(dev_priv);
343 	intel_device_info_runtime_init(dev_priv);
344 
345 	for_each_gt(gt, dev_priv, i) {
346 		ret = intel_gt_init_mmio(gt);
347 		if (ret)
348 			goto err_uncore;
349 	}
350 
351 	/* As early as possible, scrub existing GPU state before clobbering */
352 	sanitize_gpu(dev_priv);
353 
354 	return 0;
355 
356 err_uncore:
357 	intel_gmch_bar_teardown(dev_priv);
358 
359 	return ret;
360 }
361 
362 /**
363  * i915_driver_mmio_release - cleanup the setup done in i915_driver_mmio_probe()
364  * @dev_priv: device private
365  */
366 static void i915_driver_mmio_release(struct drm_i915_private *dev_priv)
367 {
368 	intel_gmch_bar_teardown(dev_priv);
369 }
370 
371 /**
372  * i915_set_dma_info - set all relevant PCI dma info as configured for the
373  * platform
374  * @i915: valid i915 instance
375  *
376  * Set the dma max segment size, device and coherent masks.  The dma mask set
377  * needs to occur before i915_ggtt_probe_hw.
378  *
379  * A couple of platforms have special needs.  Address them as well.
380  *
381  */
382 static int i915_set_dma_info(struct drm_i915_private *i915)
383 {
384 	unsigned int mask_size = INTEL_INFO(i915)->dma_mask_size;
385 	int ret;
386 
387 	GEM_BUG_ON(!mask_size);
388 
389 	/*
390 	 * We don't have a max segment size, so set it to the max so sg's
391 	 * debugging layer doesn't complain
392 	 */
393 	dma_set_max_seg_size(i915->drm.dev, UINT_MAX);
394 
395 	ret = dma_set_mask(i915->drm.dev, DMA_BIT_MASK(mask_size));
396 	if (ret)
397 		goto mask_err;
398 
399 	/* overlay on gen2 is broken and can't address above 1G */
400 	if (GRAPHICS_VER(i915) == 2)
401 		mask_size = 30;
402 
403 	/*
404 	 * 965GM sometimes incorrectly writes to hardware status page (HWS)
405 	 * using 32bit addressing, overwriting memory if HWS is located
406 	 * above 4GB.
407 	 *
408 	 * The documentation also mentions an issue with undefined
409 	 * behaviour if any general state is accessed within a page above 4GB,
410 	 * which also needs to be handled carefully.
411 	 */
412 	if (IS_I965G(i915) || IS_I965GM(i915))
413 		mask_size = 32;
414 
415 	ret = dma_set_coherent_mask(i915->drm.dev, DMA_BIT_MASK(mask_size));
416 	if (ret)
417 		goto mask_err;
418 
419 	return 0;
420 
421 mask_err:
422 	drm_err(&i915->drm, "Can't set DMA mask/consistent mask (%d)\n", ret);
423 	return ret;
424 }
425 
426 static int i915_pcode_init(struct drm_i915_private *i915)
427 {
428 	struct intel_gt *gt;
429 	int id, ret;
430 
431 	for_each_gt(gt, i915, id) {
432 		ret = intel_pcode_init(gt->uncore);
433 		if (ret) {
434 			drm_err(&gt->i915->drm, "gt%d: intel_pcode_init failed %d\n", id, ret);
435 			return ret;
436 		}
437 	}
438 
439 	return 0;
440 }
441 
442 /**
443  * i915_driver_hw_probe - setup state requiring device access
444  * @dev_priv: device private
445  *
446  * Setup state that requires accessing the device, but doesn't require
447  * exposing the driver via kernel internal or userspace interfaces.
448  */
449 static int i915_driver_hw_probe(struct drm_i915_private *dev_priv)
450 {
451 	struct pci_dev *pdev = dev_priv->drm.pdev;
452 	int ret;
453 
454 	if (i915_inject_probe_failure(dev_priv))
455 		return -ENODEV;
456 
457 	if (HAS_PPGTT(dev_priv)) {
458 		if (intel_vgpu_active(dev_priv) &&
459 		    !intel_vgpu_has_full_ppgtt(dev_priv)) {
460 			i915_report_error(dev_priv,
461 					  "incompatible vGPU found, support for isolated ppGTT required\n");
462 			return -ENXIO;
463 		}
464 	}
465 
466 	if (HAS_EXECLISTS(dev_priv)) {
467 		/*
468 		 * Older GVT emulation depends upon intercepting CSB mmio,
469 		 * which we no longer use, preferring to use the HWSP cache
470 		 * instead.
471 		 */
472 		if (intel_vgpu_active(dev_priv) &&
473 		    !intel_vgpu_has_hwsp_emulation(dev_priv)) {
474 			i915_report_error(dev_priv,
475 					  "old vGPU host found, support for HWSP emulation required\n");
476 			return -ENXIO;
477 		}
478 	}
479 
480 	/* needs to be done before ggtt probe */
481 	intel_dram_edram_detect(dev_priv);
482 
483 	ret = i915_set_dma_info(dev_priv);
484 	if (ret)
485 		return ret;
486 
487 	ret = i915_perf_init(dev_priv);
488 	if (ret)
489 		return ret;
490 
491 	ret = i915_ggtt_probe_hw(dev_priv);
492 	if (ret)
493 		goto err_perf;
494 
495 	ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, dev_priv->drm.driver);
496 	if (ret)
497 		goto err_ggtt;
498 
499 	ret = i915_ggtt_init_hw(dev_priv);
500 	if (ret)
501 		goto err_ggtt;
502 
503 	/*
504 	 * Make sure we probe lmem before we probe stolen-lmem. The BAR size
505 	 * might be different due to bar resizing.
506 	 */
507 	ret = intel_gt_tiles_init(dev_priv);
508 	if (ret)
509 		goto err_ggtt;
510 
511 	ret = intel_memory_regions_hw_probe(dev_priv);
512 	if (ret)
513 		goto err_ggtt;
514 
515 	ret = i915_ggtt_enable_hw(dev_priv);
516 	if (ret) {
517 		drm_err(&dev_priv->drm, "failed to enable GGTT\n");
518 		goto err_mem_regions;
519 	}
520 
521 	pci_set_master(pdev);
522 
523 	/* On the 945G/GM, the chipset reports the MSI capability on the
524 	 * integrated graphics even though the support isn't actually there
525 	 * according to the published specs.  It doesn't appear to function
526 	 * correctly in testing on 945G.
527 	 * This may be a side effect of MSI having been made available for PEG
528 	 * and the registers being closely associated.
529 	 *
530 	 * According to chipset errata, on the 965GM, MSI interrupts may
531 	 * be lost or delayed, and was defeatured. MSI interrupts seem to
532 	 * get lost on g4x as well, and interrupt delivery seems to stay
533 	 * properly dead afterwards. So we'll just disable them for all
534 	 * pre-gen5 chipsets.
535 	 *
536 	 * dp aux and gmbus irq on gen4 seems to be able to generate legacy
537 	 * interrupts even when in MSI mode. This results in spurious
538 	 * interrupt warnings if the legacy irq no. is shared with another
539 	 * device. The kernel then disables that interrupt source and so
540 	 * prevents the other device from working properly.
541 	 */
542 	if (GRAPHICS_VER(dev_priv) >= 5) {
543 		if (pci_enable_msi(pdev) < 0)
544 			drm_dbg(&dev_priv->drm, "can't enable MSI");
545 	}
546 
547 	ret = intel_gvt_init(dev_priv);
548 	if (ret)
549 		goto err_msi;
550 
551 	intel_opregion_setup(dev_priv);
552 
553 	ret = i915_pcode_init(dev_priv);
554 	if (ret)
555 		goto err_opregion;
556 
557 	/*
558 	 * Fill the dram structure to get the system dram info. This will be
559 	 * used for memory latency calculation.
560 	 */
561 	intel_dram_detect(dev_priv);
562 
563 	intel_bw_init_hw(dev_priv);
564 
565 	return 0;
566 
567 err_opregion:
568 	intel_opregion_cleanup(dev_priv);
569 err_msi:
570 	if (pdev->msi_enabled)
571 		pci_disable_msi(pdev);
572 err_mem_regions:
573 	intel_memory_regions_driver_release(dev_priv);
574 err_ggtt:
575 	i915_ggtt_driver_release(dev_priv);
576 	i915_gem_drain_freed_objects(dev_priv);
577 	i915_ggtt_driver_late_release(dev_priv);
578 err_perf:
579 	i915_perf_fini(dev_priv);
580 	return ret;
581 }
582 
583 /**
584  * i915_driver_hw_remove - cleanup the setup done in i915_driver_hw_probe()
585  * @dev_priv: device private
586  */
587 static void i915_driver_hw_remove(struct drm_i915_private *dev_priv)
588 {
589 	struct pci_dev *pdev = dev_priv->drm.pdev;
590 
591 	i915_perf_fini(dev_priv);
592 
593 	intel_opregion_cleanup(dev_priv);
594 
595 	if (pdev->msi_enabled)
596 		pci_disable_msi(pdev);
597 }
598 
599 /**
600  * i915_driver_register - register the driver with the rest of the system
601  * @dev_priv: device private
602  *
603  * Perform any steps necessary to make the driver available via kernel
604  * internal or userspace interfaces.
605  */
606 static void i915_driver_register(struct drm_i915_private *dev_priv)
607 {
608 	struct intel_gt *gt;
609 	unsigned int i;
610 
611 	i915_gem_driver_register(dev_priv);
612 	i915_pmu_register(dev_priv);
613 
614 	intel_vgpu_register(dev_priv);
615 
616 	/* Reveal our presence to userspace */
617 	if (drm_dev_register(&dev_priv->drm, 0)) {
618 		drm_err(&dev_priv->drm,
619 			"Failed to register driver for userspace access!\n");
620 		return;
621 	}
622 
623 	i915_debugfs_register(dev_priv);
624 	i915_setup_sysfs(dev_priv);
625 
626 	/* Depends on sysfs having been initialized */
627 	i915_perf_register(dev_priv);
628 
629 	for_each_gt(gt, dev_priv, i)
630 		intel_gt_driver_register(gt);
631 
632 	intel_pxp_debugfs_register(dev_priv->pxp);
633 
634 	i915_hwmon_register(dev_priv);
635 
636 	intel_display_driver_register(dev_priv);
637 
638 	intel_power_domains_enable(dev_priv);
639 	intel_runtime_pm_enable(&dev_priv->runtime_pm);
640 
641 	intel_register_dsm_handler();
642 
643 	if (i915_switcheroo_register(dev_priv))
644 		drm_err(&dev_priv->drm, "Failed to register vga switcheroo!\n");
645 }
646 
647 /**
648  * i915_driver_unregister - cleanup the registration done in i915_driver_regiser()
649  * @dev_priv: device private
650  */
651 static void i915_driver_unregister(struct drm_i915_private *dev_priv)
652 {
653 	struct intel_gt *gt;
654 	unsigned int i;
655 
656 	i915_switcheroo_unregister(dev_priv);
657 
658 	intel_unregister_dsm_handler();
659 
660 	intel_runtime_pm_disable(&dev_priv->runtime_pm);
661 	intel_power_domains_disable(dev_priv);
662 
663 	intel_display_driver_unregister(dev_priv);
664 
665 	intel_pxp_fini(dev_priv);
666 
667 	for_each_gt(gt, dev_priv, i)
668 		intel_gt_driver_unregister(gt);
669 
670 	i915_hwmon_unregister(dev_priv);
671 
672 	i915_perf_unregister(dev_priv);
673 	i915_pmu_unregister(dev_priv);
674 
675 	i915_teardown_sysfs(dev_priv);
676 	drm_dev_unplug(&dev_priv->drm);
677 
678 	i915_gem_driver_unregister(dev_priv);
679 }
680 
681 void
682 i915_print_iommu_status(struct drm_i915_private *i915, struct drm_printer *p)
683 {
684 	drm_printf(p, "iommu: %s\n",
685 		   str_enabled_disabled(i915_vtd_active(i915)));
686 }
687 
688 static void i915_welcome_messages(struct drm_i915_private *dev_priv)
689 {
690 	if (drm_debug_enabled(DRM_UT_DRIVER)) {
691 		struct drm_printer p = drm_debug_printer("i915 device info:");
692 		struct intel_gt *gt;
693 		unsigned int i;
694 
695 		drm_printf(&p, "pciid=0x%04x rev=0x%02x platform=%s (subplatform=0x%x) gen=%i\n",
696 			   INTEL_DEVID(dev_priv),
697 			   INTEL_REVID(dev_priv),
698 			   intel_platform_name(INTEL_INFO(dev_priv)->platform),
699 			   intel_subplatform(RUNTIME_INFO(dev_priv),
700 					     INTEL_INFO(dev_priv)->platform),
701 			   GRAPHICS_VER(dev_priv));
702 
703 		intel_device_info_print(INTEL_INFO(dev_priv),
704 					RUNTIME_INFO(dev_priv), &p);
705 		intel_display_device_info_print(DISPLAY_INFO(dev_priv),
706 						DISPLAY_RUNTIME_INFO(dev_priv), &p);
707 		i915_print_iommu_status(dev_priv, &p);
708 		for_each_gt(gt, dev_priv, i)
709 			intel_gt_info_print(&gt->info, &p);
710 	}
711 
712 	if (IS_ENABLED(CONFIG_DRM_I915_DEBUG))
713 		drm_info(&dev_priv->drm, "DRM_I915_DEBUG enabled\n");
714 	if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
715 		drm_info(&dev_priv->drm, "DRM_I915_DEBUG_GEM enabled\n");
716 	if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_RUNTIME_PM))
717 		drm_info(&dev_priv->drm,
718 			 "DRM_I915_DEBUG_RUNTIME_PM enabled\n");
719 }
720 
721 #ifdef __linux__
722 
723 static struct drm_i915_private *
724 i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent)
725 {
726 	const struct intel_device_info *match_info =
727 		(struct intel_device_info *)ent->driver_data;
728 	struct drm_i915_private *i915;
729 
730 	i915 = devm_drm_dev_alloc(&pdev->dev, &i915_drm_driver,
731 				  struct drm_i915_private, drm);
732 	if (IS_ERR(i915))
733 		return i915;
734 
735 	pci_set_drvdata(pdev, i915);
736 
737 	/* Device parameters start as a copy of module parameters. */
738 	i915_params_copy(&i915->params, &i915_modparams);
739 
740 	/* Set up device info and initial runtime info. */
741 	intel_device_info_driver_create(i915, pdev->device, match_info);
742 
743 	return i915;
744 }
745 
746 #endif
747 
748 void inteldrm_init_backlight(struct inteldrm_softc *);
749 
750 /**
751  * i915_driver_probe - setup chip and create an initial config
752  * @pdev: PCI device
753  * @ent: matching PCI ID entry
754  *
755  * The driver probe routine has to do several things:
756  *   - drive output discovery via intel_display_driver_probe()
757  *   - initialize the memory manager
758  *   - allocate initial config memory
759  *   - setup the DRM framebuffer with the allocated memory
760  */
761 int i915_driver_probe(struct drm_i915_private *i915, const struct pci_device_id *ent)
762 {
763 #ifdef __linux__
764 	struct drm_i915_private *i915;
765 	int ret;
766 
767 	ret = pci_enable_device(pdev);
768 	if (ret) {
769 		pr_err("Failed to enable graphics device: %pe\n", ERR_PTR(ret));
770 		return ret;
771 	}
772 
773 	i915 = i915_driver_create(pdev, ent);
774 	if (IS_ERR(i915)) {
775 		pci_disable_device(pdev);
776 		return PTR_ERR(i915);
777 	}
778 #else
779 	struct pci_dev *pdev = i915->drm.pdev;
780 	int ret;
781 #endif
782 
783 	ret = i915_driver_early_probe(i915);
784 	if (ret < 0)
785 		goto out_pci_disable;
786 
787 	disable_rpm_wakeref_asserts(&i915->runtime_pm);
788 
789 	intel_vgpu_detect(i915);
790 
791 	ret = intel_gt_probe_all(i915);
792 	if (ret < 0)
793 		goto out_runtime_pm_put;
794 
795 	ret = i915_driver_mmio_probe(i915);
796 	if (ret < 0)
797 		goto out_runtime_pm_put;
798 
799 	ret = i915_driver_hw_probe(i915);
800 	if (ret < 0)
801 		goto out_cleanup_mmio;
802 
803 	ret = intel_display_driver_probe_noirq(i915);
804 	if (ret < 0)
805 		goto out_cleanup_hw;
806 
807 	ret = intel_irq_install(i915);
808 	if (ret)
809 		goto out_cleanup_modeset;
810 
811 	ret = intel_display_driver_probe_nogem(i915);
812 	if (ret)
813 		goto out_cleanup_irq;
814 
815 	ret = i915_gem_init(i915);
816 	if (ret)
817 		goto out_cleanup_modeset2;
818 
819 	intel_pxp_init(i915);
820 
821 	ret = intel_display_driver_probe(i915);
822 	if (ret)
823 		goto out_cleanup_gem;
824 
825 	i915_driver_register(i915);
826 
827 #ifdef __OpenBSD__
828 	inteldrm_init_backlight(i915);
829 #endif
830 
831 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
832 
833 	i915_welcome_messages(i915);
834 
835 	i915->do_release = true;
836 
837 	return 0;
838 
839 out_cleanup_gem:
840 	i915_gem_suspend(i915);
841 	i915_gem_driver_remove(i915);
842 	i915_gem_driver_release(i915);
843 out_cleanup_modeset2:
844 	/* FIXME clean up the error path */
845 	intel_display_driver_remove(i915);
846 	intel_irq_uninstall(i915);
847 	intel_display_driver_remove_noirq(i915);
848 	goto out_cleanup_modeset;
849 out_cleanup_irq:
850 	intel_irq_uninstall(i915);
851 out_cleanup_modeset:
852 	intel_display_driver_remove_nogem(i915);
853 out_cleanup_hw:
854 	i915_driver_hw_remove(i915);
855 	intel_memory_regions_driver_release(i915);
856 	i915_ggtt_driver_release(i915);
857 	i915_gem_drain_freed_objects(i915);
858 	i915_ggtt_driver_late_release(i915);
859 out_cleanup_mmio:
860 	i915_driver_mmio_release(i915);
861 out_runtime_pm_put:
862 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
863 	i915_driver_late_release(i915);
864 out_pci_disable:
865 	pci_disable_device(pdev);
866 	i915_probe_error(i915, "Device initialization failed (%d)\n", ret);
867 	return ret;
868 }
869 
870 void i915_driver_remove(struct drm_i915_private *i915)
871 {
872 	intel_wakeref_t wakeref;
873 
874 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
875 
876 	i915_driver_unregister(i915);
877 
878 	/* Flush any external code that still may be under the RCU lock */
879 	synchronize_rcu();
880 
881 	i915_gem_suspend(i915);
882 
883 	intel_gvt_driver_remove(i915);
884 
885 	intel_display_driver_remove(i915);
886 
887 	intel_irq_uninstall(i915);
888 
889 	intel_display_driver_remove_noirq(i915);
890 
891 	i915_reset_error_state(i915);
892 	i915_gem_driver_remove(i915);
893 
894 	intel_display_driver_remove_nogem(i915);
895 
896 	i915_driver_hw_remove(i915);
897 
898 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
899 }
900 
901 static void i915_driver_release(struct drm_device *dev)
902 {
903 	struct drm_i915_private *dev_priv = to_i915(dev);
904 	struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
905 	intel_wakeref_t wakeref;
906 
907 	if (!dev_priv->do_release)
908 		return;
909 
910 	wakeref = intel_runtime_pm_get(rpm);
911 
912 	i915_gem_driver_release(dev_priv);
913 
914 	intel_memory_regions_driver_release(dev_priv);
915 	i915_ggtt_driver_release(dev_priv);
916 	i915_gem_drain_freed_objects(dev_priv);
917 	i915_ggtt_driver_late_release(dev_priv);
918 
919 	i915_driver_mmio_release(dev_priv);
920 
921 	intel_runtime_pm_put(rpm, wakeref);
922 
923 	intel_runtime_pm_driver_release(rpm);
924 
925 	i915_driver_late_release(dev_priv);
926 }
927 
928 static int i915_driver_open(struct drm_device *dev, struct drm_file *file)
929 {
930 	struct drm_i915_private *i915 = to_i915(dev);
931 	int ret;
932 
933 	ret = i915_gem_open(i915, file);
934 	if (ret)
935 		return ret;
936 
937 	return 0;
938 }
939 
940 /**
941  * i915_driver_lastclose - clean up after all DRM clients have exited
942  * @dev: DRM device
943  *
944  * Take care of cleaning up after all DRM clients have exited.  In the
945  * mode setting case, we want to restore the kernel's initial mode (just
946  * in case the last client left us in a bad state).
947  *
948  * Additionally, in the non-mode setting case, we'll tear down the GTT
949  * and DMA structures, since the kernel won't be using them, and clea
950  * up any GEM state.
951  */
952 static void i915_driver_lastclose(struct drm_device *dev)
953 {
954 	struct drm_i915_private *i915 = to_i915(dev);
955 
956 	intel_fbdev_restore_mode(i915);
957 
958 	vga_switcheroo_process_delayed_switch();
959 }
960 
961 static void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
962 {
963 	struct drm_i915_file_private *file_priv = file->driver_priv;
964 
965 	i915_gem_context_close(file);
966 	i915_drm_client_put(file_priv->client);
967 
968 	kfree_rcu(file_priv, rcu);
969 
970 	/* Catch up with all the deferred frees from "this" client */
971 	i915_gem_flush_free_objects(to_i915(dev));
972 }
973 
974 static void intel_suspend_encoders(struct drm_i915_private *dev_priv)
975 {
976 	struct intel_encoder *encoder;
977 
978 	if (!HAS_DISPLAY(dev_priv))
979 		return;
980 
981 	/*
982 	 * TODO: check and remove holding the modeset locks if none of
983 	 * the encoders depends on this.
984 	 */
985 	drm_modeset_lock_all(&dev_priv->drm);
986 	for_each_intel_encoder(&dev_priv->drm, encoder)
987 		if (encoder->suspend)
988 			encoder->suspend(encoder);
989 	drm_modeset_unlock_all(&dev_priv->drm);
990 
991 	for_each_intel_encoder(&dev_priv->drm, encoder)
992 		if (encoder->suspend_complete)
993 			encoder->suspend_complete(encoder);
994 }
995 
996 static void intel_shutdown_encoders(struct drm_i915_private *dev_priv)
997 {
998 	struct intel_encoder *encoder;
999 
1000 	if (!HAS_DISPLAY(dev_priv))
1001 		return;
1002 
1003 	/*
1004 	 * TODO: check and remove holding the modeset locks if none of
1005 	 * the encoders depends on this.
1006 	 */
1007 	drm_modeset_lock_all(&dev_priv->drm);
1008 	for_each_intel_encoder(&dev_priv->drm, encoder)
1009 		if (encoder->shutdown)
1010 			encoder->shutdown(encoder);
1011 	drm_modeset_unlock_all(&dev_priv->drm);
1012 
1013 	for_each_intel_encoder(&dev_priv->drm, encoder)
1014 		if (encoder->shutdown_complete)
1015 			encoder->shutdown_complete(encoder);
1016 }
1017 
1018 void i915_driver_shutdown(struct drm_i915_private *i915)
1019 {
1020 	disable_rpm_wakeref_asserts(&i915->runtime_pm);
1021 	intel_runtime_pm_disable(&i915->runtime_pm);
1022 	intel_power_domains_disable(i915);
1023 
1024 	if (HAS_DISPLAY(i915)) {
1025 		drm_kms_helper_poll_disable(&i915->drm);
1026 
1027 		drm_atomic_helper_shutdown(&i915->drm);
1028 	}
1029 
1030 	intel_dp_mst_suspend(i915);
1031 
1032 	intel_runtime_pm_disable_interrupts(i915);
1033 	intel_hpd_cancel_work(i915);
1034 
1035 	intel_suspend_encoders(i915);
1036 	intel_shutdown_encoders(i915);
1037 
1038 	intel_dmc_suspend(i915);
1039 
1040 	i915_gem_suspend(i915);
1041 
1042 	/*
1043 	 * The only requirement is to reboot with display DC states disabled,
1044 	 * for now leaving all display power wells in the INIT power domain
1045 	 * enabled.
1046 	 *
1047 	 * TODO:
1048 	 * - unify the pci_driver::shutdown sequence here with the
1049 	 *   pci_driver.driver.pm.poweroff,poweroff_late sequence.
1050 	 * - unify the driver remove and system/runtime suspend sequences with
1051 	 *   the above unified shutdown/poweroff sequence.
1052 	 */
1053 	intel_power_domains_driver_remove(i915);
1054 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
1055 
1056 	intel_runtime_pm_driver_release(&i915->runtime_pm);
1057 }
1058 
1059 static bool suspend_to_idle(struct drm_i915_private *dev_priv)
1060 {
1061 #if IS_ENABLED(CONFIG_ACPI_SLEEP)
1062 	if (acpi_target_system_state() < ACPI_STATE_S3)
1063 		return true;
1064 #endif
1065 	return false;
1066 }
1067 
1068 static void i915_drm_complete(struct drm_device *dev)
1069 {
1070 	struct drm_i915_private *i915 = to_i915(dev);
1071 
1072 	intel_pxp_resume_complete(i915->pxp);
1073 }
1074 
1075 static int i915_drm_prepare(struct drm_device *dev)
1076 {
1077 	struct drm_i915_private *i915 = to_i915(dev);
1078 
1079 	intel_pxp_suspend_prepare(i915->pxp);
1080 
1081 	/*
1082 	 * NB intel_display_driver_suspend() may issue new requests after we've
1083 	 * ostensibly marked the GPU as ready-to-sleep here. We need to
1084 	 * split out that work and pull it forward so that after point,
1085 	 * the GPU is not woken again.
1086 	 */
1087 	return i915_gem_backup_suspend(i915);
1088 }
1089 
1090 static int i915_drm_suspend(struct drm_device *dev)
1091 {
1092 	struct drm_i915_private *dev_priv = to_i915(dev);
1093 	struct pci_dev *pdev = dev_priv->drm.pdev;
1094 	pci_power_t opregion_target_state;
1095 
1096 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1097 
1098 	/* We do a lot of poking in a lot of registers, make sure they work
1099 	 * properly. */
1100 	intel_power_domains_disable(dev_priv);
1101 	if (HAS_DISPLAY(dev_priv))
1102 		drm_kms_helper_poll_disable(dev);
1103 
1104 	pci_save_state(pdev);
1105 
1106 	intel_display_driver_suspend(dev_priv);
1107 
1108 	intel_dp_mst_suspend(dev_priv);
1109 
1110 	intel_runtime_pm_disable_interrupts(dev_priv);
1111 	intel_hpd_cancel_work(dev_priv);
1112 
1113 	intel_suspend_encoders(dev_priv);
1114 
1115 	/* Must be called before GGTT is suspended. */
1116 	intel_dpt_suspend(dev_priv);
1117 	i915_ggtt_suspend(to_gt(dev_priv)->ggtt);
1118 
1119 	i915_save_display(dev_priv);
1120 
1121 	opregion_target_state = suspend_to_idle(dev_priv) ? PCI_D1 : PCI_D3cold;
1122 	intel_opregion_suspend(dev_priv, opregion_target_state);
1123 
1124 	intel_fbdev_set_suspend(dev, FBINFO_STATE_SUSPENDED, true);
1125 
1126 	dev_priv->suspend_count++;
1127 
1128 	intel_dmc_suspend(dev_priv);
1129 
1130 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1131 
1132 	i915_gem_drain_freed_objects(dev_priv);
1133 
1134 	return 0;
1135 }
1136 
1137 static int i915_drm_suspend_late(struct drm_device *dev, bool hibernation)
1138 {
1139 	struct drm_i915_private *dev_priv = to_i915(dev);
1140 	struct pci_dev *pdev = dev_priv->drm.pdev;
1141 	struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
1142 	struct intel_gt *gt;
1143 	int ret, i;
1144 	bool s2idle = !hibernation && suspend_to_idle(dev_priv);
1145 
1146 	disable_rpm_wakeref_asserts(rpm);
1147 
1148 	intel_pxp_suspend(dev_priv->pxp);
1149 
1150 	i915_gem_suspend_late(dev_priv);
1151 
1152 	for_each_gt(gt, dev_priv, i)
1153 		intel_uncore_suspend(gt->uncore);
1154 
1155 	intel_power_domains_suspend(dev_priv, s2idle);
1156 
1157 	intel_display_power_suspend_late(dev_priv);
1158 
1159 	ret = vlv_suspend_complete(dev_priv);
1160 	if (ret) {
1161 		drm_err(&dev_priv->drm, "Suspend complete failed: %d\n", ret);
1162 		intel_power_domains_resume(dev_priv);
1163 
1164 		goto out;
1165 	}
1166 
1167 	pci_disable_device(pdev);
1168 	/*
1169 	 * During hibernation on some platforms the BIOS may try to access
1170 	 * the device even though it's already in D3 and hang the machine. So
1171 	 * leave the device in D0 on those platforms and hope the BIOS will
1172 	 * power down the device properly. The issue was seen on multiple old
1173 	 * GENs with different BIOS vendors, so having an explicit blacklist
1174 	 * is inpractical; apply the workaround on everything pre GEN6. The
1175 	 * platforms where the issue was seen:
1176 	 * Lenovo Thinkpad X301, X61s, X60, T60, X41
1177 	 * Fujitsu FSC S7110
1178 	 * Acer Aspire 1830T
1179 	 */
1180 	if (!(hibernation && GRAPHICS_VER(dev_priv) < 6))
1181 		pci_set_power_state(pdev, PCI_D3hot);
1182 
1183 out:
1184 	enable_rpm_wakeref_asserts(rpm);
1185 	if (!dev_priv->uncore.user_forcewake_count)
1186 		intel_runtime_pm_driver_release(rpm);
1187 
1188 	return ret;
1189 }
1190 
1191 #ifdef __linux__
1192 int i915_driver_suspend_switcheroo(struct drm_i915_private *i915,
1193 				   pm_message_t state)
1194 {
1195 	int error;
1196 
1197 	if (drm_WARN_ON_ONCE(&i915->drm, state.event != PM_EVENT_SUSPEND &&
1198 			     state.event != PM_EVENT_FREEZE))
1199 		return -EINVAL;
1200 
1201 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
1202 		return 0;
1203 
1204 	error = i915_drm_suspend(&i915->drm);
1205 	if (error)
1206 		return error;
1207 
1208 	return i915_drm_suspend_late(&i915->drm, false);
1209 }
1210 #endif
1211 
1212 static int i915_drm_resume(struct drm_device *dev)
1213 {
1214 	struct drm_i915_private *dev_priv = to_i915(dev);
1215 	struct intel_gt *gt;
1216 	int ret, i;
1217 
1218 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1219 
1220 	ret = i915_pcode_init(dev_priv);
1221 	if (ret)
1222 		return ret;
1223 
1224 	sanitize_gpu(dev_priv);
1225 
1226 	ret = i915_ggtt_enable_hw(dev_priv);
1227 	if (ret)
1228 		drm_err(&dev_priv->drm, "failed to re-enable GGTT\n");
1229 
1230 	i915_ggtt_resume(to_gt(dev_priv)->ggtt);
1231 
1232 	for_each_gt(gt, dev_priv, i)
1233 		if (GRAPHICS_VER(gt->i915) >= 8)
1234 			setup_private_pat(gt);
1235 
1236 	/* Must be called after GGTT is resumed. */
1237 	intel_dpt_resume(dev_priv);
1238 
1239 	intel_dmc_resume(dev_priv);
1240 
1241 	i915_restore_display(dev_priv);
1242 	intel_pps_unlock_regs_wa(dev_priv);
1243 
1244 	intel_init_pch_refclk(dev_priv);
1245 
1246 	/*
1247 	 * Interrupts have to be enabled before any batches are run. If not the
1248 	 * GPU will hang. i915_gem_init_hw() will initiate batches to
1249 	 * update/restore the context.
1250 	 *
1251 	 * drm_mode_config_reset() needs AUX interrupts.
1252 	 *
1253 	 * Modeset enabling in intel_display_driver_init_hw() also needs working
1254 	 * interrupts.
1255 	 */
1256 	intel_runtime_pm_enable_interrupts(dev_priv);
1257 
1258 	if (HAS_DISPLAY(dev_priv))
1259 		drm_mode_config_reset(dev);
1260 
1261 	i915_gem_resume(dev_priv);
1262 
1263 	intel_display_driver_init_hw(dev_priv);
1264 
1265 	intel_clock_gating_init(dev_priv);
1266 	intel_hpd_init(dev_priv);
1267 
1268 	/* MST sideband requires HPD interrupts enabled */
1269 	intel_dp_mst_resume(dev_priv);
1270 	intel_display_driver_resume(dev_priv);
1271 
1272 	intel_hpd_poll_disable(dev_priv);
1273 	if (HAS_DISPLAY(dev_priv))
1274 		drm_kms_helper_poll_enable(dev);
1275 
1276 	intel_opregion_resume(dev_priv);
1277 
1278 	intel_fbdev_set_suspend(dev, FBINFO_STATE_RUNNING, false);
1279 
1280 	intel_power_domains_enable(dev_priv);
1281 
1282 	intel_gvt_resume(dev_priv);
1283 
1284 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1285 
1286 	return 0;
1287 }
1288 
1289 static int i915_drm_resume_early(struct drm_device *dev)
1290 {
1291 	struct drm_i915_private *dev_priv = to_i915(dev);
1292 	struct pci_dev *pdev = dev_priv->drm.pdev;
1293 	struct intel_gt *gt;
1294 	int ret, i;
1295 
1296 	/*
1297 	 * We have a resume ordering issue with the snd-hda driver also
1298 	 * requiring our device to be power up. Due to the lack of a
1299 	 * parent/child relationship we currently solve this with an early
1300 	 * resume hook.
1301 	 *
1302 	 * FIXME: This should be solved with a special hdmi sink device or
1303 	 * similar so that power domains can be employed.
1304 	 */
1305 
1306 	/*
1307 	 * Note that we need to set the power state explicitly, since we
1308 	 * powered off the device during freeze and the PCI core won't power
1309 	 * it back up for us during thaw. Powering off the device during
1310 	 * freeze is not a hard requirement though, and during the
1311 	 * suspend/resume phases the PCI core makes sure we get here with the
1312 	 * device powered on. So in case we change our freeze logic and keep
1313 	 * the device powered we can also remove the following set power state
1314 	 * call.
1315 	 */
1316 	ret = pci_set_power_state(pdev, PCI_D0);
1317 	if (ret) {
1318 		drm_err(&dev_priv->drm,
1319 			"failed to set PCI D0 power state (%d)\n", ret);
1320 		return ret;
1321 	}
1322 
1323 	/*
1324 	 * Note that pci_enable_device() first enables any parent bridge
1325 	 * device and only then sets the power state for this device. The
1326 	 * bridge enabling is a nop though, since bridge devices are resumed
1327 	 * first. The order of enabling power and enabling the device is
1328 	 * imposed by the PCI core as described above, so here we preserve the
1329 	 * same order for the freeze/thaw phases.
1330 	 *
1331 	 * TODO: eventually we should remove pci_disable_device() /
1332 	 * pci_enable_enable_device() from suspend/resume. Due to how they
1333 	 * depend on the device enable refcount we can't anyway depend on them
1334 	 * disabling/enabling the device.
1335 	 */
1336 	if (pci_enable_device(pdev))
1337 		return -EIO;
1338 
1339 	pci_set_master(pdev);
1340 
1341 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1342 
1343 	ret = vlv_resume_prepare(dev_priv, false);
1344 	if (ret)
1345 		drm_err(&dev_priv->drm,
1346 			"Resume prepare failed: %d, continuing anyway\n", ret);
1347 
1348 	for_each_gt(gt, dev_priv, i) {
1349 		intel_uncore_resume_early(gt->uncore);
1350 		intel_gt_check_and_clear_faults(gt);
1351 	}
1352 
1353 	intel_display_power_resume_early(dev_priv);
1354 
1355 	intel_power_domains_resume(dev_priv);
1356 
1357 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
1358 
1359 	return ret;
1360 }
1361 
1362 int i915_driver_resume_switcheroo(struct drm_i915_private *i915)
1363 {
1364 	int ret;
1365 
1366 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
1367 		return 0;
1368 
1369 	ret = i915_drm_resume_early(&i915->drm);
1370 	if (ret)
1371 		return ret;
1372 
1373 	return i915_drm_resume(&i915->drm);
1374 }
1375 
1376 #ifdef __linux__
1377 
1378 static int i915_pm_prepare(struct device *kdev)
1379 {
1380 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1381 
1382 	if (!i915) {
1383 		dev_err(kdev, "DRM not initialized, aborting suspend.\n");
1384 		return -ENODEV;
1385 	}
1386 
1387 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
1388 		return 0;
1389 
1390 	return i915_drm_prepare(&i915->drm);
1391 }
1392 
1393 static int i915_pm_suspend(struct device *kdev)
1394 {
1395 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1396 
1397 	if (!i915) {
1398 		dev_err(kdev, "DRM not initialized, aborting suspend.\n");
1399 		return -ENODEV;
1400 	}
1401 
1402 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
1403 		return 0;
1404 
1405 	return i915_drm_suspend(&i915->drm);
1406 }
1407 
1408 static int i915_pm_suspend_late(struct device *kdev)
1409 {
1410 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1411 
1412 	/*
1413 	 * We have a suspend ordering issue with the snd-hda driver also
1414 	 * requiring our device to be power up. Due to the lack of a
1415 	 * parent/child relationship we currently solve this with an late
1416 	 * suspend hook.
1417 	 *
1418 	 * FIXME: This should be solved with a special hdmi sink device or
1419 	 * similar so that power domains can be employed.
1420 	 */
1421 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
1422 		return 0;
1423 
1424 	return i915_drm_suspend_late(&i915->drm, false);
1425 }
1426 
1427 static int i915_pm_poweroff_late(struct device *kdev)
1428 {
1429 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1430 
1431 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
1432 		return 0;
1433 
1434 	return i915_drm_suspend_late(&i915->drm, true);
1435 }
1436 
1437 static int i915_pm_resume_early(struct device *kdev)
1438 {
1439 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1440 
1441 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
1442 		return 0;
1443 
1444 	return i915_drm_resume_early(&i915->drm);
1445 }
1446 
1447 static int i915_pm_resume(struct device *kdev)
1448 {
1449 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1450 
1451 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
1452 		return 0;
1453 
1454 	return i915_drm_resume(&i915->drm);
1455 }
1456 
1457 static void i915_pm_complete(struct device *kdev)
1458 {
1459 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1460 
1461 	if (i915->drm.switch_power_state == DRM_SWITCH_POWER_OFF)
1462 		return;
1463 
1464 	i915_drm_complete(&i915->drm);
1465 }
1466 
1467 /* freeze: before creating the hibernation_image */
1468 static int i915_pm_freeze(struct device *kdev)
1469 {
1470 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1471 	int ret;
1472 
1473 	if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) {
1474 		ret = i915_drm_suspend(&i915->drm);
1475 		if (ret)
1476 			return ret;
1477 	}
1478 
1479 	ret = i915_gem_freeze(i915);
1480 	if (ret)
1481 		return ret;
1482 
1483 	return 0;
1484 }
1485 
1486 static int i915_pm_freeze_late(struct device *kdev)
1487 {
1488 	struct drm_i915_private *i915 = kdev_to_i915(kdev);
1489 	int ret;
1490 
1491 	if (i915->drm.switch_power_state != DRM_SWITCH_POWER_OFF) {
1492 		ret = i915_drm_suspend_late(&i915->drm, true);
1493 		if (ret)
1494 			return ret;
1495 	}
1496 
1497 	ret = i915_gem_freeze_late(i915);
1498 	if (ret)
1499 		return ret;
1500 
1501 	return 0;
1502 }
1503 
1504 /* thaw: called after creating the hibernation image, but before turning off. */
1505 static int i915_pm_thaw_early(struct device *kdev)
1506 {
1507 	return i915_pm_resume_early(kdev);
1508 }
1509 
1510 static int i915_pm_thaw(struct device *kdev)
1511 {
1512 	return i915_pm_resume(kdev);
1513 }
1514 
1515 /* restore: called after loading the hibernation image. */
1516 static int i915_pm_restore_early(struct device *kdev)
1517 {
1518 	return i915_pm_resume_early(kdev);
1519 }
1520 
1521 static int i915_pm_restore(struct device *kdev)
1522 {
1523 	return i915_pm_resume(kdev);
1524 }
1525 
1526 static int intel_runtime_suspend(struct device *kdev)
1527 {
1528 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1529 	struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
1530 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
1531 	struct pci_dev *root_pdev;
1532 	struct intel_gt *gt;
1533 	int ret, i;
1534 
1535 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_RUNTIME_PM(dev_priv)))
1536 		return -ENODEV;
1537 
1538 	drm_dbg(&dev_priv->drm, "Suspending device\n");
1539 
1540 	disable_rpm_wakeref_asserts(rpm);
1541 
1542 	/*
1543 	 * We are safe here against re-faults, since the fault handler takes
1544 	 * an RPM reference.
1545 	 */
1546 	i915_gem_runtime_suspend(dev_priv);
1547 
1548 	intel_pxp_runtime_suspend(dev_priv->pxp);
1549 
1550 	for_each_gt(gt, dev_priv, i)
1551 		intel_gt_runtime_suspend(gt);
1552 
1553 	intel_runtime_pm_disable_interrupts(dev_priv);
1554 
1555 	for_each_gt(gt, dev_priv, i)
1556 		intel_uncore_suspend(gt->uncore);
1557 
1558 	intel_display_power_suspend(dev_priv);
1559 
1560 	ret = vlv_suspend_complete(dev_priv);
1561 	if (ret) {
1562 		drm_err(&dev_priv->drm,
1563 			"Runtime suspend failed, disabling it (%d)\n", ret);
1564 		intel_uncore_runtime_resume(&dev_priv->uncore);
1565 
1566 		intel_runtime_pm_enable_interrupts(dev_priv);
1567 
1568 		for_each_gt(gt, dev_priv, i)
1569 			intel_gt_runtime_resume(gt);
1570 
1571 		enable_rpm_wakeref_asserts(rpm);
1572 
1573 		return ret;
1574 	}
1575 
1576 	enable_rpm_wakeref_asserts(rpm);
1577 	intel_runtime_pm_driver_release(rpm);
1578 
1579 	if (intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore))
1580 		drm_err(&dev_priv->drm,
1581 			"Unclaimed access detected prior to suspending\n");
1582 
1583 	/*
1584 	 * FIXME: Temporary hammer to avoid freezing the machine on our DGFX
1585 	 * This should be totally removed when we handle the pci states properly
1586 	 * on runtime PM.
1587 	 */
1588 	root_pdev = pcie_find_root_port(pdev);
1589 	if (root_pdev)
1590 		pci_d3cold_disable(root_pdev);
1591 
1592 	rpm->suspended = true;
1593 
1594 	/*
1595 	 * FIXME: We really should find a document that references the arguments
1596 	 * used below!
1597 	 */
1598 	if (IS_BROADWELL(dev_priv)) {
1599 		/*
1600 		 * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop
1601 		 * being detected, and the call we do at intel_runtime_resume()
1602 		 * won't be able to restore them. Since PCI_D3hot matches the
1603 		 * actual specification and appears to be working, use it.
1604 		 */
1605 		intel_opregion_notify_adapter(dev_priv, PCI_D3hot);
1606 	} else {
1607 		/*
1608 		 * current versions of firmware which depend on this opregion
1609 		 * notification have repurposed the D1 definition to mean
1610 		 * "runtime suspended" vs. what you would normally expect (D3)
1611 		 * to distinguish it from notifications that might be sent via
1612 		 * the suspend path.
1613 		 */
1614 		intel_opregion_notify_adapter(dev_priv, PCI_D1);
1615 	}
1616 
1617 	assert_forcewakes_inactive(&dev_priv->uncore);
1618 
1619 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
1620 		intel_hpd_poll_enable(dev_priv);
1621 
1622 	drm_dbg(&dev_priv->drm, "Device suspended\n");
1623 	return 0;
1624 }
1625 
1626 static int intel_runtime_resume(struct device *kdev)
1627 {
1628 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
1629 	struct intel_runtime_pm *rpm = &dev_priv->runtime_pm;
1630 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
1631 	struct pci_dev *root_pdev;
1632 	struct intel_gt *gt;
1633 	int ret, i;
1634 
1635 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_RUNTIME_PM(dev_priv)))
1636 		return -ENODEV;
1637 
1638 	drm_dbg(&dev_priv->drm, "Resuming device\n");
1639 
1640 	drm_WARN_ON_ONCE(&dev_priv->drm, atomic_read(&rpm->wakeref_count));
1641 	disable_rpm_wakeref_asserts(rpm);
1642 
1643 	intel_opregion_notify_adapter(dev_priv, PCI_D0);
1644 	rpm->suspended = false;
1645 
1646 	root_pdev = pcie_find_root_port(pdev);
1647 	if (root_pdev)
1648 		pci_d3cold_enable(root_pdev);
1649 
1650 	if (intel_uncore_unclaimed_mmio(&dev_priv->uncore))
1651 		drm_dbg(&dev_priv->drm,
1652 			"Unclaimed access during suspend, bios?\n");
1653 
1654 	intel_display_power_resume(dev_priv);
1655 
1656 	ret = vlv_resume_prepare(dev_priv, true);
1657 
1658 	for_each_gt(gt, dev_priv, i)
1659 		intel_uncore_runtime_resume(gt->uncore);
1660 
1661 	intel_runtime_pm_enable_interrupts(dev_priv);
1662 
1663 	/*
1664 	 * No point of rolling back things in case of an error, as the best
1665 	 * we can do is to hope that things will still work (and disable RPM).
1666 	 */
1667 	for_each_gt(gt, dev_priv, i)
1668 		intel_gt_runtime_resume(gt);
1669 
1670 	intel_pxp_runtime_resume(dev_priv->pxp);
1671 
1672 	/*
1673 	 * On VLV/CHV display interrupts are part of the display
1674 	 * power well, so hpd is reinitialized from there. For
1675 	 * everyone else do it here.
1676 	 */
1677 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
1678 		intel_hpd_init(dev_priv);
1679 		intel_hpd_poll_disable(dev_priv);
1680 	}
1681 
1682 	skl_watermark_ipc_update(dev_priv);
1683 
1684 	enable_rpm_wakeref_asserts(rpm);
1685 
1686 	if (ret)
1687 		drm_err(&dev_priv->drm,
1688 			"Runtime resume failed, disabling it (%d)\n", ret);
1689 	else
1690 		drm_dbg(&dev_priv->drm, "Device resumed\n");
1691 
1692 	return ret;
1693 }
1694 
1695 const struct dev_pm_ops i915_pm_ops = {
1696 	/*
1697 	 * S0ix (via system suspend) and S3 event handlers [PMSG_SUSPEND,
1698 	 * PMSG_RESUME]
1699 	 */
1700 	.prepare = i915_pm_prepare,
1701 	.suspend = i915_pm_suspend,
1702 	.suspend_late = i915_pm_suspend_late,
1703 	.resume_early = i915_pm_resume_early,
1704 	.resume = i915_pm_resume,
1705 	.complete = i915_pm_complete,
1706 
1707 	/*
1708 	 * S4 event handlers
1709 	 * @freeze, @freeze_late    : called (1) before creating the
1710 	 *                            hibernation image [PMSG_FREEZE] and
1711 	 *                            (2) after rebooting, before restoring
1712 	 *                            the image [PMSG_QUIESCE]
1713 	 * @thaw, @thaw_early       : called (1) after creating the hibernation
1714 	 *                            image, before writing it [PMSG_THAW]
1715 	 *                            and (2) after failing to create or
1716 	 *                            restore the image [PMSG_RECOVER]
1717 	 * @poweroff, @poweroff_late: called after writing the hibernation
1718 	 *                            image, before rebooting [PMSG_HIBERNATE]
1719 	 * @restore, @restore_early : called after rebooting and restoring the
1720 	 *                            hibernation image [PMSG_RESTORE]
1721 	 */
1722 	.freeze = i915_pm_freeze,
1723 	.freeze_late = i915_pm_freeze_late,
1724 	.thaw_early = i915_pm_thaw_early,
1725 	.thaw = i915_pm_thaw,
1726 	.poweroff = i915_pm_suspend,
1727 	.poweroff_late = i915_pm_poweroff_late,
1728 	.restore_early = i915_pm_restore_early,
1729 	.restore = i915_pm_restore,
1730 
1731 	/* S0ix (via runtime suspend) event handlers */
1732 	.runtime_suspend = intel_runtime_suspend,
1733 	.runtime_resume = intel_runtime_resume,
1734 };
1735 
1736 static const struct file_operations i915_driver_fops = {
1737 	.owner = THIS_MODULE,
1738 	.open = drm_open,
1739 	.release = drm_release_noglobal,
1740 	.unlocked_ioctl = drm_ioctl,
1741 	.mmap = i915_gem_mmap,
1742 	.poll = drm_poll,
1743 	.read = drm_read,
1744 	.compat_ioctl = i915_ioc32_compat_ioctl,
1745 	.llseek = noop_llseek,
1746 #ifdef CONFIG_PROC_FS
1747 	.show_fdinfo = drm_show_fdinfo,
1748 #endif
1749 };
1750 
1751 #endif /* __linux__ */
1752 
1753 static int
1754 i915_gem_reject_pin_ioctl(struct drm_device *dev, void *data,
1755 			  struct drm_file *file)
1756 {
1757 	return -ENODEV;
1758 }
1759 
1760 static const struct drm_ioctl_desc i915_ioctls[] = {
1761 	DRM_IOCTL_DEF_DRV(I915_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1762 	DRM_IOCTL_DEF_DRV(I915_FLUSH, drm_noop, DRM_AUTH),
1763 	DRM_IOCTL_DEF_DRV(I915_FLIP, drm_noop, DRM_AUTH),
1764 	DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, drm_noop, DRM_AUTH),
1765 	DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, drm_noop, DRM_AUTH),
1766 	DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, drm_noop, DRM_AUTH),
1767 	DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam_ioctl, DRM_RENDER_ALLOW),
1768 	DRM_IOCTL_DEF_DRV(I915_SETPARAM, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1769 	DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
1770 	DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
1771 	DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1772 	DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, drm_noop, DRM_AUTH),
1773 	DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP,  drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1774 	DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE,  drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1775 	DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE,  drm_noop, DRM_AUTH),
1776 	DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, drm_noop, DRM_AUTH),
1777 	DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1778 	DRM_IOCTL_DEF_DRV(I915_GEM_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1779 	DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, drm_invalid_op, DRM_AUTH),
1780 	DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2_WR, i915_gem_execbuffer2_ioctl, DRM_RENDER_ALLOW),
1781 	DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1782 	DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1783 	DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_RENDER_ALLOW),
1784 	DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_RENDER_ALLOW),
1785 	DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_RENDER_ALLOW),
1786 	DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_RENDER_ALLOW),
1787 	DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1788 	DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1789 	DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_RENDER_ALLOW),
1790 	DRM_IOCTL_DEF_DRV(I915_GEM_CREATE_EXT, i915_gem_create_ext_ioctl, DRM_RENDER_ALLOW),
1791 	DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW),
1792 	DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW),
1793 	DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW),
1794 	DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_OFFSET, i915_gem_mmap_offset_ioctl, DRM_RENDER_ALLOW),
1795 	DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW),
1796 	DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW),
1797 	DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling_ioctl, DRM_RENDER_ALLOW),
1798 	DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling_ioctl, DRM_RENDER_ALLOW),
1799 	DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_RENDER_ALLOW),
1800 	DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id_ioctl, 0),
1801 	DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_RENDER_ALLOW),
1802 	DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image_ioctl, DRM_MASTER),
1803 	DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs_ioctl, DRM_MASTER),
1804 	DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey_ioctl, DRM_MASTER),
1805 	DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, drm_noop, DRM_MASTER),
1806 	DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_RENDER_ALLOW),
1807 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE_EXT, i915_gem_context_create_ioctl, DRM_RENDER_ALLOW),
1808 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_RENDER_ALLOW),
1809 	DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_RENDER_ALLOW),
1810 	DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_gem_context_reset_stats_ioctl, DRM_RENDER_ALLOW),
1811 	DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
1812 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
1813 	DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
1814 	DRM_IOCTL_DEF_DRV(I915_PERF_OPEN, i915_perf_open_ioctl, DRM_RENDER_ALLOW),
1815 	DRM_IOCTL_DEF_DRV(I915_PERF_ADD_CONFIG, i915_perf_add_config_ioctl, DRM_RENDER_ALLOW),
1816 	DRM_IOCTL_DEF_DRV(I915_PERF_REMOVE_CONFIG, i915_perf_remove_config_ioctl, DRM_RENDER_ALLOW),
1817 	DRM_IOCTL_DEF_DRV(I915_QUERY, i915_query_ioctl, DRM_RENDER_ALLOW),
1818 	DRM_IOCTL_DEF_DRV(I915_GEM_VM_CREATE, i915_gem_vm_create_ioctl, DRM_RENDER_ALLOW),
1819 	DRM_IOCTL_DEF_DRV(I915_GEM_VM_DESTROY, i915_gem_vm_destroy_ioctl, DRM_RENDER_ALLOW),
1820 };
1821 
1822 /*
1823  * Interface history:
1824  *
1825  * 1.1: Original.
1826  * 1.2: Add Power Management
1827  * 1.3: Add vblank support
1828  * 1.4: Fix cmdbuffer path, add heap destroy
1829  * 1.5: Add vblank pipe configuration
1830  * 1.6: - New ioctl for scheduling buffer swaps on vertical blank
1831  *      - Support vertical blank on secondary display pipe
1832  */
1833 #define DRIVER_MAJOR		1
1834 #define DRIVER_MINOR		6
1835 #define DRIVER_PATCHLEVEL	0
1836 
1837 static const struct drm_driver i915_drm_driver = {
1838 	/* Don't use MTRRs here; the Xserver or userspace app should
1839 	 * deal with them for Intel hardware.
1840 	 */
1841 	.driver_features =
1842 	    DRIVER_GEM |
1843 	    DRIVER_RENDER | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_SYNCOBJ |
1844 	    DRIVER_SYNCOBJ_TIMELINE,
1845 	.release = i915_driver_release,
1846 	.open = i915_driver_open,
1847 	.lastclose = i915_driver_lastclose,
1848 	.postclose = i915_driver_postclose,
1849 	.show_fdinfo = PTR_IF(IS_ENABLED(CONFIG_PROC_FS), i915_drm_client_fdinfo),
1850 
1851 	.gem_prime_import = i915_gem_prime_import,
1852 
1853 	.dumb_create = i915_gem_dumb_create,
1854 	.dumb_map_offset = i915_gem_dumb_mmap_offset,
1855 
1856 #ifdef __OpenBSD__
1857 	.mmap = i915_gem_mmap,
1858 	.gem_fault = i915_gem_fault,
1859 #endif
1860 
1861 	.ioctls = i915_ioctls,
1862 	.num_ioctls = ARRAY_SIZE(i915_ioctls),
1863 #ifdef __linux__
1864 	.fops = &i915_driver_fops,
1865 #endif
1866 	.name = DRIVER_NAME,
1867 	.desc = DRIVER_DESC,
1868 	.date = DRIVER_DATE,
1869 	.major = DRIVER_MAJOR,
1870 	.minor = DRIVER_MINOR,
1871 	.patchlevel = DRIVER_PATCHLEVEL,
1872 };
1873 
1874 #ifdef __OpenBSD__
1875 
1876 #include <drm/drm_legacy.h> /* for agp */
1877 #include <drm/drm_utils.h>
1878 #include <drm/drm_fb_helper.h>
1879 
1880 #ifdef __amd64__
1881 #include "efifb.h"
1882 #include <machine/biosvar.h>
1883 #endif
1884 
1885 #if NEFIFB > 0
1886 #include <machine/efifbvar.h>
1887 #endif
1888 
1889 #include "intagp.h"
1890 
1891 /*
1892  * some functions are only called once on init regardless of how many times
1893  * inteldrm attaches in linux this is handled via module_init()/module_exit()
1894  */
1895 int inteldrm_refcnt;
1896 
1897 #if NINTAGP > 0
1898 int	intagpsubmatch(struct device *, void *, void *);
1899 int	intagp_print(void *, const char *);
1900 
1901 int
1902 intagpsubmatch(struct device *parent, void *match, void *aux)
1903 {
1904 	extern struct cfdriver intagp_cd;
1905 	struct cfdata *cf = match;
1906 
1907 	/* only allow intagp to attach */
1908 	if (cf->cf_driver == &intagp_cd)
1909 		return ((*cf->cf_attach->ca_match)(parent, match, aux));
1910 	return (0);
1911 }
1912 
1913 int
1914 intagp_print(void *vaa, const char *pnp)
1915 {
1916 	if (pnp)
1917 		printf("intagp at %s", pnp);
1918 	return (UNCONF);
1919 }
1920 #endif
1921 
1922 int	inteldrm_wsioctl(void *, u_long, caddr_t, int, struct proc *);
1923 paddr_t	inteldrm_wsmmap(void *, off_t, int);
1924 int	inteldrm_alloc_screen(void *, const struct wsscreen_descr *,
1925 	    void **, int *, int *, uint32_t *);
1926 void	inteldrm_free_screen(void *, void *);
1927 int	inteldrm_show_screen(void *, void *, int,
1928 	    void (*)(void *, int, int), void *);
1929 void	inteldrm_doswitch(void *);
1930 void	inteldrm_enter_ddb(void *, void *);
1931 int	inteldrm_load_font(void *, void *, struct wsdisplay_font *);
1932 int	inteldrm_list_font(void *, struct wsdisplay_font *);
1933 int	inteldrm_getchar(void *, int, int, struct wsdisplay_charcell *);
1934 void	inteldrm_burner(void *, u_int, u_int);
1935 void	inteldrm_burner_cb(void *);
1936 void	inteldrm_scrollback(void *, void *, int lines);
1937 extern const struct pci_device_id pciidlist[];
1938 
1939 struct wsscreen_descr inteldrm_stdscreen = {
1940 	"std",
1941 	0, 0,
1942 	0,
1943 	0, 0,
1944 	WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
1945 	WSSCREEN_REVERSE | WSSCREEN_WSCOLORS
1946 };
1947 
1948 const struct wsscreen_descr *inteldrm_scrlist[] = {
1949 	&inteldrm_stdscreen,
1950 };
1951 
1952 struct wsscreen_list inteldrm_screenlist = {
1953 	nitems(inteldrm_scrlist), inteldrm_scrlist
1954 };
1955 
1956 struct wsdisplay_accessops inteldrm_accessops = {
1957 	.ioctl = inteldrm_wsioctl,
1958 	.mmap = inteldrm_wsmmap,
1959 	.alloc_screen = inteldrm_alloc_screen,
1960 	.free_screen = inteldrm_free_screen,
1961 	.show_screen = inteldrm_show_screen,
1962 	.enter_ddb = inteldrm_enter_ddb,
1963 	.getchar = inteldrm_getchar,
1964 	.load_font = inteldrm_load_font,
1965 	.list_font = inteldrm_list_font,
1966 	.scrollback = inteldrm_scrollback,
1967 	.burn_screen = inteldrm_burner
1968 };
1969 
1970 int
1971 inteldrm_wsioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
1972 {
1973 	struct inteldrm_softc *dev_priv = v;
1974 	struct backlight_device *bd = dev_priv->backlight;
1975 	struct rasops_info *ri = &dev_priv->ro;
1976 	struct wsdisplay_fbinfo *wdf;
1977 	struct wsdisplay_param *dp = (struct wsdisplay_param *)data;
1978 
1979 	switch (cmd) {
1980 	case WSDISPLAYIO_GTYPE:
1981 		*(u_int *)data = WSDISPLAY_TYPE_INTELDRM;
1982 		return 0;
1983 	case WSDISPLAYIO_GINFO:
1984 		wdf = (struct wsdisplay_fbinfo *)data;
1985 		wdf->width = ri->ri_width;
1986 		wdf->height = ri->ri_height;
1987 		wdf->depth = ri->ri_depth;
1988 		wdf->stride = ri->ri_stride;
1989 		wdf->offset = 0;
1990 		wdf->cmsize = 0;
1991 		return 0;
1992 	case WSDISPLAYIO_GETPARAM:
1993 		if (ws_get_param && ws_get_param(dp) == 0)
1994 			return 0;
1995 
1996 		if (bd == NULL)
1997 			return -1;
1998 
1999 		switch (dp->param) {
2000 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
2001 			dp->min = 0;
2002 			dp->max = bd->props.max_brightness;
2003 			dp->curval = bd->ops->get_brightness(bd);
2004 			return (dp->max > dp->min) ? 0 : -1;
2005 		}
2006 		break;
2007 	case WSDISPLAYIO_SETPARAM:
2008 		if (ws_set_param && ws_set_param(dp) == 0)
2009 			return 0;
2010 
2011 		if (bd == NULL || dp->curval > bd->props.max_brightness)
2012 			return -1;
2013 
2014 		switch (dp->param) {
2015 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
2016 			bd->props.brightness = dp->curval;
2017 			backlight_update_status(bd);
2018 			knote_locked(&dev_priv->drm.note, NOTE_CHANGE);
2019 			return 0;
2020 		}
2021 		break;
2022 	case WSDISPLAYIO_SVIDEO:
2023 	case WSDISPLAYIO_GVIDEO:
2024 		return 0;
2025 	}
2026 
2027 	return (-1);
2028 }
2029 
2030 paddr_t
2031 inteldrm_wsmmap(void *v, off_t off, int prot)
2032 {
2033 	return (-1);
2034 }
2035 
2036 int
2037 inteldrm_alloc_screen(void *v, const struct wsscreen_descr *type,
2038     void **cookiep, int *curxp, int *curyp, uint32_t *attrp)
2039 {
2040 	struct inteldrm_softc *dev_priv = v;
2041 	struct rasops_info *ri = &dev_priv->ro;
2042 
2043 	return rasops_alloc_screen(ri, cookiep, curxp, curyp, attrp);
2044 }
2045 
2046 void
2047 inteldrm_free_screen(void *v, void *cookie)
2048 {
2049 	struct inteldrm_softc *dev_priv = v;
2050 	struct rasops_info *ri = &dev_priv->ro;
2051 
2052 	return rasops_free_screen(ri, cookie);
2053 }
2054 
2055 int
2056 inteldrm_show_screen(void *v, void *cookie, int waitok,
2057     void (*cb)(void *, int, int), void *cbarg)
2058 {
2059 	struct inteldrm_softc *dev_priv = v;
2060 	struct rasops_info *ri = &dev_priv->ro;
2061 
2062 	if (cookie == ri->ri_active)
2063 		return (0);
2064 
2065 	dev_priv->switchcb = cb;
2066 	dev_priv->switchcbarg = cbarg;
2067 	dev_priv->switchcookie = cookie;
2068 	if (cb) {
2069 		task_add(systq, &dev_priv->switchtask);
2070 		return (EAGAIN);
2071 	}
2072 
2073 	inteldrm_doswitch(v);
2074 
2075 	return (0);
2076 }
2077 
2078 void
2079 inteldrm_doswitch(void *v)
2080 {
2081 	struct inteldrm_softc *dev_priv = v;
2082 	struct rasops_info *ri = &dev_priv->ro;
2083 	struct drm_device *dev = &dev_priv->drm;
2084 
2085 	rasops_show_screen(ri, dev_priv->switchcookie, 0, NULL, NULL);
2086 	intel_fbdev_restore_mode(dev_priv);
2087 
2088 	if (dev_priv->switchcb)
2089 		(*dev_priv->switchcb)(dev_priv->switchcbarg, 0, 0);
2090 }
2091 
2092 void
2093 inteldrm_enter_ddb(void *v, void *cookie)
2094 {
2095 	struct inteldrm_softc *dev_priv = v;
2096 	struct rasops_info *ri = &dev_priv->ro;
2097 	struct drm_device *dev = &dev_priv->drm;
2098 
2099 	if (cookie == ri->ri_active)
2100 		return;
2101 
2102 	rasops_show_screen(ri, cookie, 0, NULL, NULL);
2103 	intel_fbdev_restore_mode(dev_priv);
2104 }
2105 
2106 int
2107 inteldrm_getchar(void *v, int row, int col, struct wsdisplay_charcell *cell)
2108 {
2109 	struct inteldrm_softc *dev_priv = v;
2110 	struct rasops_info *ri = &dev_priv->ro;
2111 
2112 	return rasops_getchar(ri, row, col, cell);
2113 }
2114 
2115 int
2116 inteldrm_load_font(void *v, void *cookie, struct wsdisplay_font *font)
2117 {
2118 	struct inteldrm_softc *dev_priv = v;
2119 	struct rasops_info *ri = &dev_priv->ro;
2120 
2121 	return rasops_load_font(ri, cookie, font);
2122 }
2123 
2124 int
2125 inteldrm_list_font(void *v, struct wsdisplay_font *font)
2126 {
2127 	struct inteldrm_softc *dev_priv = v;
2128 	struct rasops_info *ri = &dev_priv->ro;
2129 
2130 	return rasops_list_font(ri, font);
2131 }
2132 
2133 void
2134 inteldrm_burner(void *v, u_int on, u_int flags)
2135 {
2136 	struct inteldrm_softc *dev_priv = v;
2137 
2138 	task_del(systq, &dev_priv->burner_task);
2139 
2140 	if (on)
2141 		dev_priv->burner_fblank = FB_BLANK_UNBLANK;
2142 	else {
2143 		if (flags & WSDISPLAY_BURN_VBLANK)
2144 			dev_priv->burner_fblank = FB_BLANK_VSYNC_SUSPEND;
2145 		else
2146 			dev_priv->burner_fblank = FB_BLANK_NORMAL;
2147 	}
2148 
2149 	/*
2150 	 * Setting the DPMS mode may sleep while waiting for the display
2151 	 * to come back on so hand things off to a taskq.
2152 	 */
2153 	task_add(systq, &dev_priv->burner_task);
2154 }
2155 
2156 void
2157 inteldrm_burner_cb(void *arg1)
2158 {
2159 	struct inteldrm_softc *dev_priv = arg1;
2160 	struct drm_device *dev = &dev_priv->drm;
2161 	struct drm_fb_helper *helper = dev->fb_helper;
2162 
2163 	drm_fb_helper_blank(dev_priv->burner_fblank, helper->info);
2164 }
2165 
2166 int
2167 inteldrm_backlight_update_status(struct backlight_device *bd)
2168 {
2169 	struct wsdisplay_param dp;
2170 
2171 	dp.param = WSDISPLAYIO_PARAM_BRIGHTNESS;
2172 	dp.curval = bd->props.brightness;
2173 	ws_set_param(&dp);
2174 	return 0;
2175 }
2176 
2177 int
2178 inteldrm_backlight_get_brightness(struct backlight_device *bd)
2179 {
2180 	struct wsdisplay_param dp;
2181 
2182 	dp.param = WSDISPLAYIO_PARAM_BRIGHTNESS;
2183 	ws_get_param(&dp);
2184 	return dp.curval;
2185 }
2186 
2187 const struct backlight_ops inteldrm_backlight_ops = {
2188 	.update_status = inteldrm_backlight_update_status,
2189 	.get_brightness = inteldrm_backlight_get_brightness
2190 };
2191 
2192 void
2193 inteldrm_scrollback(void *v, void *cookie, int lines)
2194 {
2195 	struct inteldrm_softc *dev_priv = v;
2196 	struct rasops_info *ri = &dev_priv->ro;
2197 
2198 	rasops_scrollback(ri, cookie, lines);
2199 }
2200 
2201 int	inteldrm_match(struct device *, void *, void *);
2202 void	inteldrm_attach(struct device *, struct device *, void *);
2203 int	inteldrm_detach(struct device *, int);
2204 int	inteldrm_activate(struct device *, int);
2205 void	inteldrm_attachhook(struct device *);
2206 
2207 const struct cfattach inteldrm_ca = {
2208 	sizeof(struct inteldrm_softc), inteldrm_match, inteldrm_attach,
2209 	inteldrm_detach, inteldrm_activate
2210 };
2211 
2212 struct cfdriver inteldrm_cd = {
2213 	0, "inteldrm", DV_DULL
2214 };
2215 
2216 int	inteldrm_intr(void *);
2217 
2218 /*
2219  * Set if the mountroot hook has a fatal error.
2220  */
2221 int	inteldrm_fatal_error;
2222 
2223 int
2224 inteldrm_match(struct device *parent, void *match, void *aux)
2225 {
2226 	struct pci_attach_args *pa = aux;
2227 	const struct pci_device_id *id;
2228 	struct intel_device_info *info;
2229 
2230 	if (inteldrm_fatal_error)
2231 		return 0;
2232 
2233 	id = drm_find_description(PCI_VENDOR(pa->pa_id),
2234 	    PCI_PRODUCT(pa->pa_id), pciidlist);
2235 	if (id != NULL) {
2236 		info = (struct intel_device_info *)id->driver_data;
2237 		if (info->require_force_probe == 0 &&
2238 		    pa->pa_function == 0)
2239 			return 20;
2240 	}
2241 
2242 	return 0;
2243 }
2244 
2245 int drm_gem_init(struct drm_device *);
2246 void intel_init_stolen_res(struct inteldrm_softc *);
2247 
2248 void
2249 inteldrm_attach(struct device *parent, struct device *self, void *aux)
2250 {
2251 	struct inteldrm_softc *dev_priv = (struct inteldrm_softc *)self;
2252 	struct drm_device *dev;
2253 	struct pci_attach_args *pa = aux;
2254 	const struct pci_device_id *id;
2255 	struct intel_device_info *info, *device_info;
2256 	struct intel_runtime_info *runtime;
2257 	extern int vga_console_attached;
2258 	int mmio_bar, mmio_size, mmio_type;
2259 	int ret;
2260 
2261 	dev_priv->pa = pa;
2262 	dev_priv->pc = pa->pa_pc;
2263 	dev_priv->tag = pa->pa_tag;
2264 	dev_priv->iot = pa->pa_iot;
2265 	dev_priv->dmat = pa->pa_dmat;
2266 	dev_priv->bst = pa->pa_memt;
2267 	dev_priv->memex = pa->pa_memex;
2268 	dev_priv->vga_regs = &dev_priv->bar;
2269 
2270 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
2271 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA &&
2272 	    (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG)
2273 	    & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
2274 	    == (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE)) {
2275 		dev_priv->primary = 1;
2276 		dev_priv->console = vga_is_console(pa->pa_iot, -1);;
2277 		vga_console_attached = 1;
2278 	}
2279 
2280 #if NEFIFB > 0
2281 	if (efifb_is_primary(pa)) {
2282 		dev_priv->primary = 1;
2283 		dev_priv->console = efifb_is_console(pa);
2284 		efifb_detach();
2285 	}
2286 #endif
2287 
2288 	printf("\n");
2289 
2290 	dev = drm_attach_pci(&i915_drm_driver, pa, 0, dev_priv->primary,
2291 	    self, &dev_priv->drm);
2292 	if (dev == NULL) {
2293 		printf("%s: drm attach failed\n", dev_priv->sc_dev.dv_xname);
2294 		return;
2295 	}
2296 
2297 	id = drm_find_description(PCI_VENDOR(pa->pa_id),
2298 	    PCI_PRODUCT(pa->pa_id), pciidlist);
2299 	dev_priv->id = id;
2300 	info = (struct intel_device_info *)id->driver_data;
2301 
2302 	/* Device parameters start as a copy of module parameters. */
2303 	i915_params_copy(&dev_priv->params, &i915_modparams);
2304 	dev_priv->params.enable_guc = 0;
2305 	dev_priv->params.request_timeout_ms = 0;
2306 	dev_priv->params.enable_psr = 0;
2307 
2308 	/* Set up device info and initial runtime info. */
2309 	intel_device_info_driver_create(dev_priv, dev->pdev->device, info);
2310 
2311 	mmio_bar = (GRAPHICS_VER(dev_priv) == 2) ? 0x14 : 0x10;
2312 
2313 	/* from intel_uncore_setup_mmio() */
2314 
2315 	/*
2316 	 * Before gen4, the registers and the GTT are behind different BARs.
2317 	 * However, from gen4 onwards, the registers and the GTT are shared
2318 	 * in the same BAR, so we want to restrict this ioremap from
2319 	 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
2320 	 * the register BAR remains the same size for all the earlier
2321 	 * generations up to Ironlake.
2322 	 * For dgfx chips register range is expanded to 4MB, and this larger
2323 	 * range is also used for integrated gpus beginning with Meteor Lake.
2324 	 */
2325 	if (IS_DGFX(dev_priv) || GRAPHICS_VER_FULL(dev_priv) >= IP_VER(12, 70))
2326 		mmio_size = 4 * 1024 * 1024;
2327 	else if (GRAPHICS_VER(dev_priv) >= 5)
2328 		mmio_size = 2 * 1024 * 1024;
2329 	else
2330 		mmio_size = 512 * 1024;
2331 
2332 	mmio_type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, mmio_bar);
2333 	if (pci_mapreg_map(pa, mmio_bar, mmio_type, BUS_SPACE_MAP_LINEAR,
2334 	    &dev_priv->vga_regs->bst, &dev_priv->vga_regs->bsh,
2335 	    &dev_priv->vga_regs->base, &dev_priv->vga_regs->size, mmio_size)) {
2336 		printf("%s: can't map registers\n",
2337 		    dev_priv->sc_dev.dv_xname);
2338 		return;
2339 	}
2340 	dev_priv->uncore.regs = bus_space_vaddr(dev_priv->vga_regs->bst,
2341 	     dev_priv->vga_regs->bsh);
2342 	if (dev_priv->uncore.regs == NULL) {
2343 		printf("%s: bus_space_vaddr registers failed\n",
2344 		    dev_priv->sc_dev.dv_xname);
2345 		return;
2346 	}
2347 
2348 #if NINTAGP > 0
2349 	if (GRAPHICS_VER(dev_priv) <= 5) {
2350 		config_found_sm(self, aux, intagp_print, intagpsubmatch);
2351 		dev->agp = drm_legacy_agp_init(dev);
2352 		if (dev->agp) {
2353 			if (drm_mtrr_add(dev->agp->info.ai_aperture_base,
2354 			    dev->agp->info.ai_aperture_size, DRM_MTRR_WC) == 0)
2355 				dev->agp->mtrr = 1;
2356 		}
2357 	}
2358 #endif
2359 
2360 	if (GRAPHICS_VER(dev_priv) < 5)
2361 		pa->pa_flags &= ~PCI_FLAGS_MSI_ENABLED;
2362 
2363 	if (pci_intr_map_msi(pa, &dev_priv->ih) != 0 &&
2364 	    pci_intr_map(pa, &dev_priv->ih) != 0) {
2365 		printf("%s: couldn't map interrupt\n",
2366 		    dev_priv->sc_dev.dv_xname);
2367 		return;
2368 	}
2369 
2370 	printf("%s: %s, %s, gen %d\n", dev_priv->sc_dev.dv_xname,
2371 	    pci_intr_string(dev_priv->pc, dev_priv->ih),
2372 	    intel_platform_name(INTEL_INFO(dev_priv)->platform),
2373 	    GRAPHICS_VER(dev_priv));
2374 
2375 	dev_priv->irqh = pci_intr_establish(dev_priv->pc, dev_priv->ih,
2376 	    IPL_TTY, inteldrm_intr, dev_priv, dev_priv->sc_dev.dv_xname);
2377 	if (dev_priv->irqh == NULL) {
2378 		printf("%s: couldn't establish interrupt\n",
2379 		    dev_priv->sc_dev.dv_xname);
2380 		return;
2381 	}
2382 	dev->pdev->irq = -1;
2383 	intel_gmch_bridge_setup(dev_priv);
2384 	intel_init_stolen_res(dev_priv);
2385 
2386 	config_mountroot(self, inteldrm_attachhook);
2387 }
2388 
2389 void
2390 inteldrm_forcedetach(struct inteldrm_softc *dev_priv)
2391 {
2392 	struct pci_softc *psc = (struct pci_softc *)dev_priv->sc_dev.dv_parent;
2393 	pcitag_t tag = dev_priv->tag;
2394 	extern int vga_console_attached;
2395 
2396 	if (dev_priv->primary) {
2397 		vga_console_attached = 0;
2398 #if NEFIFB > 0
2399 		efifb_reattach();
2400 #endif
2401 	}
2402 
2403 #ifdef notyet
2404 	config_detach(&dev_priv->sc_dev, 0);
2405 	pci_probe_device(psc, tag, NULL, NULL);
2406 #endif
2407 }
2408 
2409 extern int __init i915_init(void);
2410 
2411 void
2412 inteldrm_attachhook(struct device *self)
2413 {
2414 	struct inteldrm_softc *dev_priv = (struct inteldrm_softc *)self;
2415 	struct rasops_info *ri = &dev_priv->ro;
2416 	struct wsemuldisplaydev_attach_args aa;
2417 	const struct pci_device_id *id = dev_priv->id;
2418 	struct drm_device *dev = &dev_priv->drm;
2419 	int orientation_quirk;
2420 
2421 	if (inteldrm_refcnt == 0) {
2422 		i915_init();
2423 	}
2424 	inteldrm_refcnt++;
2425 
2426 	if (i915_driver_probe(dev_priv, id))
2427 		goto fail;
2428 
2429 	if (ri->ri_bits == NULL)
2430 		goto fail;
2431 
2432 	printf("%s: %dx%d, %dbpp\n", dev_priv->sc_dev.dv_xname,
2433 	    ri->ri_width, ri->ri_height, ri->ri_depth);
2434 
2435 	ri->ri_flg = RI_CENTER | RI_WRONLY | RI_VCONS | RI_CLEAR;
2436 
2437 	orientation_quirk = drm_get_panel_orientation_quirk(ri->ri_width,
2438 	    ri->ri_height);
2439 	if (orientation_quirk == DRM_MODE_PANEL_ORIENTATION_LEFT_UP)
2440 		ri->ri_flg |= RI_ROTATE_CCW;
2441 	else if (orientation_quirk == DRM_MODE_PANEL_ORIENTATION_RIGHT_UP)
2442 		ri->ri_flg |= RI_ROTATE_CW;
2443 
2444 	ri->ri_hw = dev_priv;
2445 	rasops_init(ri, 160, 160);
2446 
2447 	task_set(&dev_priv->switchtask, inteldrm_doswitch, dev_priv);
2448 	task_set(&dev_priv->burner_task, inteldrm_burner_cb, dev_priv);
2449 
2450 	inteldrm_stdscreen.capabilities = ri->ri_caps;
2451 	inteldrm_stdscreen.nrows = ri->ri_rows;
2452 	inteldrm_stdscreen.ncols = ri->ri_cols;
2453 	inteldrm_stdscreen.textops = &ri->ri_ops;
2454 	inteldrm_stdscreen.fontwidth = ri->ri_font->fontwidth;
2455 	inteldrm_stdscreen.fontheight = ri->ri_font->fontheight;
2456 
2457 	aa.console = dev_priv->console;
2458 	aa.primary = dev_priv->primary;
2459 	aa.scrdata = &inteldrm_screenlist;
2460 	aa.accessops = &inteldrm_accessops;
2461 	aa.accesscookie = dev_priv;
2462 	aa.defaultscreens = 0;
2463 
2464 	if (dev_priv->console) {
2465 		uint32_t defattr;
2466 
2467 		/*
2468 		 * Clear the entire screen if we're doing rotation to
2469 		 * make sure no unrotated content survives.
2470 		 */
2471 		if (ri->ri_flg & (RI_ROTATE_CW | RI_ROTATE_CCW))
2472 			memset(ri->ri_bits, 0, ri->ri_height * ri->ri_stride);
2473 
2474 		ri->ri_ops.pack_attr(ri->ri_active, 0, 0, 0, &defattr);
2475 		wsdisplay_cnattach(&inteldrm_stdscreen, ri->ri_active,
2476 		    0, 0, defattr);
2477 	}
2478 
2479 	config_found_sm(self, &aa, wsemuldisplaydevprint,
2480 	    wsemuldisplaydevsubmatch);
2481 	return;
2482 
2483 fail:
2484 	inteldrm_fatal_error = 1;
2485 	inteldrm_forcedetach(dev_priv);
2486 }
2487 
2488 int
2489 inteldrm_detach(struct device *self, int flags)
2490 {
2491 	return 0;
2492 }
2493 
2494 int
2495 inteldrm_activate(struct device *self, int act)
2496 {
2497 	struct inteldrm_softc *dev_priv = (struct inteldrm_softc *)self;
2498 	struct drm_device *dev = &dev_priv->drm;
2499 	int rv = 0;
2500 
2501 	if (dev->dev == NULL || inteldrm_fatal_error)
2502 		return (0);
2503 
2504 	/*
2505 	 * On hibernate resume activate is called before inteldrm_attachhook().
2506 	 * Do not try to call i915_drm_suspend() when
2507 	 * i915_load_modeset_init()/i915_gem_init() have not been called.
2508 	 */
2509 	if (dev_priv->display.wq.modeset == NULL)
2510 		return 0;
2511 
2512 	switch (act) {
2513 	case DVACT_QUIESCE:
2514 		rv = config_suspend(dev->dev, act);
2515 		i915_drm_prepare(dev);
2516 		i915_drm_suspend(dev);
2517 		i915_drm_suspend_late(dev, false);
2518 		break;
2519 	case DVACT_SUSPEND:
2520 		if (dev->agp)
2521 			config_suspend(dev->agp->agpdev->sc_chipc, act);
2522 		break;
2523 	case DVACT_RESUME:
2524 		if (dev->agp)
2525 			config_suspend(dev->agp->agpdev->sc_chipc, act);
2526 		break;
2527 	case DVACT_WAKEUP:
2528 		i915_drm_resume_early(dev);
2529 		i915_drm_resume(dev);
2530 		intel_fbdev_restore_mode(dev_priv);
2531 		rv = config_suspend(dev->dev, act);
2532 		break;
2533 	}
2534 
2535 	return (rv);
2536 }
2537 
2538 void
2539 inteldrm_native_backlight(struct inteldrm_softc *dev_priv)
2540 {
2541 	struct drm_device *dev = &dev_priv->drm;
2542 	struct drm_connector_list_iter conn_iter;
2543 	struct drm_connector *connector;
2544 
2545 	drm_connector_list_iter_begin(dev, &conn_iter);
2546 	drm_for_each_connector_iter(connector, &conn_iter) {
2547 		struct intel_connector *intel_connector;
2548 		struct intel_panel *panel;
2549 		struct backlight_device *bd;
2550 
2551 		if (connector->registration_state != DRM_CONNECTOR_REGISTERED)
2552 			continue;
2553 
2554 		intel_connector = to_intel_connector(connector);
2555 		panel = &intel_connector->panel;
2556 		bd = panel->backlight.device;
2557 
2558 		if (!panel->backlight.present || bd == NULL)
2559 			continue;
2560 
2561 		dev->registered = false;
2562 		connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
2563 
2564 		connector->backlight_device = bd;
2565 		connector->backlight_property = drm_property_create_range(dev,
2566 		    0, "Backlight", 0, bd->props.max_brightness);
2567 		drm_object_attach_property(&connector->base,
2568 		    connector->backlight_property, bd->props.brightness);
2569 
2570 		connector->registration_state = DRM_CONNECTOR_REGISTERED;
2571 		dev->registered = true;
2572 
2573 		/*
2574 		 * Use backlight from the first connector that has one
2575 		 * for wscons(4).
2576 		 */
2577 		if (dev_priv->backlight == NULL)
2578 			dev_priv->backlight = bd;
2579 	}
2580 	drm_connector_list_iter_end(&conn_iter);
2581 }
2582 
2583 void
2584 inteldrm_firmware_backlight(struct inteldrm_softc *dev_priv,
2585     struct wsdisplay_param *dp)
2586 {
2587 	struct drm_device *dev = &dev_priv->drm;
2588 	struct drm_connector_list_iter conn_iter;
2589 	struct drm_connector *connector;
2590 	struct backlight_properties props;
2591 	struct backlight_device *bd;
2592 
2593 	memset(&props, 0, sizeof(props));
2594 	props.type = BACKLIGHT_FIRMWARE;
2595 	props.brightness = dp->curval;
2596 	bd = backlight_device_register(dev->dev->dv_xname, NULL, NULL,
2597 	    &inteldrm_backlight_ops, &props);
2598 
2599 	drm_connector_list_iter_begin(dev, &conn_iter);
2600 	drm_for_each_connector_iter(connector, &conn_iter) {
2601 		if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS &&
2602 		    connector->connector_type != DRM_MODE_CONNECTOR_eDP &&
2603 		    connector->connector_type != DRM_MODE_CONNECTOR_DSI)
2604 			continue;
2605 
2606 		if (connector->registration_state != DRM_CONNECTOR_REGISTERED)
2607 			continue;
2608 
2609 		dev->registered = false;
2610 		connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
2611 
2612 		connector->backlight_device = bd;
2613 		connector->backlight_property = drm_property_create_range(dev,
2614 		    0, "Backlight", dp->min, dp->max);
2615 		drm_object_attach_property(&connector->base,
2616 		    connector->backlight_property, dp->curval);
2617 
2618 		connector->registration_state = DRM_CONNECTOR_REGISTERED;
2619 		dev->registered = true;
2620 	}
2621 	drm_connector_list_iter_end(&conn_iter);
2622 }
2623 
2624 void
2625 inteldrm_init_backlight(struct inteldrm_softc *dev_priv)
2626 {
2627 	struct drm_device *dev = &dev_priv->drm;
2628 	struct wsdisplay_param dp;
2629 
2630 	dp.param = WSDISPLAYIO_PARAM_BRIGHTNESS;
2631 	if (ws_get_param && ws_get_param(&dp) == 0)
2632 		inteldrm_firmware_backlight(dev_priv, &dp);
2633 	else
2634 		inteldrm_native_backlight(dev_priv);
2635 }
2636 
2637 int
2638 inteldrm_intr(void *arg)
2639 {
2640 	struct inteldrm_softc *dev_priv = arg;
2641 
2642 	if (dev_priv->irq_handler)
2643 		return dev_priv->irq_handler(0, dev_priv);
2644 
2645 	return 0;
2646 }
2647 
2648 #endif
2649