2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/spinlock.h>
19 #include <linux/shmem_fs.h>
26 /* called with dev->struct_mutex held */
27 static struct page
**get_pages(struct drm_gem_object
*obj
)
29 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
31 if (!msm_obj
->pages
) {
32 struct drm_device
*dev
= obj
->dev
;
33 struct page
**p
= drm_gem_get_pages(obj
, 0);
34 int npages
= obj
->size
>> PAGE_SHIFT
;
37 dev_err(dev
->dev
, "could not get pages: %ld\n",
42 msm_obj
->sgt
= drm_prime_pages_to_sg(p
, npages
);
43 if (IS_ERR(msm_obj
->sgt
)) {
44 dev_err(dev
->dev
, "failed to allocate sgt\n");
45 return ERR_CAST(msm_obj
->sgt
);
50 /* For non-cached buffers, ensure the new pages are clean
51 * because display controller, GPU, etc. are not coherent:
53 if (msm_obj
->flags
& (MSM_BO_WC
|MSM_BO_UNCACHED
))
54 dma_map_sg(dev
->dev
, msm_obj
->sgt
->sgl
,
55 msm_obj
->sgt
->nents
, DMA_BIDIRECTIONAL
);
58 return msm_obj
->pages
;
61 static void put_pages(struct drm_gem_object
*obj
)
63 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
66 /* For non-cached buffers, ensure the new pages are clean
67 * because display controller, GPU, etc. are not coherent:
69 if (msm_obj
->flags
& (MSM_BO_WC
|MSM_BO_UNCACHED
))
70 dma_unmap_sg(obj
->dev
->dev
, msm_obj
->sgt
->sgl
,
71 msm_obj
->sgt
->nents
, DMA_BIDIRECTIONAL
);
72 sg_free_table(msm_obj
->sgt
);
75 drm_gem_put_pages(obj
, msm_obj
->pages
, true, false);
76 msm_obj
->pages
= NULL
;
80 int msm_gem_mmap_obj(struct drm_gem_object
*obj
,
81 struct vm_area_struct
*vma
)
83 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
85 vma
->vm_flags
&= ~VM_PFNMAP
;
86 vma
->vm_flags
|= VM_MIXEDMAP
;
88 if (msm_obj
->flags
& MSM_BO_WC
) {
89 vma
->vm_page_prot
= pgprot_writecombine(vm_get_page_prot(vma
->vm_flags
));
90 } else if (msm_obj
->flags
& MSM_BO_UNCACHED
) {
91 vma
->vm_page_prot
= pgprot_noncached(vm_get_page_prot(vma
->vm_flags
));
94 * Shunt off cached objs to shmem file so they have their own
95 * address_space (so unmap_mapping_range does what we want,
96 * in particular in the case of mmap'd dmabufs)
101 vma
->vm_file
= obj
->filp
;
103 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
109 int msm_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
113 ret
= drm_gem_mmap(filp
, vma
);
115 DBG("mmap failed: %d", ret
);
119 return msm_gem_mmap_obj(vma
->vm_private_data
, vma
);
122 int msm_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
124 struct drm_gem_object
*obj
= vma
->vm_private_data
;
125 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
126 struct drm_device
*dev
= obj
->dev
;
132 /* Make sure we don't parallel update on a fault, nor move or remove
133 * something from beneath our feet
135 ret
= mutex_lock_interruptible(&dev
->struct_mutex
);
139 /* make sure we have pages attached now */
140 pages
= get_pages(obj
);
142 ret
= PTR_ERR(pages
);
146 /* We don't use vmf->pgoff since that has the fake offset: */
147 pgoff
= ((unsigned long)vmf
->virtual_address
-
148 vma
->vm_start
) >> PAGE_SHIFT
;
150 pfn
= page_to_pfn(msm_obj
->pages
[pgoff
]);
152 VERB("Inserting %p pfn %lx, pa %lx", vmf
->virtual_address
,
153 pfn
, pfn
<< PAGE_SHIFT
);
155 ret
= vm_insert_mixed(vma
, (unsigned long)vmf
->virtual_address
, pfn
);
158 mutex_unlock(&dev
->struct_mutex
);
165 return VM_FAULT_NOPAGE
;
169 return VM_FAULT_SIGBUS
;
173 /** get mmap offset */
174 static uint64_t mmap_offset(struct drm_gem_object
*obj
)
176 struct drm_device
*dev
= obj
->dev
;
179 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
181 /* Make it mmapable */
182 ret
= drm_gem_create_mmap_offset(obj
);
185 dev_err(dev
->dev
, "could not allocate mmap offset\n");
189 return drm_vma_node_offset_addr(&obj
->vma_node
);
192 uint64_t msm_gem_mmap_offset(struct drm_gem_object
*obj
)
195 mutex_lock(&obj
->dev
->struct_mutex
);
196 offset
= mmap_offset(obj
);
197 mutex_unlock(&obj
->dev
->struct_mutex
);
201 /* helpers for dealing w/ iommu: */
202 static int map_range(struct iommu_domain
*domain
, unsigned int iova
,
203 struct sg_table
*sgt
, unsigned int len
, int prot
)
205 struct scatterlist
*sg
;
206 unsigned int da
= iova
;
213 for_each_sg(sgt
->sgl
, sg
, sgt
->nents
, i
) {
214 u32 pa
= sg_phys(sg
) - sg
->offset
;
215 size_t bytes
= sg
->length
+ sg
->offset
;
217 VERB("map[%d]: %08x %08x(%x)", i
, iova
, pa
, bytes
);
219 ret
= iommu_map(domain
, da
, pa
, bytes
, prot
);
231 for_each_sg(sgt
->sgl
, sg
, i
, j
) {
232 size_t bytes
= sg
->length
+ sg
->offset
;
233 iommu_unmap(domain
, da
, bytes
);
239 static void unmap_range(struct iommu_domain
*domain
, unsigned int iova
,
240 struct sg_table
*sgt
, unsigned int len
)
242 struct scatterlist
*sg
;
243 unsigned int da
= iova
;
246 for_each_sg(sgt
->sgl
, sg
, sgt
->nents
, i
) {
247 size_t bytes
= sg
->length
+ sg
->offset
;
250 unmapped
= iommu_unmap(domain
, da
, bytes
);
251 if (unmapped
< bytes
)
254 VERB("unmap[%d]: %08x(%x)", i
, iova
, bytes
);
256 BUG_ON(!IS_ALIGNED(bytes
, PAGE_SIZE
));
262 /* should be called under struct_mutex.. although it can be called
263 * from atomic context without struct_mutex to acquire an extra
264 * iova ref if you know one is already held.
266 * That means when I do eventually need to add support for unpinning
267 * the refcnt counter needs to be atomic_t.
269 int msm_gem_get_iova_locked(struct drm_gem_object
*obj
, int id
,
272 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
275 if (!msm_obj
->domain
[id
].iova
) {
276 struct msm_drm_private
*priv
= obj
->dev
->dev_private
;
277 uint32_t offset
= (uint32_t)mmap_offset(obj
);
279 pages
= get_pages(obj
);
281 return PTR_ERR(pages
);
282 // XXX ideally we would not map buffers writable when not needed...
283 ret
= map_range(priv
->iommus
[id
], offset
, msm_obj
->sgt
,
284 obj
->size
, IOMMU_READ
| IOMMU_WRITE
);
285 msm_obj
->domain
[id
].iova
= offset
;
289 *iova
= msm_obj
->domain
[id
].iova
;
294 int msm_gem_get_iova(struct drm_gem_object
*obj
, int id
, uint32_t *iova
)
297 mutex_lock(&obj
->dev
->struct_mutex
);
298 ret
= msm_gem_get_iova_locked(obj
, id
, iova
);
299 mutex_unlock(&obj
->dev
->struct_mutex
);
303 void msm_gem_put_iova(struct drm_gem_object
*obj
, int id
)
306 // NOTE: probably don't need a _locked() version.. we wouldn't
307 // normally unmap here, but instead just mark that it could be
308 // unmapped (if the iova refcnt drops to zero), but then later
309 // if another _get_iova_locked() fails we can start unmapping
310 // things that are no longer needed..
313 int msm_gem_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
314 struct drm_mode_create_dumb
*args
)
316 args
->pitch
= align_pitch(args
->width
, args
->bpp
);
317 args
->size
= PAGE_ALIGN(args
->pitch
* args
->height
);
318 return msm_gem_new_handle(dev
, file
, args
->size
,
319 MSM_BO_SCANOUT
| MSM_BO_WC
, &args
->handle
);
322 int msm_gem_dumb_map_offset(struct drm_file
*file
, struct drm_device
*dev
,
323 uint32_t handle
, uint64_t *offset
)
325 struct drm_gem_object
*obj
;
328 /* GEM does all our handle to object mapping */
329 obj
= drm_gem_object_lookup(dev
, file
, handle
);
335 *offset
= msm_gem_mmap_offset(obj
);
337 drm_gem_object_unreference_unlocked(obj
);
343 void *msm_gem_vaddr_locked(struct drm_gem_object
*obj
)
345 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
346 WARN_ON(!mutex_is_locked(&obj
->dev
->struct_mutex
));
347 if (!msm_obj
->vaddr
) {
348 struct page
**pages
= get_pages(obj
);
350 return ERR_CAST(pages
);
351 msm_obj
->vaddr
= vmap(pages
, obj
->size
>> PAGE_SHIFT
,
352 VM_MAP
, pgprot_writecombine(PAGE_KERNEL
));
354 return msm_obj
->vaddr
;
357 void *msm_gem_vaddr(struct drm_gem_object
*obj
)
360 mutex_lock(&obj
->dev
->struct_mutex
);
361 ret
= msm_gem_vaddr_locked(obj
);
362 mutex_unlock(&obj
->dev
->struct_mutex
);
366 int msm_gem_queue_inactive_work(struct drm_gem_object
*obj
,
367 struct work_struct
*work
)
369 struct drm_device
*dev
= obj
->dev
;
370 struct msm_drm_private
*priv
= dev
->dev_private
;
371 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
374 mutex_lock(&dev
->struct_mutex
);
375 if (!list_empty(&work
->entry
)) {
377 } else if (is_active(msm_obj
)) {
378 list_add_tail(&work
->entry
, &msm_obj
->inactive_work
);
380 queue_work(priv
->wq
, work
);
382 mutex_unlock(&dev
->struct_mutex
);
387 void msm_gem_move_to_active(struct drm_gem_object
*obj
,
388 struct msm_gpu
*gpu
, bool write
, uint32_t fence
)
390 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
393 msm_obj
->write_fence
= fence
;
395 msm_obj
->read_fence
= fence
;
396 list_del_init(&msm_obj
->mm_list
);
397 list_add_tail(&msm_obj
->mm_list
, &gpu
->active_list
);
400 void msm_gem_move_to_inactive(struct drm_gem_object
*obj
)
402 struct drm_device
*dev
= obj
->dev
;
403 struct msm_drm_private
*priv
= dev
->dev_private
;
404 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
406 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
409 msm_obj
->read_fence
= 0;
410 msm_obj
->write_fence
= 0;
411 list_del_init(&msm_obj
->mm_list
);
412 list_add_tail(&msm_obj
->mm_list
, &priv
->inactive_list
);
414 while (!list_empty(&msm_obj
->inactive_work
)) {
415 struct work_struct
*work
;
417 work
= list_first_entry(&msm_obj
->inactive_work
,
418 struct work_struct
, entry
);
420 list_del_init(&work
->entry
);
421 queue_work(priv
->wq
, work
);
425 int msm_gem_cpu_prep(struct drm_gem_object
*obj
, uint32_t op
,
426 struct timespec
*timeout
)
428 struct drm_device
*dev
= obj
->dev
;
429 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
432 if (is_active(msm_obj
)) {
435 if (op
& MSM_PREP_READ
)
436 fence
= msm_obj
->write_fence
;
437 if (op
& MSM_PREP_WRITE
)
438 fence
= max(fence
, msm_obj
->read_fence
);
439 if (op
& MSM_PREP_NOSYNC
)
442 ret
= msm_wait_fence_interruptable(dev
, fence
, timeout
);
445 /* TODO cache maintenance */
450 int msm_gem_cpu_fini(struct drm_gem_object
*obj
)
452 /* TODO cache maintenance */
456 #ifdef CONFIG_DEBUG_FS
457 void msm_gem_describe(struct drm_gem_object
*obj
, struct seq_file
*m
)
459 struct drm_device
*dev
= obj
->dev
;
460 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
461 uint64_t off
= drm_vma_node_start(&obj
->vma_node
);
463 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
464 seq_printf(m
, "%08x: %c(r=%u,w=%u) %2d (%2d) %08llx %p %d\n",
465 msm_obj
->flags
, is_active(msm_obj
) ? 'A' : 'I',
466 msm_obj
->read_fence
, msm_obj
->write_fence
,
467 obj
->name
, obj
->refcount
.refcount
.counter
,
468 off
, msm_obj
->vaddr
, obj
->size
);
471 void msm_gem_describe_objects(struct list_head
*list
, struct seq_file
*m
)
473 struct msm_gem_object
*msm_obj
;
477 list_for_each_entry(msm_obj
, list
, mm_list
) {
478 struct drm_gem_object
*obj
= &msm_obj
->base
;
480 msm_gem_describe(obj
, m
);
485 seq_printf(m
, "Total %d objects, %zu bytes\n", count
, size
);
489 void msm_gem_free_object(struct drm_gem_object
*obj
)
491 struct drm_device
*dev
= obj
->dev
;
492 struct msm_gem_object
*msm_obj
= to_msm_bo(obj
);
495 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
497 /* object should not be on active list: */
498 WARN_ON(is_active(msm_obj
));
500 list_del(&msm_obj
->mm_list
);
502 for (id
= 0; id
< ARRAY_SIZE(msm_obj
->domain
); id
++) {
503 if (msm_obj
->domain
[id
].iova
) {
504 struct msm_drm_private
*priv
= obj
->dev
->dev_private
;
505 uint32_t offset
= (uint32_t)mmap_offset(obj
);
506 unmap_range(priv
->iommus
[id
], offset
,
507 msm_obj
->sgt
, obj
->size
);
511 drm_gem_free_mmap_offset(obj
);
514 vunmap(msm_obj
->vaddr
);
518 if (msm_obj
->resv
== &msm_obj
->_resv
)
519 reservation_object_fini(msm_obj
->resv
);
521 drm_gem_object_release(obj
);
526 /* convenience method to construct a GEM buffer object, and userspace handle */
527 int msm_gem_new_handle(struct drm_device
*dev
, struct drm_file
*file
,
528 uint32_t size
, uint32_t flags
, uint32_t *handle
)
530 struct drm_gem_object
*obj
;
533 ret
= mutex_lock_interruptible(&dev
->struct_mutex
);
537 obj
= msm_gem_new(dev
, size
, flags
);
539 mutex_unlock(&dev
->struct_mutex
);
544 ret
= drm_gem_handle_create(file
, obj
, handle
);
546 /* drop reference from allocate - handle holds it now */
547 drm_gem_object_unreference_unlocked(obj
);
552 struct drm_gem_object
*msm_gem_new(struct drm_device
*dev
,
553 uint32_t size
, uint32_t flags
)
555 struct msm_drm_private
*priv
= dev
->dev_private
;
556 struct msm_gem_object
*msm_obj
;
557 struct drm_gem_object
*obj
= NULL
;
560 WARN_ON(!mutex_is_locked(&dev
->struct_mutex
));
562 size
= PAGE_ALIGN(size
);
564 switch (flags
& MSM_BO_CACHE_MASK
) {
565 case MSM_BO_UNCACHED
:
570 dev_err(dev
->dev
, "invalid cache flag: %x\n",
571 (flags
& MSM_BO_CACHE_MASK
));
576 msm_obj
= kzalloc(sizeof(*msm_obj
), GFP_KERNEL
);
582 obj
= &msm_obj
->base
;
584 ret
= drm_gem_object_init(dev
, obj
, size
);
588 msm_obj
->flags
= flags
;
590 msm_obj
->resv
= &msm_obj
->_resv
;
591 reservation_object_init(msm_obj
->resv
);
593 INIT_LIST_HEAD(&msm_obj
->submit_entry
);
594 INIT_LIST_HEAD(&msm_obj
->inactive_work
);
595 list_add_tail(&msm_obj
->mm_list
, &priv
->inactive_list
);
601 drm_gem_object_unreference_unlocked(obj
);