xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/i915/intel_gvt.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: intel_gvt.c,v 1.2 2021/12/18 23:45:28 riastradh Exp $	*/
2 
3 /*
4  * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: intel_gvt.c,v 1.2 2021/12/18 23:45:28 riastradh Exp $");
28 
29 #include "i915_drv.h"
30 #include "intel_gvt.h"
31 
32 /**
33  * DOC: Intel GVT-g host support
34  *
35  * Intel GVT-g is a graphics virtualization technology which shares the
36  * GPU among multiple virtual machines on a time-sharing basis. Each
37  * virtual machine is presented a virtual GPU (vGPU), which has equivalent
38  * features as the underlying physical GPU (pGPU), so i915 driver can run
39  * seamlessly in a virtual machine.
40  *
41  * To virtualize GPU resources GVT-g driver depends on hypervisor technology
42  * e.g KVM/VFIO/mdev, Xen, etc. to provide resource access trapping capability
43  * and be virtualized within GVT-g device module. More architectural design
44  * doc is available on https://01.org/group/2230/documentation-list.
45  */
46 
is_supported_device(struct drm_i915_private * dev_priv)47 static bool is_supported_device(struct drm_i915_private *dev_priv)
48 {
49 	if (IS_BROADWELL(dev_priv))
50 		return true;
51 	if (IS_SKYLAKE(dev_priv))
52 		return true;
53 	if (IS_KABYLAKE(dev_priv))
54 		return true;
55 	if (IS_BROXTON(dev_priv))
56 		return true;
57 	if (IS_COFFEELAKE(dev_priv))
58 		return true;
59 
60 	return false;
61 }
62 
63 /**
64  * intel_gvt_sanitize_options - sanitize GVT related options
65  * @dev_priv: drm i915 private data
66  *
67  * This function is called at the i915 options sanitize stage.
68  */
intel_gvt_sanitize_options(struct drm_i915_private * dev_priv)69 void intel_gvt_sanitize_options(struct drm_i915_private *dev_priv)
70 {
71 	if (!i915_modparams.enable_gvt)
72 		return;
73 
74 	if (intel_vgpu_active(dev_priv)) {
75 		DRM_INFO("GVT-g is disabled for guest\n");
76 		goto bail;
77 	}
78 
79 	if (!is_supported_device(dev_priv)) {
80 		DRM_INFO("Unsupported device. GVT-g is disabled\n");
81 		goto bail;
82 	}
83 
84 	return;
85 bail:
86 	i915_modparams.enable_gvt = 0;
87 }
88 
89 /**
90  * intel_gvt_init - initialize GVT components
91  * @dev_priv: drm i915 private data
92  *
93  * This function is called at the initialization stage to create a GVT device.
94  *
95  * Returns:
96  * Zero on success, negative error code if failed.
97  *
98  */
intel_gvt_init(struct drm_i915_private * dev_priv)99 int intel_gvt_init(struct drm_i915_private *dev_priv)
100 {
101 	int ret;
102 
103 	if (i915_inject_probe_failure(dev_priv))
104 		return -ENODEV;
105 
106 	if (!i915_modparams.enable_gvt) {
107 		DRM_DEBUG_DRIVER("GVT-g is disabled by kernel params\n");
108 		return 0;
109 	}
110 
111 	if (USES_GUC_SUBMISSION(dev_priv)) {
112 		DRM_ERROR("i915 GVT-g loading failed due to Graphics virtualization is not yet supported with GuC submission\n");
113 		return -EIO;
114 	}
115 
116 	ret = intel_gvt_init_device(dev_priv);
117 	if (ret) {
118 		DRM_DEBUG_DRIVER("Fail to init GVT device\n");
119 		goto bail;
120 	}
121 
122 	return 0;
123 
124 bail:
125 	i915_modparams.enable_gvt = 0;
126 	return 0;
127 }
128 
129 /**
130  * intel_gvt_driver_remove - cleanup GVT components when i915 driver is
131  *			     unbinding
132  * @dev_priv: drm i915 private *
133  *
134  * This function is called at the i915 driver unloading stage, to shutdown
135  * GVT components and release the related resources.
136  */
intel_gvt_driver_remove(struct drm_i915_private * dev_priv)137 void intel_gvt_driver_remove(struct drm_i915_private *dev_priv)
138 {
139 	if (!intel_gvt_active(dev_priv))
140 		return;
141 
142 	intel_gvt_clean_device(dev_priv);
143 }
144