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