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>
38b42fe9caSJoonas Lahtinen #include <linux/mm.h>
398ef8561fSChris Wilson 
40b42fe9caSJoonas Lahtinen #include "i915_gem_timeline.h"
41b0decaf7SChris Wilson #include "i915_gem_request.h"
42b0decaf7SChris Wilson 
4349ef5294SChris Wilson #define I915_FENCE_REG_NONE -1
4449ef5294SChris Wilson #define I915_MAX_NUM_FENCES 32
4549ef5294SChris Wilson /* 32 fences + sign bit for FENCE_REG_NONE */
4649ef5294SChris Wilson #define I915_MAX_NUM_FENCE_BITS 6
4749ef5294SChris Wilson 
484d884705SDaniel Vetter struct drm_i915_file_private;
4949ef5294SChris Wilson struct drm_i915_fence_reg;
504d884705SDaniel Vetter 
5107749ef3SMichel Thierry typedef uint32_t gen6_pte_t;
5207749ef3SMichel Thierry typedef uint64_t gen8_pte_t;
5307749ef3SMichel Thierry typedef uint64_t gen8_pde_t;
54762d9936SMichel Thierry typedef uint64_t gen8_ppgtt_pdpe_t;
55762d9936SMichel Thierry typedef uint64_t gen8_ppgtt_pml4e_t;
560260c420SBen Widawsky 
5772e96d64SJoonas Lahtinen #define ggtt_total_entries(ggtt) ((ggtt)->base.total >> PAGE_SHIFT)
580260c420SBen Widawsky 
590260c420SBen Widawsky /* gen6-hsw has bit 11-4 for physical addr bit 39-32 */
600260c420SBen Widawsky #define GEN6_GTT_ADDR_ENCODE(addr)	((addr) | (((addr) >> 28) & 0xff0))
610260c420SBen Widawsky #define GEN6_PTE_ADDR_ENCODE(addr)	GEN6_GTT_ADDR_ENCODE(addr)
620260c420SBen Widawsky #define GEN6_PDE_ADDR_ENCODE(addr)	GEN6_GTT_ADDR_ENCODE(addr)
630260c420SBen Widawsky #define GEN6_PTE_CACHE_LLC		(2 << 1)
640260c420SBen Widawsky #define GEN6_PTE_UNCACHED		(1 << 1)
650260c420SBen Widawsky #define GEN6_PTE_VALID			(1 << 0)
660260c420SBen Widawsky 
6707749ef3SMichel Thierry #define I915_PTES(pte_len)		(PAGE_SIZE / (pte_len))
6807749ef3SMichel Thierry #define I915_PTE_MASK(pte_len)		(I915_PTES(pte_len) - 1)
6907749ef3SMichel Thierry #define I915_PDES			512
7007749ef3SMichel Thierry #define I915_PDE_MASK			(I915_PDES - 1)
71678d96fbSBen Widawsky #define NUM_PTE(pde_shift)     (1 << (pde_shift - PAGE_SHIFT))
7207749ef3SMichel Thierry 
7307749ef3SMichel Thierry #define GEN6_PTES			I915_PTES(sizeof(gen6_pte_t))
7407749ef3SMichel Thierry #define GEN6_PD_SIZE		        (I915_PDES * PAGE_SIZE)
750260c420SBen Widawsky #define GEN6_PD_ALIGN			(PAGE_SIZE * 16)
76678d96fbSBen Widawsky #define GEN6_PDE_SHIFT			22
770260c420SBen Widawsky #define GEN6_PDE_VALID			(1 << 0)
780260c420SBen Widawsky 
790260c420SBen Widawsky #define GEN7_PTE_CACHE_L3_LLC		(3 << 1)
800260c420SBen Widawsky 
810260c420SBen Widawsky #define BYT_PTE_SNOOPED_BY_CPU_CACHES	(1 << 2)
820260c420SBen Widawsky #define BYT_PTE_WRITEABLE		(1 << 1)
830260c420SBen Widawsky 
840260c420SBen Widawsky /* Cacheability Control is a 4-bit value. The low three bits are stored in bits
850260c420SBen Widawsky  * 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE.
860260c420SBen Widawsky  */
870260c420SBen Widawsky #define HSW_CACHEABILITY_CONTROL(bits)	((((bits) & 0x7) << 1) | \
880260c420SBen Widawsky 					 (((bits) & 0x8) << (11 - 3)))
890260c420SBen Widawsky #define HSW_WB_LLC_AGE3			HSW_CACHEABILITY_CONTROL(0x2)
900260c420SBen Widawsky #define HSW_WB_LLC_AGE0			HSW_CACHEABILITY_CONTROL(0x3)
910260c420SBen Widawsky #define HSW_WB_ELLC_LLC_AGE3		HSW_CACHEABILITY_CONTROL(0x8)
920260c420SBen Widawsky #define HSW_WB_ELLC_LLC_AGE0		HSW_CACHEABILITY_CONTROL(0xb)
930260c420SBen Widawsky #define HSW_WT_ELLC_LLC_AGE3		HSW_CACHEABILITY_CONTROL(0x7)
940260c420SBen Widawsky #define HSW_WT_ELLC_LLC_AGE0		HSW_CACHEABILITY_CONTROL(0x6)
950260c420SBen Widawsky #define HSW_PTE_UNCACHED		(0)
960260c420SBen Widawsky #define HSW_GTT_ADDR_ENCODE(addr)	((addr) | (((addr) >> 28) & 0x7f0))
970260c420SBen Widawsky #define HSW_PTE_ADDR_ENCODE(addr)	HSW_GTT_ADDR_ENCODE(addr)
980260c420SBen Widawsky 
990260c420SBen Widawsky /* GEN8 legacy style address is defined as a 3 level page table:
1000260c420SBen Widawsky  * 31:30 | 29:21 | 20:12 |  11:0
1010260c420SBen Widawsky  * PDPE  |  PDE  |  PTE  | offset
1020260c420SBen Widawsky  * The difference as compared to normal x86 3 level page table is the PDPEs are
1030260c420SBen Widawsky  * programmed via register.
10481ba8aefSMichel Thierry  *
10581ba8aefSMichel Thierry  * GEN8 48b legacy style address is defined as a 4 level page table:
10681ba8aefSMichel Thierry  * 47:39 | 38:30 | 29:21 | 20:12 |  11:0
10781ba8aefSMichel Thierry  * PML4E | PDPE  |  PDE  |  PTE  | offset
1080260c420SBen Widawsky  */
10981ba8aefSMichel Thierry #define GEN8_PML4ES_PER_PML4		512
11081ba8aefSMichel Thierry #define GEN8_PML4E_SHIFT		39
111762d9936SMichel Thierry #define GEN8_PML4E_MASK			(GEN8_PML4ES_PER_PML4 - 1)
1120260c420SBen Widawsky #define GEN8_PDPE_SHIFT			30
11381ba8aefSMichel Thierry /* NB: GEN8_PDPE_MASK is untrue for 32b platforms, but it has no impact on 32b page
11481ba8aefSMichel Thierry  * tables */
11581ba8aefSMichel Thierry #define GEN8_PDPE_MASK			0x1ff
1160260c420SBen Widawsky #define GEN8_PDE_SHIFT			21
1170260c420SBen Widawsky #define GEN8_PDE_MASK			0x1ff
1180260c420SBen Widawsky #define GEN8_PTE_SHIFT			12
1190260c420SBen Widawsky #define GEN8_PTE_MASK			0x1ff
12076643600SBen Widawsky #define GEN8_LEGACY_PDPES		4
12107749ef3SMichel Thierry #define GEN8_PTES			I915_PTES(sizeof(gen8_pte_t))
1220260c420SBen Widawsky 
12381ba8aefSMichel Thierry #define I915_PDPES_PER_PDP(dev) (USES_FULL_48BIT_PPGTT(dev) ?\
12481ba8aefSMichel Thierry 				 GEN8_PML4ES_PER_PML4 : GEN8_LEGACY_PDPES)
1256ac18502SMichel Thierry 
1260260c420SBen Widawsky #define PPAT_UNCACHED_INDEX		(_PAGE_PWT | _PAGE_PCD)
1270260c420SBen Widawsky #define PPAT_CACHED_PDE_INDEX		0 /* WB LLC */
1280260c420SBen Widawsky #define PPAT_CACHED_INDEX		_PAGE_PAT /* WB LLCeLLC */
1290260c420SBen Widawsky #define PPAT_DISPLAY_ELLC_INDEX		_PAGE_PCD /* WT eLLC */
1300260c420SBen Widawsky 
131ee0ce478SVille Syrjälä #define CHV_PPAT_SNOOP			(1<<6)
1320260c420SBen Widawsky #define GEN8_PPAT_AGE(x)		(x<<4)
1330260c420SBen Widawsky #define GEN8_PPAT_LLCeLLC		(3<<2)
1340260c420SBen Widawsky #define GEN8_PPAT_LLCELLC		(2<<2)
1350260c420SBen Widawsky #define GEN8_PPAT_LLC			(1<<2)
1360260c420SBen Widawsky #define GEN8_PPAT_WB			(3<<0)
1370260c420SBen Widawsky #define GEN8_PPAT_WT			(2<<0)
1380260c420SBen Widawsky #define GEN8_PPAT_WC			(1<<0)
1390260c420SBen Widawsky #define GEN8_PPAT_UC			(0<<0)
1400260c420SBen Widawsky #define GEN8_PPAT_ELLC_OVERRIDE		(0<<2)
1410260c420SBen Widawsky #define GEN8_PPAT(i, x)			((uint64_t) (x) << ((i) * 8))
1420260c420SBen Widawsky 
143b42fe9caSJoonas Lahtinen struct sg_table;
144b42fe9caSJoonas Lahtinen 
145fe14d5f4STvrtko Ursulin enum i915_ggtt_view_type {
146fe14d5f4STvrtko Ursulin 	I915_GGTT_VIEW_NORMAL = 0,
1478bd7ef16SJoonas Lahtinen 	I915_GGTT_VIEW_ROTATED,
1488bd7ef16SJoonas Lahtinen 	I915_GGTT_VIEW_PARTIAL,
14950470bb0STvrtko Ursulin };
15050470bb0STvrtko Ursulin 
15150470bb0STvrtko Ursulin struct intel_rotation_info {
1521663b9d6SVille Syrjälä 	struct {
1531663b9d6SVille Syrjälä 		/* tiles */
1546687c906SVille Syrjälä 		unsigned int width, height, stride, offset;
1551663b9d6SVille Syrjälä 	} plane[2];
156fe14d5f4STvrtko Ursulin };
157fe14d5f4STvrtko Ursulin 
158fe14d5f4STvrtko Ursulin struct i915_ggtt_view {
159fe14d5f4STvrtko Ursulin 	enum i915_ggtt_view_type type;
160fe14d5f4STvrtko Ursulin 
1618bd7ef16SJoonas Lahtinen 	union {
1628bd7ef16SJoonas Lahtinen 		struct {
163088e0df4SMichel Thierry 			u64 offset;
1648bd7ef16SJoonas Lahtinen 			unsigned int size;
1658bd7ef16SJoonas Lahtinen 		} partial;
1667723f47dSVille Syrjälä 		struct intel_rotation_info rotated;
1678bd7ef16SJoonas Lahtinen 	} params;
168fe14d5f4STvrtko Ursulin };
169fe14d5f4STvrtko Ursulin 
170fe14d5f4STvrtko Ursulin extern const struct i915_ggtt_view i915_ggtt_view_normal;
1719abc4648SJoonas Lahtinen extern const struct i915_ggtt_view i915_ggtt_view_rotated;
172fe14d5f4STvrtko Ursulin 
1730260c420SBen Widawsky enum i915_cache_level;
174fe14d5f4STvrtko Ursulin 
175b42fe9caSJoonas Lahtinen struct i915_vma;
176bde13ebdSChris Wilson 
17744159ddbSMika Kuoppala struct i915_page_dma {
178d7b3de91SBen Widawsky 	struct page *page;
17944159ddbSMika Kuoppala 	union {
1807324cc04SBen Widawsky 		dma_addr_t daddr;
181678d96fbSBen Widawsky 
18244159ddbSMika Kuoppala 		/* For gen6/gen7 only. This is the offset in the GGTT
18344159ddbSMika Kuoppala 		 * where the page directory entries for PPGTT begin
18444159ddbSMika Kuoppala 		 */
18544159ddbSMika Kuoppala 		uint32_t ggtt_offset;
18644159ddbSMika Kuoppala 	};
18744159ddbSMika Kuoppala };
18844159ddbSMika Kuoppala 
189567047beSMika Kuoppala #define px_base(px) (&(px)->base)
190567047beSMika Kuoppala #define px_page(px) (px_base(px)->page)
191567047beSMika Kuoppala #define px_dma(px) (px_base(px)->daddr)
192567047beSMika Kuoppala 
19344159ddbSMika Kuoppala struct i915_page_table {
19444159ddbSMika Kuoppala 	struct i915_page_dma base;
19544159ddbSMika Kuoppala 
196678d96fbSBen Widawsky 	unsigned long *used_ptes;
197d7b3de91SBen Widawsky };
198d7b3de91SBen Widawsky 
199ec565b3cSMichel Thierry struct i915_page_directory {
20044159ddbSMika Kuoppala 	struct i915_page_dma base;
2017324cc04SBen Widawsky 
20233c8819fSMichel Thierry 	unsigned long *used_pdes;
203ec565b3cSMichel Thierry 	struct i915_page_table *page_table[I915_PDES]; /* PDEs */
204d7b3de91SBen Widawsky };
205d7b3de91SBen Widawsky 
206ec565b3cSMichel Thierry struct i915_page_directory_pointer {
2076ac18502SMichel Thierry 	struct i915_page_dma base;
2086ac18502SMichel Thierry 
2096ac18502SMichel Thierry 	unsigned long *used_pdpes;
2106ac18502SMichel Thierry 	struct i915_page_directory **page_directory;
211d7b3de91SBen Widawsky };
212d7b3de91SBen Widawsky 
21381ba8aefSMichel Thierry struct i915_pml4 {
21481ba8aefSMichel Thierry 	struct i915_page_dma base;
21581ba8aefSMichel Thierry 
21681ba8aefSMichel Thierry 	DECLARE_BITMAP(used_pml4es, GEN8_PML4ES_PER_PML4);
21781ba8aefSMichel Thierry 	struct i915_page_directory_pointer *pdps[GEN8_PML4ES_PER_PML4];
21881ba8aefSMichel Thierry };
21981ba8aefSMichel Thierry 
2200260c420SBen Widawsky struct i915_address_space {
2210260c420SBen Widawsky 	struct drm_mm mm;
22280b204bcSChris Wilson 	struct i915_gem_timeline timeline;
2230260c420SBen Widawsky 	struct drm_device *dev;
2242bfa996eSChris Wilson 	/* Every address space belongs to a struct file - except for the global
2252bfa996eSChris Wilson 	 * GTT that is owned by the driver (and so @file is set to NULL). In
2262bfa996eSChris Wilson 	 * principle, no information should leak from one context to another
2272bfa996eSChris Wilson 	 * (or between files/processes etc) unless explicitly shared by the
2282bfa996eSChris Wilson 	 * owner. Tracking the owner is important in order to free up per-file
2292bfa996eSChris Wilson 	 * objects along with the file, to aide resource tracking, and to
2302bfa996eSChris Wilson 	 * assign blame.
2312bfa996eSChris Wilson 	 */
2322bfa996eSChris Wilson 	struct drm_i915_file_private *file;
2330260c420SBen Widawsky 	struct list_head global_link;
234c44ef60eSMika Kuoppala 	u64 start;		/* Start offset always 0 for dri2 */
235c44ef60eSMika Kuoppala 	u64 total;		/* size addr space maps (ex. 2GB for ggtt) */
2360260c420SBen Widawsky 
23750e046b6SChris Wilson 	bool closed;
23850e046b6SChris Wilson 
2398bcdd0f7SChris Wilson 	struct i915_page_dma scratch_page;
24079ab9370SMika Kuoppala 	struct i915_page_table *scratch_pt;
24179ab9370SMika Kuoppala 	struct i915_page_directory *scratch_pd;
24269ab76fdSMichel Thierry 	struct i915_page_directory_pointer *scratch_pdp; /* GEN8+ & 48b PPGTT */
2430260c420SBen Widawsky 
2440260c420SBen Widawsky 	/**
2450260c420SBen Widawsky 	 * List of objects currently involved in rendering.
2460260c420SBen Widawsky 	 *
2470260c420SBen Widawsky 	 * Includes buffers having the contents of their GPU caches
24897b2a6a1SJohn Harrison 	 * flushed, not necessarily primitives. last_read_req
2490260c420SBen Widawsky 	 * represents when the rendering involved will be completed.
2500260c420SBen Widawsky 	 *
2510260c420SBen Widawsky 	 * A reference is held on the buffer while on this list.
2520260c420SBen Widawsky 	 */
2530260c420SBen Widawsky 	struct list_head active_list;
2540260c420SBen Widawsky 
2550260c420SBen Widawsky 	/**
2560260c420SBen Widawsky 	 * LRU list of objects which are not in the ringbuffer and
2570260c420SBen Widawsky 	 * are ready to unbind, but are still in the GTT.
2580260c420SBen Widawsky 	 *
25997b2a6a1SJohn Harrison 	 * last_read_req is NULL while an object is in this list.
2600260c420SBen Widawsky 	 *
2610260c420SBen Widawsky 	 * A reference is not held on the buffer while on this list,
2620260c420SBen Widawsky 	 * as merely being GTT-bound shouldn't prevent its being
2630260c420SBen Widawsky 	 * freed, and we'll pull it off the list in the free path.
2640260c420SBen Widawsky 	 */
2650260c420SBen Widawsky 	struct list_head inactive_list;
2660260c420SBen Widawsky 
26750e046b6SChris Wilson 	/**
26850e046b6SChris Wilson 	 * List of vma that have been unbound.
26950e046b6SChris Wilson 	 *
27050e046b6SChris Wilson 	 * A reference is not held on the buffer while on this list.
27150e046b6SChris Wilson 	 */
27250e046b6SChris Wilson 	struct list_head unbound_list;
27350e046b6SChris Wilson 
2740260c420SBen Widawsky 	/* FIXME: Need a more generic return type */
27507749ef3SMichel Thierry 	gen6_pte_t (*pte_encode)(dma_addr_t addr,
2760260c420SBen Widawsky 				 enum i915_cache_level level,
2774fb84d99SMichał Winiarski 				 u32 flags); /* Create a valid PTE */
278f329f5f6SDaniel Vetter 	/* flags for pte_encode */
279f329f5f6SDaniel Vetter #define PTE_READ_ONLY	(1<<0)
280678d96fbSBen Widawsky 	int (*allocate_va_range)(struct i915_address_space *vm,
281678d96fbSBen Widawsky 				 uint64_t start,
282678d96fbSBen Widawsky 				 uint64_t length);
2830260c420SBen Widawsky 	void (*clear_range)(struct i915_address_space *vm,
2840260c420SBen Widawsky 			    uint64_t start,
2854fb84d99SMichał Winiarski 			    uint64_t length);
286d6473f56SChris Wilson 	void (*insert_page)(struct i915_address_space *vm,
287d6473f56SChris Wilson 			    dma_addr_t addr,
288d6473f56SChris Wilson 			    uint64_t offset,
289d6473f56SChris Wilson 			    enum i915_cache_level cache_level,
290d6473f56SChris Wilson 			    u32 flags);
2910260c420SBen Widawsky 	void (*insert_entries)(struct i915_address_space *vm,
2920260c420SBen Widawsky 			       struct sg_table *st,
2930260c420SBen Widawsky 			       uint64_t start,
29424f3a8cfSAkash Goel 			       enum i915_cache_level cache_level, u32 flags);
2950260c420SBen Widawsky 	void (*cleanup)(struct i915_address_space *vm);
296777dc5bbSDaniel Vetter 	/** Unmap an object from an address space. This usually consists of
297777dc5bbSDaniel Vetter 	 * setting the valid PTE entries to a reserved scratch page. */
298777dc5bbSDaniel Vetter 	void (*unbind_vma)(struct i915_vma *vma);
299777dc5bbSDaniel Vetter 	/* Map an object into an address space with the given cache flags. */
30070b9f6f8SDaniel Vetter 	int (*bind_vma)(struct i915_vma *vma,
301777dc5bbSDaniel Vetter 			enum i915_cache_level cache_level,
302777dc5bbSDaniel Vetter 			u32 flags);
3030260c420SBen Widawsky };
3040260c420SBen Widawsky 
3052bfa996eSChris Wilson #define i915_is_ggtt(V) (!(V)->file)
306596c5923SChris Wilson 
3070260c420SBen Widawsky /* The Graphics Translation Table is the way in which GEN hardware translates a
3080260c420SBen Widawsky  * Graphics Virtual Address into a Physical Address. In addition to the normal
3090260c420SBen Widawsky  * collateral associated with any va->pa translations GEN hardware also has a
3100260c420SBen Widawsky  * portion of the GTT which can be mapped by the CPU and remain both coherent
3110260c420SBen Widawsky  * and correct (in cases like swizzling). That region is referred to as GMADR in
3120260c420SBen Widawsky  * the spec.
3130260c420SBen Widawsky  */
31462106b4fSJoonas Lahtinen struct i915_ggtt {
3150260c420SBen Widawsky 	struct i915_address_space base;
316f7bbe788SChris Wilson 	struct io_mapping mappable;	/* Mapping to our CPU mappable region */
3170260c420SBen Widawsky 
318c44ef60eSMika Kuoppala 	size_t stolen_size;		/* Total size of stolen memory */
319a9da512bSPaulo Zanoni 	size_t stolen_usable_size;	/* Total size minus BIOS reserved */
320274008e8SSagar Arun Kamble 	size_t stolen_reserved_base;
321274008e8SSagar Arun Kamble 	size_t stolen_reserved_size;
322c44ef60eSMika Kuoppala 	u64 mappable_end;		/* End offset that we can CPU map */
3230260c420SBen Widawsky 	phys_addr_t mappable_base;	/* PA of our GMADR */
3240260c420SBen Widawsky 
3250260c420SBen Widawsky 	/** "Graphics Stolen Memory" holds the global PTEs */
3260260c420SBen Widawsky 	void __iomem *gsm;
3270260c420SBen Widawsky 
3280260c420SBen Widawsky 	bool do_idle_maps;
3290260c420SBen Widawsky 
3300260c420SBen Widawsky 	int mtrr;
33195374d75SChris Wilson 
33295374d75SChris Wilson 	struct drm_mm_node error_capture;
3330260c420SBen Widawsky };
3340260c420SBen Widawsky 
3350260c420SBen Widawsky struct i915_hw_ppgtt {
3360260c420SBen Widawsky 	struct i915_address_space base;
3370260c420SBen Widawsky 	struct kref ref;
3380260c420SBen Widawsky 	struct drm_mm_node node;
339563222a7SBen Widawsky 	unsigned long pd_dirty_rings;
3400260c420SBen Widawsky 	union {
34181ba8aefSMichel Thierry 		struct i915_pml4 pml4;		/* GEN8+ & 48b PPGTT */
34281ba8aefSMichel Thierry 		struct i915_page_directory_pointer pdp;	/* GEN8+ */
34381ba8aefSMichel Thierry 		struct i915_page_directory pd;		/* GEN6-7 */
344d7b3de91SBen Widawsky 	};
3450260c420SBen Widawsky 
346678d96fbSBen Widawsky 	gen6_pte_t __iomem *pd_addr;
347678d96fbSBen Widawsky 
3480260c420SBen Widawsky 	int (*enable)(struct i915_hw_ppgtt *ppgtt);
3490260c420SBen Widawsky 	int (*switch_mm)(struct i915_hw_ppgtt *ppgtt,
350e85b26dcSJohn Harrison 			 struct drm_i915_gem_request *req);
3510260c420SBen Widawsky 	void (*debug_dump)(struct i915_hw_ppgtt *ppgtt, struct seq_file *m);
3520260c420SBen Widawsky };
3530260c420SBen Widawsky 
354731f74c5SDave Gordon /*
355731f74c5SDave Gordon  * gen6_for_each_pde() iterates over every pde from start until start+length.
356731f74c5SDave Gordon  * If start and start+length are not perfectly divisible, the macro will round
357731f74c5SDave Gordon  * down and up as needed. Start=0 and length=2G effectively iterates over
358731f74c5SDave Gordon  * every PDE in the system. The macro modifies ALL its parameters except 'pd',
359731f74c5SDave Gordon  * so each of the other parameters should preferably be a simple variable, or
360731f74c5SDave Gordon  * at most an lvalue with no side-effects!
361678d96fbSBen Widawsky  */
362731f74c5SDave Gordon #define gen6_for_each_pde(pt, pd, start, length, iter)			\
363fdc454c1SMichel Thierry 	for (iter = gen6_pde_index(start);				\
364731f74c5SDave Gordon 	     length > 0 && iter < I915_PDES &&				\
365731f74c5SDave Gordon 		(pt = (pd)->page_table[iter], true);			\
366731f74c5SDave Gordon 	     ({ u32 temp = ALIGN(start+1, 1 << GEN6_PDE_SHIFT);		\
367731f74c5SDave Gordon 		    temp = min(temp - start, length);			\
368731f74c5SDave Gordon 		    start += temp, length -= temp; }), ++iter)
369678d96fbSBen Widawsky 
370731f74c5SDave Gordon #define gen6_for_all_pdes(pt, pd, iter)					\
37109942c65SMichel Thierry 	for (iter = 0;							\
372731f74c5SDave Gordon 	     iter < I915_PDES &&					\
373731f74c5SDave Gordon 		(pt = (pd)->page_table[iter], true);			\
374731f74c5SDave Gordon 	     ++iter)
37509942c65SMichel Thierry 
376678d96fbSBen Widawsky static inline uint32_t i915_pte_index(uint64_t address, uint32_t pde_shift)
377678d96fbSBen Widawsky {
378678d96fbSBen Widawsky 	const uint32_t mask = NUM_PTE(pde_shift) - 1;
379678d96fbSBen Widawsky 
380678d96fbSBen Widawsky 	return (address >> PAGE_SHIFT) & mask;
381678d96fbSBen Widawsky }
382678d96fbSBen Widawsky 
383678d96fbSBen Widawsky /* Helper to counts the number of PTEs within the given length. This count
384678d96fbSBen Widawsky  * does not cross a page table boundary, so the max value would be
385678d96fbSBen Widawsky  * GEN6_PTES for GEN6, and GEN8_PTES for GEN8.
386678d96fbSBen Widawsky */
387678d96fbSBen Widawsky static inline uint32_t i915_pte_count(uint64_t addr, size_t length,
388678d96fbSBen Widawsky 				      uint32_t pde_shift)
389678d96fbSBen Widawsky {
39069603dbbSAlan 	const uint64_t mask = ~((1ULL << pde_shift) - 1);
391678d96fbSBen Widawsky 	uint64_t end;
392678d96fbSBen Widawsky 
393678d96fbSBen Widawsky 	WARN_ON(length == 0);
394678d96fbSBen Widawsky 	WARN_ON(offset_in_page(addr|length));
395678d96fbSBen Widawsky 
396678d96fbSBen Widawsky 	end = addr + length;
397678d96fbSBen Widawsky 
398678d96fbSBen Widawsky 	if ((addr & mask) != (end & mask))
399678d96fbSBen Widawsky 		return NUM_PTE(pde_shift) - i915_pte_index(addr, pde_shift);
400678d96fbSBen Widawsky 
401678d96fbSBen Widawsky 	return i915_pte_index(end, pde_shift) - i915_pte_index(addr, pde_shift);
402678d96fbSBen Widawsky }
403678d96fbSBen Widawsky 
404678d96fbSBen Widawsky static inline uint32_t i915_pde_index(uint64_t addr, uint32_t shift)
405678d96fbSBen Widawsky {
406678d96fbSBen Widawsky 	return (addr >> shift) & I915_PDE_MASK;
407678d96fbSBen Widawsky }
408678d96fbSBen Widawsky 
409678d96fbSBen Widawsky static inline uint32_t gen6_pte_index(uint32_t addr)
410678d96fbSBen Widawsky {
411678d96fbSBen Widawsky 	return i915_pte_index(addr, GEN6_PDE_SHIFT);
412678d96fbSBen Widawsky }
413678d96fbSBen Widawsky 
414678d96fbSBen Widawsky static inline size_t gen6_pte_count(uint32_t addr, uint32_t length)
415678d96fbSBen Widawsky {
416678d96fbSBen Widawsky 	return i915_pte_count(addr, length, GEN6_PDE_SHIFT);
417678d96fbSBen Widawsky }
418678d96fbSBen Widawsky 
419678d96fbSBen Widawsky static inline uint32_t gen6_pde_index(uint32_t addr)
420678d96fbSBen Widawsky {
421678d96fbSBen Widawsky 	return i915_pde_index(addr, GEN6_PDE_SHIFT);
422678d96fbSBen Widawsky }
423678d96fbSBen Widawsky 
4249271d959SMichel Thierry /* Equivalent to the gen6 version, For each pde iterates over every pde
4259271d959SMichel Thierry  * between from start until start + length. On gen8+ it simply iterates
4269271d959SMichel Thierry  * over every page directory entry in a page directory.
4279271d959SMichel Thierry  */
428e8ebd8e2SDave Gordon #define gen8_for_each_pde(pt, pd, start, length, iter)			\
4299271d959SMichel Thierry 	for (iter = gen8_pde_index(start);				\
430e8ebd8e2SDave Gordon 	     length > 0 && iter < I915_PDES &&				\
431e8ebd8e2SDave Gordon 		(pt = (pd)->page_table[iter], true);			\
432e8ebd8e2SDave Gordon 	     ({ u64 temp = ALIGN(start+1, 1 << GEN8_PDE_SHIFT);		\
433e8ebd8e2SDave Gordon 		    temp = min(temp - start, length);			\
434e8ebd8e2SDave Gordon 		    start += temp, length -= temp; }), ++iter)
4359271d959SMichel Thierry 
436e8ebd8e2SDave Gordon #define gen8_for_each_pdpe(pd, pdp, start, length, iter)		\
4379271d959SMichel Thierry 	for (iter = gen8_pdpe_index(start);				\
438e8ebd8e2SDave Gordon 	     length > 0 && iter < I915_PDPES_PER_PDP(dev) &&		\
439e8ebd8e2SDave Gordon 		(pd = (pdp)->page_directory[iter], true);		\
440e8ebd8e2SDave Gordon 	     ({ u64 temp = ALIGN(start+1, 1 << GEN8_PDPE_SHIFT);	\
441e8ebd8e2SDave Gordon 		    temp = min(temp - start, length);			\
442e8ebd8e2SDave Gordon 		    start += temp, length -= temp; }), ++iter)
4439271d959SMichel Thierry 
444e8ebd8e2SDave Gordon #define gen8_for_each_pml4e(pdp, pml4, start, length, iter)		\
445762d9936SMichel Thierry 	for (iter = gen8_pml4e_index(start);				\
446e8ebd8e2SDave Gordon 	     length > 0 && iter < GEN8_PML4ES_PER_PML4 &&		\
447e8ebd8e2SDave Gordon 		(pdp = (pml4)->pdps[iter], true);			\
448e8ebd8e2SDave Gordon 	     ({ u64 temp = ALIGN(start+1, 1ULL << GEN8_PML4E_SHIFT);	\
449e8ebd8e2SDave Gordon 		    temp = min(temp - start, length);			\
450e8ebd8e2SDave Gordon 		    start += temp, length -= temp; }), ++iter)
451762d9936SMichel Thierry 
4529271d959SMichel Thierry static inline uint32_t gen8_pte_index(uint64_t address)
4539271d959SMichel Thierry {
4549271d959SMichel Thierry 	return i915_pte_index(address, GEN8_PDE_SHIFT);
4559271d959SMichel Thierry }
4569271d959SMichel Thierry 
4579271d959SMichel Thierry static inline uint32_t gen8_pde_index(uint64_t address)
4589271d959SMichel Thierry {
4599271d959SMichel Thierry 	return i915_pde_index(address, GEN8_PDE_SHIFT);
4609271d959SMichel Thierry }
4619271d959SMichel Thierry 
4629271d959SMichel Thierry static inline uint32_t gen8_pdpe_index(uint64_t address)
4639271d959SMichel Thierry {
4649271d959SMichel Thierry 	return (address >> GEN8_PDPE_SHIFT) & GEN8_PDPE_MASK;
4659271d959SMichel Thierry }
4669271d959SMichel Thierry 
4679271d959SMichel Thierry static inline uint32_t gen8_pml4e_index(uint64_t address)
4689271d959SMichel Thierry {
469762d9936SMichel Thierry 	return (address >> GEN8_PML4E_SHIFT) & GEN8_PML4E_MASK;
4709271d959SMichel Thierry }
4719271d959SMichel Thierry 
47233c8819fSMichel Thierry static inline size_t gen8_pte_count(uint64_t address, uint64_t length)
47333c8819fSMichel Thierry {
47433c8819fSMichel Thierry 	return i915_pte_count(address, length, GEN8_PDE_SHIFT);
47533c8819fSMichel Thierry }
47633c8819fSMichel Thierry 
477d852c7bfSMika Kuoppala static inline dma_addr_t
478d852c7bfSMika Kuoppala i915_page_dir_dma_addr(const struct i915_hw_ppgtt *ppgtt, const unsigned n)
479d852c7bfSMika Kuoppala {
480d852c7bfSMika Kuoppala 	return test_bit(n, ppgtt->pdp.used_pdpes) ?
481567047beSMika Kuoppala 		px_dma(ppgtt->pdp.page_directory[n]) :
48279ab9370SMika Kuoppala 		px_dma(ppgtt->base.scratch_pd);
483d852c7bfSMika Kuoppala }
484d852c7bfSMika Kuoppala 
485b42fe9caSJoonas Lahtinen static inline struct i915_ggtt *
486b42fe9caSJoonas Lahtinen i915_vm_to_ggtt(struct i915_address_space *vm)
487b42fe9caSJoonas Lahtinen {
488b42fe9caSJoonas Lahtinen 	GEM_BUG_ON(!i915_is_ggtt(vm));
489b42fe9caSJoonas Lahtinen 	return container_of(vm, struct i915_ggtt, base);
490b42fe9caSJoonas Lahtinen }
491b42fe9caSJoonas Lahtinen 
49297d6d7abSChris Wilson int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv);
49397d6d7abSChris Wilson int i915_ggtt_init_hw(struct drm_i915_private *dev_priv);
49497d6d7abSChris Wilson int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv);
495f6b9d5caSChris Wilson int i915_gem_init_ggtt(struct drm_i915_private *dev_priv);
49697d6d7abSChris Wilson void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv);
497ee960be7SDaniel Vetter 
49882460d97SDaniel Vetter int i915_ppgtt_init_hw(struct drm_device *dev);
499ee960be7SDaniel Vetter void i915_ppgtt_release(struct kref *kref);
5002bfa996eSChris Wilson struct i915_hw_ppgtt *i915_ppgtt_create(struct drm_i915_private *dev_priv,
50180b204bcSChris Wilson 					struct drm_i915_file_private *fpriv,
50280b204bcSChris Wilson 					const char *name);
503ee960be7SDaniel Vetter static inline void i915_ppgtt_get(struct i915_hw_ppgtt *ppgtt)
504ee960be7SDaniel Vetter {
505ee960be7SDaniel Vetter 	if (ppgtt)
506ee960be7SDaniel Vetter 		kref_get(&ppgtt->ref);
507ee960be7SDaniel Vetter }
508ee960be7SDaniel Vetter static inline void i915_ppgtt_put(struct i915_hw_ppgtt *ppgtt)
509ee960be7SDaniel Vetter {
510ee960be7SDaniel Vetter 	if (ppgtt)
511ee960be7SDaniel Vetter 		kref_put(&ppgtt->ref, i915_ppgtt_release);
512ee960be7SDaniel Vetter }
5130260c420SBen Widawsky 
514dc97997aSChris Wilson void i915_check_and_clear_faults(struct drm_i915_private *dev_priv);
5150260c420SBen Widawsky void i915_gem_suspend_gtt_mappings(struct drm_device *dev);
5160260c420SBen Widawsky void i915_gem_restore_gtt_mappings(struct drm_device *dev);
5170260c420SBen Widawsky 
51803ac84f1SChris Wilson int __must_check i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
51903ac84f1SChris Wilson 					    struct sg_table *pages);
52003ac84f1SChris Wilson void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
52103ac84f1SChris Wilson 			       struct sg_table *pages);
5220260c420SBen Widawsky 
52359bfa124SChris Wilson /* Flags used by pin/bind&friends. */
524305bc234SChris Wilson #define PIN_NONBLOCK		BIT(0)
525305bc234SChris Wilson #define PIN_MAPPABLE		BIT(1)
526305bc234SChris Wilson #define PIN_ZONE_4G		BIT(2)
52782118877SChris Wilson #define PIN_NONFAULT		BIT(3)
528305bc234SChris Wilson 
529305bc234SChris Wilson #define PIN_MBZ			BIT(5) /* I915_VMA_PIN_OVERFLOW */
530305bc234SChris Wilson #define PIN_GLOBAL		BIT(6) /* I915_VMA_GLOBAL_BIND */
531305bc234SChris Wilson #define PIN_USER		BIT(7) /* I915_VMA_LOCAL_BIND */
532305bc234SChris Wilson #define PIN_UPDATE		BIT(8)
533305bc234SChris Wilson 
534305bc234SChris Wilson #define PIN_HIGH		BIT(9)
535305bc234SChris Wilson #define PIN_OFFSET_BIAS		BIT(10)
536305bc234SChris Wilson #define PIN_OFFSET_FIXED	BIT(11)
53759bfa124SChris Wilson #define PIN_OFFSET_MASK		(~4095)
53859bfa124SChris Wilson 
5390260c420SBen Widawsky #endif
540