drm_gem.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /**
  165. * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
  166. * @name: name for the generated structure
  167. *
  168. * This macro autogenerates a suitable &struct file_operations for GEM based
  169. * drivers, which can be assigned to &drm_driver.fops. Note that this structure
  170. * cannot be shared between drivers, because it contains a reference to the
  171. * current module using THIS_MODULE.
  172. *
  173. * Note that the declaration is already marked as static - if you need a
  174. * non-static version of this you're probably doing it wrong and will break the
  175. * THIS_MODULE reference by accident.
  176. */
  177. #define DEFINE_DRM_GEM_FOPS(name) \
  178. static const struct file_operations name = {\
  179. .owner = THIS_MODULE,\
  180. .open = drm_open,\
  181. .release = drm_release,\
  182. .unlocked_ioctl = drm_ioctl,\
  183. .compat_ioctl = drm_compat_ioctl,\
  184. .poll = drm_poll,\
  185. .read = drm_read,\
  186. .llseek = noop_llseek,\
  187. .mmap = drm_gem_mmap,\
  188. }
  189. void drm_gem_object_release(struct drm_gem_object *obj);
  190. void drm_gem_object_free(struct kref *kref);
  191. int drm_gem_object_init(struct drm_device *dev,
  192. struct drm_gem_object *obj, size_t size);
  193. void drm_gem_private_object_init(struct drm_device *dev,
  194. struct drm_gem_object *obj, size_t size);
  195. void drm_gem_vm_open(struct vm_area_struct *vma);
  196. void drm_gem_vm_close(struct vm_area_struct *vma);
  197. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  198. struct vm_area_struct *vma);
  199. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
  200. /**
  201. * drm_gem_object_get - acquire a GEM buffer object reference
  202. * @obj: GEM buffer object
  203. *
  204. * This function acquires an additional reference to @obj. It is illegal to
  205. * call this without already holding a reference. No locks required.
  206. */
  207. static inline void drm_gem_object_get(struct drm_gem_object *obj)
  208. {
  209. kref_get(&obj->refcount);
  210. }
  211. /**
  212. * __drm_gem_object_put - raw function to release a GEM buffer object reference
  213. * @obj: GEM buffer object
  214. *
  215. * This function is meant to be used by drivers which are not encumbered with
  216. * &drm_device.struct_mutex legacy locking and which are using the
  217. * gem_free_object_unlocked callback. It avoids all the locking checks and
  218. * locking overhead of drm_gem_object_put() and drm_gem_object_put_unlocked().
  219. *
  220. * Drivers should never call this directly in their code. Instead they should
  221. * wrap it up into a ``driver_gem_object_put(struct driver_gem_object *obj)``
  222. * wrapper function, and use that. Shared code should never call this, to
  223. * avoid breaking drivers by accident which still depend upon
  224. * &drm_device.struct_mutex locking.
  225. */
  226. static inline void
  227. __drm_gem_object_put(struct drm_gem_object *obj)
  228. {
  229. kref_put(&obj->refcount, drm_gem_object_free);
  230. }
  231. void drm_gem_object_put_unlocked(struct drm_gem_object *obj);
  232. void drm_gem_object_put(struct drm_gem_object *obj);
  233. /**
  234. * drm_gem_object_reference - acquire a GEM buffer object reference
  235. * @obj: GEM buffer object
  236. *
  237. * This is a compatibility alias for drm_gem_object_get() and should not be
  238. * used by new code.
  239. */
  240. static inline void drm_gem_object_reference(struct drm_gem_object *obj)
  241. {
  242. drm_gem_object_get(obj);
  243. }
  244. /**
  245. * __drm_gem_object_unreference - raw function to release a GEM buffer object
  246. * reference
  247. * @obj: GEM buffer object
  248. *
  249. * This is a compatibility alias for __drm_gem_object_put() and should not be
  250. * used by new code.
  251. */
  252. static inline void __drm_gem_object_unreference(struct drm_gem_object *obj)
  253. {
  254. __drm_gem_object_put(obj);
  255. }
  256. /**
  257. * drm_gem_object_unreference_unlocked - release a GEM buffer object reference
  258. * @obj: GEM buffer object
  259. *
  260. * This is a compatibility alias for drm_gem_object_put_unlocked() and should
  261. * not be used by new code.
  262. */
  263. static inline void
  264. drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
  265. {
  266. drm_gem_object_put_unlocked(obj);
  267. }
  268. /**
  269. * drm_gem_object_unreference - release a GEM buffer object reference
  270. * @obj: GEM buffer object
  271. *
  272. * This is a compatibility alias for drm_gem_object_put() and should not be
  273. * used by new code.
  274. */
  275. static inline void drm_gem_object_unreference(struct drm_gem_object *obj)
  276. {
  277. drm_gem_object_put(obj);
  278. }
  279. int drm_gem_handle_create(struct drm_file *file_priv,
  280. struct drm_gem_object *obj,
  281. u32 *handlep);
  282. int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
  283. void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
  284. int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
  285. int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
  286. struct page **drm_gem_get_pages(struct drm_gem_object *obj);
  287. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  288. bool dirty, bool accessed);
  289. struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
  290. int drm_gem_dumb_destroy(struct drm_file *file,
  291. struct drm_device *dev,
  292. uint32_t handle);
  293. #endif /* __DRM_GEM_H__ */