drm_gem.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. * @dma_buf:
  122. *
  123. * dma-buf associated with this GEM object.
  124. *
  125. * Pointer to the dma-buf associated with this gem object (either
  126. * through importing or exporting). We break the resulting reference
  127. * loop when the last gem handle for this object is released.
  128. *
  129. * Protected by &drm_device.object_name_lock.
  130. */
  131. struct dma_buf *dma_buf;
  132. /**
  133. * @import_attach:
  134. *
  135. * dma-buf attachment backing this object.
  136. *
  137. * Any foreign dma_buf imported as a gem object has this set to the
  138. * attachment point for the device. This is invariant over the lifetime
  139. * of a gem object.
  140. *
  141. * The &drm_driver.gem_free_object callback is responsible for cleaning
  142. * up the dma_buf attachment and references acquired at import time.
  143. *
  144. * Note that the drm gem/prime core does not depend upon drivers setting
  145. * this field any more. So for drivers where this doesn't make sense
  146. * (e.g. virtual devices or a displaylink behind an usb bus) they can
  147. * simply leave it as NULL.
  148. */
  149. struct dma_buf_attachment *import_attach;
  150. };
  151. /**
  152. * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
  153. * @name: name for the generated structure
  154. *
  155. * This macro autogenerates a suitable &struct file_operations for GEM based
  156. * drivers, which can be assigned to &drm_driver.fops. Note that this structure
  157. * cannot be shared between drivers, because it contains a reference to the
  158. * current module using THIS_MODULE.
  159. *
  160. * Note that the declaration is already marked as static - if you need a
  161. * non-static version of this you're probably doing it wrong and will break the
  162. * THIS_MODULE reference by accident.
  163. */
  164. #define DEFINE_DRM_GEM_FOPS(name) \
  165. static const struct file_operations name = {\
  166. .owner = THIS_MODULE,\
  167. .open = drm_open,\
  168. .release = drm_release,\
  169. .unlocked_ioctl = drm_ioctl,\
  170. .compat_ioctl = drm_compat_ioctl,\
  171. .poll = drm_poll,\
  172. .read = drm_read,\
  173. .llseek = noop_llseek,\
  174. .mmap = drm_gem_mmap,\
  175. }
  176. void drm_gem_object_release(struct drm_gem_object *obj);
  177. void drm_gem_object_free(struct kref *kref);
  178. int drm_gem_object_init(struct drm_device *dev,
  179. struct drm_gem_object *obj, size_t size);
  180. void drm_gem_private_object_init(struct drm_device *dev,
  181. struct drm_gem_object *obj, size_t size);
  182. void drm_gem_vm_open(struct vm_area_struct *vma);
  183. void drm_gem_vm_close(struct vm_area_struct *vma);
  184. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  185. struct vm_area_struct *vma);
  186. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
  187. /**
  188. * drm_gem_object_get - acquire a GEM buffer object reference
  189. * @obj: GEM buffer object
  190. *
  191. * This function acquires an additional reference to @obj. It is illegal to
  192. * call this without already holding a reference. No locks required.
  193. */
  194. static inline void drm_gem_object_get(struct drm_gem_object *obj)
  195. {
  196. kref_get(&obj->refcount);
  197. }
  198. /**
  199. * __drm_gem_object_put - raw function to release a GEM buffer object reference
  200. * @obj: GEM buffer object
  201. *
  202. * This function is meant to be used by drivers which are not encumbered with
  203. * &drm_device.struct_mutex legacy locking and which are using the
  204. * gem_free_object_unlocked callback. It avoids all the locking checks and
  205. * locking overhead of drm_gem_object_put() and drm_gem_object_put_unlocked().
  206. *
  207. * Drivers should never call this directly in their code. Instead they should
  208. * wrap it up into a ``driver_gem_object_put(struct driver_gem_object *obj)``
  209. * wrapper function, and use that. Shared code should never call this, to
  210. * avoid breaking drivers by accident which still depend upon
  211. * &drm_device.struct_mutex locking.
  212. */
  213. static inline void
  214. __drm_gem_object_put(struct drm_gem_object *obj)
  215. {
  216. kref_put(&obj->refcount, drm_gem_object_free);
  217. }
  218. void drm_gem_object_put_unlocked(struct drm_gem_object *obj);
  219. void drm_gem_object_put(struct drm_gem_object *obj);
  220. /**
  221. * drm_gem_object_reference - acquire a GEM buffer object reference
  222. * @obj: GEM buffer object
  223. *
  224. * This is a compatibility alias for drm_gem_object_get() and should not be
  225. * used by new code.
  226. */
  227. static inline void drm_gem_object_reference(struct drm_gem_object *obj)
  228. {
  229. drm_gem_object_get(obj);
  230. }
  231. /**
  232. * __drm_gem_object_unreference - raw function to release a GEM buffer object
  233. * reference
  234. * @obj: GEM buffer object
  235. *
  236. * This is a compatibility alias for __drm_gem_object_put() and should not be
  237. * used by new code.
  238. */
  239. static inline void __drm_gem_object_unreference(struct drm_gem_object *obj)
  240. {
  241. __drm_gem_object_put(obj);
  242. }
  243. /**
  244. * drm_gem_object_unreference_unlocked - release a GEM buffer object reference
  245. * @obj: GEM buffer object
  246. *
  247. * This is a compatibility alias for drm_gem_object_put_unlocked() and should
  248. * not be used by new code.
  249. */
  250. static inline void
  251. drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
  252. {
  253. drm_gem_object_put_unlocked(obj);
  254. }
  255. /**
  256. * drm_gem_object_unreference - release a GEM buffer object reference
  257. * @obj: GEM buffer object
  258. *
  259. * This is a compatibility alias for drm_gem_object_put() and should not be
  260. * used by new code.
  261. */
  262. static inline void drm_gem_object_unreference(struct drm_gem_object *obj)
  263. {
  264. drm_gem_object_put(obj);
  265. }
  266. int drm_gem_handle_create(struct drm_file *file_priv,
  267. struct drm_gem_object *obj,
  268. u32 *handlep);
  269. int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
  270. void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
  271. int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
  272. int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
  273. struct page **drm_gem_get_pages(struct drm_gem_object *obj);
  274. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  275. bool dirty, bool accessed);
  276. struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
  277. int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  278. u32 handle, u64 *offset);
  279. int drm_gem_dumb_destroy(struct drm_file *file,
  280. struct drm_device *dev,
  281. uint32_t handle);
  282. #endif /* __DRM_GEM_H__ */