blob: 8a24bf006a822bf19fbb9c3666f6e07253cdd157 [file] [log] [blame]
Daniel Vetter76aaf222010-11-05 22:23:30 +01001/*
2 * Copyright © 2010 Daniel Vetter
Ben Widawskyc4ac5242014-02-19 22:05:47 -08003 * Copyright © 2011-2014 Intel Corporation
Daniel Vetter76aaf222010-11-05 22:23:30 +01004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
Chris Wilsonaae4a3d2017-02-13 17:15:44 +000026#include <linux/slab.h> /* fault-inject.h is not standalone! */
27
28#include <linux/fault-inject.h>
Chris Wilsone007b192017-01-11 11:23:10 +000029#include <linux/log2.h>
Chris Wilson606fec92017-01-11 11:23:12 +000030#include <linux/random.h>
Daniel Vetter0e46ce22014-01-08 16:10:27 +010031#include <linux/seq_file.h>
Chris Wilson5bab6f62015-10-23 18:43:32 +010032#include <linux/stop_machine.h>
Chris Wilsone007b192017-01-11 11:23:10 +000033
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drmP.h>
35#include <drm/i915_drm.h>
Chris Wilsone007b192017-01-11 11:23:10 +000036
Daniel Vetter76aaf222010-11-05 22:23:30 +010037#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080038#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010039#include "i915_trace.h"
40#include "intel_drv.h"
Chris Wilsond07f0e52016-10-28 13:58:44 +010041#include "intel_frontbuffer.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010042
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010043#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
44
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000045/**
46 * DOC: Global GTT views
47 *
48 * Background and previous state
49 *
50 * Historically objects could exists (be bound) in global GTT space only as
51 * singular instances with a view representing all of the object's backing pages
52 * in a linear fashion. This view will be called a normal view.
53 *
54 * To support multiple views of the same object, where the number of mapped
55 * pages is not equal to the backing store, or where the layout of the pages
56 * is not linear, concept of a GGTT view was added.
57 *
58 * One example of an alternative view is a stereo display driven by a single
59 * image. In this case we would have a framebuffer looking like this
60 * (2x2 pages):
61 *
62 * 12
63 * 34
64 *
65 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
66 * rendering. In contrast, fed to the display engine would be an alternative
67 * view which could look something like this:
68 *
69 * 1212
70 * 3434
71 *
72 * In this example both the size and layout of pages in the alternative view is
73 * different from the normal view.
74 *
75 * Implementation and usage
76 *
77 * GGTT views are implemented using VMAs and are distinguished via enum
78 * i915_ggtt_view_type and struct i915_ggtt_view.
79 *
80 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020081 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
82 * renaming in large amounts of code. They take the struct i915_ggtt_view
83 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000084 *
85 * As a helper for callers which are only interested in the normal view,
86 * globally const i915_ggtt_view_normal singleton instance exists. All old core
87 * GEM API functions, the ones not taking the view parameter, are operating on,
88 * or with the normal GGTT view.
89 *
90 * Code wanting to add or use a new GGTT view needs to:
91 *
92 * 1. Add a new enum with a suitable name.
93 * 2. Extend the metadata in the i915_ggtt_view structure if required.
94 * 3. Add support to i915_get_vma_pages().
95 *
96 * New views are required to build a scatter-gather table from within the
97 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
98 * exists for the lifetime of an VMA.
99 *
100 * Core API is designed to have copy semantics which means that passed in
101 * struct i915_ggtt_view does not need to be persistent (left around after
102 * calling the core API functions).
103 *
104 */
105
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200106static int
107i915_get_ggtt_vma_pages(struct i915_vma *vma);
108
Chris Wilson7c3f86b2017-01-12 11:00:49 +0000109static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
110{
111 /* Note that as an uncached mmio write, this should flush the
112 * WCB of the writes into the GGTT before it triggers the invalidate.
113 */
114 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
115}
116
117static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
118{
119 gen6_ggtt_invalidate(dev_priv);
120 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
121}
122
123static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
124{
125 intel_gtt_chipset_flush();
126}
127
128static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
129{
130 i915->ggtt.invalidate(i915);
131}
132
Chris Wilsonc0336662016-05-06 15:40:21 +0100133int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
134 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200135{
Chris Wilson1893a712014-09-19 11:56:27 +0100136 bool has_aliasing_ppgtt;
137 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100138 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100139
Michel Thierry9e1d0e62016-12-05 17:57:03 -0800140 has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
141 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
142 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100143
Zhi Wange320d402016-09-06 12:04:12 +0800144 if (intel_vgpu_active(dev_priv)) {
145 /* emulation is too hard */
146 has_full_ppgtt = false;
147 has_full_48bit_ppgtt = false;
148 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800149
Chris Wilson0e4ca102016-04-29 13:18:22 +0100150 if (!has_aliasing_ppgtt)
151 return 0;
152
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000153 /*
154 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
155 * execlists, the sole mechanism available to submit work.
156 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100157 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200158 return 0;
159
160 if (enable_ppgtt == 1)
161 return 1;
162
Chris Wilson1893a712014-09-19 11:56:27 +0100163 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200164 return 2;
165
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100166 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
167 return 3;
168
Daniel Vetter93a25a92014-03-06 09:40:43 +0100169#ifdef CONFIG_INTEL_IOMMU
170 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100171 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100172 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200173 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100174 }
175#endif
176
Jesse Barnes62942ed2014-06-13 09:28:33 -0700177 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100178 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700179 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
180 return 0;
181 }
182
Zhi Wange320d402016-09-06 12:04:12 +0800183 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100184 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000185 else
186 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100187}
188
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200189static int ppgtt_bind_vma(struct i915_vma *vma,
190 enum i915_cache_level cache_level,
191 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200192{
Chris Wilsonff685972017-02-15 08:43:42 +0000193 u32 pte_flags;
194 int ret;
195
196 trace_i915_va_alloc(vma);
197 ret = vma->vm->allocate_va_range(vma->vm, vma->node.start, vma->size);
198 if (ret)
199 return ret;
Daniel Vetter47552652015-04-14 17:35:24 +0200200
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100201 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100202
Daniel Vetter47552652015-04-14 17:35:24 +0200203 /* Currently applicable only to VLV */
Chris Wilsonff685972017-02-15 08:43:42 +0000204 pte_flags = 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200205 if (vma->obj->gt_ro)
206 pte_flags |= PTE_READ_ONLY;
207
Chris Wilson247177d2016-08-15 10:48:47 +0100208 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200209 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200210
211 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200212}
213
214static void ppgtt_unbind_vma(struct i915_vma *vma)
215{
Chris Wilsonff685972017-02-15 08:43:42 +0000216 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200217}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800218
Daniel Vetter2c642b02015-04-14 17:35:26 +0200219static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200220 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700221{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200222 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700223 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300224
225 switch (level) {
226 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800227 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300228 break;
229 case I915_CACHE_WT:
230 pte |= PPAT_DISPLAY_ELLC_INDEX;
231 break;
232 default:
233 pte |= PPAT_CACHED_INDEX;
234 break;
235 }
236
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700237 return pte;
238}
239
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300240static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
241 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800242{
Michel Thierry07749ef2015-03-16 16:00:54 +0000243 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800244 pde |= addr;
245 if (level != I915_CACHE_NONE)
246 pde |= PPAT_CACHED_PDE_INDEX;
247 else
248 pde |= PPAT_UNCACHED_INDEX;
249 return pde;
250}
251
Michel Thierry762d9932015-07-30 11:05:29 +0100252#define gen8_pdpe_encode gen8_pde_encode
253#define gen8_pml4e_encode gen8_pde_encode
254
Michel Thierry07749ef2015-03-16 16:00:54 +0000255static gen6_pte_t snb_pte_encode(dma_addr_t addr,
256 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200257 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700258{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200259 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700260 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700261
262 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100263 case I915_CACHE_L3_LLC:
264 case I915_CACHE_LLC:
265 pte |= GEN6_PTE_CACHE_LLC;
266 break;
267 case I915_CACHE_NONE:
268 pte |= GEN6_PTE_UNCACHED;
269 break;
270 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100271 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100272 }
273
274 return pte;
275}
276
Michel Thierry07749ef2015-03-16 16:00:54 +0000277static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
278 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200279 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100280{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200281 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100282 pte |= GEN6_PTE_ADDR_ENCODE(addr);
283
284 switch (level) {
285 case I915_CACHE_L3_LLC:
286 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700287 break;
288 case I915_CACHE_LLC:
289 pte |= GEN6_PTE_CACHE_LLC;
290 break;
291 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700292 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700293 break;
294 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100295 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700296 }
297
Ben Widawsky54d12522012-09-24 16:44:32 -0700298 return pte;
299}
300
Michel Thierry07749ef2015-03-16 16:00:54 +0000301static gen6_pte_t byt_pte_encode(dma_addr_t addr,
302 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200303 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700304{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200305 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700306 pte |= GEN6_PTE_ADDR_ENCODE(addr);
307
Akash Goel24f3a8c2014-06-17 10:59:42 +0530308 if (!(flags & PTE_READ_ONLY))
309 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700310
311 if (level != I915_CACHE_NONE)
312 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
313
314 return pte;
315}
316
Michel Thierry07749ef2015-03-16 16:00:54 +0000317static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
318 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200319 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700320{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200321 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700322 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700323
324 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700325 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700326
327 return pte;
328}
329
Michel Thierry07749ef2015-03-16 16:00:54 +0000330static gen6_pte_t iris_pte_encode(dma_addr_t addr,
331 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200332 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700333{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200334 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700335 pte |= HSW_PTE_ADDR_ENCODE(addr);
336
Chris Wilson651d7942013-08-08 14:41:10 +0100337 switch (level) {
338 case I915_CACHE_NONE:
339 break;
340 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000341 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100342 break;
343 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000344 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100345 break;
346 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700347
348 return pte;
349}
350
Chris Wilson84486612017-02-15 08:43:40 +0000351static struct page *vm_alloc_page(struct i915_address_space *vm, gfp_t gfp)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000352{
Chris Wilson84486612017-02-15 08:43:40 +0000353 struct page *page;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000354
Chris Wilson84486612017-02-15 08:43:40 +0000355 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
356 i915_gem_shrink_all(vm->i915);
Chris Wilsonaae4a3d2017-02-13 17:15:44 +0000357
Chris Wilson84486612017-02-15 08:43:40 +0000358 if (vm->free_pages.nr)
359 return vm->free_pages.pages[--vm->free_pages.nr];
360
361 page = alloc_page(gfp);
362 if (!page)
363 return NULL;
364
365 if (vm->pt_kmap_wc)
366 set_pages_array_wc(&page, 1);
367
368 return page;
369}
370
371static void vm_free_pages_release(struct i915_address_space *vm)
372{
373 GEM_BUG_ON(!pagevec_count(&vm->free_pages));
374
375 if (vm->pt_kmap_wc)
376 set_pages_array_wb(vm->free_pages.pages,
377 pagevec_count(&vm->free_pages));
378
379 __pagevec_release(&vm->free_pages);
380}
381
382static void vm_free_page(struct i915_address_space *vm, struct page *page)
383{
384 if (!pagevec_add(&vm->free_pages, page))
385 vm_free_pages_release(vm);
386}
387
388static int __setup_page_dma(struct i915_address_space *vm,
389 struct i915_page_dma *p,
390 gfp_t gfp)
391{
392 p->page = vm_alloc_page(vm, gfp | __GFP_NOWARN | __GFP_NORETRY);
393 if (unlikely(!p->page))
Michel Thierry1266cdb2015-03-24 17:06:33 +0000394 return -ENOMEM;
395
Chris Wilson84486612017-02-15 08:43:40 +0000396 p->daddr = dma_map_page(vm->dma, p->page, 0, PAGE_SIZE,
397 PCI_DMA_BIDIRECTIONAL);
398 if (unlikely(dma_mapping_error(vm->dma, p->daddr))) {
399 vm_free_page(vm, p->page);
400 return -ENOMEM;
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300401 }
402
Michel Thierry1266cdb2015-03-24 17:06:33 +0000403 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000404}
405
Chris Wilson84486612017-02-15 08:43:40 +0000406static int setup_page_dma(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000407 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300408{
Chris Wilson84486612017-02-15 08:43:40 +0000409 return __setup_page_dma(vm, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300410}
411
Chris Wilson84486612017-02-15 08:43:40 +0000412static void cleanup_page_dma(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000413 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300414{
Chris Wilson84486612017-02-15 08:43:40 +0000415 dma_unmap_page(vm->dma, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
416 vm_free_page(vm, p->page);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300417}
418
Chris Wilson9231da72017-02-15 08:43:41 +0000419#define kmap_atomic_px(px) kmap_atomic(px_base(px)->page)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300420
Chris Wilson84486612017-02-15 08:43:40 +0000421#define setup_px(vm, px) setup_page_dma((vm), px_base(px))
422#define cleanup_px(vm, px) cleanup_page_dma((vm), px_base(px))
423#define fill_px(ppgtt, px, v) fill_page_dma((vm), px_base(px), (v))
424#define fill32_px(ppgtt, px, v) fill_page_dma_32((vm), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300425
Chris Wilson84486612017-02-15 08:43:40 +0000426static void fill_page_dma(struct i915_address_space *vm,
427 struct i915_page_dma *p,
428 const u64 val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300429{
Chris Wilson9231da72017-02-15 08:43:41 +0000430 u64 * const vaddr = kmap_atomic(p->page);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300431 int i;
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300432
433 for (i = 0; i < 512; i++)
434 vaddr[i] = val;
435
Chris Wilson9231da72017-02-15 08:43:41 +0000436 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300437}
438
Chris Wilson84486612017-02-15 08:43:40 +0000439static void fill_page_dma_32(struct i915_address_space *vm,
440 struct i915_page_dma *p,
441 const u32 v)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300442{
Chris Wilson84486612017-02-15 08:43:40 +0000443 fill_page_dma(vm, p, (u64)v << 32 | v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300444}
445
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100446static int
Chris Wilson84486612017-02-15 08:43:40 +0000447setup_scratch_page(struct i915_address_space *vm, gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300448{
Chris Wilson84486612017-02-15 08:43:40 +0000449 return __setup_page_dma(vm, &vm->scratch_page, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300450}
451
Chris Wilson84486612017-02-15 08:43:40 +0000452static void cleanup_scratch_page(struct i915_address_space *vm)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300453{
Chris Wilson84486612017-02-15 08:43:40 +0000454 cleanup_page_dma(vm, &vm->scratch_page);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300455}
456
Chris Wilson84486612017-02-15 08:43:40 +0000457static struct i915_page_table *alloc_pt(struct i915_address_space *vm)
Ben Widawsky06fda602015-02-24 16:22:36 +0000458{
Michel Thierryec565b32015-04-08 12:13:23 +0100459 struct i915_page_table *pt;
Chris Wilson84486612017-02-15 08:43:40 +0000460 const size_t count = INTEL_GEN(vm->i915) >= 8 ? GEN8_PTES : GEN6_PTES;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000461 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000462
463 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
464 if (!pt)
465 return ERR_PTR(-ENOMEM);
466
Ben Widawsky678d96f2015-03-16 16:00:56 +0000467 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
468 GFP_KERNEL);
469
470 if (!pt->used_ptes)
471 goto fail_bitmap;
472
Chris Wilson84486612017-02-15 08:43:40 +0000473 ret = setup_px(vm, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000474 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300475 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000476
477 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000478
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300479fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000480 kfree(pt->used_ptes);
481fail_bitmap:
482 kfree(pt);
483
484 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000485}
486
Chris Wilson84486612017-02-15 08:43:40 +0000487static void free_pt(struct i915_address_space *vm, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000488{
Chris Wilson84486612017-02-15 08:43:40 +0000489 cleanup_px(vm, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300490 kfree(pt->used_ptes);
491 kfree(pt);
492}
493
494static void gen8_initialize_pt(struct i915_address_space *vm,
495 struct i915_page_table *pt)
496{
497 gen8_pte_t scratch_pte;
498
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100499 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200500 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300501
Chris Wilson84486612017-02-15 08:43:40 +0000502 fill_px(vm, pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300503}
504
505static void gen6_initialize_pt(struct i915_address_space *vm,
506 struct i915_page_table *pt)
507{
508 gen6_pte_t scratch_pte;
509
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100510 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300511
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100512 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200513 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300514
Chris Wilson84486612017-02-15 08:43:40 +0000515 fill32_px(vm, pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000516}
517
Chris Wilson84486612017-02-15 08:43:40 +0000518static struct i915_page_directory *alloc_pd(struct i915_address_space *vm)
Ben Widawsky06fda602015-02-24 16:22:36 +0000519{
Michel Thierryec565b32015-04-08 12:13:23 +0100520 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100521 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000522
523 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
524 if (!pd)
525 return ERR_PTR(-ENOMEM);
526
Michel Thierry33c88192015-04-08 12:13:33 +0100527 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
528 sizeof(*pd->used_pdes), GFP_KERNEL);
529 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300530 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100531
Chris Wilson84486612017-02-15 08:43:40 +0000532 ret = setup_px(vm, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100533 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300534 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100535
Ben Widawsky06fda602015-02-24 16:22:36 +0000536 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100537
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300538fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100539 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300540fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100541 kfree(pd);
542
543 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000544}
545
Chris Wilson84486612017-02-15 08:43:40 +0000546static void free_pd(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000547 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300548{
549 if (px_page(pd)) {
Chris Wilson84486612017-02-15 08:43:40 +0000550 cleanup_px(vm, pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300551 kfree(pd->used_pdes);
552 kfree(pd);
553 }
554}
555
556static void gen8_initialize_pd(struct i915_address_space *vm,
557 struct i915_page_directory *pd)
558{
559 gen8_pde_t scratch_pde;
560
561 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
562
Chris Wilson84486612017-02-15 08:43:40 +0000563 fill_px(vm, pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300564}
565
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000566static int __pdp_init(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100567 struct i915_page_directory_pointer *pdp)
568{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000569 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Michel Thierry6ac18502015-07-29 17:23:46 +0100570
571 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
572 sizeof(unsigned long),
573 GFP_KERNEL);
574 if (!pdp->used_pdpes)
575 return -ENOMEM;
576
577 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
578 GFP_KERNEL);
579 if (!pdp->page_directory) {
580 kfree(pdp->used_pdpes);
581 /* the PDP might be the statically allocated top level. Keep it
582 * as clean as possible */
583 pdp->used_pdpes = NULL;
584 return -ENOMEM;
585 }
586
587 return 0;
588}
589
590static void __pdp_fini(struct i915_page_directory_pointer *pdp)
591{
592 kfree(pdp->used_pdpes);
593 kfree(pdp->page_directory);
594 pdp->page_directory = NULL;
595}
596
Chris Wilson84486612017-02-15 08:43:40 +0000597static struct i915_page_directory_pointer *
598alloc_pdp(struct i915_address_space *vm)
Michel Thierry762d9932015-07-30 11:05:29 +0100599{
600 struct i915_page_directory_pointer *pdp;
601 int ret = -ENOMEM;
602
Chris Wilson84486612017-02-15 08:43:40 +0000603 WARN_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Michel Thierry762d9932015-07-30 11:05:29 +0100604
605 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
606 if (!pdp)
607 return ERR_PTR(-ENOMEM);
608
Chris Wilson84486612017-02-15 08:43:40 +0000609 ret = __pdp_init(vm->i915, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100610 if (ret)
611 goto fail_bitmap;
612
Chris Wilson84486612017-02-15 08:43:40 +0000613 ret = setup_px(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100614 if (ret)
615 goto fail_page_m;
616
617 return pdp;
618
619fail_page_m:
620 __pdp_fini(pdp);
621fail_bitmap:
622 kfree(pdp);
623
624 return ERR_PTR(ret);
625}
626
Chris Wilson84486612017-02-15 08:43:40 +0000627static void free_pdp(struct i915_address_space *vm,
Michel Thierry6ac18502015-07-29 17:23:46 +0100628 struct i915_page_directory_pointer *pdp)
629{
630 __pdp_fini(pdp);
Chris Wilson84486612017-02-15 08:43:40 +0000631 if (USES_FULL_48BIT_PPGTT(vm->i915)) {
632 cleanup_px(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100633 kfree(pdp);
634 }
635}
636
Michel Thierry69ab76f2015-07-29 17:23:55 +0100637static void gen8_initialize_pdp(struct i915_address_space *vm,
638 struct i915_page_directory_pointer *pdp)
639{
640 gen8_ppgtt_pdpe_t scratch_pdpe;
641
642 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
643
Chris Wilson84486612017-02-15 08:43:40 +0000644 fill_px(vm, pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100645}
646
647static void gen8_initialize_pml4(struct i915_address_space *vm,
648 struct i915_pml4 *pml4)
649{
650 gen8_ppgtt_pml4e_t scratch_pml4e;
651
652 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
653 I915_CACHE_LLC);
654
Chris Wilson84486612017-02-15 08:43:40 +0000655 fill_px(vm, pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100656}
657
Michel Thierry762d9932015-07-30 11:05:29 +0100658static void
Matthew Auld5c693b22016-12-13 16:05:10 +0000659gen8_setup_pdpe(struct i915_hw_ppgtt *ppgtt,
660 struct i915_page_directory_pointer *pdp,
661 struct i915_page_directory *pd,
662 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100663{
664 gen8_ppgtt_pdpe_t *page_directorypo;
665
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000666 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
Michel Thierry762d9932015-07-30 11:05:29 +0100667 return;
668
Chris Wilson9231da72017-02-15 08:43:41 +0000669 page_directorypo = kmap_atomic_px(pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100670 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
Chris Wilson9231da72017-02-15 08:43:41 +0000671 kunmap_atomic(page_directorypo);
Michel Thierry762d9932015-07-30 11:05:29 +0100672}
673
674static void
Chris Wilson84486612017-02-15 08:43:40 +0000675gen8_setup_pml4e(struct i915_pml4 *pml4,
Matthew Auld56843102016-12-13 16:05:11 +0000676 struct i915_page_directory_pointer *pdp,
677 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100678{
Chris Wilson9231da72017-02-15 08:43:41 +0000679 gen8_ppgtt_pml4e_t *pagemap = kmap_atomic_px(pml4);
Michel Thierry762d9932015-07-30 11:05:29 +0100680
Michel Thierry762d9932015-07-30 11:05:29 +0100681 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
Chris Wilson9231da72017-02-15 08:43:41 +0000682 kunmap_atomic(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{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000690 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000691 u32 *cs;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800692
693 BUG_ON(entry >= 4);
694
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000695 cs = intel_ring_begin(req, 6);
696 if (IS_ERR(cs))
697 return PTR_ERR(cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800698
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000699 *cs++ = MI_LOAD_REGISTER_IMM(1);
700 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, entry));
701 *cs++ = upper_32_bits(addr);
702 *cs++ = MI_LOAD_REGISTER_IMM(1);
703 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, entry));
704 *cs++ = lower_32_bits(addr);
705 intel_ring_advance(req, cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800706
707 return 0;
708}
709
Michel Thierry2dba3232015-07-30 11:06:23 +0100710static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
711 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800712{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800713 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800714
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100715 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300716 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
717
John Harrisone85b26d2015-05-29 17:43:56 +0100718 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800719 if (ret)
720 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800721 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800722
Ben Widawskyeeb94882013-12-06 14:11:10 -0800723 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800724}
725
Michel Thierry2dba3232015-07-30 11:06:23 +0100726static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
727 struct drm_i915_gem_request *req)
728{
729 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
730}
731
Mika Kuoppalafce93752016-10-31 17:24:46 +0200732/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
733 * the page table structures, we mark them dirty so that
734 * context switching/execlist queuing code takes extra steps
735 * to ensure that tlbs are flushed.
736 */
737static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
738{
Chris Wilson49d73912016-11-29 09:50:08 +0000739 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200740}
741
Michał Winiarski2ce51792016-10-13 14:02:42 +0200742/* Removes entries from a single page table, releasing it if it's empty.
743 * Caller can use the return value to update higher-level entries.
744 */
745static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200746 struct i915_page_table *pt,
747 uint64_t start,
748 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700749{
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200750 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200751 unsigned int pte = gen8_pte_index(start);
752 unsigned int pte_end = pte + num_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +0000753 const gen8_pte_t scratch_pte =
754 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
755 gen8_pte_t *vaddr;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200756
757 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200758 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700759
Mika Kuoppala37c63932016-11-01 15:27:36 +0200760 GEM_BUG_ON(pte_end > GEN8_PTES);
761
762 bitmap_clear(pt->used_ptes, pte, num_entries);
Zhi Wange81ecb52017-02-08 21:03:33 +0800763 if (USES_FULL_PPGTT(vm->i915)) {
764 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
765 return true;
766 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200767
Chris Wilson9231da72017-02-15 08:43:41 +0000768 vaddr = kmap_atomic_px(pt);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200769 while (pte < pte_end)
Chris Wilson894cceb2017-02-15 08:43:37 +0000770 vaddr[pte++] = scratch_pte;
Chris Wilson9231da72017-02-15 08:43:41 +0000771 kunmap_atomic(vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200772
773 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200774}
775
Michał Winiarski2ce51792016-10-13 14:02:42 +0200776/* Removes entries from a single page dir, releasing it if it's empty.
777 * Caller can use the return value to update higher-level entries
778 */
779static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200780 struct i915_page_directory *pd,
781 uint64_t start,
782 uint64_t length)
783{
784 struct i915_page_table *pt;
785 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200786 gen8_pde_t *pde_vaddr;
787 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
788 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200789
790 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000791 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100792 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000793
Michał Winiarski2ce51792016-10-13 14:02:42 +0200794 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
795 __clear_bit(pde, pd->used_pdes);
Chris Wilson9231da72017-02-15 08:43:41 +0000796 pde_vaddr = kmap_atomic_px(pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200797 pde_vaddr[pde] = scratch_pde;
Chris Wilson9231da72017-02-15 08:43:41 +0000798 kunmap_atomic(pde_vaddr);
Chris Wilson84486612017-02-15 08:43:40 +0000799 free_pt(vm, pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200800 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200801 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200802
Zhi Wanga18dbba2016-11-29 14:55:16 +0800803 if (bitmap_empty(pd->used_pdes, I915_PDES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200804 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200805
806 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200807}
Ben Widawsky06fda602015-02-24 16:22:36 +0000808
Michał Winiarski2ce51792016-10-13 14:02:42 +0200809/* Removes entries from a single page dir pointer, releasing it if it's empty.
810 * Caller can use the return value to update higher-level entries
811 */
812static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200813 struct i915_page_directory_pointer *pdp,
814 uint64_t start,
815 uint64_t length)
816{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200817 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200818 struct i915_page_directory *pd;
819 uint64_t pdpe;
820
821 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
822 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100823 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000824
Michał Winiarski2ce51792016-10-13 14:02:42 +0200825 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
826 __clear_bit(pdpe, pdp->used_pdpes);
Matthew Auld9e65a372016-12-13 16:05:12 +0000827 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
Chris Wilson84486612017-02-15 08:43:40 +0000828 free_pd(vm, pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200829 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200830 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200831
Mika Kuoppalafce93752016-10-31 17:24:46 +0200832 mark_tlbs_dirty(ppgtt);
833
Zhi Wanga18dbba2016-11-29 14:55:16 +0800834 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200835 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200836
837 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200838}
Ben Widawsky459108b2013-11-02 21:07:23 -0700839
Michał Winiarski2ce51792016-10-13 14:02:42 +0200840/* Removes entries from a single pml4.
841 * This is the top-level structure in 4-level page tables used on gen8+.
842 * Empty entries are always scratch pml4e.
843 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200844static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
845 struct i915_pml4 *pml4,
846 uint64_t start,
847 uint64_t length)
848{
849 struct i915_page_directory_pointer *pdp;
850 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200851
Chris Wilson49d73912016-11-29 09:50:08 +0000852 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700853
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200854 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
855 if (WARN_ON(!pml4->pdps[pml4e]))
856 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700857
Michał Winiarski2ce51792016-10-13 14:02:42 +0200858 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
859 __clear_bit(pml4e, pml4->used_pml4es);
Chris Wilson84486612017-02-15 08:43:40 +0000860 gen8_setup_pml4e(pml4, vm->scratch_pdp, pml4e);
861 free_pdp(vm, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200862 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700863 }
864}
865
Michel Thierryf9b5b782015-07-30 11:02:49 +0100866static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200867 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700868{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300869 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100870
Chris Wilsonc6385c92016-11-29 12:42:05 +0000871 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200872 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
873 else
874 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100875}
876
Chris Wilson894cceb2017-02-15 08:43:37 +0000877struct sgt_dma {
878 struct scatterlist *sg;
879 dma_addr_t dma, max;
880};
881
882static __always_inline bool
883gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100884 struct i915_page_directory_pointer *pdp,
Chris Wilson894cceb2017-02-15 08:43:37 +0000885 struct sgt_dma *iter,
886 u64 start,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100887 enum i915_cache_level cache_level)
888{
Chris Wilson894cceb2017-02-15 08:43:37 +0000889 unsigned int pdpe = gen8_pdpe_index(start);
890 unsigned int pde = gen8_pde_index(start);
891 unsigned int pte = gen8_pte_index(start);
892 struct i915_page_directory *pd;
893 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
894 gen8_pte_t *vaddr;
895 bool ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700896
Chris Wilson894cceb2017-02-15 08:43:37 +0000897 pd = pdp->page_directory[pdpe];
Chris Wilson9231da72017-02-15 08:43:41 +0000898 vaddr = kmap_atomic_px(pd->page_table[pde]);
Chris Wilson894cceb2017-02-15 08:43:37 +0000899 do {
900 vaddr[pte] = pte_encode | iter->dma;
901 iter->dma += PAGE_SIZE;
902 if (iter->dma >= iter->max) {
903 iter->sg = __sg_next(iter->sg);
904 if (!iter->sg) {
905 ret = false;
906 break;
907 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700908
Chris Wilson894cceb2017-02-15 08:43:37 +0000909 iter->dma = sg_dma_address(iter->sg);
910 iter->max = iter->dma + iter->sg->length;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000911 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800912
Michel Thierry07749ef2015-03-16 16:00:54 +0000913 if (++pte == GEN8_PTES) {
Michel Thierry07749ef2015-03-16 16:00:54 +0000914 if (++pde == I915_PDES) {
Chris Wilson894cceb2017-02-15 08:43:37 +0000915 /* Limited by sg length for 3lvl */
916 if (++pdpe == GEN8_PML4ES_PER_PML4) {
917 ret = true;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100918 break;
Chris Wilson894cceb2017-02-15 08:43:37 +0000919 }
920
921 GEM_BUG_ON(pdpe > GEN8_LEGACY_PDPES);
922 pd = pdp->page_directory[pdpe];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800923 pde = 0;
924 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000925
Chris Wilson9231da72017-02-15 08:43:41 +0000926 kunmap_atomic(vaddr);
927 vaddr = kmap_atomic_px(pd->page_table[pde]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800928 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700929 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000930 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +0000931 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300932
Chris Wilson894cceb2017-02-15 08:43:37 +0000933 return ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700934}
935
Chris Wilson894cceb2017-02-15 08:43:37 +0000936static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
937 struct sg_table *pages,
938 u64 start,
939 enum i915_cache_level cache_level,
940 u32 unused)
Michel Thierryf9b5b782015-07-30 11:02:49 +0100941{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300942 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilson894cceb2017-02-15 08:43:37 +0000943 struct sgt_dma iter = {
944 .sg = pages->sgl,
945 .dma = sg_dma_address(iter.sg),
946 .max = iter.dma + iter.sg->length,
947 };
Michel Thierryf9b5b782015-07-30 11:02:49 +0100948
Chris Wilson894cceb2017-02-15 08:43:37 +0000949 gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter,
950 start, cache_level);
951}
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100952
Chris Wilson894cceb2017-02-15 08:43:37 +0000953static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
954 struct sg_table *pages,
955 uint64_t start,
956 enum i915_cache_level cache_level,
957 u32 unused)
958{
959 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
960 struct sgt_dma iter = {
961 .sg = pages->sgl,
962 .dma = sg_dma_address(iter.sg),
963 .max = iter.dma + iter.sg->length,
964 };
965 struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
966 unsigned int pml4e = gen8_pml4e_index(start);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100967
Chris Wilson894cceb2017-02-15 08:43:37 +0000968 while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[pml4e++], &iter,
969 start, cache_level))
970 ;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100971}
972
Chris Wilson84486612017-02-15 08:43:40 +0000973static void gen8_free_page_tables(struct i915_address_space *vm,
Michel Thierryf37c0502015-06-10 17:46:39 +0100974 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800975{
976 int i;
977
Mika Kuoppala567047b2015-06-25 18:35:12 +0300978 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800979 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800980
Michel Thierry33c88192015-04-08 12:13:33 +0100981 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000982 if (WARN_ON(!pd->page_table[i]))
983 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800984
Chris Wilson84486612017-02-15 08:43:40 +0000985 free_pt(vm, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000986 pd->page_table[i] = NULL;
987 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000988}
989
Mika Kuoppala8776f022015-06-30 18:16:40 +0300990static int gen8_init_scratch(struct i915_address_space *vm)
991{
Matthew Auld64c050d2016-04-27 13:19:25 +0100992 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300993
Chris Wilson84486612017-02-15 08:43:40 +0000994 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100995 if (ret)
996 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300997
Chris Wilson84486612017-02-15 08:43:40 +0000998 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300999 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +01001000 ret = PTR_ERR(vm->scratch_pt);
1001 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001002 }
1003
Chris Wilson84486612017-02-15 08:43:40 +00001004 vm->scratch_pd = alloc_pd(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001005 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +01001006 ret = PTR_ERR(vm->scratch_pd);
1007 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001008 }
1009
Chris Wilson84486612017-02-15 08:43:40 +00001010 if (USES_FULL_48BIT_PPGTT(dev)) {
1011 vm->scratch_pdp = alloc_pdp(vm);
Michel Thierry69ab76f2015-07-29 17:23:55 +01001012 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +01001013 ret = PTR_ERR(vm->scratch_pdp);
1014 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +01001015 }
1016 }
1017
Mika Kuoppala8776f022015-06-30 18:16:40 +03001018 gen8_initialize_pt(vm, vm->scratch_pt);
1019 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001020 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +01001021 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001022
1023 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +01001024
1025free_pd:
Chris Wilson84486612017-02-15 08:43:40 +00001026 free_pd(vm, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +01001027free_pt:
Chris Wilson84486612017-02-15 08:43:40 +00001028 free_pt(vm, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +01001029free_scratch_page:
Chris Wilson84486612017-02-15 08:43:40 +00001030 cleanup_scratch_page(vm);
Matthew Auld64c050d2016-04-27 13:19:25 +01001031
1032 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001033}
1034
Zhiyuan Lv650da342015-08-28 15:41:18 +08001035static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1036{
1037 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +00001038 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001039 int i;
1040
Matthew Aulddf285642016-04-22 12:09:25 +01001041 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001042 u64 daddr = px_dma(&ppgtt->pml4);
1043
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001044 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1045 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001046
1047 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1048 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1049 } else {
1050 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1051 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1052
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001053 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1054 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001055 }
1056
1057 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1058 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1059 }
1060
1061 I915_WRITE(vgtif_reg(g2v_notify), msg);
1062
1063 return 0;
1064}
1065
Mika Kuoppala8776f022015-06-30 18:16:40 +03001066static void gen8_free_scratch(struct i915_address_space *vm)
1067{
Chris Wilson84486612017-02-15 08:43:40 +00001068 if (USES_FULL_48BIT_PPGTT(vm->i915))
1069 free_pdp(vm, vm->scratch_pdp);
1070 free_pd(vm, vm->scratch_pd);
1071 free_pt(vm, vm->scratch_pt);
1072 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001073}
1074
Chris Wilson84486612017-02-15 08:43:40 +00001075static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
Michel Thierry762d9932015-07-30 11:05:29 +01001076 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001077{
1078 int i;
1079
Chris Wilson84486612017-02-15 08:43:40 +00001080 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(vm->i915)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001081 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001082 continue;
1083
Chris Wilson84486612017-02-15 08:43:40 +00001084 gen8_free_page_tables(vm, pdp->page_directory[i]);
1085 free_pd(vm, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001086 }
Michel Thierry69876be2015-04-08 12:13:27 +01001087
Chris Wilson84486612017-02-15 08:43:40 +00001088 free_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001089}
1090
1091static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1092{
1093 int i;
1094
1095 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1096 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1097 continue;
1098
Chris Wilson84486612017-02-15 08:43:40 +00001099 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001100 }
1101
Chris Wilson84486612017-02-15 08:43:40 +00001102 cleanup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001103}
1104
1105static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1106{
Chris Wilson49d73912016-11-29 09:50:08 +00001107 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001108 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001109
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001110 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001111 gen8_ppgtt_notify_vgt(ppgtt, false);
1112
Chris Wilson84486612017-02-15 08:43:40 +00001113 if (!USES_FULL_48BIT_PPGTT(vm->i915))
1114 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001115 else
1116 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001117
Mika Kuoppala8776f022015-06-30 18:16:40 +03001118 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001119}
1120
Michel Thierryd7b26332015-04-08 12:13:34 +01001121/**
1122 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001123 * @vm: Master vm structure.
1124 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001125 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001126 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001127 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1128 * caller to free on error.
1129 *
1130 * Allocate the required number of page tables. Extremely similar to
1131 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1132 * the page directory boundary (instead of the page directory pointer). That
1133 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1134 * possible, and likely that the caller will need to use multiple calls of this
1135 * function to achieve the appropriate allocation.
1136 *
1137 * Return: 0 if success; negative error code otherwise.
1138 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001139static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001140 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001141 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001142 uint64_t length,
1143 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001144{
Michel Thierryd7b26332015-04-08 12:13:34 +01001145 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001146 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001147
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001148 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001149 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001150 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001151 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001152 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001153 continue;
1154 }
1155
Chris Wilson84486612017-02-15 08:43:40 +00001156 pt = alloc_pt(vm);
Michel Thierryd7b26332015-04-08 12:13:34 +01001157 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001158 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001159
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001160 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001161 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001162 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001163 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001164 }
1165
1166 return 0;
1167
1168unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001169 for_each_set_bit(pde, new_pts, I915_PDES)
Chris Wilson84486612017-02-15 08:43:40 +00001170 free_pt(vm, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001171
1172 return -ENOMEM;
1173}
1174
Michel Thierryd7b26332015-04-08 12:13:34 +01001175/**
1176 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001177 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001178 * @pdp: Page directory pointer for this address range.
1179 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001180 * @length: Size of the allocations.
1181 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001182 * caller to free on error.
1183 *
1184 * Allocate the required number of page directories starting at the pde index of
1185 * @start, and ending at the pde index @start + @length. This function will skip
1186 * over already allocated page directories within the range, and only allocate
1187 * new ones, setting the appropriate pointer within the pdp as well as the
1188 * correct position in the bitmap @new_pds.
1189 *
1190 * The function will only allocate the pages within the range for a give page
1191 * directory pointer. In other words, if @start + @length straddles a virtually
1192 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1193 * required by the caller, This is not currently possible, and the BUG in the
1194 * code will prevent it.
1195 *
1196 * Return: 0 if success; negative error code otherwise.
1197 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001198static int
1199gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1200 struct i915_page_directory_pointer *pdp,
1201 uint64_t start,
1202 uint64_t length,
1203 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001204{
Michel Thierryd7b26332015-04-08 12:13:34 +01001205 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001206 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001207 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001208
Michel Thierry6ac18502015-07-29 17:23:46 +01001209 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001210
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001211 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001212 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001213 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001214
Chris Wilson84486612017-02-15 08:43:40 +00001215 pd = alloc_pd(vm);
Michel Thierryd7b26332015-04-08 12:13:34 +01001216 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001217 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001218
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001219 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001220 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001221 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001222 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001223 }
1224
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001225 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001226
1227unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001228 for_each_set_bit(pdpe, new_pds, pdpes)
Chris Wilson84486612017-02-15 08:43:40 +00001229 free_pd(vm, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001230
1231 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001232}
1233
Michel Thierry762d9932015-07-30 11:05:29 +01001234/**
1235 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1236 * @vm: Master vm structure.
1237 * @pml4: Page map level 4 for this address range.
1238 * @start: Starting virtual address to begin allocations.
1239 * @length: Size of the allocations.
1240 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1241 * caller to free on error.
1242 *
1243 * Allocate the required number of page directory pointers. Extremely similar to
1244 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1245 * The main difference is here we are limited by the pml4 boundary (instead of
1246 * the page directory pointer).
1247 *
1248 * Return: 0 if success; negative error code otherwise.
1249 */
1250static int
1251gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1252 struct i915_pml4 *pml4,
1253 uint64_t start,
1254 uint64_t length,
1255 unsigned long *new_pdps)
1256{
Michel Thierry762d9932015-07-30 11:05:29 +01001257 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001258 uint32_t pml4e;
1259
1260 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1261
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001262 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001263 if (!test_bit(pml4e, pml4->used_pml4es)) {
Chris Wilson84486612017-02-15 08:43:40 +00001264 pdp = alloc_pdp(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001265 if (IS_ERR(pdp))
1266 goto unwind_out;
1267
Michel Thierry69ab76f2015-07-29 17:23:55 +01001268 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001269 pml4->pdps[pml4e] = pdp;
1270 __set_bit(pml4e, new_pdps);
1271 trace_i915_page_directory_pointer_entry_alloc(vm,
1272 pml4e,
1273 start,
1274 GEN8_PML4E_SHIFT);
1275 }
1276 }
1277
1278 return 0;
1279
1280unwind_out:
1281 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson84486612017-02-15 08:43:40 +00001282 free_pdp(vm, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001283
1284 return -ENOMEM;
1285}
1286
Michel Thierryd7b26332015-04-08 12:13:34 +01001287static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001288free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001289{
Michel Thierryd7b26332015-04-08 12:13:34 +01001290 kfree(new_pts);
1291 kfree(new_pds);
1292}
1293
1294/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1295 * of these are based on the number of PDPEs in the system.
1296 */
1297static
1298int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001299 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001300 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001301{
Michel Thierryd7b26332015-04-08 12:13:34 +01001302 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001303 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001304
Michał Winiarski3a41a052015-09-03 19:22:18 +02001305 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001306 if (!pds)
1307 return -ENOMEM;
1308
Michał Winiarski3a41a052015-09-03 19:22:18 +02001309 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1310 GFP_TEMPORARY);
1311 if (!pts)
1312 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001313
1314 *new_pds = pds;
1315 *new_pts = pts;
1316
1317 return 0;
1318
1319err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001320 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001321 return -ENOMEM;
1322}
1323
Michel Thierry762d9932015-07-30 11:05:29 +01001324static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1325 struct i915_page_directory_pointer *pdp,
1326 uint64_t start,
1327 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001328{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001329 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001330 unsigned long *new_page_dirs, *new_page_tables;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001331 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001332 const uint64_t orig_start = start;
1333 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001334 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001335 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001336 int ret;
1337
Michel Thierry6ac18502015-07-29 17:23:46 +01001338 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001339 if (ret)
1340 return ret;
1341
Michel Thierryd7b26332015-04-08 12:13:34 +01001342 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001343 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1344 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001345 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001346 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001347 return ret;
1348 }
1349
1350 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001351 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001352 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001353 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001354 if (ret)
1355 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001356 }
1357
Michel Thierry33c88192015-04-08 12:13:33 +01001358 start = orig_start;
1359 length = orig_length;
1360
Michel Thierryd7b26332015-04-08 12:13:34 +01001361 /* Allocations have completed successfully, so set the bitmaps, and do
1362 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001363 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilson9231da72017-02-15 08:43:41 +00001364 gen8_pde_t *const page_directory = kmap_atomic_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001365 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001366 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001367 uint64_t pd_start = start;
1368 uint32_t pde;
1369
Michel Thierryd7b26332015-04-08 12:13:34 +01001370 /* Every pd should be allocated, we just did that above. */
1371 WARN_ON(!pd);
1372
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001373 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001374 /* Same reasoning as pd */
1375 WARN_ON(!pt);
1376 WARN_ON(!pd_len);
1377 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1378
1379 /* Set our used ptes within the page table */
1380 bitmap_set(pt->used_ptes,
1381 gen8_pte_index(pd_start),
1382 gen8_pte_count(pd_start, pd_len));
1383
1384 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001385 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001386
1387 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001388 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1389 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001390 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1391 gen8_pte_index(start),
1392 gen8_pte_count(start, length),
1393 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001394
1395 /* NB: We haven't yet mapped ptes to pages. At this
1396 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001397 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001398
Chris Wilson9231da72017-02-15 08:43:41 +00001399 kunmap_atomic(page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001400 __set_bit(pdpe, pdp->used_pdpes);
Matthew Auld5c693b22016-12-13 16:05:10 +00001401 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001402 }
1403
Michał Winiarski3a41a052015-09-03 19:22:18 +02001404 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001405 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001406 return 0;
1407
1408err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001409 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001410 unsigned long temp;
1411
Michał Winiarski3a41a052015-09-03 19:22:18 +02001412 for_each_set_bit(temp, new_page_tables + pdpe *
1413 BITS_TO_LONGS(I915_PDES), I915_PDES)
Chris Wilson84486612017-02-15 08:43:40 +00001414 free_pt(vm, pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001415 }
1416
Michel Thierry6ac18502015-07-29 17:23:46 +01001417 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Chris Wilson84486612017-02-15 08:43:40 +00001418 free_pd(vm, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001419
Michał Winiarski3a41a052015-09-03 19:22:18 +02001420 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001421 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001422 return ret;
1423}
1424
Michel Thierry762d9932015-07-30 11:05:29 +01001425static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1426 struct i915_pml4 *pml4,
1427 uint64_t start,
1428 uint64_t length)
1429{
1430 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Michel Thierry762d9932015-07-30 11:05:29 +01001431 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001432 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001433 int ret = 0;
1434
1435 /* Do the pml4 allocations first, so we don't need to track the newly
1436 * allocated tables below the pdp */
1437 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1438
1439 /* The pagedirectory and pagetable allocations are done in the shared 3
1440 * and 4 level code. Just allocate the pdps.
1441 */
1442 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1443 new_pdps);
1444 if (ret)
1445 return ret;
1446
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001447 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001448 WARN_ON(!pdp);
1449
1450 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1451 if (ret)
1452 goto err_out;
1453
Chris Wilson84486612017-02-15 08:43:40 +00001454 gen8_setup_pml4e(pml4, pdp, pml4e);
Michel Thierry762d9932015-07-30 11:05:29 +01001455 }
1456
1457 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1458 GEN8_PML4ES_PER_PML4);
1459
1460 return 0;
1461
1462err_out:
1463 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson84486612017-02-15 08:43:40 +00001464 gen8_ppgtt_cleanup_3lvl(vm, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001465
1466 return ret;
1467}
1468
1469static int gen8_alloc_va_range(struct i915_address_space *vm,
1470 uint64_t start, uint64_t length)
1471{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001472 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001473
Chris Wilsonc6385c92016-11-29 12:42:05 +00001474 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001475 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1476 else
1477 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1478}
1479
Chris Wilson84486612017-02-15 08:43:40 +00001480static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
1481 struct i915_page_directory_pointer *pdp,
Michel Thierryea91e402015-07-29 17:23:57 +01001482 uint64_t start, uint64_t length,
1483 gen8_pte_t scratch_pte,
1484 struct seq_file *m)
1485{
1486 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001487 uint32_t pdpe;
1488
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001489 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001490 struct i915_page_table *pt;
1491 uint64_t pd_len = length;
1492 uint64_t pd_start = start;
1493 uint32_t pde;
1494
1495 if (!test_bit(pdpe, pdp->used_pdpes))
1496 continue;
1497
1498 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001499 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001500 uint32_t pte;
1501 gen8_pte_t *pt_vaddr;
1502
1503 if (!test_bit(pde, pd->used_pdes))
1504 continue;
1505
Chris Wilson9231da72017-02-15 08:43:41 +00001506 pt_vaddr = kmap_atomic_px(pt);
Michel Thierryea91e402015-07-29 17:23:57 +01001507 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1508 uint64_t va =
1509 (pdpe << GEN8_PDPE_SHIFT) |
1510 (pde << GEN8_PDE_SHIFT) |
1511 (pte << GEN8_PTE_SHIFT);
1512 int i;
1513 bool found = false;
1514
1515 for (i = 0; i < 4; i++)
1516 if (pt_vaddr[pte + i] != scratch_pte)
1517 found = true;
1518 if (!found)
1519 continue;
1520
1521 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1522 for (i = 0; i < 4; i++) {
1523 if (pt_vaddr[pte + i] != scratch_pte)
1524 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1525 else
1526 seq_puts(m, " SCRATCH ");
1527 }
1528 seq_puts(m, "\n");
1529 }
Michel Thierryea91e402015-07-29 17:23:57 +01001530 kunmap_atomic(pt_vaddr);
1531 }
1532 }
1533}
1534
1535static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1536{
1537 struct i915_address_space *vm = &ppgtt->base;
1538 uint64_t start = ppgtt->base.start;
1539 uint64_t length = ppgtt->base.total;
Chris Wilson894cceb2017-02-15 08:43:37 +00001540 const gen8_pte_t scratch_pte =
1541 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001542
Chris Wilsonc6385c92016-11-29 12:42:05 +00001543 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Chris Wilson84486612017-02-15 08:43:40 +00001544 gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001545 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001546 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001547 struct i915_pml4 *pml4 = &ppgtt->pml4;
1548 struct i915_page_directory_pointer *pdp;
1549
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001550 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001551 if (!test_bit(pml4e, pml4->used_pml4es))
1552 continue;
1553
1554 seq_printf(m, " PML4E #%llu\n", pml4e);
Chris Wilson84486612017-02-15 08:43:40 +00001555 gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001556 }
1557 }
1558}
1559
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001560static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1561{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001562 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001563 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001564 int ret;
1565
1566 /* We allocate temp bitmap for page tables for no gain
1567 * but as this is for init only, lets keep the things simple
1568 */
1569 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1570 if (ret)
1571 return ret;
1572
1573 /* Allocate for all pdps regardless of how the ppgtt
1574 * was defined.
1575 */
1576 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1577 0, 1ULL << 32,
1578 new_page_dirs);
1579 if (!ret)
1580 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1581
Michał Winiarski3a41a052015-09-03 19:22:18 +02001582 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001583
1584 return ret;
1585}
1586
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001587/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001588 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1589 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1590 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1591 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001592 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001593 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001594static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001595{
Chris Wilson49d73912016-11-29 09:50:08 +00001596 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001597 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001598
Mika Kuoppala8776f022015-06-30 18:16:40 +03001599 ret = gen8_init_scratch(&ppgtt->base);
1600 if (ret)
1601 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001602
Michel Thierryd7b26332015-04-08 12:13:34 +01001603 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001604 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001605 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001606 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001607 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1608 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001609 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001610
Chris Wilson84486612017-02-15 08:43:40 +00001611 /* There are only few exceptions for gen >=6. chv and bxt.
1612 * And we are not sure about the latter so play safe for now.
1613 */
1614 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
1615 ppgtt->base.pt_kmap_wc = true;
1616
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001617 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Chris Wilson84486612017-02-15 08:43:40 +00001618 ret = setup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001619 if (ret)
1620 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001621
Michel Thierry69ab76f2015-07-29 17:23:55 +01001622 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1623
Michel Thierry762d9932015-07-30 11:05:29 +01001624 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001625 ppgtt->switch_mm = gen8_48b_mm_switch;
Chris Wilson894cceb2017-02-15 08:43:37 +00001626
1627 ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
Michel Thierry762d9932015-07-30 11:05:29 +01001628 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001629 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001630 if (ret)
1631 goto free_scratch;
1632
1633 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001634 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001635 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1636 0, 0,
1637 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001638
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001639 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001640 ret = gen8_preallocate_top_level_pdps(ppgtt);
1641 if (ret)
1642 goto free_scratch;
1643 }
Chris Wilson894cceb2017-02-15 08:43:37 +00001644
1645 ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001646 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001647
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001648 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001649 gen8_ppgtt_notify_vgt(ppgtt, true);
1650
Michel Thierryd7b26332015-04-08 12:13:34 +01001651 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001652
1653free_scratch:
1654 gen8_free_scratch(&ppgtt->base);
1655 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001656}
1657
Ben Widawsky87d60b62013-12-06 14:11:29 -08001658static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1659{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001660 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001661 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001662 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001663 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001664 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001665 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001666
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001667 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001668 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001669
Dave Gordon731f74c2016-06-24 19:37:46 +01001670 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001671 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001672 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001673 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001674 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001675 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1676
1677 if (pd_entry != expected)
1678 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1679 pde,
1680 pd_entry,
1681 expected);
1682 seq_printf(m, "\tPDE: %x\n", pd_entry);
1683
Chris Wilson9231da72017-02-15 08:43:41 +00001684 pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[pde]);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001685
Michel Thierry07749ef2015-03-16 16:00:54 +00001686 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001687 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001688 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001689 (pte * PAGE_SIZE);
1690 int i;
1691 bool found = false;
1692 for (i = 0; i < 4; i++)
1693 if (pt_vaddr[pte + i] != scratch_pte)
1694 found = true;
1695 if (!found)
1696 continue;
1697
1698 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1699 for (i = 0; i < 4; i++) {
1700 if (pt_vaddr[pte + i] != scratch_pte)
1701 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1702 else
1703 seq_puts(m, " SCRATCH ");
1704 }
1705 seq_puts(m, "\n");
1706 }
Chris Wilson9231da72017-02-15 08:43:41 +00001707 kunmap_atomic(pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001708 }
1709}
1710
Ben Widawsky678d96f2015-03-16 16:00:56 +00001711/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001712static void gen6_write_pde(struct i915_page_directory *pd,
1713 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001714{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001715 /* Caller needs to make sure the write completes if necessary */
1716 struct i915_hw_ppgtt *ppgtt =
1717 container_of(pd, struct i915_hw_ppgtt, pd);
1718 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001719
Mika Kuoppala567047b2015-06-25 18:35:12 +03001720 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001721 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001722
Ben Widawsky678d96f2015-03-16 16:00:56 +00001723 writel(pd_entry, ppgtt->pd_addr + pde);
1724}
Ben Widawsky61973492013-04-08 18:43:54 -07001725
Ben Widawsky678d96f2015-03-16 16:00:56 +00001726/* Write all the page tables found in the ppgtt structure to incrementing page
1727 * directories. */
1728static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001729 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001730 uint32_t start, uint32_t length)
1731{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001732 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001733 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001734 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001735
Dave Gordon731f74c2016-06-24 19:37:46 +01001736 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001737 gen6_write_pde(pd, pde, pt);
1738
1739 /* Make sure write is complete before other code can use this page
1740 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001741 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001742}
1743
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001744static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001745{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001746 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001747
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001748 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001749}
Ben Widawsky61973492013-04-08 18:43:54 -07001750
Ben Widawsky90252e52013-12-06 14:11:12 -08001751static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001752 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001753{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001754 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001755 u32 *cs;
Ben Widawsky90252e52013-12-06 14:11:12 -08001756 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001757
Ben Widawsky90252e52013-12-06 14:11:12 -08001758 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001759 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001760 if (ret)
1761 return ret;
1762
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001763 cs = intel_ring_begin(req, 6);
1764 if (IS_ERR(cs))
1765 return PTR_ERR(cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001766
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001767 *cs++ = MI_LOAD_REGISTER_IMM(2);
1768 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1769 *cs++ = PP_DIR_DCLV_2G;
1770 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1771 *cs++ = get_pd_offset(ppgtt);
1772 *cs++ = MI_NOOP;
1773 intel_ring_advance(req, cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001774
1775 return 0;
1776}
1777
Ben Widawsky48a10382013-12-06 14:11:11 -08001778static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001779 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001780{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001781 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001782 u32 *cs;
Ben Widawsky48a10382013-12-06 14:11:11 -08001783 int ret;
1784
Ben Widawsky48a10382013-12-06 14:11:11 -08001785 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001786 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001787 if (ret)
1788 return ret;
1789
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001790 cs = intel_ring_begin(req, 6);
1791 if (IS_ERR(cs))
1792 return PTR_ERR(cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001793
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001794 *cs++ = MI_LOAD_REGISTER_IMM(2);
1795 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1796 *cs++ = PP_DIR_DCLV_2G;
1797 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1798 *cs++ = get_pd_offset(ppgtt);
1799 *cs++ = MI_NOOP;
1800 intel_ring_advance(req, cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001801
Ben Widawsky90252e52013-12-06 14:11:12 -08001802 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001803 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001804 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001805 if (ret)
1806 return ret;
1807 }
1808
Ben Widawsky48a10382013-12-06 14:11:11 -08001809 return 0;
1810}
1811
Ben Widawskyeeb94882013-12-06 14:11:10 -08001812static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001813 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001814{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001815 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001816 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001817
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001818 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1819 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001820 return 0;
1821}
1822
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001823static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001824{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001825 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301826 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001827
Akash Goel3b3f1652016-10-13 22:44:48 +05301828 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001829 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1830 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001831 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001832 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001833 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001834}
1835
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001836static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001837{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001838 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001839 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301840 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001841
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001842 ecobits = I915_READ(GAC_ECO_BITS);
1843 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1844
1845 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001846 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001847 ecochk |= ECOCHK_PPGTT_WB_HSW;
1848 } else {
1849 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1850 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1851 }
1852 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001853
Akash Goel3b3f1652016-10-13 22:44:48 +05301854 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001855 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001856 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001857 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001858 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001859}
1860
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001861static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001862{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001863 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001864
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001865 ecobits = I915_READ(GAC_ECO_BITS);
1866 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1867 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001868
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001869 gab_ctl = I915_READ(GAB_CTL);
1870 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001871
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001872 ecochk = I915_READ(GAM_ECOCHK);
1873 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001874
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001875 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001876}
1877
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001878/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001879static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001880 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001881 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001882{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001883 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001884 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001885 unsigned first_entry = start >> PAGE_SHIFT;
1886 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001887 unsigned act_pt = first_entry / GEN6_PTES;
1888 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001889 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001890
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001891 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001892 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001893
Daniel Vetter7bddb012012-02-09 17:15:47 +01001894 while (num_entries) {
1895 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001896 if (last_pte > GEN6_PTES)
1897 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001898
Chris Wilson9231da72017-02-15 08:43:41 +00001899 pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001900
1901 for (i = first_pte; i < last_pte; i++)
1902 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001903
Chris Wilson9231da72017-02-15 08:43:41 +00001904 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001905
Daniel Vetter7bddb012012-02-09 17:15:47 +01001906 num_entries -= last_pte - first_pte;
1907 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001908 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001909 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001910}
1911
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001912static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001913 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001914 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301915 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001916{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001917 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001918 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001919 unsigned act_pt = first_entry / GEN6_PTES;
1920 unsigned act_pte = first_entry % GEN6_PTES;
Chris Wilsonb31144c2017-02-15 08:43:36 +00001921 const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1922 struct sgt_dma iter;
1923 gen6_pte_t *vaddr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001924
Chris Wilson9231da72017-02-15 08:43:41 +00001925 vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
Chris Wilsonb31144c2017-02-15 08:43:36 +00001926 iter.sg = pages->sgl;
1927 iter.dma = sg_dma_address(iter.sg);
1928 iter.max = iter.dma + iter.sg->length;
1929 do {
1930 vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001931
Chris Wilsonb31144c2017-02-15 08:43:36 +00001932 iter.dma += PAGE_SIZE;
1933 if (iter.dma == iter.max) {
1934 iter.sg = __sg_next(iter.sg);
1935 if (!iter.sg)
1936 break;
1937
1938 iter.dma = sg_dma_address(iter.sg);
1939 iter.max = iter.dma + iter.sg->length;
1940 }
Akash Goel24f3a8c2014-06-17 10:59:42 +05301941
Michel Thierry07749ef2015-03-16 16:00:54 +00001942 if (++act_pte == GEN6_PTES) {
Chris Wilson9231da72017-02-15 08:43:41 +00001943 kunmap_atomic(vaddr);
1944 vaddr = kmap_atomic_px(ppgtt->pd.page_table[++act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +02001945 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001946 }
Chris Wilsonb31144c2017-02-15 08:43:36 +00001947 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +00001948 kunmap_atomic(vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001949}
1950
Ben Widawsky678d96f2015-03-16 16:00:56 +00001951static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001952 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001953{
Michel Thierry4933d512015-03-24 15:46:22 +00001954 DECLARE_BITMAP(new_page_tables, I915_PDES);
Chris Wilson49d73912016-11-29 09:50:08 +00001955 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001956 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001957 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001958 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001959 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001960 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001961 int ret;
1962
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001963 start = start_save = start_in;
1964 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001965
1966 bitmap_zero(new_page_tables, I915_PDES);
1967
1968 /* The allocation is done in two stages so that we can bail out with
1969 * minimal amount of pain. The first stage finds new page tables that
1970 * need allocation. The second stage marks use ptes within the page
1971 * tables.
1972 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001973 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001974 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001975 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1976 continue;
1977 }
1978
1979 /* We've already allocated a page table */
1980 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1981
Chris Wilson84486612017-02-15 08:43:40 +00001982 pt = alloc_pt(vm);
Michel Thierry4933d512015-03-24 15:46:22 +00001983 if (IS_ERR(pt)) {
1984 ret = PTR_ERR(pt);
1985 goto unwind_out;
1986 }
1987
1988 gen6_initialize_pt(vm, pt);
1989
1990 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001991 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001992 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001993 }
1994
1995 start = start_save;
1996 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001997
Dave Gordon731f74c2016-06-24 19:37:46 +01001998 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001999 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
2000
2001 bitmap_zero(tmp_bitmap, GEN6_PTES);
2002 bitmap_set(tmp_bitmap, gen6_pte_index(start),
2003 gen6_pte_count(start, length));
2004
Mika Kuoppala966082c2015-06-25 18:35:19 +03002005 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00002006 gen6_write_pde(&ppgtt->pd, pde, pt);
2007
Michel Thierry72744cb2015-03-24 15:46:23 +00002008 trace_i915_page_table_entry_map(vm, pde, pt,
2009 gen6_pte_index(start),
2010 gen6_pte_count(start, length),
2011 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00002012 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00002013 GEN6_PTES);
2014 }
2015
Michel Thierry4933d512015-03-24 15:46:22 +00002016 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
2017
2018 /* Make sure write is complete before other code can use this page
2019 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002020 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00002021
Ben Widawsky563222a2015-03-19 12:53:28 +00002022 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002023 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00002024
2025unwind_out:
2026 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002027 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002028
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002029 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Chris Wilson84486612017-02-15 08:43:40 +00002030 free_pt(vm, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002031 }
2032
2033 mark_tlbs_dirty(ppgtt);
2034 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002035}
2036
Mika Kuoppala8776f022015-06-30 18:16:40 +03002037static int gen6_init_scratch(struct i915_address_space *vm)
2038{
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002039 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002040
Chris Wilson84486612017-02-15 08:43:40 +00002041 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002042 if (ret)
2043 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002044
Chris Wilson84486612017-02-15 08:43:40 +00002045 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002046 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson84486612017-02-15 08:43:40 +00002047 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002048 return PTR_ERR(vm->scratch_pt);
2049 }
2050
2051 gen6_initialize_pt(vm, vm->scratch_pt);
2052
2053 return 0;
2054}
2055
2056static void gen6_free_scratch(struct i915_address_space *vm)
2057{
Chris Wilson84486612017-02-15 08:43:40 +00002058 free_pt(vm, vm->scratch_pt);
2059 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002060}
2061
Daniel Vetter061dd492015-04-14 17:35:13 +02002062static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002063{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002064 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002065 struct i915_page_directory *pd = &ppgtt->pd;
Michel Thierry09942c62015-04-08 12:13:30 +01002066 struct i915_page_table *pt;
2067 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002068
Daniel Vetter061dd492015-04-14 17:35:13 +02002069 drm_mm_remove_node(&ppgtt->node);
2070
Dave Gordon731f74c2016-06-24 19:37:46 +01002071 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002072 if (pt != vm->scratch_pt)
Chris Wilson84486612017-02-15 08:43:40 +00002073 free_pt(vm, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002074
Mika Kuoppala8776f022015-06-30 18:16:40 +03002075 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002076}
2077
Ben Widawskyb1465202014-02-19 22:05:49 -08002078static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002079{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002080 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00002081 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002082 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002083 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002084
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002085 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2086 * allocator works in address space sizes, so it's multiplied by page
2087 * size. We allocate at the top of the GTT to avoid fragmentation.
2088 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002089 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002090
Mika Kuoppala8776f022015-06-30 18:16:40 +03002091 ret = gen6_init_scratch(vm);
2092 if (ret)
2093 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002094
Chris Wilsone007b192017-01-11 11:23:10 +00002095 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2096 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2097 I915_COLOR_UNEVICTABLE,
2098 0, ggtt->base.total,
2099 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00002100 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002101 goto err_out;
2102
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002103 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002104 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002105
Ben Widawskyc8c26622015-01-22 17:01:25 +00002106 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002107
2108err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002109 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002110 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002111}
2112
Ben Widawskyb1465202014-02-19 22:05:49 -08002113static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2114{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002115 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002116}
2117
Michel Thierry4933d512015-03-24 15:46:22 +00002118static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2119 uint64_t start, uint64_t length)
2120{
Michel Thierryec565b32015-04-08 12:13:23 +01002121 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002122 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002123
Dave Gordon731f74c2016-06-24 19:37:46 +01002124 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002125 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002126}
2127
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002128static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002129{
Chris Wilson49d73912016-11-29 09:50:08 +00002130 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002131 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002132 int ret;
2133
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002134 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002135 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002136 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002137 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002138 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002139 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002140 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002141 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002142 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002143
2144 ret = gen6_ppgtt_alloc(ppgtt);
2145 if (ret)
2146 return ret;
2147
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002148 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002149 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2150 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002151 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2152 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002153 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002154 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002155 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002156 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002157
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002158 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002159 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002160
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002161 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002162 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002163
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002164 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002165
Ben Widawsky678d96f2015-03-16 16:00:56 +00002166 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2167
Thierry Reding440fd522015-01-23 09:05:06 +01002168 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002169 ppgtt->node.size >> 20,
2170 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002171
Daniel Vetterfa76da32014-08-06 20:19:54 +02002172 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002173 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002174
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002175 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002176}
2177
Chris Wilson2bfa9962016-08-04 07:52:25 +01002178static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2179 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002180{
Chris Wilson49d73912016-11-29 09:50:08 +00002181 ppgtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00002182 ppgtt->base.dma = &dev_priv->drm.pdev->dev;
Daniel Vetter3440d262013-01-24 13:49:56 -08002183
Chris Wilson2bfa9962016-08-04 07:52:25 +01002184 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002185 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002186 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002187 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002188}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002189
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002190static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002191 struct drm_i915_private *dev_priv,
2192 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002193{
Chris Wilson80b204b2016-10-28 13:58:58 +01002194 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00002195
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002196 drm_mm_init(&vm->mm, vm->start, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00002197 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
2198
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002199 INIT_LIST_HEAD(&vm->active_list);
2200 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002201 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00002202
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002203 list_add_tail(&vm->global_link, &dev_priv->vm_list);
Chris Wilson84486612017-02-15 08:43:40 +00002204 pagevec_init(&vm->free_pages, false);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002205}
2206
Matthew Aulded9724d2016-11-17 21:04:10 +00002207static void i915_address_space_fini(struct i915_address_space *vm)
2208{
Chris Wilson84486612017-02-15 08:43:40 +00002209 if (pagevec_count(&vm->free_pages))
2210 vm_free_pages_release(vm);
2211
Matthew Aulded9724d2016-11-17 21:04:10 +00002212 i915_gem_timeline_fini(&vm->timeline);
2213 drm_mm_takedown(&vm->mm);
2214 list_del(&vm->global_link);
2215}
2216
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002217static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002218{
Tim Gored5165eb2016-02-04 11:49:34 +00002219 /* This function is for gtt related workarounds. This function is
2220 * called on driver load and after a GPU reset, so you can place
2221 * workarounds here even if they get overwritten by GPU reset.
2222 */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002223 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002224 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002225 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002226 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002227 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08002228 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002229 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002230 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002231 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2232}
2233
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002234int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002235{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002236 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002237
Thomas Daniel671b50132014-08-20 16:24:50 +01002238 /* In the case of execlists, PPGTT is enabled by the context descriptor
2239 * and the PDPs are contained within the context itself. We don't
2240 * need to do anything here. */
2241 if (i915.enable_execlists)
2242 return 0;
2243
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002244 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002245 return 0;
2246
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002247 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002248 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002249 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002250 gen7_ppgtt_enable(dev_priv);
2251 else if (INTEL_GEN(dev_priv) >= 8)
2252 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002253 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002254 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002255
John Harrison4ad2fd82015-06-18 13:11:20 +01002256 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002257}
John Harrison4ad2fd82015-06-18 13:11:20 +01002258
Daniel Vetter4d884702014-08-06 15:04:47 +02002259struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002260i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002261 struct drm_i915_file_private *fpriv,
2262 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002263{
2264 struct i915_hw_ppgtt *ppgtt;
2265 int ret;
2266
2267 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2268 if (!ppgtt)
2269 return ERR_PTR(-ENOMEM);
2270
Chris Wilson1188bc62017-02-15 08:43:38 +00002271 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetter4d884702014-08-06 15:04:47 +02002272 if (ret) {
2273 kfree(ppgtt);
2274 return ERR_PTR(ret);
2275 }
2276
Chris Wilson1188bc62017-02-15 08:43:38 +00002277 kref_init(&ppgtt->ref);
2278 i915_address_space_init(&ppgtt->base, dev_priv, name);
2279 ppgtt->base.file = fpriv;
2280
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002281 trace_i915_ppgtt_create(&ppgtt->base);
2282
Daniel Vetter4d884702014-08-06 15:04:47 +02002283 return ppgtt;
2284}
2285
Chris Wilson0c7eeda2017-01-11 21:09:25 +00002286void i915_ppgtt_close(struct i915_address_space *vm)
2287{
2288 struct list_head *phases[] = {
2289 &vm->active_list,
2290 &vm->inactive_list,
2291 &vm->unbound_list,
2292 NULL,
2293 }, **phase;
2294
2295 GEM_BUG_ON(vm->closed);
2296 vm->closed = true;
2297
2298 for (phase = phases; *phase; phase++) {
2299 struct i915_vma *vma, *vn;
2300
2301 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2302 if (!i915_vma_is_closed(vma))
2303 i915_vma_close(vma);
2304 }
2305}
2306
Matthew Aulded9724d2016-11-17 21:04:10 +00002307void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002308{
2309 struct i915_hw_ppgtt *ppgtt =
2310 container_of(kref, struct i915_hw_ppgtt, ref);
2311
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002312 trace_i915_ppgtt_release(&ppgtt->base);
2313
Chris Wilson50e046b2016-08-04 07:52:46 +01002314 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002315 WARN_ON(!list_empty(&ppgtt->base.active_list));
2316 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002317 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002318
2319 ppgtt->base.cleanup(&ppgtt->base);
Chris Wilson84486612017-02-15 08:43:40 +00002320 i915_address_space_fini(&ppgtt->base);
Daniel Vetteree960be2014-08-06 15:04:45 +02002321 kfree(ppgtt);
2322}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002323
Ben Widawskya81cc002013-01-18 12:30:31 -08002324/* Certain Gen5 chipsets require require idling the GPU before
2325 * unmapping anything from the GTT when VT-d is enabled.
2326 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002327static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002328{
2329#ifdef CONFIG_INTEL_IOMMU
2330 /* Query intel_iommu to see if we need the workaround. Presumably that
2331 * was loaded first.
2332 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002333 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002334 return true;
2335#endif
2336 return false;
2337}
2338
Chris Wilsondc979972016-05-10 14:10:04 +01002339void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002340{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002341 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302342 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002343
Chris Wilsondc979972016-05-10 14:10:04 +01002344 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002345 return;
2346
Akash Goel3b3f1652016-10-13 22:44:48 +05302347 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002348 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002349 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002350 if (fault_reg & RING_FAULT_VALID) {
2351 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002352 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002353 "\tAddress space: %s\n"
2354 "\tSource ID: %d\n"
2355 "\tType: %d\n",
2356 fault_reg & PAGE_MASK,
2357 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2358 RING_FAULT_SRCID(fault_reg),
2359 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002360 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002361 fault_reg & ~RING_FAULT_VALID);
2362 }
2363 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302364
2365 /* Engine specific init may not have been done till this point. */
2366 if (dev_priv->engine[RCS])
2367 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002368}
2369
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002370void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002371{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002372 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002373
2374 /* Don't bother messing with faults pre GEN6 as we have little
2375 * documentation supporting that it's a good idea.
2376 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002377 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002378 return;
2379
Chris Wilsondc979972016-05-10 14:10:04 +01002380 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002381
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002382 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002383
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002384 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002385}
2386
Chris Wilson03ac84f2016-10-28 13:58:36 +01002387int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2388 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002389{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002390 do {
2391 if (dma_map_sg(&obj->base.dev->pdev->dev,
2392 pages->sgl, pages->nents,
2393 PCI_DMA_BIDIRECTIONAL))
2394 return 0;
2395
2396 /* If the DMA remap fails, one cause can be that we have
2397 * too many objects pinned in a small remapping table,
2398 * such as swiotlb. Incrementally purge all other objects and
2399 * try again - if there are no more pages to remove from
2400 * the DMA remapper, i915_gem_shrink will return 0.
2401 */
2402 GEM_BUG_ON(obj->mm.pages == pages);
2403 } while (i915_gem_shrink(to_i915(obj->base.dev),
2404 obj->base.size >> PAGE_SHIFT,
2405 I915_SHRINK_BOUND |
2406 I915_SHRINK_UNBOUND |
2407 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002408
Chris Wilson03ac84f2016-10-28 13:58:36 +01002409 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002410}
2411
Daniel Vetter2c642b02015-04-14 17:35:26 +02002412static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002413{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002414 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002415}
2416
Chris Wilsond6473f52016-06-10 14:22:59 +05302417static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2418 dma_addr_t addr,
2419 uint64_t offset,
2420 enum i915_cache_level level,
2421 u32 unused)
2422{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002423 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302424 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002425 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302426
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002427 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302428
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002429 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302430}
2431
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002432static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2433 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002434 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302435 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002436{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002437 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002438 struct sgt_iter sgt_iter;
2439 gen8_pte_t __iomem *gtt_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +00002440 const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002441 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002442
Chris Wilson894cceb2017-02-15 08:43:37 +00002443 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
2444 gtt_entries += start >> PAGE_SHIFT;
2445 for_each_sgt_dma(addr, sgt_iter, st)
2446 gen8_set_pte(gtt_entries++, pte_encode | addr);
Dave Gordon85d12252016-05-20 11:54:06 +01002447
Chris Wilson894cceb2017-02-15 08:43:37 +00002448 wmb();
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002449
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002450 /* This next bit makes the above posting read even more important. We
2451 * want to flush the TLBs only after we're certain all the PTE updates
2452 * have finished.
2453 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002454 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002455}
2456
Chris Wilsonc1403302015-11-18 15:19:39 +00002457struct insert_entries {
2458 struct i915_address_space *vm;
2459 struct sg_table *st;
2460 uint64_t start;
2461 enum i915_cache_level level;
2462 u32 flags;
2463};
2464
2465static int gen8_ggtt_insert_entries__cb(void *_arg)
2466{
2467 struct insert_entries *arg = _arg;
2468 gen8_ggtt_insert_entries(arg->vm, arg->st,
2469 arg->start, arg->level, arg->flags);
2470 return 0;
2471}
2472
2473static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2474 struct sg_table *st,
2475 uint64_t start,
2476 enum i915_cache_level level,
2477 u32 flags)
2478{
2479 struct insert_entries arg = { vm, st, start, level, flags };
2480 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2481}
2482
Chris Wilsond6473f52016-06-10 14:22:59 +05302483static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2484 dma_addr_t addr,
2485 uint64_t offset,
2486 enum i915_cache_level level,
2487 u32 flags)
2488{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002489 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302490 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002491 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302492
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002493 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302494
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002495 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302496}
2497
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002498/*
2499 * Binds an object into the global gtt with the specified cache level. The object
2500 * will be accessible to the GPU via commands whose operands reference offsets
2501 * within the global GTT as well as accessible by the GPU through the GMADR
2502 * mapped BAR (dev_priv->mm.gtt->gtt).
2503 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002504static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002505 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002506 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302507 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002508{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002509 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsonb31144c2017-02-15 08:43:36 +00002510 gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
2511 unsigned int i = start >> PAGE_SHIFT;
2512 struct sgt_iter iter;
Dave Gordon85d12252016-05-20 11:54:06 +01002513 dma_addr_t addr;
Chris Wilsonb31144c2017-02-15 08:43:36 +00002514 for_each_sgt_dma(addr, iter, st)
2515 iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2516 wmb();
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002517
2518 /* This next bit makes the above posting read even more important. We
2519 * want to flush the TLBs only after we're certain all the PTE updates
2520 * have finished.
2521 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002522 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002523}
2524
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002525static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002526 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002527{
2528}
2529
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002530static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002531 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002532{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002533 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002534 unsigned first_entry = start >> PAGE_SHIFT;
2535 unsigned num_entries = length >> PAGE_SHIFT;
Chris Wilson894cceb2017-02-15 08:43:37 +00002536 const gen8_pte_t scratch_pte =
2537 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
2538 gen8_pte_t __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002539 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2540 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002541 int i;
2542
2543 if (WARN(num_entries > max_entries,
2544 "First entry = %d; Num entries = %d (max=%d)\n",
2545 first_entry, num_entries, max_entries))
2546 num_entries = max_entries;
2547
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002548 for (i = 0; i < num_entries; i++)
2549 gen8_set_pte(&gtt_base[i], scratch_pte);
2550 readl(gtt_base);
2551}
2552
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002553static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002554 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002555 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002556{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002557 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002558 unsigned first_entry = start >> PAGE_SHIFT;
2559 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002560 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002561 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2562 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002563 int i;
2564
2565 if (WARN(num_entries > max_entries,
2566 "First entry = %d; Num entries = %d (max=%d)\n",
2567 first_entry, num_entries, max_entries))
2568 num_entries = max_entries;
2569
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002570 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002571 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002572
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002573 for (i = 0; i < num_entries; i++)
2574 iowrite32(scratch_pte, &gtt_base[i]);
2575 readl(gtt_base);
2576}
2577
Chris Wilsond6473f52016-06-10 14:22:59 +05302578static void i915_ggtt_insert_page(struct i915_address_space *vm,
2579 dma_addr_t addr,
2580 uint64_t offset,
2581 enum i915_cache_level cache_level,
2582 u32 unused)
2583{
Chris Wilsond6473f52016-06-10 14:22:59 +05302584 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2585 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302586
2587 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302588}
2589
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002590static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2591 struct sg_table *pages,
2592 uint64_t start,
2593 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002594{
2595 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2596 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2597
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002598 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002599
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002600}
2601
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002602static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002603 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002604 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002605{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002606 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002607}
2608
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002609static int ggtt_bind_vma(struct i915_vma *vma,
2610 enum i915_cache_level cache_level,
2611 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002612{
Chris Wilson49d73912016-11-29 09:50:08 +00002613 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002614 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonba7a5742017-02-15 08:43:35 +00002615 u32 pte_flags;
Daniel Vetter0a878712015-10-15 14:23:01 +02002616
Chris Wilsonba7a5742017-02-15 08:43:35 +00002617 if (unlikely(!vma->pages)) {
2618 int ret = i915_get_ggtt_vma_pages(vma);
2619 if (ret)
2620 return ret;
2621 }
Daniel Vetter0a878712015-10-15 14:23:01 +02002622
2623 /* Currently applicable only to VLV */
Chris Wilsonba7a5742017-02-15 08:43:35 +00002624 pte_flags = 0;
Daniel Vetter0a878712015-10-15 14:23:01 +02002625 if (obj->gt_ro)
2626 pte_flags |= PTE_READ_ONLY;
2627
Chris Wilson9c870d02016-10-24 13:42:15 +01002628 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002629 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002630 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002631 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002632
2633 /*
2634 * Without aliasing PPGTT there's no difference between
2635 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2636 * upgrade to both bound if we bind either to avoid double-binding.
2637 */
Chris Wilson3272db52016-08-04 16:32:32 +01002638 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002639
2640 return 0;
2641}
2642
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002643static void ggtt_unbind_vma(struct i915_vma *vma)
2644{
2645 struct drm_i915_private *i915 = vma->vm->i915;
2646
2647 intel_runtime_pm_get(i915);
2648 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2649 intel_runtime_pm_put(i915);
2650}
2651
Daniel Vetter0a878712015-10-15 14:23:01 +02002652static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2653 enum i915_cache_level cache_level,
2654 u32 flags)
2655{
Chris Wilson49d73912016-11-29 09:50:08 +00002656 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002657 u32 pte_flags;
Chris Wilsonff685972017-02-15 08:43:42 +00002658 int ret;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002659
Chris Wilsonba7a5742017-02-15 08:43:35 +00002660 if (unlikely(!vma->pages)) {
Chris Wilsonff685972017-02-15 08:43:42 +00002661 ret = i915_get_ggtt_vma_pages(vma);
Chris Wilsonba7a5742017-02-15 08:43:35 +00002662 if (ret)
2663 return ret;
2664 }
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002665
Akash Goel24f3a8c2014-06-17 10:59:42 +05302666 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002667 pte_flags = 0;
2668 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002669 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302670
Chris Wilsonff685972017-02-15 08:43:42 +00002671 if (flags & I915_VMA_LOCAL_BIND) {
2672 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
2673
2674 if (appgtt->base.allocate_va_range) {
2675 ret = appgtt->base.allocate_va_range(&appgtt->base,
2676 vma->node.start,
2677 vma->node.size);
2678 if (ret)
2679 return ret;
2680 }
2681
2682 appgtt->base.insert_entries(&appgtt->base,
2683 vma->pages, vma->node.start,
2684 cache_level, pte_flags);
2685 }
2686
Chris Wilson3272db52016-08-04 16:32:32 +01002687 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002688 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002689 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002690 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002691 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002692 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002693 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002694
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002695 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002696}
2697
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002698static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002699{
Chris Wilson49d73912016-11-29 09:50:08 +00002700 struct drm_i915_private *i915 = vma->vm->i915;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002701
Chris Wilson9c870d02016-10-24 13:42:15 +01002702 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2703 intel_runtime_pm_get(i915);
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002704 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002705 intel_runtime_pm_put(i915);
2706 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002707
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002708 if (vma->flags & I915_VMA_LOCAL_BIND) {
2709 struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
2710
2711 vm->clear_range(vm, vma->node.start, vma->size);
2712 }
Daniel Vetter74163902012-02-15 23:50:21 +01002713}
2714
Chris Wilson03ac84f2016-10-28 13:58:36 +01002715void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2716 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002717{
David Weinehall52a05c32016-08-22 13:32:44 +03002718 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2719 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002720 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002721
Chris Wilson307dc252016-08-05 10:14:12 +01002722 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002723 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002724 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2725 /* Wait a bit, in hopes it avoids the hang */
2726 udelay(10);
2727 }
2728 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002729
Chris Wilson03ac84f2016-10-28 13:58:36 +01002730 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002731}
Daniel Vetter644ec022012-03-26 09:45:40 +02002732
Chris Wilson45b186f2016-12-16 07:46:42 +00002733static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002734 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002735 u64 *start,
2736 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002737{
Chris Wilsona6508de2017-02-06 08:45:47 +00002738 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002739 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002740
Chris Wilsona6508de2017-02-06 08:45:47 +00002741 /* Also leave a space between the unallocated reserved node after the
2742 * GTT and any objects within the GTT, i.e. we use the color adjustment
2743 * to insert a guard page to prevent prefetches crossing over the
2744 * GTT boundary.
2745 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002746 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002747 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002748 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002749}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002750
Chris Wilson6cde9a02017-02-13 17:15:50 +00002751int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2752{
2753 struct i915_ggtt *ggtt = &i915->ggtt;
2754 struct i915_hw_ppgtt *ppgtt;
2755 int err;
2756
Chris Wilson1188bc62017-02-15 08:43:38 +00002757 ppgtt = i915_ppgtt_create(i915, NULL, "[alias]");
2758 if (IS_ERR(ppgtt))
2759 return PTR_ERR(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002760
2761 if (ppgtt->base.allocate_va_range) {
2762 err = ppgtt->base.allocate_va_range(&ppgtt->base,
2763 0, ppgtt->base.total);
2764 if (err)
Chris Wilson1188bc62017-02-15 08:43:38 +00002765 goto err_ppgtt;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002766 }
2767
2768 ppgtt->base.clear_range(&ppgtt->base,
2769 ppgtt->base.start,
2770 ppgtt->base.total);
2771
2772 i915->mm.aliasing_ppgtt = ppgtt;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002773
Chris Wilson6cde9a02017-02-13 17:15:50 +00002774 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2775 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2776
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002777 WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
2778 ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
2779
Chris Wilson6cde9a02017-02-13 17:15:50 +00002780 return 0;
2781
Chris Wilson6cde9a02017-02-13 17:15:50 +00002782err_ppgtt:
Chris Wilson1188bc62017-02-15 08:43:38 +00002783 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002784 return err;
2785}
2786
2787void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2788{
2789 struct i915_ggtt *ggtt = &i915->ggtt;
2790 struct i915_hw_ppgtt *ppgtt;
2791
2792 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2793 if (!ppgtt)
2794 return;
2795
Chris Wilson1188bc62017-02-15 08:43:38 +00002796 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002797
2798 ggtt->base.bind_vma = ggtt_bind_vma;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002799 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002800}
2801
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002802int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002803{
Ben Widawskye78891c2013-01-25 16:41:04 -08002804 /* Let GEM Manage all of the aperture.
2805 *
2806 * However, leave one page at the end still bound to the scratch page.
2807 * There are a number of places where the hardware apparently prefetches
2808 * past the end of the object, and we've seen multiple hangs with the
2809 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2810 * aperture. One page should be enough to keep any prefetching inside
2811 * of the aperture.
2812 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002813 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002814 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002815 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002816 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002817
Zhi Wangb02d22a2016-06-16 08:06:59 -04002818 ret = intel_vgt_balloon(dev_priv);
2819 if (ret)
2820 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002821
Chris Wilson95374d72016-10-12 10:05:20 +01002822 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002823 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2824 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2825 0, ggtt->mappable_end,
2826 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002827 if (ret)
2828 return ret;
2829
Chris Wilsoned2f3452012-11-15 11:32:19 +00002830 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002831 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002832 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2833 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002834 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002835 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002836 }
2837
2838 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002839 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002840 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002841
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002842 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Chris Wilson6cde9a02017-02-13 17:15:50 +00002843 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
Chris Wilson95374d72016-10-12 10:05:20 +01002844 if (ret)
Chris Wilson6cde9a02017-02-13 17:15:50 +00002845 goto err;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002846 }
2847
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002848 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002849
Chris Wilson95374d72016-10-12 10:05:20 +01002850err:
2851 drm_mm_remove_node(&ggtt->error_capture);
2852 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002853}
2854
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002855/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002856 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002857 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002858 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002859void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002860{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002861 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002862 struct i915_vma *vma, *vn;
2863
2864 ggtt->base.closed = true;
2865
2866 mutex_lock(&dev_priv->drm.struct_mutex);
2867 WARN_ON(!list_empty(&ggtt->base.active_list));
2868 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2869 WARN_ON(i915_vma_unbind(vma));
2870 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002871
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002872 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002873
Chris Wilson1188bc62017-02-15 08:43:38 +00002874 mutex_lock(&dev_priv->drm.struct_mutex);
2875 i915_gem_fini_aliasing_ppgtt(dev_priv);
2876
Chris Wilson95374d72016-10-12 10:05:20 +01002877 if (drm_mm_node_allocated(&ggtt->error_capture))
2878 drm_mm_remove_node(&ggtt->error_capture);
2879
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002880 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002881 intel_vgt_deballoon(dev_priv);
Matthew Aulded9724d2016-11-17 21:04:10 +00002882 i915_address_space_fini(&ggtt->base);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002883 }
2884
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002885 ggtt->base.cleanup(&ggtt->base);
Chris Wilson1188bc62017-02-15 08:43:38 +00002886 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002887
2888 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002889 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002890}
Daniel Vetter70e32542014-08-06 15:04:57 +02002891
Daniel Vetter2c642b02015-04-14 17:35:26 +02002892static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002893{
2894 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2895 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2896 return snb_gmch_ctl << 20;
2897}
2898
Daniel Vetter2c642b02015-04-14 17:35:26 +02002899static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002900{
2901 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2902 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2903 if (bdw_gmch_ctl)
2904 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002905
2906#ifdef CONFIG_X86_32
2907 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2908 if (bdw_gmch_ctl > 4)
2909 bdw_gmch_ctl = 4;
2910#endif
2911
Ben Widawsky9459d252013-11-03 16:53:55 -08002912 return bdw_gmch_ctl << 20;
2913}
2914
Daniel Vetter2c642b02015-04-14 17:35:26 +02002915static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002916{
2917 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2918 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2919
2920 if (gmch_ctrl)
2921 return 1 << (20 + gmch_ctrl);
2922
2923 return 0;
2924}
2925
Daniel Vetter2c642b02015-04-14 17:35:26 +02002926static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002927{
2928 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2929 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2930 return snb_gmch_ctl << 25; /* 32 MB units */
2931}
2932
Daniel Vetter2c642b02015-04-14 17:35:26 +02002933static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002934{
2935 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2936 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2937 return bdw_gmch_ctl << 25; /* 32 MB units */
2938}
2939
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002940static size_t chv_get_stolen_size(u16 gmch_ctrl)
2941{
2942 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2943 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2944
2945 /*
2946 * 0x0 to 0x10: 32MB increments starting at 0MB
2947 * 0x11 to 0x16: 4MB increments starting at 8MB
2948 * 0x17 to 0x1d: 4MB increments start at 36MB
2949 */
2950 if (gmch_ctrl < 0x11)
2951 return gmch_ctrl << 25;
2952 else if (gmch_ctrl < 0x17)
2953 return (gmch_ctrl - 0x11 + 2) << 22;
2954 else
2955 return (gmch_ctrl - 0x17 + 9) << 22;
2956}
2957
Damien Lespiau66375012014-01-09 18:02:46 +00002958static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2959{
2960 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2961 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2962
2963 if (gen9_gmch_ctl < 0xf0)
2964 return gen9_gmch_ctl << 25; /* 32 MB units */
2965 else
2966 /* 4MB increments starting at 0xf0 for 4MB */
2967 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2968}
2969
Chris Wilson34c998b2016-08-04 07:52:24 +01002970static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002971{
Chris Wilson49d73912016-11-29 09:50:08 +00002972 struct drm_i915_private *dev_priv = ggtt->base.i915;
2973 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002974 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002975 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002976
2977 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002978 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002979
Imre Deak2a073f892015-03-27 13:07:33 +02002980 /*
2981 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2982 * dropped. For WC mappings in general we have 64 byte burst writes
2983 * when the WC buffer is flushed, so we can't use it, but have to
2984 * resort to an uncached mapping. The WC issue is easily caught by the
2985 * readback check when writing GTT PTE entries.
2986 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002987 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002988 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002989 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002990 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002991 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002992 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002993 return -ENOMEM;
2994 }
2995
Chris Wilson84486612017-02-15 08:43:40 +00002996 ret = setup_scratch_page(&ggtt->base, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002997 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002998 DRM_ERROR("Scratch setup failed\n");
2999 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003000 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01003001 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08003002 }
3003
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03003004 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08003005}
3006
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003007/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3008 * bits. When using advanced contexts each context stores its own PAT, but
3009 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003010static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003011{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003012 uint64_t pat;
3013
3014 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
3015 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
3016 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
3017 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3018 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3019 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3020 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3021 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3022
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03003023 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08003024 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3025 * so RTL will always use the value corresponding to
3026 * pat_sel = 000".
3027 * So let's disable cache for GGTT to avoid screen corruptions.
3028 * MOCS still can be used though.
3029 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3030 * before this patch, i.e. the same uncached + snooping access
3031 * like on gen6/7 seems to be in effect.
3032 * - So this just fixes blitter/render access. Again it looks
3033 * like it's not just uncached access, but uncached + snooping.
3034 * So we can still hold onto all our assumptions wrt cpu
3035 * clflushing on LLC machines.
3036 */
3037 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3038
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003039 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3040 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003041 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3042 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003043}
3044
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003045static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3046{
3047 uint64_t pat;
3048
3049 /*
3050 * Map WB on BDW to snooped on CHV.
3051 *
3052 * Only the snoop bit has meaning for CHV, the rest is
3053 * ignored.
3054 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003055 * The hardware will never snoop for certain types of accesses:
3056 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3057 * - PPGTT page tables
3058 * - some other special cycles
3059 *
3060 * As with BDW, we also need to consider the following for GT accesses:
3061 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3062 * so RTL will always use the value corresponding to
3063 * pat_sel = 000".
3064 * Which means we must set the snoop bit in PAT entry 0
3065 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003066 */
3067 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3068 GEN8_PPAT(1, 0) |
3069 GEN8_PPAT(2, 0) |
3070 GEN8_PPAT(3, 0) |
3071 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3072 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3073 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3074 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3075
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003076 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3077 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003078}
3079
Chris Wilson34c998b2016-08-04 07:52:24 +01003080static void gen6_gmch_remove(struct i915_address_space *vm)
3081{
3082 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3083
3084 iounmap(ggtt->gsm);
Chris Wilson84486612017-02-15 08:43:40 +00003085 cleanup_scratch_page(vm);
Chris Wilson34c998b2016-08-04 07:52:24 +01003086}
3087
Joonas Lahtinend507d732016-03-18 10:42:58 +02003088static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003089{
Chris Wilson49d73912016-11-29 09:50:08 +00003090 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003091 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003092 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003093 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003094
3095 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003096 ggtt->mappable_base = pci_resource_start(pdev, 2);
3097 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003098
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003099 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3100 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003101
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003102 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003103
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003104 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003105 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003106 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003107 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003108 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003109 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003110 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003111 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003112 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003113 }
Ben Widawsky63340132013-11-04 19:32:22 -08003114
Chris Wilson34c998b2016-08-04 07:52:24 +01003115 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003116
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003117 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003118 chv_setup_private_ppat(dev_priv);
3119 else
3120 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003121
Chris Wilson34c998b2016-08-04 07:52:24 +01003122 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003123 ggtt->base.bind_vma = ggtt_bind_vma;
3124 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303125 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003126 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003127 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003128 ggtt->base.clear_range = gen8_ggtt_clear_range;
3129
3130 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3131 if (IS_CHERRYVIEW(dev_priv))
3132 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3133
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003134 ggtt->invalidate = gen6_ggtt_invalidate;
3135
Chris Wilson34c998b2016-08-04 07:52:24 +01003136 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003137}
3138
Joonas Lahtinend507d732016-03-18 10:42:58 +02003139static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003140{
Chris Wilson49d73912016-11-29 09:50:08 +00003141 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003142 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003143 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003144 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003145
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003146 ggtt->mappable_base = pci_resource_start(pdev, 2);
3147 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003148
Ben Widawskybaa09f52013-01-24 13:49:57 -08003149 /* 64/512MB is the current min/max we actually know of, but this is just
3150 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003151 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003152 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003153 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003154 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003155 }
3156
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003157 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3158 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3159 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003160
Joonas Lahtinend507d732016-03-18 10:42:58 +02003161 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003162
Chris Wilson34c998b2016-08-04 07:52:24 +01003163 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3164 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003165
Joonas Lahtinend507d732016-03-18 10:42:58 +02003166 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303167 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003168 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3169 ggtt->base.bind_vma = ggtt_bind_vma;
3170 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003171 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003172
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003173 ggtt->invalidate = gen6_ggtt_invalidate;
3174
Chris Wilson34c998b2016-08-04 07:52:24 +01003175 if (HAS_EDRAM(dev_priv))
3176 ggtt->base.pte_encode = iris_pte_encode;
3177 else if (IS_HASWELL(dev_priv))
3178 ggtt->base.pte_encode = hsw_pte_encode;
3179 else if (IS_VALLEYVIEW(dev_priv))
3180 ggtt->base.pte_encode = byt_pte_encode;
3181 else if (INTEL_GEN(dev_priv) >= 7)
3182 ggtt->base.pte_encode = ivb_pte_encode;
3183 else
3184 ggtt->base.pte_encode = snb_pte_encode;
3185
3186 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003187}
3188
Chris Wilson34c998b2016-08-04 07:52:24 +01003189static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003190{
Chris Wilson34c998b2016-08-04 07:52:24 +01003191 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003192}
3193
Joonas Lahtinend507d732016-03-18 10:42:58 +02003194static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003195{
Chris Wilson49d73912016-11-29 09:50:08 +00003196 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003197 int ret;
3198
Chris Wilson91c8a322016-07-05 10:40:23 +01003199 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003200 if (!ret) {
3201 DRM_ERROR("failed to set up gmch\n");
3202 return -EIO;
3203 }
3204
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003205 intel_gtt_get(&ggtt->base.total,
3206 &ggtt->stolen_size,
3207 &ggtt->mappable_base,
3208 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003209
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003210 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303211 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003212 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3213 ggtt->base.clear_range = i915_ggtt_clear_range;
3214 ggtt->base.bind_vma = ggtt_bind_vma;
3215 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003216 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003217
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003218 ggtt->invalidate = gmch_ggtt_invalidate;
3219
Joonas Lahtinend507d732016-03-18 10:42:58 +02003220 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003221 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3222
Ben Widawskybaa09f52013-01-24 13:49:57 -08003223 return 0;
3224}
3225
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003226/**
Chris Wilson0088e522016-08-04 07:52:21 +01003227 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003228 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003229 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003230int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003231{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003232 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003233 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003234
Chris Wilson49d73912016-11-29 09:50:08 +00003235 ggtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00003236 ggtt->base.dma = &dev_priv->drm.pdev->dev;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003237
Chris Wilson34c998b2016-08-04 07:52:24 +01003238 if (INTEL_GEN(dev_priv) <= 5)
3239 ret = i915_gmch_probe(ggtt);
3240 else if (INTEL_GEN(dev_priv) < 8)
3241 ret = gen6_gmch_probe(ggtt);
3242 else
3243 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003244 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003245 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003246
Chris Wilsondb9309a2017-01-05 15:30:23 +00003247 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3248 * This is easier than doing range restriction on the fly, as we
3249 * currently don't have any bits spare to pass in this upper
3250 * restriction!
3251 */
3252 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3253 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3254 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3255 }
3256
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003257 if ((ggtt->base.total - 1) >> 32) {
3258 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003259 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003260 ggtt->base.total >> 20);
3261 ggtt->base.total = 1ULL << 32;
3262 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3263 }
3264
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003265 if (ggtt->mappable_end > ggtt->base.total) {
3266 DRM_ERROR("mappable aperture extends past end of GGTT,"
3267 " aperture=%llx, total=%llx\n",
3268 ggtt->mappable_end, ggtt->base.total);
3269 ggtt->mappable_end = ggtt->base.total;
3270 }
3271
Ben Widawskybaa09f52013-01-24 13:49:57 -08003272 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003273 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003274 ggtt->base.total >> 20);
3275 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003276 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003277#ifdef CONFIG_INTEL_IOMMU
3278 if (intel_iommu_gfx_mapped)
3279 DRM_INFO("VT-d active for gfx access\n");
3280#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003281
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003282 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003283}
3284
3285/**
3286 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003287 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003288 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003289int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003290{
Chris Wilson0088e522016-08-04 07:52:21 +01003291 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3292 int ret;
3293
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003294 INIT_LIST_HEAD(&dev_priv->vm_list);
3295
Chris Wilsona6508de2017-02-06 08:45:47 +00003296 /* Note that we use page colouring to enforce a guard page at the
3297 * end of the address space. This is required as the CS may prefetch
3298 * beyond the end of the batch buffer, across the page boundary,
3299 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003300 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003301 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01003302 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00003303 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003304 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003305 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003306
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003307 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3308 dev_priv->ggtt.mappable_base,
3309 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003310 ret = -EIO;
3311 goto out_gtt_cleanup;
3312 }
3313
3314 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3315
Chris Wilson0088e522016-08-04 07:52:21 +01003316 /*
3317 * Initialise stolen early so that we may reserve preallocated
3318 * objects for the BIOS to KMS transition.
3319 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003320 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003321 if (ret)
3322 goto out_gtt_cleanup;
3323
3324 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003325
3326out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003327 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003328 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003329}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003330
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003331int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003332{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003333 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003334 return -EIO;
3335
3336 return 0;
3337}
3338
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003339void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3340{
3341 i915->ggtt.invalidate = guc_ggtt_invalidate;
3342}
3343
3344void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3345{
3346 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3347}
3348
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003349void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003350{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003351 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003352 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003353
Chris Wilsondc979972016-05-10 14:10:04 +01003354 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003355
3356 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003357 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003358
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003359 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3360
3361 /* clflush objects bound into the GGTT and rebind them. */
3362 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003363 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003364 bool ggtt_bound = false;
3365 struct i915_vma *vma;
3366
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003367 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003368 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003369 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003370
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003371 if (!i915_vma_unbind(vma))
3372 continue;
3373
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003374 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3375 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003376 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003377 }
3378
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003379 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003380 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003381 }
3382
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003383 ggtt->base.closed = false;
3384
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003385 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003386 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003387 chv_setup_private_ppat(dev_priv);
3388 else
3389 bdw_setup_private_ppat(dev_priv);
3390
3391 return;
3392 }
3393
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003394 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003395 struct i915_address_space *vm;
3396
Daniel Vetterfa423312015-04-14 17:35:23 +02003397 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3398 /* TODO: Perhaps it shouldn't be gen6 specific */
3399
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003400 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003401
Chris Wilson2bfa9962016-08-04 07:52:25 +01003402 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003403 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003404 else
3405 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003406
3407 gen6_write_page_range(dev_priv, &ppgtt->pd,
3408 0, ppgtt->base.total);
3409 }
3410 }
3411
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003412 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003413}
3414
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003415static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003416rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003417 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003418 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003419 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003420{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003421 unsigned int column, row;
3422 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003423
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003424 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003425 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003426 for (row = 0; row < height; row++) {
3427 st->nents++;
3428 /* We don't need the pages, but need to initialize
3429 * the entries so the sg list can be happily traversed.
3430 * The only thing we need are DMA addresses.
3431 */
3432 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003433 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003434 sg_dma_len(sg) = PAGE_SIZE;
3435 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003436 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003437 }
3438 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003439
3440 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003441}
3442
Chris Wilsonba7a5742017-02-15 08:43:35 +00003443static noinline struct sg_table *
3444intel_rotate_pages(struct intel_rotation_info *rot_info,
3445 struct drm_i915_gem_object *obj)
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003446{
Dave Gordon85d12252016-05-20 11:54:06 +01003447 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003448 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003449 struct sgt_iter sgt_iter;
3450 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003451 unsigned long i;
3452 dma_addr_t *page_addr_list;
3453 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003454 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003455 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003456
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003457 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003458 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003459 sizeof(dma_addr_t),
3460 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003461 if (!page_addr_list)
3462 return ERR_PTR(ret);
3463
3464 /* Allocate target SG list. */
3465 st = kmalloc(sizeof(*st), GFP_KERNEL);
3466 if (!st)
3467 goto err_st_alloc;
3468
Ville Syrjälä6687c902015-09-15 13:16:41 +03003469 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003470 if (ret)
3471 goto err_sg_alloc;
3472
3473 /* Populate source page list from the object. */
3474 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003475 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003476 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003477
Dave Gordon85d12252016-05-20 11:54:06 +01003478 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003479 st->nents = 0;
3480 sg = st->sgl;
3481
Ville Syrjälä6687c902015-09-15 13:16:41 +03003482 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3483 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3484 rot_info->plane[i].width, rot_info->plane[i].height,
3485 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003486 }
3487
Ville Syrjälä6687c902015-09-15 13:16:41 +03003488 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3489 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003490
3491 drm_free_large(page_addr_list);
3492
3493 return st;
3494
3495err_sg_alloc:
3496 kfree(st);
3497err_st_alloc:
3498 drm_free_large(page_addr_list);
3499
Ville Syrjälä6687c902015-09-15 13:16:41 +03003500 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3501 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3502
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003503 return ERR_PTR(ret);
3504}
3505
Chris Wilsonba7a5742017-02-15 08:43:35 +00003506static noinline struct sg_table *
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003507intel_partial_pages(const struct i915_ggtt_view *view,
3508 struct drm_i915_gem_object *obj)
3509{
3510 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003511 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003512 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003513 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003514 int ret = -ENOMEM;
3515
3516 st = kmalloc(sizeof(*st), GFP_KERNEL);
3517 if (!st)
3518 goto err_st_alloc;
3519
Chris Wilsond2a84a72016-10-28 13:58:34 +01003520 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003521 if (ret)
3522 goto err_sg_alloc;
3523
Chris Wilson8bab11932017-01-14 00:28:25 +00003524 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003525 GEM_BUG_ON(!iter);
3526
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003527 sg = st->sgl;
3528 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003529 do {
3530 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003531
Chris Wilsond2a84a72016-10-28 13:58:34 +01003532 len = min(iter->length - (offset << PAGE_SHIFT),
3533 count << PAGE_SHIFT);
3534 sg_set_page(sg, NULL, len, 0);
3535 sg_dma_address(sg) =
3536 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3537 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003538
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003539 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003540 count -= len >> PAGE_SHIFT;
3541 if (count == 0) {
3542 sg_mark_end(sg);
3543 return st;
3544 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003545
Chris Wilsond2a84a72016-10-28 13:58:34 +01003546 sg = __sg_next(sg);
3547 iter = __sg_next(iter);
3548 offset = 0;
3549 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003550
3551err_sg_alloc:
3552 kfree(st);
3553err_st_alloc:
3554 return ERR_PTR(ret);
3555}
3556
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003557static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003558i915_get_ggtt_vma_pages(struct i915_vma *vma)
3559{
Chris Wilsonba7a5742017-02-15 08:43:35 +00003560 int ret;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003561
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003562 /* The vma->pages are only valid within the lifespan of the borrowed
3563 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3564 * must be the vma->pages. A simple rule is that vma->pages must only
3565 * be accessed when the obj->mm.pages are pinned.
3566 */
3567 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3568
Chris Wilsonba7a5742017-02-15 08:43:35 +00003569 switch (vma->ggtt_view.type) {
3570 case I915_GGTT_VIEW_NORMAL:
3571 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003572 return 0;
3573
Chris Wilsonba7a5742017-02-15 08:43:35 +00003574 case I915_GGTT_VIEW_ROTATED:
Chris Wilson247177d2016-08-15 10:48:47 +01003575 vma->pages =
Chris Wilsonba7a5742017-02-15 08:43:35 +00003576 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3577 break;
3578
3579 case I915_GGTT_VIEW_PARTIAL:
Chris Wilson247177d2016-08-15 10:48:47 +01003580 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003581 break;
3582
3583 default:
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003584 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3585 vma->ggtt_view.type);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003586 return -EINVAL;
3587 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003588
Chris Wilsonba7a5742017-02-15 08:43:35 +00003589 ret = 0;
3590 if (unlikely(IS_ERR(vma->pages))) {
Chris Wilson247177d2016-08-15 10:48:47 +01003591 ret = PTR_ERR(vma->pages);
3592 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003593 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3594 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003595 }
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003596 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003597}
3598
Chris Wilsone007b192017-01-11 11:23:10 +00003599/**
Chris Wilson625d9882017-01-11 11:23:11 +00003600 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003601 * @vm: the &struct i915_address_space
3602 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3603 * @size: how much space to allocate inside the GTT,
3604 * must be #I915_GTT_PAGE_SIZE aligned
3605 * @offset: where to insert inside the GTT,
3606 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3607 * (@offset + @size) must fit within the address space
3608 * @color: color to apply to node, if this node is not from a VMA,
3609 * color must be #I915_COLOR_UNEVICTABLE
3610 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003611 *
3612 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3613 * the address space (using @size and @color). If the @node does not fit, it
3614 * tries to evict any overlapping nodes from the GTT, including any
3615 * neighbouring nodes if the colors do not match (to ensure guard pages between
3616 * differing domains). See i915_gem_evict_for_node() for the gory details
3617 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3618 * evicting active overlapping objects, and any overlapping node that is pinned
3619 * or marked as unevictable will also result in failure.
3620 *
3621 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3622 * asked to wait for eviction and interrupted.
3623 */
3624int i915_gem_gtt_reserve(struct i915_address_space *vm,
3625 struct drm_mm_node *node,
3626 u64 size, u64 offset, unsigned long color,
3627 unsigned int flags)
3628{
3629 int err;
3630
3631 GEM_BUG_ON(!size);
3632 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3633 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3634 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003635 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003636 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003637
3638 node->size = size;
3639 node->start = offset;
3640 node->color = color;
3641
3642 err = drm_mm_reserve_node(&vm->mm, node);
3643 if (err != -ENOSPC)
3644 return err;
3645
3646 err = i915_gem_evict_for_node(vm, node, flags);
3647 if (err == 0)
3648 err = drm_mm_reserve_node(&vm->mm, node);
3649
3650 return err;
3651}
3652
Chris Wilson606fec92017-01-11 11:23:12 +00003653static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3654{
3655 u64 range, addr;
3656
3657 GEM_BUG_ON(range_overflows(start, len, end));
3658 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3659
3660 range = round_down(end - len, align) - round_up(start, align);
3661 if (range) {
3662 if (sizeof(unsigned long) == sizeof(u64)) {
3663 addr = get_random_long();
3664 } else {
3665 addr = get_random_int();
3666 if (range > U32_MAX) {
3667 addr <<= 32;
3668 addr |= get_random_int();
3669 }
3670 }
3671 div64_u64_rem(addr, range, &addr);
3672 start += addr;
3673 }
3674
3675 return round_up(start, align);
3676}
3677
Chris Wilson625d9882017-01-11 11:23:11 +00003678/**
Chris Wilsone007b192017-01-11 11:23:10 +00003679 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003680 * @vm: the &struct i915_address_space
3681 * @node: the &struct drm_mm_node (typically i915_vma.node)
3682 * @size: how much space to allocate inside the GTT,
3683 * must be #I915_GTT_PAGE_SIZE aligned
3684 * @alignment: required alignment of starting offset, may be 0 but
3685 * if specified, this must be a power-of-two and at least
3686 * #I915_GTT_MIN_ALIGNMENT
3687 * @color: color to apply to node
3688 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003689 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003690 * @end: end of any range restriction inside GTT (U64_MAX for all),
3691 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3692 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003693 *
3694 * i915_gem_gtt_insert() first searches for an available hole into which
3695 * is can insert the node. The hole address is aligned to @alignment and
3696 * its @size must then fit entirely within the [@start, @end] bounds. The
3697 * nodes on either side of the hole must match @color, or else a guard page
3698 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003699 * suitable hole is found, first a victim is randomly selected and tested
3700 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003701 * is scanned to find the first set of replacement nodes to create the hole.
3702 * Those old overlapping nodes are evicted from the GTT (and so must be
3703 * rebound before any future use). Any node that is currently pinned cannot
3704 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3705 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3706 * searching for an eviction candidate. See i915_gem_evict_something() for
3707 * the gory details on the eviction algorithm.
3708 *
3709 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3710 * asked to wait for eviction and interrupted.
3711 */
3712int i915_gem_gtt_insert(struct i915_address_space *vm,
3713 struct drm_mm_node *node,
3714 u64 size, u64 alignment, unsigned long color,
3715 u64 start, u64 end, unsigned int flags)
3716{
Chris Wilson4e64e552017-02-02 21:04:38 +00003717 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003718 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003719 int err;
3720
3721 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3722 GEM_BUG_ON(!size);
3723 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3724 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3725 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3726 GEM_BUG_ON(start >= end);
3727 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3728 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003729 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003730 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003731
3732 if (unlikely(range_overflows(start, size, end)))
3733 return -ENOSPC;
3734
3735 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3736 return -ENOSPC;
3737
Chris Wilson4e64e552017-02-02 21:04:38 +00003738 mode = DRM_MM_INSERT_BEST;
3739 if (flags & PIN_HIGH)
3740 mode = DRM_MM_INSERT_HIGH;
3741 if (flags & PIN_MAPPABLE)
3742 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003743
3744 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3745 * so we know that we always have a minimum alignment of 4096.
3746 * The drm_mm range manager is optimised to return results
3747 * with zero alignment, so where possible use the optimal
3748 * path.
3749 */
3750 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3751 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3752 alignment = 0;
3753
Chris Wilson4e64e552017-02-02 21:04:38 +00003754 err = drm_mm_insert_node_in_range(&vm->mm, node,
3755 size, alignment, color,
3756 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003757 if (err != -ENOSPC)
3758 return err;
3759
Chris Wilson606fec92017-01-11 11:23:12 +00003760 /* No free space, pick a slot at random.
3761 *
3762 * There is a pathological case here using a GTT shared between
3763 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3764 *
3765 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3766 * (64k objects) (448k objects)
3767 *
3768 * Now imagine that the eviction LRU is ordered top-down (just because
3769 * pathology meets real life), and that we need to evict an object to
3770 * make room inside the aperture. The eviction scan then has to walk
3771 * the 448k list before it finds one within range. And now imagine that
3772 * it has to search for a new hole between every byte inside the memcpy,
3773 * for several simultaneous clients.
3774 *
3775 * On a full-ppgtt system, if we have run out of available space, there
3776 * will be lots and lots of objects in the eviction list! Again,
3777 * searching that LRU list may be slow if we are also applying any
3778 * range restrictions (e.g. restriction to low 4GiB) and so, for
3779 * simplicity and similarilty between different GTT, try the single
3780 * random replacement first.
3781 */
3782 offset = random_offset(start, end,
3783 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3784 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3785 if (err != -ENOSPC)
3786 return err;
3787
3788 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003789 err = i915_gem_evict_something(vm, size, alignment, color,
3790 start, end, flags);
3791 if (err)
3792 return err;
3793
Chris Wilson4e64e552017-02-02 21:04:38 +00003794 return drm_mm_insert_node_in_range(&vm->mm, node,
3795 size, alignment, color,
3796 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003797}
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003798
3799#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3800#include "selftests/mock_gtt.c"
Chris Wilson1c428192017-02-13 17:15:38 +00003801#include "selftests/i915_gem_gtt.c"
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003802#endif