blob: 09430178049ee3dd6b608bced7971145841b3464 [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
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +000095const struct i915_ggtt_view i915_ggtt_view_normal;
Joonas Lahtinen9abc4642015-03-27 13:09:22 +020096const struct i915_ggtt_view i915_ggtt_view_rotated = {
97 .type = I915_GGTT_VIEW_ROTATED
98};
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +000099
Daniel Vettercfa7c862014-04-29 11:53:58 +0200100static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
101{
Chris Wilson1893a712014-09-19 11:56:27 +0100102 bool has_aliasing_ppgtt;
103 bool has_full_ppgtt;
104
105 has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
106 has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
Chris Wilson1893a712014-09-19 11:56:27 +0100107
Yu Zhang71ba2d62015-02-10 19:05:54 +0800108 if (intel_vgpu_active(dev))
109 has_full_ppgtt = false; /* emulation is too hard */
110
Damien Lespiau70ee45e2014-11-14 15:05:59 +0000111 /*
112 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
113 * execlists, the sole mechanism available to submit work.
114 */
115 if (INTEL_INFO(dev)->gen < 9 &&
116 (enable_ppgtt == 0 || !has_aliasing_ppgtt))
Daniel Vettercfa7c862014-04-29 11:53:58 +0200117 return 0;
118
119 if (enable_ppgtt == 1)
120 return 1;
121
Chris Wilson1893a712014-09-19 11:56:27 +0100122 if (enable_ppgtt == 2 && has_full_ppgtt)
Daniel Vettercfa7c862014-04-29 11:53:58 +0200123 return 2;
124
Daniel Vetter93a25a92014-03-06 09:40:43 +0100125#ifdef CONFIG_INTEL_IOMMU
126 /* Disable ppgtt on SNB if VT-d is on. */
127 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
128 DRM_INFO("Disabling PPGTT because VT-d is on\n");
Daniel Vettercfa7c862014-04-29 11:53:58 +0200129 return 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100130 }
131#endif
132
Jesse Barnes62942ed2014-06-13 09:28:33 -0700133 /* Early VLV doesn't have this */
Ville Syrjäläca2aed6c2014-06-28 02:03:56 +0300134 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
135 dev->pdev->revision < 0xb) {
Jesse Barnes62942ed2014-06-13 09:28:33 -0700136 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
137 return 0;
138 }
139
Michel Thierry2f82bbd2014-12-15 14:58:00 +0000140 if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
141 return 2;
142 else
143 return has_aliasing_ppgtt ? 1 : 0;
Daniel Vetter93a25a92014-03-06 09:40:43 +0100144}
145
Ben Widawsky6f65e292013-12-06 14:10:56 -0800146static void ppgtt_bind_vma(struct i915_vma *vma,
147 enum i915_cache_level cache_level,
Daniel Vetter47552652015-04-14 17:35:24 +0200148 u32 unused)
149{
150 u32 pte_flags = 0;
151
152 /* Currently applicable only to VLV */
153 if (vma->obj->gt_ro)
154 pte_flags |= PTE_READ_ONLY;
155
156 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
157 cache_level, pte_flags);
158}
159
160static void ppgtt_unbind_vma(struct i915_vma *vma)
161{
162 vma->vm->clear_range(vma->vm,
163 vma->node.start,
164 vma->obj->base.size,
165 true);
166}
Ben Widawsky6f65e292013-12-06 14:10:56 -0800167
Michel Thierry07749ef2015-03-16 16:00:54 +0000168static inline gen8_pte_t gen8_pte_encode(dma_addr_t addr,
169 enum i915_cache_level level,
170 bool valid)
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700171{
Michel Thierry07749ef2015-03-16 16:00:54 +0000172 gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700173 pte |= addr;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300174
175 switch (level) {
176 case I915_CACHE_NONE:
Ben Widawskyfbe5d362013-11-04 19:56:49 -0800177 pte |= PPAT_UNCACHED_INDEX;
Ben Widawsky63c42e52014-04-18 18:04:27 -0300178 break;
179 case I915_CACHE_WT:
180 pte |= PPAT_DISPLAY_ELLC_INDEX;
181 break;
182 default:
183 pte |= PPAT_CACHED_INDEX;
184 break;
185 }
186
Ben Widawsky94ec8f62013-11-02 21:07:18 -0700187 return pte;
188}
189
Michel Thierry07749ef2015-03-16 16:00:54 +0000190static inline gen8_pde_t gen8_pde_encode(struct drm_device *dev,
191 dma_addr_t addr,
192 enum i915_cache_level level)
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800193{
Michel Thierry07749ef2015-03-16 16:00:54 +0000194 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
Ben Widawskyb1fe6672013-11-04 21:20:14 -0800195 pde |= addr;
196 if (level != I915_CACHE_NONE)
197 pde |= PPAT_CACHED_PDE_INDEX;
198 else
199 pde |= PPAT_UNCACHED_INDEX;
200 return pde;
201}
202
Michel Thierry07749ef2015-03-16 16:00:54 +0000203static gen6_pte_t snb_pte_encode(dma_addr_t addr,
204 enum i915_cache_level level,
205 bool valid, u32 unused)
Ben Widawsky54d12522012-09-24 16:44:32 -0700206{
Michel Thierry07749ef2015-03-16 16:00:54 +0000207 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky54d12522012-09-24 16:44:32 -0700208 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -0700209
210 switch (level) {
Chris Wilson350ec882013-08-06 13:17:02 +0100211 case I915_CACHE_L3_LLC:
212 case I915_CACHE_LLC:
213 pte |= GEN6_PTE_CACHE_LLC;
214 break;
215 case I915_CACHE_NONE:
216 pte |= GEN6_PTE_UNCACHED;
217 break;
218 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100219 MISSING_CASE(level);
Chris Wilson350ec882013-08-06 13:17:02 +0100220 }
221
222 return pte;
223}
224
Michel Thierry07749ef2015-03-16 16:00:54 +0000225static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
226 enum i915_cache_level level,
227 bool valid, u32 unused)
Chris Wilson350ec882013-08-06 13:17:02 +0100228{
Michel Thierry07749ef2015-03-16 16:00:54 +0000229 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Chris Wilson350ec882013-08-06 13:17:02 +0100230 pte |= GEN6_PTE_ADDR_ENCODE(addr);
231
232 switch (level) {
233 case I915_CACHE_L3_LLC:
234 pte |= GEN7_PTE_CACHE_L3_LLC;
Ben Widawskye7210c32012-10-19 09:33:22 -0700235 break;
236 case I915_CACHE_LLC:
237 pte |= GEN6_PTE_CACHE_LLC;
238 break;
239 case I915_CACHE_NONE:
Kenneth Graunke91197082013-04-22 00:53:51 -0700240 pte |= GEN6_PTE_UNCACHED;
Ben Widawskye7210c32012-10-19 09:33:22 -0700241 break;
242 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +0100243 MISSING_CASE(level);
Ben Widawskye7210c32012-10-19 09:33:22 -0700244 }
245
Ben Widawsky54d12522012-09-24 16:44:32 -0700246 return pte;
247}
248
Michel Thierry07749ef2015-03-16 16:00:54 +0000249static gen6_pte_t byt_pte_encode(dma_addr_t addr,
250 enum i915_cache_level level,
251 bool valid, u32 flags)
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700252{
Michel Thierry07749ef2015-03-16 16:00:54 +0000253 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700254 pte |= GEN6_PTE_ADDR_ENCODE(addr);
255
Akash Goel24f3a8c2014-06-17 10:59:42 +0530256 if (!(flags & PTE_READ_ONLY))
257 pte |= BYT_PTE_WRITEABLE;
Kenneth Graunke93c34e72013-04-22 00:53:50 -0700258
259 if (level != I915_CACHE_NONE)
260 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
261
262 return pte;
263}
264
Michel Thierry07749ef2015-03-16 16:00:54 +0000265static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
266 enum i915_cache_level level,
267 bool valid, u32 unused)
Kenneth Graunke91197082013-04-22 00:53:51 -0700268{
Michel Thierry07749ef2015-03-16 16:00:54 +0000269 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky0d8ff152013-07-04 11:02:03 -0700270 pte |= HSW_PTE_ADDR_ENCODE(addr);
Kenneth Graunke91197082013-04-22 00:53:51 -0700271
272 if (level != I915_CACHE_NONE)
Ben Widawsky87a6b682013-08-04 23:47:29 -0700273 pte |= HSW_WB_LLC_AGE3;
Kenneth Graunke91197082013-04-22 00:53:51 -0700274
275 return pte;
276}
277
Michel Thierry07749ef2015-03-16 16:00:54 +0000278static gen6_pte_t iris_pte_encode(dma_addr_t addr,
279 enum i915_cache_level level,
280 bool valid, u32 unused)
Ben Widawsky4d15c142013-07-04 11:02:06 -0700281{
Michel Thierry07749ef2015-03-16 16:00:54 +0000282 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
Ben Widawsky4d15c142013-07-04 11:02:06 -0700283 pte |= HSW_PTE_ADDR_ENCODE(addr);
284
Chris Wilson651d7942013-08-08 14:41:10 +0100285 switch (level) {
286 case I915_CACHE_NONE:
287 break;
288 case I915_CACHE_WT:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000289 pte |= HSW_WT_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100290 break;
291 default:
Chris Wilsonc51e9702013-11-22 10:37:53 +0000292 pte |= HSW_WB_ELLC_LLC_AGE3;
Chris Wilson651d7942013-08-08 14:41:10 +0100293 break;
294 }
Ben Widawsky4d15c142013-07-04 11:02:06 -0700295
296 return pte;
297}
298
Ben Widawsky678d96f2015-03-16 16:00:56 +0000299#define i915_dma_unmap_single(px, dev) \
300 __i915_dma_unmap_single((px)->daddr, dev)
301
302static inline void __i915_dma_unmap_single(dma_addr_t daddr,
303 struct drm_device *dev)
304{
305 struct device *device = &dev->pdev->dev;
306
307 dma_unmap_page(device, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
308}
309
310/**
311 * i915_dma_map_single() - Create a dma mapping for a page table/dir/etc.
312 * @px: Page table/dir/etc to get a DMA map for
313 * @dev: drm device
314 *
315 * Page table allocations are unified across all gens. They always require a
316 * single 4k allocation, as well as a DMA mapping. If we keep the structs
317 * symmetric here, the simple macro covers us for every page table type.
318 *
319 * Return: 0 if success.
320 */
321#define i915_dma_map_single(px, dev) \
322 i915_dma_map_page_single((px)->page, (dev), &(px)->daddr)
323
324static inline int i915_dma_map_page_single(struct page *page,
325 struct drm_device *dev,
326 dma_addr_t *daddr)
327{
328 struct device *device = &dev->pdev->dev;
329
330 *daddr = dma_map_page(device, page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
Michel Thierry1266cdb2015-03-24 17:06:33 +0000331 if (dma_mapping_error(device, *daddr))
332 return -ENOMEM;
333
334 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000335}
336
Michel Thierryec565b32015-04-08 12:13:23 +0100337static void unmap_and_free_pt(struct i915_page_table *pt,
Ben Widawsky678d96f2015-03-16 16:00:56 +0000338 struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000339{
340 if (WARN_ON(!pt->page))
341 return;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000342
343 i915_dma_unmap_single(pt, dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000344 __free_page(pt->page);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000345 kfree(pt->used_ptes);
Ben Widawsky06fda602015-02-24 16:22:36 +0000346 kfree(pt);
347}
348
Michel Thierry5a8e9942015-04-08 12:13:25 +0100349static void gen8_initialize_pt(struct i915_address_space *vm,
Michel Thierrye5815a22015-04-08 12:13:32 +0100350 struct i915_page_table *pt)
Michel Thierry5a8e9942015-04-08 12:13:25 +0100351{
352 gen8_pte_t *pt_vaddr, scratch_pte;
353 int i;
354
355 pt_vaddr = kmap_atomic(pt->page);
356 scratch_pte = gen8_pte_encode(vm->scratch.addr,
357 I915_CACHE_LLC, true);
358
359 for (i = 0; i < GEN8_PTES; i++)
360 pt_vaddr[i] = scratch_pte;
361
362 if (!HAS_LLC(vm->dev))
363 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
364 kunmap_atomic(pt_vaddr);
365}
366
Michel Thierryec565b32015-04-08 12:13:23 +0100367static struct i915_page_table *alloc_pt_single(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000368{
Michel Thierryec565b32015-04-08 12:13:23 +0100369 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000370 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
371 GEN8_PTES : GEN6_PTES;
372 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000373
374 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
375 if (!pt)
376 return ERR_PTR(-ENOMEM);
377
Ben Widawsky678d96f2015-03-16 16:00:56 +0000378 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
379 GFP_KERNEL);
380
381 if (!pt->used_ptes)
382 goto fail_bitmap;
383
Michel Thierry4933d512015-03-24 15:46:22 +0000384 pt->page = alloc_page(GFP_KERNEL);
Ben Widawsky678d96f2015-03-16 16:00:56 +0000385 if (!pt->page)
386 goto fail_page;
387
388 ret = i915_dma_map_single(pt, dev);
389 if (ret)
390 goto fail_dma;
Ben Widawsky06fda602015-02-24 16:22:36 +0000391
392 return pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +0000393
394fail_dma:
395 __free_page(pt->page);
396fail_page:
397 kfree(pt->used_ptes);
398fail_bitmap:
399 kfree(pt);
400
401 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000402}
403
Michel Thierrye5815a22015-04-08 12:13:32 +0100404static void unmap_and_free_pd(struct i915_page_directory *pd,
405 struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000406{
407 if (pd->page) {
Michel Thierrye5815a22015-04-08 12:13:32 +0100408 i915_dma_unmap_single(pd, dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000409 __free_page(pd->page);
Michel Thierry33c88192015-04-08 12:13:33 +0100410 kfree(pd->used_pdes);
Ben Widawsky06fda602015-02-24 16:22:36 +0000411 kfree(pd);
412 }
413}
414
Michel Thierrye5815a22015-04-08 12:13:32 +0100415static struct i915_page_directory *alloc_pd_single(struct drm_device *dev)
Ben Widawsky06fda602015-02-24 16:22:36 +0000416{
Michel Thierryec565b32015-04-08 12:13:23 +0100417 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100418 int ret = -ENOMEM;
Ben Widawsky06fda602015-02-24 16:22:36 +0000419
420 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
421 if (!pd)
422 return ERR_PTR(-ENOMEM);
423
Michel Thierry33c88192015-04-08 12:13:33 +0100424 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
425 sizeof(*pd->used_pdes), GFP_KERNEL);
426 if (!pd->used_pdes)
427 goto free_pd;
428
Michel Thierry5a8e9942015-04-08 12:13:25 +0100429 pd->page = alloc_page(GFP_KERNEL);
Michel Thierry33c88192015-04-08 12:13:33 +0100430 if (!pd->page)
431 goto free_bitmap;
Ben Widawsky06fda602015-02-24 16:22:36 +0000432
Michel Thierrye5815a22015-04-08 12:13:32 +0100433 ret = i915_dma_map_single(pd, dev);
Michel Thierry33c88192015-04-08 12:13:33 +0100434 if (ret)
435 goto free_page;
Michel Thierrye5815a22015-04-08 12:13:32 +0100436
Ben Widawsky06fda602015-02-24 16:22:36 +0000437 return pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100438
439free_page:
440 __free_page(pd->page);
441free_bitmap:
442 kfree(pd->used_pdes);
443free_pd:
444 kfree(pd);
445
446 return ERR_PTR(ret);
Ben Widawsky06fda602015-02-24 16:22:36 +0000447}
448
Ben Widawsky94e409c2013-11-04 22:29:36 -0800449/* Broadwell Page Directory Pointer Descriptors */
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100450static int gen8_write_pdp(struct intel_engine_cs *ring,
451 unsigned entry,
452 dma_addr_t addr)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800453{
454 int ret;
455
456 BUG_ON(entry >= 4);
457
458 ret = intel_ring_begin(ring, 6);
459 if (ret)
460 return ret;
461
462 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
463 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100464 intel_ring_emit(ring, upper_32_bits(addr));
Ben Widawsky94e409c2013-11-04 22:29:36 -0800465 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
466 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100467 intel_ring_emit(ring, lower_32_bits(addr));
Ben Widawsky94e409c2013-11-04 22:29:36 -0800468 intel_ring_advance(ring);
469
470 return 0;
471}
472
Ben Widawskyeeb94882013-12-06 14:11:10 -0800473static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +0100474 struct intel_engine_cs *ring)
Ben Widawsky94e409c2013-11-04 22:29:36 -0800475{
Ben Widawskyeeb94882013-12-06 14:11:10 -0800476 int i, ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800477
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100478 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
479 struct i915_page_directory *pd = ppgtt->pdp.page_directory[i];
480 dma_addr_t pd_daddr = pd ? pd->daddr : ppgtt->scratch_pd->daddr;
481 /* The page directory might be NULL, but we need to clear out
482 * whatever the previous context might have used. */
483 ret = gen8_write_pdp(ring, i, pd_daddr);
Ben Widawskyeeb94882013-12-06 14:11:10 -0800484 if (ret)
485 return ret;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800486 }
Ben Widawskyd595bd42013-11-25 09:54:32 -0800487
Ben Widawskyeeb94882013-12-06 14:11:10 -0800488 return 0;
Ben Widawsky94e409c2013-11-04 22:29:36 -0800489}
490
Ben Widawsky459108b2013-11-02 21:07:23 -0700491static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -0800492 uint64_t start,
493 uint64_t length,
Ben Widawsky459108b2013-11-02 21:07:23 -0700494 bool use_scratch)
495{
496 struct i915_hw_ppgtt *ppgtt =
497 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +0000498 gen8_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800499 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
500 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
501 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky782f1492014-02-20 11:50:33 -0800502 unsigned num_entries = length >> PAGE_SHIFT;
Ben Widawsky459108b2013-11-02 21:07:23 -0700503 unsigned last_pte, i;
504
505 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
506 I915_CACHE_LLC, use_scratch);
507
508 while (num_entries) {
Michel Thierryec565b32015-04-08 12:13:23 +0100509 struct i915_page_directory *pd;
510 struct i915_page_table *pt;
Ben Widawsky06fda602015-02-24 16:22:36 +0000511 struct page *page_table;
512
513 if (WARN_ON(!ppgtt->pdp.page_directory[pdpe]))
514 continue;
515
516 pd = ppgtt->pdp.page_directory[pdpe];
517
518 if (WARN_ON(!pd->page_table[pde]))
519 continue;
520
521 pt = pd->page_table[pde];
522
523 if (WARN_ON(!pt->page))
524 continue;
525
526 page_table = pt->page;
Ben Widawsky459108b2013-11-02 21:07:23 -0700527
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800528 last_pte = pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +0000529 if (last_pte > GEN8_PTES)
530 last_pte = GEN8_PTES;
Ben Widawsky459108b2013-11-02 21:07:23 -0700531
532 pt_vaddr = kmap_atomic(page_table);
533
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800534 for (i = pte; i < last_pte; i++) {
Ben Widawsky459108b2013-11-02 21:07:23 -0700535 pt_vaddr[i] = scratch_pte;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800536 num_entries--;
537 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700538
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300539 if (!HAS_LLC(ppgtt->base.dev))
540 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky459108b2013-11-02 21:07:23 -0700541 kunmap_atomic(pt_vaddr);
542
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800543 pte = 0;
Michel Thierry07749ef2015-03-16 16:00:54 +0000544 if (++pde == I915_PDES) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800545 pdpe++;
546 pde = 0;
547 }
Ben Widawsky459108b2013-11-02 21:07:23 -0700548 }
549}
550
Ben Widawsky9df15b42013-11-02 21:07:24 -0700551static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
552 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -0800553 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +0530554 enum i915_cache_level cache_level, u32 unused)
Ben Widawsky9df15b42013-11-02 21:07:24 -0700555{
556 struct i915_hw_ppgtt *ppgtt =
557 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +0000558 gen8_pte_t *pt_vaddr;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800559 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
560 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
561 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700562 struct sg_page_iter sg_iter;
563
Chris Wilson6f1cc992013-12-31 15:50:31 +0000564 pt_vaddr = NULL;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700565
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800566 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Ben Widawsky76643602015-01-22 17:01:24 +0000567 if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES))
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800568 break;
569
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000570 if (pt_vaddr == NULL) {
Michel Thierryec565b32015-04-08 12:13:23 +0100571 struct i915_page_directory *pd = ppgtt->pdp.page_directory[pdpe];
572 struct i915_page_table *pt = pd->page_table[pde];
Ben Widawsky06fda602015-02-24 16:22:36 +0000573 struct page *page_table = pt->page;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000574
575 pt_vaddr = kmap_atomic(page_table);
576 }
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800577
578 pt_vaddr[pte] =
Chris Wilson6f1cc992013-12-31 15:50:31 +0000579 gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
580 cache_level, true);
Michel Thierry07749ef2015-03-16 16:00:54 +0000581 if (++pte == GEN8_PTES) {
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300582 if (!HAS_LLC(ppgtt->base.dev))
583 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Ben Widawsky9df15b42013-11-02 21:07:24 -0700584 kunmap_atomic(pt_vaddr);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000585 pt_vaddr = NULL;
Michel Thierry07749ef2015-03-16 16:00:54 +0000586 if (++pde == I915_PDES) {
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800587 pdpe++;
588 pde = 0;
589 }
590 pte = 0;
Ben Widawsky9df15b42013-11-02 21:07:24 -0700591 }
592 }
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300593 if (pt_vaddr) {
594 if (!HAS_LLC(ppgtt->base.dev))
595 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
Chris Wilson6f1cc992013-12-31 15:50:31 +0000596 kunmap_atomic(pt_vaddr);
Rafael Barbalhofd1ab8f2014-04-09 13:28:02 +0300597 }
Ben Widawsky9df15b42013-11-02 21:07:24 -0700598}
599
Michel Thierry69876be2015-04-08 12:13:27 +0100600static void __gen8_do_map_pt(gen8_pde_t * const pde,
601 struct i915_page_table *pt,
602 struct drm_device *dev)
603{
604 gen8_pde_t entry =
605 gen8_pde_encode(dev, pt->daddr, I915_CACHE_LLC);
606 *pde = entry;
607}
608
609static void gen8_initialize_pd(struct i915_address_space *vm,
610 struct i915_page_directory *pd)
611{
612 struct i915_hw_ppgtt *ppgtt =
613 container_of(vm, struct i915_hw_ppgtt, base);
614 gen8_pde_t *page_directory;
615 struct i915_page_table *pt;
616 int i;
617
618 page_directory = kmap_atomic(pd->page);
619 pt = ppgtt->scratch_pt;
620 for (i = 0; i < I915_PDES; i++)
621 /* Map the PDE to the page table */
622 __gen8_do_map_pt(page_directory + i, pt, vm->dev);
623
624 if (!HAS_LLC(vm->dev))
625 drm_clflush_virt_range(page_directory, PAGE_SIZE);
Michel Thierrye5815a22015-04-08 12:13:32 +0100626 kunmap_atomic(page_directory);
627}
628
Michel Thierryec565b32015-04-08 12:13:23 +0100629static void gen8_free_page_tables(struct i915_page_directory *pd, struct drm_device *dev)
Ben Widawskyb45a6712014-02-12 14:28:44 -0800630{
631 int i;
632
Ben Widawsky06fda602015-02-24 16:22:36 +0000633 if (!pd->page)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800634 return;
Ben Widawskyb45a6712014-02-12 14:28:44 -0800635
Michel Thierry33c88192015-04-08 12:13:33 +0100636 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000637 if (WARN_ON(!pd->page_table[i]))
638 continue;
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800639
Michel Thierry06dc68d2015-02-24 16:22:37 +0000640 unmap_and_free_pt(pd->page_table[i], dev);
Ben Widawsky06fda602015-02-24 16:22:36 +0000641 pd->page_table[i] = NULL;
642 }
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000643}
644
Daniel Vetter061dd492015-04-14 17:35:13 +0200645static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800646{
Daniel Vetter061dd492015-04-14 17:35:13 +0200647 struct i915_hw_ppgtt *ppgtt =
648 container_of(vm, struct i915_hw_ppgtt, base);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800649 int i;
650
Michel Thierry33c88192015-04-08 12:13:33 +0100651 for_each_set_bit(i, ppgtt->pdp.used_pdpes, GEN8_LEGACY_PDPES) {
Ben Widawsky06fda602015-02-24 16:22:36 +0000652 if (WARN_ON(!ppgtt->pdp.page_directory[i]))
653 continue;
654
Michel Thierry06dc68d2015-02-24 16:22:37 +0000655 gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
Michel Thierrye5815a22015-04-08 12:13:32 +0100656 unmap_and_free_pd(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
Ben Widawsky7ad47cf2014-02-20 11:51:21 -0800657 }
Michel Thierry69876be2015-04-08 12:13:27 +0100658
Michel Thierrye5815a22015-04-08 12:13:32 +0100659 unmap_and_free_pd(ppgtt->scratch_pd, ppgtt->base.dev);
Michel Thierry69876be2015-04-08 12:13:27 +0100660 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
Ben Widawskyb45a6712014-02-12 14:28:44 -0800661}
662
Michel Thierryd7b26332015-04-08 12:13:34 +0100663/**
664 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
665 * @ppgtt: Master ppgtt structure.
666 * @pd: Page directory for this address range.
667 * @start: Starting virtual address to begin allocations.
668 * @length Size of the allocations.
669 * @new_pts: Bitmap set by function with new allocations. Likely used by the
670 * caller to free on error.
671 *
672 * Allocate the required number of page tables. Extremely similar to
673 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
674 * the page directory boundary (instead of the page directory pointer). That
675 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
676 * possible, and likely that the caller will need to use multiple calls of this
677 * function to achieve the appropriate allocation.
678 *
679 * Return: 0 if success; negative error code otherwise.
680 */
Michel Thierrye5815a22015-04-08 12:13:32 +0100681static int gen8_ppgtt_alloc_pagetabs(struct i915_hw_ppgtt *ppgtt,
682 struct i915_page_directory *pd,
Michel Thierry5441f0c2015-04-08 12:13:28 +0100683 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +0100684 uint64_t length,
685 unsigned long *new_pts)
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000686{
Michel Thierrye5815a22015-04-08 12:13:32 +0100687 struct drm_device *dev = ppgtt->base.dev;
Michel Thierryd7b26332015-04-08 12:13:34 +0100688 struct i915_page_table *pt;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100689 uint64_t temp;
690 uint32_t pde;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000691
Michel Thierryd7b26332015-04-08 12:13:34 +0100692 gen8_for_each_pde(pt, pd, start, length, temp, pde) {
693 /* Don't reallocate page tables */
694 if (pt) {
695 /* Scratch is never allocated this way */
696 WARN_ON(pt == ppgtt->scratch_pt);
697 continue;
698 }
699
700 pt = alloc_pt_single(dev);
701 if (IS_ERR(pt))
Ben Widawsky06fda602015-02-24 16:22:36 +0000702 goto unwind_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100703
Michel Thierryd7b26332015-04-08 12:13:34 +0100704 gen8_initialize_pt(&ppgtt->base, pt);
705 pd->page_table[pde] = pt;
706 set_bit(pde, new_pts);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000707 }
708
709 return 0;
710
711unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +0100712 for_each_set_bit(pde, new_pts, I915_PDES)
Michel Thierrye5815a22015-04-08 12:13:32 +0100713 unmap_and_free_pt(pd->page_table[pde], dev);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000714
715 return -ENOMEM;
716}
717
Michel Thierryd7b26332015-04-08 12:13:34 +0100718/**
719 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
720 * @ppgtt: Master ppgtt structure.
721 * @pdp: Page directory pointer for this address range.
722 * @start: Starting virtual address to begin allocations.
723 * @length Size of the allocations.
724 * @new_pds Bitmap set by function with new allocations. Likely used by the
725 * caller to free on error.
726 *
727 * Allocate the required number of page directories starting at the pde index of
728 * @start, and ending at the pde index @start + @length. This function will skip
729 * over already allocated page directories within the range, and only allocate
730 * new ones, setting the appropriate pointer within the pdp as well as the
731 * correct position in the bitmap @new_pds.
732 *
733 * The function will only allocate the pages within the range for a give page
734 * directory pointer. In other words, if @start + @length straddles a virtually
735 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
736 * required by the caller, This is not currently possible, and the BUG in the
737 * code will prevent it.
738 *
739 * Return: 0 if success; negative error code otherwise.
740 */
Michel Thierryc488dbb2015-04-08 12:13:31 +0100741static int gen8_ppgtt_alloc_page_directories(struct i915_hw_ppgtt *ppgtt,
742 struct i915_page_directory_pointer *pdp,
Michel Thierry69876be2015-04-08 12:13:27 +0100743 uint64_t start,
Michel Thierryd7b26332015-04-08 12:13:34 +0100744 uint64_t length,
745 unsigned long *new_pds)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800746{
Michel Thierrye5815a22015-04-08 12:13:32 +0100747 struct drm_device *dev = ppgtt->base.dev;
Michel Thierryd7b26332015-04-08 12:13:34 +0100748 struct i915_page_directory *pd;
Michel Thierry69876be2015-04-08 12:13:27 +0100749 uint64_t temp;
750 uint32_t pdpe;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800751
Michel Thierryd7b26332015-04-08 12:13:34 +0100752 WARN_ON(!bitmap_empty(new_pds, GEN8_LEGACY_PDPES));
753
Michel Thierry69876be2015-04-08 12:13:27 +0100754 /* FIXME: PPGTT container_of won't work for 64b */
755 WARN_ON((start + length) > 0x800000000ULL);
756
Michel Thierryd7b26332015-04-08 12:13:34 +0100757 gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
758 if (pd)
759 continue;
Michel Thierry33c88192015-04-08 12:13:33 +0100760
Michel Thierryd7b26332015-04-08 12:13:34 +0100761 pd = alloc_pd_single(dev);
762 if (IS_ERR(pd))
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000763 goto unwind_out;
Michel Thierry69876be2015-04-08 12:13:27 +0100764
Michel Thierryd7b26332015-04-08 12:13:34 +0100765 gen8_initialize_pd(&ppgtt->base, pd);
766 pdp->page_directory[pdpe] = pd;
767 set_bit(pdpe, new_pds);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000768 }
769
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800770 return 0;
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000771
772unwind_out:
Michel Thierryd7b26332015-04-08 12:13:34 +0100773 for_each_set_bit(pdpe, new_pds, GEN8_LEGACY_PDPES)
Michel Thierrye5815a22015-04-08 12:13:32 +0100774 unmap_and_free_pd(pdp->page_directory[pdpe], dev);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000775
776 return -ENOMEM;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800777}
778
Michel Thierryd7b26332015-04-08 12:13:34 +0100779static void
780free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long **new_pts)
781{
782 int i;
783
784 for (i = 0; i < GEN8_LEGACY_PDPES; i++)
785 kfree(new_pts[i]);
786 kfree(new_pts);
787 kfree(new_pds);
788}
789
790/* Fills in the page directory bitmap, and the array of page tables bitmap. Both
791 * of these are based on the number of PDPEs in the system.
792 */
793static
794int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
795 unsigned long ***new_pts)
796{
797 int i;
798 unsigned long *pds;
799 unsigned long **pts;
800
801 pds = kcalloc(BITS_TO_LONGS(GEN8_LEGACY_PDPES), sizeof(unsigned long), GFP_KERNEL);
802 if (!pds)
803 return -ENOMEM;
804
805 pts = kcalloc(GEN8_LEGACY_PDPES, sizeof(unsigned long *), GFP_KERNEL);
806 if (!pts) {
807 kfree(pds);
808 return -ENOMEM;
809 }
810
811 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
812 pts[i] = kcalloc(BITS_TO_LONGS(I915_PDES),
813 sizeof(unsigned long), GFP_KERNEL);
814 if (!pts[i])
815 goto err_out;
816 }
817
818 *new_pds = pds;
819 *new_pts = pts;
820
821 return 0;
822
823err_out:
824 free_gen8_temp_bitmaps(pds, pts);
825 return -ENOMEM;
826}
827
Michel Thierrye5815a22015-04-08 12:13:32 +0100828static int gen8_alloc_va_range(struct i915_address_space *vm,
829 uint64_t start,
830 uint64_t length)
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800831{
Michel Thierrye5815a22015-04-08 12:13:32 +0100832 struct i915_hw_ppgtt *ppgtt =
833 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierryd7b26332015-04-08 12:13:34 +0100834 unsigned long *new_page_dirs, **new_page_tables;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100835 struct i915_page_directory *pd;
Michel Thierry33c88192015-04-08 12:13:33 +0100836 const uint64_t orig_start = start;
837 const uint64_t orig_length = length;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100838 uint64_t temp;
839 uint32_t pdpe;
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800840 int ret;
841
Michel Thierryd7b26332015-04-08 12:13:34 +0100842#ifndef CONFIG_64BIT
843 /* Disallow 64b address on 32b platforms. Nothing is wrong with doing
844 * this in hardware, but a lot of the drm code is not prepared to handle
845 * 64b offset on 32b platforms.
846 * This will be addressed when 48b PPGTT is added */
847 if (start + length > 0x100000000ULL)
848 return -E2BIG;
849#endif
850
851 /* Wrap is never okay since we can only represent 48b, and we don't
852 * actually use the other side of the canonical address space.
853 */
854 if (WARN_ON(start + length < start))
855 return -ERANGE;
856
857 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800858 if (ret)
859 return ret;
860
Michel Thierryd7b26332015-04-08 12:13:34 +0100861 /* Do the allocations first so we can easily bail out */
862 ret = gen8_ppgtt_alloc_page_directories(ppgtt, &ppgtt->pdp, start, length,
863 new_page_dirs);
864 if (ret) {
865 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
866 return ret;
867 }
868
869 /* For every page directory referenced, allocate page tables */
Michel Thierry5441f0c2015-04-08 12:13:28 +0100870 gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
Michel Thierryd7b26332015-04-08 12:13:34 +0100871 ret = gen8_ppgtt_alloc_pagetabs(ppgtt, pd, start, length,
872 new_page_tables[pdpe]);
Michel Thierry5441f0c2015-04-08 12:13:28 +0100873 if (ret)
874 goto err_out;
Michel Thierry5441f0c2015-04-08 12:13:28 +0100875 }
876
Michel Thierry33c88192015-04-08 12:13:33 +0100877 start = orig_start;
878 length = orig_length;
879
Michel Thierryd7b26332015-04-08 12:13:34 +0100880 /* Allocations have completed successfully, so set the bitmaps, and do
881 * the mappings. */
Michel Thierry33c88192015-04-08 12:13:33 +0100882 gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) {
Michel Thierryd7b26332015-04-08 12:13:34 +0100883 gen8_pde_t *const page_directory = kmap_atomic(pd->page);
Michel Thierry33c88192015-04-08 12:13:33 +0100884 struct i915_page_table *pt;
885 uint64_t pd_len = gen8_clamp_pd(start, length);
886 uint64_t pd_start = start;
887 uint32_t pde;
888
Michel Thierryd7b26332015-04-08 12:13:34 +0100889 /* Every pd should be allocated, we just did that above. */
890 WARN_ON(!pd);
891
892 gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) {
893 /* Same reasoning as pd */
894 WARN_ON(!pt);
895 WARN_ON(!pd_len);
896 WARN_ON(!gen8_pte_count(pd_start, pd_len));
897
898 /* Set our used ptes within the page table */
899 bitmap_set(pt->used_ptes,
900 gen8_pte_index(pd_start),
901 gen8_pte_count(pd_start, pd_len));
902
903 /* Our pde is now pointing to the pagetable, pt */
Michel Thierry33c88192015-04-08 12:13:33 +0100904 set_bit(pde, pd->used_pdes);
Michel Thierryd7b26332015-04-08 12:13:34 +0100905
906 /* Map the PDE to the page table */
907 __gen8_do_map_pt(page_directory + pde, pt, vm->dev);
908
909 /* NB: We haven't yet mapped ptes to pages. At this
910 * point we're still relying on insert_entries() */
Michel Thierry33c88192015-04-08 12:13:33 +0100911 }
Michel Thierryd7b26332015-04-08 12:13:34 +0100912
913 if (!HAS_LLC(vm->dev))
914 drm_clflush_virt_range(page_directory, PAGE_SIZE);
915
916 kunmap_atomic(page_directory);
917
Michel Thierry33c88192015-04-08 12:13:33 +0100918 set_bit(pdpe, ppgtt->pdp.used_pdpes);
919 }
920
Michel Thierryd7b26332015-04-08 12:13:34 +0100921 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Ben Widawskyd7b3de92015-02-24 16:22:34 +0000922 return 0;
923
924err_out:
Michel Thierryd7b26332015-04-08 12:13:34 +0100925 while (pdpe--) {
926 for_each_set_bit(temp, new_page_tables[pdpe], I915_PDES)
927 unmap_and_free_pt(ppgtt->pdp.page_directory[pdpe]->page_table[temp], vm->dev);
928 }
929
930 for_each_set_bit(pdpe, new_page_dirs, GEN8_LEGACY_PDPES)
931 unmap_and_free_pd(ppgtt->pdp.page_directory[pdpe], vm->dev);
932
933 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
Ben Widawskybf2b4ed2014-02-19 22:05:43 -0800934 return ret;
935}
936
Daniel Vettereb0b44a2015-03-18 14:47:59 +0100937/*
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800938 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
939 * with a net effect resembling a 2-level page table in normal x86 terms. Each
940 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
941 * space.
Ben Widawsky37aca442013-11-04 20:47:32 -0800942 *
Ben Widawskyf3a964b2014-02-19 22:05:42 -0800943 */
Daniel Vetter5c5f6452015-04-14 17:35:14 +0200944static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky37aca442013-11-04 20:47:32 -0800945{
Michel Thierry69876be2015-04-08 12:13:27 +0100946 ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
947 if (IS_ERR(ppgtt->scratch_pt))
948 return PTR_ERR(ppgtt->scratch_pt);
949
Michel Thierrye5815a22015-04-08 12:13:32 +0100950 ppgtt->scratch_pd = alloc_pd_single(ppgtt->base.dev);
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100951 if (IS_ERR(ppgtt->scratch_pd))
952 return PTR_ERR(ppgtt->scratch_pd);
953
Michel Thierry69876be2015-04-08 12:13:27 +0100954 gen8_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
Michel Thierry7cb6d7a2015-04-08 12:13:29 +0100955 gen8_initialize_pd(&ppgtt->base, ppgtt->scratch_pd);
Michel Thierry69876be2015-04-08 12:13:27 +0100956
Michel Thierryd7b26332015-04-08 12:13:34 +0100957 ppgtt->base.start = 0;
Daniel Vetter5c5f6452015-04-14 17:35:14 +0200958 ppgtt->base.total = 1ULL << 32;
Michel Thierryd7b26332015-04-08 12:13:34 +0100959 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
Daniel Vetter5c5f6452015-04-14 17:35:14 +0200960 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
Michel Thierryd7b26332015-04-08 12:13:34 +0100961 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
Daniel Vetterc7e16f22015-04-14 17:35:11 +0200962 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
Daniel Vetter777dc5b2015-04-14 17:35:12 +0200963 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
964 ppgtt->base.bind_vma = ppgtt_bind_vma;
Michel Thierryd7b26332015-04-08 12:13:34 +0100965
966 ppgtt->switch_mm = gen8_mm_switch;
967
968 return 0;
969}
970
Ben Widawsky87d60b62013-12-06 14:11:29 -0800971static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
972{
Ben Widawsky87d60b62013-12-06 14:11:29 -0800973 struct i915_address_space *vm = &ppgtt->base;
Michel Thierry09942c62015-04-08 12:13:30 +0100974 struct i915_page_table *unused;
Michel Thierry07749ef2015-03-16 16:00:54 +0000975 gen6_pte_t scratch_pte;
Ben Widawsky87d60b62013-12-06 14:11:29 -0800976 uint32_t pd_entry;
Michel Thierry09942c62015-04-08 12:13:30 +0100977 uint32_t pte, pde, temp;
978 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
Ben Widawsky87d60b62013-12-06 14:11:29 -0800979
Akash Goel24f3a8c2014-06-17 10:59:42 +0530980 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800981
Michel Thierry09942c62015-04-08 12:13:30 +0100982 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) {
Ben Widawsky87d60b62013-12-06 14:11:29 -0800983 u32 expected;
Michel Thierry07749ef2015-03-16 16:00:54 +0000984 gen6_pte_t *pt_vaddr;
Ben Widawsky06fda602015-02-24 16:22:36 +0000985 dma_addr_t pt_addr = ppgtt->pd.page_table[pde]->daddr;
Michel Thierry09942c62015-04-08 12:13:30 +0100986 pd_entry = readl(ppgtt->pd_addr + pde);
Ben Widawsky87d60b62013-12-06 14:11:29 -0800987 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
988
989 if (pd_entry != expected)
990 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
991 pde,
992 pd_entry,
993 expected);
994 seq_printf(m, "\tPDE: %x\n", pd_entry);
995
Ben Widawsky06fda602015-02-24 16:22:36 +0000996 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[pde]->page);
Michel Thierry07749ef2015-03-16 16:00:54 +0000997 for (pte = 0; pte < GEN6_PTES; pte+=4) {
Ben Widawsky87d60b62013-12-06 14:11:29 -0800998 unsigned long va =
Michel Thierry07749ef2015-03-16 16:00:54 +0000999 (pde * PAGE_SIZE * GEN6_PTES) +
Ben Widawsky87d60b62013-12-06 14:11:29 -08001000 (pte * PAGE_SIZE);
1001 int i;
1002 bool found = false;
1003 for (i = 0; i < 4; i++)
1004 if (pt_vaddr[pte + i] != scratch_pte)
1005 found = true;
1006 if (!found)
1007 continue;
1008
1009 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1010 for (i = 0; i < 4; i++) {
1011 if (pt_vaddr[pte + i] != scratch_pte)
1012 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1013 else
1014 seq_puts(m, " SCRATCH ");
1015 }
1016 seq_puts(m, "\n");
1017 }
1018 kunmap_atomic(pt_vaddr);
1019 }
1020}
1021
Ben Widawsky678d96f2015-03-16 16:00:56 +00001022/* Write pde (index) from the page directory @pd to the page table @pt */
Michel Thierryec565b32015-04-08 12:13:23 +01001023static void gen6_write_pde(struct i915_page_directory *pd,
1024 const int pde, struct i915_page_table *pt)
Ben Widawsky61973492013-04-08 18:43:54 -07001025{
Ben Widawsky678d96f2015-03-16 16:00:56 +00001026 /* Caller needs to make sure the write completes if necessary */
1027 struct i915_hw_ppgtt *ppgtt =
1028 container_of(pd, struct i915_hw_ppgtt, pd);
1029 u32 pd_entry;
Ben Widawsky61973492013-04-08 18:43:54 -07001030
Ben Widawsky678d96f2015-03-16 16:00:56 +00001031 pd_entry = GEN6_PDE_ADDR_ENCODE(pt->daddr);
1032 pd_entry |= GEN6_PDE_VALID;
Ben Widawsky61973492013-04-08 18:43:54 -07001033
Ben Widawsky678d96f2015-03-16 16:00:56 +00001034 writel(pd_entry, ppgtt->pd_addr + pde);
1035}
Ben Widawsky61973492013-04-08 18:43:54 -07001036
Ben Widawsky678d96f2015-03-16 16:00:56 +00001037/* Write all the page tables found in the ppgtt structure to incrementing page
1038 * directories. */
1039static void gen6_write_page_range(struct drm_i915_private *dev_priv,
Michel Thierryec565b32015-04-08 12:13:23 +01001040 struct i915_page_directory *pd,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001041 uint32_t start, uint32_t length)
1042{
Michel Thierryec565b32015-04-08 12:13:23 +01001043 struct i915_page_table *pt;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001044 uint32_t pde, temp;
1045
1046 gen6_for_each_pde(pt, pd, start, length, temp, pde)
1047 gen6_write_pde(pd, pde, pt);
1048
1049 /* Make sure write is complete before other code can use this page
1050 * table. Also require for WC mapped PTEs */
1051 readl(dev_priv->gtt.gsm);
Ben Widawsky3e302542013-04-23 23:15:32 -07001052}
1053
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001054static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
Ben Widawsky3e302542013-04-23 23:15:32 -07001055{
Ben Widawsky7324cc02015-02-24 16:22:35 +00001056 BUG_ON(ppgtt->pd.pd_offset & 0x3f);
Ben Widawsky3e302542013-04-23 23:15:32 -07001057
Ben Widawsky7324cc02015-02-24 16:22:35 +00001058 return (ppgtt->pd.pd_offset / 64) << 16;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001059}
Ben Widawsky61973492013-04-08 18:43:54 -07001060
Ben Widawsky90252e52013-12-06 14:11:12 -08001061static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +01001062 struct intel_engine_cs *ring)
Ben Widawsky90252e52013-12-06 14:11:12 -08001063{
Ben Widawsky90252e52013-12-06 14:11:12 -08001064 int ret;
Ben Widawsky61973492013-04-08 18:43:54 -07001065
Ben Widawsky90252e52013-12-06 14:11:12 -08001066 /* NB: TLBs must be flushed and invalidated before a switch */
1067 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1068 if (ret)
1069 return ret;
1070
1071 ret = intel_ring_begin(ring, 6);
1072 if (ret)
1073 return ret;
1074
1075 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1076 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1077 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1078 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1079 intel_ring_emit(ring, get_pd_offset(ppgtt));
1080 intel_ring_emit(ring, MI_NOOP);
1081 intel_ring_advance(ring);
1082
1083 return 0;
1084}
1085
Yu Zhang71ba2d62015-02-10 19:05:54 +08001086static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
1087 struct intel_engine_cs *ring)
1088{
1089 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1090
1091 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1092 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1093 return 0;
1094}
1095
Ben Widawsky48a10382013-12-06 14:11:11 -08001096static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +01001097 struct intel_engine_cs *ring)
Ben Widawsky48a10382013-12-06 14:11:11 -08001098{
Ben Widawsky48a10382013-12-06 14:11:11 -08001099 int ret;
1100
Ben Widawsky48a10382013-12-06 14:11:11 -08001101 /* NB: TLBs must be flushed and invalidated before a switch */
1102 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1103 if (ret)
1104 return ret;
1105
1106 ret = intel_ring_begin(ring, 6);
1107 if (ret)
1108 return ret;
1109
1110 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1111 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1112 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1113 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1114 intel_ring_emit(ring, get_pd_offset(ppgtt));
1115 intel_ring_emit(ring, MI_NOOP);
1116 intel_ring_advance(ring);
1117
Ben Widawsky90252e52013-12-06 14:11:12 -08001118 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1119 if (ring->id != RCS) {
1120 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1121 if (ret)
1122 return ret;
1123 }
1124
Ben Widawsky48a10382013-12-06 14:11:11 -08001125 return 0;
1126}
1127
Ben Widawskyeeb94882013-12-06 14:11:10 -08001128static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
McAulay, Alistair6689c162014-08-15 18:51:35 +01001129 struct intel_engine_cs *ring)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001130{
1131 struct drm_device *dev = ppgtt->base.dev;
1132 struct drm_i915_private *dev_priv = dev->dev_private;
1133
Ben Widawsky48a10382013-12-06 14:11:11 -08001134
Ben Widawskyeeb94882013-12-06 14:11:10 -08001135 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1136 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1137
1138 POSTING_READ(RING_PP_DIR_DCLV(ring));
1139
1140 return 0;
1141}
1142
Daniel Vetter82460d92014-08-06 20:19:53 +02001143static void gen8_ppgtt_enable(struct drm_device *dev)
Ben Widawskyeeb94882013-12-06 14:11:10 -08001144{
Ben Widawskyeeb94882013-12-06 14:11:10 -08001145 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001146 struct intel_engine_cs *ring;
Daniel Vetter82460d92014-08-06 20:19:53 +02001147 int j;
Ben Widawskyeeb94882013-12-06 14:11:10 -08001148
1149 for_each_ring(ring, dev_priv, j) {
1150 I915_WRITE(RING_MODE_GEN7(ring),
1151 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawskyeeb94882013-12-06 14:11:10 -08001152 }
Ben Widawskyeeb94882013-12-06 14:11:10 -08001153}
1154
Daniel Vetter82460d92014-08-06 20:19:53 +02001155static void gen7_ppgtt_enable(struct drm_device *dev)
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001156{
Jani Nikula50227e12014-03-31 14:27:21 +03001157 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001158 struct intel_engine_cs *ring;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001159 uint32_t ecochk, ecobits;
1160 int i;
1161
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001162 ecobits = I915_READ(GAC_ECO_BITS);
1163 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1164
1165 ecochk = I915_READ(GAM_ECOCHK);
1166 if (IS_HASWELL(dev)) {
1167 ecochk |= ECOCHK_PPGTT_WB_HSW;
1168 } else {
1169 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1170 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1171 }
1172 I915_WRITE(GAM_ECOCHK, ecochk);
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001173
Ben Widawsky61973492013-04-08 18:43:54 -07001174 for_each_ring(ring, dev_priv, i) {
Ben Widawskyeeb94882013-12-06 14:11:10 -08001175 /* GFX_MODE is per-ring on gen7+ */
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001176 I915_WRITE(RING_MODE_GEN7(ring),
1177 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001178 }
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001179}
1180
Daniel Vetter82460d92014-08-06 20:19:53 +02001181static void gen6_ppgtt_enable(struct drm_device *dev)
Ben Widawsky61973492013-04-08 18:43:54 -07001182{
Jani Nikula50227e12014-03-31 14:27:21 +03001183 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001184 uint32_t ecochk, gab_ctl, ecobits;
Ben Widawsky61973492013-04-08 18:43:54 -07001185
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001186 ecobits = I915_READ(GAC_ECO_BITS);
1187 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1188 ECOBITS_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001189
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001190 gab_ctl = I915_READ(GAB_CTL);
1191 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
Ben Widawsky61973492013-04-08 18:43:54 -07001192
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001193 ecochk = I915_READ(GAM_ECOCHK);
1194 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
Ben Widawsky61973492013-04-08 18:43:54 -07001195
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001196 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
Ben Widawsky61973492013-04-08 18:43:54 -07001197}
1198
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001199/* PPGTT support for Sandybdrige/Gen6 and later */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001200static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001201 uint64_t start,
1202 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001203 bool use_scratch)
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001204{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001205 struct i915_hw_ppgtt *ppgtt =
1206 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +00001207 gen6_pte_t *pt_vaddr, scratch_pte;
Ben Widawsky782f1492014-02-20 11:50:33 -08001208 unsigned first_entry = start >> PAGE_SHIFT;
1209 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001210 unsigned act_pt = first_entry / GEN6_PTES;
1211 unsigned first_pte = first_entry % GEN6_PTES;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001212 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001213
Akash Goel24f3a8c2014-06-17 10:59:42 +05301214 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001215
Daniel Vetter7bddb012012-02-09 17:15:47 +01001216 while (num_entries) {
1217 last_pte = first_pte + num_entries;
Michel Thierry07749ef2015-03-16 16:00:54 +00001218 if (last_pte > GEN6_PTES)
1219 last_pte = GEN6_PTES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001220
Ben Widawsky06fda602015-02-24 16:22:36 +00001221 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
Daniel Vetter7bddb012012-02-09 17:15:47 +01001222
1223 for (i = first_pte; i < last_pte; i++)
1224 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001225
1226 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001227
Daniel Vetter7bddb012012-02-09 17:15:47 +01001228 num_entries -= last_pte - first_pte;
1229 first_pte = 0;
Daniel Vettera15326a2013-03-19 23:48:39 +01001230 act_pt++;
Daniel Vetter7bddb012012-02-09 17:15:47 +01001231 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001232}
1233
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001234static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
Daniel Vetterdef886c2013-01-24 14:44:56 -08001235 struct sg_table *pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001236 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301237 enum i915_cache_level cache_level, u32 flags)
Daniel Vetterdef886c2013-01-24 14:44:56 -08001238{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001239 struct i915_hw_ppgtt *ppgtt =
1240 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry07749ef2015-03-16 16:00:54 +00001241 gen6_pte_t *pt_vaddr;
Ben Widawsky782f1492014-02-20 11:50:33 -08001242 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001243 unsigned act_pt = first_entry / GEN6_PTES;
1244 unsigned act_pte = first_entry % GEN6_PTES;
Imre Deak6e995e22013-02-18 19:28:04 +02001245 struct sg_page_iter sg_iter;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001246
Chris Wilsoncc797142013-12-31 15:50:30 +00001247 pt_vaddr = NULL;
Imre Deak6e995e22013-02-18 19:28:04 +02001248 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
Chris Wilsoncc797142013-12-31 15:50:30 +00001249 if (pt_vaddr == NULL)
Ben Widawsky06fda602015-02-24 16:22:36 +00001250 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001251
Chris Wilsoncc797142013-12-31 15:50:30 +00001252 pt_vaddr[act_pte] =
1253 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
Akash Goel24f3a8c2014-06-17 10:59:42 +05301254 cache_level, true, flags);
1255
Michel Thierry07749ef2015-03-16 16:00:54 +00001256 if (++act_pte == GEN6_PTES) {
Imre Deak6e995e22013-02-18 19:28:04 +02001257 kunmap_atomic(pt_vaddr);
Chris Wilsoncc797142013-12-31 15:50:30 +00001258 pt_vaddr = NULL;
Daniel Vettera15326a2013-03-19 23:48:39 +01001259 act_pt++;
Imre Deak6e995e22013-02-18 19:28:04 +02001260 act_pte = 0;
Daniel Vetterdef886c2013-01-24 14:44:56 -08001261 }
Daniel Vetterdef886c2013-01-24 14:44:56 -08001262 }
Chris Wilsoncc797142013-12-31 15:50:30 +00001263 if (pt_vaddr)
1264 kunmap_atomic(pt_vaddr);
Daniel Vetterdef886c2013-01-24 14:44:56 -08001265}
1266
Ben Widawsky563222a2015-03-19 12:53:28 +00001267/* PDE TLBs are a pain invalidate pre GEN8. It requires a context reload. If we
1268 * are switching between contexts with the same LRCA, we also must do a force
1269 * restore.
1270 */
1271static inline void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1272{
1273 /* If current vm != vm, */
1274 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1275}
1276
Michel Thierry4933d512015-03-24 15:46:22 +00001277static void gen6_initialize_pt(struct i915_address_space *vm,
Michel Thierryec565b32015-04-08 12:13:23 +01001278 struct i915_page_table *pt)
Michel Thierry4933d512015-03-24 15:46:22 +00001279{
1280 gen6_pte_t *pt_vaddr, scratch_pte;
1281 int i;
1282
1283 WARN_ON(vm->scratch.addr == 0);
1284
1285 scratch_pte = vm->pte_encode(vm->scratch.addr,
1286 I915_CACHE_LLC, true, 0);
1287
1288 pt_vaddr = kmap_atomic(pt->page);
1289
1290 for (i = 0; i < GEN6_PTES; i++)
1291 pt_vaddr[i] = scratch_pte;
1292
1293 kunmap_atomic(pt_vaddr);
1294}
1295
Ben Widawsky678d96f2015-03-16 16:00:56 +00001296static int gen6_alloc_va_range(struct i915_address_space *vm,
1297 uint64_t start, uint64_t length)
1298{
Michel Thierry4933d512015-03-24 15:46:22 +00001299 DECLARE_BITMAP(new_page_tables, I915_PDES);
1300 struct drm_device *dev = vm->dev;
1301 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001302 struct i915_hw_ppgtt *ppgtt =
1303 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierryec565b32015-04-08 12:13:23 +01001304 struct i915_page_table *pt;
Michel Thierry4933d512015-03-24 15:46:22 +00001305 const uint32_t start_save = start, length_save = length;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001306 uint32_t pde, temp;
Michel Thierry4933d512015-03-24 15:46:22 +00001307 int ret;
1308
1309 WARN_ON(upper_32_bits(start));
1310
1311 bitmap_zero(new_page_tables, I915_PDES);
1312
1313 /* The allocation is done in two stages so that we can bail out with
1314 * minimal amount of pain. The first stage finds new page tables that
1315 * need allocation. The second stage marks use ptes within the page
1316 * tables.
1317 */
1318 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1319 if (pt != ppgtt->scratch_pt) {
1320 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1321 continue;
1322 }
1323
1324 /* We've already allocated a page table */
1325 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1326
1327 pt = alloc_pt_single(dev);
1328 if (IS_ERR(pt)) {
1329 ret = PTR_ERR(pt);
1330 goto unwind_out;
1331 }
1332
1333 gen6_initialize_pt(vm, pt);
1334
1335 ppgtt->pd.page_table[pde] = pt;
1336 set_bit(pde, new_page_tables);
Michel Thierry72744cb2015-03-24 15:46:23 +00001337 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
Michel Thierry4933d512015-03-24 15:46:22 +00001338 }
1339
1340 start = start_save;
1341 length = length_save;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001342
1343 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1344 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1345
1346 bitmap_zero(tmp_bitmap, GEN6_PTES);
1347 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1348 gen6_pte_count(start, length));
1349
Michel Thierry4933d512015-03-24 15:46:22 +00001350 if (test_and_clear_bit(pde, new_page_tables))
1351 gen6_write_pde(&ppgtt->pd, pde, pt);
1352
Michel Thierry72744cb2015-03-24 15:46:23 +00001353 trace_i915_page_table_entry_map(vm, pde, pt,
1354 gen6_pte_index(start),
1355 gen6_pte_count(start, length),
1356 GEN6_PTES);
Michel Thierry4933d512015-03-24 15:46:22 +00001357 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
Ben Widawsky678d96f2015-03-16 16:00:56 +00001358 GEN6_PTES);
1359 }
1360
Michel Thierry4933d512015-03-24 15:46:22 +00001361 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1362
1363 /* Make sure write is complete before other code can use this page
1364 * table. Also require for WC mapped PTEs */
1365 readl(dev_priv->gtt.gsm);
1366
Ben Widawsky563222a2015-03-19 12:53:28 +00001367 mark_tlbs_dirty(ppgtt);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001368 return 0;
Michel Thierry4933d512015-03-24 15:46:22 +00001369
1370unwind_out:
1371 for_each_set_bit(pde, new_page_tables, I915_PDES) {
Michel Thierryec565b32015-04-08 12:13:23 +01001372 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
Michel Thierry4933d512015-03-24 15:46:22 +00001373
1374 ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
1375 unmap_and_free_pt(pt, vm->dev);
1376 }
1377
1378 mark_tlbs_dirty(ppgtt);
1379 return ret;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001380}
1381
Daniel Vetter061dd492015-04-14 17:35:13 +02001382static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
Ben Widawskya00d8252014-02-19 22:05:48 -08001383{
Daniel Vetter061dd492015-04-14 17:35:13 +02001384 struct i915_hw_ppgtt *ppgtt =
1385 container_of(vm, struct i915_hw_ppgtt, base);
Michel Thierry09942c62015-04-08 12:13:30 +01001386 struct i915_page_table *pt;
1387 uint32_t pde;
Daniel Vetter3440d262013-01-24 13:49:56 -08001388
Daniel Vetter061dd492015-04-14 17:35:13 +02001389
1390 drm_mm_remove_node(&ppgtt->node);
1391
Michel Thierry09942c62015-04-08 12:13:30 +01001392 gen6_for_all_pdes(pt, ppgtt, pde) {
Michel Thierry4933d512015-03-24 15:46:22 +00001393 if (pt != ppgtt->scratch_pt)
Michel Thierry09942c62015-04-08 12:13:30 +01001394 unmap_and_free_pt(pt, ppgtt->base.dev);
Michel Thierry4933d512015-03-24 15:46:22 +00001395 }
1396
1397 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
Michel Thierrye5815a22015-04-08 12:13:32 +01001398 unmap_and_free_pd(&ppgtt->pd, ppgtt->base.dev);
Daniel Vetter3440d262013-01-24 13:49:56 -08001399}
1400
Ben Widawskyb1465202014-02-19 22:05:49 -08001401static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001402{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001403 struct drm_device *dev = ppgtt->base.dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001404 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001405 bool retried = false;
Ben Widawskyb1465202014-02-19 22:05:49 -08001406 int ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001407
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001408 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1409 * allocator works in address space sizes, so it's multiplied by page
1410 * size. We allocate at the top of the GTT to avoid fragmentation.
1411 */
1412 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
Michel Thierry4933d512015-03-24 15:46:22 +00001413 ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
1414 if (IS_ERR(ppgtt->scratch_pt))
1415 return PTR_ERR(ppgtt->scratch_pt);
1416
1417 gen6_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
1418
Ben Widawskye3cc1992013-12-06 14:11:08 -08001419alloc:
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001420 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1421 &ppgtt->node, GEN6_PD_SIZE,
1422 GEN6_PD_ALIGN, 0,
1423 0, dev_priv->gtt.base.total,
Ben Widawsky3e8b5ae2014-05-06 22:21:30 -07001424 DRM_MM_TOPDOWN);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001425 if (ret == -ENOSPC && !retried) {
1426 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1427 GEN6_PD_SIZE, GEN6_PD_ALIGN,
Chris Wilsond23db882014-05-23 08:48:08 +02001428 I915_CACHE_NONE,
1429 0, dev_priv->gtt.base.total,
1430 0);
Ben Widawskye3cc1992013-12-06 14:11:08 -08001431 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001432 goto err_out;
Ben Widawskye3cc1992013-12-06 14:11:08 -08001433
1434 retried = true;
1435 goto alloc;
1436 }
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001437
Ben Widawskyc8c26622015-01-22 17:01:25 +00001438 if (ret)
Ben Widawsky678d96f2015-03-16 16:00:56 +00001439 goto err_out;
1440
Ben Widawskyc8c26622015-01-22 17:01:25 +00001441
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001442 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1443 DRM_DEBUG("Forced to use aperture for PDEs\n");
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001444
Ben Widawskyc8c26622015-01-22 17:01:25 +00001445 return 0;
Ben Widawsky678d96f2015-03-16 16:00:56 +00001446
1447err_out:
Michel Thierry4933d512015-03-24 15:46:22 +00001448 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
Ben Widawsky678d96f2015-03-16 16:00:56 +00001449 return ret;
Ben Widawskyb1465202014-02-19 22:05:49 -08001450}
1451
Ben Widawskyb1465202014-02-19 22:05:49 -08001452static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1453{
kbuild test robot2f2cf682015-03-27 19:26:35 +08001454 return gen6_ppgtt_allocate_page_directories(ppgtt);
Ben Widawskyb1465202014-02-19 22:05:49 -08001455}
1456
Michel Thierry4933d512015-03-24 15:46:22 +00001457static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
1458 uint64_t start, uint64_t length)
1459{
Michel Thierryec565b32015-04-08 12:13:23 +01001460 struct i915_page_table *unused;
Michel Thierry4933d512015-03-24 15:46:22 +00001461 uint32_t pde, temp;
1462
1463 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
1464 ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
1465}
1466
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001467static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
Ben Widawskyb1465202014-02-19 22:05:49 -08001468{
1469 struct drm_device *dev = ppgtt->base.dev;
1470 struct drm_i915_private *dev_priv = dev->dev_private;
1471 int ret;
1472
1473 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
Ben Widawsky48a10382013-12-06 14:11:11 -08001474 if (IS_GEN6(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001475 ppgtt->switch_mm = gen6_mm_switch;
Ben Widawsky90252e52013-12-06 14:11:12 -08001476 } else if (IS_HASWELL(dev)) {
Ben Widawsky90252e52013-12-06 14:11:12 -08001477 ppgtt->switch_mm = hsw_mm_switch;
Ben Widawsky48a10382013-12-06 14:11:11 -08001478 } else if (IS_GEN7(dev)) {
Ben Widawsky48a10382013-12-06 14:11:11 -08001479 ppgtt->switch_mm = gen7_mm_switch;
1480 } else
Ben Widawskyb4a74e32013-12-06 14:11:09 -08001481 BUG();
Ben Widawskyb1465202014-02-19 22:05:49 -08001482
Yu Zhang71ba2d62015-02-10 19:05:54 +08001483 if (intel_vgpu_active(dev))
1484 ppgtt->switch_mm = vgpu_mm_switch;
1485
Ben Widawskyb1465202014-02-19 22:05:49 -08001486 ret = gen6_ppgtt_alloc(ppgtt);
1487 if (ret)
1488 return ret;
1489
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001490 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001491 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1492 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02001493 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1494 ppgtt->base.bind_vma = ppgtt_bind_vma;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001495 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
Ben Widawsky686e1f62013-11-25 09:54:34 -08001496 ppgtt->base.start = 0;
Michel Thierry09942c62015-04-08 12:13:30 +01001497 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
Ben Widawskyb1465202014-02-19 22:05:49 -08001498 ppgtt->debug_dump = gen6_dump_ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001499
Ben Widawsky7324cc02015-02-24 16:22:35 +00001500 ppgtt->pd.pd_offset =
Michel Thierry07749ef2015-03-16 16:00:54 +00001501 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001502
Ben Widawsky678d96f2015-03-16 16:00:56 +00001503 ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
1504 ppgtt->pd.pd_offset / sizeof(gen6_pte_t);
1505
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001506 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001507
Ben Widawsky678d96f2015-03-16 16:00:56 +00001508 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
1509
Thierry Reding440fd522015-01-23 09:05:06 +01001510 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
Ben Widawskyc8d4c0d2013-12-06 14:11:07 -08001511 ppgtt->node.size >> 20,
1512 ppgtt->node.start / PAGE_SIZE);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001513
Daniel Vetterfa76da32014-08-06 20:19:54 +02001514 DRM_DEBUG("Adding PPGTT at offset %x\n",
Ben Widawsky7324cc02015-02-24 16:22:35 +00001515 ppgtt->pd.pd_offset << 10);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001516
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001517 return 0;
Daniel Vetter3440d262013-01-24 13:49:56 -08001518}
1519
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001520static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
Daniel Vetter3440d262013-01-24 13:49:56 -08001521{
1522 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter3440d262013-01-24 13:49:56 -08001523
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001524 ppgtt->base.dev = dev;
Ben Widawsky8407bb92014-03-08 11:58:16 -08001525 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
Daniel Vetter3440d262013-01-24 13:49:56 -08001526
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001527 if (INTEL_INFO(dev)->gen < 8)
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001528 return gen6_ppgtt_init(ppgtt);
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001529 else
Michel Thierryd7b26332015-04-08 12:13:34 +01001530 return gen8_ppgtt_init(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001531}
1532int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1533{
1534 struct drm_i915_private *dev_priv = dev->dev_private;
1535 int ret = 0;
Ben Widawsky3ed124b2013-04-08 18:43:53 -07001536
Daniel Vetter5c5f6452015-04-14 17:35:14 +02001537 ret = __hw_ppgtt_init(dev, ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02001538 if (ret == 0) {
Ben Widawskyc7c48df2013-12-06 14:11:15 -08001539 kref_init(&ppgtt->ref);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001540 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1541 ppgtt->base.total);
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08001542 i915_init_vm(dev_priv, &ppgtt->base);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001543 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001544
1545 return ret;
1546}
1547
Daniel Vetter82460d92014-08-06 20:19:53 +02001548int i915_ppgtt_init_hw(struct drm_device *dev)
1549{
1550 struct drm_i915_private *dev_priv = dev->dev_private;
1551 struct intel_engine_cs *ring;
1552 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1553 int i, ret = 0;
1554
Thomas Daniel671b50132014-08-20 16:24:50 +01001555 /* In the case of execlists, PPGTT is enabled by the context descriptor
1556 * and the PDPs are contained within the context itself. We don't
1557 * need to do anything here. */
1558 if (i915.enable_execlists)
1559 return 0;
1560
Daniel Vetter82460d92014-08-06 20:19:53 +02001561 if (!USES_PPGTT(dev))
1562 return 0;
1563
1564 if (IS_GEN6(dev))
1565 gen6_ppgtt_enable(dev);
1566 else if (IS_GEN7(dev))
1567 gen7_ppgtt_enable(dev);
1568 else if (INTEL_INFO(dev)->gen >= 8)
1569 gen8_ppgtt_enable(dev);
1570 else
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01001571 MISSING_CASE(INTEL_INFO(dev)->gen);
Daniel Vetter82460d92014-08-06 20:19:53 +02001572
1573 if (ppgtt) {
1574 for_each_ring(ring, dev_priv, i) {
McAulay, Alistair6689c162014-08-15 18:51:35 +01001575 ret = ppgtt->switch_mm(ppgtt, ring);
Daniel Vetter82460d92014-08-06 20:19:53 +02001576 if (ret != 0)
1577 return ret;
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001578 }
1579 }
1580
1581 return ret;
1582}
Daniel Vetter4d884702014-08-06 15:04:47 +02001583struct i915_hw_ppgtt *
1584i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
1585{
1586 struct i915_hw_ppgtt *ppgtt;
1587 int ret;
1588
1589 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1590 if (!ppgtt)
1591 return ERR_PTR(-ENOMEM);
1592
1593 ret = i915_ppgtt_init(dev, ppgtt);
1594 if (ret) {
1595 kfree(ppgtt);
1596 return ERR_PTR(ret);
1597 }
1598
1599 ppgtt->file_priv = fpriv;
1600
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001601 trace_i915_ppgtt_create(&ppgtt->base);
1602
Daniel Vetter4d884702014-08-06 15:04:47 +02001603 return ppgtt;
1604}
1605
Daniel Vetteree960be2014-08-06 15:04:45 +02001606void i915_ppgtt_release(struct kref *kref)
1607{
1608 struct i915_hw_ppgtt *ppgtt =
1609 container_of(kref, struct i915_hw_ppgtt, ref);
1610
Daniele Ceraolo Spurio198c9742014-11-10 13:44:31 +00001611 trace_i915_ppgtt_release(&ppgtt->base);
1612
Daniel Vetteree960be2014-08-06 15:04:45 +02001613 /* vmas should already be unbound */
1614 WARN_ON(!list_empty(&ppgtt->base.active_list));
1615 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
1616
Daniel Vetter19dd1202014-08-06 15:04:55 +02001617 list_del(&ppgtt->base.global_link);
1618 drm_mm_takedown(&ppgtt->base.mm);
1619
Daniel Vetteree960be2014-08-06 15:04:45 +02001620 ppgtt->base.cleanup(&ppgtt->base);
1621 kfree(ppgtt);
1622}
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001623
Ben Widawskya81cc002013-01-18 12:30:31 -08001624extern int intel_iommu_gfx_mapped;
1625/* Certain Gen5 chipsets require require idling the GPU before
1626 * unmapping anything from the GTT when VT-d is enabled.
1627 */
1628static inline bool needs_idle_maps(struct drm_device *dev)
1629{
1630#ifdef CONFIG_INTEL_IOMMU
1631 /* Query intel_iommu to see if we need the workaround. Presumably that
1632 * was loaded first.
1633 */
1634 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1635 return true;
1636#endif
1637 return false;
1638}
1639
Ben Widawsky5c042282011-10-17 15:51:55 -07001640static bool do_idling(struct drm_i915_private *dev_priv)
1641{
1642 bool ret = dev_priv->mm.interruptible;
1643
Ben Widawskya81cc002013-01-18 12:30:31 -08001644 if (unlikely(dev_priv->gtt.do_idle_maps)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001645 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001646 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -07001647 DRM_ERROR("Couldn't idle GPU\n");
1648 /* Wait a bit, in hopes it avoids the hang */
1649 udelay(10);
1650 }
1651 }
1652
1653 return ret;
1654}
1655
1656static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1657{
Ben Widawskya81cc002013-01-18 12:30:31 -08001658 if (unlikely(dev_priv->gtt.do_idle_maps))
Ben Widawsky5c042282011-10-17 15:51:55 -07001659 dev_priv->mm.interruptible = interruptible;
1660}
1661
Ben Widawsky828c7902013-10-16 09:21:30 -07001662void i915_check_and_clear_faults(struct drm_device *dev)
1663{
1664 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001665 struct intel_engine_cs *ring;
Ben Widawsky828c7902013-10-16 09:21:30 -07001666 int i;
1667
1668 if (INTEL_INFO(dev)->gen < 6)
1669 return;
1670
1671 for_each_ring(ring, dev_priv, i) {
1672 u32 fault_reg;
1673 fault_reg = I915_READ(RING_FAULT_REG(ring));
1674 if (fault_reg & RING_FAULT_VALID) {
1675 DRM_DEBUG_DRIVER("Unexpected fault\n"
Paulo Zanoni59a5d292014-10-30 15:52:45 -02001676 "\tAddr: 0x%08lx\n"
Ben Widawsky828c7902013-10-16 09:21:30 -07001677 "\tAddress space: %s\n"
1678 "\tSource ID: %d\n"
1679 "\tType: %d\n",
1680 fault_reg & PAGE_MASK,
1681 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1682 RING_FAULT_SRCID(fault_reg),
1683 RING_FAULT_FAULT_TYPE(fault_reg));
1684 I915_WRITE(RING_FAULT_REG(ring),
1685 fault_reg & ~RING_FAULT_VALID);
1686 }
1687 }
1688 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1689}
1690
Chris Wilson91e56492014-09-25 10:13:12 +01001691static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
1692{
1693 if (INTEL_INFO(dev_priv->dev)->gen < 6) {
1694 intel_gtt_chipset_flush();
1695 } else {
1696 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1697 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1698 }
1699}
1700
Ben Widawsky828c7902013-10-16 09:21:30 -07001701void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1702{
1703 struct drm_i915_private *dev_priv = dev->dev_private;
1704
1705 /* Don't bother messing with faults pre GEN6 as we have little
1706 * documentation supporting that it's a good idea.
1707 */
1708 if (INTEL_INFO(dev)->gen < 6)
1709 return;
1710
1711 i915_check_and_clear_faults(dev);
1712
1713 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001714 dev_priv->gtt.base.start,
1715 dev_priv->gtt.base.total,
Daniel Vettere568af12014-03-26 20:08:20 +01001716 true);
Chris Wilson91e56492014-09-25 10:13:12 +01001717
1718 i915_ggtt_flush(dev_priv);
Ben Widawsky828c7902013-10-16 09:21:30 -07001719}
1720
Daniel Vetter74163902012-02-15 23:50:21 +01001721int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001722{
Chris Wilson9da3da62012-06-01 15:20:22 +01001723 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +01001724 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +01001725
1726 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1727 obj->pages->sgl, obj->pages->nents,
1728 PCI_DMA_BIDIRECTIONAL))
1729 return -ENOSPC;
1730
1731 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001732}
1733
Michel Thierry07749ef2015-03-16 16:00:54 +00001734static inline void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001735{
1736#ifdef writeq
1737 writeq(pte, addr);
1738#else
1739 iowrite32((u32)pte, addr);
1740 iowrite32(pte >> 32, addr + 4);
1741#endif
1742}
1743
1744static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1745 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001746 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301747 enum i915_cache_level level, u32 unused)
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001748{
1749 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001750 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001751 gen8_pte_t __iomem *gtt_entries =
1752 (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001753 int i = 0;
1754 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001755 dma_addr_t addr = 0; /* shut up gcc */
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001756
1757 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1758 addr = sg_dma_address(sg_iter.sg) +
1759 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1760 gen8_set_pte(&gtt_entries[i],
1761 gen8_pte_encode(addr, level, true));
1762 i++;
1763 }
1764
1765 /*
1766 * XXX: This serves as a posting read to make sure that the PTE has
1767 * actually been updated. There is some concern that even though
1768 * registers and PTEs are within the same BAR that they are potentially
1769 * of NUMA access patterns. Therefore, even with the way we assume
1770 * hardware should work, we must keep this posting read for paranoia.
1771 */
1772 if (i != 0)
1773 WARN_ON(readq(&gtt_entries[i-1])
1774 != gen8_pte_encode(addr, level, true));
1775
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001776 /* This next bit makes the above posting read even more important. We
1777 * want to flush the TLBs only after we're certain all the PTE updates
1778 * have finished.
1779 */
1780 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1781 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001782}
1783
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001784/*
1785 * Binds an object into the global gtt with the specified cache level. The object
1786 * will be accessible to the GPU via commands whose operands reference offsets
1787 * within the global GTT as well as accessible by the GPU through the GMADR
1788 * mapped BAR (dev_priv->mm.gtt->gtt).
1789 */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001790static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001791 struct sg_table *st,
Ben Widawsky782f1492014-02-20 11:50:33 -08001792 uint64_t start,
Akash Goel24f3a8c2014-06-17 10:59:42 +05301793 enum i915_cache_level level, u32 flags)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001794{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001795 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001796 unsigned first_entry = start >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001797 gen6_pte_t __iomem *gtt_entries =
1798 (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
Imre Deak6e995e22013-02-18 19:28:04 +02001799 int i = 0;
1800 struct sg_page_iter sg_iter;
Pavel Machek57007df2014-07-28 13:20:58 +02001801 dma_addr_t addr = 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001802
Imre Deak6e995e22013-02-18 19:28:04 +02001803 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001804 addr = sg_page_iter_dma_address(&sg_iter);
Akash Goel24f3a8c2014-06-17 10:59:42 +05301805 iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
Imre Deak6e995e22013-02-18 19:28:04 +02001806 i++;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001807 }
1808
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001809 /* XXX: This serves as a posting read to make sure that the PTE has
1810 * actually been updated. There is some concern that even though
1811 * registers and PTEs are within the same BAR that they are potentially
1812 * of NUMA access patterns. Therefore, even with the way we assume
1813 * hardware should work, we must keep this posting read for paranoia.
1814 */
Pavel Machek57007df2014-07-28 13:20:58 +02001815 if (i != 0) {
1816 unsigned long gtt = readl(&gtt_entries[i-1]);
1817 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1818 }
Ben Widawsky0f9b91c2012-11-04 09:21:30 -08001819
1820 /* This next bit makes the above posting read even more important. We
1821 * want to flush the TLBs only after we're certain all the PTE updates
1822 * have finished.
1823 */
1824 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1825 POSTING_READ(GFX_FLSH_CNTL_GEN6);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001826}
1827
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001828static void gen8_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001829 uint64_t start,
1830 uint64_t length,
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001831 bool use_scratch)
1832{
1833 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001834 unsigned first_entry = start >> PAGE_SHIFT;
1835 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001836 gen8_pte_t scratch_pte, __iomem *gtt_base =
1837 (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawsky94ec8f62013-11-02 21:07:18 -07001838 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1839 int i;
1840
1841 if (WARN(num_entries > max_entries,
1842 "First entry = %d; Num entries = %d (max=%d)\n",
1843 first_entry, num_entries, max_entries))
1844 num_entries = max_entries;
1845
1846 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1847 I915_CACHE_LLC,
1848 use_scratch);
1849 for (i = 0; i < num_entries; i++)
1850 gen8_set_pte(&gtt_base[i], scratch_pte);
1851 readl(gtt_base);
1852}
1853
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001854static void gen6_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001855 uint64_t start,
1856 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001857 bool use_scratch)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001858{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001859 struct drm_i915_private *dev_priv = vm->dev->dev_private;
Ben Widawsky782f1492014-02-20 11:50:33 -08001860 unsigned first_entry = start >> PAGE_SHIFT;
1861 unsigned num_entries = length >> PAGE_SHIFT;
Michel Thierry07749ef2015-03-16 16:00:54 +00001862 gen6_pte_t scratch_pte, __iomem *gtt_base =
1863 (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
Ben Widawskya54c0c22013-01-24 14:45:00 -08001864 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001865 int i;
1866
1867 if (WARN(num_entries > max_entries,
1868 "First entry = %d; Num entries = %d (max=%d)\n",
1869 first_entry, num_entries, max_entries))
1870 num_entries = max_entries;
1871
Akash Goel24f3a8c2014-06-17 10:59:42 +05301872 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
Ben Widawsky828c7902013-10-16 09:21:30 -07001873
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001874 for (i = 0; i < num_entries; i++)
1875 iowrite32(scratch_pte, &gtt_base[i]);
1876 readl(gtt_base);
1877}
1878
Daniel Vetterd369d2d2015-04-14 17:35:25 +02001879static void i915_ggtt_insert_entries(struct i915_address_space *vm,
1880 struct sg_table *pages,
1881 uint64_t start,
1882 enum i915_cache_level cache_level, u32 unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001883{
1884 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1885 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1886
Daniel Vetterd369d2d2015-04-14 17:35:25 +02001887 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07001888
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001889}
1890
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001891static void i915_ggtt_clear_range(struct i915_address_space *vm,
Ben Widawsky782f1492014-02-20 11:50:33 -08001892 uint64_t start,
1893 uint64_t length,
Ben Widawsky828c7902013-10-16 09:21:30 -07001894 bool unused)
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001895{
Ben Widawsky782f1492014-02-20 11:50:33 -08001896 unsigned first_entry = start >> PAGE_SHIFT;
1897 unsigned num_entries = length >> PAGE_SHIFT;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001898 intel_gtt_clear_range(first_entry, num_entries);
1899}
1900
Ben Widawsky6f65e292013-12-06 14:10:56 -08001901static void ggtt_bind_vma(struct i915_vma *vma,
1902 enum i915_cache_level cache_level,
1903 u32 flags)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001904{
Ben Widawsky6f65e292013-12-06 14:10:56 -08001905 struct drm_device *dev = vma->vm->dev;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001906 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001907 struct drm_i915_gem_object *obj = vma->obj;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001908 struct sg_table *pages = obj->pages;
Daniel Vetterf329f5f2015-04-14 17:35:15 +02001909 u32 pte_flags = 0;
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08001910
Akash Goel24f3a8c2014-06-17 10:59:42 +05301911 /* Currently applicable only to VLV */
1912 if (obj->gt_ro)
Daniel Vetterf329f5f2015-04-14 17:35:15 +02001913 pte_flags |= PTE_READ_ONLY;
Akash Goel24f3a8c2014-06-17 10:59:42 +05301914
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001915 if (i915_is_ggtt(vma->vm))
1916 pages = vma->ggtt_view.pages;
1917
Ben Widawsky6f65e292013-12-06 14:10:56 -08001918 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
Daniel Vetter08755462015-04-20 09:04:05 -07001919 vma->vm->insert_entries(vma->vm, pages,
1920 vma->node.start,
1921 cache_level, pte_flags);
1922
1923 vma->bound |= GLOBAL_BIND;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001924 }
Daniel Vetter74898d72012-02-15 23:50:22 +01001925
Daniel Vetter08755462015-04-20 09:04:05 -07001926 if (dev_priv->mm.aliasing_ppgtt && flags & LOCAL_BIND) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001927 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02001928 appgtt->base.insert_entries(&appgtt->base, pages,
Ben Widawsky782f1492014-02-20 11:50:33 -08001929 vma->node.start,
Daniel Vetterf329f5f2015-04-14 17:35:15 +02001930 cache_level, pte_flags);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001931 }
1932}
1933
1934static void ggtt_unbind_vma(struct i915_vma *vma)
1935{
1936 struct drm_device *dev = vma->vm->dev;
1937 struct drm_i915_private *dev_priv = dev->dev_private;
1938 struct drm_i915_gem_object *obj = vma->obj;
Ben Widawsky6f65e292013-12-06 14:10:56 -08001939
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01001940 if (vma->bound & GLOBAL_BIND) {
Ben Widawsky782f1492014-02-20 11:50:33 -08001941 vma->vm->clear_range(vma->vm,
1942 vma->node.start,
1943 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001944 true);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001945 }
1946
Daniel Vetter08755462015-04-20 09:04:05 -07001947 if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) {
Ben Widawsky6f65e292013-12-06 14:10:56 -08001948 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1949 appgtt->base.clear_range(&appgtt->base,
Ben Widawsky782f1492014-02-20 11:50:33 -08001950 vma->node.start,
1951 obj->base.size,
Ben Widawsky6f65e292013-12-06 14:10:56 -08001952 true);
Ben Widawsky6f65e292013-12-06 14:10:56 -08001953 }
Daniel Vetter74163902012-02-15 23:50:21 +01001954}
1955
1956void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1957{
Ben Widawsky5c042282011-10-17 15:51:55 -07001958 struct drm_device *dev = obj->base.dev;
1959 struct drm_i915_private *dev_priv = dev->dev_private;
1960 bool interruptible;
1961
1962 interruptible = do_idling(dev_priv);
1963
Chris Wilson9da3da62012-06-01 15:20:22 +01001964 if (!obj->has_dma_mapping)
1965 dma_unmap_sg(&dev->pdev->dev,
1966 obj->pages->sgl, obj->pages->nents,
1967 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -07001968
1969 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01001970}
Daniel Vetter644ec022012-03-26 09:45:40 +02001971
Chris Wilson42d6ab42012-07-26 11:49:32 +01001972static void i915_gtt_color_adjust(struct drm_mm_node *node,
1973 unsigned long color,
Thierry Reding440fd522015-01-23 09:05:06 +01001974 u64 *start,
1975 u64 *end)
Chris Wilson42d6ab42012-07-26 11:49:32 +01001976{
1977 if (node->color != color)
1978 *start += 4096;
1979
1980 if (!list_empty(&node->node_list)) {
1981 node = list_entry(node->node_list.next,
1982 struct drm_mm_node,
1983 node_list);
1984 if (node->allocated && node->color != color)
1985 *end -= 4096;
1986 }
1987}
Ben Widawskyfbe5d362013-11-04 19:56:49 -08001988
Daniel Vetterf548c0e2014-11-19 21:40:13 +01001989static int i915_gem_setup_global_gtt(struct drm_device *dev,
1990 unsigned long start,
1991 unsigned long mappable_end,
1992 unsigned long end)
Daniel Vetter644ec022012-03-26 09:45:40 +02001993{
Ben Widawskye78891c2013-01-25 16:41:04 -08001994 /* Let GEM Manage all of the aperture.
1995 *
1996 * However, leave one page at the end still bound to the scratch page.
1997 * There are a number of places where the hardware apparently prefetches
1998 * past the end of the object, and we've seen multiple hangs with the
1999 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2000 * aperture. One page should be enough to keep any prefetching inside
2001 * of the aperture.
2002 */
Ben Widawsky40d749802013-07-31 16:59:59 -07002003 struct drm_i915_private *dev_priv = dev->dev_private;
2004 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002005 struct drm_mm_node *entry;
2006 struct drm_i915_gem_object *obj;
2007 unsigned long hole_start, hole_end;
Daniel Vetterfa76da32014-08-06 20:19:54 +02002008 int ret;
Daniel Vetter644ec022012-03-26 09:45:40 +02002009
Ben Widawsky35451cb2013-01-17 12:45:13 -08002010 BUG_ON(mappable_end > end);
2011
Chris Wilsoned2f3452012-11-15 11:32:19 +00002012 /* Subtract the guard page ... */
Ben Widawsky40d749802013-07-31 16:59:59 -07002013 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002014
2015 dev_priv->gtt.base.start = start;
2016 dev_priv->gtt.base.total = end - start;
2017
2018 if (intel_vgpu_active(dev)) {
2019 ret = intel_vgt_balloon(dev);
2020 if (ret)
2021 return ret;
2022 }
2023
Chris Wilson42d6ab42012-07-26 11:49:32 +01002024 if (!HAS_LLC(dev))
Ben Widawsky93bd8642013-07-16 16:50:06 -07002025 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +02002026
Chris Wilsoned2f3452012-11-15 11:32:19 +00002027 /* Mark any preallocated objects as occupied */
Ben Widawsky35c20a62013-05-31 11:28:48 -07002028 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
Ben Widawsky40d749802013-07-31 16:59:59 -07002029 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002030
Ben Widawskyedd41a82013-07-05 14:41:05 -07002031 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
Ben Widawskyc6cfb322013-07-05 14:41:06 -07002032 i915_gem_obj_ggtt_offset(obj), obj->base.size);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002033
Ben Widawskyc6cfb322013-07-05 14:41:06 -07002034 WARN_ON(i915_gem_obj_ggtt_bound(obj));
Ben Widawsky40d749802013-07-31 16:59:59 -07002035 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002036 if (ret) {
2037 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
2038 return ret;
2039 }
Tvrtko Ursulinaff43762014-10-24 12:42:33 +01002040 vma->bound |= GLOBAL_BIND;
Chris Wilsoned2f3452012-11-15 11:32:19 +00002041 }
2042
Chris Wilsoned2f3452012-11-15 11:32:19 +00002043 /* Clear any non-preallocated blocks */
Ben Widawsky40d749802013-07-31 16:59:59 -07002044 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
Chris Wilsoned2f3452012-11-15 11:32:19 +00002045 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2046 hole_start, hole_end);
Ben Widawsky782f1492014-02-20 11:50:33 -08002047 ggtt_vm->clear_range(ggtt_vm, hole_start,
2048 hole_end - hole_start, true);
Chris Wilsoned2f3452012-11-15 11:32:19 +00002049 }
2050
2051 /* And finally clear the reserved guard page */
Ben Widawsky782f1492014-02-20 11:50:33 -08002052 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002053
Daniel Vetterfa76da32014-08-06 20:19:54 +02002054 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
2055 struct i915_hw_ppgtt *ppgtt;
2056
2057 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2058 if (!ppgtt)
2059 return -ENOMEM;
2060
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002061 ret = __hw_ppgtt_init(dev, ppgtt);
Michel Thierry4933d512015-03-24 15:46:22 +00002062 if (ret) {
Daniel Vetter061dd492015-04-14 17:35:13 +02002063 ppgtt->base.cleanup(&ppgtt->base);
Michel Thierry4933d512015-03-24 15:46:22 +00002064 kfree(ppgtt);
Daniel Vetterfa76da32014-08-06 20:19:54 +02002065 return ret;
Michel Thierry4933d512015-03-24 15:46:22 +00002066 }
Daniel Vetterfa76da32014-08-06 20:19:54 +02002067
Daniel Vetter5c5f6452015-04-14 17:35:14 +02002068 if (ppgtt->base.allocate_va_range)
2069 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2070 ppgtt->base.total);
2071 if (ret) {
2072 ppgtt->base.cleanup(&ppgtt->base);
2073 kfree(ppgtt);
2074 return ret;
2075 }
2076
2077 ppgtt->base.clear_range(&ppgtt->base,
2078 ppgtt->base.start,
2079 ppgtt->base.total,
2080 true);
2081
Daniel Vetterfa76da32014-08-06 20:19:54 +02002082 dev_priv->mm.aliasing_ppgtt = ppgtt;
2083 }
2084
Daniel Vetter6c5566a2014-08-06 15:04:50 +02002085 return 0;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002086}
2087
Ben Widawskyd7e50082012-12-18 10:31:25 -08002088void i915_gem_init_global_gtt(struct drm_device *dev)
2089{
2090 struct drm_i915_private *dev_priv = dev->dev_private;
2091 unsigned long gtt_size, mappable_size;
Ben Widawskyd7e50082012-12-18 10:31:25 -08002092
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002093 gtt_size = dev_priv->gtt.base.total;
Ben Widawsky93d18792013-01-17 12:45:17 -08002094 mappable_size = dev_priv->gtt.mappable_end;
Ben Widawskyd7e50082012-12-18 10:31:25 -08002095
Ben Widawskye78891c2013-01-25 16:41:04 -08002096 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002097}
2098
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002099void i915_global_gtt_cleanup(struct drm_device *dev)
2100{
2101 struct drm_i915_private *dev_priv = dev->dev_private;
2102 struct i915_address_space *vm = &dev_priv->gtt.base;
2103
Daniel Vetter70e32542014-08-06 15:04:57 +02002104 if (dev_priv->mm.aliasing_ppgtt) {
2105 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2106
2107 ppgtt->base.cleanup(&ppgtt->base);
2108 }
2109
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002110 if (drm_mm_initialized(&vm->mm)) {
Yu Zhang5dda8fa2015-02-10 19:05:48 +08002111 if (intel_vgpu_active(dev))
2112 intel_vgt_deballoon();
2113
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02002114 drm_mm_takedown(&vm->mm);
2115 list_del(&vm->global_link);
2116 }
2117
2118 vm->cleanup(vm);
2119}
Daniel Vetter70e32542014-08-06 15:04:57 +02002120
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002121static int setup_scratch_page(struct drm_device *dev)
2122{
2123 struct drm_i915_private *dev_priv = dev->dev_private;
2124 struct page *page;
2125 dma_addr_t dma_addr;
2126
2127 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
2128 if (page == NULL)
2129 return -ENOMEM;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002130 set_pages_uc(page, 1);
2131
2132#ifdef CONFIG_INTEL_IOMMU
2133 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
2134 PCI_DMA_BIDIRECTIONAL);
2135 if (pci_dma_mapping_error(dev->pdev, dma_addr))
2136 return -EINVAL;
2137#else
2138 dma_addr = page_to_phys(page);
2139#endif
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002140 dev_priv->gtt.base.scratch.page = page;
2141 dev_priv->gtt.base.scratch.addr = dma_addr;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002142
2143 return 0;
2144}
2145
2146static void teardown_scratch_page(struct drm_device *dev)
2147{
2148 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002149 struct page *page = dev_priv->gtt.base.scratch.page;
2150
2151 set_pages_wb(page, 1);
2152 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002153 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002154 __free_page(page);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002155}
2156
2157static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2158{
2159 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2160 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2161 return snb_gmch_ctl << 20;
2162}
2163
Ben Widawsky9459d252013-11-03 16:53:55 -08002164static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2165{
2166 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2167 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2168 if (bdw_gmch_ctl)
2169 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
Ben Widawsky562d55d2014-05-27 16:53:08 -07002170
2171#ifdef CONFIG_X86_32
2172 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2173 if (bdw_gmch_ctl > 4)
2174 bdw_gmch_ctl = 4;
2175#endif
2176
Ben Widawsky9459d252013-11-03 16:53:55 -08002177 return bdw_gmch_ctl << 20;
2178}
2179
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002180static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2181{
2182 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2183 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2184
2185 if (gmch_ctrl)
2186 return 1 << (20 + gmch_ctrl);
2187
2188 return 0;
2189}
2190
Ben Widawskybaa09f52013-01-24 13:49:57 -08002191static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002192{
2193 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2194 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2195 return snb_gmch_ctl << 25; /* 32 MB units */
2196}
2197
Ben Widawsky9459d252013-11-03 16:53:55 -08002198static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
2199{
2200 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2201 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2202 return bdw_gmch_ctl << 25; /* 32 MB units */
2203}
2204
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002205static size_t chv_get_stolen_size(u16 gmch_ctrl)
2206{
2207 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2208 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2209
2210 /*
2211 * 0x0 to 0x10: 32MB increments starting at 0MB
2212 * 0x11 to 0x16: 4MB increments starting at 8MB
2213 * 0x17 to 0x1d: 4MB increments start at 36MB
2214 */
2215 if (gmch_ctrl < 0x11)
2216 return gmch_ctrl << 25;
2217 else if (gmch_ctrl < 0x17)
2218 return (gmch_ctrl - 0x11 + 2) << 22;
2219 else
2220 return (gmch_ctrl - 0x17 + 9) << 22;
2221}
2222
Damien Lespiau66375012014-01-09 18:02:46 +00002223static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2224{
2225 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2226 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2227
2228 if (gen9_gmch_ctl < 0xf0)
2229 return gen9_gmch_ctl << 25; /* 32 MB units */
2230 else
2231 /* 4MB increments starting at 0xf0 for 4MB */
2232 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2233}
2234
Ben Widawsky63340132013-11-04 19:32:22 -08002235static int ggtt_probe_common(struct drm_device *dev,
2236 size_t gtt_size)
2237{
2238 struct drm_i915_private *dev_priv = dev->dev_private;
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002239 phys_addr_t gtt_phys_addr;
Ben Widawsky63340132013-11-04 19:32:22 -08002240 int ret;
2241
2242 /* For Modern GENs the PTEs and register space are split in the BAR */
Bjorn Helgaas21c34602013-12-21 10:52:52 -07002243 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
Ben Widawsky63340132013-11-04 19:32:22 -08002244 (pci_resource_len(dev->pdev, 0) / 2);
2245
Imre Deak2a073f892015-03-27 13:07:33 +02002246 /*
2247 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2248 * dropped. For WC mappings in general we have 64 byte burst writes
2249 * when the WC buffer is flushed, so we can't use it, but have to
2250 * resort to an uncached mapping. The WC issue is easily caught by the
2251 * readback check when writing GTT PTE entries.
2252 */
2253 if (IS_BROXTON(dev))
2254 dev_priv->gtt.gsm = ioremap_nocache(gtt_phys_addr, gtt_size);
2255 else
2256 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
Ben Widawsky63340132013-11-04 19:32:22 -08002257 if (!dev_priv->gtt.gsm) {
2258 DRM_ERROR("Failed to map the gtt page table\n");
2259 return -ENOMEM;
2260 }
2261
2262 ret = setup_scratch_page(dev);
2263 if (ret) {
2264 DRM_ERROR("Scratch setup failed\n");
2265 /* iounmap will also get called at remove, but meh */
2266 iounmap(dev_priv->gtt.gsm);
2267 }
2268
2269 return ret;
2270}
2271
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002272/* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2273 * bits. When using advanced contexts each context stores its own PAT, but
2274 * writing this data shouldn't be harmful even in those cases. */
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002275static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002276{
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002277 uint64_t pat;
2278
2279 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2280 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2281 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2282 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2283 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2284 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2285 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2286 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2287
Rodrigo Vivid6a8b722014-11-05 16:56:36 -08002288 if (!USES_PPGTT(dev_priv->dev))
2289 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2290 * so RTL will always use the value corresponding to
2291 * pat_sel = 000".
2292 * So let's disable cache for GGTT to avoid screen corruptions.
2293 * MOCS still can be used though.
2294 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2295 * before this patch, i.e. the same uncached + snooping access
2296 * like on gen6/7 seems to be in effect.
2297 * - So this just fixes blitter/render access. Again it looks
2298 * like it's not just uncached access, but uncached + snooping.
2299 * So we can still hold onto all our assumptions wrt cpu
2300 * clflushing on LLC machines.
2301 */
2302 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2303
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002304 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2305 * write would work. */
2306 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2307 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2308}
2309
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002310static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2311{
2312 uint64_t pat;
2313
2314 /*
2315 * Map WB on BDW to snooped on CHV.
2316 *
2317 * Only the snoop bit has meaning for CHV, the rest is
2318 * ignored.
2319 *
Ville Syrjäläcf3d2622014-11-14 21:02:44 +02002320 * The hardware will never snoop for certain types of accesses:
2321 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2322 * - PPGTT page tables
2323 * - some other special cycles
2324 *
2325 * As with BDW, we also need to consider the following for GT accesses:
2326 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2327 * so RTL will always use the value corresponding to
2328 * pat_sel = 000".
2329 * Which means we must set the snoop bit in PAT entry 0
2330 * in order to keep the global status page working.
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002331 */
2332 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2333 GEN8_PPAT(1, 0) |
2334 GEN8_PPAT(2, 0) |
2335 GEN8_PPAT(3, 0) |
2336 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2337 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2338 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2339 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2340
2341 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2342 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2343}
2344
Ben Widawsky63340132013-11-04 19:32:22 -08002345static int gen8_gmch_probe(struct drm_device *dev,
2346 size_t *gtt_total,
2347 size_t *stolen,
2348 phys_addr_t *mappable_base,
2349 unsigned long *mappable_end)
2350{
2351 struct drm_i915_private *dev_priv = dev->dev_private;
2352 unsigned int gtt_size;
2353 u16 snb_gmch_ctl;
2354 int ret;
2355
2356 /* TODO: We're not aware of mappable constraints on gen8 yet */
2357 *mappable_base = pci_resource_start(dev->pdev, 2);
2358 *mappable_end = pci_resource_len(dev->pdev, 2);
2359
2360 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
2361 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
2362
2363 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2364
Damien Lespiau66375012014-01-09 18:02:46 +00002365 if (INTEL_INFO(dev)->gen >= 9) {
2366 *stolen = gen9_get_stolen_size(snb_gmch_ctl);
2367 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2368 } else if (IS_CHERRYVIEW(dev)) {
Damien Lespiaud7f25f22014-05-08 22:19:40 +03002369 *stolen = chv_get_stolen_size(snb_gmch_ctl);
2370 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
2371 } else {
2372 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
2373 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2374 }
Ben Widawsky63340132013-11-04 19:32:22 -08002375
Michel Thierry07749ef2015-03-16 16:00:54 +00002376 *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
Ben Widawsky63340132013-11-04 19:32:22 -08002377
Sumit Singh5a4e33a2015-03-17 11:39:31 +02002378 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
Ville Syrjäläee0ce472014-04-09 13:28:01 +03002379 chv_setup_private_ppat(dev_priv);
2380 else
2381 bdw_setup_private_ppat(dev_priv);
Ben Widawskyfbe5d362013-11-04 19:56:49 -08002382
Ben Widawsky63340132013-11-04 19:32:22 -08002383 ret = ggtt_probe_common(dev, gtt_size);
2384
Ben Widawsky94ec8f62013-11-02 21:07:18 -07002385 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
2386 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002387 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
2388 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
Ben Widawsky63340132013-11-04 19:32:22 -08002389
2390 return ret;
2391}
2392
Ben Widawskybaa09f52013-01-24 13:49:57 -08002393static int gen6_gmch_probe(struct drm_device *dev,
2394 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002395 size_t *stolen,
2396 phys_addr_t *mappable_base,
2397 unsigned long *mappable_end)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002398{
2399 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002400 unsigned int gtt_size;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002401 u16 snb_gmch_ctl;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002402 int ret;
2403
Ben Widawsky41907dd2013-02-08 11:32:47 -08002404 *mappable_base = pci_resource_start(dev->pdev, 2);
2405 *mappable_end = pci_resource_len(dev->pdev, 2);
2406
Ben Widawskybaa09f52013-01-24 13:49:57 -08002407 /* 64/512MB is the current min/max we actually know of, but this is just
2408 * a coarse sanity check.
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002409 */
Ben Widawsky41907dd2013-02-08 11:32:47 -08002410 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
Ben Widawskybaa09f52013-01-24 13:49:57 -08002411 DRM_ERROR("Unknown GMADR size (%lx)\n",
2412 dev_priv->gtt.mappable_end);
2413 return -ENXIO;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002414 }
2415
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002416 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
2417 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
Ben Widawskybaa09f52013-01-24 13:49:57 -08002418 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002419
Ben Widawskyc4ae25e2013-05-01 11:00:34 -07002420 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002421
Ben Widawsky63340132013-11-04 19:32:22 -08002422 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
Michel Thierry07749ef2015-03-16 16:00:54 +00002423 *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002424
Ben Widawsky63340132013-11-04 19:32:22 -08002425 ret = ggtt_probe_common(dev, gtt_size);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002426
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002427 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2428 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002429 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
2430 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002431
2432 return ret;
2433}
2434
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002435static void gen6_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002436{
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002437
2438 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
Ben Widawsky5ed16782013-11-25 09:54:43 -08002439
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002440 iounmap(gtt->gsm);
2441 teardown_scratch_page(vm->dev);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002442}
2443
2444static int i915_gmch_probe(struct drm_device *dev,
2445 size_t *gtt_total,
Ben Widawsky41907dd2013-02-08 11:32:47 -08002446 size_t *stolen,
2447 phys_addr_t *mappable_base,
2448 unsigned long *mappable_end)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002449{
2450 struct drm_i915_private *dev_priv = dev->dev_private;
2451 int ret;
2452
Ben Widawskybaa09f52013-01-24 13:49:57 -08002453 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2454 if (!ret) {
2455 DRM_ERROR("failed to set up gmch\n");
2456 return -EIO;
2457 }
2458
Ben Widawsky41907dd2013-02-08 11:32:47 -08002459 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
Ben Widawskybaa09f52013-01-24 13:49:57 -08002460
2461 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002462 dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002463 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
Daniel Vetterd369d2d2015-04-14 17:35:25 +02002464 dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
2465 dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002466
Chris Wilsonc0a7f812013-12-30 12:16:15 +00002467 if (unlikely(dev_priv->gtt.do_idle_maps))
2468 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2469
Ben Widawskybaa09f52013-01-24 13:49:57 -08002470 return 0;
2471}
2472
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002473static void i915_gmch_remove(struct i915_address_space *vm)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002474{
2475 intel_gmch_remove();
2476}
2477
2478int i915_gem_gtt_init(struct drm_device *dev)
2479{
2480 struct drm_i915_private *dev_priv = dev->dev_private;
2481 struct i915_gtt *gtt = &dev_priv->gtt;
Ben Widawskybaa09f52013-01-24 13:49:57 -08002482 int ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002483
Ben Widawskybaa09f52013-01-24 13:49:57 -08002484 if (INTEL_INFO(dev)->gen <= 5) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002485 gtt->gtt_probe = i915_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002486 gtt->base.cleanup = i915_gmch_remove;
Ben Widawsky63340132013-11-04 19:32:22 -08002487 } else if (INTEL_INFO(dev)->gen < 8) {
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002488 gtt->gtt_probe = gen6_gmch_probe;
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002489 gtt->base.cleanup = gen6_gmch_remove;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002490 if (IS_HASWELL(dev) && dev_priv->ellc_size)
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002491 gtt->base.pte_encode = iris_pte_encode;
Ben Widawsky4d15c142013-07-04 11:02:06 -07002492 else if (IS_HASWELL(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002493 gtt->base.pte_encode = hsw_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002494 else if (IS_VALLEYVIEW(dev))
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002495 gtt->base.pte_encode = byt_pte_encode;
Chris Wilson350ec882013-08-06 13:17:02 +01002496 else if (INTEL_INFO(dev)->gen >= 7)
2497 gtt->base.pte_encode = ivb_pte_encode;
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002498 else
Chris Wilson350ec882013-08-06 13:17:02 +01002499 gtt->base.pte_encode = snb_pte_encode;
Ben Widawsky63340132013-11-04 19:32:22 -08002500 } else {
2501 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2502 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002503 }
2504
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002505 ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002506 &gtt->mappable_base, &gtt->mappable_end);
Ben Widawskya54c0c22013-01-24 14:45:00 -08002507 if (ret)
Ben Widawskybaa09f52013-01-24 13:49:57 -08002508 return ret;
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002509
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002510 gtt->base.dev = dev;
2511
Ben Widawskybaa09f52013-01-24 13:49:57 -08002512 /* GMADR is the PCI mmio aperture into the global GTT. */
Ben Widawsky853ba5d2013-07-16 16:50:05 -07002513 DRM_INFO("Memory usable by graphics device = %zdM\n",
2514 gtt->base.total >> 20);
Ben Widawskyb2f21b42013-06-27 16:30:20 -07002515 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2516 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
Daniel Vetter5db6c732014-03-31 16:23:04 +02002517#ifdef CONFIG_INTEL_IOMMU
2518 if (intel_iommu_gfx_mapped)
2519 DRM_INFO("VT-d active for gfx access\n");
2520#endif
Daniel Vettercfa7c862014-04-29 11:53:58 +02002521 /*
2522 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2523 * user's requested state against the hardware/driver capabilities. We
2524 * do this now so that we can print out any log messages once rather
2525 * than every time we check intel_enable_ppgtt().
2526 */
2527 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2528 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
Daniel Vetter7faf1ab2013-01-24 14:44:55 -08002529
Ben Widawskye76e9ae2012-11-04 09:21:27 -08002530 return 0;
Daniel Vetter644ec022012-03-26 09:45:40 +02002531}
Ben Widawsky6f65e292013-12-06 14:10:56 -08002532
Daniel Vetterfa423312015-04-14 17:35:23 +02002533void i915_gem_restore_gtt_mappings(struct drm_device *dev)
2534{
2535 struct drm_i915_private *dev_priv = dev->dev_private;
2536 struct drm_i915_gem_object *obj;
2537 struct i915_address_space *vm;
2538
2539 i915_check_and_clear_faults(dev);
2540
2541 /* First fill our portion of the GTT with scratch pages */
2542 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
2543 dev_priv->gtt.base.start,
2544 dev_priv->gtt.base.total,
2545 true);
2546
2547 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
2548 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
2549 &dev_priv->gtt.base);
2550 if (!vma)
2551 continue;
2552
2553 i915_gem_clflush_object(obj, obj->pin_display);
2554 WARN_ON(i915_vma_bind(vma, obj->cache_level, PIN_UPDATE));
2555 }
2556
2557
2558 if (INTEL_INFO(dev)->gen >= 8) {
2559 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
2560 chv_setup_private_ppat(dev_priv);
2561 else
2562 bdw_setup_private_ppat(dev_priv);
2563
2564 return;
2565 }
2566
2567 if (USES_PPGTT(dev)) {
2568 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
2569 /* TODO: Perhaps it shouldn't be gen6 specific */
2570
2571 struct i915_hw_ppgtt *ppgtt =
2572 container_of(vm, struct i915_hw_ppgtt,
2573 base);
2574
2575 if (i915_is_ggtt(vm))
2576 ppgtt = dev_priv->mm.aliasing_ppgtt;
2577
2578 gen6_write_page_range(dev_priv, &ppgtt->pd,
2579 0, ppgtt->base.total);
2580 }
2581 }
2582
2583 i915_ggtt_flush(dev_priv);
2584}
2585
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002586static struct i915_vma *
2587__i915_gem_vma_create(struct drm_i915_gem_object *obj,
2588 struct i915_address_space *vm,
2589 const struct i915_ggtt_view *ggtt_view)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002590{
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002591 struct i915_vma *vma;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002592
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002593 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
2594 return ERR_PTR(-EINVAL);
Chris Wilsone20d2ab2015-04-07 16:20:58 +01002595
2596 vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
Dan Carpenterdabde5c2015-03-18 11:21:58 +03002597 if (vma == NULL)
2598 return ERR_PTR(-ENOMEM);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002599
Ben Widawsky6f65e292013-12-06 14:10:56 -08002600 INIT_LIST_HEAD(&vma->vma_link);
2601 INIT_LIST_HEAD(&vma->mm_list);
2602 INIT_LIST_HEAD(&vma->exec_list);
2603 vma->vm = vm;
2604 vma->obj = obj;
2605
Daniel Vetter777dc5b2015-04-14 17:35:12 +02002606 if (i915_is_ggtt(vm))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002607 vma->ggtt_view = *ggtt_view;
Ben Widawsky6f65e292013-12-06 14:10:56 -08002608
Tvrtko Ursulinf7635662014-12-03 14:59:24 +00002609 list_add_tail(&vma->vma_link, &obj->vma_list);
2610 if (!i915_is_ggtt(vm))
Michel Thierrye07f0552014-08-19 15:49:41 +01002611 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
Ben Widawsky6f65e292013-12-06 14:10:56 -08002612
2613 return vma;
2614}
2615
2616struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002617i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2618 struct i915_address_space *vm)
Ben Widawsky6f65e292013-12-06 14:10:56 -08002619{
2620 struct i915_vma *vma;
2621
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002622 vma = i915_gem_obj_to_vma(obj, vm);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002623 if (!vma)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002624 vma = __i915_gem_vma_create(obj, vm,
2625 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
Ben Widawsky6f65e292013-12-06 14:10:56 -08002626
2627 return vma;
2628}
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002629
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002630struct i915_vma *
2631i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
2632 const struct i915_ggtt_view *view)
2633{
2634 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
2635 struct i915_vma *vma;
2636
2637 if (WARN_ON(!view))
2638 return ERR_PTR(-EINVAL);
2639
2640 vma = i915_gem_obj_to_ggtt_view(obj, view);
2641
2642 if (IS_ERR(vma))
2643 return vma;
2644
2645 if (!vma)
2646 vma = __i915_gem_vma_create(obj, ggtt, view);
2647
2648 return vma;
2649
2650}
2651
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002652static void
2653rotate_pages(dma_addr_t *in, unsigned int width, unsigned int height,
2654 struct sg_table *st)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002655{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002656 unsigned int column, row;
2657 unsigned int src_idx;
2658 struct scatterlist *sg = st->sgl;
2659
2660 st->nents = 0;
2661
2662 for (column = 0; column < width; column++) {
2663 src_idx = width * (height - 1) + column;
2664 for (row = 0; row < height; row++) {
2665 st->nents++;
2666 /* We don't need the pages, but need to initialize
2667 * the entries so the sg list can be happily traversed.
2668 * The only thing we need are DMA addresses.
2669 */
2670 sg_set_page(sg, NULL, PAGE_SIZE, 0);
2671 sg_dma_address(sg) = in[src_idx];
2672 sg_dma_len(sg) = PAGE_SIZE;
2673 sg = sg_next(sg);
2674 src_idx -= width;
2675 }
2676 }
2677}
2678
2679static struct sg_table *
2680intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
2681 struct drm_i915_gem_object *obj)
2682{
2683 struct drm_device *dev = obj->base.dev;
2684 struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
2685 unsigned long size, pages, rot_pages;
2686 struct sg_page_iter sg_iter;
2687 unsigned long i;
2688 dma_addr_t *page_addr_list;
2689 struct sg_table *st;
2690 unsigned int tile_pitch, tile_height;
2691 unsigned int width_pages, height_pages;
Tvrtko Ursulin1d00dad2015-03-25 10:15:26 +00002692 int ret = -ENOMEM;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002693
2694 pages = obj->base.size / PAGE_SIZE;
2695
2696 /* Calculate tiling geometry. */
2697 tile_height = intel_tile_height(dev, rot_info->pixel_format,
2698 rot_info->fb_modifier);
2699 tile_pitch = PAGE_SIZE / tile_height;
2700 width_pages = DIV_ROUND_UP(rot_info->pitch, tile_pitch);
2701 height_pages = DIV_ROUND_UP(rot_info->height, tile_height);
2702 rot_pages = width_pages * height_pages;
2703 size = rot_pages * PAGE_SIZE;
2704
2705 /* Allocate a temporary list of source pages for random access. */
2706 page_addr_list = drm_malloc_ab(pages, sizeof(dma_addr_t));
2707 if (!page_addr_list)
2708 return ERR_PTR(ret);
2709
2710 /* Allocate target SG list. */
2711 st = kmalloc(sizeof(*st), GFP_KERNEL);
2712 if (!st)
2713 goto err_st_alloc;
2714
2715 ret = sg_alloc_table(st, rot_pages, GFP_KERNEL);
2716 if (ret)
2717 goto err_sg_alloc;
2718
2719 /* Populate source page list from the object. */
2720 i = 0;
2721 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
2722 page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
2723 i++;
2724 }
2725
2726 /* Rotate the pages. */
2727 rotate_pages(page_addr_list, width_pages, height_pages, st);
2728
2729 DRM_DEBUG_KMS(
2730 "Created rotated page mapping for object size %lu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages).\n",
2731 size, rot_info->pitch, rot_info->height,
2732 rot_info->pixel_format, width_pages, height_pages,
2733 rot_pages);
2734
2735 drm_free_large(page_addr_list);
2736
2737 return st;
2738
2739err_sg_alloc:
2740 kfree(st);
2741err_st_alloc:
2742 drm_free_large(page_addr_list);
2743
2744 DRM_DEBUG_KMS(
2745 "Failed to create rotated mapping for object size %lu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages)\n",
2746 size, ret, rot_info->pitch, rot_info->height,
2747 rot_info->pixel_format, width_pages, height_pages,
2748 rot_pages);
2749 return ERR_PTR(ret);
2750}
2751
2752static inline int
2753i915_get_ggtt_vma_pages(struct i915_vma *vma)
2754{
2755 int ret = 0;
2756
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002757 if (vma->ggtt_view.pages)
2758 return 0;
2759
2760 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
2761 vma->ggtt_view.pages = vma->obj->pages;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002762 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
2763 vma->ggtt_view.pages =
2764 intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002765 else
2766 WARN_ONCE(1, "GGTT view %u not implemented!\n",
2767 vma->ggtt_view.type);
2768
2769 if (!vma->ggtt_view.pages) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002770 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002771 vma->ggtt_view.type);
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002772 ret = -EINVAL;
2773 } else if (IS_ERR(vma->ggtt_view.pages)) {
2774 ret = PTR_ERR(vma->ggtt_view.pages);
2775 vma->ggtt_view.pages = NULL;
2776 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
2777 vma->ggtt_view.type, ret);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002778 }
2779
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002780 return ret;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002781}
2782
2783/**
2784 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
2785 * @vma: VMA to map
2786 * @cache_level: mapping cache level
2787 * @flags: flags like global or local mapping
2788 *
2789 * DMA addresses are taken from the scatter-gather table of this object (or of
2790 * this VMA in case of non-default GGTT views) and PTE entries set up.
2791 * Note that DMA addresses are also the only part of the SG table we care about.
2792 */
2793int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
2794 u32 flags)
2795{
Daniel Vetter08755462015-04-20 09:04:05 -07002796 u32 bind_flags = 0;
Mika Kuoppala1d335d12015-04-10 15:54:58 +03002797 int ret;
2798
2799 if (vma->vm->allocate_va_range) {
2800 trace_i915_va_alloc(vma->vm, vma->node.start,
2801 vma->node.size,
2802 VM_TO_TRACE_NAME(vma->vm));
2803
2804 ret = vma->vm->allocate_va_range(vma->vm,
2805 vma->node.start,
2806 vma->node.size);
2807 if (ret)
2808 return ret;
2809 }
2810
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002811 if (i915_is_ggtt(vma->vm)) {
Mika Kuoppala1d335d12015-04-10 15:54:58 +03002812 ret = i915_get_ggtt_vma_pages(vma);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002813 if (ret)
Daniel Vetter08755462015-04-20 09:04:05 -07002814 return 0;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02002815 }
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002816
Daniel Vetter08755462015-04-20 09:04:05 -07002817 if (flags & PIN_GLOBAL)
2818 bind_flags |= GLOBAL_BIND;
2819 if (flags & PIN_USER)
2820 bind_flags |= LOCAL_BIND;
2821
2822 if (flags & PIN_UPDATE)
2823 bind_flags |= vma->bound;
2824 else
2825 bind_flags &= ~vma->bound;
2826
2827 if (bind_flags)
2828 vma->vm->bind_vma(vma, cache_level, bind_flags);
2829
2830 vma->bound |= bind_flags;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002831
2832 return 0;
2833}