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
45 struct iosys_map;
46 struct 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 */
63 enum 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 */
72 struct 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 */
246 struct 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 */
280 struct 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:
402 *
403 * Provides the list of GPU VAs attached to this GEM object.
404 *
405 * Drivers should lock list accesses with the GEMs &dma_resv lock
406 * (&drm_gem_object.resv) or a custom lock if one is provided.
407 */
408 struct {
409 struct list_head list;
410
411 #ifdef CONFIG_LOCKDEP
412 struct lockdep_map *lock_dep_map;
413 #endif
414 } gpuva;
415
416 /**
417 * @funcs:
418 *
419 * Optional GEM object functions. If this is set, it will be used instead of the
420 * corresponding &drm_driver GEM callbacks.
421 *
422 * New drivers should use this.
423 *
424 */
425 const struct drm_gem_object_funcs *funcs;
426
427 /**
428 * @lru_node:
429 *
430 * List node in a &drm_gem_lru.
431 */
432 struct list_head lru_node;
433
434 /**
435 * @lru:
436 *
437 * The current LRU list that the GEM object is on.
438 */
439 struct drm_gem_lru *lru;
440 };
441
442 /**
443 * DRM_GEM_FOPS - Default drm GEM file operations
444 *
445 * This macro provides a shorthand for setting the GEM file ops in the
446 * &file_operations structure. If all you need are the default ops, use
447 * DEFINE_DRM_GEM_FOPS instead.
448 */
449 #define DRM_GEM_FOPS \
450 .open = drm_open,\
451 .release = drm_release,\
452 .unlocked_ioctl = drm_ioctl,\
453 .compat_ioctl = drm_compat_ioctl,\
454 .poll = drm_poll,\
455 .read = drm_read,\
456 .llseek = noop_llseek,\
457 .mmap = drm_gem_mmap, \
458 .fop_flags = FOP_UNSIGNED_OFFSET
459
460 /**
461 * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
462 * @name: name for the generated structure
463 *
464 * This macro autogenerates a suitable &struct file_operations for GEM based
465 * drivers, which can be assigned to &drm_driver.fops. Note that this structure
466 * cannot be shared between drivers, because it contains a reference to the
467 * current module using THIS_MODULE.
468 *
469 * Note that the declaration is already marked as static - if you need a
470 * non-static version of this you're probably doing it wrong and will break the
471 * THIS_MODULE reference by accident.
472 */
473 #define DEFINE_DRM_GEM_FOPS(name) \
474 static const struct file_operations name = {\
475 .owner = THIS_MODULE,\
476 DRM_GEM_FOPS,\
477 }
478
479 void drm_gem_object_release(struct drm_gem_object *obj);
480 void drm_gem_object_free(struct kref *kref);
481 int drm_gem_object_init(struct drm_device *dev,
482 struct drm_gem_object *obj, size_t size);
483 int drm_gem_object_init_with_mnt(struct drm_device *dev,
484 struct drm_gem_object *obj, size_t size,
485 struct vfsmount *gemfs);
486 void drm_gem_private_object_init(struct drm_device *dev,
487 struct drm_gem_object *obj, size_t size);
488 void drm_gem_private_object_fini(struct drm_gem_object *obj);
489 void drm_gem_vm_open(struct vm_area_struct *vma);
490 void drm_gem_vm_close(struct vm_area_struct *vma);
491 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
492 struct vm_area_struct *vma);
493 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
494
495 /**
496 * drm_gem_object_get - acquire a GEM buffer object reference
497 * @obj: GEM buffer object
498 *
499 * This function acquires an additional reference to @obj. It is illegal to
500 * call this without already holding a reference. No locks required.
501 */
drm_gem_object_get(struct drm_gem_object * obj)502 static inline void drm_gem_object_get(struct drm_gem_object *obj)
503 {
504 kref_get(&obj->refcount);
505 }
506
507 __attribute__((nonnull))
508 static inline void
__drm_gem_object_put(struct drm_gem_object * obj)509 __drm_gem_object_put(struct drm_gem_object *obj)
510 {
511 kref_put(&obj->refcount, drm_gem_object_free);
512 }
513
514 /**
515 * drm_gem_object_put - drop a GEM buffer object reference
516 * @obj: GEM buffer object
517 *
518 * This releases a reference to @obj.
519 */
520 static inline void
drm_gem_object_put(struct drm_gem_object * obj)521 drm_gem_object_put(struct drm_gem_object *obj)
522 {
523 if (obj)
524 __drm_gem_object_put(obj);
525 }
526
527 int drm_gem_handle_create(struct drm_file *file_priv,
528 struct drm_gem_object *obj,
529 u32 *handlep);
530 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
531
532
533 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
534 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
535 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
536
537 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
538 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
539 bool dirty, bool accessed);
540
541 void drm_gem_lock(struct drm_gem_object *obj);
542 void drm_gem_unlock(struct drm_gem_object *obj);
543
544 int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map);
545 void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map);
546
547 int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
548 int count, struct drm_gem_object ***objs_out);
549 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
550 long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
551 bool wait_all, unsigned long timeout);
552 int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
553 struct ww_acquire_ctx *acquire_ctx);
554 void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
555 struct ww_acquire_ctx *acquire_ctx);
556 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
557 u32 handle, u64 *offset);
558
559 void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);
560 void drm_gem_lru_remove(struct drm_gem_object *obj);
561 void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj);
562 void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);
563 unsigned long
564 drm_gem_lru_scan(struct drm_gem_lru *lru,
565 unsigned int nr_to_scan,
566 unsigned long *remaining,
567 bool (*shrink)(struct drm_gem_object *obj, struct ww_acquire_ctx *ticket),
568 struct ww_acquire_ctx *ticket);
569
570 int drm_gem_evict_locked(struct drm_gem_object *obj);
571
572 /**
573 * drm_gem_object_is_shared_for_memory_stats - helper for shared memory stats
574 *
575 * This helper should only be used for fdinfo shared memory stats to determine
576 * if a GEM object is shared.
577 *
578 * @obj: obj in question
579 */
drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object * obj)580 static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object *obj)
581 {
582 return (obj->handle_count > 1) || obj->dma_buf;
583 }
584
585 /**
586 * drm_gem_is_imported() - Tests if GEM object's buffer has been imported
587 * @obj: the GEM object
588 *
589 * Returns:
590 * True if the GEM object's buffer has been imported, false otherwise
591 */
drm_gem_is_imported(const struct drm_gem_object * obj)592 static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
593 {
594 return !!obj->import_attach;
595 }
596
597 #ifdef CONFIG_LOCKDEP
598 /**
599 * drm_gem_gpuva_set_lock() - Set the lock protecting accesses to the gpuva list.
600 * @obj: the &drm_gem_object
601 * @lock: the lock used to protect the gpuva list. The locking primitive
602 * must contain a dep_map field.
603 *
604 * Call this if you're not proctecting access to the gpuva list with the
605 * dma-resv lock, but with a custom lock.
606 */
607 #define drm_gem_gpuva_set_lock(obj, lock) \
608 if (!WARN((obj)->gpuva.lock_dep_map, \
609 "GEM GPUVA lock should be set only once.")) \
610 (obj)->gpuva.lock_dep_map = &(lock)->dep_map
611 #define drm_gem_gpuva_assert_lock_held(obj) \
612 lockdep_assert((obj)->gpuva.lock_dep_map ? \
613 lock_is_held((obj)->gpuva.lock_dep_map) : \
614 dma_resv_held((obj)->resv))
615 #else
616 #define drm_gem_gpuva_set_lock(obj, lock) do {} while (0)
617 #define drm_gem_gpuva_assert_lock_held(obj) do {} while (0)
618 #endif
619
620 /**
621 * drm_gem_gpuva_init() - initialize the gpuva list of a GEM object
622 * @obj: the &drm_gem_object
623 *
624 * This initializes the &drm_gem_object's &drm_gpuvm_bo list.
625 *
626 * Calling this function is only necessary for drivers intending to support the
627 * &drm_driver_feature DRIVER_GEM_GPUVA.
628 *
629 * See also drm_gem_gpuva_set_lock().
630 */
drm_gem_gpuva_init(struct drm_gem_object * obj)631 static inline void drm_gem_gpuva_init(struct drm_gem_object *obj)
632 {
633 INIT_LIST_HEAD(&obj->gpuva.list);
634 }
635
636 /**
637 * drm_gem_for_each_gpuvm_bo() - iterator to walk over a list of &drm_gpuvm_bo
638 * @entry__: &drm_gpuvm_bo structure to assign to in each iteration step
639 * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
640 *
641 * This iterator walks over all &drm_gpuvm_bo structures associated with the
642 * &drm_gem_object.
643 */
644 #define drm_gem_for_each_gpuvm_bo(entry__, obj__) \
645 list_for_each_entry(entry__, &(obj__)->gpuva.list, list.entry.gem)
646
647 /**
648 * drm_gem_for_each_gpuvm_bo_safe() - iterator to safely walk over a list of
649 * &drm_gpuvm_bo
650 * @entry__: &drm_gpuvm_bostructure to assign to in each iteration step
651 * @next__: &next &drm_gpuvm_bo to store the next step
652 * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
653 *
654 * This iterator walks over all &drm_gpuvm_bo structures associated with the
655 * &drm_gem_object. It is implemented with list_for_each_entry_safe(), hence
656 * it is save against removal of elements.
657 */
658 #define drm_gem_for_each_gpuvm_bo_safe(entry__, next__, obj__) \
659 list_for_each_entry_safe(entry__, next__, &(obj__)->gpuva.list, list.entry.gem)
660
661 #endif /* __DRM_GEM_H__ */
662