Home | History | Annotate | Line # | Download | only in i915
i915_vgpu.c revision 1.3
      1 /*	$NetBSD: i915_vgpu.c,v 1.3 2018/08/27 07:16:10 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.3 2018/08/27 07:16:10 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 #ifdef __NetBSD__
     78 	magic = bus_space_read_8(dev_priv->regs_bst, dev_priv->regs_bsh,
     79 	    vgtif_reg(magic));
     80 #else
     81 	magic = readq(dev_priv->regs + vgtif_reg(magic));
     82 #endif
     83 	if (magic != VGT_MAGIC)
     84 		return;
     85 
     86 #ifdef __NetBSD__
     87 	version = INTEL_VGT_IF_VERSION_ENCODE(
     88 		bus_space_read_2(dev_priv->regs_bst, dev_priv->regs_bsh,
     89 		    vgtif_reg(version_major)),
     90 		bus_space_read_2(dev_priv->regs_bst, dev_priv->regs_bsh,
     91 		    vgtif_reg(version_minor)));
     92 #else
     93 	version = INTEL_VGT_IF_VERSION_ENCODE(
     94 		readw(dev_priv->regs + vgtif_reg(version_major)),
     95 		readw(dev_priv->regs + vgtif_reg(version_minor)));
     96 #endif
     97 	if (version != INTEL_VGT_IF_VERSION) {
     98 		DRM_INFO("VGT interface version mismatch!\n");
     99 		return;
    100 	}
    101 
    102 	dev_priv->vgpu.active = true;
    103 	DRM_INFO("Virtual GPU for Intel GVT-g detected.\n");
    104 }
    105 
    106 struct _balloon_info_ {
    107 	/*
    108 	 * There are up to 2 regions per mappable/unmappable graphic
    109 	 * memory that might be ballooned. Here, index 0/1 is for mappable
    110 	 * graphic memory, 2/3 for unmappable graphic memory.
    111 	 */
    112 	struct drm_mm_node space[4];
    113 };
    114 
    115 static struct _balloon_info_ bl_info;
    116 
    117 /**
    118  * intel_vgt_deballoon - deballoon reserved graphics address trunks
    119  *
    120  * This function is called to deallocate the ballooned-out graphic memory, when
    121  * driver is unloaded or when ballooning fails.
    122  */
    123 void intel_vgt_deballoon(void)
    124 {
    125 	int i;
    126 
    127 	DRM_DEBUG("VGT deballoon.\n");
    128 
    129 	for (i = 0; i < 4; i++) {
    130 		if (bl_info.space[i].allocated)
    131 			drm_mm_remove_node(&bl_info.space[i]);
    132 	}
    133 
    134 	memset(&bl_info, 0, sizeof(bl_info));
    135 }
    136 
    137 static int vgt_balloon_space(struct drm_mm *mm,
    138 			     struct drm_mm_node *node,
    139 			     unsigned long start, unsigned long end)
    140 {
    141 	unsigned long size = end - start;
    142 
    143 	if (start == end)
    144 		return -EINVAL;
    145 
    146 	DRM_INFO("balloon space: range [ 0x%lx - 0x%lx ] %lu KiB.\n",
    147 		 start, end, size / 1024);
    148 
    149 	node->start = start;
    150 	node->size = size;
    151 
    152 	return drm_mm_reserve_node(mm, node);
    153 }
    154 
    155 /**
    156  * intel_vgt_balloon - balloon out reserved graphics address trunks
    157  * @dev: drm device
    158  *
    159  * This function is called at the initialization stage, to balloon out the
    160  * graphic address space allocated to other vGPUs, by marking these spaces as
    161  * reserved. The ballooning related knowledge(starting address and size of
    162  * the mappable/unmappable graphic memory) is described in the vgt_if structure
    163  * in a reserved mmio range.
    164  *
    165  * To give an example, the drawing below depicts one typical scenario after
    166  * ballooning. Here the vGPU1 has 2 pieces of graphic address spaces ballooned
    167  * out each for the mappable and the non-mappable part. From the vGPU1 point of
    168  * view, the total size is the same as the physical one, with the start address
    169  * of its graphic space being zero. Yet there are some portions ballooned out(
    170  * the shadow part, which are marked as reserved by drm allocator). From the
    171  * host point of view, the graphic address space is partitioned by multiple
    172  * vGPUs in different VMs.
    173  *
    174  *                        vGPU1 view         Host view
    175  *             0 ------> +-----------+     +-----------+
    176  *               ^       |///////////|     |   vGPU3   |
    177  *               |       |///////////|     +-----------+
    178  *               |       |///////////|     |   vGPU2   |
    179  *               |       +-----------+     +-----------+
    180  *        mappable GM    | available | ==> |   vGPU1   |
    181  *               |       +-----------+     +-----------+
    182  *               |       |///////////|     |           |
    183  *               v       |///////////|     |   Host    |
    184  *               +=======+===========+     +===========+
    185  *               ^       |///////////|     |   vGPU3   |
    186  *               |       |///////////|     +-----------+
    187  *               |       |///////////|     |   vGPU2   |
    188  *               |       +-----------+     +-----------+
    189  *      unmappable GM    | available | ==> |   vGPU1   |
    190  *               |       +-----------+     +-----------+
    191  *               |       |///////////|     |           |
    192  *               |       |///////////|     |   Host    |
    193  *               v       |///////////|     |           |
    194  * total GM size ------> +-----------+     +-----------+
    195  *
    196  * Returns:
    197  * zero on success, non-zero if configuration invalid or ballooning failed
    198  */
    199 int intel_vgt_balloon(struct drm_device *dev)
    200 {
    201 	struct drm_i915_private *dev_priv = to_i915(dev);
    202 	struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
    203 	unsigned long ggtt_vm_end = ggtt_vm->start + ggtt_vm->total;
    204 
    205 	unsigned long mappable_base, mappable_size, mappable_end;
    206 	unsigned long unmappable_base, unmappable_size, unmappable_end;
    207 	int ret;
    208 
    209 	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 
    223 	if (mappable_base < ggtt_vm->start ||
    224 	    mappable_end > dev_priv->gtt.mappable_end ||
    225 	    unmappable_base < dev_priv->gtt.mappable_end ||
    226 	    unmappable_end > ggtt_vm_end) {
    227 		DRM_ERROR("Invalid ballooning configuration!\n");
    228 		return -EINVAL;
    229 	}
    230 
    231 	/* Unmappable graphic memory ballooning */
    232 	if (unmappable_base > dev_priv->gtt.mappable_end) {
    233 		ret = vgt_balloon_space(&ggtt_vm->mm,
    234 					&bl_info.space[2],
    235 					dev_priv->gtt.mappable_end,
    236 					unmappable_base);
    237 
    238 		if (ret)
    239 			goto err;
    240 	}
    241 
    242 	/*
    243 	 * No need to partition out the last physical page,
    244 	 * because it is reserved to the guard page.
    245 	 */
    246 	if (unmappable_end < ggtt_vm_end - PAGE_SIZE) {
    247 		ret = vgt_balloon_space(&ggtt_vm->mm,
    248 					&bl_info.space[3],
    249 					unmappable_end,
    250 					ggtt_vm_end - PAGE_SIZE);
    251 		if (ret)
    252 			goto err;
    253 	}
    254 
    255 	/* Mappable graphic memory ballooning */
    256 	if (mappable_base > ggtt_vm->start) {
    257 		ret = vgt_balloon_space(&ggtt_vm->mm,
    258 					&bl_info.space[0],
    259 					ggtt_vm->start, mappable_base);
    260 
    261 		if (ret)
    262 			goto err;
    263 	}
    264 
    265 	if (mappable_end < dev_priv->gtt.mappable_end) {
    266 		ret = vgt_balloon_space(&ggtt_vm->mm,
    267 					&bl_info.space[1],
    268 					mappable_end,
    269 					dev_priv->gtt.mappable_end);
    270 
    271 		if (ret)
    272 			goto err;
    273 	}
    274 
    275 	DRM_INFO("VGT balloon successfully\n");
    276 	return 0;
    277 
    278 err:
    279 	DRM_ERROR("VGT balloon fail\n");
    280 	intel_vgt_deballoon();
    281 	return ret;
    282 }
    283