2 * SPDX-License-Identifier: MIT
4 * Copyright 2012 Red Hat Inc
7 #include <linux/dma-buf.h>
8 #include <linux/highmem.h>
9 #include <linux/dma-resv.h>
10 #include <linux/module.h>
15 #include "i915_gem_object.h"
16 #include "i915_scatterlist.h"
18 MODULE_IMPORT_NS(DMA_BUF);
20 I915_SELFTEST_DECLARE(static bool force_different_devices;)
22 static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf)
24 return to_intel_bo(buf->priv);
27 static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
28 enum dma_data_direction dir)
30 struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
32 struct scatterlist *src, *dst;
35 /* Copy sg so that we make an independent mapping */
36 st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
42 ret = sg_alloc_table(st, obj->mm.pages->nents, GFP_KERNEL);
46 src = obj->mm.pages->sgl;
48 for (i = 0; i < obj->mm.pages->nents; i++) {
49 sg_set_page(dst, sg_page(src), src->length, 0);
54 ret = dma_map_sgtable(attachment->dev, st, dir, DMA_ATTR_SKIP_CPU_SYNC);
68 static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
70 enum dma_data_direction dir)
72 dma_unmap_sgtable(attachment->dev, sg, dir, DMA_ATTR_SKIP_CPU_SYNC);
77 static int i915_gem_dmabuf_vmap(struct dma_buf *dma_buf, struct dma_buf_map *map)
79 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
82 vaddr = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WB);
84 return PTR_ERR(vaddr);
86 dma_buf_map_set_vaddr(map, vaddr);
91 static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, struct dma_buf_map *map)
93 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
95 i915_gem_object_flush_map(obj);
96 i915_gem_object_unpin_map(obj);
99 static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
101 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
104 if (obj->base.size < vma->vm_end - vma->vm_start)
110 ret = call_mmap(obj->base.filp, vma);
114 vma_set_file(vma, obj->base.filp);
119 static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
121 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
122 bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
123 struct i915_gem_ww_ctx ww;
126 i915_gem_ww_ctx_init(&ww, true);
128 err = i915_gem_object_lock(obj, &ww);
130 err = i915_gem_object_pin_pages(obj);
132 err = i915_gem_object_set_to_cpu_domain(obj, write);
133 i915_gem_object_unpin_pages(obj);
135 if (err == -EDEADLK) {
136 err = i915_gem_ww_ctx_backoff(&ww);
140 i915_gem_ww_ctx_fini(&ww);
144 static int i915_gem_end_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
146 struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
147 struct i915_gem_ww_ctx ww;
150 i915_gem_ww_ctx_init(&ww, true);
152 err = i915_gem_object_lock(obj, &ww);
154 err = i915_gem_object_pin_pages(obj);
156 err = i915_gem_object_set_to_gtt_domain(obj, false);
157 i915_gem_object_unpin_pages(obj);
159 if (err == -EDEADLK) {
160 err = i915_gem_ww_ctx_backoff(&ww);
164 i915_gem_ww_ctx_fini(&ww);
168 static int i915_gem_dmabuf_attach(struct dma_buf *dmabuf,
169 struct dma_buf_attachment *attach)
171 struct drm_i915_gem_object *obj = dma_buf_to_obj(dmabuf);
172 struct i915_gem_ww_ctx ww;
175 if (!i915_gem_object_can_migrate(obj, INTEL_REGION_SMEM))
178 for_i915_gem_ww(&ww, err, true) {
179 err = i915_gem_object_lock(obj, &ww);
183 err = i915_gem_object_migrate(obj, &ww, INTEL_REGION_SMEM);
187 err = i915_gem_object_wait_migration(obj, 0);
191 err = i915_gem_object_pin_pages(obj);
197 static void i915_gem_dmabuf_detach(struct dma_buf *dmabuf,
198 struct dma_buf_attachment *attach)
200 struct drm_i915_gem_object *obj = dma_buf_to_obj(dmabuf);
202 i915_gem_object_unpin_pages(obj);
205 static const struct dma_buf_ops i915_dmabuf_ops = {
206 .attach = i915_gem_dmabuf_attach,
207 .detach = i915_gem_dmabuf_detach,
208 .map_dma_buf = i915_gem_map_dma_buf,
209 .unmap_dma_buf = i915_gem_unmap_dma_buf,
210 .release = drm_gem_dmabuf_release,
211 .mmap = i915_gem_dmabuf_mmap,
212 .vmap = i915_gem_dmabuf_vmap,
213 .vunmap = i915_gem_dmabuf_vunmap,
214 .begin_cpu_access = i915_gem_begin_cpu_access,
215 .end_cpu_access = i915_gem_end_cpu_access,
218 struct dma_buf *i915_gem_prime_export(struct drm_gem_object *gem_obj, int flags)
220 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
221 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
223 exp_info.ops = &i915_dmabuf_ops;
224 exp_info.size = gem_obj->size;
225 exp_info.flags = flags;
226 exp_info.priv = gem_obj;
227 exp_info.resv = obj->base.resv;
229 if (obj->ops->dmabuf_export) {
230 int ret = obj->ops->dmabuf_export(obj);
235 return drm_gem_dmabuf_export(gem_obj->dev, &exp_info);
238 static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
240 struct drm_i915_private *i915 = to_i915(obj->base.dev);
241 struct sg_table *pages;
242 unsigned int sg_page_sizes;
244 assert_object_held(obj);
246 pages = dma_buf_map_attachment(obj->base.import_attach,
249 return PTR_ERR(pages);
251 /* XXX: consider doing a vmap flush or something */
252 if (!HAS_LLC(i915) || i915_gem_object_can_bypass_llc(obj))
253 wbinvd_on_all_cpus();
255 sg_page_sizes = i915_sg_dma_sizes(pages->sgl);
256 __i915_gem_object_set_pages(obj, pages, sg_page_sizes);
261 static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj,
262 struct sg_table *pages)
264 dma_buf_unmap_attachment(obj->base.import_attach, pages,
268 static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
269 .name = "i915_gem_object_dmabuf",
270 .get_pages = i915_gem_object_get_pages_dmabuf,
271 .put_pages = i915_gem_object_put_pages_dmabuf,
274 struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
275 struct dma_buf *dma_buf)
277 static struct lock_class_key lock_class;
278 struct dma_buf_attachment *attach;
279 struct drm_i915_gem_object *obj;
282 /* is this one of own objects? */
283 if (dma_buf->ops == &i915_dmabuf_ops) {
284 obj = dma_buf_to_obj(dma_buf);
285 /* is it from our device? */
286 if (obj->base.dev == dev &&
287 !I915_SELFTEST_ONLY(force_different_devices)) {
289 * Importing dmabuf exported from out own gem increases
290 * refcount on gem itself instead of f_count of dmabuf.
292 return &i915_gem_object_get(obj)->base;
296 if (i915_gem_object_size_2big(dma_buf->size))
297 return ERR_PTR(-E2BIG);
300 attach = dma_buf_attach(dma_buf, dev->dev);
302 return ERR_CAST(attach);
304 get_dma_buf(dma_buf);
306 obj = i915_gem_object_alloc();
312 drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
313 i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops, &lock_class,
315 obj->base.import_attach = attach;
316 obj->base.resv = dma_buf->resv;
318 /* We use GTT as shorthand for a coherent domain, one that is
319 * neither in the GPU cache nor in the CPU cache, where all
320 * writes are immediately visible in memory. (That's not strictly
321 * true, but it's close! There are internal buffers such as the
322 * write-combined buffer or a delay through the chipset for GTT
323 * writes that do require us to treat GTT as a separate cache domain.)
325 obj->read_domains = I915_GEM_DOMAIN_GTT;
326 obj->write_domain = 0;
331 dma_buf_detach(dma_buf, attach);
332 dma_buf_put(dma_buf);
337 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
338 #include "selftests/mock_dmabuf.c"
339 #include "selftests/i915_gem_dmabuf.c"