blob: 6cee70745fea553e97eb68c1de1e43ca0ba0b3f0 [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
Zhi Wanga18dbba2016-11-29 14:55:16 +0800739 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200740 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200741
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200742 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000743
Mika Kuoppala37c63932016-11-01 15:27:36 +0200744 while (pte < pte_end)
745 pt_vaddr[pte++] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000746
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200747 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200748
749 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200750}
751
Michał Winiarski2ce51792016-10-13 14:02:42 +0200752/* Removes entries from a single page dir, releasing it if it's empty.
753 * Caller can use the return value to update higher-level entries
754 */
755static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200756 struct i915_page_directory *pd,
757 uint64_t start,
758 uint64_t length)
759{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200760 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200761 struct i915_page_table *pt;
762 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200763 gen8_pde_t *pde_vaddr;
764 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
765 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200766
767 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000768 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100769 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000770
Michał Winiarski2ce51792016-10-13 14:02:42 +0200771 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
772 __clear_bit(pde, pd->used_pdes);
773 pde_vaddr = kmap_px(pd);
774 pde_vaddr[pde] = scratch_pde;
775 kunmap_px(ppgtt, pde_vaddr);
Zhi Wanga18dbba2016-11-29 14:55:16 +0800776 free_pt(to_i915(vm->dev), pt);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200777 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200778 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200779
Zhi Wanga18dbba2016-11-29 14:55:16 +0800780 if (bitmap_empty(pd->used_pdes, I915_PDES))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200781 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200782
783 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200784}
Ben Widawsky06fda602015-02-24 16:22:36 +0000785
Michał Winiarski2ce51792016-10-13 14:02:42 +0200786/* Removes entries from a single page dir pointer, releasing it if it's empty.
787 * Caller can use the return value to update higher-level entries
788 */
789static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200790 struct i915_page_directory_pointer *pdp,
791 uint64_t start,
792 uint64_t length)
793{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200794 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200795 struct i915_page_directory *pd;
796 uint64_t pdpe;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200797 gen8_ppgtt_pdpe_t *pdpe_vaddr;
798 gen8_ppgtt_pdpe_t scratch_pdpe =
799 gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200800
801 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
802 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100803 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000804
Michał Winiarski2ce51792016-10-13 14:02:42 +0200805 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
806 __clear_bit(pdpe, pdp->used_pdpes);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000807 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Michał Winiarski2ce51792016-10-13 14:02:42 +0200808 pdpe_vaddr = kmap_px(pdp);
809 pdpe_vaddr[pdpe] = scratch_pdpe;
810 kunmap_px(ppgtt, pdpe_vaddr);
811 }
Zhi Wanga18dbba2016-11-29 14:55:16 +0800812 free_pd(to_i915(vm->dev), pd);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200813 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200814 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200815
Mika Kuoppalafce93752016-10-31 17:24:46 +0200816 mark_tlbs_dirty(ppgtt);
817
Zhi Wanga18dbba2016-11-29 14:55:16 +0800818 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200819 return true;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200820
821 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200822}
Ben Widawsky459108b2013-11-02 21:07:23 -0700823
Michał Winiarski2ce51792016-10-13 14:02:42 +0200824/* Removes entries from a single pml4.
825 * This is the top-level structure in 4-level page tables used on gen8+.
826 * Empty entries are always scratch pml4e.
827 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200828static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
829 struct i915_pml4 *pml4,
830 uint64_t start,
831 uint64_t length)
832{
Zhi Wanga18dbba2016-11-29 14:55:16 +0800833 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200834 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200835 struct i915_page_directory_pointer *pdp;
836 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200837 gen8_ppgtt_pml4e_t *pml4e_vaddr;
838 gen8_ppgtt_pml4e_t scratch_pml4e =
839 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC);
840
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000841 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(to_i915(vm->dev)));
Ben Widawsky459108b2013-11-02 21:07:23 -0700842
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200843 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
844 if (WARN_ON(!pml4->pdps[pml4e]))
845 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700846
Michał Winiarski2ce51792016-10-13 14:02:42 +0200847 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
848 __clear_bit(pml4e, pml4->used_pml4es);
849 pml4e_vaddr = kmap_px(pml4);
850 pml4e_vaddr[pml4e] = scratch_pml4e;
851 kunmap_px(ppgtt, pml4e_vaddr);
Zhi Wanga18dbba2016-11-29 14:55:16 +0800852 free_pdp(dev_priv, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200853 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700854 }
855}
856
Michel Thierryf9b5b782015-07-30 11:02:49 +0100857static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200858 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700859{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300860 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100861
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000862 if (USES_FULL_48BIT_PPGTT(to_i915(vm->dev)))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200863 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
864 else
865 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100866}
867
868static void
869gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
870 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100871 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100872 uint64_t start,
873 enum i915_cache_level cache_level)
874{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300875 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000876 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100877 unsigned pdpe = gen8_pdpe_index(start);
878 unsigned pde = gen8_pde_index(start);
879 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700880
Chris Wilson6f1cc992013-12-31 15:50:31 +0000881 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700882
Michel Thierry3387d432015-08-03 09:52:47 +0100883 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000884 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100885 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100886 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300887 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000888 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800889
890 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100891 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200892 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000893 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300894 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000895 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000896 if (++pde == I915_PDES) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000897 if (++pdpe == I915_PDPES_PER_PDP(to_i915(vm->dev)))
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100898 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800899 pde = 0;
900 }
901 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700902 }
903 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300904
905 if (pt_vaddr)
906 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700907}
908
Michel Thierryf9b5b782015-07-30 11:02:49 +0100909static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
910 struct sg_table *pages,
911 uint64_t start,
912 enum i915_cache_level cache_level,
913 u32 unused)
914{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300915 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100916 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100917
Michel Thierry3387d432015-08-03 09:52:47 +0100918 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100919
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000920 if (!USES_FULL_48BIT_PPGTT(to_i915(vm->dev))) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100921 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
922 cache_level);
923 } else {
924 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000925 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100926 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
927
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000928 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100929 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
930 start, cache_level);
931 }
932 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100933}
934
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000935static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
Michel Thierryf37c0502015-06-10 17:46:39 +0100936 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800937{
938 int i;
939
Mika Kuoppala567047b2015-06-25 18:35:12 +0300940 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800941 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800942
Michel Thierry33c88192015-04-08 12:13:33 +0100943 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000944 if (WARN_ON(!pd->page_table[i]))
945 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800946
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000947 free_pt(dev_priv, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000948 pd->page_table[i] = NULL;
949 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000950}
951
Mika Kuoppala8776f022015-06-30 18:16:40 +0300952static int gen8_init_scratch(struct i915_address_space *vm)
953{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000954 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Matthew Auld64c050d2016-04-27 13:19:25 +0100955 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300956
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000957 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100958 if (ret)
959 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300960
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000961 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300962 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100963 ret = PTR_ERR(vm->scratch_pt);
964 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300965 }
966
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000967 vm->scratch_pd = alloc_pd(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300968 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100969 ret = PTR_ERR(vm->scratch_pd);
970 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300971 }
972
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000973 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
974 vm->scratch_pdp = alloc_pdp(dev_priv);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100975 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100976 ret = PTR_ERR(vm->scratch_pdp);
977 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100978 }
979 }
980
Mika Kuoppala8776f022015-06-30 18:16:40 +0300981 gen8_initialize_pt(vm, vm->scratch_pt);
982 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000983 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100984 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300985
986 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100987
988free_pd:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000989 free_pd(dev_priv, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +0100990free_pt:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000991 free_pt(dev_priv, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +0100992free_scratch_page:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000993 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +0100994
995 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300996}
997
Zhiyuan Lv650da342015-08-28 15:41:18 +0800998static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
999{
1000 enum vgt_g2v_type msg;
Matthew Aulddf285642016-04-22 12:09:25 +01001001 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Zhiyuan Lv650da342015-08-28 15:41:18 +08001002 int i;
1003
Matthew Aulddf285642016-04-22 12:09:25 +01001004 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001005 u64 daddr = px_dma(&ppgtt->pml4);
1006
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001007 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1008 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001009
1010 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1011 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1012 } else {
1013 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1014 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1015
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001016 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1017 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001018 }
1019
1020 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1021 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1022 }
1023
1024 I915_WRITE(vgtif_reg(g2v_notify), msg);
1025
1026 return 0;
1027}
1028
Mika Kuoppala8776f022015-06-30 18:16:40 +03001029static void gen8_free_scratch(struct i915_address_space *vm)
1030{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001031 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001032
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001033 if (USES_FULL_48BIT_PPGTT(dev_priv))
1034 free_pdp(dev_priv, vm->scratch_pdp);
1035 free_pd(dev_priv, vm->scratch_pd);
1036 free_pt(dev_priv, vm->scratch_pt);
1037 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001038}
1039
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001040static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
Michel Thierry762d9932015-07-30 11:05:29 +01001041 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001042{
1043 int i;
1044
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001045 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001046 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001047 continue;
1048
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001049 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1050 free_pd(dev_priv, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001051 }
Michel Thierry69876be2015-04-08 12:13:27 +01001052
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001053 free_pdp(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001054}
1055
1056static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1057{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001058 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Michel Thierry762d9932015-07-30 11:05:29 +01001059 int i;
1060
1061 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1062 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1063 continue;
1064
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001065 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001066 }
1067
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001068 cleanup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001069}
1070
1071static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1072{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001073 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001074 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001075
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001076 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001077 gen8_ppgtt_notify_vgt(ppgtt, false);
1078
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001079 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1080 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001081 else
1082 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001083
Mika Kuoppala8776f022015-06-30 18:16:40 +03001084 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001085}
1086
Michel Thierryd7b26332015-04-08 12:13:34 +01001087/**
1088 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001089 * @vm: Master vm structure.
1090 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001091 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001092 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001093 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1094 * caller to free on error.
1095 *
1096 * Allocate the required number of page tables. Extremely similar to
1097 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1098 * the page directory boundary (instead of the page directory pointer). That
1099 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1100 * possible, and likely that the caller will need to use multiple calls of this
1101 * function to achieve the appropriate allocation.
1102 *
1103 * Return: 0 if success; negative error code otherwise.
1104 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001105static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001106 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001107 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001108 uint64_t length,
1109 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001110{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001111 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001112 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001113 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001114
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001115 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001116 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001117 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001118 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001119 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001120 continue;
1121 }
1122
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001123 pt = alloc_pt(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001124 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001125 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001126
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001127 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001128 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001129 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001130 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001131 }
1132
1133 return 0;
1134
1135unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001136 for_each_set_bit(pde, new_pts, I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001137 free_pt(dev_priv, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001138
1139 return -ENOMEM;
1140}
1141
Michel Thierryd7b26332015-04-08 12:13:34 +01001142/**
1143 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001144 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001145 * @pdp: Page directory pointer for this address range.
1146 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001147 * @length: Size of the allocations.
1148 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001149 * caller to free on error.
1150 *
1151 * Allocate the required number of page directories starting at the pde index of
1152 * @start, and ending at the pde index @start + @length. This function will skip
1153 * over already allocated page directories within the range, and only allocate
1154 * new ones, setting the appropriate pointer within the pdp as well as the
1155 * correct position in the bitmap @new_pds.
1156 *
1157 * The function will only allocate the pages within the range for a give page
1158 * directory pointer. In other words, if @start + @length straddles a virtually
1159 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1160 * required by the caller, This is not currently possible, and the BUG in the
1161 * code will prevent it.
1162 *
1163 * Return: 0 if success; negative error code otherwise.
1164 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001165static int
1166gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1167 struct i915_page_directory_pointer *pdp,
1168 uint64_t start,
1169 uint64_t length,
1170 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001171{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001172 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001173 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001174 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001175 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001176
Michel Thierry6ac18502015-07-29 17:23:46 +01001177 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001178
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001179 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001180 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001181 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001182
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001183 pd = alloc_pd(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001184 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001185 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001186
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001187 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001188 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001189 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001190 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001191 }
1192
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001193 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001194
1195unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001196 for_each_set_bit(pdpe, new_pds, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001197 free_pd(dev_priv, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001198
1199 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001200}
1201
Michel Thierry762d9932015-07-30 11:05:29 +01001202/**
1203 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1204 * @vm: Master vm structure.
1205 * @pml4: Page map level 4 for this address range.
1206 * @start: Starting virtual address to begin allocations.
1207 * @length: Size of the allocations.
1208 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1209 * caller to free on error.
1210 *
1211 * Allocate the required number of page directory pointers. Extremely similar to
1212 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1213 * The main difference is here we are limited by the pml4 boundary (instead of
1214 * the page directory pointer).
1215 *
1216 * Return: 0 if success; negative error code otherwise.
1217 */
1218static int
1219gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1220 struct i915_pml4 *pml4,
1221 uint64_t start,
1222 uint64_t length,
1223 unsigned long *new_pdps)
1224{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001225 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierry762d9932015-07-30 11:05:29 +01001226 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001227 uint32_t pml4e;
1228
1229 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1230
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001231 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001232 if (!test_bit(pml4e, pml4->used_pml4es)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001233 pdp = alloc_pdp(dev_priv);
Michel Thierry762d9932015-07-30 11:05:29 +01001234 if (IS_ERR(pdp))
1235 goto unwind_out;
1236
Michel Thierry69ab76f2015-07-29 17:23:55 +01001237 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001238 pml4->pdps[pml4e] = pdp;
1239 __set_bit(pml4e, new_pdps);
1240 trace_i915_page_directory_pointer_entry_alloc(vm,
1241 pml4e,
1242 start,
1243 GEN8_PML4E_SHIFT);
1244 }
1245 }
1246
1247 return 0;
1248
1249unwind_out:
1250 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001251 free_pdp(dev_priv, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001252
1253 return -ENOMEM;
1254}
1255
Michel Thierryd7b26332015-04-08 12:13:34 +01001256static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001257free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001258{
Michel Thierryd7b26332015-04-08 12:13:34 +01001259 kfree(new_pts);
1260 kfree(new_pds);
1261}
1262
1263/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1264 * of these are based on the number of PDPEs in the system.
1265 */
1266static
1267int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001268 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001269 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001270{
Michel Thierryd7b26332015-04-08 12:13:34 +01001271 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001272 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001273
Michał Winiarski3a41a052015-09-03 19:22:18 +02001274 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001275 if (!pds)
1276 return -ENOMEM;
1277
Michał Winiarski3a41a052015-09-03 19:22:18 +02001278 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1279 GFP_TEMPORARY);
1280 if (!pts)
1281 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001282
1283 *new_pds = pds;
1284 *new_pts = pts;
1285
1286 return 0;
1287
1288err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001289 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001290 return -ENOMEM;
1291}
1292
Michel Thierry762d9932015-07-30 11:05:29 +01001293static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1294 struct i915_page_directory_pointer *pdp,
1295 uint64_t start,
1296 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001297{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001298 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001299 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001300 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierry5441f0c2015-04-08 12:13:28 +01001301 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001302 const uint64_t orig_start = start;
1303 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001304 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001305 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001306 int ret;
1307
Michel Thierryd7b26332015-04-08 12:13:34 +01001308 /* Wrap is never okay since we can only represent 48b, and we don't
1309 * actually use the other side of the canonical address space.
1310 */
1311 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001312 return -ENODEV;
1313
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001314 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001315 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001316
Michel Thierry6ac18502015-07-29 17:23:46 +01001317 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001318 if (ret)
1319 return ret;
1320
Michel Thierryd7b26332015-04-08 12:13:34 +01001321 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001322 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1323 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001324 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001325 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001326 return ret;
1327 }
1328
1329 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001330 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001331 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001332 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001333 if (ret)
1334 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001335 }
1336
Michel Thierry33c88192015-04-08 12:13:33 +01001337 start = orig_start;
1338 length = orig_length;
1339
Michel Thierryd7b26332015-04-08 12:13:34 +01001340 /* Allocations have completed successfully, so set the bitmaps, and do
1341 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001342 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001343 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001344 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001345 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001346 uint64_t pd_start = start;
1347 uint32_t pde;
1348
Michel Thierryd7b26332015-04-08 12:13:34 +01001349 /* Every pd should be allocated, we just did that above. */
1350 WARN_ON(!pd);
1351
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001352 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001353 /* Same reasoning as pd */
1354 WARN_ON(!pt);
1355 WARN_ON(!pd_len);
1356 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1357
1358 /* Set our used ptes within the page table */
1359 bitmap_set(pt->used_ptes,
1360 gen8_pte_index(pd_start),
1361 gen8_pte_count(pd_start, pd_len));
1362
1363 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001364 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001365
1366 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001367 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1368 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001369 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1370 gen8_pte_index(start),
1371 gen8_pte_count(start, length),
1372 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001373
1374 /* NB: We haven't yet mapped ptes to pages. At this
1375 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001376 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001377
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001378 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001379 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001380 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001381 }
1382
Michał Winiarski3a41a052015-09-03 19:22:18 +02001383 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001384 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001385 return 0;
1386
1387err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001388 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001389 unsigned long temp;
1390
Michał Winiarski3a41a052015-09-03 19:22:18 +02001391 for_each_set_bit(temp, new_page_tables + pdpe *
1392 BITS_TO_LONGS(I915_PDES), I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001393 free_pt(dev_priv,
1394 pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001395 }
1396
Michel Thierry6ac18502015-07-29 17:23:46 +01001397 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001398 free_pd(dev_priv, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001399
Michał Winiarski3a41a052015-09-03 19:22:18 +02001400 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001401 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001402 return ret;
1403}
1404
Michel Thierry762d9932015-07-30 11:05:29 +01001405static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1406 struct i915_pml4 *pml4,
1407 uint64_t start,
1408 uint64_t length)
1409{
1410 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001411 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001412 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001413 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001414 int ret = 0;
1415
1416 /* Do the pml4 allocations first, so we don't need to track the newly
1417 * allocated tables below the pdp */
1418 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1419
1420 /* The pagedirectory and pagetable allocations are done in the shared 3
1421 * and 4 level code. Just allocate the pdps.
1422 */
1423 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1424 new_pdps);
1425 if (ret)
1426 return ret;
1427
1428 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1429 "The allocation has spanned more than 512GB. "
1430 "It is highly likely this is incorrect.");
1431
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001432 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001433 WARN_ON(!pdp);
1434
1435 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1436 if (ret)
1437 goto err_out;
1438
1439 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1440 }
1441
1442 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1443 GEN8_PML4ES_PER_PML4);
1444
1445 return 0;
1446
1447err_out:
1448 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001449 gen8_ppgtt_cleanup_3lvl(to_i915(vm->dev), pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001450
1451 return ret;
1452}
1453
1454static int gen8_alloc_va_range(struct i915_address_space *vm,
1455 uint64_t start, uint64_t length)
1456{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001457 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001458
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001459 if (USES_FULL_48BIT_PPGTT(to_i915(vm->dev)))
Michel Thierry762d9932015-07-30 11:05:29 +01001460 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1461 else
1462 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1463}
1464
Michel Thierryea91e402015-07-29 17:23:57 +01001465static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1466 uint64_t start, uint64_t length,
1467 gen8_pte_t scratch_pte,
1468 struct seq_file *m)
1469{
1470 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001471 uint32_t pdpe;
1472
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001473 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001474 struct i915_page_table *pt;
1475 uint64_t pd_len = length;
1476 uint64_t pd_start = start;
1477 uint32_t pde;
1478
1479 if (!test_bit(pdpe, pdp->used_pdpes))
1480 continue;
1481
1482 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001483 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001484 uint32_t pte;
1485 gen8_pte_t *pt_vaddr;
1486
1487 if (!test_bit(pde, pd->used_pdes))
1488 continue;
1489
1490 pt_vaddr = kmap_px(pt);
1491 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1492 uint64_t va =
1493 (pdpe << GEN8_PDPE_SHIFT) |
1494 (pde << GEN8_PDE_SHIFT) |
1495 (pte << GEN8_PTE_SHIFT);
1496 int i;
1497 bool found = false;
1498
1499 for (i = 0; i < 4; i++)
1500 if (pt_vaddr[pte + i] != scratch_pte)
1501 found = true;
1502 if (!found)
1503 continue;
1504
1505 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1506 for (i = 0; i < 4; i++) {
1507 if (pt_vaddr[pte + i] != scratch_pte)
1508 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1509 else
1510 seq_puts(m, " SCRATCH ");
1511 }
1512 seq_puts(m, "\n");
1513 }
1514 /* don't use kunmap_px, it could trigger
1515 * an unnecessary flush.
1516 */
1517 kunmap_atomic(pt_vaddr);
1518 }
1519 }
1520}
1521
1522static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1523{
1524 struct i915_address_space *vm = &ppgtt->base;
1525 uint64_t start = ppgtt->base.start;
1526 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001527 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001528 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001529
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001530 if (!USES_FULL_48BIT_PPGTT(to_i915(vm->dev))) {
Michel Thierryea91e402015-07-29 17:23:57 +01001531 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1532 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001533 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001534 struct i915_pml4 *pml4 = &ppgtt->pml4;
1535 struct i915_page_directory_pointer *pdp;
1536
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001537 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001538 if (!test_bit(pml4e, pml4->used_pml4es))
1539 continue;
1540
1541 seq_printf(m, " PML4E #%llu\n", pml4e);
1542 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1543 }
1544 }
1545}
1546
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001547static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1548{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001549 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001550 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001551 int ret;
1552
1553 /* We allocate temp bitmap for page tables for no gain
1554 * but as this is for init only, lets keep the things simple
1555 */
1556 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1557 if (ret)
1558 return ret;
1559
1560 /* Allocate for all pdps regardless of how the ppgtt
1561 * was defined.
1562 */
1563 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1564 0, 1ULL << 32,
1565 new_page_dirs);
1566 if (!ret)
1567 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1568
Michał Winiarski3a41a052015-09-03 19:22:18 +02001569 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001570
1571 return ret;
1572}
1573
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001574/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001575 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1576 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1577 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1578 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001579 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001580 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001581static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001582{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001583 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001584 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001585
Mika Kuoppala8776f022015-06-30 18:16:40 +03001586 ret = gen8_init_scratch(&ppgtt->base);
1587 if (ret)
1588 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001589
Michel Thierryd7b26332015-04-08 12:13:34 +01001590 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001591 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001592 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001593 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001594 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001595 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1596 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001597 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001598
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001599 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1600 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001601 if (ret)
1602 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001603
Michel Thierry69ab76f2015-07-29 17:23:55 +01001604 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1605
Michel Thierry762d9932015-07-30 11:05:29 +01001606 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001607 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001608 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001609 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001610 if (ret)
1611 goto free_scratch;
1612
1613 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001614 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001615 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1616 0, 0,
1617 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001618
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001619 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001620 ret = gen8_preallocate_top_level_pdps(ppgtt);
1621 if (ret)
1622 goto free_scratch;
1623 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001624 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001625
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001626 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001627 gen8_ppgtt_notify_vgt(ppgtt, true);
1628
Michel Thierryd7b26332015-04-08 12:13:34 +01001629 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001630
1631free_scratch:
1632 gen8_free_scratch(&ppgtt->base);
1633 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001634}
1635
Ben Widawsky87d60b62013-12-06 14:11:29 -08001636static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1637{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001638 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001639 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001640 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001641 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001642 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001643 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001644
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001645 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001646 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001647
Dave Gordon731f74c2016-06-24 19:37:46 +01001648 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001649 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001650 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001651 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001652 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001653 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1654
1655 if (pd_entry != expected)
1656 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1657 pde,
1658 pd_entry,
1659 expected);
1660 seq_printf(m, "\tPDE: %x\n", pd_entry);
1661
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001662 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1663
Michel Thierry07749ef2015-03-16 16:00:54 +00001664 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001665 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001666 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001667 (pte * PAGE_SIZE);
1668 int i;
1669 bool found = false;
1670 for (i = 0; i < 4; i++)
1671 if (pt_vaddr[pte + i] != scratch_pte)
1672 found = true;
1673 if (!found)
1674 continue;
1675
1676 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1677 for (i = 0; i < 4; i++) {
1678 if (pt_vaddr[pte + i] != scratch_pte)
1679 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1680 else
1681 seq_puts(m, " SCRATCH ");
1682 }
1683 seq_puts(m, "\n");
1684 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001685 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001686 }
1687}
1688
Ben Widawsky678d96f2015-03-16 16:00:56 +00001689/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001690static void gen6_write_pde(struct i915_page_directory *pd,
1691 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001692{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001693 /* Caller needs to make sure the write completes if necessary */
1694 struct i915_hw_ppgtt *ppgtt =
1695 container_of(pd, struct i915_hw_ppgtt, pd);
1696 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001697
Mika Kuoppala567047b2015-06-25 18:35:12 +03001698 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001699 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001700
Ben Widawsky678d96f2015-03-16 16:00:56 +00001701 writel(pd_entry, ppgtt->pd_addr + pde);
1702}
Ben Widawsky61973492013-04-08 18:43:54 -07001703
Ben Widawsky678d96f2015-03-16 16:00:56 +00001704/* Write all the page tables found in the ppgtt structure to incrementing page
1705 * directories. */
1706static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001707 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001708 uint32_t start, uint32_t length)
1709{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001710 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001711 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001712 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001713
Dave Gordon731f74c2016-06-24 19:37:46 +01001714 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001715 gen6_write_pde(pd, pde, pt);
1716
1717 /* Make sure write is complete before other code can use this page
1718 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001719 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001720}
1721
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001722static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001723{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001724 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001725
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001726 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001727}
Ben Widawsky61973492013-04-08 18:43:54 -07001728
Ben Widawsky90252e52013-12-06 14:11:12 -08001729static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001730 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001731{
Chris Wilson7e37f882016-08-02 22:50:21 +01001732 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001733 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001734 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001735
Ben Widawsky90252e52013-12-06 14:11:12 -08001736 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001737 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001738 if (ret)
1739 return ret;
1740
John Harrison5fb9de12015-05-29 17:44:07 +01001741 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001742 if (ret)
1743 return ret;
1744
Chris Wilsonb5321f32016-08-02 22:50:18 +01001745 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1746 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1747 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1748 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1749 intel_ring_emit(ring, get_pd_offset(ppgtt));
1750 intel_ring_emit(ring, MI_NOOP);
1751 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001752
1753 return 0;
1754}
1755
Ben Widawsky48a10382013-12-06 14:11:11 -08001756static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001757 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001758{
Chris Wilson7e37f882016-08-02 22:50:21 +01001759 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001760 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001761 int ret;
1762
Ben Widawsky48a10382013-12-06 14:11:11 -08001763 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001764 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001765 if (ret)
1766 return ret;
1767
John Harrison5fb9de12015-05-29 17:44:07 +01001768 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001769 if (ret)
1770 return ret;
1771
Chris Wilsonb5321f32016-08-02 22:50:18 +01001772 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1773 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1774 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1775 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1776 intel_ring_emit(ring, get_pd_offset(ppgtt));
1777 intel_ring_emit(ring, MI_NOOP);
1778 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001779
Ben Widawsky90252e52013-12-06 14:11:12 -08001780 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001781 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001782 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001783 if (ret)
1784 return ret;
1785 }
1786
Ben Widawsky48a10382013-12-06 14:11:11 -08001787 return 0;
1788}
1789
Ben Widawskyeeb94882013-12-06 14:11:10 -08001790static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001791 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001792{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001793 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001794 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001795
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001796 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1797 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001798 return 0;
1799}
1800
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001801static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001802{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001803 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301804 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001805
Akash Goel3b3f1652016-10-13 22:44:48 +05301806 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001807 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1808 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001809 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001810 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001811 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001812}
1813
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001814static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001815{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001816 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001817 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301818 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001819
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001820 ecobits = I915_READ(GAC_ECO_BITS);
1821 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1822
1823 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001824 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001825 ecochk |= ECOCHK_PPGTT_WB_HSW;
1826 } else {
1827 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1828 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1829 }
1830 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001831
Akash Goel3b3f1652016-10-13 22:44:48 +05301832 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001833 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001834 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001835 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001836 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001837}
1838
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001839static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001840{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001841 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001842
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001843 ecobits = I915_READ(GAC_ECO_BITS);
1844 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1845 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001846
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001847 gab_ctl = I915_READ(GAB_CTL);
1848 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001849
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001850 ecochk = I915_READ(GAM_ECOCHK);
1851 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001852
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001853 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001854}
1855
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001856/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001857static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001858 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001859 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001860{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001861 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001862 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001863 unsigned first_entry = start >> PAGE_SHIFT;
1864 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001865 unsigned act_pt = first_entry / GEN6_PTES;
1866 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001867 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001868
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001869 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001870 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001871
Daniel Vetter7bddb012012-02-09 17:15:47 +01001872 while (num_entries) {
1873 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001874 if (last_pte > GEN6_PTES)
1875 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001876
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001877 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001878
1879 for (i = first_pte; i < last_pte; i++)
1880 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001881
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001882 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001883
Daniel Vetter7bddb012012-02-09 17:15:47 +01001884 num_entries -= last_pte - first_pte;
1885 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001886 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001887 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001888}
1889
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001890static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001891 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001892 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301893 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001894{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001895 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001896 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001897 unsigned act_pt = first_entry / GEN6_PTES;
1898 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001899 gen6_pte_t *pt_vaddr = NULL;
1900 struct sgt_iter sgt_iter;
1901 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001902
Dave Gordon85d12252016-05-20 11:54:06 +01001903 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001904 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001905 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001906
Chris Wilsoncc797142013-12-31 15:50:30 +00001907 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001908 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301909
Michel Thierry07749ef2015-03-16 16:00:54 +00001910 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001911 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001912 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001913 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001914 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001915 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001916 }
Dave Gordon85d12252016-05-20 11:54:06 +01001917
Chris Wilsoncc797142013-12-31 15:50:30 +00001918 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001919 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001920}
1921
Ben Widawsky678d96f2015-03-16 16:00:56 +00001922static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001923 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001924{
Michel Thierry4933d512015-03-24 15:46:22 +00001925 DECLARE_BITMAP(new_page_tables, I915_PDES);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001926 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001927 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001928 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001929 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001930 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001931 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001932 int ret;
1933
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001934 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1935 return -ENODEV;
1936
1937 start = start_save = start_in;
1938 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001939
1940 bitmap_zero(new_page_tables, I915_PDES);
1941
1942 /* The allocation is done in two stages so that we can bail out with
1943 * minimal amount of pain. The first stage finds new page tables that
1944 * need allocation. The second stage marks use ptes within the page
1945 * tables.
1946 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001947 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001948 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001949 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1950 continue;
1951 }
1952
1953 /* We've already allocated a page table */
1954 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1955
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001956 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001957 if (IS_ERR(pt)) {
1958 ret = PTR_ERR(pt);
1959 goto unwind_out;
1960 }
1961
1962 gen6_initialize_pt(vm, pt);
1963
1964 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001965 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001966 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001967 }
1968
1969 start = start_save;
1970 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001971
Dave Gordon731f74c2016-06-24 19:37:46 +01001972 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001973 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1974
1975 bitmap_zero(tmp_bitmap, GEN6_PTES);
1976 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1977 gen6_pte_count(start, length));
1978
Mika Kuoppala966082c2015-06-25 18:35:19 +03001979 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001980 gen6_write_pde(&ppgtt->pd, pde, pt);
1981
Michel Thierry72744cb2015-03-24 15:46:23 +00001982 trace_i915_page_table_entry_map(vm, pde, pt,
1983 gen6_pte_index(start),
1984 gen6_pte_count(start, length),
1985 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001986 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001987 GEN6_PTES);
1988 }
1989
Michel Thierry4933d512015-03-24 15:46:22 +00001990 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1991
1992 /* Make sure write is complete before other code can use this page
1993 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001994 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001995
Ben Widawsky563222a2015-03-19 12:53:28 +00001996 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001997 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001998
1999unwind_out:
2000 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002001 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002002
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002003 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002004 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002005 }
2006
2007 mark_tlbs_dirty(ppgtt);
2008 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002009}
2010
Mika Kuoppala8776f022015-06-30 18:16:40 +03002011static int gen6_init_scratch(struct i915_address_space *vm)
2012{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002013 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002014 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002015
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002016 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002017 if (ret)
2018 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002019
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002020 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002021 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002022 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002023 return PTR_ERR(vm->scratch_pt);
2024 }
2025
2026 gen6_initialize_pt(vm, vm->scratch_pt);
2027
2028 return 0;
2029}
2030
2031static void gen6_free_scratch(struct i915_address_space *vm)
2032{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002033 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002034
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002035 free_pt(dev_priv, vm->scratch_pt);
2036 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002037}
2038
Daniel Vetter061dd492015-04-14 17:35:13 +02002039static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002040{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002041 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002042 struct i915_page_directory *pd = &ppgtt->pd;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002043 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Michel Thierry09942c62015-04-08 12:13:30 +01002044 struct i915_page_table *pt;
2045 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002046
Daniel Vetter061dd492015-04-14 17:35:13 +02002047 drm_mm_remove_node(&ppgtt->node);
2048
Dave Gordon731f74c2016-06-24 19:37:46 +01002049 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002050 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002051 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002052
Mika Kuoppala8776f022015-06-30 18:16:40 +03002053 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002054}
2055
Ben Widawskyb1465202014-02-19 22:05:49 -08002056static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002057{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002058 struct i915_address_space *vm = &ppgtt->base;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002059 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002060 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002061 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08002062 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002063
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002064 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2065 * allocator works in address space sizes, so it's multiplied by page
2066 * size. We allocate at the top of the GTT to avoid fragmentation.
2067 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002068 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002069
Mika Kuoppala8776f022015-06-30 18:16:40 +03002070 ret = gen6_init_scratch(vm);
2071 if (ret)
2072 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002073
Ben Widawskye3cc1992013-12-06 14:11:08 -08002074alloc:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002075 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002076 &ppgtt->node, GEN6_PD_SIZE,
2077 GEN6_PD_ALIGN, 0,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002078 0, ggtt->base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07002079 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002080 if (ret == -ENOSPC && !retried) {
Chris Wilsone522ac22016-08-04 16:32:18 +01002081 ret = i915_gem_evict_something(&ggtt->base,
Ben Widawskye3cc1992013-12-06 14:11:08 -08002082 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02002083 I915_CACHE_NONE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002084 0, ggtt->base.total,
Chris Wilsond23db882014-05-23 08:48:08 +02002085 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002086 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002087 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002088
2089 retried = true;
2090 goto alloc;
2091 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002092
Ben Widawskyc8c26622015-01-22 17:01:25 +00002093 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002094 goto err_out;
2095
Ben Widawskyc8c26622015-01-22 17:01:25 +00002096
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002097 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002098 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002099
Ben Widawskyc8c26622015-01-22 17:01:25 +00002100 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002101
2102err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002103 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002104 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002105}
2106
Ben Widawskyb1465202014-02-19 22:05:49 -08002107static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2108{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002109 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002110}
2111
Michel Thierry4933d512015-03-24 15:46:22 +00002112static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2113 uint64_t start, uint64_t length)
2114{
Michel Thierryec565b32015-04-08 12:13:23 +01002115 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002116 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002117
Dave Gordon731f74c2016-06-24 19:37:46 +01002118 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002119 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002120}
2121
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002122static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002123{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002124 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002125 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002126 int ret;
2127
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002128 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002129 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002130 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002131 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002132 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002133 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002134 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002135 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002136 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002137
2138 ret = gen6_ppgtt_alloc(ppgtt);
2139 if (ret)
2140 return ret;
2141
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002142 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002143 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2144 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002145 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2146 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002147 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002148 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002149 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002150 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002151
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002152 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002153 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002154
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002155 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002156 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002157
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002158 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002159
Ben Widawsky678d96f2015-03-16 16:00:56 +00002160 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2161
Thierry Reding440fd522015-01-23 09:05:06 +01002162 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002163 ppgtt->node.size >> 20,
2164 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002165
Daniel Vetterfa76da32014-08-06 20:19:54 +02002166 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002167 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002168
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002169 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002170}
2171
Chris Wilson2bfa9962016-08-04 07:52:25 +01002172static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2173 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002174{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002175 ppgtt->base.dev = &dev_priv->drm;
Daniel Vetter3440d262013-01-24 13:49:56 -08002176
Chris Wilson2bfa9962016-08-04 07:52:25 +01002177 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002178 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002179 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002180 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002181}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002182
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002183static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002184 struct drm_i915_private *dev_priv,
2185 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002186{
Chris Wilson80b204b2016-10-28 13:58:58 +01002187 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002188 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002189 INIT_LIST_HEAD(&vm->active_list);
2190 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002191 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002192 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2193}
2194
Matthew Aulded9724d2016-11-17 21:04:10 +00002195static void i915_address_space_fini(struct i915_address_space *vm)
2196{
2197 i915_gem_timeline_fini(&vm->timeline);
2198 drm_mm_takedown(&vm->mm);
2199 list_del(&vm->global_link);
2200}
2201
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002202static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002203{
Tim Gored5165eb2016-02-04 11:49:34 +00002204 /* This function is for gtt related workarounds. This function is
2205 * called on driver load and after a GPU reset, so you can place
2206 * workarounds here even if they get overwritten by GPU reset.
2207 */
2208 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002209 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002210 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002211 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002212 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002213 else if (IS_SKYLAKE(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002214 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002215 else if (IS_BROXTON(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002216 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2217}
2218
Chris Wilson2bfa9962016-08-04 07:52:25 +01002219static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2220 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002221 struct drm_i915_file_private *file_priv,
2222 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002223{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002224 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002225
Chris Wilson2bfa9962016-08-04 07:52:25 +01002226 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002227 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002228 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002229 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002230 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002231 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002232
2233 return ret;
2234}
2235
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002236int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002237{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002238 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002239
Thomas Daniel671b50132014-08-20 16:24:50 +01002240 /* In the case of execlists, PPGTT is enabled by the context descriptor
2241 * and the PDPs are contained within the context itself. We don't
2242 * need to do anything here. */
2243 if (i915.enable_execlists)
2244 return 0;
2245
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002246 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002247 return 0;
2248
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002249 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002250 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002251 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002252 gen7_ppgtt_enable(dev_priv);
2253 else if (INTEL_GEN(dev_priv) >= 8)
2254 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002255 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002256 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002257
John Harrison4ad2fd82015-06-18 13:11:20 +01002258 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002259}
John Harrison4ad2fd82015-06-18 13:11:20 +01002260
Daniel Vetter4d884702014-08-06 15:04:47 +02002261struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002262i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002263 struct drm_i915_file_private *fpriv,
2264 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002265{
2266 struct i915_hw_ppgtt *ppgtt;
2267 int ret;
2268
2269 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2270 if (!ppgtt)
2271 return ERR_PTR(-ENOMEM);
2272
Chris Wilson80b204b2016-10-28 13:58:58 +01002273 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002274 if (ret) {
2275 kfree(ppgtt);
2276 return ERR_PTR(ret);
2277 }
2278
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002279 trace_i915_ppgtt_create(&ppgtt->base);
2280
Daniel Vetter4d884702014-08-06 15:04:47 +02002281 return ppgtt;
2282}
2283
Matthew Aulded9724d2016-11-17 21:04:10 +00002284void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002285{
2286 struct i915_hw_ppgtt *ppgtt =
2287 container_of(kref, struct i915_hw_ppgtt, ref);
2288
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002289 trace_i915_ppgtt_release(&ppgtt->base);
2290
Chris Wilson50e046b2016-08-04 07:52:46 +01002291 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002292 WARN_ON(!list_empty(&ppgtt->base.active_list));
2293 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002294 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002295
Matthew Aulded9724d2016-11-17 21:04:10 +00002296 i915_address_space_fini(&ppgtt->base);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002297
Daniel Vetteree960be2014-08-06 15:04:45 +02002298 ppgtt->base.cleanup(&ppgtt->base);
2299 kfree(ppgtt);
2300}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002301
Ben Widawskya81cc002013-01-18 12:30:31 -08002302/* Certain Gen5 chipsets require require idling the GPU before
2303 * unmapping anything from the GTT when VT-d is enabled.
2304 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002305static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002306{
2307#ifdef CONFIG_INTEL_IOMMU
2308 /* Query intel_iommu to see if we need the workaround. Presumably that
2309 * was loaded first.
2310 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002311 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002312 return true;
2313#endif
2314 return false;
2315}
2316
Chris Wilsondc979972016-05-10 14:10:04 +01002317void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002318{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002319 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302320 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002321
Chris Wilsondc979972016-05-10 14:10:04 +01002322 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002323 return;
2324
Akash Goel3b3f1652016-10-13 22:44:48 +05302325 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002326 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002327 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002328 if (fault_reg & RING_FAULT_VALID) {
2329 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002330 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002331 "\tAddress space: %s\n"
2332 "\tSource ID: %d\n"
2333 "\tType: %d\n",
2334 fault_reg & PAGE_MASK,
2335 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2336 RING_FAULT_SRCID(fault_reg),
2337 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002338 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002339 fault_reg & ~RING_FAULT_VALID);
2340 }
2341 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302342
2343 /* Engine specific init may not have been done till this point. */
2344 if (dev_priv->engine[RCS])
2345 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002346}
2347
Chris Wilson91e56492014-09-25 10:13:12 +01002348static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2349{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002350 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002351 intel_gtt_chipset_flush();
2352 } else {
2353 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2354 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2355 }
2356}
2357
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002358void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002359{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002360 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002361
2362 /* Don't bother messing with faults pre GEN6 as we have little
2363 * documentation supporting that it's a good idea.
2364 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002365 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002366 return;
2367
Chris Wilsondc979972016-05-10 14:10:04 +01002368 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002369
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002370 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002371
2372 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002373}
2374
Chris Wilson03ac84f2016-10-28 13:58:36 +01002375int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2376 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002377{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002378 if (dma_map_sg(&obj->base.dev->pdev->dev,
2379 pages->sgl, pages->nents,
2380 PCI_DMA_BIDIRECTIONAL))
2381 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01002382
Chris Wilson03ac84f2016-10-28 13:58:36 +01002383 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002384}
2385
Daniel Vetter2c642b02015-04-14 17:35:26 +02002386static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002387{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002388 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002389}
2390
Chris Wilsond6473f52016-06-10 14:22:59 +05302391static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2392 dma_addr_t addr,
2393 uint64_t offset,
2394 enum i915_cache_level level,
2395 u32 unused)
2396{
2397 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2398 gen8_pte_t __iomem *pte =
2399 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2400 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302401
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002402 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302403
2404 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2405 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302406}
2407
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002408static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2409 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002410 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302411 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002412{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002413 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002414 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002415 struct sgt_iter sgt_iter;
2416 gen8_pte_t __iomem *gtt_entries;
2417 gen8_pte_t gtt_entry;
2418 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002419 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002420
Dave Gordon85d12252016-05-20 11:54:06 +01002421 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2422
2423 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002424 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002425 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002426 }
2427
2428 /*
2429 * XXX: This serves as a posting read to make sure that the PTE has
2430 * actually been updated. There is some concern that even though
2431 * registers and PTEs are within the same BAR that they are potentially
2432 * of NUMA access patterns. Therefore, even with the way we assume
2433 * hardware should work, we must keep this posting read for paranoia.
2434 */
2435 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002436 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002437
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002438 /* This next bit makes the above posting read even more important. We
2439 * want to flush the TLBs only after we're certain all the PTE updates
2440 * have finished.
2441 */
2442 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2443 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002444}
2445
Chris Wilsonc1403302015-11-18 15:19:39 +00002446struct insert_entries {
2447 struct i915_address_space *vm;
2448 struct sg_table *st;
2449 uint64_t start;
2450 enum i915_cache_level level;
2451 u32 flags;
2452};
2453
2454static int gen8_ggtt_insert_entries__cb(void *_arg)
2455{
2456 struct insert_entries *arg = _arg;
2457 gen8_ggtt_insert_entries(arg->vm, arg->st,
2458 arg->start, arg->level, arg->flags);
2459 return 0;
2460}
2461
2462static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2463 struct sg_table *st,
2464 uint64_t start,
2465 enum i915_cache_level level,
2466 u32 flags)
2467{
2468 struct insert_entries arg = { vm, st, start, level, flags };
2469 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2470}
2471
Chris Wilsond6473f52016-06-10 14:22:59 +05302472static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2473 dma_addr_t addr,
2474 uint64_t offset,
2475 enum i915_cache_level level,
2476 u32 flags)
2477{
2478 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2479 gen6_pte_t __iomem *pte =
2480 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2481 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302482
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002483 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302484
2485 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2486 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302487}
2488
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002489/*
2490 * Binds an object into the global gtt with the specified cache level. The object
2491 * will be accessible to the GPU via commands whose operands reference offsets
2492 * within the global GTT as well as accessible by the GPU through the GMADR
2493 * mapped BAR (dev_priv->mm.gtt->gtt).
2494 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002495static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002496 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002497 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302498 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002499{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002500 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002501 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002502 struct sgt_iter sgt_iter;
2503 gen6_pte_t __iomem *gtt_entries;
2504 gen6_pte_t gtt_entry;
2505 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002506 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002507
Dave Gordon85d12252016-05-20 11:54:06 +01002508 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2509
2510 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002511 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002512 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002513 }
2514
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002515 /* XXX: This serves as a posting read to make sure that the PTE has
2516 * actually been updated. There is some concern that even though
2517 * registers and PTEs are within the same BAR that they are potentially
2518 * of NUMA access patterns. Therefore, even with the way we assume
2519 * hardware should work, we must keep this posting read for paranoia.
2520 */
Dave Gordon85d12252016-05-20 11:54:06 +01002521 if (i != 0)
2522 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002523
2524 /* This next bit makes the above posting read even more important. We
2525 * want to flush the TLBs only after we're certain all the PTE updates
2526 * have finished.
2527 */
2528 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2529 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002530}
2531
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002532static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002533 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002534{
2535}
2536
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002537static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002538 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002539{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002540 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002541 unsigned first_entry = start >> PAGE_SHIFT;
2542 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002543 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002544 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2545 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002546 int i;
2547
2548 if (WARN(num_entries > max_entries,
2549 "First entry = %d; Num entries = %d (max=%d)\n",
2550 first_entry, num_entries, max_entries))
2551 num_entries = max_entries;
2552
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002553 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002554 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002555 for (i = 0; i < num_entries; i++)
2556 gen8_set_pte(&gtt_base[i], scratch_pte);
2557 readl(gtt_base);
2558}
2559
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002560static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002561 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002562 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002563{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002564 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002565 unsigned first_entry = start >> PAGE_SHIFT;
2566 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002567 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002568 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2569 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002570 int i;
2571
2572 if (WARN(num_entries > max_entries,
2573 "First entry = %d; Num entries = %d (max=%d)\n",
2574 first_entry, num_entries, max_entries))
2575 num_entries = max_entries;
2576
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002577 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002578 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002579
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002580 for (i = 0; i < num_entries; i++)
2581 iowrite32(scratch_pte, &gtt_base[i]);
2582 readl(gtt_base);
2583}
2584
Chris Wilsond6473f52016-06-10 14:22:59 +05302585static void i915_ggtt_insert_page(struct i915_address_space *vm,
2586 dma_addr_t addr,
2587 uint64_t offset,
2588 enum i915_cache_level cache_level,
2589 u32 unused)
2590{
Chris Wilsond6473f52016-06-10 14:22:59 +05302591 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2592 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302593
2594 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302595}
2596
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002597static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2598 struct sg_table *pages,
2599 uint64_t start,
2600 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002601{
2602 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2603 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2604
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002605 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002606
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002607}
2608
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002609static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002610 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002611 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002612{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002613 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002614}
2615
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002616static int ggtt_bind_vma(struct i915_vma *vma,
2617 enum i915_cache_level cache_level,
2618 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002619{
Chris Wilson9c870d02016-10-24 13:42:15 +01002620 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Daniel Vetter0a878712015-10-15 14:23:01 +02002621 struct drm_i915_gem_object *obj = vma->obj;
2622 u32 pte_flags = 0;
2623 int ret;
2624
2625 ret = i915_get_ggtt_vma_pages(vma);
2626 if (ret)
2627 return ret;
2628
2629 /* Currently applicable only to VLV */
2630 if (obj->gt_ro)
2631 pte_flags |= PTE_READ_ONLY;
2632
Chris Wilson9c870d02016-10-24 13:42:15 +01002633 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002634 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002635 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002636 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002637
2638 /*
2639 * Without aliasing PPGTT there's no difference between
2640 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2641 * upgrade to both bound if we bind either to avoid double-binding.
2642 */
Chris Wilson3272db52016-08-04 16:32:32 +01002643 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002644
2645 return 0;
2646}
2647
2648static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2649 enum i915_cache_level cache_level,
2650 u32 flags)
2651{
Chris Wilson9c870d02016-10-24 13:42:15 +01002652 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Chris Wilson321d1782015-11-20 10:27:18 +00002653 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002654 int ret;
2655
2656 ret = i915_get_ggtt_vma_pages(vma);
2657 if (ret)
2658 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002659
Akash Goel24f3a8c2014-06-17 10:59:42 +05302660 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002661 pte_flags = 0;
2662 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002663 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302664
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002665
Chris Wilson3272db52016-08-04 16:32:32 +01002666 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002667 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002668 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002669 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002670 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002671 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002672 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002673
Chris Wilson3272db52016-08-04 16:32:32 +01002674 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002675 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002676 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002677 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002678 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002679 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002680
2681 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002682}
2683
2684static void ggtt_unbind_vma(struct i915_vma *vma)
2685{
Chris Wilson9c870d02016-10-24 13:42:15 +01002686 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
2687 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002688 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002689
Chris Wilson9c870d02016-10-24 13:42:15 +01002690 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2691 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002692 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002693 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002694 intel_runtime_pm_put(i915);
2695 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002696
Chris Wilson3272db52016-08-04 16:32:32 +01002697 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002698 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002699 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002700}
2701
Chris Wilson03ac84f2016-10-28 13:58:36 +01002702void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2703 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002704{
David Weinehall52a05c32016-08-22 13:32:44 +03002705 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2706 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002707 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002708
Chris Wilson307dc252016-08-05 10:14:12 +01002709 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002710 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002711 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2712 /* Wait a bit, in hopes it avoids the hang */
2713 udelay(10);
2714 }
2715 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002716
Chris Wilson03ac84f2016-10-28 13:58:36 +01002717 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002718}
Daniel Vetter644ec022012-03-26 09:45:40 +02002719
Chris Wilson42d6ab42012-07-26 11:49:32 +01002720static void i915_gtt_color_adjust(struct drm_mm_node *node,
2721 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002722 u64 *start,
2723 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002724{
2725 if (node->color != color)
2726 *start += 4096;
2727
Chris Wilson2a1d7752016-07-26 12:01:51 +01002728 node = list_first_entry_or_null(&node->node_list,
2729 struct drm_mm_node,
2730 node_list);
2731 if (node && node->allocated && node->color != color)
2732 *end -= 4096;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002733}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002734
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002735int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002736{
Ben Widawskye78891c2013-01-25 16:41:04 -08002737 /* Let GEM Manage all of the aperture.
2738 *
2739 * However, leave one page at the end still bound to the scratch page.
2740 * There are a number of places where the hardware apparently prefetches
2741 * past the end of the object, and we've seen multiple hangs with the
2742 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2743 * aperture. One page should be enough to keep any prefetching inside
2744 * of the aperture.
2745 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002746 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002747 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002748 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002749 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002750 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002751
Zhi Wangb02d22a2016-06-16 08:06:59 -04002752 ret = intel_vgt_balloon(dev_priv);
2753 if (ret)
2754 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002755
Chris Wilson95374d72016-10-12 10:05:20 +01002756 /* Reserve a mappable slot for our lockless error capture */
2757 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2758 &ggtt->error_capture,
2759 4096, 0, -1,
2760 0, ggtt->mappable_end,
2761 0, 0);
2762 if (ret)
2763 return ret;
2764
Chris Wilsoned2f3452012-11-15 11:32:19 +00002765 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002766 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002767 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2768 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002769 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002770 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002771 }
2772
2773 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002774 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002775 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002776
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002777 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002778 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002779 if (!ppgtt) {
2780 ret = -ENOMEM;
2781 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002782 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002783
Chris Wilson95374d72016-10-12 10:05:20 +01002784 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2785 if (ret)
2786 goto err_ppgtt;
2787
2788 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002789 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2790 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002791 if (ret)
2792 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002793 }
2794
2795 ppgtt->base.clear_range(&ppgtt->base,
2796 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002797 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002798
Daniel Vetterfa76da32014-08-06 20:19:54 +02002799 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002800 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2801 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002802 }
2803
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002804 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002805
2806err_ppgtt_cleanup:
2807 ppgtt->base.cleanup(&ppgtt->base);
2808err_ppgtt:
2809 kfree(ppgtt);
2810err:
2811 drm_mm_remove_node(&ggtt->error_capture);
2812 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002813}
2814
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002815/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002816 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002817 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002818 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002819void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002820{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002821 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002822
Daniel Vetter70e32542014-08-06 15:04:57 +02002823 if (dev_priv->mm.aliasing_ppgtt) {
2824 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002825 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002826 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002827 }
2828
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002829 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002830
Chris Wilson95374d72016-10-12 10:05:20 +01002831 if (drm_mm_node_allocated(&ggtt->error_capture))
2832 drm_mm_remove_node(&ggtt->error_capture);
2833
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002834 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002835 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002836
Matthew Aulded9724d2016-11-17 21:04:10 +00002837 mutex_lock(&dev_priv->drm.struct_mutex);
2838 i915_address_space_fini(&ggtt->base);
2839 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002840 }
2841
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002842 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002843
2844 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002845 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002846}
Daniel Vetter70e32542014-08-06 15:04:57 +02002847
Daniel Vetter2c642b02015-04-14 17:35:26 +02002848static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002849{
2850 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2851 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2852 return snb_gmch_ctl << 20;
2853}
2854
Daniel Vetter2c642b02015-04-14 17:35:26 +02002855static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002856{
2857 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2858 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2859 if (bdw_gmch_ctl)
2860 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002861
2862#ifdef CONFIG_X86_32
2863 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2864 if (bdw_gmch_ctl > 4)
2865 bdw_gmch_ctl = 4;
2866#endif
2867
Ben Widawsky9459d252013-11-03 16:53:55 -08002868 return bdw_gmch_ctl << 20;
2869}
2870
Daniel Vetter2c642b02015-04-14 17:35:26 +02002871static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002872{
2873 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2874 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2875
2876 if (gmch_ctrl)
2877 return 1 << (20 + gmch_ctrl);
2878
2879 return 0;
2880}
2881
Daniel Vetter2c642b02015-04-14 17:35:26 +02002882static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002883{
2884 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2885 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2886 return snb_gmch_ctl << 25; /* 32 MB units */
2887}
2888
Daniel Vetter2c642b02015-04-14 17:35:26 +02002889static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002890{
2891 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2892 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2893 return bdw_gmch_ctl << 25; /* 32 MB units */
2894}
2895
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002896static size_t chv_get_stolen_size(u16 gmch_ctrl)
2897{
2898 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2899 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2900
2901 /*
2902 * 0x0 to 0x10: 32MB increments starting at 0MB
2903 * 0x11 to 0x16: 4MB increments starting at 8MB
2904 * 0x17 to 0x1d: 4MB increments start at 36MB
2905 */
2906 if (gmch_ctrl < 0x11)
2907 return gmch_ctrl << 25;
2908 else if (gmch_ctrl < 0x17)
2909 return (gmch_ctrl - 0x11 + 2) << 22;
2910 else
2911 return (gmch_ctrl - 0x17 + 9) << 22;
2912}
2913
Damien Lespiau66375012014-01-09 18:02:46 +00002914static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2915{
2916 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2917 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2918
2919 if (gen9_gmch_ctl < 0xf0)
2920 return gen9_gmch_ctl << 25; /* 32 MB units */
2921 else
2922 /* 4MB increments starting at 0xf0 for 4MB */
2923 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2924}
2925
Chris Wilson34c998b2016-08-04 07:52:24 +01002926static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002927{
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002928 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Chris Wilson34c998b2016-08-04 07:52:24 +01002929 struct pci_dev *pdev = ggtt->base.dev->pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002930 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002931 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002932
2933 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002934 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002935
Imre Deak2a073f892015-03-27 13:07:33 +02002936 /*
2937 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2938 * dropped. For WC mappings in general we have 64 byte burst writes
2939 * when the WC buffer is flushed, so we can't use it, but have to
2940 * resort to an uncached mapping. The WC issue is easily caught by the
2941 * readback check when writing GTT PTE entries.
2942 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002943 if (IS_BROXTON(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002944 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002945 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002946 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002947 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002948 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002949 return -ENOMEM;
2950 }
2951
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002952 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002953 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002954 DRM_ERROR("Scratch setup failed\n");
2955 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002956 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002957 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002958 }
2959
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002960 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002961}
2962
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002963/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2964 * bits. When using advanced contexts each context stores its own PAT, but
2965 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002966static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002967{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002968 uint64_t pat;
2969
2970 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2971 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2972 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2973 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2974 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2975 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2976 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2977 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2978
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002979 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002980 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2981 * so RTL will always use the value corresponding to
2982 * pat_sel = 000".
2983 * So let's disable cache for GGTT to avoid screen corruptions.
2984 * MOCS still can be used though.
2985 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2986 * before this patch, i.e. the same uncached + snooping access
2987 * like on gen6/7 seems to be in effect.
2988 * - So this just fixes blitter/render access. Again it looks
2989 * like it's not just uncached access, but uncached + snooping.
2990 * So we can still hold onto all our assumptions wrt cpu
2991 * clflushing on LLC machines.
2992 */
2993 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2994
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002995 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2996 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002997 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2998 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002999}
3000
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003001static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3002{
3003 uint64_t pat;
3004
3005 /*
3006 * Map WB on BDW to snooped on CHV.
3007 *
3008 * Only the snoop bit has meaning for CHV, the rest is
3009 * ignored.
3010 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003011 * The hardware will never snoop for certain types of accesses:
3012 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3013 * - PPGTT page tables
3014 * - some other special cycles
3015 *
3016 * As with BDW, we also need to consider the following for GT accesses:
3017 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3018 * so RTL will always use the value corresponding to
3019 * pat_sel = 000".
3020 * Which means we must set the snoop bit in PAT entry 0
3021 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003022 */
3023 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3024 GEN8_PPAT(1, 0) |
3025 GEN8_PPAT(2, 0) |
3026 GEN8_PPAT(3, 0) |
3027 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3028 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3029 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3030 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3031
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003032 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3033 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003034}
3035
Chris Wilson34c998b2016-08-04 07:52:24 +01003036static void gen6_gmch_remove(struct i915_address_space *vm)
3037{
3038 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3039
3040 iounmap(ggtt->gsm);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003041 cleanup_scratch_page(to_i915(vm->dev), &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003042}
3043
Joonas Lahtinend507d732016-03-18 10:42:58 +02003044static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003045{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003046 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3047 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003048 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003049 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003050
3051 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003052 ggtt->mappable_base = pci_resource_start(pdev, 2);
3053 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003054
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003055 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3056 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003057
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003058 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003059
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003060 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003061 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003062 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003063 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003064 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003065 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003066 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003067 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003068 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003069 }
Ben Widawsky63340132013-11-04 19:32:22 -08003070
Chris Wilson34c998b2016-08-04 07:52:24 +01003071 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003072
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003073 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003074 chv_setup_private_ppat(dev_priv);
3075 else
3076 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003077
Chris Wilson34c998b2016-08-04 07:52:24 +01003078 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003079 ggtt->base.bind_vma = ggtt_bind_vma;
3080 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303081 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003082 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003083 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003084 ggtt->base.clear_range = gen8_ggtt_clear_range;
3085
3086 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3087 if (IS_CHERRYVIEW(dev_priv))
3088 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3089
Chris Wilson34c998b2016-08-04 07:52:24 +01003090 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003091}
3092
Joonas Lahtinend507d732016-03-18 10:42:58 +02003093static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003094{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003095 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3096 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003097 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003098 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003099
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003100 ggtt->mappable_base = pci_resource_start(pdev, 2);
3101 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003102
Ben Widawskybaa09f52013-01-24 13:49:57 -08003103 /* 64/512MB is the current min/max we actually know of, but this is just
3104 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003105 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003106 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003107 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003108 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003109 }
3110
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003111 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3112 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3113 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003114
Joonas Lahtinend507d732016-03-18 10:42:58 +02003115 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003116
Chris Wilson34c998b2016-08-04 07:52:24 +01003117 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3118 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003119
Joonas Lahtinend507d732016-03-18 10:42:58 +02003120 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303121 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003122 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3123 ggtt->base.bind_vma = ggtt_bind_vma;
3124 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003125 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003126
Chris Wilson34c998b2016-08-04 07:52:24 +01003127 if (HAS_EDRAM(dev_priv))
3128 ggtt->base.pte_encode = iris_pte_encode;
3129 else if (IS_HASWELL(dev_priv))
3130 ggtt->base.pte_encode = hsw_pte_encode;
3131 else if (IS_VALLEYVIEW(dev_priv))
3132 ggtt->base.pte_encode = byt_pte_encode;
3133 else if (INTEL_GEN(dev_priv) >= 7)
3134 ggtt->base.pte_encode = ivb_pte_encode;
3135 else
3136 ggtt->base.pte_encode = snb_pte_encode;
3137
3138 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003139}
3140
Chris Wilson34c998b2016-08-04 07:52:24 +01003141static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003142{
Chris Wilson34c998b2016-08-04 07:52:24 +01003143 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003144}
3145
Joonas Lahtinend507d732016-03-18 10:42:58 +02003146static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003147{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003148 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003149 int ret;
3150
Chris Wilson91c8a322016-07-05 10:40:23 +01003151 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003152 if (!ret) {
3153 DRM_ERROR("failed to set up gmch\n");
3154 return -EIO;
3155 }
3156
Joonas Lahtinend507d732016-03-18 10:42:58 +02003157 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3158 &ggtt->mappable_base, &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003159
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003160 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303161 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003162 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3163 ggtt->base.clear_range = i915_ggtt_clear_range;
3164 ggtt->base.bind_vma = ggtt_bind_vma;
3165 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003166 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003167
Joonas Lahtinend507d732016-03-18 10:42:58 +02003168 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003169 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3170
Ben Widawskybaa09f52013-01-24 13:49:57 -08003171 return 0;
3172}
3173
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003174/**
Chris Wilson0088e522016-08-04 07:52:21 +01003175 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003176 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003177 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003178int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003179{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003180 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003181 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003182
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003183 ggtt->base.dev = &dev_priv->drm;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003184
Chris Wilson34c998b2016-08-04 07:52:24 +01003185 if (INTEL_GEN(dev_priv) <= 5)
3186 ret = i915_gmch_probe(ggtt);
3187 else if (INTEL_GEN(dev_priv) < 8)
3188 ret = gen6_gmch_probe(ggtt);
3189 else
3190 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003191 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003192 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003193
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003194 if ((ggtt->base.total - 1) >> 32) {
3195 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003196 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003197 ggtt->base.total >> 20);
3198 ggtt->base.total = 1ULL << 32;
3199 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3200 }
3201
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003202 if (ggtt->mappable_end > ggtt->base.total) {
3203 DRM_ERROR("mappable aperture extends past end of GGTT,"
3204 " aperture=%llx, total=%llx\n",
3205 ggtt->mappable_end, ggtt->base.total);
3206 ggtt->mappable_end = ggtt->base.total;
3207 }
3208
Ben Widawskybaa09f52013-01-24 13:49:57 -08003209 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003210 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003211 ggtt->base.total >> 20);
3212 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3213 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003214#ifdef CONFIG_INTEL_IOMMU
3215 if (intel_iommu_gfx_mapped)
3216 DRM_INFO("VT-d active for gfx access\n");
3217#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003218
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003219 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003220}
3221
3222/**
3223 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003224 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003225 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003226int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003227{
Chris Wilson0088e522016-08-04 07:52:21 +01003228 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3229 int ret;
3230
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003231 INIT_LIST_HEAD(&dev_priv->vm_list);
3232
3233 /* Subtract the guard page before address space initialization to
3234 * shrink the range used by drm_mm.
3235 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003236 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003237 ggtt->base.total -= PAGE_SIZE;
Chris Wilson80b204b2016-10-28 13:58:58 +01003238 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003239 ggtt->base.total += PAGE_SIZE;
3240 if (!HAS_LLC(dev_priv))
3241 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003242 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003243
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003244 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3245 dev_priv->ggtt.mappable_base,
3246 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003247 ret = -EIO;
3248 goto out_gtt_cleanup;
3249 }
3250
3251 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3252
Chris Wilson0088e522016-08-04 07:52:21 +01003253 /*
3254 * Initialise stolen early so that we may reserve preallocated
3255 * objects for the BIOS to KMS transition.
3256 */
Tvrtko Ursulin7ace3d32016-11-16 08:55:35 +00003257 ret = i915_gem_init_stolen(dev_priv);
Chris Wilson0088e522016-08-04 07:52:21 +01003258 if (ret)
3259 goto out_gtt_cleanup;
3260
3261 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003262
3263out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003264 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003265 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003266}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003267
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003268int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003269{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003270 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003271 return -EIO;
3272
3273 return 0;
3274}
3275
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003276void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
Daniel Vetterfa423312015-04-14 17:35:23 +02003277{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003278 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003279 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003280
Chris Wilsondc979972016-05-10 14:10:04 +01003281 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003282
3283 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003284 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003285
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003286 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3287
3288 /* clflush objects bound into the GGTT and rebind them. */
3289 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003290 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003291 bool ggtt_bound = false;
3292 struct i915_vma *vma;
3293
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003294 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003295 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003296 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003297
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003298 if (!i915_vma_unbind(vma))
3299 continue;
3300
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003301 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3302 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003303 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003304 }
3305
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003306 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003307 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003308 }
3309
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003310 ggtt->base.closed = false;
3311
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003312 if (INTEL_GEN(dev_priv) >= 8) {
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01003313 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003314 chv_setup_private_ppat(dev_priv);
3315 else
3316 bdw_setup_private_ppat(dev_priv);
3317
3318 return;
3319 }
3320
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00003321 if (USES_PPGTT(dev_priv)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003322 struct i915_address_space *vm;
3323
Daniel Vetterfa423312015-04-14 17:35:23 +02003324 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3325 /* TODO: Perhaps it shouldn't be gen6 specific */
3326
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003327 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003328
Chris Wilson2bfa9962016-08-04 07:52:25 +01003329 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003330 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003331 else
3332 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003333
3334 gen6_write_page_range(dev_priv, &ppgtt->pd,
3335 0, ppgtt->base.total);
3336 }
3337 }
3338
3339 i915_ggtt_flush(dev_priv);
3340}
3341
Chris Wilson058d88c2016-08-15 10:49:06 +01003342struct i915_vma *
3343i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3344 struct i915_address_space *vm,
3345 const struct i915_ggtt_view *view)
3346{
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003347 struct rb_node *rb;
Chris Wilson058d88c2016-08-15 10:49:06 +01003348
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003349 rb = obj->vma_tree.rb_node;
3350 while (rb) {
3351 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
3352 long cmp;
3353
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003354 cmp = i915_vma_compare(vma, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003355 if (cmp == 0)
Chris Wilson058d88c2016-08-15 10:49:06 +01003356 return vma;
3357
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003358 if (cmp < 0)
3359 rb = rb->rb_right;
3360 else
3361 rb = rb->rb_left;
3362 }
3363
Chris Wilson058d88c2016-08-15 10:49:06 +01003364 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003365}
3366
3367struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003368i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003369 struct i915_address_space *vm,
3370 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003371{
3372 struct i915_vma *vma;
3373
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003374 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +01003375 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3376
3377 vma = i915_gem_obj_to_vma(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003378 if (!vma) {
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003379 vma = i915_vma_create(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003380 GEM_BUG_ON(vma != i915_gem_obj_to_vma(obj, vm, view));
3381 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003382
Chris Wilson3272db52016-08-04 16:32:32 +01003383 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003384 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003385}
3386
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003387static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003388rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003389 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003390 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003391 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003392{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003393 unsigned int column, row;
3394 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003395
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003396 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003397 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003398 for (row = 0; row < height; row++) {
3399 st->nents++;
3400 /* We don't need the pages, but need to initialize
3401 * the entries so the sg list can be happily traversed.
3402 * The only thing we need are DMA addresses.
3403 */
3404 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003405 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003406 sg_dma_len(sg) = PAGE_SIZE;
3407 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003408 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003409 }
3410 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003411
3412 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003413}
3414
3415static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003416intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003417 struct drm_i915_gem_object *obj)
3418{
Dave Gordon85d12252016-05-20 11:54:06 +01003419 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003420 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003421 struct sgt_iter sgt_iter;
3422 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003423 unsigned long i;
3424 dma_addr_t *page_addr_list;
3425 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003426 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003427 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003428
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003429 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003430 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003431 sizeof(dma_addr_t),
3432 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003433 if (!page_addr_list)
3434 return ERR_PTR(ret);
3435
3436 /* Allocate target SG list. */
3437 st = kmalloc(sizeof(*st), GFP_KERNEL);
3438 if (!st)
3439 goto err_st_alloc;
3440
Ville Syrjälä6687c902015-09-15 13:16:41 +03003441 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003442 if (ret)
3443 goto err_sg_alloc;
3444
3445 /* Populate source page list from the object. */
3446 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003447 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003448 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003449
Dave Gordon85d12252016-05-20 11:54:06 +01003450 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003451 st->nents = 0;
3452 sg = st->sgl;
3453
Ville Syrjälä6687c902015-09-15 13:16:41 +03003454 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3455 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3456 rot_info->plane[i].width, rot_info->plane[i].height,
3457 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003458 }
3459
Ville Syrjälä6687c902015-09-15 13:16:41 +03003460 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3461 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003462
3463 drm_free_large(page_addr_list);
3464
3465 return st;
3466
3467err_sg_alloc:
3468 kfree(st);
3469err_st_alloc:
3470 drm_free_large(page_addr_list);
3471
Ville Syrjälä6687c902015-09-15 13:16:41 +03003472 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3473 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3474
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003475 return ERR_PTR(ret);
3476}
3477
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003478static struct sg_table *
3479intel_partial_pages(const struct i915_ggtt_view *view,
3480 struct drm_i915_gem_object *obj)
3481{
3482 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003483 struct scatterlist *sg, *iter;
3484 unsigned int count = view->params.partial.size;
3485 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003486 int ret = -ENOMEM;
3487
3488 st = kmalloc(sizeof(*st), GFP_KERNEL);
3489 if (!st)
3490 goto err_st_alloc;
3491
Chris Wilsond2a84a72016-10-28 13:58:34 +01003492 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003493 if (ret)
3494 goto err_sg_alloc;
3495
Chris Wilsond2a84a72016-10-28 13:58:34 +01003496 iter = i915_gem_object_get_sg(obj,
3497 view->params.partial.offset,
3498 &offset);
3499 GEM_BUG_ON(!iter);
3500
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003501 sg = st->sgl;
3502 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003503 do {
3504 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003505
Chris Wilsond2a84a72016-10-28 13:58:34 +01003506 len = min(iter->length - (offset << PAGE_SHIFT),
3507 count << PAGE_SHIFT);
3508 sg_set_page(sg, NULL, len, 0);
3509 sg_dma_address(sg) =
3510 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3511 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003512
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003513 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003514 count -= len >> PAGE_SHIFT;
3515 if (count == 0) {
3516 sg_mark_end(sg);
3517 return st;
3518 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003519
Chris Wilsond2a84a72016-10-28 13:58:34 +01003520 sg = __sg_next(sg);
3521 iter = __sg_next(iter);
3522 offset = 0;
3523 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003524
3525err_sg_alloc:
3526 kfree(st);
3527err_st_alloc:
3528 return ERR_PTR(ret);
3529}
3530
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003531static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003532i915_get_ggtt_vma_pages(struct i915_vma *vma)
3533{
3534 int ret = 0;
3535
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003536 /* The vma->pages are only valid within the lifespan of the borrowed
3537 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3538 * must be the vma->pages. A simple rule is that vma->pages must only
3539 * be accessed when the obj->mm.pages are pinned.
3540 */
3541 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3542
Chris Wilson247177d2016-08-15 10:48:47 +01003543 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003544 return 0;
3545
3546 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003547 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003548 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003549 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003550 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003551 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003552 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003553 else
3554 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3555 vma->ggtt_view.type);
3556
Chris Wilson247177d2016-08-15 10:48:47 +01003557 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003558 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003559 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003560 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003561 } else if (IS_ERR(vma->pages)) {
3562 ret = PTR_ERR(vma->pages);
3563 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003564 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3565 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003566 }
3567
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003568 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003569}
3570