Merge tag 'gvt-next-2020-03-10' 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 "intel_gvt.h"
39 #include "gvt.h"
40 #include <linux/vfio.h>
41 #include <linux/mdev.h>
42
43 struct intel_gvt_host intel_gvt_host;
44
45 static const char * const supported_hypervisors[] = {
46         [INTEL_GVT_HYPERVISOR_XEN] = "XEN",
47         [INTEL_GVT_HYPERVISOR_KVM] = "KVM",
48 };
49
50 static struct intel_vgpu_type *intel_gvt_find_vgpu_type(struct intel_gvt *gvt,
51                 const char *name)
52 {
53         const char *driver_name =
54                 dev_driver_string(&gvt->gt->i915->drm.pdev->dev);
55         int i;
56
57         name += strlen(driver_name) + 1;
58         for (i = 0; i < gvt->num_types; i++) {
59                 struct intel_vgpu_type *t = &gvt->types[i];
60
61                 if (!strncmp(t->name, name, sizeof(t->name)))
62                         return t;
63         }
64
65         return NULL;
66 }
67
68 static ssize_t available_instances_show(struct kobject *kobj,
69                                         struct device *dev, char *buf)
70 {
71         struct intel_vgpu_type *type;
72         unsigned int num = 0;
73         void *gvt = kdev_to_i915(dev)->gvt;
74
75         type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
76         if (!type)
77                 num = 0;
78         else
79                 num = type->avail_instance;
80
81         return sprintf(buf, "%u\n", num);
82 }
83
84 static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
85                 char *buf)
86 {
87         return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
88 }
89
90 static ssize_t description_show(struct kobject *kobj, struct device *dev,
91                 char *buf)
92 {
93         struct intel_vgpu_type *type;
94         void *gvt = kdev_to_i915(dev)->gvt;
95
96         type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
97         if (!type)
98                 return 0;
99
100         return sprintf(buf, "low_gm_size: %dMB\nhigh_gm_size: %dMB\n"
101                        "fence: %d\nresolution: %s\n"
102                        "weight: %d\n",
103                        BYTES_TO_MB(type->low_gm_size),
104                        BYTES_TO_MB(type->high_gm_size),
105                        type->fence, vgpu_edid_str(type->resolution),
106                        type->weight);
107 }
108
109 static MDEV_TYPE_ATTR_RO(available_instances);
110 static MDEV_TYPE_ATTR_RO(device_api);
111 static MDEV_TYPE_ATTR_RO(description);
112
113 static struct attribute *gvt_type_attrs[] = {
114         &mdev_type_attr_available_instances.attr,
115         &mdev_type_attr_device_api.attr,
116         &mdev_type_attr_description.attr,
117         NULL,
118 };
119
120 static struct attribute_group *gvt_vgpu_type_groups[] = {
121         [0 ... NR_MAX_INTEL_VGPU_TYPES - 1] = NULL,
122 };
123
124 static bool intel_get_gvt_attrs(struct attribute_group ***intel_vgpu_type_groups)
125 {
126         *intel_vgpu_type_groups = gvt_vgpu_type_groups;
127         return true;
128 }
129
130 static bool intel_gvt_init_vgpu_type_groups(struct intel_gvt *gvt)
131 {
132         int i, j;
133         struct intel_vgpu_type *type;
134         struct attribute_group *group;
135
136         for (i = 0; i < gvt->num_types; i++) {
137                 type = &gvt->types[i];
138
139                 group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
140                 if (WARN_ON(!group))
141                         goto unwind;
142
143                 group->name = type->name;
144                 group->attrs = gvt_type_attrs;
145                 gvt_vgpu_type_groups[i] = group;
146         }
147
148         return true;
149
150 unwind:
151         for (j = 0; j < i; j++) {
152                 group = gvt_vgpu_type_groups[j];
153                 kfree(group);
154         }
155
156         return false;
157 }
158
159 static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt)
160 {
161         int i;
162         struct attribute_group *group;
163
164         for (i = 0; i < gvt->num_types; i++) {
165                 group = gvt_vgpu_type_groups[i];
166                 gvt_vgpu_type_groups[i] = NULL;
167                 kfree(group);
168         }
169 }
170
171 static const struct intel_gvt_ops intel_gvt_ops = {
172         .emulate_cfg_read = intel_vgpu_emulate_cfg_read,
173         .emulate_cfg_write = intel_vgpu_emulate_cfg_write,
174         .emulate_mmio_read = intel_vgpu_emulate_mmio_read,
175         .emulate_mmio_write = intel_vgpu_emulate_mmio_write,
176         .vgpu_create = intel_gvt_create_vgpu,
177         .vgpu_destroy = intel_gvt_destroy_vgpu,
178         .vgpu_release = intel_gvt_release_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         .emulate_hotplug = intel_vgpu_emulate_hotplug,
188 };
189
190 static void init_device_info(struct intel_gvt *gvt)
191 {
192         struct intel_gvt_device_info *info = &gvt->device_info;
193         struct pci_dev *pdev = gvt->gt->i915->drm.pdev;
194
195         info->max_support_vgpus = 8;
196         info->cfg_space_size = PCI_CFG_SPACE_EXP_SIZE;
197         info->mmio_size = 2 * 1024 * 1024;
198         info->mmio_bar = 0;
199         info->gtt_start_offset = 8 * 1024 * 1024;
200         info->gtt_entry_size = 8;
201         info->gtt_entry_size_shift = 3;
202         info->gmadr_bytes_in_cmd = 8;
203         info->max_surface_size = 36 * 1024 * 1024;
204         info->msi_cap_offset = pdev->msi_cap;
205 }
206
207 static int gvt_service_thread(void *data)
208 {
209         struct intel_gvt *gvt = (struct intel_gvt *)data;
210         int ret;
211
212         gvt_dbg_core("service thread start\n");
213
214         while (!kthread_should_stop()) {
215                 ret = wait_event_interruptible(gvt->service_thread_wq,
216                                 kthread_should_stop() || gvt->service_request);
217
218                 if (kthread_should_stop())
219                         break;
220
221                 if (WARN_ONCE(ret, "service thread is waken up by signal.\n"))
222                         continue;
223
224                 if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK,
225                                         (void *)&gvt->service_request))
226                         intel_gvt_emulate_vblank(gvt);
227
228                 if (test_bit(INTEL_GVT_REQUEST_SCHED,
229                                 (void *)&gvt->service_request) ||
230                         test_bit(INTEL_GVT_REQUEST_EVENT_SCHED,
231                                         (void *)&gvt->service_request)) {
232                         intel_gvt_schedule(gvt);
233                 }
234         }
235
236         return 0;
237 }
238
239 static void clean_service_thread(struct intel_gvt *gvt)
240 {
241         kthread_stop(gvt->service_thread);
242 }
243
244 static int init_service_thread(struct intel_gvt *gvt)
245 {
246         init_waitqueue_head(&gvt->service_thread_wq);
247
248         gvt->service_thread = kthread_run(gvt_service_thread,
249                         gvt, "gvt_service_thread");
250         if (IS_ERR(gvt->service_thread)) {
251                 gvt_err("fail to start service thread.\n");
252                 return PTR_ERR(gvt->service_thread);
253         }
254         return 0;
255 }
256
257 /**
258  * intel_gvt_clean_device - clean a GVT device
259  * @i915: i915 private
260  *
261  * This function is called at the driver unloading stage, to free the
262  * resources owned by a GVT device.
263  *
264  */
265 void intel_gvt_clean_device(struct drm_i915_private *i915)
266 {
267         struct intel_gvt *gvt = fetch_and_zero(&i915->gvt);
268
269         if (drm_WARN_ON(&i915->drm, !gvt))
270                 return;
271
272         intel_gvt_destroy_idle_vgpu(gvt->idle_vgpu);
273         intel_gvt_cleanup_vgpu_type_groups(gvt);
274         intel_gvt_clean_vgpu_types(gvt);
275
276         intel_gvt_debugfs_clean(gvt);
277         clean_service_thread(gvt);
278         intel_gvt_clean_cmd_parser(gvt);
279         intel_gvt_clean_sched_policy(gvt);
280         intel_gvt_clean_workload_scheduler(gvt);
281         intel_gvt_clean_gtt(gvt);
282         intel_gvt_clean_irq(gvt);
283         intel_gvt_free_firmware(gvt);
284         intel_gvt_clean_mmio_info(gvt);
285         idr_destroy(&gvt->vgpu_idr);
286
287         kfree(i915->gvt);
288 }
289
290 /**
291  * intel_gvt_init_device - initialize a GVT device
292  * @i915: drm i915 private data
293  *
294  * This function is called at the initialization stage, to initialize
295  * necessary GVT components.
296  *
297  * Returns:
298  * Zero on success, negative error code if failed.
299  *
300  */
301 int intel_gvt_init_device(struct drm_i915_private *i915)
302 {
303         struct intel_gvt *gvt;
304         struct intel_vgpu *vgpu;
305         int ret;
306
307         if (drm_WARN_ON(&i915->drm, i915->gvt))
308                 return -EEXIST;
309
310         gvt = kzalloc(sizeof(struct intel_gvt), GFP_KERNEL);
311         if (!gvt)
312                 return -ENOMEM;
313
314         gvt_dbg_core("init gvt device\n");
315
316         idr_init(&gvt->vgpu_idr);
317         spin_lock_init(&gvt->scheduler.mmio_context_lock);
318         mutex_init(&gvt->lock);
319         mutex_init(&gvt->sched_lock);
320         gvt->gt = &i915->gt;
321         i915->gvt = gvt;
322
323         init_device_info(gvt);
324
325         ret = intel_gvt_setup_mmio_info(gvt);
326         if (ret)
327                 goto out_clean_idr;
328
329         intel_gvt_init_engine_mmio_context(gvt);
330
331         ret = intel_gvt_load_firmware(gvt);
332         if (ret)
333                 goto out_clean_mmio_info;
334
335         ret = intel_gvt_init_irq(gvt);
336         if (ret)
337                 goto out_free_firmware;
338
339         ret = intel_gvt_init_gtt(gvt);
340         if (ret)
341                 goto out_clean_irq;
342
343         ret = intel_gvt_init_workload_scheduler(gvt);
344         if (ret)
345                 goto out_clean_gtt;
346
347         ret = intel_gvt_init_sched_policy(gvt);
348         if (ret)
349                 goto out_clean_workload_scheduler;
350
351         ret = intel_gvt_init_cmd_parser(gvt);
352         if (ret)
353                 goto out_clean_sched_policy;
354
355         ret = init_service_thread(gvt);
356         if (ret)
357                 goto out_clean_cmd_parser;
358
359         ret = intel_gvt_init_vgpu_types(gvt);
360         if (ret)
361                 goto out_clean_thread;
362
363         ret = intel_gvt_init_vgpu_type_groups(gvt);
364         if (ret == false) {
365                 gvt_err("failed to init vgpu type groups: %d\n", ret);
366                 goto out_clean_types;
367         }
368
369         vgpu = intel_gvt_create_idle_vgpu(gvt);
370         if (IS_ERR(vgpu)) {
371                 ret = PTR_ERR(vgpu);
372                 gvt_err("failed to create idle vgpu\n");
373                 goto out_clean_types;
374         }
375         gvt->idle_vgpu = vgpu;
376
377         intel_gvt_debugfs_init(gvt);
378
379         gvt_dbg_core("gvt device initialization is done\n");
380         intel_gvt_host.dev = &i915->drm.pdev->dev;
381         intel_gvt_host.initialized = true;
382         return 0;
383
384 out_clean_types:
385         intel_gvt_clean_vgpu_types(gvt);
386 out_clean_thread:
387         clean_service_thread(gvt);
388 out_clean_cmd_parser:
389         intel_gvt_clean_cmd_parser(gvt);
390 out_clean_sched_policy:
391         intel_gvt_clean_sched_policy(gvt);
392 out_clean_workload_scheduler:
393         intel_gvt_clean_workload_scheduler(gvt);
394 out_clean_gtt:
395         intel_gvt_clean_gtt(gvt);
396 out_clean_irq:
397         intel_gvt_clean_irq(gvt);
398 out_free_firmware:
399         intel_gvt_free_firmware(gvt);
400 out_clean_mmio_info:
401         intel_gvt_clean_mmio_info(gvt);
402 out_clean_idr:
403         idr_destroy(&gvt->vgpu_idr);
404         kfree(gvt);
405         i915->gvt = NULL;
406         return ret;
407 }
408
409 int
410 intel_gvt_register_hypervisor(struct intel_gvt_mpt *m)
411 {
412         int ret;
413         void *gvt;
414
415         if (!intel_gvt_host.initialized)
416                 return -ENODEV;
417
418         if (m->type != INTEL_GVT_HYPERVISOR_KVM &&
419             m->type != INTEL_GVT_HYPERVISOR_XEN)
420                 return -EINVAL;
421
422         /* Get a reference for device model module */
423         if (!try_module_get(THIS_MODULE))
424                 return -ENODEV;
425
426         intel_gvt_host.mpt = m;
427         intel_gvt_host.hypervisor_type = m->type;
428         gvt = (void *)kdev_to_i915(intel_gvt_host.dev)->gvt;
429
430         ret = intel_gvt_hypervisor_host_init(intel_gvt_host.dev, gvt,
431                                              &intel_gvt_ops);
432         if (ret < 0) {
433                 gvt_err("Failed to init %s hypervisor module\n",
434                         supported_hypervisors[intel_gvt_host.hypervisor_type]);
435                 module_put(THIS_MODULE);
436                 return -ENODEV;
437         }
438         gvt_dbg_core("Running with hypervisor %s in host mode\n",
439                      supported_hypervisors[intel_gvt_host.hypervisor_type]);
440         return 0;
441 }
442 EXPORT_SYMBOL_GPL(intel_gvt_register_hypervisor);
443
444 void
445 intel_gvt_unregister_hypervisor(void)
446 {
447         intel_gvt_hypervisor_host_exit(intel_gvt_host.dev);
448         module_put(THIS_MODULE);
449 }
450 EXPORT_SYMBOL_GPL(intel_gvt_unregister_hypervisor);