sysctl: add helper to register a sysctl mount point
[linux-block.git] / include / drm / drm_gem_shmem_helper.h
CommitLineData
2194a63a
NT
1/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef __DRM_GEM_SHMEM_HELPER_H__
4#define __DRM_GEM_SHMEM_HELPER_H__
5
6#include <linux/fs.h>
7#include <linux/mm.h>
8#include <linux/mutex.h>
9
10#include <drm/drm_file.h>
11#include <drm/drm_gem.h>
12#include <drm/drm_ioctl.h>
13#include <drm/drm_prime.h>
14
15struct dma_buf_attachment;
16struct drm_mode_create_dumb;
17struct drm_printer;
18struct sg_table;
19
20/**
21 * struct drm_gem_shmem_object - GEM object backed by shmem
22 */
23struct drm_gem_shmem_object {
24 /**
25 * @base: Base GEM object
26 */
27 struct drm_gem_object base;
28
29 /**
30 * @pages_lock: Protects the page table and use count
31 */
32 struct mutex pages_lock;
33
34 /**
35 * @pages: Page table
36 */
37 struct page **pages;
38
39 /**
40 * @pages_use_count:
41 *
42 * Reference count on the pages table.
43 * The pages are put when the count reaches zero.
44 */
45 unsigned int pages_use_count;
46
105401b6
RH
47 /**
48 * @madv: State for madvise
49 *
50 * 0 is active/inuse.
51 * A negative value is the object is purged.
52 * Positive values are driver specific and not used by the helpers.
53 */
17acb9f3 54 int madv;
105401b6
RH
55
56 /**
57 * @madv_list: List entry for madvise tracking
58 *
59 * Typically used by drivers to track purgeable objects
60 */
17acb9f3
RH
61 struct list_head madv_list;
62
2194a63a
NT
63 /**
64 * @pages_mark_dirty_on_put:
65 *
66 * Mark pages as dirty when they are put.
67 */
68 unsigned int pages_mark_dirty_on_put : 1;
69
70 /**
71 * @pages_mark_accessed_on_put:
72 *
73 * Mark pages as accessed when they are put.
74 */
75 unsigned int pages_mark_accessed_on_put : 1;
76
77 /**
78 * @sgt: Scatter/gather table for imported PRIME buffers
79 */
80 struct sg_table *sgt;
81
82 /**
83 * @vmap_lock: Protects the vmap address and use count
84 */
85 struct mutex vmap_lock;
86
87 /**
88 * @vaddr: Kernel virtual address of the backing memory
89 */
90 void *vaddr;
91
92 /**
93 * @vmap_use_count:
94 *
95 * Reference count on the virtual address.
96 * The address are un-mapped when the count reaches zero.
97 */
98 unsigned int vmap_use_count;
1cad6292
GH
99
100 /**
0cf2ef46 101 * @map_wc: map object write-combined (instead of using shmem defaults).
1cad6292 102 */
0cf2ef46 103 bool map_wc;
2194a63a
NT
104};
105
106#define to_drm_gem_shmem_obj(obj) \
107 container_of(obj, struct drm_gem_shmem_object, base)
108
2194a63a 109struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size);
a193f3b4 110void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem);
2194a63a
NT
111
112int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem);
113void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem);
a193f3b4
TZ
114int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem);
115void drm_gem_shmem_unpin(struct drm_gem_shmem_object *shmem);
116int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem, struct dma_buf_map *map);
117void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem, struct dma_buf_map *map);
118int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma);
2194a63a 119
a193f3b4 120int drm_gem_shmem_madvise(struct drm_gem_shmem_object *shmem, int madv);
17acb9f3
RH
121
122static inline bool drm_gem_shmem_is_purgeable(struct drm_gem_shmem_object *shmem)
123{
124 return (shmem->madv > 0) &&
125 !shmem->vmap_use_count && shmem->sgt &&
126 !shmem->base.dma_buf && !shmem->base.import_attach;
127}
128
a193f3b4
TZ
129void drm_gem_shmem_purge_locked(struct drm_gem_shmem_object *shmem);
130bool drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem);
17acb9f3 131
a193f3b4
TZ
132struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem);
133struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem);
2194a63a 134
a193f3b4
TZ
135void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem,
136 struct drm_printer *p, unsigned int indent);
c7fbcb71
TZ
137
138/*
139 * GEM object functions
140 */
141
142/**
a193f3b4 143 * drm_gem_shmem_object_free - GEM object function for drm_gem_shmem_free()
c7fbcb71
TZ
144 * @obj: GEM object to free
145 *
a193f3b4 146 * This function wraps drm_gem_shmem_free(). Drivers that employ the shmem helpers
c7fbcb71
TZ
147 * should use it as their &drm_gem_object_funcs.free handler.
148 */
149static inline void drm_gem_shmem_object_free(struct drm_gem_object *obj)
150{
a193f3b4
TZ
151 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
152
153 drm_gem_shmem_free(shmem);
c7fbcb71
TZ
154}
155
156/**
157 * drm_gem_shmem_object_print_info() - Print &drm_gem_shmem_object info for debugfs
158 * @p: DRM printer
159 * @indent: Tab indentation level
160 * @obj: GEM object
161 *
162 * This function wraps drm_gem_shmem_print_info(). Drivers that employ the shmem helpers should
163 * use this function as their &drm_gem_object_funcs.print_info handler.
164 */
165static inline void drm_gem_shmem_object_print_info(struct drm_printer *p, unsigned int indent,
166 const struct drm_gem_object *obj)
167{
a193f3b4
TZ
168 const struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
169
170 drm_gem_shmem_print_info(shmem, p, indent);
c7fbcb71
TZ
171}
172
173/**
174 * drm_gem_shmem_object_pin - GEM object function for drm_gem_shmem_pin()
175 * @obj: GEM object
176 *
177 * This function wraps drm_gem_shmem_pin(). Drivers that employ the shmem helpers should
178 * use it as their &drm_gem_object_funcs.pin handler.
179 */
180static inline int drm_gem_shmem_object_pin(struct drm_gem_object *obj)
181{
a193f3b4
TZ
182 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
183
184 return drm_gem_shmem_pin(shmem);
c7fbcb71
TZ
185}
186
187/**
188 * drm_gem_shmem_object_unpin - GEM object function for drm_gem_shmem_unpin()
189 * @obj: GEM object
190 *
191 * This function wraps drm_gem_shmem_unpin(). Drivers that employ the shmem helpers should
192 * use it as their &drm_gem_object_funcs.unpin handler.
193 */
194static inline void drm_gem_shmem_object_unpin(struct drm_gem_object *obj)
195{
a193f3b4
TZ
196 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
197
198 drm_gem_shmem_unpin(shmem);
c7fbcb71
TZ
199}
200
201/**
202 * drm_gem_shmem_object_get_sg_table - GEM object function for drm_gem_shmem_get_sg_table()
203 * @obj: GEM object
204 *
205 * This function wraps drm_gem_shmem_get_sg_table(). Drivers that employ the shmem helpers should
206 * use it as their &drm_gem_object_funcs.get_sg_table handler.
207 *
208 * Returns:
209 * A pointer to the scatter/gather table of pinned pages or NULL on failure.
210 */
211static inline struct sg_table *drm_gem_shmem_object_get_sg_table(struct drm_gem_object *obj)
212{
a193f3b4
TZ
213 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
214
215 return drm_gem_shmem_get_sg_table(shmem);
c7fbcb71
TZ
216}
217
218/*
219 * drm_gem_shmem_object_vmap - GEM object function for drm_gem_shmem_vmap()
220 * @obj: GEM object
221 * @map: Returns the kernel virtual address of the SHMEM GEM object's backing store.
222 *
223 * This function wraps drm_gem_shmem_vmap(). Drivers that employ the shmem helpers should
224 * use it as their &drm_gem_object_funcs.vmap handler.
225 *
226 * Returns:
227 * 0 on success or a negative error code on failure.
228 */
229static inline int drm_gem_shmem_object_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
230{
a193f3b4
TZ
231 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
232
233 return drm_gem_shmem_vmap(shmem, map);
c7fbcb71
TZ
234}
235
236/*
237 * drm_gem_shmem_object_vunmap - GEM object function for drm_gem_shmem_vunmap()
238 * @obj: GEM object
239 * @map: Kernel virtual address where the SHMEM GEM object was mapped
240 *
241 * This function wraps drm_gem_shmem_vunmap(). Drivers that employ the shmem helpers should
242 * use it as their &drm_gem_object_funcs.vunmap handler.
243 */
244static inline void drm_gem_shmem_object_vunmap(struct drm_gem_object *obj, struct dma_buf_map *map)
245{
a193f3b4
TZ
246 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
247
248 drm_gem_shmem_vunmap(shmem, map);
c7fbcb71
TZ
249}
250
251/**
252 * drm_gem_shmem_object_mmap - GEM object function for drm_gem_shmem_mmap()
253 * @obj: GEM object
254 * @vma: VMA for the area to be mapped
255 *
256 * This function wraps drm_gem_shmem_mmap(). Drivers that employ the shmem helpers should
257 * use it as their &drm_gem_object_funcs.mmap handler.
258 *
259 * Returns:
260 * 0 on success or a negative error code on failure.
261 */
262static inline int drm_gem_shmem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
263{
a193f3b4
TZ
264 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
265
266 return drm_gem_shmem_mmap(shmem, vma);
c7fbcb71
TZ
267}
268
269/*
270 * Driver ops
271 */
272
2194a63a
NT
273struct drm_gem_object *
274drm_gem_shmem_prime_import_sg_table(struct drm_device *dev,
275 struct dma_buf_attachment *attach,
276 struct sg_table *sgt);
a193f3b4
TZ
277int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
278 struct drm_mode_create_dumb *args);
2194a63a
NT
279
280/**
281 * DRM_GEM_SHMEM_DRIVER_OPS - Default shmem GEM operations
282 *
283 * This macro provides a shortcut for setting the shmem GEM operations in
284 * the &drm_driver structure.
285 */
286#define DRM_GEM_SHMEM_DRIVER_OPS \
287 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
288 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
289 .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, \
290 .gem_prime_mmap = drm_gem_prime_mmap, \
291 .dumb_create = drm_gem_shmem_dumb_create
292
293#endif /* __DRM_GEM_SHMEM_HELPER_H__ */