1 1.3 riastrad /* $NetBSD: dmabuf.c,v 1.3 2021/12/19 11:06:55 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2017 Intel Corporation. All rights reserved. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice (including the next 14 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 15 1.1 riastrad * Software. 16 1.1 riastrad * 17 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 1.1 riastrad * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 1.1 riastrad * DEALINGS IN THE SOFTWARE. 24 1.1 riastrad * 25 1.1 riastrad * Authors: 26 1.1 riastrad * Zhiyuan Lv <zhiyuan.lv (at) intel.com> 27 1.1 riastrad * 28 1.1 riastrad * Contributors: 29 1.1 riastrad * Xiaoguang Chen 30 1.1 riastrad * Tina Zhang <tina.zhang (at) intel.com> 31 1.1 riastrad */ 32 1.1 riastrad 33 1.1 riastrad #include <sys/cdefs.h> 34 1.3 riastrad __KERNEL_RCSID(0, "$NetBSD: dmabuf.c,v 1.3 2021/12/19 11:06:55 riastradh Exp $"); 35 1.1 riastrad 36 1.1 riastrad #include <linux/dma-buf.h> 37 1.1 riastrad #include <linux/vfio.h> 38 1.1 riastrad 39 1.1 riastrad #include "i915_drv.h" 40 1.1 riastrad #include "gvt.h" 41 1.1 riastrad 42 1.1 riastrad #define GEN8_DECODE_PTE(pte) (pte & GENMASK_ULL(63, 12)) 43 1.1 riastrad 44 1.1 riastrad static int vgpu_pin_dma_address(struct intel_vgpu *vgpu, 45 1.1 riastrad unsigned long size, 46 1.1 riastrad dma_addr_t dma_addr) 47 1.1 riastrad { 48 1.1 riastrad int ret = 0; 49 1.1 riastrad 50 1.1 riastrad if (intel_gvt_hypervisor_dma_pin_guest_page(vgpu, dma_addr)) 51 1.1 riastrad ret = -EINVAL; 52 1.1 riastrad 53 1.1 riastrad return ret; 54 1.1 riastrad } 55 1.1 riastrad 56 1.1 riastrad static void vgpu_unpin_dma_address(struct intel_vgpu *vgpu, 57 1.1 riastrad dma_addr_t dma_addr) 58 1.1 riastrad { 59 1.1 riastrad intel_gvt_hypervisor_dma_unmap_guest_page(vgpu, dma_addr); 60 1.1 riastrad } 61 1.1 riastrad 62 1.1 riastrad static int vgpu_gem_get_pages( 63 1.1 riastrad struct drm_i915_gem_object *obj) 64 1.1 riastrad { 65 1.1 riastrad struct drm_i915_private *dev_priv = to_i915(obj->base.dev); 66 1.1 riastrad struct intel_vgpu *vgpu; 67 1.1 riastrad struct sg_table *st; 68 1.1 riastrad struct scatterlist *sg; 69 1.1 riastrad int i, j, ret; 70 1.1 riastrad gen8_pte_t __iomem *gtt_entries; 71 1.1 riastrad struct intel_vgpu_fb_info *fb_info; 72 1.1 riastrad u32 page_num; 73 1.1 riastrad 74 1.1 riastrad fb_info = (struct intel_vgpu_fb_info *)obj->gvt_info; 75 1.1 riastrad if (WARN_ON(!fb_info)) 76 1.1 riastrad return -ENODEV; 77 1.1 riastrad 78 1.1 riastrad vgpu = fb_info->obj->vgpu; 79 1.1 riastrad if (WARN_ON(!vgpu)) 80 1.1 riastrad return -ENODEV; 81 1.1 riastrad 82 1.1 riastrad st = kmalloc(sizeof(*st), GFP_KERNEL); 83 1.1 riastrad if (unlikely(!st)) 84 1.1 riastrad return -ENOMEM; 85 1.1 riastrad 86 1.1 riastrad page_num = obj->base.size >> PAGE_SHIFT; 87 1.1 riastrad ret = sg_alloc_table(st, page_num, GFP_KERNEL); 88 1.1 riastrad if (ret) { 89 1.1 riastrad kfree(st); 90 1.1 riastrad return ret; 91 1.1 riastrad } 92 1.1 riastrad gtt_entries = (gen8_pte_t __iomem *)dev_priv->ggtt.gsm + 93 1.1 riastrad (fb_info->start >> PAGE_SHIFT); 94 1.1 riastrad for_each_sg(st->sgl, sg, page_num, i) { 95 1.1 riastrad dma_addr_t dma_addr = 96 1.1 riastrad GEN8_DECODE_PTE(readq(>t_entries[i])); 97 1.1 riastrad if (vgpu_pin_dma_address(vgpu, PAGE_SIZE, dma_addr)) { 98 1.1 riastrad ret = -EINVAL; 99 1.1 riastrad goto out; 100 1.1 riastrad } 101 1.1 riastrad 102 1.1 riastrad sg->offset = 0; 103 1.1 riastrad sg->length = PAGE_SIZE; 104 1.1 riastrad sg_dma_len(sg) = PAGE_SIZE; 105 1.1 riastrad sg_dma_address(sg) = dma_addr; 106 1.1 riastrad } 107 1.1 riastrad 108 1.1 riastrad __i915_gem_object_set_pages(obj, st, PAGE_SIZE); 109 1.1 riastrad out: 110 1.1 riastrad if (ret) { 111 1.1 riastrad dma_addr_t dma_addr; 112 1.1 riastrad 113 1.1 riastrad for_each_sg(st->sgl, sg, i, j) { 114 1.1 riastrad dma_addr = sg_dma_address(sg); 115 1.1 riastrad if (dma_addr) 116 1.1 riastrad vgpu_unpin_dma_address(vgpu, dma_addr); 117 1.1 riastrad } 118 1.1 riastrad sg_free_table(st); 119 1.1 riastrad kfree(st); 120 1.1 riastrad } 121 1.1 riastrad 122 1.1 riastrad return ret; 123 1.1 riastrad 124 1.1 riastrad } 125 1.1 riastrad 126 1.1 riastrad static void vgpu_gem_put_pages(struct drm_i915_gem_object *obj, 127 1.1 riastrad struct sg_table *pages) 128 1.1 riastrad { 129 1.1 riastrad struct scatterlist *sg; 130 1.1 riastrad 131 1.1 riastrad if (obj->base.dma_buf) { 132 1.1 riastrad struct intel_vgpu_fb_info *fb_info = obj->gvt_info; 133 1.1 riastrad struct intel_vgpu_dmabuf_obj *obj = fb_info->obj; 134 1.1 riastrad struct intel_vgpu *vgpu = obj->vgpu; 135 1.1 riastrad int i; 136 1.1 riastrad 137 1.1 riastrad for_each_sg(pages->sgl, sg, fb_info->size, i) 138 1.1 riastrad vgpu_unpin_dma_address(vgpu, 139 1.1 riastrad sg_dma_address(sg)); 140 1.1 riastrad } 141 1.1 riastrad 142 1.1 riastrad sg_free_table(pages); 143 1.1 riastrad kfree(pages); 144 1.1 riastrad } 145 1.1 riastrad 146 1.1 riastrad static void dmabuf_gem_object_free(struct kref *kref) 147 1.1 riastrad { 148 1.1 riastrad struct intel_vgpu_dmabuf_obj *obj = 149 1.1 riastrad container_of(kref, struct intel_vgpu_dmabuf_obj, kref); 150 1.1 riastrad struct intel_vgpu *vgpu = obj->vgpu; 151 1.1 riastrad struct list_head *pos; 152 1.1 riastrad struct intel_vgpu_dmabuf_obj *dmabuf_obj; 153 1.1 riastrad 154 1.1 riastrad if (vgpu && vgpu->active && !list_empty(&vgpu->dmabuf_obj_list_head)) { 155 1.1 riastrad list_for_each(pos, &vgpu->dmabuf_obj_list_head) { 156 1.1 riastrad dmabuf_obj = container_of(pos, 157 1.1 riastrad struct intel_vgpu_dmabuf_obj, list); 158 1.1 riastrad if (dmabuf_obj == obj) { 159 1.1 riastrad intel_gvt_hypervisor_put_vfio_device(vgpu); 160 1.1 riastrad idr_remove(&vgpu->object_idr, 161 1.1 riastrad dmabuf_obj->dmabuf_id); 162 1.1 riastrad kfree(dmabuf_obj->info); 163 1.1 riastrad kfree(dmabuf_obj); 164 1.1 riastrad list_del(pos); 165 1.1 riastrad break; 166 1.1 riastrad } 167 1.1 riastrad } 168 1.1 riastrad } else { 169 1.1 riastrad /* Free the orphan dmabuf_objs here */ 170 1.1 riastrad kfree(obj->info); 171 1.1 riastrad kfree(obj); 172 1.1 riastrad } 173 1.1 riastrad } 174 1.1 riastrad 175 1.1 riastrad 176 1.1 riastrad static inline void dmabuf_obj_get(struct intel_vgpu_dmabuf_obj *obj) 177 1.1 riastrad { 178 1.1 riastrad kref_get(&obj->kref); 179 1.1 riastrad } 180 1.1 riastrad 181 1.1 riastrad static inline void dmabuf_obj_put(struct intel_vgpu_dmabuf_obj *obj) 182 1.1 riastrad { 183 1.1 riastrad kref_put(&obj->kref, dmabuf_gem_object_free); 184 1.1 riastrad } 185 1.1 riastrad 186 1.1 riastrad static void vgpu_gem_release(struct drm_i915_gem_object *gem_obj) 187 1.1 riastrad { 188 1.1 riastrad 189 1.1 riastrad struct intel_vgpu_fb_info *fb_info = gem_obj->gvt_info; 190 1.1 riastrad struct intel_vgpu_dmabuf_obj *obj = fb_info->obj; 191 1.1 riastrad struct intel_vgpu *vgpu = obj->vgpu; 192 1.1 riastrad 193 1.1 riastrad if (vgpu) { 194 1.1 riastrad mutex_lock(&vgpu->dmabuf_lock); 195 1.1 riastrad gem_obj->base.dma_buf = NULL; 196 1.1 riastrad dmabuf_obj_put(obj); 197 1.1 riastrad mutex_unlock(&vgpu->dmabuf_lock); 198 1.1 riastrad } else { 199 1.1 riastrad /* vgpu is NULL, as it has been removed already */ 200 1.1 riastrad gem_obj->base.dma_buf = NULL; 201 1.1 riastrad dmabuf_obj_put(obj); 202 1.1 riastrad } 203 1.1 riastrad } 204 1.1 riastrad 205 1.1 riastrad static const struct drm_i915_gem_object_ops intel_vgpu_gem_ops = { 206 1.1 riastrad .flags = I915_GEM_OBJECT_IS_PROXY, 207 1.1 riastrad .get_pages = vgpu_gem_get_pages, 208 1.1 riastrad .put_pages = vgpu_gem_put_pages, 209 1.1 riastrad .release = vgpu_gem_release, 210 1.1 riastrad }; 211 1.1 riastrad 212 1.1 riastrad static struct drm_i915_gem_object *vgpu_create_gem(struct drm_device *dev, 213 1.1 riastrad struct intel_vgpu_fb_info *info) 214 1.1 riastrad { 215 1.1 riastrad static struct lock_class_key lock_class; 216 1.1 riastrad struct drm_i915_private *dev_priv = to_i915(dev); 217 1.1 riastrad struct drm_i915_gem_object *obj; 218 1.1 riastrad 219 1.1 riastrad obj = i915_gem_object_alloc(); 220 1.1 riastrad if (obj == NULL) 221 1.1 riastrad return NULL; 222 1.1 riastrad 223 1.1 riastrad drm_gem_private_object_init(dev, &obj->base, 224 1.1 riastrad roundup(info->size, PAGE_SIZE)); 225 1.1 riastrad i915_gem_object_init(obj, &intel_vgpu_gem_ops, &lock_class); 226 1.1 riastrad i915_gem_object_set_readonly(obj); 227 1.1 riastrad 228 1.1 riastrad obj->read_domains = I915_GEM_DOMAIN_GTT; 229 1.1 riastrad obj->write_domain = 0; 230 1.1 riastrad if (INTEL_GEN(dev_priv) >= 9) { 231 1.1 riastrad unsigned int tiling_mode = 0; 232 1.1 riastrad unsigned int stride = 0; 233 1.1 riastrad 234 1.1 riastrad switch (info->drm_format_mod) { 235 1.1 riastrad case DRM_FORMAT_MOD_LINEAR: 236 1.1 riastrad tiling_mode = I915_TILING_NONE; 237 1.1 riastrad break; 238 1.1 riastrad case I915_FORMAT_MOD_X_TILED: 239 1.1 riastrad tiling_mode = I915_TILING_X; 240 1.1 riastrad stride = info->stride; 241 1.1 riastrad break; 242 1.1 riastrad case I915_FORMAT_MOD_Y_TILED: 243 1.1 riastrad case I915_FORMAT_MOD_Yf_TILED: 244 1.1 riastrad tiling_mode = I915_TILING_Y; 245 1.1 riastrad stride = info->stride; 246 1.1 riastrad break; 247 1.1 riastrad default: 248 1.1 riastrad gvt_dbg_core("invalid drm_format_mod %llx for tiling\n", 249 1.1 riastrad info->drm_format_mod); 250 1.1 riastrad } 251 1.1 riastrad obj->tiling_and_stride = tiling_mode | stride; 252 1.1 riastrad } else { 253 1.1 riastrad obj->tiling_and_stride = info->drm_format_mod ? 254 1.1 riastrad I915_TILING_X : 0; 255 1.1 riastrad } 256 1.1 riastrad 257 1.1 riastrad return obj; 258 1.1 riastrad } 259 1.1 riastrad 260 1.1 riastrad static bool validate_hotspot(struct intel_vgpu_cursor_plane_format *c) 261 1.1 riastrad { 262 1.1 riastrad if (c && c->x_hot <= c->width && c->y_hot <= c->height) 263 1.1 riastrad return true; 264 1.1 riastrad else 265 1.1 riastrad return false; 266 1.1 riastrad } 267 1.1 riastrad 268 1.1 riastrad static int vgpu_get_plane_info(struct drm_device *dev, 269 1.1 riastrad struct intel_vgpu *vgpu, 270 1.1 riastrad struct intel_vgpu_fb_info *info, 271 1.1 riastrad int plane_id) 272 1.1 riastrad { 273 1.1 riastrad struct intel_vgpu_primary_plane_format p; 274 1.1 riastrad struct intel_vgpu_cursor_plane_format c; 275 1.1 riastrad int ret, tile_height = 1; 276 1.1 riastrad 277 1.1 riastrad memset(info, 0, sizeof(*info)); 278 1.1 riastrad 279 1.1 riastrad if (plane_id == DRM_PLANE_TYPE_PRIMARY) { 280 1.1 riastrad ret = intel_vgpu_decode_primary_plane(vgpu, &p); 281 1.1 riastrad if (ret) 282 1.1 riastrad return ret; 283 1.1 riastrad info->start = p.base; 284 1.1 riastrad info->start_gpa = p.base_gpa; 285 1.1 riastrad info->width = p.width; 286 1.1 riastrad info->height = p.height; 287 1.1 riastrad info->stride = p.stride; 288 1.1 riastrad info->drm_format = p.drm_format; 289 1.1 riastrad 290 1.1 riastrad switch (p.tiled) { 291 1.1 riastrad case PLANE_CTL_TILED_LINEAR: 292 1.1 riastrad info->drm_format_mod = DRM_FORMAT_MOD_LINEAR; 293 1.1 riastrad break; 294 1.1 riastrad case PLANE_CTL_TILED_X: 295 1.1 riastrad info->drm_format_mod = I915_FORMAT_MOD_X_TILED; 296 1.1 riastrad tile_height = 8; 297 1.1 riastrad break; 298 1.1 riastrad case PLANE_CTL_TILED_Y: 299 1.1 riastrad info->drm_format_mod = I915_FORMAT_MOD_Y_TILED; 300 1.1 riastrad tile_height = 32; 301 1.1 riastrad break; 302 1.1 riastrad case PLANE_CTL_TILED_YF: 303 1.1 riastrad info->drm_format_mod = I915_FORMAT_MOD_Yf_TILED; 304 1.1 riastrad tile_height = 32; 305 1.1 riastrad break; 306 1.1 riastrad default: 307 1.1 riastrad gvt_vgpu_err("invalid tiling mode: %x\n", p.tiled); 308 1.1 riastrad } 309 1.1 riastrad } else if (plane_id == DRM_PLANE_TYPE_CURSOR) { 310 1.1 riastrad ret = intel_vgpu_decode_cursor_plane(vgpu, &c); 311 1.1 riastrad if (ret) 312 1.1 riastrad return ret; 313 1.1 riastrad info->start = c.base; 314 1.1 riastrad info->start_gpa = c.base_gpa; 315 1.1 riastrad info->width = c.width; 316 1.1 riastrad info->height = c.height; 317 1.1 riastrad info->stride = c.width * (c.bpp / 8); 318 1.1 riastrad info->drm_format = c.drm_format; 319 1.1 riastrad info->drm_format_mod = 0; 320 1.1 riastrad info->x_pos = c.x_pos; 321 1.1 riastrad info->y_pos = c.y_pos; 322 1.1 riastrad 323 1.1 riastrad if (validate_hotspot(&c)) { 324 1.1 riastrad info->x_hot = c.x_hot; 325 1.1 riastrad info->y_hot = c.y_hot; 326 1.1 riastrad } else { 327 1.1 riastrad info->x_hot = UINT_MAX; 328 1.1 riastrad info->y_hot = UINT_MAX; 329 1.1 riastrad } 330 1.1 riastrad } else { 331 1.1 riastrad gvt_vgpu_err("invalid plane id:%d\n", plane_id); 332 1.1 riastrad return -EINVAL; 333 1.1 riastrad } 334 1.1 riastrad 335 1.1 riastrad info->size = info->stride * roundup(info->height, tile_height); 336 1.1 riastrad if (info->size == 0) { 337 1.1 riastrad gvt_vgpu_err("fb size is zero\n"); 338 1.1 riastrad return -EINVAL; 339 1.1 riastrad } 340 1.1 riastrad 341 1.1 riastrad if (info->start & (PAGE_SIZE - 1)) { 342 1.1 riastrad gvt_vgpu_err("Not aligned fb address:0x%llx\n", info->start); 343 1.1 riastrad return -EFAULT; 344 1.1 riastrad } 345 1.1 riastrad 346 1.1 riastrad if (!intel_gvt_ggtt_validate_range(vgpu, info->start, info->size)) { 347 1.1 riastrad gvt_vgpu_err("invalid gma addr\n"); 348 1.1 riastrad return -EFAULT; 349 1.1 riastrad } 350 1.1 riastrad 351 1.1 riastrad return 0; 352 1.1 riastrad } 353 1.1 riastrad 354 1.1 riastrad static struct intel_vgpu_dmabuf_obj * 355 1.1 riastrad pick_dmabuf_by_info(struct intel_vgpu *vgpu, 356 1.1 riastrad struct intel_vgpu_fb_info *latest_info) 357 1.1 riastrad { 358 1.1 riastrad struct list_head *pos; 359 1.1 riastrad struct intel_vgpu_fb_info *fb_info; 360 1.1 riastrad struct intel_vgpu_dmabuf_obj *dmabuf_obj = NULL; 361 1.1 riastrad struct intel_vgpu_dmabuf_obj *ret = NULL; 362 1.1 riastrad 363 1.1 riastrad list_for_each(pos, &vgpu->dmabuf_obj_list_head) { 364 1.1 riastrad dmabuf_obj = container_of(pos, struct intel_vgpu_dmabuf_obj, 365 1.1 riastrad list); 366 1.1 riastrad if ((dmabuf_obj == NULL) || 367 1.1 riastrad (dmabuf_obj->info == NULL)) 368 1.1 riastrad continue; 369 1.1 riastrad 370 1.1 riastrad fb_info = (struct intel_vgpu_fb_info *)dmabuf_obj->info; 371 1.1 riastrad if ((fb_info->start == latest_info->start) && 372 1.1 riastrad (fb_info->start_gpa == latest_info->start_gpa) && 373 1.1 riastrad (fb_info->size == latest_info->size) && 374 1.1 riastrad (fb_info->drm_format_mod == latest_info->drm_format_mod) && 375 1.1 riastrad (fb_info->drm_format == latest_info->drm_format) && 376 1.1 riastrad (fb_info->width == latest_info->width) && 377 1.1 riastrad (fb_info->height == latest_info->height)) { 378 1.1 riastrad ret = dmabuf_obj; 379 1.1 riastrad break; 380 1.1 riastrad } 381 1.1 riastrad } 382 1.1 riastrad 383 1.1 riastrad return ret; 384 1.1 riastrad } 385 1.1 riastrad 386 1.1 riastrad static struct intel_vgpu_dmabuf_obj * 387 1.1 riastrad pick_dmabuf_by_num(struct intel_vgpu *vgpu, u32 id) 388 1.1 riastrad { 389 1.1 riastrad struct list_head *pos; 390 1.1 riastrad struct intel_vgpu_dmabuf_obj *dmabuf_obj = NULL; 391 1.1 riastrad struct intel_vgpu_dmabuf_obj *ret = NULL; 392 1.1 riastrad 393 1.1 riastrad list_for_each(pos, &vgpu->dmabuf_obj_list_head) { 394 1.1 riastrad dmabuf_obj = container_of(pos, struct intel_vgpu_dmabuf_obj, 395 1.1 riastrad list); 396 1.1 riastrad if (!dmabuf_obj) 397 1.1 riastrad continue; 398 1.1 riastrad 399 1.1 riastrad if (dmabuf_obj->dmabuf_id == id) { 400 1.1 riastrad ret = dmabuf_obj; 401 1.1 riastrad break; 402 1.1 riastrad } 403 1.1 riastrad } 404 1.1 riastrad 405 1.1 riastrad return ret; 406 1.1 riastrad } 407 1.1 riastrad 408 1.1 riastrad static void update_fb_info(struct vfio_device_gfx_plane_info *gvt_dmabuf, 409 1.1 riastrad struct intel_vgpu_fb_info *fb_info) 410 1.1 riastrad { 411 1.1 riastrad gvt_dmabuf->drm_format = fb_info->drm_format; 412 1.1 riastrad gvt_dmabuf->drm_format_mod = fb_info->drm_format_mod; 413 1.1 riastrad gvt_dmabuf->width = fb_info->width; 414 1.1 riastrad gvt_dmabuf->height = fb_info->height; 415 1.1 riastrad gvt_dmabuf->stride = fb_info->stride; 416 1.1 riastrad gvt_dmabuf->size = fb_info->size; 417 1.1 riastrad gvt_dmabuf->x_pos = fb_info->x_pos; 418 1.1 riastrad gvt_dmabuf->y_pos = fb_info->y_pos; 419 1.1 riastrad gvt_dmabuf->x_hot = fb_info->x_hot; 420 1.1 riastrad gvt_dmabuf->y_hot = fb_info->y_hot; 421 1.1 riastrad } 422 1.1 riastrad 423 1.1 riastrad int intel_vgpu_query_plane(struct intel_vgpu *vgpu, void *args) 424 1.1 riastrad { 425 1.1 riastrad struct drm_device *dev = &vgpu->gvt->dev_priv->drm; 426 1.1 riastrad struct vfio_device_gfx_plane_info *gfx_plane_info = args; 427 1.1 riastrad struct intel_vgpu_dmabuf_obj *dmabuf_obj; 428 1.1 riastrad struct intel_vgpu_fb_info fb_info; 429 1.1 riastrad int ret = 0; 430 1.1 riastrad 431 1.1 riastrad if (gfx_plane_info->flags == (VFIO_GFX_PLANE_TYPE_DMABUF | 432 1.1 riastrad VFIO_GFX_PLANE_TYPE_PROBE)) 433 1.1 riastrad return ret; 434 1.1 riastrad else if ((gfx_plane_info->flags & ~VFIO_GFX_PLANE_TYPE_DMABUF) || 435 1.1 riastrad (!gfx_plane_info->flags)) 436 1.1 riastrad return -EINVAL; 437 1.1 riastrad 438 1.1 riastrad ret = vgpu_get_plane_info(dev, vgpu, &fb_info, 439 1.1 riastrad gfx_plane_info->drm_plane_type); 440 1.1 riastrad if (ret != 0) 441 1.1 riastrad goto out; 442 1.1 riastrad 443 1.1 riastrad mutex_lock(&vgpu->dmabuf_lock); 444 1.1 riastrad /* If exists, pick up the exposed dmabuf_obj */ 445 1.1 riastrad dmabuf_obj = pick_dmabuf_by_info(vgpu, &fb_info); 446 1.1 riastrad if (dmabuf_obj) { 447 1.1 riastrad update_fb_info(gfx_plane_info, &fb_info); 448 1.1 riastrad gfx_plane_info->dmabuf_id = dmabuf_obj->dmabuf_id; 449 1.1 riastrad 450 1.1 riastrad /* This buffer may be released between query_plane ioctl and 451 1.1 riastrad * get_dmabuf ioctl. Add the refcount to make sure it won't 452 1.1 riastrad * be released between the two ioctls. 453 1.1 riastrad */ 454 1.1 riastrad if (!dmabuf_obj->initref) { 455 1.1 riastrad dmabuf_obj->initref = true; 456 1.1 riastrad dmabuf_obj_get(dmabuf_obj); 457 1.1 riastrad } 458 1.1 riastrad ret = 0; 459 1.1 riastrad gvt_dbg_dpy("vgpu%d: re-use dmabuf_obj ref %d, id %d\n", 460 1.1 riastrad vgpu->id, kref_read(&dmabuf_obj->kref), 461 1.1 riastrad gfx_plane_info->dmabuf_id); 462 1.1 riastrad mutex_unlock(&vgpu->dmabuf_lock); 463 1.1 riastrad goto out; 464 1.1 riastrad } 465 1.1 riastrad 466 1.1 riastrad mutex_unlock(&vgpu->dmabuf_lock); 467 1.1 riastrad 468 1.1 riastrad /* Need to allocate a new one*/ 469 1.1 riastrad dmabuf_obj = kmalloc(sizeof(struct intel_vgpu_dmabuf_obj), GFP_KERNEL); 470 1.1 riastrad if (unlikely(!dmabuf_obj)) { 471 1.1 riastrad gvt_vgpu_err("alloc dmabuf_obj failed\n"); 472 1.1 riastrad ret = -ENOMEM; 473 1.1 riastrad goto out; 474 1.1 riastrad } 475 1.1 riastrad 476 1.1 riastrad dmabuf_obj->info = kmalloc(sizeof(struct intel_vgpu_fb_info), 477 1.1 riastrad GFP_KERNEL); 478 1.1 riastrad if (unlikely(!dmabuf_obj->info)) { 479 1.1 riastrad gvt_vgpu_err("allocate intel vgpu fb info failed\n"); 480 1.1 riastrad ret = -ENOMEM; 481 1.1 riastrad goto out_free_dmabuf; 482 1.1 riastrad } 483 1.1 riastrad memcpy(dmabuf_obj->info, &fb_info, sizeof(struct intel_vgpu_fb_info)); 484 1.1 riastrad 485 1.1 riastrad ((struct intel_vgpu_fb_info *)dmabuf_obj->info)->obj = dmabuf_obj; 486 1.1 riastrad 487 1.1 riastrad dmabuf_obj->vgpu = vgpu; 488 1.1 riastrad 489 1.3 riastrad idr_preload(GFP_NOWAIT); /* XXX ??? */ 490 1.1 riastrad ret = idr_alloc(&vgpu->object_idr, dmabuf_obj, 1, 0, GFP_NOWAIT); 491 1.3 riastrad idr_preload_end(); 492 1.1 riastrad if (ret < 0) 493 1.1 riastrad goto out_free_info; 494 1.1 riastrad gfx_plane_info->dmabuf_id = ret; 495 1.1 riastrad dmabuf_obj->dmabuf_id = ret; 496 1.1 riastrad 497 1.1 riastrad dmabuf_obj->initref = true; 498 1.1 riastrad 499 1.1 riastrad kref_init(&dmabuf_obj->kref); 500 1.1 riastrad 501 1.1 riastrad mutex_lock(&vgpu->dmabuf_lock); 502 1.1 riastrad if (intel_gvt_hypervisor_get_vfio_device(vgpu)) { 503 1.1 riastrad gvt_vgpu_err("get vfio device failed\n"); 504 1.1 riastrad mutex_unlock(&vgpu->dmabuf_lock); 505 1.1 riastrad goto out_free_info; 506 1.1 riastrad } 507 1.1 riastrad mutex_unlock(&vgpu->dmabuf_lock); 508 1.1 riastrad 509 1.1 riastrad update_fb_info(gfx_plane_info, &fb_info); 510 1.1 riastrad 511 1.1 riastrad INIT_LIST_HEAD(&dmabuf_obj->list); 512 1.1 riastrad mutex_lock(&vgpu->dmabuf_lock); 513 1.1 riastrad list_add_tail(&dmabuf_obj->list, &vgpu->dmabuf_obj_list_head); 514 1.1 riastrad mutex_unlock(&vgpu->dmabuf_lock); 515 1.1 riastrad 516 1.1 riastrad gvt_dbg_dpy("vgpu%d: %s new dmabuf_obj ref %d, id %d\n", vgpu->id, 517 1.1 riastrad __func__, kref_read(&dmabuf_obj->kref), ret); 518 1.1 riastrad 519 1.1 riastrad return 0; 520 1.1 riastrad 521 1.1 riastrad out_free_info: 522 1.1 riastrad kfree(dmabuf_obj->info); 523 1.1 riastrad out_free_dmabuf: 524 1.1 riastrad kfree(dmabuf_obj); 525 1.1 riastrad out: 526 1.1 riastrad /* ENODEV means plane isn't ready, which might be a normal case. */ 527 1.1 riastrad return (ret == -ENODEV) ? 0 : ret; 528 1.1 riastrad } 529 1.1 riastrad 530 1.1 riastrad /* To associate an exposed dmabuf with the dmabuf_obj */ 531 1.1 riastrad int intel_vgpu_get_dmabuf(struct intel_vgpu *vgpu, unsigned int dmabuf_id) 532 1.1 riastrad { 533 1.1 riastrad struct drm_device *dev = &vgpu->gvt->dev_priv->drm; 534 1.1 riastrad struct intel_vgpu_dmabuf_obj *dmabuf_obj; 535 1.1 riastrad struct drm_i915_gem_object *obj; 536 1.1 riastrad struct dma_buf *dmabuf; 537 1.1 riastrad int dmabuf_fd; 538 1.1 riastrad int ret = 0; 539 1.1 riastrad 540 1.1 riastrad mutex_lock(&vgpu->dmabuf_lock); 541 1.1 riastrad 542 1.1 riastrad dmabuf_obj = pick_dmabuf_by_num(vgpu, dmabuf_id); 543 1.1 riastrad if (dmabuf_obj == NULL) { 544 1.1 riastrad gvt_vgpu_err("invalid dmabuf id:%d\n", dmabuf_id); 545 1.1 riastrad ret = -EINVAL; 546 1.1 riastrad goto out; 547 1.1 riastrad } 548 1.1 riastrad 549 1.1 riastrad obj = vgpu_create_gem(dev, dmabuf_obj->info); 550 1.1 riastrad if (obj == NULL) { 551 1.1 riastrad gvt_vgpu_err("create gvt gem obj failed\n"); 552 1.1 riastrad ret = -ENOMEM; 553 1.1 riastrad goto out; 554 1.1 riastrad } 555 1.1 riastrad 556 1.1 riastrad obj->gvt_info = dmabuf_obj->info; 557 1.1 riastrad 558 1.1 riastrad dmabuf = i915_gem_prime_export(&obj->base, DRM_CLOEXEC | DRM_RDWR); 559 1.1 riastrad if (IS_ERR(dmabuf)) { 560 1.1 riastrad gvt_vgpu_err("export dma-buf failed\n"); 561 1.1 riastrad ret = PTR_ERR(dmabuf); 562 1.1 riastrad goto out_free_gem; 563 1.1 riastrad } 564 1.1 riastrad 565 1.1 riastrad ret = dma_buf_fd(dmabuf, DRM_CLOEXEC | DRM_RDWR); 566 1.1 riastrad if (ret < 0) { 567 1.1 riastrad gvt_vgpu_err("create dma-buf fd failed ret:%d\n", ret); 568 1.1 riastrad goto out_free_dmabuf; 569 1.1 riastrad } 570 1.1 riastrad dmabuf_fd = ret; 571 1.1 riastrad 572 1.1 riastrad dmabuf_obj_get(dmabuf_obj); 573 1.1 riastrad 574 1.1 riastrad if (dmabuf_obj->initref) { 575 1.1 riastrad dmabuf_obj->initref = false; 576 1.1 riastrad dmabuf_obj_put(dmabuf_obj); 577 1.1 riastrad } 578 1.1 riastrad 579 1.1 riastrad mutex_unlock(&vgpu->dmabuf_lock); 580 1.1 riastrad 581 1.1 riastrad gvt_dbg_dpy("vgpu%d: dmabuf:%d, dmabuf ref %d, fd:%d\n" 582 1.1 riastrad " file count: %ld, GEM ref: %d\n", 583 1.1 riastrad vgpu->id, dmabuf_obj->dmabuf_id, 584 1.1 riastrad kref_read(&dmabuf_obj->kref), 585 1.1 riastrad dmabuf_fd, 586 1.1 riastrad file_count(dmabuf->file), 587 1.1 riastrad kref_read(&obj->base.refcount)); 588 1.1 riastrad 589 1.1 riastrad i915_gem_object_put(obj); 590 1.1 riastrad 591 1.1 riastrad return dmabuf_fd; 592 1.1 riastrad 593 1.1 riastrad out_free_dmabuf: 594 1.1 riastrad dma_buf_put(dmabuf); 595 1.1 riastrad out_free_gem: 596 1.1 riastrad i915_gem_object_put(obj); 597 1.1 riastrad out: 598 1.1 riastrad mutex_unlock(&vgpu->dmabuf_lock); 599 1.1 riastrad return ret; 600 1.1 riastrad } 601 1.1 riastrad 602 1.1 riastrad void intel_vgpu_dmabuf_cleanup(struct intel_vgpu *vgpu) 603 1.1 riastrad { 604 1.1 riastrad struct list_head *pos, *n; 605 1.1 riastrad struct intel_vgpu_dmabuf_obj *dmabuf_obj; 606 1.1 riastrad 607 1.1 riastrad mutex_lock(&vgpu->dmabuf_lock); 608 1.1 riastrad list_for_each_safe(pos, n, &vgpu->dmabuf_obj_list_head) { 609 1.1 riastrad dmabuf_obj = container_of(pos, struct intel_vgpu_dmabuf_obj, 610 1.1 riastrad list); 611 1.1 riastrad dmabuf_obj->vgpu = NULL; 612 1.1 riastrad 613 1.1 riastrad idr_remove(&vgpu->object_idr, dmabuf_obj->dmabuf_id); 614 1.1 riastrad intel_gvt_hypervisor_put_vfio_device(vgpu); 615 1.1 riastrad list_del(pos); 616 1.1 riastrad 617 1.1 riastrad /* dmabuf_obj might be freed in dmabuf_obj_put */ 618 1.1 riastrad if (dmabuf_obj->initref) { 619 1.1 riastrad dmabuf_obj->initref = false; 620 1.1 riastrad dmabuf_obj_put(dmabuf_obj); 621 1.1 riastrad } 622 1.1 riastrad 623 1.1 riastrad } 624 1.1 riastrad mutex_unlock(&vgpu->dmabuf_lock); 625 1.1 riastrad } 626