1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
28struct page ** _drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask);
29void _drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
30 bool dirty, bool accessed);
31int _drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
32
33
34
35
36
37#define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
38
39
40#define OMAP_BO_DMA 0x01000000
41#define OMAP_BO_EXT_SYNC 0x02000000
42#define OMAP_BO_EXT_MEM 0x04000000
43
44
45struct omap_gem_object {
46 struct drm_gem_object base;
47
48 struct list_head mm_list;
49
50 uint32_t flags;
51
52
53 uint16_t width, height;
54
55
56 uint32_t roll;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 dma_addr_t paddr;
72
73
74
75
76 uint32_t paddr_cnt;
77
78
79
80
81 struct tiler_block *block;
82
83
84
85
86
87 struct page **pages;
88
89
90 dma_addr_t *addrs;
91
92
93
94
95 void *vaddr;
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
121static int get_pages(struct drm_gem_object *obj, struct page ***pages);
122static uint64_t mmap_offset(struct drm_gem_object *obj);
123
124
125
126
127
128
129
130
131
132
133
134
135#define NUM_USERGART_ENTRIES 2
136struct usergart_entry {
137 struct tiler_block *block;
138 dma_addr_t paddr;
139 struct drm_gem_object *obj;
140 pgoff_t obj_pgoff;
141
142};
143static struct {
144 struct usergart_entry entry[NUM_USERGART_ENTRIES];
145 int height;
146 int height_shift;
147 int slot_shift;
148 int stride_pfn;
149 int last;
150} *usergart;
151
152static 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
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
179static 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
199
200
201
202
203
204
205static inline bool is_shmem(struct drm_gem_object *obj)
206{
207 return obj->filp != NULL;
208}
209
210
211
212
213
214static 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
221static DEFINE_SPINLOCK(sync_lock);
222
223
224static 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
235
236
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
245
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
264static void omap_gem_detach_pages(struct drm_gem_object *obj)
265{
266 struct omap_gem_object *omap_obj = to_omap_bo(obj);
267
268
269
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
287uint32_t omap_gem_flags(struct drm_gem_object *obj)
288{
289 return to_omap_bo(obj)->flags;
290}
291
292
293static 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
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
313uint64_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
323size_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
330
331
332
333
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
344static 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
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
370static 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];
377 unsigned long pfn;
378 pgoff_t pgoff, base_pgoff;
379 void __user *vaddr;
380 int i, ret, slots;
381
382
383
384
385
386
387 const int n = usergart[fmt].height;
388 const int n_shift = usergart[fmt].height_shift;
389
390
391
392
393
394
395
396 const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
397
398
399 pgoff = ((unsigned long)vmf->virtual_address -
400 vma->vm_start) >> PAGE_SHIFT;
401
402
403
404
405
406 base_pgoff = round_down(pgoff, m << n_shift);
407
408
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
416 if (entry->obj)
417 evict_entry(entry->obj, fmt, entry);
418
419 entry->obj = obj;
420 entry->obj_pgoff = base_pgoff;
421
422
423 base_pgoff = (base_pgoff >> n_shift) * slots;
424
425
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
437
438
439
440
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
465 usergart[fmt].last = (usergart[fmt].last + 1) % NUM_USERGART_ENTRIES;
466
467 return 0;
468}
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483int 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
492
493
494 mutex_lock(&dev->struct_mutex);
495
496
497 ret = get_pages(obj, &pages);
498 if (ret) {
499 goto fail;
500 }
501
502
503
504
505
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
514fail:
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
529int 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
542int 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
557
558
559
560 if (WARN_ON(!obj->filp))
561 return -EINVAL;
562
563
564
565
566
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
582
583
584
585
586
587
588
589
590int 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
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
609
610
611
612
613
614
615int omap_gem_dumb_destroy(struct drm_file *file, struct drm_device *dev,
616 uint32_t handle)
617{
618
619 return drm_gem_handle_delete(file, handle);
620}
621
622
623
624
625
626
627
628
629
630
631int 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
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
648fail:
649 return ret;
650}
651
652
653
654
655
656
657int 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
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
683fail:
684 mutex_unlock(&obj->dev->struct_mutex);
685
686 return ret;
687}
688
689
690
691
692void 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
705void 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
732
733
734
735int 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
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
798fail:
799 mutex_unlock(&obj->dev->struct_mutex);
800
801 return ret;
802}
803
804
805
806
807int 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 }
830fail:
831 mutex_unlock(&obj->dev->struct_mutex);
832 return ret;
833}
834
835
836
837
838static 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
852 *pages = omap_obj->pages;
853
854 return 0;
855}
856
857
858
859
860
861
862
863
864
865
866
867int 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
885int omap_gem_put_pages(struct drm_gem_object *obj)
886{
887
888
889
890
891 return 0;
892}
893
894
895
896
897
898void *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
914void 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
945void 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
964
965
966struct 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
972 void (*notify)(void *arg);
973 void *arg;
974};
975
976
977
978
979
980
981static LIST_HEAD(waiters);
982
983static 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
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
1003static 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
1016static 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
1045unlock:
1046 spin_unlock(&sync_lock);
1047
1048 return ret;
1049}
1050
1051
1052
1053
1054
1055
1056
1057
1058void omap_gem_op_update(void)
1059{
1060 spin_lock(&sync_lock);
1061 sync_op_update();
1062 spin_unlock(&sync_lock);
1063}
1064
1065
1066int omap_gem_op_start(struct drm_gem_object *obj, enum omap_gem_op op)
1067{
1068 return sync_op(obj, op, true);
1069}
1070
1071int omap_gem_op_finish(struct drm_gem_object *obj, enum omap_gem_op op)
1072{
1073 return sync_op(obj, op, false);
1074}
1075
1076static DECLARE_WAIT_QUEUE_HEAD(sync_event);
1077
1078static 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
1085int 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
1116 list_del(&waiter->list);
1117 waiter_task = NULL;
1118 } else {
1119
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
1133
1134
1135
1136
1137
1138
1139
1140
1141int 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
1172 fxn(arg);
1173
1174 return 0;
1175}
1176
1177
1178
1179
1180
1181
1182int 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
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
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
1209unlock:
1210 spin_unlock(&sync_lock);
1211 return ret;
1212}
1213
1214int omap_gem_init_object(struct drm_gem_object *obj)
1215{
1216 return -EINVAL;
1217}
1218
1219
1220
1221
1222void 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
1238
1239
1240 WARN_ON(omap_obj->paddr_cnt > 0);
1241
1242
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
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
1266int 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);
1280 return ret;
1281 }
1282
1283
1284 drm_gem_object_unreference_unlocked(obj);
1285
1286 return 0;
1287}
1288
1289
1290struct 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
1306
1307
1308 flags &= ~OMAP_BO_SCANOUT;
1309
1310
1311
1312
1313 flags &= ~(OMAP_BO_CACHED|OMAP_BO_UNCACHED);
1314 flags |= OMAP_BO_WC;
1315
1316
1317 tiler_align(gem2fmt(flags),
1318 &gsize.tiled.width, &gsize.tiled.height);
1319
1320
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
1339
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
1367fail:
1368 if (obj) {
1369 omap_gem_free_object(obj);
1370 }
1371 return NULL;
1372}
1373
1374
1375void 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
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
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
1400
1401
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
1430void omap_gem_deinit(struct drm_device *dev)
1431{
1432
1433
1434
1435 kfree(usergart);
1436}
1437