blob: cb5ea5d38f11525737ba481307c62c108f81b470 [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;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200753 gen8_pte_t *pt_vaddr;
754 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
755 I915_CACHE_LLC);
756
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
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200768 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000769
Mika Kuoppala37c63932016-11-01 15:27:36 +0200770 while (pte < pte_end)
771 pt_vaddr[pte++] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000772
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200773 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200774
775 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200776}
777
Michał Winiarski2ce51792016-10-13 14:02:42 +0200778/* Removes entries from a single page dir, releasing it if it's empty.
779 * Caller can use the return value to update higher-level entries
780 */
781static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200782 struct i915_page_directory *pd,
783 uint64_t start,
784 uint64_t length)
785{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200786 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200787 struct i915_page_table *pt;
788 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200789 gen8_pde_t *pde_vaddr;
790 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
791 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200792
793 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000794 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100795 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000796
Michał Winiarski2ce51792016-10-13 14:02:42 +0200797 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
798 __clear_bit(pde, pd->used_pdes);
799 pde_vaddr = kmap_px(pd);
800 pde_vaddr[pde] = scratch_pde;
801 kunmap_px(ppgtt, pde_vaddr);
Chris Wilson49d73912016-11-29 09:50:08 +0000802 free_pt(vm->i915, pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200803 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200804 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200805
Zhi Wanga18dbba2016-11-29 14:55:16 +0800806 if (bitmap_empty(pd->used_pdes, I915_PDES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200807 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200808
809 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200810}
Ben Widawsky06fda602015-02-24 16:22:36 +0000811
Michał Winiarski2ce51792016-10-13 14:02:42 +0200812/* Removes entries from a single page dir pointer, releasing it if it's empty.
813 * Caller can use the return value to update higher-level entries
814 */
815static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200816 struct i915_page_directory_pointer *pdp,
817 uint64_t start,
818 uint64_t length)
819{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200820 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200821 struct i915_page_directory *pd;
822 uint64_t pdpe;
823
824 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
825 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100826 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000827
Michał Winiarski2ce51792016-10-13 14:02:42 +0200828 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
829 __clear_bit(pdpe, pdp->used_pdpes);
Matthew Auld9e65a372016-12-13 16:05:12 +0000830 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
Chris Wilson49d73912016-11-29 09:50:08 +0000831 free_pd(vm->i915, pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200832 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200833 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200834
Mika Kuoppalafce93752016-10-31 17:24:46 +0200835 mark_tlbs_dirty(ppgtt);
836
Zhi Wanga18dbba2016-11-29 14:55:16 +0800837 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200838 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200839
840 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200841}
Ben Widawsky459108b2013-11-02 21:07:23 -0700842
Michał Winiarski2ce51792016-10-13 14:02:42 +0200843/* Removes entries from a single pml4.
844 * This is the top-level structure in 4-level page tables used on gen8+.
845 * Empty entries are always scratch pml4e.
846 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200847static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
848 struct i915_pml4 *pml4,
849 uint64_t start,
850 uint64_t length)
851{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200852 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200853 struct i915_page_directory_pointer *pdp;
854 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200855
Chris Wilson49d73912016-11-29 09:50:08 +0000856 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700857
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200858 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
859 if (WARN_ON(!pml4->pdps[pml4e]))
860 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700861
Michał Winiarski2ce51792016-10-13 14:02:42 +0200862 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
863 __clear_bit(pml4e, pml4->used_pml4es);
Matthew Auld9e65a372016-12-13 16:05:12 +0000864 gen8_setup_pml4e(ppgtt, pml4, vm->scratch_pdp, pml4e);
Chris Wilson49d73912016-11-29 09:50:08 +0000865 free_pdp(vm->i915, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200866 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700867 }
868}
869
Michel Thierryf9b5b782015-07-30 11:02:49 +0100870static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200871 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700872{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300873 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100874
Chris Wilsonc6385c92016-11-29 12:42:05 +0000875 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200876 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
877 else
878 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100879}
880
881static void
882gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
883 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100884 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100885 uint64_t start,
886 enum i915_cache_level cache_level)
887{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300888 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000889 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100890 unsigned pdpe = gen8_pdpe_index(start);
891 unsigned pde = gen8_pde_index(start);
892 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700893
Chris Wilson6f1cc992013-12-31 15:50:31 +0000894 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700895
Michel Thierry3387d432015-08-03 09:52:47 +0100896 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000897 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100898 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100899 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300900 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000901 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800902
903 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100904 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200905 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000906 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300907 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000908 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000909 if (++pde == I915_PDES) {
Chris Wilsonc6385c92016-11-29 12:42:05 +0000910 if (++pdpe == I915_PDPES_PER_PDP(vm->i915))
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100911 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800912 pde = 0;
913 }
914 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700915 }
916 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300917
918 if (pt_vaddr)
919 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700920}
921
Michel Thierryf9b5b782015-07-30 11:02:49 +0100922static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
923 struct sg_table *pages,
924 uint64_t start,
925 enum i915_cache_level cache_level,
926 u32 unused)
927{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300928 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100929 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100930
Michel Thierry3387d432015-08-03 09:52:47 +0100931 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100932
Chris Wilsonc6385c92016-11-29 12:42:05 +0000933 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100934 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
935 cache_level);
936 } else {
937 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000938 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100939 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
940
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000941 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100942 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
943 start, cache_level);
944 }
945 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100946}
947
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000948static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
Michel Thierryf37c0502015-06-10 17:46:39 +0100949 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800950{
951 int i;
952
Mika Kuoppala567047b2015-06-25 18:35:12 +0300953 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800954 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800955
Michel Thierry33c88192015-04-08 12:13:33 +0100956 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000957 if (WARN_ON(!pd->page_table[i]))
958 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800959
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000960 free_pt(dev_priv, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000961 pd->page_table[i] = NULL;
962 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000963}
964
Mika Kuoppala8776f022015-06-30 18:16:40 +0300965static int gen8_init_scratch(struct i915_address_space *vm)
966{
Chris Wilson49d73912016-11-29 09:50:08 +0000967 struct drm_i915_private *dev_priv = vm->i915;
Matthew Auld64c050d2016-04-27 13:19:25 +0100968 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300969
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000970 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100971 if (ret)
972 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300973
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000974 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300975 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100976 ret = PTR_ERR(vm->scratch_pt);
977 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300978 }
979
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000980 vm->scratch_pd = alloc_pd(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300981 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100982 ret = PTR_ERR(vm->scratch_pd);
983 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300984 }
985
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000986 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
987 vm->scratch_pdp = alloc_pdp(dev_priv);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100988 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100989 ret = PTR_ERR(vm->scratch_pdp);
990 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100991 }
992 }
993
Mika Kuoppala8776f022015-06-30 18:16:40 +0300994 gen8_initialize_pt(vm, vm->scratch_pt);
995 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000996 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100997 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300998
999 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +01001000
1001free_pd:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001002 free_pd(dev_priv, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +01001003free_pt:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001004 free_pt(dev_priv, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +01001005free_scratch_page:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001006 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +01001007
1008 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001009}
1010
Zhiyuan Lv650da342015-08-28 15:41:18 +08001011static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1012{
1013 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +00001014 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001015 int i;
1016
Matthew Aulddf285642016-04-22 12:09:25 +01001017 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001018 u64 daddr = px_dma(&ppgtt->pml4);
1019
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001020 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1021 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001022
1023 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1024 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1025 } else {
1026 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1027 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1028
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001029 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1030 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001031 }
1032
1033 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1034 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1035 }
1036
1037 I915_WRITE(vgtif_reg(g2v_notify), msg);
1038
1039 return 0;
1040}
1041
Mika Kuoppala8776f022015-06-30 18:16:40 +03001042static void gen8_free_scratch(struct i915_address_space *vm)
1043{
Chris Wilson49d73912016-11-29 09:50:08 +00001044 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001045
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001046 if (USES_FULL_48BIT_PPGTT(dev_priv))
1047 free_pdp(dev_priv, vm->scratch_pdp);
1048 free_pd(dev_priv, vm->scratch_pd);
1049 free_pt(dev_priv, vm->scratch_pt);
1050 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001051}
1052
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001053static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
Michel Thierry762d9932015-07-30 11:05:29 +01001054 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001055{
1056 int i;
1057
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001058 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001059 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001060 continue;
1061
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001062 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1063 free_pd(dev_priv, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001064 }
Michel Thierry69876be2015-04-08 12:13:27 +01001065
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001066 free_pdp(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001067}
1068
1069static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1070{
Chris Wilson49d73912016-11-29 09:50:08 +00001071 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001072 int i;
1073
1074 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1075 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1076 continue;
1077
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001078 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001079 }
1080
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001081 cleanup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001082}
1083
1084static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1085{
Chris Wilson49d73912016-11-29 09:50:08 +00001086 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001087 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001088
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001089 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001090 gen8_ppgtt_notify_vgt(ppgtt, false);
1091
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001092 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1093 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001094 else
1095 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001096
Mika Kuoppala8776f022015-06-30 18:16:40 +03001097 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001098}
1099
Michel Thierryd7b26332015-04-08 12:13:34 +01001100/**
1101 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001102 * @vm: Master vm structure.
1103 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001104 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001105 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001106 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1107 * caller to free on error.
1108 *
1109 * Allocate the required number of page tables. Extremely similar to
1110 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1111 * the page directory boundary (instead of the page directory pointer). That
1112 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1113 * possible, and likely that the caller will need to use multiple calls of this
1114 * function to achieve the appropriate allocation.
1115 *
1116 * Return: 0 if success; negative error code otherwise.
1117 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001118static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001119 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001120 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001121 uint64_t length,
1122 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001123{
Chris Wilson49d73912016-11-29 09:50:08 +00001124 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001125 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001126 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001127
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001128 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001129 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001130 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001131 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001132 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001133 continue;
1134 }
1135
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001136 pt = alloc_pt(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001137 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001138 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001139
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001140 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001141 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001142 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001143 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001144 }
1145
1146 return 0;
1147
1148unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001149 for_each_set_bit(pde, new_pts, I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001150 free_pt(dev_priv, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001151
1152 return -ENOMEM;
1153}
1154
Michel Thierryd7b26332015-04-08 12:13:34 +01001155/**
1156 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001157 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001158 * @pdp: Page directory pointer for this address range.
1159 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001160 * @length: Size of the allocations.
1161 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001162 * caller to free on error.
1163 *
1164 * Allocate the required number of page directories starting at the pde index of
1165 * @start, and ending at the pde index @start + @length. This function will skip
1166 * over already allocated page directories within the range, and only allocate
1167 * new ones, setting the appropriate pointer within the pdp as well as the
1168 * correct position in the bitmap @new_pds.
1169 *
1170 * The function will only allocate the pages within the range for a give page
1171 * directory pointer. In other words, if @start + @length straddles a virtually
1172 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1173 * required by the caller, This is not currently possible, and the BUG in the
1174 * code will prevent it.
1175 *
1176 * Return: 0 if success; negative error code otherwise.
1177 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001178static int
1179gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1180 struct i915_page_directory_pointer *pdp,
1181 uint64_t start,
1182 uint64_t length,
1183 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001184{
Chris Wilson49d73912016-11-29 09:50:08 +00001185 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001186 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001187 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001188 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001189
Michel Thierry6ac18502015-07-29 17:23:46 +01001190 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001191
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001192 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001193 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001194 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001195
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001196 pd = alloc_pd(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001197 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001198 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001199
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001200 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001201 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001202 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001203 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001204 }
1205
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001206 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001207
1208unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001209 for_each_set_bit(pdpe, new_pds, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001210 free_pd(dev_priv, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001211
1212 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001213}
1214
Michel Thierry762d9932015-07-30 11:05:29 +01001215/**
1216 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1217 * @vm: Master vm structure.
1218 * @pml4: Page map level 4 for this address range.
1219 * @start: Starting virtual address to begin allocations.
1220 * @length: Size of the allocations.
1221 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1222 * caller to free on error.
1223 *
1224 * Allocate the required number of page directory pointers. Extremely similar to
1225 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1226 * The main difference is here we are limited by the pml4 boundary (instead of
1227 * the page directory pointer).
1228 *
1229 * Return: 0 if success; negative error code otherwise.
1230 */
1231static int
1232gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1233 struct i915_pml4 *pml4,
1234 uint64_t start,
1235 uint64_t length,
1236 unsigned long *new_pdps)
1237{
Chris Wilson49d73912016-11-29 09:50:08 +00001238 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001239 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001240 uint32_t pml4e;
1241
1242 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1243
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001244 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001245 if (!test_bit(pml4e, pml4->used_pml4es)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001246 pdp = alloc_pdp(dev_priv);
Michel Thierry762d9932015-07-30 11:05:29 +01001247 if (IS_ERR(pdp))
1248 goto unwind_out;
1249
Michel Thierry69ab76f2015-07-29 17:23:55 +01001250 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001251 pml4->pdps[pml4e] = pdp;
1252 __set_bit(pml4e, new_pdps);
1253 trace_i915_page_directory_pointer_entry_alloc(vm,
1254 pml4e,
1255 start,
1256 GEN8_PML4E_SHIFT);
1257 }
1258 }
1259
1260 return 0;
1261
1262unwind_out:
1263 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001264 free_pdp(dev_priv, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001265
1266 return -ENOMEM;
1267}
1268
Michel Thierryd7b26332015-04-08 12:13:34 +01001269static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001270free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001271{
Michel Thierryd7b26332015-04-08 12:13:34 +01001272 kfree(new_pts);
1273 kfree(new_pds);
1274}
1275
1276/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1277 * of these are based on the number of PDPEs in the system.
1278 */
1279static
1280int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001281 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001282 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001283{
Michel Thierryd7b26332015-04-08 12:13:34 +01001284 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001285 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001286
Michał Winiarski3a41a052015-09-03 19:22:18 +02001287 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001288 if (!pds)
1289 return -ENOMEM;
1290
Michał Winiarski3a41a052015-09-03 19:22:18 +02001291 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1292 GFP_TEMPORARY);
1293 if (!pts)
1294 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001295
1296 *new_pds = pds;
1297 *new_pts = pts;
1298
1299 return 0;
1300
1301err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001302 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001303 return -ENOMEM;
1304}
1305
Michel Thierry762d9932015-07-30 11:05:29 +01001306static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1307 struct i915_page_directory_pointer *pdp,
1308 uint64_t start,
1309 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001310{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001311 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001312 unsigned long *new_page_dirs, *new_page_tables;
Chris Wilson49d73912016-11-29 09:50:08 +00001313 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001314 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001315 const uint64_t orig_start = start;
1316 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001317 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001318 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001319 int ret;
1320
Michel Thierry6ac18502015-07-29 17:23:46 +01001321 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001322 if (ret)
1323 return ret;
1324
Michel Thierryd7b26332015-04-08 12:13:34 +01001325 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001326 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1327 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001328 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001329 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001330 return ret;
1331 }
1332
1333 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001334 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001335 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001336 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001337 if (ret)
1338 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001339 }
1340
Michel Thierry33c88192015-04-08 12:13:33 +01001341 start = orig_start;
1342 length = orig_length;
1343
Michel Thierryd7b26332015-04-08 12:13:34 +01001344 /* Allocations have completed successfully, so set the bitmaps, and do
1345 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001346 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001347 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001348 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001349 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001350 uint64_t pd_start = start;
1351 uint32_t pde;
1352
Michel Thierryd7b26332015-04-08 12:13:34 +01001353 /* Every pd should be allocated, we just did that above. */
1354 WARN_ON(!pd);
1355
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001356 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001357 /* Same reasoning as pd */
1358 WARN_ON(!pt);
1359 WARN_ON(!pd_len);
1360 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1361
1362 /* Set our used ptes within the page table */
1363 bitmap_set(pt->used_ptes,
1364 gen8_pte_index(pd_start),
1365 gen8_pte_count(pd_start, pd_len));
1366
1367 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001368 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001369
1370 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001371 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1372 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001373 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1374 gen8_pte_index(start),
1375 gen8_pte_count(start, length),
1376 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001377
1378 /* NB: We haven't yet mapped ptes to pages. At this
1379 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001380 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001381
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001382 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001383 __set_bit(pdpe, pdp->used_pdpes);
Matthew Auld5c693b22016-12-13 16:05:10 +00001384 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001385 }
1386
Michał Winiarski3a41a052015-09-03 19:22:18 +02001387 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001388 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001389 return 0;
1390
1391err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001392 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001393 unsigned long temp;
1394
Michał Winiarski3a41a052015-09-03 19:22:18 +02001395 for_each_set_bit(temp, new_page_tables + pdpe *
1396 BITS_TO_LONGS(I915_PDES), I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001397 free_pt(dev_priv,
1398 pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001399 }
1400
Michel Thierry6ac18502015-07-29 17:23:46 +01001401 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001402 free_pd(dev_priv, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001403
Michał Winiarski3a41a052015-09-03 19:22:18 +02001404 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001405 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001406 return ret;
1407}
1408
Michel Thierry762d9932015-07-30 11:05:29 +01001409static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1410 struct i915_pml4 *pml4,
1411 uint64_t start,
1412 uint64_t length)
1413{
1414 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001415 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001416 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001417 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001418 int ret = 0;
1419
1420 /* Do the pml4 allocations first, so we don't need to track the newly
1421 * allocated tables below the pdp */
1422 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1423
1424 /* The pagedirectory and pagetable allocations are done in the shared 3
1425 * and 4 level code. Just allocate the pdps.
1426 */
1427 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1428 new_pdps);
1429 if (ret)
1430 return ret;
1431
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001432 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001433 WARN_ON(!pdp);
1434
1435 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1436 if (ret)
1437 goto err_out;
1438
Matthew Auld56843102016-12-13 16:05:11 +00001439 gen8_setup_pml4e(ppgtt, pml4, pdp, pml4e);
Michel Thierry762d9932015-07-30 11:05:29 +01001440 }
1441
1442 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1443 GEN8_PML4ES_PER_PML4);
1444
1445 return 0;
1446
1447err_out:
1448 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson49d73912016-11-29 09:50:08 +00001449 gen8_ppgtt_cleanup_3lvl(vm->i915, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001450
1451 return ret;
1452}
1453
1454static int gen8_alloc_va_range(struct i915_address_space *vm,
1455 uint64_t start, uint64_t length)
1456{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001457 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001458
Chris Wilsonc6385c92016-11-29 12:42:05 +00001459 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001460 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1461 else
1462 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1463}
1464
Michel Thierryea91e402015-07-29 17:23:57 +01001465static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1466 uint64_t start, uint64_t length,
1467 gen8_pte_t scratch_pte,
1468 struct seq_file *m)
1469{
1470 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001471 uint32_t pdpe;
1472
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001473 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001474 struct i915_page_table *pt;
1475 uint64_t pd_len = length;
1476 uint64_t pd_start = start;
1477 uint32_t pde;
1478
1479 if (!test_bit(pdpe, pdp->used_pdpes))
1480 continue;
1481
1482 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001483 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001484 uint32_t pte;
1485 gen8_pte_t *pt_vaddr;
1486
1487 if (!test_bit(pde, pd->used_pdes))
1488 continue;
1489
1490 pt_vaddr = kmap_px(pt);
1491 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1492 uint64_t va =
1493 (pdpe << GEN8_PDPE_SHIFT) |
1494 (pde << GEN8_PDE_SHIFT) |
1495 (pte << GEN8_PTE_SHIFT);
1496 int i;
1497 bool found = false;
1498
1499 for (i = 0; i < 4; i++)
1500 if (pt_vaddr[pte + i] != scratch_pte)
1501 found = true;
1502 if (!found)
1503 continue;
1504
1505 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1506 for (i = 0; i < 4; i++) {
1507 if (pt_vaddr[pte + i] != scratch_pte)
1508 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1509 else
1510 seq_puts(m, " SCRATCH ");
1511 }
1512 seq_puts(m, "\n");
1513 }
1514 /* don't use kunmap_px, it could trigger
1515 * an unnecessary flush.
1516 */
1517 kunmap_atomic(pt_vaddr);
1518 }
1519 }
1520}
1521
1522static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1523{
1524 struct i915_address_space *vm = &ppgtt->base;
1525 uint64_t start = ppgtt->base.start;
1526 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001527 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001528 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001529
Chris Wilsonc6385c92016-11-29 12:42:05 +00001530 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryea91e402015-07-29 17:23:57 +01001531 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1532 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001533 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001534 struct i915_pml4 *pml4 = &ppgtt->pml4;
1535 struct i915_page_directory_pointer *pdp;
1536
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001537 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001538 if (!test_bit(pml4e, pml4->used_pml4es))
1539 continue;
1540
1541 seq_printf(m, " PML4E #%llu\n", pml4e);
1542 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1543 }
1544 }
1545}
1546
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001547static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1548{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001549 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001550 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001551 int ret;
1552
1553 /* We allocate temp bitmap for page tables for no gain
1554 * but as this is for init only, lets keep the things simple
1555 */
1556 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1557 if (ret)
1558 return ret;
1559
1560 /* Allocate for all pdps regardless of how the ppgtt
1561 * was defined.
1562 */
1563 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1564 0, 1ULL << 32,
1565 new_page_dirs);
1566 if (!ret)
1567 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1568
Michał Winiarski3a41a052015-09-03 19:22:18 +02001569 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001570
1571 return ret;
1572}
1573
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001574/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001575 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1576 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1577 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1578 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001579 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001580 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001581static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001582{
Chris Wilson49d73912016-11-29 09:50:08 +00001583 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001584 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001585
Mika Kuoppala8776f022015-06-30 18:16:40 +03001586 ret = gen8_init_scratch(&ppgtt->base);
1587 if (ret)
1588 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001589
Michel Thierryd7b26332015-04-08 12:13:34 +01001590 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001591 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001592 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001593 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001594 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001595 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1596 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001597 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001598
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001599 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1600 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001601 if (ret)
1602 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001603
Michel Thierry69ab76f2015-07-29 17:23:55 +01001604 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1605
Michel Thierry762d9932015-07-30 11:05:29 +01001606 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001607 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001608 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001609 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001610 if (ret)
1611 goto free_scratch;
1612
1613 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001614 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001615 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1616 0, 0,
1617 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001618
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001619 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001620 ret = gen8_preallocate_top_level_pdps(ppgtt);
1621 if (ret)
1622 goto free_scratch;
1623 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001624 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001625
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001626 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001627 gen8_ppgtt_notify_vgt(ppgtt, true);
1628
Michel Thierryd7b26332015-04-08 12:13:34 +01001629 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001630
1631free_scratch:
1632 gen8_free_scratch(&ppgtt->base);
1633 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001634}
1635
Ben Widawsky87d60b62013-12-06 14:11:29 -08001636static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1637{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001638 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001639 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001640 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001641 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001642 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001643 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001644
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001645 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001646 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001647
Dave Gordon731f74c2016-06-24 19:37:46 +01001648 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001649 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001650 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001651 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001652 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001653 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1654
1655 if (pd_entry != expected)
1656 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1657 pde,
1658 pd_entry,
1659 expected);
1660 seq_printf(m, "\tPDE: %x\n", pd_entry);
1661
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001662 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1663
Michel Thierry07749ef2015-03-16 16:00:54 +00001664 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001665 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001666 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001667 (pte * PAGE_SIZE);
1668 int i;
1669 bool found = false;
1670 for (i = 0; i < 4; i++)
1671 if (pt_vaddr[pte + i] != scratch_pte)
1672 found = true;
1673 if (!found)
1674 continue;
1675
1676 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1677 for (i = 0; i < 4; i++) {
1678 if (pt_vaddr[pte + i] != scratch_pte)
1679 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1680 else
1681 seq_puts(m, " SCRATCH ");
1682 }
1683 seq_puts(m, "\n");
1684 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001685 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001686 }
1687}
1688
Ben Widawsky678d96f2015-03-16 16:00:56 +00001689/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001690static void gen6_write_pde(struct i915_page_directory *pd,
1691 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001692{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001693 /* Caller needs to make sure the write completes if necessary */
1694 struct i915_hw_ppgtt *ppgtt =
1695 container_of(pd, struct i915_hw_ppgtt, pd);
1696 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001697
Mika Kuoppala567047b2015-06-25 18:35:12 +03001698 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001699 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001700
Ben Widawsky678d96f2015-03-16 16:00:56 +00001701 writel(pd_entry, ppgtt->pd_addr + pde);
1702}
Ben Widawsky61973492013-04-08 18:43:54 -07001703
Ben Widawsky678d96f2015-03-16 16:00:56 +00001704/* Write all the page tables found in the ppgtt structure to incrementing page
1705 * directories. */
1706static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001707 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001708 uint32_t start, uint32_t length)
1709{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001710 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001711 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001712 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001713
Dave Gordon731f74c2016-06-24 19:37:46 +01001714 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001715 gen6_write_pde(pd, pde, pt);
1716
1717 /* Make sure write is complete before other code can use this page
1718 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001719 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001720}
1721
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001722static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001723{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001724 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001725
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001726 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001727}
Ben Widawsky61973492013-04-08 18:43:54 -07001728
Ben Widawsky90252e52013-12-06 14:11:12 -08001729static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001730 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001731{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001732 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001733 u32 *cs;
Ben Widawsky90252e52013-12-06 14:11:12 -08001734 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001735
Ben Widawsky90252e52013-12-06 14:11:12 -08001736 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001737 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001738 if (ret)
1739 return ret;
1740
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001741 cs = intel_ring_begin(req, 6);
1742 if (IS_ERR(cs))
1743 return PTR_ERR(cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001744
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001745 *cs++ = MI_LOAD_REGISTER_IMM(2);
1746 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1747 *cs++ = PP_DIR_DCLV_2G;
1748 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1749 *cs++ = get_pd_offset(ppgtt);
1750 *cs++ = MI_NOOP;
1751 intel_ring_advance(req, cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001752
1753 return 0;
1754}
1755
Ben Widawsky48a10382013-12-06 14:11:11 -08001756static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001757 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001758{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001759 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001760 u32 *cs;
Ben Widawsky48a10382013-12-06 14:11:11 -08001761 int ret;
1762
Ben Widawsky48a10382013-12-06 14:11:11 -08001763 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001764 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001765 if (ret)
1766 return ret;
1767
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001768 cs = intel_ring_begin(req, 6);
1769 if (IS_ERR(cs))
1770 return PTR_ERR(cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001771
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001772 *cs++ = MI_LOAD_REGISTER_IMM(2);
1773 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1774 *cs++ = PP_DIR_DCLV_2G;
1775 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1776 *cs++ = get_pd_offset(ppgtt);
1777 *cs++ = MI_NOOP;
1778 intel_ring_advance(req, cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001779
Ben Widawsky90252e52013-12-06 14:11:12 -08001780 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001781 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001782 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001783 if (ret)
1784 return ret;
1785 }
1786
Ben Widawsky48a10382013-12-06 14:11:11 -08001787 return 0;
1788}
1789
Ben Widawskyeeb94882013-12-06 14:11:10 -08001790static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001791 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001792{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001793 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001794 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001795
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001796 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1797 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001798 return 0;
1799}
1800
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001801static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001802{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001803 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301804 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001805
Akash Goel3b3f1652016-10-13 22:44:48 +05301806 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001807 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1808 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001809 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001810 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001811 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001812}
1813
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001814static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001815{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001816 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001817 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301818 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001819
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001820 ecobits = I915_READ(GAC_ECO_BITS);
1821 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1822
1823 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001824 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001825 ecochk |= ECOCHK_PPGTT_WB_HSW;
1826 } else {
1827 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1828 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1829 }
1830 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001831
Akash Goel3b3f1652016-10-13 22:44:48 +05301832 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001833 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001834 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001835 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001836 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001837}
1838
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001839static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001840{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001841 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001842
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001843 ecobits = I915_READ(GAC_ECO_BITS);
1844 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1845 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001846
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001847 gab_ctl = I915_READ(GAB_CTL);
1848 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001849
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001850 ecochk = I915_READ(GAM_ECOCHK);
1851 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001852
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001853 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001854}
1855
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001856/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001857static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001858 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001859 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001860{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001861 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001862 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001863 unsigned first_entry = start >> PAGE_SHIFT;
1864 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001865 unsigned act_pt = first_entry / GEN6_PTES;
1866 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001867 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001868
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001869 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001870 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001871
Daniel Vetter7bddb012012-02-09 17:15:47 +01001872 while (num_entries) {
1873 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001874 if (last_pte > GEN6_PTES)
1875 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001876
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001877 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001878
1879 for (i = first_pte; i < last_pte; i++)
1880 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001881
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001882 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001883
Daniel Vetter7bddb012012-02-09 17:15:47 +01001884 num_entries -= last_pte - first_pte;
1885 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001886 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001887 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001888}
1889
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001890static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001891 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001892 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301893 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001894{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001895 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001896 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001897 unsigned act_pt = first_entry / GEN6_PTES;
1898 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001899 gen6_pte_t *pt_vaddr = NULL;
1900 struct sgt_iter sgt_iter;
1901 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001902
Dave Gordon85d12252016-05-20 11:54:06 +01001903 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001904 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001905 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001906
Chris Wilsoncc797142013-12-31 15:50:30 +00001907 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001908 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301909
Michel Thierry07749ef2015-03-16 16:00:54 +00001910 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001911 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001912 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001913 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001914 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001915 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001916 }
Dave Gordon85d12252016-05-20 11:54:06 +01001917
Chris Wilsoncc797142013-12-31 15:50:30 +00001918 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001919 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001920}
1921
Ben Widawsky678d96f2015-03-16 16:00:56 +00001922static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001923 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001924{
Michel Thierry4933d512015-03-24 15:46:22 +00001925 DECLARE_BITMAP(new_page_tables, I915_PDES);
Chris Wilson49d73912016-11-29 09:50:08 +00001926 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001927 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001928 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001929 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001930 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001931 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001932 int ret;
1933
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001934 start = start_save = start_in;
1935 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001936
1937 bitmap_zero(new_page_tables, I915_PDES);
1938
1939 /* The allocation is done in two stages so that we can bail out with
1940 * minimal amount of pain. The first stage finds new page tables that
1941 * need allocation. The second stage marks use ptes within the page
1942 * tables.
1943 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001944 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001945 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001946 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1947 continue;
1948 }
1949
1950 /* We've already allocated a page table */
1951 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1952
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001953 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001954 if (IS_ERR(pt)) {
1955 ret = PTR_ERR(pt);
1956 goto unwind_out;
1957 }
1958
1959 gen6_initialize_pt(vm, pt);
1960
1961 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001962 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001963 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001964 }
1965
1966 start = start_save;
1967 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001968
Dave Gordon731f74c2016-06-24 19:37:46 +01001969 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001970 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1971
1972 bitmap_zero(tmp_bitmap, GEN6_PTES);
1973 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1974 gen6_pte_count(start, length));
1975
Mika Kuoppala966082c2015-06-25 18:35:19 +03001976 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001977 gen6_write_pde(&ppgtt->pd, pde, pt);
1978
Michel Thierry72744cb2015-03-24 15:46:23 +00001979 trace_i915_page_table_entry_map(vm, pde, pt,
1980 gen6_pte_index(start),
1981 gen6_pte_count(start, length),
1982 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001983 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001984 GEN6_PTES);
1985 }
1986
Michel Thierry4933d512015-03-24 15:46:22 +00001987 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1988
1989 /* Make sure write is complete before other code can use this page
1990 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001991 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001992
Ben Widawsky563222a2015-03-19 12:53:28 +00001993 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001994 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001995
1996unwind_out:
1997 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001998 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00001999
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002000 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002001 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002002 }
2003
2004 mark_tlbs_dirty(ppgtt);
2005 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002006}
2007
Mika Kuoppala8776f022015-06-30 18:16:40 +03002008static int gen6_init_scratch(struct i915_address_space *vm)
2009{
Chris Wilson49d73912016-11-29 09:50:08 +00002010 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002011 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002012
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002013 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002014 if (ret)
2015 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002016
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002017 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002018 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002019 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002020 return PTR_ERR(vm->scratch_pt);
2021 }
2022
2023 gen6_initialize_pt(vm, vm->scratch_pt);
2024
2025 return 0;
2026}
2027
2028static void gen6_free_scratch(struct i915_address_space *vm)
2029{
Chris Wilson49d73912016-11-29 09:50:08 +00002030 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002031
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002032 free_pt(dev_priv, vm->scratch_pt);
2033 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002034}
2035
Daniel Vetter061dd492015-04-14 17:35:13 +02002036static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002037{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002038 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002039 struct i915_page_directory *pd = &ppgtt->pd;
Chris Wilson49d73912016-11-29 09:50:08 +00002040 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry09942c62015-04-08 12:13:30 +01002041 struct i915_page_table *pt;
2042 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002043
Daniel Vetter061dd492015-04-14 17:35:13 +02002044 drm_mm_remove_node(&ppgtt->node);
2045
Dave Gordon731f74c2016-06-24 19:37:46 +01002046 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002047 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002048 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002049
Mika Kuoppala8776f022015-06-30 18:16:40 +03002050 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002051}
2052
Ben Widawskyb1465202014-02-19 22:05:49 -08002053static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002054{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002055 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00002056 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002057 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002058 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002059
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002060 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2061 * allocator works in address space sizes, so it's multiplied by page
2062 * size. We allocate at the top of the GTT to avoid fragmentation.
2063 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002064 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002065
Mika Kuoppala8776f022015-06-30 18:16:40 +03002066 ret = gen6_init_scratch(vm);
2067 if (ret)
2068 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002069
Chris Wilsone007b192017-01-11 11:23:10 +00002070 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2071 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2072 I915_COLOR_UNEVICTABLE,
2073 0, ggtt->base.total,
2074 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00002075 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002076 goto err_out;
2077
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002078 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002079 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002080
Ben Widawskyc8c26622015-01-22 17:01:25 +00002081 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002082
2083err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002084 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002085 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002086}
2087
Ben Widawskyb1465202014-02-19 22:05:49 -08002088static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2089{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002090 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002091}
2092
Michel Thierry4933d512015-03-24 15:46:22 +00002093static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2094 uint64_t start, uint64_t length)
2095{
Michel Thierryec565b32015-04-08 12:13:23 +01002096 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002097 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002098
Dave Gordon731f74c2016-06-24 19:37:46 +01002099 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002100 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002101}
2102
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002103static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002104{
Chris Wilson49d73912016-11-29 09:50:08 +00002105 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002106 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002107 int ret;
2108
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002109 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002110 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002111 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002112 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002113 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002114 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002115 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002116 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002117 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002118
2119 ret = gen6_ppgtt_alloc(ppgtt);
2120 if (ret)
2121 return ret;
2122
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002123 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002124 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2125 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002126 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2127 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002128 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002129 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002130 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002131 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002132
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002133 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002134 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002135
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002136 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002137 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002138
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002139 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002140
Ben Widawsky678d96f2015-03-16 16:00:56 +00002141 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2142
Thierry Reding440fd522015-01-23 09:05:06 +01002143 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002144 ppgtt->node.size >> 20,
2145 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002146
Daniel Vetterfa76da32014-08-06 20:19:54 +02002147 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002148 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002149
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002150 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002151}
2152
Chris Wilson2bfa9962016-08-04 07:52:25 +01002153static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2154 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002155{
Chris Wilson49d73912016-11-29 09:50:08 +00002156 ppgtt->base.i915 = dev_priv;
Daniel Vetter3440d262013-01-24 13:49:56 -08002157
Chris Wilson2bfa9962016-08-04 07:52:25 +01002158 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002159 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002160 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002161 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002162}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002163
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002164static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002165 struct drm_i915_private *dev_priv,
2166 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002167{
Chris Wilson80b204b2016-10-28 13:58:58 +01002168 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00002169
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002170 drm_mm_init(&vm->mm, vm->start, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00002171 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
2172
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002173 INIT_LIST_HEAD(&vm->active_list);
2174 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002175 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00002176
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002177 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2178}
2179
Matthew Aulded9724d2016-11-17 21:04:10 +00002180static void i915_address_space_fini(struct i915_address_space *vm)
2181{
2182 i915_gem_timeline_fini(&vm->timeline);
2183 drm_mm_takedown(&vm->mm);
2184 list_del(&vm->global_link);
2185}
2186
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002187static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002188{
Tim Gored5165eb2016-02-04 11:49:34 +00002189 /* This function is for gtt related workarounds. This function is
2190 * called on driver load and after a GPU reset, so you can place
2191 * workarounds here even if they get overwritten by GPU reset.
2192 */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002193 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002194 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002195 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002196 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002197 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08002198 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002199 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002200 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002201 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2202}
2203
Chris Wilson2bfa9962016-08-04 07:52:25 +01002204static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2205 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002206 struct drm_i915_file_private *file_priv,
2207 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002208{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002209 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002210
Chris Wilson2bfa9962016-08-04 07:52:25 +01002211 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002212 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002213 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002214 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002215 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002216 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002217
2218 return ret;
2219}
2220
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002221int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002222{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002223 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002224
Thomas Daniel671b50132014-08-20 16:24:50 +01002225 /* In the case of execlists, PPGTT is enabled by the context descriptor
2226 * and the PDPs are contained within the context itself. We don't
2227 * need to do anything here. */
2228 if (i915.enable_execlists)
2229 return 0;
2230
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002231 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002232 return 0;
2233
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002234 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002235 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002236 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002237 gen7_ppgtt_enable(dev_priv);
2238 else if (INTEL_GEN(dev_priv) >= 8)
2239 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002240 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002241 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002242
John Harrison4ad2fd82015-06-18 13:11:20 +01002243 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002244}
John Harrison4ad2fd82015-06-18 13:11:20 +01002245
Daniel Vetter4d884702014-08-06 15:04:47 +02002246struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002247i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002248 struct drm_i915_file_private *fpriv,
2249 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002250{
2251 struct i915_hw_ppgtt *ppgtt;
2252 int ret;
2253
2254 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2255 if (!ppgtt)
2256 return ERR_PTR(-ENOMEM);
2257
Chris Wilson80b204b2016-10-28 13:58:58 +01002258 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002259 if (ret) {
2260 kfree(ppgtt);
2261 return ERR_PTR(ret);
2262 }
2263
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002264 trace_i915_ppgtt_create(&ppgtt->base);
2265
Daniel Vetter4d884702014-08-06 15:04:47 +02002266 return ppgtt;
2267}
2268
Chris Wilson0c7eeda2017-01-11 21:09:25 +00002269void i915_ppgtt_close(struct i915_address_space *vm)
2270{
2271 struct list_head *phases[] = {
2272 &vm->active_list,
2273 &vm->inactive_list,
2274 &vm->unbound_list,
2275 NULL,
2276 }, **phase;
2277
2278 GEM_BUG_ON(vm->closed);
2279 vm->closed = true;
2280
2281 for (phase = phases; *phase; phase++) {
2282 struct i915_vma *vma, *vn;
2283
2284 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2285 if (!i915_vma_is_closed(vma))
2286 i915_vma_close(vma);
2287 }
2288}
2289
Matthew Aulded9724d2016-11-17 21:04:10 +00002290void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002291{
2292 struct i915_hw_ppgtt *ppgtt =
2293 container_of(kref, struct i915_hw_ppgtt, ref);
2294
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002295 trace_i915_ppgtt_release(&ppgtt->base);
2296
Chris Wilson50e046b2016-08-04 07:52:46 +01002297 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002298 WARN_ON(!list_empty(&ppgtt->base.active_list));
2299 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002300 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002301
Matthew Aulded9724d2016-11-17 21:04:10 +00002302 i915_address_space_fini(&ppgtt->base);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002303
Daniel Vetteree960be2014-08-06 15:04:45 +02002304 ppgtt->base.cleanup(&ppgtt->base);
2305 kfree(ppgtt);
2306}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002307
Ben Widawskya81cc002013-01-18 12:30:31 -08002308/* Certain Gen5 chipsets require require idling the GPU before
2309 * unmapping anything from the GTT when VT-d is enabled.
2310 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002311static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002312{
2313#ifdef CONFIG_INTEL_IOMMU
2314 /* Query intel_iommu to see if we need the workaround. Presumably that
2315 * was loaded first.
2316 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002317 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002318 return true;
2319#endif
2320 return false;
2321}
2322
Chris Wilsondc979972016-05-10 14:10:04 +01002323void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002324{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002325 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302326 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002327
Chris Wilsondc979972016-05-10 14:10:04 +01002328 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002329 return;
2330
Akash Goel3b3f1652016-10-13 22:44:48 +05302331 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002332 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002333 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002334 if (fault_reg & RING_FAULT_VALID) {
2335 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002336 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002337 "\tAddress space: %s\n"
2338 "\tSource ID: %d\n"
2339 "\tType: %d\n",
2340 fault_reg & PAGE_MASK,
2341 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2342 RING_FAULT_SRCID(fault_reg),
2343 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002344 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002345 fault_reg & ~RING_FAULT_VALID);
2346 }
2347 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302348
2349 /* Engine specific init may not have been done till this point. */
2350 if (dev_priv->engine[RCS])
2351 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002352}
2353
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002354void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002355{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002356 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002357
2358 /* Don't bother messing with faults pre GEN6 as we have little
2359 * documentation supporting that it's a good idea.
2360 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002361 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002362 return;
2363
Chris Wilsondc979972016-05-10 14:10:04 +01002364 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002365
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002366 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002367
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002368 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002369}
2370
Chris Wilson03ac84f2016-10-28 13:58:36 +01002371int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2372 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002373{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002374 do {
2375 if (dma_map_sg(&obj->base.dev->pdev->dev,
2376 pages->sgl, pages->nents,
2377 PCI_DMA_BIDIRECTIONAL))
2378 return 0;
2379
2380 /* If the DMA remap fails, one cause can be that we have
2381 * too many objects pinned in a small remapping table,
2382 * such as swiotlb. Incrementally purge all other objects and
2383 * try again - if there are no more pages to remove from
2384 * the DMA remapper, i915_gem_shrink will return 0.
2385 */
2386 GEM_BUG_ON(obj->mm.pages == pages);
2387 } while (i915_gem_shrink(to_i915(obj->base.dev),
2388 obj->base.size >> PAGE_SHIFT,
2389 I915_SHRINK_BOUND |
2390 I915_SHRINK_UNBOUND |
2391 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002392
Chris Wilson03ac84f2016-10-28 13:58:36 +01002393 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002394}
2395
Daniel Vetter2c642b02015-04-14 17:35:26 +02002396static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002397{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002398 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002399}
2400
Chris Wilsond6473f52016-06-10 14:22:59 +05302401static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2402 dma_addr_t addr,
2403 uint64_t offset,
2404 enum i915_cache_level level,
2405 u32 unused)
2406{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002407 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302408 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002409 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302410
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002411 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302412
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002413 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302414}
2415
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002416static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2417 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002418 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302419 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002420{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002421 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002422 struct sgt_iter sgt_iter;
2423 gen8_pte_t __iomem *gtt_entries;
2424 gen8_pte_t gtt_entry;
2425 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002426 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002427
Dave Gordon85d12252016-05-20 11:54:06 +01002428 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2429
2430 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002431 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002432 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002433 }
2434
2435 /*
2436 * XXX: This serves as a posting read to make sure that the PTE has
2437 * actually been updated. There is some concern that even though
2438 * registers and PTEs are within the same BAR that they are potentially
2439 * of NUMA access patterns. Therefore, even with the way we assume
2440 * hardware should work, we must keep this posting read for paranoia.
2441 */
2442 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002443 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002444
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002445 /* This next bit makes the above posting read even more important. We
2446 * want to flush the TLBs only after we're certain all the PTE updates
2447 * have finished.
2448 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002449 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002450}
2451
Chris Wilsonc1403302015-11-18 15:19:39 +00002452struct insert_entries {
2453 struct i915_address_space *vm;
2454 struct sg_table *st;
2455 uint64_t start;
2456 enum i915_cache_level level;
2457 u32 flags;
2458};
2459
2460static int gen8_ggtt_insert_entries__cb(void *_arg)
2461{
2462 struct insert_entries *arg = _arg;
2463 gen8_ggtt_insert_entries(arg->vm, arg->st,
2464 arg->start, arg->level, arg->flags);
2465 return 0;
2466}
2467
2468static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2469 struct sg_table *st,
2470 uint64_t start,
2471 enum i915_cache_level level,
2472 u32 flags)
2473{
2474 struct insert_entries arg = { vm, st, start, level, flags };
2475 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2476}
2477
Chris Wilsond6473f52016-06-10 14:22:59 +05302478static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2479 dma_addr_t addr,
2480 uint64_t offset,
2481 enum i915_cache_level level,
2482 u32 flags)
2483{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002484 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302485 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002486 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302487
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002488 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302489
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002490 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302491}
2492
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002493/*
2494 * Binds an object into the global gtt with the specified cache level. The object
2495 * will be accessible to the GPU via commands whose operands reference offsets
2496 * within the global GTT as well as accessible by the GPU through the GMADR
2497 * mapped BAR (dev_priv->mm.gtt->gtt).
2498 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002499static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002500 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002501 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302502 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002503{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002504 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002505 struct sgt_iter sgt_iter;
2506 gen6_pte_t __iomem *gtt_entries;
2507 gen6_pte_t gtt_entry;
2508 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002509 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002510
Dave Gordon85d12252016-05-20 11:54:06 +01002511 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2512
2513 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002514 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002515 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002516 }
2517
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002518 /* XXX: This serves as a posting read to make sure that the PTE has
2519 * actually been updated. There is some concern that even though
2520 * registers and PTEs are within the same BAR that they are potentially
2521 * of NUMA access patterns. Therefore, even with the way we assume
2522 * hardware should work, we must keep this posting read for paranoia.
2523 */
Dave Gordon85d12252016-05-20 11:54:06 +01002524 if (i != 0)
2525 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002526
2527 /* This next bit makes the above posting read even more important. We
2528 * want to flush the TLBs only after we're certain all the PTE updates
2529 * have finished.
2530 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002531 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002532}
2533
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002534static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002535 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002536{
2537}
2538
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002539static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002540 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002541{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002542 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002543 unsigned first_entry = start >> PAGE_SHIFT;
2544 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002545 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002546 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2547 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002548 int i;
2549
2550 if (WARN(num_entries > max_entries,
2551 "First entry = %d; Num entries = %d (max=%d)\n",
2552 first_entry, num_entries, max_entries))
2553 num_entries = max_entries;
2554
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002555 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002556 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002557 for (i = 0; i < num_entries; i++)
2558 gen8_set_pte(&gtt_base[i], scratch_pte);
2559 readl(gtt_base);
2560}
2561
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002562static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002563 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002564 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002565{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002566 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002567 unsigned first_entry = start >> PAGE_SHIFT;
2568 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002569 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002570 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2571 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002572 int i;
2573
2574 if (WARN(num_entries > max_entries,
2575 "First entry = %d; Num entries = %d (max=%d)\n",
2576 first_entry, num_entries, max_entries))
2577 num_entries = max_entries;
2578
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002579 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002580 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002581
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002582 for (i = 0; i < num_entries; i++)
2583 iowrite32(scratch_pte, &gtt_base[i]);
2584 readl(gtt_base);
2585}
2586
Chris Wilsond6473f52016-06-10 14:22:59 +05302587static void i915_ggtt_insert_page(struct i915_address_space *vm,
2588 dma_addr_t addr,
2589 uint64_t offset,
2590 enum i915_cache_level cache_level,
2591 u32 unused)
2592{
Chris Wilsond6473f52016-06-10 14:22:59 +05302593 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2594 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302595
2596 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302597}
2598
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002599static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2600 struct sg_table *pages,
2601 uint64_t start,
2602 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002603{
2604 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2605 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2606
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002607 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002608
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002609}
2610
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002611static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002612 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002613 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002614{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002615 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002616}
2617
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002618static int ggtt_bind_vma(struct i915_vma *vma,
2619 enum i915_cache_level cache_level,
2620 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002621{
Chris Wilson49d73912016-11-29 09:50:08 +00002622 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002623 struct drm_i915_gem_object *obj = vma->obj;
2624 u32 pte_flags = 0;
2625 int ret;
2626
2627 ret = i915_get_ggtt_vma_pages(vma);
2628 if (ret)
2629 return ret;
2630
2631 /* Currently applicable only to VLV */
2632 if (obj->gt_ro)
2633 pte_flags |= PTE_READ_ONLY;
2634
Chris Wilson9c870d02016-10-24 13:42:15 +01002635 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002636 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002637 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002638 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002639
2640 /*
2641 * Without aliasing PPGTT there's no difference between
2642 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2643 * upgrade to both bound if we bind either to avoid double-binding.
2644 */
Chris Wilson3272db52016-08-04 16:32:32 +01002645 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002646
2647 return 0;
2648}
2649
2650static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2651 enum i915_cache_level cache_level,
2652 u32 flags)
2653{
Chris Wilson49d73912016-11-29 09:50:08 +00002654 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002655 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002656 int ret;
2657
2658 ret = i915_get_ggtt_vma_pages(vma);
2659 if (ret)
2660 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002661
Akash Goel24f3a8c2014-06-17 10:59:42 +05302662 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002663 pte_flags = 0;
2664 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002665 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302666
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002667
Chris Wilson3272db52016-08-04 16:32:32 +01002668 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002669 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002670 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002671 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002672 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002673 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002674 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002675
Chris Wilson3272db52016-08-04 16:32:32 +01002676 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002677 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002678 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002679 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002680 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002681 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002682
2683 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002684}
2685
2686static void ggtt_unbind_vma(struct i915_vma *vma)
2687{
Chris Wilson49d73912016-11-29 09:50:08 +00002688 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson9c870d02016-10-24 13:42:15 +01002689 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002690 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002691
Chris Wilson9c870d02016-10-24 13:42:15 +01002692 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2693 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002694 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002695 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002696 intel_runtime_pm_put(i915);
2697 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002698
Chris Wilson3272db52016-08-04 16:32:32 +01002699 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002700 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002701 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002702}
2703
Chris Wilson03ac84f2016-10-28 13:58:36 +01002704void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2705 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002706{
David Weinehall52a05c32016-08-22 13:32:44 +03002707 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2708 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002709 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002710
Chris Wilson307dc252016-08-05 10:14:12 +01002711 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002712 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002713 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2714 /* Wait a bit, in hopes it avoids the hang */
2715 udelay(10);
2716 }
2717 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002718
Chris Wilson03ac84f2016-10-28 13:58:36 +01002719 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002720}
Daniel Vetter644ec022012-03-26 09:45:40 +02002721
Chris Wilson45b186f2016-12-16 07:46:42 +00002722static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002723 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002724 u64 *start,
2725 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002726{
Chris Wilsona6508de2017-02-06 08:45:47 +00002727 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002728 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002729
Chris Wilsona6508de2017-02-06 08:45:47 +00002730 /* Also leave a space between the unallocated reserved node after the
2731 * GTT and any objects within the GTT, i.e. we use the color adjustment
2732 * to insert a guard page to prevent prefetches crossing over the
2733 * GTT boundary.
2734 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002735 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002736 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002737 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002738}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002739
Chris Wilson6cde9a02017-02-13 17:15:50 +00002740int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2741{
2742 struct i915_ggtt *ggtt = &i915->ggtt;
2743 struct i915_hw_ppgtt *ppgtt;
2744 int err;
2745
2746 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2747 if (!ppgtt)
2748 return -ENOMEM;
2749
2750 err = __hw_ppgtt_init(ppgtt, i915);
2751 if (err)
2752 goto err_ppgtt;
2753
2754 if (ppgtt->base.allocate_va_range) {
2755 err = ppgtt->base.allocate_va_range(&ppgtt->base,
2756 0, ppgtt->base.total);
2757 if (err)
2758 goto err_ppgtt_cleanup;
2759 }
2760
2761 ppgtt->base.clear_range(&ppgtt->base,
2762 ppgtt->base.start,
2763 ppgtt->base.total);
2764
2765 i915->mm.aliasing_ppgtt = ppgtt;
2766 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2767 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2768
2769 return 0;
2770
2771err_ppgtt_cleanup:
2772 ppgtt->base.cleanup(&ppgtt->base);
2773err_ppgtt:
2774 kfree(ppgtt);
2775 return err;
2776}
2777
2778void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2779{
2780 struct i915_ggtt *ggtt = &i915->ggtt;
2781 struct i915_hw_ppgtt *ppgtt;
2782
2783 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2784 if (!ppgtt)
2785 return;
2786
2787 ppgtt->base.cleanup(&ppgtt->base);
2788 kfree(ppgtt);
2789
2790 ggtt->base.bind_vma = ggtt_bind_vma;
2791}
2792
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002793int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002794{
Ben Widawskye78891c2013-01-25 16:41:04 -08002795 /* Let GEM Manage all of the aperture.
2796 *
2797 * However, leave one page at the end still bound to the scratch page.
2798 * There are a number of places where the hardware apparently prefetches
2799 * past the end of the object, and we've seen multiple hangs with the
2800 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2801 * aperture. One page should be enough to keep any prefetching inside
2802 * of the aperture.
2803 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002804 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002805 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002806 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002807 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002808
Zhi Wangb02d22a2016-06-16 08:06:59 -04002809 ret = intel_vgt_balloon(dev_priv);
2810 if (ret)
2811 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002812
Chris Wilson95374d72016-10-12 10:05:20 +01002813 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002814 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2815 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2816 0, ggtt->mappable_end,
2817 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002818 if (ret)
2819 return ret;
2820
Chris Wilsoned2f3452012-11-15 11:32:19 +00002821 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002822 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002823 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2824 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002825 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002826 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002827 }
2828
2829 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002830 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002831 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002832
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002833 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Chris Wilson6cde9a02017-02-13 17:15:50 +00002834 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
Chris Wilson95374d72016-10-12 10:05:20 +01002835 if (ret)
Chris Wilson6cde9a02017-02-13 17:15:50 +00002836 goto err;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002837 }
2838
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002839 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002840
Chris Wilson95374d72016-10-12 10:05:20 +01002841err:
2842 drm_mm_remove_node(&ggtt->error_capture);
2843 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002844}
2845
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002846/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002847 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002848 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002849 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002850void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002851{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002852 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002853 struct i915_vma *vma, *vn;
2854
2855 ggtt->base.closed = true;
2856
2857 mutex_lock(&dev_priv->drm.struct_mutex);
2858 WARN_ON(!list_empty(&ggtt->base.active_list));
2859 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2860 WARN_ON(i915_vma_unbind(vma));
2861 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002862
Chris Wilson6cde9a02017-02-13 17:15:50 +00002863 i915_gem_fini_aliasing_ppgtt(dev_priv);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002864 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002865
Chris Wilson95374d72016-10-12 10:05:20 +01002866 if (drm_mm_node_allocated(&ggtt->error_capture))
2867 drm_mm_remove_node(&ggtt->error_capture);
2868
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002869 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002870 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002871
Matthew Aulded9724d2016-11-17 21:04:10 +00002872 mutex_lock(&dev_priv->drm.struct_mutex);
2873 i915_address_space_fini(&ggtt->base);
2874 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002875 }
2876
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002877 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002878
2879 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002880 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002881}
Daniel Vetter70e32542014-08-06 15:04:57 +02002882
Daniel Vetter2c642b02015-04-14 17:35:26 +02002883static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002884{
2885 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2886 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2887 return snb_gmch_ctl << 20;
2888}
2889
Daniel Vetter2c642b02015-04-14 17:35:26 +02002890static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002891{
2892 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2893 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2894 if (bdw_gmch_ctl)
2895 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002896
2897#ifdef CONFIG_X86_32
2898 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2899 if (bdw_gmch_ctl > 4)
2900 bdw_gmch_ctl = 4;
2901#endif
2902
Ben Widawsky9459d252013-11-03 16:53:55 -08002903 return bdw_gmch_ctl << 20;
2904}
2905
Daniel Vetter2c642b02015-04-14 17:35:26 +02002906static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002907{
2908 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2909 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2910
2911 if (gmch_ctrl)
2912 return 1 << (20 + gmch_ctrl);
2913
2914 return 0;
2915}
2916
Daniel Vetter2c642b02015-04-14 17:35:26 +02002917static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002918{
2919 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2920 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2921 return snb_gmch_ctl << 25; /* 32 MB units */
2922}
2923
Daniel Vetter2c642b02015-04-14 17:35:26 +02002924static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002925{
2926 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2927 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2928 return bdw_gmch_ctl << 25; /* 32 MB units */
2929}
2930
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002931static size_t chv_get_stolen_size(u16 gmch_ctrl)
2932{
2933 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2934 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2935
2936 /*
2937 * 0x0 to 0x10: 32MB increments starting at 0MB
2938 * 0x11 to 0x16: 4MB increments starting at 8MB
2939 * 0x17 to 0x1d: 4MB increments start at 36MB
2940 */
2941 if (gmch_ctrl < 0x11)
2942 return gmch_ctrl << 25;
2943 else if (gmch_ctrl < 0x17)
2944 return (gmch_ctrl - 0x11 + 2) << 22;
2945 else
2946 return (gmch_ctrl - 0x17 + 9) << 22;
2947}
2948
Damien Lespiau66375012014-01-09 18:02:46 +00002949static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2950{
2951 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2952 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2953
2954 if (gen9_gmch_ctl < 0xf0)
2955 return gen9_gmch_ctl << 25; /* 32 MB units */
2956 else
2957 /* 4MB increments starting at 0xf0 for 4MB */
2958 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2959}
2960
Chris Wilson34c998b2016-08-04 07:52:24 +01002961static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002962{
Chris Wilson49d73912016-11-29 09:50:08 +00002963 struct drm_i915_private *dev_priv = ggtt->base.i915;
2964 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002965 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002966 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002967
2968 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002969 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002970
Imre Deak2a073f892015-03-27 13:07:33 +02002971 /*
2972 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2973 * dropped. For WC mappings in general we have 64 byte burst writes
2974 * when the WC buffer is flushed, so we can't use it, but have to
2975 * resort to an uncached mapping. The WC issue is easily caught by the
2976 * readback check when writing GTT PTE entries.
2977 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002978 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002979 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002980 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002981 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002982 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002983 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002984 return -ENOMEM;
2985 }
2986
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002987 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002988 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002989 DRM_ERROR("Scratch setup failed\n");
2990 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002991 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002992 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002993 }
2994
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002995 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002996}
2997
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002998/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2999 * bits. When using advanced contexts each context stores its own PAT, but
3000 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003001static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003002{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003003 uint64_t pat;
3004
3005 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
3006 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
3007 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
3008 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3009 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3010 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3011 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3012 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3013
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03003014 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08003015 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3016 * so RTL will always use the value corresponding to
3017 * pat_sel = 000".
3018 * So let's disable cache for GGTT to avoid screen corruptions.
3019 * MOCS still can be used though.
3020 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3021 * before this patch, i.e. the same uncached + snooping access
3022 * like on gen6/7 seems to be in effect.
3023 * - So this just fixes blitter/render access. Again it looks
3024 * like it's not just uncached access, but uncached + snooping.
3025 * So we can still hold onto all our assumptions wrt cpu
3026 * clflushing on LLC machines.
3027 */
3028 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3029
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003030 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3031 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003032 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3033 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003034}
3035
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003036static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3037{
3038 uint64_t pat;
3039
3040 /*
3041 * Map WB on BDW to snooped on CHV.
3042 *
3043 * Only the snoop bit has meaning for CHV, the rest is
3044 * ignored.
3045 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003046 * The hardware will never snoop for certain types of accesses:
3047 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3048 * - PPGTT page tables
3049 * - some other special cycles
3050 *
3051 * As with BDW, we also need to consider the following for GT accesses:
3052 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3053 * so RTL will always use the value corresponding to
3054 * pat_sel = 000".
3055 * Which means we must set the snoop bit in PAT entry 0
3056 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003057 */
3058 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3059 GEN8_PPAT(1, 0) |
3060 GEN8_PPAT(2, 0) |
3061 GEN8_PPAT(3, 0) |
3062 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3063 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3064 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3065 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3066
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003067 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3068 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003069}
3070
Chris Wilson34c998b2016-08-04 07:52:24 +01003071static void gen6_gmch_remove(struct i915_address_space *vm)
3072{
3073 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3074
3075 iounmap(ggtt->gsm);
Chris Wilson49d73912016-11-29 09:50:08 +00003076 cleanup_scratch_page(vm->i915, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003077}
3078
Joonas Lahtinend507d732016-03-18 10:42:58 +02003079static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003080{
Chris Wilson49d73912016-11-29 09:50:08 +00003081 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003082 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003083 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003084 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003085
3086 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003087 ggtt->mappable_base = pci_resource_start(pdev, 2);
3088 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003089
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003090 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3091 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003092
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003093 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003094
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003095 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003096 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003097 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003098 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003099 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003100 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003101 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003102 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003103 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003104 }
Ben Widawsky63340132013-11-04 19:32:22 -08003105
Chris Wilson34c998b2016-08-04 07:52:24 +01003106 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003107
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003108 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003109 chv_setup_private_ppat(dev_priv);
3110 else
3111 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003112
Chris Wilson34c998b2016-08-04 07:52:24 +01003113 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003114 ggtt->base.bind_vma = ggtt_bind_vma;
3115 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303116 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003117 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003118 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003119 ggtt->base.clear_range = gen8_ggtt_clear_range;
3120
3121 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3122 if (IS_CHERRYVIEW(dev_priv))
3123 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3124
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003125 ggtt->invalidate = gen6_ggtt_invalidate;
3126
Chris Wilson34c998b2016-08-04 07:52:24 +01003127 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003128}
3129
Joonas Lahtinend507d732016-03-18 10:42:58 +02003130static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003131{
Chris Wilson49d73912016-11-29 09:50:08 +00003132 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003133 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003134 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003135 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003136
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003137 ggtt->mappable_base = pci_resource_start(pdev, 2);
3138 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003139
Ben Widawskybaa09f52013-01-24 13:49:57 -08003140 /* 64/512MB is the current min/max we actually know of, but this is just
3141 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003142 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003143 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003144 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003145 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003146 }
3147
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003148 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3149 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3150 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003151
Joonas Lahtinend507d732016-03-18 10:42:58 +02003152 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003153
Chris Wilson34c998b2016-08-04 07:52:24 +01003154 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3155 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003156
Joonas Lahtinend507d732016-03-18 10:42:58 +02003157 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303158 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003159 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3160 ggtt->base.bind_vma = ggtt_bind_vma;
3161 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003162 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003163
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003164 ggtt->invalidate = gen6_ggtt_invalidate;
3165
Chris Wilson34c998b2016-08-04 07:52:24 +01003166 if (HAS_EDRAM(dev_priv))
3167 ggtt->base.pte_encode = iris_pte_encode;
3168 else if (IS_HASWELL(dev_priv))
3169 ggtt->base.pte_encode = hsw_pte_encode;
3170 else if (IS_VALLEYVIEW(dev_priv))
3171 ggtt->base.pte_encode = byt_pte_encode;
3172 else if (INTEL_GEN(dev_priv) >= 7)
3173 ggtt->base.pte_encode = ivb_pte_encode;
3174 else
3175 ggtt->base.pte_encode = snb_pte_encode;
3176
3177 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003178}
3179
Chris Wilson34c998b2016-08-04 07:52:24 +01003180static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003181{
Chris Wilson34c998b2016-08-04 07:52:24 +01003182 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003183}
3184
Joonas Lahtinend507d732016-03-18 10:42:58 +02003185static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003186{
Chris Wilson49d73912016-11-29 09:50:08 +00003187 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003188 int ret;
3189
Chris Wilson91c8a322016-07-05 10:40:23 +01003190 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003191 if (!ret) {
3192 DRM_ERROR("failed to set up gmch\n");
3193 return -EIO;
3194 }
3195
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003196 intel_gtt_get(&ggtt->base.total,
3197 &ggtt->stolen_size,
3198 &ggtt->mappable_base,
3199 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003200
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003201 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303202 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003203 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3204 ggtt->base.clear_range = i915_ggtt_clear_range;
3205 ggtt->base.bind_vma = ggtt_bind_vma;
3206 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003207 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003208
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003209 ggtt->invalidate = gmch_ggtt_invalidate;
3210
Joonas Lahtinend507d732016-03-18 10:42:58 +02003211 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003212 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3213
Ben Widawskybaa09f52013-01-24 13:49:57 -08003214 return 0;
3215}
3216
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003217/**
Chris Wilson0088e522016-08-04 07:52:21 +01003218 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003219 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003220 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003221int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003222{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003223 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003224 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003225
Chris Wilson49d73912016-11-29 09:50:08 +00003226 ggtt->base.i915 = dev_priv;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003227
Chris Wilson34c998b2016-08-04 07:52:24 +01003228 if (INTEL_GEN(dev_priv) <= 5)
3229 ret = i915_gmch_probe(ggtt);
3230 else if (INTEL_GEN(dev_priv) < 8)
3231 ret = gen6_gmch_probe(ggtt);
3232 else
3233 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003234 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003235 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003236
Chris Wilsondb9309a2017-01-05 15:30:23 +00003237 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3238 * This is easier than doing range restriction on the fly, as we
3239 * currently don't have any bits spare to pass in this upper
3240 * restriction!
3241 */
3242 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3243 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3244 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3245 }
3246
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003247 if ((ggtt->base.total - 1) >> 32) {
3248 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003249 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003250 ggtt->base.total >> 20);
3251 ggtt->base.total = 1ULL << 32;
3252 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3253 }
3254
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003255 if (ggtt->mappable_end > ggtt->base.total) {
3256 DRM_ERROR("mappable aperture extends past end of GGTT,"
3257 " aperture=%llx, total=%llx\n",
3258 ggtt->mappable_end, ggtt->base.total);
3259 ggtt->mappable_end = ggtt->base.total;
3260 }
3261
Ben Widawskybaa09f52013-01-24 13:49:57 -08003262 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003263 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003264 ggtt->base.total >> 20);
3265 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003266 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003267#ifdef CONFIG_INTEL_IOMMU
3268 if (intel_iommu_gfx_mapped)
3269 DRM_INFO("VT-d active for gfx access\n");
3270#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003271
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003272 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003273}
3274
3275/**
3276 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003277 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003278 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003279int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003280{
Chris Wilson0088e522016-08-04 07:52:21 +01003281 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3282 int ret;
3283
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003284 INIT_LIST_HEAD(&dev_priv->vm_list);
3285
Chris Wilsona6508de2017-02-06 08:45:47 +00003286 /* Note that we use page colouring to enforce a guard page at the
3287 * end of the address space. This is required as the CS may prefetch
3288 * beyond the end of the batch buffer, across the page boundary,
3289 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003290 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003291 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01003292 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00003293 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003294 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003295 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003296
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003297 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3298 dev_priv->ggtt.mappable_base,
3299 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003300 ret = -EIO;
3301 goto out_gtt_cleanup;
3302 }
3303
3304 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3305
Chris Wilson0088e522016-08-04 07:52:21 +01003306 /*
3307 * Initialise stolen early so that we may reserve preallocated
3308 * objects for the BIOS to KMS transition.
3309 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003310 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003311 if (ret)
3312 goto out_gtt_cleanup;
3313
3314 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003315
3316out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003317 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003318 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003319}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003320
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003321int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003322{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003323 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003324 return -EIO;
3325
3326 return 0;
3327}
3328
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003329void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3330{
3331 i915->ggtt.invalidate = guc_ggtt_invalidate;
3332}
3333
3334void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3335{
3336 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3337}
3338
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003339void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003340{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003341 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003342 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003343
Chris Wilsondc979972016-05-10 14:10:04 +01003344 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003345
3346 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003347 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003348
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003349 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3350
3351 /* clflush objects bound into the GGTT and rebind them. */
3352 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003353 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003354 bool ggtt_bound = false;
3355 struct i915_vma *vma;
3356
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003357 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003358 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003359 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003360
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003361 if (!i915_vma_unbind(vma))
3362 continue;
3363
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003364 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3365 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003366 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003367 }
3368
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003369 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003370 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003371 }
3372
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003373 ggtt->base.closed = false;
3374
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003375 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003376 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003377 chv_setup_private_ppat(dev_priv);
3378 else
3379 bdw_setup_private_ppat(dev_priv);
3380
3381 return;
3382 }
3383
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003384 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003385 struct i915_address_space *vm;
3386
Daniel Vetterfa423312015-04-14 17:35:23 +02003387 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3388 /* TODO: Perhaps it shouldn't be gen6 specific */
3389
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003390 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003391
Chris Wilson2bfa9962016-08-04 07:52:25 +01003392 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003393 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003394 else
3395 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003396
3397 gen6_write_page_range(dev_priv, &ppgtt->pd,
3398 0, ppgtt->base.total);
3399 }
3400 }
3401
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003402 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003403}
3404
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003405static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003406rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003407 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003408 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003409 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003410{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003411 unsigned int column, row;
3412 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003413
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003414 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003415 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003416 for (row = 0; row < height; row++) {
3417 st->nents++;
3418 /* We don't need the pages, but need to initialize
3419 * the entries so the sg list can be happily traversed.
3420 * The only thing we need are DMA addresses.
3421 */
3422 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003423 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003424 sg_dma_len(sg) = PAGE_SIZE;
3425 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003426 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003427 }
3428 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003429
3430 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003431}
3432
3433static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003434intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003435 struct drm_i915_gem_object *obj)
3436{
Dave Gordon85d12252016-05-20 11:54:06 +01003437 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003438 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003439 struct sgt_iter sgt_iter;
3440 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003441 unsigned long i;
3442 dma_addr_t *page_addr_list;
3443 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003444 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003445 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003446
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003447 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003448 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003449 sizeof(dma_addr_t),
3450 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003451 if (!page_addr_list)
3452 return ERR_PTR(ret);
3453
3454 /* Allocate target SG list. */
3455 st = kmalloc(sizeof(*st), GFP_KERNEL);
3456 if (!st)
3457 goto err_st_alloc;
3458
Ville Syrjälä6687c902015-09-15 13:16:41 +03003459 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003460 if (ret)
3461 goto err_sg_alloc;
3462
3463 /* Populate source page list from the object. */
3464 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003465 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003466 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003467
Dave Gordon85d12252016-05-20 11:54:06 +01003468 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003469 st->nents = 0;
3470 sg = st->sgl;
3471
Ville Syrjälä6687c902015-09-15 13:16:41 +03003472 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3473 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3474 rot_info->plane[i].width, rot_info->plane[i].height,
3475 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003476 }
3477
Ville Syrjälä6687c902015-09-15 13:16:41 +03003478 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3479 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003480
3481 drm_free_large(page_addr_list);
3482
3483 return st;
3484
3485err_sg_alloc:
3486 kfree(st);
3487err_st_alloc:
3488 drm_free_large(page_addr_list);
3489
Ville Syrjälä6687c902015-09-15 13:16:41 +03003490 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3491 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3492
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003493 return ERR_PTR(ret);
3494}
3495
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003496static struct sg_table *
3497intel_partial_pages(const struct i915_ggtt_view *view,
3498 struct drm_i915_gem_object *obj)
3499{
3500 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003501 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003502 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003503 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003504 int ret = -ENOMEM;
3505
3506 st = kmalloc(sizeof(*st), GFP_KERNEL);
3507 if (!st)
3508 goto err_st_alloc;
3509
Chris Wilsond2a84a72016-10-28 13:58:34 +01003510 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003511 if (ret)
3512 goto err_sg_alloc;
3513
Chris Wilson8bab11932017-01-14 00:28:25 +00003514 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003515 GEM_BUG_ON(!iter);
3516
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003517 sg = st->sgl;
3518 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003519 do {
3520 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003521
Chris Wilsond2a84a72016-10-28 13:58:34 +01003522 len = min(iter->length - (offset << PAGE_SHIFT),
3523 count << PAGE_SHIFT);
3524 sg_set_page(sg, NULL, len, 0);
3525 sg_dma_address(sg) =
3526 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3527 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003528
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003529 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003530 count -= len >> PAGE_SHIFT;
3531 if (count == 0) {
3532 sg_mark_end(sg);
3533 return st;
3534 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003535
Chris Wilsond2a84a72016-10-28 13:58:34 +01003536 sg = __sg_next(sg);
3537 iter = __sg_next(iter);
3538 offset = 0;
3539 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003540
3541err_sg_alloc:
3542 kfree(st);
3543err_st_alloc:
3544 return ERR_PTR(ret);
3545}
3546
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003547static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003548i915_get_ggtt_vma_pages(struct i915_vma *vma)
3549{
3550 int ret = 0;
3551
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003552 /* The vma->pages are only valid within the lifespan of the borrowed
3553 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3554 * must be the vma->pages. A simple rule is that vma->pages must only
3555 * be accessed when the obj->mm.pages are pinned.
3556 */
3557 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3558
Chris Wilson247177d2016-08-15 10:48:47 +01003559 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003560 return 0;
3561
3562 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003563 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003564 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003565 vma->pages =
Chris Wilson8bab11932017-01-14 00:28:25 +00003566 intel_rotate_fb_obj_pages(&vma->ggtt_view.rotated,
3567 vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003568 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003569 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003570 else
3571 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3572 vma->ggtt_view.type);
3573
Chris Wilson247177d2016-08-15 10:48:47 +01003574 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003575 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003576 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003577 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003578 } else if (IS_ERR(vma->pages)) {
3579 ret = PTR_ERR(vma->pages);
3580 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003581 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3582 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003583 }
3584
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003585 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003586}
3587
Chris Wilsone007b192017-01-11 11:23:10 +00003588/**
Chris Wilson625d9882017-01-11 11:23:11 +00003589 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003590 * @vm: the &struct i915_address_space
3591 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3592 * @size: how much space to allocate inside the GTT,
3593 * must be #I915_GTT_PAGE_SIZE aligned
3594 * @offset: where to insert inside the GTT,
3595 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3596 * (@offset + @size) must fit within the address space
3597 * @color: color to apply to node, if this node is not from a VMA,
3598 * color must be #I915_COLOR_UNEVICTABLE
3599 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003600 *
3601 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3602 * the address space (using @size and @color). If the @node does not fit, it
3603 * tries to evict any overlapping nodes from the GTT, including any
3604 * neighbouring nodes if the colors do not match (to ensure guard pages between
3605 * differing domains). See i915_gem_evict_for_node() for the gory details
3606 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3607 * evicting active overlapping objects, and any overlapping node that is pinned
3608 * or marked as unevictable will also result in failure.
3609 *
3610 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3611 * asked to wait for eviction and interrupted.
3612 */
3613int i915_gem_gtt_reserve(struct i915_address_space *vm,
3614 struct drm_mm_node *node,
3615 u64 size, u64 offset, unsigned long color,
3616 unsigned int flags)
3617{
3618 int err;
3619
3620 GEM_BUG_ON(!size);
3621 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3622 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3623 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003624 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003625 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003626
3627 node->size = size;
3628 node->start = offset;
3629 node->color = color;
3630
3631 err = drm_mm_reserve_node(&vm->mm, node);
3632 if (err != -ENOSPC)
3633 return err;
3634
3635 err = i915_gem_evict_for_node(vm, node, flags);
3636 if (err == 0)
3637 err = drm_mm_reserve_node(&vm->mm, node);
3638
3639 return err;
3640}
3641
Chris Wilson606fec92017-01-11 11:23:12 +00003642static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3643{
3644 u64 range, addr;
3645
3646 GEM_BUG_ON(range_overflows(start, len, end));
3647 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3648
3649 range = round_down(end - len, align) - round_up(start, align);
3650 if (range) {
3651 if (sizeof(unsigned long) == sizeof(u64)) {
3652 addr = get_random_long();
3653 } else {
3654 addr = get_random_int();
3655 if (range > U32_MAX) {
3656 addr <<= 32;
3657 addr |= get_random_int();
3658 }
3659 }
3660 div64_u64_rem(addr, range, &addr);
3661 start += addr;
3662 }
3663
3664 return round_up(start, align);
3665}
3666
Chris Wilson625d9882017-01-11 11:23:11 +00003667/**
Chris Wilsone007b192017-01-11 11:23:10 +00003668 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003669 * @vm: the &struct i915_address_space
3670 * @node: the &struct drm_mm_node (typically i915_vma.node)
3671 * @size: how much space to allocate inside the GTT,
3672 * must be #I915_GTT_PAGE_SIZE aligned
3673 * @alignment: required alignment of starting offset, may be 0 but
3674 * if specified, this must be a power-of-two and at least
3675 * #I915_GTT_MIN_ALIGNMENT
3676 * @color: color to apply to node
3677 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003678 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003679 * @end: end of any range restriction inside GTT (U64_MAX for all),
3680 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3681 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003682 *
3683 * i915_gem_gtt_insert() first searches for an available hole into which
3684 * is can insert the node. The hole address is aligned to @alignment and
3685 * its @size must then fit entirely within the [@start, @end] bounds. The
3686 * nodes on either side of the hole must match @color, or else a guard page
3687 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003688 * suitable hole is found, first a victim is randomly selected and tested
3689 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003690 * is scanned to find the first set of replacement nodes to create the hole.
3691 * Those old overlapping nodes are evicted from the GTT (and so must be
3692 * rebound before any future use). Any node that is currently pinned cannot
3693 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3694 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3695 * searching for an eviction candidate. See i915_gem_evict_something() for
3696 * the gory details on the eviction algorithm.
3697 *
3698 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3699 * asked to wait for eviction and interrupted.
3700 */
3701int i915_gem_gtt_insert(struct i915_address_space *vm,
3702 struct drm_mm_node *node,
3703 u64 size, u64 alignment, unsigned long color,
3704 u64 start, u64 end, unsigned int flags)
3705{
Chris Wilson4e64e552017-02-02 21:04:38 +00003706 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003707 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003708 int err;
3709
3710 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3711 GEM_BUG_ON(!size);
3712 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3713 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3714 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3715 GEM_BUG_ON(start >= end);
3716 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3717 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003718 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003719 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003720
3721 if (unlikely(range_overflows(start, size, end)))
3722 return -ENOSPC;
3723
3724 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3725 return -ENOSPC;
3726
Chris Wilson4e64e552017-02-02 21:04:38 +00003727 mode = DRM_MM_INSERT_BEST;
3728 if (flags & PIN_HIGH)
3729 mode = DRM_MM_INSERT_HIGH;
3730 if (flags & PIN_MAPPABLE)
3731 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003732
3733 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3734 * so we know that we always have a minimum alignment of 4096.
3735 * The drm_mm range manager is optimised to return results
3736 * with zero alignment, so where possible use the optimal
3737 * path.
3738 */
3739 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3740 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3741 alignment = 0;
3742
Chris Wilson4e64e552017-02-02 21:04:38 +00003743 err = drm_mm_insert_node_in_range(&vm->mm, node,
3744 size, alignment, color,
3745 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003746 if (err != -ENOSPC)
3747 return err;
3748
Chris Wilson606fec92017-01-11 11:23:12 +00003749 /* No free space, pick a slot at random.
3750 *
3751 * There is a pathological case here using a GTT shared between
3752 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3753 *
3754 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3755 * (64k objects) (448k objects)
3756 *
3757 * Now imagine that the eviction LRU is ordered top-down (just because
3758 * pathology meets real life), and that we need to evict an object to
3759 * make room inside the aperture. The eviction scan then has to walk
3760 * the 448k list before it finds one within range. And now imagine that
3761 * it has to search for a new hole between every byte inside the memcpy,
3762 * for several simultaneous clients.
3763 *
3764 * On a full-ppgtt system, if we have run out of available space, there
3765 * will be lots and lots of objects in the eviction list! Again,
3766 * searching that LRU list may be slow if we are also applying any
3767 * range restrictions (e.g. restriction to low 4GiB) and so, for
3768 * simplicity and similarilty between different GTT, try the single
3769 * random replacement first.
3770 */
3771 offset = random_offset(start, end,
3772 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3773 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3774 if (err != -ENOSPC)
3775 return err;
3776
3777 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003778 err = i915_gem_evict_something(vm, size, alignment, color,
3779 start, end, flags);
3780 if (err)
3781 return err;
3782
Chris Wilson4e64e552017-02-02 21:04:38 +00003783 return drm_mm_insert_node_in_range(&vm->mm, node,
3784 size, alignment, color,
3785 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003786}
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003787
3788#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3789#include "selftests/mock_gtt.c"
Chris Wilson1c428192017-02-13 17:15:38 +00003790#include "selftests/i915_gem_gtt.c"
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003791#endif