blob: e72e26c61a156c6a57137277b78f25a69c768076 [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"
38
39struct intel_gvt_host intel_gvt_host;
40
41static const char * const supported_hypervisors[] = {
42 [INTEL_GVT_HYPERVISOR_XEN] = "XEN",
43 [INTEL_GVT_HYPERVISOR_KVM] = "KVM",
44};
45
Zhi Wang4d60c5fd2016-07-20 01:14:38 -040046struct intel_gvt_io_emulation_ops intel_gvt_io_emulation_ops = {
47 .emulate_cfg_read = intel_vgpu_emulate_cfg_read,
48 .emulate_cfg_write = intel_vgpu_emulate_cfg_write,
Zhi Wange39c5ad2016-09-02 13:33:29 +080049 .emulate_mmio_read = intel_vgpu_emulate_mmio_read,
50 .emulate_mmio_write = intel_vgpu_emulate_mmio_write,
Zhi Wang4d60c5fd2016-07-20 01:14:38 -040051};
52
Zhi Wang0ad35fe2016-06-16 08:07:00 -040053/**
54 * intel_gvt_init_host - Load MPT modules and detect if we're running in host
55 * @gvt: intel gvt device
56 *
57 * This function is called at the driver loading stage. If failed to find a
58 * loadable MPT module or detect currently we're running in a VM, then GVT-g
59 * will be disabled
60 *
61 * Returns:
62 * Zero on success, negative error code if failed.
63 *
64 */
65int intel_gvt_init_host(void)
66{
67 if (intel_gvt_host.initialized)
68 return 0;
69
70 /* Xen DOM U */
71 if (xen_domain() && !xen_initial_domain())
72 return -ENODEV;
73
74 /* Try to load MPT modules for hypervisors */
75 if (xen_initial_domain()) {
76 /* In Xen dom0 */
77 intel_gvt_host.mpt = try_then_request_module(
78 symbol_get(xengt_mpt), "xengt");
79 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_XEN;
80 } else {
81 /* not in Xen. Try KVMGT */
82 intel_gvt_host.mpt = try_then_request_module(
83 symbol_get(kvmgt_mpt), "kvm");
84 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_KVM;
85 }
86
87 /* Fail to load MPT modules - bail out */
88 if (!intel_gvt_host.mpt)
89 return -EINVAL;
90
91 /* Try to detect if we're running in host instead of VM. */
92 if (!intel_gvt_hypervisor_detect_host())
93 return -ENODEV;
94
95 gvt_dbg_core("Running with hypervisor %s in host mode\n",
96 supported_hypervisors[intel_gvt_host.hypervisor_type]);
97
98 intel_gvt_host.initialized = true;
99 return 0;
100}
101
102static void init_device_info(struct intel_gvt *gvt)
103{
Zhi Wang12d14cc2016-08-30 11:06:17 +0800104 struct intel_gvt_device_info *info = &gvt->device_info;
105
106 if (IS_BROADWELL(gvt->dev_priv) || IS_SKYLAKE(gvt->dev_priv)) {
107 info->max_support_vgpus = 8;
Zhi Wang579cea52016-06-30 12:45:34 -0400108 info->cfg_space_size = 256;
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800109 info->mmio_size = 2 * 1024 * 1024;
Zhi Wang579cea52016-06-30 12:45:34 -0400110 info->mmio_bar = 0;
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800111 info->msi_cap_offset = IS_SKYLAKE(gvt->dev_priv) ? 0xac : 0x90;
Zhi Wang2707e442016-03-28 23:23:16 +0800112 info->gtt_start_offset = 8 * 1024 * 1024;
113 info->gtt_entry_size = 8;
114 info->gtt_entry_size_shift = 3;
Zhi Wangbe1da702016-05-03 18:26:57 -0400115 info->gmadr_bytes_in_cmd = 8;
116 info->max_surface_size = 36 * 1024 * 1024;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800117 }
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400118}
119
Zhi Wang04d348a2016-04-25 18:28:56 -0400120static int gvt_service_thread(void *data)
121{
122 struct intel_gvt *gvt = (struct intel_gvt *)data;
123 int ret;
124
125 gvt_dbg_core("service thread start\n");
126
127 while (!kthread_should_stop()) {
128 ret = wait_event_interruptible(gvt->service_thread_wq,
129 kthread_should_stop() || gvt->service_request);
130
131 if (kthread_should_stop())
132 break;
133
134 if (WARN_ONCE(ret, "service thread is waken up by signal.\n"))
135 continue;
136
137 if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK,
138 (void *)&gvt->service_request)) {
139 mutex_lock(&gvt->lock);
140 intel_gvt_emulate_vblank(gvt);
141 mutex_unlock(&gvt->lock);
142 }
143 }
144
145 return 0;
146}
147
148static void clean_service_thread(struct intel_gvt *gvt)
149{
150 kthread_stop(gvt->service_thread);
151}
152
153static int init_service_thread(struct intel_gvt *gvt)
154{
155 init_waitqueue_head(&gvt->service_thread_wq);
156
157 gvt->service_thread = kthread_run(gvt_service_thread,
158 gvt, "gvt_service_thread");
159 if (IS_ERR(gvt->service_thread)) {
160 gvt_err("fail to start service thread.\n");
161 return PTR_ERR(gvt->service_thread);
162 }
163 return 0;
164}
165
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400166/**
167 * intel_gvt_clean_device - clean a GVT device
168 * @gvt: intel gvt device
169 *
170 * This function is called at the driver unloading stage, to free the
171 * resources owned by a GVT device.
172 *
173 */
174void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
175{
176 struct intel_gvt *gvt = &dev_priv->gvt;
177
178 if (WARN_ON(!gvt->initialized))
179 return;
180
Zhi Wang04d348a2016-04-25 18:28:56 -0400181 clean_service_thread(gvt);
Zhi Wangbe1da702016-05-03 18:26:57 -0400182 intel_gvt_clean_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400183 intel_gvt_clean_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400184 intel_gvt_clean_workload_scheduler(gvt);
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400185 intel_gvt_clean_opregion(gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800186 intel_gvt_clean_gtt(gvt);
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800187 intel_gvt_clean_irq(gvt);
Zhi Wang12d14cc2016-08-30 11:06:17 +0800188 intel_gvt_clean_mmio_info(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400189 intel_gvt_free_firmware(gvt);
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400190
191 gvt->initialized = false;
192}
193
194/**
195 * intel_gvt_init_device - initialize a GVT device
196 * @dev_priv: drm i915 private data
197 *
198 * This function is called at the initialization stage, to initialize
199 * necessary GVT components.
200 *
201 * Returns:
202 * Zero on success, negative error code if failed.
203 *
204 */
205int intel_gvt_init_device(struct drm_i915_private *dev_priv)
206{
207 struct intel_gvt *gvt = &dev_priv->gvt;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800208 int ret;
209
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400210 /*
211 * Cannot initialize GVT device without intel_gvt_host gets
212 * initialized first.
213 */
214 if (WARN_ON(!intel_gvt_host.initialized))
215 return -EINVAL;
216
217 if (WARN_ON(gvt->initialized))
218 return -EEXIST;
219
220 gvt_dbg_core("init gvt device\n");
221
Zhi Wang28a60de2016-09-02 12:41:29 +0800222 mutex_init(&gvt->lock);
223 gvt->dev_priv = dev_priv;
224
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400225 init_device_info(gvt);
Zhi Wang12d14cc2016-08-30 11:06:17 +0800226
227 ret = intel_gvt_setup_mmio_info(gvt);
228 if (ret)
229 return ret;
230
Zhi Wang579cea52016-06-30 12:45:34 -0400231 ret = intel_gvt_load_firmware(gvt);
232 if (ret)
233 goto out_clean_mmio_info;
234
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800235 ret = intel_gvt_init_irq(gvt);
236 if (ret)
237 goto out_free_firmware;
238
Zhi Wang2707e442016-03-28 23:23:16 +0800239 ret = intel_gvt_init_gtt(gvt);
240 if (ret)
241 goto out_clean_irq;
242
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400243 ret = intel_gvt_init_opregion(gvt);
244 if (ret)
245 goto out_clean_gtt;
246
Zhi Wange4734052016-05-01 07:42:16 -0400247 ret = intel_gvt_init_workload_scheduler(gvt);
Zhi Wang04d348a2016-04-25 18:28:56 -0400248 if (ret)
249 goto out_clean_opregion;
250
Zhi Wang4b639602016-05-01 17:09:58 -0400251 ret = intel_gvt_init_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400252 if (ret)
253 goto out_clean_workload_scheduler;
254
Zhi Wangbe1da702016-05-03 18:26:57 -0400255 ret = intel_gvt_init_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400256 if (ret)
257 goto out_clean_sched_policy;
258
Zhi Wangbe1da702016-05-03 18:26:57 -0400259 ret = init_service_thread(gvt);
260 if (ret)
261 goto out_clean_cmd_parser;
262
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400263 gvt_dbg_core("gvt device creation is done\n");
264 gvt->initialized = true;
265 return 0;
Zhi Wang579cea52016-06-30 12:45:34 -0400266
Zhi Wangbe1da702016-05-03 18:26:57 -0400267out_clean_cmd_parser:
268 intel_gvt_clean_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400269out_clean_sched_policy:
270 intel_gvt_clean_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400271out_clean_workload_scheduler:
272 intel_gvt_clean_workload_scheduler(gvt);
Zhi Wang04d348a2016-04-25 18:28:56 -0400273out_clean_opregion:
274 intel_gvt_clean_opregion(gvt);
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400275out_clean_gtt:
276 intel_gvt_clean_gtt(gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800277out_clean_irq:
278 intel_gvt_clean_irq(gvt);
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800279out_free_firmware:
280 intel_gvt_free_firmware(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400281out_clean_mmio_info:
282 intel_gvt_clean_mmio_info(gvt);
283 return ret;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400284}