blob: 5fe9f3f3946728aa7985ab6f251f685bc97cfe6b [file] [log] [blame]
Yu Zhangcf9d2892015-02-10 19:05:47 +08001/*
2 * Copyright(c) 2011-2015 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
24#include "intel_drv.h"
25#include "i915_vgpu.h"
26
27/**
28 * DOC: Intel GVT-g guest support
29 *
30 * Intel GVT-g is a graphics virtualization technology which shares the
31 * GPU among multiple virtual machines on a time-sharing basis. Each
32 * virtual machine is presented a virtual GPU (vGPU), which has equivalent
33 * features as the underlying physical GPU (pGPU), so i915 driver can run
34 * seamlessly in a virtual machine. This file provides vGPU specific
35 * optimizations when running in a virtual machine, to reduce the complexity
36 * of vGPU emulation and to improve the overall performance.
37 *
38 * A primary function introduced here is so-called "address space ballooning"
39 * technique. Intel GVT-g partitions global graphics memory among multiple VMs,
40 * so each VM can directly access a portion of the memory without hypervisor's
41 * intervention, e.g. filling textures or queuing commands. However with the
42 * partitioning an unmodified i915 driver would assume a smaller graphics
43 * memory starting from address ZERO, then requires vGPU emulation module to
44 * translate the graphics address between 'guest view' and 'host view', for
45 * all registers and command opcodes which contain a graphics memory address.
46 * To reduce the complexity, Intel GVT-g introduces "address space ballooning",
47 * by telling the exact partitioning knowledge to each guest i915 driver, which
48 * then reserves and prevents non-allocated portions from allocation. Thus vGPU
49 * emulation module only needs to scan and validate graphics addresses without
50 * complexity of address translation.
51 *
52 */
53
54/**
55 * i915_check_vgpu - detect virtual GPU
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +010056 * @dev_priv: i915 device private
Yu Zhangcf9d2892015-02-10 19:05:47 +080057 *
58 * This function is called at the initialization stage, to detect whether
59 * running on a vGPU.
60 */
Chris Wilsondc979972016-05-10 14:10:04 +010061void i915_check_vgpu(struct drm_i915_private *dev_priv)
Yu Zhangcf9d2892015-02-10 19:05:47 +080062{
Zhenyu Wang0c8792d2017-06-09 15:48:05 +080063 u64 magic;
64 u16 version_major;
Yu Zhangcf9d2892015-02-10 19:05:47 +080065
66 BUILD_BUG_ON(sizeof(struct vgt_if) != VGT_PVINFO_SIZE);
67
Ville Syrjälä75aa3f62015-10-22 15:34:56 +030068 magic = __raw_i915_read64(dev_priv, vgtif_reg(magic));
Yu Zhangcf9d2892015-02-10 19:05:47 +080069 if (magic != VGT_MAGIC)
70 return;
71
Zhenyu Wang0c8792d2017-06-09 15:48:05 +080072 version_major = __raw_i915_read16(dev_priv, vgtif_reg(version_major));
73 if (version_major < VGT_VERSION_MAJOR) {
Yu Zhangcf9d2892015-02-10 19:05:47 +080074 DRM_INFO("VGT interface version mismatch!\n");
75 return;
76 }
77
Tina Zhang8a4ab662017-08-14 15:20:46 +080078 dev_priv->vgpu.caps = __raw_i915_read32(dev_priv, vgtif_reg(vgt_caps));
79
Yu Zhangcf9d2892015-02-10 19:05:47 +080080 dev_priv->vgpu.active = true;
81 DRM_INFO("Virtual GPU for Intel GVT-g detected.\n");
82}
Yu Zhang5dda8fa2015-02-10 19:05:48 +080083
Tina Zhang8a4ab662017-08-14 15:20:46 +080084bool intel_vgpu_has_full_48bit_ppgtt(struct drm_i915_private *dev_priv)
85{
86 return dev_priv->vgpu.caps & VGT_CAPS_FULL_48BIT_PPGTT;
87}
88
Yu Zhang5dda8fa2015-02-10 19:05:48 +080089struct _balloon_info_ {
90 /*
91 * There are up to 2 regions per mappable/unmappable graphic
92 * memory that might be ballooned. Here, index 0/1 is for mappable
93 * graphic memory, 2/3 for unmappable graphic memory.
94 */
95 struct drm_mm_node space[4];
96};
97
98static struct _balloon_info_ bl_info;
99
Weinan Liff8f7972017-05-31 10:35:52 +0800100static void vgt_deballoon_space(struct i915_ggtt *ggtt,
101 struct drm_mm_node *node)
102{
103 DRM_DEBUG_DRIVER("deballoon space: range [0x%llx - 0x%llx] %llu KiB.\n",
104 node->start,
105 node->start + node->size,
106 node->size / 1024);
107
108 ggtt->base.reserved -= node->size;
109 drm_mm_remove_node(node);
110}
111
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800112/**
113 * intel_vgt_deballoon - deballoon reserved graphics address trunks
Daniel Vetter62f90b32016-07-15 21:48:07 +0200114 * @dev_priv: i915 device private data
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800115 *
116 * This function is called to deallocate the ballooned-out graphic memory, when
117 * driver is unloaded or when ballooning fails.
118 */
Zhi Wangb02d22a2016-06-16 08:06:59 -0400119void intel_vgt_deballoon(struct drm_i915_private *dev_priv)
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800120{
121 int i;
122
Zhi Wangb02d22a2016-06-16 08:06:59 -0400123 if (!intel_vgpu_active(dev_priv))
124 return;
125
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800126 DRM_DEBUG("VGT deballoon.\n");
127
Weinan Liff8f7972017-05-31 10:35:52 +0800128 for (i = 0; i < 4; i++)
129 vgt_deballoon_space(&dev_priv->ggtt, &bl_info.space[i]);
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800130}
131
Chris Wilson625d9882017-01-11 11:23:11 +0000132static int vgt_balloon_space(struct i915_ggtt *ggtt,
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800133 struct drm_mm_node *node,
134 unsigned long start, unsigned long end)
135{
136 unsigned long size = end - start;
Weinan Liff8f7972017-05-31 10:35:52 +0800137 int ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800138
Zhenyu Wangb368f532017-01-17 22:06:11 +0800139 if (start >= end)
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800140 return -EINVAL;
141
142 DRM_INFO("balloon space: range [ 0x%lx - 0x%lx ] %lu KiB.\n",
143 start, end, size / 1024);
Weinan Liff8f7972017-05-31 10:35:52 +0800144 ret = i915_gem_gtt_reserve(&ggtt->base, node,
145 size, start, I915_COLOR_UNEVICTABLE,
146 0);
147 if (!ret)
148 ggtt->base.reserved += size;
149
150 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800151}
152
153/**
154 * intel_vgt_balloon - balloon out reserved graphics address trunks
Daniel Vetter62f90b32016-07-15 21:48:07 +0200155 * @dev_priv: i915 device private data
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800156 *
157 * This function is called at the initialization stage, to balloon out the
158 * graphic address space allocated to other vGPUs, by marking these spaces as
159 * reserved. The ballooning related knowledge(starting address and size of
160 * the mappable/unmappable graphic memory) is described in the vgt_if structure
161 * in a reserved mmio range.
162 *
163 * To give an example, the drawing below depicts one typical scenario after
164 * ballooning. Here the vGPU1 has 2 pieces of graphic address spaces ballooned
165 * out each for the mappable and the non-mappable part. From the vGPU1 point of
166 * view, the total size is the same as the physical one, with the start address
167 * of its graphic space being zero. Yet there are some portions ballooned out(
168 * the shadow part, which are marked as reserved by drm allocator). From the
169 * host point of view, the graphic address space is partitioned by multiple
Daniel Vetterda5335b2016-05-31 22:55:13 +0200170 * vGPUs in different VMs. ::
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800171 *
Daniel Vetter62cacc72016-08-12 22:48:37 +0200172 * vGPU1 view Host view
173 * 0 ------> +-----------+ +-----------+
174 * ^ |###########| | vGPU3 |
175 * | |###########| +-----------+
176 * | |###########| | vGPU2 |
177 * | +-----------+ +-----------+
178 * mappable GM | available | ==> | vGPU1 |
179 * | +-----------+ +-----------+
180 * | |###########| | |
181 * v |###########| | Host |
182 * +=======+===========+ +===========+
183 * ^ |###########| | vGPU3 |
184 * | |###########| +-----------+
185 * | |###########| | vGPU2 |
186 * | +-----------+ +-----------+
187 * unmappable GM | available | ==> | vGPU1 |
188 * | +-----------+ +-----------+
189 * | |###########| | |
190 * | |###########| | Host |
191 * v |###########| | |
192 * total GM size ------> +-----------+ +-----------+
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800193 *
194 * Returns:
195 * zero on success, non-zero if configuration invalid or ballooning failed
196 */
Zhi Wangb02d22a2016-06-16 08:06:59 -0400197int intel_vgt_balloon(struct drm_i915_private *dev_priv)
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800198{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300199 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson381b9432017-02-15 08:43:54 +0000200 unsigned long ggtt_end = ggtt->base.total;
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800201
202 unsigned long mappable_base, mappable_size, mappable_end;
203 unsigned long unmappable_base, unmappable_size, unmappable_end;
204 int ret;
205
Zhi Wangb02d22a2016-06-16 08:06:59 -0400206 if (!intel_vgpu_active(dev_priv))
207 return 0;
208
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800209 mappable_base = I915_READ(vgtif_reg(avail_rs.mappable_gmadr.base));
210 mappable_size = I915_READ(vgtif_reg(avail_rs.mappable_gmadr.size));
211 unmappable_base = I915_READ(vgtif_reg(avail_rs.nonmappable_gmadr.base));
212 unmappable_size = I915_READ(vgtif_reg(avail_rs.nonmappable_gmadr.size));
213
214 mappable_end = mappable_base + mappable_size;
215 unmappable_end = unmappable_base + unmappable_size;
216
217 DRM_INFO("VGT ballooning configuration:\n");
218 DRM_INFO("Mappable graphic memory: base 0x%lx size %ldKiB\n",
219 mappable_base, mappable_size / 1024);
220 DRM_INFO("Unmappable graphic memory: base 0x%lx size %ldKiB\n",
221 unmappable_base, unmappable_size / 1024);
222
Chris Wilson381b9432017-02-15 08:43:54 +0000223 if (mappable_end > ggtt->mappable_end ||
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300224 unmappable_base < ggtt->mappable_end ||
225 unmappable_end > ggtt_end) {
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800226 DRM_ERROR("Invalid ballooning configuration!\n");
227 return -EINVAL;
228 }
229
230 /* Unmappable graphic memory ballooning */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300231 if (unmappable_base > ggtt->mappable_end) {
Chris Wilson625d9882017-01-11 11:23:11 +0000232 ret = vgt_balloon_space(ggtt, &bl_info.space[2],
233 ggtt->mappable_end, unmappable_base);
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800234
235 if (ret)
236 goto err;
237 }
238
Zhenyu Wangfa7e8b52017-03-10 10:22:38 +0800239 if (unmappable_end < ggtt_end) {
Chris Wilson625d9882017-01-11 11:23:11 +0000240 ret = vgt_balloon_space(ggtt, &bl_info.space[3],
Zhenyu Wangfa7e8b52017-03-10 10:22:38 +0800241 unmappable_end, ggtt_end);
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800242 if (ret)
Weinan Liff8f7972017-05-31 10:35:52 +0800243 goto err_upon_mappable;
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800244 }
245
246 /* Mappable graphic memory ballooning */
Chris Wilson381b9432017-02-15 08:43:54 +0000247 if (mappable_base) {
Chris Wilson625d9882017-01-11 11:23:11 +0000248 ret = vgt_balloon_space(ggtt, &bl_info.space[0],
Chris Wilson381b9432017-02-15 08:43:54 +0000249 0, mappable_base);
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800250
251 if (ret)
Weinan Liff8f7972017-05-31 10:35:52 +0800252 goto err_upon_unmappable;
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800253 }
254
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300255 if (mappable_end < ggtt->mappable_end) {
Chris Wilson625d9882017-01-11 11:23:11 +0000256 ret = vgt_balloon_space(ggtt, &bl_info.space[1],
257 mappable_end, ggtt->mappable_end);
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800258
259 if (ret)
Weinan Liff8f7972017-05-31 10:35:52 +0800260 goto err_below_mappable;
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800261 }
262
263 DRM_INFO("VGT balloon successfully\n");
264 return 0;
265
Weinan Liff8f7972017-05-31 10:35:52 +0800266err_below_mappable:
267 vgt_deballoon_space(ggtt, &bl_info.space[0]);
268err_upon_unmappable:
269 vgt_deballoon_space(ggtt, &bl_info.space[3]);
270err_upon_mappable:
271 vgt_deballoon_space(ggtt, &bl_info.space[2]);
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800272err:
273 DRM_ERROR("VGT balloon fail\n");
Yu Zhang5dda8fa2015-02-10 19:05:48 +0800274 return ret;
275}