2 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
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.
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
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/>.
18 #include <linux/seq_file.h>
19 #include <linux/shmem_fs.h>
20 #include <linux/spinlock.h>
21 #include <linux/pfn_t.h>
23 #include <drm/drm_vma_manager.h>
26 #include "omap_dmm_tiler.h"
29 * GEM buffer object implementation.
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 */
37 struct omap_gem_object {
38 struct drm_gem_object base;
40 struct list_head mm_list;
44 /** width/height for tiled formats (rounded up to slot boundaries) */
47 /** roll applied when mapping to DMM */
51 * dma_addr contains the buffer DMA address. It is valid for
53 * - buffers allocated through the DMA mapping API (with the
54 * OMAP_BO_MEM_DMA_API flag set)
56 * - buffers imported from dmabuf (with the OMAP_BO_MEM_DMABUF flag set)
57 * if they are physically contiguous (when sgt->orig_nents == 1)
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
62 * Physically contiguous buffers have their DMA address equal to the
63 * physical address as we don't remap those buffers through the TILER.
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().
74 * # of users of dma_addr
79 * If the buffer has been imported from a dmabuf the OMAP_DB_DMABUF flag
80 * is set and the sgt field is valid.
85 * tiler block used when buffer is remapped in DMM/TILER.
87 struct tiler_block *block;
90 * Array of backing pages, if allocated. Note that pages are never
91 * allocated for buffers originally allocated from contiguous memory
95 /** addresses corresponding to pages in above array */
96 dma_addr_t *dma_addrs;
99 * Virtual address, if mapped.
104 #define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
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.
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
117 #define NUM_USERGART_ENTRIES 2
118 struct omap_drm_usergart_entry {
119 struct tiler_block *block; /* the reserved tiler block */
121 struct drm_gem_object *obj; /* the current pinned obj */
122 pgoff_t obj_pgoff; /* page offset of obj currently
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 */
135 /* -----------------------------------------------------------------------------
139 /** get mmap offset */
140 u64 omap_gem_mmap_offset(struct drm_gem_object *obj)
142 struct drm_device *dev = obj->dev;
146 /* Make it mmapable */
147 size = omap_gem_mmap_size(obj);
148 ret = drm_gem_create_mmap_offset_size(obj, size);
150 dev_err(dev->dev, "could not allocate mmap offset\n");
154 return drm_vma_node_offset_addr(&obj->vma_node);
157 static bool omap_gem_is_contiguous(struct omap_gem_object *omap_obj)
159 if (omap_obj->flags & OMAP_BO_MEM_DMA_API)
162 if ((omap_obj->flags & OMAP_BO_MEM_DMABUF) && omap_obj->sgt->nents == 1)
168 /* -----------------------------------------------------------------------------
172 static void omap_gem_evict_entry(struct drm_gem_object *obj,
173 enum tiler_fmt fmt, struct omap_drm_usergart_entry *entry)
175 struct omap_gem_object *omap_obj = to_omap_bo(obj);
176 struct omap_drm_private *priv = obj->dev->dev_private;
177 int n = priv->usergart[fmt].height;
178 size_t size = PAGE_SIZE * n;
179 loff_t off = omap_gem_mmap_offset(obj) +
180 (entry->obj_pgoff << PAGE_SHIFT);
181 const int m = DIV_ROUND_UP(omap_obj->width << fmt, PAGE_SIZE);
185 /* if stride > than PAGE_SIZE then sparse mapping: */
186 for (i = n; i > 0; i--) {
187 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
189 off += PAGE_SIZE * m;
192 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
199 /* Evict a buffer from usergart, if it is mapped there */
200 static void omap_gem_evict(struct drm_gem_object *obj)
202 struct omap_gem_object *omap_obj = to_omap_bo(obj);
203 struct omap_drm_private *priv = obj->dev->dev_private;
205 if (omap_obj->flags & OMAP_BO_TILED) {
206 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
209 for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
210 struct omap_drm_usergart_entry *entry =
211 &priv->usergart[fmt].entry[i];
213 if (entry->obj == obj)
214 omap_gem_evict_entry(obj, fmt, entry);
219 /* -----------------------------------------------------------------------------
223 /* Ensure backing pages are allocated. */
224 static int omap_gem_attach_pages(struct drm_gem_object *obj)
226 struct drm_device *dev = obj->dev;
227 struct omap_gem_object *omap_obj = to_omap_bo(obj);
229 int npages = obj->size >> PAGE_SHIFT;
234 * If not using shmem (in which case backing pages don't need to be
235 * allocated) or if pages are already allocated we're done.
237 if (!(omap_obj->flags & OMAP_BO_MEM_SHMEM) || omap_obj->pages)
240 pages = drm_gem_get_pages(obj);
242 dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
243 return PTR_ERR(pages);
246 /* for non-cached buffers, ensure the new pages are clean because
247 * DSS, GPU, etc. are not cache coherent:
249 if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
250 addrs = kmalloc_array(npages, sizeof(*addrs), GFP_KERNEL);
256 for (i = 0; i < npages; i++) {
257 addrs[i] = dma_map_page(dev->dev, pages[i],
258 0, PAGE_SIZE, DMA_TO_DEVICE);
260 if (dma_mapping_error(dev->dev, addrs[i])) {
262 "%s: failed to map page\n", __func__);
264 for (i = i - 1; i >= 0; --i) {
265 dma_unmap_page(dev->dev, addrs[i],
266 PAGE_SIZE, DMA_TO_DEVICE);
274 addrs = kcalloc(npages, sizeof(*addrs), GFP_KERNEL);
281 omap_obj->dma_addrs = addrs;
282 omap_obj->pages = pages;
289 drm_gem_put_pages(obj, pages, true, false);
294 /** release backing pages */
295 static void omap_gem_detach_pages(struct drm_gem_object *obj)
297 struct omap_gem_object *omap_obj = to_omap_bo(obj);
298 unsigned int npages = obj->size >> PAGE_SHIFT;
301 for (i = 0; i < npages; i++) {
302 if (omap_obj->dma_addrs[i])
303 dma_unmap_page(obj->dev->dev, omap_obj->dma_addrs[i],
304 PAGE_SIZE, DMA_TO_DEVICE);
307 kfree(omap_obj->dma_addrs);
308 omap_obj->dma_addrs = NULL;
310 drm_gem_put_pages(obj, omap_obj->pages, true, false);
311 omap_obj->pages = NULL;
314 /* get buffer flags */
315 u32 omap_gem_flags(struct drm_gem_object *obj)
317 return to_omap_bo(obj)->flags;
321 size_t omap_gem_mmap_size(struct drm_gem_object *obj)
323 struct omap_gem_object *omap_obj = to_omap_bo(obj);
324 size_t size = obj->size;
326 if (omap_obj->flags & OMAP_BO_TILED) {
327 /* for tiled buffers, the virtual size has stride rounded up
328 * to 4kb.. (to hide the fact that row n+1 might start 16kb or
329 * 32kb later!). But we don't back the entire buffer with
330 * pages, only the valid picture part.. so need to adjust for
331 * this in the size used to mmap and generate mmap offset
333 size = tiler_vsize(gem2fmt(omap_obj->flags),
334 omap_obj->width, omap_obj->height);
340 /* -----------------------------------------------------------------------------
344 /* Normal handling for the case of faulting in non-tiled buffers */
345 static vm_fault_t omap_gem_fault_1d(struct drm_gem_object *obj,
346 struct vm_area_struct *vma, struct vm_fault *vmf)
348 struct omap_gem_object *omap_obj = to_omap_bo(obj);
352 /* We don't use vmf->pgoff since that has the fake offset: */
353 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
355 if (omap_obj->pages) {
356 omap_gem_cpu_sync_page(obj, pgoff);
357 pfn = page_to_pfn(omap_obj->pages[pgoff]);
359 BUG_ON(!omap_gem_is_contiguous(omap_obj));
360 pfn = (omap_obj->dma_addr >> PAGE_SHIFT) + pgoff;
363 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
364 pfn, pfn << PAGE_SHIFT);
366 return vmf_insert_mixed(vma, vmf->address,
367 __pfn_to_pfn_t(pfn, PFN_DEV));
370 /* Special handling for the case of faulting in 2d tiled buffers */
371 static vm_fault_t omap_gem_fault_2d(struct drm_gem_object *obj,
372 struct vm_area_struct *vma, struct vm_fault *vmf)
374 struct omap_gem_object *omap_obj = to_omap_bo(obj);
375 struct omap_drm_private *priv = obj->dev->dev_private;
376 struct omap_drm_usergart_entry *entry;
377 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
378 struct page *pages[64]; /* XXX is this too much to have on stack? */
380 pgoff_t pgoff, base_pgoff;
383 vm_fault_t ret = VM_FAULT_NOPAGE;
386 * Note the height of the slot is also equal to the number of pages
387 * that need to be mapped in to fill 4kb wide CPU page. If the slot
388 * height is 64, then 64 pages fill a 4kb wide by 64 row region.
390 const int n = priv->usergart[fmt].height;
391 const int n_shift = priv->usergart[fmt].height_shift;
394 * If buffer width in bytes > PAGE_SIZE then the virtual stride is
395 * rounded up to next multiple of PAGE_SIZE.. this need to be taken
396 * into account in some of the math, so figure out virtual stride
399 const int m = DIV_ROUND_UP(omap_obj->width << fmt, PAGE_SIZE);
401 /* We don't use vmf->pgoff since that has the fake offset: */
402 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
405 * Actual address we start mapping at is rounded down to previous slot
406 * boundary in the y direction:
408 base_pgoff = round_down(pgoff, m << n_shift);
410 /* figure out buffer width in slots */
411 slots = omap_obj->width >> priv->usergart[fmt].slot_shift;
413 vaddr = vmf->address - ((pgoff - base_pgoff) << PAGE_SHIFT);
415 entry = &priv->usergart[fmt].entry[priv->usergart[fmt].last];
417 /* evict previous buffer using this usergart entry, if any: */
419 omap_gem_evict_entry(entry->obj, fmt, entry);
422 entry->obj_pgoff = base_pgoff;
424 /* now convert base_pgoff to phys offset from virt offset: */
425 base_pgoff = (base_pgoff >> n_shift) * slots;
427 /* for wider-than 4k.. figure out which part of the slot-row we want: */
430 entry->obj_pgoff += off;
432 slots = min(slots - (off << n_shift), n);
433 base_pgoff += off << n_shift;
434 vaddr += off << PAGE_SHIFT;
438 * Map in pages. Beyond the valid pixel part of the buffer, we set
439 * pages[i] to NULL to get a dummy page mapped in.. if someone
440 * reads/writes it they will get random/undefined content, but at
441 * least it won't be corrupting whatever other random page used to
442 * be mapped in, or other undefined behavior.
444 memcpy(pages, &omap_obj->pages[base_pgoff],
445 sizeof(struct page *) * slots);
446 memset(pages + slots, 0,
447 sizeof(struct page *) * (n - slots));
449 err = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
451 ret = vmf_error(err);
452 dev_err(obj->dev->dev, "failed to pin: %d\n", err);
456 pfn = entry->dma_addr >> PAGE_SHIFT;
458 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
459 pfn, pfn << PAGE_SHIFT);
461 for (i = n; i > 0; i--) {
462 ret = vmf_insert_mixed(vma,
463 vaddr, __pfn_to_pfn_t(pfn, PFN_DEV));
464 if (ret & VM_FAULT_ERROR)
466 pfn += priv->usergart[fmt].stride_pfn;
467 vaddr += PAGE_SIZE * m;
470 /* simple round-robin: */
471 priv->usergart[fmt].last = (priv->usergart[fmt].last + 1)
472 % NUM_USERGART_ENTRIES;
478 * omap_gem_fault - pagefault handler for GEM objects
481 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
482 * does most of the work for us including the actual map/unmap calls
483 * but we need to do the actual page work.
485 * The VMA was set up by GEM. In doing so it also ensured that the
486 * vma->vm_private_data points to the GEM object that is backing this
489 vm_fault_t omap_gem_fault(struct vm_fault *vmf)
491 struct vm_area_struct *vma = vmf->vma;
492 struct drm_gem_object *obj = vma->vm_private_data;
493 struct omap_gem_object *omap_obj = to_omap_bo(obj);
494 struct drm_device *dev = obj->dev;
498 /* Make sure we don't parallel update on a fault, nor move or remove
499 * something from beneath our feet
501 mutex_lock(&dev->struct_mutex);
503 /* if a shmem backed object, make sure we have pages attached now */
504 err = omap_gem_attach_pages(obj);
506 ret = vmf_error(err);
510 /* where should we do corresponding put_pages().. we are mapping
511 * the original page, rather than thru a GART, so we can't rely
512 * on eviction to trigger this. But munmap() or all mappings should
513 * probably trigger put_pages()?
516 if (omap_obj->flags & OMAP_BO_TILED)
517 ret = omap_gem_fault_2d(obj, vma, vmf);
519 ret = omap_gem_fault_1d(obj, vma, vmf);
523 mutex_unlock(&dev->struct_mutex);
527 /** We override mainly to fix up some of the vm mapping flags.. */
528 int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma)
532 ret = drm_gem_mmap(filp, vma);
534 DBG("mmap failed: %d", ret);
538 return omap_gem_mmap_obj(vma->vm_private_data, vma);
541 int omap_gem_mmap_obj(struct drm_gem_object *obj,
542 struct vm_area_struct *vma)
544 struct omap_gem_object *omap_obj = to_omap_bo(obj);
546 vma->vm_flags &= ~VM_PFNMAP;
547 vma->vm_flags |= VM_MIXEDMAP;
549 if (omap_obj->flags & OMAP_BO_WC) {
550 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
551 } else if (omap_obj->flags & OMAP_BO_UNCACHED) {
552 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
555 * We do have some private objects, at least for scanout buffers
556 * on hardware without DMM/TILER. But these are allocated write-
559 if (WARN_ON(!obj->filp))
563 * Shunt off cached objs to shmem file so they have their own
564 * address_space (so unmap_mapping_range does what we want,
565 * in particular in the case of mmap'd dmabufs)
569 vma->vm_file = get_file(obj->filp);
571 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
577 /* -----------------------------------------------------------------------------
582 * omap_gem_dumb_create - create a dumb buffer
583 * @drm_file: our client file
585 * @args: the requested arguments copied from userspace
587 * Allocate a buffer suitable for use for a frame buffer of the
588 * form described by user space. Give userspace a handle by which
591 int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
592 struct drm_mode_create_dumb *args)
594 union omap_gem_size gsize;
596 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
598 args->size = PAGE_ALIGN(args->pitch * args->height);
600 gsize = (union omap_gem_size){
604 return omap_gem_new_handle(dev, file, gsize,
605 OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle);
609 * omap_gem_dumb_map - buffer mapping for dumb interface
610 * @file: our drm client file
612 * @handle: GEM handle to the object (from dumb_create)
614 * Do the necessary setup to allow the mapping of the frame buffer
615 * into user memory. We don't have to do much here at the moment.
617 int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
618 u32 handle, u64 *offset)
620 struct drm_gem_object *obj;
623 /* GEM does all our handle to object mapping */
624 obj = drm_gem_object_lookup(file, handle);
630 *offset = omap_gem_mmap_offset(obj);
632 drm_gem_object_unreference_unlocked(obj);
638 #ifdef CONFIG_DRM_FBDEV_EMULATION
639 /* Set scrolling position. This allows us to implement fast scrolling
642 * Call only from non-atomic contexts.
644 int omap_gem_roll(struct drm_gem_object *obj, u32 roll)
646 struct omap_gem_object *omap_obj = to_omap_bo(obj);
647 u32 npages = obj->size >> PAGE_SHIFT;
651 dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
655 omap_obj->roll = roll;
657 mutex_lock(&obj->dev->struct_mutex);
659 /* if we aren't mapped yet, we don't need to do anything */
660 if (omap_obj->block) {
661 ret = omap_gem_attach_pages(obj);
665 ret = tiler_pin(omap_obj->block, omap_obj->pages, npages,
668 dev_err(obj->dev->dev, "could not repin: %d\n", ret);
672 mutex_unlock(&obj->dev->struct_mutex);
678 /* -----------------------------------------------------------------------------
679 * Memory Management & DMA Sync
683 * shmem buffers that are mapped cached are not coherent.
685 * We keep track of dirty pages using page faulting to perform cache management.
686 * When a page is mapped to the CPU in read/write mode the device can't access
687 * it and omap_obj->dma_addrs[i] is NULL. When a page is mapped to the device
688 * the omap_obj->dma_addrs[i] is set to the DMA address, and the page is
689 * unmapped from the CPU.
691 static inline bool omap_gem_is_cached_coherent(struct drm_gem_object *obj)
693 struct omap_gem_object *omap_obj = to_omap_bo(obj);
695 return !((omap_obj->flags & OMAP_BO_MEM_SHMEM) &&
696 ((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED));
699 /* Sync the buffer for CPU access.. note pages should already be
700 * attached, ie. omap_gem_get_pages()
702 void omap_gem_cpu_sync_page(struct drm_gem_object *obj, int pgoff)
704 struct drm_device *dev = obj->dev;
705 struct omap_gem_object *omap_obj = to_omap_bo(obj);
707 if (omap_gem_is_cached_coherent(obj))
710 if (omap_obj->dma_addrs[pgoff]) {
711 dma_unmap_page(dev->dev, omap_obj->dma_addrs[pgoff],
712 PAGE_SIZE, DMA_TO_DEVICE);
713 omap_obj->dma_addrs[pgoff] = 0;
717 /* sync the buffer for DMA access */
718 void omap_gem_dma_sync_buffer(struct drm_gem_object *obj,
719 enum dma_data_direction dir)
721 struct drm_device *dev = obj->dev;
722 struct omap_gem_object *omap_obj = to_omap_bo(obj);
723 int i, npages = obj->size >> PAGE_SHIFT;
724 struct page **pages = omap_obj->pages;
727 if (omap_gem_is_cached_coherent(obj))
730 for (i = 0; i < npages; i++) {
731 if (!omap_obj->dma_addrs[i]) {
734 addr = dma_map_page(dev->dev, pages[i], 0,
736 if (dma_mapping_error(dev->dev, addr)) {
737 dev_warn(dev->dev, "%s: failed to map page\n",
743 omap_obj->dma_addrs[i] = addr;
748 unmap_mapping_range(obj->filp->f_mapping, 0,
749 omap_gem_mmap_size(obj), 1);
754 * omap_gem_pin() - Pin a GEM object in memory
755 * @obj: the GEM object
756 * @dma_addr: the DMA address
758 * Pin the given GEM object in memory and fill the dma_addr pointer with the
759 * object's DMA address. If the buffer is not physically contiguous it will be
760 * remapped through the TILER to provide a contiguous view.
762 * Pins are reference-counted, calling this function multiple times is allowed
763 * as long the corresponding omap_gem_unpin() calls are balanced.
765 * Return 0 on success or a negative error code otherwise.
767 int omap_gem_pin(struct drm_gem_object *obj, dma_addr_t *dma_addr)
769 struct omap_drm_private *priv = obj->dev->dev_private;
770 struct omap_gem_object *omap_obj = to_omap_bo(obj);
773 mutex_lock(&obj->dev->struct_mutex);
775 if (!omap_gem_is_contiguous(omap_obj) && priv->has_dmm) {
776 if (omap_obj->dma_addr_cnt == 0) {
777 u32 npages = obj->size >> PAGE_SHIFT;
778 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
779 struct tiler_block *block;
781 BUG_ON(omap_obj->block);
783 ret = omap_gem_attach_pages(obj);
787 if (omap_obj->flags & OMAP_BO_TILED) {
788 block = tiler_reserve_2d(fmt,
790 omap_obj->height, 0);
792 block = tiler_reserve_1d(obj->size);
796 ret = PTR_ERR(block);
797 dev_err(obj->dev->dev,
798 "could not remap: %d (%d)\n", ret, fmt);
802 /* TODO: enable async refill.. */
803 ret = tiler_pin(block, omap_obj->pages, npages,
804 omap_obj->roll, true);
806 tiler_release(block);
807 dev_err(obj->dev->dev,
808 "could not pin: %d\n", ret);
812 omap_obj->dma_addr = tiler_ssptr(block);
813 omap_obj->block = block;
815 DBG("got dma address: %pad", &omap_obj->dma_addr);
818 omap_obj->dma_addr_cnt++;
820 *dma_addr = omap_obj->dma_addr;
821 } else if (omap_gem_is_contiguous(omap_obj)) {
822 *dma_addr = omap_obj->dma_addr;
829 mutex_unlock(&obj->dev->struct_mutex);
835 * omap_gem_unpin() - Unpin a GEM object from memory
836 * @obj: the GEM object
838 * Unpin the given GEM object previously pinned with omap_gem_pin(). Pins are
839 * reference-counted, the actualy unpin will only be performed when the number
840 * of calls to this function matches the number of calls to omap_gem_pin().
842 void omap_gem_unpin(struct drm_gem_object *obj)
844 struct omap_gem_object *omap_obj = to_omap_bo(obj);
847 mutex_lock(&obj->dev->struct_mutex);
848 if (omap_obj->dma_addr_cnt > 0) {
849 omap_obj->dma_addr_cnt--;
850 if (omap_obj->dma_addr_cnt == 0) {
851 ret = tiler_unpin(omap_obj->block);
853 dev_err(obj->dev->dev,
854 "could not unpin pages: %d\n", ret);
856 ret = tiler_release(omap_obj->block);
858 dev_err(obj->dev->dev,
859 "could not release unmap: %d\n", ret);
861 omap_obj->dma_addr = 0;
862 omap_obj->block = NULL;
866 mutex_unlock(&obj->dev->struct_mutex);
869 /* Get rotated scanout address (only valid if already pinned), at the
870 * specified orientation and x,y offset from top-left corner of buffer
871 * (only valid for tiled 2d buffers)
873 int omap_gem_rotated_dma_addr(struct drm_gem_object *obj, u32 orient,
874 int x, int y, dma_addr_t *dma_addr)
876 struct omap_gem_object *omap_obj = to_omap_bo(obj);
879 mutex_lock(&obj->dev->struct_mutex);
880 if ((omap_obj->dma_addr_cnt > 0) && omap_obj->block &&
881 (omap_obj->flags & OMAP_BO_TILED)) {
882 *dma_addr = tiler_tsptr(omap_obj->block, orient, x, y);
885 mutex_unlock(&obj->dev->struct_mutex);
889 /* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
890 int omap_gem_tiled_stride(struct drm_gem_object *obj, u32 orient)
892 struct omap_gem_object *omap_obj = to_omap_bo(obj);
894 if (omap_obj->flags & OMAP_BO_TILED)
895 ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
899 /* if !remap, and we don't have pages backing, then fail, rather than
900 * increasing the pin count (which we don't really do yet anyways,
901 * because we don't support swapping pages back out). And 'remap'
902 * might not be quite the right name, but I wanted to keep it working
903 * similarly to omap_gem_pin(). Note though that mutex is not
904 * aquired if !remap (because this can be called in atomic ctxt),
905 * but probably omap_gem_unpin() should be changed to work in the
906 * same way. If !remap, a matching omap_gem_put_pages() call is not
907 * required (and should not be made).
909 int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
912 struct omap_gem_object *omap_obj = to_omap_bo(obj);
916 if (!omap_obj->pages)
918 *pages = omap_obj->pages;
921 mutex_lock(&obj->dev->struct_mutex);
922 ret = omap_gem_attach_pages(obj);
923 *pages = omap_obj->pages;
924 mutex_unlock(&obj->dev->struct_mutex);
928 /* release pages when DMA no longer being performed */
929 int omap_gem_put_pages(struct drm_gem_object *obj)
931 /* do something here if we dynamically attach/detach pages.. at
932 * least they would no longer need to be pinned if everyone has
933 * released the pages..
938 #ifdef CONFIG_DRM_FBDEV_EMULATION
939 /* Get kernel virtual address for CPU access.. this more or less only
940 * exists for omap_fbdev. This should be called with struct_mutex
943 void *omap_gem_vaddr(struct drm_gem_object *obj)
945 struct omap_gem_object *omap_obj = to_omap_bo(obj);
946 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
947 if (!omap_obj->vaddr) {
950 ret = omap_gem_attach_pages(obj);
953 omap_obj->vaddr = vmap(omap_obj->pages, obj->size >> PAGE_SHIFT,
954 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
956 return omap_obj->vaddr;
960 /* -----------------------------------------------------------------------------
965 /* re-pin objects in DMM in resume path: */
966 int omap_gem_resume(struct drm_device *dev)
968 struct omap_drm_private *priv = dev->dev_private;
969 struct omap_gem_object *omap_obj;
972 list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
973 if (omap_obj->block) {
974 struct drm_gem_object *obj = &omap_obj->base;
975 u32 npages = obj->size >> PAGE_SHIFT;
977 WARN_ON(!omap_obj->pages); /* this can't happen */
978 ret = tiler_pin(omap_obj->block,
979 omap_obj->pages, npages,
980 omap_obj->roll, true);
982 dev_err(dev->dev, "could not repin: %d\n", ret);
992 /* -----------------------------------------------------------------------------
996 #ifdef CONFIG_DEBUG_FS
997 void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
999 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1002 off = drm_vma_node_start(&obj->vma_node);
1004 seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d",
1005 omap_obj->flags, obj->name, kref_read(&obj->refcount),
1006 off, &omap_obj->dma_addr, omap_obj->dma_addr_cnt,
1007 omap_obj->vaddr, omap_obj->roll);
1009 if (omap_obj->flags & OMAP_BO_TILED) {
1010 seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
1011 if (omap_obj->block) {
1012 struct tcm_area *area = &omap_obj->block->area;
1013 seq_printf(m, " (%dx%d, %dx%d)",
1014 area->p0.x, area->p0.y,
1015 area->p1.x, area->p1.y);
1018 seq_printf(m, " %zu", obj->size);
1021 seq_printf(m, "\n");
1024 void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
1026 struct omap_gem_object *omap_obj;
1030 list_for_each_entry(omap_obj, list, mm_list) {
1031 struct drm_gem_object *obj = &omap_obj->base;
1033 omap_gem_describe(obj, m);
1038 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
1042 /* -----------------------------------------------------------------------------
1043 * Constructor & Destructor
1046 void omap_gem_free_object(struct drm_gem_object *obj)
1048 struct drm_device *dev = obj->dev;
1049 struct omap_drm_private *priv = dev->dev_private;
1050 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1052 omap_gem_evict(obj);
1054 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
1056 spin_lock(&priv->list_lock);
1057 list_del(&omap_obj->mm_list);
1058 spin_unlock(&priv->list_lock);
1060 /* this means the object is still pinned.. which really should
1061 * not happen. I think..
1063 WARN_ON(omap_obj->dma_addr_cnt > 0);
1065 if (omap_obj->pages) {
1066 if (omap_obj->flags & OMAP_BO_MEM_DMABUF)
1067 kfree(omap_obj->pages);
1069 omap_gem_detach_pages(obj);
1072 if (omap_obj->flags & OMAP_BO_MEM_DMA_API) {
1073 dma_free_wc(dev->dev, obj->size, omap_obj->vaddr,
1074 omap_obj->dma_addr);
1075 } else if (omap_obj->vaddr) {
1076 vunmap(omap_obj->vaddr);
1077 } else if (obj->import_attach) {
1078 drm_prime_gem_destroy(obj, omap_obj->sgt);
1081 drm_gem_object_release(obj);
1086 /* GEM buffer object constructor */
1087 struct drm_gem_object *omap_gem_new(struct drm_device *dev,
1088 union omap_gem_size gsize, u32 flags)
1090 struct omap_drm_private *priv = dev->dev_private;
1091 struct omap_gem_object *omap_obj;
1092 struct drm_gem_object *obj;
1093 struct address_space *mapping;
1097 /* Validate the flags and compute the memory and cache flags. */
1098 if (flags & OMAP_BO_TILED) {
1099 if (!priv->usergart) {
1100 dev_err(dev->dev, "Tiled buffers require DMM\n");
1105 * Tiled buffers are always shmem paged backed. When they are
1106 * scanned out, they are remapped into DMM/TILER.
1108 flags &= ~OMAP_BO_SCANOUT;
1109 flags |= OMAP_BO_MEM_SHMEM;
1112 * Currently don't allow cached buffers. There is some caching
1113 * stuff that needs to be handled better.
1115 flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED);
1116 flags |= tiler_get_cpu_cache_flags();
1117 } else if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
1119 * OMAP_BO_SCANOUT hints that the buffer doesn't need to be
1120 * tiled. However, to lower the pressure on memory allocation,
1121 * use contiguous memory only if no TILER is available.
1123 flags |= OMAP_BO_MEM_DMA_API;
1124 } else if (!(flags & OMAP_BO_MEM_DMABUF)) {
1126 * All other buffers not backed by dma_buf are shmem-backed.
1128 flags |= OMAP_BO_MEM_SHMEM;
1131 /* Allocate the initialize the OMAP GEM object. */
1132 omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
1136 obj = &omap_obj->base;
1137 omap_obj->flags = flags;
1139 if (flags & OMAP_BO_TILED) {
1141 * For tiled buffers align dimensions to slot boundaries and
1142 * calculate size based on aligned dimensions.
1144 tiler_align(gem2fmt(flags), &gsize.tiled.width,
1145 &gsize.tiled.height);
1147 size = tiler_size(gem2fmt(flags), gsize.tiled.width,
1148 gsize.tiled.height);
1150 omap_obj->width = gsize.tiled.width;
1151 omap_obj->height = gsize.tiled.height;
1153 size = PAGE_ALIGN(gsize.bytes);
1156 /* Initialize the GEM object. */
1157 if (!(flags & OMAP_BO_MEM_SHMEM)) {
1158 drm_gem_private_object_init(dev, obj, size);
1160 ret = drm_gem_object_init(dev, obj, size);
1164 mapping = obj->filp->f_mapping;
1165 mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32);
1168 /* Allocate memory if needed. */
1169 if (flags & OMAP_BO_MEM_DMA_API) {
1170 omap_obj->vaddr = dma_alloc_wc(dev->dev, size,
1171 &omap_obj->dma_addr,
1173 if (!omap_obj->vaddr)
1177 spin_lock(&priv->list_lock);
1178 list_add(&omap_obj->mm_list, &priv->obj_list);
1179 spin_unlock(&priv->list_lock);
1184 drm_gem_object_release(obj);
1190 struct drm_gem_object *omap_gem_new_dmabuf(struct drm_device *dev, size_t size,
1191 struct sg_table *sgt)
1193 struct omap_drm_private *priv = dev->dev_private;
1194 struct omap_gem_object *omap_obj;
1195 struct drm_gem_object *obj;
1196 union omap_gem_size gsize;
1198 /* Without a DMM only physically contiguous buffers can be supported. */
1199 if (sgt->orig_nents != 1 && !priv->has_dmm)
1200 return ERR_PTR(-EINVAL);
1202 mutex_lock(&dev->struct_mutex);
1204 gsize.bytes = PAGE_ALIGN(size);
1205 obj = omap_gem_new(dev, gsize, OMAP_BO_MEM_DMABUF | OMAP_BO_WC);
1207 obj = ERR_PTR(-ENOMEM);
1211 omap_obj = to_omap_bo(obj);
1212 omap_obj->sgt = sgt;
1214 if (sgt->orig_nents == 1) {
1215 omap_obj->dma_addr = sg_dma_address(sgt->sgl);
1217 /* Create pages list from sgt */
1218 struct sg_page_iter iter;
1219 struct page **pages;
1220 unsigned int npages;
1223 npages = DIV_ROUND_UP(size, PAGE_SIZE);
1224 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1226 omap_gem_free_object(obj);
1227 obj = ERR_PTR(-ENOMEM);
1231 omap_obj->pages = pages;
1233 for_each_sg_page(sgt->sgl, &iter, sgt->orig_nents, 0) {
1234 pages[i++] = sg_page_iter_page(&iter);
1239 if (WARN_ON(i != npages)) {
1240 omap_gem_free_object(obj);
1241 obj = ERR_PTR(-ENOMEM);
1247 mutex_unlock(&dev->struct_mutex);
1251 /* convenience method to construct a GEM buffer object, and userspace handle */
1252 int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1253 union omap_gem_size gsize, u32 flags, u32 *handle)
1255 struct drm_gem_object *obj;
1258 obj = omap_gem_new(dev, gsize, flags);
1262 ret = drm_gem_handle_create(file, obj, handle);
1264 omap_gem_free_object(obj);
1268 /* drop reference from allocate - handle holds it now */
1269 drm_gem_object_unreference_unlocked(obj);
1274 /* -----------------------------------------------------------------------------
1278 /* If DMM is used, we need to set some stuff up.. */
1279 void omap_gem_init(struct drm_device *dev)
1281 struct omap_drm_private *priv = dev->dev_private;
1282 struct omap_drm_usergart *usergart;
1283 const enum tiler_fmt fmts[] = {
1284 TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
1288 if (!dmm_is_available()) {
1289 /* DMM only supported on OMAP4 and later, so this isn't fatal */
1290 dev_warn(dev->dev, "DMM not available, disable DMM support\n");
1294 usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
1298 /* reserve 4k aligned/wide regions for userspace mappings: */
1299 for (i = 0; i < ARRAY_SIZE(fmts); i++) {
1300 u16 h = 1, w = PAGE_SIZE >> i;
1302 tiler_align(fmts[i], &w, &h);
1303 /* note: since each region is 1 4kb page wide, and minimum
1304 * number of rows, the height ends up being the same as the
1305 * # of pages in the region
1307 usergart[i].height = h;
1308 usergart[i].height_shift = ilog2(h);
1309 usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
1310 usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
1311 for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
1312 struct omap_drm_usergart_entry *entry;
1313 struct tiler_block *block;
1315 entry = &usergart[i].entry[j];
1316 block = tiler_reserve_2d(fmts[i], w, h, PAGE_SIZE);
1317 if (IS_ERR(block)) {
1319 "reserve failed: %d, %d, %ld\n",
1320 i, j, PTR_ERR(block));
1323 entry->dma_addr = tiler_ssptr(block);
1324 entry->block = block;
1326 DBG("%d:%d: %dx%d: dma_addr=%pad stride=%d", i, j, w, h,
1328 usergart[i].stride_pfn << PAGE_SHIFT);
1332 priv->usergart = usergart;
1333 priv->has_dmm = true;
1336 void omap_gem_deinit(struct drm_device *dev)
1338 struct omap_drm_private *priv = dev->dev_private;
1340 /* I believe we can rely on there being no more outstanding GEM
1341 * objects which could depend on usergart/dmm at this point.
1343 kfree(priv->usergart);