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 * @pages_mark_dirty_on_put:
50 * Mark pages as dirty when they are put.
52 unsigned int pages_mark_dirty_on_put : 1;
55 * @pages_mark_accessed_on_put:
57 * Mark pages as accessed when they are put.
59 unsigned int pages_mark_accessed_on_put : 1;
62 * @sgt: Scatter/gather table for imported PRIME buffers
67 * @vmap_lock: Protects the vmap address and use count
69 struct mutex vmap_lock;
72 * @vaddr: Kernel virtual address of the backing memory
79 * Reference count on the virtual address.
80 * The address are un-mapped when the count reaches zero.
82 unsigned int vmap_use_count;
85 #define to_drm_gem_shmem_obj(obj) \
86 container_of(obj, struct drm_gem_shmem_object, base)
89 * DEFINE_DRM_GEM_SHMEM_FOPS() - Macro to generate file operations for shmem drivers
90 * @name: name for the generated structure
92 * This macro autogenerates a suitable &struct file_operations for shmem based
93 * drivers, which can be assigned to &drm_driver.fops. Note that this structure
94 * cannot be shared between drivers, because it contains a reference to the
95 * current module using THIS_MODULE.
97 * Note that the declaration is already marked as static - if you need a
98 * non-static version of this you're probably doing it wrong and will break the
99 * THIS_MODULE reference by accident.
101 #define DEFINE_DRM_GEM_SHMEM_FOPS(name) \
102 static const struct file_operations name = {\
103 .owner = THIS_MODULE,\
105 .release = drm_release,\
106 .unlocked_ioctl = drm_ioctl,\
107 .compat_ioctl = drm_compat_ioctl,\
110 .llseek = noop_llseek,\
111 .mmap = drm_gem_shmem_mmap, \
114 struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size);
115 void drm_gem_shmem_free_object(struct drm_gem_object *obj);
117 int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem);
118 void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem);
119 int drm_gem_shmem_pin(struct drm_gem_object *obj);
120 void drm_gem_shmem_unpin(struct drm_gem_object *obj);
121 void *drm_gem_shmem_vmap(struct drm_gem_object *obj);
122 void drm_gem_shmem_vunmap(struct drm_gem_object *obj, void *vaddr);
124 struct drm_gem_shmem_object *
125 drm_gem_shmem_create_with_handle(struct drm_file *file_priv,
126 struct drm_device *dev, size_t size,
128 int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
129 struct drm_mode_create_dumb *args);
131 int drm_gem_shmem_mmap(struct file *filp, struct vm_area_struct *vma);
133 extern const struct vm_operations_struct drm_gem_shmem_vm_ops;
135 void drm_gem_shmem_print_info(struct drm_printer *p, unsigned int indent,
136 const struct drm_gem_object *obj);
138 struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_object *obj);
139 struct drm_gem_object *
140 drm_gem_shmem_prime_import_sg_table(struct drm_device *dev,
141 struct dma_buf_attachment *attach,
142 struct sg_table *sgt);
144 struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_object *obj);
147 * DRM_GEM_SHMEM_DRIVER_OPS - Default shmem GEM operations
149 * This macro provides a shortcut for setting the shmem GEM operations in
150 * the &drm_driver structure.
152 #define DRM_GEM_SHMEM_DRIVER_OPS \
153 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
154 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
155 .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, \
156 .gem_prime_mmap = drm_gem_prime_mmap, \
157 .dumb_create = drm_gem_shmem_dumb_create
159 #endif /* __DRM_GEM_SHMEM_HELPER_H__ */