blob: 09a5eca494b35a822d71ba20bd74982a8a3ddd3e [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>
Daniel Vetter0e46ce22014-01-08 16:10:27 +010027#include <linux/seq_file.h>
Chris Wilson5bab6f62015-10-23 18:43:32 +010028#include <linux/stop_machine.h>
Chris Wilsone007b192017-01-11 11:23:10 +000029
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/drmP.h>
31#include <drm/i915_drm.h>
Chris Wilsone007b192017-01-11 11:23:10 +000032
Daniel Vetter76aaf222010-11-05 22:23:30 +010033#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080034#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010035#include "i915_trace.h"
36#include "intel_drv.h"
Chris Wilsond07f0e52016-10-28 13:58:44 +010037#include "intel_frontbuffer.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010038
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010039#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
40
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000041/**
42 * DOC: Global GTT views
43 *
44 * Background and previous state
45 *
46 * Historically objects could exists (be bound) in global GTT space only as
47 * singular instances with a view representing all of the object's backing pages
48 * in a linear fashion. This view will be called a normal view.
49 *
50 * To support multiple views of the same object, where the number of mapped
51 * pages is not equal to the backing store, or where the layout of the pages
52 * is not linear, concept of a GGTT view was added.
53 *
54 * One example of an alternative view is a stereo display driven by a single
55 * image. In this case we would have a framebuffer looking like this
56 * (2x2 pages):
57 *
58 * 12
59 * 34
60 *
61 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
62 * rendering. In contrast, fed to the display engine would be an alternative
63 * view which could look something like this:
64 *
65 * 1212
66 * 3434
67 *
68 * In this example both the size and layout of pages in the alternative view is
69 * different from the normal view.
70 *
71 * Implementation and usage
72 *
73 * GGTT views are implemented using VMAs and are distinguished via enum
74 * i915_ggtt_view_type and struct i915_ggtt_view.
75 *
76 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020077 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
78 * renaming in large amounts of code. They take the struct i915_ggtt_view
79 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000080 *
81 * As a helper for callers which are only interested in the normal view,
82 * globally const i915_ggtt_view_normal singleton instance exists. All old core
83 * GEM API functions, the ones not taking the view parameter, are operating on,
84 * or with the normal GGTT view.
85 *
86 * Code wanting to add or use a new GGTT view needs to:
87 *
88 * 1. Add a new enum with a suitable name.
89 * 2. Extend the metadata in the i915_ggtt_view structure if required.
90 * 3. Add support to i915_get_vma_pages().
91 *
92 * New views are required to build a scatter-gather table from within the
93 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
94 * exists for the lifetime of an VMA.
95 *
96 * Core API is designed to have copy semantics which means that passed in
97 * struct i915_ggtt_view does not need to be persistent (left around after
98 * calling the core API functions).
99 *
100 */
101
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200102static int
103i915_get_ggtt_vma_pages(struct i915_vma *vma);
104
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200105const struct i915_ggtt_view i915_ggtt_view_normal = {
106 .type = I915_GGTT_VIEW_NORMAL,
107};
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200108const struct i915_ggtt_view i915_ggtt_view_rotated = {
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200109 .type = I915_GGTT_VIEW_ROTATED,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200110};
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000111
Chris Wilsonc0336662016-05-06 15:40:21 +0100112int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
113 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200114{
Chris Wilson1893a712014-09-19 11:56:27 +0100115 bool has_aliasing_ppgtt;
116 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100117 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100118
Michel Thierry9e1d0e62016-12-05 17:57:03 -0800119 has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
120 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
121 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100122
Zhi Wange320d402016-09-06 12:04:12 +0800123 if (intel_vgpu_active(dev_priv)) {
124 /* emulation is too hard */
125 has_full_ppgtt = false;
126 has_full_48bit_ppgtt = false;
127 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800128
Chris Wilson0e4ca102016-04-29 13:18:22 +0100129 if (!has_aliasing_ppgtt)
130 return 0;
131
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000132 /*
133 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
134 * execlists, the sole mechanism available to submit work.
135 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100136 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200137 return 0;
138
139 if (enable_ppgtt == 1)
140 return 1;
141
Chris Wilson1893a712014-09-19 11:56:27 +0100142 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200143 return 2;
144
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100145 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
146 return 3;
147
Daniel Vetter93a25a92014-03-06 09:40:43 +0100148#ifdef CONFIG_INTEL_IOMMU
149 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100150 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100151 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200152 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100153 }
154#endif
155
Jesse Barnes62942ed2014-06-13 09:28:33 -0700156 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100157 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700158 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
159 return 0;
160 }
161
Zhi Wange320d402016-09-06 12:04:12 +0800162 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100163 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000164 else
165 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100166}
167
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200168static int ppgtt_bind_vma(struct i915_vma *vma,
169 enum i915_cache_level cache_level,
170 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200171{
172 u32 pte_flags = 0;
173
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100174 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100175
Daniel Vetter47552652015-04-14 17:35:24 +0200176 /* Currently applicable only to VLV */
177 if (vma->obj->gt_ro)
178 pte_flags |= PTE_READ_ONLY;
179
Chris Wilson247177d2016-08-15 10:48:47 +0100180 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200181 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200182
183 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200184}
185
186static void ppgtt_unbind_vma(struct i915_vma *vma)
187{
188 vma->vm->clear_range(vma->vm,
189 vma->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200190 vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200191}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800192
Daniel Vetter2c642b02015-04-14 17:35:26 +0200193static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200194 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700195{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200196 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700197 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300198
199 switch (level) {
200 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800201 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300202 break;
203 case I915_CACHE_WT:
204 pte |= PPAT_DISPLAY_ELLC_INDEX;
205 break;
206 default:
207 pte |= PPAT_CACHED_INDEX;
208 break;
209 }
210
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700211 return pte;
212}
213
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300214static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
215 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800216{
Michel Thierry07749ef2015-03-16 16:00:54 +0000217 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800218 pde |= addr;
219 if (level != I915_CACHE_NONE)
220 pde |= PPAT_CACHED_PDE_INDEX;
221 else
222 pde |= PPAT_UNCACHED_INDEX;
223 return pde;
224}
225
Michel Thierry762d9932015-07-30 11:05:29 +0100226#define gen8_pdpe_encode gen8_pde_encode
227#define gen8_pml4e_encode gen8_pde_encode
228
Michel Thierry07749ef2015-03-16 16:00:54 +0000229static gen6_pte_t snb_pte_encode(dma_addr_t addr,
230 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200231 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700232{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200233 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700234 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700235
236 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100237 case I915_CACHE_L3_LLC:
238 case I915_CACHE_LLC:
239 pte |= GEN6_PTE_CACHE_LLC;
240 break;
241 case I915_CACHE_NONE:
242 pte |= GEN6_PTE_UNCACHED;
243 break;
244 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100245 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100246 }
247
248 return pte;
249}
250
Michel Thierry07749ef2015-03-16 16:00:54 +0000251static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
252 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200253 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100254{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200255 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100256 pte |= GEN6_PTE_ADDR_ENCODE(addr);
257
258 switch (level) {
259 case I915_CACHE_L3_LLC:
260 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700261 break;
262 case I915_CACHE_LLC:
263 pte |= GEN6_PTE_CACHE_LLC;
264 break;
265 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700266 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700267 break;
268 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100269 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700270 }
271
Ben Widawsky54d12522012-09-24 16:44:32 -0700272 return pte;
273}
274
Michel Thierry07749ef2015-03-16 16:00:54 +0000275static gen6_pte_t byt_pte_encode(dma_addr_t addr,
276 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200277 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700278{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200279 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700280 pte |= GEN6_PTE_ADDR_ENCODE(addr);
281
Akash Goel24f3a8c2014-06-17 10:59:42 +0530282 if (!(flags & PTE_READ_ONLY))
283 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700284
285 if (level != I915_CACHE_NONE)
286 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
287
288 return pte;
289}
290
Michel Thierry07749ef2015-03-16 16:00:54 +0000291static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
292 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200293 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700294{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200295 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700296 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700297
298 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700299 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700300
301 return pte;
302}
303
Michel Thierry07749ef2015-03-16 16:00:54 +0000304static gen6_pte_t iris_pte_encode(dma_addr_t addr,
305 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200306 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700307{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200308 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700309 pte |= HSW_PTE_ADDR_ENCODE(addr);
310
Chris Wilson651d7942013-08-08 14:41:10 +0100311 switch (level) {
312 case I915_CACHE_NONE:
313 break;
314 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000315 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100316 break;
317 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000318 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100319 break;
320 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700321
322 return pte;
323}
324
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000325static int __setup_page_dma(struct drm_i915_private *dev_priv,
Mika Kuoppalac114f762015-06-25 18:35:13 +0300326 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000327{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000328 struct device *kdev = &dev_priv->drm.pdev->dev;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000329
Mika Kuoppalac114f762015-06-25 18:35:13 +0300330 p->page = alloc_page(flags);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300331 if (!p->page)
Michel Thierry1266cdb2015-03-24 17:06:33 +0000332 return -ENOMEM;
333
David Weinehallc49d13e2016-08-22 13:32:42 +0300334 p->daddr = dma_map_page(kdev,
Chris Wilsonf51455d2017-01-10 14:47:34 +0000335 p->page, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300336
David Weinehallc49d13e2016-08-22 13:32:42 +0300337 if (dma_mapping_error(kdev, p->daddr)) {
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300338 __free_page(p->page);
339 return -EINVAL;
340 }
341
Michel Thierry1266cdb2015-03-24 17:06:33 +0000342 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000343}
344
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000345static int setup_page_dma(struct drm_i915_private *dev_priv,
346 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300347{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000348 return __setup_page_dma(dev_priv, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300349}
350
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000351static void cleanup_page_dma(struct drm_i915_private *dev_priv,
352 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300353{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000354 struct pci_dev *pdev = dev_priv->drm.pdev;
David Weinehall52a05c32016-08-22 13:32:44 +0300355
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300356 if (WARN_ON(!p->page))
357 return;
358
Chris Wilsonf51455d2017-01-10 14:47:34 +0000359 dma_unmap_page(&pdev->dev, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300360 __free_page(p->page);
361 memset(p, 0, sizeof(*p));
362}
363
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300364static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300365{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300366 return kmap_atomic(p->page);
367}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300368
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300369/* We use the flushing unmap only with ppgtt structures:
370 * page directories, page tables and scratch pages.
371 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100372static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300373{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300374 /* There are only few exceptions for gen >=6. chv and bxt.
375 * And we are not sure about the latter so play safe for now.
376 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200377 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300378 drm_clflush_virt_range(vaddr, PAGE_SIZE);
379
380 kunmap_atomic(vaddr);
381}
382
Mika Kuoppala567047b2015-06-25 18:35:12 +0300383#define kmap_px(px) kmap_page_dma(px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100384#define kunmap_px(ppgtt, vaddr) \
Chris Wilson49d73912016-11-29 09:50:08 +0000385 kunmap_page_dma((ppgtt)->base.i915, (vaddr))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300386
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000387#define setup_px(dev_priv, px) setup_page_dma((dev_priv), px_base(px))
388#define cleanup_px(dev_priv, px) cleanup_page_dma((dev_priv), px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100389#define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
390#define fill32_px(dev_priv, px, v) \
391 fill_page_dma_32((dev_priv), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300392
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100393static void fill_page_dma(struct drm_i915_private *dev_priv,
394 struct i915_page_dma *p, const uint64_t val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300395{
396 int i;
397 uint64_t * const vaddr = kmap_page_dma(p);
398
399 for (i = 0; i < 512; i++)
400 vaddr[i] = val;
401
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100402 kunmap_page_dma(dev_priv, vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300403}
404
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100405static void fill_page_dma_32(struct drm_i915_private *dev_priv,
406 struct i915_page_dma *p, const uint32_t val32)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300407{
408 uint64_t v = val32;
409
410 v = v << 32 | val32;
411
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100412 fill_page_dma(dev_priv, p, v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300413}
414
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100415static int
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000416setup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100417 struct i915_page_dma *scratch,
418 gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300419{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000420 return __setup_page_dma(dev_priv, scratch, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300421}
422
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000423static void cleanup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100424 struct i915_page_dma *scratch)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300425{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000426 cleanup_page_dma(dev_priv, scratch);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300427}
428
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000429static struct i915_page_table *alloc_pt(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000430{
Michel Thierryec565b32015-04-08 12:13:23 +0100431 struct i915_page_table *pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000432 const size_t count = INTEL_GEN(dev_priv) >= 8 ? GEN8_PTES : GEN6_PTES;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000433 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000434
435 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
436 if (!pt)
437 return ERR_PTR(-ENOMEM);
438
Ben Widawsky678d96f2015-03-16 16:00:56 +0000439 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
440 GFP_KERNEL);
441
442 if (!pt->used_ptes)
443 goto fail_bitmap;
444
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000445 ret = setup_px(dev_priv, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000446 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300447 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000448
449 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000450
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300451fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000452 kfree(pt->used_ptes);
453fail_bitmap:
454 kfree(pt);
455
456 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000457}
458
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000459static void free_pt(struct drm_i915_private *dev_priv,
460 struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000461{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000462 cleanup_px(dev_priv, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300463 kfree(pt->used_ptes);
464 kfree(pt);
465}
466
467static void gen8_initialize_pt(struct i915_address_space *vm,
468 struct i915_page_table *pt)
469{
470 gen8_pte_t scratch_pte;
471
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100472 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200473 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300474
Chris Wilson49d73912016-11-29 09:50:08 +0000475 fill_px(vm->i915, pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300476}
477
478static void gen6_initialize_pt(struct i915_address_space *vm,
479 struct i915_page_table *pt)
480{
481 gen6_pte_t scratch_pte;
482
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100483 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300484
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100485 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200486 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300487
Chris Wilson49d73912016-11-29 09:50:08 +0000488 fill32_px(vm->i915, pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000489}
490
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000491static struct i915_page_directory *alloc_pd(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000492{
Michel Thierryec565b32015-04-08 12:13:23 +0100493 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100494 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000495
496 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
497 if (!pd)
498 return ERR_PTR(-ENOMEM);
499
Michel Thierry33c88192015-04-08 12:13:33 +0100500 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
501 sizeof(*pd->used_pdes), GFP_KERNEL);
502 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300503 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100504
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000505 ret = setup_px(dev_priv, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100506 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300507 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100508
Ben Widawsky06fda602015-02-24 16:22:36 +0000509 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100510
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300511fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100512 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300513fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100514 kfree(pd);
515
516 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000517}
518
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000519static void free_pd(struct drm_i915_private *dev_priv,
520 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300521{
522 if (px_page(pd)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000523 cleanup_px(dev_priv, pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300524 kfree(pd->used_pdes);
525 kfree(pd);
526 }
527}
528
529static void gen8_initialize_pd(struct i915_address_space *vm,
530 struct i915_page_directory *pd)
531{
532 gen8_pde_t scratch_pde;
533
534 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
535
Chris Wilson49d73912016-11-29 09:50:08 +0000536 fill_px(vm->i915, pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300537}
538
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000539static int __pdp_init(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100540 struct i915_page_directory_pointer *pdp)
541{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000542 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Michel Thierry6ac18502015-07-29 17:23:46 +0100543
544 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
545 sizeof(unsigned long),
546 GFP_KERNEL);
547 if (!pdp->used_pdpes)
548 return -ENOMEM;
549
550 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
551 GFP_KERNEL);
552 if (!pdp->page_directory) {
553 kfree(pdp->used_pdpes);
554 /* the PDP might be the statically allocated top level. Keep it
555 * as clean as possible */
556 pdp->used_pdpes = NULL;
557 return -ENOMEM;
558 }
559
560 return 0;
561}
562
563static void __pdp_fini(struct i915_page_directory_pointer *pdp)
564{
565 kfree(pdp->used_pdpes);
566 kfree(pdp->page_directory);
567 pdp->page_directory = NULL;
568}
569
Michel Thierry762d9932015-07-30 11:05:29 +0100570static struct
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000571i915_page_directory_pointer *alloc_pdp(struct drm_i915_private *dev_priv)
Michel Thierry762d9932015-07-30 11:05:29 +0100572{
573 struct i915_page_directory_pointer *pdp;
574 int ret = -ENOMEM;
575
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000576 WARN_ON(!USES_FULL_48BIT_PPGTT(dev_priv));
Michel Thierry762d9932015-07-30 11:05:29 +0100577
578 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
579 if (!pdp)
580 return ERR_PTR(-ENOMEM);
581
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000582 ret = __pdp_init(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100583 if (ret)
584 goto fail_bitmap;
585
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000586 ret = setup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100587 if (ret)
588 goto fail_page_m;
589
590 return pdp;
591
592fail_page_m:
593 __pdp_fini(pdp);
594fail_bitmap:
595 kfree(pdp);
596
597 return ERR_PTR(ret);
598}
599
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000600static void free_pdp(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100601 struct i915_page_directory_pointer *pdp)
602{
603 __pdp_fini(pdp);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000604 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
605 cleanup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100606 kfree(pdp);
607 }
608}
609
Michel Thierry69ab76f2015-07-29 17:23:55 +0100610static void gen8_initialize_pdp(struct i915_address_space *vm,
611 struct i915_page_directory_pointer *pdp)
612{
613 gen8_ppgtt_pdpe_t scratch_pdpe;
614
615 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
616
Chris Wilson49d73912016-11-29 09:50:08 +0000617 fill_px(vm->i915, pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100618}
619
620static void gen8_initialize_pml4(struct i915_address_space *vm,
621 struct i915_pml4 *pml4)
622{
623 gen8_ppgtt_pml4e_t scratch_pml4e;
624
625 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
626 I915_CACHE_LLC);
627
Chris Wilson49d73912016-11-29 09:50:08 +0000628 fill_px(vm->i915, pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100629}
630
Michel Thierry762d9932015-07-30 11:05:29 +0100631static void
Matthew Auld5c693b22016-12-13 16:05:10 +0000632gen8_setup_pdpe(struct i915_hw_ppgtt *ppgtt,
633 struct i915_page_directory_pointer *pdp,
634 struct i915_page_directory *pd,
635 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100636{
637 gen8_ppgtt_pdpe_t *page_directorypo;
638
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000639 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
Michel Thierry762d9932015-07-30 11:05:29 +0100640 return;
641
642 page_directorypo = kmap_px(pdp);
643 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
644 kunmap_px(ppgtt, page_directorypo);
645}
646
647static void
Matthew Auld56843102016-12-13 16:05:11 +0000648gen8_setup_pml4e(struct i915_hw_ppgtt *ppgtt,
649 struct i915_pml4 *pml4,
650 struct i915_page_directory_pointer *pdp,
651 int index)
Michel Thierry762d9932015-07-30 11:05:29 +0100652{
653 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
654
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000655 WARN_ON(!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)));
Michel Thierry762d9932015-07-30 11:05:29 +0100656 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
657 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100658}
659
Ben Widawsky94e409c2013-11-04 22:29:36 -0800660/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100661static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100662 unsigned entry,
663 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800664{
Chris Wilson7e37f882016-08-02 22:50:21 +0100665 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000666 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800667 int ret;
668
669 BUG_ON(entry >= 4);
670
John Harrison5fb9de12015-05-29 17:44:07 +0100671 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800672 if (ret)
673 return ret;
674
Chris Wilsonb5321f32016-08-02 22:50:18 +0100675 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
676 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
677 intel_ring_emit(ring, upper_32_bits(addr));
678 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
679 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
680 intel_ring_emit(ring, lower_32_bits(addr));
681 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800682
683 return 0;
684}
685
Michel Thierry2dba3232015-07-30 11:06:23 +0100686static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
687 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800688{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800689 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800690
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100691 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300692 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
693
John Harrisone85b26d2015-05-29 17:43:56 +0100694 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800695 if (ret)
696 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800697 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800698
Ben Widawskyeeb94882013-12-06 14:11:10 -0800699 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800700}
701
Michel Thierry2dba3232015-07-30 11:06:23 +0100702static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
703 struct drm_i915_gem_request *req)
704{
705 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
706}
707
Mika Kuoppalafce93752016-10-31 17:24:46 +0200708/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
709 * the page table structures, we mark them dirty so that
710 * context switching/execlist queuing code takes extra steps
711 * to ensure that tlbs are flushed.
712 */
713static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
714{
Chris Wilson49d73912016-11-29 09:50:08 +0000715 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200716}
717
Michał Winiarski2ce51792016-10-13 14:02:42 +0200718/* Removes entries from a single page table, releasing it if it's empty.
719 * Caller can use the return value to update higher-level entries.
720 */
721static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200722 struct i915_page_table *pt,
723 uint64_t start,
724 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700725{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300726 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200727 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200728 unsigned int pte = gen8_pte_index(start);
729 unsigned int pte_end = pte + num_entries;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200730 gen8_pte_t *pt_vaddr;
731 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
732 I915_CACHE_LLC);
733
734 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200735 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700736
Mika Kuoppala37c63932016-11-01 15:27:36 +0200737 GEM_BUG_ON(pte_end > GEN8_PTES);
738
739 bitmap_clear(pt->used_ptes, pte, num_entries);
Ben Widawsky06fda602015-02-24 16:22:36 +0000740
Zhi Wanga18dbba2016-11-29 14:55:16 +0800741 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200742 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200743
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200744 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000745
Mika Kuoppala37c63932016-11-01 15:27:36 +0200746 while (pte < pte_end)
747 pt_vaddr[pte++] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000748
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200749 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200750
751 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200752}
753
Michał Winiarski2ce51792016-10-13 14:02:42 +0200754/* Removes entries from a single page dir, releasing it if it's empty.
755 * Caller can use the return value to update higher-level entries
756 */
757static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200758 struct i915_page_directory *pd,
759 uint64_t start,
760 uint64_t length)
761{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200762 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200763 struct i915_page_table *pt;
764 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200765 gen8_pde_t *pde_vaddr;
766 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
767 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200768
769 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000770 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100771 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000772
Michał Winiarski2ce51792016-10-13 14:02:42 +0200773 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
774 __clear_bit(pde, pd->used_pdes);
775 pde_vaddr = kmap_px(pd);
776 pde_vaddr[pde] = scratch_pde;
777 kunmap_px(ppgtt, pde_vaddr);
Chris Wilson49d73912016-11-29 09:50:08 +0000778 free_pt(vm->i915, pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200779 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200780 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200781
Zhi Wanga18dbba2016-11-29 14:55:16 +0800782 if (bitmap_empty(pd->used_pdes, I915_PDES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200783 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200784
785 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200786}
Ben Widawsky06fda602015-02-24 16:22:36 +0000787
Michał Winiarski2ce51792016-10-13 14:02:42 +0200788/* Removes entries from a single page dir pointer, releasing it if it's empty.
789 * Caller can use the return value to update higher-level entries
790 */
791static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200792 struct i915_page_directory_pointer *pdp,
793 uint64_t start,
794 uint64_t length)
795{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200796 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200797 struct i915_page_directory *pd;
798 uint64_t pdpe;
799
800 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
801 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100802 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000803
Michał Winiarski2ce51792016-10-13 14:02:42 +0200804 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
805 __clear_bit(pdpe, pdp->used_pdpes);
Matthew Auld9e65a372016-12-13 16:05:12 +0000806 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
Chris Wilson49d73912016-11-29 09:50:08 +0000807 free_pd(vm->i915, pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200808 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200809 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200810
Mika Kuoppalafce93752016-10-31 17:24:46 +0200811 mark_tlbs_dirty(ppgtt);
812
Zhi Wanga18dbba2016-11-29 14:55:16 +0800813 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200814 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200815
816 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200817}
Ben Widawsky459108b2013-11-02 21:07:23 -0700818
Michał Winiarski2ce51792016-10-13 14:02:42 +0200819/* Removes entries from a single pml4.
820 * This is the top-level structure in 4-level page tables used on gen8+.
821 * Empty entries are always scratch pml4e.
822 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200823static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
824 struct i915_pml4 *pml4,
825 uint64_t start,
826 uint64_t length)
827{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200828 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200829 struct i915_page_directory_pointer *pdp;
830 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200831
Chris Wilson49d73912016-11-29 09:50:08 +0000832 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700833
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200834 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
835 if (WARN_ON(!pml4->pdps[pml4e]))
836 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700837
Michał Winiarski2ce51792016-10-13 14:02:42 +0200838 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
839 __clear_bit(pml4e, pml4->used_pml4es);
Matthew Auld9e65a372016-12-13 16:05:12 +0000840 gen8_setup_pml4e(ppgtt, pml4, vm->scratch_pdp, pml4e);
Chris Wilson49d73912016-11-29 09:50:08 +0000841 free_pdp(vm->i915, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200842 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700843 }
844}
845
Michel Thierryf9b5b782015-07-30 11:02:49 +0100846static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200847 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700848{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300849 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100850
Chris Wilsonc6385c92016-11-29 12:42:05 +0000851 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200852 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
853 else
854 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100855}
856
857static void
858gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
859 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100860 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100861 uint64_t start,
862 enum i915_cache_level cache_level)
863{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300864 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000865 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100866 unsigned pdpe = gen8_pdpe_index(start);
867 unsigned pde = gen8_pde_index(start);
868 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700869
Chris Wilson6f1cc992013-12-31 15:50:31 +0000870 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700871
Michel Thierry3387d432015-08-03 09:52:47 +0100872 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000873 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100874 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100875 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300876 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000877 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800878
879 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100880 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200881 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000882 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300883 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000884 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000885 if (++pde == I915_PDES) {
Chris Wilsonc6385c92016-11-29 12:42:05 +0000886 if (++pdpe == I915_PDPES_PER_PDP(vm->i915))
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100887 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800888 pde = 0;
889 }
890 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700891 }
892 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300893
894 if (pt_vaddr)
895 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700896}
897
Michel Thierryf9b5b782015-07-30 11:02:49 +0100898static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
899 struct sg_table *pages,
900 uint64_t start,
901 enum i915_cache_level cache_level,
902 u32 unused)
903{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300904 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100905 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100906
Michel Thierry3387d432015-08-03 09:52:47 +0100907 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100908
Chris Wilsonc6385c92016-11-29 12:42:05 +0000909 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100910 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
911 cache_level);
912 } else {
913 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000914 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100915 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
916
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000917 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100918 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
919 start, cache_level);
920 }
921 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100922}
923
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000924static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
Michel Thierryf37c0502015-06-10 17:46:39 +0100925 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800926{
927 int i;
928
Mika Kuoppala567047b2015-06-25 18:35:12 +0300929 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800930 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800931
Michel Thierry33c88192015-04-08 12:13:33 +0100932 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000933 if (WARN_ON(!pd->page_table[i]))
934 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800935
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000936 free_pt(dev_priv, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000937 pd->page_table[i] = NULL;
938 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000939}
940
Mika Kuoppala8776f022015-06-30 18:16:40 +0300941static int gen8_init_scratch(struct i915_address_space *vm)
942{
Chris Wilson49d73912016-11-29 09:50:08 +0000943 struct drm_i915_private *dev_priv = vm->i915;
Matthew Auld64c050d2016-04-27 13:19:25 +0100944 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300945
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000946 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100947 if (ret)
948 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300949
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000950 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300951 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100952 ret = PTR_ERR(vm->scratch_pt);
953 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300954 }
955
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000956 vm->scratch_pd = alloc_pd(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300957 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100958 ret = PTR_ERR(vm->scratch_pd);
959 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300960 }
961
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000962 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
963 vm->scratch_pdp = alloc_pdp(dev_priv);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100964 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100965 ret = PTR_ERR(vm->scratch_pdp);
966 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100967 }
968 }
969
Mika Kuoppala8776f022015-06-30 18:16:40 +0300970 gen8_initialize_pt(vm, vm->scratch_pt);
971 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000972 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100973 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300974
975 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100976
977free_pd:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000978 free_pd(dev_priv, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +0100979free_pt:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000980 free_pt(dev_priv, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +0100981free_scratch_page:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000982 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +0100983
984 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300985}
986
Zhiyuan Lv650da342015-08-28 15:41:18 +0800987static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
988{
989 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +0000990 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +0800991 int i;
992
Matthew Aulddf285642016-04-22 12:09:25 +0100993 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +0800994 u64 daddr = px_dma(&ppgtt->pml4);
995
Ville Syrjäläab75bb52015-11-04 23:20:12 +0200996 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
997 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +0800998
999 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1000 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1001 } else {
1002 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1003 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1004
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001005 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1006 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001007 }
1008
1009 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1010 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1011 }
1012
1013 I915_WRITE(vgtif_reg(g2v_notify), msg);
1014
1015 return 0;
1016}
1017
Mika Kuoppala8776f022015-06-30 18:16:40 +03001018static void gen8_free_scratch(struct i915_address_space *vm)
1019{
Chris Wilson49d73912016-11-29 09:50:08 +00001020 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001021
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001022 if (USES_FULL_48BIT_PPGTT(dev_priv))
1023 free_pdp(dev_priv, vm->scratch_pdp);
1024 free_pd(dev_priv, vm->scratch_pd);
1025 free_pt(dev_priv, vm->scratch_pt);
1026 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001027}
1028
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001029static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
Michel Thierry762d9932015-07-30 11:05:29 +01001030 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001031{
1032 int i;
1033
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001034 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001035 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001036 continue;
1037
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001038 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1039 free_pd(dev_priv, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001040 }
Michel Thierry69876be2015-04-08 12:13:27 +01001041
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001042 free_pdp(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001043}
1044
1045static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1046{
Chris Wilson49d73912016-11-29 09:50:08 +00001047 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001048 int i;
1049
1050 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1051 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1052 continue;
1053
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001054 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001055 }
1056
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001057 cleanup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001058}
1059
1060static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1061{
Chris Wilson49d73912016-11-29 09:50:08 +00001062 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001063 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001064
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001065 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001066 gen8_ppgtt_notify_vgt(ppgtt, false);
1067
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001068 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1069 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001070 else
1071 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001072
Mika Kuoppala8776f022015-06-30 18:16:40 +03001073 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001074}
1075
Michel Thierryd7b26332015-04-08 12:13:34 +01001076/**
1077 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001078 * @vm: Master vm structure.
1079 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001080 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001081 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001082 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1083 * caller to free on error.
1084 *
1085 * Allocate the required number of page tables. Extremely similar to
1086 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1087 * the page directory boundary (instead of the page directory pointer). That
1088 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1089 * possible, and likely that the caller will need to use multiple calls of this
1090 * function to achieve the appropriate allocation.
1091 *
1092 * Return: 0 if success; negative error code otherwise.
1093 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001094static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001095 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001096 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001097 uint64_t length,
1098 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001099{
Chris Wilson49d73912016-11-29 09:50:08 +00001100 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001101 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001102 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001103
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001104 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001105 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001106 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001107 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001108 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001109 continue;
1110 }
1111
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001112 pt = alloc_pt(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001113 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001114 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001115
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001116 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001117 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001118 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001119 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001120 }
1121
1122 return 0;
1123
1124unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001125 for_each_set_bit(pde, new_pts, I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001126 free_pt(dev_priv, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001127
1128 return -ENOMEM;
1129}
1130
Michel Thierryd7b26332015-04-08 12:13:34 +01001131/**
1132 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001133 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001134 * @pdp: Page directory pointer for this address range.
1135 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001136 * @length: Size of the allocations.
1137 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001138 * caller to free on error.
1139 *
1140 * Allocate the required number of page directories starting at the pde index of
1141 * @start, and ending at the pde index @start + @length. This function will skip
1142 * over already allocated page directories within the range, and only allocate
1143 * new ones, setting the appropriate pointer within the pdp as well as the
1144 * correct position in the bitmap @new_pds.
1145 *
1146 * The function will only allocate the pages within the range for a give page
1147 * directory pointer. In other words, if @start + @length straddles a virtually
1148 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1149 * required by the caller, This is not currently possible, and the BUG in the
1150 * code will prevent it.
1151 *
1152 * Return: 0 if success; negative error code otherwise.
1153 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001154static int
1155gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1156 struct i915_page_directory_pointer *pdp,
1157 uint64_t start,
1158 uint64_t length,
1159 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001160{
Chris Wilson49d73912016-11-29 09:50:08 +00001161 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001162 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001163 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001164 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001165
Michel Thierry6ac18502015-07-29 17:23:46 +01001166 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001167
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001168 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001169 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001170 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001171
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001172 pd = alloc_pd(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001173 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001174 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001175
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001176 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001177 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001178 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001179 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001180 }
1181
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001182 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001183
1184unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001185 for_each_set_bit(pdpe, new_pds, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001186 free_pd(dev_priv, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001187
1188 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001189}
1190
Michel Thierry762d9932015-07-30 11:05:29 +01001191/**
1192 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1193 * @vm: Master vm structure.
1194 * @pml4: Page map level 4 for this address range.
1195 * @start: Starting virtual address to begin allocations.
1196 * @length: Size of the allocations.
1197 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1198 * caller to free on error.
1199 *
1200 * Allocate the required number of page directory pointers. Extremely similar to
1201 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1202 * The main difference is here we are limited by the pml4 boundary (instead of
1203 * the page directory pointer).
1204 *
1205 * Return: 0 if success; negative error code otherwise.
1206 */
1207static int
1208gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1209 struct i915_pml4 *pml4,
1210 uint64_t start,
1211 uint64_t length,
1212 unsigned long *new_pdps)
1213{
Chris Wilson49d73912016-11-29 09:50:08 +00001214 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001215 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001216 uint32_t pml4e;
1217
1218 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1219
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001220 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001221 if (!test_bit(pml4e, pml4->used_pml4es)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001222 pdp = alloc_pdp(dev_priv);
Michel Thierry762d9932015-07-30 11:05:29 +01001223 if (IS_ERR(pdp))
1224 goto unwind_out;
1225
Michel Thierry69ab76f2015-07-29 17:23:55 +01001226 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001227 pml4->pdps[pml4e] = pdp;
1228 __set_bit(pml4e, new_pdps);
1229 trace_i915_page_directory_pointer_entry_alloc(vm,
1230 pml4e,
1231 start,
1232 GEN8_PML4E_SHIFT);
1233 }
1234 }
1235
1236 return 0;
1237
1238unwind_out:
1239 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001240 free_pdp(dev_priv, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001241
1242 return -ENOMEM;
1243}
1244
Michel Thierryd7b26332015-04-08 12:13:34 +01001245static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001246free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001247{
Michel Thierryd7b26332015-04-08 12:13:34 +01001248 kfree(new_pts);
1249 kfree(new_pds);
1250}
1251
1252/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1253 * of these are based on the number of PDPEs in the system.
1254 */
1255static
1256int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001257 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001258 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001259{
Michel Thierryd7b26332015-04-08 12:13:34 +01001260 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001261 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001262
Michał Winiarski3a41a052015-09-03 19:22:18 +02001263 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001264 if (!pds)
1265 return -ENOMEM;
1266
Michał Winiarski3a41a052015-09-03 19:22:18 +02001267 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1268 GFP_TEMPORARY);
1269 if (!pts)
1270 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001271
1272 *new_pds = pds;
1273 *new_pts = pts;
1274
1275 return 0;
1276
1277err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001278 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001279 return -ENOMEM;
1280}
1281
Michel Thierry762d9932015-07-30 11:05:29 +01001282static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1283 struct i915_page_directory_pointer *pdp,
1284 uint64_t start,
1285 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001286{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001287 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001288 unsigned long *new_page_dirs, *new_page_tables;
Chris Wilson49d73912016-11-29 09:50:08 +00001289 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001290 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001291 const uint64_t orig_start = start;
1292 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001293 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001294 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001295 int ret;
1296
Michel Thierry6ac18502015-07-29 17:23:46 +01001297 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001298 if (ret)
1299 return ret;
1300
Michel Thierryd7b26332015-04-08 12:13:34 +01001301 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001302 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1303 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001304 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001305 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001306 return ret;
1307 }
1308
1309 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001310 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001311 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001312 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001313 if (ret)
1314 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001315 }
1316
Michel Thierry33c88192015-04-08 12:13:33 +01001317 start = orig_start;
1318 length = orig_length;
1319
Michel Thierryd7b26332015-04-08 12:13:34 +01001320 /* Allocations have completed successfully, so set the bitmaps, and do
1321 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001322 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001323 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001324 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001325 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001326 uint64_t pd_start = start;
1327 uint32_t pde;
1328
Michel Thierryd7b26332015-04-08 12:13:34 +01001329 /* Every pd should be allocated, we just did that above. */
1330 WARN_ON(!pd);
1331
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001332 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001333 /* Same reasoning as pd */
1334 WARN_ON(!pt);
1335 WARN_ON(!pd_len);
1336 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1337
1338 /* Set our used ptes within the page table */
1339 bitmap_set(pt->used_ptes,
1340 gen8_pte_index(pd_start),
1341 gen8_pte_count(pd_start, pd_len));
1342
1343 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001344 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001345
1346 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001347 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1348 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001349 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1350 gen8_pte_index(start),
1351 gen8_pte_count(start, length),
1352 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001353
1354 /* NB: We haven't yet mapped ptes to pages. At this
1355 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001356 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001357
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001358 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001359 __set_bit(pdpe, pdp->used_pdpes);
Matthew Auld5c693b22016-12-13 16:05:10 +00001360 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001361 }
1362
Michał Winiarski3a41a052015-09-03 19:22:18 +02001363 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001364 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001365 return 0;
1366
1367err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001368 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001369 unsigned long temp;
1370
Michał Winiarski3a41a052015-09-03 19:22:18 +02001371 for_each_set_bit(temp, new_page_tables + pdpe *
1372 BITS_TO_LONGS(I915_PDES), I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001373 free_pt(dev_priv,
1374 pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001375 }
1376
Michel Thierry6ac18502015-07-29 17:23:46 +01001377 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001378 free_pd(dev_priv, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001379
Michał Winiarski3a41a052015-09-03 19:22:18 +02001380 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001381 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001382 return ret;
1383}
1384
Michel Thierry762d9932015-07-30 11:05:29 +01001385static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1386 struct i915_pml4 *pml4,
1387 uint64_t start,
1388 uint64_t length)
1389{
1390 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001391 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001392 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001393 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001394 int ret = 0;
1395
1396 /* Do the pml4 allocations first, so we don't need to track the newly
1397 * allocated tables below the pdp */
1398 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1399
1400 /* The pagedirectory and pagetable allocations are done in the shared 3
1401 * and 4 level code. Just allocate the pdps.
1402 */
1403 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1404 new_pdps);
1405 if (ret)
1406 return ret;
1407
1408 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1409 "The allocation has spanned more than 512GB. "
1410 "It is highly likely this is incorrect.");
1411
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001412 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001413 WARN_ON(!pdp);
1414
1415 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1416 if (ret)
1417 goto err_out;
1418
Matthew Auld56843102016-12-13 16:05:11 +00001419 gen8_setup_pml4e(ppgtt, pml4, pdp, pml4e);
Michel Thierry762d9932015-07-30 11:05:29 +01001420 }
1421
1422 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1423 GEN8_PML4ES_PER_PML4);
1424
1425 return 0;
1426
1427err_out:
1428 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson49d73912016-11-29 09:50:08 +00001429 gen8_ppgtt_cleanup_3lvl(vm->i915, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001430
1431 return ret;
1432}
1433
1434static int gen8_alloc_va_range(struct i915_address_space *vm,
1435 uint64_t start, uint64_t length)
1436{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001437 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001438
Chris Wilsonc6385c92016-11-29 12:42:05 +00001439 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001440 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1441 else
1442 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1443}
1444
Michel Thierryea91e402015-07-29 17:23:57 +01001445static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1446 uint64_t start, uint64_t length,
1447 gen8_pte_t scratch_pte,
1448 struct seq_file *m)
1449{
1450 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001451 uint32_t pdpe;
1452
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001453 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001454 struct i915_page_table *pt;
1455 uint64_t pd_len = length;
1456 uint64_t pd_start = start;
1457 uint32_t pde;
1458
1459 if (!test_bit(pdpe, pdp->used_pdpes))
1460 continue;
1461
1462 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001463 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001464 uint32_t pte;
1465 gen8_pte_t *pt_vaddr;
1466
1467 if (!test_bit(pde, pd->used_pdes))
1468 continue;
1469
1470 pt_vaddr = kmap_px(pt);
1471 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1472 uint64_t va =
1473 (pdpe << GEN8_PDPE_SHIFT) |
1474 (pde << GEN8_PDE_SHIFT) |
1475 (pte << GEN8_PTE_SHIFT);
1476 int i;
1477 bool found = false;
1478
1479 for (i = 0; i < 4; i++)
1480 if (pt_vaddr[pte + i] != scratch_pte)
1481 found = true;
1482 if (!found)
1483 continue;
1484
1485 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1486 for (i = 0; i < 4; i++) {
1487 if (pt_vaddr[pte + i] != scratch_pte)
1488 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1489 else
1490 seq_puts(m, " SCRATCH ");
1491 }
1492 seq_puts(m, "\n");
1493 }
1494 /* don't use kunmap_px, it could trigger
1495 * an unnecessary flush.
1496 */
1497 kunmap_atomic(pt_vaddr);
1498 }
1499 }
1500}
1501
1502static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1503{
1504 struct i915_address_space *vm = &ppgtt->base;
1505 uint64_t start = ppgtt->base.start;
1506 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001507 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001508 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001509
Chris Wilsonc6385c92016-11-29 12:42:05 +00001510 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryea91e402015-07-29 17:23:57 +01001511 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1512 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001513 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001514 struct i915_pml4 *pml4 = &ppgtt->pml4;
1515 struct i915_page_directory_pointer *pdp;
1516
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001517 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001518 if (!test_bit(pml4e, pml4->used_pml4es))
1519 continue;
1520
1521 seq_printf(m, " PML4E #%llu\n", pml4e);
1522 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1523 }
1524 }
1525}
1526
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001527static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1528{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001529 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001530 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001531 int ret;
1532
1533 /* We allocate temp bitmap for page tables for no gain
1534 * but as this is for init only, lets keep the things simple
1535 */
1536 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1537 if (ret)
1538 return ret;
1539
1540 /* Allocate for all pdps regardless of how the ppgtt
1541 * was defined.
1542 */
1543 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1544 0, 1ULL << 32,
1545 new_page_dirs);
1546 if (!ret)
1547 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1548
Michał Winiarski3a41a052015-09-03 19:22:18 +02001549 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001550
1551 return ret;
1552}
1553
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001554/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001555 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1556 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1557 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1558 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001559 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001560 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001561static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001562{
Chris Wilson49d73912016-11-29 09:50:08 +00001563 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001564 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001565
Mika Kuoppala8776f022015-06-30 18:16:40 +03001566 ret = gen8_init_scratch(&ppgtt->base);
1567 if (ret)
1568 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001569
Michel Thierryd7b26332015-04-08 12:13:34 +01001570 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001571 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001572 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001573 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001574 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001575 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1576 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001577 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001578
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001579 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1580 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001581 if (ret)
1582 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001583
Michel Thierry69ab76f2015-07-29 17:23:55 +01001584 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1585
Michel Thierry762d9932015-07-30 11:05:29 +01001586 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001587 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001588 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001589 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001590 if (ret)
1591 goto free_scratch;
1592
1593 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001594 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001595 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1596 0, 0,
1597 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001598
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001599 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001600 ret = gen8_preallocate_top_level_pdps(ppgtt);
1601 if (ret)
1602 goto free_scratch;
1603 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001604 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001605
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001606 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001607 gen8_ppgtt_notify_vgt(ppgtt, true);
1608
Michel Thierryd7b26332015-04-08 12:13:34 +01001609 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001610
1611free_scratch:
1612 gen8_free_scratch(&ppgtt->base);
1613 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001614}
1615
Ben Widawsky87d60b62013-12-06 14:11:29 -08001616static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1617{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001618 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001619 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001620 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001621 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001622 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001623 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001624
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001625 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001626 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001627
Dave Gordon731f74c2016-06-24 19:37:46 +01001628 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001629 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001630 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001631 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001632 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001633 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1634
1635 if (pd_entry != expected)
1636 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1637 pde,
1638 pd_entry,
1639 expected);
1640 seq_printf(m, "\tPDE: %x\n", pd_entry);
1641
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001642 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1643
Michel Thierry07749ef2015-03-16 16:00:54 +00001644 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001645 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001646 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001647 (pte * PAGE_SIZE);
1648 int i;
1649 bool found = false;
1650 for (i = 0; i < 4; i++)
1651 if (pt_vaddr[pte + i] != scratch_pte)
1652 found = true;
1653 if (!found)
1654 continue;
1655
1656 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1657 for (i = 0; i < 4; i++) {
1658 if (pt_vaddr[pte + i] != scratch_pte)
1659 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1660 else
1661 seq_puts(m, " SCRATCH ");
1662 }
1663 seq_puts(m, "\n");
1664 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001665 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001666 }
1667}
1668
Ben Widawsky678d96f2015-03-16 16:00:56 +00001669/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001670static void gen6_write_pde(struct i915_page_directory *pd,
1671 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001672{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001673 /* Caller needs to make sure the write completes if necessary */
1674 struct i915_hw_ppgtt *ppgtt =
1675 container_of(pd, struct i915_hw_ppgtt, pd);
1676 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001677
Mika Kuoppala567047b2015-06-25 18:35:12 +03001678 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001679 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001680
Ben Widawsky678d96f2015-03-16 16:00:56 +00001681 writel(pd_entry, ppgtt->pd_addr + pde);
1682}
Ben Widawsky61973492013-04-08 18:43:54 -07001683
Ben Widawsky678d96f2015-03-16 16:00:56 +00001684/* Write all the page tables found in the ppgtt structure to incrementing page
1685 * directories. */
1686static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001687 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001688 uint32_t start, uint32_t length)
1689{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001690 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001691 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001692 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001693
Dave Gordon731f74c2016-06-24 19:37:46 +01001694 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001695 gen6_write_pde(pd, pde, pt);
1696
1697 /* Make sure write is complete before other code can use this page
1698 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001699 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001700}
1701
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001702static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001703{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001704 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001705
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001706 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001707}
Ben Widawsky61973492013-04-08 18:43:54 -07001708
Ben Widawsky90252e52013-12-06 14:11:12 -08001709static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001710 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001711{
Chris Wilson7e37f882016-08-02 22:50:21 +01001712 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001713 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001714 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001715
Ben Widawsky90252e52013-12-06 14:11:12 -08001716 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001717 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001718 if (ret)
1719 return ret;
1720
John Harrison5fb9de12015-05-29 17:44:07 +01001721 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001722 if (ret)
1723 return ret;
1724
Chris Wilsonb5321f32016-08-02 22:50:18 +01001725 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1726 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1727 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1728 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1729 intel_ring_emit(ring, get_pd_offset(ppgtt));
1730 intel_ring_emit(ring, MI_NOOP);
1731 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001732
1733 return 0;
1734}
1735
Ben Widawsky48a10382013-12-06 14:11:11 -08001736static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001737 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001738{
Chris Wilson7e37f882016-08-02 22:50:21 +01001739 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001740 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001741 int ret;
1742
Ben Widawsky48a10382013-12-06 14:11:11 -08001743 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001744 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001745 if (ret)
1746 return ret;
1747
John Harrison5fb9de12015-05-29 17:44:07 +01001748 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001749 if (ret)
1750 return ret;
1751
Chris Wilsonb5321f32016-08-02 22:50:18 +01001752 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1753 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1754 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1755 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1756 intel_ring_emit(ring, get_pd_offset(ppgtt));
1757 intel_ring_emit(ring, MI_NOOP);
1758 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001759
Ben Widawsky90252e52013-12-06 14:11:12 -08001760 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001761 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001762 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001763 if (ret)
1764 return ret;
1765 }
1766
Ben Widawsky48a10382013-12-06 14:11:11 -08001767 return 0;
1768}
1769
Ben Widawskyeeb94882013-12-06 14:11:10 -08001770static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001771 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001772{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001773 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001774 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001775
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001776 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1777 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001778 return 0;
1779}
1780
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001781static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001782{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001783 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301784 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001785
Akash Goel3b3f1652016-10-13 22:44:48 +05301786 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001787 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1788 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001789 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001790 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001791 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001792}
1793
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001794static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001795{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001796 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001797 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301798 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001799
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001800 ecobits = I915_READ(GAC_ECO_BITS);
1801 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1802
1803 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001804 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001805 ecochk |= ECOCHK_PPGTT_WB_HSW;
1806 } else {
1807 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1808 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1809 }
1810 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001811
Akash Goel3b3f1652016-10-13 22:44:48 +05301812 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001813 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001814 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001815 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001816 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001817}
1818
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001819static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001820{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001821 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001822
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001823 ecobits = I915_READ(GAC_ECO_BITS);
1824 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1825 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001826
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001827 gab_ctl = I915_READ(GAB_CTL);
1828 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001829
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001830 ecochk = I915_READ(GAM_ECOCHK);
1831 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001832
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001833 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001834}
1835
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001836/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001837static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001838 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001839 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001840{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001841 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001842 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001843 unsigned first_entry = start >> PAGE_SHIFT;
1844 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001845 unsigned act_pt = first_entry / GEN6_PTES;
1846 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001847 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001848
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001849 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001850 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001851
Daniel Vetter7bddb012012-02-09 17:15:47 +01001852 while (num_entries) {
1853 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001854 if (last_pte > GEN6_PTES)
1855 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001856
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001857 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001858
1859 for (i = first_pte; i < last_pte; i++)
1860 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001861
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001862 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001863
Daniel Vetter7bddb012012-02-09 17:15:47 +01001864 num_entries -= last_pte - first_pte;
1865 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001866 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001867 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001868}
1869
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001870static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001871 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001872 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301873 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001874{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001875 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001876 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001877 unsigned act_pt = first_entry / GEN6_PTES;
1878 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001879 gen6_pte_t *pt_vaddr = NULL;
1880 struct sgt_iter sgt_iter;
1881 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001882
Dave Gordon85d12252016-05-20 11:54:06 +01001883 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001884 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001885 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001886
Chris Wilsoncc797142013-12-31 15:50:30 +00001887 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001888 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301889
Michel Thierry07749ef2015-03-16 16:00:54 +00001890 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001891 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001892 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001893 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001894 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001895 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001896 }
Dave Gordon85d12252016-05-20 11:54:06 +01001897
Chris Wilsoncc797142013-12-31 15:50:30 +00001898 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001899 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001900}
1901
Ben Widawsky678d96f2015-03-16 16:00:56 +00001902static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001903 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001904{
Michel Thierry4933d512015-03-24 15:46:22 +00001905 DECLARE_BITMAP(new_page_tables, I915_PDES);
Chris Wilson49d73912016-11-29 09:50:08 +00001906 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001907 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001908 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001909 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001910 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001911 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001912 int ret;
1913
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001914 start = start_save = start_in;
1915 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001916
1917 bitmap_zero(new_page_tables, I915_PDES);
1918
1919 /* The allocation is done in two stages so that we can bail out with
1920 * minimal amount of pain. The first stage finds new page tables that
1921 * need allocation. The second stage marks use ptes within the page
1922 * tables.
1923 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001924 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001925 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001926 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1927 continue;
1928 }
1929
1930 /* We've already allocated a page table */
1931 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1932
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001933 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001934 if (IS_ERR(pt)) {
1935 ret = PTR_ERR(pt);
1936 goto unwind_out;
1937 }
1938
1939 gen6_initialize_pt(vm, pt);
1940
1941 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001942 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001943 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001944 }
1945
1946 start = start_save;
1947 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001948
Dave Gordon731f74c2016-06-24 19:37:46 +01001949 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001950 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1951
1952 bitmap_zero(tmp_bitmap, GEN6_PTES);
1953 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1954 gen6_pte_count(start, length));
1955
Mika Kuoppala966082c2015-06-25 18:35:19 +03001956 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001957 gen6_write_pde(&ppgtt->pd, pde, pt);
1958
Michel Thierry72744cb2015-03-24 15:46:23 +00001959 trace_i915_page_table_entry_map(vm, pde, pt,
1960 gen6_pte_index(start),
1961 gen6_pte_count(start, length),
1962 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001963 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001964 GEN6_PTES);
1965 }
1966
Michel Thierry4933d512015-03-24 15:46:22 +00001967 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1968
1969 /* Make sure write is complete before other code can use this page
1970 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001971 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001972
Ben Widawsky563222a2015-03-19 12:53:28 +00001973 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001974 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001975
1976unwind_out:
1977 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001978 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00001979
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001980 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001981 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001982 }
1983
1984 mark_tlbs_dirty(ppgtt);
1985 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001986}
1987
Mika Kuoppala8776f022015-06-30 18:16:40 +03001988static int gen6_init_scratch(struct i915_address_space *vm)
1989{
Chris Wilson49d73912016-11-29 09:50:08 +00001990 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001991 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001992
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001993 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001994 if (ret)
1995 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001996
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001997 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001998 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001999 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002000 return PTR_ERR(vm->scratch_pt);
2001 }
2002
2003 gen6_initialize_pt(vm, vm->scratch_pt);
2004
2005 return 0;
2006}
2007
2008static void gen6_free_scratch(struct i915_address_space *vm)
2009{
Chris Wilson49d73912016-11-29 09:50:08 +00002010 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002011
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002012 free_pt(dev_priv, vm->scratch_pt);
2013 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002014}
2015
Daniel Vetter061dd492015-04-14 17:35:13 +02002016static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002017{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002018 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002019 struct i915_page_directory *pd = &ppgtt->pd;
Chris Wilson49d73912016-11-29 09:50:08 +00002020 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry09942c62015-04-08 12:13:30 +01002021 struct i915_page_table *pt;
2022 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002023
Daniel Vetter061dd492015-04-14 17:35:13 +02002024 drm_mm_remove_node(&ppgtt->node);
2025
Dave Gordon731f74c2016-06-24 19:37:46 +01002026 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002027 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002028 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002029
Mika Kuoppala8776f022015-06-30 18:16:40 +03002030 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002031}
2032
Ben Widawskyb1465202014-02-19 22:05:49 -08002033static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002034{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002035 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00002036 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002037 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002038 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002039
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002040 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2041 * allocator works in address space sizes, so it's multiplied by page
2042 * size. We allocate at the top of the GTT to avoid fragmentation.
2043 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002044 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002045
Mika Kuoppala8776f022015-06-30 18:16:40 +03002046 ret = gen6_init_scratch(vm);
2047 if (ret)
2048 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002049
Chris Wilsone007b192017-01-11 11:23:10 +00002050 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2051 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2052 I915_COLOR_UNEVICTABLE,
2053 0, ggtt->base.total,
2054 PIN_HIGH);
Ben Widawskyc8c26622015-01-22 17:01:25 +00002055 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002056 goto err_out;
2057
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002058 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002059 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002060
Ben Widawskyc8c26622015-01-22 17:01:25 +00002061 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002062
2063err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002064 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002065 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002066}
2067
Ben Widawskyb1465202014-02-19 22:05:49 -08002068static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2069{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002070 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002071}
2072
Michel Thierry4933d512015-03-24 15:46:22 +00002073static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2074 uint64_t start, uint64_t length)
2075{
Michel Thierryec565b32015-04-08 12:13:23 +01002076 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002077 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002078
Dave Gordon731f74c2016-06-24 19:37:46 +01002079 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002080 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002081}
2082
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002083static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002084{
Chris Wilson49d73912016-11-29 09:50:08 +00002085 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002086 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002087 int ret;
2088
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002089 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002090 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002091 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002092 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002093 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002094 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002095 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002096 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002097 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002098
2099 ret = gen6_ppgtt_alloc(ppgtt);
2100 if (ret)
2101 return ret;
2102
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002103 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002104 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2105 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002106 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2107 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002108 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002109 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002110 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002111 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002112
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002113 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002114 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002115
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002116 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002117 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002118
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002119 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002120
Ben Widawsky678d96f2015-03-16 16:00:56 +00002121 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2122
Thierry Reding440fd522015-01-23 09:05:06 +01002123 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002124 ppgtt->node.size >> 20,
2125 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002126
Daniel Vetterfa76da32014-08-06 20:19:54 +02002127 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002128 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002129
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002130 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002131}
2132
Chris Wilson2bfa9962016-08-04 07:52:25 +01002133static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2134 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002135{
Chris Wilson49d73912016-11-29 09:50:08 +00002136 ppgtt->base.i915 = dev_priv;
Daniel Vetter3440d262013-01-24 13:49:56 -08002137
Chris Wilson2bfa9962016-08-04 07:52:25 +01002138 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002139 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002140 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002141 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002142}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002143
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002144static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002145 struct drm_i915_private *dev_priv,
2146 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002147{
Chris Wilson80b204b2016-10-28 13:58:58 +01002148 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002149 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002150 INIT_LIST_HEAD(&vm->active_list);
2151 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002152 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002153 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2154}
2155
Matthew Aulded9724d2016-11-17 21:04:10 +00002156static void i915_address_space_fini(struct i915_address_space *vm)
2157{
2158 i915_gem_timeline_fini(&vm->timeline);
2159 drm_mm_takedown(&vm->mm);
2160 list_del(&vm->global_link);
2161}
2162
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002163static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002164{
Tim Gored5165eb2016-02-04 11:49:34 +00002165 /* This function is for gtt related workarounds. This function is
2166 * called on driver load and after a GPU reset, so you can place
2167 * workarounds here even if they get overwritten by GPU reset.
2168 */
2169 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002170 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002171 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002172 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002173 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002174 else if (IS_SKYLAKE(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002175 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002176 else if (IS_BROXTON(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002177 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2178}
2179
Chris Wilson2bfa9962016-08-04 07:52:25 +01002180static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2181 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002182 struct drm_i915_file_private *file_priv,
2183 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002184{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002185 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002186
Chris Wilson2bfa9962016-08-04 07:52:25 +01002187 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002188 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002189 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002190 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002191 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002192 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002193
2194 return ret;
2195}
2196
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002197int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002198{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002199 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002200
Thomas Daniel671b50132014-08-20 16:24:50 +01002201 /* In the case of execlists, PPGTT is enabled by the context descriptor
2202 * and the PDPs are contained within the context itself. We don't
2203 * need to do anything here. */
2204 if (i915.enable_execlists)
2205 return 0;
2206
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002207 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002208 return 0;
2209
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002210 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002211 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002212 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002213 gen7_ppgtt_enable(dev_priv);
2214 else if (INTEL_GEN(dev_priv) >= 8)
2215 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002216 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002217 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002218
John Harrison4ad2fd82015-06-18 13:11:20 +01002219 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002220}
John Harrison4ad2fd82015-06-18 13:11:20 +01002221
Daniel Vetter4d884702014-08-06 15:04:47 +02002222struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002223i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002224 struct drm_i915_file_private *fpriv,
2225 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002226{
2227 struct i915_hw_ppgtt *ppgtt;
2228 int ret;
2229
2230 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2231 if (!ppgtt)
2232 return ERR_PTR(-ENOMEM);
2233
Chris Wilson80b204b2016-10-28 13:58:58 +01002234 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002235 if (ret) {
2236 kfree(ppgtt);
2237 return ERR_PTR(ret);
2238 }
2239
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002240 trace_i915_ppgtt_create(&ppgtt->base);
2241
Daniel Vetter4d884702014-08-06 15:04:47 +02002242 return ppgtt;
2243}
2244
Matthew Aulded9724d2016-11-17 21:04:10 +00002245void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002246{
2247 struct i915_hw_ppgtt *ppgtt =
2248 container_of(kref, struct i915_hw_ppgtt, ref);
2249
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002250 trace_i915_ppgtt_release(&ppgtt->base);
2251
Chris Wilson50e046b2016-08-04 07:52:46 +01002252 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002253 WARN_ON(!list_empty(&ppgtt->base.active_list));
2254 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002255 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002256
Matthew Aulded9724d2016-11-17 21:04:10 +00002257 i915_address_space_fini(&ppgtt->base);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002258
Daniel Vetteree960be2014-08-06 15:04:45 +02002259 ppgtt->base.cleanup(&ppgtt->base);
2260 kfree(ppgtt);
2261}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002262
Ben Widawskya81cc002013-01-18 12:30:31 -08002263/* Certain Gen5 chipsets require require idling the GPU before
2264 * unmapping anything from the GTT when VT-d is enabled.
2265 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002266static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002267{
2268#ifdef CONFIG_INTEL_IOMMU
2269 /* Query intel_iommu to see if we need the workaround. Presumably that
2270 * was loaded first.
2271 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002272 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002273 return true;
2274#endif
2275 return false;
2276}
2277
Chris Wilsondc979972016-05-10 14:10:04 +01002278void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002279{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002280 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302281 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002282
Chris Wilsondc979972016-05-10 14:10:04 +01002283 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002284 return;
2285
Akash Goel3b3f1652016-10-13 22:44:48 +05302286 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002287 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002288 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002289 if (fault_reg & RING_FAULT_VALID) {
2290 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002291 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002292 "\tAddress space: %s\n"
2293 "\tSource ID: %d\n"
2294 "\tType: %d\n",
2295 fault_reg & PAGE_MASK,
2296 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2297 RING_FAULT_SRCID(fault_reg),
2298 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002299 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002300 fault_reg & ~RING_FAULT_VALID);
2301 }
2302 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302303
2304 /* Engine specific init may not have been done till this point. */
2305 if (dev_priv->engine[RCS])
2306 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002307}
2308
Chris Wilson91e56492014-09-25 10:13:12 +01002309static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2310{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002311 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002312 intel_gtt_chipset_flush();
2313 } else {
2314 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2315 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2316 }
2317}
2318
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002319void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002320{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002321 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002322
2323 /* Don't bother messing with faults pre GEN6 as we have little
2324 * documentation supporting that it's a good idea.
2325 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002326 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002327 return;
2328
Chris Wilsondc979972016-05-10 14:10:04 +01002329 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002330
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002331 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002332
2333 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002334}
2335
Chris Wilson03ac84f2016-10-28 13:58:36 +01002336int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2337 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002338{
Chris Wilson1a292fa2017-01-06 15:22:39 +00002339 do {
2340 if (dma_map_sg(&obj->base.dev->pdev->dev,
2341 pages->sgl, pages->nents,
2342 PCI_DMA_BIDIRECTIONAL))
2343 return 0;
2344
2345 /* If the DMA remap fails, one cause can be that we have
2346 * too many objects pinned in a small remapping table,
2347 * such as swiotlb. Incrementally purge all other objects and
2348 * try again - if there are no more pages to remove from
2349 * the DMA remapper, i915_gem_shrink will return 0.
2350 */
2351 GEM_BUG_ON(obj->mm.pages == pages);
2352 } while (i915_gem_shrink(to_i915(obj->base.dev),
2353 obj->base.size >> PAGE_SHIFT,
2354 I915_SHRINK_BOUND |
2355 I915_SHRINK_UNBOUND |
2356 I915_SHRINK_ACTIVE));
Chris Wilson9da3da62012-06-01 15:20:22 +01002357
Chris Wilson03ac84f2016-10-28 13:58:36 +01002358 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002359}
2360
Daniel Vetter2c642b02015-04-14 17:35:26 +02002361static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002362{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002363 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002364}
2365
Chris Wilsond6473f52016-06-10 14:22:59 +05302366static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2367 dma_addr_t addr,
2368 uint64_t offset,
2369 enum i915_cache_level level,
2370 u32 unused)
2371{
Chris Wilson49d73912016-11-29 09:50:08 +00002372 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilsond6473f52016-06-10 14:22:59 +05302373 gen8_pte_t __iomem *pte =
2374 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2375 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302376
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002377 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302378
2379 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2380 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302381}
2382
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002383static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2384 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002385 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302386 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002387{
Chris Wilson49d73912016-11-29 09:50:08 +00002388 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilsonce7fda22016-04-28 09:56:38 +01002389 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002390 struct sgt_iter sgt_iter;
2391 gen8_pte_t __iomem *gtt_entries;
2392 gen8_pte_t gtt_entry;
2393 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002394 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002395
Dave Gordon85d12252016-05-20 11:54:06 +01002396 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2397
2398 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002399 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002400 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002401 }
2402
2403 /*
2404 * XXX: This serves as a posting read to make sure that the PTE has
2405 * actually been updated. There is some concern that even though
2406 * registers and PTEs are within the same BAR that they are potentially
2407 * of NUMA access patterns. Therefore, even with the way we assume
2408 * hardware should work, we must keep this posting read for paranoia.
2409 */
2410 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002411 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002412
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002413 /* This next bit makes the above posting read even more important. We
2414 * want to flush the TLBs only after we're certain all the PTE updates
2415 * have finished.
2416 */
2417 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2418 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002419}
2420
Chris Wilsonc1403302015-11-18 15:19:39 +00002421struct insert_entries {
2422 struct i915_address_space *vm;
2423 struct sg_table *st;
2424 uint64_t start;
2425 enum i915_cache_level level;
2426 u32 flags;
2427};
2428
2429static int gen8_ggtt_insert_entries__cb(void *_arg)
2430{
2431 struct insert_entries *arg = _arg;
2432 gen8_ggtt_insert_entries(arg->vm, arg->st,
2433 arg->start, arg->level, arg->flags);
2434 return 0;
2435}
2436
2437static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2438 struct sg_table *st,
2439 uint64_t start,
2440 enum i915_cache_level level,
2441 u32 flags)
2442{
2443 struct insert_entries arg = { vm, st, start, level, flags };
2444 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2445}
2446
Chris Wilsond6473f52016-06-10 14:22:59 +05302447static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2448 dma_addr_t addr,
2449 uint64_t offset,
2450 enum i915_cache_level level,
2451 u32 flags)
2452{
Chris Wilson49d73912016-11-29 09:50:08 +00002453 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilsond6473f52016-06-10 14:22:59 +05302454 gen6_pte_t __iomem *pte =
2455 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2456 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302457
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002458 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302459
2460 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2461 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302462}
2463
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002464/*
2465 * Binds an object into the global gtt with the specified cache level. The object
2466 * will be accessible to the GPU via commands whose operands reference offsets
2467 * within the global GTT as well as accessible by the GPU through the GMADR
2468 * mapped BAR (dev_priv->mm.gtt->gtt).
2469 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002470static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002471 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002472 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302473 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002474{
Chris Wilson49d73912016-11-29 09:50:08 +00002475 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilsonce7fda22016-04-28 09:56:38 +01002476 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002477 struct sgt_iter sgt_iter;
2478 gen6_pte_t __iomem *gtt_entries;
2479 gen6_pte_t gtt_entry;
2480 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002481 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002482
Dave Gordon85d12252016-05-20 11:54:06 +01002483 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2484
2485 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002486 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002487 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002488 }
2489
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002490 /* XXX: This serves as a posting read to make sure that the PTE has
2491 * actually been updated. There is some concern that even though
2492 * registers and PTEs are within the same BAR that they are potentially
2493 * of NUMA access patterns. Therefore, even with the way we assume
2494 * hardware should work, we must keep this posting read for paranoia.
2495 */
Dave Gordon85d12252016-05-20 11:54:06 +01002496 if (i != 0)
2497 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002498
2499 /* This next bit makes the above posting read even more important. We
2500 * want to flush the TLBs only after we're certain all the PTE updates
2501 * have finished.
2502 */
2503 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2504 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002505}
2506
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002507static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002508 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002509{
2510}
2511
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002512static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002513 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002514{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002515 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002516 unsigned first_entry = start >> PAGE_SHIFT;
2517 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002518 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002519 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2520 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002521 int i;
2522
2523 if (WARN(num_entries > max_entries,
2524 "First entry = %d; Num entries = %d (max=%d)\n",
2525 first_entry, num_entries, max_entries))
2526 num_entries = max_entries;
2527
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002528 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002529 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002530 for (i = 0; i < num_entries; i++)
2531 gen8_set_pte(&gtt_base[i], scratch_pte);
2532 readl(gtt_base);
2533}
2534
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002535static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002536 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002537 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002538{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002539 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002540 unsigned first_entry = start >> PAGE_SHIFT;
2541 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002542 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002543 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2544 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002545 int i;
2546
2547 if (WARN(num_entries > max_entries,
2548 "First entry = %d; Num entries = %d (max=%d)\n",
2549 first_entry, num_entries, max_entries))
2550 num_entries = max_entries;
2551
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002552 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002553 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002554
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002555 for (i = 0; i < num_entries; i++)
2556 iowrite32(scratch_pte, &gtt_base[i]);
2557 readl(gtt_base);
2558}
2559
Chris Wilsond6473f52016-06-10 14:22:59 +05302560static void i915_ggtt_insert_page(struct i915_address_space *vm,
2561 dma_addr_t addr,
2562 uint64_t offset,
2563 enum i915_cache_level cache_level,
2564 u32 unused)
2565{
Chris Wilsond6473f52016-06-10 14:22:59 +05302566 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2567 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302568
2569 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302570}
2571
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002572static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2573 struct sg_table *pages,
2574 uint64_t start,
2575 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002576{
2577 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2578 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2579
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002580 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002581
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002582}
2583
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002584static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002585 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002586 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002587{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002588 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002589}
2590
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002591static int ggtt_bind_vma(struct i915_vma *vma,
2592 enum i915_cache_level cache_level,
2593 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002594{
Chris Wilson49d73912016-11-29 09:50:08 +00002595 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002596 struct drm_i915_gem_object *obj = vma->obj;
2597 u32 pte_flags = 0;
2598 int ret;
2599
2600 ret = i915_get_ggtt_vma_pages(vma);
2601 if (ret)
2602 return ret;
2603
2604 /* Currently applicable only to VLV */
2605 if (obj->gt_ro)
2606 pte_flags |= PTE_READ_ONLY;
2607
Chris Wilson9c870d02016-10-24 13:42:15 +01002608 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002609 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002610 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002611 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002612
2613 /*
2614 * Without aliasing PPGTT there's no difference between
2615 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2616 * upgrade to both bound if we bind either to avoid double-binding.
2617 */
Chris Wilson3272db52016-08-04 16:32:32 +01002618 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002619
2620 return 0;
2621}
2622
2623static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2624 enum i915_cache_level cache_level,
2625 u32 flags)
2626{
Chris Wilson49d73912016-11-29 09:50:08 +00002627 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002628 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002629 int ret;
2630
2631 ret = i915_get_ggtt_vma_pages(vma);
2632 if (ret)
2633 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002634
Akash Goel24f3a8c2014-06-17 10:59:42 +05302635 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002636 pte_flags = 0;
2637 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002638 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302639
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002640
Chris Wilson3272db52016-08-04 16:32:32 +01002641 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002642 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002643 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002644 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002645 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002646 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002647 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002648
Chris Wilson3272db52016-08-04 16:32:32 +01002649 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002650 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002651 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002652 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002653 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002654 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002655
2656 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002657}
2658
2659static void ggtt_unbind_vma(struct i915_vma *vma)
2660{
Chris Wilson49d73912016-11-29 09:50:08 +00002661 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson9c870d02016-10-24 13:42:15 +01002662 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002663 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002664
Chris Wilson9c870d02016-10-24 13:42:15 +01002665 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2666 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002667 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002668 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002669 intel_runtime_pm_put(i915);
2670 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002671
Chris Wilson3272db52016-08-04 16:32:32 +01002672 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002673 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002674 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002675}
2676
Chris Wilson03ac84f2016-10-28 13:58:36 +01002677void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2678 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002679{
David Weinehall52a05c32016-08-22 13:32:44 +03002680 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2681 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002682 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002683
Chris Wilson307dc252016-08-05 10:14:12 +01002684 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002685 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002686 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2687 /* Wait a bit, in hopes it avoids the hang */
2688 udelay(10);
2689 }
2690 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002691
Chris Wilson03ac84f2016-10-28 13:58:36 +01002692 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002693}
Daniel Vetter644ec022012-03-26 09:45:40 +02002694
Chris Wilson45b186f2016-12-16 07:46:42 +00002695static void i915_gtt_color_adjust(const struct drm_mm_node *node,
Chris Wilson42d6ab42012-07-26 11:49:32 +01002696 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002697 u64 *start,
2698 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002699{
2700 if (node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002701 *start += I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002702
Chris Wilsonb44f97f2016-12-16 07:46:40 +00002703 node = list_next_entry(node, node_list);
2704 if (node->allocated && node->color != color)
Chris Wilsonf51455d2017-01-10 14:47:34 +00002705 *end -= I915_GTT_PAGE_SIZE;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002706}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002707
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002708int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002709{
Ben Widawskye78891c2013-01-25 16:41:04 -08002710 /* Let GEM Manage all of the aperture.
2711 *
2712 * However, leave one page at the end still bound to the scratch page.
2713 * There are a number of places where the hardware apparently prefetches
2714 * past the end of the object, and we've seen multiple hangs with the
2715 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2716 * aperture. One page should be enough to keep any prefetching inside
2717 * of the aperture.
2718 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002719 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002720 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002721 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002722 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002723 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002724
Zhi Wangb02d22a2016-06-16 08:06:59 -04002725 ret = intel_vgt_balloon(dev_priv);
2726 if (ret)
2727 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002728
Chris Wilson95374d72016-10-12 10:05:20 +01002729 /* Reserve a mappable slot for our lockless error capture */
2730 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2731 &ggtt->error_capture,
Chris Wilsonf51455d2017-01-10 14:47:34 +00002732 PAGE_SIZE, 0,
Chris Wilson85fd4f52016-12-05 14:29:36 +00002733 I915_COLOR_UNEVICTABLE,
Chris Wilson95374d72016-10-12 10:05:20 +01002734 0, ggtt->mappable_end,
2735 0, 0);
2736 if (ret)
2737 return ret;
2738
Chris Wilsoned2f3452012-11-15 11:32:19 +00002739 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002740 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002741 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2742 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002743 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002744 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002745 }
2746
2747 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002748 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002749 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002750
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002751 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002752 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002753 if (!ppgtt) {
2754 ret = -ENOMEM;
2755 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002756 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002757
Chris Wilson95374d72016-10-12 10:05:20 +01002758 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2759 if (ret)
2760 goto err_ppgtt;
2761
2762 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002763 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2764 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002765 if (ret)
2766 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002767 }
2768
2769 ppgtt->base.clear_range(&ppgtt->base,
2770 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002771 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002772
Daniel Vetterfa76da32014-08-06 20:19:54 +02002773 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002774 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2775 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002776 }
2777
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002778 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002779
2780err_ppgtt_cleanup:
2781 ppgtt->base.cleanup(&ppgtt->base);
2782err_ppgtt:
2783 kfree(ppgtt);
2784err:
2785 drm_mm_remove_node(&ggtt->error_capture);
2786 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002787}
2788
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002789/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002790 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002791 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002792 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002793void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002794{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002795 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002796
Daniel Vetter70e32542014-08-06 15:04:57 +02002797 if (dev_priv->mm.aliasing_ppgtt) {
2798 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002799 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002800 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002801 }
2802
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002803 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002804
Chris Wilson95374d72016-10-12 10:05:20 +01002805 if (drm_mm_node_allocated(&ggtt->error_capture))
2806 drm_mm_remove_node(&ggtt->error_capture);
2807
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002808 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002809 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002810
Matthew Aulded9724d2016-11-17 21:04:10 +00002811 mutex_lock(&dev_priv->drm.struct_mutex);
2812 i915_address_space_fini(&ggtt->base);
2813 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002814 }
2815
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002816 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002817
2818 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002819 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002820}
Daniel Vetter70e32542014-08-06 15:04:57 +02002821
Daniel Vetter2c642b02015-04-14 17:35:26 +02002822static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002823{
2824 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2825 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2826 return snb_gmch_ctl << 20;
2827}
2828
Daniel Vetter2c642b02015-04-14 17:35:26 +02002829static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002830{
2831 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2832 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2833 if (bdw_gmch_ctl)
2834 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002835
2836#ifdef CONFIG_X86_32
2837 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2838 if (bdw_gmch_ctl > 4)
2839 bdw_gmch_ctl = 4;
2840#endif
2841
Ben Widawsky9459d252013-11-03 16:53:55 -08002842 return bdw_gmch_ctl << 20;
2843}
2844
Daniel Vetter2c642b02015-04-14 17:35:26 +02002845static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002846{
2847 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2848 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2849
2850 if (gmch_ctrl)
2851 return 1 << (20 + gmch_ctrl);
2852
2853 return 0;
2854}
2855
Daniel Vetter2c642b02015-04-14 17:35:26 +02002856static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002857{
2858 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2859 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2860 return snb_gmch_ctl << 25; /* 32 MB units */
2861}
2862
Daniel Vetter2c642b02015-04-14 17:35:26 +02002863static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002864{
2865 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2866 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2867 return bdw_gmch_ctl << 25; /* 32 MB units */
2868}
2869
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002870static size_t chv_get_stolen_size(u16 gmch_ctrl)
2871{
2872 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2873 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2874
2875 /*
2876 * 0x0 to 0x10: 32MB increments starting at 0MB
2877 * 0x11 to 0x16: 4MB increments starting at 8MB
2878 * 0x17 to 0x1d: 4MB increments start at 36MB
2879 */
2880 if (gmch_ctrl < 0x11)
2881 return gmch_ctrl << 25;
2882 else if (gmch_ctrl < 0x17)
2883 return (gmch_ctrl - 0x11 + 2) << 22;
2884 else
2885 return (gmch_ctrl - 0x17 + 9) << 22;
2886}
2887
Damien Lespiau66375012014-01-09 18:02:46 +00002888static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2889{
2890 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2891 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2892
2893 if (gen9_gmch_ctl < 0xf0)
2894 return gen9_gmch_ctl << 25; /* 32 MB units */
2895 else
2896 /* 4MB increments starting at 0xf0 for 4MB */
2897 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2898}
2899
Chris Wilson34c998b2016-08-04 07:52:24 +01002900static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002901{
Chris Wilson49d73912016-11-29 09:50:08 +00002902 struct drm_i915_private *dev_priv = ggtt->base.i915;
2903 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002904 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002905 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002906
2907 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002908 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002909
Imre Deak2a073f892015-03-27 13:07:33 +02002910 /*
2911 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2912 * dropped. For WC mappings in general we have 64 byte burst writes
2913 * when the WC buffer is flushed, so we can't use it, but have to
2914 * resort to an uncached mapping. The WC issue is easily caught by the
2915 * readback check when writing GTT PTE entries.
2916 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002917 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002918 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002919 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002920 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002921 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002922 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002923 return -ENOMEM;
2924 }
2925
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002926 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002927 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002928 DRM_ERROR("Scratch setup failed\n");
2929 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002930 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002931 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002932 }
2933
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002934 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002935}
2936
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002937/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2938 * bits. When using advanced contexts each context stores its own PAT, but
2939 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002940static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002941{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002942 uint64_t pat;
2943
2944 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2945 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2946 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2947 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2948 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2949 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2950 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2951 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2952
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002953 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002954 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2955 * so RTL will always use the value corresponding to
2956 * pat_sel = 000".
2957 * So let's disable cache for GGTT to avoid screen corruptions.
2958 * MOCS still can be used though.
2959 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2960 * before this patch, i.e. the same uncached + snooping access
2961 * like on gen6/7 seems to be in effect.
2962 * - So this just fixes blitter/render access. Again it looks
2963 * like it's not just uncached access, but uncached + snooping.
2964 * So we can still hold onto all our assumptions wrt cpu
2965 * clflushing on LLC machines.
2966 */
2967 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2968
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002969 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2970 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002971 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2972 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002973}
2974
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002975static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2976{
2977 uint64_t pat;
2978
2979 /*
2980 * Map WB on BDW to snooped on CHV.
2981 *
2982 * Only the snoop bit has meaning for CHV, the rest is
2983 * ignored.
2984 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002985 * The hardware will never snoop for certain types of accesses:
2986 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2987 * - PPGTT page tables
2988 * - some other special cycles
2989 *
2990 * As with BDW, we also need to consider the following for GT accesses:
2991 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2992 * so RTL will always use the value corresponding to
2993 * pat_sel = 000".
2994 * Which means we must set the snoop bit in PAT entry 0
2995 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002996 */
2997 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2998 GEN8_PPAT(1, 0) |
2999 GEN8_PPAT(2, 0) |
3000 GEN8_PPAT(3, 0) |
3001 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3002 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3003 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3004 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3005
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003006 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3007 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003008}
3009
Chris Wilson34c998b2016-08-04 07:52:24 +01003010static void gen6_gmch_remove(struct i915_address_space *vm)
3011{
3012 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3013
3014 iounmap(ggtt->gsm);
Chris Wilson49d73912016-11-29 09:50:08 +00003015 cleanup_scratch_page(vm->i915, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003016}
3017
Joonas Lahtinend507d732016-03-18 10:42:58 +02003018static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003019{
Chris Wilson49d73912016-11-29 09:50:08 +00003020 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003021 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003022 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003023 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003024
3025 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003026 ggtt->mappable_base = pci_resource_start(pdev, 2);
3027 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003028
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003029 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3030 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003031
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003032 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003033
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003034 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003035 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003036 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003037 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003038 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003039 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003040 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003041 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003042 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003043 }
Ben Widawsky63340132013-11-04 19:32:22 -08003044
Chris Wilson34c998b2016-08-04 07:52:24 +01003045 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003046
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003047 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003048 chv_setup_private_ppat(dev_priv);
3049 else
3050 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003051
Chris Wilson34c998b2016-08-04 07:52:24 +01003052 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003053 ggtt->base.bind_vma = ggtt_bind_vma;
3054 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303055 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003056 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003057 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003058 ggtt->base.clear_range = gen8_ggtt_clear_range;
3059
3060 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3061 if (IS_CHERRYVIEW(dev_priv))
3062 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3063
Chris Wilson34c998b2016-08-04 07:52:24 +01003064 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003065}
3066
Joonas Lahtinend507d732016-03-18 10:42:58 +02003067static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003068{
Chris Wilson49d73912016-11-29 09:50:08 +00003069 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003070 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003071 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003072 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003073
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003074 ggtt->mappable_base = pci_resource_start(pdev, 2);
3075 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003076
Ben Widawskybaa09f52013-01-24 13:49:57 -08003077 /* 64/512MB is the current min/max we actually know of, but this is just
3078 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003079 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003080 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003081 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003082 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003083 }
3084
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003085 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3086 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3087 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003088
Joonas Lahtinend507d732016-03-18 10:42:58 +02003089 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003090
Chris Wilson34c998b2016-08-04 07:52:24 +01003091 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3092 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003093
Joonas Lahtinend507d732016-03-18 10:42:58 +02003094 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303095 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003096 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3097 ggtt->base.bind_vma = ggtt_bind_vma;
3098 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003099 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003100
Chris Wilson34c998b2016-08-04 07:52:24 +01003101 if (HAS_EDRAM(dev_priv))
3102 ggtt->base.pte_encode = iris_pte_encode;
3103 else if (IS_HASWELL(dev_priv))
3104 ggtt->base.pte_encode = hsw_pte_encode;
3105 else if (IS_VALLEYVIEW(dev_priv))
3106 ggtt->base.pte_encode = byt_pte_encode;
3107 else if (INTEL_GEN(dev_priv) >= 7)
3108 ggtt->base.pte_encode = ivb_pte_encode;
3109 else
3110 ggtt->base.pte_encode = snb_pte_encode;
3111
3112 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003113}
3114
Chris Wilson34c998b2016-08-04 07:52:24 +01003115static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003116{
Chris Wilson34c998b2016-08-04 07:52:24 +01003117 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003118}
3119
Joonas Lahtinend507d732016-03-18 10:42:58 +02003120static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003121{
Chris Wilson49d73912016-11-29 09:50:08 +00003122 struct drm_i915_private *dev_priv = ggtt->base.i915;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003123 int ret;
3124
Chris Wilson91c8a322016-07-05 10:40:23 +01003125 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003126 if (!ret) {
3127 DRM_ERROR("failed to set up gmch\n");
3128 return -EIO;
3129 }
3130
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003131 intel_gtt_get(&ggtt->base.total,
3132 &ggtt->stolen_size,
3133 &ggtt->mappable_base,
3134 &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003135
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003136 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303137 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003138 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3139 ggtt->base.clear_range = i915_ggtt_clear_range;
3140 ggtt->base.bind_vma = ggtt_bind_vma;
3141 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003142 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003143
Joonas Lahtinend507d732016-03-18 10:42:58 +02003144 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003145 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3146
Ben Widawskybaa09f52013-01-24 13:49:57 -08003147 return 0;
3148}
3149
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003150/**
Chris Wilson0088e522016-08-04 07:52:21 +01003151 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003152 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003153 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003154int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003155{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003156 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003157 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003158
Chris Wilson49d73912016-11-29 09:50:08 +00003159 ggtt->base.i915 = dev_priv;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003160
Chris Wilson34c998b2016-08-04 07:52:24 +01003161 if (INTEL_GEN(dev_priv) <= 5)
3162 ret = i915_gmch_probe(ggtt);
3163 else if (INTEL_GEN(dev_priv) < 8)
3164 ret = gen6_gmch_probe(ggtt);
3165 else
3166 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003167 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003168 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003169
Chris Wilsondb9309a2017-01-05 15:30:23 +00003170 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3171 * This is easier than doing range restriction on the fly, as we
3172 * currently don't have any bits spare to pass in this upper
3173 * restriction!
3174 */
3175 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3176 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3177 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3178 }
3179
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003180 if ((ggtt->base.total - 1) >> 32) {
3181 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003182 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003183 ggtt->base.total >> 20);
3184 ggtt->base.total = 1ULL << 32;
3185 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3186 }
3187
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003188 if (ggtt->mappable_end > ggtt->base.total) {
3189 DRM_ERROR("mappable aperture extends past end of GGTT,"
3190 " aperture=%llx, total=%llx\n",
3191 ggtt->mappable_end, ggtt->base.total);
3192 ggtt->mappable_end = ggtt->base.total;
3193 }
3194
Ben Widawskybaa09f52013-01-24 13:49:57 -08003195 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003196 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003197 ggtt->base.total >> 20);
3198 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
Chris Wilsonedd1f2f2017-01-06 15:20:11 +00003199 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003200#ifdef CONFIG_INTEL_IOMMU
3201 if (intel_iommu_gfx_mapped)
3202 DRM_INFO("VT-d active for gfx access\n");
3203#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003204
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003205 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003206}
3207
3208/**
3209 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003210 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003211 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003212int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003213{
Chris Wilson0088e522016-08-04 07:52:21 +01003214 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3215 int ret;
3216
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003217 INIT_LIST_HEAD(&dev_priv->vm_list);
3218
3219 /* Subtract the guard page before address space initialization to
3220 * shrink the range used by drm_mm.
3221 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003222 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003223 ggtt->base.total -= PAGE_SIZE;
Chris Wilson80b204b2016-10-28 13:58:58 +01003224 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003225 ggtt->base.total += PAGE_SIZE;
3226 if (!HAS_LLC(dev_priv))
3227 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003228 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003229
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003230 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3231 dev_priv->ggtt.mappable_base,
3232 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003233 ret = -EIO;
3234 goto out_gtt_cleanup;
3235 }
3236
3237 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3238
Chris Wilson0088e522016-08-04 07:52:21 +01003239 /*
3240 * Initialise stolen early so that we may reserve preallocated
3241 * objects for the BIOS to KMS transition.
3242 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003243 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003244 if (ret)
3245 goto out_gtt_cleanup;
3246
3247 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003248
3249out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003250 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003251 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003252}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003253
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003254int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003255{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003256 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003257 return -EIO;
3258
3259 return 0;
3260}
3261
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003262void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003263{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003264 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003265 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003266
Chris Wilsondc979972016-05-10 14:10:04 +01003267 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003268
3269 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003270 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003271
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003272 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3273
3274 /* clflush objects bound into the GGTT and rebind them. */
3275 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003276 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003277 bool ggtt_bound = false;
3278 struct i915_vma *vma;
3279
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003280 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003281 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003282 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003283
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003284 if (!i915_vma_unbind(vma))
3285 continue;
3286
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003287 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3288 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003289 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003290 }
3291
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003292 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003293 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003294 }
3295
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003296 ggtt->base.closed = false;
3297
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003298 if (INTEL_GEN(dev_priv) >= 8) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003299 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003300 chv_setup_private_ppat(dev_priv);
3301 else
3302 bdw_setup_private_ppat(dev_priv);
3303
3304 return;
3305 }
3306
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003307 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003308 struct i915_address_space *vm;
3309
Daniel Vetterfa423312015-04-14 17:35:23 +02003310 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3311 /* TODO: Perhaps it shouldn't be gen6 specific */
3312
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003313 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003314
Chris Wilson2bfa9962016-08-04 07:52:25 +01003315 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003316 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003317 else
3318 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003319
3320 gen6_write_page_range(dev_priv, &ppgtt->pd,
3321 0, ppgtt->base.total);
3322 }
3323 }
3324
3325 i915_ggtt_flush(dev_priv);
3326}
3327
Chris Wilson058d88c2016-08-15 10:49:06 +01003328struct i915_vma *
3329i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3330 struct i915_address_space *vm,
3331 const struct i915_ggtt_view *view)
3332{
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003333 struct rb_node *rb;
Chris Wilson058d88c2016-08-15 10:49:06 +01003334
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003335 rb = obj->vma_tree.rb_node;
3336 while (rb) {
3337 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
3338 long cmp;
3339
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003340 cmp = i915_vma_compare(vma, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003341 if (cmp == 0)
Chris Wilson058d88c2016-08-15 10:49:06 +01003342 return vma;
3343
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003344 if (cmp < 0)
3345 rb = rb->rb_right;
3346 else
3347 rb = rb->rb_left;
3348 }
3349
Chris Wilson058d88c2016-08-15 10:49:06 +01003350 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003351}
3352
3353struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003354i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003355 struct i915_address_space *vm,
3356 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003357{
3358 struct i915_vma *vma;
3359
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003360 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +01003361 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3362
3363 vma = i915_gem_obj_to_vma(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003364 if (!vma) {
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003365 vma = i915_vma_create(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003366 GEM_BUG_ON(vma != i915_gem_obj_to_vma(obj, vm, view));
3367 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003368
Chris Wilson3272db52016-08-04 16:32:32 +01003369 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003370 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003371}
3372
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003373static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003374rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003375 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003376 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003377 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003378{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003379 unsigned int column, row;
3380 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003381
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003382 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003383 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003384 for (row = 0; row < height; row++) {
3385 st->nents++;
3386 /* We don't need the pages, but need to initialize
3387 * the entries so the sg list can be happily traversed.
3388 * The only thing we need are DMA addresses.
3389 */
3390 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003391 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003392 sg_dma_len(sg) = PAGE_SIZE;
3393 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003394 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003395 }
3396 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003397
3398 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003399}
3400
3401static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003402intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003403 struct drm_i915_gem_object *obj)
3404{
Dave Gordon85d12252016-05-20 11:54:06 +01003405 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003406 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003407 struct sgt_iter sgt_iter;
3408 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003409 unsigned long i;
3410 dma_addr_t *page_addr_list;
3411 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003412 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003413 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003414
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003415 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003416 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003417 sizeof(dma_addr_t),
3418 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003419 if (!page_addr_list)
3420 return ERR_PTR(ret);
3421
3422 /* Allocate target SG list. */
3423 st = kmalloc(sizeof(*st), GFP_KERNEL);
3424 if (!st)
3425 goto err_st_alloc;
3426
Ville Syrjälä6687c902015-09-15 13:16:41 +03003427 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003428 if (ret)
3429 goto err_sg_alloc;
3430
3431 /* Populate source page list from the object. */
3432 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003433 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003434 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003435
Dave Gordon85d12252016-05-20 11:54:06 +01003436 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003437 st->nents = 0;
3438 sg = st->sgl;
3439
Ville Syrjälä6687c902015-09-15 13:16:41 +03003440 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3441 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3442 rot_info->plane[i].width, rot_info->plane[i].height,
3443 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003444 }
3445
Ville Syrjälä6687c902015-09-15 13:16:41 +03003446 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3447 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003448
3449 drm_free_large(page_addr_list);
3450
3451 return st;
3452
3453err_sg_alloc:
3454 kfree(st);
3455err_st_alloc:
3456 drm_free_large(page_addr_list);
3457
Ville Syrjälä6687c902015-09-15 13:16:41 +03003458 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3459 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3460
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003461 return ERR_PTR(ret);
3462}
3463
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003464static struct sg_table *
3465intel_partial_pages(const struct i915_ggtt_view *view,
3466 struct drm_i915_gem_object *obj)
3467{
3468 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003469 struct scatterlist *sg, *iter;
3470 unsigned int count = view->params.partial.size;
3471 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003472 int ret = -ENOMEM;
3473
3474 st = kmalloc(sizeof(*st), GFP_KERNEL);
3475 if (!st)
3476 goto err_st_alloc;
3477
Chris Wilsond2a84a72016-10-28 13:58:34 +01003478 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003479 if (ret)
3480 goto err_sg_alloc;
3481
Chris Wilsond2a84a72016-10-28 13:58:34 +01003482 iter = i915_gem_object_get_sg(obj,
3483 view->params.partial.offset,
3484 &offset);
3485 GEM_BUG_ON(!iter);
3486
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003487 sg = st->sgl;
3488 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003489 do {
3490 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003491
Chris Wilsond2a84a72016-10-28 13:58:34 +01003492 len = min(iter->length - (offset << PAGE_SHIFT),
3493 count << PAGE_SHIFT);
3494 sg_set_page(sg, NULL, len, 0);
3495 sg_dma_address(sg) =
3496 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3497 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003498
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003499 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003500 count -= len >> PAGE_SHIFT;
3501 if (count == 0) {
3502 sg_mark_end(sg);
3503 return st;
3504 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003505
Chris Wilsond2a84a72016-10-28 13:58:34 +01003506 sg = __sg_next(sg);
3507 iter = __sg_next(iter);
3508 offset = 0;
3509 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003510
3511err_sg_alloc:
3512 kfree(st);
3513err_st_alloc:
3514 return ERR_PTR(ret);
3515}
3516
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003517static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003518i915_get_ggtt_vma_pages(struct i915_vma *vma)
3519{
3520 int ret = 0;
3521
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003522 /* The vma->pages are only valid within the lifespan of the borrowed
3523 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3524 * must be the vma->pages. A simple rule is that vma->pages must only
3525 * be accessed when the obj->mm.pages are pinned.
3526 */
3527 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3528
Chris Wilson247177d2016-08-15 10:48:47 +01003529 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003530 return 0;
3531
3532 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003533 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003534 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003535 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003536 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003537 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003538 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003539 else
3540 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3541 vma->ggtt_view.type);
3542
Chris Wilson247177d2016-08-15 10:48:47 +01003543 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003544 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003545 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003546 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003547 } else if (IS_ERR(vma->pages)) {
3548 ret = PTR_ERR(vma->pages);
3549 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003550 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3551 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003552 }
3553
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003554 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003555}
3556
Chris Wilsone007b192017-01-11 11:23:10 +00003557/**
Chris Wilson625d9882017-01-11 11:23:11 +00003558 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
3559 * @vm - the &struct i915_address_space
3560 * @node - the &struct drm_mm_node (typically i915_vma.mode)
3561 * @size - how much space to allocate inside the GTT,
3562 * must be #I915_GTT_PAGE_SIZE aligned
3563 * @offset - where to insert inside the GTT,
3564 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3565 * (@offset + @size) must fit within the address space
3566 * @color - color to apply to node, if this node is not from a VMA,
3567 * color must be #I915_COLOR_UNEVICTABLE
3568 * @flags - control search and eviction behaviour
3569 *
3570 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3571 * the address space (using @size and @color). If the @node does not fit, it
3572 * tries to evict any overlapping nodes from the GTT, including any
3573 * neighbouring nodes if the colors do not match (to ensure guard pages between
3574 * differing domains). See i915_gem_evict_for_node() for the gory details
3575 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3576 * evicting active overlapping objects, and any overlapping node that is pinned
3577 * or marked as unevictable will also result in failure.
3578 *
3579 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3580 * asked to wait for eviction and interrupted.
3581 */
3582int i915_gem_gtt_reserve(struct i915_address_space *vm,
3583 struct drm_mm_node *node,
3584 u64 size, u64 offset, unsigned long color,
3585 unsigned int flags)
3586{
3587 int err;
3588
3589 GEM_BUG_ON(!size);
3590 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3591 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3592 GEM_BUG_ON(range_overflows(offset, size, vm->total));
3593
3594 node->size = size;
3595 node->start = offset;
3596 node->color = color;
3597
3598 err = drm_mm_reserve_node(&vm->mm, node);
3599 if (err != -ENOSPC)
3600 return err;
3601
3602 err = i915_gem_evict_for_node(vm, node, flags);
3603 if (err == 0)
3604 err = drm_mm_reserve_node(&vm->mm, node);
3605
3606 return err;
3607}
3608
3609/**
Chris Wilsone007b192017-01-11 11:23:10 +00003610 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
3611 * @vm - the &struct i915_address_space
3612 * @node - the &struct drm_mm_node (typically i915_vma.node)
3613 * @size - how much space to allocate inside the GTT,
3614 * must be #I915_GTT_PAGE_SIZE aligned
3615 * @alignment - required alignment of starting offset, may be 0 but
3616 * if specified, this must be a power-of-two and at least
3617 * #I915_GTT_MIN_ALIGNMENT
3618 * @color - color to apply to node
3619 * @start - start of any range restriction inside GTT (0 for all),
3620 * must be #I915_GTT_PAGE_SIZE aligned
3621 * @end - end of any range restriction inside GTT (U64_MAX for all),
3622 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3623 * @flags - control search and eviction behaviour
3624 *
3625 * i915_gem_gtt_insert() first searches for an available hole into which
3626 * is can insert the node. The hole address is aligned to @alignment and
3627 * its @size must then fit entirely within the [@start, @end] bounds. The
3628 * nodes on either side of the hole must match @color, or else a guard page
3629 * will be inserted between the two nodes (or the node evicted). If no
3630 * suitable hole is found, then the LRU list of objects within the GTT
3631 * is scanned to find the first set of replacement nodes to create the hole.
3632 * Those old overlapping nodes are evicted from the GTT (and so must be
3633 * rebound before any future use). Any node that is currently pinned cannot
3634 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3635 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3636 * searching for an eviction candidate. See i915_gem_evict_something() for
3637 * the gory details on the eviction algorithm.
3638 *
3639 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3640 * asked to wait for eviction and interrupted.
3641 */
3642int i915_gem_gtt_insert(struct i915_address_space *vm,
3643 struct drm_mm_node *node,
3644 u64 size, u64 alignment, unsigned long color,
3645 u64 start, u64 end, unsigned int flags)
3646{
3647 u32 search_flag, alloc_flag;
3648 int err;
3649
3650 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3651 GEM_BUG_ON(!size);
3652 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3653 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3654 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3655 GEM_BUG_ON(start >= end);
3656 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3657 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
3658
3659 if (unlikely(range_overflows(start, size, end)))
3660 return -ENOSPC;
3661
3662 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3663 return -ENOSPC;
3664
3665 if (flags & PIN_HIGH) {
3666 search_flag = DRM_MM_SEARCH_BELOW;
3667 alloc_flag = DRM_MM_CREATE_TOP;
3668 } else {
3669 search_flag = DRM_MM_SEARCH_DEFAULT;
3670 alloc_flag = DRM_MM_CREATE_DEFAULT;
3671 }
3672
3673 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3674 * so we know that we always have a minimum alignment of 4096.
3675 * The drm_mm range manager is optimised to return results
3676 * with zero alignment, so where possible use the optimal
3677 * path.
3678 */
3679 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3680 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3681 alignment = 0;
3682
3683 err = drm_mm_insert_node_in_range_generic(&vm->mm, node,
3684 size, alignment, color,
3685 start, end,
3686 search_flag, alloc_flag);
3687 if (err != -ENOSPC)
3688 return err;
3689
3690 err = i915_gem_evict_something(vm, size, alignment, color,
3691 start, end, flags);
3692 if (err)
3693 return err;
3694
3695 search_flag = DRM_MM_SEARCH_DEFAULT;
3696 return drm_mm_insert_node_in_range_generic(&vm->mm, node,
3697 size, alignment, color,
3698 start, end,
3699 search_flag, alloc_flag);
3700}