Merge tag 'gvt-next-2018-09-04' of https://github.com/intel/gvt-linux into drm-intel...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / i915 / gvt / gvt.c
1 /*
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.
22  *
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  *
31  */
32
33 #include <linux/types.h>
34 #include <xen/xen.h>
35 #include <linux/kthread.h>
36
37 #include "i915_drv.h"
38 #include "gvt.h"
39 #include <linux/vfio.h>
40 #include <linux/mdev.h>
41
42 struct intel_gvt_host intel_gvt_host;
43
44 static const char * const supported_hypervisors[] = {
45         [INTEL_GVT_HYPERVISOR_XEN] = "XEN",
46         [INTEL_GVT_HYPERVISOR_KVM] = "KVM",
47 };
48
49 static 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
67 static 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
83 static 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
89 static 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
108 static MDEV_TYPE_ATTR_RO(available_instances);
109 static MDEV_TYPE_ATTR_RO(device_api);
110 static MDEV_TYPE_ATTR_RO(description);
111
112 static 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
119 static struct attribute_group *gvt_vgpu_type_groups[] = {
120         [0 ... NR_MAX_INTEL_VGPU_TYPES - 1] = NULL,
121 };
122
123 static 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
131 static 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
151 unwind:
152         for (j = 0; j < i; j++) {
153                 group = gvt_vgpu_type_groups[j];
154                 kfree(group);
155         }
156
157         return false;
158 }
159
160 static 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
172 static const struct intel_gvt_ops intel_gvt_ops = {
173         .emulate_cfg_read = intel_vgpu_emulate_cfg_read,
174         .emulate_cfg_write = intel_vgpu_emulate_cfg_write,
175         .emulate_mmio_read = intel_vgpu_emulate_mmio_read,
176         .emulate_mmio_write = intel_vgpu_emulate_mmio_write,
177         .vgpu_create = intel_gvt_create_vgpu,
178         .vgpu_destroy = intel_gvt_destroy_vgpu,
179         .vgpu_reset = intel_gvt_reset_vgpu,
180         .vgpu_activate = intel_gvt_activate_vgpu,
181         .vgpu_deactivate = intel_gvt_deactivate_vgpu,
182         .gvt_find_vgpu_type = intel_gvt_find_vgpu_type,
183         .get_gvt_attrs = intel_get_gvt_attrs,
184         .vgpu_query_plane = intel_vgpu_query_plane,
185         .vgpu_get_dmabuf = intel_vgpu_get_dmabuf,
186         .write_protect_handler = intel_vgpu_page_track_handler,
187 };
188
189 /**
190  * intel_gvt_init_host - Load MPT modules and detect if we're running in host
191  *
192  * This function is called at the driver loading stage. If failed to find a
193  * loadable MPT module or detect currently we're running in a VM, then GVT-g
194  * will be disabled
195  *
196  * Returns:
197  * Zero on success, negative error code if failed.
198  *
199  */
200 int intel_gvt_init_host(void)
201 {
202         if (intel_gvt_host.initialized)
203                 return 0;
204
205         /* Xen DOM U */
206         if (xen_domain() && !xen_initial_domain())
207                 return -ENODEV;
208
209         /* Try to load MPT modules for hypervisors */
210         if (xen_initial_domain()) {
211                 /* In Xen dom0 */
212                 intel_gvt_host.mpt = try_then_request_module(
213                                 symbol_get(xengt_mpt), "xengt");
214                 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_XEN;
215         } else {
216 #if IS_ENABLED(CONFIG_DRM_I915_GVT_KVMGT)
217                 /* not in Xen. Try KVMGT */
218                 intel_gvt_host.mpt = try_then_request_module(
219                                 symbol_get(kvmgt_mpt), "kvmgt");
220                 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_KVM;
221 #endif
222         }
223
224         /* Fail to load MPT modules - bail out */
225         if (!intel_gvt_host.mpt)
226                 return -EINVAL;
227
228         gvt_dbg_core("Running with hypervisor %s in host mode\n",
229                         supported_hypervisors[intel_gvt_host.hypervisor_type]);
230
231         intel_gvt_host.initialized = true;
232         return 0;
233 }
234
235 static void init_device_info(struct intel_gvt *gvt)
236 {
237         struct intel_gvt_device_info *info = &gvt->device_info;
238         struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
239
240         info->max_support_vgpus = 8;
241         info->cfg_space_size = PCI_CFG_SPACE_EXP_SIZE;
242         info->mmio_size = 2 * 1024 * 1024;
243         info->mmio_bar = 0;
244         info->gtt_start_offset = 8 * 1024 * 1024;
245         info->gtt_entry_size = 8;
246         info->gtt_entry_size_shift = 3;
247         info->gmadr_bytes_in_cmd = 8;
248         info->max_surface_size = 36 * 1024 * 1024;
249         info->msi_cap_offset = pdev->msi_cap;
250 }
251
252 static int gvt_service_thread(void *data)
253 {
254         struct intel_gvt *gvt = (struct intel_gvt *)data;
255         int ret;
256
257         gvt_dbg_core("service thread start\n");
258
259         while (!kthread_should_stop()) {
260                 ret = wait_event_interruptible(gvt->service_thread_wq,
261                                 kthread_should_stop() || gvt->service_request);
262
263                 if (kthread_should_stop())
264                         break;
265
266                 if (WARN_ONCE(ret, "service thread is waken up by signal.\n"))
267                         continue;
268
269                 if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK,
270                                         (void *)&gvt->service_request))
271                         intel_gvt_emulate_vblank(gvt);
272
273                 if (test_bit(INTEL_GVT_REQUEST_SCHED,
274                                 (void *)&gvt->service_request) ||
275                         test_bit(INTEL_GVT_REQUEST_EVENT_SCHED,
276                                         (void *)&gvt->service_request)) {
277                         intel_gvt_schedule(gvt);
278                 }
279         }
280
281         return 0;
282 }
283
284 static void clean_service_thread(struct intel_gvt *gvt)
285 {
286         kthread_stop(gvt->service_thread);
287 }
288
289 static int init_service_thread(struct intel_gvt *gvt)
290 {
291         init_waitqueue_head(&gvt->service_thread_wq);
292
293         gvt->service_thread = kthread_run(gvt_service_thread,
294                         gvt, "gvt_service_thread");
295         if (IS_ERR(gvt->service_thread)) {
296                 gvt_err("fail to start service thread.\n");
297                 return PTR_ERR(gvt->service_thread);
298         }
299         return 0;
300 }
301
302 /**
303  * intel_gvt_clean_device - clean a GVT device
304  * @dev_priv: i915 private
305  *
306  * This function is called at the driver unloading stage, to free the
307  * resources owned by a GVT device.
308  *
309  */
310 void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
311 {
312         struct intel_gvt *gvt = to_gvt(dev_priv);
313
314         if (WARN_ON(!gvt))
315                 return;
316
317         intel_gvt_debugfs_clean(gvt);
318         clean_service_thread(gvt);
319         intel_gvt_clean_cmd_parser(gvt);
320         intel_gvt_clean_sched_policy(gvt);
321         intel_gvt_clean_workload_scheduler(gvt);
322         intel_gvt_clean_gtt(gvt);
323         intel_gvt_clean_irq(gvt);
324         intel_gvt_clean_mmio_info(gvt);
325         intel_gvt_free_firmware(gvt);
326
327         intel_gvt_hypervisor_host_exit(&dev_priv->drm.pdev->dev, gvt);
328         intel_gvt_cleanup_vgpu_type_groups(gvt);
329         intel_gvt_clean_vgpu_types(gvt);
330
331         idr_destroy(&gvt->vgpu_idr);
332
333         intel_gvt_destroy_idle_vgpu(gvt->idle_vgpu);
334
335         kfree(dev_priv->gvt);
336         dev_priv->gvt = NULL;
337 }
338
339 /**
340  * intel_gvt_init_device - initialize a GVT device
341  * @dev_priv: drm i915 private data
342  *
343  * This function is called at the initialization stage, to initialize
344  * necessary GVT components.
345  *
346  * Returns:
347  * Zero on success, negative error code if failed.
348  *
349  */
350 int intel_gvt_init_device(struct drm_i915_private *dev_priv)
351 {
352         struct intel_gvt *gvt;
353         struct intel_vgpu *vgpu;
354         int ret;
355
356         /*
357          * Cannot initialize GVT device without intel_gvt_host gets
358          * initialized first.
359          */
360         if (WARN_ON(!intel_gvt_host.initialized))
361                 return -EINVAL;
362
363         if (WARN_ON(dev_priv->gvt))
364                 return -EEXIST;
365
366         gvt = kzalloc(sizeof(struct intel_gvt), GFP_KERNEL);
367         if (!gvt)
368                 return -ENOMEM;
369
370         gvt_dbg_core("init gvt device\n");
371
372         idr_init(&gvt->vgpu_idr);
373         spin_lock_init(&gvt->scheduler.mmio_context_lock);
374         mutex_init(&gvt->lock);
375         mutex_init(&gvt->sched_lock);
376         gvt->dev_priv = dev_priv;
377
378         init_device_info(gvt);
379
380         ret = intel_gvt_setup_mmio_info(gvt);
381         if (ret)
382                 goto out_clean_idr;
383
384         intel_gvt_init_engine_mmio_context(gvt);
385
386         ret = intel_gvt_load_firmware(gvt);
387         if (ret)
388                 goto out_clean_mmio_info;
389
390         ret = intel_gvt_init_irq(gvt);
391         if (ret)
392                 goto out_free_firmware;
393
394         ret = intel_gvt_init_gtt(gvt);
395         if (ret)
396                 goto out_clean_irq;
397
398         ret = intel_gvt_init_workload_scheduler(gvt);
399         if (ret)
400                 goto out_clean_gtt;
401
402         ret = intel_gvt_init_sched_policy(gvt);
403         if (ret)
404                 goto out_clean_workload_scheduler;
405
406         ret = intel_gvt_init_cmd_parser(gvt);
407         if (ret)
408                 goto out_clean_sched_policy;
409
410         ret = init_service_thread(gvt);
411         if (ret)
412                 goto out_clean_cmd_parser;
413
414         ret = intel_gvt_init_vgpu_types(gvt);
415         if (ret)
416                 goto out_clean_thread;
417
418         ret = intel_gvt_init_vgpu_type_groups(gvt);
419         if (ret == false) {
420                 gvt_err("failed to init vgpu type groups: %d\n", ret);
421                 goto out_clean_types;
422         }
423
424         ret = intel_gvt_hypervisor_host_init(&dev_priv->drm.pdev->dev, gvt,
425                                 &intel_gvt_ops);
426         if (ret) {
427                 gvt_err("failed to register gvt-g host device: %d\n", ret);
428                 goto out_clean_types;
429         }
430
431         vgpu = intel_gvt_create_idle_vgpu(gvt);
432         if (IS_ERR(vgpu)) {
433                 ret = PTR_ERR(vgpu);
434                 gvt_err("failed to create idle vgpu\n");
435                 goto out_clean_types;
436         }
437         gvt->idle_vgpu = vgpu;
438
439         ret = intel_gvt_debugfs_init(gvt);
440         if (ret)
441                 gvt_err("debugfs registeration failed, go on.\n");
442
443         gvt_dbg_core("gvt device initialization is done\n");
444         dev_priv->gvt = gvt;
445         return 0;
446
447 out_clean_types:
448         intel_gvt_clean_vgpu_types(gvt);
449 out_clean_thread:
450         clean_service_thread(gvt);
451 out_clean_cmd_parser:
452         intel_gvt_clean_cmd_parser(gvt);
453 out_clean_sched_policy:
454         intel_gvt_clean_sched_policy(gvt);
455 out_clean_workload_scheduler:
456         intel_gvt_clean_workload_scheduler(gvt);
457 out_clean_gtt:
458         intel_gvt_clean_gtt(gvt);
459 out_clean_irq:
460         intel_gvt_clean_irq(gvt);
461 out_free_firmware:
462         intel_gvt_free_firmware(gvt);
463 out_clean_mmio_info:
464         intel_gvt_clean_mmio_info(gvt);
465 out_clean_idr:
466         idr_destroy(&gvt->vgpu_idr);
467         kfree(gvt);
468         return ret;
469 }
470
471 #if IS_ENABLED(CONFIG_DRM_I915_GVT_KVMGT)
472 MODULE_SOFTDEP("pre: kvmgt");
473 #endif