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