blob: 17c5c1a5b6524016e1c6ffc5e31f73906c2c8092 [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
196 trace_i915_va_alloc(vma);
197 ret = vma->vm->allocate_va_range(vma->vm, vma->node.start, vma->size);
198 if (ret)
199 return ret;
Daniel Vetter47552652015-04-14 17:35:24 +0200200
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100201 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100202
Daniel Vetter47552652015-04-14 17:35:24 +0200203 /* Currently applicable only to VLV */
Chris Wilsonff685972017-02-15 08:43:42 +0000204 pte_flags = 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200205 if (vma->obj->gt_ro)
206 pte_flags |= PTE_READ_ONLY;
207
Chris Wilson247177d2016-08-15 10:48:47 +0100208 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200209 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200210
211 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200212}
213
214static void ppgtt_unbind_vma(struct i915_vma *vma)
215{
Chris Wilsonff685972017-02-15 08:43:42 +0000216 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200217}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800218
Daniel Vetter2c642b02015-04-14 17:35:26 +0200219static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200220 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700221{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200222 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700223 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300224
225 switch (level) {
226 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800227 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300228 break;
229 case I915_CACHE_WT:
230 pte |= PPAT_DISPLAY_ELLC_INDEX;
231 break;
232 default:
233 pte |= PPAT_CACHED_INDEX;
234 break;
235 }
236
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700237 return pte;
238}
239
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300240static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
241 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800242{
Michel Thierry07749ef2015-03-16 16:00:54 +0000243 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800244 pde |= addr;
245 if (level != I915_CACHE_NONE)
246 pde |= PPAT_CACHED_PDE_INDEX;
247 else
248 pde |= PPAT_UNCACHED_INDEX;
249 return pde;
250}
251
Michel Thierry762d9932015-07-30 11:05:29 +0100252#define gen8_pdpe_encode gen8_pde_encode
253#define gen8_pml4e_encode gen8_pde_encode
254
Michel Thierry07749ef2015-03-16 16:00:54 +0000255static gen6_pte_t snb_pte_encode(dma_addr_t addr,
256 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200257 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700258{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200259 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700260 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700261
262 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100263 case I915_CACHE_L3_LLC:
264 case I915_CACHE_LLC:
265 pte |= GEN6_PTE_CACHE_LLC;
266 break;
267 case I915_CACHE_NONE:
268 pte |= GEN6_PTE_UNCACHED;
269 break;
270 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100271 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100272 }
273
274 return pte;
275}
276
Michel Thierry07749ef2015-03-16 16:00:54 +0000277static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
278 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200279 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100280{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200281 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100282 pte |= GEN6_PTE_ADDR_ENCODE(addr);
283
284 switch (level) {
285 case I915_CACHE_L3_LLC:
286 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700287 break;
288 case I915_CACHE_LLC:
289 pte |= GEN6_PTE_CACHE_LLC;
290 break;
291 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700292 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700293 break;
294 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100295 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700296 }
297
Ben Widawsky54d12522012-09-24 16:44:32 -0700298 return pte;
299}
300
Michel Thierry07749ef2015-03-16 16:00:54 +0000301static gen6_pte_t byt_pte_encode(dma_addr_t addr,
302 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200303 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700304{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200305 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700306 pte |= GEN6_PTE_ADDR_ENCODE(addr);
307
Akash Goel24f3a8c2014-06-17 10:59:42 +0530308 if (!(flags & PTE_READ_ONLY))
309 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700310
311 if (level != I915_CACHE_NONE)
312 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
313
314 return pte;
315}
316
Michel Thierry07749ef2015-03-16 16:00:54 +0000317static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
318 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200319 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700320{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200321 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700322 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700323
324 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700325 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700326
327 return pte;
328}
329
Michel Thierry07749ef2015-03-16 16:00:54 +0000330static gen6_pte_t iris_pte_encode(dma_addr_t addr,
331 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200332 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700333{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200334 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700335 pte |= HSW_PTE_ADDR_ENCODE(addr);
336
Chris Wilson651d7942013-08-08 14:41:10 +0100337 switch (level) {
338 case I915_CACHE_NONE:
339 break;
340 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000341 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100342 break;
343 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000344 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100345 break;
346 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700347
348 return pte;
349}
350
Chris Wilson84486612017-02-15 08:43:40 +0000351static struct page *vm_alloc_page(struct i915_address_space *vm, gfp_t gfp)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000352{
Chris Wilson84486612017-02-15 08:43:40 +0000353 struct page *page;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000354
Chris Wilson84486612017-02-15 08:43:40 +0000355 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
356 i915_gem_shrink_all(vm->i915);
Chris Wilsonaae4a3d2017-02-13 17:15:44 +0000357
Chris Wilson84486612017-02-15 08:43:40 +0000358 if (vm->free_pages.nr)
359 return vm->free_pages.pages[--vm->free_pages.nr];
360
361 page = alloc_page(gfp);
362 if (!page)
363 return NULL;
364
365 if (vm->pt_kmap_wc)
366 set_pages_array_wc(&page, 1);
367
368 return page;
369}
370
371static void vm_free_pages_release(struct i915_address_space *vm)
372{
373 GEM_BUG_ON(!pagevec_count(&vm->free_pages));
374
375 if (vm->pt_kmap_wc)
376 set_pages_array_wb(vm->free_pages.pages,
377 pagevec_count(&vm->free_pages));
378
379 __pagevec_release(&vm->free_pages);
380}
381
382static void vm_free_page(struct i915_address_space *vm, struct page *page)
383{
384 if (!pagevec_add(&vm->free_pages, page))
385 vm_free_pages_release(vm);
386}
387
388static int __setup_page_dma(struct i915_address_space *vm,
389 struct i915_page_dma *p,
390 gfp_t gfp)
391{
392 p->page = vm_alloc_page(vm, gfp | __GFP_NOWARN | __GFP_NORETRY);
393 if (unlikely(!p->page))
Michel Thierry1266cdb2015-03-24 17:06:33 +0000394 return -ENOMEM;
395
Chris Wilson84486612017-02-15 08:43:40 +0000396 p->daddr = dma_map_page(vm->dma, p->page, 0, PAGE_SIZE,
397 PCI_DMA_BIDIRECTIONAL);
398 if (unlikely(dma_mapping_error(vm->dma, p->daddr))) {
399 vm_free_page(vm, p->page);
400 return -ENOMEM;
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300401 }
402
Michel Thierry1266cdb2015-03-24 17:06:33 +0000403 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000404}
405
Chris Wilson84486612017-02-15 08:43:40 +0000406static int setup_page_dma(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000407 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300408{
Chris Wilson84486612017-02-15 08:43:40 +0000409 return __setup_page_dma(vm, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300410}
411
Chris Wilson84486612017-02-15 08:43:40 +0000412static void cleanup_page_dma(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000413 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300414{
Chris Wilson84486612017-02-15 08:43:40 +0000415 dma_unmap_page(vm->dma, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
416 vm_free_page(vm, p->page);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300417}
418
Chris Wilson9231da72017-02-15 08:43:41 +0000419#define kmap_atomic_px(px) kmap_atomic(px_base(px)->page)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300420
Chris Wilson84486612017-02-15 08:43:40 +0000421#define setup_px(vm, px) setup_page_dma((vm), px_base(px))
422#define cleanup_px(vm, px) cleanup_page_dma((vm), px_base(px))
423#define fill_px(ppgtt, px, v) fill_page_dma((vm), px_base(px), (v))
424#define fill32_px(ppgtt, px, v) fill_page_dma_32((vm), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300425
Chris Wilson84486612017-02-15 08:43:40 +0000426static void fill_page_dma(struct i915_address_space *vm,
427 struct i915_page_dma *p,
428 const u64 val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300429{
Chris Wilson9231da72017-02-15 08:43:41 +0000430 u64 * const vaddr = kmap_atomic(p->page);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300431 int i;
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300432
433 for (i = 0; i < 512; i++)
434 vaddr[i] = val;
435
Chris Wilson9231da72017-02-15 08:43:41 +0000436 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300437}
438
Chris Wilson84486612017-02-15 08:43:40 +0000439static void fill_page_dma_32(struct i915_address_space *vm,
440 struct i915_page_dma *p,
441 const u32 v)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300442{
Chris Wilson84486612017-02-15 08:43:40 +0000443 fill_page_dma(vm, p, (u64)v << 32 | v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300444}
445
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100446static int
Chris Wilson84486612017-02-15 08:43:40 +0000447setup_scratch_page(struct i915_address_space *vm, gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300448{
Chris Wilson84486612017-02-15 08:43:40 +0000449 return __setup_page_dma(vm, &vm->scratch_page, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300450}
451
Chris Wilson84486612017-02-15 08:43:40 +0000452static void cleanup_scratch_page(struct i915_address_space *vm)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300453{
Chris Wilson84486612017-02-15 08:43:40 +0000454 cleanup_page_dma(vm, &vm->scratch_page);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300455}
456
Chris Wilson84486612017-02-15 08:43:40 +0000457static struct i915_page_table *alloc_pt(struct i915_address_space *vm)
Ben Widawsky06fda602015-02-24 16:22:36 +0000458{
Michel Thierryec565b32015-04-08 12:13:23 +0100459 struct i915_page_table *pt;
Chris Wilson84486612017-02-15 08:43:40 +0000460 const size_t count = INTEL_GEN(vm->i915) >= 8 ? GEN8_PTES : GEN6_PTES;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000461 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000462
463 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
464 if (!pt)
465 return ERR_PTR(-ENOMEM);
466
Ben Widawsky678d96f2015-03-16 16:00:56 +0000467 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
468 GFP_KERNEL);
469
470 if (!pt->used_ptes)
471 goto fail_bitmap;
472
Chris Wilson84486612017-02-15 08:43:40 +0000473 ret = setup_px(vm, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000474 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300475 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000476
477 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000478
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300479fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000480 kfree(pt->used_ptes);
481fail_bitmap:
482 kfree(pt);
483
484 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000485}
486
Chris Wilson84486612017-02-15 08:43:40 +0000487static void free_pt(struct i915_address_space *vm, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000488{
Chris Wilson84486612017-02-15 08:43:40 +0000489 cleanup_px(vm, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300490 kfree(pt->used_ptes);
491 kfree(pt);
492}
493
494static void gen8_initialize_pt(struct i915_address_space *vm,
495 struct i915_page_table *pt)
496{
497 gen8_pte_t scratch_pte;
498
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100499 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200500 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300501
Chris Wilson84486612017-02-15 08:43:40 +0000502 fill_px(vm, pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300503}
504
505static void gen6_initialize_pt(struct i915_address_space *vm,
506 struct i915_page_table *pt)
507{
508 gen6_pte_t scratch_pte;
509
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100510 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300511
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100512 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200513 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300514
Chris Wilson84486612017-02-15 08:43:40 +0000515 fill32_px(vm, pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000516}
517
Chris Wilson84486612017-02-15 08:43:40 +0000518static struct i915_page_directory *alloc_pd(struct i915_address_space *vm)
Ben Widawsky06fda602015-02-24 16:22:36 +0000519{
Michel Thierryec565b32015-04-08 12:13:23 +0100520 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100521 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000522
523 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
524 if (!pd)
525 return ERR_PTR(-ENOMEM);
526
Michel Thierry33c88192015-04-08 12:13:33 +0100527 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
528 sizeof(*pd->used_pdes), GFP_KERNEL);
529 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300530 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100531
Chris Wilson84486612017-02-15 08:43:40 +0000532 ret = setup_px(vm, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100533 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300534 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100535
Ben Widawsky06fda602015-02-24 16:22:36 +0000536 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100537
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300538fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100539 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300540fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100541 kfree(pd);
542
543 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000544}
545
Chris Wilson84486612017-02-15 08:43:40 +0000546static void free_pd(struct i915_address_space *vm,
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000547 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300548{
549 if (px_page(pd)) {
Chris Wilson84486612017-02-15 08:43:40 +0000550 cleanup_px(vm, pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300551 kfree(pd->used_pdes);
552 kfree(pd);
553 }
554}
555
556static void gen8_initialize_pd(struct i915_address_space *vm,
557 struct i915_page_directory *pd)
558{
559 gen8_pde_t scratch_pde;
560
561 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
562
Chris Wilson84486612017-02-15 08:43:40 +0000563 fill_px(vm, pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300564}
565
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000566static int __pdp_init(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100567 struct i915_page_directory_pointer *pdp)
568{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000569 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Michel Thierry6ac18502015-07-29 17:23:46 +0100570
571 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
572 sizeof(unsigned long),
573 GFP_KERNEL);
574 if (!pdp->used_pdpes)
575 return -ENOMEM;
576
577 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
578 GFP_KERNEL);
579 if (!pdp->page_directory) {
580 kfree(pdp->used_pdpes);
581 /* the PDP might be the statically allocated top level. Keep it
582 * as clean as possible */
583 pdp->used_pdpes = NULL;
584 return -ENOMEM;
585 }
586
587 return 0;
588}
589
590static void __pdp_fini(struct i915_page_directory_pointer *pdp)
591{
592 kfree(pdp->used_pdpes);
593 kfree(pdp->page_directory);
594 pdp->page_directory = NULL;
595}
596
Chris Wilson84486612017-02-15 08:43:40 +0000597static struct i915_page_directory_pointer *
598alloc_pdp(struct i915_address_space *vm)
Michel Thierry762d9932015-07-30 11:05:29 +0100599{
600 struct i915_page_directory_pointer *pdp;
601 int ret = -ENOMEM;
602
Chris Wilson84486612017-02-15 08:43:40 +0000603 WARN_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Michel Thierry762d9932015-07-30 11:05:29 +0100604
605 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
606 if (!pdp)
607 return ERR_PTR(-ENOMEM);
608
Chris Wilson84486612017-02-15 08:43:40 +0000609 ret = __pdp_init(vm->i915, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100610 if (ret)
611 goto fail_bitmap;
612
Chris Wilson84486612017-02-15 08:43:40 +0000613 ret = setup_px(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100614 if (ret)
615 goto fail_page_m;
616
617 return pdp;
618
619fail_page_m:
620 __pdp_fini(pdp);
621fail_bitmap:
622 kfree(pdp);
623
624 return ERR_PTR(ret);
625}
626
Chris Wilson84486612017-02-15 08:43:40 +0000627static void free_pdp(struct i915_address_space *vm,
Michel Thierry6ac18502015-07-29 17:23:46 +0100628 struct i915_page_directory_pointer *pdp)
629{
630 __pdp_fini(pdp);
Chris Wilson84486612017-02-15 08:43:40 +0000631 if (USES_FULL_48BIT_PPGTT(vm->i915)) {
632 cleanup_px(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100633 kfree(pdp);
634 }
635}
636
Michel Thierry69ab76f2015-07-29 17:23:55 +0100637static void gen8_initialize_pdp(struct i915_address_space *vm,
638 struct i915_page_directory_pointer *pdp)
639{
640 gen8_ppgtt_pdpe_t scratch_pdpe;
641
642 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
643
Chris Wilson84486612017-02-15 08:43:40 +0000644 fill_px(vm, pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100645}
646
647static void gen8_initialize_pml4(struct i915_address_space *vm,
648 struct i915_pml4 *pml4)
649{
650 gen8_ppgtt_pml4e_t scratch_pml4e;
651
652 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
653 I915_CACHE_LLC);
654
Chris Wilson84486612017-02-15 08:43:40 +0000655 fill_px(vm, pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100656}
657
Michel Thierry762d9932015-07-30 11:05:29 +0100658static void
Matthew Auld5c693b22016-12-13 16:05:10 +0000659gen8_setup_pdpe(struct i915_hw_ppgtt *ppgtt,
660 struct i915_page_directory_pointer *pdp,
661 struct i915_page_directory *pd,
662 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100663{
664 gen8_ppgtt_pdpe_t *page_directorypo;
665
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000666 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
Michel Thierry762d9932015-07-30 11:05:29 +0100667 return;
668
Chris Wilson9231da72017-02-15 08:43:41 +0000669 page_directorypo = kmap_atomic_px(pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100670 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
Chris Wilson9231da72017-02-15 08:43:41 +0000671 kunmap_atomic(page_directorypo);
Michel Thierry762d9932015-07-30 11:05:29 +0100672}
673
674static void
Chris Wilson84486612017-02-15 08:43:40 +0000675gen8_setup_pml4e(struct i915_pml4 *pml4,
Matthew Auld56843102016-12-13 16:05:11 +0000676 struct i915_page_directory_pointer *pdp,
677 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100678{
Chris Wilson9231da72017-02-15 08:43:41 +0000679 gen8_ppgtt_pml4e_t *pagemap = kmap_atomic_px(pml4);
Michel Thierry762d9932015-07-30 11:05:29 +0100680
Michel Thierry762d9932015-07-30 11:05:29 +0100681 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
Chris Wilson9231da72017-02-15 08:43:41 +0000682 kunmap_atomic(pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100683}
684
Ben Widawsky94e409c2013-11-04 22:29:36 -0800685/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100686static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100687 unsigned entry,
688 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800689{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000690 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000691 u32 *cs;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800692
693 BUG_ON(entry >= 4);
694
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000695 cs = intel_ring_begin(req, 6);
696 if (IS_ERR(cs))
697 return PTR_ERR(cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800698
Tvrtko Ursulin73dec952017-02-14 11:32:42 +0000699 *cs++ = MI_LOAD_REGISTER_IMM(1);
700 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, entry));
701 *cs++ = upper_32_bits(addr);
702 *cs++ = MI_LOAD_REGISTER_IMM(1);
703 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, entry));
704 *cs++ = lower_32_bits(addr);
705 intel_ring_advance(req, cs);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800706
707 return 0;
708}
709
Michel Thierry2dba3232015-07-30 11:06:23 +0100710static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
711 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800712{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800713 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800714
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100715 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300716 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
717
John Harrisone85b26d2015-05-29 17:43:56 +0100718 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800719 if (ret)
720 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800721 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800722
Ben Widawskyeeb94882013-12-06 14:11:10 -0800723 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800724}
725
Michel Thierry2dba3232015-07-30 11:06:23 +0100726static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
727 struct drm_i915_gem_request *req)
728{
729 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
730}
731
Mika Kuoppalafce93752016-10-31 17:24:46 +0200732/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
733 * the page table structures, we mark them dirty so that
734 * context switching/execlist queuing code takes extra steps
735 * to ensure that tlbs are flushed.
736 */
737static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
738{
Chris Wilson49d73912016-11-29 09:50:08 +0000739 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200740}
741
Michał Winiarski2ce51792016-10-13 14:02:42 +0200742/* Removes entries from a single page table, releasing it if it's empty.
743 * Caller can use the return value to update higher-level entries.
744 */
745static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200746 struct i915_page_table *pt,
747 uint64_t start,
748 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700749{
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200750 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200751 unsigned int pte = gen8_pte_index(start);
752 unsigned int pte_end = pte + num_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +0000753 const gen8_pte_t scratch_pte =
754 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
755 gen8_pte_t *vaddr;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200756
757 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200758 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700759
Mika Kuoppala37c63932016-11-01 15:27:36 +0200760 GEM_BUG_ON(pte_end > GEN8_PTES);
761
762 bitmap_clear(pt->used_ptes, pte, num_entries);
Zhi Wange81ecb52017-02-08 21:03:33 +0800763 if (USES_FULL_PPGTT(vm->i915)) {
764 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
765 return true;
766 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200767
Chris Wilson9231da72017-02-15 08:43:41 +0000768 vaddr = kmap_atomic_px(pt);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200769 while (pte < pte_end)
Chris Wilson894cceb2017-02-15 08:43:37 +0000770 vaddr[pte++] = scratch_pte;
Chris Wilson9231da72017-02-15 08:43:41 +0000771 kunmap_atomic(vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200772
773 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200774}
775
Michał Winiarski2ce51792016-10-13 14:02:42 +0200776/* Removes entries from a single page dir, releasing it if it's empty.
777 * Caller can use the return value to update higher-level entries
778 */
779static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200780 struct i915_page_directory *pd,
781 uint64_t start,
782 uint64_t length)
783{
784 struct i915_page_table *pt;
785 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200786 gen8_pde_t *pde_vaddr;
787 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
788 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200789
790 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000791 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100792 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000793
Michał Winiarski2ce51792016-10-13 14:02:42 +0200794 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
795 __clear_bit(pde, pd->used_pdes);
Chris Wilson9231da72017-02-15 08:43:41 +0000796 pde_vaddr = kmap_atomic_px(pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200797 pde_vaddr[pde] = scratch_pde;
Chris Wilson9231da72017-02-15 08:43:41 +0000798 kunmap_atomic(pde_vaddr);
Chris Wilson84486612017-02-15 08:43:40 +0000799 free_pt(vm, pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200800 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200801 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200802
Zhi Wanga18dbba2016-11-29 14:55:16 +0800803 if (bitmap_empty(pd->used_pdes, I915_PDES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200804 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200805
806 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200807}
Ben Widawsky06fda602015-02-24 16:22:36 +0000808
Michał Winiarski2ce51792016-10-13 14:02:42 +0200809/* Removes entries from a single page dir pointer, releasing it if it's empty.
810 * Caller can use the return value to update higher-level entries
811 */
812static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200813 struct i915_page_directory_pointer *pdp,
814 uint64_t start,
815 uint64_t length)
816{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200817 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200818 struct i915_page_directory *pd;
819 uint64_t pdpe;
820
821 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
822 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100823 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000824
Michał Winiarski2ce51792016-10-13 14:02:42 +0200825 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
826 __clear_bit(pdpe, pdp->used_pdpes);
Matthew Auld9e65a372016-12-13 16:05:12 +0000827 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
Chris Wilson84486612017-02-15 08:43:40 +0000828 free_pd(vm, pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200829 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200830 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200831
Mika Kuoppalafce93752016-10-31 17:24:46 +0200832 mark_tlbs_dirty(ppgtt);
833
Zhi Wanga18dbba2016-11-29 14:55:16 +0800834 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200835 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200836
837 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200838}
Ben Widawsky459108b2013-11-02 21:07:23 -0700839
Michał Winiarski2ce51792016-10-13 14:02:42 +0200840/* Removes entries from a single pml4.
841 * This is the top-level structure in 4-level page tables used on gen8+.
842 * Empty entries are always scratch pml4e.
843 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200844static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
845 struct i915_pml4 *pml4,
846 uint64_t start,
847 uint64_t length)
848{
849 struct i915_page_directory_pointer *pdp;
850 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200851
Chris Wilson49d73912016-11-29 09:50:08 +0000852 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700853
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200854 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
855 if (WARN_ON(!pml4->pdps[pml4e]))
856 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700857
Michał Winiarski2ce51792016-10-13 14:02:42 +0200858 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
859 __clear_bit(pml4e, pml4->used_pml4es);
Chris Wilson84486612017-02-15 08:43:40 +0000860 gen8_setup_pml4e(pml4, vm->scratch_pdp, pml4e);
861 free_pdp(vm, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200862 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700863 }
864}
865
Michel Thierryf9b5b782015-07-30 11:02:49 +0100866static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200867 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700868{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300869 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100870
Chris Wilsonc6385c92016-11-29 12:42:05 +0000871 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200872 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
873 else
874 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100875}
876
Chris Wilson894cceb2017-02-15 08:43:37 +0000877struct sgt_dma {
878 struct scatterlist *sg;
879 dma_addr_t dma, max;
880};
881
882static __always_inline bool
883gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100884 struct i915_page_directory_pointer *pdp,
Chris Wilson894cceb2017-02-15 08:43:37 +0000885 struct sgt_dma *iter,
886 u64 start,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100887 enum i915_cache_level cache_level)
888{
Chris Wilson894cceb2017-02-15 08:43:37 +0000889 unsigned int pdpe = gen8_pdpe_index(start);
890 unsigned int pde = gen8_pde_index(start);
891 unsigned int pte = gen8_pte_index(start);
892 struct i915_page_directory *pd;
893 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
894 gen8_pte_t *vaddr;
895 bool ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700896
Chris Wilson894cceb2017-02-15 08:43:37 +0000897 pd = pdp->page_directory[pdpe];
Chris Wilson9231da72017-02-15 08:43:41 +0000898 vaddr = kmap_atomic_px(pd->page_table[pde]);
Chris Wilson894cceb2017-02-15 08:43:37 +0000899 do {
900 vaddr[pte] = pte_encode | iter->dma;
901 iter->dma += PAGE_SIZE;
902 if (iter->dma >= iter->max) {
903 iter->sg = __sg_next(iter->sg);
904 if (!iter->sg) {
905 ret = false;
906 break;
907 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700908
Chris Wilson894cceb2017-02-15 08:43:37 +0000909 iter->dma = sg_dma_address(iter->sg);
910 iter->max = iter->dma + iter->sg->length;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000911 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800912
Michel Thierry07749ef2015-03-16 16:00:54 +0000913 if (++pte == GEN8_PTES) {
Michel Thierry07749ef2015-03-16 16:00:54 +0000914 if (++pde == I915_PDES) {
Chris Wilson894cceb2017-02-15 08:43:37 +0000915 /* Limited by sg length for 3lvl */
916 if (++pdpe == GEN8_PML4ES_PER_PML4) {
917 ret = true;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100918 break;
Chris Wilson894cceb2017-02-15 08:43:37 +0000919 }
920
921 GEM_BUG_ON(pdpe > GEN8_LEGACY_PDPES);
922 pd = pdp->page_directory[pdpe];
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800923 pde = 0;
924 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000925
Chris Wilson9231da72017-02-15 08:43:41 +0000926 kunmap_atomic(vaddr);
927 vaddr = kmap_atomic_px(pd->page_table[pde]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800928 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700929 }
Chris Wilson894cceb2017-02-15 08:43:37 +0000930 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +0000931 kunmap_atomic(vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300932
Chris Wilson894cceb2017-02-15 08:43:37 +0000933 return ret;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700934}
935
Chris Wilson894cceb2017-02-15 08:43:37 +0000936static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
937 struct sg_table *pages,
938 u64 start,
939 enum i915_cache_level cache_level,
940 u32 unused)
Michel Thierryf9b5b782015-07-30 11:02:49 +0100941{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300942 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Chris Wilson894cceb2017-02-15 08:43:37 +0000943 struct sgt_dma iter = {
944 .sg = pages->sgl,
945 .dma = sg_dma_address(iter.sg),
946 .max = iter.dma + iter.sg->length,
947 };
Michel Thierryf9b5b782015-07-30 11:02:49 +0100948
Chris Wilson894cceb2017-02-15 08:43:37 +0000949 gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter,
950 start, cache_level);
951}
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100952
Chris Wilson894cceb2017-02-15 08:43:37 +0000953static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
954 struct sg_table *pages,
955 uint64_t start,
956 enum i915_cache_level cache_level,
957 u32 unused)
958{
959 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
960 struct sgt_dma iter = {
961 .sg = pages->sgl,
962 .dma = sg_dma_address(iter.sg),
963 .max = iter.dma + iter.sg->length,
964 };
965 struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
966 unsigned int pml4e = gen8_pml4e_index(start);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100967
Chris Wilson894cceb2017-02-15 08:43:37 +0000968 while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[pml4e++], &iter,
969 start, cache_level))
970 ;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100971}
972
Chris Wilson84486612017-02-15 08:43:40 +0000973static void gen8_free_page_tables(struct i915_address_space *vm,
Michel Thierryf37c0502015-06-10 17:46:39 +0100974 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800975{
976 int i;
977
Mika Kuoppala567047b2015-06-25 18:35:12 +0300978 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800979 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800980
Michel Thierry33c88192015-04-08 12:13:33 +0100981 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000982 if (WARN_ON(!pd->page_table[i]))
983 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800984
Chris Wilson84486612017-02-15 08:43:40 +0000985 free_pt(vm, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000986 pd->page_table[i] = NULL;
987 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000988}
989
Mika Kuoppala8776f022015-06-30 18:16:40 +0300990static int gen8_init_scratch(struct i915_address_space *vm)
991{
Matthew Auld64c050d2016-04-27 13:19:25 +0100992 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300993
Chris Wilson84486612017-02-15 08:43:40 +0000994 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100995 if (ret)
996 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300997
Chris Wilson84486612017-02-15 08:43:40 +0000998 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300999 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +01001000 ret = PTR_ERR(vm->scratch_pt);
1001 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001002 }
1003
Chris Wilson84486612017-02-15 08:43:40 +00001004 vm->scratch_pd = alloc_pd(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001005 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +01001006 ret = PTR_ERR(vm->scratch_pd);
1007 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001008 }
1009
Chris Wilson84486612017-02-15 08:43:40 +00001010 if (USES_FULL_48BIT_PPGTT(dev)) {
1011 vm->scratch_pdp = alloc_pdp(vm);
Michel Thierry69ab76f2015-07-29 17:23:55 +01001012 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +01001013 ret = PTR_ERR(vm->scratch_pdp);
1014 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +01001015 }
1016 }
1017
Mika Kuoppala8776f022015-06-30 18:16:40 +03001018 gen8_initialize_pt(vm, vm->scratch_pt);
1019 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001020 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +01001021 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001022
1023 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +01001024
1025free_pd:
Chris Wilson84486612017-02-15 08:43:40 +00001026 free_pd(vm, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +01001027free_pt:
Chris Wilson84486612017-02-15 08:43:40 +00001028 free_pt(vm, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +01001029free_scratch_page:
Chris Wilson84486612017-02-15 08:43:40 +00001030 cleanup_scratch_page(vm);
Matthew Auld64c050d2016-04-27 13:19:25 +01001031
1032 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001033}
1034
Zhiyuan Lv650da342015-08-28 15:41:18 +08001035static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1036{
1037 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +00001038 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001039 int i;
1040
Matthew Aulddf285642016-04-22 12:09:25 +01001041 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001042 u64 daddr = px_dma(&ppgtt->pml4);
1043
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001044 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1045 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001046
1047 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1048 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1049 } else {
1050 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1051 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1052
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001053 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1054 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001055 }
1056
1057 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1058 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1059 }
1060
1061 I915_WRITE(vgtif_reg(g2v_notify), msg);
1062
1063 return 0;
1064}
1065
Mika Kuoppala8776f022015-06-30 18:16:40 +03001066static void gen8_free_scratch(struct i915_address_space *vm)
1067{
Chris Wilson84486612017-02-15 08:43:40 +00001068 if (USES_FULL_48BIT_PPGTT(vm->i915))
1069 free_pdp(vm, vm->scratch_pdp);
1070 free_pd(vm, vm->scratch_pd);
1071 free_pt(vm, vm->scratch_pt);
1072 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001073}
1074
Chris Wilson84486612017-02-15 08:43:40 +00001075static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
Michel Thierry762d9932015-07-30 11:05:29 +01001076 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001077{
1078 int i;
1079
Chris Wilson84486612017-02-15 08:43:40 +00001080 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(vm->i915)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001081 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001082 continue;
1083
Chris Wilson84486612017-02-15 08:43:40 +00001084 gen8_free_page_tables(vm, pdp->page_directory[i]);
1085 free_pd(vm, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001086 }
Michel Thierry69876be2015-04-08 12:13:27 +01001087
Chris Wilson84486612017-02-15 08:43:40 +00001088 free_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001089}
1090
1091static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1092{
1093 int i;
1094
1095 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1096 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1097 continue;
1098
Chris Wilson84486612017-02-15 08:43:40 +00001099 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001100 }
1101
Chris Wilson84486612017-02-15 08:43:40 +00001102 cleanup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001103}
1104
1105static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1106{
Chris Wilson49d73912016-11-29 09:50:08 +00001107 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001108 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001109
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001110 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001111 gen8_ppgtt_notify_vgt(ppgtt, false);
1112
Chris Wilson84486612017-02-15 08:43:40 +00001113 if (!USES_FULL_48BIT_PPGTT(vm->i915))
1114 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001115 else
1116 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001117
Mika Kuoppala8776f022015-06-30 18:16:40 +03001118 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001119}
1120
Michel Thierryd7b26332015-04-08 12:13:34 +01001121/**
1122 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001123 * @vm: Master vm structure.
1124 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001125 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001126 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001127 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1128 * caller to free on error.
1129 *
1130 * Allocate the required number of page tables. Extremely similar to
1131 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1132 * the page directory boundary (instead of the page directory pointer). That
1133 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1134 * possible, and likely that the caller will need to use multiple calls of this
1135 * function to achieve the appropriate allocation.
1136 *
1137 * Return: 0 if success; negative error code otherwise.
1138 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001139static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001140 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001141 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001142 uint64_t length,
1143 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001144{
Michel Thierryd7b26332015-04-08 12:13:34 +01001145 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001146 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001147
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001148 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001149 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001150 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001151 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001152 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001153 continue;
1154 }
1155
Chris Wilson84486612017-02-15 08:43:40 +00001156 pt = alloc_pt(vm);
Michel Thierryd7b26332015-04-08 12:13:34 +01001157 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001158 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001159
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001160 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001161 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001162 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001163 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001164 }
1165
1166 return 0;
1167
1168unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001169 for_each_set_bit(pde, new_pts, I915_PDES)
Chris Wilson84486612017-02-15 08:43:40 +00001170 free_pt(vm, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001171
1172 return -ENOMEM;
1173}
1174
Michel Thierryd7b26332015-04-08 12:13:34 +01001175/**
1176 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001177 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001178 * @pdp: Page directory pointer for this address range.
1179 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001180 * @length: Size of the allocations.
1181 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001182 * caller to free on error.
1183 *
1184 * Allocate the required number of page directories starting at the pde index of
1185 * @start, and ending at the pde index @start + @length. This function will skip
1186 * over already allocated page directories within the range, and only allocate
1187 * new ones, setting the appropriate pointer within the pdp as well as the
1188 * correct position in the bitmap @new_pds.
1189 *
1190 * The function will only allocate the pages within the range for a give page
1191 * directory pointer. In other words, if @start + @length straddles a virtually
1192 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1193 * required by the caller, This is not currently possible, and the BUG in the
1194 * code will prevent it.
1195 *
1196 * Return: 0 if success; negative error code otherwise.
1197 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001198static int
1199gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1200 struct i915_page_directory_pointer *pdp,
1201 uint64_t start,
1202 uint64_t length,
1203 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001204{
Michel Thierryd7b26332015-04-08 12:13:34 +01001205 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001206 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001207 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001208
Michel Thierry6ac18502015-07-29 17:23:46 +01001209 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001210
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001211 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001212 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001213 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001214
Chris Wilson84486612017-02-15 08:43:40 +00001215 pd = alloc_pd(vm);
Michel Thierryd7b26332015-04-08 12:13:34 +01001216 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001217 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001218
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001219 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001220 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001221 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001222 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001223 }
1224
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001225 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001226
1227unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001228 for_each_set_bit(pdpe, new_pds, pdpes)
Chris Wilson84486612017-02-15 08:43:40 +00001229 free_pd(vm, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001230
1231 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001232}
1233
Michel Thierry762d9932015-07-30 11:05:29 +01001234/**
1235 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1236 * @vm: Master vm structure.
1237 * @pml4: Page map level 4 for this address range.
1238 * @start: Starting virtual address to begin allocations.
1239 * @length: Size of the allocations.
1240 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1241 * caller to free on error.
1242 *
1243 * Allocate the required number of page directory pointers. Extremely similar to
1244 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1245 * The main difference is here we are limited by the pml4 boundary (instead of
1246 * the page directory pointer).
1247 *
1248 * Return: 0 if success; negative error code otherwise.
1249 */
1250static int
1251gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1252 struct i915_pml4 *pml4,
1253 uint64_t start,
1254 uint64_t length,
1255 unsigned long *new_pdps)
1256{
Michel Thierry762d9932015-07-30 11:05:29 +01001257 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001258 uint32_t pml4e;
1259
1260 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1261
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001262 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001263 if (!test_bit(pml4e, pml4->used_pml4es)) {
Chris Wilson84486612017-02-15 08:43:40 +00001264 pdp = alloc_pdp(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001265 if (IS_ERR(pdp))
1266 goto unwind_out;
1267
Michel Thierry69ab76f2015-07-29 17:23:55 +01001268 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001269 pml4->pdps[pml4e] = pdp;
1270 __set_bit(pml4e, new_pdps);
1271 trace_i915_page_directory_pointer_entry_alloc(vm,
1272 pml4e,
1273 start,
1274 GEN8_PML4E_SHIFT);
1275 }
1276 }
1277
1278 return 0;
1279
1280unwind_out:
1281 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson84486612017-02-15 08:43:40 +00001282 free_pdp(vm, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001283
1284 return -ENOMEM;
1285}
1286
Michel Thierryd7b26332015-04-08 12:13:34 +01001287static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001288free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001289{
Michel Thierryd7b26332015-04-08 12:13:34 +01001290 kfree(new_pts);
1291 kfree(new_pds);
1292}
1293
1294/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1295 * of these are based on the number of PDPEs in the system.
1296 */
1297static
1298int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001299 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001300 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001301{
Michel Thierryd7b26332015-04-08 12:13:34 +01001302 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001303 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001304
Michał Winiarski3a41a052015-09-03 19:22:18 +02001305 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001306 if (!pds)
1307 return -ENOMEM;
1308
Michał Winiarski3a41a052015-09-03 19:22:18 +02001309 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1310 GFP_TEMPORARY);
1311 if (!pts)
1312 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001313
1314 *new_pds = pds;
1315 *new_pts = pts;
1316
1317 return 0;
1318
1319err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001320 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001321 return -ENOMEM;
1322}
1323
Michel Thierry762d9932015-07-30 11:05:29 +01001324static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1325 struct i915_page_directory_pointer *pdp,
1326 uint64_t start,
1327 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001328{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001329 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001330 unsigned long *new_page_dirs, *new_page_tables;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001331 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001332 const uint64_t orig_start = start;
1333 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001334 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001335 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001336 int ret;
1337
Michel Thierry6ac18502015-07-29 17:23:46 +01001338 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001339 if (ret)
1340 return ret;
1341
Michel Thierryd7b26332015-04-08 12:13:34 +01001342 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001343 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1344 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001345 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001346 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001347 return ret;
1348 }
1349
1350 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001351 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001352 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001353 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001354 if (ret)
1355 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001356 }
1357
Michel Thierry33c88192015-04-08 12:13:33 +01001358 start = orig_start;
1359 length = orig_length;
1360
Michel Thierryd7b26332015-04-08 12:13:34 +01001361 /* Allocations have completed successfully, so set the bitmaps, and do
1362 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001363 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Chris Wilson9231da72017-02-15 08:43:41 +00001364 gen8_pde_t *const page_directory = kmap_atomic_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001365 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001366 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001367 uint64_t pd_start = start;
1368 uint32_t pde;
1369
Michel Thierryd7b26332015-04-08 12:13:34 +01001370 /* Every pd should be allocated, we just did that above. */
1371 WARN_ON(!pd);
1372
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001373 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001374 /* Same reasoning as pd */
1375 WARN_ON(!pt);
1376 WARN_ON(!pd_len);
1377 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1378
1379 /* Set our used ptes within the page table */
1380 bitmap_set(pt->used_ptes,
1381 gen8_pte_index(pd_start),
1382 gen8_pte_count(pd_start, pd_len));
1383
1384 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001385 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001386
1387 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001388 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1389 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001390 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1391 gen8_pte_index(start),
1392 gen8_pte_count(start, length),
1393 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001394
1395 /* NB: We haven't yet mapped ptes to pages. At this
1396 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001397 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001398
Chris Wilson9231da72017-02-15 08:43:41 +00001399 kunmap_atomic(page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001400 __set_bit(pdpe, pdp->used_pdpes);
Matthew Auld5c693b22016-12-13 16:05:10 +00001401 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001402 }
1403
Michał Winiarski3a41a052015-09-03 19:22:18 +02001404 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001405 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001406 return 0;
1407
1408err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001409 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001410 unsigned long temp;
1411
Michał Winiarski3a41a052015-09-03 19:22:18 +02001412 for_each_set_bit(temp, new_page_tables + pdpe *
1413 BITS_TO_LONGS(I915_PDES), I915_PDES)
Chris Wilson84486612017-02-15 08:43:40 +00001414 free_pt(vm, pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001415 }
1416
Michel Thierry6ac18502015-07-29 17:23:46 +01001417 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Chris Wilson84486612017-02-15 08:43:40 +00001418 free_pd(vm, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001419
Michał Winiarski3a41a052015-09-03 19:22:18 +02001420 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001421 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001422 return ret;
1423}
1424
Michel Thierry762d9932015-07-30 11:05:29 +01001425static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1426 struct i915_pml4 *pml4,
1427 uint64_t start,
1428 uint64_t length)
1429{
1430 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Michel Thierry762d9932015-07-30 11:05:29 +01001431 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001432 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001433 int ret = 0;
1434
1435 /* Do the pml4 allocations first, so we don't need to track the newly
1436 * allocated tables below the pdp */
1437 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1438
1439 /* The pagedirectory and pagetable allocations are done in the shared 3
1440 * and 4 level code. Just allocate the pdps.
1441 */
1442 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1443 new_pdps);
1444 if (ret)
1445 return ret;
1446
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001447 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001448 WARN_ON(!pdp);
1449
1450 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1451 if (ret)
1452 goto err_out;
1453
Chris Wilson84486612017-02-15 08:43:40 +00001454 gen8_setup_pml4e(pml4, pdp, pml4e);
Michel Thierry762d9932015-07-30 11:05:29 +01001455 }
1456
1457 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1458 GEN8_PML4ES_PER_PML4);
1459
1460 return 0;
1461
1462err_out:
1463 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson84486612017-02-15 08:43:40 +00001464 gen8_ppgtt_cleanup_3lvl(vm, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001465
1466 return ret;
1467}
1468
1469static int gen8_alloc_va_range(struct i915_address_space *vm,
1470 uint64_t start, uint64_t length)
1471{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001472 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001473
Chris Wilsonc6385c92016-11-29 12:42:05 +00001474 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001475 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1476 else
1477 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1478}
1479
Chris Wilson84486612017-02-15 08:43:40 +00001480static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
1481 struct i915_page_directory_pointer *pdp,
Michel Thierryea91e402015-07-29 17:23:57 +01001482 uint64_t start, uint64_t length,
1483 gen8_pte_t scratch_pte,
1484 struct seq_file *m)
1485{
1486 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001487 uint32_t pdpe;
1488
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001489 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001490 struct i915_page_table *pt;
1491 uint64_t pd_len = length;
1492 uint64_t pd_start = start;
1493 uint32_t pde;
1494
1495 if (!test_bit(pdpe, pdp->used_pdpes))
1496 continue;
1497
1498 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001499 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001500 uint32_t pte;
1501 gen8_pte_t *pt_vaddr;
1502
1503 if (!test_bit(pde, pd->used_pdes))
1504 continue;
1505
Chris Wilson9231da72017-02-15 08:43:41 +00001506 pt_vaddr = kmap_atomic_px(pt);
Michel Thierryea91e402015-07-29 17:23:57 +01001507 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1508 uint64_t va =
1509 (pdpe << GEN8_PDPE_SHIFT) |
1510 (pde << GEN8_PDE_SHIFT) |
1511 (pte << GEN8_PTE_SHIFT);
1512 int i;
1513 bool found = false;
1514
1515 for (i = 0; i < 4; i++)
1516 if (pt_vaddr[pte + i] != scratch_pte)
1517 found = true;
1518 if (!found)
1519 continue;
1520
1521 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1522 for (i = 0; i < 4; i++) {
1523 if (pt_vaddr[pte + i] != scratch_pte)
1524 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1525 else
1526 seq_puts(m, " SCRATCH ");
1527 }
1528 seq_puts(m, "\n");
1529 }
Michel Thierryea91e402015-07-29 17:23:57 +01001530 kunmap_atomic(pt_vaddr);
1531 }
1532 }
1533}
1534
1535static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1536{
1537 struct i915_address_space *vm = &ppgtt->base;
1538 uint64_t start = ppgtt->base.start;
1539 uint64_t length = ppgtt->base.total;
Chris Wilson894cceb2017-02-15 08:43:37 +00001540 const gen8_pte_t scratch_pte =
1541 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001542
Chris Wilsonc6385c92016-11-29 12:42:05 +00001543 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Chris Wilson84486612017-02-15 08:43:40 +00001544 gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001545 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001546 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001547 struct i915_pml4 *pml4 = &ppgtt->pml4;
1548 struct i915_page_directory_pointer *pdp;
1549
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001550 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001551 if (!test_bit(pml4e, pml4->used_pml4es))
1552 continue;
1553
1554 seq_printf(m, " PML4E #%llu\n", pml4e);
Chris Wilson84486612017-02-15 08:43:40 +00001555 gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
Michel Thierryea91e402015-07-29 17:23:57 +01001556 }
1557 }
1558}
1559
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001560static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1561{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001562 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001563 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001564 int ret;
1565
1566 /* We allocate temp bitmap for page tables for no gain
1567 * but as this is for init only, lets keep the things simple
1568 */
1569 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1570 if (ret)
1571 return ret;
1572
1573 /* Allocate for all pdps regardless of how the ppgtt
1574 * was defined.
1575 */
1576 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1577 0, 1ULL << 32,
1578 new_page_dirs);
1579 if (!ret)
1580 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1581
Michał Winiarski3a41a052015-09-03 19:22:18 +02001582 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001583
1584 return ret;
1585}
1586
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001587/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001588 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1589 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1590 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1591 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001592 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001593 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001594static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001595{
Chris Wilson49d73912016-11-29 09:50:08 +00001596 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001597 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001598
Mika Kuoppala8776f022015-06-30 18:16:40 +03001599 ret = gen8_init_scratch(&ppgtt->base);
1600 if (ret)
1601 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001602
Michel Thierryd7b26332015-04-08 12:13:34 +01001603 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001604 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001605 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001606 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001607 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1608 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001609 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001610
Chris Wilson84486612017-02-15 08:43:40 +00001611 /* There are only few exceptions for gen >=6. chv and bxt.
1612 * And we are not sure about the latter so play safe for now.
1613 */
1614 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
1615 ppgtt->base.pt_kmap_wc = true;
1616
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001617 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Chris Wilson84486612017-02-15 08:43:40 +00001618 ret = setup_px(&ppgtt->base, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001619 if (ret)
1620 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001621
Michel Thierry69ab76f2015-07-29 17:23:55 +01001622 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1623
Michel Thierry762d9932015-07-30 11:05:29 +01001624 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001625 ppgtt->switch_mm = gen8_48b_mm_switch;
Chris Wilson894cceb2017-02-15 08:43:37 +00001626
1627 ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
Michel Thierry762d9932015-07-30 11:05:29 +01001628 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001629 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001630 if (ret)
1631 goto free_scratch;
1632
1633 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001634 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001635 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1636 0, 0,
1637 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001638
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001639 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001640 ret = gen8_preallocate_top_level_pdps(ppgtt);
1641 if (ret)
1642 goto free_scratch;
1643 }
Chris Wilson894cceb2017-02-15 08:43:37 +00001644
1645 ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001646 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001647
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001648 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001649 gen8_ppgtt_notify_vgt(ppgtt, true);
1650
Michel Thierryd7b26332015-04-08 12:13:34 +01001651 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001652
1653free_scratch:
1654 gen8_free_scratch(&ppgtt->base);
1655 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001656}
1657
Ben Widawsky87d60b62013-12-06 14:11:29 -08001658static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1659{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001660 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001661 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001662 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001663 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001664 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001665 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001666
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001667 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001668 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001669
Dave Gordon731f74c2016-06-24 19:37:46 +01001670 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001671 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001672 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001673 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001674 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001675 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1676
1677 if (pd_entry != expected)
1678 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1679 pde,
1680 pd_entry,
1681 expected);
1682 seq_printf(m, "\tPDE: %x\n", pd_entry);
1683
Chris Wilson9231da72017-02-15 08:43:41 +00001684 pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[pde]);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001685
Michel Thierry07749ef2015-03-16 16:00:54 +00001686 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001687 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001688 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001689 (pte * PAGE_SIZE);
1690 int i;
1691 bool found = false;
1692 for (i = 0; i < 4; i++)
1693 if (pt_vaddr[pte + i] != scratch_pte)
1694 found = true;
1695 if (!found)
1696 continue;
1697
1698 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1699 for (i = 0; i < 4; i++) {
1700 if (pt_vaddr[pte + i] != scratch_pte)
1701 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1702 else
1703 seq_puts(m, " SCRATCH ");
1704 }
1705 seq_puts(m, "\n");
1706 }
Chris Wilson9231da72017-02-15 08:43:41 +00001707 kunmap_atomic(pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001708 }
1709}
1710
Ben Widawsky678d96f2015-03-16 16:00:56 +00001711/* Write pde (index) from the page directory @pd to the page table @pt */
Chris Wilson16a011c2017-02-15 08:43:45 +00001712static inline void gen6_write_pde(const struct i915_hw_ppgtt *ppgtt,
1713 const unsigned int pde,
1714 const struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001715{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001716 /* Caller needs to make sure the write completes if necessary */
Chris Wilson16a011c2017-02-15 08:43:45 +00001717 writel_relaxed(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
1718 ppgtt->pd_addr + pde);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001719}
Ben Widawsky61973492013-04-08 18:43:54 -07001720
Ben Widawsky678d96f2015-03-16 16:00:56 +00001721/* Write all the page tables found in the ppgtt structure to incrementing page
1722 * directories. */
Chris Wilson16a011c2017-02-15 08:43:45 +00001723static void gen6_write_page_range(struct i915_hw_ppgtt *ppgtt,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001724 uint32_t start, uint32_t length)
1725{
Michel Thierryec565b32015-04-08 12:13:23 +01001726 struct i915_page_table *pt;
Chris Wilson16a011c2017-02-15 08:43:45 +00001727 unsigned int pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001728
Chris Wilson16a011c2017-02-15 08:43:45 +00001729 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde)
1730 gen6_write_pde(ppgtt, pde, pt);
1731 wmb();
Ben Widawsky678d96f2015-03-16 16:00:56 +00001732
Chris Wilson16a011c2017-02-15 08:43:45 +00001733 mark_tlbs_dirty(ppgtt);
Ben Widawsky3e302542013-04-23 23:15:32 -07001734}
1735
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001736static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001737{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001738 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001739
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001740 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001741}
Ben Widawsky61973492013-04-08 18:43:54 -07001742
Ben Widawsky90252e52013-12-06 14:11:12 -08001743static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001744 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001745{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001746 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001747 u32 *cs;
Ben Widawsky90252e52013-12-06 14:11:12 -08001748 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001749
Ben Widawsky90252e52013-12-06 14:11:12 -08001750 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001751 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001752 if (ret)
1753 return ret;
1754
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001755 cs = intel_ring_begin(req, 6);
1756 if (IS_ERR(cs))
1757 return PTR_ERR(cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001758
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001759 *cs++ = MI_LOAD_REGISTER_IMM(2);
1760 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1761 *cs++ = PP_DIR_DCLV_2G;
1762 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1763 *cs++ = get_pd_offset(ppgtt);
1764 *cs++ = MI_NOOP;
1765 intel_ring_advance(req, cs);
Ben Widawsky90252e52013-12-06 14:11:12 -08001766
1767 return 0;
1768}
1769
Ben Widawsky48a10382013-12-06 14:11:11 -08001770static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001771 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001772{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001773 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001774 u32 *cs;
Ben Widawsky48a10382013-12-06 14:11:11 -08001775 int ret;
1776
Ben Widawsky48a10382013-12-06 14:11:11 -08001777 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001778 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001779 if (ret)
1780 return ret;
1781
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001782 cs = intel_ring_begin(req, 6);
1783 if (IS_ERR(cs))
1784 return PTR_ERR(cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001785
Tvrtko Ursulin73dec952017-02-14 11:32:42 +00001786 *cs++ = MI_LOAD_REGISTER_IMM(2);
1787 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1788 *cs++ = PP_DIR_DCLV_2G;
1789 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1790 *cs++ = get_pd_offset(ppgtt);
1791 *cs++ = MI_NOOP;
1792 intel_ring_advance(req, cs);
Ben Widawsky48a10382013-12-06 14:11:11 -08001793
Ben Widawsky90252e52013-12-06 14:11:12 -08001794 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001795 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001796 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001797 if (ret)
1798 return ret;
1799 }
1800
Ben Widawsky48a10382013-12-06 14:11:11 -08001801 return 0;
1802}
1803
Ben Widawskyeeb94882013-12-06 14:11:10 -08001804static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001805 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001806{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001807 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001808 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001809
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001810 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1811 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001812 return 0;
1813}
1814
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001815static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001816{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001817 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301818 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001819
Akash Goel3b3f1652016-10-13 22:44:48 +05301820 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001821 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1822 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001823 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001824 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001825 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001826}
1827
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001828static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001829{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001830 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001831 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301832 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001833
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001834 ecobits = I915_READ(GAC_ECO_BITS);
1835 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1836
1837 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001838 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001839 ecochk |= ECOCHK_PPGTT_WB_HSW;
1840 } else {
1841 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1842 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1843 }
1844 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001845
Akash Goel3b3f1652016-10-13 22:44:48 +05301846 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001847 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001848 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001849 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001850 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001851}
1852
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001853static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001854{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001855 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001856
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001857 ecobits = I915_READ(GAC_ECO_BITS);
1858 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1859 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001860
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001861 gab_ctl = I915_READ(GAB_CTL);
1862 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001863
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001864 ecochk = I915_READ(GAM_ECOCHK);
1865 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001866
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001867 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001868}
1869
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001870/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001871static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001872 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001873 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001874{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001875 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001876 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001877 unsigned first_entry = start >> PAGE_SHIFT;
1878 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001879 unsigned act_pt = first_entry / GEN6_PTES;
1880 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001881 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001882
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001883 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001884 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001885
Daniel Vetter7bddb012012-02-09 17:15:47 +01001886 while (num_entries) {
1887 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001888 if (last_pte > GEN6_PTES)
1889 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001890
Chris Wilson9231da72017-02-15 08:43:41 +00001891 pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001892
1893 for (i = first_pte; i < last_pte; i++)
1894 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001895
Chris Wilson9231da72017-02-15 08:43:41 +00001896 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001897
Daniel Vetter7bddb012012-02-09 17:15:47 +01001898 num_entries -= last_pte - first_pte;
1899 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001900 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001901 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001902}
1903
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001904static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001905 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001906 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301907 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001908{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001909 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001910 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001911 unsigned act_pt = first_entry / GEN6_PTES;
1912 unsigned act_pte = first_entry % GEN6_PTES;
Chris Wilsonb31144c2017-02-15 08:43:36 +00001913 const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1914 struct sgt_dma iter;
1915 gen6_pte_t *vaddr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001916
Chris Wilson9231da72017-02-15 08:43:41 +00001917 vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
Chris Wilsonb31144c2017-02-15 08:43:36 +00001918 iter.sg = pages->sgl;
1919 iter.dma = sg_dma_address(iter.sg);
1920 iter.max = iter.dma + iter.sg->length;
1921 do {
1922 vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001923
Chris Wilsonb31144c2017-02-15 08:43:36 +00001924 iter.dma += PAGE_SIZE;
1925 if (iter.dma == iter.max) {
1926 iter.sg = __sg_next(iter.sg);
1927 if (!iter.sg)
1928 break;
1929
1930 iter.dma = sg_dma_address(iter.sg);
1931 iter.max = iter.dma + iter.sg->length;
1932 }
Akash Goel24f3a8c2014-06-17 10:59:42 +05301933
Michel Thierry07749ef2015-03-16 16:00:54 +00001934 if (++act_pte == GEN6_PTES) {
Chris Wilson9231da72017-02-15 08:43:41 +00001935 kunmap_atomic(vaddr);
1936 vaddr = kmap_atomic_px(ppgtt->pd.page_table[++act_pt]);
Imre Deak6e995e22013-02-18 19:28:04 +02001937 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001938 }
Chris Wilsonb31144c2017-02-15 08:43:36 +00001939 } while (1);
Chris Wilson9231da72017-02-15 08:43:41 +00001940 kunmap_atomic(vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001941}
1942
Ben Widawsky678d96f2015-03-16 16:00:56 +00001943static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001944 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001945{
Michel Thierry4933d512015-03-24 15:46:22 +00001946 DECLARE_BITMAP(new_page_tables, I915_PDES);
Chris Wilson49d73912016-11-29 09:50:08 +00001947 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001948 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001949 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001950 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001951 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001952 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001953 int ret;
1954
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001955 start = start_save = start_in;
1956 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001957
1958 bitmap_zero(new_page_tables, I915_PDES);
1959
1960 /* The allocation is done in two stages so that we can bail out with
1961 * minimal amount of pain. The first stage finds new page tables that
1962 * need allocation. The second stage marks use ptes within the page
1963 * tables.
1964 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001965 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001966 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001967 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1968 continue;
1969 }
1970
1971 /* We've already allocated a page table */
1972 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1973
Chris Wilson84486612017-02-15 08:43:40 +00001974 pt = alloc_pt(vm);
Michel Thierry4933d512015-03-24 15:46:22 +00001975 if (IS_ERR(pt)) {
1976 ret = PTR_ERR(pt);
1977 goto unwind_out;
1978 }
1979
1980 gen6_initialize_pt(vm, pt);
1981
1982 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001983 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001984 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001985 }
1986
1987 start = start_save;
1988 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001989
Dave Gordon731f74c2016-06-24 19:37:46 +01001990 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001991 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1992
1993 bitmap_zero(tmp_bitmap, GEN6_PTES);
1994 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1995 gen6_pte_count(start, length));
1996
Mika Kuoppala966082c2015-06-25 18:35:19 +03001997 if (__test_and_clear_bit(pde, new_page_tables))
Chris Wilson16a011c2017-02-15 08:43:45 +00001998 gen6_write_pde(ppgtt, pde, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001999
Michel Thierry72744cb2015-03-24 15:46:23 +00002000 trace_i915_page_table_entry_map(vm, pde, pt,
2001 gen6_pte_index(start),
2002 gen6_pte_count(start, length),
2003 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00002004 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00002005 GEN6_PTES);
2006 }
2007
Michel Thierry4933d512015-03-24 15:46:22 +00002008 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
2009
2010 /* Make sure write is complete before other code can use this page
2011 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002012 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00002013
Ben Widawsky563222a2015-03-19 12:53:28 +00002014 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002015 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00002016
2017unwind_out:
2018 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002019 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002020
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002021 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Chris Wilson84486612017-02-15 08:43:40 +00002022 free_pt(vm, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002023 }
2024
2025 mark_tlbs_dirty(ppgtt);
2026 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002027}
2028
Mika Kuoppala8776f022015-06-30 18:16:40 +03002029static int gen6_init_scratch(struct i915_address_space *vm)
2030{
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002031 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002032
Chris Wilson84486612017-02-15 08:43:40 +00002033 ret = setup_scratch_page(vm, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002034 if (ret)
2035 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002036
Chris Wilson84486612017-02-15 08:43:40 +00002037 vm->scratch_pt = alloc_pt(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002038 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson84486612017-02-15 08:43:40 +00002039 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002040 return PTR_ERR(vm->scratch_pt);
2041 }
2042
2043 gen6_initialize_pt(vm, vm->scratch_pt);
2044
2045 return 0;
2046}
2047
2048static void gen6_free_scratch(struct i915_address_space *vm)
2049{
Chris Wilson84486612017-02-15 08:43:40 +00002050 free_pt(vm, vm->scratch_pt);
2051 cleanup_scratch_page(vm);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002052}
2053
Daniel Vetter061dd492015-04-14 17:35:13 +02002054static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002055{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002056 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002057 struct i915_page_directory *pd = &ppgtt->pd;
Michel Thierry09942c62015-04-08 12:13:30 +01002058 struct i915_page_table *pt;
2059 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002060
Daniel Vetter061dd492015-04-14 17:35:13 +02002061 drm_mm_remove_node(&ppgtt->node);
2062
Dave Gordon731f74c2016-06-24 19:37:46 +01002063 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002064 if (pt != vm->scratch_pt)
Chris Wilson84486612017-02-15 08:43:40 +00002065 free_pt(vm, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002066
Mika Kuoppala8776f022015-06-30 18:16:40 +03002067 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002068}
2069
Ben Widawskyb1465202014-02-19 22:05:49 -08002070static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002071{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002072 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00002073 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002074 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002075 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002076
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002077 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2078 * allocator works in address space sizes, so it's multiplied by page
2079 * size. We allocate at the top of the GTT to avoid fragmentation.
2080 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002081 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002082
Mika Kuoppala8776f022015-06-30 18:16:40 +03002083 ret = gen6_init_scratch(vm);
2084 if (ret)
2085 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002086
Chris Wilsone007b192017-01-11 11:23:10 +00002087 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2088 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2089 I915_COLOR_UNEVICTABLE,
2090 0, ggtt->base.total,
2091 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00002092 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002093 goto err_out;
2094
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002095 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002096 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002097
Chris Wilson52c126e2017-02-15 08:43:43 +00002098 ppgtt->pd.base.ggtt_offset =
2099 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
2100
2101 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
2102 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
2103
Ben Widawskyc8c26622015-01-22 17:01:25 +00002104 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002105
2106err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002107 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002108 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002109}
2110
Ben Widawskyb1465202014-02-19 22:05:49 -08002111static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2112{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002113 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002114}
2115
Michel Thierry4933d512015-03-24 15:46:22 +00002116static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2117 uint64_t start, uint64_t length)
2118{
Michel Thierryec565b32015-04-08 12:13:23 +01002119 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002120 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002121
Dave Gordon731f74c2016-06-24 19:37:46 +01002122 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002123 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002124}
2125
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002126static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002127{
Chris Wilson49d73912016-11-29 09:50:08 +00002128 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002129 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002130 int ret;
2131
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002132 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002133 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002134 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002135 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002136 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002137 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002138 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002139 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002140 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002141
2142 ret = gen6_ppgtt_alloc(ppgtt);
2143 if (ret)
2144 return ret;
2145
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002146 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2147 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002148 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2149 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002150 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002151 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002152 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002153 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002154
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002155 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Chris Wilson16a011c2017-02-15 08:43:45 +00002156 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002157
Chris Wilson52c126e2017-02-15 08:43:43 +00002158 ret = gen6_alloc_va_range(&ppgtt->base, 0, ppgtt->base.total);
2159 if (ret) {
2160 gen6_ppgtt_cleanup(&ppgtt->base);
2161 return ret;
2162 }
2163
Thierry Reding440fd522015-01-23 09:05:06 +01002164 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002165 ppgtt->node.size >> 20,
2166 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002167
Chris Wilson52c126e2017-02-15 08:43:43 +00002168 DRM_DEBUG_DRIVER("Adding PPGTT at offset %x\n",
2169 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002170
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002171 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002172}
2173
Chris Wilson2bfa9962016-08-04 07:52:25 +01002174static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2175 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002176{
Chris Wilson49d73912016-11-29 09:50:08 +00002177 ppgtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00002178 ppgtt->base.dma = &dev_priv->drm.pdev->dev;
Daniel Vetter3440d262013-01-24 13:49:56 -08002179
Chris Wilson2bfa9962016-08-04 07:52:25 +01002180 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002181 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002182 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002183 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002184}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002185
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002186static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002187 struct drm_i915_private *dev_priv,
2188 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002189{
Chris Wilson80b204b2016-10-28 13:58:58 +01002190 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00002191
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002192 drm_mm_init(&vm->mm, vm->start, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00002193 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
2194
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002195 INIT_LIST_HEAD(&vm->active_list);
2196 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002197 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00002198
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002199 list_add_tail(&vm->global_link, &dev_priv->vm_list);
Chris Wilson84486612017-02-15 08:43:40 +00002200 pagevec_init(&vm->free_pages, false);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002201}
2202
Matthew Aulded9724d2016-11-17 21:04:10 +00002203static void i915_address_space_fini(struct i915_address_space *vm)
2204{
Chris Wilson84486612017-02-15 08:43:40 +00002205 if (pagevec_count(&vm->free_pages))
2206 vm_free_pages_release(vm);
2207
Matthew Aulded9724d2016-11-17 21:04:10 +00002208 i915_gem_timeline_fini(&vm->timeline);
2209 drm_mm_takedown(&vm->mm);
2210 list_del(&vm->global_link);
2211}
2212
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002213static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002214{
Tim Gored5165eb2016-02-04 11:49:34 +00002215 /* This function is for gtt related workarounds. This function is
2216 * called on driver load and after a GPU reset, so you can place
2217 * workarounds here even if they get overwritten by GPU reset.
2218 */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002219 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002220 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002221 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002222 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002223 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08002224 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002225 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002226 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002227 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2228}
2229
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002230int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002231{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002232 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002233
Thomas Daniel671b50132014-08-20 16:24:50 +01002234 /* In the case of execlists, PPGTT is enabled by the context descriptor
2235 * and the PDPs are contained within the context itself. We don't
2236 * need to do anything here. */
2237 if (i915.enable_execlists)
2238 return 0;
2239
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002240 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002241 return 0;
2242
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002243 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002244 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002245 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002246 gen7_ppgtt_enable(dev_priv);
2247 else if (INTEL_GEN(dev_priv) >= 8)
2248 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002249 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002250 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002251
John Harrison4ad2fd82015-06-18 13:11:20 +01002252 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002253}
John Harrison4ad2fd82015-06-18 13:11:20 +01002254
Daniel Vetter4d884702014-08-06 15:04:47 +02002255struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002256i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002257 struct drm_i915_file_private *fpriv,
2258 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002259{
2260 struct i915_hw_ppgtt *ppgtt;
2261 int ret;
2262
2263 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2264 if (!ppgtt)
2265 return ERR_PTR(-ENOMEM);
2266
Chris Wilson1188bc62017-02-15 08:43:38 +00002267 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetter4d884702014-08-06 15:04:47 +02002268 if (ret) {
2269 kfree(ppgtt);
2270 return ERR_PTR(ret);
2271 }
2272
Chris Wilson1188bc62017-02-15 08:43:38 +00002273 kref_init(&ppgtt->ref);
2274 i915_address_space_init(&ppgtt->base, dev_priv, name);
2275 ppgtt->base.file = fpriv;
2276
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002277 trace_i915_ppgtt_create(&ppgtt->base);
2278
Daniel Vetter4d884702014-08-06 15:04:47 +02002279 return ppgtt;
2280}
2281
Chris Wilson0c7eeda2017-01-11 21:09:25 +00002282void i915_ppgtt_close(struct i915_address_space *vm)
2283{
2284 struct list_head *phases[] = {
2285 &vm->active_list,
2286 &vm->inactive_list,
2287 &vm->unbound_list,
2288 NULL,
2289 }, **phase;
2290
2291 GEM_BUG_ON(vm->closed);
2292 vm->closed = true;
2293
2294 for (phase = phases; *phase; phase++) {
2295 struct i915_vma *vma, *vn;
2296
2297 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2298 if (!i915_vma_is_closed(vma))
2299 i915_vma_close(vma);
2300 }
2301}
2302
Matthew Aulded9724d2016-11-17 21:04:10 +00002303void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002304{
2305 struct i915_hw_ppgtt *ppgtt =
2306 container_of(kref, struct i915_hw_ppgtt, ref);
2307
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002308 trace_i915_ppgtt_release(&ppgtt->base);
2309
Chris Wilson50e046b2016-08-04 07:52:46 +01002310 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002311 WARN_ON(!list_empty(&ppgtt->base.active_list));
2312 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002313 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002314
2315 ppgtt->base.cleanup(&ppgtt->base);
Chris Wilson84486612017-02-15 08:43:40 +00002316 i915_address_space_fini(&ppgtt->base);
Daniel Vetteree960be2014-08-06 15:04:45 +02002317 kfree(ppgtt);
2318}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002319
Ben Widawskya81cc002013-01-18 12:30:31 -08002320/* Certain Gen5 chipsets require require idling the GPU before
2321 * unmapping anything from the GTT when VT-d is enabled.
2322 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002323static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002324{
2325#ifdef CONFIG_INTEL_IOMMU
2326 /* Query intel_iommu to see if we need the workaround. Presumably that
2327 * was loaded first.
2328 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002329 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002330 return true;
2331#endif
2332 return false;
2333}
2334
Chris Wilsondc979972016-05-10 14:10:04 +01002335void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002336{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002337 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302338 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002339
Chris Wilsondc979972016-05-10 14:10:04 +01002340 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002341 return;
2342
Akash Goel3b3f1652016-10-13 22:44:48 +05302343 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002344 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002345 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002346 if (fault_reg & RING_FAULT_VALID) {
2347 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002348 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002349 "\tAddress space: %s\n"
2350 "\tSource ID: %d\n"
2351 "\tType: %d\n",
2352 fault_reg & PAGE_MASK,
2353 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2354 RING_FAULT_SRCID(fault_reg),
2355 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002356 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002357 fault_reg & ~RING_FAULT_VALID);
2358 }
2359 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302360
2361 /* Engine specific init may not have been done till this point. */
2362 if (dev_priv->engine[RCS])
2363 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002364}
2365
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002366void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002367{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002368 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002369
2370 /* Don't bother messing with faults pre GEN6 as we have little
2371 * documentation supporting that it's a good idea.
2372 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002373 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002374 return;
2375
Chris Wilsondc979972016-05-10 14:10:04 +01002376 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002377
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002378 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002379
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002380 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002381}
2382
Chris Wilson03ac84f2016-10-28 13:58:36 +01002383int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2384 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002385{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002386 do {
2387 if (dma_map_sg(&obj->base.dev->pdev->dev,
2388 pages->sgl, pages->nents,
2389 PCI_DMA_BIDIRECTIONAL))
2390 return 0;
2391
2392 /* If the DMA remap fails, one cause can be that we have
2393 * too many objects pinned in a small remapping table,
2394 * such as swiotlb. Incrementally purge all other objects and
2395 * try again - if there are no more pages to remove from
2396 * the DMA remapper, i915_gem_shrink will return 0.
2397 */
2398 GEM_BUG_ON(obj->mm.pages == pages);
2399 } while (i915_gem_shrink(to_i915(obj->base.dev),
2400 obj->base.size >> PAGE_SHIFT,
2401 I915_SHRINK_BOUND |
2402 I915_SHRINK_UNBOUND |
2403 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002404
Chris Wilson03ac84f2016-10-28 13:58:36 +01002405 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002406}
2407
Daniel Vetter2c642b02015-04-14 17:35:26 +02002408static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002409{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002410 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002411}
2412
Chris Wilsond6473f52016-06-10 14:22:59 +05302413static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2414 dma_addr_t addr,
2415 uint64_t offset,
2416 enum i915_cache_level level,
2417 u32 unused)
2418{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002419 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302420 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002421 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302422
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002423 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302424
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002425 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302426}
2427
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002428static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2429 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002430 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302431 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002432{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002433 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002434 struct sgt_iter sgt_iter;
2435 gen8_pte_t __iomem *gtt_entries;
Chris Wilson894cceb2017-02-15 08:43:37 +00002436 const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002437 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002438
Chris Wilson894cceb2017-02-15 08:43:37 +00002439 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
2440 gtt_entries += start >> PAGE_SHIFT;
2441 for_each_sgt_dma(addr, sgt_iter, st)
2442 gen8_set_pte(gtt_entries++, pte_encode | addr);
Dave Gordon85d12252016-05-20 11:54:06 +01002443
Chris Wilson894cceb2017-02-15 08:43:37 +00002444 wmb();
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002445
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002446 /* This next bit makes the above posting read even more important. We
2447 * want to flush the TLBs only after we're certain all the PTE updates
2448 * have finished.
2449 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002450 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002451}
2452
Chris Wilsonc1403302015-11-18 15:19:39 +00002453struct insert_entries {
2454 struct i915_address_space *vm;
2455 struct sg_table *st;
2456 uint64_t start;
2457 enum i915_cache_level level;
2458 u32 flags;
2459};
2460
2461static int gen8_ggtt_insert_entries__cb(void *_arg)
2462{
2463 struct insert_entries *arg = _arg;
2464 gen8_ggtt_insert_entries(arg->vm, arg->st,
2465 arg->start, arg->level, arg->flags);
2466 return 0;
2467}
2468
2469static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2470 struct sg_table *st,
2471 uint64_t start,
2472 enum i915_cache_level level,
2473 u32 flags)
2474{
2475 struct insert_entries arg = { vm, st, start, level, flags };
2476 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2477}
2478
Chris Wilsond6473f52016-06-10 14:22:59 +05302479static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2480 dma_addr_t addr,
2481 uint64_t offset,
2482 enum i915_cache_level level,
2483 u32 flags)
2484{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002485 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302486 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002487 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302488
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002489 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302490
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002491 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302492}
2493
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002494/*
2495 * Binds an object into the global gtt with the specified cache level. The object
2496 * will be accessible to the GPU via commands whose operands reference offsets
2497 * within the global GTT as well as accessible by the GPU through the GMADR
2498 * mapped BAR (dev_priv->mm.gtt->gtt).
2499 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002500static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002501 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002502 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302503 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002504{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002505 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsonb31144c2017-02-15 08:43:36 +00002506 gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
2507 unsigned int i = start >> PAGE_SHIFT;
2508 struct sgt_iter iter;
Dave Gordon85d12252016-05-20 11:54:06 +01002509 dma_addr_t addr;
Chris Wilsonb31144c2017-02-15 08:43:36 +00002510 for_each_sgt_dma(addr, iter, st)
2511 iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2512 wmb();
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002513
2514 /* This next bit makes the above posting read even more important. We
2515 * want to flush the TLBs only after we're certain all the PTE updates
2516 * have finished.
2517 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002518 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002519}
2520
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002521static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002522 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002523{
2524}
2525
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002526static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002527 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002528{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002529 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002530 unsigned first_entry = start >> PAGE_SHIFT;
2531 unsigned num_entries = length >> PAGE_SHIFT;
Chris Wilson894cceb2017-02-15 08:43:37 +00002532 const gen8_pte_t scratch_pte =
2533 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
2534 gen8_pte_t __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002535 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2536 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002537 int i;
2538
2539 if (WARN(num_entries > max_entries,
2540 "First entry = %d; Num entries = %d (max=%d)\n",
2541 first_entry, num_entries, max_entries))
2542 num_entries = max_entries;
2543
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002544 for (i = 0; i < num_entries; i++)
2545 gen8_set_pte(&gtt_base[i], scratch_pte);
2546 readl(gtt_base);
2547}
2548
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002549static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002550 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002551 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002552{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002553 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002554 unsigned first_entry = start >> PAGE_SHIFT;
2555 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002556 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002557 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2558 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002559 int i;
2560
2561 if (WARN(num_entries > max_entries,
2562 "First entry = %d; Num entries = %d (max=%d)\n",
2563 first_entry, num_entries, max_entries))
2564 num_entries = max_entries;
2565
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002566 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002567 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002568
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002569 for (i = 0; i < num_entries; i++)
2570 iowrite32(scratch_pte, &gtt_base[i]);
2571 readl(gtt_base);
2572}
2573
Chris Wilsond6473f52016-06-10 14:22:59 +05302574static void i915_ggtt_insert_page(struct i915_address_space *vm,
2575 dma_addr_t addr,
2576 uint64_t offset,
2577 enum i915_cache_level cache_level,
2578 u32 unused)
2579{
Chris Wilsond6473f52016-06-10 14:22:59 +05302580 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2581 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302582
2583 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302584}
2585
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002586static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2587 struct sg_table *pages,
2588 uint64_t start,
2589 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002590{
2591 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2592 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2593
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002594 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002595
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002596}
2597
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002598static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002599 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002600 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002601{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002602 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002603}
2604
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002605static int ggtt_bind_vma(struct i915_vma *vma,
2606 enum i915_cache_level cache_level,
2607 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002608{
Chris Wilson49d73912016-11-29 09:50:08 +00002609 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002610 struct drm_i915_gem_object *obj = vma->obj;
Chris Wilsonba7a5742017-02-15 08:43:35 +00002611 u32 pte_flags;
Daniel Vetter0a878712015-10-15 14:23:01 +02002612
Chris Wilsonba7a5742017-02-15 08:43:35 +00002613 if (unlikely(!vma->pages)) {
2614 int ret = i915_get_ggtt_vma_pages(vma);
2615 if (ret)
2616 return ret;
2617 }
Daniel Vetter0a878712015-10-15 14:23:01 +02002618
2619 /* Currently applicable only to VLV */
Chris Wilsonba7a5742017-02-15 08:43:35 +00002620 pte_flags = 0;
Daniel Vetter0a878712015-10-15 14:23:01 +02002621 if (obj->gt_ro)
2622 pte_flags |= PTE_READ_ONLY;
2623
Chris Wilson9c870d02016-10-24 13:42:15 +01002624 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002625 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002626 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002627 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002628
2629 /*
2630 * Without aliasing PPGTT there's no difference between
2631 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2632 * upgrade to both bound if we bind either to avoid double-binding.
2633 */
Chris Wilson3272db52016-08-04 16:32:32 +01002634 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002635
2636 return 0;
2637}
2638
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002639static void ggtt_unbind_vma(struct i915_vma *vma)
2640{
2641 struct drm_i915_private *i915 = vma->vm->i915;
2642
2643 intel_runtime_pm_get(i915);
2644 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2645 intel_runtime_pm_put(i915);
2646}
2647
Daniel Vetter0a878712015-10-15 14:23:01 +02002648static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2649 enum i915_cache_level cache_level,
2650 u32 flags)
2651{
Chris Wilson49d73912016-11-29 09:50:08 +00002652 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002653 u32 pte_flags;
Chris Wilsonff685972017-02-15 08:43:42 +00002654 int ret;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002655
Chris Wilsonba7a5742017-02-15 08:43:35 +00002656 if (unlikely(!vma->pages)) {
Chris Wilsonff685972017-02-15 08:43:42 +00002657 ret = i915_get_ggtt_vma_pages(vma);
Chris Wilsonba7a5742017-02-15 08:43:35 +00002658 if (ret)
2659 return ret;
2660 }
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002661
Akash Goel24f3a8c2014-06-17 10:59:42 +05302662 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002663 pte_flags = 0;
2664 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002665 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302666
Chris Wilsonff685972017-02-15 08:43:42 +00002667 if (flags & I915_VMA_LOCAL_BIND) {
2668 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
2669
2670 if (appgtt->base.allocate_va_range) {
2671 ret = appgtt->base.allocate_va_range(&appgtt->base,
2672 vma->node.start,
2673 vma->node.size);
2674 if (ret)
2675 return ret;
2676 }
2677
2678 appgtt->base.insert_entries(&appgtt->base,
2679 vma->pages, vma->node.start,
2680 cache_level, pte_flags);
2681 }
2682
Chris Wilson3272db52016-08-04 16:32:32 +01002683 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002684 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002685 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002686 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002687 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002688 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002689 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002690
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002691 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002692}
2693
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002694static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002695{
Chris Wilson49d73912016-11-29 09:50:08 +00002696 struct drm_i915_private *i915 = vma->vm->i915;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002697
Chris Wilson9c870d02016-10-24 13:42:15 +01002698 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2699 intel_runtime_pm_get(i915);
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002700 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002701 intel_runtime_pm_put(i915);
2702 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002703
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002704 if (vma->flags & I915_VMA_LOCAL_BIND) {
2705 struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
2706
2707 vm->clear_range(vm, vma->node.start, vma->size);
2708 }
Daniel Vetter74163902012-02-15 23:50:21 +01002709}
2710
Chris Wilson03ac84f2016-10-28 13:58:36 +01002711void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2712 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002713{
David Weinehall52a05c32016-08-22 13:32:44 +03002714 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2715 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002716 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002717
Chris Wilson307dc252016-08-05 10:14:12 +01002718 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002719 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002720 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2721 /* Wait a bit, in hopes it avoids the hang */
2722 udelay(10);
2723 }
2724 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002725
Chris Wilson03ac84f2016-10-28 13:58:36 +01002726 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002727}
Daniel Vetter644ec022012-03-26 09:45:40 +02002728
Chris Wilson45b186f2016-12-16 07:46:42 +00002729static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002730 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002731 u64 *start,
2732 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002733{
Chris Wilsona6508de2017-02-06 08:45:47 +00002734 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002735 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002736
Chris Wilsona6508de2017-02-06 08:45:47 +00002737 /* Also leave a space between the unallocated reserved node after the
2738 * GTT and any objects within the GTT, i.e. we use the color adjustment
2739 * to insert a guard page to prevent prefetches crossing over the
2740 * GTT boundary.
2741 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002742 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002743 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002744 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002745}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002746
Chris Wilson6cde9a02017-02-13 17:15:50 +00002747int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2748{
2749 struct i915_ggtt *ggtt = &i915->ggtt;
2750 struct i915_hw_ppgtt *ppgtt;
2751 int err;
2752
Chris Wilson1188bc62017-02-15 08:43:38 +00002753 ppgtt = i915_ppgtt_create(i915, NULL, "[alias]");
2754 if (IS_ERR(ppgtt))
2755 return PTR_ERR(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002756
2757 if (ppgtt->base.allocate_va_range) {
2758 err = ppgtt->base.allocate_va_range(&ppgtt->base,
2759 0, ppgtt->base.total);
2760 if (err)
Chris Wilson1188bc62017-02-15 08:43:38 +00002761 goto err_ppgtt;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002762 }
2763
Chris Wilson6cde9a02017-02-13 17:15:50 +00002764 i915->mm.aliasing_ppgtt = ppgtt;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002765
Chris Wilson6cde9a02017-02-13 17:15:50 +00002766 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2767 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2768
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002769 WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
2770 ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
2771
Chris Wilson6cde9a02017-02-13 17:15:50 +00002772 return 0;
2773
Chris Wilson6cde9a02017-02-13 17:15:50 +00002774err_ppgtt:
Chris Wilson1188bc62017-02-15 08:43:38 +00002775 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002776 return err;
2777}
2778
2779void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2780{
2781 struct i915_ggtt *ggtt = &i915->ggtt;
2782 struct i915_hw_ppgtt *ppgtt;
2783
2784 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2785 if (!ppgtt)
2786 return;
2787
Chris Wilson1188bc62017-02-15 08:43:38 +00002788 i915_ppgtt_put(ppgtt);
Chris Wilson6cde9a02017-02-13 17:15:50 +00002789
2790 ggtt->base.bind_vma = ggtt_bind_vma;
Chris Wilsoncbc4e9e2017-02-15 08:43:39 +00002791 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson6cde9a02017-02-13 17:15:50 +00002792}
2793
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002794int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002795{
Ben Widawskye78891c2013-01-25 16:41:04 -08002796 /* Let GEM Manage all of the aperture.
2797 *
2798 * However, leave one page at the end still bound to the scratch page.
2799 * There are a number of places where the hardware apparently prefetches
2800 * past the end of the object, and we've seen multiple hangs with the
2801 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2802 * aperture. One page should be enough to keep any prefetching inside
2803 * of the aperture.
2804 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002805 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002806 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002807 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002808 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002809
Zhi Wangb02d22a2016-06-16 08:06:59 -04002810 ret = intel_vgt_balloon(dev_priv);
2811 if (ret)
2812 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002813
Chris Wilson95374d72016-10-12 10:05:20 +01002814 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002815 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2816 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2817 0, ggtt->mappable_end,
2818 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002819 if (ret)
2820 return ret;
2821
Chris Wilsoned2f3452012-11-15 11:32:19 +00002822 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002823 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002824 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2825 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002826 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002827 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002828 }
2829
2830 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002831 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002832 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002833
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002834 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Chris Wilson6cde9a02017-02-13 17:15:50 +00002835 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
Chris Wilson95374d72016-10-12 10:05:20 +01002836 if (ret)
Chris Wilson6cde9a02017-02-13 17:15:50 +00002837 goto err;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002838 }
2839
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002840 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002841
Chris Wilson95374d72016-10-12 10:05:20 +01002842err:
2843 drm_mm_remove_node(&ggtt->error_capture);
2844 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002845}
2846
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002847/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002848 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002849 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002850 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002851void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002852{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002853 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002854 struct i915_vma *vma, *vn;
2855
2856 ggtt->base.closed = true;
2857
2858 mutex_lock(&dev_priv->drm.struct_mutex);
2859 WARN_ON(!list_empty(&ggtt->base.active_list));
2860 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2861 WARN_ON(i915_vma_unbind(vma));
2862 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002863
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002864 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002865
Chris Wilson1188bc62017-02-15 08:43:38 +00002866 mutex_lock(&dev_priv->drm.struct_mutex);
2867 i915_gem_fini_aliasing_ppgtt(dev_priv);
2868
Chris Wilson95374d72016-10-12 10:05:20 +01002869 if (drm_mm_node_allocated(&ggtt->error_capture))
2870 drm_mm_remove_node(&ggtt->error_capture);
2871
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002872 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002873 intel_vgt_deballoon(dev_priv);
Matthew Aulded9724d2016-11-17 21:04:10 +00002874 i915_address_space_fini(&ggtt->base);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002875 }
2876
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002877 ggtt->base.cleanup(&ggtt->base);
Chris Wilson1188bc62017-02-15 08:43:38 +00002878 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002879
2880 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002881 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002882}
Daniel Vetter70e32542014-08-06 15:04:57 +02002883
Daniel Vetter2c642b02015-04-14 17:35:26 +02002884static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002885{
2886 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2887 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2888 return snb_gmch_ctl << 20;
2889}
2890
Daniel Vetter2c642b02015-04-14 17:35:26 +02002891static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002892{
2893 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2894 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2895 if (bdw_gmch_ctl)
2896 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002897
2898#ifdef CONFIG_X86_32
2899 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2900 if (bdw_gmch_ctl > 4)
2901 bdw_gmch_ctl = 4;
2902#endif
2903
Ben Widawsky9459d252013-11-03 16:53:55 -08002904 return bdw_gmch_ctl << 20;
2905}
2906
Daniel Vetter2c642b02015-04-14 17:35:26 +02002907static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002908{
2909 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2910 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2911
2912 if (gmch_ctrl)
2913 return 1 << (20 + gmch_ctrl);
2914
2915 return 0;
2916}
2917
Daniel Vetter2c642b02015-04-14 17:35:26 +02002918static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002919{
2920 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2921 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2922 return snb_gmch_ctl << 25; /* 32 MB units */
2923}
2924
Daniel Vetter2c642b02015-04-14 17:35:26 +02002925static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002926{
2927 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2928 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2929 return bdw_gmch_ctl << 25; /* 32 MB units */
2930}
2931
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002932static size_t chv_get_stolen_size(u16 gmch_ctrl)
2933{
2934 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2935 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2936
2937 /*
2938 * 0x0 to 0x10: 32MB increments starting at 0MB
2939 * 0x11 to 0x16: 4MB increments starting at 8MB
2940 * 0x17 to 0x1d: 4MB increments start at 36MB
2941 */
2942 if (gmch_ctrl < 0x11)
2943 return gmch_ctrl << 25;
2944 else if (gmch_ctrl < 0x17)
2945 return (gmch_ctrl - 0x11 + 2) << 22;
2946 else
2947 return (gmch_ctrl - 0x17 + 9) << 22;
2948}
2949
Damien Lespiau66375012014-01-09 18:02:46 +00002950static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2951{
2952 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2953 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2954
2955 if (gen9_gmch_ctl < 0xf0)
2956 return gen9_gmch_ctl << 25; /* 32 MB units */
2957 else
2958 /* 4MB increments starting at 0xf0 for 4MB */
2959 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2960}
2961
Chris Wilson34c998b2016-08-04 07:52:24 +01002962static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002963{
Chris Wilson49d73912016-11-29 09:50:08 +00002964 struct drm_i915_private *dev_priv = ggtt->base.i915;
2965 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002966 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002967 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002968
2969 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002970 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002971
Imre Deak2a073f892015-03-27 13:07:33 +02002972 /*
2973 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2974 * dropped. For WC mappings in general we have 64 byte burst writes
2975 * when the WC buffer is flushed, so we can't use it, but have to
2976 * resort to an uncached mapping. The WC issue is easily caught by the
2977 * readback check when writing GTT PTE entries.
2978 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002979 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002980 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002981 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002982 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002983 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002984 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002985 return -ENOMEM;
2986 }
2987
Chris Wilson84486612017-02-15 08:43:40 +00002988 ret = setup_scratch_page(&ggtt->base, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002989 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002990 DRM_ERROR("Scratch setup failed\n");
2991 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002992 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002993 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002994 }
2995
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002996 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002997}
2998
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002999/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3000 * bits. When using advanced contexts each context stores its own PAT, but
3001 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003002static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003003{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003004 uint64_t pat;
3005
3006 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
3007 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
3008 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
3009 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3010 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3011 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3012 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3013 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3014
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03003015 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08003016 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3017 * so RTL will always use the value corresponding to
3018 * pat_sel = 000".
3019 * So let's disable cache for GGTT to avoid screen corruptions.
3020 * MOCS still can be used though.
3021 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3022 * before this patch, i.e. the same uncached + snooping access
3023 * like on gen6/7 seems to be in effect.
3024 * - So this just fixes blitter/render access. Again it looks
3025 * like it's not just uncached access, but uncached + snooping.
3026 * So we can still hold onto all our assumptions wrt cpu
3027 * clflushing on LLC machines.
3028 */
3029 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3030
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003031 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3032 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003033 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3034 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003035}
3036
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003037static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3038{
3039 uint64_t pat;
3040
3041 /*
3042 * Map WB on BDW to snooped on CHV.
3043 *
3044 * Only the snoop bit has meaning for CHV, the rest is
3045 * ignored.
3046 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003047 * The hardware will never snoop for certain types of accesses:
3048 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3049 * - PPGTT page tables
3050 * - some other special cycles
3051 *
3052 * As with BDW, we also need to consider the following for GT accesses:
3053 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3054 * so RTL will always use the value corresponding to
3055 * pat_sel = 000".
3056 * Which means we must set the snoop bit in PAT entry 0
3057 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003058 */
3059 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3060 GEN8_PPAT(1, 0) |
3061 GEN8_PPAT(2, 0) |
3062 GEN8_PPAT(3, 0) |
3063 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3064 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3065 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3066 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3067
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003068 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3069 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003070}
3071
Chris Wilson34c998b2016-08-04 07:52:24 +01003072static void gen6_gmch_remove(struct i915_address_space *vm)
3073{
3074 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3075
3076 iounmap(ggtt->gsm);
Chris Wilson84486612017-02-15 08:43:40 +00003077 cleanup_scratch_page(vm);
Chris Wilson34c998b2016-08-04 07:52:24 +01003078}
3079
Joonas Lahtinend507d732016-03-18 10:42:58 +02003080static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003081{
Chris Wilson49d73912016-11-29 09:50:08 +00003082 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003083 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003084 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003085 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003086
3087 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003088 ggtt->mappable_base = pci_resource_start(pdev, 2);
3089 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003090
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003091 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3092 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003093
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003094 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003095
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003096 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003097 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003098 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003099 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003100 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003101 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003102 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003103 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003104 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003105 }
Ben Widawsky63340132013-11-04 19:32:22 -08003106
Chris Wilson34c998b2016-08-04 07:52:24 +01003107 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003108
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003109 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003110 chv_setup_private_ppat(dev_priv);
3111 else
3112 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003113
Chris Wilson34c998b2016-08-04 07:52:24 +01003114 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003115 ggtt->base.bind_vma = ggtt_bind_vma;
3116 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303117 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003118 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003119 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003120 ggtt->base.clear_range = gen8_ggtt_clear_range;
3121
3122 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3123 if (IS_CHERRYVIEW(dev_priv))
3124 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3125
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003126 ggtt->invalidate = gen6_ggtt_invalidate;
3127
Chris Wilson34c998b2016-08-04 07:52:24 +01003128 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003129}
3130
Joonas Lahtinend507d732016-03-18 10:42:58 +02003131static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003132{
Chris Wilson49d73912016-11-29 09:50:08 +00003133 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003134 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003135 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003136 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003137
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003138 ggtt->mappable_base = pci_resource_start(pdev, 2);
3139 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003140
Ben Widawskybaa09f52013-01-24 13:49:57 -08003141 /* 64/512MB is the current min/max we actually know of, but this is just
3142 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003143 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003144 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003145 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003146 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003147 }
3148
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003149 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3150 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3151 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003152
Joonas Lahtinend507d732016-03-18 10:42:58 +02003153 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003154
Chris Wilson34c998b2016-08-04 07:52:24 +01003155 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3156 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003157
Joonas Lahtinend507d732016-03-18 10:42:58 +02003158 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303159 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003160 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3161 ggtt->base.bind_vma = ggtt_bind_vma;
3162 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003163 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003164
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003165 ggtt->invalidate = gen6_ggtt_invalidate;
3166
Chris Wilson34c998b2016-08-04 07:52:24 +01003167 if (HAS_EDRAM(dev_priv))
3168 ggtt->base.pte_encode = iris_pte_encode;
3169 else if (IS_HASWELL(dev_priv))
3170 ggtt->base.pte_encode = hsw_pte_encode;
3171 else if (IS_VALLEYVIEW(dev_priv))
3172 ggtt->base.pte_encode = byt_pte_encode;
3173 else if (INTEL_GEN(dev_priv) >= 7)
3174 ggtt->base.pte_encode = ivb_pte_encode;
3175 else
3176 ggtt->base.pte_encode = snb_pte_encode;
3177
3178 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003179}
3180
Chris Wilson34c998b2016-08-04 07:52:24 +01003181static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003182{
Chris Wilson34c998b2016-08-04 07:52:24 +01003183 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003184}
3185
Joonas Lahtinend507d732016-03-18 10:42:58 +02003186static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003187{
Chris Wilson49d73912016-11-29 09:50:08 +00003188 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003189 int ret;
3190
Chris Wilson91c8a322016-07-05 10:40:23 +01003191 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003192 if (!ret) {
3193 DRM_ERROR("failed to set up gmch\n");
3194 return -EIO;
3195 }
3196
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003197 intel_gtt_get(&ggtt->base.total,
3198 &ggtt->stolen_size,
3199 &ggtt->mappable_base,
3200 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003201
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003202 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303203 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003204 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3205 ggtt->base.clear_range = i915_ggtt_clear_range;
3206 ggtt->base.bind_vma = ggtt_bind_vma;
3207 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003208 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003209
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003210 ggtt->invalidate = gmch_ggtt_invalidate;
3211
Joonas Lahtinend507d732016-03-18 10:42:58 +02003212 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003213 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3214
Ben Widawskybaa09f52013-01-24 13:49:57 -08003215 return 0;
3216}
3217
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003218/**
Chris Wilson0088e522016-08-04 07:52:21 +01003219 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003220 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003221 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003222int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003223{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003224 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003225 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003226
Chris Wilson49d73912016-11-29 09:50:08 +00003227 ggtt->base.i915 = dev_priv;
Chris Wilson84486612017-02-15 08:43:40 +00003228 ggtt->base.dma = &dev_priv->drm.pdev->dev;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003229
Chris Wilson34c998b2016-08-04 07:52:24 +01003230 if (INTEL_GEN(dev_priv) <= 5)
3231 ret = i915_gmch_probe(ggtt);
3232 else if (INTEL_GEN(dev_priv) < 8)
3233 ret = gen6_gmch_probe(ggtt);
3234 else
3235 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003236 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003237 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003238
Chris Wilsondb9309a2017-01-05 15:30:23 +00003239 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3240 * This is easier than doing range restriction on the fly, as we
3241 * currently don't have any bits spare to pass in this upper
3242 * restriction!
3243 */
3244 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3245 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3246 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3247 }
3248
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003249 if ((ggtt->base.total - 1) >> 32) {
3250 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003251 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003252 ggtt->base.total >> 20);
3253 ggtt->base.total = 1ULL << 32;
3254 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3255 }
3256
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003257 if (ggtt->mappable_end > ggtt->base.total) {
3258 DRM_ERROR("mappable aperture extends past end of GGTT,"
3259 " aperture=%llx, total=%llx\n",
3260 ggtt->mappable_end, ggtt->base.total);
3261 ggtt->mappable_end = ggtt->base.total;
3262 }
3263
Ben Widawskybaa09f52013-01-24 13:49:57 -08003264 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003265 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003266 ggtt->base.total >> 20);
3267 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003268 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003269#ifdef CONFIG_INTEL_IOMMU
3270 if (intel_iommu_gfx_mapped)
3271 DRM_INFO("VT-d active for gfx access\n");
3272#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003273
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003274 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003275}
3276
3277/**
3278 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003279 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003280 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003281int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003282{
Chris Wilson0088e522016-08-04 07:52:21 +01003283 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3284 int ret;
3285
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003286 INIT_LIST_HEAD(&dev_priv->vm_list);
3287
Chris Wilsona6508de2017-02-06 08:45:47 +00003288 /* Note that we use page colouring to enforce a guard page at the
3289 * end of the address space. This is required as the CS may prefetch
3290 * beyond the end of the batch buffer, across the page boundary,
3291 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003292 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003293 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01003294 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00003295 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003296 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003297 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003298
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003299 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3300 dev_priv->ggtt.mappable_base,
3301 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003302 ret = -EIO;
3303 goto out_gtt_cleanup;
3304 }
3305
3306 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3307
Chris Wilson0088e522016-08-04 07:52:21 +01003308 /*
3309 * Initialise stolen early so that we may reserve preallocated
3310 * objects for the BIOS to KMS transition.
3311 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003312 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003313 if (ret)
3314 goto out_gtt_cleanup;
3315
3316 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003317
3318out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003319 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003320 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003321}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003322
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003323int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003324{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003325 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003326 return -EIO;
3327
3328 return 0;
3329}
3330
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003331void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3332{
3333 i915->ggtt.invalidate = guc_ggtt_invalidate;
3334}
3335
3336void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3337{
3338 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3339}
3340
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003341void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003342{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003343 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003344 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003345
Chris Wilsondc979972016-05-10 14:10:04 +01003346 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003347
3348 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003349 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003350
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003351 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3352
3353 /* clflush objects bound into the GGTT and rebind them. */
3354 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003355 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003356 bool ggtt_bound = false;
3357 struct i915_vma *vma;
3358
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003359 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003360 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003361 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003362
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003363 if (!i915_vma_unbind(vma))
3364 continue;
3365
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003366 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3367 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003368 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003369 }
3370
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003371 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003372 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003373 }
3374
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003375 ggtt->base.closed = false;
3376
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003377 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003378 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003379 chv_setup_private_ppat(dev_priv);
3380 else
3381 bdw_setup_private_ppat(dev_priv);
3382
3383 return;
3384 }
3385
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003386 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003387 struct i915_address_space *vm;
3388
Daniel Vetterfa423312015-04-14 17:35:23 +02003389 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003390 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003391
Chris Wilson2bfa9962016-08-04 07:52:25 +01003392 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003393 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003394 else
3395 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003396
Chris Wilson16a011c2017-02-15 08:43:45 +00003397 gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003398 }
3399 }
3400
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003401 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003402}
3403
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003404static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003405rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003406 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003407 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003408 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003409{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003410 unsigned int column, row;
3411 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003412
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003413 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003414 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003415 for (row = 0; row < height; row++) {
3416 st->nents++;
3417 /* We don't need the pages, but need to initialize
3418 * the entries so the sg list can be happily traversed.
3419 * The only thing we need are DMA addresses.
3420 */
3421 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003422 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003423 sg_dma_len(sg) = PAGE_SIZE;
3424 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003425 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003426 }
3427 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003428
3429 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003430}
3431
Chris Wilsonba7a5742017-02-15 08:43:35 +00003432static noinline struct sg_table *
3433intel_rotate_pages(struct intel_rotation_info *rot_info,
3434 struct drm_i915_gem_object *obj)
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003435{
Dave Gordon85d12252016-05-20 11:54:06 +01003436 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003437 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003438 struct sgt_iter sgt_iter;
3439 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003440 unsigned long i;
3441 dma_addr_t *page_addr_list;
3442 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003443 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003444 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003445
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003446 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003447 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003448 sizeof(dma_addr_t),
3449 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003450 if (!page_addr_list)
3451 return ERR_PTR(ret);
3452
3453 /* Allocate target SG list. */
3454 st = kmalloc(sizeof(*st), GFP_KERNEL);
3455 if (!st)
3456 goto err_st_alloc;
3457
Ville Syrjälä6687c902015-09-15 13:16:41 +03003458 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003459 if (ret)
3460 goto err_sg_alloc;
3461
3462 /* Populate source page list from the object. */
3463 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003464 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003465 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003466
Dave Gordon85d12252016-05-20 11:54:06 +01003467 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003468 st->nents = 0;
3469 sg = st->sgl;
3470
Ville Syrjälä6687c902015-09-15 13:16:41 +03003471 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3472 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3473 rot_info->plane[i].width, rot_info->plane[i].height,
3474 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003475 }
3476
Ville Syrjälä6687c902015-09-15 13:16:41 +03003477 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3478 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003479
3480 drm_free_large(page_addr_list);
3481
3482 return st;
3483
3484err_sg_alloc:
3485 kfree(st);
3486err_st_alloc:
3487 drm_free_large(page_addr_list);
3488
Ville Syrjälä6687c902015-09-15 13:16:41 +03003489 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3490 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3491
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003492 return ERR_PTR(ret);
3493}
3494
Chris Wilsonba7a5742017-02-15 08:43:35 +00003495static noinline struct sg_table *
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003496intel_partial_pages(const struct i915_ggtt_view *view,
3497 struct drm_i915_gem_object *obj)
3498{
3499 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003500 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003501 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003502 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003503 int ret = -ENOMEM;
3504
3505 st = kmalloc(sizeof(*st), GFP_KERNEL);
3506 if (!st)
3507 goto err_st_alloc;
3508
Chris Wilsond2a84a72016-10-28 13:58:34 +01003509 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003510 if (ret)
3511 goto err_sg_alloc;
3512
Chris Wilson8bab11932017-01-14 00:28:25 +00003513 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003514 GEM_BUG_ON(!iter);
3515
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003516 sg = st->sgl;
3517 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003518 do {
3519 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003520
Chris Wilsond2a84a72016-10-28 13:58:34 +01003521 len = min(iter->length - (offset << PAGE_SHIFT),
3522 count << PAGE_SHIFT);
3523 sg_set_page(sg, NULL, len, 0);
3524 sg_dma_address(sg) =
3525 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3526 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003527
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003528 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003529 count -= len >> PAGE_SHIFT;
3530 if (count == 0) {
3531 sg_mark_end(sg);
3532 return st;
3533 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003534
Chris Wilsond2a84a72016-10-28 13:58:34 +01003535 sg = __sg_next(sg);
3536 iter = __sg_next(iter);
3537 offset = 0;
3538 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003539
3540err_sg_alloc:
3541 kfree(st);
3542err_st_alloc:
3543 return ERR_PTR(ret);
3544}
3545
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003546static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003547i915_get_ggtt_vma_pages(struct i915_vma *vma)
3548{
Chris Wilsonba7a5742017-02-15 08:43:35 +00003549 int ret;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003550
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003551 /* The vma->pages are only valid within the lifespan of the borrowed
3552 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3553 * must be the vma->pages. A simple rule is that vma->pages must only
3554 * be accessed when the obj->mm.pages are pinned.
3555 */
3556 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3557
Chris Wilsonba7a5742017-02-15 08:43:35 +00003558 switch (vma->ggtt_view.type) {
3559 case I915_GGTT_VIEW_NORMAL:
3560 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003561 return 0;
3562
Chris Wilsonba7a5742017-02-15 08:43:35 +00003563 case I915_GGTT_VIEW_ROTATED:
Chris Wilson247177d2016-08-15 10:48:47 +01003564 vma->pages =
Chris Wilsonba7a5742017-02-15 08:43:35 +00003565 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3566 break;
3567
3568 case I915_GGTT_VIEW_PARTIAL:
Chris Wilson247177d2016-08-15 10:48:47 +01003569 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003570 break;
3571
3572 default:
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003573 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3574 vma->ggtt_view.type);
Chris Wilsonba7a5742017-02-15 08:43:35 +00003575 return -EINVAL;
3576 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003577
Chris Wilsonba7a5742017-02-15 08:43:35 +00003578 ret = 0;
3579 if (unlikely(IS_ERR(vma->pages))) {
Chris Wilson247177d2016-08-15 10:48:47 +01003580 ret = PTR_ERR(vma->pages);
3581 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003582 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3583 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003584 }
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003585 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003586}
3587
Chris Wilsone007b192017-01-11 11:23:10 +00003588/**
Chris Wilson625d9882017-01-11 11:23:11 +00003589 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003590 * @vm: the &struct i915_address_space
3591 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3592 * @size: how much space to allocate inside the GTT,
3593 * must be #I915_GTT_PAGE_SIZE aligned
3594 * @offset: where to insert inside the GTT,
3595 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3596 * (@offset + @size) must fit within the address space
3597 * @color: color to apply to node, if this node is not from a VMA,
3598 * color must be #I915_COLOR_UNEVICTABLE
3599 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003600 *
3601 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3602 * the address space (using @size and @color). If the @node does not fit, it
3603 * tries to evict any overlapping nodes from the GTT, including any
3604 * neighbouring nodes if the colors do not match (to ensure guard pages between
3605 * differing domains). See i915_gem_evict_for_node() for the gory details
3606 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3607 * evicting active overlapping objects, and any overlapping node that is pinned
3608 * or marked as unevictable will also result in failure.
3609 *
3610 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3611 * asked to wait for eviction and interrupted.
3612 */
3613int i915_gem_gtt_reserve(struct i915_address_space *vm,
3614 struct drm_mm_node *node,
3615 u64 size, u64 offset, unsigned long color,
3616 unsigned int flags)
3617{
3618 int err;
3619
3620 GEM_BUG_ON(!size);
3621 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3622 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3623 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003624 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003625 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003626
3627 node->size = size;
3628 node->start = offset;
3629 node->color = color;
3630
3631 err = drm_mm_reserve_node(&vm->mm, node);
3632 if (err != -ENOSPC)
3633 return err;
3634
3635 err = i915_gem_evict_for_node(vm, node, flags);
3636 if (err == 0)
3637 err = drm_mm_reserve_node(&vm->mm, node);
3638
3639 return err;
3640}
3641
Chris Wilson606fec92017-01-11 11:23:12 +00003642static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3643{
3644 u64 range, addr;
3645
3646 GEM_BUG_ON(range_overflows(start, len, end));
3647 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3648
3649 range = round_down(end - len, align) - round_up(start, align);
3650 if (range) {
3651 if (sizeof(unsigned long) == sizeof(u64)) {
3652 addr = get_random_long();
3653 } else {
3654 addr = get_random_int();
3655 if (range > U32_MAX) {
3656 addr <<= 32;
3657 addr |= get_random_int();
3658 }
3659 }
3660 div64_u64_rem(addr, range, &addr);
3661 start += addr;
3662 }
3663
3664 return round_up(start, align);
3665}
3666
Chris Wilson625d9882017-01-11 11:23:11 +00003667/**
Chris Wilsone007b192017-01-11 11:23:10 +00003668 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003669 * @vm: the &struct i915_address_space
3670 * @node: the &struct drm_mm_node (typically i915_vma.node)
3671 * @size: how much space to allocate inside the GTT,
3672 * must be #I915_GTT_PAGE_SIZE aligned
3673 * @alignment: required alignment of starting offset, may be 0 but
3674 * if specified, this must be a power-of-two and at least
3675 * #I915_GTT_MIN_ALIGNMENT
3676 * @color: color to apply to node
3677 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003678 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003679 * @end: end of any range restriction inside GTT (U64_MAX for all),
3680 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3681 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003682 *
3683 * i915_gem_gtt_insert() first searches for an available hole into which
3684 * is can insert the node. The hole address is aligned to @alignment and
3685 * its @size must then fit entirely within the [@start, @end] bounds. The
3686 * nodes on either side of the hole must match @color, or else a guard page
3687 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003688 * suitable hole is found, first a victim is randomly selected and tested
3689 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003690 * is scanned to find the first set of replacement nodes to create the hole.
3691 * Those old overlapping nodes are evicted from the GTT (and so must be
3692 * rebound before any future use). Any node that is currently pinned cannot
3693 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3694 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3695 * searching for an eviction candidate. See i915_gem_evict_something() for
3696 * the gory details on the eviction algorithm.
3697 *
3698 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3699 * asked to wait for eviction and interrupted.
3700 */
3701int i915_gem_gtt_insert(struct i915_address_space *vm,
3702 struct drm_mm_node *node,
3703 u64 size, u64 alignment, unsigned long color,
3704 u64 start, u64 end, unsigned int flags)
3705{
Chris Wilson4e64e552017-02-02 21:04:38 +00003706 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003707 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003708 int err;
3709
3710 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3711 GEM_BUG_ON(!size);
3712 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3713 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3714 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3715 GEM_BUG_ON(start >= end);
3716 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3717 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003718 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003719 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003720
3721 if (unlikely(range_overflows(start, size, end)))
3722 return -ENOSPC;
3723
3724 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3725 return -ENOSPC;
3726
Chris Wilson4e64e552017-02-02 21:04:38 +00003727 mode = DRM_MM_INSERT_BEST;
3728 if (flags & PIN_HIGH)
3729 mode = DRM_MM_INSERT_HIGH;
3730 if (flags & PIN_MAPPABLE)
3731 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003732
3733 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3734 * so we know that we always have a minimum alignment of 4096.
3735 * The drm_mm range manager is optimised to return results
3736 * with zero alignment, so where possible use the optimal
3737 * path.
3738 */
3739 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3740 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3741 alignment = 0;
3742
Chris Wilson4e64e552017-02-02 21:04:38 +00003743 err = drm_mm_insert_node_in_range(&vm->mm, node,
3744 size, alignment, color,
3745 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003746 if (err != -ENOSPC)
3747 return err;
3748
Chris Wilson606fec92017-01-11 11:23:12 +00003749 /* No free space, pick a slot at random.
3750 *
3751 * There is a pathological case here using a GTT shared between
3752 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3753 *
3754 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3755 * (64k objects) (448k objects)
3756 *
3757 * Now imagine that the eviction LRU is ordered top-down (just because
3758 * pathology meets real life), and that we need to evict an object to
3759 * make room inside the aperture. The eviction scan then has to walk
3760 * the 448k list before it finds one within range. And now imagine that
3761 * it has to search for a new hole between every byte inside the memcpy,
3762 * for several simultaneous clients.
3763 *
3764 * On a full-ppgtt system, if we have run out of available space, there
3765 * will be lots and lots of objects in the eviction list! Again,
3766 * searching that LRU list may be slow if we are also applying any
3767 * range restrictions (e.g. restriction to low 4GiB) and so, for
3768 * simplicity and similarilty between different GTT, try the single
3769 * random replacement first.
3770 */
3771 offset = random_offset(start, end,
3772 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3773 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3774 if (err != -ENOSPC)
3775 return err;
3776
3777 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003778 err = i915_gem_evict_something(vm, size, alignment, color,
3779 start, end, flags);
3780 if (err)
3781 return err;
3782
Chris Wilson4e64e552017-02-02 21:04:38 +00003783 return drm_mm_insert_node_in_range(&vm->mm, node,
3784 size, alignment, color,
3785 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003786}
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003787
3788#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3789#include "selftests/mock_gtt.c"
Chris Wilson1c428192017-02-13 17:15:38 +00003790#include "selftests/i915_gem_gtt.c"
Chris Wilson3b5bb0a2017-02-13 17:15:18 +00003791#endif