2018-05-08 14:20:54 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-12-03 17:21:29 +00:00
|
|
|
/*
|
2018-05-08 14:20:54 +00:00
|
|
|
* Copyright (C) 2014-2018 Etnaviv Project
|
2015-12-03 17:21:29 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-30 05:21:03 +00:00
|
|
|
#include <drm/drm_prime.h>
|
2015-12-03 17:21:29 +00:00
|
|
|
#include <linux/dma-buf.h>
|
2019-06-30 05:21:03 +00:00
|
|
|
|
2015-12-03 17:21:29 +00:00
|
|
|
#include "etnaviv_drv.h"
|
|
|
|
#include "etnaviv_gem.h"
|
|
|
|
|
2017-11-17 10:14:58 +00:00
|
|
|
static struct lock_class_key etnaviv_prime_lock_class;
|
2015-12-03 17:21:29 +00:00
|
|
|
|
|
|
|
struct sg_table *etnaviv_gem_prime_get_sg_table(struct drm_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
|
2016-11-30 14:26:26 +00:00
|
|
|
int npages = obj->size >> PAGE_SHIFT;
|
2015-12-03 17:21:29 +00:00
|
|
|
|
2016-11-30 14:26:26 +00:00
|
|
|
if (WARN_ON(!etnaviv_obj->pages)) /* should have already pinned! */
|
2019-02-05 09:08:19 +00:00
|
|
|
return ERR_PTR(-EINVAL);
|
2015-12-03 17:21:29 +00:00
|
|
|
|
2016-11-30 14:26:26 +00:00
|
|
|
return drm_prime_pages_to_sg(etnaviv_obj->pages, npages);
|
2015-12-03 17:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *etnaviv_gem_prime_vmap(struct drm_gem_object *obj)
|
|
|
|
{
|
2016-01-26 17:10:32 +00:00
|
|
|
return etnaviv_gem_vmap(obj);
|
2015-12-03 17:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void etnaviv_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
|
|
|
|
{
|
|
|
|
/* TODO msm_gem_vunmap() */
|
|
|
|
}
|
|
|
|
|
2016-11-18 14:15:16 +00:00
|
|
|
int etnaviv_gem_prime_mmap(struct drm_gem_object *obj,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = drm_gem_mmap_obj(obj, obj->size, vma);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return etnaviv_obj->ops->mmap(etnaviv_obj, vma);
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:21:29 +00:00
|
|
|
int etnaviv_gem_prime_pin(struct drm_gem_object *obj)
|
|
|
|
{
|
|
|
|
if (!obj->import_attach) {
|
|
|
|
struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
|
|
|
|
|
|
|
|
mutex_lock(&etnaviv_obj->lock);
|
|
|
|
etnaviv_gem_get_pages(etnaviv_obj);
|
|
|
|
mutex_unlock(&etnaviv_obj->lock);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void etnaviv_gem_prime_unpin(struct drm_gem_object *obj)
|
|
|
|
{
|
|
|
|
if (!obj->import_attach) {
|
|
|
|
struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
|
|
|
|
|
|
|
|
mutex_lock(&etnaviv_obj->lock);
|
|
|
|
etnaviv_gem_put_pages(to_etnaviv_bo(obj));
|
|
|
|
mutex_unlock(&etnaviv_obj->lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void etnaviv_gem_prime_release(struct etnaviv_gem_object *etnaviv_obj)
|
|
|
|
{
|
|
|
|
if (etnaviv_obj->vaddr)
|
|
|
|
dma_buf_vunmap(etnaviv_obj->base.import_attach->dmabuf,
|
|
|
|
etnaviv_obj->vaddr);
|
|
|
|
|
|
|
|
/* Don't drop the pages for imported dmabuf, as they are not
|
|
|
|
* ours, just free the array we allocated:
|
|
|
|
*/
|
|
|
|
if (etnaviv_obj->pages)
|
2017-05-17 12:23:12 +00:00
|
|
|
kvfree(etnaviv_obj->pages);
|
2015-12-03 17:21:29 +00:00
|
|
|
|
|
|
|
drm_prime_gem_destroy(&etnaviv_obj->base, etnaviv_obj->sgt);
|
|
|
|
}
|
|
|
|
|
2016-01-25 14:47:28 +00:00
|
|
|
static void *etnaviv_gem_prime_vmap_impl(struct etnaviv_gem_object *etnaviv_obj)
|
|
|
|
{
|
|
|
|
lockdep_assert_held(&etnaviv_obj->lock);
|
|
|
|
|
|
|
|
return dma_buf_vmap(etnaviv_obj->base.import_attach->dmabuf);
|
|
|
|
}
|
|
|
|
|
2016-04-27 10:27:02 +00:00
|
|
|
static int etnaviv_gem_prime_mmap_obj(struct etnaviv_gem_object *etnaviv_obj,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
return dma_buf_mmap(etnaviv_obj->base.dma_buf, vma, 0);
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:21:29 +00:00
|
|
|
static const struct etnaviv_gem_ops etnaviv_gem_prime_ops = {
|
|
|
|
/* .get_pages should never be called */
|
|
|
|
.release = etnaviv_gem_prime_release,
|
2016-01-25 14:47:28 +00:00
|
|
|
.vmap = etnaviv_gem_prime_vmap_impl,
|
2016-04-27 10:27:02 +00:00
|
|
|
.mmap = etnaviv_gem_prime_mmap_obj,
|
2015-12-03 17:21:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct drm_gem_object *etnaviv_gem_prime_import_sg_table(struct drm_device *dev,
|
|
|
|
struct dma_buf_attachment *attach, struct sg_table *sgt)
|
|
|
|
{
|
|
|
|
struct etnaviv_gem_object *etnaviv_obj;
|
|
|
|
size_t size = PAGE_ALIGN(attach->dmabuf->size);
|
|
|
|
int ret, npages;
|
|
|
|
|
|
|
|
ret = etnaviv_gem_new_private(dev, size, ETNA_BO_WC,
|
|
|
|
attach->dmabuf->resv,
|
|
|
|
&etnaviv_gem_prime_ops, &etnaviv_obj);
|
|
|
|
if (ret < 0)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
|
2017-11-17 10:14:58 +00:00
|
|
|
lockdep_set_class(&etnaviv_obj->lock, &etnaviv_prime_lock_class);
|
|
|
|
|
2015-12-03 17:21:29 +00:00
|
|
|
npages = size / PAGE_SIZE;
|
|
|
|
|
|
|
|
etnaviv_obj->sgt = sgt;
|
2017-05-17 12:23:12 +00:00
|
|
|
etnaviv_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
|
2015-12-03 17:21:29 +00:00
|
|
|
if (!etnaviv_obj->pages) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = drm_prime_sg_to_page_addr_arrays(sgt, etnaviv_obj->pages,
|
|
|
|
NULL, npages);
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
2017-11-17 11:17:14 +00:00
|
|
|
etnaviv_gem_obj_add(dev, &etnaviv_obj->base);
|
2015-12-03 17:21:29 +00:00
|
|
|
|
|
|
|
return &etnaviv_obj->base;
|
|
|
|
|
|
|
|
fail:
|
2017-08-03 11:58:23 +00:00
|
|
|
drm_gem_object_put_unlocked(&etnaviv_obj->base);
|
2015-12-03 17:21:29 +00:00
|
|
|
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|