xref: /openbmc/linux/include/drm/drm_gem.h (revision 913447d0)
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 
40 #include <drm/drm_vma_manager.h>
41 
42 struct iosys_map;
43 struct drm_gem_object;
44 
45 /**
46  * struct drm_gem_object_funcs - GEM object functions
47  */
48 struct drm_gem_object_funcs {
49 	/**
50 	 * @free:
51 	 *
52 	 * Deconstructor for drm_gem_objects.
53 	 *
54 	 * This callback is mandatory.
55 	 */
56 	void (*free)(struct drm_gem_object *obj);
57 
58 	/**
59 	 * @open:
60 	 *
61 	 * Called upon GEM handle creation.
62 	 *
63 	 * This callback is optional.
64 	 */
65 	int (*open)(struct drm_gem_object *obj, struct drm_file *file);
66 
67 	/**
68 	 * @close:
69 	 *
70 	 * Called upon GEM handle release.
71 	 *
72 	 * This callback is optional.
73 	 */
74 	void (*close)(struct drm_gem_object *obj, struct drm_file *file);
75 
76 	/**
77 	 * @print_info:
78 	 *
79 	 * If driver subclasses struct &drm_gem_object, it can implement this
80 	 * optional hook for printing additional driver specific info.
81 	 *
82 	 * drm_printf_indent() should be used in the callback passing it the
83 	 * indent argument.
84 	 *
85 	 * This callback is called from drm_gem_print_info().
86 	 *
87 	 * This callback is optional.
88 	 */
89 	void (*print_info)(struct drm_printer *p, unsigned int indent,
90 			   const struct drm_gem_object *obj);
91 
92 	/**
93 	 * @export:
94 	 *
95 	 * Export backing buffer as a &dma_buf.
96 	 * If this is not set drm_gem_prime_export() is used.
97 	 *
98 	 * This callback is optional.
99 	 */
100 	struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);
101 
102 	/**
103 	 * @pin:
104 	 *
105 	 * Pin backing buffer in memory. Used by the drm_gem_map_attach() helper.
106 	 *
107 	 * This callback is optional.
108 	 */
109 	int (*pin)(struct drm_gem_object *obj);
110 
111 	/**
112 	 * @unpin:
113 	 *
114 	 * Unpin backing buffer. Used by the drm_gem_map_detach() helper.
115 	 *
116 	 * This callback is optional.
117 	 */
118 	void (*unpin)(struct drm_gem_object *obj);
119 
120 	/**
121 	 * @get_sg_table:
122 	 *
123 	 * Returns a Scatter-Gather table representation of the buffer.
124 	 * Used when exporting a buffer by the drm_gem_map_dma_buf() helper.
125 	 * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()
126 	 * in drm_gem_unmap_buf(), therefore these helpers and this callback
127 	 * here cannot be used for sg tables pointing at driver private memory
128 	 * ranges.
129 	 *
130 	 * See also drm_prime_pages_to_sg().
131 	 */
132 	struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);
133 
134 	/**
135 	 * @vmap:
136 	 *
137 	 * Returns a virtual address for the buffer. Used by the
138 	 * drm_gem_dmabuf_vmap() helper.
139 	 *
140 	 * This callback is optional.
141 	 */
142 	int (*vmap)(struct drm_gem_object *obj, struct iosys_map *map);
143 
144 	/**
145 	 * @vunmap:
146 	 *
147 	 * Releases the address previously returned by @vmap. Used by the
148 	 * drm_gem_dmabuf_vunmap() helper.
149 	 *
150 	 * This callback is optional.
151 	 */
152 	void (*vunmap)(struct drm_gem_object *obj, struct iosys_map *map);
153 
154 	/**
155 	 * @mmap:
156 	 *
157 	 * Handle mmap() of the gem object, setup vma accordingly.
158 	 *
159 	 * This callback is optional.
160 	 *
161 	 * The callback is used by both drm_gem_mmap_obj() and
162 	 * drm_gem_prime_mmap().  When @mmap is present @vm_ops is not
163 	 * used, the @mmap callback must set vma->vm_ops instead.
164 	 */
165 	int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
166 
167 	/**
168 	 * @vm_ops:
169 	 *
170 	 * Virtual memory operations used with mmap.
171 	 *
172 	 * This is optional but necessary for mmap support.
173 	 */
174 	const struct vm_operations_struct *vm_ops;
175 };
176 
177 /**
178  * struct drm_gem_lru - A simple LRU helper
179  *
180  * A helper for tracking GEM objects in a given state, to aid in
181  * driver's shrinker implementation.  Tracks the count of pages
182  * for lockless &shrinker.count_objects, and provides
183  * &drm_gem_lru_scan for driver's &shrinker.scan_objects
184  * implementation.
185  */
186 struct drm_gem_lru {
187 	/**
188 	 * @lock:
189 	 *
190 	 * Lock protecting movement of GEM objects between LRUs.  All
191 	 * LRUs that the object can move between should be protected
192 	 * by the same lock.
193 	 */
194 	struct mutex *lock;
195 
196 	/**
197 	 * @count:
198 	 *
199 	 * The total number of backing pages of the GEM objects in
200 	 * this LRU.
201 	 */
202 	long count;
203 
204 	/**
205 	 * @list:
206 	 *
207 	 * The LRU list.
208 	 */
209 	struct list_head list;
210 };
211 
212 /**
213  * struct drm_gem_object - GEM buffer object
214  *
215  * This structure defines the generic parts for GEM buffer objects, which are
216  * mostly around handling mmap and userspace handles.
217  *
218  * Buffer objects are often abbreviated to BO.
219  */
220 struct drm_gem_object {
221 	/**
222 	 * @refcount:
223 	 *
224 	 * Reference count of this object
225 	 *
226 	 * Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked()
227 	 * or drm_gem_object_put() to release a reference to a GEM
228 	 * buffer object.
229 	 */
230 	struct kref refcount;
231 
232 	/**
233 	 * @handle_count:
234 	 *
235 	 * This is the GEM file_priv handle count of this object.
236 	 *
237 	 * Each handle also holds a reference. Note that when the handle_count
238 	 * drops to 0 any global names (e.g. the id in the flink namespace) will
239 	 * be cleared.
240 	 *
241 	 * Protected by &drm_device.object_name_lock.
242 	 */
243 	unsigned handle_count;
244 
245 	/**
246 	 * @dev: DRM dev this object belongs to.
247 	 */
248 	struct drm_device *dev;
249 
250 	/**
251 	 * @filp:
252 	 *
253 	 * SHMEM file node used as backing storage for swappable buffer objects.
254 	 * GEM also supports driver private objects with driver-specific backing
255 	 * storage (contiguous DMA memory, special reserved blocks). In this
256 	 * case @filp is NULL.
257 	 */
258 	struct file *filp;
259 
260 	/**
261 	 * @vma_node:
262 	 *
263 	 * Mapping info for this object to support mmap. Drivers are supposed to
264 	 * allocate the mmap offset using drm_gem_create_mmap_offset(). The
265 	 * offset itself can be retrieved using drm_vma_node_offset_addr().
266 	 *
267 	 * Memory mapping itself is handled by drm_gem_mmap(), which also checks
268 	 * that userspace is allowed to access the object.
269 	 */
270 	struct drm_vma_offset_node vma_node;
271 
272 	/**
273 	 * @size:
274 	 *
275 	 * Size of the object, in bytes.  Immutable over the object's
276 	 * lifetime.
277 	 */
278 	size_t size;
279 
280 	/**
281 	 * @name:
282 	 *
283 	 * Global name for this object, starts at 1. 0 means unnamed.
284 	 * Access is covered by &drm_device.object_name_lock. This is used by
285 	 * the GEM_FLINK and GEM_OPEN ioctls.
286 	 */
287 	int name;
288 
289 	/**
290 	 * @dma_buf:
291 	 *
292 	 * dma-buf associated with this GEM object.
293 	 *
294 	 * Pointer to the dma-buf associated with this gem object (either
295 	 * through importing or exporting). We break the resulting reference
296 	 * loop when the last gem handle for this object is released.
297 	 *
298 	 * Protected by &drm_device.object_name_lock.
299 	 */
300 	struct dma_buf *dma_buf;
301 
302 	/**
303 	 * @import_attach:
304 	 *
305 	 * dma-buf attachment backing this object.
306 	 *
307 	 * Any foreign dma_buf imported as a gem object has this set to the
308 	 * attachment point for the device. This is invariant over the lifetime
309 	 * of a gem object.
310 	 *
311 	 * The &drm_gem_object_funcs.free callback is responsible for
312 	 * cleaning up the dma_buf attachment and references acquired at import
313 	 * time.
314 	 *
315 	 * Note that the drm gem/prime core does not depend upon drivers setting
316 	 * this field any more. So for drivers where this doesn't make sense
317 	 * (e.g. virtual devices or a displaylink behind an usb bus) they can
318 	 * simply leave it as NULL.
319 	 */
320 	struct dma_buf_attachment *import_attach;
321 
322 	/**
323 	 * @resv:
324 	 *
325 	 * Pointer to reservation object associated with the this GEM object.
326 	 *
327 	 * Normally (@resv == &@_resv) except for imported GEM objects.
328 	 */
329 	struct dma_resv *resv;
330 
331 	/**
332 	 * @_resv:
333 	 *
334 	 * A reservation object for this GEM object.
335 	 *
336 	 * This is unused for imported GEM objects.
337 	 */
338 	struct dma_resv _resv;
339 
340 	/**
341 	 * @funcs:
342 	 *
343 	 * Optional GEM object functions. If this is set, it will be used instead of the
344 	 * corresponding &drm_driver GEM callbacks.
345 	 *
346 	 * New drivers should use this.
347 	 *
348 	 */
349 	const struct drm_gem_object_funcs *funcs;
350 
351 	/**
352 	 * @lru_node:
353 	 *
354 	 * List node in a &drm_gem_lru.
355 	 */
356 	struct list_head lru_node;
357 
358 	/**
359 	 * @lru:
360 	 *
361 	 * The current LRU list that the GEM object is on.
362 	 */
363 	struct drm_gem_lru *lru;
364 };
365 
366 /**
367  * DRM_GEM_FOPS - Default drm GEM file operations
368  *
369  * This macro provides a shorthand for setting the GEM file ops in the
370  * &file_operations structure.  If all you need are the default ops, use
371  * DEFINE_DRM_GEM_FOPS instead.
372  */
373 #define DRM_GEM_FOPS \
374 	.open		= drm_open,\
375 	.release	= drm_release,\
376 	.unlocked_ioctl	= drm_ioctl,\
377 	.compat_ioctl	= drm_compat_ioctl,\
378 	.poll		= drm_poll,\
379 	.read		= drm_read,\
380 	.llseek		= noop_llseek,\
381 	.mmap		= drm_gem_mmap
382 
383 /**
384  * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
385  * @name: name for the generated structure
386  *
387  * This macro autogenerates a suitable &struct file_operations for GEM based
388  * drivers, which can be assigned to &drm_driver.fops. Note that this structure
389  * cannot be shared between drivers, because it contains a reference to the
390  * current module using THIS_MODULE.
391  *
392  * Note that the declaration is already marked as static - if you need a
393  * non-static version of this you're probably doing it wrong and will break the
394  * THIS_MODULE reference by accident.
395  */
396 #define DEFINE_DRM_GEM_FOPS(name) \
397 	static const struct file_operations name = {\
398 		.owner		= THIS_MODULE,\
399 		DRM_GEM_FOPS,\
400 	}
401 
402 void drm_gem_object_release(struct drm_gem_object *obj);
403 void drm_gem_object_free(struct kref *kref);
404 int drm_gem_object_init(struct drm_device *dev,
405 			struct drm_gem_object *obj, size_t size);
406 void drm_gem_private_object_init(struct drm_device *dev,
407 				 struct drm_gem_object *obj, size_t size);
408 void drm_gem_vm_open(struct vm_area_struct *vma);
409 void drm_gem_vm_close(struct vm_area_struct *vma);
410 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
411 		     struct vm_area_struct *vma);
412 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
413 
414 /**
415  * drm_gem_object_get - acquire a GEM buffer object reference
416  * @obj: GEM buffer object
417  *
418  * This function acquires an additional reference to @obj. It is illegal to
419  * call this without already holding a reference. No locks required.
420  */
421 static inline void drm_gem_object_get(struct drm_gem_object *obj)
422 {
423 	kref_get(&obj->refcount);
424 }
425 
426 __attribute__((nonnull))
427 static inline void
428 __drm_gem_object_put(struct drm_gem_object *obj)
429 {
430 	kref_put(&obj->refcount, drm_gem_object_free);
431 }
432 
433 /**
434  * drm_gem_object_put - drop a GEM buffer object reference
435  * @obj: GEM buffer object
436  *
437  * This releases a reference to @obj.
438  */
439 static inline void
440 drm_gem_object_put(struct drm_gem_object *obj)
441 {
442 	if (obj)
443 		__drm_gem_object_put(obj);
444 }
445 
446 int drm_gem_handle_create(struct drm_file *file_priv,
447 			  struct drm_gem_object *obj,
448 			  u32 *handlep);
449 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
450 
451 
452 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
453 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
454 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
455 
456 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
457 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
458 		bool dirty, bool accessed);
459 
460 int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
461 			   int count, struct drm_gem_object ***objs_out);
462 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
463 long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
464 				    bool wait_all, unsigned long timeout);
465 int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
466 			      struct ww_acquire_ctx *acquire_ctx);
467 void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
468 				 struct ww_acquire_ctx *acquire_ctx);
469 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
470 			    u32 handle, u64 *offset);
471 
472 void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);
473 void drm_gem_lru_remove(struct drm_gem_object *obj);
474 void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);
475 unsigned long drm_gem_lru_scan(struct drm_gem_lru *lru, unsigned nr_to_scan,
476 			       bool (*shrink)(struct drm_gem_object *obj));
477 
478 #endif /* __DRM_GEM_H__ */
479