10260c420SBen Widawsky /*
20260c420SBen Widawsky  * Copyright © 2014 Intel Corporation
30260c420SBen Widawsky  *
40260c420SBen Widawsky  * Permission is hereby granted, free of charge, to any person obtaining a
50260c420SBen Widawsky  * copy of this software and associated documentation files (the "Software"),
60260c420SBen Widawsky  * to deal in the Software without restriction, including without limitation
70260c420SBen Widawsky  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
80260c420SBen Widawsky  * and/or sell copies of the Software, and to permit persons to whom the
90260c420SBen Widawsky  * Software is furnished to do so, subject to the following conditions:
100260c420SBen Widawsky  *
110260c420SBen Widawsky  * The above copyright notice and this permission notice (including the next
120260c420SBen Widawsky  * paragraph) shall be included in all copies or substantial portions of the
130260c420SBen Widawsky  * Software.
140260c420SBen Widawsky  *
150260c420SBen Widawsky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
160260c420SBen Widawsky  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
170260c420SBen Widawsky  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
180260c420SBen Widawsky  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
190260c420SBen Widawsky  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
200260c420SBen Widawsky  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
210260c420SBen Widawsky  * IN THE SOFTWARE.
220260c420SBen Widawsky  *
230260c420SBen Widawsky  * Please try to maintain the following order within this file unless it makes
240260c420SBen Widawsky  * sense to do otherwise. From top to bottom:
250260c420SBen Widawsky  * 1. typedefs
260260c420SBen Widawsky  * 2. #defines, and macros
270260c420SBen Widawsky  * 3. structure definitions
280260c420SBen Widawsky  * 4. function prototypes
290260c420SBen Widawsky  *
300260c420SBen Widawsky  * Within each section, please try to order by generation in ascending order,
310260c420SBen Widawsky  * from top to bottom (ie. gen6 on the top, gen8 on the bottom).
320260c420SBen Widawsky  */
330260c420SBen Widawsky 
340260c420SBen Widawsky #ifndef __I915_GEM_GTT_H__
350260c420SBen Widawsky #define __I915_GEM_GTT_H__
360260c420SBen Widawsky 
378ef8561fSChris Wilson #include <linux/io-mapping.h>
388ef8561fSChris Wilson 
394d884705SDaniel Vetter struct drm_i915_file_private;
404d884705SDaniel Vetter 
4107749ef3SMichel Thierry typedef uint32_t gen6_pte_t;
4207749ef3SMichel Thierry typedef uint64_t gen8_pte_t;
4307749ef3SMichel Thierry typedef uint64_t gen8_pde_t;
44762d9936SMichel Thierry typedef uint64_t gen8_ppgtt_pdpe_t;
45762d9936SMichel Thierry typedef uint64_t gen8_ppgtt_pml4e_t;
460260c420SBen Widawsky 
4772e96d64SJoonas Lahtinen #define ggtt_total_entries(ggtt) ((ggtt)->base.total >> PAGE_SHIFT)
480260c420SBen Widawsky 
490260c420SBen Widawsky /* gen6-hsw has bit 11-4 for physical addr bit 39-32 */
500260c420SBen Widawsky #define GEN6_GTT_ADDR_ENCODE(addr)	((addr) | (((addr) >> 28) & 0xff0))
510260c420SBen Widawsky #define GEN6_PTE_ADDR_ENCODE(addr)	GEN6_GTT_ADDR_ENCODE(addr)
520260c420SBen Widawsky #define GEN6_PDE_ADDR_ENCODE(addr)	GEN6_GTT_ADDR_ENCODE(addr)
530260c420SBen Widawsky #define GEN6_PTE_CACHE_LLC		(2 << 1)
540260c420SBen Widawsky #define GEN6_PTE_UNCACHED		(1 << 1)
550260c420SBen Widawsky #define GEN6_PTE_VALID			(1 << 0)
560260c420SBen Widawsky 
5707749ef3SMichel Thierry #define I915_PTES(pte_len)		(PAGE_SIZE / (pte_len))
5807749ef3SMichel Thierry #define I915_PTE_MASK(pte_len)		(I915_PTES(pte_len) - 1)
5907749ef3SMichel Thierry #define I915_PDES			512
6007749ef3SMichel Thierry #define I915_PDE_MASK			(I915_PDES - 1)
61678d96fbSBen Widawsky #define NUM_PTE(pde_shift)     (1 << (pde_shift - PAGE_SHIFT))
6207749ef3SMichel Thierry 
6307749ef3SMichel Thierry #define GEN6_PTES			I915_PTES(sizeof(gen6_pte_t))
6407749ef3SMichel Thierry #define GEN6_PD_SIZE		        (I915_PDES * PAGE_SIZE)
650260c420SBen Widawsky #define GEN6_PD_ALIGN			(PAGE_SIZE * 16)
66678d96fbSBen Widawsky #define GEN6_PDE_SHIFT			22
670260c420SBen Widawsky #define GEN6_PDE_VALID			(1 << 0)
680260c420SBen Widawsky 
690260c420SBen Widawsky #define GEN7_PTE_CACHE_L3_LLC		(3 << 1)
700260c420SBen Widawsky 
710260c420SBen Widawsky #define BYT_PTE_SNOOPED_BY_CPU_CACHES	(1 << 2)
720260c420SBen Widawsky #define BYT_PTE_WRITEABLE		(1 << 1)
730260c420SBen Widawsky 
740260c420SBen Widawsky /* Cacheability Control is a 4-bit value. The low three bits are stored in bits
750260c420SBen Widawsky  * 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE.
760260c420SBen Widawsky  */
770260c420SBen Widawsky #define HSW_CACHEABILITY_CONTROL(bits)	((((bits) & 0x7) << 1) | \
780260c420SBen Widawsky 					 (((bits) & 0x8) << (11 - 3)))
790260c420SBen Widawsky #define HSW_WB_LLC_AGE3			HSW_CACHEABILITY_CONTROL(0x2)
800260c420SBen Widawsky #define HSW_WB_LLC_AGE0			HSW_CACHEABILITY_CONTROL(0x3)
810260c420SBen Widawsky #define HSW_WB_ELLC_LLC_AGE3		HSW_CACHEABILITY_CONTROL(0x8)
820260c420SBen Widawsky #define HSW_WB_ELLC_LLC_AGE0		HSW_CACHEABILITY_CONTROL(0xb)
830260c420SBen Widawsky #define HSW_WT_ELLC_LLC_AGE3		HSW_CACHEABILITY_CONTROL(0x7)
840260c420SBen Widawsky #define HSW_WT_ELLC_LLC_AGE0		HSW_CACHEABILITY_CONTROL(0x6)
850260c420SBen Widawsky #define HSW_PTE_UNCACHED		(0)
860260c420SBen Widawsky #define HSW_GTT_ADDR_ENCODE(addr)	((addr) | (((addr) >> 28) & 0x7f0))
870260c420SBen Widawsky #define HSW_PTE_ADDR_ENCODE(addr)	HSW_GTT_ADDR_ENCODE(addr)
880260c420SBen Widawsky 
890260c420SBen Widawsky /* GEN8 legacy style address is defined as a 3 level page table:
900260c420SBen Widawsky  * 31:30 | 29:21 | 20:12 |  11:0
910260c420SBen Widawsky  * PDPE  |  PDE  |  PTE  | offset
920260c420SBen Widawsky  * The difference as compared to normal x86 3 level page table is the PDPEs are
930260c420SBen Widawsky  * programmed via register.
9481ba8aefSMichel Thierry  *
9581ba8aefSMichel Thierry  * GEN8 48b legacy style address is defined as a 4 level page table:
9681ba8aefSMichel Thierry  * 47:39 | 38:30 | 29:21 | 20:12 |  11:0
9781ba8aefSMichel Thierry  * PML4E | PDPE  |  PDE  |  PTE  | offset
980260c420SBen Widawsky  */
9981ba8aefSMichel Thierry #define GEN8_PML4ES_PER_PML4		512
10081ba8aefSMichel Thierry #define GEN8_PML4E_SHIFT		39
101762d9936SMichel Thierry #define GEN8_PML4E_MASK			(GEN8_PML4ES_PER_PML4 - 1)
1020260c420SBen Widawsky #define GEN8_PDPE_SHIFT			30
10381ba8aefSMichel Thierry /* NB: GEN8_PDPE_MASK is untrue for 32b platforms, but it has no impact on 32b page
10481ba8aefSMichel Thierry  * tables */
10581ba8aefSMichel Thierry #define GEN8_PDPE_MASK			0x1ff
1060260c420SBen Widawsky #define GEN8_PDE_SHIFT			21
1070260c420SBen Widawsky #define GEN8_PDE_MASK			0x1ff
1080260c420SBen Widawsky #define GEN8_PTE_SHIFT			12
1090260c420SBen Widawsky #define GEN8_PTE_MASK			0x1ff
11076643600SBen Widawsky #define GEN8_LEGACY_PDPES		4
11107749ef3SMichel Thierry #define GEN8_PTES			I915_PTES(sizeof(gen8_pte_t))
1120260c420SBen Widawsky 
11381ba8aefSMichel Thierry #define I915_PDPES_PER_PDP(dev) (USES_FULL_48BIT_PPGTT(dev) ?\
11481ba8aefSMichel Thierry 				 GEN8_PML4ES_PER_PML4 : GEN8_LEGACY_PDPES)
1156ac18502SMichel Thierry 
1160260c420SBen Widawsky #define PPAT_UNCACHED_INDEX		(_PAGE_PWT | _PAGE_PCD)
1170260c420SBen Widawsky #define PPAT_CACHED_PDE_INDEX		0 /* WB LLC */
1180260c420SBen Widawsky #define PPAT_CACHED_INDEX		_PAGE_PAT /* WB LLCeLLC */
1190260c420SBen Widawsky #define PPAT_DISPLAY_ELLC_INDEX		_PAGE_PCD /* WT eLLC */
1200260c420SBen Widawsky 
121ee0ce478SVille Syrjälä #define CHV_PPAT_SNOOP			(1<<6)
1220260c420SBen Widawsky #define GEN8_PPAT_AGE(x)		(x<<4)
1230260c420SBen Widawsky #define GEN8_PPAT_LLCeLLC		(3<<2)
1240260c420SBen Widawsky #define GEN8_PPAT_LLCELLC		(2<<2)
1250260c420SBen Widawsky #define GEN8_PPAT_LLC			(1<<2)
1260260c420SBen Widawsky #define GEN8_PPAT_WB			(3<<0)
1270260c420SBen Widawsky #define GEN8_PPAT_WT			(2<<0)
1280260c420SBen Widawsky #define GEN8_PPAT_WC			(1<<0)
1290260c420SBen Widawsky #define GEN8_PPAT_UC			(0<<0)
1300260c420SBen Widawsky #define GEN8_PPAT_ELLC_OVERRIDE		(0<<2)
1310260c420SBen Widawsky #define GEN8_PPAT(i, x)			((uint64_t) (x) << ((i) * 8))
1320260c420SBen Widawsky 
133fe14d5f4STvrtko Ursulin enum i915_ggtt_view_type {
134fe14d5f4STvrtko Ursulin 	I915_GGTT_VIEW_NORMAL = 0,
1358bd7ef16SJoonas Lahtinen 	I915_GGTT_VIEW_ROTATED,
1368bd7ef16SJoonas Lahtinen 	I915_GGTT_VIEW_PARTIAL,
13750470bb0STvrtko Ursulin };
13850470bb0STvrtko Ursulin 
13950470bb0STvrtko Ursulin struct intel_rotation_info {
14089e3e142STvrtko Ursulin 	unsigned int uv_offset;
14150470bb0STvrtko Ursulin 	uint32_t pixel_format;
142dedf278cSTvrtko Ursulin 	unsigned int uv_start_page;
1431663b9d6SVille Syrjälä 	struct {
1441663b9d6SVille Syrjälä 		/* tiles */
1451663b9d6SVille Syrjälä 		unsigned int width, height;
1461663b9d6SVille Syrjälä 	} plane[2];
147fe14d5f4STvrtko Ursulin };
148fe14d5f4STvrtko Ursulin 
149fe14d5f4STvrtko Ursulin struct i915_ggtt_view {
150fe14d5f4STvrtko Ursulin 	enum i915_ggtt_view_type type;
151fe14d5f4STvrtko Ursulin 
1528bd7ef16SJoonas Lahtinen 	union {
1538bd7ef16SJoonas Lahtinen 		struct {
154088e0df4SMichel Thierry 			u64 offset;
1558bd7ef16SJoonas Lahtinen 			unsigned int size;
1568bd7ef16SJoonas Lahtinen 		} partial;
1577723f47dSVille Syrjälä 		struct intel_rotation_info rotated;
1588bd7ef16SJoonas Lahtinen 	} params;
1598bd7ef16SJoonas Lahtinen 
160fe14d5f4STvrtko Ursulin 	struct sg_table *pages;
161fe14d5f4STvrtko Ursulin };
162fe14d5f4STvrtko Ursulin 
163fe14d5f4STvrtko Ursulin extern const struct i915_ggtt_view i915_ggtt_view_normal;
1649abc4648SJoonas Lahtinen extern const struct i915_ggtt_view i915_ggtt_view_rotated;
165fe14d5f4STvrtko Ursulin 
1660260c420SBen Widawsky enum i915_cache_level;
167fe14d5f4STvrtko Ursulin 
1680260c420SBen Widawsky /**
1690260c420SBen Widawsky  * A VMA represents a GEM BO that is bound into an address space. Therefore, a
1700260c420SBen Widawsky  * VMA's presence cannot be guaranteed before binding, or after unbinding the
1710260c420SBen Widawsky  * object into/from the address space.
1720260c420SBen Widawsky  *
1730260c420SBen Widawsky  * To make things as simple as possible (ie. no refcounting), a VMA's lifetime
1740260c420SBen Widawsky  * will always be <= an objects lifetime. So object refcounting should cover us.
1750260c420SBen Widawsky  */
1760260c420SBen Widawsky struct i915_vma {
1770260c420SBen Widawsky 	struct drm_mm_node node;
1780260c420SBen Widawsky 	struct drm_i915_gem_object *obj;
1790260c420SBen Widawsky 	struct i915_address_space *vm;
1808ef8561fSChris Wilson 	void __iomem *iomap;
1810260c420SBen Widawsky 
182aff43766STvrtko Ursulin 	/** Flags and address space this VMA is bound to */
183aff43766STvrtko Ursulin #define GLOBAL_BIND	(1<<0)
184aff43766STvrtko Ursulin #define LOCAL_BIND	(1<<1)
185aff43766STvrtko Ursulin 	unsigned int bound : 4;
186596c5923SChris Wilson 	bool is_ggtt : 1;
187aff43766STvrtko Ursulin 
188fe14d5f4STvrtko Ursulin 	/**
189fe14d5f4STvrtko Ursulin 	 * Support different GGTT views into the same object.
190fe14d5f4STvrtko Ursulin 	 * This means there can be multiple VMA mappings per object and per VM.
191fe14d5f4STvrtko Ursulin 	 * i915_ggtt_view_type is used to distinguish between those entries.
192fe14d5f4STvrtko Ursulin 	 * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
193fe14d5f4STvrtko Ursulin 	 * assumed in GEM functions which take no ggtt view parameter.
194fe14d5f4STvrtko Ursulin 	 */
195fe14d5f4STvrtko Ursulin 	struct i915_ggtt_view ggtt_view;
196fe14d5f4STvrtko Ursulin 
1970260c420SBen Widawsky 	/** This object's place on the active/inactive lists */
1981c7f4bcaSChris Wilson 	struct list_head vm_link;
1990260c420SBen Widawsky 
2001c7f4bcaSChris Wilson 	struct list_head obj_link; /* Link in the object's VMA list */
2010260c420SBen Widawsky 
2020260c420SBen Widawsky 	/** This vma's place in the batchbuffer or on the eviction list */
2030260c420SBen Widawsky 	struct list_head exec_list;
2040260c420SBen Widawsky 
2050260c420SBen Widawsky 	/**
2060260c420SBen Widawsky 	 * Used for performing relocations during execbuffer insertion.
2070260c420SBen Widawsky 	 */
2080260c420SBen Widawsky 	struct hlist_node exec_node;
2090260c420SBen Widawsky 	unsigned long exec_handle;
2100260c420SBen Widawsky 	struct drm_i915_gem_exec_object2 *exec_entry;
2110260c420SBen Widawsky 
2120260c420SBen Widawsky 	/**
2130260c420SBen Widawsky 	 * How many users have pinned this object in GTT space. The following
2144feb7659SDaniel Vetter 	 * users can each hold at most one reference: pwrite/pread, execbuffer
2154feb7659SDaniel Vetter 	 * (objects are not allowed multiple times for the same batchbuffer),
2164feb7659SDaniel Vetter 	 * and the framebuffer code. When switching/pageflipping, the
2174feb7659SDaniel Vetter 	 * framebuffer code has at most two buffers pinned per crtc.
2180260c420SBen Widawsky 	 *
2190260c420SBen Widawsky 	 * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3
2200260c420SBen Widawsky 	 * bits with absolutely no headroom. So use 4 bits. */
2210260c420SBen Widawsky 	unsigned int pin_count:4;
2220260c420SBen Widawsky #define DRM_I915_GEM_OBJECT_MAX_PIN_COUNT 0xf
2230260c420SBen Widawsky };
2240260c420SBen Widawsky 
22544159ddbSMika Kuoppala struct i915_page_dma {
226d7b3de91SBen Widawsky 	struct page *page;
22744159ddbSMika Kuoppala 	union {
2287324cc04SBen Widawsky 		dma_addr_t daddr;
229678d96fbSBen Widawsky 
23044159ddbSMika Kuoppala 		/* For gen6/gen7 only. This is the offset in the GGTT
23144159ddbSMika Kuoppala 		 * where the page directory entries for PPGTT begin
23244159ddbSMika Kuoppala 		 */
23344159ddbSMika Kuoppala 		uint32_t ggtt_offset;
23444159ddbSMika Kuoppala 	};
23544159ddbSMika Kuoppala };
23644159ddbSMika Kuoppala 
237567047beSMika Kuoppala #define px_base(px) (&(px)->base)
238567047beSMika Kuoppala #define px_page(px) (px_base(px)->page)
239567047beSMika Kuoppala #define px_dma(px) (px_base(px)->daddr)
240567047beSMika Kuoppala 
241c114f76aSMika Kuoppala struct i915_page_scratch {
242c114f76aSMika Kuoppala 	struct i915_page_dma base;
243c114f76aSMika Kuoppala };
244c114f76aSMika Kuoppala 
24544159ddbSMika Kuoppala struct i915_page_table {
24644159ddbSMika Kuoppala 	struct i915_page_dma base;
24744159ddbSMika Kuoppala 
248678d96fbSBen Widawsky 	unsigned long *used_ptes;
249d7b3de91SBen Widawsky };
250d7b3de91SBen Widawsky 
251ec565b3cSMichel Thierry struct i915_page_directory {
25244159ddbSMika Kuoppala 	struct i915_page_dma base;
2537324cc04SBen Widawsky 
25433c8819fSMichel Thierry 	unsigned long *used_pdes;
255ec565b3cSMichel Thierry 	struct i915_page_table *page_table[I915_PDES]; /* PDEs */
256d7b3de91SBen Widawsky };
257d7b3de91SBen Widawsky 
258ec565b3cSMichel Thierry struct i915_page_directory_pointer {
2596ac18502SMichel Thierry 	struct i915_page_dma base;
2606ac18502SMichel Thierry 
2616ac18502SMichel Thierry 	unsigned long *used_pdpes;
2626ac18502SMichel Thierry 	struct i915_page_directory **page_directory;
263d7b3de91SBen Widawsky };
264d7b3de91SBen Widawsky 
26581ba8aefSMichel Thierry struct i915_pml4 {
26681ba8aefSMichel Thierry 	struct i915_page_dma base;
26781ba8aefSMichel Thierry 
26881ba8aefSMichel Thierry 	DECLARE_BITMAP(used_pml4es, GEN8_PML4ES_PER_PML4);
26981ba8aefSMichel Thierry 	struct i915_page_directory_pointer *pdps[GEN8_PML4ES_PER_PML4];
27081ba8aefSMichel Thierry };
27181ba8aefSMichel Thierry 
2720260c420SBen Widawsky struct i915_address_space {
2730260c420SBen Widawsky 	struct drm_mm mm;
2740260c420SBen Widawsky 	struct drm_device *dev;
2750260c420SBen Widawsky 	struct list_head global_link;
276c44ef60eSMika Kuoppala 	u64 start;		/* Start offset always 0 for dri2 */
277c44ef60eSMika Kuoppala 	u64 total;		/* size addr space maps (ex. 2GB for ggtt) */
2780260c420SBen Widawsky 
279596c5923SChris Wilson 	bool is_ggtt;
280596c5923SChris Wilson 
281c114f76aSMika Kuoppala 	struct i915_page_scratch *scratch_page;
28279ab9370SMika Kuoppala 	struct i915_page_table *scratch_pt;
28379ab9370SMika Kuoppala 	struct i915_page_directory *scratch_pd;
28469ab76fdSMichel Thierry 	struct i915_page_directory_pointer *scratch_pdp; /* GEN8+ & 48b PPGTT */
2850260c420SBen Widawsky 
2860260c420SBen Widawsky 	/**
2870260c420SBen Widawsky 	 * List of objects currently involved in rendering.
2880260c420SBen Widawsky 	 *
2890260c420SBen Widawsky 	 * Includes buffers having the contents of their GPU caches
29097b2a6a1SJohn Harrison 	 * flushed, not necessarily primitives. last_read_req
2910260c420SBen Widawsky 	 * represents when the rendering involved will be completed.
2920260c420SBen Widawsky 	 *
2930260c420SBen Widawsky 	 * A reference is held on the buffer while on this list.
2940260c420SBen Widawsky 	 */
2950260c420SBen Widawsky 	struct list_head active_list;
2960260c420SBen Widawsky 
2970260c420SBen Widawsky 	/**
2980260c420SBen Widawsky 	 * LRU list of objects which are not in the ringbuffer and
2990260c420SBen Widawsky 	 * are ready to unbind, but are still in the GTT.
3000260c420SBen Widawsky 	 *
30197b2a6a1SJohn Harrison 	 * last_read_req is NULL while an object is in this list.
3020260c420SBen Widawsky 	 *
3030260c420SBen Widawsky 	 * A reference is not held on the buffer while on this list,
3040260c420SBen Widawsky 	 * as merely being GTT-bound shouldn't prevent its being
3050260c420SBen Widawsky 	 * freed, and we'll pull it off the list in the free path.
3060260c420SBen Widawsky 	 */
3070260c420SBen Widawsky 	struct list_head inactive_list;
3080260c420SBen Widawsky 
3090260c420SBen Widawsky 	/* FIXME: Need a more generic return type */
31007749ef3SMichel Thierry 	gen6_pte_t (*pte_encode)(dma_addr_t addr,
3110260c420SBen Widawsky 				 enum i915_cache_level level,
31224f3a8cfSAkash Goel 				 bool valid, u32 flags); /* Create a valid PTE */
313f329f5f6SDaniel Vetter 	/* flags for pte_encode */
314f329f5f6SDaniel Vetter #define PTE_READ_ONLY	(1<<0)
315678d96fbSBen Widawsky 	int (*allocate_va_range)(struct i915_address_space *vm,
316678d96fbSBen Widawsky 				 uint64_t start,
317678d96fbSBen Widawsky 				 uint64_t length);
3180260c420SBen Widawsky 	void (*clear_range)(struct i915_address_space *vm,
3190260c420SBen Widawsky 			    uint64_t start,
3200260c420SBen Widawsky 			    uint64_t length,
3210260c420SBen Widawsky 			    bool use_scratch);
322d6473f56SChris Wilson 	void (*insert_page)(struct i915_address_space *vm,
323d6473f56SChris Wilson 			    dma_addr_t addr,
324d6473f56SChris Wilson 			    uint64_t offset,
325d6473f56SChris Wilson 			    enum i915_cache_level cache_level,
326d6473f56SChris Wilson 			    u32 flags);
3270260c420SBen Widawsky 	void (*insert_entries)(struct i915_address_space *vm,
3280260c420SBen Widawsky 			       struct sg_table *st,
3290260c420SBen Widawsky 			       uint64_t start,
33024f3a8cfSAkash Goel 			       enum i915_cache_level cache_level, u32 flags);
3310260c420SBen Widawsky 	void (*cleanup)(struct i915_address_space *vm);
332777dc5bbSDaniel Vetter 	/** Unmap an object from an address space. This usually consists of
333777dc5bbSDaniel Vetter 	 * setting the valid PTE entries to a reserved scratch page. */
334777dc5bbSDaniel Vetter 	void (*unbind_vma)(struct i915_vma *vma);
335777dc5bbSDaniel Vetter 	/* Map an object into an address space with the given cache flags. */
33670b9f6f8SDaniel Vetter 	int (*bind_vma)(struct i915_vma *vma,
337777dc5bbSDaniel Vetter 			enum i915_cache_level cache_level,
338777dc5bbSDaniel Vetter 			u32 flags);
3390260c420SBen Widawsky };
3400260c420SBen Widawsky 
341596c5923SChris Wilson #define i915_is_ggtt(V) ((V)->is_ggtt)
342596c5923SChris Wilson 
3430260c420SBen Widawsky /* The Graphics Translation Table is the way in which GEN hardware translates a
3440260c420SBen Widawsky  * Graphics Virtual Address into a Physical Address. In addition to the normal
3450260c420SBen Widawsky  * collateral associated with any va->pa translations GEN hardware also has a
3460260c420SBen Widawsky  * portion of the GTT which can be mapped by the CPU and remain both coherent
3470260c420SBen Widawsky  * and correct (in cases like swizzling). That region is referred to as GMADR in
3480260c420SBen Widawsky  * the spec.
3490260c420SBen Widawsky  */
35062106b4fSJoonas Lahtinen struct i915_ggtt {
3510260c420SBen Widawsky 	struct i915_address_space base;
3520260c420SBen Widawsky 
353c44ef60eSMika Kuoppala 	size_t stolen_size;		/* Total size of stolen memory */
354a9da512bSPaulo Zanoni 	size_t stolen_usable_size;	/* Total size minus BIOS reserved */
355274008e8SSagar Arun Kamble 	size_t stolen_reserved_base;
356274008e8SSagar Arun Kamble 	size_t stolen_reserved_size;
357d507d735SJoonas Lahtinen 	size_t size;			/* Total size of Global GTT */
358c44ef60eSMika Kuoppala 	u64 mappable_end;		/* End offset that we can CPU map */
3590260c420SBen Widawsky 	struct io_mapping *mappable;	/* Mapping to our CPU mappable region */
3600260c420SBen Widawsky 	phys_addr_t mappable_base;	/* PA of our GMADR */
3610260c420SBen Widawsky 
3620260c420SBen Widawsky 	/** "Graphics Stolen Memory" holds the global PTEs */
3630260c420SBen Widawsky 	void __iomem *gsm;
3640260c420SBen Widawsky 
3650260c420SBen Widawsky 	bool do_idle_maps;
3660260c420SBen Widawsky 
3670260c420SBen Widawsky 	int mtrr;
3680260c420SBen Widawsky 
369d507d735SJoonas Lahtinen 	int (*probe)(struct i915_ggtt *ggtt);
3700260c420SBen Widawsky };
3710260c420SBen Widawsky 
3720260c420SBen Widawsky struct i915_hw_ppgtt {
3730260c420SBen Widawsky 	struct i915_address_space base;
3740260c420SBen Widawsky 	struct kref ref;
3750260c420SBen Widawsky 	struct drm_mm_node node;
376563222a7SBen Widawsky 	unsigned long pd_dirty_rings;
3770260c420SBen Widawsky 	union {
37881ba8aefSMichel Thierry 		struct i915_pml4 pml4;		/* GEN8+ & 48b PPGTT */
37981ba8aefSMichel Thierry 		struct i915_page_directory_pointer pdp;	/* GEN8+ */
38081ba8aefSMichel Thierry 		struct i915_page_directory pd;		/* GEN6-7 */
381d7b3de91SBen Widawsky 	};
3820260c420SBen Widawsky 
3834d884705SDaniel Vetter 	struct drm_i915_file_private *file_priv;
3840260c420SBen Widawsky 
385678d96fbSBen Widawsky 	gen6_pte_t __iomem *pd_addr;
386678d96fbSBen Widawsky 
3870260c420SBen Widawsky 	int (*enable)(struct i915_hw_ppgtt *ppgtt);
3880260c420SBen Widawsky 	int (*switch_mm)(struct i915_hw_ppgtt *ppgtt,
389e85b26dcSJohn Harrison 			 struct drm_i915_gem_request *req);
3900260c420SBen Widawsky 	void (*debug_dump)(struct i915_hw_ppgtt *ppgtt, struct seq_file *m);
3910260c420SBen Widawsky };
3920260c420SBen Widawsky 
393731f74c5SDave Gordon /*
394731f74c5SDave Gordon  * gen6_for_each_pde() iterates over every pde from start until start+length.
395731f74c5SDave Gordon  * If start and start+length are not perfectly divisible, the macro will round
396731f74c5SDave Gordon  * down and up as needed. Start=0 and length=2G effectively iterates over
397731f74c5SDave Gordon  * every PDE in the system. The macro modifies ALL its parameters except 'pd',
398731f74c5SDave Gordon  * so each of the other parameters should preferably be a simple variable, or
399731f74c5SDave Gordon  * at most an lvalue with no side-effects!
400678d96fbSBen Widawsky  */
401731f74c5SDave Gordon #define gen6_for_each_pde(pt, pd, start, length, iter)			\
402fdc454c1SMichel Thierry 	for (iter = gen6_pde_index(start);				\
403731f74c5SDave Gordon 	     length > 0 && iter < I915_PDES &&				\
404731f74c5SDave Gordon 		(pt = (pd)->page_table[iter], true);			\
405731f74c5SDave Gordon 	     ({ u32 temp = ALIGN(start+1, 1 << GEN6_PDE_SHIFT);		\
406731f74c5SDave Gordon 		    temp = min(temp - start, length);			\
407731f74c5SDave Gordon 		    start += temp, length -= temp; }), ++iter)
408678d96fbSBen Widawsky 
409731f74c5SDave Gordon #define gen6_for_all_pdes(pt, pd, iter)					\
41009942c65SMichel Thierry 	for (iter = 0;							\
411731f74c5SDave Gordon 	     iter < I915_PDES &&					\
412731f74c5SDave Gordon 		(pt = (pd)->page_table[iter], true);			\
413731f74c5SDave Gordon 	     ++iter)
41409942c65SMichel Thierry 
415678d96fbSBen Widawsky static inline uint32_t i915_pte_index(uint64_t address, uint32_t pde_shift)
416678d96fbSBen Widawsky {
417678d96fbSBen Widawsky 	const uint32_t mask = NUM_PTE(pde_shift) - 1;
418678d96fbSBen Widawsky 
419678d96fbSBen Widawsky 	return (address >> PAGE_SHIFT) & mask;
420678d96fbSBen Widawsky }
421678d96fbSBen Widawsky 
422678d96fbSBen Widawsky /* Helper to counts the number of PTEs within the given length. This count
423678d96fbSBen Widawsky  * does not cross a page table boundary, so the max value would be
424678d96fbSBen Widawsky  * GEN6_PTES for GEN6, and GEN8_PTES for GEN8.
425678d96fbSBen Widawsky */
426678d96fbSBen Widawsky static inline uint32_t i915_pte_count(uint64_t addr, size_t length,
427678d96fbSBen Widawsky 				      uint32_t pde_shift)
428678d96fbSBen Widawsky {
42969603dbbSAlan 	const uint64_t mask = ~((1ULL << pde_shift) - 1);
430678d96fbSBen Widawsky 	uint64_t end;
431678d96fbSBen Widawsky 
432678d96fbSBen Widawsky 	WARN_ON(length == 0);
433678d96fbSBen Widawsky 	WARN_ON(offset_in_page(addr|length));
434678d96fbSBen Widawsky 
435678d96fbSBen Widawsky 	end = addr + length;
436678d96fbSBen Widawsky 
437678d96fbSBen Widawsky 	if ((addr & mask) != (end & mask))
438678d96fbSBen Widawsky 		return NUM_PTE(pde_shift) - i915_pte_index(addr, pde_shift);
439678d96fbSBen Widawsky 
440678d96fbSBen Widawsky 	return i915_pte_index(end, pde_shift) - i915_pte_index(addr, pde_shift);
441678d96fbSBen Widawsky }
442678d96fbSBen Widawsky 
443678d96fbSBen Widawsky static inline uint32_t i915_pde_index(uint64_t addr, uint32_t shift)
444678d96fbSBen Widawsky {
445678d96fbSBen Widawsky 	return (addr >> shift) & I915_PDE_MASK;
446678d96fbSBen Widawsky }
447678d96fbSBen Widawsky 
448678d96fbSBen Widawsky static inline uint32_t gen6_pte_index(uint32_t addr)
449678d96fbSBen Widawsky {
450678d96fbSBen Widawsky 	return i915_pte_index(addr, GEN6_PDE_SHIFT);
451678d96fbSBen Widawsky }
452678d96fbSBen Widawsky 
453678d96fbSBen Widawsky static inline size_t gen6_pte_count(uint32_t addr, uint32_t length)
454678d96fbSBen Widawsky {
455678d96fbSBen Widawsky 	return i915_pte_count(addr, length, GEN6_PDE_SHIFT);
456678d96fbSBen Widawsky }
457678d96fbSBen Widawsky 
458678d96fbSBen Widawsky static inline uint32_t gen6_pde_index(uint32_t addr)
459678d96fbSBen Widawsky {
460678d96fbSBen Widawsky 	return i915_pde_index(addr, GEN6_PDE_SHIFT);
461678d96fbSBen Widawsky }
462678d96fbSBen Widawsky 
4639271d959SMichel Thierry /* Equivalent to the gen6 version, For each pde iterates over every pde
4649271d959SMichel Thierry  * between from start until start + length. On gen8+ it simply iterates
4659271d959SMichel Thierry  * over every page directory entry in a page directory.
4669271d959SMichel Thierry  */
467e8ebd8e2SDave Gordon #define gen8_for_each_pde(pt, pd, start, length, iter)			\
4689271d959SMichel Thierry 	for (iter = gen8_pde_index(start);				\
469e8ebd8e2SDave Gordon 	     length > 0 && iter < I915_PDES &&				\
470e8ebd8e2SDave Gordon 		(pt = (pd)->page_table[iter], true);			\
471e8ebd8e2SDave Gordon 	     ({ u64 temp = ALIGN(start+1, 1 << GEN8_PDE_SHIFT);		\
472e8ebd8e2SDave Gordon 		    temp = min(temp - start, length);			\
473e8ebd8e2SDave Gordon 		    start += temp, length -= temp; }), ++iter)
4749271d959SMichel Thierry 
475e8ebd8e2SDave Gordon #define gen8_for_each_pdpe(pd, pdp, start, length, iter)		\
4769271d959SMichel Thierry 	for (iter = gen8_pdpe_index(start);				\
477e8ebd8e2SDave Gordon 	     length > 0 && iter < I915_PDPES_PER_PDP(dev) &&		\
478e8ebd8e2SDave Gordon 		(pd = (pdp)->page_directory[iter], true);		\
479e8ebd8e2SDave Gordon 	     ({ u64 temp = ALIGN(start+1, 1 << GEN8_PDPE_SHIFT);	\
480e8ebd8e2SDave Gordon 		    temp = min(temp - start, length);			\
481e8ebd8e2SDave Gordon 		    start += temp, length -= temp; }), ++iter)
4829271d959SMichel Thierry 
483e8ebd8e2SDave Gordon #define gen8_for_each_pml4e(pdp, pml4, start, length, iter)		\
484762d9936SMichel Thierry 	for (iter = gen8_pml4e_index(start);				\
485e8ebd8e2SDave Gordon 	     length > 0 && iter < GEN8_PML4ES_PER_PML4 &&		\
486e8ebd8e2SDave Gordon 		(pdp = (pml4)->pdps[iter], true);			\
487e8ebd8e2SDave Gordon 	     ({ u64 temp = ALIGN(start+1, 1ULL << GEN8_PML4E_SHIFT);	\
488e8ebd8e2SDave Gordon 		    temp = min(temp - start, length);			\
489e8ebd8e2SDave Gordon 		    start += temp, length -= temp; }), ++iter)
490762d9936SMichel Thierry 
4919271d959SMichel Thierry static inline uint32_t gen8_pte_index(uint64_t address)
4929271d959SMichel Thierry {
4939271d959SMichel Thierry 	return i915_pte_index(address, GEN8_PDE_SHIFT);
4949271d959SMichel Thierry }
4959271d959SMichel Thierry 
4969271d959SMichel Thierry static inline uint32_t gen8_pde_index(uint64_t address)
4979271d959SMichel Thierry {
4989271d959SMichel Thierry 	return i915_pde_index(address, GEN8_PDE_SHIFT);
4999271d959SMichel Thierry }
5009271d959SMichel Thierry 
5019271d959SMichel Thierry static inline uint32_t gen8_pdpe_index(uint64_t address)
5029271d959SMichel Thierry {
5039271d959SMichel Thierry 	return (address >> GEN8_PDPE_SHIFT) & GEN8_PDPE_MASK;
5049271d959SMichel Thierry }
5059271d959SMichel Thierry 
5069271d959SMichel Thierry static inline uint32_t gen8_pml4e_index(uint64_t address)
5079271d959SMichel Thierry {
508762d9936SMichel Thierry 	return (address >> GEN8_PML4E_SHIFT) & GEN8_PML4E_MASK;
5099271d959SMichel Thierry }
5109271d959SMichel Thierry 
51133c8819fSMichel Thierry static inline size_t gen8_pte_count(uint64_t address, uint64_t length)
51233c8819fSMichel Thierry {
51333c8819fSMichel Thierry 	return i915_pte_count(address, length, GEN8_PDE_SHIFT);
51433c8819fSMichel Thierry }
51533c8819fSMichel Thierry 
516d852c7bfSMika Kuoppala static inline dma_addr_t
517d852c7bfSMika Kuoppala i915_page_dir_dma_addr(const struct i915_hw_ppgtt *ppgtt, const unsigned n)
518d852c7bfSMika Kuoppala {
519d852c7bfSMika Kuoppala 	return test_bit(n, ppgtt->pdp.used_pdpes) ?
520567047beSMika Kuoppala 		px_dma(ppgtt->pdp.page_directory[n]) :
52179ab9370SMika Kuoppala 		px_dma(ppgtt->base.scratch_pd);
522d852c7bfSMika Kuoppala }
523d852c7bfSMika Kuoppala 
524d85489d3SJoonas Lahtinen int i915_ggtt_init_hw(struct drm_device *dev);
525ac840ae5SVille Syrjälä int i915_ggtt_enable_hw(struct drm_device *dev);
526d85489d3SJoonas Lahtinen void i915_gem_init_ggtt(struct drm_device *dev);
527d85489d3SJoonas Lahtinen void i915_ggtt_cleanup_hw(struct drm_device *dev);
528ee960be7SDaniel Vetter 
52982460d97SDaniel Vetter int i915_ppgtt_init_hw(struct drm_device *dev);
530ee960be7SDaniel Vetter void i915_ppgtt_release(struct kref *kref);
5314d884705SDaniel Vetter struct i915_hw_ppgtt *i915_ppgtt_create(struct drm_device *dev,
5324d884705SDaniel Vetter 					struct drm_i915_file_private *fpriv);
533ee960be7SDaniel Vetter static inline void i915_ppgtt_get(struct i915_hw_ppgtt *ppgtt)
534ee960be7SDaniel Vetter {
535ee960be7SDaniel Vetter 	if (ppgtt)
536ee960be7SDaniel Vetter 		kref_get(&ppgtt->ref);
537ee960be7SDaniel Vetter }
538ee960be7SDaniel Vetter static inline void i915_ppgtt_put(struct i915_hw_ppgtt *ppgtt)
539ee960be7SDaniel Vetter {
540ee960be7SDaniel Vetter 	if (ppgtt)
541ee960be7SDaniel Vetter 		kref_put(&ppgtt->ref, i915_ppgtt_release);
542ee960be7SDaniel Vetter }
5430260c420SBen Widawsky 
544dc97997aSChris Wilson void i915_check_and_clear_faults(struct drm_i915_private *dev_priv);
5450260c420SBen Widawsky void i915_gem_suspend_gtt_mappings(struct drm_device *dev);
5460260c420SBen Widawsky void i915_gem_restore_gtt_mappings(struct drm_device *dev);
5470260c420SBen Widawsky 
5480260c420SBen Widawsky int __must_check i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj);
5490260c420SBen Widawsky void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj);
5500260c420SBen Widawsky 
5519abc4648SJoonas Lahtinen static inline bool
5529abc4648SJoonas Lahtinen i915_ggtt_view_equal(const struct i915_ggtt_view *a,
5539abc4648SJoonas Lahtinen                      const struct i915_ggtt_view *b)
5549abc4648SJoonas Lahtinen {
5559abc4648SJoonas Lahtinen 	if (WARN_ON(!a || !b))
5569abc4648SJoonas Lahtinen 		return false;
5579abc4648SJoonas Lahtinen 
5588bd7ef16SJoonas Lahtinen 	if (a->type != b->type)
5598bd7ef16SJoonas Lahtinen 		return false;
560ce7f1728SDaniel Vetter 	if (a->type != I915_GGTT_VIEW_NORMAL)
5618bd7ef16SJoonas Lahtinen 		return !memcmp(&a->params, &b->params, sizeof(a->params));
5628bd7ef16SJoonas Lahtinen 	return true;
5639abc4648SJoonas Lahtinen }
5649abc4648SJoonas Lahtinen 
56591e6711eSJoonas Lahtinen size_t
56691e6711eSJoonas Lahtinen i915_ggtt_view_size(struct drm_i915_gem_object *obj,
56791e6711eSJoonas Lahtinen 		    const struct i915_ggtt_view *view);
56891e6711eSJoonas Lahtinen 
5698ef8561fSChris Wilson /**
5708ef8561fSChris Wilson  * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
5718ef8561fSChris Wilson  * @vma: VMA to iomap
5728ef8561fSChris Wilson  *
5738ef8561fSChris Wilson  * The passed in VMA has to be pinned in the global GTT mappable region.
5748ef8561fSChris Wilson  * An extra pinning of the VMA is acquired for the return iomapping,
5758ef8561fSChris Wilson  * the caller must call i915_vma_unpin_iomap to relinquish the pinning
5768ef8561fSChris Wilson  * after the iomapping is no longer required.
5778ef8561fSChris Wilson  *
5788ef8561fSChris Wilson  * Callers must hold the struct_mutex.
5798ef8561fSChris Wilson  *
5808ef8561fSChris Wilson  * Returns a valid iomapped pointer or ERR_PTR.
5818ef8561fSChris Wilson  */
5828ef8561fSChris Wilson void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
583406ea8d2SChris Wilson #define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
5848ef8561fSChris Wilson 
5858ef8561fSChris Wilson /**
5868ef8561fSChris Wilson  * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
5878ef8561fSChris Wilson  * @vma: VMA to unpin
5888ef8561fSChris Wilson  *
5898ef8561fSChris Wilson  * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
5908ef8561fSChris Wilson  *
5918ef8561fSChris Wilson  * Callers must hold the struct_mutex. This function is only valid to be
5928ef8561fSChris Wilson  * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap().
5938ef8561fSChris Wilson  */
5948ef8561fSChris Wilson static inline void i915_vma_unpin_iomap(struct i915_vma *vma)
5958ef8561fSChris Wilson {
5968ef8561fSChris Wilson 	lockdep_assert_held(&vma->vm->dev->struct_mutex);
5978ef8561fSChris Wilson 	GEM_BUG_ON(vma->pin_count == 0);
5988ef8561fSChris Wilson 	GEM_BUG_ON(vma->iomap == NULL);
5998ef8561fSChris Wilson 	vma->pin_count--;
6008ef8561fSChris Wilson }
6018ef8561fSChris Wilson 
6020260c420SBen Widawsky #endif
603