i915_gem_object.h 13 KB

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