i915_gem_object.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. #include "i915_gem_request.h"
  32. #include "i915_selftest.h"
  33. struct drm_i915_gem_object;
  34. /*
  35. * struct i915_lut_handle tracks the fast lookups from handle to vma used
  36. * for execbuf. Although we use a radixtree for that mapping, in order to
  37. * remove them as the object or context is closed, we need a secondary list
  38. * and a translation entry (i915_lut_handle).
  39. */
  40. struct i915_lut_handle {
  41. struct list_head obj_link;
  42. struct list_head ctx_link;
  43. struct i915_gem_context *ctx;
  44. u32 handle;
  45. };
  46. struct drm_i915_gem_object_ops {
  47. unsigned int flags;
  48. #define I915_GEM_OBJECT_HAS_STRUCT_PAGE BIT(0)
  49. #define I915_GEM_OBJECT_IS_SHRINKABLE BIT(1)
  50. /* Interface between the GEM object and its backing storage.
  51. * get_pages() is called once prior to the use of the associated set
  52. * of pages before to binding them into the GTT, and put_pages() is
  53. * called after we no longer need them. As we expect there to be
  54. * associated cost with migrating pages between the backing storage
  55. * and making them available for the GPU (e.g. clflush), we may hold
  56. * onto the pages after they are no longer referenced by the GPU
  57. * in case they may be used again shortly (for example migrating the
  58. * pages to a different memory domain within the GTT). put_pages()
  59. * will therefore most likely be called when the object itself is
  60. * being released or under memory pressure (where we attempt to
  61. * reap pages for the shrinker).
  62. */
  63. struct sg_table *(*get_pages)(struct drm_i915_gem_object *);
  64. void (*put_pages)(struct drm_i915_gem_object *, struct sg_table *);
  65. int (*pwrite)(struct drm_i915_gem_object *,
  66. const struct drm_i915_gem_pwrite *);
  67. int (*dmabuf_export)(struct drm_i915_gem_object *);
  68. void (*release)(struct drm_i915_gem_object *);
  69. };
  70. struct drm_i915_gem_object {
  71. struct drm_gem_object base;
  72. const struct drm_i915_gem_object_ops *ops;
  73. /**
  74. * @vma_list: List of VMAs backed by this object
  75. *
  76. * The VMA on this list are ordered by type, all GGTT vma are placed
  77. * at the head and all ppGTT vma are placed at the tail. The different
  78. * types of GGTT vma are unordered between themselves, use the
  79. * @vma_tree (which has a defined order between all VMA) to find an
  80. * exact match.
  81. */
  82. struct list_head vma_list;
  83. /**
  84. * @vma_tree: Ordered tree of VMAs backed by this object
  85. *
  86. * All VMA created for this object are placed in the @vma_tree for
  87. * fast retrieval via a binary search in i915_vma_instance().
  88. * They are also added to @vma_list for easy iteration.
  89. */
  90. struct rb_root vma_tree;
  91. /**
  92. * @lut_list: List of vma lookup entries in use for this object.
  93. *
  94. * If this object is closed, we need to remove all of its VMA from
  95. * the fast lookup index in associated contexts; @lut_list provides
  96. * this translation from object to context->handles_vma.
  97. */
  98. struct list_head lut_list;
  99. /** Stolen memory for this object, instead of being backed by shmem. */
  100. struct drm_mm_node *stolen;
  101. struct list_head global_link;
  102. union {
  103. struct rcu_head rcu;
  104. struct llist_node freed;
  105. };
  106. /**
  107. * Whether the object is currently in the GGTT mmap.
  108. */
  109. struct list_head userfault_link;
  110. struct list_head batch_pool_link;
  111. I915_SELFTEST_DECLARE(struct list_head st_link);
  112. unsigned long flags;
  113. /**
  114. * Have we taken a reference for the object for incomplete GPU
  115. * activity?
  116. */
  117. #define I915_BO_ACTIVE_REF 0
  118. /*
  119. * Is the object to be mapped as read-only to the GPU
  120. * Only honoured if hardware has relevant pte bit
  121. */
  122. unsigned long gt_ro:1;
  123. unsigned int cache_level:3;
  124. unsigned int cache_coherent:2;
  125. #define I915_BO_CACHE_COHERENT_FOR_READ BIT(0)
  126. #define I915_BO_CACHE_COHERENT_FOR_WRITE BIT(1)
  127. unsigned int cache_dirty:1;
  128. atomic_t frontbuffer_bits;
  129. unsigned int frontbuffer_ggtt_origin; /* write once */
  130. struct i915_gem_active frontbuffer_write;
  131. /** Current tiling stride for the object, if it's tiled. */
  132. unsigned int tiling_and_stride;
  133. #define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
  134. #define TILING_MASK (FENCE_MINIMUM_STRIDE-1)
  135. #define STRIDE_MASK (~TILING_MASK)
  136. /** Count of VMA actually bound by this object */
  137. unsigned int bind_count;
  138. unsigned int active_count;
  139. unsigned int pin_display;
  140. struct {
  141. struct mutex lock; /* protects the pages and their use */
  142. atomic_t pages_pin_count;
  143. struct sg_table *pages;
  144. void *mapping;
  145. struct i915_gem_object_page_iter {
  146. struct scatterlist *sg_pos;
  147. unsigned int sg_idx; /* in pages, but 32bit eek! */
  148. struct radix_tree_root radix;
  149. struct mutex lock; /* protects this cache */
  150. } get_page;
  151. /**
  152. * Advice: are the backing pages purgeable?
  153. */
  154. unsigned int madv:2;
  155. /**
  156. * This is set if the object has been written to since the
  157. * pages were last acquired.
  158. */
  159. bool dirty:1;
  160. /**
  161. * This is set if the object has been pinned due to unknown
  162. * swizzling.
  163. */
  164. bool quirked:1;
  165. } mm;
  166. /** Breadcrumb of last rendering to the buffer.
  167. * There can only be one writer, but we allow for multiple readers.
  168. * If there is a writer that necessarily implies that all other
  169. * read requests are complete - but we may only be lazily clearing
  170. * the read requests. A read request is naturally the most recent
  171. * request on a ring, so we may have two different write and read
  172. * requests on one ring where the write request is older than the
  173. * read request. This allows for the CPU to read from an active
  174. * buffer by only waiting for the write to complete.
  175. */
  176. struct reservation_object *resv;
  177. /** References from framebuffers, locks out tiling changes. */
  178. unsigned int framebuffer_references;
  179. /** Record of address bit 17 of each page at last unbind. */
  180. unsigned long *bit_17;
  181. union {
  182. struct i915_gem_userptr {
  183. uintptr_t ptr;
  184. unsigned read_only :1;
  185. struct i915_mm_struct *mm;
  186. struct i915_mmu_object *mmu_object;
  187. struct work_struct *work;
  188. } userptr;
  189. unsigned long scratch;
  190. };
  191. /** for phys allocated objects */
  192. struct drm_dma_handle *phys_handle;
  193. struct reservation_object __builtin_resv;
  194. };
  195. static inline struct drm_i915_gem_object *
  196. to_intel_bo(struct drm_gem_object *gem)
  197. {
  198. /* Assert that to_intel_bo(NULL) == NULL */
  199. BUILD_BUG_ON(offsetof(struct drm_i915_gem_object, base));
  200. return container_of(gem, struct drm_i915_gem_object, base);
  201. }
  202. /**
  203. * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
  204. * @filp: DRM file private date
  205. * @handle: userspace handle
  206. *
  207. * Returns:
  208. *
  209. * A pointer to the object named by the handle if such exists on @filp, NULL
  210. * otherwise. This object is only valid whilst under the RCU read lock, and
  211. * note carefully the object may be in the process of being destroyed.
  212. */
  213. static inline struct drm_i915_gem_object *
  214. i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
  215. {
  216. #ifdef CONFIG_LOCKDEP
  217. WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
  218. #endif
  219. return idr_find(&file->object_idr, handle);
  220. }
  221. static inline struct drm_i915_gem_object *
  222. i915_gem_object_lookup(struct drm_file *file, u32 handle)
  223. {
  224. struct drm_i915_gem_object *obj;
  225. rcu_read_lock();
  226. obj = i915_gem_object_lookup_rcu(file, handle);
  227. if (obj && !kref_get_unless_zero(&obj->base.refcount))
  228. obj = NULL;
  229. rcu_read_unlock();
  230. return obj;
  231. }
  232. __deprecated
  233. extern struct drm_gem_object *
  234. drm_gem_object_lookup(struct drm_file *file, u32 handle);
  235. __attribute__((nonnull))
  236. static inline struct drm_i915_gem_object *
  237. i915_gem_object_get(struct drm_i915_gem_object *obj)
  238. {
  239. drm_gem_object_reference(&obj->base);
  240. return obj;
  241. }
  242. __deprecated
  243. extern void drm_gem_object_reference(struct drm_gem_object *);
  244. __attribute__((nonnull))
  245. static inline void
  246. i915_gem_object_put(struct drm_i915_gem_object *obj)
  247. {
  248. __drm_gem_object_unreference(&obj->base);
  249. }
  250. __deprecated
  251. extern void drm_gem_object_unreference(struct drm_gem_object *);
  252. __deprecated
  253. extern void drm_gem_object_unreference_unlocked(struct drm_gem_object *);
  254. static inline void i915_gem_object_lock(struct drm_i915_gem_object *obj)
  255. {
  256. reservation_object_lock(obj->resv, NULL);
  257. }
  258. static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj)
  259. {
  260. reservation_object_unlock(obj->resv);
  261. }
  262. static inline bool
  263. i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
  264. {
  265. return obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE;
  266. }
  267. static inline bool
  268. i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
  269. {
  270. return obj->ops->flags & I915_GEM_OBJECT_IS_SHRINKABLE;
  271. }
  272. static inline bool
  273. i915_gem_object_is_active(const struct drm_i915_gem_object *obj)
  274. {
  275. return obj->active_count;
  276. }
  277. static inline bool
  278. i915_gem_object_has_active_reference(const struct drm_i915_gem_object *obj)
  279. {
  280. return test_bit(I915_BO_ACTIVE_REF, &obj->flags);
  281. }
  282. static inline void
  283. i915_gem_object_set_active_reference(struct drm_i915_gem_object *obj)
  284. {
  285. lockdep_assert_held(&obj->base.dev->struct_mutex);
  286. __set_bit(I915_BO_ACTIVE_REF, &obj->flags);
  287. }
  288. static inline void
  289. i915_gem_object_clear_active_reference(struct drm_i915_gem_object *obj)
  290. {
  291. lockdep_assert_held(&obj->base.dev->struct_mutex);
  292. __clear_bit(I915_BO_ACTIVE_REF, &obj->flags);
  293. }
  294. void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj);
  295. static inline bool
  296. i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj)
  297. {
  298. return READ_ONCE(obj->framebuffer_references);
  299. }
  300. static inline unsigned int
  301. i915_gem_object_get_tiling(struct drm_i915_gem_object *obj)
  302. {
  303. return obj->tiling_and_stride & TILING_MASK;
  304. }
  305. static inline bool
  306. i915_gem_object_is_tiled(struct drm_i915_gem_object *obj)
  307. {
  308. return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
  309. }
  310. static inline unsigned int
  311. i915_gem_object_get_stride(struct drm_i915_gem_object *obj)
  312. {
  313. return obj->tiling_and_stride & STRIDE_MASK;
  314. }
  315. static inline unsigned int
  316. i915_gem_tile_height(unsigned int tiling)
  317. {
  318. GEM_BUG_ON(!tiling);
  319. return tiling == I915_TILING_Y ? 32 : 8;
  320. }
  321. static inline unsigned int
  322. i915_gem_object_get_tile_height(struct drm_i915_gem_object *obj)
  323. {
  324. return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
  325. }
  326. static inline unsigned int
  327. i915_gem_object_get_tile_row_size(struct drm_i915_gem_object *obj)
  328. {
  329. return (i915_gem_object_get_stride(obj) *
  330. i915_gem_object_get_tile_height(obj));
  331. }
  332. int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
  333. unsigned int tiling, unsigned int stride);
  334. static inline struct intel_engine_cs *
  335. i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj)
  336. {
  337. struct intel_engine_cs *engine = NULL;
  338. struct dma_fence *fence;
  339. rcu_read_lock();
  340. fence = reservation_object_get_excl_rcu(obj->resv);
  341. rcu_read_unlock();
  342. if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence))
  343. engine = to_request(fence)->engine;
  344. dma_fence_put(fence);
  345. return engine;
  346. }
  347. void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
  348. unsigned int cache_level);
  349. void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj);
  350. #endif