2 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Kevin Tian <kevin.tian@intel.com>
25 * Eddie Dong <eddie.dong@intel.com>
28 * Niu Bing <bing.niu@intel.com>
29 * Zhi Wang <zhi.a.wang@intel.com>
33 #include <linux/types.h>
35 #include <linux/kthread.h>
40 struct intel_gvt_host intel_gvt_host
;
42 static const char * const supported_hypervisors
[] = {
43 [INTEL_GVT_HYPERVISOR_XEN
] = "XEN",
44 [INTEL_GVT_HYPERVISOR_KVM
] = "KVM",
47 static const struct intel_gvt_ops intel_gvt_ops
= {
48 .emulate_cfg_read
= intel_vgpu_emulate_cfg_read
,
49 .emulate_cfg_write
= intel_vgpu_emulate_cfg_write
,
50 .emulate_mmio_read
= intel_vgpu_emulate_mmio_read
,
51 .emulate_mmio_write
= intel_vgpu_emulate_mmio_write
,
52 .vgpu_create
= intel_gvt_create_vgpu
,
53 .vgpu_destroy
= intel_gvt_destroy_vgpu
,
54 .vgpu_reset
= intel_gvt_reset_vgpu
,
58 * intel_gvt_init_host - Load MPT modules and detect if we're running in host
59 * @gvt: intel gvt device
61 * This function is called at the driver loading stage. If failed to find a
62 * loadable MPT module or detect currently we're running in a VM, then GVT-g
66 * Zero on success, negative error code if failed.
69 int intel_gvt_init_host(void)
73 if (intel_gvt_host
.initialized
)
77 if (xen_domain() && !xen_initial_domain())
80 /* Try to load MPT modules for hypervisors */
81 if (xen_initial_domain()) {
83 intel_gvt_host
.mpt
= try_then_request_module(
84 symbol_get(xengt_mpt
), "xengt");
85 intel_gvt_host
.hypervisor_type
= INTEL_GVT_HYPERVISOR_XEN
;
87 #if IS_ENABLED(CONFIG_DRM_I915_GVT_KVMGT)
88 /* not in Xen. Try KVMGT */
89 intel_gvt_host
.mpt
= try_then_request_module(
90 symbol_get(kvmgt_mpt
), "kvmgt");
91 intel_gvt_host
.hypervisor_type
= INTEL_GVT_HYPERVISOR_KVM
;
95 /* Fail to load MPT modules - bail out */
96 if (!intel_gvt_host
.mpt
)
99 /* Try to detect if we're running in host instead of VM. */
100 ret
= intel_gvt_hypervisor_detect_host();
104 gvt_dbg_core("Running with hypervisor %s in host mode\n",
105 supported_hypervisors
[intel_gvt_host
.hypervisor_type
]);
107 intel_gvt_host
.initialized
= true;
111 static void init_device_info(struct intel_gvt
*gvt
)
113 struct intel_gvt_device_info
*info
= &gvt
->device_info
;
114 struct pci_dev
*pdev
= gvt
->dev_priv
->drm
.pdev
;
116 if (IS_BROADWELL(gvt
->dev_priv
) || IS_SKYLAKE(gvt
->dev_priv
)) {
117 info
->max_support_vgpus
= 8;
118 info
->cfg_space_size
= 256;
119 info
->mmio_size
= 2 * 1024 * 1024;
121 info
->gtt_start_offset
= 8 * 1024 * 1024;
122 info
->gtt_entry_size
= 8;
123 info
->gtt_entry_size_shift
= 3;
124 info
->gmadr_bytes_in_cmd
= 8;
125 info
->max_surface_size
= 36 * 1024 * 1024;
127 info
->msi_cap_offset
= pdev
->msi_cap
;
130 static int gvt_service_thread(void *data
)
132 struct intel_gvt
*gvt
= (struct intel_gvt
*)data
;
135 gvt_dbg_core("service thread start\n");
137 while (!kthread_should_stop()) {
138 ret
= wait_event_interruptible(gvt
->service_thread_wq
,
139 kthread_should_stop() || gvt
->service_request
);
141 if (kthread_should_stop())
144 if (WARN_ONCE(ret
, "service thread is waken up by signal.\n"))
147 if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK
,
148 (void *)&gvt
->service_request
)) {
149 mutex_lock(&gvt
->lock
);
150 intel_gvt_emulate_vblank(gvt
);
151 mutex_unlock(&gvt
->lock
);
158 static void clean_service_thread(struct intel_gvt
*gvt
)
160 kthread_stop(gvt
->service_thread
);
163 static int init_service_thread(struct intel_gvt
*gvt
)
165 init_waitqueue_head(&gvt
->service_thread_wq
);
167 gvt
->service_thread
= kthread_run(gvt_service_thread
,
168 gvt
, "gvt_service_thread");
169 if (IS_ERR(gvt
->service_thread
)) {
170 gvt_err("fail to start service thread.\n");
171 return PTR_ERR(gvt
->service_thread
);
177 * intel_gvt_clean_device - clean a GVT device
178 * @gvt: intel gvt device
180 * This function is called at the driver unloading stage, to free the
181 * resources owned by a GVT device.
184 void intel_gvt_clean_device(struct drm_i915_private
*dev_priv
)
186 struct intel_gvt
*gvt
= to_gvt(dev_priv
);
191 clean_service_thread(gvt
);
192 intel_gvt_clean_cmd_parser(gvt
);
193 intel_gvt_clean_sched_policy(gvt
);
194 intel_gvt_clean_workload_scheduler(gvt
);
195 intel_gvt_clean_opregion(gvt
);
196 intel_gvt_clean_gtt(gvt
);
197 intel_gvt_clean_irq(gvt
);
198 intel_gvt_clean_mmio_info(gvt
);
199 intel_gvt_free_firmware(gvt
);
201 intel_gvt_hypervisor_host_exit(&dev_priv
->drm
.pdev
->dev
, gvt
);
202 intel_gvt_clean_vgpu_types(gvt
);
204 kfree(dev_priv
->gvt
);
205 dev_priv
->gvt
= NULL
;
209 * intel_gvt_init_device - initialize a GVT device
210 * @dev_priv: drm i915 private data
212 * This function is called at the initialization stage, to initialize
213 * necessary GVT components.
216 * Zero on success, negative error code if failed.
219 int intel_gvt_init_device(struct drm_i915_private
*dev_priv
)
221 struct intel_gvt
*gvt
;
225 * Cannot initialize GVT device without intel_gvt_host gets
228 if (WARN_ON(!intel_gvt_host
.initialized
))
231 if (WARN_ON(dev_priv
->gvt
))
234 gvt
= kzalloc(sizeof(struct intel_gvt
), GFP_KERNEL
);
238 gvt_dbg_core("init gvt device\n");
240 mutex_init(&gvt
->lock
);
241 gvt
->dev_priv
= dev_priv
;
243 init_device_info(gvt
);
245 ret
= intel_gvt_setup_mmio_info(gvt
);
249 ret
= intel_gvt_load_firmware(gvt
);
251 goto out_clean_mmio_info
;
253 ret
= intel_gvt_init_irq(gvt
);
255 goto out_free_firmware
;
257 ret
= intel_gvt_init_gtt(gvt
);
261 ret
= intel_gvt_init_opregion(gvt
);
265 ret
= intel_gvt_init_workload_scheduler(gvt
);
267 goto out_clean_opregion
;
269 ret
= intel_gvt_init_sched_policy(gvt
);
271 goto out_clean_workload_scheduler
;
273 ret
= intel_gvt_init_cmd_parser(gvt
);
275 goto out_clean_sched_policy
;
277 ret
= init_service_thread(gvt
);
279 goto out_clean_cmd_parser
;
281 ret
= intel_gvt_init_vgpu_types(gvt
);
283 goto out_clean_thread
;
285 ret
= intel_gvt_hypervisor_host_init(&dev_priv
->drm
.pdev
->dev
, gvt
,
288 gvt_err("failed to register gvt-g host device: %d\n", ret
);
289 goto out_clean_types
;
292 gvt_dbg_core("gvt device initialization is done\n");
297 intel_gvt_clean_vgpu_types(gvt
);
299 clean_service_thread(gvt
);
300 out_clean_cmd_parser
:
301 intel_gvt_clean_cmd_parser(gvt
);
302 out_clean_sched_policy
:
303 intel_gvt_clean_sched_policy(gvt
);
304 out_clean_workload_scheduler
:
305 intel_gvt_clean_workload_scheduler(gvt
);
307 intel_gvt_clean_opregion(gvt
);
309 intel_gvt_clean_gtt(gvt
);
311 intel_gvt_clean_irq(gvt
);
313 intel_gvt_free_firmware(gvt
);
315 intel_gvt_clean_mmio_info(gvt
);