1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
4 * Author: Rob Clark <rob.clark@linaro.org>
7 #include <linux/dma-mapping.h>
8 #include <linux/seq_file.h>
9 #include <linux/shmem_fs.h>
10 #include <linux/spinlock.h>
11 #include <linux/pfn_t.h>
13 #include <drm/drm_prime.h>
14 #include <drm/drm_vma_manager.h>
17 #include "omap_dmm_tiler.h"
20 * GEM buffer object implementation.
23 /* note: we use upper 8 bits of flags for driver-internal flags: */
24 #define OMAP_BO_MEM_DMA_API 0x01000000 /* memory allocated with the dma_alloc_* API */
25 #define OMAP_BO_MEM_SHMEM 0x02000000 /* memory allocated through shmem backing */
26 #define OMAP_BO_MEM_DMABUF 0x08000000 /* memory imported from a dmabuf */
28 struct omap_gem_object {
29 struct drm_gem_object base;
31 struct list_head mm_list;
35 /** width/height for tiled formats (rounded up to slot boundaries) */
38 /** roll applied when mapping to DMM */
41 /** protects dma_addr_cnt, block, pages, dma_addrs and vaddr */
45 * dma_addr contains the buffer DMA address. It is valid for
47 * - buffers allocated through the DMA mapping API (with the
48 * OMAP_BO_MEM_DMA_API flag set)
50 * - buffers imported from dmabuf (with the OMAP_BO_MEM_DMABUF flag set)
51 * if they are physically contiguous (when sgt->orig_nents == 1)
53 * - buffers mapped through the TILER when dma_addr_cnt is not zero, in
54 * which case the DMA address points to the TILER aperture
56 * Physically contiguous buffers have their DMA address equal to the
57 * physical address as we don't remap those buffers through the TILER.
59 * Buffers mapped to the TILER have their DMA address pointing to the
60 * TILER aperture. As TILER mappings are refcounted (through
61 * dma_addr_cnt) the DMA address must be accessed through omap_gem_pin()
62 * to ensure that the mapping won't disappear unexpectedly. References
63 * must be released with omap_gem_unpin().
68 * # of users of dma_addr
70 refcount_t dma_addr_cnt;
73 * If the buffer has been imported from a dmabuf the OMAP_DB_DMABUF flag
74 * is set and the sgt field is valid.
79 * tiler block used when buffer is remapped in DMM/TILER.
81 struct tiler_block *block;
84 * Array of backing pages, if allocated. Note that pages are never
85 * allocated for buffers originally allocated from contiguous memory
89 /** addresses corresponding to pages in above array */
90 dma_addr_t *dma_addrs;
93 * Virtual address, if mapped.
98 #define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
100 /* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
101 * not necessarily pinned in TILER all the time, and (b) when they are
102 * they are not necessarily page aligned, we reserve one or more small
103 * regions in each of the 2d containers to use as a user-GART where we
104 * can create a second page-aligned mapping of parts of the buffer
105 * being accessed from userspace.
107 * Note that we could optimize slightly when we know that multiple
108 * tiler containers are backed by the same PAT.. but I'll leave that
111 #define NUM_USERGART_ENTRIES 2
112 struct omap_drm_usergart_entry {
113 struct tiler_block *block; /* the reserved tiler block */
115 struct drm_gem_object *obj; /* the current pinned obj */
116 pgoff_t obj_pgoff; /* page offset of obj currently
120 struct omap_drm_usergart {
121 struct omap_drm_usergart_entry entry[NUM_USERGART_ENTRIES];
122 int height; /* height in rows */
123 int height_shift; /* ilog2(height in rows) */
124 int slot_shift; /* ilog2(width per slot) */
125 int stride_pfn; /* stride in pages */
126 int last; /* index of last used entry */
129 /* -----------------------------------------------------------------------------
133 /** get mmap offset */
134 u64 omap_gem_mmap_offset(struct drm_gem_object *obj)
136 struct drm_device *dev = obj->dev;
140 /* Make it mmapable */
141 size = omap_gem_mmap_size(obj);
142 ret = drm_gem_create_mmap_offset_size(obj, size);
144 dev_err(dev->dev, "could not allocate mmap offset\n");
148 return drm_vma_node_offset_addr(&obj->vma_node);
151 static bool omap_gem_is_contiguous(struct omap_gem_object *omap_obj)
153 if (omap_obj->flags & OMAP_BO_MEM_DMA_API)
156 if ((omap_obj->flags & OMAP_BO_MEM_DMABUF) && omap_obj->sgt->nents == 1)
162 /* -----------------------------------------------------------------------------
166 static void omap_gem_evict_entry(struct drm_gem_object *obj,
167 enum tiler_fmt fmt, struct omap_drm_usergart_entry *entry)
169 struct omap_gem_object *omap_obj = to_omap_bo(obj);
170 struct omap_drm_private *priv = obj->dev->dev_private;
171 int n = priv->usergart[fmt].height;
172 size_t size = PAGE_SIZE * n;
173 loff_t off = omap_gem_mmap_offset(obj) +
174 (entry->obj_pgoff << PAGE_SHIFT);
175 const int m = DIV_ROUND_UP(omap_obj->width << fmt, PAGE_SIZE);
179 /* if stride > than PAGE_SIZE then sparse mapping: */
180 for (i = n; i > 0; i--) {
181 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
183 off += PAGE_SIZE * m;
186 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
193 /* Evict a buffer from usergart, if it is mapped there */
194 static void omap_gem_evict(struct drm_gem_object *obj)
196 struct omap_gem_object *omap_obj = to_omap_bo(obj);
197 struct omap_drm_private *priv = obj->dev->dev_private;
199 if (omap_obj->flags & OMAP_BO_TILED_MASK) {
200 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
203 for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
204 struct omap_drm_usergart_entry *entry =
205 &priv->usergart[fmt].entry[i];
207 if (entry->obj == obj)
208 omap_gem_evict_entry(obj, fmt, entry);
213 /* -----------------------------------------------------------------------------
218 * Ensure backing pages are allocated. Must be called with the omap_obj.lock
221 static int omap_gem_attach_pages(struct drm_gem_object *obj)
223 struct drm_device *dev = obj->dev;
224 struct omap_gem_object *omap_obj = to_omap_bo(obj);
226 int npages = obj->size >> PAGE_SHIFT;
230 lockdep_assert_held(&omap_obj->lock);
233 * If not using shmem (in which case backing pages don't need to be
234 * allocated) or if pages are already allocated we're done.
236 if (!(omap_obj->flags & OMAP_BO_MEM_SHMEM) || omap_obj->pages)
239 pages = drm_gem_get_pages(obj);
241 dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
242 return PTR_ERR(pages);
245 /* for non-cached buffers, ensure the new pages are clean because
246 * DSS, GPU, etc. are not cache coherent:
248 if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
249 addrs = kmalloc_array(npages, sizeof(*addrs), GFP_KERNEL);
255 for (i = 0; i < npages; i++) {
256 addrs[i] = dma_map_page(dev->dev, pages[i],
257 0, PAGE_SIZE, DMA_TO_DEVICE);
259 if (dma_mapping_error(dev->dev, addrs[i])) {
261 "%s: failed to map page\n", __func__);
263 for (i = i - 1; i >= 0; --i) {
264 dma_unmap_page(dev->dev, addrs[i],
265 PAGE_SIZE, DMA_TO_DEVICE);
273 addrs = kcalloc(npages, sizeof(*addrs), GFP_KERNEL);
280 omap_obj->dma_addrs = addrs;
281 omap_obj->pages = pages;
288 drm_gem_put_pages(obj, pages, true, false);
293 /* Release backing pages. Must be called with the omap_obj.lock held. */
294 static void omap_gem_detach_pages(struct drm_gem_object *obj)
296 struct omap_gem_object *omap_obj = to_omap_bo(obj);
297 unsigned int npages = obj->size >> PAGE_SHIFT;
300 lockdep_assert_held(&omap_obj->lock);
302 for (i = 0; i < npages; i++) {
303 if (omap_obj->dma_addrs[i])
304 dma_unmap_page(obj->dev->dev, omap_obj->dma_addrs[i],
305 PAGE_SIZE, DMA_TO_DEVICE);
308 kfree(omap_obj->dma_addrs);
309 omap_obj->dma_addrs = NULL;
311 drm_gem_put_pages(obj, omap_obj->pages, true, false);
312 omap_obj->pages = NULL;
315 /* get buffer flags */
316 u32 omap_gem_flags(struct drm_gem_object *obj)
318 return to_omap_bo(obj)->flags;
322 size_t omap_gem_mmap_size(struct drm_gem_object *obj)
324 struct omap_gem_object *omap_obj = to_omap_bo(obj);
325 size_t size = obj->size;
327 if (omap_obj->flags & OMAP_BO_TILED_MASK) {
328 /* for tiled buffers, the virtual size has stride rounded up
329 * to 4kb.. (to hide the fact that row n+1 might start 16kb or
330 * 32kb later!). But we don't back the entire buffer with
331 * pages, only the valid picture part.. so need to adjust for
332 * this in the size used to mmap and generate mmap offset
334 size = tiler_vsize(gem2fmt(omap_obj->flags),
335 omap_obj->width, omap_obj->height);
341 /* -----------------------------------------------------------------------------
345 /* Normal handling for the case of faulting in non-tiled buffers */
346 static vm_fault_t omap_gem_fault_1d(struct drm_gem_object *obj,
347 struct vm_area_struct *vma, struct vm_fault *vmf)
349 struct omap_gem_object *omap_obj = to_omap_bo(obj);
353 /* We don't use vmf->pgoff since that has the fake offset: */
354 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
356 if (omap_obj->pages) {
357 omap_gem_cpu_sync_page(obj, pgoff);
358 pfn = page_to_pfn(omap_obj->pages[pgoff]);
360 BUG_ON(!omap_gem_is_contiguous(omap_obj));
361 pfn = (omap_obj->dma_addr >> PAGE_SHIFT) + pgoff;
364 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
365 pfn, pfn << PAGE_SHIFT);
367 return vmf_insert_mixed(vma, vmf->address,
368 __pfn_to_pfn_t(pfn, PFN_DEV));
371 /* Special handling for the case of faulting in 2d tiled buffers */
372 static vm_fault_t omap_gem_fault_2d(struct drm_gem_object *obj,
373 struct vm_area_struct *vma, struct vm_fault *vmf)
375 struct omap_gem_object *omap_obj = to_omap_bo(obj);
376 struct omap_drm_private *priv = obj->dev->dev_private;
377 struct omap_drm_usergart_entry *entry;
378 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
379 struct page *pages[64]; /* XXX is this too much to have on stack? */
381 pgoff_t pgoff, base_pgoff;
384 vm_fault_t ret = VM_FAULT_NOPAGE;
387 * Note the height of the slot is also equal to the number of pages
388 * that need to be mapped in to fill 4kb wide CPU page. If the slot
389 * height is 64, then 64 pages fill a 4kb wide by 64 row region.
391 const int n = priv->usergart[fmt].height;
392 const int n_shift = priv->usergart[fmt].height_shift;
395 * If buffer width in bytes > PAGE_SIZE then the virtual stride is
396 * rounded up to next multiple of PAGE_SIZE.. this need to be taken
397 * into account in some of the math, so figure out virtual stride
400 const int m = DIV_ROUND_UP(omap_obj->width << fmt, PAGE_SIZE);
402 /* We don't use vmf->pgoff since that has the fake offset: */
403 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
406 * Actual address we start mapping at is rounded down to previous slot
407 * boundary in the y direction:
409 base_pgoff = round_down(pgoff, m << n_shift);
411 /* figure out buffer width in slots */
412 slots = omap_obj->width >> priv->usergart[fmt].slot_shift;
414 vaddr = vmf->address - ((pgoff - base_pgoff) << PAGE_SHIFT);
416 entry = &priv->usergart[fmt].entry[priv->usergart[fmt].last];
418 /* evict previous buffer using this usergart entry, if any: */
420 omap_gem_evict_entry(entry->obj, fmt, entry);
423 entry->obj_pgoff = base_pgoff;
425 /* now convert base_pgoff to phys offset from virt offset: */
426 base_pgoff = (base_pgoff >> n_shift) * slots;
428 /* for wider-than 4k.. figure out which part of the slot-row we want: */
431 entry->obj_pgoff += off;
433 slots = min(slots - (off << n_shift), n);
434 base_pgoff += off << n_shift;
435 vaddr += off << PAGE_SHIFT;
439 * Map in pages. Beyond the valid pixel part of the buffer, we set
440 * pages[i] to NULL to get a dummy page mapped in.. if someone
441 * reads/writes it they will get random/undefined content, but at
442 * least it won't be corrupting whatever other random page used to
443 * be mapped in, or other undefined behavior.
445 memcpy(pages, &omap_obj->pages[base_pgoff],
446 sizeof(struct page *) * slots);
447 memset(pages + slots, 0,
448 sizeof(struct page *) * (n - slots));
450 err = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
452 ret = vmf_error(err);
453 dev_err(obj->dev->dev, "failed to pin: %d\n", err);
457 pfn = entry->dma_addr >> PAGE_SHIFT;
459 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
460 pfn, pfn << PAGE_SHIFT);
462 for (i = n; i > 0; i--) {
463 ret = vmf_insert_mixed(vma,
464 vaddr, __pfn_to_pfn_t(pfn, PFN_DEV));
465 if (ret & VM_FAULT_ERROR)
467 pfn += priv->usergart[fmt].stride_pfn;
468 vaddr += PAGE_SIZE * m;
471 /* simple round-robin: */
472 priv->usergart[fmt].last = (priv->usergart[fmt].last + 1)
473 % NUM_USERGART_ENTRIES;
479 * omap_gem_fault - pagefault handler for GEM objects
482 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
483 * does most of the work for us including the actual map/unmap calls
484 * but we need to do the actual page work.
486 * The VMA was set up by GEM. In doing so it also ensured that the
487 * vma->vm_private_data points to the GEM object that is backing this
490 vm_fault_t omap_gem_fault(struct vm_fault *vmf)
492 struct vm_area_struct *vma = vmf->vma;
493 struct drm_gem_object *obj = vma->vm_private_data;
494 struct omap_gem_object *omap_obj = to_omap_bo(obj);
498 /* Make sure we don't parallel update on a fault, nor move or remove
499 * something from beneath our feet
501 mutex_lock(&omap_obj->lock);
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_MASK)
517 ret = omap_gem_fault_2d(obj, vma, vmf);
519 ret = omap_gem_fault_1d(obj, vma, vmf);
523 mutex_unlock(&omap_obj->lock);
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_put_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(&omap_obj->lock);
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(&omap_obj->lock);
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(&omap_obj->lock);
775 if (!omap_gem_is_contiguous(omap_obj) && priv->has_dmm) {
776 if (refcount_read(&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 refcount_set(&omap_obj->dma_addr_cnt, 1);
785 ret = omap_gem_attach_pages(obj);
789 if (omap_obj->flags & OMAP_BO_TILED_MASK) {
790 block = tiler_reserve_2d(fmt,
792 omap_obj->height, 0);
794 block = tiler_reserve_1d(obj->size);
798 ret = PTR_ERR(block);
799 dev_err(obj->dev->dev,
800 "could not remap: %d (%d)\n", ret, fmt);
804 /* TODO: enable async refill.. */
805 ret = tiler_pin(block, omap_obj->pages, npages,
806 omap_obj->roll, true);
808 tiler_release(block);
809 dev_err(obj->dev->dev,
810 "could not pin: %d\n", ret);
814 omap_obj->dma_addr = tiler_ssptr(block);
815 omap_obj->block = block;
817 DBG("got dma address: %pad", &omap_obj->dma_addr);
819 refcount_inc(&omap_obj->dma_addr_cnt);
823 *dma_addr = omap_obj->dma_addr;
824 } else if (omap_gem_is_contiguous(omap_obj)) {
826 *dma_addr = omap_obj->dma_addr;
833 mutex_unlock(&omap_obj->lock);
839 * omap_gem_unpin_locked() - Unpin a GEM object from memory
840 * @obj: the GEM object
842 * omap_gem_unpin() without locking.
844 static void omap_gem_unpin_locked(struct drm_gem_object *obj)
846 struct omap_drm_private *priv = obj->dev->dev_private;
847 struct omap_gem_object *omap_obj = to_omap_bo(obj);
850 if (omap_gem_is_contiguous(omap_obj) || !priv->has_dmm)
853 if (refcount_dec_and_test(&omap_obj->dma_addr_cnt)) {
854 ret = tiler_unpin(omap_obj->block);
856 dev_err(obj->dev->dev,
857 "could not unpin pages: %d\n", ret);
859 ret = tiler_release(omap_obj->block);
861 dev_err(obj->dev->dev,
862 "could not release unmap: %d\n", ret);
864 omap_obj->dma_addr = 0;
865 omap_obj->block = NULL;
870 * omap_gem_unpin() - Unpin a GEM object from memory
871 * @obj: the GEM object
873 * Unpin the given GEM object previously pinned with omap_gem_pin(). Pins are
874 * reference-counted, the actual unpin will only be performed when the number
875 * of calls to this function matches the number of calls to omap_gem_pin().
877 void omap_gem_unpin(struct drm_gem_object *obj)
879 struct omap_gem_object *omap_obj = to_omap_bo(obj);
881 mutex_lock(&omap_obj->lock);
882 omap_gem_unpin_locked(obj);
883 mutex_unlock(&omap_obj->lock);
886 /* Get rotated scanout address (only valid if already pinned), at the
887 * specified orientation and x,y offset from top-left corner of buffer
888 * (only valid for tiled 2d buffers)
890 int omap_gem_rotated_dma_addr(struct drm_gem_object *obj, u32 orient,
891 int x, int y, dma_addr_t *dma_addr)
893 struct omap_gem_object *omap_obj = to_omap_bo(obj);
896 mutex_lock(&omap_obj->lock);
898 if ((refcount_read(&omap_obj->dma_addr_cnt) > 0) && omap_obj->block &&
899 (omap_obj->flags & OMAP_BO_TILED_MASK)) {
900 *dma_addr = tiler_tsptr(omap_obj->block, orient, x, y);
904 mutex_unlock(&omap_obj->lock);
909 /* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
910 int omap_gem_tiled_stride(struct drm_gem_object *obj, u32 orient)
912 struct omap_gem_object *omap_obj = to_omap_bo(obj);
914 if (omap_obj->flags & OMAP_BO_TILED_MASK)
915 ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
919 /* if !remap, and we don't have pages backing, then fail, rather than
920 * increasing the pin count (which we don't really do yet anyways,
921 * because we don't support swapping pages back out). And 'remap'
922 * might not be quite the right name, but I wanted to keep it working
923 * similarly to omap_gem_pin(). Note though that mutex is not
924 * aquired if !remap (because this can be called in atomic ctxt),
925 * but probably omap_gem_unpin() should be changed to work in the
926 * same way. If !remap, a matching omap_gem_put_pages() call is not
927 * required (and should not be made).
929 int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
932 struct omap_gem_object *omap_obj = to_omap_bo(obj);
935 mutex_lock(&omap_obj->lock);
938 ret = omap_gem_attach_pages(obj);
943 if (!omap_obj->pages) {
948 *pages = omap_obj->pages;
951 mutex_unlock(&omap_obj->lock);
956 /* release pages when DMA no longer being performed */
957 int omap_gem_put_pages(struct drm_gem_object *obj)
959 /* do something here if we dynamically attach/detach pages.. at
960 * least they would no longer need to be pinned if everyone has
961 * released the pages..
966 #ifdef CONFIG_DRM_FBDEV_EMULATION
968 * Get kernel virtual address for CPU access.. this more or less only
969 * exists for omap_fbdev.
971 void *omap_gem_vaddr(struct drm_gem_object *obj)
973 struct omap_gem_object *omap_obj = to_omap_bo(obj);
977 mutex_lock(&omap_obj->lock);
979 if (!omap_obj->vaddr) {
980 ret = omap_gem_attach_pages(obj);
982 vaddr = ERR_PTR(ret);
986 omap_obj->vaddr = vmap(omap_obj->pages, obj->size >> PAGE_SHIFT,
987 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
990 vaddr = omap_obj->vaddr;
993 mutex_unlock(&omap_obj->lock);
998 /* -----------------------------------------------------------------------------
1003 /* re-pin objects in DMM in resume path: */
1004 int omap_gem_resume(struct drm_device *dev)
1006 struct omap_drm_private *priv = dev->dev_private;
1007 struct omap_gem_object *omap_obj;
1010 mutex_lock(&priv->list_lock);
1011 list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
1012 if (omap_obj->block) {
1013 struct drm_gem_object *obj = &omap_obj->base;
1014 u32 npages = obj->size >> PAGE_SHIFT;
1016 WARN_ON(!omap_obj->pages); /* this can't happen */
1017 ret = tiler_pin(omap_obj->block,
1018 omap_obj->pages, npages,
1019 omap_obj->roll, true);
1021 dev_err(dev->dev, "could not repin: %d\n", ret);
1028 mutex_unlock(&priv->list_lock);
1033 /* -----------------------------------------------------------------------------
1037 #ifdef CONFIG_DEBUG_FS
1038 void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
1040 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1043 off = drm_vma_node_start(&obj->vma_node);
1045 mutex_lock(&omap_obj->lock);
1047 seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d",
1048 omap_obj->flags, obj->name, kref_read(&obj->refcount),
1049 off, &omap_obj->dma_addr,
1050 refcount_read(&omap_obj->dma_addr_cnt),
1051 omap_obj->vaddr, omap_obj->roll);
1053 if (omap_obj->flags & OMAP_BO_TILED_MASK) {
1054 seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
1055 if (omap_obj->block) {
1056 struct tcm_area *area = &omap_obj->block->area;
1057 seq_printf(m, " (%dx%d, %dx%d)",
1058 area->p0.x, area->p0.y,
1059 area->p1.x, area->p1.y);
1062 seq_printf(m, " %zu", obj->size);
1065 mutex_unlock(&omap_obj->lock);
1067 seq_printf(m, "\n");
1070 void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
1072 struct omap_gem_object *omap_obj;
1076 list_for_each_entry(omap_obj, list, mm_list) {
1077 struct drm_gem_object *obj = &omap_obj->base;
1079 omap_gem_describe(obj, m);
1084 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
1088 /* -----------------------------------------------------------------------------
1089 * Constructor & Destructor
1092 void omap_gem_free_object(struct drm_gem_object *obj)
1094 struct drm_device *dev = obj->dev;
1095 struct omap_drm_private *priv = dev->dev_private;
1096 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1098 omap_gem_evict(obj);
1100 mutex_lock(&priv->list_lock);
1101 list_del(&omap_obj->mm_list);
1102 mutex_unlock(&priv->list_lock);
1105 * We own the sole reference to the object at this point, but to keep
1106 * lockdep happy, we must still take the omap_obj_lock to call
1107 * omap_gem_detach_pages(). This should hardly make any difference as
1108 * there can't be any lock contention.
1110 mutex_lock(&omap_obj->lock);
1112 /* The object should not be pinned. */
1113 WARN_ON(refcount_read(&omap_obj->dma_addr_cnt) > 0);
1115 if (omap_obj->pages) {
1116 if (omap_obj->flags & OMAP_BO_MEM_DMABUF)
1117 kfree(omap_obj->pages);
1119 omap_gem_detach_pages(obj);
1122 if (omap_obj->flags & OMAP_BO_MEM_DMA_API) {
1123 dma_free_wc(dev->dev, obj->size, omap_obj->vaddr,
1124 omap_obj->dma_addr);
1125 } else if (omap_obj->vaddr) {
1126 vunmap(omap_obj->vaddr);
1127 } else if (obj->import_attach) {
1128 drm_prime_gem_destroy(obj, omap_obj->sgt);
1131 mutex_unlock(&omap_obj->lock);
1133 drm_gem_object_release(obj);
1135 mutex_destroy(&omap_obj->lock);
1140 static bool omap_gem_validate_flags(struct drm_device *dev, u32 flags)
1142 struct omap_drm_private *priv = dev->dev_private;
1144 switch (flags & OMAP_BO_CACHE_MASK) {
1145 case OMAP_BO_CACHED:
1147 case OMAP_BO_CACHE_MASK:
1154 if (flags & OMAP_BO_TILED_MASK) {
1155 if (!priv->usergart)
1158 switch (flags & OMAP_BO_TILED_MASK) {
1159 case OMAP_BO_TILED_8:
1160 case OMAP_BO_TILED_16:
1161 case OMAP_BO_TILED_32:
1172 /* GEM buffer object constructor */
1173 struct drm_gem_object *omap_gem_new(struct drm_device *dev,
1174 union omap_gem_size gsize, u32 flags)
1176 struct omap_drm_private *priv = dev->dev_private;
1177 struct omap_gem_object *omap_obj;
1178 struct drm_gem_object *obj;
1179 struct address_space *mapping;
1183 if (!omap_gem_validate_flags(dev, flags))
1186 /* Validate the flags and compute the memory and cache flags. */
1187 if (flags & OMAP_BO_TILED_MASK) {
1189 * Tiled buffers are always shmem paged backed. When they are
1190 * scanned out, they are remapped into DMM/TILER.
1192 flags |= OMAP_BO_MEM_SHMEM;
1195 * Currently don't allow cached buffers. There is some caching
1196 * stuff that needs to be handled better.
1198 flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED);
1199 flags |= tiler_get_cpu_cache_flags();
1200 } else if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
1202 * If we don't have DMM, we must allocate scanout buffers
1203 * from contiguous DMA memory.
1205 flags |= OMAP_BO_MEM_DMA_API;
1206 } else if (!(flags & OMAP_BO_MEM_DMABUF)) {
1208 * All other buffers not backed by dma_buf are shmem-backed.
1210 flags |= OMAP_BO_MEM_SHMEM;
1213 /* Allocate the initialize the OMAP GEM object. */
1214 omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
1218 obj = &omap_obj->base;
1219 omap_obj->flags = flags;
1220 mutex_init(&omap_obj->lock);
1222 if (flags & OMAP_BO_TILED_MASK) {
1224 * For tiled buffers align dimensions to slot boundaries and
1225 * calculate size based on aligned dimensions.
1227 tiler_align(gem2fmt(flags), &gsize.tiled.width,
1228 &gsize.tiled.height);
1230 size = tiler_size(gem2fmt(flags), gsize.tiled.width,
1231 gsize.tiled.height);
1233 omap_obj->width = gsize.tiled.width;
1234 omap_obj->height = gsize.tiled.height;
1236 size = PAGE_ALIGN(gsize.bytes);
1239 /* Initialize the GEM object. */
1240 if (!(flags & OMAP_BO_MEM_SHMEM)) {
1241 drm_gem_private_object_init(dev, obj, size);
1243 ret = drm_gem_object_init(dev, obj, size);
1247 mapping = obj->filp->f_mapping;
1248 mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32);
1251 /* Allocate memory if needed. */
1252 if (flags & OMAP_BO_MEM_DMA_API) {
1253 omap_obj->vaddr = dma_alloc_wc(dev->dev, size,
1254 &omap_obj->dma_addr,
1256 if (!omap_obj->vaddr)
1260 mutex_lock(&priv->list_lock);
1261 list_add(&omap_obj->mm_list, &priv->obj_list);
1262 mutex_unlock(&priv->list_lock);
1267 drm_gem_object_release(obj);
1273 struct drm_gem_object *omap_gem_new_dmabuf(struct drm_device *dev, size_t size,
1274 struct sg_table *sgt)
1276 struct omap_drm_private *priv = dev->dev_private;
1277 struct omap_gem_object *omap_obj;
1278 struct drm_gem_object *obj;
1279 union omap_gem_size gsize;
1281 /* Without a DMM only physically contiguous buffers can be supported. */
1282 if (sgt->orig_nents != 1 && !priv->has_dmm)
1283 return ERR_PTR(-EINVAL);
1285 gsize.bytes = PAGE_ALIGN(size);
1286 obj = omap_gem_new(dev, gsize, OMAP_BO_MEM_DMABUF | OMAP_BO_WC);
1288 return ERR_PTR(-ENOMEM);
1290 omap_obj = to_omap_bo(obj);
1292 mutex_lock(&omap_obj->lock);
1294 omap_obj->sgt = sgt;
1296 if (sgt->orig_nents == 1) {
1297 omap_obj->dma_addr = sg_dma_address(sgt->sgl);
1299 /* Create pages list from sgt */
1300 struct sg_page_iter iter;
1301 struct page **pages;
1302 unsigned int npages;
1305 npages = DIV_ROUND_UP(size, PAGE_SIZE);
1306 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1308 omap_gem_free_object(obj);
1309 obj = ERR_PTR(-ENOMEM);
1313 omap_obj->pages = pages;
1315 for_each_sg_page(sgt->sgl, &iter, sgt->orig_nents, 0) {
1316 pages[i++] = sg_page_iter_page(&iter);
1321 if (WARN_ON(i != npages)) {
1322 omap_gem_free_object(obj);
1323 obj = ERR_PTR(-ENOMEM);
1329 mutex_unlock(&omap_obj->lock);
1333 /* convenience method to construct a GEM buffer object, and userspace handle */
1334 int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1335 union omap_gem_size gsize, u32 flags, u32 *handle)
1337 struct drm_gem_object *obj;
1340 obj = omap_gem_new(dev, gsize, flags);
1344 ret = drm_gem_handle_create(file, obj, handle);
1346 omap_gem_free_object(obj);
1350 /* drop reference from allocate - handle holds it now */
1351 drm_gem_object_put_unlocked(obj);
1356 /* -----------------------------------------------------------------------------
1360 /* If DMM is used, we need to set some stuff up.. */
1361 void omap_gem_init(struct drm_device *dev)
1363 struct omap_drm_private *priv = dev->dev_private;
1364 struct omap_drm_usergart *usergart;
1365 const enum tiler_fmt fmts[] = {
1366 TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
1370 if (!dmm_is_available()) {
1371 /* DMM only supported on OMAP4 and later, so this isn't fatal */
1372 dev_warn(dev->dev, "DMM not available, disable DMM support\n");
1376 usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
1380 /* reserve 4k aligned/wide regions for userspace mappings: */
1381 for (i = 0; i < ARRAY_SIZE(fmts); i++) {
1382 u16 h = 1, w = PAGE_SIZE >> i;
1384 tiler_align(fmts[i], &w, &h);
1385 /* note: since each region is 1 4kb page wide, and minimum
1386 * number of rows, the height ends up being the same as the
1387 * # of pages in the region
1389 usergart[i].height = h;
1390 usergart[i].height_shift = ilog2(h);
1391 usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
1392 usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
1393 for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
1394 struct omap_drm_usergart_entry *entry;
1395 struct tiler_block *block;
1397 entry = &usergart[i].entry[j];
1398 block = tiler_reserve_2d(fmts[i], w, h, PAGE_SIZE);
1399 if (IS_ERR(block)) {
1401 "reserve failed: %d, %d, %ld\n",
1402 i, j, PTR_ERR(block));
1405 entry->dma_addr = tiler_ssptr(block);
1406 entry->block = block;
1408 DBG("%d:%d: %dx%d: dma_addr=%pad stride=%d", i, j, w, h,
1410 usergart[i].stride_pfn << PAGE_SHIFT);
1414 priv->usergart = usergart;
1415 priv->has_dmm = true;
1418 void omap_gem_deinit(struct drm_device *dev)
1420 struct omap_drm_private *priv = dev->dev_private;
1422 /* I believe we can rely on there being no more outstanding GEM
1423 * objects which could depend on usergart/dmm at this point.
1425 kfree(priv->usergart);