blob: e24b961c30c657a827e02f267861aa3a2397aeed [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
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200106const struct i915_ggtt_view i915_ggtt_view_normal = {
107 .type = I915_GGTT_VIEW_NORMAL,
108};
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200109const struct i915_ggtt_view i915_ggtt_view_rotated = {
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200110 .type = I915_GGTT_VIEW_ROTATED,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200111};
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000112
Chris Wilson7c3f86b2017-01-12 11:00:49 +0000113static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
114{
115 /* Note that as an uncached mmio write, this should flush the
116 * WCB of the writes into the GGTT before it triggers the invalidate.
117 */
118 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
119}
120
121static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
122{
123 gen6_ggtt_invalidate(dev_priv);
124 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
125}
126
127static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
128{
129 intel_gtt_chipset_flush();
130}
131
132static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
133{
134 i915->ggtt.invalidate(i915);
135}
136
Chris Wilsonc0336662016-05-06 15:40:21 +0100137int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
138 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200139{
Chris Wilson1893a712014-09-19 11:56:27 +0100140 bool has_aliasing_ppgtt;
141 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100142 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100143
Michel Thierry9e1d0e62016-12-05 17:57:03 -0800144 has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
145 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
146 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100147
Zhi Wange320d402016-09-06 12:04:12 +0800148 if (intel_vgpu_active(dev_priv)) {
149 /* emulation is too hard */
150 has_full_ppgtt = false;
151 has_full_48bit_ppgtt = false;
152 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800153
Chris Wilson0e4ca102016-04-29 13:18:22 +0100154 if (!has_aliasing_ppgtt)
155 return 0;
156
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000157 /*
158 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
159 * execlists, the sole mechanism available to submit work.
160 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100161 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200162 return 0;
163
164 if (enable_ppgtt == 1)
165 return 1;
166
Chris Wilson1893a712014-09-19 11:56:27 +0100167 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200168 return 2;
169
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100170 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
171 return 3;
172
Daniel Vetter93a25a92014-03-06 09:40:43 +0100173#ifdef CONFIG_INTEL_IOMMU
174 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100175 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100176 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200177 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100178 }
179#endif
180
Jesse Barnes62942ed2014-06-13 09:28:33 -0700181 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100182 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700183 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
184 return 0;
185 }
186
Zhi Wange320d402016-09-06 12:04:12 +0800187 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100188 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000189 else
190 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100191}
192
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200193static int ppgtt_bind_vma(struct i915_vma *vma,
194 enum i915_cache_level cache_level,
195 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200196{
197 u32 pte_flags = 0;
198
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100199 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100200
Daniel Vetter47552652015-04-14 17:35:24 +0200201 /* Currently applicable only to VLV */
202 if (vma->obj->gt_ro)
203 pte_flags |= PTE_READ_ONLY;
204
Chris Wilson247177d2016-08-15 10:48:47 +0100205 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200206 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200207
208 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200209}
210
211static void ppgtt_unbind_vma(struct i915_vma *vma)
212{
213 vma->vm->clear_range(vma->vm,
214 vma->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200215 vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200216}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800217
Daniel Vetter2c642b02015-04-14 17:35:26 +0200218static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200219 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700220{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200221 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700222 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300223
224 switch (level) {
225 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800226 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300227 break;
228 case I915_CACHE_WT:
229 pte |= PPAT_DISPLAY_ELLC_INDEX;
230 break;
231 default:
232 pte |= PPAT_CACHED_INDEX;
233 break;
234 }
235
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700236 return pte;
237}
238
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300239static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
240 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800241{
Michel Thierry07749ef2015-03-16 16:00:54 +0000242 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800243 pde |= addr;
244 if (level != I915_CACHE_NONE)
245 pde |= PPAT_CACHED_PDE_INDEX;
246 else
247 pde |= PPAT_UNCACHED_INDEX;
248 return pde;
249}
250
Michel Thierry762d9932015-07-30 11:05:29 +0100251#define gen8_pdpe_encode gen8_pde_encode
252#define gen8_pml4e_encode gen8_pde_encode
253
Michel Thierry07749ef2015-03-16 16:00:54 +0000254static gen6_pte_t snb_pte_encode(dma_addr_t addr,
255 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200256 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700257{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200258 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700259 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700260
261 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100262 case I915_CACHE_L3_LLC:
263 case I915_CACHE_LLC:
264 pte |= GEN6_PTE_CACHE_LLC;
265 break;
266 case I915_CACHE_NONE:
267 pte |= GEN6_PTE_UNCACHED;
268 break;
269 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100270 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100271 }
272
273 return pte;
274}
275
Michel Thierry07749ef2015-03-16 16:00:54 +0000276static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
277 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200278 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100279{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200280 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100281 pte |= GEN6_PTE_ADDR_ENCODE(addr);
282
283 switch (level) {
284 case I915_CACHE_L3_LLC:
285 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700286 break;
287 case I915_CACHE_LLC:
288 pte |= GEN6_PTE_CACHE_LLC;
289 break;
290 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700291 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700292 break;
293 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100294 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700295 }
296
Ben Widawsky54d12522012-09-24 16:44:32 -0700297 return pte;
298}
299
Michel Thierry07749ef2015-03-16 16:00:54 +0000300static gen6_pte_t byt_pte_encode(dma_addr_t addr,
301 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200302 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700303{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200304 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700305 pte |= GEN6_PTE_ADDR_ENCODE(addr);
306
Akash Goel24f3a8c2014-06-17 10:59:42 +0530307 if (!(flags & PTE_READ_ONLY))
308 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700309
310 if (level != I915_CACHE_NONE)
311 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
312
313 return pte;
314}
315
Michel Thierry07749ef2015-03-16 16:00:54 +0000316static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
317 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200318 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700319{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200320 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700321 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700322
323 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700324 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700325
326 return pte;
327}
328
Michel Thierry07749ef2015-03-16 16:00:54 +0000329static gen6_pte_t iris_pte_encode(dma_addr_t addr,
330 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200331 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700332{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200333 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700334 pte |= HSW_PTE_ADDR_ENCODE(addr);
335
Chris Wilson651d7942013-08-08 14:41:10 +0100336 switch (level) {
337 case I915_CACHE_NONE:
338 break;
339 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000340 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100341 break;
342 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000343 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100344 break;
345 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700346
347 return pte;
348}
349
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000350static int __setup_page_dma(struct drm_i915_private *dev_priv,
Mika Kuoppalac114f762015-06-25 18:35:13 +0300351 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000352{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000353 struct device *kdev = &dev_priv->drm.pdev->dev;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000354
Mika Kuoppalac114f762015-06-25 18:35:13 +0300355 p->page = alloc_page(flags);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300356 if (!p->page)
Michel Thierry1266cdb2015-03-24 17:06:33 +0000357 return -ENOMEM;
358
David Weinehallc49d13e2016-08-22 13:32:42 +0300359 p->daddr = dma_map_page(kdev,
Chris Wilsonf51455d2017-01-10 14:47:34 +0000360 p->page, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300361
David Weinehallc49d13e2016-08-22 13:32:42 +0300362 if (dma_mapping_error(kdev, p->daddr)) {
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300363 __free_page(p->page);
364 return -EINVAL;
365 }
366
Michel Thierry1266cdb2015-03-24 17:06:33 +0000367 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000368}
369
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000370static int setup_page_dma(struct drm_i915_private *dev_priv,
371 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300372{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000373 return __setup_page_dma(dev_priv, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300374}
375
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000376static void cleanup_page_dma(struct drm_i915_private *dev_priv,
377 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300378{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000379 struct pci_dev *pdev = dev_priv->drm.pdev;
David Weinehall52a05c32016-08-22 13:32:44 +0300380
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300381 if (WARN_ON(!p->page))
382 return;
383
Chris Wilsonf51455d2017-01-10 14:47:34 +0000384 dma_unmap_page(&pdev->dev, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300385 __free_page(p->page);
386 memset(p, 0, sizeof(*p));
387}
388
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300389static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300390{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300391 return kmap_atomic(p->page);
392}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300393
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300394/* We use the flushing unmap only with ppgtt structures:
395 * page directories, page tables and scratch pages.
396 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100397static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300398{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300399 /* There are only few exceptions for gen >=6. chv and bxt.
400 * And we are not sure about the latter so play safe for now.
401 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200402 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300403 drm_clflush_virt_range(vaddr, PAGE_SIZE);
404
405 kunmap_atomic(vaddr);
406}
407
Mika Kuoppala567047b2015-06-25 18:35:12 +0300408#define kmap_px(px) kmap_page_dma(px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100409#define kunmap_px(ppgtt, vaddr) \
Chris Wilson49d73912016-11-29 09:50:08 +0000410 kunmap_page_dma((ppgtt)->base.i915, (vaddr))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300411
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000412#define setup_px(dev_priv, px) setup_page_dma((dev_priv), px_base(px))
413#define cleanup_px(dev_priv, px) cleanup_page_dma((dev_priv), px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100414#define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
415#define fill32_px(dev_priv, px, v) \
416 fill_page_dma_32((dev_priv), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300417
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100418static void fill_page_dma(struct drm_i915_private *dev_priv,
419 struct i915_page_dma *p, const uint64_t val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300420{
421 int i;
422 uint64_t * const vaddr = kmap_page_dma(p);
423
424 for (i = 0; i < 512; i++)
425 vaddr[i] = val;
426
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100427 kunmap_page_dma(dev_priv, vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300428}
429
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100430static void fill_page_dma_32(struct drm_i915_private *dev_priv,
431 struct i915_page_dma *p, const uint32_t val32)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300432{
433 uint64_t v = val32;
434
435 v = v << 32 | val32;
436
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100437 fill_page_dma(dev_priv, p, v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300438}
439
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100440static int
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000441setup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100442 struct i915_page_dma *scratch,
443 gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300444{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000445 return __setup_page_dma(dev_priv, scratch, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300446}
447
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000448static void cleanup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100449 struct i915_page_dma *scratch)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300450{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000451 cleanup_page_dma(dev_priv, scratch);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300452}
453
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000454static struct i915_page_table *alloc_pt(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000455{
Michel Thierryec565b32015-04-08 12:13:23 +0100456 struct i915_page_table *pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000457 const size_t count = INTEL_GEN(dev_priv) >= 8 ? GEN8_PTES : GEN6_PTES;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000458 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000459
460 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
461 if (!pt)
462 return ERR_PTR(-ENOMEM);
463
Ben Widawsky678d96f2015-03-16 16:00:56 +0000464 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
465 GFP_KERNEL);
466
467 if (!pt->used_ptes)
468 goto fail_bitmap;
469
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000470 ret = setup_px(dev_priv, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000471 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300472 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000473
474 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000475
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300476fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000477 kfree(pt->used_ptes);
478fail_bitmap:
479 kfree(pt);
480
481 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000482}
483
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000484static void free_pt(struct drm_i915_private *dev_priv,
485 struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000486{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000487 cleanup_px(dev_priv, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300488 kfree(pt->used_ptes);
489 kfree(pt);
490}
491
492static void gen8_initialize_pt(struct i915_address_space *vm,
493 struct i915_page_table *pt)
494{
495 gen8_pte_t scratch_pte;
496
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100497 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200498 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300499
Chris Wilson49d73912016-11-29 09:50:08 +0000500 fill_px(vm->i915, pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300501}
502
503static void gen6_initialize_pt(struct i915_address_space *vm,
504 struct i915_page_table *pt)
505{
506 gen6_pte_t scratch_pte;
507
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100508 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300509
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100510 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200511 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300512
Chris Wilson49d73912016-11-29 09:50:08 +0000513 fill32_px(vm->i915, pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000514}
515
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000516static struct i915_page_directory *alloc_pd(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000517{
Michel Thierryec565b32015-04-08 12:13:23 +0100518 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100519 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000520
521 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
522 if (!pd)
523 return ERR_PTR(-ENOMEM);
524
Michel Thierry33c88192015-04-08 12:13:33 +0100525 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
526 sizeof(*pd->used_pdes), GFP_KERNEL);
527 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300528 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100529
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000530 ret = setup_px(dev_priv, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100531 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300532 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100533
Ben Widawsky06fda602015-02-24 16:22:36 +0000534 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100535
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300536fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100537 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300538fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100539 kfree(pd);
540
541 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000542}
543
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000544static void free_pd(struct drm_i915_private *dev_priv,
545 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300546{
547 if (px_page(pd)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000548 cleanup_px(dev_priv, pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300549 kfree(pd->used_pdes);
550 kfree(pd);
551 }
552}
553
554static void gen8_initialize_pd(struct i915_address_space *vm,
555 struct i915_page_directory *pd)
556{
557 gen8_pde_t scratch_pde;
558
559 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
560
Chris Wilson49d73912016-11-29 09:50:08 +0000561 fill_px(vm->i915, pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300562}
563
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000564static int __pdp_init(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100565 struct i915_page_directory_pointer *pdp)
566{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000567 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Michel Thierry6ac18502015-07-29 17:23:46 +0100568
569 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
570 sizeof(unsigned long),
571 GFP_KERNEL);
572 if (!pdp->used_pdpes)
573 return -ENOMEM;
574
575 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
576 GFP_KERNEL);
577 if (!pdp->page_directory) {
578 kfree(pdp->used_pdpes);
579 /* the PDP might be the statically allocated top level. Keep it
580 * as clean as possible */
581 pdp->used_pdpes = NULL;
582 return -ENOMEM;
583 }
584
585 return 0;
586}
587
588static void __pdp_fini(struct i915_page_directory_pointer *pdp)
589{
590 kfree(pdp->used_pdpes);
591 kfree(pdp->page_directory);
592 pdp->page_directory = NULL;
593}
594
Michel Thierry762d9932015-07-30 11:05:29 +0100595static struct
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000596i915_page_directory_pointer *alloc_pdp(struct drm_i915_private *dev_priv)
Michel Thierry762d9932015-07-30 11:05:29 +0100597{
598 struct i915_page_directory_pointer *pdp;
599 int ret = -ENOMEM;
600
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000601 WARN_ON(!USES_FULL_48BIT_PPGTT(dev_priv));
Michel Thierry762d9932015-07-30 11:05:29 +0100602
603 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
604 if (!pdp)
605 return ERR_PTR(-ENOMEM);
606
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000607 ret = __pdp_init(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100608 if (ret)
609 goto fail_bitmap;
610
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000611 ret = setup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100612 if (ret)
613 goto fail_page_m;
614
615 return pdp;
616
617fail_page_m:
618 __pdp_fini(pdp);
619fail_bitmap:
620 kfree(pdp);
621
622 return ERR_PTR(ret);
623}
624
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000625static void free_pdp(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100626 struct i915_page_directory_pointer *pdp)
627{
628 __pdp_fini(pdp);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000629 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
630 cleanup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100631 kfree(pdp);
632 }
633}
634
Michel Thierry69ab76f2015-07-29 17:23:55 +0100635static void gen8_initialize_pdp(struct i915_address_space *vm,
636 struct i915_page_directory_pointer *pdp)
637{
638 gen8_ppgtt_pdpe_t scratch_pdpe;
639
640 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
641
Chris Wilson49d73912016-11-29 09:50:08 +0000642 fill_px(vm->i915, pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100643}
644
645static void gen8_initialize_pml4(struct i915_address_space *vm,
646 struct i915_pml4 *pml4)
647{
648 gen8_ppgtt_pml4e_t scratch_pml4e;
649
650 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
651 I915_CACHE_LLC);
652
Chris Wilson49d73912016-11-29 09:50:08 +0000653 fill_px(vm->i915, pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100654}
655
Michel Thierry762d9932015-07-30 11:05:29 +0100656static void
Matthew Auld5c693b22016-12-13 16:05:10 +0000657gen8_setup_pdpe(struct i915_hw_ppgtt *ppgtt,
658 struct i915_page_directory_pointer *pdp,
659 struct i915_page_directory *pd,
660 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100661{
662 gen8_ppgtt_pdpe_t *page_directorypo;
663
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000664 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
Michel Thierry762d9932015-07-30 11:05:29 +0100665 return;
666
667 page_directorypo = kmap_px(pdp);
668 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
669 kunmap_px(ppgtt, page_directorypo);
670}
671
672static void
Matthew Auld56843102016-12-13 16:05:11 +0000673gen8_setup_pml4e(struct i915_hw_ppgtt *ppgtt,
674 struct i915_pml4 *pml4,
675 struct i915_page_directory_pointer *pdp,
676 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100677{
678 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
679
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000680 WARN_ON(!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)));
Michel Thierry762d9932015-07-30 11:05:29 +0100681 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
682 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100683}
684
Ben Widawsky94e409c2013-11-04 22:29:36 -0800685/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100686static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100687 unsigned entry,
688 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800689{
Chris Wilson7e37f882016-08-02 22:50:21 +0100690 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000691 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800692 int ret;
693
694 BUG_ON(entry >= 4);
695
John Harrison5fb9de12015-05-29 17:44:07 +0100696 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800697 if (ret)
698 return ret;
699
Chris Wilsonb5321f32016-08-02 22:50:18 +0100700 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
701 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
702 intel_ring_emit(ring, upper_32_bits(addr));
703 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
704 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
705 intel_ring_emit(ring, lower_32_bits(addr));
706 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800707
708 return 0;
709}
710
Michel Thierry2dba3232015-07-30 11:06:23 +0100711static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
712 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800713{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800714 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800715
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100716 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300717 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
718
John Harrisone85b26d2015-05-29 17:43:56 +0100719 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800720 if (ret)
721 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800722 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800723
Ben Widawskyeeb94882013-12-06 14:11:10 -0800724 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800725}
726
Michel Thierry2dba3232015-07-30 11:06:23 +0100727static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
728 struct drm_i915_gem_request *req)
729{
730 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
731}
732
Mika Kuoppalafce93752016-10-31 17:24:46 +0200733/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
734 * the page table structures, we mark them dirty so that
735 * context switching/execlist queuing code takes extra steps
736 * to ensure that tlbs are flushed.
737 */
738static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
739{
Chris Wilson49d73912016-11-29 09:50:08 +0000740 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200741}
742
Michał Winiarski2ce51792016-10-13 14:02:42 +0200743/* Removes entries from a single page table, releasing it if it's empty.
744 * Caller can use the return value to update higher-level entries.
745 */
746static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200747 struct i915_page_table *pt,
748 uint64_t start,
749 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700750{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300751 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200752 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200753 unsigned int pte = gen8_pte_index(start);
754 unsigned int pte_end = pte + num_entries;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200755 gen8_pte_t *pt_vaddr;
756 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
757 I915_CACHE_LLC);
758
759 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200760 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700761
Mika Kuoppala37c63932016-11-01 15:27:36 +0200762 GEM_BUG_ON(pte_end > GEN8_PTES);
763
764 bitmap_clear(pt->used_ptes, pte, num_entries);
Ben Widawsky06fda602015-02-24 16:22:36 +0000765
Zhi Wanga18dbba2016-11-29 14:55:16 +0800766 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200767 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200768
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200769 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000770
Mika Kuoppala37c63932016-11-01 15:27:36 +0200771 while (pte < pte_end)
772 pt_vaddr[pte++] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000773
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200774 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200775
776 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200777}
778
Michał Winiarski2ce51792016-10-13 14:02:42 +0200779/* Removes entries from a single page dir, releasing it if it's empty.
780 * Caller can use the return value to update higher-level entries
781 */
782static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200783 struct i915_page_directory *pd,
784 uint64_t start,
785 uint64_t length)
786{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200787 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200788 struct i915_page_table *pt;
789 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200790 gen8_pde_t *pde_vaddr;
791 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
792 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200793
794 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000795 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100796 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000797
Michał Winiarski2ce51792016-10-13 14:02:42 +0200798 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
799 __clear_bit(pde, pd->used_pdes);
800 pde_vaddr = kmap_px(pd);
801 pde_vaddr[pde] = scratch_pde;
802 kunmap_px(ppgtt, pde_vaddr);
Chris Wilson49d73912016-11-29 09:50:08 +0000803 free_pt(vm->i915, pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200804 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200805 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200806
Zhi Wanga18dbba2016-11-29 14:55:16 +0800807 if (bitmap_empty(pd->used_pdes, I915_PDES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200808 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200809
810 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200811}
Ben Widawsky06fda602015-02-24 16:22:36 +0000812
Michał Winiarski2ce51792016-10-13 14:02:42 +0200813/* Removes entries from a single page dir pointer, releasing it if it's empty.
814 * Caller can use the return value to update higher-level entries
815 */
816static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200817 struct i915_page_directory_pointer *pdp,
818 uint64_t start,
819 uint64_t length)
820{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200821 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200822 struct i915_page_directory *pd;
823 uint64_t pdpe;
824
825 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
826 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100827 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000828
Michał Winiarski2ce51792016-10-13 14:02:42 +0200829 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
830 __clear_bit(pdpe, pdp->used_pdpes);
Matthew Auld9e65a372016-12-13 16:05:12 +0000831 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
Chris Wilson49d73912016-11-29 09:50:08 +0000832 free_pd(vm->i915, pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200833 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200834 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200835
Mika Kuoppalafce93752016-10-31 17:24:46 +0200836 mark_tlbs_dirty(ppgtt);
837
Zhi Wanga18dbba2016-11-29 14:55:16 +0800838 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200839 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200840
841 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200842}
Ben Widawsky459108b2013-11-02 21:07:23 -0700843
Michał Winiarski2ce51792016-10-13 14:02:42 +0200844/* Removes entries from a single pml4.
845 * This is the top-level structure in 4-level page tables used on gen8+.
846 * Empty entries are always scratch pml4e.
847 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200848static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
849 struct i915_pml4 *pml4,
850 uint64_t start,
851 uint64_t length)
852{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200853 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200854 struct i915_page_directory_pointer *pdp;
855 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200856
Chris Wilson49d73912016-11-29 09:50:08 +0000857 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700858
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200859 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
860 if (WARN_ON(!pml4->pdps[pml4e]))
861 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700862
Michał Winiarski2ce51792016-10-13 14:02:42 +0200863 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
864 __clear_bit(pml4e, pml4->used_pml4es);
Matthew Auld9e65a372016-12-13 16:05:12 +0000865 gen8_setup_pml4e(ppgtt, pml4, vm->scratch_pdp, pml4e);
Chris Wilson49d73912016-11-29 09:50:08 +0000866 free_pdp(vm->i915, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200867 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700868 }
869}
870
Michel Thierryf9b5b782015-07-30 11:02:49 +0100871static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200872 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700873{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300874 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100875
Chris Wilsonc6385c92016-11-29 12:42:05 +0000876 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200877 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
878 else
879 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100880}
881
882static void
883gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
884 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100885 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100886 uint64_t start,
887 enum i915_cache_level cache_level)
888{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300889 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000890 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100891 unsigned pdpe = gen8_pdpe_index(start);
892 unsigned pde = gen8_pde_index(start);
893 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700894
Chris Wilson6f1cc992013-12-31 15:50:31 +0000895 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700896
Michel Thierry3387d432015-08-03 09:52:47 +0100897 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000898 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100899 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100900 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300901 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000902 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800903
904 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100905 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200906 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000907 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300908 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000909 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000910 if (++pde == I915_PDES) {
Chris Wilsonc6385c92016-11-29 12:42:05 +0000911 if (++pdpe == I915_PDPES_PER_PDP(vm->i915))
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100912 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800913 pde = 0;
914 }
915 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700916 }
917 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300918
919 if (pt_vaddr)
920 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700921}
922
Michel Thierryf9b5b782015-07-30 11:02:49 +0100923static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
924 struct sg_table *pages,
925 uint64_t start,
926 enum i915_cache_level cache_level,
927 u32 unused)
928{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300929 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100930 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100931
Michel Thierry3387d432015-08-03 09:52:47 +0100932 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100933
Chris Wilsonc6385c92016-11-29 12:42:05 +0000934 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100935 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
936 cache_level);
937 } else {
938 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000939 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100940 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
941
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000942 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100943 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
944 start, cache_level);
945 }
946 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100947}
948
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000949static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
Michel Thierryf37c0502015-06-10 17:46:39 +0100950 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800951{
952 int i;
953
Mika Kuoppala567047b2015-06-25 18:35:12 +0300954 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800955 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800956
Michel Thierry33c88192015-04-08 12:13:33 +0100957 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000958 if (WARN_ON(!pd->page_table[i]))
959 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800960
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000961 free_pt(dev_priv, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000962 pd->page_table[i] = NULL;
963 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000964}
965
Mika Kuoppala8776f022015-06-30 18:16:40 +0300966static int gen8_init_scratch(struct i915_address_space *vm)
967{
Chris Wilson49d73912016-11-29 09:50:08 +0000968 struct drm_i915_private *dev_priv = vm->i915;
Matthew Auld64c050d2016-04-27 13:19:25 +0100969 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300970
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000971 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100972 if (ret)
973 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300974
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000975 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300976 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100977 ret = PTR_ERR(vm->scratch_pt);
978 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300979 }
980
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000981 vm->scratch_pd = alloc_pd(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300982 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100983 ret = PTR_ERR(vm->scratch_pd);
984 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300985 }
986
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000987 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
988 vm->scratch_pdp = alloc_pdp(dev_priv);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100989 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100990 ret = PTR_ERR(vm->scratch_pdp);
991 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100992 }
993 }
994
Mika Kuoppala8776f022015-06-30 18:16:40 +0300995 gen8_initialize_pt(vm, vm->scratch_pt);
996 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000997 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100998 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300999
1000 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +01001001
1002free_pd:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001003 free_pd(dev_priv, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +01001004free_pt:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001005 free_pt(dev_priv, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +01001006free_scratch_page:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001007 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +01001008
1009 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001010}
1011
Zhiyuan Lv650da342015-08-28 15:41:18 +08001012static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1013{
1014 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +00001015 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001016 int i;
1017
Matthew Aulddf285642016-04-22 12:09:25 +01001018 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001019 u64 daddr = px_dma(&ppgtt->pml4);
1020
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001021 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1022 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001023
1024 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1025 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1026 } else {
1027 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1028 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1029
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001030 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1031 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001032 }
1033
1034 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1035 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1036 }
1037
1038 I915_WRITE(vgtif_reg(g2v_notify), msg);
1039
1040 return 0;
1041}
1042
Mika Kuoppala8776f022015-06-30 18:16:40 +03001043static void gen8_free_scratch(struct i915_address_space *vm)
1044{
Chris Wilson49d73912016-11-29 09:50:08 +00001045 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001046
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001047 if (USES_FULL_48BIT_PPGTT(dev_priv))
1048 free_pdp(dev_priv, vm->scratch_pdp);
1049 free_pd(dev_priv, vm->scratch_pd);
1050 free_pt(dev_priv, vm->scratch_pt);
1051 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001052}
1053
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001054static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
Michel Thierry762d9932015-07-30 11:05:29 +01001055 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001056{
1057 int i;
1058
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001059 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001060 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001061 continue;
1062
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001063 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1064 free_pd(dev_priv, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001065 }
Michel Thierry69876be2015-04-08 12:13:27 +01001066
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001067 free_pdp(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001068}
1069
1070static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1071{
Chris Wilson49d73912016-11-29 09:50:08 +00001072 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001073 int i;
1074
1075 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1076 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1077 continue;
1078
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001079 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001080 }
1081
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001082 cleanup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001083}
1084
1085static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1086{
Chris Wilson49d73912016-11-29 09:50:08 +00001087 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001088 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001089
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001090 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001091 gen8_ppgtt_notify_vgt(ppgtt, false);
1092
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001093 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1094 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001095 else
1096 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001097
Mika Kuoppala8776f022015-06-30 18:16:40 +03001098 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001099}
1100
Michel Thierryd7b26332015-04-08 12:13:34 +01001101/**
1102 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001103 * @vm: Master vm structure.
1104 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001105 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001106 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001107 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1108 * caller to free on error.
1109 *
1110 * Allocate the required number of page tables. Extremely similar to
1111 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1112 * the page directory boundary (instead of the page directory pointer). That
1113 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1114 * possible, and likely that the caller will need to use multiple calls of this
1115 * function to achieve the appropriate allocation.
1116 *
1117 * Return: 0 if success; negative error code otherwise.
1118 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001119static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001120 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001121 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001122 uint64_t length,
1123 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001124{
Chris Wilson49d73912016-11-29 09:50:08 +00001125 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001126 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001127 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001128
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001129 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001130 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001131 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001132 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001133 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001134 continue;
1135 }
1136
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001137 pt = alloc_pt(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001138 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001139 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001140
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001141 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001142 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001143 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001144 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001145 }
1146
1147 return 0;
1148
1149unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001150 for_each_set_bit(pde, new_pts, I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001151 free_pt(dev_priv, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001152
1153 return -ENOMEM;
1154}
1155
Michel Thierryd7b26332015-04-08 12:13:34 +01001156/**
1157 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001158 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001159 * @pdp: Page directory pointer for this address range.
1160 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001161 * @length: Size of the allocations.
1162 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001163 * caller to free on error.
1164 *
1165 * Allocate the required number of page directories starting at the pde index of
1166 * @start, and ending at the pde index @start + @length. This function will skip
1167 * over already allocated page directories within the range, and only allocate
1168 * new ones, setting the appropriate pointer within the pdp as well as the
1169 * correct position in the bitmap @new_pds.
1170 *
1171 * The function will only allocate the pages within the range for a give page
1172 * directory pointer. In other words, if @start + @length straddles a virtually
1173 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1174 * required by the caller, This is not currently possible, and the BUG in the
1175 * code will prevent it.
1176 *
1177 * Return: 0 if success; negative error code otherwise.
1178 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001179static int
1180gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1181 struct i915_page_directory_pointer *pdp,
1182 uint64_t start,
1183 uint64_t length,
1184 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001185{
Chris Wilson49d73912016-11-29 09:50:08 +00001186 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001187 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001188 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001189 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001190
Michel Thierry6ac18502015-07-29 17:23:46 +01001191 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001192
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001193 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001194 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001195 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001196
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001197 pd = alloc_pd(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001198 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001199 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001200
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001201 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001202 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001203 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001204 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001205 }
1206
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001207 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001208
1209unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001210 for_each_set_bit(pdpe, new_pds, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001211 free_pd(dev_priv, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001212
1213 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001214}
1215
Michel Thierry762d9932015-07-30 11:05:29 +01001216/**
1217 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1218 * @vm: Master vm structure.
1219 * @pml4: Page map level 4 for this address range.
1220 * @start: Starting virtual address to begin allocations.
1221 * @length: Size of the allocations.
1222 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1223 * caller to free on error.
1224 *
1225 * Allocate the required number of page directory pointers. Extremely similar to
1226 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1227 * The main difference is here we are limited by the pml4 boundary (instead of
1228 * the page directory pointer).
1229 *
1230 * Return: 0 if success; negative error code otherwise.
1231 */
1232static int
1233gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1234 struct i915_pml4 *pml4,
1235 uint64_t start,
1236 uint64_t length,
1237 unsigned long *new_pdps)
1238{
Chris Wilson49d73912016-11-29 09:50:08 +00001239 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001240 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001241 uint32_t pml4e;
1242
1243 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1244
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001245 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001246 if (!test_bit(pml4e, pml4->used_pml4es)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001247 pdp = alloc_pdp(dev_priv);
Michel Thierry762d9932015-07-30 11:05:29 +01001248 if (IS_ERR(pdp))
1249 goto unwind_out;
1250
Michel Thierry69ab76f2015-07-29 17:23:55 +01001251 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001252 pml4->pdps[pml4e] = pdp;
1253 __set_bit(pml4e, new_pdps);
1254 trace_i915_page_directory_pointer_entry_alloc(vm,
1255 pml4e,
1256 start,
1257 GEN8_PML4E_SHIFT);
1258 }
1259 }
1260
1261 return 0;
1262
1263unwind_out:
1264 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001265 free_pdp(dev_priv, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001266
1267 return -ENOMEM;
1268}
1269
Michel Thierryd7b26332015-04-08 12:13:34 +01001270static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001271free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001272{
Michel Thierryd7b26332015-04-08 12:13:34 +01001273 kfree(new_pts);
1274 kfree(new_pds);
1275}
1276
1277/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1278 * of these are based on the number of PDPEs in the system.
1279 */
1280static
1281int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001282 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001283 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001284{
Michel Thierryd7b26332015-04-08 12:13:34 +01001285 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001286 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001287
Michał Winiarski3a41a052015-09-03 19:22:18 +02001288 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001289 if (!pds)
1290 return -ENOMEM;
1291
Michał Winiarski3a41a052015-09-03 19:22:18 +02001292 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1293 GFP_TEMPORARY);
1294 if (!pts)
1295 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001296
1297 *new_pds = pds;
1298 *new_pts = pts;
1299
1300 return 0;
1301
1302err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001303 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001304 return -ENOMEM;
1305}
1306
Michel Thierry762d9932015-07-30 11:05:29 +01001307static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1308 struct i915_page_directory_pointer *pdp,
1309 uint64_t start,
1310 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001311{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001312 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001313 unsigned long *new_page_dirs, *new_page_tables;
Chris Wilson49d73912016-11-29 09:50:08 +00001314 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001315 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001316 const uint64_t orig_start = start;
1317 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001318 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001319 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001320 int ret;
1321
Michel Thierry6ac18502015-07-29 17:23:46 +01001322 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001323 if (ret)
1324 return ret;
1325
Michel Thierryd7b26332015-04-08 12:13:34 +01001326 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001327 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1328 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001329 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001330 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001331 return ret;
1332 }
1333
1334 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001335 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001336 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001337 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001338 if (ret)
1339 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001340 }
1341
Michel Thierry33c88192015-04-08 12:13:33 +01001342 start = orig_start;
1343 length = orig_length;
1344
Michel Thierryd7b26332015-04-08 12:13:34 +01001345 /* Allocations have completed successfully, so set the bitmaps, and do
1346 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001347 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001348 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001349 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001350 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001351 uint64_t pd_start = start;
1352 uint32_t pde;
1353
Michel Thierryd7b26332015-04-08 12:13:34 +01001354 /* Every pd should be allocated, we just did that above. */
1355 WARN_ON(!pd);
1356
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001357 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001358 /* Same reasoning as pd */
1359 WARN_ON(!pt);
1360 WARN_ON(!pd_len);
1361 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1362
1363 /* Set our used ptes within the page table */
1364 bitmap_set(pt->used_ptes,
1365 gen8_pte_index(pd_start),
1366 gen8_pte_count(pd_start, pd_len));
1367
1368 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001369 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001370
1371 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001372 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1373 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001374 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1375 gen8_pte_index(start),
1376 gen8_pte_count(start, length),
1377 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001378
1379 /* NB: We haven't yet mapped ptes to pages. At this
1380 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001381 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001382
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001383 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001384 __set_bit(pdpe, pdp->used_pdpes);
Matthew Auld5c693b22016-12-13 16:05:10 +00001385 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001386 }
1387
Michał Winiarski3a41a052015-09-03 19:22:18 +02001388 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001389 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001390 return 0;
1391
1392err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001393 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001394 unsigned long temp;
1395
Michał Winiarski3a41a052015-09-03 19:22:18 +02001396 for_each_set_bit(temp, new_page_tables + pdpe *
1397 BITS_TO_LONGS(I915_PDES), I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001398 free_pt(dev_priv,
1399 pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001400 }
1401
Michel Thierry6ac18502015-07-29 17:23:46 +01001402 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001403 free_pd(dev_priv, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001404
Michał Winiarski3a41a052015-09-03 19:22:18 +02001405 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001406 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001407 return ret;
1408}
1409
Michel Thierry762d9932015-07-30 11:05:29 +01001410static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1411 struct i915_pml4 *pml4,
1412 uint64_t start,
1413 uint64_t length)
1414{
1415 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001416 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001417 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001418 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001419 int ret = 0;
1420
1421 /* Do the pml4 allocations first, so we don't need to track the newly
1422 * allocated tables below the pdp */
1423 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1424
1425 /* The pagedirectory and pagetable allocations are done in the shared 3
1426 * and 4 level code. Just allocate the pdps.
1427 */
1428 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1429 new_pdps);
1430 if (ret)
1431 return ret;
1432
1433 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1434 "The allocation has spanned more than 512GB. "
1435 "It is highly likely this is incorrect.");
1436
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001437 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001438 WARN_ON(!pdp);
1439
1440 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1441 if (ret)
1442 goto err_out;
1443
Matthew Auld56843102016-12-13 16:05:11 +00001444 gen8_setup_pml4e(ppgtt, pml4, pdp, pml4e);
Michel Thierry762d9932015-07-30 11:05:29 +01001445 }
1446
1447 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1448 GEN8_PML4ES_PER_PML4);
1449
1450 return 0;
1451
1452err_out:
1453 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson49d73912016-11-29 09:50:08 +00001454 gen8_ppgtt_cleanup_3lvl(vm->i915, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001455
1456 return ret;
1457}
1458
1459static int gen8_alloc_va_range(struct i915_address_space *vm,
1460 uint64_t start, uint64_t length)
1461{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001462 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001463
Chris Wilsonc6385c92016-11-29 12:42:05 +00001464 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001465 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1466 else
1467 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1468}
1469
Michel Thierryea91e402015-07-29 17:23:57 +01001470static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1471 uint64_t start, uint64_t length,
1472 gen8_pte_t scratch_pte,
1473 struct seq_file *m)
1474{
1475 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001476 uint32_t pdpe;
1477
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001478 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001479 struct i915_page_table *pt;
1480 uint64_t pd_len = length;
1481 uint64_t pd_start = start;
1482 uint32_t pde;
1483
1484 if (!test_bit(pdpe, pdp->used_pdpes))
1485 continue;
1486
1487 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001488 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001489 uint32_t pte;
1490 gen8_pte_t *pt_vaddr;
1491
1492 if (!test_bit(pde, pd->used_pdes))
1493 continue;
1494
1495 pt_vaddr = kmap_px(pt);
1496 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1497 uint64_t va =
1498 (pdpe << GEN8_PDPE_SHIFT) |
1499 (pde << GEN8_PDE_SHIFT) |
1500 (pte << GEN8_PTE_SHIFT);
1501 int i;
1502 bool found = false;
1503
1504 for (i = 0; i < 4; i++)
1505 if (pt_vaddr[pte + i] != scratch_pte)
1506 found = true;
1507 if (!found)
1508 continue;
1509
1510 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1511 for (i = 0; i < 4; i++) {
1512 if (pt_vaddr[pte + i] != scratch_pte)
1513 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1514 else
1515 seq_puts(m, " SCRATCH ");
1516 }
1517 seq_puts(m, "\n");
1518 }
1519 /* don't use kunmap_px, it could trigger
1520 * an unnecessary flush.
1521 */
1522 kunmap_atomic(pt_vaddr);
1523 }
1524 }
1525}
1526
1527static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1528{
1529 struct i915_address_space *vm = &ppgtt->base;
1530 uint64_t start = ppgtt->base.start;
1531 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001532 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001533 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001534
Chris Wilsonc6385c92016-11-29 12:42:05 +00001535 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryea91e402015-07-29 17:23:57 +01001536 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1537 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001538 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001539 struct i915_pml4 *pml4 = &ppgtt->pml4;
1540 struct i915_page_directory_pointer *pdp;
1541
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001542 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001543 if (!test_bit(pml4e, pml4->used_pml4es))
1544 continue;
1545
1546 seq_printf(m, " PML4E #%llu\n", pml4e);
1547 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1548 }
1549 }
1550}
1551
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001552static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1553{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001554 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001555 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001556 int ret;
1557
1558 /* We allocate temp bitmap for page tables for no gain
1559 * but as this is for init only, lets keep the things simple
1560 */
1561 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1562 if (ret)
1563 return ret;
1564
1565 /* Allocate for all pdps regardless of how the ppgtt
1566 * was defined.
1567 */
1568 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1569 0, 1ULL << 32,
1570 new_page_dirs);
1571 if (!ret)
1572 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1573
Michał Winiarski3a41a052015-09-03 19:22:18 +02001574 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001575
1576 return ret;
1577}
1578
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001579/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001580 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1581 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1582 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1583 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001584 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001585 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001586static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001587{
Chris Wilson49d73912016-11-29 09:50:08 +00001588 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001589 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001590
Mika Kuoppala8776f022015-06-30 18:16:40 +03001591 ret = gen8_init_scratch(&ppgtt->base);
1592 if (ret)
1593 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001594
Michel Thierryd7b26332015-04-08 12:13:34 +01001595 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001596 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001597 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001598 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001599 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001600 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1601 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001602 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001603
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001604 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1605 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001606 if (ret)
1607 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001608
Michel Thierry69ab76f2015-07-29 17:23:55 +01001609 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1610
Michel Thierry762d9932015-07-30 11:05:29 +01001611 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001612 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001613 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001614 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001615 if (ret)
1616 goto free_scratch;
1617
1618 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001619 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001620 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1621 0, 0,
1622 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001623
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001624 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001625 ret = gen8_preallocate_top_level_pdps(ppgtt);
1626 if (ret)
1627 goto free_scratch;
1628 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001629 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001630
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001631 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001632 gen8_ppgtt_notify_vgt(ppgtt, true);
1633
Michel Thierryd7b26332015-04-08 12:13:34 +01001634 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001635
1636free_scratch:
1637 gen8_free_scratch(&ppgtt->base);
1638 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001639}
1640
Ben Widawsky87d60b62013-12-06 14:11:29 -08001641static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1642{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001643 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001644 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001645 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001646 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001647 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001648 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001649
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001650 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001651 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001652
Dave Gordon731f74c2016-06-24 19:37:46 +01001653 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001654 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001655 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001656 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001657 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001658 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1659
1660 if (pd_entry != expected)
1661 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1662 pde,
1663 pd_entry,
1664 expected);
1665 seq_printf(m, "\tPDE: %x\n", pd_entry);
1666
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001667 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1668
Michel Thierry07749ef2015-03-16 16:00:54 +00001669 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001670 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001671 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001672 (pte * PAGE_SIZE);
1673 int i;
1674 bool found = false;
1675 for (i = 0; i < 4; i++)
1676 if (pt_vaddr[pte + i] != scratch_pte)
1677 found = true;
1678 if (!found)
1679 continue;
1680
1681 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1682 for (i = 0; i < 4; i++) {
1683 if (pt_vaddr[pte + i] != scratch_pte)
1684 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1685 else
1686 seq_puts(m, " SCRATCH ");
1687 }
1688 seq_puts(m, "\n");
1689 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001690 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001691 }
1692}
1693
Ben Widawsky678d96f2015-03-16 16:00:56 +00001694/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001695static void gen6_write_pde(struct i915_page_directory *pd,
1696 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001697{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001698 /* Caller needs to make sure the write completes if necessary */
1699 struct i915_hw_ppgtt *ppgtt =
1700 container_of(pd, struct i915_hw_ppgtt, pd);
1701 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001702
Mika Kuoppala567047b2015-06-25 18:35:12 +03001703 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001704 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001705
Ben Widawsky678d96f2015-03-16 16:00:56 +00001706 writel(pd_entry, ppgtt->pd_addr + pde);
1707}
Ben Widawsky61973492013-04-08 18:43:54 -07001708
Ben Widawsky678d96f2015-03-16 16:00:56 +00001709/* Write all the page tables found in the ppgtt structure to incrementing page
1710 * directories. */
1711static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001712 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001713 uint32_t start, uint32_t length)
1714{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001715 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001716 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001717 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001718
Dave Gordon731f74c2016-06-24 19:37:46 +01001719 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001720 gen6_write_pde(pd, pde, pt);
1721
1722 /* Make sure write is complete before other code can use this page
1723 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001724 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001725}
1726
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001727static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001728{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001729 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001730
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001731 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001732}
Ben Widawsky61973492013-04-08 18:43:54 -07001733
Ben Widawsky90252e52013-12-06 14:11:12 -08001734static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001735 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001736{
Chris Wilson7e37f882016-08-02 22:50:21 +01001737 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001738 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001739 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001740
Ben Widawsky90252e52013-12-06 14:11:12 -08001741 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001742 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001743 if (ret)
1744 return ret;
1745
John Harrison5fb9de12015-05-29 17:44:07 +01001746 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001747 if (ret)
1748 return ret;
1749
Chris Wilsonb5321f32016-08-02 22:50:18 +01001750 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1751 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1752 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1753 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1754 intel_ring_emit(ring, get_pd_offset(ppgtt));
1755 intel_ring_emit(ring, MI_NOOP);
1756 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001757
1758 return 0;
1759}
1760
Ben Widawsky48a10382013-12-06 14:11:11 -08001761static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001762 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001763{
Chris Wilson7e37f882016-08-02 22:50:21 +01001764 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001765 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001766 int ret;
1767
Ben Widawsky48a10382013-12-06 14:11:11 -08001768 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001769 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001770 if (ret)
1771 return ret;
1772
John Harrison5fb9de12015-05-29 17:44:07 +01001773 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001774 if (ret)
1775 return ret;
1776
Chris Wilsonb5321f32016-08-02 22:50:18 +01001777 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1778 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1779 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1780 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1781 intel_ring_emit(ring, get_pd_offset(ppgtt));
1782 intel_ring_emit(ring, MI_NOOP);
1783 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001784
Ben Widawsky90252e52013-12-06 14:11:12 -08001785 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001786 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001787 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001788 if (ret)
1789 return ret;
1790 }
1791
Ben Widawsky48a10382013-12-06 14:11:11 -08001792 return 0;
1793}
1794
Ben Widawskyeeb94882013-12-06 14:11:10 -08001795static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001796 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001797{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001798 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001799 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001800
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001801 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1802 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001803 return 0;
1804}
1805
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001806static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001807{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001808 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301809 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001810
Akash Goel3b3f1652016-10-13 22:44:48 +05301811 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001812 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1813 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001814 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001815 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001816 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001817}
1818
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001819static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001820{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001821 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001822 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301823 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001824
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001825 ecobits = I915_READ(GAC_ECO_BITS);
1826 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1827
1828 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001829 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001830 ecochk |= ECOCHK_PPGTT_WB_HSW;
1831 } else {
1832 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1833 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1834 }
1835 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001836
Akash Goel3b3f1652016-10-13 22:44:48 +05301837 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001838 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001839 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001840 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001841 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001842}
1843
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001844static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001845{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001846 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001847
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001848 ecobits = I915_READ(GAC_ECO_BITS);
1849 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1850 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001851
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001852 gab_ctl = I915_READ(GAB_CTL);
1853 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001854
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001855 ecochk = I915_READ(GAM_ECOCHK);
1856 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001857
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001858 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001859}
1860
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001861/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001862static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001863 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001864 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001865{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001866 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001867 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001868 unsigned first_entry = start >> PAGE_SHIFT;
1869 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001870 unsigned act_pt = first_entry / GEN6_PTES;
1871 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001872 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001873
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001874 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001875 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001876
Daniel Vetter7bddb012012-02-09 17:15:47 +01001877 while (num_entries) {
1878 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001879 if (last_pte > GEN6_PTES)
1880 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001881
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001882 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001883
1884 for (i = first_pte; i < last_pte; i++)
1885 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001886
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001887 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001888
Daniel Vetter7bddb012012-02-09 17:15:47 +01001889 num_entries -= last_pte - first_pte;
1890 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001891 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001892 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001893}
1894
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001895static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001896 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001897 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301898 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001899{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001900 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001901 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001902 unsigned act_pt = first_entry / GEN6_PTES;
1903 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001904 gen6_pte_t *pt_vaddr = NULL;
1905 struct sgt_iter sgt_iter;
1906 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001907
Dave Gordon85d12252016-05-20 11:54:06 +01001908 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001909 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001910 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001911
Chris Wilsoncc797142013-12-31 15:50:30 +00001912 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001913 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301914
Michel Thierry07749ef2015-03-16 16:00:54 +00001915 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001916 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001917 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001918 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001919 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001920 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001921 }
Dave Gordon85d12252016-05-20 11:54:06 +01001922
Chris Wilsoncc797142013-12-31 15:50:30 +00001923 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001924 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001925}
1926
Ben Widawsky678d96f2015-03-16 16:00:56 +00001927static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001928 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001929{
Michel Thierry4933d512015-03-24 15:46:22 +00001930 DECLARE_BITMAP(new_page_tables, I915_PDES);
Chris Wilson49d73912016-11-29 09:50:08 +00001931 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001932 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001933 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001934 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001935 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001936 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001937 int ret;
1938
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001939 start = start_save = start_in;
1940 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001941
1942 bitmap_zero(new_page_tables, I915_PDES);
1943
1944 /* The allocation is done in two stages so that we can bail out with
1945 * minimal amount of pain. The first stage finds new page tables that
1946 * need allocation. The second stage marks use ptes within the page
1947 * tables.
1948 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001949 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001950 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001951 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1952 continue;
1953 }
1954
1955 /* We've already allocated a page table */
1956 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1957
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001958 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001959 if (IS_ERR(pt)) {
1960 ret = PTR_ERR(pt);
1961 goto unwind_out;
1962 }
1963
1964 gen6_initialize_pt(vm, pt);
1965
1966 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001967 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001968 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001969 }
1970
1971 start = start_save;
1972 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001973
Dave Gordon731f74c2016-06-24 19:37:46 +01001974 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001975 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1976
1977 bitmap_zero(tmp_bitmap, GEN6_PTES);
1978 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1979 gen6_pte_count(start, length));
1980
Mika Kuoppala966082c2015-06-25 18:35:19 +03001981 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001982 gen6_write_pde(&ppgtt->pd, pde, pt);
1983
Michel Thierry72744cb2015-03-24 15:46:23 +00001984 trace_i915_page_table_entry_map(vm, pde, pt,
1985 gen6_pte_index(start),
1986 gen6_pte_count(start, length),
1987 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001988 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001989 GEN6_PTES);
1990 }
1991
Michel Thierry4933d512015-03-24 15:46:22 +00001992 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1993
1994 /* Make sure write is complete before other code can use this page
1995 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001996 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001997
Ben Widawsky563222a2015-03-19 12:53:28 +00001998 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001999 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00002000
2001unwind_out:
2002 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002003 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002004
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002005 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002006 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002007 }
2008
2009 mark_tlbs_dirty(ppgtt);
2010 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002011}
2012
Mika Kuoppala8776f022015-06-30 18:16:40 +03002013static int gen6_init_scratch(struct i915_address_space *vm)
2014{
Chris Wilson49d73912016-11-29 09:50:08 +00002015 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002016 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002017
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002018 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002019 if (ret)
2020 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002021
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002022 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002023 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002024 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002025 return PTR_ERR(vm->scratch_pt);
2026 }
2027
2028 gen6_initialize_pt(vm, vm->scratch_pt);
2029
2030 return 0;
2031}
2032
2033static void gen6_free_scratch(struct i915_address_space *vm)
2034{
Chris Wilson49d73912016-11-29 09:50:08 +00002035 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002036
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002037 free_pt(dev_priv, vm->scratch_pt);
2038 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002039}
2040
Daniel Vetter061dd492015-04-14 17:35:13 +02002041static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002042{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002043 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002044 struct i915_page_directory *pd = &ppgtt->pd;
Chris Wilson49d73912016-11-29 09:50:08 +00002045 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry09942c62015-04-08 12:13:30 +01002046 struct i915_page_table *pt;
2047 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002048
Daniel Vetter061dd492015-04-14 17:35:13 +02002049 drm_mm_remove_node(&ppgtt->node);
2050
Dave Gordon731f74c2016-06-24 19:37:46 +01002051 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002052 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002053 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002054
Mika Kuoppala8776f022015-06-30 18:16:40 +03002055 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002056}
2057
Ben Widawskyb1465202014-02-19 22:05:49 -08002058static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002059{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002060 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00002061 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002062 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002063 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002064
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002065 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2066 * allocator works in address space sizes, so it's multiplied by page
2067 * size. We allocate at the top of the GTT to avoid fragmentation.
2068 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002069 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002070
Mika Kuoppala8776f022015-06-30 18:16:40 +03002071 ret = gen6_init_scratch(vm);
2072 if (ret)
2073 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002074
Chris Wilsone007b192017-01-11 11:23:10 +00002075 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2076 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2077 I915_COLOR_UNEVICTABLE,
2078 0, ggtt->base.total,
2079 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00002080 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002081 goto err_out;
2082
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002083 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002084 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002085
Ben Widawskyc8c26622015-01-22 17:01:25 +00002086 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002087
2088err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002089 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002090 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002091}
2092
Ben Widawskyb1465202014-02-19 22:05:49 -08002093static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2094{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002095 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002096}
2097
Michel Thierry4933d512015-03-24 15:46:22 +00002098static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2099 uint64_t start, uint64_t length)
2100{
Michel Thierryec565b32015-04-08 12:13:23 +01002101 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002102 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002103
Dave Gordon731f74c2016-06-24 19:37:46 +01002104 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002105 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002106}
2107
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002108static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002109{
Chris Wilson49d73912016-11-29 09:50:08 +00002110 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002111 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002112 int ret;
2113
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002114 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002115 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002116 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002117 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002118 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002119 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002120 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002121 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002122 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002123
2124 ret = gen6_ppgtt_alloc(ppgtt);
2125 if (ret)
2126 return ret;
2127
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002128 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002129 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2130 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002131 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2132 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002133 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002134 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002135 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002136 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002137
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002138 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002139 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002140
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002141 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002142 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002143
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002144 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002145
Ben Widawsky678d96f2015-03-16 16:00:56 +00002146 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2147
Thierry Reding440fd522015-01-23 09:05:06 +01002148 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002149 ppgtt->node.size >> 20,
2150 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002151
Daniel Vetterfa76da32014-08-06 20:19:54 +02002152 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002153 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002154
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002155 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002156}
2157
Chris Wilson2bfa9962016-08-04 07:52:25 +01002158static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2159 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002160{
Chris Wilson49d73912016-11-29 09:50:08 +00002161 ppgtt->base.i915 = dev_priv;
Daniel Vetter3440d262013-01-24 13:49:56 -08002162
Chris Wilson2bfa9962016-08-04 07:52:25 +01002163 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002164 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002165 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002166 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002167}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002168
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002169static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002170 struct drm_i915_private *dev_priv,
2171 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002172{
Chris Wilson80b204b2016-10-28 13:58:58 +01002173 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002174 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002175 INIT_LIST_HEAD(&vm->active_list);
2176 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002177 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002178 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2179}
2180
Matthew Aulded9724d2016-11-17 21:04:10 +00002181static void i915_address_space_fini(struct i915_address_space *vm)
2182{
2183 i915_gem_timeline_fini(&vm->timeline);
2184 drm_mm_takedown(&vm->mm);
2185 list_del(&vm->global_link);
2186}
2187
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002188static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002189{
Tim Gored5165eb2016-02-04 11:49:34 +00002190 /* This function is for gtt related workarounds. This function is
2191 * called on driver load and after a GPU reset, so you can place
2192 * workarounds here even if they get overwritten by GPU reset.
2193 */
2194 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002195 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002196 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002197 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002198 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002199 else if (IS_SKYLAKE(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002200 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002201 else if (IS_BROXTON(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002202 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2203}
2204
Chris Wilson2bfa9962016-08-04 07:52:25 +01002205static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2206 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002207 struct drm_i915_file_private *file_priv,
2208 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002209{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002210 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002211
Chris Wilson2bfa9962016-08-04 07:52:25 +01002212 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002213 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002214 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002215 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002216 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002217 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002218
2219 return ret;
2220}
2221
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002222int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002223{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002224 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002225
Thomas Daniel671b50132014-08-20 16:24:50 +01002226 /* In the case of execlists, PPGTT is enabled by the context descriptor
2227 * and the PDPs are contained within the context itself. We don't
2228 * need to do anything here. */
2229 if (i915.enable_execlists)
2230 return 0;
2231
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002232 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002233 return 0;
2234
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002235 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002236 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002237 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002238 gen7_ppgtt_enable(dev_priv);
2239 else if (INTEL_GEN(dev_priv) >= 8)
2240 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002241 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002242 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002243
John Harrison4ad2fd82015-06-18 13:11:20 +01002244 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002245}
John Harrison4ad2fd82015-06-18 13:11:20 +01002246
Daniel Vetter4d884702014-08-06 15:04:47 +02002247struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002248i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002249 struct drm_i915_file_private *fpriv,
2250 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002251{
2252 struct i915_hw_ppgtt *ppgtt;
2253 int ret;
2254
2255 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2256 if (!ppgtt)
2257 return ERR_PTR(-ENOMEM);
2258
Chris Wilson80b204b2016-10-28 13:58:58 +01002259 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002260 if (ret) {
2261 kfree(ppgtt);
2262 return ERR_PTR(ret);
2263 }
2264
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002265 trace_i915_ppgtt_create(&ppgtt->base);
2266
Daniel Vetter4d884702014-08-06 15:04:47 +02002267 return ppgtt;
2268}
2269
Chris Wilson0c7eeda2017-01-11 21:09:25 +00002270void i915_ppgtt_close(struct i915_address_space *vm)
2271{
2272 struct list_head *phases[] = {
2273 &vm->active_list,
2274 &vm->inactive_list,
2275 &vm->unbound_list,
2276 NULL,
2277 }, **phase;
2278
2279 GEM_BUG_ON(vm->closed);
2280 vm->closed = true;
2281
2282 for (phase = phases; *phase; phase++) {
2283 struct i915_vma *vma, *vn;
2284
2285 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2286 if (!i915_vma_is_closed(vma))
2287 i915_vma_close(vma);
2288 }
2289}
2290
Matthew Aulded9724d2016-11-17 21:04:10 +00002291void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002292{
2293 struct i915_hw_ppgtt *ppgtt =
2294 container_of(kref, struct i915_hw_ppgtt, ref);
2295
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002296 trace_i915_ppgtt_release(&ppgtt->base);
2297
Chris Wilson50e046b2016-08-04 07:52:46 +01002298 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002299 WARN_ON(!list_empty(&ppgtt->base.active_list));
2300 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002301 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002302
Matthew Aulded9724d2016-11-17 21:04:10 +00002303 i915_address_space_fini(&ppgtt->base);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002304
Daniel Vetteree960be2014-08-06 15:04:45 +02002305 ppgtt->base.cleanup(&ppgtt->base);
2306 kfree(ppgtt);
2307}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002308
Ben Widawskya81cc002013-01-18 12:30:31 -08002309/* Certain Gen5 chipsets require require idling the GPU before
2310 * unmapping anything from the GTT when VT-d is enabled.
2311 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002312static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002313{
2314#ifdef CONFIG_INTEL_IOMMU
2315 /* Query intel_iommu to see if we need the workaround. Presumably that
2316 * was loaded first.
2317 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002318 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002319 return true;
2320#endif
2321 return false;
2322}
2323
Chris Wilsondc979972016-05-10 14:10:04 +01002324void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002325{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002326 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302327 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002328
Chris Wilsondc979972016-05-10 14:10:04 +01002329 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002330 return;
2331
Akash Goel3b3f1652016-10-13 22:44:48 +05302332 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002333 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002334 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002335 if (fault_reg & RING_FAULT_VALID) {
2336 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002337 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002338 "\tAddress space: %s\n"
2339 "\tSource ID: %d\n"
2340 "\tType: %d\n",
2341 fault_reg & PAGE_MASK,
2342 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2343 RING_FAULT_SRCID(fault_reg),
2344 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002345 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002346 fault_reg & ~RING_FAULT_VALID);
2347 }
2348 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302349
2350 /* Engine specific init may not have been done till this point. */
2351 if (dev_priv->engine[RCS])
2352 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002353}
2354
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002355void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002356{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002357 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002358
2359 /* Don't bother messing with faults pre GEN6 as we have little
2360 * documentation supporting that it's a good idea.
2361 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002362 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002363 return;
2364
Chris Wilsondc979972016-05-10 14:10:04 +01002365 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002366
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002367 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002368
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002369 i915_ggtt_invalidate(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002370}
2371
Chris Wilson03ac84f2016-10-28 13:58:36 +01002372int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2373 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002374{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002375 do {
2376 if (dma_map_sg(&obj->base.dev->pdev->dev,
2377 pages->sgl, pages->nents,
2378 PCI_DMA_BIDIRECTIONAL))
2379 return 0;
2380
2381 /* If the DMA remap fails, one cause can be that we have
2382 * too many objects pinned in a small remapping table,
2383 * such as swiotlb. Incrementally purge all other objects and
2384 * try again - if there are no more pages to remove from
2385 * the DMA remapper, i915_gem_shrink will return 0.
2386 */
2387 GEM_BUG_ON(obj->mm.pages == pages);
2388 } while (i915_gem_shrink(to_i915(obj->base.dev),
2389 obj->base.size >> PAGE_SHIFT,
2390 I915_SHRINK_BOUND |
2391 I915_SHRINK_UNBOUND |
2392 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002393
Chris Wilson03ac84f2016-10-28 13:58:36 +01002394 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002395}
2396
Daniel Vetter2c642b02015-04-14 17:35:26 +02002397static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002398{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002399 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002400}
2401
Chris Wilsond6473f52016-06-10 14:22:59 +05302402static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2403 dma_addr_t addr,
2404 uint64_t offset,
2405 enum i915_cache_level level,
2406 u32 unused)
2407{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002408 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302409 gen8_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002410 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302411
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002412 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302413
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002414 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302415}
2416
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002417static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2418 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002419 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302420 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002421{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002422 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002423 struct sgt_iter sgt_iter;
2424 gen8_pte_t __iomem *gtt_entries;
2425 gen8_pte_t gtt_entry;
2426 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002427 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002428
Dave Gordon85d12252016-05-20 11:54:06 +01002429 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2430
2431 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002432 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002433 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002434 }
2435
2436 /*
2437 * XXX: This serves as a posting read to make sure that the PTE has
2438 * actually been updated. There is some concern that even though
2439 * registers and PTEs are within the same BAR that they are potentially
2440 * of NUMA access patterns. Therefore, even with the way we assume
2441 * hardware should work, we must keep this posting read for paranoia.
2442 */
2443 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002444 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002445
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002446 /* This next bit makes the above posting read even more important. We
2447 * want to flush the TLBs only after we're certain all the PTE updates
2448 * have finished.
2449 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002450 ggtt->invalidate(vm->i915);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002451}
2452
Chris Wilsonc1403302015-11-18 15:19:39 +00002453struct insert_entries {
2454 struct i915_address_space *vm;
2455 struct sg_table *st;
2456 uint64_t start;
2457 enum i915_cache_level level;
2458 u32 flags;
2459};
2460
2461static int gen8_ggtt_insert_entries__cb(void *_arg)
2462{
2463 struct insert_entries *arg = _arg;
2464 gen8_ggtt_insert_entries(arg->vm, arg->st,
2465 arg->start, arg->level, arg->flags);
2466 return 0;
2467}
2468
2469static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2470 struct sg_table *st,
2471 uint64_t start,
2472 enum i915_cache_level level,
2473 u32 flags)
2474{
2475 struct insert_entries arg = { vm, st, start, level, flags };
2476 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2477}
2478
Chris Wilsond6473f52016-06-10 14:22:59 +05302479static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2480 dma_addr_t addr,
2481 uint64_t offset,
2482 enum i915_cache_level level,
2483 u32 flags)
2484{
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002485 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Chris Wilsond6473f52016-06-10 14:22:59 +05302486 gen6_pte_t __iomem *pte =
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002487 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302488
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002489 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302490
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002491 ggtt->invalidate(vm->i915);
Chris Wilsond6473f52016-06-10 14:22:59 +05302492}
2493
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002494/*
2495 * Binds an object into the global gtt with the specified cache level. The object
2496 * will be accessible to the GPU via commands whose operands reference offsets
2497 * within the global GTT as well as accessible by the GPU through the GMADR
2498 * mapped BAR (dev_priv->mm.gtt->gtt).
2499 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002500static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002501 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002502 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302503 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002504{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002505 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002506 struct sgt_iter sgt_iter;
2507 gen6_pte_t __iomem *gtt_entries;
2508 gen6_pte_t gtt_entry;
2509 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002510 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002511
Dave Gordon85d12252016-05-20 11:54:06 +01002512 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2513
2514 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002515 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002516 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002517 }
2518
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002519 /* XXX: This serves as a posting read to make sure that the PTE has
2520 * actually been updated. There is some concern that even though
2521 * registers and PTEs are within the same BAR that they are potentially
2522 * of NUMA access patterns. Therefore, even with the way we assume
2523 * hardware should work, we must keep this posting read for paranoia.
2524 */
Dave Gordon85d12252016-05-20 11:54:06 +01002525 if (i != 0)
2526 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002527
2528 /* This next bit makes the above posting read even more important. We
2529 * want to flush the TLBs only after we're certain all the PTE updates
2530 * have finished.
2531 */
Chris Wilson7c3f86b2017-01-12 11:00:49 +00002532 ggtt->invalidate(vm->i915);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002533}
2534
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002535static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002536 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002537{
2538}
2539
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002540static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002541 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002542{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002543 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002544 unsigned first_entry = start >> PAGE_SHIFT;
2545 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002546 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002547 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2548 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002549 int i;
2550
2551 if (WARN(num_entries > max_entries,
2552 "First entry = %d; Num entries = %d (max=%d)\n",
2553 first_entry, num_entries, max_entries))
2554 num_entries = max_entries;
2555
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002556 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002557 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002558 for (i = 0; i < num_entries; i++)
2559 gen8_set_pte(&gtt_base[i], scratch_pte);
2560 readl(gtt_base);
2561}
2562
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002563static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002564 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002565 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002566{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002567 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002568 unsigned first_entry = start >> PAGE_SHIFT;
2569 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002570 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002571 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2572 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002573 int i;
2574
2575 if (WARN(num_entries > max_entries,
2576 "First entry = %d; Num entries = %d (max=%d)\n",
2577 first_entry, num_entries, max_entries))
2578 num_entries = max_entries;
2579
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002580 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002581 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002582
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002583 for (i = 0; i < num_entries; i++)
2584 iowrite32(scratch_pte, &gtt_base[i]);
2585 readl(gtt_base);
2586}
2587
Chris Wilsond6473f52016-06-10 14:22:59 +05302588static void i915_ggtt_insert_page(struct i915_address_space *vm,
2589 dma_addr_t addr,
2590 uint64_t offset,
2591 enum i915_cache_level cache_level,
2592 u32 unused)
2593{
Chris Wilsond6473f52016-06-10 14:22:59 +05302594 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2595 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302596
2597 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302598}
2599
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002600static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2601 struct sg_table *pages,
2602 uint64_t start,
2603 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002604{
2605 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2606 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2607
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002608 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002609
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002610}
2611
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002612static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002613 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002614 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002615{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002616 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002617}
2618
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002619static int ggtt_bind_vma(struct i915_vma *vma,
2620 enum i915_cache_level cache_level,
2621 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002622{
Chris Wilson49d73912016-11-29 09:50:08 +00002623 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002624 struct drm_i915_gem_object *obj = vma->obj;
2625 u32 pte_flags = 0;
2626 int ret;
2627
2628 ret = i915_get_ggtt_vma_pages(vma);
2629 if (ret)
2630 return ret;
2631
2632 /* Currently applicable only to VLV */
2633 if (obj->gt_ro)
2634 pte_flags |= PTE_READ_ONLY;
2635
Chris Wilson9c870d02016-10-24 13:42:15 +01002636 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002637 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002638 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002639 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002640
2641 /*
2642 * Without aliasing PPGTT there's no difference between
2643 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2644 * upgrade to both bound if we bind either to avoid double-binding.
2645 */
Chris Wilson3272db52016-08-04 16:32:32 +01002646 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002647
2648 return 0;
2649}
2650
2651static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2652 enum i915_cache_level cache_level,
2653 u32 flags)
2654{
Chris Wilson49d73912016-11-29 09:50:08 +00002655 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002656 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002657 int ret;
2658
2659 ret = i915_get_ggtt_vma_pages(vma);
2660 if (ret)
2661 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002662
Akash Goel24f3a8c2014-06-17 10:59:42 +05302663 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002664 pte_flags = 0;
2665 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002666 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302667
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002668
Chris Wilson3272db52016-08-04 16:32:32 +01002669 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002670 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002671 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002672 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002673 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002674 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002675 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002676
Chris Wilson3272db52016-08-04 16:32:32 +01002677 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002678 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002679 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002680 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002681 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002682 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002683
2684 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002685}
2686
2687static void ggtt_unbind_vma(struct i915_vma *vma)
2688{
Chris Wilson49d73912016-11-29 09:50:08 +00002689 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson9c870d02016-10-24 13:42:15 +01002690 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002691 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002692
Chris Wilson9c870d02016-10-24 13:42:15 +01002693 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2694 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002695 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002696 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002697 intel_runtime_pm_put(i915);
2698 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002699
Chris Wilson3272db52016-08-04 16:32:32 +01002700 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002701 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002702 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002703}
2704
Chris Wilson03ac84f2016-10-28 13:58:36 +01002705void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2706 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002707{
David Weinehall52a05c32016-08-22 13:32:44 +03002708 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2709 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002710 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002711
Chris Wilson307dc252016-08-05 10:14:12 +01002712 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002713 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002714 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2715 /* Wait a bit, in hopes it avoids the hang */
2716 udelay(10);
2717 }
2718 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002719
Chris Wilson03ac84f2016-10-28 13:58:36 +01002720 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002721}
Daniel Vetter644ec022012-03-26 09:45:40 +02002722
Chris Wilson45b186f2016-12-16 07:46:42 +00002723static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002724 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002725 u64 *start,
2726 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002727{
2728 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002729 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002730
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002731 node = list_next_entry(node, node_list);
2732 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002733 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002734}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002735
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002736int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002737{
Ben Widawskye78891c2013-01-25 16:41:04 -08002738 /* Let GEM Manage all of the aperture.
2739 *
2740 * However, leave one page at the end still bound to the scratch page.
2741 * There are a number of places where the hardware apparently prefetches
2742 * past the end of the object, and we've seen multiple hangs with the
2743 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2744 * aperture. One page should be enough to keep any prefetching inside
2745 * of the aperture.
2746 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002747 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002748 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002749 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002750 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002751 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002752
Zhi Wangb02d22a2016-06-16 08:06:59 -04002753 ret = intel_vgt_balloon(dev_priv);
2754 if (ret)
2755 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002756
Chris Wilson95374d72016-10-12 10:05:20 +01002757 /* Reserve a mappable slot for our lockless error capture */
2758 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2759 &ggtt->error_capture,
Chris Wilsonf51455d2017-01-10 14:47:34 +00002760 PAGE_SIZE, 0,
Chris Wilson85fd4f52016-12-05 14:29:36 +00002761 I915_COLOR_UNEVICTABLE,
Chris Wilson95374d72016-10-12 10:05:20 +01002762 0, ggtt->mappable_end,
2763 0, 0);
2764 if (ret)
2765 return ret;
2766
Chris Wilsoned2f3452012-11-15 11:32:19 +00002767 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002768 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002769 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2770 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002771 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002772 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002773 }
2774
2775 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002776 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002777 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002778
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002779 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002780 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002781 if (!ppgtt) {
2782 ret = -ENOMEM;
2783 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002784 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002785
Chris Wilson95374d72016-10-12 10:05:20 +01002786 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2787 if (ret)
2788 goto err_ppgtt;
2789
2790 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002791 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2792 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002793 if (ret)
2794 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002795 }
2796
2797 ppgtt->base.clear_range(&ppgtt->base,
2798 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002799 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002800
Daniel Vetterfa76da32014-08-06 20:19:54 +02002801 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002802 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2803 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002804 }
2805
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002806 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002807
2808err_ppgtt_cleanup:
2809 ppgtt->base.cleanup(&ppgtt->base);
2810err_ppgtt:
2811 kfree(ppgtt);
2812err:
2813 drm_mm_remove_node(&ggtt->error_capture);
2814 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002815}
2816
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002817/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002818 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002819 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002820 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002821void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002822{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002823 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002824
Daniel Vetter70e32542014-08-06 15:04:57 +02002825 if (dev_priv->mm.aliasing_ppgtt) {
2826 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002827 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002828 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002829 }
2830
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002831 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002832
Chris Wilson95374d72016-10-12 10:05:20 +01002833 if (drm_mm_node_allocated(&ggtt->error_capture))
2834 drm_mm_remove_node(&ggtt->error_capture);
2835
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002836 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002837 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002838
Matthew Aulded9724d2016-11-17 21:04:10 +00002839 mutex_lock(&dev_priv->drm.struct_mutex);
2840 i915_address_space_fini(&ggtt->base);
2841 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002842 }
2843
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002844 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002845
2846 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002847 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002848}
Daniel Vetter70e32542014-08-06 15:04:57 +02002849
Daniel Vetter2c642b02015-04-14 17:35:26 +02002850static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002851{
2852 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2853 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2854 return snb_gmch_ctl << 20;
2855}
2856
Daniel Vetter2c642b02015-04-14 17:35:26 +02002857static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002858{
2859 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2860 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2861 if (bdw_gmch_ctl)
2862 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002863
2864#ifdef CONFIG_X86_32
2865 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2866 if (bdw_gmch_ctl > 4)
2867 bdw_gmch_ctl = 4;
2868#endif
2869
Ben Widawsky9459d252013-11-03 16:53:55 -08002870 return bdw_gmch_ctl << 20;
2871}
2872
Daniel Vetter2c642b02015-04-14 17:35:26 +02002873static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002874{
2875 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2876 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2877
2878 if (gmch_ctrl)
2879 return 1 << (20 + gmch_ctrl);
2880
2881 return 0;
2882}
2883
Daniel Vetter2c642b02015-04-14 17:35:26 +02002884static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002885{
2886 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2887 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2888 return snb_gmch_ctl << 25; /* 32 MB units */
2889}
2890
Daniel Vetter2c642b02015-04-14 17:35:26 +02002891static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002892{
2893 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2894 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2895 return bdw_gmch_ctl << 25; /* 32 MB units */
2896}
2897
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002898static size_t chv_get_stolen_size(u16 gmch_ctrl)
2899{
2900 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2901 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2902
2903 /*
2904 * 0x0 to 0x10: 32MB increments starting at 0MB
2905 * 0x11 to 0x16: 4MB increments starting at 8MB
2906 * 0x17 to 0x1d: 4MB increments start at 36MB
2907 */
2908 if (gmch_ctrl < 0x11)
2909 return gmch_ctrl << 25;
2910 else if (gmch_ctrl < 0x17)
2911 return (gmch_ctrl - 0x11 + 2) << 22;
2912 else
2913 return (gmch_ctrl - 0x17 + 9) << 22;
2914}
2915
Damien Lespiau66375012014-01-09 18:02:46 +00002916static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2917{
2918 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2919 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2920
2921 if (gen9_gmch_ctl < 0xf0)
2922 return gen9_gmch_ctl << 25; /* 32 MB units */
2923 else
2924 /* 4MB increments starting at 0xf0 for 4MB */
2925 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2926}
2927
Chris Wilson34c998b2016-08-04 07:52:24 +01002928static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002929{
Chris Wilson49d73912016-11-29 09:50:08 +00002930 struct drm_i915_private *dev_priv = ggtt->base.i915;
2931 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002932 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002933 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002934
2935 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002936 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002937
Imre Deak2a073f892015-03-27 13:07:33 +02002938 /*
2939 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2940 * dropped. For WC mappings in general we have 64 byte burst writes
2941 * when the WC buffer is flushed, so we can't use it, but have to
2942 * resort to an uncached mapping. The WC issue is easily caught by the
2943 * readback check when writing GTT PTE entries.
2944 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002945 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002946 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002947 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002948 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002949 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002950 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002951 return -ENOMEM;
2952 }
2953
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002954 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002955 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002956 DRM_ERROR("Scratch setup failed\n");
2957 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002958 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002959 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002960 }
2961
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002962 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002963}
2964
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002965/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2966 * bits. When using advanced contexts each context stores its own PAT, but
2967 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002968static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002969{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002970 uint64_t pat;
2971
2972 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2973 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2974 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2975 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2976 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2977 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2978 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2979 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2980
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002981 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002982 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2983 * so RTL will always use the value corresponding to
2984 * pat_sel = 000".
2985 * So let's disable cache for GGTT to avoid screen corruptions.
2986 * MOCS still can be used though.
2987 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2988 * before this patch, i.e. the same uncached + snooping access
2989 * like on gen6/7 seems to be in effect.
2990 * - So this just fixes blitter/render access. Again it looks
2991 * like it's not just uncached access, but uncached + snooping.
2992 * So we can still hold onto all our assumptions wrt cpu
2993 * clflushing on LLC machines.
2994 */
2995 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2996
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002997 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2998 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002999 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3000 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003001}
3002
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003003static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3004{
3005 uint64_t pat;
3006
3007 /*
3008 * Map WB on BDW to snooped on CHV.
3009 *
3010 * Only the snoop bit has meaning for CHV, the rest is
3011 * ignored.
3012 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003013 * The hardware will never snoop for certain types of accesses:
3014 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3015 * - PPGTT page tables
3016 * - some other special cycles
3017 *
3018 * As with BDW, we also need to consider the following for GT accesses:
3019 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3020 * so RTL will always use the value corresponding to
3021 * pat_sel = 000".
3022 * Which means we must set the snoop bit in PAT entry 0
3023 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003024 */
3025 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3026 GEN8_PPAT(1, 0) |
3027 GEN8_PPAT(2, 0) |
3028 GEN8_PPAT(3, 0) |
3029 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3030 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3031 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3032 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3033
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003034 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3035 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003036}
3037
Chris Wilson34c998b2016-08-04 07:52:24 +01003038static void gen6_gmch_remove(struct i915_address_space *vm)
3039{
3040 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3041
3042 iounmap(ggtt->gsm);
Chris Wilson49d73912016-11-29 09:50:08 +00003043 cleanup_scratch_page(vm->i915, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003044}
3045
Joonas Lahtinend507d732016-03-18 10:42:58 +02003046static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003047{
Chris Wilson49d73912016-11-29 09:50:08 +00003048 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003049 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003050 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003051 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003052
3053 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003054 ggtt->mappable_base = pci_resource_start(pdev, 2);
3055 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003056
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003057 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3058 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003059
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003060 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003061
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003062 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003063 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003064 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003065 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003066 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003067 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003068 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003069 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003070 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003071 }
Ben Widawsky63340132013-11-04 19:32:22 -08003072
Chris Wilson34c998b2016-08-04 07:52:24 +01003073 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003074
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003075 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003076 chv_setup_private_ppat(dev_priv);
3077 else
3078 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003079
Chris Wilson34c998b2016-08-04 07:52:24 +01003080 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003081 ggtt->base.bind_vma = ggtt_bind_vma;
3082 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303083 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003084 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003085 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003086 ggtt->base.clear_range = gen8_ggtt_clear_range;
3087
3088 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3089 if (IS_CHERRYVIEW(dev_priv))
3090 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3091
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003092 ggtt->invalidate = gen6_ggtt_invalidate;
3093
Chris Wilson34c998b2016-08-04 07:52:24 +01003094 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003095}
3096
Joonas Lahtinend507d732016-03-18 10:42:58 +02003097static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003098{
Chris Wilson49d73912016-11-29 09:50:08 +00003099 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003100 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003101 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003102 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003103
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003104 ggtt->mappable_base = pci_resource_start(pdev, 2);
3105 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003106
Ben Widawskybaa09f52013-01-24 13:49:57 -08003107 /* 64/512MB is the current min/max we actually know of, but this is just
3108 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003109 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003110 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003111 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003112 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003113 }
3114
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003115 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3116 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3117 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003118
Joonas Lahtinend507d732016-03-18 10:42:58 +02003119 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003120
Chris Wilson34c998b2016-08-04 07:52:24 +01003121 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3122 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003123
Joonas Lahtinend507d732016-03-18 10:42:58 +02003124 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303125 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003126 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3127 ggtt->base.bind_vma = ggtt_bind_vma;
3128 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003129 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003130
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003131 ggtt->invalidate = gen6_ggtt_invalidate;
3132
Chris Wilson34c998b2016-08-04 07:52:24 +01003133 if (HAS_EDRAM(dev_priv))
3134 ggtt->base.pte_encode = iris_pte_encode;
3135 else if (IS_HASWELL(dev_priv))
3136 ggtt->base.pte_encode = hsw_pte_encode;
3137 else if (IS_VALLEYVIEW(dev_priv))
3138 ggtt->base.pte_encode = byt_pte_encode;
3139 else if (INTEL_GEN(dev_priv) >= 7)
3140 ggtt->base.pte_encode = ivb_pte_encode;
3141 else
3142 ggtt->base.pte_encode = snb_pte_encode;
3143
3144 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003145}
3146
Chris Wilson34c998b2016-08-04 07:52:24 +01003147static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003148{
Chris Wilson34c998b2016-08-04 07:52:24 +01003149 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003150}
3151
Joonas Lahtinend507d732016-03-18 10:42:58 +02003152static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003153{
Chris Wilson49d73912016-11-29 09:50:08 +00003154 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003155 int ret;
3156
Chris Wilson91c8a322016-07-05 10:40:23 +01003157 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003158 if (!ret) {
3159 DRM_ERROR("failed to set up gmch\n");
3160 return -EIO;
3161 }
3162
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003163 intel_gtt_get(&ggtt->base.total,
3164 &ggtt->stolen_size,
3165 &ggtt->mappable_base,
3166 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003167
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003168 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303169 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003170 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3171 ggtt->base.clear_range = i915_ggtt_clear_range;
3172 ggtt->base.bind_vma = ggtt_bind_vma;
3173 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003174 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003175
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003176 ggtt->invalidate = gmch_ggtt_invalidate;
3177
Joonas Lahtinend507d732016-03-18 10:42:58 +02003178 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003179 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3180
Ben Widawskybaa09f52013-01-24 13:49:57 -08003181 return 0;
3182}
3183
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003184/**
Chris Wilson0088e522016-08-04 07:52:21 +01003185 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003186 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003187 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003188int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003189{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003190 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003191 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003192
Chris Wilson49d73912016-11-29 09:50:08 +00003193 ggtt->base.i915 = dev_priv;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003194
Chris Wilson34c998b2016-08-04 07:52:24 +01003195 if (INTEL_GEN(dev_priv) <= 5)
3196 ret = i915_gmch_probe(ggtt);
3197 else if (INTEL_GEN(dev_priv) < 8)
3198 ret = gen6_gmch_probe(ggtt);
3199 else
3200 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003201 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003202 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003203
Chris Wilsondb9309a2017-01-05 15:30:23 +00003204 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3205 * This is easier than doing range restriction on the fly, as we
3206 * currently don't have any bits spare to pass in this upper
3207 * restriction!
3208 */
3209 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3210 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3211 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3212 }
3213
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003214 if ((ggtt->base.total - 1) >> 32) {
3215 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003216 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003217 ggtt->base.total >> 20);
3218 ggtt->base.total = 1ULL << 32;
3219 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3220 }
3221
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003222 if (ggtt->mappable_end > ggtt->base.total) {
3223 DRM_ERROR("mappable aperture extends past end of GGTT,"
3224 " aperture=%llx, total=%llx\n",
3225 ggtt->mappable_end, ggtt->base.total);
3226 ggtt->mappable_end = ggtt->base.total;
3227 }
3228
Ben Widawskybaa09f52013-01-24 13:49:57 -08003229 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003230 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003231 ggtt->base.total >> 20);
3232 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003233 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003234#ifdef CONFIG_INTEL_IOMMU
3235 if (intel_iommu_gfx_mapped)
3236 DRM_INFO("VT-d active for gfx access\n");
3237#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003238
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003239 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003240}
3241
3242/**
3243 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003244 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003245 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003246int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003247{
Chris Wilson0088e522016-08-04 07:52:21 +01003248 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3249 int ret;
3250
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003251 INIT_LIST_HEAD(&dev_priv->vm_list);
3252
3253 /* Subtract the guard page before address space initialization to
3254 * shrink the range used by drm_mm.
3255 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003256 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003257 ggtt->base.total -= PAGE_SIZE;
Chris Wilson80b204b2016-10-28 13:58:58 +01003258 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003259 ggtt->base.total += PAGE_SIZE;
3260 if (!HAS_LLC(dev_priv))
3261 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003262 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003263
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003264 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3265 dev_priv->ggtt.mappable_base,
3266 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003267 ret = -EIO;
3268 goto out_gtt_cleanup;
3269 }
3270
3271 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3272
Chris Wilson0088e522016-08-04 07:52:21 +01003273 /*
3274 * Initialise stolen early so that we may reserve preallocated
3275 * objects for the BIOS to KMS transition.
3276 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003277 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003278 if (ret)
3279 goto out_gtt_cleanup;
3280
3281 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003282
3283out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003284 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003285 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003286}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003287
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003288int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003289{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003290 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003291 return -EIO;
3292
3293 return 0;
3294}
3295
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003296void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3297{
3298 i915->ggtt.invalidate = guc_ggtt_invalidate;
3299}
3300
3301void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3302{
3303 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3304}
3305
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003306void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003307{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003308 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003309 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003310
Chris Wilsondc979972016-05-10 14:10:04 +01003311 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003312
3313 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003314 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003315
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003316 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3317
3318 /* clflush objects bound into the GGTT and rebind them. */
3319 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003320 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003321 bool ggtt_bound = false;
3322 struct i915_vma *vma;
3323
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003324 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003325 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003326 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003327
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003328 if (!i915_vma_unbind(vma))
3329 continue;
3330
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003331 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3332 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003333 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003334 }
3335
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003336 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003337 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003338 }
3339
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003340 ggtt->base.closed = false;
3341
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003342 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003343 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003344 chv_setup_private_ppat(dev_priv);
3345 else
3346 bdw_setup_private_ppat(dev_priv);
3347
3348 return;
3349 }
3350
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003351 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003352 struct i915_address_space *vm;
3353
Daniel Vetterfa423312015-04-14 17:35:23 +02003354 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3355 /* TODO: Perhaps it shouldn't be gen6 specific */
3356
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003357 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003358
Chris Wilson2bfa9962016-08-04 07:52:25 +01003359 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003360 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003361 else
3362 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003363
3364 gen6_write_page_range(dev_priv, &ppgtt->pd,
3365 0, ppgtt->base.total);
3366 }
3367 }
3368
Chris Wilson7c3f86b2017-01-12 11:00:49 +00003369 i915_ggtt_invalidate(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003370}
3371
Chris Wilson058d88c2016-08-15 10:49:06 +01003372struct i915_vma *
3373i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3374 struct i915_address_space *vm,
3375 const struct i915_ggtt_view *view)
3376{
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003377 struct rb_node *rb;
Chris Wilson058d88c2016-08-15 10:49:06 +01003378
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003379 rb = obj->vma_tree.rb_node;
3380 while (rb) {
3381 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
3382 long cmp;
3383
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003384 cmp = i915_vma_compare(vma, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003385 if (cmp == 0)
Chris Wilson058d88c2016-08-15 10:49:06 +01003386 return vma;
3387
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003388 if (cmp < 0)
3389 rb = rb->rb_right;
3390 else
3391 rb = rb->rb_left;
3392 }
3393
Chris Wilson058d88c2016-08-15 10:49:06 +01003394 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003395}
3396
3397struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003398i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003399 struct i915_address_space *vm,
3400 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003401{
3402 struct i915_vma *vma;
3403
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003404 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +01003405 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3406
3407 vma = i915_gem_obj_to_vma(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003408 if (!vma) {
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003409 vma = i915_vma_create(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003410 GEM_BUG_ON(vma != i915_gem_obj_to_vma(obj, vm, view));
3411 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003412
Chris Wilson3272db52016-08-04 16:32:32 +01003413 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003414 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003415}
3416
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003417static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003418rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003419 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003420 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003421 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003422{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003423 unsigned int column, row;
3424 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003425
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003426 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003427 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003428 for (row = 0; row < height; row++) {
3429 st->nents++;
3430 /* We don't need the pages, but need to initialize
3431 * the entries so the sg list can be happily traversed.
3432 * The only thing we need are DMA addresses.
3433 */
3434 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003435 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003436 sg_dma_len(sg) = PAGE_SIZE;
3437 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003438 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003439 }
3440 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003441
3442 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003443}
3444
3445static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003446intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003447 struct drm_i915_gem_object *obj)
3448{
Dave Gordon85d12252016-05-20 11:54:06 +01003449 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003450 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003451 struct sgt_iter sgt_iter;
3452 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003453 unsigned long i;
3454 dma_addr_t *page_addr_list;
3455 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003456 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003457 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003458
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003459 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003460 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003461 sizeof(dma_addr_t),
3462 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003463 if (!page_addr_list)
3464 return ERR_PTR(ret);
3465
3466 /* Allocate target SG list. */
3467 st = kmalloc(sizeof(*st), GFP_KERNEL);
3468 if (!st)
3469 goto err_st_alloc;
3470
Ville Syrjälä6687c902015-09-15 13:16:41 +03003471 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003472 if (ret)
3473 goto err_sg_alloc;
3474
3475 /* Populate source page list from the object. */
3476 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003477 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003478 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003479
Dave Gordon85d12252016-05-20 11:54:06 +01003480 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003481 st->nents = 0;
3482 sg = st->sgl;
3483
Ville Syrjälä6687c902015-09-15 13:16:41 +03003484 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3485 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3486 rot_info->plane[i].width, rot_info->plane[i].height,
3487 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003488 }
3489
Ville Syrjälä6687c902015-09-15 13:16:41 +03003490 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3491 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003492
3493 drm_free_large(page_addr_list);
3494
3495 return st;
3496
3497err_sg_alloc:
3498 kfree(st);
3499err_st_alloc:
3500 drm_free_large(page_addr_list);
3501
Ville Syrjälä6687c902015-09-15 13:16:41 +03003502 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3503 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3504
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003505 return ERR_PTR(ret);
3506}
3507
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003508static struct sg_table *
3509intel_partial_pages(const struct i915_ggtt_view *view,
3510 struct drm_i915_gem_object *obj)
3511{
3512 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003513 struct scatterlist *sg, *iter;
Chris Wilson8bab11932017-01-14 00:28:25 +00003514 unsigned int count = view->partial.size;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003515 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003516 int ret = -ENOMEM;
3517
3518 st = kmalloc(sizeof(*st), GFP_KERNEL);
3519 if (!st)
3520 goto err_st_alloc;
3521
Chris Wilsond2a84a72016-10-28 13:58:34 +01003522 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003523 if (ret)
3524 goto err_sg_alloc;
3525
Chris Wilson8bab11932017-01-14 00:28:25 +00003526 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
Chris Wilsond2a84a72016-10-28 13:58:34 +01003527 GEM_BUG_ON(!iter);
3528
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003529 sg = st->sgl;
3530 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003531 do {
3532 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003533
Chris Wilsond2a84a72016-10-28 13:58:34 +01003534 len = min(iter->length - (offset << PAGE_SHIFT),
3535 count << PAGE_SHIFT);
3536 sg_set_page(sg, NULL, len, 0);
3537 sg_dma_address(sg) =
3538 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3539 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003540
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003541 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003542 count -= len >> PAGE_SHIFT;
3543 if (count == 0) {
3544 sg_mark_end(sg);
3545 return st;
3546 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003547
Chris Wilsond2a84a72016-10-28 13:58:34 +01003548 sg = __sg_next(sg);
3549 iter = __sg_next(iter);
3550 offset = 0;
3551 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003552
3553err_sg_alloc:
3554 kfree(st);
3555err_st_alloc:
3556 return ERR_PTR(ret);
3557}
3558
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003559static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003560i915_get_ggtt_vma_pages(struct i915_vma *vma)
3561{
3562 int ret = 0;
3563
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003564 /* The vma->pages are only valid within the lifespan of the borrowed
3565 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3566 * must be the vma->pages. A simple rule is that vma->pages must only
3567 * be accessed when the obj->mm.pages are pinned.
3568 */
3569 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3570
Chris Wilson247177d2016-08-15 10:48:47 +01003571 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003572 return 0;
3573
3574 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003575 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003576 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003577 vma->pages =
Chris Wilson8bab11932017-01-14 00:28:25 +00003578 intel_rotate_fb_obj_pages(&vma->ggtt_view.rotated,
3579 vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003580 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003581 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003582 else
3583 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3584 vma->ggtt_view.type);
3585
Chris Wilson247177d2016-08-15 10:48:47 +01003586 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003587 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003588 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003589 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003590 } else if (IS_ERR(vma->pages)) {
3591 ret = PTR_ERR(vma->pages);
3592 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003593 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3594 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003595 }
3596
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003597 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003598}
3599
Chris Wilsone007b192017-01-11 11:23:10 +00003600/**
Chris Wilson625d9882017-01-11 11:23:11 +00003601 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003602 * @vm: the &struct i915_address_space
3603 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3604 * @size: how much space to allocate inside the GTT,
3605 * must be #I915_GTT_PAGE_SIZE aligned
3606 * @offset: where to insert inside the GTT,
3607 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3608 * (@offset + @size) must fit within the address space
3609 * @color: color to apply to node, if this node is not from a VMA,
3610 * color must be #I915_COLOR_UNEVICTABLE
3611 * @flags: control search and eviction behaviour
Chris Wilson625d9882017-01-11 11:23:11 +00003612 *
3613 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3614 * the address space (using @size and @color). If the @node does not fit, it
3615 * tries to evict any overlapping nodes from the GTT, including any
3616 * neighbouring nodes if the colors do not match (to ensure guard pages between
3617 * differing domains). See i915_gem_evict_for_node() for the gory details
3618 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3619 * evicting active overlapping objects, and any overlapping node that is pinned
3620 * or marked as unevictable will also result in failure.
3621 *
3622 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3623 * asked to wait for eviction and interrupted.
3624 */
3625int i915_gem_gtt_reserve(struct i915_address_space *vm,
3626 struct drm_mm_node *node,
3627 u64 size, u64 offset, unsigned long color,
3628 unsigned int flags)
3629{
3630 int err;
3631
3632 GEM_BUG_ON(!size);
3633 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3634 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3635 GEM_BUG_ON(range_overflows(offset, size, vm->total));
3636
3637 node->size = size;
3638 node->start = offset;
3639 node->color = color;
3640
3641 err = drm_mm_reserve_node(&vm->mm, node);
3642 if (err != -ENOSPC)
3643 return err;
3644
3645 err = i915_gem_evict_for_node(vm, node, flags);
3646 if (err == 0)
3647 err = drm_mm_reserve_node(&vm->mm, node);
3648
3649 return err;
3650}
3651
Chris Wilson606fec92017-01-11 11:23:12 +00003652static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3653{
3654 u64 range, addr;
3655
3656 GEM_BUG_ON(range_overflows(start, len, end));
3657 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3658
3659 range = round_down(end - len, align) - round_up(start, align);
3660 if (range) {
3661 if (sizeof(unsigned long) == sizeof(u64)) {
3662 addr = get_random_long();
3663 } else {
3664 addr = get_random_int();
3665 if (range > U32_MAX) {
3666 addr <<= 32;
3667 addr |= get_random_int();
3668 }
3669 }
3670 div64_u64_rem(addr, range, &addr);
3671 start += addr;
3672 }
3673
3674 return round_up(start, align);
3675}
3676
Chris Wilson625d9882017-01-11 11:23:11 +00003677/**
Chris Wilsone007b192017-01-11 11:23:10 +00003678 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003679 * @vm: the &struct i915_address_space
3680 * @node: the &struct drm_mm_node (typically i915_vma.node)
3681 * @size: how much space to allocate inside the GTT,
3682 * must be #I915_GTT_PAGE_SIZE aligned
3683 * @alignment: required alignment of starting offset, may be 0 but
3684 * if specified, this must be a power-of-two and at least
3685 * #I915_GTT_MIN_ALIGNMENT
3686 * @color: color to apply to node
3687 * @start: start of any range restriction inside GTT (0 for all),
Chris Wilsone007b192017-01-11 11:23:10 +00003688 * must be #I915_GTT_PAGE_SIZE aligned
Chris Wilsona4dbf7c2017-01-12 16:45:59 +00003689 * @end: end of any range restriction inside GTT (U64_MAX for all),
3690 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3691 * @flags: control search and eviction behaviour
Chris Wilsone007b192017-01-11 11:23:10 +00003692 *
3693 * i915_gem_gtt_insert() first searches for an available hole into which
3694 * is can insert the node. The hole address is aligned to @alignment and
3695 * its @size must then fit entirely within the [@start, @end] bounds. The
3696 * nodes on either side of the hole must match @color, or else a guard page
3697 * will be inserted between the two nodes (or the node evicted). If no
Chris Wilson606fec92017-01-11 11:23:12 +00003698 * suitable hole is found, first a victim is randomly selected and tested
3699 * for eviction, otherwise then the LRU list of objects within the GTT
Chris Wilsone007b192017-01-11 11:23:10 +00003700 * is scanned to find the first set of replacement nodes to create the hole.
3701 * Those old overlapping nodes are evicted from the GTT (and so must be
3702 * rebound before any future use). Any node that is currently pinned cannot
3703 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3704 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3705 * searching for an eviction candidate. See i915_gem_evict_something() for
3706 * the gory details on the eviction algorithm.
3707 *
3708 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3709 * asked to wait for eviction and interrupted.
3710 */
3711int i915_gem_gtt_insert(struct i915_address_space *vm,
3712 struct drm_mm_node *node,
3713 u64 size, u64 alignment, unsigned long color,
3714 u64 start, u64 end, unsigned int flags)
3715{
3716 u32 search_flag, alloc_flag;
Chris Wilson606fec92017-01-11 11:23:12 +00003717 u64 offset;
Chris Wilsone007b192017-01-11 11:23:10 +00003718 int err;
3719
3720 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3721 GEM_BUG_ON(!size);
3722 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3723 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3724 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3725 GEM_BUG_ON(start >= end);
3726 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3727 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
3728
3729 if (unlikely(range_overflows(start, size, end)))
3730 return -ENOSPC;
3731
3732 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3733 return -ENOSPC;
3734
3735 if (flags & PIN_HIGH) {
3736 search_flag = DRM_MM_SEARCH_BELOW;
3737 alloc_flag = DRM_MM_CREATE_TOP;
3738 } else {
3739 search_flag = DRM_MM_SEARCH_DEFAULT;
3740 alloc_flag = DRM_MM_CREATE_DEFAULT;
3741 }
3742
3743 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3744 * so we know that we always have a minimum alignment of 4096.
3745 * The drm_mm range manager is optimised to return results
3746 * with zero alignment, so where possible use the optimal
3747 * path.
3748 */
3749 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3750 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3751 alignment = 0;
3752
3753 err = drm_mm_insert_node_in_range_generic(&vm->mm, node,
3754 size, alignment, color,
3755 start, end,
3756 search_flag, alloc_flag);
3757 if (err != -ENOSPC)
3758 return err;
3759
Chris Wilson606fec92017-01-11 11:23:12 +00003760 /* No free space, pick a slot at random.
3761 *
3762 * There is a pathological case here using a GTT shared between
3763 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3764 *
3765 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3766 * (64k objects) (448k objects)
3767 *
3768 * Now imagine that the eviction LRU is ordered top-down (just because
3769 * pathology meets real life), and that we need to evict an object to
3770 * make room inside the aperture. The eviction scan then has to walk
3771 * the 448k list before it finds one within range. And now imagine that
3772 * it has to search for a new hole between every byte inside the memcpy,
3773 * for several simultaneous clients.
3774 *
3775 * On a full-ppgtt system, if we have run out of available space, there
3776 * will be lots and lots of objects in the eviction list! Again,
3777 * searching that LRU list may be slow if we are also applying any
3778 * range restrictions (e.g. restriction to low 4GiB) and so, for
3779 * simplicity and similarilty between different GTT, try the single
3780 * random replacement first.
3781 */
3782 offset = random_offset(start, end,
3783 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3784 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3785 if (err != -ENOSPC)
3786 return err;
3787
3788 /* Randomly selected placement is pinned, do a search */
Chris Wilsone007b192017-01-11 11:23:10 +00003789 err = i915_gem_evict_something(vm, size, alignment, color,
3790 start, end, flags);
3791 if (err)
3792 return err;
3793
3794 search_flag = DRM_MM_SEARCH_DEFAULT;
3795 return drm_mm_insert_node_in_range_generic(&vm->mm, node,
3796 size, alignment, color,
3797 start, end,
3798 search_flag, alloc_flag);
3799}