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