xref: /dflybsd-src/sys/dev/drm/i915/i915_gem_gtt.c (revision 19c468b4dff0c9c2ceaa37f7e1e4cec69674b238)
1e3adcf8fSFrançois Tigeot /*
2e3adcf8fSFrançois Tigeot  * Copyright © 2010 Daniel Vetter
3ba55f2f5SFrançois Tigeot  * Copyright © 2011-2014 Intel Corporation
4e3adcf8fSFrançois Tigeot  *
5e3adcf8fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
6e3adcf8fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
7e3adcf8fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
8e3adcf8fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9e3adcf8fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
10e3adcf8fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
11e3adcf8fSFrançois Tigeot  *
12e3adcf8fSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
13e3adcf8fSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
14e3adcf8fSFrançois Tigeot  * Software.
15e3adcf8fSFrançois Tigeot  *
16e3adcf8fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17e3adcf8fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18e3adcf8fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19e3adcf8fSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20e3adcf8fSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21e3adcf8fSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22e3adcf8fSFrançois Tigeot  * IN THE SOFTWARE.
23e3adcf8fSFrançois Tigeot  *
24e3adcf8fSFrançois Tigeot  */
25e3adcf8fSFrançois Tigeot 
26ba55f2f5SFrançois Tigeot #include <linux/seq_file.h>
2718e26a6dSFrançois Tigeot #include <drm/drmP.h>
285c6c6f23SFrançois Tigeot #include <drm/i915_drm.h>
29e3adcf8fSFrançois Tigeot #include "i915_drv.h"
30477eb7f9SFrançois Tigeot #include "i915_vgpu.h"
31477eb7f9SFrançois Tigeot #include "i915_trace.h"
32e3adcf8fSFrançois Tigeot #include "intel_drv.h"
33e3adcf8fSFrançois Tigeot 
34477eb7f9SFrançois Tigeot #include <linux/bitmap.h>
3582046b5cSFrançois Tigeot #include <linux/highmem.h>
3682046b5cSFrançois Tigeot 
372c9916cdSFrançois Tigeot /**
382c9916cdSFrançois Tigeot  * DOC: Global GTT views
392c9916cdSFrançois Tigeot  *
402c9916cdSFrançois Tigeot  * Background and previous state
412c9916cdSFrançois Tigeot  *
422c9916cdSFrançois Tigeot  * Historically objects could exists (be bound) in global GTT space only as
432c9916cdSFrançois Tigeot  * singular instances with a view representing all of the object's backing pages
442c9916cdSFrançois Tigeot  * in a linear fashion. This view will be called a normal view.
452c9916cdSFrançois Tigeot  *
462c9916cdSFrançois Tigeot  * To support multiple views of the same object, where the number of mapped
472c9916cdSFrançois Tigeot  * pages is not equal to the backing store, or where the layout of the pages
482c9916cdSFrançois Tigeot  * is not linear, concept of a GGTT view was added.
492c9916cdSFrançois Tigeot  *
502c9916cdSFrançois Tigeot  * One example of an alternative view is a stereo display driven by a single
512c9916cdSFrançois Tigeot  * image. In this case we would have a framebuffer looking like this
522c9916cdSFrançois Tigeot  * (2x2 pages):
532c9916cdSFrançois Tigeot  *
542c9916cdSFrançois Tigeot  *    12
552c9916cdSFrançois Tigeot  *    34
562c9916cdSFrançois Tigeot  *
572c9916cdSFrançois Tigeot  * Above would represent a normal GGTT view as normally mapped for GPU or CPU
582c9916cdSFrançois Tigeot  * rendering. In contrast, fed to the display engine would be an alternative
592c9916cdSFrançois Tigeot  * view which could look something like this:
602c9916cdSFrançois Tigeot  *
612c9916cdSFrançois Tigeot  *   1212
622c9916cdSFrançois Tigeot  *   3434
632c9916cdSFrançois Tigeot  *
642c9916cdSFrançois Tigeot  * In this example both the size and layout of pages in the alternative view is
652c9916cdSFrançois Tigeot  * different from the normal view.
662c9916cdSFrançois Tigeot  *
672c9916cdSFrançois Tigeot  * Implementation and usage
682c9916cdSFrançois Tigeot  *
692c9916cdSFrançois Tigeot  * GGTT views are implemented using VMAs and are distinguished via enum
702c9916cdSFrançois Tigeot  * i915_ggtt_view_type and struct i915_ggtt_view.
712c9916cdSFrançois Tigeot  *
722c9916cdSFrançois Tigeot  * A new flavour of core GEM functions which work with GGTT bound objects were
73477eb7f9SFrançois Tigeot  * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
74477eb7f9SFrançois Tigeot  * renaming  in large amounts of code. They take the struct i915_ggtt_view
75477eb7f9SFrançois Tigeot  * parameter encapsulating all metadata required to implement a view.
762c9916cdSFrançois Tigeot  *
772c9916cdSFrançois Tigeot  * As a helper for callers which are only interested in the normal view,
782c9916cdSFrançois Tigeot  * globally const i915_ggtt_view_normal singleton instance exists. All old core
792c9916cdSFrançois Tigeot  * GEM API functions, the ones not taking the view parameter, are operating on,
802c9916cdSFrançois Tigeot  * or with the normal GGTT view.
812c9916cdSFrançois Tigeot  *
822c9916cdSFrançois Tigeot  * Code wanting to add or use a new GGTT view needs to:
832c9916cdSFrançois Tigeot  *
842c9916cdSFrançois Tigeot  * 1. Add a new enum with a suitable name.
852c9916cdSFrançois Tigeot  * 2. Extend the metadata in the i915_ggtt_view structure if required.
862c9916cdSFrançois Tigeot  * 3. Add support to i915_get_vma_pages().
872c9916cdSFrançois Tigeot  *
882c9916cdSFrançois Tigeot  * New views are required to build a scatter-gather table from within the
892c9916cdSFrançois Tigeot  * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
902c9916cdSFrançois Tigeot  * exists for the lifetime of an VMA.
912c9916cdSFrançois Tigeot  *
922c9916cdSFrançois Tigeot  * Core API is designed to have copy semantics which means that passed in
932c9916cdSFrançois Tigeot  * struct i915_ggtt_view does not need to be persistent (left around after
942c9916cdSFrançois Tigeot  * calling the core API functions).
952c9916cdSFrançois Tigeot  *
962c9916cdSFrançois Tigeot  */
972c9916cdSFrançois Tigeot 
98*19c468b4SFrançois Tigeot static int
99*19c468b4SFrançois Tigeot i915_get_ggtt_vma_pages(struct i915_vma *vma);
100*19c468b4SFrançois Tigeot 
1012c9916cdSFrançois Tigeot const struct i915_ggtt_view i915_ggtt_view_normal;
102477eb7f9SFrançois Tigeot const struct i915_ggtt_view i915_ggtt_view_rotated = {
103477eb7f9SFrançois Tigeot         .type = I915_GGTT_VIEW_ROTATED
104477eb7f9SFrançois Tigeot };
1052c9916cdSFrançois Tigeot 
10624edb884SFrançois Tigeot static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
10724edb884SFrançois Tigeot {
1082c9916cdSFrançois Tigeot 	bool has_aliasing_ppgtt;
1092c9916cdSFrançois Tigeot 	bool has_full_ppgtt;
1102c9916cdSFrançois Tigeot 
1112c9916cdSFrançois Tigeot 	has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
1122c9916cdSFrançois Tigeot 	has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
1132c9916cdSFrançois Tigeot 
114477eb7f9SFrançois Tigeot 	if (intel_vgpu_active(dev))
115477eb7f9SFrançois Tigeot 		has_full_ppgtt = false; /* emulation is too hard */
116477eb7f9SFrançois Tigeot 
1172c9916cdSFrançois Tigeot 	/*
1182c9916cdSFrançois Tigeot 	 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
1192c9916cdSFrançois Tigeot 	 * execlists, the sole mechanism available to submit work.
1202c9916cdSFrançois Tigeot 	 */
1212c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 9 &&
1222c9916cdSFrançois Tigeot 	    (enable_ppgtt == 0 || !has_aliasing_ppgtt))
12324edb884SFrançois Tigeot 		return 0;
12424edb884SFrançois Tigeot 
12524edb884SFrançois Tigeot 	if (enable_ppgtt == 1)
12624edb884SFrançois Tigeot 		return 1;
12724edb884SFrançois Tigeot 
1282c9916cdSFrançois Tigeot 	if (enable_ppgtt == 2 && has_full_ppgtt)
12924edb884SFrançois Tigeot 		return 2;
13024edb884SFrançois Tigeot 
131ba55f2f5SFrançois Tigeot #ifdef CONFIG_INTEL_IOMMU
132ba55f2f5SFrançois Tigeot 	/* Disable ppgtt on SNB if VT-d is on. */
133ba55f2f5SFrançois Tigeot 	if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
134ba55f2f5SFrançois Tigeot 		DRM_INFO("Disabling PPGTT because VT-d is on\n");
13524edb884SFrançois Tigeot 		return 0;
136ba55f2f5SFrançois Tigeot 	}
137ba55f2f5SFrançois Tigeot #endif
138f4e1c372SFrançois Tigeot 
13924edb884SFrançois Tigeot 	/* Early VLV doesn't have this */
14024edb884SFrançois Tigeot 	if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
14130d4b906SFrançois Tigeot 	    dev->pdev->revision < 0xb) {
14224edb884SFrançois Tigeot 		DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
14324edb884SFrançois Tigeot 		return 0;
14424edb884SFrançois Tigeot 	}
14524edb884SFrançois Tigeot 
1462c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
1472c9916cdSFrançois Tigeot 		return 2;
1482c9916cdSFrançois Tigeot 	else
1492c9916cdSFrançois Tigeot 		return has_aliasing_ppgtt ? 1 : 0;
150ba55f2f5SFrançois Tigeot }
1519edbd4a0SFrançois Tigeot 
152*19c468b4SFrançois Tigeot static int ppgtt_bind_vma(struct i915_vma *vma,
153ba55f2f5SFrançois Tigeot 			  enum i915_cache_level cache_level,
154*19c468b4SFrançois Tigeot 			  u32 unused)
155*19c468b4SFrançois Tigeot {
156*19c468b4SFrançois Tigeot 	u32 pte_flags = 0;
157*19c468b4SFrançois Tigeot 	const unsigned int num_entries = vma->obj->base.size >> PAGE_SHIFT;
1589edbd4a0SFrançois Tigeot 
159*19c468b4SFrançois Tigeot 	/* Currently applicable only to VLV */
160*19c468b4SFrançois Tigeot 	if (vma->obj->gt_ro)
161*19c468b4SFrançois Tigeot 		pte_flags |= PTE_READ_ONLY;
162*19c468b4SFrançois Tigeot 
163*19c468b4SFrançois Tigeot 	vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
164*19c468b4SFrançois Tigeot 				num_entries,
165*19c468b4SFrançois Tigeot 				cache_level, pte_flags);
166*19c468b4SFrançois Tigeot 
167*19c468b4SFrançois Tigeot 	return 0;
168*19c468b4SFrançois Tigeot }
169*19c468b4SFrançois Tigeot 
170*19c468b4SFrançois Tigeot static void ppgtt_unbind_vma(struct i915_vma *vma)
171*19c468b4SFrançois Tigeot {
172*19c468b4SFrançois Tigeot 	vma->vm->clear_range(vma->vm,
173*19c468b4SFrançois Tigeot 			     vma->node.start,
174*19c468b4SFrançois Tigeot 			     vma->obj->base.size,
175*19c468b4SFrançois Tigeot 			     true);
176*19c468b4SFrançois Tigeot }
177*19c468b4SFrançois Tigeot 
178*19c468b4SFrançois Tigeot static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
1799edbd4a0SFrançois Tigeot 				  enum i915_cache_level level,
1809edbd4a0SFrançois Tigeot 				  bool valid)
1819edbd4a0SFrançois Tigeot {
182477eb7f9SFrançois Tigeot 	gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
1839edbd4a0SFrançois Tigeot 	pte |= addr;
184ba55f2f5SFrançois Tigeot 
185ba55f2f5SFrançois Tigeot 	switch (level) {
186ba55f2f5SFrançois Tigeot 	case I915_CACHE_NONE:
1879edbd4a0SFrançois Tigeot 		pte |= PPAT_UNCACHED_INDEX;
188ba55f2f5SFrançois Tigeot 		break;
189ba55f2f5SFrançois Tigeot 	case I915_CACHE_WT:
190ba55f2f5SFrançois Tigeot 		pte |= PPAT_DISPLAY_ELLC_INDEX;
191ba55f2f5SFrançois Tigeot 		break;
192ba55f2f5SFrançois Tigeot 	default:
193ba55f2f5SFrançois Tigeot 		pte |= PPAT_CACHED_INDEX;
194ba55f2f5SFrançois Tigeot 		break;
195ba55f2f5SFrançois Tigeot 	}
196ba55f2f5SFrançois Tigeot 
1979edbd4a0SFrançois Tigeot 	return pte;
1989edbd4a0SFrançois Tigeot }
1999edbd4a0SFrançois Tigeot 
200*19c468b4SFrançois Tigeot static gen8_pde_t gen8_pde_encode(struct drm_device *dev,
201f4e1c372SFrançois Tigeot 				  dma_addr_t addr,
202f4e1c372SFrançois Tigeot 				  enum i915_cache_level level)
203e3adcf8fSFrançois Tigeot {
204477eb7f9SFrançois Tigeot 	gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
2059edbd4a0SFrançois Tigeot 	pde |= addr;
2069edbd4a0SFrançois Tigeot 	if (level != I915_CACHE_NONE)
2079edbd4a0SFrançois Tigeot 		pde |= PPAT_CACHED_PDE_INDEX;
2089edbd4a0SFrançois Tigeot 	else
2099edbd4a0SFrançois Tigeot 		pde |= PPAT_UNCACHED_INDEX;
2109edbd4a0SFrançois Tigeot 	return pde;
2119edbd4a0SFrançois Tigeot }
2129edbd4a0SFrançois Tigeot 
213477eb7f9SFrançois Tigeot static gen6_pte_t snb_pte_encode(dma_addr_t addr,
2149edbd4a0SFrançois Tigeot 				 enum i915_cache_level level,
21524edb884SFrançois Tigeot 				 bool valid, u32 unused)
2169edbd4a0SFrançois Tigeot {
217477eb7f9SFrançois Tigeot 	gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
218f4e1c372SFrançois Tigeot 	pte |= GEN6_PTE_ADDR_ENCODE(addr);
219f4e1c372SFrançois Tigeot 
220f4e1c372SFrançois Tigeot 	switch (level) {
2219edbd4a0SFrançois Tigeot 	case I915_CACHE_L3_LLC:
2229edbd4a0SFrançois Tigeot 	case I915_CACHE_LLC:
2239edbd4a0SFrançois Tigeot 		pte |= GEN6_PTE_CACHE_LLC;
2249edbd4a0SFrançois Tigeot 		break;
2259edbd4a0SFrançois Tigeot 	case I915_CACHE_NONE:
2269edbd4a0SFrançois Tigeot 		pte |= GEN6_PTE_UNCACHED;
2279edbd4a0SFrançois Tigeot 		break;
2289edbd4a0SFrançois Tigeot 	default:
2292c9916cdSFrançois Tigeot 		MISSING_CASE(level);
2309edbd4a0SFrançois Tigeot 	}
2319edbd4a0SFrançois Tigeot 
2329edbd4a0SFrançois Tigeot 	return pte;
2339edbd4a0SFrançois Tigeot }
2349edbd4a0SFrançois Tigeot 
235477eb7f9SFrançois Tigeot static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
2369edbd4a0SFrançois Tigeot 				 enum i915_cache_level level,
23724edb884SFrançois Tigeot 				 bool valid, u32 unused)
2389edbd4a0SFrançois Tigeot {
239477eb7f9SFrançois Tigeot 	gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
2409edbd4a0SFrançois Tigeot 	pte |= GEN6_PTE_ADDR_ENCODE(addr);
2419edbd4a0SFrançois Tigeot 
2429edbd4a0SFrançois Tigeot 	switch (level) {
2439edbd4a0SFrançois Tigeot 	case I915_CACHE_L3_LLC:
2449edbd4a0SFrançois Tigeot 		pte |= GEN7_PTE_CACHE_L3_LLC;
245f4e1c372SFrançois Tigeot 		break;
246f4e1c372SFrançois Tigeot 	case I915_CACHE_LLC:
247f4e1c372SFrançois Tigeot 		pte |= GEN6_PTE_CACHE_LLC;
248f4e1c372SFrançois Tigeot 		break;
249f4e1c372SFrançois Tigeot 	case I915_CACHE_NONE:
250f4e1c372SFrançois Tigeot 		pte |= GEN6_PTE_UNCACHED;
251f4e1c372SFrançois Tigeot 		break;
252f4e1c372SFrançois Tigeot 	default:
2532c9916cdSFrançois Tigeot 		MISSING_CASE(level);
254f4e1c372SFrançois Tigeot 	}
255f4e1c372SFrançois Tigeot 
256f4e1c372SFrançois Tigeot 	return pte;
257f4e1c372SFrançois Tigeot }
258f4e1c372SFrançois Tigeot 
259477eb7f9SFrançois Tigeot static gen6_pte_t byt_pte_encode(dma_addr_t addr,
2609edbd4a0SFrançois Tigeot 				 enum i915_cache_level level,
26124edb884SFrançois Tigeot 				 bool valid, u32 flags)
2625d0b1887SFrançois Tigeot {
263477eb7f9SFrançois Tigeot 	gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
2645d0b1887SFrançois Tigeot 	pte |= GEN6_PTE_ADDR_ENCODE(addr);
2655d0b1887SFrançois Tigeot 
26624edb884SFrançois Tigeot 	if (!(flags & PTE_READ_ONLY))
2675d0b1887SFrançois Tigeot 		pte |= BYT_PTE_WRITEABLE;
2685d0b1887SFrançois Tigeot 
2695d0b1887SFrançois Tigeot 	if (level != I915_CACHE_NONE)
2705d0b1887SFrançois Tigeot 		pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
2715d0b1887SFrançois Tigeot 
2725d0b1887SFrançois Tigeot 	return pte;
2735d0b1887SFrançois Tigeot }
2745d0b1887SFrançois Tigeot 
275477eb7f9SFrançois Tigeot static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
2769edbd4a0SFrançois Tigeot 				 enum i915_cache_level level,
27724edb884SFrançois Tigeot 				 bool valid, u32 unused)
2785d0b1887SFrançois Tigeot {
279477eb7f9SFrançois Tigeot 	gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
2809edbd4a0SFrançois Tigeot 	pte |= HSW_PTE_ADDR_ENCODE(addr);
2815d0b1887SFrançois Tigeot 
2825d0b1887SFrançois Tigeot 	if (level != I915_CACHE_NONE)
2839edbd4a0SFrançois Tigeot 		pte |= HSW_WB_LLC_AGE3;
2845d0b1887SFrançois Tigeot 
2855d0b1887SFrançois Tigeot 	return pte;
2865d0b1887SFrançois Tigeot }
2875d0b1887SFrançois Tigeot 
288477eb7f9SFrançois Tigeot static gen6_pte_t iris_pte_encode(dma_addr_t addr,
2899edbd4a0SFrançois Tigeot 				  enum i915_cache_level level,
29024edb884SFrançois Tigeot 				  bool valid, u32 unused)
2919edbd4a0SFrançois Tigeot {
292477eb7f9SFrançois Tigeot 	gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
2939edbd4a0SFrançois Tigeot 	pte |= HSW_PTE_ADDR_ENCODE(addr);
2949edbd4a0SFrançois Tigeot 
2959edbd4a0SFrançois Tigeot 	switch (level) {
2969edbd4a0SFrançois Tigeot 	case I915_CACHE_NONE:
2979edbd4a0SFrançois Tigeot 		break;
2989edbd4a0SFrançois Tigeot 	case I915_CACHE_WT:
2999edbd4a0SFrançois Tigeot 		pte |= HSW_WT_ELLC_LLC_AGE3;
3009edbd4a0SFrançois Tigeot 		break;
3019edbd4a0SFrançois Tigeot 	default:
3029edbd4a0SFrançois Tigeot 		pte |= HSW_WB_ELLC_LLC_AGE3;
3039edbd4a0SFrançois Tigeot 		break;
3049edbd4a0SFrançois Tigeot 	}
3059edbd4a0SFrançois Tigeot 
3069edbd4a0SFrançois Tigeot 	return pte;
3079edbd4a0SFrançois Tigeot }
3089edbd4a0SFrançois Tigeot 
309477eb7f9SFrançois Tigeot #define i915_dma_unmap_single(px, dev) \
310477eb7f9SFrançois Tigeot 	__i915_dma_unmap_single((px)->daddr, dev)
311477eb7f9SFrançois Tigeot 
312*19c468b4SFrançois Tigeot static void __i915_dma_unmap_single(dma_addr_t daddr,
313477eb7f9SFrançois Tigeot 				    struct drm_device *dev)
314477eb7f9SFrançois Tigeot {
315477eb7f9SFrançois Tigeot #if 0
316477eb7f9SFrançois Tigeot 	struct device *device = &dev->pdev->dev;
317477eb7f9SFrançois Tigeot 
318477eb7f9SFrançois Tigeot 	dma_unmap_page(device, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
319477eb7f9SFrançois Tigeot #endif
320477eb7f9SFrançois Tigeot }
321477eb7f9SFrançois Tigeot 
322477eb7f9SFrançois Tigeot /**
323477eb7f9SFrançois Tigeot  * i915_dma_map_single() - Create a dma mapping for a page table/dir/etc.
324477eb7f9SFrançois Tigeot  * @px:	Page table/dir/etc to get a DMA map for
325477eb7f9SFrançois Tigeot  * @dev:	drm device
326477eb7f9SFrançois Tigeot  *
327477eb7f9SFrançois Tigeot  * Page table allocations are unified across all gens. They always require a
328477eb7f9SFrançois Tigeot  * single 4k allocation, as well as a DMA mapping. If we keep the structs
329477eb7f9SFrançois Tigeot  * symmetric here, the simple macro covers us for every page table type.
330477eb7f9SFrançois Tigeot  *
331477eb7f9SFrançois Tigeot  * Return: 0 if success.
332477eb7f9SFrançois Tigeot  */
333477eb7f9SFrançois Tigeot #define i915_dma_map_single(px, dev) \
334477eb7f9SFrançois Tigeot 	i915_dma_map_page_single((px)->page, (dev), &(px)->daddr)
335477eb7f9SFrançois Tigeot 
336*19c468b4SFrançois Tigeot static int i915_dma_map_page_single(struct vm_page *page,
337477eb7f9SFrançois Tigeot 				    struct drm_device *dev,
338477eb7f9SFrançois Tigeot 				    dma_addr_t *daddr)
339477eb7f9SFrançois Tigeot {
340477eb7f9SFrançois Tigeot 	struct device *device = dev->pdev->dev;
341477eb7f9SFrançois Tigeot 
342477eb7f9SFrançois Tigeot 	*daddr = dma_map_page(device, page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
343477eb7f9SFrançois Tigeot 	if (dma_mapping_error(device, *daddr))
344477eb7f9SFrançois Tigeot 		return -ENOMEM;
345477eb7f9SFrançois Tigeot 
346477eb7f9SFrançois Tigeot 	return 0;
347477eb7f9SFrançois Tigeot }
348477eb7f9SFrançois Tigeot 
349*19c468b4SFrançois Tigeot static void unmap_and_free_pt(struct i915_page_table *pt,
350477eb7f9SFrançois Tigeot 			       struct drm_device *dev)
351477eb7f9SFrançois Tigeot {
352477eb7f9SFrançois Tigeot 	if (WARN_ON(!pt->page))
353477eb7f9SFrançois Tigeot 		return;
354477eb7f9SFrançois Tigeot 
355477eb7f9SFrançois Tigeot 	i915_dma_unmap_single(pt, dev);
356477eb7f9SFrançois Tigeot 	__free_page(pt->page);
357477eb7f9SFrançois Tigeot 	kfree(pt->used_ptes);
358477eb7f9SFrançois Tigeot 	kfree(pt);
359477eb7f9SFrançois Tigeot }
360477eb7f9SFrançois Tigeot 
361*19c468b4SFrançois Tigeot static void gen8_initialize_pt(struct i915_address_space *vm,
362*19c468b4SFrançois Tigeot 			       struct i915_page_table *pt)
363477eb7f9SFrançois Tigeot {
364*19c468b4SFrançois Tigeot 	gen8_pte_t *pt_vaddr, scratch_pte;
365*19c468b4SFrançois Tigeot 	int i;
366*19c468b4SFrançois Tigeot 
367*19c468b4SFrançois Tigeot 	pt_vaddr = kmap_atomic(pt->page);
368*19c468b4SFrançois Tigeot 	scratch_pte = gen8_pte_encode(vm->scratch.addr,
369*19c468b4SFrançois Tigeot 				      I915_CACHE_LLC, true);
370*19c468b4SFrançois Tigeot 
371*19c468b4SFrançois Tigeot 	for (i = 0; i < GEN8_PTES; i++)
372*19c468b4SFrançois Tigeot 		pt_vaddr[i] = scratch_pte;
373*19c468b4SFrançois Tigeot 
374*19c468b4SFrançois Tigeot 	if (!HAS_LLC(vm->dev))
375*19c468b4SFrançois Tigeot 		drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
376*19c468b4SFrançois Tigeot 	kunmap_atomic(pt_vaddr);
377*19c468b4SFrançois Tigeot }
378*19c468b4SFrançois Tigeot 
379*19c468b4SFrançois Tigeot static struct i915_page_table *alloc_pt_single(struct drm_device *dev)
380*19c468b4SFrançois Tigeot {
381*19c468b4SFrançois Tigeot 	struct i915_page_table *pt;
382477eb7f9SFrançois Tigeot 	const size_t count = INTEL_INFO(dev)->gen >= 8 ?
383477eb7f9SFrançois Tigeot 		GEN8_PTES : GEN6_PTES;
384477eb7f9SFrançois Tigeot 	int ret = -ENOMEM;
385477eb7f9SFrançois Tigeot 
386477eb7f9SFrançois Tigeot 	pt = kzalloc(sizeof(*pt), GFP_KERNEL);
387477eb7f9SFrançois Tigeot 	if (!pt)
388477eb7f9SFrançois Tigeot 		return ERR_PTR(-ENOMEM);
389477eb7f9SFrançois Tigeot 
390477eb7f9SFrançois Tigeot 	pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
391477eb7f9SFrançois Tigeot 				GFP_KERNEL);
392477eb7f9SFrançois Tigeot 
393477eb7f9SFrançois Tigeot 	if (!pt->used_ptes)
394477eb7f9SFrançois Tigeot 		goto fail_bitmap;
395477eb7f9SFrançois Tigeot 
396477eb7f9SFrançois Tigeot 	pt->page = alloc_page(GFP_KERNEL);
397477eb7f9SFrançois Tigeot 	if (!pt->page)
398477eb7f9SFrançois Tigeot 		goto fail_page;
399477eb7f9SFrançois Tigeot 
400477eb7f9SFrançois Tigeot 	ret = i915_dma_map_single(pt, dev);
401477eb7f9SFrançois Tigeot 	if (ret)
402477eb7f9SFrançois Tigeot 		goto fail_dma;
403477eb7f9SFrançois Tigeot 
404477eb7f9SFrançois Tigeot 	return pt;
405477eb7f9SFrançois Tigeot 
406477eb7f9SFrançois Tigeot fail_dma:
407477eb7f9SFrançois Tigeot 	__free_page(pt->page);
408477eb7f9SFrançois Tigeot fail_page:
409477eb7f9SFrançois Tigeot 	kfree(pt->used_ptes);
410477eb7f9SFrançois Tigeot fail_bitmap:
411477eb7f9SFrançois Tigeot 	kfree(pt);
412477eb7f9SFrançois Tigeot 
413477eb7f9SFrançois Tigeot 	return ERR_PTR(ret);
414477eb7f9SFrançois Tigeot }
415477eb7f9SFrançois Tigeot 
416477eb7f9SFrançois Tigeot /**
417477eb7f9SFrançois Tigeot  * alloc_pt_range() - Allocate a multiple page tables
418477eb7f9SFrançois Tigeot  * @pd:		The page directory which will have at least @count entries
419477eb7f9SFrançois Tigeot  *		available to point to the allocated page tables.
420477eb7f9SFrançois Tigeot  * @pde:	First page directory entry for which we are allocating.
421477eb7f9SFrançois Tigeot  * @count:	Number of pages to allocate.
422477eb7f9SFrançois Tigeot  * @dev:	DRM device.
423477eb7f9SFrançois Tigeot  *
424477eb7f9SFrançois Tigeot  * Allocates multiple page table pages and sets the appropriate entries in the
425477eb7f9SFrançois Tigeot  * page table structure within the page directory. Function cleans up after
426477eb7f9SFrançois Tigeot  * itself on any failures.
427477eb7f9SFrançois Tigeot  *
428477eb7f9SFrançois Tigeot  * Return: 0 if allocation succeeded.
429477eb7f9SFrançois Tigeot  */
430*19c468b4SFrançois Tigeot static int alloc_pt_range(struct i915_page_directory *pd, uint16_t pde, size_t count,
431477eb7f9SFrançois Tigeot 			  struct drm_device *dev)
432477eb7f9SFrançois Tigeot {
433477eb7f9SFrançois Tigeot 	int i, ret;
434477eb7f9SFrançois Tigeot 
435477eb7f9SFrançois Tigeot 	/* 512 is the max page tables per page_directory on any platform. */
436477eb7f9SFrançois Tigeot 	if (WARN_ON(pde + count > I915_PDES))
437477eb7f9SFrançois Tigeot 		return -EINVAL;
438477eb7f9SFrançois Tigeot 
439477eb7f9SFrançois Tigeot 	for (i = pde; i < pde + count; i++) {
440*19c468b4SFrançois Tigeot 		struct i915_page_table *pt = alloc_pt_single(dev);
441477eb7f9SFrançois Tigeot 
442477eb7f9SFrançois Tigeot 		if (IS_ERR(pt)) {
443477eb7f9SFrançois Tigeot 			ret = PTR_ERR(pt);
444477eb7f9SFrançois Tigeot 			goto err_out;
445477eb7f9SFrançois Tigeot 		}
446477eb7f9SFrançois Tigeot 		WARN(pd->page_table[i],
447477eb7f9SFrançois Tigeot 		     "Leaking page directory entry %d (%p)\n",
448477eb7f9SFrançois Tigeot 		     i, pd->page_table[i]);
449477eb7f9SFrançois Tigeot 		pd->page_table[i] = pt;
450477eb7f9SFrançois Tigeot 	}
451477eb7f9SFrançois Tigeot 
452477eb7f9SFrançois Tigeot 	return 0;
453477eb7f9SFrançois Tigeot 
454477eb7f9SFrançois Tigeot err_out:
455477eb7f9SFrançois Tigeot 	while (i-- > pde)
456477eb7f9SFrançois Tigeot 		unmap_and_free_pt(pd->page_table[i], dev);
457477eb7f9SFrançois Tigeot 	return ret;
458477eb7f9SFrançois Tigeot }
459477eb7f9SFrançois Tigeot 
460*19c468b4SFrançois Tigeot static void unmap_and_free_pd(struct i915_page_directory *pd,
461*19c468b4SFrançois Tigeot 			      struct drm_device *dev)
462477eb7f9SFrançois Tigeot {
463477eb7f9SFrançois Tigeot 	if (pd->page) {
464*19c468b4SFrançois Tigeot 		i915_dma_unmap_single(pd, dev);
465477eb7f9SFrançois Tigeot 		__free_page(pd->page);
466*19c468b4SFrançois Tigeot 		kfree(pd->used_pdes);
467477eb7f9SFrançois Tigeot 		kfree(pd);
468477eb7f9SFrançois Tigeot 	}
469477eb7f9SFrançois Tigeot }
470477eb7f9SFrançois Tigeot 
471*19c468b4SFrançois Tigeot static struct i915_page_directory *alloc_pd_single(struct drm_device *dev)
472477eb7f9SFrançois Tigeot {
473*19c468b4SFrançois Tigeot 	struct i915_page_directory *pd;
474*19c468b4SFrançois Tigeot 	int ret = -ENOMEM;
475477eb7f9SFrançois Tigeot 
476477eb7f9SFrançois Tigeot 	pd = kzalloc(sizeof(*pd), GFP_KERNEL);
477477eb7f9SFrançois Tigeot 	if (!pd)
478477eb7f9SFrançois Tigeot 		return ERR_PTR(-ENOMEM);
479477eb7f9SFrançois Tigeot 
480*19c468b4SFrançois Tigeot 	pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
481*19c468b4SFrançois Tigeot 				sizeof(*pd->used_pdes), GFP_KERNEL);
482*19c468b4SFrançois Tigeot 	if (!pd->used_pdes)
483*19c468b4SFrançois Tigeot 		goto free_pd;
484*19c468b4SFrançois Tigeot 
485*19c468b4SFrançois Tigeot 	pd->page = alloc_page(GFP_KERNEL);
486*19c468b4SFrançois Tigeot 	if (!pd->page)
487*19c468b4SFrançois Tigeot 		goto free_bitmap;
488*19c468b4SFrançois Tigeot 
489*19c468b4SFrançois Tigeot 	ret = i915_dma_map_single(pd, dev);
490*19c468b4SFrançois Tigeot 	if (ret)
491*19c468b4SFrançois Tigeot 		goto free_page;
492477eb7f9SFrançois Tigeot 
493477eb7f9SFrançois Tigeot 	return pd;
494*19c468b4SFrançois Tigeot 
495*19c468b4SFrançois Tigeot free_page:
496*19c468b4SFrançois Tigeot 	__free_page(pd->page);
497*19c468b4SFrançois Tigeot free_bitmap:
498*19c468b4SFrançois Tigeot 	kfree(pd->used_pdes);
499*19c468b4SFrançois Tigeot free_pd:
500*19c468b4SFrançois Tigeot 	kfree(pd);
501*19c468b4SFrançois Tigeot 
502*19c468b4SFrançois Tigeot 	return ERR_PTR(ret);
503477eb7f9SFrançois Tigeot }
504477eb7f9SFrançois Tigeot 
5059edbd4a0SFrançois Tigeot /* Broadwell Page Directory Pointer Descriptors */
506*19c468b4SFrançois Tigeot static int gen8_write_pdp(struct intel_engine_cs *ring,
507*19c468b4SFrançois Tigeot 			  unsigned entry,
508*19c468b4SFrançois Tigeot 			  dma_addr_t addr)
5099edbd4a0SFrançois Tigeot {
5109edbd4a0SFrançois Tigeot 	int ret;
5119edbd4a0SFrançois Tigeot 
5129edbd4a0SFrançois Tigeot 	BUG_ON(entry >= 4);
5139edbd4a0SFrançois Tigeot 
5149edbd4a0SFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
5159edbd4a0SFrançois Tigeot 	if (ret)
5169edbd4a0SFrançois Tigeot 		return ret;
5179edbd4a0SFrançois Tigeot 
5189edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
5199edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
520*19c468b4SFrançois Tigeot 	intel_ring_emit(ring, upper_32_bits(addr));
5219edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
5229edbd4a0SFrançois Tigeot 	intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
523*19c468b4SFrançois Tigeot 	intel_ring_emit(ring, lower_32_bits(addr));
5249edbd4a0SFrançois Tigeot 	intel_ring_advance(ring);
5259edbd4a0SFrançois Tigeot 
5269edbd4a0SFrançois Tigeot 	return 0;
5279edbd4a0SFrançois Tigeot }
5289edbd4a0SFrançois Tigeot 
529ba55f2f5SFrançois Tigeot static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
5301b13d190SFrançois Tigeot 			  struct intel_engine_cs *ring)
5319edbd4a0SFrançois Tigeot {
532ba55f2f5SFrançois Tigeot 	int i, ret;
5339edbd4a0SFrançois Tigeot 
534*19c468b4SFrançois Tigeot 	for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
535*19c468b4SFrançois Tigeot 		struct i915_page_directory *pd = ppgtt->pdp.page_directory[i];
536*19c468b4SFrançois Tigeot 		dma_addr_t pd_daddr = pd ? pd->daddr : ppgtt->scratch_pd->daddr;
537*19c468b4SFrançois Tigeot 		/* The page directory might be NULL, but we need to clear out
538*19c468b4SFrançois Tigeot 		 * whatever the previous context might have used. */
539*19c468b4SFrançois Tigeot 		ret = gen8_write_pdp(ring, i, pd_daddr);
5409edbd4a0SFrançois Tigeot 		if (ret)
5419edbd4a0SFrançois Tigeot 			return ret;
5429edbd4a0SFrançois Tigeot 	}
5439edbd4a0SFrançois Tigeot 
544ba55f2f5SFrançois Tigeot 	return 0;
545ba55f2f5SFrançois Tigeot }
546ba55f2f5SFrançois Tigeot 
5479edbd4a0SFrançois Tigeot static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
548ba55f2f5SFrançois Tigeot 				   uint64_t start,
549ba55f2f5SFrançois Tigeot 				   uint64_t length,
5509edbd4a0SFrançois Tigeot 				   bool use_scratch)
5519edbd4a0SFrançois Tigeot {
5529edbd4a0SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
5539edbd4a0SFrançois Tigeot 		container_of(vm, struct i915_hw_ppgtt, base);
554477eb7f9SFrançois Tigeot 	gen8_pte_t *pt_vaddr, scratch_pte;
555ba55f2f5SFrançois Tigeot 	unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
556ba55f2f5SFrançois Tigeot 	unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
557ba55f2f5SFrançois Tigeot 	unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
558ba55f2f5SFrançois Tigeot 	unsigned num_entries = length >> PAGE_SHIFT;
5599edbd4a0SFrançois Tigeot 	unsigned last_pte, i;
5609edbd4a0SFrançois Tigeot 
5619edbd4a0SFrançois Tigeot 	scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
5629edbd4a0SFrançois Tigeot 				      I915_CACHE_LLC, use_scratch);
5639edbd4a0SFrançois Tigeot 
5649edbd4a0SFrançois Tigeot 	while (num_entries) {
565*19c468b4SFrançois Tigeot 		struct i915_page_directory *pd;
566*19c468b4SFrançois Tigeot 		struct i915_page_table *pt;
567477eb7f9SFrançois Tigeot 		struct vm_page *page_table;
568477eb7f9SFrançois Tigeot 
569477eb7f9SFrançois Tigeot 		if (WARN_ON(!ppgtt->pdp.page_directory[pdpe]))
570*19c468b4SFrançois Tigeot 			break;
571477eb7f9SFrançois Tigeot 
572477eb7f9SFrançois Tigeot 		pd = ppgtt->pdp.page_directory[pdpe];
573477eb7f9SFrançois Tigeot 
574477eb7f9SFrançois Tigeot 		if (WARN_ON(!pd->page_table[pde]))
575*19c468b4SFrançois Tigeot 			break;
576477eb7f9SFrançois Tigeot 
577477eb7f9SFrançois Tigeot 		pt = pd->page_table[pde];
578477eb7f9SFrançois Tigeot 
579477eb7f9SFrançois Tigeot 		if (WARN_ON(!pt->page))
580*19c468b4SFrançois Tigeot 			break;
581477eb7f9SFrançois Tigeot 
582477eb7f9SFrançois Tigeot 		page_table = pt->page;
5839edbd4a0SFrançois Tigeot 
584ba55f2f5SFrançois Tigeot 		last_pte = pte + num_entries;
585477eb7f9SFrançois Tigeot 		if (last_pte > GEN8_PTES)
586477eb7f9SFrançois Tigeot 			last_pte = GEN8_PTES;
5879edbd4a0SFrançois Tigeot 
5889edbd4a0SFrançois Tigeot 		pt_vaddr = kmap_atomic(page_table);
5899edbd4a0SFrançois Tigeot 
590ba55f2f5SFrançois Tigeot 		for (i = pte; i < last_pte; i++) {
5919edbd4a0SFrançois Tigeot 			pt_vaddr[i] = scratch_pte;
592ba55f2f5SFrançois Tigeot 			num_entries--;
593ba55f2f5SFrançois Tigeot 		}
5949edbd4a0SFrançois Tigeot 
595ba55f2f5SFrançois Tigeot 		if (!HAS_LLC(ppgtt->base.dev))
596ba55f2f5SFrançois Tigeot 			drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
5979edbd4a0SFrançois Tigeot 		kunmap_atomic(pt_vaddr);
5989edbd4a0SFrançois Tigeot 
599ba55f2f5SFrançois Tigeot 		pte = 0;
600477eb7f9SFrançois Tigeot 		if (++pde == I915_PDES) {
601ba55f2f5SFrançois Tigeot 			pdpe++;
602ba55f2f5SFrançois Tigeot 			pde = 0;
603ba55f2f5SFrançois Tigeot 		}
6049edbd4a0SFrançois Tigeot 	}
6059edbd4a0SFrançois Tigeot }
6069edbd4a0SFrançois Tigeot 
6079edbd4a0SFrançois Tigeot static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
6089edbd4a0SFrançois Tigeot 				      vm_page_t *pages,
609ba55f2f5SFrançois Tigeot 				      uint64_t start,
6109edbd4a0SFrançois Tigeot 				      unsigned int num_entries,
61124edb884SFrançois Tigeot 				      enum i915_cache_level cache_level, u32 unused)
6129edbd4a0SFrançois Tigeot {
6139edbd4a0SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
6149edbd4a0SFrançois Tigeot 		container_of(vm, struct i915_hw_ppgtt, base);
615477eb7f9SFrançois Tigeot 	gen8_pte_t *pt_vaddr;
616ba55f2f5SFrançois Tigeot 	unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
617ba55f2f5SFrançois Tigeot 	unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
618ba55f2f5SFrançois Tigeot 	unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
6199edbd4a0SFrançois Tigeot 	int i;
6209edbd4a0SFrançois Tigeot 
6219edbd4a0SFrançois Tigeot 	pt_vaddr = NULL;
6229edbd4a0SFrançois Tigeot 
623ba55f2f5SFrançois Tigeot 	for (i=0;i<num_entries;i++) {
624477eb7f9SFrançois Tigeot 		if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES))
625ba55f2f5SFrançois Tigeot 			break;
626ba55f2f5SFrançois Tigeot 
627477eb7f9SFrançois Tigeot 		if (pt_vaddr == NULL) {
628*19c468b4SFrançois Tigeot 			struct i915_page_directory *pd = ppgtt->pdp.page_directory[pdpe];
629*19c468b4SFrançois Tigeot 			struct i915_page_table *pt = pd->page_table[pde];
630477eb7f9SFrançois Tigeot 			struct vm_page *page_table = pt->page;
631477eb7f9SFrançois Tigeot 
632477eb7f9SFrançois Tigeot 			pt_vaddr = kmap_atomic(page_table);
633477eb7f9SFrançois Tigeot 		}
634ba55f2f5SFrançois Tigeot 
635ba55f2f5SFrançois Tigeot 		pt_vaddr[pte] =
6369edbd4a0SFrançois Tigeot 			gen8_pte_encode(VM_PAGE_TO_PHYS(pages[i]),
6379edbd4a0SFrançois Tigeot 					cache_level, true);
638477eb7f9SFrançois Tigeot 		if (++pte == GEN8_PTES) {
639ba55f2f5SFrançois Tigeot 			if (!HAS_LLC(ppgtt->base.dev))
640ba55f2f5SFrançois Tigeot 				drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
6419edbd4a0SFrançois Tigeot 			kunmap_atomic(pt_vaddr);
6429edbd4a0SFrançois Tigeot 			pt_vaddr = NULL;
643477eb7f9SFrançois Tigeot 			if (++pde == I915_PDES) {
644ba55f2f5SFrançois Tigeot 				pdpe++;
645ba55f2f5SFrançois Tigeot 				pde = 0;
646ba55f2f5SFrançois Tigeot 			}
647ba55f2f5SFrançois Tigeot 			pte = 0;
6489edbd4a0SFrançois Tigeot 		}
6499edbd4a0SFrançois Tigeot 	}
650ba55f2f5SFrançois Tigeot 	if (pt_vaddr) {
651ba55f2f5SFrançois Tigeot 		if (!HAS_LLC(ppgtt->base.dev))
652ba55f2f5SFrançois Tigeot 			drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
6539edbd4a0SFrançois Tigeot 		kunmap_atomic(pt_vaddr);
6549edbd4a0SFrançois Tigeot 	}
655ba55f2f5SFrançois Tigeot }
656ba55f2f5SFrançois Tigeot 
657*19c468b4SFrançois Tigeot static void __gen8_do_map_pt(gen8_pde_t * const pde,
658*19c468b4SFrançois Tigeot 			     struct i915_page_table *pt,
659*19c468b4SFrançois Tigeot 			     struct drm_device *dev)
660*19c468b4SFrançois Tigeot {
661*19c468b4SFrançois Tigeot 	gen8_pde_t entry =
662*19c468b4SFrançois Tigeot 		gen8_pde_encode(dev, pt->daddr, I915_CACHE_LLC);
663*19c468b4SFrançois Tigeot 	*pde = entry;
664*19c468b4SFrançois Tigeot }
665*19c468b4SFrançois Tigeot 
666*19c468b4SFrançois Tigeot static void gen8_initialize_pd(struct i915_address_space *vm,
667*19c468b4SFrançois Tigeot 			       struct i915_page_directory *pd)
668*19c468b4SFrançois Tigeot {
669*19c468b4SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
670*19c468b4SFrançois Tigeot 			container_of(vm, struct i915_hw_ppgtt, base);
671*19c468b4SFrançois Tigeot 	gen8_pde_t *page_directory;
672*19c468b4SFrançois Tigeot 	struct i915_page_table *pt;
673*19c468b4SFrançois Tigeot 	int i;
674*19c468b4SFrançois Tigeot 
675*19c468b4SFrançois Tigeot 	page_directory = kmap_atomic(pd->page);
676*19c468b4SFrançois Tigeot 	pt = ppgtt->scratch_pt;
677*19c468b4SFrançois Tigeot 	for (i = 0; i < I915_PDES; i++)
678*19c468b4SFrançois Tigeot 		/* Map the PDE to the page table */
679*19c468b4SFrançois Tigeot 		__gen8_do_map_pt(page_directory + i, pt, vm->dev);
680*19c468b4SFrançois Tigeot 
681*19c468b4SFrançois Tigeot 	if (!HAS_LLC(vm->dev))
682*19c468b4SFrançois Tigeot 		drm_clflush_virt_range(page_directory, PAGE_SIZE);
683*19c468b4SFrançois Tigeot 	kunmap_atomic(page_directory);
684*19c468b4SFrançois Tigeot }
685*19c468b4SFrançois Tigeot 
686*19c468b4SFrançois Tigeot static void gen8_free_page_tables(struct i915_page_directory *pd, struct drm_device *dev)
687ba55f2f5SFrançois Tigeot {
688ba55f2f5SFrançois Tigeot 	int i;
689ba55f2f5SFrançois Tigeot 
690477eb7f9SFrançois Tigeot 	if (!pd->page)
691ba55f2f5SFrançois Tigeot 		return;
692ba55f2f5SFrançois Tigeot 
693*19c468b4SFrançois Tigeot 	for_each_set_bit(i, pd->used_pdes, I915_PDES) {
694477eb7f9SFrançois Tigeot 		if (WARN_ON(!pd->page_table[i]))
695477eb7f9SFrançois Tigeot 			continue;
696477eb7f9SFrançois Tigeot 
697477eb7f9SFrançois Tigeot 		unmap_and_free_pt(pd->page_table[i], dev);
698477eb7f9SFrançois Tigeot 		pd->page_table[i] = NULL;
699477eb7f9SFrançois Tigeot 	}
700ba55f2f5SFrançois Tigeot }
701ba55f2f5SFrançois Tigeot 
7029edbd4a0SFrançois Tigeot static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
7039edbd4a0SFrançois Tigeot {
7049edbd4a0SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
7059edbd4a0SFrançois Tigeot 		container_of(vm, struct i915_hw_ppgtt, base);
706*19c468b4SFrançois Tigeot 	int i;
7079edbd4a0SFrançois Tigeot 
708*19c468b4SFrançois Tigeot 	for_each_set_bit(i, ppgtt->pdp.used_pdpes, GEN8_LEGACY_PDPES) {
709*19c468b4SFrançois Tigeot 		if (WARN_ON(!ppgtt->pdp.page_directory[i]))
710*19c468b4SFrançois Tigeot 			continue;
711*19c468b4SFrançois Tigeot 
712*19c468b4SFrançois Tigeot 		gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
713*19c468b4SFrançois Tigeot 		unmap_and_free_pd(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
7149edbd4a0SFrançois Tigeot 	}
7159edbd4a0SFrançois Tigeot 
716*19c468b4SFrançois Tigeot 	unmap_and_free_pd(ppgtt->scratch_pd, ppgtt->base.dev);
717*19c468b4SFrançois Tigeot 	unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
718*19c468b4SFrançois Tigeot }
719ba55f2f5SFrançois Tigeot 
720*19c468b4SFrançois Tigeot /**
721*19c468b4SFrançois Tigeot  * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
722*19c468b4SFrançois Tigeot  * @ppgtt:	Master ppgtt structure.
723*19c468b4SFrançois Tigeot  * @pd:		Page directory for this address range.
724*19c468b4SFrançois Tigeot  * @start:	Starting virtual address to begin allocations.
725*19c468b4SFrançois Tigeot  * @length	Size of the allocations.
726*19c468b4SFrançois Tigeot  * @new_pts:	Bitmap set by function with new allocations. Likely used by the
727*19c468b4SFrançois Tigeot  *		caller to free on error.
728*19c468b4SFrançois Tigeot  *
729*19c468b4SFrançois Tigeot  * Allocate the required number of page tables. Extremely similar to
730*19c468b4SFrançois Tigeot  * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
731*19c468b4SFrançois Tigeot  * the page directory boundary (instead of the page directory pointer). That
732*19c468b4SFrançois Tigeot  * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
733*19c468b4SFrançois Tigeot  * possible, and likely that the caller will need to use multiple calls of this
734*19c468b4SFrançois Tigeot  * function to achieve the appropriate allocation.
735*19c468b4SFrançois Tigeot  *
736*19c468b4SFrançois Tigeot  * Return: 0 if success; negative error code otherwise.
737*19c468b4SFrançois Tigeot  */
738*19c468b4SFrançois Tigeot static int gen8_ppgtt_alloc_pagetabs(struct i915_hw_ppgtt *ppgtt,
739*19c468b4SFrançois Tigeot 				     struct i915_page_directory *pd,
740*19c468b4SFrançois Tigeot 				     uint64_t start,
741*19c468b4SFrançois Tigeot 				     uint64_t length,
742*19c468b4SFrançois Tigeot 				     unsigned long *new_pts)
743*19c468b4SFrançois Tigeot {
744*19c468b4SFrançois Tigeot 	struct drm_device *dev = ppgtt->base.dev;
745*19c468b4SFrançois Tigeot 	struct i915_page_table *pt;
746*19c468b4SFrançois Tigeot 	uint64_t temp;
747*19c468b4SFrançois Tigeot 	uint32_t pde;
748*19c468b4SFrançois Tigeot 
749*19c468b4SFrançois Tigeot 	gen8_for_each_pde(pt, pd, start, length, temp, pde) {
750*19c468b4SFrançois Tigeot 		/* Don't reallocate page tables */
751*19c468b4SFrançois Tigeot 		if (pt) {
752*19c468b4SFrançois Tigeot 			/* Scratch is never allocated this way */
753*19c468b4SFrançois Tigeot 			WARN_ON(pt == ppgtt->scratch_pt);
754*19c468b4SFrançois Tigeot 			continue;
755*19c468b4SFrançois Tigeot 		}
756*19c468b4SFrançois Tigeot 
757*19c468b4SFrançois Tigeot 		pt = alloc_pt_single(dev);
758*19c468b4SFrançois Tigeot 		if (IS_ERR(pt))
759ba55f2f5SFrançois Tigeot 			goto unwind_out;
760*19c468b4SFrançois Tigeot 
761*19c468b4SFrançois Tigeot 		gen8_initialize_pt(&ppgtt->base, pt);
762*19c468b4SFrançois Tigeot 		pd->page_table[pde] = pt;
763*19c468b4SFrançois Tigeot 		set_bit(pde, new_pts);
764ba55f2f5SFrançois Tigeot 	}
765ba55f2f5SFrançois Tigeot 
766ba55f2f5SFrançois Tigeot 	return 0;
767ba55f2f5SFrançois Tigeot 
768ba55f2f5SFrançois Tigeot unwind_out:
769*19c468b4SFrançois Tigeot 	for_each_set_bit(pde, new_pts, I915_PDES)
770*19c468b4SFrançois Tigeot 		unmap_and_free_pt(pd->page_table[pde], dev);
771ba55f2f5SFrançois Tigeot 
772ba55f2f5SFrançois Tigeot 	return -ENOMEM;
773ba55f2f5SFrançois Tigeot }
774ba55f2f5SFrançois Tigeot 
775*19c468b4SFrançois Tigeot /**
776*19c468b4SFrançois Tigeot  * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
777*19c468b4SFrançois Tigeot  * @ppgtt:	Master ppgtt structure.
778*19c468b4SFrançois Tigeot  * @pdp:	Page directory pointer for this address range.
779*19c468b4SFrançois Tigeot  * @start:	Starting virtual address to begin allocations.
780*19c468b4SFrançois Tigeot  * @length	Size of the allocations.
781*19c468b4SFrançois Tigeot  * @new_pds	Bitmap set by function with new allocations. Likely used by the
782*19c468b4SFrançois Tigeot  *		caller to free on error.
783*19c468b4SFrançois Tigeot  *
784*19c468b4SFrançois Tigeot  * Allocate the required number of page directories starting at the pde index of
785*19c468b4SFrançois Tigeot  * @start, and ending at the pde index @start + @length. This function will skip
786*19c468b4SFrançois Tigeot  * over already allocated page directories within the range, and only allocate
787*19c468b4SFrançois Tigeot  * new ones, setting the appropriate pointer within the pdp as well as the
788*19c468b4SFrançois Tigeot  * correct position in the bitmap @new_pds.
789*19c468b4SFrançois Tigeot  *
790*19c468b4SFrançois Tigeot  * The function will only allocate the pages within the range for a give page
791*19c468b4SFrançois Tigeot  * directory pointer. In other words, if @start + @length straddles a virtually
792*19c468b4SFrançois Tigeot  * addressed PDP boundary (512GB for 4k pages), there will be more allocations
793*19c468b4SFrançois Tigeot  * required by the caller, This is not currently possible, and the BUG in the
794*19c468b4SFrançois Tigeot  * code will prevent it.
795*19c468b4SFrançois Tigeot  *
796*19c468b4SFrançois Tigeot  * Return: 0 if success; negative error code otherwise.
797*19c468b4SFrançois Tigeot  */
798*19c468b4SFrançois Tigeot static int gen8_ppgtt_alloc_page_directories(struct i915_hw_ppgtt *ppgtt,
799*19c468b4SFrançois Tigeot 				     struct i915_page_directory_pointer *pdp,
800*19c468b4SFrançois Tigeot 				     uint64_t start,
801*19c468b4SFrançois Tigeot 				     uint64_t length,
802*19c468b4SFrançois Tigeot 				     unsigned long *new_pds)
803*19c468b4SFrançois Tigeot {
804*19c468b4SFrançois Tigeot 	struct drm_device *dev = ppgtt->base.dev;
805*19c468b4SFrançois Tigeot 	struct i915_page_directory *pd;
806*19c468b4SFrançois Tigeot 	uint64_t temp;
807*19c468b4SFrançois Tigeot 	uint32_t pdpe;
808*19c468b4SFrançois Tigeot 
809*19c468b4SFrançois Tigeot 	WARN_ON(!bitmap_empty(new_pds, GEN8_LEGACY_PDPES));
810*19c468b4SFrançois Tigeot 
811*19c468b4SFrançois Tigeot 	/* FIXME: upper bound must not overflow 32 bits  */
812*19c468b4SFrançois Tigeot 	WARN_ON((start + length) > (1ULL << 32));
813*19c468b4SFrançois Tigeot 
814*19c468b4SFrançois Tigeot 	gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
815*19c468b4SFrançois Tigeot 		if (pd)
816*19c468b4SFrançois Tigeot 			continue;
817*19c468b4SFrançois Tigeot 
818*19c468b4SFrançois Tigeot 		pd = alloc_pd_single(dev);
819*19c468b4SFrançois Tigeot 		if (IS_ERR(pd))
820*19c468b4SFrançois Tigeot 			goto unwind_out;
821*19c468b4SFrançois Tigeot 
822*19c468b4SFrançois Tigeot 		gen8_initialize_pd(&ppgtt->base, pd);
823*19c468b4SFrançois Tigeot 		pdp->page_directory[pdpe] = pd;
824*19c468b4SFrançois Tigeot 		set_bit(pdpe, new_pds);
825*19c468b4SFrançois Tigeot 	}
826*19c468b4SFrançois Tigeot 
827*19c468b4SFrançois Tigeot 	return 0;
828*19c468b4SFrançois Tigeot 
829*19c468b4SFrançois Tigeot unwind_out:
830*19c468b4SFrançois Tigeot 	for_each_set_bit(pdpe, new_pds, GEN8_LEGACY_PDPES)
831*19c468b4SFrançois Tigeot 		unmap_and_free_pd(pdp->page_directory[pdpe], dev);
832*19c468b4SFrançois Tigeot 
833*19c468b4SFrançois Tigeot 	return -ENOMEM;
834*19c468b4SFrançois Tigeot }
835*19c468b4SFrançois Tigeot 
836*19c468b4SFrançois Tigeot static void
837*19c468b4SFrançois Tigeot free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long **new_pts)
838ba55f2f5SFrançois Tigeot {
839477eb7f9SFrançois Tigeot 	int i;
8409edbd4a0SFrançois Tigeot 
841*19c468b4SFrançois Tigeot 	for (i = 0; i < GEN8_LEGACY_PDPES; i++)
842*19c468b4SFrançois Tigeot 		kfree(new_pts[i]);
843*19c468b4SFrançois Tigeot 	kfree(new_pts);
844*19c468b4SFrançois Tigeot 	kfree(new_pds);
845477eb7f9SFrançois Tigeot }
846477eb7f9SFrançois Tigeot 
847*19c468b4SFrançois Tigeot /* Fills in the page directory bitmap, and the array of page tables bitmap. Both
848*19c468b4SFrançois Tigeot  * of these are based on the number of PDPEs in the system.
849*19c468b4SFrançois Tigeot  */
850*19c468b4SFrançois Tigeot static
851*19c468b4SFrançois Tigeot int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
852*19c468b4SFrançois Tigeot 					 unsigned long ***new_pts)
853*19c468b4SFrançois Tigeot {
854*19c468b4SFrançois Tigeot 	int i;
855*19c468b4SFrançois Tigeot 	unsigned long *pds;
856*19c468b4SFrançois Tigeot 	unsigned long **pts;
8579edbd4a0SFrançois Tigeot 
858*19c468b4SFrançois Tigeot 	pds = kcalloc(BITS_TO_LONGS(GEN8_LEGACY_PDPES), sizeof(unsigned long), GFP_KERNEL);
859*19c468b4SFrançois Tigeot 	if (!pds)
860*19c468b4SFrançois Tigeot 		return -ENOMEM;
861477eb7f9SFrançois Tigeot 
862*19c468b4SFrançois Tigeot 	pts = kcalloc(GEN8_LEGACY_PDPES, sizeof(unsigned long *), GFP_KERNEL);
863*19c468b4SFrançois Tigeot 	if (!pts) {
864*19c468b4SFrançois Tigeot 		kfree(pds);
865477eb7f9SFrançois Tigeot 		return -ENOMEM;
866ba55f2f5SFrançois Tigeot 	}
867ba55f2f5SFrançois Tigeot 
868*19c468b4SFrançois Tigeot 	for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
869*19c468b4SFrançois Tigeot 		pts[i] = kcalloc(BITS_TO_LONGS(I915_PDES),
870*19c468b4SFrançois Tigeot 				 sizeof(unsigned long), GFP_KERNEL);
871*19c468b4SFrançois Tigeot 		if (!pts[i])
872477eb7f9SFrançois Tigeot 			goto err_out;
873*19c468b4SFrançois Tigeot 	}
874ba55f2f5SFrançois Tigeot 
875*19c468b4SFrançois Tigeot 	*new_pds = pds;
876*19c468b4SFrançois Tigeot 	*new_pts = pts;
877477eb7f9SFrançois Tigeot 
878477eb7f9SFrançois Tigeot 	return 0;
879477eb7f9SFrançois Tigeot 
880477eb7f9SFrançois Tigeot err_out:
881*19c468b4SFrançois Tigeot 	free_gen8_temp_bitmaps(pds, pts);
882*19c468b4SFrançois Tigeot 	return -ENOMEM;
883ba55f2f5SFrançois Tigeot }
884ba55f2f5SFrançois Tigeot 
885*19c468b4SFrançois Tigeot static int gen8_alloc_va_range(struct i915_address_space *vm,
886*19c468b4SFrançois Tigeot 			       uint64_t start,
887*19c468b4SFrançois Tigeot 			       uint64_t length)
888ba55f2f5SFrançois Tigeot {
889*19c468b4SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
890*19c468b4SFrançois Tigeot 		container_of(vm, struct i915_hw_ppgtt, base);
891*19c468b4SFrançois Tigeot 	unsigned long *new_page_dirs, **new_page_tables;
892*19c468b4SFrançois Tigeot 	struct i915_page_directory *pd;
893*19c468b4SFrançois Tigeot 	const uint64_t orig_start = start;
894*19c468b4SFrançois Tigeot 	const uint64_t orig_length = length;
895*19c468b4SFrançois Tigeot 	uint64_t temp;
896*19c468b4SFrançois Tigeot 	uint32_t pdpe;
897ba55f2f5SFrançois Tigeot 	int ret;
898ba55f2f5SFrançois Tigeot 
899*19c468b4SFrançois Tigeot 	/* Wrap is never okay since we can only represent 48b, and we don't
900*19c468b4SFrançois Tigeot 	 * actually use the other side of the canonical address space.
901*19c468b4SFrançois Tigeot 	 */
902*19c468b4SFrançois Tigeot 	if (WARN_ON(start + length < start))
903*19c468b4SFrançois Tigeot 		return -ERANGE;
904ba55f2f5SFrançois Tigeot 
905*19c468b4SFrançois Tigeot 	ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables);
906ba55f2f5SFrançois Tigeot 	if (ret)
907ba55f2f5SFrançois Tigeot 		return ret;
908ba55f2f5SFrançois Tigeot 
909*19c468b4SFrançois Tigeot 	/* Do the allocations first so we can easily bail out */
910*19c468b4SFrançois Tigeot 	ret = gen8_ppgtt_alloc_page_directories(ppgtt, &ppgtt->pdp, start, length,
911*19c468b4SFrançois Tigeot 					new_page_dirs);
912*19c468b4SFrançois Tigeot 	if (ret) {
913*19c468b4SFrançois Tigeot 		free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
914*19c468b4SFrançois Tigeot 		return ret;
915*19c468b4SFrançois Tigeot 	}
916*19c468b4SFrançois Tigeot 
917*19c468b4SFrançois Tigeot 	/* For every page directory referenced, allocate page tables */
918*19c468b4SFrançois Tigeot 	gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
919*19c468b4SFrançois Tigeot 		ret = gen8_ppgtt_alloc_pagetabs(ppgtt, pd, start, length,
920*19c468b4SFrançois Tigeot 						new_page_tables[pdpe]);
921*19c468b4SFrançois Tigeot 		if (ret)
922*19c468b4SFrançois Tigeot 			goto err_out;
923*19c468b4SFrançois Tigeot 	}
924*19c468b4SFrançois Tigeot 
925*19c468b4SFrançois Tigeot 	start = orig_start;
926*19c468b4SFrançois Tigeot 	length = orig_length;
927*19c468b4SFrançois Tigeot 
928*19c468b4SFrançois Tigeot 	/* Allocations have completed successfully, so set the bitmaps, and do
929*19c468b4SFrançois Tigeot 	 * the mappings. */
930*19c468b4SFrançois Tigeot 	gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
931*19c468b4SFrançois Tigeot 		gen8_pde_t *const page_directory = kmap_atomic(pd->page);
932*19c468b4SFrançois Tigeot 		struct i915_page_table *pt;
933*19c468b4SFrançois Tigeot 		uint64_t pd_len = gen8_clamp_pd(start, length);
934*19c468b4SFrançois Tigeot 		uint64_t pd_start = start;
935*19c468b4SFrançois Tigeot 		uint32_t pde;
936*19c468b4SFrançois Tigeot 
937*19c468b4SFrançois Tigeot 		/* Every pd should be allocated, we just did that above. */
938*19c468b4SFrançois Tigeot 		WARN_ON(!pd);
939*19c468b4SFrançois Tigeot 
940*19c468b4SFrançois Tigeot 		gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) {
941*19c468b4SFrançois Tigeot 			/* Same reasoning as pd */
942*19c468b4SFrançois Tigeot 			WARN_ON(!pt);
943*19c468b4SFrançois Tigeot 			WARN_ON(!pd_len);
944*19c468b4SFrançois Tigeot 			WARN_ON(!gen8_pte_count(pd_start, pd_len));
945*19c468b4SFrançois Tigeot 
946*19c468b4SFrançois Tigeot 			/* Set our used ptes within the page table */
947*19c468b4SFrançois Tigeot 			bitmap_set(pt->used_ptes,
948*19c468b4SFrançois Tigeot 				   gen8_pte_index(pd_start),
949*19c468b4SFrançois Tigeot 				   gen8_pte_count(pd_start, pd_len));
950*19c468b4SFrançois Tigeot 
951*19c468b4SFrançois Tigeot 			/* Our pde is now pointing to the pagetable, pt */
952*19c468b4SFrançois Tigeot 			set_bit(pde, pd->used_pdes);
953*19c468b4SFrançois Tigeot 
954*19c468b4SFrançois Tigeot 			/* Map the PDE to the page table */
955*19c468b4SFrançois Tigeot 			__gen8_do_map_pt(page_directory + pde, pt, vm->dev);
956*19c468b4SFrançois Tigeot 
957*19c468b4SFrançois Tigeot 			/* NB: We haven't yet mapped ptes to pages. At this
958*19c468b4SFrançois Tigeot 			 * point we're still relying on insert_entries() */
959*19c468b4SFrançois Tigeot 		}
960*19c468b4SFrançois Tigeot 
961*19c468b4SFrançois Tigeot 		if (!HAS_LLC(vm->dev))
962*19c468b4SFrançois Tigeot 			drm_clflush_virt_range(page_directory, PAGE_SIZE);
963*19c468b4SFrançois Tigeot 
964*19c468b4SFrançois Tigeot 		kunmap_atomic(page_directory);
965*19c468b4SFrançois Tigeot 
966*19c468b4SFrançois Tigeot 		set_bit(pdpe, ppgtt->pdp.used_pdpes);
967*19c468b4SFrançois Tigeot 	}
968*19c468b4SFrançois Tigeot 
969*19c468b4SFrançois Tigeot 	free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
970*19c468b4SFrançois Tigeot 	return 0;
971*19c468b4SFrançois Tigeot 
972*19c468b4SFrançois Tigeot err_out:
973*19c468b4SFrançois Tigeot 	while (pdpe--) {
974*19c468b4SFrançois Tigeot 		for_each_set_bit(temp, new_page_tables[pdpe], I915_PDES)
975*19c468b4SFrançois Tigeot 			unmap_and_free_pt(ppgtt->pdp.page_directory[pdpe]->page_table[temp], vm->dev);
976*19c468b4SFrançois Tigeot 	}
977*19c468b4SFrançois Tigeot 
978*19c468b4SFrançois Tigeot 	for_each_set_bit(pdpe, new_page_dirs, GEN8_LEGACY_PDPES)
979*19c468b4SFrançois Tigeot 		unmap_and_free_pd(ppgtt->pdp.page_directory[pdpe], vm->dev);
980*19c468b4SFrançois Tigeot 
981*19c468b4SFrançois Tigeot 	free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
982*19c468b4SFrançois Tigeot 	return ret;
983*19c468b4SFrançois Tigeot }
984*19c468b4SFrançois Tigeot 
985*19c468b4SFrançois Tigeot /*
986*19c468b4SFrançois Tigeot  * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
987*19c468b4SFrançois Tigeot  * with a net effect resembling a 2-level page table in normal x86 terms. Each
988*19c468b4SFrançois Tigeot  * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
989*19c468b4SFrançois Tigeot  * space.
990*19c468b4SFrançois Tigeot  *
991*19c468b4SFrançois Tigeot  */
992*19c468b4SFrançois Tigeot static int gen8_ppgtt_init_common(struct i915_hw_ppgtt *ppgtt, uint64_t size)
993*19c468b4SFrançois Tigeot {
994*19c468b4SFrançois Tigeot 	ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
995*19c468b4SFrançois Tigeot 	if (IS_ERR(ppgtt->scratch_pt))
996*19c468b4SFrançois Tigeot 		return PTR_ERR(ppgtt->scratch_pt);
997*19c468b4SFrançois Tigeot 
998*19c468b4SFrançois Tigeot 	ppgtt->scratch_pd = alloc_pd_single(ppgtt->base.dev);
999*19c468b4SFrançois Tigeot 	if (IS_ERR(ppgtt->scratch_pd))
1000*19c468b4SFrançois Tigeot 		return PTR_ERR(ppgtt->scratch_pd);
1001*19c468b4SFrançois Tigeot 
1002*19c468b4SFrançois Tigeot 	gen8_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
1003*19c468b4SFrançois Tigeot 	gen8_initialize_pd(&ppgtt->base, ppgtt->scratch_pd);
1004*19c468b4SFrançois Tigeot 
1005*19c468b4SFrançois Tigeot 	ppgtt->base.start = 0;
1006*19c468b4SFrançois Tigeot 	ppgtt->base.total = size;
1007*19c468b4SFrançois Tigeot 	ppgtt->base.cleanup = gen8_ppgtt_cleanup;
1008*19c468b4SFrançois Tigeot 	ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
1009*19c468b4SFrançois Tigeot 	ppgtt->base.clear_range = gen8_ppgtt_clear_range;
1010*19c468b4SFrançois Tigeot 	ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1011*19c468b4SFrançois Tigeot 	ppgtt->base.bind_vma = ppgtt_bind_vma;
1012*19c468b4SFrançois Tigeot 
1013*19c468b4SFrançois Tigeot 	ppgtt->switch_mm = gen8_mm_switch;
1014ba55f2f5SFrançois Tigeot 
1015ba55f2f5SFrançois Tigeot 	return 0;
1016ba55f2f5SFrançois Tigeot }
1017ba55f2f5SFrançois Tigeot 
1018*19c468b4SFrançois Tigeot static int gen8_aliasing_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1019ba55f2f5SFrançois Tigeot {
1020*19c468b4SFrançois Tigeot 	struct drm_device *dev = ppgtt->base.dev;
1021*19c468b4SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1022*19c468b4SFrançois Tigeot 	uint64_t start = 0, size = dev_priv->gtt.base.total;
1023ba55f2f5SFrançois Tigeot 	int ret;
1024ba55f2f5SFrançois Tigeot 
1025*19c468b4SFrançois Tigeot 	ret = gen8_ppgtt_init_common(ppgtt, dev_priv->gtt.base.total);
1026ba55f2f5SFrançois Tigeot 	if (ret)
1027ba55f2f5SFrançois Tigeot 		return ret;
1028ba55f2f5SFrançois Tigeot 
1029*19c468b4SFrançois Tigeot 	/* Aliasing PPGTT has to always work and be mapped because of the way we
1030*19c468b4SFrançois Tigeot 	 * use RESTORE_INHIBIT in the context switch. This will be fixed
1031*19c468b4SFrançois Tigeot 	 * eventually. */
1032*19c468b4SFrançois Tigeot 	ret = gen8_alloc_va_range(&ppgtt->base, start, size);
1033*19c468b4SFrançois Tigeot 	if (ret) {
1034*19c468b4SFrançois Tigeot 		unmap_and_free_pd(ppgtt->scratch_pd, ppgtt->base.dev);
1035*19c468b4SFrançois Tigeot 		unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
1036*19c468b4SFrançois Tigeot 		return ret;
1037*19c468b4SFrançois Tigeot 	}
1038*19c468b4SFrançois Tigeot 
1039*19c468b4SFrançois Tigeot 	ppgtt->base.allocate_va_range = NULL;
1040*19c468b4SFrançois Tigeot 	ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
1041ba55f2f5SFrançois Tigeot 
1042ba55f2f5SFrançois Tigeot 	return 0;
1043ba55f2f5SFrançois Tigeot }
1044ba55f2f5SFrançois Tigeot 
1045477eb7f9SFrançois Tigeot /*
1046ba55f2f5SFrançois Tigeot  * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1047ba55f2f5SFrançois Tigeot  * with a net effect resembling a 2-level page table in normal x86 terms. Each
1048ba55f2f5SFrançois Tigeot  * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1049ba55f2f5SFrançois Tigeot  * space.
1050ba55f2f5SFrançois Tigeot  *
1051ba55f2f5SFrançois Tigeot  */
1052*19c468b4SFrançois Tigeot static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1053ba55f2f5SFrançois Tigeot {
1054*19c468b4SFrançois Tigeot 	ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
1055*19c468b4SFrançois Tigeot 	if (IS_ERR(ppgtt->scratch_pt))
1056*19c468b4SFrançois Tigeot 		return PTR_ERR(ppgtt->scratch_pt);
1057ba55f2f5SFrançois Tigeot 
1058*19c468b4SFrançois Tigeot 	ppgtt->scratch_pd = alloc_pd_single(ppgtt->base.dev);
1059*19c468b4SFrançois Tigeot 	if (IS_ERR(ppgtt->scratch_pd))
1060*19c468b4SFrançois Tigeot 		return PTR_ERR(ppgtt->scratch_pd);
1061ba55f2f5SFrançois Tigeot 
1062*19c468b4SFrançois Tigeot 	gen8_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
1063*19c468b4SFrançois Tigeot 	gen8_initialize_pd(&ppgtt->base, ppgtt->scratch_pd);
1064*19c468b4SFrançois Tigeot 
1065*19c468b4SFrançois Tigeot 	ppgtt->base.start = 0;
1066*19c468b4SFrançois Tigeot 	ppgtt->base.total = 1ULL << 32;
1067*19c468b4SFrançois Tigeot #define CONFIG_X86_32 0
1068*19c468b4SFrançois Tigeot 	if (IS_ENABLED(CONFIG_X86_32))
1069*19c468b4SFrançois Tigeot 		/* While we have a proliferation of size_t variables
1070*19c468b4SFrançois Tigeot 		 * we cannot represent the full ppgtt size on 32bit,
1071*19c468b4SFrançois Tigeot 		 * so limit it to the same size as the GGTT (currently
1072*19c468b4SFrançois Tigeot 		 * 2GiB).
1073477eb7f9SFrançois Tigeot 		 */
1074*19c468b4SFrançois Tigeot 		ppgtt->base.total = to_i915(ppgtt->base.dev)->gtt.base.total;
1075*19c468b4SFrançois Tigeot 	ppgtt->base.cleanup = gen8_ppgtt_cleanup;
1076*19c468b4SFrançois Tigeot 	ppgtt->base.allocate_va_range = gen8_alloc_va_range;
1077*19c468b4SFrançois Tigeot 	ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
1078*19c468b4SFrançois Tigeot 	ppgtt->base.clear_range = gen8_ppgtt_clear_range;
1079*19c468b4SFrançois Tigeot 	ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1080*19c468b4SFrançois Tigeot 	ppgtt->base.bind_vma = ppgtt_bind_vma;
10819edbd4a0SFrançois Tigeot 
1082ba55f2f5SFrançois Tigeot 	ppgtt->switch_mm = gen8_mm_switch;
1083ba55f2f5SFrançois Tigeot 
10849edbd4a0SFrançois Tigeot 	return 0;
10859edbd4a0SFrançois Tigeot }
10869edbd4a0SFrançois Tigeot 
1087ba55f2f5SFrançois Tigeot static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1088ba55f2f5SFrançois Tigeot {
1089ba55f2f5SFrançois Tigeot 	struct i915_address_space *vm = &ppgtt->base;
1090*19c468b4SFrançois Tigeot 	struct i915_page_table *unused;
1091477eb7f9SFrançois Tigeot 	gen6_pte_t scratch_pte;
1092ba55f2f5SFrançois Tigeot 	uint32_t pd_entry;
1093*19c468b4SFrançois Tigeot 	uint32_t  pte, pde, temp;
1094*19c468b4SFrançois Tigeot 	uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
1095ba55f2f5SFrançois Tigeot 
109624edb884SFrançois Tigeot 	scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
1097ba55f2f5SFrançois Tigeot 
1098*19c468b4SFrançois Tigeot 	gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) {
1099ba55f2f5SFrançois Tigeot 		u32 expected;
1100477eb7f9SFrançois Tigeot 		gen6_pte_t *pt_vaddr;
1101477eb7f9SFrançois Tigeot 		dma_addr_t pt_addr = ppgtt->pd.page_table[pde]->daddr;
1102*19c468b4SFrançois Tigeot 		pd_entry = readl(ppgtt->pd_addr + pde);
1103ba55f2f5SFrançois Tigeot 		expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1104ba55f2f5SFrançois Tigeot 
1105ba55f2f5SFrançois Tigeot 		if (pd_entry != expected)
1106ba55f2f5SFrançois Tigeot 			seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1107ba55f2f5SFrançois Tigeot 				   pde,
1108ba55f2f5SFrançois Tigeot 				   pd_entry,
1109ba55f2f5SFrançois Tigeot 				   expected);
1110ba55f2f5SFrançois Tigeot 		seq_printf(m, "\tPDE: %x\n", pd_entry);
1111ba55f2f5SFrançois Tigeot 
1112477eb7f9SFrançois Tigeot 		pt_vaddr = kmap_atomic(ppgtt->pd.page_table[pde]->page);
1113477eb7f9SFrançois Tigeot 		for (pte = 0; pte < GEN6_PTES; pte+=4) {
1114ba55f2f5SFrançois Tigeot 			unsigned long va =
1115477eb7f9SFrançois Tigeot 				(pde * PAGE_SIZE * GEN6_PTES) +
1116ba55f2f5SFrançois Tigeot 				(pte * PAGE_SIZE);
1117ba55f2f5SFrançois Tigeot 			int i;
1118ba55f2f5SFrançois Tigeot 			bool found = false;
1119ba55f2f5SFrançois Tigeot 			for (i = 0; i < 4; i++)
1120ba55f2f5SFrançois Tigeot 				if (pt_vaddr[pte + i] != scratch_pte)
1121ba55f2f5SFrançois Tigeot 					found = true;
1122ba55f2f5SFrançois Tigeot 			if (!found)
1123ba55f2f5SFrançois Tigeot 				continue;
1124ba55f2f5SFrançois Tigeot 
1125ba55f2f5SFrançois Tigeot 			seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1126ba55f2f5SFrançois Tigeot 			for (i = 0; i < 4; i++) {
1127ba55f2f5SFrançois Tigeot 				if (pt_vaddr[pte + i] != scratch_pte)
1128ba55f2f5SFrançois Tigeot 					seq_printf(m, " %08x", pt_vaddr[pte + i]);
1129ba55f2f5SFrançois Tigeot 				else
1130477eb7f9SFrançois Tigeot 					seq_puts(m, "  SCRATCH ");
1131ba55f2f5SFrançois Tigeot 			}
1132477eb7f9SFrançois Tigeot 			seq_puts(m, "\n");
1133ba55f2f5SFrançois Tigeot 		}
1134ba55f2f5SFrançois Tigeot 		kunmap_atomic(pt_vaddr);
1135ba55f2f5SFrançois Tigeot 	}
1136ba55f2f5SFrançois Tigeot }
1137ba55f2f5SFrançois Tigeot 
1138477eb7f9SFrançois Tigeot /* Write pde (index) from the page directory @pd to the page table @pt */
1139*19c468b4SFrançois Tigeot static void gen6_write_pde(struct i915_page_directory *pd,
1140*19c468b4SFrançois Tigeot 			    const int pde, struct i915_page_table *pt)
11419edbd4a0SFrançois Tigeot {
1142477eb7f9SFrançois Tigeot 	/* Caller needs to make sure the write completes if necessary */
1143477eb7f9SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
1144477eb7f9SFrançois Tigeot 		container_of(pd, struct i915_hw_ppgtt, pd);
1145477eb7f9SFrançois Tigeot 	u32 pd_entry;
11469edbd4a0SFrançois Tigeot 
1147477eb7f9SFrançois Tigeot 	pd_entry = GEN6_PDE_ADDR_ENCODE(pt->daddr);
11489edbd4a0SFrançois Tigeot 	pd_entry |= GEN6_PDE_VALID;
11499edbd4a0SFrançois Tigeot 
1150477eb7f9SFrançois Tigeot 	writel(pd_entry, ppgtt->pd_addr + pde);
11519edbd4a0SFrançois Tigeot }
1152477eb7f9SFrançois Tigeot 
1153477eb7f9SFrançois Tigeot /* Write all the page tables found in the ppgtt structure to incrementing page
1154477eb7f9SFrançois Tigeot  * directories. */
1155477eb7f9SFrançois Tigeot static void gen6_write_page_range(struct drm_i915_private *dev_priv,
1156*19c468b4SFrançois Tigeot 				  struct i915_page_directory *pd,
1157477eb7f9SFrançois Tigeot 				  uint32_t start, uint32_t length)
1158477eb7f9SFrançois Tigeot {
1159*19c468b4SFrançois Tigeot 	struct i915_page_table *pt;
1160477eb7f9SFrançois Tigeot 	uint32_t pde, temp;
1161477eb7f9SFrançois Tigeot 
1162477eb7f9SFrançois Tigeot 	gen6_for_each_pde(pt, pd, start, length, temp, pde)
1163477eb7f9SFrançois Tigeot 		gen6_write_pde(pd, pde, pt);
1164477eb7f9SFrançois Tigeot 
1165477eb7f9SFrançois Tigeot 	/* Make sure write is complete before other code can use this page
1166477eb7f9SFrançois Tigeot 	 * table. Also require for WC mapped PTEs */
1167477eb7f9SFrançois Tigeot 	readl(dev_priv->gtt.gsm);
11689edbd4a0SFrançois Tigeot }
11699edbd4a0SFrançois Tigeot 
1170ba55f2f5SFrançois Tigeot static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
11718e26cdf6SFrançois Tigeot {
1172477eb7f9SFrançois Tigeot 	BUG_ON(ppgtt->pd.pd_offset & 0x3f);
11735d0b1887SFrançois Tigeot 
1174477eb7f9SFrançois Tigeot 	return (ppgtt->pd.pd_offset / 64) << 16;
1175ba55f2f5SFrançois Tigeot }
11768e26cdf6SFrançois Tigeot 
1177ba55f2f5SFrançois Tigeot static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
11781b13d190SFrançois Tigeot 			 struct intel_engine_cs *ring)
1179ba55f2f5SFrançois Tigeot {
1180ba55f2f5SFrançois Tigeot 	int ret;
11818e26cdf6SFrançois Tigeot 
1182ba55f2f5SFrançois Tigeot 	/* NB: TLBs must be flushed and invalidated before a switch */
1183ba55f2f5SFrançois Tigeot 	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1184ba55f2f5SFrançois Tigeot 	if (ret)
1185ba55f2f5SFrançois Tigeot 		return ret;
11868e26cdf6SFrançois Tigeot 
1187ba55f2f5SFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
1188ba55f2f5SFrançois Tigeot 	if (ret)
1189ba55f2f5SFrançois Tigeot 		return ret;
11908e26cdf6SFrançois Tigeot 
1191ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1192ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1193ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, PP_DIR_DCLV_2G);
1194ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1195ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, get_pd_offset(ppgtt));
1196ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1197ba55f2f5SFrançois Tigeot 	intel_ring_advance(ring);
1198ba55f2f5SFrançois Tigeot 
1199ba55f2f5SFrançois Tigeot 	return 0;
1200ba55f2f5SFrançois Tigeot }
1201ba55f2f5SFrançois Tigeot 
1202477eb7f9SFrançois Tigeot static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
1203477eb7f9SFrançois Tigeot 			  struct intel_engine_cs *ring)
1204477eb7f9SFrançois Tigeot {
1205477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1206477eb7f9SFrançois Tigeot 
1207477eb7f9SFrançois Tigeot 	I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1208477eb7f9SFrançois Tigeot 	I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1209477eb7f9SFrançois Tigeot 	return 0;
1210477eb7f9SFrançois Tigeot }
1211477eb7f9SFrançois Tigeot 
1212ba55f2f5SFrançois Tigeot static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
12131b13d190SFrançois Tigeot 			  struct intel_engine_cs *ring)
1214ba55f2f5SFrançois Tigeot {
1215ba55f2f5SFrançois Tigeot 	int ret;
1216ba55f2f5SFrançois Tigeot 
1217ba55f2f5SFrançois Tigeot 	/* NB: TLBs must be flushed and invalidated before a switch */
1218ba55f2f5SFrançois Tigeot 	ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1219ba55f2f5SFrançois Tigeot 	if (ret)
1220ba55f2f5SFrançois Tigeot 		return ret;
1221ba55f2f5SFrançois Tigeot 
1222ba55f2f5SFrançois Tigeot 	ret = intel_ring_begin(ring, 6);
1223ba55f2f5SFrançois Tigeot 	if (ret)
1224ba55f2f5SFrançois Tigeot 		return ret;
1225ba55f2f5SFrançois Tigeot 
1226ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1227ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1228ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, PP_DIR_DCLV_2G);
1229ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1230ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, get_pd_offset(ppgtt));
1231ba55f2f5SFrançois Tigeot 	intel_ring_emit(ring, MI_NOOP);
1232ba55f2f5SFrançois Tigeot 	intel_ring_advance(ring);
1233ba55f2f5SFrançois Tigeot 
1234ba55f2f5SFrançois Tigeot 	/* XXX: RCS is the only one to auto invalidate the TLBs? */
1235ba55f2f5SFrançois Tigeot 	if (ring->id != RCS) {
1236ba55f2f5SFrançois Tigeot 		ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1237ba55f2f5SFrançois Tigeot 		if (ret)
1238ba55f2f5SFrançois Tigeot 			return ret;
1239ba55f2f5SFrançois Tigeot 	}
1240ba55f2f5SFrançois Tigeot 
1241ba55f2f5SFrançois Tigeot 	return 0;
1242ba55f2f5SFrançois Tigeot }
1243ba55f2f5SFrançois Tigeot 
1244ba55f2f5SFrançois Tigeot static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
12451b13d190SFrançois Tigeot 			  struct intel_engine_cs *ring)
1246ba55f2f5SFrançois Tigeot {
1247ba55f2f5SFrançois Tigeot 	struct drm_device *dev = ppgtt->base.dev;
1248ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1249ba55f2f5SFrançois Tigeot 
1250ba55f2f5SFrançois Tigeot 
1251ba55f2f5SFrançois Tigeot 	I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1252ba55f2f5SFrançois Tigeot 	I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1253ba55f2f5SFrançois Tigeot 
1254ba55f2f5SFrançois Tigeot 	POSTING_READ(RING_PP_DIR_DCLV(ring));
1255ba55f2f5SFrançois Tigeot 
1256ba55f2f5SFrançois Tigeot 	return 0;
1257ba55f2f5SFrançois Tigeot }
1258ba55f2f5SFrançois Tigeot 
12591b13d190SFrançois Tigeot static void gen8_ppgtt_enable(struct drm_device *dev)
1260ba55f2f5SFrançois Tigeot {
1261ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1262ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring;
12631b13d190SFrançois Tigeot 	int j;
1264ba55f2f5SFrançois Tigeot 
1265ba55f2f5SFrançois Tigeot 	for_each_ring(ring, dev_priv, j) {
1266ba55f2f5SFrançois Tigeot 		I915_WRITE(RING_MODE_GEN7(ring),
1267ba55f2f5SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
12681b13d190SFrançois Tigeot 	}
1269ba55f2f5SFrançois Tigeot }
1270ba55f2f5SFrançois Tigeot 
12711b13d190SFrançois Tigeot static void gen7_ppgtt_enable(struct drm_device *dev)
1272ba55f2f5SFrançois Tigeot {
1273ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1274ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring;
12758e26cdf6SFrançois Tigeot 	uint32_t ecochk, ecobits;
1276ba55f2f5SFrançois Tigeot 	int i;
12778e26cdf6SFrançois Tigeot 
12788e26cdf6SFrançois Tigeot 	ecobits = I915_READ(GAC_ECO_BITS);
12798e26cdf6SFrançois Tigeot 	I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
12808e26cdf6SFrançois Tigeot 
12818e26cdf6SFrançois Tigeot 	ecochk = I915_READ(GAM_ECOCHK);
12828e26cdf6SFrançois Tigeot 	if (IS_HASWELL(dev)) {
12838e26cdf6SFrançois Tigeot 		ecochk |= ECOCHK_PPGTT_WB_HSW;
12848e26cdf6SFrançois Tigeot 	} else {
12858e26cdf6SFrançois Tigeot 		ecochk |= ECOCHK_PPGTT_LLC_IVB;
12868e26cdf6SFrançois Tigeot 		ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
12878e26cdf6SFrançois Tigeot 	}
12888e26cdf6SFrançois Tigeot 	I915_WRITE(GAM_ECOCHK, ecochk);
12898e26cdf6SFrançois Tigeot 
12908e26cdf6SFrançois Tigeot 	for_each_ring(ring, dev_priv, i) {
1291ba55f2f5SFrançois Tigeot 		/* GFX_MODE is per-ring on gen7+ */
12928e26cdf6SFrançois Tigeot 		I915_WRITE(RING_MODE_GEN7(ring),
12938e26cdf6SFrançois Tigeot 			   _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
12941b13d190SFrançois Tigeot 	}
12958e26cdf6SFrançois Tigeot }
1296ba55f2f5SFrançois Tigeot 
12971b13d190SFrançois Tigeot static void gen6_ppgtt_enable(struct drm_device *dev)
1298ba55f2f5SFrançois Tigeot {
1299ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1300ba55f2f5SFrançois Tigeot 	uint32_t ecochk, gab_ctl, ecobits;
1301ba55f2f5SFrançois Tigeot 
1302ba55f2f5SFrançois Tigeot 	ecobits = I915_READ(GAC_ECO_BITS);
1303ba55f2f5SFrançois Tigeot 	I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1304ba55f2f5SFrançois Tigeot 		   ECOBITS_PPGTT_CACHE64B);
1305ba55f2f5SFrançois Tigeot 
1306ba55f2f5SFrançois Tigeot 	gab_ctl = I915_READ(GAB_CTL);
1307ba55f2f5SFrançois Tigeot 	I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
1308ba55f2f5SFrançois Tigeot 
1309ba55f2f5SFrançois Tigeot 	ecochk = I915_READ(GAM_ECOCHK);
1310ba55f2f5SFrançois Tigeot 	I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1311ba55f2f5SFrançois Tigeot 
1312ba55f2f5SFrançois Tigeot 	I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
13138e26cdf6SFrançois Tigeot }
13148e26cdf6SFrançois Tigeot 
1315f4e1c372SFrançois Tigeot /* PPGTT support for Sandybdrige/Gen6 and later */
13169edbd4a0SFrançois Tigeot static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
1317ba55f2f5SFrançois Tigeot 				   uint64_t start,
1318ba55f2f5SFrançois Tigeot 				   uint64_t length,
13199edbd4a0SFrançois Tigeot 				   bool use_scratch)
1320f4e1c372SFrançois Tigeot {
13219edbd4a0SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
13229edbd4a0SFrançois Tigeot 		container_of(vm, struct i915_hw_ppgtt, base);
1323477eb7f9SFrançois Tigeot 	gen6_pte_t *pt_vaddr, scratch_pte;
1324ba55f2f5SFrançois Tigeot 	unsigned first_entry = start >> PAGE_SHIFT;
1325ba55f2f5SFrançois Tigeot 	unsigned num_entries = length >> PAGE_SHIFT;
1326477eb7f9SFrançois Tigeot 	unsigned act_pt = first_entry / GEN6_PTES;
1327477eb7f9SFrançois Tigeot 	unsigned first_pte = first_entry % GEN6_PTES;
1328f4e1c372SFrançois Tigeot 	unsigned last_pte, i;
1329e3adcf8fSFrançois Tigeot 
133024edb884SFrançois Tigeot 	scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
1331e3adcf8fSFrançois Tigeot 
1332e3adcf8fSFrançois Tigeot 	while (num_entries) {
1333e3adcf8fSFrançois Tigeot 		last_pte = first_pte + num_entries;
1334477eb7f9SFrançois Tigeot 		if (last_pte > GEN6_PTES)
1335477eb7f9SFrançois Tigeot 			last_pte = GEN6_PTES;
1336e3adcf8fSFrançois Tigeot 
1337477eb7f9SFrançois Tigeot 		pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
1338e3adcf8fSFrançois Tigeot 
1339e3adcf8fSFrançois Tigeot 		for (i = first_pte; i < last_pte; i++)
1340e3adcf8fSFrançois Tigeot 			pt_vaddr[i] = scratch_pte;
1341e3adcf8fSFrançois Tigeot 
134282046b5cSFrançois Tigeot 		kunmap_atomic(pt_vaddr);
1343e3adcf8fSFrançois Tigeot 
1344e3adcf8fSFrançois Tigeot 		num_entries -= last_pte - first_pte;
1345e3adcf8fSFrançois Tigeot 		first_pte = 0;
13468e26cdf6SFrançois Tigeot 		act_pt++;
1347e3adcf8fSFrançois Tigeot 	}
1348e3adcf8fSFrançois Tigeot }
1349e3adcf8fSFrançois Tigeot 
13509edbd4a0SFrançois Tigeot static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
13519edbd4a0SFrançois Tigeot 				      vm_page_t *pages,
1352ba55f2f5SFrançois Tigeot 				      uint64_t start,
13539edbd4a0SFrançois Tigeot 				      unsigned num_entries,
135424edb884SFrançois Tigeot 				      enum i915_cache_level cache_level, u32 flags)
1355e3adcf8fSFrançois Tigeot {
13569edbd4a0SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
13579edbd4a0SFrançois Tigeot 		container_of(vm, struct i915_hw_ppgtt, base);
1358477eb7f9SFrançois Tigeot 	gen6_pte_t *pt_vaddr;
1359ba55f2f5SFrançois Tigeot 	unsigned first_entry = start >> PAGE_SHIFT;
1360477eb7f9SFrançois Tigeot 	unsigned act_pt = first_entry / GEN6_PTES;
1361477eb7f9SFrançois Tigeot 	unsigned act_pte = first_entry % GEN6_PTES;
1362a2fdbec6SFrançois Tigeot 
13639edbd4a0SFrançois Tigeot 	pt_vaddr = NULL;
13649edbd4a0SFrançois Tigeot 	for (int i=0;i<num_entries;i++) {
13659edbd4a0SFrançois Tigeot 		if (pt_vaddr == NULL)
1366477eb7f9SFrançois Tigeot 			pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
1367a2fdbec6SFrançois Tigeot 
13689edbd4a0SFrançois Tigeot 		pt_vaddr[act_pte] =
13699edbd4a0SFrançois Tigeot 			vm->pte_encode(VM_PAGE_TO_PHYS(pages[i]),
137024edb884SFrançois Tigeot 				       cache_level, true, flags);
1371477eb7f9SFrançois Tigeot 
1372477eb7f9SFrançois Tigeot 		if (++act_pte == GEN6_PTES) {
1373a2fdbec6SFrançois Tigeot 			kunmap_atomic(pt_vaddr);
13749edbd4a0SFrançois Tigeot 			pt_vaddr = NULL;
13758e26cdf6SFrançois Tigeot 			act_pt++;
13769edbd4a0SFrançois Tigeot 			act_pte = 0;
1377a2fdbec6SFrançois Tigeot 		}
1378a2fdbec6SFrançois Tigeot 	}
13799edbd4a0SFrançois Tigeot 	if (pt_vaddr)
13809edbd4a0SFrançois Tigeot 		kunmap_atomic(pt_vaddr);
13819edbd4a0SFrançois Tigeot }
1382a2fdbec6SFrançois Tigeot 
1383477eb7f9SFrançois Tigeot /* PDE TLBs are a pain invalidate pre GEN8. It requires a context reload. If we
1384477eb7f9SFrançois Tigeot  * are switching between contexts with the same LRCA, we also must do a force
1385477eb7f9SFrançois Tigeot  * restore.
1386477eb7f9SFrançois Tigeot  */
1387*19c468b4SFrançois Tigeot static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1388a2fdbec6SFrançois Tigeot {
1389477eb7f9SFrançois Tigeot 	/* If current vm != vm, */
1390477eb7f9SFrançois Tigeot 	ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1391477eb7f9SFrançois Tigeot }
1392477eb7f9SFrançois Tigeot 
1393477eb7f9SFrançois Tigeot static void gen6_initialize_pt(struct i915_address_space *vm,
1394*19c468b4SFrançois Tigeot 		struct i915_page_table *pt)
1395477eb7f9SFrançois Tigeot {
1396477eb7f9SFrançois Tigeot 	gen6_pte_t *pt_vaddr, scratch_pte;
1397a2fdbec6SFrançois Tigeot 	int i;
1398a2fdbec6SFrançois Tigeot 
1399477eb7f9SFrançois Tigeot 	WARN_ON(vm->scratch.addr == 0);
1400477eb7f9SFrançois Tigeot 
1401477eb7f9SFrançois Tigeot 	scratch_pte = vm->pte_encode(vm->scratch.addr,
1402477eb7f9SFrançois Tigeot 			I915_CACHE_LLC, true, 0);
1403477eb7f9SFrançois Tigeot 
1404477eb7f9SFrançois Tigeot 	pt_vaddr = kmap_atomic(pt->page);
1405477eb7f9SFrançois Tigeot 
1406477eb7f9SFrançois Tigeot 	for (i = 0; i < GEN6_PTES; i++)
1407477eb7f9SFrançois Tigeot 		pt_vaddr[i] = scratch_pte;
1408477eb7f9SFrançois Tigeot 
1409477eb7f9SFrançois Tigeot 	kunmap_atomic(pt_vaddr);
1410a2fdbec6SFrançois Tigeot }
1411477eb7f9SFrançois Tigeot 
1412477eb7f9SFrançois Tigeot static int gen6_alloc_va_range(struct i915_address_space *vm,
1413477eb7f9SFrançois Tigeot 			       uint64_t start, uint64_t length)
1414477eb7f9SFrançois Tigeot {
1415477eb7f9SFrançois Tigeot 	DECLARE_BITMAP(new_page_tables, I915_PDES);
1416477eb7f9SFrançois Tigeot 	struct drm_device *dev = vm->dev;
1417477eb7f9SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1418477eb7f9SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
1419477eb7f9SFrançois Tigeot 				container_of(vm, struct i915_hw_ppgtt, base);
1420*19c468b4SFrançois Tigeot 	struct i915_page_table *pt;
1421477eb7f9SFrançois Tigeot 	const uint32_t start_save = start, length_save = length;
1422477eb7f9SFrançois Tigeot 	uint32_t pde, temp;
1423477eb7f9SFrançois Tigeot 	int ret;
1424477eb7f9SFrançois Tigeot 
1425477eb7f9SFrançois Tigeot 	WARN_ON(upper_32_bits(start));
1426477eb7f9SFrançois Tigeot 
1427477eb7f9SFrançois Tigeot 	bitmap_zero(new_page_tables, I915_PDES);
1428477eb7f9SFrançois Tigeot 
1429477eb7f9SFrançois Tigeot 	/* The allocation is done in two stages so that we can bail out with
1430477eb7f9SFrançois Tigeot 	 * minimal amount of pain. The first stage finds new page tables that
1431477eb7f9SFrançois Tigeot 	 * need allocation. The second stage marks use ptes within the page
1432477eb7f9SFrançois Tigeot 	 * tables.
1433477eb7f9SFrançois Tigeot 	 */
1434477eb7f9SFrançois Tigeot 	gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1435477eb7f9SFrançois Tigeot 		if (pt != ppgtt->scratch_pt) {
1436477eb7f9SFrançois Tigeot 			WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1437477eb7f9SFrançois Tigeot 			continue;
1438477eb7f9SFrançois Tigeot 		}
1439477eb7f9SFrançois Tigeot 
1440477eb7f9SFrançois Tigeot 		/* We've already allocated a page table */
1441477eb7f9SFrançois Tigeot 		WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1442477eb7f9SFrançois Tigeot 
1443477eb7f9SFrançois Tigeot 		pt = alloc_pt_single(dev);
1444477eb7f9SFrançois Tigeot 		if (IS_ERR(pt)) {
1445477eb7f9SFrançois Tigeot 			ret = PTR_ERR(pt);
1446477eb7f9SFrançois Tigeot 			goto unwind_out;
1447477eb7f9SFrançois Tigeot 		}
1448477eb7f9SFrançois Tigeot 
1449477eb7f9SFrançois Tigeot 		gen6_initialize_pt(vm, pt);
1450477eb7f9SFrançois Tigeot 
1451477eb7f9SFrançois Tigeot 		ppgtt->pd.page_table[pde] = pt;
1452477eb7f9SFrançois Tigeot 		set_bit(pde, new_page_tables);
1453477eb7f9SFrançois Tigeot 		trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
1454477eb7f9SFrançois Tigeot 	}
1455477eb7f9SFrançois Tigeot 
1456477eb7f9SFrançois Tigeot 	start = start_save;
1457477eb7f9SFrançois Tigeot 	length = length_save;
1458477eb7f9SFrançois Tigeot 
1459477eb7f9SFrançois Tigeot 	gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1460477eb7f9SFrançois Tigeot 		DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1461477eb7f9SFrançois Tigeot 
1462477eb7f9SFrançois Tigeot 		bitmap_zero(tmp_bitmap, GEN6_PTES);
1463477eb7f9SFrançois Tigeot 		bitmap_set(tmp_bitmap, gen6_pte_index(start),
1464477eb7f9SFrançois Tigeot 			   gen6_pte_count(start, length));
1465477eb7f9SFrançois Tigeot 
1466477eb7f9SFrançois Tigeot 		if (test_and_clear_bit(pde, new_page_tables))
1467477eb7f9SFrançois Tigeot 			gen6_write_pde(&ppgtt->pd, pde, pt);
1468477eb7f9SFrançois Tigeot 
1469477eb7f9SFrançois Tigeot 		trace_i915_page_table_entry_map(vm, pde, pt,
1470477eb7f9SFrançois Tigeot 					 gen6_pte_index(start),
1471477eb7f9SFrançois Tigeot 					 gen6_pte_count(start, length),
1472477eb7f9SFrançois Tigeot 					 GEN6_PTES);
1473477eb7f9SFrançois Tigeot 		bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
1474477eb7f9SFrançois Tigeot 				GEN6_PTES);
1475477eb7f9SFrançois Tigeot 	}
1476477eb7f9SFrançois Tigeot 
1477477eb7f9SFrançois Tigeot 	WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1478477eb7f9SFrançois Tigeot 
1479477eb7f9SFrançois Tigeot 	/* Make sure write is complete before other code can use this page
1480477eb7f9SFrançois Tigeot 	 * table. Also require for WC mapped PTEs */
1481477eb7f9SFrançois Tigeot 	readl(dev_priv->gtt.gsm);
1482477eb7f9SFrançois Tigeot 
1483477eb7f9SFrançois Tigeot 	mark_tlbs_dirty(ppgtt);
1484477eb7f9SFrançois Tigeot 	return 0;
1485477eb7f9SFrançois Tigeot 
1486477eb7f9SFrançois Tigeot unwind_out:
1487477eb7f9SFrançois Tigeot 	for_each_set_bit(pde, new_page_tables, I915_PDES) {
1488*19c468b4SFrançois Tigeot 		struct i915_page_table *pt = ppgtt->pd.page_table[pde];
1489477eb7f9SFrançois Tigeot 
1490477eb7f9SFrançois Tigeot 		ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
1491477eb7f9SFrançois Tigeot 		unmap_and_free_pt(pt, vm->dev);
1492477eb7f9SFrançois Tigeot 	}
1493477eb7f9SFrançois Tigeot 
1494477eb7f9SFrançois Tigeot 	mark_tlbs_dirty(ppgtt);
1495477eb7f9SFrançois Tigeot 	return ret;
1496ba55f2f5SFrançois Tigeot }
1497ba55f2f5SFrançois Tigeot 
1498ba55f2f5SFrançois Tigeot static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
1499ba55f2f5SFrançois Tigeot {
1500ba55f2f5SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
1501ba55f2f5SFrançois Tigeot 		container_of(vm, struct i915_hw_ppgtt, base);
1502*19c468b4SFrançois Tigeot 	struct i915_page_table *pt;
1503*19c468b4SFrançois Tigeot 	uint32_t pde;
1504*19c468b4SFrançois Tigeot 
1505ba55f2f5SFrançois Tigeot 
1506ba55f2f5SFrançois Tigeot 	drm_mm_remove_node(&ppgtt->node);
1507ba55f2f5SFrançois Tigeot 
1508*19c468b4SFrançois Tigeot 	gen6_for_all_pdes(pt, ppgtt, pde) {
1509*19c468b4SFrançois Tigeot 		if (pt != ppgtt->scratch_pt)
1510*19c468b4SFrançois Tigeot 			unmap_and_free_pt(pt, ppgtt->base.dev);
1511*19c468b4SFrançois Tigeot 	}
1512*19c468b4SFrançois Tigeot 
1513*19c468b4SFrançois Tigeot 	unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
1514*19c468b4SFrançois Tigeot 	unmap_and_free_pd(&ppgtt->pd, ppgtt->base.dev);
1515ba55f2f5SFrançois Tigeot }
1516ba55f2f5SFrançois Tigeot 
1517ba55f2f5SFrançois Tigeot static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
1518a2fdbec6SFrançois Tigeot {
15199edbd4a0SFrançois Tigeot 	struct drm_device *dev = ppgtt->base.dev;
15207cbd1a46SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1521ba55f2f5SFrançois Tigeot 	bool retried = false;
1522ba55f2f5SFrançois Tigeot 	int ret;
1523e3adcf8fSFrançois Tigeot 
1524ba55f2f5SFrançois Tigeot 	/* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1525ba55f2f5SFrançois Tigeot 	 * allocator works in address space sizes, so it's multiplied by page
1526ba55f2f5SFrançois Tigeot 	 * size. We allocate at the top of the GTT to avoid fragmentation.
1527ba55f2f5SFrançois Tigeot 	 */
1528ba55f2f5SFrançois Tigeot 	BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
1529477eb7f9SFrançois Tigeot 	ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
1530477eb7f9SFrançois Tigeot 	if (IS_ERR(ppgtt->scratch_pt))
1531477eb7f9SFrançois Tigeot 		return PTR_ERR(ppgtt->scratch_pt);
1532477eb7f9SFrançois Tigeot 
1533477eb7f9SFrançois Tigeot 	gen6_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
1534477eb7f9SFrançois Tigeot 
1535ba55f2f5SFrançois Tigeot alloc:
1536ba55f2f5SFrançois Tigeot 	ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1537ba55f2f5SFrançois Tigeot 						  &ppgtt->node, GEN6_PD_SIZE,
1538ba55f2f5SFrançois Tigeot 						  GEN6_PD_ALIGN, 0,
1539ba55f2f5SFrançois Tigeot 						  0, dev_priv->gtt.base.total,
1540ba55f2f5SFrançois Tigeot 						  DRM_MM_TOPDOWN);
1541ba55f2f5SFrançois Tigeot 	if (ret == -ENOSPC && !retried) {
1542ba55f2f5SFrançois Tigeot 		ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1543ba55f2f5SFrançois Tigeot 					       GEN6_PD_SIZE, GEN6_PD_ALIGN,
1544ba55f2f5SFrançois Tigeot 					       I915_CACHE_NONE,
1545ba55f2f5SFrançois Tigeot 					       0, dev_priv->gtt.base.total,
1546ba55f2f5SFrançois Tigeot 					       0);
1547ba55f2f5SFrançois Tigeot 		if (ret)
1548477eb7f9SFrançois Tigeot 			goto err_out;
1549e3adcf8fSFrançois Tigeot 
1550ba55f2f5SFrançois Tigeot 		retried = true;
1551ba55f2f5SFrançois Tigeot 		goto alloc;
1552ba55f2f5SFrançois Tigeot 	}
1553ba55f2f5SFrançois Tigeot 
1554477eb7f9SFrançois Tigeot 	if (ret)
1555477eb7f9SFrançois Tigeot 		goto err_out;
1556477eb7f9SFrançois Tigeot 
1557477eb7f9SFrançois Tigeot 
1558ba55f2f5SFrançois Tigeot 	if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1559ba55f2f5SFrançois Tigeot 		DRM_DEBUG("Forced to use aperture for PDEs\n");
1560ba55f2f5SFrançois Tigeot 
1561ba55f2f5SFrançois Tigeot 	return 0;
1562477eb7f9SFrançois Tigeot 
1563477eb7f9SFrançois Tigeot err_out:
1564477eb7f9SFrançois Tigeot 	unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
1565477eb7f9SFrançois Tigeot 	return ret;
1566ba55f2f5SFrançois Tigeot }
1567ba55f2f5SFrançois Tigeot 
1568ba55f2f5SFrançois Tigeot static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1569ba55f2f5SFrançois Tigeot {
1570477eb7f9SFrançois Tigeot 	return gen6_ppgtt_allocate_page_directories(ppgtt);
1571e3adcf8fSFrançois Tigeot }
1572e3adcf8fSFrançois Tigeot 
1573477eb7f9SFrançois Tigeot static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
1574477eb7f9SFrançois Tigeot 				  uint64_t start, uint64_t length)
1575ba55f2f5SFrançois Tigeot {
1576*19c468b4SFrançois Tigeot 	struct i915_page_table *unused;
1577477eb7f9SFrançois Tigeot 	uint32_t pde, temp;
15789edbd4a0SFrançois Tigeot 
1579477eb7f9SFrançois Tigeot 	gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
1580477eb7f9SFrançois Tigeot 		ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
15819edbd4a0SFrançois Tigeot }
1582ba55f2f5SFrançois Tigeot 
1583477eb7f9SFrançois Tigeot static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt, bool aliasing)
1584ba55f2f5SFrançois Tigeot {
1585ba55f2f5SFrançois Tigeot 	struct drm_device *dev = ppgtt->base.dev;
1586ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1587ba55f2f5SFrançois Tigeot 	int ret;
1588ba55f2f5SFrançois Tigeot 
1589ba55f2f5SFrançois Tigeot 	ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
1590ba55f2f5SFrançois Tigeot 	if (IS_GEN6(dev)) {
1591ba55f2f5SFrançois Tigeot 		ppgtt->switch_mm = gen6_mm_switch;
1592ba55f2f5SFrançois Tigeot 	} else if (IS_HASWELL(dev)) {
1593ba55f2f5SFrançois Tigeot 		ppgtt->switch_mm = hsw_mm_switch;
1594ba55f2f5SFrançois Tigeot 	} else if (IS_GEN7(dev)) {
1595ba55f2f5SFrançois Tigeot 		ppgtt->switch_mm = gen7_mm_switch;
1596ba55f2f5SFrançois Tigeot 	} else
1597ba55f2f5SFrançois Tigeot 		BUG();
1598ba55f2f5SFrançois Tigeot 
1599477eb7f9SFrançois Tigeot 	if (intel_vgpu_active(dev))
1600477eb7f9SFrançois Tigeot 		ppgtt->switch_mm = vgpu_mm_switch;
1601477eb7f9SFrançois Tigeot 
1602ba55f2f5SFrançois Tigeot 	ret = gen6_ppgtt_alloc(ppgtt);
1603ba55f2f5SFrançois Tigeot 	if (ret)
1604ba55f2f5SFrançois Tigeot 		return ret;
1605ba55f2f5SFrançois Tigeot 
1606477eb7f9SFrançois Tigeot 	if (aliasing) {
1607477eb7f9SFrançois Tigeot 		/* preallocate all pts */
1608*19c468b4SFrançois Tigeot 		ret = alloc_pt_range(&ppgtt->pd, 0, I915_PDES,
1609477eb7f9SFrançois Tigeot 				ppgtt->base.dev);
1610477eb7f9SFrançois Tigeot 
1611ba55f2f5SFrançois Tigeot 		if (ret) {
1612477eb7f9SFrançois Tigeot 			gen6_ppgtt_cleanup(&ppgtt->base);
16130b869d8aSFrançois Tigeot 			return ret;
1614e3adcf8fSFrançois Tigeot 		}
1615477eb7f9SFrançois Tigeot 	}
1616e3adcf8fSFrançois Tigeot 
1617*19c468b4SFrançois Tigeot 	ppgtt->base.allocate_va_range = aliasing ? NULL : gen6_alloc_va_range;
1618ba55f2f5SFrançois Tigeot 	ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1619ba55f2f5SFrançois Tigeot 	ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1620*19c468b4SFrançois Tigeot 	ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1621*19c468b4SFrançois Tigeot 	ppgtt->base.bind_vma = ppgtt_bind_vma;
1622ba55f2f5SFrançois Tigeot 	ppgtt->base.cleanup = gen6_ppgtt_cleanup;
1623ba55f2f5SFrançois Tigeot 	ppgtt->base.start = 0;
1624*19c468b4SFrançois Tigeot 	ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
1625ba55f2f5SFrançois Tigeot 	ppgtt->debug_dump = gen6_dump_ppgtt;
1626ba55f2f5SFrançois Tigeot 
1627477eb7f9SFrançois Tigeot 	ppgtt->pd.pd_offset =
1628477eb7f9SFrançois Tigeot 		ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
1629ba55f2f5SFrançois Tigeot 
1630477eb7f9SFrançois Tigeot 	ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
1631477eb7f9SFrançois Tigeot 		ppgtt->pd.pd_offset / sizeof(gen6_pte_t);
1632477eb7f9SFrançois Tigeot 
1633477eb7f9SFrançois Tigeot 	if (aliasing)
1634ba55f2f5SFrançois Tigeot 		ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
1635477eb7f9SFrançois Tigeot 	else
1636477eb7f9SFrançois Tigeot 		gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
1637477eb7f9SFrançois Tigeot 
1638477eb7f9SFrançois Tigeot 	gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
1639ba55f2f5SFrançois Tigeot 
1640ba55f2f5SFrançois Tigeot 	DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
1641ba55f2f5SFrançois Tigeot 			 ppgtt->node.size >> 20,
1642ba55f2f5SFrançois Tigeot 			 ppgtt->node.start / PAGE_SIZE);
1643ba55f2f5SFrançois Tigeot 
16441b13d190SFrançois Tigeot 	DRM_DEBUG("Adding PPGTT at offset %x\n",
1645477eb7f9SFrançois Tigeot 		  ppgtt->pd.pd_offset << 10);
16461b13d190SFrançois Tigeot 
1647ba55f2f5SFrançois Tigeot 	return 0;
1648ba55f2f5SFrançois Tigeot }
1649ba55f2f5SFrançois Tigeot 
1650477eb7f9SFrançois Tigeot static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt,
1651477eb7f9SFrançois Tigeot 		bool aliasing)
1652a2fdbec6SFrançois Tigeot {
1653a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1654a2fdbec6SFrançois Tigeot 
16559edbd4a0SFrançois Tigeot 	ppgtt->base.dev = dev;
1656ba55f2f5SFrançois Tigeot 	ppgtt->base.scratch = dev_priv->gtt.base.scratch;
1657a2fdbec6SFrançois Tigeot 
16588e26cdf6SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 8)
1659477eb7f9SFrançois Tigeot 		return gen6_ppgtt_init(ppgtt, aliasing);
1660*19c468b4SFrançois Tigeot 	else if (aliasing)
1661*19c468b4SFrançois Tigeot 		return gen8_aliasing_ppgtt_init(ppgtt);
16628e26cdf6SFrançois Tigeot 	else
1663*19c468b4SFrançois Tigeot 		return gen8_ppgtt_init(ppgtt);
16641b13d190SFrançois Tigeot }
16651b13d190SFrançois Tigeot int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
16661b13d190SFrançois Tigeot {
1667ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
16681b13d190SFrançois Tigeot 	int ret = 0;
16691b13d190SFrançois Tigeot 
1670477eb7f9SFrançois Tigeot 	ret = __hw_ppgtt_init(dev, ppgtt, false);
16711b13d190SFrançois Tigeot 	if (ret == 0) {
1672ba55f2f5SFrançois Tigeot 		kref_init(&ppgtt->ref);
16739edbd4a0SFrançois Tigeot 		drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
16749edbd4a0SFrançois Tigeot 			    ppgtt->base.total);
1675ba55f2f5SFrançois Tigeot 		i915_init_vm(dev_priv, &ppgtt->base);
16761b13d190SFrançois Tigeot 	}
16771b13d190SFrançois Tigeot 
16781b13d190SFrançois Tigeot 	return ret;
16791b13d190SFrançois Tigeot }
16801b13d190SFrançois Tigeot 
16811b13d190SFrançois Tigeot int i915_ppgtt_init_hw(struct drm_device *dev)
16821b13d190SFrançois Tigeot {
16831b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
16841b13d190SFrançois Tigeot 	struct intel_engine_cs *ring;
16851b13d190SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
16861b13d190SFrançois Tigeot 	int i, ret = 0;
16871b13d190SFrançois Tigeot 
16881b13d190SFrançois Tigeot 	/* In the case of execlists, PPGTT is enabled by the context descriptor
16891b13d190SFrançois Tigeot 	 * and the PDPs are contained within the context itself.  We don't
16901b13d190SFrançois Tigeot 	 * need to do anything here. */
16911b13d190SFrançois Tigeot 	if (i915.enable_execlists)
16921b13d190SFrançois Tigeot 		return 0;
16931b13d190SFrançois Tigeot 
16941b13d190SFrançois Tigeot 	if (!USES_PPGTT(dev))
16951b13d190SFrançois Tigeot 		return 0;
16961b13d190SFrançois Tigeot 
16971b13d190SFrançois Tigeot 	if (IS_GEN6(dev))
16981b13d190SFrançois Tigeot 		gen6_ppgtt_enable(dev);
16991b13d190SFrançois Tigeot 	else if (IS_GEN7(dev))
17001b13d190SFrançois Tigeot 		gen7_ppgtt_enable(dev);
17011b13d190SFrançois Tigeot 	else if (INTEL_INFO(dev)->gen >= 8)
17021b13d190SFrançois Tigeot 		gen8_ppgtt_enable(dev);
17031b13d190SFrançois Tigeot 	else
17042c9916cdSFrançois Tigeot 		MISSING_CASE(INTEL_INFO(dev)->gen);
17051b13d190SFrançois Tigeot 
17061b13d190SFrançois Tigeot 	if (ppgtt) {
17071b13d190SFrançois Tigeot 		for_each_ring(ring, dev_priv, i) {
17081b13d190SFrançois Tigeot 			ret = ppgtt->switch_mm(ppgtt, ring);
17091b13d190SFrançois Tigeot 			if (ret != 0)
17101b13d190SFrançois Tigeot 				return ret;
1711ba55f2f5SFrançois Tigeot 		}
17129edbd4a0SFrançois Tigeot 	}
1713a2fdbec6SFrançois Tigeot 
1714a2fdbec6SFrançois Tigeot 	return ret;
1715a2fdbec6SFrançois Tigeot }
17161b13d190SFrançois Tigeot struct i915_hw_ppgtt *
17171b13d190SFrançois Tigeot i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
17181b13d190SFrançois Tigeot {
17191b13d190SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt;
17201b13d190SFrançois Tigeot 	int ret;
17211b13d190SFrançois Tigeot 
17221b13d190SFrançois Tigeot 	ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
17231b13d190SFrançois Tigeot 	if (!ppgtt)
17241b13d190SFrançois Tigeot 		return ERR_PTR(-ENOMEM);
17251b13d190SFrançois Tigeot 
17261b13d190SFrançois Tigeot 	ret = i915_ppgtt_init(dev, ppgtt);
17271b13d190SFrançois Tigeot 	if (ret) {
17281b13d190SFrançois Tigeot 		kfree(ppgtt);
17291b13d190SFrançois Tigeot 		return ERR_PTR(ret);
17301b13d190SFrançois Tigeot 	}
17311b13d190SFrançois Tigeot 
17321b13d190SFrançois Tigeot 	ppgtt->file_priv = fpriv;
17331b13d190SFrançois Tigeot 
17342c9916cdSFrançois Tigeot 	trace_i915_ppgtt_create(&ppgtt->base);
17352c9916cdSFrançois Tigeot 
17361b13d190SFrançois Tigeot 	return ppgtt;
17371b13d190SFrançois Tigeot }
17381b13d190SFrançois Tigeot 
17391b13d190SFrançois Tigeot void  i915_ppgtt_release(struct kref *kref)
17401b13d190SFrançois Tigeot {
17411b13d190SFrançois Tigeot 	struct i915_hw_ppgtt *ppgtt =
17421b13d190SFrançois Tigeot 		container_of(kref, struct i915_hw_ppgtt, ref);
17431b13d190SFrançois Tigeot 
17442c9916cdSFrançois Tigeot 	trace_i915_ppgtt_release(&ppgtt->base);
17452c9916cdSFrançois Tigeot 
17461b13d190SFrançois Tigeot 	/* vmas should already be unbound */
17471b13d190SFrançois Tigeot 	WARN_ON(!list_empty(&ppgtt->base.active_list));
17481b13d190SFrançois Tigeot 	WARN_ON(!list_empty(&ppgtt->base.inactive_list));
17491b13d190SFrançois Tigeot 
17501b13d190SFrançois Tigeot 	list_del(&ppgtt->base.global_link);
17511b13d190SFrançois Tigeot 	drm_mm_takedown(&ppgtt->base.mm);
17521b13d190SFrançois Tigeot 
17531b13d190SFrançois Tigeot 	ppgtt->base.cleanup(&ppgtt->base);
17541b13d190SFrançois Tigeot 	kfree(ppgtt);
17551b13d190SFrançois Tigeot }
1756a2fdbec6SFrançois Tigeot 
1757a2fdbec6SFrançois Tigeot extern int intel_iommu_gfx_mapped;
1758a2fdbec6SFrançois Tigeot /* Certain Gen5 chipsets require require idling the GPU before
1759a2fdbec6SFrançois Tigeot  * unmapping anything from the GTT when VT-d is enabled.
1760a2fdbec6SFrançois Tigeot  */
1761*19c468b4SFrançois Tigeot static bool needs_idle_maps(struct drm_device *dev)
1762a2fdbec6SFrançois Tigeot {
1763a2fdbec6SFrançois Tigeot #ifdef CONFIG_INTEL_IOMMU
1764a2fdbec6SFrançois Tigeot 	/* Query intel_iommu to see if we need the workaround. Presumably that
1765a2fdbec6SFrançois Tigeot 	 * was loaded first.
1766a2fdbec6SFrançois Tigeot 	 */
1767a2fdbec6SFrançois Tigeot 	if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1768a2fdbec6SFrançois Tigeot 		return true;
1769a2fdbec6SFrançois Tigeot #endif
1770a2fdbec6SFrançois Tigeot 	return false;
1771a2fdbec6SFrançois Tigeot }
1772a2fdbec6SFrançois Tigeot 
17737cbd1a46SFrançois Tigeot static bool do_idling(struct drm_i915_private *dev_priv)
1774e3adcf8fSFrançois Tigeot {
1775e3adcf8fSFrançois Tigeot 	bool ret = dev_priv->mm.interruptible;
1776e3adcf8fSFrançois Tigeot 
1777a2fdbec6SFrançois Tigeot 	if (unlikely(dev_priv->gtt.do_idle_maps)) {
1778e3adcf8fSFrançois Tigeot 		dev_priv->mm.interruptible = false;
1779b030f26bSFrançois Tigeot 		if (i915_gpu_idle(dev_priv->dev)) {
1780e3adcf8fSFrançois Tigeot 			DRM_ERROR("Couldn't idle GPU\n");
1781e3adcf8fSFrançois Tigeot 			/* Wait a bit, in hopes it avoids the hang */
17820b869d8aSFrançois Tigeot 			udelay(10);
1783e3adcf8fSFrançois Tigeot 		}
1784e3adcf8fSFrançois Tigeot 	}
1785e3adcf8fSFrançois Tigeot 
1786e3adcf8fSFrançois Tigeot 	return ret;
1787e3adcf8fSFrançois Tigeot }
1788e3adcf8fSFrançois Tigeot 
17897cbd1a46SFrançois Tigeot static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1790e3adcf8fSFrançois Tigeot {
1791a2fdbec6SFrançois Tigeot 	if (unlikely(dev_priv->gtt.do_idle_maps))
1792e3adcf8fSFrançois Tigeot 		dev_priv->mm.interruptible = interruptible;
1793e3adcf8fSFrançois Tigeot }
1794e3adcf8fSFrançois Tigeot 
17959edbd4a0SFrançois Tigeot void i915_check_and_clear_faults(struct drm_device *dev)
17969edbd4a0SFrançois Tigeot {
17979edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1798ba55f2f5SFrançois Tigeot 	struct intel_engine_cs *ring;
17999edbd4a0SFrançois Tigeot 	int i;
18009edbd4a0SFrançois Tigeot 
18019edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 6)
18029edbd4a0SFrançois Tigeot 		return;
18039edbd4a0SFrançois Tigeot 
18049edbd4a0SFrançois Tigeot 	for_each_ring(ring, dev_priv, i) {
18059edbd4a0SFrançois Tigeot 		u32 fault_reg;
18069edbd4a0SFrançois Tigeot 		fault_reg = I915_READ(RING_FAULT_REG(ring));
18079edbd4a0SFrançois Tigeot 		if (fault_reg & RING_FAULT_VALID) {
18089edbd4a0SFrançois Tigeot #if 0
18099edbd4a0SFrançois Tigeot 			DRM_DEBUG_DRIVER("Unexpected fault\n"
18102c9916cdSFrançois Tigeot 					 "\tAddr: 0x%08lx\n"
18119edbd4a0SFrançois Tigeot 					 "\tAddress space: %s\n"
18129edbd4a0SFrançois Tigeot 					 "\tSource ID: %d\n"
18139edbd4a0SFrançois Tigeot 					 "\tType: %d\n",
18149edbd4a0SFrançois Tigeot 					 fault_reg & PAGE_MASK,
18159edbd4a0SFrançois Tigeot 					 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
18169edbd4a0SFrançois Tigeot 					 RING_FAULT_SRCID(fault_reg),
18179edbd4a0SFrançois Tigeot 					 RING_FAULT_FAULT_TYPE(fault_reg));
18189edbd4a0SFrançois Tigeot #endif
18199edbd4a0SFrançois Tigeot 			I915_WRITE(RING_FAULT_REG(ring),
18209edbd4a0SFrançois Tigeot 				   fault_reg & ~RING_FAULT_VALID);
18219edbd4a0SFrançois Tigeot 		}
18229edbd4a0SFrançois Tigeot 	}
18239edbd4a0SFrançois Tigeot 	POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
18249edbd4a0SFrançois Tigeot }
18259edbd4a0SFrançois Tigeot 
182624edb884SFrançois Tigeot static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
182724edb884SFrançois Tigeot {
182824edb884SFrançois Tigeot 	if (INTEL_INFO(dev_priv->dev)->gen < 6) {
182924edb884SFrançois Tigeot 		intel_gtt_chipset_flush();
183024edb884SFrançois Tigeot 	} else {
183124edb884SFrançois Tigeot 		I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
183224edb884SFrançois Tigeot 		POSTING_READ(GFX_FLSH_CNTL_GEN6);
183324edb884SFrançois Tigeot 	}
183424edb884SFrançois Tigeot }
183524edb884SFrançois Tigeot 
18369edbd4a0SFrançois Tigeot void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
18379edbd4a0SFrançois Tigeot {
18389edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
18399edbd4a0SFrançois Tigeot 
18409edbd4a0SFrançois Tigeot 	/* Don't bother messing with faults pre GEN6 as we have little
18419edbd4a0SFrançois Tigeot 	 * documentation supporting that it's a good idea.
18429edbd4a0SFrançois Tigeot 	 */
18439edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 6)
18449edbd4a0SFrançois Tigeot 		return;
18459edbd4a0SFrançois Tigeot 
18469edbd4a0SFrançois Tigeot 	i915_check_and_clear_faults(dev);
18479edbd4a0SFrançois Tigeot 
18489edbd4a0SFrançois Tigeot 	dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1849ba55f2f5SFrançois Tigeot 				       dev_priv->gtt.base.start,
1850ba55f2f5SFrançois Tigeot 				       dev_priv->gtt.base.total,
18519edbd4a0SFrançois Tigeot 				       true);
185224edb884SFrançois Tigeot 
185324edb884SFrançois Tigeot 	i915_ggtt_flush(dev_priv);
18549edbd4a0SFrançois Tigeot }
18559edbd4a0SFrançois Tigeot 
18560b869d8aSFrançois Tigeot int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
18570b869d8aSFrançois Tigeot {
18589edbd4a0SFrançois Tigeot #if 0
18590b869d8aSFrançois Tigeot 	if (!dma_map_sg(&obj->base.dev->pdev->dev,
18600b869d8aSFrançois Tigeot 			obj->pages->sgl, obj->pages->nents,
18610b869d8aSFrançois Tigeot 			PCI_DMA_BIDIRECTIONAL))
18620b869d8aSFrançois Tigeot 		return -ENOSPC;
18639edbd4a0SFrançois Tigeot #endif
18640b869d8aSFrançois Tigeot 
18650b869d8aSFrançois Tigeot 	return 0;
18660b869d8aSFrançois Tigeot }
18670b869d8aSFrançois Tigeot 
1868*19c468b4SFrançois Tigeot static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
1869e3adcf8fSFrançois Tigeot {
18709edbd4a0SFrançois Tigeot #if 0
18719edbd4a0SFrançois Tigeot 	writeq(pte, addr);
18729edbd4a0SFrançois Tigeot #else
18739edbd4a0SFrançois Tigeot 	iowrite32((u32)pte, addr);
18749edbd4a0SFrançois Tigeot 	iowrite32(pte >> 32, addr + 4);
18759edbd4a0SFrançois Tigeot #endif
18767cbd1a46SFrançois Tigeot }
1877e3adcf8fSFrançois Tigeot 
18789edbd4a0SFrançois Tigeot static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
18799edbd4a0SFrançois Tigeot 				     vm_page_t *pages,
1880ba55f2f5SFrançois Tigeot 				     uint64_t start,
18819edbd4a0SFrançois Tigeot 				     unsigned int num_entries,
188224edb884SFrançois Tigeot 				     enum i915_cache_level level, u32 unused)
18839edbd4a0SFrançois Tigeot {
18849edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1885ba55f2f5SFrançois Tigeot 	unsigned first_entry = start >> PAGE_SHIFT;
1886477eb7f9SFrançois Tigeot 	gen8_pte_t __iomem *gtt_entries =
1887477eb7f9SFrançois Tigeot 		(gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
18889edbd4a0SFrançois Tigeot 	int i = 0;
1889ba55f2f5SFrançois Tigeot 	dma_addr_t addr = 0;
18909edbd4a0SFrançois Tigeot 
18919edbd4a0SFrançois Tigeot 	for (i=0;i<num_entries;i++) {
18929edbd4a0SFrançois Tigeot 		addr = VM_PAGE_TO_PHYS(pages[i]);
18939edbd4a0SFrançois Tigeot 		gen8_set_pte(&gtt_entries[i],
18949edbd4a0SFrançois Tigeot 			     gen8_pte_encode(addr, level, true));
18959edbd4a0SFrançois Tigeot 	}
18969edbd4a0SFrançois Tigeot 
18979edbd4a0SFrançois Tigeot 	/*
18989edbd4a0SFrançois Tigeot 	 * XXX: This serves as a posting read to make sure that the PTE has
18997cbd1a46SFrançois Tigeot 	 * actually been updated. There is some concern that even though
19007cbd1a46SFrançois Tigeot 	 * registers and PTEs are within the same BAR that they are potentially
19017cbd1a46SFrançois Tigeot 	 * of NUMA access patterns. Therefore, even with the way we assume
19027cbd1a46SFrançois Tigeot 	 * hardware should work, we must keep this posting read for paranoia.
19037cbd1a46SFrançois Tigeot 	 */
19047cbd1a46SFrançois Tigeot 	if (i != 0)
19059edbd4a0SFrançois Tigeot 		WARN_ON(readq(&gtt_entries[i-1])
19069edbd4a0SFrançois Tigeot 			!= gen8_pte_encode(addr, level, true));
19077cbd1a46SFrançois Tigeot 
19087cbd1a46SFrançois Tigeot 	/* This next bit makes the above posting read even more important. We
19097cbd1a46SFrançois Tigeot 	 * want to flush the TLBs only after we're certain all the PTE updates
19107cbd1a46SFrançois Tigeot 	 * have finished.
19117cbd1a46SFrançois Tigeot 	 */
19127cbd1a46SFrançois Tigeot 	I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
19137cbd1a46SFrançois Tigeot 	POSTING_READ(GFX_FLSH_CNTL_GEN6);
1914a2fdbec6SFrançois Tigeot }
1915a2fdbec6SFrançois Tigeot 
19169edbd4a0SFrançois Tigeot /*
19179edbd4a0SFrançois Tigeot  * Binds an object into the global gtt with the specified cache level. The object
19189edbd4a0SFrançois Tigeot  * will be accessible to the GPU via commands whose operands reference offsets
19199edbd4a0SFrançois Tigeot  * within the global GTT as well as accessible by the GPU through the GMADR
19209edbd4a0SFrançois Tigeot  * mapped BAR (dev_priv->mm.gtt->gtt).
19219edbd4a0SFrançois Tigeot  */
19229edbd4a0SFrançois Tigeot static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
19239edbd4a0SFrançois Tigeot 				     vm_page_t *pages,
1924ba55f2f5SFrançois Tigeot 				     uint64_t start,
19259edbd4a0SFrançois Tigeot 				     unsigned int num_entries,
192624edb884SFrançois Tigeot 				     enum i915_cache_level level, u32 flags)
1927a2fdbec6SFrançois Tigeot {
19289edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1929ba55f2f5SFrançois Tigeot 	unsigned first_entry = start >> PAGE_SHIFT;
1930477eb7f9SFrançois Tigeot 	gen6_pte_t __iomem *gtt_entries =
1931477eb7f9SFrançois Tigeot 		(gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
19329edbd4a0SFrançois Tigeot 	int i = 0;
193324edb884SFrançois Tigeot 	dma_addr_t addr = 0; /* shut up gcc */
19349edbd4a0SFrançois Tigeot 
19359edbd4a0SFrançois Tigeot 	for (i = 0; i < num_entries; i++) {
19369edbd4a0SFrançois Tigeot 		addr = VM_PAGE_TO_PHYS(pages[i]);
193724edb884SFrançois Tigeot 		iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
19389edbd4a0SFrançois Tigeot 	}
19399edbd4a0SFrançois Tigeot 
19409edbd4a0SFrançois Tigeot 	/* XXX: This serves as a posting read to make sure that the PTE has
19419edbd4a0SFrançois Tigeot 	 * actually been updated. There is some concern that even though
19429edbd4a0SFrançois Tigeot 	 * registers and PTEs are within the same BAR that they are potentially
19439edbd4a0SFrançois Tigeot 	 * of NUMA access patterns. Therefore, even with the way we assume
19449edbd4a0SFrançois Tigeot 	 * hardware should work, we must keep this posting read for paranoia.
19459edbd4a0SFrançois Tigeot 	 */
194624edb884SFrançois Tigeot 	if (i != 0) {
194724edb884SFrançois Tigeot 		unsigned long gtt = readl(&gtt_entries[i-1]);
194824edb884SFrançois Tigeot 		WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
194924edb884SFrançois Tigeot 	}
19509edbd4a0SFrançois Tigeot 
19519edbd4a0SFrançois Tigeot 	/* This next bit makes the above posting read even more important. We
19529edbd4a0SFrançois Tigeot 	 * want to flush the TLBs only after we're certain all the PTE updates
19539edbd4a0SFrançois Tigeot 	 * have finished.
19549edbd4a0SFrançois Tigeot 	 */
19559edbd4a0SFrançois Tigeot 	I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
19569edbd4a0SFrançois Tigeot 	POSTING_READ(GFX_FLSH_CNTL_GEN6);
19579edbd4a0SFrançois Tigeot }
19589edbd4a0SFrançois Tigeot 
19599edbd4a0SFrançois Tigeot static void gen8_ggtt_clear_range(struct i915_address_space *vm,
1960ba55f2f5SFrançois Tigeot 				  uint64_t start,
1961ba55f2f5SFrançois Tigeot 				  uint64_t length,
19629edbd4a0SFrançois Tigeot 				  bool use_scratch)
19639edbd4a0SFrançois Tigeot {
19649edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1965ba55f2f5SFrançois Tigeot 	unsigned first_entry = start >> PAGE_SHIFT;
1966ba55f2f5SFrançois Tigeot 	unsigned num_entries = length >> PAGE_SHIFT;
1967477eb7f9SFrançois Tigeot 	gen8_pte_t scratch_pte, __iomem *gtt_base =
1968477eb7f9SFrançois Tigeot 		(gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
19699edbd4a0SFrançois Tigeot 	const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
19709edbd4a0SFrançois Tigeot 	int i;
19719edbd4a0SFrançois Tigeot 
19729edbd4a0SFrançois Tigeot 	if (WARN(num_entries > max_entries,
19739edbd4a0SFrançois Tigeot 		 "First entry = %d; Num entries = %d (max=%d)\n",
19749edbd4a0SFrançois Tigeot 		 first_entry, num_entries, max_entries))
19759edbd4a0SFrançois Tigeot 		num_entries = max_entries;
19769edbd4a0SFrançois Tigeot 
19779edbd4a0SFrançois Tigeot 	scratch_pte = gen8_pte_encode(vm->scratch.addr,
19789edbd4a0SFrançois Tigeot 				      I915_CACHE_LLC,
19799edbd4a0SFrançois Tigeot 				      use_scratch);
19809edbd4a0SFrançois Tigeot 	for (i = 0; i < num_entries; i++)
19819edbd4a0SFrançois Tigeot 		gen8_set_pte(&gtt_base[i], scratch_pte);
19829edbd4a0SFrançois Tigeot 	readl(gtt_base);
19839edbd4a0SFrançois Tigeot }
19849edbd4a0SFrançois Tigeot 
19859edbd4a0SFrançois Tigeot static void gen6_ggtt_clear_range(struct i915_address_space *vm,
1986ba55f2f5SFrançois Tigeot 				  uint64_t start,
1987ba55f2f5SFrançois Tigeot 				  uint64_t length,
19889edbd4a0SFrançois Tigeot 				  bool use_scratch)
19899edbd4a0SFrançois Tigeot {
19909edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = vm->dev->dev_private;
1991ba55f2f5SFrançois Tigeot 	unsigned first_entry = start >> PAGE_SHIFT;
1992ba55f2f5SFrançois Tigeot 	unsigned num_entries = length >> PAGE_SHIFT;
1993477eb7f9SFrançois Tigeot 	gen6_pte_t scratch_pte, __iomem *gtt_base =
1994477eb7f9SFrançois Tigeot 		(gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
19958e26cdf6SFrançois Tigeot 	const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1996a2fdbec6SFrançois Tigeot 	int i;
1997a2fdbec6SFrançois Tigeot 
1998a2fdbec6SFrançois Tigeot 	if (WARN(num_entries > max_entries,
1999a2fdbec6SFrançois Tigeot 		 "First entry = %d; Num entries = %d (max=%d)\n",
2000a2fdbec6SFrançois Tigeot 		 first_entry, num_entries, max_entries))
2001a2fdbec6SFrançois Tigeot 		num_entries = max_entries;
2002a2fdbec6SFrançois Tigeot 
200324edb884SFrançois Tigeot 	scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
20049edbd4a0SFrançois Tigeot 
2005a2fdbec6SFrançois Tigeot 	for (i = 0; i < num_entries; i++)
2006a2fdbec6SFrançois Tigeot 		iowrite32(scratch_pte, &gtt_base[i]);
2007a2fdbec6SFrançois Tigeot 	readl(gtt_base);
2008a2fdbec6SFrançois Tigeot }
2009a2fdbec6SFrançois Tigeot 
2010*19c468b4SFrançois Tigeot static int i915_ggtt_bind_vma(struct i915_vma *vma,
2011ba55f2f5SFrançois Tigeot 			      enum i915_cache_level cache_level,
2012ba55f2f5SFrançois Tigeot 			      u32 unused)
2013a2fdbec6SFrançois Tigeot {
2014ba55f2f5SFrançois Tigeot 	const unsigned long entry = vma->node.start >> PAGE_SHIFT;
2015ba55f2f5SFrançois Tigeot 	const unsigned int num_entries = vma->obj->base.size >> PAGE_SHIFT;
2016a2fdbec6SFrançois Tigeot 	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2017a2fdbec6SFrançois Tigeot 		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2018a2fdbec6SFrançois Tigeot 
2019ba55f2f5SFrançois Tigeot 	BUG_ON(!i915_is_ggtt(vma->vm));
20202c9916cdSFrançois Tigeot 	intel_gtt_insert_pages(entry, num_entries, vma->ggtt_view.pages, flags);
2021*19c468b4SFrançois Tigeot 
2022*19c468b4SFrançois Tigeot 	vma->bound |= GLOBAL_BIND;
2023*19c468b4SFrançois Tigeot 	return 0;
2024a2fdbec6SFrançois Tigeot }
2025a2fdbec6SFrançois Tigeot 
20269edbd4a0SFrançois Tigeot static void i915_ggtt_clear_range(struct i915_address_space *vm,
2027ba55f2f5SFrançois Tigeot 				  uint64_t start,
2028ba55f2f5SFrançois Tigeot 				  uint64_t length,
20299edbd4a0SFrançois Tigeot 				  bool unused)
2030a2fdbec6SFrançois Tigeot {
2031ba55f2f5SFrançois Tigeot 	unsigned first_entry = start >> PAGE_SHIFT;
2032ba55f2f5SFrançois Tigeot 	unsigned num_entries = length >> PAGE_SHIFT;
2033a2fdbec6SFrançois Tigeot 	intel_gtt_clear_range(first_entry, num_entries);
2034a2fdbec6SFrançois Tigeot }
20357cbd1a46SFrançois Tigeot 
2036ba55f2f5SFrançois Tigeot static void i915_ggtt_unbind_vma(struct i915_vma *vma)
2037e3adcf8fSFrançois Tigeot {
2038ba55f2f5SFrançois Tigeot 	const unsigned int first = vma->node.start >> PAGE_SHIFT;
2039ba55f2f5SFrançois Tigeot 	const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
20409edbd4a0SFrançois Tigeot 
2041ba55f2f5SFrançois Tigeot 	BUG_ON(!i915_is_ggtt(vma->vm));
2042ba55f2f5SFrançois Tigeot 	intel_gtt_clear_range(first, size);
2043e3adcf8fSFrançois Tigeot }
2044e3adcf8fSFrançois Tigeot 
2045*19c468b4SFrançois Tigeot static int ggtt_bind_vma(struct i915_vma *vma,
2046ba55f2f5SFrançois Tigeot 			 enum i915_cache_level cache_level,
2047ba55f2f5SFrançois Tigeot 			 u32 flags)
2048e3adcf8fSFrançois Tigeot {
2049ba55f2f5SFrançois Tigeot 	struct drm_device *dev = vma->vm->dev;
2050e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2051ba55f2f5SFrançois Tigeot 	struct drm_i915_gem_object *obj = vma->obj;
2052477eb7f9SFrançois Tigeot 	struct vm_page **pages = obj->pages;
2053*19c468b4SFrançois Tigeot 	u32 pte_flags = 0;
2054e3adcf8fSFrançois Tigeot 
205524edb884SFrançois Tigeot 	/* Currently applicable only to VLV */
205624edb884SFrançois Tigeot 	if (obj->gt_ro)
2057*19c468b4SFrançois Tigeot 		pte_flags |= PTE_READ_ONLY;
205824edb884SFrançois Tigeot 
2059477eb7f9SFrançois Tigeot 	if (i915_is_ggtt(vma->vm))
2060477eb7f9SFrançois Tigeot 		pages = vma->ggtt_view.pages;
2061477eb7f9SFrançois Tigeot 
2062ba55f2f5SFrançois Tigeot 	if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
2063477eb7f9SFrançois Tigeot 		vma->vm->insert_entries(vma->vm, pages,
2064ba55f2f5SFrançois Tigeot 					vma->node.start,
20659edbd4a0SFrançois Tigeot 					obj->base.size >> PAGE_SHIFT,
2066*19c468b4SFrançois Tigeot 					cache_level, pte_flags);
2067*19c468b4SFrançois Tigeot 
2068*19c468b4SFrançois Tigeot 		/* Note the inconsistency here is due to absence of the
2069*19c468b4SFrançois Tigeot 		 * aliasing ppgtt on gen4 and earlier. Though we always
2070*19c468b4SFrançois Tigeot 		 * request PIN_USER for execbuffer (translated to LOCAL_BIND),
2071*19c468b4SFrançois Tigeot 		 * without the appgtt, we cannot honour that request and so
2072*19c468b4SFrançois Tigeot 		 * must substitute it with a global binding. Since we do this
2073*19c468b4SFrançois Tigeot 		 * behind the upper layers back, we need to explicitly set
2074*19c468b4SFrançois Tigeot 		 * the bound flag ourselves.
2075*19c468b4SFrançois Tigeot 		 */
20762c9916cdSFrançois Tigeot 		vma->bound |= GLOBAL_BIND;
2077*19c468b4SFrançois Tigeot 
2078ba55f2f5SFrançois Tigeot 	}
2079e3adcf8fSFrançois Tigeot 
2080*19c468b4SFrançois Tigeot 	if (dev_priv->mm.aliasing_ppgtt && flags & LOCAL_BIND) {
2081ba55f2f5SFrançois Tigeot 		struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
2082477eb7f9SFrançois Tigeot 		appgtt->base.insert_entries(&appgtt->base, pages,
2083ba55f2f5SFrançois Tigeot 					    vma->node.start,
2084ba55f2f5SFrançois Tigeot 					    obj->base.size >> PAGE_SHIFT,
2085*19c468b4SFrançois Tigeot 					    cache_level, pte_flags);
2086ba55f2f5SFrançois Tigeot 	}
2087*19c468b4SFrançois Tigeot 
2088*19c468b4SFrançois Tigeot 	return 0;
2089ba55f2f5SFrançois Tigeot }
2090ba55f2f5SFrançois Tigeot 
2091ba55f2f5SFrançois Tigeot static void ggtt_unbind_vma(struct i915_vma *vma)
2092ba55f2f5SFrançois Tigeot {
2093ba55f2f5SFrançois Tigeot 	struct drm_device *dev = vma->vm->dev;
2094ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2095ba55f2f5SFrançois Tigeot 	struct drm_i915_gem_object *obj = vma->obj;
2096*19c468b4SFrançois Tigeot 	const uint64_t size = min_t(uint64_t,
2097*19c468b4SFrançois Tigeot 				    obj->base.size,
2098*19c468b4SFrançois Tigeot 				    vma->node.size);
2099ba55f2f5SFrançois Tigeot 
21002c9916cdSFrançois Tigeot 	if (vma->bound & GLOBAL_BIND) {
2101ba55f2f5SFrançois Tigeot 		vma->vm->clear_range(vma->vm,
2102ba55f2f5SFrançois Tigeot 				     vma->node.start,
2103*19c468b4SFrançois Tigeot 				     size,
2104ba55f2f5SFrançois Tigeot 				     true);
2105f4e1c372SFrançois Tigeot 	}
2106f192107fSFrançois Tigeot 
2107*19c468b4SFrançois Tigeot 	if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) {
2108ba55f2f5SFrançois Tigeot 		struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
2109*19c468b4SFrançois Tigeot 
2110ba55f2f5SFrançois Tigeot 		appgtt->base.clear_range(&appgtt->base,
2111ba55f2f5SFrançois Tigeot 					 vma->node.start,
2112*19c468b4SFrançois Tigeot 					 size,
2113ba55f2f5SFrançois Tigeot 					 true);
2114ba55f2f5SFrançois Tigeot 	}
2115ba55f2f5SFrançois Tigeot }
2116ba55f2f5SFrançois Tigeot 
2117f192107fSFrançois Tigeot void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
2118f192107fSFrançois Tigeot {
2119f192107fSFrançois Tigeot 	struct drm_device *dev = obj->base.dev;
2120f192107fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2121f192107fSFrançois Tigeot 	bool interruptible;
2122f192107fSFrançois Tigeot 
2123f192107fSFrançois Tigeot 	interruptible = do_idling(dev_priv);
2124f192107fSFrançois Tigeot 
2125f192107fSFrançois Tigeot #if 0
2126*19c468b4SFrançois Tigeot 	dma_unmap_sg(&dev->pdev->dev, obj->pages->sgl, obj->pages->nents,
2127f192107fSFrançois Tigeot 		     PCI_DMA_BIDIRECTIONAL);
2128f192107fSFrançois Tigeot #endif
2129f192107fSFrançois Tigeot 
2130f192107fSFrançois Tigeot 	undo_idling(dev_priv, interruptible);
2131f192107fSFrançois Tigeot }
2132d1c259eeSFrançois Tigeot 
2133d1c259eeSFrançois Tigeot static void i915_gtt_color_adjust(struct drm_mm_node *node,
2134d1c259eeSFrançois Tigeot 				  unsigned long color,
21352c9916cdSFrançois Tigeot 				  u64 *start,
21362c9916cdSFrançois Tigeot 				  u64 *end)
2137d1c259eeSFrançois Tigeot {
2138d1c259eeSFrançois Tigeot 	if (node->color != color)
2139d1c259eeSFrançois Tigeot 		*start += 4096;
2140d1c259eeSFrançois Tigeot 
2141d1c259eeSFrançois Tigeot 	if (!list_empty(&node->node_list)) {
2142d1c259eeSFrançois Tigeot 		node = list_entry(node->node_list.next,
2143d1c259eeSFrançois Tigeot 				  struct drm_mm_node,
2144d1c259eeSFrançois Tigeot 				  node_list);
2145d1c259eeSFrançois Tigeot 		if (node->allocated && node->color != color)
2146d1c259eeSFrançois Tigeot 			*end -= 4096;
2147d1c259eeSFrançois Tigeot 	}
2148d1c259eeSFrançois Tigeot }
21499edbd4a0SFrançois Tigeot 
21502c9916cdSFrançois Tigeot static int i915_gem_setup_global_gtt(struct drm_device *dev,
2151d1c259eeSFrançois Tigeot 				     unsigned long start,
2152d1c259eeSFrançois Tigeot 				     unsigned long mappable_end,
2153d1c259eeSFrançois Tigeot 				     unsigned long end)
2154d1c259eeSFrançois Tigeot {
2155a2fdbec6SFrançois Tigeot 	/* Let GEM Manage all of the aperture.
2156a2fdbec6SFrançois Tigeot 	 *
2157a2fdbec6SFrançois Tigeot 	 * However, leave one page at the end still bound to the scratch page.
2158a2fdbec6SFrançois Tigeot 	 * There are a number of places where the hardware apparently prefetches
2159a2fdbec6SFrançois Tigeot 	 * past the end of the object, and we've seen multiple hangs with the
2160a2fdbec6SFrançois Tigeot 	 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2161a2fdbec6SFrançois Tigeot 	 * aperture.  One page should be enough to keep any prefetching inside
2162a2fdbec6SFrançois Tigeot 	 * of the aperture.
2163a2fdbec6SFrançois Tigeot 	 */
21649edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
21659edbd4a0SFrançois Tigeot 	struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
2166e084c92dSFrançois Tigeot 	unsigned long mappable;
2167e084c92dSFrançois Tigeot 	int error;
21689edbd4a0SFrançois Tigeot 	struct drm_mm_node *entry;
21699edbd4a0SFrançois Tigeot 	struct drm_i915_gem_object *obj;
21709edbd4a0SFrançois Tigeot 	unsigned long hole_start, hole_end;
21711b13d190SFrançois Tigeot 	int ret;
2172e084c92dSFrançois Tigeot 
21739edbd4a0SFrançois Tigeot 	kprintf("MAPPABLE_END VS END %016jx %016jx\n", mappable_end, end);
21749edbd4a0SFrançois Tigeot 	tsleep(&mappable_end, 0, "DELAY", hz); /* for kprintf */
21759edbd4a0SFrançois Tigeot 	/*BUG_ON(mappable_end > end);*/
2176a2fdbec6SFrançois Tigeot 
2177e084c92dSFrançois Tigeot 	mappable = min(end, mappable_end) - start;
2178e084c92dSFrançois Tigeot 
21799edbd4a0SFrançois Tigeot 	/* Subtract the guard page ... */
21809edbd4a0SFrançois Tigeot 	drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
2181477eb7f9SFrançois Tigeot 
2182477eb7f9SFrançois Tigeot 	dev_priv->gtt.base.start = start;
2183477eb7f9SFrançois Tigeot 	dev_priv->gtt.base.total = end - start;
2184477eb7f9SFrançois Tigeot 
2185477eb7f9SFrançois Tigeot 	if (intel_vgpu_active(dev)) {
2186477eb7f9SFrançois Tigeot 		ret = intel_vgt_balloon(dev);
2187477eb7f9SFrançois Tigeot 		if (ret)
2188477eb7f9SFrançois Tigeot 			return ret;
2189477eb7f9SFrançois Tigeot 	}
2190477eb7f9SFrançois Tigeot 
2191d1c259eeSFrançois Tigeot 	if (!HAS_LLC(dev))
21929edbd4a0SFrançois Tigeot 		dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
2193d1c259eeSFrançois Tigeot 
21949edbd4a0SFrançois Tigeot 	/* Mark any preallocated objects as occupied */
21959edbd4a0SFrançois Tigeot 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
21969edbd4a0SFrançois Tigeot 		struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
21971b13d190SFrançois Tigeot 
21989edbd4a0SFrançois Tigeot 		DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
21999edbd4a0SFrançois Tigeot 			      i915_gem_obj_ggtt_offset(obj), obj->base.size);
2200d1c259eeSFrançois Tigeot 
22019edbd4a0SFrançois Tigeot 		WARN_ON(i915_gem_obj_ggtt_bound(obj));
22029edbd4a0SFrançois Tigeot 		ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
22031b13d190SFrançois Tigeot 		if (ret) {
22041b13d190SFrançois Tigeot 			DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
22051b13d190SFrançois Tigeot 			return ret;
22061b13d190SFrançois Tigeot 		}
22072c9916cdSFrançois Tigeot 		vma->bound |= GLOBAL_BIND;
22089edbd4a0SFrançois Tigeot 	}
22099edbd4a0SFrançois Tigeot 
22109edbd4a0SFrançois Tigeot 	/* Clear any non-preallocated blocks */
22119edbd4a0SFrançois Tigeot 	drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
22129edbd4a0SFrançois Tigeot 		DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
22139edbd4a0SFrançois Tigeot 			      hole_start, hole_end);
2214ba55f2f5SFrançois Tigeot 		ggtt_vm->clear_range(ggtt_vm, hole_start,
2215ba55f2f5SFrançois Tigeot 				     hole_end - hole_start, true);
22169edbd4a0SFrançois Tigeot 	}
2217cb170299SFrançois Tigeot 
2218310880c6SSascha Wildner #ifdef __DragonFly__
2219e084c92dSFrançois Tigeot 	device_printf(dev->dev,
2220e084c92dSFrançois Tigeot 	    "taking over the fictitious range 0x%lx-0x%lx\n",
2221cb170299SFrançois Tigeot 	    dev_priv->gtt.mappable_base + start, dev_priv->gtt.mappable_base + start + mappable);
2222cb170299SFrançois Tigeot 	error = -vm_phys_fictitious_reg_range(dev_priv->gtt.mappable_base + start,
2223cb170299SFrançois Tigeot 	    dev_priv->gtt.mappable_base + start + mappable, VM_MEMATTR_WRITE_COMBINING);
2224310880c6SSascha Wildner #endif
22259edbd4a0SFrançois Tigeot 
22269edbd4a0SFrançois Tigeot 	/* And finally clear the reserved guard page */
2227ba55f2f5SFrançois Tigeot 	ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
22281b13d190SFrançois Tigeot 
22291b13d190SFrançois Tigeot 	if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
22301b13d190SFrançois Tigeot 		struct i915_hw_ppgtt *ppgtt;
22311b13d190SFrançois Tigeot 
22321b13d190SFrançois Tigeot 		ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
22331b13d190SFrançois Tigeot 		if (!ppgtt)
22341b13d190SFrançois Tigeot 			return -ENOMEM;
22351b13d190SFrançois Tigeot 
2236477eb7f9SFrançois Tigeot 		ret = __hw_ppgtt_init(dev, ppgtt, true);
2237477eb7f9SFrançois Tigeot 		if (ret) {
2238*19c468b4SFrançois Tigeot 			ppgtt->base.cleanup(&ppgtt->base);
2239477eb7f9SFrançois Tigeot 			kfree(ppgtt);
22401b13d190SFrançois Tigeot 			return ret;
2241477eb7f9SFrançois Tigeot 		}
22421b13d190SFrançois Tigeot 
22431b13d190SFrançois Tigeot 		dev_priv->mm.aliasing_ppgtt = ppgtt;
22441b13d190SFrançois Tigeot 	}
22451b13d190SFrançois Tigeot 
22461b13d190SFrançois Tigeot 	return 0;
2247a2fdbec6SFrançois Tigeot }
2248a2fdbec6SFrançois Tigeot 
2249a2fdbec6SFrançois Tigeot void i915_gem_init_global_gtt(struct drm_device *dev)
2250a2fdbec6SFrançois Tigeot {
2251a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2252a2fdbec6SFrançois Tigeot 	unsigned long gtt_size, mappable_size;
2253a2fdbec6SFrançois Tigeot 
22549edbd4a0SFrançois Tigeot 	gtt_size = dev_priv->gtt.base.total;
22559edbd4a0SFrançois Tigeot 	mappable_size = dev_priv->gtt.mappable_end;
2256a2fdbec6SFrançois Tigeot 
2257a2fdbec6SFrançois Tigeot 	i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
2258a2fdbec6SFrançois Tigeot }
2259a2fdbec6SFrançois Tigeot 
22601b13d190SFrançois Tigeot void i915_global_gtt_cleanup(struct drm_device *dev)
22611b13d190SFrançois Tigeot {
22621b13d190SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
22631b13d190SFrançois Tigeot 	struct i915_address_space *vm = &dev_priv->gtt.base;
22641b13d190SFrançois Tigeot 
22651b13d190SFrançois Tigeot 	if (dev_priv->mm.aliasing_ppgtt) {
22661b13d190SFrançois Tigeot 		struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
22671b13d190SFrançois Tigeot 
22681b13d190SFrançois Tigeot 		ppgtt->base.cleanup(&ppgtt->base);
22691b13d190SFrançois Tigeot 	}
22701b13d190SFrançois Tigeot 
22711b13d190SFrançois Tigeot 	if (drm_mm_initialized(&vm->mm)) {
2272477eb7f9SFrançois Tigeot 		if (intel_vgpu_active(dev))
2273477eb7f9SFrançois Tigeot 			intel_vgt_deballoon();
2274477eb7f9SFrançois Tigeot 
22751b13d190SFrançois Tigeot 		drm_mm_takedown(&vm->mm);
22761b13d190SFrançois Tigeot 		list_del(&vm->global_link);
22771b13d190SFrançois Tigeot 	}
22781b13d190SFrançois Tigeot 
22791b13d190SFrançois Tigeot 	vm->cleanup(vm);
22801b13d190SFrançois Tigeot }
22811b13d190SFrançois Tigeot 
22829edbd4a0SFrançois Tigeot static int setup_scratch_page(struct drm_device *dev)
22839edbd4a0SFrançois Tigeot {
22849edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
22859edbd4a0SFrançois Tigeot 	struct vm_page *page;
22869edbd4a0SFrançois Tigeot 	dma_addr_t dma_addr;
22879edbd4a0SFrançois Tigeot 
22889edbd4a0SFrançois Tigeot 	page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
22899edbd4a0SFrançois Tigeot 	if (page == NULL)
22909edbd4a0SFrançois Tigeot 		return -ENOMEM;
22919edbd4a0SFrançois Tigeot 	set_pages_uc(page, 1);
22929edbd4a0SFrançois Tigeot 
22939edbd4a0SFrançois Tigeot #ifdef CONFIG_INTEL_IOMMU
22949edbd4a0SFrançois Tigeot 	dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
22959edbd4a0SFrançois Tigeot 				PCI_DMA_BIDIRECTIONAL);
22969edbd4a0SFrançois Tigeot 	if (pci_dma_mapping_error(dev->pdev, dma_addr))
22979edbd4a0SFrançois Tigeot 		return -EINVAL;
22989edbd4a0SFrançois Tigeot #else
22999edbd4a0SFrançois Tigeot 	dma_addr = page_to_phys(page);
23009edbd4a0SFrançois Tigeot #endif
23019edbd4a0SFrançois Tigeot 	dev_priv->gtt.base.scratch.page = page;
23029edbd4a0SFrançois Tigeot 	dev_priv->gtt.base.scratch.addr = dma_addr;
23039edbd4a0SFrançois Tigeot 
23049edbd4a0SFrançois Tigeot 	return 0;
23059edbd4a0SFrançois Tigeot }
23069edbd4a0SFrançois Tigeot 
23079edbd4a0SFrançois Tigeot #if 0
23089edbd4a0SFrançois Tigeot static void teardown_scratch_page(struct drm_device *dev)
23099edbd4a0SFrançois Tigeot {
23109edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2311ba55f2f5SFrançois Tigeot 	struct vm_page *page = dev_priv->gtt.base.scratch.page;
23129edbd4a0SFrançois Tigeot 
23139edbd4a0SFrançois Tigeot 	set_pages_wb(page, 1);
23149edbd4a0SFrançois Tigeot 	pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
23159edbd4a0SFrançois Tigeot 		       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
23169edbd4a0SFrançois Tigeot 	__free_page(page);
23179edbd4a0SFrançois Tigeot }
23189edbd4a0SFrançois Tigeot #endif
23199edbd4a0SFrançois Tigeot 
2320*19c468b4SFrançois Tigeot static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
23219edbd4a0SFrançois Tigeot {
23229edbd4a0SFrançois Tigeot 	snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
23239edbd4a0SFrançois Tigeot 	snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
23249edbd4a0SFrançois Tigeot 	return snb_gmch_ctl << 20;
23259edbd4a0SFrançois Tigeot }
23269edbd4a0SFrançois Tigeot 
2327*19c468b4SFrançois Tigeot static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
23289edbd4a0SFrançois Tigeot {
23299edbd4a0SFrançois Tigeot 	bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
23309edbd4a0SFrançois Tigeot 	bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
23319edbd4a0SFrançois Tigeot 	if (bdw_gmch_ctl)
23329edbd4a0SFrançois Tigeot 		bdw_gmch_ctl = 1 << bdw_gmch_ctl;
2333ba55f2f5SFrançois Tigeot 
2334ba55f2f5SFrançois Tigeot #ifdef CONFIG_X86_32
2335ba55f2f5SFrançois Tigeot 	/* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2336ba55f2f5SFrançois Tigeot 	if (bdw_gmch_ctl > 4)
2337ba55f2f5SFrançois Tigeot 		bdw_gmch_ctl = 4;
2338ba55f2f5SFrançois Tigeot #endif
23399edbd4a0SFrançois Tigeot 
23409edbd4a0SFrançois Tigeot 	return bdw_gmch_ctl << 20;
23419edbd4a0SFrançois Tigeot }
23429edbd4a0SFrançois Tigeot 
2343*19c468b4SFrançois Tigeot static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2344ba55f2f5SFrançois Tigeot {
2345ba55f2f5SFrançois Tigeot 	gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2346ba55f2f5SFrançois Tigeot 	gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2347ba55f2f5SFrançois Tigeot 
2348ba55f2f5SFrançois Tigeot 	if (gmch_ctrl)
2349ba55f2f5SFrançois Tigeot 		return 1 << (20 + gmch_ctrl);
2350ba55f2f5SFrançois Tigeot 
2351ba55f2f5SFrançois Tigeot 	return 0;
2352ba55f2f5SFrançois Tigeot }
2353ba55f2f5SFrançois Tigeot 
2354*19c468b4SFrançois Tigeot static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
23559edbd4a0SFrançois Tigeot {
23569edbd4a0SFrançois Tigeot 	snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
23579edbd4a0SFrançois Tigeot 	snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
23589edbd4a0SFrançois Tigeot 	return snb_gmch_ctl << 25; /* 32 MB units */
23599edbd4a0SFrançois Tigeot }
23609edbd4a0SFrançois Tigeot 
2361*19c468b4SFrançois Tigeot static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
23629edbd4a0SFrançois Tigeot {
23639edbd4a0SFrançois Tigeot 	bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
23649edbd4a0SFrançois Tigeot 	bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
23659edbd4a0SFrançois Tigeot 	return bdw_gmch_ctl << 25; /* 32 MB units */
23669edbd4a0SFrançois Tigeot }
23679edbd4a0SFrançois Tigeot 
2368ba55f2f5SFrançois Tigeot static size_t chv_get_stolen_size(u16 gmch_ctrl)
2369ba55f2f5SFrançois Tigeot {
2370ba55f2f5SFrançois Tigeot 	gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2371ba55f2f5SFrançois Tigeot 	gmch_ctrl &= SNB_GMCH_GMS_MASK;
2372ba55f2f5SFrançois Tigeot 
2373ba55f2f5SFrançois Tigeot 	/*
2374ba55f2f5SFrançois Tigeot 	 * 0x0  to 0x10: 32MB increments starting at 0MB
2375ba55f2f5SFrançois Tigeot 	 * 0x11 to 0x16: 4MB increments starting at 8MB
2376ba55f2f5SFrançois Tigeot 	 * 0x17 to 0x1d: 4MB increments start at 36MB
2377ba55f2f5SFrançois Tigeot 	 */
2378ba55f2f5SFrançois Tigeot 	if (gmch_ctrl < 0x11)
2379ba55f2f5SFrançois Tigeot 		return gmch_ctrl << 25;
2380ba55f2f5SFrançois Tigeot 	else if (gmch_ctrl < 0x17)
2381ba55f2f5SFrançois Tigeot 		return (gmch_ctrl - 0x11 + 2) << 22;
2382ba55f2f5SFrançois Tigeot 	else
2383ba55f2f5SFrançois Tigeot 		return (gmch_ctrl - 0x17 + 9) << 22;
2384ba55f2f5SFrançois Tigeot }
2385ba55f2f5SFrançois Tigeot 
23862c9916cdSFrançois Tigeot static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
23872c9916cdSFrançois Tigeot {
23882c9916cdSFrançois Tigeot 	gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
23892c9916cdSFrançois Tigeot 	gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
23902c9916cdSFrançois Tigeot 
23912c9916cdSFrançois Tigeot 	if (gen9_gmch_ctl < 0xf0)
23922c9916cdSFrançois Tigeot 		return gen9_gmch_ctl << 25; /* 32 MB units */
23932c9916cdSFrançois Tigeot 	else
23942c9916cdSFrançois Tigeot 		/* 4MB increments starting at 0xf0 for 4MB */
23952c9916cdSFrançois Tigeot 		return (gen9_gmch_ctl - 0xf0 + 1) << 22;
23962c9916cdSFrançois Tigeot }
23972c9916cdSFrançois Tigeot 
23989edbd4a0SFrançois Tigeot static int ggtt_probe_common(struct drm_device *dev,
23999edbd4a0SFrançois Tigeot 			     size_t gtt_size)
24009edbd4a0SFrançois Tigeot {
24019edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
24029edbd4a0SFrançois Tigeot 	phys_addr_t gtt_phys_addr;
24039edbd4a0SFrançois Tigeot 	int ret;
24049edbd4a0SFrançois Tigeot 
24059edbd4a0SFrançois Tigeot 	/* For Modern GENs the PTEs and register space are split in the BAR */
24069edbd4a0SFrançois Tigeot 	gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
24079edbd4a0SFrançois Tigeot 		(pci_resource_len(dev->pdev, 0) / 2);
24089edbd4a0SFrançois Tigeot 
2409*19c468b4SFrançois Tigeot 	/*
2410*19c468b4SFrançois Tigeot 	 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2411*19c468b4SFrançois Tigeot 	 * dropped. For WC mappings in general we have 64 byte burst writes
2412*19c468b4SFrançois Tigeot 	 * when the WC buffer is flushed, so we can't use it, but have to
2413*19c468b4SFrançois Tigeot 	 * resort to an uncached mapping. The WC issue is easily caught by the
2414*19c468b4SFrançois Tigeot 	 * readback check when writing GTT PTE entries.
2415*19c468b4SFrançois Tigeot 	 */
2416*19c468b4SFrançois Tigeot 	if (IS_BROXTON(dev))
2417*19c468b4SFrançois Tigeot 		dev_priv->gtt.gsm = ioremap_nocache(gtt_phys_addr, gtt_size);
2418*19c468b4SFrançois Tigeot 	else
24199edbd4a0SFrançois Tigeot 		dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
24209edbd4a0SFrançois Tigeot 	if (!dev_priv->gtt.gsm) {
24219edbd4a0SFrançois Tigeot 		DRM_ERROR("Failed to map the gtt page table\n");
24229edbd4a0SFrançois Tigeot 		return -ENOMEM;
24239edbd4a0SFrançois Tigeot 	}
24249edbd4a0SFrançois Tigeot 
24259edbd4a0SFrançois Tigeot 	ret = setup_scratch_page(dev);
24269edbd4a0SFrançois Tigeot 	if (ret) {
24279edbd4a0SFrançois Tigeot 		DRM_ERROR("Scratch setup failed\n");
24289edbd4a0SFrançois Tigeot 		/* iounmap will also get called at remove, but meh */
24299edbd4a0SFrançois Tigeot #if 0
24309edbd4a0SFrançois Tigeot 		iounmap(dev_priv->gtt.gsm);
24319edbd4a0SFrançois Tigeot #endif
24329edbd4a0SFrançois Tigeot 	}
24339edbd4a0SFrançois Tigeot 
24349edbd4a0SFrançois Tigeot 	return ret;
24359edbd4a0SFrançois Tigeot }
24369edbd4a0SFrançois Tigeot 
24379edbd4a0SFrançois Tigeot /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
24389edbd4a0SFrançois Tigeot  * bits. When using advanced contexts each context stores its own PAT, but
24399edbd4a0SFrançois Tigeot  * writing this data shouldn't be harmful even in those cases. */
2440ba55f2f5SFrançois Tigeot static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
24419edbd4a0SFrançois Tigeot {
24429edbd4a0SFrançois Tigeot 	uint64_t pat;
24439edbd4a0SFrançois Tigeot 
24449edbd4a0SFrançois Tigeot 	pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC)     | /* for normal objects, no eLLC */
24459edbd4a0SFrançois Tigeot 	      GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
24469edbd4a0SFrançois Tigeot 	      GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
24479edbd4a0SFrançois Tigeot 	      GEN8_PPAT(3, GEN8_PPAT_UC)                     | /* Uncached objects, mostly for scanout */
24489edbd4a0SFrançois Tigeot 	      GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
24499edbd4a0SFrançois Tigeot 	      GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
24509edbd4a0SFrançois Tigeot 	      GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
24519edbd4a0SFrançois Tigeot 	      GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
24529edbd4a0SFrançois Tigeot 
2453a6e033d9SFrançois Tigeot 	if (!USES_PPGTT(dev_priv->dev))
2454a6e033d9SFrançois Tigeot 		/* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2455a6e033d9SFrançois Tigeot 		 * so RTL will always use the value corresponding to
2456a6e033d9SFrançois Tigeot 		 * pat_sel = 000".
2457a6e033d9SFrançois Tigeot 		 * So let's disable cache for GGTT to avoid screen corruptions.
2458a6e033d9SFrançois Tigeot 		 * MOCS still can be used though.
2459a6e033d9SFrançois Tigeot 		 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2460a6e033d9SFrançois Tigeot 		 * before this patch, i.e. the same uncached + snooping access
2461a6e033d9SFrançois Tigeot 		 * like on gen6/7 seems to be in effect.
2462a6e033d9SFrançois Tigeot 		 * - So this just fixes blitter/render access. Again it looks
2463a6e033d9SFrançois Tigeot 		 * like it's not just uncached access, but uncached + snooping.
2464a6e033d9SFrançois Tigeot 		 * So we can still hold onto all our assumptions wrt cpu
2465a6e033d9SFrançois Tigeot 		 * clflushing on LLC machines.
2466a6e033d9SFrançois Tigeot 		 */
2467a6e033d9SFrançois Tigeot 		pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2468a6e033d9SFrançois Tigeot 
24699edbd4a0SFrançois Tigeot 	/* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
24709edbd4a0SFrançois Tigeot 	 * write would work. */
24719edbd4a0SFrançois Tigeot 	I915_WRITE(GEN8_PRIVATE_PAT, pat);
24729edbd4a0SFrançois Tigeot 	I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
24739edbd4a0SFrançois Tigeot }
24749edbd4a0SFrançois Tigeot 
2475ba55f2f5SFrançois Tigeot static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2476ba55f2f5SFrançois Tigeot {
2477ba55f2f5SFrançois Tigeot 	uint64_t pat;
2478ba55f2f5SFrançois Tigeot 
2479ba55f2f5SFrançois Tigeot 	/*
2480ba55f2f5SFrançois Tigeot 	 * Map WB on BDW to snooped on CHV.
2481ba55f2f5SFrançois Tigeot 	 *
2482ba55f2f5SFrançois Tigeot 	 * Only the snoop bit has meaning for CHV, the rest is
2483ba55f2f5SFrançois Tigeot 	 * ignored.
2484ba55f2f5SFrançois Tigeot 	 *
24852c9916cdSFrançois Tigeot 	 * The hardware will never snoop for certain types of accesses:
24862c9916cdSFrançois Tigeot 	 * - CPU GTT (GMADR->GGTT->no snoop->memory)
24872c9916cdSFrançois Tigeot 	 * - PPGTT page tables
24882c9916cdSFrançois Tigeot 	 * - some other special cycles
24892c9916cdSFrançois Tigeot 	 *
24902c9916cdSFrançois Tigeot 	 * As with BDW, we also need to consider the following for GT accesses:
24912c9916cdSFrançois Tigeot 	 * "For GGTT, there is NO pat_sel[2:0] from the entry,
24922c9916cdSFrançois Tigeot 	 * so RTL will always use the value corresponding to
24932c9916cdSFrançois Tigeot 	 * pat_sel = 000".
24942c9916cdSFrançois Tigeot 	 * Which means we must set the snoop bit in PAT entry 0
24952c9916cdSFrançois Tigeot 	 * in order to keep the global status page working.
2496ba55f2f5SFrançois Tigeot 	 */
2497ba55f2f5SFrançois Tigeot 	pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2498ba55f2f5SFrançois Tigeot 	      GEN8_PPAT(1, 0) |
2499ba55f2f5SFrançois Tigeot 	      GEN8_PPAT(2, 0) |
2500ba55f2f5SFrançois Tigeot 	      GEN8_PPAT(3, 0) |
2501ba55f2f5SFrançois Tigeot 	      GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2502ba55f2f5SFrançois Tigeot 	      GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2503ba55f2f5SFrançois Tigeot 	      GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2504ba55f2f5SFrançois Tigeot 	      GEN8_PPAT(7, CHV_PPAT_SNOOP);
2505ba55f2f5SFrançois Tigeot 
2506ba55f2f5SFrançois Tigeot 	I915_WRITE(GEN8_PRIVATE_PAT, pat);
2507ba55f2f5SFrançois Tigeot 	I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2508ba55f2f5SFrançois Tigeot }
2509ba55f2f5SFrançois Tigeot 
25109edbd4a0SFrançois Tigeot static int gen8_gmch_probe(struct drm_device *dev,
25119edbd4a0SFrançois Tigeot 			   size_t *gtt_total,
25129edbd4a0SFrançois Tigeot 			   size_t *stolen,
25139edbd4a0SFrançois Tigeot 			   phys_addr_t *mappable_base,
25149edbd4a0SFrançois Tigeot 			   unsigned long *mappable_end)
25159edbd4a0SFrançois Tigeot {
25169edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
25179edbd4a0SFrançois Tigeot 	unsigned int gtt_size;
25189edbd4a0SFrançois Tigeot 	u16 snb_gmch_ctl;
25199edbd4a0SFrançois Tigeot 	int ret;
25209edbd4a0SFrançois Tigeot 
25219edbd4a0SFrançois Tigeot 	/* TODO: We're not aware of mappable constraints on gen8 yet */
25229edbd4a0SFrançois Tigeot 	*mappable_base = pci_resource_start(dev->pdev, 2);
25239edbd4a0SFrançois Tigeot 	*mappable_end = pci_resource_len(dev->pdev, 2);
25249edbd4a0SFrançois Tigeot 
25259edbd4a0SFrançois Tigeot #if 0
25269edbd4a0SFrançois Tigeot 	if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
25279edbd4a0SFrançois Tigeot 		pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
25289edbd4a0SFrançois Tigeot #endif
25299edbd4a0SFrançois Tigeot 
25309edbd4a0SFrançois Tigeot 	pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
25319edbd4a0SFrançois Tigeot 
25322c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 9) {
25332c9916cdSFrançois Tigeot 		*stolen = gen9_get_stolen_size(snb_gmch_ctl);
25342c9916cdSFrançois Tigeot 		gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
25352c9916cdSFrançois Tigeot 	} else if (IS_CHERRYVIEW(dev)) {
2536ba55f2f5SFrançois Tigeot 		*stolen = chv_get_stolen_size(snb_gmch_ctl);
2537ba55f2f5SFrançois Tigeot 		gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
2538ba55f2f5SFrançois Tigeot 	} else {
25399edbd4a0SFrançois Tigeot 		*stolen = gen8_get_stolen_size(snb_gmch_ctl);
25409edbd4a0SFrançois Tigeot 		gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2541ba55f2f5SFrançois Tigeot 	}
2542ba55f2f5SFrançois Tigeot 
2543477eb7f9SFrançois Tigeot 	*gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
25449edbd4a0SFrançois Tigeot 
2545*19c468b4SFrançois Tigeot 	if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
2546ba55f2f5SFrançois Tigeot 		chv_setup_private_ppat(dev_priv);
2547ba55f2f5SFrançois Tigeot 	else
2548ba55f2f5SFrançois Tigeot 		bdw_setup_private_ppat(dev_priv);
25499edbd4a0SFrançois Tigeot 
25509edbd4a0SFrançois Tigeot 	ret = ggtt_probe_common(dev, gtt_size);
25519edbd4a0SFrançois Tigeot 
25529edbd4a0SFrançois Tigeot 	dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
25539edbd4a0SFrançois Tigeot 	dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
2554*19c468b4SFrançois Tigeot 	dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
2555*19c468b4SFrançois Tigeot 	dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
25569edbd4a0SFrançois Tigeot 
25579edbd4a0SFrançois Tigeot 	return ret;
25589edbd4a0SFrançois Tigeot }
25599edbd4a0SFrançois Tigeot 
25605d0b1887SFrançois Tigeot static int gen6_gmch_probe(struct drm_device *dev,
25615d0b1887SFrançois Tigeot 			   size_t *gtt_total,
25625d0b1887SFrançois Tigeot 			   size_t *stolen,
25635d0b1887SFrançois Tigeot 			   phys_addr_t *mappable_base,
25645d0b1887SFrançois Tigeot 			   unsigned long *mappable_end)
25655d0b1887SFrançois Tigeot {
25665d0b1887SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
25675d0b1887SFrançois Tigeot 	unsigned int gtt_size;
25685d0b1887SFrançois Tigeot 	u16 snb_gmch_ctl;
25695d0b1887SFrançois Tigeot 	int ret;
25705d0b1887SFrançois Tigeot 
25715d0b1887SFrançois Tigeot 	*mappable_base = pci_resource_start(dev->pdev, 2);
25725d0b1887SFrançois Tigeot 	*mappable_end = pci_resource_len(dev->pdev, 2);
25735d0b1887SFrançois Tigeot 
25745d0b1887SFrançois Tigeot 	/* 64/512MB is the current min/max we actually know of, but this is just
25755d0b1887SFrançois Tigeot 	 * a coarse sanity check.
25765d0b1887SFrançois Tigeot 	 */
25775d0b1887SFrançois Tigeot 	if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
25785d0b1887SFrançois Tigeot 		DRM_ERROR("Unknown GMADR size (%lx)\n",
25795d0b1887SFrançois Tigeot 			  dev_priv->gtt.mappable_end);
25805d0b1887SFrançois Tigeot 		return -ENXIO;
25815d0b1887SFrançois Tigeot 	}
25825d0b1887SFrançois Tigeot 
25839edbd4a0SFrançois Tigeot #if 0
25845d0b1887SFrançois Tigeot 	if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
25855d0b1887SFrançois Tigeot 		pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
25869edbd4a0SFrançois Tigeot #endif
25875d0b1887SFrançois Tigeot 	pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
25885d0b1887SFrançois Tigeot 
25895d0b1887SFrançois Tigeot 	*stolen = gen6_get_stolen_size(snb_gmch_ctl);
25905d0b1887SFrançois Tigeot 
25919edbd4a0SFrançois Tigeot 	gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
2592477eb7f9SFrançois Tigeot 	*gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
25935d0b1887SFrançois Tigeot 
25949edbd4a0SFrançois Tigeot 	ret = ggtt_probe_common(dev, gtt_size);
25955d0b1887SFrançois Tigeot 
25969edbd4a0SFrançois Tigeot 	dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
25979edbd4a0SFrançois Tigeot 	dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
2598*19c468b4SFrançois Tigeot 	dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
2599*19c468b4SFrançois Tigeot 	dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
26009edbd4a0SFrançois Tigeot 
26019edbd4a0SFrançois Tigeot 	return ret;
26025d0b1887SFrançois Tigeot }
26035d0b1887SFrançois Tigeot 
26049edbd4a0SFrançois Tigeot static void gen6_gmch_remove(struct i915_address_space *vm)
26055d0b1887SFrançois Tigeot {
26069edbd4a0SFrançois Tigeot #if 0
26079edbd4a0SFrançois Tigeot 	struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
26089edbd4a0SFrançois Tigeot 
26099edbd4a0SFrançois Tigeot 	iounmap(gtt->gsm);
26109edbd4a0SFrançois Tigeot 	teardown_scratch_page(vm->dev);
26115d0b1887SFrançois Tigeot #endif
26129edbd4a0SFrançois Tigeot }
26135d0b1887SFrançois Tigeot 
26145d0b1887SFrançois Tigeot static int i915_gmch_probe(struct drm_device *dev,
26155d0b1887SFrançois Tigeot 			   size_t *gtt_total,
26165d0b1887SFrançois Tigeot 			   size_t *stolen,
26175d0b1887SFrançois Tigeot 			   phys_addr_t *mappable_base,
26185d0b1887SFrançois Tigeot 			   unsigned long *mappable_end)
26195d0b1887SFrançois Tigeot {
26209edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
26219edbd4a0SFrançois Tigeot #if 0
26229edbd4a0SFrançois Tigeot 	int ret;
26239edbd4a0SFrançois Tigeot 
26249edbd4a0SFrançois Tigeot 	ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
26259edbd4a0SFrançois Tigeot 	if (!ret) {
26269edbd4a0SFrançois Tigeot 		DRM_ERROR("failed to set up gmch\n");
26279edbd4a0SFrançois Tigeot 		return -EIO;
26289edbd4a0SFrançois Tigeot 	}
26299edbd4a0SFrançois Tigeot #endif
26309edbd4a0SFrançois Tigeot 
26319edbd4a0SFrançois Tigeot 	intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
26329edbd4a0SFrançois Tigeot 
26339edbd4a0SFrançois Tigeot 	dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
26349edbd4a0SFrançois Tigeot 	dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
2635*19c468b4SFrançois Tigeot 	dev_priv->gtt.base.bind_vma = i915_ggtt_bind_vma;
2636*19c468b4SFrançois Tigeot 	dev_priv->gtt.base.unbind_vma = i915_ggtt_unbind_vma;
26379edbd4a0SFrançois Tigeot 
26389edbd4a0SFrançois Tigeot 	if (unlikely(dev_priv->gtt.do_idle_maps))
26399edbd4a0SFrançois Tigeot 		DRM_INFO("applying Ironlake quirks for intel_iommu\n");
26409edbd4a0SFrançois Tigeot 
26415d0b1887SFrançois Tigeot 	return 0;
26425d0b1887SFrançois Tigeot }
26435d0b1887SFrançois Tigeot 
26449edbd4a0SFrançois Tigeot static void i915_gmch_remove(struct i915_address_space *vm)
26455d0b1887SFrançois Tigeot {
264624edb884SFrançois Tigeot 	intel_gmch_remove();
26475d0b1887SFrançois Tigeot }
26485d0b1887SFrançois Tigeot 
26490b869d8aSFrançois Tigeot int i915_gem_gtt_init(struct drm_device *dev)
26500b869d8aSFrançois Tigeot {
26510b869d8aSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
26529edbd4a0SFrançois Tigeot 	struct i915_gtt *gtt = &dev_priv->gtt;
26539edbd4a0SFrançois Tigeot 	int ret;
26540b869d8aSFrançois Tigeot 
26559edbd4a0SFrançois Tigeot 	if (INTEL_INFO(dev)->gen <= 5) {
26569edbd4a0SFrançois Tigeot 		gtt->gtt_probe = i915_gmch_probe;
26579edbd4a0SFrançois Tigeot 		gtt->base.cleanup = i915_gmch_remove;
26589edbd4a0SFrançois Tigeot 	} else if (INTEL_INFO(dev)->gen < 8) {
26599edbd4a0SFrançois Tigeot 		gtt->gtt_probe = gen6_gmch_probe;
26609edbd4a0SFrançois Tigeot 		gtt->base.cleanup = gen6_gmch_remove;
26619edbd4a0SFrançois Tigeot 		if (IS_HASWELL(dev) && dev_priv->ellc_size)
26629edbd4a0SFrançois Tigeot 			gtt->base.pte_encode = iris_pte_encode;
26639edbd4a0SFrançois Tigeot 		else if (IS_HASWELL(dev))
26649edbd4a0SFrançois Tigeot 			gtt->base.pte_encode = hsw_pte_encode;
26659edbd4a0SFrançois Tigeot 		else if (IS_VALLEYVIEW(dev))
26669edbd4a0SFrançois Tigeot 			gtt->base.pte_encode = byt_pte_encode;
26679edbd4a0SFrançois Tigeot 		else if (INTEL_INFO(dev)->gen >= 7)
26689edbd4a0SFrançois Tigeot 			gtt->base.pte_encode = ivb_pte_encode;
26699edbd4a0SFrançois Tigeot 		else
26709edbd4a0SFrançois Tigeot 			gtt->base.pte_encode = snb_pte_encode;
26715d0b1887SFrançois Tigeot 	} else {
26729edbd4a0SFrançois Tigeot 		dev_priv->gtt.gtt_probe = gen8_gmch_probe;
26739edbd4a0SFrançois Tigeot 		dev_priv->gtt.base.cleanup = gen6_gmch_remove;
26745d0b1887SFrançois Tigeot 	}
2675a2fdbec6SFrançois Tigeot 
26769edbd4a0SFrançois Tigeot 	ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
26779edbd4a0SFrançois Tigeot 			     &gtt->mappable_base, &gtt->mappable_end);
26789edbd4a0SFrançois Tigeot 	if (ret)
26799edbd4a0SFrançois Tigeot 		return ret;
26800b869d8aSFrançois Tigeot 
26819edbd4a0SFrançois Tigeot 	gtt->base.dev = dev;
26820b869d8aSFrançois Tigeot 
26835d0b1887SFrançois Tigeot 	/* GMADR is the PCI mmio aperture into the global GTT. */
26845d0b1887SFrançois Tigeot 	DRM_INFO("Memory usable by graphics device = %zdM\n",
26859edbd4a0SFrançois Tigeot 		 gtt->base.total >> 20);
26869edbd4a0SFrançois Tigeot 	DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
26879edbd4a0SFrançois Tigeot 	DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
2688ba55f2f5SFrançois Tigeot #ifdef CONFIG_INTEL_IOMMU
2689ba55f2f5SFrançois Tigeot 	if (intel_iommu_gfx_mapped)
2690ba55f2f5SFrançois Tigeot 		DRM_INFO("VT-d active for gfx access\n");
2691ba55f2f5SFrançois Tigeot #endif
269224edb884SFrançois Tigeot 	/*
269324edb884SFrançois Tigeot 	 * i915.enable_ppgtt is read-only, so do an early pass to validate the
269424edb884SFrançois Tigeot 	 * user's requested state against the hardware/driver capabilities.  We
269524edb884SFrançois Tigeot 	 * do this now so that we can print out any log messages once rather
269624edb884SFrançois Tigeot 	 * than every time we check intel_enable_ppgtt().
269724edb884SFrançois Tigeot 	 */
269824edb884SFrançois Tigeot 	i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
269924edb884SFrançois Tigeot 	DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
2700a2fdbec6SFrançois Tigeot 
27010b869d8aSFrançois Tigeot 	return 0;
27020b869d8aSFrançois Tigeot }
2703ba55f2f5SFrançois Tigeot 
2704*19c468b4SFrançois Tigeot void i915_gem_restore_gtt_mappings(struct drm_device *dev)
2705*19c468b4SFrançois Tigeot {
2706*19c468b4SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2707*19c468b4SFrançois Tigeot 	struct drm_i915_gem_object *obj;
2708*19c468b4SFrançois Tigeot 	struct i915_address_space *vm;
2709*19c468b4SFrançois Tigeot 	struct i915_vma *vma;
2710*19c468b4SFrançois Tigeot 	bool flush;
2711*19c468b4SFrançois Tigeot 
2712*19c468b4SFrançois Tigeot 	i915_check_and_clear_faults(dev);
2713*19c468b4SFrançois Tigeot 
2714*19c468b4SFrançois Tigeot 	/* First fill our portion of the GTT with scratch pages */
2715*19c468b4SFrançois Tigeot 	dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
2716*19c468b4SFrançois Tigeot 				       dev_priv->gtt.base.start,
2717*19c468b4SFrançois Tigeot 				       dev_priv->gtt.base.total,
2718*19c468b4SFrançois Tigeot 				       true);
2719*19c468b4SFrançois Tigeot 
2720*19c468b4SFrançois Tigeot 	/* Cache flush objects bound into GGTT and rebind them. */
2721*19c468b4SFrançois Tigeot 	vm = &dev_priv->gtt.base;
2722*19c468b4SFrançois Tigeot 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
2723*19c468b4SFrançois Tigeot 		flush = false;
2724*19c468b4SFrançois Tigeot 		list_for_each_entry(vma, &obj->vma_list, vma_link) {
2725*19c468b4SFrançois Tigeot 			if (vma->vm != vm)
2726*19c468b4SFrançois Tigeot 				continue;
2727*19c468b4SFrançois Tigeot 
2728*19c468b4SFrançois Tigeot 			WARN_ON(i915_vma_bind(vma, obj->cache_level,
2729*19c468b4SFrançois Tigeot 					      PIN_UPDATE));
2730*19c468b4SFrançois Tigeot 
2731*19c468b4SFrançois Tigeot 			flush = true;
2732*19c468b4SFrançois Tigeot 		}
2733*19c468b4SFrançois Tigeot 
2734*19c468b4SFrançois Tigeot 		if (flush)
2735*19c468b4SFrançois Tigeot 			i915_gem_clflush_object(obj, obj->pin_display);
2736*19c468b4SFrançois Tigeot 	}
2737*19c468b4SFrançois Tigeot 
2738*19c468b4SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 8) {
2739*19c468b4SFrançois Tigeot 		if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
2740*19c468b4SFrançois Tigeot 			chv_setup_private_ppat(dev_priv);
2741*19c468b4SFrançois Tigeot 		else
2742*19c468b4SFrançois Tigeot 			bdw_setup_private_ppat(dev_priv);
2743*19c468b4SFrançois Tigeot 
2744*19c468b4SFrançois Tigeot 		return;
2745*19c468b4SFrançois Tigeot 	}
2746*19c468b4SFrançois Tigeot 
2747*19c468b4SFrançois Tigeot 	if (USES_PPGTT(dev)) {
2748*19c468b4SFrançois Tigeot 		list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
2749*19c468b4SFrançois Tigeot 			/* TODO: Perhaps it shouldn't be gen6 specific */
2750*19c468b4SFrançois Tigeot 
2751*19c468b4SFrançois Tigeot 			struct i915_hw_ppgtt *ppgtt =
2752*19c468b4SFrançois Tigeot 					container_of(vm, struct i915_hw_ppgtt,
2753*19c468b4SFrançois Tigeot 						     base);
2754*19c468b4SFrançois Tigeot 
2755*19c468b4SFrançois Tigeot 			if (i915_is_ggtt(vm))
2756*19c468b4SFrançois Tigeot 				ppgtt = dev_priv->mm.aliasing_ppgtt;
2757*19c468b4SFrançois Tigeot 
2758*19c468b4SFrançois Tigeot 			gen6_write_page_range(dev_priv, &ppgtt->pd,
2759*19c468b4SFrançois Tigeot 					      0, ppgtt->base.total);
2760*19c468b4SFrançois Tigeot 		}
2761*19c468b4SFrançois Tigeot 	}
2762*19c468b4SFrançois Tigeot 
2763*19c468b4SFrançois Tigeot 	i915_ggtt_flush(dev_priv);
2764*19c468b4SFrançois Tigeot }
2765*19c468b4SFrançois Tigeot 
2766477eb7f9SFrançois Tigeot static struct i915_vma *
2767477eb7f9SFrançois Tigeot __i915_gem_vma_create(struct drm_i915_gem_object *obj,
27682c9916cdSFrançois Tigeot 		      struct i915_address_space *vm,
2769477eb7f9SFrançois Tigeot 		      const struct i915_ggtt_view *ggtt_view)
2770ba55f2f5SFrançois Tigeot {
2771477eb7f9SFrançois Tigeot 	struct i915_vma *vma;
2772477eb7f9SFrançois Tigeot 
2773477eb7f9SFrançois Tigeot 	if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
2774477eb7f9SFrançois Tigeot 		return ERR_PTR(-EINVAL);
2775477eb7f9SFrançois Tigeot 	vma = kzalloc(sizeof(*vma), GFP_KERNEL);
2776ba55f2f5SFrançois Tigeot 	if (vma == NULL)
2777ba55f2f5SFrançois Tigeot 		return ERR_PTR(-ENOMEM);
2778ba55f2f5SFrançois Tigeot 
2779ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&vma->vma_link);
2780ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&vma->mm_list);
2781ba55f2f5SFrançois Tigeot 	INIT_LIST_HEAD(&vma->exec_list);
2782ba55f2f5SFrançois Tigeot 	vma->vm = vm;
2783ba55f2f5SFrançois Tigeot 	vma->obj = obj;
2784ba55f2f5SFrançois Tigeot 
2785*19c468b4SFrançois Tigeot 	if (i915_is_ggtt(vm))
2786477eb7f9SFrançois Tigeot 		vma->ggtt_view = *ggtt_view;
2787477eb7f9SFrançois Tigeot 
2788ba55f2f5SFrançois Tigeot 	list_add_tail(&vma->vma_link, &obj->vma_list);
27892c9916cdSFrançois Tigeot 	if (!i915_is_ggtt(vm))
27901b13d190SFrançois Tigeot 		i915_ppgtt_get(i915_vm_to_ppgtt(vm));
2791ba55f2f5SFrançois Tigeot 
2792ba55f2f5SFrançois Tigeot 	return vma;
2793ba55f2f5SFrançois Tigeot }
2794ba55f2f5SFrançois Tigeot 
2795ba55f2f5SFrançois Tigeot struct i915_vma *
2796477eb7f9SFrançois Tigeot i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2797477eb7f9SFrançois Tigeot 				  struct i915_address_space *vm)
2798ba55f2f5SFrançois Tigeot {
2799ba55f2f5SFrançois Tigeot 	struct i915_vma *vma;
2800ba55f2f5SFrançois Tigeot 
2801477eb7f9SFrançois Tigeot 	vma = i915_gem_obj_to_vma(obj, vm);
2802ba55f2f5SFrançois Tigeot 	if (!vma)
2803477eb7f9SFrançois Tigeot 		vma = __i915_gem_vma_create(obj, vm,
2804477eb7f9SFrançois Tigeot 					    i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
2805ba55f2f5SFrançois Tigeot 
2806ba55f2f5SFrançois Tigeot 	return vma;
2807ba55f2f5SFrançois Tigeot }
28082c9916cdSFrançois Tigeot 
2809477eb7f9SFrançois Tigeot struct i915_vma *
2810477eb7f9SFrançois Tigeot i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
2811477eb7f9SFrançois Tigeot 				       const struct i915_ggtt_view *view)
28122c9916cdSFrançois Tigeot {
2813477eb7f9SFrançois Tigeot 	struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
2814477eb7f9SFrançois Tigeot 	struct i915_vma *vma;
2815477eb7f9SFrançois Tigeot 
2816477eb7f9SFrançois Tigeot 	if (WARN_ON(!view))
2817477eb7f9SFrançois Tigeot 		return ERR_PTR(-EINVAL);
2818477eb7f9SFrançois Tigeot 
2819477eb7f9SFrançois Tigeot 	vma = i915_gem_obj_to_ggtt_view(obj, view);
2820477eb7f9SFrançois Tigeot 
2821477eb7f9SFrançois Tigeot 	if (IS_ERR(vma))
2822477eb7f9SFrançois Tigeot 		return vma;
2823477eb7f9SFrançois Tigeot 
2824477eb7f9SFrançois Tigeot 	if (!vma)
2825477eb7f9SFrançois Tigeot 		vma = __i915_gem_vma_create(obj, ggtt, view);
2826477eb7f9SFrançois Tigeot 
2827477eb7f9SFrançois Tigeot 	return vma;
2828477eb7f9SFrançois Tigeot 
2829477eb7f9SFrançois Tigeot }
2830477eb7f9SFrançois Tigeot 
2831477eb7f9SFrançois Tigeot #if 0
2832477eb7f9SFrançois Tigeot static void
2833477eb7f9SFrançois Tigeot rotate_pages(dma_addr_t *in, unsigned int width, unsigned int height,
2834477eb7f9SFrançois Tigeot 	     struct sg_table *st)
2835477eb7f9SFrançois Tigeot {
2836477eb7f9SFrançois Tigeot 	unsigned int column, row;
2837477eb7f9SFrançois Tigeot 	unsigned int src_idx;
2838477eb7f9SFrançois Tigeot 	struct scatterlist *sg = st->sgl;
2839477eb7f9SFrançois Tigeot 
2840477eb7f9SFrançois Tigeot 	st->nents = 0;
2841477eb7f9SFrançois Tigeot 
2842477eb7f9SFrançois Tigeot 	for (column = 0; column < width; column++) {
2843477eb7f9SFrançois Tigeot 		src_idx = width * (height - 1) + column;
2844477eb7f9SFrançois Tigeot 		for (row = 0; row < height; row++) {
2845477eb7f9SFrançois Tigeot 			st->nents++;
2846477eb7f9SFrançois Tigeot 			/* We don't need the pages, but need to initialize
2847477eb7f9SFrançois Tigeot 			 * the entries so the sg list can be happily traversed.
2848477eb7f9SFrançois Tigeot 			 * The only thing we need are DMA addresses.
2849477eb7f9SFrançois Tigeot 			 */
2850477eb7f9SFrançois Tigeot 			sg_set_page(sg, NULL, PAGE_SIZE, 0);
2851477eb7f9SFrançois Tigeot 			sg_dma_address(sg) = in[src_idx];
2852477eb7f9SFrançois Tigeot 			sg_dma_len(sg) = PAGE_SIZE;
2853477eb7f9SFrançois Tigeot 			sg = sg_next(sg);
2854477eb7f9SFrançois Tigeot 			src_idx -= width;
2855477eb7f9SFrançois Tigeot 		}
2856477eb7f9SFrançois Tigeot 	}
2857477eb7f9SFrançois Tigeot }
2858477eb7f9SFrançois Tigeot 
2859477eb7f9SFrançois Tigeot static struct sg_table *
2860477eb7f9SFrançois Tigeot intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
2861477eb7f9SFrançois Tigeot 			  struct drm_i915_gem_object *obj)
2862477eb7f9SFrançois Tigeot {
2863477eb7f9SFrançois Tigeot 	struct drm_device *dev = obj->base.dev;
2864477eb7f9SFrançois Tigeot 	struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
2865477eb7f9SFrançois Tigeot 	unsigned long size, pages, rot_pages;
2866477eb7f9SFrançois Tigeot 	struct sg_page_iter sg_iter;
2867477eb7f9SFrançois Tigeot 	unsigned long i;
2868477eb7f9SFrançois Tigeot 	dma_addr_t *page_addr_list;
2869477eb7f9SFrançois Tigeot 	struct sg_table *st;
2870477eb7f9SFrançois Tigeot 	unsigned int tile_pitch, tile_height;
2871477eb7f9SFrançois Tigeot 	unsigned int width_pages, height_pages;
2872477eb7f9SFrançois Tigeot 	int ret = -ENOMEM;
2873477eb7f9SFrançois Tigeot 
2874477eb7f9SFrançois Tigeot 	pages = obj->base.size / PAGE_SIZE;
2875477eb7f9SFrançois Tigeot 
2876477eb7f9SFrançois Tigeot 	/* Calculate tiling geometry. */
2877477eb7f9SFrançois Tigeot 	tile_height = intel_tile_height(dev, rot_info->pixel_format,
2878477eb7f9SFrançois Tigeot 					rot_info->fb_modifier);
2879477eb7f9SFrançois Tigeot 	tile_pitch = PAGE_SIZE / tile_height;
2880477eb7f9SFrançois Tigeot 	width_pages = DIV_ROUND_UP(rot_info->pitch, tile_pitch);
2881477eb7f9SFrançois Tigeot 	height_pages = DIV_ROUND_UP(rot_info->height, tile_height);
2882477eb7f9SFrançois Tigeot 	rot_pages = width_pages * height_pages;
2883477eb7f9SFrançois Tigeot 	size = rot_pages * PAGE_SIZE;
2884477eb7f9SFrançois Tigeot 
2885477eb7f9SFrançois Tigeot 	/* Allocate a temporary list of source pages for random access. */
2886477eb7f9SFrançois Tigeot 	page_addr_list = drm_malloc_ab(pages, sizeof(dma_addr_t));
2887477eb7f9SFrançois Tigeot 	if (!page_addr_list)
2888477eb7f9SFrançois Tigeot 		return ERR_PTR(ret);
2889477eb7f9SFrançois Tigeot 
2890477eb7f9SFrançois Tigeot 	/* Allocate target SG list. */
2891477eb7f9SFrançois Tigeot 	st = kmalloc(sizeof(*st), GFP_KERNEL);
2892477eb7f9SFrançois Tigeot 	if (!st)
2893477eb7f9SFrançois Tigeot 		goto err_st_alloc;
2894477eb7f9SFrançois Tigeot 
2895477eb7f9SFrançois Tigeot 	ret = sg_alloc_table(st, rot_pages, GFP_KERNEL);
2896477eb7f9SFrançois Tigeot 	if (ret)
2897477eb7f9SFrançois Tigeot 		goto err_sg_alloc;
2898477eb7f9SFrançois Tigeot 
2899477eb7f9SFrançois Tigeot 	/* Populate source page list from the object. */
2900477eb7f9SFrançois Tigeot 	i = 0;
2901477eb7f9SFrançois Tigeot 	for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
2902477eb7f9SFrançois Tigeot 		page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
2903477eb7f9SFrançois Tigeot 		i++;
2904477eb7f9SFrançois Tigeot 	}
2905477eb7f9SFrançois Tigeot 
2906477eb7f9SFrançois Tigeot 	/* Rotate the pages. */
2907477eb7f9SFrançois Tigeot 	rotate_pages(page_addr_list, width_pages, height_pages, st);
2908477eb7f9SFrançois Tigeot 
2909477eb7f9SFrançois Tigeot 	DRM_DEBUG_KMS(
2910477eb7f9SFrançois Tigeot 		      "Created rotated page mapping for object size %lu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages).\n",
2911477eb7f9SFrançois Tigeot 		      size, rot_info->pitch, rot_info->height,
2912477eb7f9SFrançois Tigeot 		      rot_info->pixel_format, width_pages, height_pages,
2913477eb7f9SFrançois Tigeot 		      rot_pages);
2914477eb7f9SFrançois Tigeot 
2915477eb7f9SFrançois Tigeot 	drm_free_large(page_addr_list);
2916477eb7f9SFrançois Tigeot 
2917477eb7f9SFrançois Tigeot 	return st;
2918477eb7f9SFrançois Tigeot 
2919477eb7f9SFrançois Tigeot err_sg_alloc:
2920477eb7f9SFrançois Tigeot 	kfree(st);
2921477eb7f9SFrançois Tigeot err_st_alloc:
2922477eb7f9SFrançois Tigeot 	drm_free_large(page_addr_list);
2923477eb7f9SFrançois Tigeot 
2924477eb7f9SFrançois Tigeot 	DRM_DEBUG_KMS(
2925477eb7f9SFrançois Tigeot 		      "Failed to create rotated mapping for object size %lu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages)\n",
2926477eb7f9SFrançois Tigeot 		      size, ret, rot_info->pitch, rot_info->height,
2927477eb7f9SFrançois Tigeot 		      rot_info->pixel_format, width_pages, height_pages,
2928477eb7f9SFrançois Tigeot 		      rot_pages);
2929477eb7f9SFrançois Tigeot 	return ERR_PTR(ret);
2930477eb7f9SFrançois Tigeot }
2931*19c468b4SFrançois Tigeot 
2932*19c468b4SFrançois Tigeot static struct sg_table *
2933*19c468b4SFrançois Tigeot intel_partial_pages(const struct i915_ggtt_view *view,
2934*19c468b4SFrançois Tigeot 		    struct drm_i915_gem_object *obj)
2935*19c468b4SFrançois Tigeot {
2936*19c468b4SFrançois Tigeot 	struct sg_table *st;
2937*19c468b4SFrançois Tigeot 	struct scatterlist *sg;
2938*19c468b4SFrançois Tigeot 	struct sg_page_iter obj_sg_iter;
2939*19c468b4SFrançois Tigeot 	int ret = -ENOMEM;
2940*19c468b4SFrançois Tigeot 
2941*19c468b4SFrançois Tigeot 	st = kmalloc(sizeof(*st), GFP_KERNEL);
2942*19c468b4SFrançois Tigeot 	if (!st)
2943*19c468b4SFrançois Tigeot 		goto err_st_alloc;
2944*19c468b4SFrançois Tigeot 
2945*19c468b4SFrançois Tigeot 	ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL);
2946*19c468b4SFrançois Tigeot 	if (ret)
2947*19c468b4SFrançois Tigeot 		goto err_sg_alloc;
2948*19c468b4SFrançois Tigeot 
2949*19c468b4SFrançois Tigeot 	sg = st->sgl;
2950*19c468b4SFrançois Tigeot 	st->nents = 0;
2951*19c468b4SFrançois Tigeot 	for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents,
2952*19c468b4SFrançois Tigeot 		view->params.partial.offset)
2953*19c468b4SFrançois Tigeot 	{
2954*19c468b4SFrançois Tigeot 		if (st->nents >= view->params.partial.size)
2955*19c468b4SFrançois Tigeot 			break;
2956*19c468b4SFrançois Tigeot 
2957*19c468b4SFrançois Tigeot 		sg_set_page(sg, NULL, PAGE_SIZE, 0);
2958*19c468b4SFrançois Tigeot 		sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter);
2959*19c468b4SFrançois Tigeot 		sg_dma_len(sg) = PAGE_SIZE;
2960*19c468b4SFrançois Tigeot 
2961*19c468b4SFrançois Tigeot 		sg = sg_next(sg);
2962*19c468b4SFrançois Tigeot 		st->nents++;
2963*19c468b4SFrançois Tigeot 	}
2964*19c468b4SFrançois Tigeot 
2965*19c468b4SFrançois Tigeot 	return st;
2966*19c468b4SFrançois Tigeot 
2967*19c468b4SFrançois Tigeot err_sg_alloc:
2968*19c468b4SFrançois Tigeot 	kfree(st);
2969*19c468b4SFrançois Tigeot err_st_alloc:
2970*19c468b4SFrançois Tigeot 	return ERR_PTR(ret);
2971*19c468b4SFrançois Tigeot }
2972477eb7f9SFrançois Tigeot #endif
2973477eb7f9SFrançois Tigeot 
2974*19c468b4SFrançois Tigeot static int
2975477eb7f9SFrançois Tigeot i915_get_ggtt_vma_pages(struct i915_vma *vma)
2976477eb7f9SFrançois Tigeot {
2977477eb7f9SFrançois Tigeot 	int ret = 0;
2978477eb7f9SFrançois Tigeot 
29792c9916cdSFrançois Tigeot 	if (vma->ggtt_view.pages)
29802c9916cdSFrançois Tigeot 		return 0;
29812c9916cdSFrançois Tigeot 
29822c9916cdSFrançois Tigeot 	if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
29832c9916cdSFrançois Tigeot 		vma->ggtt_view.pages = vma->obj->pages;
2984477eb7f9SFrançois Tigeot #if 0
2985477eb7f9SFrançois Tigeot 	else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
2986477eb7f9SFrançois Tigeot 		vma->ggtt_view.pages =
2987477eb7f9SFrançois Tigeot 			intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
2988*19c468b4SFrançois Tigeot 	else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
2989*19c468b4SFrançois Tigeot 		vma->ggtt_view.pages =
2990*19c468b4SFrançois Tigeot 			intel_partial_pages(&vma->ggtt_view, vma->obj);
2991477eb7f9SFrançois Tigeot #endif
29922c9916cdSFrançois Tigeot 	else
29932c9916cdSFrançois Tigeot 		WARN_ONCE(1, "GGTT view %u not implemented!\n",
29942c9916cdSFrançois Tigeot 			  vma->ggtt_view.type);
29952c9916cdSFrançois Tigeot 
29962c9916cdSFrançois Tigeot 	if (!vma->ggtt_view.pages) {
2997477eb7f9SFrançois Tigeot 		DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
29982c9916cdSFrançois Tigeot 			  vma->ggtt_view.type);
2999477eb7f9SFrançois Tigeot 		ret = -EINVAL;
3000477eb7f9SFrançois Tigeot 	} else if (IS_ERR(vma->ggtt_view.pages)) {
3001477eb7f9SFrançois Tigeot 		ret = PTR_ERR(vma->ggtt_view.pages);
3002477eb7f9SFrançois Tigeot 		vma->ggtt_view.pages = NULL;
3003477eb7f9SFrançois Tigeot 		DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3004477eb7f9SFrançois Tigeot 			  vma->ggtt_view.type, ret);
30052c9916cdSFrançois Tigeot 	}
30062c9916cdSFrançois Tigeot 
3007477eb7f9SFrançois Tigeot 	return ret;
30082c9916cdSFrançois Tigeot }
30092c9916cdSFrançois Tigeot 
30102c9916cdSFrançois Tigeot /**
30112c9916cdSFrançois Tigeot  * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
30122c9916cdSFrançois Tigeot  * @vma: VMA to map
30132c9916cdSFrançois Tigeot  * @cache_level: mapping cache level
30142c9916cdSFrançois Tigeot  * @flags: flags like global or local mapping
30152c9916cdSFrançois Tigeot  *
30162c9916cdSFrançois Tigeot  * DMA addresses are taken from the scatter-gather table of this object (or of
30172c9916cdSFrançois Tigeot  * this VMA in case of non-default GGTT views) and PTE entries set up.
30182c9916cdSFrançois Tigeot  * Note that DMA addresses are also the only part of the SG table we care about.
30192c9916cdSFrançois Tigeot  */
30202c9916cdSFrançois Tigeot int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
30212c9916cdSFrançois Tigeot 		  u32 flags)
30222c9916cdSFrançois Tigeot {
3023*19c468b4SFrançois Tigeot 	int ret;
3024*19c468b4SFrançois Tigeot 	u32 bind_flags;
30252c9916cdSFrançois Tigeot 
3026*19c468b4SFrançois Tigeot 	if (WARN_ON(flags == 0))
3027*19c468b4SFrançois Tigeot 		return -EINVAL;
3028*19c468b4SFrançois Tigeot 
3029*19c468b4SFrançois Tigeot 	if (vma->vm->allocate_va_range) {
3030*19c468b4SFrançois Tigeot 		trace_i915_va_alloc(vma->vm, vma->node.start,
3031*19c468b4SFrançois Tigeot 				    vma->node.size,
3032*19c468b4SFrançois Tigeot 				    VM_TO_TRACE_NAME(vma->vm));
3033*19c468b4SFrançois Tigeot 
3034*19c468b4SFrançois Tigeot 		ret = vma->vm->allocate_va_range(vma->vm,
3035*19c468b4SFrançois Tigeot 						 vma->node.start,
3036*19c468b4SFrançois Tigeot 						 vma->node.size);
30372c9916cdSFrançois Tigeot 		if (ret)
30382c9916cdSFrançois Tigeot 			return ret;
3039477eb7f9SFrançois Tigeot 	}
30402c9916cdSFrançois Tigeot 
3041*19c468b4SFrançois Tigeot 	if (i915_is_ggtt(vma->vm)) {
3042*19c468b4SFrançois Tigeot 		ret = i915_get_ggtt_vma_pages(vma);
3043*19c468b4SFrançois Tigeot 		if (ret)
3044*19c468b4SFrançois Tigeot 			return 0;
3045*19c468b4SFrançois Tigeot 	}
3046*19c468b4SFrançois Tigeot 
3047*19c468b4SFrançois Tigeot 	bind_flags = 0;
3048*19c468b4SFrançois Tigeot 	if (flags & PIN_GLOBAL)
3049*19c468b4SFrançois Tigeot 		bind_flags |= GLOBAL_BIND;
3050*19c468b4SFrançois Tigeot 	if (flags & PIN_USER)
3051*19c468b4SFrançois Tigeot 		bind_flags |= LOCAL_BIND;
3052*19c468b4SFrançois Tigeot 
3053*19c468b4SFrançois Tigeot 	if (flags & PIN_UPDATE)
3054*19c468b4SFrançois Tigeot 		bind_flags |= vma->bound;
3055*19c468b4SFrançois Tigeot 	else
3056*19c468b4SFrançois Tigeot 		bind_flags &= ~vma->bound;
3057*19c468b4SFrançois Tigeot 
3058*19c468b4SFrançois Tigeot 	if (bind_flags == 0)
3059*19c468b4SFrançois Tigeot 		return 0;
3060*19c468b4SFrançois Tigeot 
3061*19c468b4SFrançois Tigeot 	if (vma->bound == 0 && vma->vm->allocate_va_range) {
3062*19c468b4SFrançois Tigeot 		trace_i915_va_alloc(vma->vm,
3063*19c468b4SFrançois Tigeot 				    vma->node.start,
3064*19c468b4SFrançois Tigeot 				    vma->node.size,
3065*19c468b4SFrançois Tigeot 				    VM_TO_TRACE_NAME(vma->vm));
3066*19c468b4SFrançois Tigeot 
3067*19c468b4SFrançois Tigeot 		ret = vma->vm->allocate_va_range(vma->vm,
3068*19c468b4SFrançois Tigeot 						 vma->node.start,
3069*19c468b4SFrançois Tigeot 						 vma->node.size);
3070*19c468b4SFrançois Tigeot 		if (ret)
3071*19c468b4SFrançois Tigeot 			return ret;
3072*19c468b4SFrançois Tigeot 	}
3073*19c468b4SFrançois Tigeot 
3074*19c468b4SFrançois Tigeot 	ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
3075*19c468b4SFrançois Tigeot 	if (ret)
3076*19c468b4SFrançois Tigeot 		return ret;
3077*19c468b4SFrançois Tigeot 
3078*19c468b4SFrançois Tigeot 	vma->bound |= bind_flags;
30792c9916cdSFrançois Tigeot 
30802c9916cdSFrançois Tigeot 	return 0;
30812c9916cdSFrançois Tigeot }
3082*19c468b4SFrançois Tigeot 
3083*19c468b4SFrançois Tigeot /**
3084*19c468b4SFrançois Tigeot  * i915_ggtt_view_size - Get the size of a GGTT view.
3085*19c468b4SFrançois Tigeot  * @obj: Object the view is of.
3086*19c468b4SFrançois Tigeot  * @view: The view in question.
3087*19c468b4SFrançois Tigeot  *
3088*19c468b4SFrançois Tigeot  * @return The size of the GGTT view in bytes.
3089*19c468b4SFrançois Tigeot  */
3090*19c468b4SFrançois Tigeot size_t
3091*19c468b4SFrançois Tigeot i915_ggtt_view_size(struct drm_i915_gem_object *obj,
3092*19c468b4SFrançois Tigeot 		    const struct i915_ggtt_view *view)
3093*19c468b4SFrançois Tigeot {
3094*19c468b4SFrançois Tigeot 	if (view->type == I915_GGTT_VIEW_NORMAL ||
3095*19c468b4SFrançois Tigeot 	    view->type == I915_GGTT_VIEW_ROTATED) {
3096*19c468b4SFrançois Tigeot 		return obj->base.size;
3097*19c468b4SFrançois Tigeot 	} else if (view->type == I915_GGTT_VIEW_PARTIAL) {
3098*19c468b4SFrançois Tigeot 		return view->params.partial.size << PAGE_SHIFT;
3099*19c468b4SFrançois Tigeot 	} else {
3100*19c468b4SFrançois Tigeot 		WARN_ONCE(1, "GGTT view %u not implemented!\n", view->type);
3101*19c468b4SFrançois Tigeot 		return obj->base.size;
3102*19c468b4SFrançois Tigeot 	}
3103*19c468b4SFrançois Tigeot }
3104