blob: 13db29d045a46329ca848769a271fe39c05a75cb [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
Jike Song40df6ea2016-11-03 18:38:33 +0800196 intel_gvt_hypervisor_host_exit(&dev_priv->drm.pdev->dev, gvt);
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800197 intel_gvt_clean_vgpu_types(gvt);
198
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800199 kfree(dev_priv->gvt);
200 dev_priv->gvt = NULL;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400201}
202
203/**
204 * intel_gvt_init_device - initialize a GVT device
205 * @dev_priv: drm i915 private data
206 *
207 * This function is called at the initialization stage, to initialize
208 * necessary GVT components.
209 *
210 * Returns:
211 * Zero on success, negative error code if failed.
212 *
213 */
214int intel_gvt_init_device(struct drm_i915_private *dev_priv)
215{
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800216 struct intel_gvt *gvt;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800217 int ret;
218
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400219 /*
220 * Cannot initialize GVT device without intel_gvt_host gets
221 * initialized first.
222 */
223 if (WARN_ON(!intel_gvt_host.initialized))
224 return -EINVAL;
225
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800226 if (WARN_ON(dev_priv->gvt))
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400227 return -EEXIST;
228
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800229 gvt = kzalloc(sizeof(struct intel_gvt), GFP_KERNEL);
230 if (!gvt)
231 return -ENOMEM;
232
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400233 gvt_dbg_core("init gvt device\n");
234
Zhi Wang28a60de2016-09-02 12:41:29 +0800235 mutex_init(&gvt->lock);
236 gvt->dev_priv = dev_priv;
237
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400238 init_device_info(gvt);
Zhi Wang12d14cc2016-08-30 11:06:17 +0800239
240 ret = intel_gvt_setup_mmio_info(gvt);
241 if (ret)
242 return ret;
243
Zhi Wang579cea52016-06-30 12:45:34 -0400244 ret = intel_gvt_load_firmware(gvt);
245 if (ret)
246 goto out_clean_mmio_info;
247
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800248 ret = intel_gvt_init_irq(gvt);
249 if (ret)
250 goto out_free_firmware;
251
Zhi Wang2707e442016-03-28 23:23:16 +0800252 ret = intel_gvt_init_gtt(gvt);
253 if (ret)
254 goto out_clean_irq;
255
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400256 ret = intel_gvt_init_opregion(gvt);
257 if (ret)
258 goto out_clean_gtt;
259
Zhi Wange4734052016-05-01 07:42:16 -0400260 ret = intel_gvt_init_workload_scheduler(gvt);
Zhi Wang04d348a2016-04-25 18:28:56 -0400261 if (ret)
262 goto out_clean_opregion;
263
Zhi Wang4b639602016-05-01 17:09:58 -0400264 ret = intel_gvt_init_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400265 if (ret)
266 goto out_clean_workload_scheduler;
267
Zhi Wangbe1da702016-05-03 18:26:57 -0400268 ret = intel_gvt_init_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400269 if (ret)
270 goto out_clean_sched_policy;
271
Zhi Wangbe1da702016-05-03 18:26:57 -0400272 ret = init_service_thread(gvt);
273 if (ret)
274 goto out_clean_cmd_parser;
275
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800276 ret = intel_gvt_init_vgpu_types(gvt);
277 if (ret)
278 goto out_clean_thread;
279
Jike Song40df6ea2016-11-03 18:38:33 +0800280 ret = intel_gvt_hypervisor_host_init(&dev_priv->drm.pdev->dev, gvt,
281 &intel_gvt_io_emulation_ops);
282 if (ret) {
283 gvt_err("failed to register gvt-g host device: %d\n", ret);
284 goto out_clean_types;
285 }
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800286
287 gvt_dbg_core("gvt device initialization is done\n");
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800288 dev_priv->gvt = gvt;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400289 return 0;
Zhi Wang579cea52016-06-30 12:45:34 -0400290
Jike Song40df6ea2016-11-03 18:38:33 +0800291out_clean_types:
292 intel_gvt_clean_vgpu_types(gvt);
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800293out_clean_thread:
294 clean_service_thread(gvt);
Zhi Wangbe1da702016-05-03 18:26:57 -0400295out_clean_cmd_parser:
296 intel_gvt_clean_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400297out_clean_sched_policy:
298 intel_gvt_clean_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400299out_clean_workload_scheduler:
300 intel_gvt_clean_workload_scheduler(gvt);
Zhi Wang04d348a2016-04-25 18:28:56 -0400301out_clean_opregion:
302 intel_gvt_clean_opregion(gvt);
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400303out_clean_gtt:
304 intel_gvt_clean_gtt(gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800305out_clean_irq:
306 intel_gvt_clean_irq(gvt);
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800307out_free_firmware:
308 intel_gvt_free_firmware(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400309out_clean_mmio_info:
310 intel_gvt_clean_mmio_info(gvt);
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800311 kfree(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400312 return ret;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400313}