blob: 850d40ec77afd773a8b5bf09f1daa49d6d5e2e87 [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);
Ben Widawsky06fda602015-02-24 16:22:36 +0000758
Zhi Wanga18dbba2016-11-29 14:55:16 +0800759 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200760 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200761
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200762 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000763
Mika Kuoppala37c63932016-11-01 15:27:36 +0200764 while (pte < pte_end)
765 pt_vaddr[pte++] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000766
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200767 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200768
769 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200770}
771
Michał Winiarski2ce51792016-10-13 14:02:42 +0200772/* Removes entries from a single page dir, releasing it if it's empty.
773 * Caller can use the return value to update higher-level entries
774 */
775static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200776 struct i915_page_directory *pd,
777 uint64_t start,
778 uint64_t length)
779{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200780 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200781 struct i915_page_table *pt;
782 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200783 gen8_pde_t *pde_vaddr;
784 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
785 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200786
787 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000788 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100789 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000790
Michał Winiarski2ce51792016-10-13 14:02:42 +0200791 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
792 __clear_bit(pde, pd->used_pdes);
793 pde_vaddr = kmap_px(pd);
794 pde_vaddr[pde] = scratch_pde;
795 kunmap_px(ppgtt, pde_vaddr);
Chris Wilson49d73912016-11-29 09:50:08 +0000796 free_pt(vm->i915, pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200797 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200798 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200799
Zhi Wanga18dbba2016-11-29 14:55:16 +0800800 if (bitmap_empty(pd->used_pdes, I915_PDES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200801 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200802
803 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200804}
Ben Widawsky06fda602015-02-24 16:22:36 +0000805
Michał Winiarski2ce51792016-10-13 14:02:42 +0200806/* Removes entries from a single page dir pointer, releasing it if it's empty.
807 * Caller can use the return value to update higher-level entries
808 */
809static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200810 struct i915_page_directory_pointer *pdp,
811 uint64_t start,
812 uint64_t length)
813{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200814 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200815 struct i915_page_directory *pd;
816 uint64_t pdpe;
817
818 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
819 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100820 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000821
Michał Winiarski2ce51792016-10-13 14:02:42 +0200822 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
823 __clear_bit(pdpe, pdp->used_pdpes);
Matthew Auld9e65a372016-12-13 16:05:12 +0000824 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
Chris Wilson49d73912016-11-29 09:50:08 +0000825 free_pd(vm->i915, pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200826 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200827 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200828
Mika Kuoppalafce93752016-10-31 17:24:46 +0200829 mark_tlbs_dirty(ppgtt);
830
Zhi Wanga18dbba2016-11-29 14:55:16 +0800831 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200832 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200833
834 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200835}
Ben Widawsky459108b2013-11-02 21:07:23 -0700836
Michał Winiarski2ce51792016-10-13 14:02:42 +0200837/* Removes entries from a single pml4.
838 * This is the top-level structure in 4-level page tables used on gen8+.
839 * Empty entries are always scratch pml4e.
840 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200841static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
842 struct i915_pml4 *pml4,
843 uint64_t start,
844 uint64_t length)
845{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200846 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200847 struct i915_page_directory_pointer *pdp;
848 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200849
Chris Wilson49d73912016-11-29 09:50:08 +0000850 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700851
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200852 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
853 if (WARN_ON(!pml4->pdps[pml4e]))
854 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700855
Michał Winiarski2ce51792016-10-13 14:02:42 +0200856 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
857 __clear_bit(pml4e, pml4->used_pml4es);
Matthew Auld9e65a372016-12-13 16:05:12 +0000858 gen8_setup_pml4e(ppgtt, pml4, vm->scratch_pdp, pml4e);
Chris Wilson49d73912016-11-29 09:50:08 +0000859 free_pdp(vm->i915, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200860 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700861 }
862}
863
Michel Thierryf9b5b782015-07-30 11:02:49 +0100864static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200865 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700866{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300867 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100868
Chris Wilsonc6385c92016-11-29 12:42:05 +0000869 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200870 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
871 else
872 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100873}
874
875static void
876gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
877 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100878 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100879 uint64_t start,
880 enum i915_cache_level cache_level)
881{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300882 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000883 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100884 unsigned pdpe = gen8_pdpe_index(start);
885 unsigned pde = gen8_pde_index(start);
886 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700887
Chris Wilson6f1cc992013-12-31 15:50:31 +0000888 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700889
Michel Thierry3387d432015-08-03 09:52:47 +0100890 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000891 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100892 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100893 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300894 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000895 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800896
897 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100898 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200899 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000900 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300901 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000902 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000903 if (++pde == I915_PDES) {
Chris Wilsonc6385c92016-11-29 12:42:05 +0000904 if (++pdpe == I915_PDPES_PER_PDP(vm->i915))
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100905 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800906 pde = 0;
907 }
908 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700909 }
910 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300911
912 if (pt_vaddr)
913 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700914}
915
Michel Thierryf9b5b782015-07-30 11:02:49 +0100916static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
917 struct sg_table *pages,
918 uint64_t start,
919 enum i915_cache_level cache_level,
920 u32 unused)
921{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300922 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100923 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100924
Michel Thierry3387d432015-08-03 09:52:47 +0100925 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100926
Chris Wilsonc6385c92016-11-29 12:42:05 +0000927 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100928 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
929 cache_level);
930 } else {
931 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000932 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100933 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
934
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000935 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100936 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
937 start, cache_level);
938 }
939 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100940}
941
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000942static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
Michel Thierryf37c0502015-06-10 17:46:39 +0100943 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800944{
945 int i;
946
Mika Kuoppala567047b2015-06-25 18:35:12 +0300947 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800948 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800949
Michel Thierry33c88192015-04-08 12:13:33 +0100950 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000951 if (WARN_ON(!pd->page_table[i]))
952 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800953
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000954 free_pt(dev_priv, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000955 pd->page_table[i] = NULL;
956 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000957}
958
Mika Kuoppala8776f022015-06-30 18:16:40 +0300959static int gen8_init_scratch(struct i915_address_space *vm)
960{
Chris Wilson49d73912016-11-29 09:50:08 +0000961 struct drm_i915_private *dev_priv = vm->i915;
Matthew Auld64c050d2016-04-27 13:19:25 +0100962 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300963
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000964 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100965 if (ret)
966 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300967
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000968 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300969 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100970 ret = PTR_ERR(vm->scratch_pt);
971 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300972 }
973
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000974 vm->scratch_pd = alloc_pd(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300975 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100976 ret = PTR_ERR(vm->scratch_pd);
977 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300978 }
979
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000980 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
981 vm->scratch_pdp = alloc_pdp(dev_priv);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100982 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100983 ret = PTR_ERR(vm->scratch_pdp);
984 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100985 }
986 }
987
Mika Kuoppala8776f022015-06-30 18:16:40 +0300988 gen8_initialize_pt(vm, vm->scratch_pt);
989 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000990 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100991 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300992
993 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100994
995free_pd:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000996 free_pd(dev_priv, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +0100997free_pt:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000998 free_pt(dev_priv, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +0100999free_scratch_page:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001000 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +01001001
1002 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001003}
1004
Zhiyuan Lv650da342015-08-28 15:41:18 +08001005static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1006{
1007 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +00001008 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001009 int i;
1010
Matthew Aulddf285642016-04-22 12:09:25 +01001011 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001012 u64 daddr = px_dma(&ppgtt->pml4);
1013
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001014 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1015 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001016
1017 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1018 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1019 } else {
1020 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1021 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1022
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001023 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1024 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001025 }
1026
1027 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1028 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1029 }
1030
1031 I915_WRITE(vgtif_reg(g2v_notify), msg);
1032
1033 return 0;
1034}
1035
Mika Kuoppala8776f022015-06-30 18:16:40 +03001036static void gen8_free_scratch(struct i915_address_space *vm)
1037{
Chris Wilson49d73912016-11-29 09:50:08 +00001038 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001039
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001040 if (USES_FULL_48BIT_PPGTT(dev_priv))
1041 free_pdp(dev_priv, vm->scratch_pdp);
1042 free_pd(dev_priv, vm->scratch_pd);
1043 free_pt(dev_priv, vm->scratch_pt);
1044 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001045}
1046
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001047static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
Michel Thierry762d9932015-07-30 11:05:29 +01001048 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001049{
1050 int i;
1051
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001052 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001053 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001054 continue;
1055
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001056 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1057 free_pd(dev_priv, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001058 }
Michel Thierry69876be2015-04-08 12:13:27 +01001059
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001060 free_pdp(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001061}
1062
1063static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1064{
Chris Wilson49d73912016-11-29 09:50:08 +00001065 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001066 int i;
1067
1068 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1069 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1070 continue;
1071
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001072 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001073 }
1074
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001075 cleanup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001076}
1077
1078static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1079{
Chris Wilson49d73912016-11-29 09:50:08 +00001080 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001081 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001082
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001083 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001084 gen8_ppgtt_notify_vgt(ppgtt, false);
1085
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001086 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1087 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001088 else
1089 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001090
Mika Kuoppala8776f022015-06-30 18:16:40 +03001091 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001092}
1093
Michel Thierryd7b26332015-04-08 12:13:34 +01001094/**
1095 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001096 * @vm: Master vm structure.
1097 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001098 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001099 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001100 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1101 * caller to free on error.
1102 *
1103 * Allocate the required number of page tables. Extremely similar to
1104 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1105 * the page directory boundary (instead of the page directory pointer). That
1106 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1107 * possible, and likely that the caller will need to use multiple calls of this
1108 * function to achieve the appropriate allocation.
1109 *
1110 * Return: 0 if success; negative error code otherwise.
1111 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001112static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001113 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001114 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001115 uint64_t length,
1116 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001117{
Chris Wilson49d73912016-11-29 09:50:08 +00001118 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001119 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001120 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001121
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001122 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001123 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001124 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001125 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001126 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001127 continue;
1128 }
1129
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001130 pt = alloc_pt(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001131 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001132 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001133
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001134 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001135 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001136 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001137 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001138 }
1139
1140 return 0;
1141
1142unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001143 for_each_set_bit(pde, new_pts, I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001144 free_pt(dev_priv, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001145
1146 return -ENOMEM;
1147}
1148
Michel Thierryd7b26332015-04-08 12:13:34 +01001149/**
1150 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001151 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001152 * @pdp: Page directory pointer for this address range.
1153 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001154 * @length: Size of the allocations.
1155 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001156 * caller to free on error.
1157 *
1158 * Allocate the required number of page directories starting at the pde index of
1159 * @start, and ending at the pde index @start + @length. This function will skip
1160 * over already allocated page directories within the range, and only allocate
1161 * new ones, setting the appropriate pointer within the pdp as well as the
1162 * correct position in the bitmap @new_pds.
1163 *
1164 * The function will only allocate the pages within the range for a give page
1165 * directory pointer. In other words, if @start + @length straddles a virtually
1166 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1167 * required by the caller, This is not currently possible, and the BUG in the
1168 * code will prevent it.
1169 *
1170 * Return: 0 if success; negative error code otherwise.
1171 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001172static int
1173gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1174 struct i915_page_directory_pointer *pdp,
1175 uint64_t start,
1176 uint64_t length,
1177 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001178{
Chris Wilson49d73912016-11-29 09:50:08 +00001179 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001180 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001181 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001182 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001183
Michel Thierry6ac18502015-07-29 17:23:46 +01001184 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001185
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001186 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001187 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001188 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001189
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001190 pd = alloc_pd(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001191 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001192 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001193
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001194 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001195 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001196 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001197 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001198 }
1199
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001200 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001201
1202unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001203 for_each_set_bit(pdpe, new_pds, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001204 free_pd(dev_priv, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001205
1206 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001207}
1208
Michel Thierry762d9932015-07-30 11:05:29 +01001209/**
1210 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1211 * @vm: Master vm structure.
1212 * @pml4: Page map level 4 for this address range.
1213 * @start: Starting virtual address to begin allocations.
1214 * @length: Size of the allocations.
1215 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1216 * caller to free on error.
1217 *
1218 * Allocate the required number of page directory pointers. Extremely similar to
1219 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1220 * The main difference is here we are limited by the pml4 boundary (instead of
1221 * the page directory pointer).
1222 *
1223 * Return: 0 if success; negative error code otherwise.
1224 */
1225static int
1226gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1227 struct i915_pml4 *pml4,
1228 uint64_t start,
1229 uint64_t length,
1230 unsigned long *new_pdps)
1231{
Chris Wilson49d73912016-11-29 09:50:08 +00001232 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001233 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001234 uint32_t pml4e;
1235
1236 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1237
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001238 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001239 if (!test_bit(pml4e, pml4->used_pml4es)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001240 pdp = alloc_pdp(dev_priv);
Michel Thierry762d9932015-07-30 11:05:29 +01001241 if (IS_ERR(pdp))
1242 goto unwind_out;
1243
Michel Thierry69ab76f2015-07-29 17:23:55 +01001244 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001245 pml4->pdps[pml4e] = pdp;
1246 __set_bit(pml4e, new_pdps);
1247 trace_i915_page_directory_pointer_entry_alloc(vm,
1248 pml4e,
1249 start,
1250 GEN8_PML4E_SHIFT);
1251 }
1252 }
1253
1254 return 0;
1255
1256unwind_out:
1257 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001258 free_pdp(dev_priv, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001259
1260 return -ENOMEM;
1261}
1262
Michel Thierryd7b26332015-04-08 12:13:34 +01001263static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001264free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001265{
Michel Thierryd7b26332015-04-08 12:13:34 +01001266 kfree(new_pts);
1267 kfree(new_pds);
1268}
1269
1270/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1271 * of these are based on the number of PDPEs in the system.
1272 */
1273static
1274int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001275 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001276 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001277{
Michel Thierryd7b26332015-04-08 12:13:34 +01001278 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001279 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001280
Michał Winiarski3a41a052015-09-03 19:22:18 +02001281 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001282 if (!pds)
1283 return -ENOMEM;
1284
Michał Winiarski3a41a052015-09-03 19:22:18 +02001285 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1286 GFP_TEMPORARY);
1287 if (!pts)
1288 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001289
1290 *new_pds = pds;
1291 *new_pts = pts;
1292
1293 return 0;
1294
1295err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001296 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001297 return -ENOMEM;
1298}
1299
Michel Thierry762d9932015-07-30 11:05:29 +01001300static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1301 struct i915_page_directory_pointer *pdp,
1302 uint64_t start,
1303 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001304{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001305 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001306 unsigned long *new_page_dirs, *new_page_tables;
Chris Wilson49d73912016-11-29 09:50:08 +00001307 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001308 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001309 const uint64_t orig_start = start;
1310 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001311 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001312 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001313 int ret;
1314
Michel Thierry6ac18502015-07-29 17:23:46 +01001315 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001316 if (ret)
1317 return ret;
1318
Michel Thierryd7b26332015-04-08 12:13:34 +01001319 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001320 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1321 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001322 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001323 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001324 return ret;
1325 }
1326
1327 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001328 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001329 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001330 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001331 if (ret)
1332 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001333 }
1334
Michel Thierry33c88192015-04-08 12:13:33 +01001335 start = orig_start;
1336 length = orig_length;
1337
Michel Thierryd7b26332015-04-08 12:13:34 +01001338 /* Allocations have completed successfully, so set the bitmaps, and do
1339 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001340 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001341 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001342 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001343 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001344 uint64_t pd_start = start;
1345 uint32_t pde;
1346
Michel Thierryd7b26332015-04-08 12:13:34 +01001347 /* Every pd should be allocated, we just did that above. */
1348 WARN_ON(!pd);
1349
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001350 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001351 /* Same reasoning as pd */
1352 WARN_ON(!pt);
1353 WARN_ON(!pd_len);
1354 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1355
1356 /* Set our used ptes within the page table */
1357 bitmap_set(pt->used_ptes,
1358 gen8_pte_index(pd_start),
1359 gen8_pte_count(pd_start, pd_len));
1360
1361 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001362 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001363
1364 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001365 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1366 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001367 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1368 gen8_pte_index(start),
1369 gen8_pte_count(start, length),
1370 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001371
1372 /* NB: We haven't yet mapped ptes to pages. At this
1373 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001374 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001375
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001376 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001377 __set_bit(pdpe, pdp->used_pdpes);
Matthew Auld5c693b22016-12-13 16:05:10 +00001378 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001379 }
1380
Michał Winiarski3a41a052015-09-03 19:22:18 +02001381 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001382 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001383 return 0;
1384
1385err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001386 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001387 unsigned long temp;
1388
Michał Winiarski3a41a052015-09-03 19:22:18 +02001389 for_each_set_bit(temp, new_page_tables + pdpe *
1390 BITS_TO_LONGS(I915_PDES), I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001391 free_pt(dev_priv,
1392 pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001393 }
1394
Michel Thierry6ac18502015-07-29 17:23:46 +01001395 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001396 free_pd(dev_priv, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001397
Michał Winiarski3a41a052015-09-03 19:22:18 +02001398 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001399 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001400 return ret;
1401}
1402
Michel Thierry762d9932015-07-30 11:05:29 +01001403static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1404 struct i915_pml4 *pml4,
1405 uint64_t start,
1406 uint64_t length)
1407{
1408 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001409 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001410 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001411 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001412 int ret = 0;
1413
1414 /* Do the pml4 allocations first, so we don't need to track the newly
1415 * allocated tables below the pdp */
1416 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1417
1418 /* The pagedirectory and pagetable allocations are done in the shared 3
1419 * and 4 level code. Just allocate the pdps.
1420 */
1421 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1422 new_pdps);
1423 if (ret)
1424 return ret;
1425
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001426 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001427 WARN_ON(!pdp);
1428
1429 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1430 if (ret)
1431 goto err_out;
1432
Matthew Auld56843102016-12-13 16:05:11 +00001433 gen8_setup_pml4e(ppgtt, pml4, pdp, pml4e);
Michel Thierry762d9932015-07-30 11:05:29 +01001434 }
1435
1436 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1437 GEN8_PML4ES_PER_PML4);
1438
1439 return 0;
1440
1441err_out:
1442 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson49d73912016-11-29 09:50:08 +00001443 gen8_ppgtt_cleanup_3lvl(vm->i915, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001444
1445 return ret;
1446}
1447
1448static int gen8_alloc_va_range(struct i915_address_space *vm,
1449 uint64_t start, uint64_t length)
1450{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001451 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001452
Chris Wilsonc6385c92016-11-29 12:42:05 +00001453 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001454 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1455 else
1456 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1457}
1458
Michel Thierryea91e402015-07-29 17:23:57 +01001459static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1460 uint64_t start, uint64_t length,
1461 gen8_pte_t scratch_pte,
1462 struct seq_file *m)
1463{
1464 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001465 uint32_t pdpe;
1466
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001467 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001468 struct i915_page_table *pt;
1469 uint64_t pd_len = length;
1470 uint64_t pd_start = start;
1471 uint32_t pde;
1472
1473 if (!test_bit(pdpe, pdp->used_pdpes))
1474 continue;
1475
1476 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001477 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001478 uint32_t pte;
1479 gen8_pte_t *pt_vaddr;
1480
1481 if (!test_bit(pde, pd->used_pdes))
1482 continue;
1483
1484 pt_vaddr = kmap_px(pt);
1485 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1486 uint64_t va =
1487 (pdpe << GEN8_PDPE_SHIFT) |
1488 (pde << GEN8_PDE_SHIFT) |
1489 (pte << GEN8_PTE_SHIFT);
1490 int i;
1491 bool found = false;
1492
1493 for (i = 0; i < 4; i++)
1494 if (pt_vaddr[pte + i] != scratch_pte)
1495 found = true;
1496 if (!found)
1497 continue;
1498
1499 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1500 for (i = 0; i < 4; i++) {
1501 if (pt_vaddr[pte + i] != scratch_pte)
1502 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1503 else
1504 seq_puts(m, " SCRATCH ");
1505 }
1506 seq_puts(m, "\n");
1507 }
1508 /* don't use kunmap_px, it could trigger
1509 * an unnecessary flush.
1510 */
1511 kunmap_atomic(pt_vaddr);
1512 }
1513 }
1514}
1515
1516static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1517{
1518 struct i915_address_space *vm = &ppgtt->base;
1519 uint64_t start = ppgtt->base.start;
1520 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001521 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001522 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001523
Chris Wilsonc6385c92016-11-29 12:42:05 +00001524 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryea91e402015-07-29 17:23:57 +01001525 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1526 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001527 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001528 struct i915_pml4 *pml4 = &ppgtt->pml4;
1529 struct i915_page_directory_pointer *pdp;
1530
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001531 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001532 if (!test_bit(pml4e, pml4->used_pml4es))
1533 continue;
1534
1535 seq_printf(m, " PML4E #%llu\n", pml4e);
1536 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1537 }
1538 }
1539}
1540
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001541static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1542{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001543 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001544 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001545 int ret;
1546
1547 /* We allocate temp bitmap for page tables for no gain
1548 * but as this is for init only, lets keep the things simple
1549 */
1550 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1551 if (ret)
1552 return ret;
1553
1554 /* Allocate for all pdps regardless of how the ppgtt
1555 * was defined.
1556 */
1557 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1558 0, 1ULL << 32,
1559 new_page_dirs);
1560 if (!ret)
1561 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1562
Michał Winiarski3a41a052015-09-03 19:22:18 +02001563 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001564
1565 return ret;
1566}
1567
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001568/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001569 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1570 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1571 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1572 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001573 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001574 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001575static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001576{
Chris Wilson49d73912016-11-29 09:50:08 +00001577 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001578 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001579
Mika Kuoppala8776f022015-06-30 18:16:40 +03001580 ret = gen8_init_scratch(&ppgtt->base);
1581 if (ret)
1582 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001583
Michel Thierryd7b26332015-04-08 12:13:34 +01001584 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001585 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001586 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001587 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001588 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001589 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1590 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001591 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001592
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001593 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1594 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001595 if (ret)
1596 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001597
Michel Thierry69ab76f2015-07-29 17:23:55 +01001598 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1599
Michel Thierry762d9932015-07-30 11:05:29 +01001600 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001601 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001602 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001603 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001604 if (ret)
1605 goto free_scratch;
1606
1607 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001608 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001609 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1610 0, 0,
1611 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001612
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001613 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001614 ret = gen8_preallocate_top_level_pdps(ppgtt);
1615 if (ret)
1616 goto free_scratch;
1617 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001618 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001619
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001620 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001621 gen8_ppgtt_notify_vgt(ppgtt, true);
1622
Michel Thierryd7b26332015-04-08 12:13:34 +01001623 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001624
1625free_scratch:
1626 gen8_free_scratch(&ppgtt->base);
1627 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001628}
1629
Ben Widawsky87d60b62013-12-06 14:11:29 -08001630static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1631{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001632 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001633 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001634 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001635 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001636 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001637 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001638
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001639 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001640 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001641
Dave Gordon731f74c2016-06-24 19:37:46 +01001642 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001643 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001644 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001645 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001646 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001647 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1648
1649 if (pd_entry != expected)
1650 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1651 pde,
1652 pd_entry,
1653 expected);
1654 seq_printf(m, "\tPDE: %x\n", pd_entry);
1655
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001656 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1657
Michel Thierry07749ef2015-03-16 16:00:54 +00001658 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001659 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001660 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001661 (pte * PAGE_SIZE);
1662 int i;
1663 bool found = false;
1664 for (i = 0; i < 4; i++)
1665 if (pt_vaddr[pte + i] != scratch_pte)
1666 found = true;
1667 if (!found)
1668 continue;
1669
1670 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1671 for (i = 0; i < 4; i++) {
1672 if (pt_vaddr[pte + i] != scratch_pte)
1673 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1674 else
1675 seq_puts(m, " SCRATCH ");
1676 }
1677 seq_puts(m, "\n");
1678 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001679 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001680 }
1681}
1682
Ben Widawsky678d96f2015-03-16 16:00:56 +00001683/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001684static void gen6_write_pde(struct i915_page_directory *pd,
1685 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001686{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001687 /* Caller needs to make sure the write completes if necessary */
1688 struct i915_hw_ppgtt *ppgtt =
1689 container_of(pd, struct i915_hw_ppgtt, pd);
1690 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001691
Mika Kuoppala567047b2015-06-25 18:35:12 +03001692 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001693 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001694
Ben Widawsky678d96f2015-03-16 16:00:56 +00001695 writel(pd_entry, ppgtt->pd_addr + pde);
1696}
Ben Widawsky61973492013-04-08 18:43:54 -07001697
Ben Widawsky678d96f2015-03-16 16:00:56 +00001698/* Write all the page tables found in the ppgtt structure to incrementing page
1699 * directories. */
1700static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001701 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001702 uint32_t start, uint32_t length)
1703{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001704 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001705 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001706 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001707
Dave Gordon731f74c2016-06-24 19:37:46 +01001708 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001709 gen6_write_pde(pd, pde, pt);
1710
1711 /* Make sure write is complete before other code can use this page
1712 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001713 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001714}
1715
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001716static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001717{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001718 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001719
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001720 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001721}
Ben Widawsky61973492013-04-08 18:43:54 -07001722
Ben Widawsky90252e52013-12-06 14:11:12 -08001723static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001724 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001725{
Chris Wilson7e37f882016-08-02 22:50:21 +01001726 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001727 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001728 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001729
Ben Widawsky90252e52013-12-06 14:11:12 -08001730 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001731 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001732 if (ret)
1733 return ret;
1734
John Harrison5fb9de12015-05-29 17:44:07 +01001735 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001736 if (ret)
1737 return ret;
1738
Chris Wilsonb5321f32016-08-02 22:50:18 +01001739 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1740 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1741 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1742 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1743 intel_ring_emit(ring, get_pd_offset(ppgtt));
1744 intel_ring_emit(ring, MI_NOOP);
1745 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001746
1747 return 0;
1748}
1749
Ben Widawsky48a10382013-12-06 14:11:11 -08001750static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001751 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001752{
Chris Wilson7e37f882016-08-02 22:50:21 +01001753 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001754 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001755 int ret;
1756
Ben Widawsky48a10382013-12-06 14:11:11 -08001757 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001758 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001759 if (ret)
1760 return ret;
1761
John Harrison5fb9de12015-05-29 17:44:07 +01001762 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001763 if (ret)
1764 return ret;
1765
Chris Wilsonb5321f32016-08-02 22:50:18 +01001766 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1767 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1768 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1769 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1770 intel_ring_emit(ring, get_pd_offset(ppgtt));
1771 intel_ring_emit(ring, MI_NOOP);
1772 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001773
Ben Widawsky90252e52013-12-06 14:11:12 -08001774 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001775 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001776 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001777 if (ret)
1778 return ret;
1779 }
1780
Ben Widawsky48a10382013-12-06 14:11:11 -08001781 return 0;
1782}
1783
Ben Widawskyeeb94882013-12-06 14:11:10 -08001784static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001785 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001786{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001787 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001788 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001789
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001790 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1791 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001792 return 0;
1793}
1794
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001795static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001796{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001797 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301798 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001799
Akash Goel3b3f1652016-10-13 22:44:48 +05301800 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001801 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1802 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001803 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001804 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001805 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001806}
1807
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001808static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001809{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001810 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001811 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301812 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001813
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001814 ecobits = I915_READ(GAC_ECO_BITS);
1815 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1816
1817 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001818 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001819 ecochk |= ECOCHK_PPGTT_WB_HSW;
1820 } else {
1821 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1822 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1823 }
1824 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001825
Akash Goel3b3f1652016-10-13 22:44:48 +05301826 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001827 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001828 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001829 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001830 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001831}
1832
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001833static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001834{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001835 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001836
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001837 ecobits = I915_READ(GAC_ECO_BITS);
1838 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1839 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001840
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001841 gab_ctl = I915_READ(GAB_CTL);
1842 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001843
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001844 ecochk = I915_READ(GAM_ECOCHK);
1845 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001846
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001847 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001848}
1849
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001850/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001851static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001852 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001853 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001854{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001855 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001856 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001857 unsigned first_entry = start >> PAGE_SHIFT;
1858 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001859 unsigned act_pt = first_entry / GEN6_PTES;
1860 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001861 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001862
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001863 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001864 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001865
Daniel Vetter7bddb012012-02-09 17:15:47 +01001866 while (num_entries) {
1867 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001868 if (last_pte > GEN6_PTES)
1869 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001870
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001871 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001872
1873 for (i = first_pte; i < last_pte; i++)
1874 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001875
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001876 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001877
Daniel Vetter7bddb012012-02-09 17:15:47 +01001878 num_entries -= last_pte - first_pte;
1879 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001880 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001881 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001882}
1883
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001884static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001885 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001886 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301887 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001888{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001889 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001890 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001891 unsigned act_pt = first_entry / GEN6_PTES;
1892 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001893 gen6_pte_t *pt_vaddr = NULL;
1894 struct sgt_iter sgt_iter;
1895 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001896
Dave Gordon85d12252016-05-20 11:54:06 +01001897 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001898 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001899 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001900
Chris Wilsoncc797142013-12-31 15:50:30 +00001901 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001902 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301903
Michel Thierry07749ef2015-03-16 16:00:54 +00001904 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001905 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001906 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001907 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001908 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001909 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001910 }
Dave Gordon85d12252016-05-20 11:54:06 +01001911
Chris Wilsoncc797142013-12-31 15:50:30 +00001912 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001913 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001914}
1915
Ben Widawsky678d96f2015-03-16 16:00:56 +00001916static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001917 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001918{
Michel Thierry4933d512015-03-24 15:46:22 +00001919 DECLARE_BITMAP(new_page_tables, I915_PDES);
Chris Wilson49d73912016-11-29 09:50:08 +00001920 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001921 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001922 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001923 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001924 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001925 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001926 int ret;
1927
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001928 start = start_save = start_in;
1929 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001930
1931 bitmap_zero(new_page_tables, I915_PDES);
1932
1933 /* The allocation is done in two stages so that we can bail out with
1934 * minimal amount of pain. The first stage finds new page tables that
1935 * need allocation. The second stage marks use ptes within the page
1936 * tables.
1937 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001938 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001939 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001940 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1941 continue;
1942 }
1943
1944 /* We've already allocated a page table */
1945 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1946
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001947 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001948 if (IS_ERR(pt)) {
1949 ret = PTR_ERR(pt);
1950 goto unwind_out;
1951 }
1952
1953 gen6_initialize_pt(vm, pt);
1954
1955 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001956 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001957 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001958 }
1959
1960 start = start_save;
1961 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001962
Dave Gordon731f74c2016-06-24 19:37:46 +01001963 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001964 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1965
1966 bitmap_zero(tmp_bitmap, GEN6_PTES);
1967 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1968 gen6_pte_count(start, length));
1969
Mika Kuoppala966082c2015-06-25 18:35:19 +03001970 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001971 gen6_write_pde(&ppgtt->pd, pde, pt);
1972
Michel Thierry72744cb2015-03-24 15:46:23 +00001973 trace_i915_page_table_entry_map(vm, pde, pt,
1974 gen6_pte_index(start),
1975 gen6_pte_count(start, length),
1976 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001977 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001978 GEN6_PTES);
1979 }
1980
Michel Thierry4933d512015-03-24 15:46:22 +00001981 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1982
1983 /* Make sure write is complete before other code can use this page
1984 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001985 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001986
Ben Widawsky563222a2015-03-19 12:53:28 +00001987 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001988 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001989
1990unwind_out:
1991 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001992 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00001993
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001994 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001995 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001996 }
1997
1998 mark_tlbs_dirty(ppgtt);
1999 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002000}
2001
Mika Kuoppala8776f022015-06-30 18:16:40 +03002002static int gen6_init_scratch(struct i915_address_space *vm)
2003{
Chris Wilson49d73912016-11-29 09:50:08 +00002004 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002005 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002006
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002007 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002008 if (ret)
2009 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002010
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002011 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002012 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002013 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002014 return PTR_ERR(vm->scratch_pt);
2015 }
2016
2017 gen6_initialize_pt(vm, vm->scratch_pt);
2018
2019 return 0;
2020}
2021
2022static void gen6_free_scratch(struct i915_address_space *vm)
2023{
Chris Wilson49d73912016-11-29 09:50:08 +00002024 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002025
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002026 free_pt(dev_priv, vm->scratch_pt);
2027 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002028}
2029
Daniel Vetter061dd492015-04-14 17:35:13 +02002030static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002031{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002032 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002033 struct i915_page_directory *pd = &ppgtt->pd;
Chris Wilson49d73912016-11-29 09:50:08 +00002034 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry09942c62015-04-08 12:13:30 +01002035 struct i915_page_table *pt;
2036 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002037
Daniel Vetter061dd492015-04-14 17:35:13 +02002038 drm_mm_remove_node(&ppgtt->node);
2039
Dave Gordon731f74c2016-06-24 19:37:46 +01002040 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002041 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002042 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002043
Mika Kuoppala8776f022015-06-30 18:16:40 +03002044 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002045}
2046
Ben Widawskyb1465202014-02-19 22:05:49 -08002047static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002048{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002049 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00002050 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002051 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002052 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002053
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002054 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2055 * allocator works in address space sizes, so it's multiplied by page
2056 * size. We allocate at the top of the GTT to avoid fragmentation.
2057 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002058 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002059
Mika Kuoppala8776f022015-06-30 18:16:40 +03002060 ret = gen6_init_scratch(vm);
2061 if (ret)
2062 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002063
Chris Wilsone007b192017-01-11 11:23:10 +00002064 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2065 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2066 I915_COLOR_UNEVICTABLE,
2067 0, ggtt->base.total,
2068 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00002069 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002070 goto err_out;
2071
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002072 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002073 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002074
Ben Widawskyc8c26622015-01-22 17:01:25 +00002075 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002076
2077err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002078 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002079 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002080}
2081
Ben Widawskyb1465202014-02-19 22:05:49 -08002082static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2083{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002084 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002085}
2086
Michel Thierry4933d512015-03-24 15:46:22 +00002087static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2088 uint64_t start, uint64_t length)
2089{
Michel Thierryec565b32015-04-08 12:13:23 +01002090 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002091 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002092
Dave Gordon731f74c2016-06-24 19:37:46 +01002093 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002094 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002095}
2096
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002097static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002098{
Chris Wilson49d73912016-11-29 09:50:08 +00002099 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002100 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002101 int ret;
2102
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002103 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002104 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002105 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002106 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002107 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002108 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002109 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002110 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002111 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002112
2113 ret = gen6_ppgtt_alloc(ppgtt);
2114 if (ret)
2115 return ret;
2116
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002117 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002118 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2119 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002120 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2121 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002122 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002123 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002124 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002125 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002126
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002127 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002128 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002129
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002130 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002131 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002132
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002133 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002134
Ben Widawsky678d96f2015-03-16 16:00:56 +00002135 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2136
Thierry Reding440fd522015-01-23 09:05:06 +01002137 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002138 ppgtt->node.size >> 20,
2139 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002140
Daniel Vetterfa76da32014-08-06 20:19:54 +02002141 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002142 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002143
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002144 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002145}
2146
Chris Wilson2bfa9962016-08-04 07:52:25 +01002147static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2148 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002149{
Chris Wilson49d73912016-11-29 09:50:08 +00002150 ppgtt->base.i915 = dev_priv;
Daniel Vetter3440d262013-01-24 13:49:56 -08002151
Chris Wilson2bfa9962016-08-04 07:52:25 +01002152 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002153 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002154 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002155 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002156}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002157
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002158static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002159 struct drm_i915_private *dev_priv,
2160 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002161{
Chris Wilson80b204b2016-10-28 13:58:58 +01002162 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Chris Wilson47db9222017-02-06 08:45:46 +00002163
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002164 drm_mm_init(&vm->mm, vm->start, vm->total);
Chris Wilson47db9222017-02-06 08:45:46 +00002165 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
2166
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002167 INIT_LIST_HEAD(&vm->active_list);
2168 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002169 INIT_LIST_HEAD(&vm->unbound_list);
Chris Wilson47db9222017-02-06 08:45:46 +00002170
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002171 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2172}
2173
Matthew Aulded9724d2016-11-17 21:04:10 +00002174static void i915_address_space_fini(struct i915_address_space *vm)
2175{
2176 i915_gem_timeline_fini(&vm->timeline);
2177 drm_mm_takedown(&vm->mm);
2178 list_del(&vm->global_link);
2179}
2180
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002181static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002182{
Tim Gored5165eb2016-02-04 11:49:34 +00002183 /* This function is for gtt related workarounds. This function is
2184 * called on driver load and after a GPU reset, so you can place
2185 * workarounds here even if they get overwritten by GPU reset.
2186 */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002187 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002188 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002189 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002190 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002191 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08002192 else if (IS_GEN9_BC(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002193 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002194 else if (IS_GEN9_LP(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002195 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2196}
2197
Chris Wilson2bfa9962016-08-04 07:52:25 +01002198static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2199 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002200 struct drm_i915_file_private *file_priv,
2201 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002202{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002203 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002204
Chris Wilson2bfa9962016-08-04 07:52:25 +01002205 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002206 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002207 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002208 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002209 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002210 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002211
2212 return ret;
2213}
2214
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002215int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002216{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002217 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002218
Thomas Daniel671b50132014-08-20 16:24:50 +01002219 /* In the case of execlists, PPGTT is enabled by the context descriptor
2220 * and the PDPs are contained within the context itself. We don't
2221 * need to do anything here. */
2222 if (i915.enable_execlists)
2223 return 0;
2224
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002225 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002226 return 0;
2227
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002228 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002229 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002230 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002231 gen7_ppgtt_enable(dev_priv);
2232 else if (INTEL_GEN(dev_priv) >= 8)
2233 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002234 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002235 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002236
John Harrison4ad2fd82015-06-18 13:11:20 +01002237 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002238}
John Harrison4ad2fd82015-06-18 13:11:20 +01002239
Daniel Vetter4d884702014-08-06 15:04:47 +02002240struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002241i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002242 struct drm_i915_file_private *fpriv,
2243 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002244{
2245 struct i915_hw_ppgtt *ppgtt;
2246 int ret;
2247
2248 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2249 if (!ppgtt)
2250 return ERR_PTR(-ENOMEM);
2251
Chris Wilson80b204b2016-10-28 13:58:58 +01002252 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002253 if (ret) {
2254 kfree(ppgtt);
2255 return ERR_PTR(ret);
2256 }
2257
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002258 trace_i915_ppgtt_create(&ppgtt->base);
2259
Daniel Vetter4d884702014-08-06 15:04:47 +02002260 return ppgtt;
2261}
2262
Chris Wilson0c7eeda2017-01-11 21:09:25 +00002263void i915_ppgtt_close(struct i915_address_space *vm)
2264{
2265 struct list_head *phases[] = {
2266 &vm->active_list,
2267 &vm->inactive_list,
2268 &vm->unbound_list,
2269 NULL,
2270 }, **phase;
2271
2272 GEM_BUG_ON(vm->closed);
2273 vm->closed = true;
2274
2275 for (phase = phases; *phase; phase++) {
2276 struct i915_vma *vma, *vn;
2277
2278 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2279 if (!i915_vma_is_closed(vma))
2280 i915_vma_close(vma);
2281 }
2282}
2283
Matthew Aulded9724d2016-11-17 21:04:10 +00002284void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002285{
2286 struct i915_hw_ppgtt *ppgtt =
2287 container_of(kref, struct i915_hw_ppgtt, ref);
2288
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002289 trace_i915_ppgtt_release(&ppgtt->base);
2290
Chris Wilson50e046b2016-08-04 07:52:46 +01002291 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002292 WARN_ON(!list_empty(&ppgtt->base.active_list));
2293 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002294 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002295
Matthew Aulded9724d2016-11-17 21:04:10 +00002296 i915_address_space_fini(&ppgtt->base);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002297
Daniel Vetteree960be2014-08-06 15:04:45 +02002298 ppgtt->base.cleanup(&ppgtt->base);
2299 kfree(ppgtt);
2300}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002301
Ben Widawskya81cc002013-01-18 12:30:31 -08002302/* Certain Gen5 chipsets require require idling the GPU before
2303 * unmapping anything from the GTT when VT-d is enabled.
2304 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002305static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002306{
2307#ifdef CONFIG_INTEL_IOMMU
2308 /* Query intel_iommu to see if we need the workaround. Presumably that
2309 * was loaded first.
2310 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002311 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002312 return true;
2313#endif
2314 return false;
2315}
2316
Chris Wilsondc979972016-05-10 14:10:04 +01002317void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002318{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002319 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302320 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002321
Chris Wilsondc979972016-05-10 14:10:04 +01002322 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002323 return;
2324
Akash Goel3b3f1652016-10-13 22:44:48 +05302325 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002326 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002327 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002328 if (fault_reg & RING_FAULT_VALID) {
2329 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002330 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002331 "\tAddress space: %s\n"
2332 "\tSource ID: %d\n"
2333 "\tType: %d\n",
2334 fault_reg & PAGE_MASK,
2335 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2336 RING_FAULT_SRCID(fault_reg),
2337 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002338 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002339 fault_reg & ~RING_FAULT_VALID);
2340 }
2341 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302342
2343 /* Engine specific init may not have been done till this point. */
2344 if (dev_priv->engine[RCS])
2345 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002346}
2347
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002348void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002349{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002350 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002351
2352 /* Don't bother messing with faults pre GEN6 as we have little
2353 * documentation supporting that it's a good idea.
2354 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002355 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002356 return;
2357
Chris Wilsondc979972016-05-10 14:10:04 +01002358 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002359
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002360 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002361
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002362 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002363}
2364
Chris Wilson03ac84f2016-10-28 13:58:36 +01002365int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2366 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002367{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002368 do {
2369 if (dma_map_sg(&obj->base.dev->pdev->dev,
2370 pages->sgl, pages->nents,
2371 PCI_DMA_BIDIRECTIONAL))
2372 return 0;
2373
2374 /* If the DMA remap fails, one cause can be that we have
2375 * too many objects pinned in a small remapping table,
2376 * such as swiotlb. Incrementally purge all other objects and
2377 * try again - if there are no more pages to remove from
2378 * the DMA remapper, i915_gem_shrink will return 0.
2379 */
2380 GEM_BUG_ON(obj->mm.pages == pages);
2381 } while (i915_gem_shrink(to_i915(obj->base.dev),
2382 obj->base.size >> PAGE_SHIFT,
2383 I915_SHRINK_BOUND |
2384 I915_SHRINK_UNBOUND |
2385 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002386
Chris Wilson03ac84f2016-10-28 13:58:36 +01002387 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002388}
2389
Daniel Vetter2c642b02015-04-14 17:35:26 +02002390static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002391{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002392 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002393}
2394
Chris Wilsond6473f52016-06-10 14:22:59 +05302395static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2396 dma_addr_t addr,
2397 uint64_t offset,
2398 enum i915_cache_level level,
2399 u32 unused)
2400{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002401 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302402 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002403 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302404
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002405 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302406
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002407 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302408}
2409
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002410static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2411 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002412 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302413 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002414{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002415 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002416 struct sgt_iter sgt_iter;
2417 gen8_pte_t __iomem *gtt_entries;
2418 gen8_pte_t gtt_entry;
2419 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002420 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002421
Dave Gordon85d12252016-05-20 11:54:06 +01002422 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2423
2424 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002425 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002426 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002427 }
2428
2429 /*
2430 * XXX: This serves as a posting read to make sure that the PTE has
2431 * actually been updated. There is some concern that even though
2432 * registers and PTEs are within the same BAR that they are potentially
2433 * of NUMA access patterns. Therefore, even with the way we assume
2434 * hardware should work, we must keep this posting read for paranoia.
2435 */
2436 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002437 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002438
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002439 /* This next bit makes the above posting read even more important. We
2440 * want to flush the TLBs only after we're certain all the PTE updates
2441 * have finished.
2442 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002443 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002444}
2445
Chris Wilsonc1403302015-11-18 15:19:39 +00002446struct insert_entries {
2447 struct i915_address_space *vm;
2448 struct sg_table *st;
2449 uint64_t start;
2450 enum i915_cache_level level;
2451 u32 flags;
2452};
2453
2454static int gen8_ggtt_insert_entries__cb(void *_arg)
2455{
2456 struct insert_entries *arg = _arg;
2457 gen8_ggtt_insert_entries(arg->vm, arg->st,
2458 arg->start, arg->level, arg->flags);
2459 return 0;
2460}
2461
2462static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2463 struct sg_table *st,
2464 uint64_t start,
2465 enum i915_cache_level level,
2466 u32 flags)
2467{
2468 struct insert_entries arg = { vm, st, start, level, flags };
2469 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2470}
2471
Chris Wilsond6473f52016-06-10 14:22:59 +05302472static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2473 dma_addr_t addr,
2474 uint64_t offset,
2475 enum i915_cache_level level,
2476 u32 flags)
2477{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002478 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302479 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002480 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302481
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002482 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302483
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002484 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302485}
2486
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002487/*
2488 * Binds an object into the global gtt with the specified cache level. The object
2489 * will be accessible to the GPU via commands whose operands reference offsets
2490 * within the global GTT as well as accessible by the GPU through the GMADR
2491 * mapped BAR (dev_priv->mm.gtt->gtt).
2492 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002493static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002494 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002495 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302496 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002497{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002498 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002499 struct sgt_iter sgt_iter;
2500 gen6_pte_t __iomem *gtt_entries;
2501 gen6_pte_t gtt_entry;
2502 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002503 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002504
Dave Gordon85d12252016-05-20 11:54:06 +01002505 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2506
2507 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002508 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002509 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002510 }
2511
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002512 /* XXX: This serves as a posting read to make sure that the PTE has
2513 * actually been updated. There is some concern that even though
2514 * registers and PTEs are within the same BAR that they are potentially
2515 * of NUMA access patterns. Therefore, even with the way we assume
2516 * hardware should work, we must keep this posting read for paranoia.
2517 */
Dave Gordon85d12252016-05-20 11:54:06 +01002518 if (i != 0)
2519 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002520
2521 /* This next bit makes the above posting read even more important. We
2522 * want to flush the TLBs only after we're certain all the PTE updates
2523 * have finished.
2524 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002525 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002526}
2527
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002528static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002529 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002530{
2531}
2532
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002533static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002534 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002535{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002536 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002537 unsigned first_entry = start >> PAGE_SHIFT;
2538 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002539 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002540 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2541 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002542 int i;
2543
2544 if (WARN(num_entries > max_entries,
2545 "First entry = %d; Num entries = %d (max=%d)\n",
2546 first_entry, num_entries, max_entries))
2547 num_entries = max_entries;
2548
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002549 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002550 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002551 for (i = 0; i < num_entries; i++)
2552 gen8_set_pte(&gtt_base[i], scratch_pte);
2553 readl(gtt_base);
2554}
2555
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002556static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002557 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002558 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002559{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002560 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002561 unsigned first_entry = start >> PAGE_SHIFT;
2562 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002563 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002564 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2565 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002566 int i;
2567
2568 if (WARN(num_entries > max_entries,
2569 "First entry = %d; Num entries = %d (max=%d)\n",
2570 first_entry, num_entries, max_entries))
2571 num_entries = max_entries;
2572
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002573 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002574 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002575
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002576 for (i = 0; i < num_entries; i++)
2577 iowrite32(scratch_pte, &gtt_base[i]);
2578 readl(gtt_base);
2579}
2580
Chris Wilsond6473f52016-06-10 14:22:59 +05302581static void i915_ggtt_insert_page(struct i915_address_space *vm,
2582 dma_addr_t addr,
2583 uint64_t offset,
2584 enum i915_cache_level cache_level,
2585 u32 unused)
2586{
Chris Wilsond6473f52016-06-10 14:22:59 +05302587 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2588 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302589
2590 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302591}
2592
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002593static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2594 struct sg_table *pages,
2595 uint64_t start,
2596 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002597{
2598 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2599 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2600
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002601 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002602
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002603}
2604
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002605static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002606 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002607 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002608{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002609 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002610}
2611
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002612static int ggtt_bind_vma(struct i915_vma *vma,
2613 enum i915_cache_level cache_level,
2614 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002615{
Chris Wilson49d73912016-11-29 09:50:08 +00002616 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002617 struct drm_i915_gem_object *obj = vma->obj;
2618 u32 pte_flags = 0;
2619 int ret;
2620
2621 ret = i915_get_ggtt_vma_pages(vma);
2622 if (ret)
2623 return ret;
2624
2625 /* Currently applicable only to VLV */
2626 if (obj->gt_ro)
2627 pte_flags |= PTE_READ_ONLY;
2628
Chris Wilson9c870d02016-10-24 13:42:15 +01002629 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002630 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002631 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002632 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002633
2634 /*
2635 * Without aliasing PPGTT there's no difference between
2636 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2637 * upgrade to both bound if we bind either to avoid double-binding.
2638 */
Chris Wilson3272db52016-08-04 16:32:32 +01002639 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002640
2641 return 0;
2642}
2643
2644static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2645 enum i915_cache_level cache_level,
2646 u32 flags)
2647{
Chris Wilson49d73912016-11-29 09:50:08 +00002648 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002649 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002650 int ret;
2651
2652 ret = i915_get_ggtt_vma_pages(vma);
2653 if (ret)
2654 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002655
Akash Goel24f3a8c2014-06-17 10:59:42 +05302656 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002657 pte_flags = 0;
2658 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002659 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302660
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002661
Chris Wilson3272db52016-08-04 16:32:32 +01002662 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002663 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002664 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002665 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002666 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002667 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002668 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002669
Chris Wilson3272db52016-08-04 16:32:32 +01002670 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002671 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002672 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002673 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002674 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002675 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002676
2677 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002678}
2679
2680static void ggtt_unbind_vma(struct i915_vma *vma)
2681{
Chris Wilson49d73912016-11-29 09:50:08 +00002682 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson9c870d02016-10-24 13:42:15 +01002683 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002684 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002685
Chris Wilson9c870d02016-10-24 13:42:15 +01002686 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2687 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002688 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002689 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002690 intel_runtime_pm_put(i915);
2691 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002692
Chris Wilson3272db52016-08-04 16:32:32 +01002693 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002694 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002695 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002696}
2697
Chris Wilson03ac84f2016-10-28 13:58:36 +01002698void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2699 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002700{
David Weinehall52a05c32016-08-22 13:32:44 +03002701 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2702 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002703 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002704
Chris Wilson307dc252016-08-05 10:14:12 +01002705 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002706 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002707 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2708 /* Wait a bit, in hopes it avoids the hang */
2709 udelay(10);
2710 }
2711 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002712
Chris Wilson03ac84f2016-10-28 13:58:36 +01002713 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002714}
Daniel Vetter644ec022012-03-26 09:45:40 +02002715
Chris Wilson45b186f2016-12-16 07:46:42 +00002716static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002717 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002718 u64 *start,
2719 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002720{
Chris Wilsona6508de2017-02-06 08:45:47 +00002721 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002722 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002723
Chris Wilsona6508de2017-02-06 08:45:47 +00002724 /* Also leave a space between the unallocated reserved node after the
2725 * GTT and any objects within the GTT, i.e. we use the color adjustment
2726 * to insert a guard page to prevent prefetches crossing over the
2727 * GTT boundary.
2728 */
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002729 node = list_next_entry(node, node_list);
Chris Wilsona6508de2017-02-06 08:45:47 +00002730 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002731 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002732}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002733
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002734int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002735{
Ben Widawskye78891c2013-01-25 16:41:04 -08002736 /* Let GEM Manage all of the aperture.
2737 *
2738 * However, leave one page at the end still bound to the scratch page.
2739 * There are a number of places where the hardware apparently prefetches
2740 * past the end of the object, and we've seen multiple hangs with the
2741 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2742 * aperture. One page should be enough to keep any prefetching inside
2743 * of the aperture.
2744 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002745 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002746 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002747 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002748 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002749 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002750
Zhi Wangb02d22a2016-06-16 08:06:59 -04002751 ret = intel_vgt_balloon(dev_priv);
2752 if (ret)
2753 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002754
Chris Wilson95374d72016-10-12 10:05:20 +01002755 /* Reserve a mappable slot for our lockless error capture */
2756 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2757 &ggtt->error_capture,
Chris Wilsonf51455d2017-01-10 14:47:34 +00002758 PAGE_SIZE, 0,
Chris Wilson85fd4f52016-12-05 14:29:36 +00002759 I915_COLOR_UNEVICTABLE,
Chris Wilson95374d72016-10-12 10:05:20 +01002760 0, ggtt->mappable_end,
2761 0, 0);
2762 if (ret)
2763 return ret;
2764
Chris Wilsoned2f3452012-11-15 11:32:19 +00002765 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002766 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002767 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2768 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002769 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002770 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002771 }
2772
2773 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002774 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002775 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002776
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002777 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002778 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002779 if (!ppgtt) {
2780 ret = -ENOMEM;
2781 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002782 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002783
Chris Wilson95374d72016-10-12 10:05:20 +01002784 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2785 if (ret)
2786 goto err_ppgtt;
2787
2788 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002789 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2790 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002791 if (ret)
2792 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002793 }
2794
2795 ppgtt->base.clear_range(&ppgtt->base,
2796 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002797 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002798
Daniel Vetterfa76da32014-08-06 20:19:54 +02002799 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002800 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2801 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002802 }
2803
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002804 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002805
2806err_ppgtt_cleanup:
2807 ppgtt->base.cleanup(&ppgtt->base);
2808err_ppgtt:
2809 kfree(ppgtt);
2810err:
2811 drm_mm_remove_node(&ggtt->error_capture);
2812 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002813}
2814
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002815/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002816 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002817 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002818 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002819void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002820{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002821 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002822
Daniel Vetter70e32542014-08-06 15:04:57 +02002823 if (dev_priv->mm.aliasing_ppgtt) {
2824 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002825 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002826 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002827 }
2828
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002829 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002830
Chris Wilson95374d72016-10-12 10:05:20 +01002831 if (drm_mm_node_allocated(&ggtt->error_capture))
2832 drm_mm_remove_node(&ggtt->error_capture);
2833
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002834 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002835 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002836
Matthew Aulded9724d2016-11-17 21:04:10 +00002837 mutex_lock(&dev_priv->drm.struct_mutex);
2838 i915_address_space_fini(&ggtt->base);
2839 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002840 }
2841
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002842 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002843
2844 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002845 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002846}
Daniel Vetter70e32542014-08-06 15:04:57 +02002847
Daniel Vetter2c642b02015-04-14 17:35:26 +02002848static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002849{
2850 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2851 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2852 return snb_gmch_ctl << 20;
2853}
2854
Daniel Vetter2c642b02015-04-14 17:35:26 +02002855static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002856{
2857 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2858 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2859 if (bdw_gmch_ctl)
2860 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002861
2862#ifdef CONFIG_X86_32
2863 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2864 if (bdw_gmch_ctl > 4)
2865 bdw_gmch_ctl = 4;
2866#endif
2867
Ben Widawsky9459d252013-11-03 16:53:55 -08002868 return bdw_gmch_ctl << 20;
2869}
2870
Daniel Vetter2c642b02015-04-14 17:35:26 +02002871static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002872{
2873 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2874 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2875
2876 if (gmch_ctrl)
2877 return 1 << (20 + gmch_ctrl);
2878
2879 return 0;
2880}
2881
Daniel Vetter2c642b02015-04-14 17:35:26 +02002882static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002883{
2884 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2885 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2886 return snb_gmch_ctl << 25; /* 32 MB units */
2887}
2888
Daniel Vetter2c642b02015-04-14 17:35:26 +02002889static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002890{
2891 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2892 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2893 return bdw_gmch_ctl << 25; /* 32 MB units */
2894}
2895
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002896static size_t chv_get_stolen_size(u16 gmch_ctrl)
2897{
2898 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2899 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2900
2901 /*
2902 * 0x0 to 0x10: 32MB increments starting at 0MB
2903 * 0x11 to 0x16: 4MB increments starting at 8MB
2904 * 0x17 to 0x1d: 4MB increments start at 36MB
2905 */
2906 if (gmch_ctrl < 0x11)
2907 return gmch_ctrl << 25;
2908 else if (gmch_ctrl < 0x17)
2909 return (gmch_ctrl - 0x11 + 2) << 22;
2910 else
2911 return (gmch_ctrl - 0x17 + 9) << 22;
2912}
2913
Damien Lespiau66375012014-01-09 18:02:46 +00002914static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2915{
2916 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2917 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2918
2919 if (gen9_gmch_ctl < 0xf0)
2920 return gen9_gmch_ctl << 25; /* 32 MB units */
2921 else
2922 /* 4MB increments starting at 0xf0 for 4MB */
2923 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2924}
2925
Chris Wilson34c998b2016-08-04 07:52:24 +01002926static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002927{
Chris Wilson49d73912016-11-29 09:50:08 +00002928 struct drm_i915_private *dev_priv = ggtt->base.i915;
2929 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002930 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002931 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002932
2933 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002934 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002935
Imre Deak2a073f892015-03-27 13:07:33 +02002936 /*
2937 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2938 * dropped. For WC mappings in general we have 64 byte burst writes
2939 * when the WC buffer is flushed, so we can't use it, but have to
2940 * resort to an uncached mapping. The WC issue is easily caught by the
2941 * readback check when writing GTT PTE entries.
2942 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002943 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002944 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002945 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002946 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002947 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002948 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002949 return -ENOMEM;
2950 }
2951
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002952 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002953 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002954 DRM_ERROR("Scratch setup failed\n");
2955 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002956 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002957 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002958 }
2959
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002960 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002961}
2962
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002963/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2964 * bits. When using advanced contexts each context stores its own PAT, but
2965 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002966static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002967{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002968 uint64_t pat;
2969
2970 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2971 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2972 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2973 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2974 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2975 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2976 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2977 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2978
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002979 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002980 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2981 * so RTL will always use the value corresponding to
2982 * pat_sel = 000".
2983 * So let's disable cache for GGTT to avoid screen corruptions.
2984 * MOCS still can be used though.
2985 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2986 * before this patch, i.e. the same uncached + snooping access
2987 * like on gen6/7 seems to be in effect.
2988 * - So this just fixes blitter/render access. Again it looks
2989 * like it's not just uncached access, but uncached + snooping.
2990 * So we can still hold onto all our assumptions wrt cpu
2991 * clflushing on LLC machines.
2992 */
2993 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2994
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002995 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2996 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002997 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2998 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002999}
3000
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003001static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3002{
3003 uint64_t pat;
3004
3005 /*
3006 * Map WB on BDW to snooped on CHV.
3007 *
3008 * Only the snoop bit has meaning for CHV, the rest is
3009 * ignored.
3010 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003011 * The hardware will never snoop for certain types of accesses:
3012 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3013 * - PPGTT page tables
3014 * - some other special cycles
3015 *
3016 * As with BDW, we also need to consider the following for GT accesses:
3017 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3018 * so RTL will always use the value corresponding to
3019 * pat_sel = 000".
3020 * Which means we must set the snoop bit in PAT entry 0
3021 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003022 */
3023 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3024 GEN8_PPAT(1, 0) |
3025 GEN8_PPAT(2, 0) |
3026 GEN8_PPAT(3, 0) |
3027 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3028 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3029 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3030 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3031
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003032 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3033 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003034}
3035
Chris Wilson34c998b2016-08-04 07:52:24 +01003036static void gen6_gmch_remove(struct i915_address_space *vm)
3037{
3038 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3039
3040 iounmap(ggtt->gsm);
Chris Wilson49d73912016-11-29 09:50:08 +00003041 cleanup_scratch_page(vm->i915, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003042}
3043
Joonas Lahtinend507d732016-03-18 10:42:58 +02003044static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003045{
Chris Wilson49d73912016-11-29 09:50:08 +00003046 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003047 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003048 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003049 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003050
3051 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003052 ggtt->mappable_base = pci_resource_start(pdev, 2);
3053 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003054
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003055 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3056 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003057
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003058 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003059
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003060 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003061 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003062 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003063 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003064 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003065 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003066 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003067 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003068 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003069 }
Ben Widawsky63340132013-11-04 19:32:22 -08003070
Chris Wilson34c998b2016-08-04 07:52:24 +01003071 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003072
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003073 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003074 chv_setup_private_ppat(dev_priv);
3075 else
3076 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003077
Chris Wilson34c998b2016-08-04 07:52:24 +01003078 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003079 ggtt->base.bind_vma = ggtt_bind_vma;
3080 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303081 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003082 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003083 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003084 ggtt->base.clear_range = gen8_ggtt_clear_range;
3085
3086 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3087 if (IS_CHERRYVIEW(dev_priv))
3088 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3089
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003090 ggtt->invalidate = gen6_ggtt_invalidate;
3091
Chris Wilson34c998b2016-08-04 07:52:24 +01003092 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003093}
3094
Joonas Lahtinend507d732016-03-18 10:42:58 +02003095static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003096{
Chris Wilson49d73912016-11-29 09:50:08 +00003097 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003098 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003099 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003100 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003101
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003102 ggtt->mappable_base = pci_resource_start(pdev, 2);
3103 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003104
Ben Widawskybaa09f52013-01-24 13:49:57 -08003105 /* 64/512MB is the current min/max we actually know of, but this is just
3106 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003107 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003108 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003109 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003110 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003111 }
3112
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003113 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3114 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3115 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003116
Joonas Lahtinend507d732016-03-18 10:42:58 +02003117 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003118
Chris Wilson34c998b2016-08-04 07:52:24 +01003119 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3120 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003121
Joonas Lahtinend507d732016-03-18 10:42:58 +02003122 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303123 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003124 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3125 ggtt->base.bind_vma = ggtt_bind_vma;
3126 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003127 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003128
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003129 ggtt->invalidate = gen6_ggtt_invalidate;
3130
Chris Wilson34c998b2016-08-04 07:52:24 +01003131 if (HAS_EDRAM(dev_priv))
3132 ggtt->base.pte_encode = iris_pte_encode;
3133 else if (IS_HASWELL(dev_priv))
3134 ggtt->base.pte_encode = hsw_pte_encode;
3135 else if (IS_VALLEYVIEW(dev_priv))
3136 ggtt->base.pte_encode = byt_pte_encode;
3137 else if (INTEL_GEN(dev_priv) >= 7)
3138 ggtt->base.pte_encode = ivb_pte_encode;
3139 else
3140 ggtt->base.pte_encode = snb_pte_encode;
3141
3142 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003143}
3144
Chris Wilson34c998b2016-08-04 07:52:24 +01003145static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003146{
Chris Wilson34c998b2016-08-04 07:52:24 +01003147 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003148}
3149
Joonas Lahtinend507d732016-03-18 10:42:58 +02003150static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003151{
Chris Wilson49d73912016-11-29 09:50:08 +00003152 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003153 int ret;
3154
Chris Wilson91c8a322016-07-05 10:40:23 +01003155 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003156 if (!ret) {
3157 DRM_ERROR("failed to set up gmch\n");
3158 return -EIO;
3159 }
3160
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003161 intel_gtt_get(&ggtt->base.total,
3162 &ggtt->stolen_size,
3163 &ggtt->mappable_base,
3164 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003165
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003166 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303167 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003168 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3169 ggtt->base.clear_range = i915_ggtt_clear_range;
3170 ggtt->base.bind_vma = ggtt_bind_vma;
3171 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003172 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003173
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003174 ggtt->invalidate = gmch_ggtt_invalidate;
3175
Joonas Lahtinend507d732016-03-18 10:42:58 +02003176 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003177 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3178
Ben Widawskybaa09f52013-01-24 13:49:57 -08003179 return 0;
3180}
3181
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003182/**
Chris Wilson0088e522016-08-04 07:52:21 +01003183 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003184 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003185 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003186int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003187{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003188 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003189 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003190
Chris Wilson49d73912016-11-29 09:50:08 +00003191 ggtt->base.i915 = dev_priv;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003192
Chris Wilson34c998b2016-08-04 07:52:24 +01003193 if (INTEL_GEN(dev_priv) <= 5)
3194 ret = i915_gmch_probe(ggtt);
3195 else if (INTEL_GEN(dev_priv) < 8)
3196 ret = gen6_gmch_probe(ggtt);
3197 else
3198 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003199 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003200 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003201
Chris Wilsondb9309a2017-01-05 15:30:23 +00003202 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3203 * This is easier than doing range restriction on the fly, as we
3204 * currently don't have any bits spare to pass in this upper
3205 * restriction!
3206 */
3207 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3208 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3209 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3210 }
3211
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003212 if ((ggtt->base.total - 1) >> 32) {
3213 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003214 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003215 ggtt->base.total >> 20);
3216 ggtt->base.total = 1ULL << 32;
3217 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3218 }
3219
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003220 if (ggtt->mappable_end > ggtt->base.total) {
3221 DRM_ERROR("mappable aperture extends past end of GGTT,"
3222 " aperture=%llx, total=%llx\n",
3223 ggtt->mappable_end, ggtt->base.total);
3224 ggtt->mappable_end = ggtt->base.total;
3225 }
3226
Ben Widawskybaa09f52013-01-24 13:49:57 -08003227 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003228 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003229 ggtt->base.total >> 20);
3230 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003231 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003232#ifdef CONFIG_INTEL_IOMMU
3233 if (intel_iommu_gfx_mapped)
3234 DRM_INFO("VT-d active for gfx access\n");
3235#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003236
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003237 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003238}
3239
3240/**
3241 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003242 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003243 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003244int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003245{
Chris Wilson0088e522016-08-04 07:52:21 +01003246 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3247 int ret;
3248
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003249 INIT_LIST_HEAD(&dev_priv->vm_list);
3250
Chris Wilsona6508de2017-02-06 08:45:47 +00003251 /* Note that we use page colouring to enforce a guard page at the
3252 * end of the address space. This is required as the CS may prefetch
3253 * beyond the end of the batch buffer, across the page boundary,
3254 * and beyond the end of the GTT if we do not provide a guard.
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003255 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003256 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson80b204b2016-10-28 13:58:58 +01003257 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsona6508de2017-02-06 08:45:47 +00003258 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003259 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003260 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003261
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003262 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3263 dev_priv->ggtt.mappable_base,
3264 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003265 ret = -EIO;
3266 goto out_gtt_cleanup;
3267 }
3268
3269 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3270
Chris Wilson0088e522016-08-04 07:52:21 +01003271 /*
3272 * Initialise stolen early so that we may reserve preallocated
3273 * objects for the BIOS to KMS transition.
3274 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003275 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003276 if (ret)
3277 goto out_gtt_cleanup;
3278
3279 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003280
3281out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003282 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003283 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003284}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003285
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003286int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003287{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003288 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003289 return -EIO;
3290
3291 return 0;
3292}
3293
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003294void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3295{
3296 i915->ggtt.invalidate = guc_ggtt_invalidate;
3297}
3298
3299void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3300{
3301 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3302}
3303
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003304void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003305{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003306 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003307 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003308
Chris Wilsondc979972016-05-10 14:10:04 +01003309 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003310
3311 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003312 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003313
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003314 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3315
3316 /* clflush objects bound into the GGTT and rebind them. */
3317 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003318 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003319 bool ggtt_bound = false;
3320 struct i915_vma *vma;
3321
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003322 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003323 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003324 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003325
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003326 if (!i915_vma_unbind(vma))
3327 continue;
3328
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003329 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3330 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003331 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003332 }
3333
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003334 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003335 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003336 }
3337
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003338 ggtt->base.closed = false;
3339
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003340 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003341 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003342 chv_setup_private_ppat(dev_priv);
3343 else
3344 bdw_setup_private_ppat(dev_priv);
3345
3346 return;
3347 }
3348
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003349 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003350 struct i915_address_space *vm;
3351
Daniel Vetterfa423312015-04-14 17:35:23 +02003352 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3353 /* TODO: Perhaps it shouldn't be gen6 specific */
3354
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003355 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003356
Chris Wilson2bfa9962016-08-04 07:52:25 +01003357 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003358 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003359 else
3360 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003361
3362 gen6_write_page_range(dev_priv, &ppgtt->pd,
3363 0, ppgtt->base.total);
3364 }
3365 }
3366
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003367 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003368}
3369
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003370static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003371rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003372 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003373 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003374 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003375{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003376 unsigned int column, row;
3377 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003378
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003379 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003380 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003381 for (row = 0; row < height; row++) {
3382 st->nents++;
3383 /* We don't need the pages, but need to initialize
3384 * the entries so the sg list can be happily traversed.
3385 * The only thing we need are DMA addresses.
3386 */
3387 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003388 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003389 sg_dma_len(sg) = PAGE_SIZE;
3390 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003391 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003392 }
3393 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003394
3395 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003396}
3397
3398static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003399intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003400 struct drm_i915_gem_object *obj)
3401{
Dave Gordon85d12252016-05-20 11:54:06 +01003402 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003403 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003404 struct sgt_iter sgt_iter;
3405 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003406 unsigned long i;
3407 dma_addr_t *page_addr_list;
3408 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003409 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003410 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003411
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003412 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003413 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003414 sizeof(dma_addr_t),
3415 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003416 if (!page_addr_list)
3417 return ERR_PTR(ret);
3418
3419 /* Allocate target SG list. */
3420 st = kmalloc(sizeof(*st), GFP_KERNEL);
3421 if (!st)
3422 goto err_st_alloc;
3423
Ville Syrjälä6687c902015-09-15 13:16:41 +03003424 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003425 if (ret)
3426 goto err_sg_alloc;
3427
3428 /* Populate source page list from the object. */
3429 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003430 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003431 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003432
Dave Gordon85d12252016-05-20 11:54:06 +01003433 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003434 st->nents = 0;
3435 sg = st->sgl;
3436
Ville Syrjälä6687c902015-09-15 13:16:41 +03003437 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3438 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3439 rot_info->plane[i].width, rot_info->plane[i].height,
3440 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003441 }
3442
Ville Syrjälä6687c902015-09-15 13:16:41 +03003443 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3444 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003445
3446 drm_free_large(page_addr_list);
3447
3448 return st;
3449
3450err_sg_alloc:
3451 kfree(st);
3452err_st_alloc:
3453 drm_free_large(page_addr_list);
3454
Ville Syrjälä6687c902015-09-15 13:16:41 +03003455 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3456 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3457
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003458 return ERR_PTR(ret);
3459}
3460
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003461static struct sg_table *
3462intel_partial_pages(const struct i915_ggtt_view *view,
3463 struct drm_i915_gem_object *obj)
3464{
3465 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003466 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003467 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003468 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003469 int ret = -ENOMEM;
3470
3471 st = kmalloc(sizeof(*st), GFP_KERNEL);
3472 if (!st)
3473 goto err_st_alloc;
3474
Chris Wilsond2a84a72016-10-28 13:58:34 +01003475 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003476 if (ret)
3477 goto err_sg_alloc;
3478
Chris Wilson8bab11932017-01-14 00:28:25 +00003479 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003480 GEM_BUG_ON(!iter);
3481
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003482 sg = st->sgl;
3483 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003484 do {
3485 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003486
Chris Wilsond2a84a72016-10-28 13:58:34 +01003487 len = min(iter->length - (offset << PAGE_SHIFT),
3488 count << PAGE_SHIFT);
3489 sg_set_page(sg, NULL, len, 0);
3490 sg_dma_address(sg) =
3491 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3492 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003493
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003494 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003495 count -= len >> PAGE_SHIFT;
3496 if (count == 0) {
3497 sg_mark_end(sg);
3498 return st;
3499 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003500
Chris Wilsond2a84a72016-10-28 13:58:34 +01003501 sg = __sg_next(sg);
3502 iter = __sg_next(iter);
3503 offset = 0;
3504 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003505
3506err_sg_alloc:
3507 kfree(st);
3508err_st_alloc:
3509 return ERR_PTR(ret);
3510}
3511
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003512static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003513i915_get_ggtt_vma_pages(struct i915_vma *vma)
3514{
3515 int ret = 0;
3516
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003517 /* The vma->pages are only valid within the lifespan of the borrowed
3518 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3519 * must be the vma->pages. A simple rule is that vma->pages must only
3520 * be accessed when the obj->mm.pages are pinned.
3521 */
3522 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3523
Chris Wilson247177d2016-08-15 10:48:47 +01003524 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003525 return 0;
3526
3527 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003528 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003529 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003530 vma->pages =
Chris Wilson8bab11932017-01-14 00:28:25 +00003531 intel_rotate_fb_obj_pages(&vma->ggtt_view.rotated,
3532 vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003533 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003534 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003535 else
3536 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3537 vma->ggtt_view.type);
3538
Chris Wilson247177d2016-08-15 10:48:47 +01003539 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003540 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003541 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003542 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003543 } else if (IS_ERR(vma->pages)) {
3544 ret = PTR_ERR(vma->pages);
3545 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003546 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3547 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003548 }
3549
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003550 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003551}
3552
Chris Wilsone007b192017-01-11 11:23:10 +00003553/**
Chris Wilson625d9882017-01-11 11:23:11 +00003554 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003555 * @vm: the &struct i915_address_space
3556 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3557 * @size: how much space to allocate inside the GTT,
3558 * must be #I915_GTT_PAGE_SIZE aligned
3559 * @offset: where to insert inside the GTT,
3560 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3561 * (@offset + @size) must fit within the address space
3562 * @color: color to apply to node, if this node is not from a VMA,
3563 * color must be #I915_COLOR_UNEVICTABLE
3564 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003565 *
3566 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3567 * the address space (using @size and @color). If the @node does not fit, it
3568 * tries to evict any overlapping nodes from the GTT, including any
3569 * neighbouring nodes if the colors do not match (to ensure guard pages between
3570 * differing domains). See i915_gem_evict_for_node() for the gory details
3571 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3572 * evicting active overlapping objects, and any overlapping node that is pinned
3573 * or marked as unevictable will also result in failure.
3574 *
3575 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3576 * asked to wait for eviction and interrupted.
3577 */
3578int i915_gem_gtt_reserve(struct i915_address_space *vm,
3579 struct drm_mm_node *node,
3580 u64 size, u64 offset, unsigned long color,
3581 unsigned int flags)
3582{
3583 int err;
3584
3585 GEM_BUG_ON(!size);
3586 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3587 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3588 GEM_BUG_ON(range_overflows(offset, size, vm->total));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003589 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003590 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilson625d9882017-01-11 11:23:11 +00003591
3592 node->size = size;
3593 node->start = offset;
3594 node->color = color;
3595
3596 err = drm_mm_reserve_node(&vm->mm, node);
3597 if (err != -ENOSPC)
3598 return err;
3599
3600 err = i915_gem_evict_for_node(vm, node, flags);
3601 if (err == 0)
3602 err = drm_mm_reserve_node(&vm->mm, node);
3603
3604 return err;
3605}
3606
Chris Wilson606fec92017-01-11 11:23:12 +00003607static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3608{
3609 u64 range, addr;
3610
3611 GEM_BUG_ON(range_overflows(start, len, end));
3612 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3613
3614 range = round_down(end - len, align) - round_up(start, align);
3615 if (range) {
3616 if (sizeof(unsigned long) == sizeof(u64)) {
3617 addr = get_random_long();
3618 } else {
3619 addr = get_random_int();
3620 if (range > U32_MAX) {
3621 addr <<= 32;
3622 addr |= get_random_int();
3623 }
3624 }
3625 div64_u64_rem(addr, range, &addr);
3626 start += addr;
3627 }
3628
3629 return round_up(start, align);
3630}
3631
Chris Wilson625d9882017-01-11 11:23:11 +00003632/**
Chris Wilsone007b192017-01-11 11:23:10 +00003633 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003634 * @vm: the &struct i915_address_space
3635 * @node: the &struct drm_mm_node (typically i915_vma.node)
3636 * @size: how much space to allocate inside the GTT,
3637 * must be #I915_GTT_PAGE_SIZE aligned
3638 * @alignment: required alignment of starting offset, may be 0 but
3639 * if specified, this must be a power-of-two and at least
3640 * #I915_GTT_MIN_ALIGNMENT
3641 * @color: color to apply to node
3642 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003643 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003644 * @end: end of any range restriction inside GTT (U64_MAX for all),
3645 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3646 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003647 *
3648 * i915_gem_gtt_insert() first searches for an available hole into which
3649 * is can insert the node. The hole address is aligned to @alignment and
3650 * its @size must then fit entirely within the [@start, @end] bounds. The
3651 * nodes on either side of the hole must match @color, or else a guard page
3652 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003653 * suitable hole is found, first a victim is randomly selected and tested
3654 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003655 * is scanned to find the first set of replacement nodes to create the hole.
3656 * Those old overlapping nodes are evicted from the GTT (and so must be
3657 * rebound before any future use). Any node that is currently pinned cannot
3658 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3659 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3660 * searching for an eviction candidate. See i915_gem_evict_something() for
3661 * the gory details on the eviction algorithm.
3662 *
3663 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3664 * asked to wait for eviction and interrupted.
3665 */
3666int i915_gem_gtt_insert(struct i915_address_space *vm,
3667 struct drm_mm_node *node,
3668 u64 size, u64 alignment, unsigned long color,
3669 u64 start, u64 end, unsigned int flags)
3670{
3671 u32 search_flag, alloc_flag;
Chris Wilson606fec92017-01-11 11:23:12 +00003672 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003673 int err;
3674
3675 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3676 GEM_BUG_ON(!size);
3677 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3678 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3679 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3680 GEM_BUG_ON(start >= end);
3681 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3682 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
Chris Wilson3fec7ec2017-01-15 13:47:46 +00003683 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
Chris Wilson9734ad12017-01-15 17:27:40 +00003684 GEM_BUG_ON(drm_mm_node_allocated(node));
Chris Wilsone007b192017-01-11 11:23:10 +00003685
3686 if (unlikely(range_overflows(start, size, end)))
3687 return -ENOSPC;
3688
3689 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3690 return -ENOSPC;
3691
3692 if (flags & PIN_HIGH) {
3693 search_flag = DRM_MM_SEARCH_BELOW;
3694 alloc_flag = DRM_MM_CREATE_TOP;
3695 } else {
3696 search_flag = DRM_MM_SEARCH_DEFAULT;
3697 alloc_flag = DRM_MM_CREATE_DEFAULT;
3698 }
3699
3700 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3701 * so we know that we always have a minimum alignment of 4096.
3702 * The drm_mm range manager is optimised to return results
3703 * with zero alignment, so where possible use the optimal
3704 * path.
3705 */
3706 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3707 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3708 alignment = 0;
3709
3710 err = drm_mm_insert_node_in_range_generic(&vm->mm, node,
3711 size, alignment, color,
3712 start, end,
3713 search_flag, alloc_flag);
3714 if (err != -ENOSPC)
3715 return err;
3716
Chris Wilson606fec92017-01-11 11:23:12 +00003717 /* No free space, pick a slot at random.
3718 *
3719 * There is a pathological case here using a GTT shared between
3720 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3721 *
3722 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3723 * (64k objects) (448k objects)
3724 *
3725 * Now imagine that the eviction LRU is ordered top-down (just because
3726 * pathology meets real life), and that we need to evict an object to
3727 * make room inside the aperture. The eviction scan then has to walk
3728 * the 448k list before it finds one within range. And now imagine that
3729 * it has to search for a new hole between every byte inside the memcpy,
3730 * for several simultaneous clients.
3731 *
3732 * On a full-ppgtt system, if we have run out of available space, there
3733 * will be lots and lots of objects in the eviction list! Again,
3734 * searching that LRU list may be slow if we are also applying any
3735 * range restrictions (e.g. restriction to low 4GiB) and so, for
3736 * simplicity and similarilty between different GTT, try the single
3737 * random replacement first.
3738 */
3739 offset = random_offset(start, end,
3740 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3741 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3742 if (err != -ENOSPC)
3743 return err;
3744
3745 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003746 err = i915_gem_evict_something(vm, size, alignment, color,
3747 start, end, flags);
3748 if (err)
3749 return err;
3750
3751 search_flag = DRM_MM_SEARCH_DEFAULT;
3752 return drm_mm_insert_node_in_range_generic(&vm->mm, node,
3753 size, alignment, color,
3754 start, end,
3755 search_flag, alloc_flag);
3756}