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
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/seq_file.h>
19 #include <linux/shmem_fs.h>
20 #include <linux/spinlock.h>
21 #include <linux/pfn_t.h>
23 #include <drm/drm_vma_manager.h>
26 #include "omap_dmm_tiler.h"
29 * GEM buffer object implementation.
32 /* note: we use upper 8 bits of flags for driver-internal flags: */
33 #define OMAP_BO_MEM_DMA_API 0x01000000 /* memory allocated with the dma_alloc_* API */
34 #define OMAP_BO_MEM_SHMEM 0x02000000 /* memory allocated through shmem backing */
35 #define OMAP_BO_MEM_DMABUF 0x08000000 /* memory imported from a dmabuf */
37 struct omap_gem_object
{
38 struct drm_gem_object base
;
40 struct list_head mm_list
;
44 /** width/height for tiled formats (rounded up to slot boundaries) */
45 uint16_t width
, height
;
47 /** roll applied when mapping to DMM */
51 * dma_addr contains the buffer DMA address. It is valid for
53 * - buffers allocated through the DMA mapping API (with the
54 * OMAP_BO_MEM_DMA_API flag set)
56 * - buffers imported from dmabuf (with the OMAP_BO_MEM_DMABUF flag set)
57 * if they are physically contiguous (when sgt->orig_nents == 1)
59 * - buffers mapped through the TILER when dma_addr_cnt is not zero, in
60 * which case the DMA address points to the TILER aperture
62 * Physically contiguous buffers have their DMA address equal to the
63 * physical address as we don't remap those buffers through the TILER.
65 * Buffers mapped to the TILER have their DMA address pointing to the
66 * TILER aperture. As TILER mappings are refcounted (through
67 * dma_addr_cnt) the DMA address must be accessed through omap_gem_pin()
68 * to ensure that the mapping won't disappear unexpectedly. References
69 * must be released with omap_gem_unpin().
74 * # of users of dma_addr
76 uint32_t dma_addr_cnt
;
79 * If the buffer has been imported from a dmabuf the OMAP_DB_DMABUF flag
80 * is set and the sgt field is valid.
85 * tiler block used when buffer is remapped in DMM/TILER.
87 struct tiler_block
*block
;
90 * Array of backing pages, if allocated. Note that pages are never
91 * allocated for buffers originally allocated from contiguous memory
95 /** addresses corresponding to pages in above array */
96 dma_addr_t
*dma_addrs
;
99 * Virtual address, if mapped.
104 #define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
106 /* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
107 * not necessarily pinned in TILER all the time, and (b) when they are
108 * they are not necessarily page aligned, we reserve one or more small
109 * regions in each of the 2d containers to use as a user-GART where we
110 * can create a second page-aligned mapping of parts of the buffer
111 * being accessed from userspace.
113 * Note that we could optimize slightly when we know that multiple
114 * tiler containers are backed by the same PAT.. but I'll leave that
117 #define NUM_USERGART_ENTRIES 2
118 struct omap_drm_usergart_entry
{
119 struct tiler_block
*block
; /* the reserved tiler block */
121 struct drm_gem_object
*obj
; /* the current pinned obj */
122 pgoff_t obj_pgoff
; /* page offset of obj currently
126 struct omap_drm_usergart
{
127 struct omap_drm_usergart_entry entry
[NUM_USERGART_ENTRIES
];
128 int height
; /* height in rows */
129 int height_shift
; /* ilog2(height in rows) */
130 int slot_shift
; /* ilog2(width per slot) */
131 int stride_pfn
; /* stride in pages */
132 int last
; /* index of last used entry */
135 /* -----------------------------------------------------------------------------
139 /** get mmap offset */
140 static uint64_t mmap_offset(struct drm_gem_object
*obj
)
142 struct drm_device
*dev
= obj
->dev
;
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
);
152 dev_err(dev
->dev
, "could not allocate mmap offset\n");
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
)
164 if ((omap_obj
->flags
& OMAP_BO_MEM_DMABUF
) && omap_obj
->sgt
->nents
== 1)
170 /* -----------------------------------------------------------------------------
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
);
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
,
191 off
+= PAGE_SIZE
* m
;
194 unmap_mapping_range(obj
->dev
->anon_inode
->i_mapping
,
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
);
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 /* -----------------------------------------------------------------------------
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
);
231 int npages
= obj
->size
>> PAGE_SHIFT
;
235 WARN_ON(omap_obj
->pages
);
237 pages
= drm_gem_get_pages(obj
);
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
);
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
])) {
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
);
271 addrs
= kzalloc(npages
* sizeof(*addrs
), GFP_KERNEL
);
278 omap_obj
->dma_addrs
= addrs
;
279 omap_obj
->pages
= pages
;
286 drm_gem_put_pages(obj
, pages
, true, false);
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
);
299 if ((omap_obj
->flags
& OMAP_BO_MEM_SHMEM
) && !omap_obj
->pages
) {
300 ret
= omap_gem_attach_pages(obj
);
302 dev_err(obj
->dev
->dev
, "could not attach pages\n");
307 /* TODO: even phys-contig.. we should have a list of pages? */
308 *pages
= omap_obj
->pages
;
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
;
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
)
342 mutex_lock(&obj
->dev
->struct_mutex
);
343 offset
= mmap_offset(obj
);
344 mutex_unlock(&obj
->dev
->struct_mutex
);
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
);
368 /* -----------------------------------------------------------------------------
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
);
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
]);
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? */
407 pgoff_t pgoff
, base_pgoff
;
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
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: */
445 evict_entry(entry
->obj
, fmt
, entry
);
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: */
456 entry
->obj_pgoff
+= off
;
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);
477 dev_err(obj
->dev
->dev
, "failed to pin: %d\n", 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
;
500 * omap_gem_fault - pagefault handler for GEM objects
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
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
;
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
);
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
);
539 ret
= fault_1d(obj
, vma
, vmf
);
543 mutex_unlock(&dev
->struct_mutex
);
550 * EBUSY is ok: this just means that another thread
551 * already did the job.
553 return VM_FAULT_NOPAGE
;
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
)
566 ret
= drm_gem_mmap(filp
, vma
);
568 DBG("mmap failed: %d", 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
));
589 * We do have some private objects, at least for scanout buffers
590 * on hardware without DMM/TILER. But these are allocated write-
593 if (WARN_ON(!obj
->filp
))
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)
603 vma
->vm_file
= get_file(obj
->filp
);
605 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
611 /* -----------------------------------------------------------------------------
616 * omap_gem_dumb_create - create a dumb buffer
617 * @drm_file: our client file
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
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
){
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
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
;
657 /* GEM does all our handle to object mapping */
658 obj
= drm_gem_object_lookup(file
, handle
);
664 *offset
= omap_gem_mmap_offset(obj
);
666 drm_gem_object_unreference_unlocked(obj
);
672 #ifdef CONFIG_DRM_FBDEV_EMULATION
673 /* Set scrolling position. This allows us to implement fast scrolling
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
;
685 dev_err(obj
->dev
->dev
, "invalid roll: %d\n", roll
);
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
) {
696 ret
= get_pages(obj
, &pages
);
699 ret
= tiler_pin(omap_obj
->block
, pages
, npages
, roll
, true);
701 dev_err(obj
->dev
->dev
, "could not repin: %d\n", ret
);
705 mutex_unlock(&obj
->dev
->struct_mutex
);
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
))
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
;
760 if (is_cached_coherent(obj
))
763 for (i
= 0; i
< npages
; i
++) {
764 if (!omap_obj
->dma_addrs
[i
]) {
767 addr
= dma_map_page(dev
->dev
, pages
[i
], 0,
769 if (dma_mapping_error(dev
->dev
, addr
)) {
770 dev_warn(dev
->dev
, "%s: failed to map page\n",
776 omap_obj
->dma_addrs
[i
] = addr
;
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
);
806 mutex_lock(&obj
->dev
->struct_mutex
);
808 if (!is_contiguous(omap_obj
) && priv
->has_dmm
) {
809 if (omap_obj
->dma_addr_cnt
== 0) {
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
);
821 if (omap_obj
->flags
& OMAP_BO_TILED
) {
822 block
= tiler_reserve_2d(fmt
,
824 omap_obj
->height
, 0);
826 block
= tiler_reserve_1d(obj
->size
);
830 ret
= PTR_ERR(block
);
831 dev_err(obj
->dev
->dev
,
832 "could not remap: %d (%d)\n", ret
, fmt
);
836 /* TODO: enable async refill.. */
837 ret
= tiler_pin(block
, pages
, npages
,
838 omap_obj
->roll
, true);
840 tiler_release(block
);
841 dev_err(obj
->dev
->dev
,
842 "could not pin: %d\n", ret
);
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
;
863 mutex_unlock(&obj
->dev
->struct_mutex
);
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
);
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
);
887 dev_err(obj
->dev
->dev
,
888 "could not unpin pages: %d\n", ret
);
890 ret
= tiler_release(omap_obj
->block
);
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
);
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
);
919 mutex_unlock(&obj
->dev
->struct_mutex
);
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
);
928 if (omap_obj
->flags
& OMAP_BO_TILED
)
929 ret
= tiler_stride(gem2fmt(omap_obj
->flags
), orient
);
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
,
948 struct omap_gem_object
*omap_obj
= to_omap_bo(obj
);
949 if (!omap_obj
->pages
)
951 *pages
= omap_obj
->pages
;
954 mutex_lock(&obj
->dev
->struct_mutex
);
955 ret
= get_pages(obj
, pages
);
956 mutex_unlock(&obj
->dev
->struct_mutex
);
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..
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
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
) {
981 int ret
= get_pages(obj
, &pages
);
984 omap_obj
->vaddr
= vmap(pages
, obj
->size
>> PAGE_SHIFT
,
985 VM_MAP
, pgprot_writecombine(PAGE_KERNEL
));
987 return omap_obj
->vaddr
;
991 /* -----------------------------------------------------------------------------
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
;
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);
1012 dev_err(dev
->dev
, "could not repin: %d\n", ret
);
1022 /* -----------------------------------------------------------------------------
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
);
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
);
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
;
1060 list_for_each_entry(omap_obj
, list
, mm_list
) {
1061 struct drm_gem_object
*obj
= &omap_obj
->base
;
1063 omap_gem_describe(obj
, m
);
1068 seq_printf(m
, "Total %d objects, %zu bytes\n", count
, size
);
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
);
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
);
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
);
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
;
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");
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
);
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
;
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
);
1190 ret
= drm_gem_object_init(dev
, obj
, size
);
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
,
1203 if (!omap_obj
->vaddr
)
1207 spin_lock(&priv
->list_lock
);
1208 list_add(&omap_obj
->mm_list
, &priv
->obj_list
);
1209 spin_unlock(&priv
->list_lock
);
1214 drm_gem_object_release(obj
);
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
);
1237 obj
= ERR_PTR(-ENOMEM
);
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
);
1247 /* Create pages list from sgt */
1248 struct sg_page_iter iter
;
1249 struct page
**pages
;
1250 unsigned int npages
;
1253 npages
= DIV_ROUND_UP(size
, PAGE_SIZE
);
1254 pages
= kcalloc(npages
, sizeof(*pages
), GFP_KERNEL
);
1256 omap_gem_free_object(obj
);
1257 obj
= ERR_PTR(-ENOMEM
);
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
);
1269 if (WARN_ON(i
!= npages
)) {
1270 omap_gem_free_object(obj
);
1271 obj
= ERR_PTR(-ENOMEM
);
1277 mutex_unlock(&dev
->struct_mutex
);
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
;
1288 obj
= omap_gem_new(dev
, gsize
, flags
);
1292 ret
= drm_gem_handle_create(file
, obj
, handle
);
1294 omap_gem_free_object(obj
);
1298 /* drop reference from allocate - handle holds it now */
1299 drm_gem_object_unreference_unlocked(obj
);
1304 /* -----------------------------------------------------------------------------
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
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");
1324 usergart
= kcalloc(3, sizeof(*usergart
), GFP_KERNEL
);
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
)) {
1348 "reserve failed: %d, %d, %ld\n",
1349 i
, j
, PTR_ERR(block
));
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
,
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
);