i915_gem_object.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright © 2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #ifndef __I915_GEM_OBJECT_H__
  25. #define __I915_GEM_OBJECT_H__
  26. #include <linux/reservation.h>
  27. #include <drm/drm_vma_manager.h>
  28. #include <drm/drm_gem.h>
  29. #include <drm/drmP.h>
  30. #include <drm/i915_drm.h>
  31. struct drm_i915_gem_object_ops {
  32. unsigned int flags;
  33. #define I915_GEM_OBJECT_HAS_STRUCT_PAGE 0x1
  34. #define I915_GEM_OBJECT_IS_SHRINKABLE 0x2
  35. /* Interface between the GEM object and its backing storage.
  36. * get_pages() is called once prior to the use of the associated set
  37. * of pages before to binding them into the GTT, and put_pages() is
  38. * called after we no longer need them. As we expect there to be
  39. * associated cost with migrating pages between the backing storage
  40. * and making them available for the GPU (e.g. clflush), we may hold
  41. * onto the pages after they are no longer referenced by the GPU
  42. * in case they may be used again shortly (for example migrating the
  43. * pages to a different memory domain within the GTT). put_pages()
  44. * will therefore most likely be called when the object itself is
  45. * being released or under memory pressure (where we attempt to
  46. * reap pages for the shrinker).
  47. */
  48. struct sg_table *(*get_pages)(struct drm_i915_gem_object *);
  49. void (*put_pages)(struct drm_i915_gem_object *, struct sg_table *);
  50. int (*dmabuf_export)(struct drm_i915_gem_object *);
  51. void (*release)(struct drm_i915_gem_object *);
  52. };
  53. struct drm_i915_gem_object {
  54. struct drm_gem_object base;
  55. const struct drm_i915_gem_object_ops *ops;
  56. /** List of VMAs backed by this object */
  57. struct list_head vma_list;
  58. struct rb_root vma_tree;
  59. /** Stolen memory for this object, instead of being backed by shmem. */
  60. struct drm_mm_node *stolen;
  61. struct list_head global_link;
  62. union {
  63. struct rcu_head rcu;
  64. struct llist_node freed;
  65. };
  66. /**
  67. * Whether the object is currently in the GGTT mmap.
  68. */
  69. struct list_head userfault_link;
  70. /** Used in execbuf to temporarily hold a ref */
  71. struct list_head obj_exec_link;
  72. struct list_head batch_pool_link;
  73. unsigned long flags;
  74. /**
  75. * Have we taken a reference for the object for incomplete GPU
  76. * activity?
  77. */
  78. #define I915_BO_ACTIVE_REF 0
  79. /*
  80. * Is the object to be mapped as read-only to the GPU
  81. * Only honoured if hardware has relevant pte bit
  82. */
  83. unsigned long gt_ro:1;
  84. unsigned int cache_level:3;
  85. unsigned int cache_dirty:1;
  86. atomic_t frontbuffer_bits;
  87. unsigned int frontbuffer_ggtt_origin; /* write once */
  88. struct i915_gem_active frontbuffer_write;
  89. /** Current tiling stride for the object, if it's tiled. */
  90. unsigned int tiling_and_stride;
  91. #define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
  92. #define TILING_MASK (FENCE_MINIMUM_STRIDE-1)
  93. #define STRIDE_MASK (~TILING_MASK)
  94. /** Count of VMA actually bound by this object */
  95. unsigned int bind_count;
  96. unsigned int active_count;
  97. unsigned int pin_display;
  98. struct {
  99. struct mutex lock; /* protects the pages and their use */
  100. atomic_t pages_pin_count;
  101. struct sg_table *pages;
  102. void *mapping;
  103. struct i915_gem_object_page_iter {
  104. struct scatterlist *sg_pos;
  105. unsigned int sg_idx; /* in pages, but 32bit eek! */
  106. struct radix_tree_root radix;
  107. struct mutex lock; /* protects this cache */
  108. } get_page;
  109. /**
  110. * Advice: are the backing pages purgeable?
  111. */
  112. unsigned int madv:2;
  113. /**
  114. * This is set if the object has been written to since the
  115. * pages were last acquired.
  116. */
  117. bool dirty:1;
  118. /**
  119. * This is set if the object has been pinned due to unknown
  120. * swizzling.
  121. */
  122. bool quirked:1;
  123. } mm;
  124. /** Breadcrumb of last rendering to the buffer.
  125. * There can only be one writer, but we allow for multiple readers.
  126. * If there is a writer that necessarily implies that all other
  127. * read requests are complete - but we may only be lazily clearing
  128. * the read requests. A read request is naturally the most recent
  129. * request on a ring, so we may have two different write and read
  130. * requests on one ring where the write request is older than the
  131. * read request. This allows for the CPU to read from an active
  132. * buffer by only waiting for the write to complete.
  133. */
  134. struct reservation_object *resv;
  135. /** References from framebuffers, locks out tiling changes. */
  136. unsigned long framebuffer_references;
  137. /** Record of address bit 17 of each page at last unbind. */
  138. unsigned long *bit_17;
  139. struct i915_gem_userptr {
  140. uintptr_t ptr;
  141. unsigned read_only :1;
  142. struct i915_mm_struct *mm;
  143. struct i915_mmu_object *mmu_object;
  144. struct work_struct *work;
  145. } userptr;
  146. /** for phys allocated objects */
  147. struct drm_dma_handle *phys_handle;
  148. struct reservation_object __builtin_resv;
  149. };
  150. static inline struct drm_i915_gem_object *
  151. to_intel_bo(struct drm_gem_object *gem)
  152. {
  153. /* Assert that to_intel_bo(NULL) == NULL */
  154. BUILD_BUG_ON(offsetof(struct drm_i915_gem_object, base));
  155. return container_of(gem, struct drm_i915_gem_object, base);
  156. }
  157. /**
  158. * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
  159. * @filp: DRM file private date
  160. * @handle: userspace handle
  161. *
  162. * Returns:
  163. *
  164. * A pointer to the object named by the handle if such exists on @filp, NULL
  165. * otherwise. This object is only valid whilst under the RCU read lock, and
  166. * note carefully the object may be in the process of being destroyed.
  167. */
  168. static inline struct drm_i915_gem_object *
  169. i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
  170. {
  171. #ifdef CONFIG_LOCKDEP
  172. WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
  173. #endif
  174. return idr_find(&file->object_idr, handle);
  175. }
  176. static inline struct drm_i915_gem_object *
  177. i915_gem_object_lookup(struct drm_file *file, u32 handle)
  178. {
  179. struct drm_i915_gem_object *obj;
  180. rcu_read_lock();
  181. obj = i915_gem_object_lookup_rcu(file, handle);
  182. if (obj && !kref_get_unless_zero(&obj->base.refcount))
  183. obj = NULL;
  184. rcu_read_unlock();
  185. return obj;
  186. }
  187. __deprecated
  188. extern struct drm_gem_object *
  189. drm_gem_object_lookup(struct drm_file *file, u32 handle);
  190. __attribute__((nonnull))
  191. static inline struct drm_i915_gem_object *
  192. i915_gem_object_get(struct drm_i915_gem_object *obj)
  193. {
  194. drm_gem_object_reference(&obj->base);
  195. return obj;
  196. }
  197. __deprecated
  198. extern void drm_gem_object_reference(struct drm_gem_object *);
  199. __attribute__((nonnull))
  200. static inline void
  201. i915_gem_object_put(struct drm_i915_gem_object *obj)
  202. {
  203. __drm_gem_object_unreference(&obj->base);
  204. }
  205. __deprecated
  206. extern void drm_gem_object_unreference(struct drm_gem_object *);
  207. __deprecated
  208. extern void drm_gem_object_unreference_unlocked(struct drm_gem_object *);
  209. static inline bool
  210. i915_gem_object_is_dead(const struct drm_i915_gem_object *obj)
  211. {
  212. return atomic_read(&obj->base.refcount.refcount) == 0;
  213. }
  214. static inline bool
  215. i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
  216. {
  217. return obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE;
  218. }
  219. static inline bool
  220. i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
  221. {
  222. return obj->ops->flags & I915_GEM_OBJECT_IS_SHRINKABLE;
  223. }
  224. static inline bool
  225. i915_gem_object_is_active(const struct drm_i915_gem_object *obj)
  226. {
  227. return obj->active_count;
  228. }
  229. static inline bool
  230. i915_gem_object_has_active_reference(const struct drm_i915_gem_object *obj)
  231. {
  232. return test_bit(I915_BO_ACTIVE_REF, &obj->flags);
  233. }
  234. static inline void
  235. i915_gem_object_set_active_reference(struct drm_i915_gem_object *obj)
  236. {
  237. lockdep_assert_held(&obj->base.dev->struct_mutex);
  238. __set_bit(I915_BO_ACTIVE_REF, &obj->flags);
  239. }
  240. static inline void
  241. i915_gem_object_clear_active_reference(struct drm_i915_gem_object *obj)
  242. {
  243. lockdep_assert_held(&obj->base.dev->struct_mutex);
  244. __clear_bit(I915_BO_ACTIVE_REF, &obj->flags);
  245. }
  246. void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj);
  247. static inline unsigned int
  248. i915_gem_object_get_tiling(struct drm_i915_gem_object *obj)
  249. {
  250. return obj->tiling_and_stride & TILING_MASK;
  251. }
  252. static inline bool
  253. i915_gem_object_is_tiled(struct drm_i915_gem_object *obj)
  254. {
  255. return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
  256. }
  257. static inline unsigned int
  258. i915_gem_object_get_stride(struct drm_i915_gem_object *obj)
  259. {
  260. return obj->tiling_and_stride & STRIDE_MASK;
  261. }
  262. static inline unsigned int
  263. i915_gem_tile_height(unsigned int tiling)
  264. {
  265. GEM_BUG_ON(!tiling);
  266. return tiling == I915_TILING_Y ? 32 : 8;
  267. }
  268. static inline unsigned int
  269. i915_gem_object_get_tile_height(struct drm_i915_gem_object *obj)
  270. {
  271. return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
  272. }
  273. static inline unsigned int
  274. i915_gem_object_get_tile_row_size(struct drm_i915_gem_object *obj)
  275. {
  276. return (i915_gem_object_get_stride(obj) *
  277. i915_gem_object_get_tile_height(obj));
  278. }
  279. int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
  280. unsigned int tiling, unsigned int stride);
  281. static inline struct intel_engine_cs *
  282. i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj)
  283. {
  284. struct intel_engine_cs *engine = NULL;
  285. struct dma_fence *fence;
  286. rcu_read_lock();
  287. fence = reservation_object_get_excl_rcu(obj->resv);
  288. rcu_read_unlock();
  289. if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence))
  290. engine = to_request(fence)->engine;
  291. dma_fence_put(fence);
  292. return engine;
  293. }
  294. #endif