1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2016 Intel Corporation
5  */
6 
7 #ifndef __I915_GEM_OBJECT_TYPES_H__
8 #define __I915_GEM_OBJECT_TYPES_H__
9 
10 #include <linux/mmu_notifier.h>
11 
12 #include <drm/drm_gem.h>
13 #include <drm/ttm/ttm_bo.h>
14 #include <uapi/drm/i915_drm.h>
15 
16 #include "i915_active.h"
17 #include "i915_selftest.h"
18 #include "i915_vma_resource.h"
19 
20 struct drm_i915_gem_object;
21 struct intel_fronbuffer;
22 struct intel_memory_region;
23 
24 /*
25  * struct i915_lut_handle tracks the fast lookups from handle to vma used
26  * for execbuf. Although we use a radixtree for that mapping, in order to
27  * remove them as the object or context is closed, we need a secondary list
28  * and a translation entry (i915_lut_handle).
29  */
30 struct i915_lut_handle {
31 	struct list_head obj_link;
32 	struct i915_gem_context *ctx;
33 	u32 handle;
34 };
35 
36 struct drm_i915_gem_object_ops {
37 	unsigned int flags;
38 #define I915_GEM_OBJECT_IS_SHRINKABLE			BIT(1)
39 /* Skip the shrinker management in set_pages/unset_pages */
40 #define I915_GEM_OBJECT_SELF_MANAGED_SHRINK_LIST	BIT(2)
41 #define I915_GEM_OBJECT_IS_PROXY			BIT(3)
42 #define I915_GEM_OBJECT_NO_MMAP				BIT(4)
43 
44 	/* Interface between the GEM object and its backing storage.
45 	 * get_pages() is called once prior to the use of the associated set
46 	 * of pages before to binding them into the GTT, and put_pages() is
47 	 * called after we no longer need them. As we expect there to be
48 	 * associated cost with migrating pages between the backing storage
49 	 * and making them available for the GPU (e.g. clflush), we may hold
50 	 * onto the pages after they are no longer referenced by the GPU
51 	 * in case they may be used again shortly (for example migrating the
52 	 * pages to a different memory domain within the GTT). put_pages()
53 	 * will therefore most likely be called when the object itself is
54 	 * being released or under memory pressure (where we attempt to
55 	 * reap pages for the shrinker).
56 	 */
57 	int (*get_pages)(struct drm_i915_gem_object *obj);
58 	void (*put_pages)(struct drm_i915_gem_object *obj,
59 			  struct sg_table *pages);
60 	int (*truncate)(struct drm_i915_gem_object *obj);
61 	/**
62 	 * shrink - Perform further backend specific actions to facilate
63 	 * shrinking.
64 	 * @obj: The gem object
65 	 * @flags: Extra flags to control shrinking behaviour in the backend
66 	 *
67 	 * Possible values for @flags:
68 	 *
69 	 * I915_GEM_OBJECT_SHRINK_WRITEBACK - Try to perform writeback of the
70 	 * backing pages, if supported.
71 	 *
72 	 * I915_GEM_OBJECT_SHRINK_NO_GPU_WAIT - Don't wait for the object to
73 	 * idle.  Active objects can be considered later. The TTM backend for
74 	 * example might have aync migrations going on, which don't use any
75 	 * i915_vma to track the active GTT binding, and hence having an unbound
76 	 * object might not be enough.
77 	 */
78 #define I915_GEM_OBJECT_SHRINK_WRITEBACK   BIT(0)
79 #define I915_GEM_OBJECT_SHRINK_NO_GPU_WAIT BIT(1)
80 	int (*shrink)(struct drm_i915_gem_object *obj, unsigned int flags);
81 
82 	int (*pread)(struct drm_i915_gem_object *obj,
83 		     const struct drm_i915_gem_pread *arg);
84 	int (*pwrite)(struct drm_i915_gem_object *obj,
85 		      const struct drm_i915_gem_pwrite *arg);
86 	u64 (*mmap_offset)(struct drm_i915_gem_object *obj);
87 	void (*unmap_virtual)(struct drm_i915_gem_object *obj);
88 
89 	int (*dmabuf_export)(struct drm_i915_gem_object *obj);
90 
91 	/**
92 	 * adjust_lru - notify that the madvise value was updated
93 	 * @obj: The gem object
94 	 *
95 	 * The madvise value may have been updated, or object was recently
96 	 * referenced so act accordingly (Perhaps changing an LRU list etc).
97 	 */
98 	void (*adjust_lru)(struct drm_i915_gem_object *obj);
99 
100 	/**
101 	 * delayed_free - Override the default delayed free implementation
102 	 */
103 	void (*delayed_free)(struct drm_i915_gem_object *obj);
104 
105 	/**
106 	 * migrate - Migrate object to a different region either for
107 	 * pinning or for as long as the object lock is held.
108 	 */
109 	int (*migrate)(struct drm_i915_gem_object *obj,
110 		       struct intel_memory_region *mr,
111 		       unsigned int flags);
112 
113 	void (*release)(struct drm_i915_gem_object *obj);
114 
115 	const struct vm_operations_struct *mmap_ops;
116 	const char *name; /* friendly name for debug, e.g. lockdep classes */
117 };
118 
119 /**
120  * enum i915_cache_level - The supported GTT caching values for system memory
121  * pages.
122  *
123  * These translate to some special GTT PTE bits when binding pages into some
124  * address space. It also determines whether an object, or rather its pages are
125  * coherent with the GPU, when also reading or writing through the CPU cache
126  * with those pages.
127  *
128  * Userspace can also control this through struct drm_i915_gem_caching.
129  */
130 enum i915_cache_level {
131 	/**
132 	 * @I915_CACHE_NONE:
133 	 *
134 	 * GPU access is not coherent with the CPU cache. If the cache is dirty
135 	 * and we need the underlying pages to be coherent with some later GPU
136 	 * access then we need to manually flush the pages.
137 	 *
138 	 * On shared LLC platforms reads and writes through the CPU cache are
139 	 * still coherent even with this setting. See also
140 	 * &drm_i915_gem_object.cache_coherent for more details. Due to this we
141 	 * should only ever use uncached for scanout surfaces, otherwise we end
142 	 * up over-flushing in some places.
143 	 *
144 	 * This is the default on non-LLC platforms.
145 	 */
146 	I915_CACHE_NONE = 0,
147 	/**
148 	 * @I915_CACHE_LLC:
149 	 *
150 	 * GPU access is coherent with the CPU cache. If the cache is dirty,
151 	 * then the GPU will ensure that access remains coherent, when both
152 	 * reading and writing through the CPU cache. GPU writes can dirty the
153 	 * CPU cache.
154 	 *
155 	 * Not used for scanout surfaces.
156 	 *
157 	 * Applies to both platforms with shared LLC(HAS_LLC), and snooping
158 	 * based platforms(HAS_SNOOP).
159 	 *
160 	 * This is the default on shared LLC platforms.  The only exception is
161 	 * scanout objects, where the display engine is not coherent with the
162 	 * CPU cache. For such objects I915_CACHE_NONE or I915_CACHE_WT is
163 	 * automatically applied by the kernel in pin_for_display, if userspace
164 	 * has not done so already.
165 	 */
166 	I915_CACHE_LLC,
167 	/**
168 	 * @I915_CACHE_L3_LLC:
169 	 *
170 	 * Explicitly enable the Gfx L3 cache, with coherent LLC.
171 	 *
172 	 * The Gfx L3 sits between the domain specific caches, e.g
173 	 * sampler/render caches, and the larger LLC. LLC is coherent with the
174 	 * GPU, but L3 is only visible to the GPU, so likely needs to be flushed
175 	 * when the workload completes.
176 	 *
177 	 * Not used for scanout surfaces.
178 	 *
179 	 * Only exposed on some gen7 + GGTT. More recent hardware has dropped
180 	 * this explicit setting, where it should now be enabled by default.
181 	 */
182 	I915_CACHE_L3_LLC,
183 	/**
184 	 * @I915_CACHE_WT:
185 	 *
186 	 * Write-through. Used for scanout surfaces.
187 	 *
188 	 * The GPU can utilise the caches, while still having the display engine
189 	 * be coherent with GPU writes, as a result we don't need to flush the
190 	 * CPU caches when moving out of the render domain. This is the default
191 	 * setting chosen by the kernel, if supported by the HW, otherwise we
192 	 * fallback to I915_CACHE_NONE. On the CPU side writes through the CPU
193 	 * cache still need to be flushed, to remain coherent with the display
194 	 * engine.
195 	 */
196 	I915_CACHE_WT,
197 	/**
198 	 * @I915_MAX_CACHE_LEVEL:
199 	 *
200 	 * Mark the last entry in the enum. Used for defining cachelevel_to_pat
201 	 * array for cache_level to pat translation table.
202 	 */
203 	I915_MAX_CACHE_LEVEL,
204 };
205 
206 enum i915_map_type {
207 	I915_MAP_WB = 0,
208 	I915_MAP_WC,
209 #define I915_MAP_OVERRIDE BIT(31)
210 	I915_MAP_FORCE_WB = I915_MAP_WB | I915_MAP_OVERRIDE,
211 	I915_MAP_FORCE_WC = I915_MAP_WC | I915_MAP_OVERRIDE,
212 };
213 
214 enum i915_mmap_type {
215 	I915_MMAP_TYPE_GTT = 0,
216 	I915_MMAP_TYPE_WC,
217 	I915_MMAP_TYPE_WB,
218 	I915_MMAP_TYPE_UC,
219 	I915_MMAP_TYPE_FIXED,
220 };
221 
222 struct i915_mmap_offset {
223 	struct drm_vma_offset_node vma_node;
224 	struct drm_i915_gem_object *obj;
225 	enum i915_mmap_type mmap_type;
226 
227 	struct rb_node offset;
228 };
229 
230 struct i915_gem_object_page_iter {
231 	struct scatterlist *sg_pos;
232 	unsigned int sg_idx; /* in pages, but 32bit eek! */
233 
234 	struct radix_tree_root radix;
235 	struct mutex lock; /* protects this cache */
236 };
237 
238 struct drm_i915_gem_object {
239 	/*
240 	 * We might have reason to revisit the below since it wastes
241 	 * a lot of space for non-ttm gem objects.
242 	 * In any case, always use the accessors for the ttm_buffer_object
243 	 * when accessing it.
244 	 */
245 	union {
246 		struct drm_gem_object base;
247 		struct ttm_buffer_object __do_not_access;
248 	};
249 
250 	const struct drm_i915_gem_object_ops *ops;
251 
252 	struct {
253 		/**
254 		 * @vma.lock: protect the list/tree of vmas
255 		 */
256 		spinlock_t lock;
257 
258 		/**
259 		 * @vma.list: List of VMAs backed by this object
260 		 *
261 		 * The VMA on this list are ordered by type, all GGTT vma are
262 		 * placed at the head and all ppGTT vma are placed at the tail.
263 		 * The different types of GGTT vma are unordered between
264 		 * themselves, use the @vma.tree (which has a defined order
265 		 * between all VMA) to quickly find an exact match.
266 		 */
267 		struct list_head list;
268 
269 		/**
270 		 * @vma.tree: Ordered tree of VMAs backed by this object
271 		 *
272 		 * All VMA created for this object are placed in the @vma.tree
273 		 * for fast retrieval via a binary search in
274 		 * i915_vma_instance(). They are also added to @vma.list for
275 		 * easy iteration.
276 		 */
277 		struct rb_root tree;
278 	} vma;
279 
280 	/**
281 	 * @lut_list: List of vma lookup entries in use for this object.
282 	 *
283 	 * If this object is closed, we need to remove all of its VMA from
284 	 * the fast lookup index in associated contexts; @lut_list provides
285 	 * this translation from object to context->handles_vma.
286 	 */
287 	struct list_head lut_list;
288 	spinlock_t lut_lock; /* guards lut_list */
289 
290 	/**
291 	 * @obj_link: Link into @i915_gem_ww_ctx.obj_list
292 	 *
293 	 * When we lock this object through i915_gem_object_lock() with a
294 	 * context, we add it to the list to ensure we can unlock everything
295 	 * when i915_gem_ww_ctx_backoff() or i915_gem_ww_ctx_fini() are called.
296 	 */
297 	struct list_head obj_link;
298 	/**
299 	 * @shared_resv_from: The object shares the resv from this vm.
300 	 */
301 	struct i915_address_space *shares_resv_from;
302 
303 	union {
304 		struct rcu_head rcu;
305 		struct llist_node freed;
306 	};
307 
308 	/**
309 	 * Whether the object is currently in the GGTT or any other supported
310 	 * fake offset mmap backed by lmem.
311 	 */
312 	unsigned int userfault_count;
313 	struct list_head userfault_link;
314 
315 	struct {
316 		spinlock_t lock; /* Protects access to mmo offsets */
317 		struct rb_root offsets;
318 	} mmo;
319 
320 	I915_SELFTEST_DECLARE(struct list_head st_link);
321 
322 	unsigned long flags;
323 #define I915_BO_ALLOC_CONTIGUOUS  BIT(0)
324 #define I915_BO_ALLOC_VOLATILE    BIT(1)
325 #define I915_BO_ALLOC_CPU_CLEAR   BIT(2)
326 #define I915_BO_ALLOC_USER        BIT(3)
327 /* Object is allowed to lose its contents on suspend / resume, even if pinned */
328 #define I915_BO_ALLOC_PM_VOLATILE BIT(4)
329 /* Object needs to be restored early using memcpy during resume */
330 #define I915_BO_ALLOC_PM_EARLY    BIT(5)
331 /*
332  * Object is likely never accessed by the CPU. This will prioritise the BO to be
333  * allocated in the non-mappable portion of lmem. This is merely a hint, and if
334  * dealing with userspace objects the CPU fault handler is free to ignore this.
335  */
336 #define I915_BO_ALLOC_GPU_ONLY	  BIT(6)
337 #define I915_BO_ALLOC_CCS_AUX	  BIT(7)
338 /*
339  * Object is allowed to retain its initial data and will not be cleared on first
340  * access if used along with I915_BO_ALLOC_USER. This is mainly to keep
341  * preallocated framebuffer data intact while transitioning it to i915drmfb.
342  */
343 #define I915_BO_PREALLOC	  BIT(8)
344 #define I915_BO_ALLOC_FLAGS (I915_BO_ALLOC_CONTIGUOUS | \
345 			     I915_BO_ALLOC_VOLATILE | \
346 			     I915_BO_ALLOC_CPU_CLEAR | \
347 			     I915_BO_ALLOC_USER | \
348 			     I915_BO_ALLOC_PM_VOLATILE | \
349 			     I915_BO_ALLOC_PM_EARLY | \
350 			     I915_BO_ALLOC_GPU_ONLY | \
351 			     I915_BO_ALLOC_CCS_AUX | \
352 			     I915_BO_PREALLOC)
353 #define I915_BO_READONLY          BIT(9)
354 #define I915_TILING_QUIRK_BIT     10 /* unknown swizzling; do not release! */
355 #define I915_BO_PROTECTED         BIT(11)
356 	/**
357 	 * @mem_flags - Mutable placement-related flags
358 	 *
359 	 * These are flags that indicate specifics of the memory region
360 	 * the object is currently in. As such they are only stable
361 	 * either under the object lock or if the object is pinned.
362 	 */
363 	unsigned int mem_flags;
364 #define I915_BO_FLAG_STRUCT_PAGE BIT(0) /* Object backed by struct pages */
365 #define I915_BO_FLAG_IOMEM       BIT(1) /* Object backed by IO memory */
366 	/**
367 	 * @pat_index: The desired PAT index.
368 	 *
369 	 * See hardware specification for valid PAT indices for each platform.
370 	 * This field replaces the @cache_level that contains a value of enum
371 	 * i915_cache_level since PAT indices are being used by both userspace
372 	 * and kernel mode driver for caching policy control after GEN12.
373 	 * In the meantime platform specific tables are created to translate
374 	 * i915_cache_level into pat index, for more details check the macros
375 	 * defined i915/i915_pci.c, e.g. PVC_CACHELEVEL.
376 	 * For backward compatibility, this field contains values exactly match
377 	 * the entries of enum i915_cache_level for pre-GEN12 platforms (See
378 	 * LEGACY_CACHELEVEL), so that the PTE encode functions for these
379 	 * legacy platforms can stay the same.
380 	 */
381 	unsigned int pat_index:6;
382 	/**
383 	 * @pat_set_by_user: Indicate whether pat_index is set by user space
384 	 *
385 	 * This field is set to false by default, only set to true if the
386 	 * pat_index is set by user space. By design, user space is capable of
387 	 * managing caching behavior by setting pat_index, in which case this
388 	 * kernel mode driver should never touch the pat_index.
389 	 */
390 	unsigned int pat_set_by_user:1;
391 	/**
392 	 * @cache_coherent:
393 	 *
394 	 * Note: with the change above which replaced @cache_level with pat_index,
395 	 * the use of @cache_coherent is limited to the objects created by kernel
396 	 * or by userspace without pat index specified.
397 	 * Check for @pat_set_by_user to find out if an object has pat index set
398 	 * by userspace. The ioctl's to change cache settings have also been
399 	 * disabled for the objects with pat index set by userspace. Please don't
400 	 * assume @cache_coherent having the flags set as describe here. A helper
401 	 * function i915_gem_object_has_cache_level() provides one way to bypass
402 	 * the use of this field.
403 	 *
404 	 * Track whether the pages are coherent with the GPU if reading or
405 	 * writing through the CPU caches. The largely depends on the
406 	 * @cache_level setting.
407 	 *
408 	 * On platforms which don't have the shared LLC(HAS_SNOOP), like on Atom
409 	 * platforms, coherency must be explicitly requested with some special
410 	 * GTT caching bits(see enum i915_cache_level). When enabling coherency
411 	 * it does come at a performance and power cost on such platforms. On
412 	 * the flip side the kernel does not need to manually flush any buffers
413 	 * which need to be coherent with the GPU, if the object is not coherent
414 	 * i.e @cache_coherent is zero.
415 	 *
416 	 * On platforms that share the LLC with the CPU(HAS_LLC), all GT memory
417 	 * access will automatically snoop the CPU caches(even with CACHE_NONE).
418 	 * The one exception is when dealing with the display engine, like with
419 	 * scanout surfaces. To handle this the kernel will always flush the
420 	 * surface out of the CPU caches when preparing it for scanout.  Also
421 	 * note that since scanout surfaces are only ever read by the display
422 	 * engine we only need to care about flushing any writes through the CPU
423 	 * cache, reads on the other hand will always be coherent.
424 	 *
425 	 * Something strange here is why @cache_coherent is not a simple
426 	 * boolean, i.e coherent vs non-coherent. The reasoning for this is back
427 	 * to the display engine not being fully coherent. As a result scanout
428 	 * surfaces will either be marked as I915_CACHE_NONE or I915_CACHE_WT.
429 	 * In the case of seeing I915_CACHE_NONE the kernel makes the assumption
430 	 * that this is likely a scanout surface, and will set @cache_coherent
431 	 * as only I915_BO_CACHE_COHERENT_FOR_READ, on platforms with the shared
432 	 * LLC. The kernel uses this to always flush writes through the CPU
433 	 * cache as early as possible, where it can, in effect keeping
434 	 * @cache_dirty clean, so we can potentially avoid stalling when
435 	 * flushing the surface just before doing the scanout.  This does mean
436 	 * we might unnecessarily flush non-scanout objects in some places, but
437 	 * the default assumption is that all normal objects should be using
438 	 * I915_CACHE_LLC, at least on platforms with the shared LLC.
439 	 *
440 	 * Supported values:
441 	 *
442 	 * I915_BO_CACHE_COHERENT_FOR_READ:
443 	 *
444 	 * On shared LLC platforms, we use this for special scanout surfaces,
445 	 * where the display engine is not coherent with the CPU cache. As such
446 	 * we need to ensure we flush any writes before doing the scanout. As an
447 	 * optimisation we try to flush any writes as early as possible to avoid
448 	 * stalling later.
449 	 *
450 	 * Thus for scanout surfaces using I915_CACHE_NONE, on shared LLC
451 	 * platforms, we use:
452 	 *
453 	 *	cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ
454 	 *
455 	 * While for normal objects that are fully coherent, including special
456 	 * scanout surfaces marked as I915_CACHE_WT, we use:
457 	 *
458 	 *	cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ |
459 	 *			 I915_BO_CACHE_COHERENT_FOR_WRITE
460 	 *
461 	 * And then for objects that are not coherent at all we use:
462 	 *
463 	 *	cache_coherent = 0
464 	 *
465 	 * I915_BO_CACHE_COHERENT_FOR_WRITE:
466 	 *
467 	 * When writing through the CPU cache, the GPU is still coherent. Note
468 	 * that this also implies I915_BO_CACHE_COHERENT_FOR_READ.
469 	 */
470 #define I915_BO_CACHE_COHERENT_FOR_READ BIT(0)
471 #define I915_BO_CACHE_COHERENT_FOR_WRITE BIT(1)
472 	unsigned int cache_coherent:2;
473 
474 	/**
475 	 * @cache_dirty:
476 	 *
477 	 * Note: with the change above which replaced cache_level with pat_index,
478 	 * the use of @cache_dirty is limited to the objects created by kernel
479 	 * or by userspace without pat index specified.
480 	 * Check for @pat_set_by_user to find out if an object has pat index set
481 	 * by userspace. The ioctl's to change cache settings have also been
482 	 * disabled for the objects with pat_index set by userspace. Please don't
483 	 * assume @cache_dirty is set as describe here. Also see helper function
484 	 * i915_gem_object_has_cache_level() for possible ways to bypass the use
485 	 * of this field.
486 	 *
487 	 * Track if we are we dirty with writes through the CPU cache for this
488 	 * object. As a result reading directly from main memory might yield
489 	 * stale data.
490 	 *
491 	 * This also ties into whether the kernel is tracking the object as
492 	 * coherent with the GPU, as per @cache_coherent, as it determines if
493 	 * flushing might be needed at various points.
494 	 *
495 	 * Another part of @cache_dirty is managing flushing when first
496 	 * acquiring the pages for system memory, at this point the pages are
497 	 * considered foreign, so the default assumption is that the cache is
498 	 * dirty, for example the page zeroing done by the kernel might leave
499 	 * writes though the CPU cache, or swapping-in, while the actual data in
500 	 * main memory is potentially stale.  Note that this is a potential
501 	 * security issue when dealing with userspace objects and zeroing. Now,
502 	 * whether we actually need apply the big sledgehammer of flushing all
503 	 * the pages on acquire depends on if @cache_coherent is marked as
504 	 * I915_BO_CACHE_COHERENT_FOR_WRITE, i.e that the GPU will be coherent
505 	 * for both reads and writes though the CPU cache.
506 	 *
507 	 * Note that on shared LLC platforms we still apply the heavy flush for
508 	 * I915_CACHE_NONE objects, under the assumption that this is going to
509 	 * be used for scanout.
510 	 *
511 	 * Update: On some hardware there is now also the 'Bypass LLC' MOCS
512 	 * entry, which defeats our @cache_coherent tracking, since userspace
513 	 * can freely bypass the CPU cache when touching the pages with the GPU,
514 	 * where the kernel is completely unaware. On such platform we need
515 	 * apply the sledgehammer-on-acquire regardless of the @cache_coherent.
516 	 *
517 	 * Special care is taken on non-LLC platforms, to prevent potential
518 	 * information leak. The driver currently ensures:
519 	 *
520 	 *   1. All userspace objects, by default, have @cache_level set as
521 	 *   I915_CACHE_NONE. The only exception is userptr objects, where we
522 	 *   instead force I915_CACHE_LLC, but we also don't allow userspace to
523 	 *   ever change the @cache_level for such objects. Another special case
524 	 *   is dma-buf, which doesn't rely on @cache_dirty,  but there we
525 	 *   always do a forced flush when acquiring the pages, if there is a
526 	 *   chance that the pages can be read directly from main memory with
527 	 *   the GPU.
528 	 *
529 	 *   2. All I915_CACHE_NONE objects have @cache_dirty initially true.
530 	 *
531 	 *   3. All swapped-out objects(i.e shmem) have @cache_dirty set to
532 	 *   true.
533 	 *
534 	 *   4. The @cache_dirty is never freely reset before the initial
535 	 *   flush, even if userspace adjusts the @cache_level through the
536 	 *   i915_gem_set_caching_ioctl.
537 	 *
538 	 *   5. All @cache_dirty objects(including swapped-in) are initially
539 	 *   flushed with a synchronous call to drm_clflush_sg in
540 	 *   __i915_gem_object_set_pages. The @cache_dirty can be freely reset
541 	 *   at this point. All further asynchronous clfushes are never security
542 	 *   critical, i.e userspace is free to race against itself.
543 	 */
544 	unsigned int cache_dirty:1;
545 
546 	/* @is_dpt: Object houses a display page table (DPT) */
547 	unsigned int is_dpt:1;
548 
549 	/**
550 	 * @read_domains: Read memory domains.
551 	 *
552 	 * These monitor which caches contain read/write data related to the
553 	 * object. When transitioning from one set of domains to another,
554 	 * the driver is called to ensure that caches are suitably flushed and
555 	 * invalidated.
556 	 */
557 	u16 read_domains;
558 
559 	/**
560 	 * @write_domain: Corresponding unique write memory domain.
561 	 */
562 	u16 write_domain;
563 
564 	struct intel_frontbuffer __rcu *frontbuffer;
565 
566 	/** Current tiling stride for the object, if it's tiled. */
567 	unsigned int tiling_and_stride;
568 #define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
569 #define TILING_MASK (FENCE_MINIMUM_STRIDE - 1)
570 #define STRIDE_MASK (~TILING_MASK)
571 
572 	struct {
573 		/*
574 		 * Protects the pages and their use. Do not use directly, but
575 		 * instead go through the pin/unpin interfaces.
576 		 */
577 		atomic_t pages_pin_count;
578 
579 		/**
580 		 * @shrink_pin: Prevents the pages from being made visible to
581 		 * the shrinker, while the shrink_pin is non-zero. Most users
582 		 * should pretty much never have to care about this, outside of
583 		 * some special use cases.
584 		 *
585 		 * By default most objects will start out as visible to the
586 		 * shrinker(if I915_GEM_OBJECT_IS_SHRINKABLE) as soon as the
587 		 * backing pages are attached to the object, like in
588 		 * __i915_gem_object_set_pages(). They will then be removed the
589 		 * shrinker list once the pages are released.
590 		 *
591 		 * The @shrink_pin is incremented by calling
592 		 * i915_gem_object_make_unshrinkable(), which will also remove
593 		 * the object from the shrinker list, if the pin count was zero.
594 		 *
595 		 * Callers will then typically call
596 		 * i915_gem_object_make_shrinkable() or
597 		 * i915_gem_object_make_purgeable() to decrement the pin count,
598 		 * and make the pages visible again.
599 		 */
600 		atomic_t shrink_pin;
601 
602 		/**
603 		 * @ttm_shrinkable: True when the object is using shmem pages
604 		 * underneath. Protected by the object lock.
605 		 */
606 		bool ttm_shrinkable;
607 
608 		/**
609 		 * @unknown_state: Indicate that the object is effectively
610 		 * borked. This is write-once and set if we somehow encounter a
611 		 * fatal error when moving/clearing the pages, and we are not
612 		 * able to fallback to memcpy/memset, like on small-BAR systems.
613 		 * The GPU should also be wedged (or in the process) at this
614 		 * point.
615 		 *
616 		 * Only valid to read this after acquiring the dma-resv lock and
617 		 * waiting for all DMA_RESV_USAGE_KERNEL fences to be signalled,
618 		 * or if we otherwise know that the moving fence has signalled,
619 		 * and we are certain the pages underneath are valid for
620 		 * immediate access (under normal operation), like just prior to
621 		 * binding the object or when setting up the CPU fault handler.
622 		 * See i915_gem_object_has_unknown_state();
623 		 */
624 		bool unknown_state;
625 
626 		/**
627 		 * Priority list of potential placements for this object.
628 		 */
629 		struct intel_memory_region **placements;
630 		int n_placements;
631 
632 		/**
633 		 * Memory region for this object.
634 		 */
635 		struct intel_memory_region *region;
636 
637 		/**
638 		 * Memory manager resource allocated for this object. Only
639 		 * needed for the mock region.
640 		 */
641 		struct ttm_resource *res;
642 
643 		/**
644 		 * Element within memory_region->objects or region->purgeable
645 		 * if the object is marked as DONTNEED. Access is protected by
646 		 * region->obj_lock.
647 		 */
648 		struct list_head region_link;
649 
650 		struct i915_refct_sgt *rsgt;
651 		struct sg_table *pages;
652 		void *mapping;
653 
654 		struct i915_page_sizes page_sizes;
655 
656 		I915_SELFTEST_DECLARE(unsigned int page_mask);
657 
658 		struct i915_gem_object_page_iter get_page;
659 		struct i915_gem_object_page_iter get_dma_page;
660 
661 		/**
662 		 * Element within i915->mm.shrink_list or i915->mm.purge_list,
663 		 * locked by i915->mm.obj_lock.
664 		 */
665 		struct list_head link;
666 
667 		/**
668 		 * Advice: are the backing pages purgeable?
669 		 */
670 		unsigned int madv:2;
671 
672 		/**
673 		 * This is set if the object has been written to since the
674 		 * pages were last acquired.
675 		 */
676 		bool dirty:1;
677 
678 		u32 tlb;
679 	} mm;
680 
681 	struct {
682 		struct i915_refct_sgt *cached_io_rsgt;
683 		struct i915_gem_object_page_iter get_io_page;
684 		struct drm_i915_gem_object *backup;
685 		bool created:1;
686 	} ttm;
687 
688 	/*
689 	 * Record which PXP key instance this object was created against (if
690 	 * any), so we can use it to determine if the encryption is valid by
691 	 * comparing against the current key instance.
692 	 */
693 	u32 pxp_key_instance;
694 
695 	/** Record of address bit 17 of each page at last unbind. */
696 	unsigned long *bit_17;
697 
698 	union {
699 #ifdef CONFIG_MMU_NOTIFIER
700 		struct i915_gem_userptr {
701 			uintptr_t ptr;
702 			unsigned long notifier_seq;
703 
704 			struct mmu_interval_notifier notifier;
705 			struct page **pvec;
706 			int page_ref;
707 		} userptr;
708 #endif
709 
710 		struct drm_mm_node *stolen;
711 
712 		resource_size_t bo_offset;
713 
714 		unsigned long scratch;
715 		u64 encode;
716 
717 		void *gvt_info;
718 	};
719 };
720 
721 static inline struct drm_i915_gem_object *
722 to_intel_bo(struct drm_gem_object *gem)
723 {
724 	/* Assert that to_intel_bo(NULL) == NULL */
725 	BUILD_BUG_ON(offsetof(struct drm_i915_gem_object, base));
726 
727 	return container_of(gem, struct drm_i915_gem_object, base);
728 }
729 
730 #endif
731