blob: 09e2190a09ed1ac69fd976fcc07e825d88c94237 [file] [log] [blame]
Rob Clarkc8afe682013-06-26 12:44:06 -04001/*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
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.
8 *
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
12 * more details.
13 *
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/>.
16 */
17
18#include <linux/spinlock.h>
19#include <linux/shmem_fs.h>
Rob Clark05b84912013-09-28 11:28:35 -040020#include <linux/dma-buf.h>
Dan Williams01c8f1c2016-01-15 16:56:40 -080021#include <linux/pfn_t.h>
Rob Clarkc8afe682013-06-26 12:44:06 -040022
23#include "msm_drv.h"
Rob Clarkfde5de62016-03-15 15:35:08 -040024#include "msm_fence.h"
Rob Clarkc8afe682013-06-26 12:44:06 -040025#include "msm_gem.h"
Rob Clark7198e6b2013-07-19 12:59:32 -040026#include "msm_gpu.h"
Rob Clark871d8122013-11-16 12:56:06 -050027#include "msm_mmu.h"
Rob Clarkc8afe682013-06-26 12:44:06 -040028
Rob Clark871d8122013-11-16 12:56:06 -050029static dma_addr_t physaddr(struct drm_gem_object *obj)
30{
31 struct msm_gem_object *msm_obj = to_msm_bo(obj);
32 struct msm_drm_private *priv = obj->dev->dev_private;
33 return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) +
34 priv->vram.paddr;
35}
36
Rob Clark072f1f92015-03-03 15:04:25 -050037static bool use_pages(struct drm_gem_object *obj)
38{
39 struct msm_gem_object *msm_obj = to_msm_bo(obj);
40 return !msm_obj->vram_node;
41}
42
Rob Clark871d8122013-11-16 12:56:06 -050043/* allocate pages from VRAM carveout, used when no IOMMU: */
44static struct page **get_pages_vram(struct drm_gem_object *obj,
45 int npages)
46{
47 struct msm_gem_object *msm_obj = to_msm_bo(obj);
48 struct msm_drm_private *priv = obj->dev->dev_private;
49 dma_addr_t paddr;
50 struct page **p;
51 int ret, i;
52
53 p = drm_malloc_ab(npages, sizeof(struct page *));
54 if (!p)
55 return ERR_PTR(-ENOMEM);
56
57 ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node,
58 npages, 0, DRM_MM_SEARCH_DEFAULT);
59 if (ret) {
60 drm_free_large(p);
61 return ERR_PTR(ret);
62 }
63
64 paddr = physaddr(obj);
65 for (i = 0; i < npages; i++) {
66 p[i] = phys_to_page(paddr);
67 paddr += PAGE_SIZE;
68 }
69
70 return p;
71}
Rob Clarkc8afe682013-06-26 12:44:06 -040072
73/* called with dev->struct_mutex held */
74static struct page **get_pages(struct drm_gem_object *obj)
75{
76 struct msm_gem_object *msm_obj = to_msm_bo(obj);
77
78 if (!msm_obj->pages) {
79 struct drm_device *dev = obj->dev;
Rob Clark871d8122013-11-16 12:56:06 -050080 struct page **p;
Rob Clarkc8afe682013-06-26 12:44:06 -040081 int npages = obj->size >> PAGE_SHIFT;
82
Rob Clark072f1f92015-03-03 15:04:25 -050083 if (use_pages(obj))
David Herrmann0cdbe8a2014-05-25 12:59:47 +020084 p = drm_gem_get_pages(obj);
Rob Clark871d8122013-11-16 12:56:06 -050085 else
86 p = get_pages_vram(obj, npages);
87
Rob Clarkc8afe682013-06-26 12:44:06 -040088 if (IS_ERR(p)) {
89 dev_err(dev->dev, "could not get pages: %ld\n",
90 PTR_ERR(p));
91 return p;
92 }
93
94 msm_obj->sgt = drm_prime_pages_to_sg(p, npages);
Wei Yongjun1f70e072013-09-11 06:56:12 +080095 if (IS_ERR(msm_obj->sgt)) {
Rob Clarkc8afe682013-06-26 12:44:06 -040096 dev_err(dev->dev, "failed to allocate sgt\n");
Wei Yongjun1f70e072013-09-11 06:56:12 +080097 return ERR_CAST(msm_obj->sgt);
Rob Clarkc8afe682013-06-26 12:44:06 -040098 }
99
100 msm_obj->pages = p;
101
102 /* For non-cached buffers, ensure the new pages are clean
103 * because display controller, GPU, etc. are not coherent:
104 */
105 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
106 dma_map_sg(dev->dev, msm_obj->sgt->sgl,
107 msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
108 }
109
110 return msm_obj->pages;
111}
112
113static void put_pages(struct drm_gem_object *obj)
114{
115 struct msm_gem_object *msm_obj = to_msm_bo(obj);
116
117 if (msm_obj->pages) {
118 /* For non-cached buffers, ensure the new pages are clean
119 * because display controller, GPU, etc. are not coherent:
120 */
121 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
122 dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl,
123 msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
124 sg_free_table(msm_obj->sgt);
125 kfree(msm_obj->sgt);
126
Rob Clark072f1f92015-03-03 15:04:25 -0500127 if (use_pages(obj))
Rob Clark871d8122013-11-16 12:56:06 -0500128 drm_gem_put_pages(obj, msm_obj->pages, true, false);
Micah Richert1ffa2422014-04-09 14:11:31 -0700129 else {
Rob Clark871d8122013-11-16 12:56:06 -0500130 drm_mm_remove_node(msm_obj->vram_node);
Micah Richert1ffa2422014-04-09 14:11:31 -0700131 drm_free_large(msm_obj->pages);
132 }
Rob Clark871d8122013-11-16 12:56:06 -0500133
Rob Clarkc8afe682013-06-26 12:44:06 -0400134 msm_obj->pages = NULL;
135 }
136}
137
Rob Clark05b84912013-09-28 11:28:35 -0400138struct page **msm_gem_get_pages(struct drm_gem_object *obj)
139{
140 struct drm_device *dev = obj->dev;
141 struct page **p;
142 mutex_lock(&dev->struct_mutex);
143 p = get_pages(obj);
144 mutex_unlock(&dev->struct_mutex);
145 return p;
146}
147
148void msm_gem_put_pages(struct drm_gem_object *obj)
149{
150 /* when we start tracking the pin count, then do something here */
151}
152
Rob Clarkc8afe682013-06-26 12:44:06 -0400153int msm_gem_mmap_obj(struct drm_gem_object *obj,
154 struct vm_area_struct *vma)
155{
156 struct msm_gem_object *msm_obj = to_msm_bo(obj);
157
158 vma->vm_flags &= ~VM_PFNMAP;
159 vma->vm_flags |= VM_MIXEDMAP;
160
161 if (msm_obj->flags & MSM_BO_WC) {
162 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
163 } else if (msm_obj->flags & MSM_BO_UNCACHED) {
164 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
165 } else {
166 /*
167 * Shunt off cached objs to shmem file so they have their own
168 * address_space (so unmap_mapping_range does what we want,
169 * in particular in the case of mmap'd dmabufs)
170 */
171 fput(vma->vm_file);
172 get_file(obj->filp);
173 vma->vm_pgoff = 0;
174 vma->vm_file = obj->filp;
175
176 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
177 }
178
179 return 0;
180}
181
182int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
183{
184 int ret;
185
186 ret = drm_gem_mmap(filp, vma);
187 if (ret) {
188 DBG("mmap failed: %d", ret);
189 return ret;
190 }
191
192 return msm_gem_mmap_obj(vma->vm_private_data, vma);
193}
194
195int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
196{
197 struct drm_gem_object *obj = vma->vm_private_data;
Rob Clarkc8afe682013-06-26 12:44:06 -0400198 struct drm_device *dev = obj->dev;
199 struct page **pages;
200 unsigned long pfn;
201 pgoff_t pgoff;
202 int ret;
203
204 /* Make sure we don't parallel update on a fault, nor move or remove
205 * something from beneath our feet
206 */
207 ret = mutex_lock_interruptible(&dev->struct_mutex);
208 if (ret)
209 goto out;
210
211 /* make sure we have pages attached now */
212 pages = get_pages(obj);
213 if (IS_ERR(pages)) {
214 ret = PTR_ERR(pages);
215 goto out_unlock;
216 }
217
218 /* We don't use vmf->pgoff since that has the fake offset: */
219 pgoff = ((unsigned long)vmf->virtual_address -
220 vma->vm_start) >> PAGE_SHIFT;
221
Rob Clark871d8122013-11-16 12:56:06 -0500222 pfn = page_to_pfn(pages[pgoff]);
Rob Clarkc8afe682013-06-26 12:44:06 -0400223
224 VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
225 pfn, pfn << PAGE_SHIFT);
226
Dan Williams01c8f1c2016-01-15 16:56:40 -0800227 ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address,
228 __pfn_to_pfn_t(pfn, PFN_DEV));
Rob Clarkc8afe682013-06-26 12:44:06 -0400229
230out_unlock:
231 mutex_unlock(&dev->struct_mutex);
232out:
233 switch (ret) {
234 case -EAGAIN:
Rob Clarkc8afe682013-06-26 12:44:06 -0400235 case 0:
236 case -ERESTARTSYS:
237 case -EINTR:
Rob Clark505886d2013-10-20 11:57:52 -0400238 case -EBUSY:
239 /*
240 * EBUSY is ok: this just means that another thread
241 * already did the job.
242 */
Rob Clarkc8afe682013-06-26 12:44:06 -0400243 return VM_FAULT_NOPAGE;
244 case -ENOMEM:
245 return VM_FAULT_OOM;
246 default:
247 return VM_FAULT_SIGBUS;
248 }
249}
250
251/** get mmap offset */
252static uint64_t mmap_offset(struct drm_gem_object *obj)
253{
254 struct drm_device *dev = obj->dev;
255 int ret;
256
257 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
258
259 /* Make it mmapable */
260 ret = drm_gem_create_mmap_offset(obj);
261
262 if (ret) {
263 dev_err(dev->dev, "could not allocate mmap offset\n");
264 return 0;
265 }
266
267 return drm_vma_node_offset_addr(&obj->vma_node);
268}
269
270uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
271{
272 uint64_t offset;
273 mutex_lock(&obj->dev->struct_mutex);
274 offset = mmap_offset(obj);
275 mutex_unlock(&obj->dev->struct_mutex);
276 return offset;
277}
278
Rob Clarkc8afe682013-06-26 12:44:06 -0400279/* should be called under struct_mutex.. although it can be called
280 * from atomic context without struct_mutex to acquire an extra
281 * iova ref if you know one is already held.
282 *
283 * That means when I do eventually need to add support for unpinning
284 * the refcnt counter needs to be atomic_t.
285 */
286int msm_gem_get_iova_locked(struct drm_gem_object *obj, int id,
287 uint32_t *iova)
288{
289 struct msm_gem_object *msm_obj = to_msm_bo(obj);
290 int ret = 0;
291
292 if (!msm_obj->domain[id].iova) {
293 struct msm_drm_private *priv = obj->dev->dev_private;
Rob Clark871d8122013-11-16 12:56:06 -0500294 struct page **pages = get_pages(obj);
295
Rob Clarkc8afe682013-06-26 12:44:06 -0400296 if (IS_ERR(pages))
297 return PTR_ERR(pages);
Rob Clark871d8122013-11-16 12:56:06 -0500298
299 if (iommu_present(&platform_bus_type)) {
Rob Clark1c4997f2014-07-01 14:49:55 -0400300 struct msm_mmu *mmu = priv->mmus[id];
301 uint32_t offset;
302
303 if (WARN_ON(!mmu))
304 return -EINVAL;
305
306 offset = (uint32_t)mmap_offset(obj);
Rob Clark871d8122013-11-16 12:56:06 -0500307 ret = mmu->funcs->map(mmu, offset, msm_obj->sgt,
308 obj->size, IOMMU_READ | IOMMU_WRITE);
309 msm_obj->domain[id].iova = offset;
310 } else {
311 msm_obj->domain[id].iova = physaddr(obj);
312 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400313 }
314
315 if (!ret)
316 *iova = msm_obj->domain[id].iova;
317
318 return ret;
319}
320
Rob Clark2638d902014-11-08 09:13:37 -0500321/* get iova, taking a reference. Should have a matching put */
Rob Clarkc8afe682013-06-26 12:44:06 -0400322int msm_gem_get_iova(struct drm_gem_object *obj, int id, uint32_t *iova)
323{
Rob Clarkedd4fc62013-09-14 14:01:55 -0400324 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400325 int ret;
Rob Clarkedd4fc62013-09-14 14:01:55 -0400326
327 /* this is safe right now because we don't unmap until the
328 * bo is deleted:
329 */
330 if (msm_obj->domain[id].iova) {
331 *iova = msm_obj->domain[id].iova;
332 return 0;
333 }
334
Rob Clarkc8afe682013-06-26 12:44:06 -0400335 mutex_lock(&obj->dev->struct_mutex);
336 ret = msm_gem_get_iova_locked(obj, id, iova);
337 mutex_unlock(&obj->dev->struct_mutex);
338 return ret;
339}
340
Rob Clark2638d902014-11-08 09:13:37 -0500341/* get iova without taking a reference, used in places where you have
342 * already done a 'msm_gem_get_iova()'.
343 */
344uint32_t msm_gem_iova(struct drm_gem_object *obj, int id)
345{
346 struct msm_gem_object *msm_obj = to_msm_bo(obj);
347 WARN_ON(!msm_obj->domain[id].iova);
348 return msm_obj->domain[id].iova;
349}
350
Rob Clarkc8afe682013-06-26 12:44:06 -0400351void msm_gem_put_iova(struct drm_gem_object *obj, int id)
352{
353 // XXX TODO ..
354 // NOTE: probably don't need a _locked() version.. we wouldn't
355 // normally unmap here, but instead just mark that it could be
356 // unmapped (if the iova refcnt drops to zero), but then later
357 // if another _get_iova_locked() fails we can start unmapping
358 // things that are no longer needed..
359}
360
361int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
362 struct drm_mode_create_dumb *args)
363{
364 args->pitch = align_pitch(args->width, args->bpp);
365 args->size = PAGE_ALIGN(args->pitch * args->height);
366 return msm_gem_new_handle(dev, file, args->size,
367 MSM_BO_SCANOUT | MSM_BO_WC, &args->handle);
368}
369
Rob Clarkc8afe682013-06-26 12:44:06 -0400370int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
371 uint32_t handle, uint64_t *offset)
372{
373 struct drm_gem_object *obj;
374 int ret = 0;
375
376 /* GEM does all our handle to object mapping */
377 obj = drm_gem_object_lookup(dev, file, handle);
378 if (obj == NULL) {
379 ret = -ENOENT;
380 goto fail;
381 }
382
383 *offset = msm_gem_mmap_offset(obj);
384
385 drm_gem_object_unreference_unlocked(obj);
386
387fail:
388 return ret;
389}
390
391void *msm_gem_vaddr_locked(struct drm_gem_object *obj)
392{
393 struct msm_gem_object *msm_obj = to_msm_bo(obj);
394 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
395 if (!msm_obj->vaddr) {
396 struct page **pages = get_pages(obj);
397 if (IS_ERR(pages))
398 return ERR_CAST(pages);
399 msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
400 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
401 }
402 return msm_obj->vaddr;
403}
404
405void *msm_gem_vaddr(struct drm_gem_object *obj)
406{
407 void *ret;
408 mutex_lock(&obj->dev->struct_mutex);
409 ret = msm_gem_vaddr_locked(obj);
410 mutex_unlock(&obj->dev->struct_mutex);
411 return ret;
412}
413
Rob Clarkedd4fc62013-09-14 14:01:55 -0400414/* setup callback for when bo is no longer busy..
415 * TODO probably want to differentiate read vs write..
416 */
417int msm_gem_queue_inactive_cb(struct drm_gem_object *obj,
418 struct msm_fence_cb *cb)
Rob Clarkc8afe682013-06-26 12:44:06 -0400419{
Rob Clark7198e6b2013-07-19 12:59:32 -0400420 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clark69193e52014-11-07 18:10:04 -0500421 uint32_t fence = msm_gem_fence(msm_obj,
422 MSM_PREP_READ | MSM_PREP_WRITE);
423 return msm_queue_fence_cb(obj->dev, cb, fence);
Rob Clark7198e6b2013-07-19 12:59:32 -0400424}
425
426void msm_gem_move_to_active(struct drm_gem_object *obj,
Rob Clarkbf6811f2013-09-01 13:25:09 -0400427 struct msm_gpu *gpu, bool write, uint32_t fence)
Rob Clark7198e6b2013-07-19 12:59:32 -0400428{
429 struct msm_gem_object *msm_obj = to_msm_bo(obj);
430 msm_obj->gpu = gpu;
Rob Clarkbf6811f2013-09-01 13:25:09 -0400431 if (write)
432 msm_obj->write_fence = fence;
433 else
434 msm_obj->read_fence = fence;
Rob Clark7198e6b2013-07-19 12:59:32 -0400435 list_del_init(&msm_obj->mm_list);
436 list_add_tail(&msm_obj->mm_list, &gpu->active_list);
437}
438
439void msm_gem_move_to_inactive(struct drm_gem_object *obj)
440{
441 struct drm_device *dev = obj->dev;
442 struct msm_drm_private *priv = dev->dev_private;
443 struct msm_gem_object *msm_obj = to_msm_bo(obj);
444
445 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
446
447 msm_obj->gpu = NULL;
Rob Clarkbf6811f2013-09-01 13:25:09 -0400448 msm_obj->read_fence = 0;
449 msm_obj->write_fence = 0;
Rob Clark7198e6b2013-07-19 12:59:32 -0400450 list_del_init(&msm_obj->mm_list);
451 list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
Rob Clark7198e6b2013-07-19 12:59:32 -0400452}
453
Rob Clark56c2da82015-05-11 11:50:03 -0400454int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
Rob Clark7198e6b2013-07-19 12:59:32 -0400455{
456 struct drm_device *dev = obj->dev;
457 struct msm_gem_object *msm_obj = to_msm_bo(obj);
458 int ret = 0;
459
Rob Clarkf816f272013-09-11 17:34:07 -0400460 if (is_active(msm_obj)) {
Rob Clark69193e52014-11-07 18:10:04 -0500461 uint32_t fence = msm_gem_fence(msm_obj, op);
Rob Clarkf816f272013-09-11 17:34:07 -0400462
Rob Clarkf816f272013-09-11 17:34:07 -0400463 if (op & MSM_PREP_NOSYNC)
464 timeout = NULL;
465
Wentao Xu99fc1bc2015-06-22 11:53:42 -0400466 ret = msm_wait_fence(dev, fence, timeout, true);
Rob Clarkbf6811f2013-09-01 13:25:09 -0400467 }
Rob Clark7198e6b2013-07-19 12:59:32 -0400468
469 /* TODO cache maintenance */
470
471 return ret;
472}
473
474int msm_gem_cpu_fini(struct drm_gem_object *obj)
475{
476 /* TODO cache maintenance */
Rob Clarkc8afe682013-06-26 12:44:06 -0400477 return 0;
478}
479
480#ifdef CONFIG_DEBUG_FS
481void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
482{
483 struct drm_device *dev = obj->dev;
484 struct msm_gem_object *msm_obj = to_msm_bo(obj);
485 uint64_t off = drm_vma_node_start(&obj->vma_node);
486
487 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
Thierry Redingfc99f972015-04-09 16:39:51 +0200488 seq_printf(m, "%08x: %c(r=%u,w=%u) %2d (%2d) %08llx %p %zu\n",
Rob Clark7198e6b2013-07-19 12:59:32 -0400489 msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
Rob Clarkbf6811f2013-09-01 13:25:09 -0400490 msm_obj->read_fence, msm_obj->write_fence,
491 obj->name, obj->refcount.refcount.counter,
Rob Clarkc8afe682013-06-26 12:44:06 -0400492 off, msm_obj->vaddr, obj->size);
493}
494
495void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
496{
497 struct msm_gem_object *msm_obj;
498 int count = 0;
499 size_t size = 0;
500
501 list_for_each_entry(msm_obj, list, mm_list) {
502 struct drm_gem_object *obj = &msm_obj->base;
503 seq_printf(m, " ");
504 msm_gem_describe(obj, m);
505 count++;
506 size += obj->size;
507 }
508
509 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
510}
511#endif
512
513void msm_gem_free_object(struct drm_gem_object *obj)
514{
515 struct drm_device *dev = obj->dev;
Rob Clark871d8122013-11-16 12:56:06 -0500516 struct msm_drm_private *priv = obj->dev->dev_private;
Rob Clarkc8afe682013-06-26 12:44:06 -0400517 struct msm_gem_object *msm_obj = to_msm_bo(obj);
518 int id;
519
520 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
521
Rob Clark7198e6b2013-07-19 12:59:32 -0400522 /* object should not be on active list: */
523 WARN_ON(is_active(msm_obj));
524
Rob Clarkc8afe682013-06-26 12:44:06 -0400525 list_del(&msm_obj->mm_list);
526
527 for (id = 0; id < ARRAY_SIZE(msm_obj->domain); id++) {
Rob Clark871d8122013-11-16 12:56:06 -0500528 struct msm_mmu *mmu = priv->mmus[id];
529 if (mmu && msm_obj->domain[id].iova) {
Rob Clark257d06f2014-11-12 15:25:50 -0500530 uint32_t offset = msm_obj->domain[id].iova;
Rob Clark871d8122013-11-16 12:56:06 -0500531 mmu->funcs->unmap(mmu, offset, msm_obj->sgt, obj->size);
Rob Clarkc8afe682013-06-26 12:44:06 -0400532 }
533 }
534
Rob Clark05b84912013-09-28 11:28:35 -0400535 if (obj->import_attach) {
536 if (msm_obj->vaddr)
537 dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr);
Rob Clarkc8afe682013-06-26 12:44:06 -0400538
Rob Clark05b84912013-09-28 11:28:35 -0400539 /* Don't drop the pages for imported dmabuf, as they are not
540 * ours, just free the array we allocated:
541 */
542 if (msm_obj->pages)
543 drm_free_large(msm_obj->pages);
544
jilai wangf28730c2015-04-07 13:51:32 -0400545 drm_prime_gem_destroy(obj, msm_obj->sgt);
Rob Clark05b84912013-09-28 11:28:35 -0400546 } else {
Markus Elfring264f7d62014-11-25 14:30:28 +0100547 vunmap(msm_obj->vaddr);
Rob Clark05b84912013-09-28 11:28:35 -0400548 put_pages(obj);
549 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400550
Rob Clark7198e6b2013-07-19 12:59:32 -0400551 if (msm_obj->resv == &msm_obj->_resv)
552 reservation_object_fini(msm_obj->resv);
553
Rob Clarkc8afe682013-06-26 12:44:06 -0400554 drm_gem_object_release(obj);
555
556 kfree(msm_obj);
557}
558
559/* convenience method to construct a GEM buffer object, and userspace handle */
560int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
561 uint32_t size, uint32_t flags, uint32_t *handle)
562{
563 struct drm_gem_object *obj;
564 int ret;
565
566 ret = mutex_lock_interruptible(&dev->struct_mutex);
567 if (ret)
568 return ret;
569
570 obj = msm_gem_new(dev, size, flags);
571
572 mutex_unlock(&dev->struct_mutex);
573
574 if (IS_ERR(obj))
575 return PTR_ERR(obj);
576
577 ret = drm_gem_handle_create(file, obj, handle);
578
579 /* drop reference from allocate - handle holds it now */
580 drm_gem_object_unreference_unlocked(obj);
581
582 return ret;
583}
584
Rob Clark05b84912013-09-28 11:28:35 -0400585static int msm_gem_new_impl(struct drm_device *dev,
586 uint32_t size, uint32_t flags,
Rob Clark79f0e202016-03-16 12:40:35 -0400587 struct reservation_object *resv,
Rob Clark05b84912013-09-28 11:28:35 -0400588 struct drm_gem_object **obj)
Rob Clarkc8afe682013-06-26 12:44:06 -0400589{
590 struct msm_drm_private *priv = dev->dev_private;
591 struct msm_gem_object *msm_obj;
Rob Clark871d8122013-11-16 12:56:06 -0500592 unsigned sz;
Rob Clark072f1f92015-03-03 15:04:25 -0500593 bool use_vram = false;
Rob Clarkc8afe682013-06-26 12:44:06 -0400594
595 switch (flags & MSM_BO_CACHE_MASK) {
596 case MSM_BO_UNCACHED:
597 case MSM_BO_CACHED:
598 case MSM_BO_WC:
599 break;
600 default:
601 dev_err(dev->dev, "invalid cache flag: %x\n",
602 (flags & MSM_BO_CACHE_MASK));
Rob Clark05b84912013-09-28 11:28:35 -0400603 return -EINVAL;
Rob Clarkc8afe682013-06-26 12:44:06 -0400604 }
605
Rob Clark871d8122013-11-16 12:56:06 -0500606 if (!iommu_present(&platform_bus_type))
Rob Clark072f1f92015-03-03 15:04:25 -0500607 use_vram = true;
608 else if ((flags & MSM_BO_STOLEN) && priv->vram.size)
609 use_vram = true;
610
611 if (WARN_ON(use_vram && !priv->vram.size))
612 return -EINVAL;
613
614 sz = sizeof(*msm_obj);
615 if (use_vram)
Rob Clark871d8122013-11-16 12:56:06 -0500616 sz += sizeof(struct drm_mm_node);
617
618 msm_obj = kzalloc(sz, GFP_KERNEL);
Rob Clark05b84912013-09-28 11:28:35 -0400619 if (!msm_obj)
620 return -ENOMEM;
Rob Clarkc8afe682013-06-26 12:44:06 -0400621
Rob Clark072f1f92015-03-03 15:04:25 -0500622 if (use_vram)
Rob Clark871d8122013-11-16 12:56:06 -0500623 msm_obj->vram_node = (void *)&msm_obj[1];
624
Rob Clarkc8afe682013-06-26 12:44:06 -0400625 msm_obj->flags = flags;
626
Rob Clark79f0e202016-03-16 12:40:35 -0400627 if (resv) {
628 msm_obj->resv = resv;
629 } else {
630 msm_obj->resv = &msm_obj->_resv;
631 reservation_object_init(msm_obj->resv);
632 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400633
Rob Clark7198e6b2013-07-19 12:59:32 -0400634 INIT_LIST_HEAD(&msm_obj->submit_entry);
Rob Clarkc8afe682013-06-26 12:44:06 -0400635 list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
636
Rob Clark05b84912013-09-28 11:28:35 -0400637 *obj = &msm_obj->base;
638
639 return 0;
640}
641
642struct drm_gem_object *msm_gem_new(struct drm_device *dev,
643 uint32_t size, uint32_t flags)
644{
Rob Clark871d8122013-11-16 12:56:06 -0500645 struct drm_gem_object *obj = NULL;
Rob Clark05b84912013-09-28 11:28:35 -0400646 int ret;
647
648 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
649
650 size = PAGE_ALIGN(size);
651
Rob Clark79f0e202016-03-16 12:40:35 -0400652 ret = msm_gem_new_impl(dev, size, flags, NULL, &obj);
Rob Clark05b84912013-09-28 11:28:35 -0400653 if (ret)
654 goto fail;
655
Rob Clark072f1f92015-03-03 15:04:25 -0500656 if (use_pages(obj)) {
Rob Clark871d8122013-11-16 12:56:06 -0500657 ret = drm_gem_object_init(dev, obj, size);
658 if (ret)
659 goto fail;
660 } else {
661 drm_gem_private_object_init(dev, obj, size);
662 }
Rob Clark05b84912013-09-28 11:28:35 -0400663
664 return obj;
665
666fail:
667 if (obj)
Rob Clark9999f102014-02-04 14:17:32 -0500668 drm_gem_object_unreference(obj);
Rob Clark05b84912013-09-28 11:28:35 -0400669
670 return ERR_PTR(ret);
671}
672
673struct drm_gem_object *msm_gem_import(struct drm_device *dev,
Rob Clark79f0e202016-03-16 12:40:35 -0400674 struct dma_buf *dmabuf, struct sg_table *sgt)
Rob Clark05b84912013-09-28 11:28:35 -0400675{
676 struct msm_gem_object *msm_obj;
677 struct drm_gem_object *obj;
Rob Clark79f0e202016-03-16 12:40:35 -0400678 uint32_t size;
Rob Clark05b84912013-09-28 11:28:35 -0400679 int ret, npages;
680
Rob Clark871d8122013-11-16 12:56:06 -0500681 /* if we don't have IOMMU, don't bother pretending we can import: */
682 if (!iommu_present(&platform_bus_type)) {
683 dev_err(dev->dev, "cannot import without IOMMU\n");
684 return ERR_PTR(-EINVAL);
685 }
686
Rob Clark79f0e202016-03-16 12:40:35 -0400687 size = PAGE_ALIGN(dmabuf->size);
Rob Clark05b84912013-09-28 11:28:35 -0400688
Rob Clark79f0e202016-03-16 12:40:35 -0400689 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, dmabuf->resv, &obj);
Rob Clark05b84912013-09-28 11:28:35 -0400690 if (ret)
691 goto fail;
692
693 drm_gem_private_object_init(dev, obj, size);
694
695 npages = size / PAGE_SIZE;
696
697 msm_obj = to_msm_bo(obj);
698 msm_obj->sgt = sgt;
699 msm_obj->pages = drm_malloc_ab(npages, sizeof(struct page *));
700 if (!msm_obj->pages) {
701 ret = -ENOMEM;
702 goto fail;
703 }
704
705 ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages);
706 if (ret)
707 goto fail;
708
Rob Clarkc8afe682013-06-26 12:44:06 -0400709 return obj;
710
711fail:
712 if (obj)
713 drm_gem_object_unreference_unlocked(obj);
714
715 return ERR_PTR(ret);
716}