blob: 3b9d59e457ba7dbf2a1baffff7cd4f4c9aa75f3f [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
Jike Song9ec1e662016-11-03 18:38:35 +080047static const struct intel_gvt_ops intel_gvt_ops = {
Zhi Wang4d60c5fd2016-07-20 01:14:38 -040048 .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,
Jike Song9ec1e662016-11-03 18:38:35 +080052 .vgpu_create = intel_gvt_create_vgpu,
53 .vgpu_destroy = intel_gvt_destroy_vgpu,
54 .vgpu_reset = intel_gvt_reset_vgpu,
Zhi Wang4d60c5fd2016-07-20 01:14:38 -040055};
56
Zhi Wang0ad35fe2016-06-16 08:07:00 -040057/**
58 * intel_gvt_init_host - Load MPT modules and detect if we're running in host
59 * @gvt: intel gvt device
60 *
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
63 * will be disabled
64 *
65 * Returns:
66 * Zero on success, negative error code if failed.
67 *
68 */
69int intel_gvt_init_host(void)
70{
71 if (intel_gvt_host.initialized)
72 return 0;
73
74 /* Xen DOM U */
75 if (xen_domain() && !xen_initial_domain())
76 return -ENODEV;
77
78 /* Try to load MPT modules for hypervisors */
79 if (xen_initial_domain()) {
80 /* In Xen dom0 */
81 intel_gvt_host.mpt = try_then_request_module(
82 symbol_get(xengt_mpt), "xengt");
83 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_XEN;
84 } else {
Jike Songf30437c2016-11-09 20:30:59 +080085#if IS_ENABLED(CONFIG_DRM_I915_GVT_KVMGT)
Zhi Wang0ad35fe2016-06-16 08:07:00 -040086 /* not in Xen. Try KVMGT */
87 intel_gvt_host.mpt = try_then_request_module(
Jike Songf30437c2016-11-09 20:30:59 +080088 symbol_get(kvmgt_mpt), "kvmgt");
Zhi Wang0ad35fe2016-06-16 08:07:00 -040089 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_KVM;
Jike Songf30437c2016-11-09 20:30:59 +080090#endif
Zhi Wang0ad35fe2016-06-16 08:07:00 -040091 }
92
93 /* Fail to load MPT modules - bail out */
94 if (!intel_gvt_host.mpt)
95 return -EINVAL;
96
Zhi Wang0ad35fe2016-06-16 08:07:00 -040097 gvt_dbg_core("Running with hypervisor %s in host mode\n",
98 supported_hypervisors[intel_gvt_host.hypervisor_type]);
99
100 intel_gvt_host.initialized = true;
101 return 0;
102}
103
104static void init_device_info(struct intel_gvt *gvt)
105{
Zhi Wang12d14cc2016-08-30 11:06:17 +0800106 struct intel_gvt_device_info *info = &gvt->device_info;
Du, Changbin49129202016-10-24 15:58:44 +0800107 struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800108
109 if (IS_BROADWELL(gvt->dev_priv) || IS_SKYLAKE(gvt->dev_priv)) {
110 info->max_support_vgpus = 8;
Zhi Wang579cea52016-06-30 12:45:34 -0400111 info->cfg_space_size = 256;
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800112 info->mmio_size = 2 * 1024 * 1024;
Zhi Wang579cea52016-06-30 12:45:34 -0400113 info->mmio_bar = 0;
Zhi Wang2707e442016-03-28 23:23:16 +0800114 info->gtt_start_offset = 8 * 1024 * 1024;
115 info->gtt_entry_size = 8;
116 info->gtt_entry_size_shift = 3;
Zhi Wangbe1da702016-05-03 18:26:57 -0400117 info->gmadr_bytes_in_cmd = 8;
118 info->max_surface_size = 36 * 1024 * 1024;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800119 }
Du, Changbin49129202016-10-24 15:58:44 +0800120 info->msi_cap_offset = pdev->msi_cap;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400121}
122
Zhi Wang04d348a2016-04-25 18:28:56 -0400123static int gvt_service_thread(void *data)
124{
125 struct intel_gvt *gvt = (struct intel_gvt *)data;
126 int ret;
127
128 gvt_dbg_core("service thread start\n");
129
130 while (!kthread_should_stop()) {
131 ret = wait_event_interruptible(gvt->service_thread_wq,
132 kthread_should_stop() || gvt->service_request);
133
134 if (kthread_should_stop())
135 break;
136
137 if (WARN_ONCE(ret, "service thread is waken up by signal.\n"))
138 continue;
139
140 if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK,
141 (void *)&gvt->service_request)) {
142 mutex_lock(&gvt->lock);
143 intel_gvt_emulate_vblank(gvt);
144 mutex_unlock(&gvt->lock);
145 }
146 }
147
148 return 0;
149}
150
151static void clean_service_thread(struct intel_gvt *gvt)
152{
153 kthread_stop(gvt->service_thread);
154}
155
156static int init_service_thread(struct intel_gvt *gvt)
157{
158 init_waitqueue_head(&gvt->service_thread_wq);
159
160 gvt->service_thread = kthread_run(gvt_service_thread,
161 gvt, "gvt_service_thread");
162 if (IS_ERR(gvt->service_thread)) {
163 gvt_err("fail to start service thread.\n");
164 return PTR_ERR(gvt->service_thread);
165 }
166 return 0;
167}
168
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400169/**
170 * intel_gvt_clean_device - clean a GVT device
171 * @gvt: intel gvt device
172 *
173 * This function is called at the driver unloading stage, to free the
174 * resources owned by a GVT device.
175 *
176 */
177void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
178{
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800179 struct intel_gvt *gvt = to_gvt(dev_priv);
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400180
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800181 if (WARN_ON(!gvt))
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400182 return;
183
Zhi Wang04d348a2016-04-25 18:28:56 -0400184 clean_service_thread(gvt);
Zhi Wangbe1da702016-05-03 18:26:57 -0400185 intel_gvt_clean_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400186 intel_gvt_clean_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400187 intel_gvt_clean_workload_scheduler(gvt);
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400188 intel_gvt_clean_opregion(gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800189 intel_gvt_clean_gtt(gvt);
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800190 intel_gvt_clean_irq(gvt);
Zhi Wang12d14cc2016-08-30 11:06:17 +0800191 intel_gvt_clean_mmio_info(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400192 intel_gvt_free_firmware(gvt);
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400193
Jike Song40df6ea2016-11-03 18:38:33 +0800194 intel_gvt_hypervisor_host_exit(&dev_priv->drm.pdev->dev, gvt);
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800195 intel_gvt_clean_vgpu_types(gvt);
196
Jike Song59c05732017-01-06 15:16:21 +0800197 idr_destroy(&gvt->vgpu_idr);
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
Jike Song59c05732017-01-06 15:16:21 +0800235 idr_init(&gvt->vgpu_idr);
236
Zhi Wang28a60de2016-09-02 12:41:29 +0800237 mutex_init(&gvt->lock);
238 gvt->dev_priv = dev_priv;
239
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400240 init_device_info(gvt);
Zhi Wang12d14cc2016-08-30 11:06:17 +0800241
242 ret = intel_gvt_setup_mmio_info(gvt);
243 if (ret)
Jike Song59c05732017-01-06 15:16:21 +0800244 goto out_clean_idr;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800245
Zhi Wang579cea52016-06-30 12:45:34 -0400246 ret = intel_gvt_load_firmware(gvt);
247 if (ret)
248 goto out_clean_mmio_info;
249
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800250 ret = intel_gvt_init_irq(gvt);
251 if (ret)
252 goto out_free_firmware;
253
Zhi Wang2707e442016-03-28 23:23:16 +0800254 ret = intel_gvt_init_gtt(gvt);
255 if (ret)
256 goto out_clean_irq;
257
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400258 ret = intel_gvt_init_opregion(gvt);
259 if (ret)
260 goto out_clean_gtt;
261
Zhi Wange4734052016-05-01 07:42:16 -0400262 ret = intel_gvt_init_workload_scheduler(gvt);
Zhi Wang04d348a2016-04-25 18:28:56 -0400263 if (ret)
264 goto out_clean_opregion;
265
Zhi Wang4b639602016-05-01 17:09:58 -0400266 ret = intel_gvt_init_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400267 if (ret)
268 goto out_clean_workload_scheduler;
269
Zhi Wangbe1da702016-05-03 18:26:57 -0400270 ret = intel_gvt_init_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400271 if (ret)
272 goto out_clean_sched_policy;
273
Zhi Wangbe1da702016-05-03 18:26:57 -0400274 ret = init_service_thread(gvt);
275 if (ret)
276 goto out_clean_cmd_parser;
277
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800278 ret = intel_gvt_init_vgpu_types(gvt);
279 if (ret)
280 goto out_clean_thread;
281
Jike Song40df6ea2016-11-03 18:38:33 +0800282 ret = intel_gvt_hypervisor_host_init(&dev_priv->drm.pdev->dev, gvt,
Jike Song9ec1e662016-11-03 18:38:35 +0800283 &intel_gvt_ops);
Jike Song40df6ea2016-11-03 18:38:33 +0800284 if (ret) {
285 gvt_err("failed to register gvt-g host device: %d\n", ret);
286 goto out_clean_types;
287 }
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800288
289 gvt_dbg_core("gvt device initialization is done\n");
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800290 dev_priv->gvt = gvt;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400291 return 0;
Zhi Wang579cea52016-06-30 12:45:34 -0400292
Jike Song40df6ea2016-11-03 18:38:33 +0800293out_clean_types:
294 intel_gvt_clean_vgpu_types(gvt);
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800295out_clean_thread:
296 clean_service_thread(gvt);
Zhi Wangbe1da702016-05-03 18:26:57 -0400297out_clean_cmd_parser:
298 intel_gvt_clean_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400299out_clean_sched_policy:
300 intel_gvt_clean_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400301out_clean_workload_scheduler:
302 intel_gvt_clean_workload_scheduler(gvt);
Zhi Wang04d348a2016-04-25 18:28:56 -0400303out_clean_opregion:
304 intel_gvt_clean_opregion(gvt);
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400305out_clean_gtt:
306 intel_gvt_clean_gtt(gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800307out_clean_irq:
308 intel_gvt_clean_irq(gvt);
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800309out_free_firmware:
310 intel_gvt_free_firmware(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400311out_clean_mmio_info:
312 intel_gvt_clean_mmio_info(gvt);
Jike Song59c05732017-01-06 15:16:21 +0800313out_clean_idr:
314 idr_destroy(&gvt->vgpu_idr);
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800315 kfree(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400316 return ret;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400317}