i915_gem_object.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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_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. /**
  130. * @read_domains: Read memory domains.
  131. *
  132. * These monitor which caches contain read/write data related to the
  133. * object. When transitioning from one set of domains to another,
  134. * the driver is called to ensure that caches are suitably flushed and
  135. * invalidated.
  136. */
  137. u16 read_domains;
  138. /**
  139. * @write_domain: Corresponding unique write memory domain.
  140. */
  141. u16 write_domain;
  142. atomic_t frontbuffer_bits;
  143. unsigned int frontbuffer_ggtt_origin; /* write once */
  144. struct i915_gem_active frontbuffer_write;
  145. /** Current tiling stride for the object, if it's tiled. */
  146. unsigned int tiling_and_stride;
  147. #define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
  148. #define TILING_MASK (FENCE_MINIMUM_STRIDE-1)
  149. #define STRIDE_MASK (~TILING_MASK)
  150. /** Count of VMA actually bound by this object */
  151. unsigned int bind_count;
  152. unsigned int active_count;
  153. /** Count of how many global VMA are currently pinned for use by HW */
  154. unsigned int pin_global;
  155. struct {
  156. struct mutex lock; /* protects the pages and their use */
  157. atomic_t pages_pin_count;
  158. struct sg_table *pages;
  159. void *mapping;
  160. /* TODO: whack some of this into the error state */
  161. struct i915_page_sizes {
  162. /**
  163. * The sg mask of the pages sg_table. i.e the mask of
  164. * of the lengths for each sg entry.
  165. */
  166. unsigned int phys;
  167. /**
  168. * The gtt page sizes we are allowed to use given the
  169. * sg mask and the supported page sizes. This will
  170. * express the smallest unit we can use for the whole
  171. * object, as well as the larger sizes we may be able
  172. * to use opportunistically.
  173. */
  174. unsigned int sg;
  175. /**
  176. * The actual gtt page size usage. Since we can have
  177. * multiple vma associated with this object we need to
  178. * prevent any trampling of state, hence a copy of this
  179. * struct also lives in each vma, therefore the gtt
  180. * value here should only be read/write through the vma.
  181. */
  182. unsigned int gtt;
  183. } page_sizes;
  184. I915_SELFTEST_DECLARE(unsigned int page_mask);
  185. struct i915_gem_object_page_iter {
  186. struct scatterlist *sg_pos;
  187. unsigned int sg_idx; /* in pages, but 32bit eek! */
  188. struct radix_tree_root radix;
  189. struct mutex lock; /* protects this cache */
  190. } get_page;
  191. /**
  192. * Element within i915->mm.unbound_list or i915->mm.bound_list,
  193. * locked by i915->mm.obj_lock.
  194. */
  195. struct list_head link;
  196. /**
  197. * Advice: are the backing pages purgeable?
  198. */
  199. unsigned int madv:2;
  200. /**
  201. * This is set if the object has been written to since the
  202. * pages were last acquired.
  203. */
  204. bool dirty:1;
  205. /**
  206. * This is set if the object has been pinned due to unknown
  207. * swizzling.
  208. */
  209. bool quirked:1;
  210. } mm;
  211. /** Breadcrumb of last rendering to the buffer.
  212. * There can only be one writer, but we allow for multiple readers.
  213. * If there is a writer that necessarily implies that all other
  214. * read requests are complete - but we may only be lazily clearing
  215. * the read requests. A read request is naturally the most recent
  216. * request on a ring, so we may have two different write and read
  217. * requests on one ring where the write request is older than the
  218. * read request. This allows for the CPU to read from an active
  219. * buffer by only waiting for the write to complete.
  220. */
  221. struct reservation_object *resv;
  222. /** References from framebuffers, locks out tiling changes. */
  223. unsigned int framebuffer_references;
  224. /** Record of address bit 17 of each page at last unbind. */
  225. unsigned long *bit_17;
  226. union {
  227. struct i915_gem_userptr {
  228. uintptr_t ptr;
  229. unsigned read_only :1;
  230. struct i915_mm_struct *mm;
  231. struct i915_mmu_object *mmu_object;
  232. struct work_struct *work;
  233. } userptr;
  234. unsigned long scratch;
  235. void *gvt_info;
  236. };
  237. /** for phys allocated objects */
  238. struct drm_dma_handle *phys_handle;
  239. struct reservation_object __builtin_resv;
  240. };
  241. static inline struct drm_i915_gem_object *
  242. to_intel_bo(struct drm_gem_object *gem)
  243. {
  244. /* Assert that to_intel_bo(NULL) == NULL */
  245. BUILD_BUG_ON(offsetof(struct drm_i915_gem_object, base));
  246. return container_of(gem, struct drm_i915_gem_object, base);
  247. }
  248. /**
  249. * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
  250. * @filp: DRM file private date
  251. * @handle: userspace handle
  252. *
  253. * Returns:
  254. *
  255. * A pointer to the object named by the handle if such exists on @filp, NULL
  256. * otherwise. This object is only valid whilst under the RCU read lock, and
  257. * note carefully the object may be in the process of being destroyed.
  258. */
  259. static inline struct drm_i915_gem_object *
  260. i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
  261. {
  262. #ifdef CONFIG_LOCKDEP
  263. WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
  264. #endif
  265. return idr_find(&file->object_idr, handle);
  266. }
  267. static inline struct drm_i915_gem_object *
  268. i915_gem_object_lookup(struct drm_file *file, u32 handle)
  269. {
  270. struct drm_i915_gem_object *obj;
  271. rcu_read_lock();
  272. obj = i915_gem_object_lookup_rcu(file, handle);
  273. if (obj && !kref_get_unless_zero(&obj->base.refcount))
  274. obj = NULL;
  275. rcu_read_unlock();
  276. return obj;
  277. }
  278. __deprecated
  279. extern struct drm_gem_object *
  280. drm_gem_object_lookup(struct drm_file *file, u32 handle);
  281. __attribute__((nonnull))
  282. static inline struct drm_i915_gem_object *
  283. i915_gem_object_get(struct drm_i915_gem_object *obj)
  284. {
  285. drm_gem_object_reference(&obj->base);
  286. return obj;
  287. }
  288. __deprecated
  289. extern void drm_gem_object_reference(struct drm_gem_object *);
  290. __attribute__((nonnull))
  291. static inline void
  292. i915_gem_object_put(struct drm_i915_gem_object *obj)
  293. {
  294. __drm_gem_object_unreference(&obj->base);
  295. }
  296. __deprecated
  297. extern void drm_gem_object_unreference(struct drm_gem_object *);
  298. __deprecated
  299. extern void drm_gem_object_unreference_unlocked(struct drm_gem_object *);
  300. static inline void i915_gem_object_lock(struct drm_i915_gem_object *obj)
  301. {
  302. reservation_object_lock(obj->resv, NULL);
  303. }
  304. static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj)
  305. {
  306. reservation_object_unlock(obj->resv);
  307. }
  308. static inline bool
  309. i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
  310. {
  311. return obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE;
  312. }
  313. static inline bool
  314. i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
  315. {
  316. return obj->ops->flags & I915_GEM_OBJECT_IS_SHRINKABLE;
  317. }
  318. static inline bool
  319. i915_gem_object_is_proxy(const struct drm_i915_gem_object *obj)
  320. {
  321. return obj->ops->flags & I915_GEM_OBJECT_IS_PROXY;
  322. }
  323. static inline bool
  324. i915_gem_object_is_active(const struct drm_i915_gem_object *obj)
  325. {
  326. return obj->active_count;
  327. }
  328. static inline bool
  329. i915_gem_object_has_active_reference(const struct drm_i915_gem_object *obj)
  330. {
  331. return test_bit(I915_BO_ACTIVE_REF, &obj->flags);
  332. }
  333. static inline void
  334. i915_gem_object_set_active_reference(struct drm_i915_gem_object *obj)
  335. {
  336. lockdep_assert_held(&obj->base.dev->struct_mutex);
  337. __set_bit(I915_BO_ACTIVE_REF, &obj->flags);
  338. }
  339. static inline void
  340. i915_gem_object_clear_active_reference(struct drm_i915_gem_object *obj)
  341. {
  342. lockdep_assert_held(&obj->base.dev->struct_mutex);
  343. __clear_bit(I915_BO_ACTIVE_REF, &obj->flags);
  344. }
  345. void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj);
  346. static inline bool
  347. i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj)
  348. {
  349. return READ_ONCE(obj->framebuffer_references);
  350. }
  351. static inline unsigned int
  352. i915_gem_object_get_tiling(struct drm_i915_gem_object *obj)
  353. {
  354. return obj->tiling_and_stride & TILING_MASK;
  355. }
  356. static inline bool
  357. i915_gem_object_is_tiled(struct drm_i915_gem_object *obj)
  358. {
  359. return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
  360. }
  361. static inline unsigned int
  362. i915_gem_object_get_stride(struct drm_i915_gem_object *obj)
  363. {
  364. return obj->tiling_and_stride & STRIDE_MASK;
  365. }
  366. static inline unsigned int
  367. i915_gem_tile_height(unsigned int tiling)
  368. {
  369. GEM_BUG_ON(!tiling);
  370. return tiling == I915_TILING_Y ? 32 : 8;
  371. }
  372. static inline unsigned int
  373. i915_gem_object_get_tile_height(struct drm_i915_gem_object *obj)
  374. {
  375. return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
  376. }
  377. static inline unsigned int
  378. i915_gem_object_get_tile_row_size(struct drm_i915_gem_object *obj)
  379. {
  380. return (i915_gem_object_get_stride(obj) *
  381. i915_gem_object_get_tile_height(obj));
  382. }
  383. int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
  384. unsigned int tiling, unsigned int stride);
  385. static inline struct intel_engine_cs *
  386. i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj)
  387. {
  388. struct intel_engine_cs *engine = NULL;
  389. struct dma_fence *fence;
  390. rcu_read_lock();
  391. fence = reservation_object_get_excl_rcu(obj->resv);
  392. rcu_read_unlock();
  393. if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence))
  394. engine = to_request(fence)->engine;
  395. dma_fence_put(fence);
  396. return engine;
  397. }
  398. void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
  399. unsigned int cache_level);
  400. void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj);
  401. #endif