blob: 6fdbd5ae4fcb6465710d16f03da581e08863349b [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
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000818struct gen8_insert_pte {
819 u16 pml4e;
820 u16 pdpe;
821 u16 pde;
822 u16 pte;
823};
824
825static __always_inline struct gen8_insert_pte gen8_insert_pte(u64 start)
826{
827 return (struct gen8_insert_pte) {
828 gen8_pml4e_index(start),
829 gen8_pdpe_index(start),
830 gen8_pde_index(start),
831 gen8_pte_index(start),
832 };
833}
834
Chris Wilson894cceb2017-02-15 08:43:37 +0000835static __always_inline bool
836gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100837 struct i915_page_directory_pointer *pdp,
Chris Wilson894cceb2017-02-15 08:43:37 +0000838 struct sgt_dma *iter,
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000839 struct gen8_insert_pte *idx,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100840 enum i915_cache_level cache_level)
841{
Chris Wilson894cceb2017-02-15 08:43:37 +0000842 struct i915_page_directory *pd;
843 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
844 gen8_pte_t *vaddr;
845 bool ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700846
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000847 GEM_BUG_ON(idx->pdpe >= I915_PDPES_PER_PDP(vm));
848 pd = pdp->page_directory[idx->pdpe];
849 vaddr = kmap_atomic_px(pd->page_table[idx->pde]);
Chris Wilson894cceb2017-02-15 08:43:37 +0000850 do {
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000851 vaddr[idx->pte] = pte_encode | iter->dma;
852
Chris Wilson894cceb2017-02-15 08:43:37 +0000853 iter->dma += PAGE_SIZE;
854 if (iter->dma >= iter->max) {
855 iter->sg = __sg_next(iter->sg);
856 if (!iter->sg) {
857 ret = false;
858 break;
859 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700860
Chris Wilson894cceb2017-02-15 08:43:37 +0000861 iter->dma = sg_dma_address(iter->sg);
862 iter->max = iter->dma + iter->sg->length;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000863 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800864
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000865 if (++idx->pte == GEN8_PTES) {
866 idx->pte = 0;
867
868 if (++idx->pde == I915_PDES) {
869 idx->pde = 0;
870
Chris Wilson894cceb2017-02-15 08:43:37 +0000871 /* Limited by sg length for 3lvl */
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000872 if (++idx->pdpe == GEN8_PML4ES_PER_PML4) {
873 idx->pdpe = 0;
Chris Wilson894cceb2017-02-15 08:43:37 +0000874 ret = true;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100875 break;
Chris Wilson894cceb2017-02-15 08:43:37 +0000876 }
877
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000878 GEM_BUG_ON(idx->pdpe >= I915_PDPES_PER_PDP(vm));
879 pd = pdp->page_directory[idx->pdpe];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800880 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000881
Chris Wilson9231da72017-02-15 08:43:41 +0000882 kunmap_atomic(vaddr);
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000883 vaddr = kmap_atomic_px(pd->page_table[idx->pde]);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700884 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000885 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +0000886 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300887
Chris Wilson894cceb2017-02-15 08:43:37 +0000888 return ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700889}
890
Chris Wilson894cceb2017-02-15 08:43:37 +0000891static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
892 struct sg_table *pages,
893 u64 start,
894 enum i915_cache_level cache_level,
895 u32 unused)
Michel Thierryf9b5b782015-07-30 11:02:49 +0100896{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300897 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilson894cceb2017-02-15 08:43:37 +0000898 struct sgt_dma iter = {
899 .sg = pages->sgl,
900 .dma = sg_dma_address(iter.sg),
901 .max = iter.dma + iter.sg->length,
902 };
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000903 struct gen8_insert_pte idx = gen8_insert_pte(start);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100904
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000905 gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter, &idx,
906 cache_level);
Chris Wilson894cceb2017-02-15 08:43:37 +0000907}
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100908
Chris Wilson894cceb2017-02-15 08:43:37 +0000909static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
910 struct sg_table *pages,
Chris Wilson75c7b0b2017-02-15 08:43:57 +0000911 u64 start,
Chris Wilson894cceb2017-02-15 08:43:37 +0000912 enum i915_cache_level cache_level,
913 u32 unused)
914{
915 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
916 struct sgt_dma iter = {
917 .sg = pages->sgl,
918 .dma = sg_dma_address(iter.sg),
919 .max = iter.dma + iter.sg->length,
920 };
921 struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000922 struct gen8_insert_pte idx = gen8_insert_pte(start);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100923
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000924 while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[idx.pml4e++], &iter,
925 &idx, cache_level))
926 GEM_BUG_ON(idx.pml4e >= GEN8_PML4ES_PER_PML4);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100927}
928
Chris Wilson84486612017-02-15 08:43:40 +0000929static void gen8_free_page_tables(struct i915_address_space *vm,
Michel Thierryf37c0502015-06-10 17:46:39 +0100930 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800931{
932 int i;
933
Mika Kuoppala567047b2015-06-25 18:35:12 +0300934 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800935 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800936
Chris Wilsonfe52e372017-02-15 08:43:47 +0000937 for (i = 0; i < I915_PDES; i++) {
938 if (pd->page_table[i] != vm->scratch_pt)
939 free_pt(vm, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000940 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000941}
942
Mika Kuoppala8776f022015-06-30 18:16:40 +0300943static int gen8_init_scratch(struct i915_address_space *vm)
944{
Matthew Auld64c050d2016-04-27 13:19:25 +0100945 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300946
Chris Wilson84486612017-02-15 08:43:40 +0000947 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100948 if (ret)
949 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300950
Chris Wilson84486612017-02-15 08:43:40 +0000951 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300952 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100953 ret = PTR_ERR(vm->scratch_pt);
954 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300955 }
956
Chris Wilson84486612017-02-15 08:43:40 +0000957 vm->scratch_pd = alloc_pd(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300958 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100959 ret = PTR_ERR(vm->scratch_pd);
960 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300961 }
962
Chris Wilson84486612017-02-15 08:43:40 +0000963 if (USES_FULL_48BIT_PPGTT(dev)) {
964 vm->scratch_pdp = alloc_pdp(vm);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100965 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100966 ret = PTR_ERR(vm->scratch_pdp);
967 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100968 }
969 }
970
Mika Kuoppala8776f022015-06-30 18:16:40 +0300971 gen8_initialize_pt(vm, vm->scratch_pt);
972 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000973 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100974 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300975
976 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100977
978free_pd:
Chris Wilson84486612017-02-15 08:43:40 +0000979 free_pd(vm, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +0100980free_pt:
Chris Wilson84486612017-02-15 08:43:40 +0000981 free_pt(vm, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +0100982free_scratch_page:
Chris Wilson84486612017-02-15 08:43:40 +0000983 cleanup_scratch_page(vm);
Matthew Auld64c050d2016-04-27 13:19:25 +0100984
985 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300986}
987
Zhiyuan Lv650da342015-08-28 15:41:18 +0800988static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
989{
990 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +0000991 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +0800992 int i;
993
Matthew Aulddf285642016-04-22 12:09:25 +0100994 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +0800995 u64 daddr = px_dma(&ppgtt->pml4);
996
Ville Syrjäläab75bb52015-11-04 23:20:12 +0200997 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
998 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +0800999
1000 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1001 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1002 } else {
1003 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1004 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1005
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001006 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1007 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001008 }
1009
1010 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1011 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1012 }
1013
1014 I915_WRITE(vgtif_reg(g2v_notify), msg);
1015
1016 return 0;
1017}
1018
Mika Kuoppala8776f022015-06-30 18:16:40 +03001019static void gen8_free_scratch(struct i915_address_space *vm)
1020{
Chris Wilson84486612017-02-15 08:43:40 +00001021 if (USES_FULL_48BIT_PPGTT(vm->i915))
1022 free_pdp(vm, vm->scratch_pdp);
1023 free_pd(vm, vm->scratch_pd);
1024 free_pt(vm, vm->scratch_pt);
1025 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001026}
1027
Chris Wilson84486612017-02-15 08:43:40 +00001028static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
Michel Thierry762d9932015-07-30 11:05:29 +01001029 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001030{
1031 int i;
1032
Chris Wilsone2b763c2017-02-15 08:43:48 +00001033 for (i = 0; i < I915_PDPES_PER_PDP(vm->i915); i++) {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001034 if (pdp->page_directory[i] == vm->scratch_pd)
Ben Widawsky06fda602015-02-24 16:22:36 +00001035 continue;
1036
Chris Wilson84486612017-02-15 08:43:40 +00001037 gen8_free_page_tables(vm, pdp->page_directory[i]);
1038 free_pd(vm, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001039 }
Michel Thierry69876be2015-04-08 12:13:27 +01001040
Chris Wilson84486612017-02-15 08:43:40 +00001041 free_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001042}
1043
1044static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1045{
1046 int i;
1047
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001048 for (i = 0; i < GEN8_PML4ES_PER_PML4; i++) {
1049 if (ppgtt->pml4.pdps[i] == ppgtt->base.scratch_pdp)
Michel Thierry762d9932015-07-30 11:05:29 +01001050 continue;
1051
Chris Wilson84486612017-02-15 08:43:40 +00001052 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001053 }
1054
Chris Wilson84486612017-02-15 08:43:40 +00001055 cleanup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001056}
1057
1058static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1059{
Chris Wilson49d73912016-11-29 09:50:08 +00001060 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001061 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001062
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001063 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001064 gen8_ppgtt_notify_vgt(ppgtt, false);
1065
Chris Wilson84486612017-02-15 08:43:40 +00001066 if (!USES_FULL_48BIT_PPGTT(vm->i915))
1067 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001068 else
1069 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001070
Mika Kuoppala8776f022015-06-30 18:16:40 +03001071 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001072}
1073
Chris Wilsonfe52e372017-02-15 08:43:47 +00001074static int gen8_ppgtt_alloc_pd(struct i915_address_space *vm,
1075 struct i915_page_directory *pd,
1076 u64 start, u64 length)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001077{
Michel Thierryd7b26332015-04-08 12:13:34 +01001078 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +00001079 u64 from = start;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001080 unsigned int pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001081
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001082 gen8_for_each_pde(pt, pd, start, length, pde) {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001083 if (pt == vm->scratch_pt) {
Chris Wilsondd196742017-02-15 08:43:46 +00001084 pt = alloc_pt(vm);
1085 if (IS_ERR(pt))
1086 goto unwind;
1087
1088 gen8_initialize_pt(vm, pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001089
Chris Wilsonfe52e372017-02-15 08:43:47 +00001090 gen8_ppgtt_set_pde(vm, pd, pt, pde);
1091 pd->used_pdes++;
1092 }
1093
1094 pt->used_ptes += gen8_pte_count(start, length);
1095 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001096 return 0;
1097
Chris Wilsondd196742017-02-15 08:43:46 +00001098unwind:
1099 gen8_ppgtt_clear_pd(vm, pd, from, start - from);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001100 return -ENOMEM;
1101}
1102
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001103static int gen8_ppgtt_alloc_pdp(struct i915_address_space *vm,
1104 struct i915_page_directory_pointer *pdp,
1105 u64 start, u64 length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001106{
Michel Thierry5441f0c2015-04-08 12:13:28 +01001107 struct i915_page_directory *pd;
Chris Wilsone2b763c2017-02-15 08:43:48 +00001108 u64 from = start;
1109 unsigned int pdpe;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001110 int ret;
1111
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001112 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilsone2b763c2017-02-15 08:43:48 +00001113 if (pd == vm->scratch_pd) {
1114 pd = alloc_pd(vm);
1115 if (IS_ERR(pd))
1116 goto unwind;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001117
Chris Wilsone2b763c2017-02-15 08:43:48 +00001118 gen8_initialize_pd(vm, pd);
Chris Wilsonfe52e372017-02-15 08:43:47 +00001119 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
Chris Wilsone2b763c2017-02-15 08:43:48 +00001120 pdp->used_pdpes++;
Chris Wilson75afcf72017-02-15 08:43:51 +00001121
1122 mark_tlbs_dirty(i915_vm_to_ppgtt(vm));
Chris Wilsone2b763c2017-02-15 08:43:48 +00001123 }
1124
1125 ret = gen8_ppgtt_alloc_pd(vm, pd, start, length);
1126 if (unlikely(ret)) {
1127 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1128 pdp->used_pdpes--;
1129 free_pd(vm, pd);
1130 goto unwind;
1131 }
Chris Wilsonfe52e372017-02-15 08:43:47 +00001132 }
Michel Thierry33c88192015-04-08 12:13:33 +01001133
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001134 return 0;
1135
Chris Wilsone2b763c2017-02-15 08:43:48 +00001136unwind:
1137 gen8_ppgtt_clear_pdp(vm, pdp, from, start - from);
1138 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001139}
1140
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001141static int gen8_ppgtt_alloc_3lvl(struct i915_address_space *vm,
1142 u64 start, u64 length)
Michel Thierry762d9932015-07-30 11:05:29 +01001143{
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001144 return gen8_ppgtt_alloc_pdp(vm,
1145 &i915_vm_to_ppgtt(vm)->pdp, start, length);
1146}
1147
1148static int gen8_ppgtt_alloc_4lvl(struct i915_address_space *vm,
1149 u64 start, u64 length)
1150{
1151 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1152 struct i915_pml4 *pml4 = &ppgtt->pml4;
Michel Thierry762d9932015-07-30 11:05:29 +01001153 struct i915_page_directory_pointer *pdp;
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001154 u64 from = start;
1155 u32 pml4e;
1156 int ret;
Michel Thierry762d9932015-07-30 11:05:29 +01001157
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001158 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001159 if (pml4->pdps[pml4e] == vm->scratch_pdp) {
1160 pdp = alloc_pdp(vm);
1161 if (IS_ERR(pdp))
1162 goto unwind;
Michel Thierry762d9932015-07-30 11:05:29 +01001163
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001164 gen8_initialize_pdp(vm, pdp);
1165 gen8_ppgtt_set_pml4e(pml4, pdp, pml4e);
1166 }
Michel Thierry762d9932015-07-30 11:05:29 +01001167
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001168 ret = gen8_ppgtt_alloc_pdp(vm, pdp, start, length);
1169 if (unlikely(ret)) {
1170 gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
1171 free_pdp(vm, pdp);
1172 goto unwind;
1173 }
Michel Thierry762d9932015-07-30 11:05:29 +01001174 }
1175
Michel Thierry762d9932015-07-30 11:05:29 +01001176 return 0;
1177
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001178unwind:
1179 gen8_ppgtt_clear_4lvl(vm, from, start - from);
1180 return -ENOMEM;
Michel Thierry762d9932015-07-30 11:05:29 +01001181}
1182
Chris Wilson84486612017-02-15 08:43:40 +00001183static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
1184 struct i915_page_directory_pointer *pdp,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001185 u64 start, u64 length,
Michel Thierryea91e402015-07-29 17:23:57 +01001186 gen8_pte_t scratch_pte,
1187 struct seq_file *m)
1188{
1189 struct i915_page_directory *pd;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001190 u32 pdpe;
Michel Thierryea91e402015-07-29 17:23:57 +01001191
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001192 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001193 struct i915_page_table *pt;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001194 u64 pd_len = length;
1195 u64 pd_start = start;
1196 u32 pde;
Michel Thierryea91e402015-07-29 17:23:57 +01001197
Chris Wilsone2b763c2017-02-15 08:43:48 +00001198 if (pdp->page_directory[pdpe] == ppgtt->base.scratch_pd)
Michel Thierryea91e402015-07-29 17:23:57 +01001199 continue;
1200
1201 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001202 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001203 u32 pte;
Michel Thierryea91e402015-07-29 17:23:57 +01001204 gen8_pte_t *pt_vaddr;
1205
Chris Wilsonfe52e372017-02-15 08:43:47 +00001206 if (pd->page_table[pde] == ppgtt->base.scratch_pt)
Michel Thierryea91e402015-07-29 17:23:57 +01001207 continue;
1208
Chris Wilson9231da72017-02-15 08:43:41 +00001209 pt_vaddr = kmap_atomic_px(pt);
Michel Thierryea91e402015-07-29 17:23:57 +01001210 for (pte = 0; pte < GEN8_PTES; pte += 4) {
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001211 u64 va = (pdpe << GEN8_PDPE_SHIFT |
1212 pde << GEN8_PDE_SHIFT |
1213 pte << GEN8_PTE_SHIFT);
Michel Thierryea91e402015-07-29 17:23:57 +01001214 int i;
1215 bool found = false;
1216
1217 for (i = 0; i < 4; i++)
1218 if (pt_vaddr[pte + i] != scratch_pte)
1219 found = true;
1220 if (!found)
1221 continue;
1222
1223 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1224 for (i = 0; i < 4; i++) {
1225 if (pt_vaddr[pte + i] != scratch_pte)
1226 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1227 else
1228 seq_puts(m, " SCRATCH ");
1229 }
1230 seq_puts(m, "\n");
1231 }
Michel Thierryea91e402015-07-29 17:23:57 +01001232 kunmap_atomic(pt_vaddr);
1233 }
1234 }
1235}
1236
1237static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1238{
1239 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson894cceb2017-02-15 08:43:37 +00001240 const gen8_pte_t scratch_pte =
1241 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
Chris Wilson381b9432017-02-15 08:43:54 +00001242 u64 start = 0, length = ppgtt->base.total;
Michel Thierryea91e402015-07-29 17:23:57 +01001243
Chris Wilsonc6385c92016-11-29 12:42:05 +00001244 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Chris Wilson84486612017-02-15 08:43:40 +00001245 gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001246 } else {
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001247 u64 pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001248 struct i915_pml4 *pml4 = &ppgtt->pml4;
1249 struct i915_page_directory_pointer *pdp;
1250
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001251 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001252 if (pml4->pdps[pml4e] == ppgtt->base.scratch_pdp)
Michel Thierryea91e402015-07-29 17:23:57 +01001253 continue;
1254
1255 seq_printf(m, " PML4E #%llu\n", pml4e);
Chris Wilson84486612017-02-15 08:43:40 +00001256 gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001257 }
1258 }
1259}
1260
Chris Wilsone2b763c2017-02-15 08:43:48 +00001261static int gen8_preallocate_top_level_pdp(struct i915_hw_ppgtt *ppgtt)
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001262{
Chris Wilsone2b763c2017-02-15 08:43:48 +00001263 struct i915_address_space *vm = &ppgtt->base;
1264 struct i915_page_directory_pointer *pdp = &ppgtt->pdp;
1265 struct i915_page_directory *pd;
1266 u64 start = 0, length = ppgtt->base.total;
1267 u64 from = start;
1268 unsigned int pdpe;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001269
Chris Wilsone2b763c2017-02-15 08:43:48 +00001270 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1271 pd = alloc_pd(vm);
1272 if (IS_ERR(pd))
1273 goto unwind;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001274
Chris Wilsone2b763c2017-02-15 08:43:48 +00001275 gen8_initialize_pd(vm, pd);
1276 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
1277 pdp->used_pdpes++;
1278 }
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001279
Chris Wilsone2b763c2017-02-15 08:43:48 +00001280 pdp->used_pdpes++; /* never remove */
1281 return 0;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001282
Chris Wilsone2b763c2017-02-15 08:43:48 +00001283unwind:
1284 start -= from;
1285 gen8_for_each_pdpe(pd, pdp, from, start, pdpe) {
1286 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1287 free_pd(vm, pd);
1288 }
1289 pdp->used_pdpes = 0;
1290 return -ENOMEM;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001291}
1292
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001293/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001294 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1295 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1296 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1297 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001298 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001299 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001300static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001301{
Chris Wilson49d73912016-11-29 09:50:08 +00001302 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001303 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001304
Mika Kuoppala8776f022015-06-30 18:16:40 +03001305 ret = gen8_init_scratch(&ppgtt->base);
1306 if (ret)
1307 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001308
Michel Thierryd7b26332015-04-08 12:13:34 +01001309 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001310 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1311 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001312 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001313
Chris Wilson84486612017-02-15 08:43:40 +00001314 /* There are only few exceptions for gen >=6. chv and bxt.
1315 * And we are not sure about the latter so play safe for now.
1316 */
1317 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
1318 ppgtt->base.pt_kmap_wc = true;
1319
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001320 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Chris Wilson84486612017-02-15 08:43:40 +00001321 ret = setup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001322 if (ret)
1323 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001324
Michel Thierry69ab76f2015-07-29 17:23:55 +01001325 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1326
Michel Thierry762d9932015-07-30 11:05:29 +01001327 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001328 ppgtt->switch_mm = gen8_48b_mm_switch;
Chris Wilson894cceb2017-02-15 08:43:37 +00001329
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001330 ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_4lvl;
Chris Wilson894cceb2017-02-15 08:43:37 +00001331 ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001332 ppgtt->base.clear_range = gen8_ppgtt_clear_4lvl;
Michel Thierry762d9932015-07-30 11:05:29 +01001333 } else {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001334 ret = __pdp_init(&ppgtt->base, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001335 if (ret)
1336 goto free_scratch;
1337
1338 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001339 ppgtt->switch_mm = gen8_legacy_mm_switch;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001340
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001341 if (intel_vgpu_active(dev_priv)) {
Chris Wilsone2b763c2017-02-15 08:43:48 +00001342 ret = gen8_preallocate_top_level_pdp(ppgtt);
1343 if (ret) {
1344 __pdp_fini(&ppgtt->pdp);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001345 goto free_scratch;
Chris Wilsone2b763c2017-02-15 08:43:48 +00001346 }
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001347 }
Chris Wilson894cceb2017-02-15 08:43:37 +00001348
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001349 ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_3lvl;
Chris Wilson894cceb2017-02-15 08:43:37 +00001350 ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001351 ppgtt->base.clear_range = gen8_ppgtt_clear_3lvl;
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001352 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001353
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001354 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001355 gen8_ppgtt_notify_vgt(ppgtt, true);
1356
Michel Thierryd7b26332015-04-08 12:13:34 +01001357 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001358
1359free_scratch:
1360 gen8_free_scratch(&ppgtt->base);
1361 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001362}
1363
Ben Widawsky87d60b62013-12-06 14:11:29 -08001364static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1365{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001366 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001367 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001368 gen6_pte_t scratch_pte;
Chris Wilson381b9432017-02-15 08:43:54 +00001369 u32 pd_entry, pte, pde;
1370 u32 start = 0, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001371
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001372 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001373 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001374
Dave Gordon731f74c2016-06-24 19:37:46 +01001375 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001376 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001377 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001378 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001379 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001380 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1381
1382 if (pd_entry != expected)
1383 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1384 pde,
1385 pd_entry,
1386 expected);
1387 seq_printf(m, "\tPDE: %x\n", pd_entry);
1388
Chris Wilson9231da72017-02-15 08:43:41 +00001389 pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[pde]);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001390
Michel Thierry07749ef2015-03-16 16:00:54 +00001391 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001392 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001393 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001394 (pte * PAGE_SIZE);
1395 int i;
1396 bool found = false;
1397 for (i = 0; i < 4; i++)
1398 if (pt_vaddr[pte + i] != scratch_pte)
1399 found = true;
1400 if (!found)
1401 continue;
1402
1403 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1404 for (i = 0; i < 4; i++) {
1405 if (pt_vaddr[pte + i] != scratch_pte)
1406 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1407 else
1408 seq_puts(m, " SCRATCH ");
1409 }
1410 seq_puts(m, "\n");
1411 }
Chris Wilson9231da72017-02-15 08:43:41 +00001412 kunmap_atomic(pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001413 }
1414}
1415
Ben Widawsky678d96f2015-03-16 16:00:56 +00001416/* Write pde (index) from the page directory @pd to the page table @pt */
Chris Wilson16a011c2017-02-15 08:43:45 +00001417static inline void gen6_write_pde(const struct i915_hw_ppgtt *ppgtt,
1418 const unsigned int pde,
1419 const struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001420{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001421 /* Caller needs to make sure the write completes if necessary */
Chris Wilson16a011c2017-02-15 08:43:45 +00001422 writel_relaxed(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
1423 ppgtt->pd_addr + pde);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001424}
Ben Widawsky61973492013-04-08 18:43:54 -07001425
Ben Widawsky678d96f2015-03-16 16:00:56 +00001426/* Write all the page tables found in the ppgtt structure to incrementing page
1427 * directories. */
Chris Wilson16a011c2017-02-15 08:43:45 +00001428static void gen6_write_page_range(struct i915_hw_ppgtt *ppgtt,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001429 u32 start, u32 length)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001430{
Michel Thierryec565b32015-04-08 12:13:23 +01001431 struct i915_page_table *pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001432 unsigned int pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001433
Chris Wilson16a011c2017-02-15 08:43:45 +00001434 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde)
1435 gen6_write_pde(ppgtt, pde, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001436
Chris Wilson16a011c2017-02-15 08:43:45 +00001437 mark_tlbs_dirty(ppgtt);
Chris Wilsondd196742017-02-15 08:43:46 +00001438 wmb();
Ben Widawsky3e302542013-04-23 23:15:32 -07001439}
1440
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001441static inline u32 get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001442{
Chris Wilsondd196742017-02-15 08:43:46 +00001443 GEM_BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1444 return ppgtt->pd.base.ggtt_offset << 10;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001445}
Ben Widawsky61973492013-04-08 18:43:54 -07001446
Ben Widawsky90252e52013-12-06 14:11:12 -08001447static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001448 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001449{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001450 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001451 u32 *cs;
Ben Widawsky90252e52013-12-06 14:11:12 -08001452 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001453
Ben Widawsky90252e52013-12-06 14:11:12 -08001454 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001455 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001456 if (ret)
1457 return ret;
1458
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001459 cs = intel_ring_begin(req, 6);
1460 if (IS_ERR(cs))
1461 return PTR_ERR(cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001462
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001463 *cs++ = MI_LOAD_REGISTER_IMM(2);
1464 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1465 *cs++ = PP_DIR_DCLV_2G;
1466 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1467 *cs++ = get_pd_offset(ppgtt);
1468 *cs++ = MI_NOOP;
1469 intel_ring_advance(req, cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001470
1471 return 0;
1472}
1473
Ben Widawsky48a10382013-12-06 14:11:11 -08001474static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001475 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001476{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001477 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001478 u32 *cs;
Ben Widawsky48a10382013-12-06 14:11:11 -08001479 int ret;
1480
Ben Widawsky48a10382013-12-06 14:11:11 -08001481 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001482 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001483 if (ret)
1484 return ret;
1485
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001486 cs = intel_ring_begin(req, 6);
1487 if (IS_ERR(cs))
1488 return PTR_ERR(cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001489
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001490 *cs++ = MI_LOAD_REGISTER_IMM(2);
1491 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1492 *cs++ = PP_DIR_DCLV_2G;
1493 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1494 *cs++ = get_pd_offset(ppgtt);
1495 *cs++ = MI_NOOP;
1496 intel_ring_advance(req, cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001497
Ben Widawsky90252e52013-12-06 14:11:12 -08001498 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001499 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001500 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001501 if (ret)
1502 return ret;
1503 }
1504
Ben Widawsky48a10382013-12-06 14:11:11 -08001505 return 0;
1506}
1507
Ben Widawskyeeb94882013-12-06 14:11:10 -08001508static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001509 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001510{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001511 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001512 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001513
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001514 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1515 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001516 return 0;
1517}
1518
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001519static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001520{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001521 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301522 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001523
Akash Goel3b3f1652016-10-13 22:44:48 +05301524 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001525 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1526 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001527 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001528 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001529 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001530}
1531
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001532static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001533{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001534 struct intel_engine_cs *engine;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001535 u32 ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301536 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001537
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001538 ecobits = I915_READ(GAC_ECO_BITS);
1539 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1540
1541 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001542 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001543 ecochk |= ECOCHK_PPGTT_WB_HSW;
1544 } else {
1545 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1546 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1547 }
1548 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001549
Akash Goel3b3f1652016-10-13 22:44:48 +05301550 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001551 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001552 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001553 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001554 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001555}
1556
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001557static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001558{
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001559 u32 ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001560
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001561 ecobits = I915_READ(GAC_ECO_BITS);
1562 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1563 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001564
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001565 gab_ctl = I915_READ(GAB_CTL);
1566 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001567
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001568 ecochk = I915_READ(GAM_ECOCHK);
1569 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001570
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001571 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001572}
1573
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001574/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001575static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Chris Wilsondd196742017-02-15 08:43:46 +00001576 u64 start, u64 length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001577{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001578 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilsondd196742017-02-15 08:43:46 +00001579 unsigned int first_entry = start >> PAGE_SHIFT;
1580 unsigned int pde = first_entry / GEN6_PTES;
1581 unsigned int pte = first_entry % GEN6_PTES;
1582 unsigned int num_entries = length >> PAGE_SHIFT;
1583 gen6_pte_t scratch_pte =
1584 vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001585
Daniel Vetter7bddb012012-02-09 17:15:47 +01001586 while (num_entries) {
Chris Wilsondd196742017-02-15 08:43:46 +00001587 struct i915_page_table *pt = ppgtt->pd.page_table[pde++];
1588 unsigned int end = min(pte + num_entries, GEN6_PTES);
1589 gen6_pte_t *vaddr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001590
Chris Wilsondd196742017-02-15 08:43:46 +00001591 num_entries -= end - pte;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001592
Chris Wilsondd196742017-02-15 08:43:46 +00001593 /* Note that the hw doesn't support removing PDE on the fly
1594 * (they are cached inside the context with no means to
1595 * invalidate the cache), so we can only reset the PTE
1596 * entries back to scratch.
1597 */
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001598
Chris Wilsondd196742017-02-15 08:43:46 +00001599 vaddr = kmap_atomic_px(pt);
1600 do {
1601 vaddr[pte++] = scratch_pte;
1602 } while (pte < end);
1603 kunmap_atomic(vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001604
Chris Wilsondd196742017-02-15 08:43:46 +00001605 pte = 0;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001606 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001607}
1608
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001609static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001610 struct sg_table *pages,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001611 u64 start,
1612 enum i915_cache_level cache_level,
1613 u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001614{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001615 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001616 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001617 unsigned act_pt = first_entry / GEN6_PTES;
1618 unsigned act_pte = first_entry % GEN6_PTES;
Chris Wilsonb31144c2017-02-15 08:43:36 +00001619 const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1620 struct sgt_dma iter;
1621 gen6_pte_t *vaddr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001622
Chris Wilson9231da72017-02-15 08:43:41 +00001623 vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
Chris Wilsonb31144c2017-02-15 08:43:36 +00001624 iter.sg = pages->sgl;
1625 iter.dma = sg_dma_address(iter.sg);
1626 iter.max = iter.dma + iter.sg->length;
1627 do {
1628 vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001629
Chris Wilsonb31144c2017-02-15 08:43:36 +00001630 iter.dma += PAGE_SIZE;
1631 if (iter.dma == iter.max) {
1632 iter.sg = __sg_next(iter.sg);
1633 if (!iter.sg)
1634 break;
1635
1636 iter.dma = sg_dma_address(iter.sg);
1637 iter.max = iter.dma + iter.sg->length;
1638 }
Akash Goel24f3a8c2014-06-17 10:59:42 +05301639
Michel Thierry07749ef2015-03-16 16:00:54 +00001640 if (++act_pte == GEN6_PTES) {
Chris Wilson9231da72017-02-15 08:43:41 +00001641 kunmap_atomic(vaddr);
1642 vaddr = kmap_atomic_px(ppgtt->pd.page_table[++act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +02001643 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001644 }
Chris Wilsonb31144c2017-02-15 08:43:36 +00001645 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +00001646 kunmap_atomic(vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001647}
1648
Ben Widawsky678d96f2015-03-16 16:00:56 +00001649static int gen6_alloc_va_range(struct i915_address_space *vm,
Chris Wilsondd196742017-02-15 08:43:46 +00001650 u64 start, u64 length)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001651{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001652 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001653 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +00001654 u64 from = start;
1655 unsigned int pde;
1656 bool flush = false;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001657
Dave Gordon731f74c2016-06-24 19:37:46 +01001658 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Chris Wilsondd196742017-02-15 08:43:46 +00001659 if (pt == vm->scratch_pt) {
1660 pt = alloc_pt(vm);
1661 if (IS_ERR(pt))
1662 goto unwind_out;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001663
Chris Wilsondd196742017-02-15 08:43:46 +00001664 gen6_initialize_pt(vm, pt);
1665 ppgtt->pd.page_table[pde] = pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001666 gen6_write_pde(ppgtt, pde, pt);
Chris Wilsondd196742017-02-15 08:43:46 +00001667 flush = true;
1668 }
Ben Widawsky678d96f2015-03-16 16:00:56 +00001669 }
1670
Chris Wilsondd196742017-02-15 08:43:46 +00001671 if (flush) {
1672 mark_tlbs_dirty(ppgtt);
1673 wmb();
1674 }
Michel Thierry4933d512015-03-24 15:46:22 +00001675
Ben Widawsky678d96f2015-03-16 16:00:56 +00001676 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001677
1678unwind_out:
Chris Wilsondd196742017-02-15 08:43:46 +00001679 gen6_ppgtt_clear_range(vm, from, start);
1680 return -ENOMEM;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001681}
1682
Mika Kuoppala8776f022015-06-30 18:16:40 +03001683static int gen6_init_scratch(struct i915_address_space *vm)
1684{
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001685 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001686
Chris Wilson84486612017-02-15 08:43:40 +00001687 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001688 if (ret)
1689 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001690
Chris Wilson84486612017-02-15 08:43:40 +00001691 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001692 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson84486612017-02-15 08:43:40 +00001693 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001694 return PTR_ERR(vm->scratch_pt);
1695 }
1696
1697 gen6_initialize_pt(vm, vm->scratch_pt);
1698
1699 return 0;
1700}
1701
1702static void gen6_free_scratch(struct i915_address_space *vm)
1703{
Chris Wilson84486612017-02-15 08:43:40 +00001704 free_pt(vm, vm->scratch_pt);
1705 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001706}
1707
Daniel Vetter061dd492015-04-14 17:35:13 +02001708static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08001709{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001710 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01001711 struct i915_page_directory *pd = &ppgtt->pd;
Michel Thierry09942c62015-04-08 12:13:30 +01001712 struct i915_page_table *pt;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001713 u32 pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08001714
Daniel Vetter061dd492015-04-14 17:35:13 +02001715 drm_mm_remove_node(&ppgtt->node);
1716
Dave Gordon731f74c2016-06-24 19:37:46 +01001717 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001718 if (pt != vm->scratch_pt)
Chris Wilson84486612017-02-15 08:43:40 +00001719 free_pt(vm, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001720
Mika Kuoppala8776f022015-06-30 18:16:40 +03001721 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08001722}
1723
Ben Widawskyb1465202014-02-19 22:05:49 -08001724static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001725{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001726 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00001727 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001728 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08001729 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001730
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001731 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1732 * allocator works in address space sizes, so it's multiplied by page
1733 * size. We allocate at the top of the GTT to avoid fragmentation.
1734 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001735 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00001736
Mika Kuoppala8776f022015-06-30 18:16:40 +03001737 ret = gen6_init_scratch(vm);
1738 if (ret)
1739 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00001740
Chris Wilsone007b192017-01-11 11:23:10 +00001741 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
1742 GEN6_PD_SIZE, GEN6_PD_ALIGN,
1743 I915_COLOR_UNEVICTABLE,
1744 0, ggtt->base.total,
1745 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00001746 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001747 goto err_out;
1748
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001749 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001750 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001751
Chris Wilson52c126e2017-02-15 08:43:43 +00001752 ppgtt->pd.base.ggtt_offset =
1753 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
1754
1755 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
1756 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
1757
Ben Widawskyc8c26622015-01-22 17:01:25 +00001758 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001759
1760err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03001761 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001762 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08001763}
1764
Ben Widawskyb1465202014-02-19 22:05:49 -08001765static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1766{
kbuild test robot2f2cf682015-03-27 19:26:35 +08001767 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08001768}
1769
Michel Thierry4933d512015-03-24 15:46:22 +00001770static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001771 u64 start, u64 length)
Michel Thierry4933d512015-03-24 15:46:22 +00001772{
Michel Thierryec565b32015-04-08 12:13:23 +01001773 struct i915_page_table *unused;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001774 u32 pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001775
Dave Gordon731f74c2016-06-24 19:37:46 +01001776 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001777 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00001778}
1779
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001780static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08001781{
Chris Wilson49d73912016-11-29 09:50:08 +00001782 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001783 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08001784 int ret;
1785
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001786 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001787 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08001788 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001789 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08001790 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001791 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08001792 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01001793 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001794 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001795
1796 ret = gen6_ppgtt_alloc(ppgtt);
1797 if (ret)
1798 return ret;
1799
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001800 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1801 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001802 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1803 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001804 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Michel Thierry09942c62015-04-08 12:13:30 +01001805 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001806 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001807
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001808 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Chris Wilson16a011c2017-02-15 08:43:45 +00001809 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001810
Chris Wilson52c126e2017-02-15 08:43:43 +00001811 ret = gen6_alloc_va_range(&ppgtt->base, 0, ppgtt->base.total);
1812 if (ret) {
1813 gen6_ppgtt_cleanup(&ppgtt->base);
1814 return ret;
1815 }
1816
Thierry Reding440fd522015-01-23 09:05:06 +01001817 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001818 ppgtt->node.size >> 20,
1819 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001820
Chris Wilson52c126e2017-02-15 08:43:43 +00001821 DRM_DEBUG_DRIVER("Adding PPGTT at offset %x\n",
1822 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001823
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001824 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001825}
1826
Chris Wilson2bfa9962016-08-04 07:52:25 +01001827static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
1828 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08001829{
Chris Wilson49d73912016-11-29 09:50:08 +00001830 ppgtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00001831 ppgtt->base.dma = &dev_priv->drm.pdev->dev;
Daniel Vetter3440d262013-01-24 13:49:56 -08001832
Chris Wilson2bfa9962016-08-04 07:52:25 +01001833 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001834 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001835 else
Michel Thierryd7b26332015-04-08 12:13:34 +01001836 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001837}
Mika Kuoppalac114f762015-06-25 18:35:13 +03001838
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001839static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01001840 struct drm_i915_private *dev_priv,
1841 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001842{
Chris Wilson80b204b2016-10-28 13:58:58 +01001843 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00001844
Chris Wilson381b9432017-02-15 08:43:54 +00001845 drm_mm_init(&vm->mm, 0, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00001846 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
1847
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001848 INIT_LIST_HEAD(&vm->active_list);
1849 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01001850 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00001851
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001852 list_add_tail(&vm->global_link, &dev_priv->vm_list);
Chris Wilson84486612017-02-15 08:43:40 +00001853 pagevec_init(&vm->free_pages, false);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001854}
1855
Matthew Aulded9724d2016-11-17 21:04:10 +00001856static void i915_address_space_fini(struct i915_address_space *vm)
1857{
Chris Wilson84486612017-02-15 08:43:40 +00001858 if (pagevec_count(&vm->free_pages))
1859 vm_free_pages_release(vm);
1860
Matthew Aulded9724d2016-11-17 21:04:10 +00001861 i915_gem_timeline_fini(&vm->timeline);
1862 drm_mm_takedown(&vm->mm);
1863 list_del(&vm->global_link);
1864}
1865
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001866static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00001867{
Tim Gored5165eb2016-02-04 11:49:34 +00001868 /* This function is for gtt related workarounds. This function is
1869 * called on driver load and after a GPU reset, so you can place
1870 * workarounds here even if they get overwritten by GPU reset.
1871 */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001872 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01001873 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001874 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01001875 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001876 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08001877 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001878 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001879 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001880 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
1881}
1882
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001883int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02001884{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001885 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00001886
Thomas Daniel671b50132014-08-20 16:24:50 +01001887 /* In the case of execlists, PPGTT is enabled by the context descriptor
1888 * and the PDPs are contained within the context itself. We don't
1889 * need to do anything here. */
1890 if (i915.enable_execlists)
1891 return 0;
1892
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001893 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02001894 return 0;
1895
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001896 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001897 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001898 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001899 gen7_ppgtt_enable(dev_priv);
1900 else if (INTEL_GEN(dev_priv) >= 8)
1901 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02001902 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001903 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02001904
John Harrison4ad2fd82015-06-18 13:11:20 +01001905 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001906}
John Harrison4ad2fd82015-06-18 13:11:20 +01001907
Daniel Vetter4d884702014-08-06 15:04:47 +02001908struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01001909i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01001910 struct drm_i915_file_private *fpriv,
1911 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02001912{
1913 struct i915_hw_ppgtt *ppgtt;
1914 int ret;
1915
1916 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1917 if (!ppgtt)
1918 return ERR_PTR(-ENOMEM);
1919
Chris Wilson1188bc62017-02-15 08:43:38 +00001920 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetter4d884702014-08-06 15:04:47 +02001921 if (ret) {
1922 kfree(ppgtt);
1923 return ERR_PTR(ret);
1924 }
1925
Chris Wilson1188bc62017-02-15 08:43:38 +00001926 kref_init(&ppgtt->ref);
1927 i915_address_space_init(&ppgtt->base, dev_priv, name);
1928 ppgtt->base.file = fpriv;
1929
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001930 trace_i915_ppgtt_create(&ppgtt->base);
1931
Daniel Vetter4d884702014-08-06 15:04:47 +02001932 return ppgtt;
1933}
1934
Chris Wilson0c7eeda2017-01-11 21:09:25 +00001935void i915_ppgtt_close(struct i915_address_space *vm)
1936{
1937 struct list_head *phases[] = {
1938 &vm->active_list,
1939 &vm->inactive_list,
1940 &vm->unbound_list,
1941 NULL,
1942 }, **phase;
1943
1944 GEM_BUG_ON(vm->closed);
1945 vm->closed = true;
1946
1947 for (phase = phases; *phase; phase++) {
1948 struct i915_vma *vma, *vn;
1949
1950 list_for_each_entry_safe(vma, vn, *phase, vm_link)
1951 if (!i915_vma_is_closed(vma))
1952 i915_vma_close(vma);
1953 }
1954}
1955
Matthew Aulded9724d2016-11-17 21:04:10 +00001956void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02001957{
1958 struct i915_hw_ppgtt *ppgtt =
1959 container_of(kref, struct i915_hw_ppgtt, ref);
1960
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001961 trace_i915_ppgtt_release(&ppgtt->base);
1962
Chris Wilson50e046b2016-08-04 07:52:46 +01001963 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02001964 WARN_ON(!list_empty(&ppgtt->base.active_list));
1965 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01001966 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02001967
1968 ppgtt->base.cleanup(&ppgtt->base);
Chris Wilson84486612017-02-15 08:43:40 +00001969 i915_address_space_fini(&ppgtt->base);
Daniel Vetteree960be2014-08-06 15:04:45 +02001970 kfree(ppgtt);
1971}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001972
Ben Widawskya81cc002013-01-18 12:30:31 -08001973/* Certain Gen5 chipsets require require idling the GPU before
1974 * unmapping anything from the GTT when VT-d is enabled.
1975 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001976static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08001977{
1978#ifdef CONFIG_INTEL_IOMMU
1979 /* Query intel_iommu to see if we need the workaround. Presumably that
1980 * was loaded first.
1981 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001982 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08001983 return true;
1984#endif
1985 return false;
1986}
1987
Chris Wilsondc979972016-05-10 14:10:04 +01001988void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07001989{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001990 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301991 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07001992
Chris Wilsondc979972016-05-10 14:10:04 +01001993 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07001994 return;
1995
Akash Goel3b3f1652016-10-13 22:44:48 +05301996 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07001997 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001998 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07001999 if (fault_reg & RING_FAULT_VALID) {
2000 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002001 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002002 "\tAddress space: %s\n"
2003 "\tSource ID: %d\n"
2004 "\tType: %d\n",
2005 fault_reg & PAGE_MASK,
2006 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2007 RING_FAULT_SRCID(fault_reg),
2008 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002009 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002010 fault_reg & ~RING_FAULT_VALID);
2011 }
2012 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302013
2014 /* Engine specific init may not have been done till this point. */
2015 if (dev_priv->engine[RCS])
2016 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002017}
2018
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002019void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002020{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002021 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002022
2023 /* Don't bother messing with faults pre GEN6 as we have little
2024 * documentation supporting that it's a good idea.
2025 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002026 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002027 return;
2028
Chris Wilsondc979972016-05-10 14:10:04 +01002029 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002030
Chris Wilson381b9432017-02-15 08:43:54 +00002031 ggtt->base.clear_range(&ggtt->base, 0, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002032
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002033 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002034}
2035
Chris Wilson03ac84f2016-10-28 13:58:36 +01002036int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2037 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002038{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002039 do {
2040 if (dma_map_sg(&obj->base.dev->pdev->dev,
2041 pages->sgl, pages->nents,
2042 PCI_DMA_BIDIRECTIONAL))
2043 return 0;
2044
2045 /* If the DMA remap fails, one cause can be that we have
2046 * too many objects pinned in a small remapping table,
2047 * such as swiotlb. Incrementally purge all other objects and
2048 * try again - if there are no more pages to remove from
2049 * the DMA remapper, i915_gem_shrink will return 0.
2050 */
2051 GEM_BUG_ON(obj->mm.pages == pages);
2052 } while (i915_gem_shrink(to_i915(obj->base.dev),
2053 obj->base.size >> PAGE_SHIFT,
2054 I915_SHRINK_BOUND |
2055 I915_SHRINK_UNBOUND |
2056 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002057
Chris Wilson03ac84f2016-10-28 13:58:36 +01002058 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002059}
2060
Daniel Vetter2c642b02015-04-14 17:35:26 +02002061static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002062{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002063 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002064}
2065
Chris Wilsond6473f52016-06-10 14:22:59 +05302066static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2067 dma_addr_t addr,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002068 u64 offset,
Chris Wilsond6473f52016-06-10 14:22:59 +05302069 enum i915_cache_level level,
2070 u32 unused)
2071{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002072 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302073 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002074 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302075
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002076 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302077
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002078 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302079}
2080
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002081static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2082 struct sg_table *st,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002083 u64 start,
2084 enum i915_cache_level level,
2085 u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002086{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002087 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002088 struct sgt_iter sgt_iter;
2089 gen8_pte_t __iomem *gtt_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +00002090 const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002091 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002092
Chris Wilson894cceb2017-02-15 08:43:37 +00002093 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
2094 gtt_entries += start >> PAGE_SHIFT;
2095 for_each_sgt_dma(addr, sgt_iter, st)
2096 gen8_set_pte(gtt_entries++, pte_encode | addr);
Dave Gordon85d12252016-05-20 11:54:06 +01002097
Chris Wilson894cceb2017-02-15 08:43:37 +00002098 wmb();
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002099
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002100 /* This next bit makes the above posting read even more important. We
2101 * want to flush the TLBs only after we're certain all the PTE updates
2102 * have finished.
2103 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002104 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002105}
2106
Chris Wilsond6473f52016-06-10 14:22:59 +05302107static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2108 dma_addr_t addr,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002109 u64 offset,
Chris Wilsond6473f52016-06-10 14:22:59 +05302110 enum i915_cache_level level,
2111 u32 flags)
2112{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002113 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302114 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002115 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302116
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002117 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302118
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002119 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302120}
2121
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002122/*
2123 * Binds an object into the global gtt with the specified cache level. The object
2124 * will be accessible to the GPU via commands whose operands reference offsets
2125 * within the global GTT as well as accessible by the GPU through the GMADR
2126 * mapped BAR (dev_priv->mm.gtt->gtt).
2127 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002128static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002129 struct sg_table *st,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002130 u64 start,
2131 enum i915_cache_level level,
2132 u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002133{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002134 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsonb31144c2017-02-15 08:43:36 +00002135 gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
2136 unsigned int i = start >> PAGE_SHIFT;
2137 struct sgt_iter iter;
Dave Gordon85d12252016-05-20 11:54:06 +01002138 dma_addr_t addr;
Chris Wilsonb31144c2017-02-15 08:43:36 +00002139 for_each_sgt_dma(addr, iter, st)
2140 iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2141 wmb();
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002142
2143 /* This next bit makes the above posting read even more important. We
2144 * want to flush the TLBs only after we're certain all the PTE updates
2145 * have finished.
2146 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002147 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002148}
2149
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002150static void nop_clear_range(struct i915_address_space *vm,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002151 u64 start, u64 length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002152{
2153}
2154
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002155static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002156 u64 start, u64 length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002157{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002158 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002159 unsigned first_entry = start >> PAGE_SHIFT;
2160 unsigned num_entries = length >> PAGE_SHIFT;
Chris Wilson894cceb2017-02-15 08:43:37 +00002161 const gen8_pte_t scratch_pte =
2162 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
2163 gen8_pte_t __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002164 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2165 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002166 int i;
2167
2168 if (WARN(num_entries > max_entries,
2169 "First entry = %d; Num entries = %d (max=%d)\n",
2170 first_entry, num_entries, max_entries))
2171 num_entries = max_entries;
2172
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002173 for (i = 0; i < num_entries; i++)
2174 gen8_set_pte(&gtt_base[i], scratch_pte);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002175}
2176
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002177static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002178 u64 start, u64 length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002179{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002180 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002181 unsigned first_entry = start >> PAGE_SHIFT;
2182 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002183 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002184 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2185 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002186 int i;
2187
2188 if (WARN(num_entries > max_entries,
2189 "First entry = %d; Num entries = %d (max=%d)\n",
2190 first_entry, num_entries, max_entries))
2191 num_entries = max_entries;
2192
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002193 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002194 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002195
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002196 for (i = 0; i < num_entries; i++)
2197 iowrite32(scratch_pte, &gtt_base[i]);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002198}
2199
Chris Wilsond6473f52016-06-10 14:22:59 +05302200static void i915_ggtt_insert_page(struct i915_address_space *vm,
2201 dma_addr_t addr,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002202 u64 offset,
Chris Wilsond6473f52016-06-10 14:22:59 +05302203 enum i915_cache_level cache_level,
2204 u32 unused)
2205{
Chris Wilsond6473f52016-06-10 14:22:59 +05302206 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2207 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302208
2209 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302210}
2211
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002212static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2213 struct sg_table *pages,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002214 u64 start,
2215 enum i915_cache_level cache_level,
2216 u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002217{
2218 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2219 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2220
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002221 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002222}
2223
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002224static void i915_ggtt_clear_range(struct i915_address_space *vm,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002225 u64 start, u64 length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002226{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002227 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002228}
2229
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002230static int ggtt_bind_vma(struct i915_vma *vma,
2231 enum i915_cache_level cache_level,
2232 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002233{
Chris Wilson49d73912016-11-29 09:50:08 +00002234 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002235 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonba7a5742017-02-15 08:43:35 +00002236 u32 pte_flags;
Daniel Vetter0a878712015-10-15 14:23:01 +02002237
Chris Wilsonba7a5742017-02-15 08:43:35 +00002238 if (unlikely(!vma->pages)) {
2239 int ret = i915_get_ggtt_vma_pages(vma);
2240 if (ret)
2241 return ret;
2242 }
Daniel Vetter0a878712015-10-15 14:23:01 +02002243
2244 /* Currently applicable only to VLV */
Chris Wilsonba7a5742017-02-15 08:43:35 +00002245 pte_flags = 0;
Daniel Vetter0a878712015-10-15 14:23:01 +02002246 if (obj->gt_ro)
2247 pte_flags |= PTE_READ_ONLY;
2248
Chris Wilson9c870d02016-10-24 13:42:15 +01002249 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002250 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002251 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002252 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002253
2254 /*
2255 * Without aliasing PPGTT there's no difference between
2256 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2257 * upgrade to both bound if we bind either to avoid double-binding.
2258 */
Chris Wilson3272db52016-08-04 16:32:32 +01002259 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002260
2261 return 0;
2262}
2263
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002264static void ggtt_unbind_vma(struct i915_vma *vma)
2265{
2266 struct drm_i915_private *i915 = vma->vm->i915;
2267
2268 intel_runtime_pm_get(i915);
2269 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2270 intel_runtime_pm_put(i915);
2271}
2272
Daniel Vetter0a878712015-10-15 14:23:01 +02002273static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2274 enum i915_cache_level cache_level,
2275 u32 flags)
2276{
Chris Wilson49d73912016-11-29 09:50:08 +00002277 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002278 u32 pte_flags;
Chris Wilsonff685972017-02-15 08:43:42 +00002279 int ret;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002280
Chris Wilsonba7a5742017-02-15 08:43:35 +00002281 if (unlikely(!vma->pages)) {
Chris Wilsonff685972017-02-15 08:43:42 +00002282 ret = i915_get_ggtt_vma_pages(vma);
Chris Wilsonba7a5742017-02-15 08:43:35 +00002283 if (ret)
2284 return ret;
2285 }
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002286
Akash Goel24f3a8c2014-06-17 10:59:42 +05302287 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002288 pte_flags = 0;
2289 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002290 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302291
Chris Wilsonff685972017-02-15 08:43:42 +00002292 if (flags & I915_VMA_LOCAL_BIND) {
2293 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
2294
2295 if (appgtt->base.allocate_va_range) {
2296 ret = appgtt->base.allocate_va_range(&appgtt->base,
2297 vma->node.start,
2298 vma->node.size);
2299 if (ret)
2300 return ret;
2301 }
2302
2303 appgtt->base.insert_entries(&appgtt->base,
2304 vma->pages, vma->node.start,
2305 cache_level, pte_flags);
2306 }
2307
Chris Wilson3272db52016-08-04 16:32:32 +01002308 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002309 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002310 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002311 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002312 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002313 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002314 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002315
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002316 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002317}
2318
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002319static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002320{
Chris Wilson49d73912016-11-29 09:50:08 +00002321 struct drm_i915_private *i915 = vma->vm->i915;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002322
Chris Wilson9c870d02016-10-24 13:42:15 +01002323 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2324 intel_runtime_pm_get(i915);
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002325 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002326 intel_runtime_pm_put(i915);
2327 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002328
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002329 if (vma->flags & I915_VMA_LOCAL_BIND) {
2330 struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
2331
2332 vm->clear_range(vm, vma->node.start, vma->size);
2333 }
Daniel Vetter74163902012-02-15 23:50:21 +01002334}
2335
Chris Wilson03ac84f2016-10-28 13:58:36 +01002336void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2337 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002338{
David Weinehall52a05c32016-08-22 13:32:44 +03002339 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2340 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002341 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002342
Chris Wilson307dc252016-08-05 10:14:12 +01002343 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002344 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002345 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2346 /* Wait a bit, in hopes it avoids the hang */
2347 udelay(10);
2348 }
2349 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002350
Chris Wilson03ac84f2016-10-28 13:58:36 +01002351 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002352}
Daniel Vetter644ec022012-03-26 09:45:40 +02002353
Chris Wilson45b186f2016-12-16 07:46:42 +00002354static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002355 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002356 u64 *start,
2357 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002358{
Chris Wilsona6508de2017-02-06 08:45:47 +00002359 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002360 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002361
Chris Wilsona6508de2017-02-06 08:45:47 +00002362 /* Also leave a space between the unallocated reserved node after the
2363 * GTT and any objects within the GTT, i.e. we use the color adjustment
2364 * to insert a guard page to prevent prefetches crossing over the
2365 * GTT boundary.
2366 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002367 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002368 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002369 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002370}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002371
Chris Wilson6cde9a02017-02-13 17:15:50 +00002372int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2373{
2374 struct i915_ggtt *ggtt = &i915->ggtt;
2375 struct i915_hw_ppgtt *ppgtt;
2376 int err;
2377
Chris Wilson57202f42017-02-15 08:43:56 +00002378 ppgtt = i915_ppgtt_create(i915, ERR_PTR(-EPERM), "[alias]");
Chris Wilson1188bc62017-02-15 08:43:38 +00002379 if (IS_ERR(ppgtt))
2380 return PTR_ERR(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002381
Chris Wilsone565ceb2017-02-15 08:43:55 +00002382 if (WARN_ON(ppgtt->base.total < ggtt->base.total)) {
2383 err = -ENODEV;
2384 goto err_ppgtt;
2385 }
2386
Chris Wilson6cde9a02017-02-13 17:15:50 +00002387 if (ppgtt->base.allocate_va_range) {
Chris Wilsone565ceb2017-02-15 08:43:55 +00002388 /* Note we only pre-allocate as far as the end of the global
2389 * GTT. On 48b / 4-level page-tables, the difference is very,
2390 * very significant! We have to preallocate as GVT/vgpu does
2391 * not like the page directory disappearing.
2392 */
Chris Wilson6cde9a02017-02-13 17:15:50 +00002393 err = ppgtt->base.allocate_va_range(&ppgtt->base,
Chris Wilsone565ceb2017-02-15 08:43:55 +00002394 0, ggtt->base.total);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002395 if (err)
Chris Wilson1188bc62017-02-15 08:43:38 +00002396 goto err_ppgtt;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002397 }
2398
Chris Wilson6cde9a02017-02-13 17:15:50 +00002399 i915->mm.aliasing_ppgtt = ppgtt;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002400
Chris Wilson6cde9a02017-02-13 17:15:50 +00002401 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2402 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2403
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002404 WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
2405 ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
2406
Chris Wilson6cde9a02017-02-13 17:15:50 +00002407 return 0;
2408
Chris Wilson6cde9a02017-02-13 17:15:50 +00002409err_ppgtt:
Chris Wilson1188bc62017-02-15 08:43:38 +00002410 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002411 return err;
2412}
2413
2414void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2415{
2416 struct i915_ggtt *ggtt = &i915->ggtt;
2417 struct i915_hw_ppgtt *ppgtt;
2418
2419 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2420 if (!ppgtt)
2421 return;
2422
Chris Wilson1188bc62017-02-15 08:43:38 +00002423 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002424
2425 ggtt->base.bind_vma = ggtt_bind_vma;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002426 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002427}
2428
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002429int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002430{
Ben Widawskye78891c2013-01-25 16:41:04 -08002431 /* Let GEM Manage all of the aperture.
2432 *
2433 * However, leave one page at the end still bound to the scratch page.
2434 * There are a number of places where the hardware apparently prefetches
2435 * past the end of the object, and we've seen multiple hangs with the
2436 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2437 * aperture. One page should be enough to keep any prefetching inside
2438 * of the aperture.
2439 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002440 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002441 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002442 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002443 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002444
Zhi Wangb02d22a2016-06-16 08:06:59 -04002445 ret = intel_vgt_balloon(dev_priv);
2446 if (ret)
2447 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002448
Chris Wilson95374d72016-10-12 10:05:20 +01002449 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002450 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2451 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2452 0, ggtt->mappable_end,
2453 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002454 if (ret)
2455 return ret;
2456
Chris Wilsoned2f3452012-11-15 11:32:19 +00002457 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002458 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002459 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2460 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002461 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002462 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002463 }
2464
2465 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002466 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002467 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002468
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002469 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Chris Wilson6cde9a02017-02-13 17:15:50 +00002470 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
Chris Wilson95374d72016-10-12 10:05:20 +01002471 if (ret)
Chris Wilson6cde9a02017-02-13 17:15:50 +00002472 goto err;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002473 }
2474
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002475 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002476
Chris Wilson95374d72016-10-12 10:05:20 +01002477err:
2478 drm_mm_remove_node(&ggtt->error_capture);
2479 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002480}
2481
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002482/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002483 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002484 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002485 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002486void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002487{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002488 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002489 struct i915_vma *vma, *vn;
2490
2491 ggtt->base.closed = true;
2492
2493 mutex_lock(&dev_priv->drm.struct_mutex);
2494 WARN_ON(!list_empty(&ggtt->base.active_list));
2495 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2496 WARN_ON(i915_vma_unbind(vma));
2497 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002498
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002499 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002500
Chris Wilson1188bc62017-02-15 08:43:38 +00002501 mutex_lock(&dev_priv->drm.struct_mutex);
2502 i915_gem_fini_aliasing_ppgtt(dev_priv);
2503
Chris Wilson95374d72016-10-12 10:05:20 +01002504 if (drm_mm_node_allocated(&ggtt->error_capture))
2505 drm_mm_remove_node(&ggtt->error_capture);
2506
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002507 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002508 intel_vgt_deballoon(dev_priv);
Matthew Aulded9724d2016-11-17 21:04:10 +00002509 i915_address_space_fini(&ggtt->base);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002510 }
2511
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002512 ggtt->base.cleanup(&ggtt->base);
Chris Wilson1188bc62017-02-15 08:43:38 +00002513 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002514
2515 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002516 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002517}
Daniel Vetter70e32542014-08-06 15:04:57 +02002518
Daniel Vetter2c642b02015-04-14 17:35:26 +02002519static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002520{
2521 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2522 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2523 return snb_gmch_ctl << 20;
2524}
2525
Daniel Vetter2c642b02015-04-14 17:35:26 +02002526static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002527{
2528 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2529 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2530 if (bdw_gmch_ctl)
2531 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002532
2533#ifdef CONFIG_X86_32
2534 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2535 if (bdw_gmch_ctl > 4)
2536 bdw_gmch_ctl = 4;
2537#endif
2538
Ben Widawsky9459d252013-11-03 16:53:55 -08002539 return bdw_gmch_ctl << 20;
2540}
2541
Daniel Vetter2c642b02015-04-14 17:35:26 +02002542static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002543{
2544 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2545 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2546
2547 if (gmch_ctrl)
2548 return 1 << (20 + gmch_ctrl);
2549
2550 return 0;
2551}
2552
Daniel Vetter2c642b02015-04-14 17:35:26 +02002553static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002554{
2555 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2556 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2557 return snb_gmch_ctl << 25; /* 32 MB units */
2558}
2559
Daniel Vetter2c642b02015-04-14 17:35:26 +02002560static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002561{
2562 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2563 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2564 return bdw_gmch_ctl << 25; /* 32 MB units */
2565}
2566
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002567static size_t chv_get_stolen_size(u16 gmch_ctrl)
2568{
2569 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2570 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2571
2572 /*
2573 * 0x0 to 0x10: 32MB increments starting at 0MB
2574 * 0x11 to 0x16: 4MB increments starting at 8MB
2575 * 0x17 to 0x1d: 4MB increments start at 36MB
2576 */
2577 if (gmch_ctrl < 0x11)
2578 return gmch_ctrl << 25;
2579 else if (gmch_ctrl < 0x17)
2580 return (gmch_ctrl - 0x11 + 2) << 22;
2581 else
2582 return (gmch_ctrl - 0x17 + 9) << 22;
2583}
2584
Damien Lespiau66375012014-01-09 18:02:46 +00002585static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2586{
2587 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2588 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2589
2590 if (gen9_gmch_ctl < 0xf0)
2591 return gen9_gmch_ctl << 25; /* 32 MB units */
2592 else
2593 /* 4MB increments starting at 0xf0 for 4MB */
2594 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2595}
2596
Chris Wilson34c998b2016-08-04 07:52:24 +01002597static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002598{
Chris Wilson49d73912016-11-29 09:50:08 +00002599 struct drm_i915_private *dev_priv = ggtt->base.i915;
2600 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002601 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002602 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002603
2604 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002605 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002606
Imre Deak2a073f892015-03-27 13:07:33 +02002607 /*
2608 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2609 * dropped. For WC mappings in general we have 64 byte burst writes
2610 * when the WC buffer is flushed, so we can't use it, but have to
2611 * resort to an uncached mapping. The WC issue is easily caught by the
2612 * readback check when writing GTT PTE entries.
2613 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002614 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002615 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002616 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002617 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002618 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002619 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002620 return -ENOMEM;
2621 }
2622
Chris Wilson84486612017-02-15 08:43:40 +00002623 ret = setup_scratch_page(&ggtt->base, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002624 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002625 DRM_ERROR("Scratch setup failed\n");
2626 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002627 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002628 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002629 }
2630
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002631 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002632}
2633
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002634/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2635 * bits. When using advanced contexts each context stores its own PAT, but
2636 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002637static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002638{
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002639 u64 pat;
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002640
2641 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2642 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2643 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2644 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2645 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2646 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2647 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2648 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2649
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002650 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002651 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2652 * so RTL will always use the value corresponding to
2653 * pat_sel = 000".
2654 * So let's disable cache for GGTT to avoid screen corruptions.
2655 * MOCS still can be used though.
2656 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2657 * before this patch, i.e. the same uncached + snooping access
2658 * like on gen6/7 seems to be in effect.
2659 * - So this just fixes blitter/render access. Again it looks
2660 * like it's not just uncached access, but uncached + snooping.
2661 * So we can still hold onto all our assumptions wrt cpu
2662 * clflushing on LLC machines.
2663 */
2664 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2665
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002666 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2667 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002668 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2669 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002670}
2671
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002672static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2673{
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002674 u64 pat;
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002675
2676 /*
2677 * Map WB on BDW to snooped on CHV.
2678 *
2679 * Only the snoop bit has meaning for CHV, the rest is
2680 * ignored.
2681 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002682 * The hardware will never snoop for certain types of accesses:
2683 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2684 * - PPGTT page tables
2685 * - some other special cycles
2686 *
2687 * As with BDW, we also need to consider the following for GT accesses:
2688 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2689 * so RTL will always use the value corresponding to
2690 * pat_sel = 000".
2691 * Which means we must set the snoop bit in PAT entry 0
2692 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002693 */
2694 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2695 GEN8_PPAT(1, 0) |
2696 GEN8_PPAT(2, 0) |
2697 GEN8_PPAT(3, 0) |
2698 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2699 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2700 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2701 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2702
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002703 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2704 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002705}
2706
Chris Wilson34c998b2016-08-04 07:52:24 +01002707static void gen6_gmch_remove(struct i915_address_space *vm)
2708{
2709 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2710
2711 iounmap(ggtt->gsm);
Chris Wilson84486612017-02-15 08:43:40 +00002712 cleanup_scratch_page(vm);
Chris Wilson34c998b2016-08-04 07:52:24 +01002713}
2714
Joonas Lahtinend507d732016-03-18 10:42:58 +02002715static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08002716{
Chris Wilson49d73912016-11-29 09:50:08 +00002717 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002718 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002719 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08002720 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08002721
2722 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002723 ggtt->mappable_base = pci_resource_start(pdev, 2);
2724 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08002725
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002726 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
2727 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08002728
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002729 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08002730
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002731 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002732 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002733 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002734 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002735 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002736 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002737 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002738 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002739 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002740 }
Ben Widawsky63340132013-11-04 19:32:22 -08002741
Chris Wilson34c998b2016-08-04 07:52:24 +01002742 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002743
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002744 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002745 chv_setup_private_ppat(dev_priv);
2746 else
2747 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002748
Chris Wilson34c998b2016-08-04 07:52:24 +01002749 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002750 ggtt->base.bind_vma = ggtt_bind_vma;
2751 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05302752 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002753 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01002754 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002755 ggtt->base.clear_range = gen8_ggtt_clear_range;
2756
2757 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002758
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002759 ggtt->invalidate = gen6_ggtt_invalidate;
2760
Chris Wilson34c998b2016-08-04 07:52:24 +01002761 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08002762}
2763
Joonas Lahtinend507d732016-03-18 10:42:58 +02002764static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002765{
Chris Wilson49d73912016-11-29 09:50:08 +00002766 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002767 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002768 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002769 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002770
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002771 ggtt->mappable_base = pci_resource_start(pdev, 2);
2772 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08002773
Ben Widawskybaa09f52013-01-24 13:49:57 -08002774 /* 64/512MB is the current min/max we actually know of, but this is just
2775 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002776 */
Chris Wilson34c998b2016-08-04 07:52:24 +01002777 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002778 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002779 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002780 }
2781
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002782 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
2783 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
2784 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002785
Joonas Lahtinend507d732016-03-18 10:42:58 +02002786 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002787
Chris Wilson34c998b2016-08-04 07:52:24 +01002788 size = gen6_get_total_gtt_size(snb_gmch_ctl);
2789 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002790
Joonas Lahtinend507d732016-03-18 10:42:58 +02002791 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05302792 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002793 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
2794 ggtt->base.bind_vma = ggtt_bind_vma;
2795 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01002796 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002797
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002798 ggtt->invalidate = gen6_ggtt_invalidate;
2799
Chris Wilson34c998b2016-08-04 07:52:24 +01002800 if (HAS_EDRAM(dev_priv))
2801 ggtt->base.pte_encode = iris_pte_encode;
2802 else if (IS_HASWELL(dev_priv))
2803 ggtt->base.pte_encode = hsw_pte_encode;
2804 else if (IS_VALLEYVIEW(dev_priv))
2805 ggtt->base.pte_encode = byt_pte_encode;
2806 else if (INTEL_GEN(dev_priv) >= 7)
2807 ggtt->base.pte_encode = ivb_pte_encode;
2808 else
2809 ggtt->base.pte_encode = snb_pte_encode;
2810
2811 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002812}
2813
Chris Wilson34c998b2016-08-04 07:52:24 +01002814static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002815{
Chris Wilson34c998b2016-08-04 07:52:24 +01002816 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08002817}
2818
Joonas Lahtinend507d732016-03-18 10:42:58 +02002819static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002820{
Chris Wilson49d73912016-11-29 09:50:08 +00002821 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002822 int ret;
2823
Chris Wilson91c8a322016-07-05 10:40:23 +01002824 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002825 if (!ret) {
2826 DRM_ERROR("failed to set up gmch\n");
2827 return -EIO;
2828 }
2829
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00002830 intel_gtt_get(&ggtt->base.total,
2831 &ggtt->stolen_size,
2832 &ggtt->mappable_base,
2833 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002834
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002835 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05302836 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002837 ggtt->base.insert_entries = i915_ggtt_insert_entries;
2838 ggtt->base.clear_range = i915_ggtt_clear_range;
2839 ggtt->base.bind_vma = ggtt_bind_vma;
2840 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01002841 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002842
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002843 ggtt->invalidate = gmch_ggtt_invalidate;
2844
Joonas Lahtinend507d732016-03-18 10:42:58 +02002845 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002846 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2847
Ben Widawskybaa09f52013-01-24 13:49:57 -08002848 return 0;
2849}
2850
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002851/**
Chris Wilson0088e522016-08-04 07:52:21 +01002852 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002853 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002854 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002855int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002856{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002857 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002858 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002859
Chris Wilson49d73912016-11-29 09:50:08 +00002860 ggtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00002861 ggtt->base.dma = &dev_priv->drm.pdev->dev;
Mika Kuoppalac114f762015-06-25 18:35:13 +03002862
Chris Wilson34c998b2016-08-04 07:52:24 +01002863 if (INTEL_GEN(dev_priv) <= 5)
2864 ret = i915_gmch_probe(ggtt);
2865 else if (INTEL_GEN(dev_priv) < 8)
2866 ret = gen6_gmch_probe(ggtt);
2867 else
2868 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002869 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002870 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002871
Chris Wilsondb9309a2017-01-05 15:30:23 +00002872 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
2873 * This is easier than doing range restriction on the fly, as we
2874 * currently don't have any bits spare to pass in this upper
2875 * restriction!
2876 */
2877 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
2878 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
2879 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
2880 }
2881
Chris Wilsonc890e2d2016-03-18 10:42:59 +02002882 if ((ggtt->base.total - 1) >> 32) {
2883 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002884 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02002885 ggtt->base.total >> 20);
2886 ggtt->base.total = 1ULL << 32;
2887 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
2888 }
2889
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002890 if (ggtt->mappable_end > ggtt->base.total) {
2891 DRM_ERROR("mappable aperture extends past end of GGTT,"
2892 " aperture=%llx, total=%llx\n",
2893 ggtt->mappable_end, ggtt->base.total);
2894 ggtt->mappable_end = ggtt->base.total;
2895 }
2896
Ben Widawskybaa09f52013-01-24 13:49:57 -08002897 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002898 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002899 ggtt->base.total >> 20);
2900 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00002901 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002902#ifdef CONFIG_INTEL_IOMMU
2903 if (intel_iommu_gfx_mapped)
2904 DRM_INFO("VT-d active for gfx access\n");
2905#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002906
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002907 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01002908}
2909
2910/**
2911 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002912 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01002913 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002914int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01002915{
Chris Wilson0088e522016-08-04 07:52:21 +01002916 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2917 int ret;
2918
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002919 INIT_LIST_HEAD(&dev_priv->vm_list);
2920
Chris Wilsona6508de2017-02-06 08:45:47 +00002921 /* Note that we use page colouring to enforce a guard page at the
2922 * end of the address space. This is required as the CS may prefetch
2923 * beyond the end of the batch buffer, across the page boundary,
2924 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002925 */
Chris Wilson80b204b2016-10-28 13:58:58 +01002926 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01002927 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00002928 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002929 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01002930 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002931
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002932 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
2933 dev_priv->ggtt.mappable_base,
2934 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002935 ret = -EIO;
2936 goto out_gtt_cleanup;
2937 }
2938
2939 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
2940
Chris Wilson0088e522016-08-04 07:52:21 +01002941 /*
2942 * Initialise stolen early so that we may reserve preallocated
2943 * objects for the BIOS to KMS transition.
2944 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00002945 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01002946 if (ret)
2947 goto out_gtt_cleanup;
2948
2949 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02002950
2951out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002952 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02002953 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002954}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002955
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002956int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03002957{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002958 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03002959 return -EIO;
2960
2961 return 0;
2962}
2963
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002964void i915_ggtt_enable_guc(struct drm_i915_private *i915)
2965{
2966 i915->ggtt.invalidate = guc_ggtt_invalidate;
2967}
2968
2969void i915_ggtt_disable_guc(struct drm_i915_private *i915)
2970{
2971 i915->ggtt.invalidate = gen6_ggtt_invalidate;
2972}
2973
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002974void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02002975{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002976 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002977 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02002978
Chris Wilsondc979972016-05-10 14:10:04 +01002979 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02002980
2981 /* First fill our portion of the GTT with scratch pages */
Chris Wilson381b9432017-02-15 08:43:54 +00002982 ggtt->base.clear_range(&ggtt->base, 0, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02002983
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002984 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
2985
2986 /* clflush objects bound into the GGTT and rebind them. */
2987 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02002988 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002989 bool ggtt_bound = false;
2990 struct i915_vma *vma;
2991
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002992 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002993 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01002994 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02002995
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002996 if (!i915_vma_unbind(vma))
2997 continue;
2998
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01002999 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3000 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003001 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003002 }
3003
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003004 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003005 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003006 }
3007
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003008 ggtt->base.closed = false;
3009
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003010 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003011 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003012 chv_setup_private_ppat(dev_priv);
3013 else
3014 bdw_setup_private_ppat(dev_priv);
3015
3016 return;
3017 }
3018
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003019 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003020 struct i915_address_space *vm;
3021
Daniel Vetterfa423312015-04-14 17:35:23 +02003022 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003023 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003024
Chris Wilson2bfa9962016-08-04 07:52:25 +01003025 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003026 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003027 else
3028 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003029
Chris Wilson16a011c2017-02-15 08:43:45 +00003030 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003031 }
3032 }
3033
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003034 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003035}
3036
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003037static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003038rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003039 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003040 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003041 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003042{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003043 unsigned int column, row;
3044 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003045
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003046 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003047 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003048 for (row = 0; row < height; row++) {
3049 st->nents++;
3050 /* We don't need the pages, but need to initialize
3051 * the entries so the sg list can be happily traversed.
3052 * The only thing we need are DMA addresses.
3053 */
3054 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003055 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003056 sg_dma_len(sg) = PAGE_SIZE;
3057 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003058 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003059 }
3060 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003061
3062 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003063}
3064
Chris Wilsonba7a5742017-02-15 08:43:35 +00003065static noinline struct sg_table *
3066intel_rotate_pages(struct intel_rotation_info *rot_info,
3067 struct drm_i915_gem_object *obj)
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003068{
Chris Wilson75c7b0b2017-02-15 08:43:57 +00003069 const unsigned long n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003070 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003071 struct sgt_iter sgt_iter;
3072 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003073 unsigned long i;
3074 dma_addr_t *page_addr_list;
3075 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003076 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003077 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003078
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003079 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003080 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003081 sizeof(dma_addr_t),
3082 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003083 if (!page_addr_list)
3084 return ERR_PTR(ret);
3085
3086 /* Allocate target SG list. */
3087 st = kmalloc(sizeof(*st), GFP_KERNEL);
3088 if (!st)
3089 goto err_st_alloc;
3090
Ville Syrjälä6687c902015-09-15 13:16:41 +03003091 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003092 if (ret)
3093 goto err_sg_alloc;
3094
3095 /* Populate source page list from the object. */
3096 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003097 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003098 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003099
Dave Gordon85d12252016-05-20 11:54:06 +01003100 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003101 st->nents = 0;
3102 sg = st->sgl;
3103
Ville Syrjälä6687c902015-09-15 13:16:41 +03003104 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3105 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3106 rot_info->plane[i].width, rot_info->plane[i].height,
3107 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003108 }
3109
Ville Syrjälä6687c902015-09-15 13:16:41 +03003110 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3111 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003112
3113 drm_free_large(page_addr_list);
3114
3115 return st;
3116
3117err_sg_alloc:
3118 kfree(st);
3119err_st_alloc:
3120 drm_free_large(page_addr_list);
3121
Ville Syrjälä6687c902015-09-15 13:16:41 +03003122 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3123 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3124
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003125 return ERR_PTR(ret);
3126}
3127
Chris Wilsonba7a5742017-02-15 08:43:35 +00003128static noinline struct sg_table *
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003129intel_partial_pages(const struct i915_ggtt_view *view,
3130 struct drm_i915_gem_object *obj)
3131{
3132 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003133 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003134 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003135 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003136 int ret = -ENOMEM;
3137
3138 st = kmalloc(sizeof(*st), GFP_KERNEL);
3139 if (!st)
3140 goto err_st_alloc;
3141
Chris Wilsond2a84a72016-10-28 13:58:34 +01003142 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003143 if (ret)
3144 goto err_sg_alloc;
3145
Chris Wilson8bab11932017-01-14 00:28:25 +00003146 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003147 GEM_BUG_ON(!iter);
3148
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003149 sg = st->sgl;
3150 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003151 do {
3152 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003153
Chris Wilsond2a84a72016-10-28 13:58:34 +01003154 len = min(iter->length - (offset << PAGE_SHIFT),
3155 count << PAGE_SHIFT);
3156 sg_set_page(sg, NULL, len, 0);
3157 sg_dma_address(sg) =
3158 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3159 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003160
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003161 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003162 count -= len >> PAGE_SHIFT;
3163 if (count == 0) {
3164 sg_mark_end(sg);
3165 return st;
3166 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003167
Chris Wilsond2a84a72016-10-28 13:58:34 +01003168 sg = __sg_next(sg);
3169 iter = __sg_next(iter);
3170 offset = 0;
3171 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003172
3173err_sg_alloc:
3174 kfree(st);
3175err_st_alloc:
3176 return ERR_PTR(ret);
3177}
3178
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003179static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003180i915_get_ggtt_vma_pages(struct i915_vma *vma)
3181{
Chris Wilsonba7a5742017-02-15 08:43:35 +00003182 int ret;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003183
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003184 /* The vma->pages are only valid within the lifespan of the borrowed
3185 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3186 * must be the vma->pages. A simple rule is that vma->pages must only
3187 * be accessed when the obj->mm.pages are pinned.
3188 */
3189 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3190
Chris Wilsonba7a5742017-02-15 08:43:35 +00003191 switch (vma->ggtt_view.type) {
3192 case I915_GGTT_VIEW_NORMAL:
3193 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003194 return 0;
3195
Chris Wilsonba7a5742017-02-15 08:43:35 +00003196 case I915_GGTT_VIEW_ROTATED:
Chris Wilson247177d2016-08-15 10:48:47 +01003197 vma->pages =
Chris Wilsonba7a5742017-02-15 08:43:35 +00003198 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3199 break;
3200
3201 case I915_GGTT_VIEW_PARTIAL:
Chris Wilson247177d2016-08-15 10:48:47 +01003202 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003203 break;
3204
3205 default:
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003206 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3207 vma->ggtt_view.type);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003208 return -EINVAL;
3209 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003210
Chris Wilsonba7a5742017-02-15 08:43:35 +00003211 ret = 0;
3212 if (unlikely(IS_ERR(vma->pages))) {
Chris Wilson247177d2016-08-15 10:48:47 +01003213 ret = PTR_ERR(vma->pages);
3214 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003215 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3216 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003217 }
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003218 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003219}
3220
Chris Wilsone007b192017-01-11 11:23:10 +00003221/**
Chris Wilson625d9882017-01-11 11:23:11 +00003222 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003223 * @vm: the &struct i915_address_space
3224 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3225 * @size: how much space to allocate inside the GTT,
3226 * must be #I915_GTT_PAGE_SIZE aligned
3227 * @offset: where to insert inside the GTT,
3228 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3229 * (@offset + @size) must fit within the address space
3230 * @color: color to apply to node, if this node is not from a VMA,
3231 * color must be #I915_COLOR_UNEVICTABLE
3232 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003233 *
3234 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3235 * the address space (using @size and @color). If the @node does not fit, it
3236 * tries to evict any overlapping nodes from the GTT, including any
3237 * neighbouring nodes if the colors do not match (to ensure guard pages between
3238 * differing domains). See i915_gem_evict_for_node() for the gory details
3239 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3240 * evicting active overlapping objects, and any overlapping node that is pinned
3241 * or marked as unevictable will also result in failure.
3242 *
3243 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3244 * asked to wait for eviction and interrupted.
3245 */
3246int i915_gem_gtt_reserve(struct i915_address_space *vm,
3247 struct drm_mm_node *node,
3248 u64 size, u64 offset, unsigned long color,
3249 unsigned int flags)
3250{
3251 int err;
3252
3253 GEM_BUG_ON(!size);
3254 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3255 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3256 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003257 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003258 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003259
3260 node->size = size;
3261 node->start = offset;
3262 node->color = color;
3263
3264 err = drm_mm_reserve_node(&vm->mm, node);
3265 if (err != -ENOSPC)
3266 return err;
3267
3268 err = i915_gem_evict_for_node(vm, node, flags);
3269 if (err == 0)
3270 err = drm_mm_reserve_node(&vm->mm, node);
3271
3272 return err;
3273}
3274
Chris Wilson606fec92017-01-11 11:23:12 +00003275static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3276{
3277 u64 range, addr;
3278
3279 GEM_BUG_ON(range_overflows(start, len, end));
3280 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3281
3282 range = round_down(end - len, align) - round_up(start, align);
3283 if (range) {
3284 if (sizeof(unsigned long) == sizeof(u64)) {
3285 addr = get_random_long();
3286 } else {
3287 addr = get_random_int();
3288 if (range > U32_MAX) {
3289 addr <<= 32;
3290 addr |= get_random_int();
3291 }
3292 }
3293 div64_u64_rem(addr, range, &addr);
3294 start += addr;
3295 }
3296
3297 return round_up(start, align);
3298}
3299
Chris Wilson625d9882017-01-11 11:23:11 +00003300/**
Chris Wilsone007b192017-01-11 11:23:10 +00003301 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003302 * @vm: the &struct i915_address_space
3303 * @node: the &struct drm_mm_node (typically i915_vma.node)
3304 * @size: how much space to allocate inside the GTT,
3305 * must be #I915_GTT_PAGE_SIZE aligned
3306 * @alignment: required alignment of starting offset, may be 0 but
3307 * if specified, this must be a power-of-two and at least
3308 * #I915_GTT_MIN_ALIGNMENT
3309 * @color: color to apply to node
3310 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003311 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003312 * @end: end of any range restriction inside GTT (U64_MAX for all),
3313 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3314 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003315 *
3316 * i915_gem_gtt_insert() first searches for an available hole into which
3317 * is can insert the node. The hole address is aligned to @alignment and
3318 * its @size must then fit entirely within the [@start, @end] bounds. The
3319 * nodes on either side of the hole must match @color, or else a guard page
3320 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003321 * suitable hole is found, first a victim is randomly selected and tested
3322 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003323 * is scanned to find the first set of replacement nodes to create the hole.
3324 * Those old overlapping nodes are evicted from the GTT (and so must be
3325 * rebound before any future use). Any node that is currently pinned cannot
3326 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3327 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3328 * searching for an eviction candidate. See i915_gem_evict_something() for
3329 * the gory details on the eviction algorithm.
3330 *
3331 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3332 * asked to wait for eviction and interrupted.
3333 */
3334int i915_gem_gtt_insert(struct i915_address_space *vm,
3335 struct drm_mm_node *node,
3336 u64 size, u64 alignment, unsigned long color,
3337 u64 start, u64 end, unsigned int flags)
3338{
Chris Wilson4e64e552017-02-02 21:04:38 +00003339 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003340 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003341 int err;
3342
3343 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3344 GEM_BUG_ON(!size);
3345 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3346 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3347 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3348 GEM_BUG_ON(start >= end);
3349 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3350 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003351 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003352 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003353
3354 if (unlikely(range_overflows(start, size, end)))
3355 return -ENOSPC;
3356
3357 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3358 return -ENOSPC;
3359
Chris Wilson4e64e552017-02-02 21:04:38 +00003360 mode = DRM_MM_INSERT_BEST;
3361 if (flags & PIN_HIGH)
3362 mode = DRM_MM_INSERT_HIGH;
3363 if (flags & PIN_MAPPABLE)
3364 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003365
3366 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3367 * so we know that we always have a minimum alignment of 4096.
3368 * The drm_mm range manager is optimised to return results
3369 * with zero alignment, so where possible use the optimal
3370 * path.
3371 */
3372 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3373 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3374 alignment = 0;
3375
Chris Wilson4e64e552017-02-02 21:04:38 +00003376 err = drm_mm_insert_node_in_range(&vm->mm, node,
3377 size, alignment, color,
3378 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003379 if (err != -ENOSPC)
3380 return err;
3381
Chris Wilson606fec92017-01-11 11:23:12 +00003382 /* No free space, pick a slot at random.
3383 *
3384 * There is a pathological case here using a GTT shared between
3385 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3386 *
3387 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3388 * (64k objects) (448k objects)
3389 *
3390 * Now imagine that the eviction LRU is ordered top-down (just because
3391 * pathology meets real life), and that we need to evict an object to
3392 * make room inside the aperture. The eviction scan then has to walk
3393 * the 448k list before it finds one within range. And now imagine that
3394 * it has to search for a new hole between every byte inside the memcpy,
3395 * for several simultaneous clients.
3396 *
3397 * On a full-ppgtt system, if we have run out of available space, there
3398 * will be lots and lots of objects in the eviction list! Again,
3399 * searching that LRU list may be slow if we are also applying any
3400 * range restrictions (e.g. restriction to low 4GiB) and so, for
3401 * simplicity and similarilty between different GTT, try the single
3402 * random replacement first.
3403 */
3404 offset = random_offset(start, end,
3405 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3406 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3407 if (err != -ENOSPC)
3408 return err;
3409
3410 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003411 err = i915_gem_evict_something(vm, size, alignment, color,
3412 start, end, flags);
3413 if (err)
3414 return err;
3415
Chris Wilson4e64e552017-02-02 21:04:38 +00003416 return drm_mm_insert_node_in_range(&vm->mm, node,
3417 size, alignment, color,
3418 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003419}
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003420
3421#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3422#include "selftests/mock_gtt.c"
Chris Wilson1c428192017-02-13 17:15:38 +00003423#include "selftests/i915_gem_gtt.c"
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003424#endif