Blame view

include/drm/drm_gem.h 12.1 KB
d9fc9413f   Daniel Vetter   drm: Extract <drm...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #ifndef __DRM_GEM_H__
  #define __DRM_GEM_H__
  
  /*
   * GEM Graphics Execution Manager Driver Interfaces
   *
   * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
   * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
   * Copyright (c) 2009-2010, Code Aurora Forum.
   * All rights reserved.
   * Copyright © 2014 Intel Corporation
   *   Daniel Vetter <daniel.vetter@ffwll.ch>
   *
   * Author: Rickard E. (Rik) Faith <faith@valinux.com>
   * Author: Gareth Hughes <gareth@valinux.com>
   *
   * Permission is hereby granted, free of charge, to any person obtaining a
   * copy of this software and associated documentation files (the "Software"),
   * to deal in the Software without restriction, including without limitation
   * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   * and/or sell copies of the Software, and to permit persons to whom the
   * Software is furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice (including the next
   * paragraph) shall be included in all copies or substantial portions of the
   * Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
   * OTHER DEALINGS IN THE SOFTWARE.
   */
c6bb9baa0   Daniel Vetter   drm: Extract drm_...
36
  #include <linux/kref.h>
52791eeec   Christian König   dma-buf: rename r...
37
  #include <linux/dma-resv.h>
c6bb9baa0   Daniel Vetter   drm: Extract drm_...
38
39
  
  #include <drm/drm_vma_manager.h>
b39b5394f   Noralf Trønnes   drm/gem: Add drm_...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  struct drm_gem_object;
  
  /**
   * struct drm_gem_object_funcs - GEM object functions
   */
  struct drm_gem_object_funcs {
  	/**
  	 * @free:
  	 *
  	 * Deconstructor for drm_gem_objects.
  	 *
  	 * This callback is mandatory.
  	 */
  	void (*free)(struct drm_gem_object *obj);
  
  	/**
  	 * @open:
  	 *
  	 * Called upon GEM handle creation.
  	 *
  	 * This callback is optional.
  	 */
  	int (*open)(struct drm_gem_object *obj, struct drm_file *file);
  
  	/**
  	 * @close:
  	 *
  	 * Called upon GEM handle release.
  	 *
  	 * This callback is optional.
  	 */
  	void (*close)(struct drm_gem_object *obj, struct drm_file *file);
  
  	/**
  	 * @print_info:
  	 *
  	 * If driver subclasses struct &drm_gem_object, it can implement this
  	 * optional hook for printing additional driver specific info.
  	 *
  	 * drm_printf_indent() should be used in the callback passing it the
  	 * indent argument.
  	 *
  	 * This callback is called from drm_gem_print_info().
  	 *
  	 * This callback is optional.
  	 */
  	void (*print_info)(struct drm_printer *p, unsigned int indent,
  			   const struct drm_gem_object *obj);
  
  	/**
  	 * @export:
  	 *
  	 * Export backing buffer as a &dma_buf.
  	 * If this is not set drm_gem_prime_export() is used.
  	 *
  	 * This callback is optional.
  	 */
  	struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);
  
  	/**
  	 * @pin:
  	 *
805dc614d   Daniel Vetter   drm/prime: Update...
102
  	 * Pin backing buffer in memory. Used by the drm_gem_map_attach() helper.
b39b5394f   Noralf Trønnes   drm/gem: Add drm_...
103
104
105
106
107
108
109
110
  	 *
  	 * This callback is optional.
  	 */
  	int (*pin)(struct drm_gem_object *obj);
  
  	/**
  	 * @unpin:
  	 *
805dc614d   Daniel Vetter   drm/prime: Update...
111
  	 * Unpin backing buffer. Used by the drm_gem_map_detach() helper.
b39b5394f   Noralf Trønnes   drm/gem: Add drm_...
112
113
114
115
116
117
118
119
120
  	 *
  	 * This callback is optional.
  	 */
  	void (*unpin)(struct drm_gem_object *obj);
  
  	/**
  	 * @get_sg_table:
  	 *
  	 * Returns a Scatter-Gather table representation of the buffer.
805dc614d   Daniel Vetter   drm/prime: Update...
121
122
123
124
125
  	 * Used when exporting a buffer by the drm_gem_map_dma_buf() helper.
  	 * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()
  	 * in drm_gem_unmap_buf(), therefore these helpers and this callback
  	 * here cannot be used for sg tables pointing at driver private memory
  	 * ranges.
b39b5394f   Noralf Trønnes   drm/gem: Add drm_...
126
  	 *
805dc614d   Daniel Vetter   drm/prime: Update...
127
  	 * See also drm_prime_pages_to_sg().
b39b5394f   Noralf Trønnes   drm/gem: Add drm_...
128
129
130
131
132
133
  	 */
  	struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);
  
  	/**
  	 * @vmap:
  	 *
805dc614d   Daniel Vetter   drm/prime: Update...
134
135
  	 * Returns a virtual address for the buffer. Used by the
  	 * drm_gem_dmabuf_vmap() helper.
b39b5394f   Noralf Trønnes   drm/gem: Add drm_...
136
137
138
139
140
141
142
143
  	 *
  	 * This callback is optional.
  	 */
  	void *(*vmap)(struct drm_gem_object *obj);
  
  	/**
  	 * @vunmap:
  	 *
805dc614d   Daniel Vetter   drm/prime: Update...
144
145
  	 * Releases the the address previously returned by @vmap. Used by the
  	 * drm_gem_dmabuf_vunmap() helper.
b39b5394f   Noralf Trønnes   drm/gem: Add drm_...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  	 *
  	 * This callback is optional.
  	 */
  	void (*vunmap)(struct drm_gem_object *obj, void *vaddr);
  
  	/**
  	 * @vm_ops:
  	 *
  	 * Virtual memory operations used with mmap.
  	 *
  	 * This is optional but necessary for mmap support.
  	 */
  	const struct vm_operations_struct *vm_ops;
  };
d9fc9413f   Daniel Vetter   drm: Extract <drm...
160
  /**
decc60bf4   Daniel Vetter   drm: Update GEM r...
161
162
163
164
165
166
   * struct drm_gem_object - GEM buffer object
   *
   * This structure defines the generic parts for GEM buffer objects, which are
   * mostly around handling mmap and userspace handles.
   *
   * Buffer objects are often abbreviated to BO.
d9fc9413f   Daniel Vetter   drm: Extract <drm...
167
168
   */
  struct drm_gem_object {
decc60bf4   Daniel Vetter   drm: Update GEM r...
169
170
171
172
173
  	/**
  	 * @refcount:
  	 *
  	 * Reference count of this object
  	 *
e6b62714e   Thierry Reding   drm: Introduce dr...
174
175
176
  	 * Please use drm_gem_object_get() to acquire and drm_gem_object_put()
  	 * or drm_gem_object_put_unlocked() to release a reference to a GEM
  	 * buffer object.
decc60bf4   Daniel Vetter   drm: Update GEM r...
177
  	 */
d9fc9413f   Daniel Vetter   drm: Extract <drm...
178
179
180
  	struct kref refcount;
  
  	/**
decc60bf4   Daniel Vetter   drm: Update GEM r...
181
182
183
  	 * @handle_count:
  	 *
  	 * This is the GEM file_priv handle count of this object.
d9fc9413f   Daniel Vetter   drm: Extract <drm...
184
185
186
187
188
  	 *
  	 * Each handle also holds a reference. Note that when the handle_count
  	 * drops to 0 any global names (e.g. the id in the flink namespace) will
  	 * be cleared.
  	 *
940eba2d5   Daniel Vetter   drm/gem|prime|mm:...
189
  	 * Protected by &drm_device.object_name_lock.
decc60bf4   Daniel Vetter   drm: Update GEM r...
190
  	 */
d9fc9413f   Daniel Vetter   drm: Extract <drm...
191
  	unsigned handle_count;
decc60bf4   Daniel Vetter   drm: Update GEM r...
192
193
194
  	/**
  	 * @dev: DRM dev this object belongs to.
  	 */
d9fc9413f   Daniel Vetter   drm: Extract <drm...
195
  	struct drm_device *dev;
decc60bf4   Daniel Vetter   drm: Update GEM r...
196
197
198
199
200
201
202
203
  	/**
  	 * @filp:
  	 *
  	 * SHMEM file node used as backing storage for swappable buffer objects.
  	 * GEM also supports driver private objects with driver-specific backing
  	 * storage (contiguous CMA memory, special reserved blocks). In this
  	 * case @filp is NULL.
  	 */
d9fc9413f   Daniel Vetter   drm: Extract <drm...
204
  	struct file *filp;
decc60bf4   Daniel Vetter   drm: Update GEM r...
205
206
207
208
209
210
211
212
213
214
  	/**
  	 * @vma_node:
  	 *
  	 * Mapping info for this object to support mmap. Drivers are supposed to
  	 * allocate the mmap offset using drm_gem_create_mmap_offset(). The
  	 * offset itself can be retrieved using drm_vma_node_offset_addr().
  	 *
  	 * Memory mapping itself is handled by drm_gem_mmap(), which also checks
  	 * that userspace is allowed to access the object.
  	 */
d9fc9413f   Daniel Vetter   drm: Extract <drm...
215
216
217
  	struct drm_vma_offset_node vma_node;
  
  	/**
decc60bf4   Daniel Vetter   drm: Update GEM r...
218
219
  	 * @size:
  	 *
d9fc9413f   Daniel Vetter   drm: Extract <drm...
220
221
222
223
224
225
  	 * Size of the object, in bytes.  Immutable over the object's
  	 * lifetime.
  	 */
  	size_t size;
  
  	/**
decc60bf4   Daniel Vetter   drm: Update GEM r...
226
227
  	 * @name:
  	 *
d9fc9413f   Daniel Vetter   drm: Extract <drm...
228
  	 * Global name for this object, starts at 1. 0 means unnamed.
940eba2d5   Daniel Vetter   drm/gem|prime|mm:...
229
230
  	 * Access is covered by &drm_device.object_name_lock. This is used by
  	 * the GEM_FLINK and GEM_OPEN ioctls.
d9fc9413f   Daniel Vetter   drm: Extract <drm...
231
232
233
234
  	 */
  	int name;
  
  	/**
decc60bf4   Daniel Vetter   drm: Update GEM r...
235
236
237
  	 * @dma_buf:
  	 *
  	 * dma-buf associated with this GEM object.
d9fc9413f   Daniel Vetter   drm: Extract <drm...
238
239
240
241
242
  	 *
  	 * Pointer to the dma-buf associated with this gem object (either
  	 * through importing or exporting). We break the resulting reference
  	 * loop when the last gem handle for this object is released.
  	 *
940eba2d5   Daniel Vetter   drm/gem|prime|mm:...
243
  	 * Protected by &drm_device.object_name_lock.
d9fc9413f   Daniel Vetter   drm: Extract <drm...
244
245
246
247
  	 */
  	struct dma_buf *dma_buf;
  
  	/**
decc60bf4   Daniel Vetter   drm: Update GEM r...
248
249
250
  	 * @import_attach:
  	 *
  	 * dma-buf attachment backing this object.
d9fc9413f   Daniel Vetter   drm: Extract <drm...
251
252
253
254
255
  	 *
  	 * Any foreign dma_buf imported as a gem object has this set to the
  	 * attachment point for the device. This is invariant over the lifetime
  	 * of a gem object.
  	 *
940eba2d5   Daniel Vetter   drm/gem|prime|mm:...
256
  	 * The &drm_driver.gem_free_object callback is responsible for cleaning
d9fc9413f   Daniel Vetter   drm: Extract <drm...
257
258
259
260
261
262
263
264
  	 * up the dma_buf attachment and references acquired at import time.
  	 *
  	 * Note that the drm gem/prime core does not depend upon drivers setting
  	 * this field any more. So for drivers where this doesn't make sense
  	 * (e.g. virtual devices or a displaylink behind an usb bus) they can
  	 * simply leave it as NULL.
  	 */
  	struct dma_buf_attachment *import_attach;
b39b5394f   Noralf Trønnes   drm/gem: Add drm_...
265
266
  
  	/**
1ba627148   Rob Herring   drm: Add reservat...
267
268
269
270
271
272
  	 * @resv:
  	 *
  	 * Pointer to reservation object associated with the this GEM object.
  	 *
  	 * Normally (@resv == &@_resv) except for imported GEM objects.
  	 */
52791eeec   Christian König   dma-buf: rename r...
273
  	struct dma_resv *resv;
1ba627148   Rob Herring   drm: Add reservat...
274
275
276
277
278
279
280
281
  
  	/**
  	 * @_resv:
  	 *
  	 * A reservation object for this GEM object.
  	 *
  	 * This is unused for imported GEM objects.
  	 */
52791eeec   Christian König   dma-buf: rename r...
282
  	struct dma_resv _resv;
1ba627148   Rob Herring   drm: Add reservat...
283
284
  
  	/**
b39b5394f   Noralf Trønnes   drm/gem: Add drm_...
285
286
287
288
289
290
291
292
293
  	 * @funcs:
  	 *
  	 * Optional GEM object functions. If this is set, it will be used instead of the
  	 * corresponding &drm_driver GEM callbacks.
  	 *
  	 * New drivers should use this.
  	 *
  	 */
  	const struct drm_gem_object_funcs *funcs;
d9fc9413f   Daniel Vetter   drm: Extract <drm...
294
  };
f42e18193   Daniel Vetter   drm/gem: Add DEFI...
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  /**
   * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
   * @name: name for the generated structure
   *
   * This macro autogenerates a suitable &struct file_operations for GEM based
   * drivers, which can be assigned to &drm_driver.fops. Note that this structure
   * cannot be shared between drivers, because it contains a reference to the
   * current module using THIS_MODULE.
   *
   * Note that the declaration is already marked as static - if you need a
   * non-static version of this you're probably doing it wrong and will break the
   * THIS_MODULE reference by accident.
   */
  #define DEFINE_DRM_GEM_FOPS(name) \
  	static const struct file_operations name = {\
  		.owner		= THIS_MODULE,\
  		.open		= drm_open,\
  		.release	= drm_release,\
  		.unlocked_ioctl	= drm_ioctl,\
  		.compat_ioctl	= drm_compat_ioctl,\
  		.poll		= drm_poll,\
  		.read		= drm_read,\
  		.llseek		= noop_llseek,\
  		.mmap		= drm_gem_mmap,\
  	}
d9fc9413f   Daniel Vetter   drm: Extract <drm...
320
321
322
323
324
325
326
327
328
329
330
  void drm_gem_object_release(struct drm_gem_object *obj);
  void drm_gem_object_free(struct kref *kref);
  int drm_gem_object_init(struct drm_device *dev,
  			struct drm_gem_object *obj, size_t size);
  void drm_gem_private_object_init(struct drm_device *dev,
  				 struct drm_gem_object *obj, size_t size);
  void drm_gem_vm_open(struct vm_area_struct *vma);
  void drm_gem_vm_close(struct vm_area_struct *vma);
  int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  		     struct vm_area_struct *vma);
  int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
decc60bf4   Daniel Vetter   drm: Update GEM r...
331
  /**
e6b62714e   Thierry Reding   drm: Introduce dr...
332
   * drm_gem_object_get - acquire a GEM buffer object reference
decc60bf4   Daniel Vetter   drm: Update GEM r...
333
334
   * @obj: GEM buffer object
   *
e6b62714e   Thierry Reding   drm: Introduce dr...
335
336
   * This function acquires an additional reference to @obj. It is illegal to
   * call this without already holding a reference. No locks required.
decc60bf4   Daniel Vetter   drm: Update GEM r...
337
   */
e6b62714e   Thierry Reding   drm: Introduce dr...
338
  static inline void drm_gem_object_get(struct drm_gem_object *obj)
d9fc9413f   Daniel Vetter   drm: Extract <drm...
339
340
341
  {
  	kref_get(&obj->refcount);
  }
decc60bf4   Daniel Vetter   drm: Update GEM r...
342
  /**
e6b62714e   Thierry Reding   drm: Introduce dr...
343
   * __drm_gem_object_put - raw function to release a GEM buffer object reference
decc60bf4   Daniel Vetter   drm: Update GEM r...
344
345
   * @obj: GEM buffer object
   *
9f0ba539d   Daniel Vetter   drm/gem: support ...
346
   * This function is meant to be used by drivers which are not encumbered with
940eba2d5   Daniel Vetter   drm/gem|prime|mm:...
347
   * &drm_device.struct_mutex legacy locking and which are using the
9f0ba539d   Daniel Vetter   drm/gem: support ...
348
   * gem_free_object_unlocked callback. It avoids all the locking checks and
e6b62714e   Thierry Reding   drm: Introduce dr...
349
   * locking overhead of drm_gem_object_put() and drm_gem_object_put_unlocked().
decc60bf4   Daniel Vetter   drm: Update GEM r...
350
   *
9f0ba539d   Daniel Vetter   drm/gem: support ...
351
   * Drivers should never call this directly in their code. Instead they should
e6b62714e   Thierry Reding   drm: Introduce dr...
352
353
   * wrap it up into a ``driver_gem_object_put(struct driver_gem_object *obj)``
   * wrapper function, and use that. Shared code should never call this, to
940eba2d5   Daniel Vetter   drm/gem|prime|mm:...
354
355
   * avoid breaking drivers by accident which still depend upon
   * &drm_device.struct_mutex locking.
decc60bf4   Daniel Vetter   drm: Update GEM r...
356
   */
d9fc9413f   Daniel Vetter   drm: Extract <drm...
357
  static inline void
e6b62714e   Thierry Reding   drm: Introduce dr...
358
  __drm_gem_object_put(struct drm_gem_object *obj)
d9fc9413f   Daniel Vetter   drm: Extract <drm...
359
  {
9f0ba539d   Daniel Vetter   drm/gem: support ...
360
  	kref_put(&obj->refcount, drm_gem_object_free);
d9fc9413f   Daniel Vetter   drm: Extract <drm...
361
  }
e6b62714e   Thierry Reding   drm: Introduce dr...
362
363
  void drm_gem_object_put_unlocked(struct drm_gem_object *obj);
  void drm_gem_object_put(struct drm_gem_object *obj);
d9fc9413f   Daniel Vetter   drm: Extract <drm...
364
365
366
367
368
369
370
371
372
373
374
375
376
  int drm_gem_handle_create(struct drm_file *file_priv,
  			  struct drm_gem_object *obj,
  			  u32 *handlep);
  int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
  
  
  void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
  int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
  int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
  
  struct page **drm_gem_get_pages(struct drm_gem_object *obj);
  void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  		bool dirty, bool accessed);
c117aa4d8   Rob Herring   drm: Add a drm_ge...
377
378
  int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
  			   int count, struct drm_gem_object ***objs_out);
a8ad0bd84   Chris Wilson   drm: Remove unuse...
379
  struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
52791eeec   Christian König   dma-buf: rename r...
380
  long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
1ba627148   Rob Herring   drm: Add reservat...
381
  				    bool wait_all, unsigned long timeout);
7edc3e3b9   Eric Anholt   drm: Add helpers ...
382
383
384
385
  int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
  			      struct ww_acquire_ctx *acquire_ctx);
  void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
  				 struct ww_acquire_ctx *acquire_ctx);
5d5a179d3   Eric Anholt   drm: Add helpers ...
386
387
388
389
390
  int drm_gem_fence_array_add(struct xarray *fence_array,
  			    struct dma_fence *fence);
  int drm_gem_fence_array_add_implicit(struct xarray *fence_array,
  				     struct drm_gem_object *obj,
  				     bool write);
abd4e745f   Rob Herring   Revert "drm/gem: ...
391
392
  int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  			    u32 handle, u64 *offset);
d9fc9413f   Daniel Vetter   drm: Extract <drm...
393
394
395
396
397
  int drm_gem_dumb_destroy(struct drm_file *file,
  			 struct drm_device *dev,
  			 uint32_t handle);
  
  #endif /* __DRM_GEM_H__ */