blob: 385969a8921680f71e1f57a0843f605d20538cea [file] [log] [blame]
Zhi Wang0ad35fe2016-06-16 08:07:00 -04001/*
2 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * SOFTWARE.
Zhi Wang12d14cc2016-08-30 11:06:17 +080022 *
23 * Authors:
24 * Kevin Tian <kevin.tian@intel.com>
25 * Eddie Dong <eddie.dong@intel.com>
26 *
27 * Contributors:
28 * Niu Bing <bing.niu@intel.com>
29 * Zhi Wang <zhi.a.wang@intel.com>
30 *
Zhi Wang0ad35fe2016-06-16 08:07:00 -040031 */
32
33#include <linux/types.h>
34#include <xen/xen.h>
Zhi Wang04d348a2016-04-25 18:28:56 -040035#include <linux/kthread.h>
Zhi Wang0ad35fe2016-06-16 08:07:00 -040036
37#include "i915_drv.h"
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +080038#include "gvt.h"
Zhi Wang0ad35fe2016-06-16 08:07:00 -040039
40struct intel_gvt_host intel_gvt_host;
41
42static const char * const supported_hypervisors[] = {
43 [INTEL_GVT_HYPERVISOR_XEN] = "XEN",
44 [INTEL_GVT_HYPERVISOR_KVM] = "KVM",
45};
46
Zhi Wang4d60c5fd2016-07-20 01:14:38 -040047struct intel_gvt_io_emulation_ops intel_gvt_io_emulation_ops = {
48 .emulate_cfg_read = intel_vgpu_emulate_cfg_read,
49 .emulate_cfg_write = intel_vgpu_emulate_cfg_write,
Zhi Wange39c5ad2016-09-02 13:33:29 +080050 .emulate_mmio_read = intel_vgpu_emulate_mmio_read,
51 .emulate_mmio_write = intel_vgpu_emulate_mmio_write,
Zhi Wang4d60c5fd2016-07-20 01:14:38 -040052};
53
Zhi Wang0ad35fe2016-06-16 08:07:00 -040054/**
55 * intel_gvt_init_host - Load MPT modules and detect if we're running in host
56 * @gvt: intel gvt device
57 *
58 * This function is called at the driver loading stage. If failed to find a
59 * loadable MPT module or detect currently we're running in a VM, then GVT-g
60 * will be disabled
61 *
62 * Returns:
63 * Zero on success, negative error code if failed.
64 *
65 */
66int intel_gvt_init_host(void)
67{
Xiaoguang Chenb0122f32016-10-25 16:56:45 +080068 int ret;
69
Zhi Wang0ad35fe2016-06-16 08:07:00 -040070 if (intel_gvt_host.initialized)
71 return 0;
72
73 /* Xen DOM U */
74 if (xen_domain() && !xen_initial_domain())
75 return -ENODEV;
76
77 /* Try to load MPT modules for hypervisors */
78 if (xen_initial_domain()) {
79 /* In Xen dom0 */
80 intel_gvt_host.mpt = try_then_request_module(
81 symbol_get(xengt_mpt), "xengt");
82 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_XEN;
83 } else {
84 /* not in Xen. Try KVMGT */
85 intel_gvt_host.mpt = try_then_request_module(
86 symbol_get(kvmgt_mpt), "kvm");
87 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_KVM;
88 }
89
90 /* Fail to load MPT modules - bail out */
91 if (!intel_gvt_host.mpt)
92 return -EINVAL;
93
94 /* Try to detect if we're running in host instead of VM. */
Xiaoguang Chenb0122f32016-10-25 16:56:45 +080095 ret = intel_gvt_hypervisor_detect_host();
96 if (ret)
Zhi Wang0ad35fe2016-06-16 08:07:00 -040097 return -ENODEV;
98
99 gvt_dbg_core("Running with hypervisor %s in host mode\n",
100 supported_hypervisors[intel_gvt_host.hypervisor_type]);
101
102 intel_gvt_host.initialized = true;
103 return 0;
104}
105
106static void init_device_info(struct intel_gvt *gvt)
107{
Zhi Wang12d14cc2016-08-30 11:06:17 +0800108 struct intel_gvt_device_info *info = &gvt->device_info;
Du, Changbin49129202016-10-24 15:58:44 +0800109 struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800110
111 if (IS_BROADWELL(gvt->dev_priv) || IS_SKYLAKE(gvt->dev_priv)) {
112 info->max_support_vgpus = 8;
Zhi Wang579cea52016-06-30 12:45:34 -0400113 info->cfg_space_size = 256;
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800114 info->mmio_size = 2 * 1024 * 1024;
Zhi Wang579cea52016-06-30 12:45:34 -0400115 info->mmio_bar = 0;
Zhi Wang2707e442016-03-28 23:23:16 +0800116 info->gtt_start_offset = 8 * 1024 * 1024;
117 info->gtt_entry_size = 8;
118 info->gtt_entry_size_shift = 3;
Zhi Wangbe1da702016-05-03 18:26:57 -0400119 info->gmadr_bytes_in_cmd = 8;
120 info->max_surface_size = 36 * 1024 * 1024;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800121 }
Du, Changbin49129202016-10-24 15:58:44 +0800122 info->msi_cap_offset = pdev->msi_cap;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400123}
124
Zhi Wang04d348a2016-04-25 18:28:56 -0400125static int gvt_service_thread(void *data)
126{
127 struct intel_gvt *gvt = (struct intel_gvt *)data;
128 int ret;
129
130 gvt_dbg_core("service thread start\n");
131
132 while (!kthread_should_stop()) {
133 ret = wait_event_interruptible(gvt->service_thread_wq,
134 kthread_should_stop() || gvt->service_request);
135
136 if (kthread_should_stop())
137 break;
138
139 if (WARN_ONCE(ret, "service thread is waken up by signal.\n"))
140 continue;
141
142 if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK,
143 (void *)&gvt->service_request)) {
144 mutex_lock(&gvt->lock);
145 intel_gvt_emulate_vblank(gvt);
146 mutex_unlock(&gvt->lock);
147 }
148 }
149
150 return 0;
151}
152
153static void clean_service_thread(struct intel_gvt *gvt)
154{
155 kthread_stop(gvt->service_thread);
156}
157
158static int init_service_thread(struct intel_gvt *gvt)
159{
160 init_waitqueue_head(&gvt->service_thread_wq);
161
162 gvt->service_thread = kthread_run(gvt_service_thread,
163 gvt, "gvt_service_thread");
164 if (IS_ERR(gvt->service_thread)) {
165 gvt_err("fail to start service thread.\n");
166 return PTR_ERR(gvt->service_thread);
167 }
168 return 0;
169}
170
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400171/**
172 * intel_gvt_clean_device - clean a GVT device
173 * @gvt: intel gvt device
174 *
175 * This function is called at the driver unloading stage, to free the
176 * resources owned by a GVT device.
177 *
178 */
179void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
180{
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800181 struct intel_gvt *gvt = to_gvt(dev_priv);
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400182
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800183 if (WARN_ON(!gvt))
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400184 return;
185
Zhi Wang04d348a2016-04-25 18:28:56 -0400186 clean_service_thread(gvt);
Zhi Wangbe1da702016-05-03 18:26:57 -0400187 intel_gvt_clean_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400188 intel_gvt_clean_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400189 intel_gvt_clean_workload_scheduler(gvt);
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400190 intel_gvt_clean_opregion(gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800191 intel_gvt_clean_gtt(gvt);
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800192 intel_gvt_clean_irq(gvt);
Zhi Wang12d14cc2016-08-30 11:06:17 +0800193 intel_gvt_clean_mmio_info(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400194 intel_gvt_free_firmware(gvt);
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400195
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800196 kfree(dev_priv->gvt);
197 dev_priv->gvt = NULL;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400198}
199
200/**
201 * intel_gvt_init_device - initialize a GVT device
202 * @dev_priv: drm i915 private data
203 *
204 * This function is called at the initialization stage, to initialize
205 * necessary GVT components.
206 *
207 * Returns:
208 * Zero on success, negative error code if failed.
209 *
210 */
211int intel_gvt_init_device(struct drm_i915_private *dev_priv)
212{
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800213 struct intel_gvt *gvt;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800214 int ret;
215
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400216 /*
217 * Cannot initialize GVT device without intel_gvt_host gets
218 * initialized first.
219 */
220 if (WARN_ON(!intel_gvt_host.initialized))
221 return -EINVAL;
222
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800223 if (WARN_ON(dev_priv->gvt))
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400224 return -EEXIST;
225
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800226 gvt = kzalloc(sizeof(struct intel_gvt), GFP_KERNEL);
227 if (!gvt)
228 return -ENOMEM;
229
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400230 gvt_dbg_core("init gvt device\n");
231
Zhi Wang28a60de2016-09-02 12:41:29 +0800232 mutex_init(&gvt->lock);
233 gvt->dev_priv = dev_priv;
234
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400235 init_device_info(gvt);
Zhi Wang12d14cc2016-08-30 11:06:17 +0800236
237 ret = intel_gvt_setup_mmio_info(gvt);
238 if (ret)
239 return ret;
240
Zhi Wang579cea52016-06-30 12:45:34 -0400241 ret = intel_gvt_load_firmware(gvt);
242 if (ret)
243 goto out_clean_mmio_info;
244
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800245 ret = intel_gvt_init_irq(gvt);
246 if (ret)
247 goto out_free_firmware;
248
Zhi Wang2707e442016-03-28 23:23:16 +0800249 ret = intel_gvt_init_gtt(gvt);
250 if (ret)
251 goto out_clean_irq;
252
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400253 ret = intel_gvt_init_opregion(gvt);
254 if (ret)
255 goto out_clean_gtt;
256
Zhi Wange4734052016-05-01 07:42:16 -0400257 ret = intel_gvt_init_workload_scheduler(gvt);
Zhi Wang04d348a2016-04-25 18:28:56 -0400258 if (ret)
259 goto out_clean_opregion;
260
Zhi Wang4b639602016-05-01 17:09:58 -0400261 ret = intel_gvt_init_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400262 if (ret)
263 goto out_clean_workload_scheduler;
264
Zhi Wangbe1da702016-05-03 18:26:57 -0400265 ret = intel_gvt_init_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400266 if (ret)
267 goto out_clean_sched_policy;
268
Zhi Wangbe1da702016-05-03 18:26:57 -0400269 ret = init_service_thread(gvt);
270 if (ret)
271 goto out_clean_cmd_parser;
272
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400273 gvt_dbg_core("gvt device creation is done\n");
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800274 dev_priv->gvt = gvt;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400275 return 0;
Zhi Wang579cea52016-06-30 12:45:34 -0400276
Zhi Wangbe1da702016-05-03 18:26:57 -0400277out_clean_cmd_parser:
278 intel_gvt_clean_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400279out_clean_sched_policy:
280 intel_gvt_clean_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400281out_clean_workload_scheduler:
282 intel_gvt_clean_workload_scheduler(gvt);
Zhi Wang04d348a2016-04-25 18:28:56 -0400283out_clean_opregion:
284 intel_gvt_clean_opregion(gvt);
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400285out_clean_gtt:
286 intel_gvt_clean_gtt(gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800287out_clean_irq:
288 intel_gvt_clean_irq(gvt);
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800289out_free_firmware:
290 intel_gvt_free_firmware(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400291out_clean_mmio_info:
292 intel_gvt_clean_mmio_info(gvt);
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800293 kfree(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400294 return ret;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400295}