2 * drivers/gpu/drm/omapdrm/omap_gem.c
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob.clark@linaro.org>
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/>.
21 #include <linux/spinlock.h>
22 #include <linux/shmem_fs.h>
23 #include <drm/drm_vma_manager.h>
26 #include "omap_dmm_tiler.h"
28 /* remove these once drm core helpers are merged */
29 struct page
**_drm_gem_get_pages(struct drm_gem_object
*obj
, gfp_t gfpmask
);
30 void _drm_gem_put_pages(struct drm_gem_object
*obj
, struct page
**pages
,
31 bool dirty
, bool accessed
);
32 int _drm_gem_create_mmap_offset_size(struct drm_gem_object
*obj
, size_t size
);
35 * GEM buffer object implementation.
38 #define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
40 /* note: we use upper 8 bits of flags for driver-internal flags: */
41 #define OMAP_BO_DMA 0x01000000 /* actually is physically contiguous */
42 #define OMAP_BO_EXT_SYNC 0x02000000 /* externally allocated sync object */
43 #define OMAP_BO_EXT_MEM 0x04000000 /* externally allocated memory */
46 struct omap_gem_object
{
47 struct drm_gem_object base
;
49 struct list_head mm_list
;
53 /** width/height for tiled formats (rounded up to slot boundaries) */
54 uint16_t width
, height
;
56 /** roll applied when mapping to DMM */
60 * If buffer is allocated physically contiguous, the OMAP_BO_DMA flag
61 * is set and the paddr is valid. Also if the buffer is remapped in
62 * TILER and paddr_cnt > 0, then paddr is valid. But if you are using
63 * the physical address and OMAP_BO_DMA is not set, then you should
64 * be going thru omap_gem_{get,put}_paddr() to ensure the mapping is
65 * not removed from under your feet.
67 * Note that OMAP_BO_SCANOUT is a hint from userspace that DMA capable
68 * buffer is requested, but doesn't mean that it is. Use the
69 * OMAP_BO_DMA flag to determine if the buffer has a DMA capable
80 * tiler block used when buffer is remapped in DMM/TILER.
82 struct tiler_block
*block
;
85 * Array of backing pages, if allocated. Note that pages are never
86 * allocated for buffers originally allocated from contiguous memory
90 /** addresses corresponding to pages in above array */
94 * Virtual address, if mapped.
99 * sync-object allocated on demand (if needed)
101 * Per-buffer sync-object for tracking pending and completed hw/dma
102 * read and write operations. The layout in memory is dictated by
103 * the SGX firmware, which uses this information to stall the command
104 * stream if a surface is not ready yet.
106 * Note that when buffer is used by SGX, the sync-object needs to be
107 * allocated from a special heap of sync-objects. This way many sync
108 * objects can be packed in a page, and not waste GPU virtual address
109 * space. Because of this we have to have a omap_gem_set_sync_object()
110 * API to allow replacement of the syncobj after it has (potentially)
111 * already been allocated. A bit ugly but I haven't thought of a
112 * better alternative.
115 uint32_t write_pending
;
116 uint32_t write_complete
;
117 uint32_t read_pending
;
118 uint32_t read_complete
;
122 static int get_pages(struct drm_gem_object
*obj
, struct page
***pages
);
123 static uint64_t mmap_offset(struct drm_gem_object
*obj
);
125 /* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
126 * not necessarily pinned in TILER all the time, and (b) when they are
127 * they are not necessarily page aligned, we reserve one or more small
128 * regions in each of the 2d containers to use as a user-GART where we
129 * can create a second page-aligned mapping of parts of the buffer
130 * being accessed from userspace.
132 * Note that we could optimize slightly when we know that multiple
133 * tiler containers are backed by the same PAT.. but I'll leave that
136 #define NUM_USERGART_ENTRIES 2
137 struct usergart_entry
{
138 struct tiler_block
*block
; /* the reserved tiler block */
140 struct drm_gem_object
*obj
; /* the current pinned obj */
141 pgoff_t obj_pgoff
; /* page offset of obj currently
145 struct usergart_entry entry
[NUM_USERGART_ENTRIES
];
146 int height
; /* height in rows */
147 int height_shift
; /* ilog2(height in rows) */
148 int slot_shift
; /* ilog2(width per slot) */
149 int stride_pfn
; /* stride in pages */
150 int last
; /* index of last used entry */
153 static void evict_entry(struct drm_gem_object
*obj
,
154 enum tiler_fmt fmt
, struct usergart_entry
*entry
)
156 if (obj
->dev
->dev_mapping
) {
157 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
158 int n
= usergart
[fmt
].height
;
159 size_t size
= PAGE_SIZE
* n
;
160 loff_t off
= mmap_offset(obj
) +
161 (entry
->obj_pgoff
<< PAGE_SHIFT
);
162 const int m
= 1 + ((omap_obj
->width
<< fmt
) / PAGE_SIZE
);
165 /* if stride > than PAGE_SIZE then sparse mapping: */
166 for (i
= n
; i
> 0; i
--) {
167 unmap_mapping_range(obj
->dev
->dev_mapping
,
169 off
+= PAGE_SIZE
* m
;
172 unmap_mapping_range(obj
->dev
->dev_mapping
, off
, size
, 1);
179 /* Evict a buffer from usergart, if it is mapped there */
180 static void evict(struct drm_gem_object
*obj
)
182 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
184 if (omap_obj
->flags
& OMAP_BO_TILED
) {
185 enum tiler_fmt fmt
= gem2fmt(omap_obj
->flags
);
191 for (i
= 0; i
< NUM_USERGART_ENTRIES
; i
++) {
192 struct usergart_entry
*entry
= &usergart
[fmt
].entry
[i
];
193 if (entry
->obj
== obj
)
194 evict_entry(obj
, fmt
, entry
);
199 /* GEM objects can either be allocated from contiguous memory (in which
200 * case obj->filp==NULL), or w/ shmem backing (obj->filp!=NULL). But non
201 * contiguous buffers can be remapped in TILER/DMM if they need to be
202 * contiguous... but we don't do this all the time to reduce pressure
203 * on TILER/DMM space when we know at allocation time that the buffer
204 * will need to be scanned out.
206 static inline bool is_shmem(struct drm_gem_object
*obj
)
208 return obj
->filp
!= NULL
;
212 * shmem buffers that are mapped cached can simulate coherency via using
213 * page faulting to keep track of dirty pages
215 static inline bool is_cached_coherent(struct drm_gem_object
*obj
)
217 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
218 return is_shmem(obj
) &&
219 ((omap_obj
->flags
& OMAP_BO_CACHE_MASK
) == OMAP_BO_CACHED
);
222 static DEFINE_SPINLOCK(sync_lock
);
224 /** ensure backing pages are allocated */
225 static int omap_gem_attach_pages(struct drm_gem_object
*obj
)
227 struct drm_device
*dev
= obj
->dev
;
228 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
230 int npages
= obj
->size
>> PAGE_SHIFT
;
234 WARN_ON(omap_obj
->pages
);
236 /* TODO: __GFP_DMA32 .. but somehow GFP_HIGHMEM is coming from the
237 * mapping_gfp_mask(mapping) which conflicts w/ GFP_DMA32.. probably
238 * we actually want CMA memory for it all anyways..
240 pages
= drm_gem_get_pages(obj
, GFP_KERNEL
);
242 dev_err(obj
->dev
->dev
, "could not get pages: %ld\n", PTR_ERR(pages
));
243 return PTR_ERR(pages
);
246 /* for non-cached buffers, ensure the new pages are clean because
247 * DSS, GPU, etc. are not cache coherent:
249 if (omap_obj
->flags
& (OMAP_BO_WC
|OMAP_BO_UNCACHED
)) {
250 addrs
= kmalloc(npages
* sizeof(*addrs
), GFP_KERNEL
);
256 for (i
= 0; i
< npages
; i
++) {
257 addrs
[i
] = dma_map_page(dev
->dev
, pages
[i
],
258 0, PAGE_SIZE
, DMA_BIDIRECTIONAL
);
261 addrs
= kzalloc(npages
* sizeof(*addrs
), GFP_KERNEL
);
268 omap_obj
->addrs
= addrs
;
269 omap_obj
->pages
= pages
;
274 drm_gem_put_pages(obj
, pages
, true, false);
279 /** release backing pages */
280 static void omap_gem_detach_pages(struct drm_gem_object
*obj
)
282 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
284 /* for non-cached buffers, ensure the new pages are clean because
285 * DSS, GPU, etc. are not cache coherent:
287 if (omap_obj
->flags
& (OMAP_BO_WC
|OMAP_BO_UNCACHED
)) {
288 int i
, npages
= obj
->size
>> PAGE_SHIFT
;
289 for (i
= 0; i
< npages
; i
++) {
290 dma_unmap_page(obj
->dev
->dev
, omap_obj
->addrs
[i
],
291 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
295 kfree(omap_obj
->addrs
);
296 omap_obj
->addrs
= NULL
;
298 drm_gem_put_pages(obj
, omap_obj
->pages
, true, false);
299 omap_obj
->pages
= NULL
;
302 /* get buffer flags */
303 uint32_t omap_gem_flags(struct drm_gem_object
*obj
)
305 return to_omap_bo(obj
)->flags
;
308 /** get mmap offset */
309 static uint64_t mmap_offset(struct drm_gem_object
*obj
)
311 struct drm_device
*dev
= obj
->dev
;
315 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
317 /* Make it mmapable */
318 size
= omap_gem_mmap_size(obj
);
319 ret
= drm_gem_create_mmap_offset_size(obj
, size
);
321 dev_err(dev
->dev
, "could not allocate mmap offset\n");
325 return drm_vma_node_offset_addr(&obj
->vma_node
);
328 uint64_t omap_gem_mmap_offset(struct drm_gem_object
*obj
)
331 mutex_lock(&obj
->dev
->struct_mutex
);
332 offset
= mmap_offset(obj
);
333 mutex_unlock(&obj
->dev
->struct_mutex
);
338 size_t omap_gem_mmap_size(struct drm_gem_object
*obj
)
340 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
341 size_t size
= obj
->size
;
343 if (omap_obj
->flags
& OMAP_BO_TILED
) {
344 /* for tiled buffers, the virtual size has stride rounded up
345 * to 4kb.. (to hide the fact that row n+1 might start 16kb or
346 * 32kb later!). But we don't back the entire buffer with
347 * pages, only the valid picture part.. so need to adjust for
348 * this in the size used to mmap and generate mmap offset
350 size
= tiler_vsize(gem2fmt(omap_obj
->flags
),
351 omap_obj
->width
, omap_obj
->height
);
357 /* get tiled size, returns -EINVAL if not tiled buffer */
358 int omap_gem_tiled_size(struct drm_gem_object
*obj
, uint16_t *w
, uint16_t *h
)
360 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
361 if (omap_obj
->flags
& OMAP_BO_TILED
) {
362 *w
= omap_obj
->width
;
363 *h
= omap_obj
->height
;
369 /* Normal handling for the case of faulting in non-tiled buffers */
370 static int fault_1d(struct drm_gem_object
*obj
,
371 struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
373 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
377 /* We don't use vmf->pgoff since that has the fake offset: */
378 pgoff
= ((unsigned long)vmf
->virtual_address
-
379 vma
->vm_start
) >> PAGE_SHIFT
;
381 if (omap_obj
->pages
) {
382 omap_gem_cpu_sync(obj
, pgoff
);
383 pfn
= page_to_pfn(omap_obj
->pages
[pgoff
]);
385 BUG_ON(!(omap_obj
->flags
& OMAP_BO_DMA
));
386 pfn
= (omap_obj
->paddr
>> PAGE_SHIFT
) + pgoff
;
389 VERB("Inserting %p pfn %lx, pa %lx", vmf
->virtual_address
,
390 pfn
, pfn
<< PAGE_SHIFT
);
392 return vm_insert_mixed(vma
, (unsigned long)vmf
->virtual_address
, pfn
);
395 /* Special handling for the case of faulting in 2d tiled buffers */
396 static int fault_2d(struct drm_gem_object
*obj
,
397 struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
399 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
400 struct usergart_entry
*entry
;
401 enum tiler_fmt fmt
= gem2fmt(omap_obj
->flags
);
402 struct page
*pages
[64]; /* XXX is this too much to have on stack? */
404 pgoff_t pgoff
, base_pgoff
;
409 * Note the height of the slot is also equal to the number of pages
410 * that need to be mapped in to fill 4kb wide CPU page. If the slot
411 * height is 64, then 64 pages fill a 4kb wide by 64 row region.
413 const int n
= usergart
[fmt
].height
;
414 const int n_shift
= usergart
[fmt
].height_shift
;
417 * If buffer width in bytes > PAGE_SIZE then the virtual stride is
418 * rounded up to next multiple of PAGE_SIZE.. this need to be taken
419 * into account in some of the math, so figure out virtual stride
422 const int m
= 1 + ((omap_obj
->width
<< fmt
) / PAGE_SIZE
);
424 /* We don't use vmf->pgoff since that has the fake offset: */
425 pgoff
= ((unsigned long)vmf
->virtual_address
-
426 vma
->vm_start
) >> PAGE_SHIFT
;
429 * Actual address we start mapping at is rounded down to previous slot
430 * boundary in the y direction:
432 base_pgoff
= round_down(pgoff
, m
<< n_shift
);
434 /* figure out buffer width in slots */
435 slots
= omap_obj
->width
>> usergart
[fmt
].slot_shift
;
437 vaddr
= vmf
->virtual_address
- ((pgoff
- base_pgoff
) << PAGE_SHIFT
);
439 entry
= &usergart
[fmt
].entry
[usergart
[fmt
].last
];
441 /* evict previous buffer using this usergart entry, if any: */
443 evict_entry(entry
->obj
, fmt
, entry
);
446 entry
->obj_pgoff
= base_pgoff
;
448 /* now convert base_pgoff to phys offset from virt offset: */
449 base_pgoff
= (base_pgoff
>> n_shift
) * slots
;
451 /* for wider-than 4k.. figure out which part of the slot-row we want: */
454 entry
->obj_pgoff
+= off
;
456 slots
= min(slots
- (off
<< n_shift
), n
);
457 base_pgoff
+= off
<< n_shift
;
458 vaddr
+= off
<< PAGE_SHIFT
;
462 * Map in pages. Beyond the valid pixel part of the buffer, we set
463 * pages[i] to NULL to get a dummy page mapped in.. if someone
464 * reads/writes it they will get random/undefined content, but at
465 * least it won't be corrupting whatever other random page used to
466 * be mapped in, or other undefined behavior.
468 memcpy(pages
, &omap_obj
->pages
[base_pgoff
],
469 sizeof(struct page
*) * slots
);
470 memset(pages
+ slots
, 0,
471 sizeof(struct page
*) * (n
- slots
));
473 ret
= tiler_pin(entry
->block
, pages
, ARRAY_SIZE(pages
), 0, true);
475 dev_err(obj
->dev
->dev
, "failed to pin: %d\n", ret
);
479 pfn
= entry
->paddr
>> PAGE_SHIFT
;
481 VERB("Inserting %p pfn %lx, pa %lx", vmf
->virtual_address
,
482 pfn
, pfn
<< PAGE_SHIFT
);
484 for (i
= n
; i
> 0; i
--) {
485 vm_insert_mixed(vma
, (unsigned long)vaddr
, pfn
);
486 pfn
+= usergart
[fmt
].stride_pfn
;
487 vaddr
+= PAGE_SIZE
* m
;
490 /* simple round-robin: */
491 usergart
[fmt
].last
= (usergart
[fmt
].last
+ 1) % NUM_USERGART_ENTRIES
;
497 * omap_gem_fault - pagefault handler for GEM objects
498 * @vma: the VMA of the GEM object
501 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
502 * does most of the work for us including the actual map/unmap calls
503 * but we need to do the actual page work.
505 * The VMA was set up by GEM. In doing so it also ensured that the
506 * vma->vm_private_data points to the GEM object that is backing this
509 int omap_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
511 struct drm_gem_object
*obj
= vma
->vm_private_data
;
512 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
513 struct drm_device
*dev
= obj
->dev
;
517 /* Make sure we don't parallel update on a fault, nor move or remove
518 * something from beneath our feet
520 mutex_lock(&dev
->struct_mutex
);
522 /* if a shmem backed object, make sure we have pages attached now */
523 ret
= get_pages(obj
, &pages
);
527 /* where should we do corresponding put_pages().. we are mapping
528 * the original page, rather than thru a GART, so we can't rely
529 * on eviction to trigger this. But munmap() or all mappings should
530 * probably trigger put_pages()?
533 if (omap_obj
->flags
& OMAP_BO_TILED
)
534 ret
= fault_2d(obj
, vma
, vmf
);
536 ret
= fault_1d(obj
, vma
, vmf
);
540 mutex_unlock(&dev
->struct_mutex
);
545 return VM_FAULT_NOPAGE
;
549 return VM_FAULT_SIGBUS
;
553 /** We override mainly to fix up some of the vm mapping flags.. */
554 int omap_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
558 ret
= drm_gem_mmap(filp
, vma
);
560 DBG("mmap failed: %d", ret
);
564 return omap_gem_mmap_obj(vma
->vm_private_data
, vma
);
567 int omap_gem_mmap_obj(struct drm_gem_object
*obj
,
568 struct vm_area_struct
*vma
)
570 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
572 vma
->vm_flags
&= ~VM_PFNMAP
;
573 vma
->vm_flags
|= VM_MIXEDMAP
;
575 if (omap_obj
->flags
& OMAP_BO_WC
) {
576 vma
->vm_page_prot
= pgprot_writecombine(vm_get_page_prot(vma
->vm_flags
));
577 } else if (omap_obj
->flags
& OMAP_BO_UNCACHED
) {
578 vma
->vm_page_prot
= pgprot_noncached(vm_get_page_prot(vma
->vm_flags
));
581 * We do have some private objects, at least for scanout buffers
582 * on hardware without DMM/TILER. But these are allocated write-
585 if (WARN_ON(!obj
->filp
))
589 * Shunt off cached objs to shmem file so they have their own
590 * address_space (so unmap_mapping_range does what we want,
591 * in particular in the case of mmap'd dmabufs)
595 vma
->vm_file
= get_file(obj
->filp
);
597 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
605 * omap_gem_dumb_create - create a dumb buffer
606 * @drm_file: our client file
608 * @args: the requested arguments copied from userspace
610 * Allocate a buffer suitable for use for a frame buffer of the
611 * form described by user space. Give userspace a handle by which
614 int omap_gem_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
615 struct drm_mode_create_dumb
*args
)
617 union omap_gem_size gsize
;
619 /* in case someone tries to feed us a completely bogus stride: */
620 args
->pitch
= align_pitch(args
->pitch
, args
->width
, args
->bpp
);
621 args
->size
= PAGE_ALIGN(args
->pitch
* args
->height
);
623 gsize
= (union omap_gem_size
){
627 return omap_gem_new_handle(dev
, file
, gsize
,
628 OMAP_BO_SCANOUT
| OMAP_BO_WC
, &args
->handle
);
632 * omap_gem_dumb_map - buffer mapping for dumb interface
633 * @file: our drm client file
635 * @handle: GEM handle to the object (from dumb_create)
637 * Do the necessary setup to allow the mapping of the frame buffer
638 * into user memory. We don't have to do much here at the moment.
640 int omap_gem_dumb_map_offset(struct drm_file
*file
, struct drm_device
*dev
,
641 uint32_t handle
, uint64_t *offset
)
643 struct drm_gem_object
*obj
;
646 /* GEM does all our handle to object mapping */
647 obj
= drm_gem_object_lookup(dev
, file
, handle
);
653 *offset
= omap_gem_mmap_offset(obj
);
655 drm_gem_object_unreference_unlocked(obj
);
661 /* Set scrolling position. This allows us to implement fast scrolling
664 * Call only from non-atomic contexts.
666 int omap_gem_roll(struct drm_gem_object
*obj
, uint32_t roll
)
668 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
669 uint32_t npages
= obj
->size
>> PAGE_SHIFT
;
673 dev_err(obj
->dev
->dev
, "invalid roll: %d\n", roll
);
677 omap_obj
->roll
= roll
;
679 mutex_lock(&obj
->dev
->struct_mutex
);
681 /* if we aren't mapped yet, we don't need to do anything */
682 if (omap_obj
->block
) {
684 ret
= get_pages(obj
, &pages
);
687 ret
= tiler_pin(omap_obj
->block
, pages
, npages
, roll
, true);
689 dev_err(obj
->dev
->dev
, "could not repin: %d\n", ret
);
693 mutex_unlock(&obj
->dev
->struct_mutex
);
698 /* Sync the buffer for CPU access.. note pages should already be
699 * attached, ie. omap_gem_get_pages()
701 void omap_gem_cpu_sync(struct drm_gem_object
*obj
, int pgoff
)
703 struct drm_device
*dev
= obj
->dev
;
704 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
706 if (is_cached_coherent(obj
) && omap_obj
->addrs
[pgoff
]) {
707 dma_unmap_page(dev
->dev
, omap_obj
->addrs
[pgoff
],
708 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
709 omap_obj
->addrs
[pgoff
] = 0;
713 /* sync the buffer for DMA access */
714 void omap_gem_dma_sync(struct drm_gem_object
*obj
,
715 enum dma_data_direction dir
)
717 struct drm_device
*dev
= obj
->dev
;
718 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
720 if (is_cached_coherent(obj
)) {
721 int i
, npages
= obj
->size
>> PAGE_SHIFT
;
722 struct page
**pages
= omap_obj
->pages
;
725 for (i
= 0; i
< npages
; i
++) {
726 if (!omap_obj
->addrs
[i
]) {
727 omap_obj
->addrs
[i
] = dma_map_page(dev
->dev
, pages
[i
], 0,
728 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
734 unmap_mapping_range(obj
->filp
->f_mapping
, 0,
735 omap_gem_mmap_size(obj
), 1);
740 /* Get physical address for DMA.. if 'remap' is true, and the buffer is not
741 * already contiguous, remap it to pin in physically contiguous memory.. (ie.
744 int omap_gem_get_paddr(struct drm_gem_object
*obj
,
745 dma_addr_t
*paddr
, bool remap
)
747 struct omap_drm_private
*priv
= obj
->dev
->dev_private
;
748 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
751 mutex_lock(&obj
->dev
->struct_mutex
);
753 if (remap
&& is_shmem(obj
) && priv
->has_dmm
) {
754 if (omap_obj
->paddr_cnt
== 0) {
756 uint32_t npages
= obj
->size
>> PAGE_SHIFT
;
757 enum tiler_fmt fmt
= gem2fmt(omap_obj
->flags
);
758 struct tiler_block
*block
;
760 BUG_ON(omap_obj
->block
);
762 ret
= get_pages(obj
, &pages
);
766 if (omap_obj
->flags
& OMAP_BO_TILED
) {
767 block
= tiler_reserve_2d(fmt
,
769 omap_obj
->height
, 0);
771 block
= tiler_reserve_1d(obj
->size
);
775 ret
= PTR_ERR(block
);
776 dev_err(obj
->dev
->dev
,
777 "could not remap: %d (%d)\n", ret
, fmt
);
781 /* TODO: enable async refill.. */
782 ret
= tiler_pin(block
, pages
, npages
,
783 omap_obj
->roll
, true);
785 tiler_release(block
);
786 dev_err(obj
->dev
->dev
,
787 "could not pin: %d\n", ret
);
791 omap_obj
->paddr
= tiler_ssptr(block
);
792 omap_obj
->block
= block
;
794 DBG("got paddr: %08x", omap_obj
->paddr
);
797 omap_obj
->paddr_cnt
++;
799 *paddr
= omap_obj
->paddr
;
800 } else if (omap_obj
->flags
& OMAP_BO_DMA
) {
801 *paddr
= omap_obj
->paddr
;
808 mutex_unlock(&obj
->dev
->struct_mutex
);
813 /* Release physical address, when DMA is no longer being performed.. this
814 * could potentially unpin and unmap buffers from TILER
816 int omap_gem_put_paddr(struct drm_gem_object
*obj
)
818 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
821 mutex_lock(&obj
->dev
->struct_mutex
);
822 if (omap_obj
->paddr_cnt
> 0) {
823 omap_obj
->paddr_cnt
--;
824 if (omap_obj
->paddr_cnt
== 0) {
825 ret
= tiler_unpin(omap_obj
->block
);
827 dev_err(obj
->dev
->dev
,
828 "could not unpin pages: %d\n", ret
);
831 ret
= tiler_release(omap_obj
->block
);
833 dev_err(obj
->dev
->dev
,
834 "could not release unmap: %d\n", ret
);
836 omap_obj
->block
= NULL
;
840 mutex_unlock(&obj
->dev
->struct_mutex
);
844 /* Get rotated scanout address (only valid if already pinned), at the
845 * specified orientation and x,y offset from top-left corner of buffer
846 * (only valid for tiled 2d buffers)
848 int omap_gem_rotated_paddr(struct drm_gem_object
*obj
, uint32_t orient
,
849 int x
, int y
, dma_addr_t
*paddr
)
851 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
854 mutex_lock(&obj
->dev
->struct_mutex
);
855 if ((omap_obj
->paddr_cnt
> 0) && omap_obj
->block
&&
856 (omap_obj
->flags
& OMAP_BO_TILED
)) {
857 *paddr
= tiler_tsptr(omap_obj
->block
, orient
, x
, y
);
860 mutex_unlock(&obj
->dev
->struct_mutex
);
864 /* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
865 int omap_gem_tiled_stride(struct drm_gem_object
*obj
, uint32_t orient
)
867 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
869 if (omap_obj
->flags
& OMAP_BO_TILED
)
870 ret
= tiler_stride(gem2fmt(omap_obj
->flags
), orient
);
874 /* acquire pages when needed (for example, for DMA where physically
875 * contiguous buffer is not required
877 static int get_pages(struct drm_gem_object
*obj
, struct page
***pages
)
879 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
882 if (is_shmem(obj
) && !omap_obj
->pages
) {
883 ret
= omap_gem_attach_pages(obj
);
885 dev_err(obj
->dev
->dev
, "could not attach pages\n");
890 /* TODO: even phys-contig.. we should have a list of pages? */
891 *pages
= omap_obj
->pages
;
896 /* if !remap, and we don't have pages backing, then fail, rather than
897 * increasing the pin count (which we don't really do yet anyways,
898 * because we don't support swapping pages back out). And 'remap'
899 * might not be quite the right name, but I wanted to keep it working
900 * similarly to omap_gem_get_paddr(). Note though that mutex is not
901 * aquired if !remap (because this can be called in atomic ctxt),
902 * but probably omap_gem_get_paddr() should be changed to work in the
903 * same way. If !remap, a matching omap_gem_put_pages() call is not
904 * required (and should not be made).
906 int omap_gem_get_pages(struct drm_gem_object
*obj
, struct page
***pages
,
911 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
912 if (!omap_obj
->pages
)
914 *pages
= omap_obj
->pages
;
917 mutex_lock(&obj
->dev
->struct_mutex
);
918 ret
= get_pages(obj
, pages
);
919 mutex_unlock(&obj
->dev
->struct_mutex
);
923 /* release pages when DMA no longer being performed */
924 int omap_gem_put_pages(struct drm_gem_object
*obj
)
926 /* do something here if we dynamically attach/detach pages.. at
927 * least they would no longer need to be pinned if everyone has
928 * released the pages..
933 /* Get kernel virtual address for CPU access.. this more or less only
934 * exists for omap_fbdev. This should be called with struct_mutex
937 void *omap_gem_vaddr(struct drm_gem_object
*obj
)
939 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
940 WARN_ON(!mutex_is_locked(&obj
->dev
->struct_mutex
));
941 if (!omap_obj
->vaddr
) {
943 int ret
= get_pages(obj
, &pages
);
946 omap_obj
->vaddr
= vmap(pages
, obj
->size
>> PAGE_SHIFT
,
947 VM_MAP
, pgprot_writecombine(PAGE_KERNEL
));
949 return omap_obj
->vaddr
;
953 /* re-pin objects in DMM in resume path: */
954 int omap_gem_resume(struct device
*dev
)
956 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
957 struct omap_drm_private
*priv
= drm_dev
->dev_private
;
958 struct omap_gem_object
*omap_obj
;
961 list_for_each_entry(omap_obj
, &priv
->obj_list
, mm_list
) {
962 if (omap_obj
->block
) {
963 struct drm_gem_object
*obj
= &omap_obj
->base
;
964 uint32_t npages
= obj
->size
>> PAGE_SHIFT
;
965 WARN_ON(!omap_obj
->pages
); /* this can't happen */
966 ret
= tiler_pin(omap_obj
->block
,
967 omap_obj
->pages
, npages
,
968 omap_obj
->roll
, true);
970 dev_err(dev
, "could not repin: %d\n", ret
);
980 #ifdef CONFIG_DEBUG_FS
981 void omap_gem_describe(struct drm_gem_object
*obj
, struct seq_file
*m
)
983 struct drm_device
*dev
= obj
->dev
;
984 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
987 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
989 off
= drm_vma_node_start(&obj
->vma_node
);
991 seq_printf(m
, "%08x: %2d (%2d) %08llx %08Zx (%2d) %p %4d",
992 omap_obj
->flags
, obj
->name
, obj
->refcount
.refcount
.counter
,
993 off
, omap_obj
->paddr
, omap_obj
->paddr_cnt
,
994 omap_obj
->vaddr
, omap_obj
->roll
);
996 if (omap_obj
->flags
& OMAP_BO_TILED
) {
997 seq_printf(m
, " %dx%d", omap_obj
->width
, omap_obj
->height
);
998 if (omap_obj
->block
) {
999 struct tcm_area
*area
= &omap_obj
->block
->area
;
1000 seq_printf(m
, " (%dx%d, %dx%d)",
1001 area
->p0
.x
, area
->p0
.y
,
1002 area
->p1
.x
, area
->p1
.y
);
1005 seq_printf(m
, " %d", obj
->size
);
1008 seq_printf(m
, "\n");
1011 void omap_gem_describe_objects(struct list_head
*list
, struct seq_file
*m
)
1013 struct omap_gem_object
*omap_obj
;
1017 list_for_each_entry(omap_obj
, list
, mm_list
) {
1018 struct drm_gem_object
*obj
= &omap_obj
->base
;
1020 omap_gem_describe(obj
, m
);
1025 seq_printf(m
, "Total %d objects, %zu bytes\n", count
, size
);
1029 /* Buffer Synchronization:
1032 struct omap_gem_sync_waiter
{
1033 struct list_head list
;
1034 struct omap_gem_object
*omap_obj
;
1035 enum omap_gem_op op
;
1036 uint32_t read_target
, write_target
;
1037 /* notify called w/ sync_lock held */
1038 void (*notify
)(void *arg
);
1042 /* list of omap_gem_sync_waiter.. the notify fxn gets called back when
1043 * the read and/or write target count is achieved which can call a user
1044 * callback (ex. to kick 3d and/or 2d), wakeup blocked task (prep for
1047 static LIST_HEAD(waiters
);
1049 static inline bool is_waiting(struct omap_gem_sync_waiter
*waiter
)
1051 struct omap_gem_object
*omap_obj
= waiter
->omap_obj
;
1052 if ((waiter
->op
& OMAP_GEM_READ
) &&
1053 (omap_obj
->sync
->read_complete
< waiter
->read_target
))
1055 if ((waiter
->op
& OMAP_GEM_WRITE
) &&
1056 (omap_obj
->sync
->write_complete
< waiter
->write_target
))
1061 /* macro for sync debug.. */
1063 #define SYNC(fmt, ...) do { if (SYNCDBG) \
1064 printk(KERN_ERR "%s:%d: "fmt"\n", \
1065 __func__, __LINE__, ##__VA_ARGS__); \
1069 static void sync_op_update(void)
1071 struct omap_gem_sync_waiter
*waiter
, *n
;
1072 list_for_each_entry_safe(waiter
, n
, &waiters
, list
) {
1073 if (!is_waiting(waiter
)) {
1074 list_del(&waiter
->list
);
1075 SYNC("notify: %p", waiter
);
1076 waiter
->notify(waiter
->arg
);
1082 static inline int sync_op(struct drm_gem_object
*obj
,
1083 enum omap_gem_op op
, bool start
)
1085 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
1088 spin_lock(&sync_lock
);
1090 if (!omap_obj
->sync
) {
1091 omap_obj
->sync
= kzalloc(sizeof(*omap_obj
->sync
), GFP_ATOMIC
);
1092 if (!omap_obj
->sync
) {
1099 if (op
& OMAP_GEM_READ
)
1100 omap_obj
->sync
->read_pending
++;
1101 if (op
& OMAP_GEM_WRITE
)
1102 omap_obj
->sync
->write_pending
++;
1104 if (op
& OMAP_GEM_READ
)
1105 omap_obj
->sync
->read_complete
++;
1106 if (op
& OMAP_GEM_WRITE
)
1107 omap_obj
->sync
->write_complete
++;
1112 spin_unlock(&sync_lock
);
1117 /* it is a bit lame to handle updates in this sort of polling way, but
1118 * in case of PVR, the GPU can directly update read/write complete
1119 * values, and not really tell us which ones it updated.. this also
1120 * means that sync_lock is not quite sufficient. So we'll need to
1121 * do something a bit better when it comes time to add support for
1124 void omap_gem_op_update(void)
1126 spin_lock(&sync_lock
);
1128 spin_unlock(&sync_lock
);
1131 /* mark the start of read and/or write operation */
1132 int omap_gem_op_start(struct drm_gem_object
*obj
, enum omap_gem_op op
)
1134 return sync_op(obj
, op
, true);
1137 int omap_gem_op_finish(struct drm_gem_object
*obj
, enum omap_gem_op op
)
1139 return sync_op(obj
, op
, false);
1142 static DECLARE_WAIT_QUEUE_HEAD(sync_event
);
1144 static void sync_notify(void *arg
)
1146 struct task_struct
**waiter_task
= arg
;
1147 *waiter_task
= NULL
;
1148 wake_up_all(&sync_event
);
1151 int omap_gem_op_sync(struct drm_gem_object
*obj
, enum omap_gem_op op
)
1153 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
1155 if (omap_obj
->sync
) {
1156 struct task_struct
*waiter_task
= current
;
1157 struct omap_gem_sync_waiter
*waiter
=
1158 kzalloc(sizeof(*waiter
), GFP_KERNEL
);
1163 waiter
->omap_obj
= omap_obj
;
1165 waiter
->read_target
= omap_obj
->sync
->read_pending
;
1166 waiter
->write_target
= omap_obj
->sync
->write_pending
;
1167 waiter
->notify
= sync_notify
;
1168 waiter
->arg
= &waiter_task
;
1170 spin_lock(&sync_lock
);
1171 if (is_waiting(waiter
)) {
1172 SYNC("waited: %p", waiter
);
1173 list_add_tail(&waiter
->list
, &waiters
);
1174 spin_unlock(&sync_lock
);
1175 ret
= wait_event_interruptible(sync_event
,
1176 (waiter_task
== NULL
));
1177 spin_lock(&sync_lock
);
1179 SYNC("interrupted: %p", waiter
);
1180 /* we were interrupted */
1181 list_del(&waiter
->list
);
1184 /* freed in sync_op_update() */
1188 spin_unlock(&sync_lock
);
1196 /* call fxn(arg), either synchronously or asynchronously if the op
1197 * is currently blocked.. fxn() can be called from any context
1199 * (TODO for now fxn is called back from whichever context calls
1200 * omap_gem_op_update().. but this could be better defined later
1203 * TODO more code in common w/ _sync()..
1205 int omap_gem_op_async(struct drm_gem_object
*obj
, enum omap_gem_op op
,
1206 void (*fxn
)(void *arg
), void *arg
)
1208 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
1209 if (omap_obj
->sync
) {
1210 struct omap_gem_sync_waiter
*waiter
=
1211 kzalloc(sizeof(*waiter
), GFP_ATOMIC
);
1216 waiter
->omap_obj
= omap_obj
;
1218 waiter
->read_target
= omap_obj
->sync
->read_pending
;
1219 waiter
->write_target
= omap_obj
->sync
->write_pending
;
1220 waiter
->notify
= fxn
;
1223 spin_lock(&sync_lock
);
1224 if (is_waiting(waiter
)) {
1225 SYNC("waited: %p", waiter
);
1226 list_add_tail(&waiter
->list
, &waiters
);
1227 spin_unlock(&sync_lock
);
1231 spin_unlock(&sync_lock
);
1240 /* special API so PVR can update the buffer to use a sync-object allocated
1241 * from it's sync-obj heap. Only used for a newly allocated (from PVR's
1242 * perspective) sync-object, so we overwrite the new syncobj w/ values
1243 * from the already allocated syncobj (if there is one)
1245 int omap_gem_set_sync_object(struct drm_gem_object
*obj
, void *syncobj
)
1247 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
1250 spin_lock(&sync_lock
);
1252 if ((omap_obj
->flags
& OMAP_BO_EXT_SYNC
) && !syncobj
) {
1253 /* clearing a previously set syncobj */
1254 syncobj
= kmemdup(omap_obj
->sync
, sizeof(*omap_obj
->sync
),
1260 omap_obj
->flags
&= ~OMAP_BO_EXT_SYNC
;
1261 omap_obj
->sync
= syncobj
;
1262 } else if (syncobj
&& !(omap_obj
->flags
& OMAP_BO_EXT_SYNC
)) {
1263 /* replacing an existing syncobj */
1264 if (omap_obj
->sync
) {
1265 memcpy(syncobj
, omap_obj
->sync
, sizeof(*omap_obj
->sync
));
1266 kfree(omap_obj
->sync
);
1268 omap_obj
->flags
|= OMAP_BO_EXT_SYNC
;
1269 omap_obj
->sync
= syncobj
;
1273 spin_unlock(&sync_lock
);
1277 /* don't call directly.. called from GEM core when it is time to actually
1280 void omap_gem_free_object(struct drm_gem_object
*obj
)
1282 struct drm_device
*dev
= obj
->dev
;
1283 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
1287 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
1289 list_del(&omap_obj
->mm_list
);
1291 drm_gem_free_mmap_offset(obj
);
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 /* don't free externally allocated backing memory */
1299 if (!(omap_obj
->flags
& OMAP_BO_EXT_MEM
)) {
1300 if (omap_obj
->pages
)
1301 omap_gem_detach_pages(obj
);
1303 if (!is_shmem(obj
)) {
1304 dma_free_writecombine(dev
->dev
, obj
->size
,
1305 omap_obj
->vaddr
, omap_obj
->paddr
);
1306 } else if (omap_obj
->vaddr
) {
1307 vunmap(omap_obj
->vaddr
);
1311 /* don't free externally allocated syncobj */
1312 if (!(omap_obj
->flags
& OMAP_BO_EXT_SYNC
))
1313 kfree(omap_obj
->sync
);
1315 drm_gem_object_release(obj
);
1320 /* convenience method to construct a GEM buffer object, and userspace handle */
1321 int omap_gem_new_handle(struct drm_device
*dev
, struct drm_file
*file
,
1322 union omap_gem_size gsize
, uint32_t flags
, uint32_t *handle
)
1324 struct drm_gem_object
*obj
;
1327 obj
= omap_gem_new(dev
, gsize
, flags
);
1331 ret
= drm_gem_handle_create(file
, obj
, handle
);
1333 drm_gem_object_release(obj
);
1334 kfree(obj
); /* TODO isn't there a dtor to call? just copying i915 */
1338 /* drop reference from allocate - handle holds it now */
1339 drm_gem_object_unreference_unlocked(obj
);
1344 /* GEM buffer object constructor */
1345 struct drm_gem_object
*omap_gem_new(struct drm_device
*dev
,
1346 union omap_gem_size gsize
, uint32_t flags
)
1348 struct omap_drm_private
*priv
= dev
->dev_private
;
1349 struct omap_gem_object
*omap_obj
;
1350 struct drm_gem_object
*obj
= NULL
;
1354 if (flags
& OMAP_BO_TILED
) {
1356 dev_err(dev
->dev
, "Tiled buffers require DMM\n");
1360 /* tiled buffers are always shmem paged backed.. when they are
1361 * scanned out, they are remapped into DMM/TILER
1363 flags
&= ~OMAP_BO_SCANOUT
;
1365 /* currently don't allow cached buffers.. there is some caching
1366 * stuff that needs to be handled better
1368 flags
&= ~(OMAP_BO_CACHED
|OMAP_BO_UNCACHED
);
1369 flags
|= OMAP_BO_WC
;
1371 /* align dimensions to slot boundaries... */
1372 tiler_align(gem2fmt(flags
),
1373 &gsize
.tiled
.width
, &gsize
.tiled
.height
);
1375 /* ...and calculate size based on aligned dimensions */
1376 size
= tiler_size(gem2fmt(flags
),
1377 gsize
.tiled
.width
, gsize
.tiled
.height
);
1379 size
= PAGE_ALIGN(gsize
.bytes
);
1382 omap_obj
= kzalloc(sizeof(*omap_obj
), GFP_KERNEL
);
1386 list_add(&omap_obj
->mm_list
, &priv
->obj_list
);
1388 obj
= &omap_obj
->base
;
1390 if ((flags
& OMAP_BO_SCANOUT
) && !priv
->has_dmm
) {
1391 /* attempt to allocate contiguous memory if we don't
1392 * have DMM for remappign discontiguous buffers
1394 omap_obj
->vaddr
= dma_alloc_writecombine(dev
->dev
, size
,
1395 &omap_obj
->paddr
, GFP_KERNEL
);
1396 if (omap_obj
->vaddr
)
1397 flags
|= OMAP_BO_DMA
;
1401 omap_obj
->flags
= flags
;
1403 if (flags
& OMAP_BO_TILED
) {
1404 omap_obj
->width
= gsize
.tiled
.width
;
1405 omap_obj
->height
= gsize
.tiled
.height
;
1409 if (flags
& (OMAP_BO_DMA
|OMAP_BO_EXT_MEM
))
1410 drm_gem_private_object_init(dev
, obj
, size
);
1412 ret
= drm_gem_object_init(dev
, obj
, size
);
1421 omap_gem_free_object(obj
);
1426 /* init/cleanup.. if DMM is used, we need to set some stuff up.. */
1427 void omap_gem_init(struct drm_device
*dev
)
1429 struct omap_drm_private
*priv
= dev
->dev_private
;
1430 const enum tiler_fmt fmts
[] = {
1431 TILFMT_8BIT
, TILFMT_16BIT
, TILFMT_32BIT
1435 if (!dmm_is_available()) {
1436 /* DMM only supported on OMAP4 and later, so this isn't fatal */
1437 dev_warn(dev
->dev
, "DMM not available, disable DMM support\n");
1441 usergart
= kcalloc(3, sizeof(*usergart
), GFP_KERNEL
);
1445 /* reserve 4k aligned/wide regions for userspace mappings: */
1446 for (i
= 0; i
< ARRAY_SIZE(fmts
); i
++) {
1447 uint16_t h
= 1, w
= PAGE_SIZE
>> i
;
1448 tiler_align(fmts
[i
], &w
, &h
);
1449 /* note: since each region is 1 4kb page wide, and minimum
1450 * number of rows, the height ends up being the same as the
1451 * # of pages in the region
1453 usergart
[i
].height
= h
;
1454 usergart
[i
].height_shift
= ilog2(h
);
1455 usergart
[i
].stride_pfn
= tiler_stride(fmts
[i
], 0) >> PAGE_SHIFT
;
1456 usergart
[i
].slot_shift
= ilog2((PAGE_SIZE
/ h
) >> i
);
1457 for (j
= 0; j
< NUM_USERGART_ENTRIES
; j
++) {
1458 struct usergart_entry
*entry
= &usergart
[i
].entry
[j
];
1459 struct tiler_block
*block
=
1460 tiler_reserve_2d(fmts
[i
], w
, h
,
1462 if (IS_ERR(block
)) {
1464 "reserve failed: %d, %d, %ld\n",
1465 i
, j
, PTR_ERR(block
));
1468 entry
->paddr
= tiler_ssptr(block
);
1469 entry
->block
= block
;
1471 DBG("%d:%d: %dx%d: paddr=%08x stride=%d", i
, j
, w
, h
,
1473 usergart
[i
].stride_pfn
<< PAGE_SHIFT
);
1477 priv
->has_dmm
= true;
1480 void omap_gem_deinit(struct drm_device
*dev
)
1482 /* I believe we can rely on there being no more outstanding GEM
1483 * objects which could depend on usergart/dmm at this point.