xref: /openbmc/linux/drivers/gpu/drm/omapdrm/omap_gem.c (revision ae0be8de)
1 /*
2  * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
3  * Author: Rob Clark <rob.clark@linaro.org>
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/seq_file.h>
19 #include <linux/shmem_fs.h>
20 #include <linux/spinlock.h>
21 #include <linux/pfn_t.h>
22 
23 #include <drm/drm_vma_manager.h>
24 
25 #include "omap_drv.h"
26 #include "omap_dmm_tiler.h"
27 
28 /*
29  * GEM buffer object implementation.
30  */
31 
32 /* note: we use upper 8 bits of flags for driver-internal flags: */
33 #define OMAP_BO_MEM_DMA_API	0x01000000	/* memory allocated with the dma_alloc_* API */
34 #define OMAP_BO_MEM_SHMEM	0x02000000	/* memory allocated through shmem backing */
35 #define OMAP_BO_MEM_DMABUF	0x08000000	/* memory imported from a dmabuf */
36 
37 struct omap_gem_object {
38 	struct drm_gem_object base;
39 
40 	struct list_head mm_list;
41 
42 	u32 flags;
43 
44 	/** width/height for tiled formats (rounded up to slot boundaries) */
45 	u16 width, height;
46 
47 	/** roll applied when mapping to DMM */
48 	u32 roll;
49 
50 	/** protects dma_addr_cnt, block, pages, dma_addrs and vaddr */
51 	struct mutex lock;
52 
53 	/**
54 	 * dma_addr contains the buffer DMA address. It is valid for
55 	 *
56 	 * - buffers allocated through the DMA mapping API (with the
57 	 *   OMAP_BO_MEM_DMA_API flag set)
58 	 *
59 	 * - buffers imported from dmabuf (with the OMAP_BO_MEM_DMABUF flag set)
60 	 *   if they are physically contiguous (when sgt->orig_nents == 1)
61 	 *
62 	 * - buffers mapped through the TILER when dma_addr_cnt is not zero, in
63 	 *   which case the DMA address points to the TILER aperture
64 	 *
65 	 * Physically contiguous buffers have their DMA address equal to the
66 	 * physical address as we don't remap those buffers through the TILER.
67 	 *
68 	 * Buffers mapped to the TILER have their DMA address pointing to the
69 	 * TILER aperture. As TILER mappings are refcounted (through
70 	 * dma_addr_cnt) the DMA address must be accessed through omap_gem_pin()
71 	 * to ensure that the mapping won't disappear unexpectedly. References
72 	 * must be released with omap_gem_unpin().
73 	 */
74 	dma_addr_t dma_addr;
75 
76 	/**
77 	 * # of users of dma_addr
78 	 */
79 	u32 dma_addr_cnt;
80 
81 	/**
82 	 * If the buffer has been imported from a dmabuf the OMAP_DB_DMABUF flag
83 	 * is set and the sgt field is valid.
84 	 */
85 	struct sg_table *sgt;
86 
87 	/**
88 	 * tiler block used when buffer is remapped in DMM/TILER.
89 	 */
90 	struct tiler_block *block;
91 
92 	/**
93 	 * Array of backing pages, if allocated.  Note that pages are never
94 	 * allocated for buffers originally allocated from contiguous memory
95 	 */
96 	struct page **pages;
97 
98 	/** addresses corresponding to pages in above array */
99 	dma_addr_t *dma_addrs;
100 
101 	/**
102 	 * Virtual address, if mapped.
103 	 */
104 	void *vaddr;
105 };
106 
107 #define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
108 
109 /* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
110  * not necessarily pinned in TILER all the time, and (b) when they are
111  * they are not necessarily page aligned, we reserve one or more small
112  * regions in each of the 2d containers to use as a user-GART where we
113  * can create a second page-aligned mapping of parts of the buffer
114  * being accessed from userspace.
115  *
116  * Note that we could optimize slightly when we know that multiple
117  * tiler containers are backed by the same PAT.. but I'll leave that
118  * for later..
119  */
120 #define NUM_USERGART_ENTRIES 2
121 struct omap_drm_usergart_entry {
122 	struct tiler_block *block;	/* the reserved tiler block */
123 	dma_addr_t dma_addr;
124 	struct drm_gem_object *obj;	/* the current pinned obj */
125 	pgoff_t obj_pgoff;		/* page offset of obj currently
126 					   mapped in */
127 };
128 
129 struct omap_drm_usergart {
130 	struct omap_drm_usergart_entry entry[NUM_USERGART_ENTRIES];
131 	int height;				/* height in rows */
132 	int height_shift;		/* ilog2(height in rows) */
133 	int slot_shift;			/* ilog2(width per slot) */
134 	int stride_pfn;			/* stride in pages */
135 	int last;				/* index of last used entry */
136 };
137 
138 /* -----------------------------------------------------------------------------
139  * Helpers
140  */
141 
142 /** get mmap offset */
143 u64 omap_gem_mmap_offset(struct drm_gem_object *obj)
144 {
145 	struct drm_device *dev = obj->dev;
146 	int ret;
147 	size_t size;
148 
149 	/* Make it mmapable */
150 	size = omap_gem_mmap_size(obj);
151 	ret = drm_gem_create_mmap_offset_size(obj, size);
152 	if (ret) {
153 		dev_err(dev->dev, "could not allocate mmap offset\n");
154 		return 0;
155 	}
156 
157 	return drm_vma_node_offset_addr(&obj->vma_node);
158 }
159 
160 static bool omap_gem_is_contiguous(struct omap_gem_object *omap_obj)
161 {
162 	if (omap_obj->flags & OMAP_BO_MEM_DMA_API)
163 		return true;
164 
165 	if ((omap_obj->flags & OMAP_BO_MEM_DMABUF) && omap_obj->sgt->nents == 1)
166 		return true;
167 
168 	return false;
169 }
170 
171 /* -----------------------------------------------------------------------------
172  * Eviction
173  */
174 
175 static void omap_gem_evict_entry(struct drm_gem_object *obj,
176 		enum tiler_fmt fmt, struct omap_drm_usergart_entry *entry)
177 {
178 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
179 	struct omap_drm_private *priv = obj->dev->dev_private;
180 	int n = priv->usergart[fmt].height;
181 	size_t size = PAGE_SIZE * n;
182 	loff_t off = omap_gem_mmap_offset(obj) +
183 			(entry->obj_pgoff << PAGE_SHIFT);
184 	const int m = DIV_ROUND_UP(omap_obj->width << fmt, PAGE_SIZE);
185 
186 	if (m > 1) {
187 		int i;
188 		/* if stride > than PAGE_SIZE then sparse mapping: */
189 		for (i = n; i > 0; i--) {
190 			unmap_mapping_range(obj->dev->anon_inode->i_mapping,
191 					    off, PAGE_SIZE, 1);
192 			off += PAGE_SIZE * m;
193 		}
194 	} else {
195 		unmap_mapping_range(obj->dev->anon_inode->i_mapping,
196 				    off, size, 1);
197 	}
198 
199 	entry->obj = NULL;
200 }
201 
202 /* Evict a buffer from usergart, if it is mapped there */
203 static void omap_gem_evict(struct drm_gem_object *obj)
204 {
205 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
206 	struct omap_drm_private *priv = obj->dev->dev_private;
207 
208 	if (omap_obj->flags & OMAP_BO_TILED) {
209 		enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
210 		int i;
211 
212 		for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
213 			struct omap_drm_usergart_entry *entry =
214 				&priv->usergart[fmt].entry[i];
215 
216 			if (entry->obj == obj)
217 				omap_gem_evict_entry(obj, fmt, entry);
218 		}
219 	}
220 }
221 
222 /* -----------------------------------------------------------------------------
223  * Page Management
224  */
225 
226 /*
227  * Ensure backing pages are allocated. Must be called with the omap_obj.lock
228  * held.
229  */
230 static int omap_gem_attach_pages(struct drm_gem_object *obj)
231 {
232 	struct drm_device *dev = obj->dev;
233 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
234 	struct page **pages;
235 	int npages = obj->size >> PAGE_SHIFT;
236 	int i, ret;
237 	dma_addr_t *addrs;
238 
239 	lockdep_assert_held(&omap_obj->lock);
240 
241 	/*
242 	 * If not using shmem (in which case backing pages don't need to be
243 	 * allocated) or if pages are already allocated we're done.
244 	 */
245 	if (!(omap_obj->flags & OMAP_BO_MEM_SHMEM) || omap_obj->pages)
246 		return 0;
247 
248 	pages = drm_gem_get_pages(obj);
249 	if (IS_ERR(pages)) {
250 		dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
251 		return PTR_ERR(pages);
252 	}
253 
254 	/* for non-cached buffers, ensure the new pages are clean because
255 	 * DSS, GPU, etc. are not cache coherent:
256 	 */
257 	if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
258 		addrs = kmalloc_array(npages, sizeof(*addrs), GFP_KERNEL);
259 		if (!addrs) {
260 			ret = -ENOMEM;
261 			goto free_pages;
262 		}
263 
264 		for (i = 0; i < npages; i++) {
265 			addrs[i] = dma_map_page(dev->dev, pages[i],
266 					0, PAGE_SIZE, DMA_TO_DEVICE);
267 
268 			if (dma_mapping_error(dev->dev, addrs[i])) {
269 				dev_warn(dev->dev,
270 					"%s: failed to map page\n", __func__);
271 
272 				for (i = i - 1; i >= 0; --i) {
273 					dma_unmap_page(dev->dev, addrs[i],
274 						PAGE_SIZE, DMA_TO_DEVICE);
275 				}
276 
277 				ret = -ENOMEM;
278 				goto free_addrs;
279 			}
280 		}
281 	} else {
282 		addrs = kcalloc(npages, sizeof(*addrs), GFP_KERNEL);
283 		if (!addrs) {
284 			ret = -ENOMEM;
285 			goto free_pages;
286 		}
287 	}
288 
289 	omap_obj->dma_addrs = addrs;
290 	omap_obj->pages = pages;
291 
292 	return 0;
293 
294 free_addrs:
295 	kfree(addrs);
296 free_pages:
297 	drm_gem_put_pages(obj, pages, true, false);
298 
299 	return ret;
300 }
301 
302 /* Release backing pages. Must be called with the omap_obj.lock held. */
303 static void omap_gem_detach_pages(struct drm_gem_object *obj)
304 {
305 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
306 	unsigned int npages = obj->size >> PAGE_SHIFT;
307 	unsigned int i;
308 
309 	lockdep_assert_held(&omap_obj->lock);
310 
311 	for (i = 0; i < npages; i++) {
312 		if (omap_obj->dma_addrs[i])
313 			dma_unmap_page(obj->dev->dev, omap_obj->dma_addrs[i],
314 				       PAGE_SIZE, DMA_TO_DEVICE);
315 	}
316 
317 	kfree(omap_obj->dma_addrs);
318 	omap_obj->dma_addrs = NULL;
319 
320 	drm_gem_put_pages(obj, omap_obj->pages, true, false);
321 	omap_obj->pages = NULL;
322 }
323 
324 /* get buffer flags */
325 u32 omap_gem_flags(struct drm_gem_object *obj)
326 {
327 	return to_omap_bo(obj)->flags;
328 }
329 
330 /** get mmap size */
331 size_t omap_gem_mmap_size(struct drm_gem_object *obj)
332 {
333 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
334 	size_t size = obj->size;
335 
336 	if (omap_obj->flags & OMAP_BO_TILED) {
337 		/* for tiled buffers, the virtual size has stride rounded up
338 		 * to 4kb.. (to hide the fact that row n+1 might start 16kb or
339 		 * 32kb later!).  But we don't back the entire buffer with
340 		 * pages, only the valid picture part.. so need to adjust for
341 		 * this in the size used to mmap and generate mmap offset
342 		 */
343 		size = tiler_vsize(gem2fmt(omap_obj->flags),
344 				omap_obj->width, omap_obj->height);
345 	}
346 
347 	return size;
348 }
349 
350 /* -----------------------------------------------------------------------------
351  * Fault Handling
352  */
353 
354 /* Normal handling for the case of faulting in non-tiled buffers */
355 static vm_fault_t omap_gem_fault_1d(struct drm_gem_object *obj,
356 		struct vm_area_struct *vma, struct vm_fault *vmf)
357 {
358 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
359 	unsigned long pfn;
360 	pgoff_t pgoff;
361 
362 	/* We don't use vmf->pgoff since that has the fake offset: */
363 	pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
364 
365 	if (omap_obj->pages) {
366 		omap_gem_cpu_sync_page(obj, pgoff);
367 		pfn = page_to_pfn(omap_obj->pages[pgoff]);
368 	} else {
369 		BUG_ON(!omap_gem_is_contiguous(omap_obj));
370 		pfn = (omap_obj->dma_addr >> PAGE_SHIFT) + pgoff;
371 	}
372 
373 	VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
374 			pfn, pfn << PAGE_SHIFT);
375 
376 	return vmf_insert_mixed(vma, vmf->address,
377 			__pfn_to_pfn_t(pfn, PFN_DEV));
378 }
379 
380 /* Special handling for the case of faulting in 2d tiled buffers */
381 static vm_fault_t omap_gem_fault_2d(struct drm_gem_object *obj,
382 		struct vm_area_struct *vma, struct vm_fault *vmf)
383 {
384 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
385 	struct omap_drm_private *priv = obj->dev->dev_private;
386 	struct omap_drm_usergart_entry *entry;
387 	enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
388 	struct page *pages[64];  /* XXX is this too much to have on stack? */
389 	unsigned long pfn;
390 	pgoff_t pgoff, base_pgoff;
391 	unsigned long vaddr;
392 	int i, err, slots;
393 	vm_fault_t ret = VM_FAULT_NOPAGE;
394 
395 	/*
396 	 * Note the height of the slot is also equal to the number of pages
397 	 * that need to be mapped in to fill 4kb wide CPU page.  If the slot
398 	 * height is 64, then 64 pages fill a 4kb wide by 64 row region.
399 	 */
400 	const int n = priv->usergart[fmt].height;
401 	const int n_shift = priv->usergart[fmt].height_shift;
402 
403 	/*
404 	 * If buffer width in bytes > PAGE_SIZE then the virtual stride is
405 	 * rounded up to next multiple of PAGE_SIZE.. this need to be taken
406 	 * into account in some of the math, so figure out virtual stride
407 	 * in pages
408 	 */
409 	const int m = DIV_ROUND_UP(omap_obj->width << fmt, PAGE_SIZE);
410 
411 	/* We don't use vmf->pgoff since that has the fake offset: */
412 	pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
413 
414 	/*
415 	 * Actual address we start mapping at is rounded down to previous slot
416 	 * boundary in the y direction:
417 	 */
418 	base_pgoff = round_down(pgoff, m << n_shift);
419 
420 	/* figure out buffer width in slots */
421 	slots = omap_obj->width >> priv->usergart[fmt].slot_shift;
422 
423 	vaddr = vmf->address - ((pgoff - base_pgoff) << PAGE_SHIFT);
424 
425 	entry = &priv->usergart[fmt].entry[priv->usergart[fmt].last];
426 
427 	/* evict previous buffer using this usergart entry, if any: */
428 	if (entry->obj)
429 		omap_gem_evict_entry(entry->obj, fmt, entry);
430 
431 	entry->obj = obj;
432 	entry->obj_pgoff = base_pgoff;
433 
434 	/* now convert base_pgoff to phys offset from virt offset: */
435 	base_pgoff = (base_pgoff >> n_shift) * slots;
436 
437 	/* for wider-than 4k.. figure out which part of the slot-row we want: */
438 	if (m > 1) {
439 		int off = pgoff % m;
440 		entry->obj_pgoff += off;
441 		base_pgoff /= m;
442 		slots = min(slots - (off << n_shift), n);
443 		base_pgoff += off << n_shift;
444 		vaddr += off << PAGE_SHIFT;
445 	}
446 
447 	/*
448 	 * Map in pages. Beyond the valid pixel part of the buffer, we set
449 	 * pages[i] to NULL to get a dummy page mapped in.. if someone
450 	 * reads/writes it they will get random/undefined content, but at
451 	 * least it won't be corrupting whatever other random page used to
452 	 * be mapped in, or other undefined behavior.
453 	 */
454 	memcpy(pages, &omap_obj->pages[base_pgoff],
455 			sizeof(struct page *) * slots);
456 	memset(pages + slots, 0,
457 			sizeof(struct page *) * (n - slots));
458 
459 	err = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
460 	if (err) {
461 		ret = vmf_error(err);
462 		dev_err(obj->dev->dev, "failed to pin: %d\n", err);
463 		return ret;
464 	}
465 
466 	pfn = entry->dma_addr >> PAGE_SHIFT;
467 
468 	VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
469 			pfn, pfn << PAGE_SHIFT);
470 
471 	for (i = n; i > 0; i--) {
472 		ret = vmf_insert_mixed(vma,
473 			vaddr, __pfn_to_pfn_t(pfn, PFN_DEV));
474 		if (ret & VM_FAULT_ERROR)
475 			break;
476 		pfn += priv->usergart[fmt].stride_pfn;
477 		vaddr += PAGE_SIZE * m;
478 	}
479 
480 	/* simple round-robin: */
481 	priv->usergart[fmt].last = (priv->usergart[fmt].last + 1)
482 				 % NUM_USERGART_ENTRIES;
483 
484 	return ret;
485 }
486 
487 /**
488  * omap_gem_fault		-	pagefault handler for GEM objects
489  * @vmf: fault detail
490  *
491  * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
492  * does most of the work for us including the actual map/unmap calls
493  * but we need to do the actual page work.
494  *
495  * The VMA was set up by GEM. In doing so it also ensured that the
496  * vma->vm_private_data points to the GEM object that is backing this
497  * mapping.
498  */
499 vm_fault_t omap_gem_fault(struct vm_fault *vmf)
500 {
501 	struct vm_area_struct *vma = vmf->vma;
502 	struct drm_gem_object *obj = vma->vm_private_data;
503 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
504 	int err;
505 	vm_fault_t ret;
506 
507 	/* Make sure we don't parallel update on a fault, nor move or remove
508 	 * something from beneath our feet
509 	 */
510 	mutex_lock(&omap_obj->lock);
511 
512 	/* if a shmem backed object, make sure we have pages attached now */
513 	err = omap_gem_attach_pages(obj);
514 	if (err) {
515 		ret = vmf_error(err);
516 		goto fail;
517 	}
518 
519 	/* where should we do corresponding put_pages().. we are mapping
520 	 * the original page, rather than thru a GART, so we can't rely
521 	 * on eviction to trigger this.  But munmap() or all mappings should
522 	 * probably trigger put_pages()?
523 	 */
524 
525 	if (omap_obj->flags & OMAP_BO_TILED)
526 		ret = omap_gem_fault_2d(obj, vma, vmf);
527 	else
528 		ret = omap_gem_fault_1d(obj, vma, vmf);
529 
530 
531 fail:
532 	mutex_unlock(&omap_obj->lock);
533 	return ret;
534 }
535 
536 /** We override mainly to fix up some of the vm mapping flags.. */
537 int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma)
538 {
539 	int ret;
540 
541 	ret = drm_gem_mmap(filp, vma);
542 	if (ret) {
543 		DBG("mmap failed: %d", ret);
544 		return ret;
545 	}
546 
547 	return omap_gem_mmap_obj(vma->vm_private_data, vma);
548 }
549 
550 int omap_gem_mmap_obj(struct drm_gem_object *obj,
551 		struct vm_area_struct *vma)
552 {
553 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
554 
555 	vma->vm_flags &= ~VM_PFNMAP;
556 	vma->vm_flags |= VM_MIXEDMAP;
557 
558 	if (omap_obj->flags & OMAP_BO_WC) {
559 		vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
560 	} else if (omap_obj->flags & OMAP_BO_UNCACHED) {
561 		vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
562 	} else {
563 		/*
564 		 * We do have some private objects, at least for scanout buffers
565 		 * on hardware without DMM/TILER.  But these are allocated write-
566 		 * combine
567 		 */
568 		if (WARN_ON(!obj->filp))
569 			return -EINVAL;
570 
571 		/*
572 		 * Shunt off cached objs to shmem file so they have their own
573 		 * address_space (so unmap_mapping_range does what we want,
574 		 * in particular in the case of mmap'd dmabufs)
575 		 */
576 		fput(vma->vm_file);
577 		vma->vm_pgoff = 0;
578 		vma->vm_file  = get_file(obj->filp);
579 
580 		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
581 	}
582 
583 	return 0;
584 }
585 
586 /* -----------------------------------------------------------------------------
587  * Dumb Buffers
588  */
589 
590 /**
591  * omap_gem_dumb_create	-	create a dumb buffer
592  * @drm_file: our client file
593  * @dev: our device
594  * @args: the requested arguments copied from userspace
595  *
596  * Allocate a buffer suitable for use for a frame buffer of the
597  * form described by user space. Give userspace a handle by which
598  * to reference it.
599  */
600 int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
601 		struct drm_mode_create_dumb *args)
602 {
603 	union omap_gem_size gsize;
604 
605 	args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
606 
607 	args->size = PAGE_ALIGN(args->pitch * args->height);
608 
609 	gsize = (union omap_gem_size){
610 		.bytes = args->size,
611 	};
612 
613 	return omap_gem_new_handle(dev, file, gsize,
614 			OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle);
615 }
616 
617 /**
618  * omap_gem_dumb_map	-	buffer mapping for dumb interface
619  * @file: our drm client file
620  * @dev: drm device
621  * @handle: GEM handle to the object (from dumb_create)
622  *
623  * Do the necessary setup to allow the mapping of the frame buffer
624  * into user memory. We don't have to do much here at the moment.
625  */
626 int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
627 		u32 handle, u64 *offset)
628 {
629 	struct drm_gem_object *obj;
630 	int ret = 0;
631 
632 	/* GEM does all our handle to object mapping */
633 	obj = drm_gem_object_lookup(file, handle);
634 	if (obj == NULL) {
635 		ret = -ENOENT;
636 		goto fail;
637 	}
638 
639 	*offset = omap_gem_mmap_offset(obj);
640 
641 	drm_gem_object_put_unlocked(obj);
642 
643 fail:
644 	return ret;
645 }
646 
647 #ifdef CONFIG_DRM_FBDEV_EMULATION
648 /* Set scrolling position.  This allows us to implement fast scrolling
649  * for console.
650  *
651  * Call only from non-atomic contexts.
652  */
653 int omap_gem_roll(struct drm_gem_object *obj, u32 roll)
654 {
655 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
656 	u32 npages = obj->size >> PAGE_SHIFT;
657 	int ret = 0;
658 
659 	if (roll > npages) {
660 		dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
661 		return -EINVAL;
662 	}
663 
664 	omap_obj->roll = roll;
665 
666 	mutex_lock(&omap_obj->lock);
667 
668 	/* if we aren't mapped yet, we don't need to do anything */
669 	if (omap_obj->block) {
670 		ret = omap_gem_attach_pages(obj);
671 		if (ret)
672 			goto fail;
673 
674 		ret = tiler_pin(omap_obj->block, omap_obj->pages, npages,
675 				roll, true);
676 		if (ret)
677 			dev_err(obj->dev->dev, "could not repin: %d\n", ret);
678 	}
679 
680 fail:
681 	mutex_unlock(&omap_obj->lock);
682 
683 	return ret;
684 }
685 #endif
686 
687 /* -----------------------------------------------------------------------------
688  * Memory Management & DMA Sync
689  */
690 
691 /*
692  * shmem buffers that are mapped cached are not coherent.
693  *
694  * We keep track of dirty pages using page faulting to perform cache management.
695  * When a page is mapped to the CPU in read/write mode the device can't access
696  * it and omap_obj->dma_addrs[i] is NULL. When a page is mapped to the device
697  * the omap_obj->dma_addrs[i] is set to the DMA address, and the page is
698  * unmapped from the CPU.
699  */
700 static inline bool omap_gem_is_cached_coherent(struct drm_gem_object *obj)
701 {
702 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
703 
704 	return !((omap_obj->flags & OMAP_BO_MEM_SHMEM) &&
705 		((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED));
706 }
707 
708 /* Sync the buffer for CPU access.. note pages should already be
709  * attached, ie. omap_gem_get_pages()
710  */
711 void omap_gem_cpu_sync_page(struct drm_gem_object *obj, int pgoff)
712 {
713 	struct drm_device *dev = obj->dev;
714 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
715 
716 	if (omap_gem_is_cached_coherent(obj))
717 		return;
718 
719 	if (omap_obj->dma_addrs[pgoff]) {
720 		dma_unmap_page(dev->dev, omap_obj->dma_addrs[pgoff],
721 				PAGE_SIZE, DMA_TO_DEVICE);
722 		omap_obj->dma_addrs[pgoff] = 0;
723 	}
724 }
725 
726 /* sync the buffer for DMA access */
727 void omap_gem_dma_sync_buffer(struct drm_gem_object *obj,
728 		enum dma_data_direction dir)
729 {
730 	struct drm_device *dev = obj->dev;
731 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
732 	int i, npages = obj->size >> PAGE_SHIFT;
733 	struct page **pages = omap_obj->pages;
734 	bool dirty = false;
735 
736 	if (omap_gem_is_cached_coherent(obj))
737 		return;
738 
739 	for (i = 0; i < npages; i++) {
740 		if (!omap_obj->dma_addrs[i]) {
741 			dma_addr_t addr;
742 
743 			addr = dma_map_page(dev->dev, pages[i], 0,
744 					    PAGE_SIZE, dir);
745 			if (dma_mapping_error(dev->dev, addr)) {
746 				dev_warn(dev->dev, "%s: failed to map page\n",
747 					__func__);
748 				break;
749 			}
750 
751 			dirty = true;
752 			omap_obj->dma_addrs[i] = addr;
753 		}
754 	}
755 
756 	if (dirty) {
757 		unmap_mapping_range(obj->filp->f_mapping, 0,
758 				    omap_gem_mmap_size(obj), 1);
759 	}
760 }
761 
762 /**
763  * omap_gem_pin() - Pin a GEM object in memory
764  * @obj: the GEM object
765  * @dma_addr: the DMA address
766  *
767  * Pin the given GEM object in memory and fill the dma_addr pointer with the
768  * object's DMA address. If the buffer is not physically contiguous it will be
769  * remapped through the TILER to provide a contiguous view.
770  *
771  * Pins are reference-counted, calling this function multiple times is allowed
772  * as long the corresponding omap_gem_unpin() calls are balanced.
773  *
774  * Return 0 on success or a negative error code otherwise.
775  */
776 int omap_gem_pin(struct drm_gem_object *obj, dma_addr_t *dma_addr)
777 {
778 	struct omap_drm_private *priv = obj->dev->dev_private;
779 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
780 	int ret = 0;
781 
782 	mutex_lock(&omap_obj->lock);
783 
784 	if (!omap_gem_is_contiguous(omap_obj) && priv->has_dmm) {
785 		if (omap_obj->dma_addr_cnt == 0) {
786 			u32 npages = obj->size >> PAGE_SHIFT;
787 			enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
788 			struct tiler_block *block;
789 
790 			BUG_ON(omap_obj->block);
791 
792 			ret = omap_gem_attach_pages(obj);
793 			if (ret)
794 				goto fail;
795 
796 			if (omap_obj->flags & OMAP_BO_TILED) {
797 				block = tiler_reserve_2d(fmt,
798 						omap_obj->width,
799 						omap_obj->height, 0);
800 			} else {
801 				block = tiler_reserve_1d(obj->size);
802 			}
803 
804 			if (IS_ERR(block)) {
805 				ret = PTR_ERR(block);
806 				dev_err(obj->dev->dev,
807 					"could not remap: %d (%d)\n", ret, fmt);
808 				goto fail;
809 			}
810 
811 			/* TODO: enable async refill.. */
812 			ret = tiler_pin(block, omap_obj->pages, npages,
813 					omap_obj->roll, true);
814 			if (ret) {
815 				tiler_release(block);
816 				dev_err(obj->dev->dev,
817 						"could not pin: %d\n", ret);
818 				goto fail;
819 			}
820 
821 			omap_obj->dma_addr = tiler_ssptr(block);
822 			omap_obj->block = block;
823 
824 			DBG("got dma address: %pad", &omap_obj->dma_addr);
825 		}
826 
827 		omap_obj->dma_addr_cnt++;
828 
829 		*dma_addr = omap_obj->dma_addr;
830 	} else if (omap_gem_is_contiguous(omap_obj)) {
831 		*dma_addr = omap_obj->dma_addr;
832 	} else {
833 		ret = -EINVAL;
834 		goto fail;
835 	}
836 
837 fail:
838 	mutex_unlock(&omap_obj->lock);
839 
840 	return ret;
841 }
842 
843 /**
844  * omap_gem_unpin() - Unpin a GEM object from memory
845  * @obj: the GEM object
846  *
847  * Unpin the given GEM object previously pinned with omap_gem_pin(). Pins are
848  * reference-counted, the actualy unpin will only be performed when the number
849  * of calls to this function matches the number of calls to omap_gem_pin().
850  */
851 void omap_gem_unpin(struct drm_gem_object *obj)
852 {
853 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
854 	int ret;
855 
856 	mutex_lock(&omap_obj->lock);
857 
858 	if (omap_obj->dma_addr_cnt > 0) {
859 		omap_obj->dma_addr_cnt--;
860 		if (omap_obj->dma_addr_cnt == 0) {
861 			ret = tiler_unpin(omap_obj->block);
862 			if (ret) {
863 				dev_err(obj->dev->dev,
864 					"could not unpin pages: %d\n", ret);
865 			}
866 			ret = tiler_release(omap_obj->block);
867 			if (ret) {
868 				dev_err(obj->dev->dev,
869 					"could not release unmap: %d\n", ret);
870 			}
871 			omap_obj->dma_addr = 0;
872 			omap_obj->block = NULL;
873 		}
874 	}
875 
876 	mutex_unlock(&omap_obj->lock);
877 }
878 
879 /* Get rotated scanout address (only valid if already pinned), at the
880  * specified orientation and x,y offset from top-left corner of buffer
881  * (only valid for tiled 2d buffers)
882  */
883 int omap_gem_rotated_dma_addr(struct drm_gem_object *obj, u32 orient,
884 		int x, int y, dma_addr_t *dma_addr)
885 {
886 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
887 	int ret = -EINVAL;
888 
889 	mutex_lock(&omap_obj->lock);
890 
891 	if ((omap_obj->dma_addr_cnt > 0) && omap_obj->block &&
892 			(omap_obj->flags & OMAP_BO_TILED)) {
893 		*dma_addr = tiler_tsptr(omap_obj->block, orient, x, y);
894 		ret = 0;
895 	}
896 
897 	mutex_unlock(&omap_obj->lock);
898 
899 	return ret;
900 }
901 
902 /* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
903 int omap_gem_tiled_stride(struct drm_gem_object *obj, u32 orient)
904 {
905 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
906 	int ret = -EINVAL;
907 	if (omap_obj->flags & OMAP_BO_TILED)
908 		ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
909 	return ret;
910 }
911 
912 /* if !remap, and we don't have pages backing, then fail, rather than
913  * increasing the pin count (which we don't really do yet anyways,
914  * because we don't support swapping pages back out).  And 'remap'
915  * might not be quite the right name, but I wanted to keep it working
916  * similarly to omap_gem_pin().  Note though that mutex is not
917  * aquired if !remap (because this can be called in atomic ctxt),
918  * but probably omap_gem_unpin() should be changed to work in the
919  * same way.  If !remap, a matching omap_gem_put_pages() call is not
920  * required (and should not be made).
921  */
922 int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
923 		bool remap)
924 {
925 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
926 	int ret = 0;
927 
928 	mutex_lock(&omap_obj->lock);
929 
930 	if (remap) {
931 		ret = omap_gem_attach_pages(obj);
932 		if (ret)
933 			goto unlock;
934 	}
935 
936 	if (!omap_obj->pages) {
937 		ret = -ENOMEM;
938 		goto unlock;
939 	}
940 
941 	*pages = omap_obj->pages;
942 
943 unlock:
944 	mutex_unlock(&omap_obj->lock);
945 
946 	return ret;
947 }
948 
949 /* release pages when DMA no longer being performed */
950 int omap_gem_put_pages(struct drm_gem_object *obj)
951 {
952 	/* do something here if we dynamically attach/detach pages.. at
953 	 * least they would no longer need to be pinned if everyone has
954 	 * released the pages..
955 	 */
956 	return 0;
957 }
958 
959 #ifdef CONFIG_DRM_FBDEV_EMULATION
960 /*
961  * Get kernel virtual address for CPU access.. this more or less only
962  * exists for omap_fbdev.
963  */
964 void *omap_gem_vaddr(struct drm_gem_object *obj)
965 {
966 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
967 	void *vaddr;
968 	int ret;
969 
970 	mutex_lock(&omap_obj->lock);
971 
972 	if (!omap_obj->vaddr) {
973 		ret = omap_gem_attach_pages(obj);
974 		if (ret) {
975 			vaddr = ERR_PTR(ret);
976 			goto unlock;
977 		}
978 
979 		omap_obj->vaddr = vmap(omap_obj->pages, obj->size >> PAGE_SHIFT,
980 				VM_MAP, pgprot_writecombine(PAGE_KERNEL));
981 	}
982 
983 	vaddr = omap_obj->vaddr;
984 
985 unlock:
986 	mutex_unlock(&omap_obj->lock);
987 	return vaddr;
988 }
989 #endif
990 
991 /* -----------------------------------------------------------------------------
992  * Power Management
993  */
994 
995 #ifdef CONFIG_PM
996 /* re-pin objects in DMM in resume path: */
997 int omap_gem_resume(struct drm_device *dev)
998 {
999 	struct omap_drm_private *priv = dev->dev_private;
1000 	struct omap_gem_object *omap_obj;
1001 	int ret = 0;
1002 
1003 	mutex_lock(&priv->list_lock);
1004 	list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
1005 		if (omap_obj->block) {
1006 			struct drm_gem_object *obj = &omap_obj->base;
1007 			u32 npages = obj->size >> PAGE_SHIFT;
1008 
1009 			WARN_ON(!omap_obj->pages);  /* this can't happen */
1010 			ret = tiler_pin(omap_obj->block,
1011 					omap_obj->pages, npages,
1012 					omap_obj->roll, true);
1013 			if (ret) {
1014 				dev_err(dev->dev, "could not repin: %d\n", ret);
1015 				goto done;
1016 			}
1017 		}
1018 	}
1019 
1020 done:
1021 	mutex_unlock(&priv->list_lock);
1022 	return ret;
1023 }
1024 #endif
1025 
1026 /* -----------------------------------------------------------------------------
1027  * DebugFS
1028  */
1029 
1030 #ifdef CONFIG_DEBUG_FS
1031 void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
1032 {
1033 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
1034 	u64 off;
1035 
1036 	off = drm_vma_node_start(&obj->vma_node);
1037 
1038 	mutex_lock(&omap_obj->lock);
1039 
1040 	seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d",
1041 			omap_obj->flags, obj->name, kref_read(&obj->refcount),
1042 			off, &omap_obj->dma_addr, omap_obj->dma_addr_cnt,
1043 			omap_obj->vaddr, omap_obj->roll);
1044 
1045 	if (omap_obj->flags & OMAP_BO_TILED) {
1046 		seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
1047 		if (omap_obj->block) {
1048 			struct tcm_area *area = &omap_obj->block->area;
1049 			seq_printf(m, " (%dx%d, %dx%d)",
1050 					area->p0.x, area->p0.y,
1051 					area->p1.x, area->p1.y);
1052 		}
1053 	} else {
1054 		seq_printf(m, " %zu", obj->size);
1055 	}
1056 
1057 	mutex_unlock(&omap_obj->lock);
1058 
1059 	seq_printf(m, "\n");
1060 }
1061 
1062 void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
1063 {
1064 	struct omap_gem_object *omap_obj;
1065 	int count = 0;
1066 	size_t size = 0;
1067 
1068 	list_for_each_entry(omap_obj, list, mm_list) {
1069 		struct drm_gem_object *obj = &omap_obj->base;
1070 		seq_printf(m, "   ");
1071 		omap_gem_describe(obj, m);
1072 		count++;
1073 		size += obj->size;
1074 	}
1075 
1076 	seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
1077 }
1078 #endif
1079 
1080 /* -----------------------------------------------------------------------------
1081  * Constructor & Destructor
1082  */
1083 
1084 void omap_gem_free_object(struct drm_gem_object *obj)
1085 {
1086 	struct drm_device *dev = obj->dev;
1087 	struct omap_drm_private *priv = dev->dev_private;
1088 	struct omap_gem_object *omap_obj = to_omap_bo(obj);
1089 
1090 	omap_gem_evict(obj);
1091 
1092 	mutex_lock(&priv->list_lock);
1093 	list_del(&omap_obj->mm_list);
1094 	mutex_unlock(&priv->list_lock);
1095 
1096 	/*
1097 	 * We own the sole reference to the object at this point, but to keep
1098 	 * lockdep happy, we must still take the omap_obj_lock to call
1099 	 * omap_gem_detach_pages(). This should hardly make any difference as
1100 	 * there can't be any lock contention.
1101 	 */
1102 	mutex_lock(&omap_obj->lock);
1103 
1104 	/* The object should not be pinned. */
1105 	WARN_ON(omap_obj->dma_addr_cnt > 0);
1106 
1107 	if (omap_obj->pages) {
1108 		if (omap_obj->flags & OMAP_BO_MEM_DMABUF)
1109 			kfree(omap_obj->pages);
1110 		else
1111 			omap_gem_detach_pages(obj);
1112 	}
1113 
1114 	if (omap_obj->flags & OMAP_BO_MEM_DMA_API) {
1115 		dma_free_wc(dev->dev, obj->size, omap_obj->vaddr,
1116 			    omap_obj->dma_addr);
1117 	} else if (omap_obj->vaddr) {
1118 		vunmap(omap_obj->vaddr);
1119 	} else if (obj->import_attach) {
1120 		drm_prime_gem_destroy(obj, omap_obj->sgt);
1121 	}
1122 
1123 	mutex_unlock(&omap_obj->lock);
1124 
1125 	drm_gem_object_release(obj);
1126 
1127 	mutex_destroy(&omap_obj->lock);
1128 
1129 	kfree(omap_obj);
1130 }
1131 
1132 /* GEM buffer object constructor */
1133 struct drm_gem_object *omap_gem_new(struct drm_device *dev,
1134 		union omap_gem_size gsize, u32 flags)
1135 {
1136 	struct omap_drm_private *priv = dev->dev_private;
1137 	struct omap_gem_object *omap_obj;
1138 	struct drm_gem_object *obj;
1139 	struct address_space *mapping;
1140 	size_t size;
1141 	int ret;
1142 
1143 	/* Validate the flags and compute the memory and cache flags. */
1144 	if (flags & OMAP_BO_TILED) {
1145 		if (!priv->usergart) {
1146 			dev_err(dev->dev, "Tiled buffers require DMM\n");
1147 			return NULL;
1148 		}
1149 
1150 		/*
1151 		 * Tiled buffers are always shmem paged backed. When they are
1152 		 * scanned out, they are remapped into DMM/TILER.
1153 		 */
1154 		flags &= ~OMAP_BO_SCANOUT;
1155 		flags |= OMAP_BO_MEM_SHMEM;
1156 
1157 		/*
1158 		 * Currently don't allow cached buffers. There is some caching
1159 		 * stuff that needs to be handled better.
1160 		 */
1161 		flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED);
1162 		flags |= tiler_get_cpu_cache_flags();
1163 	} else if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
1164 		/*
1165 		 * OMAP_BO_SCANOUT hints that the buffer doesn't need to be
1166 		 * tiled. However, to lower the pressure on memory allocation,
1167 		 * use contiguous memory only if no TILER is available.
1168 		 */
1169 		flags |= OMAP_BO_MEM_DMA_API;
1170 	} else if (!(flags & OMAP_BO_MEM_DMABUF)) {
1171 		/*
1172 		 * All other buffers not backed by dma_buf are shmem-backed.
1173 		 */
1174 		flags |= OMAP_BO_MEM_SHMEM;
1175 	}
1176 
1177 	/* Allocate the initialize the OMAP GEM object. */
1178 	omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
1179 	if (!omap_obj)
1180 		return NULL;
1181 
1182 	obj = &omap_obj->base;
1183 	omap_obj->flags = flags;
1184 	mutex_init(&omap_obj->lock);
1185 
1186 	if (flags & OMAP_BO_TILED) {
1187 		/*
1188 		 * For tiled buffers align dimensions to slot boundaries and
1189 		 * calculate size based on aligned dimensions.
1190 		 */
1191 		tiler_align(gem2fmt(flags), &gsize.tiled.width,
1192 			    &gsize.tiled.height);
1193 
1194 		size = tiler_size(gem2fmt(flags), gsize.tiled.width,
1195 				  gsize.tiled.height);
1196 
1197 		omap_obj->width = gsize.tiled.width;
1198 		omap_obj->height = gsize.tiled.height;
1199 	} else {
1200 		size = PAGE_ALIGN(gsize.bytes);
1201 	}
1202 
1203 	/* Initialize the GEM object. */
1204 	if (!(flags & OMAP_BO_MEM_SHMEM)) {
1205 		drm_gem_private_object_init(dev, obj, size);
1206 	} else {
1207 		ret = drm_gem_object_init(dev, obj, size);
1208 		if (ret)
1209 			goto err_free;
1210 
1211 		mapping = obj->filp->f_mapping;
1212 		mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32);
1213 	}
1214 
1215 	/* Allocate memory if needed. */
1216 	if (flags & OMAP_BO_MEM_DMA_API) {
1217 		omap_obj->vaddr = dma_alloc_wc(dev->dev, size,
1218 					       &omap_obj->dma_addr,
1219 					       GFP_KERNEL);
1220 		if (!omap_obj->vaddr)
1221 			goto err_release;
1222 	}
1223 
1224 	mutex_lock(&priv->list_lock);
1225 	list_add(&omap_obj->mm_list, &priv->obj_list);
1226 	mutex_unlock(&priv->list_lock);
1227 
1228 	return obj;
1229 
1230 err_release:
1231 	drm_gem_object_release(obj);
1232 err_free:
1233 	kfree(omap_obj);
1234 	return NULL;
1235 }
1236 
1237 struct drm_gem_object *omap_gem_new_dmabuf(struct drm_device *dev, size_t size,
1238 					   struct sg_table *sgt)
1239 {
1240 	struct omap_drm_private *priv = dev->dev_private;
1241 	struct omap_gem_object *omap_obj;
1242 	struct drm_gem_object *obj;
1243 	union omap_gem_size gsize;
1244 
1245 	/* Without a DMM only physically contiguous buffers can be supported. */
1246 	if (sgt->orig_nents != 1 && !priv->has_dmm)
1247 		return ERR_PTR(-EINVAL);
1248 
1249 	gsize.bytes = PAGE_ALIGN(size);
1250 	obj = omap_gem_new(dev, gsize, OMAP_BO_MEM_DMABUF | OMAP_BO_WC);
1251 	if (!obj)
1252 		return ERR_PTR(-ENOMEM);
1253 
1254 	omap_obj = to_omap_bo(obj);
1255 
1256 	mutex_lock(&omap_obj->lock);
1257 
1258 	omap_obj->sgt = sgt;
1259 
1260 	if (sgt->orig_nents == 1) {
1261 		omap_obj->dma_addr = sg_dma_address(sgt->sgl);
1262 	} else {
1263 		/* Create pages list from sgt */
1264 		struct sg_page_iter iter;
1265 		struct page **pages;
1266 		unsigned int npages;
1267 		unsigned int i = 0;
1268 
1269 		npages = DIV_ROUND_UP(size, PAGE_SIZE);
1270 		pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1271 		if (!pages) {
1272 			omap_gem_free_object(obj);
1273 			obj = ERR_PTR(-ENOMEM);
1274 			goto done;
1275 		}
1276 
1277 		omap_obj->pages = pages;
1278 
1279 		for_each_sg_page(sgt->sgl, &iter, sgt->orig_nents, 0) {
1280 			pages[i++] = sg_page_iter_page(&iter);
1281 			if (i > npages)
1282 				break;
1283 		}
1284 
1285 		if (WARN_ON(i != npages)) {
1286 			omap_gem_free_object(obj);
1287 			obj = ERR_PTR(-ENOMEM);
1288 			goto done;
1289 		}
1290 	}
1291 
1292 done:
1293 	mutex_unlock(&omap_obj->lock);
1294 	return obj;
1295 }
1296 
1297 /* convenience method to construct a GEM buffer object, and userspace handle */
1298 int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1299 		union omap_gem_size gsize, u32 flags, u32 *handle)
1300 {
1301 	struct drm_gem_object *obj;
1302 	int ret;
1303 
1304 	obj = omap_gem_new(dev, gsize, flags);
1305 	if (!obj)
1306 		return -ENOMEM;
1307 
1308 	ret = drm_gem_handle_create(file, obj, handle);
1309 	if (ret) {
1310 		omap_gem_free_object(obj);
1311 		return ret;
1312 	}
1313 
1314 	/* drop reference from allocate - handle holds it now */
1315 	drm_gem_object_put_unlocked(obj);
1316 
1317 	return 0;
1318 }
1319 
1320 /* -----------------------------------------------------------------------------
1321  * Init & Cleanup
1322  */
1323 
1324 /* If DMM is used, we need to set some stuff up.. */
1325 void omap_gem_init(struct drm_device *dev)
1326 {
1327 	struct omap_drm_private *priv = dev->dev_private;
1328 	struct omap_drm_usergart *usergart;
1329 	const enum tiler_fmt fmts[] = {
1330 			TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
1331 	};
1332 	int i, j;
1333 
1334 	if (!dmm_is_available()) {
1335 		/* DMM only supported on OMAP4 and later, so this isn't fatal */
1336 		dev_warn(dev->dev, "DMM not available, disable DMM support\n");
1337 		return;
1338 	}
1339 
1340 	usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
1341 	if (!usergart)
1342 		return;
1343 
1344 	/* reserve 4k aligned/wide regions for userspace mappings: */
1345 	for (i = 0; i < ARRAY_SIZE(fmts); i++) {
1346 		u16 h = 1, w = PAGE_SIZE >> i;
1347 
1348 		tiler_align(fmts[i], &w, &h);
1349 		/* note: since each region is 1 4kb page wide, and minimum
1350 		 * number of rows, the height ends up being the same as the
1351 		 * # of pages in the region
1352 		 */
1353 		usergart[i].height = h;
1354 		usergart[i].height_shift = ilog2(h);
1355 		usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
1356 		usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
1357 		for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
1358 			struct omap_drm_usergart_entry *entry;
1359 			struct tiler_block *block;
1360 
1361 			entry = &usergart[i].entry[j];
1362 			block = tiler_reserve_2d(fmts[i], w, h, PAGE_SIZE);
1363 			if (IS_ERR(block)) {
1364 				dev_err(dev->dev,
1365 						"reserve failed: %d, %d, %ld\n",
1366 						i, j, PTR_ERR(block));
1367 				return;
1368 			}
1369 			entry->dma_addr = tiler_ssptr(block);
1370 			entry->block = block;
1371 
1372 			DBG("%d:%d: %dx%d: dma_addr=%pad stride=%d", i, j, w, h,
1373 					&entry->dma_addr,
1374 					usergart[i].stride_pfn << PAGE_SHIFT);
1375 		}
1376 	}
1377 
1378 	priv->usergart = usergart;
1379 	priv->has_dmm = true;
1380 }
1381 
1382 void omap_gem_deinit(struct drm_device *dev)
1383 {
1384 	struct omap_drm_private *priv = dev->dev_private;
1385 
1386 	/* I believe we can rely on there being no more outstanding GEM
1387 	 * objects which could depend on usergart/dmm at this point.
1388 	 */
1389 	kfree(priv->usergart);
1390 }
1391