blob: 0748c4406f27d66c3b13df425ec04934e435cd9f [file] [log] [blame]
Daniel Vetter76aaf222010-11-05 22:23:30 +01001/*
2 * Copyright © 2010 Daniel Vetter
Ben Widawskyc4ac5242014-02-19 22:05:47 -08003 * Copyright © 2011-2014 Intel Corporation
Daniel Vetter76aaf222010-11-05 22:23:30 +01004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
Chris Wilsonaae4a3d2017-02-13 17:15:44 +000026#include <linux/slab.h> /* fault-inject.h is not standalone! */
27
28#include <linux/fault-inject.h>
Chris Wilsone007b192017-01-11 11:23:10 +000029#include <linux/log2.h>
Chris Wilson606fec92017-01-11 11:23:12 +000030#include <linux/random.h>
Daniel Vetter0e46ce22014-01-08 16:10:27 +010031#include <linux/seq_file.h>
Chris Wilson5bab6f62015-10-23 18:43:32 +010032#include <linux/stop_machine.h>
Chris Wilsone007b192017-01-11 11:23:10 +000033
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drmP.h>
35#include <drm/i915_drm.h>
Chris Wilsone007b192017-01-11 11:23:10 +000036
Daniel Vetter76aaf222010-11-05 22:23:30 +010037#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080038#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010039#include "i915_trace.h"
40#include "intel_drv.h"
Chris Wilsond07f0e52016-10-28 13:58:44 +010041#include "intel_frontbuffer.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010042
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010043#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
44
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000045/**
46 * DOC: Global GTT views
47 *
48 * Background and previous state
49 *
50 * Historically objects could exists (be bound) in global GTT space only as
51 * singular instances with a view representing all of the object's backing pages
52 * in a linear fashion. This view will be called a normal view.
53 *
54 * To support multiple views of the same object, where the number of mapped
55 * pages is not equal to the backing store, or where the layout of the pages
56 * is not linear, concept of a GGTT view was added.
57 *
58 * One example of an alternative view is a stereo display driven by a single
59 * image. In this case we would have a framebuffer looking like this
60 * (2x2 pages):
61 *
62 * 12
63 * 34
64 *
65 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
66 * rendering. In contrast, fed to the display engine would be an alternative
67 * view which could look something like this:
68 *
69 * 1212
70 * 3434
71 *
72 * In this example both the size and layout of pages in the alternative view is
73 * different from the normal view.
74 *
75 * Implementation and usage
76 *
77 * GGTT views are implemented using VMAs and are distinguished via enum
78 * i915_ggtt_view_type and struct i915_ggtt_view.
79 *
80 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020081 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
82 * renaming in large amounts of code. They take the struct i915_ggtt_view
83 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000084 *
85 * As a helper for callers which are only interested in the normal view,
86 * globally const i915_ggtt_view_normal singleton instance exists. All old core
87 * GEM API functions, the ones not taking the view parameter, are operating on,
88 * or with the normal GGTT view.
89 *
90 * Code wanting to add or use a new GGTT view needs to:
91 *
92 * 1. Add a new enum with a suitable name.
93 * 2. Extend the metadata in the i915_ggtt_view structure if required.
94 * 3. Add support to i915_get_vma_pages().
95 *
96 * New views are required to build a scatter-gather table from within the
97 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
98 * exists for the lifetime of an VMA.
99 *
100 * Core API is designed to have copy semantics which means that passed in
101 * struct i915_ggtt_view does not need to be persistent (left around after
102 * calling the core API functions).
103 *
104 */
105
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200106static int
107i915_get_ggtt_vma_pages(struct i915_vma *vma);
108
Chris Wilson7c3f86b2017-01-12 11:00:49 +0000109static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
110{
111 /* Note that as an uncached mmio write, this should flush the
112 * WCB of the writes into the GGTT before it triggers the invalidate.
113 */
114 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
115}
116
117static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
118{
119 gen6_ggtt_invalidate(dev_priv);
120 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
121}
122
123static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
124{
125 intel_gtt_chipset_flush();
126}
127
128static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
129{
130 i915->ggtt.invalidate(i915);
131}
132
Chris Wilsonc0336662016-05-06 15:40:21 +0100133int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
134 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200135{
Chris Wilson1893a712014-09-19 11:56:27 +0100136 bool has_aliasing_ppgtt;
137 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100138 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100139
Michel Thierry9e1d0e62016-12-05 17:57:03 -0800140 has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
141 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
142 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100143
Zhi Wange320d402016-09-06 12:04:12 +0800144 if (intel_vgpu_active(dev_priv)) {
145 /* emulation is too hard */
146 has_full_ppgtt = false;
147 has_full_48bit_ppgtt = false;
148 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800149
Chris Wilson0e4ca102016-04-29 13:18:22 +0100150 if (!has_aliasing_ppgtt)
151 return 0;
152
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000153 /*
154 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
155 * execlists, the sole mechanism available to submit work.
156 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100157 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200158 return 0;
159
160 if (enable_ppgtt == 1)
161 return 1;
162
Chris Wilson1893a712014-09-19 11:56:27 +0100163 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200164 return 2;
165
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100166 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
167 return 3;
168
Daniel Vetter93a25a92014-03-06 09:40:43 +0100169#ifdef CONFIG_INTEL_IOMMU
170 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100171 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100172 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200173 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100174 }
175#endif
176
Jesse Barnes62942ed2014-06-13 09:28:33 -0700177 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100178 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700179 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
180 return 0;
181 }
182
Zhi Wange320d402016-09-06 12:04:12 +0800183 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100184 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000185 else
186 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100187}
188
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200189static int ppgtt_bind_vma(struct i915_vma *vma,
190 enum i915_cache_level cache_level,
191 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200192{
Chris Wilsonff685972017-02-15 08:43:42 +0000193 u32 pte_flags;
194 int ret;
195
Chris Wilsonff685972017-02-15 08:43:42 +0000196 ret = vma->vm->allocate_va_range(vma->vm, vma->node.start, vma->size);
197 if (ret)
198 return ret;
Daniel Vetter47552652015-04-14 17:35:24 +0200199
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100200 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100201
Daniel Vetter47552652015-04-14 17:35:24 +0200202 /* Currently applicable only to VLV */
Chris Wilsonff685972017-02-15 08:43:42 +0000203 pte_flags = 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200204 if (vma->obj->gt_ro)
205 pte_flags |= PTE_READ_ONLY;
206
Chris Wilson247177d2016-08-15 10:48:47 +0100207 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200208 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200209
210 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200211}
212
213static void ppgtt_unbind_vma(struct i915_vma *vma)
214{
Chris Wilsonff685972017-02-15 08:43:42 +0000215 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200216}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800217
Daniel Vetter2c642b02015-04-14 17:35:26 +0200218static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200219 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700220{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200221 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700222 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300223
224 switch (level) {
225 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800226 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300227 break;
228 case I915_CACHE_WT:
229 pte |= PPAT_DISPLAY_ELLC_INDEX;
230 break;
231 default:
232 pte |= PPAT_CACHED_INDEX;
233 break;
234 }
235
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700236 return pte;
237}
238
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300239static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
240 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800241{
Michel Thierry07749ef2015-03-16 16:00:54 +0000242 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800243 pde |= addr;
244 if (level != I915_CACHE_NONE)
245 pde |= PPAT_CACHED_PDE_INDEX;
246 else
247 pde |= PPAT_UNCACHED_INDEX;
248 return pde;
249}
250
Michel Thierry762d9932015-07-30 11:05:29 +0100251#define gen8_pdpe_encode gen8_pde_encode
252#define gen8_pml4e_encode gen8_pde_encode
253
Michel Thierry07749ef2015-03-16 16:00:54 +0000254static gen6_pte_t snb_pte_encode(dma_addr_t addr,
255 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200256 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700257{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200258 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700259 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700260
261 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100262 case I915_CACHE_L3_LLC:
263 case I915_CACHE_LLC:
264 pte |= GEN6_PTE_CACHE_LLC;
265 break;
266 case I915_CACHE_NONE:
267 pte |= GEN6_PTE_UNCACHED;
268 break;
269 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100270 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100271 }
272
273 return pte;
274}
275
Michel Thierry07749ef2015-03-16 16:00:54 +0000276static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
277 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200278 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100279{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200280 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100281 pte |= GEN6_PTE_ADDR_ENCODE(addr);
282
283 switch (level) {
284 case I915_CACHE_L3_LLC:
285 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700286 break;
287 case I915_CACHE_LLC:
288 pte |= GEN6_PTE_CACHE_LLC;
289 break;
290 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700291 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700292 break;
293 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100294 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700295 }
296
Ben Widawsky54d12522012-09-24 16:44:32 -0700297 return pte;
298}
299
Michel Thierry07749ef2015-03-16 16:00:54 +0000300static gen6_pte_t byt_pte_encode(dma_addr_t addr,
301 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200302 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700303{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200304 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700305 pte |= GEN6_PTE_ADDR_ENCODE(addr);
306
Akash Goel24f3a8c2014-06-17 10:59:42 +0530307 if (!(flags & PTE_READ_ONLY))
308 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700309
310 if (level != I915_CACHE_NONE)
311 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
312
313 return pte;
314}
315
Michel Thierry07749ef2015-03-16 16:00:54 +0000316static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
317 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200318 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700319{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200320 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700321 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700322
323 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700324 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700325
326 return pte;
327}
328
Michel Thierry07749ef2015-03-16 16:00:54 +0000329static gen6_pte_t iris_pte_encode(dma_addr_t addr,
330 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200331 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700332{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200333 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700334 pte |= HSW_PTE_ADDR_ENCODE(addr);
335
Chris Wilson651d7942013-08-08 14:41:10 +0100336 switch (level) {
337 case I915_CACHE_NONE:
338 break;
339 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000340 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100341 break;
342 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000343 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100344 break;
345 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700346
347 return pte;
348}
349
Chris Wilson84486612017-02-15 08:43:40 +0000350static struct page *vm_alloc_page(struct i915_address_space *vm, gfp_t gfp)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000351{
Chris Wilson84486612017-02-15 08:43:40 +0000352 struct page *page;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000353
Chris Wilson84486612017-02-15 08:43:40 +0000354 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
355 i915_gem_shrink_all(vm->i915);
Chris Wilsonaae4a3d2017-02-13 17:15:44 +0000356
Chris Wilson84486612017-02-15 08:43:40 +0000357 if (vm->free_pages.nr)
358 return vm->free_pages.pages[--vm->free_pages.nr];
359
360 page = alloc_page(gfp);
361 if (!page)
362 return NULL;
363
364 if (vm->pt_kmap_wc)
365 set_pages_array_wc(&page, 1);
366
367 return page;
368}
369
370static void vm_free_pages_release(struct i915_address_space *vm)
371{
372 GEM_BUG_ON(!pagevec_count(&vm->free_pages));
373
374 if (vm->pt_kmap_wc)
375 set_pages_array_wb(vm->free_pages.pages,
376 pagevec_count(&vm->free_pages));
377
378 __pagevec_release(&vm->free_pages);
379}
380
381static void vm_free_page(struct i915_address_space *vm, struct page *page)
382{
383 if (!pagevec_add(&vm->free_pages, page))
384 vm_free_pages_release(vm);
385}
386
387static int __setup_page_dma(struct i915_address_space *vm,
388 struct i915_page_dma *p,
389 gfp_t gfp)
390{
391 p->page = vm_alloc_page(vm, gfp | __GFP_NOWARN | __GFP_NORETRY);
392 if (unlikely(!p->page))
Michel Thierry1266cdb2015-03-24 17:06:33 +0000393 return -ENOMEM;
394
Chris Wilson84486612017-02-15 08:43:40 +0000395 p->daddr = dma_map_page(vm->dma, p->page, 0, PAGE_SIZE,
396 PCI_DMA_BIDIRECTIONAL);
397 if (unlikely(dma_mapping_error(vm->dma, p->daddr))) {
398 vm_free_page(vm, p->page);
399 return -ENOMEM;
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300400 }
401
Michel Thierry1266cdb2015-03-24 17:06:33 +0000402 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000403}
404
Chris Wilson84486612017-02-15 08:43:40 +0000405static int setup_page_dma(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000406 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300407{
Chris Wilson84486612017-02-15 08:43:40 +0000408 return __setup_page_dma(vm, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300409}
410
Chris Wilson84486612017-02-15 08:43:40 +0000411static void cleanup_page_dma(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000412 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300413{
Chris Wilson84486612017-02-15 08:43:40 +0000414 dma_unmap_page(vm->dma, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
415 vm_free_page(vm, p->page);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300416}
417
Chris Wilson9231da72017-02-15 08:43:41 +0000418#define kmap_atomic_px(px) kmap_atomic(px_base(px)->page)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300419
Chris Wilson84486612017-02-15 08:43:40 +0000420#define setup_px(vm, px) setup_page_dma((vm), px_base(px))
421#define cleanup_px(vm, px) cleanup_page_dma((vm), px_base(px))
422#define fill_px(ppgtt, px, v) fill_page_dma((vm), px_base(px), (v))
423#define fill32_px(ppgtt, px, v) fill_page_dma_32((vm), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300424
Chris Wilson84486612017-02-15 08:43:40 +0000425static void fill_page_dma(struct i915_address_space *vm,
426 struct i915_page_dma *p,
427 const u64 val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300428{
Chris Wilson9231da72017-02-15 08:43:41 +0000429 u64 * const vaddr = kmap_atomic(p->page);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300430 int i;
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300431
432 for (i = 0; i < 512; i++)
433 vaddr[i] = val;
434
Chris Wilson9231da72017-02-15 08:43:41 +0000435 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300436}
437
Chris Wilson84486612017-02-15 08:43:40 +0000438static void fill_page_dma_32(struct i915_address_space *vm,
439 struct i915_page_dma *p,
440 const u32 v)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300441{
Chris Wilson84486612017-02-15 08:43:40 +0000442 fill_page_dma(vm, p, (u64)v << 32 | v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300443}
444
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100445static int
Chris Wilson84486612017-02-15 08:43:40 +0000446setup_scratch_page(struct i915_address_space *vm, gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300447{
Chris Wilson84486612017-02-15 08:43:40 +0000448 return __setup_page_dma(vm, &vm->scratch_page, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300449}
450
Chris Wilson84486612017-02-15 08:43:40 +0000451static void cleanup_scratch_page(struct i915_address_space *vm)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300452{
Chris Wilson84486612017-02-15 08:43:40 +0000453 cleanup_page_dma(vm, &vm->scratch_page);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300454}
455
Chris Wilson84486612017-02-15 08:43:40 +0000456static struct i915_page_table *alloc_pt(struct i915_address_space *vm)
Ben Widawsky06fda602015-02-24 16:22:36 +0000457{
Michel Thierryec565b32015-04-08 12:13:23 +0100458 struct i915_page_table *pt;
Ben Widawsky06fda602015-02-24 16:22:36 +0000459
Chris Wilsondd196742017-02-15 08:43:46 +0000460 pt = kmalloc(sizeof(*pt), GFP_KERNEL | __GFP_NOWARN);
461 if (unlikely(!pt))
Ben Widawsky06fda602015-02-24 16:22:36 +0000462 return ERR_PTR(-ENOMEM);
463
Chris Wilsondd196742017-02-15 08:43:46 +0000464 if (unlikely(setup_px(vm, pt))) {
465 kfree(pt);
466 return ERR_PTR(-ENOMEM);
467 }
Ben Widawsky678d96f2015-03-16 16:00:56 +0000468
Chris Wilsondd196742017-02-15 08:43:46 +0000469 pt->used_ptes = 0;
Ben Widawsky06fda602015-02-24 16:22:36 +0000470 return pt;
471}
472
Chris Wilson84486612017-02-15 08:43:40 +0000473static void free_pt(struct i915_address_space *vm, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000474{
Chris Wilson84486612017-02-15 08:43:40 +0000475 cleanup_px(vm, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300476 kfree(pt);
477}
478
479static void gen8_initialize_pt(struct i915_address_space *vm,
480 struct i915_page_table *pt)
481{
Chris Wilsondd196742017-02-15 08:43:46 +0000482 fill_px(vm, pt,
483 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC));
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300484}
485
486static void gen6_initialize_pt(struct i915_address_space *vm,
487 struct i915_page_table *pt)
488{
Chris Wilsondd196742017-02-15 08:43:46 +0000489 fill32_px(vm, pt,
490 vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0));
Ben Widawsky06fda602015-02-24 16:22:36 +0000491}
492
Chris Wilson84486612017-02-15 08:43:40 +0000493static struct i915_page_directory *alloc_pd(struct i915_address_space *vm)
Ben Widawsky06fda602015-02-24 16:22:36 +0000494{
Michel Thierryec565b32015-04-08 12:13:23 +0100495 struct i915_page_directory *pd;
Ben Widawsky06fda602015-02-24 16:22:36 +0000496
Chris Wilsonfe52e372017-02-15 08:43:47 +0000497 pd = kzalloc(sizeof(*pd), GFP_KERNEL | __GFP_NOWARN);
498 if (unlikely(!pd))
Ben Widawsky06fda602015-02-24 16:22:36 +0000499 return ERR_PTR(-ENOMEM);
500
Chris Wilsonfe52e372017-02-15 08:43:47 +0000501 if (unlikely(setup_px(vm, pd))) {
502 kfree(pd);
503 return ERR_PTR(-ENOMEM);
504 }
Michel Thierry33c88192015-04-08 12:13:33 +0100505
Chris Wilsonfe52e372017-02-15 08:43:47 +0000506 pd->used_pdes = 0;
Ben Widawsky06fda602015-02-24 16:22:36 +0000507 return pd;
508}
509
Chris Wilson84486612017-02-15 08:43:40 +0000510static void free_pd(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000511 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300512{
Chris Wilsonfe52e372017-02-15 08:43:47 +0000513 cleanup_px(vm, pd);
514 kfree(pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300515}
516
517static void gen8_initialize_pd(struct i915_address_space *vm,
518 struct i915_page_directory *pd)
519{
Chris Wilsondd196742017-02-15 08:43:46 +0000520 unsigned int i;
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300521
Chris Wilsondd196742017-02-15 08:43:46 +0000522 fill_px(vm, pd,
523 gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC));
524 for (i = 0; i < I915_PDES; i++)
525 pd->page_table[i] = vm->scratch_pt;
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300526}
527
Chris Wilsonfe52e372017-02-15 08:43:47 +0000528static int __pdp_init(struct i915_address_space *vm,
Michel Thierry6ac18502015-07-29 17:23:46 +0100529 struct i915_page_directory_pointer *pdp)
530{
Chris Wilsone2b763c2017-02-15 08:43:48 +0000531 const unsigned int pdpes = I915_PDPES_PER_PDP(vm->i915);
532 unsigned int i;
Michel Thierry6ac18502015-07-29 17:23:46 +0100533
Chris Wilsonfe52e372017-02-15 08:43:47 +0000534 pdp->page_directory = kmalloc_array(pdpes, sizeof(*pdp->page_directory),
Chris Wilsone2b763c2017-02-15 08:43:48 +0000535 GFP_KERNEL | __GFP_NOWARN);
536 if (unlikely(!pdp->page_directory))
Michel Thierry6ac18502015-07-29 17:23:46 +0100537 return -ENOMEM;
Michel Thierry6ac18502015-07-29 17:23:46 +0100538
Chris Wilsonfe52e372017-02-15 08:43:47 +0000539 for (i = 0; i < pdpes; i++)
540 pdp->page_directory[i] = vm->scratch_pd;
541
Michel Thierry6ac18502015-07-29 17:23:46 +0100542 return 0;
543}
544
545static void __pdp_fini(struct i915_page_directory_pointer *pdp)
546{
Michel Thierry6ac18502015-07-29 17:23:46 +0100547 kfree(pdp->page_directory);
548 pdp->page_directory = NULL;
549}
550
Chris Wilson84486612017-02-15 08:43:40 +0000551static struct i915_page_directory_pointer *
552alloc_pdp(struct i915_address_space *vm)
Michel Thierry762d9932015-07-30 11:05:29 +0100553{
554 struct i915_page_directory_pointer *pdp;
555 int ret = -ENOMEM;
556
Chris Wilson84486612017-02-15 08:43:40 +0000557 WARN_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Michel Thierry762d9932015-07-30 11:05:29 +0100558
559 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
560 if (!pdp)
561 return ERR_PTR(-ENOMEM);
562
Chris Wilsonfe52e372017-02-15 08:43:47 +0000563 ret = __pdp_init(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100564 if (ret)
565 goto fail_bitmap;
566
Chris Wilson84486612017-02-15 08:43:40 +0000567 ret = setup_px(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100568 if (ret)
569 goto fail_page_m;
570
571 return pdp;
572
573fail_page_m:
574 __pdp_fini(pdp);
575fail_bitmap:
576 kfree(pdp);
577
578 return ERR_PTR(ret);
579}
580
Chris Wilson84486612017-02-15 08:43:40 +0000581static void free_pdp(struct i915_address_space *vm,
Michel Thierry6ac18502015-07-29 17:23:46 +0100582 struct i915_page_directory_pointer *pdp)
583{
584 __pdp_fini(pdp);
Chris Wilson84486612017-02-15 08:43:40 +0000585 if (USES_FULL_48BIT_PPGTT(vm->i915)) {
586 cleanup_px(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100587 kfree(pdp);
588 }
589}
590
Michel Thierry69ab76f2015-07-29 17:23:55 +0100591static void gen8_initialize_pdp(struct i915_address_space *vm,
592 struct i915_page_directory_pointer *pdp)
593{
594 gen8_ppgtt_pdpe_t scratch_pdpe;
595
596 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
597
Chris Wilson84486612017-02-15 08:43:40 +0000598 fill_px(vm, pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100599}
600
601static void gen8_initialize_pml4(struct i915_address_space *vm,
602 struct i915_pml4 *pml4)
603{
Chris Wilsone2b763c2017-02-15 08:43:48 +0000604 unsigned int i;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100605
Chris Wilsone2b763c2017-02-15 08:43:48 +0000606 fill_px(vm, pml4,
607 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC));
608 for (i = 0; i < GEN8_PML4ES_PER_PML4; i++)
609 pml4->pdps[i] = vm->scratch_pdp;
Michel Thierry6ac18502015-07-29 17:23:46 +0100610}
611
Ben Widawsky94e409c2013-11-04 22:29:36 -0800612/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100613static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100614 unsigned entry,
615 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800616{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000617 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000618 u32 *cs;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800619
620 BUG_ON(entry >= 4);
621
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000622 cs = intel_ring_begin(req, 6);
623 if (IS_ERR(cs))
624 return PTR_ERR(cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800625
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000626 *cs++ = MI_LOAD_REGISTER_IMM(1);
627 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, entry));
628 *cs++ = upper_32_bits(addr);
629 *cs++ = MI_LOAD_REGISTER_IMM(1);
630 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, entry));
631 *cs++ = lower_32_bits(addr);
632 intel_ring_advance(req, cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800633
634 return 0;
635}
636
Michel Thierry2dba3232015-07-30 11:06:23 +0100637static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
638 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800639{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800640 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800641
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100642 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300643 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
644
John Harrisone85b26d2015-05-29 17:43:56 +0100645 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800646 if (ret)
647 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800648 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800649
Ben Widawskyeeb94882013-12-06 14:11:10 -0800650 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800651}
652
Michel Thierry2dba3232015-07-30 11:06:23 +0100653static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
654 struct drm_i915_gem_request *req)
655{
656 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
657}
658
Mika Kuoppalafce93752016-10-31 17:24:46 +0200659/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
660 * the page table structures, we mark them dirty so that
661 * context switching/execlist queuing code takes extra steps
662 * to ensure that tlbs are flushed.
663 */
664static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
665{
Chris Wilson49d73912016-11-29 09:50:08 +0000666 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200667}
668
Michał Winiarski2ce51792016-10-13 14:02:42 +0200669/* Removes entries from a single page table, releasing it if it's empty.
670 * Caller can use the return value to update higher-level entries.
671 */
672static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200673 struct i915_page_table *pt,
Chris Wilsondd196742017-02-15 08:43:46 +0000674 u64 start, u64 length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700675{
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200676 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200677 unsigned int pte = gen8_pte_index(start);
678 unsigned int pte_end = pte + num_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +0000679 const gen8_pte_t scratch_pte =
680 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
681 gen8_pte_t *vaddr;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200682
Chris Wilsondd196742017-02-15 08:43:46 +0000683 GEM_BUG_ON(num_entries > pt->used_ptes);
Ben Widawsky459108b2013-11-02 21:07:23 -0700684
Chris Wilsondd196742017-02-15 08:43:46 +0000685 pt->used_ptes -= num_entries;
686 if (!pt->used_ptes)
687 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200688
Chris Wilson9231da72017-02-15 08:43:41 +0000689 vaddr = kmap_atomic_px(pt);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200690 while (pte < pte_end)
Chris Wilson894cceb2017-02-15 08:43:37 +0000691 vaddr[pte++] = scratch_pte;
Chris Wilson9231da72017-02-15 08:43:41 +0000692 kunmap_atomic(vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200693
694 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200695}
696
Chris Wilsondd196742017-02-15 08:43:46 +0000697static void gen8_ppgtt_set_pde(struct i915_address_space *vm,
698 struct i915_page_directory *pd,
699 struct i915_page_table *pt,
700 unsigned int pde)
701{
702 gen8_pde_t *vaddr;
703
704 pd->page_table[pde] = pt;
705
706 vaddr = kmap_atomic_px(pd);
707 vaddr[pde] = gen8_pde_encode(px_dma(pt), I915_CACHE_LLC);
708 kunmap_atomic(vaddr);
709}
710
Michał Winiarski2ce51792016-10-13 14:02:42 +0200711static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200712 struct i915_page_directory *pd,
Chris Wilsondd196742017-02-15 08:43:46 +0000713 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200714{
715 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +0000716 u32 pde;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200717
718 gen8_for_each_pde(pt, pd, start, length, pde) {
Chris Wilsondd196742017-02-15 08:43:46 +0000719 if (!gen8_ppgtt_clear_pt(vm, pt, start, length))
720 continue;
Ben Widawsky06fda602015-02-24 16:22:36 +0000721
Chris Wilsondd196742017-02-15 08:43:46 +0000722 gen8_ppgtt_set_pde(vm, pd, vm->scratch_pt, pde);
Chris Wilsonfe52e372017-02-15 08:43:47 +0000723 pd->used_pdes--;
Chris Wilsondd196742017-02-15 08:43:46 +0000724
725 free_pt(vm, pt);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200726 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200727
Chris Wilsonfe52e372017-02-15 08:43:47 +0000728 return !pd->used_pdes;
729}
Michał Winiarski2ce51792016-10-13 14:02:42 +0200730
Chris Wilsonfe52e372017-02-15 08:43:47 +0000731static void gen8_ppgtt_set_pdpe(struct i915_address_space *vm,
732 struct i915_page_directory_pointer *pdp,
733 struct i915_page_directory *pd,
734 unsigned int pdpe)
735{
736 gen8_ppgtt_pdpe_t *vaddr;
737
738 pdp->page_directory[pdpe] = pd;
739 if (!USES_FULL_48BIT_PPGTT(vm->i915))
740 return;
741
742 vaddr = kmap_atomic_px(pdp);
743 vaddr[pdpe] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
744 kunmap_atomic(vaddr);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200745}
Ben Widawsky06fda602015-02-24 16:22:36 +0000746
Michał Winiarski2ce51792016-10-13 14:02:42 +0200747/* Removes entries from a single page dir pointer, releasing it if it's empty.
748 * Caller can use the return value to update higher-level entries
749 */
750static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200751 struct i915_page_directory_pointer *pdp,
Chris Wilsonfe52e372017-02-15 08:43:47 +0000752 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200753{
754 struct i915_page_directory *pd;
Chris Wilsonfe52e372017-02-15 08:43:47 +0000755 unsigned int pdpe;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200756
757 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilsonfe52e372017-02-15 08:43:47 +0000758 if (!gen8_ppgtt_clear_pd(vm, pd, start, length))
759 continue;
Ben Widawsky06fda602015-02-24 16:22:36 +0000760
Chris Wilsonfe52e372017-02-15 08:43:47 +0000761 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
Chris Wilsone2b763c2017-02-15 08:43:48 +0000762 pdp->used_pdpes--;
Chris Wilsonfe52e372017-02-15 08:43:47 +0000763
764 free_pd(vm, pd);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200765 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200766
Chris Wilsone2b763c2017-02-15 08:43:48 +0000767 return !pdp->used_pdpes;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200768}
Ben Widawsky459108b2013-11-02 21:07:23 -0700769
Chris Wilsonfe52e372017-02-15 08:43:47 +0000770static void gen8_ppgtt_clear_3lvl(struct i915_address_space *vm,
771 u64 start, u64 length)
772{
773 gen8_ppgtt_clear_pdp(vm, &i915_vm_to_ppgtt(vm)->pdp, start, length);
774}
775
Chris Wilsone2b763c2017-02-15 08:43:48 +0000776static void gen8_ppgtt_set_pml4e(struct i915_pml4 *pml4,
777 struct i915_page_directory_pointer *pdp,
778 unsigned int pml4e)
779{
780 gen8_ppgtt_pml4e_t *vaddr;
781
782 pml4->pdps[pml4e] = pdp;
783
784 vaddr = kmap_atomic_px(pml4);
785 vaddr[pml4e] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
786 kunmap_atomic(vaddr);
787}
788
Michał Winiarski2ce51792016-10-13 14:02:42 +0200789/* Removes entries from a single pml4.
790 * This is the top-level structure in 4-level page tables used on gen8+.
791 * Empty entries are always scratch pml4e.
792 */
Chris Wilsonfe52e372017-02-15 08:43:47 +0000793static void gen8_ppgtt_clear_4lvl(struct i915_address_space *vm,
794 u64 start, u64 length)
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200795{
Chris Wilsonfe52e372017-02-15 08:43:47 +0000796 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
797 struct i915_pml4 *pml4 = &ppgtt->pml4;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200798 struct i915_page_directory_pointer *pdp;
Chris Wilsone2b763c2017-02-15 08:43:48 +0000799 unsigned int pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200800
Chris Wilson49d73912016-11-29 09:50:08 +0000801 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700802
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200803 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsone2b763c2017-02-15 08:43:48 +0000804 if (!gen8_ppgtt_clear_pdp(vm, pdp, start, length))
805 continue;
Ben Widawsky459108b2013-11-02 21:07:23 -0700806
Chris Wilsone2b763c2017-02-15 08:43:48 +0000807 gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
Chris Wilsone2b763c2017-02-15 08:43:48 +0000808
809 free_pdp(vm, pdp);
Ben Widawsky459108b2013-11-02 21:07:23 -0700810 }
811}
812
Chris Wilson894cceb2017-02-15 08:43:37 +0000813struct sgt_dma {
814 struct scatterlist *sg;
815 dma_addr_t dma, max;
816};
817
818static __always_inline bool
819gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100820 struct i915_page_directory_pointer *pdp,
Chris Wilson894cceb2017-02-15 08:43:37 +0000821 struct sgt_dma *iter,
822 u64 start,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100823 enum i915_cache_level cache_level)
824{
Chris Wilson894cceb2017-02-15 08:43:37 +0000825 unsigned int pdpe = gen8_pdpe_index(start);
826 unsigned int pde = gen8_pde_index(start);
827 unsigned int pte = gen8_pte_index(start);
828 struct i915_page_directory *pd;
829 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
830 gen8_pte_t *vaddr;
831 bool ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700832
Chris Wilson894cceb2017-02-15 08:43:37 +0000833 pd = pdp->page_directory[pdpe];
Chris Wilson9231da72017-02-15 08:43:41 +0000834 vaddr = kmap_atomic_px(pd->page_table[pde]);
Chris Wilson894cceb2017-02-15 08:43:37 +0000835 do {
836 vaddr[pte] = pte_encode | iter->dma;
837 iter->dma += PAGE_SIZE;
838 if (iter->dma >= iter->max) {
839 iter->sg = __sg_next(iter->sg);
840 if (!iter->sg) {
841 ret = false;
842 break;
843 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700844
Chris Wilson894cceb2017-02-15 08:43:37 +0000845 iter->dma = sg_dma_address(iter->sg);
846 iter->max = iter->dma + iter->sg->length;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000847 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800848
Michel Thierry07749ef2015-03-16 16:00:54 +0000849 if (++pte == GEN8_PTES) {
Michel Thierry07749ef2015-03-16 16:00:54 +0000850 if (++pde == I915_PDES) {
Chris Wilson894cceb2017-02-15 08:43:37 +0000851 /* Limited by sg length for 3lvl */
852 if (++pdpe == GEN8_PML4ES_PER_PML4) {
853 ret = true;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100854 break;
Chris Wilson894cceb2017-02-15 08:43:37 +0000855 }
856
857 GEM_BUG_ON(pdpe > GEN8_LEGACY_PDPES);
858 pd = pdp->page_directory[pdpe];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800859 pde = 0;
860 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000861
Chris Wilson9231da72017-02-15 08:43:41 +0000862 kunmap_atomic(vaddr);
863 vaddr = kmap_atomic_px(pd->page_table[pde]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800864 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700865 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000866 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +0000867 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300868
Chris Wilson894cceb2017-02-15 08:43:37 +0000869 return ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700870}
871
Chris Wilson894cceb2017-02-15 08:43:37 +0000872static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
873 struct sg_table *pages,
874 u64 start,
875 enum i915_cache_level cache_level,
876 u32 unused)
Michel Thierryf9b5b782015-07-30 11:02:49 +0100877{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300878 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilson894cceb2017-02-15 08:43:37 +0000879 struct sgt_dma iter = {
880 .sg = pages->sgl,
881 .dma = sg_dma_address(iter.sg),
882 .max = iter.dma + iter.sg->length,
883 };
Michel Thierryf9b5b782015-07-30 11:02:49 +0100884
Chris Wilson894cceb2017-02-15 08:43:37 +0000885 gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter,
886 start, cache_level);
887}
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100888
Chris Wilson894cceb2017-02-15 08:43:37 +0000889static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
890 struct sg_table *pages,
891 uint64_t start,
892 enum i915_cache_level cache_level,
893 u32 unused)
894{
895 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
896 struct sgt_dma iter = {
897 .sg = pages->sgl,
898 .dma = sg_dma_address(iter.sg),
899 .max = iter.dma + iter.sg->length,
900 };
901 struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
902 unsigned int pml4e = gen8_pml4e_index(start);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100903
Chris Wilson894cceb2017-02-15 08:43:37 +0000904 while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[pml4e++], &iter,
905 start, cache_level))
906 ;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100907}
908
Chris Wilson84486612017-02-15 08:43:40 +0000909static void gen8_free_page_tables(struct i915_address_space *vm,
Michel Thierryf37c0502015-06-10 17:46:39 +0100910 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800911{
912 int i;
913
Mika Kuoppala567047b2015-06-25 18:35:12 +0300914 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800915 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800916
Chris Wilsonfe52e372017-02-15 08:43:47 +0000917 for (i = 0; i < I915_PDES; i++) {
918 if (pd->page_table[i] != vm->scratch_pt)
919 free_pt(vm, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000920 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000921}
922
Mika Kuoppala8776f022015-06-30 18:16:40 +0300923static int gen8_init_scratch(struct i915_address_space *vm)
924{
Matthew Auld64c050d2016-04-27 13:19:25 +0100925 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300926
Chris Wilson84486612017-02-15 08:43:40 +0000927 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100928 if (ret)
929 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300930
Chris Wilson84486612017-02-15 08:43:40 +0000931 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300932 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100933 ret = PTR_ERR(vm->scratch_pt);
934 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300935 }
936
Chris Wilson84486612017-02-15 08:43:40 +0000937 vm->scratch_pd = alloc_pd(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300938 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100939 ret = PTR_ERR(vm->scratch_pd);
940 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300941 }
942
Chris Wilson84486612017-02-15 08:43:40 +0000943 if (USES_FULL_48BIT_PPGTT(dev)) {
944 vm->scratch_pdp = alloc_pdp(vm);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100945 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100946 ret = PTR_ERR(vm->scratch_pdp);
947 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100948 }
949 }
950
Mika Kuoppala8776f022015-06-30 18:16:40 +0300951 gen8_initialize_pt(vm, vm->scratch_pt);
952 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000953 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100954 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300955
956 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100957
958free_pd:
Chris Wilson84486612017-02-15 08:43:40 +0000959 free_pd(vm, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +0100960free_pt:
Chris Wilson84486612017-02-15 08:43:40 +0000961 free_pt(vm, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +0100962free_scratch_page:
Chris Wilson84486612017-02-15 08:43:40 +0000963 cleanup_scratch_page(vm);
Matthew Auld64c050d2016-04-27 13:19:25 +0100964
965 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300966}
967
Zhiyuan Lv650da342015-08-28 15:41:18 +0800968static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
969{
970 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +0000971 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +0800972 int i;
973
Matthew Aulddf285642016-04-22 12:09:25 +0100974 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +0800975 u64 daddr = px_dma(&ppgtt->pml4);
976
Ville Syrjäläab75bb52015-11-04 23:20:12 +0200977 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
978 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +0800979
980 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
981 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
982 } else {
983 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
984 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
985
Ville Syrjäläab75bb52015-11-04 23:20:12 +0200986 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
987 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +0800988 }
989
990 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
991 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
992 }
993
994 I915_WRITE(vgtif_reg(g2v_notify), msg);
995
996 return 0;
997}
998
Mika Kuoppala8776f022015-06-30 18:16:40 +0300999static void gen8_free_scratch(struct i915_address_space *vm)
1000{
Chris Wilson84486612017-02-15 08:43:40 +00001001 if (USES_FULL_48BIT_PPGTT(vm->i915))
1002 free_pdp(vm, vm->scratch_pdp);
1003 free_pd(vm, vm->scratch_pd);
1004 free_pt(vm, vm->scratch_pt);
1005 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001006}
1007
Chris Wilson84486612017-02-15 08:43:40 +00001008static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
Michel Thierry762d9932015-07-30 11:05:29 +01001009 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001010{
1011 int i;
1012
Chris Wilsone2b763c2017-02-15 08:43:48 +00001013 for (i = 0; i < I915_PDPES_PER_PDP(vm->i915); i++) {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001014 if (pdp->page_directory[i] == vm->scratch_pd)
Ben Widawsky06fda602015-02-24 16:22:36 +00001015 continue;
1016
Chris Wilson84486612017-02-15 08:43:40 +00001017 gen8_free_page_tables(vm, pdp->page_directory[i]);
1018 free_pd(vm, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001019 }
Michel Thierry69876be2015-04-08 12:13:27 +01001020
Chris Wilson84486612017-02-15 08:43:40 +00001021 free_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001022}
1023
1024static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1025{
1026 int i;
1027
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001028 for (i = 0; i < GEN8_PML4ES_PER_PML4; i++) {
1029 if (ppgtt->pml4.pdps[i] == ppgtt->base.scratch_pdp)
Michel Thierry762d9932015-07-30 11:05:29 +01001030 continue;
1031
Chris Wilson84486612017-02-15 08:43:40 +00001032 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001033 }
1034
Chris Wilson84486612017-02-15 08:43:40 +00001035 cleanup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001036}
1037
1038static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1039{
Chris Wilson49d73912016-11-29 09:50:08 +00001040 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001041 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001042
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001043 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001044 gen8_ppgtt_notify_vgt(ppgtt, false);
1045
Chris Wilson84486612017-02-15 08:43:40 +00001046 if (!USES_FULL_48BIT_PPGTT(vm->i915))
1047 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001048 else
1049 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001050
Mika Kuoppala8776f022015-06-30 18:16:40 +03001051 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001052}
1053
Chris Wilsonfe52e372017-02-15 08:43:47 +00001054static int gen8_ppgtt_alloc_pd(struct i915_address_space *vm,
1055 struct i915_page_directory *pd,
1056 u64 start, u64 length)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001057{
Michel Thierryd7b26332015-04-08 12:13:34 +01001058 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +00001059 u64 from = start;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001060 unsigned int pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001061
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001062 gen8_for_each_pde(pt, pd, start, length, pde) {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001063 if (pt == vm->scratch_pt) {
Chris Wilsondd196742017-02-15 08:43:46 +00001064 pt = alloc_pt(vm);
1065 if (IS_ERR(pt))
1066 goto unwind;
1067
1068 gen8_initialize_pt(vm, pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001069
Chris Wilsonfe52e372017-02-15 08:43:47 +00001070 gen8_ppgtt_set_pde(vm, pd, pt, pde);
1071 pd->used_pdes++;
1072 }
1073
1074 pt->used_ptes += gen8_pte_count(start, length);
1075 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001076 return 0;
1077
Chris Wilsondd196742017-02-15 08:43:46 +00001078unwind:
1079 gen8_ppgtt_clear_pd(vm, pd, from, start - from);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001080 return -ENOMEM;
1081}
1082
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001083static int gen8_ppgtt_alloc_pdp(struct i915_address_space *vm,
1084 struct i915_page_directory_pointer *pdp,
1085 u64 start, u64 length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001086{
Michel Thierry5441f0c2015-04-08 12:13:28 +01001087 struct i915_page_directory *pd;
Chris Wilsone2b763c2017-02-15 08:43:48 +00001088 u64 from = start;
1089 unsigned int pdpe;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001090 int ret;
1091
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001092 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilsone2b763c2017-02-15 08:43:48 +00001093 if (pd == vm->scratch_pd) {
1094 pd = alloc_pd(vm);
1095 if (IS_ERR(pd))
1096 goto unwind;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001097
Chris Wilsone2b763c2017-02-15 08:43:48 +00001098 gen8_initialize_pd(vm, pd);
Chris Wilsonfe52e372017-02-15 08:43:47 +00001099 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
Chris Wilsone2b763c2017-02-15 08:43:48 +00001100 pdp->used_pdpes++;
Chris Wilson75afcf72017-02-15 08:43:51 +00001101
1102 mark_tlbs_dirty(i915_vm_to_ppgtt(vm));
Chris Wilsone2b763c2017-02-15 08:43:48 +00001103 }
1104
1105 ret = gen8_ppgtt_alloc_pd(vm, pd, start, length);
1106 if (unlikely(ret)) {
1107 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1108 pdp->used_pdpes--;
1109 free_pd(vm, pd);
1110 goto unwind;
1111 }
Chris Wilsonfe52e372017-02-15 08:43:47 +00001112 }
Michel Thierry33c88192015-04-08 12:13:33 +01001113
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001114 return 0;
1115
Chris Wilsone2b763c2017-02-15 08:43:48 +00001116unwind:
1117 gen8_ppgtt_clear_pdp(vm, pdp, from, start - from);
1118 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001119}
1120
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001121static int gen8_ppgtt_alloc_3lvl(struct i915_address_space *vm,
1122 u64 start, u64 length)
Michel Thierry762d9932015-07-30 11:05:29 +01001123{
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001124 return gen8_ppgtt_alloc_pdp(vm,
1125 &i915_vm_to_ppgtt(vm)->pdp, start, length);
1126}
1127
1128static int gen8_ppgtt_alloc_4lvl(struct i915_address_space *vm,
1129 u64 start, u64 length)
1130{
1131 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1132 struct i915_pml4 *pml4 = &ppgtt->pml4;
Michel Thierry762d9932015-07-30 11:05:29 +01001133 struct i915_page_directory_pointer *pdp;
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001134 u64 from = start;
1135 u32 pml4e;
1136 int ret;
Michel Thierry762d9932015-07-30 11:05:29 +01001137
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001138 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001139 if (pml4->pdps[pml4e] == vm->scratch_pdp) {
1140 pdp = alloc_pdp(vm);
1141 if (IS_ERR(pdp))
1142 goto unwind;
Michel Thierry762d9932015-07-30 11:05:29 +01001143
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001144 gen8_initialize_pdp(vm, pdp);
1145 gen8_ppgtt_set_pml4e(pml4, pdp, pml4e);
1146 }
Michel Thierry762d9932015-07-30 11:05:29 +01001147
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001148 ret = gen8_ppgtt_alloc_pdp(vm, pdp, start, length);
1149 if (unlikely(ret)) {
1150 gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
1151 free_pdp(vm, pdp);
1152 goto unwind;
1153 }
Michel Thierry762d9932015-07-30 11:05:29 +01001154 }
1155
Michel Thierry762d9932015-07-30 11:05:29 +01001156 return 0;
1157
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001158unwind:
1159 gen8_ppgtt_clear_4lvl(vm, from, start - from);
1160 return -ENOMEM;
Michel Thierry762d9932015-07-30 11:05:29 +01001161}
1162
Chris Wilson84486612017-02-15 08:43:40 +00001163static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
1164 struct i915_page_directory_pointer *pdp,
Michel Thierryea91e402015-07-29 17:23:57 +01001165 uint64_t start, uint64_t length,
1166 gen8_pte_t scratch_pte,
1167 struct seq_file *m)
1168{
1169 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001170 uint32_t pdpe;
1171
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001172 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001173 struct i915_page_table *pt;
1174 uint64_t pd_len = length;
1175 uint64_t pd_start = start;
1176 uint32_t pde;
1177
Chris Wilsone2b763c2017-02-15 08:43:48 +00001178 if (pdp->page_directory[pdpe] == ppgtt->base.scratch_pd)
Michel Thierryea91e402015-07-29 17:23:57 +01001179 continue;
1180
1181 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001182 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001183 uint32_t pte;
1184 gen8_pte_t *pt_vaddr;
1185
Chris Wilsonfe52e372017-02-15 08:43:47 +00001186 if (pd->page_table[pde] == ppgtt->base.scratch_pt)
Michel Thierryea91e402015-07-29 17:23:57 +01001187 continue;
1188
Chris Wilson9231da72017-02-15 08:43:41 +00001189 pt_vaddr = kmap_atomic_px(pt);
Michel Thierryea91e402015-07-29 17:23:57 +01001190 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1191 uint64_t va =
1192 (pdpe << GEN8_PDPE_SHIFT) |
1193 (pde << GEN8_PDE_SHIFT) |
1194 (pte << GEN8_PTE_SHIFT);
1195 int i;
1196 bool found = false;
1197
1198 for (i = 0; i < 4; i++)
1199 if (pt_vaddr[pte + i] != scratch_pte)
1200 found = true;
1201 if (!found)
1202 continue;
1203
1204 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1205 for (i = 0; i < 4; i++) {
1206 if (pt_vaddr[pte + i] != scratch_pte)
1207 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1208 else
1209 seq_puts(m, " SCRATCH ");
1210 }
1211 seq_puts(m, "\n");
1212 }
Michel Thierryea91e402015-07-29 17:23:57 +01001213 kunmap_atomic(pt_vaddr);
1214 }
1215 }
1216}
1217
1218static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1219{
1220 struct i915_address_space *vm = &ppgtt->base;
1221 uint64_t start = ppgtt->base.start;
1222 uint64_t length = ppgtt->base.total;
Chris Wilson894cceb2017-02-15 08:43:37 +00001223 const gen8_pte_t scratch_pte =
1224 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001225
Chris Wilsonc6385c92016-11-29 12:42:05 +00001226 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Chris Wilson84486612017-02-15 08:43:40 +00001227 gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001228 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001229 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001230 struct i915_pml4 *pml4 = &ppgtt->pml4;
1231 struct i915_page_directory_pointer *pdp;
1232
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001233 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001234 if (pml4->pdps[pml4e] == ppgtt->base.scratch_pdp)
Michel Thierryea91e402015-07-29 17:23:57 +01001235 continue;
1236
1237 seq_printf(m, " PML4E #%llu\n", pml4e);
Chris Wilson84486612017-02-15 08:43:40 +00001238 gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001239 }
1240 }
1241}
1242
Chris Wilsone2b763c2017-02-15 08:43:48 +00001243static int gen8_preallocate_top_level_pdp(struct i915_hw_ppgtt *ppgtt)
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001244{
Chris Wilsone2b763c2017-02-15 08:43:48 +00001245 struct i915_address_space *vm = &ppgtt->base;
1246 struct i915_page_directory_pointer *pdp = &ppgtt->pdp;
1247 struct i915_page_directory *pd;
1248 u64 start = 0, length = ppgtt->base.total;
1249 u64 from = start;
1250 unsigned int pdpe;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001251
Chris Wilsone2b763c2017-02-15 08:43:48 +00001252 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1253 pd = alloc_pd(vm);
1254 if (IS_ERR(pd))
1255 goto unwind;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001256
Chris Wilsone2b763c2017-02-15 08:43:48 +00001257 gen8_initialize_pd(vm, pd);
1258 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
1259 pdp->used_pdpes++;
1260 }
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001261
Chris Wilsone2b763c2017-02-15 08:43:48 +00001262 pdp->used_pdpes++; /* never remove */
1263 return 0;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001264
Chris Wilsone2b763c2017-02-15 08:43:48 +00001265unwind:
1266 start -= from;
1267 gen8_for_each_pdpe(pd, pdp, from, start, pdpe) {
1268 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1269 free_pd(vm, pd);
1270 }
1271 pdp->used_pdpes = 0;
1272 return -ENOMEM;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001273}
1274
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001275/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001276 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1277 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1278 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1279 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001280 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001281 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001282static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001283{
Chris Wilson49d73912016-11-29 09:50:08 +00001284 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001285 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001286
Mika Kuoppala8776f022015-06-30 18:16:40 +03001287 ret = gen8_init_scratch(&ppgtt->base);
1288 if (ret)
1289 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001290
Michel Thierryd7b26332015-04-08 12:13:34 +01001291 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001292 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001293 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1294 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001295 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001296
Chris Wilson84486612017-02-15 08:43:40 +00001297 /* There are only few exceptions for gen >=6. chv and bxt.
1298 * And we are not sure about the latter so play safe for now.
1299 */
1300 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
1301 ppgtt->base.pt_kmap_wc = true;
1302
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001303 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Chris Wilson84486612017-02-15 08:43:40 +00001304 ret = setup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001305 if (ret)
1306 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001307
Michel Thierry69ab76f2015-07-29 17:23:55 +01001308 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1309
Michel Thierry762d9932015-07-30 11:05:29 +01001310 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001311 ppgtt->switch_mm = gen8_48b_mm_switch;
Chris Wilson894cceb2017-02-15 08:43:37 +00001312
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001313 ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_4lvl;
Chris Wilson894cceb2017-02-15 08:43:37 +00001314 ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001315 ppgtt->base.clear_range = gen8_ppgtt_clear_4lvl;
Michel Thierry762d9932015-07-30 11:05:29 +01001316 } else {
Chris Wilsonfe52e372017-02-15 08:43:47 +00001317 ret = __pdp_init(&ppgtt->base, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001318 if (ret)
1319 goto free_scratch;
1320
1321 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001322 ppgtt->switch_mm = gen8_legacy_mm_switch;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001323
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001324 if (intel_vgpu_active(dev_priv)) {
Chris Wilsone2b763c2017-02-15 08:43:48 +00001325 ret = gen8_preallocate_top_level_pdp(ppgtt);
1326 if (ret) {
1327 __pdp_fini(&ppgtt->pdp);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001328 goto free_scratch;
Chris Wilsone2b763c2017-02-15 08:43:48 +00001329 }
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001330 }
Chris Wilson894cceb2017-02-15 08:43:37 +00001331
Chris Wilsonc5d092a2017-02-15 08:43:49 +00001332 ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_3lvl;
Chris Wilson894cceb2017-02-15 08:43:37 +00001333 ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
Chris Wilsonfe52e372017-02-15 08:43:47 +00001334 ppgtt->base.clear_range = gen8_ppgtt_clear_3lvl;
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001335 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001336
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001337 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001338 gen8_ppgtt_notify_vgt(ppgtt, true);
1339
Michel Thierryd7b26332015-04-08 12:13:34 +01001340 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001341
1342free_scratch:
1343 gen8_free_scratch(&ppgtt->base);
1344 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001345}
1346
Ben Widawsky87d60b62013-12-06 14:11:29 -08001347static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1348{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001349 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001350 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001351 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001352 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001353 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001354 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001355
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001356 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001357 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001358
Dave Gordon731f74c2016-06-24 19:37:46 +01001359 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001360 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001361 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001362 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001363 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001364 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1365
1366 if (pd_entry != expected)
1367 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1368 pde,
1369 pd_entry,
1370 expected);
1371 seq_printf(m, "\tPDE: %x\n", pd_entry);
1372
Chris Wilson9231da72017-02-15 08:43:41 +00001373 pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[pde]);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001374
Michel Thierry07749ef2015-03-16 16:00:54 +00001375 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001376 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001377 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001378 (pte * PAGE_SIZE);
1379 int i;
1380 bool found = false;
1381 for (i = 0; i < 4; i++)
1382 if (pt_vaddr[pte + i] != scratch_pte)
1383 found = true;
1384 if (!found)
1385 continue;
1386
1387 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1388 for (i = 0; i < 4; i++) {
1389 if (pt_vaddr[pte + i] != scratch_pte)
1390 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1391 else
1392 seq_puts(m, " SCRATCH ");
1393 }
1394 seq_puts(m, "\n");
1395 }
Chris Wilson9231da72017-02-15 08:43:41 +00001396 kunmap_atomic(pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001397 }
1398}
1399
Ben Widawsky678d96f2015-03-16 16:00:56 +00001400/* Write pde (index) from the page directory @pd to the page table @pt */
Chris Wilson16a011c2017-02-15 08:43:45 +00001401static inline void gen6_write_pde(const struct i915_hw_ppgtt *ppgtt,
1402 const unsigned int pde,
1403 const struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001404{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001405 /* Caller needs to make sure the write completes if necessary */
Chris Wilson16a011c2017-02-15 08:43:45 +00001406 writel_relaxed(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
1407 ppgtt->pd_addr + pde);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001408}
Ben Widawsky61973492013-04-08 18:43:54 -07001409
Ben Widawsky678d96f2015-03-16 16:00:56 +00001410/* Write all the page tables found in the ppgtt structure to incrementing page
1411 * directories. */
Chris Wilson16a011c2017-02-15 08:43:45 +00001412static void gen6_write_page_range(struct i915_hw_ppgtt *ppgtt,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001413 uint32_t start, uint32_t length)
1414{
Michel Thierryec565b32015-04-08 12:13:23 +01001415 struct i915_page_table *pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001416 unsigned int pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001417
Chris Wilson16a011c2017-02-15 08:43:45 +00001418 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde)
1419 gen6_write_pde(ppgtt, pde, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001420
Chris Wilson16a011c2017-02-15 08:43:45 +00001421 mark_tlbs_dirty(ppgtt);
Chris Wilsondd196742017-02-15 08:43:46 +00001422 wmb();
Ben Widawsky3e302542013-04-23 23:15:32 -07001423}
1424
Chris Wilsondd196742017-02-15 08:43:46 +00001425static inline uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001426{
Chris Wilsondd196742017-02-15 08:43:46 +00001427 GEM_BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1428 return ppgtt->pd.base.ggtt_offset << 10;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001429}
Ben Widawsky61973492013-04-08 18:43:54 -07001430
Ben Widawsky90252e52013-12-06 14:11:12 -08001431static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001432 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001433{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001434 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001435 u32 *cs;
Ben Widawsky90252e52013-12-06 14:11:12 -08001436 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001437
Ben Widawsky90252e52013-12-06 14:11:12 -08001438 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001439 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001440 if (ret)
1441 return ret;
1442
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001443 cs = intel_ring_begin(req, 6);
1444 if (IS_ERR(cs))
1445 return PTR_ERR(cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001446
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001447 *cs++ = MI_LOAD_REGISTER_IMM(2);
1448 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1449 *cs++ = PP_DIR_DCLV_2G;
1450 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1451 *cs++ = get_pd_offset(ppgtt);
1452 *cs++ = MI_NOOP;
1453 intel_ring_advance(req, cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001454
1455 return 0;
1456}
1457
Ben Widawsky48a10382013-12-06 14:11:11 -08001458static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001459 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001460{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001461 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001462 u32 *cs;
Ben Widawsky48a10382013-12-06 14:11:11 -08001463 int ret;
1464
Ben Widawsky48a10382013-12-06 14:11:11 -08001465 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001466 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001467 if (ret)
1468 return ret;
1469
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001470 cs = intel_ring_begin(req, 6);
1471 if (IS_ERR(cs))
1472 return PTR_ERR(cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001473
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001474 *cs++ = MI_LOAD_REGISTER_IMM(2);
1475 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1476 *cs++ = PP_DIR_DCLV_2G;
1477 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1478 *cs++ = get_pd_offset(ppgtt);
1479 *cs++ = MI_NOOP;
1480 intel_ring_advance(req, cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001481
Ben Widawsky90252e52013-12-06 14:11:12 -08001482 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001483 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001484 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001485 if (ret)
1486 return ret;
1487 }
1488
Ben Widawsky48a10382013-12-06 14:11:11 -08001489 return 0;
1490}
1491
Ben Widawskyeeb94882013-12-06 14:11:10 -08001492static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001493 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001494{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001495 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001496 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001497
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001498 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1499 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001500 return 0;
1501}
1502
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001503static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001504{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001505 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301506 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001507
Akash Goel3b3f1652016-10-13 22:44:48 +05301508 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001509 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1510 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001511 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001512 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001513 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001514}
1515
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001516static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001517{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001518 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001519 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301520 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001521
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001522 ecobits = I915_READ(GAC_ECO_BITS);
1523 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1524
1525 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001526 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001527 ecochk |= ECOCHK_PPGTT_WB_HSW;
1528 } else {
1529 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1530 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1531 }
1532 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001533
Akash Goel3b3f1652016-10-13 22:44:48 +05301534 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001535 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001536 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001537 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001538 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001539}
1540
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001541static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001542{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001543 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001544
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001545 ecobits = I915_READ(GAC_ECO_BITS);
1546 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1547 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001548
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001549 gab_ctl = I915_READ(GAB_CTL);
1550 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001551
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001552 ecochk = I915_READ(GAM_ECOCHK);
1553 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001554
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001555 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001556}
1557
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001558/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001559static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Chris Wilsondd196742017-02-15 08:43:46 +00001560 u64 start, u64 length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001561{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001562 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilsondd196742017-02-15 08:43:46 +00001563 unsigned int first_entry = start >> PAGE_SHIFT;
1564 unsigned int pde = first_entry / GEN6_PTES;
1565 unsigned int pte = first_entry % GEN6_PTES;
1566 unsigned int num_entries = length >> PAGE_SHIFT;
1567 gen6_pte_t scratch_pte =
1568 vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001569
Daniel Vetter7bddb012012-02-09 17:15:47 +01001570 while (num_entries) {
Chris Wilsondd196742017-02-15 08:43:46 +00001571 struct i915_page_table *pt = ppgtt->pd.page_table[pde++];
1572 unsigned int end = min(pte + num_entries, GEN6_PTES);
1573 gen6_pte_t *vaddr;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001574
Chris Wilsondd196742017-02-15 08:43:46 +00001575 num_entries -= end - pte;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001576
Chris Wilsondd196742017-02-15 08:43:46 +00001577 /* Note that the hw doesn't support removing PDE on the fly
1578 * (they are cached inside the context with no means to
1579 * invalidate the cache), so we can only reset the PTE
1580 * entries back to scratch.
1581 */
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001582
Chris Wilsondd196742017-02-15 08:43:46 +00001583 vaddr = kmap_atomic_px(pt);
1584 do {
1585 vaddr[pte++] = scratch_pte;
1586 } while (pte < end);
1587 kunmap_atomic(vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001588
Chris Wilsondd196742017-02-15 08:43:46 +00001589 pte = 0;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001590 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001591}
1592
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001593static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001594 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001595 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301596 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001597{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001598 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001599 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001600 unsigned act_pt = first_entry / GEN6_PTES;
1601 unsigned act_pte = first_entry % GEN6_PTES;
Chris Wilsonb31144c2017-02-15 08:43:36 +00001602 const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1603 struct sgt_dma iter;
1604 gen6_pte_t *vaddr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001605
Chris Wilson9231da72017-02-15 08:43:41 +00001606 vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
Chris Wilsonb31144c2017-02-15 08:43:36 +00001607 iter.sg = pages->sgl;
1608 iter.dma = sg_dma_address(iter.sg);
1609 iter.max = iter.dma + iter.sg->length;
1610 do {
1611 vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001612
Chris Wilsonb31144c2017-02-15 08:43:36 +00001613 iter.dma += PAGE_SIZE;
1614 if (iter.dma == iter.max) {
1615 iter.sg = __sg_next(iter.sg);
1616 if (!iter.sg)
1617 break;
1618
1619 iter.dma = sg_dma_address(iter.sg);
1620 iter.max = iter.dma + iter.sg->length;
1621 }
Akash Goel24f3a8c2014-06-17 10:59:42 +05301622
Michel Thierry07749ef2015-03-16 16:00:54 +00001623 if (++act_pte == GEN6_PTES) {
Chris Wilson9231da72017-02-15 08:43:41 +00001624 kunmap_atomic(vaddr);
1625 vaddr = kmap_atomic_px(ppgtt->pd.page_table[++act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +02001626 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001627 }
Chris Wilsonb31144c2017-02-15 08:43:36 +00001628 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +00001629 kunmap_atomic(vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001630}
1631
Ben Widawsky678d96f2015-03-16 16:00:56 +00001632static int gen6_alloc_va_range(struct i915_address_space *vm,
Chris Wilsondd196742017-02-15 08:43:46 +00001633 u64 start, u64 length)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001634{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001635 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001636 struct i915_page_table *pt;
Chris Wilsondd196742017-02-15 08:43:46 +00001637 u64 from = start;
1638 unsigned int pde;
1639 bool flush = false;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001640
Dave Gordon731f74c2016-06-24 19:37:46 +01001641 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Chris Wilsondd196742017-02-15 08:43:46 +00001642 if (pt == vm->scratch_pt) {
1643 pt = alloc_pt(vm);
1644 if (IS_ERR(pt))
1645 goto unwind_out;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001646
Chris Wilsondd196742017-02-15 08:43:46 +00001647 gen6_initialize_pt(vm, pt);
1648 ppgtt->pd.page_table[pde] = pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001649 gen6_write_pde(ppgtt, pde, pt);
Chris Wilsondd196742017-02-15 08:43:46 +00001650 flush = true;
1651 }
Ben Widawsky678d96f2015-03-16 16:00:56 +00001652 }
1653
Chris Wilsondd196742017-02-15 08:43:46 +00001654 if (flush) {
1655 mark_tlbs_dirty(ppgtt);
1656 wmb();
1657 }
Michel Thierry4933d512015-03-24 15:46:22 +00001658
Ben Widawsky678d96f2015-03-16 16:00:56 +00001659 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001660
1661unwind_out:
Chris Wilsondd196742017-02-15 08:43:46 +00001662 gen6_ppgtt_clear_range(vm, from, start);
1663 return -ENOMEM;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001664}
1665
Mika Kuoppala8776f022015-06-30 18:16:40 +03001666static int gen6_init_scratch(struct i915_address_space *vm)
1667{
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001668 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001669
Chris Wilson84486612017-02-15 08:43:40 +00001670 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001671 if (ret)
1672 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001673
Chris Wilson84486612017-02-15 08:43:40 +00001674 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001675 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson84486612017-02-15 08:43:40 +00001676 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001677 return PTR_ERR(vm->scratch_pt);
1678 }
1679
1680 gen6_initialize_pt(vm, vm->scratch_pt);
1681
1682 return 0;
1683}
1684
1685static void gen6_free_scratch(struct i915_address_space *vm)
1686{
Chris Wilson84486612017-02-15 08:43:40 +00001687 free_pt(vm, vm->scratch_pt);
1688 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001689}
1690
Daniel Vetter061dd492015-04-14 17:35:13 +02001691static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08001692{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001693 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01001694 struct i915_page_directory *pd = &ppgtt->pd;
Michel Thierry09942c62015-04-08 12:13:30 +01001695 struct i915_page_table *pt;
1696 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08001697
Daniel Vetter061dd492015-04-14 17:35:13 +02001698 drm_mm_remove_node(&ppgtt->node);
1699
Dave Gordon731f74c2016-06-24 19:37:46 +01001700 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001701 if (pt != vm->scratch_pt)
Chris Wilson84486612017-02-15 08:43:40 +00001702 free_pt(vm, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001703
Mika Kuoppala8776f022015-06-30 18:16:40 +03001704 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08001705}
1706
Ben Widawskyb1465202014-02-19 22:05:49 -08001707static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001708{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001709 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00001710 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001711 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08001712 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001713
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001714 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1715 * allocator works in address space sizes, so it's multiplied by page
1716 * size. We allocate at the top of the GTT to avoid fragmentation.
1717 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001718 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00001719
Mika Kuoppala8776f022015-06-30 18:16:40 +03001720 ret = gen6_init_scratch(vm);
1721 if (ret)
1722 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00001723
Chris Wilsone007b192017-01-11 11:23:10 +00001724 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
1725 GEN6_PD_SIZE, GEN6_PD_ALIGN,
1726 I915_COLOR_UNEVICTABLE,
1727 0, ggtt->base.total,
1728 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00001729 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001730 goto err_out;
1731
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001732 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001733 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001734
Chris Wilson52c126e2017-02-15 08:43:43 +00001735 ppgtt->pd.base.ggtt_offset =
1736 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
1737
1738 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
1739 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
1740
Ben Widawskyc8c26622015-01-22 17:01:25 +00001741 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001742
1743err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03001744 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001745 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08001746}
1747
Ben Widawskyb1465202014-02-19 22:05:49 -08001748static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1749{
kbuild test robot2f2cf682015-03-27 19:26:35 +08001750 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08001751}
1752
Michel Thierry4933d512015-03-24 15:46:22 +00001753static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
1754 uint64_t start, uint64_t length)
1755{
Michel Thierryec565b32015-04-08 12:13:23 +01001756 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01001757 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001758
Dave Gordon731f74c2016-06-24 19:37:46 +01001759 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001760 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00001761}
1762
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001763static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08001764{
Chris Wilson49d73912016-11-29 09:50:08 +00001765 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001766 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08001767 int ret;
1768
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001769 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001770 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08001771 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001772 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08001773 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001774 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08001775 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01001776 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001777 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001778
1779 ret = gen6_ppgtt_alloc(ppgtt);
1780 if (ret)
1781 return ret;
1782
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001783 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1784 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001785 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1786 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001787 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08001788 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01001789 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001790 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001791
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001792 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Chris Wilson16a011c2017-02-15 08:43:45 +00001793 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001794
Chris Wilson52c126e2017-02-15 08:43:43 +00001795 ret = gen6_alloc_va_range(&ppgtt->base, 0, ppgtt->base.total);
1796 if (ret) {
1797 gen6_ppgtt_cleanup(&ppgtt->base);
1798 return ret;
1799 }
1800
Thierry Reding440fd522015-01-23 09:05:06 +01001801 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001802 ppgtt->node.size >> 20,
1803 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001804
Chris Wilson52c126e2017-02-15 08:43:43 +00001805 DRM_DEBUG_DRIVER("Adding PPGTT at offset %x\n",
1806 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001807
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001808 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001809}
1810
Chris Wilson2bfa9962016-08-04 07:52:25 +01001811static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
1812 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08001813{
Chris Wilson49d73912016-11-29 09:50:08 +00001814 ppgtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00001815 ppgtt->base.dma = &dev_priv->drm.pdev->dev;
Daniel Vetter3440d262013-01-24 13:49:56 -08001816
Chris Wilson2bfa9962016-08-04 07:52:25 +01001817 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001818 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001819 else
Michel Thierryd7b26332015-04-08 12:13:34 +01001820 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001821}
Mika Kuoppalac114f762015-06-25 18:35:13 +03001822
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001823static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01001824 struct drm_i915_private *dev_priv,
1825 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001826{
Chris Wilson80b204b2016-10-28 13:58:58 +01001827 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00001828
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001829 drm_mm_init(&vm->mm, vm->start, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00001830 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
1831
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001832 INIT_LIST_HEAD(&vm->active_list);
1833 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01001834 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00001835
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001836 list_add_tail(&vm->global_link, &dev_priv->vm_list);
Chris Wilson84486612017-02-15 08:43:40 +00001837 pagevec_init(&vm->free_pages, false);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02001838}
1839
Matthew Aulded9724d2016-11-17 21:04:10 +00001840static void i915_address_space_fini(struct i915_address_space *vm)
1841{
Chris Wilson84486612017-02-15 08:43:40 +00001842 if (pagevec_count(&vm->free_pages))
1843 vm_free_pages_release(vm);
1844
Matthew Aulded9724d2016-11-17 21:04:10 +00001845 i915_gem_timeline_fini(&vm->timeline);
1846 drm_mm_takedown(&vm->mm);
1847 list_del(&vm->global_link);
1848}
1849
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001850static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00001851{
Tim Gored5165eb2016-02-04 11:49:34 +00001852 /* This function is for gtt related workarounds. This function is
1853 * called on driver load and after a GPU reset, so you can place
1854 * workarounds here even if they get overwritten by GPU reset.
1855 */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001856 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01001857 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001858 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01001859 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001860 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08001861 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001862 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02001863 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00001864 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
1865}
1866
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001867int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02001868{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001869 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00001870
Thomas Daniel671b50132014-08-20 16:24:50 +01001871 /* In the case of execlists, PPGTT is enabled by the context descriptor
1872 * and the PDPs are contained within the context itself. We don't
1873 * need to do anything here. */
1874 if (i915.enable_execlists)
1875 return 0;
1876
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001877 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02001878 return 0;
1879
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001880 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001881 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001882 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001883 gen7_ppgtt_enable(dev_priv);
1884 else if (INTEL_GEN(dev_priv) >= 8)
1885 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02001886 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001887 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02001888
John Harrison4ad2fd82015-06-18 13:11:20 +01001889 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001890}
John Harrison4ad2fd82015-06-18 13:11:20 +01001891
Daniel Vetter4d884702014-08-06 15:04:47 +02001892struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01001893i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01001894 struct drm_i915_file_private *fpriv,
1895 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02001896{
1897 struct i915_hw_ppgtt *ppgtt;
1898 int ret;
1899
1900 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1901 if (!ppgtt)
1902 return ERR_PTR(-ENOMEM);
1903
Chris Wilson1188bc62017-02-15 08:43:38 +00001904 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetter4d884702014-08-06 15:04:47 +02001905 if (ret) {
1906 kfree(ppgtt);
1907 return ERR_PTR(ret);
1908 }
1909
Chris Wilson1188bc62017-02-15 08:43:38 +00001910 kref_init(&ppgtt->ref);
1911 i915_address_space_init(&ppgtt->base, dev_priv, name);
1912 ppgtt->base.file = fpriv;
1913
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001914 trace_i915_ppgtt_create(&ppgtt->base);
1915
Daniel Vetter4d884702014-08-06 15:04:47 +02001916 return ppgtt;
1917}
1918
Chris Wilson0c7eeda2017-01-11 21:09:25 +00001919void i915_ppgtt_close(struct i915_address_space *vm)
1920{
1921 struct list_head *phases[] = {
1922 &vm->active_list,
1923 &vm->inactive_list,
1924 &vm->unbound_list,
1925 NULL,
1926 }, **phase;
1927
1928 GEM_BUG_ON(vm->closed);
1929 vm->closed = true;
1930
1931 for (phase = phases; *phase; phase++) {
1932 struct i915_vma *vma, *vn;
1933
1934 list_for_each_entry_safe(vma, vn, *phase, vm_link)
1935 if (!i915_vma_is_closed(vma))
1936 i915_vma_close(vma);
1937 }
1938}
1939
Matthew Aulded9724d2016-11-17 21:04:10 +00001940void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02001941{
1942 struct i915_hw_ppgtt *ppgtt =
1943 container_of(kref, struct i915_hw_ppgtt, ref);
1944
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001945 trace_i915_ppgtt_release(&ppgtt->base);
1946
Chris Wilson50e046b2016-08-04 07:52:46 +01001947 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02001948 WARN_ON(!list_empty(&ppgtt->base.active_list));
1949 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01001950 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02001951
1952 ppgtt->base.cleanup(&ppgtt->base);
Chris Wilson84486612017-02-15 08:43:40 +00001953 i915_address_space_fini(&ppgtt->base);
Daniel Vetteree960be2014-08-06 15:04:45 +02001954 kfree(ppgtt);
1955}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001956
Ben Widawskya81cc002013-01-18 12:30:31 -08001957/* Certain Gen5 chipsets require require idling the GPU before
1958 * unmapping anything from the GTT when VT-d is enabled.
1959 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001960static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08001961{
1962#ifdef CONFIG_INTEL_IOMMU
1963 /* Query intel_iommu to see if we need the workaround. Presumably that
1964 * was loaded first.
1965 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01001966 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08001967 return true;
1968#endif
1969 return false;
1970}
1971
Chris Wilsondc979972016-05-10 14:10:04 +01001972void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07001973{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001974 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301975 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07001976
Chris Wilsondc979972016-05-10 14:10:04 +01001977 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07001978 return;
1979
Akash Goel3b3f1652016-10-13 22:44:48 +05301980 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07001981 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001982 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07001983 if (fault_reg & RING_FAULT_VALID) {
1984 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02001985 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07001986 "\tAddress space: %s\n"
1987 "\tSource ID: %d\n"
1988 "\tType: %d\n",
1989 fault_reg & PAGE_MASK,
1990 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1991 RING_FAULT_SRCID(fault_reg),
1992 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001993 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07001994 fault_reg & ~RING_FAULT_VALID);
1995 }
1996 }
Akash Goel3b3f1652016-10-13 22:44:48 +05301997
1998 /* Engine specific init may not have been done till this point. */
1999 if (dev_priv->engine[RCS])
2000 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002001}
2002
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002003void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002004{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002005 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002006
2007 /* Don't bother messing with faults pre GEN6 as we have little
2008 * documentation supporting that it's a good idea.
2009 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002010 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002011 return;
2012
Chris Wilsondc979972016-05-10 14:10:04 +01002013 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002014
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002015 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002016
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002017 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002018}
2019
Chris Wilson03ac84f2016-10-28 13:58:36 +01002020int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2021 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002022{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002023 do {
2024 if (dma_map_sg(&obj->base.dev->pdev->dev,
2025 pages->sgl, pages->nents,
2026 PCI_DMA_BIDIRECTIONAL))
2027 return 0;
2028
2029 /* If the DMA remap fails, one cause can be that we have
2030 * too many objects pinned in a small remapping table,
2031 * such as swiotlb. Incrementally purge all other objects and
2032 * try again - if there are no more pages to remove from
2033 * the DMA remapper, i915_gem_shrink will return 0.
2034 */
2035 GEM_BUG_ON(obj->mm.pages == pages);
2036 } while (i915_gem_shrink(to_i915(obj->base.dev),
2037 obj->base.size >> PAGE_SHIFT,
2038 I915_SHRINK_BOUND |
2039 I915_SHRINK_UNBOUND |
2040 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002041
Chris Wilson03ac84f2016-10-28 13:58:36 +01002042 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002043}
2044
Daniel Vetter2c642b02015-04-14 17:35:26 +02002045static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002046{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002047 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002048}
2049
Chris Wilsond6473f52016-06-10 14:22:59 +05302050static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2051 dma_addr_t addr,
2052 uint64_t offset,
2053 enum i915_cache_level level,
2054 u32 unused)
2055{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002056 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302057 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002058 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302059
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002060 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302061
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002062 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302063}
2064
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002065static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2066 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002067 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302068 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002069{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002070 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002071 struct sgt_iter sgt_iter;
2072 gen8_pte_t __iomem *gtt_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +00002073 const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002074 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002075
Chris Wilson894cceb2017-02-15 08:43:37 +00002076 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
2077 gtt_entries += start >> PAGE_SHIFT;
2078 for_each_sgt_dma(addr, sgt_iter, st)
2079 gen8_set_pte(gtt_entries++, pte_encode | addr);
Dave Gordon85d12252016-05-20 11:54:06 +01002080
Chris Wilson894cceb2017-02-15 08:43:37 +00002081 wmb();
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002082
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002083 /* This next bit makes the above posting read even more important. We
2084 * want to flush the TLBs only after we're certain all the PTE updates
2085 * have finished.
2086 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002087 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002088}
2089
Chris Wilsonc1403302015-11-18 15:19:39 +00002090struct insert_entries {
2091 struct i915_address_space *vm;
2092 struct sg_table *st;
2093 uint64_t start;
2094 enum i915_cache_level level;
2095 u32 flags;
2096};
2097
2098static int gen8_ggtt_insert_entries__cb(void *_arg)
2099{
2100 struct insert_entries *arg = _arg;
2101 gen8_ggtt_insert_entries(arg->vm, arg->st,
2102 arg->start, arg->level, arg->flags);
2103 return 0;
2104}
2105
2106static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2107 struct sg_table *st,
2108 uint64_t start,
2109 enum i915_cache_level level,
2110 u32 flags)
2111{
2112 struct insert_entries arg = { vm, st, start, level, flags };
2113 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2114}
2115
Chris Wilsond6473f52016-06-10 14:22:59 +05302116static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2117 dma_addr_t addr,
2118 uint64_t offset,
2119 enum i915_cache_level level,
2120 u32 flags)
2121{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002122 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302123 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002124 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302125
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002126 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302127
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002128 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302129}
2130
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002131/*
2132 * Binds an object into the global gtt with the specified cache level. The object
2133 * will be accessible to the GPU via commands whose operands reference offsets
2134 * within the global GTT as well as accessible by the GPU through the GMADR
2135 * mapped BAR (dev_priv->mm.gtt->gtt).
2136 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002137static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002138 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002139 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302140 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002141{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002142 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsonb31144c2017-02-15 08:43:36 +00002143 gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
2144 unsigned int i = start >> PAGE_SHIFT;
2145 struct sgt_iter iter;
Dave Gordon85d12252016-05-20 11:54:06 +01002146 dma_addr_t addr;
Chris Wilsonb31144c2017-02-15 08:43:36 +00002147 for_each_sgt_dma(addr, iter, st)
2148 iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2149 wmb();
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002150
2151 /* This next bit makes the above posting read even more important. We
2152 * want to flush the TLBs only after we're certain all the PTE updates
2153 * have finished.
2154 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002155 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002156}
2157
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002158static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002159 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002160{
2161}
2162
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002163static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002164 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002165{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002166 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002167 unsigned first_entry = start >> PAGE_SHIFT;
2168 unsigned num_entries = length >> PAGE_SHIFT;
Chris Wilson894cceb2017-02-15 08:43:37 +00002169 const gen8_pte_t scratch_pte =
2170 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
2171 gen8_pte_t __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002172 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2173 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002174 int i;
2175
2176 if (WARN(num_entries > max_entries,
2177 "First entry = %d; Num entries = %d (max=%d)\n",
2178 first_entry, num_entries, max_entries))
2179 num_entries = max_entries;
2180
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002181 for (i = 0; i < num_entries; i++)
2182 gen8_set_pte(&gtt_base[i], scratch_pte);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002183}
2184
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002185static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002186 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002187 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002188{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002189 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002190 unsigned first_entry = start >> PAGE_SHIFT;
2191 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002192 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002193 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2194 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002195 int i;
2196
2197 if (WARN(num_entries > max_entries,
2198 "First entry = %d; Num entries = %d (max=%d)\n",
2199 first_entry, num_entries, max_entries))
2200 num_entries = max_entries;
2201
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002202 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002203 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002204
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002205 for (i = 0; i < num_entries; i++)
2206 iowrite32(scratch_pte, &gtt_base[i]);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002207}
2208
Chris Wilsond6473f52016-06-10 14:22:59 +05302209static void i915_ggtt_insert_page(struct i915_address_space *vm,
2210 dma_addr_t addr,
2211 uint64_t offset,
2212 enum i915_cache_level cache_level,
2213 u32 unused)
2214{
Chris Wilsond6473f52016-06-10 14:22:59 +05302215 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2216 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302217
2218 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302219}
2220
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002221static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2222 struct sg_table *pages,
2223 uint64_t start,
2224 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002225{
2226 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2227 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2228
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002229 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002230}
2231
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002232static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002233 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002234 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002235{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002236 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002237}
2238
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002239static int ggtt_bind_vma(struct i915_vma *vma,
2240 enum i915_cache_level cache_level,
2241 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002242{
Chris Wilson49d73912016-11-29 09:50:08 +00002243 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002244 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonba7a5742017-02-15 08:43:35 +00002245 u32 pte_flags;
Daniel Vetter0a878712015-10-15 14:23:01 +02002246
Chris Wilsonba7a5742017-02-15 08:43:35 +00002247 if (unlikely(!vma->pages)) {
2248 int ret = i915_get_ggtt_vma_pages(vma);
2249 if (ret)
2250 return ret;
2251 }
Daniel Vetter0a878712015-10-15 14:23:01 +02002252
2253 /* Currently applicable only to VLV */
Chris Wilsonba7a5742017-02-15 08:43:35 +00002254 pte_flags = 0;
Daniel Vetter0a878712015-10-15 14:23:01 +02002255 if (obj->gt_ro)
2256 pte_flags |= PTE_READ_ONLY;
2257
Chris Wilson9c870d02016-10-24 13:42:15 +01002258 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002259 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002260 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002261 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002262
2263 /*
2264 * Without aliasing PPGTT there's no difference between
2265 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2266 * upgrade to both bound if we bind either to avoid double-binding.
2267 */
Chris Wilson3272db52016-08-04 16:32:32 +01002268 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002269
2270 return 0;
2271}
2272
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002273static void ggtt_unbind_vma(struct i915_vma *vma)
2274{
2275 struct drm_i915_private *i915 = vma->vm->i915;
2276
2277 intel_runtime_pm_get(i915);
2278 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2279 intel_runtime_pm_put(i915);
2280}
2281
Daniel Vetter0a878712015-10-15 14:23:01 +02002282static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2283 enum i915_cache_level cache_level,
2284 u32 flags)
2285{
Chris Wilson49d73912016-11-29 09:50:08 +00002286 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002287 u32 pte_flags;
Chris Wilsonff685972017-02-15 08:43:42 +00002288 int ret;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002289
Chris Wilsonba7a5742017-02-15 08:43:35 +00002290 if (unlikely(!vma->pages)) {
Chris Wilsonff685972017-02-15 08:43:42 +00002291 ret = i915_get_ggtt_vma_pages(vma);
Chris Wilsonba7a5742017-02-15 08:43:35 +00002292 if (ret)
2293 return ret;
2294 }
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002295
Akash Goel24f3a8c2014-06-17 10:59:42 +05302296 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002297 pte_flags = 0;
2298 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002299 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302300
Chris Wilsonff685972017-02-15 08:43:42 +00002301 if (flags & I915_VMA_LOCAL_BIND) {
2302 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
2303
2304 if (appgtt->base.allocate_va_range) {
2305 ret = appgtt->base.allocate_va_range(&appgtt->base,
2306 vma->node.start,
2307 vma->node.size);
2308 if (ret)
2309 return ret;
2310 }
2311
2312 appgtt->base.insert_entries(&appgtt->base,
2313 vma->pages, vma->node.start,
2314 cache_level, pte_flags);
2315 }
2316
Chris Wilson3272db52016-08-04 16:32:32 +01002317 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002318 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002319 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002320 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002321 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002322 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002323 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002324
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002325 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002326}
2327
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002328static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002329{
Chris Wilson49d73912016-11-29 09:50:08 +00002330 struct drm_i915_private *i915 = vma->vm->i915;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002331
Chris Wilson9c870d02016-10-24 13:42:15 +01002332 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2333 intel_runtime_pm_get(i915);
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002334 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002335 intel_runtime_pm_put(i915);
2336 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002337
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002338 if (vma->flags & I915_VMA_LOCAL_BIND) {
2339 struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
2340
2341 vm->clear_range(vm, vma->node.start, vma->size);
2342 }
Daniel Vetter74163902012-02-15 23:50:21 +01002343}
2344
Chris Wilson03ac84f2016-10-28 13:58:36 +01002345void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2346 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002347{
David Weinehall52a05c32016-08-22 13:32:44 +03002348 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2349 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002350 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002351
Chris Wilson307dc252016-08-05 10:14:12 +01002352 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002353 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002354 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2355 /* Wait a bit, in hopes it avoids the hang */
2356 udelay(10);
2357 }
2358 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002359
Chris Wilson03ac84f2016-10-28 13:58:36 +01002360 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002361}
Daniel Vetter644ec022012-03-26 09:45:40 +02002362
Chris Wilson45b186f2016-12-16 07:46:42 +00002363static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002364 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002365 u64 *start,
2366 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002367{
Chris Wilsona6508de2017-02-06 08:45:47 +00002368 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002369 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002370
Chris Wilsona6508de2017-02-06 08:45:47 +00002371 /* Also leave a space between the unallocated reserved node after the
2372 * GTT and any objects within the GTT, i.e. we use the color adjustment
2373 * to insert a guard page to prevent prefetches crossing over the
2374 * GTT boundary.
2375 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002376 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002377 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002378 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002379}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002380
Chris Wilson6cde9a02017-02-13 17:15:50 +00002381int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2382{
2383 struct i915_ggtt *ggtt = &i915->ggtt;
2384 struct i915_hw_ppgtt *ppgtt;
2385 int err;
2386
Chris Wilson1188bc62017-02-15 08:43:38 +00002387 ppgtt = i915_ppgtt_create(i915, NULL, "[alias]");
2388 if (IS_ERR(ppgtt))
2389 return PTR_ERR(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002390
2391 if (ppgtt->base.allocate_va_range) {
2392 err = ppgtt->base.allocate_va_range(&ppgtt->base,
2393 0, ppgtt->base.total);
2394 if (err)
Chris Wilson1188bc62017-02-15 08:43:38 +00002395 goto err_ppgtt;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002396 }
2397
Chris Wilson6cde9a02017-02-13 17:15:50 +00002398 i915->mm.aliasing_ppgtt = ppgtt;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002399
Chris Wilson6cde9a02017-02-13 17:15:50 +00002400 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2401 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2402
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002403 WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
2404 ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
2405
Chris Wilson6cde9a02017-02-13 17:15:50 +00002406 return 0;
2407
Chris Wilson6cde9a02017-02-13 17:15:50 +00002408err_ppgtt:
Chris Wilson1188bc62017-02-15 08:43:38 +00002409 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002410 return err;
2411}
2412
2413void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2414{
2415 struct i915_ggtt *ggtt = &i915->ggtt;
2416 struct i915_hw_ppgtt *ppgtt;
2417
2418 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2419 if (!ppgtt)
2420 return;
2421
Chris Wilson1188bc62017-02-15 08:43:38 +00002422 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002423
2424 ggtt->base.bind_vma = ggtt_bind_vma;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002425 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002426}
2427
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002428int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002429{
Ben Widawskye78891c2013-01-25 16:41:04 -08002430 /* Let GEM Manage all of the aperture.
2431 *
2432 * However, leave one page at the end still bound to the scratch page.
2433 * There are a number of places where the hardware apparently prefetches
2434 * past the end of the object, and we've seen multiple hangs with the
2435 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2436 * aperture. One page should be enough to keep any prefetching inside
2437 * of the aperture.
2438 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002439 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002440 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002441 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002442 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002443
Zhi Wangb02d22a2016-06-16 08:06:59 -04002444 ret = intel_vgt_balloon(dev_priv);
2445 if (ret)
2446 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002447
Chris Wilson95374d72016-10-12 10:05:20 +01002448 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002449 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2450 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2451 0, ggtt->mappable_end,
2452 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002453 if (ret)
2454 return ret;
2455
Chris Wilsoned2f3452012-11-15 11:32:19 +00002456 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002457 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002458 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2459 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002460 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002461 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002462 }
2463
2464 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002465 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002466 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002467
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002468 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Chris Wilson6cde9a02017-02-13 17:15:50 +00002469 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
Chris Wilson95374d72016-10-12 10:05:20 +01002470 if (ret)
Chris Wilson6cde9a02017-02-13 17:15:50 +00002471 goto err;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002472 }
2473
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002474 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002475
Chris Wilson95374d72016-10-12 10:05:20 +01002476err:
2477 drm_mm_remove_node(&ggtt->error_capture);
2478 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002479}
2480
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002481/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002482 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002483 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002484 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002485void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002486{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002487 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002488 struct i915_vma *vma, *vn;
2489
2490 ggtt->base.closed = true;
2491
2492 mutex_lock(&dev_priv->drm.struct_mutex);
2493 WARN_ON(!list_empty(&ggtt->base.active_list));
2494 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2495 WARN_ON(i915_vma_unbind(vma));
2496 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002497
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002498 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002499
Chris Wilson1188bc62017-02-15 08:43:38 +00002500 mutex_lock(&dev_priv->drm.struct_mutex);
2501 i915_gem_fini_aliasing_ppgtt(dev_priv);
2502
Chris Wilson95374d72016-10-12 10:05:20 +01002503 if (drm_mm_node_allocated(&ggtt->error_capture))
2504 drm_mm_remove_node(&ggtt->error_capture);
2505
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002506 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002507 intel_vgt_deballoon(dev_priv);
Matthew Aulded9724d2016-11-17 21:04:10 +00002508 i915_address_space_fini(&ggtt->base);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002509 }
2510
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002511 ggtt->base.cleanup(&ggtt->base);
Chris Wilson1188bc62017-02-15 08:43:38 +00002512 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002513
2514 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002515 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002516}
Daniel Vetter70e32542014-08-06 15:04:57 +02002517
Daniel Vetter2c642b02015-04-14 17:35:26 +02002518static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002519{
2520 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2521 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2522 return snb_gmch_ctl << 20;
2523}
2524
Daniel Vetter2c642b02015-04-14 17:35:26 +02002525static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002526{
2527 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2528 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2529 if (bdw_gmch_ctl)
2530 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002531
2532#ifdef CONFIG_X86_32
2533 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2534 if (bdw_gmch_ctl > 4)
2535 bdw_gmch_ctl = 4;
2536#endif
2537
Ben Widawsky9459d252013-11-03 16:53:55 -08002538 return bdw_gmch_ctl << 20;
2539}
2540
Daniel Vetter2c642b02015-04-14 17:35:26 +02002541static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002542{
2543 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2544 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2545
2546 if (gmch_ctrl)
2547 return 1 << (20 + gmch_ctrl);
2548
2549 return 0;
2550}
2551
Daniel Vetter2c642b02015-04-14 17:35:26 +02002552static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002553{
2554 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2555 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2556 return snb_gmch_ctl << 25; /* 32 MB units */
2557}
2558
Daniel Vetter2c642b02015-04-14 17:35:26 +02002559static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002560{
2561 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2562 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2563 return bdw_gmch_ctl << 25; /* 32 MB units */
2564}
2565
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002566static size_t chv_get_stolen_size(u16 gmch_ctrl)
2567{
2568 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2569 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2570
2571 /*
2572 * 0x0 to 0x10: 32MB increments starting at 0MB
2573 * 0x11 to 0x16: 4MB increments starting at 8MB
2574 * 0x17 to 0x1d: 4MB increments start at 36MB
2575 */
2576 if (gmch_ctrl < 0x11)
2577 return gmch_ctrl << 25;
2578 else if (gmch_ctrl < 0x17)
2579 return (gmch_ctrl - 0x11 + 2) << 22;
2580 else
2581 return (gmch_ctrl - 0x17 + 9) << 22;
2582}
2583
Damien Lespiau66375012014-01-09 18:02:46 +00002584static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2585{
2586 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2587 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2588
2589 if (gen9_gmch_ctl < 0xf0)
2590 return gen9_gmch_ctl << 25; /* 32 MB units */
2591 else
2592 /* 4MB increments starting at 0xf0 for 4MB */
2593 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2594}
2595
Chris Wilson34c998b2016-08-04 07:52:24 +01002596static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002597{
Chris Wilson49d73912016-11-29 09:50:08 +00002598 struct drm_i915_private *dev_priv = ggtt->base.i915;
2599 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002600 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002601 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002602
2603 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002604 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002605
Imre Deak2a073f892015-03-27 13:07:33 +02002606 /*
2607 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2608 * dropped. For WC mappings in general we have 64 byte burst writes
2609 * when the WC buffer is flushed, so we can't use it, but have to
2610 * resort to an uncached mapping. The WC issue is easily caught by the
2611 * readback check when writing GTT PTE entries.
2612 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002613 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002614 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002615 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002616 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002617 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002618 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002619 return -ENOMEM;
2620 }
2621
Chris Wilson84486612017-02-15 08:43:40 +00002622 ret = setup_scratch_page(&ggtt->base, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002623 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002624 DRM_ERROR("Scratch setup failed\n");
2625 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002626 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002627 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002628 }
2629
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002630 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002631}
2632
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002633/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2634 * bits. When using advanced contexts each context stores its own PAT, but
2635 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002636static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002637{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002638 uint64_t pat;
2639
2640 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2641 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2642 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2643 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2644 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2645 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2646 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2647 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2648
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002649 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002650 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2651 * so RTL will always use the value corresponding to
2652 * pat_sel = 000".
2653 * So let's disable cache for GGTT to avoid screen corruptions.
2654 * MOCS still can be used though.
2655 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2656 * before this patch, i.e. the same uncached + snooping access
2657 * like on gen6/7 seems to be in effect.
2658 * - So this just fixes blitter/render access. Again it looks
2659 * like it's not just uncached access, but uncached + snooping.
2660 * So we can still hold onto all our assumptions wrt cpu
2661 * clflushing on LLC machines.
2662 */
2663 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2664
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002665 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2666 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002667 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2668 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002669}
2670
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002671static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2672{
2673 uint64_t pat;
2674
2675 /*
2676 * Map WB on BDW to snooped on CHV.
2677 *
2678 * Only the snoop bit has meaning for CHV, the rest is
2679 * ignored.
2680 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002681 * The hardware will never snoop for certain types of accesses:
2682 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2683 * - PPGTT page tables
2684 * - some other special cycles
2685 *
2686 * As with BDW, we also need to consider the following for GT accesses:
2687 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2688 * so RTL will always use the value corresponding to
2689 * pat_sel = 000".
2690 * Which means we must set the snoop bit in PAT entry 0
2691 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002692 */
2693 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2694 GEN8_PPAT(1, 0) |
2695 GEN8_PPAT(2, 0) |
2696 GEN8_PPAT(3, 0) |
2697 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2698 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2699 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2700 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2701
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002702 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2703 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002704}
2705
Chris Wilson34c998b2016-08-04 07:52:24 +01002706static void gen6_gmch_remove(struct i915_address_space *vm)
2707{
2708 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2709
2710 iounmap(ggtt->gsm);
Chris Wilson84486612017-02-15 08:43:40 +00002711 cleanup_scratch_page(vm);
Chris Wilson34c998b2016-08-04 07:52:24 +01002712}
2713
Joonas Lahtinend507d732016-03-18 10:42:58 +02002714static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08002715{
Chris Wilson49d73912016-11-29 09:50:08 +00002716 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002717 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002718 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08002719 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08002720
2721 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002722 ggtt->mappable_base = pci_resource_start(pdev, 2);
2723 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08002724
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002725 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
2726 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08002727
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002728 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08002729
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002730 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002731 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002732 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002733 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002734 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002735 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002736 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002737 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01002738 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002739 }
Ben Widawsky63340132013-11-04 19:32:22 -08002740
Chris Wilson34c998b2016-08-04 07:52:24 +01002741 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002742
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002743 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002744 chv_setup_private_ppat(dev_priv);
2745 else
2746 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002747
Chris Wilson34c998b2016-08-04 07:52:24 +01002748 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002749 ggtt->base.bind_vma = ggtt_bind_vma;
2750 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05302751 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002752 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01002753 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002754 ggtt->base.clear_range = gen8_ggtt_clear_range;
2755
2756 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
2757 if (IS_CHERRYVIEW(dev_priv))
2758 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
2759
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002760 ggtt->invalidate = gen6_ggtt_invalidate;
2761
Chris Wilson34c998b2016-08-04 07:52:24 +01002762 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08002763}
2764
Joonas Lahtinend507d732016-03-18 10:42:58 +02002765static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002766{
Chris Wilson49d73912016-11-29 09:50:08 +00002767 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002768 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002769 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002770 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002771
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002772 ggtt->mappable_base = pci_resource_start(pdev, 2);
2773 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08002774
Ben Widawskybaa09f52013-01-24 13:49:57 -08002775 /* 64/512MB is the current min/max we actually know of, but this is just
2776 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002777 */
Chris Wilson34c998b2016-08-04 07:52:24 +01002778 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02002779 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002780 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002781 }
2782
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002783 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
2784 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
2785 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002786
Joonas Lahtinend507d732016-03-18 10:42:58 +02002787 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002788
Chris Wilson34c998b2016-08-04 07:52:24 +01002789 size = gen6_get_total_gtt_size(snb_gmch_ctl);
2790 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002791
Joonas Lahtinend507d732016-03-18 10:42:58 +02002792 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05302793 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002794 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
2795 ggtt->base.bind_vma = ggtt_bind_vma;
2796 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01002797 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002798
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002799 ggtt->invalidate = gen6_ggtt_invalidate;
2800
Chris Wilson34c998b2016-08-04 07:52:24 +01002801 if (HAS_EDRAM(dev_priv))
2802 ggtt->base.pte_encode = iris_pte_encode;
2803 else if (IS_HASWELL(dev_priv))
2804 ggtt->base.pte_encode = hsw_pte_encode;
2805 else if (IS_VALLEYVIEW(dev_priv))
2806 ggtt->base.pte_encode = byt_pte_encode;
2807 else if (INTEL_GEN(dev_priv) >= 7)
2808 ggtt->base.pte_encode = ivb_pte_encode;
2809 else
2810 ggtt->base.pte_encode = snb_pte_encode;
2811
2812 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002813}
2814
Chris Wilson34c998b2016-08-04 07:52:24 +01002815static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002816{
Chris Wilson34c998b2016-08-04 07:52:24 +01002817 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08002818}
2819
Joonas Lahtinend507d732016-03-18 10:42:58 +02002820static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002821{
Chris Wilson49d73912016-11-29 09:50:08 +00002822 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002823 int ret;
2824
Chris Wilson91c8a322016-07-05 10:40:23 +01002825 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002826 if (!ret) {
2827 DRM_ERROR("failed to set up gmch\n");
2828 return -EIO;
2829 }
2830
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00002831 intel_gtt_get(&ggtt->base.total,
2832 &ggtt->stolen_size,
2833 &ggtt->mappable_base,
2834 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002835
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002836 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05302837 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02002838 ggtt->base.insert_entries = i915_ggtt_insert_entries;
2839 ggtt->base.clear_range = i915_ggtt_clear_range;
2840 ggtt->base.bind_vma = ggtt_bind_vma;
2841 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01002842 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002843
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002844 ggtt->invalidate = gmch_ggtt_invalidate;
2845
Joonas Lahtinend507d732016-03-18 10:42:58 +02002846 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002847 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2848
Ben Widawskybaa09f52013-01-24 13:49:57 -08002849 return 0;
2850}
2851
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002852/**
Chris Wilson0088e522016-08-04 07:52:21 +01002853 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002854 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002855 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002856int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002857{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002858 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002859 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002860
Chris Wilson49d73912016-11-29 09:50:08 +00002861 ggtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00002862 ggtt->base.dma = &dev_priv->drm.pdev->dev;
Mika Kuoppalac114f762015-06-25 18:35:13 +03002863
Chris Wilson34c998b2016-08-04 07:52:24 +01002864 if (INTEL_GEN(dev_priv) <= 5)
2865 ret = i915_gmch_probe(ggtt);
2866 else if (INTEL_GEN(dev_priv) < 8)
2867 ret = gen6_gmch_probe(ggtt);
2868 else
2869 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002870 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002871 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002872
Chris Wilsondb9309a2017-01-05 15:30:23 +00002873 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
2874 * This is easier than doing range restriction on the fly, as we
2875 * currently don't have any bits spare to pass in this upper
2876 * restriction!
2877 */
2878 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
2879 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
2880 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
2881 }
2882
Chris Wilsonc890e2d2016-03-18 10:42:59 +02002883 if ((ggtt->base.total - 1) >> 32) {
2884 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002885 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02002886 ggtt->base.total >> 20);
2887 ggtt->base.total = 1ULL << 32;
2888 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
2889 }
2890
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002891 if (ggtt->mappable_end > ggtt->base.total) {
2892 DRM_ERROR("mappable aperture extends past end of GGTT,"
2893 " aperture=%llx, total=%llx\n",
2894 ggtt->mappable_end, ggtt->base.total);
2895 ggtt->mappable_end = ggtt->base.total;
2896 }
2897
Ben Widawskybaa09f52013-01-24 13:49:57 -08002898 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002899 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02002900 ggtt->base.total >> 20);
2901 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00002902 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002903#ifdef CONFIG_INTEL_IOMMU
2904 if (intel_iommu_gfx_mapped)
2905 DRM_INFO("VT-d active for gfx access\n");
2906#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002907
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002908 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01002909}
2910
2911/**
2912 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002913 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01002914 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002915int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01002916{
Chris Wilson0088e522016-08-04 07:52:21 +01002917 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2918 int ret;
2919
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002920 INIT_LIST_HEAD(&dev_priv->vm_list);
2921
Chris Wilsona6508de2017-02-06 08:45:47 +00002922 /* Note that we use page colouring to enforce a guard page at the
2923 * end of the address space. This is required as the CS may prefetch
2924 * beyond the end of the batch buffer, across the page boundary,
2925 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002926 */
Chris Wilson80b204b2016-10-28 13:58:58 +01002927 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01002928 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00002929 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002930 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01002931 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002932
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002933 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
2934 dev_priv->ggtt.mappable_base,
2935 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002936 ret = -EIO;
2937 goto out_gtt_cleanup;
2938 }
2939
2940 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
2941
Chris Wilson0088e522016-08-04 07:52:21 +01002942 /*
2943 * Initialise stolen early so that we may reserve preallocated
2944 * objects for the BIOS to KMS transition.
2945 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00002946 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01002947 if (ret)
2948 goto out_gtt_cleanup;
2949
2950 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02002951
2952out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002953 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02002954 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002955}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002956
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002957int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03002958{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002959 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03002960 return -EIO;
2961
2962 return 0;
2963}
2964
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002965void i915_ggtt_enable_guc(struct drm_i915_private *i915)
2966{
2967 i915->ggtt.invalidate = guc_ggtt_invalidate;
2968}
2969
2970void i915_ggtt_disable_guc(struct drm_i915_private *i915)
2971{
2972 i915->ggtt.invalidate = gen6_ggtt_invalidate;
2973}
2974
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002975void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02002976{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002977 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002978 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02002979
Chris Wilsondc979972016-05-10 14:10:04 +01002980 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02002981
2982 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002983 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02002984
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002985 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
2986
2987 /* clflush objects bound into the GGTT and rebind them. */
2988 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02002989 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002990 bool ggtt_bound = false;
2991 struct i915_vma *vma;
2992
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002993 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002994 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01002995 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02002996
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01002997 if (!i915_vma_unbind(vma))
2998 continue;
2999
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003000 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3001 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003002 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003003 }
3004
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003005 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003006 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003007 }
3008
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003009 ggtt->base.closed = false;
3010
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003011 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003012 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003013 chv_setup_private_ppat(dev_priv);
3014 else
3015 bdw_setup_private_ppat(dev_priv);
3016
3017 return;
3018 }
3019
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003020 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003021 struct i915_address_space *vm;
3022
Daniel Vetterfa423312015-04-14 17:35:23 +02003023 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003024 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003025
Chris Wilson2bfa9962016-08-04 07:52:25 +01003026 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003027 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003028 else
3029 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003030
Chris Wilson16a011c2017-02-15 08:43:45 +00003031 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003032 }
3033 }
3034
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003035 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003036}
3037
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003038static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003039rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003040 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003041 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003042 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003043{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003044 unsigned int column, row;
3045 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003046
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003047 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003048 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003049 for (row = 0; row < height; row++) {
3050 st->nents++;
3051 /* We don't need the pages, but need to initialize
3052 * the entries so the sg list can be happily traversed.
3053 * The only thing we need are DMA addresses.
3054 */
3055 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003056 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003057 sg_dma_len(sg) = PAGE_SIZE;
3058 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003059 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003060 }
3061 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003062
3063 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003064}
3065
Chris Wilsonba7a5742017-02-15 08:43:35 +00003066static noinline struct sg_table *
3067intel_rotate_pages(struct intel_rotation_info *rot_info,
3068 struct drm_i915_gem_object *obj)
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003069{
Dave Gordon85d12252016-05-20 11:54:06 +01003070 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003071 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003072 struct sgt_iter sgt_iter;
3073 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003074 unsigned long i;
3075 dma_addr_t *page_addr_list;
3076 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003077 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003078 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003079
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003080 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003081 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003082 sizeof(dma_addr_t),
3083 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003084 if (!page_addr_list)
3085 return ERR_PTR(ret);
3086
3087 /* Allocate target SG list. */
3088 st = kmalloc(sizeof(*st), GFP_KERNEL);
3089 if (!st)
3090 goto err_st_alloc;
3091
Ville Syrjälä6687c902015-09-15 13:16:41 +03003092 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003093 if (ret)
3094 goto err_sg_alloc;
3095
3096 /* Populate source page list from the object. */
3097 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003098 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003099 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003100
Dave Gordon85d12252016-05-20 11:54:06 +01003101 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003102 st->nents = 0;
3103 sg = st->sgl;
3104
Ville Syrjälä6687c902015-09-15 13:16:41 +03003105 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3106 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3107 rot_info->plane[i].width, rot_info->plane[i].height,
3108 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003109 }
3110
Ville Syrjälä6687c902015-09-15 13:16:41 +03003111 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3112 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003113
3114 drm_free_large(page_addr_list);
3115
3116 return st;
3117
3118err_sg_alloc:
3119 kfree(st);
3120err_st_alloc:
3121 drm_free_large(page_addr_list);
3122
Ville Syrjälä6687c902015-09-15 13:16:41 +03003123 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3124 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3125
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003126 return ERR_PTR(ret);
3127}
3128
Chris Wilsonba7a5742017-02-15 08:43:35 +00003129static noinline struct sg_table *
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003130intel_partial_pages(const struct i915_ggtt_view *view,
3131 struct drm_i915_gem_object *obj)
3132{
3133 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003134 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003135 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003136 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003137 int ret = -ENOMEM;
3138
3139 st = kmalloc(sizeof(*st), GFP_KERNEL);
3140 if (!st)
3141 goto err_st_alloc;
3142
Chris Wilsond2a84a72016-10-28 13:58:34 +01003143 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003144 if (ret)
3145 goto err_sg_alloc;
3146
Chris Wilson8bab11932017-01-14 00:28:25 +00003147 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003148 GEM_BUG_ON(!iter);
3149
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003150 sg = st->sgl;
3151 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003152 do {
3153 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003154
Chris Wilsond2a84a72016-10-28 13:58:34 +01003155 len = min(iter->length - (offset << PAGE_SHIFT),
3156 count << PAGE_SHIFT);
3157 sg_set_page(sg, NULL, len, 0);
3158 sg_dma_address(sg) =
3159 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3160 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003161
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003162 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003163 count -= len >> PAGE_SHIFT;
3164 if (count == 0) {
3165 sg_mark_end(sg);
3166 return st;
3167 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003168
Chris Wilsond2a84a72016-10-28 13:58:34 +01003169 sg = __sg_next(sg);
3170 iter = __sg_next(iter);
3171 offset = 0;
3172 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003173
3174err_sg_alloc:
3175 kfree(st);
3176err_st_alloc:
3177 return ERR_PTR(ret);
3178}
3179
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003180static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003181i915_get_ggtt_vma_pages(struct i915_vma *vma)
3182{
Chris Wilsonba7a5742017-02-15 08:43:35 +00003183 int ret;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003184
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003185 /* The vma->pages are only valid within the lifespan of the borrowed
3186 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3187 * must be the vma->pages. A simple rule is that vma->pages must only
3188 * be accessed when the obj->mm.pages are pinned.
3189 */
3190 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3191
Chris Wilsonba7a5742017-02-15 08:43:35 +00003192 switch (vma->ggtt_view.type) {
3193 case I915_GGTT_VIEW_NORMAL:
3194 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003195 return 0;
3196
Chris Wilsonba7a5742017-02-15 08:43:35 +00003197 case I915_GGTT_VIEW_ROTATED:
Chris Wilson247177d2016-08-15 10:48:47 +01003198 vma->pages =
Chris Wilsonba7a5742017-02-15 08:43:35 +00003199 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3200 break;
3201
3202 case I915_GGTT_VIEW_PARTIAL:
Chris Wilson247177d2016-08-15 10:48:47 +01003203 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003204 break;
3205
3206 default:
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003207 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3208 vma->ggtt_view.type);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003209 return -EINVAL;
3210 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003211
Chris Wilsonba7a5742017-02-15 08:43:35 +00003212 ret = 0;
3213 if (unlikely(IS_ERR(vma->pages))) {
Chris Wilson247177d2016-08-15 10:48:47 +01003214 ret = PTR_ERR(vma->pages);
3215 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003216 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3217 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003218 }
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003219 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003220}
3221
Chris Wilsone007b192017-01-11 11:23:10 +00003222/**
Chris Wilson625d9882017-01-11 11:23:11 +00003223 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003224 * @vm: the &struct i915_address_space
3225 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3226 * @size: how much space to allocate inside the GTT,
3227 * must be #I915_GTT_PAGE_SIZE aligned
3228 * @offset: where to insert inside the GTT,
3229 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3230 * (@offset + @size) must fit within the address space
3231 * @color: color to apply to node, if this node is not from a VMA,
3232 * color must be #I915_COLOR_UNEVICTABLE
3233 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003234 *
3235 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3236 * the address space (using @size and @color). If the @node does not fit, it
3237 * tries to evict any overlapping nodes from the GTT, including any
3238 * neighbouring nodes if the colors do not match (to ensure guard pages between
3239 * differing domains). See i915_gem_evict_for_node() for the gory details
3240 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3241 * evicting active overlapping objects, and any overlapping node that is pinned
3242 * or marked as unevictable will also result in failure.
3243 *
3244 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3245 * asked to wait for eviction and interrupted.
3246 */
3247int i915_gem_gtt_reserve(struct i915_address_space *vm,
3248 struct drm_mm_node *node,
3249 u64 size, u64 offset, unsigned long color,
3250 unsigned int flags)
3251{
3252 int err;
3253
3254 GEM_BUG_ON(!size);
3255 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3256 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3257 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003258 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003259 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003260
3261 node->size = size;
3262 node->start = offset;
3263 node->color = color;
3264
3265 err = drm_mm_reserve_node(&vm->mm, node);
3266 if (err != -ENOSPC)
3267 return err;
3268
3269 err = i915_gem_evict_for_node(vm, node, flags);
3270 if (err == 0)
3271 err = drm_mm_reserve_node(&vm->mm, node);
3272
3273 return err;
3274}
3275
Chris Wilson606fec92017-01-11 11:23:12 +00003276static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3277{
3278 u64 range, addr;
3279
3280 GEM_BUG_ON(range_overflows(start, len, end));
3281 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3282
3283 range = round_down(end - len, align) - round_up(start, align);
3284 if (range) {
3285 if (sizeof(unsigned long) == sizeof(u64)) {
3286 addr = get_random_long();
3287 } else {
3288 addr = get_random_int();
3289 if (range > U32_MAX) {
3290 addr <<= 32;
3291 addr |= get_random_int();
3292 }
3293 }
3294 div64_u64_rem(addr, range, &addr);
3295 start += addr;
3296 }
3297
3298 return round_up(start, align);
3299}
3300
Chris Wilson625d9882017-01-11 11:23:11 +00003301/**
Chris Wilsone007b192017-01-11 11:23:10 +00003302 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003303 * @vm: the &struct i915_address_space
3304 * @node: the &struct drm_mm_node (typically i915_vma.node)
3305 * @size: how much space to allocate inside the GTT,
3306 * must be #I915_GTT_PAGE_SIZE aligned
3307 * @alignment: required alignment of starting offset, may be 0 but
3308 * if specified, this must be a power-of-two and at least
3309 * #I915_GTT_MIN_ALIGNMENT
3310 * @color: color to apply to node
3311 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003312 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003313 * @end: end of any range restriction inside GTT (U64_MAX for all),
3314 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3315 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003316 *
3317 * i915_gem_gtt_insert() first searches for an available hole into which
3318 * is can insert the node. The hole address is aligned to @alignment and
3319 * its @size must then fit entirely within the [@start, @end] bounds. The
3320 * nodes on either side of the hole must match @color, or else a guard page
3321 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003322 * suitable hole is found, first a victim is randomly selected and tested
3323 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003324 * is scanned to find the first set of replacement nodes to create the hole.
3325 * Those old overlapping nodes are evicted from the GTT (and so must be
3326 * rebound before any future use). Any node that is currently pinned cannot
3327 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3328 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3329 * searching for an eviction candidate. See i915_gem_evict_something() for
3330 * the gory details on the eviction algorithm.
3331 *
3332 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3333 * asked to wait for eviction and interrupted.
3334 */
3335int i915_gem_gtt_insert(struct i915_address_space *vm,
3336 struct drm_mm_node *node,
3337 u64 size, u64 alignment, unsigned long color,
3338 u64 start, u64 end, unsigned int flags)
3339{
Chris Wilson4e64e552017-02-02 21:04:38 +00003340 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003341 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003342 int err;
3343
3344 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3345 GEM_BUG_ON(!size);
3346 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3347 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3348 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3349 GEM_BUG_ON(start >= end);
3350 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3351 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003352 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003353 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003354
3355 if (unlikely(range_overflows(start, size, end)))
3356 return -ENOSPC;
3357
3358 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3359 return -ENOSPC;
3360
Chris Wilson4e64e552017-02-02 21:04:38 +00003361 mode = DRM_MM_INSERT_BEST;
3362 if (flags & PIN_HIGH)
3363 mode = DRM_MM_INSERT_HIGH;
3364 if (flags & PIN_MAPPABLE)
3365 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003366
3367 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3368 * so we know that we always have a minimum alignment of 4096.
3369 * The drm_mm range manager is optimised to return results
3370 * with zero alignment, so where possible use the optimal
3371 * path.
3372 */
3373 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3374 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3375 alignment = 0;
3376
Chris Wilson4e64e552017-02-02 21:04:38 +00003377 err = drm_mm_insert_node_in_range(&vm->mm, node,
3378 size, alignment, color,
3379 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003380 if (err != -ENOSPC)
3381 return err;
3382
Chris Wilson606fec92017-01-11 11:23:12 +00003383 /* No free space, pick a slot at random.
3384 *
3385 * There is a pathological case here using a GTT shared between
3386 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3387 *
3388 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3389 * (64k objects) (448k objects)
3390 *
3391 * Now imagine that the eviction LRU is ordered top-down (just because
3392 * pathology meets real life), and that we need to evict an object to
3393 * make room inside the aperture. The eviction scan then has to walk
3394 * the 448k list before it finds one within range. And now imagine that
3395 * it has to search for a new hole between every byte inside the memcpy,
3396 * for several simultaneous clients.
3397 *
3398 * On a full-ppgtt system, if we have run out of available space, there
3399 * will be lots and lots of objects in the eviction list! Again,
3400 * searching that LRU list may be slow if we are also applying any
3401 * range restrictions (e.g. restriction to low 4GiB) and so, for
3402 * simplicity and similarilty between different GTT, try the single
3403 * random replacement first.
3404 */
3405 offset = random_offset(start, end,
3406 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3407 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3408 if (err != -ENOSPC)
3409 return err;
3410
3411 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003412 err = i915_gem_evict_something(vm, size, alignment, color,
3413 start, end, flags);
3414 if (err)
3415 return err;
3416
Chris Wilson4e64e552017-02-02 21:04:38 +00003417 return drm_mm_insert_node_in_range(&vm->mm, node,
3418 size, alignment, color,
3419 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003420}
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003421
3422#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3423#include "selftests/mock_gtt.c"
Chris Wilson1c428192017-02-13 17:15:38 +00003424#include "selftests/i915_gem_gtt.c"
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003425#endif