2 * drivers/gpu/drm/omapdrm/omap_gem.c
4 * Copyright (C) 2011 Texas Instruments
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/seq_file.h>
21 #include <linux/shmem_fs.h>
22 #include <linux/spinlock.h>
23 #include <linux/pfn_t.h>
25 #include <drm/drm_vma_manager.h>
28 #include "omap_dmm_tiler.h"
31 * GEM buffer object implementation.
34 /* note: we use upper 8 bits of flags for driver-internal flags: */
35 #define OMAP_BO_MEM_DMA_API 0x01000000 /* memory allocated with the dma_alloc_* API */
36 #define OMAP_BO_MEM_SHMEM 0x02000000 /* memory allocated through shmem backing */
37 #define OMAP_BO_MEM_DMABUF 0x08000000 /* memory imported from a dmabuf */
39 struct omap_gem_object {
40 struct drm_gem_object base;
42 struct list_head mm_list;
46 /** width/height for tiled formats (rounded up to slot boundaries) */
47 uint16_t width, height;
49 /** roll applied when mapping to DMM */
53 * paddr contains the buffer DMA address. It is valid for
55 * - buffers allocated through the DMA mapping API (with the
56 * OMAP_BO_MEM_DMA_API flag set)
58 * - buffers imported from dmabuf (with the OMAP_BO_MEM_DMABUF flag set)
59 * if they are physically contiguous (when sgt->orig_nents == 1)
61 * - buffers mapped through the TILER when paddr_cnt is not zero, in
62 * which case the DMA address points to the TILER aperture
64 * Physically contiguous buffers have their DMA address equal to the
65 * physical address as we don't remap those buffers through the TILER.
67 * Buffers mapped to the TILER have their DMA address pointing to the
68 * TILER aperture. As TILER mappings are refcounted (through paddr_cnt)
69 * the DMA address must be accessed through omap_get_get_paddr() to
70 * ensure that the mapping won't disappear unexpectedly. References must
71 * be released with omap_gem_put_paddr().
81 * If the buffer has been imported from a dmabuf the OMAP_DB_DMABUF flag
82 * is set and the sgt field is valid.
87 * tiler block used when buffer is remapped in DMM/TILER.
89 struct tiler_block *block;
92 * Array of backing pages, if allocated. Note that pages are never
93 * allocated for buffers originally allocated from contiguous memory
97 /** addresses corresponding to pages in above array */
101 * Virtual address, if mapped.
106 * sync-object allocated on demand (if needed)
108 * Per-buffer sync-object for tracking pending and completed hw/dma
109 * read and write operations.
112 uint32_t write_pending;
113 uint32_t write_complete;
114 uint32_t read_pending;
115 uint32_t read_complete;
119 #define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
121 /* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
122 * not necessarily pinned in TILER all the time, and (b) when they are
123 * they are not necessarily page aligned, we reserve one or more small
124 * regions in each of the 2d containers to use as a user-GART where we
125 * can create a second page-aligned mapping of parts of the buffer
126 * being accessed from userspace.
128 * Note that we could optimize slightly when we know that multiple
129 * tiler containers are backed by the same PAT.. but I'll leave that
132 #define NUM_USERGART_ENTRIES 2
133 struct omap_drm_usergart_entry {
134 struct tiler_block *block; /* the reserved tiler block */
136 struct drm_gem_object *obj; /* the current pinned obj */
137 pgoff_t obj_pgoff; /* page offset of obj currently
141 struct omap_drm_usergart {
142 struct omap_drm_usergart_entry entry[NUM_USERGART_ENTRIES];
143 int height; /* height in rows */
144 int height_shift; /* ilog2(height in rows) */
145 int slot_shift; /* ilog2(width per slot) */
146 int stride_pfn; /* stride in pages */
147 int last; /* index of last used entry */
150 /* -----------------------------------------------------------------------------
154 /** get mmap offset */
155 static uint64_t mmap_offset(struct drm_gem_object *obj)
157 struct drm_device *dev = obj->dev;
161 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
163 /* Make it mmapable */
164 size = omap_gem_mmap_size(obj);
165 ret = drm_gem_create_mmap_offset_size(obj, size);
167 dev_err(dev->dev, "could not allocate mmap offset\n");
171 return drm_vma_node_offset_addr(&obj->vma_node);
174 static bool is_contiguous(struct omap_gem_object *omap_obj)
176 if (omap_obj->flags & OMAP_BO_MEM_DMA_API)
179 if ((omap_obj->flags & OMAP_BO_MEM_DMABUF) && omap_obj->sgt->nents == 1)
185 /* -----------------------------------------------------------------------------
189 static void evict_entry(struct drm_gem_object *obj,
190 enum tiler_fmt fmt, struct omap_drm_usergart_entry *entry)
192 struct omap_gem_object *omap_obj = to_omap_bo(obj);
193 struct omap_drm_private *priv = obj->dev->dev_private;
194 int n = priv->usergart[fmt].height;
195 size_t size = PAGE_SIZE * n;
196 loff_t off = mmap_offset(obj) +
197 (entry->obj_pgoff << PAGE_SHIFT);
198 const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
202 /* if stride > than PAGE_SIZE then sparse mapping: */
203 for (i = n; i > 0; i--) {
204 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
206 off += PAGE_SIZE * m;
209 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
216 /* Evict a buffer from usergart, if it is mapped there */
217 static void evict(struct drm_gem_object *obj)
219 struct omap_gem_object *omap_obj = to_omap_bo(obj);
220 struct omap_drm_private *priv = obj->dev->dev_private;
222 if (omap_obj->flags & OMAP_BO_TILED) {
223 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
226 for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
227 struct omap_drm_usergart_entry *entry =
228 &priv->usergart[fmt].entry[i];
230 if (entry->obj == obj)
231 evict_entry(obj, fmt, entry);
236 /* -----------------------------------------------------------------------------
240 /** ensure backing pages are allocated */
241 static int omap_gem_attach_pages(struct drm_gem_object *obj)
243 struct drm_device *dev = obj->dev;
244 struct omap_gem_object *omap_obj = to_omap_bo(obj);
246 int npages = obj->size >> PAGE_SHIFT;
250 WARN_ON(omap_obj->pages);
252 pages = drm_gem_get_pages(obj);
254 dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
255 return PTR_ERR(pages);
258 /* for non-cached buffers, ensure the new pages are clean because
259 * DSS, GPU, etc. are not cache coherent:
261 if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
262 addrs = kmalloc(npages * sizeof(*addrs), GFP_KERNEL);
268 for (i = 0; i < npages; i++) {
269 addrs[i] = dma_map_page(dev->dev, pages[i],
270 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
272 if (dma_mapping_error(dev->dev, addrs[i])) {
274 "%s: failed to map page\n", __func__);
276 for (i = i - 1; i >= 0; --i) {
277 dma_unmap_page(dev->dev, addrs[i],
278 PAGE_SIZE, DMA_BIDIRECTIONAL);
286 addrs = kzalloc(npages * sizeof(*addrs), GFP_KERNEL);
293 omap_obj->addrs = addrs;
294 omap_obj->pages = pages;
301 drm_gem_put_pages(obj, pages, true, false);
306 /* acquire pages when needed (for example, for DMA where physically
307 * contiguous buffer is not required
309 static int get_pages(struct drm_gem_object *obj, struct page ***pages)
311 struct omap_gem_object *omap_obj = to_omap_bo(obj);
314 if ((omap_obj->flags & OMAP_BO_MEM_SHMEM) && !omap_obj->pages) {
315 ret = omap_gem_attach_pages(obj);
317 dev_err(obj->dev->dev, "could not attach pages\n");
322 /* TODO: even phys-contig.. we should have a list of pages? */
323 *pages = omap_obj->pages;
328 /** release backing pages */
329 static void omap_gem_detach_pages(struct drm_gem_object *obj)
331 struct omap_gem_object *omap_obj = to_omap_bo(obj);
333 /* for non-cached buffers, ensure the new pages are clean because
334 * DSS, GPU, etc. are not cache coherent:
336 if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
337 int i, npages = obj->size >> PAGE_SHIFT;
338 for (i = 0; i < npages; i++) {
339 dma_unmap_page(obj->dev->dev, omap_obj->addrs[i],
340 PAGE_SIZE, DMA_BIDIRECTIONAL);
344 kfree(omap_obj->addrs);
345 omap_obj->addrs = NULL;
347 drm_gem_put_pages(obj, omap_obj->pages, true, false);
348 omap_obj->pages = NULL;
351 /* get buffer flags */
352 uint32_t omap_gem_flags(struct drm_gem_object *obj)
354 return to_omap_bo(obj)->flags;
357 uint64_t omap_gem_mmap_offset(struct drm_gem_object *obj)
360 mutex_lock(&obj->dev->struct_mutex);
361 offset = mmap_offset(obj);
362 mutex_unlock(&obj->dev->struct_mutex);
367 size_t omap_gem_mmap_size(struct drm_gem_object *obj)
369 struct omap_gem_object *omap_obj = to_omap_bo(obj);
370 size_t size = obj->size;
372 if (omap_obj->flags & OMAP_BO_TILED) {
373 /* for tiled buffers, the virtual size has stride rounded up
374 * to 4kb.. (to hide the fact that row n+1 might start 16kb or
375 * 32kb later!). But we don't back the entire buffer with
376 * pages, only the valid picture part.. so need to adjust for
377 * this in the size used to mmap and generate mmap offset
379 size = tiler_vsize(gem2fmt(omap_obj->flags),
380 omap_obj->width, omap_obj->height);
386 /* -----------------------------------------------------------------------------
390 /* Normal handling for the case of faulting in non-tiled buffers */
391 static int fault_1d(struct drm_gem_object *obj,
392 struct vm_area_struct *vma, struct vm_fault *vmf)
394 struct omap_gem_object *omap_obj = to_omap_bo(obj);
398 /* We don't use vmf->pgoff since that has the fake offset: */
399 pgoff = ((unsigned long)vmf->virtual_address -
400 vma->vm_start) >> PAGE_SHIFT;
402 if (omap_obj->pages) {
403 omap_gem_cpu_sync(obj, pgoff);
404 pfn = page_to_pfn(omap_obj->pages[pgoff]);
406 BUG_ON(!is_contiguous(omap_obj));
407 pfn = (omap_obj->paddr >> PAGE_SHIFT) + pgoff;
410 VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
411 pfn, pfn << PAGE_SHIFT);
413 return vm_insert_mixed(vma, (unsigned long)vmf->virtual_address,
414 __pfn_to_pfn_t(pfn, PFN_DEV));
417 /* Special handling for the case of faulting in 2d tiled buffers */
418 static int fault_2d(struct drm_gem_object *obj,
419 struct vm_area_struct *vma, struct vm_fault *vmf)
421 struct omap_gem_object *omap_obj = to_omap_bo(obj);
422 struct omap_drm_private *priv = obj->dev->dev_private;
423 struct omap_drm_usergart_entry *entry;
424 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
425 struct page *pages[64]; /* XXX is this too much to have on stack? */
427 pgoff_t pgoff, base_pgoff;
432 * Note the height of the slot is also equal to the number of pages
433 * that need to be mapped in to fill 4kb wide CPU page. If the slot
434 * height is 64, then 64 pages fill a 4kb wide by 64 row region.
436 const int n = priv->usergart[fmt].height;
437 const int n_shift = priv->usergart[fmt].height_shift;
440 * If buffer width in bytes > PAGE_SIZE then the virtual stride is
441 * rounded up to next multiple of PAGE_SIZE.. this need to be taken
442 * into account in some of the math, so figure out virtual stride
445 const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
447 /* We don't use vmf->pgoff since that has the fake offset: */
448 pgoff = ((unsigned long)vmf->virtual_address -
449 vma->vm_start) >> PAGE_SHIFT;
452 * Actual address we start mapping at is rounded down to previous slot
453 * boundary in the y direction:
455 base_pgoff = round_down(pgoff, m << n_shift);
457 /* figure out buffer width in slots */
458 slots = omap_obj->width >> priv->usergart[fmt].slot_shift;
460 vaddr = vmf->virtual_address - ((pgoff - base_pgoff) << PAGE_SHIFT);
462 entry = &priv->usergart[fmt].entry[priv->usergart[fmt].last];
464 /* evict previous buffer using this usergart entry, if any: */
466 evict_entry(entry->obj, fmt, entry);
469 entry->obj_pgoff = base_pgoff;
471 /* now convert base_pgoff to phys offset from virt offset: */
472 base_pgoff = (base_pgoff >> n_shift) * slots;
474 /* for wider-than 4k.. figure out which part of the slot-row we want: */
477 entry->obj_pgoff += off;
479 slots = min(slots - (off << n_shift), n);
480 base_pgoff += off << n_shift;
481 vaddr += off << PAGE_SHIFT;
485 * Map in pages. Beyond the valid pixel part of the buffer, we set
486 * pages[i] to NULL to get a dummy page mapped in.. if someone
487 * reads/writes it they will get random/undefined content, but at
488 * least it won't be corrupting whatever other random page used to
489 * be mapped in, or other undefined behavior.
491 memcpy(pages, &omap_obj->pages[base_pgoff],
492 sizeof(struct page *) * slots);
493 memset(pages + slots, 0,
494 sizeof(struct page *) * (n - slots));
496 ret = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
498 dev_err(obj->dev->dev, "failed to pin: %d\n", ret);
502 pfn = entry->paddr >> PAGE_SHIFT;
504 VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
505 pfn, pfn << PAGE_SHIFT);
507 for (i = n; i > 0; i--) {
508 vm_insert_mixed(vma, (unsigned long)vaddr,
509 __pfn_to_pfn_t(pfn, PFN_DEV));
510 pfn += priv->usergart[fmt].stride_pfn;
511 vaddr += PAGE_SIZE * m;
514 /* simple round-robin: */
515 priv->usergart[fmt].last = (priv->usergart[fmt].last + 1)
516 % NUM_USERGART_ENTRIES;
522 * omap_gem_fault - pagefault handler for GEM objects
523 * @vma: the VMA of the GEM object
526 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
527 * does most of the work for us including the actual map/unmap calls
528 * but we need to do the actual page work.
530 * The VMA was set up by GEM. In doing so it also ensured that the
531 * vma->vm_private_data points to the GEM object that is backing this
534 int omap_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
536 struct drm_gem_object *obj = vma->vm_private_data;
537 struct omap_gem_object *omap_obj = to_omap_bo(obj);
538 struct drm_device *dev = obj->dev;
542 /* Make sure we don't parallel update on a fault, nor move or remove
543 * something from beneath our feet
545 mutex_lock(&dev->struct_mutex);
547 /* if a shmem backed object, make sure we have pages attached now */
548 ret = get_pages(obj, &pages);
552 /* where should we do corresponding put_pages().. we are mapping
553 * the original page, rather than thru a GART, so we can't rely
554 * on eviction to trigger this. But munmap() or all mappings should
555 * probably trigger put_pages()?
558 if (omap_obj->flags & OMAP_BO_TILED)
559 ret = fault_2d(obj, vma, vmf);
561 ret = fault_1d(obj, vma, vmf);
565 mutex_unlock(&dev->struct_mutex);
572 * EBUSY is ok: this just means that another thread
573 * already did the job.
575 return VM_FAULT_NOPAGE;
579 return VM_FAULT_SIGBUS;
583 /** We override mainly to fix up some of the vm mapping flags.. */
584 int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma)
588 ret = drm_gem_mmap(filp, vma);
590 DBG("mmap failed: %d", ret);
594 return omap_gem_mmap_obj(vma->vm_private_data, vma);
597 int omap_gem_mmap_obj(struct drm_gem_object *obj,
598 struct vm_area_struct *vma)
600 struct omap_gem_object *omap_obj = to_omap_bo(obj);
602 vma->vm_flags &= ~VM_PFNMAP;
603 vma->vm_flags |= VM_MIXEDMAP;
605 if (omap_obj->flags & OMAP_BO_WC) {
606 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
607 } else if (omap_obj->flags & OMAP_BO_UNCACHED) {
608 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
611 * We do have some private objects, at least for scanout buffers
612 * on hardware without DMM/TILER. But these are allocated write-
615 if (WARN_ON(!obj->filp))
619 * Shunt off cached objs to shmem file so they have their own
620 * address_space (so unmap_mapping_range does what we want,
621 * in particular in the case of mmap'd dmabufs)
625 vma->vm_file = get_file(obj->filp);
627 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
633 /* -----------------------------------------------------------------------------
638 * omap_gem_dumb_create - create a dumb buffer
639 * @drm_file: our client file
641 * @args: the requested arguments copied from userspace
643 * Allocate a buffer suitable for use for a frame buffer of the
644 * form described by user space. Give userspace a handle by which
647 int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
648 struct drm_mode_create_dumb *args)
650 union omap_gem_size gsize;
652 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
654 args->size = PAGE_ALIGN(args->pitch * args->height);
656 gsize = (union omap_gem_size){
660 return omap_gem_new_handle(dev, file, gsize,
661 OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle);
665 * omap_gem_dumb_map - buffer mapping for dumb interface
666 * @file: our drm client file
668 * @handle: GEM handle to the object (from dumb_create)
670 * Do the necessary setup to allow the mapping of the frame buffer
671 * into user memory. We don't have to do much here at the moment.
673 int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
674 uint32_t handle, uint64_t *offset)
676 struct drm_gem_object *obj;
679 /* GEM does all our handle to object mapping */
680 obj = drm_gem_object_lookup(file, handle);
686 *offset = omap_gem_mmap_offset(obj);
688 drm_gem_object_unreference_unlocked(obj);
694 #ifdef CONFIG_DRM_FBDEV_EMULATION
695 /* Set scrolling position. This allows us to implement fast scrolling
698 * Call only from non-atomic contexts.
700 int omap_gem_roll(struct drm_gem_object *obj, uint32_t roll)
702 struct omap_gem_object *omap_obj = to_omap_bo(obj);
703 uint32_t npages = obj->size >> PAGE_SHIFT;
707 dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
711 omap_obj->roll = roll;
713 mutex_lock(&obj->dev->struct_mutex);
715 /* if we aren't mapped yet, we don't need to do anything */
716 if (omap_obj->block) {
718 ret = get_pages(obj, &pages);
721 ret = tiler_pin(omap_obj->block, pages, npages, roll, true);
723 dev_err(obj->dev->dev, "could not repin: %d\n", ret);
727 mutex_unlock(&obj->dev->struct_mutex);
733 /* -----------------------------------------------------------------------------
734 * Memory Management & DMA Sync
738 * shmem buffers that are mapped cached can simulate coherency via using
739 * page faulting to keep track of dirty pages
741 static inline bool is_cached_coherent(struct drm_gem_object *obj)
743 struct omap_gem_object *omap_obj = to_omap_bo(obj);
745 return (omap_obj->flags & OMAP_BO_MEM_SHMEM) &&
746 ((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED);
749 /* Sync the buffer for CPU access.. note pages should already be
750 * attached, ie. omap_gem_get_pages()
752 void omap_gem_cpu_sync(struct drm_gem_object *obj, int pgoff)
754 struct drm_device *dev = obj->dev;
755 struct omap_gem_object *omap_obj = to_omap_bo(obj);
757 if (is_cached_coherent(obj) && omap_obj->addrs[pgoff]) {
758 dma_unmap_page(dev->dev, omap_obj->addrs[pgoff],
759 PAGE_SIZE, DMA_BIDIRECTIONAL);
760 omap_obj->addrs[pgoff] = 0;
764 /* sync the buffer for DMA access */
765 void omap_gem_dma_sync(struct drm_gem_object *obj,
766 enum dma_data_direction dir)
768 struct drm_device *dev = obj->dev;
769 struct omap_gem_object *omap_obj = to_omap_bo(obj);
771 if (is_cached_coherent(obj)) {
772 int i, npages = obj->size >> PAGE_SHIFT;
773 struct page **pages = omap_obj->pages;
776 for (i = 0; i < npages; i++) {
777 if (!omap_obj->addrs[i]) {
780 addr = dma_map_page(dev->dev, pages[i], 0,
781 PAGE_SIZE, DMA_BIDIRECTIONAL);
783 if (dma_mapping_error(dev->dev, addr)) {
785 "%s: failed to map page\n",
791 omap_obj->addrs[i] = addr;
796 unmap_mapping_range(obj->filp->f_mapping, 0,
797 omap_gem_mmap_size(obj), 1);
802 /* Get physical address for DMA.. if 'remap' is true, and the buffer is not
803 * already contiguous, remap it to pin in physically contiguous memory.. (ie.
806 int omap_gem_get_paddr(struct drm_gem_object *obj,
807 dma_addr_t *paddr, bool remap)
809 struct omap_drm_private *priv = obj->dev->dev_private;
810 struct omap_gem_object *omap_obj = to_omap_bo(obj);
813 mutex_lock(&obj->dev->struct_mutex);
815 if (!is_contiguous(omap_obj) && remap && priv->has_dmm) {
816 if (omap_obj->paddr_cnt == 0) {
818 uint32_t npages = obj->size >> PAGE_SHIFT;
819 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
820 struct tiler_block *block;
822 BUG_ON(omap_obj->block);
824 ret = get_pages(obj, &pages);
828 if (omap_obj->flags & OMAP_BO_TILED) {
829 block = tiler_reserve_2d(fmt,
831 omap_obj->height, 0);
833 block = tiler_reserve_1d(obj->size);
837 ret = PTR_ERR(block);
838 dev_err(obj->dev->dev,
839 "could not remap: %d (%d)\n", ret, fmt);
843 /* TODO: enable async refill.. */
844 ret = tiler_pin(block, pages, npages,
845 omap_obj->roll, true);
847 tiler_release(block);
848 dev_err(obj->dev->dev,
849 "could not pin: %d\n", ret);
853 omap_obj->paddr = tiler_ssptr(block);
854 omap_obj->block = block;
856 DBG("got paddr: %pad", &omap_obj->paddr);
859 omap_obj->paddr_cnt++;
861 *paddr = omap_obj->paddr;
862 } else if (is_contiguous(omap_obj)) {
863 *paddr = omap_obj->paddr;
870 mutex_unlock(&obj->dev->struct_mutex);
875 /* Release physical address, when DMA is no longer being performed.. this
876 * could potentially unpin and unmap buffers from TILER
878 void omap_gem_put_paddr(struct drm_gem_object *obj)
880 struct omap_gem_object *omap_obj = to_omap_bo(obj);
883 mutex_lock(&obj->dev->struct_mutex);
884 if (omap_obj->paddr_cnt > 0) {
885 omap_obj->paddr_cnt--;
886 if (omap_obj->paddr_cnt == 0) {
887 ret = tiler_unpin(omap_obj->block);
889 dev_err(obj->dev->dev,
890 "could not unpin pages: %d\n", ret);
892 ret = tiler_release(omap_obj->block);
894 dev_err(obj->dev->dev,
895 "could not release unmap: %d\n", ret);
898 omap_obj->block = NULL;
902 mutex_unlock(&obj->dev->struct_mutex);
905 /* Get rotated scanout address (only valid if already pinned), at the
906 * specified orientation and x,y offset from top-left corner of buffer
907 * (only valid for tiled 2d buffers)
909 int omap_gem_rotated_paddr(struct drm_gem_object *obj, uint32_t orient,
910 int x, int y, dma_addr_t *paddr)
912 struct omap_gem_object *omap_obj = to_omap_bo(obj);
915 mutex_lock(&obj->dev->struct_mutex);
916 if ((omap_obj->paddr_cnt > 0) && omap_obj->block &&
917 (omap_obj->flags & OMAP_BO_TILED)) {
918 *paddr = tiler_tsptr(omap_obj->block, orient, x, y);
921 mutex_unlock(&obj->dev->struct_mutex);
925 /* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
926 int omap_gem_tiled_stride(struct drm_gem_object *obj, uint32_t orient)
928 struct omap_gem_object *omap_obj = to_omap_bo(obj);
930 if (omap_obj->flags & OMAP_BO_TILED)
931 ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
935 /* if !remap, and we don't have pages backing, then fail, rather than
936 * increasing the pin count (which we don't really do yet anyways,
937 * because we don't support swapping pages back out). And 'remap'
938 * might not be quite the right name, but I wanted to keep it working
939 * similarly to omap_gem_get_paddr(). Note though that mutex is not
940 * aquired if !remap (because this can be called in atomic ctxt),
941 * but probably omap_gem_get_paddr() should be changed to work in the
942 * same way. If !remap, a matching omap_gem_put_pages() call is not
943 * required (and should not be made).
945 int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
950 struct omap_gem_object *omap_obj = to_omap_bo(obj);
951 if (!omap_obj->pages)
953 *pages = omap_obj->pages;
956 mutex_lock(&obj->dev->struct_mutex);
957 ret = get_pages(obj, pages);
958 mutex_unlock(&obj->dev->struct_mutex);
962 /* release pages when DMA no longer being performed */
963 int omap_gem_put_pages(struct drm_gem_object *obj)
965 /* do something here if we dynamically attach/detach pages.. at
966 * least they would no longer need to be pinned if everyone has
967 * released the pages..
972 #ifdef CONFIG_DRM_FBDEV_EMULATION
973 /* Get kernel virtual address for CPU access.. this more or less only
974 * exists for omap_fbdev. This should be called with struct_mutex
977 void *omap_gem_vaddr(struct drm_gem_object *obj)
979 struct omap_gem_object *omap_obj = to_omap_bo(obj);
980 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
981 if (!omap_obj->vaddr) {
983 int ret = get_pages(obj, &pages);
986 omap_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
987 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
989 return omap_obj->vaddr;
993 /* -----------------------------------------------------------------------------
998 /* re-pin objects in DMM in resume path: */
999 int omap_gem_resume(struct device *dev)
1001 struct drm_device *drm_dev = dev_get_drvdata(dev);
1002 struct omap_drm_private *priv = drm_dev->dev_private;
1003 struct omap_gem_object *omap_obj;
1006 list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
1007 if (omap_obj->block) {
1008 struct drm_gem_object *obj = &omap_obj->base;
1009 uint32_t npages = obj->size >> PAGE_SHIFT;
1010 WARN_ON(!omap_obj->pages); /* this can't happen */
1011 ret = tiler_pin(omap_obj->block,
1012 omap_obj->pages, npages,
1013 omap_obj->roll, true);
1015 dev_err(dev, "could not repin: %d\n", ret);
1025 /* -----------------------------------------------------------------------------
1029 #ifdef CONFIG_DEBUG_FS
1030 void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
1032 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1035 off = drm_vma_node_start(&obj->vma_node);
1037 seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d",
1038 omap_obj->flags, obj->name, obj->refcount.refcount.counter,
1039 off, &omap_obj->paddr, omap_obj->paddr_cnt,
1040 omap_obj->vaddr, omap_obj->roll);
1042 if (omap_obj->flags & OMAP_BO_TILED) {
1043 seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
1044 if (omap_obj->block) {
1045 struct tcm_area *area = &omap_obj->block->area;
1046 seq_printf(m, " (%dx%d, %dx%d)",
1047 area->p0.x, area->p0.y,
1048 area->p1.x, area->p1.y);
1051 seq_printf(m, " %d", obj->size);
1054 seq_printf(m, "\n");
1057 void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
1059 struct omap_gem_object *omap_obj;
1063 list_for_each_entry(omap_obj, list, mm_list) {
1064 struct drm_gem_object *obj = &omap_obj->base;
1066 omap_gem_describe(obj, m);
1071 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
1075 /* -----------------------------------------------------------------------------
1076 * Buffer Synchronization
1079 static DEFINE_SPINLOCK(sync_lock);
1081 struct omap_gem_sync_waiter {
1082 struct list_head list;
1083 struct omap_gem_object *omap_obj;
1084 enum omap_gem_op op;
1085 uint32_t read_target, write_target;
1086 /* notify called w/ sync_lock held */
1087 void (*notify)(void *arg);
1091 /* list of omap_gem_sync_waiter.. the notify fxn gets called back when
1092 * the read and/or write target count is achieved which can call a user
1093 * callback (ex. to kick 3d and/or 2d), wakeup blocked task (prep for
1096 static LIST_HEAD(waiters);
1098 static inline bool is_waiting(struct omap_gem_sync_waiter *waiter)
1100 struct omap_gem_object *omap_obj = waiter->omap_obj;
1101 if ((waiter->op & OMAP_GEM_READ) &&
1102 (omap_obj->sync->write_complete < waiter->write_target))
1104 if ((waiter->op & OMAP_GEM_WRITE) &&
1105 (omap_obj->sync->read_complete < waiter->read_target))
1110 /* macro for sync debug.. */
1112 #define SYNC(fmt, ...) do { if (SYNCDBG) \
1113 printk(KERN_ERR "%s:%d: "fmt"\n", \
1114 __func__, __LINE__, ##__VA_ARGS__); \
1118 static void sync_op_update(void)
1120 struct omap_gem_sync_waiter *waiter, *n;
1121 list_for_each_entry_safe(waiter, n, &waiters, list) {
1122 if (!is_waiting(waiter)) {
1123 list_del(&waiter->list);
1124 SYNC("notify: %p", waiter);
1125 waiter->notify(waiter->arg);
1131 static inline int sync_op(struct drm_gem_object *obj,
1132 enum omap_gem_op op, bool start)
1134 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1137 spin_lock(&sync_lock);
1139 if (!omap_obj->sync) {
1140 omap_obj->sync = kzalloc(sizeof(*omap_obj->sync), GFP_ATOMIC);
1141 if (!omap_obj->sync) {
1148 if (op & OMAP_GEM_READ)
1149 omap_obj->sync->read_pending++;
1150 if (op & OMAP_GEM_WRITE)
1151 omap_obj->sync->write_pending++;
1153 if (op & OMAP_GEM_READ)
1154 omap_obj->sync->read_complete++;
1155 if (op & OMAP_GEM_WRITE)
1156 omap_obj->sync->write_complete++;
1161 spin_unlock(&sync_lock);
1166 /* mark the start of read and/or write operation */
1167 int omap_gem_op_start(struct drm_gem_object *obj, enum omap_gem_op op)
1169 return sync_op(obj, op, true);
1172 int omap_gem_op_finish(struct drm_gem_object *obj, enum omap_gem_op op)
1174 return sync_op(obj, op, false);
1177 static DECLARE_WAIT_QUEUE_HEAD(sync_event);
1179 static void sync_notify(void *arg)
1181 struct task_struct **waiter_task = arg;
1182 *waiter_task = NULL;
1183 wake_up_all(&sync_event);
1186 int omap_gem_op_sync(struct drm_gem_object *obj, enum omap_gem_op op)
1188 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1190 if (omap_obj->sync) {
1191 struct task_struct *waiter_task = current;
1192 struct omap_gem_sync_waiter *waiter =
1193 kzalloc(sizeof(*waiter), GFP_KERNEL);
1198 waiter->omap_obj = omap_obj;
1200 waiter->read_target = omap_obj->sync->read_pending;
1201 waiter->write_target = omap_obj->sync->write_pending;
1202 waiter->notify = sync_notify;
1203 waiter->arg = &waiter_task;
1205 spin_lock(&sync_lock);
1206 if (is_waiting(waiter)) {
1207 SYNC("waited: %p", waiter);
1208 list_add_tail(&waiter->list, &waiters);
1209 spin_unlock(&sync_lock);
1210 ret = wait_event_interruptible(sync_event,
1211 (waiter_task == NULL));
1212 spin_lock(&sync_lock);
1214 SYNC("interrupted: %p", waiter);
1215 /* we were interrupted */
1216 list_del(&waiter->list);
1219 /* freed in sync_op_update() */
1223 spin_unlock(&sync_lock);
1229 /* call fxn(arg), either synchronously or asynchronously if the op
1230 * is currently blocked.. fxn() can be called from any context
1232 * (TODO for now fxn is called back from whichever context calls
1233 * omap_gem_op_finish().. but this could be better defined later
1236 * TODO more code in common w/ _sync()..
1238 int omap_gem_op_async(struct drm_gem_object *obj, enum omap_gem_op op,
1239 void (*fxn)(void *arg), void *arg)
1241 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1242 if (omap_obj->sync) {
1243 struct omap_gem_sync_waiter *waiter =
1244 kzalloc(sizeof(*waiter), GFP_ATOMIC);
1249 waiter->omap_obj = omap_obj;
1251 waiter->read_target = omap_obj->sync->read_pending;
1252 waiter->write_target = omap_obj->sync->write_pending;
1253 waiter->notify = fxn;
1256 spin_lock(&sync_lock);
1257 if (is_waiting(waiter)) {
1258 SYNC("waited: %p", waiter);
1259 list_add_tail(&waiter->list, &waiters);
1260 spin_unlock(&sync_lock);
1264 spin_unlock(&sync_lock);
1275 /* -----------------------------------------------------------------------------
1276 * Constructor & Destructor
1279 void omap_gem_free_object(struct drm_gem_object *obj)
1281 struct drm_device *dev = obj->dev;
1282 struct omap_drm_private *priv = dev->dev_private;
1283 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1287 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
1289 spin_lock(&priv->list_lock);
1290 list_del(&omap_obj->mm_list);
1291 spin_unlock(&priv->list_lock);
1293 /* this means the object is still pinned.. which really should
1294 * not happen. I think..
1296 WARN_ON(omap_obj->paddr_cnt > 0);
1298 if (omap_obj->pages) {
1299 if (omap_obj->flags & OMAP_BO_MEM_DMABUF)
1300 kfree(omap_obj->pages);
1302 omap_gem_detach_pages(obj);
1305 if (omap_obj->flags & OMAP_BO_MEM_DMA_API) {
1306 dma_free_wc(dev->dev, obj->size, omap_obj->vaddr,
1308 } else if (omap_obj->vaddr) {
1309 vunmap(omap_obj->vaddr);
1310 } else if (obj->import_attach) {
1311 drm_prime_gem_destroy(obj, omap_obj->sgt);
1314 kfree(omap_obj->sync);
1316 drm_gem_object_release(obj);
1321 /* GEM buffer object constructor */
1322 struct drm_gem_object *omap_gem_new(struct drm_device *dev,
1323 union omap_gem_size gsize, uint32_t flags)
1325 struct omap_drm_private *priv = dev->dev_private;
1326 struct omap_gem_object *omap_obj;
1327 struct drm_gem_object *obj;
1328 struct address_space *mapping;
1332 /* Validate the flags and compute the memory and cache flags. */
1333 if (flags & OMAP_BO_TILED) {
1334 if (!priv->usergart) {
1335 dev_err(dev->dev, "Tiled buffers require DMM\n");
1340 * Tiled buffers are always shmem paged backed. When they are
1341 * scanned out, they are remapped into DMM/TILER.
1343 flags &= ~OMAP_BO_SCANOUT;
1344 flags |= OMAP_BO_MEM_SHMEM;
1347 * Currently don't allow cached buffers. There is some caching
1348 * stuff that needs to be handled better.
1350 flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED);
1351 flags |= tiler_get_cpu_cache_flags();
1352 } else if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
1354 * OMAP_BO_SCANOUT hints that the buffer doesn't need to be
1355 * tiled. However, to lower the pressure on memory allocation,
1356 * use contiguous memory only if no TILER is available.
1358 flags |= OMAP_BO_MEM_DMA_API;
1359 } else if (!(flags & OMAP_BO_MEM_DMABUF)) {
1361 * All other buffers not backed by dma_buf are shmem-backed.
1363 flags |= OMAP_BO_MEM_SHMEM;
1366 /* Allocate the initialize the OMAP GEM object. */
1367 omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
1371 obj = &omap_obj->base;
1372 omap_obj->flags = flags;
1374 if (flags & OMAP_BO_TILED) {
1376 * For tiled buffers align dimensions to slot boundaries and
1377 * calculate size based on aligned dimensions.
1379 tiler_align(gem2fmt(flags), &gsize.tiled.width,
1380 &gsize.tiled.height);
1382 size = tiler_size(gem2fmt(flags), gsize.tiled.width,
1383 gsize.tiled.height);
1385 omap_obj->width = gsize.tiled.width;
1386 omap_obj->height = gsize.tiled.height;
1388 size = PAGE_ALIGN(gsize.bytes);
1391 /* Initialize the GEM object. */
1392 if (!(flags & OMAP_BO_MEM_SHMEM)) {
1393 drm_gem_private_object_init(dev, obj, size);
1395 ret = drm_gem_object_init(dev, obj, size);
1399 mapping = obj->filp->f_mapping;
1400 mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32);
1403 /* Allocate memory if needed. */
1404 if (flags & OMAP_BO_MEM_DMA_API) {
1405 omap_obj->vaddr = dma_alloc_wc(dev->dev, size,
1408 if (!omap_obj->vaddr)
1412 spin_lock(&priv->list_lock);
1413 list_add(&omap_obj->mm_list, &priv->obj_list);
1414 spin_unlock(&priv->list_lock);
1419 drm_gem_object_release(obj);
1425 struct drm_gem_object *omap_gem_new_dmabuf(struct drm_device *dev, size_t size,
1426 struct sg_table *sgt)
1428 struct omap_drm_private *priv = dev->dev_private;
1429 struct omap_gem_object *omap_obj;
1430 struct drm_gem_object *obj;
1431 union omap_gem_size gsize;
1433 /* Without a DMM only physically contiguous buffers can be supported. */
1434 if (sgt->orig_nents != 1 && !priv->has_dmm)
1435 return ERR_PTR(-EINVAL);
1437 mutex_lock(&dev->struct_mutex);
1439 gsize.bytes = PAGE_ALIGN(size);
1440 obj = omap_gem_new(dev, gsize, OMAP_BO_MEM_DMABUF | OMAP_BO_WC);
1442 obj = ERR_PTR(-ENOMEM);
1446 omap_obj = to_omap_bo(obj);
1447 omap_obj->sgt = sgt;
1449 if (sgt->orig_nents == 1) {
1450 omap_obj->paddr = sg_dma_address(sgt->sgl);
1452 /* Create pages list from sgt */
1453 struct sg_page_iter iter;
1454 struct page **pages;
1455 unsigned int npages;
1458 npages = DIV_ROUND_UP(size, PAGE_SIZE);
1459 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1461 omap_gem_free_object(obj);
1462 obj = ERR_PTR(-ENOMEM);
1466 omap_obj->pages = pages;
1468 for_each_sg_page(sgt->sgl, &iter, sgt->orig_nents, 0) {
1469 pages[i++] = sg_page_iter_page(&iter);
1474 if (WARN_ON(i != npages)) {
1475 omap_gem_free_object(obj);
1476 obj = ERR_PTR(-ENOMEM);
1482 mutex_unlock(&dev->struct_mutex);
1486 /* convenience method to construct a GEM buffer object, and userspace handle */
1487 int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1488 union omap_gem_size gsize, uint32_t flags, uint32_t *handle)
1490 struct drm_gem_object *obj;
1493 obj = omap_gem_new(dev, gsize, flags);
1497 ret = drm_gem_handle_create(file, obj, handle);
1499 omap_gem_free_object(obj);
1503 /* drop reference from allocate - handle holds it now */
1504 drm_gem_object_unreference_unlocked(obj);
1509 /* -----------------------------------------------------------------------------
1513 /* If DMM is used, we need to set some stuff up.. */
1514 void omap_gem_init(struct drm_device *dev)
1516 struct omap_drm_private *priv = dev->dev_private;
1517 struct omap_drm_usergart *usergart;
1518 const enum tiler_fmt fmts[] = {
1519 TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
1523 if (!dmm_is_available()) {
1524 /* DMM only supported on OMAP4 and later, so this isn't fatal */
1525 dev_warn(dev->dev, "DMM not available, disable DMM support\n");
1529 usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
1533 /* reserve 4k aligned/wide regions for userspace mappings: */
1534 for (i = 0; i < ARRAY_SIZE(fmts); i++) {
1535 uint16_t h = 1, w = PAGE_SIZE >> i;
1536 tiler_align(fmts[i], &w, &h);
1537 /* note: since each region is 1 4kb page wide, and minimum
1538 * number of rows, the height ends up being the same as the
1539 * # of pages in the region
1541 usergart[i].height = h;
1542 usergart[i].height_shift = ilog2(h);
1543 usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
1544 usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
1545 for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
1546 struct omap_drm_usergart_entry *entry;
1547 struct tiler_block *block;
1549 entry = &usergart[i].entry[j];
1550 block = tiler_reserve_2d(fmts[i], w, h, PAGE_SIZE);
1551 if (IS_ERR(block)) {
1553 "reserve failed: %d, %d, %ld\n",
1554 i, j, PTR_ERR(block));
1557 entry->paddr = tiler_ssptr(block);
1558 entry->block = block;
1560 DBG("%d:%d: %dx%d: paddr=%pad stride=%d", i, j, w, h,
1562 usergart[i].stride_pfn << PAGE_SHIFT);
1566 priv->usergart = usergart;
1567 priv->has_dmm = true;
1570 void omap_gem_deinit(struct drm_device *dev)
1572 struct omap_drm_private *priv = dev->dev_private;
1574 /* I believe we can rely on there being no more outstanding GEM
1575 * objects which could depend on usergart/dmm at this point.
1577 kfree(priv->usergart);