blob: 68e509b3b9e4d08730e3901f46a397519c33e77c [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
Chris Wilson4e64e552017-02-02 21:04:38 +000057 ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node, npages);
Rob Clark871d8122013-11-16 12:56:06 -050058 if (ret) {
59 drm_free_large(p);
60 return ERR_PTR(ret);
61 }
62
63 paddr = physaddr(obj);
64 for (i = 0; i < npages; i++) {
65 p[i] = phys_to_page(paddr);
66 paddr += PAGE_SIZE;
67 }
68
69 return p;
70}
Rob Clarkc8afe682013-06-26 12:44:06 -040071
72/* called with dev->struct_mutex held */
73static struct page **get_pages(struct drm_gem_object *obj)
74{
75 struct msm_gem_object *msm_obj = to_msm_bo(obj);
76
77 if (!msm_obj->pages) {
78 struct drm_device *dev = obj->dev;
Rob Clark871d8122013-11-16 12:56:06 -050079 struct page **p;
Rob Clarkc8afe682013-06-26 12:44:06 -040080 int npages = obj->size >> PAGE_SHIFT;
81
Rob Clark072f1f92015-03-03 15:04:25 -050082 if (use_pages(obj))
David Herrmann0cdbe8a2014-05-25 12:59:47 +020083 p = drm_gem_get_pages(obj);
Rob Clark871d8122013-11-16 12:56:06 -050084 else
85 p = get_pages_vram(obj, npages);
86
Rob Clarkc8afe682013-06-26 12:44:06 -040087 if (IS_ERR(p)) {
88 dev_err(dev->dev, "could not get pages: %ld\n",
89 PTR_ERR(p));
90 return p;
91 }
92
93 msm_obj->sgt = drm_prime_pages_to_sg(p, npages);
Wei Yongjun1f70e072013-09-11 06:56:12 +080094 if (IS_ERR(msm_obj->sgt)) {
Rob Clarkc8afe682013-06-26 12:44:06 -040095 dev_err(dev->dev, "failed to allocate sgt\n");
Wei Yongjun1f70e072013-09-11 06:56:12 +080096 return ERR_CAST(msm_obj->sgt);
Rob Clarkc8afe682013-06-26 12:44:06 -040097 }
98
99 msm_obj->pages = p;
100
101 /* For non-cached buffers, ensure the new pages are clean
102 * because display controller, GPU, etc. are not coherent:
103 */
104 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
105 dma_map_sg(dev->dev, msm_obj->sgt->sgl,
106 msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
107 }
108
109 return msm_obj->pages;
110}
111
112static void put_pages(struct drm_gem_object *obj)
113{
114 struct msm_gem_object *msm_obj = to_msm_bo(obj);
115
116 if (msm_obj->pages) {
117 /* For non-cached buffers, ensure the new pages are clean
118 * because display controller, GPU, etc. are not coherent:
119 */
120 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
121 dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl,
122 msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
123 sg_free_table(msm_obj->sgt);
124 kfree(msm_obj->sgt);
125
Rob Clark072f1f92015-03-03 15:04:25 -0500126 if (use_pages(obj))
Rob Clark871d8122013-11-16 12:56:06 -0500127 drm_gem_put_pages(obj, msm_obj->pages, true, false);
Micah Richert1ffa2422014-04-09 14:11:31 -0700128 else {
Rob Clark871d8122013-11-16 12:56:06 -0500129 drm_mm_remove_node(msm_obj->vram_node);
Micah Richert1ffa2422014-04-09 14:11:31 -0700130 drm_free_large(msm_obj->pages);
131 }
Rob Clark871d8122013-11-16 12:56:06 -0500132
Rob Clarkc8afe682013-06-26 12:44:06 -0400133 msm_obj->pages = NULL;
134 }
135}
136
Rob Clark05b84912013-09-28 11:28:35 -0400137struct page **msm_gem_get_pages(struct drm_gem_object *obj)
138{
139 struct drm_device *dev = obj->dev;
140 struct page **p;
141 mutex_lock(&dev->struct_mutex);
142 p = get_pages(obj);
143 mutex_unlock(&dev->struct_mutex);
144 return p;
145}
146
147void msm_gem_put_pages(struct drm_gem_object *obj)
148{
149 /* when we start tracking the pin count, then do something here */
150}
151
Rob Clarkc8afe682013-06-26 12:44:06 -0400152int msm_gem_mmap_obj(struct drm_gem_object *obj,
153 struct vm_area_struct *vma)
154{
155 struct msm_gem_object *msm_obj = to_msm_bo(obj);
156
157 vma->vm_flags &= ~VM_PFNMAP;
158 vma->vm_flags |= VM_MIXEDMAP;
159
160 if (msm_obj->flags & MSM_BO_WC) {
161 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
162 } else if (msm_obj->flags & MSM_BO_UNCACHED) {
163 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
164 } else {
165 /*
166 * Shunt off cached objs to shmem file so they have their own
167 * address_space (so unmap_mapping_range does what we want,
168 * in particular in the case of mmap'd dmabufs)
169 */
170 fput(vma->vm_file);
171 get_file(obj->filp);
172 vma->vm_pgoff = 0;
173 vma->vm_file = obj->filp;
174
175 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
176 }
177
178 return 0;
179}
180
181int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
182{
183 int ret;
184
185 ret = drm_gem_mmap(filp, vma);
186 if (ret) {
187 DBG("mmap failed: %d", ret);
188 return ret;
189 }
190
191 return msm_gem_mmap_obj(vma->vm_private_data, vma);
192}
193
Dave Jiang11bac802017-02-24 14:56:41 -0800194int msm_gem_fault(struct vm_fault *vmf)
Rob Clarkc8afe682013-06-26 12:44:06 -0400195{
Dave Jiang11bac802017-02-24 14:56:41 -0800196 struct vm_area_struct *vma = vmf->vma;
Rob Clarkc8afe682013-06-26 12:44:06 -0400197 struct drm_gem_object *obj = vma->vm_private_data;
Rob Clarkc8afe682013-06-26 12:44:06 -0400198 struct drm_device *dev = obj->dev;
Rob Clarkd78d3832016-08-22 15:28:38 -0400199 struct msm_drm_private *priv = dev->dev_private;
Rob Clarkc8afe682013-06-26 12:44:06 -0400200 struct page **pages;
201 unsigned long pfn;
202 pgoff_t pgoff;
203 int ret;
204
Rob Clarkd78d3832016-08-22 15:28:38 -0400205 /* This should only happen if userspace tries to pass a mmap'd
206 * but unfaulted gem bo vaddr into submit ioctl, triggering
207 * a page fault while struct_mutex is already held. This is
208 * not a valid use-case so just bail.
209 */
210 if (priv->struct_mutex_task == current)
211 return VM_FAULT_SIGBUS;
212
Rob Clarkc8afe682013-06-26 12:44:06 -0400213 /* Make sure we don't parallel update on a fault, nor move or remove
214 * something from beneath our feet
215 */
216 ret = mutex_lock_interruptible(&dev->struct_mutex);
217 if (ret)
218 goto out;
219
220 /* make sure we have pages attached now */
221 pages = get_pages(obj);
222 if (IS_ERR(pages)) {
223 ret = PTR_ERR(pages);
224 goto out_unlock;
225 }
226
227 /* We don't use vmf->pgoff since that has the fake offset: */
Jan Kara1a29d852016-12-14 15:07:01 -0800228 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
Rob Clarkc8afe682013-06-26 12:44:06 -0400229
Rob Clark871d8122013-11-16 12:56:06 -0500230 pfn = page_to_pfn(pages[pgoff]);
Rob Clarkc8afe682013-06-26 12:44:06 -0400231
Jan Kara1a29d852016-12-14 15:07:01 -0800232 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
Rob Clarkc8afe682013-06-26 12:44:06 -0400233 pfn, pfn << PAGE_SHIFT);
234
Jan Kara1a29d852016-12-14 15:07:01 -0800235 ret = vm_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV));
Rob Clarkc8afe682013-06-26 12:44:06 -0400236
237out_unlock:
238 mutex_unlock(&dev->struct_mutex);
239out:
240 switch (ret) {
241 case -EAGAIN:
Rob Clarkc8afe682013-06-26 12:44:06 -0400242 case 0:
243 case -ERESTARTSYS:
244 case -EINTR:
Rob Clark505886d2013-10-20 11:57:52 -0400245 case -EBUSY:
246 /*
247 * EBUSY is ok: this just means that another thread
248 * already did the job.
249 */
Rob Clarkc8afe682013-06-26 12:44:06 -0400250 return VM_FAULT_NOPAGE;
251 case -ENOMEM:
252 return VM_FAULT_OOM;
253 default:
254 return VM_FAULT_SIGBUS;
255 }
256}
257
258/** get mmap offset */
259static uint64_t mmap_offset(struct drm_gem_object *obj)
260{
261 struct drm_device *dev = obj->dev;
262 int ret;
263
264 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
265
266 /* Make it mmapable */
267 ret = drm_gem_create_mmap_offset(obj);
268
269 if (ret) {
270 dev_err(dev->dev, "could not allocate mmap offset\n");
271 return 0;
272 }
273
274 return drm_vma_node_offset_addr(&obj->vma_node);
275}
276
277uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
278{
279 uint64_t offset;
280 mutex_lock(&obj->dev->struct_mutex);
281 offset = mmap_offset(obj);
282 mutex_unlock(&obj->dev->struct_mutex);
283 return offset;
284}
285
Rob Clark4fe5f652016-06-01 11:38:28 -0400286static void
287put_iova(struct drm_gem_object *obj)
288{
289 struct drm_device *dev = obj->dev;
290 struct msm_drm_private *priv = obj->dev->dev_private;
291 struct msm_gem_object *msm_obj = to_msm_bo(obj);
292 int id;
293
294 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
295
296 for (id = 0; id < ARRAY_SIZE(msm_obj->domain); id++) {
Rob Clarkde85d2b2017-01-12 17:41:44 -0500297 if (!priv->aspace[id])
298 continue;
Rob Clark667ce332016-09-28 19:58:32 -0400299 msm_gem_unmap_vma(priv->aspace[id],
300 &msm_obj->domain[id], msm_obj->sgt);
Rob Clark4fe5f652016-06-01 11:38:28 -0400301 }
302}
303
Rob Clarkc8afe682013-06-26 12:44:06 -0400304/* should be called under struct_mutex.. although it can be called
305 * from atomic context without struct_mutex to acquire an extra
306 * iova ref if you know one is already held.
307 *
308 * That means when I do eventually need to add support for unpinning
309 * the refcnt counter needs to be atomic_t.
310 */
311int msm_gem_get_iova_locked(struct drm_gem_object *obj, int id,
Rob Clark78babc12016-11-11 12:06:46 -0500312 uint64_t *iova)
Rob Clarkc8afe682013-06-26 12:44:06 -0400313{
314 struct msm_gem_object *msm_obj = to_msm_bo(obj);
315 int ret = 0;
316
317 if (!msm_obj->domain[id].iova) {
318 struct msm_drm_private *priv = obj->dev->dev_private;
Rob Clark871d8122013-11-16 12:56:06 -0500319 struct page **pages = get_pages(obj);
320
Rob Clarkc8afe682013-06-26 12:44:06 -0400321 if (IS_ERR(pages))
322 return PTR_ERR(pages);
Rob Clark871d8122013-11-16 12:56:06 -0500323
324 if (iommu_present(&platform_bus_type)) {
Rob Clark667ce332016-09-28 19:58:32 -0400325 ret = msm_gem_map_vma(priv->aspace[id], &msm_obj->domain[id],
326 msm_obj->sgt, obj->size >> PAGE_SHIFT);
Rob Clark871d8122013-11-16 12:56:06 -0500327 } else {
328 msm_obj->domain[id].iova = physaddr(obj);
329 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400330 }
331
332 if (!ret)
333 *iova = msm_obj->domain[id].iova;
334
335 return ret;
336}
337
Rob Clark2638d902014-11-08 09:13:37 -0500338/* get iova, taking a reference. Should have a matching put */
Rob Clark78babc12016-11-11 12:06:46 -0500339int msm_gem_get_iova(struct drm_gem_object *obj, int id, uint64_t *iova)
Rob Clarkc8afe682013-06-26 12:44:06 -0400340{
Rob Clarkedd4fc62013-09-14 14:01:55 -0400341 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400342 int ret;
Rob Clarkedd4fc62013-09-14 14:01:55 -0400343
344 /* this is safe right now because we don't unmap until the
345 * bo is deleted:
346 */
347 if (msm_obj->domain[id].iova) {
348 *iova = msm_obj->domain[id].iova;
349 return 0;
350 }
351
Rob Clarkc8afe682013-06-26 12:44:06 -0400352 mutex_lock(&obj->dev->struct_mutex);
353 ret = msm_gem_get_iova_locked(obj, id, iova);
354 mutex_unlock(&obj->dev->struct_mutex);
355 return ret;
356}
357
Rob Clark2638d902014-11-08 09:13:37 -0500358/* get iova without taking a reference, used in places where you have
359 * already done a 'msm_gem_get_iova()'.
360 */
Rob Clark78babc12016-11-11 12:06:46 -0500361uint64_t msm_gem_iova(struct drm_gem_object *obj, int id)
Rob Clark2638d902014-11-08 09:13:37 -0500362{
363 struct msm_gem_object *msm_obj = to_msm_bo(obj);
364 WARN_ON(!msm_obj->domain[id].iova);
365 return msm_obj->domain[id].iova;
366}
367
Rob Clarkc8afe682013-06-26 12:44:06 -0400368void msm_gem_put_iova(struct drm_gem_object *obj, int id)
369{
370 // XXX TODO ..
371 // NOTE: probably don't need a _locked() version.. we wouldn't
372 // normally unmap here, but instead just mark that it could be
373 // unmapped (if the iova refcnt drops to zero), but then later
374 // if another _get_iova_locked() fails we can start unmapping
375 // things that are no longer needed..
376}
377
378int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
379 struct drm_mode_create_dumb *args)
380{
381 args->pitch = align_pitch(args->width, args->bpp);
382 args->size = PAGE_ALIGN(args->pitch * args->height);
383 return msm_gem_new_handle(dev, file, args->size,
384 MSM_BO_SCANOUT | MSM_BO_WC, &args->handle);
385}
386
Rob Clarkc8afe682013-06-26 12:44:06 -0400387int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
388 uint32_t handle, uint64_t *offset)
389{
390 struct drm_gem_object *obj;
391 int ret = 0;
392
393 /* GEM does all our handle to object mapping */
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100394 obj = drm_gem_object_lookup(file, handle);
Rob Clarkc8afe682013-06-26 12:44:06 -0400395 if (obj == NULL) {
396 ret = -ENOENT;
397 goto fail;
398 }
399
400 *offset = msm_gem_mmap_offset(obj);
401
402 drm_gem_object_unreference_unlocked(obj);
403
404fail:
405 return ret;
406}
407
Rob Clark18f23042016-05-26 16:24:35 -0400408void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj)
Rob Clarkc8afe682013-06-26 12:44:06 -0400409{
410 struct msm_gem_object *msm_obj = to_msm_bo(obj);
411 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
412 if (!msm_obj->vaddr) {
413 struct page **pages = get_pages(obj);
414 if (IS_ERR(pages))
415 return ERR_CAST(pages);
416 msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
417 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
Rob Clark69a834c2016-05-24 18:29:38 -0400418 if (msm_obj->vaddr == NULL)
419 return ERR_PTR(-ENOMEM);
Rob Clarkc8afe682013-06-26 12:44:06 -0400420 }
Rob Clarke1e9db22016-05-27 11:16:28 -0400421 msm_obj->vmap_count++;
Rob Clarkc8afe682013-06-26 12:44:06 -0400422 return msm_obj->vaddr;
423}
424
Rob Clark18f23042016-05-26 16:24:35 -0400425void *msm_gem_get_vaddr(struct drm_gem_object *obj)
Rob Clarkc8afe682013-06-26 12:44:06 -0400426{
427 void *ret;
428 mutex_lock(&obj->dev->struct_mutex);
Rob Clark18f23042016-05-26 16:24:35 -0400429 ret = msm_gem_get_vaddr_locked(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400430 mutex_unlock(&obj->dev->struct_mutex);
431 return ret;
432}
433
Rob Clark18f23042016-05-26 16:24:35 -0400434void msm_gem_put_vaddr_locked(struct drm_gem_object *obj)
435{
Rob Clarke1e9db22016-05-27 11:16:28 -0400436 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clark18f23042016-05-26 16:24:35 -0400437 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
Rob Clarke1e9db22016-05-27 11:16:28 -0400438 WARN_ON(msm_obj->vmap_count < 1);
439 msm_obj->vmap_count--;
Rob Clark18f23042016-05-26 16:24:35 -0400440}
441
442void msm_gem_put_vaddr(struct drm_gem_object *obj)
443{
Rob Clarke1e9db22016-05-27 11:16:28 -0400444 mutex_lock(&obj->dev->struct_mutex);
445 msm_gem_put_vaddr_locked(obj);
446 mutex_unlock(&obj->dev->struct_mutex);
Rob Clark18f23042016-05-26 16:24:35 -0400447}
448
Rob Clark4cd33c42016-05-17 15:44:49 -0400449/* Update madvise status, returns true if not purged, else
450 * false or -errno.
451 */
452int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv)
453{
454 struct msm_gem_object *msm_obj = to_msm_bo(obj);
455
456 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
457
458 if (msm_obj->madv != __MSM_MADV_PURGED)
459 msm_obj->madv = madv;
460
461 return (msm_obj->madv != __MSM_MADV_PURGED);
462}
463
Rob Clark68209392016-05-17 16:19:32 -0400464void msm_gem_purge(struct drm_gem_object *obj)
465{
466 struct drm_device *dev = obj->dev;
467 struct msm_gem_object *msm_obj = to_msm_bo(obj);
468
469 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
470 WARN_ON(!is_purgeable(msm_obj));
471 WARN_ON(obj->import_attach);
472
473 put_iova(obj);
474
Rob Clarke1e9db22016-05-27 11:16:28 -0400475 msm_gem_vunmap(obj);
Rob Clark68209392016-05-17 16:19:32 -0400476
477 put_pages(obj);
478
479 msm_obj->madv = __MSM_MADV_PURGED;
480
481 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
482 drm_gem_free_mmap_offset(obj);
483
484 /* Our goal here is to return as much of the memory as
485 * is possible back to the system as we are called from OOM.
486 * To do this we must instruct the shmfs to drop all of its
487 * backing pages, *now*.
488 */
489 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
490
491 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
492 0, (loff_t)-1);
493}
494
Rob Clarke1e9db22016-05-27 11:16:28 -0400495void msm_gem_vunmap(struct drm_gem_object *obj)
496{
497 struct msm_gem_object *msm_obj = to_msm_bo(obj);
498
499 if (!msm_obj->vaddr || WARN_ON(!is_vunmapable(msm_obj)))
500 return;
501
502 vunmap(msm_obj->vaddr);
503 msm_obj->vaddr = NULL;
504}
505
Rob Clarkb6295f92016-03-15 18:26:28 -0400506/* must be called before _move_to_active().. */
507int msm_gem_sync_object(struct drm_gem_object *obj,
508 struct msm_fence_context *fctx, bool exclusive)
509{
510 struct msm_gem_object *msm_obj = to_msm_bo(obj);
511 struct reservation_object_list *fobj;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100512 struct dma_fence *fence;
Rob Clarkb6295f92016-03-15 18:26:28 -0400513 int i, ret;
514
515 if (!exclusive) {
516 /* NOTE: _reserve_shared() must happen before _add_shared_fence(),
517 * which makes this a slightly strange place to call it. OTOH this
518 * is a convenient can-fail point to hook it in. (And similar to
519 * how etnaviv and nouveau handle this.)
520 */
521 ret = reservation_object_reserve_shared(msm_obj->resv);
522 if (ret)
523 return ret;
524 }
525
526 fobj = reservation_object_get_list(msm_obj->resv);
527 if (!fobj || (fobj->shared_count == 0)) {
528 fence = reservation_object_get_excl(msm_obj->resv);
529 /* don't need to wait on our own fences, since ring is fifo */
530 if (fence && (fence->context != fctx->context)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100531 ret = dma_fence_wait(fence, true);
Rob Clarkb6295f92016-03-15 18:26:28 -0400532 if (ret)
533 return ret;
534 }
535 }
536
537 if (!exclusive || !fobj)
538 return 0;
539
540 for (i = 0; i < fobj->shared_count; i++) {
541 fence = rcu_dereference_protected(fobj->shared[i],
542 reservation_object_held(msm_obj->resv));
543 if (fence->context != fctx->context) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100544 ret = dma_fence_wait(fence, true);
Rob Clarkb6295f92016-03-15 18:26:28 -0400545 if (ret)
546 return ret;
547 }
548 }
549
550 return 0;
551}
552
Rob Clark7198e6b2013-07-19 12:59:32 -0400553void msm_gem_move_to_active(struct drm_gem_object *obj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100554 struct msm_gpu *gpu, bool exclusive, struct dma_fence *fence)
Rob Clark7198e6b2013-07-19 12:59:32 -0400555{
556 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clark4cd33c42016-05-17 15:44:49 -0400557 WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED);
Rob Clark7198e6b2013-07-19 12:59:32 -0400558 msm_obj->gpu = gpu;
Rob Clarkb6295f92016-03-15 18:26:28 -0400559 if (exclusive)
560 reservation_object_add_excl_fence(msm_obj->resv, fence);
Rob Clarkbf6811f2013-09-01 13:25:09 -0400561 else
Rob Clarkb6295f92016-03-15 18:26:28 -0400562 reservation_object_add_shared_fence(msm_obj->resv, fence);
Rob Clark7198e6b2013-07-19 12:59:32 -0400563 list_del_init(&msm_obj->mm_list);
564 list_add_tail(&msm_obj->mm_list, &gpu->active_list);
565}
566
567void msm_gem_move_to_inactive(struct drm_gem_object *obj)
568{
569 struct drm_device *dev = obj->dev;
570 struct msm_drm_private *priv = dev->dev_private;
571 struct msm_gem_object *msm_obj = to_msm_bo(obj);
572
573 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
574
575 msm_obj->gpu = NULL;
Rob Clark7198e6b2013-07-19 12:59:32 -0400576 list_del_init(&msm_obj->mm_list);
577 list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
Rob Clark7198e6b2013-07-19 12:59:32 -0400578}
579
Rob Clarkba00c3f2016-03-16 18:18:17 -0400580int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
581{
Rob Clarkb6295f92016-03-15 18:26:28 -0400582 struct msm_gem_object *msm_obj = to_msm_bo(obj);
583 bool write = !!(op & MSM_PREP_WRITE);
Chris Wilsonf755e222016-08-29 08:08:26 +0100584 unsigned long remain =
585 op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout);
586 long ret;
Rob Clarkb6295f92016-03-15 18:26:28 -0400587
Chris Wilsonf755e222016-08-29 08:08:26 +0100588 ret = reservation_object_wait_timeout_rcu(msm_obj->resv, write,
589 true, remain);
590 if (ret == 0)
591 return remain == 0 ? -EBUSY : -ETIMEDOUT;
592 else if (ret < 0)
593 return ret;
Rob Clarkba00c3f2016-03-16 18:18:17 -0400594
Rob Clark7198e6b2013-07-19 12:59:32 -0400595 /* TODO cache maintenance */
596
Rob Clarkb6295f92016-03-15 18:26:28 -0400597 return 0;
Rob Clark7198e6b2013-07-19 12:59:32 -0400598}
599
600int msm_gem_cpu_fini(struct drm_gem_object *obj)
601{
602 /* TODO cache maintenance */
Rob Clarkc8afe682013-06-26 12:44:06 -0400603 return 0;
604}
605
606#ifdef CONFIG_DEBUG_FS
Chris Wilsonf54d1862016-10-25 13:00:45 +0100607static void describe_fence(struct dma_fence *fence, const char *type,
Rob Clarkb6295f92016-03-15 18:26:28 -0400608 struct seq_file *m)
609{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100610 if (!dma_fence_is_signaled(fence))
Rob Clarkb6295f92016-03-15 18:26:28 -0400611 seq_printf(m, "\t%9s: %s %s seq %u\n", type,
612 fence->ops->get_driver_name(fence),
613 fence->ops->get_timeline_name(fence),
614 fence->seqno);
615}
616
Rob Clarkc8afe682013-06-26 12:44:06 -0400617void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
618{
Rob Clarkc8afe682013-06-26 12:44:06 -0400619 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clarkb6295f92016-03-15 18:26:28 -0400620 struct reservation_object *robj = msm_obj->resv;
621 struct reservation_object_list *fobj;
Rob Clark667ce332016-09-28 19:58:32 -0400622 struct msm_drm_private *priv = obj->dev->dev_private;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100623 struct dma_fence *fence;
Rob Clarkc8afe682013-06-26 12:44:06 -0400624 uint64_t off = drm_vma_node_start(&obj->vma_node);
Rob Clark4cd33c42016-05-17 15:44:49 -0400625 const char *madv;
Rob Clark667ce332016-09-28 19:58:32 -0400626 unsigned id;
Rob Clarkc8afe682013-06-26 12:44:06 -0400627
Rob Clarkb6295f92016-03-15 18:26:28 -0400628 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
629
Rob Clark4cd33c42016-05-17 15:44:49 -0400630 switch (msm_obj->madv) {
631 case __MSM_MADV_PURGED:
632 madv = " purged";
633 break;
634 case MSM_MADV_DONTNEED:
635 madv = " purgeable";
636 break;
637 case MSM_MADV_WILLNEED:
638 default:
639 madv = "";
640 break;
641 }
642
Rob Clark667ce332016-09-28 19:58:32 -0400643 seq_printf(m, "%08x: %c %2d (%2d) %08llx %p\t",
Rob Clark7198e6b2013-07-19 12:59:32 -0400644 msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100645 obj->name, kref_read(&obj->refcount),
Rob Clark667ce332016-09-28 19:58:32 -0400646 off, msm_obj->vaddr);
647
648 for (id = 0; id < priv->num_aspaces; id++)
649 seq_printf(m, " %08llx", msm_obj->domain[id].iova);
650
651 seq_printf(m, " %zu%s\n", obj->size, madv);
Rob Clarkb6295f92016-03-15 18:26:28 -0400652
653 rcu_read_lock();
654 fobj = rcu_dereference(robj->fence);
655 if (fobj) {
656 unsigned int i, shared_count = fobj->shared_count;
657
658 for (i = 0; i < shared_count; i++) {
659 fence = rcu_dereference(fobj->shared[i]);
660 describe_fence(fence, "Shared", m);
661 }
662 }
663
664 fence = rcu_dereference(robj->fence_excl);
665 if (fence)
666 describe_fence(fence, "Exclusive", m);
667 rcu_read_unlock();
Rob Clarkc8afe682013-06-26 12:44:06 -0400668}
669
670void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
671{
672 struct msm_gem_object *msm_obj;
673 int count = 0;
674 size_t size = 0;
675
676 list_for_each_entry(msm_obj, list, mm_list) {
677 struct drm_gem_object *obj = &msm_obj->base;
678 seq_printf(m, " ");
679 msm_gem_describe(obj, m);
680 count++;
681 size += obj->size;
682 }
683
684 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
685}
686#endif
687
688void msm_gem_free_object(struct drm_gem_object *obj)
689{
690 struct drm_device *dev = obj->dev;
691 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400692
693 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
694
Rob Clark7198e6b2013-07-19 12:59:32 -0400695 /* object should not be on active list: */
696 WARN_ON(is_active(msm_obj));
697
Rob Clarkc8afe682013-06-26 12:44:06 -0400698 list_del(&msm_obj->mm_list);
699
Rob Clark4fe5f652016-06-01 11:38:28 -0400700 put_iova(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400701
Rob Clark05b84912013-09-28 11:28:35 -0400702 if (obj->import_attach) {
703 if (msm_obj->vaddr)
704 dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr);
Rob Clarkc8afe682013-06-26 12:44:06 -0400705
Rob Clark05b84912013-09-28 11:28:35 -0400706 /* Don't drop the pages for imported dmabuf, as they are not
707 * ours, just free the array we allocated:
708 */
709 if (msm_obj->pages)
710 drm_free_large(msm_obj->pages);
711
jilai wangf28730c2015-04-07 13:51:32 -0400712 drm_prime_gem_destroy(obj, msm_obj->sgt);
Rob Clark05b84912013-09-28 11:28:35 -0400713 } else {
Rob Clarke1e9db22016-05-27 11:16:28 -0400714 msm_gem_vunmap(obj);
Rob Clark05b84912013-09-28 11:28:35 -0400715 put_pages(obj);
716 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400717
Rob Clark7198e6b2013-07-19 12:59:32 -0400718 if (msm_obj->resv == &msm_obj->_resv)
719 reservation_object_fini(msm_obj->resv);
720
Rob Clarkc8afe682013-06-26 12:44:06 -0400721 drm_gem_object_release(obj);
722
723 kfree(msm_obj);
724}
725
726/* convenience method to construct a GEM buffer object, and userspace handle */
727int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
728 uint32_t size, uint32_t flags, uint32_t *handle)
729{
730 struct drm_gem_object *obj;
731 int ret;
732
733 ret = mutex_lock_interruptible(&dev->struct_mutex);
734 if (ret)
735 return ret;
736
737 obj = msm_gem_new(dev, size, flags);
738
739 mutex_unlock(&dev->struct_mutex);
740
741 if (IS_ERR(obj))
742 return PTR_ERR(obj);
743
744 ret = drm_gem_handle_create(file, obj, handle);
745
746 /* drop reference from allocate - handle holds it now */
747 drm_gem_object_unreference_unlocked(obj);
748
749 return ret;
750}
751
Rob Clark05b84912013-09-28 11:28:35 -0400752static int msm_gem_new_impl(struct drm_device *dev,
753 uint32_t size, uint32_t flags,
Rob Clark79f0e202016-03-16 12:40:35 -0400754 struct reservation_object *resv,
Rob Clark05b84912013-09-28 11:28:35 -0400755 struct drm_gem_object **obj)
Rob Clarkc8afe682013-06-26 12:44:06 -0400756{
757 struct msm_drm_private *priv = dev->dev_private;
758 struct msm_gem_object *msm_obj;
Rob Clark072f1f92015-03-03 15:04:25 -0500759 bool use_vram = false;
Rob Clarkc8afe682013-06-26 12:44:06 -0400760
761 switch (flags & MSM_BO_CACHE_MASK) {
762 case MSM_BO_UNCACHED:
763 case MSM_BO_CACHED:
764 case MSM_BO_WC:
765 break;
766 default:
767 dev_err(dev->dev, "invalid cache flag: %x\n",
768 (flags & MSM_BO_CACHE_MASK));
Rob Clark05b84912013-09-28 11:28:35 -0400769 return -EINVAL;
Rob Clarkc8afe682013-06-26 12:44:06 -0400770 }
771
Rob Clark871d8122013-11-16 12:56:06 -0500772 if (!iommu_present(&platform_bus_type))
Rob Clark072f1f92015-03-03 15:04:25 -0500773 use_vram = true;
774 else if ((flags & MSM_BO_STOLEN) && priv->vram.size)
775 use_vram = true;
776
777 if (WARN_ON(use_vram && !priv->vram.size))
778 return -EINVAL;
779
Rob Clark667ce332016-09-28 19:58:32 -0400780 msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL);
Rob Clark05b84912013-09-28 11:28:35 -0400781 if (!msm_obj)
782 return -ENOMEM;
Rob Clarkc8afe682013-06-26 12:44:06 -0400783
Rob Clark072f1f92015-03-03 15:04:25 -0500784 if (use_vram)
Rob Clark667ce332016-09-28 19:58:32 -0400785 msm_obj->vram_node = &msm_obj->domain[0].node;
Rob Clark871d8122013-11-16 12:56:06 -0500786
Rob Clarkc8afe682013-06-26 12:44:06 -0400787 msm_obj->flags = flags;
Rob Clark4cd33c42016-05-17 15:44:49 -0400788 msm_obj->madv = MSM_MADV_WILLNEED;
Rob Clarkc8afe682013-06-26 12:44:06 -0400789
Rob Clark79f0e202016-03-16 12:40:35 -0400790 if (resv) {
791 msm_obj->resv = resv;
792 } else {
793 msm_obj->resv = &msm_obj->_resv;
794 reservation_object_init(msm_obj->resv);
795 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400796
Rob Clark7198e6b2013-07-19 12:59:32 -0400797 INIT_LIST_HEAD(&msm_obj->submit_entry);
Rob Clarkc8afe682013-06-26 12:44:06 -0400798 list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
799
Rob Clark05b84912013-09-28 11:28:35 -0400800 *obj = &msm_obj->base;
801
802 return 0;
803}
804
805struct drm_gem_object *msm_gem_new(struct drm_device *dev,
806 uint32_t size, uint32_t flags)
807{
Rob Clark871d8122013-11-16 12:56:06 -0500808 struct drm_gem_object *obj = NULL;
Rob Clark05b84912013-09-28 11:28:35 -0400809 int ret;
810
811 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
812
813 size = PAGE_ALIGN(size);
814
Jordan Crouse1a5dff52017-03-07 10:02:51 -0700815 /* Disallow zero sized objects as they make the underlying
816 * infrastructure grumpy
817 */
818 if (size == 0)
819 return ERR_PTR(-EINVAL);
820
Rob Clark79f0e202016-03-16 12:40:35 -0400821 ret = msm_gem_new_impl(dev, size, flags, NULL, &obj);
Rob Clark05b84912013-09-28 11:28:35 -0400822 if (ret)
823 goto fail;
824
Rob Clark072f1f92015-03-03 15:04:25 -0500825 if (use_pages(obj)) {
Rob Clark871d8122013-11-16 12:56:06 -0500826 ret = drm_gem_object_init(dev, obj, size);
827 if (ret)
828 goto fail;
829 } else {
830 drm_gem_private_object_init(dev, obj, size);
831 }
Rob Clark05b84912013-09-28 11:28:35 -0400832
833 return obj;
834
835fail:
Markus Elfring0a677122016-07-13 19:29:19 +0200836 drm_gem_object_unreference(obj);
Rob Clark05b84912013-09-28 11:28:35 -0400837 return ERR_PTR(ret);
838}
839
840struct drm_gem_object *msm_gem_import(struct drm_device *dev,
Rob Clark79f0e202016-03-16 12:40:35 -0400841 struct dma_buf *dmabuf, struct sg_table *sgt)
Rob Clark05b84912013-09-28 11:28:35 -0400842{
843 struct msm_gem_object *msm_obj;
844 struct drm_gem_object *obj;
Rob Clark79f0e202016-03-16 12:40:35 -0400845 uint32_t size;
Rob Clark05b84912013-09-28 11:28:35 -0400846 int ret, npages;
847
Rob Clark871d8122013-11-16 12:56:06 -0500848 /* if we don't have IOMMU, don't bother pretending we can import: */
849 if (!iommu_present(&platform_bus_type)) {
850 dev_err(dev->dev, "cannot import without IOMMU\n");
851 return ERR_PTR(-EINVAL);
852 }
853
Rob Clark79f0e202016-03-16 12:40:35 -0400854 size = PAGE_ALIGN(dmabuf->size);
Rob Clark05b84912013-09-28 11:28:35 -0400855
Rob Clark79f0e202016-03-16 12:40:35 -0400856 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, dmabuf->resv, &obj);
Rob Clark05b84912013-09-28 11:28:35 -0400857 if (ret)
858 goto fail;
859
860 drm_gem_private_object_init(dev, obj, size);
861
862 npages = size / PAGE_SIZE;
863
864 msm_obj = to_msm_bo(obj);
865 msm_obj->sgt = sgt;
866 msm_obj->pages = drm_malloc_ab(npages, sizeof(struct page *));
867 if (!msm_obj->pages) {
868 ret = -ENOMEM;
869 goto fail;
870 }
871
872 ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages);
873 if (ret)
874 goto fail;
875
Rob Clarkc8afe682013-06-26 12:44:06 -0400876 return obj;
877
878fail:
Markus Elfringe73a8562016-07-13 19:15:35 +0200879 drm_gem_object_unreference_unlocked(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400880 return ERR_PTR(ret);
881}