blob: 5299600b473378bf4d4c582aedeed221bd4f2461 [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{
Mika Kuoppala3e490042017-02-28 17:28:07 +0200531 const unsigned int pdpes = i915_pdpes_per_pdp(vm);
Chris Wilsone2b763c2017-02-15 08:43:48 +0000532 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 Wilsonbf75d592017-02-27 12:26:52 +0000719 GEM_BUG_ON(pt == vm->scratch_pt);
720
Chris Wilsondd196742017-02-15 08:43:46 +0000721 if (!gen8_ppgtt_clear_pt(vm, pt, start, length))
722 continue;
Ben Widawsky06fda602015-02-24 16:22:36 +0000723
Chris Wilsondd196742017-02-15 08:43:46 +0000724 gen8_ppgtt_set_pde(vm, pd, vm->scratch_pt, pde);
Chris Wilsonbf75d592017-02-27 12:26:52 +0000725 GEM_BUG_ON(!pd->used_pdes);
Chris Wilsonfe52e372017-02-15 08:43:47 +0000726 pd->used_pdes--;
Chris Wilsondd196742017-02-15 08:43:46 +0000727
728 free_pt(vm, pt);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200729 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200730
Chris Wilsonfe52e372017-02-15 08:43:47 +0000731 return !pd->used_pdes;
732}
Michał Winiarski2ce51792016-10-13 14:02:42 +0200733
Chris Wilsonfe52e372017-02-15 08:43:47 +0000734static void gen8_ppgtt_set_pdpe(struct i915_address_space *vm,
735 struct i915_page_directory_pointer *pdp,
736 struct i915_page_directory *pd,
737 unsigned int pdpe)
738{
739 gen8_ppgtt_pdpe_t *vaddr;
740
741 pdp->page_directory[pdpe] = pd;
742 if (!USES_FULL_48BIT_PPGTT(vm->i915))
743 return;
744
745 vaddr = kmap_atomic_px(pdp);
746 vaddr[pdpe] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
747 kunmap_atomic(vaddr);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200748}
Ben Widawsky06fda602015-02-24 16:22:36 +0000749
Michał Winiarski2ce51792016-10-13 14:02:42 +0200750/* Removes entries from a single page dir pointer, releasing it if it's empty.
751 * Caller can use the return value to update higher-level entries
752 */
753static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200754 struct i915_page_directory_pointer *pdp,
Chris Wilsonfe52e372017-02-15 08:43:47 +0000755 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200756{
757 struct i915_page_directory *pd;
Chris Wilsonfe52e372017-02-15 08:43:47 +0000758 unsigned int pdpe;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200759
760 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilsonbf75d592017-02-27 12:26:52 +0000761 GEM_BUG_ON(pd == vm->scratch_pd);
762
Chris Wilsonfe52e372017-02-15 08:43:47 +0000763 if (!gen8_ppgtt_clear_pd(vm, pd, start, length))
764 continue;
Ben Widawsky06fda602015-02-24 16:22:36 +0000765
Chris Wilsonfe52e372017-02-15 08:43:47 +0000766 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
Chris Wilsonbf75d592017-02-27 12:26:52 +0000767 GEM_BUG_ON(!pdp->used_pdpes);
Chris Wilsone2b763c2017-02-15 08:43:48 +0000768 pdp->used_pdpes--;
Chris Wilsonfe52e372017-02-15 08:43:47 +0000769
770 free_pd(vm, pd);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200771 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200772
Chris Wilsone2b763c2017-02-15 08:43:48 +0000773 return !pdp->used_pdpes;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200774}
Ben Widawsky459108b2013-11-02 21:07:23 -0700775
Chris Wilsonfe52e372017-02-15 08:43:47 +0000776static void gen8_ppgtt_clear_3lvl(struct i915_address_space *vm,
777 u64 start, u64 length)
778{
779 gen8_ppgtt_clear_pdp(vm, &i915_vm_to_ppgtt(vm)->pdp, start, length);
780}
781
Chris Wilsone2b763c2017-02-15 08:43:48 +0000782static void gen8_ppgtt_set_pml4e(struct i915_pml4 *pml4,
783 struct i915_page_directory_pointer *pdp,
784 unsigned int pml4e)
785{
786 gen8_ppgtt_pml4e_t *vaddr;
787
788 pml4->pdps[pml4e] = pdp;
789
790 vaddr = kmap_atomic_px(pml4);
791 vaddr[pml4e] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
792 kunmap_atomic(vaddr);
793}
794
Michał Winiarski2ce51792016-10-13 14:02:42 +0200795/* Removes entries from a single pml4.
796 * This is the top-level structure in 4-level page tables used on gen8+.
797 * Empty entries are always scratch pml4e.
798 */
Chris Wilsonfe52e372017-02-15 08:43:47 +0000799static void gen8_ppgtt_clear_4lvl(struct i915_address_space *vm,
800 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200801{
Chris Wilsonfe52e372017-02-15 08:43:47 +0000802 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
803 struct i915_pml4 *pml4 = &ppgtt->pml4;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200804 struct i915_page_directory_pointer *pdp;
Chris Wilsone2b763c2017-02-15 08:43:48 +0000805 unsigned int pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200806
Chris Wilson49d73912016-11-29 09:50:08 +0000807 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700808
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200809 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonbf75d592017-02-27 12:26:52 +0000810 GEM_BUG_ON(pdp == vm->scratch_pdp);
811
Chris Wilsone2b763c2017-02-15 08:43:48 +0000812 if (!gen8_ppgtt_clear_pdp(vm, pdp, start, length))
813 continue;
Ben Widawsky459108b2013-11-02 21:07:23 -0700814
Chris Wilsone2b763c2017-02-15 08:43:48 +0000815 gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
Chris Wilsone2b763c2017-02-15 08:43:48 +0000816
817 free_pdp(vm, pdp);
Ben Widawsky459108b2013-11-02 21:07:23 -0700818 }
819}
820
Chris Wilson894cceb2017-02-15 08:43:37 +0000821struct sgt_dma {
822 struct scatterlist *sg;
823 dma_addr_t dma, max;
824};
825
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000826struct gen8_insert_pte {
827 u16 pml4e;
828 u16 pdpe;
829 u16 pde;
830 u16 pte;
831};
832
833static __always_inline struct gen8_insert_pte gen8_insert_pte(u64 start)
834{
835 return (struct gen8_insert_pte) {
836 gen8_pml4e_index(start),
837 gen8_pdpe_index(start),
838 gen8_pde_index(start),
839 gen8_pte_index(start),
840 };
841}
842
Chris Wilson894cceb2017-02-15 08:43:37 +0000843static __always_inline bool
844gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100845 struct i915_page_directory_pointer *pdp,
Chris Wilson894cceb2017-02-15 08:43:37 +0000846 struct sgt_dma *iter,
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000847 struct gen8_insert_pte *idx,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100848 enum i915_cache_level cache_level)
849{
Chris Wilson894cceb2017-02-15 08:43:37 +0000850 struct i915_page_directory *pd;
851 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
852 gen8_pte_t *vaddr;
853 bool ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700854
Mika Kuoppala3e490042017-02-28 17:28:07 +0200855 GEM_BUG_ON(idx->pdpe >= i915_pdpes_per_pdp(&ppgtt->base));
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000856 pd = pdp->page_directory[idx->pdpe];
857 vaddr = kmap_atomic_px(pd->page_table[idx->pde]);
Chris Wilson894cceb2017-02-15 08:43:37 +0000858 do {
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000859 vaddr[idx->pte] = pte_encode | iter->dma;
860
Chris Wilson894cceb2017-02-15 08:43:37 +0000861 iter->dma += PAGE_SIZE;
862 if (iter->dma >= iter->max) {
863 iter->sg = __sg_next(iter->sg);
864 if (!iter->sg) {
865 ret = false;
866 break;
867 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700868
Chris Wilson894cceb2017-02-15 08:43:37 +0000869 iter->dma = sg_dma_address(iter->sg);
870 iter->max = iter->dma + iter->sg->length;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000871 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800872
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000873 if (++idx->pte == GEN8_PTES) {
874 idx->pte = 0;
875
876 if (++idx->pde == I915_PDES) {
877 idx->pde = 0;
878
Chris Wilson894cceb2017-02-15 08:43:37 +0000879 /* Limited by sg length for 3lvl */
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000880 if (++idx->pdpe == GEN8_PML4ES_PER_PML4) {
881 idx->pdpe = 0;
Chris Wilson894cceb2017-02-15 08:43:37 +0000882 ret = true;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100883 break;
Chris Wilson894cceb2017-02-15 08:43:37 +0000884 }
885
Mika Kuoppala3e490042017-02-28 17:28:07 +0200886 GEM_BUG_ON(idx->pdpe >= i915_pdpes_per_pdp(&ppgtt->base));
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000887 pd = pdp->page_directory[idx->pdpe];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800888 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000889
Chris Wilson9231da72017-02-15 08:43:41 +0000890 kunmap_atomic(vaddr);
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000891 vaddr = kmap_atomic_px(pd->page_table[idx->pde]);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700892 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000893 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +0000894 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300895
Chris Wilson894cceb2017-02-15 08:43:37 +0000896 return ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700897}
898
Chris Wilson894cceb2017-02-15 08:43:37 +0000899static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
900 struct sg_table *pages,
901 u64 start,
902 enum i915_cache_level cache_level,
903 u32 unused)
Michel Thierryf9b5b782015-07-30 11:02:49 +0100904{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300905 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilson894cceb2017-02-15 08:43:37 +0000906 struct sgt_dma iter = {
907 .sg = pages->sgl,
908 .dma = sg_dma_address(iter.sg),
909 .max = iter.dma + iter.sg->length,
910 };
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000911 struct gen8_insert_pte idx = gen8_insert_pte(start);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100912
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000913 gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter, &idx,
914 cache_level);
Chris Wilson894cceb2017-02-15 08:43:37 +0000915}
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100916
Chris Wilson894cceb2017-02-15 08:43:37 +0000917static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
918 struct sg_table *pages,
Chris Wilson75c7b0b2017-02-15 08:43:57 +0000919 u64 start,
Chris Wilson894cceb2017-02-15 08:43:37 +0000920 enum i915_cache_level cache_level,
921 u32 unused)
922{
923 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
924 struct sgt_dma iter = {
925 .sg = pages->sgl,
926 .dma = sg_dma_address(iter.sg),
927 .max = iter.dma + iter.sg->length,
928 };
929 struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000930 struct gen8_insert_pte idx = gen8_insert_pte(start);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100931
Chris Wilson9e89f9e2017-02-25 18:11:22 +0000932 while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[idx.pml4e++], &iter,
933 &idx, cache_level))
934 GEM_BUG_ON(idx.pml4e >= GEN8_PML4ES_PER_PML4);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100935}
936
Chris Wilson84486612017-02-15 08:43:40 +0000937static void gen8_free_page_tables(struct i915_address_space *vm,
Michel Thierryf37c0502015-06-10 17:46:39 +0100938 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800939{
940 int i;
941
Mika Kuoppala567047b2015-06-25 18:35:12 +0300942 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800943 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800944
Chris Wilsonfe52e372017-02-15 08:43:47 +0000945 for (i = 0; i < I915_PDES; i++) {
946 if (pd->page_table[i] != vm->scratch_pt)
947 free_pt(vm, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000948 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000949}
950
Mika Kuoppala8776f022015-06-30 18:16:40 +0300951static int gen8_init_scratch(struct i915_address_space *vm)
952{
Matthew Auld64c050d2016-04-27 13:19:25 +0100953 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300954
Chris Wilson84486612017-02-15 08:43:40 +0000955 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100956 if (ret)
957 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300958
Chris Wilson84486612017-02-15 08:43:40 +0000959 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300960 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100961 ret = PTR_ERR(vm->scratch_pt);
962 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300963 }
964
Chris Wilson84486612017-02-15 08:43:40 +0000965 vm->scratch_pd = alloc_pd(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300966 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100967 ret = PTR_ERR(vm->scratch_pd);
968 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300969 }
970
Chris Wilson84486612017-02-15 08:43:40 +0000971 if (USES_FULL_48BIT_PPGTT(dev)) {
972 vm->scratch_pdp = alloc_pdp(vm);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100973 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100974 ret = PTR_ERR(vm->scratch_pdp);
975 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100976 }
977 }
978
Mika Kuoppala8776f022015-06-30 18:16:40 +0300979 gen8_initialize_pt(vm, vm->scratch_pt);
980 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000981 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100982 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300983
984 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100985
986free_pd:
Chris Wilson84486612017-02-15 08:43:40 +0000987 free_pd(vm, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +0100988free_pt:
Chris Wilson84486612017-02-15 08:43:40 +0000989 free_pt(vm, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +0100990free_scratch_page:
Chris Wilson84486612017-02-15 08:43:40 +0000991 cleanup_scratch_page(vm);
Matthew Auld64c050d2016-04-27 13:19:25 +0100992
993 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300994}
995
Zhiyuan Lv650da342015-08-28 15:41:18 +0800996static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
997{
998 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +0000999 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001000 int i;
1001
Matthew Aulddf285642016-04-22 12:09:25 +01001002 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001003 u64 daddr = px_dma(&ppgtt->pml4);
1004
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001005 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1006 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001007
1008 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1009 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1010 } else {
1011 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1012 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1013
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001014 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1015 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001016 }
1017
1018 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1019 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1020 }
1021
1022 I915_WRITE(vgtif_reg(g2v_notify), msg);
1023
1024 return 0;
1025}
1026
Mika Kuoppala8776f022015-06-30 18:16:40 +03001027static void gen8_free_scratch(struct i915_address_space *vm)
1028{
Chris Wilson84486612017-02-15 08:43:40 +00001029 if (USES_FULL_48BIT_PPGTT(vm->i915))
1030 free_pdp(vm, vm->scratch_pdp);
1031 free_pd(vm, vm->scratch_pd);
1032 free_pt(vm, vm->scratch_pt);
1033 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001034}
1035
Chris Wilson84486612017-02-15 08:43:40 +00001036static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
Michel Thierry762d9932015-07-30 11:05:29 +01001037 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001038{
Mika Kuoppala3e490042017-02-28 17:28:07 +02001039 const unsigned int pdpes = i915_pdpes_per_pdp(vm);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001040 int i;
1041
Mika Kuoppala3e490042017-02-28 17:28:07 +02001042 for (i = 0; i < pdpes; i++) {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001043 if (pdp->page_directory[i] == vm->scratch_pd)
Ben Widawsky06fda602015-02-24 16:22:36 +00001044 continue;
1045
Chris Wilson84486612017-02-15 08:43:40 +00001046 gen8_free_page_tables(vm, pdp->page_directory[i]);
1047 free_pd(vm, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001048 }
Michel Thierry69876be2015-04-08 12:13:27 +01001049
Chris Wilson84486612017-02-15 08:43:40 +00001050 free_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001051}
1052
1053static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1054{
1055 int i;
1056
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001057 for (i = 0; i < GEN8_PML4ES_PER_PML4; i++) {
1058 if (ppgtt->pml4.pdps[i] == ppgtt->base.scratch_pdp)
Michel Thierry762d9932015-07-30 11:05:29 +01001059 continue;
1060
Chris Wilson84486612017-02-15 08:43:40 +00001061 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001062 }
1063
Chris Wilson84486612017-02-15 08:43:40 +00001064 cleanup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001065}
1066
1067static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1068{
Chris Wilson49d73912016-11-29 09:50:08 +00001069 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001070 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001071
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001072 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001073 gen8_ppgtt_notify_vgt(ppgtt, false);
1074
Chris Wilson84486612017-02-15 08:43:40 +00001075 if (!USES_FULL_48BIT_PPGTT(vm->i915))
1076 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001077 else
1078 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001079
Mika Kuoppala8776f022015-06-30 18:16:40 +03001080 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001081}
1082
Chris Wilsonfe52e372017-02-15 08:43:47 +00001083static int gen8_ppgtt_alloc_pd(struct i915_address_space *vm,
1084 struct i915_page_directory *pd,
1085 u64 start, u64 length)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001086{
Michel Thierryd7b26332015-04-08 12:13:34 +01001087 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +00001088 u64 from = start;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001089 unsigned int pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001090
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001091 gen8_for_each_pde(pt, pd, start, length, pde) {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001092 if (pt == vm->scratch_pt) {
Chris Wilsondd196742017-02-15 08:43:46 +00001093 pt = alloc_pt(vm);
1094 if (IS_ERR(pt))
1095 goto unwind;
1096
1097 gen8_initialize_pt(vm, pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001098
Chris Wilsonfe52e372017-02-15 08:43:47 +00001099 gen8_ppgtt_set_pde(vm, pd, pt, pde);
1100 pd->used_pdes++;
Chris Wilsonbf75d592017-02-27 12:26:52 +00001101 GEM_BUG_ON(pd->used_pdes > I915_PDES);
Chris Wilsonfe52e372017-02-15 08:43:47 +00001102 }
1103
1104 pt->used_ptes += gen8_pte_count(start, length);
1105 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001106 return 0;
1107
Chris Wilsondd196742017-02-15 08:43:46 +00001108unwind:
1109 gen8_ppgtt_clear_pd(vm, pd, from, start - from);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001110 return -ENOMEM;
1111}
1112
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001113static int gen8_ppgtt_alloc_pdp(struct i915_address_space *vm,
1114 struct i915_page_directory_pointer *pdp,
1115 u64 start, u64 length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001116{
Michel Thierry5441f0c2015-04-08 12:13:28 +01001117 struct i915_page_directory *pd;
Chris Wilsone2b763c2017-02-15 08:43:48 +00001118 u64 from = start;
1119 unsigned int pdpe;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001120 int ret;
1121
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001122 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilsone2b763c2017-02-15 08:43:48 +00001123 if (pd == vm->scratch_pd) {
1124 pd = alloc_pd(vm);
1125 if (IS_ERR(pd))
1126 goto unwind;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001127
Chris Wilsone2b763c2017-02-15 08:43:48 +00001128 gen8_initialize_pd(vm, pd);
Chris Wilsonfe52e372017-02-15 08:43:47 +00001129 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
Chris Wilsone2b763c2017-02-15 08:43:48 +00001130 pdp->used_pdpes++;
Mika Kuoppala3e490042017-02-28 17:28:07 +02001131 GEM_BUG_ON(pdp->used_pdpes > i915_pdpes_per_pdp(vm));
Chris Wilson75afcf72017-02-15 08:43:51 +00001132
1133 mark_tlbs_dirty(i915_vm_to_ppgtt(vm));
Chris Wilsone2b763c2017-02-15 08:43:48 +00001134 }
1135
1136 ret = gen8_ppgtt_alloc_pd(vm, pd, start, length);
Chris Wilsonbf75d592017-02-27 12:26:52 +00001137 if (unlikely(ret))
1138 goto unwind_pd;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001139 }
Michel Thierry33c88192015-04-08 12:13:33 +01001140
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001141 return 0;
1142
Chris Wilsonbf75d592017-02-27 12:26:52 +00001143unwind_pd:
1144 if (!pd->used_pdes) {
1145 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1146 GEM_BUG_ON(!pdp->used_pdpes);
1147 pdp->used_pdpes--;
1148 free_pd(vm, pd);
1149 }
Chris Wilsone2b763c2017-02-15 08:43:48 +00001150unwind:
1151 gen8_ppgtt_clear_pdp(vm, pdp, from, start - from);
1152 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001153}
1154
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001155static int gen8_ppgtt_alloc_3lvl(struct i915_address_space *vm,
1156 u64 start, u64 length)
Michel Thierry762d9932015-07-30 11:05:29 +01001157{
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001158 return gen8_ppgtt_alloc_pdp(vm,
1159 &i915_vm_to_ppgtt(vm)->pdp, start, length);
1160}
1161
1162static int gen8_ppgtt_alloc_4lvl(struct i915_address_space *vm,
1163 u64 start, u64 length)
1164{
1165 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1166 struct i915_pml4 *pml4 = &ppgtt->pml4;
Michel Thierry762d9932015-07-30 11:05:29 +01001167 struct i915_page_directory_pointer *pdp;
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001168 u64 from = start;
1169 u32 pml4e;
1170 int ret;
Michel Thierry762d9932015-07-30 11:05:29 +01001171
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001172 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001173 if (pml4->pdps[pml4e] == vm->scratch_pdp) {
1174 pdp = alloc_pdp(vm);
1175 if (IS_ERR(pdp))
1176 goto unwind;
Michel Thierry762d9932015-07-30 11:05:29 +01001177
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001178 gen8_initialize_pdp(vm, pdp);
1179 gen8_ppgtt_set_pml4e(pml4, pdp, pml4e);
1180 }
Michel Thierry762d9932015-07-30 11:05:29 +01001181
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001182 ret = gen8_ppgtt_alloc_pdp(vm, pdp, start, length);
Chris Wilsonbf75d592017-02-27 12:26:52 +00001183 if (unlikely(ret))
1184 goto unwind_pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001185 }
1186
Michel Thierry762d9932015-07-30 11:05:29 +01001187 return 0;
1188
Chris Wilsonbf75d592017-02-27 12:26:52 +00001189unwind_pdp:
1190 if (!pdp->used_pdpes) {
1191 gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
1192 free_pdp(vm, pdp);
1193 }
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001194unwind:
1195 gen8_ppgtt_clear_4lvl(vm, from, start - from);
1196 return -ENOMEM;
Michel Thierry762d9932015-07-30 11:05:29 +01001197}
1198
Chris Wilson84486612017-02-15 08:43:40 +00001199static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
1200 struct i915_page_directory_pointer *pdp,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001201 u64 start, u64 length,
Michel Thierryea91e402015-07-29 17:23:57 +01001202 gen8_pte_t scratch_pte,
1203 struct seq_file *m)
1204{
Mika Kuoppala3e490042017-02-28 17:28:07 +02001205 struct i915_address_space *vm = &ppgtt->base;
Michel Thierryea91e402015-07-29 17:23:57 +01001206 struct i915_page_directory *pd;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001207 u32 pdpe;
Michel Thierryea91e402015-07-29 17:23:57 +01001208
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001209 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001210 struct i915_page_table *pt;
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001211 u64 pd_len = length;
1212 u64 pd_start = start;
1213 u32 pde;
Michel Thierryea91e402015-07-29 17:23:57 +01001214
Chris Wilsone2b763c2017-02-15 08:43:48 +00001215 if (pdp->page_directory[pdpe] == ppgtt->base.scratch_pd)
Michel Thierryea91e402015-07-29 17:23:57 +01001216 continue;
1217
1218 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001219 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001220 u32 pte;
Michel Thierryea91e402015-07-29 17:23:57 +01001221 gen8_pte_t *pt_vaddr;
1222
Chris Wilsonfe52e372017-02-15 08:43:47 +00001223 if (pd->page_table[pde] == ppgtt->base.scratch_pt)
Michel Thierryea91e402015-07-29 17:23:57 +01001224 continue;
1225
Chris Wilson9231da72017-02-15 08:43:41 +00001226 pt_vaddr = kmap_atomic_px(pt);
Michel Thierryea91e402015-07-29 17:23:57 +01001227 for (pte = 0; pte < GEN8_PTES; pte += 4) {
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001228 u64 va = (pdpe << GEN8_PDPE_SHIFT |
1229 pde << GEN8_PDE_SHIFT |
1230 pte << GEN8_PTE_SHIFT);
Michel Thierryea91e402015-07-29 17:23:57 +01001231 int i;
1232 bool found = false;
1233
1234 for (i = 0; i < 4; i++)
1235 if (pt_vaddr[pte + i] != scratch_pte)
1236 found = true;
1237 if (!found)
1238 continue;
1239
1240 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1241 for (i = 0; i < 4; i++) {
1242 if (pt_vaddr[pte + i] != scratch_pte)
1243 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1244 else
1245 seq_puts(m, " SCRATCH ");
1246 }
1247 seq_puts(m, "\n");
1248 }
Michel Thierryea91e402015-07-29 17:23:57 +01001249 kunmap_atomic(pt_vaddr);
1250 }
1251 }
1252}
1253
1254static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1255{
1256 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson894cceb2017-02-15 08:43:37 +00001257 const gen8_pte_t scratch_pte =
1258 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
Chris Wilson381b9432017-02-15 08:43:54 +00001259 u64 start = 0, length = ppgtt->base.total;
Michel Thierryea91e402015-07-29 17:23:57 +01001260
Chris Wilsonc6385c92016-11-29 12:42:05 +00001261 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Chris Wilson84486612017-02-15 08:43:40 +00001262 gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001263 } else {
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001264 u64 pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001265 struct i915_pml4 *pml4 = &ppgtt->pml4;
1266 struct i915_page_directory_pointer *pdp;
1267
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001268 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001269 if (pml4->pdps[pml4e] == ppgtt->base.scratch_pdp)
Michel Thierryea91e402015-07-29 17:23:57 +01001270 continue;
1271
1272 seq_printf(m, " PML4E #%llu\n", pml4e);
Chris Wilson84486612017-02-15 08:43:40 +00001273 gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001274 }
1275 }
1276}
1277
Chris Wilsone2b763c2017-02-15 08:43:48 +00001278static int gen8_preallocate_top_level_pdp(struct i915_hw_ppgtt *ppgtt)
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001279{
Chris Wilsone2b763c2017-02-15 08:43:48 +00001280 struct i915_address_space *vm = &ppgtt->base;
1281 struct i915_page_directory_pointer *pdp = &ppgtt->pdp;
1282 struct i915_page_directory *pd;
1283 u64 start = 0, length = ppgtt->base.total;
1284 u64 from = start;
1285 unsigned int pdpe;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001286
Chris Wilsone2b763c2017-02-15 08:43:48 +00001287 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1288 pd = alloc_pd(vm);
1289 if (IS_ERR(pd))
1290 goto unwind;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001291
Chris Wilsone2b763c2017-02-15 08:43:48 +00001292 gen8_initialize_pd(vm, pd);
1293 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
1294 pdp->used_pdpes++;
1295 }
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001296
Chris Wilsone2b763c2017-02-15 08:43:48 +00001297 pdp->used_pdpes++; /* never remove */
1298 return 0;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001299
Chris Wilsone2b763c2017-02-15 08:43:48 +00001300unwind:
1301 start -= from;
1302 gen8_for_each_pdpe(pd, pdp, from, start, pdpe) {
1303 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1304 free_pd(vm, pd);
1305 }
1306 pdp->used_pdpes = 0;
1307 return -ENOMEM;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001308}
1309
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001310/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001311 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1312 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1313 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1314 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001315 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001316 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001317static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001318{
Chris Wilson49d73912016-11-29 09:50:08 +00001319 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001320 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001321
Mika Kuoppala8776f022015-06-30 18:16:40 +03001322 ret = gen8_init_scratch(&ppgtt->base);
1323 if (ret)
1324 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001325
Michel Thierryd7b26332015-04-08 12:13:34 +01001326 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001327 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1328 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001329 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001330
Chris Wilson84486612017-02-15 08:43:40 +00001331 /* There are only few exceptions for gen >=6. chv and bxt.
1332 * And we are not sure about the latter so play safe for now.
1333 */
1334 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
1335 ppgtt->base.pt_kmap_wc = true;
1336
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001337 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Chris Wilson84486612017-02-15 08:43:40 +00001338 ret = setup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001339 if (ret)
1340 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001341
Michel Thierry69ab76f2015-07-29 17:23:55 +01001342 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1343
Michel Thierry762d9932015-07-30 11:05:29 +01001344 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001345 ppgtt->switch_mm = gen8_48b_mm_switch;
Chris Wilson894cceb2017-02-15 08:43:37 +00001346
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001347 ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_4lvl;
Chris Wilson894cceb2017-02-15 08:43:37 +00001348 ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001349 ppgtt->base.clear_range = gen8_ppgtt_clear_4lvl;
Michel Thierry762d9932015-07-30 11:05:29 +01001350 } else {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001351 ret = __pdp_init(&ppgtt->base, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001352 if (ret)
1353 goto free_scratch;
1354
1355 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001356 ppgtt->switch_mm = gen8_legacy_mm_switch;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001357
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001358 if (intel_vgpu_active(dev_priv)) {
Chris Wilsone2b763c2017-02-15 08:43:48 +00001359 ret = gen8_preallocate_top_level_pdp(ppgtt);
1360 if (ret) {
1361 __pdp_fini(&ppgtt->pdp);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001362 goto free_scratch;
Chris Wilsone2b763c2017-02-15 08:43:48 +00001363 }
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001364 }
Chris Wilson894cceb2017-02-15 08:43:37 +00001365
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001366 ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_3lvl;
Chris Wilson894cceb2017-02-15 08:43:37 +00001367 ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001368 ppgtt->base.clear_range = gen8_ppgtt_clear_3lvl;
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001369 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001370
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001371 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001372 gen8_ppgtt_notify_vgt(ppgtt, true);
1373
Michel Thierryd7b26332015-04-08 12:13:34 +01001374 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001375
1376free_scratch:
1377 gen8_free_scratch(&ppgtt->base);
1378 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001379}
1380
Ben Widawsky87d60b62013-12-06 14:11:29 -08001381static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1382{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001383 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001384 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001385 gen6_pte_t scratch_pte;
Chris Wilson381b9432017-02-15 08:43:54 +00001386 u32 pd_entry, pte, pde;
1387 u32 start = 0, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001388
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001389 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001390 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001391
Dave Gordon731f74c2016-06-24 19:37:46 +01001392 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001393 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001394 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001395 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001396 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001397 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1398
1399 if (pd_entry != expected)
1400 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1401 pde,
1402 pd_entry,
1403 expected);
1404 seq_printf(m, "\tPDE: %x\n", pd_entry);
1405
Chris Wilson9231da72017-02-15 08:43:41 +00001406 pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[pde]);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001407
Michel Thierry07749ef2015-03-16 16:00:54 +00001408 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001409 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001410 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001411 (pte * PAGE_SIZE);
1412 int i;
1413 bool found = false;
1414 for (i = 0; i < 4; i++)
1415 if (pt_vaddr[pte + i] != scratch_pte)
1416 found = true;
1417 if (!found)
1418 continue;
1419
1420 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1421 for (i = 0; i < 4; i++) {
1422 if (pt_vaddr[pte + i] != scratch_pte)
1423 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1424 else
1425 seq_puts(m, " SCRATCH ");
1426 }
1427 seq_puts(m, "\n");
1428 }
Chris Wilson9231da72017-02-15 08:43:41 +00001429 kunmap_atomic(pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001430 }
1431}
1432
Ben Widawsky678d96f2015-03-16 16:00:56 +00001433/* Write pde (index) from the page directory @pd to the page table @pt */
Chris Wilson16a011c2017-02-15 08:43:45 +00001434static inline void gen6_write_pde(const struct i915_hw_ppgtt *ppgtt,
1435 const unsigned int pde,
1436 const struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001437{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001438 /* Caller needs to make sure the write completes if necessary */
Chris Wilson16a011c2017-02-15 08:43:45 +00001439 writel_relaxed(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
1440 ppgtt->pd_addr + pde);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001441}
Ben Widawsky61973492013-04-08 18:43:54 -07001442
Ben Widawsky678d96f2015-03-16 16:00:56 +00001443/* Write all the page tables found in the ppgtt structure to incrementing page
1444 * directories. */
Chris Wilson16a011c2017-02-15 08:43:45 +00001445static void gen6_write_page_range(struct i915_hw_ppgtt *ppgtt,
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001446 u32 start, u32 length)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001447{
Michel Thierryec565b32015-04-08 12:13:23 +01001448 struct i915_page_table *pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001449 unsigned int pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001450
Chris Wilson16a011c2017-02-15 08:43:45 +00001451 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde)
1452 gen6_write_pde(ppgtt, pde, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001453
Chris Wilson16a011c2017-02-15 08:43:45 +00001454 mark_tlbs_dirty(ppgtt);
Chris Wilsondd196742017-02-15 08:43:46 +00001455 wmb();
Ben Widawsky3e302542013-04-23 23:15:32 -07001456}
1457
Chris Wilson75c7b0b2017-02-15 08:43:57 +00001458static inline u32 get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001459{
Chris Wilsondd196742017-02-15 08:43:46 +00001460 GEM_BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1461 return ppgtt->pd.base.ggtt_offset << 10;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001462}
Ben Widawsky61973492013-04-08 18:43:54 -07001463
Ben Widawsky90252e52013-12-06 14:11:12 -08001464static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001465 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001466{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001467 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001468 u32 *cs;
Ben Widawsky61973492013-04-08 18:43:54 -07001469
Ben Widawsky90252e52013-12-06 14:11:12 -08001470 /* NB: TLBs must be flushed and invalidated before a switch */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001471 cs = intel_ring_begin(req, 6);
1472 if (IS_ERR(cs))
1473 return PTR_ERR(cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001474
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001475 *cs++ = MI_LOAD_REGISTER_IMM(2);
1476 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1477 *cs++ = PP_DIR_DCLV_2G;
1478 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1479 *cs++ = get_pd_offset(ppgtt);
1480 *cs++ = MI_NOOP;
1481 intel_ring_advance(req, cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001482
1483 return 0;
1484}
1485
Ben Widawsky48a10382013-12-06 14:11:11 -08001486static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001487 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001488{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001489 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001490 u32 *cs;
Ben Widawsky48a10382013-12-06 14:11:11 -08001491
Ben Widawsky48a10382013-12-06 14:11:11 -08001492 /* NB: TLBs must be flushed and invalidated before a switch */
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001493 cs = intel_ring_begin(req, 6);
1494 if (IS_ERR(cs))
1495 return PTR_ERR(cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001496
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001497 *cs++ = MI_LOAD_REGISTER_IMM(2);
1498 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1499 *cs++ = PP_DIR_DCLV_2G;
1500 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1501 *cs++ = get_pd_offset(ppgtt);
1502 *cs++ = MI_NOOP;
1503 intel_ring_advance(req, cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001504
1505 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)
Chris Wilson2f7399a2017-02-27 12:26:53 +00002300 goto err_pages;
Chris Wilsonff685972017-02-15 08:43:42 +00002301 }
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;
Chris Wilson2f7399a2017-02-27 12:26:53 +00002317
2318err_pages:
2319 if (!(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND))) {
2320 if (vma->pages != vma->obj->mm.pages) {
2321 GEM_BUG_ON(!vma->pages);
2322 sg_free_table(vma->pages);
2323 kfree(vma->pages);
2324 }
2325 vma->pages = NULL;
2326 }
2327 return ret;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002328}
2329
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002330static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002331{
Chris Wilson49d73912016-11-29 09:50:08 +00002332 struct drm_i915_private *i915 = vma->vm->i915;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002333
Chris Wilson9c870d02016-10-24 13:42:15 +01002334 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2335 intel_runtime_pm_get(i915);
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002336 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002337 intel_runtime_pm_put(i915);
2338 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002339
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002340 if (vma->flags & I915_VMA_LOCAL_BIND) {
2341 struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
2342
2343 vm->clear_range(vm, vma->node.start, vma->size);
2344 }
Daniel Vetter74163902012-02-15 23:50:21 +01002345}
2346
Chris Wilson03ac84f2016-10-28 13:58:36 +01002347void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2348 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002349{
David Weinehall52a05c32016-08-22 13:32:44 +03002350 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2351 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002352 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002353
Chris Wilson307dc252016-08-05 10:14:12 +01002354 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002355 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002356 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2357 /* Wait a bit, in hopes it avoids the hang */
2358 udelay(10);
2359 }
2360 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002361
Chris Wilson03ac84f2016-10-28 13:58:36 +01002362 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002363}
Daniel Vetter644ec022012-03-26 09:45:40 +02002364
Chris Wilson45b186f2016-12-16 07:46:42 +00002365static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002366 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002367 u64 *start,
2368 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002369{
Chris Wilsona6508de2017-02-06 08:45:47 +00002370 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002371 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002372
Chris Wilsona6508de2017-02-06 08:45:47 +00002373 /* Also leave a space between the unallocated reserved node after the
2374 * GTT and any objects within the GTT, i.e. we use the color adjustment
2375 * to insert a guard page to prevent prefetches crossing over the
2376 * GTT boundary.
2377 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002378 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002379 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002380 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002381}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002382
Chris Wilson6cde9a02017-02-13 17:15:50 +00002383int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2384{
2385 struct i915_ggtt *ggtt = &i915->ggtt;
2386 struct i915_hw_ppgtt *ppgtt;
2387 int err;
2388
Chris Wilson57202f42017-02-15 08:43:56 +00002389 ppgtt = i915_ppgtt_create(i915, ERR_PTR(-EPERM), "[alias]");
Chris Wilson1188bc62017-02-15 08:43:38 +00002390 if (IS_ERR(ppgtt))
2391 return PTR_ERR(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002392
Chris Wilsone565ceb2017-02-15 08:43:55 +00002393 if (WARN_ON(ppgtt->base.total < ggtt->base.total)) {
2394 err = -ENODEV;
2395 goto err_ppgtt;
2396 }
2397
Chris Wilson6cde9a02017-02-13 17:15:50 +00002398 if (ppgtt->base.allocate_va_range) {
Chris Wilsone565ceb2017-02-15 08:43:55 +00002399 /* Note we only pre-allocate as far as the end of the global
2400 * GTT. On 48b / 4-level page-tables, the difference is very,
2401 * very significant! We have to preallocate as GVT/vgpu does
2402 * not like the page directory disappearing.
2403 */
Chris Wilson6cde9a02017-02-13 17:15:50 +00002404 err = ppgtt->base.allocate_va_range(&ppgtt->base,
Chris Wilsone565ceb2017-02-15 08:43:55 +00002405 0, ggtt->base.total);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002406 if (err)
Chris Wilson1188bc62017-02-15 08:43:38 +00002407 goto err_ppgtt;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002408 }
2409
Chris Wilson6cde9a02017-02-13 17:15:50 +00002410 i915->mm.aliasing_ppgtt = ppgtt;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002411
Chris Wilson6cde9a02017-02-13 17:15:50 +00002412 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2413 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2414
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002415 WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
2416 ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
2417
Chris Wilson6cde9a02017-02-13 17:15:50 +00002418 return 0;
2419
Chris Wilson6cde9a02017-02-13 17:15:50 +00002420err_ppgtt:
Chris Wilson1188bc62017-02-15 08:43:38 +00002421 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002422 return err;
2423}
2424
2425void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2426{
2427 struct i915_ggtt *ggtt = &i915->ggtt;
2428 struct i915_hw_ppgtt *ppgtt;
2429
2430 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2431 if (!ppgtt)
2432 return;
2433
Chris Wilson1188bc62017-02-15 08:43:38 +00002434 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002435
2436 ggtt->base.bind_vma = ggtt_bind_vma;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002437 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002438}
2439
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002440int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002441{
Ben Widawskye78891c2013-01-25 16:41:04 -08002442 /* Let GEM Manage all of the aperture.
2443 *
2444 * However, leave one page at the end still bound to the scratch page.
2445 * There are a number of places where the hardware apparently prefetches
2446 * past the end of the object, and we've seen multiple hangs with the
2447 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2448 * aperture. One page should be enough to keep any prefetching inside
2449 * of the aperture.
2450 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002451 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002452 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002453 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002454 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002455
Zhi Wangb02d22a2016-06-16 08:06:59 -04002456 ret = intel_vgt_balloon(dev_priv);
2457 if (ret)
2458 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002459
Chris Wilson95374d72016-10-12 10:05:20 +01002460 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002461 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2462 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2463 0, ggtt->mappable_end,
2464 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002465 if (ret)
2466 return ret;
2467
Chris Wilsoned2f3452012-11-15 11:32:19 +00002468 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002469 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002470 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2471 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002472 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002473 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002474 }
2475
2476 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002477 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002478 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002479
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002480 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Chris Wilson6cde9a02017-02-13 17:15:50 +00002481 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
Chris Wilson95374d72016-10-12 10:05:20 +01002482 if (ret)
Chris Wilson6cde9a02017-02-13 17:15:50 +00002483 goto err;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002484 }
2485
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002486 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002487
Chris Wilson95374d72016-10-12 10:05:20 +01002488err:
2489 drm_mm_remove_node(&ggtt->error_capture);
2490 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002491}
2492
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002493/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002494 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002495 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002496 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002497void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002498{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002499 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002500 struct i915_vma *vma, *vn;
2501
2502 ggtt->base.closed = true;
2503
2504 mutex_lock(&dev_priv->drm.struct_mutex);
2505 WARN_ON(!list_empty(&ggtt->base.active_list));
2506 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2507 WARN_ON(i915_vma_unbind(vma));
2508 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002509
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002510 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002511
Chris Wilson1188bc62017-02-15 08:43:38 +00002512 mutex_lock(&dev_priv->drm.struct_mutex);
2513 i915_gem_fini_aliasing_ppgtt(dev_priv);
2514
Chris Wilson95374d72016-10-12 10:05:20 +01002515 if (drm_mm_node_allocated(&ggtt->error_capture))
2516 drm_mm_remove_node(&ggtt->error_capture);
2517
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002518 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002519 intel_vgt_deballoon(dev_priv);
Matthew Aulded9724d2016-11-17 21:04:10 +00002520 i915_address_space_fini(&ggtt->base);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002521 }
2522
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002523 ggtt->base.cleanup(&ggtt->base);
Chris Wilson1188bc62017-02-15 08:43:38 +00002524 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002525
2526 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002527 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002528}
Daniel Vetter70e32542014-08-06 15:04:57 +02002529
Daniel Vetter2c642b02015-04-14 17:35:26 +02002530static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002531{
2532 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2533 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2534 return snb_gmch_ctl << 20;
2535}
2536
Daniel Vetter2c642b02015-04-14 17:35:26 +02002537static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002538{
2539 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2540 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2541 if (bdw_gmch_ctl)
2542 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002543
2544#ifdef CONFIG_X86_32
2545 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2546 if (bdw_gmch_ctl > 4)
2547 bdw_gmch_ctl = 4;
2548#endif
2549
Ben Widawsky9459d252013-11-03 16:53:55 -08002550 return bdw_gmch_ctl << 20;
2551}
2552
Daniel Vetter2c642b02015-04-14 17:35:26 +02002553static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002554{
2555 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2556 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2557
2558 if (gmch_ctrl)
2559 return 1 << (20 + gmch_ctrl);
2560
2561 return 0;
2562}
2563
Daniel Vetter2c642b02015-04-14 17:35:26 +02002564static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002565{
2566 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2567 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2568 return snb_gmch_ctl << 25; /* 32 MB units */
2569}
2570
Daniel Vetter2c642b02015-04-14 17:35:26 +02002571static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002572{
2573 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2574 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2575 return bdw_gmch_ctl << 25; /* 32 MB units */
2576}
2577
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002578static size_t chv_get_stolen_size(u16 gmch_ctrl)
2579{
2580 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2581 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2582
2583 /*
2584 * 0x0 to 0x10: 32MB increments starting at 0MB
2585 * 0x11 to 0x16: 4MB increments starting at 8MB
2586 * 0x17 to 0x1d: 4MB increments start at 36MB
2587 */
2588 if (gmch_ctrl < 0x11)
2589 return gmch_ctrl << 25;
2590 else if (gmch_ctrl < 0x17)
2591 return (gmch_ctrl - 0x11 + 2) << 22;
2592 else
2593 return (gmch_ctrl - 0x17 + 9) << 22;
2594}
2595
Damien Lespiau66375012014-01-09 18:02:46 +00002596static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2597{
2598 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2599 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2600
2601 if (gen9_gmch_ctl < 0xf0)
2602 return gen9_gmch_ctl << 25; /* 32 MB units */
2603 else
2604 /* 4MB increments starting at 0xf0 for 4MB */
2605 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2606}
2607
Chris Wilson34c998b2016-08-04 07:52:24 +01002608static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002609{
Chris Wilson49d73912016-11-29 09:50:08 +00002610 struct drm_i915_private *dev_priv = ggtt->base.i915;
2611 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002612 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002613 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002614
2615 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002616 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002617
Imre Deak2a073f892015-03-27 13:07:33 +02002618 /*
2619 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2620 * dropped. For WC mappings in general we have 64 byte burst writes
2621 * when the WC buffer is flushed, so we can't use it, but have to
2622 * resort to an uncached mapping. The WC issue is easily caught by the
2623 * readback check when writing GTT PTE entries.
2624 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002625 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002626 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002627 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002628 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002629 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002630 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002631 return -ENOMEM;
2632 }
2633
Chris Wilson84486612017-02-15 08:43:40 +00002634 ret = setup_scratch_page(&ggtt->base, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002635 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002636 DRM_ERROR("Scratch setup failed\n");
2637 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002638 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002639 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002640 }
2641
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002642 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002643}
2644
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002645/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2646 * bits. When using advanced contexts each context stores its own PAT, but
2647 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002648static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002649{
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002650 u64 pat;
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002651
2652 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2653 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2654 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2655 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2656 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2657 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2658 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2659 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2660
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002661 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002662 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2663 * so RTL will always use the value corresponding to
2664 * pat_sel = 000".
2665 * So let's disable cache for GGTT to avoid screen corruptions.
2666 * MOCS still can be used though.
2667 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2668 * before this patch, i.e. the same uncached + snooping access
2669 * like on gen6/7 seems to be in effect.
2670 * - So this just fixes blitter/render access. Again it looks
2671 * like it's not just uncached access, but uncached + snooping.
2672 * So we can still hold onto all our assumptions wrt cpu
2673 * clflushing on LLC machines.
2674 */
2675 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2676
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002677 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2678 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002679 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2680 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002681}
2682
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002683static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2684{
Chris Wilson75c7b0b2017-02-15 08:43:57 +00002685 u64 pat;
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002686
2687 /*
2688 * Map WB on BDW to snooped on CHV.
2689 *
2690 * Only the snoop bit has meaning for CHV, the rest is
2691 * ignored.
2692 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002693 * The hardware will never snoop for certain types of accesses:
2694 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2695 * - PPGTT page tables
2696 * - some other special cycles
2697 *
2698 * As with BDW, we also need to consider the following for GT accesses:
2699 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2700 * so RTL will always use the value corresponding to
2701 * pat_sel = 000".
2702 * Which means we must set the snoop bit in PAT entry 0
2703 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002704 */
2705 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2706 GEN8_PPAT(1, 0) |
2707 GEN8_PPAT(2, 0) |
2708 GEN8_PPAT(3, 0) |
2709 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2710 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2711 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2712 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2713
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002714 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2715 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002716}
2717
Chris Wilson34c998b2016-08-04 07:52:24 +01002718static void gen6_gmch_remove(struct i915_address_space *vm)
2719{
2720 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2721
2722 iounmap(ggtt->gsm);
Chris Wilson84486612017-02-15 08:43:40 +00002723 cleanup_scratch_page(vm);
Chris Wilson34c998b2016-08-04 07:52:24 +01002724}
2725
Joonas Lahtinend507d732016-03-18 10:42:58 +02002726static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08002727{
Chris Wilson49d73912016-11-29 09:50:08 +00002728 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002729 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002730 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08002731 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08002732
2733 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002734 ggtt->mappable_base = pci_resource_start(pdev, 2);
2735 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08002736
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002737 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
2738 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08002739
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002740 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08002741
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002742 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002743 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002744 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002745 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002746 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002747 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002748 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002749 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002750 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002751 }
Ben Widawsky63340132013-11-04 19:32:22 -08002752
Chris Wilson34c998b2016-08-04 07:52:24 +01002753 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002754
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002755 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002756 chv_setup_private_ppat(dev_priv);
2757 else
2758 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002759
Chris Wilson34c998b2016-08-04 07:52:24 +01002760 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002761 ggtt->base.bind_vma = ggtt_bind_vma;
2762 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05302763 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002764 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01002765 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002766 ggtt->base.clear_range = gen8_ggtt_clear_range;
2767
2768 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002769
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002770 ggtt->invalidate = gen6_ggtt_invalidate;
2771
Chris Wilson34c998b2016-08-04 07:52:24 +01002772 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08002773}
2774
Joonas Lahtinend507d732016-03-18 10:42:58 +02002775static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002776{
Chris Wilson49d73912016-11-29 09:50:08 +00002777 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002778 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002779 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002780 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002781
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002782 ggtt->mappable_base = pci_resource_start(pdev, 2);
2783 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08002784
Ben Widawskybaa09f52013-01-24 13:49:57 -08002785 /* 64/512MB is the current min/max we actually know of, but this is just
2786 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002787 */
Chris Wilson34c998b2016-08-04 07:52:24 +01002788 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002789 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002790 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002791 }
2792
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002793 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
2794 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
2795 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002796
Joonas Lahtinend507d732016-03-18 10:42:58 +02002797 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002798
Chris Wilson34c998b2016-08-04 07:52:24 +01002799 size = gen6_get_total_gtt_size(snb_gmch_ctl);
2800 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002801
Joonas Lahtinend507d732016-03-18 10:42:58 +02002802 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05302803 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002804 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
2805 ggtt->base.bind_vma = ggtt_bind_vma;
2806 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01002807 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002808
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002809 ggtt->invalidate = gen6_ggtt_invalidate;
2810
Chris Wilson34c998b2016-08-04 07:52:24 +01002811 if (HAS_EDRAM(dev_priv))
2812 ggtt->base.pte_encode = iris_pte_encode;
2813 else if (IS_HASWELL(dev_priv))
2814 ggtt->base.pte_encode = hsw_pte_encode;
2815 else if (IS_VALLEYVIEW(dev_priv))
2816 ggtt->base.pte_encode = byt_pte_encode;
2817 else if (INTEL_GEN(dev_priv) >= 7)
2818 ggtt->base.pte_encode = ivb_pte_encode;
2819 else
2820 ggtt->base.pte_encode = snb_pte_encode;
2821
2822 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002823}
2824
Chris Wilson34c998b2016-08-04 07:52:24 +01002825static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002826{
Chris Wilson34c998b2016-08-04 07:52:24 +01002827 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08002828}
2829
Joonas Lahtinend507d732016-03-18 10:42:58 +02002830static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002831{
Chris Wilson49d73912016-11-29 09:50:08 +00002832 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002833 int ret;
2834
Chris Wilson91c8a322016-07-05 10:40:23 +01002835 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002836 if (!ret) {
2837 DRM_ERROR("failed to set up gmch\n");
2838 return -EIO;
2839 }
2840
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00002841 intel_gtt_get(&ggtt->base.total,
2842 &ggtt->stolen_size,
2843 &ggtt->mappable_base,
2844 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002845
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002846 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05302847 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002848 ggtt->base.insert_entries = i915_ggtt_insert_entries;
2849 ggtt->base.clear_range = i915_ggtt_clear_range;
2850 ggtt->base.bind_vma = ggtt_bind_vma;
2851 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01002852 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002853
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002854 ggtt->invalidate = gmch_ggtt_invalidate;
2855
Joonas Lahtinend507d732016-03-18 10:42:58 +02002856 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002857 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2858
Ben Widawskybaa09f52013-01-24 13:49:57 -08002859 return 0;
2860}
2861
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002862/**
Chris Wilson0088e522016-08-04 07:52:21 +01002863 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002864 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002865 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002866int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002867{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002868 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002869 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002870
Chris Wilson49d73912016-11-29 09:50:08 +00002871 ggtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00002872 ggtt->base.dma = &dev_priv->drm.pdev->dev;
Mika Kuoppalac114f762015-06-25 18:35:13 +03002873
Chris Wilson34c998b2016-08-04 07:52:24 +01002874 if (INTEL_GEN(dev_priv) <= 5)
2875 ret = i915_gmch_probe(ggtt);
2876 else if (INTEL_GEN(dev_priv) < 8)
2877 ret = gen6_gmch_probe(ggtt);
2878 else
2879 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002880 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002881 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002882
Chris Wilsondb9309a2017-01-05 15:30:23 +00002883 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
2884 * This is easier than doing range restriction on the fly, as we
2885 * currently don't have any bits spare to pass in this upper
2886 * restriction!
2887 */
2888 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
2889 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
2890 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
2891 }
2892
Chris Wilsonc890e2d2016-03-18 10:42:59 +02002893 if ((ggtt->base.total - 1) >> 32) {
2894 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002895 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02002896 ggtt->base.total >> 20);
2897 ggtt->base.total = 1ULL << 32;
2898 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
2899 }
2900
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002901 if (ggtt->mappable_end > ggtt->base.total) {
2902 DRM_ERROR("mappable aperture extends past end of GGTT,"
2903 " aperture=%llx, total=%llx\n",
2904 ggtt->mappable_end, ggtt->base.total);
2905 ggtt->mappable_end = ggtt->base.total;
2906 }
2907
Ben Widawskybaa09f52013-01-24 13:49:57 -08002908 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002909 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002910 ggtt->base.total >> 20);
2911 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00002912 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002913#ifdef CONFIG_INTEL_IOMMU
2914 if (intel_iommu_gfx_mapped)
2915 DRM_INFO("VT-d active for gfx access\n");
2916#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002917
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002918 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01002919}
2920
2921/**
2922 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002923 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01002924 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002925int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01002926{
Chris Wilson0088e522016-08-04 07:52:21 +01002927 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2928 int ret;
2929
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002930 INIT_LIST_HEAD(&dev_priv->vm_list);
2931
Chris Wilsona6508de2017-02-06 08:45:47 +00002932 /* Note that we use page colouring to enforce a guard page at the
2933 * end of the address space. This is required as the CS may prefetch
2934 * beyond the end of the batch buffer, across the page boundary,
2935 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002936 */
Chris Wilson80b204b2016-10-28 13:58:58 +01002937 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01002938 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00002939 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002940 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01002941 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002942
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002943 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
2944 dev_priv->ggtt.mappable_base,
2945 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002946 ret = -EIO;
2947 goto out_gtt_cleanup;
2948 }
2949
2950 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
2951
Chris Wilson0088e522016-08-04 07:52:21 +01002952 /*
2953 * Initialise stolen early so that we may reserve preallocated
2954 * objects for the BIOS to KMS transition.
2955 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00002956 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01002957 if (ret)
2958 goto out_gtt_cleanup;
2959
2960 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02002961
2962out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002963 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02002964 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002965}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002966
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002967int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03002968{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002969 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03002970 return -EIO;
2971
2972 return 0;
2973}
2974
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002975void i915_ggtt_enable_guc(struct drm_i915_private *i915)
2976{
2977 i915->ggtt.invalidate = guc_ggtt_invalidate;
2978}
2979
2980void i915_ggtt_disable_guc(struct drm_i915_private *i915)
2981{
2982 i915->ggtt.invalidate = gen6_ggtt_invalidate;
2983}
2984
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002985void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02002986{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002987 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002988 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02002989
Chris Wilsondc979972016-05-10 14:10:04 +01002990 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02002991
2992 /* First fill our portion of the GTT with scratch pages */
Chris Wilson381b9432017-02-15 08:43:54 +00002993 ggtt->base.clear_range(&ggtt->base, 0, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02002994
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002995 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
2996
2997 /* clflush objects bound into the GGTT and rebind them. */
2998 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02002999 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003000 bool ggtt_bound = false;
3001 struct i915_vma *vma;
3002
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003003 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003004 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003005 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003006
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003007 if (!i915_vma_unbind(vma))
3008 continue;
3009
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003010 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3011 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003012 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003013 }
3014
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003015 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003016 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003017 }
3018
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003019 ggtt->base.closed = false;
3020
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003021 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003022 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003023 chv_setup_private_ppat(dev_priv);
3024 else
3025 bdw_setup_private_ppat(dev_priv);
3026
3027 return;
3028 }
3029
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003030 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003031 struct i915_address_space *vm;
3032
Daniel Vetterfa423312015-04-14 17:35:23 +02003033 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003034 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003035
Chris Wilson2bfa9962016-08-04 07:52:25 +01003036 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003037 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003038 else
3039 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003040
Chris Wilson16a011c2017-02-15 08:43:45 +00003041 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003042 }
3043 }
3044
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003045 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003046}
3047
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003048static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003049rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003050 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003051 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003052 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003053{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003054 unsigned int column, row;
3055 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003056
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003057 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003058 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003059 for (row = 0; row < height; row++) {
3060 st->nents++;
3061 /* We don't need the pages, but need to initialize
3062 * the entries so the sg list can be happily traversed.
3063 * The only thing we need are DMA addresses.
3064 */
3065 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003066 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003067 sg_dma_len(sg) = PAGE_SIZE;
3068 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003069 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003070 }
3071 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003072
3073 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003074}
3075
Chris Wilsonba7a5742017-02-15 08:43:35 +00003076static noinline struct sg_table *
3077intel_rotate_pages(struct intel_rotation_info *rot_info,
3078 struct drm_i915_gem_object *obj)
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003079{
Chris Wilson75c7b0b2017-02-15 08:43:57 +00003080 const unsigned long n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003081 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003082 struct sgt_iter sgt_iter;
3083 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003084 unsigned long i;
3085 dma_addr_t *page_addr_list;
3086 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003087 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003088 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003089
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003090 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003091 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003092 sizeof(dma_addr_t),
3093 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003094 if (!page_addr_list)
3095 return ERR_PTR(ret);
3096
3097 /* Allocate target SG list. */
3098 st = kmalloc(sizeof(*st), GFP_KERNEL);
3099 if (!st)
3100 goto err_st_alloc;
3101
Ville Syrjälä6687c902015-09-15 13:16:41 +03003102 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003103 if (ret)
3104 goto err_sg_alloc;
3105
3106 /* Populate source page list from the object. */
3107 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003108 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003109 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003110
Dave Gordon85d12252016-05-20 11:54:06 +01003111 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003112 st->nents = 0;
3113 sg = st->sgl;
3114
Ville Syrjälä6687c902015-09-15 13:16:41 +03003115 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3116 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3117 rot_info->plane[i].width, rot_info->plane[i].height,
3118 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003119 }
3120
Ville Syrjälä6687c902015-09-15 13:16:41 +03003121 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3122 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003123
3124 drm_free_large(page_addr_list);
3125
3126 return st;
3127
3128err_sg_alloc:
3129 kfree(st);
3130err_st_alloc:
3131 drm_free_large(page_addr_list);
3132
Ville Syrjälä6687c902015-09-15 13:16:41 +03003133 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3134 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3135
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003136 return ERR_PTR(ret);
3137}
3138
Chris Wilsonba7a5742017-02-15 08:43:35 +00003139static noinline struct sg_table *
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003140intel_partial_pages(const struct i915_ggtt_view *view,
3141 struct drm_i915_gem_object *obj)
3142{
3143 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003144 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003145 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003146 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003147 int ret = -ENOMEM;
3148
3149 st = kmalloc(sizeof(*st), GFP_KERNEL);
3150 if (!st)
3151 goto err_st_alloc;
3152
Chris Wilsond2a84a72016-10-28 13:58:34 +01003153 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003154 if (ret)
3155 goto err_sg_alloc;
3156
Chris Wilson8bab11932017-01-14 00:28:25 +00003157 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003158 GEM_BUG_ON(!iter);
3159
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003160 sg = st->sgl;
3161 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003162 do {
3163 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003164
Chris Wilsond2a84a72016-10-28 13:58:34 +01003165 len = min(iter->length - (offset << PAGE_SHIFT),
3166 count << PAGE_SHIFT);
3167 sg_set_page(sg, NULL, len, 0);
3168 sg_dma_address(sg) =
3169 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3170 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003171
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003172 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003173 count -= len >> PAGE_SHIFT;
3174 if (count == 0) {
3175 sg_mark_end(sg);
3176 return st;
3177 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003178
Chris Wilsond2a84a72016-10-28 13:58:34 +01003179 sg = __sg_next(sg);
3180 iter = __sg_next(iter);
3181 offset = 0;
3182 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003183
3184err_sg_alloc:
3185 kfree(st);
3186err_st_alloc:
3187 return ERR_PTR(ret);
3188}
3189
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003190static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003191i915_get_ggtt_vma_pages(struct i915_vma *vma)
3192{
Chris Wilsonba7a5742017-02-15 08:43:35 +00003193 int ret;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003194
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003195 /* The vma->pages are only valid within the lifespan of the borrowed
3196 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3197 * must be the vma->pages. A simple rule is that vma->pages must only
3198 * be accessed when the obj->mm.pages are pinned.
3199 */
3200 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3201
Chris Wilsonba7a5742017-02-15 08:43:35 +00003202 switch (vma->ggtt_view.type) {
3203 case I915_GGTT_VIEW_NORMAL:
3204 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003205 return 0;
3206
Chris Wilsonba7a5742017-02-15 08:43:35 +00003207 case I915_GGTT_VIEW_ROTATED:
Chris Wilson247177d2016-08-15 10:48:47 +01003208 vma->pages =
Chris Wilsonba7a5742017-02-15 08:43:35 +00003209 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3210 break;
3211
3212 case I915_GGTT_VIEW_PARTIAL:
Chris Wilson247177d2016-08-15 10:48:47 +01003213 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003214 break;
3215
3216 default:
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003217 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3218 vma->ggtt_view.type);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003219 return -EINVAL;
3220 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003221
Chris Wilsonba7a5742017-02-15 08:43:35 +00003222 ret = 0;
3223 if (unlikely(IS_ERR(vma->pages))) {
Chris Wilson247177d2016-08-15 10:48:47 +01003224 ret = PTR_ERR(vma->pages);
3225 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003226 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3227 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003228 }
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003229 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003230}
3231
Chris Wilsone007b192017-01-11 11:23:10 +00003232/**
Chris Wilson625d9882017-01-11 11:23:11 +00003233 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003234 * @vm: the &struct i915_address_space
3235 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3236 * @size: how much space to allocate inside the GTT,
3237 * must be #I915_GTT_PAGE_SIZE aligned
3238 * @offset: where to insert inside the GTT,
3239 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3240 * (@offset + @size) must fit within the address space
3241 * @color: color to apply to node, if this node is not from a VMA,
3242 * color must be #I915_COLOR_UNEVICTABLE
3243 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003244 *
3245 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3246 * the address space (using @size and @color). If the @node does not fit, it
3247 * tries to evict any overlapping nodes from the GTT, including any
3248 * neighbouring nodes if the colors do not match (to ensure guard pages between
3249 * differing domains). See i915_gem_evict_for_node() for the gory details
3250 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3251 * evicting active overlapping objects, and any overlapping node that is pinned
3252 * or marked as unevictable will also result in failure.
3253 *
3254 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3255 * asked to wait for eviction and interrupted.
3256 */
3257int i915_gem_gtt_reserve(struct i915_address_space *vm,
3258 struct drm_mm_node *node,
3259 u64 size, u64 offset, unsigned long color,
3260 unsigned int flags)
3261{
3262 int err;
3263
3264 GEM_BUG_ON(!size);
3265 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3266 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3267 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003268 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003269 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003270
3271 node->size = size;
3272 node->start = offset;
3273 node->color = color;
3274
3275 err = drm_mm_reserve_node(&vm->mm, node);
3276 if (err != -ENOSPC)
3277 return err;
3278
3279 err = i915_gem_evict_for_node(vm, node, flags);
3280 if (err == 0)
3281 err = drm_mm_reserve_node(&vm->mm, node);
3282
3283 return err;
3284}
3285
Chris Wilson606fec92017-01-11 11:23:12 +00003286static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3287{
3288 u64 range, addr;
3289
3290 GEM_BUG_ON(range_overflows(start, len, end));
3291 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3292
3293 range = round_down(end - len, align) - round_up(start, align);
3294 if (range) {
3295 if (sizeof(unsigned long) == sizeof(u64)) {
3296 addr = get_random_long();
3297 } else {
3298 addr = get_random_int();
3299 if (range > U32_MAX) {
3300 addr <<= 32;
3301 addr |= get_random_int();
3302 }
3303 }
3304 div64_u64_rem(addr, range, &addr);
3305 start += addr;
3306 }
3307
3308 return round_up(start, align);
3309}
3310
Chris Wilson625d9882017-01-11 11:23:11 +00003311/**
Chris Wilsone007b192017-01-11 11:23:10 +00003312 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003313 * @vm: the &struct i915_address_space
3314 * @node: the &struct drm_mm_node (typically i915_vma.node)
3315 * @size: how much space to allocate inside the GTT,
3316 * must be #I915_GTT_PAGE_SIZE aligned
3317 * @alignment: required alignment of starting offset, may be 0 but
3318 * if specified, this must be a power-of-two and at least
3319 * #I915_GTT_MIN_ALIGNMENT
3320 * @color: color to apply to node
3321 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003322 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003323 * @end: end of any range restriction inside GTT (U64_MAX for all),
3324 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3325 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003326 *
3327 * i915_gem_gtt_insert() first searches for an available hole into which
3328 * is can insert the node. The hole address is aligned to @alignment and
3329 * its @size must then fit entirely within the [@start, @end] bounds. The
3330 * nodes on either side of the hole must match @color, or else a guard page
3331 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003332 * suitable hole is found, first a victim is randomly selected and tested
3333 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003334 * is scanned to find the first set of replacement nodes to create the hole.
3335 * Those old overlapping nodes are evicted from the GTT (and so must be
3336 * rebound before any future use). Any node that is currently pinned cannot
3337 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3338 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3339 * searching for an eviction candidate. See i915_gem_evict_something() for
3340 * the gory details on the eviction algorithm.
3341 *
3342 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3343 * asked to wait for eviction and interrupted.
3344 */
3345int i915_gem_gtt_insert(struct i915_address_space *vm,
3346 struct drm_mm_node *node,
3347 u64 size, u64 alignment, unsigned long color,
3348 u64 start, u64 end, unsigned int flags)
3349{
Chris Wilson4e64e552017-02-02 21:04:38 +00003350 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003351 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003352 int err;
3353
3354 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3355 GEM_BUG_ON(!size);
3356 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3357 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3358 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3359 GEM_BUG_ON(start >= end);
3360 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3361 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003362 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003363 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003364
3365 if (unlikely(range_overflows(start, size, end)))
3366 return -ENOSPC;
3367
3368 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3369 return -ENOSPC;
3370
Chris Wilson4e64e552017-02-02 21:04:38 +00003371 mode = DRM_MM_INSERT_BEST;
3372 if (flags & PIN_HIGH)
3373 mode = DRM_MM_INSERT_HIGH;
3374 if (flags & PIN_MAPPABLE)
3375 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003376
3377 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3378 * so we know that we always have a minimum alignment of 4096.
3379 * The drm_mm range manager is optimised to return results
3380 * with zero alignment, so where possible use the optimal
3381 * path.
3382 */
3383 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3384 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3385 alignment = 0;
3386
Chris Wilson4e64e552017-02-02 21:04:38 +00003387 err = drm_mm_insert_node_in_range(&vm->mm, node,
3388 size, alignment, color,
3389 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003390 if (err != -ENOSPC)
3391 return err;
3392
Chris Wilson606fec92017-01-11 11:23:12 +00003393 /* No free space, pick a slot at random.
3394 *
3395 * There is a pathological case here using a GTT shared between
3396 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3397 *
3398 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3399 * (64k objects) (448k objects)
3400 *
3401 * Now imagine that the eviction LRU is ordered top-down (just because
3402 * pathology meets real life), and that we need to evict an object to
3403 * make room inside the aperture. The eviction scan then has to walk
3404 * the 448k list before it finds one within range. And now imagine that
3405 * it has to search for a new hole between every byte inside the memcpy,
3406 * for several simultaneous clients.
3407 *
3408 * On a full-ppgtt system, if we have run out of available space, there
3409 * will be lots and lots of objects in the eviction list! Again,
3410 * searching that LRU list may be slow if we are also applying any
3411 * range restrictions (e.g. restriction to low 4GiB) and so, for
3412 * simplicity and similarilty between different GTT, try the single
3413 * random replacement first.
3414 */
3415 offset = random_offset(start, end,
3416 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3417 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3418 if (err != -ENOSPC)
3419 return err;
3420
3421 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003422 err = i915_gem_evict_something(vm, size, alignment, color,
3423 start, end, flags);
3424 if (err)
3425 return err;
3426
Chris Wilson4e64e552017-02-02 21:04:38 +00003427 return drm_mm_insert_node_in_range(&vm->mm, node,
3428 size, alignment, color,
3429 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003430}
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003431
3432#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3433#include "selftests/mock_gtt.c"
Chris Wilson1c428192017-02-13 17:15:38 +00003434#include "selftests/i915_gem_gtt.c"
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003435#endif