blob: ed313811fa7613e6f55abb52a577b850d892e041 [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"
fred gaoc5d71cb2017-09-28 11:03:02 +080039#include <linux/vfio.h>
40#include <linux/mdev.h>
Zhi Wang0ad35fe2016-06-16 08:07:00 -040041
42struct intel_gvt_host intel_gvt_host;
43
44static const char * const supported_hypervisors[] = {
45 [INTEL_GVT_HYPERVISOR_XEN] = "XEN",
46 [INTEL_GVT_HYPERVISOR_KVM] = "KVM",
47};
48
fred gaoc5d71cb2017-09-28 11:03:02 +080049static struct intel_vgpu_type *intel_gvt_find_vgpu_type(struct intel_gvt *gvt,
50 const char *name)
51{
52 int i;
53 struct intel_vgpu_type *t;
54 const char *driver_name = dev_driver_string(
55 &gvt->dev_priv->drm.pdev->dev);
56
57 for (i = 0; i < gvt->num_types; i++) {
58 t = &gvt->types[i];
59 if (!strncmp(t->name, name + strlen(driver_name) + 1,
60 sizeof(t->name)))
61 return t;
62 }
63
64 return NULL;
65}
66
67static ssize_t available_instances_show(struct kobject *kobj,
68 struct device *dev, char *buf)
69{
70 struct intel_vgpu_type *type;
71 unsigned int num = 0;
72 void *gvt = kdev_to_i915(dev)->gvt;
73
74 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
75 if (!type)
76 num = 0;
77 else
78 num = type->avail_instance;
79
80 return sprintf(buf, "%u\n", num);
81}
82
83static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
84 char *buf)
85{
86 return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
87}
88
89static ssize_t description_show(struct kobject *kobj, struct device *dev,
90 char *buf)
91{
92 struct intel_vgpu_type *type;
93 void *gvt = kdev_to_i915(dev)->gvt;
94
95 type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
96 if (!type)
97 return 0;
98
99 return sprintf(buf, "low_gm_size: %dMB\nhigh_gm_size: %dMB\n"
100 "fence: %d\nresolution: %s\n"
101 "weight: %d\n",
102 BYTES_TO_MB(type->low_gm_size),
103 BYTES_TO_MB(type->high_gm_size),
104 type->fence, vgpu_edid_str(type->resolution),
105 type->weight);
106}
107
108static MDEV_TYPE_ATTR_RO(available_instances);
109static MDEV_TYPE_ATTR_RO(device_api);
110static MDEV_TYPE_ATTR_RO(description);
111
112static struct attribute *gvt_type_attrs[] = {
113 &mdev_type_attr_available_instances.attr,
114 &mdev_type_attr_device_api.attr,
115 &mdev_type_attr_description.attr,
116 NULL,
117};
118
119static struct attribute_group *gvt_vgpu_type_groups[] = {
120 [0 ... NR_MAX_INTEL_VGPU_TYPES - 1] = NULL,
121};
122
123static bool intel_get_gvt_attrs(struct attribute ***type_attrs,
124 struct attribute_group ***intel_vgpu_type_groups)
125{
126 *type_attrs = gvt_type_attrs;
127 *intel_vgpu_type_groups = gvt_vgpu_type_groups;
128 return true;
129}
130
131static bool intel_gvt_init_vgpu_type_groups(struct intel_gvt *gvt)
132{
133 int i, j;
134 struct intel_vgpu_type *type;
135 struct attribute_group *group;
136
137 for (i = 0; i < gvt->num_types; i++) {
138 type = &gvt->types[i];
139
140 group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
141 if (WARN_ON(!group))
142 goto unwind;
143
144 group->name = type->name;
145 group->attrs = gvt_type_attrs;
146 gvt_vgpu_type_groups[i] = group;
147 }
148
149 return true;
150
151unwind:
152 for (j = 0; j < i; j++) {
153 group = gvt_vgpu_type_groups[j];
154 kfree(group);
155 }
156
157 return false;
158}
159
160static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt)
161{
162 int i;
163 struct attribute_group *group;
164
165 for (i = 0; i < gvt->num_types; i++) {
166 group = gvt_vgpu_type_groups[i];
167 gvt_vgpu_type_groups[i] = NULL;
168 kfree(group);
169 }
170}
171
Jike Song9ec1e662016-11-03 18:38:35 +0800172static const struct intel_gvt_ops intel_gvt_ops = {
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400173 .emulate_cfg_read = intel_vgpu_emulate_cfg_read,
174 .emulate_cfg_write = intel_vgpu_emulate_cfg_write,
Zhi Wange39c5ad2016-09-02 13:33:29 +0800175 .emulate_mmio_read = intel_vgpu_emulate_mmio_read,
176 .emulate_mmio_write = intel_vgpu_emulate_mmio_write,
Jike Song9ec1e662016-11-03 18:38:35 +0800177 .vgpu_create = intel_gvt_create_vgpu,
178 .vgpu_destroy = intel_gvt_destroy_vgpu,
179 .vgpu_reset = intel_gvt_reset_vgpu,
Zhi Wangb79c52a2017-03-30 01:48:39 +0800180 .vgpu_activate = intel_gvt_activate_vgpu,
181 .vgpu_deactivate = intel_gvt_deactivate_vgpu,
fred gaoc5d71cb2017-09-28 11:03:02 +0800182 .gvt_find_vgpu_type = intel_gvt_find_vgpu_type,
183 .get_gvt_attrs = intel_get_gvt_attrs,
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400184};
185
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400186/**
187 * intel_gvt_init_host - Load MPT modules and detect if we're running in host
188 * @gvt: intel gvt device
189 *
190 * This function is called at the driver loading stage. If failed to find a
191 * loadable MPT module or detect currently we're running in a VM, then GVT-g
192 * will be disabled
193 *
194 * Returns:
195 * Zero on success, negative error code if failed.
196 *
197 */
198int intel_gvt_init_host(void)
199{
200 if (intel_gvt_host.initialized)
201 return 0;
202
203 /* Xen DOM U */
204 if (xen_domain() && !xen_initial_domain())
205 return -ENODEV;
206
207 /* Try to load MPT modules for hypervisors */
208 if (xen_initial_domain()) {
209 /* In Xen dom0 */
210 intel_gvt_host.mpt = try_then_request_module(
211 symbol_get(xengt_mpt), "xengt");
212 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_XEN;
213 } else {
Jike Songf30437c2016-11-09 20:30:59 +0800214#if IS_ENABLED(CONFIG_DRM_I915_GVT_KVMGT)
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400215 /* not in Xen. Try KVMGT */
216 intel_gvt_host.mpt = try_then_request_module(
Jike Songf30437c2016-11-09 20:30:59 +0800217 symbol_get(kvmgt_mpt), "kvmgt");
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400218 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_KVM;
Jike Songf30437c2016-11-09 20:30:59 +0800219#endif
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400220 }
221
222 /* Fail to load MPT modules - bail out */
223 if (!intel_gvt_host.mpt)
224 return -EINVAL;
225
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400226 gvt_dbg_core("Running with hypervisor %s in host mode\n",
227 supported_hypervisors[intel_gvt_host.hypervisor_type]);
228
229 intel_gvt_host.initialized = true;
230 return 0;
231}
232
233static void init_device_info(struct intel_gvt *gvt)
234{
Zhi Wang12d14cc2016-08-30 11:06:17 +0800235 struct intel_gvt_device_info *info = &gvt->device_info;
Du, Changbin49129202016-10-24 15:58:44 +0800236 struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800237
Xu Hane3476c02017-03-29 10:13:59 +0800238 if (IS_BROADWELL(gvt->dev_priv) || IS_SKYLAKE(gvt->dev_priv)
239 || IS_KABYLAKE(gvt->dev_priv)) {
Zhi Wang12d14cc2016-08-30 11:06:17 +0800240 info->max_support_vgpus = 8;
Changbin Du02d578e2017-08-23 14:08:10 +0800241 info->cfg_space_size = PCI_CFG_SPACE_EXP_SIZE;
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800242 info->mmio_size = 2 * 1024 * 1024;
Zhi Wang579cea52016-06-30 12:45:34 -0400243 info->mmio_bar = 0;
Zhi Wang2707e442016-03-28 23:23:16 +0800244 info->gtt_start_offset = 8 * 1024 * 1024;
245 info->gtt_entry_size = 8;
246 info->gtt_entry_size_shift = 3;
Zhi Wangbe1da702016-05-03 18:26:57 -0400247 info->gmadr_bytes_in_cmd = 8;
248 info->max_surface_size = 36 * 1024 * 1024;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800249 }
Du, Changbin49129202016-10-24 15:58:44 +0800250 info->msi_cap_offset = pdev->msi_cap;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400251}
252
Zhi Wang04d348a2016-04-25 18:28:56 -0400253static int gvt_service_thread(void *data)
254{
255 struct intel_gvt *gvt = (struct intel_gvt *)data;
256 int ret;
257
258 gvt_dbg_core("service thread start\n");
259
260 while (!kthread_should_stop()) {
261 ret = wait_event_interruptible(gvt->service_thread_wq,
262 kthread_should_stop() || gvt->service_request);
263
264 if (kthread_should_stop())
265 break;
266
267 if (WARN_ONCE(ret, "service thread is waken up by signal.\n"))
268 continue;
269
270 if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK,
271 (void *)&gvt->service_request)) {
272 mutex_lock(&gvt->lock);
273 intel_gvt_emulate_vblank(gvt);
274 mutex_unlock(&gvt->lock);
275 }
Ping Gao91d01012017-03-30 00:36:34 +0800276
Ping Gaoc713cb22017-05-24 20:30:17 +0800277 if (test_bit(INTEL_GVT_REQUEST_SCHED,
278 (void *)&gvt->service_request) ||
279 test_bit(INTEL_GVT_REQUEST_EVENT_SCHED,
Ping Gao91d01012017-03-30 00:36:34 +0800280 (void *)&gvt->service_request)) {
281 intel_gvt_schedule(gvt);
282 }
Zhi Wang04d348a2016-04-25 18:28:56 -0400283 }
284
285 return 0;
286}
287
288static void clean_service_thread(struct intel_gvt *gvt)
289{
290 kthread_stop(gvt->service_thread);
291}
292
293static int init_service_thread(struct intel_gvt *gvt)
294{
295 init_waitqueue_head(&gvt->service_thread_wq);
296
297 gvt->service_thread = kthread_run(gvt_service_thread,
298 gvt, "gvt_service_thread");
299 if (IS_ERR(gvt->service_thread)) {
300 gvt_err("fail to start service thread.\n");
301 return PTR_ERR(gvt->service_thread);
302 }
303 return 0;
304}
305
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400306/**
307 * intel_gvt_clean_device - clean a GVT device
308 * @gvt: intel gvt device
309 *
310 * This function is called at the driver unloading stage, to free the
311 * resources owned by a GVT device.
312 *
313 */
314void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
315{
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800316 struct intel_gvt *gvt = to_gvt(dev_priv);
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400317
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800318 if (WARN_ON(!gvt))
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400319 return;
320
Zhi Wang04d348a2016-04-25 18:28:56 -0400321 clean_service_thread(gvt);
Zhi Wangbe1da702016-05-03 18:26:57 -0400322 intel_gvt_clean_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400323 intel_gvt_clean_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400324 intel_gvt_clean_workload_scheduler(gvt);
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400325 intel_gvt_clean_opregion(gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800326 intel_gvt_clean_gtt(gvt);
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800327 intel_gvt_clean_irq(gvt);
Zhi Wang12d14cc2016-08-30 11:06:17 +0800328 intel_gvt_clean_mmio_info(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400329 intel_gvt_free_firmware(gvt);
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400330
Jike Song40df6ea2016-11-03 18:38:33 +0800331 intel_gvt_hypervisor_host_exit(&dev_priv->drm.pdev->dev, gvt);
fred gaoc5d71cb2017-09-28 11:03:02 +0800332 intel_gvt_cleanup_vgpu_type_groups(gvt);
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800333 intel_gvt_clean_vgpu_types(gvt);
334
Jike Song59c05732017-01-06 15:16:21 +0800335 idr_destroy(&gvt->vgpu_idr);
336
Ping Gaoafe04fb2017-03-30 00:36:39 +0800337 intel_gvt_destroy_idle_vgpu(gvt->idle_vgpu);
338
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800339 kfree(dev_priv->gvt);
340 dev_priv->gvt = NULL;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400341}
342
343/**
344 * intel_gvt_init_device - initialize a GVT device
345 * @dev_priv: drm i915 private data
346 *
347 * This function is called at the initialization stage, to initialize
348 * necessary GVT components.
349 *
350 * Returns:
351 * Zero on success, negative error code if failed.
352 *
353 */
354int intel_gvt_init_device(struct drm_i915_private *dev_priv)
355{
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800356 struct intel_gvt *gvt;
Ping Gaoafe04fb2017-03-30 00:36:39 +0800357 struct intel_vgpu *vgpu;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800358 int ret;
359
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400360 /*
361 * Cannot initialize GVT device without intel_gvt_host gets
362 * initialized first.
363 */
364 if (WARN_ON(!intel_gvt_host.initialized))
365 return -EINVAL;
366
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800367 if (WARN_ON(dev_priv->gvt))
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400368 return -EEXIST;
369
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800370 gvt = kzalloc(sizeof(struct intel_gvt), GFP_KERNEL);
371 if (!gvt)
372 return -ENOMEM;
373
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400374 gvt_dbg_core("init gvt device\n");
375
Jike Song59c05732017-01-06 15:16:21 +0800376 idr_init(&gvt->vgpu_idr);
Changbin Du0e86cc92017-05-04 10:52:38 +0800377 spin_lock_init(&gvt->scheduler.mmio_context_lock);
Zhi Wang28a60de2016-09-02 12:41:29 +0800378 mutex_init(&gvt->lock);
379 gvt->dev_priv = dev_priv;
380
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400381 init_device_info(gvt);
Zhi Wang12d14cc2016-08-30 11:06:17 +0800382
383 ret = intel_gvt_setup_mmio_info(gvt);
384 if (ret)
Jike Song59c05732017-01-06 15:16:21 +0800385 goto out_clean_idr;
Zhi Wang12d14cc2016-08-30 11:06:17 +0800386
Zhi Wang579cea52016-06-30 12:45:34 -0400387 ret = intel_gvt_load_firmware(gvt);
388 if (ret)
389 goto out_clean_mmio_info;
390
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800391 ret = intel_gvt_init_irq(gvt);
392 if (ret)
393 goto out_free_firmware;
394
Zhi Wang2707e442016-03-28 23:23:16 +0800395 ret = intel_gvt_init_gtt(gvt);
396 if (ret)
397 goto out_clean_irq;
398
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400399 ret = intel_gvt_init_opregion(gvt);
400 if (ret)
401 goto out_clean_gtt;
402
Zhi Wange4734052016-05-01 07:42:16 -0400403 ret = intel_gvt_init_workload_scheduler(gvt);
Zhi Wang04d348a2016-04-25 18:28:56 -0400404 if (ret)
405 goto out_clean_opregion;
406
Zhi Wang4b639602016-05-01 17:09:58 -0400407 ret = intel_gvt_init_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400408 if (ret)
409 goto out_clean_workload_scheduler;
410
Zhi Wangbe1da702016-05-03 18:26:57 -0400411 ret = intel_gvt_init_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400412 if (ret)
413 goto out_clean_sched_policy;
414
Zhi Wangbe1da702016-05-03 18:26:57 -0400415 ret = init_service_thread(gvt);
416 if (ret)
417 goto out_clean_cmd_parser;
418
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800419 ret = intel_gvt_init_vgpu_types(gvt);
420 if (ret)
421 goto out_clean_thread;
422
fred gaoc5d71cb2017-09-28 11:03:02 +0800423 ret = intel_gvt_init_vgpu_type_groups(gvt);
424 if (ret == false) {
425 gvt_err("failed to init vgpu type groups: %d\n", ret);
426 goto out_clean_types;
427 }
428
Jike Song40df6ea2016-11-03 18:38:33 +0800429 ret = intel_gvt_hypervisor_host_init(&dev_priv->drm.pdev->dev, gvt,
Jike Song9ec1e662016-11-03 18:38:35 +0800430 &intel_gvt_ops);
Jike Song40df6ea2016-11-03 18:38:33 +0800431 if (ret) {
432 gvt_err("failed to register gvt-g host device: %d\n", ret);
433 goto out_clean_types;
434 }
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800435
Ping Gaoafe04fb2017-03-30 00:36:39 +0800436 vgpu = intel_gvt_create_idle_vgpu(gvt);
437 if (IS_ERR(vgpu)) {
438 ret = PTR_ERR(vgpu);
439 gvt_err("failed to create idle vgpu\n");
440 goto out_clean_types;
441 }
442 gvt->idle_vgpu = vgpu;
443
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800444 gvt_dbg_core("gvt device initialization is done\n");
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800445 dev_priv->gvt = gvt;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400446 return 0;
Zhi Wang579cea52016-06-30 12:45:34 -0400447
Jike Song40df6ea2016-11-03 18:38:33 +0800448out_clean_types:
449 intel_gvt_clean_vgpu_types(gvt);
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800450out_clean_thread:
451 clean_service_thread(gvt);
Zhi Wangbe1da702016-05-03 18:26:57 -0400452out_clean_cmd_parser:
453 intel_gvt_clean_cmd_parser(gvt);
Zhi Wang4b639602016-05-01 17:09:58 -0400454out_clean_sched_policy:
455 intel_gvt_clean_sched_policy(gvt);
Zhi Wange4734052016-05-01 07:42:16 -0400456out_clean_workload_scheduler:
457 intel_gvt_clean_workload_scheduler(gvt);
Zhi Wang04d348a2016-04-25 18:28:56 -0400458out_clean_opregion:
459 intel_gvt_clean_opregion(gvt);
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400460out_clean_gtt:
461 intel_gvt_clean_gtt(gvt);
Zhi Wang2707e442016-03-28 23:23:16 +0800462out_clean_irq:
463 intel_gvt_clean_irq(gvt);
Zhi Wangc8fe6a682015-09-17 09:22:08 +0800464out_free_firmware:
465 intel_gvt_free_firmware(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400466out_clean_mmio_info:
467 intel_gvt_clean_mmio_info(gvt);
Jike Song59c05732017-01-06 15:16:21 +0800468out_clean_idr:
469 idr_destroy(&gvt->vgpu_idr);
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +0800470 kfree(gvt);
Zhi Wang579cea52016-06-30 12:45:34 -0400471 return ret;
Zhi Wang0ad35fe2016-06-16 08:07:00 -0400472}