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