Lines Matching full:shmem

30  * This library provides helpers for GEM objects backed by shmem buffers
54 struct drm_gem_shmem_object *shmem; in __drm_gem_shmem_create() local
64 shmem = to_drm_gem_shmem_obj(obj); in __drm_gem_shmem_create()
66 shmem = kzalloc(sizeof(*shmem), GFP_KERNEL); in __drm_gem_shmem_create()
67 if (!shmem) in __drm_gem_shmem_create()
69 obj = &shmem->base; in __drm_gem_shmem_create()
77 shmem->map_wc = false; /* dma-buf mappings use always writecombine */ in __drm_gem_shmem_create()
90 INIT_LIST_HEAD(&shmem->madv_list); in __drm_gem_shmem_create()
104 return shmem; in __drm_gem_shmem_create()
118 * This function creates a shmem GEM object.
131 * drm_gem_shmem_free - Free resources associated with a shmem GEM object
132 * @shmem: shmem GEM object to free
137 void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem) in drm_gem_shmem_free() argument
139 struct drm_gem_object *obj = &shmem->base; in drm_gem_shmem_free()
142 drm_prime_gem_destroy(obj, shmem->sgt); in drm_gem_shmem_free()
144 dma_resv_lock(shmem->base.resv, NULL); in drm_gem_shmem_free()
146 drm_WARN_ON(obj->dev, shmem->vmap_use_count); in drm_gem_shmem_free()
148 if (shmem->sgt) { in drm_gem_shmem_free()
149 dma_unmap_sgtable(obj->dev->dev, shmem->sgt, in drm_gem_shmem_free()
151 sg_free_table(shmem->sgt); in drm_gem_shmem_free()
152 kfree(shmem->sgt); in drm_gem_shmem_free()
154 if (shmem->pages) in drm_gem_shmem_free()
155 drm_gem_shmem_put_pages(shmem); in drm_gem_shmem_free()
157 drm_WARN_ON(obj->dev, shmem->pages_use_count); in drm_gem_shmem_free()
159 dma_resv_unlock(shmem->base.resv); in drm_gem_shmem_free()
163 kfree(shmem); in drm_gem_shmem_free()
167 static int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem) in drm_gem_shmem_get_pages() argument
169 struct drm_gem_object *obj = &shmem->base; in drm_gem_shmem_get_pages()
172 dma_resv_assert_held(shmem->base.resv); in drm_gem_shmem_get_pages()
174 if (shmem->pages_use_count++ > 0) in drm_gem_shmem_get_pages()
181 shmem->pages_use_count = 0; in drm_gem_shmem_get_pages()
191 if (shmem->map_wc) in drm_gem_shmem_get_pages()
195 shmem->pages = pages; in drm_gem_shmem_get_pages()
201 * drm_gem_shmem_put_pages - Decrease use count on the backing pages for a shmem GEM object
202 * @shmem: shmem GEM object
206 void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem) in drm_gem_shmem_put_pages() argument
208 struct drm_gem_object *obj = &shmem->base; in drm_gem_shmem_put_pages()
210 dma_resv_assert_held(shmem->base.resv); in drm_gem_shmem_put_pages()
212 if (drm_WARN_ON_ONCE(obj->dev, !shmem->pages_use_count)) in drm_gem_shmem_put_pages()
215 if (--shmem->pages_use_count > 0) in drm_gem_shmem_put_pages()
219 if (shmem->map_wc) in drm_gem_shmem_put_pages()
220 set_pages_array_wb(shmem->pages, obj->size >> PAGE_SHIFT); in drm_gem_shmem_put_pages()
223 drm_gem_put_pages(obj, shmem->pages, in drm_gem_shmem_put_pages()
224 shmem->pages_mark_dirty_on_put, in drm_gem_shmem_put_pages()
225 shmem->pages_mark_accessed_on_put); in drm_gem_shmem_put_pages()
226 shmem->pages = NULL; in drm_gem_shmem_put_pages()
230 int drm_gem_shmem_pin_locked(struct drm_gem_shmem_object *shmem) in drm_gem_shmem_pin_locked() argument
234 dma_resv_assert_held(shmem->base.resv); in drm_gem_shmem_pin_locked()
236 drm_WARN_ON(shmem->base.dev, shmem->base.import_attach); in drm_gem_shmem_pin_locked()
238 ret = drm_gem_shmem_get_pages(shmem); in drm_gem_shmem_pin_locked()
244 void drm_gem_shmem_unpin_locked(struct drm_gem_shmem_object *shmem) in drm_gem_shmem_unpin_locked() argument
246 dma_resv_assert_held(shmem->base.resv); in drm_gem_shmem_unpin_locked()
248 drm_gem_shmem_put_pages(shmem); in drm_gem_shmem_unpin_locked()
253 * drm_gem_shmem_pin - Pin backing pages for a shmem GEM object
254 * @shmem: shmem GEM object
262 int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem) in drm_gem_shmem_pin() argument
264 struct drm_gem_object *obj = &shmem->base; in drm_gem_shmem_pin()
269 ret = dma_resv_lock_interruptible(shmem->base.resv, NULL); in drm_gem_shmem_pin()
272 ret = drm_gem_shmem_pin_locked(shmem); in drm_gem_shmem_pin()
273 dma_resv_unlock(shmem->base.resv); in drm_gem_shmem_pin()
280 * drm_gem_shmem_unpin - Unpin backing pages for a shmem GEM object
281 * @shmem: shmem GEM object
286 void drm_gem_shmem_unpin(struct drm_gem_shmem_object *shmem) in drm_gem_shmem_unpin() argument
288 struct drm_gem_object *obj = &shmem->base; in drm_gem_shmem_unpin()
292 dma_resv_lock(shmem->base.resv, NULL); in drm_gem_shmem_unpin()
293 drm_gem_shmem_unpin_locked(shmem); in drm_gem_shmem_unpin()
294 dma_resv_unlock(shmem->base.resv); in drm_gem_shmem_unpin()
299 * drm_gem_shmem_vmap - Create a virtual mapping for a shmem GEM object
300 * @shmem: shmem GEM object
301 * @map: Returns the kernel virtual address of the SHMEM GEM object's backing
305 * exists for the buffer backing the shmem GEM object. It hides the differences
313 int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem, in drm_gem_shmem_vmap() argument
316 struct drm_gem_object *obj = &shmem->base; in drm_gem_shmem_vmap()
330 dma_resv_assert_held(shmem->base.resv); in drm_gem_shmem_vmap()
332 if (shmem->vmap_use_count++ > 0) { in drm_gem_shmem_vmap()
333 iosys_map_set_vaddr(map, shmem->vaddr); in drm_gem_shmem_vmap()
337 ret = drm_gem_shmem_get_pages(shmem); in drm_gem_shmem_vmap()
341 if (shmem->map_wc) in drm_gem_shmem_vmap()
343 shmem->vaddr = vmap(shmem->pages, obj->size >> PAGE_SHIFT, in drm_gem_shmem_vmap()
345 if (!shmem->vaddr) in drm_gem_shmem_vmap()
348 iosys_map_set_vaddr(map, shmem->vaddr); in drm_gem_shmem_vmap()
360 drm_gem_shmem_put_pages(shmem); in drm_gem_shmem_vmap()
362 shmem->vmap_use_count = 0; in drm_gem_shmem_vmap()
369 * drm_gem_shmem_vunmap - Unmap a virtual mapping for a shmem GEM object
370 * @shmem: shmem GEM object
371 * @map: Kernel virtual address where the SHMEM GEM object was mapped
380 void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem, in drm_gem_shmem_vunmap() argument
383 struct drm_gem_object *obj = &shmem->base; in drm_gem_shmem_vunmap()
388 dma_resv_assert_held(shmem->base.resv); in drm_gem_shmem_vunmap()
390 if (drm_WARN_ON_ONCE(obj->dev, !shmem->vmap_use_count)) in drm_gem_shmem_vunmap()
393 if (--shmem->vmap_use_count > 0) in drm_gem_shmem_vunmap()
396 vunmap(shmem->vaddr); in drm_gem_shmem_vunmap()
397 drm_gem_shmem_put_pages(shmem); in drm_gem_shmem_vunmap()
400 shmem->vaddr = NULL; in drm_gem_shmem_vunmap()
409 struct drm_gem_shmem_object *shmem; in drm_gem_shmem_create_with_handle() local
412 shmem = drm_gem_shmem_create(dev, size); in drm_gem_shmem_create_with_handle()
413 if (IS_ERR(shmem)) in drm_gem_shmem_create_with_handle()
414 return PTR_ERR(shmem); in drm_gem_shmem_create_with_handle()
420 ret = drm_gem_handle_create(file_priv, &shmem->base, handle); in drm_gem_shmem_create_with_handle()
422 drm_gem_object_put(&shmem->base); in drm_gem_shmem_create_with_handle()
430 int drm_gem_shmem_madvise(struct drm_gem_shmem_object *shmem, int madv) in drm_gem_shmem_madvise() argument
432 dma_resv_assert_held(shmem->base.resv); in drm_gem_shmem_madvise()
434 if (shmem->madv >= 0) in drm_gem_shmem_madvise()
435 shmem->madv = madv; in drm_gem_shmem_madvise()
437 madv = shmem->madv; in drm_gem_shmem_madvise()
443 void drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem) in drm_gem_shmem_purge() argument
445 struct drm_gem_object *obj = &shmem->base; in drm_gem_shmem_purge()
448 dma_resv_assert_held(shmem->base.resv); in drm_gem_shmem_purge()
450 drm_WARN_ON(obj->dev, !drm_gem_shmem_is_purgeable(shmem)); in drm_gem_shmem_purge()
452 dma_unmap_sgtable(dev->dev, shmem->sgt, DMA_BIDIRECTIONAL, 0); in drm_gem_shmem_purge()
453 sg_free_table(shmem->sgt); in drm_gem_shmem_purge()
454 kfree(shmem->sgt); in drm_gem_shmem_purge()
455 shmem->sgt = NULL; in drm_gem_shmem_purge()
457 drm_gem_shmem_put_pages(shmem); in drm_gem_shmem_purge()
459 shmem->madv = -1; in drm_gem_shmem_purge()
476 * drm_gem_shmem_dumb_create - Create a dumb shmem buffer object
516 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); in drm_gem_shmem_fault() local
525 dma_resv_lock(shmem->base.resv, NULL); in drm_gem_shmem_fault()
528 drm_WARN_ON_ONCE(obj->dev, !shmem->pages) || in drm_gem_shmem_fault()
529 shmem->madv < 0) { in drm_gem_shmem_fault()
532 page = shmem->pages[page_offset]; in drm_gem_shmem_fault()
537 dma_resv_unlock(shmem->base.resv); in drm_gem_shmem_fault()
545 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); in drm_gem_shmem_vm_open() local
549 dma_resv_lock(shmem->base.resv, NULL); in drm_gem_shmem_vm_open()
556 if (!drm_WARN_ON_ONCE(obj->dev, !shmem->pages_use_count)) in drm_gem_shmem_vm_open()
557 shmem->pages_use_count++; in drm_gem_shmem_vm_open()
559 dma_resv_unlock(shmem->base.resv); in drm_gem_shmem_vm_open()
567 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); in drm_gem_shmem_vm_close() local
569 dma_resv_lock(shmem->base.resv, NULL); in drm_gem_shmem_vm_close()
570 drm_gem_shmem_put_pages(shmem); in drm_gem_shmem_vm_close()
571 dma_resv_unlock(shmem->base.resv); in drm_gem_shmem_vm_close()
584 * drm_gem_shmem_mmap - Memory-map a shmem GEM object
585 * @shmem: shmem GEM object
589 * operation for shmem objects.
594 int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma) in drm_gem_shmem_mmap() argument
596 struct drm_gem_object *obj = &shmem->base; in drm_gem_shmem_mmap()
619 dma_resv_lock(shmem->base.resv, NULL); in drm_gem_shmem_mmap()
620 ret = drm_gem_shmem_get_pages(shmem); in drm_gem_shmem_mmap()
621 dma_resv_unlock(shmem->base.resv); in drm_gem_shmem_mmap()
628 if (shmem->map_wc) in drm_gem_shmem_mmap()
637 * @shmem: shmem GEM object
641 void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem, in drm_gem_shmem_print_info() argument
644 if (shmem->base.import_attach) in drm_gem_shmem_print_info()
647 drm_printf_indent(p, indent, "pages_use_count=%u\n", shmem->pages_use_count); in drm_gem_shmem_print_info()
648 drm_printf_indent(p, indent, "vmap_use_count=%u\n", shmem->vmap_use_count); in drm_gem_shmem_print_info()
649 drm_printf_indent(p, indent, "vaddr=%p\n", shmem->vaddr); in drm_gem_shmem_print_info()
655 * pages for a shmem GEM object
656 * @shmem: shmem GEM object
667 struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem) in drm_gem_shmem_get_sg_table() argument
669 struct drm_gem_object *obj = &shmem->base; in drm_gem_shmem_get_sg_table()
673 return drm_prime_pages_to_sg(obj->dev, shmem->pages, obj->size >> PAGE_SHIFT); in drm_gem_shmem_get_sg_table()
677 static struct sg_table *drm_gem_shmem_get_pages_sgt_locked(struct drm_gem_shmem_object *shmem) in drm_gem_shmem_get_pages_sgt_locked() argument
679 struct drm_gem_object *obj = &shmem->base; in drm_gem_shmem_get_pages_sgt_locked()
683 if (shmem->sgt) in drm_gem_shmem_get_pages_sgt_locked()
684 return shmem->sgt; in drm_gem_shmem_get_pages_sgt_locked()
688 ret = drm_gem_shmem_get_pages(shmem); in drm_gem_shmem_get_pages_sgt_locked()
692 sgt = drm_gem_shmem_get_sg_table(shmem); in drm_gem_shmem_get_pages_sgt_locked()
702 shmem->sgt = sgt; in drm_gem_shmem_get_pages_sgt_locked()
710 drm_gem_shmem_put_pages(shmem); in drm_gem_shmem_get_pages_sgt_locked()
716 * scatter/gather table for a shmem GEM object.
717 * @shmem: shmem GEM object
730 struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem) in drm_gem_shmem_get_pages_sgt() argument
735 ret = dma_resv_lock_interruptible(shmem->base.resv, NULL); in drm_gem_shmem_get_pages_sgt()
738 sgt = drm_gem_shmem_get_pages_sgt_locked(shmem); in drm_gem_shmem_get_pages_sgt()
739 dma_resv_unlock(shmem->base.resv); in drm_gem_shmem_get_pages_sgt()
746 * drm_gem_shmem_prime_import_sg_table - Produce a shmem GEM object from
753 * another driver. Drivers that use the shmem helpers should set this as their
766 struct drm_gem_shmem_object *shmem; in drm_gem_shmem_prime_import_sg_table() local
768 shmem = __drm_gem_shmem_create(dev, size, true); in drm_gem_shmem_prime_import_sg_table()
769 if (IS_ERR(shmem)) in drm_gem_shmem_prime_import_sg_table()
770 return ERR_CAST(shmem); in drm_gem_shmem_prime_import_sg_table()
772 shmem->sgt = sgt; in drm_gem_shmem_prime_import_sg_table()
776 return &shmem->base; in drm_gem_shmem_prime_import_sg_table()
780 MODULE_DESCRIPTION("DRM SHMEM memory-management helpers");