drm_gem.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. /**
  36. * struct drm_gem_object - GEM buffer object
  37. *
  38. * This structure defines the generic parts for GEM buffer objects, which are
  39. * mostly around handling mmap and userspace handles.
  40. *
  41. * Buffer objects are often abbreviated to BO.
  42. */
  43. struct drm_gem_object {
  44. /**
  45. * @refcount:
  46. *
  47. * Reference count of this object
  48. *
  49. * Please use drm_gem_object_reference() to acquire and
  50. * drm_gem_object_unreference() or drm_gem_object_unreference_unlocked()
  51. * to release a reference to a GEM buffer object.
  52. */
  53. struct kref refcount;
  54. /**
  55. * @handle_count:
  56. *
  57. * This is the GEM file_priv handle count of this object.
  58. *
  59. * Each handle also holds a reference. Note that when the handle_count
  60. * drops to 0 any global names (e.g. the id in the flink namespace) will
  61. * be cleared.
  62. *
  63. * Protected by dev->object_name_lock.
  64. */
  65. unsigned handle_count;
  66. /**
  67. * @dev: DRM dev this object belongs to.
  68. */
  69. struct drm_device *dev;
  70. /**
  71. * @filp:
  72. *
  73. * SHMEM file node used as backing storage for swappable buffer objects.
  74. * GEM also supports driver private objects with driver-specific backing
  75. * storage (contiguous CMA memory, special reserved blocks). In this
  76. * case @filp is NULL.
  77. */
  78. struct file *filp;
  79. /**
  80. * @vma_node:
  81. *
  82. * Mapping info for this object to support mmap. Drivers are supposed to
  83. * allocate the mmap offset using drm_gem_create_mmap_offset(). The
  84. * offset itself can be retrieved using drm_vma_node_offset_addr().
  85. *
  86. * Memory mapping itself is handled by drm_gem_mmap(), which also checks
  87. * that userspace is allowed to access the object.
  88. */
  89. struct drm_vma_offset_node vma_node;
  90. /**
  91. * @size:
  92. *
  93. * Size of the object, in bytes. Immutable over the object's
  94. * lifetime.
  95. */
  96. size_t size;
  97. /**
  98. * @name:
  99. *
  100. * Global name for this object, starts at 1. 0 means unnamed.
  101. * Access is covered by dev->object_name_lock. This is used by the GEM_FLINK
  102. * and GEM_OPEN ioctls.
  103. */
  104. int name;
  105. /**
  106. * @read_domains:
  107. *
  108. * Read memory domains. These monitor which caches contain read/write data
  109. * related to the object. When transitioning from one set of domains
  110. * to another, the driver is called to ensure that caches are suitably
  111. * flushed and invalidated.
  112. */
  113. uint32_t read_domains;
  114. /**
  115. * @write_domain: Corresponding unique write memory domain.
  116. */
  117. uint32_t write_domain;
  118. /**
  119. * @pending_read_domains:
  120. *
  121. * While validating an exec operation, the
  122. * new read/write domain values are computed here.
  123. * They will be transferred to the above values
  124. * at the point that any cache flushing occurs
  125. */
  126. uint32_t pending_read_domains;
  127. /**
  128. * @pending_write_domain: Write domain similar to @pending_read_domains.
  129. */
  130. uint32_t pending_write_domain;
  131. /**
  132. * @dma_buf:
  133. *
  134. * dma-buf associated with this GEM object.
  135. *
  136. * Pointer to the dma-buf associated with this gem object (either
  137. * through importing or exporting). We break the resulting reference
  138. * loop when the last gem handle for this object is released.
  139. *
  140. * Protected by obj->object_name_lock.
  141. */
  142. struct dma_buf *dma_buf;
  143. /**
  144. * @import_attach:
  145. *
  146. * dma-buf attachment backing this object.
  147. *
  148. * Any foreign dma_buf imported as a gem object has this set to the
  149. * attachment point for the device. This is invariant over the lifetime
  150. * of a gem object.
  151. *
  152. * The driver's ->gem_free_object callback is responsible for cleaning
  153. * up the dma_buf attachment and references acquired at import time.
  154. *
  155. * Note that the drm gem/prime core does not depend upon drivers setting
  156. * this field any more. So for drivers where this doesn't make sense
  157. * (e.g. virtual devices or a displaylink behind an usb bus) they can
  158. * simply leave it as NULL.
  159. */
  160. struct dma_buf_attachment *import_attach;
  161. };
  162. void drm_gem_object_release(struct drm_gem_object *obj);
  163. void drm_gem_object_free(struct kref *kref);
  164. int drm_gem_object_init(struct drm_device *dev,
  165. struct drm_gem_object *obj, size_t size);
  166. void drm_gem_private_object_init(struct drm_device *dev,
  167. struct drm_gem_object *obj, size_t size);
  168. void drm_gem_vm_open(struct vm_area_struct *vma);
  169. void drm_gem_vm_close(struct vm_area_struct *vma);
  170. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  171. struct vm_area_struct *vma);
  172. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
  173. /**
  174. * drm_gem_object_reference - acquire a GEM BO reference
  175. * @obj: GEM buffer object
  176. *
  177. * This acquires additional reference to @obj. It is illegal to call this
  178. * without already holding a reference. No locks required.
  179. */
  180. static inline void
  181. drm_gem_object_reference(struct drm_gem_object *obj)
  182. {
  183. kref_get(&obj->refcount);
  184. }
  185. /**
  186. * __drm_gem_object_unreference - raw function to release a GEM BO reference
  187. * @obj: GEM buffer object
  188. *
  189. * This function is meant to be used by drivers which are not encumbered with
  190. * dev->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_unreference() and
  193. * drm_gem_object_unreference_unlocked().
  194. *
  195. * Drivers should never call this directly in their code. Instead they should
  196. * wrap it up into a ``driver_gem_object_unreference(struct driver_gem_object
  197. * *obj)`` wrapper function, and use that. Shared code should never call this, to
  198. * avoid breaking drivers by accident which still depend upon dev->struct_mutex
  199. * locking.
  200. */
  201. static inline void
  202. __drm_gem_object_unreference(struct drm_gem_object *obj)
  203. {
  204. kref_put(&obj->refcount, drm_gem_object_free);
  205. }
  206. void drm_gem_object_unreference_unlocked(struct drm_gem_object *obj);
  207. void drm_gem_object_unreference(struct drm_gem_object *obj);
  208. int drm_gem_handle_create(struct drm_file *file_priv,
  209. struct drm_gem_object *obj,
  210. u32 *handlep);
  211. int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
  212. void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
  213. int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
  214. int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
  215. struct page **drm_gem_get_pages(struct drm_gem_object *obj);
  216. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  217. bool dirty, bool accessed);
  218. struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
  219. int drm_gem_dumb_destroy(struct drm_file *file,
  220. struct drm_device *dev,
  221. uint32_t handle);
  222. #endif /* __DRM_GEM_H__ */