]> Pileus Git - ~andy/linux/blob - drivers/staging/omapdrm/omap_gem.c
staging: drm/omap: add drm_plane support
[~andy/linux] / drivers / staging / omapdrm / omap_gem.c
1 /*
2  * drivers/staging/omapdrm/omap_gem.c
3  *
4  * Copyright (C) 2011 Texas Instruments
5  * Author: Rob Clark <rob.clark@linaro.org>
6  *
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.
10  *
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
14  * more details.
15  *
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/>.
18  */
19
20
21 #include <linux/spinlock.h>
22 #include <linux/shmem_fs.h>
23
24 #include "omap_drv.h"
25 #include "omap_dmm_tiler.h"
26
27 /* remove these once drm core helpers are merged */
28 struct page ** _drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask);
29 void _drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
30                 bool dirty, bool accessed);
31 int _drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
32
33 /*
34  * GEM buffer object implementation.
35  */
36
37 #define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
38
39 /* note: we use upper 8 bits of flags for driver-internal flags: */
40 #define OMAP_BO_DMA                     0x01000000      /* actually is physically contiguous */
41 #define OMAP_BO_EXT_SYNC        0x02000000      /* externally allocated sync object */
42 #define OMAP_BO_EXT_MEM         0x04000000      /* externally allocated memory */
43
44
45 struct omap_gem_object {
46         struct drm_gem_object base;
47
48         uint32_t flags;
49
50         /** width/height for tiled formats (rounded up to slot boundaries) */
51         uint16_t width, height;
52
53         /** roll applied when mapping to DMM */
54         uint32_t roll;
55
56         /**
57          * If buffer is allocated physically contiguous, the OMAP_BO_DMA flag
58          * is set and the paddr is valid.  Also if the buffer is remapped in
59          * TILER and paddr_cnt > 0, then paddr is valid.  But if you are using
60          * the physical address and OMAP_BO_DMA is not set, then you should
61          * be going thru omap_gem_{get,put}_paddr() to ensure the mapping is
62          * not removed from under your feet.
63          *
64          * Note that OMAP_BO_SCANOUT is a hint from userspace that DMA capable
65          * buffer is requested, but doesn't mean that it is.  Use the
66          * OMAP_BO_DMA flag to determine if the buffer has a DMA capable
67          * physical address.
68          */
69         dma_addr_t paddr;
70
71         /**
72          * # of users of paddr
73          */
74         uint32_t paddr_cnt;
75
76         /**
77          * tiler block used when buffer is remapped in DMM/TILER.
78          */
79         struct tiler_block *block;
80
81         /**
82          * Array of backing pages, if allocated.  Note that pages are never
83          * allocated for buffers originally allocated from contiguous memory
84          */
85         struct page **pages;
86
87         /** addresses corresponding to pages in above array */
88         dma_addr_t *addrs;
89
90         /**
91          * Virtual address, if mapped.
92          */
93         void *vaddr;
94
95         /**
96          * sync-object allocated on demand (if needed)
97          *
98          * Per-buffer sync-object for tracking pending and completed hw/dma
99          * read and write operations.  The layout in memory is dictated by
100          * the SGX firmware, which uses this information to stall the command
101          * stream if a surface is not ready yet.
102          *
103          * Note that when buffer is used by SGX, the sync-object needs to be
104          * allocated from a special heap of sync-objects.  This way many sync
105          * objects can be packed in a page, and not waste GPU virtual address
106          * space.  Because of this we have to have a omap_gem_set_sync_object()
107          * API to allow replacement of the syncobj after it has (potentially)
108          * already been allocated.  A bit ugly but I haven't thought of a
109          * better alternative.
110          */
111         struct {
112                 uint32_t write_pending;
113                 uint32_t write_complete;
114                 uint32_t read_pending;
115                 uint32_t read_complete;
116         } *sync;
117 };
118
119 /* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
120  * not necessarily pinned in TILER all the time, and (b) when they are
121  * they are not necessarily page aligned, we reserve one or more small
122  * regions in each of the 2d containers to use as a user-GART where we
123  * can create a second page-aligned mapping of parts of the buffer
124  * being accessed from userspace.
125  *
126  * Note that we could optimize slightly when we know that multiple
127  * tiler containers are backed by the same PAT.. but I'll leave that
128  * for later..
129  */
130 #define NUM_USERGART_ENTRIES 2
131 struct usergart_entry {
132         struct tiler_block *block;      /* the reserved tiler block */
133         dma_addr_t paddr;
134         struct drm_gem_object *obj;     /* the current pinned obj */
135         pgoff_t obj_pgoff;              /* page offset of obj currently
136                                            mapped in */
137 };
138 static struct {
139         struct usergart_entry entry[NUM_USERGART_ENTRIES];
140         int height;                             /* height in rows */
141         int height_shift;               /* ilog2(height in rows) */
142         int slot_shift;                 /* ilog2(width per slot) */
143         int stride_pfn;                 /* stride in pages */
144         int last;                               /* index of last used entry */
145 } *usergart;
146
147 static void evict_entry(struct drm_gem_object *obj,
148                 enum tiler_fmt fmt, struct usergart_entry *entry)
149 {
150         if (obj->dev->dev_mapping) {
151                 size_t size = PAGE_SIZE * usergart[fmt].height;
152                 loff_t off = omap_gem_mmap_offset(obj) +
153                                 (entry->obj_pgoff << PAGE_SHIFT);
154                 unmap_mapping_range(obj->dev->dev_mapping, off, size, 1);
155         }
156
157         entry->obj = NULL;
158 }
159
160 /* Evict a buffer from usergart, if it is mapped there */
161 static void evict(struct drm_gem_object *obj)
162 {
163         struct omap_gem_object *omap_obj = to_omap_bo(obj);
164
165         if (omap_obj->flags & OMAP_BO_TILED) {
166                 enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
167                 int i;
168
169                 if (!usergart)
170                         return;
171
172                 for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
173                         struct usergart_entry *entry = &usergart[fmt].entry[i];
174                         if (entry->obj == obj)
175                                 evict_entry(obj, fmt, entry);
176                 }
177         }
178 }
179
180 /* GEM objects can either be allocated from contiguous memory (in which
181  * case obj->filp==NULL), or w/ shmem backing (obj->filp!=NULL).  But non
182  * contiguous buffers can be remapped in TILER/DMM if they need to be
183  * contiguous... but we don't do this all the time to reduce pressure
184  * on TILER/DMM space when we know at allocation time that the buffer
185  * will need to be scanned out.
186  */
187 static inline bool is_shmem(struct drm_gem_object *obj)
188 {
189         return obj->filp != NULL;
190 }
191
192 static int get_pages(struct drm_gem_object *obj, struct page ***pages);
193
194 static DEFINE_SPINLOCK(sync_lock);
195
196 /** ensure backing pages are allocated */
197 static int omap_gem_attach_pages(struct drm_gem_object *obj)
198 {
199         struct omap_gem_object *omap_obj = to_omap_bo(obj);
200         struct page **pages;
201
202         WARN_ON(omap_obj->pages);
203
204         /* TODO: __GFP_DMA32 .. but somehow GFP_HIGHMEM is coming from the
205          * mapping_gfp_mask(mapping) which conflicts w/ GFP_DMA32.. probably
206          * we actually want CMA memory for it all anyways..
207          */
208         pages = _drm_gem_get_pages(obj, GFP_KERNEL);
209         if (IS_ERR(pages)) {
210                 dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
211                 return PTR_ERR(pages);
212         }
213
214         /* for non-cached buffers, ensure the new pages are clean because
215          * DSS, GPU, etc. are not cache coherent:
216          */
217         if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
218                 int i, npages = obj->size >> PAGE_SHIFT;
219                 dma_addr_t *addrs = kmalloc(npages * sizeof(addrs), GFP_KERNEL);
220                 for (i = 0; i < npages; i++) {
221                         addrs[i] = dma_map_page(obj->dev->dev, pages[i],
222                                         0, PAGE_SIZE, DMA_BIDIRECTIONAL);
223                 }
224                 omap_obj->addrs = addrs;
225         }
226
227         omap_obj->pages = pages;
228         return 0;
229 }
230
231 /** release backing pages */
232 static void omap_gem_detach_pages(struct drm_gem_object *obj)
233 {
234         struct omap_gem_object *omap_obj = to_omap_bo(obj);
235
236         /* for non-cached buffers, ensure the new pages are clean because
237          * DSS, GPU, etc. are not cache coherent:
238          */
239         if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
240                 int i, npages = obj->size >> PAGE_SHIFT;
241                 for (i = 0; i < npages; i++) {
242                         dma_unmap_page(obj->dev->dev, omap_obj->addrs[i],
243                                         PAGE_SIZE, DMA_BIDIRECTIONAL);
244                 }
245                 kfree(omap_obj->addrs);
246                 omap_obj->addrs = NULL;
247         }
248
249         _drm_gem_put_pages(obj, omap_obj->pages, true, false);
250         omap_obj->pages = NULL;
251 }
252
253 /** get mmap offset */
254 uint64_t omap_gem_mmap_offset(struct drm_gem_object *obj)
255 {
256         if (!obj->map_list.map) {
257                 /* Make it mmapable */
258                 size_t size = omap_gem_mmap_size(obj);
259                 int ret = _drm_gem_create_mmap_offset_size(obj, size);
260
261                 if (ret) {
262                         dev_err(obj->dev->dev, "could not allocate mmap offset");
263                         return 0;
264                 }
265         }
266
267         return (uint64_t)obj->map_list.hash.key << PAGE_SHIFT;
268 }
269
270 /** get mmap size */
271 size_t omap_gem_mmap_size(struct drm_gem_object *obj)
272 {
273         struct omap_gem_object *omap_obj = to_omap_bo(obj);
274         size_t size = obj->size;
275
276         if (omap_obj->flags & OMAP_BO_TILED) {
277                 /* for tiled buffers, the virtual size has stride rounded up
278                  * to 4kb.. (to hide the fact that row n+1 might start 16kb or
279                  * 32kb later!).  But we don't back the entire buffer with
280                  * pages, only the valid picture part.. so need to adjust for
281                  * this in the size used to mmap and generate mmap offset
282                  */
283                 size = tiler_vsize(gem2fmt(omap_obj->flags),
284                                 omap_obj->width, omap_obj->height);
285         }
286
287         return size;
288 }
289
290
291 /* Normal handling for the case of faulting in non-tiled buffers */
292 static int fault_1d(struct drm_gem_object *obj,
293                 struct vm_area_struct *vma, struct vm_fault *vmf)
294 {
295         struct omap_gem_object *omap_obj = to_omap_bo(obj);
296         unsigned long pfn;
297         pgoff_t pgoff;
298
299         /* We don't use vmf->pgoff since that has the fake offset: */
300         pgoff = ((unsigned long)vmf->virtual_address -
301                         vma->vm_start) >> PAGE_SHIFT;
302
303         if (omap_obj->pages) {
304                 pfn = page_to_pfn(omap_obj->pages[pgoff]);
305         } else {
306                 BUG_ON(!(omap_obj->flags & OMAP_BO_DMA));
307                 pfn = (omap_obj->paddr >> PAGE_SHIFT) + pgoff;
308         }
309
310         VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
311                         pfn, pfn << PAGE_SHIFT);
312
313         return vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
314 }
315
316 /* Special handling for the case of faulting in 2d tiled buffers */
317 static int fault_2d(struct drm_gem_object *obj,
318                 struct vm_area_struct *vma, struct vm_fault *vmf)
319 {
320         struct omap_gem_object *omap_obj = to_omap_bo(obj);
321         struct usergart_entry *entry;
322         enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
323         struct page *pages[64];  /* XXX is this too much to have on stack? */
324         unsigned long pfn;
325         pgoff_t pgoff, base_pgoff;
326         void __user *vaddr;
327         int i, ret, slots;
328
329         if (!usergart)
330                 return -EFAULT;
331
332         /* TODO: this fxn might need a bit tweaking to deal w/ tiled buffers
333          * that are wider than 4kb
334          */
335
336         /* We don't use vmf->pgoff since that has the fake offset: */
337         pgoff = ((unsigned long)vmf->virtual_address -
338                         vma->vm_start) >> PAGE_SHIFT;
339
340         /* actual address we start mapping at is rounded down to previous slot
341          * boundary in the y direction:
342          */
343         base_pgoff = round_down(pgoff, usergart[fmt].height);
344         vaddr = vmf->virtual_address - ((pgoff - base_pgoff) << PAGE_SHIFT);
345         entry = &usergart[fmt].entry[usergart[fmt].last];
346
347         slots = omap_obj->width >> usergart[fmt].slot_shift;
348
349         /* evict previous buffer using this usergart entry, if any: */
350         if (entry->obj)
351                 evict_entry(entry->obj, fmt, entry);
352
353         entry->obj = obj;
354         entry->obj_pgoff = base_pgoff;
355
356         /* now convert base_pgoff to phys offset from virt offset:
357          */
358         base_pgoff = (base_pgoff >> usergart[fmt].height_shift) * slots;
359
360         /* map in pages.  Note the height of the slot is also equal to the
361          * number of pages that need to be mapped in to fill 4kb wide CPU page.
362          * If the height is 64, then 64 pages fill a 4kb wide by 64 row region.
363          * Beyond the valid pixel part of the buffer, we set pages[i] to NULL to
364          * get a dummy page mapped in.. if someone reads/writes it they will get
365          * random/undefined content, but at least it won't be corrupting
366          * whatever other random page used to be mapped in, or other undefined
367          * behavior.
368          */
369         memcpy(pages, &omap_obj->pages[base_pgoff],
370                         sizeof(struct page *) * slots);
371         memset(pages + slots, 0,
372                         sizeof(struct page *) * (usergart[fmt].height - slots));
373
374         ret = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
375         if (ret) {
376                 dev_err(obj->dev->dev, "failed to pin: %d\n", ret);
377                 return ret;
378         }
379
380         i = usergart[fmt].height;
381         pfn = entry->paddr >> PAGE_SHIFT;
382
383         VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
384                         pfn, pfn << PAGE_SHIFT);
385
386         while (i--) {
387                 vm_insert_mixed(vma, (unsigned long)vaddr, pfn);
388                 pfn += usergart[fmt].stride_pfn;
389                 vaddr += PAGE_SIZE;
390         }
391
392         /* simple round-robin: */
393         usergart[fmt].last = (usergart[fmt].last + 1) % NUM_USERGART_ENTRIES;
394
395         return 0;
396 }
397
398 /**
399  * omap_gem_fault               -       pagefault handler for GEM objects
400  * @vma: the VMA of the GEM object
401  * @vmf: fault detail
402  *
403  * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
404  * does most of the work for us including the actual map/unmap calls
405  * but we need to do the actual page work.
406  *
407  * The VMA was set up by GEM. In doing so it also ensured that the
408  * vma->vm_private_data points to the GEM object that is backing this
409  * mapping.
410  */
411 int omap_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
412 {
413         struct drm_gem_object *obj = vma->vm_private_data;
414         struct omap_gem_object *omap_obj = to_omap_bo(obj);
415         struct drm_device *dev = obj->dev;
416         struct page **pages;
417         int ret;
418
419         /* Make sure we don't parallel update on a fault, nor move or remove
420          * something from beneath our feet
421          */
422         mutex_lock(&dev->struct_mutex);
423
424         /* if a shmem backed object, make sure we have pages attached now */
425         ret = get_pages(obj, &pages);
426         if (ret) {
427                 goto fail;
428         }
429
430         /* where should we do corresponding put_pages().. we are mapping
431          * the original page, rather than thru a GART, so we can't rely
432          * on eviction to trigger this.  But munmap() or all mappings should
433          * probably trigger put_pages()?
434          */
435
436         if (omap_obj->flags & OMAP_BO_TILED)
437                 ret = fault_2d(obj, vma, vmf);
438         else
439                 ret = fault_1d(obj, vma, vmf);
440
441
442 fail:
443         mutex_unlock(&dev->struct_mutex);
444         switch (ret) {
445         case 0:
446         case -ERESTARTSYS:
447         case -EINTR:
448                 return VM_FAULT_NOPAGE;
449         case -ENOMEM:
450                 return VM_FAULT_OOM;
451         default:
452                 return VM_FAULT_SIGBUS;
453         }
454 }
455
456 /** We override mainly to fix up some of the vm mapping flags.. */
457 int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma)
458 {
459         struct omap_gem_object *omap_obj;
460         int ret;
461
462         ret = drm_gem_mmap(filp, vma);
463         if (ret) {
464                 DBG("mmap failed: %d", ret);
465                 return ret;
466         }
467
468         /* after drm_gem_mmap(), it is safe to access the obj */
469         omap_obj = to_omap_bo(vma->vm_private_data);
470
471         vma->vm_flags &= ~VM_PFNMAP;
472         vma->vm_flags |= VM_MIXEDMAP;
473
474         if (omap_obj->flags & OMAP_BO_WC) {
475                 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
476         } else if (omap_obj->flags & OMAP_BO_UNCACHED) {
477                 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
478         } else {
479                 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
480         }
481
482         return ret;
483 }
484
485 /**
486  * omap_gem_dumb_create -       create a dumb buffer
487  * @drm_file: our client file
488  * @dev: our device
489  * @args: the requested arguments copied from userspace
490  *
491  * Allocate a buffer suitable for use for a frame buffer of the
492  * form described by user space. Give userspace a handle by which
493  * to reference it.
494  */
495 int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
496                 struct drm_mode_create_dumb *args)
497 {
498         union omap_gem_size gsize;
499
500         /* in case someone tries to feed us a completely bogus stride: */
501         args->pitch = align_pitch(args->pitch, args->width, args->bpp);
502         args->size = PAGE_ALIGN(args->pitch * args->height);
503
504         gsize = (union omap_gem_size){
505                 .bytes = args->size,
506         };
507
508         return omap_gem_new_handle(dev, file, gsize,
509                         OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle);
510 }
511
512 /**
513  * omap_gem_dumb_destroy        -       destroy a dumb buffer
514  * @file: client file
515  * @dev: our DRM device
516  * @handle: the object handle
517  *
518  * Destroy a handle that was created via omap_gem_dumb_create.
519  */
520 int omap_gem_dumb_destroy(struct drm_file *file, struct drm_device *dev,
521                 uint32_t handle)
522 {
523         /* No special work needed, drop the reference and see what falls out */
524         return drm_gem_handle_delete(file, handle);
525 }
526
527 /**
528  * omap_gem_dumb_map    -       buffer mapping for dumb interface
529  * @file: our drm client file
530  * @dev: drm device
531  * @handle: GEM handle to the object (from dumb_create)
532  *
533  * Do the necessary setup to allow the mapping of the frame buffer
534  * into user memory. We don't have to do much here at the moment.
535  */
536 int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
537                 uint32_t handle, uint64_t *offset)
538 {
539         struct drm_gem_object *obj;
540         int ret = 0;
541
542         /* GEM does all our handle to object mapping */
543         obj = drm_gem_object_lookup(dev, file, handle);
544         if (obj == NULL) {
545                 ret = -ENOENT;
546                 goto fail;
547         }
548
549         *offset = omap_gem_mmap_offset(obj);
550
551         drm_gem_object_unreference_unlocked(obj);
552
553 fail:
554         return ret;
555 }
556
557 /* Set scrolling position.  This allows us to implement fast scrolling
558  * for console.
559  */
560 int omap_gem_roll(struct drm_gem_object *obj, uint32_t roll)
561 {
562         struct omap_gem_object *omap_obj = to_omap_bo(obj);
563         uint32_t npages = obj->size >> PAGE_SHIFT;
564         int ret = 0;
565
566         if (roll > npages) {
567                 dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
568                 return -EINVAL;
569         }
570
571         omap_obj->roll = roll;
572
573         if (in_atomic() || mutex_is_locked(&obj->dev->struct_mutex)) {
574                 /* this can get called from fbcon in atomic context.. so
575                  * just ignore it and wait for next time called from
576                  * interruptible context to update the PAT.. the result
577                  * may be that user sees wrap-around instead of scrolling
578                  * momentarily on the screen.  If we wanted to be fancier
579                  * we could perhaps schedule some workqueue work at this
580                  * point.
581                  */
582                 return 0;
583         }
584
585         mutex_lock(&obj->dev->struct_mutex);
586
587         /* if we aren't mapped yet, we don't need to do anything */
588         if (omap_obj->block) {
589                 struct page **pages;
590                 ret = get_pages(obj, &pages);
591                 if (ret)
592                         goto fail;
593                 ret = tiler_pin(omap_obj->block, pages, npages, roll, true);
594                 if (ret)
595                         dev_err(obj->dev->dev, "could not repin: %d\n", ret);
596         }
597
598 fail:
599         mutex_unlock(&obj->dev->struct_mutex);
600
601         return ret;
602 }
603
604 /* Get physical address for DMA.. if 'remap' is true, and the buffer is not
605  * already contiguous, remap it to pin in physically contiguous memory.. (ie.
606  * map in TILER)
607  */
608 int omap_gem_get_paddr(struct drm_gem_object *obj,
609                 dma_addr_t *paddr, bool remap)
610 {
611         struct omap_drm_private *priv = obj->dev->dev_private;
612         struct omap_gem_object *omap_obj = to_omap_bo(obj);
613         int ret = 0;
614
615         mutex_lock(&obj->dev->struct_mutex);
616
617         if (remap && is_shmem(obj) && priv->has_dmm) {
618                 if (omap_obj->paddr_cnt == 0) {
619                         struct page **pages;
620                         uint32_t npages = obj->size >> PAGE_SHIFT;
621                         enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
622                         struct tiler_block *block;
623
624                         BUG_ON(omap_obj->block);
625
626                         ret = get_pages(obj, &pages);
627                         if (ret)
628                                 goto fail;
629
630                         if (omap_obj->flags & OMAP_BO_TILED) {
631                                 block = tiler_reserve_2d(fmt,
632                                                 omap_obj->width,
633                                                 omap_obj->height, 0);
634                         } else {
635                                 block = tiler_reserve_1d(obj->size);
636                         }
637
638                         if (IS_ERR(block)) {
639                                 ret = PTR_ERR(block);
640                                 dev_err(obj->dev->dev,
641                                         "could not remap: %d (%d)\n", ret, fmt);
642                                 goto fail;
643                         }
644
645                         /* TODO: enable async refill.. */
646                         ret = tiler_pin(block, pages, npages,
647                                         omap_obj->roll, true);
648                         if (ret) {
649                                 tiler_release(block);
650                                 dev_err(obj->dev->dev,
651                                                 "could not pin: %d\n", ret);
652                                 goto fail;
653                         }
654
655                         omap_obj->paddr = tiler_ssptr(block);
656                         omap_obj->block = block;
657
658                         DBG("got paddr: %08x", omap_obj->paddr);
659                 }
660
661                 omap_obj->paddr_cnt++;
662
663                 *paddr = omap_obj->paddr;
664         } else if (omap_obj->flags & OMAP_BO_DMA) {
665                 *paddr = omap_obj->paddr;
666         } else {
667                 ret = -EINVAL;
668         }
669
670 fail:
671         mutex_unlock(&obj->dev->struct_mutex);
672
673         return ret;
674 }
675
676 /* Release physical address, when DMA is no longer being performed.. this
677  * could potentially unpin and unmap buffers from TILER
678  */
679 int omap_gem_put_paddr(struct drm_gem_object *obj)
680 {
681         struct omap_gem_object *omap_obj = to_omap_bo(obj);
682         int ret = 0;
683
684         mutex_lock(&obj->dev->struct_mutex);
685         if (omap_obj->paddr_cnt > 0) {
686                 omap_obj->paddr_cnt--;
687                 if (omap_obj->paddr_cnt == 0) {
688                         ret = tiler_unpin(omap_obj->block);
689                         if (ret) {
690                                 dev_err(obj->dev->dev,
691                                         "could not unpin pages: %d\n", ret);
692                                 goto fail;
693                         }
694                         ret = tiler_release(omap_obj->block);
695                         if (ret) {
696                                 dev_err(obj->dev->dev,
697                                         "could not release unmap: %d\n", ret);
698                         }
699                         omap_obj->block = NULL;
700                 }
701         }
702 fail:
703         mutex_unlock(&obj->dev->struct_mutex);
704         return ret;
705 }
706
707 /* acquire pages when needed (for example, for DMA where physically
708  * contiguous buffer is not required
709  */
710 static int get_pages(struct drm_gem_object *obj, struct page ***pages)
711 {
712         struct omap_gem_object *omap_obj = to_omap_bo(obj);
713         int ret = 0;
714
715         if (is_shmem(obj) && !omap_obj->pages) {
716                 ret = omap_gem_attach_pages(obj);
717                 if (ret) {
718                         dev_err(obj->dev->dev, "could not attach pages\n");
719                         return ret;
720                 }
721         }
722
723         /* TODO: even phys-contig.. we should have a list of pages? */
724         *pages = omap_obj->pages;
725
726         return 0;
727 }
728
729 int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages)
730 {
731         int ret;
732         mutex_lock(&obj->dev->struct_mutex);
733         ret = get_pages(obj, pages);
734         mutex_unlock(&obj->dev->struct_mutex);
735         return ret;
736 }
737
738 /* release pages when DMA no longer being performed */
739 int omap_gem_put_pages(struct drm_gem_object *obj)
740 {
741         /* do something here if we dynamically attach/detach pages.. at
742          * least they would no longer need to be pinned if everyone has
743          * released the pages..
744          */
745         return 0;
746 }
747
748 /* Get kernel virtual address for CPU access.. this more or less only
749  * exists for omap_fbdev.  This should be called with struct_mutex
750  * held.
751  */
752 void *omap_gem_vaddr(struct drm_gem_object *obj)
753 {
754         struct omap_gem_object *omap_obj = to_omap_bo(obj);
755         WARN_ON(! mutex_is_locked(&obj->dev->struct_mutex));
756         if (!omap_obj->vaddr) {
757                 struct page **pages;
758                 int ret = get_pages(obj, &pages);
759                 if (ret)
760                         return ERR_PTR(ret);
761                 omap_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
762                                 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
763         }
764         return omap_obj->vaddr;
765 }
766
767 /* Buffer Synchronization:
768  */
769
770 struct omap_gem_sync_waiter {
771         struct list_head list;
772         struct omap_gem_object *omap_obj;
773         enum omap_gem_op op;
774         uint32_t read_target, write_target;
775         /* notify called w/ sync_lock held */
776         void (*notify)(void *arg);
777         void *arg;
778 };
779
780 /* list of omap_gem_sync_waiter.. the notify fxn gets called back when
781  * the read and/or write target count is achieved which can call a user
782  * callback (ex. to kick 3d and/or 2d), wakeup blocked task (prep for
783  * cpu access), etc.
784  */
785 static LIST_HEAD(waiters);
786
787 static inline bool is_waiting(struct omap_gem_sync_waiter *waiter)
788 {
789         struct omap_gem_object *omap_obj = waiter->omap_obj;
790         if ((waiter->op & OMAP_GEM_READ) &&
791                         (omap_obj->sync->read_complete < waiter->read_target))
792                 return true;
793         if ((waiter->op & OMAP_GEM_WRITE) &&
794                         (omap_obj->sync->write_complete < waiter->write_target))
795                 return true;
796         return false;
797 }
798
799 /* macro for sync debug.. */
800 #define SYNCDBG 0
801 #define SYNC(fmt, ...) do { if (SYNCDBG) \
802                 printk(KERN_ERR "%s:%d: "fmt"\n", \
803                                 __func__, __LINE__, ##__VA_ARGS__); \
804         } while (0)
805
806
807 static void sync_op_update(void)
808 {
809         struct omap_gem_sync_waiter *waiter, *n;
810         list_for_each_entry_safe(waiter, n, &waiters, list) {
811                 if (!is_waiting(waiter)) {
812                         list_del(&waiter->list);
813                         SYNC("notify: %p", waiter);
814                         waiter->notify(waiter->arg);
815                         kfree(waiter);
816                 }
817         }
818 }
819
820 static inline int sync_op(struct drm_gem_object *obj,
821                 enum omap_gem_op op, bool start)
822 {
823         struct omap_gem_object *omap_obj = to_omap_bo(obj);
824         int ret = 0;
825
826         spin_lock(&sync_lock);
827
828         if (!omap_obj->sync) {
829                 omap_obj->sync = kzalloc(sizeof(*omap_obj->sync), GFP_ATOMIC);
830                 if (!omap_obj->sync) {
831                         ret = -ENOMEM;
832                         goto unlock;
833                 }
834         }
835
836         if (start) {
837                 if (op & OMAP_GEM_READ)
838                         omap_obj->sync->read_pending++;
839                 if (op & OMAP_GEM_WRITE)
840                         omap_obj->sync->write_pending++;
841         } else {
842                 if (op & OMAP_GEM_READ)
843                         omap_obj->sync->read_complete++;
844                 if (op & OMAP_GEM_WRITE)
845                         omap_obj->sync->write_complete++;
846                 sync_op_update();
847         }
848
849 unlock:
850         spin_unlock(&sync_lock);
851
852         return ret;
853 }
854
855 /* it is a bit lame to handle updates in this sort of polling way, but
856  * in case of PVR, the GPU can directly update read/write complete
857  * values, and not really tell us which ones it updated.. this also
858  * means that sync_lock is not quite sufficient.  So we'll need to
859  * do something a bit better when it comes time to add support for
860  * separate 2d hw..
861  */
862 void omap_gem_op_update(void)
863 {
864         spin_lock(&sync_lock);
865         sync_op_update();
866         spin_unlock(&sync_lock);
867 }
868
869 /* mark the start of read and/or write operation */
870 int omap_gem_op_start(struct drm_gem_object *obj, enum omap_gem_op op)
871 {
872         return sync_op(obj, op, true);
873 }
874
875 int omap_gem_op_finish(struct drm_gem_object *obj, enum omap_gem_op op)
876 {
877         return sync_op(obj, op, false);
878 }
879
880 static DECLARE_WAIT_QUEUE_HEAD(sync_event);
881
882 static void sync_notify(void *arg)
883 {
884         struct task_struct **waiter_task = arg;
885         *waiter_task = NULL;
886         wake_up_all(&sync_event);
887 }
888
889 int omap_gem_op_sync(struct drm_gem_object *obj, enum omap_gem_op op)
890 {
891         struct omap_gem_object *omap_obj = to_omap_bo(obj);
892         int ret = 0;
893         if (omap_obj->sync) {
894                 struct task_struct *waiter_task = current;
895                 struct omap_gem_sync_waiter *waiter =
896                                 kzalloc(sizeof(*waiter), GFP_KERNEL);
897
898                 if (!waiter) {
899                         return -ENOMEM;
900                 }
901
902                 waiter->omap_obj = omap_obj;
903                 waiter->op = op;
904                 waiter->read_target = omap_obj->sync->read_pending;
905                 waiter->write_target = omap_obj->sync->write_pending;
906                 waiter->notify = sync_notify;
907                 waiter->arg = &waiter_task;
908
909                 spin_lock(&sync_lock);
910                 if (is_waiting(waiter)) {
911                         SYNC("waited: %p", waiter);
912                         list_add_tail(&waiter->list, &waiters);
913                         spin_unlock(&sync_lock);
914                         ret = wait_event_interruptible(sync_event,
915                                         (waiter_task == NULL));
916                         spin_lock(&sync_lock);
917                         if (waiter_task) {
918                                 SYNC("interrupted: %p", waiter);
919                                 /* we were interrupted */
920                                 list_del(&waiter->list);
921                                 waiter_task = NULL;
922                         } else {
923                                 /* freed in sync_op_update() */
924                                 waiter = NULL;
925                         }
926                 }
927                 spin_unlock(&sync_lock);
928
929                 if (waiter) {
930                         kfree(waiter);
931                 }
932         }
933         return ret;
934 }
935
936 /* call fxn(arg), either synchronously or asynchronously if the op
937  * is currently blocked..  fxn() can be called from any context
938  *
939  * (TODO for now fxn is called back from whichever context calls
940  * omap_gem_op_update().. but this could be better defined later
941  * if needed)
942  *
943  * TODO more code in common w/ _sync()..
944  */
945 int omap_gem_op_async(struct drm_gem_object *obj, enum omap_gem_op op,
946                 void (*fxn)(void *arg), void *arg)
947 {
948         struct omap_gem_object *omap_obj = to_omap_bo(obj);
949         if (omap_obj->sync) {
950                 struct omap_gem_sync_waiter *waiter =
951                                 kzalloc(sizeof(*waiter), GFP_ATOMIC);
952
953                 if (!waiter) {
954                         return -ENOMEM;
955                 }
956
957                 waiter->omap_obj = omap_obj;
958                 waiter->op = op;
959                 waiter->read_target = omap_obj->sync->read_pending;
960                 waiter->write_target = omap_obj->sync->write_pending;
961                 waiter->notify = fxn;
962                 waiter->arg = arg;
963
964                 spin_lock(&sync_lock);
965                 if (is_waiting(waiter)) {
966                         SYNC("waited: %p", waiter);
967                         list_add_tail(&waiter->list, &waiters);
968                         spin_unlock(&sync_lock);
969                         return 0;
970                 }
971
972                 spin_unlock(&sync_lock);
973         }
974
975         /* no waiting.. */
976         fxn(arg);
977
978         return 0;
979 }
980
981 /* special API so PVR can update the buffer to use a sync-object allocated
982  * from it's sync-obj heap.  Only used for a newly allocated (from PVR's
983  * perspective) sync-object, so we overwrite the new syncobj w/ values
984  * from the already allocated syncobj (if there is one)
985  */
986 int omap_gem_set_sync_object(struct drm_gem_object *obj, void *syncobj)
987 {
988         struct omap_gem_object *omap_obj = to_omap_bo(obj);
989         int ret = 0;
990
991         spin_lock(&sync_lock);
992
993         if ((omap_obj->flags & OMAP_BO_EXT_SYNC) && !syncobj) {
994                 /* clearing a previously set syncobj */
995                 syncobj = kzalloc(sizeof(*omap_obj->sync), GFP_ATOMIC);
996                 if (!syncobj) {
997                         ret = -ENOMEM;
998                         goto unlock;
999                 }
1000                 memcpy(syncobj, omap_obj->sync, sizeof(*omap_obj->sync));
1001                 omap_obj->flags &= ~OMAP_BO_EXT_SYNC;
1002                 omap_obj->sync = syncobj;
1003         } else if (syncobj && !(omap_obj->flags & OMAP_BO_EXT_SYNC)) {
1004                 /* replacing an existing syncobj */
1005                 if (omap_obj->sync) {
1006                         memcpy(syncobj, omap_obj->sync, sizeof(*omap_obj->sync));
1007                         kfree(omap_obj->sync);
1008                 }
1009                 omap_obj->flags |= OMAP_BO_EXT_SYNC;
1010                 omap_obj->sync = syncobj;
1011         }
1012
1013 unlock:
1014         spin_unlock(&sync_lock);
1015         return ret;
1016 }
1017
1018 int omap_gem_init_object(struct drm_gem_object *obj)
1019 {
1020         return -EINVAL;          /* unused */
1021 }
1022
1023 /* don't call directly.. called from GEM core when it is time to actually
1024  * free the object..
1025  */
1026 void omap_gem_free_object(struct drm_gem_object *obj)
1027 {
1028         struct drm_device *dev = obj->dev;
1029         struct omap_gem_object *omap_obj = to_omap_bo(obj);
1030
1031         evict(obj);
1032
1033         if (obj->map_list.map) {
1034                 drm_gem_free_mmap_offset(obj);
1035         }
1036
1037         /* don't free externally allocated backing memory */
1038         if (!(omap_obj->flags & OMAP_BO_EXT_MEM)) {
1039                 if (omap_obj->pages) {
1040                         omap_gem_detach_pages(obj);
1041                 }
1042                 if (!is_shmem(obj)) {
1043                         dma_free_writecombine(dev->dev, obj->size,
1044                                         omap_obj->vaddr, omap_obj->paddr);
1045                 } else if (omap_obj->vaddr) {
1046                         vunmap(omap_obj->vaddr);
1047                 }
1048         }
1049
1050         /* don't free externally allocated syncobj */
1051         if (!(omap_obj->flags & OMAP_BO_EXT_SYNC)) {
1052                 kfree(omap_obj->sync);
1053         }
1054
1055         drm_gem_object_release(obj);
1056
1057         kfree(obj);
1058 }
1059
1060 /* convenience method to construct a GEM buffer object, and userspace handle */
1061 int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1062                 union omap_gem_size gsize, uint32_t flags, uint32_t *handle)
1063 {
1064         struct drm_gem_object *obj;
1065         int ret;
1066
1067         obj = omap_gem_new(dev, gsize, flags);
1068         if (!obj)
1069                 return -ENOMEM;
1070
1071         ret = drm_gem_handle_create(file, obj, handle);
1072         if (ret) {
1073                 drm_gem_object_release(obj);
1074                 kfree(obj); /* TODO isn't there a dtor to call? just copying i915 */
1075                 return ret;
1076         }
1077
1078         /* drop reference from allocate - handle holds it now */
1079         drm_gem_object_unreference_unlocked(obj);
1080
1081         return 0;
1082 }
1083
1084 /* GEM buffer object constructor */
1085 struct drm_gem_object *omap_gem_new(struct drm_device *dev,
1086                 union omap_gem_size gsize, uint32_t flags)
1087 {
1088         struct omap_drm_private *priv = dev->dev_private;
1089         struct omap_gem_object *omap_obj;
1090         struct drm_gem_object *obj = NULL;
1091         size_t size;
1092         int ret;
1093
1094         if (flags & OMAP_BO_TILED) {
1095                 if (!usergart) {
1096                         dev_err(dev->dev, "Tiled buffers require DMM\n");
1097                         goto fail;
1098                 }
1099
1100                 /* tiled buffers are always shmem paged backed.. when they are
1101                  * scanned out, they are remapped into DMM/TILER
1102                  */
1103                 flags &= ~OMAP_BO_SCANOUT;
1104
1105                 /* currently don't allow cached buffers.. there is some caching
1106                  * stuff that needs to be handled better
1107                  */
1108                 flags &= ~(OMAP_BO_CACHED|OMAP_BO_UNCACHED);
1109                 flags |= OMAP_BO_WC;
1110
1111                 /* align dimensions to slot boundaries... */
1112                 tiler_align(gem2fmt(flags),
1113                                 &gsize.tiled.width, &gsize.tiled.height);
1114
1115                 /* ...and calculate size based on aligned dimensions */
1116                 size = tiler_size(gem2fmt(flags),
1117                                 gsize.tiled.width, gsize.tiled.height);
1118         } else {
1119                 size = PAGE_ALIGN(gsize.bytes);
1120         }
1121
1122         omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
1123         if (!omap_obj) {
1124                 dev_err(dev->dev, "could not allocate GEM object\n");
1125                 goto fail;
1126         }
1127
1128         obj = &omap_obj->base;
1129
1130         if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
1131                 /* attempt to allocate contiguous memory if we don't
1132                  * have DMM for remappign discontiguous buffers
1133                  */
1134                 omap_obj->vaddr =  dma_alloc_writecombine(dev->dev, size,
1135                                 &omap_obj->paddr, GFP_KERNEL);
1136                 if (omap_obj->vaddr) {
1137                         flags |= OMAP_BO_DMA;
1138                 }
1139         }
1140
1141         omap_obj->flags = flags;
1142
1143         if (flags & OMAP_BO_TILED) {
1144                 omap_obj->width = gsize.tiled.width;
1145                 omap_obj->height = gsize.tiled.height;
1146         }
1147
1148         if (flags & (OMAP_BO_DMA|OMAP_BO_EXT_MEM)) {
1149                 ret = drm_gem_private_object_init(dev, obj, size);
1150         } else {
1151                 ret = drm_gem_object_init(dev, obj, size);
1152         }
1153
1154         if (ret) {
1155                 goto fail;
1156         }
1157
1158         return obj;
1159
1160 fail:
1161         if (obj) {
1162                 omap_gem_free_object(obj);
1163         }
1164         return NULL;
1165 }
1166
1167 /* init/cleanup.. if DMM is used, we need to set some stuff up.. */
1168 void omap_gem_init(struct drm_device *dev)
1169 {
1170         struct omap_drm_private *priv = dev->dev_private;
1171         const enum tiler_fmt fmts[] = {
1172                         TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
1173         };
1174         int i, j, ret;
1175
1176         ret = omap_dmm_init(dev);
1177         if (ret) {
1178                 /* DMM only supported on OMAP4 and later, so this isn't fatal */
1179                 dev_warn(dev->dev, "omap_dmm_init failed, disabling DMM\n");
1180                 return;
1181         }
1182
1183         usergart = kzalloc(3 * sizeof(*usergart), GFP_KERNEL);
1184         if (!usergart) {
1185                 dev_warn(dev->dev, "could not allocate usergart\n");
1186                 return;
1187         }
1188
1189         /* reserve 4k aligned/wide regions for userspace mappings: */
1190         for (i = 0; i < ARRAY_SIZE(fmts); i++) {
1191                 uint16_t h = 1, w = PAGE_SIZE >> i;
1192                 tiler_align(fmts[i], &w, &h);
1193                 /* note: since each region is 1 4kb page wide, and minimum
1194                  * number of rows, the height ends up being the same as the
1195                  * # of pages in the region
1196                  */
1197                 usergart[i].height = h;
1198                 usergart[i].height_shift = ilog2(h);
1199                 usergart[i].stride_pfn = tiler_stride(fmts[i]) >> PAGE_SHIFT;
1200                 usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
1201                 for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
1202                         struct usergart_entry *entry = &usergart[i].entry[j];
1203                         struct tiler_block *block =
1204                                         tiler_reserve_2d(fmts[i], w, h,
1205                                                         PAGE_SIZE);
1206                         if (IS_ERR(block)) {
1207                                 dev_err(dev->dev,
1208                                                 "reserve failed: %d, %d, %ld\n",
1209                                                 i, j, PTR_ERR(block));
1210                                 return;
1211                         }
1212                         entry->paddr = tiler_ssptr(block);
1213                         entry->block = block;
1214
1215                         DBG("%d:%d: %dx%d: paddr=%08x stride=%d", i, j, w, h,
1216                                         entry->paddr,
1217                                         usergart[i].stride_pfn << PAGE_SHIFT);
1218                 }
1219         }
1220
1221         priv->has_dmm = true;
1222 }
1223
1224 void omap_gem_deinit(struct drm_device *dev)
1225 {
1226         /* I believe we can rely on there being no more outstanding GEM
1227          * objects which could depend on usergart/dmm at this point.
1228          */
1229         omap_dmm_remove();
1230         kfree(usergart);
1231 }