Home | History | Annotate | Line # | Download | only in i915
i915_vgpu.c revision 1.1.1.1
      1 /*	$NetBSD: i915_vgpu.c,v 1.1.1.1 2018/08/27 01:34:54 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright(c) 2011-2015 Intel Corporation. All rights reserved.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     23  * SOFTWARE.
     24  */
     25 
     26 #include <sys/cdefs.h>
     27 __KERNEL_RCSID(0, "$NetBSD: i915_vgpu.c,v 1.1.1.1 2018/08/27 01:34:54 riastradh Exp $");
     28 
     29 #include "intel_drv.h"
     30 #include "i915_vgpu.h"
     31 
     32 /**
     33  * DOC: Intel GVT-g guest support
     34  *
     35  * Intel GVT-g is a graphics virtualization technology which shares the
     36  * GPU among multiple virtual machines on a time-sharing basis. Each
     37  * virtual machine is presented a virtual GPU (vGPU), which has equivalent
     38  * features as the underlying physical GPU (pGPU), so i915 driver can run
     39  * seamlessly in a virtual machine. This file provides vGPU specific
     40  * optimizations when running in a virtual machine, to reduce the complexity
     41  * of vGPU emulation and to improve the overall performance.
     42  *
     43  * A primary function introduced here is so-called "address space ballooning"
     44  * technique. Intel GVT-g partitions global graphics memory among multiple VMs,
     45  * so each VM can directly access a portion of the memory without hypervisor's
     46  * intervention, e.g. filling textures or queuing commands. However with the
     47  * partitioning an unmodified i915 driver would assume a smaller graphics
     48  * memory starting from address ZERO, then requires vGPU emulation module to
     49  * translate the graphics address between 'guest view' and 'host view', for
     50  * all registers and command opcodes which contain a graphics memory address.
     51  * To reduce the complexity, Intel GVT-g introduces "address space ballooning",
     52  * by telling the exact partitioning knowledge to each guest i915 driver, which
     53  * then reserves and prevents non-allocated portions from allocation. Thus vGPU
     54  * emulation module only needs to scan and validate graphics addresses without
     55  * complexity of address translation.
     56  *
     57  */
     58 
     59 /**
     60  * i915_check_vgpu - detect virtual GPU
     61  * @dev: drm device *
     62  *
     63  * This function is called at the initialization stage, to detect whether
     64  * running on a vGPU.
     65  */
     66 void i915_check_vgpu(struct drm_device *dev)
     67 {
     68 	struct drm_i915_private *dev_priv = to_i915(dev);
     69 	uint64_t magic;
     70 	uint32_t version;
     71 
     72 	BUILD_BUG_ON(sizeof(struct vgt_if) != VGT_PVINFO_SIZE);
     73 
     74 	if (!IS_HASWELL(dev))
     75 		return;
     76 
     77 	magic = readq(dev_priv->regs + vgtif_reg(magic));
     78 	if (magic != VGT_MAGIC)
     79 		return;
     80 
     81 	version = INTEL_VGT_IF_VERSION_ENCODE(
     82 		readw(dev_priv->regs + vgtif_reg(version_major)),
     83 		readw(dev_priv->regs + vgtif_reg(version_minor)));
     84 	if (version != INTEL_VGT_IF_VERSION) {
     85 		DRM_INFO("VGT interface version mismatch!\n");
     86 		return;
     87 	}
     88 
     89 	dev_priv->vgpu.active = true;
     90 	DRM_INFO("Virtual GPU for Intel GVT-g detected.\n");
     91 }
     92 
     93 struct _balloon_info_ {
     94 	/*
     95 	 * There are up to 2 regions per mappable/unmappable graphic
     96 	 * memory that might be ballooned. Here, index 0/1 is for mappable
     97 	 * graphic memory, 2/3 for unmappable graphic memory.
     98 	 */
     99 	struct drm_mm_node space[4];
    100 };
    101 
    102 static struct _balloon_info_ bl_info;
    103 
    104 /**
    105  * intel_vgt_deballoon - deballoon reserved graphics address trunks
    106  *
    107  * This function is called to deallocate the ballooned-out graphic memory, when
    108  * driver is unloaded or when ballooning fails.
    109  */
    110 void intel_vgt_deballoon(void)
    111 {
    112 	int i;
    113 
    114 	DRM_DEBUG("VGT deballoon.\n");
    115 
    116 	for (i = 0; i < 4; i++) {
    117 		if (bl_info.space[i].allocated)
    118 			drm_mm_remove_node(&bl_info.space[i]);
    119 	}
    120 
    121 	memset(&bl_info, 0, sizeof(bl_info));
    122 }
    123 
    124 static int vgt_balloon_space(struct drm_mm *mm,
    125 			     struct drm_mm_node *node,
    126 			     unsigned long start, unsigned long end)
    127 {
    128 	unsigned long size = end - start;
    129 
    130 	if (start == end)
    131 		return -EINVAL;
    132 
    133 	DRM_INFO("balloon space: range [ 0x%lx - 0x%lx ] %lu KiB.\n",
    134 		 start, end, size / 1024);
    135 
    136 	node->start = start;
    137 	node->size = size;
    138 
    139 	return drm_mm_reserve_node(mm, node);
    140 }
    141 
    142 /**
    143  * intel_vgt_balloon - balloon out reserved graphics address trunks
    144  * @dev: drm device
    145  *
    146  * This function is called at the initialization stage, to balloon out the
    147  * graphic address space allocated to other vGPUs, by marking these spaces as
    148  * reserved. The ballooning related knowledge(starting address and size of
    149  * the mappable/unmappable graphic memory) is described in the vgt_if structure
    150  * in a reserved mmio range.
    151  *
    152  * To give an example, the drawing below depicts one typical scenario after
    153  * ballooning. Here the vGPU1 has 2 pieces of graphic address spaces ballooned
    154  * out each for the mappable and the non-mappable part. From the vGPU1 point of
    155  * view, the total size is the same as the physical one, with the start address
    156  * of its graphic space being zero. Yet there are some portions ballooned out(
    157  * the shadow part, which are marked as reserved by drm allocator). From the
    158  * host point of view, the graphic address space is partitioned by multiple
    159  * vGPUs in different VMs.
    160  *
    161  *                        vGPU1 view         Host view
    162  *             0 ------> +-----------+     +-----------+
    163  *               ^       |///////////|     |   vGPU3   |
    164  *               |       |///////////|     +-----------+
    165  *               |       |///////////|     |   vGPU2   |
    166  *               |       +-----------+     +-----------+
    167  *        mappable GM    | available | ==> |   vGPU1   |
    168  *               |       +-----------+     +-----------+
    169  *               |       |///////////|     |           |
    170  *               v       |///////////|     |   Host    |
    171  *               +=======+===========+     +===========+
    172  *               ^       |///////////|     |   vGPU3   |
    173  *               |       |///////////|     +-----------+
    174  *               |       |///////////|     |   vGPU2   |
    175  *               |       +-----------+     +-----------+
    176  *      unmappable GM    | available | ==> |   vGPU1   |
    177  *               |       +-----------+     +-----------+
    178  *               |       |///////////|     |           |
    179  *               |       |///////////|     |   Host    |
    180  *               v       |///////////|     |           |
    181  * total GM size ------> +-----------+     +-----------+
    182  *
    183  * Returns:
    184  * zero on success, non-zero if configuration invalid or ballooning failed
    185  */
    186 int intel_vgt_balloon(struct drm_device *dev)
    187 {
    188 	struct drm_i915_private *dev_priv = to_i915(dev);
    189 	struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
    190 	unsigned long ggtt_vm_end = ggtt_vm->start + ggtt_vm->total;
    191 
    192 	unsigned long mappable_base, mappable_size, mappable_end;
    193 	unsigned long unmappable_base, unmappable_size, unmappable_end;
    194 	int ret;
    195 
    196 	mappable_base = I915_READ(vgtif_reg(avail_rs.mappable_gmadr.base));
    197 	mappable_size = I915_READ(vgtif_reg(avail_rs.mappable_gmadr.size));
    198 	unmappable_base = I915_READ(vgtif_reg(avail_rs.nonmappable_gmadr.base));
    199 	unmappable_size = I915_READ(vgtif_reg(avail_rs.nonmappable_gmadr.size));
    200 
    201 	mappable_end = mappable_base + mappable_size;
    202 	unmappable_end = unmappable_base + unmappable_size;
    203 
    204 	DRM_INFO("VGT ballooning configuration:\n");
    205 	DRM_INFO("Mappable graphic memory: base 0x%lx size %ldKiB\n",
    206 		 mappable_base, mappable_size / 1024);
    207 	DRM_INFO("Unmappable graphic memory: base 0x%lx size %ldKiB\n",
    208 		 unmappable_base, unmappable_size / 1024);
    209 
    210 	if (mappable_base < ggtt_vm->start ||
    211 	    mappable_end > dev_priv->gtt.mappable_end ||
    212 	    unmappable_base < dev_priv->gtt.mappable_end ||
    213 	    unmappable_end > ggtt_vm_end) {
    214 		DRM_ERROR("Invalid ballooning configuration!\n");
    215 		return -EINVAL;
    216 	}
    217 
    218 	/* Unmappable graphic memory ballooning */
    219 	if (unmappable_base > dev_priv->gtt.mappable_end) {
    220 		ret = vgt_balloon_space(&ggtt_vm->mm,
    221 					&bl_info.space[2],
    222 					dev_priv->gtt.mappable_end,
    223 					unmappable_base);
    224 
    225 		if (ret)
    226 			goto err;
    227 	}
    228 
    229 	/*
    230 	 * No need to partition out the last physical page,
    231 	 * because it is reserved to the guard page.
    232 	 */
    233 	if (unmappable_end < ggtt_vm_end - PAGE_SIZE) {
    234 		ret = vgt_balloon_space(&ggtt_vm->mm,
    235 					&bl_info.space[3],
    236 					unmappable_end,
    237 					ggtt_vm_end - PAGE_SIZE);
    238 		if (ret)
    239 			goto err;
    240 	}
    241 
    242 	/* Mappable graphic memory ballooning */
    243 	if (mappable_base > ggtt_vm->start) {
    244 		ret = vgt_balloon_space(&ggtt_vm->mm,
    245 					&bl_info.space[0],
    246 					ggtt_vm->start, mappable_base);
    247 
    248 		if (ret)
    249 			goto err;
    250 	}
    251 
    252 	if (mappable_end < dev_priv->gtt.mappable_end) {
    253 		ret = vgt_balloon_space(&ggtt_vm->mm,
    254 					&bl_info.space[1],
    255 					mappable_end,
    256 					dev_priv->gtt.mappable_end);
    257 
    258 		if (ret)
    259 			goto err;
    260 	}
    261 
    262 	DRM_INFO("VGT balloon successfully\n");
    263 	return 0;
    264 
    265 err:
    266 	DRM_ERROR("VGT balloon fail\n");
    267 	intel_vgt_deballoon();
    268 	return ret;
    269 }
    270