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