drm_gem.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #ifndef __DRM_GEM_H__
  2. #define __DRM_GEM_H__
  3. /*
  4. * GEM Graphics Execution Manager Driver Interfaces
  5. *
  6. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  7. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  8. * Copyright (c) 2009-2010, Code Aurora Forum.
  9. * All rights reserved.
  10. * Copyright © 2014 Intel Corporation
  11. * Daniel Vetter <daniel.vetter@ffwll.ch>
  12. *
  13. * Author: Rickard E. (Rik) Faith <faith@valinux.com>
  14. * Author: Gareth Hughes <gareth@valinux.com>
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files (the "Software"),
  18. * to deal in the Software without restriction, including without limitation
  19. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  20. * and/or sell copies of the Software, and to permit persons to whom the
  21. * Software is furnished to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice (including the next
  24. * paragraph) shall be included in all copies or substantial portions of the
  25. * Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  31. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  32. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  33. * OTHER DEALINGS IN THE SOFTWARE.
  34. */
  35. #include <linux/kref.h>
  36. #include <drm/drm_vma_manager.h>
  37. /**
  38. * struct drm_gem_object - GEM buffer object
  39. *
  40. * This structure defines the generic parts for GEM buffer objects, which are
  41. * mostly around handling mmap and userspace handles.
  42. *
  43. * Buffer objects are often abbreviated to BO.
  44. */
  45. struct drm_gem_object {
  46. /**
  47. * @refcount:
  48. *
  49. * Reference count of this object
  50. *
  51. * Please use drm_gem_object_get() to acquire and drm_gem_object_put()
  52. * or drm_gem_object_put_unlocked() to release a reference to a GEM
  53. * buffer object.
  54. */
  55. struct kref refcount;
  56. /**
  57. * @handle_count:
  58. *
  59. * This is the GEM file_priv handle count of this object.
  60. *
  61. * Each handle also holds a reference. Note that when the handle_count
  62. * drops to 0 any global names (e.g. the id in the flink namespace) will
  63. * be cleared.
  64. *
  65. * Protected by &drm_device.object_name_lock.
  66. */
  67. unsigned handle_count;
  68. /**
  69. * @dev: DRM dev this object belongs to.
  70. */
  71. struct drm_device *dev;
  72. /**
  73. * @filp:
  74. *
  75. * SHMEM file node used as backing storage for swappable buffer objects.
  76. * GEM also supports driver private objects with driver-specific backing
  77. * storage (contiguous CMA memory, special reserved blocks). In this
  78. * case @filp is NULL.
  79. */
  80. struct file *filp;
  81. /**
  82. * @vma_node:
  83. *
  84. * Mapping info for this object to support mmap. Drivers are supposed to
  85. * allocate the mmap offset using drm_gem_create_mmap_offset(). The
  86. * offset itself can be retrieved using drm_vma_node_offset_addr().
  87. *
  88. * Memory mapping itself is handled by drm_gem_mmap(), which also checks
  89. * that userspace is allowed to access the object.
  90. */
  91. struct drm_vma_offset_node vma_node;
  92. /**
  93. * @size:
  94. *
  95. * Size of the object, in bytes. Immutable over the object's
  96. * lifetime.
  97. */
  98. size_t size;
  99. /**
  100. * @name:
  101. *
  102. * Global name for this object, starts at 1. 0 means unnamed.
  103. * Access is covered by &drm_device.object_name_lock. This is used by
  104. * the GEM_FLINK and GEM_OPEN ioctls.
  105. */
  106. int name;
  107. /**
  108. * @dma_buf:
  109. *
  110. * dma-buf associated with this GEM object.
  111. *
  112. * Pointer to the dma-buf associated with this gem object (either
  113. * through importing or exporting). We break the resulting reference
  114. * loop when the last gem handle for this object is released.
  115. *
  116. * Protected by &drm_device.object_name_lock.
  117. */
  118. struct dma_buf *dma_buf;
  119. /**
  120. * @import_attach:
  121. *
  122. * dma-buf attachment backing this object.
  123. *
  124. * Any foreign dma_buf imported as a gem object has this set to the
  125. * attachment point for the device. This is invariant over the lifetime
  126. * of a gem object.
  127. *
  128. * The &drm_driver.gem_free_object callback is responsible for cleaning
  129. * up the dma_buf attachment and references acquired at import time.
  130. *
  131. * Note that the drm gem/prime core does not depend upon drivers setting
  132. * this field any more. So for drivers where this doesn't make sense
  133. * (e.g. virtual devices or a displaylink behind an usb bus) they can
  134. * simply leave it as NULL.
  135. */
  136. struct dma_buf_attachment *import_attach;
  137. };
  138. /**
  139. * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
  140. * @name: name for the generated structure
  141. *
  142. * This macro autogenerates a suitable &struct file_operations for GEM based
  143. * drivers, which can be assigned to &drm_driver.fops. Note that this structure
  144. * cannot be shared between drivers, because it contains a reference to the
  145. * current module using THIS_MODULE.
  146. *
  147. * Note that the declaration is already marked as static - if you need a
  148. * non-static version of this you're probably doing it wrong and will break the
  149. * THIS_MODULE reference by accident.
  150. */
  151. #define DEFINE_DRM_GEM_FOPS(name) \
  152. static const struct file_operations name = {\
  153. .owner = THIS_MODULE,\
  154. .open = drm_open,\
  155. .release = drm_release,\
  156. .unlocked_ioctl = drm_ioctl,\
  157. .compat_ioctl = drm_compat_ioctl,\
  158. .poll = drm_poll,\
  159. .read = drm_read,\
  160. .llseek = noop_llseek,\
  161. .mmap = drm_gem_mmap,\
  162. }
  163. void drm_gem_object_release(struct drm_gem_object *obj);
  164. void drm_gem_object_free(struct kref *kref);
  165. int drm_gem_object_init(struct drm_device *dev,
  166. struct drm_gem_object *obj, size_t size);
  167. void drm_gem_private_object_init(struct drm_device *dev,
  168. struct drm_gem_object *obj, size_t size);
  169. void drm_gem_vm_open(struct vm_area_struct *vma);
  170. void drm_gem_vm_close(struct vm_area_struct *vma);
  171. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  172. struct vm_area_struct *vma);
  173. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
  174. /**
  175. * drm_gem_object_get - acquire a GEM buffer object reference
  176. * @obj: GEM buffer object
  177. *
  178. * This function acquires an additional reference to @obj. It is illegal to
  179. * call this without already holding a reference. No locks required.
  180. */
  181. static inline void drm_gem_object_get(struct drm_gem_object *obj)
  182. {
  183. kref_get(&obj->refcount);
  184. }
  185. /**
  186. * __drm_gem_object_put - raw function to release a GEM buffer object reference
  187. * @obj: GEM buffer object
  188. *
  189. * This function is meant to be used by drivers which are not encumbered with
  190. * &drm_device.struct_mutex legacy locking and which are using the
  191. * gem_free_object_unlocked callback. It avoids all the locking checks and
  192. * locking overhead of drm_gem_object_put() and drm_gem_object_put_unlocked().
  193. *
  194. * Drivers should never call this directly in their code. Instead they should
  195. * wrap it up into a ``driver_gem_object_put(struct driver_gem_object *obj)``
  196. * wrapper function, and use that. Shared code should never call this, to
  197. * avoid breaking drivers by accident which still depend upon
  198. * &drm_device.struct_mutex locking.
  199. */
  200. static inline void
  201. __drm_gem_object_put(struct drm_gem_object *obj)
  202. {
  203. kref_put(&obj->refcount, drm_gem_object_free);
  204. }
  205. void drm_gem_object_put_unlocked(struct drm_gem_object *obj);
  206. void drm_gem_object_put(struct drm_gem_object *obj);
  207. /**
  208. * drm_gem_object_reference - acquire a GEM buffer object reference
  209. * @obj: GEM buffer object
  210. *
  211. * This is a compatibility alias for drm_gem_object_get() and should not be
  212. * used by new code.
  213. */
  214. static inline void drm_gem_object_reference(struct drm_gem_object *obj)
  215. {
  216. drm_gem_object_get(obj);
  217. }
  218. /**
  219. * __drm_gem_object_unreference - raw function to release a GEM buffer object
  220. * reference
  221. * @obj: GEM buffer object
  222. *
  223. * This is a compatibility alias for __drm_gem_object_put() and should not be
  224. * used by new code.
  225. */
  226. static inline void __drm_gem_object_unreference(struct drm_gem_object *obj)
  227. {
  228. __drm_gem_object_put(obj);
  229. }
  230. /**
  231. * drm_gem_object_unreference_unlocked - release a GEM buffer object reference
  232. * @obj: GEM buffer object
  233. *
  234. * This is a compatibility alias for drm_gem_object_put_unlocked() and should
  235. * not be used by new code.
  236. */
  237. static inline void
  238. drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
  239. {
  240. drm_gem_object_put_unlocked(obj);
  241. }
  242. /**
  243. * drm_gem_object_unreference - release a GEM buffer object reference
  244. * @obj: GEM buffer object
  245. *
  246. * This is a compatibility alias for drm_gem_object_put() and should not be
  247. * used by new code.
  248. */
  249. static inline void drm_gem_object_unreference(struct drm_gem_object *obj)
  250. {
  251. drm_gem_object_put(obj);
  252. }
  253. int drm_gem_handle_create(struct drm_file *file_priv,
  254. struct drm_gem_object *obj,
  255. u32 *handlep);
  256. int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
  257. void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
  258. int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
  259. int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
  260. struct page **drm_gem_get_pages(struct drm_gem_object *obj);
  261. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  262. bool dirty, bool accessed);
  263. struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
  264. int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  265. u32 handle, u64 *offset);
  266. int drm_gem_dumb_destroy(struct drm_file *file,
  267. struct drm_device *dev,
  268. uint32_t handle);
  269. #endif /* __DRM_GEM_H__ */