xref: /openbmc/linux/include/drm/drm_gem.h (revision 569820be)
1 #ifndef __DRM_GEM_H__
2 #define __DRM_GEM_H__
3 
4 /*
5  * GEM Graphics Execution Manager Driver Interfaces
6  *
7  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
8  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
9  * Copyright (c) 2009-2010, Code Aurora Forum.
10  * All rights reserved.
11  * Copyright © 2014 Intel Corporation
12  *   Daniel Vetter <daniel.vetter@ffwll.ch>
13  *
14  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
15  * Author: Gareth Hughes <gareth@valinux.com>
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining a
18  * copy of this software and associated documentation files (the "Software"),
19  * to deal in the Software without restriction, including without limitation
20  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21  * and/or sell copies of the Software, and to permit persons to whom the
22  * Software is furnished to do so, subject to the following conditions:
23  *
24  * The above copyright notice and this permission notice (including the next
25  * paragraph) shall be included in all copies or substantial portions of the
26  * Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
31  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  * OTHER DEALINGS IN THE SOFTWARE.
35  */
36 
37 #include <linux/kref.h>
38 #include <linux/dma-resv.h>
39 
40 #include <drm/drm_vma_manager.h>
41 
42 struct iosys_map;
43 struct drm_gem_object;
44 
45 /**
46  * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
47  * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
48  * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
49  *
50  * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
51  * and drm_show_fdinfo().  Note that an object can DRM_GEM_OBJECT_PURGEABLE if
52  * it still active or not resident, in which case drm_show_fdinfo() will not
53  * account for it as purgeable.  So drivers do not need to check if the buffer
54  * is idle and resident to return this bit.  (Ie. userspace can mark a buffer
55  * as purgeable even while it is still busy on the GPU.. it does not _actually_
56  * become puregeable until it becomes idle.  The status gem object func does
57  * not need to consider this.)
58  */
59 enum drm_gem_object_status {
60 	DRM_GEM_OBJECT_RESIDENT  = BIT(0),
61 	DRM_GEM_OBJECT_PURGEABLE = BIT(1),
62 };
63 
64 /**
65  * struct drm_gem_object_funcs - GEM object functions
66  */
67 struct drm_gem_object_funcs {
68 	/**
69 	 * @free:
70 	 *
71 	 * Deconstructor for drm_gem_objects.
72 	 *
73 	 * This callback is mandatory.
74 	 */
75 	void (*free)(struct drm_gem_object *obj);
76 
77 	/**
78 	 * @open:
79 	 *
80 	 * Called upon GEM handle creation.
81 	 *
82 	 * This callback is optional.
83 	 */
84 	int (*open)(struct drm_gem_object *obj, struct drm_file *file);
85 
86 	/**
87 	 * @close:
88 	 *
89 	 * Called upon GEM handle release.
90 	 *
91 	 * This callback is optional.
92 	 */
93 	void (*close)(struct drm_gem_object *obj, struct drm_file *file);
94 
95 	/**
96 	 * @print_info:
97 	 *
98 	 * If driver subclasses struct &drm_gem_object, it can implement this
99 	 * optional hook for printing additional driver specific info.
100 	 *
101 	 * drm_printf_indent() should be used in the callback passing it the
102 	 * indent argument.
103 	 *
104 	 * This callback is called from drm_gem_print_info().
105 	 *
106 	 * This callback is optional.
107 	 */
108 	void (*print_info)(struct drm_printer *p, unsigned int indent,
109 			   const struct drm_gem_object *obj);
110 
111 	/**
112 	 * @export:
113 	 *
114 	 * Export backing buffer as a &dma_buf.
115 	 * If this is not set drm_gem_prime_export() is used.
116 	 *
117 	 * This callback is optional.
118 	 */
119 	struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);
120 
121 	/**
122 	 * @pin:
123 	 *
124 	 * Pin backing buffer in memory. Used by the drm_gem_map_attach() helper.
125 	 *
126 	 * This callback is optional.
127 	 */
128 	int (*pin)(struct drm_gem_object *obj);
129 
130 	/**
131 	 * @unpin:
132 	 *
133 	 * Unpin backing buffer. Used by the drm_gem_map_detach() helper.
134 	 *
135 	 * This callback is optional.
136 	 */
137 	void (*unpin)(struct drm_gem_object *obj);
138 
139 	/**
140 	 * @get_sg_table:
141 	 *
142 	 * Returns a Scatter-Gather table representation of the buffer.
143 	 * Used when exporting a buffer by the drm_gem_map_dma_buf() helper.
144 	 * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()
145 	 * in drm_gem_unmap_buf(), therefore these helpers and this callback
146 	 * here cannot be used for sg tables pointing at driver private memory
147 	 * ranges.
148 	 *
149 	 * See also drm_prime_pages_to_sg().
150 	 */
151 	struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);
152 
153 	/**
154 	 * @vmap:
155 	 *
156 	 * Returns a virtual address for the buffer. Used by the
157 	 * drm_gem_dmabuf_vmap() helper.
158 	 *
159 	 * This callback is optional.
160 	 */
161 	int (*vmap)(struct drm_gem_object *obj, struct iosys_map *map);
162 
163 	/**
164 	 * @vunmap:
165 	 *
166 	 * Releases the address previously returned by @vmap. Used by the
167 	 * drm_gem_dmabuf_vunmap() helper.
168 	 *
169 	 * This callback is optional.
170 	 */
171 	void (*vunmap)(struct drm_gem_object *obj, struct iosys_map *map);
172 
173 	/**
174 	 * @mmap:
175 	 *
176 	 * Handle mmap() of the gem object, setup vma accordingly.
177 	 *
178 	 * This callback is optional.
179 	 *
180 	 * The callback is used by both drm_gem_mmap_obj() and
181 	 * drm_gem_prime_mmap().  When @mmap is present @vm_ops is not
182 	 * used, the @mmap callback must set vma->vm_ops instead.
183 	 */
184 	int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
185 
186 	/**
187 	 * @evict:
188 	 *
189 	 * Evicts gem object out from memory. Used by the drm_gem_object_evict()
190 	 * helper. Returns 0 on success, -errno otherwise.
191 	 *
192 	 * This callback is optional.
193 	 */
194 	int (*evict)(struct drm_gem_object *obj);
195 
196 	/**
197 	 * @status:
198 	 *
199 	 * The optional status callback can return additional object state
200 	 * which determines which stats the object is counted against.  The
201 	 * callback is called under table_lock.  Racing against object status
202 	 * change is "harmless", and the callback can expect to not race
203 	 * against object destruction.
204 	 *
205 	 * Called by drm_show_memory_stats().
206 	 */
207 	enum drm_gem_object_status (*status)(struct drm_gem_object *obj);
208 
209 	/**
210 	 * @vm_ops:
211 	 *
212 	 * Virtual memory operations used with mmap.
213 	 *
214 	 * This is optional but necessary for mmap support.
215 	 */
216 	const struct vm_operations_struct *vm_ops;
217 };
218 
219 /**
220  * struct drm_gem_lru - A simple LRU helper
221  *
222  * A helper for tracking GEM objects in a given state, to aid in
223  * driver's shrinker implementation.  Tracks the count of pages
224  * for lockless &shrinker.count_objects, and provides
225  * &drm_gem_lru_scan for driver's &shrinker.scan_objects
226  * implementation.
227  */
228 struct drm_gem_lru {
229 	/**
230 	 * @lock:
231 	 *
232 	 * Lock protecting movement of GEM objects between LRUs.  All
233 	 * LRUs that the object can move between should be protected
234 	 * by the same lock.
235 	 */
236 	struct mutex *lock;
237 
238 	/**
239 	 * @count:
240 	 *
241 	 * The total number of backing pages of the GEM objects in
242 	 * this LRU.
243 	 */
244 	long count;
245 
246 	/**
247 	 * @list:
248 	 *
249 	 * The LRU list.
250 	 */
251 	struct list_head list;
252 };
253 
254 /**
255  * struct drm_gem_object - GEM buffer object
256  *
257  * This structure defines the generic parts for GEM buffer objects, which are
258  * mostly around handling mmap and userspace handles.
259  *
260  * Buffer objects are often abbreviated to BO.
261  */
262 struct drm_gem_object {
263 	/**
264 	 * @refcount:
265 	 *
266 	 * Reference count of this object
267 	 *
268 	 * Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked()
269 	 * or drm_gem_object_put() to release a reference to a GEM
270 	 * buffer object.
271 	 */
272 	struct kref refcount;
273 
274 	/**
275 	 * @handle_count:
276 	 *
277 	 * This is the GEM file_priv handle count of this object.
278 	 *
279 	 * Each handle also holds a reference. Note that when the handle_count
280 	 * drops to 0 any global names (e.g. the id in the flink namespace) will
281 	 * be cleared.
282 	 *
283 	 * Protected by &drm_device.object_name_lock.
284 	 */
285 	unsigned handle_count;
286 
287 	/**
288 	 * @dev: DRM dev this object belongs to.
289 	 */
290 	struct drm_device *dev;
291 
292 	/**
293 	 * @filp:
294 	 *
295 	 * SHMEM file node used as backing storage for swappable buffer objects.
296 	 * GEM also supports driver private objects with driver-specific backing
297 	 * storage (contiguous DMA memory, special reserved blocks). In this
298 	 * case @filp is NULL.
299 	 */
300 	struct file *filp;
301 
302 	/**
303 	 * @vma_node:
304 	 *
305 	 * Mapping info for this object to support mmap. Drivers are supposed to
306 	 * allocate the mmap offset using drm_gem_create_mmap_offset(). The
307 	 * offset itself can be retrieved using drm_vma_node_offset_addr().
308 	 *
309 	 * Memory mapping itself is handled by drm_gem_mmap(), which also checks
310 	 * that userspace is allowed to access the object.
311 	 */
312 	struct drm_vma_offset_node vma_node;
313 
314 	/**
315 	 * @size:
316 	 *
317 	 * Size of the object, in bytes.  Immutable over the object's
318 	 * lifetime.
319 	 */
320 	size_t size;
321 
322 	/**
323 	 * @name:
324 	 *
325 	 * Global name for this object, starts at 1. 0 means unnamed.
326 	 * Access is covered by &drm_device.object_name_lock. This is used by
327 	 * the GEM_FLINK and GEM_OPEN ioctls.
328 	 */
329 	int name;
330 
331 	/**
332 	 * @dma_buf:
333 	 *
334 	 * dma-buf associated with this GEM object.
335 	 *
336 	 * Pointer to the dma-buf associated with this gem object (either
337 	 * through importing or exporting). We break the resulting reference
338 	 * loop when the last gem handle for this object is released.
339 	 *
340 	 * Protected by &drm_device.object_name_lock.
341 	 */
342 	struct dma_buf *dma_buf;
343 
344 	/**
345 	 * @import_attach:
346 	 *
347 	 * dma-buf attachment backing this object.
348 	 *
349 	 * Any foreign dma_buf imported as a gem object has this set to the
350 	 * attachment point for the device. This is invariant over the lifetime
351 	 * of a gem object.
352 	 *
353 	 * The &drm_gem_object_funcs.free callback is responsible for
354 	 * cleaning up the dma_buf attachment and references acquired at import
355 	 * time.
356 	 *
357 	 * Note that the drm gem/prime core does not depend upon drivers setting
358 	 * this field any more. So for drivers where this doesn't make sense
359 	 * (e.g. virtual devices or a displaylink behind an usb bus) they can
360 	 * simply leave it as NULL.
361 	 */
362 	struct dma_buf_attachment *import_attach;
363 
364 	/**
365 	 * @resv:
366 	 *
367 	 * Pointer to reservation object associated with the this GEM object.
368 	 *
369 	 * Normally (@resv == &@_resv) except for imported GEM objects.
370 	 */
371 	struct dma_resv *resv;
372 
373 	/**
374 	 * @_resv:
375 	 *
376 	 * A reservation object for this GEM object.
377 	 *
378 	 * This is unused for imported GEM objects.
379 	 */
380 	struct dma_resv _resv;
381 
382 	/**
383 	 * @funcs:
384 	 *
385 	 * Optional GEM object functions. If this is set, it will be used instead of the
386 	 * corresponding &drm_driver GEM callbacks.
387 	 *
388 	 * New drivers should use this.
389 	 *
390 	 */
391 	const struct drm_gem_object_funcs *funcs;
392 
393 	/**
394 	 * @lru_node:
395 	 *
396 	 * List node in a &drm_gem_lru.
397 	 */
398 	struct list_head lru_node;
399 
400 	/**
401 	 * @lru:
402 	 *
403 	 * The current LRU list that the GEM object is on.
404 	 */
405 	struct drm_gem_lru *lru;
406 };
407 
408 /**
409  * DRM_GEM_FOPS - Default drm GEM file operations
410  *
411  * This macro provides a shorthand for setting the GEM file ops in the
412  * &file_operations structure.  If all you need are the default ops, use
413  * DEFINE_DRM_GEM_FOPS instead.
414  */
415 #define DRM_GEM_FOPS \
416 	.open		= drm_open,\
417 	.release	= drm_release,\
418 	.unlocked_ioctl	= drm_ioctl,\
419 	.compat_ioctl	= drm_compat_ioctl,\
420 	.poll		= drm_poll,\
421 	.read		= drm_read,\
422 	.llseek		= noop_llseek,\
423 	.mmap		= drm_gem_mmap
424 
425 /**
426  * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
427  * @name: name for the generated structure
428  *
429  * This macro autogenerates a suitable &struct file_operations for GEM based
430  * drivers, which can be assigned to &drm_driver.fops. Note that this structure
431  * cannot be shared between drivers, because it contains a reference to the
432  * current module using THIS_MODULE.
433  *
434  * Note that the declaration is already marked as static - if you need a
435  * non-static version of this you're probably doing it wrong and will break the
436  * THIS_MODULE reference by accident.
437  */
438 #define DEFINE_DRM_GEM_FOPS(name) \
439 	static const struct file_operations name = {\
440 		.owner		= THIS_MODULE,\
441 		DRM_GEM_FOPS,\
442 	}
443 
444 void drm_gem_object_release(struct drm_gem_object *obj);
445 void drm_gem_object_free(struct kref *kref);
446 int drm_gem_object_init(struct drm_device *dev,
447 			struct drm_gem_object *obj, size_t size);
448 void drm_gem_private_object_init(struct drm_device *dev,
449 				 struct drm_gem_object *obj, size_t size);
450 void drm_gem_private_object_fini(struct drm_gem_object *obj);
451 void drm_gem_vm_open(struct vm_area_struct *vma);
452 void drm_gem_vm_close(struct vm_area_struct *vma);
453 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
454 		     struct vm_area_struct *vma);
455 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
456 
457 /**
458  * drm_gem_object_get - acquire a GEM buffer object reference
459  * @obj: GEM buffer object
460  *
461  * This function acquires an additional reference to @obj. It is illegal to
462  * call this without already holding a reference. No locks required.
463  */
464 static inline void drm_gem_object_get(struct drm_gem_object *obj)
465 {
466 	kref_get(&obj->refcount);
467 }
468 
469 __attribute__((nonnull))
470 static inline void
471 __drm_gem_object_put(struct drm_gem_object *obj)
472 {
473 	kref_put(&obj->refcount, drm_gem_object_free);
474 }
475 
476 /**
477  * drm_gem_object_put - drop a GEM buffer object reference
478  * @obj: GEM buffer object
479  *
480  * This releases a reference to @obj.
481  */
482 static inline void
483 drm_gem_object_put(struct drm_gem_object *obj)
484 {
485 	if (obj)
486 		__drm_gem_object_put(obj);
487 }
488 
489 int drm_gem_handle_create(struct drm_file *file_priv,
490 			  struct drm_gem_object *obj,
491 			  u32 *handlep);
492 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
493 
494 
495 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
496 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
497 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
498 
499 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
500 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
501 		bool dirty, bool accessed);
502 
503 int drm_gem_vmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map);
504 void drm_gem_vunmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map);
505 
506 int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
507 			   int count, struct drm_gem_object ***objs_out);
508 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
509 long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
510 				    bool wait_all, unsigned long timeout);
511 int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
512 			      struct ww_acquire_ctx *acquire_ctx);
513 void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
514 				 struct ww_acquire_ctx *acquire_ctx);
515 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
516 			    u32 handle, u64 *offset);
517 
518 void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);
519 void drm_gem_lru_remove(struct drm_gem_object *obj);
520 void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj);
521 void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);
522 unsigned long drm_gem_lru_scan(struct drm_gem_lru *lru,
523 			       unsigned int nr_to_scan,
524 			       unsigned long *remaining,
525 			       bool (*shrink)(struct drm_gem_object *obj));
526 
527 int drm_gem_evict(struct drm_gem_object *obj);
528 
529 #endif /* __DRM_GEM_H__ */
530