blob: ae2ff36b275ad51ced56e80ec8689ef0cfcb36f8 [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;
Ben Widawsky06fda602015-02-24 16:22:36 +0000460
Chris Wilsondd196742017-02-15 08:43:46 +0000461 pt = kmalloc(sizeof(*pt), GFP_KERNEL | __GFP_NOWARN);
462 if (unlikely(!pt))
Ben Widawsky06fda602015-02-24 16:22:36 +0000463 return ERR_PTR(-ENOMEM);
464
Chris Wilsondd196742017-02-15 08:43:46 +0000465 if (unlikely(setup_px(vm, pt))) {
466 kfree(pt);
467 return ERR_PTR(-ENOMEM);
468 }
Ben Widawsky678d96f2015-03-16 16:00:56 +0000469
Chris Wilsondd196742017-02-15 08:43:46 +0000470 pt->used_ptes = 0;
Ben Widawsky06fda602015-02-24 16:22:36 +0000471 return pt;
472}
473
Chris Wilson84486612017-02-15 08:43:40 +0000474static void free_pt(struct i915_address_space *vm, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000475{
Chris Wilson84486612017-02-15 08:43:40 +0000476 cleanup_px(vm, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300477 kfree(pt);
478}
479
480static void gen8_initialize_pt(struct i915_address_space *vm,
481 struct i915_page_table *pt)
482{
Chris Wilsondd196742017-02-15 08:43:46 +0000483 fill_px(vm, pt,
484 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC));
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300485}
486
487static void gen6_initialize_pt(struct i915_address_space *vm,
488 struct i915_page_table *pt)
489{
Chris Wilsondd196742017-02-15 08:43:46 +0000490 fill32_px(vm, pt,
491 vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0));
Ben Widawsky06fda602015-02-24 16:22:36 +0000492}
493
Chris Wilson84486612017-02-15 08:43:40 +0000494static struct i915_page_directory *alloc_pd(struct i915_address_space *vm)
Ben Widawsky06fda602015-02-24 16:22:36 +0000495{
Michel Thierryec565b32015-04-08 12:13:23 +0100496 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100497 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000498
499 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
500 if (!pd)
501 return ERR_PTR(-ENOMEM);
502
Michel Thierry33c88192015-04-08 12:13:33 +0100503 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
504 sizeof(*pd->used_pdes), GFP_KERNEL);
505 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300506 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100507
Chris Wilson84486612017-02-15 08:43:40 +0000508 ret = setup_px(vm, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100509 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300510 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100511
Ben Widawsky06fda602015-02-24 16:22:36 +0000512 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100513
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300514fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100515 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300516fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100517 kfree(pd);
518
519 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000520}
521
Chris Wilson84486612017-02-15 08:43:40 +0000522static void free_pd(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000523 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300524{
525 if (px_page(pd)) {
Chris Wilson84486612017-02-15 08:43:40 +0000526 cleanup_px(vm, pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300527 kfree(pd->used_pdes);
528 kfree(pd);
529 }
530}
531
532static void gen8_initialize_pd(struct i915_address_space *vm,
533 struct i915_page_directory *pd)
534{
Chris Wilsondd196742017-02-15 08:43:46 +0000535 unsigned int i;
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300536
Chris Wilsondd196742017-02-15 08:43:46 +0000537 fill_px(vm, pd,
538 gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC));
539 for (i = 0; i < I915_PDES; i++)
540 pd->page_table[i] = vm->scratch_pt;
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300541}
542
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000543static int __pdp_init(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100544 struct i915_page_directory_pointer *pdp)
545{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000546 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Michel Thierry6ac18502015-07-29 17:23:46 +0100547
548 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
549 sizeof(unsigned long),
550 GFP_KERNEL);
551 if (!pdp->used_pdpes)
552 return -ENOMEM;
553
554 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
555 GFP_KERNEL);
556 if (!pdp->page_directory) {
557 kfree(pdp->used_pdpes);
558 /* the PDP might be the statically allocated top level. Keep it
559 * as clean as possible */
560 pdp->used_pdpes = NULL;
561 return -ENOMEM;
562 }
563
564 return 0;
565}
566
567static void __pdp_fini(struct i915_page_directory_pointer *pdp)
568{
569 kfree(pdp->used_pdpes);
570 kfree(pdp->page_directory);
571 pdp->page_directory = NULL;
572}
573
Chris Wilson84486612017-02-15 08:43:40 +0000574static struct i915_page_directory_pointer *
575alloc_pdp(struct i915_address_space *vm)
Michel Thierry762d9932015-07-30 11:05:29 +0100576{
577 struct i915_page_directory_pointer *pdp;
578 int ret = -ENOMEM;
579
Chris Wilson84486612017-02-15 08:43:40 +0000580 WARN_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Michel Thierry762d9932015-07-30 11:05:29 +0100581
582 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
583 if (!pdp)
584 return ERR_PTR(-ENOMEM);
585
Chris Wilson84486612017-02-15 08:43:40 +0000586 ret = __pdp_init(vm->i915, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100587 if (ret)
588 goto fail_bitmap;
589
Chris Wilson84486612017-02-15 08:43:40 +0000590 ret = setup_px(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100591 if (ret)
592 goto fail_page_m;
593
594 return pdp;
595
596fail_page_m:
597 __pdp_fini(pdp);
598fail_bitmap:
599 kfree(pdp);
600
601 return ERR_PTR(ret);
602}
603
Chris Wilson84486612017-02-15 08:43:40 +0000604static void free_pdp(struct i915_address_space *vm,
Michel Thierry6ac18502015-07-29 17:23:46 +0100605 struct i915_page_directory_pointer *pdp)
606{
607 __pdp_fini(pdp);
Chris Wilson84486612017-02-15 08:43:40 +0000608 if (USES_FULL_48BIT_PPGTT(vm->i915)) {
609 cleanup_px(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100610 kfree(pdp);
611 }
612}
613
Michel Thierry69ab76f2015-07-29 17:23:55 +0100614static void gen8_initialize_pdp(struct i915_address_space *vm,
615 struct i915_page_directory_pointer *pdp)
616{
617 gen8_ppgtt_pdpe_t scratch_pdpe;
618
619 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
620
Chris Wilson84486612017-02-15 08:43:40 +0000621 fill_px(vm, pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100622}
623
624static void gen8_initialize_pml4(struct i915_address_space *vm,
625 struct i915_pml4 *pml4)
626{
627 gen8_ppgtt_pml4e_t scratch_pml4e;
628
629 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
630 I915_CACHE_LLC);
631
Chris Wilson84486612017-02-15 08:43:40 +0000632 fill_px(vm, pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100633}
634
Michel Thierry762d9932015-07-30 11:05:29 +0100635static void
Matthew Auld5c693b22016-12-13 16:05:10 +0000636gen8_setup_pdpe(struct i915_hw_ppgtt *ppgtt,
637 struct i915_page_directory_pointer *pdp,
638 struct i915_page_directory *pd,
639 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100640{
641 gen8_ppgtt_pdpe_t *page_directorypo;
642
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000643 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
Michel Thierry762d9932015-07-30 11:05:29 +0100644 return;
645
Chris Wilson9231da72017-02-15 08:43:41 +0000646 page_directorypo = kmap_atomic_px(pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100647 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
Chris Wilson9231da72017-02-15 08:43:41 +0000648 kunmap_atomic(page_directorypo);
Michel Thierry762d9932015-07-30 11:05:29 +0100649}
650
651static void
Chris Wilson84486612017-02-15 08:43:40 +0000652gen8_setup_pml4e(struct i915_pml4 *pml4,
Matthew Auld56843102016-12-13 16:05:11 +0000653 struct i915_page_directory_pointer *pdp,
654 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100655{
Chris Wilson9231da72017-02-15 08:43:41 +0000656 gen8_ppgtt_pml4e_t *pagemap = kmap_atomic_px(pml4);
Michel Thierry762d9932015-07-30 11:05:29 +0100657
Michel Thierry762d9932015-07-30 11:05:29 +0100658 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
Chris Wilson9231da72017-02-15 08:43:41 +0000659 kunmap_atomic(pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100660}
661
Ben Widawsky94e409c2013-11-04 22:29:36 -0800662/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100663static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100664 unsigned entry,
665 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800666{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000667 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000668 u32 *cs;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800669
670 BUG_ON(entry >= 4);
671
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000672 cs = intel_ring_begin(req, 6);
673 if (IS_ERR(cs))
674 return PTR_ERR(cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800675
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000676 *cs++ = MI_LOAD_REGISTER_IMM(1);
677 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, entry));
678 *cs++ = upper_32_bits(addr);
679 *cs++ = MI_LOAD_REGISTER_IMM(1);
680 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, entry));
681 *cs++ = lower_32_bits(addr);
682 intel_ring_advance(req, cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800683
684 return 0;
685}
686
Michel Thierry2dba3232015-07-30 11:06:23 +0100687static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
688 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800689{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800690 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800691
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100692 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300693 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
694
John Harrisone85b26d2015-05-29 17:43:56 +0100695 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800696 if (ret)
697 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800698 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800699
Ben Widawskyeeb94882013-12-06 14:11:10 -0800700 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800701}
702
Michel Thierry2dba3232015-07-30 11:06:23 +0100703static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
704 struct drm_i915_gem_request *req)
705{
706 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
707}
708
Mika Kuoppalafce93752016-10-31 17:24:46 +0200709/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
710 * the page table structures, we mark them dirty so that
711 * context switching/execlist queuing code takes extra steps
712 * to ensure that tlbs are flushed.
713 */
714static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
715{
Chris Wilson49d73912016-11-29 09:50:08 +0000716 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200717}
718
Michał Winiarski2ce51792016-10-13 14:02:42 +0200719/* Removes entries from a single page table, releasing it if it's empty.
720 * Caller can use the return value to update higher-level entries.
721 */
722static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200723 struct i915_page_table *pt,
Chris Wilsondd196742017-02-15 08:43:46 +0000724 u64 start, u64 length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700725{
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200726 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200727 unsigned int pte = gen8_pte_index(start);
728 unsigned int pte_end = pte + num_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +0000729 const gen8_pte_t scratch_pte =
730 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
731 gen8_pte_t *vaddr;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200732
Chris Wilsondd196742017-02-15 08:43:46 +0000733 GEM_BUG_ON(num_entries > pt->used_ptes);
Ben Widawsky459108b2013-11-02 21:07:23 -0700734
Chris Wilsondd196742017-02-15 08:43:46 +0000735 pt->used_ptes -= num_entries;
736 if (!pt->used_ptes)
737 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200738
Chris Wilson9231da72017-02-15 08:43:41 +0000739 vaddr = kmap_atomic_px(pt);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200740 while (pte < pte_end)
Chris Wilson894cceb2017-02-15 08:43:37 +0000741 vaddr[pte++] = scratch_pte;
Chris Wilson9231da72017-02-15 08:43:41 +0000742 kunmap_atomic(vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200743
744 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200745}
746
Chris Wilsondd196742017-02-15 08:43:46 +0000747static void gen8_ppgtt_set_pde(struct i915_address_space *vm,
748 struct i915_page_directory *pd,
749 struct i915_page_table *pt,
750 unsigned int pde)
751{
752 gen8_pde_t *vaddr;
753
754 pd->page_table[pde] = pt;
755
756 vaddr = kmap_atomic_px(pd);
757 vaddr[pde] = gen8_pde_encode(px_dma(pt), I915_CACHE_LLC);
758 kunmap_atomic(vaddr);
759}
760
Michał Winiarski2ce51792016-10-13 14:02:42 +0200761/* Removes entries from a single page dir, releasing it if it's empty.
762 * Caller can use the return value to update higher-level entries
763 */
764static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200765 struct i915_page_directory *pd,
Chris Wilsondd196742017-02-15 08:43:46 +0000766 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200767{
768 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +0000769 u32 pde;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200770
771 gen8_for_each_pde(pt, pd, start, length, pde) {
Chris Wilsondd196742017-02-15 08:43:46 +0000772 if (!gen8_ppgtt_clear_pt(vm, pt, start, length))
773 continue;
Ben Widawsky06fda602015-02-24 16:22:36 +0000774
Chris Wilsondd196742017-02-15 08:43:46 +0000775 gen8_ppgtt_set_pde(vm, pd, vm->scratch_pt, pde);
776 __clear_bit(pde, pd->used_pdes);
777
778 free_pt(vm, pt);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200779 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200780
Zhi Wanga18dbba2016-11-29 14:55:16 +0800781 if (bitmap_empty(pd->used_pdes, I915_PDES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200782 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200783
784 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200785}
Ben Widawsky06fda602015-02-24 16:22:36 +0000786
Michał Winiarski2ce51792016-10-13 14:02:42 +0200787/* Removes entries from a single page dir pointer, releasing it if it's empty.
788 * Caller can use the return value to update higher-level entries
789 */
790static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200791 struct i915_page_directory_pointer *pdp,
792 uint64_t start,
793 uint64_t length)
794{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200795 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200796 struct i915_page_directory *pd;
797 uint64_t pdpe;
798
799 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
800 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100801 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000802
Michał Winiarski2ce51792016-10-13 14:02:42 +0200803 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
804 __clear_bit(pdpe, pdp->used_pdpes);
Matthew Auld9e65a372016-12-13 16:05:12 +0000805 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
Chris Wilson84486612017-02-15 08:43:40 +0000806 free_pd(vm, pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200807 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200808 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200809
Mika Kuoppalafce93752016-10-31 17:24:46 +0200810 mark_tlbs_dirty(ppgtt);
811
Zhi Wanga18dbba2016-11-29 14:55:16 +0800812 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200813 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200814
815 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200816}
Ben Widawsky459108b2013-11-02 21:07:23 -0700817
Michał Winiarski2ce51792016-10-13 14:02:42 +0200818/* Removes entries from a single pml4.
819 * This is the top-level structure in 4-level page tables used on gen8+.
820 * Empty entries are always scratch pml4e.
821 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200822static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
823 struct i915_pml4 *pml4,
824 uint64_t start,
825 uint64_t length)
826{
827 struct i915_page_directory_pointer *pdp;
828 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200829
Chris Wilson49d73912016-11-29 09:50:08 +0000830 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700831
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200832 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
833 if (WARN_ON(!pml4->pdps[pml4e]))
834 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700835
Michał Winiarski2ce51792016-10-13 14:02:42 +0200836 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
837 __clear_bit(pml4e, pml4->used_pml4es);
Chris Wilson84486612017-02-15 08:43:40 +0000838 gen8_setup_pml4e(pml4, vm->scratch_pdp, pml4e);
839 free_pdp(vm, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200840 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700841 }
842}
843
Michel Thierryf9b5b782015-07-30 11:02:49 +0100844static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200845 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700846{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300847 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100848
Chris Wilsonc6385c92016-11-29 12:42:05 +0000849 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200850 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
851 else
852 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100853}
854
Chris Wilson894cceb2017-02-15 08:43:37 +0000855struct sgt_dma {
856 struct scatterlist *sg;
857 dma_addr_t dma, max;
858};
859
860static __always_inline bool
861gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100862 struct i915_page_directory_pointer *pdp,
Chris Wilson894cceb2017-02-15 08:43:37 +0000863 struct sgt_dma *iter,
864 u64 start,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100865 enum i915_cache_level cache_level)
866{
Chris Wilson894cceb2017-02-15 08:43:37 +0000867 unsigned int pdpe = gen8_pdpe_index(start);
868 unsigned int pde = gen8_pde_index(start);
869 unsigned int pte = gen8_pte_index(start);
870 struct i915_page_directory *pd;
871 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
872 gen8_pte_t *vaddr;
873 bool ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700874
Chris Wilson894cceb2017-02-15 08:43:37 +0000875 pd = pdp->page_directory[pdpe];
Chris Wilson9231da72017-02-15 08:43:41 +0000876 vaddr = kmap_atomic_px(pd->page_table[pde]);
Chris Wilson894cceb2017-02-15 08:43:37 +0000877 do {
878 vaddr[pte] = pte_encode | iter->dma;
879 iter->dma += PAGE_SIZE;
880 if (iter->dma >= iter->max) {
881 iter->sg = __sg_next(iter->sg);
882 if (!iter->sg) {
883 ret = false;
884 break;
885 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700886
Chris Wilson894cceb2017-02-15 08:43:37 +0000887 iter->dma = sg_dma_address(iter->sg);
888 iter->max = iter->dma + iter->sg->length;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000889 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800890
Michel Thierry07749ef2015-03-16 16:00:54 +0000891 if (++pte == GEN8_PTES) {
Michel Thierry07749ef2015-03-16 16:00:54 +0000892 if (++pde == I915_PDES) {
Chris Wilson894cceb2017-02-15 08:43:37 +0000893 /* Limited by sg length for 3lvl */
894 if (++pdpe == GEN8_PML4ES_PER_PML4) {
895 ret = true;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100896 break;
Chris Wilson894cceb2017-02-15 08:43:37 +0000897 }
898
899 GEM_BUG_ON(pdpe > GEN8_LEGACY_PDPES);
900 pd = pdp->page_directory[pdpe];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800901 pde = 0;
902 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000903
Chris Wilson9231da72017-02-15 08:43:41 +0000904 kunmap_atomic(vaddr);
905 vaddr = kmap_atomic_px(pd->page_table[pde]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800906 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700907 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000908 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +0000909 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300910
Chris Wilson894cceb2017-02-15 08:43:37 +0000911 return ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700912}
913
Chris Wilson894cceb2017-02-15 08:43:37 +0000914static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
915 struct sg_table *pages,
916 u64 start,
917 enum i915_cache_level cache_level,
918 u32 unused)
Michel Thierryf9b5b782015-07-30 11:02:49 +0100919{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300920 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilson894cceb2017-02-15 08:43:37 +0000921 struct sgt_dma iter = {
922 .sg = pages->sgl,
923 .dma = sg_dma_address(iter.sg),
924 .max = iter.dma + iter.sg->length,
925 };
Michel Thierryf9b5b782015-07-30 11:02:49 +0100926
Chris Wilson894cceb2017-02-15 08:43:37 +0000927 gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter,
928 start, cache_level);
929}
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100930
Chris Wilson894cceb2017-02-15 08:43:37 +0000931static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
932 struct sg_table *pages,
933 uint64_t start,
934 enum i915_cache_level cache_level,
935 u32 unused)
936{
937 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
938 struct sgt_dma iter = {
939 .sg = pages->sgl,
940 .dma = sg_dma_address(iter.sg),
941 .max = iter.dma + iter.sg->length,
942 };
943 struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
944 unsigned int pml4e = gen8_pml4e_index(start);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100945
Chris Wilson894cceb2017-02-15 08:43:37 +0000946 while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[pml4e++], &iter,
947 start, cache_level))
948 ;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100949}
950
Chris Wilson84486612017-02-15 08:43:40 +0000951static void gen8_free_page_tables(struct i915_address_space *vm,
Michel Thierryf37c0502015-06-10 17:46:39 +0100952 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800953{
954 int i;
955
Mika Kuoppala567047b2015-06-25 18:35:12 +0300956 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800957 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800958
Michel Thierry33c88192015-04-08 12:13:33 +0100959 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000960 if (WARN_ON(!pd->page_table[i]))
961 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800962
Chris Wilson84486612017-02-15 08:43:40 +0000963 free_pt(vm, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000964 pd->page_table[i] = NULL;
965 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000966}
967
Mika Kuoppala8776f022015-06-30 18:16:40 +0300968static int gen8_init_scratch(struct i915_address_space *vm)
969{
Matthew Auld64c050d2016-04-27 13:19:25 +0100970 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300971
Chris Wilson84486612017-02-15 08:43:40 +0000972 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100973 if (ret)
974 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300975
Chris Wilson84486612017-02-15 08:43:40 +0000976 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300977 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100978 ret = PTR_ERR(vm->scratch_pt);
979 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300980 }
981
Chris Wilson84486612017-02-15 08:43:40 +0000982 vm->scratch_pd = alloc_pd(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300983 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100984 ret = PTR_ERR(vm->scratch_pd);
985 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300986 }
987
Chris Wilson84486612017-02-15 08:43:40 +0000988 if (USES_FULL_48BIT_PPGTT(dev)) {
989 vm->scratch_pdp = alloc_pdp(vm);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100990 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100991 ret = PTR_ERR(vm->scratch_pdp);
992 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100993 }
994 }
995
Mika Kuoppala8776f022015-06-30 18:16:40 +0300996 gen8_initialize_pt(vm, vm->scratch_pt);
997 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000998 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100999 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001000
1001 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +01001002
1003free_pd:
Chris Wilson84486612017-02-15 08:43:40 +00001004 free_pd(vm, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +01001005free_pt:
Chris Wilson84486612017-02-15 08:43:40 +00001006 free_pt(vm, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +01001007free_scratch_page:
Chris Wilson84486612017-02-15 08:43:40 +00001008 cleanup_scratch_page(vm);
Matthew Auld64c050d2016-04-27 13:19:25 +01001009
1010 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001011}
1012
Zhiyuan Lv650da342015-08-28 15:41:18 +08001013static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1014{
1015 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +00001016 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001017 int i;
1018
Matthew Aulddf285642016-04-22 12:09:25 +01001019 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001020 u64 daddr = px_dma(&ppgtt->pml4);
1021
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001022 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1023 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001024
1025 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1026 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1027 } else {
1028 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1029 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1030
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001031 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1032 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001033 }
1034
1035 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1036 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1037 }
1038
1039 I915_WRITE(vgtif_reg(g2v_notify), msg);
1040
1041 return 0;
1042}
1043
Mika Kuoppala8776f022015-06-30 18:16:40 +03001044static void gen8_free_scratch(struct i915_address_space *vm)
1045{
Chris Wilson84486612017-02-15 08:43:40 +00001046 if (USES_FULL_48BIT_PPGTT(vm->i915))
1047 free_pdp(vm, vm->scratch_pdp);
1048 free_pd(vm, vm->scratch_pd);
1049 free_pt(vm, vm->scratch_pt);
1050 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001051}
1052
Chris Wilson84486612017-02-15 08:43:40 +00001053static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
Michel Thierry762d9932015-07-30 11:05:29 +01001054 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001055{
1056 int i;
1057
Chris Wilson84486612017-02-15 08:43:40 +00001058 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(vm->i915)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001059 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001060 continue;
1061
Chris Wilson84486612017-02-15 08:43:40 +00001062 gen8_free_page_tables(vm, pdp->page_directory[i]);
1063 free_pd(vm, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001064 }
Michel Thierry69876be2015-04-08 12:13:27 +01001065
Chris Wilson84486612017-02-15 08:43:40 +00001066 free_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001067}
1068
1069static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1070{
1071 int i;
1072
1073 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1074 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1075 continue;
1076
Chris Wilson84486612017-02-15 08:43:40 +00001077 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001078 }
1079
Chris Wilson84486612017-02-15 08:43:40 +00001080 cleanup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001081}
1082
1083static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1084{
Chris Wilson49d73912016-11-29 09:50:08 +00001085 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001086 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001087
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001088 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001089 gen8_ppgtt_notify_vgt(ppgtt, false);
1090
Chris Wilson84486612017-02-15 08:43:40 +00001091 if (!USES_FULL_48BIT_PPGTT(vm->i915))
1092 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001093 else
1094 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001095
Mika Kuoppala8776f022015-06-30 18:16:40 +03001096 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001097}
1098
Michel Thierryd7b26332015-04-08 12:13:34 +01001099/**
1100 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001101 * @vm: Master vm structure.
1102 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001103 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001104 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001105 *
1106 * Allocate the required number of page tables. Extremely similar to
1107 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1108 * the page directory boundary (instead of the page directory pointer). That
1109 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1110 * possible, and likely that the caller will need to use multiple calls of this
1111 * function to achieve the appropriate allocation.
1112 *
1113 * Return: 0 if success; negative error code otherwise.
1114 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001115static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001116 struct i915_page_directory *pd,
Chris Wilsondd196742017-02-15 08:43:46 +00001117 u64 start, u64 length)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001118{
Michel Thierryd7b26332015-04-08 12:13:34 +01001119 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +00001120 u64 from = start;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001121 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001122
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001123 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001124 /* Don't reallocate page tables */
Chris Wilsondd196742017-02-15 08:43:46 +00001125 if (!test_bit(pde, pd->used_pdes)) {
1126 pt = alloc_pt(vm);
1127 if (IS_ERR(pt))
1128 goto unwind;
1129
1130 gen8_initialize_pt(vm, pt);
1131 pd->page_table[pde] = pt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001132 }
Chris Wilsondd196742017-02-15 08:43:46 +00001133 pt->used_ptes += gen8_pte_count(start, length);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001134 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001135 }
1136
1137 return 0;
1138
Chris Wilsondd196742017-02-15 08:43:46 +00001139unwind:
1140 gen8_ppgtt_clear_pd(vm, pd, from, start - from);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001141 return -ENOMEM;
1142}
1143
Michel Thierryd7b26332015-04-08 12:13:34 +01001144/**
1145 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001146 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001147 * @pdp: Page directory pointer for this address range.
1148 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001149 * @length: Size of the allocations.
1150 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001151 * caller to free on error.
1152 *
1153 * Allocate the required number of page directories starting at the pde index of
1154 * @start, and ending at the pde index @start + @length. This function will skip
1155 * over already allocated page directories within the range, and only allocate
1156 * new ones, setting the appropriate pointer within the pdp as well as the
1157 * correct position in the bitmap @new_pds.
1158 *
1159 * The function will only allocate the pages within the range for a give page
1160 * directory pointer. In other words, if @start + @length straddles a virtually
1161 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1162 * required by the caller, This is not currently possible, and the BUG in the
1163 * code will prevent it.
1164 *
1165 * Return: 0 if success; negative error code otherwise.
1166 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001167static int
1168gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1169 struct i915_page_directory_pointer *pdp,
1170 uint64_t start,
1171 uint64_t length,
1172 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001173{
Michel Thierryd7b26332015-04-08 12:13:34 +01001174 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001175 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001176 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001177
Michel Thierry6ac18502015-07-29 17:23:46 +01001178 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001179
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001180 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001181 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001182 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001183
Chris Wilson84486612017-02-15 08:43:40 +00001184 pd = alloc_pd(vm);
Michel Thierryd7b26332015-04-08 12:13:34 +01001185 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001186 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001187
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001188 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001189 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001190 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001191 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001192 }
1193
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001194 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001195
1196unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001197 for_each_set_bit(pdpe, new_pds, pdpes)
Chris Wilson84486612017-02-15 08:43:40 +00001198 free_pd(vm, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001199
1200 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001201}
1202
Michel Thierry762d9932015-07-30 11:05:29 +01001203/**
1204 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1205 * @vm: Master vm structure.
1206 * @pml4: Page map level 4 for this address range.
1207 * @start: Starting virtual address to begin allocations.
1208 * @length: Size of the allocations.
1209 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1210 * caller to free on error.
1211 *
1212 * Allocate the required number of page directory pointers. Extremely similar to
1213 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1214 * The main difference is here we are limited by the pml4 boundary (instead of
1215 * the page directory pointer).
1216 *
1217 * Return: 0 if success; negative error code otherwise.
1218 */
1219static int
1220gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1221 struct i915_pml4 *pml4,
1222 uint64_t start,
1223 uint64_t length,
1224 unsigned long *new_pdps)
1225{
Michel Thierry762d9932015-07-30 11:05:29 +01001226 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001227 uint32_t pml4e;
1228
1229 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1230
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001231 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001232 if (!test_bit(pml4e, pml4->used_pml4es)) {
Chris Wilson84486612017-02-15 08:43:40 +00001233 pdp = alloc_pdp(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001234 if (IS_ERR(pdp))
1235 goto unwind_out;
1236
Michel Thierry69ab76f2015-07-29 17:23:55 +01001237 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001238 pml4->pdps[pml4e] = pdp;
1239 __set_bit(pml4e, new_pdps);
1240 trace_i915_page_directory_pointer_entry_alloc(vm,
1241 pml4e,
1242 start,
1243 GEN8_PML4E_SHIFT);
1244 }
1245 }
1246
1247 return 0;
1248
1249unwind_out:
1250 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson84486612017-02-15 08:43:40 +00001251 free_pdp(vm, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001252
1253 return -ENOMEM;
1254}
1255
Michel Thierryd7b26332015-04-08 12:13:34 +01001256static void
Chris Wilsondd196742017-02-15 08:43:46 +00001257free_gen8_temp_bitmaps(unsigned long *new_pds)
Michel Thierryd7b26332015-04-08 12:13:34 +01001258{
Michel Thierryd7b26332015-04-08 12:13:34 +01001259 kfree(new_pds);
1260}
1261
1262/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1263 * of these are based on the number of PDPEs in the system.
1264 */
1265static
1266int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michel Thierry6ac18502015-07-29 17:23:46 +01001267 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001268{
Michel Thierryd7b26332015-04-08 12:13:34 +01001269 unsigned long *pds;
Michel Thierryd7b26332015-04-08 12:13:34 +01001270
Michał Winiarski3a41a052015-09-03 19:22:18 +02001271 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001272 if (!pds)
1273 return -ENOMEM;
1274
Michel Thierryd7b26332015-04-08 12:13:34 +01001275 *new_pds = pds;
Michel Thierryd7b26332015-04-08 12:13:34 +01001276 return 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001277}
1278
Michel Thierry762d9932015-07-30 11:05:29 +01001279static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1280 struct i915_page_directory_pointer *pdp,
1281 uint64_t start,
1282 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001283{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001284 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilsondd196742017-02-15 08:43:46 +00001285 unsigned long *new_page_dirs;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001286 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001287 const uint64_t orig_start = start;
1288 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001289 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001290 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001291 int ret;
1292
Chris Wilsondd196742017-02-15 08:43:46 +00001293 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001294 if (ret)
1295 return ret;
1296
Michel Thierryd7b26332015-04-08 12:13:34 +01001297 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001298 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1299 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001300 if (ret) {
Chris Wilsondd196742017-02-15 08:43:46 +00001301 free_gen8_temp_bitmaps(new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001302 return ret;
1303 }
1304
1305 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001306 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilsondd196742017-02-15 08:43:46 +00001307 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length);
Michel Thierry5441f0c2015-04-08 12:13:28 +01001308 if (ret)
1309 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001310 }
1311
Michel Thierry33c88192015-04-08 12:13:33 +01001312 start = orig_start;
1313 length = orig_length;
1314
Michel Thierryd7b26332015-04-08 12:13:34 +01001315 /* Allocations have completed successfully, so set the bitmaps, and do
1316 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001317 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilson9231da72017-02-15 08:43:41 +00001318 gen8_pde_t *const page_directory = kmap_atomic_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001319 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001320 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001321 uint64_t pd_start = start;
1322 uint32_t pde;
1323
Michel Thierryd7b26332015-04-08 12:13:34 +01001324 /* Every pd should be allocated, we just did that above. */
1325 WARN_ON(!pd);
1326
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001327 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001328 /* Same reasoning as pd */
1329 WARN_ON(!pt);
1330 WARN_ON(!pd_len);
1331 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1332
Michel Thierryd7b26332015-04-08 12:13:34 +01001333 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001334 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001335
1336 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001337 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1338 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001339 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1340 gen8_pte_index(start),
Chris Wilsondd196742017-02-15 08:43:46 +00001341 gen8_pte_count(start, length));
Michel Thierryd7b26332015-04-08 12:13:34 +01001342
1343 /* NB: We haven't yet mapped ptes to pages. At this
1344 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001345 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001346
Chris Wilson9231da72017-02-15 08:43:41 +00001347 kunmap_atomic(page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001348 __set_bit(pdpe, pdp->used_pdpes);
Matthew Auld5c693b22016-12-13 16:05:10 +00001349 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001350 }
1351
Chris Wilsondd196742017-02-15 08:43:46 +00001352 free_gen8_temp_bitmaps(new_page_dirs);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001353 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001354 return 0;
1355
1356err_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001357 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Chris Wilson84486612017-02-15 08:43:40 +00001358 free_pd(vm, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001359
Chris Wilsondd196742017-02-15 08:43:46 +00001360 free_gen8_temp_bitmaps(new_page_dirs);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001361 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001362 return ret;
1363}
1364
Michel Thierry762d9932015-07-30 11:05:29 +01001365static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1366 struct i915_pml4 *pml4,
1367 uint64_t start,
1368 uint64_t length)
1369{
1370 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Michel Thierry762d9932015-07-30 11:05:29 +01001371 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001372 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001373 int ret = 0;
1374
1375 /* Do the pml4 allocations first, so we don't need to track the newly
1376 * allocated tables below the pdp */
1377 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1378
1379 /* The pagedirectory and pagetable allocations are done in the shared 3
1380 * and 4 level code. Just allocate the pdps.
1381 */
1382 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1383 new_pdps);
1384 if (ret)
1385 return ret;
1386
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001387 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001388 WARN_ON(!pdp);
1389
1390 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1391 if (ret)
1392 goto err_out;
1393
Chris Wilson84486612017-02-15 08:43:40 +00001394 gen8_setup_pml4e(pml4, pdp, pml4e);
Michel Thierry762d9932015-07-30 11:05:29 +01001395 }
1396
1397 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1398 GEN8_PML4ES_PER_PML4);
1399
1400 return 0;
1401
1402err_out:
1403 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson84486612017-02-15 08:43:40 +00001404 gen8_ppgtt_cleanup_3lvl(vm, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001405
1406 return ret;
1407}
1408
1409static int gen8_alloc_va_range(struct i915_address_space *vm,
1410 uint64_t start, uint64_t length)
1411{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001412 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001413
Chris Wilsonc6385c92016-11-29 12:42:05 +00001414 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001415 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1416 else
1417 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1418}
1419
Chris Wilson84486612017-02-15 08:43:40 +00001420static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
1421 struct i915_page_directory_pointer *pdp,
Michel Thierryea91e402015-07-29 17:23:57 +01001422 uint64_t start, uint64_t length,
1423 gen8_pte_t scratch_pte,
1424 struct seq_file *m)
1425{
1426 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001427 uint32_t pdpe;
1428
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001429 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001430 struct i915_page_table *pt;
1431 uint64_t pd_len = length;
1432 uint64_t pd_start = start;
1433 uint32_t pde;
1434
1435 if (!test_bit(pdpe, pdp->used_pdpes))
1436 continue;
1437
1438 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001439 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001440 uint32_t pte;
1441 gen8_pte_t *pt_vaddr;
1442
1443 if (!test_bit(pde, pd->used_pdes))
1444 continue;
1445
Chris Wilson9231da72017-02-15 08:43:41 +00001446 pt_vaddr = kmap_atomic_px(pt);
Michel Thierryea91e402015-07-29 17:23:57 +01001447 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1448 uint64_t va =
1449 (pdpe << GEN8_PDPE_SHIFT) |
1450 (pde << GEN8_PDE_SHIFT) |
1451 (pte << GEN8_PTE_SHIFT);
1452 int i;
1453 bool found = false;
1454
1455 for (i = 0; i < 4; i++)
1456 if (pt_vaddr[pte + i] != scratch_pte)
1457 found = true;
1458 if (!found)
1459 continue;
1460
1461 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1462 for (i = 0; i < 4; i++) {
1463 if (pt_vaddr[pte + i] != scratch_pte)
1464 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1465 else
1466 seq_puts(m, " SCRATCH ");
1467 }
1468 seq_puts(m, "\n");
1469 }
Michel Thierryea91e402015-07-29 17:23:57 +01001470 kunmap_atomic(pt_vaddr);
1471 }
1472 }
1473}
1474
1475static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1476{
1477 struct i915_address_space *vm = &ppgtt->base;
1478 uint64_t start = ppgtt->base.start;
1479 uint64_t length = ppgtt->base.total;
Chris Wilson894cceb2017-02-15 08:43:37 +00001480 const gen8_pte_t scratch_pte =
1481 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001482
Chris Wilsonc6385c92016-11-29 12:42:05 +00001483 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Chris Wilson84486612017-02-15 08:43:40 +00001484 gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001485 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001486 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001487 struct i915_pml4 *pml4 = &ppgtt->pml4;
1488 struct i915_page_directory_pointer *pdp;
1489
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001490 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001491 if (!test_bit(pml4e, pml4->used_pml4es))
1492 continue;
1493
1494 seq_printf(m, " PML4E #%llu\n", pml4e);
Chris Wilson84486612017-02-15 08:43:40 +00001495 gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001496 }
1497 }
1498}
1499
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001500static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1501{
Chris Wilsondd196742017-02-15 08:43:46 +00001502 unsigned long *new_page_dirs;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001503 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001504 int ret;
1505
1506 /* We allocate temp bitmap for page tables for no gain
1507 * but as this is for init only, lets keep the things simple
1508 */
Chris Wilsondd196742017-02-15 08:43:46 +00001509 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, pdpes);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001510 if (ret)
1511 return ret;
1512
1513 /* Allocate for all pdps regardless of how the ppgtt
1514 * was defined.
1515 */
1516 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1517 0, 1ULL << 32,
1518 new_page_dirs);
1519 if (!ret)
1520 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1521
Chris Wilsondd196742017-02-15 08:43:46 +00001522 free_gen8_temp_bitmaps(new_page_dirs);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001523
1524 return ret;
1525}
1526
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001527/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001528 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1529 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1530 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1531 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001532 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001533 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001534static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001535{
Chris Wilson49d73912016-11-29 09:50:08 +00001536 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001537 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001538
Mika Kuoppala8776f022015-06-30 18:16:40 +03001539 ret = gen8_init_scratch(&ppgtt->base);
1540 if (ret)
1541 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001542
Michel Thierryd7b26332015-04-08 12:13:34 +01001543 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001544 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001545 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001546 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001547 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1548 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001549 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001550
Chris Wilson84486612017-02-15 08:43:40 +00001551 /* There are only few exceptions for gen >=6. chv and bxt.
1552 * And we are not sure about the latter so play safe for now.
1553 */
1554 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
1555 ppgtt->base.pt_kmap_wc = true;
1556
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001557 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Chris Wilson84486612017-02-15 08:43:40 +00001558 ret = setup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001559 if (ret)
1560 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001561
Michel Thierry69ab76f2015-07-29 17:23:55 +01001562 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1563
Michel Thierry762d9932015-07-30 11:05:29 +01001564 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001565 ppgtt->switch_mm = gen8_48b_mm_switch;
Chris Wilson894cceb2017-02-15 08:43:37 +00001566
1567 ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
Michel Thierry762d9932015-07-30 11:05:29 +01001568 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001569 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001570 if (ret)
1571 goto free_scratch;
1572
1573 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001574 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001575 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1576 0, 0,
1577 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001578
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001579 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001580 ret = gen8_preallocate_top_level_pdps(ppgtt);
1581 if (ret)
1582 goto free_scratch;
1583 }
Chris Wilson894cceb2017-02-15 08:43:37 +00001584
1585 ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001586 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001587
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001588 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001589 gen8_ppgtt_notify_vgt(ppgtt, true);
1590
Michel Thierryd7b26332015-04-08 12:13:34 +01001591 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001592
1593free_scratch:
1594 gen8_free_scratch(&ppgtt->base);
1595 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001596}
1597
Ben Widawsky87d60b62013-12-06 14:11:29 -08001598static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1599{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001600 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001601 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001602 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001603 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001604 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001605 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001606
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001607 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001608 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001609
Dave Gordon731f74c2016-06-24 19:37:46 +01001610 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001611 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001612 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001613 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001614 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001615 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1616
1617 if (pd_entry != expected)
1618 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1619 pde,
1620 pd_entry,
1621 expected);
1622 seq_printf(m, "\tPDE: %x\n", pd_entry);
1623
Chris Wilson9231da72017-02-15 08:43:41 +00001624 pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[pde]);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001625
Michel Thierry07749ef2015-03-16 16:00:54 +00001626 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001627 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001628 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001629 (pte * PAGE_SIZE);
1630 int i;
1631 bool found = false;
1632 for (i = 0; i < 4; i++)
1633 if (pt_vaddr[pte + i] != scratch_pte)
1634 found = true;
1635 if (!found)
1636 continue;
1637
1638 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1639 for (i = 0; i < 4; i++) {
1640 if (pt_vaddr[pte + i] != scratch_pte)
1641 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1642 else
1643 seq_puts(m, " SCRATCH ");
1644 }
1645 seq_puts(m, "\n");
1646 }
Chris Wilson9231da72017-02-15 08:43:41 +00001647 kunmap_atomic(pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001648 }
1649}
1650
Ben Widawsky678d96f2015-03-16 16:00:56 +00001651/* Write pde (index) from the page directory @pd to the page table @pt */
Chris Wilson16a011c2017-02-15 08:43:45 +00001652static inline void gen6_write_pde(const struct i915_hw_ppgtt *ppgtt,
1653 const unsigned int pde,
1654 const struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001655{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001656 /* Caller needs to make sure the write completes if necessary */
Chris Wilson16a011c2017-02-15 08:43:45 +00001657 writel_relaxed(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
1658 ppgtt->pd_addr + pde);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001659}
Ben Widawsky61973492013-04-08 18:43:54 -07001660
Ben Widawsky678d96f2015-03-16 16:00:56 +00001661/* Write all the page tables found in the ppgtt structure to incrementing page
1662 * directories. */
Chris Wilson16a011c2017-02-15 08:43:45 +00001663static void gen6_write_page_range(struct i915_hw_ppgtt *ppgtt,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001664 uint32_t start, uint32_t length)
1665{
Michel Thierryec565b32015-04-08 12:13:23 +01001666 struct i915_page_table *pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001667 unsigned int pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001668
Chris Wilson16a011c2017-02-15 08:43:45 +00001669 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde)
1670 gen6_write_pde(ppgtt, pde, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001671
Chris Wilson16a011c2017-02-15 08:43:45 +00001672 mark_tlbs_dirty(ppgtt);
Chris Wilsondd196742017-02-15 08:43:46 +00001673 wmb();
Ben Widawsky3e302542013-04-23 23:15:32 -07001674}
1675
Chris Wilsondd196742017-02-15 08:43:46 +00001676static inline uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001677{
Chris Wilsondd196742017-02-15 08:43:46 +00001678 GEM_BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1679 return ppgtt->pd.base.ggtt_offset << 10;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001680}
Ben Widawsky61973492013-04-08 18:43:54 -07001681
Ben Widawsky90252e52013-12-06 14:11:12 -08001682static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001683 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001684{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001685 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001686 u32 *cs;
Ben Widawsky90252e52013-12-06 14:11:12 -08001687 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001688
Ben Widawsky90252e52013-12-06 14:11:12 -08001689 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001690 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001691 if (ret)
1692 return ret;
1693
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001694 cs = intel_ring_begin(req, 6);
1695 if (IS_ERR(cs))
1696 return PTR_ERR(cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001697
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001698 *cs++ = MI_LOAD_REGISTER_IMM(2);
1699 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1700 *cs++ = PP_DIR_DCLV_2G;
1701 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1702 *cs++ = get_pd_offset(ppgtt);
1703 *cs++ = MI_NOOP;
1704 intel_ring_advance(req, cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001705
1706 return 0;
1707}
1708
Ben Widawsky48a10382013-12-06 14:11:11 -08001709static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001710 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001711{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001712 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001713 u32 *cs;
Ben Widawsky48a10382013-12-06 14:11:11 -08001714 int ret;
1715
Ben Widawsky48a10382013-12-06 14:11:11 -08001716 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001717 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001718 if (ret)
1719 return ret;
1720
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001721 cs = intel_ring_begin(req, 6);
1722 if (IS_ERR(cs))
1723 return PTR_ERR(cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001724
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001725 *cs++ = MI_LOAD_REGISTER_IMM(2);
1726 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1727 *cs++ = PP_DIR_DCLV_2G;
1728 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1729 *cs++ = get_pd_offset(ppgtt);
1730 *cs++ = MI_NOOP;
1731 intel_ring_advance(req, cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001732
Ben Widawsky90252e52013-12-06 14:11:12 -08001733 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001734 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001735 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001736 if (ret)
1737 return ret;
1738 }
1739
Ben Widawsky48a10382013-12-06 14:11:11 -08001740 return 0;
1741}
1742
Ben Widawskyeeb94882013-12-06 14:11:10 -08001743static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001744 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001745{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001746 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001747 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001748
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001749 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1750 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001751 return 0;
1752}
1753
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001754static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001755{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001756 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301757 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001758
Akash Goel3b3f1652016-10-13 22:44:48 +05301759 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001760 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1761 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001762 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001763 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001764 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001765}
1766
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001767static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001768{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001769 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001770 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301771 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001772
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001773 ecobits = I915_READ(GAC_ECO_BITS);
1774 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1775
1776 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001777 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001778 ecochk |= ECOCHK_PPGTT_WB_HSW;
1779 } else {
1780 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1781 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1782 }
1783 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001784
Akash Goel3b3f1652016-10-13 22:44:48 +05301785 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001786 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001787 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001788 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001789 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001790}
1791
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001792static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001793{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001794 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001795
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001796 ecobits = I915_READ(GAC_ECO_BITS);
1797 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1798 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001799
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001800 gab_ctl = I915_READ(GAB_CTL);
1801 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001802
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001803 ecochk = I915_READ(GAM_ECOCHK);
1804 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001805
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001806 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001807}
1808
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001809/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001810static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Chris Wilsondd196742017-02-15 08:43:46 +00001811 u64 start, u64 length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001812{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001813 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilsondd196742017-02-15 08:43:46 +00001814 unsigned int first_entry = start >> PAGE_SHIFT;
1815 unsigned int pde = first_entry / GEN6_PTES;
1816 unsigned int pte = first_entry % GEN6_PTES;
1817 unsigned int num_entries = length >> PAGE_SHIFT;
1818 gen6_pte_t scratch_pte =
1819 vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001820
Daniel Vetter7bddb012012-02-09 17:15:47 +01001821 while (num_entries) {
Chris Wilsondd196742017-02-15 08:43:46 +00001822 struct i915_page_table *pt = ppgtt->pd.page_table[pde++];
1823 unsigned int end = min(pte + num_entries, GEN6_PTES);
1824 gen6_pte_t *vaddr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001825
Chris Wilsondd196742017-02-15 08:43:46 +00001826 num_entries -= end - pte;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001827
Chris Wilsondd196742017-02-15 08:43:46 +00001828 /* Note that the hw doesn't support removing PDE on the fly
1829 * (they are cached inside the context with no means to
1830 * invalidate the cache), so we can only reset the PTE
1831 * entries back to scratch.
1832 */
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001833
Chris Wilsondd196742017-02-15 08:43:46 +00001834 vaddr = kmap_atomic_px(pt);
1835 do {
1836 vaddr[pte++] = scratch_pte;
1837 } while (pte < end);
1838 kunmap_atomic(vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001839
Chris Wilsondd196742017-02-15 08:43:46 +00001840 pte = 0;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001841 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001842}
1843
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001844static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001845 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001846 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301847 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001848{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001849 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001850 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001851 unsigned act_pt = first_entry / GEN6_PTES;
1852 unsigned act_pte = first_entry % GEN6_PTES;
Chris Wilsonb31144c2017-02-15 08:43:36 +00001853 const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1854 struct sgt_dma iter;
1855 gen6_pte_t *vaddr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001856
Chris Wilson9231da72017-02-15 08:43:41 +00001857 vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
Chris Wilsonb31144c2017-02-15 08:43:36 +00001858 iter.sg = pages->sgl;
1859 iter.dma = sg_dma_address(iter.sg);
1860 iter.max = iter.dma + iter.sg->length;
1861 do {
1862 vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001863
Chris Wilsonb31144c2017-02-15 08:43:36 +00001864 iter.dma += PAGE_SIZE;
1865 if (iter.dma == iter.max) {
1866 iter.sg = __sg_next(iter.sg);
1867 if (!iter.sg)
1868 break;
1869
1870 iter.dma = sg_dma_address(iter.sg);
1871 iter.max = iter.dma + iter.sg->length;
1872 }
Akash Goel24f3a8c2014-06-17 10:59:42 +05301873
Michel Thierry07749ef2015-03-16 16:00:54 +00001874 if (++act_pte == GEN6_PTES) {
Chris Wilson9231da72017-02-15 08:43:41 +00001875 kunmap_atomic(vaddr);
1876 vaddr = kmap_atomic_px(ppgtt->pd.page_table[++act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +02001877 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001878 }
Chris Wilsonb31144c2017-02-15 08:43:36 +00001879 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +00001880 kunmap_atomic(vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001881}
1882
Ben Widawsky678d96f2015-03-16 16:00:56 +00001883static int gen6_alloc_va_range(struct i915_address_space *vm,
Chris Wilsondd196742017-02-15 08:43:46 +00001884 u64 start, u64 length)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001885{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001886 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001887 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +00001888 u64 from = start;
1889 unsigned int pde;
1890 bool flush = false;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001891
Dave Gordon731f74c2016-06-24 19:37:46 +01001892 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Chris Wilsondd196742017-02-15 08:43:46 +00001893 if (pt == vm->scratch_pt) {
1894 pt = alloc_pt(vm);
1895 if (IS_ERR(pt))
1896 goto unwind_out;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001897
Chris Wilsondd196742017-02-15 08:43:46 +00001898 gen6_initialize_pt(vm, pt);
1899 ppgtt->pd.page_table[pde] = pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001900 gen6_write_pde(ppgtt, pde, pt);
Chris Wilsondd196742017-02-15 08:43:46 +00001901 flush = true;
1902 }
Ben Widawsky678d96f2015-03-16 16:00:56 +00001903 }
1904
Chris Wilsondd196742017-02-15 08:43:46 +00001905 if (flush) {
1906 mark_tlbs_dirty(ppgtt);
1907 wmb();
1908 }
Michel Thierry4933d512015-03-24 15:46:22 +00001909
Ben Widawsky678d96f2015-03-16 16:00:56 +00001910 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001911
1912unwind_out:
Chris Wilsondd196742017-02-15 08:43:46 +00001913 gen6_ppgtt_clear_range(vm, from, start);
1914 return -ENOMEM;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001915}
1916
Mika Kuoppala8776f022015-06-30 18:16:40 +03001917static int gen6_init_scratch(struct i915_address_space *vm)
1918{
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001919 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001920
Chris Wilson84486612017-02-15 08:43:40 +00001921 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001922 if (ret)
1923 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001924
Chris Wilson84486612017-02-15 08:43:40 +00001925 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001926 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson84486612017-02-15 08:43:40 +00001927 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001928 return PTR_ERR(vm->scratch_pt);
1929 }
1930
1931 gen6_initialize_pt(vm, vm->scratch_pt);
1932
1933 return 0;
1934}
1935
1936static void gen6_free_scratch(struct i915_address_space *vm)
1937{
Chris Wilson84486612017-02-15 08:43:40 +00001938 free_pt(vm, vm->scratch_pt);
1939 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001940}
1941
Daniel Vetter061dd492015-04-14 17:35:13 +02001942static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08001943{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001944 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01001945 struct i915_page_directory *pd = &ppgtt->pd;
Michel Thierry09942c62015-04-08 12:13:30 +01001946 struct i915_page_table *pt;
1947 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08001948
Daniel Vetter061dd492015-04-14 17:35:13 +02001949 drm_mm_remove_node(&ppgtt->node);
1950
Dave Gordon731f74c2016-06-24 19:37:46 +01001951 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001952 if (pt != vm->scratch_pt)
Chris Wilson84486612017-02-15 08:43:40 +00001953 free_pt(vm, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001954
Mika Kuoppala8776f022015-06-30 18:16:40 +03001955 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08001956}
1957
Ben Widawskyb1465202014-02-19 22:05:49 -08001958static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001959{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001960 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00001961 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001962 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08001963 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001964
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001965 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1966 * allocator works in address space sizes, so it's multiplied by page
1967 * size. We allocate at the top of the GTT to avoid fragmentation.
1968 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001969 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00001970
Mika Kuoppala8776f022015-06-30 18:16:40 +03001971 ret = gen6_init_scratch(vm);
1972 if (ret)
1973 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00001974
Chris Wilsone007b192017-01-11 11:23:10 +00001975 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
1976 GEN6_PD_SIZE, GEN6_PD_ALIGN,
1977 I915_COLOR_UNEVICTABLE,
1978 0, ggtt->base.total,
1979 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00001980 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001981 goto err_out;
1982
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001983 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001984 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001985
Chris Wilson52c126e2017-02-15 08:43:43 +00001986 ppgtt->pd.base.ggtt_offset =
1987 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
1988
1989 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
1990 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
1991
Ben Widawskyc8c26622015-01-22 17:01:25 +00001992 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001993
1994err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03001995 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001996 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08001997}
1998
Ben Widawskyb1465202014-02-19 22:05:49 -08001999static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2000{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002001 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002002}
2003
Michel Thierry4933d512015-03-24 15:46:22 +00002004static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2005 uint64_t start, uint64_t length)
2006{
Michel Thierryec565b32015-04-08 12:13:23 +01002007 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002008 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002009
Dave Gordon731f74c2016-06-24 19:37:46 +01002010 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002011 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002012}
2013
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002014static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002015{
Chris Wilson49d73912016-11-29 09:50:08 +00002016 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002017 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002018 int ret;
2019
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002020 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002021 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002022 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002023 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002024 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002025 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002026 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002027 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002028 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002029
2030 ret = gen6_ppgtt_alloc(ppgtt);
2031 if (ret)
2032 return ret;
2033
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002034 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2035 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002036 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2037 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002038 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002039 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002040 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002041 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002042
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002043 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Chris Wilson16a011c2017-02-15 08:43:45 +00002044 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002045
Chris Wilson52c126e2017-02-15 08:43:43 +00002046 ret = gen6_alloc_va_range(&ppgtt->base, 0, ppgtt->base.total);
2047 if (ret) {
2048 gen6_ppgtt_cleanup(&ppgtt->base);
2049 return ret;
2050 }
2051
Thierry Reding440fd522015-01-23 09:05:06 +01002052 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002053 ppgtt->node.size >> 20,
2054 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002055
Chris Wilson52c126e2017-02-15 08:43:43 +00002056 DRM_DEBUG_DRIVER("Adding PPGTT at offset %x\n",
2057 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002058
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002059 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002060}
2061
Chris Wilson2bfa9962016-08-04 07:52:25 +01002062static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2063 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002064{
Chris Wilson49d73912016-11-29 09:50:08 +00002065 ppgtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00002066 ppgtt->base.dma = &dev_priv->drm.pdev->dev;
Daniel Vetter3440d262013-01-24 13:49:56 -08002067
Chris Wilson2bfa9962016-08-04 07:52:25 +01002068 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002069 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002070 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002071 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002072}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002073
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002074static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002075 struct drm_i915_private *dev_priv,
2076 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002077{
Chris Wilson80b204b2016-10-28 13:58:58 +01002078 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00002079
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002080 drm_mm_init(&vm->mm, vm->start, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00002081 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
2082
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002083 INIT_LIST_HEAD(&vm->active_list);
2084 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002085 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00002086
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002087 list_add_tail(&vm->global_link, &dev_priv->vm_list);
Chris Wilson84486612017-02-15 08:43:40 +00002088 pagevec_init(&vm->free_pages, false);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002089}
2090
Matthew Aulded9724d2016-11-17 21:04:10 +00002091static void i915_address_space_fini(struct i915_address_space *vm)
2092{
Chris Wilson84486612017-02-15 08:43:40 +00002093 if (pagevec_count(&vm->free_pages))
2094 vm_free_pages_release(vm);
2095
Matthew Aulded9724d2016-11-17 21:04:10 +00002096 i915_gem_timeline_fini(&vm->timeline);
2097 drm_mm_takedown(&vm->mm);
2098 list_del(&vm->global_link);
2099}
2100
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002101static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002102{
Tim Gored5165eb2016-02-04 11:49:34 +00002103 /* This function is for gtt related workarounds. This function is
2104 * called on driver load and after a GPU reset, so you can place
2105 * workarounds here even if they get overwritten by GPU reset.
2106 */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002107 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002108 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002109 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002110 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002111 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08002112 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002113 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002114 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002115 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2116}
2117
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002118int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002119{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002120 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002121
Thomas Daniel671b50132014-08-20 16:24:50 +01002122 /* In the case of execlists, PPGTT is enabled by the context descriptor
2123 * and the PDPs are contained within the context itself. We don't
2124 * need to do anything here. */
2125 if (i915.enable_execlists)
2126 return 0;
2127
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002128 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002129 return 0;
2130
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002131 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002132 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002133 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002134 gen7_ppgtt_enable(dev_priv);
2135 else if (INTEL_GEN(dev_priv) >= 8)
2136 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002137 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002138 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002139
John Harrison4ad2fd82015-06-18 13:11:20 +01002140 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002141}
John Harrison4ad2fd82015-06-18 13:11:20 +01002142
Daniel Vetter4d884702014-08-06 15:04:47 +02002143struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002144i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002145 struct drm_i915_file_private *fpriv,
2146 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002147{
2148 struct i915_hw_ppgtt *ppgtt;
2149 int ret;
2150
2151 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2152 if (!ppgtt)
2153 return ERR_PTR(-ENOMEM);
2154
Chris Wilson1188bc62017-02-15 08:43:38 +00002155 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetter4d884702014-08-06 15:04:47 +02002156 if (ret) {
2157 kfree(ppgtt);
2158 return ERR_PTR(ret);
2159 }
2160
Chris Wilson1188bc62017-02-15 08:43:38 +00002161 kref_init(&ppgtt->ref);
2162 i915_address_space_init(&ppgtt->base, dev_priv, name);
2163 ppgtt->base.file = fpriv;
2164
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002165 trace_i915_ppgtt_create(&ppgtt->base);
2166
Daniel Vetter4d884702014-08-06 15:04:47 +02002167 return ppgtt;
2168}
2169
Chris Wilson0c7eeda2017-01-11 21:09:25 +00002170void i915_ppgtt_close(struct i915_address_space *vm)
2171{
2172 struct list_head *phases[] = {
2173 &vm->active_list,
2174 &vm->inactive_list,
2175 &vm->unbound_list,
2176 NULL,
2177 }, **phase;
2178
2179 GEM_BUG_ON(vm->closed);
2180 vm->closed = true;
2181
2182 for (phase = phases; *phase; phase++) {
2183 struct i915_vma *vma, *vn;
2184
2185 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2186 if (!i915_vma_is_closed(vma))
2187 i915_vma_close(vma);
2188 }
2189}
2190
Matthew Aulded9724d2016-11-17 21:04:10 +00002191void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002192{
2193 struct i915_hw_ppgtt *ppgtt =
2194 container_of(kref, struct i915_hw_ppgtt, ref);
2195
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002196 trace_i915_ppgtt_release(&ppgtt->base);
2197
Chris Wilson50e046b2016-08-04 07:52:46 +01002198 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002199 WARN_ON(!list_empty(&ppgtt->base.active_list));
2200 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002201 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002202
2203 ppgtt->base.cleanup(&ppgtt->base);
Chris Wilson84486612017-02-15 08:43:40 +00002204 i915_address_space_fini(&ppgtt->base);
Daniel Vetteree960be2014-08-06 15:04:45 +02002205 kfree(ppgtt);
2206}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002207
Ben Widawskya81cc002013-01-18 12:30:31 -08002208/* Certain Gen5 chipsets require require idling the GPU before
2209 * unmapping anything from the GTT when VT-d is enabled.
2210 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002211static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002212{
2213#ifdef CONFIG_INTEL_IOMMU
2214 /* Query intel_iommu to see if we need the workaround. Presumably that
2215 * was loaded first.
2216 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002217 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002218 return true;
2219#endif
2220 return false;
2221}
2222
Chris Wilsondc979972016-05-10 14:10:04 +01002223void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002224{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002225 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302226 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002227
Chris Wilsondc979972016-05-10 14:10:04 +01002228 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002229 return;
2230
Akash Goel3b3f1652016-10-13 22:44:48 +05302231 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002232 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002233 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002234 if (fault_reg & RING_FAULT_VALID) {
2235 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002236 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002237 "\tAddress space: %s\n"
2238 "\tSource ID: %d\n"
2239 "\tType: %d\n",
2240 fault_reg & PAGE_MASK,
2241 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2242 RING_FAULT_SRCID(fault_reg),
2243 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002244 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002245 fault_reg & ~RING_FAULT_VALID);
2246 }
2247 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302248
2249 /* Engine specific init may not have been done till this point. */
2250 if (dev_priv->engine[RCS])
2251 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002252}
2253
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002254void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002255{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002256 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002257
2258 /* Don't bother messing with faults pre GEN6 as we have little
2259 * documentation supporting that it's a good idea.
2260 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002261 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002262 return;
2263
Chris Wilsondc979972016-05-10 14:10:04 +01002264 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002265
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002266 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002267
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002268 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002269}
2270
Chris Wilson03ac84f2016-10-28 13:58:36 +01002271int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2272 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002273{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002274 do {
2275 if (dma_map_sg(&obj->base.dev->pdev->dev,
2276 pages->sgl, pages->nents,
2277 PCI_DMA_BIDIRECTIONAL))
2278 return 0;
2279
2280 /* If the DMA remap fails, one cause can be that we have
2281 * too many objects pinned in a small remapping table,
2282 * such as swiotlb. Incrementally purge all other objects and
2283 * try again - if there are no more pages to remove from
2284 * the DMA remapper, i915_gem_shrink will return 0.
2285 */
2286 GEM_BUG_ON(obj->mm.pages == pages);
2287 } while (i915_gem_shrink(to_i915(obj->base.dev),
2288 obj->base.size >> PAGE_SHIFT,
2289 I915_SHRINK_BOUND |
2290 I915_SHRINK_UNBOUND |
2291 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002292
Chris Wilson03ac84f2016-10-28 13:58:36 +01002293 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002294}
2295
Daniel Vetter2c642b02015-04-14 17:35:26 +02002296static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002297{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002298 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002299}
2300
Chris Wilsond6473f52016-06-10 14:22:59 +05302301static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2302 dma_addr_t addr,
2303 uint64_t offset,
2304 enum i915_cache_level level,
2305 u32 unused)
2306{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002307 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302308 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002309 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302310
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002311 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302312
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002313 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302314}
2315
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002316static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2317 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002318 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302319 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002320{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002321 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002322 struct sgt_iter sgt_iter;
2323 gen8_pte_t __iomem *gtt_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +00002324 const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002325 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002326
Chris Wilson894cceb2017-02-15 08:43:37 +00002327 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
2328 gtt_entries += start >> PAGE_SHIFT;
2329 for_each_sgt_dma(addr, sgt_iter, st)
2330 gen8_set_pte(gtt_entries++, pte_encode | addr);
Dave Gordon85d12252016-05-20 11:54:06 +01002331
Chris Wilson894cceb2017-02-15 08:43:37 +00002332 wmb();
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002333
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002334 /* This next bit makes the above posting read even more important. We
2335 * want to flush the TLBs only after we're certain all the PTE updates
2336 * have finished.
2337 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002338 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002339}
2340
Chris Wilsonc1403302015-11-18 15:19:39 +00002341struct insert_entries {
2342 struct i915_address_space *vm;
2343 struct sg_table *st;
2344 uint64_t start;
2345 enum i915_cache_level level;
2346 u32 flags;
2347};
2348
2349static int gen8_ggtt_insert_entries__cb(void *_arg)
2350{
2351 struct insert_entries *arg = _arg;
2352 gen8_ggtt_insert_entries(arg->vm, arg->st,
2353 arg->start, arg->level, arg->flags);
2354 return 0;
2355}
2356
2357static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2358 struct sg_table *st,
2359 uint64_t start,
2360 enum i915_cache_level level,
2361 u32 flags)
2362{
2363 struct insert_entries arg = { vm, st, start, level, flags };
2364 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2365}
2366
Chris Wilsond6473f52016-06-10 14:22:59 +05302367static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2368 dma_addr_t addr,
2369 uint64_t offset,
2370 enum i915_cache_level level,
2371 u32 flags)
2372{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002373 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302374 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002375 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302376
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002377 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302378
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002379 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302380}
2381
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002382/*
2383 * Binds an object into the global gtt with the specified cache level. The object
2384 * will be accessible to the GPU via commands whose operands reference offsets
2385 * within the global GTT as well as accessible by the GPU through the GMADR
2386 * mapped BAR (dev_priv->mm.gtt->gtt).
2387 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002388static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002389 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002390 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302391 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002392{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002393 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsonb31144c2017-02-15 08:43:36 +00002394 gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
2395 unsigned int i = start >> PAGE_SHIFT;
2396 struct sgt_iter iter;
Dave Gordon85d12252016-05-20 11:54:06 +01002397 dma_addr_t addr;
Chris Wilsonb31144c2017-02-15 08:43:36 +00002398 for_each_sgt_dma(addr, iter, st)
2399 iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2400 wmb();
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002401
2402 /* This next bit makes the above posting read even more important. We
2403 * want to flush the TLBs only after we're certain all the PTE updates
2404 * have finished.
2405 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002406 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002407}
2408
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002409static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002410 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002411{
2412}
2413
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002414static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002415 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002416{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002417 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002418 unsigned first_entry = start >> PAGE_SHIFT;
2419 unsigned num_entries = length >> PAGE_SHIFT;
Chris Wilson894cceb2017-02-15 08:43:37 +00002420 const gen8_pte_t scratch_pte =
2421 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
2422 gen8_pte_t __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002423 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2424 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002425 int i;
2426
2427 if (WARN(num_entries > max_entries,
2428 "First entry = %d; Num entries = %d (max=%d)\n",
2429 first_entry, num_entries, max_entries))
2430 num_entries = max_entries;
2431
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002432 for (i = 0; i < num_entries; i++)
2433 gen8_set_pte(&gtt_base[i], scratch_pte);
2434 readl(gtt_base);
2435}
2436
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002437static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002438 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002439 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002440{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002441 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002442 unsigned first_entry = start >> PAGE_SHIFT;
2443 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002444 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002445 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2446 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002447 int i;
2448
2449 if (WARN(num_entries > max_entries,
2450 "First entry = %d; Num entries = %d (max=%d)\n",
2451 first_entry, num_entries, max_entries))
2452 num_entries = max_entries;
2453
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002454 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002455 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002456
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002457 for (i = 0; i < num_entries; i++)
2458 iowrite32(scratch_pte, &gtt_base[i]);
2459 readl(gtt_base);
2460}
2461
Chris Wilsond6473f52016-06-10 14:22:59 +05302462static void i915_ggtt_insert_page(struct i915_address_space *vm,
2463 dma_addr_t addr,
2464 uint64_t offset,
2465 enum i915_cache_level cache_level,
2466 u32 unused)
2467{
Chris Wilsond6473f52016-06-10 14:22:59 +05302468 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2469 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302470
2471 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302472}
2473
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002474static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2475 struct sg_table *pages,
2476 uint64_t start,
2477 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002478{
2479 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2480 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2481
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002482 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002483
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002484}
2485
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002486static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002487 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002488 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002489{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002490 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002491}
2492
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002493static int ggtt_bind_vma(struct i915_vma *vma,
2494 enum i915_cache_level cache_level,
2495 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002496{
Chris Wilson49d73912016-11-29 09:50:08 +00002497 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002498 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonba7a5742017-02-15 08:43:35 +00002499 u32 pte_flags;
Daniel Vetter0a878712015-10-15 14:23:01 +02002500
Chris Wilsonba7a5742017-02-15 08:43:35 +00002501 if (unlikely(!vma->pages)) {
2502 int ret = i915_get_ggtt_vma_pages(vma);
2503 if (ret)
2504 return ret;
2505 }
Daniel Vetter0a878712015-10-15 14:23:01 +02002506
2507 /* Currently applicable only to VLV */
Chris Wilsonba7a5742017-02-15 08:43:35 +00002508 pte_flags = 0;
Daniel Vetter0a878712015-10-15 14:23:01 +02002509 if (obj->gt_ro)
2510 pte_flags |= PTE_READ_ONLY;
2511
Chris Wilson9c870d02016-10-24 13:42:15 +01002512 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002513 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002514 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002515 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002516
2517 /*
2518 * Without aliasing PPGTT there's no difference between
2519 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2520 * upgrade to both bound if we bind either to avoid double-binding.
2521 */
Chris Wilson3272db52016-08-04 16:32:32 +01002522 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002523
2524 return 0;
2525}
2526
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002527static void ggtt_unbind_vma(struct i915_vma *vma)
2528{
2529 struct drm_i915_private *i915 = vma->vm->i915;
2530
2531 intel_runtime_pm_get(i915);
2532 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2533 intel_runtime_pm_put(i915);
2534}
2535
Daniel Vetter0a878712015-10-15 14:23:01 +02002536static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2537 enum i915_cache_level cache_level,
2538 u32 flags)
2539{
Chris Wilson49d73912016-11-29 09:50:08 +00002540 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002541 u32 pte_flags;
Chris Wilsonff685972017-02-15 08:43:42 +00002542 int ret;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002543
Chris Wilsonba7a5742017-02-15 08:43:35 +00002544 if (unlikely(!vma->pages)) {
Chris Wilsonff685972017-02-15 08:43:42 +00002545 ret = i915_get_ggtt_vma_pages(vma);
Chris Wilsonba7a5742017-02-15 08:43:35 +00002546 if (ret)
2547 return ret;
2548 }
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002549
Akash Goel24f3a8c2014-06-17 10:59:42 +05302550 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002551 pte_flags = 0;
2552 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002553 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302554
Chris Wilsonff685972017-02-15 08:43:42 +00002555 if (flags & I915_VMA_LOCAL_BIND) {
2556 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
2557
2558 if (appgtt->base.allocate_va_range) {
2559 ret = appgtt->base.allocate_va_range(&appgtt->base,
2560 vma->node.start,
2561 vma->node.size);
2562 if (ret)
2563 return ret;
2564 }
2565
2566 appgtt->base.insert_entries(&appgtt->base,
2567 vma->pages, vma->node.start,
2568 cache_level, pte_flags);
2569 }
2570
Chris Wilson3272db52016-08-04 16:32:32 +01002571 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002572 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002573 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002574 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002575 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002576 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002577 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002578
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002579 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002580}
2581
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002582static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002583{
Chris Wilson49d73912016-11-29 09:50:08 +00002584 struct drm_i915_private *i915 = vma->vm->i915;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002585
Chris Wilson9c870d02016-10-24 13:42:15 +01002586 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2587 intel_runtime_pm_get(i915);
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002588 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002589 intel_runtime_pm_put(i915);
2590 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002591
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002592 if (vma->flags & I915_VMA_LOCAL_BIND) {
2593 struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
2594
2595 vm->clear_range(vm, vma->node.start, vma->size);
2596 }
Daniel Vetter74163902012-02-15 23:50:21 +01002597}
2598
Chris Wilson03ac84f2016-10-28 13:58:36 +01002599void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2600 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002601{
David Weinehall52a05c32016-08-22 13:32:44 +03002602 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2603 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002604 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002605
Chris Wilson307dc252016-08-05 10:14:12 +01002606 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002607 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002608 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2609 /* Wait a bit, in hopes it avoids the hang */
2610 udelay(10);
2611 }
2612 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002613
Chris Wilson03ac84f2016-10-28 13:58:36 +01002614 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002615}
Daniel Vetter644ec022012-03-26 09:45:40 +02002616
Chris Wilson45b186f2016-12-16 07:46:42 +00002617static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002618 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002619 u64 *start,
2620 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002621{
Chris Wilsona6508de2017-02-06 08:45:47 +00002622 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002623 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002624
Chris Wilsona6508de2017-02-06 08:45:47 +00002625 /* Also leave a space between the unallocated reserved node after the
2626 * GTT and any objects within the GTT, i.e. we use the color adjustment
2627 * to insert a guard page to prevent prefetches crossing over the
2628 * GTT boundary.
2629 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002630 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002631 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002632 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002633}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002634
Chris Wilson6cde9a02017-02-13 17:15:50 +00002635int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2636{
2637 struct i915_ggtt *ggtt = &i915->ggtt;
2638 struct i915_hw_ppgtt *ppgtt;
2639 int err;
2640
Chris Wilson1188bc62017-02-15 08:43:38 +00002641 ppgtt = i915_ppgtt_create(i915, NULL, "[alias]");
2642 if (IS_ERR(ppgtt))
2643 return PTR_ERR(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002644
2645 if (ppgtt->base.allocate_va_range) {
2646 err = ppgtt->base.allocate_va_range(&ppgtt->base,
2647 0, ppgtt->base.total);
2648 if (err)
Chris Wilson1188bc62017-02-15 08:43:38 +00002649 goto err_ppgtt;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002650 }
2651
Chris Wilson6cde9a02017-02-13 17:15:50 +00002652 i915->mm.aliasing_ppgtt = ppgtt;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002653
Chris Wilson6cde9a02017-02-13 17:15:50 +00002654 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2655 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2656
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002657 WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
2658 ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
2659
Chris Wilson6cde9a02017-02-13 17:15:50 +00002660 return 0;
2661
Chris Wilson6cde9a02017-02-13 17:15:50 +00002662err_ppgtt:
Chris Wilson1188bc62017-02-15 08:43:38 +00002663 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002664 return err;
2665}
2666
2667void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2668{
2669 struct i915_ggtt *ggtt = &i915->ggtt;
2670 struct i915_hw_ppgtt *ppgtt;
2671
2672 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2673 if (!ppgtt)
2674 return;
2675
Chris Wilson1188bc62017-02-15 08:43:38 +00002676 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002677
2678 ggtt->base.bind_vma = ggtt_bind_vma;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002679 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002680}
2681
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002682int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002683{
Ben Widawskye78891c2013-01-25 16:41:04 -08002684 /* Let GEM Manage all of the aperture.
2685 *
2686 * However, leave one page at the end still bound to the scratch page.
2687 * There are a number of places where the hardware apparently prefetches
2688 * past the end of the object, and we've seen multiple hangs with the
2689 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2690 * aperture. One page should be enough to keep any prefetching inside
2691 * of the aperture.
2692 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002693 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002694 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002695 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002696 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002697
Zhi Wangb02d22a2016-06-16 08:06:59 -04002698 ret = intel_vgt_balloon(dev_priv);
2699 if (ret)
2700 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002701
Chris Wilson95374d72016-10-12 10:05:20 +01002702 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002703 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2704 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2705 0, ggtt->mappable_end,
2706 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002707 if (ret)
2708 return ret;
2709
Chris Wilsoned2f3452012-11-15 11:32:19 +00002710 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002711 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002712 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2713 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002714 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002715 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002716 }
2717
2718 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002719 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002720 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002721
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002722 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Chris Wilson6cde9a02017-02-13 17:15:50 +00002723 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
Chris Wilson95374d72016-10-12 10:05:20 +01002724 if (ret)
Chris Wilson6cde9a02017-02-13 17:15:50 +00002725 goto err;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002726 }
2727
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002728 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002729
Chris Wilson95374d72016-10-12 10:05:20 +01002730err:
2731 drm_mm_remove_node(&ggtt->error_capture);
2732 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002733}
2734
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002735/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002736 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002737 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002738 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002739void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002740{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002741 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002742 struct i915_vma *vma, *vn;
2743
2744 ggtt->base.closed = true;
2745
2746 mutex_lock(&dev_priv->drm.struct_mutex);
2747 WARN_ON(!list_empty(&ggtt->base.active_list));
2748 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2749 WARN_ON(i915_vma_unbind(vma));
2750 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002751
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002752 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002753
Chris Wilson1188bc62017-02-15 08:43:38 +00002754 mutex_lock(&dev_priv->drm.struct_mutex);
2755 i915_gem_fini_aliasing_ppgtt(dev_priv);
2756
Chris Wilson95374d72016-10-12 10:05:20 +01002757 if (drm_mm_node_allocated(&ggtt->error_capture))
2758 drm_mm_remove_node(&ggtt->error_capture);
2759
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002760 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002761 intel_vgt_deballoon(dev_priv);
Matthew Aulded9724d2016-11-17 21:04:10 +00002762 i915_address_space_fini(&ggtt->base);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002763 }
2764
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002765 ggtt->base.cleanup(&ggtt->base);
Chris Wilson1188bc62017-02-15 08:43:38 +00002766 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002767
2768 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002769 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002770}
Daniel Vetter70e32542014-08-06 15:04:57 +02002771
Daniel Vetter2c642b02015-04-14 17:35:26 +02002772static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002773{
2774 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2775 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2776 return snb_gmch_ctl << 20;
2777}
2778
Daniel Vetter2c642b02015-04-14 17:35:26 +02002779static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002780{
2781 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2782 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2783 if (bdw_gmch_ctl)
2784 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002785
2786#ifdef CONFIG_X86_32
2787 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2788 if (bdw_gmch_ctl > 4)
2789 bdw_gmch_ctl = 4;
2790#endif
2791
Ben Widawsky9459d252013-11-03 16:53:55 -08002792 return bdw_gmch_ctl << 20;
2793}
2794
Daniel Vetter2c642b02015-04-14 17:35:26 +02002795static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002796{
2797 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2798 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2799
2800 if (gmch_ctrl)
2801 return 1 << (20 + gmch_ctrl);
2802
2803 return 0;
2804}
2805
Daniel Vetter2c642b02015-04-14 17:35:26 +02002806static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002807{
2808 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2809 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2810 return snb_gmch_ctl << 25; /* 32 MB units */
2811}
2812
Daniel Vetter2c642b02015-04-14 17:35:26 +02002813static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002814{
2815 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2816 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2817 return bdw_gmch_ctl << 25; /* 32 MB units */
2818}
2819
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002820static size_t chv_get_stolen_size(u16 gmch_ctrl)
2821{
2822 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2823 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2824
2825 /*
2826 * 0x0 to 0x10: 32MB increments starting at 0MB
2827 * 0x11 to 0x16: 4MB increments starting at 8MB
2828 * 0x17 to 0x1d: 4MB increments start at 36MB
2829 */
2830 if (gmch_ctrl < 0x11)
2831 return gmch_ctrl << 25;
2832 else if (gmch_ctrl < 0x17)
2833 return (gmch_ctrl - 0x11 + 2) << 22;
2834 else
2835 return (gmch_ctrl - 0x17 + 9) << 22;
2836}
2837
Damien Lespiau66375012014-01-09 18:02:46 +00002838static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2839{
2840 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2841 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2842
2843 if (gen9_gmch_ctl < 0xf0)
2844 return gen9_gmch_ctl << 25; /* 32 MB units */
2845 else
2846 /* 4MB increments starting at 0xf0 for 4MB */
2847 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2848}
2849
Chris Wilson34c998b2016-08-04 07:52:24 +01002850static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002851{
Chris Wilson49d73912016-11-29 09:50:08 +00002852 struct drm_i915_private *dev_priv = ggtt->base.i915;
2853 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002854 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002855 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002856
2857 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002858 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002859
Imre Deak2a073f892015-03-27 13:07:33 +02002860 /*
2861 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2862 * dropped. For WC mappings in general we have 64 byte burst writes
2863 * when the WC buffer is flushed, so we can't use it, but have to
2864 * resort to an uncached mapping. The WC issue is easily caught by the
2865 * readback check when writing GTT PTE entries.
2866 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002867 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002868 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002869 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002870 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002871 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002872 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002873 return -ENOMEM;
2874 }
2875
Chris Wilson84486612017-02-15 08:43:40 +00002876 ret = setup_scratch_page(&ggtt->base, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002877 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002878 DRM_ERROR("Scratch setup failed\n");
2879 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002880 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002881 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002882 }
2883
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002884 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002885}
2886
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002887/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2888 * bits. When using advanced contexts each context stores its own PAT, but
2889 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002890static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002891{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002892 uint64_t pat;
2893
2894 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2895 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2896 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2897 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2898 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2899 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2900 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2901 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2902
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002903 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002904 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2905 * so RTL will always use the value corresponding to
2906 * pat_sel = 000".
2907 * So let's disable cache for GGTT to avoid screen corruptions.
2908 * MOCS still can be used though.
2909 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2910 * before this patch, i.e. the same uncached + snooping access
2911 * like on gen6/7 seems to be in effect.
2912 * - So this just fixes blitter/render access. Again it looks
2913 * like it's not just uncached access, but uncached + snooping.
2914 * So we can still hold onto all our assumptions wrt cpu
2915 * clflushing on LLC machines.
2916 */
2917 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2918
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002919 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2920 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002921 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2922 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002923}
2924
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002925static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2926{
2927 uint64_t pat;
2928
2929 /*
2930 * Map WB on BDW to snooped on CHV.
2931 *
2932 * Only the snoop bit has meaning for CHV, the rest is
2933 * ignored.
2934 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002935 * The hardware will never snoop for certain types of accesses:
2936 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2937 * - PPGTT page tables
2938 * - some other special cycles
2939 *
2940 * As with BDW, we also need to consider the following for GT accesses:
2941 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2942 * so RTL will always use the value corresponding to
2943 * pat_sel = 000".
2944 * Which means we must set the snoop bit in PAT entry 0
2945 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002946 */
2947 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2948 GEN8_PPAT(1, 0) |
2949 GEN8_PPAT(2, 0) |
2950 GEN8_PPAT(3, 0) |
2951 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2952 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2953 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2954 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2955
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002956 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2957 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002958}
2959
Chris Wilson34c998b2016-08-04 07:52:24 +01002960static void gen6_gmch_remove(struct i915_address_space *vm)
2961{
2962 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2963
2964 iounmap(ggtt->gsm);
Chris Wilson84486612017-02-15 08:43:40 +00002965 cleanup_scratch_page(vm);
Chris Wilson34c998b2016-08-04 07:52:24 +01002966}
2967
Joonas Lahtinend507d732016-03-18 10:42:58 +02002968static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08002969{
Chris Wilson49d73912016-11-29 09:50:08 +00002970 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002971 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002972 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08002973 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08002974
2975 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002976 ggtt->mappable_base = pci_resource_start(pdev, 2);
2977 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08002978
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002979 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
2980 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08002981
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002982 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08002983
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002984 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002985 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002986 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002987 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002988 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002989 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002990 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002991 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002992 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002993 }
Ben Widawsky63340132013-11-04 19:32:22 -08002994
Chris Wilson34c998b2016-08-04 07:52:24 +01002995 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002996
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002997 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002998 chv_setup_private_ppat(dev_priv);
2999 else
3000 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003001
Chris Wilson34c998b2016-08-04 07:52:24 +01003002 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003003 ggtt->base.bind_vma = ggtt_bind_vma;
3004 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303005 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003006 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003007 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003008 ggtt->base.clear_range = gen8_ggtt_clear_range;
3009
3010 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3011 if (IS_CHERRYVIEW(dev_priv))
3012 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3013
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003014 ggtt->invalidate = gen6_ggtt_invalidate;
3015
Chris Wilson34c998b2016-08-04 07:52:24 +01003016 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003017}
3018
Joonas Lahtinend507d732016-03-18 10:42:58 +02003019static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003020{
Chris Wilson49d73912016-11-29 09:50:08 +00003021 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003022 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003023 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003024 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003025
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003026 ggtt->mappable_base = pci_resource_start(pdev, 2);
3027 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003028
Ben Widawskybaa09f52013-01-24 13:49:57 -08003029 /* 64/512MB is the current min/max we actually know of, but this is just
3030 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003031 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003032 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003033 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003034 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003035 }
3036
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003037 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3038 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3039 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003040
Joonas Lahtinend507d732016-03-18 10:42:58 +02003041 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003042
Chris Wilson34c998b2016-08-04 07:52:24 +01003043 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3044 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003045
Joonas Lahtinend507d732016-03-18 10:42:58 +02003046 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303047 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003048 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3049 ggtt->base.bind_vma = ggtt_bind_vma;
3050 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003051 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003052
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003053 ggtt->invalidate = gen6_ggtt_invalidate;
3054
Chris Wilson34c998b2016-08-04 07:52:24 +01003055 if (HAS_EDRAM(dev_priv))
3056 ggtt->base.pte_encode = iris_pte_encode;
3057 else if (IS_HASWELL(dev_priv))
3058 ggtt->base.pte_encode = hsw_pte_encode;
3059 else if (IS_VALLEYVIEW(dev_priv))
3060 ggtt->base.pte_encode = byt_pte_encode;
3061 else if (INTEL_GEN(dev_priv) >= 7)
3062 ggtt->base.pte_encode = ivb_pte_encode;
3063 else
3064 ggtt->base.pte_encode = snb_pte_encode;
3065
3066 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003067}
3068
Chris Wilson34c998b2016-08-04 07:52:24 +01003069static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003070{
Chris Wilson34c998b2016-08-04 07:52:24 +01003071 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003072}
3073
Joonas Lahtinend507d732016-03-18 10:42:58 +02003074static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003075{
Chris Wilson49d73912016-11-29 09:50:08 +00003076 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003077 int ret;
3078
Chris Wilson91c8a322016-07-05 10:40:23 +01003079 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003080 if (!ret) {
3081 DRM_ERROR("failed to set up gmch\n");
3082 return -EIO;
3083 }
3084
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003085 intel_gtt_get(&ggtt->base.total,
3086 &ggtt->stolen_size,
3087 &ggtt->mappable_base,
3088 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003089
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003090 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303091 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003092 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3093 ggtt->base.clear_range = i915_ggtt_clear_range;
3094 ggtt->base.bind_vma = ggtt_bind_vma;
3095 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003096 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003097
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003098 ggtt->invalidate = gmch_ggtt_invalidate;
3099
Joonas Lahtinend507d732016-03-18 10:42:58 +02003100 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003101 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3102
Ben Widawskybaa09f52013-01-24 13:49:57 -08003103 return 0;
3104}
3105
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003106/**
Chris Wilson0088e522016-08-04 07:52:21 +01003107 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003108 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003109 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003110int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003111{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003112 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003113 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003114
Chris Wilson49d73912016-11-29 09:50:08 +00003115 ggtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00003116 ggtt->base.dma = &dev_priv->drm.pdev->dev;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003117
Chris Wilson34c998b2016-08-04 07:52:24 +01003118 if (INTEL_GEN(dev_priv) <= 5)
3119 ret = i915_gmch_probe(ggtt);
3120 else if (INTEL_GEN(dev_priv) < 8)
3121 ret = gen6_gmch_probe(ggtt);
3122 else
3123 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003124 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003125 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003126
Chris Wilsondb9309a2017-01-05 15:30:23 +00003127 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3128 * This is easier than doing range restriction on the fly, as we
3129 * currently don't have any bits spare to pass in this upper
3130 * restriction!
3131 */
3132 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3133 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3134 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3135 }
3136
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003137 if ((ggtt->base.total - 1) >> 32) {
3138 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003139 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003140 ggtt->base.total >> 20);
3141 ggtt->base.total = 1ULL << 32;
3142 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3143 }
3144
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003145 if (ggtt->mappable_end > ggtt->base.total) {
3146 DRM_ERROR("mappable aperture extends past end of GGTT,"
3147 " aperture=%llx, total=%llx\n",
3148 ggtt->mappable_end, ggtt->base.total);
3149 ggtt->mappable_end = ggtt->base.total;
3150 }
3151
Ben Widawskybaa09f52013-01-24 13:49:57 -08003152 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003153 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003154 ggtt->base.total >> 20);
3155 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003156 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003157#ifdef CONFIG_INTEL_IOMMU
3158 if (intel_iommu_gfx_mapped)
3159 DRM_INFO("VT-d active for gfx access\n");
3160#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003161
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003162 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003163}
3164
3165/**
3166 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003167 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003168 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003169int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003170{
Chris Wilson0088e522016-08-04 07:52:21 +01003171 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3172 int ret;
3173
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003174 INIT_LIST_HEAD(&dev_priv->vm_list);
3175
Chris Wilsona6508de2017-02-06 08:45:47 +00003176 /* Note that we use page colouring to enforce a guard page at the
3177 * end of the address space. This is required as the CS may prefetch
3178 * beyond the end of the batch buffer, across the page boundary,
3179 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003180 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003181 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01003182 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00003183 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003184 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003185 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003186
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003187 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3188 dev_priv->ggtt.mappable_base,
3189 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003190 ret = -EIO;
3191 goto out_gtt_cleanup;
3192 }
3193
3194 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3195
Chris Wilson0088e522016-08-04 07:52:21 +01003196 /*
3197 * Initialise stolen early so that we may reserve preallocated
3198 * objects for the BIOS to KMS transition.
3199 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003200 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003201 if (ret)
3202 goto out_gtt_cleanup;
3203
3204 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003205
3206out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003207 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003208 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003209}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003210
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003211int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003212{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003213 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003214 return -EIO;
3215
3216 return 0;
3217}
3218
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003219void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3220{
3221 i915->ggtt.invalidate = guc_ggtt_invalidate;
3222}
3223
3224void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3225{
3226 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3227}
3228
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003229void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003230{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003231 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003232 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003233
Chris Wilsondc979972016-05-10 14:10:04 +01003234 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003235
3236 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003237 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003238
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003239 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3240
3241 /* clflush objects bound into the GGTT and rebind them. */
3242 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003243 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003244 bool ggtt_bound = false;
3245 struct i915_vma *vma;
3246
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003247 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003248 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003249 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003250
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003251 if (!i915_vma_unbind(vma))
3252 continue;
3253
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003254 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3255 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003256 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003257 }
3258
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003259 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003260 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003261 }
3262
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003263 ggtt->base.closed = false;
3264
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003265 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003266 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003267 chv_setup_private_ppat(dev_priv);
3268 else
3269 bdw_setup_private_ppat(dev_priv);
3270
3271 return;
3272 }
3273
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003274 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003275 struct i915_address_space *vm;
3276
Daniel Vetterfa423312015-04-14 17:35:23 +02003277 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003278 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003279
Chris Wilson2bfa9962016-08-04 07:52:25 +01003280 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003281 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003282 else
3283 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003284
Chris Wilson16a011c2017-02-15 08:43:45 +00003285 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003286 }
3287 }
3288
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003289 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003290}
3291
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003292static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003293rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003294 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003295 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003296 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003297{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003298 unsigned int column, row;
3299 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003300
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003301 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003302 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003303 for (row = 0; row < height; row++) {
3304 st->nents++;
3305 /* We don't need the pages, but need to initialize
3306 * the entries so the sg list can be happily traversed.
3307 * The only thing we need are DMA addresses.
3308 */
3309 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003310 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003311 sg_dma_len(sg) = PAGE_SIZE;
3312 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003313 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003314 }
3315 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003316
3317 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003318}
3319
Chris Wilsonba7a5742017-02-15 08:43:35 +00003320static noinline struct sg_table *
3321intel_rotate_pages(struct intel_rotation_info *rot_info,
3322 struct drm_i915_gem_object *obj)
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003323{
Dave Gordon85d12252016-05-20 11:54:06 +01003324 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003325 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003326 struct sgt_iter sgt_iter;
3327 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003328 unsigned long i;
3329 dma_addr_t *page_addr_list;
3330 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003331 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003332 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003333
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003334 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003335 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003336 sizeof(dma_addr_t),
3337 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003338 if (!page_addr_list)
3339 return ERR_PTR(ret);
3340
3341 /* Allocate target SG list. */
3342 st = kmalloc(sizeof(*st), GFP_KERNEL);
3343 if (!st)
3344 goto err_st_alloc;
3345
Ville Syrjälä6687c902015-09-15 13:16:41 +03003346 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003347 if (ret)
3348 goto err_sg_alloc;
3349
3350 /* Populate source page list from the object. */
3351 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003352 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003353 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003354
Dave Gordon85d12252016-05-20 11:54:06 +01003355 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003356 st->nents = 0;
3357 sg = st->sgl;
3358
Ville Syrjälä6687c902015-09-15 13:16:41 +03003359 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3360 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3361 rot_info->plane[i].width, rot_info->plane[i].height,
3362 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003363 }
3364
Ville Syrjälä6687c902015-09-15 13:16:41 +03003365 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3366 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003367
3368 drm_free_large(page_addr_list);
3369
3370 return st;
3371
3372err_sg_alloc:
3373 kfree(st);
3374err_st_alloc:
3375 drm_free_large(page_addr_list);
3376
Ville Syrjälä6687c902015-09-15 13:16:41 +03003377 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3378 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3379
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003380 return ERR_PTR(ret);
3381}
3382
Chris Wilsonba7a5742017-02-15 08:43:35 +00003383static noinline struct sg_table *
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003384intel_partial_pages(const struct i915_ggtt_view *view,
3385 struct drm_i915_gem_object *obj)
3386{
3387 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003388 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003389 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003390 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003391 int ret = -ENOMEM;
3392
3393 st = kmalloc(sizeof(*st), GFP_KERNEL);
3394 if (!st)
3395 goto err_st_alloc;
3396
Chris Wilsond2a84a72016-10-28 13:58:34 +01003397 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003398 if (ret)
3399 goto err_sg_alloc;
3400
Chris Wilson8bab11932017-01-14 00:28:25 +00003401 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003402 GEM_BUG_ON(!iter);
3403
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003404 sg = st->sgl;
3405 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003406 do {
3407 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003408
Chris Wilsond2a84a72016-10-28 13:58:34 +01003409 len = min(iter->length - (offset << PAGE_SHIFT),
3410 count << PAGE_SHIFT);
3411 sg_set_page(sg, NULL, len, 0);
3412 sg_dma_address(sg) =
3413 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3414 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003415
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003416 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003417 count -= len >> PAGE_SHIFT;
3418 if (count == 0) {
3419 sg_mark_end(sg);
3420 return st;
3421 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003422
Chris Wilsond2a84a72016-10-28 13:58:34 +01003423 sg = __sg_next(sg);
3424 iter = __sg_next(iter);
3425 offset = 0;
3426 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003427
3428err_sg_alloc:
3429 kfree(st);
3430err_st_alloc:
3431 return ERR_PTR(ret);
3432}
3433
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003434static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003435i915_get_ggtt_vma_pages(struct i915_vma *vma)
3436{
Chris Wilsonba7a5742017-02-15 08:43:35 +00003437 int ret;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003438
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003439 /* The vma->pages are only valid within the lifespan of the borrowed
3440 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3441 * must be the vma->pages. A simple rule is that vma->pages must only
3442 * be accessed when the obj->mm.pages are pinned.
3443 */
3444 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3445
Chris Wilsonba7a5742017-02-15 08:43:35 +00003446 switch (vma->ggtt_view.type) {
3447 case I915_GGTT_VIEW_NORMAL:
3448 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003449 return 0;
3450
Chris Wilsonba7a5742017-02-15 08:43:35 +00003451 case I915_GGTT_VIEW_ROTATED:
Chris Wilson247177d2016-08-15 10:48:47 +01003452 vma->pages =
Chris Wilsonba7a5742017-02-15 08:43:35 +00003453 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3454 break;
3455
3456 case I915_GGTT_VIEW_PARTIAL:
Chris Wilson247177d2016-08-15 10:48:47 +01003457 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003458 break;
3459
3460 default:
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003461 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3462 vma->ggtt_view.type);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003463 return -EINVAL;
3464 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003465
Chris Wilsonba7a5742017-02-15 08:43:35 +00003466 ret = 0;
3467 if (unlikely(IS_ERR(vma->pages))) {
Chris Wilson247177d2016-08-15 10:48:47 +01003468 ret = PTR_ERR(vma->pages);
3469 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003470 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3471 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003472 }
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003473 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003474}
3475
Chris Wilsone007b192017-01-11 11:23:10 +00003476/**
Chris Wilson625d9882017-01-11 11:23:11 +00003477 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003478 * @vm: the &struct i915_address_space
3479 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3480 * @size: how much space to allocate inside the GTT,
3481 * must be #I915_GTT_PAGE_SIZE aligned
3482 * @offset: where to insert inside the GTT,
3483 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3484 * (@offset + @size) must fit within the address space
3485 * @color: color to apply to node, if this node is not from a VMA,
3486 * color must be #I915_COLOR_UNEVICTABLE
3487 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003488 *
3489 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3490 * the address space (using @size and @color). If the @node does not fit, it
3491 * tries to evict any overlapping nodes from the GTT, including any
3492 * neighbouring nodes if the colors do not match (to ensure guard pages between
3493 * differing domains). See i915_gem_evict_for_node() for the gory details
3494 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3495 * evicting active overlapping objects, and any overlapping node that is pinned
3496 * or marked as unevictable will also result in failure.
3497 *
3498 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3499 * asked to wait for eviction and interrupted.
3500 */
3501int i915_gem_gtt_reserve(struct i915_address_space *vm,
3502 struct drm_mm_node *node,
3503 u64 size, u64 offset, unsigned long color,
3504 unsigned int flags)
3505{
3506 int err;
3507
3508 GEM_BUG_ON(!size);
3509 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3510 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3511 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003512 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003513 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003514
3515 node->size = size;
3516 node->start = offset;
3517 node->color = color;
3518
3519 err = drm_mm_reserve_node(&vm->mm, node);
3520 if (err != -ENOSPC)
3521 return err;
3522
3523 err = i915_gem_evict_for_node(vm, node, flags);
3524 if (err == 0)
3525 err = drm_mm_reserve_node(&vm->mm, node);
3526
3527 return err;
3528}
3529
Chris Wilson606fec92017-01-11 11:23:12 +00003530static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3531{
3532 u64 range, addr;
3533
3534 GEM_BUG_ON(range_overflows(start, len, end));
3535 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3536
3537 range = round_down(end - len, align) - round_up(start, align);
3538 if (range) {
3539 if (sizeof(unsigned long) == sizeof(u64)) {
3540 addr = get_random_long();
3541 } else {
3542 addr = get_random_int();
3543 if (range > U32_MAX) {
3544 addr <<= 32;
3545 addr |= get_random_int();
3546 }
3547 }
3548 div64_u64_rem(addr, range, &addr);
3549 start += addr;
3550 }
3551
3552 return round_up(start, align);
3553}
3554
Chris Wilson625d9882017-01-11 11:23:11 +00003555/**
Chris Wilsone007b192017-01-11 11:23:10 +00003556 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003557 * @vm: the &struct i915_address_space
3558 * @node: the &struct drm_mm_node (typically i915_vma.node)
3559 * @size: how much space to allocate inside the GTT,
3560 * must be #I915_GTT_PAGE_SIZE aligned
3561 * @alignment: required alignment of starting offset, may be 0 but
3562 * if specified, this must be a power-of-two and at least
3563 * #I915_GTT_MIN_ALIGNMENT
3564 * @color: color to apply to node
3565 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003566 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003567 * @end: end of any range restriction inside GTT (U64_MAX for all),
3568 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3569 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003570 *
3571 * i915_gem_gtt_insert() first searches for an available hole into which
3572 * is can insert the node. The hole address is aligned to @alignment and
3573 * its @size must then fit entirely within the [@start, @end] bounds. The
3574 * nodes on either side of the hole must match @color, or else a guard page
3575 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003576 * suitable hole is found, first a victim is randomly selected and tested
3577 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003578 * is scanned to find the first set of replacement nodes to create the hole.
3579 * Those old overlapping nodes are evicted from the GTT (and so must be
3580 * rebound before any future use). Any node that is currently pinned cannot
3581 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3582 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3583 * searching for an eviction candidate. See i915_gem_evict_something() for
3584 * the gory details on the eviction algorithm.
3585 *
3586 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3587 * asked to wait for eviction and interrupted.
3588 */
3589int i915_gem_gtt_insert(struct i915_address_space *vm,
3590 struct drm_mm_node *node,
3591 u64 size, u64 alignment, unsigned long color,
3592 u64 start, u64 end, unsigned int flags)
3593{
Chris Wilson4e64e552017-02-02 21:04:38 +00003594 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003595 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003596 int err;
3597
3598 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3599 GEM_BUG_ON(!size);
3600 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3601 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3602 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3603 GEM_BUG_ON(start >= end);
3604 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3605 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003606 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003607 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003608
3609 if (unlikely(range_overflows(start, size, end)))
3610 return -ENOSPC;
3611
3612 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3613 return -ENOSPC;
3614
Chris Wilson4e64e552017-02-02 21:04:38 +00003615 mode = DRM_MM_INSERT_BEST;
3616 if (flags & PIN_HIGH)
3617 mode = DRM_MM_INSERT_HIGH;
3618 if (flags & PIN_MAPPABLE)
3619 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003620
3621 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3622 * so we know that we always have a minimum alignment of 4096.
3623 * The drm_mm range manager is optimised to return results
3624 * with zero alignment, so where possible use the optimal
3625 * path.
3626 */
3627 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3628 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3629 alignment = 0;
3630
Chris Wilson4e64e552017-02-02 21:04:38 +00003631 err = drm_mm_insert_node_in_range(&vm->mm, node,
3632 size, alignment, color,
3633 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003634 if (err != -ENOSPC)
3635 return err;
3636
Chris Wilson606fec92017-01-11 11:23:12 +00003637 /* No free space, pick a slot at random.
3638 *
3639 * There is a pathological case here using a GTT shared between
3640 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3641 *
3642 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3643 * (64k objects) (448k objects)
3644 *
3645 * Now imagine that the eviction LRU is ordered top-down (just because
3646 * pathology meets real life), and that we need to evict an object to
3647 * make room inside the aperture. The eviction scan then has to walk
3648 * the 448k list before it finds one within range. And now imagine that
3649 * it has to search for a new hole between every byte inside the memcpy,
3650 * for several simultaneous clients.
3651 *
3652 * On a full-ppgtt system, if we have run out of available space, there
3653 * will be lots and lots of objects in the eviction list! Again,
3654 * searching that LRU list may be slow if we are also applying any
3655 * range restrictions (e.g. restriction to low 4GiB) and so, for
3656 * simplicity and similarilty between different GTT, try the single
3657 * random replacement first.
3658 */
3659 offset = random_offset(start, end,
3660 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3661 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3662 if (err != -ENOSPC)
3663 return err;
3664
3665 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003666 err = i915_gem_evict_something(vm, size, alignment, color,
3667 start, end, flags);
3668 if (err)
3669 return err;
3670
Chris Wilson4e64e552017-02-02 21:04:38 +00003671 return drm_mm_insert_node_in_range(&vm->mm, node,
3672 size, alignment, color,
3673 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003674}
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003675
3676#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3677#include "selftests/mock_gtt.c"
Chris Wilson1c428192017-02-13 17:15:38 +00003678#include "selftests/i915_gem_gtt.c"
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003679#endif