2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/dma-buf.h>
19 #include "etnaviv_drv.h"
20 #include "etnaviv_gem.h"
23 struct sg_table
*etnaviv_gem_prime_get_sg_table(struct drm_gem_object
*obj
)
25 struct etnaviv_gem_object
*etnaviv_obj
= to_etnaviv_bo(obj
);
26 int npages
= obj
->size
>> PAGE_SHIFT
;
28 if (WARN_ON(!etnaviv_obj
->pages
)) /* should have already pinned! */
31 return drm_prime_pages_to_sg(etnaviv_obj
->pages
, npages
);
34 void *etnaviv_gem_prime_vmap(struct drm_gem_object
*obj
)
36 return etnaviv_gem_vmap(obj
);
39 void etnaviv_gem_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)
41 /* TODO msm_gem_vunmap() */
44 int etnaviv_gem_prime_mmap(struct drm_gem_object
*obj
,
45 struct vm_area_struct
*vma
)
47 struct etnaviv_gem_object
*etnaviv_obj
= to_etnaviv_bo(obj
);
50 ret
= drm_gem_mmap_obj(obj
, obj
->size
, vma
);
54 return etnaviv_obj
->ops
->mmap(etnaviv_obj
, vma
);
57 int etnaviv_gem_prime_pin(struct drm_gem_object
*obj
)
59 if (!obj
->import_attach
) {
60 struct etnaviv_gem_object
*etnaviv_obj
= to_etnaviv_bo(obj
);
62 mutex_lock(&etnaviv_obj
->lock
);
63 etnaviv_gem_get_pages(etnaviv_obj
);
64 mutex_unlock(&etnaviv_obj
->lock
);
69 void etnaviv_gem_prime_unpin(struct drm_gem_object
*obj
)
71 if (!obj
->import_attach
) {
72 struct etnaviv_gem_object
*etnaviv_obj
= to_etnaviv_bo(obj
);
74 mutex_lock(&etnaviv_obj
->lock
);
75 etnaviv_gem_put_pages(to_etnaviv_bo(obj
));
76 mutex_unlock(&etnaviv_obj
->lock
);
80 static void etnaviv_gem_prime_release(struct etnaviv_gem_object
*etnaviv_obj
)
82 if (etnaviv_obj
->vaddr
)
83 dma_buf_vunmap(etnaviv_obj
->base
.import_attach
->dmabuf
,
86 /* Don't drop the pages for imported dmabuf, as they are not
87 * ours, just free the array we allocated:
89 if (etnaviv_obj
->pages
)
90 drm_free_large(etnaviv_obj
->pages
);
92 drm_prime_gem_destroy(&etnaviv_obj
->base
, etnaviv_obj
->sgt
);
95 static void *etnaviv_gem_prime_vmap_impl(struct etnaviv_gem_object
*etnaviv_obj
)
97 lockdep_assert_held(&etnaviv_obj
->lock
);
99 return dma_buf_vmap(etnaviv_obj
->base
.import_attach
->dmabuf
);
102 static int etnaviv_gem_prime_mmap_obj(struct etnaviv_gem_object
*etnaviv_obj
,
103 struct vm_area_struct
*vma
)
105 return dma_buf_mmap(etnaviv_obj
->base
.dma_buf
, vma
, 0);
108 static const struct etnaviv_gem_ops etnaviv_gem_prime_ops
= {
109 /* .get_pages should never be called */
110 .release
= etnaviv_gem_prime_release
,
111 .vmap
= etnaviv_gem_prime_vmap_impl
,
112 .mmap
= etnaviv_gem_prime_mmap_obj
,
115 struct drm_gem_object
*etnaviv_gem_prime_import_sg_table(struct drm_device
*dev
,
116 struct dma_buf_attachment
*attach
, struct sg_table
*sgt
)
118 struct etnaviv_gem_object
*etnaviv_obj
;
119 size_t size
= PAGE_ALIGN(attach
->dmabuf
->size
);
122 ret
= etnaviv_gem_new_private(dev
, size
, ETNA_BO_WC
,
123 attach
->dmabuf
->resv
,
124 &etnaviv_gem_prime_ops
, &etnaviv_obj
);
128 npages
= size
/ PAGE_SIZE
;
130 etnaviv_obj
->sgt
= sgt
;
131 etnaviv_obj
->pages
= drm_malloc_ab(npages
, sizeof(struct page
*));
132 if (!etnaviv_obj
->pages
) {
137 ret
= drm_prime_sg_to_page_addr_arrays(sgt
, etnaviv_obj
->pages
,
142 ret
= etnaviv_gem_obj_add(dev
, &etnaviv_obj
->base
);
146 return &etnaviv_obj
->base
;
149 drm_gem_object_unreference_unlocked(&etnaviv_obj
->base
);