blob: 6d2ff20ec973a1ecaa50682ca69df60fae3e75ee [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 Wilsone007b192017-01-11 11:23:10 +000026#include <linux/log2.h>
Chris Wilson606fec92017-01-11 11:23:12 +000027#include <linux/random.h>
Daniel Vetter0e46ce22014-01-08 16:10:27 +010028#include <linux/seq_file.h>
Chris Wilson5bab6f62015-10-23 18:43:32 +010029#include <linux/stop_machine.h>
Chris Wilsone007b192017-01-11 11:23:10 +000030
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
32#include <drm/i915_drm.h>
Chris Wilsone007b192017-01-11 11:23:10 +000033
Daniel Vetter76aaf222010-11-05 22:23:30 +010034#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080035#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010036#include "i915_trace.h"
37#include "intel_drv.h"
Chris Wilsond07f0e52016-10-28 13:58:44 +010038#include "intel_frontbuffer.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010039
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010040#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
41
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000042/**
43 * DOC: Global GTT views
44 *
45 * Background and previous state
46 *
47 * Historically objects could exists (be bound) in global GTT space only as
48 * singular instances with a view representing all of the object's backing pages
49 * in a linear fashion. This view will be called a normal view.
50 *
51 * To support multiple views of the same object, where the number of mapped
52 * pages is not equal to the backing store, or where the layout of the pages
53 * is not linear, concept of a GGTT view was added.
54 *
55 * One example of an alternative view is a stereo display driven by a single
56 * image. In this case we would have a framebuffer looking like this
57 * (2x2 pages):
58 *
59 * 12
60 * 34
61 *
62 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
63 * rendering. In contrast, fed to the display engine would be an alternative
64 * view which could look something like this:
65 *
66 * 1212
67 * 3434
68 *
69 * In this example both the size and layout of pages in the alternative view is
70 * different from the normal view.
71 *
72 * Implementation and usage
73 *
74 * GGTT views are implemented using VMAs and are distinguished via enum
75 * i915_ggtt_view_type and struct i915_ggtt_view.
76 *
77 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020078 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
79 * renaming in large amounts of code. They take the struct i915_ggtt_view
80 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000081 *
82 * As a helper for callers which are only interested in the normal view,
83 * globally const i915_ggtt_view_normal singleton instance exists. All old core
84 * GEM API functions, the ones not taking the view parameter, are operating on,
85 * or with the normal GGTT view.
86 *
87 * Code wanting to add or use a new GGTT view needs to:
88 *
89 * 1. Add a new enum with a suitable name.
90 * 2. Extend the metadata in the i915_ggtt_view structure if required.
91 * 3. Add support to i915_get_vma_pages().
92 *
93 * New views are required to build a scatter-gather table from within the
94 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
95 * exists for the lifetime of an VMA.
96 *
97 * Core API is designed to have copy semantics which means that passed in
98 * struct i915_ggtt_view does not need to be persistent (left around after
99 * calling the core API functions).
100 *
101 */
102
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200103static int
104i915_get_ggtt_vma_pages(struct i915_vma *vma);
105
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200106const struct i915_ggtt_view i915_ggtt_view_normal = {
107 .type = I915_GGTT_VIEW_NORMAL,
108};
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200109const struct i915_ggtt_view i915_ggtt_view_rotated = {
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200110 .type = I915_GGTT_VIEW_ROTATED,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200111};
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000112
Chris Wilson7c3f86b2017-01-12 11:00:49 +0000113static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
114{
115 /* Note that as an uncached mmio write, this should flush the
116 * WCB of the writes into the GGTT before it triggers the invalidate.
117 */
118 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
119}
120
121static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
122{
123 gen6_ggtt_invalidate(dev_priv);
124 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
125}
126
127static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
128{
129 intel_gtt_chipset_flush();
130}
131
132static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
133{
134 i915->ggtt.invalidate(i915);
135}
136
Chris Wilsonc0336662016-05-06 15:40:21 +0100137int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
138 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200139{
Chris Wilson1893a712014-09-19 11:56:27 +0100140 bool has_aliasing_ppgtt;
141 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100142 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100143
Michel Thierry9e1d0e62016-12-05 17:57:03 -0800144 has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
145 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
146 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100147
Zhi Wange320d402016-09-06 12:04:12 +0800148 if (intel_vgpu_active(dev_priv)) {
149 /* emulation is too hard */
150 has_full_ppgtt = false;
151 has_full_48bit_ppgtt = false;
152 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800153
Chris Wilson0e4ca102016-04-29 13:18:22 +0100154 if (!has_aliasing_ppgtt)
155 return 0;
156
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000157 /*
158 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
159 * execlists, the sole mechanism available to submit work.
160 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100161 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200162 return 0;
163
164 if (enable_ppgtt == 1)
165 return 1;
166
Chris Wilson1893a712014-09-19 11:56:27 +0100167 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200168 return 2;
169
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100170 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
171 return 3;
172
Daniel Vetter93a25a92014-03-06 09:40:43 +0100173#ifdef CONFIG_INTEL_IOMMU
174 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100175 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100176 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200177 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100178 }
179#endif
180
Jesse Barnes62942ed2014-06-13 09:28:33 -0700181 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100182 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700183 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
184 return 0;
185 }
186
Zhi Wange320d402016-09-06 12:04:12 +0800187 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100188 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000189 else
190 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100191}
192
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200193static int ppgtt_bind_vma(struct i915_vma *vma,
194 enum i915_cache_level cache_level,
195 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200196{
197 u32 pte_flags = 0;
198
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100199 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100200
Daniel Vetter47552652015-04-14 17:35:24 +0200201 /* Currently applicable only to VLV */
202 if (vma->obj->gt_ro)
203 pte_flags |= PTE_READ_ONLY;
204
Chris Wilson247177d2016-08-15 10:48:47 +0100205 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200206 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200207
208 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200209}
210
211static void ppgtt_unbind_vma(struct i915_vma *vma)
212{
213 vma->vm->clear_range(vma->vm,
214 vma->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200215 vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200216}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800217
Daniel Vetter2c642b02015-04-14 17:35:26 +0200218static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200219 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700220{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200221 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700222 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300223
224 switch (level) {
225 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800226 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300227 break;
228 case I915_CACHE_WT:
229 pte |= PPAT_DISPLAY_ELLC_INDEX;
230 break;
231 default:
232 pte |= PPAT_CACHED_INDEX;
233 break;
234 }
235
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700236 return pte;
237}
238
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300239static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
240 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800241{
Michel Thierry07749ef2015-03-16 16:00:54 +0000242 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800243 pde |= addr;
244 if (level != I915_CACHE_NONE)
245 pde |= PPAT_CACHED_PDE_INDEX;
246 else
247 pde |= PPAT_UNCACHED_INDEX;
248 return pde;
249}
250
Michel Thierry762d9932015-07-30 11:05:29 +0100251#define gen8_pdpe_encode gen8_pde_encode
252#define gen8_pml4e_encode gen8_pde_encode
253
Michel Thierry07749ef2015-03-16 16:00:54 +0000254static gen6_pte_t snb_pte_encode(dma_addr_t addr,
255 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200256 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700257{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200258 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700259 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700260
261 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100262 case I915_CACHE_L3_LLC:
263 case I915_CACHE_LLC:
264 pte |= GEN6_PTE_CACHE_LLC;
265 break;
266 case I915_CACHE_NONE:
267 pte |= GEN6_PTE_UNCACHED;
268 break;
269 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100270 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100271 }
272
273 return pte;
274}
275
Michel Thierry07749ef2015-03-16 16:00:54 +0000276static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
277 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200278 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100279{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200280 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100281 pte |= GEN6_PTE_ADDR_ENCODE(addr);
282
283 switch (level) {
284 case I915_CACHE_L3_LLC:
285 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700286 break;
287 case I915_CACHE_LLC:
288 pte |= GEN6_PTE_CACHE_LLC;
289 break;
290 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700291 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700292 break;
293 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100294 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700295 }
296
Ben Widawsky54d12522012-09-24 16:44:32 -0700297 return pte;
298}
299
Michel Thierry07749ef2015-03-16 16:00:54 +0000300static gen6_pte_t byt_pte_encode(dma_addr_t addr,
301 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200302 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700303{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200304 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700305 pte |= GEN6_PTE_ADDR_ENCODE(addr);
306
Akash Goel24f3a8c2014-06-17 10:59:42 +0530307 if (!(flags & PTE_READ_ONLY))
308 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700309
310 if (level != I915_CACHE_NONE)
311 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
312
313 return pte;
314}
315
Michel Thierry07749ef2015-03-16 16:00:54 +0000316static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
317 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200318 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700319{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200320 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700321 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700322
323 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700324 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700325
326 return pte;
327}
328
Michel Thierry07749ef2015-03-16 16:00:54 +0000329static gen6_pte_t iris_pte_encode(dma_addr_t addr,
330 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200331 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700332{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200333 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700334 pte |= HSW_PTE_ADDR_ENCODE(addr);
335
Chris Wilson651d7942013-08-08 14:41:10 +0100336 switch (level) {
337 case I915_CACHE_NONE:
338 break;
339 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000340 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100341 break;
342 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000343 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100344 break;
345 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700346
347 return pte;
348}
349
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000350static int __setup_page_dma(struct drm_i915_private *dev_priv,
Mika Kuoppalac114f762015-06-25 18:35:13 +0300351 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000352{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000353 struct device *kdev = &dev_priv->drm.pdev->dev;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000354
Mika Kuoppalac114f762015-06-25 18:35:13 +0300355 p->page = alloc_page(flags);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300356 if (!p->page)
Michel Thierry1266cdb2015-03-24 17:06:33 +0000357 return -ENOMEM;
358
David Weinehallc49d13e2016-08-22 13:32:42 +0300359 p->daddr = dma_map_page(kdev,
Chris Wilsonf51455d2017-01-10 14:47:34 +0000360 p->page, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300361
David Weinehallc49d13e2016-08-22 13:32:42 +0300362 if (dma_mapping_error(kdev, p->daddr)) {
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300363 __free_page(p->page);
364 return -EINVAL;
365 }
366
Michel Thierry1266cdb2015-03-24 17:06:33 +0000367 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000368}
369
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000370static int setup_page_dma(struct drm_i915_private *dev_priv,
371 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300372{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000373 return __setup_page_dma(dev_priv, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300374}
375
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000376static void cleanup_page_dma(struct drm_i915_private *dev_priv,
377 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300378{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000379 struct pci_dev *pdev = dev_priv->drm.pdev;
David Weinehall52a05c32016-08-22 13:32:44 +0300380
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300381 if (WARN_ON(!p->page))
382 return;
383
Chris Wilsonf51455d2017-01-10 14:47:34 +0000384 dma_unmap_page(&pdev->dev, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300385 __free_page(p->page);
386 memset(p, 0, sizeof(*p));
387}
388
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300389static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300390{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300391 return kmap_atomic(p->page);
392}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300393
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300394/* We use the flushing unmap only with ppgtt structures:
395 * page directories, page tables and scratch pages.
396 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100397static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300398{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300399 /* There are only few exceptions for gen >=6. chv and bxt.
400 * And we are not sure about the latter so play safe for now.
401 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200402 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300403 drm_clflush_virt_range(vaddr, PAGE_SIZE);
404
405 kunmap_atomic(vaddr);
406}
407
Mika Kuoppala567047b2015-06-25 18:35:12 +0300408#define kmap_px(px) kmap_page_dma(px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100409#define kunmap_px(ppgtt, vaddr) \
Chris Wilson49d73912016-11-29 09:50:08 +0000410 kunmap_page_dma((ppgtt)->base.i915, (vaddr))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300411
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000412#define setup_px(dev_priv, px) setup_page_dma((dev_priv), px_base(px))
413#define cleanup_px(dev_priv, px) cleanup_page_dma((dev_priv), px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100414#define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
415#define fill32_px(dev_priv, px, v) \
416 fill_page_dma_32((dev_priv), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300417
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100418static void fill_page_dma(struct drm_i915_private *dev_priv,
419 struct i915_page_dma *p, const uint64_t val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300420{
421 int i;
422 uint64_t * const vaddr = kmap_page_dma(p);
423
424 for (i = 0; i < 512; i++)
425 vaddr[i] = val;
426
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100427 kunmap_page_dma(dev_priv, vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300428}
429
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100430static void fill_page_dma_32(struct drm_i915_private *dev_priv,
431 struct i915_page_dma *p, const uint32_t val32)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300432{
433 uint64_t v = val32;
434
435 v = v << 32 | val32;
436
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100437 fill_page_dma(dev_priv, p, v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300438}
439
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100440static int
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000441setup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100442 struct i915_page_dma *scratch,
443 gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300444{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000445 return __setup_page_dma(dev_priv, scratch, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300446}
447
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000448static void cleanup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100449 struct i915_page_dma *scratch)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300450{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000451 cleanup_page_dma(dev_priv, scratch);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300452}
453
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000454static struct i915_page_table *alloc_pt(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000455{
Michel Thierryec565b32015-04-08 12:13:23 +0100456 struct i915_page_table *pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000457 const size_t count = INTEL_GEN(dev_priv) >= 8 ? GEN8_PTES : GEN6_PTES;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000458 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000459
460 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
461 if (!pt)
462 return ERR_PTR(-ENOMEM);
463
Ben Widawsky678d96f2015-03-16 16:00:56 +0000464 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
465 GFP_KERNEL);
466
467 if (!pt->used_ptes)
468 goto fail_bitmap;
469
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000470 ret = setup_px(dev_priv, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000471 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300472 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000473
474 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000475
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300476fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000477 kfree(pt->used_ptes);
478fail_bitmap:
479 kfree(pt);
480
481 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000482}
483
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000484static void free_pt(struct drm_i915_private *dev_priv,
485 struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000486{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000487 cleanup_px(dev_priv, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300488 kfree(pt->used_ptes);
489 kfree(pt);
490}
491
492static void gen8_initialize_pt(struct i915_address_space *vm,
493 struct i915_page_table *pt)
494{
495 gen8_pte_t scratch_pte;
496
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100497 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200498 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300499
Chris Wilson49d73912016-11-29 09:50:08 +0000500 fill_px(vm->i915, pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300501}
502
503static void gen6_initialize_pt(struct i915_address_space *vm,
504 struct i915_page_table *pt)
505{
506 gen6_pte_t scratch_pte;
507
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100508 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300509
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100510 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200511 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300512
Chris Wilson49d73912016-11-29 09:50:08 +0000513 fill32_px(vm->i915, pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000514}
515
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000516static struct i915_page_directory *alloc_pd(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000517{
Michel Thierryec565b32015-04-08 12:13:23 +0100518 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100519 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000520
521 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
522 if (!pd)
523 return ERR_PTR(-ENOMEM);
524
Michel Thierry33c88192015-04-08 12:13:33 +0100525 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
526 sizeof(*pd->used_pdes), GFP_KERNEL);
527 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300528 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100529
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000530 ret = setup_px(dev_priv, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100531 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300532 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100533
Ben Widawsky06fda602015-02-24 16:22:36 +0000534 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100535
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300536fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100537 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300538fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100539 kfree(pd);
540
541 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000542}
543
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000544static void free_pd(struct drm_i915_private *dev_priv,
545 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300546{
547 if (px_page(pd)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000548 cleanup_px(dev_priv, pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300549 kfree(pd->used_pdes);
550 kfree(pd);
551 }
552}
553
554static void gen8_initialize_pd(struct i915_address_space *vm,
555 struct i915_page_directory *pd)
556{
557 gen8_pde_t scratch_pde;
558
559 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
560
Chris Wilson49d73912016-11-29 09:50:08 +0000561 fill_px(vm->i915, pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300562}
563
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000564static int __pdp_init(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100565 struct i915_page_directory_pointer *pdp)
566{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000567 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Michel Thierry6ac18502015-07-29 17:23:46 +0100568
569 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
570 sizeof(unsigned long),
571 GFP_KERNEL);
572 if (!pdp->used_pdpes)
573 return -ENOMEM;
574
575 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
576 GFP_KERNEL);
577 if (!pdp->page_directory) {
578 kfree(pdp->used_pdpes);
579 /* the PDP might be the statically allocated top level. Keep it
580 * as clean as possible */
581 pdp->used_pdpes = NULL;
582 return -ENOMEM;
583 }
584
585 return 0;
586}
587
588static void __pdp_fini(struct i915_page_directory_pointer *pdp)
589{
590 kfree(pdp->used_pdpes);
591 kfree(pdp->page_directory);
592 pdp->page_directory = NULL;
593}
594
Michel Thierry762d9932015-07-30 11:05:29 +0100595static struct
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000596i915_page_directory_pointer *alloc_pdp(struct drm_i915_private *dev_priv)
Michel Thierry762d9932015-07-30 11:05:29 +0100597{
598 struct i915_page_directory_pointer *pdp;
599 int ret = -ENOMEM;
600
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000601 WARN_ON(!USES_FULL_48BIT_PPGTT(dev_priv));
Michel Thierry762d9932015-07-30 11:05:29 +0100602
603 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
604 if (!pdp)
605 return ERR_PTR(-ENOMEM);
606
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000607 ret = __pdp_init(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100608 if (ret)
609 goto fail_bitmap;
610
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000611 ret = setup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100612 if (ret)
613 goto fail_page_m;
614
615 return pdp;
616
617fail_page_m:
618 __pdp_fini(pdp);
619fail_bitmap:
620 kfree(pdp);
621
622 return ERR_PTR(ret);
623}
624
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000625static void free_pdp(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100626 struct i915_page_directory_pointer *pdp)
627{
628 __pdp_fini(pdp);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000629 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
630 cleanup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100631 kfree(pdp);
632 }
633}
634
Michel Thierry69ab76f2015-07-29 17:23:55 +0100635static void gen8_initialize_pdp(struct i915_address_space *vm,
636 struct i915_page_directory_pointer *pdp)
637{
638 gen8_ppgtt_pdpe_t scratch_pdpe;
639
640 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
641
Chris Wilson49d73912016-11-29 09:50:08 +0000642 fill_px(vm->i915, pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100643}
644
645static void gen8_initialize_pml4(struct i915_address_space *vm,
646 struct i915_pml4 *pml4)
647{
648 gen8_ppgtt_pml4e_t scratch_pml4e;
649
650 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
651 I915_CACHE_LLC);
652
Chris Wilson49d73912016-11-29 09:50:08 +0000653 fill_px(vm->i915, pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100654}
655
Michel Thierry762d9932015-07-30 11:05:29 +0100656static void
Matthew Auld5c693b22016-12-13 16:05:10 +0000657gen8_setup_pdpe(struct i915_hw_ppgtt *ppgtt,
658 struct i915_page_directory_pointer *pdp,
659 struct i915_page_directory *pd,
660 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100661{
662 gen8_ppgtt_pdpe_t *page_directorypo;
663
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000664 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
Michel Thierry762d9932015-07-30 11:05:29 +0100665 return;
666
667 page_directorypo = kmap_px(pdp);
668 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
669 kunmap_px(ppgtt, page_directorypo);
670}
671
672static void
Matthew Auld56843102016-12-13 16:05:11 +0000673gen8_setup_pml4e(struct i915_hw_ppgtt *ppgtt,
674 struct i915_pml4 *pml4,
675 struct i915_page_directory_pointer *pdp,
676 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100677{
678 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
679
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000680 WARN_ON(!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)));
Michel Thierry762d9932015-07-30 11:05:29 +0100681 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
682 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100683}
684
Ben Widawsky94e409c2013-11-04 22:29:36 -0800685/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100686static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100687 unsigned entry,
688 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800689{
Chris Wilson7e37f882016-08-02 22:50:21 +0100690 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000691 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800692 int ret;
693
694 BUG_ON(entry >= 4);
695
John Harrison5fb9de12015-05-29 17:44:07 +0100696 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800697 if (ret)
698 return ret;
699
Chris Wilsonb5321f32016-08-02 22:50:18 +0100700 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
701 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
702 intel_ring_emit(ring, upper_32_bits(addr));
703 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
704 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
705 intel_ring_emit(ring, lower_32_bits(addr));
706 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800707
708 return 0;
709}
710
Michel Thierry2dba3232015-07-30 11:06:23 +0100711static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
712 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800713{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800714 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800715
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100716 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300717 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
718
John Harrisone85b26d2015-05-29 17:43:56 +0100719 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800720 if (ret)
721 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800722 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800723
Ben Widawskyeeb94882013-12-06 14:11:10 -0800724 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800725}
726
Michel Thierry2dba3232015-07-30 11:06:23 +0100727static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
728 struct drm_i915_gem_request *req)
729{
730 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
731}
732
Mika Kuoppalafce93752016-10-31 17:24:46 +0200733/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
734 * the page table structures, we mark them dirty so that
735 * context switching/execlist queuing code takes extra steps
736 * to ensure that tlbs are flushed.
737 */
738static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
739{
Chris Wilson49d73912016-11-29 09:50:08 +0000740 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200741}
742
Michał Winiarski2ce51792016-10-13 14:02:42 +0200743/* Removes entries from a single page table, releasing it if it's empty.
744 * Caller can use the return value to update higher-level entries.
745 */
746static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200747 struct i915_page_table *pt,
748 uint64_t start,
749 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700750{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300751 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200752 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200753 unsigned int pte = gen8_pte_index(start);
754 unsigned int pte_end = pte + num_entries;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200755 gen8_pte_t *pt_vaddr;
756 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
757 I915_CACHE_LLC);
758
759 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200760 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700761
Mika Kuoppala37c63932016-11-01 15:27:36 +0200762 GEM_BUG_ON(pte_end > GEN8_PTES);
763
764 bitmap_clear(pt->used_ptes, pte, num_entries);
Ben Widawsky06fda602015-02-24 16:22:36 +0000765
Zhi Wanga18dbba2016-11-29 14:55:16 +0800766 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200767 return true;
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
1433 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1434 "The allocation has spanned more than 512GB. "
1435 "It is highly likely this is incorrect.");
1436
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001437 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001438 WARN_ON(!pdp);
1439
1440 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1441 if (ret)
1442 goto err_out;
1443
Matthew Auld56843102016-12-13 16:05:11 +00001444 gen8_setup_pml4e(ppgtt, pml4, pdp, pml4e);
Michel Thierry762d9932015-07-30 11:05:29 +01001445 }
1446
1447 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1448 GEN8_PML4ES_PER_PML4);
1449
1450 return 0;
1451
1452err_out:
1453 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson49d73912016-11-29 09:50:08 +00001454 gen8_ppgtt_cleanup_3lvl(vm->i915, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001455
1456 return ret;
1457}
1458
1459static int gen8_alloc_va_range(struct i915_address_space *vm,
1460 uint64_t start, uint64_t length)
1461{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001462 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001463
Chris Wilsonc6385c92016-11-29 12:42:05 +00001464 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001465 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1466 else
1467 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1468}
1469
Michel Thierryea91e402015-07-29 17:23:57 +01001470static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1471 uint64_t start, uint64_t length,
1472 gen8_pte_t scratch_pte,
1473 struct seq_file *m)
1474{
1475 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001476 uint32_t pdpe;
1477
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001478 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001479 struct i915_page_table *pt;
1480 uint64_t pd_len = length;
1481 uint64_t pd_start = start;
1482 uint32_t pde;
1483
1484 if (!test_bit(pdpe, pdp->used_pdpes))
1485 continue;
1486
1487 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001488 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001489 uint32_t pte;
1490 gen8_pte_t *pt_vaddr;
1491
1492 if (!test_bit(pde, pd->used_pdes))
1493 continue;
1494
1495 pt_vaddr = kmap_px(pt);
1496 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1497 uint64_t va =
1498 (pdpe << GEN8_PDPE_SHIFT) |
1499 (pde << GEN8_PDE_SHIFT) |
1500 (pte << GEN8_PTE_SHIFT);
1501 int i;
1502 bool found = false;
1503
1504 for (i = 0; i < 4; i++)
1505 if (pt_vaddr[pte + i] != scratch_pte)
1506 found = true;
1507 if (!found)
1508 continue;
1509
1510 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1511 for (i = 0; i < 4; i++) {
1512 if (pt_vaddr[pte + i] != scratch_pte)
1513 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1514 else
1515 seq_puts(m, " SCRATCH ");
1516 }
1517 seq_puts(m, "\n");
1518 }
1519 /* don't use kunmap_px, it could trigger
1520 * an unnecessary flush.
1521 */
1522 kunmap_atomic(pt_vaddr);
1523 }
1524 }
1525}
1526
1527static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1528{
1529 struct i915_address_space *vm = &ppgtt->base;
1530 uint64_t start = ppgtt->base.start;
1531 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001532 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001533 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001534
Chris Wilsonc6385c92016-11-29 12:42:05 +00001535 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryea91e402015-07-29 17:23:57 +01001536 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1537 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001538 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001539 struct i915_pml4 *pml4 = &ppgtt->pml4;
1540 struct i915_page_directory_pointer *pdp;
1541
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001542 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001543 if (!test_bit(pml4e, pml4->used_pml4es))
1544 continue;
1545
1546 seq_printf(m, " PML4E #%llu\n", pml4e);
1547 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1548 }
1549 }
1550}
1551
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001552static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1553{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001554 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001555 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001556 int ret;
1557
1558 /* We allocate temp bitmap for page tables for no gain
1559 * but as this is for init only, lets keep the things simple
1560 */
1561 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1562 if (ret)
1563 return ret;
1564
1565 /* Allocate for all pdps regardless of how the ppgtt
1566 * was defined.
1567 */
1568 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1569 0, 1ULL << 32,
1570 new_page_dirs);
1571 if (!ret)
1572 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1573
Michał Winiarski3a41a052015-09-03 19:22:18 +02001574 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001575
1576 return ret;
1577}
1578
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001579/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001580 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1581 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1582 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1583 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001584 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001585 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001586static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001587{
Chris Wilson49d73912016-11-29 09:50:08 +00001588 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001589 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001590
Mika Kuoppala8776f022015-06-30 18:16:40 +03001591 ret = gen8_init_scratch(&ppgtt->base);
1592 if (ret)
1593 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001594
Michel Thierryd7b26332015-04-08 12:13:34 +01001595 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001596 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001597 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001598 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001599 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001600 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1601 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001602 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001603
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001604 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1605 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001606 if (ret)
1607 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001608
Michel Thierry69ab76f2015-07-29 17:23:55 +01001609 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1610
Michel Thierry762d9932015-07-30 11:05:29 +01001611 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001612 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001613 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001614 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001615 if (ret)
1616 goto free_scratch;
1617
1618 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001619 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001620 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1621 0, 0,
1622 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001623
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001624 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001625 ret = gen8_preallocate_top_level_pdps(ppgtt);
1626 if (ret)
1627 goto free_scratch;
1628 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001629 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001630
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001631 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001632 gen8_ppgtt_notify_vgt(ppgtt, true);
1633
Michel Thierryd7b26332015-04-08 12:13:34 +01001634 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001635
1636free_scratch:
1637 gen8_free_scratch(&ppgtt->base);
1638 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001639}
1640
Ben Widawsky87d60b62013-12-06 14:11:29 -08001641static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1642{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001643 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001644 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001645 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001646 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001647 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001648 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001649
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001650 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001651 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001652
Dave Gordon731f74c2016-06-24 19:37:46 +01001653 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001654 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001655 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001656 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001657 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001658 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1659
1660 if (pd_entry != expected)
1661 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1662 pde,
1663 pd_entry,
1664 expected);
1665 seq_printf(m, "\tPDE: %x\n", pd_entry);
1666
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001667 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1668
Michel Thierry07749ef2015-03-16 16:00:54 +00001669 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001670 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001671 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001672 (pte * PAGE_SIZE);
1673 int i;
1674 bool found = false;
1675 for (i = 0; i < 4; i++)
1676 if (pt_vaddr[pte + i] != scratch_pte)
1677 found = true;
1678 if (!found)
1679 continue;
1680
1681 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1682 for (i = 0; i < 4; i++) {
1683 if (pt_vaddr[pte + i] != scratch_pte)
1684 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1685 else
1686 seq_puts(m, " SCRATCH ");
1687 }
1688 seq_puts(m, "\n");
1689 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001690 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001691 }
1692}
1693
Ben Widawsky678d96f2015-03-16 16:00:56 +00001694/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001695static void gen6_write_pde(struct i915_page_directory *pd,
1696 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001697{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001698 /* Caller needs to make sure the write completes if necessary */
1699 struct i915_hw_ppgtt *ppgtt =
1700 container_of(pd, struct i915_hw_ppgtt, pd);
1701 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001702
Mika Kuoppala567047b2015-06-25 18:35:12 +03001703 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001704 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001705
Ben Widawsky678d96f2015-03-16 16:00:56 +00001706 writel(pd_entry, ppgtt->pd_addr + pde);
1707}
Ben Widawsky61973492013-04-08 18:43:54 -07001708
Ben Widawsky678d96f2015-03-16 16:00:56 +00001709/* Write all the page tables found in the ppgtt structure to incrementing page
1710 * directories. */
1711static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001712 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001713 uint32_t start, uint32_t length)
1714{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001715 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001716 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001717 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001718
Dave Gordon731f74c2016-06-24 19:37:46 +01001719 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001720 gen6_write_pde(pd, pde, pt);
1721
1722 /* Make sure write is complete before other code can use this page
1723 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001724 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001725}
1726
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001727static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001728{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001729 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001730
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001731 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001732}
Ben Widawsky61973492013-04-08 18:43:54 -07001733
Ben Widawsky90252e52013-12-06 14:11:12 -08001734static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001735 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001736{
Chris Wilson7e37f882016-08-02 22:50:21 +01001737 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001738 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001739 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001740
Ben Widawsky90252e52013-12-06 14:11:12 -08001741 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001742 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001743 if (ret)
1744 return ret;
1745
John Harrison5fb9de12015-05-29 17:44:07 +01001746 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001747 if (ret)
1748 return ret;
1749
Chris Wilsonb5321f32016-08-02 22:50:18 +01001750 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1751 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1752 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1753 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1754 intel_ring_emit(ring, get_pd_offset(ppgtt));
1755 intel_ring_emit(ring, MI_NOOP);
1756 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001757
1758 return 0;
1759}
1760
Ben Widawsky48a10382013-12-06 14:11:11 -08001761static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001762 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001763{
Chris Wilson7e37f882016-08-02 22:50:21 +01001764 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001765 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001766 int ret;
1767
Ben Widawsky48a10382013-12-06 14:11:11 -08001768 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001769 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001770 if (ret)
1771 return ret;
1772
John Harrison5fb9de12015-05-29 17:44:07 +01001773 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001774 if (ret)
1775 return ret;
1776
Chris Wilsonb5321f32016-08-02 22:50:18 +01001777 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1778 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1779 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1780 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1781 intel_ring_emit(ring, get_pd_offset(ppgtt));
1782 intel_ring_emit(ring, MI_NOOP);
1783 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001784
Ben Widawsky90252e52013-12-06 14:11:12 -08001785 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001786 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001787 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001788 if (ret)
1789 return ret;
1790 }
1791
Ben Widawsky48a10382013-12-06 14:11:11 -08001792 return 0;
1793}
1794
Ben Widawskyeeb94882013-12-06 14:11:10 -08001795static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001796 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001797{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001798 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001799 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001800
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001801 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1802 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001803 return 0;
1804}
1805
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001806static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001807{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001808 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301809 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001810
Akash Goel3b3f1652016-10-13 22:44:48 +05301811 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001812 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1813 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001814 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001815 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001816 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001817}
1818
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001819static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001820{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001821 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001822 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301823 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001824
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001825 ecobits = I915_READ(GAC_ECO_BITS);
1826 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1827
1828 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001829 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001830 ecochk |= ECOCHK_PPGTT_WB_HSW;
1831 } else {
1832 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1833 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1834 }
1835 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001836
Akash Goel3b3f1652016-10-13 22:44:48 +05301837 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001838 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001839 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001840 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001841 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001842}
1843
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001844static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001845{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001846 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001847
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001848 ecobits = I915_READ(GAC_ECO_BITS);
1849 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1850 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001851
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001852 gab_ctl = I915_READ(GAB_CTL);
1853 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001854
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001855 ecochk = I915_READ(GAM_ECOCHK);
1856 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001857
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001858 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001859}
1860
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001861/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001862static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001863 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001864 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001865{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001866 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001867 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001868 unsigned first_entry = start >> PAGE_SHIFT;
1869 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001870 unsigned act_pt = first_entry / GEN6_PTES;
1871 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001872 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001873
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001874 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001875 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001876
Daniel Vetter7bddb012012-02-09 17:15:47 +01001877 while (num_entries) {
1878 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001879 if (last_pte > GEN6_PTES)
1880 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001881
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001882 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001883
1884 for (i = first_pte; i < last_pte; i++)
1885 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001886
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001887 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001888
Daniel Vetter7bddb012012-02-09 17:15:47 +01001889 num_entries -= last_pte - first_pte;
1890 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001891 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001892 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001893}
1894
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001895static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001896 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001897 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301898 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001899{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001900 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001901 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001902 unsigned act_pt = first_entry / GEN6_PTES;
1903 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001904 gen6_pte_t *pt_vaddr = NULL;
1905 struct sgt_iter sgt_iter;
1906 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001907
Dave Gordon85d12252016-05-20 11:54:06 +01001908 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001909 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001910 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001911
Chris Wilsoncc797142013-12-31 15:50:30 +00001912 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001913 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301914
Michel Thierry07749ef2015-03-16 16:00:54 +00001915 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001916 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001917 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001918 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001919 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001920 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001921 }
Dave Gordon85d12252016-05-20 11:54:06 +01001922
Chris Wilsoncc797142013-12-31 15:50:30 +00001923 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001924 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001925}
1926
Ben Widawsky678d96f2015-03-16 16:00:56 +00001927static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001928 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001929{
Michel Thierry4933d512015-03-24 15:46:22 +00001930 DECLARE_BITMAP(new_page_tables, I915_PDES);
Chris Wilson49d73912016-11-29 09:50:08 +00001931 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001932 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001933 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001934 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001935 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001936 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001937 int ret;
1938
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001939 start = start_save = start_in;
1940 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001941
1942 bitmap_zero(new_page_tables, I915_PDES);
1943
1944 /* The allocation is done in two stages so that we can bail out with
1945 * minimal amount of pain. The first stage finds new page tables that
1946 * need allocation. The second stage marks use ptes within the page
1947 * tables.
1948 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001949 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001950 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001951 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1952 continue;
1953 }
1954
1955 /* We've already allocated a page table */
1956 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1957
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001958 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001959 if (IS_ERR(pt)) {
1960 ret = PTR_ERR(pt);
1961 goto unwind_out;
1962 }
1963
1964 gen6_initialize_pt(vm, pt);
1965
1966 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001967 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001968 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001969 }
1970
1971 start = start_save;
1972 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001973
Dave Gordon731f74c2016-06-24 19:37:46 +01001974 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001975 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1976
1977 bitmap_zero(tmp_bitmap, GEN6_PTES);
1978 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1979 gen6_pte_count(start, length));
1980
Mika Kuoppala966082c2015-06-25 18:35:19 +03001981 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001982 gen6_write_pde(&ppgtt->pd, pde, pt);
1983
Michel Thierry72744cb2015-03-24 15:46:23 +00001984 trace_i915_page_table_entry_map(vm, pde, pt,
1985 gen6_pte_index(start),
1986 gen6_pte_count(start, length),
1987 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001988 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001989 GEN6_PTES);
1990 }
1991
Michel Thierry4933d512015-03-24 15:46:22 +00001992 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1993
1994 /* Make sure write is complete before other code can use this page
1995 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001996 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001997
Ben Widawsky563222a2015-03-19 12:53:28 +00001998 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001999 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00002000
2001unwind_out:
2002 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002003 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002004
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002005 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002006 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002007 }
2008
2009 mark_tlbs_dirty(ppgtt);
2010 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002011}
2012
Mika Kuoppala8776f022015-06-30 18:16:40 +03002013static int gen6_init_scratch(struct i915_address_space *vm)
2014{
Chris Wilson49d73912016-11-29 09:50:08 +00002015 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002016 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002017
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002018 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002019 if (ret)
2020 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002021
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002022 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002023 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002024 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002025 return PTR_ERR(vm->scratch_pt);
2026 }
2027
2028 gen6_initialize_pt(vm, vm->scratch_pt);
2029
2030 return 0;
2031}
2032
2033static void gen6_free_scratch(struct i915_address_space *vm)
2034{
Chris Wilson49d73912016-11-29 09:50:08 +00002035 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002036
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002037 free_pt(dev_priv, vm->scratch_pt);
2038 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002039}
2040
Daniel Vetter061dd492015-04-14 17:35:13 +02002041static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002042{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002043 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002044 struct i915_page_directory *pd = &ppgtt->pd;
Chris Wilson49d73912016-11-29 09:50:08 +00002045 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry09942c62015-04-08 12:13:30 +01002046 struct i915_page_table *pt;
2047 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002048
Daniel Vetter061dd492015-04-14 17:35:13 +02002049 drm_mm_remove_node(&ppgtt->node);
2050
Dave Gordon731f74c2016-06-24 19:37:46 +01002051 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002052 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002053 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002054
Mika Kuoppala8776f022015-06-30 18:16:40 +03002055 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002056}
2057
Ben Widawskyb1465202014-02-19 22:05:49 -08002058static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002059{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002060 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00002061 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002062 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002063 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002064
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002065 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2066 * allocator works in address space sizes, so it's multiplied by page
2067 * size. We allocate at the top of the GTT to avoid fragmentation.
2068 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002069 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002070
Mika Kuoppala8776f022015-06-30 18:16:40 +03002071 ret = gen6_init_scratch(vm);
2072 if (ret)
2073 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002074
Chris Wilsone007b192017-01-11 11:23:10 +00002075 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2076 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2077 I915_COLOR_UNEVICTABLE,
2078 0, ggtt->base.total,
2079 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00002080 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002081 goto err_out;
2082
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002083 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002084 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002085
Ben Widawskyc8c26622015-01-22 17:01:25 +00002086 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002087
2088err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002089 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002090 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002091}
2092
Ben Widawskyb1465202014-02-19 22:05:49 -08002093static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2094{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002095 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002096}
2097
Michel Thierry4933d512015-03-24 15:46:22 +00002098static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2099 uint64_t start, uint64_t length)
2100{
Michel Thierryec565b32015-04-08 12:13:23 +01002101 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002102 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002103
Dave Gordon731f74c2016-06-24 19:37:46 +01002104 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002105 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002106}
2107
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002108static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002109{
Chris Wilson49d73912016-11-29 09:50:08 +00002110 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002111 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002112 int ret;
2113
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002114 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002115 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002116 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002117 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002118 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002119 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002120 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002121 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002122 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002123
2124 ret = gen6_ppgtt_alloc(ppgtt);
2125 if (ret)
2126 return ret;
2127
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002128 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002129 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2130 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002131 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2132 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002133 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002134 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002135 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002136 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002137
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002138 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002139 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002140
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002141 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002142 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002143
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002144 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002145
Ben Widawsky678d96f2015-03-16 16:00:56 +00002146 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2147
Thierry Reding440fd522015-01-23 09:05:06 +01002148 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002149 ppgtt->node.size >> 20,
2150 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002151
Daniel Vetterfa76da32014-08-06 20:19:54 +02002152 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002153 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002154
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002155 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002156}
2157
Chris Wilson2bfa9962016-08-04 07:52:25 +01002158static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2159 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002160{
Chris Wilson49d73912016-11-29 09:50:08 +00002161 ppgtt->base.i915 = dev_priv;
Daniel Vetter3440d262013-01-24 13:49:56 -08002162
Chris Wilson2bfa9962016-08-04 07:52:25 +01002163 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002164 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002165 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002166 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002167}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002168
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002169static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002170 struct drm_i915_private *dev_priv,
2171 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002172{
Chris Wilson80b204b2016-10-28 13:58:58 +01002173 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002174 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002175 INIT_LIST_HEAD(&vm->active_list);
2176 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002177 INIT_LIST_HEAD(&vm->unbound_list);
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 */
2194 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
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);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002199 else if (IS_SKYLAKE(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);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002201 else if (IS_BROXTON(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
Matthew Aulded9724d2016-11-17 21:04:10 +00002270void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002271{
2272 struct i915_hw_ppgtt *ppgtt =
2273 container_of(kref, struct i915_hw_ppgtt, ref);
2274
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002275 trace_i915_ppgtt_release(&ppgtt->base);
2276
Chris Wilson50e046b2016-08-04 07:52:46 +01002277 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002278 WARN_ON(!list_empty(&ppgtt->base.active_list));
2279 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002280 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002281
Matthew Aulded9724d2016-11-17 21:04:10 +00002282 i915_address_space_fini(&ppgtt->base);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002283
Daniel Vetteree960be2014-08-06 15:04:45 +02002284 ppgtt->base.cleanup(&ppgtt->base);
2285 kfree(ppgtt);
2286}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002287
Ben Widawskya81cc002013-01-18 12:30:31 -08002288/* Certain Gen5 chipsets require require idling the GPU before
2289 * unmapping anything from the GTT when VT-d is enabled.
2290 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002291static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002292{
2293#ifdef CONFIG_INTEL_IOMMU
2294 /* Query intel_iommu to see if we need the workaround. Presumably that
2295 * was loaded first.
2296 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002297 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002298 return true;
2299#endif
2300 return false;
2301}
2302
Chris Wilsondc979972016-05-10 14:10:04 +01002303void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002304{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002305 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302306 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002307
Chris Wilsondc979972016-05-10 14:10:04 +01002308 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002309 return;
2310
Akash Goel3b3f1652016-10-13 22:44:48 +05302311 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002312 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002313 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002314 if (fault_reg & RING_FAULT_VALID) {
2315 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002316 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002317 "\tAddress space: %s\n"
2318 "\tSource ID: %d\n"
2319 "\tType: %d\n",
2320 fault_reg & PAGE_MASK,
2321 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2322 RING_FAULT_SRCID(fault_reg),
2323 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002324 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002325 fault_reg & ~RING_FAULT_VALID);
2326 }
2327 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302328
2329 /* Engine specific init may not have been done till this point. */
2330 if (dev_priv->engine[RCS])
2331 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002332}
2333
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002334void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002335{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002336 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002337
2338 /* Don't bother messing with faults pre GEN6 as we have little
2339 * documentation supporting that it's a good idea.
2340 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002341 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002342 return;
2343
Chris Wilsondc979972016-05-10 14:10:04 +01002344 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002345
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002346 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002347
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002348 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002349}
2350
Chris Wilson03ac84f2016-10-28 13:58:36 +01002351int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2352 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002353{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002354 do {
2355 if (dma_map_sg(&obj->base.dev->pdev->dev,
2356 pages->sgl, pages->nents,
2357 PCI_DMA_BIDIRECTIONAL))
2358 return 0;
2359
2360 /* If the DMA remap fails, one cause can be that we have
2361 * too many objects pinned in a small remapping table,
2362 * such as swiotlb. Incrementally purge all other objects and
2363 * try again - if there are no more pages to remove from
2364 * the DMA remapper, i915_gem_shrink will return 0.
2365 */
2366 GEM_BUG_ON(obj->mm.pages == pages);
2367 } while (i915_gem_shrink(to_i915(obj->base.dev),
2368 obj->base.size >> PAGE_SHIFT,
2369 I915_SHRINK_BOUND |
2370 I915_SHRINK_UNBOUND |
2371 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002372
Chris Wilson03ac84f2016-10-28 13:58:36 +01002373 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002374}
2375
Daniel Vetter2c642b02015-04-14 17:35:26 +02002376static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002377{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002378 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002379}
2380
Chris Wilsond6473f52016-06-10 14:22:59 +05302381static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2382 dma_addr_t addr,
2383 uint64_t offset,
2384 enum i915_cache_level level,
2385 u32 unused)
2386{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002387 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302388 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002389 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302390
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002391 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302392
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002393 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302394}
2395
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002396static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2397 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002398 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302399 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002400{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002401 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002402 struct sgt_iter sgt_iter;
2403 gen8_pte_t __iomem *gtt_entries;
2404 gen8_pte_t gtt_entry;
2405 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002406 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002407
Dave Gordon85d12252016-05-20 11:54:06 +01002408 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2409
2410 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002411 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002412 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002413 }
2414
2415 /*
2416 * XXX: This serves as a posting read to make sure that the PTE has
2417 * actually been updated. There is some concern that even though
2418 * registers and PTEs are within the same BAR that they are potentially
2419 * of NUMA access patterns. Therefore, even with the way we assume
2420 * hardware should work, we must keep this posting read for paranoia.
2421 */
2422 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002423 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002424
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002425 /* This next bit makes the above posting read even more important. We
2426 * want to flush the TLBs only after we're certain all the PTE updates
2427 * have finished.
2428 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002429 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002430}
2431
Chris Wilsonc1403302015-11-18 15:19:39 +00002432struct insert_entries {
2433 struct i915_address_space *vm;
2434 struct sg_table *st;
2435 uint64_t start;
2436 enum i915_cache_level level;
2437 u32 flags;
2438};
2439
2440static int gen8_ggtt_insert_entries__cb(void *_arg)
2441{
2442 struct insert_entries *arg = _arg;
2443 gen8_ggtt_insert_entries(arg->vm, arg->st,
2444 arg->start, arg->level, arg->flags);
2445 return 0;
2446}
2447
2448static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2449 struct sg_table *st,
2450 uint64_t start,
2451 enum i915_cache_level level,
2452 u32 flags)
2453{
2454 struct insert_entries arg = { vm, st, start, level, flags };
2455 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2456}
2457
Chris Wilsond6473f52016-06-10 14:22:59 +05302458static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2459 dma_addr_t addr,
2460 uint64_t offset,
2461 enum i915_cache_level level,
2462 u32 flags)
2463{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002464 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302465 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002466 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302467
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002468 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302469
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002470 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302471}
2472
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002473/*
2474 * Binds an object into the global gtt with the specified cache level. The object
2475 * will be accessible to the GPU via commands whose operands reference offsets
2476 * within the global GTT as well as accessible by the GPU through the GMADR
2477 * mapped BAR (dev_priv->mm.gtt->gtt).
2478 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002479static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002480 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002481 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302482 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002483{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002484 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002485 struct sgt_iter sgt_iter;
2486 gen6_pte_t __iomem *gtt_entries;
2487 gen6_pte_t gtt_entry;
2488 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002489 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002490
Dave Gordon85d12252016-05-20 11:54:06 +01002491 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2492
2493 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002494 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002495 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002496 }
2497
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002498 /* XXX: This serves as a posting read to make sure that the PTE has
2499 * actually been updated. There is some concern that even though
2500 * registers and PTEs are within the same BAR that they are potentially
2501 * of NUMA access patterns. Therefore, even with the way we assume
2502 * hardware should work, we must keep this posting read for paranoia.
2503 */
Dave Gordon85d12252016-05-20 11:54:06 +01002504 if (i != 0)
2505 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002506
2507 /* This next bit makes the above posting read even more important. We
2508 * want to flush the TLBs only after we're certain all the PTE updates
2509 * have finished.
2510 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002511 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002512}
2513
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002514static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002515 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002516{
2517}
2518
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002519static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002520 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002521{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002522 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002523 unsigned first_entry = start >> PAGE_SHIFT;
2524 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002525 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002526 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2527 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002528 int i;
2529
2530 if (WARN(num_entries > max_entries,
2531 "First entry = %d; Num entries = %d (max=%d)\n",
2532 first_entry, num_entries, max_entries))
2533 num_entries = max_entries;
2534
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002535 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002536 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002537 for (i = 0; i < num_entries; i++)
2538 gen8_set_pte(&gtt_base[i], scratch_pte);
2539 readl(gtt_base);
2540}
2541
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002542static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002543 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002544 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002545{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002546 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002547 unsigned first_entry = start >> PAGE_SHIFT;
2548 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002549 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002550 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2551 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002552 int i;
2553
2554 if (WARN(num_entries > max_entries,
2555 "First entry = %d; Num entries = %d (max=%d)\n",
2556 first_entry, num_entries, max_entries))
2557 num_entries = max_entries;
2558
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002559 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002560 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002561
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002562 for (i = 0; i < num_entries; i++)
2563 iowrite32(scratch_pte, &gtt_base[i]);
2564 readl(gtt_base);
2565}
2566
Chris Wilsond6473f52016-06-10 14:22:59 +05302567static void i915_ggtt_insert_page(struct i915_address_space *vm,
2568 dma_addr_t addr,
2569 uint64_t offset,
2570 enum i915_cache_level cache_level,
2571 u32 unused)
2572{
Chris Wilsond6473f52016-06-10 14:22:59 +05302573 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2574 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302575
2576 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302577}
2578
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002579static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2580 struct sg_table *pages,
2581 uint64_t start,
2582 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002583{
2584 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2585 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2586
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002587 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002588
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002589}
2590
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002591static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002592 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002593 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002594{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002595 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002596}
2597
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002598static int ggtt_bind_vma(struct i915_vma *vma,
2599 enum i915_cache_level cache_level,
2600 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002601{
Chris Wilson49d73912016-11-29 09:50:08 +00002602 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002603 struct drm_i915_gem_object *obj = vma->obj;
2604 u32 pte_flags = 0;
2605 int ret;
2606
2607 ret = i915_get_ggtt_vma_pages(vma);
2608 if (ret)
2609 return ret;
2610
2611 /* Currently applicable only to VLV */
2612 if (obj->gt_ro)
2613 pte_flags |= PTE_READ_ONLY;
2614
Chris Wilson9c870d02016-10-24 13:42:15 +01002615 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002616 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002617 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002618 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002619
2620 /*
2621 * Without aliasing PPGTT there's no difference between
2622 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2623 * upgrade to both bound if we bind either to avoid double-binding.
2624 */
Chris Wilson3272db52016-08-04 16:32:32 +01002625 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002626
2627 return 0;
2628}
2629
2630static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2631 enum i915_cache_level cache_level,
2632 u32 flags)
2633{
Chris Wilson49d73912016-11-29 09:50:08 +00002634 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002635 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002636 int ret;
2637
2638 ret = i915_get_ggtt_vma_pages(vma);
2639 if (ret)
2640 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002641
Akash Goel24f3a8c2014-06-17 10:59:42 +05302642 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002643 pte_flags = 0;
2644 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002645 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302646
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002647
Chris Wilson3272db52016-08-04 16:32:32 +01002648 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002649 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002650 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002651 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002652 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002653 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002654 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002655
Chris Wilson3272db52016-08-04 16:32:32 +01002656 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002657 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002658 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002659 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002660 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002661 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002662
2663 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002664}
2665
2666static void ggtt_unbind_vma(struct i915_vma *vma)
2667{
Chris Wilson49d73912016-11-29 09:50:08 +00002668 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson9c870d02016-10-24 13:42:15 +01002669 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002670 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002671
Chris Wilson9c870d02016-10-24 13:42:15 +01002672 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2673 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002674 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002675 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002676 intel_runtime_pm_put(i915);
2677 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002678
Chris Wilson3272db52016-08-04 16:32:32 +01002679 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002680 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002681 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002682}
2683
Chris Wilson03ac84f2016-10-28 13:58:36 +01002684void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2685 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002686{
David Weinehall52a05c32016-08-22 13:32:44 +03002687 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2688 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002689 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002690
Chris Wilson307dc252016-08-05 10:14:12 +01002691 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002692 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002693 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2694 /* Wait a bit, in hopes it avoids the hang */
2695 udelay(10);
2696 }
2697 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002698
Chris Wilson03ac84f2016-10-28 13:58:36 +01002699 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002700}
Daniel Vetter644ec022012-03-26 09:45:40 +02002701
Chris Wilson45b186f2016-12-16 07:46:42 +00002702static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002703 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002704 u64 *start,
2705 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002706{
2707 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002708 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002709
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002710 node = list_next_entry(node, node_list);
2711 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002712 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002713}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002714
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002715int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002716{
Ben Widawskye78891c2013-01-25 16:41:04 -08002717 /* Let GEM Manage all of the aperture.
2718 *
2719 * However, leave one page at the end still bound to the scratch page.
2720 * There are a number of places where the hardware apparently prefetches
2721 * past the end of the object, and we've seen multiple hangs with the
2722 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2723 * aperture. One page should be enough to keep any prefetching inside
2724 * of the aperture.
2725 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002726 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002727 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002728 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002729 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002730 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002731
Zhi Wangb02d22a2016-06-16 08:06:59 -04002732 ret = intel_vgt_balloon(dev_priv);
2733 if (ret)
2734 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002735
Chris Wilson95374d72016-10-12 10:05:20 +01002736 /* Reserve a mappable slot for our lockless error capture */
2737 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2738 &ggtt->error_capture,
Chris Wilsonf51455d2017-01-10 14:47:34 +00002739 PAGE_SIZE, 0,
Chris Wilson85fd4f52016-12-05 14:29:36 +00002740 I915_COLOR_UNEVICTABLE,
Chris Wilson95374d72016-10-12 10:05:20 +01002741 0, ggtt->mappable_end,
2742 0, 0);
2743 if (ret)
2744 return ret;
2745
Chris Wilsoned2f3452012-11-15 11:32:19 +00002746 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002747 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002748 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2749 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002750 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002751 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002752 }
2753
2754 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002755 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002756 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002757
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002758 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002759 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002760 if (!ppgtt) {
2761 ret = -ENOMEM;
2762 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002763 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002764
Chris Wilson95374d72016-10-12 10:05:20 +01002765 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2766 if (ret)
2767 goto err_ppgtt;
2768
2769 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002770 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2771 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002772 if (ret)
2773 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002774 }
2775
2776 ppgtt->base.clear_range(&ppgtt->base,
2777 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002778 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002779
Daniel Vetterfa76da32014-08-06 20:19:54 +02002780 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002781 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2782 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002783 }
2784
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002785 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002786
2787err_ppgtt_cleanup:
2788 ppgtt->base.cleanup(&ppgtt->base);
2789err_ppgtt:
2790 kfree(ppgtt);
2791err:
2792 drm_mm_remove_node(&ggtt->error_capture);
2793 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002794}
2795
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002796/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002797 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002798 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002799 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002800void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002801{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002802 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002803
Daniel Vetter70e32542014-08-06 15:04:57 +02002804 if (dev_priv->mm.aliasing_ppgtt) {
2805 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002806 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002807 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002808 }
2809
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002810 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002811
Chris Wilson95374d72016-10-12 10:05:20 +01002812 if (drm_mm_node_allocated(&ggtt->error_capture))
2813 drm_mm_remove_node(&ggtt->error_capture);
2814
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002815 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002816 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002817
Matthew Aulded9724d2016-11-17 21:04:10 +00002818 mutex_lock(&dev_priv->drm.struct_mutex);
2819 i915_address_space_fini(&ggtt->base);
2820 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002821 }
2822
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002823 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002824
2825 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002826 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002827}
Daniel Vetter70e32542014-08-06 15:04:57 +02002828
Daniel Vetter2c642b02015-04-14 17:35:26 +02002829static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002830{
2831 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2832 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2833 return snb_gmch_ctl << 20;
2834}
2835
Daniel Vetter2c642b02015-04-14 17:35:26 +02002836static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002837{
2838 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2839 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2840 if (bdw_gmch_ctl)
2841 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002842
2843#ifdef CONFIG_X86_32
2844 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2845 if (bdw_gmch_ctl > 4)
2846 bdw_gmch_ctl = 4;
2847#endif
2848
Ben Widawsky9459d252013-11-03 16:53:55 -08002849 return bdw_gmch_ctl << 20;
2850}
2851
Daniel Vetter2c642b02015-04-14 17:35:26 +02002852static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002853{
2854 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2855 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2856
2857 if (gmch_ctrl)
2858 return 1 << (20 + gmch_ctrl);
2859
2860 return 0;
2861}
2862
Daniel Vetter2c642b02015-04-14 17:35:26 +02002863static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002864{
2865 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2866 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2867 return snb_gmch_ctl << 25; /* 32 MB units */
2868}
2869
Daniel Vetter2c642b02015-04-14 17:35:26 +02002870static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002871{
2872 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2873 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2874 return bdw_gmch_ctl << 25; /* 32 MB units */
2875}
2876
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002877static size_t chv_get_stolen_size(u16 gmch_ctrl)
2878{
2879 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2880 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2881
2882 /*
2883 * 0x0 to 0x10: 32MB increments starting at 0MB
2884 * 0x11 to 0x16: 4MB increments starting at 8MB
2885 * 0x17 to 0x1d: 4MB increments start at 36MB
2886 */
2887 if (gmch_ctrl < 0x11)
2888 return gmch_ctrl << 25;
2889 else if (gmch_ctrl < 0x17)
2890 return (gmch_ctrl - 0x11 + 2) << 22;
2891 else
2892 return (gmch_ctrl - 0x17 + 9) << 22;
2893}
2894
Damien Lespiau66375012014-01-09 18:02:46 +00002895static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2896{
2897 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2898 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2899
2900 if (gen9_gmch_ctl < 0xf0)
2901 return gen9_gmch_ctl << 25; /* 32 MB units */
2902 else
2903 /* 4MB increments starting at 0xf0 for 4MB */
2904 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2905}
2906
Chris Wilson34c998b2016-08-04 07:52:24 +01002907static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002908{
Chris Wilson49d73912016-11-29 09:50:08 +00002909 struct drm_i915_private *dev_priv = ggtt->base.i915;
2910 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002911 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002912 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002913
2914 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002915 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002916
Imre Deak2a073f892015-03-27 13:07:33 +02002917 /*
2918 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2919 * dropped. For WC mappings in general we have 64 byte burst writes
2920 * when the WC buffer is flushed, so we can't use it, but have to
2921 * resort to an uncached mapping. The WC issue is easily caught by the
2922 * readback check when writing GTT PTE entries.
2923 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002924 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002925 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002926 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002927 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002928 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002929 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002930 return -ENOMEM;
2931 }
2932
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002933 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002934 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002935 DRM_ERROR("Scratch setup failed\n");
2936 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002937 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002938 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002939 }
2940
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002941 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002942}
2943
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002944/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2945 * bits. When using advanced contexts each context stores its own PAT, but
2946 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002947static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002948{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002949 uint64_t pat;
2950
2951 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2952 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2953 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2954 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2955 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2956 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2957 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2958 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2959
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002960 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002961 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2962 * so RTL will always use the value corresponding to
2963 * pat_sel = 000".
2964 * So let's disable cache for GGTT to avoid screen corruptions.
2965 * MOCS still can be used though.
2966 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2967 * before this patch, i.e. the same uncached + snooping access
2968 * like on gen6/7 seems to be in effect.
2969 * - So this just fixes blitter/render access. Again it looks
2970 * like it's not just uncached access, but uncached + snooping.
2971 * So we can still hold onto all our assumptions wrt cpu
2972 * clflushing on LLC machines.
2973 */
2974 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2975
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002976 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2977 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002978 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2979 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002980}
2981
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002982static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2983{
2984 uint64_t pat;
2985
2986 /*
2987 * Map WB on BDW to snooped on CHV.
2988 *
2989 * Only the snoop bit has meaning for CHV, the rest is
2990 * ignored.
2991 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002992 * The hardware will never snoop for certain types of accesses:
2993 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2994 * - PPGTT page tables
2995 * - some other special cycles
2996 *
2997 * As with BDW, we also need to consider the following for GT accesses:
2998 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2999 * so RTL will always use the value corresponding to
3000 * pat_sel = 000".
3001 * Which means we must set the snoop bit in PAT entry 0
3002 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003003 */
3004 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3005 GEN8_PPAT(1, 0) |
3006 GEN8_PPAT(2, 0) |
3007 GEN8_PPAT(3, 0) |
3008 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3009 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3010 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3011 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3012
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003013 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3014 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003015}
3016
Chris Wilson34c998b2016-08-04 07:52:24 +01003017static void gen6_gmch_remove(struct i915_address_space *vm)
3018{
3019 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3020
3021 iounmap(ggtt->gsm);
Chris Wilson49d73912016-11-29 09:50:08 +00003022 cleanup_scratch_page(vm->i915, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003023}
3024
Joonas Lahtinend507d732016-03-18 10:42:58 +02003025static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003026{
Chris Wilson49d73912016-11-29 09:50:08 +00003027 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003028 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003029 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003030 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003031
3032 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003033 ggtt->mappable_base = pci_resource_start(pdev, 2);
3034 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003035
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003036 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3037 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003038
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003039 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003040
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003041 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003042 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003043 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003044 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003045 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003046 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003047 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003048 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003049 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003050 }
Ben Widawsky63340132013-11-04 19:32:22 -08003051
Chris Wilson34c998b2016-08-04 07:52:24 +01003052 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003053
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003054 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003055 chv_setup_private_ppat(dev_priv);
3056 else
3057 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003058
Chris Wilson34c998b2016-08-04 07:52:24 +01003059 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003060 ggtt->base.bind_vma = ggtt_bind_vma;
3061 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303062 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003063 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003064 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003065 ggtt->base.clear_range = gen8_ggtt_clear_range;
3066
3067 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3068 if (IS_CHERRYVIEW(dev_priv))
3069 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3070
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003071 ggtt->invalidate = gen6_ggtt_invalidate;
3072
Chris Wilson34c998b2016-08-04 07:52:24 +01003073 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003074}
3075
Joonas Lahtinend507d732016-03-18 10:42:58 +02003076static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003077{
Chris Wilson49d73912016-11-29 09:50:08 +00003078 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003079 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003080 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003081 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003082
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003083 ggtt->mappable_base = pci_resource_start(pdev, 2);
3084 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003085
Ben Widawskybaa09f52013-01-24 13:49:57 -08003086 /* 64/512MB is the current min/max we actually know of, but this is just
3087 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003088 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003089 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003090 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003091 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003092 }
3093
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003094 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3095 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3096 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003097
Joonas Lahtinend507d732016-03-18 10:42:58 +02003098 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003099
Chris Wilson34c998b2016-08-04 07:52:24 +01003100 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3101 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003102
Joonas Lahtinend507d732016-03-18 10:42:58 +02003103 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303104 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003105 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3106 ggtt->base.bind_vma = ggtt_bind_vma;
3107 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003108 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003109
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003110 ggtt->invalidate = gen6_ggtt_invalidate;
3111
Chris Wilson34c998b2016-08-04 07:52:24 +01003112 if (HAS_EDRAM(dev_priv))
3113 ggtt->base.pte_encode = iris_pte_encode;
3114 else if (IS_HASWELL(dev_priv))
3115 ggtt->base.pte_encode = hsw_pte_encode;
3116 else if (IS_VALLEYVIEW(dev_priv))
3117 ggtt->base.pte_encode = byt_pte_encode;
3118 else if (INTEL_GEN(dev_priv) >= 7)
3119 ggtt->base.pte_encode = ivb_pte_encode;
3120 else
3121 ggtt->base.pte_encode = snb_pte_encode;
3122
3123 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003124}
3125
Chris Wilson34c998b2016-08-04 07:52:24 +01003126static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003127{
Chris Wilson34c998b2016-08-04 07:52:24 +01003128 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003129}
3130
Joonas Lahtinend507d732016-03-18 10:42:58 +02003131static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003132{
Chris Wilson49d73912016-11-29 09:50:08 +00003133 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003134 int ret;
3135
Chris Wilson91c8a322016-07-05 10:40:23 +01003136 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003137 if (!ret) {
3138 DRM_ERROR("failed to set up gmch\n");
3139 return -EIO;
3140 }
3141
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003142 intel_gtt_get(&ggtt->base.total,
3143 &ggtt->stolen_size,
3144 &ggtt->mappable_base,
3145 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003146
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003147 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303148 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003149 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3150 ggtt->base.clear_range = i915_ggtt_clear_range;
3151 ggtt->base.bind_vma = ggtt_bind_vma;
3152 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003153 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003154
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003155 ggtt->invalidate = gmch_ggtt_invalidate;
3156
Joonas Lahtinend507d732016-03-18 10:42:58 +02003157 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003158 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3159
Ben Widawskybaa09f52013-01-24 13:49:57 -08003160 return 0;
3161}
3162
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003163/**
Chris Wilson0088e522016-08-04 07:52:21 +01003164 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003165 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003166 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003167int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003168{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003169 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003170 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003171
Chris Wilson49d73912016-11-29 09:50:08 +00003172 ggtt->base.i915 = dev_priv;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003173
Chris Wilson34c998b2016-08-04 07:52:24 +01003174 if (INTEL_GEN(dev_priv) <= 5)
3175 ret = i915_gmch_probe(ggtt);
3176 else if (INTEL_GEN(dev_priv) < 8)
3177 ret = gen6_gmch_probe(ggtt);
3178 else
3179 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003180 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003181 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003182
Chris Wilsondb9309a2017-01-05 15:30:23 +00003183 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3184 * This is easier than doing range restriction on the fly, as we
3185 * currently don't have any bits spare to pass in this upper
3186 * restriction!
3187 */
3188 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3189 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3190 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3191 }
3192
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003193 if ((ggtt->base.total - 1) >> 32) {
3194 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003195 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003196 ggtt->base.total >> 20);
3197 ggtt->base.total = 1ULL << 32;
3198 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3199 }
3200
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003201 if (ggtt->mappable_end > ggtt->base.total) {
3202 DRM_ERROR("mappable aperture extends past end of GGTT,"
3203 " aperture=%llx, total=%llx\n",
3204 ggtt->mappable_end, ggtt->base.total);
3205 ggtt->mappable_end = ggtt->base.total;
3206 }
3207
Ben Widawskybaa09f52013-01-24 13:49:57 -08003208 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003209 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003210 ggtt->base.total >> 20);
3211 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003212 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003213#ifdef CONFIG_INTEL_IOMMU
3214 if (intel_iommu_gfx_mapped)
3215 DRM_INFO("VT-d active for gfx access\n");
3216#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003217
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003218 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003219}
3220
3221/**
3222 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003223 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003224 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003225int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003226{
Chris Wilson0088e522016-08-04 07:52:21 +01003227 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3228 int ret;
3229
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003230 INIT_LIST_HEAD(&dev_priv->vm_list);
3231
3232 /* Subtract the guard page before address space initialization to
3233 * shrink the range used by drm_mm.
3234 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003235 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003236 ggtt->base.total -= PAGE_SIZE;
Chris Wilson80b204b2016-10-28 13:58:58 +01003237 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003238 ggtt->base.total += PAGE_SIZE;
3239 if (!HAS_LLC(dev_priv))
3240 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003241 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003242
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003243 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3244 dev_priv->ggtt.mappable_base,
3245 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003246 ret = -EIO;
3247 goto out_gtt_cleanup;
3248 }
3249
3250 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3251
Chris Wilson0088e522016-08-04 07:52:21 +01003252 /*
3253 * Initialise stolen early so that we may reserve preallocated
3254 * objects for the BIOS to KMS transition.
3255 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003256 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003257 if (ret)
3258 goto out_gtt_cleanup;
3259
3260 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003261
3262out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003263 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003264 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003265}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003266
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003267int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003268{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003269 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003270 return -EIO;
3271
3272 return 0;
3273}
3274
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003275void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3276{
3277 i915->ggtt.invalidate = guc_ggtt_invalidate;
3278}
3279
3280void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3281{
3282 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3283}
3284
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003285void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003286{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003287 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003288 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003289
Chris Wilsondc979972016-05-10 14:10:04 +01003290 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003291
3292 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003293 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003294
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003295 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3296
3297 /* clflush objects bound into the GGTT and rebind them. */
3298 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003299 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003300 bool ggtt_bound = false;
3301 struct i915_vma *vma;
3302
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003303 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003304 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003305 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003306
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003307 if (!i915_vma_unbind(vma))
3308 continue;
3309
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003310 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3311 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003312 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003313 }
3314
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003315 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003316 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003317 }
3318
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003319 ggtt->base.closed = false;
3320
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003321 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003322 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003323 chv_setup_private_ppat(dev_priv);
3324 else
3325 bdw_setup_private_ppat(dev_priv);
3326
3327 return;
3328 }
3329
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003330 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003331 struct i915_address_space *vm;
3332
Daniel Vetterfa423312015-04-14 17:35:23 +02003333 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3334 /* TODO: Perhaps it shouldn't be gen6 specific */
3335
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003336 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003337
Chris Wilson2bfa9962016-08-04 07:52:25 +01003338 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003339 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003340 else
3341 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003342
3343 gen6_write_page_range(dev_priv, &ppgtt->pd,
3344 0, ppgtt->base.total);
3345 }
3346 }
3347
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003348 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003349}
3350
Chris Wilson058d88c2016-08-15 10:49:06 +01003351struct i915_vma *
3352i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3353 struct i915_address_space *vm,
3354 const struct i915_ggtt_view *view)
3355{
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003356 struct rb_node *rb;
Chris Wilson058d88c2016-08-15 10:49:06 +01003357
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003358 rb = obj->vma_tree.rb_node;
3359 while (rb) {
3360 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
3361 long cmp;
3362
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003363 cmp = i915_vma_compare(vma, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003364 if (cmp == 0)
Chris Wilson058d88c2016-08-15 10:49:06 +01003365 return vma;
3366
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003367 if (cmp < 0)
3368 rb = rb->rb_right;
3369 else
3370 rb = rb->rb_left;
3371 }
3372
Chris Wilson058d88c2016-08-15 10:49:06 +01003373 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003374}
3375
3376struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003377i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003378 struct i915_address_space *vm,
3379 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003380{
3381 struct i915_vma *vma;
3382
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003383 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +01003384 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3385
3386 vma = i915_gem_obj_to_vma(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003387 if (!vma) {
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003388 vma = i915_vma_create(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003389 GEM_BUG_ON(vma != i915_gem_obj_to_vma(obj, vm, view));
3390 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003391
Chris Wilson3272db52016-08-04 16:32:32 +01003392 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003393 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003394}
3395
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003396static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003397rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003398 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003399 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003400 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003401{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003402 unsigned int column, row;
3403 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003404
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003405 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003406 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003407 for (row = 0; row < height; row++) {
3408 st->nents++;
3409 /* We don't need the pages, but need to initialize
3410 * the entries so the sg list can be happily traversed.
3411 * The only thing we need are DMA addresses.
3412 */
3413 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003414 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003415 sg_dma_len(sg) = PAGE_SIZE;
3416 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003417 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003418 }
3419 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003420
3421 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003422}
3423
3424static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003425intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003426 struct drm_i915_gem_object *obj)
3427{
Dave Gordon85d12252016-05-20 11:54:06 +01003428 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003429 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003430 struct sgt_iter sgt_iter;
3431 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003432 unsigned long i;
3433 dma_addr_t *page_addr_list;
3434 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003435 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003436 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003437
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003438 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003439 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003440 sizeof(dma_addr_t),
3441 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003442 if (!page_addr_list)
3443 return ERR_PTR(ret);
3444
3445 /* Allocate target SG list. */
3446 st = kmalloc(sizeof(*st), GFP_KERNEL);
3447 if (!st)
3448 goto err_st_alloc;
3449
Ville Syrjälä6687c902015-09-15 13:16:41 +03003450 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003451 if (ret)
3452 goto err_sg_alloc;
3453
3454 /* Populate source page list from the object. */
3455 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003456 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003457 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003458
Dave Gordon85d12252016-05-20 11:54:06 +01003459 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003460 st->nents = 0;
3461 sg = st->sgl;
3462
Ville Syrjälä6687c902015-09-15 13:16:41 +03003463 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3464 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3465 rot_info->plane[i].width, rot_info->plane[i].height,
3466 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003467 }
3468
Ville Syrjälä6687c902015-09-15 13:16:41 +03003469 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3470 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003471
3472 drm_free_large(page_addr_list);
3473
3474 return st;
3475
3476err_sg_alloc:
3477 kfree(st);
3478err_st_alloc:
3479 drm_free_large(page_addr_list);
3480
Ville Syrjälä6687c902015-09-15 13:16:41 +03003481 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3482 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3483
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003484 return ERR_PTR(ret);
3485}
3486
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003487static struct sg_table *
3488intel_partial_pages(const struct i915_ggtt_view *view,
3489 struct drm_i915_gem_object *obj)
3490{
3491 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003492 struct scatterlist *sg, *iter;
3493 unsigned int count = view->params.partial.size;
3494 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003495 int ret = -ENOMEM;
3496
3497 st = kmalloc(sizeof(*st), GFP_KERNEL);
3498 if (!st)
3499 goto err_st_alloc;
3500
Chris Wilsond2a84a72016-10-28 13:58:34 +01003501 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003502 if (ret)
3503 goto err_sg_alloc;
3504
Chris Wilsond2a84a72016-10-28 13:58:34 +01003505 iter = i915_gem_object_get_sg(obj,
3506 view->params.partial.offset,
3507 &offset);
3508 GEM_BUG_ON(!iter);
3509
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003510 sg = st->sgl;
3511 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003512 do {
3513 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003514
Chris Wilsond2a84a72016-10-28 13:58:34 +01003515 len = min(iter->length - (offset << PAGE_SHIFT),
3516 count << PAGE_SHIFT);
3517 sg_set_page(sg, NULL, len, 0);
3518 sg_dma_address(sg) =
3519 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3520 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003521
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003522 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003523 count -= len >> PAGE_SHIFT;
3524 if (count == 0) {
3525 sg_mark_end(sg);
3526 return st;
3527 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003528
Chris Wilsond2a84a72016-10-28 13:58:34 +01003529 sg = __sg_next(sg);
3530 iter = __sg_next(iter);
3531 offset = 0;
3532 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003533
3534err_sg_alloc:
3535 kfree(st);
3536err_st_alloc:
3537 return ERR_PTR(ret);
3538}
3539
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003540static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003541i915_get_ggtt_vma_pages(struct i915_vma *vma)
3542{
3543 int ret = 0;
3544
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003545 /* The vma->pages are only valid within the lifespan of the borrowed
3546 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3547 * must be the vma->pages. A simple rule is that vma->pages must only
3548 * be accessed when the obj->mm.pages are pinned.
3549 */
3550 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3551
Chris Wilson247177d2016-08-15 10:48:47 +01003552 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003553 return 0;
3554
3555 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003556 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003557 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003558 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003559 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003560 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003561 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003562 else
3563 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3564 vma->ggtt_view.type);
3565
Chris Wilson247177d2016-08-15 10:48:47 +01003566 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003567 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003568 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003569 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003570 } else if (IS_ERR(vma->pages)) {
3571 ret = PTR_ERR(vma->pages);
3572 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003573 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3574 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003575 }
3576
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003577 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003578}
3579
Chris Wilsone007b192017-01-11 11:23:10 +00003580/**
Chris Wilson625d9882017-01-11 11:23:11 +00003581 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003582 * @vm: the &struct i915_address_space
3583 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3584 * @size: how much space to allocate inside the GTT,
3585 * must be #I915_GTT_PAGE_SIZE aligned
3586 * @offset: where to insert inside the GTT,
3587 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3588 * (@offset + @size) must fit within the address space
3589 * @color: color to apply to node, if this node is not from a VMA,
3590 * color must be #I915_COLOR_UNEVICTABLE
3591 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003592 *
3593 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3594 * the address space (using @size and @color). If the @node does not fit, it
3595 * tries to evict any overlapping nodes from the GTT, including any
3596 * neighbouring nodes if the colors do not match (to ensure guard pages between
3597 * differing domains). See i915_gem_evict_for_node() for the gory details
3598 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3599 * evicting active overlapping objects, and any overlapping node that is pinned
3600 * or marked as unevictable will also result in failure.
3601 *
3602 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3603 * asked to wait for eviction and interrupted.
3604 */
3605int i915_gem_gtt_reserve(struct i915_address_space *vm,
3606 struct drm_mm_node *node,
3607 u64 size, u64 offset, unsigned long color,
3608 unsigned int flags)
3609{
3610 int err;
3611
3612 GEM_BUG_ON(!size);
3613 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3614 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3615 GEM_BUG_ON(range_overflows(offset, size, vm->total));
3616
3617 node->size = size;
3618 node->start = offset;
3619 node->color = color;
3620
3621 err = drm_mm_reserve_node(&vm->mm, node);
3622 if (err != -ENOSPC)
3623 return err;
3624
3625 err = i915_gem_evict_for_node(vm, node, flags);
3626 if (err == 0)
3627 err = drm_mm_reserve_node(&vm->mm, node);
3628
3629 return err;
3630}
3631
Chris Wilson606fec92017-01-11 11:23:12 +00003632static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3633{
3634 u64 range, addr;
3635
3636 GEM_BUG_ON(range_overflows(start, len, end));
3637 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3638
3639 range = round_down(end - len, align) - round_up(start, align);
3640 if (range) {
3641 if (sizeof(unsigned long) == sizeof(u64)) {
3642 addr = get_random_long();
3643 } else {
3644 addr = get_random_int();
3645 if (range > U32_MAX) {
3646 addr <<= 32;
3647 addr |= get_random_int();
3648 }
3649 }
3650 div64_u64_rem(addr, range, &addr);
3651 start += addr;
3652 }
3653
3654 return round_up(start, align);
3655}
3656
Chris Wilson625d9882017-01-11 11:23:11 +00003657/**
Chris Wilsone007b192017-01-11 11:23:10 +00003658 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003659 * @vm: the &struct i915_address_space
3660 * @node: the &struct drm_mm_node (typically i915_vma.node)
3661 * @size: how much space to allocate inside the GTT,
3662 * must be #I915_GTT_PAGE_SIZE aligned
3663 * @alignment: required alignment of starting offset, may be 0 but
3664 * if specified, this must be a power-of-two and at least
3665 * #I915_GTT_MIN_ALIGNMENT
3666 * @color: color to apply to node
3667 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003668 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003669 * @end: end of any range restriction inside GTT (U64_MAX for all),
3670 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3671 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003672 *
3673 * i915_gem_gtt_insert() first searches for an available hole into which
3674 * is can insert the node. The hole address is aligned to @alignment and
3675 * its @size must then fit entirely within the [@start, @end] bounds. The
3676 * nodes on either side of the hole must match @color, or else a guard page
3677 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003678 * suitable hole is found, first a victim is randomly selected and tested
3679 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003680 * is scanned to find the first set of replacement nodes to create the hole.
3681 * Those old overlapping nodes are evicted from the GTT (and so must be
3682 * rebound before any future use). Any node that is currently pinned cannot
3683 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3684 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3685 * searching for an eviction candidate. See i915_gem_evict_something() for
3686 * the gory details on the eviction algorithm.
3687 *
3688 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3689 * asked to wait for eviction and interrupted.
3690 */
3691int i915_gem_gtt_insert(struct i915_address_space *vm,
3692 struct drm_mm_node *node,
3693 u64 size, u64 alignment, unsigned long color,
3694 u64 start, u64 end, unsigned int flags)
3695{
3696 u32 search_flag, alloc_flag;
Chris Wilson606fec92017-01-11 11:23:12 +00003697 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003698 int err;
3699
3700 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3701 GEM_BUG_ON(!size);
3702 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3703 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3704 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3705 GEM_BUG_ON(start >= end);
3706 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3707 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
3708
3709 if (unlikely(range_overflows(start, size, end)))
3710 return -ENOSPC;
3711
3712 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3713 return -ENOSPC;
3714
3715 if (flags & PIN_HIGH) {
3716 search_flag = DRM_MM_SEARCH_BELOW;
3717 alloc_flag = DRM_MM_CREATE_TOP;
3718 } else {
3719 search_flag = DRM_MM_SEARCH_DEFAULT;
3720 alloc_flag = DRM_MM_CREATE_DEFAULT;
3721 }
3722
3723 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3724 * so we know that we always have a minimum alignment of 4096.
3725 * The drm_mm range manager is optimised to return results
3726 * with zero alignment, so where possible use the optimal
3727 * path.
3728 */
3729 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3730 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3731 alignment = 0;
3732
3733 err = drm_mm_insert_node_in_range_generic(&vm->mm, node,
3734 size, alignment, color,
3735 start, end,
3736 search_flag, alloc_flag);
3737 if (err != -ENOSPC)
3738 return err;
3739
Chris Wilson606fec92017-01-11 11:23:12 +00003740 /* No free space, pick a slot at random.
3741 *
3742 * There is a pathological case here using a GTT shared between
3743 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3744 *
3745 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3746 * (64k objects) (448k objects)
3747 *
3748 * Now imagine that the eviction LRU is ordered top-down (just because
3749 * pathology meets real life), and that we need to evict an object to
3750 * make room inside the aperture. The eviction scan then has to walk
3751 * the 448k list before it finds one within range. And now imagine that
3752 * it has to search for a new hole between every byte inside the memcpy,
3753 * for several simultaneous clients.
3754 *
3755 * On a full-ppgtt system, if we have run out of available space, there
3756 * will be lots and lots of objects in the eviction list! Again,
3757 * searching that LRU list may be slow if we are also applying any
3758 * range restrictions (e.g. restriction to low 4GiB) and so, for
3759 * simplicity and similarilty between different GTT, try the single
3760 * random replacement first.
3761 */
3762 offset = random_offset(start, end,
3763 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3764 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3765 if (err != -ENOSPC)
3766 return err;
3767
3768 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003769 err = i915_gem_evict_something(vm, size, alignment, color,
3770 start, end, flags);
3771 if (err)
3772 return err;
3773
3774 search_flag = DRM_MM_SEARCH_DEFAULT;
3775 return drm_mm_insert_node_in_range_generic(&vm->mm, node,
3776 size, alignment, color,
3777 start, end,
3778 search_flag, alloc_flag);
3779}