drm_gem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 <[email protected]>
  12. *
  13. * Author: Rickard E. (Rik) Faith <[email protected]>
  14. * Author: Gareth Hughes <[email protected]>
  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 <linux/dma-resv.h>
  37. #include <drm/drm_vma_manager.h>
  38. struct iosys_map;
  39. struct drm_gem_object;
  40. /**
  41. * struct drm_gem_object_funcs - GEM object functions
  42. */
  43. struct drm_gem_object_funcs {
  44. /**
  45. * @free:
  46. *
  47. * Deconstructor for drm_gem_objects.
  48. *
  49. * This callback is mandatory.
  50. */
  51. void (*free)(struct drm_gem_object *obj);
  52. /**
  53. * @open:
  54. *
  55. * Called upon GEM handle creation.
  56. *
  57. * This callback is optional.
  58. */
  59. int (*open)(struct drm_gem_object *obj, struct drm_file *file);
  60. /**
  61. * @close:
  62. *
  63. * Called upon GEM handle release.
  64. *
  65. * This callback is optional.
  66. */
  67. void (*close)(struct drm_gem_object *obj, struct drm_file *file);
  68. /**
  69. * @print_info:
  70. *
  71. * If driver subclasses struct &drm_gem_object, it can implement this
  72. * optional hook for printing additional driver specific info.
  73. *
  74. * drm_printf_indent() should be used in the callback passing it the
  75. * indent argument.
  76. *
  77. * This callback is called from drm_gem_print_info().
  78. *
  79. * This callback is optional.
  80. */
  81. void (*print_info)(struct drm_printer *p, unsigned int indent,
  82. const struct drm_gem_object *obj);
  83. /**
  84. * @export:
  85. *
  86. * Export backing buffer as a &dma_buf.
  87. * If this is not set drm_gem_prime_export() is used.
  88. *
  89. * This callback is optional.
  90. */
  91. struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);
  92. /**
  93. * @pin:
  94. *
  95. * Pin backing buffer in memory. Used by the drm_gem_map_attach() helper.
  96. *
  97. * This callback is optional.
  98. */
  99. int (*pin)(struct drm_gem_object *obj);
  100. /**
  101. * @unpin:
  102. *
  103. * Unpin backing buffer. Used by the drm_gem_map_detach() helper.
  104. *
  105. * This callback is optional.
  106. */
  107. void (*unpin)(struct drm_gem_object *obj);
  108. /**
  109. * @get_sg_table:
  110. *
  111. * Returns a Scatter-Gather table representation of the buffer.
  112. * Used when exporting a buffer by the drm_gem_map_dma_buf() helper.
  113. * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()
  114. * in drm_gem_unmap_buf(), therefore these helpers and this callback
  115. * here cannot be used for sg tables pointing at driver private memory
  116. * ranges.
  117. *
  118. * See also drm_prime_pages_to_sg().
  119. */
  120. struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);
  121. /**
  122. * @vmap:
  123. *
  124. * Returns a virtual address for the buffer. Used by the
  125. * drm_gem_dmabuf_vmap() helper.
  126. *
  127. * This callback is optional.
  128. */
  129. int (*vmap)(struct drm_gem_object *obj, struct iosys_map *map);
  130. /**
  131. * @vunmap:
  132. *
  133. * Releases the address previously returned by @vmap. Used by the
  134. * drm_gem_dmabuf_vunmap() helper.
  135. *
  136. * This callback is optional.
  137. */
  138. void (*vunmap)(struct drm_gem_object *obj, struct iosys_map *map);
  139. /**
  140. * @mmap:
  141. *
  142. * Handle mmap() of the gem object, setup vma accordingly.
  143. *
  144. * This callback is optional.
  145. *
  146. * The callback is used by both drm_gem_mmap_obj() and
  147. * drm_gem_prime_mmap(). When @mmap is present @vm_ops is not
  148. * used, the @mmap callback must set vma->vm_ops instead.
  149. */
  150. int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
  151. /**
  152. * @vm_ops:
  153. *
  154. * Virtual memory operations used with mmap.
  155. *
  156. * This is optional but necessary for mmap support.
  157. */
  158. const struct vm_operations_struct *vm_ops;
  159. };
  160. /**
  161. * struct drm_gem_lru - A simple LRU helper
  162. *
  163. * A helper for tracking GEM objects in a given state, to aid in
  164. * driver's shrinker implementation. Tracks the count of pages
  165. * for lockless &shrinker.count_objects, and provides
  166. * &drm_gem_lru_scan for driver's &shrinker.scan_objects
  167. * implementation.
  168. */
  169. struct drm_gem_lru {
  170. /**
  171. * @lock:
  172. *
  173. * Lock protecting movement of GEM objects between LRUs. All
  174. * LRUs that the object can move between should be protected
  175. * by the same lock.
  176. */
  177. struct mutex *lock;
  178. /**
  179. * @count:
  180. *
  181. * The total number of backing pages of the GEM objects in
  182. * this LRU.
  183. */
  184. long count;
  185. /**
  186. * @list:
  187. *
  188. * The LRU list.
  189. */
  190. struct list_head list;
  191. };
  192. /**
  193. * struct drm_gem_object - GEM buffer object
  194. *
  195. * This structure defines the generic parts for GEM buffer objects, which are
  196. * mostly around handling mmap and userspace handles.
  197. *
  198. * Buffer objects are often abbreviated to BO.
  199. */
  200. struct drm_gem_object {
  201. /**
  202. * @refcount:
  203. *
  204. * Reference count of this object
  205. *
  206. * Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked()
  207. * or drm_gem_object_put() to release a reference to a GEM
  208. * buffer object.
  209. */
  210. struct kref refcount;
  211. /**
  212. * @handle_count:
  213. *
  214. * This is the GEM file_priv handle count of this object.
  215. *
  216. * Each handle also holds a reference. Note that when the handle_count
  217. * drops to 0 any global names (e.g. the id in the flink namespace) will
  218. * be cleared.
  219. *
  220. * Protected by &drm_device.object_name_lock.
  221. */
  222. unsigned handle_count;
  223. /**
  224. * @dev: DRM dev this object belongs to.
  225. */
  226. struct drm_device *dev;
  227. /**
  228. * @filp:
  229. *
  230. * SHMEM file node used as backing storage for swappable buffer objects.
  231. * GEM also supports driver private objects with driver-specific backing
  232. * storage (contiguous DMA memory, special reserved blocks). In this
  233. * case @filp is NULL.
  234. */
  235. struct file *filp;
  236. /**
  237. * @vma_node:
  238. *
  239. * Mapping info for this object to support mmap. Drivers are supposed to
  240. * allocate the mmap offset using drm_gem_create_mmap_offset(). The
  241. * offset itself can be retrieved using drm_vma_node_offset_addr().
  242. *
  243. * Memory mapping itself is handled by drm_gem_mmap(), which also checks
  244. * that userspace is allowed to access the object.
  245. */
  246. struct drm_vma_offset_node vma_node;
  247. /**
  248. * @size:
  249. *
  250. * Size of the object, in bytes. Immutable over the object's
  251. * lifetime.
  252. */
  253. size_t size;
  254. /**
  255. * @name:
  256. *
  257. * Global name for this object, starts at 1. 0 means unnamed.
  258. * Access is covered by &drm_device.object_name_lock. This is used by
  259. * the GEM_FLINK and GEM_OPEN ioctls.
  260. */
  261. int name;
  262. /**
  263. * @dma_buf:
  264. *
  265. * dma-buf associated with this GEM object.
  266. *
  267. * Pointer to the dma-buf associated with this gem object (either
  268. * through importing or exporting). We break the resulting reference
  269. * loop when the last gem handle for this object is released.
  270. *
  271. * Protected by &drm_device.object_name_lock.
  272. */
  273. struct dma_buf *dma_buf;
  274. /**
  275. * @import_attach:
  276. *
  277. * dma-buf attachment backing this object.
  278. *
  279. * Any foreign dma_buf imported as a gem object has this set to the
  280. * attachment point for the device. This is invariant over the lifetime
  281. * of a gem object.
  282. *
  283. * The &drm_gem_object_funcs.free callback is responsible for
  284. * cleaning up the dma_buf attachment and references acquired at import
  285. * time.
  286. *
  287. * Note that the drm gem/prime core does not depend upon drivers setting
  288. * this field any more. So for drivers where this doesn't make sense
  289. * (e.g. virtual devices or a displaylink behind an usb bus) they can
  290. * simply leave it as NULL.
  291. */
  292. struct dma_buf_attachment *import_attach;
  293. /**
  294. * @resv:
  295. *
  296. * Pointer to reservation object associated with the this GEM object.
  297. *
  298. * Normally (@resv == &@_resv) except for imported GEM objects.
  299. */
  300. struct dma_resv *resv;
  301. /**
  302. * @_resv:
  303. *
  304. * A reservation object for this GEM object.
  305. *
  306. * This is unused for imported GEM objects.
  307. */
  308. struct dma_resv _resv;
  309. /**
  310. * @funcs:
  311. *
  312. * Optional GEM object functions. If this is set, it will be used instead of the
  313. * corresponding &drm_driver GEM callbacks.
  314. *
  315. * New drivers should use this.
  316. *
  317. */
  318. const struct drm_gem_object_funcs *funcs;
  319. /**
  320. * @lru_node:
  321. *
  322. * List node in a &drm_gem_lru.
  323. */
  324. struct list_head lru_node;
  325. /**
  326. * @lru:
  327. *
  328. * The current LRU list that the GEM object is on.
  329. */
  330. struct drm_gem_lru *lru;
  331. };
  332. /**
  333. * DRM_GEM_FOPS - Default drm GEM file operations
  334. *
  335. * This macro provides a shorthand for setting the GEM file ops in the
  336. * &file_operations structure. If all you need are the default ops, use
  337. * DEFINE_DRM_GEM_FOPS instead.
  338. */
  339. #define DRM_GEM_FOPS \
  340. .open = drm_open,\
  341. .release = drm_release,\
  342. .unlocked_ioctl = drm_ioctl,\
  343. .compat_ioctl = drm_compat_ioctl,\
  344. .poll = drm_poll,\
  345. .read = drm_read,\
  346. .llseek = noop_llseek,\
  347. .mmap = drm_gem_mmap
  348. /**
  349. * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
  350. * @name: name for the generated structure
  351. *
  352. * This macro autogenerates a suitable &struct file_operations for GEM based
  353. * drivers, which can be assigned to &drm_driver.fops. Note that this structure
  354. * cannot be shared between drivers, because it contains a reference to the
  355. * current module using THIS_MODULE.
  356. *
  357. * Note that the declaration is already marked as static - if you need a
  358. * non-static version of this you're probably doing it wrong and will break the
  359. * THIS_MODULE reference by accident.
  360. */
  361. #define DEFINE_DRM_GEM_FOPS(name) \
  362. static const struct file_operations name = {\
  363. .owner = THIS_MODULE,\
  364. DRM_GEM_FOPS,\
  365. }
  366. void drm_gem_object_release(struct drm_gem_object *obj);
  367. void drm_gem_object_free(struct kref *kref);
  368. int drm_gem_object_init(struct drm_device *dev,
  369. struct drm_gem_object *obj, size_t size);
  370. void drm_gem_private_object_init(struct drm_device *dev,
  371. struct drm_gem_object *obj, size_t size);
  372. void drm_gem_vm_open(struct vm_area_struct *vma);
  373. void drm_gem_vm_close(struct vm_area_struct *vma);
  374. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  375. struct vm_area_struct *vma);
  376. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
  377. /**
  378. * drm_gem_object_get - acquire a GEM buffer object reference
  379. * @obj: GEM buffer object
  380. *
  381. * This function acquires an additional reference to @obj. It is illegal to
  382. * call this without already holding a reference. No locks required.
  383. */
  384. static inline void drm_gem_object_get(struct drm_gem_object *obj)
  385. {
  386. kref_get(&obj->refcount);
  387. }
  388. __attribute__((nonnull))
  389. static inline void
  390. __drm_gem_object_put(struct drm_gem_object *obj)
  391. {
  392. kref_put(&obj->refcount, drm_gem_object_free);
  393. }
  394. /**
  395. * drm_gem_object_put - drop a GEM buffer object reference
  396. * @obj: GEM buffer object
  397. *
  398. * This releases a reference to @obj.
  399. */
  400. static inline void
  401. drm_gem_object_put(struct drm_gem_object *obj)
  402. {
  403. if (obj)
  404. __drm_gem_object_put(obj);
  405. }
  406. int drm_gem_handle_create(struct drm_file *file_priv,
  407. struct drm_gem_object *obj,
  408. u32 *handlep);
  409. int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
  410. void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
  411. int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
  412. int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
  413. struct page **drm_gem_get_pages(struct drm_gem_object *obj);
  414. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  415. bool dirty, bool accessed);
  416. int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
  417. int count, struct drm_gem_object ***objs_out);
  418. struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
  419. long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
  420. bool wait_all, unsigned long timeout);
  421. int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
  422. struct ww_acquire_ctx *acquire_ctx);
  423. void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
  424. struct ww_acquire_ctx *acquire_ctx);
  425. int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  426. u32 handle, u64 *offset);
  427. void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);
  428. void drm_gem_lru_remove(struct drm_gem_object *obj);
  429. void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);
  430. unsigned long drm_gem_lru_scan(struct drm_gem_lru *lru,
  431. unsigned int nr_to_scan,
  432. unsigned long *remaining,
  433. bool (*shrink)(struct drm_gem_object *obj));
  434. #endif /* __DRM_GEM_H__ */