Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / gpu / drm / omapdrm / omap_gem.c
blob443469d4fa463e7782d0db1a9f380c46bef358ed
1 /*
2 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
3 * Author: Rob Clark <rob.clark@linaro.org>
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
12 * more details.
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>
25 #include "omap_drv.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;
42 uint32_t flags;
44 /** width/height for tiled formats (rounded up to slot boundaries) */
45 uint16_t width, height;
47 /** roll applied when mapping to DMM */
48 uint32_t roll;
50 /**
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().
71 dma_addr_t dma_addr;
73 /**
74 * # of users of dma_addr
76 uint32_t dma_addr_cnt;
78 /**
79 * If the buffer has been imported from a dmabuf the OMAP_DB_DMABUF flag
80 * is set and the sgt field is valid.
82 struct sg_table *sgt;
84 /**
85 * tiler block used when buffer is remapped in DMM/TILER.
87 struct tiler_block *block;
89 /**
90 * Array of backing pages, if allocated. Note that pages are never
91 * allocated for buffers originally allocated from contiguous memory
93 struct page **pages;
95 /** addresses corresponding to pages in above array */
96 dma_addr_t *dma_addrs;
98 /**
99 * Virtual address, if mapped.
101 void *vaddr;
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
115 * for later..
117 #define NUM_USERGART_ENTRIES 2
118 struct omap_drm_usergart_entry {
119 struct tiler_block *block; /* the reserved tiler block */
120 dma_addr_t dma_addr;
121 struct drm_gem_object *obj; /* the current pinned obj */
122 pgoff_t obj_pgoff; /* page offset of obj currently
123 mapped in */
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 /* -----------------------------------------------------------------------------
136 * Helpers
139 /** get mmap offset */
140 static uint64_t mmap_offset(struct drm_gem_object *obj)
142 struct drm_device *dev = obj->dev;
143 int ret;
144 size_t size;
146 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
148 /* Make it mmapable */
149 size = omap_gem_mmap_size(obj);
150 ret = drm_gem_create_mmap_offset_size(obj, size);
151 if (ret) {
152 dev_err(dev->dev, "could not allocate mmap offset\n");
153 return 0;
156 return drm_vma_node_offset_addr(&obj->vma_node);
159 static bool is_contiguous(struct omap_gem_object *omap_obj)
161 if (omap_obj->flags & OMAP_BO_MEM_DMA_API)
162 return true;
164 if ((omap_obj->flags & OMAP_BO_MEM_DMABUF) && omap_obj->sgt->nents == 1)
165 return true;
167 return false;
170 /* -----------------------------------------------------------------------------
171 * Eviction
174 static void evict_entry(struct drm_gem_object *obj,
175 enum tiler_fmt fmt, struct omap_drm_usergart_entry *entry)
177 struct omap_gem_object *omap_obj = to_omap_bo(obj);
178 struct omap_drm_private *priv = obj->dev->dev_private;
179 int n = priv->usergart[fmt].height;
180 size_t size = PAGE_SIZE * n;
181 loff_t off = mmap_offset(obj) +
182 (entry->obj_pgoff << PAGE_SHIFT);
183 const int m = DIV_ROUND_UP(omap_obj->width << fmt, PAGE_SIZE);
185 if (m > 1) {
186 int i;
187 /* if stride > than PAGE_SIZE then sparse mapping: */
188 for (i = n; i > 0; i--) {
189 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
190 off, PAGE_SIZE, 1);
191 off += PAGE_SIZE * m;
193 } else {
194 unmap_mapping_range(obj->dev->anon_inode->i_mapping,
195 off, size, 1);
198 entry->obj = NULL;
201 /* Evict a buffer from usergart, if it is mapped there */
202 static void evict(struct drm_gem_object *obj)
204 struct omap_gem_object *omap_obj = to_omap_bo(obj);
205 struct omap_drm_private *priv = obj->dev->dev_private;
207 if (omap_obj->flags & OMAP_BO_TILED) {
208 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
209 int i;
211 for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
212 struct omap_drm_usergart_entry *entry =
213 &priv->usergart[fmt].entry[i];
215 if (entry->obj == obj)
216 evict_entry(obj, fmt, entry);
221 /* -----------------------------------------------------------------------------
222 * Page Management
225 /** ensure backing pages are allocated */
226 static int omap_gem_attach_pages(struct drm_gem_object *obj)
228 struct drm_device *dev = obj->dev;
229 struct omap_gem_object *omap_obj = to_omap_bo(obj);
230 struct page **pages;
231 int npages = obj->size >> PAGE_SHIFT;
232 int i, ret;
233 dma_addr_t *addrs;
235 WARN_ON(omap_obj->pages);
237 pages = drm_gem_get_pages(obj);
238 if (IS_ERR(pages)) {
239 dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
240 return PTR_ERR(pages);
243 /* for non-cached buffers, ensure the new pages are clean because
244 * DSS, GPU, etc. are not cache coherent:
246 if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
247 addrs = kmalloc(npages * sizeof(*addrs), GFP_KERNEL);
248 if (!addrs) {
249 ret = -ENOMEM;
250 goto free_pages;
253 for (i = 0; i < npages; i++) {
254 addrs[i] = dma_map_page(dev->dev, pages[i],
255 0, PAGE_SIZE, DMA_TO_DEVICE);
257 if (dma_mapping_error(dev->dev, addrs[i])) {
258 dev_warn(dev->dev,
259 "%s: failed to map page\n", __func__);
261 for (i = i - 1; i >= 0; --i) {
262 dma_unmap_page(dev->dev, addrs[i],
263 PAGE_SIZE, DMA_TO_DEVICE);
266 ret = -ENOMEM;
267 goto free_addrs;
270 } else {
271 addrs = kzalloc(npages * sizeof(*addrs), GFP_KERNEL);
272 if (!addrs) {
273 ret = -ENOMEM;
274 goto free_pages;
278 omap_obj->dma_addrs = addrs;
279 omap_obj->pages = pages;
281 return 0;
283 free_addrs:
284 kfree(addrs);
285 free_pages:
286 drm_gem_put_pages(obj, pages, true, false);
288 return ret;
291 /* acquire pages when needed (for example, for DMA where physically
292 * contiguous buffer is not required
294 static int get_pages(struct drm_gem_object *obj, struct page ***pages)
296 struct omap_gem_object *omap_obj = to_omap_bo(obj);
297 int ret = 0;
299 if ((omap_obj->flags & OMAP_BO_MEM_SHMEM) && !omap_obj->pages) {
300 ret = omap_gem_attach_pages(obj);
301 if (ret) {
302 dev_err(obj->dev->dev, "could not attach pages\n");
303 return ret;
307 /* TODO: even phys-contig.. we should have a list of pages? */
308 *pages = omap_obj->pages;
310 return 0;
313 /** release backing pages */
314 static void omap_gem_detach_pages(struct drm_gem_object *obj)
316 struct omap_gem_object *omap_obj = to_omap_bo(obj);
317 unsigned int npages = obj->size >> PAGE_SHIFT;
318 unsigned int i;
320 for (i = 0; i < npages; i++) {
321 if (omap_obj->dma_addrs[i])
322 dma_unmap_page(obj->dev->dev, omap_obj->dma_addrs[i],
323 PAGE_SIZE, DMA_TO_DEVICE);
326 kfree(omap_obj->dma_addrs);
327 omap_obj->dma_addrs = NULL;
329 drm_gem_put_pages(obj, omap_obj->pages, true, false);
330 omap_obj->pages = NULL;
333 /* get buffer flags */
334 uint32_t omap_gem_flags(struct drm_gem_object *obj)
336 return to_omap_bo(obj)->flags;
339 uint64_t omap_gem_mmap_offset(struct drm_gem_object *obj)
341 uint64_t offset;
342 mutex_lock(&obj->dev->struct_mutex);
343 offset = mmap_offset(obj);
344 mutex_unlock(&obj->dev->struct_mutex);
345 return offset;
348 /** get mmap size */
349 size_t omap_gem_mmap_size(struct drm_gem_object *obj)
351 struct omap_gem_object *omap_obj = to_omap_bo(obj);
352 size_t size = obj->size;
354 if (omap_obj->flags & OMAP_BO_TILED) {
355 /* for tiled buffers, the virtual size has stride rounded up
356 * to 4kb.. (to hide the fact that row n+1 might start 16kb or
357 * 32kb later!). But we don't back the entire buffer with
358 * pages, only the valid picture part.. so need to adjust for
359 * this in the size used to mmap and generate mmap offset
361 size = tiler_vsize(gem2fmt(omap_obj->flags),
362 omap_obj->width, omap_obj->height);
365 return size;
368 /* -----------------------------------------------------------------------------
369 * Fault Handling
372 /* Normal handling for the case of faulting in non-tiled buffers */
373 static int fault_1d(struct drm_gem_object *obj,
374 struct vm_area_struct *vma, struct vm_fault *vmf)
376 struct omap_gem_object *omap_obj = to_omap_bo(obj);
377 unsigned long pfn;
378 pgoff_t pgoff;
380 /* We don't use vmf->pgoff since that has the fake offset: */
381 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
383 if (omap_obj->pages) {
384 omap_gem_cpu_sync_page(obj, pgoff);
385 pfn = page_to_pfn(omap_obj->pages[pgoff]);
386 } else {
387 BUG_ON(!is_contiguous(omap_obj));
388 pfn = (omap_obj->dma_addr >> PAGE_SHIFT) + pgoff;
391 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
392 pfn, pfn << PAGE_SHIFT);
394 return vm_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV));
397 /* Special handling for the case of faulting in 2d tiled buffers */
398 static int fault_2d(struct drm_gem_object *obj,
399 struct vm_area_struct *vma, struct vm_fault *vmf)
401 struct omap_gem_object *omap_obj = to_omap_bo(obj);
402 struct omap_drm_private *priv = obj->dev->dev_private;
403 struct omap_drm_usergart_entry *entry;
404 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
405 struct page *pages[64]; /* XXX is this too much to have on stack? */
406 unsigned long pfn;
407 pgoff_t pgoff, base_pgoff;
408 unsigned long vaddr;
409 int i, ret, slots;
412 * Note the height of the slot is also equal to the number of pages
413 * that need to be mapped in to fill 4kb wide CPU page. If the slot
414 * height is 64, then 64 pages fill a 4kb wide by 64 row region.
416 const int n = priv->usergart[fmt].height;
417 const int n_shift = priv->usergart[fmt].height_shift;
420 * If buffer width in bytes > PAGE_SIZE then the virtual stride is
421 * rounded up to next multiple of PAGE_SIZE.. this need to be taken
422 * into account in some of the math, so figure out virtual stride
423 * in pages
425 const int m = DIV_ROUND_UP(omap_obj->width << fmt, PAGE_SIZE);
427 /* We don't use vmf->pgoff since that has the fake offset: */
428 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
431 * Actual address we start mapping at is rounded down to previous slot
432 * boundary in the y direction:
434 base_pgoff = round_down(pgoff, m << n_shift);
436 /* figure out buffer width in slots */
437 slots = omap_obj->width >> priv->usergart[fmt].slot_shift;
439 vaddr = vmf->address - ((pgoff - base_pgoff) << PAGE_SHIFT);
441 entry = &priv->usergart[fmt].entry[priv->usergart[fmt].last];
443 /* evict previous buffer using this usergart entry, if any: */
444 if (entry->obj)
445 evict_entry(entry->obj, fmt, entry);
447 entry->obj = obj;
448 entry->obj_pgoff = base_pgoff;
450 /* now convert base_pgoff to phys offset from virt offset: */
451 base_pgoff = (base_pgoff >> n_shift) * slots;
453 /* for wider-than 4k.. figure out which part of the slot-row we want: */
454 if (m > 1) {
455 int off = pgoff % m;
456 entry->obj_pgoff += off;
457 base_pgoff /= m;
458 slots = min(slots - (off << n_shift), n);
459 base_pgoff += off << n_shift;
460 vaddr += off << PAGE_SHIFT;
464 * Map in pages. Beyond the valid pixel part of the buffer, we set
465 * pages[i] to NULL to get a dummy page mapped in.. if someone
466 * reads/writes it they will get random/undefined content, but at
467 * least it won't be corrupting whatever other random page used to
468 * be mapped in, or other undefined behavior.
470 memcpy(pages, &omap_obj->pages[base_pgoff],
471 sizeof(struct page *) * slots);
472 memset(pages + slots, 0,
473 sizeof(struct page *) * (n - slots));
475 ret = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
476 if (ret) {
477 dev_err(obj->dev->dev, "failed to pin: %d\n", ret);
478 return ret;
481 pfn = entry->dma_addr >> PAGE_SHIFT;
483 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
484 pfn, pfn << PAGE_SHIFT);
486 for (i = n; i > 0; i--) {
487 vm_insert_mixed(vma, vaddr, __pfn_to_pfn_t(pfn, PFN_DEV));
488 pfn += priv->usergart[fmt].stride_pfn;
489 vaddr += PAGE_SIZE * m;
492 /* simple round-robin: */
493 priv->usergart[fmt].last = (priv->usergart[fmt].last + 1)
494 % NUM_USERGART_ENTRIES;
496 return 0;
500 * omap_gem_fault - pagefault handler for GEM objects
501 * @vmf: fault detail
503 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
504 * does most of the work for us including the actual map/unmap calls
505 * but we need to do the actual page work.
507 * The VMA was set up by GEM. In doing so it also ensured that the
508 * vma->vm_private_data points to the GEM object that is backing this
509 * mapping.
511 int omap_gem_fault(struct vm_fault *vmf)
513 struct vm_area_struct *vma = vmf->vma;
514 struct drm_gem_object *obj = vma->vm_private_data;
515 struct omap_gem_object *omap_obj = to_omap_bo(obj);
516 struct drm_device *dev = obj->dev;
517 struct page **pages;
518 int ret;
520 /* Make sure we don't parallel update on a fault, nor move or remove
521 * something from beneath our feet
523 mutex_lock(&dev->struct_mutex);
525 /* if a shmem backed object, make sure we have pages attached now */
526 ret = get_pages(obj, &pages);
527 if (ret)
528 goto fail;
530 /* where should we do corresponding put_pages().. we are mapping
531 * the original page, rather than thru a GART, so we can't rely
532 * on eviction to trigger this. But munmap() or all mappings should
533 * probably trigger put_pages()?
536 if (omap_obj->flags & OMAP_BO_TILED)
537 ret = fault_2d(obj, vma, vmf);
538 else
539 ret = fault_1d(obj, vma, vmf);
542 fail:
543 mutex_unlock(&dev->struct_mutex);
544 switch (ret) {
545 case 0:
546 case -ERESTARTSYS:
547 case -EINTR:
548 case -EBUSY:
550 * EBUSY is ok: this just means that another thread
551 * already did the job.
553 return VM_FAULT_NOPAGE;
554 case -ENOMEM:
555 return VM_FAULT_OOM;
556 default:
557 return VM_FAULT_SIGBUS;
561 /** We override mainly to fix up some of the vm mapping flags.. */
562 int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma)
564 int ret;
566 ret = drm_gem_mmap(filp, vma);
567 if (ret) {
568 DBG("mmap failed: %d", ret);
569 return ret;
572 return omap_gem_mmap_obj(vma->vm_private_data, vma);
575 int omap_gem_mmap_obj(struct drm_gem_object *obj,
576 struct vm_area_struct *vma)
578 struct omap_gem_object *omap_obj = to_omap_bo(obj);
580 vma->vm_flags &= ~VM_PFNMAP;
581 vma->vm_flags |= VM_MIXEDMAP;
583 if (omap_obj->flags & OMAP_BO_WC) {
584 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
585 } else if (omap_obj->flags & OMAP_BO_UNCACHED) {
586 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
587 } else {
589 * We do have some private objects, at least for scanout buffers
590 * on hardware without DMM/TILER. But these are allocated write-
591 * combine
593 if (WARN_ON(!obj->filp))
594 return -EINVAL;
597 * Shunt off cached objs to shmem file so they have their own
598 * address_space (so unmap_mapping_range does what we want,
599 * in particular in the case of mmap'd dmabufs)
601 fput(vma->vm_file);
602 vma->vm_pgoff = 0;
603 vma->vm_file = get_file(obj->filp);
605 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
608 return 0;
611 /* -----------------------------------------------------------------------------
612 * Dumb Buffers
616 * omap_gem_dumb_create - create a dumb buffer
617 * @drm_file: our client file
618 * @dev: our device
619 * @args: the requested arguments copied from userspace
621 * Allocate a buffer suitable for use for a frame buffer of the
622 * form described by user space. Give userspace a handle by which
623 * to reference it.
625 int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
626 struct drm_mode_create_dumb *args)
628 union omap_gem_size gsize;
630 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
632 args->size = PAGE_ALIGN(args->pitch * args->height);
634 gsize = (union omap_gem_size){
635 .bytes = args->size,
638 return omap_gem_new_handle(dev, file, gsize,
639 OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle);
643 * omap_gem_dumb_map - buffer mapping for dumb interface
644 * @file: our drm client file
645 * @dev: drm device
646 * @handle: GEM handle to the object (from dumb_create)
648 * Do the necessary setup to allow the mapping of the frame buffer
649 * into user memory. We don't have to do much here at the moment.
651 int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
652 uint32_t handle, uint64_t *offset)
654 struct drm_gem_object *obj;
655 int ret = 0;
657 /* GEM does all our handle to object mapping */
658 obj = drm_gem_object_lookup(file, handle);
659 if (obj == NULL) {
660 ret = -ENOENT;
661 goto fail;
664 *offset = omap_gem_mmap_offset(obj);
666 drm_gem_object_unreference_unlocked(obj);
668 fail:
669 return ret;
672 #ifdef CONFIG_DRM_FBDEV_EMULATION
673 /* Set scrolling position. This allows us to implement fast scrolling
674 * for console.
676 * Call only from non-atomic contexts.
678 int omap_gem_roll(struct drm_gem_object *obj, uint32_t roll)
680 struct omap_gem_object *omap_obj = to_omap_bo(obj);
681 uint32_t npages = obj->size >> PAGE_SHIFT;
682 int ret = 0;
684 if (roll > npages) {
685 dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
686 return -EINVAL;
689 omap_obj->roll = roll;
691 mutex_lock(&obj->dev->struct_mutex);
693 /* if we aren't mapped yet, we don't need to do anything */
694 if (omap_obj->block) {
695 struct page **pages;
696 ret = get_pages(obj, &pages);
697 if (ret)
698 goto fail;
699 ret = tiler_pin(omap_obj->block, pages, npages, roll, true);
700 if (ret)
701 dev_err(obj->dev->dev, "could not repin: %d\n", ret);
704 fail:
705 mutex_unlock(&obj->dev->struct_mutex);
707 return ret;
709 #endif
711 /* -----------------------------------------------------------------------------
712 * Memory Management & DMA Sync
716 * shmem buffers that are mapped cached are not coherent.
718 * We keep track of dirty pages using page faulting to perform cache management.
719 * When a page is mapped to the CPU in read/write mode the device can't access
720 * it and omap_obj->dma_addrs[i] is NULL. When a page is mapped to the device
721 * the omap_obj->dma_addrs[i] is set to the DMA address, and the page is
722 * unmapped from the CPU.
724 static inline bool is_cached_coherent(struct drm_gem_object *obj)
726 struct omap_gem_object *omap_obj = to_omap_bo(obj);
728 return !((omap_obj->flags & OMAP_BO_MEM_SHMEM) &&
729 ((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED));
732 /* Sync the buffer for CPU access.. note pages should already be
733 * attached, ie. omap_gem_get_pages()
735 void omap_gem_cpu_sync_page(struct drm_gem_object *obj, int pgoff)
737 struct drm_device *dev = obj->dev;
738 struct omap_gem_object *omap_obj = to_omap_bo(obj);
740 if (is_cached_coherent(obj))
741 return;
743 if (omap_obj->dma_addrs[pgoff]) {
744 dma_unmap_page(dev->dev, omap_obj->dma_addrs[pgoff],
745 PAGE_SIZE, DMA_TO_DEVICE);
746 omap_obj->dma_addrs[pgoff] = 0;
750 /* sync the buffer for DMA access */
751 void omap_gem_dma_sync_buffer(struct drm_gem_object *obj,
752 enum dma_data_direction dir)
754 struct drm_device *dev = obj->dev;
755 struct omap_gem_object *omap_obj = to_omap_bo(obj);
756 int i, npages = obj->size >> PAGE_SHIFT;
757 struct page **pages = omap_obj->pages;
758 bool dirty = false;
760 if (is_cached_coherent(obj))
761 return;
763 for (i = 0; i < npages; i++) {
764 if (!omap_obj->dma_addrs[i]) {
765 dma_addr_t addr;
767 addr = dma_map_page(dev->dev, pages[i], 0,
768 PAGE_SIZE, dir);
769 if (dma_mapping_error(dev->dev, addr)) {
770 dev_warn(dev->dev, "%s: failed to map page\n",
771 __func__);
772 break;
775 dirty = true;
776 omap_obj->dma_addrs[i] = addr;
780 if (dirty) {
781 unmap_mapping_range(obj->filp->f_mapping, 0,
782 omap_gem_mmap_size(obj), 1);
787 * omap_gem_pin() - Pin a GEM object in memory
788 * @obj: the GEM object
789 * @dma_addr: the DMA address
791 * Pin the given GEM object in memory and fill the dma_addr pointer with the
792 * object's DMA address. If the buffer is not physically contiguous it will be
793 * remapped through the TILER to provide a contiguous view.
795 * Pins are reference-counted, calling this function multiple times is allowed
796 * as long the corresponding omap_gem_unpin() calls are balanced.
798 * Return 0 on success or a negative error code otherwise.
800 int omap_gem_pin(struct drm_gem_object *obj, dma_addr_t *dma_addr)
802 struct omap_drm_private *priv = obj->dev->dev_private;
803 struct omap_gem_object *omap_obj = to_omap_bo(obj);
804 int ret = 0;
806 mutex_lock(&obj->dev->struct_mutex);
808 if (!is_contiguous(omap_obj) && priv->has_dmm) {
809 if (omap_obj->dma_addr_cnt == 0) {
810 struct page **pages;
811 uint32_t npages = obj->size >> PAGE_SHIFT;
812 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
813 struct tiler_block *block;
815 BUG_ON(omap_obj->block);
817 ret = get_pages(obj, &pages);
818 if (ret)
819 goto fail;
821 if (omap_obj->flags & OMAP_BO_TILED) {
822 block = tiler_reserve_2d(fmt,
823 omap_obj->width,
824 omap_obj->height, 0);
825 } else {
826 block = tiler_reserve_1d(obj->size);
829 if (IS_ERR(block)) {
830 ret = PTR_ERR(block);
831 dev_err(obj->dev->dev,
832 "could not remap: %d (%d)\n", ret, fmt);
833 goto fail;
836 /* TODO: enable async refill.. */
837 ret = tiler_pin(block, pages, npages,
838 omap_obj->roll, true);
839 if (ret) {
840 tiler_release(block);
841 dev_err(obj->dev->dev,
842 "could not pin: %d\n", ret);
843 goto fail;
846 omap_obj->dma_addr = tiler_ssptr(block);
847 omap_obj->block = block;
849 DBG("got dma address: %pad", &omap_obj->dma_addr);
852 omap_obj->dma_addr_cnt++;
854 *dma_addr = omap_obj->dma_addr;
855 } else if (is_contiguous(omap_obj)) {
856 *dma_addr = omap_obj->dma_addr;
857 } else {
858 ret = -EINVAL;
859 goto fail;
862 fail:
863 mutex_unlock(&obj->dev->struct_mutex);
865 return ret;
869 * omap_gem_unpin() - Unpin a GEM object from memory
870 * @obj: the GEM object
872 * Unpin the given GEM object previously pinned with omap_gem_pin(). Pins are
873 * reference-counted, the actualy unpin will only be performed when the number
874 * of calls to this function matches the number of calls to omap_gem_pin().
876 void omap_gem_unpin(struct drm_gem_object *obj)
878 struct omap_gem_object *omap_obj = to_omap_bo(obj);
879 int ret;
881 mutex_lock(&obj->dev->struct_mutex);
882 if (omap_obj->dma_addr_cnt > 0) {
883 omap_obj->dma_addr_cnt--;
884 if (omap_obj->dma_addr_cnt == 0) {
885 ret = tiler_unpin(omap_obj->block);
886 if (ret) {
887 dev_err(obj->dev->dev,
888 "could not unpin pages: %d\n", ret);
890 ret = tiler_release(omap_obj->block);
891 if (ret) {
892 dev_err(obj->dev->dev,
893 "could not release unmap: %d\n", ret);
895 omap_obj->dma_addr = 0;
896 omap_obj->block = NULL;
900 mutex_unlock(&obj->dev->struct_mutex);
903 /* Get rotated scanout address (only valid if already pinned), at the
904 * specified orientation and x,y offset from top-left corner of buffer
905 * (only valid for tiled 2d buffers)
907 int omap_gem_rotated_dma_addr(struct drm_gem_object *obj, uint32_t orient,
908 int x, int y, dma_addr_t *dma_addr)
910 struct omap_gem_object *omap_obj = to_omap_bo(obj);
911 int ret = -EINVAL;
913 mutex_lock(&obj->dev->struct_mutex);
914 if ((omap_obj->dma_addr_cnt > 0) && omap_obj->block &&
915 (omap_obj->flags & OMAP_BO_TILED)) {
916 *dma_addr = tiler_tsptr(omap_obj->block, orient, x, y);
917 ret = 0;
919 mutex_unlock(&obj->dev->struct_mutex);
920 return ret;
923 /* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
924 int omap_gem_tiled_stride(struct drm_gem_object *obj, uint32_t orient)
926 struct omap_gem_object *omap_obj = to_omap_bo(obj);
927 int ret = -EINVAL;
928 if (omap_obj->flags & OMAP_BO_TILED)
929 ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
930 return ret;
933 /* if !remap, and we don't have pages backing, then fail, rather than
934 * increasing the pin count (which we don't really do yet anyways,
935 * because we don't support swapping pages back out). And 'remap'
936 * might not be quite the right name, but I wanted to keep it working
937 * similarly to omap_gem_pin(). Note though that mutex is not
938 * aquired if !remap (because this can be called in atomic ctxt),
939 * but probably omap_gem_unpin() should be changed to work in the
940 * same way. If !remap, a matching omap_gem_put_pages() call is not
941 * required (and should not be made).
943 int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
944 bool remap)
946 int ret;
947 if (!remap) {
948 struct omap_gem_object *omap_obj = to_omap_bo(obj);
949 if (!omap_obj->pages)
950 return -ENOMEM;
951 *pages = omap_obj->pages;
952 return 0;
954 mutex_lock(&obj->dev->struct_mutex);
955 ret = get_pages(obj, pages);
956 mutex_unlock(&obj->dev->struct_mutex);
957 return ret;
960 /* release pages when DMA no longer being performed */
961 int omap_gem_put_pages(struct drm_gem_object *obj)
963 /* do something here if we dynamically attach/detach pages.. at
964 * least they would no longer need to be pinned if everyone has
965 * released the pages..
967 return 0;
970 #ifdef CONFIG_DRM_FBDEV_EMULATION
971 /* Get kernel virtual address for CPU access.. this more or less only
972 * exists for omap_fbdev. This should be called with struct_mutex
973 * held.
975 void *omap_gem_vaddr(struct drm_gem_object *obj)
977 struct omap_gem_object *omap_obj = to_omap_bo(obj);
978 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
979 if (!omap_obj->vaddr) {
980 struct page **pages;
981 int ret = get_pages(obj, &pages);
982 if (ret)
983 return ERR_PTR(ret);
984 omap_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
985 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
987 return omap_obj->vaddr;
989 #endif
991 /* -----------------------------------------------------------------------------
992 * Power Management
995 #ifdef CONFIG_PM
996 /* re-pin objects in DMM in resume path: */
997 int omap_gem_resume(struct drm_device *dev)
999 struct omap_drm_private *priv = dev->dev_private;
1000 struct omap_gem_object *omap_obj;
1001 int ret = 0;
1003 list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
1004 if (omap_obj->block) {
1005 struct drm_gem_object *obj = &omap_obj->base;
1006 uint32_t npages = obj->size >> PAGE_SHIFT;
1007 WARN_ON(!omap_obj->pages); /* this can't happen */
1008 ret = tiler_pin(omap_obj->block,
1009 omap_obj->pages, npages,
1010 omap_obj->roll, true);
1011 if (ret) {
1012 dev_err(dev->dev, "could not repin: %d\n", ret);
1013 return ret;
1018 return 0;
1020 #endif
1022 /* -----------------------------------------------------------------------------
1023 * DebugFS
1026 #ifdef CONFIG_DEBUG_FS
1027 void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
1029 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1030 uint64_t off;
1032 off = drm_vma_node_start(&obj->vma_node);
1034 seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d",
1035 omap_obj->flags, obj->name, kref_read(&obj->refcount),
1036 off, &omap_obj->dma_addr, omap_obj->dma_addr_cnt,
1037 omap_obj->vaddr, omap_obj->roll);
1039 if (omap_obj->flags & OMAP_BO_TILED) {
1040 seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
1041 if (omap_obj->block) {
1042 struct tcm_area *area = &omap_obj->block->area;
1043 seq_printf(m, " (%dx%d, %dx%d)",
1044 area->p0.x, area->p0.y,
1045 area->p1.x, area->p1.y);
1047 } else {
1048 seq_printf(m, " %zu", obj->size);
1051 seq_printf(m, "\n");
1054 void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
1056 struct omap_gem_object *omap_obj;
1057 int count = 0;
1058 size_t size = 0;
1060 list_for_each_entry(omap_obj, list, mm_list) {
1061 struct drm_gem_object *obj = &omap_obj->base;
1062 seq_printf(m, " ");
1063 omap_gem_describe(obj, m);
1064 count++;
1065 size += obj->size;
1068 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
1070 #endif
1072 /* -----------------------------------------------------------------------------
1073 * Constructor & Destructor
1076 void omap_gem_free_object(struct drm_gem_object *obj)
1078 struct drm_device *dev = obj->dev;
1079 struct omap_drm_private *priv = dev->dev_private;
1080 struct omap_gem_object *omap_obj = to_omap_bo(obj);
1082 evict(obj);
1084 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
1086 spin_lock(&priv->list_lock);
1087 list_del(&omap_obj->mm_list);
1088 spin_unlock(&priv->list_lock);
1090 /* this means the object is still pinned.. which really should
1091 * not happen. I think..
1093 WARN_ON(omap_obj->dma_addr_cnt > 0);
1095 if (omap_obj->pages) {
1096 if (omap_obj->flags & OMAP_BO_MEM_DMABUF)
1097 kfree(omap_obj->pages);
1098 else
1099 omap_gem_detach_pages(obj);
1102 if (omap_obj->flags & OMAP_BO_MEM_DMA_API) {
1103 dma_free_wc(dev->dev, obj->size, omap_obj->vaddr,
1104 omap_obj->dma_addr);
1105 } else if (omap_obj->vaddr) {
1106 vunmap(omap_obj->vaddr);
1107 } else if (obj->import_attach) {
1108 drm_prime_gem_destroy(obj, omap_obj->sgt);
1111 drm_gem_object_release(obj);
1113 kfree(omap_obj);
1116 /* GEM buffer object constructor */
1117 struct drm_gem_object *omap_gem_new(struct drm_device *dev,
1118 union omap_gem_size gsize, uint32_t flags)
1120 struct omap_drm_private *priv = dev->dev_private;
1121 struct omap_gem_object *omap_obj;
1122 struct drm_gem_object *obj;
1123 struct address_space *mapping;
1124 size_t size;
1125 int ret;
1127 /* Validate the flags and compute the memory and cache flags. */
1128 if (flags & OMAP_BO_TILED) {
1129 if (!priv->usergart) {
1130 dev_err(dev->dev, "Tiled buffers require DMM\n");
1131 return NULL;
1135 * Tiled buffers are always shmem paged backed. When they are
1136 * scanned out, they are remapped into DMM/TILER.
1138 flags &= ~OMAP_BO_SCANOUT;
1139 flags |= OMAP_BO_MEM_SHMEM;
1142 * Currently don't allow cached buffers. There is some caching
1143 * stuff that needs to be handled better.
1145 flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED);
1146 flags |= tiler_get_cpu_cache_flags();
1147 } else if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
1149 * OMAP_BO_SCANOUT hints that the buffer doesn't need to be
1150 * tiled. However, to lower the pressure on memory allocation,
1151 * use contiguous memory only if no TILER is available.
1153 flags |= OMAP_BO_MEM_DMA_API;
1154 } else if (!(flags & OMAP_BO_MEM_DMABUF)) {
1156 * All other buffers not backed by dma_buf are shmem-backed.
1158 flags |= OMAP_BO_MEM_SHMEM;
1161 /* Allocate the initialize the OMAP GEM object. */
1162 omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
1163 if (!omap_obj)
1164 return NULL;
1166 obj = &omap_obj->base;
1167 omap_obj->flags = flags;
1169 if (flags & OMAP_BO_TILED) {
1171 * For tiled buffers align dimensions to slot boundaries and
1172 * calculate size based on aligned dimensions.
1174 tiler_align(gem2fmt(flags), &gsize.tiled.width,
1175 &gsize.tiled.height);
1177 size = tiler_size(gem2fmt(flags), gsize.tiled.width,
1178 gsize.tiled.height);
1180 omap_obj->width = gsize.tiled.width;
1181 omap_obj->height = gsize.tiled.height;
1182 } else {
1183 size = PAGE_ALIGN(gsize.bytes);
1186 /* Initialize the GEM object. */
1187 if (!(flags & OMAP_BO_MEM_SHMEM)) {
1188 drm_gem_private_object_init(dev, obj, size);
1189 } else {
1190 ret = drm_gem_object_init(dev, obj, size);
1191 if (ret)
1192 goto err_free;
1194 mapping = obj->filp->f_mapping;
1195 mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32);
1198 /* Allocate memory if needed. */
1199 if (flags & OMAP_BO_MEM_DMA_API) {
1200 omap_obj->vaddr = dma_alloc_wc(dev->dev, size,
1201 &omap_obj->dma_addr,
1202 GFP_KERNEL);
1203 if (!omap_obj->vaddr)
1204 goto err_release;
1207 spin_lock(&priv->list_lock);
1208 list_add(&omap_obj->mm_list, &priv->obj_list);
1209 spin_unlock(&priv->list_lock);
1211 return obj;
1213 err_release:
1214 drm_gem_object_release(obj);
1215 err_free:
1216 kfree(omap_obj);
1217 return NULL;
1220 struct drm_gem_object *omap_gem_new_dmabuf(struct drm_device *dev, size_t size,
1221 struct sg_table *sgt)
1223 struct omap_drm_private *priv = dev->dev_private;
1224 struct omap_gem_object *omap_obj;
1225 struct drm_gem_object *obj;
1226 union omap_gem_size gsize;
1228 /* Without a DMM only physically contiguous buffers can be supported. */
1229 if (sgt->orig_nents != 1 && !priv->has_dmm)
1230 return ERR_PTR(-EINVAL);
1232 mutex_lock(&dev->struct_mutex);
1234 gsize.bytes = PAGE_ALIGN(size);
1235 obj = omap_gem_new(dev, gsize, OMAP_BO_MEM_DMABUF | OMAP_BO_WC);
1236 if (!obj) {
1237 obj = ERR_PTR(-ENOMEM);
1238 goto done;
1241 omap_obj = to_omap_bo(obj);
1242 omap_obj->sgt = sgt;
1244 if (sgt->orig_nents == 1) {
1245 omap_obj->dma_addr = sg_dma_address(sgt->sgl);
1246 } else {
1247 /* Create pages list from sgt */
1248 struct sg_page_iter iter;
1249 struct page **pages;
1250 unsigned int npages;
1251 unsigned int i = 0;
1253 npages = DIV_ROUND_UP(size, PAGE_SIZE);
1254 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1255 if (!pages) {
1256 omap_gem_free_object(obj);
1257 obj = ERR_PTR(-ENOMEM);
1258 goto done;
1261 omap_obj->pages = pages;
1263 for_each_sg_page(sgt->sgl, &iter, sgt->orig_nents, 0) {
1264 pages[i++] = sg_page_iter_page(&iter);
1265 if (i > npages)
1266 break;
1269 if (WARN_ON(i != npages)) {
1270 omap_gem_free_object(obj);
1271 obj = ERR_PTR(-ENOMEM);
1272 goto done;
1276 done:
1277 mutex_unlock(&dev->struct_mutex);
1278 return obj;
1281 /* convenience method to construct a GEM buffer object, and userspace handle */
1282 int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1283 union omap_gem_size gsize, uint32_t flags, uint32_t *handle)
1285 struct drm_gem_object *obj;
1286 int ret;
1288 obj = omap_gem_new(dev, gsize, flags);
1289 if (!obj)
1290 return -ENOMEM;
1292 ret = drm_gem_handle_create(file, obj, handle);
1293 if (ret) {
1294 omap_gem_free_object(obj);
1295 return ret;
1298 /* drop reference from allocate - handle holds it now */
1299 drm_gem_object_unreference_unlocked(obj);
1301 return 0;
1304 /* -----------------------------------------------------------------------------
1305 * Init & Cleanup
1308 /* If DMM is used, we need to set some stuff up.. */
1309 void omap_gem_init(struct drm_device *dev)
1311 struct omap_drm_private *priv = dev->dev_private;
1312 struct omap_drm_usergart *usergart;
1313 const enum tiler_fmt fmts[] = {
1314 TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
1316 int i, j;
1318 if (!dmm_is_available()) {
1319 /* DMM only supported on OMAP4 and later, so this isn't fatal */
1320 dev_warn(dev->dev, "DMM not available, disable DMM support\n");
1321 return;
1324 usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
1325 if (!usergart)
1326 return;
1328 /* reserve 4k aligned/wide regions for userspace mappings: */
1329 for (i = 0; i < ARRAY_SIZE(fmts); i++) {
1330 uint16_t h = 1, w = PAGE_SIZE >> i;
1331 tiler_align(fmts[i], &w, &h);
1332 /* note: since each region is 1 4kb page wide, and minimum
1333 * number of rows, the height ends up being the same as the
1334 * # of pages in the region
1336 usergart[i].height = h;
1337 usergart[i].height_shift = ilog2(h);
1338 usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
1339 usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
1340 for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
1341 struct omap_drm_usergart_entry *entry;
1342 struct tiler_block *block;
1344 entry = &usergart[i].entry[j];
1345 block = tiler_reserve_2d(fmts[i], w, h, PAGE_SIZE);
1346 if (IS_ERR(block)) {
1347 dev_err(dev->dev,
1348 "reserve failed: %d, %d, %ld\n",
1349 i, j, PTR_ERR(block));
1350 return;
1352 entry->dma_addr = tiler_ssptr(block);
1353 entry->block = block;
1355 DBG("%d:%d: %dx%d: dma_addr=%pad stride=%d", i, j, w, h,
1356 &entry->dma_addr,
1357 usergart[i].stride_pfn << PAGE_SHIFT);
1361 priv->usergart = usergart;
1362 priv->has_dmm = true;
1365 void omap_gem_deinit(struct drm_device *dev)
1367 struct omap_drm_private *priv = dev->dev_private;
1369 /* I believe we can rely on there being no more outstanding GEM
1370 * objects which could depend on usergart/dmm at this point.
1372 kfree(priv->usergart);