blob: b4bde1452f2a3215126d8cf327b24405b8f78f6d [file] [log] [blame]
Daniel Vetter76aaf222010-11-05 22:23:30 +01001/*
2 * Copyright © 2010 Daniel Vetter
Ben Widawskyc4ac5242014-02-19 22:05:47 -08003 * Copyright © 2011-2014 Intel Corporation
Daniel Vetter76aaf222010-11-05 22:23:30 +01004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
Daniel Vetter0e46ce22014-01-08 16:10:27 +010026#include <linux/seq_file.h>
Chris Wilson5bab6f62015-10-23 18:43:32 +010027#include <linux/stop_machine.h>
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
29#include <drm/i915_drm.h>
Daniel Vetter76aaf222010-11-05 22:23:30 +010030#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080031#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010032#include "i915_trace.h"
33#include "intel_drv.h"
Chris Wilsond07f0e52016-10-28 13:58:44 +010034#include "intel_frontbuffer.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010035
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010036#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
37
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000038/**
39 * DOC: Global GTT views
40 *
41 * Background and previous state
42 *
43 * Historically objects could exists (be bound) in global GTT space only as
44 * singular instances with a view representing all of the object's backing pages
45 * in a linear fashion. This view will be called a normal view.
46 *
47 * To support multiple views of the same object, where the number of mapped
48 * pages is not equal to the backing store, or where the layout of the pages
49 * is not linear, concept of a GGTT view was added.
50 *
51 * One example of an alternative view is a stereo display driven by a single
52 * image. In this case we would have a framebuffer looking like this
53 * (2x2 pages):
54 *
55 * 12
56 * 34
57 *
58 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
59 * rendering. In contrast, fed to the display engine would be an alternative
60 * view which could look something like this:
61 *
62 * 1212
63 * 3434
64 *
65 * In this example both the size and layout of pages in the alternative view is
66 * different from the normal view.
67 *
68 * Implementation and usage
69 *
70 * GGTT views are implemented using VMAs and are distinguished via enum
71 * i915_ggtt_view_type and struct i915_ggtt_view.
72 *
73 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020074 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
75 * renaming in large amounts of code. They take the struct i915_ggtt_view
76 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000077 *
78 * As a helper for callers which are only interested in the normal view,
79 * globally const i915_ggtt_view_normal singleton instance exists. All old core
80 * GEM API functions, the ones not taking the view parameter, are operating on,
81 * or with the normal GGTT view.
82 *
83 * Code wanting to add or use a new GGTT view needs to:
84 *
85 * 1. Add a new enum with a suitable name.
86 * 2. Extend the metadata in the i915_ggtt_view structure if required.
87 * 3. Add support to i915_get_vma_pages().
88 *
89 * New views are required to build a scatter-gather table from within the
90 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
91 * exists for the lifetime of an VMA.
92 *
93 * Core API is designed to have copy semantics which means that passed in
94 * struct i915_ggtt_view does not need to be persistent (left around after
95 * calling the core API functions).
96 *
97 */
98
Daniel Vetter70b9f6f2015-04-14 17:35:27 +020099static int
100i915_get_ggtt_vma_pages(struct i915_vma *vma);
101
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200102const struct i915_ggtt_view i915_ggtt_view_normal = {
103 .type = I915_GGTT_VIEW_NORMAL,
104};
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200105const struct i915_ggtt_view i915_ggtt_view_rotated = {
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200106 .type = I915_GGTT_VIEW_ROTATED,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200107};
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000108
Chris Wilsonc0336662016-05-06 15:40:21 +0100109int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
110 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200111{
Chris Wilson1893a712014-09-19 11:56:27 +0100112 bool has_aliasing_ppgtt;
113 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100114 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100115
Chris Wilsonc0336662016-05-06 15:40:21 +0100116 has_aliasing_ppgtt = INTEL_GEN(dev_priv) >= 6;
117 has_full_ppgtt = INTEL_GEN(dev_priv) >= 7;
118 has_full_48bit_ppgtt =
119 IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9;
Chris Wilson1893a712014-09-19 11:56:27 +0100120
Zhi Wange320d402016-09-06 12:04:12 +0800121 if (intel_vgpu_active(dev_priv)) {
122 /* emulation is too hard */
123 has_full_ppgtt = false;
124 has_full_48bit_ppgtt = false;
125 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800126
Chris Wilson0e4ca102016-04-29 13:18:22 +0100127 if (!has_aliasing_ppgtt)
128 return 0;
129
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000130 /*
131 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
132 * execlists, the sole mechanism available to submit work.
133 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100134 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200135 return 0;
136
137 if (enable_ppgtt == 1)
138 return 1;
139
Chris Wilson1893a712014-09-19 11:56:27 +0100140 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200141 return 2;
142
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100143 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
144 return 3;
145
Daniel Vetter93a25a92014-03-06 09:40:43 +0100146#ifdef CONFIG_INTEL_IOMMU
147 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100148 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100149 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200150 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100151 }
152#endif
153
Jesse Barnes62942ed2014-06-13 09:28:33 -0700154 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100155 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700156 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
157 return 0;
158 }
159
Zhi Wange320d402016-09-06 12:04:12 +0800160 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100161 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000162 else
163 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100164}
165
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200166static int ppgtt_bind_vma(struct i915_vma *vma,
167 enum i915_cache_level cache_level,
168 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200169{
170 u32 pte_flags = 0;
171
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100172 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100173
Daniel Vetter47552652015-04-14 17:35:24 +0200174 /* Currently applicable only to VLV */
175 if (vma->obj->gt_ro)
176 pte_flags |= PTE_READ_ONLY;
177
Chris Wilson247177d2016-08-15 10:48:47 +0100178 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200179 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200180
181 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200182}
183
184static void ppgtt_unbind_vma(struct i915_vma *vma)
185{
186 vma->vm->clear_range(vma->vm,
187 vma->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200188 vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200189}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800190
Daniel Vetter2c642b02015-04-14 17:35:26 +0200191static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200192 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700193{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200194 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700195 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300196
197 switch (level) {
198 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800199 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300200 break;
201 case I915_CACHE_WT:
202 pte |= PPAT_DISPLAY_ELLC_INDEX;
203 break;
204 default:
205 pte |= PPAT_CACHED_INDEX;
206 break;
207 }
208
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700209 return pte;
210}
211
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300212static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
213 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800214{
Michel Thierry07749ef2015-03-16 16:00:54 +0000215 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800216 pde |= addr;
217 if (level != I915_CACHE_NONE)
218 pde |= PPAT_CACHED_PDE_INDEX;
219 else
220 pde |= PPAT_UNCACHED_INDEX;
221 return pde;
222}
223
Michel Thierry762d9932015-07-30 11:05:29 +0100224#define gen8_pdpe_encode gen8_pde_encode
225#define gen8_pml4e_encode gen8_pde_encode
226
Michel Thierry07749ef2015-03-16 16:00:54 +0000227static gen6_pte_t snb_pte_encode(dma_addr_t addr,
228 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200229 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700230{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200231 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700232 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700233
234 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100235 case I915_CACHE_L3_LLC:
236 case I915_CACHE_LLC:
237 pte |= GEN6_PTE_CACHE_LLC;
238 break;
239 case I915_CACHE_NONE:
240 pte |= GEN6_PTE_UNCACHED;
241 break;
242 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100243 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100244 }
245
246 return pte;
247}
248
Michel Thierry07749ef2015-03-16 16:00:54 +0000249static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
250 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200251 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100252{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200253 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100254 pte |= GEN6_PTE_ADDR_ENCODE(addr);
255
256 switch (level) {
257 case I915_CACHE_L3_LLC:
258 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700259 break;
260 case I915_CACHE_LLC:
261 pte |= GEN6_PTE_CACHE_LLC;
262 break;
263 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700264 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700265 break;
266 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100267 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700268 }
269
Ben Widawsky54d12522012-09-24 16:44:32 -0700270 return pte;
271}
272
Michel Thierry07749ef2015-03-16 16:00:54 +0000273static gen6_pte_t byt_pte_encode(dma_addr_t addr,
274 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200275 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700276{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200277 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700278 pte |= GEN6_PTE_ADDR_ENCODE(addr);
279
Akash Goel24f3a8c2014-06-17 10:59:42 +0530280 if (!(flags & PTE_READ_ONLY))
281 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700282
283 if (level != I915_CACHE_NONE)
284 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
285
286 return pte;
287}
288
Michel Thierry07749ef2015-03-16 16:00:54 +0000289static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
290 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200291 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700292{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200293 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700294 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700295
296 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700297 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700298
299 return pte;
300}
301
Michel Thierry07749ef2015-03-16 16:00:54 +0000302static gen6_pte_t iris_pte_encode(dma_addr_t addr,
303 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200304 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700305{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200306 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700307 pte |= HSW_PTE_ADDR_ENCODE(addr);
308
Chris Wilson651d7942013-08-08 14:41:10 +0100309 switch (level) {
310 case I915_CACHE_NONE:
311 break;
312 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000313 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100314 break;
315 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000316 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100317 break;
318 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700319
320 return pte;
321}
322
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000323static int __setup_page_dma(struct drm_i915_private *dev_priv,
Mika Kuoppalac114f762015-06-25 18:35:13 +0300324 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000325{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000326 struct device *kdev = &dev_priv->drm.pdev->dev;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000327
Mika Kuoppalac114f762015-06-25 18:35:13 +0300328 p->page = alloc_page(flags);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300329 if (!p->page)
Michel Thierry1266cdb2015-03-24 17:06:33 +0000330 return -ENOMEM;
331
David Weinehallc49d13e2016-08-22 13:32:42 +0300332 p->daddr = dma_map_page(kdev,
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300333 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
334
David Weinehallc49d13e2016-08-22 13:32:42 +0300335 if (dma_mapping_error(kdev, p->daddr)) {
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300336 __free_page(p->page);
337 return -EINVAL;
338 }
339
Michel Thierry1266cdb2015-03-24 17:06:33 +0000340 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000341}
342
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000343static int setup_page_dma(struct drm_i915_private *dev_priv,
344 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300345{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000346 return __setup_page_dma(dev_priv, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300347}
348
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000349static void cleanup_page_dma(struct drm_i915_private *dev_priv,
350 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300351{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000352 struct pci_dev *pdev = dev_priv->drm.pdev;
David Weinehall52a05c32016-08-22 13:32:44 +0300353
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300354 if (WARN_ON(!p->page))
355 return;
356
David Weinehall52a05c32016-08-22 13:32:44 +0300357 dma_unmap_page(&pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300358 __free_page(p->page);
359 memset(p, 0, sizeof(*p));
360}
361
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300362static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300363{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300364 return kmap_atomic(p->page);
365}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300366
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300367/* We use the flushing unmap only with ppgtt structures:
368 * page directories, page tables and scratch pages.
369 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100370static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300371{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300372 /* There are only few exceptions for gen >=6. chv and bxt.
373 * And we are not sure about the latter so play safe for now.
374 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100375 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300376 drm_clflush_virt_range(vaddr, PAGE_SIZE);
377
378 kunmap_atomic(vaddr);
379}
380
Mika Kuoppala567047b2015-06-25 18:35:12 +0300381#define kmap_px(px) kmap_page_dma(px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100382#define kunmap_px(ppgtt, vaddr) \
383 kunmap_page_dma(to_i915((ppgtt)->base.dev), (vaddr))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300384
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000385#define setup_px(dev_priv, px) setup_page_dma((dev_priv), px_base(px))
386#define cleanup_px(dev_priv, px) cleanup_page_dma((dev_priv), px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100387#define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
388#define fill32_px(dev_priv, px, v) \
389 fill_page_dma_32((dev_priv), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300390
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100391static void fill_page_dma(struct drm_i915_private *dev_priv,
392 struct i915_page_dma *p, const uint64_t val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300393{
394 int i;
395 uint64_t * const vaddr = kmap_page_dma(p);
396
397 for (i = 0; i < 512; i++)
398 vaddr[i] = val;
399
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100400 kunmap_page_dma(dev_priv, vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300401}
402
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100403static void fill_page_dma_32(struct drm_i915_private *dev_priv,
404 struct i915_page_dma *p, const uint32_t val32)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300405{
406 uint64_t v = val32;
407
408 v = v << 32 | val32;
409
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100410 fill_page_dma(dev_priv, p, v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300411}
412
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100413static int
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000414setup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100415 struct i915_page_dma *scratch,
416 gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300417{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000418 return __setup_page_dma(dev_priv, scratch, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300419}
420
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000421static void cleanup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100422 struct i915_page_dma *scratch)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300423{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000424 cleanup_page_dma(dev_priv, scratch);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300425}
426
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000427static struct i915_page_table *alloc_pt(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000428{
Michel Thierryec565b32015-04-08 12:13:23 +0100429 struct i915_page_table *pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000430 const size_t count = INTEL_GEN(dev_priv) >= 8 ? GEN8_PTES : GEN6_PTES;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000431 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000432
433 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
434 if (!pt)
435 return ERR_PTR(-ENOMEM);
436
Ben Widawsky678d96f2015-03-16 16:00:56 +0000437 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
438 GFP_KERNEL);
439
440 if (!pt->used_ptes)
441 goto fail_bitmap;
442
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000443 ret = setup_px(dev_priv, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000444 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300445 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000446
447 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000448
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300449fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000450 kfree(pt->used_ptes);
451fail_bitmap:
452 kfree(pt);
453
454 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000455}
456
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000457static void free_pt(struct drm_i915_private *dev_priv,
458 struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000459{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000460 cleanup_px(dev_priv, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300461 kfree(pt->used_ptes);
462 kfree(pt);
463}
464
465static void gen8_initialize_pt(struct i915_address_space *vm,
466 struct i915_page_table *pt)
467{
468 gen8_pte_t scratch_pte;
469
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100470 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200471 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300472
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100473 fill_px(to_i915(vm->dev), pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300474}
475
476static void gen6_initialize_pt(struct i915_address_space *vm,
477 struct i915_page_table *pt)
478{
479 gen6_pte_t scratch_pte;
480
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100481 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300482
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100483 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200484 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300485
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100486 fill32_px(to_i915(vm->dev), pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000487}
488
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000489static struct i915_page_directory *alloc_pd(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000490{
Michel Thierryec565b32015-04-08 12:13:23 +0100491 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100492 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000493
494 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
495 if (!pd)
496 return ERR_PTR(-ENOMEM);
497
Michel Thierry33c88192015-04-08 12:13:33 +0100498 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
499 sizeof(*pd->used_pdes), GFP_KERNEL);
500 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300501 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100502
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000503 ret = setup_px(dev_priv, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100504 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300505 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100506
Ben Widawsky06fda602015-02-24 16:22:36 +0000507 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100508
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300509fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100510 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300511fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100512 kfree(pd);
513
514 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000515}
516
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000517static void free_pd(struct drm_i915_private *dev_priv,
518 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300519{
520 if (px_page(pd)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000521 cleanup_px(dev_priv, pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300522 kfree(pd->used_pdes);
523 kfree(pd);
524 }
525}
526
527static void gen8_initialize_pd(struct i915_address_space *vm,
528 struct i915_page_directory *pd)
529{
530 gen8_pde_t scratch_pde;
531
532 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
533
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100534 fill_px(to_i915(vm->dev), pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300535}
536
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000537static int __pdp_init(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100538 struct i915_page_directory_pointer *pdp)
539{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000540 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Michel Thierry6ac18502015-07-29 17:23:46 +0100541
542 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
543 sizeof(unsigned long),
544 GFP_KERNEL);
545 if (!pdp->used_pdpes)
546 return -ENOMEM;
547
548 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
549 GFP_KERNEL);
550 if (!pdp->page_directory) {
551 kfree(pdp->used_pdpes);
552 /* the PDP might be the statically allocated top level. Keep it
553 * as clean as possible */
554 pdp->used_pdpes = NULL;
555 return -ENOMEM;
556 }
557
558 return 0;
559}
560
561static void __pdp_fini(struct i915_page_directory_pointer *pdp)
562{
563 kfree(pdp->used_pdpes);
564 kfree(pdp->page_directory);
565 pdp->page_directory = NULL;
566}
567
Michel Thierry762d9932015-07-30 11:05:29 +0100568static struct
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000569i915_page_directory_pointer *alloc_pdp(struct drm_i915_private *dev_priv)
Michel Thierry762d9932015-07-30 11:05:29 +0100570{
571 struct i915_page_directory_pointer *pdp;
572 int ret = -ENOMEM;
573
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000574 WARN_ON(!USES_FULL_48BIT_PPGTT(dev_priv));
Michel Thierry762d9932015-07-30 11:05:29 +0100575
576 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
577 if (!pdp)
578 return ERR_PTR(-ENOMEM);
579
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000580 ret = __pdp_init(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100581 if (ret)
582 goto fail_bitmap;
583
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000584 ret = setup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100585 if (ret)
586 goto fail_page_m;
587
588 return pdp;
589
590fail_page_m:
591 __pdp_fini(pdp);
592fail_bitmap:
593 kfree(pdp);
594
595 return ERR_PTR(ret);
596}
597
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000598static void free_pdp(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100599 struct i915_page_directory_pointer *pdp)
600{
601 __pdp_fini(pdp);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000602 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
603 cleanup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100604 kfree(pdp);
605 }
606}
607
Michel Thierry69ab76f2015-07-29 17:23:55 +0100608static void gen8_initialize_pdp(struct i915_address_space *vm,
609 struct i915_page_directory_pointer *pdp)
610{
611 gen8_ppgtt_pdpe_t scratch_pdpe;
612
613 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
614
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100615 fill_px(to_i915(vm->dev), pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100616}
617
618static void gen8_initialize_pml4(struct i915_address_space *vm,
619 struct i915_pml4 *pml4)
620{
621 gen8_ppgtt_pml4e_t scratch_pml4e;
622
623 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
624 I915_CACHE_LLC);
625
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100626 fill_px(to_i915(vm->dev), pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100627}
628
Michel Thierry762d9932015-07-30 11:05:29 +0100629static void
630gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
631 struct i915_page_directory_pointer *pdp,
632 struct i915_page_directory *pd,
633 int index)
634{
635 gen8_ppgtt_pdpe_t *page_directorypo;
636
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000637 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
Michel Thierry762d9932015-07-30 11:05:29 +0100638 return;
639
640 page_directorypo = kmap_px(pdp);
641 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
642 kunmap_px(ppgtt, page_directorypo);
643}
644
645static void
646gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
647 struct i915_pml4 *pml4,
648 struct i915_page_directory_pointer *pdp,
649 int index)
650{
651 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
652
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000653 WARN_ON(!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)));
Michel Thierry762d9932015-07-30 11:05:29 +0100654 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
655 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100656}
657
Ben Widawsky94e409c2013-11-04 22:29:36 -0800658/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100659static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100660 unsigned entry,
661 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800662{
Chris Wilson7e37f882016-08-02 22:50:21 +0100663 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000664 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800665 int ret;
666
667 BUG_ON(entry >= 4);
668
John Harrison5fb9de12015-05-29 17:44:07 +0100669 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800670 if (ret)
671 return ret;
672
Chris Wilsonb5321f32016-08-02 22:50:18 +0100673 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
674 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
675 intel_ring_emit(ring, upper_32_bits(addr));
676 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
677 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
678 intel_ring_emit(ring, lower_32_bits(addr));
679 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800680
681 return 0;
682}
683
Michel Thierry2dba3232015-07-30 11:06:23 +0100684static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
685 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800686{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800687 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800688
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100689 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300690 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
691
John Harrisone85b26d2015-05-29 17:43:56 +0100692 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800693 if (ret)
694 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800695 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800696
Ben Widawskyeeb94882013-12-06 14:11:10 -0800697 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800698}
699
Michel Thierry2dba3232015-07-30 11:06:23 +0100700static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
701 struct drm_i915_gem_request *req)
702{
703 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
704}
705
Mika Kuoppalafce93752016-10-31 17:24:46 +0200706/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
707 * the page table structures, we mark them dirty so that
708 * context switching/execlist queuing code takes extra steps
709 * to ensure that tlbs are flushed.
710 */
711static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
712{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000713 ppgtt->pd_dirty_rings = INTEL_INFO(to_i915(ppgtt->base.dev))->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200714}
715
Michał Winiarski2ce51792016-10-13 14:02:42 +0200716/* Removes entries from a single page table, releasing it if it's empty.
717 * Caller can use the return value to update higher-level entries.
718 */
719static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200720 struct i915_page_table *pt,
721 uint64_t start,
722 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700723{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300724 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200725 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200726 unsigned int pte = gen8_pte_index(start);
727 unsigned int pte_end = pte + num_entries;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200728 gen8_pte_t *pt_vaddr;
729 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
730 I915_CACHE_LLC);
731
732 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200733 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700734
Mika Kuoppala37c63932016-11-01 15:27:36 +0200735 GEM_BUG_ON(pte_end > GEN8_PTES);
736
737 bitmap_clear(pt->used_ptes, pte, num_entries);
Ben Widawsky06fda602015-02-24 16:22:36 +0000738
Michał Winiarski2ce51792016-10-13 14:02:42 +0200739 if (bitmap_empty(pt->used_ptes, GEN8_PTES)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000740 free_pt(to_i915(vm->dev), pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200741 return true;
742 }
743
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200744 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000745
Mika Kuoppala37c63932016-11-01 15:27:36 +0200746 while (pte < pte_end)
747 pt_vaddr[pte++] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000748
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200749 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200750
751 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200752}
753
Michał Winiarski2ce51792016-10-13 14:02:42 +0200754/* Removes entries from a single page dir, releasing it if it's empty.
755 * Caller can use the return value to update higher-level entries
756 */
757static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200758 struct i915_page_directory *pd,
759 uint64_t start,
760 uint64_t length)
761{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200762 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200763 struct i915_page_table *pt;
764 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200765 gen8_pde_t *pde_vaddr;
766 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
767 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200768
769 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000770 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100771 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000772
Michał Winiarski2ce51792016-10-13 14:02:42 +0200773 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
774 __clear_bit(pde, pd->used_pdes);
775 pde_vaddr = kmap_px(pd);
776 pde_vaddr[pde] = scratch_pde;
777 kunmap_px(ppgtt, pde_vaddr);
778 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200779 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200780
781 if (bitmap_empty(pd->used_pdes, I915_PDES)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000782 free_pd(to_i915(vm->dev), pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200783 return true;
784 }
785
786 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200787}
Ben Widawsky06fda602015-02-24 16:22:36 +0000788
Michał Winiarski2ce51792016-10-13 14:02:42 +0200789/* Removes entries from a single page dir pointer, releasing it if it's empty.
790 * Caller can use the return value to update higher-level entries
791 */
792static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200793 struct i915_page_directory_pointer *pdp,
794 uint64_t start,
795 uint64_t length)
796{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200797 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000798 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200799 struct i915_page_directory *pd;
800 uint64_t pdpe;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200801 gen8_ppgtt_pdpe_t *pdpe_vaddr;
802 gen8_ppgtt_pdpe_t scratch_pdpe =
803 gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200804
805 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
806 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100807 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000808
Michał Winiarski2ce51792016-10-13 14:02:42 +0200809 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
810 __clear_bit(pdpe, pdp->used_pdpes);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000811 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Michał Winiarski2ce51792016-10-13 14:02:42 +0200812 pdpe_vaddr = kmap_px(pdp);
813 pdpe_vaddr[pdpe] = scratch_pdpe;
814 kunmap_px(ppgtt, pdpe_vaddr);
815 }
816 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200817 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200818
Mika Kuoppalafce93752016-10-31 17:24:46 +0200819 mark_tlbs_dirty(ppgtt);
820
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000821 if (USES_FULL_48BIT_PPGTT(dev_priv) &&
822 bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv))) {
823 free_pdp(dev_priv, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200824 return true;
825 }
826
827 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200828}
Ben Widawsky459108b2013-11-02 21:07:23 -0700829
Michał Winiarski2ce51792016-10-13 14:02:42 +0200830/* Removes entries from a single pml4.
831 * This is the top-level structure in 4-level page tables used on gen8+.
832 * Empty entries are always scratch pml4e.
833 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200834static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
835 struct i915_pml4 *pml4,
836 uint64_t start,
837 uint64_t length)
838{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200839 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200840 struct i915_page_directory_pointer *pdp;
841 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200842 gen8_ppgtt_pml4e_t *pml4e_vaddr;
843 gen8_ppgtt_pml4e_t scratch_pml4e =
844 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC);
845
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000846 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(to_i915(vm->dev)));
Ben Widawsky459108b2013-11-02 21:07:23 -0700847
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200848 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
849 if (WARN_ON(!pml4->pdps[pml4e]))
850 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700851
Michał Winiarski2ce51792016-10-13 14:02:42 +0200852 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
853 __clear_bit(pml4e, pml4->used_pml4es);
854 pml4e_vaddr = kmap_px(pml4);
855 pml4e_vaddr[pml4e] = scratch_pml4e;
856 kunmap_px(ppgtt, pml4e_vaddr);
857 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700858 }
859}
860
Michel Thierryf9b5b782015-07-30 11:02:49 +0100861static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200862 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700863{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300864 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100865
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000866 if (USES_FULL_48BIT_PPGTT(to_i915(vm->dev)))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200867 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
868 else
869 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100870}
871
872static void
873gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
874 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100875 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100876 uint64_t start,
877 enum i915_cache_level cache_level)
878{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300879 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000880 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100881 unsigned pdpe = gen8_pdpe_index(start);
882 unsigned pde = gen8_pde_index(start);
883 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700884
Chris Wilson6f1cc992013-12-31 15:50:31 +0000885 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700886
Michel Thierry3387d432015-08-03 09:52:47 +0100887 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000888 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100889 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100890 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300891 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000892 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800893
894 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100895 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200896 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000897 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300898 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000899 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000900 if (++pde == I915_PDES) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000901 if (++pdpe == I915_PDPES_PER_PDP(to_i915(vm->dev)))
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100902 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800903 pde = 0;
904 }
905 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700906 }
907 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300908
909 if (pt_vaddr)
910 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700911}
912
Michel Thierryf9b5b782015-07-30 11:02:49 +0100913static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
914 struct sg_table *pages,
915 uint64_t start,
916 enum i915_cache_level cache_level,
917 u32 unused)
918{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300919 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100920 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100921
Michel Thierry3387d432015-08-03 09:52:47 +0100922 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100923
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000924 if (!USES_FULL_48BIT_PPGTT(to_i915(vm->dev))) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100925 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
926 cache_level);
927 } else {
928 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000929 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100930 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
931
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000932 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100933 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
934 start, cache_level);
935 }
936 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100937}
938
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000939static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
Michel Thierryf37c0502015-06-10 17:46:39 +0100940 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800941{
942 int i;
943
Mika Kuoppala567047b2015-06-25 18:35:12 +0300944 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800945 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800946
Michel Thierry33c88192015-04-08 12:13:33 +0100947 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000948 if (WARN_ON(!pd->page_table[i]))
949 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800950
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000951 free_pt(dev_priv, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000952 pd->page_table[i] = NULL;
953 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000954}
955
Mika Kuoppala8776f022015-06-30 18:16:40 +0300956static int gen8_init_scratch(struct i915_address_space *vm)
957{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000958 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Matthew Auld64c050d2016-04-27 13:19:25 +0100959 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300960
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000961 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100962 if (ret)
963 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300964
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000965 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300966 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100967 ret = PTR_ERR(vm->scratch_pt);
968 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300969 }
970
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000971 vm->scratch_pd = alloc_pd(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300972 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100973 ret = PTR_ERR(vm->scratch_pd);
974 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300975 }
976
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000977 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
978 vm->scratch_pdp = alloc_pdp(dev_priv);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100979 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100980 ret = PTR_ERR(vm->scratch_pdp);
981 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100982 }
983 }
984
Mika Kuoppala8776f022015-06-30 18:16:40 +0300985 gen8_initialize_pt(vm, vm->scratch_pt);
986 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000987 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100988 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300989
990 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100991
992free_pd:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000993 free_pd(dev_priv, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +0100994free_pt:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000995 free_pt(dev_priv, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +0100996free_scratch_page:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000997 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +0100998
999 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001000}
1001
Zhiyuan Lv650da342015-08-28 15:41:18 +08001002static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1003{
1004 enum vgt_g2v_type msg;
Matthew Aulddf285642016-04-22 12:09:25 +01001005 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Zhiyuan Lv650da342015-08-28 15:41:18 +08001006 int i;
1007
Matthew Aulddf285642016-04-22 12:09:25 +01001008 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001009 u64 daddr = px_dma(&ppgtt->pml4);
1010
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001011 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1012 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001013
1014 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1015 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1016 } else {
1017 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1018 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1019
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001020 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1021 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001022 }
1023
1024 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1025 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1026 }
1027
1028 I915_WRITE(vgtif_reg(g2v_notify), msg);
1029
1030 return 0;
1031}
1032
Mika Kuoppala8776f022015-06-30 18:16:40 +03001033static void gen8_free_scratch(struct i915_address_space *vm)
1034{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001035 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001036
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001037 if (USES_FULL_48BIT_PPGTT(dev_priv))
1038 free_pdp(dev_priv, vm->scratch_pdp);
1039 free_pd(dev_priv, vm->scratch_pd);
1040 free_pt(dev_priv, vm->scratch_pt);
1041 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001042}
1043
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001044static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
Michel Thierry762d9932015-07-30 11:05:29 +01001045 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001046{
1047 int i;
1048
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001049 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001050 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001051 continue;
1052
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001053 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1054 free_pd(dev_priv, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001055 }
Michel Thierry69876be2015-04-08 12:13:27 +01001056
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001057 free_pdp(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001058}
1059
1060static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1061{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001062 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Michel Thierry762d9932015-07-30 11:05:29 +01001063 int i;
1064
1065 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1066 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1067 continue;
1068
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001069 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001070 }
1071
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001072 cleanup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001073}
1074
1075static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1076{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001077 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001078 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001079
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001080 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001081 gen8_ppgtt_notify_vgt(ppgtt, false);
1082
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001083 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1084 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001085 else
1086 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001087
Mika Kuoppala8776f022015-06-30 18:16:40 +03001088 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001089}
1090
Michel Thierryd7b26332015-04-08 12:13:34 +01001091/**
1092 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001093 * @vm: Master vm structure.
1094 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001095 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001096 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001097 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1098 * caller to free on error.
1099 *
1100 * Allocate the required number of page tables. Extremely similar to
1101 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1102 * the page directory boundary (instead of the page directory pointer). That
1103 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1104 * possible, and likely that the caller will need to use multiple calls of this
1105 * function to achieve the appropriate allocation.
1106 *
1107 * Return: 0 if success; negative error code otherwise.
1108 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001109static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001110 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001111 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001112 uint64_t length,
1113 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001114{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001115 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001116 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001117 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001118
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001119 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001120 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001121 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001122 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001123 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001124 continue;
1125 }
1126
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001127 pt = alloc_pt(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001128 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001129 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001130
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001131 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001132 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001133 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001134 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001135 }
1136
1137 return 0;
1138
1139unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001140 for_each_set_bit(pde, new_pts, I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001141 free_pt(dev_priv, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001142
1143 return -ENOMEM;
1144}
1145
Michel Thierryd7b26332015-04-08 12:13:34 +01001146/**
1147 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001148 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001149 * @pdp: Page directory pointer for this address range.
1150 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001151 * @length: Size of the allocations.
1152 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001153 * caller to free on error.
1154 *
1155 * Allocate the required number of page directories starting at the pde index of
1156 * @start, and ending at the pde index @start + @length. This function will skip
1157 * over already allocated page directories within the range, and only allocate
1158 * new ones, setting the appropriate pointer within the pdp as well as the
1159 * correct position in the bitmap @new_pds.
1160 *
1161 * The function will only allocate the pages within the range for a give page
1162 * directory pointer. In other words, if @start + @length straddles a virtually
1163 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1164 * required by the caller, This is not currently possible, and the BUG in the
1165 * code will prevent it.
1166 *
1167 * Return: 0 if success; negative error code otherwise.
1168 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001169static int
1170gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1171 struct i915_page_directory_pointer *pdp,
1172 uint64_t start,
1173 uint64_t length,
1174 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001175{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001176 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001177 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001178 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001179 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001180
Michel Thierry6ac18502015-07-29 17:23:46 +01001181 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001182
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001183 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001184 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001185 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001186
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001187 pd = alloc_pd(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001188 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001189 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001190
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001191 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001192 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001193 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001194 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001195 }
1196
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001197 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001198
1199unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001200 for_each_set_bit(pdpe, new_pds, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001201 free_pd(dev_priv, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001202
1203 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001204}
1205
Michel Thierry762d9932015-07-30 11:05:29 +01001206/**
1207 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1208 * @vm: Master vm structure.
1209 * @pml4: Page map level 4 for this address range.
1210 * @start: Starting virtual address to begin allocations.
1211 * @length: Size of the allocations.
1212 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1213 * caller to free on error.
1214 *
1215 * Allocate the required number of page directory pointers. Extremely similar to
1216 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1217 * The main difference is here we are limited by the pml4 boundary (instead of
1218 * the page directory pointer).
1219 *
1220 * Return: 0 if success; negative error code otherwise.
1221 */
1222static int
1223gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1224 struct i915_pml4 *pml4,
1225 uint64_t start,
1226 uint64_t length,
1227 unsigned long *new_pdps)
1228{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001229 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierry762d9932015-07-30 11:05:29 +01001230 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001231 uint32_t pml4e;
1232
1233 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1234
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001235 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001236 if (!test_bit(pml4e, pml4->used_pml4es)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001237 pdp = alloc_pdp(dev_priv);
Michel Thierry762d9932015-07-30 11:05:29 +01001238 if (IS_ERR(pdp))
1239 goto unwind_out;
1240
Michel Thierry69ab76f2015-07-29 17:23:55 +01001241 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001242 pml4->pdps[pml4e] = pdp;
1243 __set_bit(pml4e, new_pdps);
1244 trace_i915_page_directory_pointer_entry_alloc(vm,
1245 pml4e,
1246 start,
1247 GEN8_PML4E_SHIFT);
1248 }
1249 }
1250
1251 return 0;
1252
1253unwind_out:
1254 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001255 free_pdp(dev_priv, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001256
1257 return -ENOMEM;
1258}
1259
Michel Thierryd7b26332015-04-08 12:13:34 +01001260static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001261free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001262{
Michel Thierryd7b26332015-04-08 12:13:34 +01001263 kfree(new_pts);
1264 kfree(new_pds);
1265}
1266
1267/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1268 * of these are based on the number of PDPEs in the system.
1269 */
1270static
1271int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001272 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001273 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001274{
Michel Thierryd7b26332015-04-08 12:13:34 +01001275 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001276 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001277
Michał Winiarski3a41a052015-09-03 19:22:18 +02001278 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001279 if (!pds)
1280 return -ENOMEM;
1281
Michał Winiarski3a41a052015-09-03 19:22:18 +02001282 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1283 GFP_TEMPORARY);
1284 if (!pts)
1285 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001286
1287 *new_pds = pds;
1288 *new_pts = pts;
1289
1290 return 0;
1291
1292err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001293 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001294 return -ENOMEM;
1295}
1296
Michel Thierry762d9932015-07-30 11:05:29 +01001297static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1298 struct i915_page_directory_pointer *pdp,
1299 uint64_t start,
1300 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001301{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001302 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001303 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001304 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierry5441f0c2015-04-08 12:13:28 +01001305 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001306 const uint64_t orig_start = start;
1307 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001308 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001309 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001310 int ret;
1311
Michel Thierryd7b26332015-04-08 12:13:34 +01001312 /* Wrap is never okay since we can only represent 48b, and we don't
1313 * actually use the other side of the canonical address space.
1314 */
1315 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001316 return -ENODEV;
1317
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001318 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001319 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001320
Michel Thierry6ac18502015-07-29 17:23:46 +01001321 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001322 if (ret)
1323 return ret;
1324
Michel Thierryd7b26332015-04-08 12:13:34 +01001325 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001326 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1327 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001328 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001329 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001330 return ret;
1331 }
1332
1333 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001334 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001335 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001336 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001337 if (ret)
1338 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001339 }
1340
Michel Thierry33c88192015-04-08 12:13:33 +01001341 start = orig_start;
1342 length = orig_length;
1343
Michel Thierryd7b26332015-04-08 12:13:34 +01001344 /* Allocations have completed successfully, so set the bitmaps, and do
1345 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001346 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001347 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001348 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001349 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001350 uint64_t pd_start = start;
1351 uint32_t pde;
1352
Michel Thierryd7b26332015-04-08 12:13:34 +01001353 /* Every pd should be allocated, we just did that above. */
1354 WARN_ON(!pd);
1355
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001356 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001357 /* Same reasoning as pd */
1358 WARN_ON(!pt);
1359 WARN_ON(!pd_len);
1360 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1361
1362 /* Set our used ptes within the page table */
1363 bitmap_set(pt->used_ptes,
1364 gen8_pte_index(pd_start),
1365 gen8_pte_count(pd_start, pd_len));
1366
1367 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001368 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001369
1370 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001371 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1372 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001373 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1374 gen8_pte_index(start),
1375 gen8_pte_count(start, length),
1376 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001377
1378 /* NB: We haven't yet mapped ptes to pages. At this
1379 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001380 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001381
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001382 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001383 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001384 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001385 }
1386
Michał Winiarski3a41a052015-09-03 19:22:18 +02001387 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001388 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001389 return 0;
1390
1391err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001392 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001393 unsigned long temp;
1394
Michał Winiarski3a41a052015-09-03 19:22:18 +02001395 for_each_set_bit(temp, new_page_tables + pdpe *
1396 BITS_TO_LONGS(I915_PDES), I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001397 free_pt(dev_priv,
1398 pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001399 }
1400
Michel Thierry6ac18502015-07-29 17:23:46 +01001401 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001402 free_pd(dev_priv, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001403
Michał Winiarski3a41a052015-09-03 19:22:18 +02001404 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001405 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001406 return ret;
1407}
1408
Michel Thierry762d9932015-07-30 11:05:29 +01001409static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1410 struct i915_pml4 *pml4,
1411 uint64_t start,
1412 uint64_t length)
1413{
1414 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001415 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001416 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001417 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001418 int ret = 0;
1419
1420 /* Do the pml4 allocations first, so we don't need to track the newly
1421 * allocated tables below the pdp */
1422 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1423
1424 /* The pagedirectory and pagetable allocations are done in the shared 3
1425 * and 4 level code. Just allocate the pdps.
1426 */
1427 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1428 new_pdps);
1429 if (ret)
1430 return ret;
1431
1432 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1433 "The allocation has spanned more than 512GB. "
1434 "It is highly likely this is incorrect.");
1435
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001436 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001437 WARN_ON(!pdp);
1438
1439 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1440 if (ret)
1441 goto err_out;
1442
1443 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1444 }
1445
1446 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1447 GEN8_PML4ES_PER_PML4);
1448
1449 return 0;
1450
1451err_out:
1452 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001453 gen8_ppgtt_cleanup_3lvl(to_i915(vm->dev), pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001454
1455 return ret;
1456}
1457
1458static int gen8_alloc_va_range(struct i915_address_space *vm,
1459 uint64_t start, uint64_t length)
1460{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001461 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001462
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001463 if (USES_FULL_48BIT_PPGTT(to_i915(vm->dev)))
Michel Thierry762d9932015-07-30 11:05:29 +01001464 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1465 else
1466 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1467}
1468
Michel Thierryea91e402015-07-29 17:23:57 +01001469static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1470 uint64_t start, uint64_t length,
1471 gen8_pte_t scratch_pte,
1472 struct seq_file *m)
1473{
1474 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001475 uint32_t pdpe;
1476
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001477 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001478 struct i915_page_table *pt;
1479 uint64_t pd_len = length;
1480 uint64_t pd_start = start;
1481 uint32_t pde;
1482
1483 if (!test_bit(pdpe, pdp->used_pdpes))
1484 continue;
1485
1486 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001487 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001488 uint32_t pte;
1489 gen8_pte_t *pt_vaddr;
1490
1491 if (!test_bit(pde, pd->used_pdes))
1492 continue;
1493
1494 pt_vaddr = kmap_px(pt);
1495 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1496 uint64_t va =
1497 (pdpe << GEN8_PDPE_SHIFT) |
1498 (pde << GEN8_PDE_SHIFT) |
1499 (pte << GEN8_PTE_SHIFT);
1500 int i;
1501 bool found = false;
1502
1503 for (i = 0; i < 4; i++)
1504 if (pt_vaddr[pte + i] != scratch_pte)
1505 found = true;
1506 if (!found)
1507 continue;
1508
1509 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1510 for (i = 0; i < 4; i++) {
1511 if (pt_vaddr[pte + i] != scratch_pte)
1512 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1513 else
1514 seq_puts(m, " SCRATCH ");
1515 }
1516 seq_puts(m, "\n");
1517 }
1518 /* don't use kunmap_px, it could trigger
1519 * an unnecessary flush.
1520 */
1521 kunmap_atomic(pt_vaddr);
1522 }
1523 }
1524}
1525
1526static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1527{
1528 struct i915_address_space *vm = &ppgtt->base;
1529 uint64_t start = ppgtt->base.start;
1530 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001531 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001532 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001533
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001534 if (!USES_FULL_48BIT_PPGTT(to_i915(vm->dev))) {
Michel Thierryea91e402015-07-29 17:23:57 +01001535 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1536 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001537 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001538 struct i915_pml4 *pml4 = &ppgtt->pml4;
1539 struct i915_page_directory_pointer *pdp;
1540
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001541 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001542 if (!test_bit(pml4e, pml4->used_pml4es))
1543 continue;
1544
1545 seq_printf(m, " PML4E #%llu\n", pml4e);
1546 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1547 }
1548 }
1549}
1550
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001551static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1552{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001553 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001554 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001555 int ret;
1556
1557 /* We allocate temp bitmap for page tables for no gain
1558 * but as this is for init only, lets keep the things simple
1559 */
1560 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1561 if (ret)
1562 return ret;
1563
1564 /* Allocate for all pdps regardless of how the ppgtt
1565 * was defined.
1566 */
1567 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1568 0, 1ULL << 32,
1569 new_page_dirs);
1570 if (!ret)
1571 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1572
Michał Winiarski3a41a052015-09-03 19:22:18 +02001573 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001574
1575 return ret;
1576}
1577
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001578/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001579 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1580 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1581 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1582 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001583 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001584 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001585static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001586{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001587 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001588 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001589
Mika Kuoppala8776f022015-06-30 18:16:40 +03001590 ret = gen8_init_scratch(&ppgtt->base);
1591 if (ret)
1592 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001593
Michel Thierryd7b26332015-04-08 12:13:34 +01001594 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001595 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001596 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001597 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001598 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001599 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1600 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001601 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001602
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001603 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1604 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001605 if (ret)
1606 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001607
Michel Thierry69ab76f2015-07-29 17:23:55 +01001608 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1609
Michel Thierry762d9932015-07-30 11:05:29 +01001610 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001611 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001612 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001613 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001614 if (ret)
1615 goto free_scratch;
1616
1617 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001618 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001619 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1620 0, 0,
1621 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001622
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001623 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001624 ret = gen8_preallocate_top_level_pdps(ppgtt);
1625 if (ret)
1626 goto free_scratch;
1627 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001628 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001629
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001630 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001631 gen8_ppgtt_notify_vgt(ppgtt, true);
1632
Michel Thierryd7b26332015-04-08 12:13:34 +01001633 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001634
1635free_scratch:
1636 gen8_free_scratch(&ppgtt->base);
1637 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001638}
1639
Ben Widawsky87d60b62013-12-06 14:11:29 -08001640static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1641{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001642 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001643 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001644 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001645 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001646 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001647 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001648
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001649 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001650 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001651
Dave Gordon731f74c2016-06-24 19:37:46 +01001652 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001653 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001654 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001655 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001656 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001657 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1658
1659 if (pd_entry != expected)
1660 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1661 pde,
1662 pd_entry,
1663 expected);
1664 seq_printf(m, "\tPDE: %x\n", pd_entry);
1665
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001666 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1667
Michel Thierry07749ef2015-03-16 16:00:54 +00001668 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001669 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001670 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001671 (pte * PAGE_SIZE);
1672 int i;
1673 bool found = false;
1674 for (i = 0; i < 4; i++)
1675 if (pt_vaddr[pte + i] != scratch_pte)
1676 found = true;
1677 if (!found)
1678 continue;
1679
1680 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1681 for (i = 0; i < 4; i++) {
1682 if (pt_vaddr[pte + i] != scratch_pte)
1683 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1684 else
1685 seq_puts(m, " SCRATCH ");
1686 }
1687 seq_puts(m, "\n");
1688 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001689 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001690 }
1691}
1692
Ben Widawsky678d96f2015-03-16 16:00:56 +00001693/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001694static void gen6_write_pde(struct i915_page_directory *pd,
1695 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001696{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001697 /* Caller needs to make sure the write completes if necessary */
1698 struct i915_hw_ppgtt *ppgtt =
1699 container_of(pd, struct i915_hw_ppgtt, pd);
1700 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001701
Mika Kuoppala567047b2015-06-25 18:35:12 +03001702 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001703 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001704
Ben Widawsky678d96f2015-03-16 16:00:56 +00001705 writel(pd_entry, ppgtt->pd_addr + pde);
1706}
Ben Widawsky61973492013-04-08 18:43:54 -07001707
Ben Widawsky678d96f2015-03-16 16:00:56 +00001708/* Write all the page tables found in the ppgtt structure to incrementing page
1709 * directories. */
1710static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001711 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001712 uint32_t start, uint32_t length)
1713{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001714 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001715 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001716 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001717
Dave Gordon731f74c2016-06-24 19:37:46 +01001718 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001719 gen6_write_pde(pd, pde, pt);
1720
1721 /* Make sure write is complete before other code can use this page
1722 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001723 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001724}
1725
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001726static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001727{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001728 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001729
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001730 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001731}
Ben Widawsky61973492013-04-08 18:43:54 -07001732
Ben Widawsky90252e52013-12-06 14:11:12 -08001733static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001734 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001735{
Chris Wilson7e37f882016-08-02 22:50:21 +01001736 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001737 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001738 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001739
Ben Widawsky90252e52013-12-06 14:11:12 -08001740 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001741 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001742 if (ret)
1743 return ret;
1744
John Harrison5fb9de12015-05-29 17:44:07 +01001745 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001746 if (ret)
1747 return ret;
1748
Chris Wilsonb5321f32016-08-02 22:50:18 +01001749 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1750 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1751 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1752 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1753 intel_ring_emit(ring, get_pd_offset(ppgtt));
1754 intel_ring_emit(ring, MI_NOOP);
1755 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001756
1757 return 0;
1758}
1759
Ben Widawsky48a10382013-12-06 14:11:11 -08001760static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001761 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001762{
Chris Wilson7e37f882016-08-02 22:50:21 +01001763 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001764 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001765 int ret;
1766
Ben Widawsky48a10382013-12-06 14:11:11 -08001767 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001768 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001769 if (ret)
1770 return ret;
1771
John Harrison5fb9de12015-05-29 17:44:07 +01001772 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001773 if (ret)
1774 return ret;
1775
Chris Wilsonb5321f32016-08-02 22:50:18 +01001776 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1777 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1778 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1779 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1780 intel_ring_emit(ring, get_pd_offset(ppgtt));
1781 intel_ring_emit(ring, MI_NOOP);
1782 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001783
Ben Widawsky90252e52013-12-06 14:11:12 -08001784 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001785 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001786 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001787 if (ret)
1788 return ret;
1789 }
1790
Ben Widawsky48a10382013-12-06 14:11:11 -08001791 return 0;
1792}
1793
Ben Widawskyeeb94882013-12-06 14:11:10 -08001794static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001795 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001796{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001797 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001798 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001799
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001800 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1801 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001802 return 0;
1803}
1804
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001805static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001806{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001807 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301808 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001809
Akash Goel3b3f1652016-10-13 22:44:48 +05301810 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001811 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1812 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001813 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001814 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001815 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001816}
1817
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001818static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001819{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001820 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001821 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301822 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001823
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001824 ecobits = I915_READ(GAC_ECO_BITS);
1825 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1826
1827 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001828 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001829 ecochk |= ECOCHK_PPGTT_WB_HSW;
1830 } else {
1831 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1832 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1833 }
1834 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001835
Akash Goel3b3f1652016-10-13 22:44:48 +05301836 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001837 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001838 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001839 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001840 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001841}
1842
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001843static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001844{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001845 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001846
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001847 ecobits = I915_READ(GAC_ECO_BITS);
1848 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1849 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001850
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001851 gab_ctl = I915_READ(GAB_CTL);
1852 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001853
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001854 ecochk = I915_READ(GAM_ECOCHK);
1855 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001856
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001857 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001858}
1859
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001860/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001861static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001862 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001863 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001864{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001865 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001866 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001867 unsigned first_entry = start >> PAGE_SHIFT;
1868 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001869 unsigned act_pt = first_entry / GEN6_PTES;
1870 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001871 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001872
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001873 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001874 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001875
Daniel Vetter7bddb012012-02-09 17:15:47 +01001876 while (num_entries) {
1877 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001878 if (last_pte > GEN6_PTES)
1879 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001880
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001881 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001882
1883 for (i = first_pte; i < last_pte; i++)
1884 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001885
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001886 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001887
Daniel Vetter7bddb012012-02-09 17:15:47 +01001888 num_entries -= last_pte - first_pte;
1889 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001890 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001891 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001892}
1893
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001894static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001895 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001896 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301897 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001898{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001899 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001900 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001901 unsigned act_pt = first_entry / GEN6_PTES;
1902 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001903 gen6_pte_t *pt_vaddr = NULL;
1904 struct sgt_iter sgt_iter;
1905 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001906
Dave Gordon85d12252016-05-20 11:54:06 +01001907 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001908 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001909 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001910
Chris Wilsoncc797142013-12-31 15:50:30 +00001911 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001912 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301913
Michel Thierry07749ef2015-03-16 16:00:54 +00001914 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001915 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001916 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001917 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001918 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001919 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001920 }
Dave Gordon85d12252016-05-20 11:54:06 +01001921
Chris Wilsoncc797142013-12-31 15:50:30 +00001922 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001923 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001924}
1925
Ben Widawsky678d96f2015-03-16 16:00:56 +00001926static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001927 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001928{
Michel Thierry4933d512015-03-24 15:46:22 +00001929 DECLARE_BITMAP(new_page_tables, I915_PDES);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001930 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001931 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001932 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001933 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001934 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001935 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001936 int ret;
1937
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001938 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1939 return -ENODEV;
1940
1941 start = start_save = start_in;
1942 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001943
1944 bitmap_zero(new_page_tables, I915_PDES);
1945
1946 /* The allocation is done in two stages so that we can bail out with
1947 * minimal amount of pain. The first stage finds new page tables that
1948 * need allocation. The second stage marks use ptes within the page
1949 * tables.
1950 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001951 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001952 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001953 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1954 continue;
1955 }
1956
1957 /* We've already allocated a page table */
1958 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1959
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001960 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001961 if (IS_ERR(pt)) {
1962 ret = PTR_ERR(pt);
1963 goto unwind_out;
1964 }
1965
1966 gen6_initialize_pt(vm, pt);
1967
1968 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001969 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001970 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001971 }
1972
1973 start = start_save;
1974 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001975
Dave Gordon731f74c2016-06-24 19:37:46 +01001976 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001977 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1978
1979 bitmap_zero(tmp_bitmap, GEN6_PTES);
1980 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1981 gen6_pte_count(start, length));
1982
Mika Kuoppala966082c2015-06-25 18:35:19 +03001983 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001984 gen6_write_pde(&ppgtt->pd, pde, pt);
1985
Michel Thierry72744cb2015-03-24 15:46:23 +00001986 trace_i915_page_table_entry_map(vm, pde, pt,
1987 gen6_pte_index(start),
1988 gen6_pte_count(start, length),
1989 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001990 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001991 GEN6_PTES);
1992 }
1993
Michel Thierry4933d512015-03-24 15:46:22 +00001994 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1995
1996 /* Make sure write is complete before other code can use this page
1997 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001998 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001999
Ben Widawsky563222a2015-03-19 12:53:28 +00002000 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002001 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00002002
2003unwind_out:
2004 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002005 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002006
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002007 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002008 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002009 }
2010
2011 mark_tlbs_dirty(ppgtt);
2012 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002013}
2014
Mika Kuoppala8776f022015-06-30 18:16:40 +03002015static int gen6_init_scratch(struct i915_address_space *vm)
2016{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002017 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002018 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002019
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002020 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002021 if (ret)
2022 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002023
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002024 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002025 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002026 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002027 return PTR_ERR(vm->scratch_pt);
2028 }
2029
2030 gen6_initialize_pt(vm, vm->scratch_pt);
2031
2032 return 0;
2033}
2034
2035static void gen6_free_scratch(struct i915_address_space *vm)
2036{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002037 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002038
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002039 free_pt(dev_priv, vm->scratch_pt);
2040 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002041}
2042
Daniel Vetter061dd492015-04-14 17:35:13 +02002043static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002044{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002045 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002046 struct i915_page_directory *pd = &ppgtt->pd;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002047 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierry09942c62015-04-08 12:13:30 +01002048 struct i915_page_table *pt;
2049 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002050
Daniel Vetter061dd492015-04-14 17:35:13 +02002051 drm_mm_remove_node(&ppgtt->node);
2052
Dave Gordon731f74c2016-06-24 19:37:46 +01002053 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002054 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002055 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002056
Mika Kuoppala8776f022015-06-30 18:16:40 +03002057 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002058}
2059
Ben Widawskyb1465202014-02-19 22:05:49 -08002060static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002061{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002062 struct i915_address_space *vm = &ppgtt->base;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002063 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002064 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002065 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08002066 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002067
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002068 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2069 * allocator works in address space sizes, so it's multiplied by page
2070 * size. We allocate at the top of the GTT to avoid fragmentation.
2071 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002072 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002073
Mika Kuoppala8776f022015-06-30 18:16:40 +03002074 ret = gen6_init_scratch(vm);
2075 if (ret)
2076 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002077
Ben Widawskye3cc1992013-12-06 14:11:08 -08002078alloc:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002079 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002080 &ppgtt->node, GEN6_PD_SIZE,
2081 GEN6_PD_ALIGN, 0,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002082 0, ggtt->base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07002083 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002084 if (ret == -ENOSPC && !retried) {
Chris Wilsone522ac22016-08-04 16:32:18 +01002085 ret = i915_gem_evict_something(&ggtt->base,
Ben Widawskye3cc1992013-12-06 14:11:08 -08002086 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02002087 I915_CACHE_NONE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002088 0, ggtt->base.total,
Chris Wilsond23db882014-05-23 08:48:08 +02002089 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002090 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002091 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002092
2093 retried = true;
2094 goto alloc;
2095 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002096
Ben Widawskyc8c26622015-01-22 17:01:25 +00002097 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002098 goto err_out;
2099
Ben Widawskyc8c26622015-01-22 17:01:25 +00002100
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002101 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002102 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002103
Ben Widawskyc8c26622015-01-22 17:01:25 +00002104 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002105
2106err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002107 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002108 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002109}
2110
Ben Widawskyb1465202014-02-19 22:05:49 -08002111static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2112{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002113 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002114}
2115
Michel Thierry4933d512015-03-24 15:46:22 +00002116static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2117 uint64_t start, uint64_t length)
2118{
Michel Thierryec565b32015-04-08 12:13:23 +01002119 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002120 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002121
Dave Gordon731f74c2016-06-24 19:37:46 +01002122 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002123 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002124}
2125
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002126static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002127{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002128 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002129 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002130 int ret;
2131
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002132 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002133 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002134 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002135 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002136 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002137 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002138 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002139 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002140 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002141
2142 ret = gen6_ppgtt_alloc(ppgtt);
2143 if (ret)
2144 return ret;
2145
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002146 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002147 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2148 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002149 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2150 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002151 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002152 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002153 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002154 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002155
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002156 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002157 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002158
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002159 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002160 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002161
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002162 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002163
Ben Widawsky678d96f2015-03-16 16:00:56 +00002164 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2165
Thierry Reding440fd522015-01-23 09:05:06 +01002166 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002167 ppgtt->node.size >> 20,
2168 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002169
Daniel Vetterfa76da32014-08-06 20:19:54 +02002170 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002171 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002172
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002173 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002174}
2175
Chris Wilson2bfa9962016-08-04 07:52:25 +01002176static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2177 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002178{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002179 ppgtt->base.dev = &dev_priv->drm;
Daniel Vetter3440d262013-01-24 13:49:56 -08002180
Chris Wilson2bfa9962016-08-04 07:52:25 +01002181 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002182 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002183 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002184 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002185}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002186
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002187static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002188 struct drm_i915_private *dev_priv,
2189 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002190{
Chris Wilson80b204b2016-10-28 13:58:58 +01002191 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002192 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002193 INIT_LIST_HEAD(&vm->active_list);
2194 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002195 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002196 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2197}
2198
Matthew Aulded9724d2016-11-17 21:04:10 +00002199static void i915_address_space_fini(struct i915_address_space *vm)
2200{
2201 i915_gem_timeline_fini(&vm->timeline);
2202 drm_mm_takedown(&vm->mm);
2203 list_del(&vm->global_link);
2204}
2205
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002206static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002207{
Tim Gored5165eb2016-02-04 11:49:34 +00002208 /* This function is for gtt related workarounds. This function is
2209 * called on driver load and after a GPU reset, so you can place
2210 * workarounds here even if they get overwritten by GPU reset.
2211 */
2212 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002213 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002214 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002215 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002216 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002217 else if (IS_SKYLAKE(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002218 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002219 else if (IS_BROXTON(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002220 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2221}
2222
Chris Wilson2bfa9962016-08-04 07:52:25 +01002223static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2224 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002225 struct drm_i915_file_private *file_priv,
2226 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002227{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002228 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002229
Chris Wilson2bfa9962016-08-04 07:52:25 +01002230 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002231 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002232 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002233 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002234 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002235 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002236
2237 return ret;
2238}
2239
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002240int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002241{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002242 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002243
Thomas Daniel671b50132014-08-20 16:24:50 +01002244 /* In the case of execlists, PPGTT is enabled by the context descriptor
2245 * and the PDPs are contained within the context itself. We don't
2246 * need to do anything here. */
2247 if (i915.enable_execlists)
2248 return 0;
2249
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002250 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002251 return 0;
2252
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002253 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002254 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002255 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002256 gen7_ppgtt_enable(dev_priv);
2257 else if (INTEL_GEN(dev_priv) >= 8)
2258 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002259 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002260 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002261
John Harrison4ad2fd82015-06-18 13:11:20 +01002262 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002263}
John Harrison4ad2fd82015-06-18 13:11:20 +01002264
Daniel Vetter4d884702014-08-06 15:04:47 +02002265struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002266i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002267 struct drm_i915_file_private *fpriv,
2268 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002269{
2270 struct i915_hw_ppgtt *ppgtt;
2271 int ret;
2272
2273 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2274 if (!ppgtt)
2275 return ERR_PTR(-ENOMEM);
2276
Chris Wilson80b204b2016-10-28 13:58:58 +01002277 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002278 if (ret) {
2279 kfree(ppgtt);
2280 return ERR_PTR(ret);
2281 }
2282
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002283 trace_i915_ppgtt_create(&ppgtt->base);
2284
Daniel Vetter4d884702014-08-06 15:04:47 +02002285 return ppgtt;
2286}
2287
Matthew Aulded9724d2016-11-17 21:04:10 +00002288void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002289{
2290 struct i915_hw_ppgtt *ppgtt =
2291 container_of(kref, struct i915_hw_ppgtt, ref);
2292
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002293 trace_i915_ppgtt_release(&ppgtt->base);
2294
Chris Wilson50e046b2016-08-04 07:52:46 +01002295 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002296 WARN_ON(!list_empty(&ppgtt->base.active_list));
2297 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002298 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002299
Matthew Aulded9724d2016-11-17 21:04:10 +00002300 i915_address_space_fini(&ppgtt->base);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002301
Daniel Vetteree960be2014-08-06 15:04:45 +02002302 ppgtt->base.cleanup(&ppgtt->base);
2303 kfree(ppgtt);
2304}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002305
Ben Widawskya81cc002013-01-18 12:30:31 -08002306/* Certain Gen5 chipsets require require idling the GPU before
2307 * unmapping anything from the GTT when VT-d is enabled.
2308 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002309static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002310{
2311#ifdef CONFIG_INTEL_IOMMU
2312 /* Query intel_iommu to see if we need the workaround. Presumably that
2313 * was loaded first.
2314 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002315 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002316 return true;
2317#endif
2318 return false;
2319}
2320
Chris Wilsondc979972016-05-10 14:10:04 +01002321void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002322{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002323 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302324 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002325
Chris Wilsondc979972016-05-10 14:10:04 +01002326 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002327 return;
2328
Akash Goel3b3f1652016-10-13 22:44:48 +05302329 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002330 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002331 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002332 if (fault_reg & RING_FAULT_VALID) {
2333 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002334 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002335 "\tAddress space: %s\n"
2336 "\tSource ID: %d\n"
2337 "\tType: %d\n",
2338 fault_reg & PAGE_MASK,
2339 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2340 RING_FAULT_SRCID(fault_reg),
2341 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002342 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002343 fault_reg & ~RING_FAULT_VALID);
2344 }
2345 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302346
2347 /* Engine specific init may not have been done till this point. */
2348 if (dev_priv->engine[RCS])
2349 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002350}
2351
Chris Wilson91e56492014-09-25 10:13:12 +01002352static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2353{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002354 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002355 intel_gtt_chipset_flush();
2356 } else {
2357 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2358 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2359 }
2360}
2361
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002362void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002363{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002364 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002365
2366 /* Don't bother messing with faults pre GEN6 as we have little
2367 * documentation supporting that it's a good idea.
2368 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002369 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002370 return;
2371
Chris Wilsondc979972016-05-10 14:10:04 +01002372 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002373
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002374 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002375
2376 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002377}
2378
Chris Wilson03ac84f2016-10-28 13:58:36 +01002379int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2380 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002381{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002382 if (dma_map_sg(&obj->base.dev->pdev->dev,
2383 pages->sgl, pages->nents,
2384 PCI_DMA_BIDIRECTIONAL))
2385 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01002386
Chris Wilson03ac84f2016-10-28 13:58:36 +01002387 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002388}
2389
Daniel Vetter2c642b02015-04-14 17:35:26 +02002390static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002391{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002392 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002393}
2394
Chris Wilsond6473f52016-06-10 14:22:59 +05302395static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2396 dma_addr_t addr,
2397 uint64_t offset,
2398 enum i915_cache_level level,
2399 u32 unused)
2400{
2401 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2402 gen8_pte_t __iomem *pte =
2403 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2404 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302405
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002406 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302407
2408 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2409 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302410}
2411
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002412static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2413 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002414 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302415 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002416{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002417 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002418 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002419 struct sgt_iter sgt_iter;
2420 gen8_pte_t __iomem *gtt_entries;
2421 gen8_pte_t gtt_entry;
2422 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002423 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002424
Dave Gordon85d12252016-05-20 11:54:06 +01002425 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2426
2427 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002428 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002429 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002430 }
2431
2432 /*
2433 * XXX: This serves as a posting read to make sure that the PTE has
2434 * actually been updated. There is some concern that even though
2435 * registers and PTEs are within the same BAR that they are potentially
2436 * of NUMA access patterns. Therefore, even with the way we assume
2437 * hardware should work, we must keep this posting read for paranoia.
2438 */
2439 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002440 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002441
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002442 /* This next bit makes the above posting read even more important. We
2443 * want to flush the TLBs only after we're certain all the PTE updates
2444 * have finished.
2445 */
2446 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2447 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002448}
2449
Chris Wilsonc1403302015-11-18 15:19:39 +00002450struct insert_entries {
2451 struct i915_address_space *vm;
2452 struct sg_table *st;
2453 uint64_t start;
2454 enum i915_cache_level level;
2455 u32 flags;
2456};
2457
2458static int gen8_ggtt_insert_entries__cb(void *_arg)
2459{
2460 struct insert_entries *arg = _arg;
2461 gen8_ggtt_insert_entries(arg->vm, arg->st,
2462 arg->start, arg->level, arg->flags);
2463 return 0;
2464}
2465
2466static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2467 struct sg_table *st,
2468 uint64_t start,
2469 enum i915_cache_level level,
2470 u32 flags)
2471{
2472 struct insert_entries arg = { vm, st, start, level, flags };
2473 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2474}
2475
Chris Wilsond6473f52016-06-10 14:22:59 +05302476static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2477 dma_addr_t addr,
2478 uint64_t offset,
2479 enum i915_cache_level level,
2480 u32 flags)
2481{
2482 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2483 gen6_pte_t __iomem *pte =
2484 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2485 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302486
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002487 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302488
2489 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2490 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302491}
2492
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002493/*
2494 * Binds an object into the global gtt with the specified cache level. The object
2495 * will be accessible to the GPU via commands whose operands reference offsets
2496 * within the global GTT as well as accessible by the GPU through the GMADR
2497 * mapped BAR (dev_priv->mm.gtt->gtt).
2498 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002499static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002500 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002501 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302502 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002503{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002504 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002505 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002506 struct sgt_iter sgt_iter;
2507 gen6_pte_t __iomem *gtt_entries;
2508 gen6_pte_t gtt_entry;
2509 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002510 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002511
Dave Gordon85d12252016-05-20 11:54:06 +01002512 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2513
2514 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002515 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002516 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002517 }
2518
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002519 /* XXX: This serves as a posting read to make sure that the PTE has
2520 * actually been updated. There is some concern that even though
2521 * registers and PTEs are within the same BAR that they are potentially
2522 * of NUMA access patterns. Therefore, even with the way we assume
2523 * hardware should work, we must keep this posting read for paranoia.
2524 */
Dave Gordon85d12252016-05-20 11:54:06 +01002525 if (i != 0)
2526 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002527
2528 /* This next bit makes the above posting read even more important. We
2529 * want to flush the TLBs only after we're certain all the PTE updates
2530 * have finished.
2531 */
2532 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2533 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002534}
2535
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002536static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002537 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002538{
2539}
2540
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002541static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002542 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002543{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002544 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002545 unsigned first_entry = start >> PAGE_SHIFT;
2546 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002547 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002548 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2549 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002550 int i;
2551
2552 if (WARN(num_entries > max_entries,
2553 "First entry = %d; Num entries = %d (max=%d)\n",
2554 first_entry, num_entries, max_entries))
2555 num_entries = max_entries;
2556
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002557 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002558 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002559 for (i = 0; i < num_entries; i++)
2560 gen8_set_pte(&gtt_base[i], scratch_pte);
2561 readl(gtt_base);
2562}
2563
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002564static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002565 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002566 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002567{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002568 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002569 unsigned first_entry = start >> PAGE_SHIFT;
2570 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002571 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002572 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2573 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002574 int i;
2575
2576 if (WARN(num_entries > max_entries,
2577 "First entry = %d; Num entries = %d (max=%d)\n",
2578 first_entry, num_entries, max_entries))
2579 num_entries = max_entries;
2580
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002581 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002582 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002583
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002584 for (i = 0; i < num_entries; i++)
2585 iowrite32(scratch_pte, &gtt_base[i]);
2586 readl(gtt_base);
2587}
2588
Chris Wilsond6473f52016-06-10 14:22:59 +05302589static void i915_ggtt_insert_page(struct i915_address_space *vm,
2590 dma_addr_t addr,
2591 uint64_t offset,
2592 enum i915_cache_level cache_level,
2593 u32 unused)
2594{
Chris Wilsond6473f52016-06-10 14:22:59 +05302595 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2596 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302597
2598 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302599}
2600
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002601static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2602 struct sg_table *pages,
2603 uint64_t start,
2604 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002605{
2606 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2607 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2608
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002609 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002610
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002611}
2612
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002613static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002614 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002615 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002616{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002617 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002618}
2619
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002620static int ggtt_bind_vma(struct i915_vma *vma,
2621 enum i915_cache_level cache_level,
2622 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002623{
Chris Wilson9c870d02016-10-24 13:42:15 +01002624 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Daniel Vetter0a878712015-10-15 14:23:01 +02002625 struct drm_i915_gem_object *obj = vma->obj;
2626 u32 pte_flags = 0;
2627 int ret;
2628
2629 ret = i915_get_ggtt_vma_pages(vma);
2630 if (ret)
2631 return ret;
2632
2633 /* Currently applicable only to VLV */
2634 if (obj->gt_ro)
2635 pte_flags |= PTE_READ_ONLY;
2636
Chris Wilson9c870d02016-10-24 13:42:15 +01002637 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002638 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002639 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002640 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002641
2642 /*
2643 * Without aliasing PPGTT there's no difference between
2644 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2645 * upgrade to both bound if we bind either to avoid double-binding.
2646 */
Chris Wilson3272db52016-08-04 16:32:32 +01002647 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002648
2649 return 0;
2650}
2651
2652static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2653 enum i915_cache_level cache_level,
2654 u32 flags)
2655{
Chris Wilson9c870d02016-10-24 13:42:15 +01002656 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Chris Wilson321d1782015-11-20 10:27:18 +00002657 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002658 int ret;
2659
2660 ret = i915_get_ggtt_vma_pages(vma);
2661 if (ret)
2662 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002663
Akash Goel24f3a8c2014-06-17 10:59:42 +05302664 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002665 pte_flags = 0;
2666 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002667 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302668
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002669
Chris Wilson3272db52016-08-04 16:32:32 +01002670 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002671 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002672 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002673 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002674 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002675 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002676 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002677
Chris Wilson3272db52016-08-04 16:32:32 +01002678 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002679 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002680 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002681 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002682 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002683 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002684
2685 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002686}
2687
2688static void ggtt_unbind_vma(struct i915_vma *vma)
2689{
Chris Wilson9c870d02016-10-24 13:42:15 +01002690 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
2691 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002692 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002693
Chris Wilson9c870d02016-10-24 13:42:15 +01002694 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2695 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002696 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002697 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002698 intel_runtime_pm_put(i915);
2699 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002700
Chris Wilson3272db52016-08-04 16:32:32 +01002701 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002702 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002703 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002704}
2705
Chris Wilson03ac84f2016-10-28 13:58:36 +01002706void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2707 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002708{
David Weinehall52a05c32016-08-22 13:32:44 +03002709 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2710 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002711 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002712
Chris Wilson307dc252016-08-05 10:14:12 +01002713 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002714 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002715 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2716 /* Wait a bit, in hopes it avoids the hang */
2717 udelay(10);
2718 }
2719 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002720
Chris Wilson03ac84f2016-10-28 13:58:36 +01002721 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002722}
Daniel Vetter644ec022012-03-26 09:45:40 +02002723
Chris Wilson42d6ab42012-07-26 11:49:32 +01002724static void i915_gtt_color_adjust(struct drm_mm_node *node,
2725 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002726 u64 *start,
2727 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002728{
2729 if (node->color != color)
2730 *start += 4096;
2731
Chris Wilson2a1d7752016-07-26 12:01:51 +01002732 node = list_first_entry_or_null(&node->node_list,
2733 struct drm_mm_node,
2734 node_list);
2735 if (node && node->allocated && node->color != color)
2736 *end -= 4096;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002737}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002738
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002739int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002740{
Ben Widawskye78891c2013-01-25 16:41:04 -08002741 /* Let GEM Manage all of the aperture.
2742 *
2743 * However, leave one page at the end still bound to the scratch page.
2744 * There are a number of places where the hardware apparently prefetches
2745 * past the end of the object, and we've seen multiple hangs with the
2746 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2747 * aperture. One page should be enough to keep any prefetching inside
2748 * of the aperture.
2749 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002750 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002751 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002752 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002753 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002754 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002755
Zhi Wangb02d22a2016-06-16 08:06:59 -04002756 ret = intel_vgt_balloon(dev_priv);
2757 if (ret)
2758 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002759
Chris Wilson95374d72016-10-12 10:05:20 +01002760 /* Reserve a mappable slot for our lockless error capture */
2761 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2762 &ggtt->error_capture,
2763 4096, 0, -1,
2764 0, ggtt->mappable_end,
2765 0, 0);
2766 if (ret)
2767 return ret;
2768
Chris Wilsoned2f3452012-11-15 11:32:19 +00002769 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002770 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002771 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2772 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002773 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002774 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002775 }
2776
2777 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002778 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002779 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002780
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002781 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002782 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002783 if (!ppgtt) {
2784 ret = -ENOMEM;
2785 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002786 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002787
Chris Wilson95374d72016-10-12 10:05:20 +01002788 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2789 if (ret)
2790 goto err_ppgtt;
2791
2792 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002793 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2794 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002795 if (ret)
2796 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002797 }
2798
2799 ppgtt->base.clear_range(&ppgtt->base,
2800 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002801 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002802
Daniel Vetterfa76da32014-08-06 20:19:54 +02002803 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002804 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2805 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002806 }
2807
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002808 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002809
2810err_ppgtt_cleanup:
2811 ppgtt->base.cleanup(&ppgtt->base);
2812err_ppgtt:
2813 kfree(ppgtt);
2814err:
2815 drm_mm_remove_node(&ggtt->error_capture);
2816 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002817}
2818
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002819/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002820 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002821 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002822 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002823void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002824{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002825 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002826
Daniel Vetter70e32542014-08-06 15:04:57 +02002827 if (dev_priv->mm.aliasing_ppgtt) {
2828 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002829 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002830 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002831 }
2832
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002833 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002834
Chris Wilson95374d72016-10-12 10:05:20 +01002835 if (drm_mm_node_allocated(&ggtt->error_capture))
2836 drm_mm_remove_node(&ggtt->error_capture);
2837
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002838 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002839 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002840
Matthew Aulded9724d2016-11-17 21:04:10 +00002841 mutex_lock(&dev_priv->drm.struct_mutex);
2842 i915_address_space_fini(&ggtt->base);
2843 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002844 }
2845
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002846 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002847
2848 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002849 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002850}
Daniel Vetter70e32542014-08-06 15:04:57 +02002851
Daniel Vetter2c642b02015-04-14 17:35:26 +02002852static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002853{
2854 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2855 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2856 return snb_gmch_ctl << 20;
2857}
2858
Daniel Vetter2c642b02015-04-14 17:35:26 +02002859static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002860{
2861 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2862 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2863 if (bdw_gmch_ctl)
2864 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002865
2866#ifdef CONFIG_X86_32
2867 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2868 if (bdw_gmch_ctl > 4)
2869 bdw_gmch_ctl = 4;
2870#endif
2871
Ben Widawsky9459d252013-11-03 16:53:55 -08002872 return bdw_gmch_ctl << 20;
2873}
2874
Daniel Vetter2c642b02015-04-14 17:35:26 +02002875static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002876{
2877 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2878 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2879
2880 if (gmch_ctrl)
2881 return 1 << (20 + gmch_ctrl);
2882
2883 return 0;
2884}
2885
Daniel Vetter2c642b02015-04-14 17:35:26 +02002886static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002887{
2888 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2889 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2890 return snb_gmch_ctl << 25; /* 32 MB units */
2891}
2892
Daniel Vetter2c642b02015-04-14 17:35:26 +02002893static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002894{
2895 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2896 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2897 return bdw_gmch_ctl << 25; /* 32 MB units */
2898}
2899
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002900static size_t chv_get_stolen_size(u16 gmch_ctrl)
2901{
2902 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2903 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2904
2905 /*
2906 * 0x0 to 0x10: 32MB increments starting at 0MB
2907 * 0x11 to 0x16: 4MB increments starting at 8MB
2908 * 0x17 to 0x1d: 4MB increments start at 36MB
2909 */
2910 if (gmch_ctrl < 0x11)
2911 return gmch_ctrl << 25;
2912 else if (gmch_ctrl < 0x17)
2913 return (gmch_ctrl - 0x11 + 2) << 22;
2914 else
2915 return (gmch_ctrl - 0x17 + 9) << 22;
2916}
2917
Damien Lespiau66375012014-01-09 18:02:46 +00002918static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2919{
2920 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2921 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2922
2923 if (gen9_gmch_ctl < 0xf0)
2924 return gen9_gmch_ctl << 25; /* 32 MB units */
2925 else
2926 /* 4MB increments starting at 0xf0 for 4MB */
2927 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2928}
2929
Chris Wilson34c998b2016-08-04 07:52:24 +01002930static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002931{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002932 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Chris Wilson34c998b2016-08-04 07:52:24 +01002933 struct pci_dev *pdev = ggtt->base.dev->pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002934 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002935 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002936
2937 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002938 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002939
Imre Deak2a073f892015-03-27 13:07:33 +02002940 /*
2941 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2942 * dropped. For WC mappings in general we have 64 byte burst writes
2943 * when the WC buffer is flushed, so we can't use it, but have to
2944 * resort to an uncached mapping. The WC issue is easily caught by the
2945 * readback check when writing GTT PTE entries.
2946 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002947 if (IS_BROXTON(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002948 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002949 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002950 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002951 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002952 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002953 return -ENOMEM;
2954 }
2955
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002956 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002957 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002958 DRM_ERROR("Scratch setup failed\n");
2959 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002960 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002961 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002962 }
2963
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002964 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002965}
2966
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002967/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2968 * bits. When using advanced contexts each context stores its own PAT, but
2969 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002970static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002971{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002972 uint64_t pat;
2973
2974 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2975 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2976 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2977 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2978 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2979 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2980 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2981 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2982
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002983 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002984 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2985 * so RTL will always use the value corresponding to
2986 * pat_sel = 000".
2987 * So let's disable cache for GGTT to avoid screen corruptions.
2988 * MOCS still can be used though.
2989 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2990 * before this patch, i.e. the same uncached + snooping access
2991 * like on gen6/7 seems to be in effect.
2992 * - So this just fixes blitter/render access. Again it looks
2993 * like it's not just uncached access, but uncached + snooping.
2994 * So we can still hold onto all our assumptions wrt cpu
2995 * clflushing on LLC machines.
2996 */
2997 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2998
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002999 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3000 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003001 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3002 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003003}
3004
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003005static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3006{
3007 uint64_t pat;
3008
3009 /*
3010 * Map WB on BDW to snooped on CHV.
3011 *
3012 * Only the snoop bit has meaning for CHV, the rest is
3013 * ignored.
3014 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003015 * The hardware will never snoop for certain types of accesses:
3016 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3017 * - PPGTT page tables
3018 * - some other special cycles
3019 *
3020 * As with BDW, we also need to consider the following for GT accesses:
3021 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3022 * so RTL will always use the value corresponding to
3023 * pat_sel = 000".
3024 * Which means we must set the snoop bit in PAT entry 0
3025 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003026 */
3027 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3028 GEN8_PPAT(1, 0) |
3029 GEN8_PPAT(2, 0) |
3030 GEN8_PPAT(3, 0) |
3031 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3032 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3033 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3034 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3035
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003036 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3037 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003038}
3039
Chris Wilson34c998b2016-08-04 07:52:24 +01003040static void gen6_gmch_remove(struct i915_address_space *vm)
3041{
3042 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3043
3044 iounmap(ggtt->gsm);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003045 cleanup_scratch_page(to_i915(vm->dev), &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003046}
3047
Joonas Lahtinend507d732016-03-18 10:42:58 +02003048static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003049{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003050 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3051 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003052 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003053 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003054
3055 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003056 ggtt->mappable_base = pci_resource_start(pdev, 2);
3057 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003058
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003059 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3060 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003061
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003062 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003063
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003064 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003065 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003066 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003067 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003068 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003069 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003070 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003071 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003072 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003073 }
Ben Widawsky63340132013-11-04 19:32:22 -08003074
Chris Wilson34c998b2016-08-04 07:52:24 +01003075 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003076
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003077 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003078 chv_setup_private_ppat(dev_priv);
3079 else
3080 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003081
Chris Wilson34c998b2016-08-04 07:52:24 +01003082 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003083 ggtt->base.bind_vma = ggtt_bind_vma;
3084 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303085 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003086 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003087 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003088 ggtt->base.clear_range = gen8_ggtt_clear_range;
3089
3090 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3091 if (IS_CHERRYVIEW(dev_priv))
3092 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3093
Chris Wilson34c998b2016-08-04 07:52:24 +01003094 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003095}
3096
Joonas Lahtinend507d732016-03-18 10:42:58 +02003097static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003098{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003099 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3100 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003101 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003102 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003103
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003104 ggtt->mappable_base = pci_resource_start(pdev, 2);
3105 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003106
Ben Widawskybaa09f52013-01-24 13:49:57 -08003107 /* 64/512MB is the current min/max we actually know of, but this is just
3108 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003109 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003110 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003111 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003112 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003113 }
3114
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003115 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3116 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3117 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003118
Joonas Lahtinend507d732016-03-18 10:42:58 +02003119 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003120
Chris Wilson34c998b2016-08-04 07:52:24 +01003121 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3122 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003123
Joonas Lahtinend507d732016-03-18 10:42:58 +02003124 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303125 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003126 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3127 ggtt->base.bind_vma = ggtt_bind_vma;
3128 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003129 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003130
Chris Wilson34c998b2016-08-04 07:52:24 +01003131 if (HAS_EDRAM(dev_priv))
3132 ggtt->base.pte_encode = iris_pte_encode;
3133 else if (IS_HASWELL(dev_priv))
3134 ggtt->base.pte_encode = hsw_pte_encode;
3135 else if (IS_VALLEYVIEW(dev_priv))
3136 ggtt->base.pte_encode = byt_pte_encode;
3137 else if (INTEL_GEN(dev_priv) >= 7)
3138 ggtt->base.pte_encode = ivb_pte_encode;
3139 else
3140 ggtt->base.pte_encode = snb_pte_encode;
3141
3142 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003143}
3144
Chris Wilson34c998b2016-08-04 07:52:24 +01003145static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003146{
Chris Wilson34c998b2016-08-04 07:52:24 +01003147 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003148}
3149
Joonas Lahtinend507d732016-03-18 10:42:58 +02003150static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003151{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003152 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003153 int ret;
3154
Chris Wilson91c8a322016-07-05 10:40:23 +01003155 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003156 if (!ret) {
3157 DRM_ERROR("failed to set up gmch\n");
3158 return -EIO;
3159 }
3160
Joonas Lahtinend507d732016-03-18 10:42:58 +02003161 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3162 &ggtt->mappable_base, &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003163
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003164 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303165 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003166 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3167 ggtt->base.clear_range = i915_ggtt_clear_range;
3168 ggtt->base.bind_vma = ggtt_bind_vma;
3169 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003170 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003171
Joonas Lahtinend507d732016-03-18 10:42:58 +02003172 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003173 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3174
Ben Widawskybaa09f52013-01-24 13:49:57 -08003175 return 0;
3176}
3177
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003178/**
Chris Wilson0088e522016-08-04 07:52:21 +01003179 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003180 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003181 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003182int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003183{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003184 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003185 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003186
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003187 ggtt->base.dev = &dev_priv->drm;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003188
Chris Wilson34c998b2016-08-04 07:52:24 +01003189 if (INTEL_GEN(dev_priv) <= 5)
3190 ret = i915_gmch_probe(ggtt);
3191 else if (INTEL_GEN(dev_priv) < 8)
3192 ret = gen6_gmch_probe(ggtt);
3193 else
3194 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003195 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003196 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003197
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003198 if ((ggtt->base.total - 1) >> 32) {
3199 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003200 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003201 ggtt->base.total >> 20);
3202 ggtt->base.total = 1ULL << 32;
3203 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3204 }
3205
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003206 if (ggtt->mappable_end > ggtt->base.total) {
3207 DRM_ERROR("mappable aperture extends past end of GGTT,"
3208 " aperture=%llx, total=%llx\n",
3209 ggtt->mappable_end, ggtt->base.total);
3210 ggtt->mappable_end = ggtt->base.total;
3211 }
3212
Ben Widawskybaa09f52013-01-24 13:49:57 -08003213 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003214 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003215 ggtt->base.total >> 20);
3216 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3217 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003218#ifdef CONFIG_INTEL_IOMMU
3219 if (intel_iommu_gfx_mapped)
3220 DRM_INFO("VT-d active for gfx access\n");
3221#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003222
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003223 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003224}
3225
3226/**
3227 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003228 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003229 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003230int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003231{
Chris Wilson0088e522016-08-04 07:52:21 +01003232 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3233 int ret;
3234
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003235 INIT_LIST_HEAD(&dev_priv->vm_list);
3236
3237 /* Subtract the guard page before address space initialization to
3238 * shrink the range used by drm_mm.
3239 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003240 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003241 ggtt->base.total -= PAGE_SIZE;
Chris Wilson80b204b2016-10-28 13:58:58 +01003242 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003243 ggtt->base.total += PAGE_SIZE;
3244 if (!HAS_LLC(dev_priv))
3245 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003246 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003247
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003248 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3249 dev_priv->ggtt.mappable_base,
3250 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003251 ret = -EIO;
3252 goto out_gtt_cleanup;
3253 }
3254
3255 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3256
Chris Wilson0088e522016-08-04 07:52:21 +01003257 /*
3258 * Initialise stolen early so that we may reserve preallocated
3259 * objects for the BIOS to KMS transition.
3260 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003261 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003262 if (ret)
3263 goto out_gtt_cleanup;
3264
3265 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003266
3267out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003268 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003269 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003270}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003271
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003272int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003273{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003274 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003275 return -EIO;
3276
3277 return 0;
3278}
3279
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003280void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003281{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003282 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003283 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003284
Chris Wilsondc979972016-05-10 14:10:04 +01003285 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003286
3287 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003288 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003289
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003290 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3291
3292 /* clflush objects bound into the GGTT and rebind them. */
3293 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003294 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003295 bool ggtt_bound = false;
3296 struct i915_vma *vma;
3297
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003298 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003299 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003300 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003301
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003302 if (!i915_vma_unbind(vma))
3303 continue;
3304
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003305 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3306 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003307 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003308 }
3309
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003310 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003311 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003312 }
3313
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003314 ggtt->base.closed = false;
3315
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003316 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01003317 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003318 chv_setup_private_ppat(dev_priv);
3319 else
3320 bdw_setup_private_ppat(dev_priv);
3321
3322 return;
3323 }
3324
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003325 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003326 struct i915_address_space *vm;
3327
Daniel Vetterfa423312015-04-14 17:35:23 +02003328 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3329 /* TODO: Perhaps it shouldn't be gen6 specific */
3330
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003331 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003332
Chris Wilson2bfa9962016-08-04 07:52:25 +01003333 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003334 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003335 else
3336 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003337
3338 gen6_write_page_range(dev_priv, &ppgtt->pd,
3339 0, ppgtt->base.total);
3340 }
3341 }
3342
3343 i915_ggtt_flush(dev_priv);
3344}
3345
Chris Wilson058d88c2016-08-15 10:49:06 +01003346struct i915_vma *
3347i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3348 struct i915_address_space *vm,
3349 const struct i915_ggtt_view *view)
3350{
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003351 struct rb_node *rb;
Chris Wilson058d88c2016-08-15 10:49:06 +01003352
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003353 rb = obj->vma_tree.rb_node;
3354 while (rb) {
3355 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
3356 long cmp;
3357
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003358 cmp = i915_vma_compare(vma, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003359 if (cmp == 0)
Chris Wilson058d88c2016-08-15 10:49:06 +01003360 return vma;
3361
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003362 if (cmp < 0)
3363 rb = rb->rb_right;
3364 else
3365 rb = rb->rb_left;
3366 }
3367
Chris Wilson058d88c2016-08-15 10:49:06 +01003368 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003369}
3370
3371struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003372i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003373 struct i915_address_space *vm,
3374 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003375{
3376 struct i915_vma *vma;
3377
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003378 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +01003379 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3380
3381 vma = i915_gem_obj_to_vma(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003382 if (!vma) {
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003383 vma = i915_vma_create(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003384 GEM_BUG_ON(vma != i915_gem_obj_to_vma(obj, vm, view));
3385 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003386
Chris Wilson3272db52016-08-04 16:32:32 +01003387 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003388 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003389}
3390
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003391static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003392rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003393 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003394 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003395 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003396{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003397 unsigned int column, row;
3398 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003399
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003400 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003401 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003402 for (row = 0; row < height; row++) {
3403 st->nents++;
3404 /* We don't need the pages, but need to initialize
3405 * the entries so the sg list can be happily traversed.
3406 * The only thing we need are DMA addresses.
3407 */
3408 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003409 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003410 sg_dma_len(sg) = PAGE_SIZE;
3411 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003412 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003413 }
3414 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003415
3416 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003417}
3418
3419static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003420intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003421 struct drm_i915_gem_object *obj)
3422{
Dave Gordon85d12252016-05-20 11:54:06 +01003423 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003424 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003425 struct sgt_iter sgt_iter;
3426 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003427 unsigned long i;
3428 dma_addr_t *page_addr_list;
3429 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003430 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003431 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003432
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003433 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003434 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003435 sizeof(dma_addr_t),
3436 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003437 if (!page_addr_list)
3438 return ERR_PTR(ret);
3439
3440 /* Allocate target SG list. */
3441 st = kmalloc(sizeof(*st), GFP_KERNEL);
3442 if (!st)
3443 goto err_st_alloc;
3444
Ville Syrjälä6687c902015-09-15 13:16:41 +03003445 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003446 if (ret)
3447 goto err_sg_alloc;
3448
3449 /* Populate source page list from the object. */
3450 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003451 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003452 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003453
Dave Gordon85d12252016-05-20 11:54:06 +01003454 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003455 st->nents = 0;
3456 sg = st->sgl;
3457
Ville Syrjälä6687c902015-09-15 13:16:41 +03003458 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3459 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3460 rot_info->plane[i].width, rot_info->plane[i].height,
3461 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003462 }
3463
Ville Syrjälä6687c902015-09-15 13:16:41 +03003464 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3465 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003466
3467 drm_free_large(page_addr_list);
3468
3469 return st;
3470
3471err_sg_alloc:
3472 kfree(st);
3473err_st_alloc:
3474 drm_free_large(page_addr_list);
3475
Ville Syrjälä6687c902015-09-15 13:16:41 +03003476 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3477 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3478
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003479 return ERR_PTR(ret);
3480}
3481
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003482static struct sg_table *
3483intel_partial_pages(const struct i915_ggtt_view *view,
3484 struct drm_i915_gem_object *obj)
3485{
3486 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003487 struct scatterlist *sg, *iter;
3488 unsigned int count = view->params.partial.size;
3489 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003490 int ret = -ENOMEM;
3491
3492 st = kmalloc(sizeof(*st), GFP_KERNEL);
3493 if (!st)
3494 goto err_st_alloc;
3495
Chris Wilsond2a84a72016-10-28 13:58:34 +01003496 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003497 if (ret)
3498 goto err_sg_alloc;
3499
Chris Wilsond2a84a72016-10-28 13:58:34 +01003500 iter = i915_gem_object_get_sg(obj,
3501 view->params.partial.offset,
3502 &offset);
3503 GEM_BUG_ON(!iter);
3504
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003505 sg = st->sgl;
3506 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003507 do {
3508 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003509
Chris Wilsond2a84a72016-10-28 13:58:34 +01003510 len = min(iter->length - (offset << PAGE_SHIFT),
3511 count << PAGE_SHIFT);
3512 sg_set_page(sg, NULL, len, 0);
3513 sg_dma_address(sg) =
3514 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3515 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003516
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003517 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003518 count -= len >> PAGE_SHIFT;
3519 if (count == 0) {
3520 sg_mark_end(sg);
3521 return st;
3522 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003523
Chris Wilsond2a84a72016-10-28 13:58:34 +01003524 sg = __sg_next(sg);
3525 iter = __sg_next(iter);
3526 offset = 0;
3527 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003528
3529err_sg_alloc:
3530 kfree(st);
3531err_st_alloc:
3532 return ERR_PTR(ret);
3533}
3534
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003535static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003536i915_get_ggtt_vma_pages(struct i915_vma *vma)
3537{
3538 int ret = 0;
3539
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003540 /* The vma->pages are only valid within the lifespan of the borrowed
3541 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3542 * must be the vma->pages. A simple rule is that vma->pages must only
3543 * be accessed when the obj->mm.pages are pinned.
3544 */
3545 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3546
Chris Wilson247177d2016-08-15 10:48:47 +01003547 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003548 return 0;
3549
3550 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003551 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003552 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003553 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003554 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003555 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003556 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003557 else
3558 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3559 vma->ggtt_view.type);
3560
Chris Wilson247177d2016-08-15 10:48:47 +01003561 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003562 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003563 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003564 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003565 } else if (IS_ERR(vma->pages)) {
3566 ret = PTR_ERR(vma->pages);
3567 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003568 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3569 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003570 }
3571
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003572 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003573}
3574