i915_gem_object.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 (*pwrite)(struct drm_i915_gem_object *,
  51. const struct drm_i915_gem_pwrite *);
  52. int (*dmabuf_export)(struct drm_i915_gem_object *);
  53. void (*release)(struct drm_i915_gem_object *);
  54. };
  55. struct drm_i915_gem_object {
  56. struct drm_gem_object base;
  57. const struct drm_i915_gem_object_ops *ops;
  58. /** List of VMAs backed by this object */
  59. struct list_head vma_list;
  60. struct rb_root vma_tree;
  61. /** Stolen memory for this object, instead of being backed by shmem. */
  62. struct drm_mm_node *stolen;
  63. struct list_head global_link;
  64. union {
  65. struct rcu_head rcu;
  66. struct llist_node freed;
  67. };
  68. /**
  69. * Whether the object is currently in the GGTT mmap.
  70. */
  71. struct list_head userfault_link;
  72. /** Used in execbuf to temporarily hold a ref */
  73. struct list_head obj_exec_link;
  74. struct list_head batch_pool_link;
  75. unsigned long flags;
  76. /**
  77. * Have we taken a reference for the object for incomplete GPU
  78. * activity?
  79. */
  80. #define I915_BO_ACTIVE_REF 0
  81. /*
  82. * Is the object to be mapped as read-only to the GPU
  83. * Only honoured if hardware has relevant pte bit
  84. */
  85. unsigned long gt_ro:1;
  86. unsigned int cache_level:3;
  87. unsigned int cache_dirty:1;
  88. atomic_t frontbuffer_bits;
  89. unsigned int frontbuffer_ggtt_origin; /* write once */
  90. struct i915_gem_active frontbuffer_write;
  91. /** Current tiling stride for the object, if it's tiled. */
  92. unsigned int tiling_and_stride;
  93. #define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
  94. #define TILING_MASK (FENCE_MINIMUM_STRIDE-1)
  95. #define STRIDE_MASK (~TILING_MASK)
  96. /** Count of VMA actually bound by this object */
  97. unsigned int bind_count;
  98. unsigned int active_count;
  99. unsigned int pin_display;
  100. struct {
  101. struct mutex lock; /* protects the pages and their use */
  102. atomic_t pages_pin_count;
  103. struct sg_table *pages;
  104. void *mapping;
  105. struct i915_gem_object_page_iter {
  106. struct scatterlist *sg_pos;
  107. unsigned int sg_idx; /* in pages, but 32bit eek! */
  108. struct radix_tree_root radix;
  109. struct mutex lock; /* protects this cache */
  110. } get_page;
  111. /**
  112. * Advice: are the backing pages purgeable?
  113. */
  114. unsigned int madv:2;
  115. /**
  116. * This is set if the object has been written to since the
  117. * pages were last acquired.
  118. */
  119. bool dirty:1;
  120. /**
  121. * This is set if the object has been pinned due to unknown
  122. * swizzling.
  123. */
  124. bool quirked:1;
  125. } mm;
  126. /** Breadcrumb of last rendering to the buffer.
  127. * There can only be one writer, but we allow for multiple readers.
  128. * If there is a writer that necessarily implies that all other
  129. * read requests are complete - but we may only be lazily clearing
  130. * the read requests. A read request is naturally the most recent
  131. * request on a ring, so we may have two different write and read
  132. * requests on one ring where the write request is older than the
  133. * read request. This allows for the CPU to read from an active
  134. * buffer by only waiting for the write to complete.
  135. */
  136. struct reservation_object *resv;
  137. /** References from framebuffers, locks out tiling changes. */
  138. unsigned long framebuffer_references;
  139. /** Record of address bit 17 of each page at last unbind. */
  140. unsigned long *bit_17;
  141. struct i915_gem_userptr {
  142. uintptr_t ptr;
  143. unsigned read_only :1;
  144. struct i915_mm_struct *mm;
  145. struct i915_mmu_object *mmu_object;
  146. struct work_struct *work;
  147. } userptr;
  148. /** for phys allocated objects */
  149. struct drm_dma_handle *phys_handle;
  150. struct reservation_object __builtin_resv;
  151. };
  152. static inline struct drm_i915_gem_object *
  153. to_intel_bo(struct drm_gem_object *gem)
  154. {
  155. /* Assert that to_intel_bo(NULL) == NULL */
  156. BUILD_BUG_ON(offsetof(struct drm_i915_gem_object, base));
  157. return container_of(gem, struct drm_i915_gem_object, base);
  158. }
  159. /**
  160. * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
  161. * @filp: DRM file private date
  162. * @handle: userspace handle
  163. *
  164. * Returns:
  165. *
  166. * A pointer to the object named by the handle if such exists on @filp, NULL
  167. * otherwise. This object is only valid whilst under the RCU read lock, and
  168. * note carefully the object may be in the process of being destroyed.
  169. */
  170. static inline struct drm_i915_gem_object *
  171. i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
  172. {
  173. #ifdef CONFIG_LOCKDEP
  174. WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
  175. #endif
  176. return idr_find(&file->object_idr, handle);
  177. }
  178. static inline struct drm_i915_gem_object *
  179. i915_gem_object_lookup(struct drm_file *file, u32 handle)
  180. {
  181. struct drm_i915_gem_object *obj;
  182. rcu_read_lock();
  183. obj = i915_gem_object_lookup_rcu(file, handle);
  184. if (obj && !kref_get_unless_zero(&obj->base.refcount))
  185. obj = NULL;
  186. rcu_read_unlock();
  187. return obj;
  188. }
  189. __deprecated
  190. extern struct drm_gem_object *
  191. drm_gem_object_lookup(struct drm_file *file, u32 handle);
  192. __attribute__((nonnull))
  193. static inline struct drm_i915_gem_object *
  194. i915_gem_object_get(struct drm_i915_gem_object *obj)
  195. {
  196. drm_gem_object_reference(&obj->base);
  197. return obj;
  198. }
  199. __deprecated
  200. extern void drm_gem_object_reference(struct drm_gem_object *);
  201. __attribute__((nonnull))
  202. static inline void
  203. i915_gem_object_put(struct drm_i915_gem_object *obj)
  204. {
  205. __drm_gem_object_unreference(&obj->base);
  206. }
  207. __deprecated
  208. extern void drm_gem_object_unreference(struct drm_gem_object *);
  209. __deprecated
  210. extern void drm_gem_object_unreference_unlocked(struct drm_gem_object *);
  211. static inline bool
  212. i915_gem_object_is_dead(const struct drm_i915_gem_object *obj)
  213. {
  214. return kref_read(&obj->base.refcount) == 0;
  215. }
  216. static inline bool
  217. i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
  218. {
  219. return obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE;
  220. }
  221. static inline bool
  222. i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
  223. {
  224. return obj->ops->flags & I915_GEM_OBJECT_IS_SHRINKABLE;
  225. }
  226. static inline bool
  227. i915_gem_object_is_active(const struct drm_i915_gem_object *obj)
  228. {
  229. return obj->active_count;
  230. }
  231. static inline bool
  232. i915_gem_object_has_active_reference(const struct drm_i915_gem_object *obj)
  233. {
  234. return test_bit(I915_BO_ACTIVE_REF, &obj->flags);
  235. }
  236. static inline void
  237. i915_gem_object_set_active_reference(struct drm_i915_gem_object *obj)
  238. {
  239. lockdep_assert_held(&obj->base.dev->struct_mutex);
  240. __set_bit(I915_BO_ACTIVE_REF, &obj->flags);
  241. }
  242. static inline void
  243. i915_gem_object_clear_active_reference(struct drm_i915_gem_object *obj)
  244. {
  245. lockdep_assert_held(&obj->base.dev->struct_mutex);
  246. __clear_bit(I915_BO_ACTIVE_REF, &obj->flags);
  247. }
  248. void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj);
  249. static inline unsigned int
  250. i915_gem_object_get_tiling(struct drm_i915_gem_object *obj)
  251. {
  252. return obj->tiling_and_stride & TILING_MASK;
  253. }
  254. static inline bool
  255. i915_gem_object_is_tiled(struct drm_i915_gem_object *obj)
  256. {
  257. return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
  258. }
  259. static inline unsigned int
  260. i915_gem_object_get_stride(struct drm_i915_gem_object *obj)
  261. {
  262. return obj->tiling_and_stride & STRIDE_MASK;
  263. }
  264. static inline unsigned int
  265. i915_gem_tile_height(unsigned int tiling)
  266. {
  267. GEM_BUG_ON(!tiling);
  268. return tiling == I915_TILING_Y ? 32 : 8;
  269. }
  270. static inline unsigned int
  271. i915_gem_object_get_tile_height(struct drm_i915_gem_object *obj)
  272. {
  273. return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
  274. }
  275. static inline unsigned int
  276. i915_gem_object_get_tile_row_size(struct drm_i915_gem_object *obj)
  277. {
  278. return (i915_gem_object_get_stride(obj) *
  279. i915_gem_object_get_tile_height(obj));
  280. }
  281. int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
  282. unsigned int tiling, unsigned int stride);
  283. static inline struct intel_engine_cs *
  284. i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj)
  285. {
  286. struct intel_engine_cs *engine = NULL;
  287. struct dma_fence *fence;
  288. rcu_read_lock();
  289. fence = reservation_object_get_excl_rcu(obj->resv);
  290. rcu_read_unlock();
  291. if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence))
  292. engine = to_request(fence)->engine;
  293. dma_fence_put(fence);
  294. return engine;
  295. }
  296. #endif