]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/gvt/gvt.c
drm/i915/gvt: vGPU graphics memory virtualization
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / gvt / gvt.c
CommitLineData
0ad35fed
ZW
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.
12d14cc4
ZW
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 *
0ad35fed
ZW
31 */
32
33#include <linux/types.h>
34#include <xen/xen.h>
35
36#include "i915_drv.h"
37
38struct intel_gvt_host intel_gvt_host;
39
40static const char * const supported_hypervisors[] = {
41 [INTEL_GVT_HYPERVISOR_XEN] = "XEN",
42 [INTEL_GVT_HYPERVISOR_KVM] = "KVM",
43};
44
45/**
46 * intel_gvt_init_host - Load MPT modules and detect if we're running in host
47 * @gvt: intel gvt device
48 *
49 * This function is called at the driver loading stage. If failed to find a
50 * loadable MPT module or detect currently we're running in a VM, then GVT-g
51 * will be disabled
52 *
53 * Returns:
54 * Zero on success, negative error code if failed.
55 *
56 */
57int intel_gvt_init_host(void)
58{
59 if (intel_gvt_host.initialized)
60 return 0;
61
62 /* Xen DOM U */
63 if (xen_domain() && !xen_initial_domain())
64 return -ENODEV;
65
66 /* Try to load MPT modules for hypervisors */
67 if (xen_initial_domain()) {
68 /* In Xen dom0 */
69 intel_gvt_host.mpt = try_then_request_module(
70 symbol_get(xengt_mpt), "xengt");
71 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_XEN;
72 } else {
73 /* not in Xen. Try KVMGT */
74 intel_gvt_host.mpt = try_then_request_module(
75 symbol_get(kvmgt_mpt), "kvm");
76 intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_KVM;
77 }
78
79 /* Fail to load MPT modules - bail out */
80 if (!intel_gvt_host.mpt)
81 return -EINVAL;
82
83 /* Try to detect if we're running in host instead of VM. */
84 if (!intel_gvt_hypervisor_detect_host())
85 return -ENODEV;
86
87 gvt_dbg_core("Running with hypervisor %s in host mode\n",
88 supported_hypervisors[intel_gvt_host.hypervisor_type]);
89
90 intel_gvt_host.initialized = true;
91 return 0;
92}
93
94static void init_device_info(struct intel_gvt *gvt)
95{
12d14cc4
ZW
96 struct intel_gvt_device_info *info = &gvt->device_info;
97
98 if (IS_BROADWELL(gvt->dev_priv) || IS_SKYLAKE(gvt->dev_priv)) {
99 info->max_support_vgpus = 8;
579cea5f 100 info->cfg_space_size = 256;
c8fe6a68 101 info->mmio_size = 2 * 1024 * 1024;
579cea5f 102 info->mmio_bar = 0;
c8fe6a68 103 info->msi_cap_offset = IS_SKYLAKE(gvt->dev_priv) ? 0xac : 0x90;
2707e444
ZW
104 info->gtt_start_offset = 8 * 1024 * 1024;
105 info->gtt_entry_size = 8;
106 info->gtt_entry_size_shift = 3;
12d14cc4 107 }
0ad35fed
ZW
108}
109
110/**
111 * intel_gvt_clean_device - clean a GVT device
112 * @gvt: intel gvt device
113 *
114 * This function is called at the driver unloading stage, to free the
115 * resources owned by a GVT device.
116 *
117 */
118void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
119{
120 struct intel_gvt *gvt = &dev_priv->gvt;
121
122 if (WARN_ON(!gvt->initialized))
123 return;
124
2707e444 125 intel_gvt_clean_gtt(gvt);
c8fe6a68 126 intel_gvt_clean_irq(gvt);
12d14cc4 127 intel_gvt_clean_mmio_info(gvt);
579cea5f 128 intel_gvt_free_firmware(gvt);
0ad35fed
ZW
129
130 gvt->initialized = false;
131}
132
133/**
134 * intel_gvt_init_device - initialize a GVT device
135 * @dev_priv: drm i915 private data
136 *
137 * This function is called at the initialization stage, to initialize
138 * necessary GVT components.
139 *
140 * Returns:
141 * Zero on success, negative error code if failed.
142 *
143 */
144int intel_gvt_init_device(struct drm_i915_private *dev_priv)
145{
146 struct intel_gvt *gvt = &dev_priv->gvt;
12d14cc4
ZW
147 int ret;
148
0ad35fed
ZW
149 /*
150 * Cannot initialize GVT device without intel_gvt_host gets
151 * initialized first.
152 */
153 if (WARN_ON(!intel_gvt_host.initialized))
154 return -EINVAL;
155
156 if (WARN_ON(gvt->initialized))
157 return -EEXIST;
158
159 gvt_dbg_core("init gvt device\n");
160
28a60dee
ZW
161 mutex_init(&gvt->lock);
162 gvt->dev_priv = dev_priv;
163
0ad35fed 164 init_device_info(gvt);
12d14cc4
ZW
165
166 ret = intel_gvt_setup_mmio_info(gvt);
167 if (ret)
168 return ret;
169
579cea5f
ZW
170 ret = intel_gvt_load_firmware(gvt);
171 if (ret)
172 goto out_clean_mmio_info;
173
c8fe6a68
ZW
174 ret = intel_gvt_init_irq(gvt);
175 if (ret)
176 goto out_free_firmware;
177
2707e444
ZW
178 ret = intel_gvt_init_gtt(gvt);
179 if (ret)
180 goto out_clean_irq;
181
0ad35fed
ZW
182 gvt_dbg_core("gvt device creation is done\n");
183 gvt->initialized = true;
184 return 0;
579cea5f 185
2707e444
ZW
186out_clean_irq:
187 intel_gvt_clean_irq(gvt);
c8fe6a68
ZW
188out_free_firmware:
189 intel_gvt_free_firmware(gvt);
579cea5f
ZW
190out_clean_mmio_info:
191 intel_gvt_clean_mmio_info(gvt);
192 return ret;
0ad35fed 193}