xref: /dragonfly/sys/dev/drm/i915/i915_vgpu.c (revision 3f2dd94a)
1477eb7f9SFrançois Tigeot /*
2477eb7f9SFrançois Tigeot  * Copyright(c) 2011-2015 Intel Corporation. All rights reserved.
3477eb7f9SFrançois Tigeot  *
4477eb7f9SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5477eb7f9SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6477eb7f9SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7477eb7f9SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8477eb7f9SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9477eb7f9SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10477eb7f9SFrançois Tigeot  *
11477eb7f9SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12477eb7f9SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13477eb7f9SFrançois Tigeot  * Software.
14477eb7f9SFrançois Tigeot  *
15477eb7f9SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16477eb7f9SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17477eb7f9SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18477eb7f9SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19477eb7f9SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20477eb7f9SFrançois Tigeot  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21477eb7f9SFrançois Tigeot  * SOFTWARE.
22477eb7f9SFrançois Tigeot  */
23477eb7f9SFrançois Tigeot 
24477eb7f9SFrançois Tigeot #include "intel_drv.h"
25477eb7f9SFrançois Tigeot #include "i915_vgpu.h"
26477eb7f9SFrançois Tigeot 
27477eb7f9SFrançois Tigeot /**
28477eb7f9SFrançois Tigeot  * DOC: Intel GVT-g guest support
29477eb7f9SFrançois Tigeot  *
30477eb7f9SFrançois Tigeot  * Intel GVT-g is a graphics virtualization technology which shares the
31477eb7f9SFrançois Tigeot  * GPU among multiple virtual machines on a time-sharing basis. Each
32477eb7f9SFrançois Tigeot  * virtual machine is presented a virtual GPU (vGPU), which has equivalent
33477eb7f9SFrançois Tigeot  * features as the underlying physical GPU (pGPU), so i915 driver can run
34477eb7f9SFrançois Tigeot  * seamlessly in a virtual machine. This file provides vGPU specific
35477eb7f9SFrançois Tigeot  * optimizations when running in a virtual machine, to reduce the complexity
36477eb7f9SFrançois Tigeot  * of vGPU emulation and to improve the overall performance.
37477eb7f9SFrançois Tigeot  *
38477eb7f9SFrançois Tigeot  * A primary function introduced here is so-called "address space ballooning"
39477eb7f9SFrançois Tigeot  * technique. Intel GVT-g partitions global graphics memory among multiple VMs,
40477eb7f9SFrançois Tigeot  * so each VM can directly access a portion of the memory without hypervisor's
41477eb7f9SFrançois Tigeot  * intervention, e.g. filling textures or queuing commands. However with the
42477eb7f9SFrançois Tigeot  * partitioning an unmodified i915 driver would assume a smaller graphics
43477eb7f9SFrançois Tigeot  * memory starting from address ZERO, then requires vGPU emulation module to
44477eb7f9SFrançois Tigeot  * translate the graphics address between 'guest view' and 'host view', for
45477eb7f9SFrançois Tigeot  * all registers and command opcodes which contain a graphics memory address.
46477eb7f9SFrançois Tigeot  * To reduce the complexity, Intel GVT-g introduces "address space ballooning",
47477eb7f9SFrançois Tigeot  * by telling the exact partitioning knowledge to each guest i915 driver, which
48477eb7f9SFrançois Tigeot  * then reserves and prevents non-allocated portions from allocation. Thus vGPU
49477eb7f9SFrançois Tigeot  * emulation module only needs to scan and validate graphics addresses without
50477eb7f9SFrançois Tigeot  * complexity of address translation.
51477eb7f9SFrançois Tigeot  *
52477eb7f9SFrançois Tigeot  */
53477eb7f9SFrançois Tigeot 
54477eb7f9SFrançois Tigeot /**
55477eb7f9SFrançois Tigeot  * i915_check_vgpu - detect virtual GPU
561487f786SFrançois Tigeot  * @dev_priv: i915 device private
57477eb7f9SFrançois Tigeot  *
58477eb7f9SFrançois Tigeot  * This function is called at the initialization stage, to detect whether
59477eb7f9SFrançois Tigeot  * running on a vGPU.
60477eb7f9SFrançois Tigeot  */
i915_check_vgpu(struct drm_i915_private * dev_priv)611487f786SFrançois Tigeot void i915_check_vgpu(struct drm_i915_private *dev_priv)
62477eb7f9SFrançois Tigeot {
63a85cb24fSFrançois Tigeot 	u64 magic;
64a85cb24fSFrançois Tigeot 	u16 version_major;
65477eb7f9SFrançois Tigeot 
66477eb7f9SFrançois Tigeot 	BUILD_BUG_ON(sizeof(struct vgt_if) != VGT_PVINFO_SIZE);
67477eb7f9SFrançois Tigeot 
68aee94f86SFrançois Tigeot 	magic = __raw_i915_read64(dev_priv, vgtif_reg(magic));
69477eb7f9SFrançois Tigeot 	if (magic != VGT_MAGIC)
70477eb7f9SFrançois Tigeot 		return;
71477eb7f9SFrançois Tigeot 
72a85cb24fSFrançois Tigeot 	version_major = __raw_i915_read16(dev_priv, vgtif_reg(version_major));
73a85cb24fSFrançois Tigeot 	if (version_major < VGT_VERSION_MAJOR) {
74477eb7f9SFrançois Tigeot 		DRM_INFO("VGT interface version mismatch!\n");
75477eb7f9SFrançois Tigeot 		return;
76477eb7f9SFrançois Tigeot 	}
77477eb7f9SFrançois Tigeot 
78*3f2dd94aSFrançois Tigeot 	dev_priv->vgpu.caps = __raw_i915_read32(dev_priv, vgtif_reg(vgt_caps));
79*3f2dd94aSFrançois Tigeot 
80477eb7f9SFrançois Tigeot 	dev_priv->vgpu.active = true;
81477eb7f9SFrançois Tigeot 	DRM_INFO("Virtual GPU for Intel GVT-g detected.\n");
82477eb7f9SFrançois Tigeot }
83477eb7f9SFrançois Tigeot 
intel_vgpu_has_full_48bit_ppgtt(struct drm_i915_private * dev_priv)84*3f2dd94aSFrançois Tigeot bool intel_vgpu_has_full_48bit_ppgtt(struct drm_i915_private *dev_priv)
85*3f2dd94aSFrançois Tigeot {
86*3f2dd94aSFrançois Tigeot 	return dev_priv->vgpu.caps & VGT_CAPS_FULL_48BIT_PPGTT;
87*3f2dd94aSFrançois Tigeot }
88*3f2dd94aSFrançois Tigeot 
89477eb7f9SFrançois Tigeot struct _balloon_info_ {
90477eb7f9SFrançois Tigeot 	/*
91477eb7f9SFrançois Tigeot 	 * There are up to 2 regions per mappable/unmappable graphic
92477eb7f9SFrançois Tigeot 	 * memory that might be ballooned. Here, index 0/1 is for mappable
93477eb7f9SFrançois Tigeot 	 * graphic memory, 2/3 for unmappable graphic memory.
94477eb7f9SFrançois Tigeot 	 */
95477eb7f9SFrançois Tigeot 	struct drm_mm_node space[4];
96477eb7f9SFrançois Tigeot };
97477eb7f9SFrançois Tigeot 
98477eb7f9SFrançois Tigeot static struct _balloon_info_ bl_info;
99477eb7f9SFrançois Tigeot 
vgt_deballoon_space(struct i915_ggtt * ggtt,struct drm_mm_node * node)100*3f2dd94aSFrançois Tigeot static void vgt_deballoon_space(struct i915_ggtt *ggtt,
101*3f2dd94aSFrançois Tigeot 				struct drm_mm_node *node)
102*3f2dd94aSFrançois Tigeot {
103*3f2dd94aSFrançois Tigeot 	DRM_DEBUG_DRIVER("deballoon space: range [0x%llx - 0x%llx] %llu KiB.\n",
104*3f2dd94aSFrançois Tigeot 			 node->start,
105*3f2dd94aSFrançois Tigeot 			 node->start + node->size,
106*3f2dd94aSFrançois Tigeot 			 node->size / 1024);
107*3f2dd94aSFrançois Tigeot 
108*3f2dd94aSFrançois Tigeot 	ggtt->base.reserved -= node->size;
109*3f2dd94aSFrançois Tigeot 	drm_mm_remove_node(node);
110*3f2dd94aSFrançois Tigeot }
111*3f2dd94aSFrançois Tigeot 
112477eb7f9SFrançois Tigeot /**
113477eb7f9SFrançois Tigeot  * intel_vgt_deballoon - deballoon reserved graphics address trunks
11487df8fc6SFrançois Tigeot  * @dev_priv: i915 device private data
115477eb7f9SFrançois Tigeot  *
116477eb7f9SFrançois Tigeot  * This function is called to deallocate the ballooned-out graphic memory, when
117477eb7f9SFrançois Tigeot  * driver is unloaded or when ballooning fails.
118477eb7f9SFrançois Tigeot  */
intel_vgt_deballoon(struct drm_i915_private * dev_priv)1191487f786SFrançois Tigeot void intel_vgt_deballoon(struct drm_i915_private *dev_priv)
120477eb7f9SFrançois Tigeot {
121477eb7f9SFrançois Tigeot 	int i;
122477eb7f9SFrançois Tigeot 
1231487f786SFrançois Tigeot 	if (!intel_vgpu_active(dev_priv))
1241487f786SFrançois Tigeot 		return;
1251487f786SFrançois Tigeot 
126477eb7f9SFrançois Tigeot 	DRM_DEBUG("VGT deballoon.\n");
127477eb7f9SFrançois Tigeot 
128*3f2dd94aSFrançois Tigeot 	for (i = 0; i < 4; i++)
129*3f2dd94aSFrançois Tigeot 		vgt_deballoon_space(&dev_priv->ggtt, &bl_info.space[i]);
130477eb7f9SFrançois Tigeot }
131477eb7f9SFrançois Tigeot 
vgt_balloon_space(struct i915_ggtt * ggtt,struct drm_mm_node * node,unsigned long start,unsigned long end)132a85cb24fSFrançois Tigeot static int vgt_balloon_space(struct i915_ggtt *ggtt,
133477eb7f9SFrançois Tigeot 			     struct drm_mm_node *node,
134477eb7f9SFrançois Tigeot 			     unsigned long start, unsigned long end)
135477eb7f9SFrançois Tigeot {
136477eb7f9SFrançois Tigeot 	unsigned long size = end - start;
137*3f2dd94aSFrançois Tigeot 	int ret;
138477eb7f9SFrançois Tigeot 
139a85cb24fSFrançois Tigeot 	if (start >= end)
140477eb7f9SFrançois Tigeot 		return -EINVAL;
141477eb7f9SFrançois Tigeot 
142477eb7f9SFrançois Tigeot 	DRM_INFO("balloon space: range [ 0x%lx - 0x%lx ] %lu KiB.\n",
143477eb7f9SFrançois Tigeot 		 start, end, size / 1024);
144*3f2dd94aSFrançois Tigeot 	ret = i915_gem_gtt_reserve(&ggtt->base, node,
145a85cb24fSFrançois Tigeot 				   size, start, I915_COLOR_UNEVICTABLE,
146a85cb24fSFrançois Tigeot 				   0);
147*3f2dd94aSFrançois Tigeot 	if (!ret)
148*3f2dd94aSFrançois Tigeot 		ggtt->base.reserved += size;
149*3f2dd94aSFrançois Tigeot 
150*3f2dd94aSFrançois Tigeot 	return ret;
151477eb7f9SFrançois Tigeot }
152477eb7f9SFrançois Tigeot 
153477eb7f9SFrançois Tigeot /**
154477eb7f9SFrançois Tigeot  * intel_vgt_balloon - balloon out reserved graphics address trunks
15587df8fc6SFrançois Tigeot  * @dev_priv: i915 device private data
156477eb7f9SFrançois Tigeot  *
157477eb7f9SFrançois Tigeot  * This function is called at the initialization stage, to balloon out the
158477eb7f9SFrançois Tigeot  * graphic address space allocated to other vGPUs, by marking these spaces as
159477eb7f9SFrançois Tigeot  * reserved. The ballooning related knowledge(starting address and size of
160477eb7f9SFrançois Tigeot  * the mappable/unmappable graphic memory) is described in the vgt_if structure
161477eb7f9SFrançois Tigeot  * in a reserved mmio range.
162477eb7f9SFrançois Tigeot  *
163477eb7f9SFrançois Tigeot  * To give an example, the drawing below depicts one typical scenario after
164477eb7f9SFrançois Tigeot  * ballooning. Here the vGPU1 has 2 pieces of graphic address spaces ballooned
165477eb7f9SFrançois Tigeot  * out each for the mappable and the non-mappable part. From the vGPU1 point of
166477eb7f9SFrançois Tigeot  * view, the total size is the same as the physical one, with the start address
167477eb7f9SFrançois Tigeot  * of its graphic space being zero. Yet there are some portions ballooned out(
168477eb7f9SFrançois Tigeot  * the shadow part, which are marked as reserved by drm allocator). From the
169477eb7f9SFrançois Tigeot  * host point of view, the graphic address space is partitioned by multiple
1701487f786SFrançois Tigeot  * vGPUs in different VMs. ::
171477eb7f9SFrançois Tigeot  *
172477eb7f9SFrançois Tigeot  *                         vGPU1 view         Host view
173477eb7f9SFrançois Tigeot  *              0 ------> +-----------+     +-----------+
1741487f786SFrançois Tigeot  *                ^       |###########|     |   vGPU3   |
1751487f786SFrançois Tigeot  *                |       |###########|     +-----------+
1761487f786SFrançois Tigeot  *                |       |###########|     |   vGPU2   |
177477eb7f9SFrançois Tigeot  *                |       +-----------+     +-----------+
178477eb7f9SFrançois Tigeot  *         mappable GM    | available | ==> |   vGPU1   |
179477eb7f9SFrançois Tigeot  *                |       +-----------+     +-----------+
1801487f786SFrançois Tigeot  *                |       |###########|     |           |
1811487f786SFrançois Tigeot  *                v       |###########|     |   Host    |
182477eb7f9SFrançois Tigeot  *                +=======+===========+     +===========+
1831487f786SFrançois Tigeot  *                ^       |###########|     |   vGPU3   |
1841487f786SFrançois Tigeot  *                |       |###########|     +-----------+
1851487f786SFrançois Tigeot  *                |       |###########|     |   vGPU2   |
186477eb7f9SFrançois Tigeot  *                |       +-----------+     +-----------+
187477eb7f9SFrançois Tigeot  *       unmappable GM    | available | ==> |   vGPU1   |
188477eb7f9SFrançois Tigeot  *                |       +-----------+     +-----------+
1891487f786SFrançois Tigeot  *                |       |###########|     |           |
1901487f786SFrançois Tigeot  *                |       |###########|     |   Host    |
1911487f786SFrançois Tigeot  *                v       |###########|     |           |
192477eb7f9SFrançois Tigeot  *  total GM size ------> +-----------+     +-----------+
193477eb7f9SFrançois Tigeot  *
194477eb7f9SFrançois Tigeot  * Returns:
195477eb7f9SFrançois Tigeot  * zero on success, non-zero if configuration invalid or ballooning failed
196477eb7f9SFrançois Tigeot  */
intel_vgt_balloon(struct drm_i915_private * dev_priv)1971487f786SFrançois Tigeot int intel_vgt_balloon(struct drm_i915_private *dev_priv)
198477eb7f9SFrançois Tigeot {
1998621f407SFrançois Tigeot 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
200a85cb24fSFrançois Tigeot 	unsigned long ggtt_end = ggtt->base.total;
201477eb7f9SFrançois Tigeot 
202477eb7f9SFrançois Tigeot 	unsigned long mappable_base, mappable_size, mappable_end;
203477eb7f9SFrançois Tigeot 	unsigned long unmappable_base, unmappable_size, unmappable_end;
204477eb7f9SFrançois Tigeot 	int ret;
205477eb7f9SFrançois Tigeot 
2061487f786SFrançois Tigeot 	if (!intel_vgpu_active(dev_priv))
2071487f786SFrançois Tigeot 		return 0;
2081487f786SFrançois Tigeot 
209477eb7f9SFrançois Tigeot 	mappable_base = I915_READ(vgtif_reg(avail_rs.mappable_gmadr.base));
210477eb7f9SFrançois Tigeot 	mappable_size = I915_READ(vgtif_reg(avail_rs.mappable_gmadr.size));
211477eb7f9SFrançois Tigeot 	unmappable_base = I915_READ(vgtif_reg(avail_rs.nonmappable_gmadr.base));
212477eb7f9SFrançois Tigeot 	unmappable_size = I915_READ(vgtif_reg(avail_rs.nonmappable_gmadr.size));
213477eb7f9SFrançois Tigeot 
214477eb7f9SFrançois Tigeot 	mappable_end = mappable_base + mappable_size;
215477eb7f9SFrançois Tigeot 	unmappable_end = unmappable_base + unmappable_size;
216477eb7f9SFrançois Tigeot 
217477eb7f9SFrançois Tigeot 	DRM_INFO("VGT ballooning configuration:\n");
218477eb7f9SFrançois Tigeot 	DRM_INFO("Mappable graphic memory: base 0x%lx size %ldKiB\n",
219477eb7f9SFrançois Tigeot 		 mappable_base, mappable_size / 1024);
220477eb7f9SFrançois Tigeot 	DRM_INFO("Unmappable graphic memory: base 0x%lx size %ldKiB\n",
221477eb7f9SFrançois Tigeot 		 unmappable_base, unmappable_size / 1024);
222477eb7f9SFrançois Tigeot 
223a85cb24fSFrançois Tigeot 	if (mappable_end > ggtt->mappable_end ||
2248621f407SFrançois Tigeot 	    unmappable_base < ggtt->mappable_end ||
2258621f407SFrançois Tigeot 	    unmappable_end > ggtt_end) {
226477eb7f9SFrançois Tigeot 		DRM_ERROR("Invalid ballooning configuration!\n");
227477eb7f9SFrançois Tigeot 		return -EINVAL;
228477eb7f9SFrançois Tigeot 	}
229477eb7f9SFrançois Tigeot 
230477eb7f9SFrançois Tigeot 	/* Unmappable graphic memory ballooning */
2318621f407SFrançois Tigeot 	if (unmappable_base > ggtt->mappable_end) {
232a85cb24fSFrançois Tigeot 		ret = vgt_balloon_space(ggtt, &bl_info.space[2],
233a85cb24fSFrançois Tigeot 					ggtt->mappable_end, unmappable_base);
234477eb7f9SFrançois Tigeot 
235477eb7f9SFrançois Tigeot 		if (ret)
236477eb7f9SFrançois Tigeot 			goto err;
237477eb7f9SFrançois Tigeot 	}
238477eb7f9SFrançois Tigeot 
239a85cb24fSFrançois Tigeot 	if (unmappable_end < ggtt_end) {
240a85cb24fSFrançois Tigeot 		ret = vgt_balloon_space(ggtt, &bl_info.space[3],
241a85cb24fSFrançois Tigeot 					unmappable_end, ggtt_end);
242477eb7f9SFrançois Tigeot 		if (ret)
243*3f2dd94aSFrançois Tigeot 			goto err_upon_mappable;
244477eb7f9SFrançois Tigeot 	}
245477eb7f9SFrançois Tigeot 
246477eb7f9SFrançois Tigeot 	/* Mappable graphic memory ballooning */
247a85cb24fSFrançois Tigeot 	if (mappable_base) {
248a85cb24fSFrançois Tigeot 		ret = vgt_balloon_space(ggtt, &bl_info.space[0],
249a85cb24fSFrançois Tigeot 					0, mappable_base);
250477eb7f9SFrançois Tigeot 
251477eb7f9SFrançois Tigeot 		if (ret)
252*3f2dd94aSFrançois Tigeot 			goto err_upon_unmappable;
253477eb7f9SFrançois Tigeot 	}
254477eb7f9SFrançois Tigeot 
2558621f407SFrançois Tigeot 	if (mappable_end < ggtt->mappable_end) {
256a85cb24fSFrançois Tigeot 		ret = vgt_balloon_space(ggtt, &bl_info.space[1],
257a85cb24fSFrançois Tigeot 					mappable_end, ggtt->mappable_end);
258477eb7f9SFrançois Tigeot 
259477eb7f9SFrançois Tigeot 		if (ret)
260*3f2dd94aSFrançois Tigeot 			goto err_below_mappable;
261477eb7f9SFrançois Tigeot 	}
262477eb7f9SFrançois Tigeot 
263477eb7f9SFrançois Tigeot 	DRM_INFO("VGT balloon successfully\n");
264477eb7f9SFrançois Tigeot 	return 0;
265477eb7f9SFrançois Tigeot 
266*3f2dd94aSFrançois Tigeot err_below_mappable:
267*3f2dd94aSFrançois Tigeot 	vgt_deballoon_space(ggtt, &bl_info.space[0]);
268*3f2dd94aSFrançois Tigeot err_upon_unmappable:
269*3f2dd94aSFrançois Tigeot 	vgt_deballoon_space(ggtt, &bl_info.space[3]);
270*3f2dd94aSFrançois Tigeot err_upon_mappable:
271*3f2dd94aSFrançois Tigeot 	vgt_deballoon_space(ggtt, &bl_info.space[2]);
272477eb7f9SFrançois Tigeot err:
273477eb7f9SFrançois Tigeot 	DRM_ERROR("VGT balloon fail\n");
274477eb7f9SFrançois Tigeot 	return ret;
275477eb7f9SFrançois Tigeot }
276