drm_gem.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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 <linux/dma-buf.h>
  37. #include <linux/dma-resv.h>
  38. #include <linux/list.h>
  39. #include <linux/mutex.h>
  40. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  41. #include <drm/drm_device.h>
  42. #endif
  43. #include <drm/drm_vma_manager.h>
  44. struct iosys_map;
  45. struct drm_gem_object;
  46. /**
  47. * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
  48. * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
  49. * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
  50. * @DRM_GEM_OBJECT_ACTIVE: object is currently used by an active submission
  51. *
  52. * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
  53. * and drm_show_fdinfo(). Note that an object can report DRM_GEM_OBJECT_PURGEABLE
  54. * and be active or not resident, in which case drm_show_fdinfo() will not
  55. * account for it as purgeable. So drivers do not need to check if the buffer
  56. * is idle and resident to return this bit, i.e. userspace can mark a buffer as
  57. * purgeable even while it is still busy on the GPU. It will not get reported in
  58. * the puregeable stats until it becomes idle. The status gem object func does
  59. * not need to consider this.
  60. */
  61. enum drm_gem_object_status {
  62. DRM_GEM_OBJECT_RESIDENT = BIT(0),
  63. DRM_GEM_OBJECT_PURGEABLE = BIT(1),
  64. DRM_GEM_OBJECT_ACTIVE = BIT(2),
  65. };
  66. /**
  67. * struct drm_gem_object_funcs - GEM object functions
  68. */
  69. struct drm_gem_object_funcs {
  70. /**
  71. * @free:
  72. *
  73. * Deconstructor for drm_gem_objects.
  74. *
  75. * This callback is mandatory.
  76. */
  77. void (*free)(struct drm_gem_object *obj);
  78. /**
  79. * @open:
  80. *
  81. * Called upon GEM handle creation.
  82. *
  83. * This callback is optional.
  84. */
  85. int (*open)(struct drm_gem_object *obj, struct drm_file *file);
  86. /**
  87. * @close:
  88. *
  89. * Called upon GEM handle release.
  90. *
  91. * This callback is optional.
  92. */
  93. void (*close)(struct drm_gem_object *obj, struct drm_file *file);
  94. /**
  95. * @print_info:
  96. *
  97. * If driver subclasses struct &drm_gem_object, it can implement this
  98. * optional hook for printing additional driver specific info.
  99. *
  100. * drm_printf_indent() should be used in the callback passing it the
  101. * indent argument.
  102. *
  103. * This callback is called from drm_gem_print_info().
  104. *
  105. * This callback is optional.
  106. */
  107. void (*print_info)(struct drm_printer *p, unsigned int indent,
  108. const struct drm_gem_object *obj);
  109. /**
  110. * @export:
  111. *
  112. * Export backing buffer as a &dma_buf.
  113. * If this is not set drm_gem_prime_export() is used.
  114. *
  115. * This callback is optional.
  116. */
  117. struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);
  118. /**
  119. * @pin:
  120. *
  121. * Pin backing buffer in memory, such that dma-buf importers can
  122. * access it. Used by the drm_gem_map_attach() helper.
  123. *
  124. * This callback is optional.
  125. */
  126. int (*pin)(struct drm_gem_object *obj);
  127. /**
  128. * @unpin:
  129. *
  130. * Unpin backing buffer. Used by the drm_gem_map_detach() helper.
  131. *
  132. * This callback is optional.
  133. */
  134. void (*unpin)(struct drm_gem_object *obj);
  135. /**
  136. * @get_sg_table:
  137. *
  138. * Returns a Scatter-Gather table representation of the buffer.
  139. * Used when exporting a buffer by the drm_gem_map_dma_buf() helper.
  140. * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()
  141. * in drm_gem_unmap_buf(), therefore these helpers and this callback
  142. * here cannot be used for sg tables pointing at driver private memory
  143. * ranges.
  144. *
  145. * See also drm_prime_pages_to_sg().
  146. */
  147. struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);
  148. /**
  149. * @vmap:
  150. *
  151. * Returns a virtual address for the buffer. Used by the
  152. * drm_gem_dmabuf_vmap() helper. Called with a held GEM reservation
  153. * lock.
  154. *
  155. * This callback is optional.
  156. */
  157. int (*vmap)(struct drm_gem_object *obj, struct iosys_map *map);
  158. /**
  159. * @vunmap:
  160. *
  161. * Releases the address previously returned by @vmap. Used by the
  162. * drm_gem_dmabuf_vunmap() helper. Called with a held GEM reservation
  163. * lock.
  164. *
  165. * This callback is optional.
  166. */
  167. void (*vunmap)(struct drm_gem_object *obj, struct iosys_map *map);
  168. /**
  169. * @mmap:
  170. *
  171. * Handle mmap() of the gem object, setup vma accordingly.
  172. *
  173. * This callback is optional.
  174. *
  175. * The callback is used by both drm_gem_mmap_obj() and
  176. * drm_gem_prime_mmap(). When @mmap is present @vm_ops is not
  177. * used, the @mmap callback must set vma->vm_ops instead.
  178. */
  179. int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
  180. /**
  181. * @evict:
  182. *
  183. * Evicts gem object out from memory. Used by the drm_gem_object_evict()
  184. * helper. Returns 0 on success, -errno otherwise. Called with a held
  185. * GEM reservation lock.
  186. *
  187. * This callback is optional.
  188. */
  189. int (*evict)(struct drm_gem_object *obj);
  190. /**
  191. * @status:
  192. *
  193. * The optional status callback can return additional object state
  194. * which determines which stats the object is counted against. The
  195. * callback is called under table_lock. Racing against object status
  196. * change is "harmless", and the callback can expect to not race
  197. * against object destruction.
  198. *
  199. * Called by drm_show_memory_stats().
  200. */
  201. enum drm_gem_object_status (*status)(struct drm_gem_object *obj);
  202. /**
  203. * @rss:
  204. *
  205. * Return resident size of the object in physical memory.
  206. *
  207. * Called by drm_show_memory_stats().
  208. */
  209. size_t (*rss)(struct drm_gem_object *obj);
  210. /**
  211. * @vm_ops:
  212. *
  213. * Virtual memory operations used with mmap.
  214. *
  215. * This is optional but necessary for mmap support.
  216. */
  217. const struct vm_operations_struct *vm_ops;
  218. };
  219. /**
  220. * struct drm_gem_lru - A simple LRU helper
  221. *
  222. * A helper for tracking GEM objects in a given state, to aid in
  223. * driver's shrinker implementation. Tracks the count of pages
  224. * for lockless &shrinker.count_objects, and provides
  225. * &drm_gem_lru_scan for driver's &shrinker.scan_objects
  226. * implementation.
  227. */
  228. struct drm_gem_lru {
  229. /**
  230. * @lock:
  231. *
  232. * Lock protecting movement of GEM objects between LRUs. All
  233. * LRUs that the object can move between should be protected
  234. * by the same lock.
  235. */
  236. struct mutex *lock;
  237. /**
  238. * @count:
  239. *
  240. * The total number of backing pages of the GEM objects in
  241. * this LRU.
  242. */
  243. long count;
  244. /**
  245. * @list:
  246. *
  247. * The LRU list.
  248. */
  249. struct list_head list;
  250. };
  251. /**
  252. * struct drm_gem_object - GEM buffer object
  253. *
  254. * This structure defines the generic parts for GEM buffer objects, which are
  255. * mostly around handling mmap and userspace handles.
  256. *
  257. * Buffer objects are often abbreviated to BO.
  258. */
  259. struct drm_gem_object {
  260. /**
  261. * @refcount:
  262. *
  263. * Reference count of this object
  264. *
  265. * Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked()
  266. * or drm_gem_object_put() to release a reference to a GEM
  267. * buffer object.
  268. */
  269. struct kref refcount;
  270. /**
  271. * @handle_count:
  272. *
  273. * This is the GEM file_priv handle count of this object.
  274. *
  275. * Each handle also holds a reference. Note that when the handle_count
  276. * drops to 0 any global names (e.g. the id in the flink namespace) will
  277. * be cleared.
  278. *
  279. * Protected by &drm_device.object_name_lock.
  280. */
  281. unsigned handle_count;
  282. /**
  283. * @dev: DRM dev this object belongs to.
  284. */
  285. struct drm_device *dev;
  286. /**
  287. * @filp:
  288. *
  289. * SHMEM file node used as backing storage for swappable buffer objects.
  290. * GEM also supports driver private objects with driver-specific backing
  291. * storage (contiguous DMA memory, special reserved blocks). In this
  292. * case @filp is NULL.
  293. */
  294. struct file *filp;
  295. /**
  296. * @vma_node:
  297. *
  298. * Mapping info for this object to support mmap. Drivers are supposed to
  299. * allocate the mmap offset using drm_gem_create_mmap_offset(). The
  300. * offset itself can be retrieved using drm_vma_node_offset_addr().
  301. *
  302. * Memory mapping itself is handled by drm_gem_mmap(), which also checks
  303. * that userspace is allowed to access the object.
  304. */
  305. struct drm_vma_offset_node vma_node;
  306. /**
  307. * @size:
  308. *
  309. * Size of the object, in bytes. Immutable over the object's
  310. * lifetime.
  311. */
  312. size_t size;
  313. /**
  314. * @name:
  315. *
  316. * Global name for this object, starts at 1. 0 means unnamed.
  317. * Access is covered by &drm_device.object_name_lock. This is used by
  318. * the GEM_FLINK and GEM_OPEN ioctls.
  319. */
  320. int name;
  321. /**
  322. * @dma_buf:
  323. *
  324. * dma-buf associated with this GEM object.
  325. *
  326. * Pointer to the dma-buf associated with this gem object (either
  327. * through importing or exporting). We break the resulting reference
  328. * loop when the last gem handle for this object is released.
  329. *
  330. * Protected by &drm_device.object_name_lock.
  331. */
  332. struct dma_buf *dma_buf;
  333. /**
  334. * @import_attach:
  335. *
  336. * dma-buf attachment backing this object.
  337. *
  338. * Any foreign dma_buf imported as a gem object has this set to the
  339. * attachment point for the device. This is invariant over the lifetime
  340. * of a gem object.
  341. *
  342. * The &drm_gem_object_funcs.free callback is responsible for
  343. * cleaning up the dma_buf attachment and references acquired at import
  344. * time.
  345. *
  346. * Note that the drm gem/prime core does not depend upon drivers setting
  347. * this field any more. So for drivers where this doesn't make sense
  348. * (e.g. virtual devices or a displaylink behind an usb bus) they can
  349. * simply leave it as NULL.
  350. */
  351. struct dma_buf_attachment *import_attach;
  352. /**
  353. * @resv:
  354. *
  355. * Pointer to reservation object associated with the this GEM object.
  356. *
  357. * Normally (@resv == &@_resv) except for imported GEM objects.
  358. */
  359. struct dma_resv *resv;
  360. /**
  361. * @_resv:
  362. *
  363. * A reservation object for this GEM object.
  364. *
  365. * This is unused for imported GEM objects.
  366. */
  367. struct dma_resv _resv;
  368. /**
  369. * @gpuva: Fields used by GPUVM to manage mappings pointing to this GEM object.
  370. *
  371. * When DRM_GPUVM_IMMEDIATE_MODE is set, this list is protected by the
  372. * mutex. Otherwise, the list is protected by the GEMs &dma_resv lock.
  373. *
  374. * Note that all entries in this list must agree on whether
  375. * DRM_GPUVM_IMMEDIATE_MODE is set.
  376. */
  377. struct {
  378. /**
  379. * @gpuva.list: list of GPUVM mappings attached to this GEM object.
  380. *
  381. * Drivers should lock list accesses with either the GEMs
  382. * &dma_resv lock (&drm_gem_object.resv) or the
  383. * &drm_gem_object.gpuva.lock mutex.
  384. */
  385. struct list_head list;
  386. /**
  387. * @gpuva.lock: lock protecting access to &drm_gem_object.gpuva.list
  388. * when DRM_GPUVM_IMMEDIATE_MODE is used.
  389. *
  390. * Only used when DRM_GPUVM_IMMEDIATE_MODE is set. It should be
  391. * safe to take this mutex during the fence signalling path, so
  392. * do not allocate memory while holding this lock. Otherwise,
  393. * the &dma_resv lock should be used.
  394. */
  395. struct mutex lock;
  396. } gpuva;
  397. /**
  398. * @funcs:
  399. *
  400. * Optional GEM object functions. If this is set, it will be used instead of the
  401. * corresponding &drm_driver GEM callbacks.
  402. *
  403. * New drivers should use this.
  404. *
  405. */
  406. const struct drm_gem_object_funcs *funcs;
  407. /**
  408. * @lru_node:
  409. *
  410. * List node in a &drm_gem_lru.
  411. */
  412. struct list_head lru_node;
  413. /**
  414. * @lru:
  415. *
  416. * The current LRU list that the GEM object is on.
  417. */
  418. struct drm_gem_lru *lru;
  419. };
  420. /**
  421. * DRM_GEM_FOPS - Default drm GEM file operations
  422. *
  423. * This macro provides a shorthand for setting the GEM file ops in the
  424. * &file_operations structure. If all you need are the default ops, use
  425. * DEFINE_DRM_GEM_FOPS instead.
  426. */
  427. #define DRM_GEM_FOPS \
  428. .open = drm_open,\
  429. .release = drm_release,\
  430. .unlocked_ioctl = drm_ioctl,\
  431. .compat_ioctl = drm_compat_ioctl,\
  432. .poll = drm_poll,\
  433. .read = drm_read,\
  434. .llseek = noop_llseek,\
  435. .get_unmapped_area = drm_gem_get_unmapped_area,\
  436. .mmap = drm_gem_mmap, \
  437. .fop_flags = FOP_UNSIGNED_OFFSET
  438. /**
  439. * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
  440. * @name: name for the generated structure
  441. *
  442. * This macro autogenerates a suitable &struct file_operations for GEM based
  443. * drivers, which can be assigned to &drm_driver.fops. Note that this structure
  444. * cannot be shared between drivers, because it contains a reference to the
  445. * current module using THIS_MODULE.
  446. *
  447. * Note that the declaration is already marked as static - if you need a
  448. * non-static version of this you're probably doing it wrong and will break the
  449. * THIS_MODULE reference by accident.
  450. */
  451. #define DEFINE_DRM_GEM_FOPS(name) \
  452. static const struct file_operations name = {\
  453. .owner = THIS_MODULE,\
  454. DRM_GEM_FOPS,\
  455. }
  456. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  457. int drm_gem_huge_mnt_create(struct drm_device *dev, const char *value);
  458. #else
  459. static inline int drm_gem_huge_mnt_create(struct drm_device *dev,
  460. const char *value)
  461. {
  462. return 0;
  463. }
  464. #endif
  465. /**
  466. * drm_gem_get_huge_mnt - Get the huge tmpfs mountpoint used by a DRM device
  467. * @dev: DRM device
  468. *
  469. * This function gets the huge tmpfs mountpoint used by DRM device @dev. A huge
  470. * tmpfs mountpoint is used instead of `shm_mnt` after a successful call to
  471. * drm_gem_huge_mnt_create() when CONFIG_TRANSPARENT_HUGEPAGE is enabled.
  472. *
  473. * Returns:
  474. * The huge tmpfs mountpoint in use, NULL otherwise.
  475. */
  476. static inline struct vfsmount *drm_gem_get_huge_mnt(struct drm_device *dev)
  477. {
  478. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  479. return dev->huge_mnt;
  480. #else
  481. return NULL;
  482. #endif
  483. }
  484. void drm_gem_object_release(struct drm_gem_object *obj);
  485. void drm_gem_object_free(struct kref *kref);
  486. int drm_gem_object_init(struct drm_device *dev,
  487. struct drm_gem_object *obj, size_t size);
  488. void drm_gem_private_object_init(struct drm_device *dev,
  489. struct drm_gem_object *obj, size_t size);
  490. void drm_gem_private_object_fini(struct drm_gem_object *obj);
  491. void drm_gem_vm_open(struct vm_area_struct *vma);
  492. void drm_gem_vm_close(struct vm_area_struct *vma);
  493. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  494. struct vm_area_struct *vma);
  495. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
  496. #ifdef CONFIG_MMU
  497. unsigned long drm_gem_get_unmapped_area(struct file *filp, unsigned long uaddr,
  498. unsigned long len, unsigned long pgoff,
  499. unsigned long flags);
  500. #else
  501. #define drm_gem_get_unmapped_area NULL
  502. #endif
  503. /**
  504. * drm_gem_object_get - acquire a GEM buffer object reference
  505. * @obj: GEM buffer object
  506. *
  507. * This function acquires an additional reference to @obj. It is illegal to
  508. * call this without already holding a reference. No locks required.
  509. */
  510. static inline void drm_gem_object_get(struct drm_gem_object *obj)
  511. {
  512. kref_get(&obj->refcount);
  513. }
  514. __attribute__((nonnull))
  515. static inline void
  516. __drm_gem_object_put(struct drm_gem_object *obj)
  517. {
  518. kref_put(&obj->refcount, drm_gem_object_free);
  519. }
  520. /**
  521. * drm_gem_object_put - drop a GEM buffer object reference
  522. * @obj: GEM buffer object
  523. *
  524. * This releases a reference to @obj.
  525. */
  526. static inline void
  527. drm_gem_object_put(struct drm_gem_object *obj)
  528. {
  529. if (obj)
  530. __drm_gem_object_put(obj);
  531. }
  532. int drm_gem_handle_create(struct drm_file *file_priv,
  533. struct drm_gem_object *obj,
  534. u32 *handlep);
  535. int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
  536. void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
  537. int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
  538. int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
  539. struct page **drm_gem_get_pages(struct drm_gem_object *obj);
  540. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  541. bool dirty, bool accessed);
  542. void drm_gem_lock(struct drm_gem_object *obj);
  543. void drm_gem_unlock(struct drm_gem_object *obj);
  544. int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map);
  545. void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map);
  546. int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
  547. int count, struct drm_gem_object ***objs_out);
  548. struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
  549. long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
  550. bool wait_all, unsigned long timeout);
  551. int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
  552. struct ww_acquire_ctx *acquire_ctx);
  553. void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
  554. struct ww_acquire_ctx *acquire_ctx);
  555. int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  556. u32 handle, u64 *offset);
  557. void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);
  558. void drm_gem_lru_remove(struct drm_gem_object *obj);
  559. void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj);
  560. void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);
  561. unsigned long
  562. drm_gem_lru_scan(struct drm_gem_lru *lru,
  563. unsigned int nr_to_scan,
  564. unsigned long *remaining,
  565. bool (*shrink)(struct drm_gem_object *obj, struct ww_acquire_ctx *ticket),
  566. struct ww_acquire_ctx *ticket);
  567. int drm_gem_evict_locked(struct drm_gem_object *obj);
  568. /**
  569. * drm_gem_object_is_shared_for_memory_stats - helper for shared memory stats
  570. *
  571. * This helper should only be used for fdinfo shared memory stats to determine
  572. * if a GEM object is shared.
  573. *
  574. * @obj: obj in question
  575. */
  576. static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object *obj)
  577. {
  578. return (obj->handle_count > 1) || obj->dma_buf;
  579. }
  580. /**
  581. * drm_gem_is_imported() - Tests if GEM object's buffer has been imported
  582. * @obj: the GEM object
  583. *
  584. * Returns:
  585. * True if the GEM object's buffer has been imported, false otherwise
  586. */
  587. static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
  588. {
  589. return !!obj->import_attach;
  590. }
  591. #ifdef CONFIG_LOCKDEP
  592. #define drm_gem_gpuva_assert_lock_held(gpuvm, obj) \
  593. lockdep_assert(drm_gpuvm_immediate_mode(gpuvm) ? \
  594. lockdep_is_held(&(obj)->gpuva.lock) : \
  595. dma_resv_held((obj)->resv))
  596. #else
  597. #define drm_gem_gpuva_assert_lock_held(gpuvm, obj) do {} while (0)
  598. #endif
  599. /**
  600. * drm_gem_gpuva_init() - initialize the gpuva list of a GEM object
  601. * @obj: the &drm_gem_object
  602. *
  603. * This initializes the &drm_gem_object's &drm_gpuvm_bo list.
  604. *
  605. * Calling this function is only necessary for drivers intending to support the
  606. * &drm_driver_feature DRIVER_GEM_GPUVA.
  607. *
  608. * See also drm_gem_gpuva_set_lock().
  609. */
  610. static inline void drm_gem_gpuva_init(struct drm_gem_object *obj)
  611. {
  612. INIT_LIST_HEAD(&obj->gpuva.list);
  613. }
  614. /**
  615. * drm_gem_for_each_gpuvm_bo() - iterator to walk over a list of &drm_gpuvm_bo
  616. * @entry__: &drm_gpuvm_bo structure to assign to in each iteration step
  617. * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
  618. *
  619. * This iterator walks over all &drm_gpuvm_bo structures associated with the
  620. * &drm_gem_object.
  621. */
  622. #define drm_gem_for_each_gpuvm_bo(entry__, obj__) \
  623. list_for_each_entry(entry__, &(obj__)->gpuva.list, list.entry.gem)
  624. /**
  625. * drm_gem_for_each_gpuvm_bo_safe() - iterator to safely walk over a list of
  626. * &drm_gpuvm_bo
  627. * @entry__: &drm_gpuvm_bostructure to assign to in each iteration step
  628. * @next__: &next &drm_gpuvm_bo to store the next step
  629. * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
  630. *
  631. * This iterator walks over all &drm_gpuvm_bo structures associated with the
  632. * &drm_gem_object. It is implemented with list_for_each_entry_safe(), hence
  633. * it is save against removal of elements.
  634. */
  635. #define drm_gem_for_each_gpuvm_bo_safe(entry__, next__, obj__) \
  636. list_for_each_entry_safe(entry__, next__, &(obj__)->gpuva.list, list.entry.gem)
  637. #endif /* __DRM_GEM_H__ */