drm_gem.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. * @read_domains:
  109. *
  110. * Read memory domains. These monitor which caches contain read/write data
  111. * related to the object. When transitioning from one set of domains
  112. * to another, the driver is called to ensure that caches are suitably
  113. * flushed and invalidated.
  114. */
  115. uint32_t read_domains;
  116. /**
  117. * @write_domain: Corresponding unique write memory domain.
  118. */
  119. uint32_t write_domain;
  120. /**
  121. * @pending_read_domains:
  122. *
  123. * While validating an exec operation, the
  124. * new read/write domain values are computed here.
  125. * They will be transferred to the above values
  126. * at the point that any cache flushing occurs
  127. */
  128. uint32_t pending_read_domains;
  129. /**
  130. * @pending_write_domain: Write domain similar to @pending_read_domains.
  131. */
  132. uint32_t pending_write_domain;
  133. /**
  134. * @dma_buf:
  135. *
  136. * dma-buf associated with this GEM object.
  137. *
  138. * Pointer to the dma-buf associated with this gem object (either
  139. * through importing or exporting). We break the resulting reference
  140. * loop when the last gem handle for this object is released.
  141. *
  142. * Protected by &drm_device.object_name_lock.
  143. */
  144. struct dma_buf *dma_buf;
  145. /**
  146. * @import_attach:
  147. *
  148. * dma-buf attachment backing this object.
  149. *
  150. * Any foreign dma_buf imported as a gem object has this set to the
  151. * attachment point for the device. This is invariant over the lifetime
  152. * of a gem object.
  153. *
  154. * The &drm_driver.gem_free_object callback is responsible for cleaning
  155. * up the dma_buf attachment and references acquired at import time.
  156. *
  157. * Note that the drm gem/prime core does not depend upon drivers setting
  158. * this field any more. So for drivers where this doesn't make sense
  159. * (e.g. virtual devices or a displaylink behind an usb bus) they can
  160. * simply leave it as NULL.
  161. */
  162. struct dma_buf_attachment *import_attach;
  163. };
  164. void drm_gem_object_release(struct drm_gem_object *obj);
  165. void drm_gem_object_free(struct kref *kref);
  166. int drm_gem_object_init(struct drm_device *dev,
  167. struct drm_gem_object *obj, size_t size);
  168. void drm_gem_private_object_init(struct drm_device *dev,
  169. struct drm_gem_object *obj, size_t size);
  170. void drm_gem_vm_open(struct vm_area_struct *vma);
  171. void drm_gem_vm_close(struct vm_area_struct *vma);
  172. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  173. struct vm_area_struct *vma);
  174. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
  175. /**
  176. * drm_gem_object_get - acquire a GEM buffer object reference
  177. * @obj: GEM buffer object
  178. *
  179. * This function acquires an additional reference to @obj. It is illegal to
  180. * call this without already holding a reference. No locks required.
  181. */
  182. static inline void drm_gem_object_get(struct drm_gem_object *obj)
  183. {
  184. kref_get(&obj->refcount);
  185. }
  186. /**
  187. * __drm_gem_object_put - raw function to release a GEM buffer object reference
  188. * @obj: GEM buffer object
  189. *
  190. * This function is meant to be used by drivers which are not encumbered with
  191. * &drm_device.struct_mutex legacy locking and which are using the
  192. * gem_free_object_unlocked callback. It avoids all the locking checks and
  193. * locking overhead of drm_gem_object_put() and drm_gem_object_put_unlocked().
  194. *
  195. * Drivers should never call this directly in their code. Instead they should
  196. * wrap it up into a ``driver_gem_object_put(struct driver_gem_object *obj)``
  197. * wrapper function, and use that. Shared code should never call this, to
  198. * avoid breaking drivers by accident which still depend upon
  199. * &drm_device.struct_mutex locking.
  200. */
  201. static inline void
  202. __drm_gem_object_put(struct drm_gem_object *obj)
  203. {
  204. kref_put(&obj->refcount, drm_gem_object_free);
  205. }
  206. void drm_gem_object_put_unlocked(struct drm_gem_object *obj);
  207. void drm_gem_object_put(struct drm_gem_object *obj);
  208. /**
  209. * drm_gem_object_reference - acquire a GEM buffer object reference
  210. * @obj: GEM buffer object
  211. *
  212. * This is a compatibility alias for drm_gem_object_get() and should not be
  213. * used by new code.
  214. */
  215. static inline void drm_gem_object_reference(struct drm_gem_object *obj)
  216. {
  217. drm_gem_object_get(obj);
  218. }
  219. /**
  220. * __drm_gem_object_unreference - raw function to release a GEM buffer object
  221. * reference
  222. * @obj: GEM buffer object
  223. *
  224. * This is a compatibility alias for __drm_gem_object_put() and should not be
  225. * used by new code.
  226. */
  227. static inline void __drm_gem_object_unreference(struct drm_gem_object *obj)
  228. {
  229. __drm_gem_object_put(obj);
  230. }
  231. /**
  232. * drm_gem_object_unreference_unlocked - release a GEM buffer object reference
  233. * @obj: GEM buffer object
  234. *
  235. * This is a compatibility alias for drm_gem_object_put_unlocked() and should
  236. * not be used by new code.
  237. */
  238. static inline void
  239. drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
  240. {
  241. drm_gem_object_put_unlocked(obj);
  242. }
  243. /**
  244. * drm_gem_object_unreference - release a GEM buffer object reference
  245. * @obj: GEM buffer object
  246. *
  247. * This is a compatibility alias for drm_gem_object_put() and should not be
  248. * used by new code.
  249. */
  250. static inline void drm_gem_object_unreference(struct drm_gem_object *obj)
  251. {
  252. drm_gem_object_put(obj);
  253. }
  254. int drm_gem_handle_create(struct drm_file *file_priv,
  255. struct drm_gem_object *obj,
  256. u32 *handlep);
  257. int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
  258. void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
  259. int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
  260. int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
  261. struct page **drm_gem_get_pages(struct drm_gem_object *obj);
  262. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  263. bool dirty, bool accessed);
  264. struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
  265. int drm_gem_dumb_destroy(struct drm_file *file,
  266. struct drm_device *dev,
  267. uint32_t handle);
  268. #endif /* __DRM_GEM_H__ */