5 #include "nouveau_drv.h"
6 #include "nouveau_drm.h"
7 #include "nouveau_dma.h"
9 #include <linux/dma-buf.h>
11 static struct sg_table *nouveau_gem_map_dma_buf(struct dma_buf_attachment *attachment,
12 enum dma_data_direction dir)
14 struct nouveau_bo *nvbo = attachment->dmabuf->priv;
15 struct drm_device *dev = nvbo->gem->dev;
16 int npages = nvbo->bo.num_pages;
20 mutex_lock(&dev->struct_mutex);
21 sg = drm_prime_pages_to_sg(nvbo->bo.ttm->pages, npages);
22 nents = dma_map_sg(attachment->dev, sg->sgl, sg->nents, dir);
23 mutex_unlock(&dev->struct_mutex);
27 static void nouveau_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
28 struct sg_table *sg, enum dma_data_direction dir)
30 dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
35 static void nouveau_gem_dmabuf_release(struct dma_buf *dma_buf)
37 struct nouveau_bo *nvbo = dma_buf->priv;
39 if (nvbo->gem->export_dma_buf == dma_buf) {
40 nvbo->gem->export_dma_buf = NULL;
41 drm_gem_object_unreference_unlocked(nvbo->gem);
45 static void *nouveau_gem_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
50 static void nouveau_gem_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
54 static void *nouveau_gem_kmap(struct dma_buf *dma_buf, unsigned long page_num)
59 static void nouveau_gem_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
64 static const struct dma_buf_ops nouveau_dmabuf_ops = {
65 .map_dma_buf = nouveau_gem_map_dma_buf,
66 .unmap_dma_buf = nouveau_gem_unmap_dma_buf,
67 .release = nouveau_gem_dmabuf_release,
68 .kmap = nouveau_gem_kmap,
69 .kmap_atomic = nouveau_gem_kmap_atomic,
70 .kunmap = nouveau_gem_kunmap,
71 .kunmap_atomic = nouveau_gem_kunmap_atomic,
75 nouveau_prime_new(struct drm_device *dev,
78 struct nouveau_bo **pnvbo)
80 struct nouveau_bo *nvbo;
84 flags = TTM_PL_FLAG_TT;
86 ret = nouveau_bo_new(dev, size, 0, flags, 0, 0,
92 /* we restrict allowed domains on nv50+ to only the types
93 * that were requested at creation time. not possibly on
94 * earlier chips without busting the ABI.
96 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART;
97 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
99 nouveau_bo_ref(NULL, pnvbo);
103 nvbo->gem->driver_private = nvbo;
107 struct dma_buf *nouveau_gem_prime_export(struct drm_device *dev,
108 struct drm_gem_object *obj, int flags)
110 struct nouveau_bo *nvbo = nouveau_gem_object(obj);
113 /* pin buffer into GTT */
114 ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_TT);
116 return ERR_PTR(-EINVAL);
118 return dma_buf_export(nvbo, &nouveau_dmabuf_ops, obj->size, flags);
121 struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev,
122 struct dma_buf *dma_buf)
124 struct dma_buf_attachment *attach;
126 struct nouveau_bo *nvbo;
129 if (dma_buf->ops == &nouveau_dmabuf_ops) {
130 nvbo = dma_buf->priv;
132 if (nvbo->gem->dev == dev) {
133 drm_gem_object_reference(nvbo->gem);
139 attach = dma_buf_attach(dma_buf, dev->dev);
141 return ERR_PTR(PTR_ERR(attach));
143 sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
149 ret = nouveau_prime_new(dev, dma_buf->size, sg, &nvbo);
153 nvbo->gem->import_attach = attach;
158 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
160 dma_buf_detach(dma_buf, attach);