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 
374d884705SDaniel Vetter struct drm_i915_file_private;
384d884705SDaniel Vetter 
3907749ef3SMichel Thierry typedef uint32_t gen6_pte_t;
4007749ef3SMichel Thierry typedef uint64_t gen8_pte_t;
4107749ef3SMichel Thierry typedef uint64_t gen8_pde_t;
420260c420SBen Widawsky 
430260c420SBen Widawsky #define gtt_total_entries(gtt) ((gtt).base.total >> PAGE_SHIFT)
440260c420SBen Widawsky 
4507749ef3SMichel Thierry 
460260c420SBen Widawsky /* gen6-hsw has bit 11-4 for physical addr bit 39-32 */
470260c420SBen Widawsky #define GEN6_GTT_ADDR_ENCODE(addr)	((addr) | (((addr) >> 28) & 0xff0))
480260c420SBen Widawsky #define GEN6_PTE_ADDR_ENCODE(addr)	GEN6_GTT_ADDR_ENCODE(addr)
490260c420SBen Widawsky #define GEN6_PDE_ADDR_ENCODE(addr)	GEN6_GTT_ADDR_ENCODE(addr)
500260c420SBen Widawsky #define GEN6_PTE_CACHE_LLC		(2 << 1)
510260c420SBen Widawsky #define GEN6_PTE_UNCACHED		(1 << 1)
520260c420SBen Widawsky #define GEN6_PTE_VALID			(1 << 0)
530260c420SBen Widawsky 
5407749ef3SMichel Thierry #define I915_PTES(pte_len)		(PAGE_SIZE / (pte_len))
5507749ef3SMichel Thierry #define I915_PTE_MASK(pte_len)		(I915_PTES(pte_len) - 1)
5607749ef3SMichel Thierry #define I915_PDES			512
5707749ef3SMichel Thierry #define I915_PDE_MASK			(I915_PDES - 1)
58678d96fbSBen Widawsky #define NUM_PTE(pde_shift)     (1 << (pde_shift - PAGE_SHIFT))
5907749ef3SMichel Thierry 
6007749ef3SMichel Thierry #define GEN6_PTES			I915_PTES(sizeof(gen6_pte_t))
6107749ef3SMichel Thierry #define GEN6_PD_SIZE		        (I915_PDES * PAGE_SIZE)
620260c420SBen Widawsky #define GEN6_PD_ALIGN			(PAGE_SIZE * 16)
63678d96fbSBen Widawsky #define GEN6_PDE_SHIFT			22
640260c420SBen Widawsky #define GEN6_PDE_VALID			(1 << 0)
650260c420SBen Widawsky 
660260c420SBen Widawsky #define GEN7_PTE_CACHE_L3_LLC		(3 << 1)
670260c420SBen Widawsky 
680260c420SBen Widawsky #define BYT_PTE_SNOOPED_BY_CPU_CACHES	(1 << 2)
690260c420SBen Widawsky #define BYT_PTE_WRITEABLE		(1 << 1)
700260c420SBen Widawsky 
710260c420SBen Widawsky /* Cacheability Control is a 4-bit value. The low three bits are stored in bits
720260c420SBen Widawsky  * 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE.
730260c420SBen Widawsky  */
740260c420SBen Widawsky #define HSW_CACHEABILITY_CONTROL(bits)	((((bits) & 0x7) << 1) | \
750260c420SBen Widawsky 					 (((bits) & 0x8) << (11 - 3)))
760260c420SBen Widawsky #define HSW_WB_LLC_AGE3			HSW_CACHEABILITY_CONTROL(0x2)
770260c420SBen Widawsky #define HSW_WB_LLC_AGE0			HSW_CACHEABILITY_CONTROL(0x3)
780260c420SBen Widawsky #define HSW_WB_ELLC_LLC_AGE3		HSW_CACHEABILITY_CONTROL(0x8)
790260c420SBen Widawsky #define HSW_WB_ELLC_LLC_AGE0		HSW_CACHEABILITY_CONTROL(0xb)
800260c420SBen Widawsky #define HSW_WT_ELLC_LLC_AGE3		HSW_CACHEABILITY_CONTROL(0x7)
810260c420SBen Widawsky #define HSW_WT_ELLC_LLC_AGE0		HSW_CACHEABILITY_CONTROL(0x6)
820260c420SBen Widawsky #define HSW_PTE_UNCACHED		(0)
830260c420SBen Widawsky #define HSW_GTT_ADDR_ENCODE(addr)	((addr) | (((addr) >> 28) & 0x7f0))
840260c420SBen Widawsky #define HSW_PTE_ADDR_ENCODE(addr)	HSW_GTT_ADDR_ENCODE(addr)
850260c420SBen Widawsky 
860260c420SBen Widawsky /* GEN8 legacy style address is defined as a 3 level page table:
870260c420SBen Widawsky  * 31:30 | 29:21 | 20:12 |  11:0
880260c420SBen Widawsky  * PDPE  |  PDE  |  PTE  | offset
890260c420SBen Widawsky  * The difference as compared to normal x86 3 level page table is the PDPEs are
900260c420SBen Widawsky  * programmed via register.
910260c420SBen Widawsky  */
920260c420SBen Widawsky #define GEN8_PDPE_SHIFT			30
930260c420SBen Widawsky #define GEN8_PDPE_MASK			0x3
940260c420SBen Widawsky #define GEN8_PDE_SHIFT			21
950260c420SBen Widawsky #define GEN8_PDE_MASK			0x1ff
960260c420SBen Widawsky #define GEN8_PTE_SHIFT			12
970260c420SBen Widawsky #define GEN8_PTE_MASK			0x1ff
9876643600SBen Widawsky #define GEN8_LEGACY_PDPES		4
9907749ef3SMichel Thierry #define GEN8_PTES			I915_PTES(sizeof(gen8_pte_t))
1000260c420SBen Widawsky 
1010260c420SBen Widawsky #define PPAT_UNCACHED_INDEX		(_PAGE_PWT | _PAGE_PCD)
1020260c420SBen Widawsky #define PPAT_CACHED_PDE_INDEX		0 /* WB LLC */
1030260c420SBen Widawsky #define PPAT_CACHED_INDEX		_PAGE_PAT /* WB LLCeLLC */
1040260c420SBen Widawsky #define PPAT_DISPLAY_ELLC_INDEX		_PAGE_PCD /* WT eLLC */
1050260c420SBen Widawsky 
106ee0ce478SVille Syrjälä #define CHV_PPAT_SNOOP			(1<<6)
1070260c420SBen Widawsky #define GEN8_PPAT_AGE(x)		(x<<4)
1080260c420SBen Widawsky #define GEN8_PPAT_LLCeLLC		(3<<2)
1090260c420SBen Widawsky #define GEN8_PPAT_LLCELLC		(2<<2)
1100260c420SBen Widawsky #define GEN8_PPAT_LLC			(1<<2)
1110260c420SBen Widawsky #define GEN8_PPAT_WB			(3<<0)
1120260c420SBen Widawsky #define GEN8_PPAT_WT			(2<<0)
1130260c420SBen Widawsky #define GEN8_PPAT_WC			(1<<0)
1140260c420SBen Widawsky #define GEN8_PPAT_UC			(0<<0)
1150260c420SBen Widawsky #define GEN8_PPAT_ELLC_OVERRIDE		(0<<2)
1160260c420SBen Widawsky #define GEN8_PPAT(i, x)			((uint64_t) (x) << ((i) * 8))
1170260c420SBen Widawsky 
118fe14d5f4STvrtko Ursulin enum i915_ggtt_view_type {
119fe14d5f4STvrtko Ursulin 	I915_GGTT_VIEW_NORMAL = 0,
12050470bb0STvrtko Ursulin 	I915_GGTT_VIEW_ROTATED
12150470bb0STvrtko Ursulin };
12250470bb0STvrtko Ursulin 
12350470bb0STvrtko Ursulin struct intel_rotation_info {
12450470bb0STvrtko Ursulin 	unsigned int height;
12550470bb0STvrtko Ursulin 	unsigned int pitch;
12650470bb0STvrtko Ursulin 	uint32_t pixel_format;
12750470bb0STvrtko Ursulin 	uint64_t fb_modifier;
128fe14d5f4STvrtko Ursulin };
129fe14d5f4STvrtko Ursulin 
130fe14d5f4STvrtko Ursulin struct i915_ggtt_view {
131fe14d5f4STvrtko Ursulin 	enum i915_ggtt_view_type type;
132fe14d5f4STvrtko Ursulin 
133fe14d5f4STvrtko Ursulin 	struct sg_table *pages;
13450470bb0STvrtko Ursulin 
13550470bb0STvrtko Ursulin 	union {
13650470bb0STvrtko Ursulin 		struct intel_rotation_info rotation_info;
13750470bb0STvrtko Ursulin 	};
138fe14d5f4STvrtko Ursulin };
139fe14d5f4STvrtko Ursulin 
140fe14d5f4STvrtko Ursulin extern const struct i915_ggtt_view i915_ggtt_view_normal;
1419abc4648SJoonas Lahtinen extern const struct i915_ggtt_view i915_ggtt_view_rotated;
142fe14d5f4STvrtko Ursulin 
1430260c420SBen Widawsky enum i915_cache_level;
144fe14d5f4STvrtko Ursulin 
1450260c420SBen Widawsky /**
1460260c420SBen Widawsky  * A VMA represents a GEM BO that is bound into an address space. Therefore, a
1470260c420SBen Widawsky  * VMA's presence cannot be guaranteed before binding, or after unbinding the
1480260c420SBen Widawsky  * object into/from the address space.
1490260c420SBen Widawsky  *
1500260c420SBen Widawsky  * To make things as simple as possible (ie. no refcounting), a VMA's lifetime
1510260c420SBen Widawsky  * will always be <= an objects lifetime. So object refcounting should cover us.
1520260c420SBen Widawsky  */
1530260c420SBen Widawsky struct i915_vma {
1540260c420SBen Widawsky 	struct drm_mm_node node;
1550260c420SBen Widawsky 	struct drm_i915_gem_object *obj;
1560260c420SBen Widawsky 	struct i915_address_space *vm;
1570260c420SBen Widawsky 
158aff43766STvrtko Ursulin 	/** Flags and address space this VMA is bound to */
159aff43766STvrtko Ursulin #define GLOBAL_BIND	(1<<0)
160aff43766STvrtko Ursulin #define LOCAL_BIND	(1<<1)
161aff43766STvrtko Ursulin #define PTE_READ_ONLY	(1<<2)
162aff43766STvrtko Ursulin 	unsigned int bound : 4;
163aff43766STvrtko Ursulin 
164fe14d5f4STvrtko Ursulin 	/**
165fe14d5f4STvrtko Ursulin 	 * Support different GGTT views into the same object.
166fe14d5f4STvrtko Ursulin 	 * This means there can be multiple VMA mappings per object and per VM.
167fe14d5f4STvrtko Ursulin 	 * i915_ggtt_view_type is used to distinguish between those entries.
168fe14d5f4STvrtko Ursulin 	 * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
169fe14d5f4STvrtko Ursulin 	 * assumed in GEM functions which take no ggtt view parameter.
170fe14d5f4STvrtko Ursulin 	 */
171fe14d5f4STvrtko Ursulin 	struct i915_ggtt_view ggtt_view;
172fe14d5f4STvrtko Ursulin 
1730260c420SBen Widawsky 	/** This object's place on the active/inactive lists */
1740260c420SBen Widawsky 	struct list_head mm_list;
1750260c420SBen Widawsky 
1760260c420SBen Widawsky 	struct list_head vma_link; /* Link in the object's VMA list */
1770260c420SBen Widawsky 
1780260c420SBen Widawsky 	/** This vma's place in the batchbuffer or on the eviction list */
1790260c420SBen Widawsky 	struct list_head exec_list;
1800260c420SBen Widawsky 
1810260c420SBen Widawsky 	/**
1820260c420SBen Widawsky 	 * Used for performing relocations during execbuffer insertion.
1830260c420SBen Widawsky 	 */
1840260c420SBen Widawsky 	struct hlist_node exec_node;
1850260c420SBen Widawsky 	unsigned long exec_handle;
1860260c420SBen Widawsky 	struct drm_i915_gem_exec_object2 *exec_entry;
1870260c420SBen Widawsky 
1880260c420SBen Widawsky 	/**
1890260c420SBen Widawsky 	 * How many users have pinned this object in GTT space. The following
1904feb7659SDaniel Vetter 	 * users can each hold at most one reference: pwrite/pread, execbuffer
1914feb7659SDaniel Vetter 	 * (objects are not allowed multiple times for the same batchbuffer),
1924feb7659SDaniel Vetter 	 * and the framebuffer code. When switching/pageflipping, the
1934feb7659SDaniel Vetter 	 * framebuffer code has at most two buffers pinned per crtc.
1940260c420SBen Widawsky 	 *
1950260c420SBen Widawsky 	 * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3
1960260c420SBen Widawsky 	 * bits with absolutely no headroom. So use 4 bits. */
1970260c420SBen Widawsky 	unsigned int pin_count:4;
1980260c420SBen Widawsky #define DRM_I915_GEM_OBJECT_MAX_PIN_COUNT 0xf
1990260c420SBen Widawsky 
2000260c420SBen Widawsky 	/** Unmap an object from an address space. This usually consists of
2010260c420SBen Widawsky 	 * setting the valid PTE entries to a reserved scratch page. */
2020260c420SBen Widawsky 	void (*unbind_vma)(struct i915_vma *vma);
2030260c420SBen Widawsky 	/* Map an object into an address space with the given cache flags. */
2040260c420SBen Widawsky 	void (*bind_vma)(struct i915_vma *vma,
2050260c420SBen Widawsky 			 enum i915_cache_level cache_level,
2060260c420SBen Widawsky 			 u32 flags);
2070260c420SBen Widawsky };
2080260c420SBen Widawsky 
209ec565b3cSMichel Thierry struct i915_page_table {
210d7b3de91SBen Widawsky 	struct page *page;
2117324cc04SBen Widawsky 	dma_addr_t daddr;
212678d96fbSBen Widawsky 
213678d96fbSBen Widawsky 	unsigned long *used_ptes;
214d7b3de91SBen Widawsky };
215d7b3de91SBen Widawsky 
216ec565b3cSMichel Thierry struct i915_page_directory {
217d7b3de91SBen Widawsky 	struct page *page; /* NULL for GEN6-GEN7 */
2187324cc04SBen Widawsky 	union {
2197324cc04SBen Widawsky 		uint32_t pd_offset;
2207324cc04SBen Widawsky 		dma_addr_t daddr;
2217324cc04SBen Widawsky 	};
2227324cc04SBen Widawsky 
223ec565b3cSMichel Thierry 	struct i915_page_table *page_table[I915_PDES]; /* PDEs */
224d7b3de91SBen Widawsky };
225d7b3de91SBen Widawsky 
226ec565b3cSMichel Thierry struct i915_page_directory_pointer {
227d7b3de91SBen Widawsky 	/* struct page *page; */
228ec565b3cSMichel Thierry 	struct i915_page_directory *page_directory[GEN8_LEGACY_PDPES];
229d7b3de91SBen Widawsky };
230d7b3de91SBen Widawsky 
2310260c420SBen Widawsky struct i915_address_space {
2320260c420SBen Widawsky 	struct drm_mm mm;
2330260c420SBen Widawsky 	struct drm_device *dev;
2340260c420SBen Widawsky 	struct list_head global_link;
2350260c420SBen Widawsky 	unsigned long start;		/* Start offset always 0 for dri2 */
2360260c420SBen Widawsky 	size_t total;		/* size addr space maps (ex. 2GB for ggtt) */
2370260c420SBen Widawsky 
2380260c420SBen Widawsky 	struct {
2390260c420SBen Widawsky 		dma_addr_t addr;
2400260c420SBen Widawsky 		struct page *page;
2410260c420SBen Widawsky 	} scratch;
2420260c420SBen Widawsky 
2430260c420SBen Widawsky 	/**
2440260c420SBen Widawsky 	 * List of objects currently involved in rendering.
2450260c420SBen Widawsky 	 *
2460260c420SBen Widawsky 	 * Includes buffers having the contents of their GPU caches
24797b2a6a1SJohn Harrison 	 * flushed, not necessarily primitives. last_read_req
2480260c420SBen Widawsky 	 * represents when the rendering involved will be completed.
2490260c420SBen Widawsky 	 *
2500260c420SBen Widawsky 	 * A reference is held on the buffer while on this list.
2510260c420SBen Widawsky 	 */
2520260c420SBen Widawsky 	struct list_head active_list;
2530260c420SBen Widawsky 
2540260c420SBen Widawsky 	/**
2550260c420SBen Widawsky 	 * LRU list of objects which are not in the ringbuffer and
2560260c420SBen Widawsky 	 * are ready to unbind, but are still in the GTT.
2570260c420SBen Widawsky 	 *
25897b2a6a1SJohn Harrison 	 * last_read_req is NULL while an object is in this list.
2590260c420SBen Widawsky 	 *
2600260c420SBen Widawsky 	 * A reference is not held on the buffer while on this list,
2610260c420SBen Widawsky 	 * as merely being GTT-bound shouldn't prevent its being
2620260c420SBen Widawsky 	 * freed, and we'll pull it off the list in the free path.
2630260c420SBen Widawsky 	 */
2640260c420SBen Widawsky 	struct list_head inactive_list;
2650260c420SBen Widawsky 
2660260c420SBen Widawsky 	/* FIXME: Need a more generic return type */
26707749ef3SMichel Thierry 	gen6_pte_t (*pte_encode)(dma_addr_t addr,
2680260c420SBen Widawsky 				 enum i915_cache_level level,
26924f3a8cfSAkash Goel 				 bool valid, u32 flags); /* Create a valid PTE */
270678d96fbSBen Widawsky 	int (*allocate_va_range)(struct i915_address_space *vm,
271678d96fbSBen Widawsky 				 uint64_t start,
272678d96fbSBen Widawsky 				 uint64_t length);
2730260c420SBen Widawsky 	void (*clear_range)(struct i915_address_space *vm,
2740260c420SBen Widawsky 			    uint64_t start,
2750260c420SBen Widawsky 			    uint64_t length,
2760260c420SBen Widawsky 			    bool use_scratch);
2770260c420SBen Widawsky 	void (*insert_entries)(struct i915_address_space *vm,
2780260c420SBen Widawsky 			       struct sg_table *st,
2790260c420SBen Widawsky 			       uint64_t start,
28024f3a8cfSAkash Goel 			       enum i915_cache_level cache_level, u32 flags);
2810260c420SBen Widawsky 	void (*cleanup)(struct i915_address_space *vm);
2820260c420SBen Widawsky };
2830260c420SBen Widawsky 
2840260c420SBen Widawsky /* The Graphics Translation Table is the way in which GEN hardware translates a
2850260c420SBen Widawsky  * Graphics Virtual Address into a Physical Address. In addition to the normal
2860260c420SBen Widawsky  * collateral associated with any va->pa translations GEN hardware also has a
2870260c420SBen Widawsky  * portion of the GTT which can be mapped by the CPU and remain both coherent
2880260c420SBen Widawsky  * and correct (in cases like swizzling). That region is referred to as GMADR in
2890260c420SBen Widawsky  * the spec.
2900260c420SBen Widawsky  */
2910260c420SBen Widawsky struct i915_gtt {
2920260c420SBen Widawsky 	struct i915_address_space base;
2930260c420SBen Widawsky 	size_t stolen_size;		/* Total size of stolen memory */
2940260c420SBen Widawsky 
2950260c420SBen Widawsky 	unsigned long mappable_end;	/* End offset that we can CPU map */
2960260c420SBen Widawsky 	struct io_mapping *mappable;	/* Mapping to our CPU mappable region */
2970260c420SBen Widawsky 	phys_addr_t mappable_base;	/* PA of our GMADR */
2980260c420SBen Widawsky 
2990260c420SBen Widawsky 	/** "Graphics Stolen Memory" holds the global PTEs */
3000260c420SBen Widawsky 	void __iomem *gsm;
3010260c420SBen Widawsky 
3020260c420SBen Widawsky 	bool do_idle_maps;
3030260c420SBen Widawsky 
3040260c420SBen Widawsky 	int mtrr;
3050260c420SBen Widawsky 
3060260c420SBen Widawsky 	/* global gtt ops */
3070260c420SBen Widawsky 	int (*gtt_probe)(struct drm_device *dev, size_t *gtt_total,
3080260c420SBen Widawsky 			  size_t *stolen, phys_addr_t *mappable_base,
3090260c420SBen Widawsky 			  unsigned long *mappable_end);
3100260c420SBen Widawsky };
3110260c420SBen Widawsky 
3120260c420SBen Widawsky struct i915_hw_ppgtt {
3130260c420SBen Widawsky 	struct i915_address_space base;
3140260c420SBen Widawsky 	struct kref ref;
3150260c420SBen Widawsky 	struct drm_mm_node node;
316563222a7SBen Widawsky 	unsigned long pd_dirty_rings;
3170260c420SBen Widawsky 	unsigned num_pd_entries;
3180260c420SBen Widawsky 	unsigned num_pd_pages; /* gen8+ */
3190260c420SBen Widawsky 	union {
320ec565b3cSMichel Thierry 		struct i915_page_directory_pointer pdp;
321ec565b3cSMichel Thierry 		struct i915_page_directory pd;
322d7b3de91SBen Widawsky 	};
3230260c420SBen Widawsky 
324ec565b3cSMichel Thierry 	struct i915_page_table *scratch_pt;
3254933d519SMichel Thierry 
3264d884705SDaniel Vetter 	struct drm_i915_file_private *file_priv;
3270260c420SBen Widawsky 
328678d96fbSBen Widawsky 	gen6_pte_t __iomem *pd_addr;
329678d96fbSBen Widawsky 
3300260c420SBen Widawsky 	int (*enable)(struct i915_hw_ppgtt *ppgtt);
3310260c420SBen Widawsky 	int (*switch_mm)(struct i915_hw_ppgtt *ppgtt,
3326689c167SMcAulay, Alistair 			 struct intel_engine_cs *ring);
3330260c420SBen Widawsky 	void (*debug_dump)(struct i915_hw_ppgtt *ppgtt, struct seq_file *m);
3340260c420SBen Widawsky };
3350260c420SBen Widawsky 
336678d96fbSBen Widawsky /* For each pde iterates over every pde between from start until start + length.
337678d96fbSBen Widawsky  * If start, and start+length are not perfectly divisible, the macro will round
338678d96fbSBen Widawsky  * down, and up as needed. The macro modifies pde, start, and length. Dev is
339678d96fbSBen Widawsky  * only used to differentiate shift values. Temp is temp.  On gen6/7, start = 0,
340678d96fbSBen Widawsky  * and length = 2G effectively iterates over every PDE in the system.
341678d96fbSBen Widawsky  *
342678d96fbSBen Widawsky  * XXX: temp is not actually needed, but it saves doing the ALIGN operation.
343678d96fbSBen Widawsky  */
344678d96fbSBen Widawsky #define gen6_for_each_pde(pt, pd, start, length, temp, iter) \
345fdc454c1SMichel Thierry 	for (iter = gen6_pde_index(start); \
346fdc454c1SMichel Thierry 	     pt = (pd)->page_table[iter], length > 0 && iter < I915_PDES; \
347fdc454c1SMichel Thierry 	     iter++, \
348678d96fbSBen Widawsky 	     temp = ALIGN(start+1, 1 << GEN6_PDE_SHIFT) - start, \
349678d96fbSBen Widawsky 	     temp = min_t(unsigned, temp, length), \
350678d96fbSBen Widawsky 	     start += temp, length -= temp)
351678d96fbSBen Widawsky 
352678d96fbSBen Widawsky static inline uint32_t i915_pte_index(uint64_t address, uint32_t pde_shift)
353678d96fbSBen Widawsky {
354678d96fbSBen Widawsky 	const uint32_t mask = NUM_PTE(pde_shift) - 1;
355678d96fbSBen Widawsky 
356678d96fbSBen Widawsky 	return (address >> PAGE_SHIFT) & mask;
357678d96fbSBen Widawsky }
358678d96fbSBen Widawsky 
359678d96fbSBen Widawsky /* Helper to counts the number of PTEs within the given length. This count
360678d96fbSBen Widawsky  * does not cross a page table boundary, so the max value would be
361678d96fbSBen Widawsky  * GEN6_PTES for GEN6, and GEN8_PTES for GEN8.
362678d96fbSBen Widawsky */
363678d96fbSBen Widawsky static inline uint32_t i915_pte_count(uint64_t addr, size_t length,
364678d96fbSBen Widawsky 				      uint32_t pde_shift)
365678d96fbSBen Widawsky {
366678d96fbSBen Widawsky 	const uint64_t mask = ~((1 << pde_shift) - 1);
367678d96fbSBen Widawsky 	uint64_t end;
368678d96fbSBen Widawsky 
369678d96fbSBen Widawsky 	WARN_ON(length == 0);
370678d96fbSBen Widawsky 	WARN_ON(offset_in_page(addr|length));
371678d96fbSBen Widawsky 
372678d96fbSBen Widawsky 	end = addr + length;
373678d96fbSBen Widawsky 
374678d96fbSBen Widawsky 	if ((addr & mask) != (end & mask))
375678d96fbSBen Widawsky 		return NUM_PTE(pde_shift) - i915_pte_index(addr, pde_shift);
376678d96fbSBen Widawsky 
377678d96fbSBen Widawsky 	return i915_pte_index(end, pde_shift) - i915_pte_index(addr, pde_shift);
378678d96fbSBen Widawsky }
379678d96fbSBen Widawsky 
380678d96fbSBen Widawsky static inline uint32_t i915_pde_index(uint64_t addr, uint32_t shift)
381678d96fbSBen Widawsky {
382678d96fbSBen Widawsky 	return (addr >> shift) & I915_PDE_MASK;
383678d96fbSBen Widawsky }
384678d96fbSBen Widawsky 
385678d96fbSBen Widawsky static inline uint32_t gen6_pte_index(uint32_t addr)
386678d96fbSBen Widawsky {
387678d96fbSBen Widawsky 	return i915_pte_index(addr, GEN6_PDE_SHIFT);
388678d96fbSBen Widawsky }
389678d96fbSBen Widawsky 
390678d96fbSBen Widawsky static inline size_t gen6_pte_count(uint32_t addr, uint32_t length)
391678d96fbSBen Widawsky {
392678d96fbSBen Widawsky 	return i915_pte_count(addr, length, GEN6_PDE_SHIFT);
393678d96fbSBen Widawsky }
394678d96fbSBen Widawsky 
395678d96fbSBen Widawsky static inline uint32_t gen6_pde_index(uint32_t addr)
396678d96fbSBen Widawsky {
397678d96fbSBen Widawsky 	return i915_pde_index(addr, GEN6_PDE_SHIFT);
398678d96fbSBen Widawsky }
399678d96fbSBen Widawsky 
4000260c420SBen Widawsky int i915_gem_gtt_init(struct drm_device *dev);
4010260c420SBen Widawsky void i915_gem_init_global_gtt(struct drm_device *dev);
40290d0a0e8SDaniel Vetter void i915_global_gtt_cleanup(struct drm_device *dev);
4030260c420SBen Widawsky 
404ee960be7SDaniel Vetter 
405ee960be7SDaniel Vetter int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt);
40682460d97SDaniel Vetter int i915_ppgtt_init_hw(struct drm_device *dev);
407ee960be7SDaniel Vetter void i915_ppgtt_release(struct kref *kref);
4084d884705SDaniel Vetter struct i915_hw_ppgtt *i915_ppgtt_create(struct drm_device *dev,
4094d884705SDaniel Vetter 					struct drm_i915_file_private *fpriv);
410ee960be7SDaniel Vetter static inline void i915_ppgtt_get(struct i915_hw_ppgtt *ppgtt)
411ee960be7SDaniel Vetter {
412ee960be7SDaniel Vetter 	if (ppgtt)
413ee960be7SDaniel Vetter 		kref_get(&ppgtt->ref);
414ee960be7SDaniel Vetter }
415ee960be7SDaniel Vetter static inline void i915_ppgtt_put(struct i915_hw_ppgtt *ppgtt)
416ee960be7SDaniel Vetter {
417ee960be7SDaniel Vetter 	if (ppgtt)
418ee960be7SDaniel Vetter 		kref_put(&ppgtt->ref, i915_ppgtt_release);
419ee960be7SDaniel Vetter }
4200260c420SBen Widawsky 
4210260c420SBen Widawsky void i915_check_and_clear_faults(struct drm_device *dev);
4220260c420SBen Widawsky void i915_gem_suspend_gtt_mappings(struct drm_device *dev);
4230260c420SBen Widawsky void i915_gem_restore_gtt_mappings(struct drm_device *dev);
4240260c420SBen Widawsky 
4250260c420SBen Widawsky int __must_check i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj);
4260260c420SBen Widawsky void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj);
4270260c420SBen Widawsky 
4289abc4648SJoonas Lahtinen static inline bool
4299abc4648SJoonas Lahtinen i915_ggtt_view_equal(const struct i915_ggtt_view *a,
4309abc4648SJoonas Lahtinen                      const struct i915_ggtt_view *b)
4319abc4648SJoonas Lahtinen {
4329abc4648SJoonas Lahtinen 	if (WARN_ON(!a || !b))
4339abc4648SJoonas Lahtinen 		return false;
4349abc4648SJoonas Lahtinen 
4359abc4648SJoonas Lahtinen 	return a->type == b->type;
4369abc4648SJoonas Lahtinen }
4379abc4648SJoonas Lahtinen 
4380260c420SBen Widawsky #endif
439