blob: 84b50d6366884e9968dd65c063c004fa2865c0f4 [file] [log] [blame]
Daniel Vetter76aaf222010-11-05 22:23:30 +01001/*
2 * Copyright © 2010 Daniel Vetter
Ben Widawskyc4ac5242014-02-19 22:05:47 -08003 * Copyright © 2011-2014 Intel Corporation
Daniel Vetter76aaf222010-11-05 22:23:30 +01004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
Chris Wilsonaae4a3d2017-02-13 17:15:44 +000026#include <linux/slab.h> /* fault-inject.h is not standalone! */
27
28#include <linux/fault-inject.h>
Chris Wilsone007b192017-01-11 11:23:10 +000029#include <linux/log2.h>
Chris Wilson606fec92017-01-11 11:23:12 +000030#include <linux/random.h>
Daniel Vetter0e46ce22014-01-08 16:10:27 +010031#include <linux/seq_file.h>
Chris Wilson5bab6f62015-10-23 18:43:32 +010032#include <linux/stop_machine.h>
Chris Wilsone007b192017-01-11 11:23:10 +000033
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drmP.h>
35#include <drm/i915_drm.h>
Chris Wilsone007b192017-01-11 11:23:10 +000036
Daniel Vetter76aaf222010-11-05 22:23:30 +010037#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080038#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010039#include "i915_trace.h"
40#include "intel_drv.h"
Chris Wilsond07f0e52016-10-28 13:58:44 +010041#include "intel_frontbuffer.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010042
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010043#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
44
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000045/**
46 * DOC: Global GTT views
47 *
48 * Background and previous state
49 *
50 * Historically objects could exists (be bound) in global GTT space only as
51 * singular instances with a view representing all of the object's backing pages
52 * in a linear fashion. This view will be called a normal view.
53 *
54 * To support multiple views of the same object, where the number of mapped
55 * pages is not equal to the backing store, or where the layout of the pages
56 * is not linear, concept of a GGTT view was added.
57 *
58 * One example of an alternative view is a stereo display driven by a single
59 * image. In this case we would have a framebuffer looking like this
60 * (2x2 pages):
61 *
62 * 12
63 * 34
64 *
65 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
66 * rendering. In contrast, fed to the display engine would be an alternative
67 * view which could look something like this:
68 *
69 * 1212
70 * 3434
71 *
72 * In this example both the size and layout of pages in the alternative view is
73 * different from the normal view.
74 *
75 * Implementation and usage
76 *
77 * GGTT views are implemented using VMAs and are distinguished via enum
78 * i915_ggtt_view_type and struct i915_ggtt_view.
79 *
80 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020081 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
82 * renaming in large amounts of code. They take the struct i915_ggtt_view
83 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000084 *
85 * As a helper for callers which are only interested in the normal view,
86 * globally const i915_ggtt_view_normal singleton instance exists. All old core
87 * GEM API functions, the ones not taking the view parameter, are operating on,
88 * or with the normal GGTT view.
89 *
90 * Code wanting to add or use a new GGTT view needs to:
91 *
92 * 1. Add a new enum with a suitable name.
93 * 2. Extend the metadata in the i915_ggtt_view structure if required.
94 * 3. Add support to i915_get_vma_pages().
95 *
96 * New views are required to build a scatter-gather table from within the
97 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
98 * exists for the lifetime of an VMA.
99 *
100 * Core API is designed to have copy semantics which means that passed in
101 * struct i915_ggtt_view does not need to be persistent (left around after
102 * calling the core API functions).
103 *
104 */
105
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200106static int
107i915_get_ggtt_vma_pages(struct i915_vma *vma);
108
Chris Wilson7c3f86b2017-01-12 11:00:49 +0000109static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
110{
111 /* Note that as an uncached mmio write, this should flush the
112 * WCB of the writes into the GGTT before it triggers the invalidate.
113 */
114 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
115}
116
117static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
118{
119 gen6_ggtt_invalidate(dev_priv);
120 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
121}
122
123static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
124{
125 intel_gtt_chipset_flush();
126}
127
128static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
129{
130 i915->ggtt.invalidate(i915);
131}
132
Chris Wilsonc0336662016-05-06 15:40:21 +0100133int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
134 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200135{
Chris Wilson1893a712014-09-19 11:56:27 +0100136 bool has_aliasing_ppgtt;
137 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100138 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100139
Michel Thierry9e1d0e62016-12-05 17:57:03 -0800140 has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
141 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
142 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100143
Zhi Wange320d402016-09-06 12:04:12 +0800144 if (intel_vgpu_active(dev_priv)) {
145 /* emulation is too hard */
146 has_full_ppgtt = false;
147 has_full_48bit_ppgtt = false;
148 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800149
Chris Wilson0e4ca102016-04-29 13:18:22 +0100150 if (!has_aliasing_ppgtt)
151 return 0;
152
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000153 /*
154 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
155 * execlists, the sole mechanism available to submit work.
156 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100157 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200158 return 0;
159
160 if (enable_ppgtt == 1)
161 return 1;
162
Chris Wilson1893a712014-09-19 11:56:27 +0100163 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200164 return 2;
165
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100166 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
167 return 3;
168
Daniel Vetter93a25a92014-03-06 09:40:43 +0100169#ifdef CONFIG_INTEL_IOMMU
170 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100171 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100172 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200173 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100174 }
175#endif
176
Jesse Barnes62942ed2014-06-13 09:28:33 -0700177 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100178 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700179 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
180 return 0;
181 }
182
Zhi Wange320d402016-09-06 12:04:12 +0800183 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100184 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000185 else
186 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100187}
188
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200189static int ppgtt_bind_vma(struct i915_vma *vma,
190 enum i915_cache_level cache_level,
191 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200192{
Chris Wilsonff685972017-02-15 08:43:42 +0000193 u32 pte_flags;
194 int ret;
195
Chris Wilsonff685972017-02-15 08:43:42 +0000196 ret = vma->vm->allocate_va_range(vma->vm, vma->node.start, vma->size);
197 if (ret)
198 return ret;
Daniel Vetter47552652015-04-14 17:35:24 +0200199
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100200 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100201
Daniel Vetter47552652015-04-14 17:35:24 +0200202 /* Currently applicable only to VLV */
Chris Wilsonff685972017-02-15 08:43:42 +0000203 pte_flags = 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200204 if (vma->obj->gt_ro)
205 pte_flags |= PTE_READ_ONLY;
206
Chris Wilson247177d2016-08-15 10:48:47 +0100207 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200208 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200209
210 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200211}
212
213static void ppgtt_unbind_vma(struct i915_vma *vma)
214{
Chris Wilsonff685972017-02-15 08:43:42 +0000215 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200216}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800217
Daniel Vetter2c642b02015-04-14 17:35:26 +0200218static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200219 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700220{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200221 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700222 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300223
224 switch (level) {
225 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800226 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300227 break;
228 case I915_CACHE_WT:
229 pte |= PPAT_DISPLAY_ELLC_INDEX;
230 break;
231 default:
232 pte |= PPAT_CACHED_INDEX;
233 break;
234 }
235
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700236 return pte;
237}
238
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300239static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
240 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800241{
Michel Thierry07749ef2015-03-16 16:00:54 +0000242 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800243 pde |= addr;
244 if (level != I915_CACHE_NONE)
245 pde |= PPAT_CACHED_PDE_INDEX;
246 else
247 pde |= PPAT_UNCACHED_INDEX;
248 return pde;
249}
250
Michel Thierry762d9932015-07-30 11:05:29 +0100251#define gen8_pdpe_encode gen8_pde_encode
252#define gen8_pml4e_encode gen8_pde_encode
253
Michel Thierry07749ef2015-03-16 16:00:54 +0000254static gen6_pte_t snb_pte_encode(dma_addr_t addr,
255 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200256 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700257{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200258 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700259 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700260
261 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100262 case I915_CACHE_L3_LLC:
263 case I915_CACHE_LLC:
264 pte |= GEN6_PTE_CACHE_LLC;
265 break;
266 case I915_CACHE_NONE:
267 pte |= GEN6_PTE_UNCACHED;
268 break;
269 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100270 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100271 }
272
273 return pte;
274}
275
Michel Thierry07749ef2015-03-16 16:00:54 +0000276static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
277 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200278 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100279{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200280 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100281 pte |= GEN6_PTE_ADDR_ENCODE(addr);
282
283 switch (level) {
284 case I915_CACHE_L3_LLC:
285 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700286 break;
287 case I915_CACHE_LLC:
288 pte |= GEN6_PTE_CACHE_LLC;
289 break;
290 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700291 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700292 break;
293 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100294 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700295 }
296
Ben Widawsky54d12522012-09-24 16:44:32 -0700297 return pte;
298}
299
Michel Thierry07749ef2015-03-16 16:00:54 +0000300static gen6_pte_t byt_pte_encode(dma_addr_t addr,
301 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200302 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700303{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200304 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700305 pte |= GEN6_PTE_ADDR_ENCODE(addr);
306
Akash Goel24f3a8c2014-06-17 10:59:42 +0530307 if (!(flags & PTE_READ_ONLY))
308 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700309
310 if (level != I915_CACHE_NONE)
311 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
312
313 return pte;
314}
315
Michel Thierry07749ef2015-03-16 16:00:54 +0000316static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
317 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200318 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700319{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200320 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700321 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700322
323 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700324 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700325
326 return pte;
327}
328
Michel Thierry07749ef2015-03-16 16:00:54 +0000329static gen6_pte_t iris_pte_encode(dma_addr_t addr,
330 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200331 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700332{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200333 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700334 pte |= HSW_PTE_ADDR_ENCODE(addr);
335
Chris Wilson651d7942013-08-08 14:41:10 +0100336 switch (level) {
337 case I915_CACHE_NONE:
338 break;
339 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000340 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100341 break;
342 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000343 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100344 break;
345 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700346
347 return pte;
348}
349
Chris Wilson84486612017-02-15 08:43:40 +0000350static struct page *vm_alloc_page(struct i915_address_space *vm, gfp_t gfp)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000351{
Chris Wilson84486612017-02-15 08:43:40 +0000352 struct page *page;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000353
Chris Wilson84486612017-02-15 08:43:40 +0000354 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
355 i915_gem_shrink_all(vm->i915);
Chris Wilsonaae4a3d2017-02-13 17:15:44 +0000356
Chris Wilson84486612017-02-15 08:43:40 +0000357 if (vm->free_pages.nr)
358 return vm->free_pages.pages[--vm->free_pages.nr];
359
360 page = alloc_page(gfp);
361 if (!page)
362 return NULL;
363
364 if (vm->pt_kmap_wc)
365 set_pages_array_wc(&page, 1);
366
367 return page;
368}
369
370static void vm_free_pages_release(struct i915_address_space *vm)
371{
372 GEM_BUG_ON(!pagevec_count(&vm->free_pages));
373
374 if (vm->pt_kmap_wc)
375 set_pages_array_wb(vm->free_pages.pages,
376 pagevec_count(&vm->free_pages));
377
378 __pagevec_release(&vm->free_pages);
379}
380
381static void vm_free_page(struct i915_address_space *vm, struct page *page)
382{
383 if (!pagevec_add(&vm->free_pages, page))
384 vm_free_pages_release(vm);
385}
386
387static int __setup_page_dma(struct i915_address_space *vm,
388 struct i915_page_dma *p,
389 gfp_t gfp)
390{
391 p->page = vm_alloc_page(vm, gfp | __GFP_NOWARN | __GFP_NORETRY);
392 if (unlikely(!p->page))
Michel Thierry1266cdb2015-03-24 17:06:33 +0000393 return -ENOMEM;
394
Chris Wilson84486612017-02-15 08:43:40 +0000395 p->daddr = dma_map_page(vm->dma, p->page, 0, PAGE_SIZE,
396 PCI_DMA_BIDIRECTIONAL);
397 if (unlikely(dma_mapping_error(vm->dma, p->daddr))) {
398 vm_free_page(vm, p->page);
399 return -ENOMEM;
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300400 }
401
Michel Thierry1266cdb2015-03-24 17:06:33 +0000402 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000403}
404
Chris Wilson84486612017-02-15 08:43:40 +0000405static int setup_page_dma(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000406 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300407{
Chris Wilson84486612017-02-15 08:43:40 +0000408 return __setup_page_dma(vm, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300409}
410
Chris Wilson84486612017-02-15 08:43:40 +0000411static void cleanup_page_dma(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000412 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300413{
Chris Wilson84486612017-02-15 08:43:40 +0000414 dma_unmap_page(vm->dma, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
415 vm_free_page(vm, p->page);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300416}
417
Chris Wilson9231da72017-02-15 08:43:41 +0000418#define kmap_atomic_px(px) kmap_atomic(px_base(px)->page)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300419
Chris Wilson84486612017-02-15 08:43:40 +0000420#define setup_px(vm, px) setup_page_dma((vm), px_base(px))
421#define cleanup_px(vm, px) cleanup_page_dma((vm), px_base(px))
422#define fill_px(ppgtt, px, v) fill_page_dma((vm), px_base(px), (v))
423#define fill32_px(ppgtt, px, v) fill_page_dma_32((vm), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300424
Chris Wilson84486612017-02-15 08:43:40 +0000425static void fill_page_dma(struct i915_address_space *vm,
426 struct i915_page_dma *p,
427 const u64 val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300428{
Chris Wilson9231da72017-02-15 08:43:41 +0000429 u64 * const vaddr = kmap_atomic(p->page);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300430 int i;
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300431
432 for (i = 0; i < 512; i++)
433 vaddr[i] = val;
434
Chris Wilson9231da72017-02-15 08:43:41 +0000435 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300436}
437
Chris Wilson84486612017-02-15 08:43:40 +0000438static void fill_page_dma_32(struct i915_address_space *vm,
439 struct i915_page_dma *p,
440 const u32 v)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300441{
Chris Wilson84486612017-02-15 08:43:40 +0000442 fill_page_dma(vm, p, (u64)v << 32 | v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300443}
444
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100445static int
Chris Wilson84486612017-02-15 08:43:40 +0000446setup_scratch_page(struct i915_address_space *vm, gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300447{
Chris Wilson84486612017-02-15 08:43:40 +0000448 return __setup_page_dma(vm, &vm->scratch_page, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300449}
450
Chris Wilson84486612017-02-15 08:43:40 +0000451static void cleanup_scratch_page(struct i915_address_space *vm)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300452{
Chris Wilson84486612017-02-15 08:43:40 +0000453 cleanup_page_dma(vm, &vm->scratch_page);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300454}
455
Chris Wilson84486612017-02-15 08:43:40 +0000456static struct i915_page_table *alloc_pt(struct i915_address_space *vm)
Ben Widawsky06fda602015-02-24 16:22:36 +0000457{
Michel Thierryec565b32015-04-08 12:13:23 +0100458 struct i915_page_table *pt;
Ben Widawsky06fda602015-02-24 16:22:36 +0000459
Chris Wilsondd196742017-02-15 08:43:46 +0000460 pt = kmalloc(sizeof(*pt), GFP_KERNEL | __GFP_NOWARN);
461 if (unlikely(!pt))
Ben Widawsky06fda602015-02-24 16:22:36 +0000462 return ERR_PTR(-ENOMEM);
463
Chris Wilsondd196742017-02-15 08:43:46 +0000464 if (unlikely(setup_px(vm, pt))) {
465 kfree(pt);
466 return ERR_PTR(-ENOMEM);
467 }
Ben Widawsky678d96f2015-03-16 16:00:56 +0000468
Chris Wilsondd196742017-02-15 08:43:46 +0000469 pt->used_ptes = 0;
Ben Widawsky06fda602015-02-24 16:22:36 +0000470 return pt;
471}
472
Chris Wilson84486612017-02-15 08:43:40 +0000473static void free_pt(struct i915_address_space *vm, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000474{
Chris Wilson84486612017-02-15 08:43:40 +0000475 cleanup_px(vm, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300476 kfree(pt);
477}
478
479static void gen8_initialize_pt(struct i915_address_space *vm,
480 struct i915_page_table *pt)
481{
Chris Wilsondd196742017-02-15 08:43:46 +0000482 fill_px(vm, pt,
483 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC));
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300484}
485
486static void gen6_initialize_pt(struct i915_address_space *vm,
487 struct i915_page_table *pt)
488{
Chris Wilsondd196742017-02-15 08:43:46 +0000489 fill32_px(vm, pt,
490 vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0));
Ben Widawsky06fda602015-02-24 16:22:36 +0000491}
492
Chris Wilson84486612017-02-15 08:43:40 +0000493static struct i915_page_directory *alloc_pd(struct i915_address_space *vm)
Ben Widawsky06fda602015-02-24 16:22:36 +0000494{
Michel Thierryec565b32015-04-08 12:13:23 +0100495 struct i915_page_directory *pd;
Ben Widawsky06fda602015-02-24 16:22:36 +0000496
Chris Wilsonfe52e372017-02-15 08:43:47 +0000497 pd = kzalloc(sizeof(*pd), GFP_KERNEL | __GFP_NOWARN);
498 if (unlikely(!pd))
Ben Widawsky06fda602015-02-24 16:22:36 +0000499 return ERR_PTR(-ENOMEM);
500
Chris Wilsonfe52e372017-02-15 08:43:47 +0000501 if (unlikely(setup_px(vm, pd))) {
502 kfree(pd);
503 return ERR_PTR(-ENOMEM);
504 }
Michel Thierry33c88192015-04-08 12:13:33 +0100505
Chris Wilsonfe52e372017-02-15 08:43:47 +0000506 pd->used_pdes = 0;
Ben Widawsky06fda602015-02-24 16:22:36 +0000507 return pd;
508}
509
Chris Wilson84486612017-02-15 08:43:40 +0000510static void free_pd(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000511 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300512{
Chris Wilsonfe52e372017-02-15 08:43:47 +0000513 cleanup_px(vm, pd);
514 kfree(pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300515}
516
517static void gen8_initialize_pd(struct i915_address_space *vm,
518 struct i915_page_directory *pd)
519{
Chris Wilsondd196742017-02-15 08:43:46 +0000520 unsigned int i;
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300521
Chris Wilsondd196742017-02-15 08:43:46 +0000522 fill_px(vm, pd,
523 gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC));
524 for (i = 0; i < I915_PDES; i++)
525 pd->page_table[i] = vm->scratch_pt;
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300526}
527
Chris Wilsonfe52e372017-02-15 08:43:47 +0000528static int __pdp_init(struct i915_address_space *vm,
Michel Thierry6ac18502015-07-29 17:23:46 +0100529 struct i915_page_directory_pointer *pdp)
530{
Chris Wilsone2b763c2017-02-15 08:43:48 +0000531 const unsigned int pdpes = I915_PDPES_PER_PDP(vm->i915);
532 unsigned int i;
Michel Thierry6ac18502015-07-29 17:23:46 +0100533
Chris Wilsonfe52e372017-02-15 08:43:47 +0000534 pdp->page_directory = kmalloc_array(pdpes, sizeof(*pdp->page_directory),
Chris Wilsone2b763c2017-02-15 08:43:48 +0000535 GFP_KERNEL | __GFP_NOWARN);
536 if (unlikely(!pdp->page_directory))
Michel Thierry6ac18502015-07-29 17:23:46 +0100537 return -ENOMEM;
Michel Thierry6ac18502015-07-29 17:23:46 +0100538
Chris Wilsonfe52e372017-02-15 08:43:47 +0000539 for (i = 0; i < pdpes; i++)
540 pdp->page_directory[i] = vm->scratch_pd;
541
Michel Thierry6ac18502015-07-29 17:23:46 +0100542 return 0;
543}
544
545static void __pdp_fini(struct i915_page_directory_pointer *pdp)
546{
Michel Thierry6ac18502015-07-29 17:23:46 +0100547 kfree(pdp->page_directory);
548 pdp->page_directory = NULL;
549}
550
Chris Wilson84486612017-02-15 08:43:40 +0000551static struct i915_page_directory_pointer *
552alloc_pdp(struct i915_address_space *vm)
Michel Thierry762d9932015-07-30 11:05:29 +0100553{
554 struct i915_page_directory_pointer *pdp;
555 int ret = -ENOMEM;
556
Chris Wilson84486612017-02-15 08:43:40 +0000557 WARN_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Michel Thierry762d9932015-07-30 11:05:29 +0100558
559 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
560 if (!pdp)
561 return ERR_PTR(-ENOMEM);
562
Chris Wilsonfe52e372017-02-15 08:43:47 +0000563 ret = __pdp_init(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100564 if (ret)
565 goto fail_bitmap;
566
Chris Wilson84486612017-02-15 08:43:40 +0000567 ret = setup_px(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100568 if (ret)
569 goto fail_page_m;
570
571 return pdp;
572
573fail_page_m:
574 __pdp_fini(pdp);
575fail_bitmap:
576 kfree(pdp);
577
578 return ERR_PTR(ret);
579}
580
Chris Wilson84486612017-02-15 08:43:40 +0000581static void free_pdp(struct i915_address_space *vm,
Michel Thierry6ac18502015-07-29 17:23:46 +0100582 struct i915_page_directory_pointer *pdp)
583{
584 __pdp_fini(pdp);
Chris Wilson84486612017-02-15 08:43:40 +0000585 if (USES_FULL_48BIT_PPGTT(vm->i915)) {
586 cleanup_px(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100587 kfree(pdp);
588 }
589}
590
Michel Thierry69ab76f2015-07-29 17:23:55 +0100591static void gen8_initialize_pdp(struct i915_address_space *vm,
592 struct i915_page_directory_pointer *pdp)
593{
594 gen8_ppgtt_pdpe_t scratch_pdpe;
595
596 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
597
Chris Wilson84486612017-02-15 08:43:40 +0000598 fill_px(vm, pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100599}
600
601static void gen8_initialize_pml4(struct i915_address_space *vm,
602 struct i915_pml4 *pml4)
603{
Chris Wilsone2b763c2017-02-15 08:43:48 +0000604 unsigned int i;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100605
Chris Wilsone2b763c2017-02-15 08:43:48 +0000606 fill_px(vm, pml4,
607 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC));
608 for (i = 0; i < GEN8_PML4ES_PER_PML4; i++)
609 pml4->pdps[i] = vm->scratch_pdp;
Michel Thierry6ac18502015-07-29 17:23:46 +0100610}
611
Ben Widawsky94e409c2013-11-04 22:29:36 -0800612/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100613static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100614 unsigned entry,
615 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800616{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000617 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000618 u32 *cs;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800619
620 BUG_ON(entry >= 4);
621
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000622 cs = intel_ring_begin(req, 6);
623 if (IS_ERR(cs))
624 return PTR_ERR(cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800625
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000626 *cs++ = MI_LOAD_REGISTER_IMM(1);
627 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, entry));
628 *cs++ = upper_32_bits(addr);
629 *cs++ = MI_LOAD_REGISTER_IMM(1);
630 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, entry));
631 *cs++ = lower_32_bits(addr);
632 intel_ring_advance(req, cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800633
634 return 0;
635}
636
Michel Thierry2dba3232015-07-30 11:06:23 +0100637static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
638 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800639{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800640 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800641
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100642 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300643 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
644
John Harrisone85b26d2015-05-29 17:43:56 +0100645 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800646 if (ret)
647 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800648 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800649
Ben Widawskyeeb94882013-12-06 14:11:10 -0800650 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800651}
652
Michel Thierry2dba3232015-07-30 11:06:23 +0100653static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
654 struct drm_i915_gem_request *req)
655{
656 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
657}
658
Mika Kuoppalafce93752016-10-31 17:24:46 +0200659/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
660 * the page table structures, we mark them dirty so that
661 * context switching/execlist queuing code takes extra steps
662 * to ensure that tlbs are flushed.
663 */
664static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
665{
Chris Wilson49d73912016-11-29 09:50:08 +0000666 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200667}
668
Michał Winiarski2ce51792016-10-13 14:02:42 +0200669/* Removes entries from a single page table, releasing it if it's empty.
670 * Caller can use the return value to update higher-level entries.
671 */
672static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200673 struct i915_page_table *pt,
Chris Wilsondd196742017-02-15 08:43:46 +0000674 u64 start, u64 length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700675{
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200676 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200677 unsigned int pte = gen8_pte_index(start);
678 unsigned int pte_end = pte + num_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +0000679 const gen8_pte_t scratch_pte =
680 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
681 gen8_pte_t *vaddr;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200682
Chris Wilsondd196742017-02-15 08:43:46 +0000683 GEM_BUG_ON(num_entries > pt->used_ptes);
Ben Widawsky459108b2013-11-02 21:07:23 -0700684
Chris Wilsondd196742017-02-15 08:43:46 +0000685 pt->used_ptes -= num_entries;
686 if (!pt->used_ptes)
687 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200688
Chris Wilson9231da72017-02-15 08:43:41 +0000689 vaddr = kmap_atomic_px(pt);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200690 while (pte < pte_end)
Chris Wilson894cceb2017-02-15 08:43:37 +0000691 vaddr[pte++] = scratch_pte;
Chris Wilson9231da72017-02-15 08:43:41 +0000692 kunmap_atomic(vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200693
694 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200695}
696
Chris Wilsondd196742017-02-15 08:43:46 +0000697static void gen8_ppgtt_set_pde(struct i915_address_space *vm,
698 struct i915_page_directory *pd,
699 struct i915_page_table *pt,
700 unsigned int pde)
701{
702 gen8_pde_t *vaddr;
703
704 pd->page_table[pde] = pt;
705
706 vaddr = kmap_atomic_px(pd);
707 vaddr[pde] = gen8_pde_encode(px_dma(pt), I915_CACHE_LLC);
708 kunmap_atomic(vaddr);
709}
710
Michał Winiarski2ce51792016-10-13 14:02:42 +0200711static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200712 struct i915_page_directory *pd,
Chris Wilsondd196742017-02-15 08:43:46 +0000713 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200714{
715 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +0000716 u32 pde;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200717
718 gen8_for_each_pde(pt, pd, start, length, pde) {
Chris Wilsondd196742017-02-15 08:43:46 +0000719 if (!gen8_ppgtt_clear_pt(vm, pt, start, length))
720 continue;
Ben Widawsky06fda602015-02-24 16:22:36 +0000721
Chris Wilsondd196742017-02-15 08:43:46 +0000722 gen8_ppgtt_set_pde(vm, pd, vm->scratch_pt, pde);
Chris Wilsonfe52e372017-02-15 08:43:47 +0000723 pd->used_pdes--;
Chris Wilsondd196742017-02-15 08:43:46 +0000724
725 free_pt(vm, pt);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200726 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200727
Chris Wilsonfe52e372017-02-15 08:43:47 +0000728 return !pd->used_pdes;
729}
Michał Winiarski2ce51792016-10-13 14:02:42 +0200730
Chris Wilsonfe52e372017-02-15 08:43:47 +0000731static void gen8_ppgtt_set_pdpe(struct i915_address_space *vm,
732 struct i915_page_directory_pointer *pdp,
733 struct i915_page_directory *pd,
734 unsigned int pdpe)
735{
736 gen8_ppgtt_pdpe_t *vaddr;
737
738 pdp->page_directory[pdpe] = pd;
739 if (!USES_FULL_48BIT_PPGTT(vm->i915))
740 return;
741
742 vaddr = kmap_atomic_px(pdp);
743 vaddr[pdpe] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
744 kunmap_atomic(vaddr);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200745}
Ben Widawsky06fda602015-02-24 16:22:36 +0000746
Michał Winiarski2ce51792016-10-13 14:02:42 +0200747/* Removes entries from a single page dir pointer, releasing it if it's empty.
748 * Caller can use the return value to update higher-level entries
749 */
750static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200751 struct i915_page_directory_pointer *pdp,
Chris Wilsonfe52e372017-02-15 08:43:47 +0000752 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200753{
754 struct i915_page_directory *pd;
Chris Wilsonfe52e372017-02-15 08:43:47 +0000755 unsigned int pdpe;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200756
757 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilsonfe52e372017-02-15 08:43:47 +0000758 if (!gen8_ppgtt_clear_pd(vm, pd, start, length))
759 continue;
Ben Widawsky06fda602015-02-24 16:22:36 +0000760
Chris Wilsonfe52e372017-02-15 08:43:47 +0000761 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
Chris Wilsone2b763c2017-02-15 08:43:48 +0000762 pdp->used_pdpes--;
Chris Wilsonfe52e372017-02-15 08:43:47 +0000763
764 free_pd(vm, pd);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200765 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200766
Chris Wilsone2b763c2017-02-15 08:43:48 +0000767 return !pdp->used_pdpes;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200768}
Ben Widawsky459108b2013-11-02 21:07:23 -0700769
Chris Wilsonfe52e372017-02-15 08:43:47 +0000770static void gen8_ppgtt_clear_3lvl(struct i915_address_space *vm,
771 u64 start, u64 length)
772{
773 gen8_ppgtt_clear_pdp(vm, &i915_vm_to_ppgtt(vm)->pdp, start, length);
774}
775
Chris Wilsone2b763c2017-02-15 08:43:48 +0000776static void gen8_ppgtt_set_pml4e(struct i915_pml4 *pml4,
777 struct i915_page_directory_pointer *pdp,
778 unsigned int pml4e)
779{
780 gen8_ppgtt_pml4e_t *vaddr;
781
782 pml4->pdps[pml4e] = pdp;
783
784 vaddr = kmap_atomic_px(pml4);
785 vaddr[pml4e] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
786 kunmap_atomic(vaddr);
787}
788
Michał Winiarski2ce51792016-10-13 14:02:42 +0200789/* Removes entries from a single pml4.
790 * This is the top-level structure in 4-level page tables used on gen8+.
791 * Empty entries are always scratch pml4e.
792 */
Chris Wilsonfe52e372017-02-15 08:43:47 +0000793static void gen8_ppgtt_clear_4lvl(struct i915_address_space *vm,
794 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200795{
Chris Wilsonfe52e372017-02-15 08:43:47 +0000796 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
797 struct i915_pml4 *pml4 = &ppgtt->pml4;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200798 struct i915_page_directory_pointer *pdp;
Chris Wilsone2b763c2017-02-15 08:43:48 +0000799 unsigned int pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200800
Chris Wilson49d73912016-11-29 09:50:08 +0000801 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700802
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200803 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsone2b763c2017-02-15 08:43:48 +0000804 if (!gen8_ppgtt_clear_pdp(vm, pdp, start, length))
805 continue;
Ben Widawsky459108b2013-11-02 21:07:23 -0700806
Chris Wilsone2b763c2017-02-15 08:43:48 +0000807 gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
Chris Wilsone2b763c2017-02-15 08:43:48 +0000808
809 free_pdp(vm, pdp);
Ben Widawsky459108b2013-11-02 21:07:23 -0700810 }
811}
812
Chris Wilson894cceb2017-02-15 08:43:37 +0000813struct sgt_dma {
814 struct scatterlist *sg;
815 dma_addr_t dma, max;
816};
817
818static __always_inline bool
819gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100820 struct i915_page_directory_pointer *pdp,
Chris Wilson894cceb2017-02-15 08:43:37 +0000821 struct sgt_dma *iter,
822 u64 start,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100823 enum i915_cache_level cache_level)
824{
Chris Wilson894cceb2017-02-15 08:43:37 +0000825 unsigned int pdpe = gen8_pdpe_index(start);
826 unsigned int pde = gen8_pde_index(start);
827 unsigned int pte = gen8_pte_index(start);
828 struct i915_page_directory *pd;
829 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
830 gen8_pte_t *vaddr;
831 bool ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700832
Chris Wilson894cceb2017-02-15 08:43:37 +0000833 pd = pdp->page_directory[pdpe];
Chris Wilson9231da72017-02-15 08:43:41 +0000834 vaddr = kmap_atomic_px(pd->page_table[pde]);
Chris Wilson894cceb2017-02-15 08:43:37 +0000835 do {
836 vaddr[pte] = pte_encode | iter->dma;
837 iter->dma += PAGE_SIZE;
838 if (iter->dma >= iter->max) {
839 iter->sg = __sg_next(iter->sg);
840 if (!iter->sg) {
841 ret = false;
842 break;
843 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700844
Chris Wilson894cceb2017-02-15 08:43:37 +0000845 iter->dma = sg_dma_address(iter->sg);
846 iter->max = iter->dma + iter->sg->length;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000847 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800848
Michel Thierry07749ef2015-03-16 16:00:54 +0000849 if (++pte == GEN8_PTES) {
Michel Thierry07749ef2015-03-16 16:00:54 +0000850 if (++pde == I915_PDES) {
Chris Wilson894cceb2017-02-15 08:43:37 +0000851 /* Limited by sg length for 3lvl */
852 if (++pdpe == GEN8_PML4ES_PER_PML4) {
853 ret = true;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100854 break;
Chris Wilson894cceb2017-02-15 08:43:37 +0000855 }
856
857 GEM_BUG_ON(pdpe > GEN8_LEGACY_PDPES);
858 pd = pdp->page_directory[pdpe];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800859 pde = 0;
860 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000861
Chris Wilson9231da72017-02-15 08:43:41 +0000862 kunmap_atomic(vaddr);
863 vaddr = kmap_atomic_px(pd->page_table[pde]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800864 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700865 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000866 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +0000867 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300868
Chris Wilson894cceb2017-02-15 08:43:37 +0000869 return ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700870}
871
Chris Wilson894cceb2017-02-15 08:43:37 +0000872static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
873 struct sg_table *pages,
874 u64 start,
875 enum i915_cache_level cache_level,
876 u32 unused)
Michel Thierryf9b5b782015-07-30 11:02:49 +0100877{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300878 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilson894cceb2017-02-15 08:43:37 +0000879 struct sgt_dma iter = {
880 .sg = pages->sgl,
881 .dma = sg_dma_address(iter.sg),
882 .max = iter.dma + iter.sg->length,
883 };
Michel Thierryf9b5b782015-07-30 11:02:49 +0100884
Chris Wilson894cceb2017-02-15 08:43:37 +0000885 gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter,
886 start, cache_level);
887}
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100888
Chris Wilson894cceb2017-02-15 08:43:37 +0000889static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
890 struct sg_table *pages,
891 uint64_t start,
892 enum i915_cache_level cache_level,
893 u32 unused)
894{
895 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
896 struct sgt_dma iter = {
897 .sg = pages->sgl,
898 .dma = sg_dma_address(iter.sg),
899 .max = iter.dma + iter.sg->length,
900 };
901 struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
902 unsigned int pml4e = gen8_pml4e_index(start);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100903
Chris Wilson894cceb2017-02-15 08:43:37 +0000904 while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[pml4e++], &iter,
905 start, cache_level))
906 ;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100907}
908
Chris Wilson84486612017-02-15 08:43:40 +0000909static void gen8_free_page_tables(struct i915_address_space *vm,
Michel Thierryf37c0502015-06-10 17:46:39 +0100910 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800911{
912 int i;
913
Mika Kuoppala567047b2015-06-25 18:35:12 +0300914 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800915 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800916
Chris Wilsonfe52e372017-02-15 08:43:47 +0000917 for (i = 0; i < I915_PDES; i++) {
918 if (pd->page_table[i] != vm->scratch_pt)
919 free_pt(vm, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000920 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000921}
922
Mika Kuoppala8776f022015-06-30 18:16:40 +0300923static int gen8_init_scratch(struct i915_address_space *vm)
924{
Matthew Auld64c050d2016-04-27 13:19:25 +0100925 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300926
Chris Wilson84486612017-02-15 08:43:40 +0000927 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100928 if (ret)
929 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300930
Chris Wilson84486612017-02-15 08:43:40 +0000931 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300932 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100933 ret = PTR_ERR(vm->scratch_pt);
934 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300935 }
936
Chris Wilson84486612017-02-15 08:43:40 +0000937 vm->scratch_pd = alloc_pd(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300938 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100939 ret = PTR_ERR(vm->scratch_pd);
940 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300941 }
942
Chris Wilson84486612017-02-15 08:43:40 +0000943 if (USES_FULL_48BIT_PPGTT(dev)) {
944 vm->scratch_pdp = alloc_pdp(vm);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100945 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100946 ret = PTR_ERR(vm->scratch_pdp);
947 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100948 }
949 }
950
Mika Kuoppala8776f022015-06-30 18:16:40 +0300951 gen8_initialize_pt(vm, vm->scratch_pt);
952 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000953 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100954 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300955
956 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100957
958free_pd:
Chris Wilson84486612017-02-15 08:43:40 +0000959 free_pd(vm, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +0100960free_pt:
Chris Wilson84486612017-02-15 08:43:40 +0000961 free_pt(vm, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +0100962free_scratch_page:
Chris Wilson84486612017-02-15 08:43:40 +0000963 cleanup_scratch_page(vm);
Matthew Auld64c050d2016-04-27 13:19:25 +0100964
965 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300966}
967
Zhiyuan Lv650da342015-08-28 15:41:18 +0800968static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
969{
970 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +0000971 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +0800972 int i;
973
Matthew Aulddf285642016-04-22 12:09:25 +0100974 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +0800975 u64 daddr = px_dma(&ppgtt->pml4);
976
Ville Syrjäläab75bb52015-11-04 23:20:12 +0200977 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
978 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +0800979
980 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
981 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
982 } else {
983 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
984 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
985
Ville Syrjäläab75bb52015-11-04 23:20:12 +0200986 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
987 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +0800988 }
989
990 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
991 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
992 }
993
994 I915_WRITE(vgtif_reg(g2v_notify), msg);
995
996 return 0;
997}
998
Mika Kuoppala8776f022015-06-30 18:16:40 +0300999static void gen8_free_scratch(struct i915_address_space *vm)
1000{
Chris Wilson84486612017-02-15 08:43:40 +00001001 if (USES_FULL_48BIT_PPGTT(vm->i915))
1002 free_pdp(vm, vm->scratch_pdp);
1003 free_pd(vm, vm->scratch_pd);
1004 free_pt(vm, vm->scratch_pt);
1005 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001006}
1007
Chris Wilson84486612017-02-15 08:43:40 +00001008static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
Michel Thierry762d9932015-07-30 11:05:29 +01001009 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001010{
1011 int i;
1012
Chris Wilsone2b763c2017-02-15 08:43:48 +00001013 for (i = 0; i < I915_PDPES_PER_PDP(vm->i915); i++) {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001014 if (pdp->page_directory[i] == vm->scratch_pd)
Ben Widawsky06fda602015-02-24 16:22:36 +00001015 continue;
1016
Chris Wilson84486612017-02-15 08:43:40 +00001017 gen8_free_page_tables(vm, pdp->page_directory[i]);
1018 free_pd(vm, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001019 }
Michel Thierry69876be2015-04-08 12:13:27 +01001020
Chris Wilson84486612017-02-15 08:43:40 +00001021 free_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001022}
1023
1024static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1025{
1026 int i;
1027
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001028 for (i = 0; i < GEN8_PML4ES_PER_PML4; i++) {
1029 if (ppgtt->pml4.pdps[i] == ppgtt->base.scratch_pdp)
Michel Thierry762d9932015-07-30 11:05:29 +01001030 continue;
1031
Chris Wilson84486612017-02-15 08:43:40 +00001032 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001033 }
1034
Chris Wilson84486612017-02-15 08:43:40 +00001035 cleanup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001036}
1037
1038static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1039{
Chris Wilson49d73912016-11-29 09:50:08 +00001040 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001041 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001042
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001043 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001044 gen8_ppgtt_notify_vgt(ppgtt, false);
1045
Chris Wilson84486612017-02-15 08:43:40 +00001046 if (!USES_FULL_48BIT_PPGTT(vm->i915))
1047 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001048 else
1049 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001050
Mika Kuoppala8776f022015-06-30 18:16:40 +03001051 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001052}
1053
Chris Wilsonfe52e372017-02-15 08:43:47 +00001054static int gen8_ppgtt_alloc_pd(struct i915_address_space *vm,
1055 struct i915_page_directory *pd,
1056 u64 start, u64 length)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001057{
Michel Thierryd7b26332015-04-08 12:13:34 +01001058 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +00001059 u64 from = start;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001060 unsigned int pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001061
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001062 gen8_for_each_pde(pt, pd, start, length, pde) {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001063 if (pt == vm->scratch_pt) {
Chris Wilsondd196742017-02-15 08:43:46 +00001064 pt = alloc_pt(vm);
1065 if (IS_ERR(pt))
1066 goto unwind;
1067
1068 gen8_initialize_pt(vm, pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001069
Chris Wilsonfe52e372017-02-15 08:43:47 +00001070 gen8_ppgtt_set_pde(vm, pd, pt, pde);
1071 pd->used_pdes++;
1072 }
1073
1074 pt->used_ptes += gen8_pte_count(start, length);
1075 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001076 return 0;
1077
Chris Wilsondd196742017-02-15 08:43:46 +00001078unwind:
1079 gen8_ppgtt_clear_pd(vm, pd, from, start - from);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001080 return -ENOMEM;
1081}
1082
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001083static int gen8_ppgtt_alloc_pdp(struct i915_address_space *vm,
1084 struct i915_page_directory_pointer *pdp,
1085 u64 start, u64 length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001086{
Michel Thierry5441f0c2015-04-08 12:13:28 +01001087 struct i915_page_directory *pd;
Chris Wilsone2b763c2017-02-15 08:43:48 +00001088 u64 from = start;
1089 unsigned int pdpe;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001090 int ret;
1091
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001092 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilsone2b763c2017-02-15 08:43:48 +00001093 if (pd == vm->scratch_pd) {
1094 pd = alloc_pd(vm);
1095 if (IS_ERR(pd))
1096 goto unwind;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001097
Chris Wilsone2b763c2017-02-15 08:43:48 +00001098 gen8_initialize_pd(vm, pd);
Chris Wilsonfe52e372017-02-15 08:43:47 +00001099 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
Chris Wilsone2b763c2017-02-15 08:43:48 +00001100 pdp->used_pdpes++;
Chris Wilson75afcf72017-02-15 08:43:51 +00001101
1102 mark_tlbs_dirty(i915_vm_to_ppgtt(vm));
Chris Wilsone2b763c2017-02-15 08:43:48 +00001103 }
1104
1105 ret = gen8_ppgtt_alloc_pd(vm, pd, start, length);
1106 if (unlikely(ret)) {
1107 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1108 pdp->used_pdpes--;
1109 free_pd(vm, pd);
1110 goto unwind;
1111 }
Chris Wilsonfe52e372017-02-15 08:43:47 +00001112 }
Michel Thierry33c88192015-04-08 12:13:33 +01001113
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001114 return 0;
1115
Chris Wilsone2b763c2017-02-15 08:43:48 +00001116unwind:
1117 gen8_ppgtt_clear_pdp(vm, pdp, from, start - from);
1118 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001119}
1120
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001121static int gen8_ppgtt_alloc_3lvl(struct i915_address_space *vm,
1122 u64 start, u64 length)
Michel Thierry762d9932015-07-30 11:05:29 +01001123{
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001124 return gen8_ppgtt_alloc_pdp(vm,
1125 &i915_vm_to_ppgtt(vm)->pdp, start, length);
1126}
1127
1128static int gen8_ppgtt_alloc_4lvl(struct i915_address_space *vm,
1129 u64 start, u64 length)
1130{
1131 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1132 struct i915_pml4 *pml4 = &ppgtt->pml4;
Michel Thierry762d9932015-07-30 11:05:29 +01001133 struct i915_page_directory_pointer *pdp;
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001134 u64 from = start;
1135 u32 pml4e;
1136 int ret;
Michel Thierry762d9932015-07-30 11:05:29 +01001137
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001138 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001139 if (pml4->pdps[pml4e] == vm->scratch_pdp) {
1140 pdp = alloc_pdp(vm);
1141 if (IS_ERR(pdp))
1142 goto unwind;
Michel Thierry762d9932015-07-30 11:05:29 +01001143
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001144 gen8_initialize_pdp(vm, pdp);
1145 gen8_ppgtt_set_pml4e(pml4, pdp, pml4e);
1146 }
Michel Thierry762d9932015-07-30 11:05:29 +01001147
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001148 ret = gen8_ppgtt_alloc_pdp(vm, pdp, start, length);
1149 if (unlikely(ret)) {
1150 gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
1151 free_pdp(vm, pdp);
1152 goto unwind;
1153 }
Michel Thierry762d9932015-07-30 11:05:29 +01001154 }
1155
Michel Thierry762d9932015-07-30 11:05:29 +01001156 return 0;
1157
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001158unwind:
1159 gen8_ppgtt_clear_4lvl(vm, from, start - from);
1160 return -ENOMEM;
Michel Thierry762d9932015-07-30 11:05:29 +01001161}
1162
Chris Wilson84486612017-02-15 08:43:40 +00001163static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
1164 struct i915_page_directory_pointer *pdp,
Michel Thierryea91e402015-07-29 17:23:57 +01001165 uint64_t start, uint64_t length,
1166 gen8_pte_t scratch_pte,
1167 struct seq_file *m)
1168{
1169 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001170 uint32_t pdpe;
1171
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001172 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001173 struct i915_page_table *pt;
1174 uint64_t pd_len = length;
1175 uint64_t pd_start = start;
1176 uint32_t pde;
1177
Chris Wilsone2b763c2017-02-15 08:43:48 +00001178 if (pdp->page_directory[pdpe] == ppgtt->base.scratch_pd)
Michel Thierryea91e402015-07-29 17:23:57 +01001179 continue;
1180
1181 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001182 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001183 uint32_t pte;
1184 gen8_pte_t *pt_vaddr;
1185
Chris Wilsonfe52e372017-02-15 08:43:47 +00001186 if (pd->page_table[pde] == ppgtt->base.scratch_pt)
Michel Thierryea91e402015-07-29 17:23:57 +01001187 continue;
1188
Chris Wilson9231da72017-02-15 08:43:41 +00001189 pt_vaddr = kmap_atomic_px(pt);
Michel Thierryea91e402015-07-29 17:23:57 +01001190 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1191 uint64_t va =
1192 (pdpe << GEN8_PDPE_SHIFT) |
1193 (pde << GEN8_PDE_SHIFT) |
1194 (pte << GEN8_PTE_SHIFT);
1195 int i;
1196 bool found = false;
1197
1198 for (i = 0; i < 4; i++)
1199 if (pt_vaddr[pte + i] != scratch_pte)
1200 found = true;
1201 if (!found)
1202 continue;
1203
1204 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1205 for (i = 0; i < 4; i++) {
1206 if (pt_vaddr[pte + i] != scratch_pte)
1207 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1208 else
1209 seq_puts(m, " SCRATCH ");
1210 }
1211 seq_puts(m, "\n");
1212 }
Michel Thierryea91e402015-07-29 17:23:57 +01001213 kunmap_atomic(pt_vaddr);
1214 }
1215 }
1216}
1217
1218static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1219{
1220 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson894cceb2017-02-15 08:43:37 +00001221 const gen8_pte_t scratch_pte =
1222 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
Chris Wilson381b9432017-02-15 08:43:54 +00001223 u64 start = 0, length = ppgtt->base.total;
Michel Thierryea91e402015-07-29 17:23:57 +01001224
Chris Wilsonc6385c92016-11-29 12:42:05 +00001225 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Chris Wilson84486612017-02-15 08:43:40 +00001226 gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001227 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001228 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001229 struct i915_pml4 *pml4 = &ppgtt->pml4;
1230 struct i915_page_directory_pointer *pdp;
1231
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001232 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001233 if (pml4->pdps[pml4e] == ppgtt->base.scratch_pdp)
Michel Thierryea91e402015-07-29 17:23:57 +01001234 continue;
1235
1236 seq_printf(m, " PML4E #%llu\n", pml4e);
Chris Wilson84486612017-02-15 08:43:40 +00001237 gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001238 }
1239 }
1240}
1241
Chris Wilsone2b763c2017-02-15 08:43:48 +00001242static int gen8_preallocate_top_level_pdp(struct i915_hw_ppgtt *ppgtt)
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001243{
Chris Wilsone2b763c2017-02-15 08:43:48 +00001244 struct i915_address_space *vm = &ppgtt->base;
1245 struct i915_page_directory_pointer *pdp = &ppgtt->pdp;
1246 struct i915_page_directory *pd;
1247 u64 start = 0, length = ppgtt->base.total;
1248 u64 from = start;
1249 unsigned int pdpe;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001250
Chris Wilsone2b763c2017-02-15 08:43:48 +00001251 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1252 pd = alloc_pd(vm);
1253 if (IS_ERR(pd))
1254 goto unwind;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001255
Chris Wilsone2b763c2017-02-15 08:43:48 +00001256 gen8_initialize_pd(vm, pd);
1257 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
1258 pdp->used_pdpes++;
1259 }
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001260
Chris Wilsone2b763c2017-02-15 08:43:48 +00001261 pdp->used_pdpes++; /* never remove */
1262 return 0;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001263
Chris Wilsone2b763c2017-02-15 08:43:48 +00001264unwind:
1265 start -= from;
1266 gen8_for_each_pdpe(pd, pdp, from, start, pdpe) {
1267 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1268 free_pd(vm, pd);
1269 }
1270 pdp->used_pdpes = 0;
1271 return -ENOMEM;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001272}
1273
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001274/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001275 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1276 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1277 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1278 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001279 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001280 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001281static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001282{
Chris Wilson49d73912016-11-29 09:50:08 +00001283 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001284 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001285
Mika Kuoppala8776f022015-06-30 18:16:40 +03001286 ret = gen8_init_scratch(&ppgtt->base);
1287 if (ret)
1288 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001289
Michel Thierryd7b26332015-04-08 12:13:34 +01001290 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001291 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1292 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001293 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001294
Chris Wilson84486612017-02-15 08:43:40 +00001295 /* There are only few exceptions for gen >=6. chv and bxt.
1296 * And we are not sure about the latter so play safe for now.
1297 */
1298 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
1299 ppgtt->base.pt_kmap_wc = true;
1300
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001301 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Chris Wilson84486612017-02-15 08:43:40 +00001302 ret = setup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001303 if (ret)
1304 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001305
Michel Thierry69ab76f2015-07-29 17:23:55 +01001306 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1307
Michel Thierry762d9932015-07-30 11:05:29 +01001308 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001309 ppgtt->switch_mm = gen8_48b_mm_switch;
Chris Wilson894cceb2017-02-15 08:43:37 +00001310
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001311 ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_4lvl;
Chris Wilson894cceb2017-02-15 08:43:37 +00001312 ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001313 ppgtt->base.clear_range = gen8_ppgtt_clear_4lvl;
Michel Thierry762d9932015-07-30 11:05:29 +01001314 } else {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001315 ret = __pdp_init(&ppgtt->base, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001316 if (ret)
1317 goto free_scratch;
1318
1319 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001320 ppgtt->switch_mm = gen8_legacy_mm_switch;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001321
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001322 if (intel_vgpu_active(dev_priv)) {
Chris Wilsone2b763c2017-02-15 08:43:48 +00001323 ret = gen8_preallocate_top_level_pdp(ppgtt);
1324 if (ret) {
1325 __pdp_fini(&ppgtt->pdp);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001326 goto free_scratch;
Chris Wilsone2b763c2017-02-15 08:43:48 +00001327 }
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001328 }
Chris Wilson894cceb2017-02-15 08:43:37 +00001329
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001330 ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_3lvl;
Chris Wilson894cceb2017-02-15 08:43:37 +00001331 ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001332 ppgtt->base.clear_range = gen8_ppgtt_clear_3lvl;
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001333 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001334
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001335 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001336 gen8_ppgtt_notify_vgt(ppgtt, true);
1337
Michel Thierryd7b26332015-04-08 12:13:34 +01001338 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001339
1340free_scratch:
1341 gen8_free_scratch(&ppgtt->base);
1342 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001343}
1344
Ben Widawsky87d60b62013-12-06 14:11:29 -08001345static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1346{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001347 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001348 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001349 gen6_pte_t scratch_pte;
Chris Wilson381b9432017-02-15 08:43:54 +00001350 u32 pd_entry, pte, pde;
1351 u32 start = 0, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001352
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001353 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001354 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001355
Dave Gordon731f74c2016-06-24 19:37:46 +01001356 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001357 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001358 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001359 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001360 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001361 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1362
1363 if (pd_entry != expected)
1364 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1365 pde,
1366 pd_entry,
1367 expected);
1368 seq_printf(m, "\tPDE: %x\n", pd_entry);
1369
Chris Wilson9231da72017-02-15 08:43:41 +00001370 pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[pde]);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001371
Michel Thierry07749ef2015-03-16 16:00:54 +00001372 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001373 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001374 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001375 (pte * PAGE_SIZE);
1376 int i;
1377 bool found = false;
1378 for (i = 0; i < 4; i++)
1379 if (pt_vaddr[pte + i] != scratch_pte)
1380 found = true;
1381 if (!found)
1382 continue;
1383
1384 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1385 for (i = 0; i < 4; i++) {
1386 if (pt_vaddr[pte + i] != scratch_pte)
1387 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1388 else
1389 seq_puts(m, " SCRATCH ");
1390 }
1391 seq_puts(m, "\n");
1392 }
Chris Wilson9231da72017-02-15 08:43:41 +00001393 kunmap_atomic(pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001394 }
1395}
1396
Ben Widawsky678d96f2015-03-16 16:00:56 +00001397/* Write pde (index) from the page directory @pd to the page table @pt */
Chris Wilson16a011c2017-02-15 08:43:45 +00001398static inline void gen6_write_pde(const struct i915_hw_ppgtt *ppgtt,
1399 const unsigned int pde,
1400 const struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001401{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001402 /* Caller needs to make sure the write completes if necessary */
Chris Wilson16a011c2017-02-15 08:43:45 +00001403 writel_relaxed(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
1404 ppgtt->pd_addr + pde);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001405}
Ben Widawsky61973492013-04-08 18:43:54 -07001406
Ben Widawsky678d96f2015-03-16 16:00:56 +00001407/* Write all the page tables found in the ppgtt structure to incrementing page
1408 * directories. */
Chris Wilson16a011c2017-02-15 08:43:45 +00001409static void gen6_write_page_range(struct i915_hw_ppgtt *ppgtt,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001410 uint32_t start, uint32_t length)
1411{
Michel Thierryec565b32015-04-08 12:13:23 +01001412 struct i915_page_table *pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001413 unsigned int pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001414
Chris Wilson16a011c2017-02-15 08:43:45 +00001415 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde)
1416 gen6_write_pde(ppgtt, pde, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001417
Chris Wilson16a011c2017-02-15 08:43:45 +00001418 mark_tlbs_dirty(ppgtt);
Chris Wilsondd196742017-02-15 08:43:46 +00001419 wmb();
Ben Widawsky3e302542013-04-23 23:15:32 -07001420}
1421
Chris Wilsondd196742017-02-15 08:43:46 +00001422static inline uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001423{
Chris Wilsondd196742017-02-15 08:43:46 +00001424 GEM_BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1425 return ppgtt->pd.base.ggtt_offset << 10;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001426}
Ben Widawsky61973492013-04-08 18:43:54 -07001427
Ben Widawsky90252e52013-12-06 14:11:12 -08001428static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001429 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001430{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001431 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001432 u32 *cs;
Ben Widawsky90252e52013-12-06 14:11:12 -08001433 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001434
Ben Widawsky90252e52013-12-06 14:11:12 -08001435 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001436 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001437 if (ret)
1438 return ret;
1439
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001440 cs = intel_ring_begin(req, 6);
1441 if (IS_ERR(cs))
1442 return PTR_ERR(cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001443
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001444 *cs++ = MI_LOAD_REGISTER_IMM(2);
1445 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1446 *cs++ = PP_DIR_DCLV_2G;
1447 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1448 *cs++ = get_pd_offset(ppgtt);
1449 *cs++ = MI_NOOP;
1450 intel_ring_advance(req, cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001451
1452 return 0;
1453}
1454
Ben Widawsky48a10382013-12-06 14:11:11 -08001455static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001456 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001457{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001458 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001459 u32 *cs;
Ben Widawsky48a10382013-12-06 14:11:11 -08001460 int ret;
1461
Ben Widawsky48a10382013-12-06 14:11:11 -08001462 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001463 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001464 if (ret)
1465 return ret;
1466
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001467 cs = intel_ring_begin(req, 6);
1468 if (IS_ERR(cs))
1469 return PTR_ERR(cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001470
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001471 *cs++ = MI_LOAD_REGISTER_IMM(2);
1472 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1473 *cs++ = PP_DIR_DCLV_2G;
1474 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1475 *cs++ = get_pd_offset(ppgtt);
1476 *cs++ = MI_NOOP;
1477 intel_ring_advance(req, cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001478
Ben Widawsky90252e52013-12-06 14:11:12 -08001479 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001480 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001481 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001482 if (ret)
1483 return ret;
1484 }
1485
Ben Widawsky48a10382013-12-06 14:11:11 -08001486 return 0;
1487}
1488
Ben Widawskyeeb94882013-12-06 14:11:10 -08001489static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001490 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001491{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001492 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001493 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001494
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001495 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1496 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001497 return 0;
1498}
1499
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001500static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001501{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001502 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301503 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001504
Akash Goel3b3f1652016-10-13 22:44:48 +05301505 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001506 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1507 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001508 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001509 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001510 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001511}
1512
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001513static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001514{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001515 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001516 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301517 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001518
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001519 ecobits = I915_READ(GAC_ECO_BITS);
1520 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1521
1522 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001523 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001524 ecochk |= ECOCHK_PPGTT_WB_HSW;
1525 } else {
1526 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1527 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1528 }
1529 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001530
Akash Goel3b3f1652016-10-13 22:44:48 +05301531 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001532 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001533 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001534 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001535 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001536}
1537
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001538static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001539{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001540 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001541
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001542 ecobits = I915_READ(GAC_ECO_BITS);
1543 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1544 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001545
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001546 gab_ctl = I915_READ(GAB_CTL);
1547 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001548
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001549 ecochk = I915_READ(GAM_ECOCHK);
1550 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001551
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001552 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001553}
1554
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001555/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001556static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Chris Wilsondd196742017-02-15 08:43:46 +00001557 u64 start, u64 length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001558{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001559 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilsondd196742017-02-15 08:43:46 +00001560 unsigned int first_entry = start >> PAGE_SHIFT;
1561 unsigned int pde = first_entry / GEN6_PTES;
1562 unsigned int pte = first_entry % GEN6_PTES;
1563 unsigned int num_entries = length >> PAGE_SHIFT;
1564 gen6_pte_t scratch_pte =
1565 vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001566
Daniel Vetter7bddb012012-02-09 17:15:47 +01001567 while (num_entries) {
Chris Wilsondd196742017-02-15 08:43:46 +00001568 struct i915_page_table *pt = ppgtt->pd.page_table[pde++];
1569 unsigned int end = min(pte + num_entries, GEN6_PTES);
1570 gen6_pte_t *vaddr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001571
Chris Wilsondd196742017-02-15 08:43:46 +00001572 num_entries -= end - pte;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001573
Chris Wilsondd196742017-02-15 08:43:46 +00001574 /* Note that the hw doesn't support removing PDE on the fly
1575 * (they are cached inside the context with no means to
1576 * invalidate the cache), so we can only reset the PTE
1577 * entries back to scratch.
1578 */
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001579
Chris Wilsondd196742017-02-15 08:43:46 +00001580 vaddr = kmap_atomic_px(pt);
1581 do {
1582 vaddr[pte++] = scratch_pte;
1583 } while (pte < end);
1584 kunmap_atomic(vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001585
Chris Wilsondd196742017-02-15 08:43:46 +00001586 pte = 0;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001587 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001588}
1589
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001590static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001591 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001592 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301593 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001594{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001595 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001596 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001597 unsigned act_pt = first_entry / GEN6_PTES;
1598 unsigned act_pte = first_entry % GEN6_PTES;
Chris Wilsonb31144c2017-02-15 08:43:36 +00001599 const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1600 struct sgt_dma iter;
1601 gen6_pte_t *vaddr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001602
Chris Wilson9231da72017-02-15 08:43:41 +00001603 vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
Chris Wilsonb31144c2017-02-15 08:43:36 +00001604 iter.sg = pages->sgl;
1605 iter.dma = sg_dma_address(iter.sg);
1606 iter.max = iter.dma + iter.sg->length;
1607 do {
1608 vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001609
Chris Wilsonb31144c2017-02-15 08:43:36 +00001610 iter.dma += PAGE_SIZE;
1611 if (iter.dma == iter.max) {
1612 iter.sg = __sg_next(iter.sg);
1613 if (!iter.sg)
1614 break;
1615
1616 iter.dma = sg_dma_address(iter.sg);
1617 iter.max = iter.dma + iter.sg->length;
1618 }
Akash Goel24f3a8c2014-06-17 10:59:42 +05301619
Michel Thierry07749ef2015-03-16 16:00:54 +00001620 if (++act_pte == GEN6_PTES) {
Chris Wilson9231da72017-02-15 08:43:41 +00001621 kunmap_atomic(vaddr);
1622 vaddr = kmap_atomic_px(ppgtt->pd.page_table[++act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +02001623 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001624 }
Chris Wilsonb31144c2017-02-15 08:43:36 +00001625 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +00001626 kunmap_atomic(vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001627}
1628
Ben Widawsky678d96f2015-03-16 16:00:56 +00001629static int gen6_alloc_va_range(struct i915_address_space *vm,
Chris Wilsondd196742017-02-15 08:43:46 +00001630 u64 start, u64 length)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001631{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001632 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001633 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +00001634 u64 from = start;
1635 unsigned int pde;
1636 bool flush = false;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001637
Dave Gordon731f74c2016-06-24 19:37:46 +01001638 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Chris Wilsondd196742017-02-15 08:43:46 +00001639 if (pt == vm->scratch_pt) {
1640 pt = alloc_pt(vm);
1641 if (IS_ERR(pt))
1642 goto unwind_out;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001643
Chris Wilsondd196742017-02-15 08:43:46 +00001644 gen6_initialize_pt(vm, pt);
1645 ppgtt->pd.page_table[pde] = pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001646 gen6_write_pde(ppgtt, pde, pt);
Chris Wilsondd196742017-02-15 08:43:46 +00001647 flush = true;
1648 }
Ben Widawsky678d96f2015-03-16 16:00:56 +00001649 }
1650
Chris Wilsondd196742017-02-15 08:43:46 +00001651 if (flush) {
1652 mark_tlbs_dirty(ppgtt);
1653 wmb();
1654 }
Michel Thierry4933d512015-03-24 15:46:22 +00001655
Ben Widawsky678d96f2015-03-16 16:00:56 +00001656 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001657
1658unwind_out:
Chris Wilsondd196742017-02-15 08:43:46 +00001659 gen6_ppgtt_clear_range(vm, from, start);
1660 return -ENOMEM;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001661}
1662
Mika Kuoppala8776f022015-06-30 18:16:40 +03001663static int gen6_init_scratch(struct i915_address_space *vm)
1664{
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001665 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001666
Chris Wilson84486612017-02-15 08:43:40 +00001667 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001668 if (ret)
1669 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001670
Chris Wilson84486612017-02-15 08:43:40 +00001671 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001672 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson84486612017-02-15 08:43:40 +00001673 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001674 return PTR_ERR(vm->scratch_pt);
1675 }
1676
1677 gen6_initialize_pt(vm, vm->scratch_pt);
1678
1679 return 0;
1680}
1681
1682static void gen6_free_scratch(struct i915_address_space *vm)
1683{
Chris Wilson84486612017-02-15 08:43:40 +00001684 free_pt(vm, vm->scratch_pt);
1685 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001686}
1687
Daniel Vetter061dd492015-04-14 17:35:13 +02001688static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08001689{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001690 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01001691 struct i915_page_directory *pd = &ppgtt->pd;
Michel Thierry09942c62015-04-08 12:13:30 +01001692 struct i915_page_table *pt;
1693 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08001694
Daniel Vetter061dd492015-04-14 17:35:13 +02001695 drm_mm_remove_node(&ppgtt->node);
1696
Dave Gordon731f74c2016-06-24 19:37:46 +01001697 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001698 if (pt != vm->scratch_pt)
Chris Wilson84486612017-02-15 08:43:40 +00001699 free_pt(vm, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001700
Mika Kuoppala8776f022015-06-30 18:16:40 +03001701 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08001702}
1703
Ben Widawskyb1465202014-02-19 22:05:49 -08001704static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001705{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001706 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00001707 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001708 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08001709 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001710
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001711 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1712 * allocator works in address space sizes, so it's multiplied by page
1713 * size. We allocate at the top of the GTT to avoid fragmentation.
1714 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001715 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00001716
Mika Kuoppala8776f022015-06-30 18:16:40 +03001717 ret = gen6_init_scratch(vm);
1718 if (ret)
1719 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00001720
Chris Wilsone007b192017-01-11 11:23:10 +00001721 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
1722 GEN6_PD_SIZE, GEN6_PD_ALIGN,
1723 I915_COLOR_UNEVICTABLE,
1724 0, ggtt->base.total,
1725 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00001726 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001727 goto err_out;
1728
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001729 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001730 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001731
Chris Wilson52c126e2017-02-15 08:43:43 +00001732 ppgtt->pd.base.ggtt_offset =
1733 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
1734
1735 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
1736 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
1737
Ben Widawskyc8c26622015-01-22 17:01:25 +00001738 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001739
1740err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03001741 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001742 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08001743}
1744
Ben Widawskyb1465202014-02-19 22:05:49 -08001745static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1746{
kbuild test robot2f2cf682015-03-27 19:26:35 +08001747 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08001748}
1749
Michel Thierry4933d512015-03-24 15:46:22 +00001750static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
1751 uint64_t start, uint64_t length)
1752{
Michel Thierryec565b32015-04-08 12:13:23 +01001753 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01001754 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001755
Dave Gordon731f74c2016-06-24 19:37:46 +01001756 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001757 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00001758}
1759
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001760static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08001761{
Chris Wilson49d73912016-11-29 09:50:08 +00001762 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001763 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08001764 int ret;
1765
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001766 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001767 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08001768 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001769 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08001770 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001771 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08001772 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01001773 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001774 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001775
1776 ret = gen6_ppgtt_alloc(ppgtt);
1777 if (ret)
1778 return ret;
1779
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001780 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1781 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001782 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1783 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001784 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Michel Thierry09942c62015-04-08 12:13:30 +01001785 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001786 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001787
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001788 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Chris Wilson16a011c2017-02-15 08:43:45 +00001789 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001790
Chris Wilson52c126e2017-02-15 08:43:43 +00001791 ret = gen6_alloc_va_range(&ppgtt->base, 0, ppgtt->base.total);
1792 if (ret) {
1793 gen6_ppgtt_cleanup(&ppgtt->base);
1794 return ret;
1795 }
1796
Thierry Reding440fd522015-01-23 09:05:06 +01001797 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001798 ppgtt->node.size >> 20,
1799 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001800
Chris Wilson52c126e2017-02-15 08:43:43 +00001801 DRM_DEBUG_DRIVER("Adding PPGTT at offset %x\n",
1802 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001803
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001804 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001805}
1806
Chris Wilson2bfa9962016-08-04 07:52:25 +01001807static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
1808 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08001809{
Chris Wilson49d73912016-11-29 09:50:08 +00001810 ppgtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00001811 ppgtt->base.dma = &dev_priv->drm.pdev->dev;
Daniel Vetter3440d262013-01-24 13:49:56 -08001812
Chris Wilson2bfa9962016-08-04 07:52:25 +01001813 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001814 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001815 else
Michel Thierryd7b26332015-04-08 12:13:34 +01001816 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001817}
Mika Kuoppalac114f762015-06-25 18:35:13 +03001818
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001819static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01001820 struct drm_i915_private *dev_priv,
1821 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001822{
Chris Wilson80b204b2016-10-28 13:58:58 +01001823 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00001824
Chris Wilson381b9432017-02-15 08:43:54 +00001825 drm_mm_init(&vm->mm, 0, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00001826 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
1827
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001828 INIT_LIST_HEAD(&vm->active_list);
1829 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01001830 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00001831
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001832 list_add_tail(&vm->global_link, &dev_priv->vm_list);
Chris Wilson84486612017-02-15 08:43:40 +00001833 pagevec_init(&vm->free_pages, false);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001834}
1835
Matthew Aulded9724d2016-11-17 21:04:10 +00001836static void i915_address_space_fini(struct i915_address_space *vm)
1837{
Chris Wilson84486612017-02-15 08:43:40 +00001838 if (pagevec_count(&vm->free_pages))
1839 vm_free_pages_release(vm);
1840
Matthew Aulded9724d2016-11-17 21:04:10 +00001841 i915_gem_timeline_fini(&vm->timeline);
1842 drm_mm_takedown(&vm->mm);
1843 list_del(&vm->global_link);
1844}
1845
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001846static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00001847{
Tim Gored5165eb2016-02-04 11:49:34 +00001848 /* This function is for gtt related workarounds. This function is
1849 * called on driver load and after a GPU reset, so you can place
1850 * workarounds here even if they get overwritten by GPU reset.
1851 */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001852 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01001853 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001854 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01001855 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001856 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08001857 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001858 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001859 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001860 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
1861}
1862
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001863int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02001864{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001865 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00001866
Thomas Daniel671b50132014-08-20 16:24:50 +01001867 /* In the case of execlists, PPGTT is enabled by the context descriptor
1868 * and the PDPs are contained within the context itself. We don't
1869 * need to do anything here. */
1870 if (i915.enable_execlists)
1871 return 0;
1872
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001873 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02001874 return 0;
1875
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001876 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001877 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001878 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001879 gen7_ppgtt_enable(dev_priv);
1880 else if (INTEL_GEN(dev_priv) >= 8)
1881 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02001882 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001883 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02001884
John Harrison4ad2fd82015-06-18 13:11:20 +01001885 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001886}
John Harrison4ad2fd82015-06-18 13:11:20 +01001887
Daniel Vetter4d884702014-08-06 15:04:47 +02001888struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01001889i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01001890 struct drm_i915_file_private *fpriv,
1891 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02001892{
1893 struct i915_hw_ppgtt *ppgtt;
1894 int ret;
1895
1896 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1897 if (!ppgtt)
1898 return ERR_PTR(-ENOMEM);
1899
Chris Wilson1188bc62017-02-15 08:43:38 +00001900 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetter4d884702014-08-06 15:04:47 +02001901 if (ret) {
1902 kfree(ppgtt);
1903 return ERR_PTR(ret);
1904 }
1905
Chris Wilson1188bc62017-02-15 08:43:38 +00001906 kref_init(&ppgtt->ref);
1907 i915_address_space_init(&ppgtt->base, dev_priv, name);
1908 ppgtt->base.file = fpriv;
1909
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001910 trace_i915_ppgtt_create(&ppgtt->base);
1911
Daniel Vetter4d884702014-08-06 15:04:47 +02001912 return ppgtt;
1913}
1914
Chris Wilson0c7eeda2017-01-11 21:09:25 +00001915void i915_ppgtt_close(struct i915_address_space *vm)
1916{
1917 struct list_head *phases[] = {
1918 &vm->active_list,
1919 &vm->inactive_list,
1920 &vm->unbound_list,
1921 NULL,
1922 }, **phase;
1923
1924 GEM_BUG_ON(vm->closed);
1925 vm->closed = true;
1926
1927 for (phase = phases; *phase; phase++) {
1928 struct i915_vma *vma, *vn;
1929
1930 list_for_each_entry_safe(vma, vn, *phase, vm_link)
1931 if (!i915_vma_is_closed(vma))
1932 i915_vma_close(vma);
1933 }
1934}
1935
Matthew Aulded9724d2016-11-17 21:04:10 +00001936void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02001937{
1938 struct i915_hw_ppgtt *ppgtt =
1939 container_of(kref, struct i915_hw_ppgtt, ref);
1940
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001941 trace_i915_ppgtt_release(&ppgtt->base);
1942
Chris Wilson50e046b2016-08-04 07:52:46 +01001943 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02001944 WARN_ON(!list_empty(&ppgtt->base.active_list));
1945 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01001946 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02001947
1948 ppgtt->base.cleanup(&ppgtt->base);
Chris Wilson84486612017-02-15 08:43:40 +00001949 i915_address_space_fini(&ppgtt->base);
Daniel Vetteree960be2014-08-06 15:04:45 +02001950 kfree(ppgtt);
1951}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001952
Ben Widawskya81cc002013-01-18 12:30:31 -08001953/* Certain Gen5 chipsets require require idling the GPU before
1954 * unmapping anything from the GTT when VT-d is enabled.
1955 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001956static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08001957{
1958#ifdef CONFIG_INTEL_IOMMU
1959 /* Query intel_iommu to see if we need the workaround. Presumably that
1960 * was loaded first.
1961 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001962 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08001963 return true;
1964#endif
1965 return false;
1966}
1967
Chris Wilsondc979972016-05-10 14:10:04 +01001968void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07001969{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001970 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301971 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07001972
Chris Wilsondc979972016-05-10 14:10:04 +01001973 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07001974 return;
1975
Akash Goel3b3f1652016-10-13 22:44:48 +05301976 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07001977 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001978 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07001979 if (fault_reg & RING_FAULT_VALID) {
1980 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02001981 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07001982 "\tAddress space: %s\n"
1983 "\tSource ID: %d\n"
1984 "\tType: %d\n",
1985 fault_reg & PAGE_MASK,
1986 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1987 RING_FAULT_SRCID(fault_reg),
1988 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001989 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07001990 fault_reg & ~RING_FAULT_VALID);
1991 }
1992 }
Akash Goel3b3f1652016-10-13 22:44:48 +05301993
1994 /* Engine specific init may not have been done till this point. */
1995 if (dev_priv->engine[RCS])
1996 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07001997}
1998
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001999void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002000{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002001 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002002
2003 /* Don't bother messing with faults pre GEN6 as we have little
2004 * documentation supporting that it's a good idea.
2005 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002006 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002007 return;
2008
Chris Wilsondc979972016-05-10 14:10:04 +01002009 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002010
Chris Wilson381b9432017-02-15 08:43:54 +00002011 ggtt->base.clear_range(&ggtt->base, 0, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002012
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002013 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002014}
2015
Chris Wilson03ac84f2016-10-28 13:58:36 +01002016int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2017 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002018{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002019 do {
2020 if (dma_map_sg(&obj->base.dev->pdev->dev,
2021 pages->sgl, pages->nents,
2022 PCI_DMA_BIDIRECTIONAL))
2023 return 0;
2024
2025 /* If the DMA remap fails, one cause can be that we have
2026 * too many objects pinned in a small remapping table,
2027 * such as swiotlb. Incrementally purge all other objects and
2028 * try again - if there are no more pages to remove from
2029 * the DMA remapper, i915_gem_shrink will return 0.
2030 */
2031 GEM_BUG_ON(obj->mm.pages == pages);
2032 } while (i915_gem_shrink(to_i915(obj->base.dev),
2033 obj->base.size >> PAGE_SHIFT,
2034 I915_SHRINK_BOUND |
2035 I915_SHRINK_UNBOUND |
2036 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002037
Chris Wilson03ac84f2016-10-28 13:58:36 +01002038 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002039}
2040
Daniel Vetter2c642b02015-04-14 17:35:26 +02002041static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002042{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002043 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002044}
2045
Chris Wilsond6473f52016-06-10 14:22:59 +05302046static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2047 dma_addr_t addr,
2048 uint64_t offset,
2049 enum i915_cache_level level,
2050 u32 unused)
2051{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002052 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302053 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002054 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302055
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002056 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302057
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002058 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302059}
2060
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002061static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2062 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002063 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302064 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002065{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002066 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002067 struct sgt_iter sgt_iter;
2068 gen8_pte_t __iomem *gtt_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +00002069 const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002070 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002071
Chris Wilson894cceb2017-02-15 08:43:37 +00002072 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
2073 gtt_entries += start >> PAGE_SHIFT;
2074 for_each_sgt_dma(addr, sgt_iter, st)
2075 gen8_set_pte(gtt_entries++, pte_encode | addr);
Dave Gordon85d12252016-05-20 11:54:06 +01002076
Chris Wilson894cceb2017-02-15 08:43:37 +00002077 wmb();
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002078
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002079 /* This next bit makes the above posting read even more important. We
2080 * want to flush the TLBs only after we're certain all the PTE updates
2081 * have finished.
2082 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002083 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002084}
2085
Chris Wilsonc1403302015-11-18 15:19:39 +00002086struct insert_entries {
2087 struct i915_address_space *vm;
2088 struct sg_table *st;
2089 uint64_t start;
2090 enum i915_cache_level level;
2091 u32 flags;
2092};
2093
2094static int gen8_ggtt_insert_entries__cb(void *_arg)
2095{
2096 struct insert_entries *arg = _arg;
2097 gen8_ggtt_insert_entries(arg->vm, arg->st,
2098 arg->start, arg->level, arg->flags);
2099 return 0;
2100}
2101
2102static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2103 struct sg_table *st,
2104 uint64_t start,
2105 enum i915_cache_level level,
2106 u32 flags)
2107{
2108 struct insert_entries arg = { vm, st, start, level, flags };
2109 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2110}
2111
Chris Wilsond6473f52016-06-10 14:22:59 +05302112static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2113 dma_addr_t addr,
2114 uint64_t offset,
2115 enum i915_cache_level level,
2116 u32 flags)
2117{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002118 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302119 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002120 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302121
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002122 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302123
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002124 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302125}
2126
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002127/*
2128 * Binds an object into the global gtt with the specified cache level. The object
2129 * will be accessible to the GPU via commands whose operands reference offsets
2130 * within the global GTT as well as accessible by the GPU through the GMADR
2131 * mapped BAR (dev_priv->mm.gtt->gtt).
2132 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002133static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002134 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002135 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302136 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002137{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002138 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsonb31144c2017-02-15 08:43:36 +00002139 gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
2140 unsigned int i = start >> PAGE_SHIFT;
2141 struct sgt_iter iter;
Dave Gordon85d12252016-05-20 11:54:06 +01002142 dma_addr_t addr;
Chris Wilsonb31144c2017-02-15 08:43:36 +00002143 for_each_sgt_dma(addr, iter, st)
2144 iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2145 wmb();
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002146
2147 /* This next bit makes the above posting read even more important. We
2148 * want to flush the TLBs only after we're certain all the PTE updates
2149 * have finished.
2150 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002151 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002152}
2153
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002154static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002155 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002156{
2157}
2158
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002159static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002160 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002161{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002162 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002163 unsigned first_entry = start >> PAGE_SHIFT;
2164 unsigned num_entries = length >> PAGE_SHIFT;
Chris Wilson894cceb2017-02-15 08:43:37 +00002165 const gen8_pte_t scratch_pte =
2166 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
2167 gen8_pte_t __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002168 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2169 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002170 int i;
2171
2172 if (WARN(num_entries > max_entries,
2173 "First entry = %d; Num entries = %d (max=%d)\n",
2174 first_entry, num_entries, max_entries))
2175 num_entries = max_entries;
2176
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002177 for (i = 0; i < num_entries; i++)
2178 gen8_set_pte(&gtt_base[i], scratch_pte);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002179}
2180
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002181static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002182 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002183 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002184{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002185 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002186 unsigned first_entry = start >> PAGE_SHIFT;
2187 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002188 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002189 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2190 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002191 int i;
2192
2193 if (WARN(num_entries > max_entries,
2194 "First entry = %d; Num entries = %d (max=%d)\n",
2195 first_entry, num_entries, max_entries))
2196 num_entries = max_entries;
2197
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002198 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002199 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002200
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002201 for (i = 0; i < num_entries; i++)
2202 iowrite32(scratch_pte, &gtt_base[i]);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002203}
2204
Chris Wilsond6473f52016-06-10 14:22:59 +05302205static void i915_ggtt_insert_page(struct i915_address_space *vm,
2206 dma_addr_t addr,
2207 uint64_t offset,
2208 enum i915_cache_level cache_level,
2209 u32 unused)
2210{
Chris Wilsond6473f52016-06-10 14:22:59 +05302211 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2212 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302213
2214 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302215}
2216
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002217static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2218 struct sg_table *pages,
2219 uint64_t start,
2220 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002221{
2222 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2223 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2224
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002225 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002226}
2227
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002228static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002229 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002230 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002231{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002232 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002233}
2234
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002235static int ggtt_bind_vma(struct i915_vma *vma,
2236 enum i915_cache_level cache_level,
2237 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002238{
Chris Wilson49d73912016-11-29 09:50:08 +00002239 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002240 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonba7a5742017-02-15 08:43:35 +00002241 u32 pte_flags;
Daniel Vetter0a878712015-10-15 14:23:01 +02002242
Chris Wilsonba7a5742017-02-15 08:43:35 +00002243 if (unlikely(!vma->pages)) {
2244 int ret = i915_get_ggtt_vma_pages(vma);
2245 if (ret)
2246 return ret;
2247 }
Daniel Vetter0a878712015-10-15 14:23:01 +02002248
2249 /* Currently applicable only to VLV */
Chris Wilsonba7a5742017-02-15 08:43:35 +00002250 pte_flags = 0;
Daniel Vetter0a878712015-10-15 14:23:01 +02002251 if (obj->gt_ro)
2252 pte_flags |= PTE_READ_ONLY;
2253
Chris Wilson9c870d02016-10-24 13:42:15 +01002254 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002255 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002256 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002257 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002258
2259 /*
2260 * Without aliasing PPGTT there's no difference between
2261 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2262 * upgrade to both bound if we bind either to avoid double-binding.
2263 */
Chris Wilson3272db52016-08-04 16:32:32 +01002264 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002265
2266 return 0;
2267}
2268
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002269static void ggtt_unbind_vma(struct i915_vma *vma)
2270{
2271 struct drm_i915_private *i915 = vma->vm->i915;
2272
2273 intel_runtime_pm_get(i915);
2274 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2275 intel_runtime_pm_put(i915);
2276}
2277
Daniel Vetter0a878712015-10-15 14:23:01 +02002278static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2279 enum i915_cache_level cache_level,
2280 u32 flags)
2281{
Chris Wilson49d73912016-11-29 09:50:08 +00002282 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002283 u32 pte_flags;
Chris Wilsonff685972017-02-15 08:43:42 +00002284 int ret;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002285
Chris Wilsonba7a5742017-02-15 08:43:35 +00002286 if (unlikely(!vma->pages)) {
Chris Wilsonff685972017-02-15 08:43:42 +00002287 ret = i915_get_ggtt_vma_pages(vma);
Chris Wilsonba7a5742017-02-15 08:43:35 +00002288 if (ret)
2289 return ret;
2290 }
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002291
Akash Goel24f3a8c2014-06-17 10:59:42 +05302292 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002293 pte_flags = 0;
2294 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002295 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302296
Chris Wilsonff685972017-02-15 08:43:42 +00002297 if (flags & I915_VMA_LOCAL_BIND) {
2298 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
2299
2300 if (appgtt->base.allocate_va_range) {
2301 ret = appgtt->base.allocate_va_range(&appgtt->base,
2302 vma->node.start,
2303 vma->node.size);
2304 if (ret)
2305 return ret;
2306 }
2307
2308 appgtt->base.insert_entries(&appgtt->base,
2309 vma->pages, vma->node.start,
2310 cache_level, pte_flags);
2311 }
2312
Chris Wilson3272db52016-08-04 16:32:32 +01002313 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002314 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002315 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002316 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002317 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002318 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002319 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002320
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002321 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002322}
2323
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002324static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002325{
Chris Wilson49d73912016-11-29 09:50:08 +00002326 struct drm_i915_private *i915 = vma->vm->i915;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002327
Chris Wilson9c870d02016-10-24 13:42:15 +01002328 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2329 intel_runtime_pm_get(i915);
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002330 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002331 intel_runtime_pm_put(i915);
2332 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002333
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002334 if (vma->flags & I915_VMA_LOCAL_BIND) {
2335 struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
2336
2337 vm->clear_range(vm, vma->node.start, vma->size);
2338 }
Daniel Vetter74163902012-02-15 23:50:21 +01002339}
2340
Chris Wilson03ac84f2016-10-28 13:58:36 +01002341void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2342 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002343{
David Weinehall52a05c32016-08-22 13:32:44 +03002344 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2345 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002346 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002347
Chris Wilson307dc252016-08-05 10:14:12 +01002348 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002349 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002350 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2351 /* Wait a bit, in hopes it avoids the hang */
2352 udelay(10);
2353 }
2354 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002355
Chris Wilson03ac84f2016-10-28 13:58:36 +01002356 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002357}
Daniel Vetter644ec022012-03-26 09:45:40 +02002358
Chris Wilson45b186f2016-12-16 07:46:42 +00002359static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002360 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002361 u64 *start,
2362 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002363{
Chris Wilsona6508de2017-02-06 08:45:47 +00002364 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002365 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002366
Chris Wilsona6508de2017-02-06 08:45:47 +00002367 /* Also leave a space between the unallocated reserved node after the
2368 * GTT and any objects within the GTT, i.e. we use the color adjustment
2369 * to insert a guard page to prevent prefetches crossing over the
2370 * GTT boundary.
2371 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002372 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002373 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002374 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002375}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002376
Chris Wilson6cde9a02017-02-13 17:15:50 +00002377int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2378{
2379 struct i915_ggtt *ggtt = &i915->ggtt;
2380 struct i915_hw_ppgtt *ppgtt;
2381 int err;
2382
Chris Wilson57202f42017-02-15 08:43:56 +00002383 ppgtt = i915_ppgtt_create(i915, ERR_PTR(-EPERM), "[alias]");
Chris Wilson1188bc62017-02-15 08:43:38 +00002384 if (IS_ERR(ppgtt))
2385 return PTR_ERR(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002386
Chris Wilsone565ceb2017-02-15 08:43:55 +00002387 if (WARN_ON(ppgtt->base.total < ggtt->base.total)) {
2388 err = -ENODEV;
2389 goto err_ppgtt;
2390 }
2391
Chris Wilson6cde9a02017-02-13 17:15:50 +00002392 if (ppgtt->base.allocate_va_range) {
Chris Wilsone565ceb2017-02-15 08:43:55 +00002393 /* Note we only pre-allocate as far as the end of the global
2394 * GTT. On 48b / 4-level page-tables, the difference is very,
2395 * very significant! We have to preallocate as GVT/vgpu does
2396 * not like the page directory disappearing.
2397 */
Chris Wilson6cde9a02017-02-13 17:15:50 +00002398 err = ppgtt->base.allocate_va_range(&ppgtt->base,
Chris Wilsone565ceb2017-02-15 08:43:55 +00002399 0, ggtt->base.total);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002400 if (err)
Chris Wilson1188bc62017-02-15 08:43:38 +00002401 goto err_ppgtt;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002402 }
2403
Chris Wilson6cde9a02017-02-13 17:15:50 +00002404 i915->mm.aliasing_ppgtt = ppgtt;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002405
Chris Wilson6cde9a02017-02-13 17:15:50 +00002406 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2407 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2408
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002409 WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
2410 ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
2411
Chris Wilson6cde9a02017-02-13 17:15:50 +00002412 return 0;
2413
Chris Wilson6cde9a02017-02-13 17:15:50 +00002414err_ppgtt:
Chris Wilson1188bc62017-02-15 08:43:38 +00002415 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002416 return err;
2417}
2418
2419void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2420{
2421 struct i915_ggtt *ggtt = &i915->ggtt;
2422 struct i915_hw_ppgtt *ppgtt;
2423
2424 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2425 if (!ppgtt)
2426 return;
2427
Chris Wilson1188bc62017-02-15 08:43:38 +00002428 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002429
2430 ggtt->base.bind_vma = ggtt_bind_vma;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002431 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002432}
2433
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002434int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002435{
Ben Widawskye78891c2013-01-25 16:41:04 -08002436 /* Let GEM Manage all of the aperture.
2437 *
2438 * However, leave one page at the end still bound to the scratch page.
2439 * There are a number of places where the hardware apparently prefetches
2440 * past the end of the object, and we've seen multiple hangs with the
2441 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2442 * aperture. One page should be enough to keep any prefetching inside
2443 * of the aperture.
2444 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002445 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002446 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002447 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002448 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002449
Zhi Wangb02d22a2016-06-16 08:06:59 -04002450 ret = intel_vgt_balloon(dev_priv);
2451 if (ret)
2452 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002453
Chris Wilson95374d72016-10-12 10:05:20 +01002454 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002455 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2456 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2457 0, ggtt->mappable_end,
2458 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002459 if (ret)
2460 return ret;
2461
Chris Wilsoned2f3452012-11-15 11:32:19 +00002462 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002463 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002464 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2465 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002466 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002467 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002468 }
2469
2470 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002471 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002472 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002473
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002474 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Chris Wilson6cde9a02017-02-13 17:15:50 +00002475 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
Chris Wilson95374d72016-10-12 10:05:20 +01002476 if (ret)
Chris Wilson6cde9a02017-02-13 17:15:50 +00002477 goto err;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002478 }
2479
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002480 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002481
Chris Wilson95374d72016-10-12 10:05:20 +01002482err:
2483 drm_mm_remove_node(&ggtt->error_capture);
2484 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002485}
2486
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002487/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002488 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002489 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002490 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002491void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002492{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002493 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002494 struct i915_vma *vma, *vn;
2495
2496 ggtt->base.closed = true;
2497
2498 mutex_lock(&dev_priv->drm.struct_mutex);
2499 WARN_ON(!list_empty(&ggtt->base.active_list));
2500 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2501 WARN_ON(i915_vma_unbind(vma));
2502 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002503
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002504 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002505
Chris Wilson1188bc62017-02-15 08:43:38 +00002506 mutex_lock(&dev_priv->drm.struct_mutex);
2507 i915_gem_fini_aliasing_ppgtt(dev_priv);
2508
Chris Wilson95374d72016-10-12 10:05:20 +01002509 if (drm_mm_node_allocated(&ggtt->error_capture))
2510 drm_mm_remove_node(&ggtt->error_capture);
2511
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002512 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002513 intel_vgt_deballoon(dev_priv);
Matthew Aulded9724d2016-11-17 21:04:10 +00002514 i915_address_space_fini(&ggtt->base);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002515 }
2516
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002517 ggtt->base.cleanup(&ggtt->base);
Chris Wilson1188bc62017-02-15 08:43:38 +00002518 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002519
2520 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002521 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002522}
Daniel Vetter70e32542014-08-06 15:04:57 +02002523
Daniel Vetter2c642b02015-04-14 17:35:26 +02002524static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002525{
2526 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2527 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2528 return snb_gmch_ctl << 20;
2529}
2530
Daniel Vetter2c642b02015-04-14 17:35:26 +02002531static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002532{
2533 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2534 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2535 if (bdw_gmch_ctl)
2536 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002537
2538#ifdef CONFIG_X86_32
2539 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2540 if (bdw_gmch_ctl > 4)
2541 bdw_gmch_ctl = 4;
2542#endif
2543
Ben Widawsky9459d252013-11-03 16:53:55 -08002544 return bdw_gmch_ctl << 20;
2545}
2546
Daniel Vetter2c642b02015-04-14 17:35:26 +02002547static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002548{
2549 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2550 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2551
2552 if (gmch_ctrl)
2553 return 1 << (20 + gmch_ctrl);
2554
2555 return 0;
2556}
2557
Daniel Vetter2c642b02015-04-14 17:35:26 +02002558static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002559{
2560 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2561 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2562 return snb_gmch_ctl << 25; /* 32 MB units */
2563}
2564
Daniel Vetter2c642b02015-04-14 17:35:26 +02002565static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002566{
2567 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2568 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2569 return bdw_gmch_ctl << 25; /* 32 MB units */
2570}
2571
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002572static size_t chv_get_stolen_size(u16 gmch_ctrl)
2573{
2574 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2575 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2576
2577 /*
2578 * 0x0 to 0x10: 32MB increments starting at 0MB
2579 * 0x11 to 0x16: 4MB increments starting at 8MB
2580 * 0x17 to 0x1d: 4MB increments start at 36MB
2581 */
2582 if (gmch_ctrl < 0x11)
2583 return gmch_ctrl << 25;
2584 else if (gmch_ctrl < 0x17)
2585 return (gmch_ctrl - 0x11 + 2) << 22;
2586 else
2587 return (gmch_ctrl - 0x17 + 9) << 22;
2588}
2589
Damien Lespiau66375012014-01-09 18:02:46 +00002590static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2591{
2592 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2593 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2594
2595 if (gen9_gmch_ctl < 0xf0)
2596 return gen9_gmch_ctl << 25; /* 32 MB units */
2597 else
2598 /* 4MB increments starting at 0xf0 for 4MB */
2599 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2600}
2601
Chris Wilson34c998b2016-08-04 07:52:24 +01002602static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002603{
Chris Wilson49d73912016-11-29 09:50:08 +00002604 struct drm_i915_private *dev_priv = ggtt->base.i915;
2605 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002606 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002607 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002608
2609 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002610 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002611
Imre Deak2a073f892015-03-27 13:07:33 +02002612 /*
2613 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2614 * dropped. For WC mappings in general we have 64 byte burst writes
2615 * when the WC buffer is flushed, so we can't use it, but have to
2616 * resort to an uncached mapping. The WC issue is easily caught by the
2617 * readback check when writing GTT PTE entries.
2618 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002619 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002620 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002621 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002622 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002623 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002624 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002625 return -ENOMEM;
2626 }
2627
Chris Wilson84486612017-02-15 08:43:40 +00002628 ret = setup_scratch_page(&ggtt->base, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002629 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002630 DRM_ERROR("Scratch setup failed\n");
2631 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002632 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002633 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002634 }
2635
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002636 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002637}
2638
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002639/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2640 * bits. When using advanced contexts each context stores its own PAT, but
2641 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002642static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002643{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002644 uint64_t pat;
2645
2646 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2647 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2648 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2649 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2650 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2651 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2652 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2653 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2654
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002655 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002656 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2657 * so RTL will always use the value corresponding to
2658 * pat_sel = 000".
2659 * So let's disable cache for GGTT to avoid screen corruptions.
2660 * MOCS still can be used though.
2661 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2662 * before this patch, i.e. the same uncached + snooping access
2663 * like on gen6/7 seems to be in effect.
2664 * - So this just fixes blitter/render access. Again it looks
2665 * like it's not just uncached access, but uncached + snooping.
2666 * So we can still hold onto all our assumptions wrt cpu
2667 * clflushing on LLC machines.
2668 */
2669 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2670
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002671 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2672 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002673 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2674 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002675}
2676
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002677static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2678{
2679 uint64_t pat;
2680
2681 /*
2682 * Map WB on BDW to snooped on CHV.
2683 *
2684 * Only the snoop bit has meaning for CHV, the rest is
2685 * ignored.
2686 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002687 * The hardware will never snoop for certain types of accesses:
2688 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2689 * - PPGTT page tables
2690 * - some other special cycles
2691 *
2692 * As with BDW, we also need to consider the following for GT accesses:
2693 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2694 * so RTL will always use the value corresponding to
2695 * pat_sel = 000".
2696 * Which means we must set the snoop bit in PAT entry 0
2697 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002698 */
2699 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2700 GEN8_PPAT(1, 0) |
2701 GEN8_PPAT(2, 0) |
2702 GEN8_PPAT(3, 0) |
2703 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2704 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2705 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2706 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2707
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002708 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2709 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002710}
2711
Chris Wilson34c998b2016-08-04 07:52:24 +01002712static void gen6_gmch_remove(struct i915_address_space *vm)
2713{
2714 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2715
2716 iounmap(ggtt->gsm);
Chris Wilson84486612017-02-15 08:43:40 +00002717 cleanup_scratch_page(vm);
Chris Wilson34c998b2016-08-04 07:52:24 +01002718}
2719
Joonas Lahtinend507d732016-03-18 10:42:58 +02002720static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08002721{
Chris Wilson49d73912016-11-29 09:50:08 +00002722 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002723 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002724 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08002725 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08002726
2727 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002728 ggtt->mappable_base = pci_resource_start(pdev, 2);
2729 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08002730
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002731 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
2732 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08002733
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002734 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08002735
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002736 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002737 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002738 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002739 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002740 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002741 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002742 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002743 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002744 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002745 }
Ben Widawsky63340132013-11-04 19:32:22 -08002746
Chris Wilson34c998b2016-08-04 07:52:24 +01002747 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002748
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002749 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002750 chv_setup_private_ppat(dev_priv);
2751 else
2752 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002753
Chris Wilson34c998b2016-08-04 07:52:24 +01002754 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002755 ggtt->base.bind_vma = ggtt_bind_vma;
2756 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05302757 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002758 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01002759 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002760 ggtt->base.clear_range = gen8_ggtt_clear_range;
2761
2762 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
2763 if (IS_CHERRYVIEW(dev_priv))
2764 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
2765
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002766 ggtt->invalidate = gen6_ggtt_invalidate;
2767
Chris Wilson34c998b2016-08-04 07:52:24 +01002768 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08002769}
2770
Joonas Lahtinend507d732016-03-18 10:42:58 +02002771static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002772{
Chris Wilson49d73912016-11-29 09:50:08 +00002773 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002774 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002775 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002776 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002777
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002778 ggtt->mappable_base = pci_resource_start(pdev, 2);
2779 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08002780
Ben Widawskybaa09f52013-01-24 13:49:57 -08002781 /* 64/512MB is the current min/max we actually know of, but this is just
2782 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002783 */
Chris Wilson34c998b2016-08-04 07:52:24 +01002784 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002785 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002786 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002787 }
2788
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002789 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
2790 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
2791 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002792
Joonas Lahtinend507d732016-03-18 10:42:58 +02002793 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002794
Chris Wilson34c998b2016-08-04 07:52:24 +01002795 size = gen6_get_total_gtt_size(snb_gmch_ctl);
2796 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002797
Joonas Lahtinend507d732016-03-18 10:42:58 +02002798 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05302799 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002800 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
2801 ggtt->base.bind_vma = ggtt_bind_vma;
2802 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01002803 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002804
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002805 ggtt->invalidate = gen6_ggtt_invalidate;
2806
Chris Wilson34c998b2016-08-04 07:52:24 +01002807 if (HAS_EDRAM(dev_priv))
2808 ggtt->base.pte_encode = iris_pte_encode;
2809 else if (IS_HASWELL(dev_priv))
2810 ggtt->base.pte_encode = hsw_pte_encode;
2811 else if (IS_VALLEYVIEW(dev_priv))
2812 ggtt->base.pte_encode = byt_pte_encode;
2813 else if (INTEL_GEN(dev_priv) >= 7)
2814 ggtt->base.pte_encode = ivb_pte_encode;
2815 else
2816 ggtt->base.pte_encode = snb_pte_encode;
2817
2818 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002819}
2820
Chris Wilson34c998b2016-08-04 07:52:24 +01002821static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002822{
Chris Wilson34c998b2016-08-04 07:52:24 +01002823 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08002824}
2825
Joonas Lahtinend507d732016-03-18 10:42:58 +02002826static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002827{
Chris Wilson49d73912016-11-29 09:50:08 +00002828 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002829 int ret;
2830
Chris Wilson91c8a322016-07-05 10:40:23 +01002831 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002832 if (!ret) {
2833 DRM_ERROR("failed to set up gmch\n");
2834 return -EIO;
2835 }
2836
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00002837 intel_gtt_get(&ggtt->base.total,
2838 &ggtt->stolen_size,
2839 &ggtt->mappable_base,
2840 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002841
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002842 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05302843 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002844 ggtt->base.insert_entries = i915_ggtt_insert_entries;
2845 ggtt->base.clear_range = i915_ggtt_clear_range;
2846 ggtt->base.bind_vma = ggtt_bind_vma;
2847 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01002848 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002849
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002850 ggtt->invalidate = gmch_ggtt_invalidate;
2851
Joonas Lahtinend507d732016-03-18 10:42:58 +02002852 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002853 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2854
Ben Widawskybaa09f52013-01-24 13:49:57 -08002855 return 0;
2856}
2857
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002858/**
Chris Wilson0088e522016-08-04 07:52:21 +01002859 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002860 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002861 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002862int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002863{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002864 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002865 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002866
Chris Wilson49d73912016-11-29 09:50:08 +00002867 ggtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00002868 ggtt->base.dma = &dev_priv->drm.pdev->dev;
Mika Kuoppalac114f762015-06-25 18:35:13 +03002869
Chris Wilson34c998b2016-08-04 07:52:24 +01002870 if (INTEL_GEN(dev_priv) <= 5)
2871 ret = i915_gmch_probe(ggtt);
2872 else if (INTEL_GEN(dev_priv) < 8)
2873 ret = gen6_gmch_probe(ggtt);
2874 else
2875 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002876 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002877 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002878
Chris Wilsondb9309a2017-01-05 15:30:23 +00002879 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
2880 * This is easier than doing range restriction on the fly, as we
2881 * currently don't have any bits spare to pass in this upper
2882 * restriction!
2883 */
2884 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
2885 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
2886 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
2887 }
2888
Chris Wilsonc890e2d2016-03-18 10:42:59 +02002889 if ((ggtt->base.total - 1) >> 32) {
2890 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002891 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02002892 ggtt->base.total >> 20);
2893 ggtt->base.total = 1ULL << 32;
2894 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
2895 }
2896
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002897 if (ggtt->mappable_end > ggtt->base.total) {
2898 DRM_ERROR("mappable aperture extends past end of GGTT,"
2899 " aperture=%llx, total=%llx\n",
2900 ggtt->mappable_end, ggtt->base.total);
2901 ggtt->mappable_end = ggtt->base.total;
2902 }
2903
Ben Widawskybaa09f52013-01-24 13:49:57 -08002904 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002905 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002906 ggtt->base.total >> 20);
2907 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00002908 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002909#ifdef CONFIG_INTEL_IOMMU
2910 if (intel_iommu_gfx_mapped)
2911 DRM_INFO("VT-d active for gfx access\n");
2912#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002913
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002914 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01002915}
2916
2917/**
2918 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002919 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01002920 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002921int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01002922{
Chris Wilson0088e522016-08-04 07:52:21 +01002923 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2924 int ret;
2925
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002926 INIT_LIST_HEAD(&dev_priv->vm_list);
2927
Chris Wilsona6508de2017-02-06 08:45:47 +00002928 /* Note that we use page colouring to enforce a guard page at the
2929 * end of the address space. This is required as the CS may prefetch
2930 * beyond the end of the batch buffer, across the page boundary,
2931 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002932 */
Chris Wilson80b204b2016-10-28 13:58:58 +01002933 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01002934 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00002935 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002936 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01002937 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002938
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002939 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
2940 dev_priv->ggtt.mappable_base,
2941 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002942 ret = -EIO;
2943 goto out_gtt_cleanup;
2944 }
2945
2946 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
2947
Chris Wilson0088e522016-08-04 07:52:21 +01002948 /*
2949 * Initialise stolen early so that we may reserve preallocated
2950 * objects for the BIOS to KMS transition.
2951 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00002952 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01002953 if (ret)
2954 goto out_gtt_cleanup;
2955
2956 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02002957
2958out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002959 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02002960 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002961}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002962
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002963int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03002964{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002965 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03002966 return -EIO;
2967
2968 return 0;
2969}
2970
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002971void i915_ggtt_enable_guc(struct drm_i915_private *i915)
2972{
2973 i915->ggtt.invalidate = guc_ggtt_invalidate;
2974}
2975
2976void i915_ggtt_disable_guc(struct drm_i915_private *i915)
2977{
2978 i915->ggtt.invalidate = gen6_ggtt_invalidate;
2979}
2980
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002981void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02002982{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002983 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002984 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02002985
Chris Wilsondc979972016-05-10 14:10:04 +01002986 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02002987
2988 /* First fill our portion of the GTT with scratch pages */
Chris Wilson381b9432017-02-15 08:43:54 +00002989 ggtt->base.clear_range(&ggtt->base, 0, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02002990
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002991 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
2992
2993 /* clflush objects bound into the GGTT and rebind them. */
2994 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02002995 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002996 bool ggtt_bound = false;
2997 struct i915_vma *vma;
2998
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002999 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003000 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003001 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003002
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003003 if (!i915_vma_unbind(vma))
3004 continue;
3005
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003006 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3007 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003008 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003009 }
3010
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003011 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003012 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003013 }
3014
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003015 ggtt->base.closed = false;
3016
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003017 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003018 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003019 chv_setup_private_ppat(dev_priv);
3020 else
3021 bdw_setup_private_ppat(dev_priv);
3022
3023 return;
3024 }
3025
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003026 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003027 struct i915_address_space *vm;
3028
Daniel Vetterfa423312015-04-14 17:35:23 +02003029 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003030 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003031
Chris Wilson2bfa9962016-08-04 07:52:25 +01003032 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003033 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003034 else
3035 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003036
Chris Wilson16a011c2017-02-15 08:43:45 +00003037 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003038 }
3039 }
3040
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003041 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003042}
3043
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003044static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003045rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003046 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003047 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003048 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003049{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003050 unsigned int column, row;
3051 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003052
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003053 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003054 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003055 for (row = 0; row < height; row++) {
3056 st->nents++;
3057 /* We don't need the pages, but need to initialize
3058 * the entries so the sg list can be happily traversed.
3059 * The only thing we need are DMA addresses.
3060 */
3061 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003062 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003063 sg_dma_len(sg) = PAGE_SIZE;
3064 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003065 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003066 }
3067 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003068
3069 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003070}
3071
Chris Wilsonba7a5742017-02-15 08:43:35 +00003072static noinline struct sg_table *
3073intel_rotate_pages(struct intel_rotation_info *rot_info,
3074 struct drm_i915_gem_object *obj)
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003075{
Dave Gordon85d12252016-05-20 11:54:06 +01003076 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003077 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003078 struct sgt_iter sgt_iter;
3079 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003080 unsigned long i;
3081 dma_addr_t *page_addr_list;
3082 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003083 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003084 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003085
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003086 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003087 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003088 sizeof(dma_addr_t),
3089 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003090 if (!page_addr_list)
3091 return ERR_PTR(ret);
3092
3093 /* Allocate target SG list. */
3094 st = kmalloc(sizeof(*st), GFP_KERNEL);
3095 if (!st)
3096 goto err_st_alloc;
3097
Ville Syrjälä6687c902015-09-15 13:16:41 +03003098 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003099 if (ret)
3100 goto err_sg_alloc;
3101
3102 /* Populate source page list from the object. */
3103 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003104 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003105 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003106
Dave Gordon85d12252016-05-20 11:54:06 +01003107 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003108 st->nents = 0;
3109 sg = st->sgl;
3110
Ville Syrjälä6687c902015-09-15 13:16:41 +03003111 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3112 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3113 rot_info->plane[i].width, rot_info->plane[i].height,
3114 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003115 }
3116
Ville Syrjälä6687c902015-09-15 13:16:41 +03003117 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3118 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003119
3120 drm_free_large(page_addr_list);
3121
3122 return st;
3123
3124err_sg_alloc:
3125 kfree(st);
3126err_st_alloc:
3127 drm_free_large(page_addr_list);
3128
Ville Syrjälä6687c902015-09-15 13:16:41 +03003129 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3130 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3131
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003132 return ERR_PTR(ret);
3133}
3134
Chris Wilsonba7a5742017-02-15 08:43:35 +00003135static noinline struct sg_table *
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003136intel_partial_pages(const struct i915_ggtt_view *view,
3137 struct drm_i915_gem_object *obj)
3138{
3139 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003140 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003141 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003142 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003143 int ret = -ENOMEM;
3144
3145 st = kmalloc(sizeof(*st), GFP_KERNEL);
3146 if (!st)
3147 goto err_st_alloc;
3148
Chris Wilsond2a84a72016-10-28 13:58:34 +01003149 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003150 if (ret)
3151 goto err_sg_alloc;
3152
Chris Wilson8bab11932017-01-14 00:28:25 +00003153 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003154 GEM_BUG_ON(!iter);
3155
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003156 sg = st->sgl;
3157 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003158 do {
3159 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003160
Chris Wilsond2a84a72016-10-28 13:58:34 +01003161 len = min(iter->length - (offset << PAGE_SHIFT),
3162 count << PAGE_SHIFT);
3163 sg_set_page(sg, NULL, len, 0);
3164 sg_dma_address(sg) =
3165 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3166 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003167
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003168 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003169 count -= len >> PAGE_SHIFT;
3170 if (count == 0) {
3171 sg_mark_end(sg);
3172 return st;
3173 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003174
Chris Wilsond2a84a72016-10-28 13:58:34 +01003175 sg = __sg_next(sg);
3176 iter = __sg_next(iter);
3177 offset = 0;
3178 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003179
3180err_sg_alloc:
3181 kfree(st);
3182err_st_alloc:
3183 return ERR_PTR(ret);
3184}
3185
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003186static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003187i915_get_ggtt_vma_pages(struct i915_vma *vma)
3188{
Chris Wilsonba7a5742017-02-15 08:43:35 +00003189 int ret;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003190
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003191 /* The vma->pages are only valid within the lifespan of the borrowed
3192 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3193 * must be the vma->pages. A simple rule is that vma->pages must only
3194 * be accessed when the obj->mm.pages are pinned.
3195 */
3196 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3197
Chris Wilsonba7a5742017-02-15 08:43:35 +00003198 switch (vma->ggtt_view.type) {
3199 case I915_GGTT_VIEW_NORMAL:
3200 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003201 return 0;
3202
Chris Wilsonba7a5742017-02-15 08:43:35 +00003203 case I915_GGTT_VIEW_ROTATED:
Chris Wilson247177d2016-08-15 10:48:47 +01003204 vma->pages =
Chris Wilsonba7a5742017-02-15 08:43:35 +00003205 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3206 break;
3207
3208 case I915_GGTT_VIEW_PARTIAL:
Chris Wilson247177d2016-08-15 10:48:47 +01003209 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003210 break;
3211
3212 default:
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003213 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3214 vma->ggtt_view.type);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003215 return -EINVAL;
3216 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003217
Chris Wilsonba7a5742017-02-15 08:43:35 +00003218 ret = 0;
3219 if (unlikely(IS_ERR(vma->pages))) {
Chris Wilson247177d2016-08-15 10:48:47 +01003220 ret = PTR_ERR(vma->pages);
3221 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003222 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3223 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003224 }
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003225 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003226}
3227
Chris Wilsone007b192017-01-11 11:23:10 +00003228/**
Chris Wilson625d9882017-01-11 11:23:11 +00003229 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003230 * @vm: the &struct i915_address_space
3231 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3232 * @size: how much space to allocate inside the GTT,
3233 * must be #I915_GTT_PAGE_SIZE aligned
3234 * @offset: where to insert inside the GTT,
3235 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3236 * (@offset + @size) must fit within the address space
3237 * @color: color to apply to node, if this node is not from a VMA,
3238 * color must be #I915_COLOR_UNEVICTABLE
3239 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003240 *
3241 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3242 * the address space (using @size and @color). If the @node does not fit, it
3243 * tries to evict any overlapping nodes from the GTT, including any
3244 * neighbouring nodes if the colors do not match (to ensure guard pages between
3245 * differing domains). See i915_gem_evict_for_node() for the gory details
3246 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3247 * evicting active overlapping objects, and any overlapping node that is pinned
3248 * or marked as unevictable will also result in failure.
3249 *
3250 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3251 * asked to wait for eviction and interrupted.
3252 */
3253int i915_gem_gtt_reserve(struct i915_address_space *vm,
3254 struct drm_mm_node *node,
3255 u64 size, u64 offset, unsigned long color,
3256 unsigned int flags)
3257{
3258 int err;
3259
3260 GEM_BUG_ON(!size);
3261 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3262 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3263 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003264 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003265 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003266
3267 node->size = size;
3268 node->start = offset;
3269 node->color = color;
3270
3271 err = drm_mm_reserve_node(&vm->mm, node);
3272 if (err != -ENOSPC)
3273 return err;
3274
3275 err = i915_gem_evict_for_node(vm, node, flags);
3276 if (err == 0)
3277 err = drm_mm_reserve_node(&vm->mm, node);
3278
3279 return err;
3280}
3281
Chris Wilson606fec92017-01-11 11:23:12 +00003282static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3283{
3284 u64 range, addr;
3285
3286 GEM_BUG_ON(range_overflows(start, len, end));
3287 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3288
3289 range = round_down(end - len, align) - round_up(start, align);
3290 if (range) {
3291 if (sizeof(unsigned long) == sizeof(u64)) {
3292 addr = get_random_long();
3293 } else {
3294 addr = get_random_int();
3295 if (range > U32_MAX) {
3296 addr <<= 32;
3297 addr |= get_random_int();
3298 }
3299 }
3300 div64_u64_rem(addr, range, &addr);
3301 start += addr;
3302 }
3303
3304 return round_up(start, align);
3305}
3306
Chris Wilson625d9882017-01-11 11:23:11 +00003307/**
Chris Wilsone007b192017-01-11 11:23:10 +00003308 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003309 * @vm: the &struct i915_address_space
3310 * @node: the &struct drm_mm_node (typically i915_vma.node)
3311 * @size: how much space to allocate inside the GTT,
3312 * must be #I915_GTT_PAGE_SIZE aligned
3313 * @alignment: required alignment of starting offset, may be 0 but
3314 * if specified, this must be a power-of-two and at least
3315 * #I915_GTT_MIN_ALIGNMENT
3316 * @color: color to apply to node
3317 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003318 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003319 * @end: end of any range restriction inside GTT (U64_MAX for all),
3320 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3321 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003322 *
3323 * i915_gem_gtt_insert() first searches for an available hole into which
3324 * is can insert the node. The hole address is aligned to @alignment and
3325 * its @size must then fit entirely within the [@start, @end] bounds. The
3326 * nodes on either side of the hole must match @color, or else a guard page
3327 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003328 * suitable hole is found, first a victim is randomly selected and tested
3329 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003330 * is scanned to find the first set of replacement nodes to create the hole.
3331 * Those old overlapping nodes are evicted from the GTT (and so must be
3332 * rebound before any future use). Any node that is currently pinned cannot
3333 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3334 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3335 * searching for an eviction candidate. See i915_gem_evict_something() for
3336 * the gory details on the eviction algorithm.
3337 *
3338 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3339 * asked to wait for eviction and interrupted.
3340 */
3341int i915_gem_gtt_insert(struct i915_address_space *vm,
3342 struct drm_mm_node *node,
3343 u64 size, u64 alignment, unsigned long color,
3344 u64 start, u64 end, unsigned int flags)
3345{
Chris Wilson4e64e552017-02-02 21:04:38 +00003346 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003347 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003348 int err;
3349
3350 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3351 GEM_BUG_ON(!size);
3352 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3353 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3354 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3355 GEM_BUG_ON(start >= end);
3356 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3357 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003358 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003359 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003360
3361 if (unlikely(range_overflows(start, size, end)))
3362 return -ENOSPC;
3363
3364 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3365 return -ENOSPC;
3366
Chris Wilson4e64e552017-02-02 21:04:38 +00003367 mode = DRM_MM_INSERT_BEST;
3368 if (flags & PIN_HIGH)
3369 mode = DRM_MM_INSERT_HIGH;
3370 if (flags & PIN_MAPPABLE)
3371 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003372
3373 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3374 * so we know that we always have a minimum alignment of 4096.
3375 * The drm_mm range manager is optimised to return results
3376 * with zero alignment, so where possible use the optimal
3377 * path.
3378 */
3379 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3380 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3381 alignment = 0;
3382
Chris Wilson4e64e552017-02-02 21:04:38 +00003383 err = drm_mm_insert_node_in_range(&vm->mm, node,
3384 size, alignment, color,
3385 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003386 if (err != -ENOSPC)
3387 return err;
3388
Chris Wilson606fec92017-01-11 11:23:12 +00003389 /* No free space, pick a slot at random.
3390 *
3391 * There is a pathological case here using a GTT shared between
3392 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3393 *
3394 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3395 * (64k objects) (448k objects)
3396 *
3397 * Now imagine that the eviction LRU is ordered top-down (just because
3398 * pathology meets real life), and that we need to evict an object to
3399 * make room inside the aperture. The eviction scan then has to walk
3400 * the 448k list before it finds one within range. And now imagine that
3401 * it has to search for a new hole between every byte inside the memcpy,
3402 * for several simultaneous clients.
3403 *
3404 * On a full-ppgtt system, if we have run out of available space, there
3405 * will be lots and lots of objects in the eviction list! Again,
3406 * searching that LRU list may be slow if we are also applying any
3407 * range restrictions (e.g. restriction to low 4GiB) and so, for
3408 * simplicity and similarilty between different GTT, try the single
3409 * random replacement first.
3410 */
3411 offset = random_offset(start, end,
3412 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3413 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3414 if (err != -ENOSPC)
3415 return err;
3416
3417 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003418 err = i915_gem_evict_something(vm, size, alignment, color,
3419 start, end, flags);
3420 if (err)
3421 return err;
3422
Chris Wilson4e64e552017-02-02 21:04:38 +00003423 return drm_mm_insert_node_in_range(&vm->mm, node,
3424 size, alignment, color,
3425 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003426}
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003427
3428#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3429#include "selftests/mock_gtt.c"
Chris Wilson1c428192017-02-13 17:15:38 +00003430#include "selftests/i915_gem_gtt.c"
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003431#endif