blob: d60f38adc4c43aa49b1db183d2c859bf565d8c4b [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
Laura Abbotted3ba072017-05-08 15:58:17 -070034#include <asm/set_memory.h>
35
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
37#include <drm/i915_drm.h>
Chris Wilsone007b192017-01-11 11:23:10 +000038
Daniel Vetter76aaf222010-11-05 22:23:30 +010039#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080040#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010041#include "i915_trace.h"
42#include "intel_drv.h"
Chris Wilsond07f0e52016-10-28 13:58:44 +010043#include "intel_frontbuffer.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010044
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010045#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
46
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000047/**
48 * DOC: Global GTT views
49 *
50 * Background and previous state
51 *
52 * Historically objects could exists (be bound) in global GTT space only as
53 * singular instances with a view representing all of the object's backing pages
54 * in a linear fashion. This view will be called a normal view.
55 *
56 * To support multiple views of the same object, where the number of mapped
57 * pages is not equal to the backing store, or where the layout of the pages
58 * is not linear, concept of a GGTT view was added.
59 *
60 * One example of an alternative view is a stereo display driven by a single
61 * image. In this case we would have a framebuffer looking like this
62 * (2x2 pages):
63 *
64 * 12
65 * 34
66 *
67 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
68 * rendering. In contrast, fed to the display engine would be an alternative
69 * view which could look something like this:
70 *
71 * 1212
72 * 3434
73 *
74 * In this example both the size and layout of pages in the alternative view is
75 * different from the normal view.
76 *
77 * Implementation and usage
78 *
79 * GGTT views are implemented using VMAs and are distinguished via enum
80 * i915_ggtt_view_type and struct i915_ggtt_view.
81 *
82 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020083 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
84 * renaming in large amounts of code. They take the struct i915_ggtt_view
85 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000086 *
87 * As a helper for callers which are only interested in the normal view,
88 * globally const i915_ggtt_view_normal singleton instance exists. All old core
89 * GEM API functions, the ones not taking the view parameter, are operating on,
90 * or with the normal GGTT view.
91 *
92 * Code wanting to add or use a new GGTT view needs to:
93 *
94 * 1. Add a new enum with a suitable name.
95 * 2. Extend the metadata in the i915_ggtt_view structure if required.
96 * 3. Add support to i915_get_vma_pages().
97 *
98 * New views are required to build a scatter-gather table from within the
99 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
100 * exists for the lifetime of an VMA.
101 *
102 * Core API is designed to have copy semantics which means that passed in
103 * struct i915_ggtt_view does not need to be persistent (left around after
104 * calling the core API functions).
105 *
106 */
107
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200108static int
109i915_get_ggtt_vma_pages(struct i915_vma *vma);
110
Chris Wilson7c3f86b2017-01-12 11:00:49 +0000111static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
112{
113 /* Note that as an uncached mmio write, this should flush the
114 * WCB of the writes into the GGTT before it triggers the invalidate.
115 */
116 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
117}
118
119static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
120{
121 gen6_ggtt_invalidate(dev_priv);
122 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
123}
124
125static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
126{
127 intel_gtt_chipset_flush();
128}
129
130static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
131{
132 i915->ggtt.invalidate(i915);
133}
134
Chris Wilsonc0336662016-05-06 15:40:21 +0100135int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
136 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200137{
Chris Wilson1893a712014-09-19 11:56:27 +0100138 bool has_aliasing_ppgtt;
139 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100140 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100141
Michel Thierry9e1d0e62016-12-05 17:57:03 -0800142 has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
143 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
144 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100145
Zhi Wange320d402016-09-06 12:04:12 +0800146 if (intel_vgpu_active(dev_priv)) {
Tina Zhang8a4ab662017-08-14 15:20:46 +0800147 /* GVT-g has no support for 32bit ppgtt */
Zhi Wange320d402016-09-06 12:04:12 +0800148 has_full_ppgtt = false;
Tina Zhang8a4ab662017-08-14 15:20:46 +0800149 has_full_48bit_ppgtt = intel_vgpu_has_full_48bit_ppgtt(dev_priv);
Zhi Wange320d402016-09-06 12:04:12 +0800150 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800151
Chris Wilson0e4ca102016-04-29 13:18:22 +0100152 if (!has_aliasing_ppgtt)
153 return 0;
154
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000155 /*
156 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
157 * execlists, the sole mechanism available to submit work.
158 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100159 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200160 return 0;
161
162 if (enable_ppgtt == 1)
163 return 1;
164
Chris Wilson1893a712014-09-19 11:56:27 +0100165 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200166 return 2;
167
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100168 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
169 return 3;
170
Daniel Vetter93a25a92014-03-06 09:40:43 +0100171 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilson80debff2017-05-25 13:16:12 +0100172 if (IS_GEN6(dev_priv) && intel_vtd_active()) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100173 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200174 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100175 }
Daniel Vetter93a25a92014-03-06 09:40:43 +0100176
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
Joonas Lahtinen4fc05062017-08-11 12:51:26 +0300183 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists) {
184 if (has_full_48bit_ppgtt)
185 return 3;
186
187 if (has_full_ppgtt)
188 return 2;
189 }
190
191 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100192}
193
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200194static int ppgtt_bind_vma(struct i915_vma *vma,
195 enum i915_cache_level cache_level,
196 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200197{
Chris Wilsonff685972017-02-15 08:43:42 +0000198 u32 pte_flags;
199 int ret;
200
Matthew Auld1f234752017-05-12 10:14:23 +0100201 if (!(vma->flags & I915_VMA_LOCAL_BIND)) {
202 ret = vma->vm->allocate_va_range(vma->vm, vma->node.start,
203 vma->size);
204 if (ret)
205 return ret;
206 }
Daniel Vetter47552652015-04-14 17:35:24 +0200207
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100208 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100209
Daniel Vetter47552652015-04-14 17:35:24 +0200210 /* Currently applicable only to VLV */
Chris Wilsonff685972017-02-15 08:43:42 +0000211 pte_flags = 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200212 if (vma->obj->gt_ro)
213 pte_flags |= PTE_READ_ONLY;
214
Matthew Auld4a234c52017-06-22 10:58:36 +0100215 vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200216
217 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200218}
219
220static void ppgtt_unbind_vma(struct i915_vma *vma)
221{
Chris Wilsonff685972017-02-15 08:43:42 +0000222 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200223}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800224
Daniel Vetter2c642b02015-04-14 17:35:26 +0200225static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200226 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700227{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200228 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700229 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300230
231 switch (level) {
232 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800233 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300234 break;
235 case I915_CACHE_WT:
236 pte |= PPAT_DISPLAY_ELLC_INDEX;
237 break;
238 default:
239 pte |= PPAT_CACHED_INDEX;
240 break;
241 }
242
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700243 return pte;
244}
245
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300246static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
247 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800248{
Michel Thierry07749ef2015-03-16 16:00:54 +0000249 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800250 pde |= addr;
251 if (level != I915_CACHE_NONE)
252 pde |= PPAT_CACHED_PDE_INDEX;
253 else
254 pde |= PPAT_UNCACHED_INDEX;
255 return pde;
256}
257
Michel Thierry762d9932015-07-30 11:05:29 +0100258#define gen8_pdpe_encode gen8_pde_encode
259#define gen8_pml4e_encode gen8_pde_encode
260
Michel Thierry07749ef2015-03-16 16:00:54 +0000261static gen6_pte_t snb_pte_encode(dma_addr_t addr,
262 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200263 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700264{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200265 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700266 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700267
268 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100269 case I915_CACHE_L3_LLC:
270 case I915_CACHE_LLC:
271 pte |= GEN6_PTE_CACHE_LLC;
272 break;
273 case I915_CACHE_NONE:
274 pte |= GEN6_PTE_UNCACHED;
275 break;
276 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100277 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100278 }
279
280 return pte;
281}
282
Michel Thierry07749ef2015-03-16 16:00:54 +0000283static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
284 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200285 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100286{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200287 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100288 pte |= GEN6_PTE_ADDR_ENCODE(addr);
289
290 switch (level) {
291 case I915_CACHE_L3_LLC:
292 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700293 break;
294 case I915_CACHE_LLC:
295 pte |= GEN6_PTE_CACHE_LLC;
296 break;
297 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700298 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700299 break;
300 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100301 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700302 }
303
Ben Widawsky54d12522012-09-24 16:44:32 -0700304 return pte;
305}
306
Michel Thierry07749ef2015-03-16 16:00:54 +0000307static gen6_pte_t byt_pte_encode(dma_addr_t addr,
308 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200309 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700310{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200311 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700312 pte |= GEN6_PTE_ADDR_ENCODE(addr);
313
Akash Goel24f3a8c2014-06-17 10:59:42 +0530314 if (!(flags & PTE_READ_ONLY))
315 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700316
317 if (level != I915_CACHE_NONE)
318 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
319
320 return pte;
321}
322
Michel Thierry07749ef2015-03-16 16:00:54 +0000323static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
324 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200325 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700326{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200327 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700328 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700329
330 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700331 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700332
333 return pte;
334}
335
Michel Thierry07749ef2015-03-16 16:00:54 +0000336static gen6_pte_t iris_pte_encode(dma_addr_t addr,
337 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200338 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700339{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200340 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700341 pte |= HSW_PTE_ADDR_ENCODE(addr);
342
Chris Wilson651d7942013-08-08 14:41:10 +0100343 switch (level) {
344 case I915_CACHE_NONE:
345 break;
346 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000347 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100348 break;
349 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000350 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100351 break;
352 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700353
354 return pte;
355}
356
Chris Wilson84486612017-02-15 08:43:40 +0000357static struct page *vm_alloc_page(struct i915_address_space *vm, gfp_t gfp)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000358{
Chris Wilson84486612017-02-15 08:43:40 +0000359 struct page *page;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000360
Chris Wilson84486612017-02-15 08:43:40 +0000361 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
362 i915_gem_shrink_all(vm->i915);
Chris Wilsonaae4a3d2017-02-13 17:15:44 +0000363
Chris Wilson84486612017-02-15 08:43:40 +0000364 if (vm->free_pages.nr)
365 return vm->free_pages.pages[--vm->free_pages.nr];
366
367 page = alloc_page(gfp);
368 if (!page)
369 return NULL;
370
371 if (vm->pt_kmap_wc)
372 set_pages_array_wc(&page, 1);
373
374 return page;
375}
376
377static void vm_free_pages_release(struct i915_address_space *vm)
378{
379 GEM_BUG_ON(!pagevec_count(&vm->free_pages));
380
381 if (vm->pt_kmap_wc)
382 set_pages_array_wb(vm->free_pages.pages,
383 pagevec_count(&vm->free_pages));
384
385 __pagevec_release(&vm->free_pages);
386}
387
388static void vm_free_page(struct i915_address_space *vm, struct page *page)
389{
390 if (!pagevec_add(&vm->free_pages, page))
391 vm_free_pages_release(vm);
392}
393
394static int __setup_page_dma(struct i915_address_space *vm,
395 struct i915_page_dma *p,
396 gfp_t gfp)
397{
398 p->page = vm_alloc_page(vm, gfp | __GFP_NOWARN | __GFP_NORETRY);
399 if (unlikely(!p->page))
Michel Thierry1266cdb2015-03-24 17:06:33 +0000400 return -ENOMEM;
401
Chris Wilson84486612017-02-15 08:43:40 +0000402 p->daddr = dma_map_page(vm->dma, p->page, 0, PAGE_SIZE,
403 PCI_DMA_BIDIRECTIONAL);
404 if (unlikely(dma_mapping_error(vm->dma, p->daddr))) {
405 vm_free_page(vm, p->page);
406 return -ENOMEM;
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300407 }
408
Michel Thierry1266cdb2015-03-24 17:06:33 +0000409 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000410}
411
Chris Wilson84486612017-02-15 08:43:40 +0000412static int setup_page_dma(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000413 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300414{
Chris Wilson84486612017-02-15 08:43:40 +0000415 return __setup_page_dma(vm, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300416}
417
Chris Wilson84486612017-02-15 08:43:40 +0000418static void cleanup_page_dma(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000419 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300420{
Chris Wilson84486612017-02-15 08:43:40 +0000421 dma_unmap_page(vm->dma, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
422 vm_free_page(vm, p->page);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300423}
424
Chris Wilson9231da72017-02-15 08:43:41 +0000425#define kmap_atomic_px(px) kmap_atomic(px_base(px)->page)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300426
Chris Wilson84486612017-02-15 08:43:40 +0000427#define setup_px(vm, px) setup_page_dma((vm), px_base(px))
428#define cleanup_px(vm, px) cleanup_page_dma((vm), px_base(px))
429#define fill_px(ppgtt, px, v) fill_page_dma((vm), px_base(px), (v))
430#define fill32_px(ppgtt, px, v) fill_page_dma_32((vm), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300431
Chris Wilson84486612017-02-15 08:43:40 +0000432static void fill_page_dma(struct i915_address_space *vm,
433 struct i915_page_dma *p,
434 const u64 val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300435{
Chris Wilson9231da72017-02-15 08:43:41 +0000436 u64 * const vaddr = kmap_atomic(p->page);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300437 int i;
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300438
439 for (i = 0; i < 512; i++)
440 vaddr[i] = val;
441
Chris Wilson9231da72017-02-15 08:43:41 +0000442 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300443}
444
Chris Wilson84486612017-02-15 08:43:40 +0000445static void fill_page_dma_32(struct i915_address_space *vm,
446 struct i915_page_dma *p,
447 const u32 v)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300448{
Chris Wilson84486612017-02-15 08:43:40 +0000449 fill_page_dma(vm, p, (u64)v << 32 | v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300450}
451
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100452static int
Chris Wilson84486612017-02-15 08:43:40 +0000453setup_scratch_page(struct i915_address_space *vm, gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300454{
Chris Wilson84486612017-02-15 08:43:40 +0000455 return __setup_page_dma(vm, &vm->scratch_page, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300456}
457
Chris Wilson84486612017-02-15 08:43:40 +0000458static void cleanup_scratch_page(struct i915_address_space *vm)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300459{
Chris Wilson84486612017-02-15 08:43:40 +0000460 cleanup_page_dma(vm, &vm->scratch_page);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300461}
462
Chris Wilson84486612017-02-15 08:43:40 +0000463static struct i915_page_table *alloc_pt(struct i915_address_space *vm)
Ben Widawsky06fda602015-02-24 16:22:36 +0000464{
Michel Thierryec565b32015-04-08 12:13:23 +0100465 struct i915_page_table *pt;
Ben Widawsky06fda602015-02-24 16:22:36 +0000466
Chris Wilsondd196742017-02-15 08:43:46 +0000467 pt = kmalloc(sizeof(*pt), GFP_KERNEL | __GFP_NOWARN);
468 if (unlikely(!pt))
Ben Widawsky06fda602015-02-24 16:22:36 +0000469 return ERR_PTR(-ENOMEM);
470
Chris Wilsondd196742017-02-15 08:43:46 +0000471 if (unlikely(setup_px(vm, pt))) {
472 kfree(pt);
473 return ERR_PTR(-ENOMEM);
474 }
Ben Widawsky678d96f2015-03-16 16:00:56 +0000475
Chris Wilsondd196742017-02-15 08:43:46 +0000476 pt->used_ptes = 0;
Ben Widawsky06fda602015-02-24 16:22:36 +0000477 return pt;
478}
479
Chris Wilson84486612017-02-15 08:43:40 +0000480static void free_pt(struct i915_address_space *vm, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000481{
Chris Wilson84486612017-02-15 08:43:40 +0000482 cleanup_px(vm, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300483 kfree(pt);
484}
485
486static void gen8_initialize_pt(struct i915_address_space *vm,
487 struct i915_page_table *pt)
488{
Chris Wilsondd196742017-02-15 08:43:46 +0000489 fill_px(vm, pt,
490 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC));
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300491}
492
493static void gen6_initialize_pt(struct i915_address_space *vm,
494 struct i915_page_table *pt)
495{
Chris Wilsondd196742017-02-15 08:43:46 +0000496 fill32_px(vm, pt,
497 vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0));
Ben Widawsky06fda602015-02-24 16:22:36 +0000498}
499
Chris Wilson84486612017-02-15 08:43:40 +0000500static struct i915_page_directory *alloc_pd(struct i915_address_space *vm)
Ben Widawsky06fda602015-02-24 16:22:36 +0000501{
Michel Thierryec565b32015-04-08 12:13:23 +0100502 struct i915_page_directory *pd;
Ben Widawsky06fda602015-02-24 16:22:36 +0000503
Chris Wilsonfe52e372017-02-15 08:43:47 +0000504 pd = kzalloc(sizeof(*pd), GFP_KERNEL | __GFP_NOWARN);
505 if (unlikely(!pd))
Ben Widawsky06fda602015-02-24 16:22:36 +0000506 return ERR_PTR(-ENOMEM);
507
Chris Wilsonfe52e372017-02-15 08:43:47 +0000508 if (unlikely(setup_px(vm, pd))) {
509 kfree(pd);
510 return ERR_PTR(-ENOMEM);
511 }
Michel Thierry33c88192015-04-08 12:13:33 +0100512
Chris Wilsonfe52e372017-02-15 08:43:47 +0000513 pd->used_pdes = 0;
Ben Widawsky06fda602015-02-24 16:22:36 +0000514 return pd;
515}
516
Chris Wilson84486612017-02-15 08:43:40 +0000517static void free_pd(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000518 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300519{
Chris Wilsonfe52e372017-02-15 08:43:47 +0000520 cleanup_px(vm, pd);
521 kfree(pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300522}
523
524static void gen8_initialize_pd(struct i915_address_space *vm,
525 struct i915_page_directory *pd)
526{
Chris Wilsondd196742017-02-15 08:43:46 +0000527 unsigned int i;
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300528
Chris Wilsondd196742017-02-15 08:43:46 +0000529 fill_px(vm, pd,
530 gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC));
531 for (i = 0; i < I915_PDES; i++)
532 pd->page_table[i] = vm->scratch_pt;
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300533}
534
Chris Wilsonfe52e372017-02-15 08:43:47 +0000535static int __pdp_init(struct i915_address_space *vm,
Michel Thierry6ac18502015-07-29 17:23:46 +0100536 struct i915_page_directory_pointer *pdp)
537{
Mika Kuoppala3e490042017-02-28 17:28:07 +0200538 const unsigned int pdpes = i915_pdpes_per_pdp(vm);
Chris Wilsone2b763c2017-02-15 08:43:48 +0000539 unsigned int i;
Michel Thierry6ac18502015-07-29 17:23:46 +0100540
Chris Wilsonfe52e372017-02-15 08:43:47 +0000541 pdp->page_directory = kmalloc_array(pdpes, sizeof(*pdp->page_directory),
Chris Wilsone2b763c2017-02-15 08:43:48 +0000542 GFP_KERNEL | __GFP_NOWARN);
543 if (unlikely(!pdp->page_directory))
Michel Thierry6ac18502015-07-29 17:23:46 +0100544 return -ENOMEM;
Michel Thierry6ac18502015-07-29 17:23:46 +0100545
Chris Wilsonfe52e372017-02-15 08:43:47 +0000546 for (i = 0; i < pdpes; i++)
547 pdp->page_directory[i] = vm->scratch_pd;
548
Michel Thierry6ac18502015-07-29 17:23:46 +0100549 return 0;
550}
551
552static void __pdp_fini(struct i915_page_directory_pointer *pdp)
553{
Michel Thierry6ac18502015-07-29 17:23:46 +0100554 kfree(pdp->page_directory);
555 pdp->page_directory = NULL;
556}
557
Mika Kuoppala1e6437b2017-02-28 17:28:09 +0200558static inline bool use_4lvl(const struct i915_address_space *vm)
559{
560 return i915_vm_is_48bit(vm);
561}
562
Chris Wilson84486612017-02-15 08:43:40 +0000563static struct i915_page_directory_pointer *
564alloc_pdp(struct i915_address_space *vm)
Michel Thierry762d9932015-07-30 11:05:29 +0100565{
566 struct i915_page_directory_pointer *pdp;
567 int ret = -ENOMEM;
568
Mika Kuoppala1e6437b2017-02-28 17:28:09 +0200569 WARN_ON(!use_4lvl(vm));
Michel Thierry762d9932015-07-30 11:05:29 +0100570
571 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
572 if (!pdp)
573 return ERR_PTR(-ENOMEM);
574
Chris Wilsonfe52e372017-02-15 08:43:47 +0000575 ret = __pdp_init(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100576 if (ret)
577 goto fail_bitmap;
578
Chris Wilson84486612017-02-15 08:43:40 +0000579 ret = setup_px(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100580 if (ret)
581 goto fail_page_m;
582
583 return pdp;
584
585fail_page_m:
586 __pdp_fini(pdp);
587fail_bitmap:
588 kfree(pdp);
589
590 return ERR_PTR(ret);
591}
592
Chris Wilson84486612017-02-15 08:43:40 +0000593static void free_pdp(struct i915_address_space *vm,
Michel Thierry6ac18502015-07-29 17:23:46 +0100594 struct i915_page_directory_pointer *pdp)
595{
596 __pdp_fini(pdp);
Mika Kuoppala1e6437b2017-02-28 17:28:09 +0200597
598 if (!use_4lvl(vm))
599 return;
600
601 cleanup_px(vm, pdp);
602 kfree(pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100603}
604
Michel Thierry69ab76f2015-07-29 17:23:55 +0100605static void gen8_initialize_pdp(struct i915_address_space *vm,
606 struct i915_page_directory_pointer *pdp)
607{
608 gen8_ppgtt_pdpe_t scratch_pdpe;
609
610 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
611
Chris Wilson84486612017-02-15 08:43:40 +0000612 fill_px(vm, pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100613}
614
615static void gen8_initialize_pml4(struct i915_address_space *vm,
616 struct i915_pml4 *pml4)
617{
Chris Wilsone2b763c2017-02-15 08:43:48 +0000618 unsigned int i;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100619
Chris Wilsone2b763c2017-02-15 08:43:48 +0000620 fill_px(vm, pml4,
621 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC));
622 for (i = 0; i < GEN8_PML4ES_PER_PML4; i++)
623 pml4->pdps[i] = vm->scratch_pdp;
Michel Thierry6ac18502015-07-29 17:23:46 +0100624}
625
Ben Widawsky94e409c2013-11-04 22:29:36 -0800626/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100627static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100628 unsigned entry,
629 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800630{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000631 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000632 u32 *cs;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800633
634 BUG_ON(entry >= 4);
635
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000636 cs = intel_ring_begin(req, 6);
637 if (IS_ERR(cs))
638 return PTR_ERR(cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800639
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000640 *cs++ = MI_LOAD_REGISTER_IMM(1);
641 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, entry));
642 *cs++ = upper_32_bits(addr);
643 *cs++ = MI_LOAD_REGISTER_IMM(1);
644 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, entry));
645 *cs++ = lower_32_bits(addr);
646 intel_ring_advance(req, cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800647
648 return 0;
649}
650
Mika Kuoppalae7167762017-02-28 17:28:10 +0200651static int gen8_mm_switch_3lvl(struct i915_hw_ppgtt *ppgtt,
652 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800653{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800654 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800655
Mika Kuoppalae7167762017-02-28 17:28:10 +0200656 for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300657 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
658
John Harrisone85b26d2015-05-29 17:43:56 +0100659 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800660 if (ret)
661 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800662 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800663
Ben Widawskyeeb94882013-12-06 14:11:10 -0800664 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800665}
666
Mika Kuoppalae7167762017-02-28 17:28:10 +0200667static int gen8_mm_switch_4lvl(struct i915_hw_ppgtt *ppgtt,
668 struct drm_i915_gem_request *req)
Michel Thierry2dba3232015-07-30 11:06:23 +0100669{
670 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
671}
672
Mika Kuoppalafce93752016-10-31 17:24:46 +0200673/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
674 * the page table structures, we mark them dirty so that
675 * context switching/execlist queuing code takes extra steps
676 * to ensure that tlbs are flushed.
677 */
678static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
679{
Chris Wilson49d73912016-11-29 09:50:08 +0000680 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200681}
682
Michał Winiarski2ce51792016-10-13 14:02:42 +0200683/* Removes entries from a single page table, releasing it if it's empty.
684 * Caller can use the return value to update higher-level entries.
685 */
686static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200687 struct i915_page_table *pt,
Chris Wilsondd196742017-02-15 08:43:46 +0000688 u64 start, u64 length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700689{
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200690 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200691 unsigned int pte = gen8_pte_index(start);
692 unsigned int pte_end = pte + num_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +0000693 const gen8_pte_t scratch_pte =
694 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
695 gen8_pte_t *vaddr;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200696
Chris Wilsondd196742017-02-15 08:43:46 +0000697 GEM_BUG_ON(num_entries > pt->used_ptes);
Ben Widawsky459108b2013-11-02 21:07:23 -0700698
Chris Wilsondd196742017-02-15 08:43:46 +0000699 pt->used_ptes -= num_entries;
700 if (!pt->used_ptes)
701 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200702
Chris Wilson9231da72017-02-15 08:43:41 +0000703 vaddr = kmap_atomic_px(pt);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200704 while (pte < pte_end)
Chris Wilson894cceb2017-02-15 08:43:37 +0000705 vaddr[pte++] = scratch_pte;
Chris Wilson9231da72017-02-15 08:43:41 +0000706 kunmap_atomic(vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200707
708 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200709}
710
Chris Wilsondd196742017-02-15 08:43:46 +0000711static void gen8_ppgtt_set_pde(struct i915_address_space *vm,
712 struct i915_page_directory *pd,
713 struct i915_page_table *pt,
714 unsigned int pde)
715{
716 gen8_pde_t *vaddr;
717
718 pd->page_table[pde] = pt;
719
720 vaddr = kmap_atomic_px(pd);
721 vaddr[pde] = gen8_pde_encode(px_dma(pt), I915_CACHE_LLC);
722 kunmap_atomic(vaddr);
723}
724
Michał Winiarski2ce51792016-10-13 14:02:42 +0200725static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200726 struct i915_page_directory *pd,
Chris Wilsondd196742017-02-15 08:43:46 +0000727 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200728{
729 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +0000730 u32 pde;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200731
732 gen8_for_each_pde(pt, pd, start, length, pde) {
Chris Wilsonbf75d592017-02-27 12:26:52 +0000733 GEM_BUG_ON(pt == vm->scratch_pt);
734
Chris Wilsondd196742017-02-15 08:43:46 +0000735 if (!gen8_ppgtt_clear_pt(vm, pt, start, length))
736 continue;
Ben Widawsky06fda602015-02-24 16:22:36 +0000737
Chris Wilsondd196742017-02-15 08:43:46 +0000738 gen8_ppgtt_set_pde(vm, pd, vm->scratch_pt, pde);
Chris Wilsonbf75d592017-02-27 12:26:52 +0000739 GEM_BUG_ON(!pd->used_pdes);
Chris Wilsonfe52e372017-02-15 08:43:47 +0000740 pd->used_pdes--;
Chris Wilsondd196742017-02-15 08:43:46 +0000741
742 free_pt(vm, pt);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200743 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200744
Chris Wilsonfe52e372017-02-15 08:43:47 +0000745 return !pd->used_pdes;
746}
Michał Winiarski2ce51792016-10-13 14:02:42 +0200747
Chris Wilsonfe52e372017-02-15 08:43:47 +0000748static void gen8_ppgtt_set_pdpe(struct i915_address_space *vm,
749 struct i915_page_directory_pointer *pdp,
750 struct i915_page_directory *pd,
751 unsigned int pdpe)
752{
753 gen8_ppgtt_pdpe_t *vaddr;
754
755 pdp->page_directory[pdpe] = pd;
Mika Kuoppala1e6437b2017-02-28 17:28:09 +0200756 if (!use_4lvl(vm))
Chris Wilsonfe52e372017-02-15 08:43:47 +0000757 return;
758
759 vaddr = kmap_atomic_px(pdp);
760 vaddr[pdpe] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
761 kunmap_atomic(vaddr);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200762}
Ben Widawsky06fda602015-02-24 16:22:36 +0000763
Michał Winiarski2ce51792016-10-13 14:02:42 +0200764/* Removes entries from a single page dir pointer, releasing it if it's empty.
765 * Caller can use the return value to update higher-level entries
766 */
767static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200768 struct i915_page_directory_pointer *pdp,
Chris Wilsonfe52e372017-02-15 08:43:47 +0000769 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200770{
771 struct i915_page_directory *pd;
Chris Wilsonfe52e372017-02-15 08:43:47 +0000772 unsigned int pdpe;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200773
774 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilsonbf75d592017-02-27 12:26:52 +0000775 GEM_BUG_ON(pd == vm->scratch_pd);
776
Chris Wilsonfe52e372017-02-15 08:43:47 +0000777 if (!gen8_ppgtt_clear_pd(vm, pd, start, length))
778 continue;
Ben Widawsky06fda602015-02-24 16:22:36 +0000779
Chris Wilsonfe52e372017-02-15 08:43:47 +0000780 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
Chris Wilsonbf75d592017-02-27 12:26:52 +0000781 GEM_BUG_ON(!pdp->used_pdpes);
Chris Wilsone2b763c2017-02-15 08:43:48 +0000782 pdp->used_pdpes--;
Chris Wilsonfe52e372017-02-15 08:43:47 +0000783
784 free_pd(vm, pd);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200785 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200786
Chris Wilsone2b763c2017-02-15 08:43:48 +0000787 return !pdp->used_pdpes;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200788}
Ben Widawsky459108b2013-11-02 21:07:23 -0700789
Chris Wilsonfe52e372017-02-15 08:43:47 +0000790static void gen8_ppgtt_clear_3lvl(struct i915_address_space *vm,
791 u64 start, u64 length)
792{
793 gen8_ppgtt_clear_pdp(vm, &i915_vm_to_ppgtt(vm)->pdp, start, length);
794}
795
Chris Wilsone2b763c2017-02-15 08:43:48 +0000796static void gen8_ppgtt_set_pml4e(struct i915_pml4 *pml4,
797 struct i915_page_directory_pointer *pdp,
798 unsigned int pml4e)
799{
800 gen8_ppgtt_pml4e_t *vaddr;
801
802 pml4->pdps[pml4e] = pdp;
803
804 vaddr = kmap_atomic_px(pml4);
805 vaddr[pml4e] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
806 kunmap_atomic(vaddr);
807}
808
Michał Winiarski2ce51792016-10-13 14:02:42 +0200809/* Removes entries from a single pml4.
810 * This is the top-level structure in 4-level page tables used on gen8+.
811 * Empty entries are always scratch pml4e.
812 */
Chris Wilsonfe52e372017-02-15 08:43:47 +0000813static void gen8_ppgtt_clear_4lvl(struct i915_address_space *vm,
814 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200815{
Chris Wilsonfe52e372017-02-15 08:43:47 +0000816 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
817 struct i915_pml4 *pml4 = &ppgtt->pml4;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200818 struct i915_page_directory_pointer *pdp;
Chris Wilsone2b763c2017-02-15 08:43:48 +0000819 unsigned int pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200820
Mika Kuoppala1e6437b2017-02-28 17:28:09 +0200821 GEM_BUG_ON(!use_4lvl(vm));
Ben Widawsky459108b2013-11-02 21:07:23 -0700822
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200823 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonbf75d592017-02-27 12:26:52 +0000824 GEM_BUG_ON(pdp == vm->scratch_pdp);
825
Chris Wilsone2b763c2017-02-15 08:43:48 +0000826 if (!gen8_ppgtt_clear_pdp(vm, pdp, start, length))
827 continue;
Ben Widawsky459108b2013-11-02 21:07:23 -0700828
Chris Wilsone2b763c2017-02-15 08:43:48 +0000829 gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
Chris Wilsone2b763c2017-02-15 08:43:48 +0000830
831 free_pdp(vm, pdp);
Ben Widawsky459108b2013-11-02 21:07:23 -0700832 }
833}
834
Chris Wilson894cceb2017-02-15 08:43:37 +0000835struct sgt_dma {
836 struct scatterlist *sg;
837 dma_addr_t dma, max;
838};
839
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000840struct gen8_insert_pte {
841 u16 pml4e;
842 u16 pdpe;
843 u16 pde;
844 u16 pte;
845};
846
847static __always_inline struct gen8_insert_pte gen8_insert_pte(u64 start)
848{
849 return (struct gen8_insert_pte) {
850 gen8_pml4e_index(start),
851 gen8_pdpe_index(start),
852 gen8_pde_index(start),
853 gen8_pte_index(start),
854 };
855}
856
Chris Wilson894cceb2017-02-15 08:43:37 +0000857static __always_inline bool
858gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100859 struct i915_page_directory_pointer *pdp,
Chris Wilson894cceb2017-02-15 08:43:37 +0000860 struct sgt_dma *iter,
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000861 struct gen8_insert_pte *idx,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100862 enum i915_cache_level cache_level)
863{
Chris Wilson894cceb2017-02-15 08:43:37 +0000864 struct i915_page_directory *pd;
865 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
866 gen8_pte_t *vaddr;
867 bool ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700868
Mika Kuoppala3e490042017-02-28 17:28:07 +0200869 GEM_BUG_ON(idx->pdpe >= i915_pdpes_per_pdp(&ppgtt->base));
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000870 pd = pdp->page_directory[idx->pdpe];
871 vaddr = kmap_atomic_px(pd->page_table[idx->pde]);
Chris Wilson894cceb2017-02-15 08:43:37 +0000872 do {
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000873 vaddr[idx->pte] = pte_encode | iter->dma;
874
Chris Wilson894cceb2017-02-15 08:43:37 +0000875 iter->dma += PAGE_SIZE;
876 if (iter->dma >= iter->max) {
877 iter->sg = __sg_next(iter->sg);
878 if (!iter->sg) {
879 ret = false;
880 break;
881 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700882
Chris Wilson894cceb2017-02-15 08:43:37 +0000883 iter->dma = sg_dma_address(iter->sg);
884 iter->max = iter->dma + iter->sg->length;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000885 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800886
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000887 if (++idx->pte == GEN8_PTES) {
888 idx->pte = 0;
889
890 if (++idx->pde == I915_PDES) {
891 idx->pde = 0;
892
Chris Wilson894cceb2017-02-15 08:43:37 +0000893 /* Limited by sg length for 3lvl */
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000894 if (++idx->pdpe == GEN8_PML4ES_PER_PML4) {
895 idx->pdpe = 0;
Chris Wilson894cceb2017-02-15 08:43:37 +0000896 ret = true;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100897 break;
Chris Wilson894cceb2017-02-15 08:43:37 +0000898 }
899
Mika Kuoppala3e490042017-02-28 17:28:07 +0200900 GEM_BUG_ON(idx->pdpe >= i915_pdpes_per_pdp(&ppgtt->base));
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000901 pd = pdp->page_directory[idx->pdpe];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800902 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000903
Chris Wilson9231da72017-02-15 08:43:41 +0000904 kunmap_atomic(vaddr);
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000905 vaddr = kmap_atomic_px(pd->page_table[idx->pde]);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700906 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000907 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +0000908 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300909
Chris Wilson894cceb2017-02-15 08:43:37 +0000910 return ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700911}
912
Chris Wilson894cceb2017-02-15 08:43:37 +0000913static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
Matthew Auld4a234c52017-06-22 10:58:36 +0100914 struct i915_vma *vma,
Chris Wilson894cceb2017-02-15 08:43:37 +0000915 enum i915_cache_level cache_level,
916 u32 unused)
Michel Thierryf9b5b782015-07-30 11:02:49 +0100917{
Chuanxiao Dong17369ba2017-07-07 17:50:59 +0800918 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilson894cceb2017-02-15 08:43:37 +0000919 struct sgt_dma iter = {
Matthew Auld4a234c52017-06-22 10:58:36 +0100920 .sg = vma->pages->sgl,
Chris Wilson894cceb2017-02-15 08:43:37 +0000921 .dma = sg_dma_address(iter.sg),
922 .max = iter.dma + iter.sg->length,
923 };
Matthew Auld4a234c52017-06-22 10:58:36 +0100924 struct gen8_insert_pte idx = gen8_insert_pte(vma->node.start);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100925
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000926 gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter, &idx,
927 cache_level);
Chris Wilson894cceb2017-02-15 08:43:37 +0000928}
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100929
Chris Wilson894cceb2017-02-15 08:43:37 +0000930static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
Matthew Auld4a234c52017-06-22 10:58:36 +0100931 struct i915_vma *vma,
Chris Wilson894cceb2017-02-15 08:43:37 +0000932 enum i915_cache_level cache_level,
933 u32 unused)
934{
935 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
936 struct sgt_dma iter = {
Matthew Auld4a234c52017-06-22 10:58:36 +0100937 .sg = vma->pages->sgl,
Chris Wilson894cceb2017-02-15 08:43:37 +0000938 .dma = sg_dma_address(iter.sg),
939 .max = iter.dma + iter.sg->length,
940 };
941 struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
Matthew Auld4a234c52017-06-22 10:58:36 +0100942 struct gen8_insert_pte idx = gen8_insert_pte(vma->node.start);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100943
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000944 while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[idx.pml4e++], &iter,
945 &idx, cache_level))
946 GEM_BUG_ON(idx.pml4e >= GEN8_PML4ES_PER_PML4);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100947}
948
Chris Wilson84486612017-02-15 08:43:40 +0000949static void gen8_free_page_tables(struct i915_address_space *vm,
Michel Thierryf37c0502015-06-10 17:46:39 +0100950 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800951{
952 int i;
953
Mika Kuoppala567047b2015-06-25 18:35:12 +0300954 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800955 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800956
Chris Wilsonfe52e372017-02-15 08:43:47 +0000957 for (i = 0; i < I915_PDES; i++) {
958 if (pd->page_table[i] != vm->scratch_pt)
959 free_pt(vm, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000960 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000961}
962
Mika Kuoppala8776f022015-06-30 18:16:40 +0300963static int gen8_init_scratch(struct i915_address_space *vm)
964{
Matthew Auld64c050d2016-04-27 13:19:25 +0100965 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300966
Chris Wilson84486612017-02-15 08:43:40 +0000967 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100968 if (ret)
969 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300970
Chris Wilson84486612017-02-15 08:43:40 +0000971 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300972 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100973 ret = PTR_ERR(vm->scratch_pt);
974 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300975 }
976
Chris Wilson84486612017-02-15 08:43:40 +0000977 vm->scratch_pd = alloc_pd(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300978 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100979 ret = PTR_ERR(vm->scratch_pd);
980 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300981 }
982
Mika Kuoppala1e6437b2017-02-28 17:28:09 +0200983 if (use_4lvl(vm)) {
Chris Wilson84486612017-02-15 08:43:40 +0000984 vm->scratch_pdp = alloc_pdp(vm);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100985 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100986 ret = PTR_ERR(vm->scratch_pdp);
987 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100988 }
989 }
990
Mika Kuoppala8776f022015-06-30 18:16:40 +0300991 gen8_initialize_pt(vm, vm->scratch_pt);
992 gen8_initialize_pd(vm, vm->scratch_pd);
Mika Kuoppala1e6437b2017-02-28 17:28:09 +0200993 if (use_4lvl(vm))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100994 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300995
996 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100997
998free_pd:
Chris Wilson84486612017-02-15 08:43:40 +0000999 free_pd(vm, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +01001000free_pt:
Chris Wilson84486612017-02-15 08:43:40 +00001001 free_pt(vm, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +01001002free_scratch_page:
Chris Wilson84486612017-02-15 08:43:40 +00001003 cleanup_scratch_page(vm);
Matthew Auld64c050d2016-04-27 13:19:25 +01001004
1005 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001006}
1007
Zhiyuan Lv650da342015-08-28 15:41:18 +08001008static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1009{
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001010 struct i915_address_space *vm = &ppgtt->base;
1011 struct drm_i915_private *dev_priv = vm->i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001012 enum vgt_g2v_type msg;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001013 int i;
1014
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001015 if (use_4lvl(vm)) {
1016 const u64 daddr = px_dma(&ppgtt->pml4);
Zhiyuan Lv650da342015-08-28 15:41:18 +08001017
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001018 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1019 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001020
1021 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1022 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1023 } else {
Mika Kuoppalae7167762017-02-28 17:28:10 +02001024 for (i = 0; i < GEN8_3LVL_PDPES; i++) {
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001025 const u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
Zhiyuan Lv650da342015-08-28 15:41:18 +08001026
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001027 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1028 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001029 }
1030
1031 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1032 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1033 }
1034
1035 I915_WRITE(vgtif_reg(g2v_notify), msg);
1036
1037 return 0;
1038}
1039
Mika Kuoppala8776f022015-06-30 18:16:40 +03001040static void gen8_free_scratch(struct i915_address_space *vm)
1041{
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001042 if (use_4lvl(vm))
Chris Wilson84486612017-02-15 08:43:40 +00001043 free_pdp(vm, vm->scratch_pdp);
1044 free_pd(vm, vm->scratch_pd);
1045 free_pt(vm, vm->scratch_pt);
1046 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001047}
1048
Chris Wilson84486612017-02-15 08:43:40 +00001049static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
Michel Thierry762d9932015-07-30 11:05:29 +01001050 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001051{
Mika Kuoppala3e490042017-02-28 17:28:07 +02001052 const unsigned int pdpes = i915_pdpes_per_pdp(vm);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001053 int i;
1054
Mika Kuoppala3e490042017-02-28 17:28:07 +02001055 for (i = 0; i < pdpes; i++) {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001056 if (pdp->page_directory[i] == vm->scratch_pd)
Ben Widawsky06fda602015-02-24 16:22:36 +00001057 continue;
1058
Chris Wilson84486612017-02-15 08:43:40 +00001059 gen8_free_page_tables(vm, pdp->page_directory[i]);
1060 free_pd(vm, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001061 }
Michel Thierry69876be2015-04-08 12:13:27 +01001062
Chris Wilson84486612017-02-15 08:43:40 +00001063 free_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001064}
1065
1066static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1067{
1068 int i;
1069
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001070 for (i = 0; i < GEN8_PML4ES_PER_PML4; i++) {
1071 if (ppgtt->pml4.pdps[i] == ppgtt->base.scratch_pdp)
Michel Thierry762d9932015-07-30 11:05:29 +01001072 continue;
1073
Chris Wilson84486612017-02-15 08:43:40 +00001074 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001075 }
1076
Chris Wilson84486612017-02-15 08:43:40 +00001077 cleanup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001078}
1079
1080static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1081{
Chris Wilson49d73912016-11-29 09:50:08 +00001082 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001083 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001084
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001085 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001086 gen8_ppgtt_notify_vgt(ppgtt, false);
1087
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001088 if (use_4lvl(vm))
Michel Thierry762d9932015-07-30 11:05:29 +01001089 gen8_ppgtt_cleanup_4lvl(ppgtt);
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001090 else
1091 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, &ppgtt->pdp);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001092
Mika Kuoppala8776f022015-06-30 18:16:40 +03001093 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001094}
1095
Chris Wilsonfe52e372017-02-15 08:43:47 +00001096static int gen8_ppgtt_alloc_pd(struct i915_address_space *vm,
1097 struct i915_page_directory *pd,
1098 u64 start, u64 length)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001099{
Michel Thierryd7b26332015-04-08 12:13:34 +01001100 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +00001101 u64 from = start;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001102 unsigned int pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001103
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001104 gen8_for_each_pde(pt, pd, start, length, pde) {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001105 if (pt == vm->scratch_pt) {
Chris Wilsondd196742017-02-15 08:43:46 +00001106 pt = alloc_pt(vm);
1107 if (IS_ERR(pt))
1108 goto unwind;
1109
1110 gen8_initialize_pt(vm, pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001111
Chris Wilsonfe52e372017-02-15 08:43:47 +00001112 gen8_ppgtt_set_pde(vm, pd, pt, pde);
1113 pd->used_pdes++;
Chris Wilsonbf75d592017-02-27 12:26:52 +00001114 GEM_BUG_ON(pd->used_pdes > I915_PDES);
Chris Wilsonfe52e372017-02-15 08:43:47 +00001115 }
1116
1117 pt->used_ptes += gen8_pte_count(start, length);
1118 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001119 return 0;
1120
Chris Wilsondd196742017-02-15 08:43:46 +00001121unwind:
1122 gen8_ppgtt_clear_pd(vm, pd, from, start - from);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001123 return -ENOMEM;
1124}
1125
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001126static int gen8_ppgtt_alloc_pdp(struct i915_address_space *vm,
1127 struct i915_page_directory_pointer *pdp,
1128 u64 start, u64 length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001129{
Michel Thierry5441f0c2015-04-08 12:13:28 +01001130 struct i915_page_directory *pd;
Chris Wilsone2b763c2017-02-15 08:43:48 +00001131 u64 from = start;
1132 unsigned int pdpe;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001133 int ret;
1134
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001135 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilsone2b763c2017-02-15 08:43:48 +00001136 if (pd == vm->scratch_pd) {
1137 pd = alloc_pd(vm);
1138 if (IS_ERR(pd))
1139 goto unwind;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001140
Chris Wilsone2b763c2017-02-15 08:43:48 +00001141 gen8_initialize_pd(vm, pd);
Chris Wilsonfe52e372017-02-15 08:43:47 +00001142 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
Chris Wilsone2b763c2017-02-15 08:43:48 +00001143 pdp->used_pdpes++;
Mika Kuoppala3e490042017-02-28 17:28:07 +02001144 GEM_BUG_ON(pdp->used_pdpes > i915_pdpes_per_pdp(vm));
Chris Wilson75afcf72017-02-15 08:43:51 +00001145
1146 mark_tlbs_dirty(i915_vm_to_ppgtt(vm));
Chris Wilsone2b763c2017-02-15 08:43:48 +00001147 }
1148
1149 ret = gen8_ppgtt_alloc_pd(vm, pd, start, length);
Chris Wilsonbf75d592017-02-27 12:26:52 +00001150 if (unlikely(ret))
1151 goto unwind_pd;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001152 }
Michel Thierry33c88192015-04-08 12:13:33 +01001153
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001154 return 0;
1155
Chris Wilsonbf75d592017-02-27 12:26:52 +00001156unwind_pd:
1157 if (!pd->used_pdes) {
1158 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1159 GEM_BUG_ON(!pdp->used_pdpes);
1160 pdp->used_pdpes--;
1161 free_pd(vm, pd);
1162 }
Chris Wilsone2b763c2017-02-15 08:43:48 +00001163unwind:
1164 gen8_ppgtt_clear_pdp(vm, pdp, from, start - from);
1165 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001166}
1167
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001168static int gen8_ppgtt_alloc_3lvl(struct i915_address_space *vm,
1169 u64 start, u64 length)
Michel Thierry762d9932015-07-30 11:05:29 +01001170{
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001171 return gen8_ppgtt_alloc_pdp(vm,
1172 &i915_vm_to_ppgtt(vm)->pdp, start, length);
1173}
1174
1175static int gen8_ppgtt_alloc_4lvl(struct i915_address_space *vm,
1176 u64 start, u64 length)
1177{
1178 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1179 struct i915_pml4 *pml4 = &ppgtt->pml4;
Michel Thierry762d9932015-07-30 11:05:29 +01001180 struct i915_page_directory_pointer *pdp;
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001181 u64 from = start;
1182 u32 pml4e;
1183 int ret;
Michel Thierry762d9932015-07-30 11:05:29 +01001184
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001185 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001186 if (pml4->pdps[pml4e] == vm->scratch_pdp) {
1187 pdp = alloc_pdp(vm);
1188 if (IS_ERR(pdp))
1189 goto unwind;
Michel Thierry762d9932015-07-30 11:05:29 +01001190
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001191 gen8_initialize_pdp(vm, pdp);
1192 gen8_ppgtt_set_pml4e(pml4, pdp, pml4e);
1193 }
Michel Thierry762d9932015-07-30 11:05:29 +01001194
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001195 ret = gen8_ppgtt_alloc_pdp(vm, pdp, start, length);
Chris Wilsonbf75d592017-02-27 12:26:52 +00001196 if (unlikely(ret))
1197 goto unwind_pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001198 }
1199
Michel Thierry762d9932015-07-30 11:05:29 +01001200 return 0;
1201
Chris Wilsonbf75d592017-02-27 12:26:52 +00001202unwind_pdp:
1203 if (!pdp->used_pdpes) {
1204 gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
1205 free_pdp(vm, pdp);
1206 }
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001207unwind:
1208 gen8_ppgtt_clear_4lvl(vm, from, start - from);
1209 return -ENOMEM;
Michel Thierry762d9932015-07-30 11:05:29 +01001210}
1211
Chris Wilson84486612017-02-15 08:43:40 +00001212static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
1213 struct i915_page_directory_pointer *pdp,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001214 u64 start, u64 length,
Michel Thierryea91e402015-07-29 17:23:57 +01001215 gen8_pte_t scratch_pte,
1216 struct seq_file *m)
1217{
Mika Kuoppala3e490042017-02-28 17:28:07 +02001218 struct i915_address_space *vm = &ppgtt->base;
Michel Thierryea91e402015-07-29 17:23:57 +01001219 struct i915_page_directory *pd;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001220 u32 pdpe;
Michel Thierryea91e402015-07-29 17:23:57 +01001221
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001222 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001223 struct i915_page_table *pt;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001224 u64 pd_len = length;
1225 u64 pd_start = start;
1226 u32 pde;
Michel Thierryea91e402015-07-29 17:23:57 +01001227
Chris Wilsone2b763c2017-02-15 08:43:48 +00001228 if (pdp->page_directory[pdpe] == ppgtt->base.scratch_pd)
Michel Thierryea91e402015-07-29 17:23:57 +01001229 continue;
1230
1231 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001232 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001233 u32 pte;
Michel Thierryea91e402015-07-29 17:23:57 +01001234 gen8_pte_t *pt_vaddr;
1235
Chris Wilsonfe52e372017-02-15 08:43:47 +00001236 if (pd->page_table[pde] == ppgtt->base.scratch_pt)
Michel Thierryea91e402015-07-29 17:23:57 +01001237 continue;
1238
Chris Wilson9231da72017-02-15 08:43:41 +00001239 pt_vaddr = kmap_atomic_px(pt);
Michel Thierryea91e402015-07-29 17:23:57 +01001240 for (pte = 0; pte < GEN8_PTES; pte += 4) {
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001241 u64 va = (pdpe << GEN8_PDPE_SHIFT |
1242 pde << GEN8_PDE_SHIFT |
1243 pte << GEN8_PTE_SHIFT);
Michel Thierryea91e402015-07-29 17:23:57 +01001244 int i;
1245 bool found = false;
1246
1247 for (i = 0; i < 4; i++)
1248 if (pt_vaddr[pte + i] != scratch_pte)
1249 found = true;
1250 if (!found)
1251 continue;
1252
1253 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1254 for (i = 0; i < 4; i++) {
1255 if (pt_vaddr[pte + i] != scratch_pte)
1256 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1257 else
1258 seq_puts(m, " SCRATCH ");
1259 }
1260 seq_puts(m, "\n");
1261 }
Michel Thierryea91e402015-07-29 17:23:57 +01001262 kunmap_atomic(pt_vaddr);
1263 }
1264 }
1265}
1266
1267static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1268{
1269 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson894cceb2017-02-15 08:43:37 +00001270 const gen8_pte_t scratch_pte =
1271 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
Chris Wilson381b9432017-02-15 08:43:54 +00001272 u64 start = 0, length = ppgtt->base.total;
Michel Thierryea91e402015-07-29 17:23:57 +01001273
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001274 if (use_4lvl(vm)) {
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001275 u64 pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001276 struct i915_pml4 *pml4 = &ppgtt->pml4;
1277 struct i915_page_directory_pointer *pdp;
1278
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001279 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001280 if (pml4->pdps[pml4e] == ppgtt->base.scratch_pdp)
Michel Thierryea91e402015-07-29 17:23:57 +01001281 continue;
1282
1283 seq_printf(m, " PML4E #%llu\n", pml4e);
Chris Wilson84486612017-02-15 08:43:40 +00001284 gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001285 }
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001286 } else {
1287 gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001288 }
1289}
1290
Chris Wilsone2b763c2017-02-15 08:43:48 +00001291static int gen8_preallocate_top_level_pdp(struct i915_hw_ppgtt *ppgtt)
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001292{
Chris Wilsone2b763c2017-02-15 08:43:48 +00001293 struct i915_address_space *vm = &ppgtt->base;
1294 struct i915_page_directory_pointer *pdp = &ppgtt->pdp;
1295 struct i915_page_directory *pd;
1296 u64 start = 0, length = ppgtt->base.total;
1297 u64 from = start;
1298 unsigned int pdpe;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001299
Chris Wilsone2b763c2017-02-15 08:43:48 +00001300 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1301 pd = alloc_pd(vm);
1302 if (IS_ERR(pd))
1303 goto unwind;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001304
Chris Wilsone2b763c2017-02-15 08:43:48 +00001305 gen8_initialize_pd(vm, pd);
1306 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
1307 pdp->used_pdpes++;
1308 }
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001309
Chris Wilsone2b763c2017-02-15 08:43:48 +00001310 pdp->used_pdpes++; /* never remove */
1311 return 0;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001312
Chris Wilsone2b763c2017-02-15 08:43:48 +00001313unwind:
1314 start -= from;
1315 gen8_for_each_pdpe(pd, pdp, from, start, pdpe) {
1316 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1317 free_pd(vm, pd);
1318 }
1319 pdp->used_pdpes = 0;
1320 return -ENOMEM;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001321}
1322
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001323/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001324 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1325 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1326 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1327 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001328 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001329 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001330static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001331{
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001332 struct i915_address_space *vm = &ppgtt->base;
1333 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001334 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001335
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001336 ppgtt->base.total = USES_FULL_48BIT_PPGTT(dev_priv) ?
1337 1ULL << 48 :
1338 1ULL << 32;
1339
Mika Kuoppala8776f022015-06-30 18:16:40 +03001340 ret = gen8_init_scratch(&ppgtt->base);
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001341 if (ret) {
1342 ppgtt->base.total = 0;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001343 return ret;
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001344 }
Michel Thierry69876be2015-04-08 12:13:27 +01001345
Chris Wilson84486612017-02-15 08:43:40 +00001346 /* There are only few exceptions for gen >=6. chv and bxt.
1347 * And we are not sure about the latter so play safe for now.
1348 */
1349 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
1350 ppgtt->base.pt_kmap_wc = true;
1351
Mika Kuoppala1e6437b2017-02-28 17:28:09 +02001352 if (use_4lvl(vm)) {
Chris Wilson84486612017-02-15 08:43:40 +00001353 ret = setup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001354 if (ret)
1355 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001356
Michel Thierry69ab76f2015-07-29 17:23:55 +01001357 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1358
Mika Kuoppalae7167762017-02-28 17:28:10 +02001359 ppgtt->switch_mm = gen8_mm_switch_4lvl;
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001360 ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_4lvl;
Chris Wilson894cceb2017-02-15 08:43:37 +00001361 ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001362 ppgtt->base.clear_range = gen8_ppgtt_clear_4lvl;
Michel Thierry762d9932015-07-30 11:05:29 +01001363 } else {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001364 ret = __pdp_init(&ppgtt->base, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001365 if (ret)
1366 goto free_scratch;
1367
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001368 if (intel_vgpu_active(dev_priv)) {
Chris Wilsone2b763c2017-02-15 08:43:48 +00001369 ret = gen8_preallocate_top_level_pdp(ppgtt);
1370 if (ret) {
1371 __pdp_fini(&ppgtt->pdp);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001372 goto free_scratch;
Chris Wilsone2b763c2017-02-15 08:43:48 +00001373 }
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001374 }
Chris Wilson894cceb2017-02-15 08:43:37 +00001375
Mika Kuoppalae7167762017-02-28 17:28:10 +02001376 ppgtt->switch_mm = gen8_mm_switch_3lvl;
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001377 ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_3lvl;
Chris Wilson894cceb2017-02-15 08:43:37 +00001378 ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001379 ppgtt->base.clear_range = gen8_ppgtt_clear_3lvl;
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001380 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001381
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001382 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001383 gen8_ppgtt_notify_vgt(ppgtt, true);
1384
Mika Kuoppala054b9ac2017-02-28 17:28:11 +02001385 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
1386 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1387 ppgtt->base.bind_vma = ppgtt_bind_vma;
1388 ppgtt->debug_dump = gen8_dump_ppgtt;
1389
Michel Thierryd7b26332015-04-08 12:13:34 +01001390 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001391
1392free_scratch:
1393 gen8_free_scratch(&ppgtt->base);
1394 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001395}
1396
Ben Widawsky87d60b62013-12-06 14:11:29 -08001397static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1398{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001399 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001400 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001401 gen6_pte_t scratch_pte;
Chris Wilson381b9432017-02-15 08:43:54 +00001402 u32 pd_entry, pte, pde;
1403 u32 start = 0, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001404
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001405 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001406 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001407
Dave Gordon731f74c2016-06-24 19:37:46 +01001408 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001409 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001410 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001411 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001412 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001413 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1414
1415 if (pd_entry != expected)
1416 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1417 pde,
1418 pd_entry,
1419 expected);
1420 seq_printf(m, "\tPDE: %x\n", pd_entry);
1421
Chris Wilson9231da72017-02-15 08:43:41 +00001422 pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[pde]);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001423
Michel Thierry07749ef2015-03-16 16:00:54 +00001424 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001425 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001426 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001427 (pte * PAGE_SIZE);
1428 int i;
1429 bool found = false;
1430 for (i = 0; i < 4; i++)
1431 if (pt_vaddr[pte + i] != scratch_pte)
1432 found = true;
1433 if (!found)
1434 continue;
1435
1436 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1437 for (i = 0; i < 4; i++) {
1438 if (pt_vaddr[pte + i] != scratch_pte)
1439 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1440 else
1441 seq_puts(m, " SCRATCH ");
1442 }
1443 seq_puts(m, "\n");
1444 }
Chris Wilson9231da72017-02-15 08:43:41 +00001445 kunmap_atomic(pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001446 }
1447}
1448
Ben Widawsky678d96f2015-03-16 16:00:56 +00001449/* Write pde (index) from the page directory @pd to the page table @pt */
Chris Wilson16a011c2017-02-15 08:43:45 +00001450static inline void gen6_write_pde(const struct i915_hw_ppgtt *ppgtt,
1451 const unsigned int pde,
1452 const struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001453{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001454 /* Caller needs to make sure the write completes if necessary */
Chris Wilson16a011c2017-02-15 08:43:45 +00001455 writel_relaxed(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
1456 ppgtt->pd_addr + pde);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001457}
Ben Widawsky61973492013-04-08 18:43:54 -07001458
Ben Widawsky678d96f2015-03-16 16:00:56 +00001459/* Write all the page tables found in the ppgtt structure to incrementing page
1460 * directories. */
Chris Wilson16a011c2017-02-15 08:43:45 +00001461static void gen6_write_page_range(struct i915_hw_ppgtt *ppgtt,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001462 u32 start, u32 length)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001463{
Michel Thierryec565b32015-04-08 12:13:23 +01001464 struct i915_page_table *pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001465 unsigned int pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001466
Chris Wilson16a011c2017-02-15 08:43:45 +00001467 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde)
1468 gen6_write_pde(ppgtt, pde, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001469
Chris Wilson16a011c2017-02-15 08:43:45 +00001470 mark_tlbs_dirty(ppgtt);
Chris Wilsondd196742017-02-15 08:43:46 +00001471 wmb();
Ben Widawsky3e302542013-04-23 23:15:32 -07001472}
1473
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001474static inline u32 get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001475{
Chris Wilsondd196742017-02-15 08:43:46 +00001476 GEM_BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1477 return ppgtt->pd.base.ggtt_offset << 10;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001478}
Ben Widawsky61973492013-04-08 18:43:54 -07001479
Ben Widawsky90252e52013-12-06 14:11:12 -08001480static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001481 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001482{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001483 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001484 u32 *cs;
Ben Widawsky61973492013-04-08 18:43:54 -07001485
Ben Widawsky90252e52013-12-06 14:11:12 -08001486 /* NB: TLBs must be flushed and invalidated before a switch */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001487 cs = intel_ring_begin(req, 6);
1488 if (IS_ERR(cs))
1489 return PTR_ERR(cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001490
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001491 *cs++ = MI_LOAD_REGISTER_IMM(2);
1492 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1493 *cs++ = PP_DIR_DCLV_2G;
1494 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1495 *cs++ = get_pd_offset(ppgtt);
1496 *cs++ = MI_NOOP;
1497 intel_ring_advance(req, cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001498
1499 return 0;
1500}
1501
Ben Widawsky48a10382013-12-06 14:11:11 -08001502static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001503 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001504{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001505 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001506 u32 *cs;
Ben Widawsky48a10382013-12-06 14:11:11 -08001507
Ben Widawsky48a10382013-12-06 14:11:11 -08001508 /* NB: TLBs must be flushed and invalidated before a switch */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001509 cs = intel_ring_begin(req, 6);
1510 if (IS_ERR(cs))
1511 return PTR_ERR(cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001512
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001513 *cs++ = MI_LOAD_REGISTER_IMM(2);
1514 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1515 *cs++ = PP_DIR_DCLV_2G;
1516 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1517 *cs++ = get_pd_offset(ppgtt);
1518 *cs++ = MI_NOOP;
1519 intel_ring_advance(req, cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001520
1521 return 0;
1522}
1523
Ben Widawskyeeb94882013-12-06 14:11:10 -08001524static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001525 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001526{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001527 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001528 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001529
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001530 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1531 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001532 return 0;
1533}
1534
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001535static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001536{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001537 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301538 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001539
Akash Goel3b3f1652016-10-13 22:44:48 +05301540 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001541 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1542 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001543 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001544 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001545 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001546}
1547
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001548static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001549{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001550 struct intel_engine_cs *engine;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001551 u32 ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301552 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001553
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001554 ecobits = I915_READ(GAC_ECO_BITS);
1555 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1556
1557 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001558 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001559 ecochk |= ECOCHK_PPGTT_WB_HSW;
1560 } else {
1561 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1562 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1563 }
1564 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001565
Akash Goel3b3f1652016-10-13 22:44:48 +05301566 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001567 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001568 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001569 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001570 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001571}
1572
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001573static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001574{
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001575 u32 ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001576
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001577 ecobits = I915_READ(GAC_ECO_BITS);
1578 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1579 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001580
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001581 gab_ctl = I915_READ(GAB_CTL);
1582 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001583
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001584 ecochk = I915_READ(GAM_ECOCHK);
1585 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001586
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001587 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001588}
1589
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001590/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001591static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Chris Wilsondd196742017-02-15 08:43:46 +00001592 u64 start, u64 length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001593{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001594 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilsondd196742017-02-15 08:43:46 +00001595 unsigned int first_entry = start >> PAGE_SHIFT;
1596 unsigned int pde = first_entry / GEN6_PTES;
1597 unsigned int pte = first_entry % GEN6_PTES;
1598 unsigned int num_entries = length >> PAGE_SHIFT;
1599 gen6_pte_t scratch_pte =
1600 vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001601
Daniel Vetter7bddb012012-02-09 17:15:47 +01001602 while (num_entries) {
Chris Wilsondd196742017-02-15 08:43:46 +00001603 struct i915_page_table *pt = ppgtt->pd.page_table[pde++];
1604 unsigned int end = min(pte + num_entries, GEN6_PTES);
1605 gen6_pte_t *vaddr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001606
Chris Wilsondd196742017-02-15 08:43:46 +00001607 num_entries -= end - pte;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001608
Chris Wilsondd196742017-02-15 08:43:46 +00001609 /* Note that the hw doesn't support removing PDE on the fly
1610 * (they are cached inside the context with no means to
1611 * invalidate the cache), so we can only reset the PTE
1612 * entries back to scratch.
1613 */
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001614
Chris Wilsondd196742017-02-15 08:43:46 +00001615 vaddr = kmap_atomic_px(pt);
1616 do {
1617 vaddr[pte++] = scratch_pte;
1618 } while (pte < end);
1619 kunmap_atomic(vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001620
Chris Wilsondd196742017-02-15 08:43:46 +00001621 pte = 0;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001622 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001623}
1624
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001625static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Matthew Auld4a234c52017-06-22 10:58:36 +01001626 struct i915_vma *vma,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001627 enum i915_cache_level cache_level,
1628 u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001629{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001630 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Matthew Auld4a234c52017-06-22 10:58:36 +01001631 unsigned first_entry = vma->node.start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001632 unsigned act_pt = first_entry / GEN6_PTES;
1633 unsigned act_pte = first_entry % GEN6_PTES;
Chris Wilsonb31144c2017-02-15 08:43:36 +00001634 const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1635 struct sgt_dma iter;
1636 gen6_pte_t *vaddr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001637
Chris Wilson9231da72017-02-15 08:43:41 +00001638 vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
Matthew Auld4a234c52017-06-22 10:58:36 +01001639 iter.sg = vma->pages->sgl;
Chris Wilsonb31144c2017-02-15 08:43:36 +00001640 iter.dma = sg_dma_address(iter.sg);
1641 iter.max = iter.dma + iter.sg->length;
1642 do {
1643 vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001644
Chris Wilsonb31144c2017-02-15 08:43:36 +00001645 iter.dma += PAGE_SIZE;
1646 if (iter.dma == iter.max) {
1647 iter.sg = __sg_next(iter.sg);
1648 if (!iter.sg)
1649 break;
1650
1651 iter.dma = sg_dma_address(iter.sg);
1652 iter.max = iter.dma + iter.sg->length;
1653 }
Akash Goel24f3a8c2014-06-17 10:59:42 +05301654
Michel Thierry07749ef2015-03-16 16:00:54 +00001655 if (++act_pte == GEN6_PTES) {
Chris Wilson9231da72017-02-15 08:43:41 +00001656 kunmap_atomic(vaddr);
1657 vaddr = kmap_atomic_px(ppgtt->pd.page_table[++act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +02001658 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001659 }
Chris Wilsonb31144c2017-02-15 08:43:36 +00001660 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +00001661 kunmap_atomic(vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001662}
1663
Ben Widawsky678d96f2015-03-16 16:00:56 +00001664static int gen6_alloc_va_range(struct i915_address_space *vm,
Chris Wilsondd196742017-02-15 08:43:46 +00001665 u64 start, u64 length)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001666{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001667 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001668 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +00001669 u64 from = start;
1670 unsigned int pde;
1671 bool flush = false;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001672
Dave Gordon731f74c2016-06-24 19:37:46 +01001673 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Chris Wilsondd196742017-02-15 08:43:46 +00001674 if (pt == vm->scratch_pt) {
1675 pt = alloc_pt(vm);
1676 if (IS_ERR(pt))
1677 goto unwind_out;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001678
Chris Wilsondd196742017-02-15 08:43:46 +00001679 gen6_initialize_pt(vm, pt);
1680 ppgtt->pd.page_table[pde] = pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001681 gen6_write_pde(ppgtt, pde, pt);
Chris Wilsondd196742017-02-15 08:43:46 +00001682 flush = true;
1683 }
Ben Widawsky678d96f2015-03-16 16:00:56 +00001684 }
1685
Chris Wilsondd196742017-02-15 08:43:46 +00001686 if (flush) {
1687 mark_tlbs_dirty(ppgtt);
1688 wmb();
1689 }
Michel Thierry4933d512015-03-24 15:46:22 +00001690
Ben Widawsky678d96f2015-03-16 16:00:56 +00001691 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001692
1693unwind_out:
Chris Wilsondd196742017-02-15 08:43:46 +00001694 gen6_ppgtt_clear_range(vm, from, start);
1695 return -ENOMEM;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001696}
1697
Mika Kuoppala8776f022015-06-30 18:16:40 +03001698static int gen6_init_scratch(struct i915_address_space *vm)
1699{
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001700 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001701
Chris Wilson84486612017-02-15 08:43:40 +00001702 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001703 if (ret)
1704 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001705
Chris Wilson84486612017-02-15 08:43:40 +00001706 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001707 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson84486612017-02-15 08:43:40 +00001708 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001709 return PTR_ERR(vm->scratch_pt);
1710 }
1711
1712 gen6_initialize_pt(vm, vm->scratch_pt);
1713
1714 return 0;
1715}
1716
1717static void gen6_free_scratch(struct i915_address_space *vm)
1718{
Chris Wilson84486612017-02-15 08:43:40 +00001719 free_pt(vm, vm->scratch_pt);
1720 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001721}
1722
Daniel Vetter061dd492015-04-14 17:35:13 +02001723static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08001724{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001725 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01001726 struct i915_page_directory *pd = &ppgtt->pd;
Michel Thierry09942c62015-04-08 12:13:30 +01001727 struct i915_page_table *pt;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001728 u32 pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08001729
Daniel Vetter061dd492015-04-14 17:35:13 +02001730 drm_mm_remove_node(&ppgtt->node);
1731
Dave Gordon731f74c2016-06-24 19:37:46 +01001732 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001733 if (pt != vm->scratch_pt)
Chris Wilson84486612017-02-15 08:43:40 +00001734 free_pt(vm, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001735
Mika Kuoppala8776f022015-06-30 18:16:40 +03001736 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08001737}
1738
Ben Widawskyb1465202014-02-19 22:05:49 -08001739static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001740{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001741 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00001742 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001743 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08001744 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001745
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001746 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1747 * allocator works in address space sizes, so it's multiplied by page
1748 * size. We allocate at the top of the GTT to avoid fragmentation.
1749 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001750 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00001751
Mika Kuoppala8776f022015-06-30 18:16:40 +03001752 ret = gen6_init_scratch(vm);
1753 if (ret)
1754 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00001755
Chris Wilsone007b192017-01-11 11:23:10 +00001756 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
1757 GEN6_PD_SIZE, GEN6_PD_ALIGN,
1758 I915_COLOR_UNEVICTABLE,
1759 0, ggtt->base.total,
1760 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00001761 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001762 goto err_out;
1763
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001764 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001765 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001766
Chris Wilson52c126e2017-02-15 08:43:43 +00001767 ppgtt->pd.base.ggtt_offset =
1768 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
1769
1770 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
1771 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
1772
Ben Widawskyc8c26622015-01-22 17:01:25 +00001773 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001774
1775err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03001776 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001777 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08001778}
1779
Ben Widawskyb1465202014-02-19 22:05:49 -08001780static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1781{
kbuild test robot2f2cf682015-03-27 19:26:35 +08001782 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08001783}
1784
Michel Thierry4933d512015-03-24 15:46:22 +00001785static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001786 u64 start, u64 length)
Michel Thierry4933d512015-03-24 15:46:22 +00001787{
Michel Thierryec565b32015-04-08 12:13:23 +01001788 struct i915_page_table *unused;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001789 u32 pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001790
Dave Gordon731f74c2016-06-24 19:37:46 +01001791 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001792 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00001793}
1794
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001795static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08001796{
Chris Wilson49d73912016-11-29 09:50:08 +00001797 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001798 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08001799 int ret;
1800
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001801 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001802 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08001803 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001804 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08001805 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001806 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08001807 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01001808 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001809 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001810
1811 ret = gen6_ppgtt_alloc(ppgtt);
1812 if (ret)
1813 return ret;
1814
Michel Thierry09942c62015-04-08 12:13:30 +01001815 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001816
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001817 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Chris Wilson16a011c2017-02-15 08:43:45 +00001818 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001819
Chris Wilson52c126e2017-02-15 08:43:43 +00001820 ret = gen6_alloc_va_range(&ppgtt->base, 0, ppgtt->base.total);
1821 if (ret) {
1822 gen6_ppgtt_cleanup(&ppgtt->base);
1823 return ret;
1824 }
1825
Mika Kuoppala054b9ac2017-02-28 17:28:11 +02001826 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1827 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1828 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1829 ppgtt->base.bind_vma = ppgtt_bind_vma;
1830 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
1831 ppgtt->debug_dump = gen6_dump_ppgtt;
1832
Thierry Reding440fd522015-01-23 09:05:06 +01001833 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001834 ppgtt->node.size >> 20,
1835 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001836
Chris Wilson52c126e2017-02-15 08:43:43 +00001837 DRM_DEBUG_DRIVER("Adding PPGTT at offset %x\n",
1838 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001839
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001840 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001841}
1842
Chris Wilson2bfa9962016-08-04 07:52:25 +01001843static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
1844 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08001845{
Chris Wilson49d73912016-11-29 09:50:08 +00001846 ppgtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00001847 ppgtt->base.dma = &dev_priv->drm.pdev->dev;
Daniel Vetter3440d262013-01-24 13:49:56 -08001848
Chris Wilson2bfa9962016-08-04 07:52:25 +01001849 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001850 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001851 else
Michel Thierryd7b26332015-04-08 12:13:34 +01001852 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001853}
Mika Kuoppalac114f762015-06-25 18:35:13 +03001854
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001855static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01001856 struct drm_i915_private *dev_priv,
1857 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001858{
Chris Wilson80b204b2016-10-28 13:58:58 +01001859 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00001860
Chris Wilson381b9432017-02-15 08:43:54 +00001861 drm_mm_init(&vm->mm, 0, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00001862 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
1863
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001864 INIT_LIST_HEAD(&vm->active_list);
1865 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01001866 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00001867
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001868 list_add_tail(&vm->global_link, &dev_priv->vm_list);
Chris Wilson84486612017-02-15 08:43:40 +00001869 pagevec_init(&vm->free_pages, false);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001870}
1871
Matthew Aulded9724d2016-11-17 21:04:10 +00001872static void i915_address_space_fini(struct i915_address_space *vm)
1873{
Chris Wilson84486612017-02-15 08:43:40 +00001874 if (pagevec_count(&vm->free_pages))
1875 vm_free_pages_release(vm);
1876
Matthew Aulded9724d2016-11-17 21:04:10 +00001877 i915_gem_timeline_fini(&vm->timeline);
1878 drm_mm_takedown(&vm->mm);
1879 list_del(&vm->global_link);
1880}
1881
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001882static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00001883{
Tim Gored5165eb2016-02-04 11:49:34 +00001884 /* This function is for gtt related workarounds. This function is
1885 * called on driver load and after a GPU reset, so you can place
1886 * workarounds here even if they get overwritten by GPU reset.
1887 */
Rodrigo Vivi46c26662017-06-16 15:49:58 -07001888 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk,cfl */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01001889 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001890 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01001891 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001892 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08001893 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001894 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001895 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001896 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
1897}
1898
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001899int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02001900{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001901 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00001902
Thomas Daniel671b50132014-08-20 16:24:50 +01001903 /* In the case of execlists, PPGTT is enabled by the context descriptor
1904 * and the PDPs are contained within the context itself. We don't
1905 * need to do anything here. */
1906 if (i915.enable_execlists)
1907 return 0;
1908
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001909 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02001910 return 0;
1911
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001912 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001913 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001914 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001915 gen7_ppgtt_enable(dev_priv);
1916 else if (INTEL_GEN(dev_priv) >= 8)
1917 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02001918 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001919 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02001920
John Harrison4ad2fd82015-06-18 13:11:20 +01001921 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001922}
John Harrison4ad2fd82015-06-18 13:11:20 +01001923
Daniel Vetter4d884702014-08-06 15:04:47 +02001924struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01001925i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01001926 struct drm_i915_file_private *fpriv,
1927 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02001928{
1929 struct i915_hw_ppgtt *ppgtt;
1930 int ret;
1931
1932 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1933 if (!ppgtt)
1934 return ERR_PTR(-ENOMEM);
1935
Chris Wilson1188bc62017-02-15 08:43:38 +00001936 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetter4d884702014-08-06 15:04:47 +02001937 if (ret) {
1938 kfree(ppgtt);
1939 return ERR_PTR(ret);
1940 }
1941
Chris Wilson1188bc62017-02-15 08:43:38 +00001942 kref_init(&ppgtt->ref);
1943 i915_address_space_init(&ppgtt->base, dev_priv, name);
1944 ppgtt->base.file = fpriv;
1945
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001946 trace_i915_ppgtt_create(&ppgtt->base);
1947
Daniel Vetter4d884702014-08-06 15:04:47 +02001948 return ppgtt;
1949}
1950
Chris Wilson0c7eeda2017-01-11 21:09:25 +00001951void i915_ppgtt_close(struct i915_address_space *vm)
1952{
1953 struct list_head *phases[] = {
1954 &vm->active_list,
1955 &vm->inactive_list,
1956 &vm->unbound_list,
1957 NULL,
1958 }, **phase;
1959
1960 GEM_BUG_ON(vm->closed);
1961 vm->closed = true;
1962
1963 for (phase = phases; *phase; phase++) {
1964 struct i915_vma *vma, *vn;
1965
1966 list_for_each_entry_safe(vma, vn, *phase, vm_link)
1967 if (!i915_vma_is_closed(vma))
1968 i915_vma_close(vma);
1969 }
1970}
1971
Matthew Aulded9724d2016-11-17 21:04:10 +00001972void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02001973{
1974 struct i915_hw_ppgtt *ppgtt =
1975 container_of(kref, struct i915_hw_ppgtt, ref);
1976
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001977 trace_i915_ppgtt_release(&ppgtt->base);
1978
Chris Wilson50e046b2016-08-04 07:52:46 +01001979 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02001980 WARN_ON(!list_empty(&ppgtt->base.active_list));
1981 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01001982 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02001983
1984 ppgtt->base.cleanup(&ppgtt->base);
Chris Wilson84486612017-02-15 08:43:40 +00001985 i915_address_space_fini(&ppgtt->base);
Daniel Vetteree960be2014-08-06 15:04:45 +02001986 kfree(ppgtt);
1987}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001988
Ben Widawskya81cc002013-01-18 12:30:31 -08001989/* Certain Gen5 chipsets require require idling the GPU before
1990 * unmapping anything from the GTT when VT-d is enabled.
1991 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001992static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08001993{
Ben Widawskya81cc002013-01-18 12:30:31 -08001994 /* Query intel_iommu to see if we need the workaround. Presumably that
1995 * was loaded first.
1996 */
Chris Wilson80debff2017-05-25 13:16:12 +01001997 return IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_vtd_active();
Ben Widawskya81cc002013-01-18 12:30:31 -08001998}
1999
Chris Wilsondc979972016-05-10 14:10:04 +01002000void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002001{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002002 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302003 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002004
Chris Wilsondc979972016-05-10 14:10:04 +01002005 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002006 return;
2007
Akash Goel3b3f1652016-10-13 22:44:48 +05302008 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002009 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002010 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002011 if (fault_reg & RING_FAULT_VALID) {
2012 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002013 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002014 "\tAddress space: %s\n"
2015 "\tSource ID: %d\n"
2016 "\tType: %d\n",
2017 fault_reg & PAGE_MASK,
2018 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2019 RING_FAULT_SRCID(fault_reg),
2020 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002021 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002022 fault_reg & ~RING_FAULT_VALID);
2023 }
2024 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302025
2026 /* Engine specific init may not have been done till this point. */
2027 if (dev_priv->engine[RCS])
2028 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002029}
2030
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002031void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002032{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002033 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002034
2035 /* Don't bother messing with faults pre GEN6 as we have little
2036 * documentation supporting that it's a good idea.
2037 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002038 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002039 return;
2040
Chris Wilsondc979972016-05-10 14:10:04 +01002041 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002042
Chris Wilson381b9432017-02-15 08:43:54 +00002043 ggtt->base.clear_range(&ggtt->base, 0, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002044
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002045 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002046}
2047
Chris Wilson03ac84f2016-10-28 13:58:36 +01002048int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2049 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002050{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002051 do {
2052 if (dma_map_sg(&obj->base.dev->pdev->dev,
2053 pages->sgl, pages->nents,
2054 PCI_DMA_BIDIRECTIONAL))
2055 return 0;
2056
2057 /* If the DMA remap fails, one cause can be that we have
2058 * too many objects pinned in a small remapping table,
2059 * such as swiotlb. Incrementally purge all other objects and
2060 * try again - if there are no more pages to remove from
2061 * the DMA remapper, i915_gem_shrink will return 0.
2062 */
2063 GEM_BUG_ON(obj->mm.pages == pages);
2064 } while (i915_gem_shrink(to_i915(obj->base.dev),
2065 obj->base.size >> PAGE_SHIFT,
2066 I915_SHRINK_BOUND |
2067 I915_SHRINK_UNBOUND |
2068 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002069
Chris Wilson03ac84f2016-10-28 13:58:36 +01002070 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002071}
2072
Daniel Vetter2c642b02015-04-14 17:35:26 +02002073static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002074{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002075 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002076}
2077
Chris Wilsond6473f52016-06-10 14:22:59 +05302078static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2079 dma_addr_t addr,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002080 u64 offset,
Chris Wilsond6473f52016-06-10 14:22:59 +05302081 enum i915_cache_level level,
2082 u32 unused)
2083{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002084 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302085 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002086 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302087
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002088 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302089
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002090 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302091}
2092
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002093static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
Matthew Auld4a234c52017-06-22 10:58:36 +01002094 struct i915_vma *vma,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002095 enum i915_cache_level level,
2096 u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002097{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002098 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002099 struct sgt_iter sgt_iter;
2100 gen8_pte_t __iomem *gtt_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +00002101 const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002102 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002103
Chris Wilson894cceb2017-02-15 08:43:37 +00002104 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
Matthew Auld4a234c52017-06-22 10:58:36 +01002105 gtt_entries += vma->node.start >> PAGE_SHIFT;
2106 for_each_sgt_dma(addr, sgt_iter, vma->pages)
Chris Wilson894cceb2017-02-15 08:43:37 +00002107 gen8_set_pte(gtt_entries++, pte_encode | addr);
Dave Gordon85d12252016-05-20 11:54:06 +01002108
Chris Wilson894cceb2017-02-15 08:43:37 +00002109 wmb();
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002110
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002111 /* This next bit makes the above posting read even more important. We
2112 * want to flush the TLBs only after we're certain all the PTE updates
2113 * have finished.
2114 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002115 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002116}
2117
Chris Wilsond6473f52016-06-10 14:22:59 +05302118static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2119 dma_addr_t addr,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002120 u64 offset,
Chris Wilsond6473f52016-06-10 14:22:59 +05302121 enum i915_cache_level level,
2122 u32 flags)
2123{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002124 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302125 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002126 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302127
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002128 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302129
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002130 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302131}
2132
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002133/*
2134 * Binds an object into the global gtt with the specified cache level. The object
2135 * will be accessible to the GPU via commands whose operands reference offsets
2136 * within the global GTT as well as accessible by the GPU through the GMADR
2137 * mapped BAR (dev_priv->mm.gtt->gtt).
2138 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002139static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Matthew Auld4a234c52017-06-22 10:58:36 +01002140 struct i915_vma *vma,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002141 enum i915_cache_level level,
2142 u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002143{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002144 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsonb31144c2017-02-15 08:43:36 +00002145 gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
Matthew Auld4a234c52017-06-22 10:58:36 +01002146 unsigned int i = vma->node.start >> PAGE_SHIFT;
Chris Wilsonb31144c2017-02-15 08:43:36 +00002147 struct sgt_iter iter;
Dave Gordon85d12252016-05-20 11:54:06 +01002148 dma_addr_t addr;
Matthew Auld4a234c52017-06-22 10:58:36 +01002149 for_each_sgt_dma(addr, iter, vma->pages)
Chris Wilsonb31144c2017-02-15 08:43:36 +00002150 iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2151 wmb();
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002152
2153 /* This next bit makes the above posting read even more important. We
2154 * want to flush the TLBs only after we're certain all the PTE updates
2155 * have finished.
2156 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002157 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002158}
2159
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002160static void nop_clear_range(struct i915_address_space *vm,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002161 u64 start, u64 length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002162{
2163}
2164
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002165static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002166 u64 start, u64 length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002167{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002168 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002169 unsigned first_entry = start >> PAGE_SHIFT;
2170 unsigned num_entries = length >> PAGE_SHIFT;
Chris Wilson894cceb2017-02-15 08:43:37 +00002171 const gen8_pte_t scratch_pte =
2172 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
2173 gen8_pte_t __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002174 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2175 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002176 int i;
2177
2178 if (WARN(num_entries > max_entries,
2179 "First entry = %d; Num entries = %d (max=%d)\n",
2180 first_entry, num_entries, max_entries))
2181 num_entries = max_entries;
2182
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002183 for (i = 0; i < num_entries; i++)
2184 gen8_set_pte(&gtt_base[i], scratch_pte);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002185}
2186
Jon Bloomfield0ef34ad2017-05-24 08:54:11 -07002187static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
2188{
2189 struct drm_i915_private *dev_priv = vm->i915;
2190
2191 /*
2192 * Make sure the internal GAM fifo has been cleared of all GTT
2193 * writes before exiting stop_machine(). This guarantees that
2194 * any aperture accesses waiting to start in another process
2195 * cannot back up behind the GTT writes causing a hang.
2196 * The register can be any arbitrary GAM register.
2197 */
2198 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2199}
2200
2201struct insert_page {
2202 struct i915_address_space *vm;
2203 dma_addr_t addr;
2204 u64 offset;
2205 enum i915_cache_level level;
2206};
2207
2208static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
2209{
2210 struct insert_page *arg = _arg;
2211
2212 gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset, arg->level, 0);
2213 bxt_vtd_ggtt_wa(arg->vm);
2214
2215 return 0;
2216}
2217
2218static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
2219 dma_addr_t addr,
2220 u64 offset,
2221 enum i915_cache_level level,
2222 u32 unused)
2223{
2224 struct insert_page arg = { vm, addr, offset, level };
2225
2226 stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
2227}
2228
2229struct insert_entries {
2230 struct i915_address_space *vm;
Matthew Auld4a234c52017-06-22 10:58:36 +01002231 struct i915_vma *vma;
Jon Bloomfield0ef34ad2017-05-24 08:54:11 -07002232 enum i915_cache_level level;
2233};
2234
2235static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
2236{
2237 struct insert_entries *arg = _arg;
2238
Matthew Auld4a234c52017-06-22 10:58:36 +01002239 gen8_ggtt_insert_entries(arg->vm, arg->vma, arg->level, 0);
Jon Bloomfield0ef34ad2017-05-24 08:54:11 -07002240 bxt_vtd_ggtt_wa(arg->vm);
2241
2242 return 0;
2243}
2244
2245static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
Matthew Auld4a234c52017-06-22 10:58:36 +01002246 struct i915_vma *vma,
Jon Bloomfield0ef34ad2017-05-24 08:54:11 -07002247 enum i915_cache_level level,
2248 u32 unused)
2249{
Chuanxiao Dong17369ba2017-07-07 17:50:59 +08002250 struct insert_entries arg = { vm, vma, level };
Jon Bloomfield0ef34ad2017-05-24 08:54:11 -07002251
2252 stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
2253}
2254
2255struct clear_range {
2256 struct i915_address_space *vm;
2257 u64 start;
2258 u64 length;
2259};
2260
2261static int bxt_vtd_ggtt_clear_range__cb(void *_arg)
2262{
2263 struct clear_range *arg = _arg;
2264
2265 gen8_ggtt_clear_range(arg->vm, arg->start, arg->length);
2266 bxt_vtd_ggtt_wa(arg->vm);
2267
2268 return 0;
2269}
2270
2271static void bxt_vtd_ggtt_clear_range__BKL(struct i915_address_space *vm,
2272 u64 start,
2273 u64 length)
2274{
2275 struct clear_range arg = { vm, start, length };
2276
2277 stop_machine(bxt_vtd_ggtt_clear_range__cb, &arg, NULL);
2278}
2279
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002280static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002281 u64 start, u64 length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002282{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002283 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002284 unsigned first_entry = start >> PAGE_SHIFT;
2285 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002286 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002287 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2288 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002289 int i;
2290
2291 if (WARN(num_entries > max_entries,
2292 "First entry = %d; Num entries = %d (max=%d)\n",
2293 first_entry, num_entries, max_entries))
2294 num_entries = max_entries;
2295
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002296 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002297 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002298
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002299 for (i = 0; i < num_entries; i++)
2300 iowrite32(scratch_pte, &gtt_base[i]);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002301}
2302
Chris Wilsond6473f52016-06-10 14:22:59 +05302303static void i915_ggtt_insert_page(struct i915_address_space *vm,
2304 dma_addr_t addr,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002305 u64 offset,
Chris Wilsond6473f52016-06-10 14:22:59 +05302306 enum i915_cache_level cache_level,
2307 u32 unused)
2308{
Chris Wilsond6473f52016-06-10 14:22:59 +05302309 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2310 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302311
2312 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302313}
2314
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002315static void i915_ggtt_insert_entries(struct i915_address_space *vm,
Matthew Auld4a234c52017-06-22 10:58:36 +01002316 struct i915_vma *vma,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002317 enum i915_cache_level cache_level,
2318 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002319{
2320 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2321 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2322
Matthew Auld4a234c52017-06-22 10:58:36 +01002323 intel_gtt_insert_sg_entries(vma->pages, vma->node.start >> PAGE_SHIFT,
2324 flags);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002325}
2326
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002327static void i915_ggtt_clear_range(struct i915_address_space *vm,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002328 u64 start, u64 length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002329{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002330 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002331}
2332
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002333static int ggtt_bind_vma(struct i915_vma *vma,
2334 enum i915_cache_level cache_level,
2335 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002336{
Chris Wilson49d73912016-11-29 09:50:08 +00002337 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002338 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonba7a5742017-02-15 08:43:35 +00002339 u32 pte_flags;
Daniel Vetter0a878712015-10-15 14:23:01 +02002340
Chris Wilsonba7a5742017-02-15 08:43:35 +00002341 if (unlikely(!vma->pages)) {
2342 int ret = i915_get_ggtt_vma_pages(vma);
2343 if (ret)
2344 return ret;
2345 }
Daniel Vetter0a878712015-10-15 14:23:01 +02002346
2347 /* Currently applicable only to VLV */
Chris Wilsonba7a5742017-02-15 08:43:35 +00002348 pte_flags = 0;
Daniel Vetter0a878712015-10-15 14:23:01 +02002349 if (obj->gt_ro)
2350 pte_flags |= PTE_READ_ONLY;
2351
Chris Wilson9c870d02016-10-24 13:42:15 +01002352 intel_runtime_pm_get(i915);
Matthew Auld4a234c52017-06-22 10:58:36 +01002353 vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002354 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002355
2356 /*
2357 * Without aliasing PPGTT there's no difference between
2358 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2359 * upgrade to both bound if we bind either to avoid double-binding.
2360 */
Chris Wilson3272db52016-08-04 16:32:32 +01002361 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002362
2363 return 0;
2364}
2365
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002366static void ggtt_unbind_vma(struct i915_vma *vma)
2367{
2368 struct drm_i915_private *i915 = vma->vm->i915;
2369
2370 intel_runtime_pm_get(i915);
2371 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2372 intel_runtime_pm_put(i915);
2373}
2374
Daniel Vetter0a878712015-10-15 14:23:01 +02002375static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2376 enum i915_cache_level cache_level,
2377 u32 flags)
2378{
Chris Wilson49d73912016-11-29 09:50:08 +00002379 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002380 u32 pte_flags;
Chris Wilsonff685972017-02-15 08:43:42 +00002381 int ret;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002382
Chris Wilsonba7a5742017-02-15 08:43:35 +00002383 if (unlikely(!vma->pages)) {
Chris Wilsonff685972017-02-15 08:43:42 +00002384 ret = i915_get_ggtt_vma_pages(vma);
Chris Wilsonba7a5742017-02-15 08:43:35 +00002385 if (ret)
2386 return ret;
2387 }
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002388
Akash Goel24f3a8c2014-06-17 10:59:42 +05302389 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002390 pte_flags = 0;
2391 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002392 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302393
Chris Wilsonff685972017-02-15 08:43:42 +00002394 if (flags & I915_VMA_LOCAL_BIND) {
2395 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
2396
Matthew Auld1f234752017-05-12 10:14:23 +01002397 if (!(vma->flags & I915_VMA_LOCAL_BIND) &&
2398 appgtt->base.allocate_va_range) {
Chris Wilsonff685972017-02-15 08:43:42 +00002399 ret = appgtt->base.allocate_va_range(&appgtt->base,
2400 vma->node.start,
Matthew Auldd5672322017-05-16 09:55:14 +01002401 vma->size);
Chris Wilsonff685972017-02-15 08:43:42 +00002402 if (ret)
Chris Wilson2f7399a2017-02-27 12:26:53 +00002403 goto err_pages;
Chris Wilsonff685972017-02-15 08:43:42 +00002404 }
2405
Matthew Auld4a234c52017-06-22 10:58:36 +01002406 appgtt->base.insert_entries(&appgtt->base, vma, cache_level,
2407 pte_flags);
Chris Wilsonff685972017-02-15 08:43:42 +00002408 }
2409
Chris Wilson3272db52016-08-04 16:32:32 +01002410 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002411 intel_runtime_pm_get(i915);
Matthew Auld4a234c52017-06-22 10:58:36 +01002412 vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002413 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002414 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002415
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002416 return 0;
Chris Wilson2f7399a2017-02-27 12:26:53 +00002417
2418err_pages:
2419 if (!(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND))) {
2420 if (vma->pages != vma->obj->mm.pages) {
2421 GEM_BUG_ON(!vma->pages);
2422 sg_free_table(vma->pages);
2423 kfree(vma->pages);
2424 }
2425 vma->pages = NULL;
2426 }
2427 return ret;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002428}
2429
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002430static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002431{
Chris Wilson49d73912016-11-29 09:50:08 +00002432 struct drm_i915_private *i915 = vma->vm->i915;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002433
Chris Wilson9c870d02016-10-24 13:42:15 +01002434 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2435 intel_runtime_pm_get(i915);
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002436 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002437 intel_runtime_pm_put(i915);
2438 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002439
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002440 if (vma->flags & I915_VMA_LOCAL_BIND) {
2441 struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
2442
2443 vm->clear_range(vm, vma->node.start, vma->size);
2444 }
Daniel Vetter74163902012-02-15 23:50:21 +01002445}
2446
Chris Wilson03ac84f2016-10-28 13:58:36 +01002447void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2448 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002449{
David Weinehall52a05c32016-08-22 13:32:44 +03002450 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2451 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002452 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002453
Chris Wilson307dc252016-08-05 10:14:12 +01002454 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson228ec872017-03-30 09:53:41 +01002455 if (i915_gem_wait_for_idle(dev_priv, 0)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002456 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2457 /* Wait a bit, in hopes it avoids the hang */
2458 udelay(10);
2459 }
2460 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002461
Chris Wilson03ac84f2016-10-28 13:58:36 +01002462 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002463}
Daniel Vetter644ec022012-03-26 09:45:40 +02002464
Chris Wilson45b186f2016-12-16 07:46:42 +00002465static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002466 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002467 u64 *start,
2468 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002469{
Chris Wilsona6508de2017-02-06 08:45:47 +00002470 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002471 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002472
Chris Wilsona6508de2017-02-06 08:45:47 +00002473 /* Also leave a space between the unallocated reserved node after the
2474 * GTT and any objects within the GTT, i.e. we use the color adjustment
2475 * to insert a guard page to prevent prefetches crossing over the
2476 * GTT boundary.
2477 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002478 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002479 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002480 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002481}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002482
Chris Wilson6cde9a02017-02-13 17:15:50 +00002483int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2484{
2485 struct i915_ggtt *ggtt = &i915->ggtt;
2486 struct i915_hw_ppgtt *ppgtt;
2487 int err;
2488
Chris Wilson57202f42017-02-15 08:43:56 +00002489 ppgtt = i915_ppgtt_create(i915, ERR_PTR(-EPERM), "[alias]");
Chris Wilson1188bc62017-02-15 08:43:38 +00002490 if (IS_ERR(ppgtt))
2491 return PTR_ERR(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002492
Chris Wilsone565ceb2017-02-15 08:43:55 +00002493 if (WARN_ON(ppgtt->base.total < ggtt->base.total)) {
2494 err = -ENODEV;
2495 goto err_ppgtt;
2496 }
2497
Chris Wilson6cde9a02017-02-13 17:15:50 +00002498 if (ppgtt->base.allocate_va_range) {
Chris Wilsone565ceb2017-02-15 08:43:55 +00002499 /* Note we only pre-allocate as far as the end of the global
2500 * GTT. On 48b / 4-level page-tables, the difference is very,
2501 * very significant! We have to preallocate as GVT/vgpu does
2502 * not like the page directory disappearing.
2503 */
Chris Wilson6cde9a02017-02-13 17:15:50 +00002504 err = ppgtt->base.allocate_va_range(&ppgtt->base,
Chris Wilsone565ceb2017-02-15 08:43:55 +00002505 0, ggtt->base.total);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002506 if (err)
Chris Wilson1188bc62017-02-15 08:43:38 +00002507 goto err_ppgtt;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002508 }
2509
Chris Wilson6cde9a02017-02-13 17:15:50 +00002510 i915->mm.aliasing_ppgtt = ppgtt;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002511
Chris Wilson6cde9a02017-02-13 17:15:50 +00002512 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2513 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2514
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002515 WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
2516 ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
2517
Chris Wilson6cde9a02017-02-13 17:15:50 +00002518 return 0;
2519
Chris Wilson6cde9a02017-02-13 17:15:50 +00002520err_ppgtt:
Chris Wilson1188bc62017-02-15 08:43:38 +00002521 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002522 return err;
2523}
2524
2525void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2526{
2527 struct i915_ggtt *ggtt = &i915->ggtt;
2528 struct i915_hw_ppgtt *ppgtt;
2529
2530 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2531 if (!ppgtt)
2532 return;
2533
Chris Wilson1188bc62017-02-15 08:43:38 +00002534 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002535
2536 ggtt->base.bind_vma = ggtt_bind_vma;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002537 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002538}
2539
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002540int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002541{
Ben Widawskye78891c2013-01-25 16:41:04 -08002542 /* Let GEM Manage all of the aperture.
2543 *
2544 * However, leave one page at the end still bound to the scratch page.
2545 * There are a number of places where the hardware apparently prefetches
2546 * past the end of the object, and we've seen multiple hangs with the
2547 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2548 * aperture. One page should be enough to keep any prefetching inside
2549 * of the aperture.
2550 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002551 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002552 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002553 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002554 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002555
Zhi Wangb02d22a2016-06-16 08:06:59 -04002556 ret = intel_vgt_balloon(dev_priv);
2557 if (ret)
2558 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002559
Chris Wilson95374d72016-10-12 10:05:20 +01002560 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002561 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2562 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2563 0, ggtt->mappable_end,
2564 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002565 if (ret)
2566 return ret;
2567
Chris Wilsoned2f3452012-11-15 11:32:19 +00002568 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002569 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002570 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2571 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002572 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002573 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002574 }
2575
2576 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002577 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002578 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002579
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002580 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Chris Wilson6cde9a02017-02-13 17:15:50 +00002581 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
Chris Wilson95374d72016-10-12 10:05:20 +01002582 if (ret)
Chris Wilson6cde9a02017-02-13 17:15:50 +00002583 goto err;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002584 }
2585
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002586 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002587
Chris Wilson95374d72016-10-12 10:05:20 +01002588err:
2589 drm_mm_remove_node(&ggtt->error_capture);
2590 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002591}
2592
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002593/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002594 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002595 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002596 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002597void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002598{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002599 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002600 struct i915_vma *vma, *vn;
2601
2602 ggtt->base.closed = true;
2603
2604 mutex_lock(&dev_priv->drm.struct_mutex);
2605 WARN_ON(!list_empty(&ggtt->base.active_list));
2606 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2607 WARN_ON(i915_vma_unbind(vma));
2608 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002609
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002610 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002611
Chris Wilson1188bc62017-02-15 08:43:38 +00002612 mutex_lock(&dev_priv->drm.struct_mutex);
2613 i915_gem_fini_aliasing_ppgtt(dev_priv);
2614
Chris Wilson95374d72016-10-12 10:05:20 +01002615 if (drm_mm_node_allocated(&ggtt->error_capture))
2616 drm_mm_remove_node(&ggtt->error_capture);
2617
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002618 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002619 intel_vgt_deballoon(dev_priv);
Matthew Aulded9724d2016-11-17 21:04:10 +00002620 i915_address_space_fini(&ggtt->base);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002621 }
2622
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002623 ggtt->base.cleanup(&ggtt->base);
Chris Wilson1188bc62017-02-15 08:43:38 +00002624 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002625
2626 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002627 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002628}
Daniel Vetter70e32542014-08-06 15:04:57 +02002629
Daniel Vetter2c642b02015-04-14 17:35:26 +02002630static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002631{
2632 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2633 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2634 return snb_gmch_ctl << 20;
2635}
2636
Daniel Vetter2c642b02015-04-14 17:35:26 +02002637static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002638{
2639 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2640 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2641 if (bdw_gmch_ctl)
2642 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002643
2644#ifdef CONFIG_X86_32
2645 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2646 if (bdw_gmch_ctl > 4)
2647 bdw_gmch_ctl = 4;
2648#endif
2649
Ben Widawsky9459d252013-11-03 16:53:55 -08002650 return bdw_gmch_ctl << 20;
2651}
2652
Daniel Vetter2c642b02015-04-14 17:35:26 +02002653static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002654{
2655 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2656 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2657
2658 if (gmch_ctrl)
2659 return 1 << (20 + gmch_ctrl);
2660
2661 return 0;
2662}
2663
Daniel Vetter2c642b02015-04-14 17:35:26 +02002664static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002665{
2666 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2667 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
Imre Deaka92d1a92017-05-10 12:21:52 +03002668 return (size_t)snb_gmch_ctl << 25; /* 32 MB units */
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002669}
2670
Daniel Vetter2c642b02015-04-14 17:35:26 +02002671static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002672{
2673 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2674 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
Imre Deaka92d1a92017-05-10 12:21:52 +03002675 return (size_t)bdw_gmch_ctl << 25; /* 32 MB units */
Ben Widawsky9459d252013-11-03 16:53:55 -08002676}
2677
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002678static size_t chv_get_stolen_size(u16 gmch_ctrl)
2679{
2680 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2681 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2682
2683 /*
2684 * 0x0 to 0x10: 32MB increments starting at 0MB
2685 * 0x11 to 0x16: 4MB increments starting at 8MB
2686 * 0x17 to 0x1d: 4MB increments start at 36MB
2687 */
2688 if (gmch_ctrl < 0x11)
Imre Deaka92d1a92017-05-10 12:21:52 +03002689 return (size_t)gmch_ctrl << 25;
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002690 else if (gmch_ctrl < 0x17)
Imre Deaka92d1a92017-05-10 12:21:52 +03002691 return (size_t)(gmch_ctrl - 0x11 + 2) << 22;
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002692 else
Imre Deaka92d1a92017-05-10 12:21:52 +03002693 return (size_t)(gmch_ctrl - 0x17 + 9) << 22;
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002694}
2695
Damien Lespiau66375012014-01-09 18:02:46 +00002696static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2697{
2698 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2699 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2700
2701 if (gen9_gmch_ctl < 0xf0)
Imre Deaka92d1a92017-05-10 12:21:52 +03002702 return (size_t)gen9_gmch_ctl << 25; /* 32 MB units */
Damien Lespiau66375012014-01-09 18:02:46 +00002703 else
2704 /* 4MB increments starting at 0xf0 for 4MB */
Imre Deaka92d1a92017-05-10 12:21:52 +03002705 return (size_t)(gen9_gmch_ctl - 0xf0 + 1) << 22;
Damien Lespiau66375012014-01-09 18:02:46 +00002706}
2707
Chris Wilson34c998b2016-08-04 07:52:24 +01002708static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002709{
Chris Wilson49d73912016-11-29 09:50:08 +00002710 struct drm_i915_private *dev_priv = ggtt->base.i915;
2711 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002712 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002713 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002714
2715 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002716 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002717
Imre Deak2a073f892015-03-27 13:07:33 +02002718 /*
2719 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2720 * dropped. For WC mappings in general we have 64 byte burst writes
2721 * when the WC buffer is flushed, so we can't use it, but have to
2722 * resort to an uncached mapping. The WC issue is easily caught by the
2723 * readback check when writing GTT PTE entries.
2724 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002725 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002726 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002727 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002728 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002729 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002730 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002731 return -ENOMEM;
2732 }
2733
Chris Wilson84486612017-02-15 08:43:40 +00002734 ret = setup_scratch_page(&ggtt->base, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002735 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002736 DRM_ERROR("Scratch setup failed\n");
2737 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002738 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002739 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002740 }
2741
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002742 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002743}
2744
Rodrigo Vivi4e349352017-08-15 16:25:39 -07002745static void cnl_setup_private_ppat(struct drm_i915_private *dev_priv)
2746{
2747 /* XXX: spec is unclear if this is still needed for CNL+ */
2748 if (!USES_PPGTT(dev_priv)) {
2749 I915_WRITE(GEN10_PAT_INDEX(0), GEN8_PPAT_UC);
2750 return;
2751 }
2752
2753 I915_WRITE(GEN10_PAT_INDEX(0), GEN8_PPAT_WB | GEN8_PPAT_LLC);
2754 I915_WRITE(GEN10_PAT_INDEX(1), GEN8_PPAT_WC | GEN8_PPAT_LLCELLC);
2755 I915_WRITE(GEN10_PAT_INDEX(2), GEN8_PPAT_WT | GEN8_PPAT_LLCELLC);
2756 I915_WRITE(GEN10_PAT_INDEX(3), GEN8_PPAT_UC);
2757 I915_WRITE(GEN10_PAT_INDEX(4), GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0));
2758 I915_WRITE(GEN10_PAT_INDEX(5), GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1));
2759 I915_WRITE(GEN10_PAT_INDEX(6), GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2));
2760 I915_WRITE(GEN10_PAT_INDEX(7), GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2761}
2762
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002763/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2764 * bits. When using advanced contexts each context stores its own PAT, but
2765 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002766static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002767{
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002768 u64 pat;
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002769
2770 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2771 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2772 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2773 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2774 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2775 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2776 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2777 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2778
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002779 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002780 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2781 * so RTL will always use the value corresponding to
2782 * pat_sel = 000".
2783 * So let's disable cache for GGTT to avoid screen corruptions.
2784 * MOCS still can be used though.
2785 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2786 * before this patch, i.e. the same uncached + snooping access
2787 * like on gen6/7 seems to be in effect.
2788 * - So this just fixes blitter/render access. Again it looks
2789 * like it's not just uncached access, but uncached + snooping.
2790 * So we can still hold onto all our assumptions wrt cpu
2791 * clflushing on LLC machines.
2792 */
2793 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2794
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002795 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2796 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002797 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2798 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002799}
2800
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002801static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2802{
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002803 u64 pat;
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002804
2805 /*
2806 * Map WB on BDW to snooped on CHV.
2807 *
2808 * Only the snoop bit has meaning for CHV, the rest is
2809 * ignored.
2810 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002811 * The hardware will never snoop for certain types of accesses:
2812 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2813 * - PPGTT page tables
2814 * - some other special cycles
2815 *
2816 * As with BDW, we also need to consider the following for GT accesses:
2817 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2818 * so RTL will always use the value corresponding to
2819 * pat_sel = 000".
2820 * Which means we must set the snoop bit in PAT entry 0
2821 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002822 */
2823 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2824 GEN8_PPAT(1, 0) |
2825 GEN8_PPAT(2, 0) |
2826 GEN8_PPAT(3, 0) |
2827 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2828 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2829 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2830 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2831
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002832 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2833 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002834}
2835
Chris Wilson34c998b2016-08-04 07:52:24 +01002836static void gen6_gmch_remove(struct i915_address_space *vm)
2837{
2838 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2839
2840 iounmap(ggtt->gsm);
Chris Wilson84486612017-02-15 08:43:40 +00002841 cleanup_scratch_page(vm);
Chris Wilson34c998b2016-08-04 07:52:24 +01002842}
2843
Joonas Lahtinend507d732016-03-18 10:42:58 +02002844static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08002845{
Chris Wilson49d73912016-11-29 09:50:08 +00002846 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002847 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002848 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08002849 u16 snb_gmch_ctl;
Imre Deak45192902017-05-10 12:21:50 +03002850 int err;
Ben Widawsky63340132013-11-04 19:32:22 -08002851
2852 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002853 ggtt->mappable_base = pci_resource_start(pdev, 2);
2854 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08002855
Imre Deak45192902017-05-10 12:21:50 +03002856 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(39));
2857 if (!err)
2858 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
2859 if (err)
2860 DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
Ben Widawsky63340132013-11-04 19:32:22 -08002861
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002862 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08002863
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002864 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002865 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002866 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002867 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002868 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002869 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002870 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002871 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002872 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002873 }
Ben Widawsky63340132013-11-04 19:32:22 -08002874
Chris Wilson34c998b2016-08-04 07:52:24 +01002875 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002876
Rodrigo Vivi4e349352017-08-15 16:25:39 -07002877 if (INTEL_GEN(dev_priv) >= 10)
2878 cnl_setup_private_ppat(dev_priv);
2879 else if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002880 chv_setup_private_ppat(dev_priv);
2881 else
2882 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002883
Chris Wilson34c998b2016-08-04 07:52:24 +01002884 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002885 ggtt->base.bind_vma = ggtt_bind_vma;
2886 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05302887 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002888 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01002889 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002890 ggtt->base.clear_range = gen8_ggtt_clear_range;
2891
2892 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002893
Jon Bloomfield0ef34ad2017-05-24 08:54:11 -07002894 /* Serialize GTT updates with aperture access on BXT if VT-d is on. */
2895 if (intel_ggtt_update_needs_vtd_wa(dev_priv)) {
2896 ggtt->base.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
2897 ggtt->base.insert_page = bxt_vtd_ggtt_insert_page__BKL;
2898 if (ggtt->base.clear_range != nop_clear_range)
2899 ggtt->base.clear_range = bxt_vtd_ggtt_clear_range__BKL;
2900 }
2901
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002902 ggtt->invalidate = gen6_ggtt_invalidate;
2903
Chris Wilson34c998b2016-08-04 07:52:24 +01002904 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08002905}
2906
Joonas Lahtinend507d732016-03-18 10:42:58 +02002907static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002908{
Chris Wilson49d73912016-11-29 09:50:08 +00002909 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002910 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002911 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002912 u16 snb_gmch_ctl;
Imre Deak45192902017-05-10 12:21:50 +03002913 int err;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002914
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002915 ggtt->mappable_base = pci_resource_start(pdev, 2);
2916 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08002917
Ben Widawskybaa09f52013-01-24 13:49:57 -08002918 /* 64/512MB is the current min/max we actually know of, but this is just
2919 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002920 */
Chris Wilson34c998b2016-08-04 07:52:24 +01002921 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002922 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002923 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002924 }
2925
Imre Deak45192902017-05-10 12:21:50 +03002926 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
2927 if (!err)
2928 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
2929 if (err)
2930 DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002931 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002932
Joonas Lahtinend507d732016-03-18 10:42:58 +02002933 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002934
Chris Wilson34c998b2016-08-04 07:52:24 +01002935 size = gen6_get_total_gtt_size(snb_gmch_ctl);
2936 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002937
Joonas Lahtinend507d732016-03-18 10:42:58 +02002938 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05302939 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002940 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
2941 ggtt->base.bind_vma = ggtt_bind_vma;
2942 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01002943 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002944
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002945 ggtt->invalidate = gen6_ggtt_invalidate;
2946
Chris Wilson34c998b2016-08-04 07:52:24 +01002947 if (HAS_EDRAM(dev_priv))
2948 ggtt->base.pte_encode = iris_pte_encode;
2949 else if (IS_HASWELL(dev_priv))
2950 ggtt->base.pte_encode = hsw_pte_encode;
2951 else if (IS_VALLEYVIEW(dev_priv))
2952 ggtt->base.pte_encode = byt_pte_encode;
2953 else if (INTEL_GEN(dev_priv) >= 7)
2954 ggtt->base.pte_encode = ivb_pte_encode;
2955 else
2956 ggtt->base.pte_encode = snb_pte_encode;
2957
2958 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002959}
2960
Chris Wilson34c998b2016-08-04 07:52:24 +01002961static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002962{
Chris Wilson34c998b2016-08-04 07:52:24 +01002963 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08002964}
2965
Joonas Lahtinend507d732016-03-18 10:42:58 +02002966static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002967{
Chris Wilson49d73912016-11-29 09:50:08 +00002968 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002969 int ret;
2970
Chris Wilson91c8a322016-07-05 10:40:23 +01002971 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002972 if (!ret) {
2973 DRM_ERROR("failed to set up gmch\n");
2974 return -EIO;
2975 }
2976
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00002977 intel_gtt_get(&ggtt->base.total,
2978 &ggtt->stolen_size,
2979 &ggtt->mappable_base,
2980 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002981
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002982 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05302983 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002984 ggtt->base.insert_entries = i915_ggtt_insert_entries;
2985 ggtt->base.clear_range = i915_ggtt_clear_range;
2986 ggtt->base.bind_vma = ggtt_bind_vma;
2987 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01002988 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002989
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002990 ggtt->invalidate = gmch_ggtt_invalidate;
2991
Joonas Lahtinend507d732016-03-18 10:42:58 +02002992 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002993 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2994
Ben Widawskybaa09f52013-01-24 13:49:57 -08002995 return 0;
2996}
2997
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002998/**
Chris Wilson0088e522016-08-04 07:52:21 +01002999 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003000 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003001 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003002int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003003{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003004 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003005 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003006
Chris Wilson49d73912016-11-29 09:50:08 +00003007 ggtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00003008 ggtt->base.dma = &dev_priv->drm.pdev->dev;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003009
Chris Wilson34c998b2016-08-04 07:52:24 +01003010 if (INTEL_GEN(dev_priv) <= 5)
3011 ret = i915_gmch_probe(ggtt);
3012 else if (INTEL_GEN(dev_priv) < 8)
3013 ret = gen6_gmch_probe(ggtt);
3014 else
3015 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003016 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003017 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003018
Chris Wilsondb9309a2017-01-05 15:30:23 +00003019 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3020 * This is easier than doing range restriction on the fly, as we
3021 * currently don't have any bits spare to pass in this upper
3022 * restriction!
3023 */
3024 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3025 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3026 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3027 }
3028
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003029 if ((ggtt->base.total - 1) >> 32) {
3030 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003031 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003032 ggtt->base.total >> 20);
3033 ggtt->base.total = 1ULL << 32;
3034 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3035 }
3036
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003037 if (ggtt->mappable_end > ggtt->base.total) {
3038 DRM_ERROR("mappable aperture extends past end of GGTT,"
3039 " aperture=%llx, total=%llx\n",
3040 ggtt->mappable_end, ggtt->base.total);
3041 ggtt->mappable_end = ggtt->base.total;
3042 }
3043
Ben Widawskybaa09f52013-01-24 13:49:57 -08003044 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003045 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003046 ggtt->base.total >> 20);
3047 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003048 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Chris Wilson80debff2017-05-25 13:16:12 +01003049 if (intel_vtd_active())
Daniel Vetter5db6c732014-03-31 16:23:04 +02003050 DRM_INFO("VT-d active for gfx access\n");
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003051
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003052 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003053}
3054
3055/**
3056 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003057 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003058 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003059int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003060{
Chris Wilson0088e522016-08-04 07:52:21 +01003061 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3062 int ret;
3063
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003064 INIT_LIST_HEAD(&dev_priv->vm_list);
3065
Chris Wilsona6508de2017-02-06 08:45:47 +00003066 /* Note that we use page colouring to enforce a guard page at the
3067 * end of the address space. This is required as the CS may prefetch
3068 * beyond the end of the batch buffer, across the page boundary,
3069 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003070 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003071 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01003072 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00003073 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003074 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003075 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003076
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003077 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3078 dev_priv->ggtt.mappable_base,
3079 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003080 ret = -EIO;
3081 goto out_gtt_cleanup;
3082 }
3083
3084 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3085
Chris Wilson0088e522016-08-04 07:52:21 +01003086 /*
3087 * Initialise stolen early so that we may reserve preallocated
3088 * objects for the BIOS to KMS transition.
3089 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003090 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003091 if (ret)
3092 goto out_gtt_cleanup;
3093
3094 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003095
3096out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003097 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003098 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003099}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003100
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003101int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003102{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003103 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003104 return -EIO;
3105
3106 return 0;
3107}
3108
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003109void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3110{
Chris Wilson04f7b24e2017-06-01 10:04:46 +01003111 GEM_BUG_ON(i915->ggtt.invalidate != gen6_ggtt_invalidate);
3112
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003113 i915->ggtt.invalidate = guc_ggtt_invalidate;
3114}
3115
3116void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3117{
Chris Wilson04f7b24e2017-06-01 10:04:46 +01003118 /* We should only be called after i915_ggtt_enable_guc() */
3119 GEM_BUG_ON(i915->ggtt.invalidate != guc_ggtt_invalidate);
3120
3121 i915->ggtt.invalidate = gen6_ggtt_invalidate;
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003122}
3123
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003124void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003125{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003126 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003127 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003128
Chris Wilsondc979972016-05-10 14:10:04 +01003129 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003130
3131 /* First fill our portion of the GTT with scratch pages */
Chris Wilson381b9432017-02-15 08:43:54 +00003132 ggtt->base.clear_range(&ggtt->base, 0, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003133
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003134 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3135
3136 /* clflush objects bound into the GGTT and rebind them. */
3137 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003138 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003139 bool ggtt_bound = false;
3140 struct i915_vma *vma;
3141
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003142 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003143 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003144 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003145
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003146 if (!i915_vma_unbind(vma))
3147 continue;
3148
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003149 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3150 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003151 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003152 }
3153
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003154 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003155 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003156 }
3157
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003158 ggtt->base.closed = false;
3159
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003160 if (INTEL_GEN(dev_priv) >= 8) {
Rodrigo Vivi4e349352017-08-15 16:25:39 -07003161 if (INTEL_GEN(dev_priv) >= 10)
3162 cnl_setup_private_ppat(dev_priv);
3163 else if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003164 chv_setup_private_ppat(dev_priv);
3165 else
3166 bdw_setup_private_ppat(dev_priv);
3167
3168 return;
3169 }
3170
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003171 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003172 struct i915_address_space *vm;
3173
Daniel Vetterfa423312015-04-14 17:35:23 +02003174 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003175 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003176
Chris Wilson2bfa9962016-08-04 07:52:25 +01003177 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003178 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003179 else
3180 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003181
Chris Wilson16a011c2017-02-15 08:43:45 +00003182 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003183 }
3184 }
3185
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003186 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003187}
3188
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003189static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003190rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003191 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003192 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003193 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003194{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003195 unsigned int column, row;
3196 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003197
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003198 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003199 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003200 for (row = 0; row < height; row++) {
3201 st->nents++;
3202 /* We don't need the pages, but need to initialize
3203 * the entries so the sg list can be happily traversed.
3204 * The only thing we need are DMA addresses.
3205 */
3206 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003207 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003208 sg_dma_len(sg) = PAGE_SIZE;
3209 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003210 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003211 }
3212 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003213
3214 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003215}
3216
Chris Wilsonba7a5742017-02-15 08:43:35 +00003217static noinline struct sg_table *
3218intel_rotate_pages(struct intel_rotation_info *rot_info,
3219 struct drm_i915_gem_object *obj)
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003220{
Chris Wilson75c7b0b2017-02-15 08:43:57 +00003221 const unsigned long n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003222 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003223 struct sgt_iter sgt_iter;
3224 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003225 unsigned long i;
3226 dma_addr_t *page_addr_list;
3227 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003228 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003229 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003230
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003231 /* Allocate a temporary list of source pages for random access. */
Michal Hocko20981052017-05-17 14:23:12 +02003232 page_addr_list = kvmalloc_array(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003233 sizeof(dma_addr_t),
3234 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003235 if (!page_addr_list)
3236 return ERR_PTR(ret);
3237
3238 /* Allocate target SG list. */
3239 st = kmalloc(sizeof(*st), GFP_KERNEL);
3240 if (!st)
3241 goto err_st_alloc;
3242
Ville Syrjälä6687c902015-09-15 13:16:41 +03003243 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003244 if (ret)
3245 goto err_sg_alloc;
3246
3247 /* Populate source page list from the object. */
3248 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003249 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003250 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003251
Dave Gordon85d12252016-05-20 11:54:06 +01003252 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003253 st->nents = 0;
3254 sg = st->sgl;
3255
Ville Syrjälä6687c902015-09-15 13:16:41 +03003256 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3257 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3258 rot_info->plane[i].width, rot_info->plane[i].height,
3259 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003260 }
3261
Ville Syrjälä6687c902015-09-15 13:16:41 +03003262 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3263 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003264
Michal Hocko20981052017-05-17 14:23:12 +02003265 kvfree(page_addr_list);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003266
3267 return st;
3268
3269err_sg_alloc:
3270 kfree(st);
3271err_st_alloc:
Michal Hocko20981052017-05-17 14:23:12 +02003272 kvfree(page_addr_list);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003273
Ville Syrjälä6687c902015-09-15 13:16:41 +03003274 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3275 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3276
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003277 return ERR_PTR(ret);
3278}
3279
Chris Wilsonba7a5742017-02-15 08:43:35 +00003280static noinline struct sg_table *
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003281intel_partial_pages(const struct i915_ggtt_view *view,
3282 struct drm_i915_gem_object *obj)
3283{
3284 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003285 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003286 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003287 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003288 int ret = -ENOMEM;
3289
3290 st = kmalloc(sizeof(*st), GFP_KERNEL);
3291 if (!st)
3292 goto err_st_alloc;
3293
Chris Wilsond2a84a72016-10-28 13:58:34 +01003294 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003295 if (ret)
3296 goto err_sg_alloc;
3297
Chris Wilson8bab11932017-01-14 00:28:25 +00003298 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003299 GEM_BUG_ON(!iter);
3300
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003301 sg = st->sgl;
3302 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003303 do {
3304 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003305
Chris Wilsond2a84a72016-10-28 13:58:34 +01003306 len = min(iter->length - (offset << PAGE_SHIFT),
3307 count << PAGE_SHIFT);
3308 sg_set_page(sg, NULL, len, 0);
3309 sg_dma_address(sg) =
3310 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3311 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003312
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003313 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003314 count -= len >> PAGE_SHIFT;
3315 if (count == 0) {
3316 sg_mark_end(sg);
3317 return st;
3318 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003319
Chris Wilsond2a84a72016-10-28 13:58:34 +01003320 sg = __sg_next(sg);
3321 iter = __sg_next(iter);
3322 offset = 0;
3323 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003324
3325err_sg_alloc:
3326 kfree(st);
3327err_st_alloc:
3328 return ERR_PTR(ret);
3329}
3330
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003331static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003332i915_get_ggtt_vma_pages(struct i915_vma *vma)
3333{
Chris Wilsonba7a5742017-02-15 08:43:35 +00003334 int ret;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003335
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003336 /* The vma->pages are only valid within the lifespan of the borrowed
3337 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3338 * must be the vma->pages. A simple rule is that vma->pages must only
3339 * be accessed when the obj->mm.pages are pinned.
3340 */
3341 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3342
Chris Wilsonba7a5742017-02-15 08:43:35 +00003343 switch (vma->ggtt_view.type) {
3344 case I915_GGTT_VIEW_NORMAL:
3345 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003346 return 0;
3347
Chris Wilsonba7a5742017-02-15 08:43:35 +00003348 case I915_GGTT_VIEW_ROTATED:
Chris Wilson247177d2016-08-15 10:48:47 +01003349 vma->pages =
Chris Wilsonba7a5742017-02-15 08:43:35 +00003350 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3351 break;
3352
3353 case I915_GGTT_VIEW_PARTIAL:
Chris Wilson247177d2016-08-15 10:48:47 +01003354 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003355 break;
3356
3357 default:
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003358 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3359 vma->ggtt_view.type);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003360 return -EINVAL;
3361 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003362
Chris Wilsonba7a5742017-02-15 08:43:35 +00003363 ret = 0;
3364 if (unlikely(IS_ERR(vma->pages))) {
Chris Wilson247177d2016-08-15 10:48:47 +01003365 ret = PTR_ERR(vma->pages);
3366 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003367 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3368 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003369 }
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003370 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003371}
3372
Chris Wilsone007b192017-01-11 11:23:10 +00003373/**
Chris Wilson625d9882017-01-11 11:23:11 +00003374 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003375 * @vm: the &struct i915_address_space
3376 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3377 * @size: how much space to allocate inside the GTT,
3378 * must be #I915_GTT_PAGE_SIZE aligned
3379 * @offset: where to insert inside the GTT,
3380 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3381 * (@offset + @size) must fit within the address space
3382 * @color: color to apply to node, if this node is not from a VMA,
3383 * color must be #I915_COLOR_UNEVICTABLE
3384 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003385 *
3386 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3387 * the address space (using @size and @color). If the @node does not fit, it
3388 * tries to evict any overlapping nodes from the GTT, including any
3389 * neighbouring nodes if the colors do not match (to ensure guard pages between
3390 * differing domains). See i915_gem_evict_for_node() for the gory details
3391 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3392 * evicting active overlapping objects, and any overlapping node that is pinned
3393 * or marked as unevictable will also result in failure.
3394 *
3395 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3396 * asked to wait for eviction and interrupted.
3397 */
3398int i915_gem_gtt_reserve(struct i915_address_space *vm,
3399 struct drm_mm_node *node,
3400 u64 size, u64 offset, unsigned long color,
3401 unsigned int flags)
3402{
3403 int err;
3404
3405 GEM_BUG_ON(!size);
3406 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3407 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3408 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003409 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003410 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003411
3412 node->size = size;
3413 node->start = offset;
3414 node->color = color;
3415
3416 err = drm_mm_reserve_node(&vm->mm, node);
3417 if (err != -ENOSPC)
3418 return err;
3419
Chris Wilson616d9ce2017-06-16 15:05:21 +01003420 if (flags & PIN_NOEVICT)
3421 return -ENOSPC;
3422
Chris Wilson625d9882017-01-11 11:23:11 +00003423 err = i915_gem_evict_for_node(vm, node, flags);
3424 if (err == 0)
3425 err = drm_mm_reserve_node(&vm->mm, node);
3426
3427 return err;
3428}
3429
Chris Wilson606fec92017-01-11 11:23:12 +00003430static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3431{
3432 u64 range, addr;
3433
3434 GEM_BUG_ON(range_overflows(start, len, end));
3435 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3436
3437 range = round_down(end - len, align) - round_up(start, align);
3438 if (range) {
3439 if (sizeof(unsigned long) == sizeof(u64)) {
3440 addr = get_random_long();
3441 } else {
3442 addr = get_random_int();
3443 if (range > U32_MAX) {
3444 addr <<= 32;
3445 addr |= get_random_int();
3446 }
3447 }
3448 div64_u64_rem(addr, range, &addr);
3449 start += addr;
3450 }
3451
3452 return round_up(start, align);
3453}
3454
Chris Wilson625d9882017-01-11 11:23:11 +00003455/**
Chris Wilsone007b192017-01-11 11:23:10 +00003456 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003457 * @vm: the &struct i915_address_space
3458 * @node: the &struct drm_mm_node (typically i915_vma.node)
3459 * @size: how much space to allocate inside the GTT,
3460 * must be #I915_GTT_PAGE_SIZE aligned
3461 * @alignment: required alignment of starting offset, may be 0 but
3462 * if specified, this must be a power-of-two and at least
3463 * #I915_GTT_MIN_ALIGNMENT
3464 * @color: color to apply to node
3465 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003466 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003467 * @end: end of any range restriction inside GTT (U64_MAX for all),
3468 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3469 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003470 *
3471 * i915_gem_gtt_insert() first searches for an available hole into which
3472 * is can insert the node. The hole address is aligned to @alignment and
3473 * its @size must then fit entirely within the [@start, @end] bounds. The
3474 * nodes on either side of the hole must match @color, or else a guard page
3475 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003476 * suitable hole is found, first a victim is randomly selected and tested
3477 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003478 * is scanned to find the first set of replacement nodes to create the hole.
3479 * Those old overlapping nodes are evicted from the GTT (and so must be
3480 * rebound before any future use). Any node that is currently pinned cannot
3481 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3482 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3483 * searching for an eviction candidate. See i915_gem_evict_something() for
3484 * the gory details on the eviction algorithm.
3485 *
3486 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3487 * asked to wait for eviction and interrupted.
3488 */
3489int i915_gem_gtt_insert(struct i915_address_space *vm,
3490 struct drm_mm_node *node,
3491 u64 size, u64 alignment, unsigned long color,
3492 u64 start, u64 end, unsigned int flags)
3493{
Chris Wilson4e64e552017-02-02 21:04:38 +00003494 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003495 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003496 int err;
3497
3498 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3499 GEM_BUG_ON(!size);
3500 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3501 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3502 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3503 GEM_BUG_ON(start >= end);
3504 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3505 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003506 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003507 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003508
3509 if (unlikely(range_overflows(start, size, end)))
3510 return -ENOSPC;
3511
3512 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3513 return -ENOSPC;
3514
Chris Wilson4e64e552017-02-02 21:04:38 +00003515 mode = DRM_MM_INSERT_BEST;
3516 if (flags & PIN_HIGH)
3517 mode = DRM_MM_INSERT_HIGH;
3518 if (flags & PIN_MAPPABLE)
3519 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003520
3521 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3522 * so we know that we always have a minimum alignment of 4096.
3523 * The drm_mm range manager is optimised to return results
3524 * with zero alignment, so where possible use the optimal
3525 * path.
3526 */
3527 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3528 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3529 alignment = 0;
3530
Chris Wilson4e64e552017-02-02 21:04:38 +00003531 err = drm_mm_insert_node_in_range(&vm->mm, node,
3532 size, alignment, color,
3533 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003534 if (err != -ENOSPC)
3535 return err;
3536
Chris Wilson616d9ce2017-06-16 15:05:21 +01003537 if (flags & PIN_NOEVICT)
3538 return -ENOSPC;
3539
Chris Wilson606fec92017-01-11 11:23:12 +00003540 /* No free space, pick a slot at random.
3541 *
3542 * There is a pathological case here using a GTT shared between
3543 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3544 *
3545 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3546 * (64k objects) (448k objects)
3547 *
3548 * Now imagine that the eviction LRU is ordered top-down (just because
3549 * pathology meets real life), and that we need to evict an object to
3550 * make room inside the aperture. The eviction scan then has to walk
3551 * the 448k list before it finds one within range. And now imagine that
3552 * it has to search for a new hole between every byte inside the memcpy,
3553 * for several simultaneous clients.
3554 *
3555 * On a full-ppgtt system, if we have run out of available space, there
3556 * will be lots and lots of objects in the eviction list! Again,
3557 * searching that LRU list may be slow if we are also applying any
3558 * range restrictions (e.g. restriction to low 4GiB) and so, for
3559 * simplicity and similarilty between different GTT, try the single
3560 * random replacement first.
3561 */
3562 offset = random_offset(start, end,
3563 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3564 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3565 if (err != -ENOSPC)
3566 return err;
3567
3568 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003569 err = i915_gem_evict_something(vm, size, alignment, color,
3570 start, end, flags);
3571 if (err)
3572 return err;
3573
Chris Wilson4e64e552017-02-02 21:04:38 +00003574 return drm_mm_insert_node_in_range(&vm->mm, node,
3575 size, alignment, color,
3576 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003577}
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003578
3579#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3580#include "selftests/mock_gtt.c"
Chris Wilson1c428192017-02-13 17:15:38 +00003581#include "selftests/i915_gem_gtt.c"
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003582#endif