blob: 57e6177c0a34fb95550e89b4374fb0e2bc4f66ef [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
Mika Kuoppalac114f762015-06-25 18:35:13 +0300323static int __setup_page_dma(struct drm_device *dev,
324 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000325{
David Weinehallc49d13e2016-08-22 13:32:42 +0300326 struct device *kdev = &dev->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
Mika Kuoppalac114f762015-06-25 18:35:13 +0300343static int setup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
344{
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100345 return __setup_page_dma(dev, p, I915_GFP_DMA);
Mika Kuoppalac114f762015-06-25 18:35:13 +0300346}
347
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300348static void cleanup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
349{
David Weinehall52a05c32016-08-22 13:32:44 +0300350 struct pci_dev *pdev = dev->pdev;
351
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300352 if (WARN_ON(!p->page))
353 return;
354
David Weinehall52a05c32016-08-22 13:32:44 +0300355 dma_unmap_page(&pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300356 __free_page(p->page);
357 memset(p, 0, sizeof(*p));
358}
359
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300360static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300361{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300362 return kmap_atomic(p->page);
363}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300364
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300365/* We use the flushing unmap only with ppgtt structures:
366 * page directories, page tables and scratch pages.
367 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100368static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300369{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300370 /* There are only few exceptions for gen >=6. chv and bxt.
371 * And we are not sure about the latter so play safe for now.
372 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100373 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300374 drm_clflush_virt_range(vaddr, PAGE_SIZE);
375
376 kunmap_atomic(vaddr);
377}
378
Mika Kuoppala567047b2015-06-25 18:35:12 +0300379#define kmap_px(px) kmap_page_dma(px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100380#define kunmap_px(ppgtt, vaddr) \
381 kunmap_page_dma(to_i915((ppgtt)->base.dev), (vaddr))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300382
Mika Kuoppala567047b2015-06-25 18:35:12 +0300383#define setup_px(dev, px) setup_page_dma((dev), px_base(px))
384#define cleanup_px(dev, px) cleanup_page_dma((dev), px_base(px))
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100385#define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
386#define fill32_px(dev_priv, px, v) \
387 fill_page_dma_32((dev_priv), px_base(px), (v))
Mika Kuoppala567047b2015-06-25 18:35:12 +0300388
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100389static void fill_page_dma(struct drm_i915_private *dev_priv,
390 struct i915_page_dma *p, const uint64_t val)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300391{
392 int i;
393 uint64_t * const vaddr = kmap_page_dma(p);
394
395 for (i = 0; i < 512; i++)
396 vaddr[i] = val;
397
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100398 kunmap_page_dma(dev_priv, vaddr);
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300399}
400
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100401static void fill_page_dma_32(struct drm_i915_private *dev_priv,
402 struct i915_page_dma *p, const uint32_t val32)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300403{
404 uint64_t v = val32;
405
406 v = v << 32 | val32;
407
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100408 fill_page_dma(dev_priv, p, v);
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300409}
410
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100411static int
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100412setup_scratch_page(struct drm_device *dev,
413 struct i915_page_dma *scratch,
414 gfp_t gfp)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300415{
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100416 return __setup_page_dma(dev, scratch, gfp | __GFP_ZERO);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300417}
418
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100419static void cleanup_scratch_page(struct drm_device *dev,
420 struct i915_page_dma *scratch)
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300421{
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100422 cleanup_page_dma(dev, scratch);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300423}
424
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300425static struct i915_page_table *alloc_pt(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000426{
Michel Thierryec565b32015-04-08 12:13:23 +0100427 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000428 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
429 GEN8_PTES : GEN6_PTES;
430 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000431
432 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
433 if (!pt)
434 return ERR_PTR(-ENOMEM);
435
Ben Widawsky678d96f2015-03-16 16:00:56 +0000436 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
437 GFP_KERNEL);
438
439 if (!pt->used_ptes)
440 goto fail_bitmap;
441
Mika Kuoppala567047b2015-06-25 18:35:12 +0300442 ret = setup_px(dev, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000443 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300444 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000445
446 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000447
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300448fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000449 kfree(pt->used_ptes);
450fail_bitmap:
451 kfree(pt);
452
453 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000454}
455
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300456static void free_pt(struct drm_device *dev, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000457{
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300458 cleanup_px(dev, pt);
459 kfree(pt->used_ptes);
460 kfree(pt);
461}
462
463static void gen8_initialize_pt(struct i915_address_space *vm,
464 struct i915_page_table *pt)
465{
466 gen8_pte_t scratch_pte;
467
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100468 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200469 I915_CACHE_LLC);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300470
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100471 fill_px(to_i915(vm->dev), pt, scratch_pte);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300472}
473
474static void gen6_initialize_pt(struct i915_address_space *vm,
475 struct i915_page_table *pt)
476{
477 gen6_pte_t scratch_pte;
478
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100479 WARN_ON(vm->scratch_page.daddr == 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300480
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100481 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200482 I915_CACHE_LLC, 0);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300483
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100484 fill32_px(to_i915(vm->dev), pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000485}
486
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300487static struct i915_page_directory *alloc_pd(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000488{
Michel Thierryec565b32015-04-08 12:13:23 +0100489 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100490 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000491
492 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
493 if (!pd)
494 return ERR_PTR(-ENOMEM);
495
Michel Thierry33c88192015-04-08 12:13:33 +0100496 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
497 sizeof(*pd->used_pdes), GFP_KERNEL);
498 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300499 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100500
Mika Kuoppala567047b2015-06-25 18:35:12 +0300501 ret = setup_px(dev, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100502 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300503 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100504
Ben Widawsky06fda602015-02-24 16:22:36 +0000505 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100506
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300507fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100508 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300509fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100510 kfree(pd);
511
512 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000513}
514
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300515static void free_pd(struct drm_device *dev, struct i915_page_directory *pd)
516{
517 if (px_page(pd)) {
518 cleanup_px(dev, pd);
519 kfree(pd->used_pdes);
520 kfree(pd);
521 }
522}
523
524static void gen8_initialize_pd(struct i915_address_space *vm,
525 struct i915_page_directory *pd)
526{
527 gen8_pde_t scratch_pde;
528
529 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
530
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100531 fill_px(to_i915(vm->dev), pd, scratch_pde);
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300532}
533
Michel Thierry6ac18502015-07-29 17:23:46 +0100534static int __pdp_init(struct drm_device *dev,
535 struct i915_page_directory_pointer *pdp)
536{
537 size_t pdpes = I915_PDPES_PER_PDP(dev);
538
539 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
540 sizeof(unsigned long),
541 GFP_KERNEL);
542 if (!pdp->used_pdpes)
543 return -ENOMEM;
544
545 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
546 GFP_KERNEL);
547 if (!pdp->page_directory) {
548 kfree(pdp->used_pdpes);
549 /* the PDP might be the statically allocated top level. Keep it
550 * as clean as possible */
551 pdp->used_pdpes = NULL;
552 return -ENOMEM;
553 }
554
555 return 0;
556}
557
558static void __pdp_fini(struct i915_page_directory_pointer *pdp)
559{
560 kfree(pdp->used_pdpes);
561 kfree(pdp->page_directory);
562 pdp->page_directory = NULL;
563}
564
Michel Thierry762d9932015-07-30 11:05:29 +0100565static struct
566i915_page_directory_pointer *alloc_pdp(struct drm_device *dev)
567{
568 struct i915_page_directory_pointer *pdp;
569 int ret = -ENOMEM;
570
571 WARN_ON(!USES_FULL_48BIT_PPGTT(dev));
572
573 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
574 if (!pdp)
575 return ERR_PTR(-ENOMEM);
576
577 ret = __pdp_init(dev, pdp);
578 if (ret)
579 goto fail_bitmap;
580
581 ret = setup_px(dev, pdp);
582 if (ret)
583 goto fail_page_m;
584
585 return pdp;
586
587fail_page_m:
588 __pdp_fini(pdp);
589fail_bitmap:
590 kfree(pdp);
591
592 return ERR_PTR(ret);
593}
594
Michel Thierry6ac18502015-07-29 17:23:46 +0100595static void free_pdp(struct drm_device *dev,
596 struct i915_page_directory_pointer *pdp)
597{
598 __pdp_fini(pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100599 if (USES_FULL_48BIT_PPGTT(dev)) {
600 cleanup_px(dev, pdp);
601 kfree(pdp);
602 }
603}
604
Michel Thierry69ab76f2015-07-29 17:23:55 +0100605static void gen8_initialize_pdp(struct i915_address_space *vm,
606 struct i915_page_directory_pointer *pdp)
607{
608 gen8_ppgtt_pdpe_t scratch_pdpe;
609
610 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
611
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100612 fill_px(to_i915(vm->dev), pdp, scratch_pdpe);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100613}
614
615static void gen8_initialize_pml4(struct i915_address_space *vm,
616 struct i915_pml4 *pml4)
617{
618 gen8_ppgtt_pml4e_t scratch_pml4e;
619
620 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
621 I915_CACHE_LLC);
622
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100623 fill_px(to_i915(vm->dev), pml4, scratch_pml4e);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100624}
625
Michel Thierry762d9932015-07-30 11:05:29 +0100626static void
627gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
628 struct i915_page_directory_pointer *pdp,
629 struct i915_page_directory *pd,
630 int index)
631{
632 gen8_ppgtt_pdpe_t *page_directorypo;
633
634 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
635 return;
636
637 page_directorypo = kmap_px(pdp);
638 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
639 kunmap_px(ppgtt, page_directorypo);
640}
641
642static void
643gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
644 struct i915_pml4 *pml4,
645 struct i915_page_directory_pointer *pdp,
646 int index)
647{
648 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
649
650 WARN_ON(!USES_FULL_48BIT_PPGTT(ppgtt->base.dev));
651 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
652 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100653}
654
Ben Widawsky94e409c2013-11-04 22:29:36 -0800655/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100656static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100657 unsigned entry,
658 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800659{
Chris Wilson7e37f882016-08-02 22:50:21 +0100660 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000661 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800662 int ret;
663
664 BUG_ON(entry >= 4);
665
John Harrison5fb9de12015-05-29 17:44:07 +0100666 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800667 if (ret)
668 return ret;
669
Chris Wilsonb5321f32016-08-02 22:50:18 +0100670 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
671 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
672 intel_ring_emit(ring, upper_32_bits(addr));
673 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
674 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
675 intel_ring_emit(ring, lower_32_bits(addr));
676 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800677
678 return 0;
679}
680
Michel Thierry2dba3232015-07-30 11:06:23 +0100681static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
682 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800683{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800684 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800685
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100686 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300687 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
688
John Harrisone85b26d2015-05-29 17:43:56 +0100689 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800690 if (ret)
691 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800692 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800693
Ben Widawskyeeb94882013-12-06 14:11:10 -0800694 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800695}
696
Michel Thierry2dba3232015-07-30 11:06:23 +0100697static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
698 struct drm_i915_gem_request *req)
699{
700 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
701}
702
Mika Kuoppalafce93752016-10-31 17:24:46 +0200703/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
704 * the page table structures, we mark them dirty so that
705 * context switching/execlist queuing code takes extra steps
706 * to ensure that tlbs are flushed.
707 */
708static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
709{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000710 ppgtt->pd_dirty_rings = INTEL_INFO(to_i915(ppgtt->base.dev))->ring_mask;
Mika Kuoppalafce93752016-10-31 17:24:46 +0200711}
712
Michał Winiarski2ce51792016-10-13 14:02:42 +0200713/* Removes entries from a single page table, releasing it if it's empty.
714 * Caller can use the return value to update higher-level entries.
715 */
716static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200717 struct i915_page_table *pt,
718 uint64_t start,
719 uint64_t length)
Ben Widawsky459108b2013-11-02 21:07:23 -0700720{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300721 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200722 unsigned int num_entries = gen8_pte_count(start, length);
Mika Kuoppala37c63932016-11-01 15:27:36 +0200723 unsigned int pte = gen8_pte_index(start);
724 unsigned int pte_end = pte + num_entries;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200725 gen8_pte_t *pt_vaddr;
726 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
727 I915_CACHE_LLC);
728
729 if (WARN_ON(!px_page(pt)))
Michał Winiarski2ce51792016-10-13 14:02:42 +0200730 return false;
Ben Widawsky459108b2013-11-02 21:07:23 -0700731
Mika Kuoppala37c63932016-11-01 15:27:36 +0200732 GEM_BUG_ON(pte_end > GEN8_PTES);
733
734 bitmap_clear(pt->used_ptes, pte, num_entries);
Ben Widawsky06fda602015-02-24 16:22:36 +0000735
Michał Winiarski2ce51792016-10-13 14:02:42 +0200736 if (bitmap_empty(pt->used_ptes, GEN8_PTES)) {
737 free_pt(vm->dev, pt);
738 return true;
739 }
740
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200741 pt_vaddr = kmap_px(pt);
Ben Widawsky06fda602015-02-24 16:22:36 +0000742
Mika Kuoppala37c63932016-11-01 15:27:36 +0200743 while (pte < pte_end)
744 pt_vaddr[pte++] = scratch_pte;
Ben Widawsky06fda602015-02-24 16:22:36 +0000745
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200746 kunmap_px(ppgtt, pt_vaddr);
Michał Winiarski2ce51792016-10-13 14:02:42 +0200747
748 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200749}
750
Michał Winiarski2ce51792016-10-13 14:02:42 +0200751/* Removes entries from a single page dir, releasing it if it's empty.
752 * Caller can use the return value to update higher-level entries
753 */
754static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200755 struct i915_page_directory *pd,
756 uint64_t start,
757 uint64_t length)
758{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200759 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200760 struct i915_page_table *pt;
761 uint64_t pde;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200762 gen8_pde_t *pde_vaddr;
763 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
764 I915_CACHE_LLC);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200765
766 gen8_for_each_pde(pt, pd, start, length, pde) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000767 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100768 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000769
Michał Winiarski2ce51792016-10-13 14:02:42 +0200770 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
771 __clear_bit(pde, pd->used_pdes);
772 pde_vaddr = kmap_px(pd);
773 pde_vaddr[pde] = scratch_pde;
774 kunmap_px(ppgtt, pde_vaddr);
775 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200776 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200777
778 if (bitmap_empty(pd->used_pdes, I915_PDES)) {
779 free_pd(vm->dev, pd);
780 return true;
781 }
782
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);
807 if (USES_FULL_48BIT_PPGTT(vm->dev)) {
808 pdpe_vaddr = kmap_px(pdp);
809 pdpe_vaddr[pdpe] = scratch_pdpe;
810 kunmap_px(ppgtt, pdpe_vaddr);
811 }
812 }
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200813 }
Michał Winiarski2ce51792016-10-13 14:02:42 +0200814
Mika Kuoppalafce93752016-10-31 17:24:46 +0200815 mark_tlbs_dirty(ppgtt);
816
Michał Winiarski2ce51792016-10-13 14:02:42 +0200817 if (USES_FULL_48BIT_PPGTT(vm->dev) &&
818 bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(vm->dev))) {
819 free_pdp(vm->dev, pdp);
820 return true;
821 }
822
823 return false;
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200824}
Ben Widawsky459108b2013-11-02 21:07:23 -0700825
Michał Winiarski2ce51792016-10-13 14:02:42 +0200826/* Removes entries from a single pml4.
827 * This is the top-level structure in 4-level page tables used on gen8+.
828 * Empty entries are always scratch pml4e.
829 */
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200830static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
831 struct i915_pml4 *pml4,
832 uint64_t start,
833 uint64_t length)
834{
Michał Winiarski2ce51792016-10-13 14:02:42 +0200835 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200836 struct i915_page_directory_pointer *pdp;
837 uint64_t pml4e;
Michał Winiarski2ce51792016-10-13 14:02:42 +0200838 gen8_ppgtt_pml4e_t *pml4e_vaddr;
839 gen8_ppgtt_pml4e_t scratch_pml4e =
840 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC);
841
842 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->dev));
Ben Widawsky459108b2013-11-02 21:07:23 -0700843
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200844 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
845 if (WARN_ON(!pml4->pdps[pml4e]))
846 break;
Ben Widawsky459108b2013-11-02 21:07:23 -0700847
Michał Winiarski2ce51792016-10-13 14:02:42 +0200848 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
849 __clear_bit(pml4e, pml4->used_pml4es);
850 pml4e_vaddr = kmap_px(pml4);
851 pml4e_vaddr[pml4e] = scratch_pml4e;
852 kunmap_px(ppgtt, pml4e_vaddr);
853 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700854 }
855}
856
Michel Thierryf9b5b782015-07-30 11:02:49 +0100857static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200858 uint64_t start, uint64_t length)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700859{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300860 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100861
Michał Winiarskid209b9c2016-10-13 14:02:41 +0200862 if (USES_FULL_48BIT_PPGTT(vm->dev))
863 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
864 else
865 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100866}
867
868static void
869gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
870 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100871 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100872 uint64_t start,
873 enum i915_cache_level cache_level)
874{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300875 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000876 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100877 unsigned pdpe = gen8_pdpe_index(start);
878 unsigned pde = gen8_pde_index(start);
879 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700880
Chris Wilson6f1cc992013-12-31 15:50:31 +0000881 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700882
Michel Thierry3387d432015-08-03 09:52:47 +0100883 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000884 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100885 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100886 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300887 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000888 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800889
890 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100891 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Michał Winiarski4fb84d92016-10-13 14:02:40 +0200892 cache_level);
Michel Thierry07749ef2015-03-16 16:00:54 +0000893 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300894 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000895 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000896 if (++pde == I915_PDES) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100897 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
898 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800899 pde = 0;
900 }
901 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700902 }
903 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300904
905 if (pt_vaddr)
906 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700907}
908
Michel Thierryf9b5b782015-07-30 11:02:49 +0100909static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
910 struct sg_table *pages,
911 uint64_t start,
912 enum i915_cache_level cache_level,
913 u32 unused)
914{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300915 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100916 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100917
Michel Thierry3387d432015-08-03 09:52:47 +0100918 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100919
920 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
921 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
922 cache_level);
923 } else {
924 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000925 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100926 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
927
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000928 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100929 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
930 start, cache_level);
931 }
932 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100933}
934
Michel Thierryf37c0502015-06-10 17:46:39 +0100935static void gen8_free_page_tables(struct drm_device *dev,
936 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800937{
938 int i;
939
Mika Kuoppala567047b2015-06-25 18:35:12 +0300940 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800941 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800942
Michel Thierry33c88192015-04-08 12:13:33 +0100943 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000944 if (WARN_ON(!pd->page_table[i]))
945 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800946
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300947 free_pt(dev, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000948 pd->page_table[i] = NULL;
949 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000950}
951
Mika Kuoppala8776f022015-06-30 18:16:40 +0300952static int gen8_init_scratch(struct i915_address_space *vm)
953{
954 struct drm_device *dev = vm->dev;
Matthew Auld64c050d2016-04-27 13:19:25 +0100955 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300956
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +0100957 ret = setup_scratch_page(dev, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100958 if (ret)
959 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300960
961 vm->scratch_pt = alloc_pt(dev);
962 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100963 ret = PTR_ERR(vm->scratch_pt);
964 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300965 }
966
967 vm->scratch_pd = alloc_pd(dev);
968 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100969 ret = PTR_ERR(vm->scratch_pd);
970 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300971 }
972
Michel Thierry69ab76f2015-07-29 17:23:55 +0100973 if (USES_FULL_48BIT_PPGTT(dev)) {
974 vm->scratch_pdp = alloc_pdp(dev);
975 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100976 ret = PTR_ERR(vm->scratch_pdp);
977 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100978 }
979 }
980
Mika Kuoppala8776f022015-06-30 18:16:40 +0300981 gen8_initialize_pt(vm, vm->scratch_pt);
982 gen8_initialize_pd(vm, vm->scratch_pd);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100983 if (USES_FULL_48BIT_PPGTT(dev))
984 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300985
986 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100987
988free_pd:
989 free_pd(dev, vm->scratch_pd);
990free_pt:
991 free_pt(dev, vm->scratch_pt);
992free_scratch_page:
Chris Wilson8bcdd0f72016-08-22 08:44:30 +0100993 cleanup_scratch_page(dev, &vm->scratch_page);
Matthew Auld64c050d2016-04-27 13:19:25 +0100994
995 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300996}
997
Zhiyuan Lv650da342015-08-28 15:41:18 +0800998static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
999{
1000 enum vgt_g2v_type msg;
Matthew Aulddf285642016-04-22 12:09:25 +01001001 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Zhiyuan Lv650da342015-08-28 15:41:18 +08001002 int i;
1003
Matthew Aulddf285642016-04-22 12:09:25 +01001004 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +08001005 u64 daddr = px_dma(&ppgtt->pml4);
1006
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001007 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1008 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001009
1010 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1011 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1012 } else {
1013 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1014 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1015
Ville Syrjäläab75bb52015-11-04 23:20:12 +02001016 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1017 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +08001018 }
1019
1020 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1021 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1022 }
1023
1024 I915_WRITE(vgtif_reg(g2v_notify), msg);
1025
1026 return 0;
1027}
1028
Mika Kuoppala8776f022015-06-30 18:16:40 +03001029static void gen8_free_scratch(struct i915_address_space *vm)
1030{
1031 struct drm_device *dev = vm->dev;
1032
Michel Thierry69ab76f2015-07-29 17:23:55 +01001033 if (USES_FULL_48BIT_PPGTT(dev))
1034 free_pdp(dev, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001035 free_pd(dev, vm->scratch_pd);
1036 free_pt(dev, vm->scratch_pt);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001037 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03001038}
1039
Michel Thierry762d9932015-07-30 11:05:29 +01001040static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
1041 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001042{
1043 int i;
1044
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001045 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
1046 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +00001047 continue;
1048
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001049 gen8_free_page_tables(dev, pdp->page_directory[i]);
1050 free_pd(dev, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -08001051 }
Michel Thierry69876be2015-04-08 12:13:27 +01001052
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001053 free_pdp(dev, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001054}
1055
1056static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1057{
1058 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
1064 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
1065 }
1066
1067 cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
1068}
1069
1070static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1071{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001072 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001073
Chris Wilsonc0336662016-05-06 15:40:21 +01001074 if (intel_vgpu_active(to_i915(vm->dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001075 gen8_ppgtt_notify_vgt(ppgtt, false);
1076
Michel Thierry762d9932015-07-30 11:05:29 +01001077 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
1078 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
1079 else
1080 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001081
Mika Kuoppala8776f022015-06-30 18:16:40 +03001082 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001083}
1084
Michel Thierryd7b26332015-04-08 12:13:34 +01001085/**
1086 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001087 * @vm: Master vm structure.
1088 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001089 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001090 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001091 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1092 * caller to free on error.
1093 *
1094 * Allocate the required number of page tables. Extremely similar to
1095 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1096 * the page directory boundary (instead of the page directory pointer). That
1097 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1098 * possible, and likely that the caller will need to use multiple calls of this
1099 * function to achieve the appropriate allocation.
1100 *
1101 * Return: 0 if success; negative error code otherwise.
1102 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001103static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001104 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001105 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001106 uint64_t length,
1107 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001108{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001109 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001110 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001111 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001112
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001113 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001114 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001115 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001116 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001117 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001118 continue;
1119 }
1120
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001121 pt = alloc_pt(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001122 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001123 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001124
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001125 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001126 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001127 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001128 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001129 }
1130
1131 return 0;
1132
1133unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001134 for_each_set_bit(pde, new_pts, I915_PDES)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001135 free_pt(dev, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001136
1137 return -ENOMEM;
1138}
1139
Michel Thierryd7b26332015-04-08 12:13:34 +01001140/**
1141 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001142 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001143 * @pdp: Page directory pointer for this address range.
1144 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001145 * @length: Size of the allocations.
1146 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001147 * caller to free on error.
1148 *
1149 * Allocate the required number of page directories starting at the pde index of
1150 * @start, and ending at the pde index @start + @length. This function will skip
1151 * over already allocated page directories within the range, and only allocate
1152 * new ones, setting the appropriate pointer within the pdp as well as the
1153 * correct position in the bitmap @new_pds.
1154 *
1155 * The function will only allocate the pages within the range for a give page
1156 * directory pointer. In other words, if @start + @length straddles a virtually
1157 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1158 * required by the caller, This is not currently possible, and the BUG in the
1159 * code will prevent it.
1160 *
1161 * Return: 0 if success; negative error code otherwise.
1162 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001163static int
1164gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1165 struct i915_page_directory_pointer *pdp,
1166 uint64_t start,
1167 uint64_t length,
1168 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001169{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001170 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001171 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001172 uint32_t pdpe;
Michel Thierry6ac18502015-07-29 17:23:46 +01001173 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001174
Michel Thierry6ac18502015-07-29 17:23:46 +01001175 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001176
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001177 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001178 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001179 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001180
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001181 pd = alloc_pd(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001182 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001183 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001184
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001185 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001186 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001187 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001188 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001189 }
1190
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001191 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001192
1193unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001194 for_each_set_bit(pdpe, new_pds, pdpes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001195 free_pd(dev, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001196
1197 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001198}
1199
Michel Thierry762d9932015-07-30 11:05:29 +01001200/**
1201 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1202 * @vm: Master vm structure.
1203 * @pml4: Page map level 4 for this address range.
1204 * @start: Starting virtual address to begin allocations.
1205 * @length: Size of the allocations.
1206 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1207 * caller to free on error.
1208 *
1209 * Allocate the required number of page directory pointers. Extremely similar to
1210 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1211 * The main difference is here we are limited by the pml4 boundary (instead of
1212 * the page directory pointer).
1213 *
1214 * Return: 0 if success; negative error code otherwise.
1215 */
1216static int
1217gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1218 struct i915_pml4 *pml4,
1219 uint64_t start,
1220 uint64_t length,
1221 unsigned long *new_pdps)
1222{
1223 struct drm_device *dev = vm->dev;
1224 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001225 uint32_t pml4e;
1226
1227 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1228
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001229 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001230 if (!test_bit(pml4e, pml4->used_pml4es)) {
1231 pdp = alloc_pdp(dev);
1232 if (IS_ERR(pdp))
1233 goto unwind_out;
1234
Michel Thierry69ab76f2015-07-29 17:23:55 +01001235 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001236 pml4->pdps[pml4e] = pdp;
1237 __set_bit(pml4e, new_pdps);
1238 trace_i915_page_directory_pointer_entry_alloc(vm,
1239 pml4e,
1240 start,
1241 GEN8_PML4E_SHIFT);
1242 }
1243 }
1244
1245 return 0;
1246
1247unwind_out:
1248 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1249 free_pdp(dev, pml4->pdps[pml4e]);
1250
1251 return -ENOMEM;
1252}
1253
Michel Thierryd7b26332015-04-08 12:13:34 +01001254static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001255free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001256{
Michel Thierryd7b26332015-04-08 12:13:34 +01001257 kfree(new_pts);
1258 kfree(new_pds);
1259}
1260
1261/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1262 * of these are based on the number of PDPEs in the system.
1263 */
1264static
1265int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001266 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001267 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001268{
Michel Thierryd7b26332015-04-08 12:13:34 +01001269 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001270 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001271
Michał Winiarski3a41a052015-09-03 19:22:18 +02001272 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001273 if (!pds)
1274 return -ENOMEM;
1275
Michał Winiarski3a41a052015-09-03 19:22:18 +02001276 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1277 GFP_TEMPORARY);
1278 if (!pts)
1279 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001280
1281 *new_pds = pds;
1282 *new_pts = pts;
1283
1284 return 0;
1285
1286err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001287 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001288 return -ENOMEM;
1289}
1290
Michel Thierry762d9932015-07-30 11:05:29 +01001291static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1292 struct i915_page_directory_pointer *pdp,
1293 uint64_t start,
1294 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001295{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001296 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001297 unsigned long *new_page_dirs, *new_page_tables;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001298 struct drm_device *dev = vm->dev;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001299 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001300 const uint64_t orig_start = start;
1301 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001302 uint32_t pdpe;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001303 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001304 int ret;
1305
Michel Thierryd7b26332015-04-08 12:13:34 +01001306 /* Wrap is never okay since we can only represent 48b, and we don't
1307 * actually use the other side of the canonical address space.
1308 */
1309 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001310 return -ENODEV;
1311
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001312 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001313 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001314
Michel Thierry6ac18502015-07-29 17:23:46 +01001315 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001316 if (ret)
1317 return ret;
1318
Michel Thierryd7b26332015-04-08 12:13:34 +01001319 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001320 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1321 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001322 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001323 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001324 return ret;
1325 }
1326
1327 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001328 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001329 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001330 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001331 if (ret)
1332 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001333 }
1334
Michel Thierry33c88192015-04-08 12:13:33 +01001335 start = orig_start;
1336 length = orig_length;
1337
Michel Thierryd7b26332015-04-08 12:13:34 +01001338 /* Allocations have completed successfully, so set the bitmaps, and do
1339 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001340 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001341 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001342 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001343 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001344 uint64_t pd_start = start;
1345 uint32_t pde;
1346
Michel Thierryd7b26332015-04-08 12:13:34 +01001347 /* Every pd should be allocated, we just did that above. */
1348 WARN_ON(!pd);
1349
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001350 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001351 /* Same reasoning as pd */
1352 WARN_ON(!pt);
1353 WARN_ON(!pd_len);
1354 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1355
1356 /* Set our used ptes within the page table */
1357 bitmap_set(pt->used_ptes,
1358 gen8_pte_index(pd_start),
1359 gen8_pte_count(pd_start, pd_len));
1360
1361 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001362 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001363
1364 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001365 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1366 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001367 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1368 gen8_pte_index(start),
1369 gen8_pte_count(start, length),
1370 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001371
1372 /* NB: We haven't yet mapped ptes to pages. At this
1373 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001374 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001375
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001376 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001377 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001378 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001379 }
1380
Michał Winiarski3a41a052015-09-03 19:22:18 +02001381 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001382 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001383 return 0;
1384
1385err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001386 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001387 unsigned long temp;
1388
Michał Winiarski3a41a052015-09-03 19:22:18 +02001389 for_each_set_bit(temp, new_page_tables + pdpe *
1390 BITS_TO_LONGS(I915_PDES), I915_PDES)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001391 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001392 }
1393
Michel Thierry6ac18502015-07-29 17:23:46 +01001394 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001395 free_pd(dev, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001396
Michał Winiarski3a41a052015-09-03 19:22:18 +02001397 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001398 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001399 return ret;
1400}
1401
Michel Thierry762d9932015-07-30 11:05:29 +01001402static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1403 struct i915_pml4 *pml4,
1404 uint64_t start,
1405 uint64_t length)
1406{
1407 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001408 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001409 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001410 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001411 int ret = 0;
1412
1413 /* Do the pml4 allocations first, so we don't need to track the newly
1414 * allocated tables below the pdp */
1415 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1416
1417 /* The pagedirectory and pagetable allocations are done in the shared 3
1418 * and 4 level code. Just allocate the pdps.
1419 */
1420 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1421 new_pdps);
1422 if (ret)
1423 return ret;
1424
1425 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1426 "The allocation has spanned more than 512GB. "
1427 "It is highly likely this is incorrect.");
1428
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001429 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001430 WARN_ON(!pdp);
1431
1432 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1433 if (ret)
1434 goto err_out;
1435
1436 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1437 }
1438
1439 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1440 GEN8_PML4ES_PER_PML4);
1441
1442 return 0;
1443
1444err_out:
1445 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1446 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
1447
1448 return ret;
1449}
1450
1451static int gen8_alloc_va_range(struct i915_address_space *vm,
1452 uint64_t start, uint64_t length)
1453{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001454 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001455
1456 if (USES_FULL_48BIT_PPGTT(vm->dev))
1457 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1458 else
1459 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1460}
1461
Michel Thierryea91e402015-07-29 17:23:57 +01001462static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1463 uint64_t start, uint64_t length,
1464 gen8_pte_t scratch_pte,
1465 struct seq_file *m)
1466{
1467 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001468 uint32_t pdpe;
1469
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001470 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001471 struct i915_page_table *pt;
1472 uint64_t pd_len = length;
1473 uint64_t pd_start = start;
1474 uint32_t pde;
1475
1476 if (!test_bit(pdpe, pdp->used_pdpes))
1477 continue;
1478
1479 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001480 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001481 uint32_t pte;
1482 gen8_pte_t *pt_vaddr;
1483
1484 if (!test_bit(pde, pd->used_pdes))
1485 continue;
1486
1487 pt_vaddr = kmap_px(pt);
1488 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1489 uint64_t va =
1490 (pdpe << GEN8_PDPE_SHIFT) |
1491 (pde << GEN8_PDE_SHIFT) |
1492 (pte << GEN8_PTE_SHIFT);
1493 int i;
1494 bool found = false;
1495
1496 for (i = 0; i < 4; i++)
1497 if (pt_vaddr[pte + i] != scratch_pte)
1498 found = true;
1499 if (!found)
1500 continue;
1501
1502 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1503 for (i = 0; i < 4; i++) {
1504 if (pt_vaddr[pte + i] != scratch_pte)
1505 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1506 else
1507 seq_puts(m, " SCRATCH ");
1508 }
1509 seq_puts(m, "\n");
1510 }
1511 /* don't use kunmap_px, it could trigger
1512 * an unnecessary flush.
1513 */
1514 kunmap_atomic(pt_vaddr);
1515 }
1516 }
1517}
1518
1519static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1520{
1521 struct i915_address_space *vm = &ppgtt->base;
1522 uint64_t start = ppgtt->base.start;
1523 uint64_t length = ppgtt->base.total;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001524 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001525 I915_CACHE_LLC);
Michel Thierryea91e402015-07-29 17:23:57 +01001526
1527 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
1528 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1529 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001530 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001531 struct i915_pml4 *pml4 = &ppgtt->pml4;
1532 struct i915_page_directory_pointer *pdp;
1533
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001534 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001535 if (!test_bit(pml4e, pml4->used_pml4es))
1536 continue;
1537
1538 seq_printf(m, " PML4E #%llu\n", pml4e);
1539 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1540 }
1541 }
1542}
1543
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001544static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1545{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001546 unsigned long *new_page_dirs, *new_page_tables;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001547 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1548 int ret;
1549
1550 /* We allocate temp bitmap for page tables for no gain
1551 * but as this is for init only, lets keep the things simple
1552 */
1553 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1554 if (ret)
1555 return ret;
1556
1557 /* Allocate for all pdps regardless of how the ppgtt
1558 * was defined.
1559 */
1560 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1561 0, 1ULL << 32,
1562 new_page_dirs);
1563 if (!ret)
1564 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1565
Michał Winiarski3a41a052015-09-03 19:22:18 +02001566 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001567
1568 return ret;
1569}
1570
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001571/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001572 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1573 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1574 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1575 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001576 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001577 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001578static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001579{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001580 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001581
Mika Kuoppala8776f022015-06-30 18:16:40 +03001582 ret = gen8_init_scratch(&ppgtt->base);
1583 if (ret)
1584 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001585
Michel Thierryd7b26332015-04-08 12:13:34 +01001586 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001587 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001588 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001589 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001590 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001591 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1592 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001593 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001594
Michel Thierry762d9932015-07-30 11:05:29 +01001595 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
1596 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
1597 if (ret)
1598 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001599
Michel Thierry69ab76f2015-07-29 17:23:55 +01001600 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1601
Michel Thierry762d9932015-07-30 11:05:29 +01001602 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001603 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001604 } else {
Michel Thierry25f50332015-08-07 17:40:19 +01001605 ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001606 if (ret)
1607 goto free_scratch;
1608
1609 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001610 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001611 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1612 0, 0,
1613 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001614
Chris Wilsonc0336662016-05-06 15:40:21 +01001615 if (intel_vgpu_active(to_i915(ppgtt->base.dev))) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001616 ret = gen8_preallocate_top_level_pdps(ppgtt);
1617 if (ret)
1618 goto free_scratch;
1619 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001620 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001621
Chris Wilsonc0336662016-05-06 15:40:21 +01001622 if (intel_vgpu_active(to_i915(ppgtt->base.dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001623 gen8_ppgtt_notify_vgt(ppgtt, true);
1624
Michel Thierryd7b26332015-04-08 12:13:34 +01001625 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001626
1627free_scratch:
1628 gen8_free_scratch(&ppgtt->base);
1629 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001630}
1631
Ben Widawsky87d60b62013-12-06 14:11:29 -08001632static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1633{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001634 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001635 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001636 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001637 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001638 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001639 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001640
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001641 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001642 I915_CACHE_LLC, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001643
Dave Gordon731f74c2016-06-24 19:37:46 +01001644 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001645 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001646 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001647 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001648 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001649 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1650
1651 if (pd_entry != expected)
1652 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1653 pde,
1654 pd_entry,
1655 expected);
1656 seq_printf(m, "\tPDE: %x\n", pd_entry);
1657
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001658 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1659
Michel Thierry07749ef2015-03-16 16:00:54 +00001660 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001661 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001662 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001663 (pte * PAGE_SIZE);
1664 int i;
1665 bool found = false;
1666 for (i = 0; i < 4; i++)
1667 if (pt_vaddr[pte + i] != scratch_pte)
1668 found = true;
1669 if (!found)
1670 continue;
1671
1672 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1673 for (i = 0; i < 4; i++) {
1674 if (pt_vaddr[pte + i] != scratch_pte)
1675 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1676 else
1677 seq_puts(m, " SCRATCH ");
1678 }
1679 seq_puts(m, "\n");
1680 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001681 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001682 }
1683}
1684
Ben Widawsky678d96f2015-03-16 16:00:56 +00001685/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001686static void gen6_write_pde(struct i915_page_directory *pd,
1687 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001688{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001689 /* Caller needs to make sure the write completes if necessary */
1690 struct i915_hw_ppgtt *ppgtt =
1691 container_of(pd, struct i915_hw_ppgtt, pd);
1692 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001693
Mika Kuoppala567047b2015-06-25 18:35:12 +03001694 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001695 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001696
Ben Widawsky678d96f2015-03-16 16:00:56 +00001697 writel(pd_entry, ppgtt->pd_addr + pde);
1698}
Ben Widawsky61973492013-04-08 18:43:54 -07001699
Ben Widawsky678d96f2015-03-16 16:00:56 +00001700/* Write all the page tables found in the ppgtt structure to incrementing page
1701 * directories. */
1702static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001703 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001704 uint32_t start, uint32_t length)
1705{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001706 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001707 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001708 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001709
Dave Gordon731f74c2016-06-24 19:37:46 +01001710 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001711 gen6_write_pde(pd, pde, pt);
1712
1713 /* Make sure write is complete before other code can use this page
1714 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001715 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001716}
1717
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001718static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001719{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001720 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001721
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001722 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001723}
Ben Widawsky61973492013-04-08 18:43:54 -07001724
Ben Widawsky90252e52013-12-06 14:11:12 -08001725static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001726 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001727{
Chris Wilson7e37f882016-08-02 22:50:21 +01001728 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001729 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001730 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001731
Ben Widawsky90252e52013-12-06 14:11:12 -08001732 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001733 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001734 if (ret)
1735 return ret;
1736
John Harrison5fb9de12015-05-29 17:44:07 +01001737 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001738 if (ret)
1739 return ret;
1740
Chris Wilsonb5321f32016-08-02 22:50:18 +01001741 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1742 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1743 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1744 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1745 intel_ring_emit(ring, get_pd_offset(ppgtt));
1746 intel_ring_emit(ring, MI_NOOP);
1747 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001748
1749 return 0;
1750}
1751
Ben Widawsky48a10382013-12-06 14:11:11 -08001752static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001753 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001754{
Chris Wilson7e37f882016-08-02 22:50:21 +01001755 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001756 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001757 int ret;
1758
Ben Widawsky48a10382013-12-06 14:11:11 -08001759 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001760 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001761 if (ret)
1762 return ret;
1763
John Harrison5fb9de12015-05-29 17:44:07 +01001764 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001765 if (ret)
1766 return ret;
1767
Chris Wilsonb5321f32016-08-02 22:50:18 +01001768 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1769 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1770 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1771 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1772 intel_ring_emit(ring, get_pd_offset(ppgtt));
1773 intel_ring_emit(ring, MI_NOOP);
1774 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001775
Ben Widawsky90252e52013-12-06 14:11:12 -08001776 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001777 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001778 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001779 if (ret)
1780 return ret;
1781 }
1782
Ben Widawsky48a10382013-12-06 14:11:11 -08001783 return 0;
1784}
1785
Ben Widawskyeeb94882013-12-06 14:11:10 -08001786static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001787 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001788{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001789 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001790 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001791
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001792 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1793 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001794 return 0;
1795}
1796
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001797static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001798{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001799 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05301800 enum intel_engine_id id;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001801
Akash Goel3b3f1652016-10-13 22:44:48 +05301802 for_each_engine(engine, dev_priv, id) {
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001803 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1804 GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001805 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001806 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001807 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001808}
1809
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001810static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001811{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001812 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001813 uint32_t ecochk, ecobits;
Akash Goel3b3f1652016-10-13 22:44:48 +05301814 enum intel_engine_id id;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001815
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001816 ecobits = I915_READ(GAC_ECO_BITS);
1817 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1818
1819 ecochk = I915_READ(GAM_ECOCHK);
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01001820 if (IS_HASWELL(dev_priv)) {
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001821 ecochk |= ECOCHK_PPGTT_WB_HSW;
1822 } else {
1823 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1824 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1825 }
1826 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001827
Akash Goel3b3f1652016-10-13 22:44:48 +05301828 for_each_engine(engine, dev_priv, id) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001829 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001830 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001831 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001832 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001833}
1834
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00001835static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
Ben Widawsky61973492013-04-08 18:43:54 -07001836{
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001837 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001838
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001839 ecobits = I915_READ(GAC_ECO_BITS);
1840 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1841 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001842
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001843 gab_ctl = I915_READ(GAB_CTL);
1844 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001845
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001846 ecochk = I915_READ(GAM_ECOCHK);
1847 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001848
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001849 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001850}
1851
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001852/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001853static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001854 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001855 uint64_t length)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001856{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001857 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001858 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001859 unsigned first_entry = start >> PAGE_SHIFT;
1860 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001861 unsigned act_pt = first_entry / GEN6_PTES;
1862 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001863 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001864
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01001865 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001866 I915_CACHE_LLC, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001867
Daniel Vetter7bddb012012-02-09 17:15:47 +01001868 while (num_entries) {
1869 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001870 if (last_pte > GEN6_PTES)
1871 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001872
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001873 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001874
1875 for (i = first_pte; i < last_pte; i++)
1876 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001877
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001878 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001879
Daniel Vetter7bddb012012-02-09 17:15:47 +01001880 num_entries -= last_pte - first_pte;
1881 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001882 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001883 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001884}
1885
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001886static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001887 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001888 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301889 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001890{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001891 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001892 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001893 unsigned act_pt = first_entry / GEN6_PTES;
1894 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001895 gen6_pte_t *pt_vaddr = NULL;
1896 struct sgt_iter sgt_iter;
1897 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001898
Dave Gordon85d12252016-05-20 11:54:06 +01001899 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001900 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001901 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001902
Chris Wilsoncc797142013-12-31 15:50:30 +00001903 pt_vaddr[act_pte] =
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001904 vm->pte_encode(addr, cache_level, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301905
Michel Thierry07749ef2015-03-16 16:00:54 +00001906 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001907 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001908 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001909 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001910 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001911 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001912 }
Dave Gordon85d12252016-05-20 11:54:06 +01001913
Chris Wilsoncc797142013-12-31 15:50:30 +00001914 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001915 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001916}
1917
Ben Widawsky678d96f2015-03-16 16:00:56 +00001918static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001919 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001920{
Michel Thierry4933d512015-03-24 15:46:22 +00001921 DECLARE_BITMAP(new_page_tables, I915_PDES);
1922 struct drm_device *dev = vm->dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001923 struct drm_i915_private *dev_priv = to_i915(dev);
1924 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001925 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001926 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001927 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001928 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001929 int ret;
1930
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001931 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1932 return -ENODEV;
1933
1934 start = start_save = start_in;
1935 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001936
1937 bitmap_zero(new_page_tables, I915_PDES);
1938
1939 /* The allocation is done in two stages so that we can bail out with
1940 * minimal amount of pain. The first stage finds new page tables that
1941 * need allocation. The second stage marks use ptes within the page
1942 * tables.
1943 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001944 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001945 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001946 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1947 continue;
1948 }
1949
1950 /* We've already allocated a page table */
1951 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1952
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001953 pt = alloc_pt(dev);
Michel Thierry4933d512015-03-24 15:46:22 +00001954 if (IS_ERR(pt)) {
1955 ret = PTR_ERR(pt);
1956 goto unwind_out;
1957 }
1958
1959 gen6_initialize_pt(vm, pt);
1960
1961 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001962 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001963 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001964 }
1965
1966 start = start_save;
1967 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001968
Dave Gordon731f74c2016-06-24 19:37:46 +01001969 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001970 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1971
1972 bitmap_zero(tmp_bitmap, GEN6_PTES);
1973 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1974 gen6_pte_count(start, length));
1975
Mika Kuoppala966082c2015-06-25 18:35:19 +03001976 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001977 gen6_write_pde(&ppgtt->pd, pde, pt);
1978
Michel Thierry72744cb2015-03-24 15:46:23 +00001979 trace_i915_page_table_entry_map(vm, pde, pt,
1980 gen6_pte_index(start),
1981 gen6_pte_count(start, length),
1982 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001983 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001984 GEN6_PTES);
1985 }
1986
Michel Thierry4933d512015-03-24 15:46:22 +00001987 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1988
1989 /* Make sure write is complete before other code can use this page
1990 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001991 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001992
Ben Widawsky563222a2015-03-19 12:53:28 +00001993 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001994 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001995
1996unwind_out:
1997 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001998 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00001999
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002000 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03002001 free_pt(vm->dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002002 }
2003
2004 mark_tlbs_dirty(ppgtt);
2005 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002006}
2007
Mika Kuoppala8776f022015-06-30 18:16:40 +03002008static int gen6_init_scratch(struct i915_address_space *vm)
2009{
2010 struct drm_device *dev = vm->dev;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002011 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002012
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +01002013 ret = setup_scratch_page(dev, &vm->scratch_page, I915_GFP_DMA);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002014 if (ret)
2015 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +03002016
2017 vm->scratch_pt = alloc_pt(dev);
2018 if (IS_ERR(vm->scratch_pt)) {
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002019 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002020 return PTR_ERR(vm->scratch_pt);
2021 }
2022
2023 gen6_initialize_pt(vm, vm->scratch_pt);
2024
2025 return 0;
2026}
2027
2028static void gen6_free_scratch(struct i915_address_space *vm)
2029{
2030 struct drm_device *dev = vm->dev;
2031
2032 free_pt(dev, vm->scratch_pt);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002033 cleanup_scratch_page(dev, &vm->scratch_page);
Mika Kuoppala8776f022015-06-30 18:16:40 +03002034}
2035
Daniel Vetter061dd492015-04-14 17:35:13 +02002036static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08002037{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03002038 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01002039 struct i915_page_directory *pd = &ppgtt->pd;
2040 struct drm_device *dev = vm->dev;
Michel Thierry09942c62015-04-08 12:13:30 +01002041 struct i915_page_table *pt;
2042 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08002043
Daniel Vetter061dd492015-04-14 17:35:13 +02002044 drm_mm_remove_node(&ppgtt->node);
2045
Dave Gordon731f74c2016-06-24 19:37:46 +01002046 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002047 if (pt != vm->scratch_pt)
Dave Gordon731f74c2016-06-24 19:37:46 +01002048 free_pt(dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00002049
Mika Kuoppala8776f022015-06-30 18:16:40 +03002050 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08002051}
2052
Ben Widawskyb1465202014-02-19 22:05:49 -08002053static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08002054{
Mika Kuoppala8776f022015-06-30 18:16:40 +03002055 struct i915_address_space *vm = &ppgtt->base;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002056 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002057 struct drm_i915_private *dev_priv = to_i915(dev);
2058 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002059 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08002060 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002061
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002062 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2063 * allocator works in address space sizes, so it's multiplied by page
2064 * size. We allocate at the top of the GTT to avoid fragmentation.
2065 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002066 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002067
Mika Kuoppala8776f022015-06-30 18:16:40 +03002068 ret = gen6_init_scratch(vm);
2069 if (ret)
2070 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002071
Ben Widawskye3cc1992013-12-06 14:11:08 -08002072alloc:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002073 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002074 &ppgtt->node, GEN6_PD_SIZE,
2075 GEN6_PD_ALIGN, 0,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002076 0, ggtt->base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07002077 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002078 if (ret == -ENOSPC && !retried) {
Chris Wilsone522ac22016-08-04 16:32:18 +01002079 ret = i915_gem_evict_something(&ggtt->base,
Ben Widawskye3cc1992013-12-06 14:11:08 -08002080 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02002081 I915_CACHE_NONE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002082 0, ggtt->base.total,
Chris Wilsond23db882014-05-23 08:48:08 +02002083 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002084 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002085 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002086
2087 retried = true;
2088 goto alloc;
2089 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002090
Ben Widawskyc8c26622015-01-22 17:01:25 +00002091 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002092 goto err_out;
2093
Ben Widawskyc8c26622015-01-22 17:01:25 +00002094
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002095 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002096 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002097
Ben Widawskyc8c26622015-01-22 17:01:25 +00002098 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002099
2100err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002101 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002102 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002103}
2104
Ben Widawskyb1465202014-02-19 22:05:49 -08002105static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2106{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002107 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002108}
2109
Michel Thierry4933d512015-03-24 15:46:22 +00002110static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2111 uint64_t start, uint64_t length)
2112{
Michel Thierryec565b32015-04-08 12:13:23 +01002113 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002114 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002115
Dave Gordon731f74c2016-06-24 19:37:46 +01002116 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002117 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002118}
2119
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002120static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002121{
2122 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002123 struct drm_i915_private *dev_priv = to_i915(dev);
2124 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 Wilson2bfa9962016-08-04 07:52:25 +01002174 ppgtt->base.dev = &dev_priv->drm;
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
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002194static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
Tim Gored5165eb2016-02-04 11:49:34 +00002195{
Tim Gored5165eb2016-02-04 11:49:34 +00002196 /* This function is for gtt related workarounds. This function is
2197 * called on driver load and after a GPU reset, so you can place
2198 * workarounds here even if they get overwritten by GPU reset.
2199 */
2200 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002201 if (IS_BROADWELL(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002202 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +01002203 else if (IS_CHERRYVIEW(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002204 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
Tvrtko Ursulind9486e62016-10-13 11:03:03 +01002205 else if (IS_SKYLAKE(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002206 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002207 else if (IS_BROXTON(dev_priv))
Tim Gored5165eb2016-02-04 11:49:34 +00002208 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2209}
2210
Chris Wilson2bfa9962016-08-04 07:52:25 +01002211static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2212 struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002213 struct drm_i915_file_private *file_priv,
2214 const char *name)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002215{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002216 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002217
Chris Wilson2bfa9962016-08-04 07:52:25 +01002218 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002219 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002220 kref_init(&ppgtt->ref);
Chris Wilson80b204b2016-10-28 13:58:58 +01002221 i915_address_space_init(&ppgtt->base, dev_priv, name);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002222 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002223 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002224
2225 return ret;
2226}
2227
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002228int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
Daniel Vetter82460d92014-08-06 20:19:53 +02002229{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002230 gtt_write_workarounds(dev_priv);
Tim Gored5165eb2016-02-04 11:49:34 +00002231
Thomas Daniel671b50132014-08-20 16:24:50 +01002232 /* In the case of execlists, PPGTT is enabled by the context descriptor
2233 * and the PDPs are contained within the context itself. We don't
2234 * need to do anything here. */
2235 if (i915.enable_execlists)
2236 return 0;
2237
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002238 if (!USES_PPGTT(dev_priv))
Daniel Vetter82460d92014-08-06 20:19:53 +02002239 return 0;
2240
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002241 if (IS_GEN6(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002242 gen6_ppgtt_enable(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002243 else if (IS_GEN7(dev_priv))
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002244 gen7_ppgtt_enable(dev_priv);
2245 else if (INTEL_GEN(dev_priv) >= 8)
2246 gen8_ppgtt_enable(dev_priv);
Daniel Vetter82460d92014-08-06 20:19:53 +02002247 else
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00002248 MISSING_CASE(INTEL_GEN(dev_priv));
Daniel Vetter82460d92014-08-06 20:19:53 +02002249
John Harrison4ad2fd82015-06-18 13:11:20 +01002250 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002251}
John Harrison4ad2fd82015-06-18 13:11:20 +01002252
Daniel Vetter4d884702014-08-06 15:04:47 +02002253struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002254i915_ppgtt_create(struct drm_i915_private *dev_priv,
Chris Wilson80b204b2016-10-28 13:58:58 +01002255 struct drm_i915_file_private *fpriv,
2256 const char *name)
Daniel Vetter4d884702014-08-06 15:04:47 +02002257{
2258 struct i915_hw_ppgtt *ppgtt;
2259 int ret;
2260
2261 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2262 if (!ppgtt)
2263 return ERR_PTR(-ENOMEM);
2264
Chris Wilson80b204b2016-10-28 13:58:58 +01002265 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
Daniel Vetter4d884702014-08-06 15:04:47 +02002266 if (ret) {
2267 kfree(ppgtt);
2268 return ERR_PTR(ret);
2269 }
2270
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002271 trace_i915_ppgtt_create(&ppgtt->base);
2272
Daniel Vetter4d884702014-08-06 15:04:47 +02002273 return ppgtt;
2274}
2275
Daniel Vetteree960be2014-08-06 15:04:45 +02002276void i915_ppgtt_release(struct kref *kref)
2277{
2278 struct i915_hw_ppgtt *ppgtt =
2279 container_of(kref, struct i915_hw_ppgtt, ref);
2280
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002281 trace_i915_ppgtt_release(&ppgtt->base);
2282
Chris Wilson50e046b2016-08-04 07:52:46 +01002283 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002284 WARN_ON(!list_empty(&ppgtt->base.active_list));
2285 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002286 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002287
Chris Wilson80b204b2016-10-28 13:58:58 +01002288 i915_gem_timeline_fini(&ppgtt->base.timeline);
Daniel Vetter19dd1202014-08-06 15:04:55 +02002289 list_del(&ppgtt->base.global_link);
2290 drm_mm_takedown(&ppgtt->base.mm);
2291
Daniel Vetteree960be2014-08-06 15:04:45 +02002292 ppgtt->base.cleanup(&ppgtt->base);
2293 kfree(ppgtt);
2294}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002295
Ben Widawskya81cc002013-01-18 12:30:31 -08002296/* Certain Gen5 chipsets require require idling the GPU before
2297 * unmapping anything from the GTT when VT-d is enabled.
2298 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002299static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002300{
2301#ifdef CONFIG_INTEL_IOMMU
2302 /* Query intel_iommu to see if we need the workaround. Presumably that
2303 * was loaded first.
2304 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002305 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002306 return true;
2307#endif
2308 return false;
2309}
2310
Chris Wilsondc979972016-05-10 14:10:04 +01002311void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002312{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002313 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05302314 enum intel_engine_id id;
Ben Widawsky828c7902013-10-16 09:21:30 -07002315
Chris Wilsondc979972016-05-10 14:10:04 +01002316 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002317 return;
2318
Akash Goel3b3f1652016-10-13 22:44:48 +05302319 for_each_engine(engine, dev_priv, id) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002320 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002321 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002322 if (fault_reg & RING_FAULT_VALID) {
2323 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002324 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002325 "\tAddress space: %s\n"
2326 "\tSource ID: %d\n"
2327 "\tType: %d\n",
2328 fault_reg & PAGE_MASK,
2329 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2330 RING_FAULT_SRCID(fault_reg),
2331 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002332 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002333 fault_reg & ~RING_FAULT_VALID);
2334 }
2335 }
Akash Goel3b3f1652016-10-13 22:44:48 +05302336
2337 /* Engine specific init may not have been done till this point. */
2338 if (dev_priv->engine[RCS])
2339 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002340}
2341
Chris Wilson91e56492014-09-25 10:13:12 +01002342static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2343{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002344 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002345 intel_gtt_chipset_flush();
2346 } else {
2347 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2348 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2349 }
2350}
2351
Ben Widawsky828c7902013-10-16 09:21:30 -07002352void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2353{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002354 struct drm_i915_private *dev_priv = to_i915(dev);
2355 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002356
2357 /* Don't bother messing with faults pre GEN6 as we have little
2358 * documentation supporting that it's a good idea.
2359 */
2360 if (INTEL_INFO(dev)->gen < 6)
2361 return;
2362
Chris Wilsondc979972016-05-10 14:10:04 +01002363 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002364
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002365 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Chris Wilson91e56492014-09-25 10:13:12 +01002366
2367 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002368}
2369
Chris Wilson03ac84f2016-10-28 13:58:36 +01002370int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2371 struct sg_table *pages)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002372{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002373 if (dma_map_sg(&obj->base.dev->pdev->dev,
2374 pages->sgl, pages->nents,
2375 PCI_DMA_BIDIRECTIONAL))
2376 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01002377
Chris Wilson03ac84f2016-10-28 13:58:36 +01002378 return -ENOSPC;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002379}
2380
Daniel Vetter2c642b02015-04-14 17:35:26 +02002381static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002382{
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002383 writeq(pte, addr);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002384}
2385
Chris Wilsond6473f52016-06-10 14:22:59 +05302386static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2387 dma_addr_t addr,
2388 uint64_t offset,
2389 enum i915_cache_level level,
2390 u32 unused)
2391{
2392 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2393 gen8_pte_t __iomem *pte =
2394 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2395 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302396
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002397 gen8_set_pte(pte, gen8_pte_encode(addr, level));
Chris Wilsond6473f52016-06-10 14:22:59 +05302398
2399 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2400 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302401}
2402
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002403static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2404 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002405 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302406 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002407{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002408 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002409 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002410 struct sgt_iter sgt_iter;
2411 gen8_pte_t __iomem *gtt_entries;
2412 gen8_pte_t gtt_entry;
2413 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002414 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002415
Dave Gordon85d12252016-05-20 11:54:06 +01002416 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2417
2418 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002419 gtt_entry = gen8_pte_encode(addr, level);
Dave Gordon85d12252016-05-20 11:54:06 +01002420 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002421 }
2422
2423 /*
2424 * XXX: This serves as a posting read to make sure that the PTE has
2425 * actually been updated. There is some concern that even though
2426 * registers and PTEs are within the same BAR that they are potentially
2427 * of NUMA access patterns. Therefore, even with the way we assume
2428 * hardware should work, we must keep this posting read for paranoia.
2429 */
2430 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002431 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002432
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002433 /* This next bit makes the above posting read even more important. We
2434 * want to flush the TLBs only after we're certain all the PTE updates
2435 * have finished.
2436 */
2437 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2438 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002439}
2440
Chris Wilsonc1403302015-11-18 15:19:39 +00002441struct insert_entries {
2442 struct i915_address_space *vm;
2443 struct sg_table *st;
2444 uint64_t start;
2445 enum i915_cache_level level;
2446 u32 flags;
2447};
2448
2449static int gen8_ggtt_insert_entries__cb(void *_arg)
2450{
2451 struct insert_entries *arg = _arg;
2452 gen8_ggtt_insert_entries(arg->vm, arg->st,
2453 arg->start, arg->level, arg->flags);
2454 return 0;
2455}
2456
2457static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2458 struct sg_table *st,
2459 uint64_t start,
2460 enum i915_cache_level level,
2461 u32 flags)
2462{
2463 struct insert_entries arg = { vm, st, start, level, flags };
2464 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2465}
2466
Chris Wilsond6473f52016-06-10 14:22:59 +05302467static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2468 dma_addr_t addr,
2469 uint64_t offset,
2470 enum i915_cache_level level,
2471 u32 flags)
2472{
2473 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2474 gen6_pte_t __iomem *pte =
2475 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2476 (offset >> PAGE_SHIFT);
Chris Wilsond6473f52016-06-10 14:22:59 +05302477
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002478 iowrite32(vm->pte_encode(addr, level, flags), pte);
Chris Wilsond6473f52016-06-10 14:22:59 +05302479
2480 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2481 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Chris Wilsond6473f52016-06-10 14:22:59 +05302482}
2483
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002484/*
2485 * Binds an object into the global gtt with the specified cache level. The object
2486 * will be accessible to the GPU via commands whose operands reference offsets
2487 * within the global GTT as well as accessible by the GPU through the GMADR
2488 * mapped BAR (dev_priv->mm.gtt->gtt).
2489 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002490static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002491 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002492 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302493 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002494{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002495 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002496 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002497 struct sgt_iter sgt_iter;
2498 gen6_pte_t __iomem *gtt_entries;
2499 gen6_pte_t gtt_entry;
2500 dma_addr_t addr;
Dave Gordon85d12252016-05-20 11:54:06 +01002501 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002502
Dave Gordon85d12252016-05-20 11:54:06 +01002503 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2504
2505 for_each_sgt_dma(addr, sgt_iter, st) {
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002506 gtt_entry = vm->pte_encode(addr, level, flags);
Dave Gordon85d12252016-05-20 11:54:06 +01002507 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002508 }
2509
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002510 /* XXX: This serves as a posting read to make sure that the PTE has
2511 * actually been updated. There is some concern that even though
2512 * registers and PTEs are within the same BAR that they are potentially
2513 * of NUMA access patterns. Therefore, even with the way we assume
2514 * hardware should work, we must keep this posting read for paranoia.
2515 */
Dave Gordon85d12252016-05-20 11:54:06 +01002516 if (i != 0)
2517 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002518
2519 /* This next bit makes the above posting read even more important. We
2520 * want to flush the TLBs only after we're certain all the PTE updates
2521 * have finished.
2522 */
2523 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2524 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002525}
2526
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002527static void nop_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002528 uint64_t start, uint64_t length)
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002529{
2530}
2531
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002532static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002533 uint64_t start, uint64_t length)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002534{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002535 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002536 unsigned first_entry = start >> PAGE_SHIFT;
2537 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002538 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002539 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2540 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002541 int i;
2542
2543 if (WARN(num_entries > max_entries,
2544 "First entry = %d; Num entries = %d (max=%d)\n",
2545 first_entry, num_entries, max_entries))
2546 num_entries = max_entries;
2547
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002548 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002549 I915_CACHE_LLC);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002550 for (i = 0; i < num_entries; i++)
2551 gen8_set_pte(&gtt_base[i], scratch_pte);
2552 readl(gtt_base);
2553}
2554
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002555static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002556 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002557 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002558{
Chris Wilsonce7fda22016-04-28 09:56:38 +01002559 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002560 unsigned first_entry = start >> PAGE_SHIFT;
2561 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002562 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002563 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2564 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002565 int i;
2566
2567 if (WARN(num_entries > max_entries,
2568 "First entry = %d; Num entries = %d (max=%d)\n",
2569 first_entry, num_entries, max_entries))
2570 num_entries = max_entries;
2571
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002572 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002573 I915_CACHE_LLC, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002574
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002575 for (i = 0; i < num_entries; i++)
2576 iowrite32(scratch_pte, &gtt_base[i]);
2577 readl(gtt_base);
2578}
2579
Chris Wilsond6473f52016-06-10 14:22:59 +05302580static void i915_ggtt_insert_page(struct i915_address_space *vm,
2581 dma_addr_t addr,
2582 uint64_t offset,
2583 enum i915_cache_level cache_level,
2584 u32 unused)
2585{
Chris Wilsond6473f52016-06-10 14:22:59 +05302586 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2587 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Chris Wilsond6473f52016-06-10 14:22:59 +05302588
2589 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
Chris Wilsond6473f52016-06-10 14:22:59 +05302590}
2591
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002592static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2593 struct sg_table *pages,
2594 uint64_t start,
2595 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002596{
2597 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2598 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2599
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002600 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002601
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002602}
2603
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002604static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002605 uint64_t start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002606 uint64_t length)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002607{
Chris Wilson2eedfc72016-10-24 13:42:17 +01002608 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002609}
2610
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002611static int ggtt_bind_vma(struct i915_vma *vma,
2612 enum i915_cache_level cache_level,
2613 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002614{
Chris Wilson9c870d02016-10-24 13:42:15 +01002615 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Daniel Vetter0a878712015-10-15 14:23:01 +02002616 struct drm_i915_gem_object *obj = vma->obj;
2617 u32 pte_flags = 0;
2618 int ret;
2619
2620 ret = i915_get_ggtt_vma_pages(vma);
2621 if (ret)
2622 return ret;
2623
2624 /* Currently applicable only to VLV */
2625 if (obj->gt_ro)
2626 pte_flags |= PTE_READ_ONLY;
2627
Chris Wilson9c870d02016-10-24 13:42:15 +01002628 intel_runtime_pm_get(i915);
Chris Wilson247177d2016-08-15 10:48:47 +01002629 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002630 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002631 intel_runtime_pm_put(i915);
Daniel Vetter0a878712015-10-15 14:23:01 +02002632
2633 /*
2634 * Without aliasing PPGTT there's no difference between
2635 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2636 * upgrade to both bound if we bind either to avoid double-binding.
2637 */
Chris Wilson3272db52016-08-04 16:32:32 +01002638 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002639
2640 return 0;
2641}
2642
2643static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2644 enum i915_cache_level cache_level,
2645 u32 flags)
2646{
Chris Wilson9c870d02016-10-24 13:42:15 +01002647 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
Chris Wilson321d1782015-11-20 10:27:18 +00002648 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002649 int ret;
2650
2651 ret = i915_get_ggtt_vma_pages(vma);
2652 if (ret)
2653 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002654
Akash Goel24f3a8c2014-06-17 10:59:42 +05302655 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002656 pte_flags = 0;
2657 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002658 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302659
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002660
Chris Wilson3272db52016-08-04 16:32:32 +01002661 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002662 intel_runtime_pm_get(i915);
Chris Wilson321d1782015-11-20 10:27:18 +00002663 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002664 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002665 cache_level, pte_flags);
Chris Wilson9c870d02016-10-24 13:42:15 +01002666 intel_runtime_pm_put(i915);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002667 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002668
Chris Wilson3272db52016-08-04 16:32:32 +01002669 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson9c870d02016-10-24 13:42:15 +01002670 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilson321d1782015-11-20 10:27:18 +00002671 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002672 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002673 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002674 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002675
2676 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002677}
2678
2679static void ggtt_unbind_vma(struct i915_vma *vma)
2680{
Chris Wilson9c870d02016-10-24 13:42:15 +01002681 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
2682 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
Chris Wilsonde180032016-08-04 16:32:29 +01002683 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002684
Chris Wilson9c870d02016-10-24 13:42:15 +01002685 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2686 intel_runtime_pm_get(i915);
Ben Widawsky782f1492014-02-20 11:50:33 -08002687 vma->vm->clear_range(vma->vm,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002688 vma->node.start, size);
Chris Wilson9c870d02016-10-24 13:42:15 +01002689 intel_runtime_pm_put(i915);
2690 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08002691
Chris Wilson3272db52016-08-04 16:32:32 +01002692 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002693 appgtt->base.clear_range(&appgtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002694 vma->node.start, size);
Daniel Vetter74163902012-02-15 23:50:21 +01002695}
2696
Chris Wilson03ac84f2016-10-28 13:58:36 +01002697void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2698 struct sg_table *pages)
Daniel Vetter74163902012-02-15 23:50:21 +01002699{
David Weinehall52a05c32016-08-22 13:32:44 +03002700 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2701 struct device *kdev = &dev_priv->drm.pdev->dev;
Chris Wilson307dc252016-08-05 10:14:12 +01002702 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002703
Chris Wilson307dc252016-08-05 10:14:12 +01002704 if (unlikely(ggtt->do_idle_maps)) {
Chris Wilson22dd3bb2016-09-09 14:11:50 +01002705 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
Chris Wilson307dc252016-08-05 10:14:12 +01002706 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2707 /* Wait a bit, in hopes it avoids the hang */
2708 udelay(10);
2709 }
2710 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002711
Chris Wilson03ac84f2016-10-28 13:58:36 +01002712 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002713}
Daniel Vetter644ec022012-03-26 09:45:40 +02002714
Chris Wilson42d6ab42012-07-26 11:49:32 +01002715static void i915_gtt_color_adjust(struct drm_mm_node *node,
2716 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002717 u64 *start,
2718 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002719{
2720 if (node->color != color)
2721 *start += 4096;
2722
Chris Wilson2a1d7752016-07-26 12:01:51 +01002723 node = list_first_entry_or_null(&node->node_list,
2724 struct drm_mm_node,
2725 node_list);
2726 if (node && node->allocated && node->color != color)
2727 *end -= 4096;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002728}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002729
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002730int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002731{
Ben Widawskye78891c2013-01-25 16:41:04 -08002732 /* Let GEM Manage all of the aperture.
2733 *
2734 * However, leave one page at the end still bound to the scratch page.
2735 * There are a number of places where the hardware apparently prefetches
2736 * past the end of the object, and we've seen multiple hangs with the
2737 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2738 * aperture. One page should be enough to keep any prefetching inside
2739 * of the aperture.
2740 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002741 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002742 unsigned long hole_start, hole_end;
Chris Wilson95374d72016-10-12 10:05:20 +01002743 struct i915_hw_ppgtt *ppgtt;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002744 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002745 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002746
Zhi Wangb02d22a2016-06-16 08:06:59 -04002747 ret = intel_vgt_balloon(dev_priv);
2748 if (ret)
2749 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002750
Chris Wilson95374d72016-10-12 10:05:20 +01002751 /* Reserve a mappable slot for our lockless error capture */
2752 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2753 &ggtt->error_capture,
2754 4096, 0, -1,
2755 0, ggtt->mappable_end,
2756 0, 0);
2757 if (ret)
2758 return ret;
2759
Chris Wilsoned2f3452012-11-15 11:32:19 +00002760 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002761 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002762 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2763 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002764 ggtt->base.clear_range(&ggtt->base, hole_start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002765 hole_end - hole_start);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002766 }
2767
2768 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002769 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002770 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002771
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002772 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002773 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
Chris Wilson95374d72016-10-12 10:05:20 +01002774 if (!ppgtt) {
2775 ret = -ENOMEM;
2776 goto err;
Michel Thierry4933d512015-03-24 15:46:22 +00002777 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002778
Chris Wilson95374d72016-10-12 10:05:20 +01002779 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2780 if (ret)
2781 goto err_ppgtt;
2782
2783 if (ppgtt->base.allocate_va_range) {
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002784 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2785 ppgtt->base.total);
Chris Wilson95374d72016-10-12 10:05:20 +01002786 if (ret)
2787 goto err_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002788 }
2789
2790 ppgtt->base.clear_range(&ppgtt->base,
2791 ppgtt->base.start,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02002792 ppgtt->base.total);
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002793
Daniel Vetterfa76da32014-08-06 20:19:54 +02002794 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002795 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2796 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002797 }
2798
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002799 return 0;
Chris Wilson95374d72016-10-12 10:05:20 +01002800
2801err_ppgtt_cleanup:
2802 ppgtt->base.cleanup(&ppgtt->base);
2803err_ppgtt:
2804 kfree(ppgtt);
2805err:
2806 drm_mm_remove_node(&ggtt->error_capture);
2807 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002808}
2809
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002810/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002811 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002812 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002813 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002814void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002815{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002816 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002817
Daniel Vetter70e32542014-08-06 15:04:57 +02002818 if (dev_priv->mm.aliasing_ppgtt) {
2819 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002820 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002821 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002822 }
2823
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002824 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002825
Chris Wilson95374d72016-10-12 10:05:20 +01002826 if (drm_mm_node_allocated(&ggtt->error_capture))
2827 drm_mm_remove_node(&ggtt->error_capture);
2828
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002829 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002830 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002831
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002832 drm_mm_takedown(&ggtt->base.mm);
2833 list_del(&ggtt->base.global_link);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002834 }
2835
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002836 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002837
2838 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002839 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002840}
Daniel Vetter70e32542014-08-06 15:04:57 +02002841
Daniel Vetter2c642b02015-04-14 17:35:26 +02002842static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002843{
2844 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2845 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2846 return snb_gmch_ctl << 20;
2847}
2848
Daniel Vetter2c642b02015-04-14 17:35:26 +02002849static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002850{
2851 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2852 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2853 if (bdw_gmch_ctl)
2854 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002855
2856#ifdef CONFIG_X86_32
2857 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2858 if (bdw_gmch_ctl > 4)
2859 bdw_gmch_ctl = 4;
2860#endif
2861
Ben Widawsky9459d252013-11-03 16:53:55 -08002862 return bdw_gmch_ctl << 20;
2863}
2864
Daniel Vetter2c642b02015-04-14 17:35:26 +02002865static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002866{
2867 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2868 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2869
2870 if (gmch_ctrl)
2871 return 1 << (20 + gmch_ctrl);
2872
2873 return 0;
2874}
2875
Daniel Vetter2c642b02015-04-14 17:35:26 +02002876static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002877{
2878 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2879 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2880 return snb_gmch_ctl << 25; /* 32 MB units */
2881}
2882
Daniel Vetter2c642b02015-04-14 17:35:26 +02002883static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002884{
2885 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2886 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2887 return bdw_gmch_ctl << 25; /* 32 MB units */
2888}
2889
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002890static size_t chv_get_stolen_size(u16 gmch_ctrl)
2891{
2892 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2893 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2894
2895 /*
2896 * 0x0 to 0x10: 32MB increments starting at 0MB
2897 * 0x11 to 0x16: 4MB increments starting at 8MB
2898 * 0x17 to 0x1d: 4MB increments start at 36MB
2899 */
2900 if (gmch_ctrl < 0x11)
2901 return gmch_ctrl << 25;
2902 else if (gmch_ctrl < 0x17)
2903 return (gmch_ctrl - 0x11 + 2) << 22;
2904 else
2905 return (gmch_ctrl - 0x17 + 9) << 22;
2906}
2907
Damien Lespiau66375012014-01-09 18:02:46 +00002908static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2909{
2910 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2911 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2912
2913 if (gen9_gmch_ctl < 0xf0)
2914 return gen9_gmch_ctl << 25; /* 32 MB units */
2915 else
2916 /* 4MB increments starting at 0xf0 for 4MB */
2917 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2918}
2919
Chris Wilson34c998b2016-08-04 07:52:24 +01002920static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002921{
Chris Wilson34c998b2016-08-04 07:52:24 +01002922 struct pci_dev *pdev = ggtt->base.dev->pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01002923 phys_addr_t phys_addr;
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002924 int ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002925
2926 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002927 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002928
Imre Deak2a073f892015-03-27 13:07:33 +02002929 /*
2930 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2931 * dropped. For WC mappings in general we have 64 byte burst writes
2932 * when the WC buffer is flushed, so we can't use it, but have to
2933 * resort to an uncached mapping. The WC issue is easily caught by the
2934 * readback check when writing GTT PTE entries.
2935 */
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01002936 if (IS_BROXTON(to_i915(ggtt->base.dev)))
Chris Wilson34c998b2016-08-04 07:52:24 +01002937 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002938 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002939 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002940 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002941 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002942 return -ENOMEM;
2943 }
2944
Chris Wilsonbb8f9cf2016-08-22 08:44:31 +01002945 ret = setup_scratch_page(ggtt->base.dev,
2946 &ggtt->base.scratch_page,
2947 GFP_DMA32);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002948 if (ret) {
Ben Widawsky63340132013-11-04 19:32:22 -08002949 DRM_ERROR("Scratch setup failed\n");
2950 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002951 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01002952 return ret;
Ben Widawsky63340132013-11-04 19:32:22 -08002953 }
2954
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002955 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002956}
2957
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002958/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2959 * bits. When using advanced contexts each context stores its own PAT, but
2960 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002961static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002962{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002963 uint64_t pat;
2964
2965 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2966 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2967 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2968 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2969 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2970 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2971 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2972 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2973
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002974 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002975 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2976 * so RTL will always use the value corresponding to
2977 * pat_sel = 000".
2978 * So let's disable cache for GGTT to avoid screen corruptions.
2979 * MOCS still can be used though.
2980 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2981 * before this patch, i.e. the same uncached + snooping access
2982 * like on gen6/7 seems to be in effect.
2983 * - So this just fixes blitter/render access. Again it looks
2984 * like it's not just uncached access, but uncached + snooping.
2985 * So we can still hold onto all our assumptions wrt cpu
2986 * clflushing on LLC machines.
2987 */
2988 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2989
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002990 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2991 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002992 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2993 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002994}
2995
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002996static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2997{
2998 uint64_t pat;
2999
3000 /*
3001 * Map WB on BDW to snooped on CHV.
3002 *
3003 * Only the snoop bit has meaning for CHV, the rest is
3004 * ignored.
3005 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02003006 * The hardware will never snoop for certain types of accesses:
3007 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3008 * - PPGTT page tables
3009 * - some other special cycles
3010 *
3011 * As with BDW, we also need to consider the following for GT accesses:
3012 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3013 * so RTL will always use the value corresponding to
3014 * pat_sel = 000".
3015 * Which means we must set the snoop bit in PAT entry 0
3016 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003017 */
3018 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3019 GEN8_PPAT(1, 0) |
3020 GEN8_PPAT(2, 0) |
3021 GEN8_PPAT(3, 0) |
3022 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3023 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3024 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3025 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3026
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003027 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3028 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003029}
3030
Chris Wilson34c998b2016-08-04 07:52:24 +01003031static void gen6_gmch_remove(struct i915_address_space *vm)
3032{
3033 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3034
3035 iounmap(ggtt->gsm);
Chris Wilson8bcdd0f72016-08-22 08:44:30 +01003036 cleanup_scratch_page(vm->dev, &vm->scratch_page);
Chris Wilson34c998b2016-08-04 07:52:24 +01003037}
3038
Joonas Lahtinend507d732016-03-18 10:42:58 +02003039static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003040{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003041 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3042 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003043 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003044 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003045
3046 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003047 ggtt->mappable_base = pci_resource_start(pdev, 2);
3048 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003049
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003050 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3051 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003052
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003053 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003054
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003055 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003056 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003057 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003058 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003059 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003060 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003061 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003062 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003063 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003064 }
Ben Widawsky63340132013-11-04 19:32:22 -08003065
Chris Wilson34c998b2016-08-04 07:52:24 +01003066 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003067
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003068 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003069 chv_setup_private_ppat(dev_priv);
3070 else
3071 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003072
Chris Wilson34c998b2016-08-04 07:52:24 +01003073 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003074 ggtt->base.bind_vma = ggtt_bind_vma;
3075 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303076 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003077 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003078 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003079 ggtt->base.clear_range = gen8_ggtt_clear_range;
3080
3081 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3082 if (IS_CHERRYVIEW(dev_priv))
3083 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3084
Chris Wilson34c998b2016-08-04 07:52:24 +01003085 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003086}
3087
Joonas Lahtinend507d732016-03-18 10:42:58 +02003088static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003089{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003090 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3091 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003092 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003093 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003094
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003095 ggtt->mappable_base = pci_resource_start(pdev, 2);
3096 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003097
Ben Widawskybaa09f52013-01-24 13:49:57 -08003098 /* 64/512MB is the current min/max we actually know of, but this is just
3099 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003100 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003101 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003102 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003103 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003104 }
3105
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003106 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3107 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3108 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003109
Joonas Lahtinend507d732016-03-18 10:42:58 +02003110 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003111
Chris Wilson34c998b2016-08-04 07:52:24 +01003112 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3113 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003114
Joonas Lahtinend507d732016-03-18 10:42:58 +02003115 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303116 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003117 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3118 ggtt->base.bind_vma = ggtt_bind_vma;
3119 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003120 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003121
Chris Wilson34c998b2016-08-04 07:52:24 +01003122 if (HAS_EDRAM(dev_priv))
3123 ggtt->base.pte_encode = iris_pte_encode;
3124 else if (IS_HASWELL(dev_priv))
3125 ggtt->base.pte_encode = hsw_pte_encode;
3126 else if (IS_VALLEYVIEW(dev_priv))
3127 ggtt->base.pte_encode = byt_pte_encode;
3128 else if (INTEL_GEN(dev_priv) >= 7)
3129 ggtt->base.pte_encode = ivb_pte_encode;
3130 else
3131 ggtt->base.pte_encode = snb_pte_encode;
3132
3133 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003134}
3135
Chris Wilson34c998b2016-08-04 07:52:24 +01003136static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003137{
Chris Wilson34c998b2016-08-04 07:52:24 +01003138 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003139}
3140
Joonas Lahtinend507d732016-03-18 10:42:58 +02003141static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003142{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003143 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003144 int ret;
3145
Chris Wilson91c8a322016-07-05 10:40:23 +01003146 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003147 if (!ret) {
3148 DRM_ERROR("failed to set up gmch\n");
3149 return -EIO;
3150 }
3151
Joonas Lahtinend507d732016-03-18 10:42:58 +02003152 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3153 &ggtt->mappable_base, &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003154
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003155 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303156 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003157 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3158 ggtt->base.clear_range = i915_ggtt_clear_range;
3159 ggtt->base.bind_vma = ggtt_bind_vma;
3160 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003161 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003162
Joonas Lahtinend507d732016-03-18 10:42:58 +02003163 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003164 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3165
Ben Widawskybaa09f52013-01-24 13:49:57 -08003166 return 0;
3167}
3168
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003169/**
Chris Wilson0088e522016-08-04 07:52:21 +01003170 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003171 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003172 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003173int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003174{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003175 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003176 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003177
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003178 ggtt->base.dev = &dev_priv->drm;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003179
Chris Wilson34c998b2016-08-04 07:52:24 +01003180 if (INTEL_GEN(dev_priv) <= 5)
3181 ret = i915_gmch_probe(ggtt);
3182 else if (INTEL_GEN(dev_priv) < 8)
3183 ret = gen6_gmch_probe(ggtt);
3184 else
3185 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003186 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003187 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003188
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003189 if ((ggtt->base.total - 1) >> 32) {
3190 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003191 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003192 ggtt->base.total >> 20);
3193 ggtt->base.total = 1ULL << 32;
3194 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3195 }
3196
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003197 if (ggtt->mappable_end > ggtt->base.total) {
3198 DRM_ERROR("mappable aperture extends past end of GGTT,"
3199 " aperture=%llx, total=%llx\n",
3200 ggtt->mappable_end, ggtt->base.total);
3201 ggtt->mappable_end = ggtt->base.total;
3202 }
3203
Ben Widawskybaa09f52013-01-24 13:49:57 -08003204 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003205 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003206 ggtt->base.total >> 20);
3207 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3208 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003209#ifdef CONFIG_INTEL_IOMMU
3210 if (intel_iommu_gfx_mapped)
3211 DRM_INFO("VT-d active for gfx access\n");
3212#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003213
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003214 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003215}
3216
3217/**
3218 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003219 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003220 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003221int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003222{
Chris Wilson0088e522016-08-04 07:52:21 +01003223 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3224 int ret;
3225
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003226 INIT_LIST_HEAD(&dev_priv->vm_list);
3227
3228 /* Subtract the guard page before address space initialization to
3229 * shrink the range used by drm_mm.
3230 */
Chris Wilson80b204b2016-10-28 13:58:58 +01003231 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003232 ggtt->base.total -= PAGE_SIZE;
Chris Wilson80b204b2016-10-28 13:58:58 +01003233 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003234 ggtt->base.total += PAGE_SIZE;
3235 if (!HAS_LLC(dev_priv))
3236 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
Chris Wilson80b204b2016-10-28 13:58:58 +01003237 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003238
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003239 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3240 dev_priv->ggtt.mappable_base,
3241 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003242 ret = -EIO;
3243 goto out_gtt_cleanup;
3244 }
3245
3246 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3247
Chris Wilson0088e522016-08-04 07:52:21 +01003248 /*
3249 * Initialise stolen early so that we may reserve preallocated
3250 * objects for the BIOS to KMS transition.
3251 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003252 ret = i915_gem_init_stolen(&dev_priv->drm);
Chris Wilson0088e522016-08-04 07:52:21 +01003253 if (ret)
3254 goto out_gtt_cleanup;
3255
3256 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003257
3258out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003259 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003260 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003261}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003262
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003263int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003264{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003265 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003266 return -EIO;
3267
3268 return 0;
3269}
3270
Daniel Vetterfa423312015-04-14 17:35:23 +02003271void i915_gem_restore_gtt_mappings(struct drm_device *dev)
3272{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003273 struct drm_i915_private *dev_priv = to_i915(dev);
3274 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003275 struct drm_i915_gem_object *obj, *on;
Daniel Vetterfa423312015-04-14 17:35:23 +02003276
Chris Wilsondc979972016-05-10 14:10:04 +01003277 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003278
3279 /* First fill our portion of the GTT with scratch pages */
Michał Winiarski4fb84d92016-10-13 14:02:40 +02003280 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
Daniel Vetterfa423312015-04-14 17:35:23 +02003281
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003282 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3283
3284 /* clflush objects bound into the GGTT and rebind them. */
3285 list_for_each_entry_safe(obj, on,
Joonas Lahtinen56cea322016-11-02 12:16:04 +02003286 &dev_priv->mm.bound_list, global_link) {
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003287 bool ggtt_bound = false;
3288 struct i915_vma *vma;
3289
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003290 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003291 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003292 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003293
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003294 if (!i915_vma_unbind(vma))
3295 continue;
3296
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003297 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3298 PIN_UPDATE));
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003299 ggtt_bound = true;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003300 }
3301
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003302 if (ggtt_bound)
Chris Wilson975f7ff2016-05-14 07:26:34 +01003303 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003304 }
3305
Chris Wilsonfbb30a5c2016-09-09 21:19:57 +01003306 ggtt->base.closed = false;
3307
Daniel Vetterfa423312015-04-14 17:35:23 +02003308 if (INTEL_INFO(dev)->gen >= 8) {
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +01003309 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Daniel Vetterfa423312015-04-14 17:35:23 +02003310 chv_setup_private_ppat(dev_priv);
3311 else
3312 bdw_setup_private_ppat(dev_priv);
3313
3314 return;
3315 }
3316
3317 if (USES_PPGTT(dev)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003318 struct i915_address_space *vm;
3319
Daniel Vetterfa423312015-04-14 17:35:23 +02003320 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3321 /* TODO: Perhaps it shouldn't be gen6 specific */
3322
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003323 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003324
Chris Wilson2bfa9962016-08-04 07:52:25 +01003325 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003326 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003327 else
3328 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003329
3330 gen6_write_page_range(dev_priv, &ppgtt->pd,
3331 0, ppgtt->base.total);
3332 }
3333 }
3334
3335 i915_ggtt_flush(dev_priv);
3336}
3337
Chris Wilson058d88c2016-08-15 10:49:06 +01003338struct i915_vma *
3339i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3340 struct i915_address_space *vm,
3341 const struct i915_ggtt_view *view)
3342{
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003343 struct rb_node *rb;
Chris Wilson058d88c2016-08-15 10:49:06 +01003344
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003345 rb = obj->vma_tree.rb_node;
3346 while (rb) {
3347 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
3348 long cmp;
3349
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003350 cmp = i915_vma_compare(vma, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003351 if (cmp == 0)
Chris Wilson058d88c2016-08-15 10:49:06 +01003352 return vma;
3353
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003354 if (cmp < 0)
3355 rb = rb->rb_right;
3356 else
3357 rb = rb->rb_left;
3358 }
3359
Chris Wilson058d88c2016-08-15 10:49:06 +01003360 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003361}
3362
3363struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003364i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003365 struct i915_address_space *vm,
3366 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003367{
3368 struct i915_vma *vma;
3369
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003370 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson058d88c2016-08-15 10:49:06 +01003371 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3372
3373 vma = i915_gem_obj_to_vma(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003374 if (!vma) {
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02003375 vma = i915_vma_create(obj, vm, view);
Chris Wilsondb6c2b42016-11-01 11:54:00 +00003376 GEM_BUG_ON(vma != i915_gem_obj_to_vma(obj, vm, view));
3377 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003378
Chris Wilson3272db52016-08-04 16:32:32 +01003379 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003380 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003381}
3382
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003383static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003384rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003385 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003386 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003387 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003388{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003389 unsigned int column, row;
3390 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003391
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003392 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003393 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003394 for (row = 0; row < height; row++) {
3395 st->nents++;
3396 /* We don't need the pages, but need to initialize
3397 * the entries so the sg list can be happily traversed.
3398 * The only thing we need are DMA addresses.
3399 */
3400 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003401 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003402 sg_dma_len(sg) = PAGE_SIZE;
3403 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003404 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003405 }
3406 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003407
3408 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003409}
3410
3411static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003412intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003413 struct drm_i915_gem_object *obj)
3414{
Dave Gordon85d12252016-05-20 11:54:06 +01003415 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003416 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003417 struct sgt_iter sgt_iter;
3418 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003419 unsigned long i;
3420 dma_addr_t *page_addr_list;
3421 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003422 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003423 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003424
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003425 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003426 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003427 sizeof(dma_addr_t),
3428 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003429 if (!page_addr_list)
3430 return ERR_PTR(ret);
3431
3432 /* Allocate target SG list. */
3433 st = kmalloc(sizeof(*st), GFP_KERNEL);
3434 if (!st)
3435 goto err_st_alloc;
3436
Ville Syrjälä6687c902015-09-15 13:16:41 +03003437 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003438 if (ret)
3439 goto err_sg_alloc;
3440
3441 /* Populate source page list from the object. */
3442 i = 0;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003443 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
Dave Gordon85d12252016-05-20 11:54:06 +01003444 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003445
Dave Gordon85d12252016-05-20 11:54:06 +01003446 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003447 st->nents = 0;
3448 sg = st->sgl;
3449
Ville Syrjälä6687c902015-09-15 13:16:41 +03003450 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3451 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3452 rot_info->plane[i].width, rot_info->plane[i].height,
3453 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003454 }
3455
Ville Syrjälä6687c902015-09-15 13:16:41 +03003456 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3457 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003458
3459 drm_free_large(page_addr_list);
3460
3461 return st;
3462
3463err_sg_alloc:
3464 kfree(st);
3465err_st_alloc:
3466 drm_free_large(page_addr_list);
3467
Ville Syrjälä6687c902015-09-15 13:16:41 +03003468 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3469 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3470
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003471 return ERR_PTR(ret);
3472}
3473
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003474static struct sg_table *
3475intel_partial_pages(const struct i915_ggtt_view *view,
3476 struct drm_i915_gem_object *obj)
3477{
3478 struct sg_table *st;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003479 struct scatterlist *sg, *iter;
3480 unsigned int count = view->params.partial.size;
3481 unsigned int offset;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003482 int ret = -ENOMEM;
3483
3484 st = kmalloc(sizeof(*st), GFP_KERNEL);
3485 if (!st)
3486 goto err_st_alloc;
3487
Chris Wilsond2a84a72016-10-28 13:58:34 +01003488 ret = sg_alloc_table(st, count, GFP_KERNEL);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003489 if (ret)
3490 goto err_sg_alloc;
3491
Chris Wilsond2a84a72016-10-28 13:58:34 +01003492 iter = i915_gem_object_get_sg(obj,
3493 view->params.partial.offset,
3494 &offset);
3495 GEM_BUG_ON(!iter);
3496
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003497 sg = st->sgl;
3498 st->nents = 0;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003499 do {
3500 unsigned int len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003501
Chris Wilsond2a84a72016-10-28 13:58:34 +01003502 len = min(iter->length - (offset << PAGE_SHIFT),
3503 count << PAGE_SHIFT);
3504 sg_set_page(sg, NULL, len, 0);
3505 sg_dma_address(sg) =
3506 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3507 sg_dma_len(sg) = len;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003508
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003509 st->nents++;
Chris Wilsond2a84a72016-10-28 13:58:34 +01003510 count -= len >> PAGE_SHIFT;
3511 if (count == 0) {
3512 sg_mark_end(sg);
3513 return st;
3514 }
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003515
Chris Wilsond2a84a72016-10-28 13:58:34 +01003516 sg = __sg_next(sg);
3517 iter = __sg_next(iter);
3518 offset = 0;
3519 } while (1);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003520
3521err_sg_alloc:
3522 kfree(st);
3523err_st_alloc:
3524 return ERR_PTR(ret);
3525}
3526
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003527static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003528i915_get_ggtt_vma_pages(struct i915_vma *vma)
3529{
3530 int ret = 0;
3531
Chris Wilson2c3a3f42016-11-04 10:30:01 +00003532 /* The vma->pages are only valid within the lifespan of the borrowed
3533 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3534 * must be the vma->pages. A simple rule is that vma->pages must only
3535 * be accessed when the obj->mm.pages are pinned.
3536 */
3537 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3538
Chris Wilson247177d2016-08-15 10:48:47 +01003539 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003540 return 0;
3541
3542 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003543 vma->pages = vma->obj->mm.pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003544 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003545 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003546 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003547 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003548 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003549 else
3550 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3551 vma->ggtt_view.type);
3552
Chris Wilson247177d2016-08-15 10:48:47 +01003553 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003554 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003555 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003556 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003557 } else if (IS_ERR(vma->pages)) {
3558 ret = PTR_ERR(vma->pages);
3559 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003560 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3561 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003562 }
3563
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003564 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003565}
3566