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 * @vm_ops:
214 *
215 * Virtual memory operations used with mmap.
216 *
217 * This is optional but necessary for mmap support.
218 */
219 const struct vm_operations_struct *vm_ops;
220 };
221
222 /**
223 * struct drm_gem_lru - A simple LRU helper
224 *
225 * A helper for tracking GEM objects in a given state, to aid in
226 * driver's shrinker implementation. Tracks the count of pages
227 * for lockless &shrinker.count_objects, and provides
228 * &drm_gem_lru_scan for driver's &shrinker.scan_objects
229 * implementation.
230 */
231 struct drm_gem_lru {
232 /**
233 * @lock:
234 *
235 * Lock protecting movement of GEM objects between LRUs. All
236 * LRUs that the object can move between should be protected
237 * by the same lock.
238 */
239 struct mutex *lock;
240
241 /**
242 * @count:
243 *
244 * The total number of backing pages of the GEM objects in
245 * this LRU.
246 */
247 long count;
248
249 /**
250 * @list:
251 *
252 * The LRU list.
253 */
254 struct list_head list;
255 };
256
257 /**
258 * struct drm_gem_object - GEM buffer object
259 *
260 * This structure defines the generic parts for GEM buffer objects, which are
261 * mostly around handling mmap and userspace handles.
262 *
263 * Buffer objects are often abbreviated to BO.
264 */
265 struct drm_gem_object {
266 /**
267 * @refcount:
268 *
269 * Reference count of this object
270 *
271 * Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked()
272 * or drm_gem_object_put() to release a reference to a GEM
273 * buffer object.
274 */
275 struct kref refcount;
276
277 /**
278 * @handle_count:
279 *
280 * This is the GEM file_priv handle count of this object.
281 *
282 * Each handle also holds a reference. Note that when the handle_count
283 * drops to 0 any global names (e.g. the id in the flink namespace) will
284 * be cleared.
285 *
286 * Protected by &drm_device.object_name_lock.
287 */
288 unsigned handle_count;
289
290 /**
291 * @dev: DRM dev this object belongs to.
292 */
293 struct drm_device *dev;
294
295 /**
296 * @filp:
297 *
298 * SHMEM file node used as backing storage for swappable buffer objects.
299 * GEM also supports driver private objects with driver-specific backing
300 * storage (contiguous DMA memory, special reserved blocks). In this
301 * case @filp is NULL.
302 */
303 struct file *filp;
304
305 /**
306 * @vma_node:
307 *
308 * Mapping info for this object to support mmap. Drivers are supposed to
309 * allocate the mmap offset using drm_gem_create_mmap_offset(). The
310 * offset itself can be retrieved using drm_vma_node_offset_addr().
311 *
312 * Memory mapping itself is handled by drm_gem_mmap(), which also checks
313 * that userspace is allowed to access the object.
314 */
315 struct drm_vma_offset_node vma_node;
316
317 /**
318 * @size:
319 *
320 * Size of the object, in bytes. Immutable over the object's
321 * lifetime.
322 */
323 size_t size;
324
325 /**
326 * @name:
327 *
328 * Global name for this object, starts at 1. 0 means unnamed.
329 * Access is covered by &drm_device.object_name_lock. This is used by
330 * the GEM_FLINK and GEM_OPEN ioctls.
331 */
332 int name;
333
334 /**
335 * @dma_buf:
336 *
337 * dma-buf associated with this GEM object.
338 *
339 * Pointer to the dma-buf associated with this gem object (either
340 * through importing or exporting). We break the resulting reference
341 * loop when the last gem handle for this object is released.
342 *
343 * Protected by &drm_device.object_name_lock.
344 */
345 struct dma_buf *dma_buf;
346
347 /**
348 * @import_attach:
349 *
350 * dma-buf attachment backing this object.
351 *
352 * Any foreign dma_buf imported as a gem object has this set to the
353 * attachment point for the device. This is invariant over the lifetime
354 * of a gem object.
355 *
356 * The &drm_gem_object_funcs.free callback is responsible for
357 * cleaning up the dma_buf attachment and references acquired at import
358 * time.
359 *
360 * Note that the drm gem/prime core does not depend upon drivers setting
361 * this field any more. So for drivers where this doesn't make sense
362 * (e.g. virtual devices or a displaylink behind an usb bus) they can
363 * simply leave it as NULL.
364 */
365 struct dma_buf_attachment *import_attach;
366
367 /**
368 * @resv:
369 *
370 * Pointer to reservation object associated with the this GEM object.
371 *
372 * Normally (@resv == &@_resv) except for imported GEM objects.
373 */
374 struct dma_resv *resv;
375
376 /**
377 * @_resv:
378 *
379 * A reservation object for this GEM object.
380 *
381 * This is unused for imported GEM objects.
382 */
383 struct dma_resv _resv;
384
385 /**
386 * @gpuva:
387 *
388 * Provides the list of GPU VAs attached to this GEM object.
389 *
390 * Drivers should lock list accesses with the GEMs &dma_resv lock
391 * (&drm_gem_object.resv) or a custom lock if one is provided.
392 */
393 struct {
394 struct list_head list;
395
396 #ifdef CONFIG_LOCKDEP
397 struct lockdep_map *lock_dep_map;
398 #endif
399 } gpuva;
400
401 /**
402 * @funcs:
403 *
404 * Optional GEM object functions. If this is set, it will be used instead of the
405 * corresponding &drm_driver GEM callbacks.
406 *
407 * New drivers should use this.
408 *
409 */
410 const struct drm_gem_object_funcs *funcs;
411
412 /**
413 * @lru_node:
414 *
415 * List node in a &drm_gem_lru.
416 */
417 struct list_head lru_node;
418
419 /**
420 * @lru:
421 *
422 * The current LRU list that the GEM object is on.
423 */
424 struct drm_gem_lru *lru;
425 };
426
427 /**
428 * DRM_GEM_FOPS - Default drm GEM file operations
429 *
430 * This macro provides a shorthand for setting the GEM file ops in the
431 * &file_operations structure. If all you need are the default ops, use
432 * DEFINE_DRM_GEM_FOPS instead.
433 */
434 #define DRM_GEM_FOPS \
435 .open = drm_open,\
436 .release = drm_release,\
437 .unlocked_ioctl = drm_ioctl,\
438 .compat_ioctl = drm_compat_ioctl,\
439 .poll = drm_poll,\
440 .read = drm_read,\
441 .llseek = noop_llseek,\
442 .mmap = drm_gem_mmap
443
444 /**
445 * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
446 * @name: name for the generated structure
447 *
448 * This macro autogenerates a suitable &struct file_operations for GEM based
449 * drivers, which can be assigned to &drm_driver.fops. Note that this structure
450 * cannot be shared between drivers, because it contains a reference to the
451 * current module using THIS_MODULE.
452 *
453 * Note that the declaration is already marked as static - if you need a
454 * non-static version of this you're probably doing it wrong and will break the
455 * THIS_MODULE reference by accident.
456 */
457 #define DEFINE_DRM_GEM_FOPS(name) \
458 static const struct file_operations name = {\
459 .owner = THIS_MODULE,\
460 DRM_GEM_FOPS,\
461 }
462
463 void drm_gem_object_release(struct drm_gem_object *obj);
464 void drm_gem_object_free(struct kref *kref);
465 int drm_gem_object_init(struct drm_device *dev,
466 struct drm_gem_object *obj, size_t size);
467 void drm_gem_private_object_init(struct drm_device *dev,
468 struct drm_gem_object *obj, size_t size);
469 void drm_gem_private_object_fini(struct drm_gem_object *obj);
470 void drm_gem_vm_open(struct vm_area_struct *vma);
471 void drm_gem_vm_close(struct vm_area_struct *vma);
472 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
473 struct vm_area_struct *vma);
474 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
475
476 /**
477 * drm_gem_object_get - acquire a GEM buffer object reference
478 * @obj: GEM buffer object
479 *
480 * This function acquires an additional reference to @obj. It is illegal to
481 * call this without already holding a reference. No locks required.
482 */
drm_gem_object_get(struct drm_gem_object * obj)483 static inline void drm_gem_object_get(struct drm_gem_object *obj)
484 {
485 kref_get(&obj->refcount);
486 }
487
488 __attribute__((nonnull))
489 static inline void
__drm_gem_object_put(struct drm_gem_object * obj)490 __drm_gem_object_put(struct drm_gem_object *obj)
491 {
492 kref_put(&obj->refcount, drm_gem_object_free);
493 }
494
495 /**
496 * drm_gem_object_put - drop a GEM buffer object reference
497 * @obj: GEM buffer object
498 *
499 * This releases a reference to @obj.
500 */
501 static inline void
drm_gem_object_put(struct drm_gem_object * obj)502 drm_gem_object_put(struct drm_gem_object *obj)
503 {
504 if (obj)
505 __drm_gem_object_put(obj);
506 }
507
508 int drm_gem_handle_create(struct drm_file *file_priv,
509 struct drm_gem_object *obj,
510 u32 *handlep);
511 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
512
513
514 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
515 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
516 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
517
518 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
519 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
520 bool dirty, bool accessed);
521
522 int drm_gem_vmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map);
523 void drm_gem_vunmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map);
524
525 int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
526 int count, struct drm_gem_object ***objs_out);
527 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
528 long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
529 bool wait_all, unsigned long timeout);
530 int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
531 struct ww_acquire_ctx *acquire_ctx);
532 void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
533 struct ww_acquire_ctx *acquire_ctx);
534 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
535 u32 handle, u64 *offset);
536
537 void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);
538 void drm_gem_lru_remove(struct drm_gem_object *obj);
539 void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj);
540 void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);
541 unsigned long drm_gem_lru_scan(struct drm_gem_lru *lru,
542 unsigned int nr_to_scan,
543 unsigned long *remaining,
544 bool (*shrink)(struct drm_gem_object *obj));
545
546 int drm_gem_evict(struct drm_gem_object *obj);
547
548 /**
549 * drm_gem_object_is_shared_for_memory_stats - helper for shared memory stats
550 *
551 * This helper should only be used for fdinfo shared memory stats to determine
552 * if a GEM object is shared.
553 *
554 * @obj: obj in question
555 */
drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object * obj)556 static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object *obj)
557 {
558 return (obj->handle_count > 1) || obj->dma_buf;
559 }
560
561 /**
562 * drm_gem_is_imported() - Tests if GEM object's buffer has been imported
563 * @obj: the GEM object
564 *
565 * Returns:
566 * True if the GEM object's buffer has been imported, false otherwise
567 */
drm_gem_is_imported(const struct drm_gem_object * obj)568 static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
569 {
570 return !!obj->import_attach;
571 }
572
573 #ifdef CONFIG_LOCKDEP
574 /**
575 * drm_gem_gpuva_set_lock() - Set the lock protecting accesses to the gpuva list.
576 * @obj: the &drm_gem_object
577 * @lock: the lock used to protect the gpuva list. The locking primitive
578 * must contain a dep_map field.
579 *
580 * Call this if you're not proctecting access to the gpuva list with the
581 * dma-resv lock, but with a custom lock.
582 */
583 #define drm_gem_gpuva_set_lock(obj, lock) \
584 if (!WARN((obj)->gpuva.lock_dep_map, \
585 "GEM GPUVA lock should be set only once.")) \
586 (obj)->gpuva.lock_dep_map = &(lock)->dep_map
587 #define drm_gem_gpuva_assert_lock_held(obj) \
588 lockdep_assert((obj)->gpuva.lock_dep_map ? \
589 lock_is_held((obj)->gpuva.lock_dep_map) : \
590 dma_resv_held((obj)->resv))
591 #else
592 #define drm_gem_gpuva_set_lock(obj, lock) do {} while (0)
593 #define drm_gem_gpuva_assert_lock_held(obj) do {} while (0)
594 #endif
595
596 /**
597 * drm_gem_gpuva_init() - initialize the gpuva list of a GEM object
598 * @obj: the &drm_gem_object
599 *
600 * This initializes the &drm_gem_object's &drm_gpuva list.
601 *
602 * Calling this function is only necessary for drivers intending to support the
603 * &drm_driver_feature DRIVER_GEM_GPUVA.
604 *
605 * See also drm_gem_gpuva_set_lock().
606 */
drm_gem_gpuva_init(struct drm_gem_object * obj)607 static inline void drm_gem_gpuva_init(struct drm_gem_object *obj)
608 {
609 INIT_LIST_HEAD(&obj->gpuva.list);
610 }
611
612 /**
613 * drm_gem_for_each_gpuva() - iternator to walk over a list of gpuvas
614 * @entry__: &drm_gpuva structure to assign to in each iteration step
615 * @obj__: the &drm_gem_object the &drm_gpuvas to walk are associated with
616 *
617 * This iterator walks over all &drm_gpuva structures associated with the
618 * &drm_gpuva_manager.
619 */
620 #define drm_gem_for_each_gpuva(entry__, obj__) \
621 list_for_each_entry(entry__, &(obj__)->gpuva.list, gem.entry)
622
623 /**
624 * drm_gem_for_each_gpuva_safe() - iternator to safely walk over a list of
625 * gpuvas
626 * @entry__: &drm_gpuva structure to assign to in each iteration step
627 * @next__: &next &drm_gpuva to store the next step
628 * @obj__: the &drm_gem_object the &drm_gpuvas to walk are associated with
629 *
630 * This iterator walks over all &drm_gpuva structures associated with the
631 * &drm_gem_object. It is implemented with list_for_each_entry_safe(), hence
632 * it is save against removal of elements.
633 */
634 #define drm_gem_for_each_gpuva_safe(entry__, next__, obj__) \
635 list_for_each_entry_safe(entry__, next__, &(obj__)->gpuva.list, gem.entry)
636
637 #endif /* __DRM_GEM_H__ */
638