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