1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef __DRM_GEM_SHMEM_HELPER_H__
4 #define __DRM_GEM_SHMEM_HELPER_H__
8 #include <linux/mutex.h>
10 #include <drm/drm_file.h>
11 #include <drm/drm_gem.h>
12 #include <drm/drm_ioctl.h>
13 #include <drm/drm_prime.h>
15 struct dma_buf_attachment
;
16 struct drm_mode_create_dumb
;
21 * struct drm_gem_shmem_object - GEM object backed by shmem
23 struct drm_gem_shmem_object
{
25 * @base: Base GEM object
27 struct drm_gem_object base
;
30 * @pages_lock: Protects the page table and use count
32 struct mutex pages_lock
;
42 * Reference count on the pages table.
43 * The pages are put when the count reaches zero.
45 unsigned int pages_use_count
;
48 * @madv: State for madvise
51 * A negative value is the object is purged.
52 * Positive values are driver specific and not used by the helpers.
57 * @madv_list: List entry for madvise tracking
59 * Typically used by drivers to track purgeable objects
61 struct list_head madv_list
;
64 * @pages_mark_dirty_on_put:
66 * Mark pages as dirty when they are put.
68 unsigned int pages_mark_dirty_on_put
: 1;
71 * @pages_mark_accessed_on_put:
73 * Mark pages as accessed when they are put.
75 unsigned int pages_mark_accessed_on_put
: 1;
78 * @sgt: Scatter/gather table for imported PRIME buffers
83 * @vmap_lock: Protects the vmap address and use count
85 struct mutex vmap_lock
;
88 * @vaddr: Kernel virtual address of the backing memory
95 * Reference count on the virtual address.
96 * The address are un-mapped when the count reaches zero.
98 unsigned int vmap_use_count
;
101 * @map_cached: map object cached (instead of using writecombine).
106 #define to_drm_gem_shmem_obj(obj) \
107 container_of(obj, struct drm_gem_shmem_object, base)
109 struct drm_gem_shmem_object
*drm_gem_shmem_create(struct drm_device
*dev
, size_t size
);
110 void drm_gem_shmem_free_object(struct drm_gem_object
*obj
);
112 int drm_gem_shmem_get_pages(struct drm_gem_shmem_object
*shmem
);
113 void drm_gem_shmem_put_pages(struct drm_gem_shmem_object
*shmem
);
114 int drm_gem_shmem_pin(struct drm_gem_object
*obj
);
115 void drm_gem_shmem_unpin(struct drm_gem_object
*obj
);
116 void *drm_gem_shmem_vmap(struct drm_gem_object
*obj
);
117 void drm_gem_shmem_vunmap(struct drm_gem_object
*obj
, void *vaddr
);
119 int drm_gem_shmem_madvise(struct drm_gem_object
*obj
, int madv
);
121 static inline bool drm_gem_shmem_is_purgeable(struct drm_gem_shmem_object
*shmem
)
123 return (shmem
->madv
> 0) &&
124 !shmem
->vmap_use_count
&& shmem
->sgt
&&
125 !shmem
->base
.dma_buf
&& !shmem
->base
.import_attach
;
128 void drm_gem_shmem_purge_locked(struct drm_gem_object
*obj
);
129 bool drm_gem_shmem_purge(struct drm_gem_object
*obj
);
131 struct drm_gem_shmem_object
*
132 drm_gem_shmem_create_with_handle(struct drm_file
*file_priv
,
133 struct drm_device
*dev
, size_t size
,
135 int drm_gem_shmem_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
136 struct drm_mode_create_dumb
*args
);
138 int drm_gem_shmem_mmap(struct drm_gem_object
*obj
, struct vm_area_struct
*vma
);
140 void drm_gem_shmem_print_info(struct drm_printer
*p
, unsigned int indent
,
141 const struct drm_gem_object
*obj
);
143 struct sg_table
*drm_gem_shmem_get_sg_table(struct drm_gem_object
*obj
);
144 struct drm_gem_object
*
145 drm_gem_shmem_prime_import_sg_table(struct drm_device
*dev
,
146 struct dma_buf_attachment
*attach
,
147 struct sg_table
*sgt
);
149 struct sg_table
*drm_gem_shmem_get_pages_sgt(struct drm_gem_object
*obj
);
152 * DRM_GEM_SHMEM_DRIVER_OPS - Default shmem GEM operations
154 * This macro provides a shortcut for setting the shmem GEM operations in
155 * the &drm_driver structure.
157 #define DRM_GEM_SHMEM_DRIVER_OPS \
158 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
159 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
160 .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, \
161 .gem_prime_mmap = drm_gem_prime_mmap, \
162 .dumb_create = drm_gem_shmem_dumb_create
164 #endif /* __DRM_GEM_SHMEM_HELPER_H__ */