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