Commit ab9ccb96 authored by Aaron Plattner's avatar Aaron Plattner Committed by Dave Airlie

drm/nouveau: use prime helpers

Simplify the Nouveau prime implementation by using the default behavior provided
by drm_gem_prime_import and drm_gem_prime_export.

v2: Rename functions to nouveau_gem_prime_get_sg_table and
nouveau_gem_prime_import_sg_table.
Signed-off-by: default avatarAaron Plattner <aplattner@nvidia.com>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: David Airlie <airlied@linux.ie>
Signed-off-by: default avatarDave Airlie <airlied@redhat.com>
parent 89177644
...@@ -33,7 +33,6 @@ struct nouveau_bo { ...@@ -33,7 +33,6 @@ struct nouveau_bo {
int pin_refcnt; int pin_refcnt;
struct ttm_bo_kmap_obj dma_buf_vmap; struct ttm_bo_kmap_obj dma_buf_vmap;
int vmapping_count;
}; };
static inline struct nouveau_bo * static inline struct nouveau_bo *
......
...@@ -650,8 +650,13 @@ driver = { ...@@ -650,8 +650,13 @@ driver = {
.prime_handle_to_fd = drm_gem_prime_handle_to_fd, .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
.prime_fd_to_handle = drm_gem_prime_fd_to_handle, .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
.gem_prime_export = nouveau_gem_prime_export, .gem_prime_export = drm_gem_prime_export,
.gem_prime_import = nouveau_gem_prime_import, .gem_prime_import = drm_gem_prime_import,
.gem_prime_pin = nouveau_gem_prime_pin,
.gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
.gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
.gem_prime_vmap = nouveau_gem_prime_vmap,
.gem_prime_vunmap = nouveau_gem_prime_vunmap,
.gem_init_object = nouveau_gem_object_new, .gem_init_object = nouveau_gem_object_new,
.gem_free_object = nouveau_gem_object_del, .gem_free_object = nouveau_gem_object_del,
......
...@@ -24,8 +24,6 @@ ...@@ -24,8 +24,6 @@
* *
*/ */
#include <linux/dma-buf.h>
#include <subdev/fb.h> #include <subdev/fb.h>
#include "nouveau_drm.h" #include "nouveau_drm.h"
......
...@@ -35,9 +35,11 @@ extern int nouveau_gem_ioctl_cpu_fini(struct drm_device *, void *, ...@@ -35,9 +35,11 @@ extern int nouveau_gem_ioctl_cpu_fini(struct drm_device *, void *,
extern int nouveau_gem_ioctl_info(struct drm_device *, void *, extern int nouveau_gem_ioctl_info(struct drm_device *, void *,
struct drm_file *); struct drm_file *);
extern struct dma_buf *nouveau_gem_prime_export(struct drm_device *dev, extern int nouveau_gem_prime_pin(struct drm_gem_object *);
struct drm_gem_object *obj, int flags); extern struct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *);
extern struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev, extern struct drm_gem_object *nouveau_gem_prime_import_sg_table(
struct dma_buf *dma_buf); struct drm_device *, size_t size, struct sg_table *);
extern void *nouveau_gem_prime_vmap(struct drm_gem_object *);
extern void nouveau_gem_prime_vunmap(struct drm_gem_object *, void *);
#endif #endif
...@@ -22,126 +22,42 @@ ...@@ -22,126 +22,42 @@
* Authors: Dave Airlie * Authors: Dave Airlie
*/ */
#include <linux/dma-buf.h>
#include <drm/drmP.h> #include <drm/drmP.h>
#include "nouveau_drm.h" #include "nouveau_drm.h"
#include "nouveau_gem.h" #include "nouveau_gem.h"
static struct sg_table *nouveau_gem_map_dma_buf(struct dma_buf_attachment *attachment, struct sg_table *nouveau_gem_prime_get_sg_table(struct drm_gem_object *obj)
enum dma_data_direction dir)
{ {
struct nouveau_bo *nvbo = attachment->dmabuf->priv; struct nouveau_bo *nvbo = nouveau_gem_object(obj);
struct drm_device *dev = nvbo->gem->dev;
int npages = nvbo->bo.num_pages; int npages = nvbo->bo.num_pages;
struct sg_table *sg;
int nents;
mutex_lock(&dev->struct_mutex);
sg = drm_prime_pages_to_sg(nvbo->bo.ttm->pages, npages);
nents = dma_map_sg(attachment->dev, sg->sgl, sg->nents, dir);
mutex_unlock(&dev->struct_mutex);
return sg;
}
static void nouveau_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
struct sg_table *sg, enum dma_data_direction dir)
{
dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
sg_free_table(sg);
kfree(sg);
}
static void nouveau_gem_dmabuf_release(struct dma_buf *dma_buf)
{
struct nouveau_bo *nvbo = dma_buf->priv;
if (nvbo->gem->export_dma_buf == dma_buf) {
nvbo->gem->export_dma_buf = NULL;
drm_gem_object_unreference_unlocked(nvbo->gem);
}
}
static void *nouveau_gem_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
{
return NULL;
}
static void nouveau_gem_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
{
} return drm_prime_pages_to_sg(nvbo->bo.ttm->pages, npages);
static void *nouveau_gem_kmap(struct dma_buf *dma_buf, unsigned long page_num)
{
return NULL;
} }
static void nouveau_gem_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr) void *nouveau_gem_prime_vmap(struct drm_gem_object *obj)
{ {
struct nouveau_bo *nvbo = nouveau_gem_object(obj);
}
static int nouveau_gem_prime_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
{
return -EINVAL;
}
static void *nouveau_gem_prime_vmap(struct dma_buf *dma_buf)
{
struct nouveau_bo *nvbo = dma_buf->priv;
struct drm_device *dev = nvbo->gem->dev;
int ret; int ret;
mutex_lock(&dev->struct_mutex);
if (nvbo->vmapping_count) {
nvbo->vmapping_count++;
goto out_unlock;
}
ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.num_pages, ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.num_pages,
&nvbo->dma_buf_vmap); &nvbo->dma_buf_vmap);
if (ret) { if (ret)
mutex_unlock(&dev->struct_mutex);
return ERR_PTR(ret); return ERR_PTR(ret);
}
nvbo->vmapping_count = 1;
out_unlock:
mutex_unlock(&dev->struct_mutex);
return nvbo->dma_buf_vmap.virtual; return nvbo->dma_buf_vmap.virtual;
} }
static void nouveau_gem_prime_vunmap(struct dma_buf *dma_buf, void *vaddr) void nouveau_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
{ {
struct nouveau_bo *nvbo = dma_buf->priv; struct nouveau_bo *nvbo = nouveau_gem_object(obj);
struct drm_device *dev = nvbo->gem->dev;
mutex_lock(&dev->struct_mutex); ttm_bo_kunmap(&nvbo->dma_buf_vmap);
nvbo->vmapping_count--;
if (nvbo->vmapping_count == 0) {
ttm_bo_kunmap(&nvbo->dma_buf_vmap);
}
mutex_unlock(&dev->struct_mutex);
} }
static const struct dma_buf_ops nouveau_dmabuf_ops = { struct drm_gem_object *nouveau_gem_prime_import_sg_table(struct drm_device *dev,
.map_dma_buf = nouveau_gem_map_dma_buf, size_t size,
.unmap_dma_buf = nouveau_gem_unmap_dma_buf, struct sg_table *sg)
.release = nouveau_gem_dmabuf_release,
.kmap = nouveau_gem_kmap,
.kmap_atomic = nouveau_gem_kmap_atomic,
.kunmap = nouveau_gem_kunmap,
.kunmap_atomic = nouveau_gem_kunmap_atomic,
.mmap = nouveau_gem_prime_mmap,
.vmap = nouveau_gem_prime_vmap,
.vunmap = nouveau_gem_prime_vunmap,
};
static int
nouveau_prime_new(struct drm_device *dev,
size_t size,
struct sg_table *sg,
struct nouveau_bo **pnvbo)
{ {
struct nouveau_bo *nvbo; struct nouveau_bo *nvbo;
u32 flags = 0; u32 flags = 0;
...@@ -150,24 +66,22 @@ nouveau_prime_new(struct drm_device *dev, ...@@ -150,24 +66,22 @@ nouveau_prime_new(struct drm_device *dev,
flags = TTM_PL_FLAG_TT; flags = TTM_PL_FLAG_TT;
ret = nouveau_bo_new(dev, size, 0, flags, 0, 0, ret = nouveau_bo_new(dev, size, 0, flags, 0, 0,
sg, pnvbo); sg, &nvbo);
if (ret) if (ret)
return ret; return ERR_PTR(ret);
nvbo = *pnvbo;
nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART; nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART;
nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size); nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
if (!nvbo->gem) { if (!nvbo->gem) {
nouveau_bo_ref(NULL, pnvbo); nouveau_bo_ref(NULL, &nvbo);
return -ENOMEM; return ERR_PTR(-ENOMEM);
} }
nvbo->gem->driver_private = nvbo; nvbo->gem->driver_private = nvbo;
return 0; return nvbo->gem;
} }
struct dma_buf *nouveau_gem_prime_export(struct drm_device *dev, int nouveau_gem_prime_pin(struct drm_gem_object *obj)
struct drm_gem_object *obj, int flags)
{ {
struct nouveau_bo *nvbo = nouveau_gem_object(obj); struct nouveau_bo *nvbo = nouveau_gem_object(obj);
int ret = 0; int ret = 0;
...@@ -175,52 +89,7 @@ struct dma_buf *nouveau_gem_prime_export(struct drm_device *dev, ...@@ -175,52 +89,7 @@ struct dma_buf *nouveau_gem_prime_export(struct drm_device *dev,
/* pin buffer into GTT */ /* pin buffer into GTT */
ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_TT); ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_TT);
if (ret) if (ret)
return ERR_PTR(-EINVAL); return -EINVAL;
return dma_buf_export(nvbo, &nouveau_dmabuf_ops, obj->size, flags);
}
struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev,
struct dma_buf *dma_buf)
{
struct dma_buf_attachment *attach;
struct sg_table *sg;
struct nouveau_bo *nvbo;
int ret;
if (dma_buf->ops == &nouveau_dmabuf_ops) {
nvbo = dma_buf->priv;
if (nvbo->gem) {
if (nvbo->gem->dev == dev) {
drm_gem_object_reference(nvbo->gem);
dma_buf_put(dma_buf);
return nvbo->gem;
}
}
}
/* need to attach */
attach = dma_buf_attach(dma_buf, dev->dev);
if (IS_ERR(attach))
return ERR_PTR(PTR_ERR(attach));
sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
if (IS_ERR(sg)) {
ret = PTR_ERR(sg);
goto fail_detach;
}
ret = nouveau_prime_new(dev, dma_buf->size, sg, &nvbo);
if (ret)
goto fail_unmap;
nvbo->gem->import_attach = attach;
return nvbo->gem;
fail_unmap: return 0;
dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
fail_detach:
dma_buf_detach(dma_buf, attach);
return ERR_PTR(ret);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment