Merge tag 'drm/tegra/for-5.13-rc1' of ssh://git.freedesktop.org/git/tegra/linux into...
[linux-2.6-microblaze.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 <linux/kthread.h>
35
36 #include "i915_drv.h"
37 #include "intel_gvt.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         const char *driver_name =
53                 dev_driver_string(gvt->gt->i915->drm.dev);
54         int i;
55
56         name += strlen(driver_name) + 1;
57         for (i = 0; i < gvt->num_types; i++) {
58                 struct intel_vgpu_type *t = &gvt->types[i];
59
60                 if (!strncmp(t->name, name, 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_group ***intel_vgpu_type_groups)
124 {
125         *intel_vgpu_type_groups = gvt_vgpu_type_groups;
126         return true;
127 }
128
129 static bool intel_gvt_init_vgpu_type_groups(struct intel_gvt *gvt)
130 {
131         int i, j;
132         struct intel_vgpu_type *type;
133         struct attribute_group *group;
134
135         for (i = 0; i < gvt->num_types; i++) {
136                 type = &gvt->types[i];
137
138                 group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
139                 if (WARN_ON(!group))
140                         goto unwind;
141
142                 group->name = type->name;
143                 group->attrs = gvt_type_attrs;
144                 gvt_vgpu_type_groups[i] = group;
145         }
146
147         return true;
148
149 unwind:
150         for (j = 0; j < i; j++) {
151                 group = gvt_vgpu_type_groups[j];
152                 kfree(group);
153         }
154
155         return false;
156 }
157
158 static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt)
159 {
160         int i;
161         struct attribute_group *group;
162
163         for (i = 0; i < gvt->num_types; i++) {
164                 group = gvt_vgpu_type_groups[i];
165                 gvt_vgpu_type_groups[i] = NULL;
166                 kfree(group);
167         }
168 }
169
170 static const struct intel_gvt_ops intel_gvt_ops = {
171         .emulate_cfg_read = intel_vgpu_emulate_cfg_read,
172         .emulate_cfg_write = intel_vgpu_emulate_cfg_write,
173         .emulate_mmio_read = intel_vgpu_emulate_mmio_read,
174         .emulate_mmio_write = intel_vgpu_emulate_mmio_write,
175         .vgpu_create = intel_gvt_create_vgpu,
176         .vgpu_destroy = intel_gvt_destroy_vgpu,
177         .vgpu_release = intel_gvt_release_vgpu,
178         .vgpu_reset = intel_gvt_reset_vgpu,
179         .vgpu_activate = intel_gvt_activate_vgpu,
180         .vgpu_deactivate = intel_gvt_deactivate_vgpu,
181         .gvt_find_vgpu_type = intel_gvt_find_vgpu_type,
182         .get_gvt_attrs = intel_get_gvt_attrs,
183         .vgpu_query_plane = intel_vgpu_query_plane,
184         .vgpu_get_dmabuf = intel_vgpu_get_dmabuf,
185         .write_protect_handler = intel_vgpu_page_track_handler,
186         .emulate_hotplug = intel_vgpu_emulate_hotplug,
187 };
188
189 static void init_device_info(struct intel_gvt *gvt)
190 {
191         struct intel_gvt_device_info *info = &gvt->device_info;
192         struct pci_dev *pdev = to_pci_dev(gvt->gt->i915->drm.dev);
193
194         info->max_support_vgpus = 8;
195         info->cfg_space_size = PCI_CFG_SPACE_EXP_SIZE;
196         info->mmio_size = 2 * 1024 * 1024;
197         info->mmio_bar = 0;
198         info->gtt_start_offset = 8 * 1024 * 1024;
199         info->gtt_entry_size = 8;
200         info->gtt_entry_size_shift = 3;
201         info->gmadr_bytes_in_cmd = 8;
202         info->max_surface_size = 36 * 1024 * 1024;
203         info->msi_cap_offset = pdev->msi_cap;
204 }
205
206 static void intel_gvt_test_and_emulate_vblank(struct intel_gvt *gvt)
207 {
208         struct intel_vgpu *vgpu;
209         int id;
210
211         mutex_lock(&gvt->lock);
212         idr_for_each_entry((&(gvt)->vgpu_idr), (vgpu), (id)) {
213                 if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK + id,
214                                        (void *)&gvt->service_request)) {
215                         if (vgpu->active)
216                                 intel_vgpu_emulate_vblank(vgpu);
217                 }
218         }
219         mutex_unlock(&gvt->lock);
220 }
221
222 static int gvt_service_thread(void *data)
223 {
224         struct intel_gvt *gvt = (struct intel_gvt *)data;
225         int ret;
226
227         gvt_dbg_core("service thread start\n");
228
229         while (!kthread_should_stop()) {
230                 ret = wait_event_interruptible(gvt->service_thread_wq,
231                                 kthread_should_stop() || gvt->service_request);
232
233                 if (kthread_should_stop())
234                         break;
235
236                 if (WARN_ONCE(ret, "service thread is waken up by signal.\n"))
237                         continue;
238
239                 intel_gvt_test_and_emulate_vblank(gvt);
240
241                 if (test_bit(INTEL_GVT_REQUEST_SCHED,
242                                 (void *)&gvt->service_request) ||
243                         test_bit(INTEL_GVT_REQUEST_EVENT_SCHED,
244                                         (void *)&gvt->service_request)) {
245                         intel_gvt_schedule(gvt);
246                 }
247         }
248
249         return 0;
250 }
251
252 static void clean_service_thread(struct intel_gvt *gvt)
253 {
254         kthread_stop(gvt->service_thread);
255 }
256
257 static int init_service_thread(struct intel_gvt *gvt)
258 {
259         init_waitqueue_head(&gvt->service_thread_wq);
260
261         gvt->service_thread = kthread_run(gvt_service_thread,
262                         gvt, "gvt_service_thread");
263         if (IS_ERR(gvt->service_thread)) {
264                 gvt_err("fail to start service thread.\n");
265                 return PTR_ERR(gvt->service_thread);
266         }
267         return 0;
268 }
269
270 /**
271  * intel_gvt_clean_device - clean a GVT device
272  * @i915: i915 private
273  *
274  * This function is called at the driver unloading stage, to free the
275  * resources owned by a GVT device.
276  *
277  */
278 void intel_gvt_clean_device(struct drm_i915_private *i915)
279 {
280         struct intel_gvt *gvt = fetch_and_zero(&i915->gvt);
281
282         if (drm_WARN_ON(&i915->drm, !gvt))
283                 return;
284
285         intel_gvt_destroy_idle_vgpu(gvt->idle_vgpu);
286         intel_gvt_cleanup_vgpu_type_groups(gvt);
287         intel_gvt_clean_vgpu_types(gvt);
288
289         intel_gvt_debugfs_clean(gvt);
290         clean_service_thread(gvt);
291         intel_gvt_clean_cmd_parser(gvt);
292         intel_gvt_clean_sched_policy(gvt);
293         intel_gvt_clean_workload_scheduler(gvt);
294         intel_gvt_clean_gtt(gvt);
295         intel_gvt_free_firmware(gvt);
296         intel_gvt_clean_mmio_info(gvt);
297         idr_destroy(&gvt->vgpu_idr);
298
299         kfree(i915->gvt);
300 }
301
302 /**
303  * intel_gvt_init_device - initialize a GVT device
304  * @i915: drm i915 private data
305  *
306  * This function is called at the initialization stage, to initialize
307  * necessary GVT components.
308  *
309  * Returns:
310  * Zero on success, negative error code if failed.
311  *
312  */
313 int intel_gvt_init_device(struct drm_i915_private *i915)
314 {
315         struct intel_gvt *gvt;
316         struct intel_vgpu *vgpu;
317         int ret;
318
319         if (drm_WARN_ON(&i915->drm, i915->gvt))
320                 return -EEXIST;
321
322         gvt = kzalloc(sizeof(struct intel_gvt), GFP_KERNEL);
323         if (!gvt)
324                 return -ENOMEM;
325
326         gvt_dbg_core("init gvt device\n");
327
328         idr_init_base(&gvt->vgpu_idr, 1);
329         spin_lock_init(&gvt->scheduler.mmio_context_lock);
330         mutex_init(&gvt->lock);
331         mutex_init(&gvt->sched_lock);
332         gvt->gt = &i915->gt;
333         i915->gvt = gvt;
334
335         init_device_info(gvt);
336
337         ret = intel_gvt_setup_mmio_info(gvt);
338         if (ret)
339                 goto out_clean_idr;
340
341         intel_gvt_init_engine_mmio_context(gvt);
342
343         ret = intel_gvt_load_firmware(gvt);
344         if (ret)
345                 goto out_clean_mmio_info;
346
347         ret = intel_gvt_init_irq(gvt);
348         if (ret)
349                 goto out_free_firmware;
350
351         ret = intel_gvt_init_gtt(gvt);
352         if (ret)
353                 goto out_free_firmware;
354
355         ret = intel_gvt_init_workload_scheduler(gvt);
356         if (ret)
357                 goto out_clean_gtt;
358
359         ret = intel_gvt_init_sched_policy(gvt);
360         if (ret)
361                 goto out_clean_workload_scheduler;
362
363         ret = intel_gvt_init_cmd_parser(gvt);
364         if (ret)
365                 goto out_clean_sched_policy;
366
367         ret = init_service_thread(gvt);
368         if (ret)
369                 goto out_clean_cmd_parser;
370
371         ret = intel_gvt_init_vgpu_types(gvt);
372         if (ret)
373                 goto out_clean_thread;
374
375         ret = intel_gvt_init_vgpu_type_groups(gvt);
376         if (ret == false) {
377                 gvt_err("failed to init vgpu type groups: %d\n", ret);
378                 goto out_clean_types;
379         }
380
381         vgpu = intel_gvt_create_idle_vgpu(gvt);
382         if (IS_ERR(vgpu)) {
383                 ret = PTR_ERR(vgpu);
384                 gvt_err("failed to create idle vgpu\n");
385                 goto out_clean_types;
386         }
387         gvt->idle_vgpu = vgpu;
388
389         intel_gvt_debugfs_init(gvt);
390
391         gvt_dbg_core("gvt device initialization is done\n");
392         intel_gvt_host.dev = i915->drm.dev;
393         intel_gvt_host.initialized = true;
394         return 0;
395
396 out_clean_types:
397         intel_gvt_clean_vgpu_types(gvt);
398 out_clean_thread:
399         clean_service_thread(gvt);
400 out_clean_cmd_parser:
401         intel_gvt_clean_cmd_parser(gvt);
402 out_clean_sched_policy:
403         intel_gvt_clean_sched_policy(gvt);
404 out_clean_workload_scheduler:
405         intel_gvt_clean_workload_scheduler(gvt);
406 out_clean_gtt:
407         intel_gvt_clean_gtt(gvt);
408 out_free_firmware:
409         intel_gvt_free_firmware(gvt);
410 out_clean_mmio_info:
411         intel_gvt_clean_mmio_info(gvt);
412 out_clean_idr:
413         idr_destroy(&gvt->vgpu_idr);
414         kfree(gvt);
415         i915->gvt = NULL;
416         return ret;
417 }
418
419 int
420 intel_gvt_pm_resume(struct intel_gvt *gvt)
421 {
422         intel_gvt_restore_fence(gvt);
423         intel_gvt_restore_mmio(gvt);
424         intel_gvt_restore_ggtt(gvt);
425         return 0;
426 }
427
428 int
429 intel_gvt_register_hypervisor(const struct intel_gvt_mpt *m)
430 {
431         int ret;
432         void *gvt;
433
434         if (!intel_gvt_host.initialized)
435                 return -ENODEV;
436
437         if (m->type != INTEL_GVT_HYPERVISOR_KVM &&
438             m->type != INTEL_GVT_HYPERVISOR_XEN)
439                 return -EINVAL;
440
441         /* Get a reference for device model module */
442         if (!try_module_get(THIS_MODULE))
443                 return -ENODEV;
444
445         intel_gvt_host.mpt = m;
446         intel_gvt_host.hypervisor_type = m->type;
447         gvt = (void *)kdev_to_i915(intel_gvt_host.dev)->gvt;
448
449         ret = intel_gvt_hypervisor_host_init(intel_gvt_host.dev, gvt,
450                                              &intel_gvt_ops);
451         if (ret < 0) {
452                 gvt_err("Failed to init %s hypervisor module\n",
453                         supported_hypervisors[intel_gvt_host.hypervisor_type]);
454                 module_put(THIS_MODULE);
455                 return -ENODEV;
456         }
457         gvt_dbg_core("Running with hypervisor %s in host mode\n",
458                      supported_hypervisors[intel_gvt_host.hypervisor_type]);
459         return 0;
460 }
461 EXPORT_SYMBOL_GPL(intel_gvt_register_hypervisor);
462
463 void
464 intel_gvt_unregister_hypervisor(void)
465 {
466         intel_gvt_hypervisor_host_exit(intel_gvt_host.dev);
467         module_put(THIS_MODULE);
468 }
469 EXPORT_SYMBOL_GPL(intel_gvt_unregister_hypervisor);