blob: eebbffdb9a0b5e48ae4e95294ca359409a661057 [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{
Chris Wilson7e37f882016-08-02 22:50:21 +0100689 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000690 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800691 int ret;
692
693 BUG_ON(entry >= 4);
694
John Harrison5fb9de12015-05-29 17:44:07 +0100695 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800696 if (ret)
697 return ret;
698
Chris Wilsonb5321f32016-08-02 22:50:18 +0100699 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
700 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
701 intel_ring_emit(ring, upper_32_bits(addr));
702 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
703 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
704 intel_ring_emit(ring, lower_32_bits(addr));
705 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800706
707 return 0;
708}
709
Michel Thierry2dba3232015-07-30 11:06:23 +0100710static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
711 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800712{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800713 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800714
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100715 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300716 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
717
John Harrisone85b26d2015-05-29 17:43:56 +0100718 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800719 if (ret)
720 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800721 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800722
Ben Widawskyeeb94882013-12-06 14:11:10 -0800723 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800724}
725
Michel Thierry2dba3232015-07-30 11:06:23 +0100726static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
727 struct drm_i915_gem_request *req)
728{
729 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
730}
731
Mika Kuoppalafce93752016-10-31 17:24:46 +0200732/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
733 * the page table structures, we mark them dirty so that
734 * context switching/execlist queuing code takes extra steps
735 * to ensure that tlbs are flushed.
736 */
737static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
738{
Chris Wilson49d73912016-11-29 09:50:08 +0000739 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200740}
741
Michał Winiarski2ce51792016-10-13 14:02:42 +0200742/* Removes entries from a single page table, releasing it if it's empty.
743 * Caller can use the return value to update higher-level entries.
744 */
745static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200746 struct i915_page_table *pt,
747 uint64_t start,
748 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700749{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300750 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200751 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200752 unsigned int pte = gen8_pte_index(start);
753 unsigned int pte_end = pte + num_entries;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200754 gen8_pte_t *pt_vaddr;
755 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
756 I915_CACHE_LLC);
757
758 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200759 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700760
Mika Kuoppala37c63932016-11-01 15:27:36 +0200761 GEM_BUG_ON(pte_end > GEN8_PTES);
762
763 bitmap_clear(pt->used_ptes, pte, num_entries);
Zhi Wange81ecb52017-02-08 21:03:33 +0800764 if (USES_FULL_PPGTT(vm->i915)) {
765 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
766 return true;
767 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200768
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200769 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000770
Mika Kuoppala37c63932016-11-01 15:27:36 +0200771 while (pte < pte_end)
772 pt_vaddr[pte++] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000773
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200774 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200775
776 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200777}
778
Michał Winiarski2ce51792016-10-13 14:02:42 +0200779/* Removes entries from a single page dir, releasing it if it's empty.
780 * Caller can use the return value to update higher-level entries
781 */
782static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200783 struct i915_page_directory *pd,
784 uint64_t start,
785 uint64_t length)
786{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200787 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200788 struct i915_page_table *pt;
789 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200790 gen8_pde_t *pde_vaddr;
791 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
792 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200793
794 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000795 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100796 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000797
Michał Winiarski2ce51792016-10-13 14:02:42 +0200798 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
799 __clear_bit(pde, pd->used_pdes);
800 pde_vaddr = kmap_px(pd);
801 pde_vaddr[pde] = scratch_pde;
802 kunmap_px(ppgtt, pde_vaddr);
Chris Wilson49d73912016-11-29 09:50:08 +0000803 free_pt(vm->i915, pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200804 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200805 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200806
Zhi Wanga18dbba2016-11-29 14:55:16 +0800807 if (bitmap_empty(pd->used_pdes, I915_PDES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200808 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200809
810 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200811}
Ben Widawsky06fda602015-02-24 16:22:36 +0000812
Michał Winiarski2ce51792016-10-13 14:02:42 +0200813/* Removes entries from a single page dir pointer, releasing it if it's empty.
814 * Caller can use the return value to update higher-level entries
815 */
816static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200817 struct i915_page_directory_pointer *pdp,
818 uint64_t start,
819 uint64_t length)
820{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200821 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200822 struct i915_page_directory *pd;
823 uint64_t pdpe;
824
825 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
826 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100827 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000828
Michał Winiarski2ce51792016-10-13 14:02:42 +0200829 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
830 __clear_bit(pdpe, pdp->used_pdpes);
Matthew Auld9e65a372016-12-13 16:05:12 +0000831 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
Chris Wilson49d73912016-11-29 09:50:08 +0000832 free_pd(vm->i915, pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200833 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200834 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200835
Mika Kuoppalafce93752016-10-31 17:24:46 +0200836 mark_tlbs_dirty(ppgtt);
837
Zhi Wanga18dbba2016-11-29 14:55:16 +0800838 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200839 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200840
841 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200842}
Ben Widawsky459108b2013-11-02 21:07:23 -0700843
Michał Winiarski2ce51792016-10-13 14:02:42 +0200844/* Removes entries from a single pml4.
845 * This is the top-level structure in 4-level page tables used on gen8+.
846 * Empty entries are always scratch pml4e.
847 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200848static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
849 struct i915_pml4 *pml4,
850 uint64_t start,
851 uint64_t length)
852{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200853 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200854 struct i915_page_directory_pointer *pdp;
855 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200856
Chris Wilson49d73912016-11-29 09:50:08 +0000857 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700858
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200859 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
860 if (WARN_ON(!pml4->pdps[pml4e]))
861 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700862
Michał Winiarski2ce51792016-10-13 14:02:42 +0200863 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
864 __clear_bit(pml4e, pml4->used_pml4es);
Matthew Auld9e65a372016-12-13 16:05:12 +0000865 gen8_setup_pml4e(ppgtt, pml4, vm->scratch_pdp, pml4e);
Chris Wilson49d73912016-11-29 09:50:08 +0000866 free_pdp(vm->i915, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200867 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700868 }
869}
870
Michel Thierryf9b5b782015-07-30 11:02:49 +0100871static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200872 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700873{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300874 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100875
Chris Wilsonc6385c92016-11-29 12:42:05 +0000876 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200877 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
878 else
879 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100880}
881
882static void
883gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
884 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100885 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100886 uint64_t start,
887 enum i915_cache_level cache_level)
888{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300889 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000890 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100891 unsigned pdpe = gen8_pdpe_index(start);
892 unsigned pde = gen8_pde_index(start);
893 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700894
Chris Wilson6f1cc992013-12-31 15:50:31 +0000895 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700896
Michel Thierry3387d432015-08-03 09:52:47 +0100897 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000898 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100899 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100900 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300901 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000902 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800903
904 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100905 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200906 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000907 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300908 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000909 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000910 if (++pde == I915_PDES) {
Chris Wilsonc6385c92016-11-29 12:42:05 +0000911 if (++pdpe == I915_PDPES_PER_PDP(vm->i915))
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100912 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800913 pde = 0;
914 }
915 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700916 }
917 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300918
919 if (pt_vaddr)
920 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700921}
922
Michel Thierryf9b5b782015-07-30 11:02:49 +0100923static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
924 struct sg_table *pages,
925 uint64_t start,
926 enum i915_cache_level cache_level,
927 u32 unused)
928{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300929 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100930 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100931
Michel Thierry3387d432015-08-03 09:52:47 +0100932 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100933
Chris Wilsonc6385c92016-11-29 12:42:05 +0000934 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100935 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
936 cache_level);
937 } else {
938 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000939 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100940 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
941
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000942 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100943 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
944 start, cache_level);
945 }
946 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100947}
948
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000949static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
Michel Thierryf37c0502015-06-10 17:46:39 +0100950 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800951{
952 int i;
953
Mika Kuoppala567047b2015-06-25 18:35:12 +0300954 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800955 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800956
Michel Thierry33c88192015-04-08 12:13:33 +0100957 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000958 if (WARN_ON(!pd->page_table[i]))
959 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800960
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000961 free_pt(dev_priv, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000962 pd->page_table[i] = NULL;
963 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000964}
965
Mika Kuoppala8776f022015-06-30 18:16:40 +0300966static int gen8_init_scratch(struct i915_address_space *vm)
967{
Chris Wilson49d73912016-11-29 09:50:08 +0000968 struct drm_i915_private *dev_priv = vm->i915;
Matthew Auld64c050d2016-04-27 13:19:25 +0100969 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300970
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000971 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100972 if (ret)
973 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300974
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000975 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300976 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100977 ret = PTR_ERR(vm->scratch_pt);
978 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300979 }
980
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000981 vm->scratch_pd = alloc_pd(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300982 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100983 ret = PTR_ERR(vm->scratch_pd);
984 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300985 }
986
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000987 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
988 vm->scratch_pdp = alloc_pdp(dev_priv);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100989 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100990 ret = PTR_ERR(vm->scratch_pdp);
991 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100992 }
993 }
994
Mika Kuoppala8776f022015-06-30 18:16:40 +0300995 gen8_initialize_pt(vm, vm->scratch_pt);
996 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000997 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100998 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300999
1000 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +01001001
1002free_pd:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001003 free_pd(dev_priv, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +01001004free_pt:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001005 free_pt(dev_priv, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +01001006free_scratch_page:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001007 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +01001008
1009 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001010}
1011
Zhiyuan Lv650da342015-08-28 15:41:18 +08001012static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1013{
1014 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +00001015 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001016 int i;
1017
Matthew Aulddf285642016-04-22 12:09:25 +01001018 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001019 u64 daddr = px_dma(&ppgtt->pml4);
1020
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001021 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1022 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001023
1024 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1025 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1026 } else {
1027 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1028 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1029
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001030 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1031 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001032 }
1033
1034 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1035 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1036 }
1037
1038 I915_WRITE(vgtif_reg(g2v_notify), msg);
1039
1040 return 0;
1041}
1042
Mika Kuoppala8776f022015-06-30 18:16:40 +03001043static void gen8_free_scratch(struct i915_address_space *vm)
1044{
Chris Wilson49d73912016-11-29 09:50:08 +00001045 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001046
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001047 if (USES_FULL_48BIT_PPGTT(dev_priv))
1048 free_pdp(dev_priv, vm->scratch_pdp);
1049 free_pd(dev_priv, vm->scratch_pd);
1050 free_pt(dev_priv, vm->scratch_pt);
1051 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001052}
1053
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001054static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
Michel Thierry762d9932015-07-30 11:05:29 +01001055 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001056{
1057 int i;
1058
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001059 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001060 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001061 continue;
1062
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001063 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1064 free_pd(dev_priv, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001065 }
Michel Thierry69876be2015-04-08 12:13:27 +01001066
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001067 free_pdp(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001068}
1069
1070static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1071{
Chris Wilson49d73912016-11-29 09:50:08 +00001072 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001073 int i;
1074
1075 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1076 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1077 continue;
1078
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001079 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001080 }
1081
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001082 cleanup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001083}
1084
1085static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1086{
Chris Wilson49d73912016-11-29 09:50:08 +00001087 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001088 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001089
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001090 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001091 gen8_ppgtt_notify_vgt(ppgtt, false);
1092
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001093 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1094 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001095 else
1096 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001097
Mika Kuoppala8776f022015-06-30 18:16:40 +03001098 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001099}
1100
Michel Thierryd7b26332015-04-08 12:13:34 +01001101/**
1102 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001103 * @vm: Master vm structure.
1104 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001105 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001106 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001107 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1108 * caller to free on error.
1109 *
1110 * Allocate the required number of page tables. Extremely similar to
1111 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1112 * the page directory boundary (instead of the page directory pointer). That
1113 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1114 * possible, and likely that the caller will need to use multiple calls of this
1115 * function to achieve the appropriate allocation.
1116 *
1117 * Return: 0 if success; negative error code otherwise.
1118 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001119static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001120 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001121 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001122 uint64_t length,
1123 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001124{
Chris Wilson49d73912016-11-29 09:50:08 +00001125 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001126 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001127 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001128
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001129 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001130 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001131 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001132 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001133 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001134 continue;
1135 }
1136
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001137 pt = alloc_pt(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001138 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001139 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001140
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001141 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001142 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001143 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001144 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001145 }
1146
1147 return 0;
1148
1149unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001150 for_each_set_bit(pde, new_pts, I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001151 free_pt(dev_priv, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001152
1153 return -ENOMEM;
1154}
1155
Michel Thierryd7b26332015-04-08 12:13:34 +01001156/**
1157 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001158 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001159 * @pdp: Page directory pointer for this address range.
1160 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001161 * @length: Size of the allocations.
1162 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001163 * caller to free on error.
1164 *
1165 * Allocate the required number of page directories starting at the pde index of
1166 * @start, and ending at the pde index @start + @length. This function will skip
1167 * over already allocated page directories within the range, and only allocate
1168 * new ones, setting the appropriate pointer within the pdp as well as the
1169 * correct position in the bitmap @new_pds.
1170 *
1171 * The function will only allocate the pages within the range for a give page
1172 * directory pointer. In other words, if @start + @length straddles a virtually
1173 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1174 * required by the caller, This is not currently possible, and the BUG in the
1175 * code will prevent it.
1176 *
1177 * Return: 0 if success; negative error code otherwise.
1178 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001179static int
1180gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1181 struct i915_page_directory_pointer *pdp,
1182 uint64_t start,
1183 uint64_t length,
1184 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001185{
Chris Wilson49d73912016-11-29 09:50:08 +00001186 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001187 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001188 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001189 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001190
Michel Thierry6ac18502015-07-29 17:23:46 +01001191 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001192
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001193 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001194 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001195 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001196
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001197 pd = alloc_pd(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001198 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001199 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001200
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001201 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001202 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001203 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001204 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001205 }
1206
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001207 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001208
1209unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001210 for_each_set_bit(pdpe, new_pds, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001211 free_pd(dev_priv, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001212
1213 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001214}
1215
Michel Thierry762d9932015-07-30 11:05:29 +01001216/**
1217 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1218 * @vm: Master vm structure.
1219 * @pml4: Page map level 4 for this address range.
1220 * @start: Starting virtual address to begin allocations.
1221 * @length: Size of the allocations.
1222 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1223 * caller to free on error.
1224 *
1225 * Allocate the required number of page directory pointers. Extremely similar to
1226 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1227 * The main difference is here we are limited by the pml4 boundary (instead of
1228 * the page directory pointer).
1229 *
1230 * Return: 0 if success; negative error code otherwise.
1231 */
1232static int
1233gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1234 struct i915_pml4 *pml4,
1235 uint64_t start,
1236 uint64_t length,
1237 unsigned long *new_pdps)
1238{
Chris Wilson49d73912016-11-29 09:50:08 +00001239 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001240 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001241 uint32_t pml4e;
1242
1243 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1244
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001245 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001246 if (!test_bit(pml4e, pml4->used_pml4es)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001247 pdp = alloc_pdp(dev_priv);
Michel Thierry762d9932015-07-30 11:05:29 +01001248 if (IS_ERR(pdp))
1249 goto unwind_out;
1250
Michel Thierry69ab76f2015-07-29 17:23:55 +01001251 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001252 pml4->pdps[pml4e] = pdp;
1253 __set_bit(pml4e, new_pdps);
1254 trace_i915_page_directory_pointer_entry_alloc(vm,
1255 pml4e,
1256 start,
1257 GEN8_PML4E_SHIFT);
1258 }
1259 }
1260
1261 return 0;
1262
1263unwind_out:
1264 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001265 free_pdp(dev_priv, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001266
1267 return -ENOMEM;
1268}
1269
Michel Thierryd7b26332015-04-08 12:13:34 +01001270static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001271free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001272{
Michel Thierryd7b26332015-04-08 12:13:34 +01001273 kfree(new_pts);
1274 kfree(new_pds);
1275}
1276
1277/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1278 * of these are based on the number of PDPEs in the system.
1279 */
1280static
1281int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001282 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001283 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001284{
Michel Thierryd7b26332015-04-08 12:13:34 +01001285 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001286 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001287
Michał Winiarski3a41a052015-09-03 19:22:18 +02001288 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001289 if (!pds)
1290 return -ENOMEM;
1291
Michał Winiarski3a41a052015-09-03 19:22:18 +02001292 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1293 GFP_TEMPORARY);
1294 if (!pts)
1295 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001296
1297 *new_pds = pds;
1298 *new_pts = pts;
1299
1300 return 0;
1301
1302err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001303 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001304 return -ENOMEM;
1305}
1306
Michel Thierry762d9932015-07-30 11:05:29 +01001307static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1308 struct i915_page_directory_pointer *pdp,
1309 uint64_t start,
1310 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001311{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001312 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001313 unsigned long *new_page_dirs, *new_page_tables;
Chris Wilson49d73912016-11-29 09:50:08 +00001314 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001315 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001316 const uint64_t orig_start = start;
1317 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001318 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001319 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001320 int ret;
1321
Michel Thierry6ac18502015-07-29 17:23:46 +01001322 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001323 if (ret)
1324 return ret;
1325
Michel Thierryd7b26332015-04-08 12:13:34 +01001326 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001327 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1328 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001329 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001330 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001331 return ret;
1332 }
1333
1334 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001335 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001336 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001337 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001338 if (ret)
1339 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001340 }
1341
Michel Thierry33c88192015-04-08 12:13:33 +01001342 start = orig_start;
1343 length = orig_length;
1344
Michel Thierryd7b26332015-04-08 12:13:34 +01001345 /* Allocations have completed successfully, so set the bitmaps, and do
1346 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001347 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001348 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001349 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001350 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001351 uint64_t pd_start = start;
1352 uint32_t pde;
1353
Michel Thierryd7b26332015-04-08 12:13:34 +01001354 /* Every pd should be allocated, we just did that above. */
1355 WARN_ON(!pd);
1356
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001357 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001358 /* Same reasoning as pd */
1359 WARN_ON(!pt);
1360 WARN_ON(!pd_len);
1361 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1362
1363 /* Set our used ptes within the page table */
1364 bitmap_set(pt->used_ptes,
1365 gen8_pte_index(pd_start),
1366 gen8_pte_count(pd_start, pd_len));
1367
1368 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001369 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001370
1371 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001372 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1373 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001374 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1375 gen8_pte_index(start),
1376 gen8_pte_count(start, length),
1377 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001378
1379 /* NB: We haven't yet mapped ptes to pages. At this
1380 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001381 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001382
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001383 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001384 __set_bit(pdpe, pdp->used_pdpes);
Matthew Auld5c693b22016-12-13 16:05:10 +00001385 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001386 }
1387
Michał Winiarski3a41a052015-09-03 19:22:18 +02001388 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001389 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001390 return 0;
1391
1392err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001393 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001394 unsigned long temp;
1395
Michał Winiarski3a41a052015-09-03 19:22:18 +02001396 for_each_set_bit(temp, new_page_tables + pdpe *
1397 BITS_TO_LONGS(I915_PDES), I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001398 free_pt(dev_priv,
1399 pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001400 }
1401
Michel Thierry6ac18502015-07-29 17:23:46 +01001402 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001403 free_pd(dev_priv, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001404
Michał Winiarski3a41a052015-09-03 19:22:18 +02001405 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001406 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001407 return ret;
1408}
1409
Michel Thierry762d9932015-07-30 11:05:29 +01001410static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1411 struct i915_pml4 *pml4,
1412 uint64_t start,
1413 uint64_t length)
1414{
1415 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001416 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001417 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001418 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001419 int ret = 0;
1420
1421 /* Do the pml4 allocations first, so we don't need to track the newly
1422 * allocated tables below the pdp */
1423 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1424
1425 /* The pagedirectory and pagetable allocations are done in the shared 3
1426 * and 4 level code. Just allocate the pdps.
1427 */
1428 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1429 new_pdps);
1430 if (ret)
1431 return ret;
1432
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001433 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001434 WARN_ON(!pdp);
1435
1436 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1437 if (ret)
1438 goto err_out;
1439
Matthew Auld56843102016-12-13 16:05:11 +00001440 gen8_setup_pml4e(ppgtt, pml4, pdp, pml4e);
Michel Thierry762d9932015-07-30 11:05:29 +01001441 }
1442
1443 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1444 GEN8_PML4ES_PER_PML4);
1445
1446 return 0;
1447
1448err_out:
1449 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson49d73912016-11-29 09:50:08 +00001450 gen8_ppgtt_cleanup_3lvl(vm->i915, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001451
1452 return ret;
1453}
1454
1455static int gen8_alloc_va_range(struct i915_address_space *vm,
1456 uint64_t start, uint64_t length)
1457{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001458 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001459
Chris Wilsonc6385c92016-11-29 12:42:05 +00001460 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001461 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1462 else
1463 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1464}
1465
Michel Thierryea91e402015-07-29 17:23:57 +01001466static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1467 uint64_t start, uint64_t length,
1468 gen8_pte_t scratch_pte,
1469 struct seq_file *m)
1470{
1471 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001472 uint32_t pdpe;
1473
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001474 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001475 struct i915_page_table *pt;
1476 uint64_t pd_len = length;
1477 uint64_t pd_start = start;
1478 uint32_t pde;
1479
1480 if (!test_bit(pdpe, pdp->used_pdpes))
1481 continue;
1482
1483 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001484 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001485 uint32_t pte;
1486 gen8_pte_t *pt_vaddr;
1487
1488 if (!test_bit(pde, pd->used_pdes))
1489 continue;
1490
1491 pt_vaddr = kmap_px(pt);
1492 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1493 uint64_t va =
1494 (pdpe << GEN8_PDPE_SHIFT) |
1495 (pde << GEN8_PDE_SHIFT) |
1496 (pte << GEN8_PTE_SHIFT);
1497 int i;
1498 bool found = false;
1499
1500 for (i = 0; i < 4; i++)
1501 if (pt_vaddr[pte + i] != scratch_pte)
1502 found = true;
1503 if (!found)
1504 continue;
1505
1506 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1507 for (i = 0; i < 4; i++) {
1508 if (pt_vaddr[pte + i] != scratch_pte)
1509 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1510 else
1511 seq_puts(m, " SCRATCH ");
1512 }
1513 seq_puts(m, "\n");
1514 }
1515 /* don't use kunmap_px, it could trigger
1516 * an unnecessary flush.
1517 */
1518 kunmap_atomic(pt_vaddr);
1519 }
1520 }
1521}
1522
1523static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1524{
1525 struct i915_address_space *vm = &ppgtt->base;
1526 uint64_t start = ppgtt->base.start;
1527 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001528 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001529 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001530
Chris Wilsonc6385c92016-11-29 12:42:05 +00001531 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryea91e402015-07-29 17:23:57 +01001532 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1533 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001534 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001535 struct i915_pml4 *pml4 = &ppgtt->pml4;
1536 struct i915_page_directory_pointer *pdp;
1537
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001538 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001539 if (!test_bit(pml4e, pml4->used_pml4es))
1540 continue;
1541
1542 seq_printf(m, " PML4E #%llu\n", pml4e);
1543 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1544 }
1545 }
1546}
1547
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001548static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1549{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001550 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001551 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001552 int ret;
1553
1554 /* We allocate temp bitmap for page tables for no gain
1555 * but as this is for init only, lets keep the things simple
1556 */
1557 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1558 if (ret)
1559 return ret;
1560
1561 /* Allocate for all pdps regardless of how the ppgtt
1562 * was defined.
1563 */
1564 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1565 0, 1ULL << 32,
1566 new_page_dirs);
1567 if (!ret)
1568 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1569
Michał Winiarski3a41a052015-09-03 19:22:18 +02001570 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001571
1572 return ret;
1573}
1574
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001575/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001576 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1577 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1578 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1579 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001580 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001581 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001582static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001583{
Chris Wilson49d73912016-11-29 09:50:08 +00001584 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001585 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001586
Mika Kuoppala8776f022015-06-30 18:16:40 +03001587 ret = gen8_init_scratch(&ppgtt->base);
1588 if (ret)
1589 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001590
Michel Thierryd7b26332015-04-08 12:13:34 +01001591 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001592 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001593 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001594 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001595 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001596 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1597 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001598 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001599
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001600 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1601 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001602 if (ret)
1603 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001604
Michel Thierry69ab76f2015-07-29 17:23:55 +01001605 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1606
Michel Thierry762d9932015-07-30 11:05:29 +01001607 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001608 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001609 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001610 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001611 if (ret)
1612 goto free_scratch;
1613
1614 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001615 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001616 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1617 0, 0,
1618 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001619
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001620 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001621 ret = gen8_preallocate_top_level_pdps(ppgtt);
1622 if (ret)
1623 goto free_scratch;
1624 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001625 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001626
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001627 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001628 gen8_ppgtt_notify_vgt(ppgtt, true);
1629
Michel Thierryd7b26332015-04-08 12:13:34 +01001630 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001631
1632free_scratch:
1633 gen8_free_scratch(&ppgtt->base);
1634 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001635}
1636
Ben Widawsky87d60b62013-12-06 14:11:29 -08001637static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1638{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001639 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001640 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001641 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001642 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001643 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001644 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001645
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001646 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001647 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001648
Dave Gordon731f74c2016-06-24 19:37:46 +01001649 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001650 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001651 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001652 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001653 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001654 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1655
1656 if (pd_entry != expected)
1657 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1658 pde,
1659 pd_entry,
1660 expected);
1661 seq_printf(m, "\tPDE: %x\n", pd_entry);
1662
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001663 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1664
Michel Thierry07749ef2015-03-16 16:00:54 +00001665 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001666 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001667 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001668 (pte * PAGE_SIZE);
1669 int i;
1670 bool found = false;
1671 for (i = 0; i < 4; i++)
1672 if (pt_vaddr[pte + i] != scratch_pte)
1673 found = true;
1674 if (!found)
1675 continue;
1676
1677 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1678 for (i = 0; i < 4; i++) {
1679 if (pt_vaddr[pte + i] != scratch_pte)
1680 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1681 else
1682 seq_puts(m, " SCRATCH ");
1683 }
1684 seq_puts(m, "\n");
1685 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001686 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001687 }
1688}
1689
Ben Widawsky678d96f2015-03-16 16:00:56 +00001690/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001691static void gen6_write_pde(struct i915_page_directory *pd,
1692 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001693{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001694 /* Caller needs to make sure the write completes if necessary */
1695 struct i915_hw_ppgtt *ppgtt =
1696 container_of(pd, struct i915_hw_ppgtt, pd);
1697 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001698
Mika Kuoppala567047b2015-06-25 18:35:12 +03001699 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001700 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001701
Ben Widawsky678d96f2015-03-16 16:00:56 +00001702 writel(pd_entry, ppgtt->pd_addr + pde);
1703}
Ben Widawsky61973492013-04-08 18:43:54 -07001704
Ben Widawsky678d96f2015-03-16 16:00:56 +00001705/* Write all the page tables found in the ppgtt structure to incrementing page
1706 * directories. */
1707static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001708 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001709 uint32_t start, uint32_t length)
1710{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001711 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001712 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001713 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001714
Dave Gordon731f74c2016-06-24 19:37:46 +01001715 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001716 gen6_write_pde(pd, pde, pt);
1717
1718 /* Make sure write is complete before other code can use this page
1719 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001720 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001721}
1722
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001723static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001724{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001725 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001726
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001727 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001728}
Ben Widawsky61973492013-04-08 18:43:54 -07001729
Ben Widawsky90252e52013-12-06 14:11:12 -08001730static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001731 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001732{
Chris Wilson7e37f882016-08-02 22:50:21 +01001733 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001734 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001735 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001736
Ben Widawsky90252e52013-12-06 14:11:12 -08001737 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001738 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001739 if (ret)
1740 return ret;
1741
John Harrison5fb9de12015-05-29 17:44:07 +01001742 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001743 if (ret)
1744 return ret;
1745
Chris Wilsonb5321f32016-08-02 22:50:18 +01001746 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1747 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1748 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1749 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1750 intel_ring_emit(ring, get_pd_offset(ppgtt));
1751 intel_ring_emit(ring, MI_NOOP);
1752 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001753
1754 return 0;
1755}
1756
Ben Widawsky48a10382013-12-06 14:11:11 -08001757static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001758 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001759{
Chris Wilson7e37f882016-08-02 22:50:21 +01001760 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001761 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001762 int ret;
1763
Ben Widawsky48a10382013-12-06 14:11:11 -08001764 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001765 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001766 if (ret)
1767 return ret;
1768
John Harrison5fb9de12015-05-29 17:44:07 +01001769 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001770 if (ret)
1771 return ret;
1772
Chris Wilsonb5321f32016-08-02 22:50:18 +01001773 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1774 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1775 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1776 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1777 intel_ring_emit(ring, get_pd_offset(ppgtt));
1778 intel_ring_emit(ring, MI_NOOP);
1779 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001780
Ben Widawsky90252e52013-12-06 14:11:12 -08001781 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001782 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001783 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001784 if (ret)
1785 return ret;
1786 }
1787
Ben Widawsky48a10382013-12-06 14:11:11 -08001788 return 0;
1789}
1790
Ben Widawskyeeb94882013-12-06 14:11:10 -08001791static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001792 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001793{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001794 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001795 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001796
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001797 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1798 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001799 return 0;
1800}
1801
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001802static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001803{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001804 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301805 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001806
Akash Goel3b3f1652016-10-13 22:44:48 +05301807 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001808 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1809 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001810 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001811 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001812 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001813}
1814
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001815static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001816{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001817 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001818 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301819 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001820
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001821 ecobits = I915_READ(GAC_ECO_BITS);
1822 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1823
1824 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001825 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001826 ecochk |= ECOCHK_PPGTT_WB_HSW;
1827 } else {
1828 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1829 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1830 }
1831 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001832
Akash Goel3b3f1652016-10-13 22:44:48 +05301833 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001834 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001835 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001836 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001837 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001838}
1839
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001840static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001841{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001842 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001843
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001844 ecobits = I915_READ(GAC_ECO_BITS);
1845 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1846 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001847
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001848 gab_ctl = I915_READ(GAB_CTL);
1849 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001850
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001851 ecochk = I915_READ(GAM_ECOCHK);
1852 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001853
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001854 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001855}
1856
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001857/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001858static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001859 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001860 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001861{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001862 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001863 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001864 unsigned first_entry = start >> PAGE_SHIFT;
1865 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001866 unsigned act_pt = first_entry / GEN6_PTES;
1867 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001868 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001869
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001870 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001871 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001872
Daniel Vetter7bddb012012-02-09 17:15:47 +01001873 while (num_entries) {
1874 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001875 if (last_pte > GEN6_PTES)
1876 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001877
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001878 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001879
1880 for (i = first_pte; i < last_pte; i++)
1881 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001882
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001883 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001884
Daniel Vetter7bddb012012-02-09 17:15:47 +01001885 num_entries -= last_pte - first_pte;
1886 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001887 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001888 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001889}
1890
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001891static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001892 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001893 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301894 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001895{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001896 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001897 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001898 unsigned act_pt = first_entry / GEN6_PTES;
1899 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001900 gen6_pte_t *pt_vaddr = NULL;
1901 struct sgt_iter sgt_iter;
1902 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001903
Dave Gordon85d12252016-05-20 11:54:06 +01001904 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001905 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001906 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001907
Chris Wilsoncc797142013-12-31 15:50:30 +00001908 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001909 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301910
Michel Thierry07749ef2015-03-16 16:00:54 +00001911 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001912 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001913 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001914 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001915 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001916 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001917 }
Dave Gordon85d12252016-05-20 11:54:06 +01001918
Chris Wilsoncc797142013-12-31 15:50:30 +00001919 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001920 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001921}
1922
Ben Widawsky678d96f2015-03-16 16:00:56 +00001923static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001924 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001925{
Michel Thierry4933d512015-03-24 15:46:22 +00001926 DECLARE_BITMAP(new_page_tables, I915_PDES);
Chris Wilson49d73912016-11-29 09:50:08 +00001927 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001928 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001929 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001930 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001931 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001932 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001933 int ret;
1934
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001935 start = start_save = start_in;
1936 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001937
1938 bitmap_zero(new_page_tables, I915_PDES);
1939
1940 /* The allocation is done in two stages so that we can bail out with
1941 * minimal amount of pain. The first stage finds new page tables that
1942 * need allocation. The second stage marks use ptes within the page
1943 * tables.
1944 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001945 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001946 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001947 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1948 continue;
1949 }
1950
1951 /* We've already allocated a page table */
1952 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1953
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001954 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001955 if (IS_ERR(pt)) {
1956 ret = PTR_ERR(pt);
1957 goto unwind_out;
1958 }
1959
1960 gen6_initialize_pt(vm, pt);
1961
1962 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001963 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001964 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001965 }
1966
1967 start = start_save;
1968 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001969
Dave Gordon731f74c2016-06-24 19:37:46 +01001970 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001971 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1972
1973 bitmap_zero(tmp_bitmap, GEN6_PTES);
1974 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1975 gen6_pte_count(start, length));
1976
Mika Kuoppala966082c2015-06-25 18:35:19 +03001977 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001978 gen6_write_pde(&ppgtt->pd, pde, pt);
1979
Michel Thierry72744cb2015-03-24 15:46:23 +00001980 trace_i915_page_table_entry_map(vm, pde, pt,
1981 gen6_pte_index(start),
1982 gen6_pte_count(start, length),
1983 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001984 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001985 GEN6_PTES);
1986 }
1987
Michel Thierry4933d512015-03-24 15:46:22 +00001988 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1989
1990 /* Make sure write is complete before other code can use this page
1991 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001992 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001993
Ben Widawsky563222a2015-03-19 12:53:28 +00001994 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001995 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001996
1997unwind_out:
1998 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001999 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002000
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002001 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002002 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002003 }
2004
2005 mark_tlbs_dirty(ppgtt);
2006 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002007}
2008
Mika Kuoppala8776f022015-06-30 18:16:40 +03002009static int gen6_init_scratch(struct i915_address_space *vm)
2010{
Chris Wilson49d73912016-11-29 09:50:08 +00002011 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002012 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002013
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002014 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002015 if (ret)
2016 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002017
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002018 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002019 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002020 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002021 return PTR_ERR(vm->scratch_pt);
2022 }
2023
2024 gen6_initialize_pt(vm, vm->scratch_pt);
2025
2026 return 0;
2027}
2028
2029static void gen6_free_scratch(struct i915_address_space *vm)
2030{
Chris Wilson49d73912016-11-29 09:50:08 +00002031 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002032
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002033 free_pt(dev_priv, vm->scratch_pt);
2034 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002035}
2036
Daniel Vetter061dd492015-04-14 17:35:13 +02002037static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002038{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002039 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002040 struct i915_page_directory *pd = &ppgtt->pd;
Chris Wilson49d73912016-11-29 09:50:08 +00002041 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry09942c62015-04-08 12:13:30 +01002042 struct i915_page_table *pt;
2043 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002044
Daniel Vetter061dd492015-04-14 17:35:13 +02002045 drm_mm_remove_node(&ppgtt->node);
2046
Dave Gordon731f74c2016-06-24 19:37:46 +01002047 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002048 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002049 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002050
Mika Kuoppala8776f022015-06-30 18:16:40 +03002051 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002052}
2053
Ben Widawskyb1465202014-02-19 22:05:49 -08002054static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002055{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002056 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00002057 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002058 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002059 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002060
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002061 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2062 * allocator works in address space sizes, so it's multiplied by page
2063 * size. We allocate at the top of the GTT to avoid fragmentation.
2064 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002065 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002066
Mika Kuoppala8776f022015-06-30 18:16:40 +03002067 ret = gen6_init_scratch(vm);
2068 if (ret)
2069 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002070
Chris Wilsone007b192017-01-11 11:23:10 +00002071 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2072 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2073 I915_COLOR_UNEVICTABLE,
2074 0, ggtt->base.total,
2075 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00002076 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002077 goto err_out;
2078
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002079 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002080 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002081
Ben Widawskyc8c26622015-01-22 17:01:25 +00002082 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002083
2084err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002085 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002086 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002087}
2088
Ben Widawskyb1465202014-02-19 22:05:49 -08002089static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2090{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002091 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002092}
2093
Michel Thierry4933d512015-03-24 15:46:22 +00002094static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2095 uint64_t start, uint64_t length)
2096{
Michel Thierryec565b32015-04-08 12:13:23 +01002097 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002098 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002099
Dave Gordon731f74c2016-06-24 19:37:46 +01002100 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002101 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002102}
2103
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002104static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002105{
Chris Wilson49d73912016-11-29 09:50:08 +00002106 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002107 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002108 int ret;
2109
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002110 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002111 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002112 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002113 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002114 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002115 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002116 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002117 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002118 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002119
2120 ret = gen6_ppgtt_alloc(ppgtt);
2121 if (ret)
2122 return ret;
2123
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002124 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002125 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2126 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002127 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2128 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002129 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002130 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002131 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002132 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002133
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002134 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002135 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002136
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002137 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002138 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002139
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002140 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002141
Ben Widawsky678d96f2015-03-16 16:00:56 +00002142 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2143
Thierry Reding440fd522015-01-23 09:05:06 +01002144 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002145 ppgtt->node.size >> 20,
2146 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002147
Daniel Vetterfa76da32014-08-06 20:19:54 +02002148 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002149 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002150
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002151 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002152}
2153
Chris Wilson2bfa9962016-08-04 07:52:25 +01002154static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2155 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002156{
Chris Wilson49d73912016-11-29 09:50:08 +00002157 ppgtt->base.i915 = dev_priv;
Daniel Vetter3440d262013-01-24 13:49:56 -08002158
Chris Wilson2bfa9962016-08-04 07:52:25 +01002159 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002160 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002161 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002162 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002163}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002164
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002165static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002166 struct drm_i915_private *dev_priv,
2167 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002168{
Chris Wilson80b204b2016-10-28 13:58:58 +01002169 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00002170
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002171 drm_mm_init(&vm->mm, vm->start, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00002172 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
2173
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002174 INIT_LIST_HEAD(&vm->active_list);
2175 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002176 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00002177
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002178 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2179}
2180
Matthew Aulded9724d2016-11-17 21:04:10 +00002181static void i915_address_space_fini(struct i915_address_space *vm)
2182{
2183 i915_gem_timeline_fini(&vm->timeline);
2184 drm_mm_takedown(&vm->mm);
2185 list_del(&vm->global_link);
2186}
2187
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002188static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002189{
Tim Gored5165eb2016-02-04 11:49:34 +00002190 /* This function is for gtt related workarounds. This function is
2191 * called on driver load and after a GPU reset, so you can place
2192 * workarounds here even if they get overwritten by GPU reset.
2193 */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002194 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002195 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002196 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002197 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002198 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08002199 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002200 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002201 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002202 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2203}
2204
Chris Wilson2bfa9962016-08-04 07:52:25 +01002205static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2206 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002207 struct drm_i915_file_private *file_priv,
2208 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002209{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002210 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002211
Chris Wilson2bfa9962016-08-04 07:52:25 +01002212 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002213 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002214 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002215 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002216 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002217 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002218
2219 return ret;
2220}
2221
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002222int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002223{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002224 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002225
Thomas Daniel671b50132014-08-20 16:24:50 +01002226 /* In the case of execlists, PPGTT is enabled by the context descriptor
2227 * and the PDPs are contained within the context itself. We don't
2228 * need to do anything here. */
2229 if (i915.enable_execlists)
2230 return 0;
2231
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002232 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002233 return 0;
2234
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002235 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002236 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002237 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002238 gen7_ppgtt_enable(dev_priv);
2239 else if (INTEL_GEN(dev_priv) >= 8)
2240 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002241 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002242 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002243
John Harrison4ad2fd82015-06-18 13:11:20 +01002244 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002245}
John Harrison4ad2fd82015-06-18 13:11:20 +01002246
Daniel Vetter4d884702014-08-06 15:04:47 +02002247struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002248i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002249 struct drm_i915_file_private *fpriv,
2250 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002251{
2252 struct i915_hw_ppgtt *ppgtt;
2253 int ret;
2254
2255 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2256 if (!ppgtt)
2257 return ERR_PTR(-ENOMEM);
2258
Chris Wilson80b204b2016-10-28 13:58:58 +01002259 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002260 if (ret) {
2261 kfree(ppgtt);
2262 return ERR_PTR(ret);
2263 }
2264
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002265 trace_i915_ppgtt_create(&ppgtt->base);
2266
Daniel Vetter4d884702014-08-06 15:04:47 +02002267 return ppgtt;
2268}
2269
Chris Wilson0c7eeda2017-01-11 21:09:25 +00002270void i915_ppgtt_close(struct i915_address_space *vm)
2271{
2272 struct list_head *phases[] = {
2273 &vm->active_list,
2274 &vm->inactive_list,
2275 &vm->unbound_list,
2276 NULL,
2277 }, **phase;
2278
2279 GEM_BUG_ON(vm->closed);
2280 vm->closed = true;
2281
2282 for (phase = phases; *phase; phase++) {
2283 struct i915_vma *vma, *vn;
2284
2285 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2286 if (!i915_vma_is_closed(vma))
2287 i915_vma_close(vma);
2288 }
2289}
2290
Matthew Aulded9724d2016-11-17 21:04:10 +00002291void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002292{
2293 struct i915_hw_ppgtt *ppgtt =
2294 container_of(kref, struct i915_hw_ppgtt, ref);
2295
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002296 trace_i915_ppgtt_release(&ppgtt->base);
2297
Chris Wilson50e046b2016-08-04 07:52:46 +01002298 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002299 WARN_ON(!list_empty(&ppgtt->base.active_list));
2300 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002301 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002302
Matthew Aulded9724d2016-11-17 21:04:10 +00002303 i915_address_space_fini(&ppgtt->base);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002304
Daniel Vetteree960be2014-08-06 15:04:45 +02002305 ppgtt->base.cleanup(&ppgtt->base);
2306 kfree(ppgtt);
2307}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002308
Ben Widawskya81cc002013-01-18 12:30:31 -08002309/* Certain Gen5 chipsets require require idling the GPU before
2310 * unmapping anything from the GTT when VT-d is enabled.
2311 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002312static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002313{
2314#ifdef CONFIG_INTEL_IOMMU
2315 /* Query intel_iommu to see if we need the workaround. Presumably that
2316 * was loaded first.
2317 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002318 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002319 return true;
2320#endif
2321 return false;
2322}
2323
Chris Wilsondc979972016-05-10 14:10:04 +01002324void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002325{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002326 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302327 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002328
Chris Wilsondc979972016-05-10 14:10:04 +01002329 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002330 return;
2331
Akash Goel3b3f1652016-10-13 22:44:48 +05302332 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002333 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002334 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002335 if (fault_reg & RING_FAULT_VALID) {
2336 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002337 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002338 "\tAddress space: %s\n"
2339 "\tSource ID: %d\n"
2340 "\tType: %d\n",
2341 fault_reg & PAGE_MASK,
2342 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2343 RING_FAULT_SRCID(fault_reg),
2344 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002345 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002346 fault_reg & ~RING_FAULT_VALID);
2347 }
2348 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302349
2350 /* Engine specific init may not have been done till this point. */
2351 if (dev_priv->engine[RCS])
2352 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002353}
2354
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002355void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002356{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002357 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002358
2359 /* Don't bother messing with faults pre GEN6 as we have little
2360 * documentation supporting that it's a good idea.
2361 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002362 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002363 return;
2364
Chris Wilsondc979972016-05-10 14:10:04 +01002365 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002366
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002367 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002368
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002369 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002370}
2371
Chris Wilson03ac84f2016-10-28 13:58:36 +01002372int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2373 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002374{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002375 do {
2376 if (dma_map_sg(&obj->base.dev->pdev->dev,
2377 pages->sgl, pages->nents,
2378 PCI_DMA_BIDIRECTIONAL))
2379 return 0;
2380
2381 /* If the DMA remap fails, one cause can be that we have
2382 * too many objects pinned in a small remapping table,
2383 * such as swiotlb. Incrementally purge all other objects and
2384 * try again - if there are no more pages to remove from
2385 * the DMA remapper, i915_gem_shrink will return 0.
2386 */
2387 GEM_BUG_ON(obj->mm.pages == pages);
2388 } while (i915_gem_shrink(to_i915(obj->base.dev),
2389 obj->base.size >> PAGE_SHIFT,
2390 I915_SHRINK_BOUND |
2391 I915_SHRINK_UNBOUND |
2392 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002393
Chris Wilson03ac84f2016-10-28 13:58:36 +01002394 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002395}
2396
Daniel Vetter2c642b02015-04-14 17:35:26 +02002397static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002398{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002399 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002400}
2401
Chris Wilsond6473f52016-06-10 14:22:59 +05302402static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2403 dma_addr_t addr,
2404 uint64_t offset,
2405 enum i915_cache_level level,
2406 u32 unused)
2407{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002408 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302409 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002410 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302411
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002412 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302413
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002414 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302415}
2416
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002417static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2418 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002419 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302420 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002421{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002422 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002423 struct sgt_iter sgt_iter;
2424 gen8_pte_t __iomem *gtt_entries;
2425 gen8_pte_t gtt_entry;
2426 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002427 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002428
Dave Gordon85d12252016-05-20 11:54:06 +01002429 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2430
2431 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002432 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002433 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002434 }
2435
2436 /*
2437 * XXX: This serves as a posting read to make sure that the PTE has
2438 * actually been updated. There is some concern that even though
2439 * registers and PTEs are within the same BAR that they are potentially
2440 * of NUMA access patterns. Therefore, even with the way we assume
2441 * hardware should work, we must keep this posting read for paranoia.
2442 */
2443 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002444 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002445
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002446 /* This next bit makes the above posting read even more important. We
2447 * want to flush the TLBs only after we're certain all the PTE updates
2448 * have finished.
2449 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002450 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002451}
2452
Chris Wilsonc1403302015-11-18 15:19:39 +00002453struct insert_entries {
2454 struct i915_address_space *vm;
2455 struct sg_table *st;
2456 uint64_t start;
2457 enum i915_cache_level level;
2458 u32 flags;
2459};
2460
2461static int gen8_ggtt_insert_entries__cb(void *_arg)
2462{
2463 struct insert_entries *arg = _arg;
2464 gen8_ggtt_insert_entries(arg->vm, arg->st,
2465 arg->start, arg->level, arg->flags);
2466 return 0;
2467}
2468
2469static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2470 struct sg_table *st,
2471 uint64_t start,
2472 enum i915_cache_level level,
2473 u32 flags)
2474{
2475 struct insert_entries arg = { vm, st, start, level, flags };
2476 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2477}
2478
Chris Wilsond6473f52016-06-10 14:22:59 +05302479static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2480 dma_addr_t addr,
2481 uint64_t offset,
2482 enum i915_cache_level level,
2483 u32 flags)
2484{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002485 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302486 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002487 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302488
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002489 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302490
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002491 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302492}
2493
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002494/*
2495 * Binds an object into the global gtt with the specified cache level. The object
2496 * will be accessible to the GPU via commands whose operands reference offsets
2497 * within the global GTT as well as accessible by the GPU through the GMADR
2498 * mapped BAR (dev_priv->mm.gtt->gtt).
2499 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002500static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002501 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002502 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302503 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002504{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002505 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002506 struct sgt_iter sgt_iter;
2507 gen6_pte_t __iomem *gtt_entries;
2508 gen6_pte_t gtt_entry;
2509 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002510 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002511
Dave Gordon85d12252016-05-20 11:54:06 +01002512 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2513
2514 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002515 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002516 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002517 }
2518
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002519 /* XXX: This serves as a posting read to make sure that the PTE has
2520 * actually been updated. There is some concern that even though
2521 * registers and PTEs are within the same BAR that they are potentially
2522 * of NUMA access patterns. Therefore, even with the way we assume
2523 * hardware should work, we must keep this posting read for paranoia.
2524 */
Dave Gordon85d12252016-05-20 11:54:06 +01002525 if (i != 0)
2526 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002527
2528 /* This next bit makes the above posting read even more important. We
2529 * want to flush the TLBs only after we're certain all the PTE updates
2530 * have finished.
2531 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002532 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002533}
2534
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002535static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002536 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002537{
2538}
2539
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002540static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002541 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002542{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002543 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002544 unsigned first_entry = start >> PAGE_SHIFT;
2545 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002546 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002547 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2548 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002549 int i;
2550
2551 if (WARN(num_entries > max_entries,
2552 "First entry = %d; Num entries = %d (max=%d)\n",
2553 first_entry, num_entries, max_entries))
2554 num_entries = max_entries;
2555
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002556 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002557 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002558 for (i = 0; i < num_entries; i++)
2559 gen8_set_pte(&gtt_base[i], scratch_pte);
2560 readl(gtt_base);
2561}
2562
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002563static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002564 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002565 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002566{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002567 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002568 unsigned first_entry = start >> PAGE_SHIFT;
2569 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002570 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002571 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2572 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002573 int i;
2574
2575 if (WARN(num_entries > max_entries,
2576 "First entry = %d; Num entries = %d (max=%d)\n",
2577 first_entry, num_entries, max_entries))
2578 num_entries = max_entries;
2579
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002580 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002581 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002582
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002583 for (i = 0; i < num_entries; i++)
2584 iowrite32(scratch_pte, &gtt_base[i]);
2585 readl(gtt_base);
2586}
2587
Chris Wilsond6473f52016-06-10 14:22:59 +05302588static void i915_ggtt_insert_page(struct i915_address_space *vm,
2589 dma_addr_t addr,
2590 uint64_t offset,
2591 enum i915_cache_level cache_level,
2592 u32 unused)
2593{
Chris Wilsond6473f52016-06-10 14:22:59 +05302594 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2595 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302596
2597 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302598}
2599
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002600static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2601 struct sg_table *pages,
2602 uint64_t start,
2603 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002604{
2605 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2606 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2607
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002608 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002609
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002610}
2611
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002612static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002613 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002614 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002615{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002616 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002617}
2618
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002619static int ggtt_bind_vma(struct i915_vma *vma,
2620 enum i915_cache_level cache_level,
2621 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002622{
Chris Wilson49d73912016-11-29 09:50:08 +00002623 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002624 struct drm_i915_gem_object *obj = vma->obj;
2625 u32 pte_flags = 0;
2626 int ret;
2627
2628 ret = i915_get_ggtt_vma_pages(vma);
2629 if (ret)
2630 return ret;
2631
2632 /* Currently applicable only to VLV */
2633 if (obj->gt_ro)
2634 pte_flags |= PTE_READ_ONLY;
2635
Chris Wilson9c870d02016-10-24 13:42:15 +01002636 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002637 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002638 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002639 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002640
2641 /*
2642 * Without aliasing PPGTT there's no difference between
2643 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2644 * upgrade to both bound if we bind either to avoid double-binding.
2645 */
Chris Wilson3272db52016-08-04 16:32:32 +01002646 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002647
2648 return 0;
2649}
2650
2651static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2652 enum i915_cache_level cache_level,
2653 u32 flags)
2654{
Chris Wilson49d73912016-11-29 09:50:08 +00002655 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002656 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002657 int ret;
2658
2659 ret = i915_get_ggtt_vma_pages(vma);
2660 if (ret)
2661 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002662
Akash Goel24f3a8c2014-06-17 10:59:42 +05302663 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002664 pte_flags = 0;
2665 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002666 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302667
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002668
Chris Wilson3272db52016-08-04 16:32:32 +01002669 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002670 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002671 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002672 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002673 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002674 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002675 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002676
Chris Wilson3272db52016-08-04 16:32:32 +01002677 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002678 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002679 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002680 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002681 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002682 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002683
2684 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002685}
2686
2687static void ggtt_unbind_vma(struct i915_vma *vma)
2688{
Chris Wilson49d73912016-11-29 09:50:08 +00002689 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson9c870d02016-10-24 13:42:15 +01002690 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002691 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002692
Chris Wilson9c870d02016-10-24 13:42:15 +01002693 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2694 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002695 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002696 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002697 intel_runtime_pm_put(i915);
2698 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002699
Chris Wilson3272db52016-08-04 16:32:32 +01002700 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002701 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002702 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002703}
2704
Chris Wilson03ac84f2016-10-28 13:58:36 +01002705void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2706 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002707{
David Weinehall52a05c32016-08-22 13:32:44 +03002708 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2709 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002710 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002711
Chris Wilson307dc252016-08-05 10:14:12 +01002712 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002713 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002714 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2715 /* Wait a bit, in hopes it avoids the hang */
2716 udelay(10);
2717 }
2718 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002719
Chris Wilson03ac84f2016-10-28 13:58:36 +01002720 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002721}
Daniel Vetter644ec022012-03-26 09:45:40 +02002722
Chris Wilson45b186f2016-12-16 07:46:42 +00002723static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002724 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002725 u64 *start,
2726 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002727{
Chris Wilsona6508de2017-02-06 08:45:47 +00002728 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002729 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002730
Chris Wilsona6508de2017-02-06 08:45:47 +00002731 /* Also leave a space between the unallocated reserved node after the
2732 * GTT and any objects within the GTT, i.e. we use the color adjustment
2733 * to insert a guard page to prevent prefetches crossing over the
2734 * GTT boundary.
2735 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002736 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002737 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002738 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002739}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002740
Chris Wilson6cde9a02017-02-13 17:15:50 +00002741int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2742{
2743 struct i915_ggtt *ggtt = &i915->ggtt;
2744 struct i915_hw_ppgtt *ppgtt;
2745 int err;
2746
2747 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2748 if (!ppgtt)
2749 return -ENOMEM;
2750
2751 err = __hw_ppgtt_init(ppgtt, i915);
2752 if (err)
2753 goto err_ppgtt;
2754
2755 if (ppgtt->base.allocate_va_range) {
2756 err = ppgtt->base.allocate_va_range(&ppgtt->base,
2757 0, ppgtt->base.total);
2758 if (err)
2759 goto err_ppgtt_cleanup;
2760 }
2761
2762 ppgtt->base.clear_range(&ppgtt->base,
2763 ppgtt->base.start,
2764 ppgtt->base.total);
2765
2766 i915->mm.aliasing_ppgtt = ppgtt;
2767 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2768 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2769
2770 return 0;
2771
2772err_ppgtt_cleanup:
2773 ppgtt->base.cleanup(&ppgtt->base);
2774err_ppgtt:
2775 kfree(ppgtt);
2776 return err;
2777}
2778
2779void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2780{
2781 struct i915_ggtt *ggtt = &i915->ggtt;
2782 struct i915_hw_ppgtt *ppgtt;
2783
2784 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2785 if (!ppgtt)
2786 return;
2787
2788 ppgtt->base.cleanup(&ppgtt->base);
2789 kfree(ppgtt);
2790
2791 ggtt->base.bind_vma = ggtt_bind_vma;
2792}
2793
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002794int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002795{
Ben Widawskye78891c2013-01-25 16:41:04 -08002796 /* Let GEM Manage all of the aperture.
2797 *
2798 * However, leave one page at the end still bound to the scratch page.
2799 * There are a number of places where the hardware apparently prefetches
2800 * past the end of the object, and we've seen multiple hangs with the
2801 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2802 * aperture. One page should be enough to keep any prefetching inside
2803 * of the aperture.
2804 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002805 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002806 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002807 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002808 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002809
Zhi Wangb02d22a2016-06-16 08:06:59 -04002810 ret = intel_vgt_balloon(dev_priv);
2811 if (ret)
2812 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002813
Chris Wilson95374d72016-10-12 10:05:20 +01002814 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002815 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2816 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2817 0, ggtt->mappable_end,
2818 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002819 if (ret)
2820 return ret;
2821
Chris Wilsoned2f3452012-11-15 11:32:19 +00002822 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002823 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002824 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2825 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002826 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002827 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002828 }
2829
2830 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002831 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002832 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002833
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002834 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Chris Wilson6cde9a02017-02-13 17:15:50 +00002835 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
Chris Wilson95374d72016-10-12 10:05:20 +01002836 if (ret)
Chris Wilson6cde9a02017-02-13 17:15:50 +00002837 goto err;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002838 }
2839
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002840 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002841
Chris Wilson95374d72016-10-12 10:05:20 +01002842err:
2843 drm_mm_remove_node(&ggtt->error_capture);
2844 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002845}
2846
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002847/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002848 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002849 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002850 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002851void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002852{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002853 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002854 struct i915_vma *vma, *vn;
2855
2856 ggtt->base.closed = true;
2857
2858 mutex_lock(&dev_priv->drm.struct_mutex);
2859 WARN_ON(!list_empty(&ggtt->base.active_list));
2860 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2861 WARN_ON(i915_vma_unbind(vma));
2862 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002863
Chris Wilson6cde9a02017-02-13 17:15:50 +00002864 i915_gem_fini_aliasing_ppgtt(dev_priv);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002865 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002866
Chris Wilson95374d72016-10-12 10:05:20 +01002867 if (drm_mm_node_allocated(&ggtt->error_capture))
2868 drm_mm_remove_node(&ggtt->error_capture);
2869
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002870 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002871 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002872
Matthew Aulded9724d2016-11-17 21:04:10 +00002873 mutex_lock(&dev_priv->drm.struct_mutex);
2874 i915_address_space_fini(&ggtt->base);
2875 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002876 }
2877
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002878 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002879
2880 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002881 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002882}
Daniel Vetter70e32542014-08-06 15:04:57 +02002883
Daniel Vetter2c642b02015-04-14 17:35:26 +02002884static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002885{
2886 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2887 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2888 return snb_gmch_ctl << 20;
2889}
2890
Daniel Vetter2c642b02015-04-14 17:35:26 +02002891static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002892{
2893 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2894 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2895 if (bdw_gmch_ctl)
2896 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002897
2898#ifdef CONFIG_X86_32
2899 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2900 if (bdw_gmch_ctl > 4)
2901 bdw_gmch_ctl = 4;
2902#endif
2903
Ben Widawsky9459d252013-11-03 16:53:55 -08002904 return bdw_gmch_ctl << 20;
2905}
2906
Daniel Vetter2c642b02015-04-14 17:35:26 +02002907static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002908{
2909 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2910 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2911
2912 if (gmch_ctrl)
2913 return 1 << (20 + gmch_ctrl);
2914
2915 return 0;
2916}
2917
Daniel Vetter2c642b02015-04-14 17:35:26 +02002918static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002919{
2920 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2921 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2922 return snb_gmch_ctl << 25; /* 32 MB units */
2923}
2924
Daniel Vetter2c642b02015-04-14 17:35:26 +02002925static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002926{
2927 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2928 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2929 return bdw_gmch_ctl << 25; /* 32 MB units */
2930}
2931
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002932static size_t chv_get_stolen_size(u16 gmch_ctrl)
2933{
2934 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2935 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2936
2937 /*
2938 * 0x0 to 0x10: 32MB increments starting at 0MB
2939 * 0x11 to 0x16: 4MB increments starting at 8MB
2940 * 0x17 to 0x1d: 4MB increments start at 36MB
2941 */
2942 if (gmch_ctrl < 0x11)
2943 return gmch_ctrl << 25;
2944 else if (gmch_ctrl < 0x17)
2945 return (gmch_ctrl - 0x11 + 2) << 22;
2946 else
2947 return (gmch_ctrl - 0x17 + 9) << 22;
2948}
2949
Damien Lespiau66375012014-01-09 18:02:46 +00002950static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2951{
2952 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2953 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2954
2955 if (gen9_gmch_ctl < 0xf0)
2956 return gen9_gmch_ctl << 25; /* 32 MB units */
2957 else
2958 /* 4MB increments starting at 0xf0 for 4MB */
2959 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2960}
2961
Chris Wilson34c998b2016-08-04 07:52:24 +01002962static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002963{
Chris Wilson49d73912016-11-29 09:50:08 +00002964 struct drm_i915_private *dev_priv = ggtt->base.i915;
2965 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002966 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002967 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002968
2969 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002970 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002971
Imre Deak2a073f892015-03-27 13:07:33 +02002972 /*
2973 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2974 * dropped. For WC mappings in general we have 64 byte burst writes
2975 * when the WC buffer is flushed, so we can't use it, but have to
2976 * resort to an uncached mapping. The WC issue is easily caught by the
2977 * readback check when writing GTT PTE entries.
2978 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002979 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002980 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002981 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002982 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002983 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002984 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002985 return -ENOMEM;
2986 }
2987
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002988 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002989 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002990 DRM_ERROR("Scratch setup failed\n");
2991 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002992 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002993 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002994 }
2995
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002996 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002997}
2998
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002999/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3000 * bits. When using advanced contexts each context stores its own PAT, but
3001 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003002static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003003{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003004 uint64_t pat;
3005
3006 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
3007 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
3008 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
3009 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3010 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3011 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3012 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3013 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3014
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03003015 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08003016 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3017 * so RTL will always use the value corresponding to
3018 * pat_sel = 000".
3019 * So let's disable cache for GGTT to avoid screen corruptions.
3020 * MOCS still can be used though.
3021 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3022 * before this patch, i.e. the same uncached + snooping access
3023 * like on gen6/7 seems to be in effect.
3024 * - So this just fixes blitter/render access. Again it looks
3025 * like it's not just uncached access, but uncached + snooping.
3026 * So we can still hold onto all our assumptions wrt cpu
3027 * clflushing on LLC machines.
3028 */
3029 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3030
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003031 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3032 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003033 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3034 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003035}
3036
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003037static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3038{
3039 uint64_t pat;
3040
3041 /*
3042 * Map WB on BDW to snooped on CHV.
3043 *
3044 * Only the snoop bit has meaning for CHV, the rest is
3045 * ignored.
3046 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003047 * The hardware will never snoop for certain types of accesses:
3048 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3049 * - PPGTT page tables
3050 * - some other special cycles
3051 *
3052 * As with BDW, we also need to consider the following for GT accesses:
3053 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3054 * so RTL will always use the value corresponding to
3055 * pat_sel = 000".
3056 * Which means we must set the snoop bit in PAT entry 0
3057 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003058 */
3059 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3060 GEN8_PPAT(1, 0) |
3061 GEN8_PPAT(2, 0) |
3062 GEN8_PPAT(3, 0) |
3063 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3064 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3065 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3066 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3067
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003068 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3069 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003070}
3071
Chris Wilson34c998b2016-08-04 07:52:24 +01003072static void gen6_gmch_remove(struct i915_address_space *vm)
3073{
3074 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3075
3076 iounmap(ggtt->gsm);
Chris Wilson49d73912016-11-29 09:50:08 +00003077 cleanup_scratch_page(vm->i915, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003078}
3079
Joonas Lahtinend507d732016-03-18 10:42:58 +02003080static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003081{
Chris Wilson49d73912016-11-29 09:50:08 +00003082 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003083 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003084 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003085 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003086
3087 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003088 ggtt->mappable_base = pci_resource_start(pdev, 2);
3089 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003090
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003091 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3092 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003093
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003094 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003095
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003096 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003097 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003098 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003099 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003100 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003101 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003102 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003103 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003104 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003105 }
Ben Widawsky63340132013-11-04 19:32:22 -08003106
Chris Wilson34c998b2016-08-04 07:52:24 +01003107 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003108
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003109 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003110 chv_setup_private_ppat(dev_priv);
3111 else
3112 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003113
Chris Wilson34c998b2016-08-04 07:52:24 +01003114 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003115 ggtt->base.bind_vma = ggtt_bind_vma;
3116 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303117 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003118 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003119 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003120 ggtt->base.clear_range = gen8_ggtt_clear_range;
3121
3122 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3123 if (IS_CHERRYVIEW(dev_priv))
3124 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3125
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003126 ggtt->invalidate = gen6_ggtt_invalidate;
3127
Chris Wilson34c998b2016-08-04 07:52:24 +01003128 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003129}
3130
Joonas Lahtinend507d732016-03-18 10:42:58 +02003131static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003132{
Chris Wilson49d73912016-11-29 09:50:08 +00003133 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003134 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003135 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003136 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003137
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003138 ggtt->mappable_base = pci_resource_start(pdev, 2);
3139 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003140
Ben Widawskybaa09f52013-01-24 13:49:57 -08003141 /* 64/512MB is the current min/max we actually know of, but this is just
3142 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003143 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003144 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003145 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003146 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003147 }
3148
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003149 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3150 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3151 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003152
Joonas Lahtinend507d732016-03-18 10:42:58 +02003153 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003154
Chris Wilson34c998b2016-08-04 07:52:24 +01003155 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3156 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003157
Joonas Lahtinend507d732016-03-18 10:42:58 +02003158 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303159 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003160 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3161 ggtt->base.bind_vma = ggtt_bind_vma;
3162 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003163 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003164
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003165 ggtt->invalidate = gen6_ggtt_invalidate;
3166
Chris Wilson34c998b2016-08-04 07:52:24 +01003167 if (HAS_EDRAM(dev_priv))
3168 ggtt->base.pte_encode = iris_pte_encode;
3169 else if (IS_HASWELL(dev_priv))
3170 ggtt->base.pte_encode = hsw_pte_encode;
3171 else if (IS_VALLEYVIEW(dev_priv))
3172 ggtt->base.pte_encode = byt_pte_encode;
3173 else if (INTEL_GEN(dev_priv) >= 7)
3174 ggtt->base.pte_encode = ivb_pte_encode;
3175 else
3176 ggtt->base.pte_encode = snb_pte_encode;
3177
3178 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003179}
3180
Chris Wilson34c998b2016-08-04 07:52:24 +01003181static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003182{
Chris Wilson34c998b2016-08-04 07:52:24 +01003183 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003184}
3185
Joonas Lahtinend507d732016-03-18 10:42:58 +02003186static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003187{
Chris Wilson49d73912016-11-29 09:50:08 +00003188 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003189 int ret;
3190
Chris Wilson91c8a322016-07-05 10:40:23 +01003191 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003192 if (!ret) {
3193 DRM_ERROR("failed to set up gmch\n");
3194 return -EIO;
3195 }
3196
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003197 intel_gtt_get(&ggtt->base.total,
3198 &ggtt->stolen_size,
3199 &ggtt->mappable_base,
3200 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003201
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003202 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303203 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003204 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3205 ggtt->base.clear_range = i915_ggtt_clear_range;
3206 ggtt->base.bind_vma = ggtt_bind_vma;
3207 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003208 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003209
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003210 ggtt->invalidate = gmch_ggtt_invalidate;
3211
Joonas Lahtinend507d732016-03-18 10:42:58 +02003212 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003213 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3214
Ben Widawskybaa09f52013-01-24 13:49:57 -08003215 return 0;
3216}
3217
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003218/**
Chris Wilson0088e522016-08-04 07:52:21 +01003219 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003220 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003221 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003222int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003223{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003224 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003225 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003226
Chris Wilson49d73912016-11-29 09:50:08 +00003227 ggtt->base.i915 = dev_priv;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003228
Chris Wilson34c998b2016-08-04 07:52:24 +01003229 if (INTEL_GEN(dev_priv) <= 5)
3230 ret = i915_gmch_probe(ggtt);
3231 else if (INTEL_GEN(dev_priv) < 8)
3232 ret = gen6_gmch_probe(ggtt);
3233 else
3234 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003235 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003236 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003237
Chris Wilsondb9309a2017-01-05 15:30:23 +00003238 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3239 * This is easier than doing range restriction on the fly, as we
3240 * currently don't have any bits spare to pass in this upper
3241 * restriction!
3242 */
3243 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3244 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3245 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3246 }
3247
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003248 if ((ggtt->base.total - 1) >> 32) {
3249 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003250 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003251 ggtt->base.total >> 20);
3252 ggtt->base.total = 1ULL << 32;
3253 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3254 }
3255
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003256 if (ggtt->mappable_end > ggtt->base.total) {
3257 DRM_ERROR("mappable aperture extends past end of GGTT,"
3258 " aperture=%llx, total=%llx\n",
3259 ggtt->mappable_end, ggtt->base.total);
3260 ggtt->mappable_end = ggtt->base.total;
3261 }
3262
Ben Widawskybaa09f52013-01-24 13:49:57 -08003263 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003264 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003265 ggtt->base.total >> 20);
3266 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003267 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003268#ifdef CONFIG_INTEL_IOMMU
3269 if (intel_iommu_gfx_mapped)
3270 DRM_INFO("VT-d active for gfx access\n");
3271#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003272
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003273 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003274}
3275
3276/**
3277 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003278 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003279 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003280int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003281{
Chris Wilson0088e522016-08-04 07:52:21 +01003282 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3283 int ret;
3284
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003285 INIT_LIST_HEAD(&dev_priv->vm_list);
3286
Chris Wilsona6508de2017-02-06 08:45:47 +00003287 /* Note that we use page colouring to enforce a guard page at the
3288 * end of the address space. This is required as the CS may prefetch
3289 * beyond the end of the batch buffer, across the page boundary,
3290 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003291 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003292 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01003293 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00003294 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003295 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003296 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003297
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003298 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3299 dev_priv->ggtt.mappable_base,
3300 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003301 ret = -EIO;
3302 goto out_gtt_cleanup;
3303 }
3304
3305 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3306
Chris Wilson0088e522016-08-04 07:52:21 +01003307 /*
3308 * Initialise stolen early so that we may reserve preallocated
3309 * objects for the BIOS to KMS transition.
3310 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003311 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003312 if (ret)
3313 goto out_gtt_cleanup;
3314
3315 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003316
3317out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003318 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003319 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003320}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003321
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003322int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003323{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003324 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003325 return -EIO;
3326
3327 return 0;
3328}
3329
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003330void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3331{
3332 i915->ggtt.invalidate = guc_ggtt_invalidate;
3333}
3334
3335void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3336{
3337 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3338}
3339
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003340void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003341{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003342 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003343 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003344
Chris Wilsondc979972016-05-10 14:10:04 +01003345 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003346
3347 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003348 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003349
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003350 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3351
3352 /* clflush objects bound into the GGTT and rebind them. */
3353 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003354 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003355 bool ggtt_bound = false;
3356 struct i915_vma *vma;
3357
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003358 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003359 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003360 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003361
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003362 if (!i915_vma_unbind(vma))
3363 continue;
3364
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003365 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3366 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003367 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003368 }
3369
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003370 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003371 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003372 }
3373
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003374 ggtt->base.closed = false;
3375
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003376 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003377 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003378 chv_setup_private_ppat(dev_priv);
3379 else
3380 bdw_setup_private_ppat(dev_priv);
3381
3382 return;
3383 }
3384
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003385 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003386 struct i915_address_space *vm;
3387
Daniel Vetterfa423312015-04-14 17:35:23 +02003388 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3389 /* TODO: Perhaps it shouldn't be gen6 specific */
3390
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003391 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003392
Chris Wilson2bfa9962016-08-04 07:52:25 +01003393 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003394 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003395 else
3396 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003397
3398 gen6_write_page_range(dev_priv, &ppgtt->pd,
3399 0, ppgtt->base.total);
3400 }
3401 }
3402
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003403 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003404}
3405
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003406static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003407rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003408 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003409 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003410 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003411{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003412 unsigned int column, row;
3413 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003414
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003415 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003416 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003417 for (row = 0; row < height; row++) {
3418 st->nents++;
3419 /* We don't need the pages, but need to initialize
3420 * the entries so the sg list can be happily traversed.
3421 * The only thing we need are DMA addresses.
3422 */
3423 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003424 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003425 sg_dma_len(sg) = PAGE_SIZE;
3426 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003427 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003428 }
3429 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003430
3431 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003432}
3433
3434static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003435intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003436 struct drm_i915_gem_object *obj)
3437{
Dave Gordon85d12252016-05-20 11:54:06 +01003438 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003439 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003440 struct sgt_iter sgt_iter;
3441 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003442 unsigned long i;
3443 dma_addr_t *page_addr_list;
3444 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003445 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003446 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003447
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003448 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003449 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003450 sizeof(dma_addr_t),
3451 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003452 if (!page_addr_list)
3453 return ERR_PTR(ret);
3454
3455 /* Allocate target SG list. */
3456 st = kmalloc(sizeof(*st), GFP_KERNEL);
3457 if (!st)
3458 goto err_st_alloc;
3459
Ville Syrjälä6687c902015-09-15 13:16:41 +03003460 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003461 if (ret)
3462 goto err_sg_alloc;
3463
3464 /* Populate source page list from the object. */
3465 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003466 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003467 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003468
Dave Gordon85d12252016-05-20 11:54:06 +01003469 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003470 st->nents = 0;
3471 sg = st->sgl;
3472
Ville Syrjälä6687c902015-09-15 13:16:41 +03003473 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3474 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3475 rot_info->plane[i].width, rot_info->plane[i].height,
3476 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003477 }
3478
Ville Syrjälä6687c902015-09-15 13:16:41 +03003479 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3480 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003481
3482 drm_free_large(page_addr_list);
3483
3484 return st;
3485
3486err_sg_alloc:
3487 kfree(st);
3488err_st_alloc:
3489 drm_free_large(page_addr_list);
3490
Ville Syrjälä6687c902015-09-15 13:16:41 +03003491 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3492 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3493
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003494 return ERR_PTR(ret);
3495}
3496
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003497static struct sg_table *
3498intel_partial_pages(const struct i915_ggtt_view *view,
3499 struct drm_i915_gem_object *obj)
3500{
3501 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003502 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003503 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003504 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003505 int ret = -ENOMEM;
3506
3507 st = kmalloc(sizeof(*st), GFP_KERNEL);
3508 if (!st)
3509 goto err_st_alloc;
3510
Chris Wilsond2a84a72016-10-28 13:58:34 +01003511 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003512 if (ret)
3513 goto err_sg_alloc;
3514
Chris Wilson8bab11932017-01-14 00:28:25 +00003515 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003516 GEM_BUG_ON(!iter);
3517
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003518 sg = st->sgl;
3519 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003520 do {
3521 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003522
Chris Wilsond2a84a72016-10-28 13:58:34 +01003523 len = min(iter->length - (offset << PAGE_SHIFT),
3524 count << PAGE_SHIFT);
3525 sg_set_page(sg, NULL, len, 0);
3526 sg_dma_address(sg) =
3527 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3528 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003529
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003530 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003531 count -= len >> PAGE_SHIFT;
3532 if (count == 0) {
3533 sg_mark_end(sg);
3534 return st;
3535 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003536
Chris Wilsond2a84a72016-10-28 13:58:34 +01003537 sg = __sg_next(sg);
3538 iter = __sg_next(iter);
3539 offset = 0;
3540 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003541
3542err_sg_alloc:
3543 kfree(st);
3544err_st_alloc:
3545 return ERR_PTR(ret);
3546}
3547
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003548static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003549i915_get_ggtt_vma_pages(struct i915_vma *vma)
3550{
3551 int ret = 0;
3552
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003553 /* The vma->pages are only valid within the lifespan of the borrowed
3554 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3555 * must be the vma->pages. A simple rule is that vma->pages must only
3556 * be accessed when the obj->mm.pages are pinned.
3557 */
3558 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3559
Chris Wilson247177d2016-08-15 10:48:47 +01003560 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003561 return 0;
3562
3563 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003564 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003565 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003566 vma->pages =
Chris Wilson8bab11932017-01-14 00:28:25 +00003567 intel_rotate_fb_obj_pages(&vma->ggtt_view.rotated,
3568 vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003569 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003570 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003571 else
3572 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3573 vma->ggtt_view.type);
3574
Chris Wilson247177d2016-08-15 10:48:47 +01003575 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003576 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003577 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003578 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003579 } else if (IS_ERR(vma->pages)) {
3580 ret = PTR_ERR(vma->pages);
3581 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003582 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3583 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003584 }
3585
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003586 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003587}
3588
Chris Wilsone007b192017-01-11 11:23:10 +00003589/**
Chris Wilson625d9882017-01-11 11:23:11 +00003590 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003591 * @vm: the &struct i915_address_space
3592 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3593 * @size: how much space to allocate inside the GTT,
3594 * must be #I915_GTT_PAGE_SIZE aligned
3595 * @offset: where to insert inside the GTT,
3596 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3597 * (@offset + @size) must fit within the address space
3598 * @color: color to apply to node, if this node is not from a VMA,
3599 * color must be #I915_COLOR_UNEVICTABLE
3600 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003601 *
3602 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3603 * the address space (using @size and @color). If the @node does not fit, it
3604 * tries to evict any overlapping nodes from the GTT, including any
3605 * neighbouring nodes if the colors do not match (to ensure guard pages between
3606 * differing domains). See i915_gem_evict_for_node() for the gory details
3607 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3608 * evicting active overlapping objects, and any overlapping node that is pinned
3609 * or marked as unevictable will also result in failure.
3610 *
3611 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3612 * asked to wait for eviction and interrupted.
3613 */
3614int i915_gem_gtt_reserve(struct i915_address_space *vm,
3615 struct drm_mm_node *node,
3616 u64 size, u64 offset, unsigned long color,
3617 unsigned int flags)
3618{
3619 int err;
3620
3621 GEM_BUG_ON(!size);
3622 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3623 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3624 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003625 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003626 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003627
3628 node->size = size;
3629 node->start = offset;
3630 node->color = color;
3631
3632 err = drm_mm_reserve_node(&vm->mm, node);
3633 if (err != -ENOSPC)
3634 return err;
3635
3636 err = i915_gem_evict_for_node(vm, node, flags);
3637 if (err == 0)
3638 err = drm_mm_reserve_node(&vm->mm, node);
3639
3640 return err;
3641}
3642
Chris Wilson606fec92017-01-11 11:23:12 +00003643static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3644{
3645 u64 range, addr;
3646
3647 GEM_BUG_ON(range_overflows(start, len, end));
3648 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3649
3650 range = round_down(end - len, align) - round_up(start, align);
3651 if (range) {
3652 if (sizeof(unsigned long) == sizeof(u64)) {
3653 addr = get_random_long();
3654 } else {
3655 addr = get_random_int();
3656 if (range > U32_MAX) {
3657 addr <<= 32;
3658 addr |= get_random_int();
3659 }
3660 }
3661 div64_u64_rem(addr, range, &addr);
3662 start += addr;
3663 }
3664
3665 return round_up(start, align);
3666}
3667
Chris Wilson625d9882017-01-11 11:23:11 +00003668/**
Chris Wilsone007b192017-01-11 11:23:10 +00003669 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003670 * @vm: the &struct i915_address_space
3671 * @node: the &struct drm_mm_node (typically i915_vma.node)
3672 * @size: how much space to allocate inside the GTT,
3673 * must be #I915_GTT_PAGE_SIZE aligned
3674 * @alignment: required alignment of starting offset, may be 0 but
3675 * if specified, this must be a power-of-two and at least
3676 * #I915_GTT_MIN_ALIGNMENT
3677 * @color: color to apply to node
3678 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003679 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003680 * @end: end of any range restriction inside GTT (U64_MAX for all),
3681 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3682 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003683 *
3684 * i915_gem_gtt_insert() first searches for an available hole into which
3685 * is can insert the node. The hole address is aligned to @alignment and
3686 * its @size must then fit entirely within the [@start, @end] bounds. The
3687 * nodes on either side of the hole must match @color, or else a guard page
3688 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003689 * suitable hole is found, first a victim is randomly selected and tested
3690 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003691 * is scanned to find the first set of replacement nodes to create the hole.
3692 * Those old overlapping nodes are evicted from the GTT (and so must be
3693 * rebound before any future use). Any node that is currently pinned cannot
3694 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3695 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3696 * searching for an eviction candidate. See i915_gem_evict_something() for
3697 * the gory details on the eviction algorithm.
3698 *
3699 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3700 * asked to wait for eviction and interrupted.
3701 */
3702int i915_gem_gtt_insert(struct i915_address_space *vm,
3703 struct drm_mm_node *node,
3704 u64 size, u64 alignment, unsigned long color,
3705 u64 start, u64 end, unsigned int flags)
3706{
Chris Wilson4e64e552017-02-02 21:04:38 +00003707 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003708 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003709 int err;
3710
3711 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3712 GEM_BUG_ON(!size);
3713 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3714 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3715 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3716 GEM_BUG_ON(start >= end);
3717 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3718 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003719 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003720 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003721
3722 if (unlikely(range_overflows(start, size, end)))
3723 return -ENOSPC;
3724
3725 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3726 return -ENOSPC;
3727
Chris Wilson4e64e552017-02-02 21:04:38 +00003728 mode = DRM_MM_INSERT_BEST;
3729 if (flags & PIN_HIGH)
3730 mode = DRM_MM_INSERT_HIGH;
3731 if (flags & PIN_MAPPABLE)
3732 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003733
3734 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3735 * so we know that we always have a minimum alignment of 4096.
3736 * The drm_mm range manager is optimised to return results
3737 * with zero alignment, so where possible use the optimal
3738 * path.
3739 */
3740 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3741 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3742 alignment = 0;
3743
Chris Wilson4e64e552017-02-02 21:04:38 +00003744 err = drm_mm_insert_node_in_range(&vm->mm, node,
3745 size, alignment, color,
3746 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003747 if (err != -ENOSPC)
3748 return err;
3749
Chris Wilson606fec92017-01-11 11:23:12 +00003750 /* No free space, pick a slot at random.
3751 *
3752 * There is a pathological case here using a GTT shared between
3753 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3754 *
3755 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3756 * (64k objects) (448k objects)
3757 *
3758 * Now imagine that the eviction LRU is ordered top-down (just because
3759 * pathology meets real life), and that we need to evict an object to
3760 * make room inside the aperture. The eviction scan then has to walk
3761 * the 448k list before it finds one within range. And now imagine that
3762 * it has to search for a new hole between every byte inside the memcpy,
3763 * for several simultaneous clients.
3764 *
3765 * On a full-ppgtt system, if we have run out of available space, there
3766 * will be lots and lots of objects in the eviction list! Again,
3767 * searching that LRU list may be slow if we are also applying any
3768 * range restrictions (e.g. restriction to low 4GiB) and so, for
3769 * simplicity and similarilty between different GTT, try the single
3770 * random replacement first.
3771 */
3772 offset = random_offset(start, end,
3773 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3774 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3775 if (err != -ENOSPC)
3776 return err;
3777
3778 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003779 err = i915_gem_evict_something(vm, size, alignment, color,
3780 start, end, flags);
3781 if (err)
3782 return err;
3783
Chris Wilson4e64e552017-02-02 21:04:38 +00003784 return drm_mm_insert_node_in_range(&vm->mm, node,
3785 size, alignment, color,
3786 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003787}
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003788
3789#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3790#include "selftests/mock_gtt.c"
Chris Wilson1c428192017-02-13 17:15:38 +00003791#include "selftests/i915_gem_gtt.c"
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003792#endif