• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
20 #include <linux/dma-buf.h>
21 
22 #include "msm_drv.h"
23 #include "msm_gem.h"
24 #include "msm_gpu.h"
25 #include "msm_mmu.h"
26 
physaddr(struct drm_gem_object * obj)27 static dma_addr_t physaddr(struct drm_gem_object *obj)
28 {
29 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
30 	struct msm_drm_private *priv = obj->dev->dev_private;
31 	return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) +
32 			priv->vram.paddr;
33 }
34 
use_pages(struct drm_gem_object * obj)35 static bool use_pages(struct drm_gem_object *obj)
36 {
37 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
38 	return !msm_obj->vram_node;
39 }
40 
41 /* allocate pages from VRAM carveout, used when no IOMMU: */
get_pages_vram(struct drm_gem_object * obj,int npages)42 static struct page **get_pages_vram(struct drm_gem_object *obj,
43 		int npages)
44 {
45 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
46 	struct msm_drm_private *priv = obj->dev->dev_private;
47 	dma_addr_t paddr;
48 	struct page **p;
49 	int ret, i;
50 
51 	p = drm_malloc_ab(npages, sizeof(struct page *));
52 	if (!p)
53 		return ERR_PTR(-ENOMEM);
54 
55 	ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node,
56 			npages, 0, DRM_MM_SEARCH_DEFAULT);
57 	if (ret) {
58 		drm_free_large(p);
59 		return ERR_PTR(ret);
60 	}
61 
62 	paddr = physaddr(obj);
63 	for (i = 0; i < npages; i++) {
64 		p[i] = phys_to_page(paddr);
65 		paddr += PAGE_SIZE;
66 	}
67 
68 	return p;
69 }
70 
71 /* called with dev->struct_mutex held */
get_pages(struct drm_gem_object * obj)72 static struct page **get_pages(struct drm_gem_object *obj)
73 {
74 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
75 
76 	if (!msm_obj->pages) {
77 		struct drm_device *dev = obj->dev;
78 		struct page **p;
79 		int npages = obj->size >> PAGE_SHIFT;
80 
81 		if (use_pages(obj))
82 			p = drm_gem_get_pages(obj);
83 		else
84 			p = get_pages_vram(obj, npages);
85 
86 		if (IS_ERR(p)) {
87 			dev_err(dev->dev, "could not get pages: %ld\n",
88 					PTR_ERR(p));
89 			return p;
90 		}
91 
92 		msm_obj->pages = p;
93 
94 		msm_obj->sgt = drm_prime_pages_to_sg(p, npages);
95 		if (IS_ERR(msm_obj->sgt)) {
96 			void *ptr = ERR_CAST(msm_obj->sgt);
97 
98 			dev_err(dev->dev, "failed to allocate sgt\n");
99 			msm_obj->sgt = NULL;
100 			return ptr;
101 		}
102 
103 		/* For non-cached buffers, ensure the new pages are clean
104 		 * because display controller, GPU, etc. are not coherent:
105 		 */
106 		if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
107 			dma_map_sg(dev->dev, msm_obj->sgt->sgl,
108 					msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
109 	}
110 
111 	return msm_obj->pages;
112 }
113 
put_pages(struct drm_gem_object * obj)114 static void put_pages(struct drm_gem_object *obj)
115 {
116 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
117 
118 	if (msm_obj->pages) {
119 		if (msm_obj->sgt) {
120 			/* For non-cached buffers, ensure the new
121 			 * pages are clean because display controller,
122 			 * GPU, etc. are not coherent:
123 			 */
124 			if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
125 				dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl,
126 					     msm_obj->sgt->nents,
127 					     DMA_BIDIRECTIONAL);
128 
129 			sg_free_table(msm_obj->sgt);
130 			kfree(msm_obj->sgt);
131 		}
132 
133 		if (use_pages(obj))
134 			drm_gem_put_pages(obj, msm_obj->pages, true, false);
135 		else {
136 			drm_mm_remove_node(msm_obj->vram_node);
137 			drm_free_large(msm_obj->pages);
138 		}
139 
140 		msm_obj->pages = NULL;
141 	}
142 }
143 
msm_gem_get_pages(struct drm_gem_object * obj)144 struct page **msm_gem_get_pages(struct drm_gem_object *obj)
145 {
146 	struct drm_device *dev = obj->dev;
147 	struct page **p;
148 	mutex_lock(&dev->struct_mutex);
149 	p = get_pages(obj);
150 	mutex_unlock(&dev->struct_mutex);
151 	return p;
152 }
153 
msm_gem_put_pages(struct drm_gem_object * obj)154 void msm_gem_put_pages(struct drm_gem_object *obj)
155 {
156 	/* when we start tracking the pin count, then do something here */
157 }
158 
msm_gem_mmap_obj(struct drm_gem_object * obj,struct vm_area_struct * vma)159 int msm_gem_mmap_obj(struct drm_gem_object *obj,
160 		struct vm_area_struct *vma)
161 {
162 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
163 
164 	vma->vm_flags &= ~VM_PFNMAP;
165 	vma->vm_flags |= VM_MIXEDMAP;
166 
167 	if (msm_obj->flags & MSM_BO_WC) {
168 		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
169 	} else if (msm_obj->flags & MSM_BO_UNCACHED) {
170 		vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
171 	} else {
172 		/*
173 		 * Shunt off cached objs to shmem file so they have their own
174 		 * address_space (so unmap_mapping_range does what we want,
175 		 * in particular in the case of mmap'd dmabufs)
176 		 */
177 		fput(vma->vm_file);
178 		get_file(obj->filp);
179 		vma->vm_pgoff = 0;
180 		vma->vm_file  = obj->filp;
181 
182 		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
183 	}
184 
185 	return 0;
186 }
187 
msm_gem_mmap(struct file * filp,struct vm_area_struct * vma)188 int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
189 {
190 	int ret;
191 
192 	ret = drm_gem_mmap(filp, vma);
193 	if (ret) {
194 		DBG("mmap failed: %d", ret);
195 		return ret;
196 	}
197 
198 	return msm_gem_mmap_obj(vma->vm_private_data, vma);
199 }
200 
msm_gem_fault(struct vm_area_struct * vma,struct vm_fault * vmf)201 int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
202 {
203 	struct drm_gem_object *obj = vma->vm_private_data;
204 	struct drm_device *dev = obj->dev;
205 	struct page **pages;
206 	unsigned long pfn;
207 	pgoff_t pgoff;
208 	int ret;
209 
210 	/* Make sure we don't parallel update on a fault, nor move or remove
211 	 * something from beneath our feet
212 	 */
213 	ret = mutex_lock_interruptible(&dev->struct_mutex);
214 	if (ret)
215 		goto out;
216 
217 	/* make sure we have pages attached now */
218 	pages = get_pages(obj);
219 	if (IS_ERR(pages)) {
220 		ret = PTR_ERR(pages);
221 		goto out_unlock;
222 	}
223 
224 	/* We don't use vmf->pgoff since that has the fake offset: */
225 	pgoff = ((unsigned long)vmf->virtual_address -
226 			vma->vm_start) >> PAGE_SHIFT;
227 
228 	pfn = page_to_pfn(pages[pgoff]);
229 
230 	VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
231 			pfn, pfn << PAGE_SHIFT);
232 
233 	ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
234 
235 out_unlock:
236 	mutex_unlock(&dev->struct_mutex);
237 out:
238 	switch (ret) {
239 	case -EAGAIN:
240 	case 0:
241 	case -ERESTARTSYS:
242 	case -EINTR:
243 	case -EBUSY:
244 		/*
245 		 * EBUSY is ok: this just means that another thread
246 		 * already did the job.
247 		 */
248 		return VM_FAULT_NOPAGE;
249 	case -ENOMEM:
250 		return VM_FAULT_OOM;
251 	default:
252 		return VM_FAULT_SIGBUS;
253 	}
254 }
255 
256 /** get mmap offset */
mmap_offset(struct drm_gem_object * obj)257 static uint64_t mmap_offset(struct drm_gem_object *obj)
258 {
259 	struct drm_device *dev = obj->dev;
260 	int ret;
261 
262 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
263 
264 	/* Make it mmapable */
265 	ret = drm_gem_create_mmap_offset(obj);
266 
267 	if (ret) {
268 		dev_err(dev->dev, "could not allocate mmap offset\n");
269 		return 0;
270 	}
271 
272 	return drm_vma_node_offset_addr(&obj->vma_node);
273 }
274 
msm_gem_mmap_offset(struct drm_gem_object * obj)275 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
276 {
277 	uint64_t offset;
278 	mutex_lock(&obj->dev->struct_mutex);
279 	offset = mmap_offset(obj);
280 	mutex_unlock(&obj->dev->struct_mutex);
281 	return offset;
282 }
283 
284 /* should be called under struct_mutex.. although it can be called
285  * from atomic context without struct_mutex to acquire an extra
286  * iova ref if you know one is already held.
287  *
288  * That means when I do eventually need to add support for unpinning
289  * the refcnt counter needs to be atomic_t.
290  */
msm_gem_get_iova_locked(struct drm_gem_object * obj,int id,uint32_t * iova)291 int msm_gem_get_iova_locked(struct drm_gem_object *obj, int id,
292 		uint32_t *iova)
293 {
294 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
295 	int ret = 0;
296 
297 	if (!msm_obj->domain[id].iova) {
298 		struct msm_drm_private *priv = obj->dev->dev_private;
299 		struct page **pages = get_pages(obj);
300 
301 		if (IS_ERR(pages))
302 			return PTR_ERR(pages);
303 
304 		if (iommu_present(&platform_bus_type)) {
305 			struct msm_mmu *mmu = priv->mmus[id];
306 			uint32_t offset;
307 
308 			if (WARN_ON(!mmu))
309 				return -EINVAL;
310 
311 			offset = (uint32_t)mmap_offset(obj);
312 			ret = mmu->funcs->map(mmu, offset, msm_obj->sgt,
313 					obj->size, IOMMU_READ | IOMMU_WRITE);
314 			msm_obj->domain[id].iova = offset;
315 		} else {
316 			msm_obj->domain[id].iova = physaddr(obj);
317 		}
318 	}
319 
320 	if (!ret)
321 		*iova = msm_obj->domain[id].iova;
322 
323 	return ret;
324 }
325 
326 /* get iova, taking a reference.  Should have a matching put */
msm_gem_get_iova(struct drm_gem_object * obj,int id,uint32_t * iova)327 int msm_gem_get_iova(struct drm_gem_object *obj, int id, uint32_t *iova)
328 {
329 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
330 	int ret;
331 
332 	/* this is safe right now because we don't unmap until the
333 	 * bo is deleted:
334 	 */
335 	if (msm_obj->domain[id].iova) {
336 		*iova = msm_obj->domain[id].iova;
337 		return 0;
338 	}
339 
340 	mutex_lock(&obj->dev->struct_mutex);
341 	ret = msm_gem_get_iova_locked(obj, id, iova);
342 	mutex_unlock(&obj->dev->struct_mutex);
343 	return ret;
344 }
345 
346 /* get iova without taking a reference, used in places where you have
347  * already done a 'msm_gem_get_iova()'.
348  */
msm_gem_iova(struct drm_gem_object * obj,int id)349 uint32_t msm_gem_iova(struct drm_gem_object *obj, int id)
350 {
351 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
352 	WARN_ON(!msm_obj->domain[id].iova);
353 	return msm_obj->domain[id].iova;
354 }
355 
msm_gem_put_iova(struct drm_gem_object * obj,int id)356 void msm_gem_put_iova(struct drm_gem_object *obj, int id)
357 {
358 	// XXX TODO ..
359 	// NOTE: probably don't need a _locked() version.. we wouldn't
360 	// normally unmap here, but instead just mark that it could be
361 	// unmapped (if the iova refcnt drops to zero), but then later
362 	// if another _get_iova_locked() fails we can start unmapping
363 	// things that are no longer needed..
364 }
365 
msm_gem_dumb_create(struct drm_file * file,struct drm_device * dev,struct drm_mode_create_dumb * args)366 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
367 		struct drm_mode_create_dumb *args)
368 {
369 	args->pitch = align_pitch(args->width, args->bpp);
370 	args->size  = PAGE_ALIGN(args->pitch * args->height);
371 	return msm_gem_new_handle(dev, file, args->size,
372 			MSM_BO_SCANOUT | MSM_BO_WC, &args->handle);
373 }
374 
msm_gem_dumb_map_offset(struct drm_file * file,struct drm_device * dev,uint32_t handle,uint64_t * offset)375 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
376 		uint32_t handle, uint64_t *offset)
377 {
378 	struct drm_gem_object *obj;
379 	int ret = 0;
380 
381 	/* GEM does all our handle to object mapping */
382 	obj = drm_gem_object_lookup(dev, file, handle);
383 	if (obj == NULL) {
384 		ret = -ENOENT;
385 		goto fail;
386 	}
387 
388 	*offset = msm_gem_mmap_offset(obj);
389 
390 	drm_gem_object_unreference_unlocked(obj);
391 
392 fail:
393 	return ret;
394 }
395 
msm_gem_vaddr_locked(struct drm_gem_object * obj)396 void *msm_gem_vaddr_locked(struct drm_gem_object *obj)
397 {
398 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
399 	WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
400 	if (!msm_obj->vaddr) {
401 		struct page **pages = get_pages(obj);
402 		if (IS_ERR(pages))
403 			return ERR_CAST(pages);
404 		msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
405 				VM_MAP, pgprot_writecombine(PAGE_KERNEL));
406 	}
407 	return msm_obj->vaddr;
408 }
409 
msm_gem_vaddr(struct drm_gem_object * obj)410 void *msm_gem_vaddr(struct drm_gem_object *obj)
411 {
412 	void *ret;
413 	mutex_lock(&obj->dev->struct_mutex);
414 	ret = msm_gem_vaddr_locked(obj);
415 	mutex_unlock(&obj->dev->struct_mutex);
416 	return ret;
417 }
418 
419 /* setup callback for when bo is no longer busy..
420  * TODO probably want to differentiate read vs write..
421  */
msm_gem_queue_inactive_cb(struct drm_gem_object * obj,struct msm_fence_cb * cb)422 int msm_gem_queue_inactive_cb(struct drm_gem_object *obj,
423 		struct msm_fence_cb *cb)
424 {
425 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
426 	uint32_t fence = msm_gem_fence(msm_obj,
427 			MSM_PREP_READ | MSM_PREP_WRITE);
428 	return msm_queue_fence_cb(obj->dev, cb, fence);
429 }
430 
msm_gem_move_to_active(struct drm_gem_object * obj,struct msm_gpu * gpu,bool write,uint32_t fence)431 void msm_gem_move_to_active(struct drm_gem_object *obj,
432 		struct msm_gpu *gpu, bool write, uint32_t fence)
433 {
434 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
435 	msm_obj->gpu = gpu;
436 	if (write)
437 		msm_obj->write_fence = fence;
438 	else
439 		msm_obj->read_fence = fence;
440 	list_del_init(&msm_obj->mm_list);
441 	list_add_tail(&msm_obj->mm_list, &gpu->active_list);
442 }
443 
msm_gem_move_to_inactive(struct drm_gem_object * obj)444 void msm_gem_move_to_inactive(struct drm_gem_object *obj)
445 {
446 	struct drm_device *dev = obj->dev;
447 	struct msm_drm_private *priv = dev->dev_private;
448 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
449 
450 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
451 
452 	msm_obj->gpu = NULL;
453 	msm_obj->read_fence = 0;
454 	msm_obj->write_fence = 0;
455 	list_del_init(&msm_obj->mm_list);
456 	list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
457 }
458 
msm_gem_cpu_prep(struct drm_gem_object * obj,uint32_t op,ktime_t * timeout)459 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
460 {
461 	struct drm_device *dev = obj->dev;
462 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
463 	int ret = 0;
464 
465 	if (is_active(msm_obj)) {
466 		uint32_t fence = msm_gem_fence(msm_obj, op);
467 
468 		if (op & MSM_PREP_NOSYNC)
469 			timeout = NULL;
470 
471 		ret = msm_wait_fence(dev, fence, timeout, true);
472 	}
473 
474 	/* TODO cache maintenance */
475 
476 	return ret;
477 }
478 
msm_gem_cpu_fini(struct drm_gem_object * obj)479 int msm_gem_cpu_fini(struct drm_gem_object *obj)
480 {
481 	/* TODO cache maintenance */
482 	return 0;
483 }
484 
485 #ifdef CONFIG_DEBUG_FS
msm_gem_describe(struct drm_gem_object * obj,struct seq_file * m)486 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
487 {
488 	struct drm_device *dev = obj->dev;
489 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
490 	uint64_t off = drm_vma_node_start(&obj->vma_node);
491 
492 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
493 	seq_printf(m, "%08x: %c(r=%u,w=%u) %2d (%2d) %08llx %p %zu\n",
494 			msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
495 			msm_obj->read_fence, msm_obj->write_fence,
496 			obj->name, obj->refcount.refcount.counter,
497 			off, msm_obj->vaddr, obj->size);
498 }
499 
msm_gem_describe_objects(struct list_head * list,struct seq_file * m)500 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
501 {
502 	struct msm_gem_object *msm_obj;
503 	int count = 0;
504 	size_t size = 0;
505 
506 	list_for_each_entry(msm_obj, list, mm_list) {
507 		struct drm_gem_object *obj = &msm_obj->base;
508 		seq_printf(m, "   ");
509 		msm_gem_describe(obj, m);
510 		count++;
511 		size += obj->size;
512 	}
513 
514 	seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
515 }
516 #endif
517 
msm_gem_free_object(struct drm_gem_object * obj)518 void msm_gem_free_object(struct drm_gem_object *obj)
519 {
520 	struct drm_device *dev = obj->dev;
521 	struct msm_drm_private *priv = obj->dev->dev_private;
522 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
523 	int id;
524 
525 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
526 
527 	/* object should not be on active list: */
528 	WARN_ON(is_active(msm_obj));
529 
530 	list_del(&msm_obj->mm_list);
531 
532 	for (id = 0; id < ARRAY_SIZE(msm_obj->domain); id++) {
533 		struct msm_mmu *mmu = priv->mmus[id];
534 		if (mmu && msm_obj->domain[id].iova) {
535 			uint32_t offset = msm_obj->domain[id].iova;
536 			mmu->funcs->unmap(mmu, offset, msm_obj->sgt, obj->size);
537 		}
538 	}
539 
540 	if (obj->import_attach) {
541 		if (msm_obj->vaddr)
542 			dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr);
543 
544 		/* Don't drop the pages for imported dmabuf, as they are not
545 		 * ours, just free the array we allocated:
546 		 */
547 		if (msm_obj->pages)
548 			drm_free_large(msm_obj->pages);
549 
550 		drm_prime_gem_destroy(obj, msm_obj->sgt);
551 	} else {
552 		vunmap(msm_obj->vaddr);
553 		put_pages(obj);
554 	}
555 
556 	if (msm_obj->resv == &msm_obj->_resv)
557 		reservation_object_fini(msm_obj->resv);
558 
559 	drm_gem_object_release(obj);
560 
561 	kfree(msm_obj);
562 }
563 
564 /* convenience method to construct a GEM buffer object, and userspace handle */
msm_gem_new_handle(struct drm_device * dev,struct drm_file * file,uint32_t size,uint32_t flags,uint32_t * handle)565 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
566 		uint32_t size, uint32_t flags, uint32_t *handle)
567 {
568 	struct drm_gem_object *obj;
569 	int ret;
570 
571 	ret = mutex_lock_interruptible(&dev->struct_mutex);
572 	if (ret)
573 		return ret;
574 
575 	obj = msm_gem_new(dev, size, flags);
576 
577 	mutex_unlock(&dev->struct_mutex);
578 
579 	if (IS_ERR(obj))
580 		return PTR_ERR(obj);
581 
582 	ret = drm_gem_handle_create(file, obj, handle);
583 
584 	/* drop reference from allocate - handle holds it now */
585 	drm_gem_object_unreference_unlocked(obj);
586 
587 	return ret;
588 }
589 
msm_gem_new_impl(struct drm_device * dev,uint32_t size,uint32_t flags,struct drm_gem_object ** obj)590 static int msm_gem_new_impl(struct drm_device *dev,
591 		uint32_t size, uint32_t flags,
592 		struct drm_gem_object **obj)
593 {
594 	struct msm_drm_private *priv = dev->dev_private;
595 	struct msm_gem_object *msm_obj;
596 	unsigned sz;
597 	bool use_vram = false;
598 
599 	switch (flags & MSM_BO_CACHE_MASK) {
600 	case MSM_BO_UNCACHED:
601 	case MSM_BO_CACHED:
602 	case MSM_BO_WC:
603 		break;
604 	default:
605 		dev_err(dev->dev, "invalid cache flag: %x\n",
606 				(flags & MSM_BO_CACHE_MASK));
607 		return -EINVAL;
608 	}
609 
610 	if (!iommu_present(&platform_bus_type))
611 		use_vram = true;
612 	else if ((flags & MSM_BO_STOLEN) && priv->vram.size)
613 		use_vram = true;
614 
615 	if (WARN_ON(use_vram && !priv->vram.size))
616 		return -EINVAL;
617 
618 	sz = sizeof(*msm_obj);
619 	if (use_vram)
620 		sz += sizeof(struct drm_mm_node);
621 
622 	msm_obj = kzalloc(sz, GFP_KERNEL);
623 	if (!msm_obj)
624 		return -ENOMEM;
625 
626 	if (use_vram)
627 		msm_obj->vram_node = (void *)&msm_obj[1];
628 
629 	msm_obj->flags = flags;
630 
631 	msm_obj->resv = &msm_obj->_resv;
632 	reservation_object_init(msm_obj->resv);
633 
634 	INIT_LIST_HEAD(&msm_obj->submit_entry);
635 	list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
636 
637 	*obj = &msm_obj->base;
638 
639 	return 0;
640 }
641 
msm_gem_new(struct drm_device * dev,uint32_t size,uint32_t flags)642 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
643 		uint32_t size, uint32_t flags)
644 {
645 	struct drm_gem_object *obj = NULL;
646 	int ret;
647 
648 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
649 
650 	size = PAGE_ALIGN(size);
651 
652 	ret = msm_gem_new_impl(dev, size, flags, &obj);
653 	if (ret)
654 		return ERR_PTR(ret);
655 
656 	if (use_pages(obj)) {
657 		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 	}
663 
664 	return obj;
665 
666 fail:
667 	if (obj)
668 		drm_gem_object_unreference(obj);
669 
670 	return ERR_PTR(ret);
671 }
672 
msm_gem_import(struct drm_device * dev,uint32_t size,struct sg_table * sgt)673 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
674 		uint32_t size, struct sg_table *sgt)
675 {
676 	struct msm_gem_object *msm_obj;
677 	struct drm_gem_object *obj;
678 	int ret, npages;
679 
680 	/* if we don't have IOMMU, don't bother pretending we can import: */
681 	if (!iommu_present(&platform_bus_type)) {
682 		dev_err(dev->dev, "cannot import without IOMMU\n");
683 		return ERR_PTR(-EINVAL);
684 	}
685 
686 	size = PAGE_ALIGN(size);
687 
688 	ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj);
689 	if (ret)
690 		return ERR_PTR(ret);
691 
692 	drm_gem_private_object_init(dev, obj, size);
693 
694 	npages = size / PAGE_SIZE;
695 
696 	msm_obj = to_msm_bo(obj);
697 	msm_obj->sgt = sgt;
698 	msm_obj->pages = drm_malloc_ab(npages, sizeof(struct page *));
699 	if (!msm_obj->pages) {
700 		ret = -ENOMEM;
701 		goto fail;
702 	}
703 
704 	ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages);
705 	if (ret)
706 		goto fail;
707 
708 	return obj;
709 
710 fail:
711 	if (obj)
712 		drm_gem_object_unreference_unlocked(obj);
713 
714 	return ERR_PTR(ret);
715 }
716