1f6ffbd4fSLucas Stach // SPDX-License-Identifier: GPL-2.0
2a8c21a54SThe etnaviv authors /*
3f6ffbd4fSLucas Stach * Copyright (C) 2015-2018 Etnaviv Project
4a8c21a54SThe etnaviv authors */
5a8c21a54SThe etnaviv authors
66eae41feSSam Ravnborg #include <drm/drm_prime.h>
76eae41feSSam Ravnborg #include <linux/dma-mapping.h>
8a8c21a54SThe etnaviv authors #include <linux/shmem_fs.h>
96eae41feSSam Ravnborg #include <linux/spinlock.h>
106eae41feSSam Ravnborg #include <linux/vmalloc.h>
11a8c21a54SThe etnaviv authors
12a8c21a54SThe etnaviv authors #include "etnaviv_drv.h"
13a8c21a54SThe etnaviv authors #include "etnaviv_gem.h"
14a8c21a54SThe etnaviv authors #include "etnaviv_gpu.h"
15a8c21a54SThe etnaviv authors #include "etnaviv_mmu.h"
16a8c21a54SThe etnaviv authors
17d6a8743dSLucas Stach static struct lock_class_key etnaviv_shm_lock_class;
18d6a8743dSLucas Stach static struct lock_class_key etnaviv_userptr_lock_class;
19d6a8743dSLucas Stach
etnaviv_gem_scatter_map(struct etnaviv_gem_object * etnaviv_obj)20a8c21a54SThe etnaviv authors static void etnaviv_gem_scatter_map(struct etnaviv_gem_object *etnaviv_obj)
21a8c21a54SThe etnaviv authors {
22a8c21a54SThe etnaviv authors struct drm_device *dev = etnaviv_obj->base.dev;
23a8c21a54SThe etnaviv authors struct sg_table *sgt = etnaviv_obj->sgt;
24a8c21a54SThe etnaviv authors
25a8c21a54SThe etnaviv authors /*
26a8c21a54SThe etnaviv authors * For non-cached buffers, ensure the new pages are clean
27a8c21a54SThe etnaviv authors * because display controller, GPU, etc. are not coherent.
28a8c21a54SThe etnaviv authors */
29a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_CACHE_MASK)
30182354a5SMarek Szyprowski dma_map_sgtable(dev->dev, sgt, DMA_BIDIRECTIONAL, 0);
31a8c21a54SThe etnaviv authors }
32a8c21a54SThe etnaviv authors
etnaviv_gem_scatterlist_unmap(struct etnaviv_gem_object * etnaviv_obj)33a8c21a54SThe etnaviv authors static void etnaviv_gem_scatterlist_unmap(struct etnaviv_gem_object *etnaviv_obj)
34a8c21a54SThe etnaviv authors {
35a8c21a54SThe etnaviv authors struct drm_device *dev = etnaviv_obj->base.dev;
36a8c21a54SThe etnaviv authors struct sg_table *sgt = etnaviv_obj->sgt;
37a8c21a54SThe etnaviv authors
38a8c21a54SThe etnaviv authors /*
39a8c21a54SThe etnaviv authors * For non-cached buffers, ensure the new pages are clean
40a8c21a54SThe etnaviv authors * because display controller, GPU, etc. are not coherent:
41a8c21a54SThe etnaviv authors *
42a8c21a54SThe etnaviv authors * WARNING: The DMA API does not support concurrent CPU
43a8c21a54SThe etnaviv authors * and device access to the memory area. With BIDIRECTIONAL,
44a8c21a54SThe etnaviv authors * we will clean the cache lines which overlap the region,
45a8c21a54SThe etnaviv authors * and invalidate all cache lines (partially) contained in
46a8c21a54SThe etnaviv authors * the region.
47a8c21a54SThe etnaviv authors *
48a8c21a54SThe etnaviv authors * If you have dirty data in the overlapping cache lines,
49a8c21a54SThe etnaviv authors * that will corrupt the GPU-written data. If you have
50a8c21a54SThe etnaviv authors * written into the remainder of the region, this can
51a8c21a54SThe etnaviv authors * discard those writes.
52a8c21a54SThe etnaviv authors */
53a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_CACHE_MASK)
54182354a5SMarek Szyprowski dma_unmap_sgtable(dev->dev, sgt, DMA_BIDIRECTIONAL, 0);
55a8c21a54SThe etnaviv authors }
56a8c21a54SThe etnaviv authors
57a8c21a54SThe etnaviv authors /* called with etnaviv_obj->lock held */
etnaviv_gem_shmem_get_pages(struct etnaviv_gem_object * etnaviv_obj)58a8c21a54SThe etnaviv authors static int etnaviv_gem_shmem_get_pages(struct etnaviv_gem_object *etnaviv_obj)
59a8c21a54SThe etnaviv authors {
60a8c21a54SThe etnaviv authors struct drm_device *dev = etnaviv_obj->base.dev;
61a8c21a54SThe etnaviv authors struct page **p = drm_gem_get_pages(&etnaviv_obj->base);
62a8c21a54SThe etnaviv authors
63a8c21a54SThe etnaviv authors if (IS_ERR(p)) {
64f91ac470SLucas Stach dev_dbg(dev->dev, "could not get pages: %ld\n", PTR_ERR(p));
65a8c21a54SThe etnaviv authors return PTR_ERR(p);
66a8c21a54SThe etnaviv authors }
67a8c21a54SThe etnaviv authors
68a8c21a54SThe etnaviv authors etnaviv_obj->pages = p;
69a8c21a54SThe etnaviv authors
70a8c21a54SThe etnaviv authors return 0;
71a8c21a54SThe etnaviv authors }
72a8c21a54SThe etnaviv authors
put_pages(struct etnaviv_gem_object * etnaviv_obj)73a8c21a54SThe etnaviv authors static void put_pages(struct etnaviv_gem_object *etnaviv_obj)
74a8c21a54SThe etnaviv authors {
75a8c21a54SThe etnaviv authors if (etnaviv_obj->sgt) {
76a8c21a54SThe etnaviv authors etnaviv_gem_scatterlist_unmap(etnaviv_obj);
77a8c21a54SThe etnaviv authors sg_free_table(etnaviv_obj->sgt);
78a8c21a54SThe etnaviv authors kfree(etnaviv_obj->sgt);
79a8c21a54SThe etnaviv authors etnaviv_obj->sgt = NULL;
80a8c21a54SThe etnaviv authors }
81a8c21a54SThe etnaviv authors if (etnaviv_obj->pages) {
82a8c21a54SThe etnaviv authors drm_gem_put_pages(&etnaviv_obj->base, etnaviv_obj->pages,
83a8c21a54SThe etnaviv authors true, false);
84a8c21a54SThe etnaviv authors
85a8c21a54SThe etnaviv authors etnaviv_obj->pages = NULL;
86a8c21a54SThe etnaviv authors }
87a8c21a54SThe etnaviv authors }
88a8c21a54SThe etnaviv authors
etnaviv_gem_get_pages(struct etnaviv_gem_object * etnaviv_obj)89a8c21a54SThe etnaviv authors struct page **etnaviv_gem_get_pages(struct etnaviv_gem_object *etnaviv_obj)
90a8c21a54SThe etnaviv authors {
91a8c21a54SThe etnaviv authors int ret;
92a8c21a54SThe etnaviv authors
93a8c21a54SThe etnaviv authors lockdep_assert_held(&etnaviv_obj->lock);
94a8c21a54SThe etnaviv authors
95a8c21a54SThe etnaviv authors if (!etnaviv_obj->pages) {
96a8c21a54SThe etnaviv authors ret = etnaviv_obj->ops->get_pages(etnaviv_obj);
97a8c21a54SThe etnaviv authors if (ret < 0)
98a8c21a54SThe etnaviv authors return ERR_PTR(ret);
99a8c21a54SThe etnaviv authors }
100a8c21a54SThe etnaviv authors
101a8c21a54SThe etnaviv authors if (!etnaviv_obj->sgt) {
102a8c21a54SThe etnaviv authors struct drm_device *dev = etnaviv_obj->base.dev;
103a8c21a54SThe etnaviv authors int npages = etnaviv_obj->base.size >> PAGE_SHIFT;
104a8c21a54SThe etnaviv authors struct sg_table *sgt;
105a8c21a54SThe etnaviv authors
106707d561fSGerd Hoffmann sgt = drm_prime_pages_to_sg(etnaviv_obj->base.dev,
107707d561fSGerd Hoffmann etnaviv_obj->pages, npages);
108a8c21a54SThe etnaviv authors if (IS_ERR(sgt)) {
109a8c21a54SThe etnaviv authors dev_err(dev->dev, "failed to allocate sgt: %ld\n",
110a8c21a54SThe etnaviv authors PTR_ERR(sgt));
111a8c21a54SThe etnaviv authors return ERR_CAST(sgt);
112a8c21a54SThe etnaviv authors }
113a8c21a54SThe etnaviv authors
114a8c21a54SThe etnaviv authors etnaviv_obj->sgt = sgt;
115a8c21a54SThe etnaviv authors
116a8c21a54SThe etnaviv authors etnaviv_gem_scatter_map(etnaviv_obj);
117a8c21a54SThe etnaviv authors }
118a8c21a54SThe etnaviv authors
119a8c21a54SThe etnaviv authors return etnaviv_obj->pages;
120a8c21a54SThe etnaviv authors }
121a8c21a54SThe etnaviv authors
etnaviv_gem_put_pages(struct etnaviv_gem_object * etnaviv_obj)122a8c21a54SThe etnaviv authors void etnaviv_gem_put_pages(struct etnaviv_gem_object *etnaviv_obj)
123a8c21a54SThe etnaviv authors {
124a8c21a54SThe etnaviv authors lockdep_assert_held(&etnaviv_obj->lock);
125a8c21a54SThe etnaviv authors /* when we start tracking the pin count, then do something here */
126a8c21a54SThe etnaviv authors }
127a8c21a54SThe etnaviv authors
etnaviv_gem_mmap_obj(struct etnaviv_gem_object * etnaviv_obj,struct vm_area_struct * vma)1280e7f26e6SLucas Stach static int etnaviv_gem_mmap_obj(struct etnaviv_gem_object *etnaviv_obj,
129a8c21a54SThe etnaviv authors struct vm_area_struct *vma)
130a8c21a54SThe etnaviv authors {
131a8c21a54SThe etnaviv authors pgprot_t vm_page_prot;
132a8c21a54SThe etnaviv authors
133*1c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
134a8c21a54SThe etnaviv authors
135a8c21a54SThe etnaviv authors vm_page_prot = vm_get_page_prot(vma->vm_flags);
136a8c21a54SThe etnaviv authors
137a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_WC) {
138a8c21a54SThe etnaviv authors vma->vm_page_prot = pgprot_writecombine(vm_page_prot);
139a8c21a54SThe etnaviv authors } else if (etnaviv_obj->flags & ETNA_BO_UNCACHED) {
140a8c21a54SThe etnaviv authors vma->vm_page_prot = pgprot_noncached(vm_page_prot);
141a8c21a54SThe etnaviv authors } else {
142a8c21a54SThe etnaviv authors /*
143a8c21a54SThe etnaviv authors * Shunt off cached objs to shmem file so they have their own
144a8c21a54SThe etnaviv authors * address_space (so unmap_mapping_range does what we want,
145a8c21a54SThe etnaviv authors * in particular in the case of mmap'd dmabufs)
146a8c21a54SThe etnaviv authors */
147a8c21a54SThe etnaviv authors vma->vm_pgoff = 0;
148295992fbSChristian König vma_set_file(vma, etnaviv_obj->base.filp);
149a8c21a54SThe etnaviv authors
150a8c21a54SThe etnaviv authors vma->vm_page_prot = vm_page_prot;
151a8c21a54SThe etnaviv authors }
152a8c21a54SThe etnaviv authors
153a8c21a54SThe etnaviv authors return 0;
154a8c21a54SThe etnaviv authors }
155a8c21a54SThe etnaviv authors
etnaviv_gem_mmap(struct drm_gem_object * obj,struct vm_area_struct * vma)15681fd23e2SThomas Zimmermann static int etnaviv_gem_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
157a8c21a54SThe etnaviv authors {
15881fd23e2SThomas Zimmermann struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
159a8c21a54SThe etnaviv authors
16081fd23e2SThomas Zimmermann return etnaviv_obj->ops->mmap(etnaviv_obj, vma);
161a8c21a54SThe etnaviv authors }
162a8c21a54SThe etnaviv authors
etnaviv_gem_fault(struct vm_fault * vmf)163a7730627SThomas Zimmermann static vm_fault_t etnaviv_gem_fault(struct vm_fault *vmf)
164a8c21a54SThe etnaviv authors {
16511bac800SDave Jiang struct vm_area_struct *vma = vmf->vma;
166a8c21a54SThe etnaviv authors struct drm_gem_object *obj = vma->vm_private_data;
167a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
168a3b4c2f9SLucas Stach struct page **pages;
169a3b4c2f9SLucas Stach unsigned long pfn;
170a8c21a54SThe etnaviv authors pgoff_t pgoff;
171cfad05a2SSouptick Joarder int err;
172a8c21a54SThe etnaviv authors
173a8c21a54SThe etnaviv authors /*
174a8c21a54SThe etnaviv authors * Make sure we don't parallel update on a fault, nor move or remove
175cfad05a2SSouptick Joarder * something from beneath our feet. Note that vmf_insert_page() is
176a8c21a54SThe etnaviv authors * specifically coded to take care of this, so we don't have to.
177a8c21a54SThe etnaviv authors */
178cfad05a2SSouptick Joarder err = mutex_lock_interruptible(&etnaviv_obj->lock);
179cfad05a2SSouptick Joarder if (err)
180cfad05a2SSouptick Joarder return VM_FAULT_NOPAGE;
181a8c21a54SThe etnaviv authors /* make sure we have pages attached now */
182a8c21a54SThe etnaviv authors pages = etnaviv_gem_get_pages(etnaviv_obj);
183a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock);
184a8c21a54SThe etnaviv authors
185a8c21a54SThe etnaviv authors if (IS_ERR(pages)) {
186cfad05a2SSouptick Joarder err = PTR_ERR(pages);
187cfad05a2SSouptick Joarder return vmf_error(err);
188a8c21a54SThe etnaviv authors }
189a8c21a54SThe etnaviv authors
190a8c21a54SThe etnaviv authors /* We don't use vmf->pgoff since that has the fake offset: */
1911a29d85eSJan Kara pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
192a8c21a54SThe etnaviv authors
193a3b4c2f9SLucas Stach pfn = page_to_pfn(pages[pgoff]);
194a8c21a54SThe etnaviv authors
1951a29d85eSJan Kara VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
196a3b4c2f9SLucas Stach pfn, pfn << PAGE_SHIFT);
197a8c21a54SThe etnaviv authors
198a3b4c2f9SLucas Stach return vmf_insert_pfn(vma, vmf->address, pfn);
199a8c21a54SThe etnaviv authors }
200a8c21a54SThe etnaviv authors
etnaviv_gem_mmap_offset(struct drm_gem_object * obj,u64 * offset)201a8c21a54SThe etnaviv authors int etnaviv_gem_mmap_offset(struct drm_gem_object *obj, u64 *offset)
202a8c21a54SThe etnaviv authors {
203a8c21a54SThe etnaviv authors int ret;
204a8c21a54SThe etnaviv authors
205a8c21a54SThe etnaviv authors /* Make it mmapable */
206a8c21a54SThe etnaviv authors ret = drm_gem_create_mmap_offset(obj);
207a8c21a54SThe etnaviv authors if (ret)
208a8c21a54SThe etnaviv authors dev_err(obj->dev->dev, "could not allocate mmap offset\n");
209a8c21a54SThe etnaviv authors else
210a8c21a54SThe etnaviv authors *offset = drm_vma_node_offset_addr(&obj->vma_node);
211a8c21a54SThe etnaviv authors
212a8c21a54SThe etnaviv authors return ret;
213a8c21a54SThe etnaviv authors }
214a8c21a54SThe etnaviv authors
215a8c21a54SThe etnaviv authors static struct etnaviv_vram_mapping *
etnaviv_gem_get_vram_mapping(struct etnaviv_gem_object * obj,struct etnaviv_iommu_context * context)216a8c21a54SThe etnaviv authors etnaviv_gem_get_vram_mapping(struct etnaviv_gem_object *obj,
21727b67278SLucas Stach struct etnaviv_iommu_context *context)
218a8c21a54SThe etnaviv authors {
219a8c21a54SThe etnaviv authors struct etnaviv_vram_mapping *mapping;
220a8c21a54SThe etnaviv authors
221a8c21a54SThe etnaviv authors list_for_each_entry(mapping, &obj->vram_list, obj_node) {
22227b67278SLucas Stach if (mapping->context == context)
223a8c21a54SThe etnaviv authors return mapping;
224a8c21a54SThe etnaviv authors }
225a8c21a54SThe etnaviv authors
226a8c21a54SThe etnaviv authors return NULL;
227a8c21a54SThe etnaviv authors }
228a8c21a54SThe etnaviv authors
etnaviv_gem_mapping_unreference(struct etnaviv_vram_mapping * mapping)229b6325f40SRussell King void etnaviv_gem_mapping_unreference(struct etnaviv_vram_mapping *mapping)
230b6325f40SRussell King {
231b6325f40SRussell King struct etnaviv_gem_object *etnaviv_obj = mapping->object;
232b6325f40SRussell King
233b6325f40SRussell King mutex_lock(&etnaviv_obj->lock);
234b6325f40SRussell King WARN_ON(mapping->use == 0);
235b6325f40SRussell King mapping->use -= 1;
236b6325f40SRussell King mutex_unlock(&etnaviv_obj->lock);
237b6325f40SRussell King
2386780bf32SEmil Velikov drm_gem_object_put(&etnaviv_obj->base);
239b6325f40SRussell King }
240b6325f40SRussell King
etnaviv_gem_mapping_get(struct drm_gem_object * obj,struct etnaviv_iommu_context * mmu_context,u64 va)241b6325f40SRussell King struct etnaviv_vram_mapping *etnaviv_gem_mapping_get(
242088880ddSLucas Stach struct drm_gem_object *obj, struct etnaviv_iommu_context *mmu_context,
243088880ddSLucas Stach u64 va)
244a8c21a54SThe etnaviv authors {
245a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
246a8c21a54SThe etnaviv authors struct etnaviv_vram_mapping *mapping;
247a8c21a54SThe etnaviv authors struct page **pages;
248a8c21a54SThe etnaviv authors int ret = 0;
249a8c21a54SThe etnaviv authors
250a8c21a54SThe etnaviv authors mutex_lock(&etnaviv_obj->lock);
251e6364d70SLucas Stach mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, mmu_context);
252a8c21a54SThe etnaviv authors if (mapping) {
253a8c21a54SThe etnaviv authors /*
254a8c21a54SThe etnaviv authors * Holding the object lock prevents the use count changing
255a8c21a54SThe etnaviv authors * beneath us. If the use count is zero, the MMU might be
256a8c21a54SThe etnaviv authors * reaping this object, so take the lock and re-check that
257a8c21a54SThe etnaviv authors * the MMU owns this mapping to close this race.
258a8c21a54SThe etnaviv authors */
259a8c21a54SThe etnaviv authors if (mapping->use == 0) {
260e6364d70SLucas Stach mutex_lock(&mmu_context->lock);
261e6364d70SLucas Stach if (mapping->context == mmu_context)
262332f8472SLucas Stach if (va && mapping->iova != va) {
263332f8472SLucas Stach etnaviv_iommu_reap_mapping(mapping);
264332f8472SLucas Stach mapping = NULL;
265332f8472SLucas Stach } else {
266a8c21a54SThe etnaviv authors mapping->use += 1;
267332f8472SLucas Stach }
268a8c21a54SThe etnaviv authors else
269a8c21a54SThe etnaviv authors mapping = NULL;
270e6364d70SLucas Stach mutex_unlock(&mmu_context->lock);
271a8c21a54SThe etnaviv authors if (mapping)
272a8c21a54SThe etnaviv authors goto out;
273a8c21a54SThe etnaviv authors } else {
274a8c21a54SThe etnaviv authors mapping->use += 1;
275a8c21a54SThe etnaviv authors goto out;
276a8c21a54SThe etnaviv authors }
277a8c21a54SThe etnaviv authors }
278a8c21a54SThe etnaviv authors
279a8c21a54SThe etnaviv authors pages = etnaviv_gem_get_pages(etnaviv_obj);
280a8c21a54SThe etnaviv authors if (IS_ERR(pages)) {
281a8c21a54SThe etnaviv authors ret = PTR_ERR(pages);
282a8c21a54SThe etnaviv authors goto out;
283a8c21a54SThe etnaviv authors }
284a8c21a54SThe etnaviv authors
285a8c21a54SThe etnaviv authors /*
286a8c21a54SThe etnaviv authors * See if we have a reaped vram mapping we can re-use before
287a8c21a54SThe etnaviv authors * allocating a fresh mapping.
288a8c21a54SThe etnaviv authors */
289a8c21a54SThe etnaviv authors mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, NULL);
290a8c21a54SThe etnaviv authors if (!mapping) {
291a8c21a54SThe etnaviv authors mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
292ed94add0SDan Carpenter if (!mapping) {
293ed94add0SDan Carpenter ret = -ENOMEM;
294ed94add0SDan Carpenter goto out;
295ed94add0SDan Carpenter }
296a8c21a54SThe etnaviv authors
297a8c21a54SThe etnaviv authors INIT_LIST_HEAD(&mapping->scan_node);
298a8c21a54SThe etnaviv authors mapping->object = etnaviv_obj;
299a8c21a54SThe etnaviv authors } else {
300a8c21a54SThe etnaviv authors list_del(&mapping->obj_node);
301a8c21a54SThe etnaviv authors }
302a8c21a54SThe etnaviv authors
303a8c21a54SThe etnaviv authors mapping->use = 1;
304a8c21a54SThe etnaviv authors
30517e4660aSLucas Stach ret = etnaviv_iommu_map_gem(mmu_context, etnaviv_obj,
30617eae23bSLucas Stach mmu_context->global->memory_base,
307088880ddSLucas Stach mapping, va);
30811ad6a1fSLucas Stach if (ret < 0)
309a8c21a54SThe etnaviv authors kfree(mapping);
31011ad6a1fSLucas Stach else
311a8c21a54SThe etnaviv authors list_add_tail(&mapping->obj_node, &etnaviv_obj->vram_list);
312a8c21a54SThe etnaviv authors
313a8c21a54SThe etnaviv authors out:
314a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock);
315a8c21a54SThe etnaviv authors
316b6325f40SRussell King if (ret)
317b6325f40SRussell King return ERR_PTR(ret);
318b6325f40SRussell King
319a8c21a54SThe etnaviv authors /* Take a reference on the object */
32023d1dd03SCihangir Akturk drm_gem_object_get(obj);
321b6325f40SRussell King return mapping;
322a8c21a54SThe etnaviv authors }
323a8c21a54SThe etnaviv authors
etnaviv_gem_vmap(struct drm_gem_object * obj)324ce3088fdSLucas Stach void *etnaviv_gem_vmap(struct drm_gem_object *obj)
325a8c21a54SThe etnaviv authors {
326a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
327a8c21a54SThe etnaviv authors
328a0a5ab3eSLucas Stach if (etnaviv_obj->vaddr)
329a0a5ab3eSLucas Stach return etnaviv_obj->vaddr;
330a0a5ab3eSLucas Stach
331a8c21a54SThe etnaviv authors mutex_lock(&etnaviv_obj->lock);
332a0a5ab3eSLucas Stach /*
333a0a5ab3eSLucas Stach * Need to check again, as we might have raced with another thread
334a0a5ab3eSLucas Stach * while waiting for the mutex.
335a0a5ab3eSLucas Stach */
336a0a5ab3eSLucas Stach if (!etnaviv_obj->vaddr)
337a0a5ab3eSLucas Stach etnaviv_obj->vaddr = etnaviv_obj->ops->vmap(etnaviv_obj);
338a8c21a54SThe etnaviv authors mutex_unlock(&etnaviv_obj->lock);
339a8c21a54SThe etnaviv authors
340a8c21a54SThe etnaviv authors return etnaviv_obj->vaddr;
341a8c21a54SThe etnaviv authors }
342a8c21a54SThe etnaviv authors
etnaviv_gem_vmap_impl(struct etnaviv_gem_object * obj)343a0a5ab3eSLucas Stach static void *etnaviv_gem_vmap_impl(struct etnaviv_gem_object *obj)
344a0a5ab3eSLucas Stach {
345a0a5ab3eSLucas Stach struct page **pages;
346a0a5ab3eSLucas Stach
347a0a5ab3eSLucas Stach lockdep_assert_held(&obj->lock);
348a0a5ab3eSLucas Stach
349a0a5ab3eSLucas Stach pages = etnaviv_gem_get_pages(obj);
350a0a5ab3eSLucas Stach if (IS_ERR(pages))
351a0a5ab3eSLucas Stach return NULL;
352a0a5ab3eSLucas Stach
353a0a5ab3eSLucas Stach return vmap(pages, obj->base.size >> PAGE_SHIFT,
354a0a5ab3eSLucas Stach VM_MAP, pgprot_writecombine(PAGE_KERNEL));
355a0a5ab3eSLucas Stach }
356a0a5ab3eSLucas Stach
etnaviv_op_to_dma_dir(u32 op)357a8c21a54SThe etnaviv authors static inline enum dma_data_direction etnaviv_op_to_dma_dir(u32 op)
358a8c21a54SThe etnaviv authors {
359a8c21a54SThe etnaviv authors if (op & ETNA_PREP_READ)
360a8c21a54SThe etnaviv authors return DMA_FROM_DEVICE;
361a8c21a54SThe etnaviv authors else if (op & ETNA_PREP_WRITE)
362a8c21a54SThe etnaviv authors return DMA_TO_DEVICE;
363a8c21a54SThe etnaviv authors else
364a8c21a54SThe etnaviv authors return DMA_BIDIRECTIONAL;
365a8c21a54SThe etnaviv authors }
366a8c21a54SThe etnaviv authors
etnaviv_gem_cpu_prep(struct drm_gem_object * obj,u32 op,struct drm_etnaviv_timespec * timeout)367a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op,
36838c4a4cfSArnd Bergmann struct drm_etnaviv_timespec *timeout)
369a8c21a54SThe etnaviv authors {
370a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
371a8c21a54SThe etnaviv authors struct drm_device *dev = obj->dev;
372a8c21a54SThe etnaviv authors bool write = !!(op & ETNA_PREP_WRITE);
37346a269daSLucas Stach int ret;
374a8c21a54SThe etnaviv authors
3758cc47b3eSLucas Stach if (!etnaviv_obj->sgt) {
3768cc47b3eSLucas Stach void *ret;
3778cc47b3eSLucas Stach
3788cc47b3eSLucas Stach mutex_lock(&etnaviv_obj->lock);
3798cc47b3eSLucas Stach ret = etnaviv_gem_get_pages(etnaviv_obj);
3808cc47b3eSLucas Stach mutex_unlock(&etnaviv_obj->lock);
3818cc47b3eSLucas Stach if (IS_ERR(ret))
3828cc47b3eSLucas Stach return PTR_ERR(ret);
3838cc47b3eSLucas Stach }
3848cc47b3eSLucas Stach
38546a269daSLucas Stach if (op & ETNA_PREP_NOSYNC) {
3867bc80a54SChristian König if (!dma_resv_test_signaled(obj->resv,
3877bc80a54SChristian König dma_resv_usage_rw(write)))
38846a269daSLucas Stach return -EBUSY;
38946a269daSLucas Stach } else {
39046a269daSLucas Stach unsigned long remain = etnaviv_timeout_to_jiffies(timeout);
39146a269daSLucas Stach
3927bc80a54SChristian König ret = dma_resv_wait_timeout(obj->resv, dma_resv_usage_rw(write),
3937bc80a54SChristian König true, remain);
39446a269daSLucas Stach if (ret <= 0)
39546a269daSLucas Stach return ret == 0 ? -ETIMEDOUT : ret;
39646a269daSLucas Stach }
397a8c21a54SThe etnaviv authors
398a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_CACHED) {
399182354a5SMarek Szyprowski dma_sync_sgtable_for_cpu(dev->dev, etnaviv_obj->sgt,
400a8c21a54SThe etnaviv authors etnaviv_op_to_dma_dir(op));
401a8c21a54SThe etnaviv authors etnaviv_obj->last_cpu_prep_op = op;
402a8c21a54SThe etnaviv authors }
403a8c21a54SThe etnaviv authors
404a8c21a54SThe etnaviv authors return 0;
405a8c21a54SThe etnaviv authors }
406a8c21a54SThe etnaviv authors
etnaviv_gem_cpu_fini(struct drm_gem_object * obj)407a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_fini(struct drm_gem_object *obj)
408a8c21a54SThe etnaviv authors {
409a8c21a54SThe etnaviv authors struct drm_device *dev = obj->dev;
410a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
411a8c21a54SThe etnaviv authors
412a8c21a54SThe etnaviv authors if (etnaviv_obj->flags & ETNA_BO_CACHED) {
413a8c21a54SThe etnaviv authors /* fini without a prep is almost certainly a userspace error */
414a8c21a54SThe etnaviv authors WARN_ON(etnaviv_obj->last_cpu_prep_op == 0);
415182354a5SMarek Szyprowski dma_sync_sgtable_for_device(dev->dev, etnaviv_obj->sgt,
416a8c21a54SThe etnaviv authors etnaviv_op_to_dma_dir(etnaviv_obj->last_cpu_prep_op));
417a8c21a54SThe etnaviv authors etnaviv_obj->last_cpu_prep_op = 0;
418a8c21a54SThe etnaviv authors }
419a8c21a54SThe etnaviv authors
420a8c21a54SThe etnaviv authors return 0;
421a8c21a54SThe etnaviv authors }
422a8c21a54SThe etnaviv authors
etnaviv_gem_wait_bo(struct etnaviv_gpu * gpu,struct drm_gem_object * obj,struct drm_etnaviv_timespec * timeout)423a8c21a54SThe etnaviv authors int etnaviv_gem_wait_bo(struct etnaviv_gpu *gpu, struct drm_gem_object *obj,
42438c4a4cfSArnd Bergmann struct drm_etnaviv_timespec *timeout)
425a8c21a54SThe etnaviv authors {
426a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
427a8c21a54SThe etnaviv authors
428a8c21a54SThe etnaviv authors return etnaviv_gpu_wait_obj_inactive(gpu, etnaviv_obj, timeout);
429a8c21a54SThe etnaviv authors }
430a8c21a54SThe etnaviv authors
431a8c21a54SThe etnaviv authors #ifdef CONFIG_DEBUG_FS
etnaviv_gem_describe(struct drm_gem_object * obj,struct seq_file * m)432a8c21a54SThe etnaviv authors static void etnaviv_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
433a8c21a54SThe etnaviv authors {
434a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
43552791eeeSChristian König struct dma_resv *robj = obj->resv;
436a8c21a54SThe etnaviv authors unsigned long off = drm_vma_node_start(&obj->vma_node);
437790f27e0SChristian König int r;
438a8c21a54SThe etnaviv authors
439a8c21a54SThe etnaviv authors seq_printf(m, "%08x: %c %2d (%2d) %08lx %p %zd\n",
440a8c21a54SThe etnaviv authors etnaviv_obj->flags, is_active(etnaviv_obj) ? 'A' : 'I',
4412c935bc5SPeter Zijlstra obj->name, kref_read(&obj->refcount),
442a8c21a54SThe etnaviv authors off, etnaviv_obj->vaddr, obj->size);
443a8c21a54SThe etnaviv authors
444790f27e0SChristian König r = dma_resv_lock(robj, NULL);
445790f27e0SChristian König if (r)
446790f27e0SChristian König return;
447790f27e0SChristian König
448790f27e0SChristian König dma_resv_describe(robj, m);
449790f27e0SChristian König dma_resv_unlock(robj);
450a8c21a54SThe etnaviv authors }
451a8c21a54SThe etnaviv authors
etnaviv_gem_describe_objects(struct etnaviv_drm_private * priv,struct seq_file * m)452a8c21a54SThe etnaviv authors void etnaviv_gem_describe_objects(struct etnaviv_drm_private *priv,
453a8c21a54SThe etnaviv authors struct seq_file *m)
454a8c21a54SThe etnaviv authors {
455a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj;
456a8c21a54SThe etnaviv authors int count = 0;
457a8c21a54SThe etnaviv authors size_t size = 0;
458a8c21a54SThe etnaviv authors
459a8c21a54SThe etnaviv authors mutex_lock(&priv->gem_lock);
460a8c21a54SThe etnaviv authors list_for_each_entry(etnaviv_obj, &priv->gem_list, gem_node) {
461a8c21a54SThe etnaviv authors struct drm_gem_object *obj = &etnaviv_obj->base;
462a8c21a54SThe etnaviv authors
463a8c21a54SThe etnaviv authors seq_puts(m, " ");
464a8c21a54SThe etnaviv authors etnaviv_gem_describe(obj, m);
465a8c21a54SThe etnaviv authors count++;
466a8c21a54SThe etnaviv authors size += obj->size;
467a8c21a54SThe etnaviv authors }
468a8c21a54SThe etnaviv authors mutex_unlock(&priv->gem_lock);
469a8c21a54SThe etnaviv authors
470a8c21a54SThe etnaviv authors seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
471a8c21a54SThe etnaviv authors }
472a8c21a54SThe etnaviv authors #endif
473a8c21a54SThe etnaviv authors
etnaviv_gem_shmem_release(struct etnaviv_gem_object * etnaviv_obj)474a8c21a54SThe etnaviv authors static void etnaviv_gem_shmem_release(struct etnaviv_gem_object *etnaviv_obj)
475a8c21a54SThe etnaviv authors {
476a8c21a54SThe etnaviv authors vunmap(etnaviv_obj->vaddr);
477a8c21a54SThe etnaviv authors put_pages(etnaviv_obj);
478a8c21a54SThe etnaviv authors }
479a8c21a54SThe etnaviv authors
480a8c21a54SThe etnaviv authors static const struct etnaviv_gem_ops etnaviv_gem_shmem_ops = {
481a8c21a54SThe etnaviv authors .get_pages = etnaviv_gem_shmem_get_pages,
482a8c21a54SThe etnaviv authors .release = etnaviv_gem_shmem_release,
483a0a5ab3eSLucas Stach .vmap = etnaviv_gem_vmap_impl,
484a10e2bdeSLucas Stach .mmap = etnaviv_gem_mmap_obj,
485a8c21a54SThe etnaviv authors };
486a8c21a54SThe etnaviv authors
etnaviv_gem_free_object(struct drm_gem_object * obj)487a8c21a54SThe etnaviv authors void etnaviv_gem_free_object(struct drm_gem_object *obj)
488a8c21a54SThe etnaviv authors {
489a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
49051841752SLucas Stach struct etnaviv_drm_private *priv = obj->dev->dev_private;
491a8c21a54SThe etnaviv authors struct etnaviv_vram_mapping *mapping, *tmp;
492a8c21a54SThe etnaviv authors
493a8c21a54SThe etnaviv authors /* object should not be active */
494a8c21a54SThe etnaviv authors WARN_ON(is_active(etnaviv_obj));
495a8c21a54SThe etnaviv authors
49651841752SLucas Stach mutex_lock(&priv->gem_lock);
497a8c21a54SThe etnaviv authors list_del(&etnaviv_obj->gem_node);
49851841752SLucas Stach mutex_unlock(&priv->gem_lock);
499a8c21a54SThe etnaviv authors
500a8c21a54SThe etnaviv authors list_for_each_entry_safe(mapping, tmp, &etnaviv_obj->vram_list,
501a8c21a54SThe etnaviv authors obj_node) {
50227b67278SLucas Stach struct etnaviv_iommu_context *context = mapping->context;
503a8c21a54SThe etnaviv authors
504a8c21a54SThe etnaviv authors WARN_ON(mapping->use);
505a8c21a54SThe etnaviv authors
50611ad6a1fSLucas Stach if (context)
50727b67278SLucas Stach etnaviv_iommu_unmap_gem(context, mapping);
508a8c21a54SThe etnaviv authors
509a8c21a54SThe etnaviv authors list_del(&mapping->obj_node);
510a8c21a54SThe etnaviv authors kfree(mapping);
511a8c21a54SThe etnaviv authors }
512a8c21a54SThe etnaviv authors
513a8c21a54SThe etnaviv authors etnaviv_obj->ops->release(etnaviv_obj);
514a8c21a54SThe etnaviv authors drm_gem_object_release(obj);
515a8c21a54SThe etnaviv authors
516a8c21a54SThe etnaviv authors kfree(etnaviv_obj);
517a8c21a54SThe etnaviv authors }
518a8c21a54SThe etnaviv authors
etnaviv_gem_obj_add(struct drm_device * dev,struct drm_gem_object * obj)51954f09288SLucas Stach void etnaviv_gem_obj_add(struct drm_device *dev, struct drm_gem_object *obj)
520a8c21a54SThe etnaviv authors {
521a8c21a54SThe etnaviv authors struct etnaviv_drm_private *priv = dev->dev_private;
522a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
523a8c21a54SThe etnaviv authors
524a8c21a54SThe etnaviv authors mutex_lock(&priv->gem_lock);
525a8c21a54SThe etnaviv authors list_add_tail(&etnaviv_obj->gem_node, &priv->gem_list);
526a8c21a54SThe etnaviv authors mutex_unlock(&priv->gem_lock);
527a8c21a54SThe etnaviv authors }
528a8c21a54SThe etnaviv authors
529a7730627SThomas Zimmermann static const struct vm_operations_struct vm_ops = {
530a7730627SThomas Zimmermann .fault = etnaviv_gem_fault,
531a7730627SThomas Zimmermann .open = drm_gem_vm_open,
532a7730627SThomas Zimmermann .close = drm_gem_vm_close,
533a7730627SThomas Zimmermann };
534a7730627SThomas Zimmermann
535a7730627SThomas Zimmermann static const struct drm_gem_object_funcs etnaviv_gem_object_funcs = {
536a7730627SThomas Zimmermann .free = etnaviv_gem_free_object,
537a7730627SThomas Zimmermann .pin = etnaviv_gem_prime_pin,
538a7730627SThomas Zimmermann .unpin = etnaviv_gem_prime_unpin,
539a7730627SThomas Zimmermann .get_sg_table = etnaviv_gem_prime_get_sg_table,
540a7730627SThomas Zimmermann .vmap = etnaviv_gem_prime_vmap,
54181fd23e2SThomas Zimmermann .mmap = etnaviv_gem_mmap,
542a7730627SThomas Zimmermann .vm_ops = &vm_ops,
543a7730627SThomas Zimmermann };
544a7730627SThomas Zimmermann
etnaviv_gem_new_impl(struct drm_device * dev,u32 size,u32 flags,const struct etnaviv_gem_ops * ops,struct drm_gem_object ** obj)545a8c21a54SThe etnaviv authors static int etnaviv_gem_new_impl(struct drm_device *dev, u32 size, u32 flags,
546c6be8086SDaniel Vetter const struct etnaviv_gem_ops *ops, struct drm_gem_object **obj)
547a8c21a54SThe etnaviv authors {
548a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj;
549a8c21a54SThe etnaviv authors unsigned sz = sizeof(*etnaviv_obj);
550a8c21a54SThe etnaviv authors bool valid = true;
551a8c21a54SThe etnaviv authors
552a8c21a54SThe etnaviv authors /* validate flags */
553a8c21a54SThe etnaviv authors switch (flags & ETNA_BO_CACHE_MASK) {
554a8c21a54SThe etnaviv authors case ETNA_BO_UNCACHED:
555a8c21a54SThe etnaviv authors case ETNA_BO_CACHED:
556a8c21a54SThe etnaviv authors case ETNA_BO_WC:
557a8c21a54SThe etnaviv authors break;
558a8c21a54SThe etnaviv authors default:
559a8c21a54SThe etnaviv authors valid = false;
560a8c21a54SThe etnaviv authors }
561a8c21a54SThe etnaviv authors
562a8c21a54SThe etnaviv authors if (!valid) {
563a8c21a54SThe etnaviv authors dev_err(dev->dev, "invalid cache flag: %x\n",
564a8c21a54SThe etnaviv authors (flags & ETNA_BO_CACHE_MASK));
565a8c21a54SThe etnaviv authors return -EINVAL;
566a8c21a54SThe etnaviv authors }
567a8c21a54SThe etnaviv authors
568a8c21a54SThe etnaviv authors etnaviv_obj = kzalloc(sz, GFP_KERNEL);
569a8c21a54SThe etnaviv authors if (!etnaviv_obj)
570a8c21a54SThe etnaviv authors return -ENOMEM;
571a8c21a54SThe etnaviv authors
572a8c21a54SThe etnaviv authors etnaviv_obj->flags = flags;
573a8c21a54SThe etnaviv authors etnaviv_obj->ops = ops;
574a8c21a54SThe etnaviv authors
575a8c21a54SThe etnaviv authors mutex_init(&etnaviv_obj->lock);
576a8c21a54SThe etnaviv authors INIT_LIST_HEAD(&etnaviv_obj->vram_list);
577a8c21a54SThe etnaviv authors
578a8c21a54SThe etnaviv authors *obj = &etnaviv_obj->base;
579a7730627SThomas Zimmermann (*obj)->funcs = &etnaviv_gem_object_funcs;
580a8c21a54SThe etnaviv authors
581a8c21a54SThe etnaviv authors return 0;
582a8c21a54SThe etnaviv authors }
583a8c21a54SThe etnaviv authors
584cdd32563SLucas Stach /* convenience method to construct a GEM buffer object, and userspace handle */
etnaviv_gem_new_handle(struct drm_device * dev,struct drm_file * file,u32 size,u32 flags,u32 * handle)585cdd32563SLucas Stach int etnaviv_gem_new_handle(struct drm_device *dev, struct drm_file *file,
586cdd32563SLucas Stach u32 size, u32 flags, u32 *handle)
587a8c21a54SThe etnaviv authors {
588b72af445SLucas Stach struct etnaviv_drm_private *priv = dev->dev_private;
589a8c21a54SThe etnaviv authors struct drm_gem_object *obj = NULL;
590a8c21a54SThe etnaviv authors int ret;
591a8c21a54SThe etnaviv authors
592a8c21a54SThe etnaviv authors size = PAGE_ALIGN(size);
593a8c21a54SThe etnaviv authors
594c6be8086SDaniel Vetter ret = etnaviv_gem_new_impl(dev, size, flags,
595a8c21a54SThe etnaviv authors &etnaviv_gem_shmem_ops, &obj);
596a8c21a54SThe etnaviv authors if (ret)
597a8c21a54SThe etnaviv authors goto fail;
598a8c21a54SThe etnaviv authors
599d6a8743dSLucas Stach lockdep_set_class(&to_etnaviv_bo(obj)->lock, &etnaviv_shm_lock_class);
600d6a8743dSLucas Stach
601a8c21a54SThe etnaviv authors ret = drm_gem_object_init(dev, obj, size);
602a8c21a54SThe etnaviv authors if (ret)
603a8c21a54SThe etnaviv authors goto fail;
604a8c21a54SThe etnaviv authors
605fd2450a7SLucas Stach /*
606fd2450a7SLucas Stach * Our buffers are kept pinned, so allocating them from the MOVABLE
607fd2450a7SLucas Stach * zone is a really bad idea, and conflicts with CMA. See comments
608fd2450a7SLucas Stach * above new_inode() why this is required _and_ expected if you're
609fd2450a7SLucas Stach * going to pin these pages.
610fd2450a7SLucas Stach */
611b72af445SLucas Stach mapping_set_gfp_mask(obj->filp->f_mapping, priv->shm_gfp_mask);
612fd2450a7SLucas Stach
61354f09288SLucas Stach etnaviv_gem_obj_add(dev, obj);
614a8c21a54SThe etnaviv authors
615a8c21a54SThe etnaviv authors ret = drm_gem_handle_create(file, obj, handle);
616a8c21a54SThe etnaviv authors
617a8c21a54SThe etnaviv authors /* drop reference from allocate - handle holds it now */
618cdd32563SLucas Stach fail:
6196780bf32SEmil Velikov drm_gem_object_put(obj);
620a8c21a54SThe etnaviv authors
621a8c21a54SThe etnaviv authors return ret;
622a8c21a54SThe etnaviv authors }
623a8c21a54SThe etnaviv authors
etnaviv_gem_new_private(struct drm_device * dev,size_t size,u32 flags,const struct etnaviv_gem_ops * ops,struct etnaviv_gem_object ** res)624a8c21a54SThe etnaviv authors int etnaviv_gem_new_private(struct drm_device *dev, size_t size, u32 flags,
625c6be8086SDaniel Vetter const struct etnaviv_gem_ops *ops, struct etnaviv_gem_object **res)
626a8c21a54SThe etnaviv authors {
627a8c21a54SThe etnaviv authors struct drm_gem_object *obj;
628a8c21a54SThe etnaviv authors int ret;
629a8c21a54SThe etnaviv authors
630c6be8086SDaniel Vetter ret = etnaviv_gem_new_impl(dev, size, flags, ops, &obj);
631a8c21a54SThe etnaviv authors if (ret)
632a8c21a54SThe etnaviv authors return ret;
633a8c21a54SThe etnaviv authors
634a8c21a54SThe etnaviv authors drm_gem_private_object_init(dev, obj, size);
635a8c21a54SThe etnaviv authors
636a8c21a54SThe etnaviv authors *res = to_etnaviv_bo(obj);
637a8c21a54SThe etnaviv authors
638a8c21a54SThe etnaviv authors return 0;
639a8c21a54SThe etnaviv authors }
640a8c21a54SThe etnaviv authors
etnaviv_gem_userptr_get_pages(struct etnaviv_gem_object * etnaviv_obj)641a8c21a54SThe etnaviv authors static int etnaviv_gem_userptr_get_pages(struct etnaviv_gem_object *etnaviv_obj)
642a8c21a54SThe etnaviv authors {
643a8c21a54SThe etnaviv authors struct page **pvec = NULL;
644b2295c24SLucas Stach struct etnaviv_gem_userptr *userptr = &etnaviv_obj->userptr;
645b2295c24SLucas Stach int ret, pinned = 0, npages = etnaviv_obj->base.size >> PAGE_SHIFT;
6467d96eb6aSDavid Hildenbrand unsigned int gup_flags = FOLL_LONGTERM;
647a8c21a54SThe etnaviv authors
648da1c55f1SMichel Lespinasse might_lock_read(¤t->mm->mmap_lock);
649783c06cbSLucas Stach
650b2295c24SLucas Stach if (userptr->mm != current->mm)
651b2295c24SLucas Stach return -EPERM;
652b2295c24SLucas Stach
653b2295c24SLucas Stach pvec = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
654b2295c24SLucas Stach if (!pvec)
655b2295c24SLucas Stach return -ENOMEM;
656b2295c24SLucas Stach
6577d96eb6aSDavid Hildenbrand if (!userptr->ro)
6587d96eb6aSDavid Hildenbrand gup_flags |= FOLL_WRITE;
6597d96eb6aSDavid Hildenbrand
660b2295c24SLucas Stach do {
661b2295c24SLucas Stach unsigned num_pages = npages - pinned;
662b2295c24SLucas Stach uint64_t ptr = userptr->ptr + pinned * PAGE_SIZE;
663b2295c24SLucas Stach struct page **pages = pvec + pinned;
664b2295c24SLucas Stach
6657d96eb6aSDavid Hildenbrand ret = pin_user_pages_fast(ptr, num_pages, gup_flags, pages);
666b2295c24SLucas Stach if (ret < 0) {
66786824e60SJohn Hubbard unpin_user_pages(pvec, pinned);
668b2295c24SLucas Stach kvfree(pvec);
669a8c21a54SThe etnaviv authors return ret;
670a8c21a54SThe etnaviv authors }
671a8c21a54SThe etnaviv authors
672b2295c24SLucas Stach pinned += ret;
673a8c21a54SThe etnaviv authors
674b2295c24SLucas Stach } while (pinned < npages);
675a8c21a54SThe etnaviv authors
676a8c21a54SThe etnaviv authors etnaviv_obj->pages = pvec;
677b2295c24SLucas Stach
678a8c21a54SThe etnaviv authors return 0;
679a8c21a54SThe etnaviv authors }
680a8c21a54SThe etnaviv authors
etnaviv_gem_userptr_release(struct etnaviv_gem_object * etnaviv_obj)681a8c21a54SThe etnaviv authors static void etnaviv_gem_userptr_release(struct etnaviv_gem_object *etnaviv_obj)
682a8c21a54SThe etnaviv authors {
683a8c21a54SThe etnaviv authors if (etnaviv_obj->sgt) {
684a8c21a54SThe etnaviv authors etnaviv_gem_scatterlist_unmap(etnaviv_obj);
685a8c21a54SThe etnaviv authors sg_free_table(etnaviv_obj->sgt);
686a8c21a54SThe etnaviv authors kfree(etnaviv_obj->sgt);
687a8c21a54SThe etnaviv authors }
688a8c21a54SThe etnaviv authors if (etnaviv_obj->pages) {
689a8c21a54SThe etnaviv authors int npages = etnaviv_obj->base.size >> PAGE_SHIFT;
690a8c21a54SThe etnaviv authors
69186824e60SJohn Hubbard unpin_user_pages(etnaviv_obj->pages, npages);
6922098105eSMichal Hocko kvfree(etnaviv_obj->pages);
693a8c21a54SThe etnaviv authors }
694a8c21a54SThe etnaviv authors }
695a8c21a54SThe etnaviv authors
etnaviv_gem_userptr_mmap_obj(struct etnaviv_gem_object * etnaviv_obj,struct vm_area_struct * vma)696a10e2bdeSLucas Stach static int etnaviv_gem_userptr_mmap_obj(struct etnaviv_gem_object *etnaviv_obj,
697a10e2bdeSLucas Stach struct vm_area_struct *vma)
698a10e2bdeSLucas Stach {
699a10e2bdeSLucas Stach return -EINVAL;
700a10e2bdeSLucas Stach }
701a10e2bdeSLucas Stach
702a8c21a54SThe etnaviv authors static const struct etnaviv_gem_ops etnaviv_gem_userptr_ops = {
703a8c21a54SThe etnaviv authors .get_pages = etnaviv_gem_userptr_get_pages,
704a8c21a54SThe etnaviv authors .release = etnaviv_gem_userptr_release,
705a0a5ab3eSLucas Stach .vmap = etnaviv_gem_vmap_impl,
706a10e2bdeSLucas Stach .mmap = etnaviv_gem_userptr_mmap_obj,
707a8c21a54SThe etnaviv authors };
708a8c21a54SThe etnaviv authors
etnaviv_gem_new_userptr(struct drm_device * dev,struct drm_file * file,uintptr_t ptr,u32 size,u32 flags,u32 * handle)709a8c21a54SThe etnaviv authors int etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file,
710a8c21a54SThe etnaviv authors uintptr_t ptr, u32 size, u32 flags, u32 *handle)
711a8c21a54SThe etnaviv authors {
712a8c21a54SThe etnaviv authors struct etnaviv_gem_object *etnaviv_obj;
713a8c21a54SThe etnaviv authors int ret;
714a8c21a54SThe etnaviv authors
715c6be8086SDaniel Vetter ret = etnaviv_gem_new_private(dev, size, ETNA_BO_CACHED,
716a8c21a54SThe etnaviv authors &etnaviv_gem_userptr_ops, &etnaviv_obj);
717a8c21a54SThe etnaviv authors if (ret)
718a8c21a54SThe etnaviv authors return ret;
719a8c21a54SThe etnaviv authors
720d6a8743dSLucas Stach lockdep_set_class(&etnaviv_obj->lock, &etnaviv_userptr_lock_class);
721d6a8743dSLucas Stach
722a8c21a54SThe etnaviv authors etnaviv_obj->userptr.ptr = ptr;
723b2295c24SLucas Stach etnaviv_obj->userptr.mm = current->mm;
724a8c21a54SThe etnaviv authors etnaviv_obj->userptr.ro = !(flags & ETNA_USERPTR_WRITE);
725a8c21a54SThe etnaviv authors
72654f09288SLucas Stach etnaviv_gem_obj_add(dev, &etnaviv_obj->base);
727a8c21a54SThe etnaviv authors
728a8c21a54SThe etnaviv authors ret = drm_gem_handle_create(file, &etnaviv_obj->base, handle);
72954f09288SLucas Stach
730a8c21a54SThe etnaviv authors /* drop reference from allocate - handle holds it now */
7316780bf32SEmil Velikov drm_gem_object_put(&etnaviv_obj->base);
732a8c21a54SThe etnaviv authors return ret;
733a8c21a54SThe etnaviv authors }
734