File Index
Files and directories
Sign in
FILE

drm_gem.h

File preview with syntax highlighting.

linux-6.19.14/include/drm/drm_gem.h C
  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-buf.h>
 39#include <linux/dma-resv.h>
 40#include <linux/list.h>
 41#include <linux/mutex.h>
 42
 43#include <drm/drm_vma_manager.h>
 44
 45struct iosys_map;
 46struct drm_gem_object;
 47
 48/**
 49 * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
 50 * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
 51 * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
 52 * @DRM_GEM_OBJECT_ACTIVE: object is currently used by an active submission
 53 *
 54 * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
 55 * and drm_show_fdinfo().  Note that an object can report DRM_GEM_OBJECT_PURGEABLE
 56 * and be active or not resident, in which case drm_show_fdinfo() will not
 57 * account for it as purgeable.  So drivers do not need to check if the buffer
 58 * is idle and resident to return this bit, i.e. userspace can mark a buffer as
 59 * purgeable even while it is still busy on the GPU. It will not get reported in
 60 * the puregeable stats until it becomes idle.  The status gem object func does
 61 * not need to consider this.
 62 */
 63enum drm_gem_object_status {
 64	DRM_GEM_OBJECT_RESIDENT  = BIT(0),
 65	DRM_GEM_OBJECT_PURGEABLE = BIT(1),
 66	DRM_GEM_OBJECT_ACTIVE    = BIT(2),
 67};
 68
 69/**
 70 * struct drm_gem_object_funcs - GEM object functions
 71 */
 72struct drm_gem_object_funcs {
 73	/**
 74	 * @free:
 75	 *
 76	 * Deconstructor for drm_gem_objects.
 77	 *
 78	 * This callback is mandatory.
 79	 */
 80	void (*free)(struct drm_gem_object *obj);
 81
 82	/**
 83	 * @open:
 84	 *
 85	 * Called upon GEM handle creation.
 86	 *
 87	 * This callback is optional.
 88	 */
 89	int (*open)(struct drm_gem_object *obj, struct drm_file *file);
 90
 91	/**
 92	 * @close:
 93	 *
 94	 * Called upon GEM handle release.
 95	 *
 96	 * This callback is optional.
 97	 */
 98	void (*close)(struct drm_gem_object *obj, struct drm_file *file);
 99
100	/**
101	 * @print_info:
102	 *
103	 * If driver subclasses struct &drm_gem_object, it can implement this
104	 * optional hook for printing additional driver specific info.
105	 *
106	 * drm_printf_indent() should be used in the callback passing it the
107	 * indent argument.
108	 *
109	 * This callback is called from drm_gem_print_info().
110	 *
111	 * This callback is optional.
112	 */
113	void (*print_info)(struct drm_printer *p, unsigned int indent,
114			   const struct drm_gem_object *obj);
115
116	/**
117	 * @export:
118	 *
119	 * Export backing buffer as a &dma_buf.
120	 * If this is not set drm_gem_prime_export() is used.
121	 *
122	 * This callback is optional.
123	 */
124	struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);
125
126	/**
127	 * @pin:
128	 *
129	 * Pin backing buffer in memory, such that dma-buf importers can
130	 * access it. Used by the drm_gem_map_attach() helper.
131	 *
132	 * This callback is optional.
133	 */
134	int (*pin)(struct drm_gem_object *obj);
135
136	/**
137	 * @unpin:
138	 *
139	 * Unpin backing buffer. Used by the drm_gem_map_detach() helper.
140	 *
141	 * This callback is optional.
142	 */
143	void (*unpin)(struct drm_gem_object *obj);
144
145	/**
146	 * @get_sg_table:
147	 *
148	 * Returns a Scatter-Gather table representation of the buffer.
149	 * Used when exporting a buffer by the drm_gem_map_dma_buf() helper.
150	 * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()
151	 * in drm_gem_unmap_buf(), therefore these helpers and this callback
152	 * here cannot be used for sg tables pointing at driver private memory
153	 * ranges.
154	 *
155	 * See also drm_prime_pages_to_sg().
156	 */
157	struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);
158
159	/**
160	 * @vmap:
161	 *
162	 * Returns a virtual address for the buffer. Used by the
163	 * drm_gem_dmabuf_vmap() helper. Called with a held GEM reservation
164	 * lock.
165	 *
166	 * This callback is optional.
167	 */
168	int (*vmap)(struct drm_gem_object *obj, struct iosys_map *map);
169
170	/**
171	 * @vunmap:
172	 *
173	 * Releases the address previously returned by @vmap. Used by the
174	 * drm_gem_dmabuf_vunmap() helper. Called with a held GEM reservation
175	 * lock.
176	 *
177	 * This callback is optional.
178	 */
179	void (*vunmap)(struct drm_gem_object *obj, struct iosys_map *map);
180
181	/**
182	 * @mmap:
183	 *
184	 * Handle mmap() of the gem object, setup vma accordingly.
185	 *
186	 * This callback is optional.
187	 *
188	 * The callback is used by both drm_gem_mmap_obj() and
189	 * drm_gem_prime_mmap().  When @mmap is present @vm_ops is not
190	 * used, the @mmap callback must set vma->vm_ops instead.
191	 */
192	int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
193
194	/**
195	 * @evict:
196	 *
197	 * Evicts gem object out from memory. Used by the drm_gem_object_evict()
198	 * helper. Returns 0 on success, -errno otherwise. Called with a held
199	 * GEM reservation lock.
200	 *
201	 * This callback is optional.
202	 */
203	int (*evict)(struct drm_gem_object *obj);
204
205	/**
206	 * @status:
207	 *
208	 * The optional status callback can return additional object state
209	 * which determines which stats the object is counted against.  The
210	 * callback is called under table_lock.  Racing against object status
211	 * change is "harmless", and the callback can expect to not race
212	 * against object destruction.
213	 *
214	 * Called by drm_show_memory_stats().
215	 */
216	enum drm_gem_object_status (*status)(struct drm_gem_object *obj);
217
218	/**
219	 * @rss:
220	 *
221	 * Return resident size of the object in physical memory.
222	 *
223	 * Called by drm_show_memory_stats().
224	 */
225	size_t (*rss)(struct drm_gem_object *obj);
226
227	/**
228	 * @vm_ops:
229	 *
230	 * Virtual memory operations used with mmap.
231	 *
232	 * This is optional but necessary for mmap support.
233	 */
234	const struct vm_operations_struct *vm_ops;
235};
236
237/**
238 * struct drm_gem_lru - A simple LRU helper
239 *
240 * A helper for tracking GEM objects in a given state, to aid in
241 * driver's shrinker implementation.  Tracks the count of pages
242 * for lockless &shrinker.count_objects, and provides
243 * &drm_gem_lru_scan for driver's &shrinker.scan_objects
244 * implementation.
245 */
246struct drm_gem_lru {
247	/**
248	 * @lock:
249	 *
250	 * Lock protecting movement of GEM objects between LRUs.  All
251	 * LRUs that the object can move between should be protected
252	 * by the same lock.
253	 */
254	struct mutex *lock;
255
256	/**
257	 * @count:
258	 *
259	 * The total number of backing pages of the GEM objects in
260	 * this LRU.
261	 */
262	long count;
263
264	/**
265	 * @list:
266	 *
267	 * The LRU list.
268	 */
269	struct list_head list;
270};
271
272/**
273 * struct drm_gem_object - GEM buffer object
274 *
275 * This structure defines the generic parts for GEM buffer objects, which are
276 * mostly around handling mmap and userspace handles.
277 *
278 * Buffer objects are often abbreviated to BO.
279 */
280struct drm_gem_object {
281	/**
282	 * @refcount:
283	 *
284	 * Reference count of this object
285	 *
286	 * Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked()
287	 * or drm_gem_object_put() to release a reference to a GEM
288	 * buffer object.
289	 */
290	struct kref refcount;
291
292	/**
293	 * @handle_count:
294	 *
295	 * This is the GEM file_priv handle count of this object.
296	 *
297	 * Each handle also holds a reference. Note that when the handle_count
298	 * drops to 0 any global names (e.g. the id in the flink namespace) will
299	 * be cleared.
300	 *
301	 * Protected by &drm_device.object_name_lock.
302	 */
303	unsigned handle_count;
304
305	/**
306	 * @dev: DRM dev this object belongs to.
307	 */
308	struct drm_device *dev;
309
310	/**
311	 * @filp:
312	 *
313	 * SHMEM file node used as backing storage for swappable buffer objects.
314	 * GEM also supports driver private objects with driver-specific backing
315	 * storage (contiguous DMA memory, special reserved blocks). In this
316	 * case @filp is NULL.
317	 */
318	struct file *filp;
319
320	/**
321	 * @vma_node:
322	 *
323	 * Mapping info for this object to support mmap. Drivers are supposed to
324	 * allocate the mmap offset using drm_gem_create_mmap_offset(). The
325	 * offset itself can be retrieved using drm_vma_node_offset_addr().
326	 *
327	 * Memory mapping itself is handled by drm_gem_mmap(), which also checks
328	 * that userspace is allowed to access the object.
329	 */
330	struct drm_vma_offset_node vma_node;
331
332	/**
333	 * @size:
334	 *
335	 * Size of the object, in bytes.  Immutable over the object's
336	 * lifetime.
337	 */
338	size_t size;
339
340	/**
341	 * @name:
342	 *
343	 * Global name for this object, starts at 1. 0 means unnamed.
344	 * Access is covered by &drm_device.object_name_lock. This is used by
345	 * the GEM_FLINK and GEM_OPEN ioctls.
346	 */
347	int name;
348
349	/**
350	 * @dma_buf:
351	 *
352	 * dma-buf associated with this GEM object.
353	 *
354	 * Pointer to the dma-buf associated with this gem object (either
355	 * through importing or exporting). We break the resulting reference
356	 * loop when the last gem handle for this object is released.
357	 *
358	 * Protected by &drm_device.object_name_lock.
359	 */
360	struct dma_buf *dma_buf;
361
362	/**
363	 * @import_attach:
364	 *
365	 * dma-buf attachment backing this object.
366	 *
367	 * Any foreign dma_buf imported as a gem object has this set to the
368	 * attachment point for the device. This is invariant over the lifetime
369	 * of a gem object.
370	 *
371	 * The &drm_gem_object_funcs.free callback is responsible for
372	 * cleaning up the dma_buf attachment and references acquired at import
373	 * time.
374	 *
375	 * Note that the drm gem/prime core does not depend upon drivers setting
376	 * this field any more. So for drivers where this doesn't make sense
377	 * (e.g. virtual devices or a displaylink behind an usb bus) they can
378	 * simply leave it as NULL.
379	 */
380	struct dma_buf_attachment *import_attach;
381
382	/**
383	 * @resv:
384	 *
385	 * Pointer to reservation object associated with the this GEM object.
386	 *
387	 * Normally (@resv == &@_resv) except for imported GEM objects.
388	 */
389	struct dma_resv *resv;
390
391	/**
392	 * @_resv:
393	 *
394	 * A reservation object for this GEM object.
395	 *
396	 * This is unused for imported GEM objects.
397	 */
398	struct dma_resv _resv;
399
400	/**
401	 * @gpuva: Fields used by GPUVM to manage mappings pointing to this GEM object.
402	 *
403	 * When DRM_GPUVM_IMMEDIATE_MODE is set, this list is protected by the
404	 * mutex. Otherwise, the list is protected by the GEMs &dma_resv lock.
405	 *
406	 * Note that all entries in this list must agree on whether
407	 * DRM_GPUVM_IMMEDIATE_MODE is set.
408	 */
409	struct {
410		/**
411		 * @gpuva.list: list of GPUVM mappings attached to this GEM object.
412		 *
413		 * Drivers should lock list accesses with either the GEMs
414		 * &dma_resv lock (&drm_gem_object.resv) or the
415		 * &drm_gem_object.gpuva.lock mutex.
416		 */
417		struct list_head list;
418
419		/**
420		 * @gpuva.lock: lock protecting access to &drm_gem_object.gpuva.list
421		 * when DRM_GPUVM_IMMEDIATE_MODE is used.
422		 *
423		 * Only used when DRM_GPUVM_IMMEDIATE_MODE is set. It should be
424		 * safe to take this mutex during the fence signalling path, so
425		 * do not allocate memory while holding this lock. Otherwise,
426		 * the &dma_resv lock should be used.
427		 */
428		struct mutex lock;
429	} gpuva;
430
431	/**
432	 * @funcs:
433	 *
434	 * Optional GEM object functions. If this is set, it will be used instead of the
435	 * corresponding &drm_driver GEM callbacks.
436	 *
437	 * New drivers should use this.
438	 *
439	 */
440	const struct drm_gem_object_funcs *funcs;
441
442	/**
443	 * @lru_node:
444	 *
445	 * List node in a &drm_gem_lru.
446	 */
447	struct list_head lru_node;
448
449	/**
450	 * @lru:
451	 *
452	 * The current LRU list that the GEM object is on.
453	 */
454	struct drm_gem_lru *lru;
455};
456
457/**
458 * DRM_GEM_FOPS - Default drm GEM file operations
459 *
460 * This macro provides a shorthand for setting the GEM file ops in the
461 * &file_operations structure.  If all you need are the default ops, use
462 * DEFINE_DRM_GEM_FOPS instead.
463 */
464#define DRM_GEM_FOPS \
465	.open		= drm_open,\
466	.release	= drm_release,\
467	.unlocked_ioctl	= drm_ioctl,\
468	.compat_ioctl	= drm_compat_ioctl,\
469	.poll		= drm_poll,\
470	.read		= drm_read,\
471	.llseek		= noop_llseek,\
472	.mmap		= drm_gem_mmap, \
473	.fop_flags	= FOP_UNSIGNED_OFFSET
474
475/**
476 * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
477 * @name: name for the generated structure
478 *
479 * This macro autogenerates a suitable &struct file_operations for GEM based
480 * drivers, which can be assigned to &drm_driver.fops. Note that this structure
481 * cannot be shared between drivers, because it contains a reference to the
482 * current module using THIS_MODULE.
483 *
484 * Note that the declaration is already marked as static - if you need a
485 * non-static version of this you're probably doing it wrong and will break the
486 * THIS_MODULE reference by accident.
487 */
488#define DEFINE_DRM_GEM_FOPS(name) \
489	static const struct file_operations name = {\
490		.owner		= THIS_MODULE,\
491		DRM_GEM_FOPS,\
492	}
493
494void drm_gem_object_release(struct drm_gem_object *obj);
495void drm_gem_object_free(struct kref *kref);
496int drm_gem_object_init(struct drm_device *dev,
497			struct drm_gem_object *obj, size_t size);
498int drm_gem_object_init_with_mnt(struct drm_device *dev,
499				 struct drm_gem_object *obj, size_t size,
500				 struct vfsmount *gemfs);
501void drm_gem_private_object_init(struct drm_device *dev,
502				 struct drm_gem_object *obj, size_t size);
503void drm_gem_private_object_fini(struct drm_gem_object *obj);
504void drm_gem_vm_open(struct vm_area_struct *vma);
505void drm_gem_vm_close(struct vm_area_struct *vma);
506int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
507		     struct vm_area_struct *vma);
508int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
509
510/**
511 * drm_gem_object_get - acquire a GEM buffer object reference
512 * @obj: GEM buffer object
513 *
514 * This function acquires an additional reference to @obj. It is illegal to
515 * call this without already holding a reference. No locks required.
516 */
517static inline void drm_gem_object_get(struct drm_gem_object *obj)
518{
519	kref_get(&obj->refcount);
520}
521
522__attribute__((nonnull))
523static inline void
524__drm_gem_object_put(struct drm_gem_object *obj)
525{
526	kref_put(&obj->refcount, drm_gem_object_free);
527}
528
529/**
530 * drm_gem_object_put - drop a GEM buffer object reference
531 * @obj: GEM buffer object
532 *
533 * This releases a reference to @obj.
534 */
535static inline void
536drm_gem_object_put(struct drm_gem_object *obj)
537{
538	if (obj)
539		__drm_gem_object_put(obj);
540}
541
542int drm_gem_handle_create(struct drm_file *file_priv,
543			  struct drm_gem_object *obj,
544			  u32 *handlep);
545int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
546
547
548void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
549int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
550int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
551
552struct page **drm_gem_get_pages(struct drm_gem_object *obj);
553void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
554		bool dirty, bool accessed);
555
556void drm_gem_lock(struct drm_gem_object *obj);
557void drm_gem_unlock(struct drm_gem_object *obj);
558
559int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map);
560void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map);
561
562int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
563			   int count, struct drm_gem_object ***objs_out);
564struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
565long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
566				    bool wait_all, unsigned long timeout);
567int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
568			      struct ww_acquire_ctx *acquire_ctx);
569void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
570				 struct ww_acquire_ctx *acquire_ctx);
571int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
572			    u32 handle, u64 *offset);
573
574void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);
575void drm_gem_lru_remove(struct drm_gem_object *obj);
576void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj);
577void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);
578unsigned long
579drm_gem_lru_scan(struct drm_gem_lru *lru,
580		 unsigned int nr_to_scan,
581		 unsigned long *remaining,
582		 bool (*shrink)(struct drm_gem_object *obj, struct ww_acquire_ctx *ticket),
583		 struct ww_acquire_ctx *ticket);
584
585int drm_gem_evict_locked(struct drm_gem_object *obj);
586
587/**
588 * drm_gem_object_is_shared_for_memory_stats - helper for shared memory stats
589 *
590 * This helper should only be used for fdinfo shared memory stats to determine
591 * if a GEM object is shared.
592 *
593 * @obj: obj in question
594 */
595static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object *obj)
596{
597	return (obj->handle_count > 1) || obj->dma_buf;
598}
599
600/**
601 * drm_gem_is_imported() - Tests if GEM object's buffer has been imported
602 * @obj: the GEM object
603 *
604 * Returns:
605 * True if the GEM object's buffer has been imported, false otherwise
606 */
607static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
608{
609	return !!obj->import_attach;
610}
611
612#ifdef CONFIG_LOCKDEP
613#define drm_gem_gpuva_assert_lock_held(gpuvm, obj) \
614	lockdep_assert(drm_gpuvm_immediate_mode(gpuvm) ? \
615		       lockdep_is_held(&(obj)->gpuva.lock) : \
616		       dma_resv_held((obj)->resv))
617#else
618#define drm_gem_gpuva_assert_lock_held(gpuvm, obj) do {} while (0)
619#endif
620
621/**
622 * drm_gem_gpuva_init() - initialize the gpuva list of a GEM object
623 * @obj: the &drm_gem_object
624 *
625 * This initializes the &drm_gem_object's &drm_gpuvm_bo list.
626 *
627 * Calling this function is only necessary for drivers intending to support the
628 * &drm_driver_feature DRIVER_GEM_GPUVA.
629 *
630 * See also drm_gem_gpuva_set_lock().
631 */
632static inline void drm_gem_gpuva_init(struct drm_gem_object *obj)
633{
634	INIT_LIST_HEAD(&obj->gpuva.list);
635}
636
637/**
638 * drm_gem_for_each_gpuvm_bo() - iterator to walk over a list of &drm_gpuvm_bo
639 * @entry__: &drm_gpuvm_bo structure to assign to in each iteration step
640 * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
641 *
642 * This iterator walks over all &drm_gpuvm_bo structures associated with the
643 * &drm_gem_object.
644 */
645#define drm_gem_for_each_gpuvm_bo(entry__, obj__) \
646	list_for_each_entry(entry__, &(obj__)->gpuva.list, list.entry.gem)
647
648/**
649 * drm_gem_for_each_gpuvm_bo_safe() - iterator to safely walk over a list of
650 * &drm_gpuvm_bo
651 * @entry__: &drm_gpuvm_bostructure to assign to in each iteration step
652 * @next__: &next &drm_gpuvm_bo to store the next step
653 * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
654 *
655 * This iterator walks over all &drm_gpuvm_bo structures associated with the
656 * &drm_gem_object. It is implemented with list_for_each_entry_safe(), hence
657 * it is save against removal of elements.
658 */
659#define drm_gem_for_each_gpuvm_bo_safe(entry__, next__, obj__) \
660	list_for_each_entry_safe(entry__, next__, &(obj__)->gpuva.list, list.entry.gem)
661
662#endif /* __DRM_GEM_H__ */
~/ Read-only file preview.