blob: efc7a6b1d6f1f3fa3e1952165239a05567131fcb [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
Chris Wilsonaae4a3d2017-02-13 17:15:44 +000026#include <linux/slab.h> /* fault-inject.h is not standalone! */
27
28#include <linux/fault-inject.h>
Chris Wilsone007b192017-01-11 11:23:10 +000029#include <linux/log2.h>
Chris Wilson606fec92017-01-11 11:23:12 +000030#include <linux/random.h>
Daniel Vetter0e46ce22014-01-08 16:10:27 +010031#include <linux/seq_file.h>
Chris Wilson5bab6f62015-10-23 18:43:32 +010032#include <linux/stop_machine.h>
Chris Wilsone007b192017-01-11 11:23:10 +000033
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drmP.h>
35#include <drm/i915_drm.h>
Chris Wilsone007b192017-01-11 11:23:10 +000036
Daniel Vetter76aaf222010-11-05 22:23:30 +010037#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080038#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010039#include "i915_trace.h"
40#include "intel_drv.h"
Chris Wilsond07f0e52016-10-28 13:58:44 +010041#include "intel_frontbuffer.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010042
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010043#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
44
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000045/**
46 * DOC: Global GTT views
47 *
48 * Background and previous state
49 *
50 * Historically objects could exists (be bound) in global GTT space only as
51 * singular instances with a view representing all of the object's backing pages
52 * in a linear fashion. This view will be called a normal view.
53 *
54 * To support multiple views of the same object, where the number of mapped
55 * pages is not equal to the backing store, or where the layout of the pages
56 * is not linear, concept of a GGTT view was added.
57 *
58 * One example of an alternative view is a stereo display driven by a single
59 * image. In this case we would have a framebuffer looking like this
60 * (2x2 pages):
61 *
62 * 12
63 * 34
64 *
65 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
66 * rendering. In contrast, fed to the display engine would be an alternative
67 * view which could look something like this:
68 *
69 * 1212
70 * 3434
71 *
72 * In this example both the size and layout of pages in the alternative view is
73 * different from the normal view.
74 *
75 * Implementation and usage
76 *
77 * GGTT views are implemented using VMAs and are distinguished via enum
78 * i915_ggtt_view_type and struct i915_ggtt_view.
79 *
80 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020081 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
82 * renaming in large amounts of code. They take the struct i915_ggtt_view
83 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000084 *
85 * As a helper for callers which are only interested in the normal view,
86 * globally const i915_ggtt_view_normal singleton instance exists. All old core
87 * GEM API functions, the ones not taking the view parameter, are operating on,
88 * or with the normal GGTT view.
89 *
90 * Code wanting to add or use a new GGTT view needs to:
91 *
92 * 1. Add a new enum with a suitable name.
93 * 2. Extend the metadata in the i915_ggtt_view structure if required.
94 * 3. Add support to i915_get_vma_pages().
95 *
96 * New views are required to build a scatter-gather table from within the
97 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
98 * exists for the lifetime of an VMA.
99 *
100 * Core API is designed to have copy semantics which means that passed in
101 * struct i915_ggtt_view does not need to be persistent (left around after
102 * calling the core API functions).
103 *
104 */
105
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200106static int
107i915_get_ggtt_vma_pages(struct i915_vma *vma);
108
Chris Wilson7c3f86b2017-01-12 11:00:49 +0000109static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
110{
111 /* Note that as an uncached mmio write, this should flush the
112 * WCB of the writes into the GGTT before it triggers the invalidate.
113 */
114 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
115}
116
117static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
118{
119 gen6_ggtt_invalidate(dev_priv);
120 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
121}
122
123static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
124{
125 intel_gtt_chipset_flush();
126}
127
128static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
129{
130 i915->ggtt.invalidate(i915);
131}
132
Chris Wilsonc0336662016-05-06 15:40:21 +0100133int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
134 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200135{
Chris Wilson1893a712014-09-19 11:56:27 +0100136 bool has_aliasing_ppgtt;
137 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100138 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100139
Michel Thierry9e1d0e62016-12-05 17:57:03 -0800140 has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
141 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
142 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100143
Zhi Wange320d402016-09-06 12:04:12 +0800144 if (intel_vgpu_active(dev_priv)) {
145 /* emulation is too hard */
146 has_full_ppgtt = false;
147 has_full_48bit_ppgtt = false;
148 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800149
Chris Wilson0e4ca102016-04-29 13:18:22 +0100150 if (!has_aliasing_ppgtt)
151 return 0;
152
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000153 /*
154 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
155 * execlists, the sole mechanism available to submit work.
156 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100157 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200158 return 0;
159
160 if (enable_ppgtt == 1)
161 return 1;
162
Chris Wilson1893a712014-09-19 11:56:27 +0100163 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200164 return 2;
165
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100166 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
167 return 3;
168
Daniel Vetter93a25a92014-03-06 09:40:43 +0100169#ifdef CONFIG_INTEL_IOMMU
170 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100171 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100172 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200173 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100174 }
175#endif
176
Jesse Barnes62942ed2014-06-13 09:28:33 -0700177 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100178 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700179 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
180 return 0;
181 }
182
Zhi Wange320d402016-09-06 12:04:12 +0800183 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100184 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000185 else
186 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100187}
188
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200189static int ppgtt_bind_vma(struct i915_vma *vma,
190 enum i915_cache_level cache_level,
191 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200192{
193 u32 pte_flags = 0;
194
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100195 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100196
Daniel Vetter47552652015-04-14 17:35:24 +0200197 /* Currently applicable only to VLV */
198 if (vma->obj->gt_ro)
199 pte_flags |= PTE_READ_ONLY;
200
Chris Wilson247177d2016-08-15 10:48:47 +0100201 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200202 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200203
204 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200205}
206
207static void ppgtt_unbind_vma(struct i915_vma *vma)
208{
209 vma->vm->clear_range(vma->vm,
210 vma->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200211 vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200212}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800213
Daniel Vetter2c642b02015-04-14 17:35:26 +0200214static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200215 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700216{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200217 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700218 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300219
220 switch (level) {
221 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800222 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300223 break;
224 case I915_CACHE_WT:
225 pte |= PPAT_DISPLAY_ELLC_INDEX;
226 break;
227 default:
228 pte |= PPAT_CACHED_INDEX;
229 break;
230 }
231
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700232 return pte;
233}
234
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300235static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
236 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800237{
Michel Thierry07749ef2015-03-16 16:00:54 +0000238 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800239 pde |= addr;
240 if (level != I915_CACHE_NONE)
241 pde |= PPAT_CACHED_PDE_INDEX;
242 else
243 pde |= PPAT_UNCACHED_INDEX;
244 return pde;
245}
246
Michel Thierry762d9932015-07-30 11:05:29 +0100247#define gen8_pdpe_encode gen8_pde_encode
248#define gen8_pml4e_encode gen8_pde_encode
249
Michel Thierry07749ef2015-03-16 16:00:54 +0000250static gen6_pte_t snb_pte_encode(dma_addr_t addr,
251 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200252 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700253{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200254 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700255 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700256
257 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100258 case I915_CACHE_L3_LLC:
259 case I915_CACHE_LLC:
260 pte |= GEN6_PTE_CACHE_LLC;
261 break;
262 case I915_CACHE_NONE:
263 pte |= GEN6_PTE_UNCACHED;
264 break;
265 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100266 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100267 }
268
269 return pte;
270}
271
Michel Thierry07749ef2015-03-16 16:00:54 +0000272static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
273 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200274 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100275{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200276 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100277 pte |= GEN6_PTE_ADDR_ENCODE(addr);
278
279 switch (level) {
280 case I915_CACHE_L3_LLC:
281 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700282 break;
283 case I915_CACHE_LLC:
284 pte |= GEN6_PTE_CACHE_LLC;
285 break;
286 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700287 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700288 break;
289 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100290 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700291 }
292
Ben Widawsky54d12522012-09-24 16:44:32 -0700293 return pte;
294}
295
Michel Thierry07749ef2015-03-16 16:00:54 +0000296static gen6_pte_t byt_pte_encode(dma_addr_t addr,
297 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200298 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700299{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200300 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700301 pte |= GEN6_PTE_ADDR_ENCODE(addr);
302
Akash Goel24f3a8c2014-06-17 10:59:42 +0530303 if (!(flags & PTE_READ_ONLY))
304 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700305
306 if (level != I915_CACHE_NONE)
307 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
308
309 return pte;
310}
311
Michel Thierry07749ef2015-03-16 16:00:54 +0000312static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
313 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200314 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700315{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200316 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700317 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700318
319 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700320 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700321
322 return pte;
323}
324
Michel Thierry07749ef2015-03-16 16:00:54 +0000325static gen6_pte_t iris_pte_encode(dma_addr_t addr,
326 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200327 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700328{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200329 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700330 pte |= HSW_PTE_ADDR_ENCODE(addr);
331
Chris Wilson651d7942013-08-08 14:41:10 +0100332 switch (level) {
333 case I915_CACHE_NONE:
334 break;
335 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000336 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100337 break;
338 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000339 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100340 break;
341 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700342
343 return pte;
344}
345
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000346static int __setup_page_dma(struct drm_i915_private *dev_priv,
Mika Kuoppalac114f762015-06-25 18:35:13 +0300347 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000348{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000349 struct device *kdev = &dev_priv->drm.pdev->dev;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000350
Chris Wilsonaae4a3d2017-02-13 17:15:44 +0000351 if (I915_SELFTEST_ONLY(should_fail(&dev_priv->vm_fault, 1)))
352 i915_gem_shrink_all(dev_priv);
353
Mika Kuoppalac114f762015-06-25 18:35:13 +0300354 p->page = alloc_page(flags);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300355 if (!p->page)
Michel Thierry1266cdb2015-03-24 17:06:33 +0000356 return -ENOMEM;
357
David Weinehallc49d13e2016-08-22 13:32:42 +0300358 p->daddr = dma_map_page(kdev,
Chris Wilsonf51455d2017-01-10 14:47:34 +0000359 p->page, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300360
David Weinehallc49d13e2016-08-22 13:32:42 +0300361 if (dma_mapping_error(kdev, p->daddr)) {
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300362 __free_page(p->page);
363 return -EINVAL;
364 }
365
Michel Thierry1266cdb2015-03-24 17:06:33 +0000366 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000367}
368
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000369static int setup_page_dma(struct drm_i915_private *dev_priv,
370 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300371{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000372 return __setup_page_dma(dev_priv, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300373}
374
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000375static void cleanup_page_dma(struct drm_i915_private *dev_priv,
376 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300377{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000378 struct pci_dev *pdev = dev_priv->drm.pdev;
David Weinehall52a05c32016-08-22 13:32:44 +0300379
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300380 if (WARN_ON(!p->page))
381 return;
382
Chris Wilsonf51455d2017-01-10 14:47:34 +0000383 dma_unmap_page(&pdev->dev, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300384 __free_page(p->page);
385 memset(p, 0, sizeof(*p));
386}
387
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300388static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300389{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300390 return kmap_atomic(p->page);
391}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300392
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300393/* We use the flushing unmap only with ppgtt structures:
394 * page directories, page tables and scratch pages.
395 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100396static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300397{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300398 /* There are only few exceptions for gen >=6. chv and bxt.
399 * And we are not sure about the latter so play safe for now.
400 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200401 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300402 drm_clflush_virt_range(vaddr, PAGE_SIZE);
403
404 kunmap_atomic(vaddr);
405}
406
Mika Kuoppala567047b2015-06-25 18:35:12 +0300407#define kmap_px(px) kmap_page_dma(px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100408#define kunmap_px(ppgtt, vaddr) \
Chris Wilson49d73912016-11-29 09:50:08 +0000409 kunmap_page_dma((ppgtt)->base.i915, (vaddr))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300410
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000411#define setup_px(dev_priv, px) setup_page_dma((dev_priv), px_base(px))
412#define cleanup_px(dev_priv, px) cleanup_page_dma((dev_priv), px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100413#define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
414#define fill32_px(dev_priv, px, v) \
415 fill_page_dma_32((dev_priv), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300416
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100417static void fill_page_dma(struct drm_i915_private *dev_priv,
418 struct i915_page_dma *p, const uint64_t val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300419{
420 int i;
421 uint64_t * const vaddr = kmap_page_dma(p);
422
423 for (i = 0; i < 512; i++)
424 vaddr[i] = val;
425
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100426 kunmap_page_dma(dev_priv, vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300427}
428
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100429static void fill_page_dma_32(struct drm_i915_private *dev_priv,
430 struct i915_page_dma *p, const uint32_t val32)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300431{
432 uint64_t v = val32;
433
434 v = v << 32 | val32;
435
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100436 fill_page_dma(dev_priv, p, v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300437}
438
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100439static int
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000440setup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100441 struct i915_page_dma *scratch,
442 gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300443{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000444 return __setup_page_dma(dev_priv, scratch, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300445}
446
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000447static void cleanup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100448 struct i915_page_dma *scratch)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300449{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000450 cleanup_page_dma(dev_priv, scratch);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300451}
452
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000453static struct i915_page_table *alloc_pt(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000454{
Michel Thierryec565b32015-04-08 12:13:23 +0100455 struct i915_page_table *pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000456 const size_t count = INTEL_GEN(dev_priv) >= 8 ? GEN8_PTES : GEN6_PTES;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000457 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000458
459 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
460 if (!pt)
461 return ERR_PTR(-ENOMEM);
462
Ben Widawsky678d96f2015-03-16 16:00:56 +0000463 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
464 GFP_KERNEL);
465
466 if (!pt->used_ptes)
467 goto fail_bitmap;
468
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000469 ret = setup_px(dev_priv, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000470 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300471 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000472
473 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000474
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300475fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000476 kfree(pt->used_ptes);
477fail_bitmap:
478 kfree(pt);
479
480 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000481}
482
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000483static void free_pt(struct drm_i915_private *dev_priv,
484 struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000485{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000486 cleanup_px(dev_priv, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300487 kfree(pt->used_ptes);
488 kfree(pt);
489}
490
491static void gen8_initialize_pt(struct i915_address_space *vm,
492 struct i915_page_table *pt)
493{
494 gen8_pte_t scratch_pte;
495
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100496 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200497 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300498
Chris Wilson49d73912016-11-29 09:50:08 +0000499 fill_px(vm->i915, pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300500}
501
502static void gen6_initialize_pt(struct i915_address_space *vm,
503 struct i915_page_table *pt)
504{
505 gen6_pte_t scratch_pte;
506
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100507 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300508
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100509 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200510 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300511
Chris Wilson49d73912016-11-29 09:50:08 +0000512 fill32_px(vm->i915, pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000513}
514
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000515static struct i915_page_directory *alloc_pd(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000516{
Michel Thierryec565b32015-04-08 12:13:23 +0100517 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100518 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000519
520 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
521 if (!pd)
522 return ERR_PTR(-ENOMEM);
523
Michel Thierry33c88192015-04-08 12:13:33 +0100524 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
525 sizeof(*pd->used_pdes), GFP_KERNEL);
526 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300527 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100528
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000529 ret = setup_px(dev_priv, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100530 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300531 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100532
Ben Widawsky06fda602015-02-24 16:22:36 +0000533 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100534
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300535fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100536 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300537fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100538 kfree(pd);
539
540 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000541}
542
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000543static void free_pd(struct drm_i915_private *dev_priv,
544 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300545{
546 if (px_page(pd)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000547 cleanup_px(dev_priv, pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300548 kfree(pd->used_pdes);
549 kfree(pd);
550 }
551}
552
553static void gen8_initialize_pd(struct i915_address_space *vm,
554 struct i915_page_directory *pd)
555{
556 gen8_pde_t scratch_pde;
557
558 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
559
Chris Wilson49d73912016-11-29 09:50:08 +0000560 fill_px(vm->i915, pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300561}
562
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000563static int __pdp_init(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100564 struct i915_page_directory_pointer *pdp)
565{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000566 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Michel Thierry6ac18502015-07-29 17:23:46 +0100567
568 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
569 sizeof(unsigned long),
570 GFP_KERNEL);
571 if (!pdp->used_pdpes)
572 return -ENOMEM;
573
574 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
575 GFP_KERNEL);
576 if (!pdp->page_directory) {
577 kfree(pdp->used_pdpes);
578 /* the PDP might be the statically allocated top level. Keep it
579 * as clean as possible */
580 pdp->used_pdpes = NULL;
581 return -ENOMEM;
582 }
583
584 return 0;
585}
586
587static void __pdp_fini(struct i915_page_directory_pointer *pdp)
588{
589 kfree(pdp->used_pdpes);
590 kfree(pdp->page_directory);
591 pdp->page_directory = NULL;
592}
593
Michel Thierry762d9932015-07-30 11:05:29 +0100594static struct
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000595i915_page_directory_pointer *alloc_pdp(struct drm_i915_private *dev_priv)
Michel Thierry762d9932015-07-30 11:05:29 +0100596{
597 struct i915_page_directory_pointer *pdp;
598 int ret = -ENOMEM;
599
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000600 WARN_ON(!USES_FULL_48BIT_PPGTT(dev_priv));
Michel Thierry762d9932015-07-30 11:05:29 +0100601
602 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
603 if (!pdp)
604 return ERR_PTR(-ENOMEM);
605
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000606 ret = __pdp_init(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100607 if (ret)
608 goto fail_bitmap;
609
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000610 ret = setup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100611 if (ret)
612 goto fail_page_m;
613
614 return pdp;
615
616fail_page_m:
617 __pdp_fini(pdp);
618fail_bitmap:
619 kfree(pdp);
620
621 return ERR_PTR(ret);
622}
623
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000624static void free_pdp(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100625 struct i915_page_directory_pointer *pdp)
626{
627 __pdp_fini(pdp);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000628 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
629 cleanup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100630 kfree(pdp);
631 }
632}
633
Michel Thierry69ab76f2015-07-29 17:23:55 +0100634static void gen8_initialize_pdp(struct i915_address_space *vm,
635 struct i915_page_directory_pointer *pdp)
636{
637 gen8_ppgtt_pdpe_t scratch_pdpe;
638
639 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
640
Chris Wilson49d73912016-11-29 09:50:08 +0000641 fill_px(vm->i915, pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100642}
643
644static void gen8_initialize_pml4(struct i915_address_space *vm,
645 struct i915_pml4 *pml4)
646{
647 gen8_ppgtt_pml4e_t scratch_pml4e;
648
649 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
650 I915_CACHE_LLC);
651
Chris Wilson49d73912016-11-29 09:50:08 +0000652 fill_px(vm->i915, pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100653}
654
Michel Thierry762d9932015-07-30 11:05:29 +0100655static void
Matthew Auld5c693b22016-12-13 16:05:10 +0000656gen8_setup_pdpe(struct i915_hw_ppgtt *ppgtt,
657 struct i915_page_directory_pointer *pdp,
658 struct i915_page_directory *pd,
659 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100660{
661 gen8_ppgtt_pdpe_t *page_directorypo;
662
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000663 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
Michel Thierry762d9932015-07-30 11:05:29 +0100664 return;
665
666 page_directorypo = kmap_px(pdp);
667 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
668 kunmap_px(ppgtt, page_directorypo);
669}
670
671static void
Matthew Auld56843102016-12-13 16:05:11 +0000672gen8_setup_pml4e(struct i915_hw_ppgtt *ppgtt,
673 struct i915_pml4 *pml4,
674 struct i915_page_directory_pointer *pdp,
675 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100676{
677 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
678
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000679 WARN_ON(!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)));
Michel Thierry762d9932015-07-30 11:05:29 +0100680 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
681 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100682}
683
Ben Widawsky94e409c2013-11-04 22:29:36 -0800684/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100685static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100686 unsigned entry,
687 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800688{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000689 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000690 u32 *cs;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800691
692 BUG_ON(entry >= 4);
693
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000694 cs = intel_ring_begin(req, 6);
695 if (IS_ERR(cs))
696 return PTR_ERR(cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800697
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000698 *cs++ = MI_LOAD_REGISTER_IMM(1);
699 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, entry));
700 *cs++ = upper_32_bits(addr);
701 *cs++ = MI_LOAD_REGISTER_IMM(1);
702 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, entry));
703 *cs++ = lower_32_bits(addr);
704 intel_ring_advance(req, cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800705
706 return 0;
707}
708
Michel Thierry2dba3232015-07-30 11:06:23 +0100709static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
710 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800711{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800712 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800713
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100714 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300715 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
716
John Harrisone85b26d2015-05-29 17:43:56 +0100717 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800718 if (ret)
719 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800720 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800721
Ben Widawskyeeb94882013-12-06 14:11:10 -0800722 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800723}
724
Michel Thierry2dba3232015-07-30 11:06:23 +0100725static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
726 struct drm_i915_gem_request *req)
727{
728 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
729}
730
Mika Kuoppalafce93752016-10-31 17:24:46 +0200731/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
732 * the page table structures, we mark them dirty so that
733 * context switching/execlist queuing code takes extra steps
734 * to ensure that tlbs are flushed.
735 */
736static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
737{
Chris Wilson49d73912016-11-29 09:50:08 +0000738 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200739}
740
Michał Winiarski2ce51792016-10-13 14:02:42 +0200741/* Removes entries from a single page table, releasing it if it's empty.
742 * Caller can use the return value to update higher-level entries.
743 */
744static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200745 struct i915_page_table *pt,
746 uint64_t start,
747 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700748{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300749 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200750 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200751 unsigned int pte = gen8_pte_index(start);
752 unsigned int pte_end = pte + num_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +0000753 const gen8_pte_t scratch_pte =
754 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
755 gen8_pte_t *vaddr;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200756
757 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200758 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700759
Mika Kuoppala37c63932016-11-01 15:27:36 +0200760 GEM_BUG_ON(pte_end > GEN8_PTES);
761
762 bitmap_clear(pt->used_ptes, pte, num_entries);
Zhi Wange81ecb52017-02-08 21:03:33 +0800763 if (USES_FULL_PPGTT(vm->i915)) {
764 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
765 return true;
766 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200767
Chris Wilson894cceb2017-02-15 08:43:37 +0000768 vaddr = kmap_px(pt);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200769 while (pte < pte_end)
Chris Wilson894cceb2017-02-15 08:43:37 +0000770 vaddr[pte++] = scratch_pte;
771 kunmap_px(ppgtt, vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200772
773 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200774}
775
Michał Winiarski2ce51792016-10-13 14:02:42 +0200776/* Removes entries from a single page dir, releasing it if it's empty.
777 * Caller can use the return value to update higher-level entries
778 */
779static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200780 struct i915_page_directory *pd,
781 uint64_t start,
782 uint64_t length)
783{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200784 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200785 struct i915_page_table *pt;
786 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200787 gen8_pde_t *pde_vaddr;
788 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
789 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200790
791 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000792 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100793 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000794
Michał Winiarski2ce51792016-10-13 14:02:42 +0200795 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
796 __clear_bit(pde, pd->used_pdes);
797 pde_vaddr = kmap_px(pd);
798 pde_vaddr[pde] = scratch_pde;
799 kunmap_px(ppgtt, pde_vaddr);
Chris Wilson49d73912016-11-29 09:50:08 +0000800 free_pt(vm->i915, pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200801 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200802 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200803
Zhi Wanga18dbba2016-11-29 14:55:16 +0800804 if (bitmap_empty(pd->used_pdes, I915_PDES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200805 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200806
807 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200808}
Ben Widawsky06fda602015-02-24 16:22:36 +0000809
Michał Winiarski2ce51792016-10-13 14:02:42 +0200810/* Removes entries from a single page dir pointer, releasing it if it's empty.
811 * Caller can use the return value to update higher-level entries
812 */
813static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200814 struct i915_page_directory_pointer *pdp,
815 uint64_t start,
816 uint64_t length)
817{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200818 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200819 struct i915_page_directory *pd;
820 uint64_t pdpe;
821
822 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
823 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100824 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000825
Michał Winiarski2ce51792016-10-13 14:02:42 +0200826 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
827 __clear_bit(pdpe, pdp->used_pdpes);
Matthew Auld9e65a372016-12-13 16:05:12 +0000828 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
Chris Wilson49d73912016-11-29 09:50:08 +0000829 free_pd(vm->i915, pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200830 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200831 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200832
Mika Kuoppalafce93752016-10-31 17:24:46 +0200833 mark_tlbs_dirty(ppgtt);
834
Zhi Wanga18dbba2016-11-29 14:55:16 +0800835 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200836 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200837
838 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200839}
Ben Widawsky459108b2013-11-02 21:07:23 -0700840
Michał Winiarski2ce51792016-10-13 14:02:42 +0200841/* Removes entries from a single pml4.
842 * This is the top-level structure in 4-level page tables used on gen8+.
843 * Empty entries are always scratch pml4e.
844 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200845static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
846 struct i915_pml4 *pml4,
847 uint64_t start,
848 uint64_t length)
849{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200850 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200851 struct i915_page_directory_pointer *pdp;
852 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200853
Chris Wilson49d73912016-11-29 09:50:08 +0000854 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700855
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200856 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
857 if (WARN_ON(!pml4->pdps[pml4e]))
858 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700859
Michał Winiarski2ce51792016-10-13 14:02:42 +0200860 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
861 __clear_bit(pml4e, pml4->used_pml4es);
Matthew Auld9e65a372016-12-13 16:05:12 +0000862 gen8_setup_pml4e(ppgtt, pml4, vm->scratch_pdp, pml4e);
Chris Wilson49d73912016-11-29 09:50:08 +0000863 free_pdp(vm->i915, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200864 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700865 }
866}
867
Michel Thierryf9b5b782015-07-30 11:02:49 +0100868static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200869 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700870{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300871 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100872
Chris Wilsonc6385c92016-11-29 12:42:05 +0000873 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200874 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
875 else
876 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100877}
878
Chris Wilson894cceb2017-02-15 08:43:37 +0000879struct sgt_dma {
880 struct scatterlist *sg;
881 dma_addr_t dma, max;
882};
883
884static __always_inline bool
885gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100886 struct i915_page_directory_pointer *pdp,
Chris Wilson894cceb2017-02-15 08:43:37 +0000887 struct sgt_dma *iter,
888 u64 start,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100889 enum i915_cache_level cache_level)
890{
Chris Wilson894cceb2017-02-15 08:43:37 +0000891 unsigned int pdpe = gen8_pdpe_index(start);
892 unsigned int pde = gen8_pde_index(start);
893 unsigned int pte = gen8_pte_index(start);
894 struct i915_page_directory *pd;
895 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
896 gen8_pte_t *vaddr;
897 bool ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700898
Chris Wilson894cceb2017-02-15 08:43:37 +0000899 pd = pdp->page_directory[pdpe];
900 vaddr = kmap_px(pd->page_table[pde]);
901 do {
902 vaddr[pte] = pte_encode | iter->dma;
903 iter->dma += PAGE_SIZE;
904 if (iter->dma >= iter->max) {
905 iter->sg = __sg_next(iter->sg);
906 if (!iter->sg) {
907 ret = false;
908 break;
909 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700910
Chris Wilson894cceb2017-02-15 08:43:37 +0000911 iter->dma = sg_dma_address(iter->sg);
912 iter->max = iter->dma + iter->sg->length;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000913 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800914
Michel Thierry07749ef2015-03-16 16:00:54 +0000915 if (++pte == GEN8_PTES) {
Michel Thierry07749ef2015-03-16 16:00:54 +0000916 if (++pde == I915_PDES) {
Chris Wilson894cceb2017-02-15 08:43:37 +0000917 /* Limited by sg length for 3lvl */
918 if (++pdpe == GEN8_PML4ES_PER_PML4) {
919 ret = true;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100920 break;
Chris Wilson894cceb2017-02-15 08:43:37 +0000921 }
922
923 GEM_BUG_ON(pdpe > GEN8_LEGACY_PDPES);
924 pd = pdp->page_directory[pdpe];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800925 pde = 0;
926 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000927
928 kunmap_px(ppgtt, vaddr);
929 vaddr = kmap_px(pd->page_table[pde]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800930 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700931 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000932 } while (1);
933 kunmap_px(ppgtt, vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300934
Chris Wilson894cceb2017-02-15 08:43:37 +0000935 return ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700936}
937
Chris Wilson894cceb2017-02-15 08:43:37 +0000938static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
939 struct sg_table *pages,
940 u64 start,
941 enum i915_cache_level cache_level,
942 u32 unused)
Michel Thierryf9b5b782015-07-30 11:02:49 +0100943{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300944 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilson894cceb2017-02-15 08:43:37 +0000945 struct sgt_dma iter = {
946 .sg = pages->sgl,
947 .dma = sg_dma_address(iter.sg),
948 .max = iter.dma + iter.sg->length,
949 };
Michel Thierryf9b5b782015-07-30 11:02:49 +0100950
Chris Wilson894cceb2017-02-15 08:43:37 +0000951 gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter,
952 start, cache_level);
953}
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100954
Chris Wilson894cceb2017-02-15 08:43:37 +0000955static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
956 struct sg_table *pages,
957 uint64_t start,
958 enum i915_cache_level cache_level,
959 u32 unused)
960{
961 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
962 struct sgt_dma iter = {
963 .sg = pages->sgl,
964 .dma = sg_dma_address(iter.sg),
965 .max = iter.dma + iter.sg->length,
966 };
967 struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
968 unsigned int pml4e = gen8_pml4e_index(start);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100969
Chris Wilson894cceb2017-02-15 08:43:37 +0000970 while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[pml4e++], &iter,
971 start, cache_level))
972 ;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100973}
974
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000975static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
Michel Thierryf37c0502015-06-10 17:46:39 +0100976 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800977{
978 int i;
979
Mika Kuoppala567047b2015-06-25 18:35:12 +0300980 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800981 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800982
Michel Thierry33c88192015-04-08 12:13:33 +0100983 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000984 if (WARN_ON(!pd->page_table[i]))
985 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800986
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000987 free_pt(dev_priv, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000988 pd->page_table[i] = NULL;
989 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000990}
991
Mika Kuoppala8776f022015-06-30 18:16:40 +0300992static int gen8_init_scratch(struct i915_address_space *vm)
993{
Chris Wilson49d73912016-11-29 09:50:08 +0000994 struct drm_i915_private *dev_priv = vm->i915;
Matthew Auld64c050d2016-04-27 13:19:25 +0100995 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300996
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000997 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100998 if (ret)
999 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001000
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001001 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001002 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +01001003 ret = PTR_ERR(vm->scratch_pt);
1004 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001005 }
1006
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001007 vm->scratch_pd = alloc_pd(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001008 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +01001009 ret = PTR_ERR(vm->scratch_pd);
1010 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001011 }
1012
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001013 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1014 vm->scratch_pdp = alloc_pdp(dev_priv);
Michel Thierry69ab76f2015-07-29 17:23:55 +01001015 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +01001016 ret = PTR_ERR(vm->scratch_pdp);
1017 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +01001018 }
1019 }
1020
Mika Kuoppala8776f022015-06-30 18:16:40 +03001021 gen8_initialize_pt(vm, vm->scratch_pt);
1022 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001023 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +01001024 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001025
1026 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +01001027
1028free_pd:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001029 free_pd(dev_priv, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +01001030free_pt:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001031 free_pt(dev_priv, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +01001032free_scratch_page:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001033 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +01001034
1035 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001036}
1037
Zhiyuan Lv650da342015-08-28 15:41:18 +08001038static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1039{
1040 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +00001041 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001042 int i;
1043
Matthew Aulddf285642016-04-22 12:09:25 +01001044 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001045 u64 daddr = px_dma(&ppgtt->pml4);
1046
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001047 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1048 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001049
1050 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1051 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1052 } else {
1053 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1054 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1055
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001056 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1057 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001058 }
1059
1060 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1061 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1062 }
1063
1064 I915_WRITE(vgtif_reg(g2v_notify), msg);
1065
1066 return 0;
1067}
1068
Mika Kuoppala8776f022015-06-30 18:16:40 +03001069static void gen8_free_scratch(struct i915_address_space *vm)
1070{
Chris Wilson49d73912016-11-29 09:50:08 +00001071 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001072
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001073 if (USES_FULL_48BIT_PPGTT(dev_priv))
1074 free_pdp(dev_priv, vm->scratch_pdp);
1075 free_pd(dev_priv, vm->scratch_pd);
1076 free_pt(dev_priv, vm->scratch_pt);
1077 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001078}
1079
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001080static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
Michel Thierry762d9932015-07-30 11:05:29 +01001081 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001082{
1083 int i;
1084
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001085 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001086 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001087 continue;
1088
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001089 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1090 free_pd(dev_priv, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001091 }
Michel Thierry69876be2015-04-08 12:13:27 +01001092
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001093 free_pdp(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001094}
1095
1096static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1097{
Chris Wilson49d73912016-11-29 09:50:08 +00001098 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001099 int i;
1100
1101 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1102 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1103 continue;
1104
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001105 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001106 }
1107
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001108 cleanup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001109}
1110
1111static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1112{
Chris Wilson49d73912016-11-29 09:50:08 +00001113 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001114 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001115
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001116 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001117 gen8_ppgtt_notify_vgt(ppgtt, false);
1118
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001119 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1120 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001121 else
1122 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001123
Mika Kuoppala8776f022015-06-30 18:16:40 +03001124 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001125}
1126
Michel Thierryd7b26332015-04-08 12:13:34 +01001127/**
1128 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001129 * @vm: Master vm structure.
1130 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001131 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001132 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001133 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1134 * caller to free on error.
1135 *
1136 * Allocate the required number of page tables. Extremely similar to
1137 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1138 * the page directory boundary (instead of the page directory pointer). That
1139 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1140 * possible, and likely that the caller will need to use multiple calls of this
1141 * function to achieve the appropriate allocation.
1142 *
1143 * Return: 0 if success; negative error code otherwise.
1144 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001145static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001146 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001147 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001148 uint64_t length,
1149 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001150{
Chris Wilson49d73912016-11-29 09:50:08 +00001151 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001152 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001153 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001154
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001155 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001156 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001157 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001158 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001159 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001160 continue;
1161 }
1162
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001163 pt = alloc_pt(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001164 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001165 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001166
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001167 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001168 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001169 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001170 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001171 }
1172
1173 return 0;
1174
1175unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001176 for_each_set_bit(pde, new_pts, I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001177 free_pt(dev_priv, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001178
1179 return -ENOMEM;
1180}
1181
Michel Thierryd7b26332015-04-08 12:13:34 +01001182/**
1183 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001184 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001185 * @pdp: Page directory pointer for this address range.
1186 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001187 * @length: Size of the allocations.
1188 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001189 * caller to free on error.
1190 *
1191 * Allocate the required number of page directories starting at the pde index of
1192 * @start, and ending at the pde index @start + @length. This function will skip
1193 * over already allocated page directories within the range, and only allocate
1194 * new ones, setting the appropriate pointer within the pdp as well as the
1195 * correct position in the bitmap @new_pds.
1196 *
1197 * The function will only allocate the pages within the range for a give page
1198 * directory pointer. In other words, if @start + @length straddles a virtually
1199 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1200 * required by the caller, This is not currently possible, and the BUG in the
1201 * code will prevent it.
1202 *
1203 * Return: 0 if success; negative error code otherwise.
1204 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001205static int
1206gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1207 struct i915_page_directory_pointer *pdp,
1208 uint64_t start,
1209 uint64_t length,
1210 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001211{
Chris Wilson49d73912016-11-29 09:50:08 +00001212 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001213 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001214 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001215 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001216
Michel Thierry6ac18502015-07-29 17:23:46 +01001217 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001218
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001219 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001220 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001221 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001222
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001223 pd = alloc_pd(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001224 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001225 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001226
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001227 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001228 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001229 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001230 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001231 }
1232
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001233 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001234
1235unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001236 for_each_set_bit(pdpe, new_pds, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001237 free_pd(dev_priv, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001238
1239 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001240}
1241
Michel Thierry762d9932015-07-30 11:05:29 +01001242/**
1243 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1244 * @vm: Master vm structure.
1245 * @pml4: Page map level 4 for this address range.
1246 * @start: Starting virtual address to begin allocations.
1247 * @length: Size of the allocations.
1248 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1249 * caller to free on error.
1250 *
1251 * Allocate the required number of page directory pointers. Extremely similar to
1252 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1253 * The main difference is here we are limited by the pml4 boundary (instead of
1254 * the page directory pointer).
1255 *
1256 * Return: 0 if success; negative error code otherwise.
1257 */
1258static int
1259gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1260 struct i915_pml4 *pml4,
1261 uint64_t start,
1262 uint64_t length,
1263 unsigned long *new_pdps)
1264{
Chris Wilson49d73912016-11-29 09:50:08 +00001265 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001266 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001267 uint32_t pml4e;
1268
1269 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1270
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001271 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001272 if (!test_bit(pml4e, pml4->used_pml4es)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001273 pdp = alloc_pdp(dev_priv);
Michel Thierry762d9932015-07-30 11:05:29 +01001274 if (IS_ERR(pdp))
1275 goto unwind_out;
1276
Michel Thierry69ab76f2015-07-29 17:23:55 +01001277 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001278 pml4->pdps[pml4e] = pdp;
1279 __set_bit(pml4e, new_pdps);
1280 trace_i915_page_directory_pointer_entry_alloc(vm,
1281 pml4e,
1282 start,
1283 GEN8_PML4E_SHIFT);
1284 }
1285 }
1286
1287 return 0;
1288
1289unwind_out:
1290 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001291 free_pdp(dev_priv, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001292
1293 return -ENOMEM;
1294}
1295
Michel Thierryd7b26332015-04-08 12:13:34 +01001296static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001297free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001298{
Michel Thierryd7b26332015-04-08 12:13:34 +01001299 kfree(new_pts);
1300 kfree(new_pds);
1301}
1302
1303/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1304 * of these are based on the number of PDPEs in the system.
1305 */
1306static
1307int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001308 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001309 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001310{
Michel Thierryd7b26332015-04-08 12:13:34 +01001311 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001312 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001313
Michał Winiarski3a41a052015-09-03 19:22:18 +02001314 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001315 if (!pds)
1316 return -ENOMEM;
1317
Michał Winiarski3a41a052015-09-03 19:22:18 +02001318 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1319 GFP_TEMPORARY);
1320 if (!pts)
1321 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001322
1323 *new_pds = pds;
1324 *new_pts = pts;
1325
1326 return 0;
1327
1328err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001329 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001330 return -ENOMEM;
1331}
1332
Michel Thierry762d9932015-07-30 11:05:29 +01001333static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1334 struct i915_page_directory_pointer *pdp,
1335 uint64_t start,
1336 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001337{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001338 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001339 unsigned long *new_page_dirs, *new_page_tables;
Chris Wilson49d73912016-11-29 09:50:08 +00001340 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001341 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001342 const uint64_t orig_start = start;
1343 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001344 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001345 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001346 int ret;
1347
Michel Thierry6ac18502015-07-29 17:23:46 +01001348 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001349 if (ret)
1350 return ret;
1351
Michel Thierryd7b26332015-04-08 12:13:34 +01001352 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001353 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1354 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001355 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001356 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001357 return ret;
1358 }
1359
1360 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001361 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001362 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001363 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001364 if (ret)
1365 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001366 }
1367
Michel Thierry33c88192015-04-08 12:13:33 +01001368 start = orig_start;
1369 length = orig_length;
1370
Michel Thierryd7b26332015-04-08 12:13:34 +01001371 /* Allocations have completed successfully, so set the bitmaps, and do
1372 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001373 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001374 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001375 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001376 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001377 uint64_t pd_start = start;
1378 uint32_t pde;
1379
Michel Thierryd7b26332015-04-08 12:13:34 +01001380 /* Every pd should be allocated, we just did that above. */
1381 WARN_ON(!pd);
1382
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001383 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001384 /* Same reasoning as pd */
1385 WARN_ON(!pt);
1386 WARN_ON(!pd_len);
1387 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1388
1389 /* Set our used ptes within the page table */
1390 bitmap_set(pt->used_ptes,
1391 gen8_pte_index(pd_start),
1392 gen8_pte_count(pd_start, pd_len));
1393
1394 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001395 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001396
1397 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001398 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1399 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001400 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1401 gen8_pte_index(start),
1402 gen8_pte_count(start, length),
1403 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001404
1405 /* NB: We haven't yet mapped ptes to pages. At this
1406 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001407 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001408
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001409 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001410 __set_bit(pdpe, pdp->used_pdpes);
Matthew Auld5c693b22016-12-13 16:05:10 +00001411 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001412 }
1413
Michał Winiarski3a41a052015-09-03 19:22:18 +02001414 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001415 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001416 return 0;
1417
1418err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001419 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001420 unsigned long temp;
1421
Michał Winiarski3a41a052015-09-03 19:22:18 +02001422 for_each_set_bit(temp, new_page_tables + pdpe *
1423 BITS_TO_LONGS(I915_PDES), I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001424 free_pt(dev_priv,
1425 pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001426 }
1427
Michel Thierry6ac18502015-07-29 17:23:46 +01001428 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001429 free_pd(dev_priv, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001430
Michał Winiarski3a41a052015-09-03 19:22:18 +02001431 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001432 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001433 return ret;
1434}
1435
Michel Thierry762d9932015-07-30 11:05:29 +01001436static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1437 struct i915_pml4 *pml4,
1438 uint64_t start,
1439 uint64_t length)
1440{
1441 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001442 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001443 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001444 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001445 int ret = 0;
1446
1447 /* Do the pml4 allocations first, so we don't need to track the newly
1448 * allocated tables below the pdp */
1449 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1450
1451 /* The pagedirectory and pagetable allocations are done in the shared 3
1452 * and 4 level code. Just allocate the pdps.
1453 */
1454 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1455 new_pdps);
1456 if (ret)
1457 return ret;
1458
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001459 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001460 WARN_ON(!pdp);
1461
1462 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1463 if (ret)
1464 goto err_out;
1465
Matthew Auld56843102016-12-13 16:05:11 +00001466 gen8_setup_pml4e(ppgtt, pml4, pdp, pml4e);
Michel Thierry762d9932015-07-30 11:05:29 +01001467 }
1468
1469 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1470 GEN8_PML4ES_PER_PML4);
1471
1472 return 0;
1473
1474err_out:
1475 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson49d73912016-11-29 09:50:08 +00001476 gen8_ppgtt_cleanup_3lvl(vm->i915, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001477
1478 return ret;
1479}
1480
1481static int gen8_alloc_va_range(struct i915_address_space *vm,
1482 uint64_t start, uint64_t length)
1483{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001484 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001485
Chris Wilsonc6385c92016-11-29 12:42:05 +00001486 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001487 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1488 else
1489 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1490}
1491
Michel Thierryea91e402015-07-29 17:23:57 +01001492static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1493 uint64_t start, uint64_t length,
1494 gen8_pte_t scratch_pte,
1495 struct seq_file *m)
1496{
1497 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001498 uint32_t pdpe;
1499
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001500 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001501 struct i915_page_table *pt;
1502 uint64_t pd_len = length;
1503 uint64_t pd_start = start;
1504 uint32_t pde;
1505
1506 if (!test_bit(pdpe, pdp->used_pdpes))
1507 continue;
1508
1509 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001510 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001511 uint32_t pte;
1512 gen8_pte_t *pt_vaddr;
1513
1514 if (!test_bit(pde, pd->used_pdes))
1515 continue;
1516
1517 pt_vaddr = kmap_px(pt);
1518 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1519 uint64_t va =
1520 (pdpe << GEN8_PDPE_SHIFT) |
1521 (pde << GEN8_PDE_SHIFT) |
1522 (pte << GEN8_PTE_SHIFT);
1523 int i;
1524 bool found = false;
1525
1526 for (i = 0; i < 4; i++)
1527 if (pt_vaddr[pte + i] != scratch_pte)
1528 found = true;
1529 if (!found)
1530 continue;
1531
1532 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1533 for (i = 0; i < 4; i++) {
1534 if (pt_vaddr[pte + i] != scratch_pte)
1535 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1536 else
1537 seq_puts(m, " SCRATCH ");
1538 }
1539 seq_puts(m, "\n");
1540 }
1541 /* don't use kunmap_px, it could trigger
1542 * an unnecessary flush.
1543 */
1544 kunmap_atomic(pt_vaddr);
1545 }
1546 }
1547}
1548
1549static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1550{
1551 struct i915_address_space *vm = &ppgtt->base;
1552 uint64_t start = ppgtt->base.start;
1553 uint64_t length = ppgtt->base.total;
Chris Wilson894cceb2017-02-15 08:43:37 +00001554 const gen8_pte_t scratch_pte =
1555 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001556
Chris Wilsonc6385c92016-11-29 12:42:05 +00001557 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryea91e402015-07-29 17:23:57 +01001558 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1559 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001560 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001561 struct i915_pml4 *pml4 = &ppgtt->pml4;
1562 struct i915_page_directory_pointer *pdp;
1563
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001564 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001565 if (!test_bit(pml4e, pml4->used_pml4es))
1566 continue;
1567
1568 seq_printf(m, " PML4E #%llu\n", pml4e);
1569 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1570 }
1571 }
1572}
1573
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001574static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1575{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001576 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001577 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001578 int ret;
1579
1580 /* We allocate temp bitmap for page tables for no gain
1581 * but as this is for init only, lets keep the things simple
1582 */
1583 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1584 if (ret)
1585 return ret;
1586
1587 /* Allocate for all pdps regardless of how the ppgtt
1588 * was defined.
1589 */
1590 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1591 0, 1ULL << 32,
1592 new_page_dirs);
1593 if (!ret)
1594 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1595
Michał Winiarski3a41a052015-09-03 19:22:18 +02001596 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001597
1598 return ret;
1599}
1600
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001601/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001602 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1603 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1604 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1605 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001606 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001607 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001608static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001609{
Chris Wilson49d73912016-11-29 09:50:08 +00001610 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001611 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001612
Mika Kuoppala8776f022015-06-30 18:16:40 +03001613 ret = gen8_init_scratch(&ppgtt->base);
1614 if (ret)
1615 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001616
Michel Thierryd7b26332015-04-08 12:13:34 +01001617 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001618 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001619 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001620 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001621 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1622 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001623 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001624
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001625 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1626 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001627 if (ret)
1628 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001629
Michel Thierry69ab76f2015-07-29 17:23:55 +01001630 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1631
Michel Thierry762d9932015-07-30 11:05:29 +01001632 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001633 ppgtt->switch_mm = gen8_48b_mm_switch;
Chris Wilson894cceb2017-02-15 08:43:37 +00001634
1635 ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
Michel Thierry762d9932015-07-30 11:05:29 +01001636 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001637 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001638 if (ret)
1639 goto free_scratch;
1640
1641 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001642 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001643 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1644 0, 0,
1645 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001646
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001647 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001648 ret = gen8_preallocate_top_level_pdps(ppgtt);
1649 if (ret)
1650 goto free_scratch;
1651 }
Chris Wilson894cceb2017-02-15 08:43:37 +00001652
1653 ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001654 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001655
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001656 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001657 gen8_ppgtt_notify_vgt(ppgtt, true);
1658
Michel Thierryd7b26332015-04-08 12:13:34 +01001659 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001660
1661free_scratch:
1662 gen8_free_scratch(&ppgtt->base);
1663 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001664}
1665
Ben Widawsky87d60b62013-12-06 14:11:29 -08001666static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1667{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001668 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001669 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001670 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001671 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001672 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001673 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001674
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001675 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001676 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001677
Dave Gordon731f74c2016-06-24 19:37:46 +01001678 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001679 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001680 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001681 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001682 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001683 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1684
1685 if (pd_entry != expected)
1686 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1687 pde,
1688 pd_entry,
1689 expected);
1690 seq_printf(m, "\tPDE: %x\n", pd_entry);
1691
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001692 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1693
Michel Thierry07749ef2015-03-16 16:00:54 +00001694 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001695 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001696 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001697 (pte * PAGE_SIZE);
1698 int i;
1699 bool found = false;
1700 for (i = 0; i < 4; i++)
1701 if (pt_vaddr[pte + i] != scratch_pte)
1702 found = true;
1703 if (!found)
1704 continue;
1705
1706 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1707 for (i = 0; i < 4; i++) {
1708 if (pt_vaddr[pte + i] != scratch_pte)
1709 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1710 else
1711 seq_puts(m, " SCRATCH ");
1712 }
1713 seq_puts(m, "\n");
1714 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001715 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001716 }
1717}
1718
Ben Widawsky678d96f2015-03-16 16:00:56 +00001719/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001720static void gen6_write_pde(struct i915_page_directory *pd,
1721 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001722{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001723 /* Caller needs to make sure the write completes if necessary */
1724 struct i915_hw_ppgtt *ppgtt =
1725 container_of(pd, struct i915_hw_ppgtt, pd);
1726 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001727
Mika Kuoppala567047b2015-06-25 18:35:12 +03001728 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001729 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001730
Ben Widawsky678d96f2015-03-16 16:00:56 +00001731 writel(pd_entry, ppgtt->pd_addr + pde);
1732}
Ben Widawsky61973492013-04-08 18:43:54 -07001733
Ben Widawsky678d96f2015-03-16 16:00:56 +00001734/* Write all the page tables found in the ppgtt structure to incrementing page
1735 * directories. */
1736static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001737 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001738 uint32_t start, uint32_t length)
1739{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001740 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001741 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001742 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001743
Dave Gordon731f74c2016-06-24 19:37:46 +01001744 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001745 gen6_write_pde(pd, pde, pt);
1746
1747 /* Make sure write is complete before other code can use this page
1748 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001749 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001750}
1751
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001752static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001753{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001754 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001755
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001756 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001757}
Ben Widawsky61973492013-04-08 18:43:54 -07001758
Ben Widawsky90252e52013-12-06 14:11:12 -08001759static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001760 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001761{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001762 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001763 u32 *cs;
Ben Widawsky90252e52013-12-06 14:11:12 -08001764 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001765
Ben Widawsky90252e52013-12-06 14:11:12 -08001766 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001767 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001768 if (ret)
1769 return ret;
1770
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001771 cs = intel_ring_begin(req, 6);
1772 if (IS_ERR(cs))
1773 return PTR_ERR(cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001774
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001775 *cs++ = MI_LOAD_REGISTER_IMM(2);
1776 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1777 *cs++ = PP_DIR_DCLV_2G;
1778 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1779 *cs++ = get_pd_offset(ppgtt);
1780 *cs++ = MI_NOOP;
1781 intel_ring_advance(req, cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001782
1783 return 0;
1784}
1785
Ben Widawsky48a10382013-12-06 14:11:11 -08001786static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001787 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001788{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001789 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001790 u32 *cs;
Ben Widawsky48a10382013-12-06 14:11:11 -08001791 int ret;
1792
Ben Widawsky48a10382013-12-06 14:11:11 -08001793 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001794 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001795 if (ret)
1796 return ret;
1797
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001798 cs = intel_ring_begin(req, 6);
1799 if (IS_ERR(cs))
1800 return PTR_ERR(cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001801
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001802 *cs++ = MI_LOAD_REGISTER_IMM(2);
1803 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1804 *cs++ = PP_DIR_DCLV_2G;
1805 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1806 *cs++ = get_pd_offset(ppgtt);
1807 *cs++ = MI_NOOP;
1808 intel_ring_advance(req, cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001809
Ben Widawsky90252e52013-12-06 14:11:12 -08001810 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001811 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001812 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001813 if (ret)
1814 return ret;
1815 }
1816
Ben Widawsky48a10382013-12-06 14:11:11 -08001817 return 0;
1818}
1819
Ben Widawskyeeb94882013-12-06 14:11:10 -08001820static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001821 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001822{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001823 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001824 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001825
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001826 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1827 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001828 return 0;
1829}
1830
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001831static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001832{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001833 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301834 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001835
Akash Goel3b3f1652016-10-13 22:44:48 +05301836 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001837 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1838 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001839 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001840 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001841 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001842}
1843
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001844static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001845{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001846 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001847 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301848 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001849
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001850 ecobits = I915_READ(GAC_ECO_BITS);
1851 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1852
1853 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001854 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001855 ecochk |= ECOCHK_PPGTT_WB_HSW;
1856 } else {
1857 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1858 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1859 }
1860 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001861
Akash Goel3b3f1652016-10-13 22:44:48 +05301862 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001863 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001864 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001865 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001866 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001867}
1868
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001869static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001870{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001871 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001872
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001873 ecobits = I915_READ(GAC_ECO_BITS);
1874 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1875 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001876
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001877 gab_ctl = I915_READ(GAB_CTL);
1878 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001879
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001880 ecochk = I915_READ(GAM_ECOCHK);
1881 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001882
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001883 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001884}
1885
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001886/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001887static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001888 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001889 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001890{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001891 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001892 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001893 unsigned first_entry = start >> PAGE_SHIFT;
1894 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001895 unsigned act_pt = first_entry / GEN6_PTES;
1896 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001897 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001898
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001899 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001900 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001901
Daniel Vetter7bddb012012-02-09 17:15:47 +01001902 while (num_entries) {
1903 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001904 if (last_pte > GEN6_PTES)
1905 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001906
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001907 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001908
1909 for (i = first_pte; i < last_pte; i++)
1910 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001911
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001912 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001913
Daniel Vetter7bddb012012-02-09 17:15:47 +01001914 num_entries -= last_pte - first_pte;
1915 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001916 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001917 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001918}
1919
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001920static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001921 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001922 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301923 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001924{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001925 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001926 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001927 unsigned act_pt = first_entry / GEN6_PTES;
1928 unsigned act_pte = first_entry % GEN6_PTES;
Chris Wilsonb31144c2017-02-15 08:43:36 +00001929 const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1930 struct sgt_dma iter;
1931 gen6_pte_t *vaddr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001932
Chris Wilsonb31144c2017-02-15 08:43:36 +00001933 vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1934 iter.sg = pages->sgl;
1935 iter.dma = sg_dma_address(iter.sg);
1936 iter.max = iter.dma + iter.sg->length;
1937 do {
1938 vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001939
Chris Wilsonb31144c2017-02-15 08:43:36 +00001940 iter.dma += PAGE_SIZE;
1941 if (iter.dma == iter.max) {
1942 iter.sg = __sg_next(iter.sg);
1943 if (!iter.sg)
1944 break;
1945
1946 iter.dma = sg_dma_address(iter.sg);
1947 iter.max = iter.dma + iter.sg->length;
1948 }
Akash Goel24f3a8c2014-06-17 10:59:42 +05301949
Michel Thierry07749ef2015-03-16 16:00:54 +00001950 if (++act_pte == GEN6_PTES) {
Chris Wilsonb31144c2017-02-15 08:43:36 +00001951 kunmap_px(ppgtt, vaddr);
1952 vaddr = kmap_px(ppgtt->pd.page_table[++act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +02001953 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001954 }
Chris Wilsonb31144c2017-02-15 08:43:36 +00001955 } while (1);
1956 kunmap_px(ppgtt, vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001957}
1958
Ben Widawsky678d96f2015-03-16 16:00:56 +00001959static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001960 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001961{
Michel Thierry4933d512015-03-24 15:46:22 +00001962 DECLARE_BITMAP(new_page_tables, I915_PDES);
Chris Wilson49d73912016-11-29 09:50:08 +00001963 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001964 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001965 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001966 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001967 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001968 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001969 int ret;
1970
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001971 start = start_save = start_in;
1972 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001973
1974 bitmap_zero(new_page_tables, I915_PDES);
1975
1976 /* The allocation is done in two stages so that we can bail out with
1977 * minimal amount of pain. The first stage finds new page tables that
1978 * need allocation. The second stage marks use ptes within the page
1979 * tables.
1980 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001981 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001982 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001983 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1984 continue;
1985 }
1986
1987 /* We've already allocated a page table */
1988 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1989
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001990 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001991 if (IS_ERR(pt)) {
1992 ret = PTR_ERR(pt);
1993 goto unwind_out;
1994 }
1995
1996 gen6_initialize_pt(vm, pt);
1997
1998 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001999 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00002000 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00002001 }
2002
2003 start = start_save;
2004 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002005
Dave Gordon731f74c2016-06-24 19:37:46 +01002006 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00002007 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
2008
2009 bitmap_zero(tmp_bitmap, GEN6_PTES);
2010 bitmap_set(tmp_bitmap, gen6_pte_index(start),
2011 gen6_pte_count(start, length));
2012
Mika Kuoppala966082c2015-06-25 18:35:19 +03002013 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00002014 gen6_write_pde(&ppgtt->pd, pde, pt);
2015
Michel Thierry72744cb2015-03-24 15:46:23 +00002016 trace_i915_page_table_entry_map(vm, pde, pt,
2017 gen6_pte_index(start),
2018 gen6_pte_count(start, length),
2019 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00002020 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00002021 GEN6_PTES);
2022 }
2023
Michel Thierry4933d512015-03-24 15:46:22 +00002024 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
2025
2026 /* Make sure write is complete before other code can use this page
2027 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002028 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00002029
Ben Widawsky563222a2015-03-19 12:53:28 +00002030 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002031 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00002032
2033unwind_out:
2034 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002035 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002036
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002037 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002038 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002039 }
2040
2041 mark_tlbs_dirty(ppgtt);
2042 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002043}
2044
Mika Kuoppala8776f022015-06-30 18:16:40 +03002045static int gen6_init_scratch(struct i915_address_space *vm)
2046{
Chris Wilson49d73912016-11-29 09:50:08 +00002047 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002048 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002049
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002050 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002051 if (ret)
2052 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002053
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002054 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002055 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002056 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002057 return PTR_ERR(vm->scratch_pt);
2058 }
2059
2060 gen6_initialize_pt(vm, vm->scratch_pt);
2061
2062 return 0;
2063}
2064
2065static void gen6_free_scratch(struct i915_address_space *vm)
2066{
Chris Wilson49d73912016-11-29 09:50:08 +00002067 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002068
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002069 free_pt(dev_priv, vm->scratch_pt);
2070 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002071}
2072
Daniel Vetter061dd492015-04-14 17:35:13 +02002073static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002074{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002075 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002076 struct i915_page_directory *pd = &ppgtt->pd;
Chris Wilson49d73912016-11-29 09:50:08 +00002077 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry09942c62015-04-08 12:13:30 +01002078 struct i915_page_table *pt;
2079 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002080
Daniel Vetter061dd492015-04-14 17:35:13 +02002081 drm_mm_remove_node(&ppgtt->node);
2082
Dave Gordon731f74c2016-06-24 19:37:46 +01002083 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002084 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002085 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002086
Mika Kuoppala8776f022015-06-30 18:16:40 +03002087 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002088}
2089
Ben Widawskyb1465202014-02-19 22:05:49 -08002090static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002091{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002092 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00002093 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002094 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002095 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002096
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002097 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2098 * allocator works in address space sizes, so it's multiplied by page
2099 * size. We allocate at the top of the GTT to avoid fragmentation.
2100 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002101 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002102
Mika Kuoppala8776f022015-06-30 18:16:40 +03002103 ret = gen6_init_scratch(vm);
2104 if (ret)
2105 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002106
Chris Wilsone007b192017-01-11 11:23:10 +00002107 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2108 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2109 I915_COLOR_UNEVICTABLE,
2110 0, ggtt->base.total,
2111 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00002112 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002113 goto err_out;
2114
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002115 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002116 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002117
Ben Widawskyc8c26622015-01-22 17:01:25 +00002118 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002119
2120err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002121 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002122 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002123}
2124
Ben Widawskyb1465202014-02-19 22:05:49 -08002125static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2126{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002127 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002128}
2129
Michel Thierry4933d512015-03-24 15:46:22 +00002130static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2131 uint64_t start, uint64_t length)
2132{
Michel Thierryec565b32015-04-08 12:13:23 +01002133 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002134 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002135
Dave Gordon731f74c2016-06-24 19:37:46 +01002136 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002137 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002138}
2139
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002140static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002141{
Chris Wilson49d73912016-11-29 09:50:08 +00002142 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002143 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002144 int ret;
2145
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002146 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002147 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002148 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002149 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002150 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002151 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002152 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002153 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002154 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002155
2156 ret = gen6_ppgtt_alloc(ppgtt);
2157 if (ret)
2158 return ret;
2159
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002160 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002161 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2162 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002163 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2164 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002165 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002166 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002167 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002168 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002169
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002170 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002171 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002172
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002173 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002174 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002175
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002176 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002177
Ben Widawsky678d96f2015-03-16 16:00:56 +00002178 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2179
Thierry Reding440fd522015-01-23 09:05:06 +01002180 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002181 ppgtt->node.size >> 20,
2182 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002183
Daniel Vetterfa76da32014-08-06 20:19:54 +02002184 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002185 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002186
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002187 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002188}
2189
Chris Wilson2bfa9962016-08-04 07:52:25 +01002190static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2191 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002192{
Chris Wilson49d73912016-11-29 09:50:08 +00002193 ppgtt->base.i915 = dev_priv;
Daniel Vetter3440d262013-01-24 13:49:56 -08002194
Chris Wilson2bfa9962016-08-04 07:52:25 +01002195 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002196 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002197 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002198 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002199}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002200
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002201static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002202 struct drm_i915_private *dev_priv,
2203 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002204{
Chris Wilson80b204b2016-10-28 13:58:58 +01002205 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00002206
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002207 drm_mm_init(&vm->mm, vm->start, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00002208 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
2209
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002210 INIT_LIST_HEAD(&vm->active_list);
2211 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002212 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00002213
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002214 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2215}
2216
Matthew Aulded9724d2016-11-17 21:04:10 +00002217static void i915_address_space_fini(struct i915_address_space *vm)
2218{
2219 i915_gem_timeline_fini(&vm->timeline);
2220 drm_mm_takedown(&vm->mm);
2221 list_del(&vm->global_link);
2222}
2223
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002224static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002225{
Tim Gored5165eb2016-02-04 11:49:34 +00002226 /* This function is for gtt related workarounds. This function is
2227 * called on driver load and after a GPU reset, so you can place
2228 * workarounds here even if they get overwritten by GPU reset.
2229 */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002230 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002231 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002232 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002233 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002234 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08002235 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002236 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002237 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002238 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2239}
2240
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002241int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002242{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002243 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002244
Thomas Daniel671b50132014-08-20 16:24:50 +01002245 /* In the case of execlists, PPGTT is enabled by the context descriptor
2246 * and the PDPs are contained within the context itself. We don't
2247 * need to do anything here. */
2248 if (i915.enable_execlists)
2249 return 0;
2250
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002251 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002252 return 0;
2253
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002254 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002255 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002256 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002257 gen7_ppgtt_enable(dev_priv);
2258 else if (INTEL_GEN(dev_priv) >= 8)
2259 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002260 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002261 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002262
John Harrison4ad2fd82015-06-18 13:11:20 +01002263 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002264}
John Harrison4ad2fd82015-06-18 13:11:20 +01002265
Daniel Vetter4d884702014-08-06 15:04:47 +02002266struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002267i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002268 struct drm_i915_file_private *fpriv,
2269 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002270{
2271 struct i915_hw_ppgtt *ppgtt;
2272 int ret;
2273
2274 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2275 if (!ppgtt)
2276 return ERR_PTR(-ENOMEM);
2277
Chris Wilson1188bc62017-02-15 08:43:38 +00002278 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetter4d884702014-08-06 15:04:47 +02002279 if (ret) {
2280 kfree(ppgtt);
2281 return ERR_PTR(ret);
2282 }
2283
Chris Wilson1188bc62017-02-15 08:43:38 +00002284 kref_init(&ppgtt->ref);
2285 i915_address_space_init(&ppgtt->base, dev_priv, name);
2286 ppgtt->base.file = fpriv;
2287
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002288 trace_i915_ppgtt_create(&ppgtt->base);
2289
Daniel Vetter4d884702014-08-06 15:04:47 +02002290 return ppgtt;
2291}
2292
Chris Wilson0c7eeda2017-01-11 21:09:25 +00002293void i915_ppgtt_close(struct i915_address_space *vm)
2294{
2295 struct list_head *phases[] = {
2296 &vm->active_list,
2297 &vm->inactive_list,
2298 &vm->unbound_list,
2299 NULL,
2300 }, **phase;
2301
2302 GEM_BUG_ON(vm->closed);
2303 vm->closed = true;
2304
2305 for (phase = phases; *phase; phase++) {
2306 struct i915_vma *vma, *vn;
2307
2308 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2309 if (!i915_vma_is_closed(vma))
2310 i915_vma_close(vma);
2311 }
2312}
2313
Matthew Aulded9724d2016-11-17 21:04:10 +00002314void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002315{
2316 struct i915_hw_ppgtt *ppgtt =
2317 container_of(kref, struct i915_hw_ppgtt, ref);
2318
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002319 trace_i915_ppgtt_release(&ppgtt->base);
2320
Chris Wilson50e046b2016-08-04 07:52:46 +01002321 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002322 WARN_ON(!list_empty(&ppgtt->base.active_list));
2323 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002324 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002325
Matthew Aulded9724d2016-11-17 21:04:10 +00002326 i915_address_space_fini(&ppgtt->base);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002327
Daniel Vetteree960be2014-08-06 15:04:45 +02002328 ppgtt->base.cleanup(&ppgtt->base);
2329 kfree(ppgtt);
2330}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002331
Ben Widawskya81cc002013-01-18 12:30:31 -08002332/* Certain Gen5 chipsets require require idling the GPU before
2333 * unmapping anything from the GTT when VT-d is enabled.
2334 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002335static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002336{
2337#ifdef CONFIG_INTEL_IOMMU
2338 /* Query intel_iommu to see if we need the workaround. Presumably that
2339 * was loaded first.
2340 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002341 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002342 return true;
2343#endif
2344 return false;
2345}
2346
Chris Wilsondc979972016-05-10 14:10:04 +01002347void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002348{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002349 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302350 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002351
Chris Wilsondc979972016-05-10 14:10:04 +01002352 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002353 return;
2354
Akash Goel3b3f1652016-10-13 22:44:48 +05302355 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002356 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002357 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002358 if (fault_reg & RING_FAULT_VALID) {
2359 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002360 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002361 "\tAddress space: %s\n"
2362 "\tSource ID: %d\n"
2363 "\tType: %d\n",
2364 fault_reg & PAGE_MASK,
2365 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2366 RING_FAULT_SRCID(fault_reg),
2367 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002368 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002369 fault_reg & ~RING_FAULT_VALID);
2370 }
2371 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302372
2373 /* Engine specific init may not have been done till this point. */
2374 if (dev_priv->engine[RCS])
2375 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002376}
2377
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002378void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002379{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002380 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002381
2382 /* Don't bother messing with faults pre GEN6 as we have little
2383 * documentation supporting that it's a good idea.
2384 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002385 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002386 return;
2387
Chris Wilsondc979972016-05-10 14:10:04 +01002388 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002389
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002390 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002391
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002392 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002393}
2394
Chris Wilson03ac84f2016-10-28 13:58:36 +01002395int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2396 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002397{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002398 do {
2399 if (dma_map_sg(&obj->base.dev->pdev->dev,
2400 pages->sgl, pages->nents,
2401 PCI_DMA_BIDIRECTIONAL))
2402 return 0;
2403
2404 /* If the DMA remap fails, one cause can be that we have
2405 * too many objects pinned in a small remapping table,
2406 * such as swiotlb. Incrementally purge all other objects and
2407 * try again - if there are no more pages to remove from
2408 * the DMA remapper, i915_gem_shrink will return 0.
2409 */
2410 GEM_BUG_ON(obj->mm.pages == pages);
2411 } while (i915_gem_shrink(to_i915(obj->base.dev),
2412 obj->base.size >> PAGE_SHIFT,
2413 I915_SHRINK_BOUND |
2414 I915_SHRINK_UNBOUND |
2415 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002416
Chris Wilson03ac84f2016-10-28 13:58:36 +01002417 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002418}
2419
Daniel Vetter2c642b02015-04-14 17:35:26 +02002420static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002421{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002422 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002423}
2424
Chris Wilsond6473f52016-06-10 14:22:59 +05302425static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2426 dma_addr_t addr,
2427 uint64_t offset,
2428 enum i915_cache_level level,
2429 u32 unused)
2430{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002431 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302432 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002433 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302434
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002435 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302436
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002437 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302438}
2439
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002440static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2441 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002442 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302443 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002444{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002445 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002446 struct sgt_iter sgt_iter;
2447 gen8_pte_t __iomem *gtt_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +00002448 const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002449 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002450
Chris Wilson894cceb2017-02-15 08:43:37 +00002451 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
2452 gtt_entries += start >> PAGE_SHIFT;
2453 for_each_sgt_dma(addr, sgt_iter, st)
2454 gen8_set_pte(gtt_entries++, pte_encode | addr);
Dave Gordon85d12252016-05-20 11:54:06 +01002455
Chris Wilson894cceb2017-02-15 08:43:37 +00002456 wmb();
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002457
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002458 /* This next bit makes the above posting read even more important. We
2459 * want to flush the TLBs only after we're certain all the PTE updates
2460 * have finished.
2461 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002462 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002463}
2464
Chris Wilsonc1403302015-11-18 15:19:39 +00002465struct insert_entries {
2466 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
2473static int gen8_ggtt_insert_entries__cb(void *_arg)
2474{
2475 struct insert_entries *arg = _arg;
2476 gen8_ggtt_insert_entries(arg->vm, arg->st,
2477 arg->start, arg->level, arg->flags);
2478 return 0;
2479}
2480
2481static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2482 struct sg_table *st,
2483 uint64_t start,
2484 enum i915_cache_level level,
2485 u32 flags)
2486{
2487 struct insert_entries arg = { vm, st, start, level, flags };
2488 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2489}
2490
Chris Wilsond6473f52016-06-10 14:22:59 +05302491static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2492 dma_addr_t addr,
2493 uint64_t offset,
2494 enum i915_cache_level level,
2495 u32 flags)
2496{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002497 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302498 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002499 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302500
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002501 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302502
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002503 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302504}
2505
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002506/*
2507 * Binds an object into the global gtt with the specified cache level. The object
2508 * will be accessible to the GPU via commands whose operands reference offsets
2509 * within the global GTT as well as accessible by the GPU through the GMADR
2510 * mapped BAR (dev_priv->mm.gtt->gtt).
2511 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002512static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002513 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002514 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302515 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002516{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002517 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsonb31144c2017-02-15 08:43:36 +00002518 gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
2519 unsigned int i = start >> PAGE_SHIFT;
2520 struct sgt_iter iter;
Dave Gordon85d12252016-05-20 11:54:06 +01002521 dma_addr_t addr;
Chris Wilsonb31144c2017-02-15 08:43:36 +00002522 for_each_sgt_dma(addr, iter, st)
2523 iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2524 wmb();
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002525
2526 /* This next bit makes the above posting read even more important. We
2527 * want to flush the TLBs only after we're certain all the PTE updates
2528 * have finished.
2529 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002530 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002531}
2532
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002533static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002534 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002535{
2536}
2537
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002538static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002539 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002540{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002541 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002542 unsigned first_entry = start >> PAGE_SHIFT;
2543 unsigned num_entries = length >> PAGE_SHIFT;
Chris Wilson894cceb2017-02-15 08:43:37 +00002544 const gen8_pte_t scratch_pte =
2545 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
2546 gen8_pte_t __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002547 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2548 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002549 int i;
2550
2551 if (WARN(num_entries > max_entries,
2552 "First entry = %d; Num entries = %d (max=%d)\n",
2553 first_entry, num_entries, max_entries))
2554 num_entries = max_entries;
2555
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002556 for (i = 0; i < num_entries; i++)
2557 gen8_set_pte(&gtt_base[i], scratch_pte);
2558 readl(gtt_base);
2559}
2560
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002561static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002562 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002563 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002564{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002565 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002566 unsigned first_entry = start >> PAGE_SHIFT;
2567 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002568 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002569 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2570 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002571 int i;
2572
2573 if (WARN(num_entries > max_entries,
2574 "First entry = %d; Num entries = %d (max=%d)\n",
2575 first_entry, num_entries, max_entries))
2576 num_entries = max_entries;
2577
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002578 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002579 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002580
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002581 for (i = 0; i < num_entries; i++)
2582 iowrite32(scratch_pte, &gtt_base[i]);
2583 readl(gtt_base);
2584}
2585
Chris Wilsond6473f52016-06-10 14:22:59 +05302586static void i915_ggtt_insert_page(struct i915_address_space *vm,
2587 dma_addr_t addr,
2588 uint64_t offset,
2589 enum i915_cache_level cache_level,
2590 u32 unused)
2591{
Chris Wilsond6473f52016-06-10 14:22:59 +05302592 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2593 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302594
2595 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302596}
2597
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002598static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2599 struct sg_table *pages,
2600 uint64_t start,
2601 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002602{
2603 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2604 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2605
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002606 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002607
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002608}
2609
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002610static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002611 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002612 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002613{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002614 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002615}
2616
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002617static int ggtt_bind_vma(struct i915_vma *vma,
2618 enum i915_cache_level cache_level,
2619 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002620{
Chris Wilson49d73912016-11-29 09:50:08 +00002621 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002622 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonba7a5742017-02-15 08:43:35 +00002623 u32 pte_flags;
Daniel Vetter0a878712015-10-15 14:23:01 +02002624
Chris Wilsonba7a5742017-02-15 08:43:35 +00002625 if (unlikely(!vma->pages)) {
2626 int ret = i915_get_ggtt_vma_pages(vma);
2627 if (ret)
2628 return ret;
2629 }
Daniel Vetter0a878712015-10-15 14:23:01 +02002630
2631 /* Currently applicable only to VLV */
Chris Wilsonba7a5742017-02-15 08:43:35 +00002632 pte_flags = 0;
Daniel Vetter0a878712015-10-15 14:23:01 +02002633 if (obj->gt_ro)
2634 pte_flags |= PTE_READ_ONLY;
2635
Chris Wilson9c870d02016-10-24 13:42:15 +01002636 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002637 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002638 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002639 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002640
2641 /*
2642 * Without aliasing PPGTT there's no difference between
2643 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2644 * upgrade to both bound if we bind either to avoid double-binding.
2645 */
Chris Wilson3272db52016-08-04 16:32:32 +01002646 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002647
2648 return 0;
2649}
2650
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002651static void ggtt_unbind_vma(struct i915_vma *vma)
2652{
2653 struct drm_i915_private *i915 = vma->vm->i915;
2654
2655 intel_runtime_pm_get(i915);
2656 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2657 intel_runtime_pm_put(i915);
2658}
2659
Daniel Vetter0a878712015-10-15 14:23:01 +02002660static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2661 enum i915_cache_level cache_level,
2662 u32 flags)
2663{
Chris Wilson49d73912016-11-29 09:50:08 +00002664 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002665 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002666
Chris Wilsonba7a5742017-02-15 08:43:35 +00002667 if (unlikely(!vma->pages)) {
2668 int ret = i915_get_ggtt_vma_pages(vma);
2669 if (ret)
2670 return ret;
2671 }
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002672
Akash Goel24f3a8c2014-06-17 10:59:42 +05302673 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002674 pte_flags = 0;
2675 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002676 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302677
Chris Wilson3272db52016-08-04 16:32:32 +01002678 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002679 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002680 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002681 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002682 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002683 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002684 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002685
Chris Wilson3272db52016-08-04 16:32:32 +01002686 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002687 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002688 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002689 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002690 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002691 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002692
2693 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002694}
2695
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002696static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002697{
Chris Wilson49d73912016-11-29 09:50:08 +00002698 struct drm_i915_private *i915 = vma->vm->i915;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002699
Chris Wilson9c870d02016-10-24 13:42:15 +01002700 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2701 intel_runtime_pm_get(i915);
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002702 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002703 intel_runtime_pm_put(i915);
2704 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002705
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002706 if (vma->flags & I915_VMA_LOCAL_BIND) {
2707 struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
2708
2709 vm->clear_range(vm, vma->node.start, vma->size);
2710 }
Daniel Vetter74163902012-02-15 23:50:21 +01002711}
2712
Chris Wilson03ac84f2016-10-28 13:58:36 +01002713void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2714 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002715{
David Weinehall52a05c32016-08-22 13:32:44 +03002716 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2717 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002718 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002719
Chris Wilson307dc252016-08-05 10:14:12 +01002720 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002721 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002722 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2723 /* Wait a bit, in hopes it avoids the hang */
2724 udelay(10);
2725 }
2726 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002727
Chris Wilson03ac84f2016-10-28 13:58:36 +01002728 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002729}
Daniel Vetter644ec022012-03-26 09:45:40 +02002730
Chris Wilson45b186f2016-12-16 07:46:42 +00002731static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002732 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002733 u64 *start,
2734 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002735{
Chris Wilsona6508de2017-02-06 08:45:47 +00002736 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002737 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002738
Chris Wilsona6508de2017-02-06 08:45:47 +00002739 /* Also leave a space between the unallocated reserved node after the
2740 * GTT and any objects within the GTT, i.e. we use the color adjustment
2741 * to insert a guard page to prevent prefetches crossing over the
2742 * GTT boundary.
2743 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002744 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002745 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002746 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002747}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002748
Chris Wilson6cde9a02017-02-13 17:15:50 +00002749int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2750{
2751 struct i915_ggtt *ggtt = &i915->ggtt;
2752 struct i915_hw_ppgtt *ppgtt;
2753 int err;
2754
Chris Wilson1188bc62017-02-15 08:43:38 +00002755 ppgtt = i915_ppgtt_create(i915, NULL, "[alias]");
2756 if (IS_ERR(ppgtt))
2757 return PTR_ERR(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002758
2759 if (ppgtt->base.allocate_va_range) {
2760 err = ppgtt->base.allocate_va_range(&ppgtt->base,
2761 0, ppgtt->base.total);
2762 if (err)
Chris Wilson1188bc62017-02-15 08:43:38 +00002763 goto err_ppgtt;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002764 }
2765
2766 ppgtt->base.clear_range(&ppgtt->base,
2767 ppgtt->base.start,
2768 ppgtt->base.total);
2769
2770 i915->mm.aliasing_ppgtt = ppgtt;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002771
Chris Wilson6cde9a02017-02-13 17:15:50 +00002772 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2773 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2774
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002775 WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
2776 ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
2777
Chris Wilson6cde9a02017-02-13 17:15:50 +00002778 return 0;
2779
Chris Wilson6cde9a02017-02-13 17:15:50 +00002780err_ppgtt:
Chris Wilson1188bc62017-02-15 08:43:38 +00002781 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002782 return err;
2783}
2784
2785void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2786{
2787 struct i915_ggtt *ggtt = &i915->ggtt;
2788 struct i915_hw_ppgtt *ppgtt;
2789
2790 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2791 if (!ppgtt)
2792 return;
2793
Chris Wilson1188bc62017-02-15 08:43:38 +00002794 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002795
2796 ggtt->base.bind_vma = ggtt_bind_vma;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002797 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002798}
2799
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002800int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002801{
Ben Widawskye78891c2013-01-25 16:41:04 -08002802 /* Let GEM Manage all of the aperture.
2803 *
2804 * However, leave one page at the end still bound to the scratch page.
2805 * There are a number of places where the hardware apparently prefetches
2806 * past the end of the object, and we've seen multiple hangs with the
2807 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2808 * aperture. One page should be enough to keep any prefetching inside
2809 * of the aperture.
2810 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002811 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002812 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002813 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002814 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002815
Zhi Wangb02d22a2016-06-16 08:06:59 -04002816 ret = intel_vgt_balloon(dev_priv);
2817 if (ret)
2818 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002819
Chris Wilson95374d72016-10-12 10:05:20 +01002820 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002821 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2822 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2823 0, ggtt->mappable_end,
2824 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002825 if (ret)
2826 return ret;
2827
Chris Wilsoned2f3452012-11-15 11:32:19 +00002828 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002829 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002830 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2831 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002832 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002833 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002834 }
2835
2836 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002837 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002838 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002839
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002840 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Chris Wilson6cde9a02017-02-13 17:15:50 +00002841 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
Chris Wilson95374d72016-10-12 10:05:20 +01002842 if (ret)
Chris Wilson6cde9a02017-02-13 17:15:50 +00002843 goto err;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002844 }
2845
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002846 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002847
Chris Wilson95374d72016-10-12 10:05:20 +01002848err:
2849 drm_mm_remove_node(&ggtt->error_capture);
2850 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002851}
2852
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002853/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002854 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002855 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002856 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002857void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002858{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002859 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002860 struct i915_vma *vma, *vn;
2861
2862 ggtt->base.closed = true;
2863
2864 mutex_lock(&dev_priv->drm.struct_mutex);
2865 WARN_ON(!list_empty(&ggtt->base.active_list));
2866 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2867 WARN_ON(i915_vma_unbind(vma));
2868 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002869
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002870 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002871
Chris Wilson1188bc62017-02-15 08:43:38 +00002872 mutex_lock(&dev_priv->drm.struct_mutex);
2873 i915_gem_fini_aliasing_ppgtt(dev_priv);
2874
Chris Wilson95374d72016-10-12 10:05:20 +01002875 if (drm_mm_node_allocated(&ggtt->error_capture))
2876 drm_mm_remove_node(&ggtt->error_capture);
2877
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002878 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002879 intel_vgt_deballoon(dev_priv);
Matthew Aulded9724d2016-11-17 21:04:10 +00002880 i915_address_space_fini(&ggtt->base);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002881 }
2882
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002883 ggtt->base.cleanup(&ggtt->base);
Chris Wilson1188bc62017-02-15 08:43:38 +00002884 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002885
2886 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002887 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002888}
Daniel Vetter70e32542014-08-06 15:04:57 +02002889
Daniel Vetter2c642b02015-04-14 17:35:26 +02002890static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002891{
2892 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2893 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2894 return snb_gmch_ctl << 20;
2895}
2896
Daniel Vetter2c642b02015-04-14 17:35:26 +02002897static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002898{
2899 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2900 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2901 if (bdw_gmch_ctl)
2902 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002903
2904#ifdef CONFIG_X86_32
2905 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2906 if (bdw_gmch_ctl > 4)
2907 bdw_gmch_ctl = 4;
2908#endif
2909
Ben Widawsky9459d252013-11-03 16:53:55 -08002910 return bdw_gmch_ctl << 20;
2911}
2912
Daniel Vetter2c642b02015-04-14 17:35:26 +02002913static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002914{
2915 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2916 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2917
2918 if (gmch_ctrl)
2919 return 1 << (20 + gmch_ctrl);
2920
2921 return 0;
2922}
2923
Daniel Vetter2c642b02015-04-14 17:35:26 +02002924static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002925{
2926 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2927 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2928 return snb_gmch_ctl << 25; /* 32 MB units */
2929}
2930
Daniel Vetter2c642b02015-04-14 17:35:26 +02002931static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002932{
2933 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2934 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2935 return bdw_gmch_ctl << 25; /* 32 MB units */
2936}
2937
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002938static size_t chv_get_stolen_size(u16 gmch_ctrl)
2939{
2940 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2941 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2942
2943 /*
2944 * 0x0 to 0x10: 32MB increments starting at 0MB
2945 * 0x11 to 0x16: 4MB increments starting at 8MB
2946 * 0x17 to 0x1d: 4MB increments start at 36MB
2947 */
2948 if (gmch_ctrl < 0x11)
2949 return gmch_ctrl << 25;
2950 else if (gmch_ctrl < 0x17)
2951 return (gmch_ctrl - 0x11 + 2) << 22;
2952 else
2953 return (gmch_ctrl - 0x17 + 9) << 22;
2954}
2955
Damien Lespiau66375012014-01-09 18:02:46 +00002956static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2957{
2958 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2959 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2960
2961 if (gen9_gmch_ctl < 0xf0)
2962 return gen9_gmch_ctl << 25; /* 32 MB units */
2963 else
2964 /* 4MB increments starting at 0xf0 for 4MB */
2965 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2966}
2967
Chris Wilson34c998b2016-08-04 07:52:24 +01002968static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002969{
Chris Wilson49d73912016-11-29 09:50:08 +00002970 struct drm_i915_private *dev_priv = ggtt->base.i915;
2971 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002972 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002973 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002974
2975 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002976 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002977
Imre Deak2a073f892015-03-27 13:07:33 +02002978 /*
2979 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2980 * dropped. For WC mappings in general we have 64 byte burst writes
2981 * when the WC buffer is flushed, so we can't use it, but have to
2982 * resort to an uncached mapping. The WC issue is easily caught by the
2983 * readback check when writing GTT PTE entries.
2984 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002985 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002986 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002987 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002988 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002989 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002990 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002991 return -ENOMEM;
2992 }
2993
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002994 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002995 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002996 DRM_ERROR("Scratch setup failed\n");
2997 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002998 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002999 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08003000 }
3001
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03003002 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08003003}
3004
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003005/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3006 * bits. When using advanced contexts each context stores its own PAT, but
3007 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003008static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003009{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003010 uint64_t pat;
3011
3012 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
3013 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
3014 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
3015 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3016 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3017 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3018 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3019 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3020
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03003021 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08003022 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3023 * so RTL will always use the value corresponding to
3024 * pat_sel = 000".
3025 * So let's disable cache for GGTT to avoid screen corruptions.
3026 * MOCS still can be used though.
3027 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3028 * before this patch, i.e. the same uncached + snooping access
3029 * like on gen6/7 seems to be in effect.
3030 * - So this just fixes blitter/render access. Again it looks
3031 * like it's not just uncached access, but uncached + snooping.
3032 * So we can still hold onto all our assumptions wrt cpu
3033 * clflushing on LLC machines.
3034 */
3035 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3036
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003037 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3038 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003039 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3040 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003041}
3042
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003043static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3044{
3045 uint64_t pat;
3046
3047 /*
3048 * Map WB on BDW to snooped on CHV.
3049 *
3050 * Only the snoop bit has meaning for CHV, the rest is
3051 * ignored.
3052 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003053 * The hardware will never snoop for certain types of accesses:
3054 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3055 * - PPGTT page tables
3056 * - some other special cycles
3057 *
3058 * As with BDW, we also need to consider the following for GT accesses:
3059 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3060 * so RTL will always use the value corresponding to
3061 * pat_sel = 000".
3062 * Which means we must set the snoop bit in PAT entry 0
3063 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003064 */
3065 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3066 GEN8_PPAT(1, 0) |
3067 GEN8_PPAT(2, 0) |
3068 GEN8_PPAT(3, 0) |
3069 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3070 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3071 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3072 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3073
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003074 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3075 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003076}
3077
Chris Wilson34c998b2016-08-04 07:52:24 +01003078static void gen6_gmch_remove(struct i915_address_space *vm)
3079{
3080 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3081
3082 iounmap(ggtt->gsm);
Chris Wilson49d73912016-11-29 09:50:08 +00003083 cleanup_scratch_page(vm->i915, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003084}
3085
Joonas Lahtinend507d732016-03-18 10:42:58 +02003086static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003087{
Chris Wilson49d73912016-11-29 09:50:08 +00003088 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003089 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003090 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003091 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003092
3093 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003094 ggtt->mappable_base = pci_resource_start(pdev, 2);
3095 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003096
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003097 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3098 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003099
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003100 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003101
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003102 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003103 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003104 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003105 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003106 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003107 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003108 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003109 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003110 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003111 }
Ben Widawsky63340132013-11-04 19:32:22 -08003112
Chris Wilson34c998b2016-08-04 07:52:24 +01003113 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003114
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003115 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003116 chv_setup_private_ppat(dev_priv);
3117 else
3118 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003119
Chris Wilson34c998b2016-08-04 07:52:24 +01003120 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003121 ggtt->base.bind_vma = ggtt_bind_vma;
3122 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303123 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003124 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003125 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003126 ggtt->base.clear_range = gen8_ggtt_clear_range;
3127
3128 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3129 if (IS_CHERRYVIEW(dev_priv))
3130 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3131
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003132 ggtt->invalidate = gen6_ggtt_invalidate;
3133
Chris Wilson34c998b2016-08-04 07:52:24 +01003134 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003135}
3136
Joonas Lahtinend507d732016-03-18 10:42:58 +02003137static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003138{
Chris Wilson49d73912016-11-29 09:50:08 +00003139 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003140 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003141 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003142 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003143
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003144 ggtt->mappable_base = pci_resource_start(pdev, 2);
3145 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003146
Ben Widawskybaa09f52013-01-24 13:49:57 -08003147 /* 64/512MB is the current min/max we actually know of, but this is just
3148 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003149 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003150 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003151 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003152 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003153 }
3154
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003155 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3156 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3157 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003158
Joonas Lahtinend507d732016-03-18 10:42:58 +02003159 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003160
Chris Wilson34c998b2016-08-04 07:52:24 +01003161 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3162 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003163
Joonas Lahtinend507d732016-03-18 10:42:58 +02003164 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303165 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003166 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3167 ggtt->base.bind_vma = ggtt_bind_vma;
3168 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003169 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003170
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003171 ggtt->invalidate = gen6_ggtt_invalidate;
3172
Chris Wilson34c998b2016-08-04 07:52:24 +01003173 if (HAS_EDRAM(dev_priv))
3174 ggtt->base.pte_encode = iris_pte_encode;
3175 else if (IS_HASWELL(dev_priv))
3176 ggtt->base.pte_encode = hsw_pte_encode;
3177 else if (IS_VALLEYVIEW(dev_priv))
3178 ggtt->base.pte_encode = byt_pte_encode;
3179 else if (INTEL_GEN(dev_priv) >= 7)
3180 ggtt->base.pte_encode = ivb_pte_encode;
3181 else
3182 ggtt->base.pte_encode = snb_pte_encode;
3183
3184 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003185}
3186
Chris Wilson34c998b2016-08-04 07:52:24 +01003187static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003188{
Chris Wilson34c998b2016-08-04 07:52:24 +01003189 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003190}
3191
Joonas Lahtinend507d732016-03-18 10:42:58 +02003192static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003193{
Chris Wilson49d73912016-11-29 09:50:08 +00003194 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003195 int ret;
3196
Chris Wilson91c8a322016-07-05 10:40:23 +01003197 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003198 if (!ret) {
3199 DRM_ERROR("failed to set up gmch\n");
3200 return -EIO;
3201 }
3202
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003203 intel_gtt_get(&ggtt->base.total,
3204 &ggtt->stolen_size,
3205 &ggtt->mappable_base,
3206 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003207
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003208 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303209 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003210 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3211 ggtt->base.clear_range = i915_ggtt_clear_range;
3212 ggtt->base.bind_vma = ggtt_bind_vma;
3213 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003214 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003215
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003216 ggtt->invalidate = gmch_ggtt_invalidate;
3217
Joonas Lahtinend507d732016-03-18 10:42:58 +02003218 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003219 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3220
Ben Widawskybaa09f52013-01-24 13:49:57 -08003221 return 0;
3222}
3223
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003224/**
Chris Wilson0088e522016-08-04 07:52:21 +01003225 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003226 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003227 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003228int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003229{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003230 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003231 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003232
Chris Wilson49d73912016-11-29 09:50:08 +00003233 ggtt->base.i915 = dev_priv;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003234
Chris Wilson34c998b2016-08-04 07:52:24 +01003235 if (INTEL_GEN(dev_priv) <= 5)
3236 ret = i915_gmch_probe(ggtt);
3237 else if (INTEL_GEN(dev_priv) < 8)
3238 ret = gen6_gmch_probe(ggtt);
3239 else
3240 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003241 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003242 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003243
Chris Wilsondb9309a2017-01-05 15:30:23 +00003244 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3245 * This is easier than doing range restriction on the fly, as we
3246 * currently don't have any bits spare to pass in this upper
3247 * restriction!
3248 */
3249 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3250 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3251 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3252 }
3253
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003254 if ((ggtt->base.total - 1) >> 32) {
3255 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003256 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003257 ggtt->base.total >> 20);
3258 ggtt->base.total = 1ULL << 32;
3259 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3260 }
3261
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003262 if (ggtt->mappable_end > ggtt->base.total) {
3263 DRM_ERROR("mappable aperture extends past end of GGTT,"
3264 " aperture=%llx, total=%llx\n",
3265 ggtt->mappable_end, ggtt->base.total);
3266 ggtt->mappable_end = ggtt->base.total;
3267 }
3268
Ben Widawskybaa09f52013-01-24 13:49:57 -08003269 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003270 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003271 ggtt->base.total >> 20);
3272 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003273 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003274#ifdef CONFIG_INTEL_IOMMU
3275 if (intel_iommu_gfx_mapped)
3276 DRM_INFO("VT-d active for gfx access\n");
3277#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003278
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003279 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003280}
3281
3282/**
3283 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003284 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003285 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003286int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003287{
Chris Wilson0088e522016-08-04 07:52:21 +01003288 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3289 int ret;
3290
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003291 INIT_LIST_HEAD(&dev_priv->vm_list);
3292
Chris Wilsona6508de2017-02-06 08:45:47 +00003293 /* Note that we use page colouring to enforce a guard page at the
3294 * end of the address space. This is required as the CS may prefetch
3295 * beyond the end of the batch buffer, across the page boundary,
3296 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003297 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003298 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01003299 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00003300 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003301 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003302 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003303
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003304 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3305 dev_priv->ggtt.mappable_base,
3306 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003307 ret = -EIO;
3308 goto out_gtt_cleanup;
3309 }
3310
3311 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3312
Chris Wilson0088e522016-08-04 07:52:21 +01003313 /*
3314 * Initialise stolen early so that we may reserve preallocated
3315 * objects for the BIOS to KMS transition.
3316 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003317 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003318 if (ret)
3319 goto out_gtt_cleanup;
3320
3321 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003322
3323out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003324 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003325 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003326}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003327
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003328int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003329{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003330 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003331 return -EIO;
3332
3333 return 0;
3334}
3335
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003336void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3337{
3338 i915->ggtt.invalidate = guc_ggtt_invalidate;
3339}
3340
3341void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3342{
3343 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3344}
3345
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003346void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003347{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003348 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003349 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003350
Chris Wilsondc979972016-05-10 14:10:04 +01003351 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003352
3353 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003354 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003355
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003356 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3357
3358 /* clflush objects bound into the GGTT and rebind them. */
3359 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003360 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003361 bool ggtt_bound = false;
3362 struct i915_vma *vma;
3363
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003364 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003365 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003366 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003367
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003368 if (!i915_vma_unbind(vma))
3369 continue;
3370
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003371 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3372 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003373 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003374 }
3375
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003376 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003377 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003378 }
3379
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003380 ggtt->base.closed = false;
3381
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003382 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003383 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003384 chv_setup_private_ppat(dev_priv);
3385 else
3386 bdw_setup_private_ppat(dev_priv);
3387
3388 return;
3389 }
3390
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003391 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003392 struct i915_address_space *vm;
3393
Daniel Vetterfa423312015-04-14 17:35:23 +02003394 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3395 /* TODO: Perhaps it shouldn't be gen6 specific */
3396
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003397 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003398
Chris Wilson2bfa9962016-08-04 07:52:25 +01003399 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003400 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003401 else
3402 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003403
3404 gen6_write_page_range(dev_priv, &ppgtt->pd,
3405 0, ppgtt->base.total);
3406 }
3407 }
3408
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003409 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003410}
3411
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003412static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003413rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003414 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003415 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003416 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003417{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003418 unsigned int column, row;
3419 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003420
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003421 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003422 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003423 for (row = 0; row < height; row++) {
3424 st->nents++;
3425 /* We don't need the pages, but need to initialize
3426 * the entries so the sg list can be happily traversed.
3427 * The only thing we need are DMA addresses.
3428 */
3429 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003430 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003431 sg_dma_len(sg) = PAGE_SIZE;
3432 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003433 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003434 }
3435 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003436
3437 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003438}
3439
Chris Wilsonba7a5742017-02-15 08:43:35 +00003440static noinline struct sg_table *
3441intel_rotate_pages(struct intel_rotation_info *rot_info,
3442 struct drm_i915_gem_object *obj)
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003443{
Dave Gordon85d12252016-05-20 11:54:06 +01003444 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003445 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003446 struct sgt_iter sgt_iter;
3447 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003448 unsigned long i;
3449 dma_addr_t *page_addr_list;
3450 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003451 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003452 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003453
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003454 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003455 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003456 sizeof(dma_addr_t),
3457 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003458 if (!page_addr_list)
3459 return ERR_PTR(ret);
3460
3461 /* Allocate target SG list. */
3462 st = kmalloc(sizeof(*st), GFP_KERNEL);
3463 if (!st)
3464 goto err_st_alloc;
3465
Ville Syrjälä6687c902015-09-15 13:16:41 +03003466 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003467 if (ret)
3468 goto err_sg_alloc;
3469
3470 /* Populate source page list from the object. */
3471 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003472 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003473 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003474
Dave Gordon85d12252016-05-20 11:54:06 +01003475 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003476 st->nents = 0;
3477 sg = st->sgl;
3478
Ville Syrjälä6687c902015-09-15 13:16:41 +03003479 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3480 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3481 rot_info->plane[i].width, rot_info->plane[i].height,
3482 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003483 }
3484
Ville Syrjälä6687c902015-09-15 13:16:41 +03003485 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3486 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003487
3488 drm_free_large(page_addr_list);
3489
3490 return st;
3491
3492err_sg_alloc:
3493 kfree(st);
3494err_st_alloc:
3495 drm_free_large(page_addr_list);
3496
Ville Syrjälä6687c902015-09-15 13:16:41 +03003497 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3498 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3499
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003500 return ERR_PTR(ret);
3501}
3502
Chris Wilsonba7a5742017-02-15 08:43:35 +00003503static noinline struct sg_table *
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003504intel_partial_pages(const struct i915_ggtt_view *view,
3505 struct drm_i915_gem_object *obj)
3506{
3507 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003508 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003509 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003510 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003511 int ret = -ENOMEM;
3512
3513 st = kmalloc(sizeof(*st), GFP_KERNEL);
3514 if (!st)
3515 goto err_st_alloc;
3516
Chris Wilsond2a84a72016-10-28 13:58:34 +01003517 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003518 if (ret)
3519 goto err_sg_alloc;
3520
Chris Wilson8bab11932017-01-14 00:28:25 +00003521 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003522 GEM_BUG_ON(!iter);
3523
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003524 sg = st->sgl;
3525 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003526 do {
3527 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003528
Chris Wilsond2a84a72016-10-28 13:58:34 +01003529 len = min(iter->length - (offset << PAGE_SHIFT),
3530 count << PAGE_SHIFT);
3531 sg_set_page(sg, NULL, len, 0);
3532 sg_dma_address(sg) =
3533 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3534 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003535
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003536 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003537 count -= len >> PAGE_SHIFT;
3538 if (count == 0) {
3539 sg_mark_end(sg);
3540 return st;
3541 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003542
Chris Wilsond2a84a72016-10-28 13:58:34 +01003543 sg = __sg_next(sg);
3544 iter = __sg_next(iter);
3545 offset = 0;
3546 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003547
3548err_sg_alloc:
3549 kfree(st);
3550err_st_alloc:
3551 return ERR_PTR(ret);
3552}
3553
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003554static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003555i915_get_ggtt_vma_pages(struct i915_vma *vma)
3556{
Chris Wilsonba7a5742017-02-15 08:43:35 +00003557 int ret;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003558
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003559 /* The vma->pages are only valid within the lifespan of the borrowed
3560 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3561 * must be the vma->pages. A simple rule is that vma->pages must only
3562 * be accessed when the obj->mm.pages are pinned.
3563 */
3564 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3565
Chris Wilsonba7a5742017-02-15 08:43:35 +00003566 switch (vma->ggtt_view.type) {
3567 case I915_GGTT_VIEW_NORMAL:
3568 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003569 return 0;
3570
Chris Wilsonba7a5742017-02-15 08:43:35 +00003571 case I915_GGTT_VIEW_ROTATED:
Chris Wilson247177d2016-08-15 10:48:47 +01003572 vma->pages =
Chris Wilsonba7a5742017-02-15 08:43:35 +00003573 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3574 break;
3575
3576 case I915_GGTT_VIEW_PARTIAL:
Chris Wilson247177d2016-08-15 10:48:47 +01003577 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003578 break;
3579
3580 default:
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003581 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3582 vma->ggtt_view.type);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003583 return -EINVAL;
3584 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003585
Chris Wilsonba7a5742017-02-15 08:43:35 +00003586 ret = 0;
3587 if (unlikely(IS_ERR(vma->pages))) {
Chris Wilson247177d2016-08-15 10:48:47 +01003588 ret = PTR_ERR(vma->pages);
3589 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003590 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3591 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003592 }
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003593 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003594}
3595
Chris Wilsone007b192017-01-11 11:23:10 +00003596/**
Chris Wilson625d9882017-01-11 11:23:11 +00003597 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003598 * @vm: the &struct i915_address_space
3599 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3600 * @size: how much space to allocate inside the GTT,
3601 * must be #I915_GTT_PAGE_SIZE aligned
3602 * @offset: where to insert inside the GTT,
3603 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3604 * (@offset + @size) must fit within the address space
3605 * @color: color to apply to node, if this node is not from a VMA,
3606 * color must be #I915_COLOR_UNEVICTABLE
3607 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003608 *
3609 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3610 * the address space (using @size and @color). If the @node does not fit, it
3611 * tries to evict any overlapping nodes from the GTT, including any
3612 * neighbouring nodes if the colors do not match (to ensure guard pages between
3613 * differing domains). See i915_gem_evict_for_node() for the gory details
3614 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3615 * evicting active overlapping objects, and any overlapping node that is pinned
3616 * or marked as unevictable will also result in failure.
3617 *
3618 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3619 * asked to wait for eviction and interrupted.
3620 */
3621int i915_gem_gtt_reserve(struct i915_address_space *vm,
3622 struct drm_mm_node *node,
3623 u64 size, u64 offset, unsigned long color,
3624 unsigned int flags)
3625{
3626 int err;
3627
3628 GEM_BUG_ON(!size);
3629 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3630 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3631 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003632 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003633 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003634
3635 node->size = size;
3636 node->start = offset;
3637 node->color = color;
3638
3639 err = drm_mm_reserve_node(&vm->mm, node);
3640 if (err != -ENOSPC)
3641 return err;
3642
3643 err = i915_gem_evict_for_node(vm, node, flags);
3644 if (err == 0)
3645 err = drm_mm_reserve_node(&vm->mm, node);
3646
3647 return err;
3648}
3649
Chris Wilson606fec92017-01-11 11:23:12 +00003650static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3651{
3652 u64 range, addr;
3653
3654 GEM_BUG_ON(range_overflows(start, len, end));
3655 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3656
3657 range = round_down(end - len, align) - round_up(start, align);
3658 if (range) {
3659 if (sizeof(unsigned long) == sizeof(u64)) {
3660 addr = get_random_long();
3661 } else {
3662 addr = get_random_int();
3663 if (range > U32_MAX) {
3664 addr <<= 32;
3665 addr |= get_random_int();
3666 }
3667 }
3668 div64_u64_rem(addr, range, &addr);
3669 start += addr;
3670 }
3671
3672 return round_up(start, align);
3673}
3674
Chris Wilson625d9882017-01-11 11:23:11 +00003675/**
Chris Wilsone007b192017-01-11 11:23:10 +00003676 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003677 * @vm: the &struct i915_address_space
3678 * @node: the &struct drm_mm_node (typically i915_vma.node)
3679 * @size: how much space to allocate inside the GTT,
3680 * must be #I915_GTT_PAGE_SIZE aligned
3681 * @alignment: required alignment of starting offset, may be 0 but
3682 * if specified, this must be a power-of-two and at least
3683 * #I915_GTT_MIN_ALIGNMENT
3684 * @color: color to apply to node
3685 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003686 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003687 * @end: end of any range restriction inside GTT (U64_MAX for all),
3688 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3689 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003690 *
3691 * i915_gem_gtt_insert() first searches for an available hole into which
3692 * is can insert the node. The hole address is aligned to @alignment and
3693 * its @size must then fit entirely within the [@start, @end] bounds. The
3694 * nodes on either side of the hole must match @color, or else a guard page
3695 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003696 * suitable hole is found, first a victim is randomly selected and tested
3697 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003698 * is scanned to find the first set of replacement nodes to create the hole.
3699 * Those old overlapping nodes are evicted from the GTT (and so must be
3700 * rebound before any future use). Any node that is currently pinned cannot
3701 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3702 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3703 * searching for an eviction candidate. See i915_gem_evict_something() for
3704 * the gory details on the eviction algorithm.
3705 *
3706 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3707 * asked to wait for eviction and interrupted.
3708 */
3709int i915_gem_gtt_insert(struct i915_address_space *vm,
3710 struct drm_mm_node *node,
3711 u64 size, u64 alignment, unsigned long color,
3712 u64 start, u64 end, unsigned int flags)
3713{
Chris Wilson4e64e552017-02-02 21:04:38 +00003714 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003715 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003716 int err;
3717
3718 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3719 GEM_BUG_ON(!size);
3720 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3721 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3722 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3723 GEM_BUG_ON(start >= end);
3724 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3725 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003726 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003727 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003728
3729 if (unlikely(range_overflows(start, size, end)))
3730 return -ENOSPC;
3731
3732 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3733 return -ENOSPC;
3734
Chris Wilson4e64e552017-02-02 21:04:38 +00003735 mode = DRM_MM_INSERT_BEST;
3736 if (flags & PIN_HIGH)
3737 mode = DRM_MM_INSERT_HIGH;
3738 if (flags & PIN_MAPPABLE)
3739 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003740
3741 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3742 * so we know that we always have a minimum alignment of 4096.
3743 * The drm_mm range manager is optimised to return results
3744 * with zero alignment, so where possible use the optimal
3745 * path.
3746 */
3747 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3748 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3749 alignment = 0;
3750
Chris Wilson4e64e552017-02-02 21:04:38 +00003751 err = drm_mm_insert_node_in_range(&vm->mm, node,
3752 size, alignment, color,
3753 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003754 if (err != -ENOSPC)
3755 return err;
3756
Chris Wilson606fec92017-01-11 11:23:12 +00003757 /* No free space, pick a slot at random.
3758 *
3759 * There is a pathological case here using a GTT shared between
3760 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3761 *
3762 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3763 * (64k objects) (448k objects)
3764 *
3765 * Now imagine that the eviction LRU is ordered top-down (just because
3766 * pathology meets real life), and that we need to evict an object to
3767 * make room inside the aperture. The eviction scan then has to walk
3768 * the 448k list before it finds one within range. And now imagine that
3769 * it has to search for a new hole between every byte inside the memcpy,
3770 * for several simultaneous clients.
3771 *
3772 * On a full-ppgtt system, if we have run out of available space, there
3773 * will be lots and lots of objects in the eviction list! Again,
3774 * searching that LRU list may be slow if we are also applying any
3775 * range restrictions (e.g. restriction to low 4GiB) and so, for
3776 * simplicity and similarilty between different GTT, try the single
3777 * random replacement first.
3778 */
3779 offset = random_offset(start, end,
3780 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3781 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3782 if (err != -ENOSPC)
3783 return err;
3784
3785 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003786 err = i915_gem_evict_something(vm, size, alignment, color,
3787 start, end, flags);
3788 if (err)
3789 return err;
3790
Chris Wilson4e64e552017-02-02 21:04:38 +00003791 return drm_mm_insert_node_in_range(&vm->mm, node,
3792 size, alignment, color,
3793 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003794}
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003795
3796#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3797#include "selftests/mock_gtt.c"
Chris Wilson1c428192017-02-13 17:15:38 +00003798#include "selftests/i915_gem_gtt.c"
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003799#endif