i915_gem_object.h 11 KB

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