blob: 75a0f2d4e62f2236ee33424aa23898736879f36f [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
Daniel Vetter0e46ce22014-01-08 16:10:27 +010026#include <linux/seq_file.h>
Chris Wilson5bab6f62015-10-23 18:43:32 +010027#include <linux/stop_machine.h>
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
29#include <drm/i915_drm.h>
Daniel Vetter76aaf222010-11-05 22:23:30 +010030#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080031#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010032#include "i915_trace.h"
33#include "intel_drv.h"
Chris Wilsond07f0e52016-10-28 13:58:44 +010034#include "intel_frontbuffer.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010035
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +010036#define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
37
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000038/**
39 * DOC: Global GTT views
40 *
41 * Background and previous state
42 *
43 * Historically objects could exists (be bound) in global GTT space only as
44 * singular instances with a view representing all of the object's backing pages
45 * in a linear fashion. This view will be called a normal view.
46 *
47 * To support multiple views of the same object, where the number of mapped
48 * pages is not equal to the backing store, or where the layout of the pages
49 * is not linear, concept of a GGTT view was added.
50 *
51 * One example of an alternative view is a stereo display driven by a single
52 * image. In this case we would have a framebuffer looking like this
53 * (2x2 pages):
54 *
55 * 12
56 * 34
57 *
58 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
59 * rendering. In contrast, fed to the display engine would be an alternative
60 * view which could look something like this:
61 *
62 * 1212
63 * 3434
64 *
65 * In this example both the size and layout of pages in the alternative view is
66 * different from the normal view.
67 *
68 * Implementation and usage
69 *
70 * GGTT views are implemented using VMAs and are distinguished via enum
71 * i915_ggtt_view_type and struct i915_ggtt_view.
72 *
73 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020074 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
75 * renaming in large amounts of code. They take the struct i915_ggtt_view
76 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000077 *
78 * As a helper for callers which are only interested in the normal view,
79 * globally const i915_ggtt_view_normal singleton instance exists. All old core
80 * GEM API functions, the ones not taking the view parameter, are operating on,
81 * or with the normal GGTT view.
82 *
83 * Code wanting to add or use a new GGTT view needs to:
84 *
85 * 1. Add a new enum with a suitable name.
86 * 2. Extend the metadata in the i915_ggtt_view structure if required.
87 * 3. Add support to i915_get_vma_pages().
88 *
89 * New views are required to build a scatter-gather table from within the
90 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
91 * exists for the lifetime of an VMA.
92 *
93 * Core API is designed to have copy semantics which means that passed in
94 * struct i915_ggtt_view does not need to be persistent (left around after
95 * calling the core API functions).
96 *
97 */
98
Daniel Vetter70b9f6f2015-04-14 17:35:27 +020099static int
100i915_get_ggtt_vma_pages(struct i915_vma *vma);
101
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200102const struct i915_ggtt_view i915_ggtt_view_normal = {
103 .type = I915_GGTT_VIEW_NORMAL,
104};
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200105const struct i915_ggtt_view i915_ggtt_view_rotated = {
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200106 .type = I915_GGTT_VIEW_ROTATED,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200107};
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000108
Chris Wilsonc0336662016-05-06 15:40:21 +0100109int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
110 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200111{
Chris Wilson1893a712014-09-19 11:56:27 +0100112 bool has_aliasing_ppgtt;
113 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100114 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100115
Chris Wilsonc0336662016-05-06 15:40:21 +0100116 has_aliasing_ppgtt = INTEL_GEN(dev_priv) >= 6;
117 has_full_ppgtt = INTEL_GEN(dev_priv) >= 7;
118 has_full_48bit_ppgtt =
119 IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9;
Chris Wilson1893a712014-09-19 11:56:27 +0100120
Zhi Wange320d402016-09-06 12:04:12 +0800121 if (intel_vgpu_active(dev_priv)) {
122 /* emulation is too hard */
123 has_full_ppgtt = false;
124 has_full_48bit_ppgtt = false;
125 }
Yu Zhang71ba2d62015-02-10 19:05:54 +0800126
Chris Wilson0e4ca102016-04-29 13:18:22 +0100127 if (!has_aliasing_ppgtt)
128 return 0;
129
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000130 /*
131 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
132 * execlists, the sole mechanism available to submit work.
133 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100134 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200135 return 0;
136
137 if (enable_ppgtt == 1)
138 return 1;
139
Chris Wilson1893a712014-09-19 11:56:27 +0100140 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200141 return 2;
142
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100143 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
144 return 3;
145
Daniel Vetter93a25a92014-03-06 09:40:43 +0100146#ifdef CONFIG_INTEL_IOMMU
147 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100148 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100149 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200150 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100151 }
152#endif
153
Jesse Barnes62942ed2014-06-13 09:28:33 -0700154 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100155 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700156 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
157 return 0;
158 }
159
Zhi Wange320d402016-09-06 12:04:12 +0800160 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100161 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000162 else
163 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100164}
165
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200166static int ppgtt_bind_vma(struct i915_vma *vma,
167 enum i915_cache_level cache_level,
168 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200169{
170 u32 pte_flags = 0;
171
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100172 vma->pages = vma->obj->mm.pages;
Chris Wilson247177d2016-08-15 10:48:47 +0100173
Daniel Vetter47552652015-04-14 17:35:24 +0200174 /* Currently applicable only to VLV */
175 if (vma->obj->gt_ro)
176 pte_flags |= PTE_READ_ONLY;
177
Chris Wilson247177d2016-08-15 10:48:47 +0100178 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200179 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200180
181 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200182}
183
184static void ppgtt_unbind_vma(struct i915_vma *vma)
185{
186 vma->vm->clear_range(vma->vm,
187 vma->node.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200188 vma->size);
Daniel Vetter47552652015-04-14 17:35:24 +0200189}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800190
Daniel Vetter2c642b02015-04-14 17:35:26 +0200191static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200192 enum i915_cache_level level)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700193{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200194 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700195 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300196
197 switch (level) {
198 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800199 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300200 break;
201 case I915_CACHE_WT:
202 pte |= PPAT_DISPLAY_ELLC_INDEX;
203 break;
204 default:
205 pte |= PPAT_CACHED_INDEX;
206 break;
207 }
208
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700209 return pte;
210}
211
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300212static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
213 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800214{
Michel Thierry07749ef2015-03-16 16:00:54 +0000215 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800216 pde |= addr;
217 if (level != I915_CACHE_NONE)
218 pde |= PPAT_CACHED_PDE_INDEX;
219 else
220 pde |= PPAT_UNCACHED_INDEX;
221 return pde;
222}
223
Michel Thierry762d9932015-07-30 11:05:29 +0100224#define gen8_pdpe_encode gen8_pde_encode
225#define gen8_pml4e_encode gen8_pde_encode
226
Michel Thierry07749ef2015-03-16 16:00:54 +0000227static gen6_pte_t snb_pte_encode(dma_addr_t addr,
228 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200229 u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700230{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200231 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky54d12522012-09-24 16:44:32 -0700232 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700233
234 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100235 case I915_CACHE_L3_LLC:
236 case I915_CACHE_LLC:
237 pte |= GEN6_PTE_CACHE_LLC;
238 break;
239 case I915_CACHE_NONE:
240 pte |= GEN6_PTE_UNCACHED;
241 break;
242 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100243 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100244 }
245
246 return pte;
247}
248
Michel Thierry07749ef2015-03-16 16:00:54 +0000249static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
250 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200251 u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100252{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200253 gen6_pte_t pte = GEN6_PTE_VALID;
Chris Wilson350ec882013-08-06 13:17:02 +0100254 pte |= GEN6_PTE_ADDR_ENCODE(addr);
255
256 switch (level) {
257 case I915_CACHE_L3_LLC:
258 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700259 break;
260 case I915_CACHE_LLC:
261 pte |= GEN6_PTE_CACHE_LLC;
262 break;
263 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700264 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700265 break;
266 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100267 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700268 }
269
Ben Widawsky54d12522012-09-24 16:44:32 -0700270 return pte;
271}
272
Michel Thierry07749ef2015-03-16 16:00:54 +0000273static gen6_pte_t byt_pte_encode(dma_addr_t addr,
274 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200275 u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700276{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200277 gen6_pte_t pte = GEN6_PTE_VALID;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700278 pte |= GEN6_PTE_ADDR_ENCODE(addr);
279
Akash Goel24f3a8c2014-06-17 10:59:42 +0530280 if (!(flags & PTE_READ_ONLY))
281 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700282
283 if (level != I915_CACHE_NONE)
284 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
285
286 return pte;
287}
288
Michel Thierry07749ef2015-03-16 16:00:54 +0000289static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
290 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200291 u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700292{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200293 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700294 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700295
296 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700297 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700298
299 return pte;
300}
301
Michel Thierry07749ef2015-03-16 16:00:54 +0000302static gen6_pte_t iris_pte_encode(dma_addr_t addr,
303 enum i915_cache_level level,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200304 u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700305{
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200306 gen6_pte_t pte = GEN6_PTE_VALID;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700307 pte |= HSW_PTE_ADDR_ENCODE(addr);
308
Chris Wilson651d7942013-08-08 14:41:10 +0100309 switch (level) {
310 case I915_CACHE_NONE:
311 break;
312 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000313 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100314 break;
315 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000316 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100317 break;
318 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700319
320 return pte;
321}
322
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000323static int __setup_page_dma(struct drm_i915_private *dev_priv,
Mika Kuoppalac114f762015-06-25 18:35:13 +0300324 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000325{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000326 struct device *kdev = &dev_priv->drm.pdev->dev;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000327
Mika Kuoppalac114f762015-06-25 18:35:13 +0300328 p->page = alloc_page(flags);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300329 if (!p->page)
Michel Thierry1266cdb2015-03-24 17:06:33 +0000330 return -ENOMEM;
331
David Weinehallc49d13e2016-08-22 13:32:42 +0300332 p->daddr = dma_map_page(kdev,
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300333 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
334
David Weinehallc49d13e2016-08-22 13:32:42 +0300335 if (dma_mapping_error(kdev, p->daddr)) {
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300336 __free_page(p->page);
337 return -EINVAL;
338 }
339
Michel Thierry1266cdb2015-03-24 17:06:33 +0000340 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000341}
342
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000343static int setup_page_dma(struct drm_i915_private *dev_priv,
344 struct i915_page_dma *p)
Mika Kuoppalac114f762015-06-25 18:35:13 +0300345{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000346 return __setup_page_dma(dev_priv, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300347}
348
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000349static void cleanup_page_dma(struct drm_i915_private *dev_priv,
350 struct i915_page_dma *p)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300351{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000352 struct pci_dev *pdev = dev_priv->drm.pdev;
David Weinehall52a05c32016-08-22 13:32:44 +0300353
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300354 if (WARN_ON(!p->page))
355 return;
356
David Weinehall52a05c32016-08-22 13:32:44 +0300357 dma_unmap_page(&pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300358 __free_page(p->page);
359 memset(p, 0, sizeof(*p));
360}
361
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300362static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300363{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300364 return kmap_atomic(p->page);
365}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300366
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300367/* We use the flushing unmap only with ppgtt structures:
368 * page directories, page tables and scratch pages.
369 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100370static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300371{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300372 /* There are only few exceptions for gen >=6. chv and bxt.
373 * And we are not sure about the latter so play safe for now.
374 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100375 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300376 drm_clflush_virt_range(vaddr, PAGE_SIZE);
377
378 kunmap_atomic(vaddr);
379}
380
Mika Kuoppala567047b2015-06-25 18:35:12 +0300381#define kmap_px(px) kmap_page_dma(px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100382#define kunmap_px(ppgtt, vaddr) \
383 kunmap_page_dma(to_i915((ppgtt)->base.dev), (vaddr))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300384
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000385#define setup_px(dev_priv, px) setup_page_dma((dev_priv), px_base(px))
386#define cleanup_px(dev_priv, px) cleanup_page_dma((dev_priv), px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100387#define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
388#define fill32_px(dev_priv, px, v) \
389 fill_page_dma_32((dev_priv), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300390
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100391static void fill_page_dma(struct drm_i915_private *dev_priv,
392 struct i915_page_dma *p, const uint64_t val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300393{
394 int i;
395 uint64_t * const vaddr = kmap_page_dma(p);
396
397 for (i = 0; i < 512; i++)
398 vaddr[i] = val;
399
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100400 kunmap_page_dma(dev_priv, vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300401}
402
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100403static void fill_page_dma_32(struct drm_i915_private *dev_priv,
404 struct i915_page_dma *p, const uint32_t val32)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300405{
406 uint64_t v = val32;
407
408 v = v << 32 | val32;
409
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100410 fill_page_dma(dev_priv, p, v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300411}
412
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100413static int
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000414setup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100415 struct i915_page_dma *scratch,
416 gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300417{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000418 return __setup_page_dma(dev_priv, scratch, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300419}
420
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000421static void cleanup_scratch_page(struct drm_i915_private *dev_priv,
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100422 struct i915_page_dma *scratch)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300423{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000424 cleanup_page_dma(dev_priv, scratch);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300425}
426
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000427static struct i915_page_table *alloc_pt(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000428{
Michel Thierryec565b32015-04-08 12:13:23 +0100429 struct i915_page_table *pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000430 const size_t count = INTEL_GEN(dev_priv) >= 8 ? GEN8_PTES : GEN6_PTES;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000431 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000432
433 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
434 if (!pt)
435 return ERR_PTR(-ENOMEM);
436
Ben Widawsky678d96f2015-03-16 16:00:56 +0000437 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
438 GFP_KERNEL);
439
440 if (!pt->used_ptes)
441 goto fail_bitmap;
442
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000443 ret = setup_px(dev_priv, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000444 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300445 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000446
447 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000448
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300449fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000450 kfree(pt->used_ptes);
451fail_bitmap:
452 kfree(pt);
453
454 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000455}
456
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000457static void free_pt(struct drm_i915_private *dev_priv,
458 struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000459{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000460 cleanup_px(dev_priv, pt);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300461 kfree(pt->used_ptes);
462 kfree(pt);
463}
464
465static void gen8_initialize_pt(struct i915_address_space *vm,
466 struct i915_page_table *pt)
467{
468 gen8_pte_t scratch_pte;
469
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100470 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200471 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300472
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100473 fill_px(to_i915(vm->dev), pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300474}
475
476static void gen6_initialize_pt(struct i915_address_space *vm,
477 struct i915_page_table *pt)
478{
479 gen6_pte_t scratch_pte;
480
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100481 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300482
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100483 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200484 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300485
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100486 fill32_px(to_i915(vm->dev), pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000487}
488
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000489static struct i915_page_directory *alloc_pd(struct drm_i915_private *dev_priv)
Ben Widawsky06fda602015-02-24 16:22:36 +0000490{
Michel Thierryec565b32015-04-08 12:13:23 +0100491 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100492 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000493
494 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
495 if (!pd)
496 return ERR_PTR(-ENOMEM);
497
Michel Thierry33c88192015-04-08 12:13:33 +0100498 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
499 sizeof(*pd->used_pdes), GFP_KERNEL);
500 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300501 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100502
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000503 ret = setup_px(dev_priv, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100504 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300505 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100506
Ben Widawsky06fda602015-02-24 16:22:36 +0000507 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100508
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300509fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100510 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300511fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100512 kfree(pd);
513
514 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000515}
516
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000517static void free_pd(struct drm_i915_private *dev_priv,
518 struct i915_page_directory *pd)
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300519{
520 if (px_page(pd)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000521 cleanup_px(dev_priv, pd);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300522 kfree(pd->used_pdes);
523 kfree(pd);
524 }
525}
526
527static void gen8_initialize_pd(struct i915_address_space *vm,
528 struct i915_page_directory *pd)
529{
530 gen8_pde_t scratch_pde;
531
532 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
533
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100534 fill_px(to_i915(vm->dev), pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300535}
536
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000537static int __pdp_init(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100538 struct i915_page_directory_pointer *pdp)
539{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000540 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Michel Thierry6ac18502015-07-29 17:23:46 +0100541
542 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
543 sizeof(unsigned long),
544 GFP_KERNEL);
545 if (!pdp->used_pdpes)
546 return -ENOMEM;
547
548 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
549 GFP_KERNEL);
550 if (!pdp->page_directory) {
551 kfree(pdp->used_pdpes);
552 /* the PDP might be the statically allocated top level. Keep it
553 * as clean as possible */
554 pdp->used_pdpes = NULL;
555 return -ENOMEM;
556 }
557
558 return 0;
559}
560
561static void __pdp_fini(struct i915_page_directory_pointer *pdp)
562{
563 kfree(pdp->used_pdpes);
564 kfree(pdp->page_directory);
565 pdp->page_directory = NULL;
566}
567
Michel Thierry762d9932015-07-30 11:05:29 +0100568static struct
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000569i915_page_directory_pointer *alloc_pdp(struct drm_i915_private *dev_priv)
Michel Thierry762d9932015-07-30 11:05:29 +0100570{
571 struct i915_page_directory_pointer *pdp;
572 int ret = -ENOMEM;
573
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000574 WARN_ON(!USES_FULL_48BIT_PPGTT(dev_priv));
Michel Thierry762d9932015-07-30 11:05:29 +0100575
576 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
577 if (!pdp)
578 return ERR_PTR(-ENOMEM);
579
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000580 ret = __pdp_init(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100581 if (ret)
582 goto fail_bitmap;
583
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000584 ret = setup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100585 if (ret)
586 goto fail_page_m;
587
588 return pdp;
589
590fail_page_m:
591 __pdp_fini(pdp);
592fail_bitmap:
593 kfree(pdp);
594
595 return ERR_PTR(ret);
596}
597
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000598static void free_pdp(struct drm_i915_private *dev_priv,
Michel Thierry6ac18502015-07-29 17:23:46 +0100599 struct i915_page_directory_pointer *pdp)
600{
601 __pdp_fini(pdp);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000602 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
603 cleanup_px(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100604 kfree(pdp);
605 }
606}
607
Michel Thierry69ab76f2015-07-29 17:23:55 +0100608static void gen8_initialize_pdp(struct i915_address_space *vm,
609 struct i915_page_directory_pointer *pdp)
610{
611 gen8_ppgtt_pdpe_t scratch_pdpe;
612
613 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
614
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100615 fill_px(to_i915(vm->dev), pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100616}
617
618static void gen8_initialize_pml4(struct i915_address_space *vm,
619 struct i915_pml4 *pml4)
620{
621 gen8_ppgtt_pml4e_t scratch_pml4e;
622
623 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
624 I915_CACHE_LLC);
625
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100626 fill_px(to_i915(vm->dev), pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100627}
628
Michel Thierry762d9932015-07-30 11:05:29 +0100629static void
630gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
631 struct i915_page_directory_pointer *pdp,
632 struct i915_page_directory *pd,
633 int index)
634{
635 gen8_ppgtt_pdpe_t *page_directorypo;
636
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000637 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
Michel Thierry762d9932015-07-30 11:05:29 +0100638 return;
639
640 page_directorypo = kmap_px(pdp);
641 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
642 kunmap_px(ppgtt, page_directorypo);
643}
644
645static void
646gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
647 struct i915_pml4 *pml4,
648 struct i915_page_directory_pointer *pdp,
649 int index)
650{
651 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
652
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000653 WARN_ON(!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)));
Michel Thierry762d9932015-07-30 11:05:29 +0100654 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
655 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100656}
657
Ben Widawsky94e409c2013-11-04 22:29:36 -0800658/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100659static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100660 unsigned entry,
661 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800662{
Chris Wilson7e37f882016-08-02 22:50:21 +0100663 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000664 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800665 int ret;
666
667 BUG_ON(entry >= 4);
668
John Harrison5fb9de12015-05-29 17:44:07 +0100669 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800670 if (ret)
671 return ret;
672
Chris Wilsonb5321f32016-08-02 22:50:18 +0100673 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
674 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
675 intel_ring_emit(ring, upper_32_bits(addr));
676 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
677 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
678 intel_ring_emit(ring, lower_32_bits(addr));
679 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800680
681 return 0;
682}
683
Michel Thierry2dba3232015-07-30 11:06:23 +0100684static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
685 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800686{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800687 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800688
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100689 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300690 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
691
John Harrisone85b26d2015-05-29 17:43:56 +0100692 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800693 if (ret)
694 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800695 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800696
Ben Widawskyeeb94882013-12-06 14:11:10 -0800697 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800698}
699
Michel Thierry2dba3232015-07-30 11:06:23 +0100700static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
701 struct drm_i915_gem_request *req)
702{
703 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
704}
705
Mika Kuoppalafce93752016-10-31 17:24:46 +0200706/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
707 * the page table structures, we mark them dirty so that
708 * context switching/execlist queuing code takes extra steps
709 * to ensure that tlbs are flushed.
710 */
711static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
712{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000713 ppgtt->pd_dirty_rings = INTEL_INFO(to_i915(ppgtt->base.dev))->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200714}
715
Michał Winiarski2ce51792016-10-13 14:02:42 +0200716/* Removes entries from a single page table, releasing it if it's empty.
717 * Caller can use the return value to update higher-level entries.
718 */
719static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200720 struct i915_page_table *pt,
721 uint64_t start,
722 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700723{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300724 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200725 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200726 unsigned int pte = gen8_pte_index(start);
727 unsigned int pte_end = pte + num_entries;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200728 gen8_pte_t *pt_vaddr;
729 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
730 I915_CACHE_LLC);
731
732 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200733 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700734
Mika Kuoppala37c63932016-11-01 15:27:36 +0200735 GEM_BUG_ON(pte_end > GEN8_PTES);
736
737 bitmap_clear(pt->used_ptes, pte, num_entries);
Ben Widawsky06fda602015-02-24 16:22:36 +0000738
Michał Winiarski2ce51792016-10-13 14:02:42 +0200739 if (bitmap_empty(pt->used_ptes, GEN8_PTES)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000740 free_pt(to_i915(vm->dev), pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200741 return true;
742 }
743
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);
778 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200779 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200780
781 if (bitmap_empty(pd->used_pdes, I915_PDES)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000782 free_pd(to_i915(vm->dev), pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200783 return true;
784 }
785
786 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200787}
Ben Widawsky06fda602015-02-24 16:22:36 +0000788
Michał Winiarski2ce51792016-10-13 14:02:42 +0200789/* Removes entries from a single page dir pointer, releasing it if it's empty.
790 * Caller can use the return value to update higher-level entries
791 */
792static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200793 struct i915_page_directory_pointer *pdp,
794 uint64_t start,
795 uint64_t length)
796{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200797 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000798 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200799 struct i915_page_directory *pd;
800 uint64_t pdpe;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200801 gen8_ppgtt_pdpe_t *pdpe_vaddr;
802 gen8_ppgtt_pdpe_t scratch_pdpe =
803 gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200804
805 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
806 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100807 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000808
Michał Winiarski2ce51792016-10-13 14:02:42 +0200809 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
810 __clear_bit(pdpe, pdp->used_pdpes);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000811 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Michał Winiarski2ce51792016-10-13 14:02:42 +0200812 pdpe_vaddr = kmap_px(pdp);
813 pdpe_vaddr[pdpe] = scratch_pdpe;
814 kunmap_px(ppgtt, pdpe_vaddr);
815 }
816 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200817 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200818
Mika Kuoppalafce93752016-10-31 17:24:46 +0200819 mark_tlbs_dirty(ppgtt);
820
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000821 if (USES_FULL_48BIT_PPGTT(dev_priv) &&
822 bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv))) {
823 free_pdp(dev_priv, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200824 return true;
825 }
826
827 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200828}
Ben Widawsky459108b2013-11-02 21:07:23 -0700829
Michał Winiarski2ce51792016-10-13 14:02:42 +0200830/* Removes entries from a single pml4.
831 * This is the top-level structure in 4-level page tables used on gen8+.
832 * Empty entries are always scratch pml4e.
833 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200834static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
835 struct i915_pml4 *pml4,
836 uint64_t start,
837 uint64_t length)
838{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200839 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200840 struct i915_page_directory_pointer *pdp;
841 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200842 gen8_ppgtt_pml4e_t *pml4e_vaddr;
843 gen8_ppgtt_pml4e_t scratch_pml4e =
844 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC);
845
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000846 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(to_i915(vm->dev)));
Ben Widawsky459108b2013-11-02 21:07:23 -0700847
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200848 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
849 if (WARN_ON(!pml4->pdps[pml4e]))
850 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700851
Michał Winiarski2ce51792016-10-13 14:02:42 +0200852 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
853 __clear_bit(pml4e, pml4->used_pml4es);
854 pml4e_vaddr = kmap_px(pml4);
855 pml4e_vaddr[pml4e] = scratch_pml4e;
856 kunmap_px(ppgtt, pml4e_vaddr);
857 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700858 }
859}
860
Michel Thierryf9b5b782015-07-30 11:02:49 +0100861static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200862 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700863{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300864 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100865
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000866 if (USES_FULL_48BIT_PPGTT(to_i915(vm->dev)))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200867 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
868 else
869 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100870}
871
872static void
873gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
874 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100875 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100876 uint64_t start,
877 enum i915_cache_level cache_level)
878{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300879 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000880 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100881 unsigned pdpe = gen8_pdpe_index(start);
882 unsigned pde = gen8_pde_index(start);
883 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700884
Chris Wilson6f1cc992013-12-31 15:50:31 +0000885 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700886
Michel Thierry3387d432015-08-03 09:52:47 +0100887 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000888 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100889 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100890 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300891 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000892 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800893
894 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100895 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200896 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000897 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300898 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000899 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000900 if (++pde == I915_PDES) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000901 if (++pdpe == I915_PDPES_PER_PDP(to_i915(vm->dev)))
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100902 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800903 pde = 0;
904 }
905 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700906 }
907 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300908
909 if (pt_vaddr)
910 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700911}
912
Michel Thierryf9b5b782015-07-30 11:02:49 +0100913static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
914 struct sg_table *pages,
915 uint64_t start,
916 enum i915_cache_level cache_level,
917 u32 unused)
918{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300919 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100920 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100921
Michel Thierry3387d432015-08-03 09:52:47 +0100922 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100923
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000924 if (!USES_FULL_48BIT_PPGTT(to_i915(vm->dev))) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100925 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
926 cache_level);
927 } else {
928 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000929 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100930 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
931
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000932 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100933 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
934 start, cache_level);
935 }
936 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100937}
938
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000939static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
Michel Thierryf37c0502015-06-10 17:46:39 +0100940 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800941{
942 int i;
943
Mika Kuoppala567047b2015-06-25 18:35:12 +0300944 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800945 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800946
Michel Thierry33c88192015-04-08 12:13:33 +0100947 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000948 if (WARN_ON(!pd->page_table[i]))
949 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800950
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000951 free_pt(dev_priv, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000952 pd->page_table[i] = NULL;
953 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000954}
955
Mika Kuoppala8776f022015-06-30 18:16:40 +0300956static int gen8_init_scratch(struct i915_address_space *vm)
957{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000958 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Matthew Auld64c050d2016-04-27 13:19:25 +0100959 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300960
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000961 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100962 if (ret)
963 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300964
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000965 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300966 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100967 ret = PTR_ERR(vm->scratch_pt);
968 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300969 }
970
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000971 vm->scratch_pd = alloc_pd(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300972 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100973 ret = PTR_ERR(vm->scratch_pd);
974 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300975 }
976
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000977 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
978 vm->scratch_pdp = alloc_pdp(dev_priv);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100979 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100980 ret = PTR_ERR(vm->scratch_pdp);
981 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100982 }
983 }
984
Mika Kuoppala8776f022015-06-30 18:16:40 +0300985 gen8_initialize_pt(vm, vm->scratch_pt);
986 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000987 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100988 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300989
990 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100991
992free_pd:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000993 free_pd(dev_priv, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +0100994free_pt:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000995 free_pt(dev_priv, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +0100996free_scratch_page:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000997 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +0100998
999 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001000}
1001
Zhiyuan Lv650da342015-08-28 15:41:18 +08001002static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1003{
1004 enum vgt_g2v_type msg;
Matthew Aulddf285642016-04-22 12:09:25 +01001005 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Zhiyuan Lv650da342015-08-28 15:41:18 +08001006 int i;
1007
Matthew Aulddf285642016-04-22 12:09:25 +01001008 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001009 u64 daddr = px_dma(&ppgtt->pml4);
1010
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001011 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1012 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001013
1014 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1015 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1016 } else {
1017 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1018 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1019
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001020 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1021 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001022 }
1023
1024 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1025 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1026 }
1027
1028 I915_WRITE(vgtif_reg(g2v_notify), msg);
1029
1030 return 0;
1031}
1032
Mika Kuoppala8776f022015-06-30 18:16:40 +03001033static void gen8_free_scratch(struct i915_address_space *vm)
1034{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001035 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001036
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001037 if (USES_FULL_48BIT_PPGTT(dev_priv))
1038 free_pdp(dev_priv, vm->scratch_pdp);
1039 free_pd(dev_priv, vm->scratch_pd);
1040 free_pt(dev_priv, vm->scratch_pt);
1041 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001042}
1043
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001044static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
Michel Thierry762d9932015-07-30 11:05:29 +01001045 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001046{
1047 int i;
1048
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001049 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001050 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001051 continue;
1052
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001053 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1054 free_pd(dev_priv, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001055 }
Michel Thierry69876be2015-04-08 12:13:27 +01001056
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001057 free_pdp(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001058}
1059
1060static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1061{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001062 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Michel Thierry762d9932015-07-30 11:05:29 +01001063 int i;
1064
1065 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1066 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1067 continue;
1068
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001069 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001070 }
1071
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001072 cleanup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001073}
1074
1075static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1076{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001077 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001078 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001079
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001080 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001081 gen8_ppgtt_notify_vgt(ppgtt, false);
1082
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001083 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1084 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001085 else
1086 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001087
Mika Kuoppala8776f022015-06-30 18:16:40 +03001088 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001089}
1090
Michel Thierryd7b26332015-04-08 12:13:34 +01001091/**
1092 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001093 * @vm: Master vm structure.
1094 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001095 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001096 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001097 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1098 * caller to free on error.
1099 *
1100 * Allocate the required number of page tables. Extremely similar to
1101 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1102 * the page directory boundary (instead of the page directory pointer). That
1103 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1104 * possible, and likely that the caller will need to use multiple calls of this
1105 * function to achieve the appropriate allocation.
1106 *
1107 * Return: 0 if success; negative error code otherwise.
1108 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001109static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001110 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001111 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001112 uint64_t length,
1113 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001114{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001115 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001116 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001117 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001118
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001119 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001120 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001121 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001122 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001123 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001124 continue;
1125 }
1126
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001127 pt = alloc_pt(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001128 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001129 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001130
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001131 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001132 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001133 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001134 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001135 }
1136
1137 return 0;
1138
1139unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001140 for_each_set_bit(pde, new_pts, I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001141 free_pt(dev_priv, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001142
1143 return -ENOMEM;
1144}
1145
Michel Thierryd7b26332015-04-08 12:13:34 +01001146/**
1147 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001148 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001149 * @pdp: Page directory pointer for this address range.
1150 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001151 * @length: Size of the allocations.
1152 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001153 * caller to free on error.
1154 *
1155 * Allocate the required number of page directories starting at the pde index of
1156 * @start, and ending at the pde index @start + @length. This function will skip
1157 * over already allocated page directories within the range, and only allocate
1158 * new ones, setting the appropriate pointer within the pdp as well as the
1159 * correct position in the bitmap @new_pds.
1160 *
1161 * The function will only allocate the pages within the range for a give page
1162 * directory pointer. In other words, if @start + @length straddles a virtually
1163 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1164 * required by the caller, This is not currently possible, and the BUG in the
1165 * code will prevent it.
1166 *
1167 * Return: 0 if success; negative error code otherwise.
1168 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001169static int
1170gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1171 struct i915_page_directory_pointer *pdp,
1172 uint64_t start,
1173 uint64_t length,
1174 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001175{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001176 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001177 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001178 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001179 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001180
Michel Thierry6ac18502015-07-29 17:23:46 +01001181 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001182
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001183 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001184 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001185 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001186
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001187 pd = alloc_pd(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001188 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001189 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001190
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001191 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001192 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001193 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001194 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001195 }
1196
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001197 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001198
1199unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001200 for_each_set_bit(pdpe, new_pds, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001201 free_pd(dev_priv, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001202
1203 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001204}
1205
Michel Thierry762d9932015-07-30 11:05:29 +01001206/**
1207 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1208 * @vm: Master vm structure.
1209 * @pml4: Page map level 4 for this address range.
1210 * @start: Starting virtual address to begin allocations.
1211 * @length: Size of the allocations.
1212 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1213 * caller to free on error.
1214 *
1215 * Allocate the required number of page directory pointers. Extremely similar to
1216 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1217 * The main difference is here we are limited by the pml4 boundary (instead of
1218 * the page directory pointer).
1219 *
1220 * Return: 0 if success; negative error code otherwise.
1221 */
1222static int
1223gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1224 struct i915_pml4 *pml4,
1225 uint64_t start,
1226 uint64_t length,
1227 unsigned long *new_pdps)
1228{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001229 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierry762d9932015-07-30 11:05:29 +01001230 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001231 uint32_t pml4e;
1232
1233 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1234
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001235 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001236 if (!test_bit(pml4e, pml4->used_pml4es)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001237 pdp = alloc_pdp(dev_priv);
Michel Thierry762d9932015-07-30 11:05:29 +01001238 if (IS_ERR(pdp))
1239 goto unwind_out;
1240
Michel Thierry69ab76f2015-07-29 17:23:55 +01001241 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001242 pml4->pdps[pml4e] = pdp;
1243 __set_bit(pml4e, new_pdps);
1244 trace_i915_page_directory_pointer_entry_alloc(vm,
1245 pml4e,
1246 start,
1247 GEN8_PML4E_SHIFT);
1248 }
1249 }
1250
1251 return 0;
1252
1253unwind_out:
1254 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001255 free_pdp(dev_priv, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001256
1257 return -ENOMEM;
1258}
1259
Michel Thierryd7b26332015-04-08 12:13:34 +01001260static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001261free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001262{
Michel Thierryd7b26332015-04-08 12:13:34 +01001263 kfree(new_pts);
1264 kfree(new_pds);
1265}
1266
1267/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1268 * of these are based on the number of PDPEs in the system.
1269 */
1270static
1271int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001272 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001273 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001274{
Michel Thierryd7b26332015-04-08 12:13:34 +01001275 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001276 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001277
Michał Winiarski3a41a052015-09-03 19:22:18 +02001278 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001279 if (!pds)
1280 return -ENOMEM;
1281
Michał Winiarski3a41a052015-09-03 19:22:18 +02001282 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1283 GFP_TEMPORARY);
1284 if (!pts)
1285 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001286
1287 *new_pds = pds;
1288 *new_pts = pts;
1289
1290 return 0;
1291
1292err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001293 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001294 return -ENOMEM;
1295}
1296
Michel Thierry762d9932015-07-30 11:05:29 +01001297static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1298 struct i915_page_directory_pointer *pdp,
1299 uint64_t start,
1300 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001301{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001302 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001303 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001304 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierry5441f0c2015-04-08 12:13:28 +01001305 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001306 const uint64_t orig_start = start;
1307 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001308 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001309 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001310 int ret;
1311
Michel Thierryd7b26332015-04-08 12:13:34 +01001312 /* Wrap is never okay since we can only represent 48b, and we don't
1313 * actually use the other side of the canonical address space.
1314 */
1315 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001316 return -ENODEV;
1317
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001318 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001319 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001320
Michel Thierry6ac18502015-07-29 17:23:46 +01001321 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001322 if (ret)
1323 return ret;
1324
Michel Thierryd7b26332015-04-08 12:13:34 +01001325 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001326 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1327 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001328 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001329 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001330 return ret;
1331 }
1332
1333 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001334 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001335 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001336 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001337 if (ret)
1338 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001339 }
1340
Michel Thierry33c88192015-04-08 12:13:33 +01001341 start = orig_start;
1342 length = orig_length;
1343
Michel Thierryd7b26332015-04-08 12:13:34 +01001344 /* Allocations have completed successfully, so set the bitmaps, and do
1345 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001346 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001347 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001348 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001349 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001350 uint64_t pd_start = start;
1351 uint32_t pde;
1352
Michel Thierryd7b26332015-04-08 12:13:34 +01001353 /* Every pd should be allocated, we just did that above. */
1354 WARN_ON(!pd);
1355
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001356 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001357 /* Same reasoning as pd */
1358 WARN_ON(!pt);
1359 WARN_ON(!pd_len);
1360 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1361
1362 /* Set our used ptes within the page table */
1363 bitmap_set(pt->used_ptes,
1364 gen8_pte_index(pd_start),
1365 gen8_pte_count(pd_start, pd_len));
1366
1367 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001368 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001369
1370 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001371 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1372 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001373 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1374 gen8_pte_index(start),
1375 gen8_pte_count(start, length),
1376 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001377
1378 /* NB: We haven't yet mapped ptes to pages. At this
1379 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001380 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001381
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001382 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001383 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001384 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001385 }
1386
Michał Winiarski3a41a052015-09-03 19:22:18 +02001387 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001388 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001389 return 0;
1390
1391err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001392 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001393 unsigned long temp;
1394
Michał Winiarski3a41a052015-09-03 19:22:18 +02001395 for_each_set_bit(temp, new_page_tables + pdpe *
1396 BITS_TO_LONGS(I915_PDES), I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001397 free_pt(dev_priv,
1398 pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001399 }
1400
Michel Thierry6ac18502015-07-29 17:23:46 +01001401 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001402 free_pd(dev_priv, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001403
Michał Winiarski3a41a052015-09-03 19:22:18 +02001404 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001405 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001406 return ret;
1407}
1408
Michel Thierry762d9932015-07-30 11:05:29 +01001409static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1410 struct i915_pml4 *pml4,
1411 uint64_t start,
1412 uint64_t length)
1413{
1414 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001415 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001416 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001417 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001418 int ret = 0;
1419
1420 /* Do the pml4 allocations first, so we don't need to track the newly
1421 * allocated tables below the pdp */
1422 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1423
1424 /* The pagedirectory and pagetable allocations are done in the shared 3
1425 * and 4 level code. Just allocate the pdps.
1426 */
1427 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1428 new_pdps);
1429 if (ret)
1430 return ret;
1431
1432 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1433 "The allocation has spanned more than 512GB. "
1434 "It is highly likely this is incorrect.");
1435
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001436 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001437 WARN_ON(!pdp);
1438
1439 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1440 if (ret)
1441 goto err_out;
1442
1443 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1444 }
1445
1446 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1447 GEN8_PML4ES_PER_PML4);
1448
1449 return 0;
1450
1451err_out:
1452 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001453 gen8_ppgtt_cleanup_3lvl(to_i915(vm->dev), pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001454
1455 return ret;
1456}
1457
1458static int gen8_alloc_va_range(struct i915_address_space *vm,
1459 uint64_t start, uint64_t length)
1460{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001461 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001462
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001463 if (USES_FULL_48BIT_PPGTT(to_i915(vm->dev)))
Michel Thierry762d9932015-07-30 11:05:29 +01001464 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1465 else
1466 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1467}
1468
Michel Thierryea91e402015-07-29 17:23:57 +01001469static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1470 uint64_t start, uint64_t length,
1471 gen8_pte_t scratch_pte,
1472 struct seq_file *m)
1473{
1474 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001475 uint32_t pdpe;
1476
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001477 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001478 struct i915_page_table *pt;
1479 uint64_t pd_len = length;
1480 uint64_t pd_start = start;
1481 uint32_t pde;
1482
1483 if (!test_bit(pdpe, pdp->used_pdpes))
1484 continue;
1485
1486 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001487 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001488 uint32_t pte;
1489 gen8_pte_t *pt_vaddr;
1490
1491 if (!test_bit(pde, pd->used_pdes))
1492 continue;
1493
1494 pt_vaddr = kmap_px(pt);
1495 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1496 uint64_t va =
1497 (pdpe << GEN8_PDPE_SHIFT) |
1498 (pde << GEN8_PDE_SHIFT) |
1499 (pte << GEN8_PTE_SHIFT);
1500 int i;
1501 bool found = false;
1502
1503 for (i = 0; i < 4; i++)
1504 if (pt_vaddr[pte + i] != scratch_pte)
1505 found = true;
1506 if (!found)
1507 continue;
1508
1509 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1510 for (i = 0; i < 4; i++) {
1511 if (pt_vaddr[pte + i] != scratch_pte)
1512 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1513 else
1514 seq_puts(m, " SCRATCH ");
1515 }
1516 seq_puts(m, "\n");
1517 }
1518 /* don't use kunmap_px, it could trigger
1519 * an unnecessary flush.
1520 */
1521 kunmap_atomic(pt_vaddr);
1522 }
1523 }
1524}
1525
1526static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1527{
1528 struct i915_address_space *vm = &ppgtt->base;
1529 uint64_t start = ppgtt->base.start;
1530 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001531 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001532 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001533
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001534 if (!USES_FULL_48BIT_PPGTT(to_i915(vm->dev))) {
Michel Thierryea91e402015-07-29 17:23:57 +01001535 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1536 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001537 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001538 struct i915_pml4 *pml4 = &ppgtt->pml4;
1539 struct i915_page_directory_pointer *pdp;
1540
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001541 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001542 if (!test_bit(pml4e, pml4->used_pml4es))
1543 continue;
1544
1545 seq_printf(m, " PML4E #%llu\n", pml4e);
1546 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1547 }
1548 }
1549}
1550
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001551static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1552{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001553 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001554 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001555 int ret;
1556
1557 /* We allocate temp bitmap for page tables for no gain
1558 * but as this is for init only, lets keep the things simple
1559 */
1560 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1561 if (ret)
1562 return ret;
1563
1564 /* Allocate for all pdps regardless of how the ppgtt
1565 * was defined.
1566 */
1567 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1568 0, 1ULL << 32,
1569 new_page_dirs);
1570 if (!ret)
1571 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1572
Michał Winiarski3a41a052015-09-03 19:22:18 +02001573 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001574
1575 return ret;
1576}
1577
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001578/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001579 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1580 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1581 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1582 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001583 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001584 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001585static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001586{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001587 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001588 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001589
Mika Kuoppala8776f022015-06-30 18:16:40 +03001590 ret = gen8_init_scratch(&ppgtt->base);
1591 if (ret)
1592 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001593
Michel Thierryd7b26332015-04-08 12:13:34 +01001594 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001595 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001596 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001597 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001598 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001599 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1600 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001601 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001602
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001603 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1604 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001605 if (ret)
1606 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001607
Michel Thierry69ab76f2015-07-29 17:23:55 +01001608 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1609
Michel Thierry762d9932015-07-30 11:05:29 +01001610 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001611 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001612 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001613 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001614 if (ret)
1615 goto free_scratch;
1616
1617 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001618 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001619 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1620 0, 0,
1621 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001622
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001623 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001624 ret = gen8_preallocate_top_level_pdps(ppgtt);
1625 if (ret)
1626 goto free_scratch;
1627 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001628 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001629
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001630 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001631 gen8_ppgtt_notify_vgt(ppgtt, true);
1632
Michel Thierryd7b26332015-04-08 12:13:34 +01001633 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001634
1635free_scratch:
1636 gen8_free_scratch(&ppgtt->base);
1637 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001638}
1639
Ben Widawsky87d60b62013-12-06 14:11:29 -08001640static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1641{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001642 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001643 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001644 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001645 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001646 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001647 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001648
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001649 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001650 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001651
Dave Gordon731f74c2016-06-24 19:37:46 +01001652 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001653 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001654 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001655 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001656 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001657 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1658
1659 if (pd_entry != expected)
1660 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1661 pde,
1662 pd_entry,
1663 expected);
1664 seq_printf(m, "\tPDE: %x\n", pd_entry);
1665
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001666 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1667
Michel Thierry07749ef2015-03-16 16:00:54 +00001668 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001669 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001670 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001671 (pte * PAGE_SIZE);
1672 int i;
1673 bool found = false;
1674 for (i = 0; i < 4; i++)
1675 if (pt_vaddr[pte + i] != scratch_pte)
1676 found = true;
1677 if (!found)
1678 continue;
1679
1680 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1681 for (i = 0; i < 4; i++) {
1682 if (pt_vaddr[pte + i] != scratch_pte)
1683 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1684 else
1685 seq_puts(m, " SCRATCH ");
1686 }
1687 seq_puts(m, "\n");
1688 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001689 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001690 }
1691}
1692
Ben Widawsky678d96f2015-03-16 16:00:56 +00001693/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001694static void gen6_write_pde(struct i915_page_directory *pd,
1695 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001696{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001697 /* Caller needs to make sure the write completes if necessary */
1698 struct i915_hw_ppgtt *ppgtt =
1699 container_of(pd, struct i915_hw_ppgtt, pd);
1700 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001701
Mika Kuoppala567047b2015-06-25 18:35:12 +03001702 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001703 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001704
Ben Widawsky678d96f2015-03-16 16:00:56 +00001705 writel(pd_entry, ppgtt->pd_addr + pde);
1706}
Ben Widawsky61973492013-04-08 18:43:54 -07001707
Ben Widawsky678d96f2015-03-16 16:00:56 +00001708/* Write all the page tables found in the ppgtt structure to incrementing page
1709 * directories. */
1710static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001711 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001712 uint32_t start, uint32_t length)
1713{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001714 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001715 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001716 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001717
Dave Gordon731f74c2016-06-24 19:37:46 +01001718 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001719 gen6_write_pde(pd, pde, pt);
1720
1721 /* Make sure write is complete before other code can use this page
1722 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001723 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001724}
1725
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001726static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001727{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001728 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001729
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001730 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001731}
Ben Widawsky61973492013-04-08 18:43:54 -07001732
Ben Widawsky90252e52013-12-06 14:11:12 -08001733static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001734 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001735{
Chris Wilson7e37f882016-08-02 22:50:21 +01001736 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001737 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001738 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001739
Ben Widawsky90252e52013-12-06 14:11:12 -08001740 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001741 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001742 if (ret)
1743 return ret;
1744
John Harrison5fb9de12015-05-29 17:44:07 +01001745 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001746 if (ret)
1747 return ret;
1748
Chris Wilsonb5321f32016-08-02 22:50:18 +01001749 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1750 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1751 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1752 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1753 intel_ring_emit(ring, get_pd_offset(ppgtt));
1754 intel_ring_emit(ring, MI_NOOP);
1755 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001756
1757 return 0;
1758}
1759
Ben Widawsky48a10382013-12-06 14:11:11 -08001760static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001761 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001762{
Chris Wilson7e37f882016-08-02 22:50:21 +01001763 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001764 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001765 int ret;
1766
Ben Widawsky48a10382013-12-06 14:11:11 -08001767 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001768 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001769 if (ret)
1770 return ret;
1771
John Harrison5fb9de12015-05-29 17:44:07 +01001772 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001773 if (ret)
1774 return ret;
1775
Chris Wilsonb5321f32016-08-02 22:50:18 +01001776 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1777 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1778 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1779 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1780 intel_ring_emit(ring, get_pd_offset(ppgtt));
1781 intel_ring_emit(ring, MI_NOOP);
1782 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001783
Ben Widawsky90252e52013-12-06 14:11:12 -08001784 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001785 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001786 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001787 if (ret)
1788 return ret;
1789 }
1790
Ben Widawsky48a10382013-12-06 14:11:11 -08001791 return 0;
1792}
1793
Ben Widawskyeeb94882013-12-06 14:11:10 -08001794static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001795 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001796{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001797 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001798 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001799
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001800 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1801 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001802 return 0;
1803}
1804
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001805static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001806{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001807 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301808 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001809
Akash Goel3b3f1652016-10-13 22:44:48 +05301810 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001811 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1812 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001813 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001814 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001815 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001816}
1817
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001818static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001819{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001820 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001821 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301822 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001823
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001824 ecobits = I915_READ(GAC_ECO_BITS);
1825 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1826
1827 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001828 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001829 ecochk |= ECOCHK_PPGTT_WB_HSW;
1830 } else {
1831 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1832 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1833 }
1834 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001835
Akash Goel3b3f1652016-10-13 22:44:48 +05301836 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001837 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001838 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001839 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001840 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001841}
1842
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001843static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001844{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001845 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001846
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001847 ecobits = I915_READ(GAC_ECO_BITS);
1848 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1849 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001850
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001851 gab_ctl = I915_READ(GAB_CTL);
1852 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001853
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001854 ecochk = I915_READ(GAM_ECOCHK);
1855 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001856
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001857 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001858}
1859
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001860/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001861static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001862 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001863 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001864{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001865 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001866 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001867 unsigned first_entry = start >> PAGE_SHIFT;
1868 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001869 unsigned act_pt = first_entry / GEN6_PTES;
1870 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001871 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001872
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001873 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001874 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001875
Daniel Vetter7bddb012012-02-09 17:15:47 +01001876 while (num_entries) {
1877 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001878 if (last_pte > GEN6_PTES)
1879 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001880
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001881 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001882
1883 for (i = first_pte; i < last_pte; i++)
1884 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001885
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001886 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001887
Daniel Vetter7bddb012012-02-09 17:15:47 +01001888 num_entries -= last_pte - first_pte;
1889 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001890 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001891 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001892}
1893
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001894static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001895 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001896 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301897 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001898{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001899 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001900 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001901 unsigned act_pt = first_entry / GEN6_PTES;
1902 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001903 gen6_pte_t *pt_vaddr = NULL;
1904 struct sgt_iter sgt_iter;
1905 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001906
Dave Gordon85d12252016-05-20 11:54:06 +01001907 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001908 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001909 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001910
Chris Wilsoncc797142013-12-31 15:50:30 +00001911 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001912 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301913
Michel Thierry07749ef2015-03-16 16:00:54 +00001914 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001915 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001916 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001917 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001918 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001919 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001920 }
Dave Gordon85d12252016-05-20 11:54:06 +01001921
Chris Wilsoncc797142013-12-31 15:50:30 +00001922 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001923 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001924}
1925
Ben Widawsky678d96f2015-03-16 16:00:56 +00001926static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001927 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001928{
Michel Thierry4933d512015-03-24 15:46:22 +00001929 DECLARE_BITMAP(new_page_tables, I915_PDES);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001930 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001931 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001932 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001933 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001934 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001935 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001936 int ret;
1937
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001938 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1939 return -ENODEV;
1940
1941 start = start_save = start_in;
1942 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001943
1944 bitmap_zero(new_page_tables, I915_PDES);
1945
1946 /* The allocation is done in two stages so that we can bail out with
1947 * minimal amount of pain. The first stage finds new page tables that
1948 * need allocation. The second stage marks use ptes within the page
1949 * tables.
1950 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001951 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001952 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001953 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1954 continue;
1955 }
1956
1957 /* We've already allocated a page table */
1958 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1959
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001960 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001961 if (IS_ERR(pt)) {
1962 ret = PTR_ERR(pt);
1963 goto unwind_out;
1964 }
1965
1966 gen6_initialize_pt(vm, pt);
1967
1968 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001969 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001970 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001971 }
1972
1973 start = start_save;
1974 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001975
Dave Gordon731f74c2016-06-24 19:37:46 +01001976 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001977 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1978
1979 bitmap_zero(tmp_bitmap, GEN6_PTES);
1980 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1981 gen6_pte_count(start, length));
1982
Mika Kuoppala966082c2015-06-25 18:35:19 +03001983 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001984 gen6_write_pde(&ppgtt->pd, pde, pt);
1985
Michel Thierry72744cb2015-03-24 15:46:23 +00001986 trace_i915_page_table_entry_map(vm, pde, pt,
1987 gen6_pte_index(start),
1988 gen6_pte_count(start, length),
1989 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001990 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001991 GEN6_PTES);
1992 }
1993
Michel Thierry4933d512015-03-24 15:46:22 +00001994 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1995
1996 /* Make sure write is complete before other code can use this page
1997 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001998 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001999
Ben Widawsky563222a2015-03-19 12:53:28 +00002000 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002001 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00002002
2003unwind_out:
2004 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002005 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002006
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002007 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002008 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002009 }
2010
2011 mark_tlbs_dirty(ppgtt);
2012 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002013}
2014
Mika Kuoppala8776f022015-06-30 18:16:40 +03002015static int gen6_init_scratch(struct i915_address_space *vm)
2016{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002017 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002018 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002019
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002020 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002021 if (ret)
2022 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002023
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002024 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002025 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002026 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002027 return PTR_ERR(vm->scratch_pt);
2028 }
2029
2030 gen6_initialize_pt(vm, vm->scratch_pt);
2031
2032 return 0;
2033}
2034
2035static void gen6_free_scratch(struct i915_address_space *vm)
2036{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002037 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002038
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002039 free_pt(dev_priv, vm->scratch_pt);
2040 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002041}
2042
Daniel Vetter061dd492015-04-14 17:35:13 +02002043static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002044{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002045 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002046 struct i915_page_directory *pd = &ppgtt->pd;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002047 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierry09942c62015-04-08 12:13:30 +01002048 struct i915_page_table *pt;
2049 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002050
Daniel Vetter061dd492015-04-14 17:35:13 +02002051 drm_mm_remove_node(&ppgtt->node);
2052
Dave Gordon731f74c2016-06-24 19:37:46 +01002053 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002054 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002055 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002056
Mika Kuoppala8776f022015-06-30 18:16:40 +03002057 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002058}
2059
Ben Widawskyb1465202014-02-19 22:05:49 -08002060static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002061{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002062 struct i915_address_space *vm = &ppgtt->base;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002063 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002064 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002065 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08002066 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002067
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002068 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2069 * allocator works in address space sizes, so it's multiplied by page
2070 * size. We allocate at the top of the GTT to avoid fragmentation.
2071 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002072 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002073
Mika Kuoppala8776f022015-06-30 18:16:40 +03002074 ret = gen6_init_scratch(vm);
2075 if (ret)
2076 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002077
Ben Widawskye3cc1992013-12-06 14:11:08 -08002078alloc:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002079 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002080 &ppgtt->node, GEN6_PD_SIZE,
2081 GEN6_PD_ALIGN, 0,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002082 0, ggtt->base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07002083 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002084 if (ret == -ENOSPC && !retried) {
Chris Wilsone522ac22016-08-04 16:32:18 +01002085 ret = i915_gem_evict_something(&ggtt->base,
Ben Widawskye3cc1992013-12-06 14:11:08 -08002086 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02002087 I915_CACHE_NONE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002088 0, ggtt->base.total,
Chris Wilsond23db882014-05-23 08:48:08 +02002089 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002090 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002091 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002092
2093 retried = true;
2094 goto alloc;
2095 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002096
Ben Widawskyc8c26622015-01-22 17:01:25 +00002097 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002098 goto err_out;
2099
Ben Widawskyc8c26622015-01-22 17:01:25 +00002100
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002101 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002102 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002103
Ben Widawskyc8c26622015-01-22 17:01:25 +00002104 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002105
2106err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002107 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002108 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002109}
2110
Ben Widawskyb1465202014-02-19 22:05:49 -08002111static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2112{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002113 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002114}
2115
Michel Thierry4933d512015-03-24 15:46:22 +00002116static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2117 uint64_t start, uint64_t length)
2118{
Michel Thierryec565b32015-04-08 12:13:23 +01002119 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002120 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002121
Dave Gordon731f74c2016-06-24 19:37:46 +01002122 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002123 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002124}
2125
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002126static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002127{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002128 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002129 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002130 int ret;
2131
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002132 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002133 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002134 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002135 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002136 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002137 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002138 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002139 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002140 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002141
2142 ret = gen6_ppgtt_alloc(ppgtt);
2143 if (ret)
2144 return ret;
2145
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002146 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002147 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2148 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002149 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2150 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002151 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f6f2013-11-25 09:54:34 -08002152 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002153 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002154 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002155
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002156 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002157 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002158
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002159 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002160 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002161
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002162 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002163
Ben Widawsky678d96f2015-03-16 16:00:56 +00002164 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2165
Thierry Reding440fd522015-01-23 09:05:06 +01002166 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002167 ppgtt->node.size >> 20,
2168 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002169
Daniel Vetterfa76da32014-08-06 20:19:54 +02002170 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002171 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002172
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002173 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002174}
2175
Chris Wilson2bfa9962016-08-04 07:52:25 +01002176static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2177 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002178{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002179 ppgtt->base.dev = &dev_priv->drm;
Daniel Vetter3440d262013-01-24 13:49:56 -08002180
Chris Wilson2bfa9962016-08-04 07:52:25 +01002181 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002182 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002183 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002184 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002185}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002186
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002187static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002188 struct drm_i915_private *dev_priv,
2189 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002190{
Chris Wilson80b204b2016-10-28 13:58:58 +01002191 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002192 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002193 INIT_LIST_HEAD(&vm->active_list);
2194 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002195 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002196 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2197}
2198
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002199static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002200{
Tim Gored5165eb2016-02-04 11:49:34 +00002201 /* This function is for gtt related workarounds. This function is
2202 * called on driver load and after a GPU reset, so you can place
2203 * workarounds here even if they get overwritten by GPU reset.
2204 */
2205 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002206 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002207 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002208 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002209 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002210 else if (IS_SKYLAKE(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002211 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002212 else if (IS_BROXTON(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002213 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2214}
2215
Chris Wilson2bfa9962016-08-04 07:52:25 +01002216static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2217 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002218 struct drm_i915_file_private *file_priv,
2219 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002220{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002221 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002222
Chris Wilson2bfa9962016-08-04 07:52:25 +01002223 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002224 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002225 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002226 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002227 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002228 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002229
2230 return ret;
2231}
2232
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002233int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002234{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002235 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002236
Thomas Daniel671b50132014-08-20 16:24:50 +01002237 /* In the case of execlists, PPGTT is enabled by the context descriptor
2238 * and the PDPs are contained within the context itself. We don't
2239 * need to do anything here. */
2240 if (i915.enable_execlists)
2241 return 0;
2242
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002243 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002244 return 0;
2245
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002246 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002247 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002248 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002249 gen7_ppgtt_enable(dev_priv);
2250 else if (INTEL_GEN(dev_priv) >= 8)
2251 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002252 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002253 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002254
John Harrison4ad2fd82015-06-18 13:11:20 +01002255 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002256}
John Harrison4ad2fd82015-06-18 13:11:20 +01002257
Daniel Vetter4d884702014-08-06 15:04:47 +02002258struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002259i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002260 struct drm_i915_file_private *fpriv,
2261 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002262{
2263 struct i915_hw_ppgtt *ppgtt;
2264 int ret;
2265
2266 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2267 if (!ppgtt)
2268 return ERR_PTR(-ENOMEM);
2269
Chris Wilson80b204b2016-10-28 13:58:58 +01002270 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002271 if (ret) {
2272 kfree(ppgtt);
2273 return ERR_PTR(ret);
2274 }
2275
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002276 trace_i915_ppgtt_create(&ppgtt->base);
2277
Daniel Vetter4d884702014-08-06 15:04:47 +02002278 return ppgtt;
2279}
2280
Daniel Vetteree960be2014-08-06 15:04:45 +02002281void i915_ppgtt_release(struct kref *kref)
2282{
2283 struct i915_hw_ppgtt *ppgtt =
2284 container_of(kref, struct i915_hw_ppgtt, ref);
2285
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002286 trace_i915_ppgtt_release(&ppgtt->base);
2287
Chris Wilson50e046b2016-08-04 07:52:46 +01002288 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002289 WARN_ON(!list_empty(&ppgtt->base.active_list));
2290 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002291 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002292
Chris Wilson80b204b2016-10-28 13:58:58 +01002293 i915_gem_timeline_fini(&ppgtt->base.timeline);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002294 list_del(&ppgtt->base.global_link);
2295 drm_mm_takedown(&ppgtt->base.mm);
2296
Daniel Vetteree960be2014-08-06 15:04:45 +02002297 ppgtt->base.cleanup(&ppgtt->base);
2298 kfree(ppgtt);
2299}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002300
Ben Widawskya81cc002013-01-18 12:30:31 -08002301/* Certain Gen5 chipsets require require idling the GPU before
2302 * unmapping anything from the GTT when VT-d is enabled.
2303 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002304static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002305{
2306#ifdef CONFIG_INTEL_IOMMU
2307 /* Query intel_iommu to see if we need the workaround. Presumably that
2308 * was loaded first.
2309 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002310 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002311 return true;
2312#endif
2313 return false;
2314}
2315
Chris Wilsondc979972016-05-10 14:10:04 +01002316void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002317{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002318 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302319 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002320
Chris Wilsondc979972016-05-10 14:10:04 +01002321 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002322 return;
2323
Akash Goel3b3f1652016-10-13 22:44:48 +05302324 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002325 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002326 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002327 if (fault_reg & RING_FAULT_VALID) {
2328 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002329 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002330 "\tAddress space: %s\n"
2331 "\tSource ID: %d\n"
2332 "\tType: %d\n",
2333 fault_reg & PAGE_MASK,
2334 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2335 RING_FAULT_SRCID(fault_reg),
2336 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002337 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002338 fault_reg & ~RING_FAULT_VALID);
2339 }
2340 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302341
2342 /* Engine specific init may not have been done till this point. */
2343 if (dev_priv->engine[RCS])
2344 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002345}
2346
Chris Wilson91e56492014-09-25 10:13:12 +01002347static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2348{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002349 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002350 intel_gtt_chipset_flush();
2351 } else {
2352 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2353 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2354 }
2355}
2356
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002357void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002358{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002359 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002360
2361 /* Don't bother messing with faults pre GEN6 as we have little
2362 * documentation supporting that it's a good idea.
2363 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002364 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002365 return;
2366
Chris Wilsondc979972016-05-10 14:10:04 +01002367 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002368
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002369 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002370
2371 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002372}
2373
Chris Wilson03ac84f2016-10-28 13:58:36 +01002374int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2375 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002376{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002377 if (dma_map_sg(&obj->base.dev->pdev->dev,
2378 pages->sgl, pages->nents,
2379 PCI_DMA_BIDIRECTIONAL))
2380 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01002381
Chris Wilson03ac84f2016-10-28 13:58:36 +01002382 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002383}
2384
Daniel Vetter2c642b02015-04-14 17:35:26 +02002385static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002386{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002387 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002388}
2389
Chris Wilsond6473f52016-06-10 14:22:59 +05302390static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2391 dma_addr_t addr,
2392 uint64_t offset,
2393 enum i915_cache_level level,
2394 u32 unused)
2395{
2396 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2397 gen8_pte_t __iomem *pte =
2398 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2399 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302400
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002401 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302402
2403 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2404 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302405}
2406
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002407static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2408 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002409 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302410 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002411{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002412 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002413 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002414 struct sgt_iter sgt_iter;
2415 gen8_pte_t __iomem *gtt_entries;
2416 gen8_pte_t gtt_entry;
2417 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002418 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002419
Dave Gordon85d12252016-05-20 11:54:06 +01002420 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2421
2422 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002423 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002424 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002425 }
2426
2427 /*
2428 * XXX: This serves as a posting read to make sure that the PTE has
2429 * actually been updated. There is some concern that even though
2430 * registers and PTEs are within the same BAR that they are potentially
2431 * of NUMA access patterns. Therefore, even with the way we assume
2432 * hardware should work, we must keep this posting read for paranoia.
2433 */
2434 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002435 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002436
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002437 /* This next bit makes the above posting read even more important. We
2438 * want to flush the TLBs only after we're certain all the PTE updates
2439 * have finished.
2440 */
2441 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2442 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002443}
2444
Chris Wilsonc1403302015-11-18 15:19:39 +00002445struct insert_entries {
2446 struct i915_address_space *vm;
2447 struct sg_table *st;
2448 uint64_t start;
2449 enum i915_cache_level level;
2450 u32 flags;
2451};
2452
2453static int gen8_ggtt_insert_entries__cb(void *_arg)
2454{
2455 struct insert_entries *arg = _arg;
2456 gen8_ggtt_insert_entries(arg->vm, arg->st,
2457 arg->start, arg->level, arg->flags);
2458 return 0;
2459}
2460
2461static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2462 struct sg_table *st,
2463 uint64_t start,
2464 enum i915_cache_level level,
2465 u32 flags)
2466{
2467 struct insert_entries arg = { vm, st, start, level, flags };
2468 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2469}
2470
Chris Wilsond6473f52016-06-10 14:22:59 +05302471static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2472 dma_addr_t addr,
2473 uint64_t offset,
2474 enum i915_cache_level level,
2475 u32 flags)
2476{
2477 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2478 gen6_pte_t __iomem *pte =
2479 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2480 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302481
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002482 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302483
2484 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2485 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302486}
2487
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002488/*
2489 * Binds an object into the global gtt with the specified cache level. The object
2490 * will be accessible to the GPU via commands whose operands reference offsets
2491 * within the global GTT as well as accessible by the GPU through the GMADR
2492 * mapped BAR (dev_priv->mm.gtt->gtt).
2493 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002494static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002495 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002496 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302497 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002498{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002499 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002500 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002501 struct sgt_iter sgt_iter;
2502 gen6_pte_t __iomem *gtt_entries;
2503 gen6_pte_t gtt_entry;
2504 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002505 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002506
Dave Gordon85d12252016-05-20 11:54:06 +01002507 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2508
2509 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002510 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002511 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002512 }
2513
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002514 /* XXX: This serves as a posting read to make sure that the PTE has
2515 * actually been updated. There is some concern that even though
2516 * registers and PTEs are within the same BAR that they are potentially
2517 * of NUMA access patterns. Therefore, even with the way we assume
2518 * hardware should work, we must keep this posting read for paranoia.
2519 */
Dave Gordon85d12252016-05-20 11:54:06 +01002520 if (i != 0)
2521 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002522
2523 /* This next bit makes the above posting read even more important. We
2524 * want to flush the TLBs only after we're certain all the PTE updates
2525 * have finished.
2526 */
2527 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2528 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002529}
2530
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002531static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002532 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002533{
2534}
2535
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002536static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002537 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002538{
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 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002543 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2544 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002545 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 = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002553 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002554 for (i = 0; i < num_entries; i++)
2555 gen8_set_pte(&gtt_base[i], scratch_pte);
2556 readl(gtt_base);
2557}
2558
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002559static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002560 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002561 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002562{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002563 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002564 unsigned first_entry = start >> PAGE_SHIFT;
2565 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002566 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002567 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2568 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002569 int i;
2570
2571 if (WARN(num_entries > max_entries,
2572 "First entry = %d; Num entries = %d (max=%d)\n",
2573 first_entry, num_entries, max_entries))
2574 num_entries = max_entries;
2575
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002576 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002577 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002578
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002579 for (i = 0; i < num_entries; i++)
2580 iowrite32(scratch_pte, &gtt_base[i]);
2581 readl(gtt_base);
2582}
2583
Chris Wilsond6473f52016-06-10 14:22:59 +05302584static void i915_ggtt_insert_page(struct i915_address_space *vm,
2585 dma_addr_t addr,
2586 uint64_t offset,
2587 enum i915_cache_level cache_level,
2588 u32 unused)
2589{
Chris Wilsond6473f52016-06-10 14:22:59 +05302590 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2591 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302592
2593 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302594}
2595
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002596static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2597 struct sg_table *pages,
2598 uint64_t start,
2599 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002600{
2601 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2602 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2603
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002604 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002605
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002606}
2607
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002608static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002609 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002610 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002611{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002612 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002613}
2614
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002615static int ggtt_bind_vma(struct i915_vma *vma,
2616 enum i915_cache_level cache_level,
2617 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002618{
Chris Wilson9c870d02016-10-24 13:42:15 +01002619 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Daniel Vetter0a878712015-10-15 14:23:01 +02002620 struct drm_i915_gem_object *obj = vma->obj;
2621 u32 pte_flags = 0;
2622 int ret;
2623
2624 ret = i915_get_ggtt_vma_pages(vma);
2625 if (ret)
2626 return ret;
2627
2628 /* Currently applicable only to VLV */
2629 if (obj->gt_ro)
2630 pte_flags |= PTE_READ_ONLY;
2631
Chris Wilson9c870d02016-10-24 13:42:15 +01002632 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002633 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002634 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002635 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002636
2637 /*
2638 * Without aliasing PPGTT there's no difference between
2639 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2640 * upgrade to both bound if we bind either to avoid double-binding.
2641 */
Chris Wilson3272db52016-08-04 16:32:32 +01002642 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002643
2644 return 0;
2645}
2646
2647static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2648 enum i915_cache_level cache_level,
2649 u32 flags)
2650{
Chris Wilson9c870d02016-10-24 13:42:15 +01002651 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Chris Wilson321d1782015-11-20 10:27:18 +00002652 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002653 int ret;
2654
2655 ret = i915_get_ggtt_vma_pages(vma);
2656 if (ret)
2657 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002658
Akash Goel24f3a8c2014-06-17 10:59:42 +05302659 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002660 pte_flags = 0;
2661 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002662 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302663
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002664
Chris Wilson3272db52016-08-04 16:32:32 +01002665 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002666 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002667 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002668 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002669 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002670 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002671 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002672
Chris Wilson3272db52016-08-04 16:32:32 +01002673 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002674 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002675 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002676 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002677 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002678 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002679
2680 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002681}
2682
2683static void ggtt_unbind_vma(struct i915_vma *vma)
2684{
Chris Wilson9c870d02016-10-24 13:42:15 +01002685 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
2686 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002687 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002688
Chris Wilson9c870d02016-10-24 13:42:15 +01002689 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2690 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002691 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002692 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002693 intel_runtime_pm_put(i915);
2694 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002695
Chris Wilson3272db52016-08-04 16:32:32 +01002696 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002697 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002698 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002699}
2700
Chris Wilson03ac84f2016-10-28 13:58:36 +01002701void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2702 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002703{
David Weinehall52a05c32016-08-22 13:32:44 +03002704 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2705 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002706 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002707
Chris Wilson307dc252016-08-05 10:14:12 +01002708 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002709 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002710 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2711 /* Wait a bit, in hopes it avoids the hang */
2712 udelay(10);
2713 }
2714 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002715
Chris Wilson03ac84f2016-10-28 13:58:36 +01002716 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002717}
Daniel Vetter644ec022012-03-26 09:45:40 +02002718
Chris Wilson42d6ab42012-07-26 11:49:32 +01002719static void i915_gtt_color_adjust(struct drm_mm_node *node,
2720 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002721 u64 *start,
2722 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002723{
2724 if (node->color != color)
2725 *start += 4096;
2726
Chris Wilson2a1d7752016-07-26 12:01:51 +01002727 node = list_first_entry_or_null(&node->node_list,
2728 struct drm_mm_node,
2729 node_list);
2730 if (node && node->allocated && node->color != color)
2731 *end -= 4096;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002732}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002733
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002734int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002735{
Ben Widawskye78891c2013-01-25 16:41:04 -08002736 /* Let GEM Manage all of the aperture.
2737 *
2738 * However, leave one page at the end still bound to the scratch page.
2739 * There are a number of places where the hardware apparently prefetches
2740 * past the end of the object, and we've seen multiple hangs with the
2741 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2742 * aperture. One page should be enough to keep any prefetching inside
2743 * of the aperture.
2744 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002745 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002746 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002747 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002748 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002749 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002750
Zhi Wangb02d22a2016-06-16 08:06:59 -04002751 ret = intel_vgt_balloon(dev_priv);
2752 if (ret)
2753 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002754
Chris Wilson95374d72016-10-12 10:05:20 +01002755 /* Reserve a mappable slot for our lockless error capture */
2756 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2757 &ggtt->error_capture,
2758 4096, 0, -1,
2759 0, ggtt->mappable_end,
2760 0, 0);
2761 if (ret)
2762 return ret;
2763
Chris Wilsoned2f3452012-11-15 11:32:19 +00002764 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002765 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002766 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2767 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002768 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002769 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002770 }
2771
2772 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002773 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002774 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002775
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002776 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002777 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002778 if (!ppgtt) {
2779 ret = -ENOMEM;
2780 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002781 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002782
Chris Wilson95374d72016-10-12 10:05:20 +01002783 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2784 if (ret)
2785 goto err_ppgtt;
2786
2787 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002788 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2789 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002790 if (ret)
2791 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002792 }
2793
2794 ppgtt->base.clear_range(&ppgtt->base,
2795 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002796 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002797
Daniel Vetterfa76da32014-08-06 20:19:54 +02002798 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002799 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2800 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002801 }
2802
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002803 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002804
2805err_ppgtt_cleanup:
2806 ppgtt->base.cleanup(&ppgtt->base);
2807err_ppgtt:
2808 kfree(ppgtt);
2809err:
2810 drm_mm_remove_node(&ggtt->error_capture);
2811 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002812}
2813
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002814/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002815 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002816 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002817 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002818void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002819{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002820 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002821
Daniel Vetter70e32542014-08-06 15:04:57 +02002822 if (dev_priv->mm.aliasing_ppgtt) {
2823 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002824 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002825 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002826 }
2827
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002828 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002829
Chris Wilson95374d72016-10-12 10:05:20 +01002830 if (drm_mm_node_allocated(&ggtt->error_capture))
2831 drm_mm_remove_node(&ggtt->error_capture);
2832
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002833 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002834 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002835
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002836 drm_mm_takedown(&ggtt->base.mm);
2837 list_del(&ggtt->base.global_link);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002838 }
2839
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002840 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002841
2842 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002843 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002844}
Daniel Vetter70e32542014-08-06 15:04:57 +02002845
Daniel Vetter2c642b02015-04-14 17:35:26 +02002846static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002847{
2848 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2849 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2850 return snb_gmch_ctl << 20;
2851}
2852
Daniel Vetter2c642b02015-04-14 17:35:26 +02002853static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002854{
2855 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2856 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2857 if (bdw_gmch_ctl)
2858 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002859
2860#ifdef CONFIG_X86_32
2861 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2862 if (bdw_gmch_ctl > 4)
2863 bdw_gmch_ctl = 4;
2864#endif
2865
Ben Widawsky9459d252013-11-03 16:53:55 -08002866 return bdw_gmch_ctl << 20;
2867}
2868
Daniel Vetter2c642b02015-04-14 17:35:26 +02002869static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002870{
2871 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2872 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2873
2874 if (gmch_ctrl)
2875 return 1 << (20 + gmch_ctrl);
2876
2877 return 0;
2878}
2879
Daniel Vetter2c642b02015-04-14 17:35:26 +02002880static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002881{
2882 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2883 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2884 return snb_gmch_ctl << 25; /* 32 MB units */
2885}
2886
Daniel Vetter2c642b02015-04-14 17:35:26 +02002887static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002888{
2889 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2890 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2891 return bdw_gmch_ctl << 25; /* 32 MB units */
2892}
2893
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002894static size_t chv_get_stolen_size(u16 gmch_ctrl)
2895{
2896 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2897 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2898
2899 /*
2900 * 0x0 to 0x10: 32MB increments starting at 0MB
2901 * 0x11 to 0x16: 4MB increments starting at 8MB
2902 * 0x17 to 0x1d: 4MB increments start at 36MB
2903 */
2904 if (gmch_ctrl < 0x11)
2905 return gmch_ctrl << 25;
2906 else if (gmch_ctrl < 0x17)
2907 return (gmch_ctrl - 0x11 + 2) << 22;
2908 else
2909 return (gmch_ctrl - 0x17 + 9) << 22;
2910}
2911
Damien Lespiau66375012014-01-09 18:02:46 +00002912static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2913{
2914 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2915 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2916
2917 if (gen9_gmch_ctl < 0xf0)
2918 return gen9_gmch_ctl << 25; /* 32 MB units */
2919 else
2920 /* 4MB increments starting at 0xf0 for 4MB */
2921 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2922}
2923
Chris Wilson34c998b2016-08-04 07:52:24 +01002924static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002925{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002926 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Chris Wilson34c998b2016-08-04 07:52:24 +01002927 struct pci_dev *pdev = ggtt->base.dev->pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002928 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002929 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002930
2931 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002932 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002933
Imre Deak2a073f892015-03-27 13:07:33 +02002934 /*
2935 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2936 * dropped. For WC mappings in general we have 64 byte burst writes
2937 * when the WC buffer is flushed, so we can't use it, but have to
2938 * resort to an uncached mapping. The WC issue is easily caught by the
2939 * readback check when writing GTT PTE entries.
2940 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002941 if (IS_BROXTON(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002942 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002943 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002944 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002945 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002946 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002947 return -ENOMEM;
2948 }
2949
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002950 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002951 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002952 DRM_ERROR("Scratch setup failed\n");
2953 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002954 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002955 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002956 }
2957
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002958 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002959}
2960
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002961/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2962 * bits. When using advanced contexts each context stores its own PAT, but
2963 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002964static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002965{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002966 uint64_t pat;
2967
2968 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2969 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2970 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2971 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2972 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2973 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2974 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2975 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2976
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002977 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002978 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2979 * so RTL will always use the value corresponding to
2980 * pat_sel = 000".
2981 * So let's disable cache for GGTT to avoid screen corruptions.
2982 * MOCS still can be used though.
2983 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2984 * before this patch, i.e. the same uncached + snooping access
2985 * like on gen6/7 seems to be in effect.
2986 * - So this just fixes blitter/render access. Again it looks
2987 * like it's not just uncached access, but uncached + snooping.
2988 * So we can still hold onto all our assumptions wrt cpu
2989 * clflushing on LLC machines.
2990 */
2991 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2992
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002993 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2994 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002995 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2996 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002997}
2998
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002999static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3000{
3001 uint64_t pat;
3002
3003 /*
3004 * Map WB on BDW to snooped on CHV.
3005 *
3006 * Only the snoop bit has meaning for CHV, the rest is
3007 * ignored.
3008 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003009 * The hardware will never snoop for certain types of accesses:
3010 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3011 * - PPGTT page tables
3012 * - some other special cycles
3013 *
3014 * As with BDW, we also need to consider the following for GT accesses:
3015 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3016 * so RTL will always use the value corresponding to
3017 * pat_sel = 000".
3018 * Which means we must set the snoop bit in PAT entry 0
3019 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003020 */
3021 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3022 GEN8_PPAT(1, 0) |
3023 GEN8_PPAT(2, 0) |
3024 GEN8_PPAT(3, 0) |
3025 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3026 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3027 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3028 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3029
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003030 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3031 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003032}
3033
Chris Wilson34c998b2016-08-04 07:52:24 +01003034static void gen6_gmch_remove(struct i915_address_space *vm)
3035{
3036 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3037
3038 iounmap(ggtt->gsm);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003039 cleanup_scratch_page(to_i915(vm->dev), &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003040}
3041
Joonas Lahtinend507d732016-03-18 10:42:58 +02003042static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003043{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003044 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3045 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003046 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003047 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003048
3049 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003050 ggtt->mappable_base = pci_resource_start(pdev, 2);
3051 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003052
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003053 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3054 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003055
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003056 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003057
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003058 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003059 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003060 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003061 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003062 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003063 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003064 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003065 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003066 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003067 }
Ben Widawsky63340132013-11-04 19:32:22 -08003068
Chris Wilson34c998b2016-08-04 07:52:24 +01003069 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003070
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003071 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003072 chv_setup_private_ppat(dev_priv);
3073 else
3074 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003075
Chris Wilson34c998b2016-08-04 07:52:24 +01003076 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003077 ggtt->base.bind_vma = ggtt_bind_vma;
3078 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303079 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003080 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003081 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003082 ggtt->base.clear_range = gen8_ggtt_clear_range;
3083
3084 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3085 if (IS_CHERRYVIEW(dev_priv))
3086 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3087
Chris Wilson34c998b2016-08-04 07:52:24 +01003088 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003089}
3090
Joonas Lahtinend507d732016-03-18 10:42:58 +02003091static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003092{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003093 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3094 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003095 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003096 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003097
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003098 ggtt->mappable_base = pci_resource_start(pdev, 2);
3099 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003100
Ben Widawskybaa09f52013-01-24 13:49:57 -08003101 /* 64/512MB is the current min/max we actually know of, but this is just
3102 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003103 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003104 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003105 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003106 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003107 }
3108
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003109 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3110 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3111 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003112
Joonas Lahtinend507d732016-03-18 10:42:58 +02003113 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003114
Chris Wilson34c998b2016-08-04 07:52:24 +01003115 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3116 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003117
Joonas Lahtinend507d732016-03-18 10:42:58 +02003118 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303119 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003120 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3121 ggtt->base.bind_vma = ggtt_bind_vma;
3122 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003123 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003124
Chris Wilson34c998b2016-08-04 07:52:24 +01003125 if (HAS_EDRAM(dev_priv))
3126 ggtt->base.pte_encode = iris_pte_encode;
3127 else if (IS_HASWELL(dev_priv))
3128 ggtt->base.pte_encode = hsw_pte_encode;
3129 else if (IS_VALLEYVIEW(dev_priv))
3130 ggtt->base.pte_encode = byt_pte_encode;
3131 else if (INTEL_GEN(dev_priv) >= 7)
3132 ggtt->base.pte_encode = ivb_pte_encode;
3133 else
3134 ggtt->base.pte_encode = snb_pte_encode;
3135
3136 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003137}
3138
Chris Wilson34c998b2016-08-04 07:52:24 +01003139static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003140{
Chris Wilson34c998b2016-08-04 07:52:24 +01003141 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003142}
3143
Joonas Lahtinend507d732016-03-18 10:42:58 +02003144static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003145{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003146 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003147 int ret;
3148
Chris Wilson91c8a322016-07-05 10:40:23 +01003149 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003150 if (!ret) {
3151 DRM_ERROR("failed to set up gmch\n");
3152 return -EIO;
3153 }
3154
Joonas Lahtinend507d732016-03-18 10:42:58 +02003155 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3156 &ggtt->mappable_base, &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003157
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003158 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303159 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003160 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3161 ggtt->base.clear_range = i915_ggtt_clear_range;
3162 ggtt->base.bind_vma = ggtt_bind_vma;
3163 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003164 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003165
Joonas Lahtinend507d732016-03-18 10:42:58 +02003166 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003167 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3168
Ben Widawskybaa09f52013-01-24 13:49:57 -08003169 return 0;
3170}
3171
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003172/**
Chris Wilson0088e522016-08-04 07:52:21 +01003173 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003174 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003175 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003176int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003177{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003178 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003179 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003180
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003181 ggtt->base.dev = &dev_priv->drm;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003182
Chris Wilson34c998b2016-08-04 07:52:24 +01003183 if (INTEL_GEN(dev_priv) <= 5)
3184 ret = i915_gmch_probe(ggtt);
3185 else if (INTEL_GEN(dev_priv) < 8)
3186 ret = gen6_gmch_probe(ggtt);
3187 else
3188 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003189 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003190 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003191
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003192 if ((ggtt->base.total - 1) >> 32) {
3193 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003194 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003195 ggtt->base.total >> 20);
3196 ggtt->base.total = 1ULL << 32;
3197 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3198 }
3199
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003200 if (ggtt->mappable_end > ggtt->base.total) {
3201 DRM_ERROR("mappable aperture extends past end of GGTT,"
3202 " aperture=%llx, total=%llx\n",
3203 ggtt->mappable_end, ggtt->base.total);
3204 ggtt->mappable_end = ggtt->base.total;
3205 }
3206
Ben Widawskybaa09f52013-01-24 13:49:57 -08003207 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003208 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003209 ggtt->base.total >> 20);
3210 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3211 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003212#ifdef CONFIG_INTEL_IOMMU
3213 if (intel_iommu_gfx_mapped)
3214 DRM_INFO("VT-d active for gfx access\n");
3215#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003216
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003217 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003218}
3219
3220/**
3221 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003222 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003223 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003224int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003225{
Chris Wilson0088e522016-08-04 07:52:21 +01003226 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3227 int ret;
3228
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003229 INIT_LIST_HEAD(&dev_priv->vm_list);
3230
3231 /* Subtract the guard page before address space initialization to
3232 * shrink the range used by drm_mm.
3233 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003234 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003235 ggtt->base.total -= PAGE_SIZE;
Chris Wilson80b204b2016-10-28 13:58:58 +01003236 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003237 ggtt->base.total += PAGE_SIZE;
3238 if (!HAS_LLC(dev_priv))
3239 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003240 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003241
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003242 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3243 dev_priv->ggtt.mappable_base,
3244 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003245 ret = -EIO;
3246 goto out_gtt_cleanup;
3247 }
3248
3249 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3250
Chris Wilson0088e522016-08-04 07:52:21 +01003251 /*
3252 * Initialise stolen early so that we may reserve preallocated
3253 * objects for the BIOS to KMS transition.
3254 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003255 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003256 if (ret)
3257 goto out_gtt_cleanup;
3258
3259 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003260
3261out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003262 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003263 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003264}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003265
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003266int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003267{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003268 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003269 return -EIO;
3270
3271 return 0;
3272}
3273
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003274void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003275{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003276 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003277 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003278
Chris Wilsondc979972016-05-10 14:10:04 +01003279 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003280
3281 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003282 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003283
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003284 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3285
3286 /* clflush objects bound into the GGTT and rebind them. */
3287 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003288 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003289 bool ggtt_bound = false;
3290 struct i915_vma *vma;
3291
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003292 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003293 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003294 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003295
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003296 if (!i915_vma_unbind(vma))
3297 continue;
3298
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003299 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3300 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003301 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003302 }
3303
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003304 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003305 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003306 }
3307
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003308 ggtt->base.closed = false;
3309
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003310 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01003311 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003312 chv_setup_private_ppat(dev_priv);
3313 else
3314 bdw_setup_private_ppat(dev_priv);
3315
3316 return;
3317 }
3318
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003319 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003320 struct i915_address_space *vm;
3321
Daniel Vetterfa423312015-04-14 17:35:23 +02003322 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3323 /* TODO: Perhaps it shouldn't be gen6 specific */
3324
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003325 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003326
Chris Wilson2bfa9962016-08-04 07:52:25 +01003327 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003328 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003329 else
3330 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003331
3332 gen6_write_page_range(dev_priv, &ppgtt->pd,
3333 0, ppgtt->base.total);
3334 }
3335 }
3336
3337 i915_ggtt_flush(dev_priv);
3338}
3339
Chris Wilson058d88c2016-08-15 10:49:06 +01003340struct i915_vma *
3341i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3342 struct i915_address_space *vm,
3343 const struct i915_ggtt_view *view)
3344{
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003345 struct rb_node *rb;
Chris Wilson058d88c2016-08-15 10:49:06 +01003346
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003347 rb = obj->vma_tree.rb_node;
3348 while (rb) {
3349 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
3350 long cmp;
3351
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003352 cmp = i915_vma_compare(vma, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003353 if (cmp == 0)
Chris Wilson058d88c2016-08-15 10:49:06 +01003354 return vma;
3355
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003356 if (cmp < 0)
3357 rb = rb->rb_right;
3358 else
3359 rb = rb->rb_left;
3360 }
3361
Chris Wilson058d88c2016-08-15 10:49:06 +01003362 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003363}
3364
3365struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003366i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003367 struct i915_address_space *vm,
3368 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003369{
3370 struct i915_vma *vma;
3371
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003372 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +01003373 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3374
3375 vma = i915_gem_obj_to_vma(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003376 if (!vma) {
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003377 vma = i915_vma_create(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003378 GEM_BUG_ON(vma != i915_gem_obj_to_vma(obj, vm, view));
3379 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003380
Chris Wilson3272db52016-08-04 16:32:32 +01003381 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003382 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003383}
3384
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003385static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003386rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003387 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003388 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003389 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003390{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003391 unsigned int column, row;
3392 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003393
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003394 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003395 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003396 for (row = 0; row < height; row++) {
3397 st->nents++;
3398 /* We don't need the pages, but need to initialize
3399 * the entries so the sg list can be happily traversed.
3400 * The only thing we need are DMA addresses.
3401 */
3402 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003403 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003404 sg_dma_len(sg) = PAGE_SIZE;
3405 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003406 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003407 }
3408 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003409
3410 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003411}
3412
3413static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003414intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003415 struct drm_i915_gem_object *obj)
3416{
Dave Gordon85d12252016-05-20 11:54:06 +01003417 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003418 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003419 struct sgt_iter sgt_iter;
3420 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003421 unsigned long i;
3422 dma_addr_t *page_addr_list;
3423 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003424 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003425 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003426
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003427 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003428 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003429 sizeof(dma_addr_t),
3430 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003431 if (!page_addr_list)
3432 return ERR_PTR(ret);
3433
3434 /* Allocate target SG list. */
3435 st = kmalloc(sizeof(*st), GFP_KERNEL);
3436 if (!st)
3437 goto err_st_alloc;
3438
Ville Syrjälä6687c902015-09-15 13:16:41 +03003439 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003440 if (ret)
3441 goto err_sg_alloc;
3442
3443 /* Populate source page list from the object. */
3444 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003445 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003446 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003447
Dave Gordon85d12252016-05-20 11:54:06 +01003448 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003449 st->nents = 0;
3450 sg = st->sgl;
3451
Ville Syrjälä6687c902015-09-15 13:16:41 +03003452 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3453 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3454 rot_info->plane[i].width, rot_info->plane[i].height,
3455 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003456 }
3457
Ville Syrjälä6687c902015-09-15 13:16:41 +03003458 DRM_DEBUG_KMS("Created rotated page 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);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003460
3461 drm_free_large(page_addr_list);
3462
3463 return st;
3464
3465err_sg_alloc:
3466 kfree(st);
3467err_st_alloc:
3468 drm_free_large(page_addr_list);
3469
Ville Syrjälä6687c902015-09-15 13:16:41 +03003470 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3471 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3472
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003473 return ERR_PTR(ret);
3474}
3475
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003476static struct sg_table *
3477intel_partial_pages(const struct i915_ggtt_view *view,
3478 struct drm_i915_gem_object *obj)
3479{
3480 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003481 struct scatterlist *sg, *iter;
3482 unsigned int count = view->params.partial.size;
3483 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003484 int ret = -ENOMEM;
3485
3486 st = kmalloc(sizeof(*st), GFP_KERNEL);
3487 if (!st)
3488 goto err_st_alloc;
3489
Chris Wilsond2a84a72016-10-28 13:58:34 +01003490 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003491 if (ret)
3492 goto err_sg_alloc;
3493
Chris Wilsond2a84a72016-10-28 13:58:34 +01003494 iter = i915_gem_object_get_sg(obj,
3495 view->params.partial.offset,
3496 &offset);
3497 GEM_BUG_ON(!iter);
3498
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003499 sg = st->sgl;
3500 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003501 do {
3502 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003503
Chris Wilsond2a84a72016-10-28 13:58:34 +01003504 len = min(iter->length - (offset << PAGE_SHIFT),
3505 count << PAGE_SHIFT);
3506 sg_set_page(sg, NULL, len, 0);
3507 sg_dma_address(sg) =
3508 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3509 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003510
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003511 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003512 count -= len >> PAGE_SHIFT;
3513 if (count == 0) {
3514 sg_mark_end(sg);
3515 return st;
3516 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003517
Chris Wilsond2a84a72016-10-28 13:58:34 +01003518 sg = __sg_next(sg);
3519 iter = __sg_next(iter);
3520 offset = 0;
3521 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003522
3523err_sg_alloc:
3524 kfree(st);
3525err_st_alloc:
3526 return ERR_PTR(ret);
3527}
3528
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003529static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003530i915_get_ggtt_vma_pages(struct i915_vma *vma)
3531{
3532 int ret = 0;
3533
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003534 /* The vma->pages are only valid within the lifespan of the borrowed
3535 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3536 * must be the vma->pages. A simple rule is that vma->pages must only
3537 * be accessed when the obj->mm.pages are pinned.
3538 */
3539 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3540
Chris Wilson247177d2016-08-15 10:48:47 +01003541 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003542 return 0;
3543
3544 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003545 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003546 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003547 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003548 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003549 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003550 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003551 else
3552 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3553 vma->ggtt_view.type);
3554
Chris Wilson247177d2016-08-15 10:48:47 +01003555 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003556 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003557 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003558 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003559 } else if (IS_ERR(vma->pages)) {
3560 ret = PTR_ERR(vma->pages);
3561 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003562 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3563 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003564 }
3565
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003566 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003567}
3568