blob: 88ddca24afdb92b1880e32d67ed62e8cfb68a5eb [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 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200375 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(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) \
Chris Wilson49d73912016-11-29 09:50:08 +0000383 kunmap_page_dma((ppgtt)->base.i915, (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
Chris Wilson49d73912016-11-29 09:50:08 +0000473 fill_px(vm->i915, 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
Chris Wilson49d73912016-11-29 09:50:08 +0000486 fill32_px(vm->i915, 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
Chris Wilson49d73912016-11-29 09:50:08 +0000534 fill_px(vm->i915, 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
Chris Wilson49d73912016-11-29 09:50:08 +0000615 fill_px(vm->i915, 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
Chris Wilson49d73912016-11-29 09:50:08 +0000626 fill_px(vm->i915, 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{
Chris Wilson49d73912016-11-29 09:50:08 +0000713 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->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);
Chris Wilson49d73912016-11-29 09:50:08 +0000776 free_pt(vm->i915, 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 }
Chris Wilson49d73912016-11-29 09:50:08 +0000812 free_pd(vm->i915, 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{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200833 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200834 struct i915_page_directory_pointer *pdp;
835 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200836 gen8_ppgtt_pml4e_t *pml4e_vaddr;
837 gen8_ppgtt_pml4e_t scratch_pml4e =
838 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC);
839
Chris Wilson49d73912016-11-29 09:50:08 +0000840 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
Ben Widawsky459108b2013-11-02 21:07:23 -0700841
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200842 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
843 if (WARN_ON(!pml4->pdps[pml4e]))
844 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700845
Michał Winiarski2ce51792016-10-13 14:02:42 +0200846 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
847 __clear_bit(pml4e, pml4->used_pml4es);
848 pml4e_vaddr = kmap_px(pml4);
849 pml4e_vaddr[pml4e] = scratch_pml4e;
850 kunmap_px(ppgtt, pml4e_vaddr);
Chris Wilson49d73912016-11-29 09:50:08 +0000851 free_pdp(vm->i915, pdp);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200852 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700853 }
854}
855
Michel Thierryf9b5b782015-07-30 11:02:49 +0100856static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200857 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700858{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300859 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100860
Chris Wilsonc6385c92016-11-29 12:42:05 +0000861 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200862 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
863 else
864 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100865}
866
867static void
868gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
869 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100870 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100871 uint64_t start,
872 enum i915_cache_level cache_level)
873{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300874 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000875 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100876 unsigned pdpe = gen8_pdpe_index(start);
877 unsigned pde = gen8_pde_index(start);
878 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700879
Chris Wilson6f1cc992013-12-31 15:50:31 +0000880 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700881
Michel Thierry3387d432015-08-03 09:52:47 +0100882 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000883 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100884 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100885 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300886 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000887 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800888
889 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100890 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200891 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000892 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300893 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000894 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000895 if (++pde == I915_PDES) {
Chris Wilsonc6385c92016-11-29 12:42:05 +0000896 if (++pdpe == I915_PDPES_PER_PDP(vm->i915))
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100897 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800898 pde = 0;
899 }
900 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700901 }
902 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300903
904 if (pt_vaddr)
905 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700906}
907
Michel Thierryf9b5b782015-07-30 11:02:49 +0100908static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
909 struct sg_table *pages,
910 uint64_t start,
911 enum i915_cache_level cache_level,
912 u32 unused)
913{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300914 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100915 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100916
Michel Thierry3387d432015-08-03 09:52:47 +0100917 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100918
Chris Wilsonc6385c92016-11-29 12:42:05 +0000919 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100920 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
921 cache_level);
922 } else {
923 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000924 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100925 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
926
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000927 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100928 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
929 start, cache_level);
930 }
931 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100932}
933
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000934static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
Michel Thierryf37c0502015-06-10 17:46:39 +0100935 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800936{
937 int i;
938
Mika Kuoppala567047b2015-06-25 18:35:12 +0300939 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800940 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800941
Michel Thierry33c88192015-04-08 12:13:33 +0100942 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000943 if (WARN_ON(!pd->page_table[i]))
944 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800945
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000946 free_pt(dev_priv, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000947 pd->page_table[i] = NULL;
948 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000949}
950
Mika Kuoppala8776f022015-06-30 18:16:40 +0300951static int gen8_init_scratch(struct i915_address_space *vm)
952{
Chris Wilson49d73912016-11-29 09:50:08 +0000953 struct drm_i915_private *dev_priv = vm->i915;
Matthew Auld64c050d2016-04-27 13:19:25 +0100954 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300955
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000956 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100957 if (ret)
958 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300959
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000960 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300961 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100962 ret = PTR_ERR(vm->scratch_pt);
963 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300964 }
965
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000966 vm->scratch_pd = alloc_pd(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300967 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100968 ret = PTR_ERR(vm->scratch_pd);
969 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300970 }
971
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000972 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
973 vm->scratch_pdp = alloc_pdp(dev_priv);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100974 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100975 ret = PTR_ERR(vm->scratch_pdp);
976 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100977 }
978 }
979
Mika Kuoppala8776f022015-06-30 18:16:40 +0300980 gen8_initialize_pt(vm, vm->scratch_pt);
981 gen8_initialize_pd(vm, vm->scratch_pd);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000982 if (USES_FULL_48BIT_PPGTT(dev_priv))
Michel Thierry69ab76f2015-07-29 17:23:55 +0100983 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300984
985 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100986
987free_pd:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000988 free_pd(dev_priv, vm->scratch_pd);
Matthew Auld64c050d2016-04-27 13:19:25 +0100989free_pt:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000990 free_pt(dev_priv, vm->scratch_pt);
Matthew Auld64c050d2016-04-27 13:19:25 +0100991free_scratch_page:
Tvrtko Ursulin275a9912016-11-16 08:55:34 +0000992 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +0100993
994 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300995}
996
Zhiyuan Lv650da342015-08-28 15:41:18 +0800997static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
998{
999 enum vgt_g2v_type msg;
Chris Wilson49d73912016-11-29 09:50:08 +00001000 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Zhiyuan Lv650da342015-08-28 15:41:18 +08001001 int i;
1002
Matthew Aulddf285642016-04-22 12:09:25 +01001003 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001004 u64 daddr = px_dma(&ppgtt->pml4);
1005
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001006 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1007 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001008
1009 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1010 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1011 } else {
1012 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1013 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1014
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001015 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1016 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001017 }
1018
1019 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1020 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1021 }
1022
1023 I915_WRITE(vgtif_reg(g2v_notify), msg);
1024
1025 return 0;
1026}
1027
Mika Kuoppala8776f022015-06-30 18:16:40 +03001028static void gen8_free_scratch(struct i915_address_space *vm)
1029{
Chris Wilson49d73912016-11-29 09:50:08 +00001030 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001031
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001032 if (USES_FULL_48BIT_PPGTT(dev_priv))
1033 free_pdp(dev_priv, vm->scratch_pdp);
1034 free_pd(dev_priv, vm->scratch_pd);
1035 free_pt(dev_priv, vm->scratch_pt);
1036 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001037}
1038
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001039static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
Michel Thierry762d9932015-07-30 11:05:29 +01001040 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001041{
1042 int i;
1043
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001044 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001045 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001046 continue;
1047
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001048 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1049 free_pd(dev_priv, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001050 }
Michel Thierry69876be2015-04-08 12:13:27 +01001051
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001052 free_pdp(dev_priv, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001053}
1054
1055static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1056{
Chris Wilson49d73912016-11-29 09:50:08 +00001057 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001058 int i;
1059
1060 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1061 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1062 continue;
1063
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001064 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
Michel Thierry762d9932015-07-30 11:05:29 +01001065 }
1066
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001067 cleanup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001068}
1069
1070static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1071{
Chris Wilson49d73912016-11-29 09:50:08 +00001072 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001073 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001074
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001075 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001076 gen8_ppgtt_notify_vgt(ppgtt, false);
1077
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001078 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1079 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001080 else
1081 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001082
Mika Kuoppala8776f022015-06-30 18:16:40 +03001083 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001084}
1085
Michel Thierryd7b26332015-04-08 12:13:34 +01001086/**
1087 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001088 * @vm: Master vm structure.
1089 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001090 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001091 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001092 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1093 * caller to free on error.
1094 *
1095 * Allocate the required number of page tables. Extremely similar to
1096 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1097 * the page directory boundary (instead of the page directory pointer). That
1098 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1099 * possible, and likely that the caller will need to use multiple calls of this
1100 * function to achieve the appropriate allocation.
1101 *
1102 * Return: 0 if success; negative error code otherwise.
1103 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001104static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001105 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001106 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001107 uint64_t length,
1108 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001109{
Chris Wilson49d73912016-11-29 09:50:08 +00001110 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001111 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001112 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001113
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001114 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001115 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001116 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001117 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001118 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001119 continue;
1120 }
1121
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001122 pt = alloc_pt(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001123 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001124 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001125
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001126 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001127 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001128 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001129 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001130 }
1131
1132 return 0;
1133
1134unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001135 for_each_set_bit(pde, new_pts, I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001136 free_pt(dev_priv, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001137
1138 return -ENOMEM;
1139}
1140
Michel Thierryd7b26332015-04-08 12:13:34 +01001141/**
1142 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001143 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001144 * @pdp: Page directory pointer for this address range.
1145 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001146 * @length: Size of the allocations.
1147 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001148 * caller to free on error.
1149 *
1150 * Allocate the required number of page directories starting at the pde index of
1151 * @start, and ending at the pde index @start + @length. This function will skip
1152 * over already allocated page directories within the range, and only allocate
1153 * new ones, setting the appropriate pointer within the pdp as well as the
1154 * correct position in the bitmap @new_pds.
1155 *
1156 * The function will only allocate the pages within the range for a give page
1157 * directory pointer. In other words, if @start + @length straddles a virtually
1158 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1159 * required by the caller, This is not currently possible, and the BUG in the
1160 * code will prevent it.
1161 *
1162 * Return: 0 if success; negative error code otherwise.
1163 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001164static int
1165gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1166 struct i915_page_directory_pointer *pdp,
1167 uint64_t start,
1168 uint64_t length,
1169 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001170{
Chris Wilson49d73912016-11-29 09:50:08 +00001171 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierryd7b26332015-04-08 12:13:34 +01001172 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001173 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001174 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001175
Michel Thierry6ac18502015-07-29 17:23:46 +01001176 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001177
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001178 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001179 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001180 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001181
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001182 pd = alloc_pd(dev_priv);
Michel Thierryd7b26332015-04-08 12:13:34 +01001183 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001184 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001185
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001186 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001187 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001188 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001189 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001190 }
1191
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001192 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001193
1194unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001195 for_each_set_bit(pdpe, new_pds, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001196 free_pd(dev_priv, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001197
1198 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001199}
1200
Michel Thierry762d9932015-07-30 11:05:29 +01001201/**
1202 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1203 * @vm: Master vm structure.
1204 * @pml4: Page map level 4 for this address range.
1205 * @start: Starting virtual address to begin allocations.
1206 * @length: Size of the allocations.
1207 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1208 * caller to free on error.
1209 *
1210 * Allocate the required number of page directory pointers. Extremely similar to
1211 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1212 * The main difference is here we are limited by the pml4 boundary (instead of
1213 * the page directory pointer).
1214 *
1215 * Return: 0 if success; negative error code otherwise.
1216 */
1217static int
1218gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1219 struct i915_pml4 *pml4,
1220 uint64_t start,
1221 uint64_t length,
1222 unsigned long *new_pdps)
1223{
Chris Wilson49d73912016-11-29 09:50:08 +00001224 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry762d9932015-07-30 11:05:29 +01001225 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001226 uint32_t pml4e;
1227
1228 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1229
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001230 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001231 if (!test_bit(pml4e, pml4->used_pml4es)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001232 pdp = alloc_pdp(dev_priv);
Michel Thierry762d9932015-07-30 11:05:29 +01001233 if (IS_ERR(pdp))
1234 goto unwind_out;
1235
Michel Thierry69ab76f2015-07-29 17:23:55 +01001236 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001237 pml4->pdps[pml4e] = pdp;
1238 __set_bit(pml4e, new_pdps);
1239 trace_i915_page_directory_pointer_entry_alloc(vm,
1240 pml4e,
1241 start,
1242 GEN8_PML4E_SHIFT);
1243 }
1244 }
1245
1246 return 0;
1247
1248unwind_out:
1249 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001250 free_pdp(dev_priv, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001251
1252 return -ENOMEM;
1253}
1254
Michel Thierryd7b26332015-04-08 12:13:34 +01001255static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001256free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001257{
Michel Thierryd7b26332015-04-08 12:13:34 +01001258 kfree(new_pts);
1259 kfree(new_pds);
1260}
1261
1262/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1263 * of these are based on the number of PDPEs in the system.
1264 */
1265static
1266int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001267 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001268 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001269{
Michel Thierryd7b26332015-04-08 12:13:34 +01001270 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001271 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001272
Michał Winiarski3a41a052015-09-03 19:22:18 +02001273 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001274 if (!pds)
1275 return -ENOMEM;
1276
Michał Winiarski3a41a052015-09-03 19:22:18 +02001277 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1278 GFP_TEMPORARY);
1279 if (!pts)
1280 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001281
1282 *new_pds = pds;
1283 *new_pts = pts;
1284
1285 return 0;
1286
1287err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001288 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001289 return -ENOMEM;
1290}
1291
Michel Thierry762d9932015-07-30 11:05:29 +01001292static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1293 struct i915_page_directory_pointer *pdp,
1294 uint64_t start,
1295 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001296{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001297 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001298 unsigned long *new_page_dirs, *new_page_tables;
Chris Wilson49d73912016-11-29 09:50:08 +00001299 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001300 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001301 const uint64_t orig_start = start;
1302 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001303 uint32_t pdpe;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001304 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001305 int ret;
1306
Michel Thierryd7b26332015-04-08 12:13:34 +01001307 /* Wrap is never okay since we can only represent 48b, and we don't
1308 * actually use the other side of the canonical address space.
1309 */
1310 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001311 return -ENODEV;
1312
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001313 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001314 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001315
Michel Thierry6ac18502015-07-29 17:23:46 +01001316 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001317 if (ret)
1318 return ret;
1319
Michel Thierryd7b26332015-04-08 12:13:34 +01001320 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001321 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1322 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001323 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001324 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001325 return ret;
1326 }
1327
1328 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001329 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001330 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001331 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001332 if (ret)
1333 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001334 }
1335
Michel Thierry33c88192015-04-08 12:13:33 +01001336 start = orig_start;
1337 length = orig_length;
1338
Michel Thierryd7b26332015-04-08 12:13:34 +01001339 /* Allocations have completed successfully, so set the bitmaps, and do
1340 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001341 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001342 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001343 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001344 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001345 uint64_t pd_start = start;
1346 uint32_t pde;
1347
Michel Thierryd7b26332015-04-08 12:13:34 +01001348 /* Every pd should be allocated, we just did that above. */
1349 WARN_ON(!pd);
1350
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001351 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001352 /* Same reasoning as pd */
1353 WARN_ON(!pt);
1354 WARN_ON(!pd_len);
1355 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1356
1357 /* Set our used ptes within the page table */
1358 bitmap_set(pt->used_ptes,
1359 gen8_pte_index(pd_start),
1360 gen8_pte_count(pd_start, pd_len));
1361
1362 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001363 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001364
1365 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001366 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1367 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001368 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1369 gen8_pte_index(start),
1370 gen8_pte_count(start, length),
1371 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001372
1373 /* NB: We haven't yet mapped ptes to pages. At this
1374 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001375 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001376
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001377 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001378 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001379 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001380 }
1381
Michał Winiarski3a41a052015-09-03 19:22:18 +02001382 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001383 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001384 return 0;
1385
1386err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001387 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001388 unsigned long temp;
1389
Michał Winiarski3a41a052015-09-03 19:22:18 +02001390 for_each_set_bit(temp, new_page_tables + pdpe *
1391 BITS_TO_LONGS(I915_PDES), I915_PDES)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001392 free_pt(dev_priv,
1393 pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001394 }
1395
Michel Thierry6ac18502015-07-29 17:23:46 +01001396 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001397 free_pd(dev_priv, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001398
Michał Winiarski3a41a052015-09-03 19:22:18 +02001399 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001400 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001401 return ret;
1402}
1403
Michel Thierry762d9932015-07-30 11:05:29 +01001404static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1405 struct i915_pml4 *pml4,
1406 uint64_t start,
1407 uint64_t length)
1408{
1409 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001410 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001411 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001412 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001413 int ret = 0;
1414
1415 /* Do the pml4 allocations first, so we don't need to track the newly
1416 * allocated tables below the pdp */
1417 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1418
1419 /* The pagedirectory and pagetable allocations are done in the shared 3
1420 * and 4 level code. Just allocate the pdps.
1421 */
1422 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1423 new_pdps);
1424 if (ret)
1425 return ret;
1426
1427 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1428 "The allocation has spanned more than 512GB. "
1429 "It is highly likely this is incorrect.");
1430
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001431 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001432 WARN_ON(!pdp);
1433
1434 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1435 if (ret)
1436 goto err_out;
1437
1438 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1439 }
1440
1441 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1442 GEN8_PML4ES_PER_PML4);
1443
1444 return 0;
1445
1446err_out:
1447 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
Chris Wilson49d73912016-11-29 09:50:08 +00001448 gen8_ppgtt_cleanup_3lvl(vm->i915, pml4->pdps[pml4e]);
Michel Thierry762d9932015-07-30 11:05:29 +01001449
1450 return ret;
1451}
1452
1453static int gen8_alloc_va_range(struct i915_address_space *vm,
1454 uint64_t start, uint64_t length)
1455{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001456 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001457
Chris Wilsonc6385c92016-11-29 12:42:05 +00001458 if (USES_FULL_48BIT_PPGTT(vm->i915))
Michel Thierry762d9932015-07-30 11:05:29 +01001459 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1460 else
1461 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1462}
1463
Michel Thierryea91e402015-07-29 17:23:57 +01001464static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1465 uint64_t start, uint64_t length,
1466 gen8_pte_t scratch_pte,
1467 struct seq_file *m)
1468{
1469 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001470 uint32_t pdpe;
1471
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001472 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001473 struct i915_page_table *pt;
1474 uint64_t pd_len = length;
1475 uint64_t pd_start = start;
1476 uint32_t pde;
1477
1478 if (!test_bit(pdpe, pdp->used_pdpes))
1479 continue;
1480
1481 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001482 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001483 uint32_t pte;
1484 gen8_pte_t *pt_vaddr;
1485
1486 if (!test_bit(pde, pd->used_pdes))
1487 continue;
1488
1489 pt_vaddr = kmap_px(pt);
1490 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1491 uint64_t va =
1492 (pdpe << GEN8_PDPE_SHIFT) |
1493 (pde << GEN8_PDE_SHIFT) |
1494 (pte << GEN8_PTE_SHIFT);
1495 int i;
1496 bool found = false;
1497
1498 for (i = 0; i < 4; i++)
1499 if (pt_vaddr[pte + i] != scratch_pte)
1500 found = true;
1501 if (!found)
1502 continue;
1503
1504 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1505 for (i = 0; i < 4; i++) {
1506 if (pt_vaddr[pte + i] != scratch_pte)
1507 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1508 else
1509 seq_puts(m, " SCRATCH ");
1510 }
1511 seq_puts(m, "\n");
1512 }
1513 /* don't use kunmap_px, it could trigger
1514 * an unnecessary flush.
1515 */
1516 kunmap_atomic(pt_vaddr);
1517 }
1518 }
1519}
1520
1521static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1522{
1523 struct i915_address_space *vm = &ppgtt->base;
1524 uint64_t start = ppgtt->base.start;
1525 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001526 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001527 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001528
Chris Wilsonc6385c92016-11-29 12:42:05 +00001529 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
Michel Thierryea91e402015-07-29 17:23:57 +01001530 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1531 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001532 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001533 struct i915_pml4 *pml4 = &ppgtt->pml4;
1534 struct i915_page_directory_pointer *pdp;
1535
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001536 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001537 if (!test_bit(pml4e, pml4->used_pml4es))
1538 continue;
1539
1540 seq_printf(m, " PML4E #%llu\n", pml4e);
1541 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1542 }
1543 }
1544}
1545
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001546static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1547{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001548 unsigned long *new_page_dirs, *new_page_tables;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001549 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001550 int ret;
1551
1552 /* We allocate temp bitmap for page tables for no gain
1553 * but as this is for init only, lets keep the things simple
1554 */
1555 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1556 if (ret)
1557 return ret;
1558
1559 /* Allocate for all pdps regardless of how the ppgtt
1560 * was defined.
1561 */
1562 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1563 0, 1ULL << 32,
1564 new_page_dirs);
1565 if (!ret)
1566 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1567
Michał Winiarski3a41a052015-09-03 19:22:18 +02001568 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001569
1570 return ret;
1571}
1572
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001573/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001574 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1575 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1576 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1577 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001578 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001579 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001580static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001581{
Chris Wilson49d73912016-11-29 09:50:08 +00001582 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03001583 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001584
Mika Kuoppala8776f022015-06-30 18:16:40 +03001585 ret = gen8_init_scratch(&ppgtt->base);
1586 if (ret)
1587 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001588
Michel Thierryd7b26332015-04-08 12:13:34 +01001589 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001590 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001591 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001592 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001593 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001594 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1595 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001596 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001597
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001598 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1599 ret = setup_px(dev_priv, &ppgtt->pml4);
Michel Thierry762d9932015-07-30 11:05:29 +01001600 if (ret)
1601 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001602
Michel Thierry69ab76f2015-07-29 17:23:55 +01001603 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1604
Michel Thierry762d9932015-07-30 11:05:29 +01001605 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001606 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001607 } else {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001608 ret = __pdp_init(dev_priv, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001609 if (ret)
1610 goto free_scratch;
1611
1612 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001613 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001614 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1615 0, 0,
1616 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001617
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001618 if (intel_vgpu_active(dev_priv)) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001619 ret = gen8_preallocate_top_level_pdps(ppgtt);
1620 if (ret)
1621 goto free_scratch;
1622 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001623 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001624
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001625 if (intel_vgpu_active(dev_priv))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001626 gen8_ppgtt_notify_vgt(ppgtt, true);
1627
Michel Thierryd7b26332015-04-08 12:13:34 +01001628 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001629
1630free_scratch:
1631 gen8_free_scratch(&ppgtt->base);
1632 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001633}
1634
Ben Widawsky87d60b62013-12-06 14:11:29 -08001635static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1636{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001637 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001638 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001639 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001640 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001641 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001642 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001643
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001644 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001645 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001646
Dave Gordon731f74c2016-06-24 19:37:46 +01001647 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001648 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001649 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001650 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001651 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001652 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1653
1654 if (pd_entry != expected)
1655 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1656 pde,
1657 pd_entry,
1658 expected);
1659 seq_printf(m, "\tPDE: %x\n", pd_entry);
1660
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001661 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1662
Michel Thierry07749ef2015-03-16 16:00:54 +00001663 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001664 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001665 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001666 (pte * PAGE_SIZE);
1667 int i;
1668 bool found = false;
1669 for (i = 0; i < 4; i++)
1670 if (pt_vaddr[pte + i] != scratch_pte)
1671 found = true;
1672 if (!found)
1673 continue;
1674
1675 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1676 for (i = 0; i < 4; i++) {
1677 if (pt_vaddr[pte + i] != scratch_pte)
1678 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1679 else
1680 seq_puts(m, " SCRATCH ");
1681 }
1682 seq_puts(m, "\n");
1683 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001684 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001685 }
1686}
1687
Ben Widawsky678d96f2015-03-16 16:00:56 +00001688/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001689static void gen6_write_pde(struct i915_page_directory *pd,
1690 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001691{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001692 /* Caller needs to make sure the write completes if necessary */
1693 struct i915_hw_ppgtt *ppgtt =
1694 container_of(pd, struct i915_hw_ppgtt, pd);
1695 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001696
Mika Kuoppala567047b2015-06-25 18:35:12 +03001697 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001698 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001699
Ben Widawsky678d96f2015-03-16 16:00:56 +00001700 writel(pd_entry, ppgtt->pd_addr + pde);
1701}
Ben Widawsky61973492013-04-08 18:43:54 -07001702
Ben Widawsky678d96f2015-03-16 16:00:56 +00001703/* Write all the page tables found in the ppgtt structure to incrementing page
1704 * directories. */
1705static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001706 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001707 uint32_t start, uint32_t length)
1708{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001709 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001710 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001711 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001712
Dave Gordon731f74c2016-06-24 19:37:46 +01001713 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001714 gen6_write_pde(pd, pde, pt);
1715
1716 /* Make sure write is complete before other code can use this page
1717 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001718 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001719}
1720
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001721static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001722{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001723 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001724
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001725 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001726}
Ben Widawsky61973492013-04-08 18:43:54 -07001727
Ben Widawsky90252e52013-12-06 14:11:12 -08001728static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001729 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001730{
Chris Wilson7e37f882016-08-02 22:50:21 +01001731 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001732 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001733 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001734
Ben Widawsky90252e52013-12-06 14:11:12 -08001735 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001736 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001737 if (ret)
1738 return ret;
1739
John Harrison5fb9de12015-05-29 17:44:07 +01001740 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001741 if (ret)
1742 return ret;
1743
Chris Wilsonb5321f32016-08-02 22:50:18 +01001744 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1745 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1746 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1747 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1748 intel_ring_emit(ring, get_pd_offset(ppgtt));
1749 intel_ring_emit(ring, MI_NOOP);
1750 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001751
1752 return 0;
1753}
1754
Ben Widawsky48a10382013-12-06 14:11:11 -08001755static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001756 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001757{
Chris Wilson7e37f882016-08-02 22:50:21 +01001758 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001759 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001760 int ret;
1761
Ben Widawsky48a10382013-12-06 14:11:11 -08001762 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001763 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001764 if (ret)
1765 return ret;
1766
John Harrison5fb9de12015-05-29 17:44:07 +01001767 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001768 if (ret)
1769 return ret;
1770
Chris Wilsonb5321f32016-08-02 22:50:18 +01001771 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1772 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1773 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1774 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1775 intel_ring_emit(ring, get_pd_offset(ppgtt));
1776 intel_ring_emit(ring, MI_NOOP);
1777 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001778
Ben Widawsky90252e52013-12-06 14:11:12 -08001779 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001780 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001781 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001782 if (ret)
1783 return ret;
1784 }
1785
Ben Widawsky48a10382013-12-06 14:11:11 -08001786 return 0;
1787}
1788
Ben Widawskyeeb94882013-12-06 14:11:10 -08001789static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001790 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001791{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001792 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001793 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001794
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001795 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1796 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001797 return 0;
1798}
1799
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001800static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001801{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001802 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301803 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001804
Akash Goel3b3f1652016-10-13 22:44:48 +05301805 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001806 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1807 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001808 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001809 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001810 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001811}
1812
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001813static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001814{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001815 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001816 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301817 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001818
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001819 ecobits = I915_READ(GAC_ECO_BITS);
1820 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1821
1822 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001823 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001824 ecochk |= ECOCHK_PPGTT_WB_HSW;
1825 } else {
1826 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1827 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1828 }
1829 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001830
Akash Goel3b3f1652016-10-13 22:44:48 +05301831 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001832 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001833 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001834 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001835 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001836}
1837
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001838static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001839{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001840 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001841
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001842 ecobits = I915_READ(GAC_ECO_BITS);
1843 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1844 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001845
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001846 gab_ctl = I915_READ(GAB_CTL);
1847 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001848
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001849 ecochk = I915_READ(GAM_ECOCHK);
1850 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001851
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001852 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001853}
1854
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001855/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001856static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001857 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001858 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001859{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001860 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001861 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001862 unsigned first_entry = start >> PAGE_SHIFT;
1863 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001864 unsigned act_pt = first_entry / GEN6_PTES;
1865 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001866 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001867
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001868 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001869 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001870
Daniel Vetter7bddb012012-02-09 17:15:47 +01001871 while (num_entries) {
1872 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001873 if (last_pte > GEN6_PTES)
1874 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001875
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001876 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001877
1878 for (i = first_pte; i < last_pte; i++)
1879 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001880
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001881 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001882
Daniel Vetter7bddb012012-02-09 17:15:47 +01001883 num_entries -= last_pte - first_pte;
1884 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001885 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001886 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001887}
1888
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001889static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001890 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001891 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301892 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001893{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001894 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001895 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001896 unsigned act_pt = first_entry / GEN6_PTES;
1897 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001898 gen6_pte_t *pt_vaddr = NULL;
1899 struct sgt_iter sgt_iter;
1900 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001901
Dave Gordon85d12252016-05-20 11:54:06 +01001902 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001903 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001904 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001905
Chris Wilsoncc797142013-12-31 15:50:30 +00001906 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001907 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301908
Michel Thierry07749ef2015-03-16 16:00:54 +00001909 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001910 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001911 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001912 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001913 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001914 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001915 }
Dave Gordon85d12252016-05-20 11:54:06 +01001916
Chris Wilsoncc797142013-12-31 15:50:30 +00001917 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001918 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001919}
1920
Ben Widawsky678d96f2015-03-16 16:00:56 +00001921static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001922 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001923{
Michel Thierry4933d512015-03-24 15:46:22 +00001924 DECLARE_BITMAP(new_page_tables, I915_PDES);
Chris Wilson49d73912016-11-29 09:50:08 +00001925 struct drm_i915_private *dev_priv = vm->i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001926 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001927 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001928 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001929 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001930 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001931 int ret;
1932
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001933 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1934 return -ENODEV;
1935
1936 start = start_save = start_in;
1937 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001938
1939 bitmap_zero(new_page_tables, I915_PDES);
1940
1941 /* The allocation is done in two stages so that we can bail out with
1942 * minimal amount of pain. The first stage finds new page tables that
1943 * need allocation. The second stage marks use ptes within the page
1944 * tables.
1945 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001946 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001947 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001948 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1949 continue;
1950 }
1951
1952 /* We've already allocated a page table */
1953 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1954
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00001955 pt = alloc_pt(dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00001956 if (IS_ERR(pt)) {
1957 ret = PTR_ERR(pt);
1958 goto unwind_out;
1959 }
1960
1961 gen6_initialize_pt(vm, pt);
1962
1963 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001964 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001965 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001966 }
1967
1968 start = start_save;
1969 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001970
Dave Gordon731f74c2016-06-24 19:37:46 +01001971 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001972 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1973
1974 bitmap_zero(tmp_bitmap, GEN6_PTES);
1975 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1976 gen6_pte_count(start, length));
1977
Mika Kuoppala966082c2015-06-25 18:35:19 +03001978 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001979 gen6_write_pde(&ppgtt->pd, pde, pt);
1980
Michel Thierry72744cb2015-03-24 15:46:23 +00001981 trace_i915_page_table_entry_map(vm, pde, pt,
1982 gen6_pte_index(start),
1983 gen6_pte_count(start, length),
1984 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001985 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001986 GEN6_PTES);
1987 }
1988
Michel Thierry4933d512015-03-24 15:46:22 +00001989 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1990
1991 /* Make sure write is complete before other code can use this page
1992 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001993 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001994
Ben Widawsky563222a2015-03-19 12:53:28 +00001995 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001996 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001997
1998unwind_out:
1999 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01002000 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00002001
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002002 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002003 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002004 }
2005
2006 mark_tlbs_dirty(ppgtt);
2007 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002008}
2009
Mika Kuoppala8776f022015-06-30 18:16:40 +03002010static int gen6_init_scratch(struct i915_address_space *vm)
2011{
Chris Wilson49d73912016-11-29 09:50:08 +00002012 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002013 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002014
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002015 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002016 if (ret)
2017 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002018
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002019 vm->scratch_pt = alloc_pt(dev_priv);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002020 if (IS_ERR(vm->scratch_pt)) {
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002021 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002022 return PTR_ERR(vm->scratch_pt);
2023 }
2024
2025 gen6_initialize_pt(vm, vm->scratch_pt);
2026
2027 return 0;
2028}
2029
2030static void gen6_free_scratch(struct i915_address_space *vm)
2031{
Chris Wilson49d73912016-11-29 09:50:08 +00002032 struct drm_i915_private *dev_priv = vm->i915;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002033
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002034 free_pt(dev_priv, vm->scratch_pt);
2035 cleanup_scratch_page(dev_priv, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002036}
2037
Daniel Vetter061dd492015-04-14 17:35:13 +02002038static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002039{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002040 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002041 struct i915_page_directory *pd = &ppgtt->pd;
Chris Wilson49d73912016-11-29 09:50:08 +00002042 struct drm_i915_private *dev_priv = vm->i915;
Michel Thierry09942c62015-04-08 12:13:30 +01002043 struct i915_page_table *pt;
2044 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002045
Daniel Vetter061dd492015-04-14 17:35:13 +02002046 drm_mm_remove_node(&ppgtt->node);
2047
Dave Gordon731f74c2016-06-24 19:37:46 +01002048 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002049 if (pt != vm->scratch_pt)
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002050 free_pt(dev_priv, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002051
Mika Kuoppala8776f022015-06-30 18:16:40 +03002052 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002053}
2054
Ben Widawskyb1465202014-02-19 22:05:49 -08002055static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002056{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002057 struct i915_address_space *vm = &ppgtt->base;
Chris Wilson49d73912016-11-29 09:50:08 +00002058 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002059 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002060 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08002061 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002062
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002063 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2064 * allocator works in address space sizes, so it's multiplied by page
2065 * size. We allocate at the top of the GTT to avoid fragmentation.
2066 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002067 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002068
Mika Kuoppala8776f022015-06-30 18:16:40 +03002069 ret = gen6_init_scratch(vm);
2070 if (ret)
2071 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002072
Ben Widawskye3cc1992013-12-06 14:11:08 -08002073alloc:
Chris Wilson85fd4f52016-12-05 14:29:36 +00002074 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm, &ppgtt->node,
2075 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2076 I915_COLOR_UNEVICTABLE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002077 0, ggtt->base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07002078 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002079 if (ret == -ENOSPC && !retried) {
Chris Wilsone522ac22016-08-04 16:32:18 +01002080 ret = i915_gem_evict_something(&ggtt->base,
Ben Widawskye3cc1992013-12-06 14:11:08 -08002081 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilson85fd4f52016-12-05 14:29:36 +00002082 I915_COLOR_UNEVICTABLE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002083 0, ggtt->base.total,
Chris Wilsond23db882014-05-23 08:48:08 +02002084 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002085 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002086 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002087
2088 retried = true;
2089 goto alloc;
2090 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002091
Ben Widawskyc8c26622015-01-22 17:01:25 +00002092 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002093 goto err_out;
2094
Ben Widawskyc8c26622015-01-22 17:01:25 +00002095
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002096 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002097 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002098
Ben Widawskyc8c26622015-01-22 17:01:25 +00002099 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002100
2101err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002102 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002103 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002104}
2105
Ben Widawskyb1465202014-02-19 22:05:49 -08002106static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2107{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002108 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002109}
2110
Michel Thierry4933d512015-03-24 15:46:22 +00002111static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2112 uint64_t start, uint64_t length)
2113{
Michel Thierryec565b32015-04-08 12:13:23 +01002114 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002115 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002116
Dave Gordon731f74c2016-06-24 19:37:46 +01002117 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002118 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002119}
2120
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002121static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002122{
Chris Wilson49d73912016-11-29 09:50:08 +00002123 struct drm_i915_private *dev_priv = ppgtt->base.i915;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002124 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002125 int ret;
2126
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002127 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002128 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002129 ppgtt->switch_mm = gen6_mm_switch;
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01002130 else if (IS_HASWELL(dev_priv))
Ben Widawsky90252e52013-12-06 14:11:12 -08002131 ppgtt->switch_mm = hsw_mm_switch;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002132 else if (IS_GEN7(dev_priv))
Ben Widawsky48a10382013-12-06 14:11:11 -08002133 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002134 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002135 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002136
2137 ret = gen6_ppgtt_alloc(ppgtt);
2138 if (ret)
2139 return ret;
2140
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002141 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002142 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2143 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002144 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2145 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002146 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002147 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002148 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002149 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002150
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002151 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002152 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002153
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002154 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002155 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002156
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002157 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002158
Ben Widawsky678d96f2015-03-16 16:00:56 +00002159 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2160
Thierry Reding440fd522015-01-23 09:05:06 +01002161 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002162 ppgtt->node.size >> 20,
2163 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002164
Daniel Vetterfa76da32014-08-06 20:19:54 +02002165 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002166 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002167
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002168 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002169}
2170
Chris Wilson2bfa9962016-08-04 07:52:25 +01002171static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2172 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002173{
Chris Wilson49d73912016-11-29 09:50:08 +00002174 ppgtt->base.i915 = dev_priv;
Daniel Vetter3440d262013-01-24 13:49:56 -08002175
Chris Wilson2bfa9962016-08-04 07:52:25 +01002176 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002177 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002178 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002179 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002180}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002181
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002182static void i915_address_space_init(struct i915_address_space *vm,
Chris Wilson80b204b2016-10-28 13:58:58 +01002183 struct drm_i915_private *dev_priv,
2184 const char *name)
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002185{
Chris Wilson80b204b2016-10-28 13:58:58 +01002186 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002187 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002188 INIT_LIST_HEAD(&vm->active_list);
2189 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002190 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002191 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2192}
2193
Matthew Aulded9724d2016-11-17 21:04:10 +00002194static void i915_address_space_fini(struct i915_address_space *vm)
2195{
2196 i915_gem_timeline_fini(&vm->timeline);
2197 drm_mm_takedown(&vm->mm);
2198 list_del(&vm->global_link);
2199}
2200
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002201static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002202{
Tim Gored5165eb2016-02-04 11:49:34 +00002203 /* This function is for gtt related workarounds. This function is
2204 * called on driver load and after a GPU reset, so you can place
2205 * workarounds here even if they get overwritten by GPU reset.
2206 */
2207 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002208 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002209 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002210 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002211 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002212 else if (IS_SKYLAKE(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002213 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002214 else if (IS_BROXTON(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002215 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2216}
2217
Chris Wilson2bfa9962016-08-04 07:52:25 +01002218static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2219 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002220 struct drm_i915_file_private *file_priv,
2221 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002222{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002223 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002224
Chris Wilson2bfa9962016-08-04 07:52:25 +01002225 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002226 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002227 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002228 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002229 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002230 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002231
2232 return ret;
2233}
2234
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002235int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002236{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002237 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002238
Thomas Daniel671b50132014-08-20 16:24:50 +01002239 /* In the case of execlists, PPGTT is enabled by the context descriptor
2240 * and the PDPs are contained within the context itself. We don't
2241 * need to do anything here. */
2242 if (i915.enable_execlists)
2243 return 0;
2244
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002245 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002246 return 0;
2247
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002248 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002249 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002250 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002251 gen7_ppgtt_enable(dev_priv);
2252 else if (INTEL_GEN(dev_priv) >= 8)
2253 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002254 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002255 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002256
John Harrison4ad2fd82015-06-18 13:11:20 +01002257 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002258}
John Harrison4ad2fd82015-06-18 13:11:20 +01002259
Daniel Vetter4d884702014-08-06 15:04:47 +02002260struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002261i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002262 struct drm_i915_file_private *fpriv,
2263 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002264{
2265 struct i915_hw_ppgtt *ppgtt;
2266 int ret;
2267
2268 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2269 if (!ppgtt)
2270 return ERR_PTR(-ENOMEM);
2271
Chris Wilson80b204b2016-10-28 13:58:58 +01002272 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002273 if (ret) {
2274 kfree(ppgtt);
2275 return ERR_PTR(ret);
2276 }
2277
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002278 trace_i915_ppgtt_create(&ppgtt->base);
2279
Daniel Vetter4d884702014-08-06 15:04:47 +02002280 return ppgtt;
2281}
2282
Matthew Aulded9724d2016-11-17 21:04:10 +00002283void i915_ppgtt_release(struct kref *kref)
Daniel Vetteree960be2014-08-06 15:04:45 +02002284{
2285 struct i915_hw_ppgtt *ppgtt =
2286 container_of(kref, struct i915_hw_ppgtt, ref);
2287
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002288 trace_i915_ppgtt_release(&ppgtt->base);
2289
Chris Wilson50e046b2016-08-04 07:52:46 +01002290 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002291 WARN_ON(!list_empty(&ppgtt->base.active_list));
2292 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002293 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002294
Matthew Aulded9724d2016-11-17 21:04:10 +00002295 i915_address_space_fini(&ppgtt->base);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002296
Daniel Vetteree960be2014-08-06 15:04:45 +02002297 ppgtt->base.cleanup(&ppgtt->base);
2298 kfree(ppgtt);
2299}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002300
Ben Widawskya81cc002013-01-18 12:30:31 -08002301/* Certain Gen5 chipsets require require idling the GPU before
2302 * unmapping anything from the GTT when VT-d is enabled.
2303 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002304static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002305{
2306#ifdef CONFIG_INTEL_IOMMU
2307 /* Query intel_iommu to see if we need the workaround. Presumably that
2308 * was loaded first.
2309 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002310 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002311 return true;
2312#endif
2313 return false;
2314}
2315
Chris Wilsondc979972016-05-10 14:10:04 +01002316void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002317{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002318 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302319 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002320
Chris Wilsondc979972016-05-10 14:10:04 +01002321 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002322 return;
2323
Akash Goel3b3f1652016-10-13 22:44:48 +05302324 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002325 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002326 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002327 if (fault_reg & RING_FAULT_VALID) {
2328 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002329 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002330 "\tAddress space: %s\n"
2331 "\tSource ID: %d\n"
2332 "\tType: %d\n",
2333 fault_reg & PAGE_MASK,
2334 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2335 RING_FAULT_SRCID(fault_reg),
2336 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002337 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002338 fault_reg & ~RING_FAULT_VALID);
2339 }
2340 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302341
2342 /* Engine specific init may not have been done till this point. */
2343 if (dev_priv->engine[RCS])
2344 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002345}
2346
Chris Wilson91e56492014-09-25 10:13:12 +01002347static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2348{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002349 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002350 intel_gtt_chipset_flush();
2351 } else {
2352 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2353 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2354 }
2355}
2356
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002357void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002358{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002359 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002360
2361 /* Don't bother messing with faults pre GEN6 as we have little
2362 * documentation supporting that it's a good idea.
2363 */
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002364 if (INTEL_GEN(dev_priv) < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002365 return;
2366
Chris Wilsondc979972016-05-10 14:10:04 +01002367 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002368
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002369 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002370
2371 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002372}
2373
Chris Wilson03ac84f2016-10-28 13:58:36 +01002374int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2375 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002376{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002377 if (dma_map_sg(&obj->base.dev->pdev->dev,
2378 pages->sgl, pages->nents,
2379 PCI_DMA_BIDIRECTIONAL))
2380 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01002381
Chris Wilson03ac84f2016-10-28 13:58:36 +01002382 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002383}
2384
Daniel Vetter2c642b02015-04-14 17:35:26 +02002385static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002386{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002387 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002388}
2389
Chris Wilsond6473f52016-06-10 14:22:59 +05302390static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2391 dma_addr_t addr,
2392 uint64_t offset,
2393 enum i915_cache_level level,
2394 u32 unused)
2395{
Chris Wilson49d73912016-11-29 09:50:08 +00002396 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilsond6473f52016-06-10 14:22:59 +05302397 gen8_pte_t __iomem *pte =
2398 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2399 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302400
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002401 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302402
2403 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2404 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302405}
2406
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002407static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2408 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002409 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302410 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002411{
Chris Wilson49d73912016-11-29 09:50:08 +00002412 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilsonce7fda22016-04-28 09:56:38 +01002413 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002414 struct sgt_iter sgt_iter;
2415 gen8_pte_t __iomem *gtt_entries;
2416 gen8_pte_t gtt_entry;
2417 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002418 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002419
Dave Gordon85d12252016-05-20 11:54:06 +01002420 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2421
2422 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002423 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002424 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002425 }
2426
2427 /*
2428 * XXX: This serves as a posting read to make sure that the PTE has
2429 * actually been updated. There is some concern that even though
2430 * registers and PTEs are within the same BAR that they are potentially
2431 * of NUMA access patterns. Therefore, even with the way we assume
2432 * hardware should work, we must keep this posting read for paranoia.
2433 */
2434 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002435 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002436
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002437 /* This next bit makes the above posting read even more important. We
2438 * want to flush the TLBs only after we're certain all the PTE updates
2439 * have finished.
2440 */
2441 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2442 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002443}
2444
Chris Wilsonc1403302015-11-18 15:19:39 +00002445struct insert_entries {
2446 struct i915_address_space *vm;
2447 struct sg_table *st;
2448 uint64_t start;
2449 enum i915_cache_level level;
2450 u32 flags;
2451};
2452
2453static int gen8_ggtt_insert_entries__cb(void *_arg)
2454{
2455 struct insert_entries *arg = _arg;
2456 gen8_ggtt_insert_entries(arg->vm, arg->st,
2457 arg->start, arg->level, arg->flags);
2458 return 0;
2459}
2460
2461static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2462 struct sg_table *st,
2463 uint64_t start,
2464 enum i915_cache_level level,
2465 u32 flags)
2466{
2467 struct insert_entries arg = { vm, st, start, level, flags };
2468 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2469}
2470
Chris Wilsond6473f52016-06-10 14:22:59 +05302471static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2472 dma_addr_t addr,
2473 uint64_t offset,
2474 enum i915_cache_level level,
2475 u32 flags)
2476{
Chris Wilson49d73912016-11-29 09:50:08 +00002477 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilsond6473f52016-06-10 14:22:59 +05302478 gen6_pte_t __iomem *pte =
2479 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2480 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302481
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002482 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302483
2484 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2485 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302486}
2487
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002488/*
2489 * Binds an object into the global gtt with the specified cache level. The object
2490 * will be accessible to the GPU via commands whose operands reference offsets
2491 * within the global GTT as well as accessible by the GPU through the GMADR
2492 * mapped BAR (dev_priv->mm.gtt->gtt).
2493 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002494static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002495 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002496 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302497 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002498{
Chris Wilson49d73912016-11-29 09:50:08 +00002499 struct drm_i915_private *dev_priv = vm->i915;
Chris Wilsonce7fda22016-04-28 09:56:38 +01002500 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002501 struct sgt_iter sgt_iter;
2502 gen6_pte_t __iomem *gtt_entries;
2503 gen6_pte_t gtt_entry;
2504 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002505 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002506
Dave Gordon85d12252016-05-20 11:54:06 +01002507 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2508
2509 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002510 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002511 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002512 }
2513
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002514 /* XXX: This serves as a posting read to make sure that the PTE has
2515 * actually been updated. There is some concern that even though
2516 * registers and PTEs are within the same BAR that they are potentially
2517 * of NUMA access patterns. Therefore, even with the way we assume
2518 * hardware should work, we must keep this posting read for paranoia.
2519 */
Dave Gordon85d12252016-05-20 11:54:06 +01002520 if (i != 0)
2521 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002522
2523 /* This next bit makes the above posting read even more important. We
2524 * want to flush the TLBs only after we're certain all the PTE updates
2525 * have finished.
2526 */
2527 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2528 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002529}
2530
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002531static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002532 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002533{
2534}
2535
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002536static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002537 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002538{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002539 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002540 unsigned first_entry = start >> PAGE_SHIFT;
2541 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002542 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002543 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2544 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002545 int i;
2546
2547 if (WARN(num_entries > max_entries,
2548 "First entry = %d; Num entries = %d (max=%d)\n",
2549 first_entry, num_entries, max_entries))
2550 num_entries = max_entries;
2551
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002552 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002553 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002554 for (i = 0; i < num_entries; i++)
2555 gen8_set_pte(&gtt_base[i], scratch_pte);
2556 readl(gtt_base);
2557}
2558
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002559static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002560 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002561 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002562{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002563 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002564 unsigned first_entry = start >> PAGE_SHIFT;
2565 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002566 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002567 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2568 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002569 int i;
2570
2571 if (WARN(num_entries > max_entries,
2572 "First entry = %d; Num entries = %d (max=%d)\n",
2573 first_entry, num_entries, max_entries))
2574 num_entries = max_entries;
2575
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002576 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002577 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002578
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002579 for (i = 0; i < num_entries; i++)
2580 iowrite32(scratch_pte, &gtt_base[i]);
2581 readl(gtt_base);
2582}
2583
Chris Wilsond6473f52016-06-10 14:22:59 +05302584static void i915_ggtt_insert_page(struct i915_address_space *vm,
2585 dma_addr_t addr,
2586 uint64_t offset,
2587 enum i915_cache_level cache_level,
2588 u32 unused)
2589{
Chris Wilsond6473f52016-06-10 14:22:59 +05302590 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2591 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302592
2593 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302594}
2595
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002596static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2597 struct sg_table *pages,
2598 uint64_t start,
2599 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002600{
2601 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2602 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2603
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002604 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002605
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002606}
2607
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002608static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002609 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002610 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002611{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002612 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002613}
2614
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002615static int ggtt_bind_vma(struct i915_vma *vma,
2616 enum i915_cache_level cache_level,
2617 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002618{
Chris Wilson49d73912016-11-29 09:50:08 +00002619 struct drm_i915_private *i915 = vma->vm->i915;
Daniel Vetter0a878712015-10-15 14:23:01 +02002620 struct drm_i915_gem_object *obj = vma->obj;
2621 u32 pte_flags = 0;
2622 int ret;
2623
2624 ret = i915_get_ggtt_vma_pages(vma);
2625 if (ret)
2626 return ret;
2627
2628 /* Currently applicable only to VLV */
2629 if (obj->gt_ro)
2630 pte_flags |= PTE_READ_ONLY;
2631
Chris Wilson9c870d02016-10-24 13:42:15 +01002632 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002633 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002634 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002635 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002636
2637 /*
2638 * Without aliasing PPGTT there's no difference between
2639 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2640 * upgrade to both bound if we bind either to avoid double-binding.
2641 */
Chris Wilson3272db52016-08-04 16:32:32 +01002642 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002643
2644 return 0;
2645}
2646
2647static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2648 enum i915_cache_level cache_level,
2649 u32 flags)
2650{
Chris Wilson49d73912016-11-29 09:50:08 +00002651 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson321d1782015-11-20 10:27:18 +00002652 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002653 int ret;
2654
2655 ret = i915_get_ggtt_vma_pages(vma);
2656 if (ret)
2657 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002658
Akash Goel24f3a8c2014-06-17 10:59:42 +05302659 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002660 pte_flags = 0;
2661 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002662 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302663
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002664
Chris Wilson3272db52016-08-04 16:32:32 +01002665 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002666 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002667 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002668 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002669 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002670 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002671 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002672
Chris Wilson3272db52016-08-04 16:32:32 +01002673 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002674 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002675 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002676 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002677 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002678 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002679
2680 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002681}
2682
2683static void ggtt_unbind_vma(struct i915_vma *vma)
2684{
Chris Wilson49d73912016-11-29 09:50:08 +00002685 struct drm_i915_private *i915 = vma->vm->i915;
Chris Wilson9c870d02016-10-24 13:42:15 +01002686 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002687 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002688
Chris Wilson9c870d02016-10-24 13:42:15 +01002689 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2690 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002691 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002692 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002693 intel_runtime_pm_put(i915);
2694 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002695
Chris Wilson3272db52016-08-04 16:32:32 +01002696 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002697 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002698 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002699}
2700
Chris Wilson03ac84f2016-10-28 13:58:36 +01002701void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2702 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002703{
David Weinehall52a05c32016-08-22 13:32:44 +03002704 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2705 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002706 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002707
Chris Wilson307dc252016-08-05 10:14:12 +01002708 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002709 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002710 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2711 /* Wait a bit, in hopes it avoids the hang */
2712 udelay(10);
2713 }
2714 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002715
Chris Wilson03ac84f2016-10-28 13:58:36 +01002716 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002717}
Daniel Vetter644ec022012-03-26 09:45:40 +02002718
Chris Wilson42d6ab42012-07-26 11:49:32 +01002719static void i915_gtt_color_adjust(struct drm_mm_node *node,
2720 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002721 u64 *start,
2722 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002723{
2724 if (node->color != color)
2725 *start += 4096;
2726
Chris Wilson2a1d7752016-07-26 12:01:51 +01002727 node = list_first_entry_or_null(&node->node_list,
2728 struct drm_mm_node,
2729 node_list);
2730 if (node && node->allocated && node->color != color)
2731 *end -= 4096;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002732}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002733
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002734int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002735{
Ben Widawskye78891c2013-01-25 16:41:04 -08002736 /* Let GEM Manage all of the aperture.
2737 *
2738 * However, leave one page at the end still bound to the scratch page.
2739 * There are a number of places where the hardware apparently prefetches
2740 * past the end of the object, and we've seen multiple hangs with the
2741 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2742 * aperture. One page should be enough to keep any prefetching inside
2743 * of the aperture.
2744 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002745 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002746 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002747 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002748 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002749 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002750
Zhi Wangb02d22a2016-06-16 08:06:59 -04002751 ret = intel_vgt_balloon(dev_priv);
2752 if (ret)
2753 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002754
Chris Wilson95374d72016-10-12 10:05:20 +01002755 /* Reserve a mappable slot for our lockless error capture */
2756 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2757 &ggtt->error_capture,
Chris Wilson85fd4f52016-12-05 14:29:36 +00002758 4096, 0,
2759 I915_COLOR_UNEVICTABLE,
Chris Wilson95374d72016-10-12 10:05:20 +01002760 0, ggtt->mappable_end,
2761 0, 0);
2762 if (ret)
2763 return ret;
2764
Chris Wilsoned2f3452012-11-15 11:32:19 +00002765 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002766 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002767 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2768 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002769 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002770 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002771 }
2772
2773 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002774 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002775 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002776
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002777 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002778 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002779 if (!ppgtt) {
2780 ret = -ENOMEM;
2781 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002782 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002783
Chris Wilson95374d72016-10-12 10:05:20 +01002784 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2785 if (ret)
2786 goto err_ppgtt;
2787
2788 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002789 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2790 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002791 if (ret)
2792 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002793 }
2794
2795 ppgtt->base.clear_range(&ppgtt->base,
2796 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002797 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002798
Daniel Vetterfa76da32014-08-06 20:19:54 +02002799 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002800 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2801 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002802 }
2803
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002804 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002805
2806err_ppgtt_cleanup:
2807 ppgtt->base.cleanup(&ppgtt->base);
2808err_ppgtt:
2809 kfree(ppgtt);
2810err:
2811 drm_mm_remove_node(&ggtt->error_capture);
2812 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002813}
2814
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002815/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002816 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002817 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002818 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002819void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002820{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002821 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002822
Daniel Vetter70e32542014-08-06 15:04:57 +02002823 if (dev_priv->mm.aliasing_ppgtt) {
2824 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002825 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002826 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002827 }
2828
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002829 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002830
Chris Wilson95374d72016-10-12 10:05:20 +01002831 if (drm_mm_node_allocated(&ggtt->error_capture))
2832 drm_mm_remove_node(&ggtt->error_capture);
2833
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002834 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002835 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002836
Matthew Aulded9724d2016-11-17 21:04:10 +00002837 mutex_lock(&dev_priv->drm.struct_mutex);
2838 i915_address_space_fini(&ggtt->base);
2839 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002840 }
2841
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002842 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002843
2844 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002845 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002846}
Daniel Vetter70e32542014-08-06 15:04:57 +02002847
Daniel Vetter2c642b02015-04-14 17:35:26 +02002848static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002849{
2850 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2851 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2852 return snb_gmch_ctl << 20;
2853}
2854
Daniel Vetter2c642b02015-04-14 17:35:26 +02002855static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002856{
2857 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2858 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2859 if (bdw_gmch_ctl)
2860 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002861
2862#ifdef CONFIG_X86_32
2863 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2864 if (bdw_gmch_ctl > 4)
2865 bdw_gmch_ctl = 4;
2866#endif
2867
Ben Widawsky9459d252013-11-03 16:53:55 -08002868 return bdw_gmch_ctl << 20;
2869}
2870
Daniel Vetter2c642b02015-04-14 17:35:26 +02002871static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002872{
2873 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2874 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2875
2876 if (gmch_ctrl)
2877 return 1 << (20 + gmch_ctrl);
2878
2879 return 0;
2880}
2881
Daniel Vetter2c642b02015-04-14 17:35:26 +02002882static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002883{
2884 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2885 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2886 return snb_gmch_ctl << 25; /* 32 MB units */
2887}
2888
Daniel Vetter2c642b02015-04-14 17:35:26 +02002889static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002890{
2891 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2892 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2893 return bdw_gmch_ctl << 25; /* 32 MB units */
2894}
2895
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002896static size_t chv_get_stolen_size(u16 gmch_ctrl)
2897{
2898 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2899 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2900
2901 /*
2902 * 0x0 to 0x10: 32MB increments starting at 0MB
2903 * 0x11 to 0x16: 4MB increments starting at 8MB
2904 * 0x17 to 0x1d: 4MB increments start at 36MB
2905 */
2906 if (gmch_ctrl < 0x11)
2907 return gmch_ctrl << 25;
2908 else if (gmch_ctrl < 0x17)
2909 return (gmch_ctrl - 0x11 + 2) << 22;
2910 else
2911 return (gmch_ctrl - 0x17 + 9) << 22;
2912}
2913
Damien Lespiau66375012014-01-09 18:02:46 +00002914static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2915{
2916 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2917 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2918
2919 if (gen9_gmch_ctl < 0xf0)
2920 return gen9_gmch_ctl << 25; /* 32 MB units */
2921 else
2922 /* 4MB increments starting at 0xf0 for 4MB */
2923 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2924}
2925
Chris Wilson34c998b2016-08-04 07:52:24 +01002926static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002927{
Chris Wilson49d73912016-11-29 09:50:08 +00002928 struct drm_i915_private *dev_priv = ggtt->base.i915;
2929 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002930 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002931 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002932
2933 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002934 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002935
Imre Deak2a073f892015-03-27 13:07:33 +02002936 /*
2937 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2938 * dropped. For WC mappings in general we have 64 byte burst writes
2939 * when the WC buffer is flushed, so we can't use it, but have to
2940 * resort to an uncached mapping. The WC issue is easily caught by the
2941 * readback check when writing GTT PTE entries.
2942 */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02002943 if (IS_GEN9_LP(dev_priv))
Chris Wilson34c998b2016-08-04 07:52:24 +01002944 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002945 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002946 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002947 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002948 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002949 return -ENOMEM;
2950 }
2951
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00002952 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002953 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002954 DRM_ERROR("Scratch setup failed\n");
2955 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002956 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002957 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002958 }
2959
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002960 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002961}
2962
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002963/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2964 * bits. When using advanced contexts each context stores its own PAT, but
2965 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002966static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002967{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002968 uint64_t pat;
2969
2970 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2971 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2972 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2973 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2974 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2975 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2976 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2977 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2978
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002979 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002980 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2981 * so RTL will always use the value corresponding to
2982 * pat_sel = 000".
2983 * So let's disable cache for GGTT to avoid screen corruptions.
2984 * MOCS still can be used though.
2985 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2986 * before this patch, i.e. the same uncached + snooping access
2987 * like on gen6/7 seems to be in effect.
2988 * - So this just fixes blitter/render access. Again it looks
2989 * like it's not just uncached access, but uncached + snooping.
2990 * So we can still hold onto all our assumptions wrt cpu
2991 * clflushing on LLC machines.
2992 */
2993 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2994
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002995 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2996 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002997 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2998 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002999}
3000
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003001static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3002{
3003 uint64_t pat;
3004
3005 /*
3006 * Map WB on BDW to snooped on CHV.
3007 *
3008 * Only the snoop bit has meaning for CHV, the rest is
3009 * ignored.
3010 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003011 * The hardware will never snoop for certain types of accesses:
3012 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3013 * - PPGTT page tables
3014 * - some other special cycles
3015 *
3016 * As with BDW, we also need to consider the following for GT accesses:
3017 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3018 * so RTL will always use the value corresponding to
3019 * pat_sel = 000".
3020 * Which means we must set the snoop bit in PAT entry 0
3021 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003022 */
3023 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3024 GEN8_PPAT(1, 0) |
3025 GEN8_PPAT(2, 0) |
3026 GEN8_PPAT(3, 0) |
3027 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3028 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3029 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3030 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3031
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003032 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3033 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003034}
3035
Chris Wilson34c998b2016-08-04 07:52:24 +01003036static void gen6_gmch_remove(struct i915_address_space *vm)
3037{
3038 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3039
3040 iounmap(ggtt->gsm);
Chris Wilson49d73912016-11-29 09:50:08 +00003041 cleanup_scratch_page(vm->i915, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003042}
3043
Joonas Lahtinend507d732016-03-18 10:42:58 +02003044static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003045{
Chris Wilson49d73912016-11-29 09:50:08 +00003046 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003047 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003048 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003049 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003050
3051 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003052 ggtt->mappable_base = pci_resource_start(pdev, 2);
3053 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003054
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003055 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3056 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003057
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003058 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003059
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003060 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003061 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003062 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003063 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003064 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003065 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003066 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003067 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003068 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003069 }
Ben Widawsky63340132013-11-04 19:32:22 -08003070
Chris Wilson34c998b2016-08-04 07:52:24 +01003071 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003072
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003073 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003074 chv_setup_private_ppat(dev_priv);
3075 else
3076 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003077
Chris Wilson34c998b2016-08-04 07:52:24 +01003078 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003079 ggtt->base.bind_vma = ggtt_bind_vma;
3080 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303081 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003082 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003083 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003084 ggtt->base.clear_range = gen8_ggtt_clear_range;
3085
3086 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3087 if (IS_CHERRYVIEW(dev_priv))
3088 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3089
Chris 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 Wilson49d73912016-11-29 09:50:08 +00003095 struct drm_i915_private *dev_priv = ggtt->base.i915;
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003096 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 Wilson49d73912016-11-29 09:50:08 +00003148 struct drm_i915_private *dev_priv = ggtt->base.i915;
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 Wilson49d73912016-11-29 09:50:08 +00003183 ggtt->base.i915 = dev_priv;
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) {
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02003313 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(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