blob: 1bec50bd651b7c102bc76eaae3fc15f9b2263fec [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{
329 struct device *device = &dev->pdev->dev;
330
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
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300335 p->daddr = dma_map_page(device,
336 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
337
338 if (dma_mapping_error(device, p->daddr)) {
339 __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
425 set_pages_uc(px_page(sp), 1);
426
427 return sp;
428}
429
430static void free_scratch_page(struct drm_device *dev,
431 struct i915_page_scratch *sp)
432{
433 set_pages_wb(px_page(sp), 1);
434
435 cleanup_px(dev, sp);
436 kfree(sp);
437}
438
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300439static struct i915_page_table *alloc_pt(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000440{
Michel Thierryec565b32015-04-08 12:13:23 +0100441 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000442 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
443 GEN8_PTES : GEN6_PTES;
444 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000445
446 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
447 if (!pt)
448 return ERR_PTR(-ENOMEM);
449
Ben Widawsky678d96f2015-03-16 16:00:56 +0000450 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
451 GFP_KERNEL);
452
453 if (!pt->used_ptes)
454 goto fail_bitmap;
455
Mika Kuoppala567047b2015-06-25 18:35:12 +0300456 ret = setup_px(dev, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000457 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300458 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000459
460 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000461
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300462fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000463 kfree(pt->used_ptes);
464fail_bitmap:
465 kfree(pt);
466
467 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000468}
469
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300470static void free_pt(struct drm_device *dev, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000471{
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300472 cleanup_px(dev, pt);
473 kfree(pt->used_ptes);
474 kfree(pt);
475}
476
477static void gen8_initialize_pt(struct i915_address_space *vm,
478 struct i915_page_table *pt)
479{
480 gen8_pte_t scratch_pte;
481
482 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
483 I915_CACHE_LLC, true);
484
485 fill_px(vm->dev, pt, scratch_pte);
486}
487
488static void gen6_initialize_pt(struct i915_address_space *vm,
489 struct i915_page_table *pt)
490{
491 gen6_pte_t scratch_pte;
492
493 WARN_ON(px_dma(vm->scratch_page) == 0);
494
495 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
496 I915_CACHE_LLC, true, 0);
497
498 fill32_px(vm->dev, pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000499}
500
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300501static struct i915_page_directory *alloc_pd(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000502{
Michel Thierryec565b32015-04-08 12:13:23 +0100503 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100504 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000505
506 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
507 if (!pd)
508 return ERR_PTR(-ENOMEM);
509
Michel Thierry33c88192015-04-08 12:13:33 +0100510 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
511 sizeof(*pd->used_pdes), GFP_KERNEL);
512 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300513 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100514
Mika Kuoppala567047b2015-06-25 18:35:12 +0300515 ret = setup_px(dev, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100516 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300517 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100518
Ben Widawsky06fda602015-02-24 16:22:36 +0000519 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100520
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300521fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100522 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300523fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100524 kfree(pd);
525
526 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000527}
528
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300529static void free_pd(struct drm_device *dev, struct i915_page_directory *pd)
530{
531 if (px_page(pd)) {
532 cleanup_px(dev, pd);
533 kfree(pd->used_pdes);
534 kfree(pd);
535 }
536}
537
538static void gen8_initialize_pd(struct i915_address_space *vm,
539 struct i915_page_directory *pd)
540{
541 gen8_pde_t scratch_pde;
542
543 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
544
545 fill_px(vm->dev, pd, scratch_pde);
546}
547
Michel Thierry6ac18502015-07-29 17:23:46 +0100548static int __pdp_init(struct drm_device *dev,
549 struct i915_page_directory_pointer *pdp)
550{
551 size_t pdpes = I915_PDPES_PER_PDP(dev);
552
553 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
554 sizeof(unsigned long),
555 GFP_KERNEL);
556 if (!pdp->used_pdpes)
557 return -ENOMEM;
558
559 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
560 GFP_KERNEL);
561 if (!pdp->page_directory) {
562 kfree(pdp->used_pdpes);
563 /* the PDP might be the statically allocated top level. Keep it
564 * as clean as possible */
565 pdp->used_pdpes = NULL;
566 return -ENOMEM;
567 }
568
569 return 0;
570}
571
572static void __pdp_fini(struct i915_page_directory_pointer *pdp)
573{
574 kfree(pdp->used_pdpes);
575 kfree(pdp->page_directory);
576 pdp->page_directory = NULL;
577}
578
Michel Thierry762d9932015-07-30 11:05:29 +0100579static struct
580i915_page_directory_pointer *alloc_pdp(struct drm_device *dev)
581{
582 struct i915_page_directory_pointer *pdp;
583 int ret = -ENOMEM;
584
585 WARN_ON(!USES_FULL_48BIT_PPGTT(dev));
586
587 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
588 if (!pdp)
589 return ERR_PTR(-ENOMEM);
590
591 ret = __pdp_init(dev, pdp);
592 if (ret)
593 goto fail_bitmap;
594
595 ret = setup_px(dev, pdp);
596 if (ret)
597 goto fail_page_m;
598
599 return pdp;
600
601fail_page_m:
602 __pdp_fini(pdp);
603fail_bitmap:
604 kfree(pdp);
605
606 return ERR_PTR(ret);
607}
608
Michel Thierry6ac18502015-07-29 17:23:46 +0100609static void free_pdp(struct drm_device *dev,
610 struct i915_page_directory_pointer *pdp)
611{
612 __pdp_fini(pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100613 if (USES_FULL_48BIT_PPGTT(dev)) {
614 cleanup_px(dev, pdp);
615 kfree(pdp);
616 }
617}
618
Michel Thierry69ab76f2015-07-29 17:23:55 +0100619static void gen8_initialize_pdp(struct i915_address_space *vm,
620 struct i915_page_directory_pointer *pdp)
621{
622 gen8_ppgtt_pdpe_t scratch_pdpe;
623
624 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
625
626 fill_px(vm->dev, pdp, scratch_pdpe);
627}
628
629static void gen8_initialize_pml4(struct i915_address_space *vm,
630 struct i915_pml4 *pml4)
631{
632 gen8_ppgtt_pml4e_t scratch_pml4e;
633
634 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
635 I915_CACHE_LLC);
636
637 fill_px(vm->dev, pml4, scratch_pml4e);
638}
639
Michel Thierry762d9932015-07-30 11:05:29 +0100640static void
641gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
642 struct i915_page_directory_pointer *pdp,
643 struct i915_page_directory *pd,
644 int index)
645{
646 gen8_ppgtt_pdpe_t *page_directorypo;
647
648 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
649 return;
650
651 page_directorypo = kmap_px(pdp);
652 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
653 kunmap_px(ppgtt, page_directorypo);
654}
655
656static void
657gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
658 struct i915_pml4 *pml4,
659 struct i915_page_directory_pointer *pdp,
660 int index)
661{
662 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
663
664 WARN_ON(!USES_FULL_48BIT_PPGTT(ppgtt->base.dev));
665 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
666 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100667}
668
Ben Widawsky94e409c2013-11-04 22:29:36 -0800669/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100670static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100671 unsigned entry,
672 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800673{
Chris Wilson7e37f882016-08-02 22:50:21 +0100674 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000675 struct intel_engine_cs *engine = req->engine;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800676 int ret;
677
678 BUG_ON(entry >= 4);
679
John Harrison5fb9de12015-05-29 17:44:07 +0100680 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800681 if (ret)
682 return ret;
683
Chris Wilsonb5321f32016-08-02 22:50:18 +0100684 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
685 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
686 intel_ring_emit(ring, upper_32_bits(addr));
687 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
688 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
689 intel_ring_emit(ring, lower_32_bits(addr));
690 intel_ring_advance(ring);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800691
692 return 0;
693}
694
Michel Thierry2dba3232015-07-30 11:06:23 +0100695static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
696 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800697{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800698 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800699
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100700 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300701 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
702
John Harrisone85b26d2015-05-29 17:43:56 +0100703 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800704 if (ret)
705 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800706 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800707
Ben Widawskyeeb94882013-12-06 14:11:10 -0800708 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800709}
710
Michel Thierry2dba3232015-07-30 11:06:23 +0100711static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
712 struct drm_i915_gem_request *req)
713{
714 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
715}
716
Michel Thierryf9b5b782015-07-30 11:02:49 +0100717static void gen8_ppgtt_clear_pte_range(struct i915_address_space *vm,
718 struct i915_page_directory_pointer *pdp,
719 uint64_t start,
720 uint64_t length,
721 gen8_pte_t scratch_pte)
Ben Widawsky459108b2013-11-02 21:07:23 -0700722{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300723 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100724 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100725 unsigned pdpe = gen8_pdpe_index(start);
726 unsigned pde = gen8_pde_index(start);
727 unsigned pte = gen8_pte_index(start);
Ben Widawsky782f1492014-02-20 11:50:33 -0800728 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky459108b2013-11-02 21:07:23 -0700729 unsigned last_pte, i;
730
Michel Thierryf9b5b782015-07-30 11:02:49 +0100731 if (WARN_ON(!pdp))
732 return;
Ben Widawsky459108b2013-11-02 21:07:23 -0700733
734 while (num_entries) {
Michel Thierryec565b32015-04-08 12:13:23 +0100735 struct i915_page_directory *pd;
736 struct i915_page_table *pt;
Ben Widawsky06fda602015-02-24 16:22:36 +0000737
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100738 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100739 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000740
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100741 pd = pdp->page_directory[pdpe];
Ben Widawsky06fda602015-02-24 16:22:36 +0000742
743 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100744 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000745
746 pt = pd->page_table[pde];
747
Mika Kuoppala567047b2015-06-25 18:35:12 +0300748 if (WARN_ON(!px_page(pt)))
Michel Thierry00245262015-06-25 12:59:38 +0100749 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000750
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800751 last_pte = pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +0000752 if (last_pte > GEN8_PTES)
753 last_pte = GEN8_PTES;
Ben Widawsky459108b2013-11-02 21:07:23 -0700754
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300755 pt_vaddr = kmap_px(pt);
Ben Widawsky459108b2013-11-02 21:07:23 -0700756
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800757 for (i = pte; i < last_pte; i++) {
Ben Widawsky459108b2013-11-02 21:07:23 -0700758 pt_vaddr[i] = scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800759 num_entries--;
760 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700761
Matthew Auld44a71022016-04-12 16:57:42 +0100762 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky459108b2013-11-02 21:07:23 -0700763
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800764 pte = 0;
Michel Thierry07749ef2015-03-16 16:00:54 +0000765 if (++pde == I915_PDES) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100766 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
767 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800768 pde = 0;
769 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700770 }
771}
772
Michel Thierryf9b5b782015-07-30 11:02:49 +0100773static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
774 uint64_t start,
775 uint64_t length,
776 bool use_scratch)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700777{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300778 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100779 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
780 I915_CACHE_LLC, use_scratch);
781
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100782 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
783 gen8_ppgtt_clear_pte_range(vm, &ppgtt->pdp, start, length,
784 scratch_pte);
785 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000786 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100787 struct i915_page_directory_pointer *pdp;
788
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000789 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100790 gen8_ppgtt_clear_pte_range(vm, pdp, start, length,
791 scratch_pte);
792 }
793 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100794}
795
796static void
797gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
798 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100799 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100800 uint64_t start,
801 enum i915_cache_level cache_level)
802{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300803 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +0000804 gen8_pte_t *pt_vaddr;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100805 unsigned pdpe = gen8_pdpe_index(start);
806 unsigned pde = gen8_pde_index(start);
807 unsigned pte = gen8_pte_index(start);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700808
Chris Wilson6f1cc992013-12-31 15:50:31 +0000809 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700810
Michel Thierry3387d432015-08-03 09:52:47 +0100811 while (__sg_page_iter_next(sg_iter)) {
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000812 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100813 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100814 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300815 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000816 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800817
818 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100819 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Chris Wilson6f1cc992013-12-31 15:50:31 +0000820 cache_level, true);
Michel Thierry07749ef2015-03-16 16:00:54 +0000821 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300822 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000823 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000824 if (++pde == I915_PDES) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100825 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
826 break;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800827 pde = 0;
828 }
829 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700830 }
831 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300832
833 if (pt_vaddr)
834 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700835}
836
Michel Thierryf9b5b782015-07-30 11:02:49 +0100837static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
838 struct sg_table *pages,
839 uint64_t start,
840 enum i915_cache_level cache_level,
841 u32 unused)
842{
Joonas Lahtinene5716f52016-04-07 11:08:03 +0300843 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry3387d432015-08-03 09:52:47 +0100844 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100845
Michel Thierry3387d432015-08-03 09:52:47 +0100846 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100847
848 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
849 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
850 cache_level);
851 } else {
852 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000853 uint64_t pml4e;
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100854 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
855
Dave Gordone8ebd8e2015-12-08 13:30:51 +0000856 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
Michel Thierryde5ba8e2015-08-03 09:53:27 +0100857 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
858 start, cache_level);
859 }
860 }
Michel Thierryf9b5b782015-07-30 11:02:49 +0100861}
862
Michel Thierryf37c0502015-06-10 17:46:39 +0100863static void gen8_free_page_tables(struct drm_device *dev,
864 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800865{
866 int i;
867
Mika Kuoppala567047b2015-06-25 18:35:12 +0300868 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800869 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800870
Michel Thierry33c88192015-04-08 12:13:33 +0100871 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000872 if (WARN_ON(!pd->page_table[i]))
873 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800874
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300875 free_pt(dev, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000876 pd->page_table[i] = NULL;
877 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000878}
879
Mika Kuoppala8776f022015-06-30 18:16:40 +0300880static int gen8_init_scratch(struct i915_address_space *vm)
881{
882 struct drm_device *dev = vm->dev;
Matthew Auld64c050d2016-04-27 13:19:25 +0100883 int ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300884
885 vm->scratch_page = alloc_scratch_page(dev);
886 if (IS_ERR(vm->scratch_page))
887 return PTR_ERR(vm->scratch_page);
888
889 vm->scratch_pt = alloc_pt(dev);
890 if (IS_ERR(vm->scratch_pt)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100891 ret = PTR_ERR(vm->scratch_pt);
892 goto free_scratch_page;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300893 }
894
895 vm->scratch_pd = alloc_pd(dev);
896 if (IS_ERR(vm->scratch_pd)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100897 ret = PTR_ERR(vm->scratch_pd);
898 goto free_pt;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300899 }
900
Michel Thierry69ab76f2015-07-29 17:23:55 +0100901 if (USES_FULL_48BIT_PPGTT(dev)) {
902 vm->scratch_pdp = alloc_pdp(dev);
903 if (IS_ERR(vm->scratch_pdp)) {
Matthew Auld64c050d2016-04-27 13:19:25 +0100904 ret = PTR_ERR(vm->scratch_pdp);
905 goto free_pd;
Michel Thierry69ab76f2015-07-29 17:23:55 +0100906 }
907 }
908
Mika Kuoppala8776f022015-06-30 18:16:40 +0300909 gen8_initialize_pt(vm, vm->scratch_pt);
910 gen8_initialize_pd(vm, vm->scratch_pd);
Michel Thierry69ab76f2015-07-29 17:23:55 +0100911 if (USES_FULL_48BIT_PPGTT(dev))
912 gen8_initialize_pdp(vm, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300913
914 return 0;
Matthew Auld64c050d2016-04-27 13:19:25 +0100915
916free_pd:
917 free_pd(dev, vm->scratch_pd);
918free_pt:
919 free_pt(dev, vm->scratch_pt);
920free_scratch_page:
921 free_scratch_page(dev, vm->scratch_page);
922
923 return ret;
Mika Kuoppala8776f022015-06-30 18:16:40 +0300924}
925
Zhiyuan Lv650da342015-08-28 15:41:18 +0800926static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
927{
928 enum vgt_g2v_type msg;
Matthew Aulddf285642016-04-22 12:09:25 +0100929 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
Zhiyuan Lv650da342015-08-28 15:41:18 +0800930 int i;
931
Matthew Aulddf285642016-04-22 12:09:25 +0100932 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
Zhiyuan Lv650da342015-08-28 15:41:18 +0800933 u64 daddr = px_dma(&ppgtt->pml4);
934
Ville Syrjäläab75bb52015-11-04 23:20:12 +0200935 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
936 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +0800937
938 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
939 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
940 } else {
941 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
942 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
943
Ville Syrjäläab75bb52015-11-04 23:20:12 +0200944 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
945 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
Zhiyuan Lv650da342015-08-28 15:41:18 +0800946 }
947
948 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
949 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
950 }
951
952 I915_WRITE(vgtif_reg(g2v_notify), msg);
953
954 return 0;
955}
956
Mika Kuoppala8776f022015-06-30 18:16:40 +0300957static void gen8_free_scratch(struct i915_address_space *vm)
958{
959 struct drm_device *dev = vm->dev;
960
Michel Thierry69ab76f2015-07-29 17:23:55 +0100961 if (USES_FULL_48BIT_PPGTT(dev))
962 free_pdp(dev, vm->scratch_pdp);
Mika Kuoppala8776f022015-06-30 18:16:40 +0300963 free_pd(dev, vm->scratch_pd);
964 free_pt(dev, vm->scratch_pt);
965 free_scratch_page(dev, vm->scratch_page);
966}
967
Michel Thierry762d9932015-07-30 11:05:29 +0100968static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
969 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800970{
971 int i;
972
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100973 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
974 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +0000975 continue;
976
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100977 gen8_free_page_tables(dev, pdp->page_directory[i]);
978 free_pd(dev, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800979 }
Michel Thierry69876be2015-04-08 12:13:27 +0100980
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100981 free_pdp(dev, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100982}
983
984static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
985{
986 int i;
987
988 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
989 if (WARN_ON(!ppgtt->pml4.pdps[i]))
990 continue;
991
992 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
993 }
994
995 cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
996}
997
998static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
999{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001000 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001001
Chris Wilsonc0336662016-05-06 15:40:21 +01001002 if (intel_vgpu_active(to_i915(vm->dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001003 gen8_ppgtt_notify_vgt(ppgtt, false);
1004
Michel Thierry762d9932015-07-30 11:05:29 +01001005 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
1006 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
1007 else
1008 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001009
Mika Kuoppala8776f022015-06-30 18:16:40 +03001010 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -08001011}
1012
Michel Thierryd7b26332015-04-08 12:13:34 +01001013/**
1014 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001015 * @vm: Master vm structure.
1016 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +01001017 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001018 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +01001019 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1020 * caller to free on error.
1021 *
1022 * Allocate the required number of page tables. Extremely similar to
1023 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1024 * the page directory boundary (instead of the page directory pointer). That
1025 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1026 * possible, and likely that the caller will need to use multiple calls of this
1027 * function to achieve the appropriate allocation.
1028 *
1029 * Return: 0 if success; negative error code otherwise.
1030 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001031static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +01001032 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +01001033 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +01001034 uint64_t length,
1035 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001036{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001037 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001038 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001039 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001040
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001041 gen8_for_each_pde(pt, pd, start, length, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001042 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +01001043 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001044 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001045 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001046 continue;
1047 }
1048
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001049 pt = alloc_pt(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001050 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +00001051 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001052
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001053 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +01001054 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001055 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001056 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001057 }
1058
1059 return 0;
1060
1061unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001062 for_each_set_bit(pde, new_pts, I915_PDES)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001063 free_pt(dev, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001064
1065 return -ENOMEM;
1066}
1067
Michel Thierryd7b26332015-04-08 12:13:34 +01001068/**
1069 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001070 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +01001071 * @pdp: Page directory pointer for this address range.
1072 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001073 * @length: Size of the allocations.
1074 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +01001075 * caller to free on error.
1076 *
1077 * Allocate the required number of page directories starting at the pde index of
1078 * @start, and ending at the pde index @start + @length. This function will skip
1079 * over already allocated page directories within the range, and only allocate
1080 * new ones, setting the appropriate pointer within the pdp as well as the
1081 * correct position in the bitmap @new_pds.
1082 *
1083 * The function will only allocate the pages within the range for a give page
1084 * directory pointer. In other words, if @start + @length straddles a virtually
1085 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1086 * required by the caller, This is not currently possible, and the BUG in the
1087 * code will prevent it.
1088 *
1089 * Return: 0 if success; negative error code otherwise.
1090 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001091static int
1092gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1093 struct i915_page_directory_pointer *pdp,
1094 uint64_t start,
1095 uint64_t length,
1096 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001097{
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001098 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +01001099 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +01001100 uint32_t pdpe;
Michel Thierry6ac18502015-07-29 17:23:46 +01001101 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001102
Michel Thierry6ac18502015-07-29 17:23:46 +01001103 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +01001104
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001105 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001106 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +01001107 continue;
Michel Thierry33c88192015-04-08 12:13:33 +01001108
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001109 pd = alloc_pd(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001110 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001111 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001112
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001113 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001114 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001115 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001116 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001117 }
1118
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001119 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001120
1121unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001122 for_each_set_bit(pdpe, new_pds, pdpes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001123 free_pd(dev, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001124
1125 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001126}
1127
Michel Thierry762d9932015-07-30 11:05:29 +01001128/**
1129 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1130 * @vm: Master vm structure.
1131 * @pml4: Page map level 4 for this address range.
1132 * @start: Starting virtual address to begin allocations.
1133 * @length: Size of the allocations.
1134 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1135 * caller to free on error.
1136 *
1137 * Allocate the required number of page directory pointers. Extremely similar to
1138 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1139 * The main difference is here we are limited by the pml4 boundary (instead of
1140 * the page directory pointer).
1141 *
1142 * Return: 0 if success; negative error code otherwise.
1143 */
1144static int
1145gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1146 struct i915_pml4 *pml4,
1147 uint64_t start,
1148 uint64_t length,
1149 unsigned long *new_pdps)
1150{
1151 struct drm_device *dev = vm->dev;
1152 struct i915_page_directory_pointer *pdp;
Michel Thierry762d9932015-07-30 11:05:29 +01001153 uint32_t pml4e;
1154
1155 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1156
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001157 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001158 if (!test_bit(pml4e, pml4->used_pml4es)) {
1159 pdp = alloc_pdp(dev);
1160 if (IS_ERR(pdp))
1161 goto unwind_out;
1162
Michel Thierry69ab76f2015-07-29 17:23:55 +01001163 gen8_initialize_pdp(vm, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +01001164 pml4->pdps[pml4e] = pdp;
1165 __set_bit(pml4e, new_pdps);
1166 trace_i915_page_directory_pointer_entry_alloc(vm,
1167 pml4e,
1168 start,
1169 GEN8_PML4E_SHIFT);
1170 }
1171 }
1172
1173 return 0;
1174
1175unwind_out:
1176 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1177 free_pdp(dev, pml4->pdps[pml4e]);
1178
1179 return -ENOMEM;
1180}
1181
Michel Thierryd7b26332015-04-08 12:13:34 +01001182static void
Michał Winiarski3a41a052015-09-03 19:22:18 +02001183free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
Michel Thierryd7b26332015-04-08 12:13:34 +01001184{
Michel Thierryd7b26332015-04-08 12:13:34 +01001185 kfree(new_pts);
1186 kfree(new_pds);
1187}
1188
1189/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1190 * of these are based on the number of PDPEs in the system.
1191 */
1192static
1193int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001194 unsigned long **new_pts,
Michel Thierry6ac18502015-07-29 17:23:46 +01001195 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001196{
Michel Thierryd7b26332015-04-08 12:13:34 +01001197 unsigned long *pds;
Michał Winiarski3a41a052015-09-03 19:22:18 +02001198 unsigned long *pts;
Michel Thierryd7b26332015-04-08 12:13:34 +01001199
Michał Winiarski3a41a052015-09-03 19:22:18 +02001200 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
Michel Thierryd7b26332015-04-08 12:13:34 +01001201 if (!pds)
1202 return -ENOMEM;
1203
Michał Winiarski3a41a052015-09-03 19:22:18 +02001204 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1205 GFP_TEMPORARY);
1206 if (!pts)
1207 goto err_out;
Michel Thierryd7b26332015-04-08 12:13:34 +01001208
1209 *new_pds = pds;
1210 *new_pts = pts;
1211
1212 return 0;
1213
1214err_out:
Michał Winiarski3a41a052015-09-03 19:22:18 +02001215 free_gen8_temp_bitmaps(pds, pts);
Michel Thierryd7b26332015-04-08 12:13:34 +01001216 return -ENOMEM;
1217}
1218
Mika Kuoppala5b7e4c92015-06-25 18:35:03 +03001219/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
1220 * the page table structures, we mark them dirty so that
1221 * context switching/execlist queuing code takes extra steps
1222 * to ensure that tlbs are flushed.
1223 */
1224static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1225{
1226 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1227}
1228
Michel Thierry762d9932015-07-30 11:05:29 +01001229static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1230 struct i915_page_directory_pointer *pdp,
1231 uint64_t start,
1232 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001233{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001234 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michał Winiarski3a41a052015-09-03 19:22:18 +02001235 unsigned long *new_page_dirs, *new_page_tables;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001236 struct drm_device *dev = vm->dev;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001237 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001238 const uint64_t orig_start = start;
1239 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001240 uint32_t pdpe;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001241 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001242 int ret;
1243
Michel Thierryd7b26332015-04-08 12:13:34 +01001244 /* Wrap is never okay since we can only represent 48b, and we don't
1245 * actually use the other side of the canonical address space.
1246 */
1247 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001248 return -ENODEV;
1249
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001250 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001251 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001252
Michel Thierry6ac18502015-07-29 17:23:46 +01001253 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001254 if (ret)
1255 return ret;
1256
Michel Thierryd7b26332015-04-08 12:13:34 +01001257 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001258 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1259 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001260 if (ret) {
Michał Winiarski3a41a052015-09-03 19:22:18 +02001261 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Michel Thierryd7b26332015-04-08 12:13:34 +01001262 return ret;
1263 }
1264
1265 /* For every page directory referenced, allocate page tables */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001266 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001267 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michał Winiarski3a41a052015-09-03 19:22:18 +02001268 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
Michel Thierry5441f0c2015-04-08 12:13:28 +01001269 if (ret)
1270 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001271 }
1272
Michel Thierry33c88192015-04-08 12:13:33 +01001273 start = orig_start;
1274 length = orig_length;
1275
Michel Thierryd7b26332015-04-08 12:13:34 +01001276 /* Allocations have completed successfully, so set the bitmaps, and do
1277 * the mappings. */
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001278 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001279 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001280 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001281 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001282 uint64_t pd_start = start;
1283 uint32_t pde;
1284
Michel Thierryd7b26332015-04-08 12:13:34 +01001285 /* Every pd should be allocated, we just did that above. */
1286 WARN_ON(!pd);
1287
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001288 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001289 /* Same reasoning as pd */
1290 WARN_ON(!pt);
1291 WARN_ON(!pd_len);
1292 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1293
1294 /* Set our used ptes within the page table */
1295 bitmap_set(pt->used_ptes,
1296 gen8_pte_index(pd_start),
1297 gen8_pte_count(pd_start, pd_len));
1298
1299 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001300 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001301
1302 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001303 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1304 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001305 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1306 gen8_pte_index(start),
1307 gen8_pte_count(start, length),
1308 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001309
1310 /* NB: We haven't yet mapped ptes to pages. At this
1311 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001312 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001313
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001314 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001315 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001316 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001317 }
1318
Michał Winiarski3a41a052015-09-03 19:22:18 +02001319 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c92015-06-25 18:35:03 +03001320 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001321 return 0;
1322
1323err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001324 while (pdpe--) {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001325 unsigned long temp;
1326
Michał Winiarski3a41a052015-09-03 19:22:18 +02001327 for_each_set_bit(temp, new_page_tables + pdpe *
1328 BITS_TO_LONGS(I915_PDES), I915_PDES)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001329 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001330 }
1331
Michel Thierry6ac18502015-07-29 17:23:46 +01001332 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001333 free_pd(dev, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001334
Michał Winiarski3a41a052015-09-03 19:22:18 +02001335 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Mika Kuoppala5b7e4c92015-06-25 18:35:03 +03001336 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001337 return ret;
1338}
1339
Michel Thierry762d9932015-07-30 11:05:29 +01001340static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1341 struct i915_pml4 *pml4,
1342 uint64_t start,
1343 uint64_t length)
1344{
1345 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001346 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001347 struct i915_page_directory_pointer *pdp;
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001348 uint64_t pml4e;
Michel Thierry762d9932015-07-30 11:05:29 +01001349 int ret = 0;
1350
1351 /* Do the pml4 allocations first, so we don't need to track the newly
1352 * allocated tables below the pdp */
1353 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1354
1355 /* The pagedirectory and pagetable allocations are done in the shared 3
1356 * and 4 level code. Just allocate the pdps.
1357 */
1358 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1359 new_pdps);
1360 if (ret)
1361 return ret;
1362
1363 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1364 "The allocation has spanned more than 512GB. "
1365 "It is highly likely this is incorrect.");
1366
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001367 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierry762d9932015-07-30 11:05:29 +01001368 WARN_ON(!pdp);
1369
1370 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1371 if (ret)
1372 goto err_out;
1373
1374 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1375 }
1376
1377 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1378 GEN8_PML4ES_PER_PML4);
1379
1380 return 0;
1381
1382err_out:
1383 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1384 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
1385
1386 return ret;
1387}
1388
1389static int gen8_alloc_va_range(struct i915_address_space *vm,
1390 uint64_t start, uint64_t length)
1391{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001392 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry762d9932015-07-30 11:05:29 +01001393
1394 if (USES_FULL_48BIT_PPGTT(vm->dev))
1395 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1396 else
1397 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1398}
1399
Michel Thierryea91e402015-07-29 17:23:57 +01001400static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1401 uint64_t start, uint64_t length,
1402 gen8_pte_t scratch_pte,
1403 struct seq_file *m)
1404{
1405 struct i915_page_directory *pd;
Michel Thierryea91e402015-07-29 17:23:57 +01001406 uint32_t pdpe;
1407
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001408 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
Michel Thierryea91e402015-07-29 17:23:57 +01001409 struct i915_page_table *pt;
1410 uint64_t pd_len = length;
1411 uint64_t pd_start = start;
1412 uint32_t pde;
1413
1414 if (!test_bit(pdpe, pdp->used_pdpes))
1415 continue;
1416
1417 seq_printf(m, "\tPDPE #%d\n", pdpe);
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001418 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
Michel Thierryea91e402015-07-29 17:23:57 +01001419 uint32_t pte;
1420 gen8_pte_t *pt_vaddr;
1421
1422 if (!test_bit(pde, pd->used_pdes))
1423 continue;
1424
1425 pt_vaddr = kmap_px(pt);
1426 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1427 uint64_t va =
1428 (pdpe << GEN8_PDPE_SHIFT) |
1429 (pde << GEN8_PDE_SHIFT) |
1430 (pte << GEN8_PTE_SHIFT);
1431 int i;
1432 bool found = false;
1433
1434 for (i = 0; i < 4; i++)
1435 if (pt_vaddr[pte + i] != scratch_pte)
1436 found = true;
1437 if (!found)
1438 continue;
1439
1440 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1441 for (i = 0; i < 4; i++) {
1442 if (pt_vaddr[pte + i] != scratch_pte)
1443 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1444 else
1445 seq_puts(m, " SCRATCH ");
1446 }
1447 seq_puts(m, "\n");
1448 }
1449 /* don't use kunmap_px, it could trigger
1450 * an unnecessary flush.
1451 */
1452 kunmap_atomic(pt_vaddr);
1453 }
1454 }
1455}
1456
1457static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1458{
1459 struct i915_address_space *vm = &ppgtt->base;
1460 uint64_t start = ppgtt->base.start;
1461 uint64_t length = ppgtt->base.total;
1462 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
1463 I915_CACHE_LLC, true);
1464
1465 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
1466 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1467 } else {
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001468 uint64_t pml4e;
Michel Thierryea91e402015-07-29 17:23:57 +01001469 struct i915_pml4 *pml4 = &ppgtt->pml4;
1470 struct i915_page_directory_pointer *pdp;
1471
Dave Gordone8ebd8e2015-12-08 13:30:51 +00001472 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
Michel Thierryea91e402015-07-29 17:23:57 +01001473 if (!test_bit(pml4e, pml4->used_pml4es))
1474 continue;
1475
1476 seq_printf(m, " PML4E #%llu\n", pml4e);
1477 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1478 }
1479 }
1480}
1481
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001482static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1483{
Michał Winiarski3a41a052015-09-03 19:22:18 +02001484 unsigned long *new_page_dirs, *new_page_tables;
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001485 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1486 int ret;
1487
1488 /* We allocate temp bitmap for page tables for no gain
1489 * but as this is for init only, lets keep the things simple
1490 */
1491 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1492 if (ret)
1493 return ret;
1494
1495 /* Allocate for all pdps regardless of how the ppgtt
1496 * was defined.
1497 */
1498 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1499 0, 1ULL << 32,
1500 new_page_dirs);
1501 if (!ret)
1502 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1503
Michał Winiarski3a41a052015-09-03 19:22:18 +02001504 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001505
1506 return ret;
1507}
1508
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001509/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001510 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1511 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1512 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1513 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001514 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001515 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001516static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001517{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001518 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001519
Mika Kuoppala8776f022015-06-30 18:16:40 +03001520 ret = gen8_init_scratch(&ppgtt->base);
1521 if (ret)
1522 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001523
Michel Thierryd7b26332015-04-08 12:13:34 +01001524 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001525 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001526 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001527 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001528 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001529 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1530 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryea91e402015-07-29 17:23:57 +01001531 ppgtt->debug_dump = gen8_dump_ppgtt;
Michel Thierryd7b26332015-04-08 12:13:34 +01001532
Michel Thierry762d9932015-07-30 11:05:29 +01001533 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
1534 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
1535 if (ret)
1536 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001537
Michel Thierry69ab76f2015-07-29 17:23:55 +01001538 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1539
Michel Thierry762d9932015-07-30 11:05:29 +01001540 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001541 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001542 } else {
Michel Thierry25f50332015-08-07 17:40:19 +01001543 ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001544 if (ret)
1545 goto free_scratch;
1546
1547 ppgtt->base.total = 1ULL << 32;
Michel Thierry2dba3232015-07-30 11:06:23 +01001548 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001549 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1550 0, 0,
1551 GEN8_PML4E_SHIFT);
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001552
Chris Wilsonc0336662016-05-06 15:40:21 +01001553 if (intel_vgpu_active(to_i915(ppgtt->base.dev))) {
Zhiyuan Lv331f38e2015-08-28 15:41:14 +08001554 ret = gen8_preallocate_top_level_pdps(ppgtt);
1555 if (ret)
1556 goto free_scratch;
1557 }
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001558 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001559
Chris Wilsonc0336662016-05-06 15:40:21 +01001560 if (intel_vgpu_active(to_i915(ppgtt->base.dev)))
Zhiyuan Lv650da342015-08-28 15:41:18 +08001561 gen8_ppgtt_notify_vgt(ppgtt, true);
1562
Michel Thierryd7b26332015-04-08 12:13:34 +01001563 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001564
1565free_scratch:
1566 gen8_free_scratch(&ppgtt->base);
1567 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001568}
1569
Ben Widawsky87d60b62013-12-06 14:11:29 -08001570static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1571{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001572 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001573 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001574 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001575 uint32_t pd_entry;
Dave Gordon731f74c2016-06-24 19:37:46 +01001576 uint32_t pte, pde;
Michel Thierry09942c62015-04-08 12:13:30 +01001577 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001578
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001579 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1580 I915_CACHE_LLC, true, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001581
Dave Gordon731f74c2016-06-24 19:37:46 +01001582 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001583 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001584 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001585 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001586 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001587 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1588
1589 if (pd_entry != expected)
1590 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1591 pde,
1592 pd_entry,
1593 expected);
1594 seq_printf(m, "\tPDE: %x\n", pd_entry);
1595
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001596 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1597
Michel Thierry07749ef2015-03-16 16:00:54 +00001598 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001599 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001600 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001601 (pte * PAGE_SIZE);
1602 int i;
1603 bool found = false;
1604 for (i = 0; i < 4; i++)
1605 if (pt_vaddr[pte + i] != scratch_pte)
1606 found = true;
1607 if (!found)
1608 continue;
1609
1610 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1611 for (i = 0; i < 4; i++) {
1612 if (pt_vaddr[pte + i] != scratch_pte)
1613 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1614 else
1615 seq_puts(m, " SCRATCH ");
1616 }
1617 seq_puts(m, "\n");
1618 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001619 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001620 }
1621}
1622
Ben Widawsky678d96f2015-03-16 16:00:56 +00001623/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001624static void gen6_write_pde(struct i915_page_directory *pd,
1625 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001626{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001627 /* Caller needs to make sure the write completes if necessary */
1628 struct i915_hw_ppgtt *ppgtt =
1629 container_of(pd, struct i915_hw_ppgtt, pd);
1630 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001631
Mika Kuoppala567047b2015-06-25 18:35:12 +03001632 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001633 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001634
Ben Widawsky678d96f2015-03-16 16:00:56 +00001635 writel(pd_entry, ppgtt->pd_addr + pde);
1636}
Ben Widawsky61973492013-04-08 18:43:54 -07001637
Ben Widawsky678d96f2015-03-16 16:00:56 +00001638/* Write all the page tables found in the ppgtt structure to incrementing page
1639 * directories. */
1640static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001641 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001642 uint32_t start, uint32_t length)
1643{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001644 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierryec565b32015-04-08 12:13:23 +01001645 struct i915_page_table *pt;
Dave Gordon731f74c2016-06-24 19:37:46 +01001646 uint32_t pde;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001647
Dave Gordon731f74c2016-06-24 19:37:46 +01001648 gen6_for_each_pde(pt, pd, start, length, pde)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001649 gen6_write_pde(pd, pde, pt);
1650
1651 /* Make sure write is complete before other code can use this page
1652 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001653 readl(ggtt->gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001654}
1655
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001656static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001657{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001658 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001659
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001660 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001661}
Ben Widawsky61973492013-04-08 18:43:54 -07001662
Ben Widawsky90252e52013-12-06 14:11:12 -08001663static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001664 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001665{
Chris Wilson7e37f882016-08-02 22:50:21 +01001666 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001667 struct intel_engine_cs *engine = req->engine;
Ben Widawsky90252e52013-12-06 14:11:12 -08001668 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001669
Ben Widawsky90252e52013-12-06 14:11:12 -08001670 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001671 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001672 if (ret)
1673 return ret;
1674
John Harrison5fb9de12015-05-29 17:44:07 +01001675 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001676 if (ret)
1677 return ret;
1678
Chris Wilsonb5321f32016-08-02 22:50:18 +01001679 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1680 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1681 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1682 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1683 intel_ring_emit(ring, get_pd_offset(ppgtt));
1684 intel_ring_emit(ring, MI_NOOP);
1685 intel_ring_advance(ring);
Ben Widawsky90252e52013-12-06 14:11:12 -08001686
1687 return 0;
1688}
1689
Ben Widawsky48a10382013-12-06 14:11:11 -08001690static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001691 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001692{
Chris Wilson7e37f882016-08-02 22:50:21 +01001693 struct intel_ring *ring = req->ring;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001694 struct intel_engine_cs *engine = req->engine;
Ben Widawsky48a10382013-12-06 14:11:11 -08001695 int ret;
1696
Ben Widawsky48a10382013-12-06 14:11:11 -08001697 /* NB: TLBs must be flushed and invalidated before a switch */
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001698 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky48a10382013-12-06 14:11:11 -08001699 if (ret)
1700 return ret;
1701
John Harrison5fb9de12015-05-29 17:44:07 +01001702 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001703 if (ret)
1704 return ret;
1705
Chris Wilsonb5321f32016-08-02 22:50:18 +01001706 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1707 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1708 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1709 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1710 intel_ring_emit(ring, get_pd_offset(ppgtt));
1711 intel_ring_emit(ring, MI_NOOP);
1712 intel_ring_advance(ring);
Ben Widawsky48a10382013-12-06 14:11:11 -08001713
Ben Widawsky90252e52013-12-06 14:11:12 -08001714 /* XXX: RCS is the only one to auto invalidate the TLBs? */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001715 if (engine->id != RCS) {
Chris Wilson7c9cf4e2016-08-02 22:50:25 +01001716 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
Ben Widawsky90252e52013-12-06 14:11:12 -08001717 if (ret)
1718 return ret;
1719 }
1720
Ben Widawsky48a10382013-12-06 14:11:11 -08001721 return 0;
1722}
1723
Ben Widawskyeeb94882013-12-06 14:11:10 -08001724static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001725 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001726{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001727 struct intel_engine_cs *engine = req->engine;
Chris Wilson8eb95202016-07-04 08:48:31 +01001728 struct drm_i915_private *dev_priv = req->i915;
Ben Widawsky48a10382013-12-06 14:11:11 -08001729
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001730 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1731 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001732 return 0;
1733}
1734
Daniel Vetter82460d92014-08-06 20:19:53 +02001735static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001736{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001737 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001738 struct intel_engine_cs *engine;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001739
Dave Gordonb4ac5af2016-03-24 11:20:38 +00001740 for_each_engine(engine, dev_priv) {
Michel Thierry2dba3232015-07-30 11:06:23 +01001741 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001742 I915_WRITE(RING_MODE_GEN7(engine),
Michel Thierry2dba3232015-07-30 11:06:23 +01001743 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001744 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001745}
1746
Daniel Vetter82460d92014-08-06 20:19:53 +02001747static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001748{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001749 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001750 struct intel_engine_cs *engine;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001751 uint32_t ecochk, ecobits;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001752
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001753 ecobits = I915_READ(GAC_ECO_BITS);
1754 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1755
1756 ecochk = I915_READ(GAM_ECOCHK);
1757 if (IS_HASWELL(dev)) {
1758 ecochk |= ECOCHK_PPGTT_WB_HSW;
1759 } else {
1760 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1761 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1762 }
1763 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001764
Dave Gordonb4ac5af2016-03-24 11:20:38 +00001765 for_each_engine(engine, dev_priv) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001766 /* GFX_MODE is per-ring on gen7+ */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001767 I915_WRITE(RING_MODE_GEN7(engine),
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001768 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001769 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001770}
1771
Daniel Vetter82460d92014-08-06 20:19:53 +02001772static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001773{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001774 struct drm_i915_private *dev_priv = to_i915(dev);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001775 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001776
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001777 ecobits = I915_READ(GAC_ECO_BITS);
1778 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1779 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001780
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001781 gab_ctl = I915_READ(GAB_CTL);
1782 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001783
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001784 ecochk = I915_READ(GAM_ECOCHK);
1785 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001786
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001787 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001788}
1789
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001790/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001791static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001792 uint64_t start,
1793 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001794 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001795{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001796 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierry07749ef2015-03-16 16:00:54 +00001797 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001798 unsigned first_entry = start >> PAGE_SHIFT;
1799 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001800 unsigned act_pt = first_entry / GEN6_PTES;
1801 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001802 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001803
Mika Kuoppalac114f762015-06-25 18:35:13 +03001804 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1805 I915_CACHE_LLC, true, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001806
Daniel Vetter7bddb012012-02-09 17:15:47 +01001807 while (num_entries) {
1808 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001809 if (last_pte > GEN6_PTES)
1810 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001811
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001812 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001813
1814 for (i = first_pte; i < last_pte; i++)
1815 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001816
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001817 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001818
Daniel Vetter7bddb012012-02-09 17:15:47 +01001819 num_entries -= last_pte - first_pte;
1820 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001821 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001822 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001823}
1824
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001825static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001826 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001827 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301828 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001829{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001830 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08001831 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001832 unsigned act_pt = first_entry / GEN6_PTES;
1833 unsigned act_pte = first_entry % GEN6_PTES;
Dave Gordon85d12252016-05-20 11:54:06 +01001834 gen6_pte_t *pt_vaddr = NULL;
1835 struct sgt_iter sgt_iter;
1836 dma_addr_t addr;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001837
Dave Gordon85d12252016-05-20 11:54:06 +01001838 for_each_sgt_dma(addr, sgt_iter, pages) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001839 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001840 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001841
Chris Wilsoncc797142013-12-31 15:50:30 +00001842 pt_vaddr[act_pte] =
Dave Gordon85d12252016-05-20 11:54:06 +01001843 vm->pte_encode(addr, cache_level, true, flags);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301844
Michel Thierry07749ef2015-03-16 16:00:54 +00001845 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001846 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001847 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001848 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001849 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001850 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001851 }
Dave Gordon85d12252016-05-20 11:54:06 +01001852
Chris Wilsoncc797142013-12-31 15:50:30 +00001853 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001854 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001855}
1856
Ben Widawsky678d96f2015-03-16 16:00:56 +00001857static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001858 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001859{
Michel Thierry4933d512015-03-24 15:46:22 +00001860 DECLARE_BITMAP(new_page_tables, I915_PDES);
1861 struct drm_device *dev = vm->dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001862 struct drm_i915_private *dev_priv = to_i915(dev);
1863 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001864 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Michel Thierryec565b32015-04-08 12:13:23 +01001865 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001866 uint32_t start, length, start_save, length_save;
Dave Gordon731f74c2016-06-24 19:37:46 +01001867 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00001868 int ret;
1869
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001870 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1871 return -ENODEV;
1872
1873 start = start_save = start_in;
1874 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001875
1876 bitmap_zero(new_page_tables, I915_PDES);
1877
1878 /* The allocation is done in two stages so that we can bail out with
1879 * minimal amount of pain. The first stage finds new page tables that
1880 * need allocation. The second stage marks use ptes within the page
1881 * tables.
1882 */
Dave Gordon731f74c2016-06-24 19:37:46 +01001883 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001884 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001885 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1886 continue;
1887 }
1888
1889 /* We've already allocated a page table */
1890 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1891
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001892 pt = alloc_pt(dev);
Michel Thierry4933d512015-03-24 15:46:22 +00001893 if (IS_ERR(pt)) {
1894 ret = PTR_ERR(pt);
1895 goto unwind_out;
1896 }
1897
1898 gen6_initialize_pt(vm, pt);
1899
1900 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001901 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001902 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001903 }
1904
1905 start = start_save;
1906 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001907
Dave Gordon731f74c2016-06-24 19:37:46 +01001908 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
Ben Widawsky678d96f2015-03-16 16:00:56 +00001909 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1910
1911 bitmap_zero(tmp_bitmap, GEN6_PTES);
1912 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1913 gen6_pte_count(start, length));
1914
Mika Kuoppala966082c2015-06-25 18:35:19 +03001915 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001916 gen6_write_pde(&ppgtt->pd, pde, pt);
1917
Michel Thierry72744cb2015-03-24 15:46:23 +00001918 trace_i915_page_table_entry_map(vm, pde, pt,
1919 gen6_pte_index(start),
1920 gen6_pte_count(start, length),
1921 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001922 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001923 GEN6_PTES);
1924 }
1925
Michel Thierry4933d512015-03-24 15:46:22 +00001926 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1927
1928 /* Make sure write is complete before other code can use this page
1929 * table. Also require for WC mapped PTEs */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001930 readl(ggtt->gsm);
Michel Thierry4933d512015-03-24 15:46:22 +00001931
Ben Widawsky563222a2015-03-19 12:53:28 +00001932 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001933 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001934
1935unwind_out:
1936 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001937 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00001938
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001939 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001940 free_pt(vm->dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001941 }
1942
1943 mark_tlbs_dirty(ppgtt);
1944 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001945}
1946
Mika Kuoppala8776f022015-06-30 18:16:40 +03001947static int gen6_init_scratch(struct i915_address_space *vm)
1948{
1949 struct drm_device *dev = vm->dev;
1950
1951 vm->scratch_page = alloc_scratch_page(dev);
1952 if (IS_ERR(vm->scratch_page))
1953 return PTR_ERR(vm->scratch_page);
1954
1955 vm->scratch_pt = alloc_pt(dev);
1956 if (IS_ERR(vm->scratch_pt)) {
1957 free_scratch_page(dev, vm->scratch_page);
1958 return PTR_ERR(vm->scratch_pt);
1959 }
1960
1961 gen6_initialize_pt(vm, vm->scratch_pt);
1962
1963 return 0;
1964}
1965
1966static void gen6_free_scratch(struct i915_address_space *vm)
1967{
1968 struct drm_device *dev = vm->dev;
1969
1970 free_pt(dev, vm->scratch_pt);
1971 free_scratch_page(dev, vm->scratch_page);
1972}
1973
Daniel Vetter061dd492015-04-14 17:35:13 +02001974static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08001975{
Joonas Lahtinene5716f52016-04-07 11:08:03 +03001976 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
Dave Gordon731f74c2016-06-24 19:37:46 +01001977 struct i915_page_directory *pd = &ppgtt->pd;
1978 struct drm_device *dev = vm->dev;
Michel Thierry09942c62015-04-08 12:13:30 +01001979 struct i915_page_table *pt;
1980 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08001981
Daniel Vetter061dd492015-04-14 17:35:13 +02001982 drm_mm_remove_node(&ppgtt->node);
1983
Dave Gordon731f74c2016-06-24 19:37:46 +01001984 gen6_for_all_pdes(pt, pd, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001985 if (pt != vm->scratch_pt)
Dave Gordon731f74c2016-06-24 19:37:46 +01001986 free_pt(dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001987
Mika Kuoppala8776f022015-06-30 18:16:40 +03001988 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08001989}
1990
Ben Widawskyb1465202014-02-19 22:05:49 -08001991static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001992{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001993 struct i915_address_space *vm = &ppgtt->base;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001994 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001995 struct drm_i915_private *dev_priv = to_i915(dev);
1996 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001997 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08001998 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001999
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002000 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2001 * allocator works in address space sizes, so it's multiplied by page
2002 * size. We allocate at the top of the GTT to avoid fragmentation.
2003 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002004 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00002005
Mika Kuoppala8776f022015-06-30 18:16:40 +03002006 ret = gen6_init_scratch(vm);
2007 if (ret)
2008 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002009
Ben Widawskye3cc1992013-12-06 14:11:08 -08002010alloc:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002011 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002012 &ppgtt->node, GEN6_PD_SIZE,
2013 GEN6_PD_ALIGN, 0,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002014 0, ggtt->base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07002015 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002016 if (ret == -ENOSPC && !retried) {
Chris Wilsone522ac22016-08-04 16:32:18 +01002017 ret = i915_gem_evict_something(&ggtt->base,
Ben Widawskye3cc1992013-12-06 14:11:08 -08002018 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02002019 I915_CACHE_NONE,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002020 0, ggtt->base.total,
Chris Wilsond23db882014-05-23 08:48:08 +02002021 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08002022 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002023 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08002024
2025 retried = true;
2026 goto alloc;
2027 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002028
Ben Widawskyc8c26622015-01-22 17:01:25 +00002029 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00002030 goto err_out;
2031
Ben Widawskyc8c26622015-01-22 17:01:25 +00002032
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002033 if (ppgtt->node.start < ggtt->mappable_end)
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002034 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002035
Ben Widawskyc8c26622015-01-22 17:01:25 +00002036 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00002037
2038err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03002039 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002040 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08002041}
2042
Ben Widawskyb1465202014-02-19 22:05:49 -08002043static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2044{
kbuild test robot2f2cf682015-03-27 19:26:35 +08002045 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08002046}
2047
Michel Thierry4933d512015-03-24 15:46:22 +00002048static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2049 uint64_t start, uint64_t length)
2050{
Michel Thierryec565b32015-04-08 12:13:23 +01002051 struct i915_page_table *unused;
Dave Gordon731f74c2016-06-24 19:37:46 +01002052 uint32_t pde;
Michel Thierry4933d512015-03-24 15:46:22 +00002053
Dave Gordon731f74c2016-06-24 19:37:46 +01002054 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03002055 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00002056}
2057
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002058static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08002059{
2060 struct drm_device *dev = ppgtt->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002061 struct drm_i915_private *dev_priv = to_i915(dev);
2062 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskyb1465202014-02-19 22:05:49 -08002063 int ret;
2064
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002065 ppgtt->base.pte_encode = ggtt->base.pte_encode;
Chris Wilson8eb95202016-07-04 08:48:31 +01002066 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev))
Ben Widawsky48a10382013-12-06 14:11:11 -08002067 ppgtt->switch_mm = gen6_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002068 else if (IS_HASWELL(dev))
Ben Widawsky90252e52013-12-06 14:11:12 -08002069 ppgtt->switch_mm = hsw_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002070 else if (IS_GEN7(dev))
Ben Widawsky48a10382013-12-06 14:11:11 -08002071 ppgtt->switch_mm = gen7_mm_switch;
Chris Wilson8eb95202016-07-04 08:48:31 +01002072 else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08002073 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08002074
2075 ret = gen6_ppgtt_alloc(ppgtt);
2076 if (ret)
2077 return ret;
2078
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002079 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002080 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2081 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002082 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2083 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002084 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08002085 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01002086 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08002087 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002088
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002089 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00002090 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002091
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002092 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002093 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00002094
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002095 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002096
Ben Widawsky678d96f2015-03-16 16:00:56 +00002097 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2098
Thierry Reding440fd522015-01-23 09:05:06 +01002099 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08002100 ppgtt->node.size >> 20,
2101 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002102
Daniel Vetterfa76da32014-08-06 20:19:54 +02002103 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03002104 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002105
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002106 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08002107}
2108
Chris Wilson2bfa9962016-08-04 07:52:25 +01002109static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2110 struct drm_i915_private *dev_priv)
Daniel Vetter3440d262013-01-24 13:49:56 -08002111{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002112 ppgtt->base.dev = &dev_priv->drm;
Daniel Vetter3440d262013-01-24 13:49:56 -08002113
Chris Wilson2bfa9962016-08-04 07:52:25 +01002114 if (INTEL_INFO(dev_priv)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002115 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002116 else
Michel Thierryd7b26332015-04-08 12:13:34 +01002117 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002118}
Mika Kuoppalac114f762015-06-25 18:35:13 +03002119
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002120static void i915_address_space_init(struct i915_address_space *vm,
2121 struct drm_i915_private *dev_priv)
2122{
2123 drm_mm_init(&vm->mm, vm->start, vm->total);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002124 INIT_LIST_HEAD(&vm->active_list);
2125 INIT_LIST_HEAD(&vm->inactive_list);
Chris Wilson50e046b2016-08-04 07:52:46 +01002126 INIT_LIST_HEAD(&vm->unbound_list);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002127 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2128}
2129
Tim Gored5165eb2016-02-04 11:49:34 +00002130static void gtt_write_workarounds(struct drm_device *dev)
2131{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002132 struct drm_i915_private *dev_priv = to_i915(dev);
Tim Gored5165eb2016-02-04 11:49:34 +00002133
2134 /* This function is for gtt related workarounds. This function is
2135 * called on driver load and after a GPU reset, so you can place
2136 * workarounds here even if they get overwritten by GPU reset.
2137 */
2138 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
2139 if (IS_BROADWELL(dev))
2140 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
2141 else if (IS_CHERRYVIEW(dev))
2142 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
2143 else if (IS_SKYLAKE(dev))
2144 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
2145 else if (IS_BROXTON(dev))
2146 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2147}
2148
Chris Wilson2bfa9962016-08-04 07:52:25 +01002149static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2150 struct drm_i915_private *dev_priv,
2151 struct drm_i915_file_private *file_priv)
Daniel Vetterfa76da32014-08-06 20:19:54 +02002152{
Chris Wilson2bfa9962016-08-04 07:52:25 +01002153 int ret;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07002154
Chris Wilson2bfa9962016-08-04 07:52:25 +01002155 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002156 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08002157 kref_init(&ppgtt->ref);
Michał Winiarskia2cad9d2015-09-16 11:49:00 +02002158 i915_address_space_init(&ppgtt->base, dev_priv);
Chris Wilson2bfa9962016-08-04 07:52:25 +01002159 ppgtt->base.file = file_priv;
Ben Widawsky93bd8642013-07-16 16:50:06 -07002160 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002161
2162 return ret;
2163}
2164
Daniel Vetter82460d92014-08-06 20:19:53 +02002165int i915_ppgtt_init_hw(struct drm_device *dev)
2166{
Tim Gored5165eb2016-02-04 11:49:34 +00002167 gtt_write_workarounds(dev);
2168
Thomas Daniel671b50132014-08-20 16:24:50 +01002169 /* In the case of execlists, PPGTT is enabled by the context descriptor
2170 * and the PDPs are contained within the context itself. We don't
2171 * need to do anything here. */
2172 if (i915.enable_execlists)
2173 return 0;
2174
Daniel Vetter82460d92014-08-06 20:19:53 +02002175 if (!USES_PPGTT(dev))
2176 return 0;
2177
2178 if (IS_GEN6(dev))
2179 gen6_ppgtt_enable(dev);
2180 else if (IS_GEN7(dev))
2181 gen7_ppgtt_enable(dev);
2182 else if (INTEL_INFO(dev)->gen >= 8)
2183 gen8_ppgtt_enable(dev);
2184 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01002185 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02002186
John Harrison4ad2fd82015-06-18 13:11:20 +01002187 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002188}
John Harrison4ad2fd82015-06-18 13:11:20 +01002189
Daniel Vetter4d884702014-08-06 15:04:47 +02002190struct i915_hw_ppgtt *
Chris Wilson2bfa9962016-08-04 07:52:25 +01002191i915_ppgtt_create(struct drm_i915_private *dev_priv,
2192 struct drm_i915_file_private *fpriv)
Daniel Vetter4d884702014-08-06 15:04:47 +02002193{
2194 struct i915_hw_ppgtt *ppgtt;
2195 int ret;
2196
2197 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2198 if (!ppgtt)
2199 return ERR_PTR(-ENOMEM);
2200
Chris Wilson2bfa9962016-08-04 07:52:25 +01002201 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv);
Daniel Vetter4d884702014-08-06 15:04:47 +02002202 if (ret) {
2203 kfree(ppgtt);
2204 return ERR_PTR(ret);
2205 }
2206
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002207 trace_i915_ppgtt_create(&ppgtt->base);
2208
Daniel Vetter4d884702014-08-06 15:04:47 +02002209 return ppgtt;
2210}
2211
Daniel Vetteree960be2014-08-06 15:04:45 +02002212void i915_ppgtt_release(struct kref *kref)
2213{
2214 struct i915_hw_ppgtt *ppgtt =
2215 container_of(kref, struct i915_hw_ppgtt, ref);
2216
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002217 trace_i915_ppgtt_release(&ppgtt->base);
2218
Chris Wilson50e046b2016-08-04 07:52:46 +01002219 /* vmas should already be unbound and destroyed */
Daniel Vetteree960be2014-08-06 15:04:45 +02002220 WARN_ON(!list_empty(&ppgtt->base.active_list));
2221 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
Chris Wilson50e046b2016-08-04 07:52:46 +01002222 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
Daniel Vetteree960be2014-08-06 15:04:45 +02002223
Daniel Vetter19dd1202014-08-06 15:04:55 +02002224 list_del(&ppgtt->base.global_link);
2225 drm_mm_takedown(&ppgtt->base.mm);
2226
Daniel Vetteree960be2014-08-06 15:04:45 +02002227 ppgtt->base.cleanup(&ppgtt->base);
2228 kfree(ppgtt);
2229}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002230
Ben Widawskya81cc002013-01-18 12:30:31 -08002231/* Certain Gen5 chipsets require require idling the GPU before
2232 * unmapping anything from the GTT when VT-d is enabled.
2233 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002234static bool needs_idle_maps(struct drm_i915_private *dev_priv)
Ben Widawskya81cc002013-01-18 12:30:31 -08002235{
2236#ifdef CONFIG_INTEL_IOMMU
2237 /* Query intel_iommu to see if we need the workaround. Presumably that
2238 * was loaded first.
2239 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002240 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
Ben Widawskya81cc002013-01-18 12:30:31 -08002241 return true;
2242#endif
2243 return false;
2244}
2245
Chris Wilsondc979972016-05-10 14:10:04 +01002246void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
Ben Widawsky828c7902013-10-16 09:21:30 -07002247{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002248 struct intel_engine_cs *engine;
Ben Widawsky828c7902013-10-16 09:21:30 -07002249
Chris Wilsondc979972016-05-10 14:10:04 +01002250 if (INTEL_INFO(dev_priv)->gen < 6)
Ben Widawsky828c7902013-10-16 09:21:30 -07002251 return;
2252
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002253 for_each_engine(engine, dev_priv) {
Ben Widawsky828c7902013-10-16 09:21:30 -07002254 u32 fault_reg;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002255 fault_reg = I915_READ(RING_FAULT_REG(engine));
Ben Widawsky828c7902013-10-16 09:21:30 -07002256 if (fault_reg & RING_FAULT_VALID) {
2257 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002258 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002259 "\tAddress space: %s\n"
2260 "\tSource ID: %d\n"
2261 "\tType: %d\n",
2262 fault_reg & PAGE_MASK,
2263 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2264 RING_FAULT_SRCID(fault_reg),
2265 RING_FAULT_FAULT_TYPE(fault_reg));
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002266 I915_WRITE(RING_FAULT_REG(engine),
Ben Widawsky828c7902013-10-16 09:21:30 -07002267 fault_reg & ~RING_FAULT_VALID);
2268 }
2269 }
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002270 POSTING_READ(RING_FAULT_REG(&dev_priv->engine[RCS]));
Ben Widawsky828c7902013-10-16 09:21:30 -07002271}
2272
Chris Wilson91e56492014-09-25 10:13:12 +01002273static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2274{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002275 if (INTEL_INFO(dev_priv)->gen < 6) {
Chris Wilson91e56492014-09-25 10:13:12 +01002276 intel_gtt_chipset_flush();
2277 } else {
2278 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2279 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2280 }
2281}
2282
Ben Widawsky828c7902013-10-16 09:21:30 -07002283void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2284{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002285 struct drm_i915_private *dev_priv = to_i915(dev);
2286 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky828c7902013-10-16 09:21:30 -07002287
2288 /* Don't bother messing with faults pre GEN6 as we have little
2289 * documentation supporting that it's a good idea.
2290 */
2291 if (INTEL_INFO(dev)->gen < 6)
2292 return;
2293
Chris Wilsondc979972016-05-10 14:10:04 +01002294 i915_check_and_clear_faults(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002295
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002296 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total,
2297 true);
Chris Wilson91e56492014-09-25 10:13:12 +01002298
2299 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002300}
2301
Daniel Vetter74163902012-02-15 23:50:21 +01002302int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002303{
Chris Wilson9da3da62012-06-01 15:20:22 +01002304 if (!dma_map_sg(&obj->base.dev->pdev->dev,
2305 obj->pages->sgl, obj->pages->nents,
2306 PCI_DMA_BIDIRECTIONAL))
2307 return -ENOSPC;
2308
2309 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002310}
2311
Daniel Vetter2c642b02015-04-14 17:35:26 +02002312static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002313{
2314#ifdef writeq
2315 writeq(pte, addr);
2316#else
2317 iowrite32((u32)pte, addr);
2318 iowrite32(pte >> 32, addr + 4);
2319#endif
2320}
2321
Chris Wilsond6473f52016-06-10 14:22:59 +05302322static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2323 dma_addr_t addr,
2324 uint64_t offset,
2325 enum i915_cache_level level,
2326 u32 unused)
2327{
2328 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2329 gen8_pte_t __iomem *pte =
2330 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2331 (offset >> PAGE_SHIFT);
2332 int rpm_atomic_seq;
2333
2334 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2335
2336 gen8_set_pte(pte, gen8_pte_encode(addr, level, true));
2337
2338 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2339 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2340
2341 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2342}
2343
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002344static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2345 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002346 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302347 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002348{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002349 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002350 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002351 struct sgt_iter sgt_iter;
2352 gen8_pte_t __iomem *gtt_entries;
2353 gen8_pte_t gtt_entry;
2354 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002355 int rpm_atomic_seq;
Dave Gordon85d12252016-05-20 11:54:06 +01002356 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002357
2358 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002359
Dave Gordon85d12252016-05-20 11:54:06 +01002360 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2361
2362 for_each_sgt_dma(addr, sgt_iter, st) {
2363 gtt_entry = gen8_pte_encode(addr, level, true);
2364 gen8_set_pte(&gtt_entries[i++], gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002365 }
2366
2367 /*
2368 * XXX: This serves as a posting read to make sure that the PTE has
2369 * actually been updated. There is some concern that even though
2370 * registers and PTEs are within the same BAR that they are potentially
2371 * of NUMA access patterns. Therefore, even with the way we assume
2372 * hardware should work, we must keep this posting read for paranoia.
2373 */
2374 if (i != 0)
Dave Gordon85d12252016-05-20 11:54:06 +01002375 WARN_ON(readq(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002376
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002377 /* This next bit makes the above posting read even more important. We
2378 * want to flush the TLBs only after we're certain all the PTE updates
2379 * have finished.
2380 */
2381 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2382 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Imre Deakbe694592015-12-15 20:10:38 +02002383
2384 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002385}
2386
Chris Wilsonc1403302015-11-18 15:19:39 +00002387struct insert_entries {
2388 struct i915_address_space *vm;
2389 struct sg_table *st;
2390 uint64_t start;
2391 enum i915_cache_level level;
2392 u32 flags;
2393};
2394
2395static int gen8_ggtt_insert_entries__cb(void *_arg)
2396{
2397 struct insert_entries *arg = _arg;
2398 gen8_ggtt_insert_entries(arg->vm, arg->st,
2399 arg->start, arg->level, arg->flags);
2400 return 0;
2401}
2402
2403static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2404 struct sg_table *st,
2405 uint64_t start,
2406 enum i915_cache_level level,
2407 u32 flags)
2408{
2409 struct insert_entries arg = { vm, st, start, level, flags };
2410 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2411}
2412
Chris Wilsond6473f52016-06-10 14:22:59 +05302413static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2414 dma_addr_t addr,
2415 uint64_t offset,
2416 enum i915_cache_level level,
2417 u32 flags)
2418{
2419 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2420 gen6_pte_t __iomem *pte =
2421 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2422 (offset >> PAGE_SHIFT);
2423 int rpm_atomic_seq;
2424
2425 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2426
2427 iowrite32(vm->pte_encode(addr, level, true, flags), pte);
2428
2429 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2430 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2431
2432 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2433}
2434
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002435/*
2436 * Binds an object into the global gtt with the specified cache level. The object
2437 * will be accessible to the GPU via commands whose operands reference offsets
2438 * within the global GTT as well as accessible by the GPU through the GMADR
2439 * mapped BAR (dev_priv->mm.gtt->gtt).
2440 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002441static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002442 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002443 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302444 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002445{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002446 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002447 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Dave Gordon85d12252016-05-20 11:54:06 +01002448 struct sgt_iter sgt_iter;
2449 gen6_pte_t __iomem *gtt_entries;
2450 gen6_pte_t gtt_entry;
2451 dma_addr_t addr;
Imre Deakbe694592015-12-15 20:10:38 +02002452 int rpm_atomic_seq;
Dave Gordon85d12252016-05-20 11:54:06 +01002453 int i = 0;
Imre Deakbe694592015-12-15 20:10:38 +02002454
2455 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002456
Dave Gordon85d12252016-05-20 11:54:06 +01002457 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2458
2459 for_each_sgt_dma(addr, sgt_iter, st) {
2460 gtt_entry = vm->pte_encode(addr, level, true, flags);
2461 iowrite32(gtt_entry, &gtt_entries[i++]);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002462 }
2463
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002464 /* XXX: This serves as a posting read to make sure that the PTE has
2465 * actually been updated. There is some concern that even though
2466 * registers and PTEs are within the same BAR that they are potentially
2467 * of NUMA access patterns. Therefore, even with the way we assume
2468 * hardware should work, we must keep this posting read for paranoia.
2469 */
Dave Gordon85d12252016-05-20 11:54:06 +01002470 if (i != 0)
2471 WARN_ON(readl(&gtt_entries[i-1]) != gtt_entry);
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002472
2473 /* This next bit makes the above posting read even more important. We
2474 * want to flush the TLBs only after we're certain all the PTE updates
2475 * have finished.
2476 */
2477 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2478 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Imre Deakbe694592015-12-15 20:10:38 +02002479
2480 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002481}
2482
Chris Wilsonf7770bf2016-05-14 07:26:35 +01002483static void nop_clear_range(struct i915_address_space *vm,
2484 uint64_t start,
2485 uint64_t length,
2486 bool use_scratch)
2487{
2488}
2489
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002490static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002491 uint64_t start,
2492 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002493 bool use_scratch)
2494{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002495 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002496 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002497 unsigned first_entry = start >> PAGE_SHIFT;
2498 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002499 gen8_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002500 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2501 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002502 int i;
Imre Deakbe694592015-12-15 20:10:38 +02002503 int rpm_atomic_seq;
2504
2505 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002506
2507 if (WARN(num_entries > max_entries,
2508 "First entry = %d; Num entries = %d (max=%d)\n",
2509 first_entry, num_entries, max_entries))
2510 num_entries = max_entries;
2511
Mika Kuoppalac114f762015-06-25 18:35:13 +03002512 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002513 I915_CACHE_LLC,
2514 use_scratch);
2515 for (i = 0; i < num_entries; i++)
2516 gen8_set_pte(&gtt_base[i], scratch_pte);
2517 readl(gtt_base);
Imre Deakbe694592015-12-15 20:10:38 +02002518
2519 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002520}
2521
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002522static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002523 uint64_t start,
2524 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07002525 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002526{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002527 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Chris Wilsonce7fda22016-04-28 09:56:38 +01002528 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
Ben Widawsky782f1492014-02-20 11:50:33 -08002529 unsigned first_entry = start >> PAGE_SHIFT;
2530 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002531 gen6_pte_t scratch_pte, __iomem *gtt_base =
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002532 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2533 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002534 int i;
Imre Deakbe694592015-12-15 20:10:38 +02002535 int rpm_atomic_seq;
2536
2537 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002538
2539 if (WARN(num_entries > max_entries,
2540 "First entry = %d; Num entries = %d (max=%d)\n",
2541 first_entry, num_entries, max_entries))
2542 num_entries = max_entries;
2543
Mika Kuoppalac114f762015-06-25 18:35:13 +03002544 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
2545 I915_CACHE_LLC, use_scratch, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002546
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002547 for (i = 0; i < num_entries; i++)
2548 iowrite32(scratch_pte, &gtt_base[i]);
2549 readl(gtt_base);
Imre Deakbe694592015-12-15 20:10:38 +02002550
2551 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002552}
2553
Chris Wilsond6473f52016-06-10 14:22:59 +05302554static void i915_ggtt_insert_page(struct i915_address_space *vm,
2555 dma_addr_t addr,
2556 uint64_t offset,
2557 enum i915_cache_level cache_level,
2558 u32 unused)
2559{
2560 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2561 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2562 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2563 int rpm_atomic_seq;
2564
2565 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2566
2567 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
2568
2569 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2570}
2571
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002572static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2573 struct sg_table *pages,
2574 uint64_t start,
2575 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002576{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002577 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002578 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2579 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
Imre Deakbe694592015-12-15 20:10:38 +02002580 int rpm_atomic_seq;
2581
2582 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002583
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002584 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002585
Imre Deakbe694592015-12-15 20:10:38 +02002586 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2587
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002588}
2589
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002590static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002591 uint64_t start,
2592 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07002593 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002594{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002595 struct drm_i915_private *dev_priv = to_i915(vm->dev);
Ben Widawsky782f1492014-02-20 11:50:33 -08002596 unsigned first_entry = start >> PAGE_SHIFT;
2597 unsigned num_entries = length >> PAGE_SHIFT;
Imre Deakbe694592015-12-15 20:10:38 +02002598 int rpm_atomic_seq;
2599
2600 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2601
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002602 intel_gtt_clear_range(first_entry, num_entries);
Imre Deakbe694592015-12-15 20:10:38 +02002603
2604 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002605}
2606
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002607static int ggtt_bind_vma(struct i915_vma *vma,
2608 enum i915_cache_level cache_level,
2609 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002610{
Daniel Vetter0a878712015-10-15 14:23:01 +02002611 struct drm_i915_gem_object *obj = vma->obj;
2612 u32 pte_flags = 0;
2613 int ret;
2614
2615 ret = i915_get_ggtt_vma_pages(vma);
2616 if (ret)
2617 return ret;
2618
2619 /* Currently applicable only to VLV */
2620 if (obj->gt_ro)
2621 pte_flags |= PTE_READ_ONLY;
2622
Chris Wilson247177d2016-08-15 10:48:47 +01002623 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
Daniel Vetter0a878712015-10-15 14:23:01 +02002624 cache_level, pte_flags);
2625
2626 /*
2627 * Without aliasing PPGTT there's no difference between
2628 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2629 * upgrade to both bound if we bind either to avoid double-binding.
2630 */
Chris Wilson3272db52016-08-04 16:32:32 +01002631 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
Daniel Vetter0a878712015-10-15 14:23:01 +02002632
2633 return 0;
2634}
2635
2636static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2637 enum i915_cache_level cache_level,
2638 u32 flags)
2639{
Chris Wilson321d1782015-11-20 10:27:18 +00002640 u32 pte_flags;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002641 int ret;
2642
2643 ret = i915_get_ggtt_vma_pages(vma);
2644 if (ret)
2645 return ret;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002646
Akash Goel24f3a8c2014-06-17 10:59:42 +05302647 /* Currently applicable only to VLV */
Chris Wilson321d1782015-11-20 10:27:18 +00002648 pte_flags = 0;
2649 if (vma->obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002650 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302651
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002652
Chris Wilson3272db52016-08-04 16:32:32 +01002653 if (flags & I915_VMA_GLOBAL_BIND) {
Chris Wilson321d1782015-11-20 10:27:18 +00002654 vma->vm->insert_entries(vma->vm,
Chris Wilson247177d2016-08-15 10:48:47 +01002655 vma->pages, vma->node.start,
Daniel Vetter08755462015-04-20 09:04:05 -07002656 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002657 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002658
Chris Wilson3272db52016-08-04 16:32:32 +01002659 if (flags & I915_VMA_LOCAL_BIND) {
Chris Wilson321d1782015-11-20 10:27:18 +00002660 struct i915_hw_ppgtt *appgtt =
2661 to_i915(vma->vm->dev)->mm.aliasing_ppgtt;
2662 appgtt->base.insert_entries(&appgtt->base,
Chris Wilson247177d2016-08-15 10:48:47 +01002663 vma->pages, vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002664 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002665 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002666
2667 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002668}
2669
2670static void ggtt_unbind_vma(struct i915_vma *vma)
2671{
Chris Wilsonde180032016-08-04 16:32:29 +01002672 struct i915_hw_ppgtt *appgtt = to_i915(vma->vm->dev)->mm.aliasing_ppgtt;
2673 const u64 size = min(vma->size, vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002674
Chris Wilson3272db52016-08-04 16:32:32 +01002675 if (vma->flags & I915_VMA_GLOBAL_BIND)
Ben Widawsky782f1492014-02-20 11:50:33 -08002676 vma->vm->clear_range(vma->vm,
Chris Wilsonde180032016-08-04 16:32:29 +01002677 vma->node.start, size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08002678 true);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002679
Chris Wilson3272db52016-08-04 16:32:32 +01002680 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002681 appgtt->base.clear_range(&appgtt->base,
Chris Wilsonde180032016-08-04 16:32:29 +01002682 vma->node.start, size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08002683 true);
Daniel Vetter74163902012-02-15 23:50:21 +01002684}
2685
2686void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
2687{
Ben Widawsky5c042282011-10-17 15:51:55 -07002688 struct drm_device *dev = obj->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002689 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson307dc252016-08-05 10:14:12 +01002690 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky5c042282011-10-17 15:51:55 -07002691
Chris Wilson307dc252016-08-05 10:14:12 +01002692 if (unlikely(ggtt->do_idle_maps)) {
2693 if (i915_gem_wait_for_idle(dev_priv, false)) {
2694 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2695 /* Wait a bit, in hopes it avoids the hang */
2696 udelay(10);
2697 }
2698 }
Ben Widawsky5c042282011-10-17 15:51:55 -07002699
Imre Deak5ec5b512015-07-08 19:18:59 +03002700 dma_unmap_sg(&dev->pdev->dev, obj->pages->sgl, obj->pages->nents,
2701 PCI_DMA_BIDIRECTIONAL);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002702}
Daniel Vetter644ec022012-03-26 09:45:40 +02002703
Chris Wilson42d6ab42012-07-26 11:49:32 +01002704static void i915_gtt_color_adjust(struct drm_mm_node *node,
2705 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002706 u64 *start,
2707 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002708{
2709 if (node->color != color)
2710 *start += 4096;
2711
Chris Wilson2a1d7752016-07-26 12:01:51 +01002712 node = list_first_entry_or_null(&node->node_list,
2713 struct drm_mm_node,
2714 node_list);
2715 if (node && node->allocated && node->color != color)
2716 *end -= 4096;
Chris Wilson42d6ab42012-07-26 11:49:32 +01002717}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002718
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002719int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
Daniel Vetter644ec022012-03-26 09:45:40 +02002720{
Ben Widawskye78891c2013-01-25 16:41:04 -08002721 /* Let GEM Manage all of the aperture.
2722 *
2723 * However, leave one page at the end still bound to the scratch page.
2724 * There are a number of places where the hardware apparently prefetches
2725 * past the end of the object, and we've seen multiple hangs with the
2726 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2727 * aperture. One page should be enough to keep any prefetching inside
2728 * of the aperture.
2729 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002730 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002731 unsigned long hole_start, hole_end;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002732 struct drm_mm_node *entry;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002733 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002734
Zhi Wangb02d22a2016-06-16 08:06:59 -04002735 ret = intel_vgt_balloon(dev_priv);
2736 if (ret)
2737 return ret;
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002738
Chris Wilsoned2f3452012-11-15 11:32:19 +00002739 /* Clear any non-preallocated blocks */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002740 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002741 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2742 hole_start, hole_end);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002743 ggtt->base.clear_range(&ggtt->base, hole_start,
Ben Widawsky782f1492014-02-20 11:50:33 -08002744 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002745 }
2746
2747 /* And finally clear the reserved guard page */
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002748 ggtt->base.clear_range(&ggtt->base,
2749 ggtt->base.total - PAGE_SIZE, PAGE_SIZE,
2750 true);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002751
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002752 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
Daniel Vetterfa76da32014-08-06 20:19:54 +02002753 struct i915_hw_ppgtt *ppgtt;
2754
2755 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2756 if (!ppgtt)
2757 return -ENOMEM;
2758
Chris Wilson2bfa9962016-08-04 07:52:25 +01002759 ret = __hw_ppgtt_init(ppgtt, dev_priv);
Michel Thierry4933d512015-03-24 15:46:22 +00002760 if (ret) {
2761 kfree(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002762 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002763 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002764
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002765 if (ppgtt->base.allocate_va_range)
2766 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2767 ppgtt->base.total);
2768 if (ret) {
2769 ppgtt->base.cleanup(&ppgtt->base);
2770 kfree(ppgtt);
2771 return ret;
2772 }
2773
2774 ppgtt->base.clear_range(&ppgtt->base,
2775 ppgtt->base.start,
2776 ppgtt->base.total,
2777 true);
2778
Daniel Vetterfa76da32014-08-06 20:19:54 +02002779 dev_priv->mm.aliasing_ppgtt = ppgtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002780 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2781 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002782 }
2783
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002784 return 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002785}
2786
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002787/**
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002788 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002789 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02002790 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002791void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002792{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002793 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002794
Daniel Vetter70e32542014-08-06 15:04:57 +02002795 if (dev_priv->mm.aliasing_ppgtt) {
2796 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2797
2798 ppgtt->base.cleanup(&ppgtt->base);
Matthew Auldcb7f2762016-08-05 19:04:40 +01002799 kfree(ppgtt);
Daniel Vetter70e32542014-08-06 15:04:57 +02002800 }
2801
Chris Wilson97d6d7a2016-08-04 07:52:22 +01002802 i915_gem_cleanup_stolen(&dev_priv->drm);
Imre Deaka4eba472016-01-19 15:26:32 +02002803
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002804 if (drm_mm_initialized(&ggtt->base.mm)) {
Zhi Wangb02d22a2016-06-16 08:06:59 -04002805 intel_vgt_deballoon(dev_priv);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002806
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002807 drm_mm_takedown(&ggtt->base.mm);
2808 list_del(&ggtt->base.global_link);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002809 }
2810
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002811 ggtt->base.cleanup(&ggtt->base);
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01002812
2813 arch_phys_wc_del(ggtt->mtrr);
2814 io_mapping_free(ggtt->mappable);
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002815}
Daniel Vetter70e32542014-08-06 15:04:57 +02002816
Daniel Vetter2c642b02015-04-14 17:35:26 +02002817static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002818{
2819 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2820 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2821 return snb_gmch_ctl << 20;
2822}
2823
Daniel Vetter2c642b02015-04-14 17:35:26 +02002824static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002825{
2826 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2827 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2828 if (bdw_gmch_ctl)
2829 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002830
2831#ifdef CONFIG_X86_32
2832 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2833 if (bdw_gmch_ctl > 4)
2834 bdw_gmch_ctl = 4;
2835#endif
2836
Ben Widawsky9459d252013-11-03 16:53:55 -08002837 return bdw_gmch_ctl << 20;
2838}
2839
Daniel Vetter2c642b02015-04-14 17:35:26 +02002840static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002841{
2842 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2843 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2844
2845 if (gmch_ctrl)
2846 return 1 << (20 + gmch_ctrl);
2847
2848 return 0;
2849}
2850
Daniel Vetter2c642b02015-04-14 17:35:26 +02002851static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002852{
2853 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2854 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2855 return snb_gmch_ctl << 25; /* 32 MB units */
2856}
2857
Daniel Vetter2c642b02015-04-14 17:35:26 +02002858static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002859{
2860 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2861 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2862 return bdw_gmch_ctl << 25; /* 32 MB units */
2863}
2864
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002865static size_t chv_get_stolen_size(u16 gmch_ctrl)
2866{
2867 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2868 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2869
2870 /*
2871 * 0x0 to 0x10: 32MB increments starting at 0MB
2872 * 0x11 to 0x16: 4MB increments starting at 8MB
2873 * 0x17 to 0x1d: 4MB increments start at 36MB
2874 */
2875 if (gmch_ctrl < 0x11)
2876 return gmch_ctrl << 25;
2877 else if (gmch_ctrl < 0x17)
2878 return (gmch_ctrl - 0x11 + 2) << 22;
2879 else
2880 return (gmch_ctrl - 0x17 + 9) << 22;
2881}
2882
Damien Lespiau66375012014-01-09 18:02:46 +00002883static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2884{
2885 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2886 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2887
2888 if (gen9_gmch_ctl < 0xf0)
2889 return gen9_gmch_ctl << 25; /* 32 MB units */
2890 else
2891 /* 4MB increments starting at 0xf0 for 4MB */
2892 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2893}
2894
Chris Wilson34c998b2016-08-04 07:52:24 +01002895static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
Ben Widawsky63340132013-11-04 19:32:22 -08002896{
Chris Wilson34c998b2016-08-04 07:52:24 +01002897 struct pci_dev *pdev = ggtt->base.dev->pdev;
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002898 struct i915_page_scratch *scratch_page;
Chris Wilson34c998b2016-08-04 07:52:24 +01002899 phys_addr_t phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08002900
2901 /* For Modern GENs the PTEs and register space are split in the BAR */
Chris Wilson34c998b2016-08-04 07:52:24 +01002902 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
Ben Widawsky63340132013-11-04 19:32:22 -08002903
Imre Deak2a073f892015-03-27 13:07:33 +02002904 /*
2905 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2906 * dropped. For WC mappings in general we have 64 byte burst writes
2907 * when the WC buffer is flushed, so we can't use it, but have to
2908 * resort to an uncached mapping. The WC issue is easily caught by the
2909 * readback check when writing GTT PTE entries.
2910 */
Chris Wilson34c998b2016-08-04 07:52:24 +01002911 if (IS_BROXTON(ggtt->base.dev))
2912 ggtt->gsm = ioremap_nocache(phys_addr, size);
Imre Deak2a073f892015-03-27 13:07:33 +02002913 else
Chris Wilson34c998b2016-08-04 07:52:24 +01002914 ggtt->gsm = ioremap_wc(phys_addr, size);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002915 if (!ggtt->gsm) {
Chris Wilson34c998b2016-08-04 07:52:24 +01002916 DRM_ERROR("Failed to map the ggtt page table\n");
Ben Widawsky63340132013-11-04 19:32:22 -08002917 return -ENOMEM;
2918 }
2919
Chris Wilson34c998b2016-08-04 07:52:24 +01002920 scratch_page = alloc_scratch_page(ggtt->base.dev);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002921 if (IS_ERR(scratch_page)) {
Ben Widawsky63340132013-11-04 19:32:22 -08002922 DRM_ERROR("Scratch setup failed\n");
2923 /* iounmap will also get called at remove, but meh */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002924 iounmap(ggtt->gsm);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002925 return PTR_ERR(scratch_page);
Ben Widawsky63340132013-11-04 19:32:22 -08002926 }
2927
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03002928 ggtt->base.scratch_page = scratch_page;
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002929
2930 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002931}
2932
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002933/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2934 * bits. When using advanced contexts each context stores its own PAT, but
2935 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002936static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002937{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002938 uint64_t pat;
2939
2940 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2941 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2942 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2943 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2944 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2945 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2946 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2947 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2948
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03002949 if (!USES_PPGTT(dev_priv))
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002950 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2951 * so RTL will always use the value corresponding to
2952 * pat_sel = 000".
2953 * So let's disable cache for GGTT to avoid screen corruptions.
2954 * MOCS still can be used though.
2955 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2956 * before this patch, i.e. the same uncached + snooping access
2957 * like on gen6/7 seems to be in effect.
2958 * - So this just fixes blitter/render access. Again it looks
2959 * like it's not just uncached access, but uncached + snooping.
2960 * So we can still hold onto all our assumptions wrt cpu
2961 * clflushing on LLC machines.
2962 */
2963 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2964
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002965 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2966 * write would work. */
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03002967 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
2968 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002969}
2970
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002971static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2972{
2973 uint64_t pat;
2974
2975 /*
2976 * Map WB on BDW to snooped on CHV.
2977 *
2978 * Only the snoop bit has meaning for CHV, the rest is
2979 * ignored.
2980 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002981 * The hardware will never snoop for certain types of accesses:
2982 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2983 * - PPGTT page tables
2984 * - some other special cycles
2985 *
2986 * As with BDW, we also need to consider the following for GT accesses:
2987 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2988 * so RTL will always use the value corresponding to
2989 * pat_sel = 000".
2990 * Which means we must set the snoop bit in PAT entry 0
2991 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002992 */
2993 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2994 GEN8_PPAT(1, 0) |
2995 GEN8_PPAT(2, 0) |
2996 GEN8_PPAT(3, 0) |
2997 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2998 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2999 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3000 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3001
Ville Syrjälä7e435ad2015-09-18 20:03:25 +03003002 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3003 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003004}
3005
Chris Wilson34c998b2016-08-04 07:52:24 +01003006static void gen6_gmch_remove(struct i915_address_space *vm)
3007{
3008 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3009
3010 iounmap(ggtt->gsm);
3011 free_scratch_page(vm->dev, vm->scratch_page);
3012}
3013
Joonas Lahtinend507d732016-03-18 10:42:58 +02003014static int gen8_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawsky63340132013-11-04 19:32:22 -08003015{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003016 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3017 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003018 unsigned int size;
Ben Widawsky63340132013-11-04 19:32:22 -08003019 u16 snb_gmch_ctl;
Ben Widawsky63340132013-11-04 19:32:22 -08003020
3021 /* TODO: We're not aware of mappable constraints on gen8 yet */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003022 ggtt->mappable_base = pci_resource_start(pdev, 2);
3023 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky63340132013-11-04 19:32:22 -08003024
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003025 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3026 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
Ben Widawsky63340132013-11-04 19:32:22 -08003027
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003028 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawsky63340132013-11-04 19:32:22 -08003029
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003030 if (INTEL_GEN(dev_priv) >= 9) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003031 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003032 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003033 } else if (IS_CHERRYVIEW(dev_priv)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003034 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003035 size = chv_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003036 } else {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003037 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
Chris Wilson34c998b2016-08-04 07:52:24 +01003038 size = gen8_get_total_gtt_size(snb_gmch_ctl);
Damien Lespiaud7f25f22014-05-08 22:19:40 +03003039 }
Ben Widawsky63340132013-11-04 19:32:22 -08003040
Chris Wilson34c998b2016-08-04 07:52:24 +01003041 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08003042
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003043 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03003044 chv_setup_private_ppat(dev_priv);
3045 else
3046 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08003047
Chris Wilson34c998b2016-08-04 07:52:24 +01003048 ggtt->base.cleanup = gen6_gmch_remove;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003049 ggtt->base.bind_vma = ggtt_bind_vma;
3050 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilsond6473f52016-06-10 14:22:59 +05303051 ggtt->base.insert_page = gen8_ggtt_insert_page;
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003052 ggtt->base.clear_range = nop_clear_range;
Chris Wilson48f112f2016-06-24 14:07:14 +01003053 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
Chris Wilsonf7770bf2016-05-14 07:26:35 +01003054 ggtt->base.clear_range = gen8_ggtt_clear_range;
3055
3056 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3057 if (IS_CHERRYVIEW(dev_priv))
3058 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3059
Chris Wilson34c998b2016-08-04 07:52:24 +01003060 return ggtt_probe_common(ggtt, size);
Ben Widawsky63340132013-11-04 19:32:22 -08003061}
3062
Joonas Lahtinend507d732016-03-18 10:42:58 +02003063static int gen6_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003064{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003065 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3066 struct pci_dev *pdev = dev_priv->drm.pdev;
Chris Wilson34c998b2016-08-04 07:52:24 +01003067 unsigned int size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003068 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003069
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003070 ggtt->mappable_base = pci_resource_start(pdev, 2);
3071 ggtt->mappable_end = pci_resource_len(pdev, 2);
Ben Widawsky41907dd2013-02-08 11:32:47 -08003072
Ben Widawskybaa09f52013-01-24 13:49:57 -08003073 /* 64/512MB is the current min/max we actually know of, but this is just
3074 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003075 */
Chris Wilson34c998b2016-08-04 07:52:24 +01003076 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
Joonas Lahtinend507d732016-03-18 10:42:58 +02003077 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003078 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003079 }
3080
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003081 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3082 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3083 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003084
Joonas Lahtinend507d732016-03-18 10:42:58 +02003085 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003086
Chris Wilson34c998b2016-08-04 07:52:24 +01003087 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3088 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003089
Joonas Lahtinend507d732016-03-18 10:42:58 +02003090 ggtt->base.clear_range = gen6_ggtt_clear_range;
Chris Wilsond6473f52016-06-10 14:22:59 +05303091 ggtt->base.insert_page = gen6_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003092 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3093 ggtt->base.bind_vma = ggtt_bind_vma;
3094 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003095 ggtt->base.cleanup = gen6_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003096
Chris Wilson34c998b2016-08-04 07:52:24 +01003097 if (HAS_EDRAM(dev_priv))
3098 ggtt->base.pte_encode = iris_pte_encode;
3099 else if (IS_HASWELL(dev_priv))
3100 ggtt->base.pte_encode = hsw_pte_encode;
3101 else if (IS_VALLEYVIEW(dev_priv))
3102 ggtt->base.pte_encode = byt_pte_encode;
3103 else if (INTEL_GEN(dev_priv) >= 7)
3104 ggtt->base.pte_encode = ivb_pte_encode;
3105 else
3106 ggtt->base.pte_encode = snb_pte_encode;
3107
3108 return ggtt_probe_common(ggtt, size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003109}
3110
Chris Wilson34c998b2016-08-04 07:52:24 +01003111static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003112{
Chris Wilson34c998b2016-08-04 07:52:24 +01003113 intel_gmch_remove();
Ben Widawskybaa09f52013-01-24 13:49:57 -08003114}
3115
Joonas Lahtinend507d732016-03-18 10:42:58 +02003116static int i915_gmch_probe(struct i915_ggtt *ggtt)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003117{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003118 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003119 int ret;
3120
Chris Wilson91c8a322016-07-05 10:40:23 +01003121 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003122 if (!ret) {
3123 DRM_ERROR("failed to set up gmch\n");
3124 return -EIO;
3125 }
3126
Joonas Lahtinend507d732016-03-18 10:42:58 +02003127 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3128 &ggtt->mappable_base, &ggtt->mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08003129
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003130 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
Chris Wilsond6473f52016-06-10 14:22:59 +05303131 ggtt->base.insert_page = i915_ggtt_insert_page;
Joonas Lahtinend507d732016-03-18 10:42:58 +02003132 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3133 ggtt->base.clear_range = i915_ggtt_clear_range;
3134 ggtt->base.bind_vma = ggtt_bind_vma;
3135 ggtt->base.unbind_vma = ggtt_unbind_vma;
Chris Wilson34c998b2016-08-04 07:52:24 +01003136 ggtt->base.cleanup = i915_gmch_remove;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003137
Joonas Lahtinend507d732016-03-18 10:42:58 +02003138 if (unlikely(ggtt->do_idle_maps))
Chris Wilsonc0a7f812013-12-30 12:16:15 +00003139 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3140
Ben Widawskybaa09f52013-01-24 13:49:57 -08003141 return 0;
3142}
3143
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003144/**
Chris Wilson0088e522016-08-04 07:52:21 +01003145 * i915_ggtt_probe_hw - Probe GGTT hardware location
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003146 * @dev_priv: i915 device
Joonas Lahtinend85489d2016-03-24 16:47:46 +02003147 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003148int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003149{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003150 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08003151 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003152
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003153 ggtt->base.dev = &dev_priv->drm;
Mika Kuoppalac114f762015-06-25 18:35:13 +03003154
Chris Wilson34c998b2016-08-04 07:52:24 +01003155 if (INTEL_GEN(dev_priv) <= 5)
3156 ret = i915_gmch_probe(ggtt);
3157 else if (INTEL_GEN(dev_priv) < 8)
3158 ret = gen6_gmch_probe(ggtt);
3159 else
3160 ret = gen8_gmch_probe(ggtt);
Ben Widawskya54c0c22013-01-24 14:45:00 -08003161 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08003162 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003163
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003164 if ((ggtt->base.total - 1) >> 32) {
3165 DRM_ERROR("We never expected a Global GTT with more than 32bits"
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003166 " of address space! Found %lldM!\n",
Chris Wilsonc890e2d2016-03-18 10:42:59 +02003167 ggtt->base.total >> 20);
3168 ggtt->base.total = 1ULL << 32;
3169 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3170 }
3171
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003172 if (ggtt->mappable_end > ggtt->base.total) {
3173 DRM_ERROR("mappable aperture extends past end of GGTT,"
3174 " aperture=%llx, total=%llx\n",
3175 ggtt->mappable_end, ggtt->base.total);
3176 ggtt->mappable_end = ggtt->base.total;
3177 }
3178
Ben Widawskybaa09f52013-01-24 13:49:57 -08003179 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03003180 DRM_INFO("Memory usable by graphics device = %lluM\n",
Joonas Lahtinen62106b42016-03-18 10:42:57 +02003181 ggtt->base.total >> 20);
3182 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3183 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02003184#ifdef CONFIG_INTEL_IOMMU
3185 if (intel_iommu_gfx_mapped)
3186 DRM_INFO("VT-d active for gfx access\n");
3187#endif
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08003188
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003189 return 0;
Chris Wilson0088e522016-08-04 07:52:21 +01003190}
3191
3192/**
3193 * i915_ggtt_init_hw - Initialize GGTT hardware
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003194 * @dev_priv: i915 device
Chris Wilson0088e522016-08-04 07:52:21 +01003195 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003196int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
Chris Wilson0088e522016-08-04 07:52:21 +01003197{
Chris Wilson0088e522016-08-04 07:52:21 +01003198 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3199 int ret;
3200
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01003201 INIT_LIST_HEAD(&dev_priv->vm_list);
3202
3203 /* Subtract the guard page before address space initialization to
3204 * shrink the range used by drm_mm.
3205 */
3206 ggtt->base.total -= PAGE_SIZE;
3207 i915_address_space_init(&ggtt->base, dev_priv);
3208 ggtt->base.total += PAGE_SIZE;
3209 if (!HAS_LLC(dev_priv))
3210 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
3211
3212 ggtt->mappable =
3213 io_mapping_create_wc(ggtt->mappable_base, ggtt->mappable_end);
3214 if (!ggtt->mappable) {
3215 ret = -EIO;
3216 goto out_gtt_cleanup;
3217 }
3218
3219 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3220
Chris Wilson0088e522016-08-04 07:52:21 +01003221 /*
3222 * Initialise stolen early so that we may reserve preallocated
3223 * objects for the BIOS to KMS transition.
3224 */
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003225 ret = i915_gem_init_stolen(&dev_priv->drm);
Chris Wilson0088e522016-08-04 07:52:21 +01003226 if (ret)
3227 goto out_gtt_cleanup;
3228
3229 return 0;
Imre Deaka4eba472016-01-19 15:26:32 +02003230
3231out_gtt_cleanup:
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003232 ggtt->base.cleanup(&ggtt->base);
Imre Deaka4eba472016-01-19 15:26:32 +02003233 return ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02003234}
Ben Widawsky6f65e292013-12-06 14:10:56 -08003235
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003236int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003237{
Chris Wilson97d6d7a2016-08-04 07:52:22 +01003238 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
Ville Syrjäläac840ae2016-05-06 21:35:55 +03003239 return -EIO;
3240
3241 return 0;
3242}
3243
Daniel Vetterfa423312015-04-14 17:35:23 +02003244void i915_gem_restore_gtt_mappings(struct drm_device *dev)
3245{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003246 struct drm_i915_private *dev_priv = to_i915(dev);
3247 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003248 struct drm_i915_gem_object *obj;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003249 struct i915_vma *vma;
Daniel Vetterfa423312015-04-14 17:35:23 +02003250
Chris Wilsondc979972016-05-10 14:10:04 +01003251 i915_check_and_clear_faults(dev_priv);
Daniel Vetterfa423312015-04-14 17:35:23 +02003252
3253 /* First fill our portion of the GTT with scratch pages */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003254 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total,
3255 true);
Daniel Vetterfa423312015-04-14 17:35:23 +02003256
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003257 /* Cache flush objects bound into GGTT and rebind them. */
Daniel Vetterfa423312015-04-14 17:35:23 +02003258 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003259 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003260 if (vma->vm != &ggtt->base)
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003261 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02003262
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003263 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3264 PIN_UPDATE));
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01003265 }
3266
Chris Wilson975f7ff2016-05-14 07:26:34 +01003267 if (obj->pin_display)
3268 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
Daniel Vetterfa423312015-04-14 17:35:23 +02003269 }
3270
Daniel Vetterfa423312015-04-14 17:35:23 +02003271 if (INTEL_INFO(dev)->gen >= 8) {
3272 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
3273 chv_setup_private_ppat(dev_priv);
3274 else
3275 bdw_setup_private_ppat(dev_priv);
3276
3277 return;
3278 }
3279
3280 if (USES_PPGTT(dev)) {
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003281 struct i915_address_space *vm;
3282
Daniel Vetterfa423312015-04-14 17:35:23 +02003283 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3284 /* TODO: Perhaps it shouldn't be gen6 specific */
3285
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003286 struct i915_hw_ppgtt *ppgtt;
Daniel Vetterfa423312015-04-14 17:35:23 +02003287
Chris Wilson2bfa9962016-08-04 07:52:25 +01003288 if (i915_is_ggtt(vm))
Daniel Vetterfa423312015-04-14 17:35:23 +02003289 ppgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinene5716f52016-04-07 11:08:03 +03003290 else
3291 ppgtt = i915_vm_to_ppgtt(vm);
Daniel Vetterfa423312015-04-14 17:35:23 +02003292
3293 gen6_write_page_range(dev_priv, &ppgtt->pd,
3294 0, ppgtt->base.total);
3295 }
3296 }
3297
3298 i915_ggtt_flush(dev_priv);
3299}
3300
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003301static void
3302i915_vma_retire(struct i915_gem_active *active,
3303 struct drm_i915_gem_request *rq)
3304{
3305 const unsigned int idx = rq->engine->id;
3306 struct i915_vma *vma =
3307 container_of(active, struct i915_vma, last_read[idx]);
3308
3309 GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx));
3310
3311 i915_vma_clear_active(vma, idx);
3312 if (i915_vma_is_active(vma))
3313 return;
3314
3315 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Chris Wilson3272db52016-08-04 16:32:32 +01003316 if (unlikely(i915_vma_is_closed(vma) && !i915_vma_is_pinned(vma)))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003317 WARN_ON(i915_vma_unbind(vma));
3318}
3319
3320void i915_vma_destroy(struct i915_vma *vma)
3321{
3322 GEM_BUG_ON(vma->node.allocated);
3323 GEM_BUG_ON(i915_vma_is_active(vma));
Chris Wilson3272db52016-08-04 16:32:32 +01003324 GEM_BUG_ON(!i915_vma_is_closed(vma));
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003325
3326 list_del(&vma->vm_link);
Chris Wilson3272db52016-08-04 16:32:32 +01003327 if (!i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003328 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
3329
3330 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
3331}
3332
3333void i915_vma_close(struct i915_vma *vma)
3334{
Chris Wilson3272db52016-08-04 16:32:32 +01003335 GEM_BUG_ON(i915_vma_is_closed(vma));
3336 vma->flags |= I915_VMA_CLOSED;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003337
3338 list_del_init(&vma->obj_link);
Chris Wilson20dfbde2016-08-04 16:32:30 +01003339 if (!i915_vma_is_active(vma) && !i915_vma_is_pinned(vma))
Chris Wilsondf0e9a22016-08-04 07:52:47 +01003340 WARN_ON(i915_vma_unbind(vma));
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003341}
3342
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003343static struct i915_vma *
3344__i915_gem_vma_create(struct drm_i915_gem_object *obj,
3345 struct i915_address_space *vm,
Chris Wilsonde180032016-08-04 16:32:29 +01003346 const struct i915_ggtt_view *view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003347{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003348 struct i915_vma *vma;
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003349 int i;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003350
Chris Wilson50e046b2016-08-04 07:52:46 +01003351 GEM_BUG_ON(vm->closed);
3352
Chris Wilsonde180032016-08-04 16:32:29 +01003353 if (WARN_ON(i915_is_ggtt(vm) != !!view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003354 return ERR_PTR(-EINVAL);
Chris Wilsone20d2ab2015-04-07 16:20:58 +01003355
3356 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03003357 if (vma == NULL)
3358 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003359
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003360 INIT_LIST_HEAD(&vma->obj_link);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003361 INIT_LIST_HEAD(&vma->exec_list);
Chris Wilsonb0decaf2016-08-04 07:52:44 +01003362 for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
3363 init_request_active(&vma->last_read[i], i915_vma_retire);
Chris Wilson50e046b2016-08-04 07:52:46 +01003364 list_add(&vma->vm_link, &vm->unbound_list);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003365 vma->vm = vm;
3366 vma->obj = obj;
Chris Wilsonde180032016-08-04 16:32:29 +01003367 vma->size = obj->base.size;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003368
Chris Wilsonde180032016-08-04 16:32:29 +01003369 if (i915_is_ggtt(vm)) {
Chris Wilson3272db52016-08-04 16:32:32 +01003370 vma->flags |= I915_VMA_GGTT;
Chris Wilsonde180032016-08-04 16:32:29 +01003371 vma->ggtt_view = *view;
3372 if (view->type == I915_GGTT_VIEW_PARTIAL) {
3373 vma->size = view->params.partial.size;
3374 vma->size <<= PAGE_SHIFT;
3375 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
3376 vma->size =
3377 intel_rotation_info_size(&view->params.rotated);
3378 vma->size <<= PAGE_SHIFT;
3379 }
3380 } else {
Chris Wilson596c5922016-02-26 11:03:20 +00003381 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Chris Wilsonde180032016-08-04 16:32:29 +01003382 }
Ben Widawsky6f65e292013-12-06 14:10:56 -08003383
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003384 list_add_tail(&vma->obj_link, &obj->vma_list);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003385
3386 return vma;
3387}
3388
3389struct i915_vma *
Chris Wilson81a8aa42016-08-15 10:48:48 +01003390i915_vma_create(struct drm_i915_gem_object *obj,
3391 struct i915_address_space *vm,
3392 const struct i915_ggtt_view *view)
3393{
3394 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3395 GEM_BUG_ON(view ? i915_gem_obj_to_ggtt_view(obj, view) : i915_gem_obj_to_vma(obj, vm));
3396
3397 return __i915_gem_vma_create(obj, vm, view ?: &i915_ggtt_view_normal);
3398}
3399
3400struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003401i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
3402 struct i915_address_space *vm)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003403{
3404 struct i915_vma *vma;
3405
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003406 vma = i915_gem_obj_to_vma(obj, vm);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003407 if (!vma)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003408 vma = __i915_gem_vma_create(obj, vm,
3409 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003410
3411 return vma;
3412}
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003413
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003414struct i915_vma *
3415i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
3416 const struct i915_ggtt_view *view)
3417{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003418 struct drm_device *dev = obj->base.dev;
3419 struct drm_i915_private *dev_priv = to_i915(dev);
3420 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Matthew Auldade7daa2016-03-24 15:54:20 +00003421 struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003422
Chris Wilsonde895082016-08-04 16:32:34 +01003423 GEM_BUG_ON(!view);
3424
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003425 if (!vma)
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003426 vma = __i915_gem_vma_create(obj, &ggtt->base, view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003427
Chris Wilson3272db52016-08-04 16:32:32 +01003428 GEM_BUG_ON(i915_vma_is_closed(vma));
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003429 return vma;
3430
3431}
3432
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003433static struct scatterlist *
Ville Syrjälä2d7f3bd2016-01-14 15:22:11 +02003434rotate_pages(const dma_addr_t *in, unsigned int offset,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003435 unsigned int width, unsigned int height,
Ville Syrjälä87130252016-01-20 21:05:23 +02003436 unsigned int stride,
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003437 struct sg_table *st, struct scatterlist *sg)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003438{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003439 unsigned int column, row;
3440 unsigned int src_idx;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003441
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003442 for (column = 0; column < width; column++) {
Ville Syrjälä87130252016-01-20 21:05:23 +02003443 src_idx = stride * (height - 1) + column;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003444 for (row = 0; row < height; row++) {
3445 st->nents++;
3446 /* We don't need the pages, but need to initialize
3447 * the entries so the sg list can be happily traversed.
3448 * The only thing we need are DMA addresses.
3449 */
3450 sg_set_page(sg, NULL, PAGE_SIZE, 0);
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003451 sg_dma_address(sg) = in[offset + src_idx];
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003452 sg_dma_len(sg) = PAGE_SIZE;
3453 sg = sg_next(sg);
Ville Syrjälä87130252016-01-20 21:05:23 +02003454 src_idx -= stride;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003455 }
3456 }
Tvrtko Ursulin804beb42015-09-21 10:45:33 +01003457
3458 return sg;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003459}
3460
3461static struct sg_table *
Ville Syrjälä6687c902015-09-15 13:16:41 +03003462intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003463 struct drm_i915_gem_object *obj)
3464{
Dave Gordon85d12252016-05-20 11:54:06 +01003465 const size_t n_pages = obj->base.size / PAGE_SIZE;
Ville Syrjälä6687c902015-09-15 13:16:41 +03003466 unsigned int size = intel_rotation_info_size(rot_info);
Dave Gordon85d12252016-05-20 11:54:06 +01003467 struct sgt_iter sgt_iter;
3468 dma_addr_t dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003469 unsigned long i;
3470 dma_addr_t *page_addr_list;
3471 struct sg_table *st;
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003472 struct scatterlist *sg;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003473 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003474
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003475 /* Allocate a temporary list of source pages for random access. */
Dave Gordon85d12252016-05-20 11:54:06 +01003476 page_addr_list = drm_malloc_gfp(n_pages,
Chris Wilsonf2a85e12016-04-08 12:11:13 +01003477 sizeof(dma_addr_t),
3478 GFP_TEMPORARY);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003479 if (!page_addr_list)
3480 return ERR_PTR(ret);
3481
3482 /* Allocate target SG list. */
3483 st = kmalloc(sizeof(*st), GFP_KERNEL);
3484 if (!st)
3485 goto err_st_alloc;
3486
Ville Syrjälä6687c902015-09-15 13:16:41 +03003487 ret = sg_alloc_table(st, size, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003488 if (ret)
3489 goto err_sg_alloc;
3490
3491 /* Populate source page list from the object. */
3492 i = 0;
Dave Gordon85d12252016-05-20 11:54:06 +01003493 for_each_sgt_dma(dma_addr, sgt_iter, obj->pages)
3494 page_addr_list[i++] = dma_addr;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003495
Dave Gordon85d12252016-05-20 11:54:06 +01003496 GEM_BUG_ON(i != n_pages);
Ville Syrjälä11f20322016-02-15 22:54:46 +02003497 st->nents = 0;
3498 sg = st->sgl;
3499
Ville Syrjälä6687c902015-09-15 13:16:41 +03003500 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3501 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3502 rot_info->plane[i].width, rot_info->plane[i].height,
3503 rot_info->plane[i].stride, st, sg);
Tvrtko Ursulin89e3e142015-09-21 10:45:34 +01003504 }
3505
Ville Syrjälä6687c902015-09-15 13:16:41 +03003506 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3507 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003508
3509 drm_free_large(page_addr_list);
3510
3511 return st;
3512
3513err_sg_alloc:
3514 kfree(st);
3515err_st_alloc:
3516 drm_free_large(page_addr_list);
3517
Ville Syrjälä6687c902015-09-15 13:16:41 +03003518 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3519 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3520
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003521 return ERR_PTR(ret);
3522}
3523
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003524static struct sg_table *
3525intel_partial_pages(const struct i915_ggtt_view *view,
3526 struct drm_i915_gem_object *obj)
3527{
3528 struct sg_table *st;
3529 struct scatterlist *sg;
3530 struct sg_page_iter obj_sg_iter;
3531 int ret = -ENOMEM;
3532
3533 st = kmalloc(sizeof(*st), GFP_KERNEL);
3534 if (!st)
3535 goto err_st_alloc;
3536
3537 ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL);
3538 if (ret)
3539 goto err_sg_alloc;
3540
3541 sg = st->sgl;
3542 st->nents = 0;
3543 for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents,
3544 view->params.partial.offset)
3545 {
3546 if (st->nents >= view->params.partial.size)
3547 break;
3548
3549 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3550 sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter);
3551 sg_dma_len(sg) = PAGE_SIZE;
3552
3553 sg = sg_next(sg);
3554 st->nents++;
3555 }
3556
3557 return st;
3558
3559err_sg_alloc:
3560 kfree(st);
3561err_st_alloc:
3562 return ERR_PTR(ret);
3563}
3564
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003565static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003566i915_get_ggtt_vma_pages(struct i915_vma *vma)
3567{
3568 int ret = 0;
3569
Chris Wilson247177d2016-08-15 10:48:47 +01003570 if (vma->pages)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003571 return 0;
3572
3573 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003574 vma->pages = vma->obj->pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003575 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
Chris Wilson247177d2016-08-15 10:48:47 +01003576 vma->pages =
Ville Syrjälä11d23e62016-01-20 21:05:24 +02003577 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003578 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
Chris Wilson247177d2016-08-15 10:48:47 +01003579 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003580 else
3581 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3582 vma->ggtt_view.type);
3583
Chris Wilson247177d2016-08-15 10:48:47 +01003584 if (!vma->pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003585 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003586 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003587 ret = -EINVAL;
Chris Wilson247177d2016-08-15 10:48:47 +01003588 } else if (IS_ERR(vma->pages)) {
3589 ret = PTR_ERR(vma->pages);
3590 vma->pages = NULL;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003591 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3592 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003593 }
3594
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003595 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003596}
3597
3598/**
3599 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
3600 * @vma: VMA to map
3601 * @cache_level: mapping cache level
3602 * @flags: flags like global or local mapping
3603 *
3604 * DMA addresses are taken from the scatter-gather table of this object (or of
3605 * this VMA in case of non-default GGTT views) and PTE entries set up.
3606 * Note that DMA addresses are also the only part of the SG table we care about.
3607 */
3608int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
3609 u32 flags)
3610{
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003611 u32 bind_flags;
Chris Wilson3272db52016-08-04 16:32:32 +01003612 u32 vma_flags;
3613 int ret;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003614
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003615 if (WARN_ON(flags == 0))
3616 return -EINVAL;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003617
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003618 bind_flags = 0;
Daniel Vetter08755462015-04-20 09:04:05 -07003619 if (flags & PIN_GLOBAL)
Chris Wilson3272db52016-08-04 16:32:32 +01003620 bind_flags |= I915_VMA_GLOBAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003621 if (flags & PIN_USER)
Chris Wilson3272db52016-08-04 16:32:32 +01003622 bind_flags |= I915_VMA_LOCAL_BIND;
Daniel Vetter08755462015-04-20 09:04:05 -07003623
Chris Wilson3272db52016-08-04 16:32:32 +01003624 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
Daniel Vetter08755462015-04-20 09:04:05 -07003625 if (flags & PIN_UPDATE)
Chris Wilson3272db52016-08-04 16:32:32 +01003626 bind_flags |= vma_flags;
Daniel Vetter08755462015-04-20 09:04:05 -07003627 else
Chris Wilson3272db52016-08-04 16:32:32 +01003628 bind_flags &= ~vma_flags;
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003629 if (bind_flags == 0)
3630 return 0;
3631
Chris Wilson3272db52016-08-04 16:32:32 +01003632 if (vma_flags == 0 && vma->vm->allocate_va_range) {
Chris Wilson596c5922016-02-26 11:03:20 +00003633 trace_i915_va_alloc(vma);
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003634 ret = vma->vm->allocate_va_range(vma->vm,
3635 vma->node.start,
3636 vma->node.size);
3637 if (ret)
3638 return ret;
3639 }
3640
3641 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003642 if (ret)
3643 return ret;
Daniel Vetter08755462015-04-20 09:04:05 -07003644
Chris Wilson3272db52016-08-04 16:32:32 +01003645 vma->flags |= bind_flags;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003646 return 0;
3647}
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003648
Chris Wilson8ef85612016-04-28 09:56:39 +01003649void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
3650{
3651 void __iomem *ptr;
3652
3653 lockdep_assert_held(&vma->vm->dev->struct_mutex);
3654 if (WARN_ON(!vma->obj->map_and_fenceable))
Chris Wilson406ea8d2016-07-20 13:31:55 +01003655 return IO_ERR_PTR(-ENODEV);
Chris Wilson8ef85612016-04-28 09:56:39 +01003656
Chris Wilson3272db52016-08-04 16:32:32 +01003657 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
3658 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
Chris Wilson8ef85612016-04-28 09:56:39 +01003659
3660 ptr = vma->iomap;
3661 if (ptr == NULL) {
3662 ptr = io_mapping_map_wc(i915_vm_to_ggtt(vma->vm)->mappable,
3663 vma->node.start,
3664 vma->node.size);
3665 if (ptr == NULL)
Chris Wilson406ea8d2016-07-20 13:31:55 +01003666 return IO_ERR_PTR(-ENOMEM);
Chris Wilson8ef85612016-04-28 09:56:39 +01003667
3668 vma->iomap = ptr;
3669 }
3670
Chris Wilson20dfbde2016-08-04 16:32:30 +01003671 __i915_vma_pin(vma);
Chris Wilson8ef85612016-04-28 09:56:39 +01003672 return ptr;
3673}