i915_gem_dmabuf.c 6.8 KB
Newer Older
1
/*
2
 * SPDX-License-Identifier: MIT
3
 *
4
 * Copyright 2012 Red Hat Inc
5
 */
6 7

#include <linux/dma-buf.h>
8
#include <linux/highmem.h>
9
#include <linux/dma-resv.h>
10

11
#include "i915_drv.h"
12
#include "i915_gem_object.h"
13
#include "i915_scatterlist.h"
14

15 16 17 18 19
static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf)
{
	return to_intel_bo(buf->priv);
}

20
static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
21
					     enum dma_data_direction dir)
22
{
23
	struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
24 25 26
	struct sg_table *st;
	struct scatterlist *src, *dst;
	int ret, i;
27

C
Chris Wilson 已提交
28
	ret = i915_gem_object_pin_pages(obj);
29
	if (ret)
30
		goto err;
31

32 33 34
	/* Copy sg so that we make an independent mapping */
	st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
	if (st == NULL) {
35
		ret = -ENOMEM;
36
		goto err_unpin_pages;
37 38
	}

C
Chris Wilson 已提交
39
	ret = sg_alloc_table(st, obj->mm.pages->nents, GFP_KERNEL);
40 41
	if (ret)
		goto err_free;
42

C
Chris Wilson 已提交
43
	src = obj->mm.pages->sgl;
44
	dst = st->sgl;
C
Chris Wilson 已提交
45
	for (i = 0; i < obj->mm.pages->nents; i++) {
46
		sg_set_page(dst, sg_page(src), src->length, 0);
47 48 49 50
		dst = sg_next(dst);
		src = sg_next(src);
	}

51 52 53
	if (!dma_map_sg_attrs(attachment->dev,
			      st->sgl, st->nents, dir,
			      DMA_ATTR_SKIP_CPU_SYNC)) {
54
		ret = -ENOMEM;
55
		goto err_free_sg;
56 57
	}

58
	return st;
59 60 61 62 63

err_free_sg:
	sg_free_table(st);
err_free:
	kfree(st);
64
err_unpin_pages:
65 66 67
	i915_gem_object_unpin_pages(obj);
err:
	return ERR_PTR(ret);
68 69
}

70
static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
71 72
				   struct sg_table *sg,
				   enum dma_data_direction dir)
73
{
74
	struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
75

76 77 78
	dma_unmap_sg_attrs(attachment->dev,
			   sg->sgl, sg->nents, dir,
			   DMA_ATTR_SKIP_CPU_SYNC);
79 80
	sg_free_table(sg);
	kfree(sg);
81 82

	i915_gem_object_unpin_pages(obj);
83 84
}

85 86
static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
{
87
	struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
88

89
	return i915_gem_object_pin_map(obj, I915_MAP_WB);
90 91 92 93
}

static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
{
94
	struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
95

96
	i915_gem_object_flush_map(obj);
97
	i915_gem_object_unpin_map(obj);
98 99
}

100 101
static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
{
102 103 104 105 106 107 108 109 110
	struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
	int ret;

	if (obj->base.size < vma->vm_end - vma->vm_start)
		return -EINVAL;

	if (!obj->base.filp)
		return -ENODEV;

111
	ret = call_mmap(obj->base.filp, vma);
112 113 114 115 116 117 118
	if (ret)
		return ret;

	fput(vma->vm_file);
	vma->vm_file = get_file(obj->base.filp);

	return 0;
119 120
}

121
static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
122
{
123
	struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
124
	bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
125
	int err;
126

127 128 129 130
	err = i915_gem_object_pin_pages(obj);
	if (err)
		return err;

131
	err = i915_gem_object_lock_interruptible(obj, NULL);
132 133
	if (err)
		goto out;
134

135
	err = i915_gem_object_set_to_cpu_domain(obj, write);
136
	i915_gem_object_unlock(obj);
137 138 139 140

out:
	i915_gem_object_unpin_pages(obj);
	return err;
141 142
}

143
static int i915_gem_end_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
144 145
{
	struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
146
	int err;
147

148 149 150 151
	err = i915_gem_object_pin_pages(obj);
	if (err)
		return err;

152
	err = i915_gem_object_lock_interruptible(obj, NULL);
153 154
	if (err)
		goto out;
155

156
	err = i915_gem_object_set_to_gtt_domain(obj, false);
157
	i915_gem_object_unlock(obj);
158

159 160 161
out:
	i915_gem_object_unpin_pages(obj);
	return err;
162 163
}

164
static const struct dma_buf_ops i915_dmabuf_ops =  {
165 166
	.map_dma_buf = i915_gem_map_dma_buf,
	.unmap_dma_buf = i915_gem_unmap_dma_buf,
167
	.release = drm_gem_dmabuf_release,
168
	.mmap = i915_gem_dmabuf_mmap,
169 170
	.vmap = i915_gem_dmabuf_vmap,
	.vunmap = i915_gem_dmabuf_vunmap,
171
	.begin_cpu_access = i915_gem_begin_cpu_access,
172
	.end_cpu_access = i915_gem_end_cpu_access,
173 174
};

D
Daniel Vetter 已提交
175
struct dma_buf *i915_gem_prime_export(struct drm_gem_object *gem_obj, int flags)
176
{
177
	struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
178 179 180 181 182 183
	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);

	exp_info.ops = &i915_dmabuf_ops;
	exp_info.size = gem_obj->size;
	exp_info.flags = flags;
	exp_info.priv = gem_obj;
C
Chris Wilson 已提交
184
	exp_info.resv = obj->base.resv;
185

186 187 188 189 190 191
	if (obj->ops->dmabuf_export) {
		int ret = obj->ops->dmabuf_export(obj);
		if (ret)
			return ERR_PTR(ret);
	}

D
Daniel Vetter 已提交
192
	return drm_gem_dmabuf_export(gem_obj->dev, &exp_info);
193 194
}

195
static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
196
{
197
	struct sg_table *pages;
M
Matthew Auld 已提交
198
	unsigned int sg_page_sizes;
199 200 201 202 203 204

	pages = dma_buf_map_attachment(obj->base.import_attach,
				       DMA_BIDIRECTIONAL);
	if (IS_ERR(pages))
		return PTR_ERR(pages);

M
Matthew Auld 已提交
205
	sg_page_sizes = i915_sg_page_sizes(pages->sgl);
206

M
Matthew Auld 已提交
207
	__i915_gem_object_set_pages(obj, pages, sg_page_sizes);
208 209

	return 0;
210 211
}

212 213
static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj,
					     struct sg_table *pages)
214
{
215 216
	dma_buf_unmap_attachment(obj->base.import_attach, pages,
				 DMA_BIDIRECTIONAL);
217 218 219
}

static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
220
	.name = "i915_gem_object_dmabuf",
221 222 223 224
	.get_pages = i915_gem_object_get_pages_dmabuf,
	.put_pages = i915_gem_object_put_pages_dmabuf,
};

225
struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
226
					     struct dma_buf *dma_buf)
227
{
228
	static struct lock_class_key lock_class;
229 230 231 232 233 234
	struct dma_buf_attachment *attach;
	struct drm_i915_gem_object *obj;
	int ret;

	/* is this one of own objects? */
	if (dma_buf->ops == &i915_dmabuf_ops) {
235
		obj = dma_buf_to_obj(dma_buf);
236 237
		/* is it from our device? */
		if (obj->base.dev == dev) {
238 239 240 241
			/*
			 * Importing dmabuf exported from out own gem increases
			 * refcount on gem itself instead of f_count of dmabuf.
			 */
242
			return &i915_gem_object_get(obj)->base;
243 244 245 246 247 248 249 250
		}
	}

	/* need to attach */
	attach = dma_buf_attach(dma_buf, dev->dev);
	if (IS_ERR(attach))
		return ERR_CAST(attach);

251 252
	get_dma_buf(dma_buf);

253
	obj = i915_gem_object_alloc();
254 255
	if (obj == NULL) {
		ret = -ENOMEM;
256
		goto fail_detach;
257 258
	}

259
	drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
260
	i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops, &lock_class);
261
	obj->base.import_attach = attach;
C
Chris Wilson 已提交
262
	obj->base.resv = dma_buf->resv;
263

264 265 266 267 268 269 270
	/* We use GTT as shorthand for a coherent domain, one that is
	 * neither in the GPU cache nor in the CPU cache, where all
	 * writes are immediately visible in memory. (That's not strictly
	 * true, but it's close! There are internal buffers such as the
	 * write-combined buffer or a delay through the chipset for GTT
	 * writes that do require us to treat GTT as a separate cache domain.)
	 */
271 272
	obj->read_domains = I915_GEM_DOMAIN_GTT;
	obj->write_domain = 0;
273

274 275 276 277
	return &obj->base;

fail_detach:
	dma_buf_detach(dma_buf, attach);
278 279
	dma_buf_put(dma_buf);

280 281
	return ERR_PTR(ret);
}
282 283 284 285 286

#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
#include "selftests/mock_dmabuf.c"
#include "selftests/i915_gem_dmabuf.c"
#endif