blob: d580e2b4081be2e434d56e6d6e2387367dd2de7f [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 Wilsone007b192017-01-11 11:23:10 +000026#include <linux/log2.h>
Chris Wilson606fec92017-01-11 11:23:12 +000027#include <linux/random.h>
Daniel Vetter0e46ce22014-01-08 16:10:27 +010028#include <linux/seq_file.h>
Chris Wilson5bab6f62015-10-23 18:43:32 +010029#include <linux/stop_machine.h>
Chris Wilsone007b192017-01-11 11:23:10 +000030
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
32#include <drm/i915_drm.h>
Chris Wilsone007b192017-01-11 11:23:10 +000033
Daniel Vetter76aaf222010-11-05 22:23:30 +010034#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080035#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010036#include "i915_trace.h"
37#include "intel_drv.h"
Chris Wilsond07f0e52016-10-28 13:58:44 +010038#include "intel_frontbuffer.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010039
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010040#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
41
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000042/**
43 * DOC: Global GTT views
44 *
45 * Background and previous state
46 *
47 * Historically objects could exists (be bound) in global GTT space only as
48 * singular instances with a view representing all of the object's backing pages
49 * in a linear fashion. This view will be called a normal view.
50 *
51 * To support multiple views of the same object, where the number of mapped
52 * pages is not equal to the backing store, or where the layout of the pages
53 * is not linear, concept of a GGTT view was added.
54 *
55 * One example of an alternative view is a stereo display driven by a single
56 * image. In this case we would have a framebuffer looking like this
57 * (2x2 pages):
58 *
59 * 12
60 * 34
61 *
62 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
63 * rendering. In contrast, fed to the display engine would be an alternative
64 * view which could look something like this:
65 *
66 * 1212
67 * 3434
68 *
69 * In this example both the size and layout of pages in the alternative view is
70 * different from the normal view.
71 *
72 * Implementation and usage
73 *
74 * GGTT views are implemented using VMAs and are distinguished via enum
75 * i915_ggtt_view_type and struct i915_ggtt_view.
76 *
77 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020078 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
79 * renaming in large amounts of code. They take the struct i915_ggtt_view
80 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000081 *
82 * As a helper for callers which are only interested in the normal view,
83 * globally const i915_ggtt_view_normal singleton instance exists. All old core
84 * GEM API functions, the ones not taking the view parameter, are operating on,
85 * or with the normal GGTT view.
86 *
87 * Code wanting to add or use a new GGTT view needs to:
88 *
89 * 1. Add a new enum with a suitable name.
90 * 2. Extend the metadata in the i915_ggtt_view structure if required.
91 * 3. Add support to i915_get_vma_pages().
92 *
93 * New views are required to build a scatter-gather table from within the
94 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
95 * exists for the lifetime of an VMA.
96 *
97 * Core API is designed to have copy semantics which means that passed in
98 * struct i915_ggtt_view does not need to be persistent (left around after
99 * calling the core API functions).
100 *
101 */
102
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200103static int
104i915_get_ggtt_vma_pages(struct i915_vma *vma);
105
Chris Wilson7c3f86b2017-01-12 11:00:49 +0000106static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
107{
108 /* Note that as an uncached mmio write, this should flush the
109 * WCB of the writes into the GGTT before it triggers the invalidate.
110 */
111 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
112}
113
114static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
115{
116 gen6_ggtt_invalidate(dev_priv);
117 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
118}
119
120static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
121{
122 intel_gtt_chipset_flush();
123}
124
125static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
126{
127 i915->ggtt.invalidate(i915);
128}
129
Chris Wilsonc0336662016-05-06 15:40:21 +0100130int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
131 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200132{
Chris Wilson1893a712014-09-19 11:56:27 +0100133 bool has_aliasing_ppgtt;
134 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100135 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100136
Michel Thierry9e1d0e62016-12-05 17:57:03 -0800137 has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
138 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
139 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100140
Zhi Wange320d402016-09-06 12:04:12 +0800141 if (intel_vgpu_active(dev_priv)) {
142 /* emulation is too hard */
143 has_full_ppgtt = false;
144 has_full_48bit_ppgtt = false;
145 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800146
Chris Wilson0e4ca102016-04-29 13:18:22 +0100147 if (!has_aliasing_ppgtt)
148 return 0;
149
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000150 /*
151 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
152 * execlists, the sole mechanism available to submit work.
153 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100154 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200155 return 0;
156
157 if (enable_ppgtt == 1)
158 return 1;
159
Chris Wilson1893a712014-09-19 11:56:27 +0100160 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200161 return 2;
162
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100163 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
164 return 3;
165
Daniel Vetter93a25a92014-03-06 09:40:43 +0100166#ifdef CONFIG_INTEL_IOMMU
167 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100168 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100169 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200170 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100171 }
172#endif
173
Jesse Barnes62942ed2014-06-13 09:28:33 -0700174 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100175 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700176 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
177 return 0;
178 }
179
Zhi Wange320d402016-09-06 12:04:12 +0800180 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100181 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000182 else
183 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100184}
185
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200186static int ppgtt_bind_vma(struct i915_vma *vma,
187 enum i915_cache_level cache_level,
188 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200189{
190 u32 pte_flags = 0;
191
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100192 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100193
Daniel Vetter47552652015-04-14 17:35:24 +0200194 /* Currently applicable only to VLV */
195 if (vma->obj->gt_ro)
196 pte_flags |= PTE_READ_ONLY;
197
Chris Wilson247177d2016-08-15 10:48:47 +0100198 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200199 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200200
201 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200202}
203
204static void ppgtt_unbind_vma(struct i915_vma *vma)
205{
206 vma->vm->clear_range(vma->vm,
207 vma->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200208 vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200209}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800210
Daniel Vetter2c642b02015-04-14 17:35:26 +0200211static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200212 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700213{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200214 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700215 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300216
217 switch (level) {
218 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800219 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300220 break;
221 case I915_CACHE_WT:
222 pte |= PPAT_DISPLAY_ELLC_INDEX;
223 break;
224 default:
225 pte |= PPAT_CACHED_INDEX;
226 break;
227 }
228
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700229 return pte;
230}
231
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300232static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
233 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800234{
Michel Thierry07749ef2015-03-16 16:00:54 +0000235 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800236 pde |= addr;
237 if (level != I915_CACHE_NONE)
238 pde |= PPAT_CACHED_PDE_INDEX;
239 else
240 pde |= PPAT_UNCACHED_INDEX;
241 return pde;
242}
243
Michel Thierry762d9932015-07-30 11:05:29 +0100244#define gen8_pdpe_encode gen8_pde_encode
245#define gen8_pml4e_encode gen8_pde_encode
246
Michel Thierry07749ef2015-03-16 16:00:54 +0000247static gen6_pte_t snb_pte_encode(dma_addr_t addr,
248 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200249 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700250{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200251 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700252 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700253
254 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100255 case I915_CACHE_L3_LLC:
256 case I915_CACHE_LLC:
257 pte |= GEN6_PTE_CACHE_LLC;
258 break;
259 case I915_CACHE_NONE:
260 pte |= GEN6_PTE_UNCACHED;
261 break;
262 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100263 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100264 }
265
266 return pte;
267}
268
Michel Thierry07749ef2015-03-16 16:00:54 +0000269static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
270 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200271 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100272{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200273 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100274 pte |= GEN6_PTE_ADDR_ENCODE(addr);
275
276 switch (level) {
277 case I915_CACHE_L3_LLC:
278 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700279 break;
280 case I915_CACHE_LLC:
281 pte |= GEN6_PTE_CACHE_LLC;
282 break;
283 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700284 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700285 break;
286 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100287 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700288 }
289
Ben Widawsky54d12522012-09-24 16:44:32 -0700290 return pte;
291}
292
Michel Thierry07749ef2015-03-16 16:00:54 +0000293static gen6_pte_t byt_pte_encode(dma_addr_t addr,
294 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200295 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700296{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200297 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700298 pte |= GEN6_PTE_ADDR_ENCODE(addr);
299
Akash Goel24f3a8c2014-06-17 10:59:42 +0530300 if (!(flags & PTE_READ_ONLY))
301 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700302
303 if (level != I915_CACHE_NONE)
304 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
305
306 return pte;
307}
308
Michel Thierry07749ef2015-03-16 16:00:54 +0000309static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
310 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200311 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700312{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200313 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700314 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700315
316 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700317 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700318
319 return pte;
320}
321
Michel Thierry07749ef2015-03-16 16:00:54 +0000322static gen6_pte_t iris_pte_encode(dma_addr_t addr,
323 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200324 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700325{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200326 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700327 pte |= HSW_PTE_ADDR_ENCODE(addr);
328
Chris Wilson651d7942013-08-08 14:41:10 +0100329 switch (level) {
330 case I915_CACHE_NONE:
331 break;
332 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000333 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100334 break;
335 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000336 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100337 break;
338 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700339
340 return pte;
341}
342
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000343static int __setup_page_dma(struct drm_i915_private *dev_priv,
Mika Kuoppalac114f762015-06-25 18:35:13 +0300344 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000345{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000346 struct device *kdev = &dev_priv->drm.pdev->dev;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000347
Mika Kuoppalac114f762015-06-25 18:35:13 +0300348 p->page = alloc_page(flags);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300349 if (!p->page)
Michel Thierry1266cdb2015-03-24 17:06:33 +0000350 return -ENOMEM;
351
David Weinehallc49d13e2016-08-22 13:32:42 +0300352 p->daddr = dma_map_page(kdev,
Chris Wilsonf51455d2017-01-10 14:47:34 +0000353 p->page, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300354
David Weinehallc49d13e2016-08-22 13:32:42 +0300355 if (dma_mapping_error(kdev, p->daddr)) {
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300356 __free_page(p->page);
357 return -EINVAL;
358 }
359
Michel Thierry1266cdb2015-03-24 17:06:33 +0000360 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000361}
362
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000363static int setup_page_dma(struct drm_i915_private *dev_priv,
364 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300365{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000366 return __setup_page_dma(dev_priv, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300367}
368
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000369static void cleanup_page_dma(struct drm_i915_private *dev_priv,
370 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300371{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000372 struct pci_dev *pdev = dev_priv->drm.pdev;
David Weinehall52a05c32016-08-22 13:32:44 +0300373
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300374 if (WARN_ON(!p->page))
375 return;
376
Chris Wilsonf51455d2017-01-10 14:47:34 +0000377 dma_unmap_page(&pdev->dev, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300378 __free_page(p->page);
379 memset(p, 0, sizeof(*p));
380}
381
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300382static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300383{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300384 return kmap_atomic(p->page);
385}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300386
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300387/* We use the flushing unmap only with ppgtt structures:
388 * page directories, page tables and scratch pages.
389 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100390static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300391{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300392 /* There are only few exceptions for gen >=6. chv and bxt.
393 * And we are not sure about the latter so play safe for now.
394 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200395 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300396 drm_clflush_virt_range(vaddr, PAGE_SIZE);
397
398 kunmap_atomic(vaddr);
399}
400
Mika Kuoppala567047b2015-06-25 18:35:12 +0300401#define kmap_px(px) kmap_page_dma(px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100402#define kunmap_px(ppgtt, vaddr) \
Chris Wilson49d73912016-11-29 09:50:08 +0000403 kunmap_page_dma((ppgtt)->base.i915, (vaddr))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300404
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000405#define setup_px(dev_priv, px) setup_page_dma((dev_priv), px_base(px))
406#define cleanup_px(dev_priv, px) cleanup_page_dma((dev_priv), px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100407#define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
408#define fill32_px(dev_priv, px, v) \
409 fill_page_dma_32((dev_priv), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300410
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100411static void fill_page_dma(struct drm_i915_private *dev_priv,
412 struct i915_page_dma *p, const uint64_t val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300413{
414 int i;
415 uint64_t * const vaddr = kmap_page_dma(p);
416
417 for (i = 0; i < 512; i++)
418 vaddr[i] = val;
419
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100420 kunmap_page_dma(dev_priv, vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300421}
422
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100423static void fill_page_dma_32(struct drm_i915_private *dev_priv,
424 struct i915_page_dma *p, const uint32_t val32)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300425{
426 uint64_t v = val32;
427
428 v = v << 32 | val32;
429
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100430 fill_page_dma(dev_priv, p, v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300431}
432
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100433static int
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000434setup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100435 struct i915_page_dma *scratch,
436 gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300437{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000438 return __setup_page_dma(dev_priv, scratch, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300439}
440
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000441static void cleanup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100442 struct i915_page_dma *scratch)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300443{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000444 cleanup_page_dma(dev_priv, scratch);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300445}
446
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000447static struct i915_page_table *alloc_pt(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000448{
Michel Thierryec565b32015-04-08 12:13:23 +0100449 struct i915_page_table *pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000450 const size_t count = INTEL_GEN(dev_priv) >= 8 ? GEN8_PTES : GEN6_PTES;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000451 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000452
453 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
454 if (!pt)
455 return ERR_PTR(-ENOMEM);
456
Ben Widawsky678d96f2015-03-16 16:00:56 +0000457 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
458 GFP_KERNEL);
459
460 if (!pt->used_ptes)
461 goto fail_bitmap;
462
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000463 ret = setup_px(dev_priv, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000464 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300465 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000466
467 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000468
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300469fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000470 kfree(pt->used_ptes);
471fail_bitmap:
472 kfree(pt);
473
474 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000475}
476
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000477static void free_pt(struct drm_i915_private *dev_priv,
478 struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000479{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000480 cleanup_px(dev_priv, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300481 kfree(pt->used_ptes);
482 kfree(pt);
483}
484
485static void gen8_initialize_pt(struct i915_address_space *vm,
486 struct i915_page_table *pt)
487{
488 gen8_pte_t scratch_pte;
489
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100490 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200491 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300492
Chris Wilson49d73912016-11-29 09:50:08 +0000493 fill_px(vm->i915, pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300494}
495
496static void gen6_initialize_pt(struct i915_address_space *vm,
497 struct i915_page_table *pt)
498{
499 gen6_pte_t scratch_pte;
500
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100501 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300502
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100503 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200504 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300505
Chris Wilson49d73912016-11-29 09:50:08 +0000506 fill32_px(vm->i915, pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000507}
508
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000509static struct i915_page_directory *alloc_pd(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000510{
Michel Thierryec565b32015-04-08 12:13:23 +0100511 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100512 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000513
514 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
515 if (!pd)
516 return ERR_PTR(-ENOMEM);
517
Michel Thierry33c88192015-04-08 12:13:33 +0100518 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
519 sizeof(*pd->used_pdes), GFP_KERNEL);
520 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300521 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100522
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000523 ret = setup_px(dev_priv, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100524 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300525 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100526
Ben Widawsky06fda602015-02-24 16:22:36 +0000527 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100528
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300529fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100530 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300531fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100532 kfree(pd);
533
534 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000535}
536
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000537static void free_pd(struct drm_i915_private *dev_priv,
538 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300539{
540 if (px_page(pd)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000541 cleanup_px(dev_priv, pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300542 kfree(pd->used_pdes);
543 kfree(pd);
544 }
545}
546
547static void gen8_initialize_pd(struct i915_address_space *vm,
548 struct i915_page_directory *pd)
549{
550 gen8_pde_t scratch_pde;
551
552 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
553
Chris Wilson49d73912016-11-29 09:50:08 +0000554 fill_px(vm->i915, pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300555}
556
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000557static int __pdp_init(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100558 struct i915_page_directory_pointer *pdp)
559{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000560 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Michel Thierry6ac18502015-07-29 17:23:46 +0100561
562 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
563 sizeof(unsigned long),
564 GFP_KERNEL);
565 if (!pdp->used_pdpes)
566 return -ENOMEM;
567
568 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
569 GFP_KERNEL);
570 if (!pdp->page_directory) {
571 kfree(pdp->used_pdpes);
572 /* the PDP might be the statically allocated top level. Keep it
573 * as clean as possible */
574 pdp->used_pdpes = NULL;
575 return -ENOMEM;
576 }
577
578 return 0;
579}
580
581static void __pdp_fini(struct i915_page_directory_pointer *pdp)
582{
583 kfree(pdp->used_pdpes);
584 kfree(pdp->page_directory);
585 pdp->page_directory = NULL;
586}
587
Michel Thierry762d9932015-07-30 11:05:29 +0100588static struct
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000589i915_page_directory_pointer *alloc_pdp(struct drm_i915_private *dev_priv)
Michel Thierry762d9932015-07-30 11:05:29 +0100590{
591 struct i915_page_directory_pointer *pdp;
592 int ret = -ENOMEM;
593
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000594 WARN_ON(!USES_FULL_48BIT_PPGTT(dev_priv));
Michel Thierry762d9932015-07-30 11:05:29 +0100595
596 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
597 if (!pdp)
598 return ERR_PTR(-ENOMEM);
599
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000600 ret = __pdp_init(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100601 if (ret)
602 goto fail_bitmap;
603
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000604 ret = setup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100605 if (ret)
606 goto fail_page_m;
607
608 return pdp;
609
610fail_page_m:
611 __pdp_fini(pdp);
612fail_bitmap:
613 kfree(pdp);
614
615 return ERR_PTR(ret);
616}
617
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000618static void free_pdp(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100619 struct i915_page_directory_pointer *pdp)
620{
621 __pdp_fini(pdp);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000622 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
623 cleanup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100624 kfree(pdp);
625 }
626}
627
Michel Thierry69ab76f2015-07-29 17:23:55 +0100628static void gen8_initialize_pdp(struct i915_address_space *vm,
629 struct i915_page_directory_pointer *pdp)
630{
631 gen8_ppgtt_pdpe_t scratch_pdpe;
632
633 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
634
Chris Wilson49d73912016-11-29 09:50:08 +0000635 fill_px(vm->i915, pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100636}
637
638static void gen8_initialize_pml4(struct i915_address_space *vm,
639 struct i915_pml4 *pml4)
640{
641 gen8_ppgtt_pml4e_t scratch_pml4e;
642
643 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
644 I915_CACHE_LLC);
645
Chris Wilson49d73912016-11-29 09:50:08 +0000646 fill_px(vm->i915, pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100647}
648
Michel Thierry762d9932015-07-30 11:05:29 +0100649static void
Matthew Auld5c693b22016-12-13 16:05:10 +0000650gen8_setup_pdpe(struct i915_hw_ppgtt *ppgtt,
651 struct i915_page_directory_pointer *pdp,
652 struct i915_page_directory *pd,
653 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100654{
655 gen8_ppgtt_pdpe_t *page_directorypo;
656
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000657 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
Michel Thierry762d9932015-07-30 11:05:29 +0100658 return;
659
660 page_directorypo = kmap_px(pdp);
661 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
662 kunmap_px(ppgtt, page_directorypo);
663}
664
665static void
Matthew Auld56843102016-12-13 16:05:11 +0000666gen8_setup_pml4e(struct i915_hw_ppgtt *ppgtt,
667 struct i915_pml4 *pml4,
668 struct i915_page_directory_pointer *pdp,
669 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100670{
671 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
672
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000673 WARN_ON(!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)));
Michel Thierry762d9932015-07-30 11:05:29 +0100674 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
675 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100676}
677
Ben Widawsky94e409c2013-11-04 22:29:36 -0800678/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100679static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100680 unsigned entry,
681 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800682{
Chris Wilson7e37f882016-08-02 22:50:21 +0100683 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000684 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800685 int ret;
686
687 BUG_ON(entry >= 4);
688
John Harrison5fb9de12015-05-29 17:44:07 +0100689 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800690 if (ret)
691 return ret;
692
Chris Wilsonb5321f32016-08-02 22:50:18 +0100693 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
694 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
695 intel_ring_emit(ring, upper_32_bits(addr));
696 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
697 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
698 intel_ring_emit(ring, lower_32_bits(addr));
699 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800700
701 return 0;
702}
703
Michel Thierry2dba3232015-07-30 11:06:23 +0100704static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
705 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800706{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800707 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800708
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100709 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300710 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
711
John Harrisone85b26d2015-05-29 17:43:56 +0100712 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800713 if (ret)
714 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800715 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800716
Ben Widawskyeeb94882013-12-06 14:11:10 -0800717 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800718}
719
Michel Thierry2dba3232015-07-30 11:06:23 +0100720static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
721 struct drm_i915_gem_request *req)
722{
723 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
724}
725
Mika Kuoppalafce93752016-10-31 17:24:46 +0200726/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
727 * the page table structures, we mark them dirty so that
728 * context switching/execlist queuing code takes extra steps
729 * to ensure that tlbs are flushed.
730 */
731static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
732{
Chris Wilson49d73912016-11-29 09:50:08 +0000733 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200734}
735
Michał Winiarski2ce51792016-10-13 14:02:42 +0200736/* Removes entries from a single page table, releasing it if it's empty.
737 * Caller can use the return value to update higher-level entries.
738 */
739static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200740 struct i915_page_table *pt,
741 uint64_t start,
742 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700743{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300744 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200745 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200746 unsigned int pte = gen8_pte_index(start);
747 unsigned int pte_end = pte + num_entries;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200748 gen8_pte_t *pt_vaddr;
749 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
750 I915_CACHE_LLC);
751
752 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200753 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700754
Mika Kuoppala37c63932016-11-01 15:27:36 +0200755 GEM_BUG_ON(pte_end > GEN8_PTES);
756
757 bitmap_clear(pt->used_ptes, pte, num_entries);
Zhi Wange81ecb52017-02-08 21:03:33 +0800758 if (USES_FULL_PPGTT(vm->i915)) {
759 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
760 return true;
761 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200762
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200763 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000764
Mika Kuoppala37c63932016-11-01 15:27:36 +0200765 while (pte < pte_end)
766 pt_vaddr[pte++] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000767
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200768 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200769
770 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200771}
772
Michał Winiarski2ce51792016-10-13 14:02:42 +0200773/* Removes entries from a single page dir, releasing it if it's empty.
774 * Caller can use the return value to update higher-level entries
775 */
776static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200777 struct i915_page_directory *pd,
778 uint64_t start,
779 uint64_t length)
780{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200781 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200782 struct i915_page_table *pt;
783 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200784 gen8_pde_t *pde_vaddr;
785 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
786 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200787
788 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000789 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100790 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000791
Michał Winiarski2ce51792016-10-13 14:02:42 +0200792 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
793 __clear_bit(pde, pd->used_pdes);
794 pde_vaddr = kmap_px(pd);
795 pde_vaddr[pde] = scratch_pde;
796 kunmap_px(ppgtt, pde_vaddr);
Chris Wilson49d73912016-11-29 09:50:08 +0000797 free_pt(vm->i915, pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200798 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200799 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200800
Zhi Wanga18dbba2016-11-29 14:55:16 +0800801 if (bitmap_empty(pd->used_pdes, I915_PDES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200802 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200803
804 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200805}
Ben Widawsky06fda602015-02-24 16:22:36 +0000806
Michał Winiarski2ce51792016-10-13 14:02:42 +0200807/* Removes entries from a single page dir pointer, releasing it if it's empty.
808 * Caller can use the return value to update higher-level entries
809 */
810static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200811 struct i915_page_directory_pointer *pdp,
812 uint64_t start,
813 uint64_t length)
814{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200815 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200816 struct i915_page_directory *pd;
817 uint64_t pdpe;
818
819 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
820 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100821 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000822
Michał Winiarski2ce51792016-10-13 14:02:42 +0200823 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
824 __clear_bit(pdpe, pdp->used_pdpes);
Matthew Auld9e65a372016-12-13 16:05:12 +0000825 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
Chris Wilson49d73912016-11-29 09:50:08 +0000826 free_pd(vm->i915, pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200827 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200828 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200829
Mika Kuoppalafce93752016-10-31 17:24:46 +0200830 mark_tlbs_dirty(ppgtt);
831
Zhi Wanga18dbba2016-11-29 14:55:16 +0800832 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200833 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200834
835 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200836}
Ben Widawsky459108b2013-11-02 21:07:23 -0700837
Michał Winiarski2ce51792016-10-13 14:02:42 +0200838/* Removes entries from a single pml4.
839 * This is the top-level structure in 4-level page tables used on gen8+.
840 * Empty entries are always scratch pml4e.
841 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200842static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
843 struct i915_pml4 *pml4,
844 uint64_t start,
845 uint64_t length)
846{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200847 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200848 struct i915_page_directory_pointer *pdp;
849 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200850
Chris Wilson49d73912016-11-29 09:50:08 +0000851 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700852
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200853 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
854 if (WARN_ON(!pml4->pdps[pml4e]))
855 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700856
Michał Winiarski2ce51792016-10-13 14:02:42 +0200857 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
858 __clear_bit(pml4e, pml4->used_pml4es);
Matthew Auld9e65a372016-12-13 16:05:12 +0000859 gen8_setup_pml4e(ppgtt, pml4, vm->scratch_pdp, pml4e);
Chris Wilson49d73912016-11-29 09:50:08 +0000860 free_pdp(vm->i915, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200861 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700862 }
863}
864
Michel Thierryf9b5b782015-07-30 11:02:49 +0100865static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200866 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700867{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300868 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100869
Chris Wilsonc6385c92016-11-29 12:42:05 +0000870 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200871 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
872 else
873 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100874}
875
876static void
877gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
878 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100879 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100880 uint64_t start,
881 enum i915_cache_level cache_level)
882{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300883 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000884 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100885 unsigned pdpe = gen8_pdpe_index(start);
886 unsigned pde = gen8_pde_index(start);
887 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700888
Chris Wilson6f1cc992013-12-31 15:50:31 +0000889 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700890
Michel Thierry3387d432015-08-03 09:52:47 +0100891 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000892 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100893 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100894 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300895 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000896 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800897
898 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100899 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200900 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000901 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300902 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000903 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000904 if (++pde == I915_PDES) {
Chris Wilsonc6385c92016-11-29 12:42:05 +0000905 if (++pdpe == I915_PDPES_PER_PDP(vm->i915))
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100906 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800907 pde = 0;
908 }
909 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700910 }
911 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300912
913 if (pt_vaddr)
914 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700915}
916
Michel Thierryf9b5b782015-07-30 11:02:49 +0100917static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
918 struct sg_table *pages,
919 uint64_t start,
920 enum i915_cache_level cache_level,
921 u32 unused)
922{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300923 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100924 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100925
Michel Thierry3387d432015-08-03 09:52:47 +0100926 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100927
Chris Wilsonc6385c92016-11-29 12:42:05 +0000928 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100929 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
930 cache_level);
931 } else {
932 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000933 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100934 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
935
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000936 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100937 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
938 start, cache_level);
939 }
940 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100941}
942
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000943static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
Michel Thierryf37c0502015-06-10 17:46:39 +0100944 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800945{
946 int i;
947
Mika Kuoppala567047b2015-06-25 18:35:12 +0300948 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800949 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800950
Michel Thierry33c88192015-04-08 12:13:33 +0100951 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000952 if (WARN_ON(!pd->page_table[i]))
953 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800954
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000955 free_pt(dev_priv, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000956 pd->page_table[i] = NULL;
957 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000958}
959
Mika Kuoppala8776f022015-06-30 18:16:40 +0300960static int gen8_init_scratch(struct i915_address_space *vm)
961{
Chris Wilson49d73912016-11-29 09:50:08 +0000962 struct drm_i915_private *dev_priv = vm->i915;
Matthew Auld64c050d2016-04-27 13:19:25 +0100963 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300964
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000965 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100966 if (ret)
967 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300968
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000969 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300970 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100971 ret = PTR_ERR(vm->scratch_pt);
972 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300973 }
974
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000975 vm->scratch_pd = alloc_pd(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300976 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100977 ret = PTR_ERR(vm->scratch_pd);
978 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300979 }
980
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000981 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
982 vm->scratch_pdp = alloc_pdp(dev_priv);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100983 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100984 ret = PTR_ERR(vm->scratch_pdp);
985 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100986 }
987 }
988
Mika Kuoppala8776f022015-06-30 18:16:40 +0300989 gen8_initialize_pt(vm, vm->scratch_pt);
990 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000991 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100992 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300993
994 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100995
996free_pd:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000997 free_pd(dev_priv, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +0100998free_pt:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000999 free_pt(dev_priv, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +01001000free_scratch_page:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001001 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +01001002
1003 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001004}
1005
Zhiyuan Lv650da342015-08-28 15:41:18 +08001006static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1007{
1008 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +00001009 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001010 int i;
1011
Matthew Aulddf285642016-04-22 12:09:25 +01001012 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001013 u64 daddr = px_dma(&ppgtt->pml4);
1014
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001015 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1016 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001017
1018 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1019 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1020 } else {
1021 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1022 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1023
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001024 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1025 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001026 }
1027
1028 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1029 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1030 }
1031
1032 I915_WRITE(vgtif_reg(g2v_notify), msg);
1033
1034 return 0;
1035}
1036
Mika Kuoppala8776f022015-06-30 18:16:40 +03001037static void gen8_free_scratch(struct i915_address_space *vm)
1038{
Chris Wilson49d73912016-11-29 09:50:08 +00001039 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001040
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001041 if (USES_FULL_48BIT_PPGTT(dev_priv))
1042 free_pdp(dev_priv, vm->scratch_pdp);
1043 free_pd(dev_priv, vm->scratch_pd);
1044 free_pt(dev_priv, vm->scratch_pt);
1045 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001046}
1047
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001048static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
Michel Thierry762d9932015-07-30 11:05:29 +01001049 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001050{
1051 int i;
1052
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001053 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001054 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001055 continue;
1056
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001057 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1058 free_pd(dev_priv, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001059 }
Michel Thierry69876be2015-04-08 12:13:27 +01001060
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001061 free_pdp(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001062}
1063
1064static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1065{
Chris Wilson49d73912016-11-29 09:50:08 +00001066 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001067 int i;
1068
1069 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1070 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1071 continue;
1072
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001073 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001074 }
1075
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001076 cleanup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001077}
1078
1079static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1080{
Chris Wilson49d73912016-11-29 09:50:08 +00001081 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001082 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001083
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001084 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001085 gen8_ppgtt_notify_vgt(ppgtt, false);
1086
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001087 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1088 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001089 else
1090 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001091
Mika Kuoppala8776f022015-06-30 18:16:40 +03001092 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001093}
1094
Michel Thierryd7b26332015-04-08 12:13:34 +01001095/**
1096 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001097 * @vm: Master vm structure.
1098 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001099 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001100 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001101 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1102 * caller to free on error.
1103 *
1104 * Allocate the required number of page tables. Extremely similar to
1105 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1106 * the page directory boundary (instead of the page directory pointer). That
1107 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1108 * possible, and likely that the caller will need to use multiple calls of this
1109 * function to achieve the appropriate allocation.
1110 *
1111 * Return: 0 if success; negative error code otherwise.
1112 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001113static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001114 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001115 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001116 uint64_t length,
1117 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001118{
Chris Wilson49d73912016-11-29 09:50:08 +00001119 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001120 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001121 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001122
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001123 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001124 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001125 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001126 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001127 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001128 continue;
1129 }
1130
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001131 pt = alloc_pt(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001132 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001133 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001134
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001135 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001136 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001137 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001138 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001139 }
1140
1141 return 0;
1142
1143unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001144 for_each_set_bit(pde, new_pts, I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001145 free_pt(dev_priv, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001146
1147 return -ENOMEM;
1148}
1149
Michel Thierryd7b26332015-04-08 12:13:34 +01001150/**
1151 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001152 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001153 * @pdp: Page directory pointer for this address range.
1154 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001155 * @length: Size of the allocations.
1156 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001157 * caller to free on error.
1158 *
1159 * Allocate the required number of page directories starting at the pde index of
1160 * @start, and ending at the pde index @start + @length. This function will skip
1161 * over already allocated page directories within the range, and only allocate
1162 * new ones, setting the appropriate pointer within the pdp as well as the
1163 * correct position in the bitmap @new_pds.
1164 *
1165 * The function will only allocate the pages within the range for a give page
1166 * directory pointer. In other words, if @start + @length straddles a virtually
1167 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1168 * required by the caller, This is not currently possible, and the BUG in the
1169 * code will prevent it.
1170 *
1171 * Return: 0 if success; negative error code otherwise.
1172 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001173static int
1174gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1175 struct i915_page_directory_pointer *pdp,
1176 uint64_t start,
1177 uint64_t length,
1178 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001179{
Chris Wilson49d73912016-11-29 09:50:08 +00001180 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001181 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001182 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001183 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001184
Michel Thierry6ac18502015-07-29 17:23:46 +01001185 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001186
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001187 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001188 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001189 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001190
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001191 pd = alloc_pd(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001192 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001193 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001194
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001195 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001196 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001197 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001198 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001199 }
1200
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001201 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001202
1203unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001204 for_each_set_bit(pdpe, new_pds, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001205 free_pd(dev_priv, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001206
1207 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001208}
1209
Michel Thierry762d9932015-07-30 11:05:29 +01001210/**
1211 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1212 * @vm: Master vm structure.
1213 * @pml4: Page map level 4 for this address range.
1214 * @start: Starting virtual address to begin allocations.
1215 * @length: Size of the allocations.
1216 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1217 * caller to free on error.
1218 *
1219 * Allocate the required number of page directory pointers. Extremely similar to
1220 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1221 * The main difference is here we are limited by the pml4 boundary (instead of
1222 * the page directory pointer).
1223 *
1224 * Return: 0 if success; negative error code otherwise.
1225 */
1226static int
1227gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1228 struct i915_pml4 *pml4,
1229 uint64_t start,
1230 uint64_t length,
1231 unsigned long *new_pdps)
1232{
Chris Wilson49d73912016-11-29 09:50:08 +00001233 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001234 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001235 uint32_t pml4e;
1236
1237 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1238
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001239 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001240 if (!test_bit(pml4e, pml4->used_pml4es)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001241 pdp = alloc_pdp(dev_priv);
Michel Thierry762d9932015-07-30 11:05:29 +01001242 if (IS_ERR(pdp))
1243 goto unwind_out;
1244
Michel Thierry69ab76f2015-07-29 17:23:55 +01001245 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001246 pml4->pdps[pml4e] = pdp;
1247 __set_bit(pml4e, new_pdps);
1248 trace_i915_page_directory_pointer_entry_alloc(vm,
1249 pml4e,
1250 start,
1251 GEN8_PML4E_SHIFT);
1252 }
1253 }
1254
1255 return 0;
1256
1257unwind_out:
1258 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001259 free_pdp(dev_priv, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001260
1261 return -ENOMEM;
1262}
1263
Michel Thierryd7b26332015-04-08 12:13:34 +01001264static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001265free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001266{
Michel Thierryd7b26332015-04-08 12:13:34 +01001267 kfree(new_pts);
1268 kfree(new_pds);
1269}
1270
1271/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1272 * of these are based on the number of PDPEs in the system.
1273 */
1274static
1275int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001276 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001277 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001278{
Michel Thierryd7b26332015-04-08 12:13:34 +01001279 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001280 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001281
Michał Winiarski3a41a052015-09-03 19:22:18 +02001282 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001283 if (!pds)
1284 return -ENOMEM;
1285
Michał Winiarski3a41a052015-09-03 19:22:18 +02001286 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1287 GFP_TEMPORARY);
1288 if (!pts)
1289 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001290
1291 *new_pds = pds;
1292 *new_pts = pts;
1293
1294 return 0;
1295
1296err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001297 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001298 return -ENOMEM;
1299}
1300
Michel Thierry762d9932015-07-30 11:05:29 +01001301static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1302 struct i915_page_directory_pointer *pdp,
1303 uint64_t start,
1304 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001305{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001306 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001307 unsigned long *new_page_dirs, *new_page_tables;
Chris Wilson49d73912016-11-29 09:50:08 +00001308 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001309 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001310 const uint64_t orig_start = start;
1311 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001312 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001313 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001314 int ret;
1315
Michel Thierry6ac18502015-07-29 17:23:46 +01001316 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001317 if (ret)
1318 return ret;
1319
Michel Thierryd7b26332015-04-08 12:13:34 +01001320 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001321 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1322 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001323 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001324 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001325 return ret;
1326 }
1327
1328 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001329 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001330 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001331 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001332 if (ret)
1333 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001334 }
1335
Michel Thierry33c88192015-04-08 12:13:33 +01001336 start = orig_start;
1337 length = orig_length;
1338
Michel Thierryd7b26332015-04-08 12:13:34 +01001339 /* Allocations have completed successfully, so set the bitmaps, and do
1340 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001341 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001342 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001343 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001344 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001345 uint64_t pd_start = start;
1346 uint32_t pde;
1347
Michel Thierryd7b26332015-04-08 12:13:34 +01001348 /* Every pd should be allocated, we just did that above. */
1349 WARN_ON(!pd);
1350
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001351 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001352 /* Same reasoning as pd */
1353 WARN_ON(!pt);
1354 WARN_ON(!pd_len);
1355 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1356
1357 /* Set our used ptes within the page table */
1358 bitmap_set(pt->used_ptes,
1359 gen8_pte_index(pd_start),
1360 gen8_pte_count(pd_start, pd_len));
1361
1362 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001363 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001364
1365 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001366 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1367 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001368 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1369 gen8_pte_index(start),
1370 gen8_pte_count(start, length),
1371 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001372
1373 /* NB: We haven't yet mapped ptes to pages. At this
1374 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001375 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001376
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001377 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001378 __set_bit(pdpe, pdp->used_pdpes);
Matthew Auld5c693b22016-12-13 16:05:10 +00001379 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001380 }
1381
Michał Winiarski3a41a052015-09-03 19:22:18 +02001382 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001383 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001384 return 0;
1385
1386err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001387 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001388 unsigned long temp;
1389
Michał Winiarski3a41a052015-09-03 19:22:18 +02001390 for_each_set_bit(temp, new_page_tables + pdpe *
1391 BITS_TO_LONGS(I915_PDES), I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001392 free_pt(dev_priv,
1393 pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001394 }
1395
Michel Thierry6ac18502015-07-29 17:23:46 +01001396 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001397 free_pd(dev_priv, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001398
Michał Winiarski3a41a052015-09-03 19:22:18 +02001399 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001400 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001401 return ret;
1402}
1403
Michel Thierry762d9932015-07-30 11:05:29 +01001404static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1405 struct i915_pml4 *pml4,
1406 uint64_t start,
1407 uint64_t length)
1408{
1409 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001410 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001411 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001412 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001413 int ret = 0;
1414
1415 /* Do the pml4 allocations first, so we don't need to track the newly
1416 * allocated tables below the pdp */
1417 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1418
1419 /* The pagedirectory and pagetable allocations are done in the shared 3
1420 * and 4 level code. Just allocate the pdps.
1421 */
1422 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1423 new_pdps);
1424 if (ret)
1425 return ret;
1426
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001427 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001428 WARN_ON(!pdp);
1429
1430 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1431 if (ret)
1432 goto err_out;
1433
Matthew Auld56843102016-12-13 16:05:11 +00001434 gen8_setup_pml4e(ppgtt, pml4, pdp, pml4e);
Michel Thierry762d9932015-07-30 11:05:29 +01001435 }
1436
1437 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1438 GEN8_PML4ES_PER_PML4);
1439
1440 return 0;
1441
1442err_out:
1443 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson49d73912016-11-29 09:50:08 +00001444 gen8_ppgtt_cleanup_3lvl(vm->i915, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001445
1446 return ret;
1447}
1448
1449static int gen8_alloc_va_range(struct i915_address_space *vm,
1450 uint64_t start, uint64_t length)
1451{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001452 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001453
Chris Wilsonc6385c92016-11-29 12:42:05 +00001454 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001455 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1456 else
1457 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1458}
1459
Michel Thierryea91e402015-07-29 17:23:57 +01001460static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1461 uint64_t start, uint64_t length,
1462 gen8_pte_t scratch_pte,
1463 struct seq_file *m)
1464{
1465 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001466 uint32_t pdpe;
1467
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001468 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001469 struct i915_page_table *pt;
1470 uint64_t pd_len = length;
1471 uint64_t pd_start = start;
1472 uint32_t pde;
1473
1474 if (!test_bit(pdpe, pdp->used_pdpes))
1475 continue;
1476
1477 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001478 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001479 uint32_t pte;
1480 gen8_pte_t *pt_vaddr;
1481
1482 if (!test_bit(pde, pd->used_pdes))
1483 continue;
1484
1485 pt_vaddr = kmap_px(pt);
1486 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1487 uint64_t va =
1488 (pdpe << GEN8_PDPE_SHIFT) |
1489 (pde << GEN8_PDE_SHIFT) |
1490 (pte << GEN8_PTE_SHIFT);
1491 int i;
1492 bool found = false;
1493
1494 for (i = 0; i < 4; i++)
1495 if (pt_vaddr[pte + i] != scratch_pte)
1496 found = true;
1497 if (!found)
1498 continue;
1499
1500 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1501 for (i = 0; i < 4; i++) {
1502 if (pt_vaddr[pte + i] != scratch_pte)
1503 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1504 else
1505 seq_puts(m, " SCRATCH ");
1506 }
1507 seq_puts(m, "\n");
1508 }
1509 /* don't use kunmap_px, it could trigger
1510 * an unnecessary flush.
1511 */
1512 kunmap_atomic(pt_vaddr);
1513 }
1514 }
1515}
1516
1517static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1518{
1519 struct i915_address_space *vm = &ppgtt->base;
1520 uint64_t start = ppgtt->base.start;
1521 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001522 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001523 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001524
Chris Wilsonc6385c92016-11-29 12:42:05 +00001525 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryea91e402015-07-29 17:23:57 +01001526 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1527 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001528 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001529 struct i915_pml4 *pml4 = &ppgtt->pml4;
1530 struct i915_page_directory_pointer *pdp;
1531
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001532 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001533 if (!test_bit(pml4e, pml4->used_pml4es))
1534 continue;
1535
1536 seq_printf(m, " PML4E #%llu\n", pml4e);
1537 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1538 }
1539 }
1540}
1541
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001542static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1543{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001544 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001545 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001546 int ret;
1547
1548 /* We allocate temp bitmap for page tables for no gain
1549 * but as this is for init only, lets keep the things simple
1550 */
1551 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1552 if (ret)
1553 return ret;
1554
1555 /* Allocate for all pdps regardless of how the ppgtt
1556 * was defined.
1557 */
1558 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1559 0, 1ULL << 32,
1560 new_page_dirs);
1561 if (!ret)
1562 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1563
Michał Winiarski3a41a052015-09-03 19:22:18 +02001564 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001565
1566 return ret;
1567}
1568
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001569/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001570 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1571 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1572 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1573 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001574 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001575 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001576static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001577{
Chris Wilson49d73912016-11-29 09:50:08 +00001578 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001579 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001580
Mika Kuoppala8776f022015-06-30 18:16:40 +03001581 ret = gen8_init_scratch(&ppgtt->base);
1582 if (ret)
1583 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001584
Michel Thierryd7b26332015-04-08 12:13:34 +01001585 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001586 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001587 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001588 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001589 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001590 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1591 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001592 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001593
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001594 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1595 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001596 if (ret)
1597 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001598
Michel Thierry69ab76f2015-07-29 17:23:55 +01001599 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1600
Michel Thierry762d9932015-07-30 11:05:29 +01001601 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001602 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001603 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001604 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001605 if (ret)
1606 goto free_scratch;
1607
1608 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001609 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001610 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1611 0, 0,
1612 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001613
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001614 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001615 ret = gen8_preallocate_top_level_pdps(ppgtt);
1616 if (ret)
1617 goto free_scratch;
1618 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001619 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001620
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001621 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001622 gen8_ppgtt_notify_vgt(ppgtt, true);
1623
Michel Thierryd7b26332015-04-08 12:13:34 +01001624 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001625
1626free_scratch:
1627 gen8_free_scratch(&ppgtt->base);
1628 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001629}
1630
Ben Widawsky87d60b62013-12-06 14:11:29 -08001631static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1632{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001633 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001634 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001635 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001636 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001637 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001638 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001639
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001640 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001641 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001642
Dave Gordon731f74c2016-06-24 19:37:46 +01001643 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001644 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001645 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001646 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001647 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001648 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1649
1650 if (pd_entry != expected)
1651 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1652 pde,
1653 pd_entry,
1654 expected);
1655 seq_printf(m, "\tPDE: %x\n", pd_entry);
1656
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001657 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1658
Michel Thierry07749ef2015-03-16 16:00:54 +00001659 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001660 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001661 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001662 (pte * PAGE_SIZE);
1663 int i;
1664 bool found = false;
1665 for (i = 0; i < 4; i++)
1666 if (pt_vaddr[pte + i] != scratch_pte)
1667 found = true;
1668 if (!found)
1669 continue;
1670
1671 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1672 for (i = 0; i < 4; i++) {
1673 if (pt_vaddr[pte + i] != scratch_pte)
1674 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1675 else
1676 seq_puts(m, " SCRATCH ");
1677 }
1678 seq_puts(m, "\n");
1679 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001680 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001681 }
1682}
1683
Ben Widawsky678d96f2015-03-16 16:00:56 +00001684/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001685static void gen6_write_pde(struct i915_page_directory *pd,
1686 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001687{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001688 /* Caller needs to make sure the write completes if necessary */
1689 struct i915_hw_ppgtt *ppgtt =
1690 container_of(pd, struct i915_hw_ppgtt, pd);
1691 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001692
Mika Kuoppala567047b2015-06-25 18:35:12 +03001693 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001694 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001695
Ben Widawsky678d96f2015-03-16 16:00:56 +00001696 writel(pd_entry, ppgtt->pd_addr + pde);
1697}
Ben Widawsky61973492013-04-08 18:43:54 -07001698
Ben Widawsky678d96f2015-03-16 16:00:56 +00001699/* Write all the page tables found in the ppgtt structure to incrementing page
1700 * directories. */
1701static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001702 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001703 uint32_t start, uint32_t length)
1704{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001705 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001706 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001707 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001708
Dave Gordon731f74c2016-06-24 19:37:46 +01001709 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001710 gen6_write_pde(pd, pde, pt);
1711
1712 /* Make sure write is complete before other code can use this page
1713 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001714 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001715}
1716
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001717static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001718{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001719 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001720
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001721 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001722}
Ben Widawsky61973492013-04-08 18:43:54 -07001723
Ben Widawsky90252e52013-12-06 14:11:12 -08001724static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001725 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001726{
Chris Wilson7e37f882016-08-02 22:50:21 +01001727 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001728 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001729 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001730
Ben Widawsky90252e52013-12-06 14:11:12 -08001731 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001732 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001733 if (ret)
1734 return ret;
1735
John Harrison5fb9de12015-05-29 17:44:07 +01001736 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001737 if (ret)
1738 return ret;
1739
Chris Wilsonb5321f32016-08-02 22:50:18 +01001740 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1741 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1742 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1743 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1744 intel_ring_emit(ring, get_pd_offset(ppgtt));
1745 intel_ring_emit(ring, MI_NOOP);
1746 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001747
1748 return 0;
1749}
1750
Ben Widawsky48a10382013-12-06 14:11:11 -08001751static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001752 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001753{
Chris Wilson7e37f882016-08-02 22:50:21 +01001754 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001755 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001756 int ret;
1757
Ben Widawsky48a10382013-12-06 14:11:11 -08001758 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001759 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001760 if (ret)
1761 return ret;
1762
John Harrison5fb9de12015-05-29 17:44:07 +01001763 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001764 if (ret)
1765 return ret;
1766
Chris Wilsonb5321f32016-08-02 22:50:18 +01001767 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1768 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1769 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1770 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1771 intel_ring_emit(ring, get_pd_offset(ppgtt));
1772 intel_ring_emit(ring, MI_NOOP);
1773 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001774
Ben Widawsky90252e52013-12-06 14:11:12 -08001775 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001776 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001777 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001778 if (ret)
1779 return ret;
1780 }
1781
Ben Widawsky48a10382013-12-06 14:11:11 -08001782 return 0;
1783}
1784
Ben Widawskyeeb94882013-12-06 14:11:10 -08001785static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001786 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001787{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001788 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001789 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001790
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001791 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1792 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001793 return 0;
1794}
1795
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001796static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001797{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001798 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301799 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001800
Akash Goel3b3f1652016-10-13 22:44:48 +05301801 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001802 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1803 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001804 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001805 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001806 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001807}
1808
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001809static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001810{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001811 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001812 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301813 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001814
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001815 ecobits = I915_READ(GAC_ECO_BITS);
1816 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1817
1818 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001819 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001820 ecochk |= ECOCHK_PPGTT_WB_HSW;
1821 } else {
1822 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1823 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1824 }
1825 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001826
Akash Goel3b3f1652016-10-13 22:44:48 +05301827 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001828 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001829 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001830 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001831 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001832}
1833
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001834static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001835{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001836 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001837
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001838 ecobits = I915_READ(GAC_ECO_BITS);
1839 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1840 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001841
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001842 gab_ctl = I915_READ(GAB_CTL);
1843 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001844
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001845 ecochk = I915_READ(GAM_ECOCHK);
1846 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001847
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001848 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001849}
1850
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001851/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001852static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001853 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001854 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001855{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001856 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001857 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001858 unsigned first_entry = start >> PAGE_SHIFT;
1859 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001860 unsigned act_pt = first_entry / GEN6_PTES;
1861 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001862 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001863
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001864 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001865 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001866
Daniel Vetter7bddb012012-02-09 17:15:47 +01001867 while (num_entries) {
1868 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001869 if (last_pte > GEN6_PTES)
1870 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001871
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001872 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001873
1874 for (i = first_pte; i < last_pte; i++)
1875 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001876
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001877 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001878
Daniel Vetter7bddb012012-02-09 17:15:47 +01001879 num_entries -= last_pte - first_pte;
1880 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001881 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001882 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001883}
1884
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001885static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001886 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001887 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301888 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001889{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001890 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001891 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001892 unsigned act_pt = first_entry / GEN6_PTES;
1893 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001894 gen6_pte_t *pt_vaddr = NULL;
1895 struct sgt_iter sgt_iter;
1896 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001897
Dave Gordon85d12252016-05-20 11:54:06 +01001898 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001899 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001900 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001901
Chris Wilsoncc797142013-12-31 15:50:30 +00001902 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001903 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301904
Michel Thierry07749ef2015-03-16 16:00:54 +00001905 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001906 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001907 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001908 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001909 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001910 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001911 }
Dave Gordon85d12252016-05-20 11:54:06 +01001912
Chris Wilsoncc797142013-12-31 15:50:30 +00001913 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001914 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001915}
1916
Ben Widawsky678d96f2015-03-16 16:00:56 +00001917static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001918 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001919{
Michel Thierry4933d512015-03-24 15:46:22 +00001920 DECLARE_BITMAP(new_page_tables, I915_PDES);
Chris Wilson49d73912016-11-29 09:50:08 +00001921 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001922 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001923 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001924 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001925 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001926 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001927 int ret;
1928
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001929 start = start_save = start_in;
1930 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001931
1932 bitmap_zero(new_page_tables, I915_PDES);
1933
1934 /* The allocation is done in two stages so that we can bail out with
1935 * minimal amount of pain. The first stage finds new page tables that
1936 * need allocation. The second stage marks use ptes within the page
1937 * tables.
1938 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001939 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001940 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001941 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1942 continue;
1943 }
1944
1945 /* We've already allocated a page table */
1946 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1947
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001948 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001949 if (IS_ERR(pt)) {
1950 ret = PTR_ERR(pt);
1951 goto unwind_out;
1952 }
1953
1954 gen6_initialize_pt(vm, pt);
1955
1956 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001957 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001958 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001959 }
1960
1961 start = start_save;
1962 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001963
Dave Gordon731f74c2016-06-24 19:37:46 +01001964 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001965 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1966
1967 bitmap_zero(tmp_bitmap, GEN6_PTES);
1968 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1969 gen6_pte_count(start, length));
1970
Mika Kuoppala966082c2015-06-25 18:35:19 +03001971 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001972 gen6_write_pde(&ppgtt->pd, pde, pt);
1973
Michel Thierry72744cb2015-03-24 15:46:23 +00001974 trace_i915_page_table_entry_map(vm, pde, pt,
1975 gen6_pte_index(start),
1976 gen6_pte_count(start, length),
1977 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001978 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001979 GEN6_PTES);
1980 }
1981
Michel Thierry4933d512015-03-24 15:46:22 +00001982 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1983
1984 /* Make sure write is complete before other code can use this page
1985 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001986 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001987
Ben Widawsky563222a2015-03-19 12:53:28 +00001988 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001989 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001990
1991unwind_out:
1992 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001993 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00001994
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001995 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001996 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001997 }
1998
1999 mark_tlbs_dirty(ppgtt);
2000 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002001}
2002
Mika Kuoppala8776f022015-06-30 18:16:40 +03002003static int gen6_init_scratch(struct i915_address_space *vm)
2004{
Chris Wilson49d73912016-11-29 09:50:08 +00002005 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002006 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002007
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002008 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002009 if (ret)
2010 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002011
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002012 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002013 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002014 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002015 return PTR_ERR(vm->scratch_pt);
2016 }
2017
2018 gen6_initialize_pt(vm, vm->scratch_pt);
2019
2020 return 0;
2021}
2022
2023static void gen6_free_scratch(struct i915_address_space *vm)
2024{
Chris Wilson49d73912016-11-29 09:50:08 +00002025 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002026
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002027 free_pt(dev_priv, vm->scratch_pt);
2028 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002029}
2030
Daniel Vetter061dd492015-04-14 17:35:13 +02002031static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002032{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002033 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002034 struct i915_page_directory *pd = &ppgtt->pd;
Chris Wilson49d73912016-11-29 09:50:08 +00002035 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry09942c62015-04-08 12:13:30 +01002036 struct i915_page_table *pt;
2037 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002038
Daniel Vetter061dd492015-04-14 17:35:13 +02002039 drm_mm_remove_node(&ppgtt->node);
2040
Dave Gordon731f74c2016-06-24 19:37:46 +01002041 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002042 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002043 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002044
Mika Kuoppala8776f022015-06-30 18:16:40 +03002045 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002046}
2047
Ben Widawskyb1465202014-02-19 22:05:49 -08002048static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002049{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002050 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00002051 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002052 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002053 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002054
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002055 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2056 * allocator works in address space sizes, so it's multiplied by page
2057 * size. We allocate at the top of the GTT to avoid fragmentation.
2058 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002059 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002060
Mika Kuoppala8776f022015-06-30 18:16:40 +03002061 ret = gen6_init_scratch(vm);
2062 if (ret)
2063 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002064
Chris Wilsone007b192017-01-11 11:23:10 +00002065 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2066 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2067 I915_COLOR_UNEVICTABLE,
2068 0, ggtt->base.total,
2069 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00002070 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002071 goto err_out;
2072
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002073 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002074 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002075
Ben Widawskyc8c26622015-01-22 17:01:25 +00002076 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002077
2078err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002079 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002080 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002081}
2082
Ben Widawskyb1465202014-02-19 22:05:49 -08002083static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2084{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002085 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002086}
2087
Michel Thierry4933d512015-03-24 15:46:22 +00002088static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2089 uint64_t start, uint64_t length)
2090{
Michel Thierryec565b32015-04-08 12:13:23 +01002091 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002092 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002093
Dave Gordon731f74c2016-06-24 19:37:46 +01002094 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002095 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002096}
2097
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002098static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002099{
Chris Wilson49d73912016-11-29 09:50:08 +00002100 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002101 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002102 int ret;
2103
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002104 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002105 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002106 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002107 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002108 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002109 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002110 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002111 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002112 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002113
2114 ret = gen6_ppgtt_alloc(ppgtt);
2115 if (ret)
2116 return ret;
2117
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002118 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002119 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2120 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002121 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2122 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002123 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002124 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002125 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002126 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002127
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002128 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002129 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002130
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002131 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002132 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002133
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002134 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002135
Ben Widawsky678d96f2015-03-16 16:00:56 +00002136 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2137
Thierry Reding440fd522015-01-23 09:05:06 +01002138 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002139 ppgtt->node.size >> 20,
2140 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002141
Daniel Vetterfa76da32014-08-06 20:19:54 +02002142 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002143 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002144
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002145 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002146}
2147
Chris Wilson2bfa9962016-08-04 07:52:25 +01002148static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2149 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002150{
Chris Wilson49d73912016-11-29 09:50:08 +00002151 ppgtt->base.i915 = dev_priv;
Daniel Vetter3440d262013-01-24 13:49:56 -08002152
Chris Wilson2bfa9962016-08-04 07:52:25 +01002153 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002154 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002155 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002156 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002157}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002158
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002159static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002160 struct drm_i915_private *dev_priv,
2161 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002162{
Chris Wilson80b204b2016-10-28 13:58:58 +01002163 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00002164
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002165 drm_mm_init(&vm->mm, vm->start, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00002166 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
2167
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002168 INIT_LIST_HEAD(&vm->active_list);
2169 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002170 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00002171
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002172 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2173}
2174
Matthew Aulded9724d2016-11-17 21:04:10 +00002175static void i915_address_space_fini(struct i915_address_space *vm)
2176{
2177 i915_gem_timeline_fini(&vm->timeline);
2178 drm_mm_takedown(&vm->mm);
2179 list_del(&vm->global_link);
2180}
2181
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002182static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002183{
Tim Gored5165eb2016-02-04 11:49:34 +00002184 /* This function is for gtt related workarounds. This function is
2185 * called on driver load and after a GPU reset, so you can place
2186 * workarounds here even if they get overwritten by GPU reset.
2187 */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002188 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002189 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002190 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002191 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002192 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08002193 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002194 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002195 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002196 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2197}
2198
Chris Wilson2bfa9962016-08-04 07:52:25 +01002199static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2200 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002201 struct drm_i915_file_private *file_priv,
2202 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002203{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002204 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002205
Chris Wilson2bfa9962016-08-04 07:52:25 +01002206 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002207 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002208 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002209 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002210 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002211 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002212
2213 return ret;
2214}
2215
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002216int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002217{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002218 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002219
Thomas Daniel671b50132014-08-20 16:24:50 +01002220 /* In the case of execlists, PPGTT is enabled by the context descriptor
2221 * and the PDPs are contained within the context itself. We don't
2222 * need to do anything here. */
2223 if (i915.enable_execlists)
2224 return 0;
2225
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002226 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002227 return 0;
2228
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002229 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002230 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002231 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002232 gen7_ppgtt_enable(dev_priv);
2233 else if (INTEL_GEN(dev_priv) >= 8)
2234 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002235 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002236 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002237
John Harrison4ad2fd82015-06-18 13:11:20 +01002238 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002239}
John Harrison4ad2fd82015-06-18 13:11:20 +01002240
Daniel Vetter4d884702014-08-06 15:04:47 +02002241struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002242i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002243 struct drm_i915_file_private *fpriv,
2244 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002245{
2246 struct i915_hw_ppgtt *ppgtt;
2247 int ret;
2248
2249 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2250 if (!ppgtt)
2251 return ERR_PTR(-ENOMEM);
2252
Chris Wilson80b204b2016-10-28 13:58:58 +01002253 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002254 if (ret) {
2255 kfree(ppgtt);
2256 return ERR_PTR(ret);
2257 }
2258
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002259 trace_i915_ppgtt_create(&ppgtt->base);
2260
Daniel Vetter4d884702014-08-06 15:04:47 +02002261 return ppgtt;
2262}
2263
Chris Wilson0c7eeda2017-01-11 21:09:25 +00002264void i915_ppgtt_close(struct i915_address_space *vm)
2265{
2266 struct list_head *phases[] = {
2267 &vm->active_list,
2268 &vm->inactive_list,
2269 &vm->unbound_list,
2270 NULL,
2271 }, **phase;
2272
2273 GEM_BUG_ON(vm->closed);
2274 vm->closed = true;
2275
2276 for (phase = phases; *phase; phase++) {
2277 struct i915_vma *vma, *vn;
2278
2279 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2280 if (!i915_vma_is_closed(vma))
2281 i915_vma_close(vma);
2282 }
2283}
2284
Matthew Aulded9724d2016-11-17 21:04:10 +00002285void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002286{
2287 struct i915_hw_ppgtt *ppgtt =
2288 container_of(kref, struct i915_hw_ppgtt, ref);
2289
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002290 trace_i915_ppgtt_release(&ppgtt->base);
2291
Chris Wilson50e046b2016-08-04 07:52:46 +01002292 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002293 WARN_ON(!list_empty(&ppgtt->base.active_list));
2294 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002295 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002296
Matthew Aulded9724d2016-11-17 21:04:10 +00002297 i915_address_space_fini(&ppgtt->base);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002298
Daniel Vetteree960be2014-08-06 15:04:45 +02002299 ppgtt->base.cleanup(&ppgtt->base);
2300 kfree(ppgtt);
2301}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002302
Ben Widawskya81cc002013-01-18 12:30:31 -08002303/* Certain Gen5 chipsets require require idling the GPU before
2304 * unmapping anything from the GTT when VT-d is enabled.
2305 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002306static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002307{
2308#ifdef CONFIG_INTEL_IOMMU
2309 /* Query intel_iommu to see if we need the workaround. Presumably that
2310 * was loaded first.
2311 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002312 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002313 return true;
2314#endif
2315 return false;
2316}
2317
Chris Wilsondc979972016-05-10 14:10:04 +01002318void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002319{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002320 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302321 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002322
Chris Wilsondc979972016-05-10 14:10:04 +01002323 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002324 return;
2325
Akash Goel3b3f1652016-10-13 22:44:48 +05302326 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002327 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002328 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002329 if (fault_reg & RING_FAULT_VALID) {
2330 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002331 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002332 "\tAddress space: %s\n"
2333 "\tSource ID: %d\n"
2334 "\tType: %d\n",
2335 fault_reg & PAGE_MASK,
2336 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2337 RING_FAULT_SRCID(fault_reg),
2338 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002339 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002340 fault_reg & ~RING_FAULT_VALID);
2341 }
2342 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302343
2344 /* Engine specific init may not have been done till this point. */
2345 if (dev_priv->engine[RCS])
2346 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002347}
2348
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002349void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002350{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002351 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002352
2353 /* Don't bother messing with faults pre GEN6 as we have little
2354 * documentation supporting that it's a good idea.
2355 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002356 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002357 return;
2358
Chris Wilsondc979972016-05-10 14:10:04 +01002359 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002360
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002361 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002362
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002363 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002364}
2365
Chris Wilson03ac84f2016-10-28 13:58:36 +01002366int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2367 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002368{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002369 do {
2370 if (dma_map_sg(&obj->base.dev->pdev->dev,
2371 pages->sgl, pages->nents,
2372 PCI_DMA_BIDIRECTIONAL))
2373 return 0;
2374
2375 /* If the DMA remap fails, one cause can be that we have
2376 * too many objects pinned in a small remapping table,
2377 * such as swiotlb. Incrementally purge all other objects and
2378 * try again - if there are no more pages to remove from
2379 * the DMA remapper, i915_gem_shrink will return 0.
2380 */
2381 GEM_BUG_ON(obj->mm.pages == pages);
2382 } while (i915_gem_shrink(to_i915(obj->base.dev),
2383 obj->base.size >> PAGE_SHIFT,
2384 I915_SHRINK_BOUND |
2385 I915_SHRINK_UNBOUND |
2386 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002387
Chris Wilson03ac84f2016-10-28 13:58:36 +01002388 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002389}
2390
Daniel Vetter2c642b02015-04-14 17:35:26 +02002391static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002392{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002393 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002394}
2395
Chris Wilsond6473f52016-06-10 14:22:59 +05302396static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2397 dma_addr_t addr,
2398 uint64_t offset,
2399 enum i915_cache_level level,
2400 u32 unused)
2401{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002402 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302403 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002404 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302405
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002406 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302407
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002408 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302409}
2410
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002411static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2412 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002413 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302414 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002415{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002416 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002417 struct sgt_iter sgt_iter;
2418 gen8_pte_t __iomem *gtt_entries;
2419 gen8_pte_t gtt_entry;
2420 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002421 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002422
Dave Gordon85d12252016-05-20 11:54:06 +01002423 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2424
2425 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002426 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002427 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002428 }
2429
2430 /*
2431 * XXX: This serves as a posting read to make sure that the PTE has
2432 * actually been updated. There is some concern that even though
2433 * registers and PTEs are within the same BAR that they are potentially
2434 * of NUMA access patterns. Therefore, even with the way we assume
2435 * hardware should work, we must keep this posting read for paranoia.
2436 */
2437 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002438 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002439
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002440 /* This next bit makes the above posting read even more important. We
2441 * want to flush the TLBs only after we're certain all the PTE updates
2442 * have finished.
2443 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002444 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002445}
2446
Chris Wilsonc1403302015-11-18 15:19:39 +00002447struct insert_entries {
2448 struct i915_address_space *vm;
2449 struct sg_table *st;
2450 uint64_t start;
2451 enum i915_cache_level level;
2452 u32 flags;
2453};
2454
2455static int gen8_ggtt_insert_entries__cb(void *_arg)
2456{
2457 struct insert_entries *arg = _arg;
2458 gen8_ggtt_insert_entries(arg->vm, arg->st,
2459 arg->start, arg->level, arg->flags);
2460 return 0;
2461}
2462
2463static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2464 struct sg_table *st,
2465 uint64_t start,
2466 enum i915_cache_level level,
2467 u32 flags)
2468{
2469 struct insert_entries arg = { vm, st, start, level, flags };
2470 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2471}
2472
Chris Wilsond6473f52016-06-10 14:22:59 +05302473static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2474 dma_addr_t addr,
2475 uint64_t offset,
2476 enum i915_cache_level level,
2477 u32 flags)
2478{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002479 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302480 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002481 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302482
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002483 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302484
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002485 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302486}
2487
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002488/*
2489 * Binds an object into the global gtt with the specified cache level. The object
2490 * will be accessible to the GPU via commands whose operands reference offsets
2491 * within the global GTT as well as accessible by the GPU through the GMADR
2492 * mapped BAR (dev_priv->mm.gtt->gtt).
2493 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002494static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002495 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002496 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302497 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002498{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002499 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002500 struct sgt_iter sgt_iter;
2501 gen6_pte_t __iomem *gtt_entries;
2502 gen6_pte_t gtt_entry;
2503 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002504 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002505
Dave Gordon85d12252016-05-20 11:54:06 +01002506 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2507
2508 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002509 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002510 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002511 }
2512
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002513 /* XXX: This serves as a posting read to make sure that the PTE has
2514 * actually been updated. There is some concern that even though
2515 * registers and PTEs are within the same BAR that they are potentially
2516 * of NUMA access patterns. Therefore, even with the way we assume
2517 * hardware should work, we must keep this posting read for paranoia.
2518 */
Dave Gordon85d12252016-05-20 11:54:06 +01002519 if (i != 0)
2520 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002521
2522 /* This next bit makes the above posting read even more important. We
2523 * want to flush the TLBs only after we're certain all the PTE updates
2524 * have finished.
2525 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002526 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002527}
2528
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002529static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002530 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002531{
2532}
2533
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002534static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002535 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002536{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002537 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002538 unsigned first_entry = start >> PAGE_SHIFT;
2539 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002540 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002541 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2542 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002543 int i;
2544
2545 if (WARN(num_entries > max_entries,
2546 "First entry = %d; Num entries = %d (max=%d)\n",
2547 first_entry, num_entries, max_entries))
2548 num_entries = max_entries;
2549
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002550 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002551 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002552 for (i = 0; i < num_entries; i++)
2553 gen8_set_pte(&gtt_base[i], scratch_pte);
2554 readl(gtt_base);
2555}
2556
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002557static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002558 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002559 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002560{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002561 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002562 unsigned first_entry = start >> PAGE_SHIFT;
2563 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002564 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002565 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2566 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002567 int i;
2568
2569 if (WARN(num_entries > max_entries,
2570 "First entry = %d; Num entries = %d (max=%d)\n",
2571 first_entry, num_entries, max_entries))
2572 num_entries = max_entries;
2573
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002574 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002575 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002576
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002577 for (i = 0; i < num_entries; i++)
2578 iowrite32(scratch_pte, &gtt_base[i]);
2579 readl(gtt_base);
2580}
2581
Chris Wilsond6473f52016-06-10 14:22:59 +05302582static void i915_ggtt_insert_page(struct i915_address_space *vm,
2583 dma_addr_t addr,
2584 uint64_t offset,
2585 enum i915_cache_level cache_level,
2586 u32 unused)
2587{
Chris Wilsond6473f52016-06-10 14:22:59 +05302588 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2589 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302590
2591 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302592}
2593
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002594static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2595 struct sg_table *pages,
2596 uint64_t start,
2597 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002598{
2599 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2600 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2601
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002602 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002603
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002604}
2605
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002606static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002607 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002608 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002609{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002610 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002611}
2612
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002613static int ggtt_bind_vma(struct i915_vma *vma,
2614 enum i915_cache_level cache_level,
2615 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002616{
Chris Wilson49d73912016-11-29 09:50:08 +00002617 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002618 struct drm_i915_gem_object *obj = vma->obj;
2619 u32 pte_flags = 0;
2620 int ret;
2621
2622 ret = i915_get_ggtt_vma_pages(vma);
2623 if (ret)
2624 return ret;
2625
2626 /* Currently applicable only to VLV */
2627 if (obj->gt_ro)
2628 pte_flags |= PTE_READ_ONLY;
2629
Chris Wilson9c870d02016-10-24 13:42:15 +01002630 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002631 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002632 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002633 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002634
2635 /*
2636 * Without aliasing PPGTT there's no difference between
2637 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2638 * upgrade to both bound if we bind either to avoid double-binding.
2639 */
Chris Wilson3272db52016-08-04 16:32:32 +01002640 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002641
2642 return 0;
2643}
2644
2645static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2646 enum i915_cache_level cache_level,
2647 u32 flags)
2648{
Chris Wilson49d73912016-11-29 09:50:08 +00002649 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002650 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002651 int ret;
2652
2653 ret = i915_get_ggtt_vma_pages(vma);
2654 if (ret)
2655 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002656
Akash Goel24f3a8c2014-06-17 10:59:42 +05302657 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002658 pte_flags = 0;
2659 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002660 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302661
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002662
Chris Wilson3272db52016-08-04 16:32:32 +01002663 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002664 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002665 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002666 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002667 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002668 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002669 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002670
Chris Wilson3272db52016-08-04 16:32:32 +01002671 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002672 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002673 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002674 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002675 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002676 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002677
2678 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002679}
2680
2681static void ggtt_unbind_vma(struct i915_vma *vma)
2682{
Chris Wilson49d73912016-11-29 09:50:08 +00002683 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson9c870d02016-10-24 13:42:15 +01002684 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002685 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002686
Chris Wilson9c870d02016-10-24 13:42:15 +01002687 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2688 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002689 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002690 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002691 intel_runtime_pm_put(i915);
2692 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002693
Chris Wilson3272db52016-08-04 16:32:32 +01002694 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002695 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002696 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002697}
2698
Chris Wilson03ac84f2016-10-28 13:58:36 +01002699void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2700 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002701{
David Weinehall52a05c32016-08-22 13:32:44 +03002702 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2703 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002704 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002705
Chris Wilson307dc252016-08-05 10:14:12 +01002706 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002707 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002708 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2709 /* Wait a bit, in hopes it avoids the hang */
2710 udelay(10);
2711 }
2712 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002713
Chris Wilson03ac84f2016-10-28 13:58:36 +01002714 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002715}
Daniel Vetter644ec022012-03-26 09:45:40 +02002716
Chris Wilson45b186f2016-12-16 07:46:42 +00002717static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002718 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002719 u64 *start,
2720 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002721{
Chris Wilsona6508de2017-02-06 08:45:47 +00002722 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002723 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002724
Chris Wilsona6508de2017-02-06 08:45:47 +00002725 /* Also leave a space between the unallocated reserved node after the
2726 * GTT and any objects within the GTT, i.e. we use the color adjustment
2727 * to insert a guard page to prevent prefetches crossing over the
2728 * GTT boundary.
2729 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002730 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002731 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002732 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002733}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002734
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002735int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002736{
Ben Widawskye78891c2013-01-25 16:41:04 -08002737 /* Let GEM Manage all of the aperture.
2738 *
2739 * However, leave one page at the end still bound to the scratch page.
2740 * There are a number of places where the hardware apparently prefetches
2741 * past the end of the object, and we've seen multiple hangs with the
2742 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2743 * aperture. One page should be enough to keep any prefetching inside
2744 * of the aperture.
2745 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002746 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002747 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002748 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002749 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002750 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002751
Zhi Wangb02d22a2016-06-16 08:06:59 -04002752 ret = intel_vgt_balloon(dev_priv);
2753 if (ret)
2754 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002755
Chris Wilson95374d72016-10-12 10:05:20 +01002756 /* Reserve a mappable slot for our lockless error capture */
Chris Wilson4e64e552017-02-02 21:04:38 +00002757 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2758 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2759 0, ggtt->mappable_end,
2760 DRM_MM_INSERT_LOW);
Chris Wilson95374d72016-10-12 10:05:20 +01002761 if (ret)
2762 return ret;
2763
Chris Wilsoned2f3452012-11-15 11:32:19 +00002764 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002765 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002766 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2767 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002768 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002769 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002770 }
2771
2772 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002773 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002774 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002775
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002776 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002777 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002778 if (!ppgtt) {
2779 ret = -ENOMEM;
2780 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002781 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002782
Chris Wilson95374d72016-10-12 10:05:20 +01002783 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2784 if (ret)
2785 goto err_ppgtt;
2786
2787 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002788 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2789 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002790 if (ret)
2791 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002792 }
2793
2794 ppgtt->base.clear_range(&ppgtt->base,
2795 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002796 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002797
Daniel Vetterfa76da32014-08-06 20:19:54 +02002798 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002799 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2800 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002801 }
2802
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002803 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002804
2805err_ppgtt_cleanup:
2806 ppgtt->base.cleanup(&ppgtt->base);
2807err_ppgtt:
2808 kfree(ppgtt);
2809err:
2810 drm_mm_remove_node(&ggtt->error_capture);
2811 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002812}
2813
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002814/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002815 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002816 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002817 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002818void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002819{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002820 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson94d4a2a2017-02-10 16:35:22 +00002821 struct i915_vma *vma, *vn;
2822
2823 ggtt->base.closed = true;
2824
2825 mutex_lock(&dev_priv->drm.struct_mutex);
2826 WARN_ON(!list_empty(&ggtt->base.active_list));
2827 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2828 WARN_ON(i915_vma_unbind(vma));
2829 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002830
Daniel Vetter70e32542014-08-06 15:04:57 +02002831 if (dev_priv->mm.aliasing_ppgtt) {
2832 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002833 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002834 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002835 }
2836
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002837 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002838
Chris Wilson95374d72016-10-12 10:05:20 +01002839 if (drm_mm_node_allocated(&ggtt->error_capture))
2840 drm_mm_remove_node(&ggtt->error_capture);
2841
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002842 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002843 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002844
Matthew Aulded9724d2016-11-17 21:04:10 +00002845 mutex_lock(&dev_priv->drm.struct_mutex);
2846 i915_address_space_fini(&ggtt->base);
2847 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002848 }
2849
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002850 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002851
2852 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002853 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002854}
Daniel Vetter70e32542014-08-06 15:04:57 +02002855
Daniel Vetter2c642b02015-04-14 17:35:26 +02002856static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002857{
2858 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2859 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2860 return snb_gmch_ctl << 20;
2861}
2862
Daniel Vetter2c642b02015-04-14 17:35:26 +02002863static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002864{
2865 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2866 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2867 if (bdw_gmch_ctl)
2868 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002869
2870#ifdef CONFIG_X86_32
2871 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2872 if (bdw_gmch_ctl > 4)
2873 bdw_gmch_ctl = 4;
2874#endif
2875
Ben Widawsky9459d252013-11-03 16:53:55 -08002876 return bdw_gmch_ctl << 20;
2877}
2878
Daniel Vetter2c642b02015-04-14 17:35:26 +02002879static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002880{
2881 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2882 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2883
2884 if (gmch_ctrl)
2885 return 1 << (20 + gmch_ctrl);
2886
2887 return 0;
2888}
2889
Daniel Vetter2c642b02015-04-14 17:35:26 +02002890static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002891{
2892 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2893 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2894 return snb_gmch_ctl << 25; /* 32 MB units */
2895}
2896
Daniel Vetter2c642b02015-04-14 17:35:26 +02002897static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002898{
2899 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2900 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2901 return bdw_gmch_ctl << 25; /* 32 MB units */
2902}
2903
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002904static size_t chv_get_stolen_size(u16 gmch_ctrl)
2905{
2906 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2907 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2908
2909 /*
2910 * 0x0 to 0x10: 32MB increments starting at 0MB
2911 * 0x11 to 0x16: 4MB increments starting at 8MB
2912 * 0x17 to 0x1d: 4MB increments start at 36MB
2913 */
2914 if (gmch_ctrl < 0x11)
2915 return gmch_ctrl << 25;
2916 else if (gmch_ctrl < 0x17)
2917 return (gmch_ctrl - 0x11 + 2) << 22;
2918 else
2919 return (gmch_ctrl - 0x17 + 9) << 22;
2920}
2921
Damien Lespiau66375012014-01-09 18:02:46 +00002922static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2923{
2924 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2925 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2926
2927 if (gen9_gmch_ctl < 0xf0)
2928 return gen9_gmch_ctl << 25; /* 32 MB units */
2929 else
2930 /* 4MB increments starting at 0xf0 for 4MB */
2931 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2932}
2933
Chris Wilson34c998b2016-08-04 07:52:24 +01002934static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002935{
Chris Wilson49d73912016-11-29 09:50:08 +00002936 struct drm_i915_private *dev_priv = ggtt->base.i915;
2937 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002938 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002939 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002940
2941 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002942 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002943
Imre Deak2a073f892015-03-27 13:07:33 +02002944 /*
2945 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2946 * dropped. For WC mappings in general we have 64 byte burst writes
2947 * when the WC buffer is flushed, so we can't use it, but have to
2948 * resort to an uncached mapping. The WC issue is easily caught by the
2949 * readback check when writing GTT PTE entries.
2950 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002951 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002952 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002953 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002954 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002955 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002956 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002957 return -ENOMEM;
2958 }
2959
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002960 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002961 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002962 DRM_ERROR("Scratch setup failed\n");
2963 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002964 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002965 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002966 }
2967
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002968 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002969}
2970
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002971/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2972 * bits. When using advanced contexts each context stores its own PAT, but
2973 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002974static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002975{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002976 uint64_t pat;
2977
2978 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2979 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2980 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2981 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2982 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2983 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2984 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2985 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2986
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002987 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002988 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2989 * so RTL will always use the value corresponding to
2990 * pat_sel = 000".
2991 * So let's disable cache for GGTT to avoid screen corruptions.
2992 * MOCS still can be used though.
2993 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2994 * before this patch, i.e. the same uncached + snooping access
2995 * like on gen6/7 seems to be in effect.
2996 * - So this just fixes blitter/render access. Again it looks
2997 * like it's not just uncached access, but uncached + snooping.
2998 * So we can still hold onto all our assumptions wrt cpu
2999 * clflushing on LLC machines.
3000 */
3001 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3002
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003003 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3004 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003005 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3006 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003007}
3008
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003009static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3010{
3011 uint64_t pat;
3012
3013 /*
3014 * Map WB on BDW to snooped on CHV.
3015 *
3016 * Only the snoop bit has meaning for CHV, the rest is
3017 * ignored.
3018 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003019 * The hardware will never snoop for certain types of accesses:
3020 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3021 * - PPGTT page tables
3022 * - some other special cycles
3023 *
3024 * As with BDW, we also need to consider the following for GT accesses:
3025 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3026 * so RTL will always use the value corresponding to
3027 * pat_sel = 000".
3028 * Which means we must set the snoop bit in PAT entry 0
3029 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003030 */
3031 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3032 GEN8_PPAT(1, 0) |
3033 GEN8_PPAT(2, 0) |
3034 GEN8_PPAT(3, 0) |
3035 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3036 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3037 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3038 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3039
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003040 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3041 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003042}
3043
Chris Wilson34c998b2016-08-04 07:52:24 +01003044static void gen6_gmch_remove(struct i915_address_space *vm)
3045{
3046 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3047
3048 iounmap(ggtt->gsm);
Chris Wilson49d73912016-11-29 09:50:08 +00003049 cleanup_scratch_page(vm->i915, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003050}
3051
Joonas Lahtinend507d732016-03-18 10:42:58 +02003052static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003053{
Chris Wilson49d73912016-11-29 09:50:08 +00003054 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003055 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003056 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003057 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003058
3059 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003060 ggtt->mappable_base = pci_resource_start(pdev, 2);
3061 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003062
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003063 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3064 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003065
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003066 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003067
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003068 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003069 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003070 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003071 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003072 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003073 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003074 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003075 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003076 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003077 }
Ben Widawsky63340132013-11-04 19:32:22 -08003078
Chris Wilson34c998b2016-08-04 07:52:24 +01003079 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003080
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003081 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003082 chv_setup_private_ppat(dev_priv);
3083 else
3084 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003085
Chris Wilson34c998b2016-08-04 07:52:24 +01003086 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003087 ggtt->base.bind_vma = ggtt_bind_vma;
3088 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303089 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003090 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003091 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003092 ggtt->base.clear_range = gen8_ggtt_clear_range;
3093
3094 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3095 if (IS_CHERRYVIEW(dev_priv))
3096 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3097
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003098 ggtt->invalidate = gen6_ggtt_invalidate;
3099
Chris Wilson34c998b2016-08-04 07:52:24 +01003100 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003101}
3102
Joonas Lahtinend507d732016-03-18 10:42:58 +02003103static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003104{
Chris Wilson49d73912016-11-29 09:50:08 +00003105 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003106 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003107 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003108 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003109
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003110 ggtt->mappable_base = pci_resource_start(pdev, 2);
3111 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003112
Ben Widawskybaa09f52013-01-24 13:49:57 -08003113 /* 64/512MB is the current min/max we actually know of, but this is just
3114 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003115 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003116 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003117 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003118 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003119 }
3120
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003121 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3122 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3123 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003124
Joonas Lahtinend507d732016-03-18 10:42:58 +02003125 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003126
Chris Wilson34c998b2016-08-04 07:52:24 +01003127 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3128 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003129
Joonas Lahtinend507d732016-03-18 10:42:58 +02003130 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303131 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003132 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3133 ggtt->base.bind_vma = ggtt_bind_vma;
3134 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003135 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003136
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003137 ggtt->invalidate = gen6_ggtt_invalidate;
3138
Chris Wilson34c998b2016-08-04 07:52:24 +01003139 if (HAS_EDRAM(dev_priv))
3140 ggtt->base.pte_encode = iris_pte_encode;
3141 else if (IS_HASWELL(dev_priv))
3142 ggtt->base.pte_encode = hsw_pte_encode;
3143 else if (IS_VALLEYVIEW(dev_priv))
3144 ggtt->base.pte_encode = byt_pte_encode;
3145 else if (INTEL_GEN(dev_priv) >= 7)
3146 ggtt->base.pte_encode = ivb_pte_encode;
3147 else
3148 ggtt->base.pte_encode = snb_pte_encode;
3149
3150 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003151}
3152
Chris Wilson34c998b2016-08-04 07:52:24 +01003153static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003154{
Chris Wilson34c998b2016-08-04 07:52:24 +01003155 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003156}
3157
Joonas Lahtinend507d732016-03-18 10:42:58 +02003158static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003159{
Chris Wilson49d73912016-11-29 09:50:08 +00003160 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003161 int ret;
3162
Chris Wilson91c8a322016-07-05 10:40:23 +01003163 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003164 if (!ret) {
3165 DRM_ERROR("failed to set up gmch\n");
3166 return -EIO;
3167 }
3168
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003169 intel_gtt_get(&ggtt->base.total,
3170 &ggtt->stolen_size,
3171 &ggtt->mappable_base,
3172 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003173
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003174 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303175 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003176 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3177 ggtt->base.clear_range = i915_ggtt_clear_range;
3178 ggtt->base.bind_vma = ggtt_bind_vma;
3179 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003180 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003181
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003182 ggtt->invalidate = gmch_ggtt_invalidate;
3183
Joonas Lahtinend507d732016-03-18 10:42:58 +02003184 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003185 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3186
Ben Widawskybaa09f52013-01-24 13:49:57 -08003187 return 0;
3188}
3189
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003190/**
Chris Wilson0088e522016-08-04 07:52:21 +01003191 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003192 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003193 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003194int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003195{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003196 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003197 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003198
Chris Wilson49d73912016-11-29 09:50:08 +00003199 ggtt->base.i915 = dev_priv;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003200
Chris Wilson34c998b2016-08-04 07:52:24 +01003201 if (INTEL_GEN(dev_priv) <= 5)
3202 ret = i915_gmch_probe(ggtt);
3203 else if (INTEL_GEN(dev_priv) < 8)
3204 ret = gen6_gmch_probe(ggtt);
3205 else
3206 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003207 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003208 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003209
Chris Wilsondb9309a2017-01-05 15:30:23 +00003210 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3211 * This is easier than doing range restriction on the fly, as we
3212 * currently don't have any bits spare to pass in this upper
3213 * restriction!
3214 */
3215 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3216 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3217 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3218 }
3219
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003220 if ((ggtt->base.total - 1) >> 32) {
3221 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003222 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003223 ggtt->base.total >> 20);
3224 ggtt->base.total = 1ULL << 32;
3225 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3226 }
3227
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003228 if (ggtt->mappable_end > ggtt->base.total) {
3229 DRM_ERROR("mappable aperture extends past end of GGTT,"
3230 " aperture=%llx, total=%llx\n",
3231 ggtt->mappable_end, ggtt->base.total);
3232 ggtt->mappable_end = ggtt->base.total;
3233 }
3234
Ben Widawskybaa09f52013-01-24 13:49:57 -08003235 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003236 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003237 ggtt->base.total >> 20);
3238 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003239 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003240#ifdef CONFIG_INTEL_IOMMU
3241 if (intel_iommu_gfx_mapped)
3242 DRM_INFO("VT-d active for gfx access\n");
3243#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003244
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003245 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003246}
3247
3248/**
3249 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003250 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003251 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003252int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003253{
Chris Wilson0088e522016-08-04 07:52:21 +01003254 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3255 int ret;
3256
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003257 INIT_LIST_HEAD(&dev_priv->vm_list);
3258
Chris Wilsona6508de2017-02-06 08:45:47 +00003259 /* Note that we use page colouring to enforce a guard page at the
3260 * end of the address space. This is required as the CS may prefetch
3261 * beyond the end of the batch buffer, across the page boundary,
3262 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003263 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003264 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01003265 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00003266 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003267 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003268 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003269
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003270 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3271 dev_priv->ggtt.mappable_base,
3272 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003273 ret = -EIO;
3274 goto out_gtt_cleanup;
3275 }
3276
3277 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3278
Chris Wilson0088e522016-08-04 07:52:21 +01003279 /*
3280 * Initialise stolen early so that we may reserve preallocated
3281 * objects for the BIOS to KMS transition.
3282 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003283 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003284 if (ret)
3285 goto out_gtt_cleanup;
3286
3287 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003288
3289out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003290 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003291 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003292}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003293
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003294int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003295{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003296 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003297 return -EIO;
3298
3299 return 0;
3300}
3301
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003302void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3303{
3304 i915->ggtt.invalidate = guc_ggtt_invalidate;
3305}
3306
3307void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3308{
3309 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3310}
3311
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003312void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003313{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003314 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003315 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003316
Chris Wilsondc979972016-05-10 14:10:04 +01003317 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003318
3319 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003320 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003321
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003322 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3323
3324 /* clflush objects bound into the GGTT and rebind them. */
3325 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003326 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003327 bool ggtt_bound = false;
3328 struct i915_vma *vma;
3329
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003330 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003331 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003332 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003333
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003334 if (!i915_vma_unbind(vma))
3335 continue;
3336
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003337 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3338 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003339 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003340 }
3341
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003342 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003343 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003344 }
3345
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003346 ggtt->base.closed = false;
3347
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003348 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003349 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003350 chv_setup_private_ppat(dev_priv);
3351 else
3352 bdw_setup_private_ppat(dev_priv);
3353
3354 return;
3355 }
3356
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003357 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003358 struct i915_address_space *vm;
3359
Daniel Vetterfa423312015-04-14 17:35:23 +02003360 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3361 /* TODO: Perhaps it shouldn't be gen6 specific */
3362
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003363 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003364
Chris Wilson2bfa9962016-08-04 07:52:25 +01003365 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003366 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003367 else
3368 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003369
3370 gen6_write_page_range(dev_priv, &ppgtt->pd,
3371 0, ppgtt->base.total);
3372 }
3373 }
3374
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003375 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003376}
3377
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003378static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003379rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003380 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003381 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003382 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003383{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003384 unsigned int column, row;
3385 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003386
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003387 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003388 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003389 for (row = 0; row < height; row++) {
3390 st->nents++;
3391 /* We don't need the pages, but need to initialize
3392 * the entries so the sg list can be happily traversed.
3393 * The only thing we need are DMA addresses.
3394 */
3395 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003396 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003397 sg_dma_len(sg) = PAGE_SIZE;
3398 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003399 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003400 }
3401 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003402
3403 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003404}
3405
3406static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003407intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003408 struct drm_i915_gem_object *obj)
3409{
Dave Gordon85d12252016-05-20 11:54:06 +01003410 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003411 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003412 struct sgt_iter sgt_iter;
3413 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003414 unsigned long i;
3415 dma_addr_t *page_addr_list;
3416 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003417 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003418 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003419
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003420 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003421 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003422 sizeof(dma_addr_t),
3423 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003424 if (!page_addr_list)
3425 return ERR_PTR(ret);
3426
3427 /* Allocate target SG list. */
3428 st = kmalloc(sizeof(*st), GFP_KERNEL);
3429 if (!st)
3430 goto err_st_alloc;
3431
Ville Syrjälä6687c902015-09-15 13:16:41 +03003432 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003433 if (ret)
3434 goto err_sg_alloc;
3435
3436 /* Populate source page list from the object. */
3437 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003438 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003439 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003440
Dave Gordon85d12252016-05-20 11:54:06 +01003441 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003442 st->nents = 0;
3443 sg = st->sgl;
3444
Ville Syrjälä6687c902015-09-15 13:16:41 +03003445 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3446 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3447 rot_info->plane[i].width, rot_info->plane[i].height,
3448 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003449 }
3450
Ville Syrjälä6687c902015-09-15 13:16:41 +03003451 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3452 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003453
3454 drm_free_large(page_addr_list);
3455
3456 return st;
3457
3458err_sg_alloc:
3459 kfree(st);
3460err_st_alloc:
3461 drm_free_large(page_addr_list);
3462
Ville Syrjälä6687c902015-09-15 13:16:41 +03003463 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3464 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3465
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003466 return ERR_PTR(ret);
3467}
3468
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003469static struct sg_table *
3470intel_partial_pages(const struct i915_ggtt_view *view,
3471 struct drm_i915_gem_object *obj)
3472{
3473 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003474 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003475 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003476 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003477 int ret = -ENOMEM;
3478
3479 st = kmalloc(sizeof(*st), GFP_KERNEL);
3480 if (!st)
3481 goto err_st_alloc;
3482
Chris Wilsond2a84a72016-10-28 13:58:34 +01003483 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003484 if (ret)
3485 goto err_sg_alloc;
3486
Chris Wilson8bab11932017-01-14 00:28:25 +00003487 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003488 GEM_BUG_ON(!iter);
3489
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003490 sg = st->sgl;
3491 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003492 do {
3493 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003494
Chris Wilsond2a84a72016-10-28 13:58:34 +01003495 len = min(iter->length - (offset << PAGE_SHIFT),
3496 count << PAGE_SHIFT);
3497 sg_set_page(sg, NULL, len, 0);
3498 sg_dma_address(sg) =
3499 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3500 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003501
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003502 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003503 count -= len >> PAGE_SHIFT;
3504 if (count == 0) {
3505 sg_mark_end(sg);
3506 return st;
3507 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003508
Chris Wilsond2a84a72016-10-28 13:58:34 +01003509 sg = __sg_next(sg);
3510 iter = __sg_next(iter);
3511 offset = 0;
3512 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003513
3514err_sg_alloc:
3515 kfree(st);
3516err_st_alloc:
3517 return ERR_PTR(ret);
3518}
3519
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003520static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003521i915_get_ggtt_vma_pages(struct i915_vma *vma)
3522{
3523 int ret = 0;
3524
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003525 /* The vma->pages are only valid within the lifespan of the borrowed
3526 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3527 * must be the vma->pages. A simple rule is that vma->pages must only
3528 * be accessed when the obj->mm.pages are pinned.
3529 */
3530 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3531
Chris Wilson247177d2016-08-15 10:48:47 +01003532 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003533 return 0;
3534
3535 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003536 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003537 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003538 vma->pages =
Chris Wilson8bab11932017-01-14 00:28:25 +00003539 intel_rotate_fb_obj_pages(&vma->ggtt_view.rotated,
3540 vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003541 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003542 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003543 else
3544 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3545 vma->ggtt_view.type);
3546
Chris Wilson247177d2016-08-15 10:48:47 +01003547 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003548 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003549 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003550 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003551 } else if (IS_ERR(vma->pages)) {
3552 ret = PTR_ERR(vma->pages);
3553 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003554 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3555 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003556 }
3557
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003558 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003559}
3560
Chris Wilsone007b192017-01-11 11:23:10 +00003561/**
Chris Wilson625d9882017-01-11 11:23:11 +00003562 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003563 * @vm: the &struct i915_address_space
3564 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3565 * @size: how much space to allocate inside the GTT,
3566 * must be #I915_GTT_PAGE_SIZE aligned
3567 * @offset: where to insert inside the GTT,
3568 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3569 * (@offset + @size) must fit within the address space
3570 * @color: color to apply to node, if this node is not from a VMA,
3571 * color must be #I915_COLOR_UNEVICTABLE
3572 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003573 *
3574 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3575 * the address space (using @size and @color). If the @node does not fit, it
3576 * tries to evict any overlapping nodes from the GTT, including any
3577 * neighbouring nodes if the colors do not match (to ensure guard pages between
3578 * differing domains). See i915_gem_evict_for_node() for the gory details
3579 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3580 * evicting active overlapping objects, and any overlapping node that is pinned
3581 * or marked as unevictable will also result in failure.
3582 *
3583 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3584 * asked to wait for eviction and interrupted.
3585 */
3586int i915_gem_gtt_reserve(struct i915_address_space *vm,
3587 struct drm_mm_node *node,
3588 u64 size, u64 offset, unsigned long color,
3589 unsigned int flags)
3590{
3591 int err;
3592
3593 GEM_BUG_ON(!size);
3594 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3595 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3596 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003597 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003598 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003599
3600 node->size = size;
3601 node->start = offset;
3602 node->color = color;
3603
3604 err = drm_mm_reserve_node(&vm->mm, node);
3605 if (err != -ENOSPC)
3606 return err;
3607
3608 err = i915_gem_evict_for_node(vm, node, flags);
3609 if (err == 0)
3610 err = drm_mm_reserve_node(&vm->mm, node);
3611
3612 return err;
3613}
3614
Chris Wilson606fec92017-01-11 11:23:12 +00003615static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3616{
3617 u64 range, addr;
3618
3619 GEM_BUG_ON(range_overflows(start, len, end));
3620 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3621
3622 range = round_down(end - len, align) - round_up(start, align);
3623 if (range) {
3624 if (sizeof(unsigned long) == sizeof(u64)) {
3625 addr = get_random_long();
3626 } else {
3627 addr = get_random_int();
3628 if (range > U32_MAX) {
3629 addr <<= 32;
3630 addr |= get_random_int();
3631 }
3632 }
3633 div64_u64_rem(addr, range, &addr);
3634 start += addr;
3635 }
3636
3637 return round_up(start, align);
3638}
3639
Chris Wilson625d9882017-01-11 11:23:11 +00003640/**
Chris Wilsone007b192017-01-11 11:23:10 +00003641 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003642 * @vm: the &struct i915_address_space
3643 * @node: the &struct drm_mm_node (typically i915_vma.node)
3644 * @size: how much space to allocate inside the GTT,
3645 * must be #I915_GTT_PAGE_SIZE aligned
3646 * @alignment: required alignment of starting offset, may be 0 but
3647 * if specified, this must be a power-of-two and at least
3648 * #I915_GTT_MIN_ALIGNMENT
3649 * @color: color to apply to node
3650 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003651 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003652 * @end: end of any range restriction inside GTT (U64_MAX for all),
3653 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3654 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003655 *
3656 * i915_gem_gtt_insert() first searches for an available hole into which
3657 * is can insert the node. The hole address is aligned to @alignment and
3658 * its @size must then fit entirely within the [@start, @end] bounds. The
3659 * nodes on either side of the hole must match @color, or else a guard page
3660 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003661 * suitable hole is found, first a victim is randomly selected and tested
3662 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003663 * is scanned to find the first set of replacement nodes to create the hole.
3664 * Those old overlapping nodes are evicted from the GTT (and so must be
3665 * rebound before any future use). Any node that is currently pinned cannot
3666 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3667 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3668 * searching for an eviction candidate. See i915_gem_evict_something() for
3669 * the gory details on the eviction algorithm.
3670 *
3671 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3672 * asked to wait for eviction and interrupted.
3673 */
3674int i915_gem_gtt_insert(struct i915_address_space *vm,
3675 struct drm_mm_node *node,
3676 u64 size, u64 alignment, unsigned long color,
3677 u64 start, u64 end, unsigned int flags)
3678{
Chris Wilson4e64e552017-02-02 21:04:38 +00003679 enum drm_mm_insert_mode mode;
Chris Wilson606fec92017-01-11 11:23:12 +00003680 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003681 int err;
3682
3683 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3684 GEM_BUG_ON(!size);
3685 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3686 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3687 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3688 GEM_BUG_ON(start >= end);
3689 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3690 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003691 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003692 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003693
3694 if (unlikely(range_overflows(start, size, end)))
3695 return -ENOSPC;
3696
3697 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3698 return -ENOSPC;
3699
Chris Wilson4e64e552017-02-02 21:04:38 +00003700 mode = DRM_MM_INSERT_BEST;
3701 if (flags & PIN_HIGH)
3702 mode = DRM_MM_INSERT_HIGH;
3703 if (flags & PIN_MAPPABLE)
3704 mode = DRM_MM_INSERT_LOW;
Chris Wilsone007b192017-01-11 11:23:10 +00003705
3706 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3707 * so we know that we always have a minimum alignment of 4096.
3708 * The drm_mm range manager is optimised to return results
3709 * with zero alignment, so where possible use the optimal
3710 * path.
3711 */
3712 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3713 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3714 alignment = 0;
3715
Chris Wilson4e64e552017-02-02 21:04:38 +00003716 err = drm_mm_insert_node_in_range(&vm->mm, node,
3717 size, alignment, color,
3718 start, end, mode);
Chris Wilsone007b192017-01-11 11:23:10 +00003719 if (err != -ENOSPC)
3720 return err;
3721
Chris Wilson606fec92017-01-11 11:23:12 +00003722 /* No free space, pick a slot at random.
3723 *
3724 * There is a pathological case here using a GTT shared between
3725 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3726 *
3727 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3728 * (64k objects) (448k objects)
3729 *
3730 * Now imagine that the eviction LRU is ordered top-down (just because
3731 * pathology meets real life), and that we need to evict an object to
3732 * make room inside the aperture. The eviction scan then has to walk
3733 * the 448k list before it finds one within range. And now imagine that
3734 * it has to search for a new hole between every byte inside the memcpy,
3735 * for several simultaneous clients.
3736 *
3737 * On a full-ppgtt system, if we have run out of available space, there
3738 * will be lots and lots of objects in the eviction list! Again,
3739 * searching that LRU list may be slow if we are also applying any
3740 * range restrictions (e.g. restriction to low 4GiB) and so, for
3741 * simplicity and similarilty between different GTT, try the single
3742 * random replacement first.
3743 */
3744 offset = random_offset(start, end,
3745 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3746 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3747 if (err != -ENOSPC)
3748 return err;
3749
3750 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003751 err = i915_gem_evict_something(vm, size, alignment, color,
3752 start, end, flags);
3753 if (err)
3754 return err;
3755
Chris Wilson4e64e552017-02-02 21:04:38 +00003756 return drm_mm_insert_node_in_range(&vm->mm, node,
3757 size, alignment, color,
3758 start, end, DRM_MM_INSERT_EVICT);
Chris Wilsone007b192017-01-11 11:23:10 +00003759}