blob: 408003445dd6cab8ed59c045188c7f06f44e9607 [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>
David Howells760285e2012-10-02 18:01:07 +010027#include <drm/drmP.h>
28#include <drm/i915_drm.h>
Daniel Vetter76aaf222010-11-05 22:23:30 +010029#include "i915_drv.h"
Yu Zhang5dda8fa2015-02-10 19:05:48 +080030#include "i915_vgpu.h"
Daniel Vetter76aaf222010-11-05 22:23:30 +010031#include "i915_trace.h"
32#include "intel_drv.h"
33
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000034/**
35 * DOC: Global GTT views
36 *
37 * Background and previous state
38 *
39 * Historically objects could exists (be bound) in global GTT space only as
40 * singular instances with a view representing all of the object's backing pages
41 * in a linear fashion. This view will be called a normal view.
42 *
43 * To support multiple views of the same object, where the number of mapped
44 * pages is not equal to the backing store, or where the layout of the pages
45 * is not linear, concept of a GGTT view was added.
46 *
47 * One example of an alternative view is a stereo display driven by a single
48 * image. In this case we would have a framebuffer looking like this
49 * (2x2 pages):
50 *
51 * 12
52 * 34
53 *
54 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
55 * rendering. In contrast, fed to the display engine would be an alternative
56 * view which could look something like this:
57 *
58 * 1212
59 * 3434
60 *
61 * In this example both the size and layout of pages in the alternative view is
62 * different from the normal view.
63 *
64 * Implementation and usage
65 *
66 * GGTT views are implemented using VMAs and are distinguished via enum
67 * i915_ggtt_view_type and struct i915_ggtt_view.
68 *
69 * A new flavour of core GEM functions which work with GGTT bound objects were
Joonas Lahtinenec7adb62015-03-16 14:11:13 +020070 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
71 * renaming in large amounts of code. They take the struct i915_ggtt_view
72 * parameter encapsulating all metadata required to implement a view.
Tvrtko Ursulin45f8f692014-12-10 17:27:59 +000073 *
74 * As a helper for callers which are only interested in the normal view,
75 * globally const i915_ggtt_view_normal singleton instance exists. All old core
76 * GEM API functions, the ones not taking the view parameter, are operating on,
77 * or with the normal GGTT view.
78 *
79 * Code wanting to add or use a new GGTT view needs to:
80 *
81 * 1. Add a new enum with a suitable name.
82 * 2. Extend the metadata in the i915_ggtt_view structure if required.
83 * 3. Add support to i915_get_vma_pages().
84 *
85 * New views are required to build a scatter-gather table from within the
86 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
87 * exists for the lifetime of an VMA.
88 *
89 * Core API is designed to have copy semantics which means that passed in
90 * struct i915_ggtt_view does not need to be persistent (left around after
91 * calling the core API functions).
92 *
93 */
94
Daniel Vetter70b9f6f2015-04-14 17:35:27 +020095static int
96i915_get_ggtt_vma_pages(struct i915_vma *vma);
97
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +000098const struct i915_ggtt_view i915_ggtt_view_normal;
Joonas Lahtinen9abc4642015-03-27 13:09:22 +020099const struct i915_ggtt_view i915_ggtt_view_rotated = {
100 .type = I915_GGTT_VIEW_ROTATED
101};
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +0000102
Daniel Vettercfa7c862014-04-29 11:53:58 +0200103static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
104{
Chris Wilson1893a712014-09-19 11:56:27 +0100105 bool has_aliasing_ppgtt;
106 bool has_full_ppgtt;
107
108 has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
109 has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
Chris Wilson1893a712014-09-19 11:56:27 +0100110
Yu Zhang71ba2d62015-02-10 19:05:54 +0800111 if (intel_vgpu_active(dev))
112 has_full_ppgtt = false; /* emulation is too hard */
113
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000114 /*
115 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
116 * execlists, the sole mechanism available to submit work.
117 */
118 if (INTEL_INFO(dev)->gen < 9 &&
119 (enable_ppgtt == 0 || !has_aliasing_ppgtt))
Daniel Vettercfa7c862014-04-29 11:53:58 +0200120 return 0;
121
122 if (enable_ppgtt == 1)
123 return 1;
124
Chris Wilson1893a712014-09-19 11:56:27 +0100125 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200126 return 2;
127
Daniel Vetter93a25a92014-03-06 09:40:43 +0100128#ifdef CONFIG_INTEL_IOMMU
129 /* Disable ppgtt on SNB if VT-d is on. */
130 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
131 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200132 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100133 }
134#endif
135
Jesse Barnes62942ed2014-06-13 09:28:33 -0700136 /* Early VLV doesn't have this */
Ville Syrjäläca2aed6c2014-06-28 02:03:56 +0300137 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
138 dev->pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700139 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
140 return 0;
141 }
142
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000143 if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
144 return 2;
145 else
146 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100147}
148
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200149static int ppgtt_bind_vma(struct i915_vma *vma,
150 enum i915_cache_level cache_level,
151 u32 unused)
Daniel Vetter47552652015-04-14 17:35:24 +0200152{
153 u32 pte_flags = 0;
154
155 /* Currently applicable only to VLV */
156 if (vma->obj->gt_ro)
157 pte_flags |= PTE_READ_ONLY;
158
159 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
160 cache_level, pte_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +0200161
162 return 0;
Daniel Vetter47552652015-04-14 17:35:24 +0200163}
164
165static void ppgtt_unbind_vma(struct i915_vma *vma)
166{
167 vma->vm->clear_range(vma->vm,
168 vma->node.start,
169 vma->obj->base.size,
170 true);
171}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800172
Daniel Vetter2c642b02015-04-14 17:35:26 +0200173static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
174 enum i915_cache_level level,
175 bool valid)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700176{
Michel Thierry07749ef2015-03-16 16:00:54 +0000177 gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700178 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300179
180 switch (level) {
181 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800182 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300183 break;
184 case I915_CACHE_WT:
185 pte |= PPAT_DISPLAY_ELLC_INDEX;
186 break;
187 default:
188 pte |= PPAT_CACHED_INDEX;
189 break;
190 }
191
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700192 return pte;
193}
194
Mika Kuoppalafe36f552015-06-25 18:35:16 +0300195static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
196 const enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800197{
Michel Thierry07749ef2015-03-16 16:00:54 +0000198 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800199 pde |= addr;
200 if (level != I915_CACHE_NONE)
201 pde |= PPAT_CACHED_PDE_INDEX;
202 else
203 pde |= PPAT_UNCACHED_INDEX;
204 return pde;
205}
206
Michel Thierry762d9932015-07-30 11:05:29 +0100207#define gen8_pdpe_encode gen8_pde_encode
208#define gen8_pml4e_encode gen8_pde_encode
209
Michel Thierry07749ef2015-03-16 16:00:54 +0000210static gen6_pte_t snb_pte_encode(dma_addr_t addr,
211 enum i915_cache_level level,
212 bool valid, u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700213{
Michel Thierry07749ef2015-03-16 16:00:54 +0000214 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -0700215 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700216
217 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100218 case I915_CACHE_L3_LLC:
219 case I915_CACHE_LLC:
220 pte |= GEN6_PTE_CACHE_LLC;
221 break;
222 case I915_CACHE_NONE:
223 pte |= GEN6_PTE_UNCACHED;
224 break;
225 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100226 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100227 }
228
229 return pte;
230}
231
Michel Thierry07749ef2015-03-16 16:00:54 +0000232static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
233 enum i915_cache_level level,
234 bool valid, u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100235{
Michel Thierry07749ef2015-03-16 16:00:54 +0000236 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100237 pte |= GEN6_PTE_ADDR_ENCODE(addr);
238
239 switch (level) {
240 case I915_CACHE_L3_LLC:
241 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700242 break;
243 case I915_CACHE_LLC:
244 pte |= GEN6_PTE_CACHE_LLC;
245 break;
246 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700247 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700248 break;
249 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100250 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700251 }
252
Ben Widawsky54d12522012-09-24 16:44:32 -0700253 return pte;
254}
255
Michel Thierry07749ef2015-03-16 16:00:54 +0000256static gen6_pte_t byt_pte_encode(dma_addr_t addr,
257 enum i915_cache_level level,
258 bool valid, u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700259{
Michel Thierry07749ef2015-03-16 16:00:54 +0000260 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700261 pte |= GEN6_PTE_ADDR_ENCODE(addr);
262
Akash Goel24f3a8c2014-06-17 10:59:42 +0530263 if (!(flags & PTE_READ_ONLY))
264 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700265
266 if (level != I915_CACHE_NONE)
267 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
268
269 return pte;
270}
271
Michel Thierry07749ef2015-03-16 16:00:54 +0000272static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
273 enum i915_cache_level level,
274 bool valid, u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700275{
Michel Thierry07749ef2015-03-16 16:00:54 +0000276 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700277 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700278
279 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700280 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700281
282 return pte;
283}
284
Michel Thierry07749ef2015-03-16 16:00:54 +0000285static gen6_pte_t iris_pte_encode(dma_addr_t addr,
286 enum i915_cache_level level,
287 bool valid, u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700288{
Michel Thierry07749ef2015-03-16 16:00:54 +0000289 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700290 pte |= HSW_PTE_ADDR_ENCODE(addr);
291
Chris Wilson651d7942013-08-08 14:41:10 +0100292 switch (level) {
293 case I915_CACHE_NONE:
294 break;
295 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000296 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100297 break;
298 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000299 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100300 break;
301 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700302
303 return pte;
304}
305
Mika Kuoppalac114f762015-06-25 18:35:13 +0300306static int __setup_page_dma(struct drm_device *dev,
307 struct i915_page_dma *p, gfp_t flags)
Ben Widawsky678d96f2015-03-16 16:00:56 +0000308{
309 struct device *device = &dev->pdev->dev;
310
Mika Kuoppalac114f762015-06-25 18:35:13 +0300311 p->page = alloc_page(flags);
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300312 if (!p->page)
Michel Thierry1266cdb2015-03-24 17:06:33 +0000313 return -ENOMEM;
314
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300315 p->daddr = dma_map_page(device,
316 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
317
318 if (dma_mapping_error(device, p->daddr)) {
319 __free_page(p->page);
320 return -EINVAL;
321 }
322
Michel Thierry1266cdb2015-03-24 17:06:33 +0000323 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000324}
325
Mika Kuoppalac114f762015-06-25 18:35:13 +0300326static int setup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
327{
328 return __setup_page_dma(dev, p, GFP_KERNEL);
329}
330
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300331static void cleanup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
332{
333 if (WARN_ON(!p->page))
334 return;
335
336 dma_unmap_page(&dev->pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
337 __free_page(p->page);
338 memset(p, 0, sizeof(*p));
339}
340
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300341static void *kmap_page_dma(struct i915_page_dma *p)
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300342{
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300343 return kmap_atomic(p->page);
344}
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300345
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300346/* We use the flushing unmap only with ppgtt structures:
347 * page directories, page tables and scratch pages.
348 */
349static void kunmap_page_dma(struct drm_device *dev, void *vaddr)
350{
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300351 /* There are only few exceptions for gen >=6. chv and bxt.
352 * And we are not sure about the latter so play safe for now.
353 */
354 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
355 drm_clflush_virt_range(vaddr, PAGE_SIZE);
356
357 kunmap_atomic(vaddr);
358}
359
Mika Kuoppala567047b2015-06-25 18:35:12 +0300360#define kmap_px(px) kmap_page_dma(px_base(px))
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300361#define kunmap_px(ppgtt, vaddr) kunmap_page_dma((ppgtt)->base.dev, (vaddr))
362
Mika Kuoppala567047b2015-06-25 18:35:12 +0300363#define setup_px(dev, px) setup_page_dma((dev), px_base(px))
364#define cleanup_px(dev, px) cleanup_page_dma((dev), px_base(px))
365#define fill_px(dev, px, v) fill_page_dma((dev), px_base(px), (v))
366#define fill32_px(dev, px, v) fill_page_dma_32((dev), px_base(px), (v))
367
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300368static void fill_page_dma(struct drm_device *dev, struct i915_page_dma *p,
369 const uint64_t val)
370{
371 int i;
372 uint64_t * const vaddr = kmap_page_dma(p);
373
374 for (i = 0; i < 512; i++)
375 vaddr[i] = val;
376
377 kunmap_page_dma(dev, vaddr);
378}
379
Mika Kuoppala73eeea52015-06-25 18:35:10 +0300380static void fill_page_dma_32(struct drm_device *dev, struct i915_page_dma *p,
381 const uint32_t val32)
382{
383 uint64_t v = val32;
384
385 v = v << 32 | val32;
386
387 fill_page_dma(dev, p, v);
388}
389
Mika Kuoppala4ad2af12015-06-30 18:16:39 +0300390static struct i915_page_scratch *alloc_scratch_page(struct drm_device *dev)
391{
392 struct i915_page_scratch *sp;
393 int ret;
394
395 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
396 if (sp == NULL)
397 return ERR_PTR(-ENOMEM);
398
399 ret = __setup_page_dma(dev, px_base(sp), GFP_DMA32 | __GFP_ZERO);
400 if (ret) {
401 kfree(sp);
402 return ERR_PTR(ret);
403 }
404
405 set_pages_uc(px_page(sp), 1);
406
407 return sp;
408}
409
410static void free_scratch_page(struct drm_device *dev,
411 struct i915_page_scratch *sp)
412{
413 set_pages_wb(px_page(sp), 1);
414
415 cleanup_px(dev, sp);
416 kfree(sp);
417}
418
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300419static struct i915_page_table *alloc_pt(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000420{
Michel Thierryec565b32015-04-08 12:13:23 +0100421 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000422 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
423 GEN8_PTES : GEN6_PTES;
424 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000425
426 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
427 if (!pt)
428 return ERR_PTR(-ENOMEM);
429
Ben Widawsky678d96f2015-03-16 16:00:56 +0000430 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
431 GFP_KERNEL);
432
433 if (!pt->used_ptes)
434 goto fail_bitmap;
435
Mika Kuoppala567047b2015-06-25 18:35:12 +0300436 ret = setup_px(dev, pt);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000437 if (ret)
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300438 goto fail_page_m;
Ben Widawsky06fda602015-02-24 16:22:36 +0000439
440 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000441
Mika Kuoppala44159dd2015-06-25 18:35:07 +0300442fail_page_m:
Ben Widawsky678d96f2015-03-16 16:00:56 +0000443 kfree(pt->used_ptes);
444fail_bitmap:
445 kfree(pt);
446
447 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000448}
449
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300450static void free_pt(struct drm_device *dev, struct i915_page_table *pt)
Ben Widawsky06fda602015-02-24 16:22:36 +0000451{
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300452 cleanup_px(dev, pt);
453 kfree(pt->used_ptes);
454 kfree(pt);
455}
456
457static void gen8_initialize_pt(struct i915_address_space *vm,
458 struct i915_page_table *pt)
459{
460 gen8_pte_t scratch_pte;
461
462 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
463 I915_CACHE_LLC, true);
464
465 fill_px(vm->dev, pt, scratch_pte);
466}
467
468static void gen6_initialize_pt(struct i915_address_space *vm,
469 struct i915_page_table *pt)
470{
471 gen6_pte_t scratch_pte;
472
473 WARN_ON(px_dma(vm->scratch_page) == 0);
474
475 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
476 I915_CACHE_LLC, true, 0);
477
478 fill32_px(vm->dev, pt, scratch_pte);
Ben Widawsky06fda602015-02-24 16:22:36 +0000479}
480
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300481static struct i915_page_directory *alloc_pd(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000482{
Michel Thierryec565b32015-04-08 12:13:23 +0100483 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100484 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000485
486 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
487 if (!pd)
488 return ERR_PTR(-ENOMEM);
489
Michel Thierry33c88192015-04-08 12:13:33 +0100490 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
491 sizeof(*pd->used_pdes), GFP_KERNEL);
492 if (!pd->used_pdes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300493 goto fail_bitmap;
Michel Thierry33c88192015-04-08 12:13:33 +0100494
Mika Kuoppala567047b2015-06-25 18:35:12 +0300495 ret = setup_px(dev, pd);
Michel Thierry33c88192015-04-08 12:13:33 +0100496 if (ret)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300497 goto fail_page_m;
Michel Thierrye5815a22015-04-08 12:13:32 +0100498
Ben Widawsky06fda602015-02-24 16:22:36 +0000499 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100500
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300501fail_page_m:
Michel Thierry33c88192015-04-08 12:13:33 +0100502 kfree(pd->used_pdes);
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300503fail_bitmap:
Michel Thierry33c88192015-04-08 12:13:33 +0100504 kfree(pd);
505
506 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000507}
508
Mika Kuoppala2e906be2015-06-30 18:16:37 +0300509static void free_pd(struct drm_device *dev, struct i915_page_directory *pd)
510{
511 if (px_page(pd)) {
512 cleanup_px(dev, pd);
513 kfree(pd->used_pdes);
514 kfree(pd);
515 }
516}
517
518static void gen8_initialize_pd(struct i915_address_space *vm,
519 struct i915_page_directory *pd)
520{
521 gen8_pde_t scratch_pde;
522
523 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
524
525 fill_px(vm->dev, pd, scratch_pde);
526}
527
Michel Thierry6ac18502015-07-29 17:23:46 +0100528static int __pdp_init(struct drm_device *dev,
529 struct i915_page_directory_pointer *pdp)
530{
531 size_t pdpes = I915_PDPES_PER_PDP(dev);
532
533 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
534 sizeof(unsigned long),
535 GFP_KERNEL);
536 if (!pdp->used_pdpes)
537 return -ENOMEM;
538
539 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
540 GFP_KERNEL);
541 if (!pdp->page_directory) {
542 kfree(pdp->used_pdpes);
543 /* the PDP might be the statically allocated top level. Keep it
544 * as clean as possible */
545 pdp->used_pdpes = NULL;
546 return -ENOMEM;
547 }
548
549 return 0;
550}
551
552static void __pdp_fini(struct i915_page_directory_pointer *pdp)
553{
554 kfree(pdp->used_pdpes);
555 kfree(pdp->page_directory);
556 pdp->page_directory = NULL;
557}
558
Michel Thierry762d9932015-07-30 11:05:29 +0100559static struct
560i915_page_directory_pointer *alloc_pdp(struct drm_device *dev)
561{
562 struct i915_page_directory_pointer *pdp;
563 int ret = -ENOMEM;
564
565 WARN_ON(!USES_FULL_48BIT_PPGTT(dev));
566
567 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
568 if (!pdp)
569 return ERR_PTR(-ENOMEM);
570
571 ret = __pdp_init(dev, pdp);
572 if (ret)
573 goto fail_bitmap;
574
575 ret = setup_px(dev, pdp);
576 if (ret)
577 goto fail_page_m;
578
579 return pdp;
580
581fail_page_m:
582 __pdp_fini(pdp);
583fail_bitmap:
584 kfree(pdp);
585
586 return ERR_PTR(ret);
587}
588
Michel Thierry6ac18502015-07-29 17:23:46 +0100589static void free_pdp(struct drm_device *dev,
590 struct i915_page_directory_pointer *pdp)
591{
592 __pdp_fini(pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100593 if (USES_FULL_48BIT_PPGTT(dev)) {
594 cleanup_px(dev, pdp);
595 kfree(pdp);
596 }
597}
598
599static void
600gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
601 struct i915_page_directory_pointer *pdp,
602 struct i915_page_directory *pd,
603 int index)
604{
605 gen8_ppgtt_pdpe_t *page_directorypo;
606
607 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
608 return;
609
610 page_directorypo = kmap_px(pdp);
611 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
612 kunmap_px(ppgtt, page_directorypo);
613}
614
615static void
616gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
617 struct i915_pml4 *pml4,
618 struct i915_page_directory_pointer *pdp,
619 int index)
620{
621 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
622
623 WARN_ON(!USES_FULL_48BIT_PPGTT(ppgtt->base.dev));
624 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
625 kunmap_px(ppgtt, pagemap);
Michel Thierry6ac18502015-07-29 17:23:46 +0100626}
627
Ben Widawsky94e409c2013-11-04 22:29:36 -0800628/* Broadwell Page Directory Pointer Descriptors */
John Harrisone85b26d2015-05-29 17:43:56 +0100629static int gen8_write_pdp(struct drm_i915_gem_request *req,
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100630 unsigned entry,
631 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800632{
John Harrisone85b26d2015-05-29 17:43:56 +0100633 struct intel_engine_cs *ring = req->ring;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800634 int ret;
635
636 BUG_ON(entry >= 4);
637
John Harrison5fb9de12015-05-29 17:44:07 +0100638 ret = intel_ring_begin(req, 6);
Ben Widawsky94e409c2013-11-04 22:29:36 -0800639 if (ret)
640 return ret;
641
642 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
643 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100644 intel_ring_emit(ring, upper_32_bits(addr));
Ben Widawsky94e409c2013-11-04 22:29:36 -0800645 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
646 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100647 intel_ring_emit(ring, lower_32_bits(addr));
Ben Widawsky94e409c2013-11-04 22:29:36 -0800648 intel_ring_advance(ring);
649
650 return 0;
651}
652
Michel Thierry2dba3232015-07-30 11:06:23 +0100653static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
654 struct drm_i915_gem_request *req)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800655{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800656 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800657
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100658 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
Mika Kuoppalad852c7b2015-06-25 18:35:06 +0300659 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
660
John Harrisone85b26d2015-05-29 17:43:56 +0100661 ret = gen8_write_pdp(req, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800662 if (ret)
663 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800664 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800665
Ben Widawskyeeb94882013-12-06 14:11:10 -0800666 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800667}
668
Michel Thierry2dba3232015-07-30 11:06:23 +0100669static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
670 struct drm_i915_gem_request *req)
671{
672 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
673}
674
Michel Thierryf9b5b782015-07-30 11:02:49 +0100675static void gen8_ppgtt_clear_pte_range(struct i915_address_space *vm,
676 struct i915_page_directory_pointer *pdp,
677 uint64_t start,
678 uint64_t length,
679 gen8_pte_t scratch_pte)
Ben Widawsky459108b2013-11-02 21:07:23 -0700680{
681 struct i915_hw_ppgtt *ppgtt =
682 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100683 gen8_pte_t *pt_vaddr;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800684 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
685 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
686 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky782f1492014-02-20 11:50:33 -0800687 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky459108b2013-11-02 21:07:23 -0700688 unsigned last_pte, i;
689
Michel Thierryf9b5b782015-07-30 11:02:49 +0100690 if (WARN_ON(!pdp))
691 return;
Ben Widawsky459108b2013-11-02 21:07:23 -0700692
693 while (num_entries) {
Michel Thierryec565b32015-04-08 12:13:23 +0100694 struct i915_page_directory *pd;
695 struct i915_page_table *pt;
Ben Widawsky06fda602015-02-24 16:22:36 +0000696
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100697 if (WARN_ON(!pdp->page_directory[pdpe]))
Michel Thierry00245262015-06-25 12:59:38 +0100698 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000699
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100700 pd = pdp->page_directory[pdpe];
Ben Widawsky06fda602015-02-24 16:22:36 +0000701
702 if (WARN_ON(!pd->page_table[pde]))
Michel Thierry00245262015-06-25 12:59:38 +0100703 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000704
705 pt = pd->page_table[pde];
706
Mika Kuoppala567047b2015-06-25 18:35:12 +0300707 if (WARN_ON(!px_page(pt)))
Michel Thierry00245262015-06-25 12:59:38 +0100708 break;
Ben Widawsky06fda602015-02-24 16:22:36 +0000709
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800710 last_pte = pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +0000711 if (last_pte > GEN8_PTES)
712 last_pte = GEN8_PTES;
Ben Widawsky459108b2013-11-02 21:07:23 -0700713
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300714 pt_vaddr = kmap_px(pt);
Ben Widawsky459108b2013-11-02 21:07:23 -0700715
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800716 for (i = pte; i < last_pte; i++) {
Ben Widawsky459108b2013-11-02 21:07:23 -0700717 pt_vaddr[i] = scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800718 num_entries--;
719 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700720
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300721 kunmap_px(ppgtt, pt);
Ben Widawsky459108b2013-11-02 21:07:23 -0700722
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800723 pte = 0;
Michel Thierry07749ef2015-03-16 16:00:54 +0000724 if (++pde == I915_PDES) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800725 pdpe++;
726 pde = 0;
727 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700728 }
729}
730
Michel Thierryf9b5b782015-07-30 11:02:49 +0100731static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
732 uint64_t start,
733 uint64_t length,
734 bool use_scratch)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700735{
736 struct i915_hw_ppgtt *ppgtt =
737 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100738 struct i915_page_directory_pointer *pdp = &ppgtt->pdp; /* FIXME: 48b */
Michel Thierryf9b5b782015-07-30 11:02:49 +0100739
740 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
741 I915_CACHE_LLC, use_scratch);
742
743 gen8_ppgtt_clear_pte_range(vm, pdp, start, length, scratch_pte);
744}
745
746static void
747gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
748 struct i915_page_directory_pointer *pdp,
Michel Thierry3387d432015-08-03 09:52:47 +0100749 struct sg_page_iter *sg_iter,
Michel Thierryf9b5b782015-07-30 11:02:49 +0100750 uint64_t start,
751 enum i915_cache_level cache_level)
752{
753 struct i915_hw_ppgtt *ppgtt =
754 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +0000755 gen8_pte_t *pt_vaddr;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800756 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
757 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
758 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700759
Chris Wilson6f1cc992013-12-31 15:50:31 +0000760 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700761
Michel Thierry3387d432015-08-03 09:52:47 +0100762 while (__sg_page_iter_next(sg_iter)) {
Ben Widawsky76643602015-01-22 17:01:24 +0000763 if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800764 break;
765
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000766 if (pt_vaddr == NULL) {
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100767 struct i915_page_directory *pd = pdp->page_directory[pdpe];
Michel Thierryec565b32015-04-08 12:13:23 +0100768 struct i915_page_table *pt = pd->page_table[pde];
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300769 pt_vaddr = kmap_px(pt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000770 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800771
772 pt_vaddr[pte] =
Michel Thierry3387d432015-08-03 09:52:47 +0100773 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
Chris Wilson6f1cc992013-12-31 15:50:31 +0000774 cache_level, true);
Michel Thierry07749ef2015-03-16 16:00:54 +0000775 if (++pte == GEN8_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300776 kunmap_px(ppgtt, pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000777 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000778 if (++pde == I915_PDES) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800779 pdpe++;
780 pde = 0;
781 }
782 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700783 }
784 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +0300785
786 if (pt_vaddr)
787 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700788}
789
Michel Thierryf9b5b782015-07-30 11:02:49 +0100790static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
791 struct sg_table *pages,
792 uint64_t start,
793 enum i915_cache_level cache_level,
794 u32 unused)
795{
796 struct i915_hw_ppgtt *ppgtt =
797 container_of(vm, struct i915_hw_ppgtt, base);
798 struct i915_page_directory_pointer *pdp = &ppgtt->pdp; /* FIXME: 48b */
Michel Thierry3387d432015-08-03 09:52:47 +0100799 struct sg_page_iter sg_iter;
Michel Thierryf9b5b782015-07-30 11:02:49 +0100800
Michel Thierry3387d432015-08-03 09:52:47 +0100801 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
802 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter, start, cache_level);
Michel Thierryf9b5b782015-07-30 11:02:49 +0100803}
804
Michel Thierryf37c0502015-06-10 17:46:39 +0100805static void gen8_free_page_tables(struct drm_device *dev,
806 struct i915_page_directory *pd)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800807{
808 int i;
809
Mika Kuoppala567047b2015-06-25 18:35:12 +0300810 if (!px_page(pd))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800811 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800812
Michel Thierry33c88192015-04-08 12:13:33 +0100813 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000814 if (WARN_ON(!pd->page_table[i]))
815 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800816
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300817 free_pt(dev, pd->page_table[i]);
Ben Widawsky06fda602015-02-24 16:22:36 +0000818 pd->page_table[i] = NULL;
819 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000820}
821
Mika Kuoppala8776f022015-06-30 18:16:40 +0300822static int gen8_init_scratch(struct i915_address_space *vm)
823{
824 struct drm_device *dev = vm->dev;
825
826 vm->scratch_page = alloc_scratch_page(dev);
827 if (IS_ERR(vm->scratch_page))
828 return PTR_ERR(vm->scratch_page);
829
830 vm->scratch_pt = alloc_pt(dev);
831 if (IS_ERR(vm->scratch_pt)) {
832 free_scratch_page(dev, vm->scratch_page);
833 return PTR_ERR(vm->scratch_pt);
834 }
835
836 vm->scratch_pd = alloc_pd(dev);
837 if (IS_ERR(vm->scratch_pd)) {
838 free_pt(dev, vm->scratch_pt);
839 free_scratch_page(dev, vm->scratch_page);
840 return PTR_ERR(vm->scratch_pd);
841 }
842
843 gen8_initialize_pt(vm, vm->scratch_pt);
844 gen8_initialize_pd(vm, vm->scratch_pd);
845
846 return 0;
847}
848
849static void gen8_free_scratch(struct i915_address_space *vm)
850{
851 struct drm_device *dev = vm->dev;
852
853 free_pd(dev, vm->scratch_pd);
854 free_pt(dev, vm->scratch_pt);
855 free_scratch_page(dev, vm->scratch_page);
856}
857
Michel Thierry762d9932015-07-30 11:05:29 +0100858static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
859 struct i915_page_directory_pointer *pdp)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800860{
861 int i;
862
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100863 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
864 if (WARN_ON(!pdp->page_directory[i]))
Ben Widawsky06fda602015-02-24 16:22:36 +0000865 continue;
866
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100867 gen8_free_page_tables(dev, pdp->page_directory[i]);
868 free_pd(dev, pdp->page_directory[i]);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800869 }
Michel Thierry69876be2015-04-08 12:13:27 +0100870
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100871 free_pdp(dev, pdp);
Michel Thierry762d9932015-07-30 11:05:29 +0100872}
873
874static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
875{
876 int i;
877
878 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
879 if (WARN_ON(!ppgtt->pml4.pdps[i]))
880 continue;
881
882 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
883 }
884
885 cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
886}
887
888static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
889{
890 struct i915_hw_ppgtt *ppgtt =
891 container_of(vm, struct i915_hw_ppgtt, base);
892
893 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
894 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
895 else
896 gen8_ppgtt_cleanup_4lvl(ppgtt);
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100897
Mika Kuoppala8776f022015-06-30 18:16:40 +0300898 gen8_free_scratch(vm);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800899}
900
Michel Thierryd7b26332015-04-08 12:13:34 +0100901/**
902 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100903 * @vm: Master vm structure.
904 * @pd: Page directory for this address range.
Michel Thierryd7b26332015-04-08 12:13:34 +0100905 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100906 * @length: Size of the allocations.
Michel Thierryd7b26332015-04-08 12:13:34 +0100907 * @new_pts: Bitmap set by function with new allocations. Likely used by the
908 * caller to free on error.
909 *
910 * Allocate the required number of page tables. Extremely similar to
911 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
912 * the page directory boundary (instead of the page directory pointer). That
913 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
914 * possible, and likely that the caller will need to use multiple calls of this
915 * function to achieve the appropriate allocation.
916 *
917 * Return: 0 if success; negative error code otherwise.
918 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100919static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +0100920 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +0100921 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +0100922 uint64_t length,
923 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000924{
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100925 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +0100926 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100927 uint64_t temp;
928 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000929
Michel Thierryd7b26332015-04-08 12:13:34 +0100930 gen8_for_each_pde(pt, pd, start, length, temp, pde) {
931 /* Don't reallocate page tables */
Michel Thierry6ac18502015-07-29 17:23:46 +0100932 if (test_bit(pde, pd->used_pdes)) {
Michel Thierryd7b26332015-04-08 12:13:34 +0100933 /* Scratch is never allocated this way */
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100934 WARN_ON(pt == vm->scratch_pt);
Michel Thierryd7b26332015-04-08 12:13:34 +0100935 continue;
936 }
937
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300938 pt = alloc_pt(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +0100939 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +0000940 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100941
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100942 gen8_initialize_pt(vm, pt);
Michel Thierryd7b26332015-04-08 12:13:34 +0100943 pd->page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +0300944 __set_bit(pde, new_pts);
Michel Thierry4c06ec82015-07-29 17:23:49 +0100945 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000946 }
947
948 return 0;
949
950unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +0100951 for_each_set_bit(pde, new_pts, I915_PDES)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +0300952 free_pt(dev, pd->page_table[pde]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000953
954 return -ENOMEM;
955}
956
Michel Thierryd7b26332015-04-08 12:13:34 +0100957/**
958 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100959 * @vm: Master vm structure.
Michel Thierryd7b26332015-04-08 12:13:34 +0100960 * @pdp: Page directory pointer for this address range.
961 * @start: Starting virtual address to begin allocations.
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100962 * @length: Size of the allocations.
963 * @new_pds: Bitmap set by function with new allocations. Likely used by the
Michel Thierryd7b26332015-04-08 12:13:34 +0100964 * caller to free on error.
965 *
966 * Allocate the required number of page directories starting at the pde index of
967 * @start, and ending at the pde index @start + @length. This function will skip
968 * over already allocated page directories within the range, and only allocate
969 * new ones, setting the appropriate pointer within the pdp as well as the
970 * correct position in the bitmap @new_pds.
971 *
972 * The function will only allocate the pages within the range for a give page
973 * directory pointer. In other words, if @start + @length straddles a virtually
974 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
975 * required by the caller, This is not currently possible, and the BUG in the
976 * code will prevent it.
977 *
978 * Return: 0 if success; negative error code otherwise.
979 */
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100980static int
981gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
982 struct i915_page_directory_pointer *pdp,
983 uint64_t start,
984 uint64_t length,
985 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800986{
Michel Thierryd4ec9da2015-07-30 11:02:03 +0100987 struct drm_device *dev = vm->dev;
Michel Thierryd7b26332015-04-08 12:13:34 +0100988 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +0100989 uint64_t temp;
990 uint32_t pdpe;
Michel Thierry6ac18502015-07-29 17:23:46 +0100991 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800992
Michel Thierry6ac18502015-07-29 17:23:46 +0100993 WARN_ON(!bitmap_empty(new_pds, pdpes));
Michel Thierryd7b26332015-04-08 12:13:34 +0100994
Michel Thierryd7b26332015-04-08 12:13:34 +0100995 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
Michel Thierry6ac18502015-07-29 17:23:46 +0100996 if (test_bit(pdpe, pdp->used_pdpes))
Michel Thierryd7b26332015-04-08 12:13:34 +0100997 continue;
Michel Thierry33c88192015-04-08 12:13:33 +0100998
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +0300999 pd = alloc_pd(dev);
Michel Thierryd7b26332015-04-08 12:13:34 +01001000 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001001 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +01001002
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001003 gen8_initialize_pd(vm, pd);
Michel Thierryd7b26332015-04-08 12:13:34 +01001004 pdp->page_directory[pdpe] = pd;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001005 __set_bit(pdpe, new_pds);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001006 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001007 }
1008
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001009 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001010
1011unwind_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001012 for_each_set_bit(pdpe, new_pds, pdpes)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001013 free_pd(dev, pdp->page_directory[pdpe]);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001014
1015 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001016}
1017
Michel Thierry762d9932015-07-30 11:05:29 +01001018/**
1019 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1020 * @vm: Master vm structure.
1021 * @pml4: Page map level 4 for this address range.
1022 * @start: Starting virtual address to begin allocations.
1023 * @length: Size of the allocations.
1024 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1025 * caller to free on error.
1026 *
1027 * Allocate the required number of page directory pointers. Extremely similar to
1028 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1029 * The main difference is here we are limited by the pml4 boundary (instead of
1030 * the page directory pointer).
1031 *
1032 * Return: 0 if success; negative error code otherwise.
1033 */
1034static int
1035gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1036 struct i915_pml4 *pml4,
1037 uint64_t start,
1038 uint64_t length,
1039 unsigned long *new_pdps)
1040{
1041 struct drm_device *dev = vm->dev;
1042 struct i915_page_directory_pointer *pdp;
1043 uint64_t temp;
1044 uint32_t pml4e;
1045
1046 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1047
1048 gen8_for_each_pml4e(pdp, pml4, start, length, temp, pml4e) {
1049 if (!test_bit(pml4e, pml4->used_pml4es)) {
1050 pdp = alloc_pdp(dev);
1051 if (IS_ERR(pdp))
1052 goto unwind_out;
1053
1054 pml4->pdps[pml4e] = pdp;
1055 __set_bit(pml4e, new_pdps);
1056 trace_i915_page_directory_pointer_entry_alloc(vm,
1057 pml4e,
1058 start,
1059 GEN8_PML4E_SHIFT);
1060 }
1061 }
1062
1063 return 0;
1064
1065unwind_out:
1066 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1067 free_pdp(dev, pml4->pdps[pml4e]);
1068
1069 return -ENOMEM;
1070}
1071
Michel Thierryd7b26332015-04-08 12:13:34 +01001072static void
Michel Thierry6ac18502015-07-29 17:23:46 +01001073free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long **new_pts,
1074 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001075{
1076 int i;
1077
Michel Thierry6ac18502015-07-29 17:23:46 +01001078 for (i = 0; i < pdpes; i++)
Michel Thierryd7b26332015-04-08 12:13:34 +01001079 kfree(new_pts[i]);
1080 kfree(new_pts);
1081 kfree(new_pds);
1082}
1083
1084/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1085 * of these are based on the number of PDPEs in the system.
1086 */
1087static
1088int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
Michel Thierry6ac18502015-07-29 17:23:46 +01001089 unsigned long ***new_pts,
1090 uint32_t pdpes)
Michel Thierryd7b26332015-04-08 12:13:34 +01001091{
1092 int i;
1093 unsigned long *pds;
1094 unsigned long **pts;
1095
Michel Thierry6ac18502015-07-29 17:23:46 +01001096 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_KERNEL);
Michel Thierryd7b26332015-04-08 12:13:34 +01001097 if (!pds)
1098 return -ENOMEM;
1099
Michel Thierry6ac18502015-07-29 17:23:46 +01001100 pts = kcalloc(pdpes, sizeof(unsigned long *), GFP_KERNEL);
Michel Thierryd7b26332015-04-08 12:13:34 +01001101 if (!pts) {
1102 kfree(pds);
1103 return -ENOMEM;
1104 }
1105
Michel Thierry6ac18502015-07-29 17:23:46 +01001106 for (i = 0; i < pdpes; i++) {
Michel Thierryd7b26332015-04-08 12:13:34 +01001107 pts[i] = kcalloc(BITS_TO_LONGS(I915_PDES),
1108 sizeof(unsigned long), GFP_KERNEL);
1109 if (!pts[i])
1110 goto err_out;
1111 }
1112
1113 *new_pds = pds;
1114 *new_pts = pts;
1115
1116 return 0;
1117
1118err_out:
Michel Thierry6ac18502015-07-29 17:23:46 +01001119 free_gen8_temp_bitmaps(pds, pts, pdpes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001120 return -ENOMEM;
1121}
1122
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001123/* PDE TLBs are a pain to invalidate on GEN8+. When we modify
1124 * the page table structures, we mark them dirty so that
1125 * context switching/execlist queuing code takes extra steps
1126 * to ensure that tlbs are flushed.
1127 */
1128static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1129{
1130 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1131}
1132
Michel Thierry762d9932015-07-30 11:05:29 +01001133static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1134 struct i915_page_directory_pointer *pdp,
1135 uint64_t start,
1136 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001137{
Michel Thierrye5815a22015-04-08 12:13:32 +01001138 struct i915_hw_ppgtt *ppgtt =
1139 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierryd7b26332015-04-08 12:13:34 +01001140 unsigned long *new_page_dirs, **new_page_tables;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001141 struct drm_device *dev = vm->dev;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001142 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +01001143 const uint64_t orig_start = start;
1144 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001145 uint64_t temp;
1146 uint32_t pdpe;
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001147 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001148 int ret;
1149
Michel Thierryd7b26332015-04-08 12:13:34 +01001150 /* Wrap is never okay since we can only represent 48b, and we don't
1151 * actually use the other side of the canonical address space.
1152 */
1153 if (WARN_ON(start + length < start))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001154 return -ENODEV;
1155
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001156 if (WARN_ON(start + length > vm->total))
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001157 return -ENODEV;
Michel Thierryd7b26332015-04-08 12:13:34 +01001158
Michel Thierry6ac18502015-07-29 17:23:46 +01001159 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001160 if (ret)
1161 return ret;
1162
Michel Thierryd7b26332015-04-08 12:13:34 +01001163 /* Do the allocations first so we can easily bail out */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001164 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1165 new_page_dirs);
Michel Thierryd7b26332015-04-08 12:13:34 +01001166 if (ret) {
Michel Thierry6ac18502015-07-29 17:23:46 +01001167 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables, pdpes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001168 return ret;
1169 }
1170
1171 /* For every page directory referenced, allocate page tables */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001172 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
1173 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
Michel Thierryd7b26332015-04-08 12:13:34 +01001174 new_page_tables[pdpe]);
Michel Thierry5441f0c2015-04-08 12:13:28 +01001175 if (ret)
1176 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +01001177 }
1178
Michel Thierry33c88192015-04-08 12:13:33 +01001179 start = orig_start;
1180 length = orig_length;
1181
Michel Thierryd7b26332015-04-08 12:13:34 +01001182 /* Allocations have completed successfully, so set the bitmaps, and do
1183 * the mappings. */
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001184 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001185 gen8_pde_t *const page_directory = kmap_px(pd);
Michel Thierry33c88192015-04-08 12:13:33 +01001186 struct i915_page_table *pt;
Michel Thierry09120d42015-07-29 17:23:45 +01001187 uint64_t pd_len = length;
Michel Thierry33c88192015-04-08 12:13:33 +01001188 uint64_t pd_start = start;
1189 uint32_t pde;
1190
Michel Thierryd7b26332015-04-08 12:13:34 +01001191 /* Every pd should be allocated, we just did that above. */
1192 WARN_ON(!pd);
1193
1194 gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) {
1195 /* Same reasoning as pd */
1196 WARN_ON(!pt);
1197 WARN_ON(!pd_len);
1198 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1199
1200 /* Set our used ptes within the page table */
1201 bitmap_set(pt->used_ptes,
1202 gen8_pte_index(pd_start),
1203 gen8_pte_count(pd_start, pd_len));
1204
1205 /* Our pde is now pointing to the pagetable, pt */
Mika Kuoppala966082c2015-06-25 18:35:19 +03001206 __set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +01001207
1208 /* Map the PDE to the page table */
Mika Kuoppalafe36f552015-06-25 18:35:16 +03001209 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1210 I915_CACHE_LLC);
Michel Thierry4c06ec82015-07-29 17:23:49 +01001211 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1212 gen8_pte_index(start),
1213 gen8_pte_count(start, length),
1214 GEN8_PTES);
Michel Thierryd7b26332015-04-08 12:13:34 +01001215
1216 /* NB: We haven't yet mapped ptes to pages. At this
1217 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +01001218 }
Michel Thierryd7b26332015-04-08 12:13:34 +01001219
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001220 kunmap_px(ppgtt, page_directory);
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001221 __set_bit(pdpe, pdp->used_pdpes);
Michel Thierry762d9932015-07-30 11:05:29 +01001222 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
Michel Thierry33c88192015-04-08 12:13:33 +01001223 }
1224
Michel Thierry6ac18502015-07-29 17:23:46 +01001225 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables, pdpes);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001226 mark_tlbs_dirty(ppgtt);
Ben Widawskyd7b3de92015-02-24 16:22:34 +00001227 return 0;
1228
1229err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +01001230 while (pdpe--) {
1231 for_each_set_bit(temp, new_page_tables[pdpe], I915_PDES)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001232 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001233 }
1234
Michel Thierry6ac18502015-07-29 17:23:46 +01001235 for_each_set_bit(pdpe, new_page_dirs, pdpes)
Michel Thierryd4ec9da2015-07-30 11:02:03 +01001236 free_pd(dev, pdp->page_directory[pdpe]);
Michel Thierryd7b26332015-04-08 12:13:34 +01001237
Michel Thierry6ac18502015-07-29 17:23:46 +01001238 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables, pdpes);
Mika Kuoppala5b7e4c9c2015-06-25 18:35:03 +03001239 mark_tlbs_dirty(ppgtt);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -08001240 return ret;
1241}
1242
Michel Thierry762d9932015-07-30 11:05:29 +01001243static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1244 struct i915_pml4 *pml4,
1245 uint64_t start,
1246 uint64_t length)
1247{
1248 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
1249 struct i915_hw_ppgtt *ppgtt =
1250 container_of(vm, struct i915_hw_ppgtt, base);
1251 struct i915_page_directory_pointer *pdp;
1252 uint64_t temp, pml4e;
1253 int ret = 0;
1254
1255 /* Do the pml4 allocations first, so we don't need to track the newly
1256 * allocated tables below the pdp */
1257 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1258
1259 /* The pagedirectory and pagetable allocations are done in the shared 3
1260 * and 4 level code. Just allocate the pdps.
1261 */
1262 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1263 new_pdps);
1264 if (ret)
1265 return ret;
1266
1267 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1268 "The allocation has spanned more than 512GB. "
1269 "It is highly likely this is incorrect.");
1270
1271 gen8_for_each_pml4e(pdp, pml4, start, length, temp, pml4e) {
1272 WARN_ON(!pdp);
1273
1274 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1275 if (ret)
1276 goto err_out;
1277
1278 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1279 }
1280
1281 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1282 GEN8_PML4ES_PER_PML4);
1283
1284 return 0;
1285
1286err_out:
1287 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1288 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
1289
1290 return ret;
1291}
1292
1293static int gen8_alloc_va_range(struct i915_address_space *vm,
1294 uint64_t start, uint64_t length)
1295{
1296 struct i915_hw_ppgtt *ppgtt =
1297 container_of(vm, struct i915_hw_ppgtt, base);
1298
1299 if (USES_FULL_48BIT_PPGTT(vm->dev))
1300 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1301 else
1302 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1303}
1304
Daniel Vettereb0b44a2015-03-18 14:47:59 +01001305/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001306 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1307 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1308 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1309 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -08001310 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -08001311 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001312static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -08001313{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001314 int ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001315
Mika Kuoppala8776f022015-06-30 18:16:40 +03001316 ret = gen8_init_scratch(&ppgtt->base);
1317 if (ret)
1318 return ret;
Michel Thierry69876be2015-04-08 12:13:27 +01001319
Michel Thierryd7b26332015-04-08 12:13:34 +01001320 ppgtt->base.start = 0;
Michel Thierryd7b26332015-04-08 12:13:34 +01001321 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001322 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +01001323 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +02001324 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001325 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1326 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryd7b26332015-04-08 12:13:34 +01001327
Michel Thierry762d9932015-07-30 11:05:29 +01001328 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
1329 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
1330 if (ret)
1331 goto free_scratch;
Michel Thierry6ac18502015-07-29 17:23:46 +01001332
Michel Thierry762d9932015-07-30 11:05:29 +01001333 ppgtt->base.total = 1ULL << 48;
Michel Thierry2dba3232015-07-30 11:06:23 +01001334 ppgtt->switch_mm = gen8_48b_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001335 } else {
1336 ret = __pdp_init(false, &ppgtt->pdp);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001337 if (ret)
1338 goto free_scratch;
1339
1340 ppgtt->base.total = 1ULL << 32;
1341 if (IS_ENABLED(CONFIG_X86_32))
1342 /* While we have a proliferation of size_t variables
1343 * we cannot represent the full ppgtt size on 32bit,
1344 * so limit it to the same size as the GGTT (currently
1345 * 2GiB).
1346 */
1347 ppgtt->base.total = to_i915(ppgtt->base.dev)->gtt.base.total;
Michel Thierry762d9932015-07-30 11:05:29 +01001348
Michel Thierry2dba3232015-07-30 11:06:23 +01001349 ppgtt->switch_mm = gen8_legacy_mm_switch;
Michel Thierry762d9932015-07-30 11:05:29 +01001350 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1351 0, 0,
1352 GEN8_PML4E_SHIFT);
Michel Thierry81ba8aef2015-08-03 09:52:01 +01001353 }
Michel Thierry6ac18502015-07-29 17:23:46 +01001354
Michel Thierryd7b26332015-04-08 12:13:34 +01001355 return 0;
Michel Thierry6ac18502015-07-29 17:23:46 +01001356
1357free_scratch:
1358 gen8_free_scratch(&ppgtt->base);
1359 return ret;
Michel Thierryd7b26332015-04-08 12:13:34 +01001360}
1361
Ben Widawsky87d60b62013-12-06 14:11:29 -08001362static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1363{
Ben Widawsky87d60b62013-12-06 14:11:29 -08001364 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +01001365 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +00001366 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001367 uint32_t pd_entry;
Michel Thierry09942c62015-04-08 12:13:30 +01001368 uint32_t pte, pde, temp;
1369 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -08001370
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001371 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1372 I915_CACHE_LLC, true, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001373
Michel Thierry09942c62015-04-08 12:13:30 +01001374 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001375 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +00001376 gen6_pte_t *pt_vaddr;
Mika Kuoppala567047b2015-06-25 18:35:12 +03001377 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
Michel Thierry09942c62015-04-08 12:13:30 +01001378 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001379 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1380
1381 if (pd_entry != expected)
1382 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1383 pde,
1384 pd_entry,
1385 expected);
1386 seq_printf(m, "\tPDE: %x\n", pd_entry);
1387
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001388 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1389
Michel Thierry07749ef2015-03-16 16:00:54 +00001390 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -08001391 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +00001392 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001393 (pte * PAGE_SIZE);
1394 int i;
1395 bool found = false;
1396 for (i = 0; i < 4; i++)
1397 if (pt_vaddr[pte + i] != scratch_pte)
1398 found = true;
1399 if (!found)
1400 continue;
1401
1402 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1403 for (i = 0; i < 4; i++) {
1404 if (pt_vaddr[pte + i] != scratch_pte)
1405 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1406 else
1407 seq_puts(m, " SCRATCH ");
1408 }
1409 seq_puts(m, "\n");
1410 }
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001411 kunmap_px(ppgtt, pt_vaddr);
Ben Widawsky87d60b62013-12-06 14:11:29 -08001412 }
1413}
1414
Ben Widawsky678d96f2015-03-16 16:00:56 +00001415/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001416static void gen6_write_pde(struct i915_page_directory *pd,
1417 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001418{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001419 /* Caller needs to make sure the write completes if necessary */
1420 struct i915_hw_ppgtt *ppgtt =
1421 container_of(pd, struct i915_hw_ppgtt, pd);
1422 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001423
Mika Kuoppala567047b2015-06-25 18:35:12 +03001424 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
Ben Widawsky678d96f2015-03-16 16:00:56 +00001425 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001426
Ben Widawsky678d96f2015-03-16 16:00:56 +00001427 writel(pd_entry, ppgtt->pd_addr + pde);
1428}
Ben Widawsky61973492013-04-08 18:43:54 -07001429
Ben Widawsky678d96f2015-03-16 16:00:56 +00001430/* Write all the page tables found in the ppgtt structure to incrementing page
1431 * directories. */
1432static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001433 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001434 uint32_t start, uint32_t length)
1435{
Michel Thierryec565b32015-04-08 12:13:23 +01001436 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001437 uint32_t pde, temp;
1438
1439 gen6_for_each_pde(pt, pd, start, length, temp, pde)
1440 gen6_write_pde(pd, pde, pt);
1441
1442 /* Make sure write is complete before other code can use this page
1443 * table. Also require for WC mapped PTEs */
1444 readl(dev_priv->gtt.gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001445}
1446
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001447static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001448{
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001449 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001450
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001451 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001452}
Ben Widawsky61973492013-04-08 18:43:54 -07001453
Ben Widawsky90252e52013-12-06 14:11:12 -08001454static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001455 struct drm_i915_gem_request *req)
Ben Widawsky90252e52013-12-06 14:11:12 -08001456{
John Harrisone85b26d2015-05-29 17:43:56 +01001457 struct intel_engine_cs *ring = req->ring;
Ben Widawsky90252e52013-12-06 14:11:12 -08001458 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001459
Ben Widawsky90252e52013-12-06 14:11:12 -08001460 /* NB: TLBs must be flushed and invalidated before a switch */
John Harrisona84c3ae2015-05-29 17:43:57 +01001461 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Ben Widawsky90252e52013-12-06 14:11:12 -08001462 if (ret)
1463 return ret;
1464
John Harrison5fb9de12015-05-29 17:44:07 +01001465 ret = intel_ring_begin(req, 6);
Ben Widawsky90252e52013-12-06 14:11:12 -08001466 if (ret)
1467 return ret;
1468
1469 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1470 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1471 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1472 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1473 intel_ring_emit(ring, get_pd_offset(ppgtt));
1474 intel_ring_emit(ring, MI_NOOP);
1475 intel_ring_advance(ring);
1476
1477 return 0;
1478}
1479
Yu Zhang71ba2d62015-02-10 19:05:54 +08001480static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001481 struct drm_i915_gem_request *req)
Yu Zhang71ba2d62015-02-10 19:05:54 +08001482{
John Harrisone85b26d2015-05-29 17:43:56 +01001483 struct intel_engine_cs *ring = req->ring;
Yu Zhang71ba2d62015-02-10 19:05:54 +08001484 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1485
1486 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1487 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1488 return 0;
1489}
1490
Ben Widawsky48a10382013-12-06 14:11:11 -08001491static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001492 struct drm_i915_gem_request *req)
Ben Widawsky48a10382013-12-06 14:11:11 -08001493{
John Harrisone85b26d2015-05-29 17:43:56 +01001494 struct intel_engine_cs *ring = req->ring;
Ben Widawsky48a10382013-12-06 14:11:11 -08001495 int ret;
1496
Ben Widawsky48a10382013-12-06 14:11:11 -08001497 /* NB: TLBs must be flushed and invalidated before a switch */
John Harrisona84c3ae2015-05-29 17:43:57 +01001498 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Ben Widawsky48a10382013-12-06 14:11:11 -08001499 if (ret)
1500 return ret;
1501
John Harrison5fb9de12015-05-29 17:44:07 +01001502 ret = intel_ring_begin(req, 6);
Ben Widawsky48a10382013-12-06 14:11:11 -08001503 if (ret)
1504 return ret;
1505
1506 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1507 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1508 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1509 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1510 intel_ring_emit(ring, get_pd_offset(ppgtt));
1511 intel_ring_emit(ring, MI_NOOP);
1512 intel_ring_advance(ring);
1513
Ben Widawsky90252e52013-12-06 14:11:12 -08001514 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1515 if (ring->id != RCS) {
John Harrisona84c3ae2015-05-29 17:43:57 +01001516 ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
Ben Widawsky90252e52013-12-06 14:11:12 -08001517 if (ret)
1518 return ret;
1519 }
1520
Ben Widawsky48a10382013-12-06 14:11:11 -08001521 return 0;
1522}
1523
Ben Widawskyeeb94882013-12-06 14:11:10 -08001524static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
John Harrisone85b26d2015-05-29 17:43:56 +01001525 struct drm_i915_gem_request *req)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001526{
John Harrisone85b26d2015-05-29 17:43:56 +01001527 struct intel_engine_cs *ring = req->ring;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001528 struct drm_device *dev = ppgtt->base.dev;
1529 struct drm_i915_private *dev_priv = dev->dev_private;
1530
Ben Widawsky48a10382013-12-06 14:11:11 -08001531
Ben Widawskyeeb94882013-12-06 14:11:10 -08001532 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1533 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1534
1535 POSTING_READ(RING_PP_DIR_DCLV(ring));
1536
1537 return 0;
1538}
1539
Daniel Vetter82460d92014-08-06 20:19:53 +02001540static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001541{
Ben Widawskyeeb94882013-12-06 14:11:10 -08001542 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001543 struct intel_engine_cs *ring;
Daniel Vetter82460d92014-08-06 20:19:53 +02001544 int j;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001545
1546 for_each_ring(ring, dev_priv, j) {
Michel Thierry2dba3232015-07-30 11:06:23 +01001547 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001548 I915_WRITE(RING_MODE_GEN7(ring),
Michel Thierry2dba3232015-07-30 11:06:23 +01001549 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001550 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001551}
1552
Daniel Vetter82460d92014-08-06 20:19:53 +02001553static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001554{
Jani Nikula50227e12014-03-31 14:27:21 +03001555 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001556 struct intel_engine_cs *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001557 uint32_t ecochk, ecobits;
1558 int i;
1559
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001560 ecobits = I915_READ(GAC_ECO_BITS);
1561 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1562
1563 ecochk = I915_READ(GAM_ECOCHK);
1564 if (IS_HASWELL(dev)) {
1565 ecochk |= ECOCHK_PPGTT_WB_HSW;
1566 } else {
1567 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1568 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1569 }
1570 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001571
Ben Widawsky61973492013-04-08 18:43:54 -07001572 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001573 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001574 I915_WRITE(RING_MODE_GEN7(ring),
1575 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001576 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001577}
1578
Daniel Vetter82460d92014-08-06 20:19:53 +02001579static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001580{
Jani Nikula50227e12014-03-31 14:27:21 +03001581 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001582 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001583
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001584 ecobits = I915_READ(GAC_ECO_BITS);
1585 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1586 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001587
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001588 gab_ctl = I915_READ(GAB_CTL);
1589 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001590
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001591 ecochk = I915_READ(GAM_ECOCHK);
1592 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001593
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001594 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001595}
1596
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001597/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001598static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001599 uint64_t start,
1600 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001601 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001602{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001603 struct i915_hw_ppgtt *ppgtt =
1604 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +00001605 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001606 unsigned first_entry = start >> PAGE_SHIFT;
1607 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001608 unsigned act_pt = first_entry / GEN6_PTES;
1609 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001610 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001611
Mika Kuoppalac114f762015-06-25 18:35:13 +03001612 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1613 I915_CACHE_LLC, true, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001614
Daniel Vetter7bddb012012-02-09 17:15:47 +01001615 while (num_entries) {
1616 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001617 if (last_pte > GEN6_PTES)
1618 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001619
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001620 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001621
1622 for (i = first_pte; i < last_pte; i++)
1623 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001624
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001625 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001626
Daniel Vetter7bddb012012-02-09 17:15:47 +01001627 num_entries -= last_pte - first_pte;
1628 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001629 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001630 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001631}
1632
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001633static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001634 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001635 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301636 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001637{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001638 struct i915_hw_ppgtt *ppgtt =
1639 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +00001640 gen6_pte_t *pt_vaddr;
Ben Widawsky782f1492014-02-20 11:50:33 -08001641 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001642 unsigned act_pt = first_entry / GEN6_PTES;
1643 unsigned act_pte = first_entry % GEN6_PTES;
Imre Deak6e995e22013-02-18 19:28:04 +02001644 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001645
Chris Wilsoncc797142013-12-31 15:50:30 +00001646 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +02001647 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001648 if (pt_vaddr == NULL)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001649 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001650
Chris Wilsoncc797142013-12-31 15:50:30 +00001651 pt_vaddr[act_pte] =
1652 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
Akash Goel24f3a8c2014-06-17 10:59:42 +05301653 cache_level, true, flags);
1654
Michel Thierry07749ef2015-03-16 16:00:54 +00001655 if (++act_pte == GEN6_PTES) {
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001656 kunmap_px(ppgtt, pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001657 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001658 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001659 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001660 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001661 }
Chris Wilsoncc797142013-12-31 15:50:30 +00001662 if (pt_vaddr)
Mika Kuoppalad1c54ac2015-06-25 18:35:11 +03001663 kunmap_px(ppgtt, pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001664}
1665
Ben Widawsky678d96f2015-03-16 16:00:56 +00001666static int gen6_alloc_va_range(struct i915_address_space *vm,
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001667 uint64_t start_in, uint64_t length_in)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001668{
Michel Thierry4933d512015-03-24 15:46:22 +00001669 DECLARE_BITMAP(new_page_tables, I915_PDES);
1670 struct drm_device *dev = vm->dev;
1671 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001672 struct i915_hw_ppgtt *ppgtt =
1673 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierryec565b32015-04-08 12:13:23 +01001674 struct i915_page_table *pt;
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001675 uint32_t start, length, start_save, length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001676 uint32_t pde, temp;
Michel Thierry4933d512015-03-24 15:46:22 +00001677 int ret;
1678
Mika Kuoppalaa05d80e2015-06-25 18:35:04 +03001679 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1680 return -ENODEV;
1681
1682 start = start_save = start_in;
1683 length = length_save = length_in;
Michel Thierry4933d512015-03-24 15:46:22 +00001684
1685 bitmap_zero(new_page_tables, I915_PDES);
1686
1687 /* The allocation is done in two stages so that we can bail out with
1688 * minimal amount of pain. The first stage finds new page tables that
1689 * need allocation. The second stage marks use ptes within the page
1690 * tables.
1691 */
1692 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001693 if (pt != vm->scratch_pt) {
Michel Thierry4933d512015-03-24 15:46:22 +00001694 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1695 continue;
1696 }
1697
1698 /* We've already allocated a page table */
1699 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1700
Mika Kuoppala8a1ebd72015-05-22 20:04:59 +03001701 pt = alloc_pt(dev);
Michel Thierry4933d512015-03-24 15:46:22 +00001702 if (IS_ERR(pt)) {
1703 ret = PTR_ERR(pt);
1704 goto unwind_out;
1705 }
1706
1707 gen6_initialize_pt(vm, pt);
1708
1709 ppgtt->pd.page_table[pde] = pt;
Mika Kuoppala966082c2015-06-25 18:35:19 +03001710 __set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001711 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001712 }
1713
1714 start = start_save;
1715 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001716
1717 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1718 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1719
1720 bitmap_zero(tmp_bitmap, GEN6_PTES);
1721 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1722 gen6_pte_count(start, length));
1723
Mika Kuoppala966082c2015-06-25 18:35:19 +03001724 if (__test_and_clear_bit(pde, new_page_tables))
Michel Thierry4933d512015-03-24 15:46:22 +00001725 gen6_write_pde(&ppgtt->pd, pde, pt);
1726
Michel Thierry72744cb2015-03-24 15:46:23 +00001727 trace_i915_page_table_entry_map(vm, pde, pt,
1728 gen6_pte_index(start),
1729 gen6_pte_count(start, length),
1730 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001731 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001732 GEN6_PTES);
1733 }
1734
Michel Thierry4933d512015-03-24 15:46:22 +00001735 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1736
1737 /* Make sure write is complete before other code can use this page
1738 * table. Also require for WC mapped PTEs */
1739 readl(dev_priv->gtt.gsm);
1740
Ben Widawsky563222a2015-03-19 12:53:28 +00001741 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001742 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001743
1744unwind_out:
1745 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001746 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00001747
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001748 ppgtt->pd.page_table[pde] = vm->scratch_pt;
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001749 free_pt(vm->dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001750 }
1751
1752 mark_tlbs_dirty(ppgtt);
1753 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001754}
1755
Mika Kuoppala8776f022015-06-30 18:16:40 +03001756static int gen6_init_scratch(struct i915_address_space *vm)
1757{
1758 struct drm_device *dev = vm->dev;
1759
1760 vm->scratch_page = alloc_scratch_page(dev);
1761 if (IS_ERR(vm->scratch_page))
1762 return PTR_ERR(vm->scratch_page);
1763
1764 vm->scratch_pt = alloc_pt(dev);
1765 if (IS_ERR(vm->scratch_pt)) {
1766 free_scratch_page(dev, vm->scratch_page);
1767 return PTR_ERR(vm->scratch_pt);
1768 }
1769
1770 gen6_initialize_pt(vm, vm->scratch_pt);
1771
1772 return 0;
1773}
1774
1775static void gen6_free_scratch(struct i915_address_space *vm)
1776{
1777 struct drm_device *dev = vm->dev;
1778
1779 free_pt(dev, vm->scratch_pt);
1780 free_scratch_page(dev, vm->scratch_page);
1781}
1782
Daniel Vetter061dd492015-04-14 17:35:13 +02001783static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08001784{
Daniel Vetter061dd492015-04-14 17:35:13 +02001785 struct i915_hw_ppgtt *ppgtt =
1786 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry09942c62015-04-08 12:13:30 +01001787 struct i915_page_table *pt;
1788 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08001789
Daniel Vetter061dd492015-04-14 17:35:13 +02001790 drm_mm_remove_node(&ppgtt->node);
1791
Michel Thierry09942c62015-04-08 12:13:30 +01001792 gen6_for_all_pdes(pt, ppgtt, pde) {
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001793 if (pt != vm->scratch_pt)
Mika Kuoppalaa08e1112015-06-25 18:35:08 +03001794 free_pt(ppgtt->base.dev, pt);
Michel Thierry4933d512015-03-24 15:46:22 +00001795 }
1796
Mika Kuoppala8776f022015-06-30 18:16:40 +03001797 gen6_free_scratch(vm);
Daniel Vetter3440d262013-01-24 13:49:56 -08001798}
1799
Ben Widawskyb1465202014-02-19 22:05:49 -08001800static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001801{
Mika Kuoppala8776f022015-06-30 18:16:40 +03001802 struct i915_address_space *vm = &ppgtt->base;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001803 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001804 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001805 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08001806 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001807
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001808 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1809 * allocator works in address space sizes, so it's multiplied by page
1810 * size. We allocate at the top of the GTT to avoid fragmentation.
1811 */
1812 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00001813
Mika Kuoppala8776f022015-06-30 18:16:40 +03001814 ret = gen6_init_scratch(vm);
1815 if (ret)
1816 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00001817
Ben Widawskye3cc1992013-12-06 14:11:08 -08001818alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001819 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1820 &ppgtt->node, GEN6_PD_SIZE,
1821 GEN6_PD_ALIGN, 0,
1822 0, dev_priv->gtt.base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07001823 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001824 if (ret == -ENOSPC && !retried) {
1825 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1826 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02001827 I915_CACHE_NONE,
1828 0, dev_priv->gtt.base.total,
1829 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001830 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001831 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001832
1833 retried = true;
1834 goto alloc;
1835 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001836
Ben Widawskyc8c26622015-01-22 17:01:25 +00001837 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001838 goto err_out;
1839
Ben Widawskyc8c26622015-01-22 17:01:25 +00001840
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001841 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1842 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001843
Ben Widawskyc8c26622015-01-22 17:01:25 +00001844 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001845
1846err_out:
Mika Kuoppala8776f022015-06-30 18:16:40 +03001847 gen6_free_scratch(vm);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001848 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08001849}
1850
Ben Widawskyb1465202014-02-19 22:05:49 -08001851static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1852{
kbuild test robot2f2cf682015-03-27 19:26:35 +08001853 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08001854}
1855
Michel Thierry4933d512015-03-24 15:46:22 +00001856static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
1857 uint64_t start, uint64_t length)
1858{
Michel Thierryec565b32015-04-08 12:13:23 +01001859 struct i915_page_table *unused;
Michel Thierry4933d512015-03-24 15:46:22 +00001860 uint32_t pde, temp;
1861
1862 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
Mika Kuoppala79ab9372015-06-25 18:35:17 +03001863 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
Michel Thierry4933d512015-03-24 15:46:22 +00001864}
1865
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001866static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08001867{
1868 struct drm_device *dev = ppgtt->base.dev;
1869 struct drm_i915_private *dev_priv = dev->dev_private;
1870 int ret;
1871
1872 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky48a10382013-12-06 14:11:11 -08001873 if (IS_GEN6(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001874 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -08001875 } else if (IS_HASWELL(dev)) {
Ben Widawsky90252e52013-12-06 14:11:12 -08001876 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -08001877 } else if (IS_GEN7(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001878 ppgtt->switch_mm = gen7_mm_switch;
1879 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001880 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001881
Yu Zhang71ba2d62015-02-10 19:05:54 +08001882 if (intel_vgpu_active(dev))
1883 ppgtt->switch_mm = vgpu_mm_switch;
1884
Ben Widawskyb1465202014-02-19 22:05:49 -08001885 ret = gen6_ppgtt_alloc(ppgtt);
1886 if (ret)
1887 return ret;
1888
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001889 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001890 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1891 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001892 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1893 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001894 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f6f2013-11-25 09:54:34 -08001895 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01001896 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001897 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001898
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001899 ppgtt->pd.base.ggtt_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00001900 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001901
Ben Widawsky678d96f2015-03-16 16:00:56 +00001902 ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001903 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001904
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001905 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001906
Ben Widawsky678d96f2015-03-16 16:00:56 +00001907 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
1908
Thierry Reding440fd522015-01-23 09:05:06 +01001909 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001910 ppgtt->node.size >> 20,
1911 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001912
Daniel Vetterfa76da32014-08-06 20:19:54 +02001913 DRM_DEBUG("Adding PPGTT at offset %x\n",
Mika Kuoppala44159dd2015-06-25 18:35:07 +03001914 ppgtt->pd.base.ggtt_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001915
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001916 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001917}
1918
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001919static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001920{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001921 ppgtt->base.dev = dev;
Daniel Vetter3440d262013-01-24 13:49:56 -08001922
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001923 if (INTEL_INFO(dev)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001924 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001925 else
Michel Thierryd7b26332015-04-08 12:13:34 +01001926 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001927}
Mika Kuoppalac114f762015-06-25 18:35:13 +03001928
Daniel Vetterfa76da32014-08-06 20:19:54 +02001929int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1930{
1931 struct drm_i915_private *dev_priv = dev->dev_private;
1932 int ret = 0;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001933
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001934 ret = __hw_ppgtt_init(dev, ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001935 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001936 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001937 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1938 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001939 i915_init_vm(dev_priv, &ppgtt->base);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001940 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001941
1942 return ret;
1943}
1944
Daniel Vetter82460d92014-08-06 20:19:53 +02001945int i915_ppgtt_init_hw(struct drm_device *dev)
1946{
Thomas Daniel671b50132014-08-20 16:24:50 +01001947 /* In the case of execlists, PPGTT is enabled by the context descriptor
1948 * and the PDPs are contained within the context itself. We don't
1949 * need to do anything here. */
1950 if (i915.enable_execlists)
1951 return 0;
1952
Daniel Vetter82460d92014-08-06 20:19:53 +02001953 if (!USES_PPGTT(dev))
1954 return 0;
1955
1956 if (IS_GEN6(dev))
1957 gen6_ppgtt_enable(dev);
1958 else if (IS_GEN7(dev))
1959 gen7_ppgtt_enable(dev);
1960 else if (INTEL_INFO(dev)->gen >= 8)
1961 gen8_ppgtt_enable(dev);
1962 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01001963 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02001964
John Harrison4ad2fd82015-06-18 13:11:20 +01001965 return 0;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001966}
John Harrison4ad2fd82015-06-18 13:11:20 +01001967
John Harrisonb3dd6b92015-05-29 17:43:40 +01001968int i915_ppgtt_init_ring(struct drm_i915_gem_request *req)
John Harrison4ad2fd82015-06-18 13:11:20 +01001969{
John Harrisonb3dd6b92015-05-29 17:43:40 +01001970 struct drm_i915_private *dev_priv = req->ring->dev->dev_private;
John Harrison4ad2fd82015-06-18 13:11:20 +01001971 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1972
1973 if (i915.enable_execlists)
1974 return 0;
1975
1976 if (!ppgtt)
1977 return 0;
1978
John Harrisone85b26d2015-05-29 17:43:56 +01001979 return ppgtt->switch_mm(ppgtt, req);
John Harrison4ad2fd82015-06-18 13:11:20 +01001980}
1981
Daniel Vetter4d884702014-08-06 15:04:47 +02001982struct i915_hw_ppgtt *
1983i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
1984{
1985 struct i915_hw_ppgtt *ppgtt;
1986 int ret;
1987
1988 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1989 if (!ppgtt)
1990 return ERR_PTR(-ENOMEM);
1991
1992 ret = i915_ppgtt_init(dev, ppgtt);
1993 if (ret) {
1994 kfree(ppgtt);
1995 return ERR_PTR(ret);
1996 }
1997
1998 ppgtt->file_priv = fpriv;
1999
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002000 trace_i915_ppgtt_create(&ppgtt->base);
2001
Daniel Vetter4d884702014-08-06 15:04:47 +02002002 return ppgtt;
2003}
2004
Daniel Vetteree960be2014-08-06 15:04:45 +02002005void i915_ppgtt_release(struct kref *kref)
2006{
2007 struct i915_hw_ppgtt *ppgtt =
2008 container_of(kref, struct i915_hw_ppgtt, ref);
2009
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00002010 trace_i915_ppgtt_release(&ppgtt->base);
2011
Daniel Vetteree960be2014-08-06 15:04:45 +02002012 /* vmas should already be unbound */
2013 WARN_ON(!list_empty(&ppgtt->base.active_list));
2014 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
2015
Daniel Vetter19dd1202014-08-06 15:04:55 +02002016 list_del(&ppgtt->base.global_link);
2017 drm_mm_takedown(&ppgtt->base.mm);
2018
Daniel Vetteree960be2014-08-06 15:04:45 +02002019 ppgtt->base.cleanup(&ppgtt->base);
2020 kfree(ppgtt);
2021}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002022
Ben Widawskya81cc002013-01-18 12:30:31 -08002023extern int intel_iommu_gfx_mapped;
2024/* Certain Gen5 chipsets require require idling the GPU before
2025 * unmapping anything from the GTT when VT-d is enabled.
2026 */
Daniel Vetter2c642b02015-04-14 17:35:26 +02002027static bool needs_idle_maps(struct drm_device *dev)
Ben Widawskya81cc002013-01-18 12:30:31 -08002028{
2029#ifdef CONFIG_INTEL_IOMMU
2030 /* Query intel_iommu to see if we need the workaround. Presumably that
2031 * was loaded first.
2032 */
2033 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
2034 return true;
2035#endif
2036 return false;
2037}
2038
Ben Widawsky5c042282011-10-17 15:51:55 -07002039static bool do_idling(struct drm_i915_private *dev_priv)
2040{
2041 bool ret = dev_priv->mm.interruptible;
2042
Ben Widawskya81cc002013-01-18 12:30:31 -08002043 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07002044 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002045 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07002046 DRM_ERROR("Couldn't idle GPU\n");
2047 /* Wait a bit, in hopes it avoids the hang */
2048 udelay(10);
2049 }
2050 }
2051
2052 return ret;
2053}
2054
2055static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
2056{
Ben Widawskya81cc002013-01-18 12:30:31 -08002057 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07002058 dev_priv->mm.interruptible = interruptible;
2059}
2060
Ben Widawsky828c7902013-10-16 09:21:30 -07002061void i915_check_and_clear_faults(struct drm_device *dev)
2062{
2063 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002064 struct intel_engine_cs *ring;
Ben Widawsky828c7902013-10-16 09:21:30 -07002065 int i;
2066
2067 if (INTEL_INFO(dev)->gen < 6)
2068 return;
2069
2070 for_each_ring(ring, dev_priv, i) {
2071 u32 fault_reg;
2072 fault_reg = I915_READ(RING_FAULT_REG(ring));
2073 if (fault_reg & RING_FAULT_VALID) {
2074 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02002075 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07002076 "\tAddress space: %s\n"
2077 "\tSource ID: %d\n"
2078 "\tType: %d\n",
2079 fault_reg & PAGE_MASK,
2080 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2081 RING_FAULT_SRCID(fault_reg),
2082 RING_FAULT_FAULT_TYPE(fault_reg));
2083 I915_WRITE(RING_FAULT_REG(ring),
2084 fault_reg & ~RING_FAULT_VALID);
2085 }
2086 }
2087 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
2088}
2089
Chris Wilson91e56492014-09-25 10:13:12 +01002090static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2091{
2092 if (INTEL_INFO(dev_priv->dev)->gen < 6) {
2093 intel_gtt_chipset_flush();
2094 } else {
2095 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2096 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2097 }
2098}
2099
Ben Widawsky828c7902013-10-16 09:21:30 -07002100void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2101{
2102 struct drm_i915_private *dev_priv = dev->dev_private;
2103
2104 /* Don't bother messing with faults pre GEN6 as we have little
2105 * documentation supporting that it's a good idea.
2106 */
2107 if (INTEL_INFO(dev)->gen < 6)
2108 return;
2109
2110 i915_check_and_clear_faults(dev);
2111
2112 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08002113 dev_priv->gtt.base.start,
2114 dev_priv->gtt.base.total,
Daniel Vettere568af12014-03-26 20:08:20 +01002115 true);
Chris Wilson91e56492014-09-25 10:13:12 +01002116
2117 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07002118}
2119
Daniel Vetter74163902012-02-15 23:50:21 +01002120int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002121{
Chris Wilson9da3da62012-06-01 15:20:22 +01002122 if (!dma_map_sg(&obj->base.dev->pdev->dev,
2123 obj->pages->sgl, obj->pages->nents,
2124 PCI_DMA_BIDIRECTIONAL))
2125 return -ENOSPC;
2126
2127 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002128}
2129
Daniel Vetter2c642b02015-04-14 17:35:26 +02002130static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002131{
2132#ifdef writeq
2133 writeq(pte, addr);
2134#else
2135 iowrite32((u32)pte, addr);
2136 iowrite32(pte >> 32, addr + 4);
2137#endif
2138}
2139
2140static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2141 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002142 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302143 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002144{
2145 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08002146 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002147 gen8_pte_t __iomem *gtt_entries =
2148 (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002149 int i = 0;
2150 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02002151 dma_addr_t addr = 0; /* shut up gcc */
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002152
2153 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
2154 addr = sg_dma_address(sg_iter.sg) +
2155 (sg_iter.sg_pgoffset << PAGE_SHIFT);
2156 gen8_set_pte(&gtt_entries[i],
2157 gen8_pte_encode(addr, level, true));
2158 i++;
2159 }
2160
2161 /*
2162 * XXX: This serves as a posting read to make sure that the PTE has
2163 * actually been updated. There is some concern that even though
2164 * registers and PTEs are within the same BAR that they are potentially
2165 * of NUMA access patterns. Therefore, even with the way we assume
2166 * hardware should work, we must keep this posting read for paranoia.
2167 */
2168 if (i != 0)
2169 WARN_ON(readq(&gtt_entries[i-1])
2170 != gen8_pte_encode(addr, level, true));
2171
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002172 /* This next bit makes the above posting read even more important. We
2173 * want to flush the TLBs only after we're certain all the PTE updates
2174 * have finished.
2175 */
2176 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2177 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002178}
2179
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002180/*
2181 * Binds an object into the global gtt with the specified cache level. The object
2182 * will be accessible to the GPU via commands whose operands reference offsets
2183 * within the global GTT as well as accessible by the GPU through the GMADR
2184 * mapped BAR (dev_priv->mm.gtt->gtt).
2185 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002186static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002187 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08002188 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05302189 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002190{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002191 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08002192 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002193 gen6_pte_t __iomem *gtt_entries =
2194 (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02002195 int i = 0;
2196 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02002197 dma_addr_t addr = 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002198
Imre Deak6e995e22013-02-18 19:28:04 +02002199 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02002200 addr = sg_page_iter_dma_address(&sg_iter);
Akash Goel24f3a8c2014-06-17 10:59:42 +05302201 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02002202 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002203 }
2204
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002205 /* XXX: This serves as a posting read to make sure that the PTE has
2206 * actually been updated. There is some concern that even though
2207 * registers and PTEs are within the same BAR that they are potentially
2208 * of NUMA access patterns. Therefore, even with the way we assume
2209 * hardware should work, we must keep this posting read for paranoia.
2210 */
Pavel Machek57007df2014-07-28 13:20:58 +02002211 if (i != 0) {
2212 unsigned long gtt = readl(&gtt_entries[i-1]);
2213 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
2214 }
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08002215
2216 /* This next bit makes the above posting read even more important. We
2217 * want to flush the TLBs only after we're certain all the PTE updates
2218 * have finished.
2219 */
2220 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2221 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002222}
2223
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002224static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002225 uint64_t start,
2226 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002227 bool use_scratch)
2228{
2229 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08002230 unsigned first_entry = start >> PAGE_SHIFT;
2231 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002232 gen8_pte_t scratch_pte, __iomem *gtt_base =
2233 (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002234 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
2235 int i;
2236
2237 if (WARN(num_entries > max_entries,
2238 "First entry = %d; Num entries = %d (max=%d)\n",
2239 first_entry, num_entries, max_entries))
2240 num_entries = max_entries;
2241
Mika Kuoppalac114f762015-06-25 18:35:13 +03002242 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002243 I915_CACHE_LLC,
2244 use_scratch);
2245 for (i = 0; i < num_entries; i++)
2246 gen8_set_pte(&gtt_base[i], scratch_pte);
2247 readl(gtt_base);
2248}
2249
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002250static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002251 uint64_t start,
2252 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07002253 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002254{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002255 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08002256 unsigned first_entry = start >> PAGE_SHIFT;
2257 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00002258 gen6_pte_t scratch_pte, __iomem *gtt_base =
2259 (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08002260 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002261 int i;
2262
2263 if (WARN(num_entries > max_entries,
2264 "First entry = %d; Num entries = %d (max=%d)\n",
2265 first_entry, num_entries, max_entries))
2266 num_entries = max_entries;
2267
Mika Kuoppalac114f762015-06-25 18:35:13 +03002268 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
2269 I915_CACHE_LLC, use_scratch, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07002270
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002271 for (i = 0; i < num_entries; i++)
2272 iowrite32(scratch_pte, &gtt_base[i]);
2273 readl(gtt_base);
2274}
2275
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002276static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2277 struct sg_table *pages,
2278 uint64_t start,
2279 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002280{
2281 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2282 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2283
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002284 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07002285
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002286}
2287
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002288static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08002289 uint64_t start,
2290 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07002291 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002292{
Ben Widawsky782f1492014-02-20 11:50:33 -08002293 unsigned first_entry = start >> PAGE_SHIFT;
2294 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002295 intel_gtt_clear_range(first_entry, num_entries);
2296}
2297
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002298static int ggtt_bind_vma(struct i915_vma *vma,
2299 enum i915_cache_level cache_level,
2300 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002301{
Ben Widawsky6f65e292013-12-06 14:10:56 -08002302 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002303 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002304 struct drm_i915_gem_object *obj = vma->obj;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002305 struct sg_table *pages = obj->pages;
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002306 u32 pte_flags = 0;
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002307 int ret;
2308
2309 ret = i915_get_ggtt_vma_pages(vma);
2310 if (ret)
2311 return ret;
2312 pages = vma->ggtt_view.pages;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002313
Akash Goel24f3a8c2014-06-17 10:59:42 +05302314 /* Currently applicable only to VLV */
2315 if (obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002316 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05302317
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002318
Ben Widawsky6f65e292013-12-06 14:10:56 -08002319 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
Daniel Vetter08755462015-04-20 09:04:05 -07002320 vma->vm->insert_entries(vma->vm, pages,
2321 vma->node.start,
2322 cache_level, pte_flags);
Chris Wilsond0e30ad2015-07-29 20:02:48 +01002323
2324 /* Note the inconsistency here is due to absence of the
2325 * aliasing ppgtt on gen4 and earlier. Though we always
2326 * request PIN_USER for execbuffer (translated to LOCAL_BIND),
2327 * without the appgtt, we cannot honour that request and so
2328 * must substitute it with a global binding. Since we do this
2329 * behind the upper layers back, we need to explicitly set
2330 * the bound flag ourselves.
2331 */
2332 vma->bound |= GLOBAL_BIND;
2333
Ben Widawsky6f65e292013-12-06 14:10:56 -08002334 }
Daniel Vetter74898d72012-02-15 23:50:22 +01002335
Daniel Vetter08755462015-04-20 09:04:05 -07002336 if (dev_priv->mm.aliasing_ppgtt && flags & LOCAL_BIND) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08002337 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002338 appgtt->base.insert_entries(&appgtt->base, pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08002339 vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02002340 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002341 }
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02002342
2343 return 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002344}
2345
2346static void ggtt_unbind_vma(struct i915_vma *vma)
2347{
2348 struct drm_device *dev = vma->vm->dev;
2349 struct drm_i915_private *dev_priv = dev->dev_private;
2350 struct drm_i915_gem_object *obj = vma->obj;
Joonas Lahtinen06615ee2015-04-24 15:09:03 +03002351 const uint64_t size = min_t(uint64_t,
2352 obj->base.size,
2353 vma->node.size);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002354
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002355 if (vma->bound & GLOBAL_BIND) {
Ben Widawsky782f1492014-02-20 11:50:33 -08002356 vma->vm->clear_range(vma->vm,
2357 vma->node.start,
Joonas Lahtinen06615ee2015-04-24 15:09:03 +03002358 size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08002359 true);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002360 }
2361
Daniel Vetter08755462015-04-20 09:04:05 -07002362 if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08002363 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinen06615ee2015-04-24 15:09:03 +03002364
Ben Widawsky6f65e292013-12-06 14:10:56 -08002365 appgtt->base.clear_range(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08002366 vma->node.start,
Joonas Lahtinen06615ee2015-04-24 15:09:03 +03002367 size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08002368 true);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002369 }
Daniel Vetter74163902012-02-15 23:50:21 +01002370}
2371
2372void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
2373{
Ben Widawsky5c042282011-10-17 15:51:55 -07002374 struct drm_device *dev = obj->base.dev;
2375 struct drm_i915_private *dev_priv = dev->dev_private;
2376 bool interruptible;
2377
2378 interruptible = do_idling(dev_priv);
2379
Imre Deak5ec5b512015-07-08 19:18:59 +03002380 dma_unmap_sg(&dev->pdev->dev, obj->pages->sgl, obj->pages->nents,
2381 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07002382
2383 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01002384}
Daniel Vetter644ec022012-03-26 09:45:40 +02002385
Chris Wilson42d6ab42012-07-26 11:49:32 +01002386static void i915_gtt_color_adjust(struct drm_mm_node *node,
2387 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01002388 u64 *start,
2389 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01002390{
2391 if (node->color != color)
2392 *start += 4096;
2393
2394 if (!list_empty(&node->node_list)) {
2395 node = list_entry(node->node_list.next,
2396 struct drm_mm_node,
2397 node_list);
2398 if (node->allocated && node->color != color)
2399 *end -= 4096;
2400 }
2401}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002402
Daniel Vetterf548c0e2014-11-19 21:40:13 +01002403static int i915_gem_setup_global_gtt(struct drm_device *dev,
2404 unsigned long start,
2405 unsigned long mappable_end,
2406 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02002407{
Ben Widawskye78891c2013-01-25 16:41:04 -08002408 /* Let GEM Manage all of the aperture.
2409 *
2410 * However, leave one page at the end still bound to the scratch page.
2411 * There are a number of places where the hardware apparently prefetches
2412 * past the end of the object, and we've seen multiple hangs with the
2413 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2414 * aperture. One page should be enough to keep any prefetching inside
2415 * of the aperture.
2416 */
Ben Widawsky40d749802013-07-31 16:59:59 -07002417 struct drm_i915_private *dev_priv = dev->dev_private;
2418 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002419 struct drm_mm_node *entry;
2420 struct drm_i915_gem_object *obj;
2421 unsigned long hole_start, hole_end;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002422 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002423
Ben Widawsky35451cb2013-01-17 12:45:13 -08002424 BUG_ON(mappable_end > end);
2425
Chris Wilsoned2f3452012-11-15 11:32:19 +00002426 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07002427 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002428
2429 dev_priv->gtt.base.start = start;
2430 dev_priv->gtt.base.total = end - start;
2431
2432 if (intel_vgpu_active(dev)) {
2433 ret = intel_vgt_balloon(dev);
2434 if (ret)
2435 return ret;
2436 }
2437
Chris Wilson42d6ab42012-07-26 11:49:32 +01002438 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07002439 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02002440
Chris Wilsoned2f3452012-11-15 11:32:19 +00002441 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07002442 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07002443 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002444
Ben Widawskyedd41a82013-07-05 14:41:05 -07002445 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07002446 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002447
Ben Widawskyc6cfb322013-07-05 14:41:06 -07002448 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07002449 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002450 if (ret) {
2451 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
2452 return ret;
2453 }
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002454 vma->bound |= GLOBAL_BIND;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002455 }
2456
Chris Wilsoned2f3452012-11-15 11:32:19 +00002457 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07002458 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002459 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2460 hole_start, hole_end);
Ben Widawsky782f1492014-02-20 11:50:33 -08002461 ggtt_vm->clear_range(ggtt_vm, hole_start,
2462 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002463 }
2464
2465 /* And finally clear the reserved guard page */
Ben Widawsky782f1492014-02-20 11:50:33 -08002466 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002467
Daniel Vetterfa76da32014-08-06 20:19:54 +02002468 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
2469 struct i915_hw_ppgtt *ppgtt;
2470
2471 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2472 if (!ppgtt)
2473 return -ENOMEM;
2474
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002475 ret = __hw_ppgtt_init(dev, ppgtt);
Michel Thierry4933d512015-03-24 15:46:22 +00002476 if (ret) {
Daniel Vetter061dd492015-04-14 17:35:13 +02002477 ppgtt->base.cleanup(&ppgtt->base);
Michel Thierry4933d512015-03-24 15:46:22 +00002478 kfree(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002479 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002480 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002481
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002482 if (ppgtt->base.allocate_va_range)
2483 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2484 ppgtt->base.total);
2485 if (ret) {
2486 ppgtt->base.cleanup(&ppgtt->base);
2487 kfree(ppgtt);
2488 return ret;
2489 }
2490
2491 ppgtt->base.clear_range(&ppgtt->base,
2492 ppgtt->base.start,
2493 ppgtt->base.total,
2494 true);
2495
Daniel Vetterfa76da32014-08-06 20:19:54 +02002496 dev_priv->mm.aliasing_ppgtt = ppgtt;
2497 }
2498
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002499 return 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002500}
2501
Ben Widawskyd7e50082012-12-18 10:31:25 -08002502void i915_gem_init_global_gtt(struct drm_device *dev)
2503{
2504 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002505 u64 gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08002506
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002507 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08002508 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08002509
Ben Widawskye78891c2013-01-25 16:41:04 -08002510 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002511}
2512
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002513void i915_global_gtt_cleanup(struct drm_device *dev)
2514{
2515 struct drm_i915_private *dev_priv = dev->dev_private;
2516 struct i915_address_space *vm = &dev_priv->gtt.base;
2517
Daniel Vetter70e32542014-08-06 15:04:57 +02002518 if (dev_priv->mm.aliasing_ppgtt) {
2519 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2520
2521 ppgtt->base.cleanup(&ppgtt->base);
2522 }
2523
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002524 if (drm_mm_initialized(&vm->mm)) {
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002525 if (intel_vgpu_active(dev))
2526 intel_vgt_deballoon();
2527
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002528 drm_mm_takedown(&vm->mm);
2529 list_del(&vm->global_link);
2530 }
2531
2532 vm->cleanup(vm);
2533}
Daniel Vetter70e32542014-08-06 15:04:57 +02002534
Daniel Vetter2c642b02015-04-14 17:35:26 +02002535static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002536{
2537 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2538 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2539 return snb_gmch_ctl << 20;
2540}
2541
Daniel Vetter2c642b02015-04-14 17:35:26 +02002542static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002543{
2544 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2545 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2546 if (bdw_gmch_ctl)
2547 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002548
2549#ifdef CONFIG_X86_32
2550 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2551 if (bdw_gmch_ctl > 4)
2552 bdw_gmch_ctl = 4;
2553#endif
2554
Ben Widawsky9459d252013-11-03 16:53:55 -08002555 return bdw_gmch_ctl << 20;
2556}
2557
Daniel Vetter2c642b02015-04-14 17:35:26 +02002558static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002559{
2560 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2561 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2562
2563 if (gmch_ctrl)
2564 return 1 << (20 + gmch_ctrl);
2565
2566 return 0;
2567}
2568
Daniel Vetter2c642b02015-04-14 17:35:26 +02002569static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002570{
2571 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2572 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2573 return snb_gmch_ctl << 25; /* 32 MB units */
2574}
2575
Daniel Vetter2c642b02015-04-14 17:35:26 +02002576static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
Ben Widawsky9459d252013-11-03 16:53:55 -08002577{
2578 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2579 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2580 return bdw_gmch_ctl << 25; /* 32 MB units */
2581}
2582
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002583static size_t chv_get_stolen_size(u16 gmch_ctrl)
2584{
2585 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2586 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2587
2588 /*
2589 * 0x0 to 0x10: 32MB increments starting at 0MB
2590 * 0x11 to 0x16: 4MB increments starting at 8MB
2591 * 0x17 to 0x1d: 4MB increments start at 36MB
2592 */
2593 if (gmch_ctrl < 0x11)
2594 return gmch_ctrl << 25;
2595 else if (gmch_ctrl < 0x17)
2596 return (gmch_ctrl - 0x11 + 2) << 22;
2597 else
2598 return (gmch_ctrl - 0x17 + 9) << 22;
2599}
2600
Damien Lespiau66375012014-01-09 18:02:46 +00002601static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2602{
2603 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2604 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2605
2606 if (gen9_gmch_ctl < 0xf0)
2607 return gen9_gmch_ctl << 25; /* 32 MB units */
2608 else
2609 /* 4MB increments starting at 0xf0 for 4MB */
2610 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2611}
2612
Ben Widawsky63340132013-11-04 19:32:22 -08002613static int ggtt_probe_common(struct drm_device *dev,
2614 size_t gtt_size)
2615{
2616 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002617 struct i915_page_scratch *scratch_page;
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002618 phys_addr_t gtt_phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08002619
2620 /* For Modern GENs the PTEs and register space are split in the BAR */
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002621 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
Ben Widawsky63340132013-11-04 19:32:22 -08002622 (pci_resource_len(dev->pdev, 0) / 2);
2623
Imre Deak2a073f892015-03-27 13:07:33 +02002624 /*
2625 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2626 * dropped. For WC mappings in general we have 64 byte burst writes
2627 * when the WC buffer is flushed, so we can't use it, but have to
2628 * resort to an uncached mapping. The WC issue is easily caught by the
2629 * readback check when writing GTT PTE entries.
2630 */
2631 if (IS_BROXTON(dev))
2632 dev_priv->gtt.gsm = ioremap_nocache(gtt_phys_addr, gtt_size);
2633 else
2634 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
Ben Widawsky63340132013-11-04 19:32:22 -08002635 if (!dev_priv->gtt.gsm) {
2636 DRM_ERROR("Failed to map the gtt page table\n");
2637 return -ENOMEM;
2638 }
2639
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002640 scratch_page = alloc_scratch_page(dev);
2641 if (IS_ERR(scratch_page)) {
Ben Widawsky63340132013-11-04 19:32:22 -08002642 DRM_ERROR("Scratch setup failed\n");
2643 /* iounmap will also get called at remove, but meh */
2644 iounmap(dev_priv->gtt.gsm);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002645 return PTR_ERR(scratch_page);
Ben Widawsky63340132013-11-04 19:32:22 -08002646 }
2647
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002648 dev_priv->gtt.base.scratch_page = scratch_page;
2649
2650 return 0;
Ben Widawsky63340132013-11-04 19:32:22 -08002651}
2652
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002653/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2654 * bits. When using advanced contexts each context stores its own PAT, but
2655 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002656static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002657{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002658 uint64_t pat;
2659
2660 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2661 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2662 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2663 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2664 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2665 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2666 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2667 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2668
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002669 if (!USES_PPGTT(dev_priv->dev))
2670 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2671 * so RTL will always use the value corresponding to
2672 * pat_sel = 000".
2673 * So let's disable cache for GGTT to avoid screen corruptions.
2674 * MOCS still can be used though.
2675 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2676 * before this patch, i.e. the same uncached + snooping access
2677 * like on gen6/7 seems to be in effect.
2678 * - So this just fixes blitter/render access. Again it looks
2679 * like it's not just uncached access, but uncached + snooping.
2680 * So we can still hold onto all our assumptions wrt cpu
2681 * clflushing on LLC machines.
2682 */
2683 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2684
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002685 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2686 * write would work. */
2687 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2688 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2689}
2690
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002691static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2692{
2693 uint64_t pat;
2694
2695 /*
2696 * Map WB on BDW to snooped on CHV.
2697 *
2698 * Only the snoop bit has meaning for CHV, the rest is
2699 * ignored.
2700 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002701 * The hardware will never snoop for certain types of accesses:
2702 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2703 * - PPGTT page tables
2704 * - some other special cycles
2705 *
2706 * As with BDW, we also need to consider the following for GT accesses:
2707 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2708 * so RTL will always use the value corresponding to
2709 * pat_sel = 000".
2710 * Which means we must set the snoop bit in PAT entry 0
2711 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002712 */
2713 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2714 GEN8_PPAT(1, 0) |
2715 GEN8_PPAT(2, 0) |
2716 GEN8_PPAT(3, 0) |
2717 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2718 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2719 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2720 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2721
2722 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2723 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2724}
2725
Ben Widawsky63340132013-11-04 19:32:22 -08002726static int gen8_gmch_probe(struct drm_device *dev,
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002727 u64 *gtt_total,
Ben Widawsky63340132013-11-04 19:32:22 -08002728 size_t *stolen,
2729 phys_addr_t *mappable_base,
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002730 u64 *mappable_end)
Ben Widawsky63340132013-11-04 19:32:22 -08002731{
2732 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002733 u64 gtt_size;
Ben Widawsky63340132013-11-04 19:32:22 -08002734 u16 snb_gmch_ctl;
2735 int ret;
2736
2737 /* TODO: We're not aware of mappable constraints on gen8 yet */
2738 *mappable_base = pci_resource_start(dev->pdev, 2);
2739 *mappable_end = pci_resource_len(dev->pdev, 2);
2740
2741 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
2742 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
2743
2744 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2745
Damien Lespiau66375012014-01-09 18:02:46 +00002746 if (INTEL_INFO(dev)->gen >= 9) {
2747 *stolen = gen9_get_stolen_size(snb_gmch_ctl);
2748 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2749 } else if (IS_CHERRYVIEW(dev)) {
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002750 *stolen = chv_get_stolen_size(snb_gmch_ctl);
2751 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
2752 } else {
2753 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
2754 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2755 }
Ben Widawsky63340132013-11-04 19:32:22 -08002756
Michel Thierry07749ef2015-03-16 16:00:54 +00002757 *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002758
Sumit Singh5a4e33a2015-03-17 11:39:31 +02002759 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002760 chv_setup_private_ppat(dev_priv);
2761 else
2762 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002763
Ben Widawsky63340132013-11-04 19:32:22 -08002764 ret = ggtt_probe_common(dev, gtt_size);
2765
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002766 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
2767 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002768 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
2769 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
Ben Widawsky63340132013-11-04 19:32:22 -08002770
2771 return ret;
2772}
2773
Ben Widawskybaa09f52013-01-24 13:49:57 -08002774static int gen6_gmch_probe(struct drm_device *dev,
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002775 u64 *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002776 size_t *stolen,
2777 phys_addr_t *mappable_base,
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002778 u64 *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002779{
2780 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002781 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002782 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002783 int ret;
2784
Ben Widawsky41907dd2013-02-08 11:32:47 -08002785 *mappable_base = pci_resource_start(dev->pdev, 2);
2786 *mappable_end = pci_resource_len(dev->pdev, 2);
2787
Ben Widawskybaa09f52013-01-24 13:49:57 -08002788 /* 64/512MB is the current min/max we actually know of, but this is just
2789 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002790 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08002791 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002792 DRM_ERROR("Unknown GMADR size (%llx)\n",
Ben Widawskybaa09f52013-01-24 13:49:57 -08002793 dev_priv->gtt.mappable_end);
2794 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002795 }
2796
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002797 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
2798 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08002799 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002800
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07002801 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002802
Ben Widawsky63340132013-11-04 19:32:22 -08002803 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Michel Thierry07749ef2015-03-16 16:00:54 +00002804 *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002805
Ben Widawsky63340132013-11-04 19:32:22 -08002806 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002807
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002808 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2809 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002810 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
2811 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002812
2813 return ret;
2814}
2815
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002816static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002817{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002818
2819 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08002820
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002821 iounmap(gtt->gsm);
Mika Kuoppala4ad2af12015-06-30 18:16:39 +03002822 free_scratch_page(vm->dev, vm->scratch_page);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002823}
2824
2825static int i915_gmch_probe(struct drm_device *dev,
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002826 u64 *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002827 size_t *stolen,
2828 phys_addr_t *mappable_base,
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002829 u64 *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002830{
2831 struct drm_i915_private *dev_priv = dev->dev_private;
2832 int ret;
2833
Ben Widawskybaa09f52013-01-24 13:49:57 -08002834 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2835 if (!ret) {
2836 DRM_ERROR("failed to set up gmch\n");
2837 return -EIO;
2838 }
2839
Ben Widawsky41907dd2013-02-08 11:32:47 -08002840 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002841
2842 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002843 dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002844 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002845 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
2846 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002847
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002848 if (unlikely(dev_priv->gtt.do_idle_maps))
2849 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2850
Ben Widawskybaa09f52013-01-24 13:49:57 -08002851 return 0;
2852}
2853
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002854static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002855{
2856 intel_gmch_remove();
2857}
2858
2859int i915_gem_gtt_init(struct drm_device *dev)
2860{
2861 struct drm_i915_private *dev_priv = dev->dev_private;
2862 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002863 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002864
Ben Widawskybaa09f52013-01-24 13:49:57 -08002865 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002866 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002867 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08002868 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002869 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002870 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002871 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002872 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002873 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002874 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002875 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002876 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01002877 else if (INTEL_INFO(dev)->gen >= 7)
2878 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002879 else
Chris Wilson350ec882013-08-06 13:17:02 +01002880 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08002881 } else {
2882 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2883 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002884 }
2885
Mika Kuoppalac114f762015-06-25 18:35:13 +03002886 gtt->base.dev = dev;
2887
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002888 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002889 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002890 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002891 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002892
Ben Widawskybaa09f52013-01-24 13:49:57 -08002893 /* GMADR is the PCI mmio aperture into the global GTT. */
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002894 DRM_INFO("Memory usable by graphics device = %lluM\n",
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002895 gtt->base.total >> 20);
Mika Kuoppalac44ef602015-06-25 18:35:05 +03002896 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", gtt->mappable_end >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002897 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002898#ifdef CONFIG_INTEL_IOMMU
2899 if (intel_iommu_gfx_mapped)
2900 DRM_INFO("VT-d active for gfx access\n");
2901#endif
Daniel Vettercfa7c862014-04-29 11:53:58 +02002902 /*
2903 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2904 * user's requested state against the hardware/driver capabilities. We
2905 * do this now so that we can print out any log messages once rather
2906 * than every time we check intel_enable_ppgtt().
2907 */
2908 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2909 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002910
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002911 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02002912}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002913
Daniel Vetterfa423312015-04-14 17:35:23 +02002914void i915_gem_restore_gtt_mappings(struct drm_device *dev)
2915{
2916 struct drm_i915_private *dev_priv = dev->dev_private;
2917 struct drm_i915_gem_object *obj;
2918 struct i915_address_space *vm;
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01002919 struct i915_vma *vma;
2920 bool flush;
Daniel Vetterfa423312015-04-14 17:35:23 +02002921
2922 i915_check_and_clear_faults(dev);
2923
2924 /* First fill our portion of the GTT with scratch pages */
2925 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
2926 dev_priv->gtt.base.start,
2927 dev_priv->gtt.base.total,
2928 true);
2929
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01002930 /* Cache flush objects bound into GGTT and rebind them. */
2931 vm = &dev_priv->gtt.base;
Daniel Vetterfa423312015-04-14 17:35:23 +02002932 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01002933 flush = false;
2934 list_for_each_entry(vma, &obj->vma_list, vma_link) {
2935 if (vma->vm != vm)
2936 continue;
Daniel Vetterfa423312015-04-14 17:35:23 +02002937
Tvrtko Ursulin2c3d9982015-07-06 15:15:01 +01002938 WARN_ON(i915_vma_bind(vma, obj->cache_level,
2939 PIN_UPDATE));
2940
2941 flush = true;
2942 }
2943
2944 if (flush)
2945 i915_gem_clflush_object(obj, obj->pin_display);
Daniel Vetterfa423312015-04-14 17:35:23 +02002946 }
2947
Daniel Vetterfa423312015-04-14 17:35:23 +02002948 if (INTEL_INFO(dev)->gen >= 8) {
2949 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
2950 chv_setup_private_ppat(dev_priv);
2951 else
2952 bdw_setup_private_ppat(dev_priv);
2953
2954 return;
2955 }
2956
2957 if (USES_PPGTT(dev)) {
2958 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
2959 /* TODO: Perhaps it shouldn't be gen6 specific */
2960
2961 struct i915_hw_ppgtt *ppgtt =
2962 container_of(vm, struct i915_hw_ppgtt,
2963 base);
2964
2965 if (i915_is_ggtt(vm))
2966 ppgtt = dev_priv->mm.aliasing_ppgtt;
2967
2968 gen6_write_page_range(dev_priv, &ppgtt->pd,
2969 0, ppgtt->base.total);
2970 }
2971 }
2972
2973 i915_ggtt_flush(dev_priv);
2974}
2975
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002976static struct i915_vma *
2977__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2978 struct i915_address_space *vm,
2979 const struct i915_ggtt_view *ggtt_view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002980{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002981 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002982
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002983 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
2984 return ERR_PTR(-EINVAL);
Chris Wilsone20d2ab2015-04-07 16:20:58 +01002985
2986 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002987 if (vma == NULL)
2988 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002989
Ben Widawsky6f65e292013-12-06 14:10:56 -08002990 INIT_LIST_HEAD(&vma->vma_link);
2991 INIT_LIST_HEAD(&vma->mm_list);
2992 INIT_LIST_HEAD(&vma->exec_list);
2993 vma->vm = vm;
2994 vma->obj = obj;
2995
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002996 if (i915_is_ggtt(vm))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002997 vma->ggtt_view = *ggtt_view;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002998
Tvrtko Ursulinf7635662014-12-03 14:59:24 +00002999 list_add_tail(&vma->vma_link, &obj->vma_list);
3000 if (!i915_is_ggtt(vm))
Michel Thierrye07f0552014-08-19 15:49:41 +01003001 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Ben Widawsky6f65e292013-12-06 14:10:56 -08003002
3003 return vma;
3004}
3005
3006struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003007i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
3008 struct i915_address_space *vm)
Ben Widawsky6f65e292013-12-06 14:10:56 -08003009{
3010 struct i915_vma *vma;
3011
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003012 vma = i915_gem_obj_to_vma(obj, vm);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003013 if (!vma)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003014 vma = __i915_gem_vma_create(obj, vm,
3015 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
Ben Widawsky6f65e292013-12-06 14:10:56 -08003016
3017 return vma;
3018}
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003019
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003020struct i915_vma *
3021i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
3022 const struct i915_ggtt_view *view)
3023{
3024 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
3025 struct i915_vma *vma;
3026
3027 if (WARN_ON(!view))
3028 return ERR_PTR(-EINVAL);
3029
3030 vma = i915_gem_obj_to_ggtt_view(obj, view);
3031
3032 if (IS_ERR(vma))
3033 return vma;
3034
3035 if (!vma)
3036 vma = __i915_gem_vma_create(obj, ggtt, view);
3037
3038 return vma;
3039
3040}
3041
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003042static void
3043rotate_pages(dma_addr_t *in, unsigned int width, unsigned int height,
3044 struct sg_table *st)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003045{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003046 unsigned int column, row;
3047 unsigned int src_idx;
3048 struct scatterlist *sg = st->sgl;
3049
3050 st->nents = 0;
3051
3052 for (column = 0; column < width; column++) {
3053 src_idx = width * (height - 1) + column;
3054 for (row = 0; row < height; row++) {
3055 st->nents++;
3056 /* We don't need the pages, but need to initialize
3057 * the entries so the sg list can be happily traversed.
3058 * The only thing we need are DMA addresses.
3059 */
3060 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3061 sg_dma_address(sg) = in[src_idx];
3062 sg_dma_len(sg) = PAGE_SIZE;
3063 sg = sg_next(sg);
3064 src_idx -= width;
3065 }
3066 }
3067}
3068
3069static struct sg_table *
3070intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
3071 struct drm_i915_gem_object *obj)
3072{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003073 struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01003074 unsigned int size_pages = rot_info->size >> PAGE_SHIFT;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003075 struct sg_page_iter sg_iter;
3076 unsigned long i;
3077 dma_addr_t *page_addr_list;
3078 struct sg_table *st;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00003079 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003080
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003081 /* Allocate a temporary list of source pages for random access. */
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01003082 page_addr_list = drm_malloc_ab(obj->base.size / PAGE_SIZE,
3083 sizeof(dma_addr_t));
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003084 if (!page_addr_list)
3085 return ERR_PTR(ret);
3086
3087 /* Allocate target SG list. */
3088 st = kmalloc(sizeof(*st), GFP_KERNEL);
3089 if (!st)
3090 goto err_st_alloc;
3091
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01003092 ret = sg_alloc_table(st, size_pages, GFP_KERNEL);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003093 if (ret)
3094 goto err_sg_alloc;
3095
3096 /* Populate source page list from the object. */
3097 i = 0;
3098 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
3099 page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
3100 i++;
3101 }
3102
3103 /* Rotate the pages. */
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01003104 rotate_pages(page_addr_list,
3105 rot_info->width_pages, rot_info->height_pages,
3106 st);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003107
3108 DRM_DEBUG_KMS(
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01003109 "Created rotated page mapping for object size %zu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %u pages).\n",
Tvrtko Ursulinc9f8fd22015-06-24 09:55:20 +01003110 obj->base.size, rot_info->pitch, rot_info->height,
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01003111 rot_info->pixel_format, rot_info->width_pages,
3112 rot_info->height_pages, size_pages);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003113
3114 drm_free_large(page_addr_list);
3115
3116 return st;
3117
3118err_sg_alloc:
3119 kfree(st);
3120err_st_alloc:
3121 drm_free_large(page_addr_list);
3122
3123 DRM_DEBUG_KMS(
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01003124 "Failed to create rotated mapping for object size %zu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %u pages)\n",
Tvrtko Ursulinc9f8fd22015-06-24 09:55:20 +01003125 obj->base.size, ret, rot_info->pitch, rot_info->height,
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01003126 rot_info->pixel_format, rot_info->width_pages,
3127 rot_info->height_pages, size_pages);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003128 return ERR_PTR(ret);
3129}
3130
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003131static struct sg_table *
3132intel_partial_pages(const struct i915_ggtt_view *view,
3133 struct drm_i915_gem_object *obj)
3134{
3135 struct sg_table *st;
3136 struct scatterlist *sg;
3137 struct sg_page_iter obj_sg_iter;
3138 int ret = -ENOMEM;
3139
3140 st = kmalloc(sizeof(*st), GFP_KERNEL);
3141 if (!st)
3142 goto err_st_alloc;
3143
3144 ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL);
3145 if (ret)
3146 goto err_sg_alloc;
3147
3148 sg = st->sgl;
3149 st->nents = 0;
3150 for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents,
3151 view->params.partial.offset)
3152 {
3153 if (st->nents >= view->params.partial.size)
3154 break;
3155
3156 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3157 sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter);
3158 sg_dma_len(sg) = PAGE_SIZE;
3159
3160 sg = sg_next(sg);
3161 st->nents++;
3162 }
3163
3164 return st;
3165
3166err_sg_alloc:
3167 kfree(st);
3168err_st_alloc:
3169 return ERR_PTR(ret);
3170}
3171
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003172static int
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003173i915_get_ggtt_vma_pages(struct i915_vma *vma)
3174{
3175 int ret = 0;
3176
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003177 if (vma->ggtt_view.pages)
3178 return 0;
3179
3180 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
3181 vma->ggtt_view.pages = vma->obj->pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003182 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
3183 vma->ggtt_view.pages =
3184 intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003185 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
3186 vma->ggtt_view.pages =
3187 intel_partial_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003188 else
3189 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3190 vma->ggtt_view.type);
3191
3192 if (!vma->ggtt_view.pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003193 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003194 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003195 ret = -EINVAL;
3196 } else if (IS_ERR(vma->ggtt_view.pages)) {
3197 ret = PTR_ERR(vma->ggtt_view.pages);
3198 vma->ggtt_view.pages = NULL;
3199 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3200 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003201 }
3202
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00003203 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003204}
3205
3206/**
3207 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
3208 * @vma: VMA to map
3209 * @cache_level: mapping cache level
3210 * @flags: flags like global or local mapping
3211 *
3212 * DMA addresses are taken from the scatter-gather table of this object (or of
3213 * this VMA in case of non-default GGTT views) and PTE entries set up.
3214 * Note that DMA addresses are also the only part of the SG table we care about.
3215 */
3216int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
3217 u32 flags)
3218{
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003219 int ret;
3220 u32 bind_flags;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003221
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003222 if (WARN_ON(flags == 0))
3223 return -EINVAL;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03003224
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003225 bind_flags = 0;
Daniel Vetter08755462015-04-20 09:04:05 -07003226 if (flags & PIN_GLOBAL)
3227 bind_flags |= GLOBAL_BIND;
3228 if (flags & PIN_USER)
3229 bind_flags |= LOCAL_BIND;
3230
3231 if (flags & PIN_UPDATE)
3232 bind_flags |= vma->bound;
3233 else
3234 bind_flags &= ~vma->bound;
3235
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003236 if (bind_flags == 0)
3237 return 0;
3238
3239 if (vma->bound == 0 && vma->vm->allocate_va_range) {
3240 trace_i915_va_alloc(vma->vm,
3241 vma->node.start,
3242 vma->node.size,
3243 VM_TO_TRACE_NAME(vma->vm));
3244
Mika Kuoppalab2dd4512015-06-25 18:35:15 +03003245 /* XXX: i915_vma_pin() will fix this +- hack */
3246 vma->pin_count++;
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003247 ret = vma->vm->allocate_va_range(vma->vm,
3248 vma->node.start,
3249 vma->node.size);
Mika Kuoppalab2dd4512015-06-25 18:35:15 +03003250 vma->pin_count--;
Mika Kuoppala75d04a32015-04-28 17:56:17 +03003251 if (ret)
3252 return ret;
3253 }
3254
3255 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
Daniel Vetter70b9f6f2015-04-14 17:35:27 +02003256 if (ret)
3257 return ret;
Daniel Vetter08755462015-04-20 09:04:05 -07003258
3259 vma->bound |= bind_flags;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003260
3261 return 0;
3262}
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003263
3264/**
3265 * i915_ggtt_view_size - Get the size of a GGTT view.
3266 * @obj: Object the view is of.
3267 * @view: The view in question.
3268 *
3269 * @return The size of the GGTT view in bytes.
3270 */
3271size_t
3272i915_ggtt_view_size(struct drm_i915_gem_object *obj,
3273 const struct i915_ggtt_view *view)
3274{
Tvrtko Ursulin9e759ff2015-06-23 12:57:43 +01003275 if (view->type == I915_GGTT_VIEW_NORMAL) {
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003276 return obj->base.size;
Tvrtko Ursulin9e759ff2015-06-23 12:57:43 +01003277 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
3278 return view->rotation_info.size;
Joonas Lahtinen8bd7ef12015-05-06 14:35:38 +03003279 } else if (view->type == I915_GGTT_VIEW_PARTIAL) {
3280 return view->params.partial.size << PAGE_SHIFT;
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003281 } else {
3282 WARN_ONCE(1, "GGTT view %u not implemented!\n", view->type);
3283 return obj->base.size;
3284 }
3285}