xref: /openbmc/linux/drivers/gpu/drm/i915/gvt/dmabuf.c (revision 10996f80)
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 <drm/drmP.h>
33e546e281STina Zhang #include <linux/vfio.h>
34e546e281STina Zhang 
35e546e281STina Zhang #include "i915_drv.h"
36e546e281STina Zhang #include "gvt.h"
37e546e281STina Zhang 
38e546e281STina Zhang #define GEN8_DECODE_PTE(pte) (pte & GENMASK_ULL(63, 12))
39e546e281STina Zhang 
40e546e281STina Zhang static int vgpu_gem_get_pages(
41e546e281STina Zhang 		struct drm_i915_gem_object *obj)
42e546e281STina Zhang {
43e546e281STina Zhang 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
44e546e281STina Zhang 	struct sg_table *st;
45e546e281STina Zhang 	struct scatterlist *sg;
46e546e281STina Zhang 	int i, ret;
47e546e281STina Zhang 	gen8_pte_t __iomem *gtt_entries;
48e546e281STina Zhang 	struct intel_vgpu_fb_info *fb_info;
49e546e281STina Zhang 
50e546e281STina Zhang 	fb_info = (struct intel_vgpu_fb_info *)obj->gvt_info;
51e546e281STina Zhang 	if (WARN_ON(!fb_info))
52e546e281STina Zhang 		return -ENODEV;
53e546e281STina Zhang 
54e546e281STina Zhang 	st = kmalloc(sizeof(*st), GFP_KERNEL);
55e546e281STina Zhang 	if (unlikely(!st))
56e546e281STina Zhang 		return -ENOMEM;
57e546e281STina Zhang 
58e546e281STina Zhang 	ret = sg_alloc_table(st, fb_info->size, GFP_KERNEL);
59e546e281STina Zhang 	if (ret) {
60e546e281STina Zhang 		kfree(st);
61e546e281STina Zhang 		return ret;
62e546e281STina Zhang 	}
63e546e281STina Zhang 	gtt_entries = (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
64e546e281STina Zhang 		(fb_info->start >> PAGE_SHIFT);
65e546e281STina Zhang 	for_each_sg(st->sgl, sg, fb_info->size, i) {
66e546e281STina Zhang 		sg->offset = 0;
67e546e281STina Zhang 		sg->length = PAGE_SIZE;
68e546e281STina Zhang 		sg_dma_address(sg) =
69e546e281STina Zhang 			GEN8_DECODE_PTE(readq(&gtt_entries[i]));
70e546e281STina Zhang 		sg_dma_len(sg) = PAGE_SIZE;
71e546e281STina Zhang 	}
72e546e281STina Zhang 
73e546e281STina Zhang 	__i915_gem_object_set_pages(obj, st, PAGE_SIZE);
74e546e281STina Zhang 
75e546e281STina Zhang 	return 0;
76e546e281STina Zhang }
77e546e281STina Zhang 
78e546e281STina Zhang static void vgpu_gem_put_pages(struct drm_i915_gem_object *obj,
79e546e281STina Zhang 		struct sg_table *pages)
80e546e281STina Zhang {
81e546e281STina Zhang 	sg_free_table(pages);
82e546e281STina Zhang 	kfree(pages);
83e546e281STina Zhang }
84e546e281STina Zhang 
85e546e281STina Zhang static void dmabuf_gem_object_free(struct kref *kref)
86e546e281STina Zhang {
87e546e281STina Zhang 	struct intel_vgpu_dmabuf_obj *obj =
88e546e281STina Zhang 		container_of(kref, struct intel_vgpu_dmabuf_obj, kref);
89e546e281STina Zhang 	struct intel_vgpu *vgpu = obj->vgpu;
90e546e281STina Zhang 	struct list_head *pos;
91e546e281STina Zhang 	struct intel_vgpu_dmabuf_obj *dmabuf_obj;
92e546e281STina Zhang 
9382a3b670STina Zhang 	if (vgpu && vgpu->active && !list_empty(&vgpu->dmabuf_obj_list_head)) {
94e546e281STina Zhang 		list_for_each(pos, &vgpu->dmabuf_obj_list_head) {
95dfb6ae4eSTina Zhang 			dmabuf_obj = container_of(pos,
96dfb6ae4eSTina Zhang 					struct intel_vgpu_dmabuf_obj, list);
97e546e281STina Zhang 			if (dmabuf_obj == obj) {
98dfb6ae4eSTina Zhang 				intel_gvt_hypervisor_put_vfio_device(vgpu);
99dfb6ae4eSTina Zhang 				idr_remove(&vgpu->object_idr,
100dfb6ae4eSTina Zhang 					   dmabuf_obj->dmabuf_id);
101e546e281STina Zhang 				kfree(dmabuf_obj->info);
102e546e281STina Zhang 				kfree(dmabuf_obj);
103e546e281STina Zhang 				list_del(pos);
104e546e281STina Zhang 				break;
105e546e281STina Zhang 			}
106e546e281STina Zhang 		}
107dfb6ae4eSTina Zhang 	} else {
108dfb6ae4eSTina Zhang 		/* Free the orphan dmabuf_objs here */
109dfb6ae4eSTina Zhang 		kfree(obj->info);
110dfb6ae4eSTina Zhang 		kfree(obj);
111dfb6ae4eSTina Zhang 	}
112e546e281STina Zhang }
113e546e281STina Zhang 
114e546e281STina Zhang 
115e546e281STina Zhang static inline void dmabuf_obj_get(struct intel_vgpu_dmabuf_obj *obj)
116e546e281STina Zhang {
117e546e281STina Zhang 	kref_get(&obj->kref);
118e546e281STina Zhang }
119e546e281STina Zhang 
120e546e281STina Zhang static inline void dmabuf_obj_put(struct intel_vgpu_dmabuf_obj *obj)
121e546e281STina Zhang {
122e546e281STina Zhang 	kref_put(&obj->kref, dmabuf_gem_object_free);
123e546e281STina Zhang }
124e546e281STina Zhang 
125e546e281STina Zhang static void vgpu_gem_release(struct drm_i915_gem_object *gem_obj)
126e546e281STina Zhang {
127e546e281STina Zhang 
128e546e281STina Zhang 	struct intel_vgpu_fb_info *fb_info = gem_obj->gvt_info;
129e546e281STina Zhang 	struct intel_vgpu_dmabuf_obj *obj = fb_info->obj;
130e546e281STina Zhang 	struct intel_vgpu *vgpu = obj->vgpu;
131e546e281STina Zhang 
132dfb6ae4eSTina Zhang 	if (vgpu) {
133e546e281STina Zhang 		mutex_lock(&vgpu->dmabuf_lock);
134e546e281STina Zhang 		gem_obj->base.dma_buf = NULL;
135e546e281STina Zhang 		dmabuf_obj_put(obj);
136e546e281STina Zhang 		mutex_unlock(&vgpu->dmabuf_lock);
137dfb6ae4eSTina Zhang 	} else {
138dfb6ae4eSTina Zhang 		/* vgpu is NULL, as it has been removed already */
139dfb6ae4eSTina Zhang 		gem_obj->base.dma_buf = NULL;
140dfb6ae4eSTina Zhang 		dmabuf_obj_put(obj);
141dfb6ae4eSTina Zhang 	}
142e546e281STina Zhang }
143e546e281STina Zhang 
144e546e281STina Zhang static const struct drm_i915_gem_object_ops intel_vgpu_gem_ops = {
145e546e281STina Zhang 	.flags = I915_GEM_OBJECT_IS_PROXY,
146e546e281STina Zhang 	.get_pages = vgpu_gem_get_pages,
147e546e281STina Zhang 	.put_pages = vgpu_gem_put_pages,
148e546e281STina Zhang 	.release = vgpu_gem_release,
149e546e281STina Zhang };
150e546e281STina Zhang 
151e546e281STina Zhang static struct drm_i915_gem_object *vgpu_create_gem(struct drm_device *dev,
152e546e281STina Zhang 		struct intel_vgpu_fb_info *info)
153e546e281STina Zhang {
154e546e281STina Zhang 	struct drm_i915_private *dev_priv = to_i915(dev);
155e546e281STina Zhang 	struct drm_i915_gem_object *obj;
156e546e281STina Zhang 
157e546e281STina Zhang 	obj = i915_gem_object_alloc(dev_priv);
158e546e281STina Zhang 	if (obj == NULL)
159e546e281STina Zhang 		return NULL;
160e546e281STina Zhang 
161e546e281STina Zhang 	drm_gem_private_object_init(dev, &obj->base,
162e546e281STina Zhang 		info->size << PAGE_SHIFT);
163e546e281STina Zhang 	i915_gem_object_init(obj, &intel_vgpu_gem_ops);
164e546e281STina Zhang 
165c0a51fd0SChristian König 	obj->read_domains = I915_GEM_DOMAIN_GTT;
166c0a51fd0SChristian König 	obj->write_domain = 0;
1674a136d59STina Zhang 	if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
168e546e281STina Zhang 		unsigned int tiling_mode = 0;
169e546e281STina Zhang 		unsigned int stride = 0;
170e546e281STina Zhang 
171e546e281STina Zhang 		switch (info->drm_format_mod << 10) {
172e546e281STina Zhang 		case PLANE_CTL_TILED_LINEAR:
173e546e281STina Zhang 			tiling_mode = I915_TILING_NONE;
174e546e281STina Zhang 			break;
175e546e281STina Zhang 		case PLANE_CTL_TILED_X:
176e546e281STina Zhang 			tiling_mode = I915_TILING_X;
177e546e281STina Zhang 			stride = info->stride;
178e546e281STina Zhang 			break;
179e546e281STina Zhang 		case PLANE_CTL_TILED_Y:
180e546e281STina Zhang 			tiling_mode = I915_TILING_Y;
181e546e281STina Zhang 			stride = info->stride;
182e546e281STina Zhang 			break;
183e546e281STina Zhang 		default:
184e546e281STina Zhang 			gvt_dbg_core("not supported tiling mode\n");
185e546e281STina Zhang 		}
186e546e281STina Zhang 		obj->tiling_and_stride = tiling_mode | stride;
187e546e281STina Zhang 	} else {
188e546e281STina Zhang 		obj->tiling_and_stride = info->drm_format_mod ?
189e546e281STina Zhang 					I915_TILING_X : 0;
190e546e281STina Zhang 	}
191e546e281STina Zhang 
192e546e281STina Zhang 	return obj;
193e546e281STina Zhang }
194e546e281STina Zhang 
195e546e281STina Zhang static int vgpu_get_plane_info(struct drm_device *dev,
196e546e281STina Zhang 		struct intel_vgpu *vgpu,
197e546e281STina Zhang 		struct intel_vgpu_fb_info *info,
198e546e281STina Zhang 		int plane_id)
199e546e281STina Zhang {
200e546e281STina Zhang 	struct drm_i915_private *dev_priv = to_i915(dev);
201e546e281STina Zhang 	struct intel_vgpu_primary_plane_format p;
202e546e281STina Zhang 	struct intel_vgpu_cursor_plane_format c;
203e546e281STina Zhang 	int ret;
204e546e281STina Zhang 
205e546e281STina Zhang 	if (plane_id == DRM_PLANE_TYPE_PRIMARY) {
206e546e281STina Zhang 		ret = intel_vgpu_decode_primary_plane(vgpu, &p);
207e546e281STina Zhang 		if (ret)
208e546e281STina Zhang 			return ret;
209e546e281STina Zhang 		info->start = p.base;
210e546e281STina Zhang 		info->start_gpa = p.base_gpa;
211e546e281STina Zhang 		info->width = p.width;
212e546e281STina Zhang 		info->height = p.height;
213e546e281STina Zhang 		info->stride = p.stride;
214e546e281STina Zhang 		info->drm_format = p.drm_format;
215e546e281STina Zhang 		info->drm_format_mod = p.tiled;
216e546e281STina Zhang 		info->size = (((p.stride * p.height * p.bpp) / 8) +
217e546e281STina Zhang 				(PAGE_SIZE - 1)) >> PAGE_SHIFT;
218e546e281STina Zhang 	} else if (plane_id == DRM_PLANE_TYPE_CURSOR) {
219e546e281STina Zhang 		ret = intel_vgpu_decode_cursor_plane(vgpu, &c);
220e546e281STina Zhang 		if (ret)
221e546e281STina Zhang 			return ret;
222e546e281STina Zhang 		info->start = c.base;
223e546e281STina Zhang 		info->start_gpa = c.base_gpa;
224e546e281STina Zhang 		info->width = c.width;
225e546e281STina Zhang 		info->height = c.height;
226e546e281STina Zhang 		info->stride = c.width * (c.bpp / 8);
227e546e281STina Zhang 		info->drm_format = c.drm_format;
228e546e281STina Zhang 		info->drm_format_mod = 0;
229e546e281STina Zhang 		info->x_pos = c.x_pos;
230e546e281STina Zhang 		info->y_pos = c.y_pos;
231e546e281STina Zhang 
232e546e281STina Zhang 		/* The invalid cursor hotspot value is delivered to host
233e546e281STina Zhang 		 * until we find a way to get the cursor hotspot info of
234e546e281STina Zhang 		 * guest OS.
235e546e281STina Zhang 		 */
236e546e281STina Zhang 		info->x_hot = UINT_MAX;
237e546e281STina Zhang 		info->y_hot = UINT_MAX;
238e546e281STina Zhang 		info->size = (((info->stride * c.height * c.bpp) / 8)
239e546e281STina Zhang 				+ (PAGE_SIZE - 1)) >> PAGE_SHIFT;
240e546e281STina Zhang 	} else {
241e546e281STina Zhang 		gvt_vgpu_err("invalid plane id:%d\n", plane_id);
242e546e281STina Zhang 		return -EINVAL;
243e546e281STina Zhang 	}
244e546e281STina Zhang 
245e546e281STina Zhang 	if (info->size == 0) {
246e546e281STina Zhang 		gvt_vgpu_err("fb size is zero\n");
247e546e281STina Zhang 		return -EINVAL;
248e546e281STina Zhang 	}
249e546e281STina Zhang 
250e546e281STina Zhang 	if (info->start & (PAGE_SIZE - 1)) {
251e546e281STina Zhang 		gvt_vgpu_err("Not aligned fb address:0x%llx\n", info->start);
252e546e281STina Zhang 		return -EFAULT;
253e546e281STina Zhang 	}
254e546e281STina Zhang 	if (((info->start >> PAGE_SHIFT) + info->size) >
255e546e281STina Zhang 		ggtt_total_entries(&dev_priv->ggtt)) {
256e546e281STina Zhang 		gvt_vgpu_err("Invalid GTT offset or size\n");
257e546e281STina Zhang 		return -EFAULT;
258e546e281STina Zhang 	}
259e546e281STina Zhang 
260e546e281STina Zhang 	if (!intel_gvt_ggtt_validate_range(vgpu, info->start, info->size)) {
261e546e281STina Zhang 		gvt_vgpu_err("invalid gma addr\n");
262e546e281STina Zhang 		return -EFAULT;
263e546e281STina Zhang 	}
264e546e281STina Zhang 
265e546e281STina Zhang 	return 0;
266e546e281STina Zhang }
267e546e281STina Zhang 
268e546e281STina Zhang static struct intel_vgpu_dmabuf_obj *
269e546e281STina Zhang pick_dmabuf_by_info(struct intel_vgpu *vgpu,
270e546e281STina Zhang 		    struct intel_vgpu_fb_info *latest_info)
271e546e281STina Zhang {
272e546e281STina Zhang 	struct list_head *pos;
273e546e281STina Zhang 	struct intel_vgpu_fb_info *fb_info;
274e546e281STina Zhang 	struct intel_vgpu_dmabuf_obj *dmabuf_obj = NULL;
275e546e281STina Zhang 	struct intel_vgpu_dmabuf_obj *ret = NULL;
276e546e281STina Zhang 
277e546e281STina Zhang 	list_for_each(pos, &vgpu->dmabuf_obj_list_head) {
278e546e281STina Zhang 		dmabuf_obj = container_of(pos, struct intel_vgpu_dmabuf_obj,
279e546e281STina Zhang 						list);
280e546e281STina Zhang 		if ((dmabuf_obj == NULL) ||
281e546e281STina Zhang 		    (dmabuf_obj->info == NULL))
282e546e281STina Zhang 			continue;
283e546e281STina Zhang 
284e546e281STina Zhang 		fb_info = (struct intel_vgpu_fb_info *)dmabuf_obj->info;
285e546e281STina Zhang 		if ((fb_info->start == latest_info->start) &&
286e546e281STina Zhang 		    (fb_info->start_gpa == latest_info->start_gpa) &&
287e546e281STina Zhang 		    (fb_info->size == latest_info->size) &&
288e546e281STina Zhang 		    (fb_info->drm_format_mod == latest_info->drm_format_mod) &&
289e546e281STina Zhang 		    (fb_info->drm_format == latest_info->drm_format) &&
290e546e281STina Zhang 		    (fb_info->width == latest_info->width) &&
291e546e281STina Zhang 		    (fb_info->height == latest_info->height)) {
292e546e281STina Zhang 			ret = dmabuf_obj;
293e546e281STina Zhang 			break;
294e546e281STina Zhang 		}
295e546e281STina Zhang 	}
296e546e281STina Zhang 
297e546e281STina Zhang 	return ret;
298e546e281STina Zhang }
299e546e281STina Zhang 
300e546e281STina Zhang static struct intel_vgpu_dmabuf_obj *
301e546e281STina Zhang pick_dmabuf_by_num(struct intel_vgpu *vgpu, u32 id)
302e546e281STina Zhang {
303e546e281STina Zhang 	struct list_head *pos;
304e546e281STina Zhang 	struct intel_vgpu_dmabuf_obj *dmabuf_obj = NULL;
305e546e281STina Zhang 	struct intel_vgpu_dmabuf_obj *ret = NULL;
306e546e281STina Zhang 
307e546e281STina Zhang 	list_for_each(pos, &vgpu->dmabuf_obj_list_head) {
308e546e281STina Zhang 		dmabuf_obj = container_of(pos, struct intel_vgpu_dmabuf_obj,
309e546e281STina Zhang 						list);
310e546e281STina Zhang 		if (!dmabuf_obj)
311e546e281STina Zhang 			continue;
312e546e281STina Zhang 
313e546e281STina Zhang 		if (dmabuf_obj->dmabuf_id == id) {
314e546e281STina Zhang 			ret = dmabuf_obj;
315e546e281STina Zhang 			break;
316e546e281STina Zhang 		}
317e546e281STina Zhang 	}
318e546e281STina Zhang 
319e546e281STina Zhang 	return ret;
320e546e281STina Zhang }
321e546e281STina Zhang 
322e546e281STina Zhang static void update_fb_info(struct vfio_device_gfx_plane_info *gvt_dmabuf,
323e546e281STina Zhang 		      struct intel_vgpu_fb_info *fb_info)
324e546e281STina Zhang {
325e546e281STina Zhang 	gvt_dmabuf->drm_format = fb_info->drm_format;
32610996f80STina Zhang 	gvt_dmabuf->drm_format_mod = fb_info->drm_format_mod;
327e546e281STina Zhang 	gvt_dmabuf->width = fb_info->width;
328e546e281STina Zhang 	gvt_dmabuf->height = fb_info->height;
329e546e281STina Zhang 	gvt_dmabuf->stride = fb_info->stride;
330e546e281STina Zhang 	gvt_dmabuf->size = fb_info->size;
331e546e281STina Zhang 	gvt_dmabuf->x_pos = fb_info->x_pos;
332e546e281STina Zhang 	gvt_dmabuf->y_pos = fb_info->y_pos;
333e546e281STina Zhang 	gvt_dmabuf->x_hot = fb_info->x_hot;
334e546e281STina Zhang 	gvt_dmabuf->y_hot = fb_info->y_hot;
335e546e281STina Zhang }
336e546e281STina Zhang 
337e546e281STina Zhang int intel_vgpu_query_plane(struct intel_vgpu *vgpu, void *args)
338e546e281STina Zhang {
339e546e281STina Zhang 	struct drm_device *dev = &vgpu->gvt->dev_priv->drm;
340e546e281STina Zhang 	struct vfio_device_gfx_plane_info *gfx_plane_info = args;
341e546e281STina Zhang 	struct intel_vgpu_dmabuf_obj *dmabuf_obj;
342e546e281STina Zhang 	struct intel_vgpu_fb_info fb_info;
343e546e281STina Zhang 	int ret = 0;
344e546e281STina Zhang 
345e546e281STina Zhang 	if (gfx_plane_info->flags == (VFIO_GFX_PLANE_TYPE_DMABUF |
346e546e281STina Zhang 				       VFIO_GFX_PLANE_TYPE_PROBE))
347e546e281STina Zhang 		return ret;
348e546e281STina Zhang 	else if ((gfx_plane_info->flags & ~VFIO_GFX_PLANE_TYPE_DMABUF) ||
349e546e281STina Zhang 			(!gfx_plane_info->flags))
350e546e281STina Zhang 		return -EINVAL;
351e546e281STina Zhang 
352e546e281STina Zhang 	ret = vgpu_get_plane_info(dev, vgpu, &fb_info,
353e546e281STina Zhang 					gfx_plane_info->drm_plane_type);
354e546e281STina Zhang 	if (ret != 0)
355e546e281STina Zhang 		goto out;
356e546e281STina Zhang 
357e546e281STina Zhang 	mutex_lock(&vgpu->dmabuf_lock);
358e546e281STina Zhang 	/* If exists, pick up the exposed dmabuf_obj */
359e546e281STina Zhang 	dmabuf_obj = pick_dmabuf_by_info(vgpu, &fb_info);
360e546e281STina Zhang 	if (dmabuf_obj) {
361e546e281STina Zhang 		update_fb_info(gfx_plane_info, &fb_info);
362e546e281STina Zhang 		gfx_plane_info->dmabuf_id = dmabuf_obj->dmabuf_id;
363e546e281STina Zhang 
364e546e281STina Zhang 		/* This buffer may be released between query_plane ioctl and
365e546e281STina Zhang 		 * get_dmabuf ioctl. Add the refcount to make sure it won't
366e546e281STina Zhang 		 * be released between the two ioctls.
367e546e281STina Zhang 		 */
368e546e281STina Zhang 		if (!dmabuf_obj->initref) {
369e546e281STina Zhang 			dmabuf_obj->initref = true;
370e546e281STina Zhang 			dmabuf_obj_get(dmabuf_obj);
371e546e281STina Zhang 		}
372e546e281STina Zhang 		ret = 0;
373e546e281STina Zhang 		gvt_dbg_dpy("vgpu%d: re-use dmabuf_obj ref %d, id %d\n",
374e546e281STina Zhang 			    vgpu->id, kref_read(&dmabuf_obj->kref),
375e546e281STina Zhang 			    gfx_plane_info->dmabuf_id);
376e546e281STina Zhang 		mutex_unlock(&vgpu->dmabuf_lock);
377e546e281STina Zhang 		goto out;
378e546e281STina Zhang 	}
379e546e281STina Zhang 
380e546e281STina Zhang 	mutex_unlock(&vgpu->dmabuf_lock);
381e546e281STina Zhang 
382e546e281STina Zhang 	/* Need to allocate a new one*/
383e546e281STina Zhang 	dmabuf_obj = kmalloc(sizeof(struct intel_vgpu_dmabuf_obj), GFP_KERNEL);
384e546e281STina Zhang 	if (unlikely(!dmabuf_obj)) {
385e546e281STina Zhang 		gvt_vgpu_err("alloc dmabuf_obj failed\n");
386e546e281STina Zhang 		ret = -ENOMEM;
387e546e281STina Zhang 		goto out;
388e546e281STina Zhang 	}
389e546e281STina Zhang 
390e546e281STina Zhang 	dmabuf_obj->info = kmalloc(sizeof(struct intel_vgpu_fb_info),
391e546e281STina Zhang 				   GFP_KERNEL);
392e546e281STina Zhang 	if (unlikely(!dmabuf_obj->info)) {
393e546e281STina Zhang 		gvt_vgpu_err("allocate intel vgpu fb info failed\n");
394e546e281STina Zhang 		ret = -ENOMEM;
395e546e281STina Zhang 		goto out_free_dmabuf;
396e546e281STina Zhang 	}
397e546e281STina Zhang 	memcpy(dmabuf_obj->info, &fb_info, sizeof(struct intel_vgpu_fb_info));
398e546e281STina Zhang 
399e546e281STina Zhang 	((struct intel_vgpu_fb_info *)dmabuf_obj->info)->obj = dmabuf_obj;
400e546e281STina Zhang 
401e546e281STina Zhang 	dmabuf_obj->vgpu = vgpu;
402e546e281STina Zhang 
403e546e281STina Zhang 	ret = idr_alloc(&vgpu->object_idr, dmabuf_obj, 1, 0, GFP_NOWAIT);
404e546e281STina Zhang 	if (ret < 0)
405e546e281STina Zhang 		goto out_free_info;
406e546e281STina Zhang 	gfx_plane_info->dmabuf_id = ret;
407e546e281STina Zhang 	dmabuf_obj->dmabuf_id = ret;
408e546e281STina Zhang 
409e546e281STina Zhang 	dmabuf_obj->initref = true;
410e546e281STina Zhang 
411e546e281STina Zhang 	kref_init(&dmabuf_obj->kref);
412e546e281STina Zhang 
413e546e281STina Zhang 	mutex_lock(&vgpu->dmabuf_lock);
414e546e281STina Zhang 	if (intel_gvt_hypervisor_get_vfio_device(vgpu)) {
415e546e281STina Zhang 		gvt_vgpu_err("get vfio device failed\n");
416e546e281STina Zhang 		mutex_unlock(&vgpu->dmabuf_lock);
417e546e281STina Zhang 		goto out_free_info;
418e546e281STina Zhang 	}
419e546e281STina Zhang 	mutex_unlock(&vgpu->dmabuf_lock);
420e546e281STina Zhang 
421e546e281STina Zhang 	update_fb_info(gfx_plane_info, &fb_info);
422e546e281STina Zhang 
423e546e281STina Zhang 	INIT_LIST_HEAD(&dmabuf_obj->list);
424e546e281STina Zhang 	mutex_lock(&vgpu->dmabuf_lock);
425e546e281STina Zhang 	list_add_tail(&dmabuf_obj->list, &vgpu->dmabuf_obj_list_head);
426e546e281STina Zhang 	mutex_unlock(&vgpu->dmabuf_lock);
427e546e281STina Zhang 
428e546e281STina Zhang 	gvt_dbg_dpy("vgpu%d: %s new dmabuf_obj ref %d, id %d\n", vgpu->id,
429e546e281STina Zhang 		    __func__, kref_read(&dmabuf_obj->kref), ret);
430e546e281STina Zhang 
431e546e281STina Zhang 	return 0;
432e546e281STina Zhang 
433e546e281STina Zhang out_free_info:
434e546e281STina Zhang 	kfree(dmabuf_obj->info);
435e546e281STina Zhang out_free_dmabuf:
436e546e281STina Zhang 	kfree(dmabuf_obj);
437e546e281STina Zhang out:
438e546e281STina Zhang 	/* ENODEV means plane isn't ready, which might be a normal case. */
439e546e281STina Zhang 	return (ret == -ENODEV) ? 0 : ret;
440e546e281STina Zhang }
441e546e281STina Zhang 
442e546e281STina Zhang /* To associate an exposed dmabuf with the dmabuf_obj */
443e546e281STina Zhang int intel_vgpu_get_dmabuf(struct intel_vgpu *vgpu, unsigned int dmabuf_id)
444e546e281STina Zhang {
445e546e281STina Zhang 	struct drm_device *dev = &vgpu->gvt->dev_priv->drm;
446e546e281STina Zhang 	struct intel_vgpu_dmabuf_obj *dmabuf_obj;
447e546e281STina Zhang 	struct drm_i915_gem_object *obj;
448e546e281STina Zhang 	struct dma_buf *dmabuf;
449e546e281STina Zhang 	int dmabuf_fd;
450e546e281STina Zhang 	int ret = 0;
451e546e281STina Zhang 
452e546e281STina Zhang 	mutex_lock(&vgpu->dmabuf_lock);
453e546e281STina Zhang 
454e546e281STina Zhang 	dmabuf_obj = pick_dmabuf_by_num(vgpu, dmabuf_id);
455e546e281STina Zhang 	if (dmabuf_obj == NULL) {
456e546e281STina Zhang 		gvt_vgpu_err("invalid dmabuf id:%d\n", dmabuf_id);
457e546e281STina Zhang 		ret = -EINVAL;
458e546e281STina Zhang 		goto out;
459e546e281STina Zhang 	}
460e546e281STina Zhang 
461e546e281STina Zhang 	obj = vgpu_create_gem(dev, dmabuf_obj->info);
462e546e281STina Zhang 	if (obj == NULL) {
4637e534ac9SZhenyu Wang 		gvt_vgpu_err("create gvt gem obj failed\n");
464e546e281STina Zhang 		ret = -ENOMEM;
465e546e281STina Zhang 		goto out;
466e546e281STina Zhang 	}
467e546e281STina Zhang 
468e546e281STina Zhang 	obj->gvt_info = dmabuf_obj->info;
469e546e281STina Zhang 
470e546e281STina Zhang 	dmabuf = i915_gem_prime_export(dev, &obj->base, DRM_CLOEXEC | DRM_RDWR);
471e546e281STina Zhang 	if (IS_ERR(dmabuf)) {
472e546e281STina Zhang 		gvt_vgpu_err("export dma-buf failed\n");
473e546e281STina Zhang 		ret = PTR_ERR(dmabuf);
474e546e281STina Zhang 		goto out_free_gem;
475e546e281STina Zhang 	}
476e546e281STina Zhang 
477e546e281STina Zhang 	i915_gem_object_put(obj);
478e546e281STina Zhang 
479e546e281STina Zhang 	ret = dma_buf_fd(dmabuf, DRM_CLOEXEC | DRM_RDWR);
480e546e281STina Zhang 	if (ret < 0) {
481e546e281STina Zhang 		gvt_vgpu_err("create dma-buf fd failed ret:%d\n", ret);
482e546e281STina Zhang 		goto out_free_dmabuf;
483e546e281STina Zhang 	}
484e546e281STina Zhang 	dmabuf_fd = ret;
485e546e281STina Zhang 
486e546e281STina Zhang 	dmabuf_obj_get(dmabuf_obj);
487e546e281STina Zhang 
488e546e281STina Zhang 	if (dmabuf_obj->initref) {
489e546e281STina Zhang 		dmabuf_obj->initref = false;
490e546e281STina Zhang 		dmabuf_obj_put(dmabuf_obj);
491e546e281STina Zhang 	}
492e546e281STina Zhang 
493e546e281STina Zhang 	mutex_unlock(&vgpu->dmabuf_lock);
494e546e281STina Zhang 
495e546e281STina Zhang 	gvt_dbg_dpy("vgpu%d: dmabuf:%d, dmabuf ref %d, fd:%d\n"
496e546e281STina Zhang 		    "        file count: %ld, GEM ref: %d\n",
497e546e281STina Zhang 		    vgpu->id, dmabuf_obj->dmabuf_id,
498e546e281STina Zhang 		    kref_read(&dmabuf_obj->kref),
499e546e281STina Zhang 		    dmabuf_fd,
500e546e281STina Zhang 		    file_count(dmabuf->file),
501e546e281STina Zhang 		    kref_read(&obj->base.refcount));
502e546e281STina Zhang 
503e546e281STina Zhang 	return dmabuf_fd;
504e546e281STina Zhang 
505e546e281STina Zhang out_free_dmabuf:
506e546e281STina Zhang 	dma_buf_put(dmabuf);
507e546e281STina Zhang out_free_gem:
508e546e281STina Zhang 	i915_gem_object_put(obj);
509e546e281STina Zhang out:
510e546e281STina Zhang 	mutex_unlock(&vgpu->dmabuf_lock);
511e546e281STina Zhang 	return ret;
512e546e281STina Zhang }
513e546e281STina Zhang 
514e546e281STina Zhang void intel_vgpu_dmabuf_cleanup(struct intel_vgpu *vgpu)
515e546e281STina Zhang {
516e546e281STina Zhang 	struct list_head *pos, *n;
517e546e281STina Zhang 	struct intel_vgpu_dmabuf_obj *dmabuf_obj;
518e546e281STina Zhang 
519e546e281STina Zhang 	mutex_lock(&vgpu->dmabuf_lock);
520e546e281STina Zhang 	list_for_each_safe(pos, n, &vgpu->dmabuf_obj_list_head) {
521e546e281STina Zhang 		dmabuf_obj = container_of(pos, struct intel_vgpu_dmabuf_obj,
522e546e281STina Zhang 						list);
5236ee942d5STina Zhang 		dmabuf_obj->vgpu = NULL;
5246ee942d5STina Zhang 
5256ee942d5STina Zhang 		idr_remove(&vgpu->object_idr, dmabuf_obj->dmabuf_id);
5266ee942d5STina Zhang 		intel_gvt_hypervisor_put_vfio_device(vgpu);
5276ee942d5STina Zhang 		list_del(pos);
5286ee942d5STina Zhang 
5296ee942d5STina Zhang 		/* dmabuf_obj might be freed in dmabuf_obj_put */
530e546e281STina Zhang 		if (dmabuf_obj->initref) {
531e546e281STina Zhang 			dmabuf_obj->initref = false;
532e546e281STina Zhang 			dmabuf_obj_put(dmabuf_obj);
533e546e281STina Zhang 		}
534dfb6ae4eSTina Zhang 
535e546e281STina Zhang 	}
536e546e281STina Zhang 	mutex_unlock(&vgpu->dmabuf_lock);
537e546e281STina Zhang }
538