blob: d31ae3d0f83fc3d2a444e644aa9f6463adfcf535 [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"
34
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000035/**
36 * DOC: Global GTT views
37 *
38 * Background and previous state
39 *
40 * Historically objects could exists (be bound) in global GTT space only as
41 * singular instances with a view representing all of the object's backing pages
42 * in a linear fashion. This view will be called a normal view.
43 *
44 * To support multiple views of the same object, where the number of mapped
45 * pages is not equal to the backing store, or where the layout of the pages
46 * is not linear, concept of a GGTT view was added.
47 *
48 * One example of an alternative view is a stereo display driven by a single
49 * image. In this case we would have a framebuffer looking like this
50 * (2x2 pages):
51 *
52 * 12
53 * 34
54 *
55 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
56 * rendering. In contrast, fed to the display engine would be an alternative
57 * view which could look something like this:
58 *
59 * 1212
60 * 3434
61 *
62 * In this example both the size and layout of pages in the alternative view is
63 * different from the normal view.
64 *
65 * Implementation and usage
66 *
67 * GGTT views are implemented using VMAs and are distinguished via enum
68 * i915_ggtt_view_type and struct i915_ggtt_view.
69 *
70 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020071 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
72 * renaming in large amounts of code. They take the struct i915_ggtt_view
73 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000074 *
75 * As a helper for callers which are only interested in the normal view,
76 * globally const i915_ggtt_view_normal singleton instance exists. All old core
77 * GEM API functions, the ones not taking the view parameter, are operating on,
78 * or with the normal GGTT view.
79 *
80 * Code wanting to add or use a new GGTT view needs to:
81 *
82 * 1. Add a new enum with a suitable name.
83 * 2. Extend the metadata in the i915_ggtt_view structure if required.
84 * 3. Add support to i915_get_vma_pages().
85 *
86 * New views are required to build a scatter-gather table from within the
87 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
88 * exists for the lifetime of an VMA.
89 *
90 * Core API is designed to have copy semantics which means that passed in
91 * struct i915_ggtt_view does not need to be persistent (left around after
92 * calling the core API functions).
93 *
94 */
95
Chris Wilsonce7fda22016-04-28 09:56:38 +010096static inline struct i915_ggtt *
97i915_vm_to_ggtt(struct i915_address_space *vm)
98{
99 GEM_BUG_ON(!i915_is_ggtt(vm));
100 return container_of(vm, struct i915_ggtt, base);
101}
102
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200103static int
104i915_get_ggtt_vma_pages(struct i915_vma *vma);
105
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200106const struct i915_ggtt_view i915_ggtt_view_normal = {
107 .type = I915_GGTT_VIEW_NORMAL,
108};
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200109const struct i915_ggtt_view i915_ggtt_view_rotated = {
Ville Syrjäläb5e16982016-01-14 15:22:10 +0200110 .type = I915_GGTT_VIEW_ROTATED,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +0200111};
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000112
Chris Wilsonc0336662016-05-06 15:40:21 +0100113int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
114 int enable_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200115{
Chris Wilson1893a712014-09-19 11:56:27 +0100116 bool has_aliasing_ppgtt;
117 bool has_full_ppgtt;
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100118 bool has_full_48bit_ppgtt;
Chris Wilson1893a712014-09-19 11:56:27 +0100119
Chris Wilsonc0336662016-05-06 15:40:21 +0100120 has_aliasing_ppgtt = INTEL_GEN(dev_priv) >= 6;
121 has_full_ppgtt = INTEL_GEN(dev_priv) >= 7;
122 has_full_48bit_ppgtt =
123 IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9;
Chris Wilson1893a712014-09-19 11:56:27 +0100124
Chris Wilsonc0336662016-05-06 15:40:21 +0100125 if (intel_vgpu_active(dev_priv))
Yu Zhang71ba2d62015-02-10 19:05:54 +0800126 has_full_ppgtt = false; /* emulation is too hard */
127
Chris Wilson0e4ca102016-04-29 13:18:22 +0100128 if (!has_aliasing_ppgtt)
129 return 0;
130
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000131 /*
132 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
133 * execlists, the sole mechanism available to submit work.
134 */
Chris Wilsonc0336662016-05-06 15:40:21 +0100135 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200136 return 0;
137
138 if (enable_ppgtt == 1)
139 return 1;
140
Chris Wilson1893a712014-09-19 11:56:27 +0100141 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200142 return 2;
143
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100144 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
145 return 3;
146
Daniel Vetter93a25a92014-03-06 09:40:43 +0100147#ifdef CONFIG_INTEL_IOMMU
148 /* Disable ppgtt on SNB if VT-d is on. */
Chris Wilsonc0336662016-05-06 15:40:21 +0100149 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
Daniel Vetter93a25a92014-03-06 09:40:43 +0100150 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200151 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100152 }
153#endif
154
Jesse Barnes62942ed2014-06-13 09:28:33 -0700155 /* Early VLV doesn't have this */
Chris Wilson91c8a322016-07-05 10:40:23 +0100156 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700157 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
158 return 0;
159 }
160
Chris Wilsonc0336662016-05-06 15:40:21 +0100161 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists)
Michel Thierry1f9a99e2015-09-30 15:36:19 +0100162 return has_full_48bit_ppgtt ? 3 : 2;
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000163 else
164 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100165}
166
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200167static int ppgtt_bind_vma(struct i915_vma *vma,
168 enum i915_cache_level cache_level,
169 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200170{
171 u32 pte_flags = 0;
172
Chris Wilson247177d2016-08-15 10:48:47 +0100173 vma->pages = vma->obj->pages;
174
Daniel Vetter47552652015-04-14 17:35:24 +0200175 /* Currently applicable only to VLV */
176 if (vma->obj->gt_ro)
177 pte_flags |= PTE_READ_ONLY;
178
Chris Wilson247177d2016-08-15 10:48:47 +0100179 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter47552652015-04-14 17:35:24 +0200180 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200181
182 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200183}
184
185static void ppgtt_unbind_vma(struct i915_vma *vma)
186{
187 vma->vm->clear_range(vma->vm,
188 vma->node.start,
Chris Wilsonde180032016-08-04 16:32:29 +0100189 vma->size,
Daniel Vetter47552652015-04-14 17:35:24 +0200190 true);
191}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800192
Daniel Vetter2c642b02015-04-14 17:35:26 +0200193static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
194 enum i915_cache_level level,
195 bool valid)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700196{
Michel Thierry07749ef2015-03-16 16:00:54 +0000197 gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700198 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300199
200 switch (level) {
201 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800202 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300203 break;
204 case I915_CACHE_WT:
205 pte |= PPAT_DISPLAY_ELLC_INDEX;
206 break;
207 default:
208 pte |= PPAT_CACHED_INDEX;
209 break;
210 }
211
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700212 return pte;
213}
214
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300215static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
216 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800217{
Michel Thierry07749ef2015-03-16 16:00:54 +0000218 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800219 pde |= addr;
220 if (level != I915_CACHE_NONE)
221 pde |= PPAT_CACHED_PDE_INDEX;
222 else
223 pde |= PPAT_UNCACHED_INDEX;
224 return pde;
225}
226
Michel Thierry762d9932015-07-30 11:05:29 +0100227#define gen8_pdpe_encode gen8_pde_encode
228#define gen8_pml4e_encode gen8_pde_encode
229
Michel Thierry07749ef2015-03-16 16:00:54 +0000230static gen6_pte_t snb_pte_encode(dma_addr_t addr,
231 enum i915_cache_level level,
232 bool valid, u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700233{
Michel Thierry07749ef2015-03-16 16:00:54 +0000234 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -0700235 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700236
237 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100238 case I915_CACHE_L3_LLC:
239 case I915_CACHE_LLC:
240 pte |= GEN6_PTE_CACHE_LLC;
241 break;
242 case I915_CACHE_NONE:
243 pte |= GEN6_PTE_UNCACHED;
244 break;
245 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100246 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100247 }
248
249 return pte;
250}
251
Michel Thierry07749ef2015-03-16 16:00:54 +0000252static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
253 enum i915_cache_level level,
254 bool valid, u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100255{
Michel Thierry07749ef2015-03-16 16:00:54 +0000256 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100257 pte |= GEN6_PTE_ADDR_ENCODE(addr);
258
259 switch (level) {
260 case I915_CACHE_L3_LLC:
261 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700262 break;
263 case I915_CACHE_LLC:
264 pte |= GEN6_PTE_CACHE_LLC;
265 break;
266 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700267 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700268 break;
269 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100270 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700271 }
272
Ben Widawsky54d12522012-09-24 16:44:32 -0700273 return pte;
274}
275
Michel Thierry07749ef2015-03-16 16:00:54 +0000276static gen6_pte_t byt_pte_encode(dma_addr_t addr,
277 enum i915_cache_level level,
278 bool valid, u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700279{
Michel Thierry07749ef2015-03-16 16:00:54 +0000280 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700281 pte |= GEN6_PTE_ADDR_ENCODE(addr);
282
Akash Goel24f3a8c2014-06-17 10:59:42 +0530283 if (!(flags & PTE_READ_ONLY))
284 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700285
286 if (level != I915_CACHE_NONE)
287 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
288
289 return pte;
290}
291
Michel Thierry07749ef2015-03-16 16:00:54 +0000292static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
293 enum i915_cache_level level,
294 bool valid, u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700295{
Michel Thierry07749ef2015-03-16 16:00:54 +0000296 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700297 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700298
299 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700300 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700301
302 return pte;
303}
304
Michel Thierry07749ef2015-03-16 16:00:54 +0000305static gen6_pte_t iris_pte_encode(dma_addr_t addr,
306 enum i915_cache_level level,
307 bool valid, u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700308{
Michel Thierry07749ef2015-03-16 16:00:54 +0000309 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700310 pte |= HSW_PTE_ADDR_ENCODE(addr);
311
Chris Wilson651d7942013-08-08 14:41:10 +0100312 switch (level) {
313 case I915_CACHE_NONE:
314 break;
315 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000316 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100317 break;
318 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000319 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100320 break;
321 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700322
323 return pte;
324}
325
Mika Kuoppalac114f762015-06-25 18:35:13 +0300326static int __setup_page_dma(struct drm_device *dev,
327 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000328{
David Weinehallc49d13e2016-08-22 13:32:42 +0300329 struct device *kdev = &dev->pdev->dev;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000330
Mika Kuoppalac114f762015-06-25 18:35:13 +0300331 p->page = alloc_page(flags);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300332 if (!p->page)
Michel Thierry1266cdb2015-03-24 17:06:33 +0000333 return -ENOMEM;
334
David Weinehallc49d13e2016-08-22 13:32:42 +0300335 p->daddr = dma_map_page(kdev,
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300336 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
337
David Weinehallc49d13e2016-08-22 13:32:42 +0300338 if (dma_mapping_error(kdev, p->daddr)) {
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300339 __free_page(p->page);
340 return -EINVAL;
341 }
342
Michel Thierry1266cdb2015-03-24 17:06:33 +0000343 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000344}
345
Mika Kuoppalac114f762015-06-25 18:35:13 +0300346static int setup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
347{
348 return __setup_page_dma(dev, p, GFP_KERNEL);
349}
350
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300351static void cleanup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
352{
353 if (WARN_ON(!p->page))
354 return;
355
356 dma_unmap_page(&dev->pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
357 __free_page(p->page);
358 memset(p, 0, sizeof(*p));
359}
360
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300361static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300362{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300363 return kmap_atomic(p->page);
364}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300365
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300366/* We use the flushing unmap only with ppgtt structures:
367 * page directories, page tables and scratch pages.
368 */
369static void kunmap_page_dma(struct drm_device *dev, void *vaddr)
370{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300371 /* There are only few exceptions for gen >=6. chv and bxt.
372 * And we are not sure about the latter so play safe for now.
373 */
374 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
375 drm_clflush_virt_range(vaddr, PAGE_SIZE);
376
377 kunmap_atomic(vaddr);
378}
379
Mika Kuoppala567047b2015-06-25 18:35:12 +0300380#define kmap_px(px) kmap_page_dma(px_base(px))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300381#define kunmap_px(ppgtt, vaddr) kunmap_page_dma((ppgtt)->base.dev, (vaddr))
382
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))
385#define fill_px(dev, px, v) fill_page_dma((dev), px_base(px), (v))
386#define fill32_px(dev, px, v) fill_page_dma_32((dev), px_base(px), (v))
387
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300388static void fill_page_dma(struct drm_device *dev, struct i915_page_dma *p,
389 const uint64_t val)
390{
391 int i;
392 uint64_t * const vaddr = kmap_page_dma(p);
393
394 for (i = 0; i < 512; i++)
395 vaddr[i] = val;
396
397 kunmap_page_dma(dev, vaddr);
398}
399
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300400static void fill_page_dma_32(struct drm_device *dev, struct i915_page_dma *p,
401 const uint32_t val32)
402{
403 uint64_t v = val32;
404
405 v = v << 32 | val32;
406
407 fill_page_dma(dev, p, v);
408}
409
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300410static struct i915_page_scratch *alloc_scratch_page(struct drm_device *dev)
411{
412 struct i915_page_scratch *sp;
413 int ret;
414
415 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
416 if (sp == NULL)
417 return ERR_PTR(-ENOMEM);
418
419 ret = __setup_page_dma(dev, px_base(sp), GFP_DMA32 | __GFP_ZERO);
420 if (ret) {
421 kfree(sp);
422 return ERR_PTR(ret);
423 }
424
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300425 return sp;
426}
427
428static void free_scratch_page(struct drm_device *dev,
429 struct i915_page_scratch *sp)
430{
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300431 cleanup_px(dev, sp);
432 kfree(sp);
433}
434
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300435static struct i915_page_table *alloc_pt(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000436{
Michel Thierryec565b32015-04-08 12:13:23 +0100437 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000438 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
439 GEN8_PTES : GEN6_PTES;
440 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000441
442 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
443 if (!pt)
444 return ERR_PTR(-ENOMEM);
445
Ben Widawsky678d96f2015-03-16 16:00:56 +0000446 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
447 GFP_KERNEL);
448
449 if (!pt->used_ptes)
450 goto fail_bitmap;
451
Mika Kuoppala567047b2015-06-25 18:35:12 +0300452 ret = setup_px(dev, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000453 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300454 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000455
456 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000457
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300458fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000459 kfree(pt->used_ptes);
460fail_bitmap:
461 kfree(pt);
462
463 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000464}
465
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300466static void free_pt(struct drm_device *dev, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000467{
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300468 cleanup_px(dev, pt);
469 kfree(pt->used_ptes);
470 kfree(pt);
471}
472
473static void gen8_initialize_pt(struct i915_address_space *vm,
474 struct i915_page_table *pt)
475{
476 gen8_pte_t scratch_pte;
477
478 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
479 I915_CACHE_LLC, true);
480
481 fill_px(vm->dev, pt, scratch_pte);
482}
483
484static void gen6_initialize_pt(struct i915_address_space *vm,
485 struct i915_page_table *pt)
486{
487 gen6_pte_t scratch_pte;
488
489 WARN_ON(px_dma(vm->scratch_page) == 0);
490
491 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
492 I915_CACHE_LLC, true, 0);
493
494 fill32_px(vm->dev, pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000495}
496
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300497static struct i915_page_directory *alloc_pd(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000498{
Michel Thierryec565b32015-04-08 12:13:23 +0100499 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100500 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000501
502 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
503 if (!pd)
504 return ERR_PTR(-ENOMEM);
505
Michel Thierry33c88192015-04-08 12:13:33 +0100506 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
507 sizeof(*pd->used_pdes), GFP_KERNEL);
508 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300509 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100510
Mika Kuoppala567047b2015-06-25 18:35:12 +0300511 ret = setup_px(dev, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100512 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300513 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100514
Ben Widawsky06fda602015-02-24 16:22:36 +0000515 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100516
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300517fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100518 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300519fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100520 kfree(pd);
521
522 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000523}
524
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300525static void free_pd(struct drm_device *dev, struct i915_page_directory *pd)
526{
527 if (px_page(pd)) {
528 cleanup_px(dev, pd);
529 kfree(pd->used_pdes);
530 kfree(pd);
531 }
532}
533
534static void gen8_initialize_pd(struct i915_address_space *vm,
535 struct i915_page_directory *pd)
536{
537 gen8_pde_t scratch_pde;
538
539 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
540
541 fill_px(vm->dev, pd, scratch_pde);
542}
543
Michel Thierry6ac18502015-07-29 17:23:46 +0100544static int __pdp_init(struct drm_device *dev,
545 struct i915_page_directory_pointer *pdp)
546{
547 size_t pdpes = I915_PDPES_PER_PDP(dev);
548
549 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
550 sizeof(unsigned long),
551 GFP_KERNEL);
552 if (!pdp->used_pdpes)
553 return -ENOMEM;
554
555 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
556 GFP_KERNEL);
557 if (!pdp->page_directory) {
558 kfree(pdp->used_pdpes);
559 /* the PDP might be the statically allocated top level. Keep it
560 * as clean as possible */
561 pdp->used_pdpes = NULL;
562 return -ENOMEM;
563 }
564
565 return 0;
566}
567
568static void __pdp_fini(struct i915_page_directory_pointer *pdp)
569{
570 kfree(pdp->used_pdpes);
571 kfree(pdp->page_directory);
572 pdp->page_directory = NULL;
573}
574
Michel Thierry762d9932015-07-30 11:05:29 +0100575static struct
576i915_page_directory_pointer *alloc_pdp(struct drm_device *dev)
577{
578 struct i915_page_directory_pointer *pdp;
579 int ret = -ENOMEM;
580
581 WARN_ON(!USES_FULL_48BIT_PPGTT(dev));
582
583 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
584 if (!pdp)
585 return ERR_PTR(-ENOMEM);
586
587 ret = __pdp_init(dev, pdp);
588 if (ret)
589 goto fail_bitmap;
590
591 ret = setup_px(dev, pdp);
592 if (ret)
593 goto fail_page_m;
594
595 return pdp;
596
597fail_page_m:
598 __pdp_fini(pdp);
599fail_bitmap:
600 kfree(pdp);
601
602 return ERR_PTR(ret);
603}
604
Michel Thierry6ac18502015-07-29 17:23:46 +0100605static void free_pdp(struct drm_device *dev,
606 struct i915_page_directory_pointer *pdp)
607{
608 __pdp_fini(pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100609 if (USES_FULL_48BIT_PPGTT(dev)) {
610 cleanup_px(dev, pdp);
611 kfree(pdp);
612 }
613}
614
Michel Thierry69ab76f2015-07-29 17:23:55 +0100615static void gen8_initialize_pdp(struct i915_address_space *vm,
616 struct i915_page_directory_pointer *pdp)
617{
618 gen8_ppgtt_pdpe_t scratch_pdpe;
619
620 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
621
622 fill_px(vm->dev, pdp, scratch_pdpe);
623}
624
625static void gen8_initialize_pml4(struct i915_address_space *vm,
626 struct i915_pml4 *pml4)
627{
628 gen8_ppgtt_pml4e_t scratch_pml4e;
629
630 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
631 I915_CACHE_LLC);
632
633 fill_px(vm->dev, pml4, scratch_pml4e);
634}
635
Michel Thierry762d9932015-07-30 11:05:29 +0100636static void
637gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
638 struct i915_page_directory_pointer *pdp,
639 struct i915_page_directory *pd,
640 int index)
641{
642 gen8_ppgtt_pdpe_t *page_directorypo;
643
644 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
645 return;
646
647 page_directorypo = kmap_px(pdp);
648 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
649 kunmap_px(ppgtt, page_directorypo);
650}
651
652static void
653gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
654 struct i915_pml4 *pml4,
655 struct i915_page_directory_pointer *pdp,
656 int index)
657{
658 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
659
660 WARN_ON(!USES_FULL_48BIT_PPGTT(ppgtt->base.dev));
661 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
662 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100663}
664
Ben Widawsky94e409c2013-11-04 22:29:36 -0800665/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100666static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100667 unsigned entry,
668 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800669{
Chris Wilson7e37f882016-08-02 22:50:21 +0100670 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000671 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800672 int ret;
673
674 BUG_ON(entry >= 4);
675
John Harrison5fb9de12015-05-29 17:44:07 +0100676 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800677 if (ret)
678 return ret;
679
Chris Wilsonb5321f32016-08-02 22:50:18 +0100680 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
681 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
682 intel_ring_emit(ring, upper_32_bits(addr));
683 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
684 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
685 intel_ring_emit(ring, lower_32_bits(addr));
686 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800687
688 return 0;
689}
690
Michel Thierry2dba3232015-07-30 11:06:23 +0100691static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
692 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800693{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800694 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800695
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100696 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300697 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
698
John Harrisone85b26d2015-05-29 17:43:56 +0100699 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800700 if (ret)
701 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800702 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800703
Ben Widawskyeeb94882013-12-06 14:11:10 -0800704 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800705}
706
Michel Thierry2dba3232015-07-30 11:06:23 +0100707static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
708 struct drm_i915_gem_request *req)
709{
710 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
711}
712
Michel Thierryf9b5b782015-07-30 11:02:49 +0100713static void gen8_ppgtt_clear_pte_range(struct i915_address_space *vm,
714 struct i915_page_directory_pointer *pdp,
715 uint64_t start,
716 uint64_t length,
717 gen8_pte_t scratch_pte)
Ben Widawsky459108b2013-11-02 21:07:23 -0700718{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300719 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100720 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100721 unsigned pdpe = gen8_pdpe_index(start);
722 unsigned pde = gen8_pde_index(start);
723 unsigned pte = gen8_pte_index(start);
Ben Widawsky782f1492014-02-20 11:50:33 -0800724 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky459108b2013-11-02 21:07:23 -0700725 unsigned last_pte, i;
726
Michel Thierryf9b5b782015-07-30 11:02:49 +0100727 if (WARN_ON(!pdp))
728 return;
Ben Widawsky459108b2013-11-02 21:07:23 -0700729
730 while (num_entries) {
Michel Thierryec565b32015-04-08 12:13:23 +0100731 struct i915_page_directory *pd;
732 struct i915_page_table *pt;
Ben Widawsky06fda602015-02-24 16:22:36 +0000733
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100734 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100735 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000736
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100737 pd = pdp->page_directory[pdpe];
Ben Widawsky06fda602015-02-24 16:22:36 +0000738
739 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100740 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000741
742 pt = pd->page_table[pde];
743
Mika Kuoppala567047b2015-06-25 18:35:12 +0300744 if (WARN_ON(!px_page(pt)))
Michel Thierry00245262015-06-25 12:59:38 +0100745 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000746
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800747 last_pte = pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +0000748 if (last_pte > GEN8_PTES)
749 last_pte = GEN8_PTES;
Ben Widawsky459108b2013-11-02 21:07:23 -0700750
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300751 pt_vaddr = kmap_px(pt);
Ben Widawsky459108b2013-11-02 21:07:23 -0700752
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800753 for (i = pte; i < last_pte; i++) {
Ben Widawsky459108b2013-11-02 21:07:23 -0700754 pt_vaddr[i] = scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800755 num_entries--;
756 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700757
Matthew Auld44a71022016-04-12 16:57:42 +0100758 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky459108b2013-11-02 21:07:23 -0700759
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800760 pte = 0;
Michel Thierry07749ef2015-03-16 16:00:54 +0000761 if (++pde == I915_PDES) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100762 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
763 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800764 pde = 0;
765 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700766 }
767}
768
Michel Thierryf9b5b782015-07-30 11:02:49 +0100769static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
770 uint64_t start,
771 uint64_t length,
772 bool use_scratch)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700773{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300774 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100775 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
776 I915_CACHE_LLC, use_scratch);
777
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100778 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
779 gen8_ppgtt_clear_pte_range(vm, &ppgtt->pdp, start, length,
780 scratch_pte);
781 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000782 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100783 struct i915_page_directory_pointer *pdp;
784
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000785 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100786 gen8_ppgtt_clear_pte_range(vm, pdp, start, length,
787 scratch_pte);
788 }
789 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100790}
791
792static void
793gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
794 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100795 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100796 uint64_t start,
797 enum i915_cache_level cache_level)
798{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300799 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000800 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100801 unsigned pdpe = gen8_pdpe_index(start);
802 unsigned pde = gen8_pde_index(start);
803 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700804
Chris Wilson6f1cc992013-12-31 15:50:31 +0000805 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700806
Michel Thierry3387d432015-08-03 09:52:47 +0100807 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000808 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100809 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100810 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300811 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000812 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800813
814 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100815 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Chris Wilson6f1cc992013-12-31 15:50:31 +0000816 cache_level, true);
Michel Thierry07749ef2015-03-16 16:00:54 +0000817 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300818 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000819 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000820 if (++pde == I915_PDES) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100821 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
822 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800823 pde = 0;
824 }
825 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700826 }
827 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300828
829 if (pt_vaddr)
830 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700831}
832
Michel Thierryf9b5b782015-07-30 11:02:49 +0100833static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
834 struct sg_table *pages,
835 uint64_t start,
836 enum i915_cache_level cache_level,
837 u32 unused)
838{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300839 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100840 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100841
Michel Thierry3387d432015-08-03 09:52:47 +0100842 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100843
844 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
845 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
846 cache_level);
847 } else {
848 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000849 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100850 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
851
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000852 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100853 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
854 start, cache_level);
855 }
856 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100857}
858
Michel Thierryf37c0502015-06-10 17:46:39 +0100859static void gen8_free_page_tables(struct drm_device *dev,
860 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800861{
862 int i;
863
Mika Kuoppala567047b2015-06-25 18:35:12 +0300864 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800865 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800866
Michel Thierry33c88192015-04-08 12:13:33 +0100867 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000868 if (WARN_ON(!pd->page_table[i]))
869 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800870
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300871 free_pt(dev, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000872 pd->page_table[i] = NULL;
873 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000874}
875
Mika Kuoppala8776f022015-06-30 18:16:40 +0300876static int gen8_init_scratch(struct i915_address_space *vm)
877{
878 struct drm_device *dev = vm->dev;
Matthew Auld64c050d2016-04-27 13:19:25 +0100879 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300880
881 vm->scratch_page = alloc_scratch_page(dev);
882 if (IS_ERR(vm->scratch_page))
883 return PTR_ERR(vm->scratch_page);
884
885 vm->scratch_pt = alloc_pt(dev);
886 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100887 ret = PTR_ERR(vm->scratch_pt);
888 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300889 }
890
891 vm->scratch_pd = alloc_pd(dev);
892 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100893 ret = PTR_ERR(vm->scratch_pd);
894 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300895 }
896
Michel Thierry69ab76f2015-07-29 17:23:55 +0100897 if (USES_FULL_48BIT_PPGTT(dev)) {
898 vm->scratch_pdp = alloc_pdp(dev);
899 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100900 ret = PTR_ERR(vm->scratch_pdp);
901 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100902 }
903 }
904
Mika Kuoppala8776f022015-06-30 18:16:40 +0300905 gen8_initialize_pt(vm, vm->scratch_pt);
906 gen8_initialize_pd(vm, vm->scratch_pd);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100907 if (USES_FULL_48BIT_PPGTT(dev))
908 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300909
910 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100911
912free_pd:
913 free_pd(dev, vm->scratch_pd);
914free_pt:
915 free_pt(dev, vm->scratch_pt);
916free_scratch_page:
917 free_scratch_page(dev, vm->scratch_page);
918
919 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300920}
921
Zhiyuan Lv650da342015-08-28 15:41:18 +0800922static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
923{
924 enum vgt_g2v_type msg;
Matthew Aulddf285642016-04-22 12:09:25 +0100925 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Zhiyuan Lv650da342015-08-28 15:41:18 +0800926 int i;
927
Matthew Aulddf285642016-04-22 12:09:25 +0100928 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +0800929 u64 daddr = px_dma(&ppgtt->pml4);
930
Ville Syrjäläab75bb52015-11-04 23:20:12 +0200931 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
932 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +0800933
934 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
935 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
936 } else {
937 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
938 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
939
Ville Syrjäläab75bb52015-11-04 23:20:12 +0200940 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
941 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +0800942 }
943
944 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
945 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
946 }
947
948 I915_WRITE(vgtif_reg(g2v_notify), msg);
949
950 return 0;
951}
952
Mika Kuoppala8776f022015-06-30 18:16:40 +0300953static void gen8_free_scratch(struct i915_address_space *vm)
954{
955 struct drm_device *dev = vm->dev;
956
Michel Thierry69ab76f2015-07-29 17:23:55 +0100957 if (USES_FULL_48BIT_PPGTT(dev))
958 free_pdp(dev, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300959 free_pd(dev, vm->scratch_pd);
960 free_pt(dev, vm->scratch_pt);
961 free_scratch_page(dev, vm->scratch_page);
962}
963
Michel Thierry762d9932015-07-30 11:05:29 +0100964static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
965 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800966{
967 int i;
968
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100969 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
970 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +0000971 continue;
972
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100973 gen8_free_page_tables(dev, pdp->page_directory[i]);
974 free_pd(dev, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800975 }
Michel Thierry69876be2015-04-08 12:13:27 +0100976
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100977 free_pdp(dev, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100978}
979
980static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
981{
982 int i;
983
984 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
985 if (WARN_ON(!ppgtt->pml4.pdps[i]))
986 continue;
987
988 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
989 }
990
991 cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
992}
993
994static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
995{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300996 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +0100997
Chris Wilsonc0336662016-05-06 15:40:21 +0100998 if (intel_vgpu_active(to_i915(vm->dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +0800999 gen8_ppgtt_notify_vgt(ppgtt, false);
1000
Michel Thierry762d9932015-07-30 11:05:29 +01001001 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
1002 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
1003 else
1004 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001005
Mika Kuoppala8776f022015-06-30 18:16:40 +03001006 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001007}
1008
Michel Thierryd7b26332015-04-08 12:13:34 +01001009/**
1010 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001011 * @vm: Master vm structure.
1012 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001013 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001014 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001015 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1016 * caller to free on error.
1017 *
1018 * Allocate the required number of page tables. Extremely similar to
1019 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1020 * the page directory boundary (instead of the page directory pointer). That
1021 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1022 * possible, and likely that the caller will need to use multiple calls of this
1023 * function to achieve the appropriate allocation.
1024 *
1025 * Return: 0 if success; negative error code otherwise.
1026 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001027static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001028 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001029 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001030 uint64_t length,
1031 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001032{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001033 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001034 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001035 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001036
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001037 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001038 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001039 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001040 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001041 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001042 continue;
1043 }
1044
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001045 pt = alloc_pt(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001046 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001047 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001048
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001049 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001050 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001051 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001052 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001053 }
1054
1055 return 0;
1056
1057unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001058 for_each_set_bit(pde, new_pts, I915_PDES)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001059 free_pt(dev, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001060
1061 return -ENOMEM;
1062}
1063
Michel Thierryd7b26332015-04-08 12:13:34 +01001064/**
1065 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001066 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001067 * @pdp: Page directory pointer for this address range.
1068 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001069 * @length: Size of the allocations.
1070 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001071 * caller to free on error.
1072 *
1073 * Allocate the required number of page directories starting at the pde index of
1074 * @start, and ending at the pde index @start + @length. This function will skip
1075 * over already allocated page directories within the range, and only allocate
1076 * new ones, setting the appropriate pointer within the pdp as well as the
1077 * correct position in the bitmap @new_pds.
1078 *
1079 * The function will only allocate the pages within the range for a give page
1080 * directory pointer. In other words, if @start + @length straddles a virtually
1081 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1082 * required by the caller, This is not currently possible, and the BUG in the
1083 * code will prevent it.
1084 *
1085 * Return: 0 if success; negative error code otherwise.
1086 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001087static int
1088gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1089 struct i915_page_directory_pointer *pdp,
1090 uint64_t start,
1091 uint64_t length,
1092 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001093{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001094 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001095 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001096 uint32_t pdpe;
Michel Thierry6ac18502015-07-29 17:23:46 +01001097 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001098
Michel Thierry6ac18502015-07-29 17:23:46 +01001099 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001100
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001101 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001102 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001103 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001104
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001105 pd = alloc_pd(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001106 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001107 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001108
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001109 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001110 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001111 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001112 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001113 }
1114
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001115 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001116
1117unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001118 for_each_set_bit(pdpe, new_pds, pdpes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001119 free_pd(dev, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001120
1121 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001122}
1123
Michel Thierry762d9932015-07-30 11:05:29 +01001124/**
1125 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1126 * @vm: Master vm structure.
1127 * @pml4: Page map level 4 for this address range.
1128 * @start: Starting virtual address to begin allocations.
1129 * @length: Size of the allocations.
1130 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1131 * caller to free on error.
1132 *
1133 * Allocate the required number of page directory pointers. Extremely similar to
1134 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1135 * The main difference is here we are limited by the pml4 boundary (instead of
1136 * the page directory pointer).
1137 *
1138 * Return: 0 if success; negative error code otherwise.
1139 */
1140static int
1141gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1142 struct i915_pml4 *pml4,
1143 uint64_t start,
1144 uint64_t length,
1145 unsigned long *new_pdps)
1146{
1147 struct drm_device *dev = vm->dev;
1148 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001149 uint32_t pml4e;
1150
1151 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1152
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001153 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001154 if (!test_bit(pml4e, pml4->used_pml4es)) {
1155 pdp = alloc_pdp(dev);
1156 if (IS_ERR(pdp))
1157 goto unwind_out;
1158
Michel Thierry69ab76f2015-07-29 17:23:55 +01001159 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001160 pml4->pdps[pml4e] = pdp;
1161 __set_bit(pml4e, new_pdps);
1162 trace_i915_page_directory_pointer_entry_alloc(vm,
1163 pml4e,
1164 start,
1165 GEN8_PML4E_SHIFT);
1166 }
1167 }
1168
1169 return 0;
1170
1171unwind_out:
1172 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1173 free_pdp(dev, pml4->pdps[pml4e]);
1174
1175 return -ENOMEM;
1176}
1177
Michel Thierryd7b26332015-04-08 12:13:34 +01001178static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001179free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001180{
Michel Thierryd7b26332015-04-08 12:13:34 +01001181 kfree(new_pts);
1182 kfree(new_pds);
1183}
1184
1185/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1186 * of these are based on the number of PDPEs in the system.
1187 */
1188static
1189int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001190 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001191 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001192{
Michel Thierryd7b26332015-04-08 12:13:34 +01001193 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001194 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001195
Michał Winiarski3a41a052015-09-03 19:22:18 +02001196 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001197 if (!pds)
1198 return -ENOMEM;
1199
Michał Winiarski3a41a052015-09-03 19:22:18 +02001200 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1201 GFP_TEMPORARY);
1202 if (!pts)
1203 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001204
1205 *new_pds = pds;
1206 *new_pts = pts;
1207
1208 return 0;
1209
1210err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001211 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001212 return -ENOMEM;
1213}
1214
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001215/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
1216 * the page table structures, we mark them dirty so that
1217 * context switching/execlist queuing code takes extra steps
1218 * to ensure that tlbs are flushed.
1219 */
1220static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1221{
1222 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1223}
1224
Michel Thierry762d9932015-07-30 11:05:29 +01001225static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1226 struct i915_page_directory_pointer *pdp,
1227 uint64_t start,
1228 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001229{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001230 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001231 unsigned long *new_page_dirs, *new_page_tables;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001232 struct drm_device *dev = vm->dev;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001233 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001234 const uint64_t orig_start = start;
1235 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001236 uint32_t pdpe;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001237 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001238 int ret;
1239
Michel Thierryd7b26332015-04-08 12:13:34 +01001240 /* Wrap is never okay since we can only represent 48b, and we don't
1241 * actually use the other side of the canonical address space.
1242 */
1243 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001244 return -ENODEV;
1245
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001246 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001247 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001248
Michel Thierry6ac18502015-07-29 17:23:46 +01001249 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001250 if (ret)
1251 return ret;
1252
Michel Thierryd7b26332015-04-08 12:13:34 +01001253 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001254 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1255 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001256 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001257 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001258 return ret;
1259 }
1260
1261 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001262 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001263 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001264 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001265 if (ret)
1266 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001267 }
1268
Michel Thierry33c88192015-04-08 12:13:33 +01001269 start = orig_start;
1270 length = orig_length;
1271
Michel Thierryd7b26332015-04-08 12:13:34 +01001272 /* Allocations have completed successfully, so set the bitmaps, and do
1273 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001274 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001275 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001276 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001277 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001278 uint64_t pd_start = start;
1279 uint32_t pde;
1280
Michel Thierryd7b26332015-04-08 12:13:34 +01001281 /* Every pd should be allocated, we just did that above. */
1282 WARN_ON(!pd);
1283
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001284 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001285 /* Same reasoning as pd */
1286 WARN_ON(!pt);
1287 WARN_ON(!pd_len);
1288 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1289
1290 /* Set our used ptes within the page table */
1291 bitmap_set(pt->used_ptes,
1292 gen8_pte_index(pd_start),
1293 gen8_pte_count(pd_start, pd_len));
1294
1295 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001296 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001297
1298 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001299 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1300 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001301 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1302 gen8_pte_index(start),
1303 gen8_pte_count(start, length),
1304 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001305
1306 /* NB: We haven't yet mapped ptes to pages. At this
1307 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001308 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001309
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001310 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001311 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001312 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001313 }
1314
Michał Winiarski3a41a052015-09-03 19:22:18 +02001315 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001316 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001317 return 0;
1318
1319err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001320 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001321 unsigned long temp;
1322
Michał Winiarski3a41a052015-09-03 19:22:18 +02001323 for_each_set_bit(temp, new_page_tables + pdpe *
1324 BITS_TO_LONGS(I915_PDES), I915_PDES)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001325 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001326 }
1327
Michel Thierry6ac18502015-07-29 17:23:46 +01001328 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001329 free_pd(dev, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001330
Michał Winiarski3a41a052015-09-03 19:22:18 +02001331 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001332 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001333 return ret;
1334}
1335
Michel Thierry762d9932015-07-30 11:05:29 +01001336static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1337 struct i915_pml4 *pml4,
1338 uint64_t start,
1339 uint64_t length)
1340{
1341 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001342 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001343 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001344 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001345 int ret = 0;
1346
1347 /* Do the pml4 allocations first, so we don't need to track the newly
1348 * allocated tables below the pdp */
1349 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1350
1351 /* The pagedirectory and pagetable allocations are done in the shared 3
1352 * and 4 level code. Just allocate the pdps.
1353 */
1354 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1355 new_pdps);
1356 if (ret)
1357 return ret;
1358
1359 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1360 "The allocation has spanned more than 512GB. "
1361 "It is highly likely this is incorrect.");
1362
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001363 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001364 WARN_ON(!pdp);
1365
1366 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1367 if (ret)
1368 goto err_out;
1369
1370 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1371 }
1372
1373 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1374 GEN8_PML4ES_PER_PML4);
1375
1376 return 0;
1377
1378err_out:
1379 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1380 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
1381
1382 return ret;
1383}
1384
1385static int gen8_alloc_va_range(struct i915_address_space *vm,
1386 uint64_t start, uint64_t length)
1387{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001388 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001389
1390 if (USES_FULL_48BIT_PPGTT(vm->dev))
1391 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1392 else
1393 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1394}
1395
Michel Thierryea91e402015-07-29 17:23:57 +01001396static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1397 uint64_t start, uint64_t length,
1398 gen8_pte_t scratch_pte,
1399 struct seq_file *m)
1400{
1401 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001402 uint32_t pdpe;
1403
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001404 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001405 struct i915_page_table *pt;
1406 uint64_t pd_len = length;
1407 uint64_t pd_start = start;
1408 uint32_t pde;
1409
1410 if (!test_bit(pdpe, pdp->used_pdpes))
1411 continue;
1412
1413 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001414 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001415 uint32_t pte;
1416 gen8_pte_t *pt_vaddr;
1417
1418 if (!test_bit(pde, pd->used_pdes))
1419 continue;
1420
1421 pt_vaddr = kmap_px(pt);
1422 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1423 uint64_t va =
1424 (pdpe << GEN8_PDPE_SHIFT) |
1425 (pde << GEN8_PDE_SHIFT) |
1426 (pte << GEN8_PTE_SHIFT);
1427 int i;
1428 bool found = false;
1429
1430 for (i = 0; i < 4; i++)
1431 if (pt_vaddr[pte + i] != scratch_pte)
1432 found = true;
1433 if (!found)
1434 continue;
1435
1436 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1437 for (i = 0; i < 4; i++) {
1438 if (pt_vaddr[pte + i] != scratch_pte)
1439 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1440 else
1441 seq_puts(m, " SCRATCH ");
1442 }
1443 seq_puts(m, "\n");
1444 }
1445 /* don't use kunmap_px, it could trigger
1446 * an unnecessary flush.
1447 */
1448 kunmap_atomic(pt_vaddr);
1449 }
1450 }
1451}
1452
1453static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1454{
1455 struct i915_address_space *vm = &ppgtt->base;
1456 uint64_t start = ppgtt->base.start;
1457 uint64_t length = ppgtt->base.total;
1458 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
1459 I915_CACHE_LLC, true);
1460
1461 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
1462 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1463 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001464 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001465 struct i915_pml4 *pml4 = &ppgtt->pml4;
1466 struct i915_page_directory_pointer *pdp;
1467
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001468 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001469 if (!test_bit(pml4e, pml4->used_pml4es))
1470 continue;
1471
1472 seq_printf(m, " PML4E #%llu\n", pml4e);
1473 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1474 }
1475 }
1476}
1477
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001478static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1479{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001480 unsigned long *new_page_dirs, *new_page_tables;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001481 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1482 int ret;
1483
1484 /* We allocate temp bitmap for page tables for no gain
1485 * but as this is for init only, lets keep the things simple
1486 */
1487 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1488 if (ret)
1489 return ret;
1490
1491 /* Allocate for all pdps regardless of how the ppgtt
1492 * was defined.
1493 */
1494 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1495 0, 1ULL << 32,
1496 new_page_dirs);
1497 if (!ret)
1498 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1499
Michał Winiarski3a41a052015-09-03 19:22:18 +02001500 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001501
1502 return ret;
1503}
1504
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001505/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001506 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1507 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1508 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1509 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001510 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001511 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001512static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001513{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001514 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001515
Mika Kuoppala8776f022015-06-30 18:16:40 +03001516 ret = gen8_init_scratch(&ppgtt->base);
1517 if (ret)
1518 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001519
Michel Thierryd7b26332015-04-08 12:13:34 +01001520 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001521 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001522 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001523 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001524 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001525 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1526 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001527 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001528
Michel Thierry762d9932015-07-30 11:05:29 +01001529 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
1530 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
1531 if (ret)
1532 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001533
Michel Thierry69ab76f2015-07-29 17:23:55 +01001534 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1535
Michel Thierry762d9932015-07-30 11:05:29 +01001536 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001537 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001538 } else {
Michel Thierry25f50332015-08-07 17:40:19 +01001539 ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001540 if (ret)
1541 goto free_scratch;
1542
1543 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001544 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001545 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1546 0, 0,
1547 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001548
Chris Wilsonc0336662016-05-06 15:40:21 +01001549 if (intel_vgpu_active(to_i915(ppgtt->base.dev))) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001550 ret = gen8_preallocate_top_level_pdps(ppgtt);
1551 if (ret)
1552 goto free_scratch;
1553 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001554 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001555
Chris Wilsonc0336662016-05-06 15:40:21 +01001556 if (intel_vgpu_active(to_i915(ppgtt->base.dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001557 gen8_ppgtt_notify_vgt(ppgtt, true);
1558
Michel Thierryd7b26332015-04-08 12:13:34 +01001559 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001560
1561free_scratch:
1562 gen8_free_scratch(&ppgtt->base);
1563 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001564}
1565
Ben Widawsky87d60b62013-12-06 14:11:29 -08001566static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1567{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001568 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001569 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001570 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001571 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001572 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001573 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001574
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001575 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1576 I915_CACHE_LLC, true, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001577
Dave Gordon731f74c2016-06-24 19:37:46 +01001578 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001579 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001580 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001581 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001582 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001583 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1584
1585 if (pd_entry != expected)
1586 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1587 pde,
1588 pd_entry,
1589 expected);
1590 seq_printf(m, "\tPDE: %x\n", pd_entry);
1591
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001592 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1593
Michel Thierry07749ef2015-03-16 16:00:54 +00001594 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001595 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001596 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001597 (pte * PAGE_SIZE);
1598 int i;
1599 bool found = false;
1600 for (i = 0; i < 4; i++)
1601 if (pt_vaddr[pte + i] != scratch_pte)
1602 found = true;
1603 if (!found)
1604 continue;
1605
1606 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1607 for (i = 0; i < 4; i++) {
1608 if (pt_vaddr[pte + i] != scratch_pte)
1609 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1610 else
1611 seq_puts(m, " SCRATCH ");
1612 }
1613 seq_puts(m, "\n");
1614 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001615 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001616 }
1617}
1618
Ben Widawsky678d96f2015-03-16 16:00:56 +00001619/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001620static void gen6_write_pde(struct i915_page_directory *pd,
1621 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001622{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001623 /* Caller needs to make sure the write completes if necessary */
1624 struct i915_hw_ppgtt *ppgtt =
1625 container_of(pd, struct i915_hw_ppgtt, pd);
1626 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001627
Mika Kuoppala567047b2015-06-25 18:35:12 +03001628 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001629 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001630
Ben Widawsky678d96f2015-03-16 16:00:56 +00001631 writel(pd_entry, ppgtt->pd_addr + pde);
1632}
Ben Widawsky61973492013-04-08 18:43:54 -07001633
Ben Widawsky678d96f2015-03-16 16:00:56 +00001634/* Write all the page tables found in the ppgtt structure to incrementing page
1635 * directories. */
1636static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001637 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001638 uint32_t start, uint32_t length)
1639{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001640 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001641 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001642 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001643
Dave Gordon731f74c2016-06-24 19:37:46 +01001644 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001645 gen6_write_pde(pd, pde, pt);
1646
1647 /* Make sure write is complete before other code can use this page
1648 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001649 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001650}
1651
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001652static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001653{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001654 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001655
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001656 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001657}
Ben Widawsky61973492013-04-08 18:43:54 -07001658
Ben Widawsky90252e52013-12-06 14:11:12 -08001659static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001660 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001661{
Chris Wilson7e37f882016-08-02 22:50:21 +01001662 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001663 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001664 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001665
Ben Widawsky90252e52013-12-06 14:11:12 -08001666 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001667 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001668 if (ret)
1669 return ret;
1670
John Harrison5fb9de12015-05-29 17:44:07 +01001671 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001672 if (ret)
1673 return ret;
1674
Chris Wilsonb5321f32016-08-02 22:50:18 +01001675 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1676 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1677 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1678 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1679 intel_ring_emit(ring, get_pd_offset(ppgtt));
1680 intel_ring_emit(ring, MI_NOOP);
1681 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001682
1683 return 0;
1684}
1685
Ben Widawsky48a10382013-12-06 14:11:11 -08001686static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001687 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001688{
Chris Wilson7e37f882016-08-02 22:50:21 +01001689 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001690 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001691 int ret;
1692
Ben Widawsky48a10382013-12-06 14:11:11 -08001693 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001694 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001695 if (ret)
1696 return ret;
1697
John Harrison5fb9de12015-05-29 17:44:07 +01001698 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001699 if (ret)
1700 return ret;
1701
Chris Wilsonb5321f32016-08-02 22:50:18 +01001702 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1703 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1704 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1705 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1706 intel_ring_emit(ring, get_pd_offset(ppgtt));
1707 intel_ring_emit(ring, MI_NOOP);
1708 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001709
Ben Widawsky90252e52013-12-06 14:11:12 -08001710 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001711 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001712 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001713 if (ret)
1714 return ret;
1715 }
1716
Ben Widawsky48a10382013-12-06 14:11:11 -08001717 return 0;
1718}
1719
Ben Widawskyeeb94882013-12-06 14:11:10 -08001720static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001721 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001722{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001723 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001724 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001725
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001726 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1727 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001728 return 0;
1729}
1730
Daniel Vetter82460d92014-08-06 20:19:53 +02001731static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001732{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001733 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001734 struct intel_engine_cs *engine;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001735
Dave Gordonb4ac5af2016-03-24 11:20:38 +00001736 for_each_engine(engine, dev_priv) {
Michel Thierry2dba3232015-07-30 11:06:23 +01001737 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001738 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001739 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001740 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001741}
1742
Daniel Vetter82460d92014-08-06 20:19:53 +02001743static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001744{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001745 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001746 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001747 uint32_t ecochk, ecobits;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001748
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001749 ecobits = I915_READ(GAC_ECO_BITS);
1750 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1751
1752 ecochk = I915_READ(GAM_ECOCHK);
1753 if (IS_HASWELL(dev)) {
1754 ecochk |= ECOCHK_PPGTT_WB_HSW;
1755 } else {
1756 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1757 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1758 }
1759 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001760
Dave Gordonb4ac5af2016-03-24 11:20:38 +00001761 for_each_engine(engine, dev_priv) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001762 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001763 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001764 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001765 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001766}
1767
Daniel Vetter82460d92014-08-06 20:19:53 +02001768static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001769{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001770 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001771 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001772
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001773 ecobits = I915_READ(GAC_ECO_BITS);
1774 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1775 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001776
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001777 gab_ctl = I915_READ(GAB_CTL);
1778 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001779
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001780 ecochk = I915_READ(GAM_ECOCHK);
1781 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001782
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001783 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001784}
1785
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001786/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001787static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001788 uint64_t start,
1789 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001790 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001791{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001792 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001793 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001794 unsigned first_entry = start >> PAGE_SHIFT;
1795 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001796 unsigned act_pt = first_entry / GEN6_PTES;
1797 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001798 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001799
Mika Kuoppalac114f762015-06-25 18:35:13 +03001800 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1801 I915_CACHE_LLC, true, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001802
Daniel Vetter7bddb012012-02-09 17:15:47 +01001803 while (num_entries) {
1804 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001805 if (last_pte > GEN6_PTES)
1806 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001807
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001808 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001809
1810 for (i = first_pte; i < last_pte; i++)
1811 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001812
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001813 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001814
Daniel Vetter7bddb012012-02-09 17:15:47 +01001815 num_entries -= last_pte - first_pte;
1816 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001817 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001818 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001819}
1820
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001821static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001822 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001823 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301824 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001825{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001826 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001827 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001828 unsigned act_pt = first_entry / GEN6_PTES;
1829 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001830 gen6_pte_t *pt_vaddr = NULL;
1831 struct sgt_iter sgt_iter;
1832 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001833
Dave Gordon85d12252016-05-20 11:54:06 +01001834 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001835 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001836 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001837
Chris Wilsoncc797142013-12-31 15:50:30 +00001838 pt_vaddr[act_pte] =
Dave Gordon85d12252016-05-20 11:54:06 +01001839 vm->pte_encode(addr, cache_level, true, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301840
Michel Thierry07749ef2015-03-16 16:00:54 +00001841 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001842 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001843 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001844 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001845 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001846 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001847 }
Dave Gordon85d12252016-05-20 11:54:06 +01001848
Chris Wilsoncc797142013-12-31 15:50:30 +00001849 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001850 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001851}
1852
Ben Widawsky678d96f2015-03-16 16:00:56 +00001853static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001854 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001855{
Michel Thierry4933d512015-03-24 15:46:22 +00001856 DECLARE_BITMAP(new_page_tables, I915_PDES);
1857 struct drm_device *dev = vm->dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001858 struct drm_i915_private *dev_priv = to_i915(dev);
1859 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001860 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001861 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001862 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001863 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001864 int ret;
1865
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001866 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1867 return -ENODEV;
1868
1869 start = start_save = start_in;
1870 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001871
1872 bitmap_zero(new_page_tables, I915_PDES);
1873
1874 /* The allocation is done in two stages so that we can bail out with
1875 * minimal amount of pain. The first stage finds new page tables that
1876 * need allocation. The second stage marks use ptes within the page
1877 * tables.
1878 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001879 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001880 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001881 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1882 continue;
1883 }
1884
1885 /* We've already allocated a page table */
1886 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1887
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001888 pt = alloc_pt(dev);
Michel Thierry4933d512015-03-24 15:46:22 +00001889 if (IS_ERR(pt)) {
1890 ret = PTR_ERR(pt);
1891 goto unwind_out;
1892 }
1893
1894 gen6_initialize_pt(vm, pt);
1895
1896 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001897 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001898 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001899 }
1900
1901 start = start_save;
1902 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001903
Dave Gordon731f74c2016-06-24 19:37:46 +01001904 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001905 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1906
1907 bitmap_zero(tmp_bitmap, GEN6_PTES);
1908 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1909 gen6_pte_count(start, length));
1910
Mika Kuoppala966082c2015-06-25 18:35:19 +03001911 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001912 gen6_write_pde(&ppgtt->pd, pde, pt);
1913
Michel Thierry72744cb2015-03-24 15:46:23 +00001914 trace_i915_page_table_entry_map(vm, pde, pt,
1915 gen6_pte_index(start),
1916 gen6_pte_count(start, length),
1917 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001918 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001919 GEN6_PTES);
1920 }
1921
Michel Thierry4933d512015-03-24 15:46:22 +00001922 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1923
1924 /* Make sure write is complete before other code can use this page
1925 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001926 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001927
Ben Widawsky563222a2015-03-19 12:53:28 +00001928 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001929 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001930
1931unwind_out:
1932 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001933 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00001934
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001935 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001936 free_pt(vm->dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001937 }
1938
1939 mark_tlbs_dirty(ppgtt);
1940 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001941}
1942
Mika Kuoppala8776f022015-06-30 18:16:40 +03001943static int gen6_init_scratch(struct i915_address_space *vm)
1944{
1945 struct drm_device *dev = vm->dev;
1946
1947 vm->scratch_page = alloc_scratch_page(dev);
1948 if (IS_ERR(vm->scratch_page))
1949 return PTR_ERR(vm->scratch_page);
1950
1951 vm->scratch_pt = alloc_pt(dev);
1952 if (IS_ERR(vm->scratch_pt)) {
1953 free_scratch_page(dev, vm->scratch_page);
1954 return PTR_ERR(vm->scratch_pt);
1955 }
1956
1957 gen6_initialize_pt(vm, vm->scratch_pt);
1958
1959 return 0;
1960}
1961
1962static void gen6_free_scratch(struct i915_address_space *vm)
1963{
1964 struct drm_device *dev = vm->dev;
1965
1966 free_pt(dev, vm->scratch_pt);
1967 free_scratch_page(dev, vm->scratch_page);
1968}
1969
Daniel Vetter061dd492015-04-14 17:35:13 +02001970static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08001971{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001972 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01001973 struct i915_page_directory *pd = &ppgtt->pd;
1974 struct drm_device *dev = vm->dev;
Michel Thierry09942c62015-04-08 12:13:30 +01001975 struct i915_page_table *pt;
1976 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08001977
Daniel Vetter061dd492015-04-14 17:35:13 +02001978 drm_mm_remove_node(&ppgtt->node);
1979
Dave Gordon731f74c2016-06-24 19:37:46 +01001980 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001981 if (pt != vm->scratch_pt)
Dave Gordon731f74c2016-06-24 19:37:46 +01001982 free_pt(dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001983
Mika Kuoppala8776f022015-06-30 18:16:40 +03001984 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08001985}
1986
Ben Widawskyb1465202014-02-19 22:05:49 -08001987static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001988{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001989 struct i915_address_space *vm = &ppgtt->base;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001990 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001991 struct drm_i915_private *dev_priv = to_i915(dev);
1992 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001993 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08001994 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001995
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001996 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1997 * allocator works in address space sizes, so it's multiplied by page
1998 * size. We allocate at the top of the GTT to avoid fragmentation.
1999 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002000 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002001
Mika Kuoppala8776f022015-06-30 18:16:40 +03002002 ret = gen6_init_scratch(vm);
2003 if (ret)
2004 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002005
Ben Widawskye3cc1992013-12-06 14:11:08 -08002006alloc:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002007 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002008 &ppgtt->node, GEN6_PD_SIZE,
2009 GEN6_PD_ALIGN, 0,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002010 0, ggtt->base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07002011 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002012 if (ret == -ENOSPC && !retried) {
Chris Wilsone522ac22016-08-04 16:32:18 +01002013 ret = i915_gem_evict_something(&ggtt->base,
Ben Widawskye3cc1992013-12-06 14:11:08 -08002014 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02002015 I915_CACHE_NONE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002016 0, ggtt->base.total,
Chris Wilsond23db882014-05-23 08:48:08 +02002017 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002018 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002019 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002020
2021 retried = true;
2022 goto alloc;
2023 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002024
Ben Widawskyc8c26622015-01-22 17:01:25 +00002025 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002026 goto err_out;
2027
Ben Widawskyc8c26622015-01-22 17:01:25 +00002028
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002029 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002030 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002031
Ben Widawskyc8c26622015-01-22 17:01:25 +00002032 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002033
2034err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002035 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002036 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002037}
2038
Ben Widawskyb1465202014-02-19 22:05:49 -08002039static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2040{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002041 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002042}
2043
Michel Thierry4933d512015-03-24 15:46:22 +00002044static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2045 uint64_t start, uint64_t length)
2046{
Michel Thierryec565b32015-04-08 12:13:23 +01002047 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002048 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002049
Dave Gordon731f74c2016-06-24 19:37:46 +01002050 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002051 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002052}
2053
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002054static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002055{
2056 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 Widawskyb1465202014-02-19 22:05:49 -08002059 int ret;
2060
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002061 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Chris Wilson8eb95202016-07-04 08:48:31 +01002062 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev))
Ben Widawsky48a10382013-12-06 14:11:11 -08002063 ppgtt->switch_mm = gen6_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002064 else if (IS_HASWELL(dev))
Ben Widawsky90252e52013-12-06 14:11:12 -08002065 ppgtt->switch_mm = hsw_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002066 else if (IS_GEN7(dev))
Ben Widawsky48a10382013-12-06 14:11:11 -08002067 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002068 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002069 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002070
2071 ret = gen6_ppgtt_alloc(ppgtt);
2072 if (ret)
2073 return ret;
2074
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002075 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002076 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2077 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002078 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2079 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002080 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f6f2013-11-25 09:54:34 -08002081 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002082 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002083 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002084
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002085 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002086 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002087
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002088 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002089 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002090
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002091 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002092
Ben Widawsky678d96f2015-03-16 16:00:56 +00002093 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2094
Thierry Reding440fd522015-01-23 09:05:06 +01002095 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002096 ppgtt->node.size >> 20,
2097 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002098
Daniel Vetterfa76da32014-08-06 20:19:54 +02002099 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002100 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002101
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002102 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002103}
2104
Chris Wilson2bfa9962016-08-04 07:52:25 +01002105static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2106 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002107{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002108 ppgtt->base.dev = &dev_priv->drm;
Daniel Vetter3440d262013-01-24 13:49:56 -08002109
Chris Wilson2bfa9962016-08-04 07:52:25 +01002110 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002111 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002112 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002113 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002114}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002115
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002116static void i915_address_space_init(struct i915_address_space *vm,
2117 struct drm_i915_private *dev_priv)
2118{
2119 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002120 INIT_LIST_HEAD(&vm->active_list);
2121 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002122 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002123 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2124}
2125
Tim Gored5165eb2016-02-04 11:49:34 +00002126static void gtt_write_workarounds(struct drm_device *dev)
2127{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002128 struct drm_i915_private *dev_priv = to_i915(dev);
Tim Gored5165eb2016-02-04 11:49:34 +00002129
2130 /* This function is for gtt related workarounds. This function is
2131 * called on driver load and after a GPU reset, so you can place
2132 * workarounds here even if they get overwritten by GPU reset.
2133 */
2134 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
2135 if (IS_BROADWELL(dev))
2136 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
2137 else if (IS_CHERRYVIEW(dev))
2138 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
2139 else if (IS_SKYLAKE(dev))
2140 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
2141 else if (IS_BROXTON(dev))
2142 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2143}
2144
Chris Wilson2bfa9962016-08-04 07:52:25 +01002145static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2146 struct drm_i915_private *dev_priv,
2147 struct drm_i915_file_private *file_priv)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002148{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002149 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002150
Chris Wilson2bfa9962016-08-04 07:52:25 +01002151 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002152 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002153 kref_init(&ppgtt->ref);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002154 i915_address_space_init(&ppgtt->base, dev_priv);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002155 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002156 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002157
2158 return ret;
2159}
2160
Daniel Vetter82460d92014-08-06 20:19:53 +02002161int i915_ppgtt_init_hw(struct drm_device *dev)
2162{
Tim Gored5165eb2016-02-04 11:49:34 +00002163 gtt_write_workarounds(dev);
2164
Thomas Daniel671b50132014-08-20 16:24:50 +01002165 /* In the case of execlists, PPGTT is enabled by the context descriptor
2166 * and the PDPs are contained within the context itself. We don't
2167 * need to do anything here. */
2168 if (i915.enable_execlists)
2169 return 0;
2170
Daniel Vetter82460d92014-08-06 20:19:53 +02002171 if (!USES_PPGTT(dev))
2172 return 0;
2173
2174 if (IS_GEN6(dev))
2175 gen6_ppgtt_enable(dev);
2176 else if (IS_GEN7(dev))
2177 gen7_ppgtt_enable(dev);
2178 else if (INTEL_INFO(dev)->gen >= 8)
2179 gen8_ppgtt_enable(dev);
2180 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01002181 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02002182
John Harrison4ad2fd82015-06-18 13:11:20 +01002183 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002184}
John Harrison4ad2fd82015-06-18 13:11:20 +01002185
Daniel Vetter4d884702014-08-06 15:04:47 +02002186struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002187i915_ppgtt_create(struct drm_i915_private *dev_priv,
2188 struct drm_i915_file_private *fpriv)
Daniel Vetter4d884702014-08-06 15:04:47 +02002189{
2190 struct i915_hw_ppgtt *ppgtt;
2191 int ret;
2192
2193 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2194 if (!ppgtt)
2195 return ERR_PTR(-ENOMEM);
2196
Chris Wilson2bfa9962016-08-04 07:52:25 +01002197 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv);
Daniel Vetter4d884702014-08-06 15:04:47 +02002198 if (ret) {
2199 kfree(ppgtt);
2200 return ERR_PTR(ret);
2201 }
2202
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002203 trace_i915_ppgtt_create(&ppgtt->base);
2204
Daniel Vetter4d884702014-08-06 15:04:47 +02002205 return ppgtt;
2206}
2207
Daniel Vetteree960be2014-08-06 15:04:45 +02002208void i915_ppgtt_release(struct kref *kref)
2209{
2210 struct i915_hw_ppgtt *ppgtt =
2211 container_of(kref, struct i915_hw_ppgtt, ref);
2212
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002213 trace_i915_ppgtt_release(&ppgtt->base);
2214
Chris Wilson50e046b2016-08-04 07:52:46 +01002215 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002216 WARN_ON(!list_empty(&ppgtt->base.active_list));
2217 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002218 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002219
Daniel Vetter19dd1202014-08-06 15:04:55 +02002220 list_del(&ppgtt->base.global_link);
2221 drm_mm_takedown(&ppgtt->base.mm);
2222
Daniel Vetteree960be2014-08-06 15:04:45 +02002223 ppgtt->base.cleanup(&ppgtt->base);
2224 kfree(ppgtt);
2225}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002226
Ben Widawskya81cc002013-01-18 12:30:31 -08002227/* Certain Gen5 chipsets require require idling the GPU before
2228 * unmapping anything from the GTT when VT-d is enabled.
2229 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002230static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002231{
2232#ifdef CONFIG_INTEL_IOMMU
2233 /* Query intel_iommu to see if we need the workaround. Presumably that
2234 * was loaded first.
2235 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002236 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002237 return true;
2238#endif
2239 return false;
2240}
2241
Chris Wilsondc979972016-05-10 14:10:04 +01002242void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002243{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002244 struct intel_engine_cs *engine;
Ben Widawsky828c7902013-10-16 09:21:30 -07002245
Chris Wilsondc979972016-05-10 14:10:04 +01002246 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002247 return;
2248
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002249 for_each_engine(engine, dev_priv) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002250 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002251 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002252 if (fault_reg & RING_FAULT_VALID) {
2253 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002254 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002255 "\tAddress space: %s\n"
2256 "\tSource ID: %d\n"
2257 "\tType: %d\n",
2258 fault_reg & PAGE_MASK,
2259 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2260 RING_FAULT_SRCID(fault_reg),
2261 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002262 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002263 fault_reg & ~RING_FAULT_VALID);
2264 }
2265 }
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002266 POSTING_READ(RING_FAULT_REG(&dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002267}
2268
Chris Wilson91e56492014-09-25 10:13:12 +01002269static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2270{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002271 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002272 intel_gtt_chipset_flush();
2273 } else {
2274 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2275 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2276 }
2277}
2278
Ben Widawsky828c7902013-10-16 09:21:30 -07002279void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2280{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002281 struct drm_i915_private *dev_priv = to_i915(dev);
2282 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002283
2284 /* Don't bother messing with faults pre GEN6 as we have little
2285 * documentation supporting that it's a good idea.
2286 */
2287 if (INTEL_INFO(dev)->gen < 6)
2288 return;
2289
Chris Wilsondc979972016-05-10 14:10:04 +01002290 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002291
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002292 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total,
2293 true);
Chris Wilson91e56492014-09-25 10:13:12 +01002294
2295 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002296}
2297
Daniel Vetter74163902012-02-15 23:50:21 +01002298int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002299{
Chris Wilson9da3da62012-06-01 15:20:22 +01002300 if (!dma_map_sg(&obj->base.dev->pdev->dev,
2301 obj->pages->sgl, obj->pages->nents,
2302 PCI_DMA_BIDIRECTIONAL))
2303 return -ENOSPC;
2304
2305 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002306}
2307
Daniel Vetter2c642b02015-04-14 17:35:26 +02002308static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002309{
2310#ifdef writeq
2311 writeq(pte, addr);
2312#else
2313 iowrite32((u32)pte, addr);
2314 iowrite32(pte >> 32, addr + 4);
2315#endif
2316}
2317
Chris Wilsond6473f52016-06-10 14:22:59 +05302318static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2319 dma_addr_t addr,
2320 uint64_t offset,
2321 enum i915_cache_level level,
2322 u32 unused)
2323{
2324 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2325 gen8_pte_t __iomem *pte =
2326 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2327 (offset >> PAGE_SHIFT);
2328 int rpm_atomic_seq;
2329
2330 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2331
2332 gen8_set_pte(pte, gen8_pte_encode(addr, level, true));
2333
2334 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2335 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2336
2337 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2338}
2339
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002340static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2341 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002342 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302343 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002344{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002345 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002346 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002347 struct sgt_iter sgt_iter;
2348 gen8_pte_t __iomem *gtt_entries;
2349 gen8_pte_t gtt_entry;
2350 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002351 int rpm_atomic_seq;
Dave Gordon85d12252016-05-20 11:54:06 +01002352 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002353
2354 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002355
Dave Gordon85d12252016-05-20 11:54:06 +01002356 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2357
2358 for_each_sgt_dma(addr, sgt_iter, st) {
2359 gtt_entry = gen8_pte_encode(addr, level, true);
2360 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002361 }
2362
2363 /*
2364 * XXX: This serves as a posting read to make sure that the PTE has
2365 * actually been updated. There is some concern that even though
2366 * registers and PTEs are within the same BAR that they are potentially
2367 * of NUMA access patterns. Therefore, even with the way we assume
2368 * hardware should work, we must keep this posting read for paranoia.
2369 */
2370 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002371 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002372
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002373 /* This next bit makes the above posting read even more important. We
2374 * want to flush the TLBs only after we're certain all the PTE updates
2375 * have finished.
2376 */
2377 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2378 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Imre Deakbe694592015-12-15 20:10:38 +02002379
2380 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002381}
2382
Chris Wilsonc1403302015-11-18 15:19:39 +00002383struct insert_entries {
2384 struct i915_address_space *vm;
2385 struct sg_table *st;
2386 uint64_t start;
2387 enum i915_cache_level level;
2388 u32 flags;
2389};
2390
2391static int gen8_ggtt_insert_entries__cb(void *_arg)
2392{
2393 struct insert_entries *arg = _arg;
2394 gen8_ggtt_insert_entries(arg->vm, arg->st,
2395 arg->start, arg->level, arg->flags);
2396 return 0;
2397}
2398
2399static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2400 struct sg_table *st,
2401 uint64_t start,
2402 enum i915_cache_level level,
2403 u32 flags)
2404{
2405 struct insert_entries arg = { vm, st, start, level, flags };
2406 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2407}
2408
Chris Wilsond6473f52016-06-10 14:22:59 +05302409static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2410 dma_addr_t addr,
2411 uint64_t offset,
2412 enum i915_cache_level level,
2413 u32 flags)
2414{
2415 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2416 gen6_pte_t __iomem *pte =
2417 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2418 (offset >> PAGE_SHIFT);
2419 int rpm_atomic_seq;
2420
2421 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2422
2423 iowrite32(vm->pte_encode(addr, level, true, flags), pte);
2424
2425 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2426 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2427
2428 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2429}
2430
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002431/*
2432 * Binds an object into the global gtt with the specified cache level. The object
2433 * will be accessible to the GPU via commands whose operands reference offsets
2434 * within the global GTT as well as accessible by the GPU through the GMADR
2435 * mapped BAR (dev_priv->mm.gtt->gtt).
2436 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002437static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002438 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002439 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302440 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002441{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002442 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002443 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002444 struct sgt_iter sgt_iter;
2445 gen6_pte_t __iomem *gtt_entries;
2446 gen6_pte_t gtt_entry;
2447 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002448 int rpm_atomic_seq;
Dave Gordon85d12252016-05-20 11:54:06 +01002449 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002450
2451 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002452
Dave Gordon85d12252016-05-20 11:54:06 +01002453 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2454
2455 for_each_sgt_dma(addr, sgt_iter, st) {
2456 gtt_entry = vm->pte_encode(addr, level, true, flags);
2457 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002458 }
2459
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002460 /* XXX: This serves as a posting read to make sure that the PTE has
2461 * actually been updated. There is some concern that even though
2462 * registers and PTEs are within the same BAR that they are potentially
2463 * of NUMA access patterns. Therefore, even with the way we assume
2464 * hardware should work, we must keep this posting read for paranoia.
2465 */
Dave Gordon85d12252016-05-20 11:54:06 +01002466 if (i != 0)
2467 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002468
2469 /* This next bit makes the above posting read even more important. We
2470 * want to flush the TLBs only after we're certain all the PTE updates
2471 * have finished.
2472 */
2473 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2474 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Imre Deakbe694592015-12-15 20:10:38 +02002475
2476 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002477}
2478
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002479static void nop_clear_range(struct i915_address_space *vm,
2480 uint64_t start,
2481 uint64_t length,
2482 bool use_scratch)
2483{
2484}
2485
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002486static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002487 uint64_t start,
2488 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002489 bool use_scratch)
2490{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002491 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002492 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002493 unsigned first_entry = start >> PAGE_SHIFT;
2494 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002495 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002496 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2497 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002498 int i;
Imre Deakbe694592015-12-15 20:10:38 +02002499 int rpm_atomic_seq;
2500
2501 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002502
2503 if (WARN(num_entries > max_entries,
2504 "First entry = %d; Num entries = %d (max=%d)\n",
2505 first_entry, num_entries, max_entries))
2506 num_entries = max_entries;
2507
Mika Kuoppalac114f762015-06-25 18:35:13 +03002508 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002509 I915_CACHE_LLC,
2510 use_scratch);
2511 for (i = 0; i < num_entries; i++)
2512 gen8_set_pte(&gtt_base[i], scratch_pte);
2513 readl(gtt_base);
Imre Deakbe694592015-12-15 20:10:38 +02002514
2515 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002516}
2517
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002518static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002519 uint64_t start,
2520 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07002521 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002522{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002523 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002524 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002525 unsigned first_entry = start >> PAGE_SHIFT;
2526 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002527 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002528 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2529 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002530 int i;
Imre Deakbe694592015-12-15 20:10:38 +02002531 int rpm_atomic_seq;
2532
2533 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002534
2535 if (WARN(num_entries > max_entries,
2536 "First entry = %d; Num entries = %d (max=%d)\n",
2537 first_entry, num_entries, max_entries))
2538 num_entries = max_entries;
2539
Mika Kuoppalac114f762015-06-25 18:35:13 +03002540 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
2541 I915_CACHE_LLC, use_scratch, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002542
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002543 for (i = 0; i < num_entries; i++)
2544 iowrite32(scratch_pte, &gtt_base[i]);
2545 readl(gtt_base);
Imre Deakbe694592015-12-15 20:10:38 +02002546
2547 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002548}
2549
Chris Wilsond6473f52016-06-10 14:22:59 +05302550static void i915_ggtt_insert_page(struct i915_address_space *vm,
2551 dma_addr_t addr,
2552 uint64_t offset,
2553 enum i915_cache_level cache_level,
2554 u32 unused)
2555{
2556 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2557 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2558 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2559 int rpm_atomic_seq;
2560
2561 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2562
2563 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
2564
2565 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2566}
2567
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002568static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2569 struct sg_table *pages,
2570 uint64_t start,
2571 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002572{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002573 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002574 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2575 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Imre Deakbe694592015-12-15 20:10:38 +02002576 int rpm_atomic_seq;
2577
2578 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002579
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002580 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002581
Imre Deakbe694592015-12-15 20:10:38 +02002582 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2583
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002584}
2585
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002586static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002587 uint64_t start,
2588 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07002589 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002590{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002591 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Ben Widawsky782f1492014-02-20 11:50:33 -08002592 unsigned first_entry = start >> PAGE_SHIFT;
2593 unsigned num_entries = length >> PAGE_SHIFT;
Imre Deakbe694592015-12-15 20:10:38 +02002594 int rpm_atomic_seq;
2595
2596 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2597
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002598 intel_gtt_clear_range(first_entry, num_entries);
Imre Deakbe694592015-12-15 20:10:38 +02002599
2600 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002601}
2602
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002603static int ggtt_bind_vma(struct i915_vma *vma,
2604 enum i915_cache_level cache_level,
2605 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002606{
Daniel Vetter0a878712015-10-15 14:23:01 +02002607 struct drm_i915_gem_object *obj = vma->obj;
2608 u32 pte_flags = 0;
2609 int ret;
2610
2611 ret = i915_get_ggtt_vma_pages(vma);
2612 if (ret)
2613 return ret;
2614
2615 /* Currently applicable only to VLV */
2616 if (obj->gt_ro)
2617 pte_flags |= PTE_READ_ONLY;
2618
Chris Wilson247177d2016-08-15 10:48:47 +01002619 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002620 cache_level, pte_flags);
2621
2622 /*
2623 * Without aliasing PPGTT there's no difference between
2624 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2625 * upgrade to both bound if we bind either to avoid double-binding.
2626 */
Chris Wilson3272db52016-08-04 16:32:32 +01002627 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002628
2629 return 0;
2630}
2631
2632static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2633 enum i915_cache_level cache_level,
2634 u32 flags)
2635{
Chris Wilson321d1782015-11-20 10:27:18 +00002636 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002637 int ret;
2638
2639 ret = i915_get_ggtt_vma_pages(vma);
2640 if (ret)
2641 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002642
Akash Goel24f3a8c2014-06-17 10:59:42 +05302643 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002644 pte_flags = 0;
2645 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002646 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302647
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002648
Chris Wilson3272db52016-08-04 16:32:32 +01002649 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson321d1782015-11-20 10:27:18 +00002650 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002651 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002652 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002653 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002654
Chris Wilson3272db52016-08-04 16:32:32 +01002655 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson321d1782015-11-20 10:27:18 +00002656 struct i915_hw_ppgtt *appgtt =
2657 to_i915(vma->vm->dev)->mm.aliasing_ppgtt;
2658 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002659 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002660 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002661 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002662
2663 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002664}
2665
2666static void ggtt_unbind_vma(struct i915_vma *vma)
2667{
Chris Wilsonde180032016-08-04 16:32:29 +01002668 struct i915_hw_ppgtt *appgtt = to_i915(vma->vm->dev)->mm.aliasing_ppgtt;
2669 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002670
Chris Wilson3272db52016-08-04 16:32:32 +01002671 if (vma->flags & I915_VMA_GLOBAL_BIND)
Ben Widawsky782f1492014-02-20 11:50:33 -08002672 vma->vm->clear_range(vma->vm,
Chris Wilsonde180032016-08-04 16:32:29 +01002673 vma->node.start, size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08002674 true);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002675
Chris Wilson3272db52016-08-04 16:32:32 +01002676 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002677 appgtt->base.clear_range(&appgtt->base,
Chris Wilsonde180032016-08-04 16:32:29 +01002678 vma->node.start, size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08002679 true);
Daniel Vetter74163902012-02-15 23:50:21 +01002680}
2681
2682void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
2683{
Ben Widawsky5c042282011-10-17 15:51:55 -07002684 struct drm_device *dev = obj->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002685 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson307dc252016-08-05 10:14:12 +01002686 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002687
Chris Wilson307dc252016-08-05 10:14:12 +01002688 if (unlikely(ggtt->do_idle_maps)) {
2689 if (i915_gem_wait_for_idle(dev_priv, false)) {
2690 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2691 /* Wait a bit, in hopes it avoids the hang */
2692 udelay(10);
2693 }
2694 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002695
Imre Deak5ec5b512015-07-08 19:18:59 +03002696 dma_unmap_sg(&dev->pdev->dev, obj->pages->sgl, obj->pages->nents,
2697 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002698}
Daniel Vetter644ec022012-03-26 09:45:40 +02002699
Chris Wilson42d6ab42012-07-26 11:49:32 +01002700static void i915_gtt_color_adjust(struct drm_mm_node *node,
2701 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002702 u64 *start,
2703 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002704{
2705 if (node->color != color)
2706 *start += 4096;
2707
Chris Wilson2a1d7752016-07-26 12:01:51 +01002708 node = list_first_entry_or_null(&node->node_list,
2709 struct drm_mm_node,
2710 node_list);
2711 if (node && node->allocated && node->color != color)
2712 *end -= 4096;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002713}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002714
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002715int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002716{
Ben Widawskye78891c2013-01-25 16:41:04 -08002717 /* Let GEM Manage all of the aperture.
2718 *
2719 * However, leave one page at the end still bound to the scratch page.
2720 * There are a number of places where the hardware apparently prefetches
2721 * past the end of the object, and we've seen multiple hangs with the
2722 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2723 * aperture. One page should be enough to keep any prefetching inside
2724 * of the aperture.
2725 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002726 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002727 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002728 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002729 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002730
Zhi Wangb02d22a2016-06-16 08:06:59 -04002731 ret = intel_vgt_balloon(dev_priv);
2732 if (ret)
2733 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002734
Chris Wilsoned2f3452012-11-15 11:32:19 +00002735 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002736 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002737 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2738 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002739 ggtt->base.clear_range(&ggtt->base, hole_start,
Ben Widawsky782f1492014-02-20 11:50:33 -08002740 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002741 }
2742
2743 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002744 ggtt->base.clear_range(&ggtt->base,
2745 ggtt->base.total - PAGE_SIZE, PAGE_SIZE,
2746 true);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002747
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002748 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002749 struct i915_hw_ppgtt *ppgtt;
2750
2751 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2752 if (!ppgtt)
2753 return -ENOMEM;
2754
Chris Wilson2bfa9962016-08-04 07:52:25 +01002755 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00002756 if (ret) {
2757 kfree(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002758 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002759 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002760
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002761 if (ppgtt->base.allocate_va_range)
2762 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2763 ppgtt->base.total);
2764 if (ret) {
2765 ppgtt->base.cleanup(&ppgtt->base);
2766 kfree(ppgtt);
2767 return ret;
2768 }
2769
2770 ppgtt->base.clear_range(&ppgtt->base,
2771 ppgtt->base.start,
2772 ppgtt->base.total,
2773 true);
2774
Daniel Vetterfa76da32014-08-06 20:19:54 +02002775 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002776 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2777 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002778 }
2779
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002780 return 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002781}
2782
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002783/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002784 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002785 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002786 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002787void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002788{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002789 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002790
Daniel Vetter70e32542014-08-06 15:04:57 +02002791 if (dev_priv->mm.aliasing_ppgtt) {
2792 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
Daniel Vetter70e32542014-08-06 15:04:57 +02002793 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002794 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002795 }
2796
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002797 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002798
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002799 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002800 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002801
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002802 drm_mm_takedown(&ggtt->base.mm);
2803 list_del(&ggtt->base.global_link);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002804 }
2805
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002806 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002807
2808 arch_phys_wc_del(ggtt->mtrr);
Chris Wilsonf7bbe782016-08-19 16:54:27 +01002809 io_mapping_fini(&ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002810}
Daniel Vetter70e32542014-08-06 15:04:57 +02002811
Daniel Vetter2c642b02015-04-14 17:35:26 +02002812static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002813{
2814 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2815 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2816 return snb_gmch_ctl << 20;
2817}
2818
Daniel Vetter2c642b02015-04-14 17:35:26 +02002819static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002820{
2821 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2822 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2823 if (bdw_gmch_ctl)
2824 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002825
2826#ifdef CONFIG_X86_32
2827 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2828 if (bdw_gmch_ctl > 4)
2829 bdw_gmch_ctl = 4;
2830#endif
2831
Ben Widawsky9459d252013-11-03 16:53:55 -08002832 return bdw_gmch_ctl << 20;
2833}
2834
Daniel Vetter2c642b02015-04-14 17:35:26 +02002835static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002836{
2837 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2838 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2839
2840 if (gmch_ctrl)
2841 return 1 << (20 + gmch_ctrl);
2842
2843 return 0;
2844}
2845
Daniel Vetter2c642b02015-04-14 17:35:26 +02002846static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002847{
2848 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2849 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2850 return snb_gmch_ctl << 25; /* 32 MB units */
2851}
2852
Daniel Vetter2c642b02015-04-14 17:35:26 +02002853static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002854{
2855 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2856 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2857 return bdw_gmch_ctl << 25; /* 32 MB units */
2858}
2859
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002860static size_t chv_get_stolen_size(u16 gmch_ctrl)
2861{
2862 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2863 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2864
2865 /*
2866 * 0x0 to 0x10: 32MB increments starting at 0MB
2867 * 0x11 to 0x16: 4MB increments starting at 8MB
2868 * 0x17 to 0x1d: 4MB increments start at 36MB
2869 */
2870 if (gmch_ctrl < 0x11)
2871 return gmch_ctrl << 25;
2872 else if (gmch_ctrl < 0x17)
2873 return (gmch_ctrl - 0x11 + 2) << 22;
2874 else
2875 return (gmch_ctrl - 0x17 + 9) << 22;
2876}
2877
Damien Lespiau66375012014-01-09 18:02:46 +00002878static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2879{
2880 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2881 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2882
2883 if (gen9_gmch_ctl < 0xf0)
2884 return gen9_gmch_ctl << 25; /* 32 MB units */
2885 else
2886 /* 4MB increments starting at 0xf0 for 4MB */
2887 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2888}
2889
Chris Wilson34c998b2016-08-04 07:52:24 +01002890static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002891{
Chris Wilson34c998b2016-08-04 07:52:24 +01002892 struct pci_dev *pdev = ggtt->base.dev->pdev;
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002893 struct i915_page_scratch *scratch_page;
Chris Wilson34c998b2016-08-04 07:52:24 +01002894 phys_addr_t phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08002895
2896 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002897 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002898
Imre Deak2a073f892015-03-27 13:07:33 +02002899 /*
2900 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2901 * dropped. For WC mappings in general we have 64 byte burst writes
2902 * when the WC buffer is flushed, so we can't use it, but have to
2903 * resort to an uncached mapping. The WC issue is easily caught by the
2904 * readback check when writing GTT PTE entries.
2905 */
Chris Wilson34c998b2016-08-04 07:52:24 +01002906 if (IS_BROXTON(ggtt->base.dev))
2907 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002908 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002909 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002910 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002911 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002912 return -ENOMEM;
2913 }
2914
Chris Wilson34c998b2016-08-04 07:52:24 +01002915 scratch_page = alloc_scratch_page(ggtt->base.dev);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002916 if (IS_ERR(scratch_page)) {
Ben Widawsky63340132013-11-04 19:32:22 -08002917 DRM_ERROR("Scratch setup failed\n");
2918 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002919 iounmap(ggtt->gsm);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002920 return PTR_ERR(scratch_page);
Ben Widawsky63340132013-11-04 19:32:22 -08002921 }
2922
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002923 ggtt->base.scratch_page = scratch_page;
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002924
2925 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002926}
2927
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002928/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2929 * bits. When using advanced contexts each context stores its own PAT, but
2930 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002931static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002932{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002933 uint64_t pat;
2934
2935 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2936 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2937 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2938 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2939 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2940 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2941 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2942 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2943
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002944 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002945 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2946 * so RTL will always use the value corresponding to
2947 * pat_sel = 000".
2948 * So let's disable cache for GGTT to avoid screen corruptions.
2949 * MOCS still can be used though.
2950 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2951 * before this patch, i.e. the same uncached + snooping access
2952 * like on gen6/7 seems to be in effect.
2953 * - So this just fixes blitter/render access. Again it looks
2954 * like it's not just uncached access, but uncached + snooping.
2955 * So we can still hold onto all our assumptions wrt cpu
2956 * clflushing on LLC machines.
2957 */
2958 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2959
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002960 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2961 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002962 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2963 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002964}
2965
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002966static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2967{
2968 uint64_t pat;
2969
2970 /*
2971 * Map WB on BDW to snooped on CHV.
2972 *
2973 * Only the snoop bit has meaning for CHV, the rest is
2974 * ignored.
2975 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002976 * The hardware will never snoop for certain types of accesses:
2977 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2978 * - PPGTT page tables
2979 * - some other special cycles
2980 *
2981 * As with BDW, we also need to consider the following for GT accesses:
2982 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2983 * so RTL will always use the value corresponding to
2984 * pat_sel = 000".
2985 * Which means we must set the snoop bit in PAT entry 0
2986 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002987 */
2988 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2989 GEN8_PPAT(1, 0) |
2990 GEN8_PPAT(2, 0) |
2991 GEN8_PPAT(3, 0) |
2992 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2993 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2994 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2995 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2996
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002997 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2998 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002999}
3000
Chris Wilson34c998b2016-08-04 07:52:24 +01003001static void gen6_gmch_remove(struct i915_address_space *vm)
3002{
3003 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3004
3005 iounmap(ggtt->gsm);
3006 free_scratch_page(vm->dev, vm->scratch_page);
3007}
3008
Joonas Lahtinend507d732016-03-18 10:42:58 +02003009static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003010{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003011 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3012 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003013 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003014 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003015
3016 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003017 ggtt->mappable_base = pci_resource_start(pdev, 2);
3018 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003019
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003020 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3021 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003022
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003023 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003024
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003025 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003026 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003027 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003028 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003029 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003030 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003031 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003032 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003033 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003034 }
Ben Widawsky63340132013-11-04 19:32:22 -08003035
Chris Wilson34c998b2016-08-04 07:52:24 +01003036 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003037
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003038 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003039 chv_setup_private_ppat(dev_priv);
3040 else
3041 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003042
Chris Wilson34c998b2016-08-04 07:52:24 +01003043 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003044 ggtt->base.bind_vma = ggtt_bind_vma;
3045 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303046 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003047 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003048 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003049 ggtt->base.clear_range = gen8_ggtt_clear_range;
3050
3051 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3052 if (IS_CHERRYVIEW(dev_priv))
3053 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3054
Chris Wilson34c998b2016-08-04 07:52:24 +01003055 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003056}
3057
Joonas Lahtinend507d732016-03-18 10:42:58 +02003058static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003059{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003060 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3061 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003062 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003063 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003064
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003065 ggtt->mappable_base = pci_resource_start(pdev, 2);
3066 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003067
Ben Widawskybaa09f52013-01-24 13:49:57 -08003068 /* 64/512MB is the current min/max we actually know of, but this is just
3069 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003070 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003071 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003072 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003073 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003074 }
3075
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003076 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3077 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3078 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003079
Joonas Lahtinend507d732016-03-18 10:42:58 +02003080 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003081
Chris Wilson34c998b2016-08-04 07:52:24 +01003082 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3083 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003084
Joonas Lahtinend507d732016-03-18 10:42:58 +02003085 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303086 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003087 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3088 ggtt->base.bind_vma = ggtt_bind_vma;
3089 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003090 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003091
Chris Wilson34c998b2016-08-04 07:52:24 +01003092 if (HAS_EDRAM(dev_priv))
3093 ggtt->base.pte_encode = iris_pte_encode;
3094 else if (IS_HASWELL(dev_priv))
3095 ggtt->base.pte_encode = hsw_pte_encode;
3096 else if (IS_VALLEYVIEW(dev_priv))
3097 ggtt->base.pte_encode = byt_pte_encode;
3098 else if (INTEL_GEN(dev_priv) >= 7)
3099 ggtt->base.pte_encode = ivb_pte_encode;
3100 else
3101 ggtt->base.pte_encode = snb_pte_encode;
3102
3103 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003104}
3105
Chris Wilson34c998b2016-08-04 07:52:24 +01003106static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003107{
Chris Wilson34c998b2016-08-04 07:52:24 +01003108 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003109}
3110
Joonas Lahtinend507d732016-03-18 10:42:58 +02003111static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003112{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003113 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003114 int ret;
3115
Chris Wilson91c8a322016-07-05 10:40:23 +01003116 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003117 if (!ret) {
3118 DRM_ERROR("failed to set up gmch\n");
3119 return -EIO;
3120 }
3121
Joonas Lahtinend507d732016-03-18 10:42:58 +02003122 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3123 &ggtt->mappable_base, &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003124
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003125 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303126 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003127 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3128 ggtt->base.clear_range = i915_ggtt_clear_range;
3129 ggtt->base.bind_vma = ggtt_bind_vma;
3130 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003131 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003132
Joonas Lahtinend507d732016-03-18 10:42:58 +02003133 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003134 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3135
Ben Widawskybaa09f52013-01-24 13:49:57 -08003136 return 0;
3137}
3138
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003139/**
Chris Wilson0088e522016-08-04 07:52:21 +01003140 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003141 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003142 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003143int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003144{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003145 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003146 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003147
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003148 ggtt->base.dev = &dev_priv->drm;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003149
Chris Wilson34c998b2016-08-04 07:52:24 +01003150 if (INTEL_GEN(dev_priv) <= 5)
3151 ret = i915_gmch_probe(ggtt);
3152 else if (INTEL_GEN(dev_priv) < 8)
3153 ret = gen6_gmch_probe(ggtt);
3154 else
3155 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003156 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003157 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003158
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003159 if ((ggtt->base.total - 1) >> 32) {
3160 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003161 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003162 ggtt->base.total >> 20);
3163 ggtt->base.total = 1ULL << 32;
3164 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3165 }
3166
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003167 if (ggtt->mappable_end > ggtt->base.total) {
3168 DRM_ERROR("mappable aperture extends past end of GGTT,"
3169 " aperture=%llx, total=%llx\n",
3170 ggtt->mappable_end, ggtt->base.total);
3171 ggtt->mappable_end = ggtt->base.total;
3172 }
3173
Ben Widawskybaa09f52013-01-24 13:49:57 -08003174 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003175 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003176 ggtt->base.total >> 20);
3177 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3178 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003179#ifdef CONFIG_INTEL_IOMMU
3180 if (intel_iommu_gfx_mapped)
3181 DRM_INFO("VT-d active for gfx access\n");
3182#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003183
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003184 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003185}
3186
3187/**
3188 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003189 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003190 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003191int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003192{
Chris Wilson0088e522016-08-04 07:52:21 +01003193 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3194 int ret;
3195
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003196 INIT_LIST_HEAD(&dev_priv->vm_list);
3197
3198 /* Subtract the guard page before address space initialization to
3199 * shrink the range used by drm_mm.
3200 */
3201 ggtt->base.total -= PAGE_SIZE;
3202 i915_address_space_init(&ggtt->base, dev_priv);
3203 ggtt->base.total += PAGE_SIZE;
3204 if (!HAS_LLC(dev_priv))
3205 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
3206
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003207 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3208 dev_priv->ggtt.mappable_base,
3209 dev_priv->ggtt.mappable_end)) {
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003210 ret = -EIO;
3211 goto out_gtt_cleanup;
3212 }
3213
3214 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3215
Chris Wilson0088e522016-08-04 07:52:21 +01003216 /*
3217 * Initialise stolen early so that we may reserve preallocated
3218 * objects for the BIOS to KMS transition.
3219 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003220 ret = i915_gem_init_stolen(&dev_priv->drm);
Chris Wilson0088e522016-08-04 07:52:21 +01003221 if (ret)
3222 goto out_gtt_cleanup;
3223
3224 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003225
3226out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003227 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003228 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003229}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003230
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003231int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003232{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003233 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003234 return -EIO;
3235
3236 return 0;
3237}
3238
Daniel Vetterfa423312015-04-14 17:35:23 +02003239void i915_gem_restore_gtt_mappings(struct drm_device *dev)
3240{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003241 struct drm_i915_private *dev_priv = to_i915(dev);
3242 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003243 struct drm_i915_gem_object *obj;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003244 struct i915_vma *vma;
Daniel Vetterfa423312015-04-14 17:35:23 +02003245
Chris Wilsondc979972016-05-10 14:10:04 +01003246 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003247
3248 /* First fill our portion of the GTT with scratch pages */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003249 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total,
3250 true);
Daniel Vetterfa423312015-04-14 17:35:23 +02003251
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003252 /* Cache flush objects bound into GGTT and rebind them. */
Daniel Vetterfa423312015-04-14 17:35:23 +02003253 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003254 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003255 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003256 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003257
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003258 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3259 PIN_UPDATE));
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003260 }
3261
Chris Wilson975f7ff2016-05-14 07:26:34 +01003262 if (obj->pin_display)
3263 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003264 }
3265
Daniel Vetterfa423312015-04-14 17:35:23 +02003266 if (INTEL_INFO(dev)->gen >= 8) {
3267 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
3268 chv_setup_private_ppat(dev_priv);
3269 else
3270 bdw_setup_private_ppat(dev_priv);
3271
3272 return;
3273 }
3274
3275 if (USES_PPGTT(dev)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003276 struct i915_address_space *vm;
3277
Daniel Vetterfa423312015-04-14 17:35:23 +02003278 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3279 /* TODO: Perhaps it shouldn't be gen6 specific */
3280
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003281 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003282
Chris Wilson2bfa9962016-08-04 07:52:25 +01003283 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003284 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003285 else
3286 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003287
3288 gen6_write_page_range(dev_priv, &ppgtt->pd,
3289 0, ppgtt->base.total);
3290 }
3291 }
3292
3293 i915_ggtt_flush(dev_priv);
3294}
3295
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003296static void
3297i915_vma_retire(struct i915_gem_active *active,
3298 struct drm_i915_gem_request *rq)
3299{
3300 const unsigned int idx = rq->engine->id;
3301 struct i915_vma *vma =
3302 container_of(active, struct i915_vma, last_read[idx]);
3303
3304 GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx));
3305
3306 i915_vma_clear_active(vma, idx);
3307 if (i915_vma_is_active(vma))
3308 return;
3309
3310 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Chris Wilson3272db52016-08-04 16:32:32 +01003311 if (unlikely(i915_vma_is_closed(vma) && !i915_vma_is_pinned(vma)))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003312 WARN_ON(i915_vma_unbind(vma));
3313}
3314
3315void i915_vma_destroy(struct i915_vma *vma)
3316{
3317 GEM_BUG_ON(vma->node.allocated);
3318 GEM_BUG_ON(i915_vma_is_active(vma));
Chris Wilson3272db52016-08-04 16:32:32 +01003319 GEM_BUG_ON(!i915_vma_is_closed(vma));
Chris Wilson49ef5292016-08-18 17:17:00 +01003320 GEM_BUG_ON(vma->fence);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003321
3322 list_del(&vma->vm_link);
Chris Wilson3272db52016-08-04 16:32:32 +01003323 if (!i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003324 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
3325
3326 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
3327}
3328
3329void i915_vma_close(struct i915_vma *vma)
3330{
Chris Wilson3272db52016-08-04 16:32:32 +01003331 GEM_BUG_ON(i915_vma_is_closed(vma));
3332 vma->flags |= I915_VMA_CLOSED;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003333
3334 list_del_init(&vma->obj_link);
Chris Wilson20dfbde2016-08-04 16:32:30 +01003335 if (!i915_vma_is_active(vma) && !i915_vma_is_pinned(vma))
Chris Wilsondf0e9a22016-08-04 07:52:47 +01003336 WARN_ON(i915_vma_unbind(vma));
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003337}
3338
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003339static struct i915_vma *
Chris Wilson058d88c2016-08-15 10:49:06 +01003340__i915_vma_create(struct drm_i915_gem_object *obj,
3341 struct i915_address_space *vm,
3342 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003343{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003344 struct i915_vma *vma;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003345 int i;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003346
Chris Wilson50e046b2016-08-04 07:52:46 +01003347 GEM_BUG_ON(vm->closed);
3348
Chris Wilsone20d2ab2015-04-07 16:20:58 +01003349 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003350 if (vma == NULL)
3351 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003352
Ben Widawsky6f65e292013-12-06 14:10:56 -08003353 INIT_LIST_HEAD(&vma->exec_list);
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003354 for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
3355 init_request_active(&vma->last_read[i], i915_vma_retire);
Chris Wilson49ef5292016-08-18 17:17:00 +01003356 init_request_active(&vma->last_fence, NULL);
Chris Wilson50e046b2016-08-04 07:52:46 +01003357 list_add(&vma->vm_link, &vm->unbound_list);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003358 vma->vm = vm;
3359 vma->obj = obj;
Chris Wilsonde180032016-08-04 16:32:29 +01003360 vma->size = obj->base.size;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003361
Chris Wilson058d88c2016-08-15 10:49:06 +01003362 if (view) {
Chris Wilsonde180032016-08-04 16:32:29 +01003363 vma->ggtt_view = *view;
3364 if (view->type == I915_GGTT_VIEW_PARTIAL) {
3365 vma->size = view->params.partial.size;
3366 vma->size <<= PAGE_SHIFT;
3367 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
3368 vma->size =
3369 intel_rotation_info_size(&view->params.rotated);
3370 vma->size <<= PAGE_SHIFT;
3371 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003372 }
3373
3374 if (i915_is_ggtt(vm)) {
3375 vma->flags |= I915_VMA_GGTT;
Chris Wilsonde180032016-08-04 16:32:29 +01003376 } else {
Chris Wilson596c5922016-02-26 11:03:20 +00003377 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Chris Wilsonde180032016-08-04 16:32:29 +01003378 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08003379
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003380 list_add_tail(&vma->obj_link, &obj->vma_list);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003381 return vma;
3382}
3383
Chris Wilson058d88c2016-08-15 10:49:06 +01003384static inline bool vma_matches(struct i915_vma *vma,
3385 struct i915_address_space *vm,
3386 const struct i915_ggtt_view *view)
3387{
3388 if (vma->vm != vm)
3389 return false;
3390
3391 if (!i915_vma_is_ggtt(vma))
3392 return true;
3393
3394 if (!view)
3395 return vma->ggtt_view.type == 0;
3396
3397 if (vma->ggtt_view.type != view->type)
3398 return false;
3399
3400 return memcmp(&vma->ggtt_view.params,
3401 &view->params,
3402 sizeof(view->params)) == 0;
3403}
3404
Ben Widawsky6f65e292013-12-06 14:10:56 -08003405struct i915_vma *
Chris Wilson81a8aa42016-08-15 10:48:48 +01003406i915_vma_create(struct drm_i915_gem_object *obj,
3407 struct i915_address_space *vm,
3408 const struct i915_ggtt_view *view)
3409{
3410 GEM_BUG_ON(view && !i915_is_ggtt(vm));
Chris Wilson058d88c2016-08-15 10:49:06 +01003411 GEM_BUG_ON(i915_gem_obj_to_vma(obj, vm, view));
Chris Wilson81a8aa42016-08-15 10:48:48 +01003412
Chris Wilson058d88c2016-08-15 10:49:06 +01003413 return __i915_vma_create(obj, vm, view);
3414}
3415
3416struct i915_vma *
3417i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3418 struct i915_address_space *vm,
3419 const struct i915_ggtt_view *view)
3420{
3421 struct i915_vma *vma;
3422
3423 list_for_each_entry_reverse(vma, &obj->vma_list, obj_link)
3424 if (vma_matches(vma, vm, view))
3425 return vma;
3426
3427 return NULL;
Chris Wilson81a8aa42016-08-15 10:48:48 +01003428}
3429
3430struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003431i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
Chris Wilson058d88c2016-08-15 10:49:06 +01003432 struct i915_address_space *vm,
3433 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003434{
3435 struct i915_vma *vma;
3436
Chris Wilson058d88c2016-08-15 10:49:06 +01003437 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3438
3439 vma = i915_gem_obj_to_vma(obj, vm, view);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003440 if (!vma)
Chris Wilson058d88c2016-08-15 10:49:06 +01003441 vma = __i915_vma_create(obj, vm, view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003442
Chris Wilson3272db52016-08-04 16:32:32 +01003443 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003444 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003445}
3446
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003447static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003448rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003449 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003450 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003451 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003452{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003453 unsigned int column, row;
3454 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003455
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003456 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003457 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003458 for (row = 0; row < height; row++) {
3459 st->nents++;
3460 /* We don't need the pages, but need to initialize
3461 * the entries so the sg list can be happily traversed.
3462 * The only thing we need are DMA addresses.
3463 */
3464 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003465 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003466 sg_dma_len(sg) = PAGE_SIZE;
3467 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003468 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003469 }
3470 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003471
3472 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003473}
3474
3475static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003476intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003477 struct drm_i915_gem_object *obj)
3478{
Dave Gordon85d12252016-05-20 11:54:06 +01003479 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003480 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003481 struct sgt_iter sgt_iter;
3482 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003483 unsigned long i;
3484 dma_addr_t *page_addr_list;
3485 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003486 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003487 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003488
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003489 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003490 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003491 sizeof(dma_addr_t),
3492 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003493 if (!page_addr_list)
3494 return ERR_PTR(ret);
3495
3496 /* Allocate target SG list. */
3497 st = kmalloc(sizeof(*st), GFP_KERNEL);
3498 if (!st)
3499 goto err_st_alloc;
3500
Ville Syrjälä6687c902015-09-15 13:16:41 +03003501 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003502 if (ret)
3503 goto err_sg_alloc;
3504
3505 /* Populate source page list from the object. */
3506 i = 0;
Dave Gordon85d12252016-05-20 11:54:06 +01003507 for_each_sgt_dma(dma_addr, sgt_iter, obj->pages)
3508 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003509
Dave Gordon85d12252016-05-20 11:54:06 +01003510 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003511 st->nents = 0;
3512 sg = st->sgl;
3513
Ville Syrjälä6687c902015-09-15 13:16:41 +03003514 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3515 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3516 rot_info->plane[i].width, rot_info->plane[i].height,
3517 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003518 }
3519
Ville Syrjälä6687c902015-09-15 13:16:41 +03003520 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3521 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003522
3523 drm_free_large(page_addr_list);
3524
3525 return st;
3526
3527err_sg_alloc:
3528 kfree(st);
3529err_st_alloc:
3530 drm_free_large(page_addr_list);
3531
Ville Syrjälä6687c902015-09-15 13:16:41 +03003532 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3533 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3534
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003535 return ERR_PTR(ret);
3536}
3537
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003538static struct sg_table *
3539intel_partial_pages(const struct i915_ggtt_view *view,
3540 struct drm_i915_gem_object *obj)
3541{
3542 struct sg_table *st;
3543 struct scatterlist *sg;
3544 struct sg_page_iter obj_sg_iter;
3545 int ret = -ENOMEM;
3546
3547 st = kmalloc(sizeof(*st), GFP_KERNEL);
3548 if (!st)
3549 goto err_st_alloc;
3550
3551 ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL);
3552 if (ret)
3553 goto err_sg_alloc;
3554
3555 sg = st->sgl;
3556 st->nents = 0;
3557 for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents,
3558 view->params.partial.offset)
3559 {
3560 if (st->nents >= view->params.partial.size)
3561 break;
3562
3563 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3564 sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter);
3565 sg_dma_len(sg) = PAGE_SIZE;
3566
3567 sg = sg_next(sg);
3568 st->nents++;
3569 }
3570
3571 return st;
3572
3573err_sg_alloc:
3574 kfree(st);
3575err_st_alloc:
3576 return ERR_PTR(ret);
3577}
3578
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003579static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003580i915_get_ggtt_vma_pages(struct i915_vma *vma)
3581{
3582 int ret = 0;
3583
Chris Wilson247177d2016-08-15 10:48:47 +01003584 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003585 return 0;
3586
3587 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003588 vma->pages = vma->obj->pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003589 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003590 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003591 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003592 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003593 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003594 else
3595 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3596 vma->ggtt_view.type);
3597
Chris Wilson247177d2016-08-15 10:48:47 +01003598 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003599 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003600 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003601 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003602 } else if (IS_ERR(vma->pages)) {
3603 ret = PTR_ERR(vma->pages);
3604 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003605 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3606 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003607 }
3608
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003609 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003610}
3611
3612/**
3613 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
3614 * @vma: VMA to map
3615 * @cache_level: mapping cache level
3616 * @flags: flags like global or local mapping
3617 *
3618 * DMA addresses are taken from the scatter-gather table of this object (or of
3619 * this VMA in case of non-default GGTT views) and PTE entries set up.
3620 * Note that DMA addresses are also the only part of the SG table we care about.
3621 */
3622int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
3623 u32 flags)
3624{
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003625 u32 bind_flags;
Chris Wilson3272db52016-08-04 16:32:32 +01003626 u32 vma_flags;
3627 int ret;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003628
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003629 if (WARN_ON(flags == 0))
3630 return -EINVAL;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003631
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003632 bind_flags = 0;
Daniel Vetter08755462015-04-20 09:04:05 -07003633 if (flags & PIN_GLOBAL)
Chris Wilson3272db52016-08-04 16:32:32 +01003634 bind_flags |= I915_VMA_GLOBAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003635 if (flags & PIN_USER)
Chris Wilson3272db52016-08-04 16:32:32 +01003636 bind_flags |= I915_VMA_LOCAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003637
Chris Wilson3272db52016-08-04 16:32:32 +01003638 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
Daniel Vetter08755462015-04-20 09:04:05 -07003639 if (flags & PIN_UPDATE)
Chris Wilson3272db52016-08-04 16:32:32 +01003640 bind_flags |= vma_flags;
Daniel Vetter08755462015-04-20 09:04:05 -07003641 else
Chris Wilson3272db52016-08-04 16:32:32 +01003642 bind_flags &= ~vma_flags;
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003643 if (bind_flags == 0)
3644 return 0;
3645
Chris Wilson3272db52016-08-04 16:32:32 +01003646 if (vma_flags == 0 && vma->vm->allocate_va_range) {
Chris Wilson596c5922016-02-26 11:03:20 +00003647 trace_i915_va_alloc(vma);
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003648 ret = vma->vm->allocate_va_range(vma->vm,
3649 vma->node.start,
3650 vma->node.size);
3651 if (ret)
3652 return ret;
3653 }
3654
3655 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003656 if (ret)
3657 return ret;
Daniel Vetter08755462015-04-20 09:04:05 -07003658
Chris Wilson3272db52016-08-04 16:32:32 +01003659 vma->flags |= bind_flags;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003660 return 0;
3661}
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003662
Chris Wilson8ef85612016-04-28 09:56:39 +01003663void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
3664{
3665 void __iomem *ptr;
3666
Chris Wilsone5cdb222016-08-15 10:48:56 +01003667 /* Access through the GTT requires the device to be awake. */
3668 assert_rpm_wakelock_held(to_i915(vma->vm->dev));
3669
Chris Wilson8ef85612016-04-28 09:56:39 +01003670 lockdep_assert_held(&vma->vm->dev->struct_mutex);
Chris Wilson05a20d02016-08-18 17:16:55 +01003671 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma)))
Chris Wilson406ea8d2016-07-20 13:31:55 +01003672 return IO_ERR_PTR(-ENODEV);
Chris Wilson8ef85612016-04-28 09:56:39 +01003673
Chris Wilson3272db52016-08-04 16:32:32 +01003674 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
3675 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
Chris Wilson8ef85612016-04-28 09:56:39 +01003676
3677 ptr = vma->iomap;
3678 if (ptr == NULL) {
Chris Wilsonf7bbe782016-08-19 16:54:27 +01003679 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->mappable,
Chris Wilson8ef85612016-04-28 09:56:39 +01003680 vma->node.start,
3681 vma->node.size);
3682 if (ptr == NULL)
Chris Wilson406ea8d2016-07-20 13:31:55 +01003683 return IO_ERR_PTR(-ENOMEM);
Chris Wilson8ef85612016-04-28 09:56:39 +01003684
3685 vma->iomap = ptr;
3686 }
3687
Chris Wilson20dfbde2016-08-04 16:32:30 +01003688 __i915_vma_pin(vma);
Chris Wilson8ef85612016-04-28 09:56:39 +01003689 return ptr;
3690}
Chris Wilson19880c42016-08-15 10:49:05 +01003691
3692void i915_vma_unpin_and_release(struct i915_vma **p_vma)
3693{
3694 struct i915_vma *vma;
3695
3696 vma = fetch_and_zero(p_vma);
3697 if (!vma)
3698 return;
3699
3700 i915_vma_unpin(vma);
3701 i915_vma_put(vma);
3702}